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