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");
253 if (force && !error_target) {
254 /* If force flag is set, replace device with error, read-only target.
255 * it should stop processes from reading it and also removed underlying
256 * device from mapping, so it is usable again.
257 * Force flag should be used only for temporary devices, which are
258 * intended to work inside cryptsetup only!
259 * Anyway, if some process try to read temporary cryptsetup device,
260 * it is bug - no other process should try touch it (e.g. udev).
262 _error_device(name, size);
266 } while (r == -EINVAL && retries);
268 dm_task_update_nodes();
273 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
275 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
277 * CRYPT-LUKS1-00000000000000000000000000000000-name
280 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
282 char *ptr, uuid2[UUID_LEN] = {0};
286 /* Remove '-' chars */
287 if (uuid && !uuid_parse(uuid, uu)) {
288 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
289 if (uuid[i] != '-') {
295 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
296 type ?: "", type ? "-" : "",
297 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
300 log_dbg("DM-UUID is %s", buf);
302 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
305 int dm_create_device(const char *name,
318 struct dm_task *dmt = NULL;
319 struct dm_task *dmt_query = NULL;
323 char dev_uuid[DM_UUID_LEN] = {0};
325 uint32_t read_ahead = 0;
327 params = get_params(device, skip, offset, cipher, key_size, key);
331 /* All devices must have DM_UUID, only resize on old device is exception */
333 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
336 if (!dm_task_set_name(dmt, name))
339 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
341 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
344 if (!dm_task_set_name(dmt, name))
347 if (!dm_task_set_uuid(dmt, dev_uuid))
352 if (read_only && !dm_task_set_ro(dmt))
354 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
357 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
358 if (_dev_read_ahead(device, &read_ahead) &&
359 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
363 if (!dm_task_run(dmt))
367 dm_task_destroy(dmt);
368 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
370 if (!dm_task_set_name(dmt, name))
372 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
374 if (!dm_task_run(dmt))
378 if (!dm_task_get_info(dmt, &dmi))
383 if (r < 0 && !reload) {
385 error = strdup(get_error());
387 dm_remove_device(name, 0, 0);
399 dm_task_destroy(dmt);
401 dm_task_destroy(dmt_query);
402 dm_task_update_nodes();
406 int dm_status_device(const char *name)
410 uint64_t start, length;
411 char *target_type, *params;
415 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
418 if (!dm_task_set_name(dmt, name)) {
423 if (!dm_task_run(dmt)) {
428 if (!dm_task_get_info(dmt, &dmi)) {
438 next = dm_get_next_target(dmt, next, &start, &length,
439 &target_type, ¶ms);
440 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
444 r = (dmi.open_count > 0);
447 dm_task_destroy(dmt);
452 int dm_query_device(const char *name,
466 uint64_t start, length, val64;
467 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
471 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
473 if (!dm_task_set_name(dmt, name))
476 if (!dm_task_run(dmt))
480 if (!dm_task_get_info(dmt, &dmi))
488 next = dm_get_next_target(dmt, next, &start, &length,
489 &target_type, ¶ms);
490 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
497 rcipher = strsep(¶ms, " ");
500 *cipher = strdup(rcipher);
503 key_ = strsep(¶ms, " ");
506 val64 = strtoull(params, ¶ms, 10);
514 rdevice = strsep(¶ms, " ");
516 *device = lookup_dev(rdevice);
521 val64 = strtoull(params, ¶ms, 10);
529 *key_size = strlen(key_) / 2;
532 if (key_size && key) {
533 *key = safe_alloc(*key_size);
540 for(i = 0; i < *key_size; i++) {
541 memcpy(buffer, &key_[i * 2], 2);
542 (*key)[i] = strtoul(buffer, &endp, 16);
543 if (endp != &buffer[2]) {
550 memset(key_, 0, strlen(key_));
553 *read_only = dmi.read_only;
556 *suspended = dmi.suspended;
558 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
559 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
560 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
562 r = (dmi.open_count > 0);
565 dm_task_destroy(dmt);
570 static int _dm_message(const char *name, const char *msg)
575 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
578 if (name && !dm_task_set_name(dmt, name))
581 if (!dm_task_set_sector(dmt, (uint64_t) 0))
584 if (!dm_task_set_message(dmt, msg))
587 r = dm_task_run(dmt);
590 dm_task_destroy(dmt);
594 int dm_suspend_and_wipe_key(const char *name)
596 if (!_dm_simple(DM_DEVICE_SUSPEND, name))
599 if (!_dm_message(name, "key wipe")) {
600 _dm_simple(DM_DEVICE_RESUME, name);
607 int dm_resume_and_reinstate_key(const char *name,
611 int msg_size = key_size * 2 + 10; // key set <key>
615 msg = safe_alloc(msg_size);
619 memset(msg, 0, msg_size);
620 strcpy(msg, "key set ");
621 hex_key(&msg[8], key_size, key);
623 if (!_dm_message(name, msg) ||
624 !_dm_simple(DM_DEVICE_RESUME, name))
631 const char *dm_get_dir(void)