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 static void set_dm_error(int level, const char *file, int line,
32 if (vasprintf(&msg, f, va) > 0) {
34 log_err(_context, msg);
35 log_err(_context, "\n");
43 static int _dm_simple(int task, const char *name);
45 int dm_init(struct crypt_device *context, int check_kernel)
47 if (!_dm_use_count++) {
48 log_dbg("Initialising device-mapper backend%s.",
49 check_kernel ? "" : " (NO kernel check requested)");
50 if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL)) {
51 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
54 if (getuid() || geteuid())
55 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
56 dm_log_init(set_dm_error);
57 dm_log_init_verbose(10);
60 // FIXME: global context is not safe
64 return 1; /* unsafe memory */
69 if (_dm_use_count && (!--_dm_use_count)) {
70 log_dbg("Releasing device-mapper backend.");
71 dm_log_init_verbose(0);
78 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
87 /* Ignore strange nested directories */
88 if (dir_level > max_level)
91 path[PATH_MAX - 1] = '\0';
92 ptr = path + strlen(path);
95 space = PATH_MAX - (ptr - path);
101 while((entry = readdir(dir))) {
102 if (entry->d_name[0] == '.' ||
103 !strncmp(entry->d_name, "..", 2))
106 strncpy(ptr, entry->d_name, space);
107 if (lstat(path, &st) < 0)
110 if (S_ISDIR(st.st_mode)) {
111 result = __lookup_dev(path, dev, dir_level + 1, max_level);
114 } else if (S_ISBLK(st.st_mode)) {
115 /* workaround: ignore dm-X devices, these are internal kernel names */
116 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
118 if (st.st_rdev == dev) {
119 result = strdup(path);
129 static char *lookup_dev(const char *dev_id)
131 uint32_t major, minor;
133 char *result, buf[PATH_MAX + 1];
135 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
138 dev = makedev(major, minor);
139 strncpy(buf, DEVICE_DIR, PATH_MAX);
140 buf[PATH_MAX] = '\0';
142 /* First try low level device */
143 if ((result = __lookup_dev(buf, dev, 0, 0)))
146 /* If it is dm, try DM dir */
147 if (dm_is_dm_major(major)) {
148 strncpy(buf, dm_dir(), PATH_MAX);
149 if ((result = __lookup_dev(buf, dev, 0, 0)))
153 strncpy(buf, DEVICE_DIR, PATH_MAX);
154 result = __lookup_dev(buf, dev, 0, 4);
156 /* If not found, return major:minor */
157 return result ?: strdup(dev_id);
160 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
163 long read_ahead_long;
165 if ((fd = open(dev, O_RDONLY)) < 0)
168 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
172 *read_ahead = (uint32_t) read_ahead_long;
177 static void hex_key(char *hexkey, size_t key_size, const char *key)
181 for(i = 0; i < key_size; i++)
182 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
185 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
186 const char *cipher, size_t key_size, const char *key)
191 hexkey = safe_alloc(key_size * 2 + 1);
195 hex_key(hexkey, key_size, key);
197 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
201 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
202 cipher, hexkey, skip, device, offset);
210 static int _dm_simple(int task, const char *name)
215 if (!(dmt = dm_task_create(task)))
218 if (name && !dm_task_set_name(dmt, name))
221 r = dm_task_run(dmt);
224 dm_task_destroy(dmt);
228 static int _error_device(const char *name, size_t size)
233 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
236 if (!dm_task_set_name(dmt, name))
239 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
242 if (!dm_task_set_ro(dmt))
245 if (!dm_task_no_open_count(dmt))
248 if (!dm_task_run(dmt))
251 if (!_dm_simple(DM_DEVICE_RESUME, name)) {
252 _dm_simple(DM_DEVICE_CLEAR, name);
259 dm_task_destroy(dmt);
263 int dm_remove_device(const char *name, int force, uint64_t size)
266 int retries = force ? RETRY_COUNT : 1;
267 int error_target = 0;
269 if (!name || (force && !size))
273 r = _dm_simple(DM_DEVICE_REMOVE, name) ? 0 : -EINVAL;
274 if (--retries && r) {
275 log_dbg("WARNING: other process locked internal device %s, %s.",
276 name, retries ? "retrying remove" : "giving up");
277 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
278 debug_processes_using_device(name);
280 if (force && !error_target) {
281 /* If force flag is set, replace device with error, read-only target.
282 * it should stop processes from reading it and also removed underlying
283 * device from mapping, so it is usable again.
284 * Force flag should be used only for temporary devices, which are
285 * intended to work inside cryptsetup only!
286 * Anyway, if some process try to read temporary cryptsetup device,
287 * it is bug - no other process should try touch it (e.g. udev).
289 _error_device(name, size);
293 } while (r == -EINVAL && retries);
295 dm_task_update_nodes();
300 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
302 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
304 * CRYPT-LUKS1-00000000000000000000000000000000-name
307 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
309 char *ptr, uuid2[UUID_LEN] = {0};
313 /* Remove '-' chars */
314 if (uuid && !uuid_parse(uuid, uu)) {
315 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
316 if (uuid[i] != '-') {
322 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
323 type ?: "", type ? "-" : "",
324 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
327 log_dbg("DM-UUID is %s", buf);
329 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
332 int dm_create_device(const char *name,
345 struct dm_task *dmt = NULL;
346 struct dm_task *dmt_query = NULL;
350 char dev_uuid[DM_UUID_LEN] = {0};
352 uint32_t read_ahead = 0;
354 params = get_params(device, skip, offset, cipher, key_size, key);
358 /* All devices must have DM_UUID, only resize on old device is exception */
360 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
363 if (!dm_task_set_name(dmt, name))
366 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
368 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
371 if (!dm_task_set_name(dmt, name))
374 if (!dm_task_set_uuid(dmt, dev_uuid))
379 if (read_only && !dm_task_set_ro(dmt))
381 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
384 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
385 if (_dev_read_ahead(device, &read_ahead) &&
386 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
390 if (!dm_task_run(dmt))
394 dm_task_destroy(dmt);
395 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
397 if (!dm_task_set_name(dmt, name))
399 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
401 if (!dm_task_run(dmt))
405 if (!dm_task_get_info(dmt, &dmi))
410 if (r < 0 && !reload) {
412 error = strdup(get_error());
414 dm_remove_device(name, 0, 0);
426 dm_task_destroy(dmt);
428 dm_task_destroy(dmt_query);
429 dm_task_update_nodes();
433 int dm_status_device(const char *name)
437 uint64_t start, length;
438 char *target_type, *params;
442 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
445 if (!dm_task_set_name(dmt, name)) {
450 if (!dm_task_run(dmt)) {
455 if (!dm_task_get_info(dmt, &dmi)) {
465 next = dm_get_next_target(dmt, next, &start, &length,
466 &target_type, ¶ms);
467 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
471 r = (dmi.open_count > 0);
474 dm_task_destroy(dmt);
479 int dm_query_device(const char *name,
493 uint64_t start, length, val64;
494 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
498 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
500 if (!dm_task_set_name(dmt, name))
503 if (!dm_task_run(dmt))
507 if (!dm_task_get_info(dmt, &dmi))
515 next = dm_get_next_target(dmt, next, &start, &length,
516 &target_type, ¶ms);
517 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
524 rcipher = strsep(¶ms, " ");
527 *cipher = strdup(rcipher);
530 key_ = strsep(¶ms, " ");
533 val64 = strtoull(params, ¶ms, 10);
541 rdevice = strsep(¶ms, " ");
543 *device = lookup_dev(rdevice);
548 val64 = strtoull(params, ¶ms, 10);
556 *key_size = strlen(key_) / 2;
559 if (key_size && key) {
560 *key = safe_alloc(*key_size);
567 for(i = 0; i < *key_size; i++) {
568 memcpy(buffer, &key_[i * 2], 2);
569 (*key)[i] = strtoul(buffer, &endp, 16);
570 if (endp != &buffer[2]) {
577 memset(key_, 0, strlen(key_));
580 *read_only = dmi.read_only;
583 *suspended = dmi.suspended;
585 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
586 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
587 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
589 r = (dmi.open_count > 0);
592 dm_task_destroy(dmt);
597 static int _dm_message(const char *name, const char *msg)
602 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
605 if (name && !dm_task_set_name(dmt, name))
608 if (!dm_task_set_sector(dmt, (uint64_t) 0))
611 if (!dm_task_set_message(dmt, msg))
614 r = dm_task_run(dmt);
617 dm_task_destroy(dmt);
621 int dm_suspend_and_wipe_key(const char *name)
623 if (!_dm_simple(DM_DEVICE_SUSPEND, name))
626 if (!_dm_message(name, "key wipe")) {
627 _dm_simple(DM_DEVICE_RESUME, name);
634 int dm_resume_and_reinstate_key(const char *name,
638 int msg_size = key_size * 2 + 10; // key set <key>
642 msg = safe_alloc(msg_size);
646 memset(msg, 0, msg_size);
647 strcpy(msg, "key set ");
648 hex_key(&msg[8], key_size, key);
650 if (!_dm_message(name, msg) ||
651 !_dm_simple(DM_DEVICE_RESUME, name))
658 const char *dm_get_dir(void)