6 #include <libdevmapper.h>
14 #define DEVICE_DIR "/dev"
15 #define DM_UUID_LEN 129
16 #define DM_UUID_PREFIX "CRYPT-"
17 #define DM_UUID_PREFIX_LEN 6
18 #define DM_CRYPT_TARGET "crypt"
21 /* Set if dm-crypt version was probed */
22 static int _dm_crypt_checked = 0;
23 static uint32_t _dm_crypt_flags = 0;
25 static int _dm_use_count = 0;
26 static struct crypt_device *_context = NULL;
28 /* Check if we have DM flag to instruct kernel to force wipe buffers */
29 #if !HAVE_DECL_DM_TASK_SECURE_DATA
30 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
33 /* Compatibility for old device-mapper without udev support */
34 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
35 #define CRYPT_TEMP_UDEV_FLAGS DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
36 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
37 DM_UDEV_DISABLE_OTHER_RULES_FLAG
38 #define _dm_task_set_cookie dm_task_set_cookie
39 #define _dm_udev_wait dm_udev_wait
41 #define CRYPT_TEMP_UDEV_FLAGS 0
42 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
43 static int _dm_udev_wait(uint32_t cookie) { return 0; };
46 static int _dm_use_udev()
48 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
49 return dm_udev_get_sync_support();
55 static void set_dm_error(int level, const char *file, int line,
62 if (vasprintf(&msg, f, va) > 0) {
64 log_err(_context, msg);
65 log_err(_context, "\n");
73 static int _dm_simple(int task, const char *name, int udev_wait);
75 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
76 unsigned crypt_min, unsigned crypt_patch)
78 unsigned dm_maj, dm_min, dm_patch;
80 if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
81 dm_maj = dm_min = dm_patch = 0;
83 log_dbg("Detected dm-crypt version %i.%i.%i, dm-ioctl version %u.%u.%u.",
84 crypt_maj, crypt_min, crypt_patch, dm_maj, dm_min, dm_patch);
86 if (crypt_maj >= 1 && crypt_min >= 2)
87 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
89 log_dbg("Suspend and resume disabled, no wipe key support.");
91 if (crypt_maj >= 1 && crypt_min >= 10)
92 _dm_crypt_flags |= DM_LMK_SUPPORTED;
94 if (dm_maj >= 4 && dm_min >= 20)
95 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
97 /* Repeat test if dm-crypt is not present */
99 _dm_crypt_checked = 1;
102 static int _dm_check_versions(void)
105 struct dm_versions *target, *last_target;
108 if (_dm_crypt_checked)
111 /* FIXME: add support to DM so it forces crypt target module load here */
112 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
115 if (!dm_task_run(dmt)) {
116 dm_task_destroy(dmt);
120 if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
121 dm_task_destroy(dmt);
125 target = dm_task_get_versions(dmt);
127 last_target = target;
128 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
129 _dm_set_crypt_compat(dm_version,
130 (unsigned)target->version[0],
131 (unsigned)target->version[1],
132 (unsigned)target->version[2]);
134 target = (void *) target + target->next;
135 } while (last_target != target);
137 dm_task_destroy(dmt);
141 uint32_t dm_flags(void)
143 if (!_dm_crypt_checked)
144 _dm_check_versions();
146 return _dm_crypt_flags;
149 int dm_init(struct crypt_device *context, int check_kernel)
151 if (!_dm_use_count++) {
152 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
153 check_kernel ? "" : " (NO kernel check requested)",
154 _dm_use_udev() ? "en" : "dis");
155 if (check_kernel && !_dm_check_versions()) {
156 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
159 if (getuid() || geteuid())
160 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
161 dm_log_init(set_dm_error);
162 dm_log_init_verbose(10);
165 // FIXME: global context is not safe
169 return 1; /* unsafe memory */
174 if (_dm_use_count && (!--_dm_use_count)) {
175 log_dbg("Releasing device-mapper backend.");
176 dm_log_init_verbose(0);
183 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
185 struct dirent *entry;
192 /* Ignore strange nested directories */
193 if (dir_level > max_level)
196 path[PATH_MAX - 1] = '\0';
197 ptr = path + strlen(path);
200 space = PATH_MAX - (ptr - path);
206 while((entry = readdir(dir))) {
207 if (entry->d_name[0] == '.' ||
208 !strncmp(entry->d_name, "..", 2))
211 strncpy(ptr, entry->d_name, space);
212 if (stat(path, &st) < 0)
215 if (S_ISDIR(st.st_mode)) {
216 result = __lookup_dev(path, dev, dir_level + 1, max_level);
219 } else if (S_ISBLK(st.st_mode)) {
220 /* workaround: ignore dm-X devices, these are internal kernel names */
221 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
223 if (st.st_rdev == dev) {
224 result = strdup(path);
234 static char *lookup_dev(const char *dev_id)
236 uint32_t major, minor;
238 char *result = NULL, buf[PATH_MAX + 1];
240 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
243 dev = makedev(major, minor);
244 strncpy(buf, DEVICE_DIR, PATH_MAX);
245 buf[PATH_MAX] = '\0';
247 /* First try low level device */
248 if ((result = __lookup_dev(buf, dev, 0, 0)))
251 /* If it is dm, try DM dir */
252 if (dm_is_dm_major(major)) {
253 strncpy(buf, dm_dir(), PATH_MAX);
254 if ((result = __lookup_dev(buf, dev, 0, 0)))
258 strncpy(buf, DEVICE_DIR, PATH_MAX);
259 result = __lookup_dev(buf, dev, 0, 4);
261 /* If not found, return NULL */
265 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
268 long read_ahead_long;
270 if ((fd = open(dev, O_RDONLY)) < 0)
273 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
277 *read_ahead = (uint32_t) read_ahead_long;
282 static void hex_key(char *hexkey, size_t key_size, const char *key)
286 for(i = 0; i < key_size; i++)
287 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
290 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
291 const char *cipher, size_t key_size, const char *key)
296 hexkey = crypt_safe_alloc(key_size * 2 + 1);
300 hex_key(hexkey, key_size, key);
302 params = crypt_safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
306 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
307 cipher, hexkey, skip, device, offset);
310 crypt_safe_free(hexkey);
315 static int _dm_simple(int task, const char *name, int udev_wait)
324 if (!(dmt = dm_task_create(task)))
327 if (name && !dm_task_set_name(dmt, name))
330 if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
333 r = dm_task_run(dmt);
336 (void)_dm_udev_wait(cookie);
339 dm_task_destroy(dmt);
343 static int _error_device(const char *name, size_t size)
348 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
351 if (!dm_task_set_name(dmt, name))
354 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
357 if (!dm_task_set_ro(dmt))
360 if (!dm_task_no_open_count(dmt))
363 if (!dm_task_run(dmt))
366 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
367 _dm_simple(DM_DEVICE_CLEAR, name, 0);
374 dm_task_destroy(dmt);
378 int dm_remove_device(const char *name, int force, uint64_t size)
381 int retries = force ? RETRY_COUNT : 1;
382 int error_target = 0;
384 if (!name || (force && !size))
388 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
389 if (--retries && r) {
390 log_dbg("WARNING: other process locked internal device %s, %s.",
391 name, retries ? "retrying remove" : "giving up");
392 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
393 debug_processes_using_device(name);
395 if (force && !error_target) {
396 /* If force flag is set, replace device with error, read-only target.
397 * it should stop processes from reading it and also removed underlying
398 * device from mapping, so it is usable again.
399 * Force flag should be used only for temporary devices, which are
400 * intended to work inside cryptsetup only!
401 * Anyway, if some process try to read temporary cryptsetup device,
402 * it is bug - no other process should try touch it (e.g. udev).
404 _error_device(name, size);
408 } while (r == -EINVAL && retries);
410 dm_task_update_nodes();
415 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
417 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
419 * CRYPT-LUKS1-00000000000000000000000000000000-name
422 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
424 char *ptr, uuid2[UUID_LEN] = {0};
428 /* Remove '-' chars */
429 if (uuid && !uuid_parse(uuid, uu)) {
430 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
431 if (uuid[i] != '-') {
437 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
438 type ?: "", type ? "-" : "",
439 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
442 log_dbg("DM-UUID is %s", buf);
444 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
447 int dm_create_device(const char *name,
460 struct dm_task *dmt = NULL;
464 char dev_uuid[DM_UUID_LEN] = {0};
466 uint32_t read_ahead = 0;
468 uint16_t udev_flags = 0;
470 params = get_params(device, skip, offset, cipher, key_size, key);
474 if (type && !strncmp(type, "TEMP", 4))
475 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
477 /* All devices must have DM_UUID, only resize on old device is exception */
479 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
482 if (!dm_task_set_name(dmt, name))
485 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
487 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
490 if (!dm_task_set_name(dmt, name))
493 if (!dm_task_set_uuid(dmt, dev_uuid))
496 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
500 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
502 if (read_only && !dm_task_set_ro(dmt))
504 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
507 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
508 if (_dev_read_ahead(device, &read_ahead) &&
509 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
513 if (!dm_task_run(dmt))
517 dm_task_destroy(dmt);
518 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
520 if (!dm_task_set_name(dmt, name))
522 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
524 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
526 if (!dm_task_run(dmt))
530 if (!dm_task_get_info(dmt, &dmi))
535 if (_dm_use_udev()) {
536 (void)_dm_udev_wait(cookie);
540 if (r < 0 && !reload) {
542 error = strdup(get_error());
544 dm_remove_device(name, 0, 0);
553 if (cookie && _dm_use_udev())
554 (void)_dm_udev_wait(cookie);
557 crypt_safe_free(params);
559 dm_task_destroy(dmt);
561 dm_task_update_nodes();
565 int dm_status_device(const char *name)
569 uint64_t start, length;
570 char *target_type, *params;
574 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
577 if (!dm_task_set_name(dmt, name))
580 if (!dm_task_run(dmt))
583 if (!dm_task_get_info(dmt, &dmi))
591 next = dm_get_next_target(dmt, next, &start, &length,
592 &target_type, ¶ms);
593 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
597 r = (dmi.open_count > 0);
600 dm_task_destroy(dmt);
605 int dm_query_device(const char *name,
619 uint64_t start, length, val64;
620 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
624 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
626 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
628 if (!dm_task_set_name(dmt, name))
631 if (!dm_task_run(dmt))
635 if (!dm_task_get_info(dmt, &dmi))
643 next = dm_get_next_target(dmt, next, &start, &length,
644 &target_type, ¶ms);
645 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
652 rcipher = strsep(¶ms, " ");
655 *cipher = strdup(rcipher);
658 key_ = strsep(¶ms, " ");
661 val64 = strtoull(params, ¶ms, 10);
669 rdevice = strsep(¶ms, " ");
671 *device = lookup_dev(rdevice);
676 val64 = strtoull(params, ¶ms, 10);
684 *key_size = strlen(key_) / 2;
687 if (key_size && key) {
688 *key = crypt_safe_alloc(*key_size);
695 for(i = 0; i < *key_size; i++) {
696 memcpy(buffer, &key_[i * 2], 2);
697 (*key)[i] = strtoul(buffer, &endp, 16);
698 if (endp != &buffer[2]) {
699 crypt_safe_free(key);
705 memset(key_, 0, strlen(key_));
708 *read_only = dmi.read_only;
711 *suspended = dmi.suspended;
713 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
714 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
715 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
717 r = (dmi.open_count > 0);
720 dm_task_destroy(dmt);
725 static int _dm_message(const char *name, const char *msg)
730 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
733 if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
736 if (name && !dm_task_set_name(dmt, name))
739 if (!dm_task_set_sector(dmt, (uint64_t) 0))
742 if (!dm_task_set_message(dmt, msg))
745 r = dm_task_run(dmt);
748 dm_task_destroy(dmt);
752 int dm_suspend_and_wipe_key(const char *name)
754 if (!_dm_check_versions())
757 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
760 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
763 if (!_dm_message(name, "key wipe")) {
764 _dm_simple(DM_DEVICE_RESUME, name, 1);
771 int dm_resume_and_reinstate_key(const char *name,
775 int msg_size = key_size * 2 + 10; // key set <key>
779 if (!_dm_check_versions())
782 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
785 msg = crypt_safe_alloc(msg_size);
789 memset(msg, 0, msg_size);
790 strcpy(msg, "key set ");
791 hex_key(&msg[8], key_size, key);
793 if (!_dm_message(name, msg) ||
794 !_dm_simple(DM_DEVICE_RESUME, name, 1))
797 crypt_safe_free(msg);
801 const char *dm_get_dir(void)