2 * libdevmapper - device-mapper backend for cryptsetup
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <libdevmapper.h>
28 #include <uuid/uuid.h>
32 #define DM_UUID_LEN 129
33 #define DM_UUID_PREFIX "CRYPT-"
34 #define DM_UUID_PREFIX_LEN 6
35 #define DM_CRYPT_TARGET "crypt"
36 #define DM_VERITY_TARGET "verity"
39 /* Set if dm-crypt version was probed */
40 static int _dm_crypt_checked = 0;
41 static uint32_t _dm_crypt_flags = 0;
43 static int _dm_use_count = 0;
44 static struct crypt_device *_context = NULL;
46 /* Check if we have DM flag to instruct kernel to force wipe buffers */
47 #if !HAVE_DECL_DM_TASK_SECURE_DATA
48 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
51 /* Compatibility for old device-mapper without udev support */
52 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
53 #define CRYPT_TEMP_UDEV_FLAGS DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
54 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
55 DM_UDEV_DISABLE_OTHER_RULES_FLAG
56 #define _dm_task_set_cookie dm_task_set_cookie
57 #define _dm_udev_wait dm_udev_wait
59 #define CRYPT_TEMP_UDEV_FLAGS 0
60 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
61 static int _dm_udev_wait(uint32_t cookie) { return 0; };
64 static int _dm_use_udev(void)
66 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
67 return dm_udev_get_sync_support();
73 __attribute__((format(printf, 4, 5)))
74 static void set_dm_error(int level,
75 const char *file __attribute__((unused)),
76 int line __attribute__((unused)),
83 if (vasprintf(&msg, f, va) > 0) {
85 log_err(_context, msg);
86 log_err(_context, "\n");
94 static int _dm_simple(int task, const char *name, int udev_wait);
96 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
97 unsigned crypt_min, unsigned crypt_patch)
99 unsigned dm_maj, dm_min, dm_patch;
101 if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
102 dm_maj = dm_min = dm_patch = 0;
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);
107 if (crypt_maj >= 1 && crypt_min >= 2)
108 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
110 log_dbg("Suspend and resume disabled, no wipe key support.");
112 if (crypt_maj >= 1 && crypt_min >= 10)
113 _dm_crypt_flags |= DM_LMK_SUPPORTED;
115 if (dm_maj >= 4 && dm_min >= 20)
116 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
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;
122 if (crypt_maj >= 1 && crypt_min >= 11)
123 _dm_crypt_flags |= DM_DISCARDS_SUPPORTED;
125 /* Repeat test if dm-crypt is not present */
127 _dm_crypt_checked = 1;
130 static void _dm_set_verity_compat(const char *dm_version, unsigned verity_maj,
131 unsigned verity_min, unsigned verity_patch)
134 _dm_crypt_flags |= DM_VERITY_SUPPORTED;
136 log_dbg("Detected dm-verity version %i.%i.%i.",
137 verity_maj, verity_min, verity_patch);
140 static int _dm_check_versions(void)
143 struct dm_versions *target, *last_target;
146 if (_dm_crypt_checked)
149 /* FIXME: add support to DM so it forces crypt target module load here */
150 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
153 if (!dm_task_run(dmt)) {
154 dm_task_destroy(dmt);
158 if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
159 dm_task_destroy(dmt);
163 target = dm_task_get_versions(dmt);
165 last_target = target;
166 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
167 _dm_set_crypt_compat(dm_version,
168 (unsigned)target->version[0],
169 (unsigned)target->version[1],
170 (unsigned)target->version[2]);
171 } else if (!strcmp(DM_VERITY_TARGET, target->name)) {
172 _dm_set_verity_compat(dm_version,
173 (unsigned)target->version[0],
174 (unsigned)target->version[1],
175 (unsigned)target->version[2]);
177 target = (struct dm_versions *)((char *) target + target->next);
178 } while (last_target != target);
180 dm_task_destroy(dmt);
184 uint32_t dm_flags(void)
186 if (!_dm_crypt_checked)
187 _dm_check_versions();
189 return _dm_crypt_flags;
192 int dm_init(struct crypt_device *context, int check_kernel)
194 if (!_dm_use_count++) {
195 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
196 check_kernel ? "" : " (NO kernel check requested)",
197 _dm_use_udev() ? "en" : "dis");
198 if (check_kernel && !_dm_check_versions()) {
199 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
202 if (getuid() || geteuid())
203 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
204 dm_log_init(set_dm_error);
205 dm_log_init_verbose(10);
208 // FIXME: global context is not safe
212 return 1; /* unsafe memory */
217 if (_dm_use_count && (!--_dm_use_count)) {
218 log_dbg("Releasing device-mapper backend.");
219 dm_log_init_verbose(0);
226 /* Return path to DM device */
227 char *dm_device_path(const char *prefix, int major, int minor)
233 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
235 if (!dm_task_set_minor(dmt, minor) ||
236 !dm_task_set_major(dmt, major) ||
238 !(name = dm_task_get_name(dmt))) {
239 dm_task_destroy(dmt);
243 if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
246 dm_task_destroy(dmt);
251 static void hex_key(char *hexkey, size_t key_size, const char *key)
255 for(i = 0; i < key_size; i++)
256 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
259 /* http://code.google.com/p/cryptsetup/wiki/DMCrypt */
260 static char *get_dm_crypt_params(struct crypt_dm_active_device *dmd)
262 int r, max_size, null_cipher = 0;
263 char *params, *hexkey;
264 const char *features = "";
269 if (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) {
270 if (dm_flags() & DM_DISCARDS_SUPPORTED) {
271 features = " 1 allow_discards";
272 log_dbg("Discard/TRIM is allowed.");
274 log_dbg("Discard/TRIM is not supported by the kernel.");
277 if (!strncmp(dmd->u.crypt.cipher, "cipher_null-", 12))
280 hexkey = crypt_safe_alloc(null_cipher ? 2 : (dmd->u.crypt.vk->keylength * 2 + 1));
285 strncpy(hexkey, "-", 2);
287 hex_key(hexkey, dmd->u.crypt.vk->keylength, dmd->u.crypt.vk->key);
289 max_size = strlen(hexkey) + strlen(dmd->u.crypt.cipher) +
290 strlen(dmd->data_device) + strlen(features) + 64;
291 params = crypt_safe_alloc(max_size);
295 r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
296 dmd->u.crypt.cipher, hexkey, dmd->u.crypt.iv_offset,
297 dmd->data_device, dmd->u.crypt.offset, features);
298 if (r < 0 || r >= max_size) {
299 crypt_safe_free(params);
303 crypt_safe_free(hexkey);
307 /* http://code.google.com/p/cryptsetup/wiki/DMVerity */
308 static char *get_dm_verity_params(struct crypt_params_verity *vp,
309 struct crypt_dm_active_device *dmd)
312 char *params = NULL, *hexroot = NULL, *hexsalt = NULL;
317 hexroot = crypt_safe_alloc(dmd->u.verity.root_hash_size * 2 + 1);
320 hex_key(hexroot, dmd->u.verity.root_hash_size, dmd->u.verity.root_hash);
322 hexsalt = crypt_safe_alloc(vp->salt_size ? vp->salt_size * 2 + 1 : 2);
326 hex_key(hexsalt, vp->salt_size, vp->salt);
328 strncpy(hexsalt, "-", 2);
330 max_size = strlen(hexroot) + strlen(hexsalt) +
331 strlen(dmd->data_device) +
332 strlen(dmd->u.verity.hash_device) +
333 strlen(vp->hash_name) + 128;
335 params = crypt_safe_alloc(max_size);
339 r = snprintf(params, max_size,
340 "%u %s %s %u %u %" PRIu64 " %" PRIu64 " %s %s %s",
341 vp->hash_type, dmd->data_device,
342 dmd->u.verity.hash_device,
343 vp->data_block_size, vp->hash_block_size,
344 vp->data_size, dmd->u.verity.hash_offset,
345 vp->hash_name, hexroot, hexsalt);
346 if (r < 0 || r >= max_size) {
347 crypt_safe_free(params);
351 crypt_safe_free(hexroot);
352 crypt_safe_free(hexsalt);
358 static int _dm_simple(int task, const char *name, int udev_wait)
367 if (!(dmt = dm_task_create(task)))
370 if (name && !dm_task_set_name(dmt, name))
373 #if HAVE_DECL_DM_TASK_RETRY_REMOVE
374 /* Used only in DM_DEVICE_REMOVE */
375 if (name && !dm_task_retry_remove(dmt))
378 if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
381 r = dm_task_run(dmt);
384 (void)_dm_udev_wait(cookie);
387 dm_task_destroy(dmt);
391 static int _error_device(const char *name, size_t size)
396 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
399 if (!dm_task_set_name(dmt, name))
402 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
405 if (!dm_task_set_ro(dmt))
408 if (!dm_task_no_open_count(dmt))
411 if (!dm_task_run(dmt))
414 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
415 _dm_simple(DM_DEVICE_CLEAR, name, 0);
422 dm_task_destroy(dmt);
426 int dm_remove_device(const char *name, int force, uint64_t size)
429 int retries = force ? RETRY_COUNT : 1;
430 int error_target = 0;
432 if (!name || (force && !size))
436 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
437 if (--retries && r) {
438 log_dbg("WARNING: other process locked internal device %s, %s.",
439 name, retries ? "retrying remove" : "giving up");
441 if (force && !error_target) {
442 /* If force flag is set, replace device with error, read-only target.
443 * it should stop processes from reading it and also removed underlying
444 * device from mapping, so it is usable again.
445 * Force flag should be used only for temporary devices, which are
446 * intended to work inside cryptsetup only!
447 * Anyway, if some process try to read temporary cryptsetup device,
448 * it is bug - no other process should try touch it (e.g. udev).
450 _error_device(name, size);
454 } while (r == -EINVAL && retries);
456 dm_task_update_nodes();
461 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
463 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
465 * CRYPT-LUKS1-00000000000000000000000000000000-name
468 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
470 char *ptr, uuid2[UUID_LEN] = {0};
474 /* Remove '-' chars */
475 if (uuid && !uuid_parse(uuid, uu)) {
476 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
477 if (uuid[i] != '-') {
483 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
484 type ?: "", type ? "-" : "",
485 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
488 log_dbg("DM-UUID is %s", buf);
490 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
493 static int _dm_create_device(const char *name, const char *type,
494 const char *device, uint32_t flags,
495 const char *uuid, uint64_t size,
496 char *params, int reload)
498 struct dm_task *dmt = NULL;
500 char dev_uuid[DM_UUID_LEN] = {0};
502 uint32_t read_ahead = 0;
504 uint16_t udev_flags = 0;
506 if (flags & CRYPT_ACTIVATE_PRIVATE)
507 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
509 /* All devices must have DM_UUID, only resize on old device is exception */
511 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
514 if (!dm_task_set_name(dmt, name))
517 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
519 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
522 if (!dm_task_set_name(dmt, name))
525 if (!dm_task_set_uuid(dmt, dev_uuid))
528 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
532 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
534 if ((flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
537 if (!dm_task_add_target(dmt, 0, size,
538 !strcmp("VERITY", type) ? DM_VERITY_TARGET : DM_CRYPT_TARGET, params))
541 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
542 if (device_read_ahead(device, &read_ahead) &&
543 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
547 if (!dm_task_run(dmt))
551 dm_task_destroy(dmt);
552 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
554 if (!dm_task_set_name(dmt, name))
556 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
558 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
560 if (!dm_task_run(dmt))
564 if (!dm_task_get_info(dmt, &dmi))
569 if (_dm_use_udev()) {
570 (void)_dm_udev_wait(cookie);
574 if (r < 0 && !reload)
575 dm_remove_device(name, 0, 0);
578 if (cookie && _dm_use_udev())
579 (void)_dm_udev_wait(cookie);
582 crypt_safe_free(params);
584 dm_task_destroy(dmt);
586 dm_task_update_nodes();
590 int dm_create_device(const char *name,
592 struct crypt_dm_active_device *dmd,
595 char *table_params = NULL;
597 if (dmd->target == DM_CRYPT)
598 table_params = get_dm_crypt_params(dmd);
599 else if (dmd->target == DM_VERITY)
600 table_params = get_dm_verity_params(dmd->u.verity.vp, dmd);
602 if (!table_params || !type)
605 return _dm_create_device(name, type, dmd->data_device, dmd->flags,
606 dmd->uuid, dmd->size, table_params, reload);
609 static int dm_status_dmi(const char *name, struct dm_info *dmi,
610 const char *target, char **status_line)
613 uint64_t start, length;
614 char *target_type, *params = NULL;
618 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
621 if (!dm_task_set_name(dmt, name))
624 if (!dm_task_run(dmt))
627 if (!dm_task_get_info(dmt, dmi))
635 next = dm_get_next_target(dmt, next, &start, &length,
636 &target_type, ¶ms);
638 if (!target_type || start != 0 || next)
641 if (target && strcmp(target_type, target))
644 /* for target == NULL check all supported */
645 if (!target && (strcmp(target_type, DM_CRYPT_TARGET) &&
646 strcmp(target_type, DM_VERITY_TARGET)))
650 if (!r && status_line && !(*status_line = strdup(params)))
654 dm_task_destroy(dmt);
659 int dm_status_device(const char *name)
664 r = dm_status_dmi(name, &dmi, NULL, NULL);
668 return (dmi.open_count > 0);
671 int dm_status_suspended(const char *name)
676 r = dm_status_dmi(name, &dmi, DM_CRYPT_TARGET, NULL);
680 return dmi.suspended ? 1 : 0;
683 int dm_status_verity_ok(const char *name)
687 char *status_line = NULL;
689 r = dm_status_dmi(name, &dmi, DM_VERITY_TARGET, &status_line);
690 if (r < 0 || !status_line) {
695 log_dbg("Verity volume %s status is %s.", name, status_line ?: "");
696 r = status_line[0] == 'V' ? 1 : 0;
702 /* FIXME use hex wrapper, user val wrappers for line parsing */
703 static int _dm_query_crypt(uint32_t get_flags,
706 struct crypt_dm_active_device *dmd)
709 char *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
712 memset(dmd, 0, sizeof(*dmd));
713 dmd->target = DM_CRYPT;
715 rcipher = strsep(¶ms, " ");
717 if (get_flags & DM_ACTIVE_CRYPT_CIPHER)
718 dmd->u.crypt.cipher = strdup(rcipher);
721 key_ = strsep(¶ms, " ");
724 val64 = strtoull(params, ¶ms, 10);
729 dmd->u.crypt.iv_offset = val64;
732 rdevice = strsep(¶ms, " ");
733 if (get_flags & DM_ACTIVE_DEVICE)
734 dmd->data_device = crypt_lookup_dev(rdevice);
739 val64 = strtoull(params, ¶ms, 10);
740 dmd->u.crypt.offset = val64;
742 /* Features section, available since crypt target version 1.11 */
748 /* Number of arguments */
749 val64 = strtoull(params, ¶ms, 10);
754 for (i = 0; i < val64; i++) {
757 arg = strsep(¶ms, " ");
758 if (!strcasecmp(arg, "allow_discards"))
759 dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
760 else /* unknown option */
764 /* All parameters shold be processed */
769 /* Never allow to return empty key */
770 if ((get_flags & DM_ACTIVE_CRYPT_KEY) && dmi->suspended) {
771 log_dbg("Cannot read volume key while suspended.");
775 if (get_flags & DM_ACTIVE_CRYPT_KEYSIZE) {
776 dmd->u.crypt.vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
777 if (!dmd->u.crypt.vk)
780 if (get_flags & DM_ACTIVE_CRYPT_KEY) {
782 for(i = 0; i < dmd->u.crypt.vk->keylength; i++) {
783 memcpy(buffer, &key_[i * 2], 2);
784 dmd->u.crypt.vk->key[i] = strtoul(buffer, &endp, 16);
785 if (endp != &buffer[2]) {
786 crypt_free_volume_key(dmd->u.crypt.vk);
787 dmd->u.crypt.vk = NULL;
793 memset(key_, 0, strlen(key_));
798 static int _dm_query_verity(uint32_t get_flags,
801 struct crypt_dm_active_device *dmd)
803 struct crypt_params_verity *vp = NULL;
809 if (get_flags & DM_ACTIVE_VERITY_PARAMS)
810 vp = dmd->u.verity.vp;
812 memset(dmd, 0, sizeof(*dmd));
814 dmd->target = DM_VERITY;
815 dmd->u.verity.vp = vp;
818 val32 = strtoul(params, ¶ms, 10);
822 vp->hash_type = val32;
826 str = strsep(¶ms, " ");
829 if (get_flags & DM_ACTIVE_DEVICE)
830 dmd->data_device = crypt_lookup_dev(str);
833 str = strsep(¶ms, " ");
836 if (get_flags & DM_ACTIVE_VERITY_HASH_DEVICE)
837 dmd->u.verity.hash_device = crypt_lookup_dev(str);
840 val32 = strtoul(params, ¶ms, 10);
844 vp->data_block_size = val32;
847 /* hash block size */
848 val32 = strtoul(params, ¶ms, 10);
852 vp->hash_block_size = val32;
856 val64 = strtoull(params, ¶ms, 10);
860 vp->data_size = val64;
864 val64 = strtoull(params, ¶ms, 10);
867 dmd->u.verity.hash_offset = val64;
871 str = strsep(¶ms, " ");
875 vp->hash_name = strdup(str);
878 str = strsep(¶ms, " ");
881 len = crypt_hex_to_bytes(str, &str2, 0);
884 dmd->u.verity.root_hash_size = len;
885 if (get_flags & DM_ACTIVE_VERITY_ROOT_HASH)
886 dmd->u.verity.root_hash = str2;
891 str = strsep(¶ms, " ");
895 if (!strcmp(str, "-")) {
899 len = crypt_hex_to_bytes(str, &str2, 0);
910 int dm_query_device(const char *name, uint32_t get_flags,
911 struct crypt_dm_active_device *dmd)
915 uint64_t start, length;
916 char *target_type, *params;
917 const char *tmp_uuid;
921 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
923 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
925 if (!dm_task_set_name(dmt, name))
928 if (!dm_task_run(dmt))
932 if (!dm_task_get_info(dmt, &dmi))
940 next = dm_get_next_target(dmt, next, &start, &length,
941 &target_type, ¶ms);
943 if (!target_type || start != 0 || next)
946 if (!strcmp(target_type, DM_CRYPT_TARGET)) {
947 r = _dm_query_crypt(get_flags, &dmi, params, dmd);
948 } else if (!strcmp(target_type, DM_VERITY_TARGET)) {
949 r = _dm_query_verity(get_flags, &dmi, params, dmd);
952 r = dm_status_verity_ok(name);
956 dmd->flags |= CRYPT_ACTIVATE_CORRUPTED;
967 dmd->flags |= CRYPT_ACTIVATE_READONLY;
969 tmp_uuid = dm_task_get_uuid(dmt);
971 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
972 else if (get_flags & DM_ACTIVE_UUID) {
973 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
974 dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
977 r = (dmi.open_count > 0);
980 dm_task_destroy(dmt);
985 static int _dm_message(const char *name, const char *msg)
990 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
993 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
996 if (name && !dm_task_set_name(dmt, name))
999 if (!dm_task_set_sector(dmt, (uint64_t) 0))
1002 if (!dm_task_set_message(dmt, msg))
1005 r = dm_task_run(dmt);
1008 dm_task_destroy(dmt);
1012 int dm_suspend_and_wipe_key(const char *name)
1014 if (!_dm_check_versions())
1017 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
1020 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
1023 if (!_dm_message(name, "key wipe")) {
1024 _dm_simple(DM_DEVICE_RESUME, name, 1);
1031 int dm_resume_and_reinstate_key(const char *name,
1035 int msg_size = key_size * 2 + 10; // key set <key>
1039 if (!_dm_check_versions())
1042 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
1045 msg = crypt_safe_alloc(msg_size);
1049 memset(msg, 0, msg_size);
1050 strcpy(msg, "key set ");
1051 hex_key(&msg[8], key_size, key);
1053 if (!_dm_message(name, msg) ||
1054 !_dm_simple(DM_DEVICE_RESUME, name, 1))
1057 crypt_safe_free(msg);
1061 const char *dm_get_dir(void)
1066 int dm_is_dm_device(int major, int minor)
1068 return dm_is_dm_major((uint32_t)major);
1071 int dm_is_dm_kernel_name(const char *name)
1073 return strncmp(name, "dm-", 3) ? 0 : 1;
1076 int dm_check_segment(const char *name, uint64_t offset, uint64_t size)
1078 struct crypt_dm_active_device dmd;
1081 log_dbg("Checking segments for device %s.", name);
1083 r = dm_query_device(name, 0, &dmd);
1087 if (offset >= (dmd.u.crypt.offset + dmd.size) ||
1088 (offset + size) <= dmd.u.crypt.offset)
1093 log_dbg("seg: %" PRIu64 " - %" PRIu64 ", new %" PRIu64 " - %" PRIu64 "%s",
1094 dmd.u.crypt.offset, dmd.u.crypt.offset + dmd.size, offset, offset + size,
1095 r ? " (overlapping)" : " (ok)");