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_use_count = 0;
22 static struct crypt_device *_context = NULL;
24 /* Compatibility for old device-mapper without udev support */
25 #ifndef HAVE_DM_TASK_SET_COOKIE
26 #define CRYPT_TEMP_UDEV_FLAGS 0
27 static int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
28 static int dm_udev_wait(uint32_t cookie) { return 0; };
30 #define CRYPT_TEMP_UDEV_FLAGS DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
31 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
32 DM_UDEV_DISABLE_OTHER_RULES_FLAG
35 static int _dm_use_udev()
37 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
38 return dm_udev_get_sync_support();
44 static void set_dm_error(int level, const char *file, int line,
51 if (vasprintf(&msg, f, va) > 0) {
53 log_err(_context, msg);
54 log_err(_context, "\n");
62 static int _dm_simple(int task, const char *name, int udev_wait);
64 int dm_init(struct crypt_device *context, int check_kernel)
66 if (!_dm_use_count++) {
67 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
68 check_kernel ? "" : " (NO kernel check requested)",
69 _dm_use_udev() ? "en" : "dis");
70 if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL, 0)) {
71 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
74 if (getuid() || geteuid())
75 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
76 dm_log_init(set_dm_error);
77 dm_log_init_verbose(10);
80 // FIXME: global context is not safe
84 return 1; /* unsafe memory */
89 if (_dm_use_count && (!--_dm_use_count)) {
90 log_dbg("Releasing device-mapper backend.");
91 dm_log_init_verbose(0);
98 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
100 struct dirent *entry;
107 /* Ignore strange nested directories */
108 if (dir_level > max_level)
111 path[PATH_MAX - 1] = '\0';
112 ptr = path + strlen(path);
115 space = PATH_MAX - (ptr - path);
121 while((entry = readdir(dir))) {
122 if (entry->d_name[0] == '.' ||
123 !strncmp(entry->d_name, "..", 2))
126 strncpy(ptr, entry->d_name, space);
127 if (lstat(path, &st) < 0)
130 if (S_ISDIR(st.st_mode)) {
131 result = __lookup_dev(path, dev, dir_level + 1, max_level);
134 } else if (S_ISBLK(st.st_mode)) {
135 /* workaround: ignore dm-X devices, these are internal kernel names */
136 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
138 if (st.st_rdev == dev) {
139 result = strdup(path);
149 static char *lookup_dev(const char *dev_id)
151 uint32_t major, minor;
153 char *result, buf[PATH_MAX + 1];
155 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
158 dev = makedev(major, minor);
159 strncpy(buf, DEVICE_DIR, PATH_MAX);
160 buf[PATH_MAX] = '\0';
162 /* First try low level device */
163 if ((result = __lookup_dev(buf, dev, 0, 0)))
166 /* If it is dm, try DM dir */
167 if (dm_is_dm_major(major)) {
168 strncpy(buf, dm_dir(), PATH_MAX);
169 if ((result = __lookup_dev(buf, dev, 0, 0)))
173 strncpy(buf, DEVICE_DIR, PATH_MAX);
174 result = __lookup_dev(buf, dev, 0, 4);
176 /* If not found, return major:minor */
177 return result ?: strdup(dev_id);
180 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
183 long read_ahead_long;
185 if ((fd = open(dev, O_RDONLY)) < 0)
188 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
192 *read_ahead = (uint32_t) read_ahead_long;
197 static void hex_key(char *hexkey, size_t key_size, const char *key)
201 for(i = 0; i < key_size; i++)
202 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
205 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
206 const char *cipher, size_t key_size, const char *key)
211 hexkey = safe_alloc(key_size * 2 + 1);
215 hex_key(hexkey, key_size, key);
217 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
221 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
222 cipher, hexkey, skip, device, offset);
230 static int _dm_simple(int task, const char *name, int udev_wait)
239 if (!(dmt = dm_task_create(task)))
242 if (name && !dm_task_set_name(dmt, name))
245 if (udev_wait && !dm_task_set_cookie(dmt, &cookie, 0))
248 r = dm_task_run(dmt);
251 (void)dm_udev_wait(cookie);
254 dm_task_destroy(dmt);
258 static int _error_device(const char *name, size_t size)
263 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
266 if (!dm_task_set_name(dmt, name))
269 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
272 if (!dm_task_set_ro(dmt))
275 if (!dm_task_no_open_count(dmt))
278 if (!dm_task_run(dmt))
281 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
282 _dm_simple(DM_DEVICE_CLEAR, name, 0);
289 dm_task_destroy(dmt);
293 int dm_remove_device(const char *name, int force, uint64_t size)
296 int retries = force ? RETRY_COUNT : 1;
297 int error_target = 0;
299 if (!name || (force && !size))
303 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
304 if (--retries && r) {
305 log_dbg("WARNING: other process locked internal device %s, %s.",
306 name, retries ? "retrying remove" : "giving up");
307 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
308 debug_processes_using_device(name);
310 if (force && !error_target) {
311 /* If force flag is set, replace device with error, read-only target.
312 * it should stop processes from reading it and also removed underlying
313 * device from mapping, so it is usable again.
314 * Force flag should be used only for temporary devices, which are
315 * intended to work inside cryptsetup only!
316 * Anyway, if some process try to read temporary cryptsetup device,
317 * it is bug - no other process should try touch it (e.g. udev).
319 _error_device(name, size);
323 } while (r == -EINVAL && retries);
325 dm_task_update_nodes();
330 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
332 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
334 * CRYPT-LUKS1-00000000000000000000000000000000-name
337 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
339 char *ptr, uuid2[UUID_LEN] = {0};
343 /* Remove '-' chars */
344 if (uuid && !uuid_parse(uuid, uu)) {
345 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
346 if (uuid[i] != '-') {
352 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
353 type ?: "", type ? "-" : "",
354 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
357 log_dbg("DM-UUID is %s", buf);
359 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
362 int dm_create_device(const char *name,
375 struct dm_task *dmt = NULL;
379 char dev_uuid[DM_UUID_LEN] = {0};
381 uint32_t read_ahead = 0;
383 uint16_t udev_flags = 0;
385 params = get_params(device, skip, offset, cipher, key_size, key);
389 if (type && !strncmp(type, "TEMP", 4))
390 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
392 /* All devices must have DM_UUID, only resize on old device is exception */
394 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
397 if (!dm_task_set_name(dmt, name))
400 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
402 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
405 if (!dm_task_set_name(dmt, name))
408 if (!dm_task_set_uuid(dmt, dev_uuid))
411 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
415 if (read_only && !dm_task_set_ro(dmt))
417 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
420 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
421 if (_dev_read_ahead(device, &read_ahead) &&
422 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
426 if (!dm_task_run(dmt))
430 dm_task_destroy(dmt);
431 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
433 if (!dm_task_set_name(dmt, name))
435 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
437 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, udev_flags))
439 if (!dm_task_run(dmt))
443 if (!dm_task_get_info(dmt, &dmi))
448 if (_dm_use_udev()) {
449 (void)dm_udev_wait(cookie);
453 if (r < 0 && !reload) {
455 error = strdup(get_error());
457 dm_remove_device(name, 0, 0);
466 if (cookie && _dm_use_udev())
467 (void)dm_udev_wait(cookie);
472 dm_task_destroy(dmt);
474 dm_task_update_nodes();
478 int dm_status_device(const char *name)
482 uint64_t start, length;
483 char *target_type, *params;
487 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
490 if (!dm_task_set_name(dmt, name)) {
495 if (!dm_task_run(dmt)) {
500 if (!dm_task_get_info(dmt, &dmi)) {
510 next = dm_get_next_target(dmt, next, &start, &length,
511 &target_type, ¶ms);
512 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
516 r = (dmi.open_count > 0);
519 dm_task_destroy(dmt);
524 int dm_query_device(const char *name,
538 uint64_t start, length, val64;
539 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
543 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
545 if (!dm_task_set_name(dmt, name))
548 if (!dm_task_run(dmt))
552 if (!dm_task_get_info(dmt, &dmi))
560 next = dm_get_next_target(dmt, next, &start, &length,
561 &target_type, ¶ms);
562 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
569 rcipher = strsep(¶ms, " ");
572 *cipher = strdup(rcipher);
575 key_ = strsep(¶ms, " ");
578 val64 = strtoull(params, ¶ms, 10);
586 rdevice = strsep(¶ms, " ");
588 *device = lookup_dev(rdevice);
593 val64 = strtoull(params, ¶ms, 10);
601 *key_size = strlen(key_) / 2;
604 if (key_size && key) {
605 *key = safe_alloc(*key_size);
612 for(i = 0; i < *key_size; i++) {
613 memcpy(buffer, &key_[i * 2], 2);
614 (*key)[i] = strtoul(buffer, &endp, 16);
615 if (endp != &buffer[2]) {
622 memset(key_, 0, strlen(key_));
625 *read_only = dmi.read_only;
628 *suspended = dmi.suspended;
630 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
631 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
632 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
634 r = (dmi.open_count > 0);
637 dm_task_destroy(dmt);
642 static int _dm_message(const char *name, const char *msg)
647 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
650 if (name && !dm_task_set_name(dmt, name))
653 if (!dm_task_set_sector(dmt, (uint64_t) 0))
656 if (!dm_task_set_message(dmt, msg))
659 r = dm_task_run(dmt);
662 dm_task_destroy(dmt);
666 int dm_suspend_and_wipe_key(const char *name)
668 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
671 if (!_dm_message(name, "key wipe")) {
672 _dm_simple(DM_DEVICE_RESUME, name, 1);
679 int dm_resume_and_reinstate_key(const char *name,
683 int msg_size = key_size * 2 + 10; // key set <key>
687 msg = safe_alloc(msg_size);
691 memset(msg, 0, msg_size);
692 strcpy(msg, "key set ");
693 hex_key(&msg[8], key_size, key);
695 if (!_dm_message(name, msg) ||
696 !_dm_simple(DM_DEVICE_RESUME, name, 1))
703 const char *dm_get_dir(void)