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>
33 #define DM_UUID_LEN 129
34 #define DM_UUID_PREFIX "CRYPT-"
35 #define DM_UUID_PREFIX_LEN 6
36 #define DM_CRYPT_TARGET "crypt"
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 int _dm_check_versions(void)
133 struct dm_versions *target, *last_target;
136 if (_dm_crypt_checked)
139 /* FIXME: add support to DM so it forces crypt target module load here */
140 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
143 if (!dm_task_run(dmt)) {
144 dm_task_destroy(dmt);
148 if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
149 dm_task_destroy(dmt);
153 target = dm_task_get_versions(dmt);
155 last_target = target;
156 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
157 _dm_set_crypt_compat(dm_version,
158 (unsigned)target->version[0],
159 (unsigned)target->version[1],
160 (unsigned)target->version[2]);
162 target = (struct dm_versions *)((char *) target + target->next);
163 } while (last_target != target);
165 dm_task_destroy(dmt);
169 uint32_t dm_flags(void)
171 if (!_dm_crypt_checked)
172 _dm_check_versions();
174 return _dm_crypt_flags;
177 int dm_init(struct crypt_device *context, int check_kernel)
179 if (!_dm_use_count++) {
180 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
181 check_kernel ? "" : " (NO kernel check requested)",
182 _dm_use_udev() ? "en" : "dis");
183 if (check_kernel && !_dm_check_versions()) {
184 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
187 if (getuid() || geteuid())
188 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
189 dm_log_init(set_dm_error);
190 dm_log_init_verbose(10);
193 // FIXME: global context is not safe
197 return 1; /* unsafe memory */
202 if (_dm_use_count && (!--_dm_use_count)) {
203 log_dbg("Releasing device-mapper backend.");
204 dm_log_init_verbose(0);
211 /* Return path to DM device */
212 char *dm_device_path(const char *prefix, int major, int minor)
218 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
220 if (!dm_task_set_minor(dmt, minor) ||
221 !dm_task_set_major(dmt, major) ||
223 !(name = dm_task_get_name(dmt))) {
224 dm_task_destroy(dmt);
228 if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
231 dm_task_destroy(dmt);
236 static void hex_key(char *hexkey, size_t key_size, const char *key)
240 for(i = 0; i < key_size; i++)
241 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
244 static char *get_params(struct crypt_dm_active_device *dmd)
247 char *params, *hexkey;
248 const char *features = "";
250 if (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) {
251 if (dm_flags() & DM_DISCARDS_SUPPORTED) {
252 features = " 1 allow_discards";
253 log_dbg("Discard/TRIM is allowed.");
255 log_dbg("Discard/TRIM is not supported by the kernel.");
258 hexkey = crypt_safe_alloc(dmd->vk->keylength * 2 + 1);
262 hex_key(hexkey, dmd->vk->keylength, dmd->vk->key);
264 max_size = strlen(hexkey) + strlen(dmd->cipher) +
265 strlen(dmd->device) + strlen(features) + 64;
266 params = crypt_safe_alloc(max_size);
270 r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
271 dmd->cipher, hexkey, dmd->iv_offset, dmd->device,
272 dmd->offset, features);
273 if (r < 0 || r >= max_size) {
274 crypt_safe_free(params);
278 crypt_safe_free(hexkey);
283 static int _dm_simple(int task, const char *name, int udev_wait)
292 if (!(dmt = dm_task_create(task)))
295 if (name && !dm_task_set_name(dmt, name))
298 if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
301 r = dm_task_run(dmt);
304 (void)_dm_udev_wait(cookie);
307 dm_task_destroy(dmt);
311 static int _error_device(const char *name, size_t size)
316 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
319 if (!dm_task_set_name(dmt, name))
322 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
325 if (!dm_task_set_ro(dmt))
328 if (!dm_task_no_open_count(dmt))
331 if (!dm_task_run(dmt))
334 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
335 _dm_simple(DM_DEVICE_CLEAR, name, 0);
342 dm_task_destroy(dmt);
346 int dm_remove_device(const char *name, int force, uint64_t size)
349 int retries = force ? RETRY_COUNT : 1;
350 int error_target = 0;
352 if (!name || (force && !size))
356 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
357 if (--retries && r) {
358 log_dbg("WARNING: other process locked internal device %s, %s.",
359 name, retries ? "retrying remove" : "giving up");
360 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
361 debug_processes_using_device(name);
363 if (force && !error_target) {
364 /* If force flag is set, replace device with error, read-only target.
365 * it should stop processes from reading it and also removed underlying
366 * device from mapping, so it is usable again.
367 * Force flag should be used only for temporary devices, which are
368 * intended to work inside cryptsetup only!
369 * Anyway, if some process try to read temporary cryptsetup device,
370 * it is bug - no other process should try touch it (e.g. udev).
372 _error_device(name, size);
376 } while (r == -EINVAL && retries);
378 dm_task_update_nodes();
383 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
385 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
387 * CRYPT-LUKS1-00000000000000000000000000000000-name
390 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
392 char *ptr, uuid2[UUID_LEN] = {0};
396 /* Remove '-' chars */
397 if (uuid && !uuid_parse(uuid, uu)) {
398 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
399 if (uuid[i] != '-') {
405 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
406 type ?: "", type ? "-" : "",
407 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
410 log_dbg("DM-UUID is %s", buf);
412 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
415 int dm_create_device(const char *name,
417 struct crypt_dm_active_device *dmd,
420 struct dm_task *dmt = NULL;
423 char dev_uuid[DM_UUID_LEN] = {0};
425 uint32_t read_ahead = 0;
427 uint16_t udev_flags = 0;
429 params = get_params(dmd);
433 if (type && !strncmp(type, "TEMP", 4))
434 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
436 /* All devices must have DM_UUID, only resize on old device is exception */
438 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
441 if (!dm_task_set_name(dmt, name))
444 dm_prepare_uuid(name, type, dmd->uuid, dev_uuid, sizeof(dev_uuid));
446 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
449 if (!dm_task_set_name(dmt, name))
452 if (!dm_task_set_uuid(dmt, dev_uuid))
455 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
459 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
461 if ((dmd->flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
463 if (!dm_task_add_target(dmt, 0, dmd->size, DM_CRYPT_TARGET, params))
466 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
467 if (device_read_ahead(dmd->device, &read_ahead) &&
468 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
472 if (!dm_task_run(dmt))
476 dm_task_destroy(dmt);
477 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
479 if (!dm_task_set_name(dmt, name))
481 if (dmd->uuid && !dm_task_set_uuid(dmt, dev_uuid))
483 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
485 if (!dm_task_run(dmt))
489 if (!dm_task_get_info(dmt, &dmi))
494 if (_dm_use_udev()) {
495 (void)_dm_udev_wait(cookie);
499 if (r < 0 && !reload)
500 dm_remove_device(name, 0, 0);
503 if (cookie && _dm_use_udev())
504 (void)_dm_udev_wait(cookie);
507 crypt_safe_free(params);
509 dm_task_destroy(dmt);
511 dm_task_update_nodes();
515 static int dm_status_dmi(const char *name, struct dm_info *dmi)
518 uint64_t start, length;
519 char *target_type, *params;
523 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
526 if (!dm_task_set_name(dmt, name))
529 if (!dm_task_run(dmt))
532 if (!dm_task_get_info(dmt, dmi))
540 next = dm_get_next_target(dmt, next, &start, &length,
541 &target_type, ¶ms);
542 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
549 dm_task_destroy(dmt);
554 int dm_status_device(const char *name)
559 r = dm_status_dmi(name, &dmi);
563 return (dmi.open_count > 0);
566 int dm_status_suspended(const char *name)
571 r = dm_status_dmi(name, &dmi);
575 return dmi.suspended ? 1 : 0;
578 int dm_query_device(const char *name, uint32_t get_flags,
579 struct crypt_dm_active_device *dmd)
583 uint64_t start, length, val64;
584 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
585 const char *tmp_uuid;
590 memset(dmd, 0, sizeof(*dmd));
592 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
594 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
596 if (!dm_task_set_name(dmt, name))
599 if (!dm_task_run(dmt))
603 if (!dm_task_get_info(dmt, &dmi))
611 tmp_uuid = dm_task_get_uuid(dmt);
613 next = dm_get_next_target(dmt, next, &start, &length,
614 &target_type, ¶ms);
615 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
621 rcipher = strsep(¶ms, " ");
623 if (get_flags & DM_ACTIVE_CIPHER)
624 dmd->cipher = strdup(rcipher);
627 key_ = strsep(¶ms, " ");
630 val64 = strtoull(params, ¶ms, 10);
635 dmd->iv_offset = val64;
638 rdevice = strsep(¶ms, " ");
639 if (get_flags & DM_ACTIVE_DEVICE)
640 dmd->device = crypt_lookup_dev(rdevice);
645 val64 = strtoull(params, ¶ms, 10);
648 /* Features section, available since crypt target version 1.11 */
654 /* Number of arguments */
655 val64 = strtoull(params, ¶ms, 10);
660 for (i = 0; i < val64; i++) {
663 arg = strsep(¶ms, " ");
664 if (!strcasecmp(arg, "allow_discards"))
665 dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
666 else /* unknown option */
670 /* All parameters shold be processed */
675 /* Never allow to return empty key */
676 if ((get_flags & DM_ACTIVE_KEY) && dmi.suspended) {
677 log_dbg("Cannot read volume key while suspended.");
682 if (get_flags & DM_ACTIVE_KEYSIZE) {
683 dmd->vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
689 if (get_flags & DM_ACTIVE_KEY) {
691 for(i = 0; i < dmd->vk->keylength; i++) {
692 memcpy(buffer, &key_[i * 2], 2);
693 dmd->vk->key[i] = strtoul(buffer, &endp, 16);
694 if (endp != &buffer[2]) {
695 crypt_free_volume_key(dmd->vk);
703 memset(key_, 0, strlen(key_));
706 dmd->flags |= CRYPT_ACTIVATE_READONLY;
709 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
710 else if (get_flags & DM_ACTIVE_UUID) {
711 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
712 dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
715 r = (dmi.open_count > 0);
718 dm_task_destroy(dmt);
723 static int _dm_message(const char *name, const char *msg)
728 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
731 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
734 if (name && !dm_task_set_name(dmt, name))
737 if (!dm_task_set_sector(dmt, (uint64_t) 0))
740 if (!dm_task_set_message(dmt, msg))
743 r = dm_task_run(dmt);
746 dm_task_destroy(dmt);
750 int dm_suspend_and_wipe_key(const char *name)
752 if (!_dm_check_versions())
755 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
758 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
761 if (!_dm_message(name, "key wipe")) {
762 _dm_simple(DM_DEVICE_RESUME, name, 1);
769 int dm_resume_and_reinstate_key(const char *name,
773 int msg_size = key_size * 2 + 10; // key set <key>
777 if (!_dm_check_versions())
780 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
783 msg = crypt_safe_alloc(msg_size);
787 memset(msg, 0, msg_size);
788 strcpy(msg, "key set ");
789 hex_key(&msg[8], key_size, key);
791 if (!_dm_message(name, msg) ||
792 !_dm_simple(DM_DEVICE_RESUME, name, 1))
795 crypt_safe_free(msg);
799 const char *dm_get_dir(void)
804 int dm_is_dm_device(int major, int minor)
806 return dm_is_dm_major((uint32_t)major);
809 int dm_is_dm_kernel_name(const char *name)
811 return strncmp(name, "dm-", 3) ? 0 : 1;
814 int dm_check_segment(const char *name, uint64_t offset, uint64_t size)
816 struct crypt_dm_active_device dmd;
819 log_dbg("Checking segments for device %s.", name);
821 r = dm_query_device(name, 0, &dmd);
825 if (offset >= (dmd.offset + dmd.size) || (offset + size) <= dmd.offset)
830 log_dbg("seg: %" PRIu64 " - %" PRIu64 ", new %" PRIu64 " - %" PRIu64 "%s",
831 dmd.offset, dmd.offset + dmd.size, offset, offset + size,
832 r ? " (overlapping)" : " (ok)");