4 #include <libdevmapper.h>
5 #include <linux/dm-ioctl.h>
13 #define DEVICE_DIR "/dev"
14 #define DM_UUID_PREFIX "CRYPT-"
15 #define DM_UUID_PREFIX_LEN 6
16 #define DM_CRYPT_TARGET "crypt"
19 static int _dm_use_count = 0;
20 static struct crypt_device *_context = NULL;
22 static void set_dm_error(int level, const char *file, int line,
29 if (vasprintf(&msg, f, va) > 0) {
31 log_err(_context, msg);
32 log_err(_context, "\n");
40 static int _dm_simple(int task, const char *name);
42 int dm_init(struct crypt_device *context, int check_kernel)
44 if (!_dm_use_count++) {
45 log_dbg("Initialising device-mapper backend%s.",
46 check_kernel ? "" : " (NO kernel check requested)");
47 if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL)) {
48 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
51 if (getuid() || geteuid())
52 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
53 dm_log_init(set_dm_error);
54 dm_log_init_verbose(10);
57 // FIXME: global context is not safe
61 return 1; /* unsafe memory */
66 if (_dm_use_count && (!--_dm_use_count)) {
67 log_dbg("Releasing device-mapper backend.");
68 dm_log_init_verbose(0);
75 static char *__lookup_dev(char *path, dev_t dev)
84 path[PATH_MAX - 1] = '\0';
85 ptr = path + strlen(path);
88 space = PATH_MAX - (ptr - path);
94 while((entry = readdir(dir))) {
95 if (entry->d_name[0] == '.' &&
96 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
97 entry->d_name[2] == '\0')))
100 strncpy(ptr, entry->d_name, space);
101 if (lstat(path, &st) < 0)
104 if (S_ISDIR(st.st_mode)) {
105 result = __lookup_dev(path, dev);
108 } else if (S_ISBLK(st.st_mode)) {
109 if (st.st_rdev == dev) {
110 result = strdup(path);
121 static char *lookup_dev(const char *dev)
123 uint32_t major, minor;
124 char buf[PATH_MAX + 1];
126 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
129 strncpy(buf, DEVICE_DIR, PATH_MAX);
130 buf[PATH_MAX] = '\0';
132 return __lookup_dev(buf, makedev(major, minor));
135 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
138 long read_ahead_long;
140 if ((fd = open(dev, O_RDONLY)) < 0)
143 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
147 *read_ahead = (uint32_t) read_ahead_long;
152 static void hex_key(char *hexkey, size_t key_size, const char *key)
156 for(i = 0; i < key_size; i++)
157 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
160 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
161 const char *cipher, size_t key_size, const char *key)
166 hexkey = safe_alloc(key_size * 2 + 1);
170 hex_key(hexkey, key_size, key);
172 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
176 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
177 cipher, hexkey, skip, device, offset);
185 static int _dm_simple(int task, const char *name)
190 if (!(dmt = dm_task_create(task)))
193 if (name && !dm_task_set_name(dmt, name))
196 r = dm_task_run(dmt);
199 dm_task_destroy(dmt);
203 static int _error_device(const char *name, size_t size)
208 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
211 if (!dm_task_set_name(dmt, name))
214 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
217 if (!dm_task_set_ro(dmt))
220 if (!dm_task_no_open_count(dmt))
223 if (!dm_task_run(dmt))
226 if (!_dm_simple(DM_DEVICE_RESUME, name)) {
227 _dm_simple(DM_DEVICE_CLEAR, name);
234 dm_task_destroy(dmt);
238 int dm_remove_device(const char *name, int force, uint64_t size)
241 int retries = force ? RETRY_COUNT : 1;
242 int error_target = 0;
244 if (!name || (force && !size))
248 r = _dm_simple(DM_DEVICE_REMOVE, name) ? 0 : -EINVAL;
249 if (--retries && r) {
250 log_dbg("WARNING: other process locked internal device %s, %s.",
251 name, retries ? "retrying remove" : "giving up");
252 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
253 debug_processes_using_device(name);
255 if (force && !error_target) {
256 /* If force flag is set, replace device with error, read-only target.
257 * it should stop processes from reading it and also removed underlying
258 * device from mapping, so it is usable again.
259 * Force flag should be used only for temporary devices, which are
260 * intended to work inside cryptsetup only!
261 * Anyway, if some process try to read temporary cryptsetup device,
262 * it is bug - no other process should try touch it (e.g. udev).
264 _error_device(name, size);
268 } while (r == -EINVAL && retries);
270 dm_task_update_nodes();
275 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
277 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
279 * CRYPT-LUKS1-00000000000000000000000000000000-name
282 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
284 char *ptr, uuid2[UUID_LEN] = {0};
288 /* Remove '-' chars */
289 if (uuid && !uuid_parse(uuid, uu)) {
290 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
291 if (uuid[i] != '-') {
297 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
298 type ?: "", type ? "-" : "",
299 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
302 log_dbg("DM-UUID is %s", buf);
304 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
307 int dm_create_device(const char *name,
320 struct dm_task *dmt = NULL;
321 struct dm_task *dmt_query = NULL;
325 char dev_uuid[DM_UUID_LEN] = {0};
327 uint32_t read_ahead = 0;
329 params = get_params(device, skip, offset, cipher, key_size, key);
333 /* All devices must have DM_UUID, only resize on old device is exception */
335 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
338 if (!dm_task_set_name(dmt, name))
341 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
343 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
346 if (!dm_task_set_name(dmt, name))
349 if (!dm_task_set_uuid(dmt, dev_uuid))
354 if (read_only && !dm_task_set_ro(dmt))
356 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
359 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
360 if (_dev_read_ahead(device, &read_ahead) &&
361 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
365 if (!dm_task_run(dmt))
369 dm_task_destroy(dmt);
370 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
372 if (!dm_task_set_name(dmt, name))
374 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
376 if (!dm_task_run(dmt))
380 if (!dm_task_get_info(dmt, &dmi))
385 if (r < 0 && !reload) {
387 error = strdup(get_error());
389 dm_remove_device(name, 0, 0);
401 dm_task_destroy(dmt);
403 dm_task_destroy(dmt_query);
404 dm_task_update_nodes();
408 int dm_status_device(const char *name)
412 uint64_t start, length;
413 char *target_type, *params;
417 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
420 if (!dm_task_set_name(dmt, name)) {
425 if (!dm_task_run(dmt)) {
430 if (!dm_task_get_info(dmt, &dmi)) {
440 next = dm_get_next_target(dmt, next, &start, &length,
441 &target_type, ¶ms);
442 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
446 r = (dmi.open_count > 0);
449 dm_task_destroy(dmt);
454 int dm_query_device(const char *name,
468 uint64_t start, length, val64;
469 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
473 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
475 if (!dm_task_set_name(dmt, name))
478 if (!dm_task_run(dmt))
482 if (!dm_task_get_info(dmt, &dmi))
490 next = dm_get_next_target(dmt, next, &start, &length,
491 &target_type, ¶ms);
492 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
499 rcipher = strsep(¶ms, " ");
502 *cipher = strdup(rcipher);
505 key_ = strsep(¶ms, " ");
508 val64 = strtoull(params, ¶ms, 10);
516 rdevice = strsep(¶ms, " ");
518 *device = lookup_dev(rdevice);
523 val64 = strtoull(params, ¶ms, 10);
531 *key_size = strlen(key_) / 2;
534 if (key_size && key) {
535 *key = safe_alloc(*key_size);
542 for(i = 0; i < *key_size; i++) {
543 memcpy(buffer, &key_[i * 2], 2);
544 (*key)[i] = strtoul(buffer, &endp, 16);
545 if (endp != &buffer[2]) {
552 memset(key_, 0, strlen(key_));
555 *read_only = dmi.read_only;
558 *suspended = dmi.suspended;
560 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
561 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
562 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
564 r = (dmi.open_count > 0);
567 dm_task_destroy(dmt);
572 static int _dm_message(const char *name, const char *msg)
577 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
580 if (name && !dm_task_set_name(dmt, name))
583 if (!dm_task_set_sector(dmt, (uint64_t) 0))
586 if (!dm_task_set_message(dmt, msg))
589 r = dm_task_run(dmt);
592 dm_task_destroy(dmt);
596 int dm_suspend_and_wipe_key(const char *name)
598 if (!_dm_simple(DM_DEVICE_SUSPEND, name))
601 if (!_dm_message(name, "key wipe")) {
602 _dm_simple(DM_DEVICE_RESUME, name);
609 int dm_resume_and_reinstate_key(const char *name,
613 int msg_size = key_size * 2 + 10; // key set <key>
617 msg = safe_alloc(msg_size);
621 memset(msg, 0, msg_size);
622 strcpy(msg, "key set ");
623 hex_key(&msg[8], key_size, key);
625 if (!_dm_message(name, msg) ||
626 !_dm_simple(DM_DEVICE_RESUME, name))
633 const char *dm_get_dir(void)