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.
7 * Copyright (C) 2009-2012, Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <libdevmapper.h>
30 #include <uuid/uuid.h>
31 #include <sys/utsname.h>
35 #define DM_UUID_LEN 129
36 #define DM_UUID_PREFIX "CRYPT-"
37 #define DM_UUID_PREFIX_LEN 6
38 #define DM_CRYPT_TARGET "crypt"
39 #define DM_VERITY_TARGET "verity"
42 /* Set if dm-crypt version was probed */
43 static int _dm_crypt_checked = 0;
44 static int _quiet_log = 0;
45 static uint32_t _dm_crypt_flags = 0;
47 static struct crypt_device *_context = NULL;
48 static int _dm_use_count = 0;
50 /* Check if we have DM flag to instruct kernel to force wipe buffers */
51 #if !HAVE_DECL_DM_TASK_SECURE_DATA
52 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
55 /* Compatibility for old device-mapper without udev support */
56 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
57 #define CRYPT_TEMP_UDEV_FLAGS DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
58 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
59 DM_UDEV_DISABLE_OTHER_RULES_FLAG
60 #define _dm_task_set_cookie dm_task_set_cookie
61 #define _dm_udev_wait dm_udev_wait
63 #define CRYPT_TEMP_UDEV_FLAGS 0
64 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
65 static int _dm_udev_wait(uint32_t cookie) { return 0; };
68 static int _dm_use_udev(void)
70 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
71 return dm_udev_get_sync_support();
77 __attribute__((format(printf, 4, 5)))
78 static void set_dm_error(int level,
79 const char *file __attribute__((unused)),
80 int line __attribute__((unused)),
87 if (vasprintf(&msg, f, va) > 0) {
88 if (level < 4 && !_quiet_log) {
89 log_err(_context, msg);
90 log_err(_context, "\n");
92 /* We do not use DM visual stack backtrace here */
93 if (strncmp(msg, "<backtrace>", 11))
101 static int _dm_simple(int task, const char *name, int udev_wait);
103 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
104 unsigned crypt_min, unsigned crypt_patch)
106 unsigned dm_maj, dm_min, dm_patch;
108 if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
109 dm_maj = dm_min = dm_patch = 0;
111 log_dbg("Detected dm-crypt version %i.%i.%i, dm-ioctl version %u.%u.%u.",
112 crypt_maj, crypt_min, crypt_patch, dm_maj, dm_min, dm_patch);
114 if (crypt_maj >= 1 && crypt_min >= 2)
115 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
117 log_dbg("Suspend and resume disabled, no wipe key support.");
119 if (crypt_maj >= 1 && crypt_min >= 10)
120 _dm_crypt_flags |= DM_LMK_SUPPORTED;
122 if (dm_maj >= 4 && dm_min >= 20)
123 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
125 /* not perfect, 2.6.33 supports with 1.7.0 */
126 if (crypt_maj >= 1 && crypt_min >= 8)
127 _dm_crypt_flags |= DM_PLAIN64_SUPPORTED;
129 if (crypt_maj >= 1 && crypt_min >= 11)
130 _dm_crypt_flags |= DM_DISCARDS_SUPPORTED;
132 /* Repeat test if dm-crypt is not present */
134 _dm_crypt_checked = 1;
137 static void _dm_set_verity_compat(const char *dm_version, unsigned verity_maj,
138 unsigned verity_min, unsigned verity_patch)
141 _dm_crypt_flags |= DM_VERITY_SUPPORTED;
143 log_dbg("Detected dm-verity version %i.%i.%i.",
144 verity_maj, verity_min, verity_patch);
147 static void _dm_kernel_info(void)
152 log_dbg("Detected kernel %s %s %s.",
153 uts.sysname, uts.release, uts.machine);
157 static int _dm_check_versions(void)
160 struct dm_versions *target, *last_target;
164 if (_dm_crypt_checked)
169 /* Shut up DM while checking */
172 /* FIXME: add support to DM so it forces crypt target module load here */
173 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
176 if (!dm_task_run(dmt))
179 if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version)))
182 target = dm_task_get_versions(dmt);
184 last_target = target;
185 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
186 _dm_set_crypt_compat(dm_version,
187 (unsigned)target->version[0],
188 (unsigned)target->version[1],
189 (unsigned)target->version[2]);
190 } else if (!strcmp(DM_VERITY_TARGET, target->name)) {
191 _dm_set_verity_compat(dm_version,
192 (unsigned)target->version[0],
193 (unsigned)target->version[1],
194 (unsigned)target->version[2]);
196 target = (struct dm_versions *)((char *) target + target->next);
197 } while (last_target != target);
200 log_dbg("Device-mapper backend running with UDEV support %sabled.",
201 _dm_use_udev() ? "en" : "dis");
204 dm_task_destroy(dmt);
210 uint32_t dm_flags(void)
212 _dm_check_versions();
213 return _dm_crypt_flags;
216 /* This doesn't run any kernel checks, just set up userspace libdevmapper */
217 void dm_backend_init(void)
219 if (!_dm_use_count++) {
220 log_dbg("Initialising device-mapper backend library.");
221 dm_log_init(set_dm_error);
222 dm_log_init_verbose(10);
226 void dm_backend_exit(void)
228 if (_dm_use_count && (!--_dm_use_count)) {
229 log_dbg("Releasing device-mapper backend.");
230 dm_log_init_verbose(0);
237 * libdevmapper is not context friendly, switch context on every DM call.
238 * FIXME: this is not safe if called in parallel but neither is DM lib.
240 static int dm_init_context(struct crypt_device *cd)
243 if (!_dm_check_versions()) {
244 if (getuid() || geteuid())
245 log_err(cd, _("Cannot initialize device-mapper, "
246 "running as non-root user.\n"));
248 log_err(cd, _("Cannot initialize device-mapper. "
249 "Is dm_mod kernel module loaded?\n"));
255 static void dm_exit_context(void)
260 /* Return path to DM device */
261 char *dm_device_path(const char *prefix, int major, int minor)
267 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
269 if (!dm_task_set_minor(dmt, minor) ||
270 !dm_task_set_major(dmt, major) ||
272 !(name = dm_task_get_name(dmt))) {
273 dm_task_destroy(dmt);
277 if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
280 dm_task_destroy(dmt);
285 static void hex_key(char *hexkey, size_t key_size, const char *key)
289 for(i = 0; i < key_size; i++)
290 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
293 /* http://code.google.com/p/cryptsetup/wiki/DMCrypt */
294 static char *get_dm_crypt_params(struct crypt_dm_active_device *dmd)
296 int r, max_size, null_cipher = 0;
297 char *params, *hexkey;
298 const char *features = "";
303 if (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) {
304 if (dm_flags() & DM_DISCARDS_SUPPORTED) {
305 features = " 1 allow_discards";
306 log_dbg("Discard/TRIM is allowed.");
308 log_dbg("Discard/TRIM is not supported by the kernel.");
311 if (!strncmp(dmd->u.crypt.cipher, "cipher_null-", 12))
314 hexkey = crypt_safe_alloc(null_cipher ? 2 : (dmd->u.crypt.vk->keylength * 2 + 1));
319 strncpy(hexkey, "-", 2);
321 hex_key(hexkey, dmd->u.crypt.vk->keylength, dmd->u.crypt.vk->key);
323 max_size = strlen(hexkey) + strlen(dmd->u.crypt.cipher) +
324 strlen(device_block_path(dmd->data_device)) +
325 strlen(features) + 64;
326 params = crypt_safe_alloc(max_size);
330 r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
331 dmd->u.crypt.cipher, hexkey, dmd->u.crypt.iv_offset,
332 device_block_path(dmd->data_device), dmd->u.crypt.offset,
334 if (r < 0 || r >= max_size) {
335 crypt_safe_free(params);
339 crypt_safe_free(hexkey);
343 /* http://code.google.com/p/cryptsetup/wiki/DMVerity */
344 static char *get_dm_verity_params(struct crypt_params_verity *vp,
345 struct crypt_dm_active_device *dmd)
348 char *params = NULL, *hexroot = NULL, *hexsalt = NULL;
353 hexroot = crypt_safe_alloc(dmd->u.verity.root_hash_size * 2 + 1);
356 hex_key(hexroot, dmd->u.verity.root_hash_size, dmd->u.verity.root_hash);
358 hexsalt = crypt_safe_alloc(vp->salt_size ? vp->salt_size * 2 + 1 : 2);
362 hex_key(hexsalt, vp->salt_size, vp->salt);
364 strncpy(hexsalt, "-", 2);
366 max_size = strlen(hexroot) + strlen(hexsalt) +
367 strlen(device_block_path(dmd->data_device)) +
368 strlen(device_block_path(dmd->u.verity.hash_device)) +
369 strlen(vp->hash_name) + 128;
371 params = crypt_safe_alloc(max_size);
375 r = snprintf(params, max_size,
376 "%u %s %s %u %u %" PRIu64 " %" PRIu64 " %s %s %s",
377 vp->hash_type, device_block_path(dmd->data_device),
378 device_block_path(dmd->u.verity.hash_device),
379 vp->data_block_size, vp->hash_block_size,
380 vp->data_size, dmd->u.verity.hash_offset,
381 vp->hash_name, hexroot, hexsalt);
382 if (r < 0 || r >= max_size) {
383 crypt_safe_free(params);
387 crypt_safe_free(hexroot);
388 crypt_safe_free(hexsalt);
394 static int _dm_simple(int task, const char *name, int udev_wait)
403 if (!(dmt = dm_task_create(task)))
406 if (name && !dm_task_set_name(dmt, name))
409 #if HAVE_DECL_DM_TASK_RETRY_REMOVE
410 /* Used only in DM_DEVICE_REMOVE */
411 if (name && !dm_task_retry_remove(dmt))
414 if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
417 r = dm_task_run(dmt);
420 (void)_dm_udev_wait(cookie);
423 dm_task_destroy(dmt);
427 static int _error_device(const char *name, size_t size)
432 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
435 if (!dm_task_set_name(dmt, name))
438 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
441 if (!dm_task_set_ro(dmt))
444 if (!dm_task_no_open_count(dmt))
447 if (!dm_task_run(dmt))
450 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
451 _dm_simple(DM_DEVICE_CLEAR, name, 0);
458 dm_task_destroy(dmt);
462 int dm_remove_device(struct crypt_device *cd, const char *name,
463 int force, uint64_t size)
466 int retries = force ? RETRY_COUNT : 1;
467 int error_target = 0;
469 if (!name || (force && !size))
472 if (dm_init_context(cd))
476 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
477 if (--retries && r) {
478 log_dbg("WARNING: other process locked internal device %s, %s.",
479 name, retries ? "retrying remove" : "giving up");
481 if (force && !error_target) {
482 /* If force flag is set, replace device with error, read-only target.
483 * it should stop processes from reading it and also removed underlying
484 * device from mapping, so it is usable again.
485 * Force flag should be used only for temporary devices, which are
486 * intended to work inside cryptsetup only!
487 * Anyway, if some process try to read temporary cryptsetup device,
488 * it is bug - no other process should try touch it (e.g. udev).
490 _error_device(name, size);
494 } while (r == -EINVAL && retries);
496 dm_task_update_nodes();
502 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
504 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
506 * CRYPT-LUKS1-00000000000000000000000000000000-name
509 static int dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
511 char *ptr, uuid2[UUID_LEN] = {0};
515 /* Remove '-' chars */
517 if (uuid_parse(uuid, uu) < 0) {
518 log_dbg("Requested UUID %s has invalid format.", uuid);
522 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
523 if (uuid[i] != '-') {
529 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
530 type ?: "", type ? "-" : "",
531 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
534 log_dbg("DM-UUID is %s", buf);
536 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
541 static int _dm_create_device(const char *name, const char *type,
542 struct device *device, uint32_t flags,
543 const char *uuid, uint64_t size,
544 char *params, int reload)
546 struct dm_task *dmt = NULL;
548 char dev_uuid[DM_UUID_LEN] = {0};
550 uint32_t read_ahead = 0;
552 uint16_t udev_flags = 0;
554 if (flags & CRYPT_ACTIVATE_PRIVATE)
555 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
557 /* All devices must have DM_UUID, only resize on old device is exception */
559 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
562 if (!dm_task_set_name(dmt, name))
565 r = dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
569 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
572 if (!dm_task_set_name(dmt, name))
575 if (!dm_task_set_uuid(dmt, dev_uuid))
578 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
582 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
584 if ((flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
587 if (!dm_task_add_target(dmt, 0, size,
588 !strcmp("VERITY", type) ? DM_VERITY_TARGET : DM_CRYPT_TARGET, params))
591 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
592 if (device_read_ahead(device, &read_ahead) &&
593 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
597 if (!dm_task_run(dmt))
601 dm_task_destroy(dmt);
602 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
604 if (!dm_task_set_name(dmt, name))
606 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
608 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
610 if (!dm_task_run(dmt))
614 if (!dm_task_get_info(dmt, &dmi))
619 if (_dm_use_udev()) {
620 (void)_dm_udev_wait(cookie);
624 if (r < 0 && !reload)
625 _dm_simple(DM_DEVICE_REMOVE, name, 1);
628 if (cookie && _dm_use_udev())
629 (void)_dm_udev_wait(cookie);
632 crypt_safe_free(params);
634 dm_task_destroy(dmt);
636 dm_task_update_nodes();
640 int dm_create_device(struct crypt_device *cd, const char *name,
642 struct crypt_dm_active_device *dmd,
645 char *table_params = NULL;
651 if (dm_init_context(cd))
654 if (dmd->target == DM_CRYPT)
655 table_params = get_dm_crypt_params(dmd);
656 else if (dmd->target == DM_VERITY)
657 table_params = get_dm_verity_params(dmd->u.verity.vp, dmd);
660 r = _dm_create_device(name, type, dmd->data_device,
661 dmd->flags, dmd->uuid, dmd->size,
662 table_params, reload);
667 static int dm_status_dmi(const char *name, struct dm_info *dmi,
668 const char *target, char **status_line)
671 uint64_t start, length;
672 char *target_type, *params = NULL;
676 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
679 if (!dm_task_set_name(dmt, name))
682 if (!dm_task_run(dmt))
685 if (!dm_task_get_info(dmt, dmi))
693 next = dm_get_next_target(dmt, next, &start, &length,
694 &target_type, ¶ms);
696 if (!target_type || start != 0 || next)
699 if (target && strcmp(target_type, target))
702 /* for target == NULL check all supported */
703 if (!target && (strcmp(target_type, DM_CRYPT_TARGET) &&
704 strcmp(target_type, DM_VERITY_TARGET)))
708 if (!r && status_line && !(*status_line = strdup(params)))
712 dm_task_destroy(dmt);
717 int dm_status_device(struct crypt_device *cd, const char *name)
723 /* libdevmapper is too clever and handles
724 * path argument differenly with error.
725 * Fail early here if parameter is non-existent path.
727 if (strchr(name, '/') && stat(name, &st) < 0)
730 if (dm_init_context(cd))
732 r = dm_status_dmi(name, &dmi, NULL, NULL);
737 return (dmi.open_count > 0);
740 int dm_status_suspended(struct crypt_device *cd, const char *name)
745 if (dm_init_context(cd))
747 r = dm_status_dmi(name, &dmi, DM_CRYPT_TARGET, NULL);
752 return dmi.suspended ? 1 : 0;
755 static int _dm_status_verity_ok(const char *name)
759 char *status_line = NULL;
761 r = dm_status_dmi(name, &dmi, DM_VERITY_TARGET, &status_line);
762 if (r < 0 || !status_line) {
767 log_dbg("Verity volume %s status is %s.", name, status_line ?: "");
768 r = status_line[0] == 'V' ? 1 : 0;
774 int dm_status_verity_ok(struct crypt_device *cd, const char *name)
778 if (dm_init_context(cd))
780 r = _dm_status_verity_ok(name);
785 /* FIXME use hex wrapper, user val wrappers for line parsing */
786 static int _dm_query_crypt(uint32_t get_flags,
789 struct crypt_dm_active_device *dmd)
792 char *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
796 memset(dmd, 0, sizeof(*dmd));
797 dmd->target = DM_CRYPT;
799 rcipher = strsep(¶ms, " ");
801 if (get_flags & DM_ACTIVE_CRYPT_CIPHER)
802 dmd->u.crypt.cipher = strdup(rcipher);
805 key_ = strsep(¶ms, " ");
808 val64 = strtoull(params, ¶ms, 10);
813 dmd->u.crypt.iv_offset = val64;
816 rdevice = strsep(¶ms, " ");
817 if (get_flags & DM_ACTIVE_DEVICE) {
818 arg = crypt_lookup_dev(rdevice);
819 r = device_alloc(&dmd->data_device, arg);
821 if (r < 0 && r != -ENOTBLK)
828 val64 = strtoull(params, ¶ms, 10);
829 dmd->u.crypt.offset = val64;
831 /* Features section, available since crypt target version 1.11 */
837 /* Number of arguments */
838 val64 = strtoull(params, ¶ms, 10);
843 for (i = 0; i < val64; i++) {
846 arg = strsep(¶ms, " ");
847 if (!strcasecmp(arg, "allow_discards"))
848 dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
849 else /* unknown option */
853 /* All parameters shold be processed */
858 /* Never allow to return empty key */
859 if ((get_flags & DM_ACTIVE_CRYPT_KEY) && dmi->suspended) {
860 log_dbg("Cannot read volume key while suspended.");
864 if (get_flags & DM_ACTIVE_CRYPT_KEYSIZE) {
865 dmd->u.crypt.vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
866 if (!dmd->u.crypt.vk)
869 if (get_flags & DM_ACTIVE_CRYPT_KEY) {
871 for(i = 0; i < dmd->u.crypt.vk->keylength; i++) {
872 memcpy(buffer, &key_[i * 2], 2);
873 dmd->u.crypt.vk->key[i] = strtoul(buffer, &endp, 16);
874 if (endp != &buffer[2]) {
875 crypt_free_volume_key(dmd->u.crypt.vk);
876 dmd->u.crypt.vk = NULL;
882 memset(key_, 0, strlen(key_));
887 static int _dm_query_verity(uint32_t get_flags,
890 struct crypt_dm_active_device *dmd)
892 struct crypt_params_verity *vp = NULL;
899 if (get_flags & DM_ACTIVE_VERITY_PARAMS)
900 vp = dmd->u.verity.vp;
902 memset(dmd, 0, sizeof(*dmd));
904 dmd->target = DM_VERITY;
905 dmd->u.verity.vp = vp;
908 val32 = strtoul(params, ¶ms, 10);
912 vp->hash_type = val32;
916 str = strsep(¶ms, " ");
919 if (get_flags & DM_ACTIVE_DEVICE) {
920 str2 = crypt_lookup_dev(str);
921 r = device_alloc(&dmd->data_device, str2);
923 if (r < 0 && r != -ENOTBLK)
928 str = strsep(¶ms, " ");
931 if (get_flags & DM_ACTIVE_VERITY_HASH_DEVICE) {
932 str2 = crypt_lookup_dev(str);
933 r = device_alloc(&dmd->u.verity.hash_device, str2);
935 if (r < 0 && r != -ENOTBLK)
940 val32 = strtoul(params, ¶ms, 10);
944 vp->data_block_size = val32;
947 /* hash block size */
948 val32 = strtoul(params, ¶ms, 10);
952 vp->hash_block_size = val32;
956 val64 = strtoull(params, ¶ms, 10);
960 vp->data_size = val64;
964 val64 = strtoull(params, ¶ms, 10);
967 dmd->u.verity.hash_offset = val64;
971 str = strsep(¶ms, " ");
975 vp->hash_name = strdup(str);
978 str = strsep(¶ms, " ");
981 len = crypt_hex_to_bytes(str, &str2, 0);
984 dmd->u.verity.root_hash_size = len;
985 if (get_flags & DM_ACTIVE_VERITY_ROOT_HASH)
986 dmd->u.verity.root_hash = str2;
991 str = strsep(¶ms, " ");
995 if (!strcmp(str, "-")) {
999 len = crypt_hex_to_bytes(str, &str2, 0);
1002 vp->salt_size = len;
1010 int dm_query_device(struct crypt_device *cd, const char *name,
1011 uint32_t get_flags, struct crypt_dm_active_device *dmd)
1013 struct dm_task *dmt;
1015 uint64_t start, length;
1016 char *target_type, *params;
1017 const char *tmp_uuid;
1021 if (dm_init_context(cd))
1023 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
1025 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
1027 if (!dm_task_set_name(dmt, name))
1030 if (!dm_task_run(dmt))
1034 if (!dm_task_get_info(dmt, &dmi))
1042 next = dm_get_next_target(dmt, next, &start, &length,
1043 &target_type, ¶ms);
1045 if (!target_type || start != 0 || next)
1048 if (!strcmp(target_type, DM_CRYPT_TARGET)) {
1049 r = _dm_query_crypt(get_flags, &dmi, params, dmd);
1050 } else if (!strcmp(target_type, DM_VERITY_TARGET)) {
1051 r = _dm_query_verity(get_flags, &dmi, params, dmd);
1054 r = _dm_status_verity_ok(name);
1058 dmd->flags |= CRYPT_ACTIVATE_CORRUPTED;
1069 dmd->flags |= CRYPT_ACTIVATE_READONLY;
1071 tmp_uuid = dm_task_get_uuid(dmt);
1073 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
1074 else if (get_flags & DM_ACTIVE_UUID) {
1075 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
1076 dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
1079 r = (dmi.open_count > 0);
1082 dm_task_destroy(dmt);
1088 static int _dm_message(const char *name, const char *msg)
1091 struct dm_task *dmt;
1093 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
1096 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
1099 if (name && !dm_task_set_name(dmt, name))
1102 if (!dm_task_set_sector(dmt, (uint64_t) 0))
1105 if (!dm_task_set_message(dmt, msg))
1108 r = dm_task_run(dmt);
1111 dm_task_destroy(dmt);
1115 int dm_suspend_and_wipe_key(struct crypt_device *cd, const char *name)
1119 if (dm_init_context(cd))
1122 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
1125 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0)) {
1130 if (!_dm_message(name, "key wipe")) {
1131 _dm_simple(DM_DEVICE_RESUME, name, 1);
1141 int dm_resume_and_reinstate_key(struct crypt_device *cd, const char *name,
1142 size_t key_size, const char *key)
1144 int msg_size = key_size * 2 + 10; // key set <key>
1148 if (dm_init_context(cd))
1151 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
1154 msg = crypt_safe_alloc(msg_size);
1160 strcpy(msg, "key set ");
1161 hex_key(&msg[8], key_size, key);
1163 if (!_dm_message(name, msg) ||
1164 !_dm_simple(DM_DEVICE_RESUME, name, 1)) {
1170 crypt_safe_free(msg);
1175 const char *dm_get_dir(void)
1180 int dm_is_dm_device(int major, int minor)
1182 return dm_is_dm_major((uint32_t)major);
1185 int dm_is_dm_kernel_name(const char *name)
1187 return strncmp(name, "dm-", 3) ? 0 : 1;