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)
87 path[PATH_MAX - 1] = '\0';
88 ptr = path + strlen(path);
91 space = PATH_MAX - (ptr - path);
97 while((entry = readdir(dir))) {
98 if (entry->d_name[0] == '.' &&
99 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
100 entry->d_name[2] == '\0')))
103 strncpy(ptr, entry->d_name, space);
104 if (lstat(path, &st) < 0)
107 if (S_ISDIR(st.st_mode)) {
108 result = __lookup_dev(path, dev);
111 } else if (S_ISBLK(st.st_mode)) {
112 if (st.st_rdev == dev) {
113 result = strdup(path);
124 static char *lookup_dev(const char *dev)
126 uint32_t major, minor;
127 char buf[PATH_MAX + 1];
129 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
132 strncpy(buf, DEVICE_DIR, PATH_MAX);
133 buf[PATH_MAX] = '\0';
135 return __lookup_dev(buf, makedev(major, minor));
138 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
141 long read_ahead_long;
143 if ((fd = open(dev, O_RDONLY)) < 0)
146 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
150 *read_ahead = (uint32_t) read_ahead_long;
155 static void hex_key(char *hexkey, size_t key_size, const char *key)
159 for(i = 0; i < key_size; i++)
160 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
163 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
164 const char *cipher, size_t key_size, const char *key)
169 hexkey = safe_alloc(key_size * 2 + 1);
173 hex_key(hexkey, key_size, key);
175 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
179 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
180 cipher, hexkey, skip, device, offset);
188 static int _dm_simple(int task, const char *name)
193 if (!(dmt = dm_task_create(task)))
196 if (name && !dm_task_set_name(dmt, name))
199 r = dm_task_run(dmt);
202 dm_task_destroy(dmt);
206 static int _error_device(const char *name, size_t size)
211 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
214 if (!dm_task_set_name(dmt, name))
217 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
220 if (!dm_task_set_ro(dmt))
223 if (!dm_task_no_open_count(dmt))
226 if (!dm_task_run(dmt))
229 if (!_dm_simple(DM_DEVICE_RESUME, name)) {
230 _dm_simple(DM_DEVICE_CLEAR, name);
237 dm_task_destroy(dmt);
241 int dm_remove_device(const char *name, int force, uint64_t size)
244 int retries = force ? RETRY_COUNT : 1;
245 int error_target = 0;
247 if (!name || (force && !size))
251 r = _dm_simple(DM_DEVICE_REMOVE, name) ? 0 : -EINVAL;
252 if (--retries && r) {
253 log_dbg("WARNING: other process locked internal device %s, %s.",
254 name, retries ? "retrying remove" : "giving up");
255 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
256 debug_processes_using_device(name);
258 if (force && !error_target) {
259 /* If force flag is set, replace device with error, read-only target.
260 * it should stop processes from reading it and also removed underlying
261 * device from mapping, so it is usable again.
262 * Force flag should be used only for temporary devices, which are
263 * intended to work inside cryptsetup only!
264 * Anyway, if some process try to read temporary cryptsetup device,
265 * it is bug - no other process should try touch it (e.g. udev).
267 _error_device(name, size);
271 } while (r == -EINVAL && retries);
273 dm_task_update_nodes();
278 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
280 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
282 * CRYPT-LUKS1-00000000000000000000000000000000-name
285 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
287 char *ptr, uuid2[UUID_LEN] = {0};
291 /* Remove '-' chars */
292 if (uuid && !uuid_parse(uuid, uu)) {
293 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
294 if (uuid[i] != '-') {
300 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
301 type ?: "", type ? "-" : "",
302 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
305 log_dbg("DM-UUID is %s", buf);
307 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
310 int dm_create_device(const char *name,
323 struct dm_task *dmt = NULL;
324 struct dm_task *dmt_query = NULL;
328 char dev_uuid[DM_UUID_LEN] = {0};
330 uint32_t read_ahead = 0;
332 params = get_params(device, skip, offset, cipher, key_size, key);
336 /* All devices must have DM_UUID, only resize on old device is exception */
338 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
341 if (!dm_task_set_name(dmt, name))
344 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
346 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
349 if (!dm_task_set_name(dmt, name))
352 if (!dm_task_set_uuid(dmt, dev_uuid))
357 if (read_only && !dm_task_set_ro(dmt))
359 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
362 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
363 if (_dev_read_ahead(device, &read_ahead) &&
364 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
368 if (!dm_task_run(dmt))
372 dm_task_destroy(dmt);
373 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
375 if (!dm_task_set_name(dmt, name))
377 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
379 if (!dm_task_run(dmt))
383 if (!dm_task_get_info(dmt, &dmi))
388 if (r < 0 && !reload) {
390 error = strdup(get_error());
392 dm_remove_device(name, 0, 0);
404 dm_task_destroy(dmt);
406 dm_task_destroy(dmt_query);
407 dm_task_update_nodes();
411 int dm_status_device(const char *name)
415 uint64_t start, length;
416 char *target_type, *params;
420 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
423 if (!dm_task_set_name(dmt, name)) {
428 if (!dm_task_run(dmt)) {
433 if (!dm_task_get_info(dmt, &dmi)) {
443 next = dm_get_next_target(dmt, next, &start, &length,
444 &target_type, ¶ms);
445 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
449 r = (dmi.open_count > 0);
452 dm_task_destroy(dmt);
457 int dm_query_device(const char *name,
471 uint64_t start, length, val64;
472 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
476 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
478 if (!dm_task_set_name(dmt, name))
481 if (!dm_task_run(dmt))
485 if (!dm_task_get_info(dmt, &dmi))
493 next = dm_get_next_target(dmt, next, &start, &length,
494 &target_type, ¶ms);
495 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
502 rcipher = strsep(¶ms, " ");
505 *cipher = strdup(rcipher);
508 key_ = strsep(¶ms, " ");
511 val64 = strtoull(params, ¶ms, 10);
519 rdevice = strsep(¶ms, " ");
521 *device = lookup_dev(rdevice);
526 val64 = strtoull(params, ¶ms, 10);
534 *key_size = strlen(key_) / 2;
537 if (key_size && key) {
538 *key = safe_alloc(*key_size);
545 for(i = 0; i < *key_size; i++) {
546 memcpy(buffer, &key_[i * 2], 2);
547 (*key)[i] = strtoul(buffer, &endp, 16);
548 if (endp != &buffer[2]) {
555 memset(key_, 0, strlen(key_));
558 *read_only = dmi.read_only;
561 *suspended = dmi.suspended;
563 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
564 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
565 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
567 r = (dmi.open_count > 0);
570 dm_task_destroy(dmt);
575 static int _dm_message(const char *name, const char *msg)
580 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
583 if (name && !dm_task_set_name(dmt, name))
586 if (!dm_task_set_sector(dmt, (uint64_t) 0))
589 if (!dm_task_set_message(dmt, msg))
592 r = dm_task_run(dmt);
595 dm_task_destroy(dmt);
599 int dm_suspend_and_wipe_key(const char *name)
601 if (!_dm_simple(DM_DEVICE_SUSPEND, name))
604 if (!_dm_message(name, "key wipe")) {
605 _dm_simple(DM_DEVICE_RESUME, name);
612 int dm_resume_and_reinstate_key(const char *name,
616 int msg_size = key_size * 2 + 10; // key set <key>
620 msg = safe_alloc(msg_size);
624 memset(msg, 0, msg_size);
625 strcpy(msg, "key set ");
626 hex_key(&msg[8], key_size, key);
628 if (!_dm_message(name, msg) ||
629 !_dm_simple(DM_DEVICE_RESUME, name))
636 const char *dm_get_dir(void)