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 dm_log_init(set_dm_error);
52 dm_log_init_verbose(10);
55 // FIXME: global context is not safe
59 return 1; /* unsafe memory */
64 if (_dm_use_count && (!--_dm_use_count)) {
65 log_dbg("Releasing device-mapper backend.");
66 dm_log_init_verbose(0);
73 static char *__lookup_dev(char *path, dev_t dev)
82 path[PATH_MAX - 1] = '\0';
83 ptr = path + strlen(path);
86 space = PATH_MAX - (ptr - path);
92 while((entry = readdir(dir))) {
93 if (entry->d_name[0] == '.' &&
94 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
95 entry->d_name[2] == '\0')))
98 strncpy(ptr, entry->d_name, space);
99 if (lstat(path, &st) < 0)
102 if (S_ISDIR(st.st_mode)) {
103 result = __lookup_dev(path, dev);
106 } else if (S_ISBLK(st.st_mode)) {
107 if (st.st_rdev == dev) {
108 result = strdup(path);
119 static char *lookup_dev(const char *dev)
121 uint32_t major, minor;
122 char buf[PATH_MAX + 1];
124 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
127 strncpy(buf, DEVICE_DIR, PATH_MAX);
128 buf[PATH_MAX] = '\0';
130 return __lookup_dev(buf, makedev(major, minor));
133 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
136 long read_ahead_long;
138 if ((fd = open(dev, O_RDONLY)) < 0)
141 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
145 *read_ahead = (uint32_t) read_ahead_long;
150 static void hex_key(char *hexkey, size_t key_size, const char *key)
154 for(i = 0; i < key_size; i++)
155 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
158 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
159 const char *cipher, size_t key_size, const char *key)
164 hexkey = safe_alloc(key_size * 2 + 1);
168 hex_key(hexkey, key_size, key);
170 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
174 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
175 cipher, hexkey, skip, device, offset);
183 static int _dm_simple(int task, const char *name)
188 if (!(dmt = dm_task_create(task)))
191 if (name && !dm_task_set_name(dmt, name))
194 r = dm_task_run(dmt);
197 dm_task_destroy(dmt);
201 static int _error_device(const char *name, size_t size)
206 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
209 if (!dm_task_set_name(dmt, name))
212 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
215 if (!dm_task_set_ro(dmt))
218 if (!dm_task_no_open_count(dmt))
221 if (!dm_task_run(dmt))
224 if (!_dm_simple(DM_DEVICE_RESUME, name)) {
225 _dm_simple(DM_DEVICE_CLEAR, name);
232 dm_task_destroy(dmt);
236 int dm_remove_device(const char *name, int force, uint64_t size)
239 int retries = force ? RETRY_COUNT : 1;
240 int error_target = 0;
242 if (!name || (force && !size))
246 r = _dm_simple(DM_DEVICE_REMOVE, name) ? 0 : -EINVAL;
247 if (--retries && r) {
248 log_dbg("WARNING: other process locked internal device %s, %s.",
249 name, retries ? "retrying remove" : "giving up");
251 if (force && !error_target) {
252 /* If force flag is set, replace device with error, read-only target.
253 * it should stop processes from reading it and also removed underlying
254 * device from mapping, so it is usable again.
255 * Force flag should be used only for temporary devices, which are
256 * intended to work inside cryptsetup only!
257 * Anyway, if some process try to read temporary cryptsetup device,
258 * it is bug - no other process should try touch it (e.g. udev).
260 _error_device(name, size);
264 } while (r == -EINVAL && retries);
266 dm_task_update_nodes();
271 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
273 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
275 * CRYPT-LUKS1-00000000000000000000000000000000-name
278 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
280 char *ptr, uuid2[UUID_LEN] = {0};
284 /* Remove '-' chars */
285 if (uuid && !uuid_parse(uuid, uu)) {
286 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
287 if (uuid[i] != '-') {
293 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
294 type ?: "", type ? "-" : "",
295 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
298 log_dbg("DM-UUID is %s", buf);
300 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
303 int dm_create_device(const char *name,
316 struct dm_task *dmt = NULL;
317 struct dm_task *dmt_query = NULL;
321 char dev_uuid[DM_UUID_LEN] = {0};
323 uint32_t read_ahead = 0;
325 params = get_params(device, skip, offset, cipher, key_size, key);
329 /* All devices must have DM_UUID, only resize on old device is exception */
331 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
334 if (!dm_task_set_name(dmt, name))
337 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
339 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
342 if (!dm_task_set_name(dmt, name))
345 if (!dm_task_set_uuid(dmt, dev_uuid))
350 if (read_only && !dm_task_set_ro(dmt))
352 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
355 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
356 if (_dev_read_ahead(device, &read_ahead) &&
357 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
361 if (!dm_task_run(dmt))
365 dm_task_destroy(dmt);
366 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
368 if (!dm_task_set_name(dmt, name))
370 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
372 if (!dm_task_run(dmt))
376 if (!dm_task_get_info(dmt, &dmi))
381 if (r < 0 && !reload) {
383 error = strdup(get_error());
385 dm_remove_device(name, 0, 0);
397 dm_task_destroy(dmt);
399 dm_task_destroy(dmt_query);
400 dm_task_update_nodes();
404 int dm_status_device(const char *name)
408 uint64_t start, length;
409 char *target_type, *params;
413 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
416 if (!dm_task_set_name(dmt, name)) {
421 if (!dm_task_run(dmt)) {
426 if (!dm_task_get_info(dmt, &dmi)) {
436 next = dm_get_next_target(dmt, next, &start, &length,
437 &target_type, ¶ms);
438 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
442 r = (dmi.open_count > 0);
445 dm_task_destroy(dmt);
450 int dm_query_device(const char *name,
464 uint64_t start, length, val64;
465 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
469 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
471 if (!dm_task_set_name(dmt, name))
474 if (!dm_task_run(dmt))
478 if (!dm_task_get_info(dmt, &dmi))
486 next = dm_get_next_target(dmt, next, &start, &length,
487 &target_type, ¶ms);
488 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
495 rcipher = strsep(¶ms, " ");
498 *cipher = strdup(rcipher);
501 key_ = strsep(¶ms, " ");
504 val64 = strtoull(params, ¶ms, 10);
512 rdevice = strsep(¶ms, " ");
514 *device = lookup_dev(rdevice);
519 val64 = strtoull(params, ¶ms, 10);
527 *key_size = strlen(key_) / 2;
530 if (key_size && key) {
531 *key = safe_alloc(*key_size);
538 for(i = 0; i < *key_size; i++) {
539 memcpy(buffer, &key_[i * 2], 2);
540 (*key)[i] = strtoul(buffer, &endp, 16);
541 if (endp != &buffer[2]) {
548 memset(key_, 0, strlen(key_));
551 *read_only = dmi.read_only;
554 *suspended = dmi.suspended;
556 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
557 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
558 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
560 r = (dmi.open_count > 0);
563 dm_task_destroy(dmt);
568 static int _dm_message(const char *name, const char *msg)
573 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
576 if (name && !dm_task_set_name(dmt, name))
579 if (!dm_task_set_sector(dmt, (uint64_t) 0))
582 if (!dm_task_set_message(dmt, msg))
585 r = dm_task_run(dmt);
588 dm_task_destroy(dmt);
592 int dm_suspend_and_wipe_key(const char *name)
594 if (!_dm_simple(DM_DEVICE_SUSPEND, name))
597 if (!_dm_message(name, "key wipe")) {
598 _dm_simple(DM_DEVICE_RESUME, name);
605 int dm_resume_and_reinstate_key(const char *name,
609 int msg_size = key_size * 2 + 10; // key set <key>
613 msg = safe_alloc(msg_size);
617 memset(msg, 0, msg_size);
618 strcpy(msg, "key set ");
619 hex_key(&msg[8], key_size, key);
621 if (!_dm_message(name, msg) ||
622 !_dm_simple(DM_DEVICE_RESUME, name))
629 const char *dm_get_dir(void)