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 int _dm_crypt_wipe_key_supported = 0;
25 static int _dm_use_count = 0;
26 static struct crypt_device *_context = NULL;
28 /* Compatibility for old device-mapper without udev support */
29 #ifndef HAVE_DM_TASK_SET_COOKIE
30 #define CRYPT_TEMP_UDEV_FLAGS 0
31 static int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
32 static int dm_udev_wait(uint32_t cookie) { return 0; };
34 #define CRYPT_TEMP_UDEV_FLAGS DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
35 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
36 DM_UDEV_DISABLE_OTHER_RULES_FLAG
39 static int _dm_use_udev()
41 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
42 return dm_udev_get_sync_support();
48 static void set_dm_error(int level, const char *file, int line,
55 if (vasprintf(&msg, f, va) > 0) {
57 log_err(_context, msg);
58 log_err(_context, "\n");
66 static int _dm_simple(int task, const char *name, int udev_wait);
68 static void _dm_set_crypt_compat(int maj, int min, int patch)
70 log_dbg("Detected dm-crypt target of version %i.%i.%i.", maj, min, patch);
72 if (maj >= 1 && min >=2)
73 _dm_crypt_wipe_key_supported = 1;
75 log_dbg("Suspend and resume disabled, no wipe key support.");
77 _dm_crypt_checked = 1;
80 static int _dm_check_versions(void)
83 struct dm_versions *target, *last_target;
85 if (_dm_crypt_checked)
88 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
91 if (!dm_task_run(dmt)) {
96 target = dm_task_get_versions(dmt);
99 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
100 _dm_set_crypt_compat((int)target->version[0],
101 (int)target->version[1],
102 (int)target->version[2]);
104 target = (void *) target + target->next;
105 } while (last_target != target);
107 dm_task_destroy(dmt);
111 int dm_init(struct crypt_device *context, int check_kernel)
113 if (!_dm_use_count++) {
114 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
115 check_kernel ? "" : " (NO kernel check requested)",
116 _dm_use_udev() ? "en" : "dis");
117 if (check_kernel && !_dm_check_versions()) {
118 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
121 if (getuid() || geteuid())
122 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
123 dm_log_init(set_dm_error);
124 dm_log_init_verbose(10);
127 // FIXME: global context is not safe
131 return 1; /* unsafe memory */
136 if (_dm_use_count && (!--_dm_use_count)) {
137 log_dbg("Releasing device-mapper backend.");
138 dm_log_init_verbose(0);
145 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
147 struct dirent *entry;
154 /* Ignore strange nested directories */
155 if (dir_level > max_level)
158 path[PATH_MAX - 1] = '\0';
159 ptr = path + strlen(path);
162 space = PATH_MAX - (ptr - path);
168 while((entry = readdir(dir))) {
169 if (entry->d_name[0] == '.' ||
170 !strncmp(entry->d_name, "..", 2))
173 strncpy(ptr, entry->d_name, space);
174 if (stat(path, &st) < 0)
177 if (S_ISDIR(st.st_mode)) {
178 result = __lookup_dev(path, dev, dir_level + 1, max_level);
181 } else if (S_ISBLK(st.st_mode)) {
182 /* workaround: ignore dm-X devices, these are internal kernel names */
183 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
185 if (st.st_rdev == dev) {
186 result = strdup(path);
196 static char *lookup_dev(const char *dev_id)
198 uint32_t major, minor;
200 char *result, buf[PATH_MAX + 1];
202 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
205 dev = makedev(major, minor);
206 strncpy(buf, DEVICE_DIR, PATH_MAX);
207 buf[PATH_MAX] = '\0';
209 /* First try low level device */
210 if ((result = __lookup_dev(buf, dev, 0, 0)))
213 /* If it is dm, try DM dir */
214 if (dm_is_dm_major(major)) {
215 strncpy(buf, dm_dir(), PATH_MAX);
216 if ((result = __lookup_dev(buf, dev, 0, 0)))
220 strncpy(buf, DEVICE_DIR, PATH_MAX);
221 result = __lookup_dev(buf, dev, 0, 4);
223 /* If not found, return major:minor */
224 return result ?: strdup(dev_id);
227 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
230 long read_ahead_long;
232 if ((fd = open(dev, O_RDONLY)) < 0)
235 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
239 *read_ahead = (uint32_t) read_ahead_long;
244 static void hex_key(char *hexkey, size_t key_size, const char *key)
248 for(i = 0; i < key_size; i++)
249 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
252 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
253 const char *cipher, size_t key_size, const char *key)
258 hexkey = safe_alloc(key_size * 2 + 1);
262 hex_key(hexkey, key_size, key);
264 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
268 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
269 cipher, hexkey, skip, device, offset);
277 static int _dm_simple(int task, const char *name, int udev_wait)
286 if (!(dmt = dm_task_create(task)))
289 if (name && !dm_task_set_name(dmt, name))
292 if (udev_wait && !dm_task_set_cookie(dmt, &cookie, 0))
295 r = dm_task_run(dmt);
298 (void)dm_udev_wait(cookie);
301 dm_task_destroy(dmt);
305 static int _error_device(const char *name, size_t size)
310 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
313 if (!dm_task_set_name(dmt, name))
316 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
319 if (!dm_task_set_ro(dmt))
322 if (!dm_task_no_open_count(dmt))
325 if (!dm_task_run(dmt))
328 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
329 _dm_simple(DM_DEVICE_CLEAR, name, 0);
336 dm_task_destroy(dmt);
340 int dm_remove_device(const char *name, int force, uint64_t size)
343 int retries = force ? RETRY_COUNT : 1;
344 int error_target = 0;
346 if (!name || (force && !size))
350 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
351 if (--retries && r) {
352 log_dbg("WARNING: other process locked internal device %s, %s.",
353 name, retries ? "retrying remove" : "giving up");
354 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
355 debug_processes_using_device(name);
357 if (force && !error_target) {
358 /* If force flag is set, replace device with error, read-only target.
359 * it should stop processes from reading it and also removed underlying
360 * device from mapping, so it is usable again.
361 * Force flag should be used only for temporary devices, which are
362 * intended to work inside cryptsetup only!
363 * Anyway, if some process try to read temporary cryptsetup device,
364 * it is bug - no other process should try touch it (e.g. udev).
366 _error_device(name, size);
370 } while (r == -EINVAL && retries);
372 dm_task_update_nodes();
377 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
379 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
381 * CRYPT-LUKS1-00000000000000000000000000000000-name
384 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
386 char *ptr, uuid2[UUID_LEN] = {0};
390 /* Remove '-' chars */
391 if (uuid && !uuid_parse(uuid, uu)) {
392 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
393 if (uuid[i] != '-') {
399 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
400 type ?: "", type ? "-" : "",
401 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
404 log_dbg("DM-UUID is %s", buf);
406 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
409 int dm_create_device(const char *name,
422 struct dm_task *dmt = NULL;
426 char dev_uuid[DM_UUID_LEN] = {0};
428 uint32_t read_ahead = 0;
430 uint16_t udev_flags = 0;
432 params = get_params(device, skip, offset, cipher, key_size, key);
436 if (type && !strncmp(type, "TEMP", 4))
437 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
439 /* All devices must have DM_UUID, only resize on old device is exception */
441 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
444 if (!dm_task_set_name(dmt, name))
447 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
449 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
452 if (!dm_task_set_name(dmt, name))
455 if (!dm_task_set_uuid(dmt, dev_uuid))
458 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
462 if (read_only && !dm_task_set_ro(dmt))
464 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
467 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
468 if (_dev_read_ahead(device, &read_ahead) &&
469 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
473 if (!dm_task_run(dmt))
477 dm_task_destroy(dmt);
478 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
480 if (!dm_task_set_name(dmt, name))
482 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
484 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
486 if (!dm_task_run(dmt))
490 if (!dm_task_get_info(dmt, &dmi))
495 if (_dm_use_udev()) {
496 (void)dm_udev_wait(cookie);
500 if (r < 0 && !reload) {
502 error = strdup(get_error());
504 dm_remove_device(name, 0, 0);
513 if (cookie && _dm_use_udev())
514 (void)dm_udev_wait(cookie);
519 dm_task_destroy(dmt);
521 dm_task_update_nodes();
525 int dm_status_device(const char *name)
529 uint64_t start, length;
530 char *target_type, *params;
534 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
537 if (!dm_task_set_name(dmt, name)) {
542 if (!dm_task_run(dmt)) {
547 if (!dm_task_get_info(dmt, &dmi)) {
557 next = dm_get_next_target(dmt, next, &start, &length,
558 &target_type, ¶ms);
559 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
563 r = (dmi.open_count > 0);
566 dm_task_destroy(dmt);
571 int dm_query_device(const char *name,
585 uint64_t start, length, val64;
586 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
590 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
592 if (!dm_task_set_name(dmt, name))
595 if (!dm_task_run(dmt))
599 if (!dm_task_get_info(dmt, &dmi))
607 next = dm_get_next_target(dmt, next, &start, &length,
608 &target_type, ¶ms);
609 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
616 rcipher = strsep(¶ms, " ");
619 *cipher = strdup(rcipher);
622 key_ = strsep(¶ms, " ");
625 val64 = strtoull(params, ¶ms, 10);
633 rdevice = strsep(¶ms, " ");
635 *device = lookup_dev(rdevice);
640 val64 = strtoull(params, ¶ms, 10);
648 *key_size = strlen(key_) / 2;
651 if (key_size && key) {
652 *key = safe_alloc(*key_size);
659 for(i = 0; i < *key_size; i++) {
660 memcpy(buffer, &key_[i * 2], 2);
661 (*key)[i] = strtoul(buffer, &endp, 16);
662 if (endp != &buffer[2]) {
669 memset(key_, 0, strlen(key_));
672 *read_only = dmi.read_only;
675 *suspended = dmi.suspended;
677 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
678 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
679 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
681 r = (dmi.open_count > 0);
684 dm_task_destroy(dmt);
689 static int _dm_message(const char *name, const char *msg)
694 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
697 if (name && !dm_task_set_name(dmt, name))
700 if (!dm_task_set_sector(dmt, (uint64_t) 0))
703 if (!dm_task_set_message(dmt, msg))
706 r = dm_task_run(dmt);
709 dm_task_destroy(dmt);
713 int dm_suspend_and_wipe_key(const char *name)
715 if (!_dm_check_versions())
718 if (!_dm_crypt_wipe_key_supported)
721 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
724 if (!_dm_message(name, "key wipe")) {
725 _dm_simple(DM_DEVICE_RESUME, name, 1);
732 int dm_resume_and_reinstate_key(const char *name,
736 int msg_size = key_size * 2 + 10; // key set <key>
740 if (!_dm_check_versions())
743 if (!_dm_crypt_wipe_key_supported)
746 msg = safe_alloc(msg_size);
750 memset(msg, 0, msg_size);
751 strcpy(msg, "key set ");
752 hex_key(&msg[8], key_size, key);
754 if (!_dm_message(name, msg) ||
755 !_dm_simple(DM_DEVICE_RESUME, name, 1))
762 const char *dm_get_dir(void)