7 #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
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(int maj, int min, int patch)
68 log_dbg("Detected dm-crypt target of version %i.%i.%i.", maj, min, patch);
70 if (maj >= 1 && min >=2)
71 _dm_crypt_wipe_key_supported = 1;
73 log_dbg("Suspend and resume disabled, no wipe key support.");
75 _dm_crypt_checked = 1;
78 static int _dm_check_versions(void)
81 struct dm_versions *target, *last_target;
83 if (_dm_crypt_checked)
86 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
89 if (!dm_task_run(dmt)) {
94 target = dm_task_get_versions(dmt);
97 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
98 _dm_set_crypt_compat((int)target->version[0],
99 (int)target->version[1],
100 (int)target->version[2]);
102 target = (void *) target + target->next;
103 } while (last_target != target);
105 dm_task_destroy(dmt);
109 int dm_init(struct crypt_device *context, int check_kernel)
111 if (!_dm_use_count++) {
112 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
113 check_kernel ? "" : " (NO kernel check requested)",
114 _dm_use_udev() ? "en" : "dis");
115 if (check_kernel && !_dm_check_versions()) {
116 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
119 if (getuid() || geteuid())
120 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
121 dm_log_init(set_dm_error);
122 dm_log_init_verbose(10);
125 // FIXME: global context is not safe
129 return 1; /* unsafe memory */
134 if (_dm_use_count && (!--_dm_use_count)) {
135 log_dbg("Releasing device-mapper backend.");
136 dm_log_init_verbose(0);
143 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
145 struct dirent *entry;
152 /* Ignore strange nested directories */
153 if (dir_level > max_level)
156 path[PATH_MAX - 1] = '\0';
157 ptr = path + strlen(path);
160 space = PATH_MAX - (ptr - path);
166 while((entry = readdir(dir))) {
167 if (entry->d_name[0] == '.' ||
168 !strncmp(entry->d_name, "..", 2))
171 strncpy(ptr, entry->d_name, space);
172 if (stat(path, &st) < 0)
175 if (S_ISDIR(st.st_mode)) {
176 result = __lookup_dev(path, dev, dir_level + 1, max_level);
179 } else if (S_ISBLK(st.st_mode)) {
180 /* workaround: ignore dm-X devices, these are internal kernel names */
181 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
183 if (st.st_rdev == dev) {
184 result = strdup(path);
194 static char *lookup_dev(const char *dev_id)
196 uint32_t major, minor;
198 char *result = NULL, buf[PATH_MAX + 1];
200 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
203 dev = makedev(major, minor);
204 strncpy(buf, DEVICE_DIR, PATH_MAX);
205 buf[PATH_MAX] = '\0';
207 /* First try low level device */
208 if ((result = __lookup_dev(buf, dev, 0, 0)))
211 /* If it is dm, try DM dir */
212 if (dm_is_dm_major(major)) {
213 strncpy(buf, dm_dir(), PATH_MAX);
214 if ((result = __lookup_dev(buf, dev, 0, 0)))
218 strncpy(buf, DEVICE_DIR, PATH_MAX);
219 result = __lookup_dev(buf, dev, 0, 4);
221 /* If not found, return NULL */
225 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
228 long read_ahead_long;
230 if ((fd = open(dev, O_RDONLY)) < 0)
234 //r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
238 *read_ahead = (uint32_t) read_ahead_long;
243 static void hex_key(char *hexkey, size_t key_size, const char *key)
247 for(i = 0; i < key_size; i++)
248 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
251 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
252 const char *cipher, size_t key_size, const char *key)
257 hexkey = safe_alloc(key_size * 2 + 1);
261 hex_key(hexkey, key_size, key);
263 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
267 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
268 cipher, hexkey, skip, device, offset);
276 static int _dm_simple(int task, const char *name, int udev_wait)
285 if (!(dmt = dm_task_create(task)))
288 if (name && !dm_task_set_name(dmt, name))
291 if (udev_wait && !dm_task_set_cookie(dmt, &cookie, 0))
294 r = dm_task_run(dmt);
297 (void)dm_udev_wait(cookie);
300 dm_task_destroy(dmt);
304 static int _error_device(const char *name, size_t size)
309 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
312 if (!dm_task_set_name(dmt, name))
315 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
318 if (!dm_task_set_ro(dmt))
321 if (!dm_task_no_open_count(dmt))
324 if (!dm_task_run(dmt))
327 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
328 _dm_simple(DM_DEVICE_CLEAR, name, 0);
335 dm_task_destroy(dmt);
339 int dm_remove_device(const char *name, int force, uint64_t size)
342 int retries = force ? RETRY_COUNT : 1;
343 int error_target = 0;
345 if (!name || (force && !size))
349 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
350 if (--retries && r) {
351 log_dbg("WARNING: other process locked internal device %s, %s.",
352 name, retries ? "retrying remove" : "giving up");
353 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
354 debug_processes_using_device(name);
356 if (force && !error_target) {
357 /* If force flag is set, replace device with error, read-only target.
358 * it should stop processes from reading it and also removed underlying
359 * device from mapping, so it is usable again.
360 * Force flag should be used only for temporary devices, which are
361 * intended to work inside cryptsetup only!
362 * Anyway, if some process try to read temporary cryptsetup device,
363 * it is bug - no other process should try touch it (e.g. udev).
365 _error_device(name, size);
369 } while (r == -EINVAL && retries);
371 dm_task_update_nodes();
376 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
378 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
380 * CRYPT-LUKS1-00000000000000000000000000000000-name
383 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
385 char *ptr, uuid2[UUID_LEN] = {0};
390 /* Remove '-' chars */
392 uuid_from_string(uuid, &uu, &ret);
393 if (ret != uuid_s_ok) {
394 printf("crap happened in uuid_from_string(%s), err = %d\n", uuid, ret);
395 for (ptr = uuid2, i = 0; i < UUID_LEN; i++) {
396 if (uuid[i] != '-') {
402 printf("went well in uuid_from_string(%s), err = %d\n", uuid, ret);
406 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
407 type ?: "", type ? "-" : "",
408 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
411 log_dbg("DM-UUID is %s", buf);
413 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
416 int dm_create_device(const char *name,
429 struct dm_task *dmt = NULL;
433 char dev_uuid[DM_UUID_LEN] = {0};
435 uint32_t read_ahead = 0;
437 uint16_t udev_flags = 0;
439 params = get_params(device, skip, offset, cipher, key_size, key);
443 if (type && !strncmp(type, "TEMP", 4))
444 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
446 /* All devices must have DM_UUID, only resize on old device is exception */
448 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
451 if (!dm_task_set_name(dmt, name))
454 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
456 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
459 if (!dm_task_set_name(dmt, name))
462 if (!dm_task_set_uuid(dmt, dev_uuid))
465 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
469 if (read_only && !dm_task_set_ro(dmt))
471 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
474 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
475 if (_dev_read_ahead(device, &read_ahead) &&
476 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
480 if (!dm_task_run(dmt))
484 dm_task_destroy(dmt);
485 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
487 if (!dm_task_set_name(dmt, name))
489 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
491 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
493 if (!dm_task_run(dmt))
497 if (!dm_task_get_info(dmt, &dmi))
502 if (_dm_use_udev()) {
503 (void)dm_udev_wait(cookie);
507 if (r < 0 && !reload) {
509 error = strdup(get_error());
511 dm_remove_device(name, 0, 0);
520 if (cookie && _dm_use_udev())
521 (void)dm_udev_wait(cookie);
526 dm_task_destroy(dmt);
528 dm_task_update_nodes();
532 int dm_status_device(const char *name)
536 uint64_t start, length;
537 char *target_type, *params;
541 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
544 if (!dm_task_set_name(dmt, name)) {
549 if (!dm_task_run(dmt)) {
554 if (!dm_task_get_info(dmt, &dmi)) {
564 next = dm_get_next_target(dmt, next, &start, &length,
565 &target_type, ¶ms);
566 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
570 r = (dmi.open_count > 0);
573 dm_task_destroy(dmt);
578 int dm_query_device(const char *name,
592 uint64_t start, length, val64;
593 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
597 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
599 if (!dm_task_set_name(dmt, name))
602 if (!dm_task_run(dmt))
606 if (!dm_task_get_info(dmt, &dmi))
614 next = dm_get_next_target(dmt, next, &start, &length,
615 &target_type, ¶ms);
616 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
623 rcipher = strsep(¶ms, " ");
626 *cipher = strdup(rcipher);
629 key_ = strsep(¶ms, " ");
632 val64 = strtoull(params, ¶ms, 10);
640 rdevice = strsep(¶ms, " ");
642 *device = lookup_dev(rdevice);
647 val64 = strtoull(params, ¶ms, 10);
655 *key_size = strlen(key_) / 2;
658 if (key_size && key) {
659 *key = safe_alloc(*key_size);
666 for(i = 0; i < *key_size; i++) {
667 memcpy(buffer, &key_[i * 2], 2);
668 (*key)[i] = strtoul(buffer, &endp, 16);
669 if (endp != &buffer[2]) {
676 memset(key_, 0, strlen(key_));
679 *read_only = dmi.read_only;
682 *suspended = dmi.suspended;
684 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
685 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
686 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
688 r = (dmi.open_count > 0);
691 dm_task_destroy(dmt);
696 static int _dm_message(const char *name, const char *msg)
701 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
704 if (name && !dm_task_set_name(dmt, name))
707 if (!dm_task_set_sector(dmt, (uint64_t) 0))
710 if (!dm_task_set_message(dmt, msg))
713 r = dm_task_run(dmt);
716 dm_task_destroy(dmt);
720 int dm_suspend_and_wipe_key(const char *name)
722 if (!_dm_check_versions())
725 if (!_dm_crypt_wipe_key_supported)
728 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
731 if (!_dm_message(name, "key wipe")) {
732 _dm_simple(DM_DEVICE_RESUME, name, 1);
739 int dm_resume_and_reinstate_key(const char *name,
743 int msg_size = key_size * 2 + 10; // key set <key>
747 if (!_dm_check_versions())
750 if (!_dm_crypt_wipe_key_supported)
753 msg = safe_alloc(msg_size);
757 memset(msg, 0, msg_size);
758 strcpy(msg, "key set ");
759 hex_key(&msg[8], key_size, key);
761 if (!_dm_message(name, msg) ||
762 !_dm_simple(DM_DEVICE_RESUME, name, 1))
769 const char *dm_get_dir(void)