1 #include <machine/inttypes.h>
17 #define DEVICE_DIR "/dev"
18 #define DM_UUID_LEN 129
19 #define DM_UUID_PREFIX "CRYPT-"
20 #define DM_UUID_PREFIX_LEN 6
21 #define DM_CRYPT_TARGET "crypt"
24 /* Set if dm-crypt version was probed */
25 static int _dm_crypt_checked = 0;
26 static int _dm_crypt_wipe_key_supported = 0;
28 static int _dm_use_count = 0;
29 static struct crypt_device *_context = NULL;
31 /* Compatibility for old device-mapper without udev support */
32 #ifndef HAVE_DM_TASK_SET_COOKIE
33 #define CRYPT_TEMP_UDEV_FLAGS 0
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
40 static int _dm_use_udev()
42 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
43 return dm_udev_get_sync_support();
49 static void set_dm_error(int level, const char *file, int line,
56 if (vasprintf(&msg, f, va) > 0) {
58 log_err(_context, msg);
59 log_err(_context, "\n");
67 static int _dm_simple(int task, const char *name, int udev_wait);
69 static void _dm_set_crypt_compat(int maj, int min, int patch)
71 log_dbg("Detected dm-crypt target of version %i.%i.%i.", maj, min, patch);
73 if (maj >= 1 && min >=2)
74 _dm_crypt_wipe_key_supported = 1;
76 log_dbg("Suspend and resume disabled, no wipe key support.");
78 _dm_crypt_checked = 1;
81 static int _dm_check_versions(void)
84 struct dm_versions *target, *last_target;
86 if (_dm_crypt_checked)
89 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
92 if (!dm_task_run(dmt)) {
97 target = dm_task_get_versions(dmt);
100 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
101 _dm_set_crypt_compat((int)target->version[0],
102 (int)target->version[1],
103 (int)target->version[2]);
105 target = (void *) target + target->next;
106 } while (last_target != target);
108 dm_task_destroy(dmt);
112 int dm_init(struct crypt_device *context, int check_kernel)
114 if (!_dm_use_count++) {
115 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
116 check_kernel ? "" : " (NO kernel check requested)",
117 _dm_use_udev() ? "en" : "dis");
118 if (check_kernel && !_dm_check_versions()) {
119 log_err(context, _("Cannot initialize device-mapper. Is dm kernel module loaded?\n"));
122 if (getuid() || geteuid())
123 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
124 dm_log_init(set_dm_error);
125 dm_log_init_verbose(10);
128 // FIXME: global context is not safe
132 return 1; /* unsafe memory */
137 if (_dm_use_count && (!--_dm_use_count)) {
138 log_dbg("Releasing device-mapper backend.");
139 dm_log_init_verbose(0);
146 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
148 struct dirent *entry;
155 /* Ignore strange nested directories */
156 if (dir_level > max_level)
159 path[PATH_MAX - 1] = '\0';
160 ptr = path + strlen(path);
163 space = PATH_MAX - (ptr - path);
169 while((entry = readdir(dir))) {
170 if (entry->d_name[0] == '.' ||
171 !strncmp(entry->d_name, "..", 2))
174 strncpy(ptr, entry->d_name, space);
175 if (stat(path, &st) < 0)
178 if (S_ISDIR(st.st_mode)) {
179 result = __lookup_dev(path, dev, dir_level + 1, max_level);
182 } else if (S_ISBLK(st.st_mode)) {
183 /* workaround: ignore dm-X devices, these are internal kernel names */
184 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
186 if (st.st_rdev == dev) {
187 result = strdup(path);
197 static char *lookup_dev(const char *dev_id)
199 uint32_t major, minor;
201 char *result = NULL, buf[PATH_MAX + 1];
203 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
206 dev = makedev(major, minor);
207 strncpy(buf, DEVICE_DIR, PATH_MAX);
208 buf[PATH_MAX] = '\0';
210 /* First try low level device */
211 if ((result = __lookup_dev(buf, dev, 0, 0)))
214 /* If it is dm, try DM dir */
215 if (dm_is_dm_major(major)) {
216 strncpy(buf, dm_dir(), PATH_MAX);
217 if ((result = __lookup_dev(buf, dev, 0, 0)))
221 strncpy(buf, DEVICE_DIR, PATH_MAX);
222 result = __lookup_dev(buf, dev, 0, 4);
224 /* If not found, return NULL */
228 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
231 long read_ahead_long;
233 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};
393 /* Remove '-' chars */
395 uuid_from_string(uuid, &uu, &ret);
396 if (ret != uuid_s_ok) {
397 printf("error in uuid_from_string(%s), err = %d\n", uuid, ret);
398 for (ptr = uuid2, i = 0; i < UUID_LEN; i++) {
399 if (uuid[i] != '-') {
407 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
408 type ?: "", type ? "-" : "",
409 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
412 log_dbg("DM-UUID is %s", buf);
414 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
417 int dm_create_device(const char *name,
430 struct dm_task *dmt = NULL;
434 char dev_uuid[DM_UUID_LEN] = {0};
436 uint32_t read_ahead = 0;
438 uint16_t udev_flags = 0;
440 params = get_params(device, skip, offset, cipher, key_size, key);
444 if (type && !strncmp(type, "TEMP", 4))
445 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
447 /* All devices must have DM_UUID, only resize on old device is exception */
449 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
452 if (!dm_task_set_name(dmt, name))
455 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
457 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
460 if (!dm_task_set_name(dmt, name))
463 if (!dm_task_set_uuid(dmt, dev_uuid))
466 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
470 if (read_only && !dm_task_set_ro(dmt))
472 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
475 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
476 if (_dev_read_ahead(device, &read_ahead) &&
477 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
481 if (!dm_task_run(dmt))
485 dm_task_destroy(dmt);
486 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
488 if (!dm_task_set_name(dmt, name))
490 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
492 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
494 if (!dm_task_run(dmt))
498 if (!dm_task_get_info(dmt, &dmi))
503 if (_dm_use_udev()) {
504 (void)dm_udev_wait(cookie);
508 if (r < 0 && !reload) {
510 error = strdup(get_error());
512 dm_remove_device(name, 0, 0);
521 if (cookie && _dm_use_udev())
522 (void)dm_udev_wait(cookie);
527 dm_task_destroy(dmt);
529 dm_task_update_nodes();
533 int dm_status_device(const char *name)
537 uint64_t start, length;
538 char *target_type, *params;
542 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
545 if (!dm_task_set_name(dmt, name)) {
550 if (!dm_task_run(dmt)) {
555 if (!dm_task_get_info(dmt, &dmi)) {
565 next = dm_get_next_target(dmt, next, &start, &length,
566 &target_type, ¶ms);
567 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
571 r = (dmi.open_count > 0);
574 dm_task_destroy(dmt);
579 int dm_query_device(const char *name,
593 uint64_t start, length, val64;
594 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
598 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
600 if (!dm_task_set_name(dmt, name))
603 if (!dm_task_run(dmt))
607 if (!dm_task_get_info(dmt, &dmi))
615 next = dm_get_next_target(dmt, next, &start, &length,
616 &target_type, ¶ms);
617 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
624 rcipher = strsep(¶ms, " ");
627 *cipher = strdup(rcipher);
630 key_ = strsep(¶ms, " ");
633 val64 = strtoull(params, ¶ms, 10);
641 rdevice = strsep(¶ms, " ");
643 *device = lookup_dev(rdevice);
648 val64 = strtoull(params, ¶ms, 10);
656 *key_size = strlen(key_) / 2;
659 if (key_size && key) {
660 *key = safe_alloc(*key_size);
667 for(i = 0; i < *key_size; i++) {
668 memcpy(buffer, &key_[i * 2], 2);
669 (*key)[i] = strtoul(buffer, &endp, 16);
670 if (endp != &buffer[2]) {
677 memset(key_, 0, strlen(key_));
680 *read_only = dmi.read_only;
683 *suspended = dmi.suspended;
685 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
686 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
687 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
689 r = (dmi.open_count > 0);
692 dm_task_destroy(dmt);
697 static int _dm_message(const char *name, const char *msg)
702 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
705 if (name && !dm_task_set_name(dmt, name))
708 if (!dm_task_set_sector(dmt, (uint64_t) 0))
711 if (!dm_task_set_message(dmt, msg))
714 r = dm_task_run(dmt);
717 dm_task_destroy(dmt);
721 int dm_suspend_and_wipe_key(const char *name)
723 if (!_dm_check_versions())
726 if (!_dm_crypt_wipe_key_supported)
729 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
732 if (!_dm_message(name, "key wipe")) {
733 _dm_simple(DM_DEVICE_RESUME, name, 1);
740 int dm_resume_and_reinstate_key(const char *name,
744 int msg_size = key_size * 2 + 10; // key set <key>
748 if (!_dm_check_versions())
751 if (!_dm_crypt_wipe_key_supported)
754 msg = safe_alloc(msg_size);
758 memset(msg, 0, msg_size);
759 strcpy(msg, "key set ");
760 hex_key(&msg[8], key_size, key);
762 if (!_dm_message(name, msg) ||
763 !_dm_simple(DM_DEVICE_RESUME, name, 1))
770 const char *dm_get_dir(void)