Fix time of check/use Coverity report (and ignore another) in loop/wipe utils.
[platform/upstream/cryptsetup.git] / lib / utils_loop.c
1 /*
2  * loopback block device utilities
3  *
4  * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <sys/ioctl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <linux/loop.h>
30
31 #include "utils_loop.h"
32
33 #define LOOP_DEV_MAJOR 7
34
35 #ifndef LO_FLAGS_AUTOCLEAR
36 #define LO_FLAGS_AUTOCLEAR 4
37 #endif
38
39 #ifndef LOOP_CTL_GET_FREE
40 #define LOOP_CTL_GET_FREE 0x4C82
41 #endif
42
43 static char *crypt_loop_get_device_old(void)
44 {
45         char dev[20];
46         int i, loop_fd;
47         struct loop_info64 lo64 = {0};
48
49         for (i = 0; i < 256; i++) {
50                 sprintf(dev, "/dev/loop%d", i);
51
52                 loop_fd = open(dev, O_RDONLY);
53                 if (loop_fd < 0)
54                         return NULL;
55
56                 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
57                     errno == ENXIO) {
58                         close(loop_fd);
59                         return strdup(dev);
60                 }
61                 close(loop_fd);
62         }
63
64         return NULL;
65 }
66
67 char *crypt_loop_get_device(void)
68 {
69         char dev[64];
70         int i, loop_fd;
71         struct stat st;
72
73         loop_fd = open("/dev/loop-control", O_RDONLY);
74         if (loop_fd < 0)
75                 return crypt_loop_get_device_old();
76
77         i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
78         if (i < 0) {
79                 close(loop_fd);
80                 return NULL;
81         }
82         close(loop_fd);
83
84         if (sprintf(dev, "/dev/loop%d", i) < 0)
85                 return NULL;
86
87         if (stat(dev, &st) || !S_ISBLK(st.st_mode))
88                 return NULL;
89
90         return strdup(dev);
91 }
92
93 int crypt_loop_attach(const char *loop, const char *file, int offset,
94                       int autoclear, int *readonly)
95 {
96         struct loop_info64 lo64 = {0};
97         int loop_fd = -1, file_fd = -1, r = 1;
98
99         file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
100         if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
101                 *readonly = 1;
102                 file_fd = open(file, O_RDONLY | O_EXCL);
103         }
104         if (file_fd < 0)
105                 goto out;
106
107         loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
108         if (loop_fd < 0)
109                 goto out;
110
111         strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
112         lo64.lo_offset = offset;
113         if (autoclear)
114                 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
115
116         if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
117                 goto out;
118
119         if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
120                 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
121                 goto out;
122         }
123
124         /* Verify that autoclear is really set */
125         if (autoclear) {
126                 memset(&lo64, 0, sizeof(lo64));
127                 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
128                    !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
129                 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
130                         goto out;
131                 }
132         }
133
134         r = 0;
135 out:
136         if (r && loop_fd >= 0)
137                 close(loop_fd);
138         if (file_fd >= 0)
139                 close(file_fd);
140         return r ? -1 : loop_fd;
141 }
142
143 int crypt_loop_detach(const char *loop)
144 {
145         int loop_fd = -1, r = 1;
146
147         loop_fd = open(loop, O_RDONLY);
148         if (loop_fd < 0)
149                 return 1;
150
151         if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
152                 r = 0;
153
154         close(loop_fd);
155         return r;
156 }
157
158 static char *_ioctl_backing_file(const char *loop)
159 {
160         struct loop_info64 lo64 = {0};
161         int loop_fd;
162
163         loop_fd = open(loop, O_RDONLY);
164         if (loop_fd < 0)
165                 return NULL;
166
167         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
168                 close(loop_fd);
169                 return NULL;
170         }
171
172         lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
173         lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
174
175         close(loop_fd);
176
177         return strdup((char*)lo64.lo_file_name);
178 }
179
180 static char *_sysfs_backing_file(const char *loop)
181 {
182         struct stat st;
183         char buf[PATH_MAX];
184         size_t len;
185         int fd;
186
187         if (stat(loop, &st) || !S_ISBLK(st.st_mode))
188                 return NULL;
189
190         snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
191                  major(st.st_rdev), minor(st.st_rdev));
192
193         fd = open(buf, O_RDONLY);
194         if (fd < 0)
195                 return NULL;
196
197         len = read(fd, buf, PATH_MAX);
198         close(fd);
199         if (len < 2)
200                 return NULL;
201
202         buf[len - 1] = '\0';
203         return strdup(buf);
204 }
205
206 char *crypt_loop_backing_file(const char *loop)
207 {
208         char *bf = _sysfs_backing_file(loop);
209         return bf ?: _ioctl_backing_file(loop);
210 }
211
212 int crypt_loop_device(const char *loop)
213 {
214         struct stat st;
215
216         if (!loop)
217                 return 0;
218
219         if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
220             major(st.st_rdev) != LOOP_DEV_MAJOR)
221                 return 0;
222
223         return 1;
224 }