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))
49 dm_log_init(set_dm_error);
50 dm_log_init_verbose(10);
56 return 1; /* unsafe memory */
61 if (_dm_use_count && (!--_dm_use_count)) {
62 log_dbg("Releasing device-mapper backend.");
63 dm_log_init_verbose(0);
70 static char *__lookup_dev(char *path, dev_t dev)
79 path[PATH_MAX - 1] = '\0';
80 ptr = path + strlen(path);
83 space = PATH_MAX - (ptr - path);
89 while((entry = readdir(dir))) {
90 if (entry->d_name[0] == '.' &&
91 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
92 entry->d_name[2] == '\0')))
95 strncpy(ptr, entry->d_name, space);
96 if (lstat(path, &st) < 0)
99 if (S_ISDIR(st.st_mode)) {
100 result = __lookup_dev(path, dev);
103 } else if (S_ISBLK(st.st_mode)) {
104 if (st.st_rdev == dev) {
105 result = strdup(path);
116 static char *lookup_dev(const char *dev)
118 uint32_t major, minor;
119 char buf[PATH_MAX + 1];
121 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
124 strncpy(buf, DEVICE_DIR, PATH_MAX);
125 buf[PATH_MAX] = '\0';
127 return __lookup_dev(buf, makedev(major, minor));
130 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
133 long read_ahead_long;
135 if ((fd = open(dev, O_RDONLY)) < 0)
138 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
142 *read_ahead = (uint32_t) read_ahead_long;
147 static void hex_key(char *hexkey, size_t key_size, const char *key)
151 for(i = 0; i < key_size; i++)
152 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
155 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
156 const char *cipher, size_t key_size, const char *key)
161 hexkey = safe_alloc(key_size * 2 + 1);
165 hex_key(hexkey, key_size, key);
167 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
171 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
172 cipher, hexkey, skip, device, offset);
180 static int _dm_simple(int task, const char *name)
185 if (!(dmt = dm_task_create(task)))
188 if (name && !dm_task_set_name(dmt, name))
191 r = dm_task_run(dmt);
194 dm_task_destroy(dmt);
198 static int _error_device(const char *name, size_t size)
203 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
206 if (!dm_task_set_name(dmt, name))
209 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
212 if (!dm_task_set_ro(dmt))
215 if (!dm_task_no_open_count(dmt))
218 if (!dm_task_run(dmt))
221 if (!_dm_simple(DM_DEVICE_RESUME, name)) {
222 _dm_simple(DM_DEVICE_CLEAR, name);
229 dm_task_destroy(dmt);
233 int dm_remove_device(const char *name, int force, uint64_t size)
236 int retries = force ? RETRY_COUNT : 1;
238 if (!name || (force && !size))
241 /* If force flag is set, replace device with error, read-only target.
242 * it should stop processes from reading it and also removed underlying
243 * device from mapping, so it is usable again.
244 * Force flag should be used only for temporary devices, which are
245 * intended to work inside cryptsetup only!
246 * Anyway, if some process try to read temporary cryptsetup device,
247 * it is bug - no other process should try touch it (e.g. udev).
250 _error_device(name, size);
251 retries = RETRY_COUNT;
255 r = _dm_simple(DM_DEVICE_REMOVE, name) ? 0 : -EINVAL;
256 if (--retries && r) {
257 log_dbg("WARNING: other process locked internal device %s, %s.",
258 name, retries ? "retrying remove" : "giving up");
261 } while (r == -EINVAL && retries);
263 dm_task_update_nodes();
268 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
270 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
272 * CRYPT-LUKS1-00000000000000000000000000000000-name
275 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
277 char *ptr, uuid2[UUID_LEN] = {0};
281 /* Remove '-' chars */
282 if (uuid && !uuid_parse(uuid, uu)) {
283 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
284 if (uuid[i] != '-') {
290 i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
291 type ?: "", type ? "-" : "",
292 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
295 log_dbg("DM-UUID is %s", buf);
297 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
300 int dm_create_device(const char *name,
313 struct dm_task *dmt = NULL;
314 struct dm_task *dmt_query = NULL;
318 char dev_uuid[DM_UUID_LEN] = {0};
320 uint32_t read_ahead = 0;
322 params = get_params(device, skip, offset, cipher, key_size, key);
326 /* All devices must have DM_UUID, only resize on old device is exception */
328 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
331 if (!dm_task_set_name(dmt, name))
334 dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
336 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
339 if (!dm_task_set_name(dmt, name))
342 if (!dm_task_set_uuid(dmt, dev_uuid))
347 if (read_only && !dm_task_set_ro(dmt))
349 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
352 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
353 if (_dev_read_ahead(device, &read_ahead) &&
354 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
358 if (!dm_task_run(dmt))
362 dm_task_destroy(dmt);
363 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
365 if (!dm_task_set_name(dmt, name))
367 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
369 if (!dm_task_run(dmt))
373 if (!dm_task_get_info(dmt, &dmi))
378 if (r < 0 && !reload) {
380 error = strdup(get_error());
382 dm_remove_device(name, 0, 0);
394 dm_task_destroy(dmt);
396 dm_task_destroy(dmt_query);
397 dm_task_update_nodes();
401 int dm_status_device(const char *name)
405 uint64_t start, length;
406 char *target_type, *params;
410 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
413 if (!dm_task_set_name(dmt, name)) {
418 if (!dm_task_run(dmt)) {
423 if (!dm_task_get_info(dmt, &dmi)) {
433 next = dm_get_next_target(dmt, next, &start, &length,
434 &target_type, ¶ms);
435 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
439 r = (dmi.open_count > 0);
442 dm_task_destroy(dmt);
447 int dm_query_device(const char *name,
461 uint64_t start, length, val64;
462 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid;
466 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
468 if (!dm_task_set_name(dmt, name))
471 if (!dm_task_run(dmt))
475 if (!dm_task_get_info(dmt, &dmi))
483 next = dm_get_next_target(dmt, next, &start, &length,
484 &target_type, ¶ms);
485 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
492 rcipher = strsep(¶ms, " ");
495 *cipher = strdup(rcipher);
498 key_ = strsep(¶ms, " ");
501 val64 = strtoull(params, ¶ms, 10);
509 rdevice = strsep(¶ms, " ");
511 *device = lookup_dev(rdevice);
516 val64 = strtoull(params, ¶ms, 10);
524 *key_size = strlen(key_) / 2;
527 if (key_size && key) {
528 *key = safe_alloc(*key_size);
535 for(i = 0; i < *key_size; i++) {
536 memcpy(buffer, &key_[i * 2], 2);
537 (*key)[i] = strtoul(buffer, &endp, 16);
538 if (endp != &buffer[2]) {
545 memset(key_, 0, strlen(key_));
548 *read_only = dmi.read_only;
551 *suspended = dmi.suspended;
553 if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) &&
554 !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
555 *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
557 r = (dmi.open_count > 0);
560 dm_task_destroy(dmt);
565 static int _dm_message(const char *name, const char *msg)
570 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
573 if (name && !dm_task_set_name(dmt, name))
576 if (!dm_task_set_sector(dmt, (uint64_t) 0))
579 if (!dm_task_set_message(dmt, msg))
582 r = dm_task_run(dmt);
585 dm_task_destroy(dmt);
589 int dm_suspend_and_wipe_key(const char *name)
591 if (!_dm_simple(DM_DEVICE_SUSPEND, name))
594 if (!_dm_message(name, "key wipe")) {
595 _dm_simple(DM_DEVICE_RESUME, name);
602 int dm_resume_and_reinstate_key(const char *name,
606 int msg_size = key_size * 2 + 10; // key set <key>
610 msg = safe_alloc(msg_size);
614 memset(msg, 0, msg_size);
615 strcpy(msg, "key set ");
616 hex_key(&msg[8], key_size, key);
618 if (!_dm_message(name, msg) ||
619 !_dm_simple(DM_DEVICE_RESUME, name))
626 const char *dm_get_dir(void)