Better detects dm-ioctl secure flag using version before use.
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
1 #include <sys/ioctl.h>
2 #include <sys/stat.h>
3 #include <stdio.h>
4 #include <dirent.h>
5 #include <errno.h>
6 #include <libdevmapper.h>
7 #include <fcntl.h>
8 #include <linux/fs.h>
9 #include <uuid/uuid.h>
10
11 #include "internal.h"
12 #include "luks.h"
13
14 #define DEVICE_DIR              "/dev"
15 #define DM_UUID_LEN             129
16 #define DM_UUID_PREFIX          "CRYPT-"
17 #define DM_UUID_PREFIX_LEN      6
18 #define DM_CRYPT_TARGET         "crypt"
19 #define RETRY_COUNT             5
20
21 /* Set if dm-crypt version was probed */
22 static int _dm_crypt_checked = 0;
23 static uint32_t _dm_crypt_flags = 0;
24
25 static int _dm_use_count = 0;
26 static struct crypt_device *_context = NULL;
27
28 /* Check if we have DM flag to instruct kernel to force wipe buffers */
29 #if !HAVE_DECL_DM_TASK_SECURE_DATA
30 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
31 #endif
32
33 /* Compatibility for old device-mapper without udev support */
34 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
35 #define CRYPT_TEMP_UDEV_FLAGS   DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
36                                 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
37                                 DM_UDEV_DISABLE_OTHER_RULES_FLAG
38 #define _dm_task_set_cookie     dm_task_set_cookie
39 #define _dm_udev_wait           dm_udev_wait
40 #else
41 #define CRYPT_TEMP_UDEV_FLAGS   0
42 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
43 static int _dm_udev_wait(uint32_t cookie) { return 0; };
44 #endif
45
46 static int _dm_use_udev()
47 {
48 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
49         return dm_udev_get_sync_support();
50 #else
51         return 0;
52 #endif
53 }
54
55 uint32_t dm_flags(void)
56 {
57         return _dm_crypt_flags;
58 }
59
60 static void set_dm_error(int level, const char *file, int line,
61                          const char *f, ...)
62 {
63         char *msg = NULL;
64         va_list va;
65
66         va_start(va, f);
67         if (vasprintf(&msg, f, va) > 0) {
68                 if (level < 4) {
69                         log_err(_context, msg);
70                         log_err(_context, "\n");
71                 } else
72                         log_dbg(msg);
73         }
74         free(msg);
75         va_end(va);
76 }
77
78 static int _dm_simple(int task, const char *name, int udev_wait);
79
80 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
81                                  unsigned crypt_min, unsigned crypt_patch)
82 {
83         unsigned dm_maj, dm_min, dm_patch;
84
85         if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
86                 dm_maj = dm_min = dm_patch = 0;
87
88         log_dbg("Detected dm-crypt version %i.%i.%i, dm-ioctl version %u.%u.%u.",
89                 crypt_maj, crypt_min, crypt_patch, dm_maj, dm_min, dm_patch);
90
91         if (crypt_maj >= 1 && crypt_min >= 2)
92                 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
93         else
94                 log_dbg("Suspend and resume disabled, no wipe key support.");
95
96         if (crypt_maj >= 1 && crypt_min >= 10)
97                 _dm_crypt_flags |= DM_LMK_SUPPORTED;
98
99         if (dm_maj >= 4 && dm_min >= 20)
100                 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
101
102         _dm_crypt_checked = 1;
103 }
104
105 static int _dm_check_versions(void)
106 {
107         struct dm_task *dmt;
108         struct dm_versions *target, *last_target;
109         char dm_version[16];
110
111         if (_dm_crypt_checked)
112                 return 1;
113
114         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
115                 return 0;
116
117         if (!dm_task_run(dmt)) {
118                 dm_task_destroy(dmt);
119                 return 0;
120         }
121
122         if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
123                 dm_task_destroy(dmt);
124                 return 0;
125         }
126
127         target = dm_task_get_versions(dmt);
128         do {
129                 last_target = target;
130                 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
131                         _dm_set_crypt_compat(dm_version,
132                                              (unsigned)target->version[0],
133                                              (unsigned)target->version[1],
134                                              (unsigned)target->version[2]);
135                 }
136                 target = (void *) target + target->next;
137         } while (last_target != target);
138
139         dm_task_destroy(dmt);
140         return 1;
141 }
142
143 int dm_init(struct crypt_device *context, int check_kernel)
144 {
145         if (!_dm_use_count++) {
146                 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
147                         check_kernel ? "" : " (NO kernel check requested)",
148                         _dm_use_udev() ? "en" : "dis");
149                 if (check_kernel && !_dm_check_versions()) {
150                         log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
151                         return -1;
152                 }
153                 if (getuid() || geteuid())
154                         log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
155                 dm_log_init(set_dm_error);
156                 dm_log_init_verbose(10);
157         }
158
159         // FIXME: global context is not safe
160         if (context)
161                 _context = context;
162
163         return 1;       /* unsafe memory */
164 }
165
166 void dm_exit(void)
167 {
168         if (_dm_use_count && (!--_dm_use_count)) {
169                 log_dbg("Releasing device-mapper backend.");
170                 dm_log_init_verbose(0);
171                 dm_log_init(NULL);
172                 dm_lib_release();
173                 _context = NULL;
174         }
175 }
176
177 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
178 {
179         struct dirent *entry;
180         struct stat st;
181         char *ptr;
182         char *result = NULL;
183         DIR *dir;
184         int space;
185
186         /* Ignore strange nested directories */
187         if (dir_level > max_level)
188                 return NULL;
189
190         path[PATH_MAX - 1] = '\0';
191         ptr = path + strlen(path);
192         *ptr++ = '/';
193         *ptr = '\0';
194         space = PATH_MAX - (ptr - path);
195
196         dir = opendir(path);
197         if (!dir)
198                 return NULL;
199
200         while((entry = readdir(dir))) {
201                 if (entry->d_name[0] == '.' ||
202                     !strncmp(entry->d_name, "..", 2))
203                         continue;
204
205                 strncpy(ptr, entry->d_name, space);
206                 if (stat(path, &st) < 0)
207                         continue;
208
209                 if (S_ISDIR(st.st_mode)) {
210                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
211                         if (result)
212                                 break;
213                 } else if (S_ISBLK(st.st_mode)) {
214                         /* workaround: ignore dm-X devices, these are internal kernel names */
215                         if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
216                                 continue;
217                         if (st.st_rdev == dev) {
218                                 result = strdup(path);
219                                 break;
220                         }
221                 }
222         }
223
224         closedir(dir);
225         return result;
226 }
227
228 static char *lookup_dev(const char *dev_id)
229 {
230         uint32_t major, minor;
231         dev_t dev;
232         char *result = NULL, buf[PATH_MAX + 1];
233
234         if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
235                 return NULL;
236
237         dev = makedev(major, minor);
238         strncpy(buf, DEVICE_DIR, PATH_MAX);
239         buf[PATH_MAX] = '\0';
240
241         /* First try low level device */
242         if ((result = __lookup_dev(buf, dev, 0, 0)))
243                 return result;
244
245         /* If it is dm, try DM dir  */
246         if (dm_is_dm_major(major)) {
247                 strncpy(buf, dm_dir(), PATH_MAX);
248                 if ((result = __lookup_dev(buf, dev, 0, 0)))
249                         return result;
250         }
251
252         strncpy(buf, DEVICE_DIR, PATH_MAX);
253         result = __lookup_dev(buf, dev, 0, 4);
254
255         /* If not found, return NULL */
256         return result;
257 }
258
259 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
260 {
261         int fd, r = 0;
262         long read_ahead_long;
263
264         if ((fd = open(dev, O_RDONLY)) < 0)
265                 return 0;
266
267         r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
268         close(fd);
269
270         if (r)
271                 *read_ahead = (uint32_t) read_ahead_long;
272
273         return r;
274 }
275
276 static void hex_key(char *hexkey, size_t key_size, const char *key)
277 {
278         int i;
279
280         for(i = 0; i < key_size; i++)
281                 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
282 }
283
284 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
285                         const char *cipher, size_t key_size, const char *key)
286 {
287         char *params;
288         char *hexkey;
289
290         hexkey = crypt_safe_alloc(key_size * 2 + 1);
291         if (!hexkey)
292                 return NULL;
293
294         hex_key(hexkey, key_size, key);
295
296         params = crypt_safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
297         if (!params)
298                 goto out;
299
300         sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
301                 cipher, hexkey, skip, device, offset);
302
303 out:
304         crypt_safe_free(hexkey);
305         return params;
306 }
307
308 /* DM helpers */
309 static int _dm_simple(int task, const char *name, int udev_wait)
310 {
311         int r = 0;
312         struct dm_task *dmt;
313         uint32_t cookie = 0;
314
315         if (!_dm_use_udev())
316                 udev_wait = 0;
317
318         if (!(dmt = dm_task_create(task)))
319                 return 0;
320
321         if (name && !dm_task_set_name(dmt, name))
322                 goto out;
323
324         if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
325                 goto out;
326
327         r = dm_task_run(dmt);
328
329         if (udev_wait)
330                 (void)_dm_udev_wait(cookie);
331
332       out:
333         dm_task_destroy(dmt);
334         return r;
335 }
336
337 static int _error_device(const char *name, size_t size)
338 {
339         struct dm_task *dmt;
340         int r = 0;
341
342         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
343                 return 0;
344
345         if (!dm_task_set_name(dmt, name))
346                 goto error;
347
348         if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
349                 goto error;
350
351         if (!dm_task_set_ro(dmt))
352                 goto error;
353
354         if (!dm_task_no_open_count(dmt))
355                 goto error;
356
357         if (!dm_task_run(dmt))
358                 goto error;
359
360         if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
361                 _dm_simple(DM_DEVICE_CLEAR, name, 0);
362                 goto error;
363         }
364
365         r = 1;
366
367 error:
368         dm_task_destroy(dmt);
369         return r;
370 }
371
372 int dm_remove_device(const char *name, int force, uint64_t size)
373 {
374         int r = -EINVAL;
375         int retries = force ? RETRY_COUNT : 1;
376         int error_target = 0;
377
378         if (!name || (force && !size))
379                 return -EINVAL;
380
381         do {
382                 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
383                 if (--retries && r) {
384                         log_dbg("WARNING: other process locked internal device %s, %s.",
385                                 name, retries ? "retrying remove" : "giving up");
386                         if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
387                                 debug_processes_using_device(name);
388                         sleep(1);
389                         if (force && !error_target) {
390                                 /* If force flag is set, replace device with error, read-only target.
391                                  * it should stop processes from reading it and also removed underlying
392                                  * device from mapping, so it is usable again.
393                                  * Force flag should be used only for temporary devices, which are
394                                  * intended to work inside cryptsetup only!
395                                  * Anyway, if some process try to read temporary cryptsetup device,
396                                  * it is bug - no other process should try touch it (e.g. udev).
397                                  */
398                                 _error_device(name, size);
399                                 error_target = 1;
400                         }
401                 }
402         } while (r == -EINVAL && retries);
403
404         dm_task_update_nodes();
405
406         return r;
407 }
408
409 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
410 /*
411  * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
412  * CRYPT-PLAIN-name
413  * CRYPT-LUKS1-00000000000000000000000000000000-name
414  * CRYPT-TEMP-name
415  */
416 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
417 {
418         char *ptr, uuid2[UUID_LEN] = {0};
419         uuid_t uu;
420         int i = 0;
421
422         /* Remove '-' chars */
423         if (uuid && !uuid_parse(uuid, uu)) {
424                 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
425                         if (uuid[i] != '-') {
426                                 *ptr = uuid[i];
427                                 ptr++;
428                         }
429         }
430
431         i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
432                 type ?: "", type ? "-" : "",
433                 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
434                 name);
435
436         log_dbg("DM-UUID is %s", buf);
437         if (i >= buflen)
438                 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
439 }
440
441 int dm_create_device(const char *name,
442                      const char *device,
443                      const char *cipher,
444                      const char *type,
445                      const char *uuid,
446                      uint64_t size,
447                      uint64_t skip,
448                      uint64_t offset,
449                      size_t key_size,
450                      const char *key,
451                      int read_only,
452                      int reload)
453 {
454         struct dm_task *dmt = NULL;
455         struct dm_info dmi;
456         char *params = NULL;
457         char *error = NULL;
458         char dev_uuid[DM_UUID_LEN] = {0};
459         int r = -EINVAL;
460         uint32_t read_ahead = 0;
461         uint32_t cookie = 0;
462         uint16_t udev_flags = 0;
463
464         params = get_params(device, skip, offset, cipher, key_size, key);
465         if (!params)
466                 goto out_no_removal;
467
468         if (type && !strncmp(type, "TEMP", 4))
469                 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
470
471         /* All devices must have DM_UUID, only resize on old device is exception */
472         if (reload) {
473                 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
474                         goto out_no_removal;
475
476                 if (!dm_task_set_name(dmt, name))
477                         goto out_no_removal;
478         } else {
479                 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
480
481                 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
482                         goto out_no_removal;
483
484                 if (!dm_task_set_name(dmt, name))
485                         goto out_no_removal;
486
487                 if (!dm_task_set_uuid(dmt, dev_uuid))
488                         goto out_no_removal;
489
490                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
491                         goto out_no_removal;
492         }
493
494         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
495                 goto out_no_removal;
496         if (read_only && !dm_task_set_ro(dmt))
497                 goto out_no_removal;
498         if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
499                 goto out_no_removal;
500
501 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
502         if (_dev_read_ahead(device, &read_ahead) &&
503             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
504                 goto out_no_removal;
505 #endif
506
507         if (!dm_task_run(dmt))
508                 goto out_no_removal;
509
510         if (reload) {
511                 dm_task_destroy(dmt);
512                 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
513                         goto out;
514                 if (!dm_task_set_name(dmt, name))
515                         goto out;
516                 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
517                         goto out;
518                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
519                         goto out;
520                 if (!dm_task_run(dmt))
521                         goto out;
522         }
523
524         if (!dm_task_get_info(dmt, &dmi))
525                 goto out;
526
527         r = 0;
528 out:
529         if (_dm_use_udev()) {
530                 (void)_dm_udev_wait(cookie);
531                 cookie = 0;
532         }
533
534         if (r < 0 && !reload) {
535                 if (get_error())
536                         error = strdup(get_error());
537
538                 dm_remove_device(name, 0, 0);
539
540                 if (error) {
541                         set_error(error);
542                         free(error);
543                 }
544         }
545
546 out_no_removal:
547         if (cookie && _dm_use_udev())
548                 (void)_dm_udev_wait(cookie);
549
550         if (params)
551                 crypt_safe_free(params);
552         if (dmt)
553                 dm_task_destroy(dmt);
554
555         dm_task_update_nodes();
556         return r;
557 }
558
559 int dm_status_device(const char *name)
560 {
561         struct dm_task *dmt;
562         struct dm_info dmi;
563         uint64_t start, length;
564         char *target_type, *params;
565         void *next = NULL;
566         int r = -EINVAL;
567
568         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
569                 goto out;
570
571         if (!dm_task_set_name(dmt, name))
572                 goto out;
573
574         if (!dm_task_run(dmt))
575                 goto out;
576
577         if (!dm_task_get_info(dmt, &dmi))
578                 goto out;
579
580         if (!dmi.exists) {
581                 r = -ENODEV;
582                 goto out;
583         }
584
585         next = dm_get_next_target(dmt, next, &start, &length,
586                                   &target_type, &params);
587         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
588             start != 0 || next)
589                 r = -EINVAL;
590         else
591                 r = (dmi.open_count > 0);
592 out:
593         if (dmt)
594                 dm_task_destroy(dmt);
595
596         return r;
597 }
598
599 int dm_query_device(const char *name,
600                     char **device,
601                     uint64_t *size,
602                     uint64_t *skip,
603                     uint64_t *offset,
604                     char **cipher,
605                     int *key_size,
606                     char **key,
607                     int *read_only,
608                     int *suspended,
609                     char **uuid)
610 {
611         struct dm_task *dmt;
612         struct dm_info dmi;
613         uint64_t start, length, val64;
614         char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
615         void *next = NULL;
616         int i, r = -EINVAL;
617
618         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
619                 goto out;
620         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
621                 goto out;
622         if (!dm_task_set_name(dmt, name))
623                 goto out;
624         r = -ENODEV;
625         if (!dm_task_run(dmt))
626                 goto out;
627
628         r = -EINVAL;
629         if (!dm_task_get_info(dmt, &dmi))
630                 goto out;
631
632         if (!dmi.exists) {
633                 r = -ENODEV;
634                 goto out;
635         }
636
637         next = dm_get_next_target(dmt, next, &start, &length,
638                                   &target_type, &params);
639         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
640             start != 0 || next)
641                 goto out;
642
643         if (size)
644                 *size = length;
645
646         rcipher = strsep(&params, " ");
647         /* cipher */
648         if (cipher)
649                 *cipher = strdup(rcipher);
650
651         /* skip */
652         key_ = strsep(&params, " ");
653         if (!params)
654                 goto out;
655         val64 = strtoull(params, &params, 10);
656         if (*params != ' ')
657                 goto out;
658         params++;
659         if (skip)
660                 *skip = val64;
661
662         /* device */
663         rdevice = strsep(&params, " ");
664         if (device)
665                 *device = lookup_dev(rdevice);
666
667         /*offset */
668         if (!params)
669                 goto out;
670         val64 = strtoull(params, &params, 10);
671         if (*params)
672                 goto out;
673         if (offset)
674                 *offset = val64;
675
676         /* key_size */
677         if (key_size)
678                 *key_size = strlen(key_) / 2;
679
680         /* key */
681         if (key_size && key) {
682                 *key = crypt_safe_alloc(*key_size);
683                 if (!*key) {
684                         r = -ENOMEM;
685                         goto out;
686                 }
687
688                 buffer[2] = '\0';
689                 for(i = 0; i < *key_size; i++) {
690                         memcpy(buffer, &key_[i * 2], 2);
691                         (*key)[i] = strtoul(buffer, &endp, 16);
692                         if (endp != &buffer[2]) {
693                                 crypt_safe_free(key);
694                                 *key = NULL;
695                                 goto out;
696                         }
697                 }
698         }
699         memset(key_, 0, strlen(key_));
700
701         if (read_only)
702                 *read_only = dmi.read_only;
703
704         if (suspended)
705                 *suspended = dmi.suspended;
706
707         if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
708             !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
709                 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
710
711         r = (dmi.open_count > 0);
712 out:
713         if (dmt)
714                 dm_task_destroy(dmt);
715
716         return r;
717 }
718
719 static int _dm_message(const char *name, const char *msg)
720 {
721         int r = 0;
722         struct dm_task *dmt;
723
724         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
725                 return 0;
726
727         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
728                 goto out;
729
730         if (name && !dm_task_set_name(dmt, name))
731                 goto out;
732
733         if (!dm_task_set_sector(dmt, (uint64_t) 0))
734                 goto out;
735
736         if (!dm_task_set_message(dmt, msg))
737                 goto out;
738
739         r = dm_task_run(dmt);
740
741       out:
742         dm_task_destroy(dmt);
743         return r;
744 }
745
746 int dm_suspend_and_wipe_key(const char *name)
747 {
748         if (!_dm_check_versions())
749                 return -ENOTSUP;
750
751         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
752                 return -ENOTSUP;
753
754         if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
755                 return -EINVAL;
756
757         if (!_dm_message(name, "key wipe")) {
758                 _dm_simple(DM_DEVICE_RESUME, name, 1);
759                 return -EINVAL;
760         }
761
762         return 0;
763 }
764
765 int dm_resume_and_reinstate_key(const char *name,
766                                 size_t key_size,
767                                 const char *key)
768 {
769         int msg_size = key_size * 2 + 10; // key set <key>
770         char *msg;
771         int r = 0;
772
773         if (!_dm_check_versions())
774                 return -ENOTSUP;
775
776         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
777                 return -ENOTSUP;
778
779         msg = crypt_safe_alloc(msg_size);
780         if (!msg)
781                 return -ENOMEM;
782
783         memset(msg, 0, msg_size);
784         strcpy(msg, "key set ");
785         hex_key(&msg[8], key_size, key);
786
787         if (!_dm_message(name, msg) ||
788             !_dm_simple(DM_DEVICE_RESUME, name, 1))
789                 r = -EINVAL;
790
791         crypt_safe_free(msg);
792         return r;
793 }
794
795 const char *dm_get_dir(void)
796 {
797         return dm_dir();
798 }