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 uint32_t dm_flags(void)
57 return _dm_crypt_flags;
60 static void set_dm_error(int level, const char *file, int line,
67 if (vasprintf(&msg, f, va) > 0) {
69 log_err(_context, msg);
70 log_err(_context, "\n");
78 static int _dm_simple(int task, const char *name, int udev_wait);
80 static void _dm_set_crypt_compat(int maj, int min, int patch)
82 log_dbg("Detected dm-crypt target of version %i.%i.%i.", maj, min, patch);
84 if (maj >= 1 && min >=2)
85 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
87 log_dbg("Suspend and resume disabled, no wipe key support.");
89 if (maj >= 1 && min >=10)
90 _dm_crypt_flags |= DM_LMK_SUPPORTED;
92 _dm_crypt_checked = 1;
95 static int _dm_check_versions(void)
98 struct dm_versions *target, *last_target;
100 if (_dm_crypt_checked)
103 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
106 if (!dm_task_run(dmt)) {
107 dm_task_destroy(dmt);
111 target = dm_task_get_versions(dmt);
113 last_target = target;
114 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
115 _dm_set_crypt_compat((int)target->version[0],
116 (int)target->version[1],
117 (int)target->version[2]);
119 target = (void *) target + target->next;
120 } while (last_target != target);
122 dm_task_destroy(dmt);
126 int dm_init(struct crypt_device *context, int check_kernel)
128 if (!_dm_use_count++) {
129 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
130 check_kernel ? "" : " (NO kernel check requested)",
131 _dm_use_udev() ? "en" : "dis");
132 if (check_kernel && !_dm_check_versions()) {
133 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
136 if (getuid() || geteuid())
137 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
138 dm_log_init(set_dm_error);
139 dm_log_init_verbose(10);
142 // FIXME: global context is not safe
146 return 1; /* unsafe memory */
151 if (_dm_use_count && (!--_dm_use_count)) {
152 log_dbg("Releasing device-mapper backend.");
153 dm_log_init_verbose(0);
160 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
162 struct dirent *entry;
169 /* Ignore strange nested directories */
170 if (dir_level > max_level)
173 path[PATH_MAX - 1] = '\0';
174 ptr = path + strlen(path);
177 space = PATH_MAX - (ptr - path);
183 while((entry = readdir(dir))) {
184 if (entry->d_name[0] == '.' ||
185 !strncmp(entry->d_name, "..", 2))
188 strncpy(ptr, entry->d_name, space);
189 if (stat(path, &st) < 0)
192 if (S_ISDIR(st.st_mode)) {
193 result = __lookup_dev(path, dev, dir_level + 1, max_level);
196 } else if (S_ISBLK(st.st_mode)) {
197 /* workaround: ignore dm-X devices, these are internal kernel names */
198 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
200 if (st.st_rdev == dev) {
201 result = strdup(path);
211 static char *lookup_dev(const char *dev_id)
213 uint32_t major, minor;
215 char *result = NULL, buf[PATH_MAX + 1];
217 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
220 dev = makedev(major, minor);
221 strncpy(buf, DEVICE_DIR, PATH_MAX);
222 buf[PATH_MAX] = '\0';
224 /* First try low level device */
225 if ((result = __lookup_dev(buf, dev, 0, 0)))
228 /* If it is dm, try DM dir */
229 if (dm_is_dm_major(major)) {
230 strncpy(buf, dm_dir(), PATH_MAX);
231 if ((result = __lookup_dev(buf, dev, 0, 0)))
235 strncpy(buf, DEVICE_DIR, PATH_MAX);
236 result = __lookup_dev(buf, dev, 0, 4);
238 /* If not found, return NULL */
242 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
245 long read_ahead_long;
247 if ((fd = open(dev, O_RDONLY)) < 0)
250 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
254 *read_ahead = (uint32_t) read_ahead_long;
259 static void hex_key(char *hexkey, size_t key_size, const char *key)
263 for(i = 0; i < key_size; i++)
264 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
267 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
268 const char *cipher, size_t key_size, const char *key)
273 hexkey = crypt_safe_alloc(key_size * 2 + 1);
277 hex_key(hexkey, key_size, key);
279 params = crypt_safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
283 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
284 cipher, hexkey, skip, device, offset);
287 crypt_safe_free(hexkey);
292 static int _dm_simple(int task, const char *name, int udev_wait)
301 if (!(dmt = dm_task_create(task)))
304 if (name && !dm_task_set_name(dmt, name))
307 if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
310 r = dm_task_run(dmt);
313 (void)_dm_udev_wait(cookie);
316 dm_task_destroy(dmt);
320 static int _error_device(const char *name, size_t size)
325 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
328 if (!dm_task_set_name(dmt, name))
331 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
334 if (!dm_task_set_ro(dmt))
337 if (!dm_task_no_open_count(dmt))
340 if (!dm_task_run(dmt))
343 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
344 _dm_simple(DM_DEVICE_CLEAR, name, 0);
351 dm_task_destroy(dmt);
355 int dm_remove_device(const char *name, int force, uint64_t size)
358 int retries = force ? RETRY_COUNT : 1;
359 int error_target = 0;
361 if (!name || (force && !size))
365 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
366 if (--retries && r) {
367 log_dbg("WARNING: other process locked internal device %s, %s.",
368 name, retries ? "retrying remove" : "giving up");
369 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
370 debug_processes_using_device(name);
372 if (force && !error_target) {
373 /* If force flag is set, replace device with error, read-only target.
374 * it should stop processes from reading it and also removed underlying
375 * device from mapping, so it is usable again.
376 * Force flag should be used only for temporary devices, which are
377 * intended to work inside cryptsetup only!
378 * Anyway, if some process try to read temporary cryptsetup device,
379 * it is bug - no other process should try touch it (e.g. udev).
381 _error_device(name, size);
385 } while (r == -EINVAL && retries);
387 dm_task_update_nodes();
392 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
394 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
396 * CRYPT-LUKS1-00000000000000000000000000000000-name
399 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
401 char *ptr, uuid2[UUID_LEN] = {0};
405 /* Remove '-' chars */
406 if (uuid && !uuid_parse(uuid, uu)) {
407 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
408 if (uuid[i] != '-') {
414 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
415 type ?: "", type ? "-" : "",
416 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
419 log_dbg("DM-UUID is %s", buf);
421 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
424 int dm_create_device(const char *name,
437 struct dm_task *dmt = NULL;
441 char dev_uuid[DM_UUID_LEN] = {0};
443 uint32_t read_ahead = 0;
445 uint16_t udev_flags = 0;
447 params = get_params(device, skip, offset, cipher, key_size, key);
451 if (type && !strncmp(type, "TEMP", 4))
452 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
454 /* All devices must have DM_UUID, only resize on old device is exception */
456 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
459 if (!dm_task_set_name(dmt, name))
462 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
464 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
467 if (!dm_task_set_name(dmt, name))
470 if (!dm_task_set_uuid(dmt, dev_uuid))
473 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
477 if (!dm_task_secure_data(dmt))
479 if (read_only && !dm_task_set_ro(dmt))
481 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
484 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
485 if (_dev_read_ahead(device, &read_ahead) &&
486 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
490 if (!dm_task_run(dmt))
494 dm_task_destroy(dmt);
495 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
497 if (!dm_task_set_name(dmt, name))
499 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
501 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
503 if (!dm_task_run(dmt))
507 if (!dm_task_get_info(dmt, &dmi))
512 if (_dm_use_udev()) {
513 (void)_dm_udev_wait(cookie);
517 if (r < 0 && !reload) {
519 error = strdup(get_error());
521 dm_remove_device(name, 0, 0);
530 if (cookie && _dm_use_udev())
531 (void)_dm_udev_wait(cookie);
534 crypt_safe_free(params);
536 dm_task_destroy(dmt);
538 dm_task_update_nodes();
542 int dm_status_device(const char *name)
546 uint64_t start, length;
547 char *target_type, *params;
551 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
554 if (!dm_task_set_name(dmt, name))
557 if (!dm_task_run(dmt))
560 if (!dm_task_get_info(dmt, &dmi))
568 next = dm_get_next_target(dmt, next, &start, &length,
569 &target_type, ¶ms);
570 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
574 r = (dmi.open_count > 0);
577 dm_task_destroy(dmt);
582 int dm_query_device(const char *name,
596 uint64_t start, length, val64;
597 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
601 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
603 if (!dm_task_secure_data(dmt))
605 if (!dm_task_set_name(dmt, name))
608 if (!dm_task_run(dmt))
612 if (!dm_task_get_info(dmt, &dmi))
620 next = dm_get_next_target(dmt, next, &start, &length,
621 &target_type, ¶ms);
622 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
629 rcipher = strsep(¶ms, " ");
632 *cipher = strdup(rcipher);
635 key_ = strsep(¶ms, " ");
638 val64 = strtoull(params, ¶ms, 10);
646 rdevice = strsep(¶ms, " ");
648 *device = lookup_dev(rdevice);
653 val64 = strtoull(params, ¶ms, 10);
661 *key_size = strlen(key_) / 2;
664 if (key_size && key) {
665 *key = crypt_safe_alloc(*key_size);
672 for(i = 0; i < *key_size; i++) {
673 memcpy(buffer, &key_[i * 2], 2);
674 (*key)[i] = strtoul(buffer, &endp, 16);
675 if (endp != &buffer[2]) {
676 crypt_safe_free(key);
682 memset(key_, 0, strlen(key_));
685 *read_only = dmi.read_only;
688 *suspended = dmi.suspended;
690 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
691 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
692 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
694 r = (dmi.open_count > 0);
697 dm_task_destroy(dmt);
702 static int _dm_message(const char *name, const char *msg)
707 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
710 if (!dm_task_secure_data(dmt))
713 if (name && !dm_task_set_name(dmt, name))
716 if (!dm_task_set_sector(dmt, (uint64_t) 0))
719 if (!dm_task_set_message(dmt, msg))
722 r = dm_task_run(dmt);
725 dm_task_destroy(dmt);
729 int dm_suspend_and_wipe_key(const char *name)
731 if (!_dm_check_versions())
734 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
737 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
740 if (!_dm_message(name, "key wipe")) {
741 _dm_simple(DM_DEVICE_RESUME, name, 1);
748 int dm_resume_and_reinstate_key(const char *name,
752 int msg_size = key_size * 2 + 10; // key set <key>
756 if (!_dm_check_versions())
759 if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
762 msg = crypt_safe_alloc(msg_size);
766 memset(msg, 0, msg_size);
767 strcpy(msg, "key set ");
768 hex_key(&msg[8], key_size, key);
770 if (!_dm_message(name, msg) ||
771 !_dm_simple(DM_DEVICE_RESUME, name, 1))
774 crypt_safe_free(msg);
778 const char *dm_get_dir(void)