2 * libdevmapper - device-mapper backend for cryptsetup
4 * Copyright (C) 2004, Jana Saout <jana@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2015, Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2009-2015, 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>
34 #define DM_UUID_LEN 129
35 #define DM_UUID_PREFIX "CRYPT-"
36 #define DM_UUID_PREFIX_LEN 6
37 #define DM_CRYPT_TARGET "crypt"
38 #define DM_VERITY_TARGET "verity"
41 /* Set if dm-crypt version was probed */
42 static int _dm_crypt_checked = 0;
43 static int _quiet_log = 0;
44 static uint32_t _dm_crypt_flags = 0;
46 static struct crypt_device *_context = NULL;
47 static int _dm_use_count = 0;
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; }
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
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; };
67 static int _dm_use_udev(void)
69 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
70 return dm_udev_get_sync_support();
76 __attribute__((format(printf, 4, 5)))
77 static void set_dm_error(int level,
78 const char *file __attribute__((unused)),
79 int line __attribute__((unused)),
86 if (vasprintf(&msg, f, va) > 0) {
87 if (level < 4 && !_quiet_log) {
88 log_err(_context, "%s", msg);
89 log_err(_context, "\n");
91 /* We do not use DM visual stack backtrace here */
92 if (strncmp(msg, "<backtrace>", 11))
100 static int _dm_simple(int task, const char *name, int udev_wait);
102 static int _dm_satisfies_version(unsigned target_maj, unsigned target_min,
103 unsigned actual_maj, unsigned actual_min)
105 if (actual_maj > target_maj)
108 if (actual_maj == target_maj && actual_min >= target_min)
114 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
115 unsigned crypt_min, unsigned crypt_patch)
117 unsigned dm_maj, dm_min, dm_patch;
119 if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
120 dm_maj = dm_min = dm_patch = 0;
122 log_dbg("Detected dm-crypt version %i.%i.%i, dm-ioctl version %u.%u.%u.",
123 crypt_maj, crypt_min, crypt_patch, dm_maj, dm_min, dm_patch);
125 if (_dm_satisfies_version(1, 2, crypt_maj, crypt_min))
126 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
128 log_dbg("Suspend and resume disabled, no wipe key support.");
130 if (_dm_satisfies_version(1, 10, crypt_maj, crypt_min))
131 _dm_crypt_flags |= DM_LMK_SUPPORTED;
133 if (_dm_satisfies_version(4, 20, dm_maj, dm_min))
134 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
136 /* not perfect, 2.6.33 supports with 1.7.0 */
137 if (_dm_satisfies_version(1, 8, crypt_maj, crypt_min))
138 _dm_crypt_flags |= DM_PLAIN64_SUPPORTED;
140 if (_dm_satisfies_version(1, 11, crypt_maj, crypt_min))
141 _dm_crypt_flags |= DM_DISCARDS_SUPPORTED;
143 if (_dm_satisfies_version(1, 13, crypt_maj, crypt_min))
144 _dm_crypt_flags |= DM_TCW_SUPPORTED;
146 if (_dm_satisfies_version(1, 14, crypt_maj, crypt_min)) {
147 _dm_crypt_flags |= DM_SAME_CPU_CRYPT_SUPPORTED;
148 _dm_crypt_flags |= DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED;
151 /* Repeat test if dm-crypt is not present */
153 _dm_crypt_checked = 1;
156 static void _dm_set_verity_compat(const char *dm_version, unsigned verity_maj,
157 unsigned verity_min, unsigned verity_patch)
160 _dm_crypt_flags |= DM_VERITY_SUPPORTED;
162 log_dbg("Detected dm-verity version %i.%i.%i.",
163 verity_maj, verity_min, verity_patch);
166 static int _dm_check_versions(void)
169 struct dm_versions *target, *last_target;
173 if (_dm_crypt_checked)
176 /* Shut up DM while checking */
179 /* FIXME: add support to DM so it forces crypt target module load here */
180 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
183 if (!dm_task_run(dmt))
186 if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version)))
189 target = dm_task_get_versions(dmt);
191 last_target = target;
192 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
193 _dm_set_crypt_compat(dm_version,
194 (unsigned)target->version[0],
195 (unsigned)target->version[1],
196 (unsigned)target->version[2]);
197 } else if (!strcmp(DM_VERITY_TARGET, target->name)) {
198 _dm_set_verity_compat(dm_version,
199 (unsigned)target->version[0],
200 (unsigned)target->version[1],
201 (unsigned)target->version[2]);
203 target = (struct dm_versions *)((char *) target + target->next);
204 } while (last_target != target);
207 log_dbg("Device-mapper backend running with UDEV support %sabled.",
208 _dm_use_udev() ? "en" : "dis");
211 dm_task_destroy(dmt);
217 uint32_t dm_flags(void)
219 _dm_check_versions();
220 return _dm_crypt_flags;
223 /* This doesn't run any kernel checks, just set up userspace libdevmapper */
224 void dm_backend_init(void)
226 if (!_dm_use_count++) {
227 log_dbg("Initialising device-mapper backend library.");
228 dm_log_init(set_dm_error);
229 dm_log_init_verbose(10);
233 void dm_backend_exit(void)
235 if (_dm_use_count && (!--_dm_use_count)) {
236 log_dbg("Releasing device-mapper backend.");
237 dm_log_init_verbose(0);
244 * libdevmapper is not context friendly, switch context on every DM call.
245 * FIXME: this is not safe if called in parallel but neither is DM lib.
247 static int dm_init_context(struct crypt_device *cd)
250 if (!_dm_check_versions()) {
251 if (getuid() || geteuid())
252 log_err(cd, _("Cannot initialize device-mapper, "
253 "running as non-root user.\n"));
255 log_err(cd, _("Cannot initialize device-mapper. "
256 "Is dm_mod kernel module loaded?\n"));
262 static void dm_exit_context(void)
267 /* Return path to DM device */
268 char *dm_device_path(const char *prefix, int major, int minor)
274 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
276 if (!dm_task_set_minor(dmt, minor) ||
277 !dm_task_set_major(dmt, major) ||
279 !(name = dm_task_get_name(dmt))) {
280 dm_task_destroy(dmt);
284 if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
287 dm_task_destroy(dmt);
292 static void hex_key(char *hexkey, size_t key_size, const char *key)
296 for(i = 0; i < key_size; i++)
297 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
300 /* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt */
301 static char *get_dm_crypt_params(struct crypt_dm_active_device *dmd, uint32_t flags)
303 int r, max_size, null_cipher = 0, num_options = 0;
304 char *params, *hexkey;
310 if (flags & CRYPT_ACTIVATE_ALLOW_DISCARDS)
312 if (flags & CRYPT_ACTIVATE_SAME_CPU_CRYPT)
314 if (flags & CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS)
318 snprintf(features, sizeof(features)-1, " %d%s%s%s", num_options,
319 (flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) ? " allow_discards" : "",
320 (flags & CRYPT_ACTIVATE_SAME_CPU_CRYPT) ? " same_cpu_crypt" : "",
321 (flags & CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS) ? " submit_from_crypt_cpus" : "");
325 if (!strncmp(dmd->u.crypt.cipher, "cipher_null-", 12))
328 hexkey = crypt_safe_alloc(null_cipher ? 2 : (dmd->u.crypt.vk->keylength * 2 + 1));
333 strncpy(hexkey, "-", 2);
335 hex_key(hexkey, dmd->u.crypt.vk->keylength, dmd->u.crypt.vk->key);
337 max_size = strlen(hexkey) + strlen(dmd->u.crypt.cipher) +
338 strlen(device_block_path(dmd->data_device)) +
339 strlen(features) + 64;
340 params = crypt_safe_alloc(max_size);
344 r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
345 dmd->u.crypt.cipher, hexkey, dmd->u.crypt.iv_offset,
346 device_block_path(dmd->data_device), dmd->u.crypt.offset,
348 if (r < 0 || r >= max_size) {
349 crypt_safe_free(params);
353 crypt_safe_free(hexkey);
357 /* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity */
358 static char *get_dm_verity_params(struct crypt_params_verity *vp,
359 struct crypt_dm_active_device *dmd)
362 char *params = NULL, *hexroot = NULL, *hexsalt = NULL;
367 hexroot = crypt_safe_alloc(dmd->u.verity.root_hash_size * 2 + 1);
370 hex_key(hexroot, dmd->u.verity.root_hash_size, dmd->u.verity.root_hash);
372 hexsalt = crypt_safe_alloc(vp->salt_size ? vp->salt_size * 2 + 1 : 2);
376 hex_key(hexsalt, vp->salt_size, vp->salt);
378 strncpy(hexsalt, "-", 2);
380 max_size = strlen(hexroot) + strlen(hexsalt) +
381 strlen(device_block_path(dmd->data_device)) +
382 strlen(device_block_path(dmd->u.verity.hash_device)) +
383 strlen(vp->hash_name) + 128;
385 params = crypt_safe_alloc(max_size);
389 r = snprintf(params, max_size,
390 "%u %s %s %u %u %" PRIu64 " %" PRIu64 " %s %s %s",
391 vp->hash_type, device_block_path(dmd->data_device),
392 device_block_path(dmd->u.verity.hash_device),
393 vp->data_block_size, vp->hash_block_size,
394 vp->data_size, dmd->u.verity.hash_offset,
395 vp->hash_name, hexroot, hexsalt);
396 if (r < 0 || r >= max_size) {
397 crypt_safe_free(params);
401 crypt_safe_free(hexroot);
402 crypt_safe_free(hexsalt);
408 static int _dm_simple(int task, const char *name, int udev_wait)
417 if (!(dmt = dm_task_create(task)))
420 if (name && !dm_task_set_name(dmt, name))
423 #if HAVE_DECL_DM_TASK_RETRY_REMOVE
424 /* Used only in DM_DEVICE_REMOVE */
425 if (name && !dm_task_retry_remove(dmt))
428 if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
431 r = dm_task_run(dmt);
434 (void)_dm_udev_wait(cookie);
437 dm_task_destroy(dmt);
441 static int _error_device(const char *name, size_t size)
446 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
449 if (!dm_task_set_name(dmt, name))
452 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
455 if (!dm_task_set_ro(dmt))
458 if (!dm_task_no_open_count(dmt))
461 if (!dm_task_run(dmt))
464 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
465 _dm_simple(DM_DEVICE_CLEAR, name, 0);
472 dm_task_destroy(dmt);
476 int dm_remove_device(struct crypt_device *cd, const char *name,
477 int force, uint64_t size)
480 int retries = force ? RETRY_COUNT : 1;
481 int error_target = 0;
483 if (!name || (force && !size))
486 if (dm_init_context(cd))
490 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
491 if (--retries && r) {
492 log_dbg("WARNING: other process locked internal device %s, %s.",
493 name, retries ? "retrying remove" : "giving up");
495 if (force && !error_target) {
496 /* If force flag is set, replace device with error, read-only target.
497 * it should stop processes from reading it and also removed underlying
498 * device from mapping, so it is usable again.
499 * Force flag should be used only for temporary devices, which are
500 * intended to work inside cryptsetup only!
501 * Anyway, if some process try to read temporary cryptsetup device,
502 * it is bug - no other process should try touch it (e.g. udev).
504 _error_device(name, size);
508 } while (r == -EINVAL && retries);
510 dm_task_update_nodes();
516 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
518 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
520 * CRYPT-LUKS1-00000000000000000000000000000000-name
523 static int dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
525 char *ptr, uuid2[UUID_LEN] = {0};
529 /* Remove '-' chars */
531 if (uuid_parse(uuid, uu) < 0) {
532 log_dbg("Requested UUID %s has invalid format.", uuid);
536 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
537 if (uuid[i] != '-') {
543 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
544 type ?: "", type ? "-" : "",
545 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
548 log_dbg("DM-UUID is %s", buf);
550 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
555 static int _dm_create_device(const char *name, const char *type,
556 struct device *device, uint32_t flags,
557 const char *uuid, uint64_t size,
558 char *params, int reload)
560 struct dm_task *dmt = NULL;
562 char dev_uuid[DM_UUID_LEN] = {0};
564 uint32_t read_ahead = 0;
566 uint16_t udev_flags = 0;
571 if (flags & CRYPT_ACTIVATE_PRIVATE)
572 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
574 /* All devices must have DM_UUID, only resize on old device is exception */
576 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
579 if (!dm_task_set_name(dmt, name))
582 if (!dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid)))
585 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
588 if (!dm_task_set_name(dmt, name))
591 if (!dm_task_set_uuid(dmt, dev_uuid))
594 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
598 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
600 if ((flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
603 if (!dm_task_add_target(dmt, 0, size,
604 !strcmp("VERITY", type) ? DM_VERITY_TARGET : DM_CRYPT_TARGET, params))
607 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
608 if (device_read_ahead(device, &read_ahead) &&
609 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
613 if (!dm_task_run(dmt))
617 dm_task_destroy(dmt);
618 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
620 if (!dm_task_set_name(dmt, name))
622 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
624 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
626 if (!dm_task_run(dmt))
630 if (!dm_task_get_info(dmt, &dmi))
635 if (_dm_use_udev()) {
636 (void)_dm_udev_wait(cookie);
640 if (r < 0 && !reload)
641 _dm_simple(DM_DEVICE_REMOVE, name, 1);
644 if (cookie && _dm_use_udev())
645 (void)_dm_udev_wait(cookie);
648 dm_task_destroy(dmt);
650 dm_task_update_nodes();
652 /* If code just loaded target module, update versions */
653 _dm_check_versions();
658 int dm_create_device(struct crypt_device *cd, const char *name,
660 struct crypt_dm_active_device *dmd,
663 char *table_params = NULL;
670 if (dm_init_context(cd))
673 dmd_flags = dmd->flags;
675 if (dmd->target == DM_CRYPT)
676 table_params = get_dm_crypt_params(dmd, dmd_flags);
677 else if (dmd->target == DM_VERITY)
678 table_params = get_dm_verity_params(dmd->u.verity.vp, dmd);
680 r = _dm_create_device(name, type, dmd->data_device, dmd_flags,
681 dmd->uuid, dmd->size, table_params, reload);
683 /* If discard not supported try to load without discard */
684 if (!reload && r && dmd->target == DM_CRYPT &&
685 (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) &&
686 !(dm_flags() & DM_DISCARDS_SUPPORTED)) {
687 log_dbg("Discard/TRIM is not supported, retrying activation.");
688 dmd_flags = dmd_flags & ~CRYPT_ACTIVATE_ALLOW_DISCARDS;
689 crypt_safe_free(table_params);
690 table_params = get_dm_crypt_params(dmd, dmd_flags);
691 r = _dm_create_device(name, type, dmd->data_device, dmd_flags,
692 dmd->uuid, dmd->size, table_params, reload);
696 dmd_flags & (CRYPT_ACTIVATE_SAME_CPU_CRYPT|CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS) &&
697 !(dm_flags() & (DM_SAME_CPU_CRYPT_SUPPORTED|DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED)))
698 log_err(cd, _("Requested dmcrypt performance options are not supported.\n"));
700 crypt_safe_free(table_params);
705 static int dm_status_dmi(const char *name, struct dm_info *dmi,
706 const char *target, char **status_line)
709 uint64_t start, length;
710 char *target_type, *params = NULL;
714 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
717 if (!dm_task_set_name(dmt, name))
720 if (!dm_task_run(dmt))
723 if (!dm_task_get_info(dmt, dmi))
731 next = dm_get_next_target(dmt, next, &start, &length,
732 &target_type, ¶ms);
734 if (!target_type || start != 0 || next)
737 if (target && strcmp(target_type, target))
740 /* for target == NULL check all supported */
741 if (!target && (strcmp(target_type, DM_CRYPT_TARGET) &&
742 strcmp(target_type, DM_VERITY_TARGET)))
746 if (!r && status_line && !(*status_line = strdup(params)))
750 dm_task_destroy(dmt);
755 int dm_status_device(struct crypt_device *cd, const char *name)
761 /* libdevmapper is too clever and handles
762 * path argument differenly with error.
763 * Fail early here if parameter is non-existent path.
765 if (strchr(name, '/') && stat(name, &st) < 0)
768 if (dm_init_context(cd))
770 r = dm_status_dmi(name, &dmi, NULL, NULL);
775 return (dmi.open_count > 0);
778 int dm_status_suspended(struct crypt_device *cd, const char *name)
783 if (dm_init_context(cd))
785 r = dm_status_dmi(name, &dmi, DM_CRYPT_TARGET, NULL);
790 return dmi.suspended ? 1 : 0;
793 static int _dm_status_verity_ok(const char *name)
797 char *status_line = NULL;
799 r = dm_status_dmi(name, &dmi, DM_VERITY_TARGET, &status_line);
800 if (r < 0 || !status_line) {
805 log_dbg("Verity volume %s status is %s.", name, status_line ?: "");
806 r = status_line[0] == 'V' ? 1 : 0;
812 int dm_status_verity_ok(struct crypt_device *cd, const char *name)
816 if (dm_init_context(cd))
818 r = _dm_status_verity_ok(name);
823 /* FIXME use hex wrapper, user val wrappers for line parsing */
824 static int _dm_query_crypt(uint32_t get_flags,
827 struct crypt_dm_active_device *dmd)
830 char *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
834 memset(dmd, 0, sizeof(*dmd));
835 dmd->target = DM_CRYPT;
837 rcipher = strsep(¶ms, " ");
839 if (get_flags & DM_ACTIVE_CRYPT_CIPHER)
840 dmd->u.crypt.cipher = strdup(rcipher);
843 key_ = strsep(¶ms, " ");
846 val64 = strtoull(params, ¶ms, 10);
851 dmd->u.crypt.iv_offset = val64;
854 rdevice = strsep(¶ms, " ");
855 if (get_flags & DM_ACTIVE_DEVICE) {
856 arg = crypt_lookup_dev(rdevice);
857 r = device_alloc(&dmd->data_device, arg);
859 if (r < 0 && r != -ENOTBLK)
866 val64 = strtoull(params, ¶ms, 10);
867 dmd->u.crypt.offset = val64;
869 /* Features section, available since crypt target version 1.11 */
875 /* Number of arguments */
876 val64 = strtoull(params, ¶ms, 10);
881 for (i = 0; i < val64; i++) {
884 arg = strsep(¶ms, " ");
885 if (!strcasecmp(arg, "allow_discards"))
886 dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
887 else if (!strcasecmp(arg, "same_cpu_crypt"))
888 dmd->flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
889 else if (!strcasecmp(arg, "submit_from_crypt_cpus"))
890 dmd->flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
891 else /* unknown option */
895 /* All parameters shold be processed */
900 /* Never allow to return empty key */
901 if ((get_flags & DM_ACTIVE_CRYPT_KEY) && dmi->suspended) {
902 log_dbg("Cannot read volume key while suspended.");
906 if (get_flags & DM_ACTIVE_CRYPT_KEYSIZE) {
907 dmd->u.crypt.vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
908 if (!dmd->u.crypt.vk)
911 if (get_flags & DM_ACTIVE_CRYPT_KEY) {
913 for(i = 0; i < dmd->u.crypt.vk->keylength; i++) {
914 memcpy(buffer, &key_[i * 2], 2);
915 dmd->u.crypt.vk->key[i] = strtoul(buffer, &endp, 16);
916 if (endp != &buffer[2]) {
917 crypt_free_volume_key(dmd->u.crypt.vk);
918 dmd->u.crypt.vk = NULL;
924 memset(key_, 0, strlen(key_));
929 static int _dm_query_verity(uint32_t get_flags,
932 struct crypt_dm_active_device *dmd)
934 struct crypt_params_verity *vp = NULL;
941 if (get_flags & DM_ACTIVE_VERITY_PARAMS)
942 vp = dmd->u.verity.vp;
944 memset(dmd, 0, sizeof(*dmd));
946 dmd->target = DM_VERITY;
947 dmd->u.verity.vp = vp;
950 val32 = strtoul(params, ¶ms, 10);
954 vp->hash_type = val32;
958 str = strsep(¶ms, " ");
961 if (get_flags & DM_ACTIVE_DEVICE) {
962 str2 = crypt_lookup_dev(str);
963 r = device_alloc(&dmd->data_device, str2);
965 if (r < 0 && r != -ENOTBLK)
970 str = strsep(¶ms, " ");
973 if (get_flags & DM_ACTIVE_VERITY_HASH_DEVICE) {
974 str2 = crypt_lookup_dev(str);
975 r = device_alloc(&dmd->u.verity.hash_device, str2);
977 if (r < 0 && r != -ENOTBLK)
982 val32 = strtoul(params, ¶ms, 10);
986 vp->data_block_size = val32;
989 /* hash block size */
990 val32 = strtoul(params, ¶ms, 10);
994 vp->hash_block_size = val32;
998 val64 = strtoull(params, ¶ms, 10);
1002 vp->data_size = val64;
1006 val64 = strtoull(params, ¶ms, 10);
1009 dmd->u.verity.hash_offset = val64;
1012 /* hash algorithm */
1013 str = strsep(¶ms, " ");
1017 vp->hash_name = strdup(str);
1020 str = strsep(¶ms, " ");
1023 len = crypt_hex_to_bytes(str, &str2, 0);
1026 dmd->u.verity.root_hash_size = len;
1027 if (get_flags & DM_ACTIVE_VERITY_ROOT_HASH)
1028 dmd->u.verity.root_hash = str2;
1033 str = strsep(¶ms, " ");
1037 if (!strcmp(str, "-")) {
1041 len = crypt_hex_to_bytes(str, &str2, 0);
1044 vp->salt_size = len;
1052 int dm_query_device(struct crypt_device *cd, const char *name,
1053 uint32_t get_flags, struct crypt_dm_active_device *dmd)
1055 struct dm_task *dmt;
1057 uint64_t start, length;
1058 char *target_type, *params;
1059 const char *tmp_uuid;
1063 if (dm_init_context(cd))
1065 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
1067 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
1069 if (!dm_task_set_name(dmt, name))
1072 if (!dm_task_run(dmt))
1076 if (!dm_task_get_info(dmt, &dmi))
1084 next = dm_get_next_target(dmt, next, &start, &length,
1085 &target_type, ¶ms);
1087 if (!target_type || start != 0 || next)
1090 if (!strcmp(target_type, DM_CRYPT_TARGET)) {
1091 r = _dm_query_crypt(get_flags, &dmi, params, dmd);
1092 } else if (!strcmp(target_type, DM_VERITY_TARGET)) {
1093 r = _dm_query_verity(get_flags, &dmi, params, dmd);
1096 r = _dm_status_verity_ok(name);
1100 dmd->flags |= CRYPT_ACTIVATE_CORRUPTED;
1111 dmd->flags |= CRYPT_ACTIVATE_READONLY;
1113 tmp_uuid = dm_task_get_uuid(dmt);
1115 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
1116 else if (get_flags & DM_ACTIVE_UUID) {
1117 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
1118 dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
1121 r = (dmi.open_count > 0);
1124 dm_task_destroy(dmt);
1130 static int _dm_message(const char *name, const char *msg)
1133 struct dm_task *dmt;
1135 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
1138 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
1141 if (name && !dm_task_set_name(dmt, name))
1144 if (!dm_task_set_sector(dmt, (uint64_t) 0))
1147 if (!dm_task_set_message(dmt, msg))
1150 r = dm_task_run(dmt);
1153 dm_task_destroy(dmt);
1157 int dm_suspend_and_wipe_key(struct crypt_device *cd, const char *name)
1161 if (dm_init_context(cd))
1164 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
1167 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0)) {
1172 if (!_dm_message(name, "key wipe")) {
1173 _dm_simple(DM_DEVICE_RESUME, name, 1);
1183 int dm_resume_and_reinstate_key(struct crypt_device *cd, const char *name,
1184 size_t key_size, const char *key)
1186 int msg_size = key_size * 2 + 10; // key set <key>
1190 if (dm_init_context(cd))
1193 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
1196 msg = crypt_safe_alloc(msg_size);
1202 strcpy(msg, "key set ");
1203 hex_key(&msg[8], key_size, key);
1205 if (!_dm_message(name, msg) ||
1206 !_dm_simple(DM_DEVICE_RESUME, name, 1)) {
1212 crypt_safe_free(msg);
1217 const char *dm_get_dir(void)
1222 int dm_is_dm_device(int major, int minor)
1224 return dm_is_dm_major((uint32_t)major);
1227 int dm_is_dm_kernel_name(const char *name)
1229 return strncmp(name, "dm-", 3) ? 0 : 1;