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 /* Compatibility for old device-mapper without udev support */
26 #ifndef HAVE_DM_TASK_SET_COOKIE
27 static int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
28 static int dm_udev_wait(uint32_t cookie) { return 0; };
31 static int _dm_use_udev()
33 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
34 return dm_udev_get_sync_support();
40 static void set_dm_error(int level, const char *file, int line,
47 if (vasprintf(&msg, f, va) > 0) {
49 log_err(_context, msg);
50 log_err(_context, "\n");
58 static int _dm_simple(int task, const char *name, int udev_wait);
60 int dm_init(struct crypt_device *context, int check_kernel)
62 if (!_dm_use_count++) {
63 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
64 check_kernel ? "" : " (NO kernel check requested)",
65 _dm_use_udev() ? "en" : "dis");
66 if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL, 0)) {
67 log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
70 if (getuid() || geteuid())
71 log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
72 dm_log_init(set_dm_error);
73 dm_log_init_verbose(10);
76 // FIXME: global context is not safe
80 return 1; /* unsafe memory */
85 if (_dm_use_count && (!--_dm_use_count)) {
86 log_dbg("Releasing device-mapper backend.");
87 dm_log_init_verbose(0);
94 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
103 /* Ignore strange nested directories */
104 if (dir_level > max_level)
107 path[PATH_MAX - 1] = '\0';
108 ptr = path + strlen(path);
111 space = PATH_MAX - (ptr - path);
117 while((entry = readdir(dir))) {
118 if (entry->d_name[0] == '.' ||
119 !strncmp(entry->d_name, "..", 2))
122 strncpy(ptr, entry->d_name, space);
123 if (lstat(path, &st) < 0)
126 if (S_ISDIR(st.st_mode)) {
127 result = __lookup_dev(path, dev, dir_level + 1, max_level);
130 } else if (S_ISBLK(st.st_mode)) {
131 /* workaround: ignore dm-X devices, these are internal kernel names */
132 if (dir_level == 0 && !strncmp(entry->d_name, "dm-", 3))
134 if (st.st_rdev == dev) {
135 result = strdup(path);
145 static char *lookup_dev(const char *dev_id)
147 uint32_t major, minor;
149 char *result, buf[PATH_MAX + 1];
151 if (sscanf(dev_id, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
154 dev = makedev(major, minor);
155 strncpy(buf, DEVICE_DIR, PATH_MAX);
156 buf[PATH_MAX] = '\0';
158 /* First try low level device */
159 if ((result = __lookup_dev(buf, dev, 0, 0)))
162 /* If it is dm, try DM dir */
163 if (dm_is_dm_major(major)) {
164 strncpy(buf, dm_dir(), PATH_MAX);
165 if ((result = __lookup_dev(buf, dev, 0, 0)))
169 strncpy(buf, DEVICE_DIR, PATH_MAX);
170 result = __lookup_dev(buf, dev, 0, 4);
172 /* If not found, return major:minor */
173 return result ?: strdup(dev_id);
176 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
179 long read_ahead_long;
181 if ((fd = open(dev, O_RDONLY)) < 0)
184 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
188 *read_ahead = (uint32_t) read_ahead_long;
193 static void hex_key(char *hexkey, size_t key_size, const char *key)
197 for(i = 0; i < key_size; i++)
198 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
201 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
202 const char *cipher, size_t key_size, const char *key)
207 hexkey = safe_alloc(key_size * 2 + 1);
211 hex_key(hexkey, key_size, key);
213 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
217 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
218 cipher, hexkey, skip, device, offset);
226 static int _dm_simple(int task, const char *name, int udev_wait)
235 if (!(dmt = dm_task_create(task)))
238 if (name && !dm_task_set_name(dmt, name))
241 if (udev_wait && !dm_task_set_cookie(dmt, &cookie, 0));
243 r = dm_task_run(dmt);
246 (void)dm_udev_wait(cookie);
249 dm_task_destroy(dmt);
253 static int _error_device(const char *name, size_t size)
258 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
261 if (!dm_task_set_name(dmt, name))
264 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
267 if (!dm_task_set_ro(dmt))
270 if (!dm_task_no_open_count(dmt))
273 if (!dm_task_run(dmt))
276 if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
277 _dm_simple(DM_DEVICE_CLEAR, name, 0);
284 dm_task_destroy(dmt);
288 int dm_remove_device(const char *name, int force, uint64_t size)
291 int retries = force ? RETRY_COUNT : 1;
292 int error_target = 0;
294 if (!name || (force && !size))
298 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
299 if (--retries && r) {
300 log_dbg("WARNING: other process locked internal device %s, %s.",
301 name, retries ? "retrying remove" : "giving up");
302 if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
303 debug_processes_using_device(name);
305 if (force && !error_target) {
306 /* If force flag is set, replace device with error, read-only target.
307 * it should stop processes from reading it and also removed underlying
308 * device from mapping, so it is usable again.
309 * Force flag should be used only for temporary devices, which are
310 * intended to work inside cryptsetup only!
311 * Anyway, if some process try to read temporary cryptsetup device,
312 * it is bug - no other process should try touch it (e.g. udev).
314 _error_device(name, size);
318 } while (r == -EINVAL && retries);
320 dm_task_update_nodes();
325 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
327 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
329 * CRYPT-LUKS1-00000000000000000000000000000000-name
332 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
334 char *ptr, uuid2[UUID_LEN] = {0};
338 /* Remove '-' chars */
339 if (uuid && !uuid_parse(uuid, uu)) {
340 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
341 if (uuid[i] != '-') {
347 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
348 type ?: "", type ? "-" : "",
349 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
352 log_dbg("DM-UUID is %s", buf);
354 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
357 int dm_create_device(const char *name,
370 struct dm_task *dmt = NULL;
371 struct dm_task *dmt_query = NULL;
375 char dev_uuid[DM_UUID_LEN] = {0};
377 uint32_t read_ahead = 0;
380 params = get_params(device, skip, offset, cipher, key_size, key);
384 /* All devices must have DM_UUID, only resize on old device is exception */
386 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
389 if (!dm_task_set_name(dmt, name))
392 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
394 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
397 if (!dm_task_set_name(dmt, name))
400 if (!dm_task_set_uuid(dmt, dev_uuid))
403 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, 0))
407 if (read_only && !dm_task_set_ro(dmt))
409 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
412 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
413 if (_dev_read_ahead(device, &read_ahead) &&
414 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
418 if (!dm_task_run(dmt))
422 dm_task_destroy(dmt);
423 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
425 if (!dm_task_set_name(dmt, name))
427 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
429 if (_dm_use_udev() && !dm_task_set_cookie(dmt, &cookie, 0))
431 if (!dm_task_run(dmt))
436 (void)dm_udev_wait(cookie);
438 if (!dm_task_get_info(dmt, &dmi))
443 if (r < 0 && !reload) {
445 error = strdup(get_error());
447 dm_remove_device(name, 0, 0);
459 dm_task_destroy(dmt);
461 dm_task_destroy(dmt_query);
462 dm_task_update_nodes();
466 int dm_status_device(const char *name)
470 uint64_t start, length;
471 char *target_type, *params;
475 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
478 if (!dm_task_set_name(dmt, name)) {
483 if (!dm_task_run(dmt)) {
488 if (!dm_task_get_info(dmt, &dmi)) {
498 next = dm_get_next_target(dmt, next, &start, &length,
499 &target_type, ¶ms);
500 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
504 r = (dmi.open_count > 0);
507 dm_task_destroy(dmt);
512 int dm_query_device(const char *name,
526 uint64_t start, length, val64;
527 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
531 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
533 if (!dm_task_set_name(dmt, name))
536 if (!dm_task_run(dmt))
540 if (!dm_task_get_info(dmt, &dmi))
548 next = dm_get_next_target(dmt, next, &start, &length,
549 &target_type, ¶ms);
550 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
557 rcipher = strsep(¶ms, " ");
560 *cipher = strdup(rcipher);
563 key_ = strsep(¶ms, " ");
566 val64 = strtoull(params, ¶ms, 10);
574 rdevice = strsep(¶ms, " ");
576 *device = lookup_dev(rdevice);
581 val64 = strtoull(params, ¶ms, 10);
589 *key_size = strlen(key_) / 2;
592 if (key_size && key) {
593 *key = safe_alloc(*key_size);
600 for(i = 0; i < *key_size; i++) {
601 memcpy(buffer, &key_[i * 2], 2);
602 (*key)[i] = strtoul(buffer, &endp, 16);
603 if (endp != &buffer[2]) {
610 memset(key_, 0, strlen(key_));
613 *read_only = dmi.read_only;
616 *suspended = dmi.suspended;
618 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
619 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
620 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
622 r = (dmi.open_count > 0);
625 dm_task_destroy(dmt);
630 static int _dm_message(const char *name, const char *msg)
635 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
638 if (name && !dm_task_set_name(dmt, name))
641 if (!dm_task_set_sector(dmt, (uint64_t) 0))
644 if (!dm_task_set_message(dmt, msg))
647 r = dm_task_run(dmt);
650 dm_task_destroy(dmt);
654 int dm_suspend_and_wipe_key(const char *name)
656 if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
659 if (!_dm_message(name, "key wipe")) {
660 _dm_simple(DM_DEVICE_RESUME, name, 1);
667 int dm_resume_and_reinstate_key(const char *name,
671 int msg_size = key_size * 2 + 10; // key set <key>
675 msg = safe_alloc(msg_size);
679 memset(msg, 0, msg_size);
680 strcpy(msg, "key set ");
681 hex_key(&msg[8], key_size, key);
683 if (!_dm_message(name, msg) ||
684 !_dm_simple(DM_DEVICE_RESUME, name, 1))
691 const char *dm_get_dir(void)