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