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 static int _dm_crypt_wipe_key_supported = 0;
23 static int _dm_use_count = 0;
24 static struct crypt_device *_context = NULL;
26 /* Compatibility for old device-mapper without udev support */
27 #ifndef HAVE_DM_TASK_SET_COOKIE
28 #define CRYPT_TEMP_UDEV_FLAGS 0
29 static int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
30 static int dm_udev_wait(uint32_t cookie) { return 0; };
32 #define CRYPT_TEMP_UDEV_FLAGS DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
33 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
34 DM_UDEV_DISABLE_OTHER_RULES_FLAG
37 static int _dm_use_udev()
39 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
40 return dm_udev_get_sync_support();
46 static void set_dm_error(int level, const char *file, int line,
53 if (vasprintf(&msg, f, va) > 0) {
55 log_err(_context, msg);
56 log_err(_context, "\n");
64 static int _dm_simple(int task, const char *name, int udev_wait);
66 static void _dm_set_crypt_compat(struct crypt_device *context,
67 int maj, int min, int patch)
69 log_dbg("Detected dm-crypt target of version %i.%i.%i.", maj, min, patch);
71 if (maj >= 1 && min >=2)
72 _dm_crypt_wipe_key_supported = 1;
74 log_dbg("Suspend and resume disabled, no wipe key support.");
77 static int _dm_check_versions(struct crypt_device *context)
81 struct dm_versions *target, *last_target;
83 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
86 if (!dm_task_run(dmt)) {
91 target = dm_task_get_versions(dmt);
94 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
96 _dm_set_crypt_compat(context,
97 (int)target->version[0],
98 (int)target->version[1],
99 (int)target->version[2]);
101 target = (void *) target + target->next;
102 } while (last_target != target);
105 log_err(context, _("Cannot find compatible device-mapper kernel modules.\n"));
107 dm_task_destroy(dmt);
111 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
115 int dm_init(struct crypt_device *context, int check_kernel)
117 if (!_dm_use_count++) {
118 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
119 check_kernel ? "" : " (NO kernel check requested)",
120 _dm_use_udev() ? "en" : "dis");
121 if (check_kernel && !_dm_check_versions(context))
123 if (getuid() || geteuid())
124 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
125 dm_log_init(set_dm_error);
126 dm_log_init_verbose(10);
129 // FIXME: global context is not safe
133 return 1; /* unsafe memory */
138 if (_dm_use_count && (!--_dm_use_count)) {
139 log_dbg("Releasing device-mapper backend.");
140 dm_log_init_verbose(0);
147 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
149 struct dirent *entry;
156 /* Ignore strange nested directories */
157 if (dir_level > max_level)
160 path[PATH_MAX - 1] = '\0';
161 ptr = path + strlen(path);
164 space = PATH_MAX - (ptr - path);
170 while((entry = readdir(dir))) {
171 if (entry->d_name[0] == '.' ||
172 !strncmp(entry->d_name, "..", 2))
175 strncpy(ptr, entry->d_name, space);
176 if (lstat(path, &st) < 0)
179 if (S_ISDIR(st.st_mode)) {
180 result = __lookup_dev(path, dev, dir_level + 1, max_level);
183 } else if (S_ISBLK(st.st_mode)) {
184 /* workaround: ignore dm-X devices, these are internal kernel names */
185 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
187 if (st.st_rdev == dev) {
188 result = strdup(path);
198 static char *lookup_dev(const char *dev_id)
200 uint32_t major, minor;
202 char *result, buf[PATH_MAX + 1];
204 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
207 dev = makedev(major, minor);
208 strncpy(buf, DEVICE_DIR, PATH_MAX);
209 buf[PATH_MAX] = '\0';
211 /* First try low level device */
212 if ((result = __lookup_dev(buf, dev, 0, 0)))
215 /* If it is dm, try DM dir */
216 if (dm_is_dm_major(major)) {
217 strncpy(buf, dm_dir(), PATH_MAX);
218 if ((result = __lookup_dev(buf, dev, 0, 0)))
222 strncpy(buf, DEVICE_DIR, PATH_MAX);
223 result = __lookup_dev(buf, dev, 0, 4);
225 /* If not found, return major:minor */
226 return result ?: strdup(dev_id);
229 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
232 long read_ahead_long;
234 if ((fd = open(dev, O_RDONLY)) < 0)
237 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
241 *read_ahead = (uint32_t) read_ahead_long;
246 static void hex_key(char *hexkey, size_t key_size, const char *key)
250 for(i = 0; i < key_size; i++)
251 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
254 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
255 const char *cipher, size_t key_size, const char *key)
260 hexkey = safe_alloc(key_size * 2 + 1);
264 hex_key(hexkey, key_size, key);
266 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
270 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
271 cipher, hexkey, skip, device, offset);
279 static int _dm_simple(int task, const char *name, int udev_wait)
288 if (!(dmt = dm_task_create(task)))
291 if (name && !dm_task_set_name(dmt, name))
294 if (udev_wait && !dm_task_set_cookie(dmt, &cookie, 0))
297 r = dm_task_run(dmt);
300 (void)dm_udev_wait(cookie);
303 dm_task_destroy(dmt);
307 static int _error_device(const char *name, size_t size)
312 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
315 if (!dm_task_set_name(dmt, name))
318 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
321 if (!dm_task_set_ro(dmt))
324 if (!dm_task_no_open_count(dmt))
327 if (!dm_task_run(dmt))
330 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
331 _dm_simple(DM_DEVICE_CLEAR, name, 0);
338 dm_task_destroy(dmt);
342 int dm_remove_device(const char *name, int force, uint64_t size)
345 int retries = force ? RETRY_COUNT : 1;
346 int error_target = 0;
348 if (!name || (force && !size))
352 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
353 if (--retries && r) {
354 log_dbg("WARNING: other process locked internal device %s, %s.",
355 name, retries ? "retrying remove" : "giving up");
356 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
357 debug_processes_using_device(name);
359 if (force && !error_target) {
360 /* If force flag is set, replace device with error, read-only target.
361 * it should stop processes from reading it and also removed underlying
362 * device from mapping, so it is usable again.
363 * Force flag should be used only for temporary devices, which are
364 * intended to work inside cryptsetup only!
365 * Anyway, if some process try to read temporary cryptsetup device,
366 * it is bug - no other process should try touch it (e.g. udev).
368 _error_device(name, size);
372 } while (r == -EINVAL && retries);
374 dm_task_update_nodes();
379 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
381 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
383 * CRYPT-LUKS1-00000000000000000000000000000000-name
386 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
388 char *ptr, uuid2[UUID_LEN] = {0};
392 /* Remove '-' chars */
393 if (uuid && !uuid_parse(uuid, uu)) {
394 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
395 if (uuid[i] != '-') {
401 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
402 type ?: "", type ? "-" : "",
403 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
406 log_dbg("DM-UUID is %s", buf);
408 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
411 int dm_create_device(const char *name,
424 struct dm_task *dmt = NULL;
428 char dev_uuid[DM_UUID_LEN] = {0};
430 uint32_t read_ahead = 0;
432 uint16_t udev_flags = 0;
434 params = get_params(device, skip, offset, cipher, key_size, key);
438 if (type && !strncmp(type, "TEMP", 4))
439 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
441 /* All devices must have DM_UUID, only resize on old device is exception */
443 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
446 if (!dm_task_set_name(dmt, name))
449 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
451 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
454 if (!dm_task_set_name(dmt, name))
457 if (!dm_task_set_uuid(dmt, dev_uuid))
460 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
464 if (read_only && !dm_task_set_ro(dmt))
466 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
469 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
470 if (_dev_read_ahead(device, &read_ahead) &&
471 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
475 if (!dm_task_run(dmt))
479 dm_task_destroy(dmt);
480 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
482 if (!dm_task_set_name(dmt, name))
484 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
486 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
488 if (!dm_task_run(dmt))
492 if (!dm_task_get_info(dmt, &dmi))
497 if (_dm_use_udev()) {
498 (void)dm_udev_wait(cookie);
502 if (r < 0 && !reload) {
504 error = strdup(get_error());
506 dm_remove_device(name, 0, 0);
515 if (cookie && _dm_use_udev())
516 (void)dm_udev_wait(cookie);
521 dm_task_destroy(dmt);
523 dm_task_update_nodes();
527 int dm_status_device(const char *name)
531 uint64_t start, length;
532 char *target_type, *params;
536 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
539 if (!dm_task_set_name(dmt, name)) {
544 if (!dm_task_run(dmt)) {
549 if (!dm_task_get_info(dmt, &dmi)) {
559 next = dm_get_next_target(dmt, next, &start, &length,
560 &target_type, ¶ms);
561 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
565 r = (dmi.open_count > 0);
568 dm_task_destroy(dmt);
573 int dm_query_device(const char *name,
587 uint64_t start, length, val64;
588 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
592 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
594 if (!dm_task_set_name(dmt, name))
597 if (!dm_task_run(dmt))
601 if (!dm_task_get_info(dmt, &dmi))
609 next = dm_get_next_target(dmt, next, &start, &length,
610 &target_type, ¶ms);
611 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
618 rcipher = strsep(¶ms, " ");
621 *cipher = strdup(rcipher);
624 key_ = strsep(¶ms, " ");
627 val64 = strtoull(params, ¶ms, 10);
635 rdevice = strsep(¶ms, " ");
637 *device = lookup_dev(rdevice);
642 val64 = strtoull(params, ¶ms, 10);
650 *key_size = strlen(key_) / 2;
653 if (key_size && key) {
654 *key = safe_alloc(*key_size);
661 for(i = 0; i < *key_size; i++) {
662 memcpy(buffer, &key_[i * 2], 2);
663 (*key)[i] = strtoul(buffer, &endp, 16);
664 if (endp != &buffer[2]) {
671 memset(key_, 0, strlen(key_));
674 *read_only = dmi.read_only;
677 *suspended = dmi.suspended;
679 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
680 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
681 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
683 r = (dmi.open_count > 0);
686 dm_task_destroy(dmt);
691 static int _dm_message(const char *name, const char *msg)
696 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
699 if (name && !dm_task_set_name(dmt, name))
702 if (!dm_task_set_sector(dmt, (uint64_t) 0))
705 if (!dm_task_set_message(dmt, msg))
708 r = dm_task_run(dmt);
711 dm_task_destroy(dmt);
715 int dm_suspend_and_wipe_key(const char *name)
717 if (!_dm_crypt_wipe_key_supported)
720 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
723 if (!_dm_message(name, "key wipe")) {
724 _dm_simple(DM_DEVICE_RESUME, name, 1);
731 int dm_resume_and_reinstate_key(const char *name,
735 int msg_size = key_size * 2 + 10; // key set <key>
739 if (!_dm_crypt_wipe_key_supported)
742 msg = safe_alloc(msg_size);
746 memset(msg, 0, msg_size);
747 strcpy(msg, "key set ");
748 hex_key(&msg[8], key_size, key);
750 if (!_dm_message(name, msg) ||
751 !_dm_simple(DM_DEVICE_RESUME, name, 1))
758 const char *dm_get_dir(void)