7 #include <libdevmapper.h>
8 #include <linux/dm-ioctl.h>
11 #include <uuid/uuid.h>
16 #define DEVICE_DIR "/dev"
17 #define DM_UUID_PREFIX "CRYPT-"
18 #define DM_UUID_PREFIX_LEN 6
19 #define DM_CRYPT_TARGET "crypt"
22 static int _dm_use_count = 0;
23 static struct crypt_device *_context = NULL;
25 /* Compatibility for old device-mapper without udev support */
26 #ifndef HAVE_DM_TASK_SET_COOKIE
27 #define CRYPT_TEMP_UDEV_FLAGS 0
28 static int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
29 static int dm_udev_wait(uint32_t cookie) { return 0; };
31 #define CRYPT_TEMP_UDEV_FLAGS DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
32 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
33 DM_UDEV_DISABLE_OTHER_RULES_FLAG
36 static int _dm_use_udev()
38 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
39 return dm_udev_get_sync_support();
45 static void set_dm_error(int level, const char *file, int line,
52 if (vasprintf(&msg, f, va) > 0) {
54 log_err(_context, msg);
55 log_err(_context, "\n");
63 static int _dm_simple(int task, const char *name, int udev_wait);
65 int dm_init(struct crypt_device *context, int check_kernel)
67 if (!_dm_use_count++) {
68 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
69 check_kernel ? "" : " (NO kernel check requested)",
70 _dm_use_udev() ? "en" : "dis");
71 if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL, 0)) {
72 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
75 if (getuid() || geteuid())
76 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
77 dm_log_init(set_dm_error);
78 dm_log_init_verbose(10);
81 // FIXME: global context is not safe
85 return 1; /* unsafe memory */
90 if (_dm_use_count && (!--_dm_use_count)) {
91 log_dbg("Releasing device-mapper backend.");
92 dm_log_init_verbose(0);
99 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
101 struct dirent *entry;
108 /* Ignore strange nested directories */
109 if (dir_level > max_level)
112 path[PATH_MAX - 1] = '\0';
113 ptr = path + strlen(path);
116 space = PATH_MAX - (ptr - path);
122 while((entry = readdir(dir))) {
123 if (entry->d_name[0] == '.' ||
124 !strncmp(entry->d_name, "..", 2))
127 strncpy(ptr, entry->d_name, space);
128 if (lstat(path, &st) < 0)
131 if (S_ISDIR(st.st_mode)) {
132 result = __lookup_dev(path, dev, dir_level + 1, max_level);
135 } else if (S_ISBLK(st.st_mode)) {
136 /* workaround: ignore dm-X devices, these are internal kernel names */
137 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
139 if (st.st_rdev == dev) {
140 result = strdup(path);
150 static char *lookup_dev(const char *dev_id)
152 uint32_t major, minor;
154 char *result, buf[PATH_MAX + 1];
156 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
159 dev = makedev(major, minor);
160 strncpy(buf, DEVICE_DIR, PATH_MAX);
161 buf[PATH_MAX] = '\0';
163 /* First try low level device */
164 if ((result = __lookup_dev(buf, dev, 0, 0)))
167 /* If it is dm, try DM dir */
168 if (dm_is_dm_major(major)) {
169 strncpy(buf, dm_dir(), PATH_MAX);
170 if ((result = __lookup_dev(buf, dev, 0, 0)))
174 strncpy(buf, DEVICE_DIR, PATH_MAX);
175 result = __lookup_dev(buf, dev, 0, 4);
177 /* If not found, return major:minor */
178 return result ?: strdup(dev_id);
181 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
184 long read_ahead_long;
186 if ((fd = open(dev, O_RDONLY)) < 0)
189 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
193 *read_ahead = (uint32_t) read_ahead_long;
198 static void hex_key(char *hexkey, size_t key_size, const char *key)
202 for(i = 0; i < key_size; i++)
203 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
206 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
207 const char *cipher, size_t key_size, const char *key)
212 hexkey = safe_alloc(key_size * 2 + 1);
216 hex_key(hexkey, key_size, key);
218 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
222 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
223 cipher, hexkey, skip, device, offset);
231 static int _dm_simple(int task, const char *name, int udev_wait)
240 if (!(dmt = dm_task_create(task)))
243 if (name && !dm_task_set_name(dmt, name))
246 if (udev_wait && !dm_task_set_cookie(dmt, &cookie, 0))
249 r = dm_task_run(dmt);
252 (void)dm_udev_wait(cookie);
255 dm_task_destroy(dmt);
259 static int _error_device(const char *name, size_t size)
264 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
267 if (!dm_task_set_name(dmt, name))
270 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
273 if (!dm_task_set_ro(dmt))
276 if (!dm_task_no_open_count(dmt))
279 if (!dm_task_run(dmt))
282 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
283 _dm_simple(DM_DEVICE_CLEAR, name, 0);
290 dm_task_destroy(dmt);
294 int dm_remove_device(const char *name, int force, uint64_t size)
297 int retries = force ? RETRY_COUNT : 1;
298 int error_target = 0;
300 if (!name || (force && !size))
304 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
305 if (--retries && r) {
306 log_dbg("WARNING: other process locked internal device %s, %s.",
307 name, retries ? "retrying remove" : "giving up");
308 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
309 debug_processes_using_device(name);
311 if (force && !error_target) {
312 /* If force flag is set, replace device with error, read-only target.
313 * it should stop processes from reading it and also removed underlying
314 * device from mapping, so it is usable again.
315 * Force flag should be used only for temporary devices, which are
316 * intended to work inside cryptsetup only!
317 * Anyway, if some process try to read temporary cryptsetup device,
318 * it is bug - no other process should try touch it (e.g. udev).
320 _error_device(name, size);
324 } while (r == -EINVAL && retries);
326 dm_task_update_nodes();
331 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
333 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
335 * CRYPT-LUKS1-00000000000000000000000000000000-name
338 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
340 char *ptr, uuid2[UUID_LEN] = {0};
344 /* Remove '-' chars */
345 if (uuid && !uuid_parse(uuid, uu)) {
346 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
347 if (uuid[i] != '-') {
353 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
354 type ?: "", type ? "-" : "",
355 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
358 log_dbg("DM-UUID is %s", buf);
360 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
363 int dm_create_device(const char *name,
376 struct dm_task *dmt = NULL;
380 char dev_uuid[DM_UUID_LEN] = {0};
382 uint32_t read_ahead = 0;
384 uint16_t udev_flags = 0;
386 params = get_params(device, skip, offset, cipher, key_size, key);
390 if (type && !strncmp(type, "TEMP", 4))
391 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
393 /* All devices must have DM_UUID, only resize on old device is exception */
395 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
398 if (!dm_task_set_name(dmt, name))
401 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
403 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
406 if (!dm_task_set_name(dmt, name))
409 if (!dm_task_set_uuid(dmt, dev_uuid))
412 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
416 if (read_only && !dm_task_set_ro(dmt))
418 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
421 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
422 if (_dev_read_ahead(device, &read_ahead) &&
423 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
427 if (!dm_task_run(dmt))
431 dm_task_destroy(dmt);
432 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
434 if (!dm_task_set_name(dmt, name))
436 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
438 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
440 if (!dm_task_run(dmt))
444 if (!dm_task_get_info(dmt, &dmi))
449 if (_dm_use_udev()) {
450 (void)dm_udev_wait(cookie);
454 if (r < 0 && !reload) {
456 error = strdup(get_error());
458 dm_remove_device(name, 0, 0);
467 if (cookie && _dm_use_udev())
468 (void)dm_udev_wait(cookie);
473 dm_task_destroy(dmt);
475 dm_task_update_nodes();
479 int dm_status_device(const char *name)
483 uint64_t start, length;
484 char *target_type, *params;
488 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
491 if (!dm_task_set_name(dmt, name)) {
496 if (!dm_task_run(dmt)) {
501 if (!dm_task_get_info(dmt, &dmi)) {
511 next = dm_get_next_target(dmt, next, &start, &length,
512 &target_type, ¶ms);
513 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
517 r = (dmi.open_count > 0);
520 dm_task_destroy(dmt);
525 int dm_query_device(const char *name,
539 uint64_t start, length, val64;
540 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
544 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
546 if (!dm_task_set_name(dmt, name))
549 if (!dm_task_run(dmt))
553 if (!dm_task_get_info(dmt, &dmi))
561 next = dm_get_next_target(dmt, next, &start, &length,
562 &target_type, ¶ms);
563 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
570 rcipher = strsep(¶ms, " ");
573 *cipher = strdup(rcipher);
576 key_ = strsep(¶ms, " ");
579 val64 = strtoull(params, ¶ms, 10);
587 rdevice = strsep(¶ms, " ");
589 *device = lookup_dev(rdevice);
594 val64 = strtoull(params, ¶ms, 10);
602 *key_size = strlen(key_) / 2;
605 if (key_size && key) {
606 *key = safe_alloc(*key_size);
613 for(i = 0; i < *key_size; i++) {
614 memcpy(buffer, &key_[i * 2], 2);
615 (*key)[i] = strtoul(buffer, &endp, 16);
616 if (endp != &buffer[2]) {
623 memset(key_, 0, strlen(key_));
626 *read_only = dmi.read_only;
629 *suspended = dmi.suspended;
631 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
632 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
633 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
635 r = (dmi.open_count > 0);
638 dm_task_destroy(dmt);
643 static int _dm_message(const char *name, const char *msg)
648 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
651 if (name && !dm_task_set_name(dmt, name))
654 if (!dm_task_set_sector(dmt, (uint64_t) 0))
657 if (!dm_task_set_message(dmt, msg))
660 r = dm_task_run(dmt);
663 dm_task_destroy(dmt);
667 int dm_suspend_and_wipe_key(const char *name)
669 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
672 if (!_dm_message(name, "key wipe")) {
673 _dm_simple(DM_DEVICE_RESUME, name, 1);
680 int dm_resume_and_reinstate_key(const char *name,
684 int msg_size = key_size * 2 + 10; // key set <key>
688 msg = safe_alloc(msg_size);
692 memset(msg, 0, msg_size);
693 strcpy(msg, "key set ");
694 hex_key(&msg[8], key_size, key);
696 if (!_dm_message(name, msg) ||
697 !_dm_simple(DM_DEVICE_RESUME, name, 1))
704 const char *dm_get_dir(void)