7809216cf67a41ef2eeec7d144a5473d25f3c371
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <stdarg.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/sysmacros.h>
9 #include <sys/ioctl.h>
10 #include <unistd.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #include <libdevmapper.h>
14 #include <fcntl.h>
15 #include <linux/fs.h>
16
17 #include "libcryptsetup.h"
18 #include "internal.h"
19
20 #define DEVICE_DIR      "/dev"
21 #define UUID_PREFIX     "CRYPT-"
22 #define CRYPT_TARGET    "crypt"
23 #define RETRY_COUNT     5
24
25 static void set_dm_error(int level, const char *file, int line,
26                          const char *f, ...)
27 {
28         va_list va;
29
30         if (level > 3)
31                 return;
32
33         va_start(va, f);
34         set_error_va(f, va);
35         va_end(va);
36 }
37
38 static int _dm_simple(int task, const char *name);
39
40 static int dm_init(void)
41 {
42         dm_log_init(set_dm_error);
43         if (!_dm_simple(DM_DEVICE_LIST_VERSIONS, "test")) {
44                 set_error("Cannot communicate with device-mapper. Is the dm_mod module loaded?");
45                 return -1;
46         }
47
48         return 1;       /* unsafe memory */
49 }
50
51 static void dm_exit(void)
52 {
53         dm_log_init(NULL);
54         dm_lib_release();
55 }
56
57 static char *__lookup_dev(char *path, dev_t dev)
58 {
59         struct dirent *entry;
60         struct stat st;
61         char *ptr;
62         char *result = NULL;
63         DIR *dir;
64         int space;
65
66         path[PATH_MAX - 1] = '\0';
67         ptr = path + strlen(path);
68         *ptr++ = '/';
69         *ptr = '\0';
70         space = PATH_MAX - (ptr - path);
71
72         dir = opendir(path);
73         if (!dir)
74                 return NULL;
75
76         while((entry = readdir(dir))) {
77                 if (entry->d_name[0] == '.' &&
78                     (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
79                                                   entry->d_name[2] == '\0')))
80                         continue;
81
82                 strncpy(ptr, entry->d_name, space);
83                 if (lstat(path, &st) < 0)
84                         continue;
85
86                 if (S_ISDIR(st.st_mode)) {
87                         result = __lookup_dev(path, dev);
88                         if (result)
89                                 break;
90                 } else if (S_ISBLK(st.st_mode)) {
91                         if (st.st_rdev == dev) {
92                                 result = strdup(path);
93                                 break;
94                         }
95                 }
96         }
97
98         closedir(dir);
99
100         return result;
101 }
102
103 static char *lookup_dev(const char *dev)
104 {
105         uint32_t major, minor;
106         char buf[PATH_MAX + 1];
107
108         if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
109                 return NULL;
110
111         strncpy(buf, DEVICE_DIR, PATH_MAX);
112         buf[PATH_MAX] = '\0';
113
114         return __lookup_dev(buf, makedev(major, minor));
115 }
116
117 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
118 {
119         int fd, r = 0;
120         long read_ahead_long;
121
122         if ((fd = open(dev, O_RDONLY)) < 0)
123                 return 0;
124
125         r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
126         close(fd);
127
128         if (r)
129                 *read_ahead = (uint32_t) read_ahead_long;
130
131         return r;
132 }
133
134 static char *get_params(struct crypt_options *options, const char *key)
135 {
136         char *params;
137         char *hexkey;
138         int i;
139
140         hexkey = safe_alloc(options->key_size * 2 + 1);
141         if (!hexkey) {
142                 set_error("Memory allocation problem");
143                 return NULL;
144         }
145
146         for(i = 0; i < options->key_size; i++)
147                 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
148
149         params = safe_alloc(strlen(hexkey) + strlen(options->cipher) +
150                             strlen(options->device) + 64);
151         if (!params) {
152                 set_error("Memory allocation problem");
153                 goto out;
154         }
155
156         sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
157                 options->cipher, hexkey, options->skip,
158                 options->device, options->offset);
159
160 out:
161         safe_free(hexkey);
162
163         return params;
164 }
165
166 /* DM helpers */
167 static int _dm_simple(int task, const char *name)
168 {
169         int r = 0;
170         struct dm_task *dmt;
171
172         if (!(dmt = dm_task_create(task)))
173                 return 0;
174
175         if (!dm_task_set_name(dmt, name))
176                 goto out;
177
178         r = dm_task_run(dmt);
179
180       out:
181         dm_task_destroy(dmt);
182         return r;
183 }
184
185 static int _error_device(struct crypt_options *options)
186 {
187         struct dm_task *dmt;
188         int r = 0;
189
190         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
191                 return 0;
192
193         if (!dm_task_set_name(dmt, options->name))
194                 goto error;
195
196         if (!dm_task_add_target(dmt, UINT64_C(0), options->size, "error", ""))
197                 goto error;
198
199         if (!dm_task_set_ro(dmt))
200                 goto error;
201
202         if (!dm_task_no_open_count(dmt))
203                 goto error;
204
205         if (!dm_task_run(dmt))
206                 goto error;
207
208         if (!_dm_simple(DM_DEVICE_RESUME, options->name)) {
209                 _dm_simple(DM_DEVICE_CLEAR, options->name);
210                 goto error;
211         }
212
213         r = 1;
214
215 error:
216         dm_task_destroy(dmt);
217         return r;
218 }
219
220 static int _dm_remove(struct crypt_options *options, int force)
221 {
222         int r = -EINVAL;
223         int retries = force ? RETRY_COUNT : 1;
224
225         /* If force flag is set, replace device with error, read-only target.
226          * it should stop processes from reading it and also removed underlying
227          * device from mapping, so it is usable again.
228          * Force flag should be used only for temporary devices, which are
229          * intended to work inside cryptsetup only!
230          * Anyway, if some process try to read temporary cryptsetup device,
231          * it is bug - no other process should try touch it (e.g. udev).
232          */
233         if (force) {
234                  _error_device(options);
235                 retries = RETRY_COUNT;
236         }
237
238         do {
239                 r = _dm_simple(DM_DEVICE_REMOVE, options->name) ? 0 : -EINVAL;
240                 if (--retries)
241                         sleep(1);
242         } while (r == -EINVAL && retries);
243
244         dm_task_update_nodes();
245
246         return r;
247 }
248
249 static int dm_create_device(int reload, struct crypt_options *options,
250                             const char *key, const char *uuid)
251 {
252         struct dm_task *dmt = NULL;
253         struct dm_task *dmt_query = NULL;
254         struct dm_info dmi;
255         char *params = NULL;
256         char *error = NULL;
257         char dev_uuid[64];
258         int r = -EINVAL;
259         uint32_t read_ahead = 0;
260
261         params = get_params(options, key);
262         if (!params)
263                 goto out_no_removal;
264  
265         if (uuid) {
266                 strcpy(dev_uuid, UUID_PREFIX);
267                 strcat(dev_uuid, uuid);
268         }
269
270         if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
271                                           : DM_DEVICE_CREATE)))
272                 goto out;
273         if (!dm_task_set_name(dmt, options->name))
274                 goto out;
275         if (options->flags & CRYPT_FLAG_READONLY && !dm_task_set_ro(dmt))
276                 goto out;
277         if (!dm_task_add_target(dmt, 0, options->size, CRYPT_TARGET, params))
278                 goto out;
279
280 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
281         if (_dev_read_ahead(options->device, &read_ahead) &&
282             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
283                 goto out;
284 #endif
285
286         if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
287                 goto out;
288
289         if (!dm_task_run(dmt))
290                 goto out;
291
292         if (reload) {
293                 dm_task_destroy(dmt);
294                 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
295                         goto out;
296                 if (!dm_task_set_name(dmt, options->name))
297                         goto out;
298                 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
299                         goto out;
300                 if (!dm_task_run(dmt))
301                         goto out;
302         }
303
304         if (!dm_task_get_info(dmt, &dmi))
305                 goto out;
306         if (dmi.read_only)
307                 options->flags |= CRYPT_FLAG_READONLY;
308
309         r = 0;
310 out:
311         if (r < 0 && !reload) {
312                 if (get_error())
313                         error = strdup(get_error());
314
315                 _dm_remove(options, 0);
316
317                 if (error) {
318                         set_error(error);
319                         free(error);
320                 }
321         }
322
323 out_no_removal:
324         if (params)
325                 safe_free(params);
326         if (dmt)
327                 dm_task_destroy(dmt);
328         if(dmt_query)
329                 dm_task_destroy(dmt_query);
330         dm_task_update_nodes();
331         return r;
332 }
333
334 static int dm_query_device(int details, struct crypt_options *options,
335                            char **key)
336 {
337         struct dm_task *dmt;
338         struct dm_info dmi;
339         uint64_t start, length;
340         char *target_type, *params;
341         void *next = NULL;
342         int r = -EINVAL;
343
344         if (!(dmt = dm_task_create(details ? DM_DEVICE_TABLE
345                                            : DM_DEVICE_STATUS)))
346                 goto out;
347         if (!dm_task_set_name(dmt, options->name))
348                 goto out;
349         r = -ENODEV;
350         if (!dm_task_run(dmt))
351                 goto out;
352
353         r = -EINVAL;
354         if (!dm_task_get_info(dmt, &dmi))
355                 goto out;
356
357         if (!dmi.exists) {
358                 r = -ENODEV;
359                 goto out;
360         }
361
362         next = dm_get_next_target(dmt, next, &start, &length,
363                                   &target_type, &params);
364         if (!target_type || strcmp(target_type, CRYPT_TARGET) != 0 ||
365             start != 0 || next)
366                 goto out;
367
368         options->hash = NULL;
369         options->cipher = NULL;
370         options->offset = 0;
371         options->skip = 0;
372         options->size = length;
373         if (details) {
374                 char *cipher, *key_, *device;
375                 uint64_t val64;
376
377                 set_error("Invalid dm table");
378
379                 cipher = strsep(&params, " ");
380                 key_ = strsep(&params, " ");
381                 if (!params)
382                         goto out;
383
384                 val64 = strtoull(params, &params, 10);
385                 if (*params != ' ')
386                         goto out;
387                 params++;
388                 options->skip = val64;
389
390                 device = strsep(&params, " ");
391                 if (!params)
392                         goto out;
393
394                 val64 = strtoull(params, &params, 10);
395                 if (*params)
396                         goto out;
397                 options->offset = val64;
398
399                 options->cipher = strdup(cipher);
400                 options->key_size = strlen(key_) / 2;
401                 if (key) {
402                         char buffer[3];
403                         char *endp;
404                         int i;
405
406                         *key = safe_alloc(options->key_size);
407                         if (!*key) {
408                                 set_error("Out of memory");
409                                 r = -ENOMEM;
410                                 goto out;
411                         }
412
413                         buffer[2] = '\0';
414                         for(i = 0; i < options->key_size; i++) {
415                                 memcpy(buffer, &key_[i * 2], 2);
416                                 (*key)[i] = strtoul(buffer, &endp, 16);
417                                 if (endp != &buffer[2]) {
418                                         safe_free(key);
419                                         *key = NULL;
420                                         goto out;
421                                 }
422                         }
423                 }
424                 memset(key_, 0, strlen(key_));
425                 options->device = lookup_dev(device);
426
427                 set_error(NULL);
428         }
429
430         r = (dmi.open_count > 0);
431
432 out:
433         if (dmt)
434                 dm_task_destroy(dmt);
435         if (r >= 0) {
436                 if (options->device)
437                         options->flags |= CRYPT_FLAG_FREE_DEVICE;
438                 if (options->cipher)
439                         options->flags |= CRYPT_FLAG_FREE_CIPHER;
440                 options->flags &= ~CRYPT_FLAG_READONLY;
441                 if (dmi.read_only)
442                         options->flags |= CRYPT_FLAG_READONLY;
443         } else {
444                 if (options->device) {
445                         free((char *)options->device);
446                         options->device = NULL;
447                         options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
448                 }
449                 if (options->cipher) {
450                         free((char *)options->cipher);
451                         options->cipher = NULL;
452                         options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
453                 }
454         }
455         return r;
456 }
457
458 static int dm_remove_device(int force, struct crypt_options *options)
459 {
460         if (!options || !options->name)
461                 return -EINVAL;
462
463         return _dm_remove(options, force);;
464 }
465
466
467 static const char *dm_get_dir(void)
468 {
469         return dm_dir();
470 }
471
472 struct setup_backend setup_libdevmapper_backend = {
473         .name = "dm-crypt",
474         .init = dm_init,
475         .exit = dm_exit,
476         .create = dm_create_device,
477         .status = dm_query_device,
478         .remove = dm_remove_device,
479         .dir = dm_get_dir
480 };