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 int _quiet_log = 0;
42 static uint32_t _dm_crypt_flags = 0;
44 static struct crypt_device *_context = NULL;
45 static int _dm_use_count = 0;
47 /* Check if we have DM flag to instruct kernel to force wipe buffers */
48 #if !HAVE_DECL_DM_TASK_SECURE_DATA
49 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
52 /* Compatibility for old device-mapper without udev support */
53 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
54 #define CRYPT_TEMP_UDEV_FLAGS DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
55 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
56 DM_UDEV_DISABLE_OTHER_RULES_FLAG
57 #define _dm_task_set_cookie dm_task_set_cookie
58 #define _dm_udev_wait dm_udev_wait
60 #define CRYPT_TEMP_UDEV_FLAGS 0
61 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
62 static int _dm_udev_wait(uint32_t cookie) { return 0; };
65 static int _dm_use_udev(void)
67 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
68 return dm_udev_get_sync_support();
74 __attribute__((format(printf, 4, 5)))
75 static void set_dm_error(int level,
76 const char *file __attribute__((unused)),
77 int line __attribute__((unused)),
84 if (vasprintf(&msg, f, va) > 0) {
85 if (level < 4 && !_quiet_log) {
86 log_err(_context, msg);
87 log_err(_context, "\n");
89 /* We do not use DM visual stack backtrace here */
90 if (strncmp(msg, "<backtrace>", 11))
98 static int _dm_simple(int task, const char *name, int udev_wait);
100 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
101 unsigned crypt_min, unsigned crypt_patch)
103 unsigned dm_maj, dm_min, dm_patch;
105 if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
106 dm_maj = dm_min = dm_patch = 0;
108 log_dbg("Detected dm-crypt version %i.%i.%i, dm-ioctl version %u.%u.%u.",
109 crypt_maj, crypt_min, crypt_patch, dm_maj, dm_min, dm_patch);
111 if (crypt_maj >= 1 && crypt_min >= 2)
112 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
114 log_dbg("Suspend and resume disabled, no wipe key support.");
116 if (crypt_maj >= 1 && crypt_min >= 10)
117 _dm_crypt_flags |= DM_LMK_SUPPORTED;
119 if (dm_maj >= 4 && dm_min >= 20)
120 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
122 /* not perfect, 2.6.33 supports with 1.7.0 */
123 if (crypt_maj >= 1 && crypt_min >= 8)
124 _dm_crypt_flags |= DM_PLAIN64_SUPPORTED;
126 if (crypt_maj >= 1 && crypt_min >= 11)
127 _dm_crypt_flags |= DM_DISCARDS_SUPPORTED;
129 /* Repeat test if dm-crypt is not present */
131 _dm_crypt_checked = 1;
134 static void _dm_set_verity_compat(const char *dm_version, unsigned verity_maj,
135 unsigned verity_min, unsigned verity_patch)
138 _dm_crypt_flags |= DM_VERITY_SUPPORTED;
140 log_dbg("Detected dm-verity version %i.%i.%i.",
141 verity_maj, verity_min, verity_patch);
144 static int _dm_check_versions(void)
147 struct dm_versions *target, *last_target;
151 if (_dm_crypt_checked)
154 /* Shut up DM while checking */
157 /* FIXME: add support to DM so it forces crypt target module load here */
158 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
161 if (!dm_task_run(dmt))
164 if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version)))
167 target = dm_task_get_versions(dmt);
169 last_target = target;
170 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
171 _dm_set_crypt_compat(dm_version,
172 (unsigned)target->version[0],
173 (unsigned)target->version[1],
174 (unsigned)target->version[2]);
175 } else if (!strcmp(DM_VERITY_TARGET, target->name)) {
176 _dm_set_verity_compat(dm_version,
177 (unsigned)target->version[0],
178 (unsigned)target->version[1],
179 (unsigned)target->version[2]);
181 target = (struct dm_versions *)((char *) target + target->next);
182 } while (last_target != target);
185 log_dbg("Device-mapper backend running with UDEV support %sabled.",
186 _dm_use_udev() ? "en" : "dis");
189 dm_task_destroy(dmt);
195 uint32_t dm_flags(void)
197 _dm_check_versions();
198 return _dm_crypt_flags;
201 /* This doesn't run any kernel checks, just set up userspace libdevmapper */
202 void dm_backend_init(void)
204 if (!_dm_use_count++) {
205 log_dbg("Initialising device-mapper backend library.");
206 dm_log_init(set_dm_error);
207 dm_log_init_verbose(10);
211 void dm_backend_exit(void)
213 if (_dm_use_count && (!--_dm_use_count)) {
214 log_dbg("Releasing device-mapper backend.");
215 dm_log_init_verbose(0);
222 * libdevmapper is not context friendly, switch context on every DM call.
223 * FIXME: this is not safe if called in parallel but neither is DM lib.
225 static int dm_init_context(struct crypt_device *cd)
228 if (!_dm_check_versions()) {
229 if (getuid() || geteuid())
230 log_err(cd, _("Cannot initialize device-mapper, "
231 "running as non-root user.\n"));
233 log_err(cd, _("Cannot initialize device-mapper. "
234 "Is dm_mod kernel module loaded?\n"));
240 static void dm_exit_context(void)
245 /* Return path to DM device */
246 char *dm_device_path(const char *prefix, int major, int minor)
252 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
254 if (!dm_task_set_minor(dmt, minor) ||
255 !dm_task_set_major(dmt, major) ||
257 !(name = dm_task_get_name(dmt))) {
258 dm_task_destroy(dmt);
262 if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
265 dm_task_destroy(dmt);
270 static void hex_key(char *hexkey, size_t key_size, const char *key)
274 for(i = 0; i < key_size; i++)
275 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
278 /* http://code.google.com/p/cryptsetup/wiki/DMCrypt */
279 static char *get_dm_crypt_params(struct crypt_dm_active_device *dmd)
281 int r, max_size, null_cipher = 0;
282 char *params, *hexkey;
283 const char *features = "";
288 if (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) {
289 if (dm_flags() & DM_DISCARDS_SUPPORTED) {
290 features = " 1 allow_discards";
291 log_dbg("Discard/TRIM is allowed.");
293 log_dbg("Discard/TRIM is not supported by the kernel.");
296 if (!strncmp(dmd->u.crypt.cipher, "cipher_null-", 12))
299 hexkey = crypt_safe_alloc(null_cipher ? 2 : (dmd->u.crypt.vk->keylength * 2 + 1));
304 strncpy(hexkey, "-", 2);
306 hex_key(hexkey, dmd->u.crypt.vk->keylength, dmd->u.crypt.vk->key);
308 max_size = strlen(hexkey) + strlen(dmd->u.crypt.cipher) +
309 strlen(device_block_path(dmd->data_device)) +
310 strlen(features) + 64;
311 params = crypt_safe_alloc(max_size);
315 r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
316 dmd->u.crypt.cipher, hexkey, dmd->u.crypt.iv_offset,
317 device_block_path(dmd->data_device), dmd->u.crypt.offset,
319 if (r < 0 || r >= max_size) {
320 crypt_safe_free(params);
324 crypt_safe_free(hexkey);
328 /* http://code.google.com/p/cryptsetup/wiki/DMVerity */
329 static char *get_dm_verity_params(struct crypt_params_verity *vp,
330 struct crypt_dm_active_device *dmd)
333 char *params = NULL, *hexroot = NULL, *hexsalt = NULL;
338 hexroot = crypt_safe_alloc(dmd->u.verity.root_hash_size * 2 + 1);
341 hex_key(hexroot, dmd->u.verity.root_hash_size, dmd->u.verity.root_hash);
343 hexsalt = crypt_safe_alloc(vp->salt_size ? vp->salt_size * 2 + 1 : 2);
347 hex_key(hexsalt, vp->salt_size, vp->salt);
349 strncpy(hexsalt, "-", 2);
351 max_size = strlen(hexroot) + strlen(hexsalt) +
352 strlen(device_block_path(dmd->data_device)) +
353 strlen(device_block_path(dmd->u.verity.hash_device)) +
354 strlen(vp->hash_name) + 128;
356 params = crypt_safe_alloc(max_size);
360 r = snprintf(params, max_size,
361 "%u %s %s %u %u %" PRIu64 " %" PRIu64 " %s %s %s",
362 vp->hash_type, device_block_path(dmd->data_device),
363 device_block_path(dmd->u.verity.hash_device),
364 vp->data_block_size, vp->hash_block_size,
365 vp->data_size, dmd->u.verity.hash_offset,
366 vp->hash_name, hexroot, hexsalt);
367 if (r < 0 || r >= max_size) {
368 crypt_safe_free(params);
372 crypt_safe_free(hexroot);
373 crypt_safe_free(hexsalt);
379 static int _dm_simple(int task, const char *name, int udev_wait)
388 if (!(dmt = dm_task_create(task)))
391 if (name && !dm_task_set_name(dmt, name))
394 #if HAVE_DECL_DM_TASK_RETRY_REMOVE
395 /* Used only in DM_DEVICE_REMOVE */
396 if (name && !dm_task_retry_remove(dmt))
399 if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
402 r = dm_task_run(dmt);
405 (void)_dm_udev_wait(cookie);
408 dm_task_destroy(dmt);
412 static int _error_device(const char *name, size_t size)
417 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
420 if (!dm_task_set_name(dmt, name))
423 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
426 if (!dm_task_set_ro(dmt))
429 if (!dm_task_no_open_count(dmt))
432 if (!dm_task_run(dmt))
435 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
436 _dm_simple(DM_DEVICE_CLEAR, name, 0);
443 dm_task_destroy(dmt);
447 int dm_remove_device(struct crypt_device *cd, const char *name,
448 int force, uint64_t size)
451 int retries = force ? RETRY_COUNT : 1;
452 int error_target = 0;
454 if (!name || (force && !size))
457 if (dm_init_context(cd))
461 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
462 if (--retries && r) {
463 log_dbg("WARNING: other process locked internal device %s, %s.",
464 name, retries ? "retrying remove" : "giving up");
466 if (force && !error_target) {
467 /* If force flag is set, replace device with error, read-only target.
468 * it should stop processes from reading it and also removed underlying
469 * device from mapping, so it is usable again.
470 * Force flag should be used only for temporary devices, which are
471 * intended to work inside cryptsetup only!
472 * Anyway, if some process try to read temporary cryptsetup device,
473 * it is bug - no other process should try touch it (e.g. udev).
475 _error_device(name, size);
479 } while (r == -EINVAL && retries);
481 dm_task_update_nodes();
487 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
489 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
491 * CRYPT-LUKS1-00000000000000000000000000000000-name
494 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
496 char *ptr, uuid2[UUID_LEN] = {0};
500 /* Remove '-' chars */
501 if (uuid && !uuid_parse(uuid, uu)) {
502 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
503 if (uuid[i] != '-') {
509 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
510 type ?: "", type ? "-" : "",
511 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
514 log_dbg("DM-UUID is %s", buf);
516 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
519 static int _dm_create_device(const char *name, const char *type,
520 struct device *device, uint32_t flags,
521 const char *uuid, uint64_t size,
522 char *params, int reload)
524 struct dm_task *dmt = NULL;
526 char dev_uuid[DM_UUID_LEN] = {0};
528 uint32_t read_ahead = 0;
530 uint16_t udev_flags = 0;
532 if (flags & CRYPT_ACTIVATE_PRIVATE)
533 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
535 /* All devices must have DM_UUID, only resize on old device is exception */
537 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
540 if (!dm_task_set_name(dmt, name))
543 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
545 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
548 if (!dm_task_set_name(dmt, name))
551 if (!dm_task_set_uuid(dmt, dev_uuid))
554 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
558 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
560 if ((flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
563 if (!dm_task_add_target(dmt, 0, size,
564 !strcmp("VERITY", type) ? DM_VERITY_TARGET : DM_CRYPT_TARGET, params))
567 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
568 if (device_read_ahead(device, &read_ahead) &&
569 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
573 if (!dm_task_run(dmt))
577 dm_task_destroy(dmt);
578 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
580 if (!dm_task_set_name(dmt, name))
582 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
584 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
586 if (!dm_task_run(dmt))
590 if (!dm_task_get_info(dmt, &dmi))
595 if (_dm_use_udev()) {
596 (void)_dm_udev_wait(cookie);
600 if (r < 0 && !reload)
601 _dm_simple(DM_DEVICE_REMOVE, name, 1);
604 if (cookie && _dm_use_udev())
605 (void)_dm_udev_wait(cookie);
608 crypt_safe_free(params);
610 dm_task_destroy(dmt);
612 dm_task_update_nodes();
616 int dm_create_device(struct crypt_device *cd, const char *name,
618 struct crypt_dm_active_device *dmd,
621 char *table_params = NULL;
627 if (dm_init_context(cd))
630 if (dmd->target == DM_CRYPT)
631 table_params = get_dm_crypt_params(dmd);
632 else if (dmd->target == DM_VERITY)
633 table_params = get_dm_verity_params(dmd->u.verity.vp, dmd);
636 r = _dm_create_device(name, type, dmd->data_device,
637 dmd->flags, dmd->uuid, dmd->size,
638 table_params, reload);
643 static int dm_status_dmi(const char *name, struct dm_info *dmi,
644 const char *target, char **status_line)
647 uint64_t start, length;
648 char *target_type, *params = NULL;
652 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
655 if (!dm_task_set_name(dmt, name))
658 if (!dm_task_run(dmt))
661 if (!dm_task_get_info(dmt, dmi))
669 next = dm_get_next_target(dmt, next, &start, &length,
670 &target_type, ¶ms);
672 if (!target_type || start != 0 || next)
675 if (target && strcmp(target_type, target))
678 /* for target == NULL check all supported */
679 if (!target && (strcmp(target_type, DM_CRYPT_TARGET) &&
680 strcmp(target_type, DM_VERITY_TARGET)))
684 if (!r && status_line && !(*status_line = strdup(params)))
688 dm_task_destroy(dmt);
693 int dm_status_device(struct crypt_device *cd, const char *name)
698 if (dm_init_context(cd))
700 r = dm_status_dmi(name, &dmi, NULL, NULL);
705 return (dmi.open_count > 0);
708 int dm_status_suspended(struct crypt_device *cd, const char *name)
713 if (dm_init_context(cd))
715 r = dm_status_dmi(name, &dmi, DM_CRYPT_TARGET, NULL);
720 return dmi.suspended ? 1 : 0;
723 static int _dm_status_verity_ok(const char *name)
727 char *status_line = NULL;
729 r = dm_status_dmi(name, &dmi, DM_VERITY_TARGET, &status_line);
730 if (r < 0 || !status_line) {
735 log_dbg("Verity volume %s status is %s.", name, status_line ?: "");
736 r = status_line[0] == 'V' ? 1 : 0;
742 int dm_status_verity_ok(struct crypt_device *cd, const char *name)
746 if (dm_init_context(cd))
748 r = _dm_status_verity_ok(name);
753 /* FIXME use hex wrapper, user val wrappers for line parsing */
754 static int _dm_query_crypt(uint32_t get_flags,
757 struct crypt_dm_active_device *dmd)
760 char *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
764 memset(dmd, 0, sizeof(*dmd));
765 dmd->target = DM_CRYPT;
767 rcipher = strsep(¶ms, " ");
769 if (get_flags & DM_ACTIVE_CRYPT_CIPHER)
770 dmd->u.crypt.cipher = strdup(rcipher);
773 key_ = strsep(¶ms, " ");
776 val64 = strtoull(params, ¶ms, 10);
781 dmd->u.crypt.iv_offset = val64;
784 rdevice = strsep(¶ms, " ");
785 if (get_flags & DM_ACTIVE_DEVICE) {
786 arg = crypt_lookup_dev(rdevice);
787 r = device_alloc(&dmd->data_device, arg);
789 if (r < 0 && r != -ENOTBLK)
796 val64 = strtoull(params, ¶ms, 10);
797 dmd->u.crypt.offset = val64;
799 /* Features section, available since crypt target version 1.11 */
805 /* Number of arguments */
806 val64 = strtoull(params, ¶ms, 10);
811 for (i = 0; i < val64; i++) {
814 arg = strsep(¶ms, " ");
815 if (!strcasecmp(arg, "allow_discards"))
816 dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
817 else /* unknown option */
821 /* All parameters shold be processed */
826 /* Never allow to return empty key */
827 if ((get_flags & DM_ACTIVE_CRYPT_KEY) && dmi->suspended) {
828 log_dbg("Cannot read volume key while suspended.");
832 if (get_flags & DM_ACTIVE_CRYPT_KEYSIZE) {
833 dmd->u.crypt.vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
834 if (!dmd->u.crypt.vk)
837 if (get_flags & DM_ACTIVE_CRYPT_KEY) {
839 for(i = 0; i < dmd->u.crypt.vk->keylength; i++) {
840 memcpy(buffer, &key_[i * 2], 2);
841 dmd->u.crypt.vk->key[i] = strtoul(buffer, &endp, 16);
842 if (endp != &buffer[2]) {
843 crypt_free_volume_key(dmd->u.crypt.vk);
844 dmd->u.crypt.vk = NULL;
850 memset(key_, 0, strlen(key_));
855 static int _dm_query_verity(uint32_t get_flags,
858 struct crypt_dm_active_device *dmd)
860 struct crypt_params_verity *vp = NULL;
867 if (get_flags & DM_ACTIVE_VERITY_PARAMS)
868 vp = dmd->u.verity.vp;
870 memset(dmd, 0, sizeof(*dmd));
872 dmd->target = DM_VERITY;
873 dmd->u.verity.vp = vp;
876 val32 = strtoul(params, ¶ms, 10);
880 vp->hash_type = val32;
884 str = strsep(¶ms, " ");
887 if (get_flags & DM_ACTIVE_DEVICE) {
888 str2 = crypt_lookup_dev(str);
889 r = device_alloc(&dmd->data_device, str2);
891 if (r < 0 && r != -ENOTBLK)
896 str = strsep(¶ms, " ");
899 if (get_flags & DM_ACTIVE_VERITY_HASH_DEVICE) {
900 str2 = crypt_lookup_dev(str);
901 r = device_alloc(&dmd->u.verity.hash_device, str2);
903 if (r < 0 && r != -ENOTBLK)
908 val32 = strtoul(params, ¶ms, 10);
912 vp->data_block_size = val32;
915 /* hash block size */
916 val32 = strtoul(params, ¶ms, 10);
920 vp->hash_block_size = val32;
924 val64 = strtoull(params, ¶ms, 10);
928 vp->data_size = val64;
932 val64 = strtoull(params, ¶ms, 10);
935 dmd->u.verity.hash_offset = val64;
939 str = strsep(¶ms, " ");
943 vp->hash_name = strdup(str);
946 str = strsep(¶ms, " ");
949 len = crypt_hex_to_bytes(str, &str2, 0);
952 dmd->u.verity.root_hash_size = len;
953 if (get_flags & DM_ACTIVE_VERITY_ROOT_HASH)
954 dmd->u.verity.root_hash = str2;
959 str = strsep(¶ms, " ");
963 if (!strcmp(str, "-")) {
967 len = crypt_hex_to_bytes(str, &str2, 0);
978 int dm_query_device(struct crypt_device *cd, const char *name,
979 uint32_t get_flags, struct crypt_dm_active_device *dmd)
983 uint64_t start, length;
984 char *target_type, *params;
985 const char *tmp_uuid;
989 if (dm_init_context(cd))
991 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
993 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
995 if (!dm_task_set_name(dmt, name))
998 if (!dm_task_run(dmt))
1002 if (!dm_task_get_info(dmt, &dmi))
1010 next = dm_get_next_target(dmt, next, &start, &length,
1011 &target_type, ¶ms);
1013 if (!target_type || start != 0 || next)
1016 if (!strcmp(target_type, DM_CRYPT_TARGET)) {
1017 r = _dm_query_crypt(get_flags, &dmi, params, dmd);
1018 } else if (!strcmp(target_type, DM_VERITY_TARGET)) {
1019 r = _dm_query_verity(get_flags, &dmi, params, dmd);
1022 r = _dm_status_verity_ok(name);
1026 dmd->flags |= CRYPT_ACTIVATE_CORRUPTED;
1037 dmd->flags |= CRYPT_ACTIVATE_READONLY;
1039 tmp_uuid = dm_task_get_uuid(dmt);
1041 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
1042 else if (get_flags & DM_ACTIVE_UUID) {
1043 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
1044 dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
1047 r = (dmi.open_count > 0);
1050 dm_task_destroy(dmt);
1056 static int _dm_message(const char *name, const char *msg)
1059 struct dm_task *dmt;
1061 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
1064 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
1067 if (name && !dm_task_set_name(dmt, name))
1070 if (!dm_task_set_sector(dmt, (uint64_t) 0))
1073 if (!dm_task_set_message(dmt, msg))
1076 r = dm_task_run(dmt);
1079 dm_task_destroy(dmt);
1083 int dm_suspend_and_wipe_key(struct crypt_device *cd, const char *name)
1087 if (dm_init_context(cd))
1090 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
1093 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0)) {
1098 if (!_dm_message(name, "key wipe")) {
1099 _dm_simple(DM_DEVICE_RESUME, name, 1);
1109 int dm_resume_and_reinstate_key(struct crypt_device *cd, const char *name,
1110 size_t key_size, const char *key)
1112 int msg_size = key_size * 2 + 10; // key set <key>
1116 if (dm_init_context(cd))
1119 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
1122 msg = crypt_safe_alloc(msg_size);
1128 memset(msg, 0, msg_size);
1129 strcpy(msg, "key set ");
1130 hex_key(&msg[8], key_size, key);
1132 if (!_dm_message(name, msg) ||
1133 !_dm_simple(DM_DEVICE_RESUME, name, 1)) {
1139 crypt_safe_free(msg);
1144 const char *dm_get_dir(void)
1149 int dm_is_dm_device(int major, int minor)
1151 return dm_is_dm_major((uint32_t)major);
1154 int dm_is_dm_kernel_name(const char *name)
1156 return strncmp(name, "dm-", 3) ? 0 : 1;