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-2011, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 /* Repeat test if dm-crypt is not present */
124 _dm_crypt_checked = 1;
127 static int _dm_check_versions(void)
130 struct dm_versions *target, *last_target;
133 if (_dm_crypt_checked)
136 /* FIXME: add support to DM so it forces crypt target module load here */
137 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
140 if (!dm_task_run(dmt)) {
141 dm_task_destroy(dmt);
145 if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
146 dm_task_destroy(dmt);
150 target = dm_task_get_versions(dmt);
152 last_target = target;
153 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
154 _dm_set_crypt_compat(dm_version,
155 (unsigned)target->version[0],
156 (unsigned)target->version[1],
157 (unsigned)target->version[2]);
159 target = (struct dm_versions *)((char *) target + target->next);
160 } while (last_target != target);
162 dm_task_destroy(dmt);
166 uint32_t dm_flags(void)
168 if (!_dm_crypt_checked)
169 _dm_check_versions();
171 return _dm_crypt_flags;
174 int dm_init(struct crypt_device *context, int check_kernel)
176 if (!_dm_use_count++) {
177 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
178 check_kernel ? "" : " (NO kernel check requested)",
179 _dm_use_udev() ? "en" : "dis");
180 if (check_kernel && !_dm_check_versions()) {
181 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
184 if (getuid() || geteuid())
185 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
186 dm_log_init(set_dm_error);
187 dm_log_init_verbose(10);
190 // FIXME: global context is not safe
194 return 1; /* unsafe memory */
199 if (_dm_use_count && (!--_dm_use_count)) {
200 log_dbg("Releasing device-mapper backend.");
201 dm_log_init_verbose(0);
208 /* Return path to DM device */
209 char *dm_device_path(const char *dev_id)
216 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
219 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
221 if (!dm_task_set_minor(dmt, minor) ||
222 !dm_task_set_major(dmt, major) ||
224 !(name = dm_task_get_name(dmt))) {
225 dm_task_destroy(dmt);
229 if (snprintf(path, sizeof(path), "/dev/mapper/%s", name) < 0)
232 dm_task_destroy(dmt);
237 static void hex_key(char *hexkey, size_t key_size, const char *key)
241 for(i = 0; i < key_size; i++)
242 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
245 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
246 const char *cipher, size_t key_size, const char *key)
251 hexkey = crypt_safe_alloc(key_size * 2 + 1);
255 hex_key(hexkey, key_size, key);
257 params = crypt_safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
261 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
262 cipher, hexkey, skip, device, offset);
265 crypt_safe_free(hexkey);
270 static int _dm_simple(int task, const char *name, int udev_wait)
279 if (!(dmt = dm_task_create(task)))
282 if (name && !dm_task_set_name(dmt, name))
285 if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
288 r = dm_task_run(dmt);
291 (void)_dm_udev_wait(cookie);
294 dm_task_destroy(dmt);
298 static int _error_device(const char *name, size_t size)
303 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
306 if (!dm_task_set_name(dmt, name))
309 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
312 if (!dm_task_set_ro(dmt))
315 if (!dm_task_no_open_count(dmt))
318 if (!dm_task_run(dmt))
321 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
322 _dm_simple(DM_DEVICE_CLEAR, name, 0);
329 dm_task_destroy(dmt);
333 int dm_remove_device(const char *name, int force, uint64_t size)
336 int retries = force ? RETRY_COUNT : 1;
337 int error_target = 0;
339 if (!name || (force && !size))
343 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
344 if (--retries && r) {
345 log_dbg("WARNING: other process locked internal device %s, %s.",
346 name, retries ? "retrying remove" : "giving up");
347 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
348 debug_processes_using_device(name);
350 if (force && !error_target) {
351 /* If force flag is set, replace device with error, read-only target.
352 * it should stop processes from reading it and also removed underlying
353 * device from mapping, so it is usable again.
354 * Force flag should be used only for temporary devices, which are
355 * intended to work inside cryptsetup only!
356 * Anyway, if some process try to read temporary cryptsetup device,
357 * it is bug - no other process should try touch it (e.g. udev).
359 _error_device(name, size);
363 } while (r == -EINVAL && retries);
365 dm_task_update_nodes();
370 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
372 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
374 * CRYPT-LUKS1-00000000000000000000000000000000-name
377 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
379 char *ptr, uuid2[UUID_LEN] = {0};
383 /* Remove '-' chars */
384 if (uuid && !uuid_parse(uuid, uu)) {
385 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
386 if (uuid[i] != '-') {
392 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
393 type ?: "", type ? "-" : "",
394 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
397 log_dbg("DM-UUID is %s", buf);
399 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
402 int dm_create_device(const char *name,
415 struct dm_task *dmt = NULL;
419 char dev_uuid[DM_UUID_LEN] = {0};
421 uint32_t read_ahead = 0;
423 uint16_t udev_flags = 0;
425 params = get_params(device, skip, offset, cipher, key_size, key);
429 if (type && !strncmp(type, "TEMP", 4))
430 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
432 /* All devices must have DM_UUID, only resize on old device is exception */
434 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
437 if (!dm_task_set_name(dmt, name))
440 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
442 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
445 if (!dm_task_set_name(dmt, name))
448 if (!dm_task_set_uuid(dmt, dev_uuid))
451 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
455 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
457 if (read_only && !dm_task_set_ro(dmt))
459 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
462 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
463 if (device_read_ahead(device, &read_ahead) &&
464 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
468 if (!dm_task_run(dmt))
472 dm_task_destroy(dmt);
473 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
475 if (!dm_task_set_name(dmt, name))
477 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
479 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
481 if (!dm_task_run(dmt))
485 if (!dm_task_get_info(dmt, &dmi))
490 if (_dm_use_udev()) {
491 (void)_dm_udev_wait(cookie);
495 if (r < 0 && !reload) {
497 error = strdup(get_error());
499 dm_remove_device(name, 0, 0);
508 if (cookie && _dm_use_udev())
509 (void)_dm_udev_wait(cookie);
512 crypt_safe_free(params);
514 dm_task_destroy(dmt);
516 dm_task_update_nodes();
520 int dm_status_device(const char *name)
524 uint64_t start, length;
525 char *target_type, *params;
529 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
532 if (!dm_task_set_name(dmt, name))
535 if (!dm_task_run(dmt))
538 if (!dm_task_get_info(dmt, &dmi))
546 next = dm_get_next_target(dmt, next, &start, &length,
547 &target_type, ¶ms);
548 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
552 r = (dmi.open_count > 0);
555 dm_task_destroy(dmt);
560 int dm_query_device(const char *name,
574 uint64_t start, length, val64;
575 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3];
576 const char *tmp_uuid;
580 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
582 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
584 if (!dm_task_set_name(dmt, name))
587 if (!dm_task_run(dmt))
591 if (!dm_task_get_info(dmt, &dmi))
599 next = dm_get_next_target(dmt, next, &start, &length,
600 &target_type, ¶ms);
601 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
608 rcipher = strsep(¶ms, " ");
611 *cipher = strdup(rcipher);
614 key_ = strsep(¶ms, " ");
617 val64 = strtoull(params, ¶ms, 10);
625 rdevice = strsep(¶ms, " ");
627 *device = crypt_lookup_dev(rdevice);
632 val64 = strtoull(params, ¶ms, 10);
640 *key_size = strlen(key_) / 2;
643 if (key_size && key) {
644 *key = crypt_safe_alloc(*key_size);
651 for(i = 0; i < *key_size; i++) {
652 memcpy(buffer, &key_[i * 2], 2);
653 (*key)[i] = strtoul(buffer, &endp, 16);
654 if (endp != &buffer[2]) {
655 crypt_safe_free(key);
661 memset(key_, 0, strlen(key_));
664 *read_only = dmi.read_only;
667 *suspended = dmi.suspended;
669 if (uuid && (tmp_uuid = dm_task_get_uuid(dmt)) &&
670 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
671 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
673 r = (dmi.open_count > 0);
676 dm_task_destroy(dmt);
681 static int _dm_message(const char *name, const char *msg)
686 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
689 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
692 if (name && !dm_task_set_name(dmt, name))
695 if (!dm_task_set_sector(dmt, (uint64_t) 0))
698 if (!dm_task_set_message(dmt, msg))
701 r = dm_task_run(dmt);
704 dm_task_destroy(dmt);
708 int dm_suspend_and_wipe_key(const char *name)
710 if (!_dm_check_versions())
713 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
716 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
719 if (!_dm_message(name, "key wipe")) {
720 _dm_simple(DM_DEVICE_RESUME, name, 1);
727 int dm_resume_and_reinstate_key(const char *name,
731 int msg_size = key_size * 2 + 10; // key set <key>
735 if (!_dm_check_versions())
738 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
741 msg = crypt_safe_alloc(msg_size);
745 memset(msg, 0, msg_size);
746 strcpy(msg, "key set ");
747 hex_key(&msg[8], key_size, key);
749 if (!_dm_message(name, msg) ||
750 !_dm_simple(DM_DEVICE_RESUME, name, 1))
753 crypt_safe_free(msg);
757 const char *dm_get_dir(void)
762 int dm_is_dm_device(int major)
764 return dm_is_dm_major((uint32_t)major);