Use struct volume key thorough.
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
1 /*
2  * libdevmapper - device-mapper backend for cryptsetup
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdio.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <libdevmapper.h>
26 #include <fcntl.h>
27 #include <linux/fs.h>
28 #include <uuid/uuid.h>
29
30 #include "internal.h"
31 #include "luks.h"
32
33 #define DM_UUID_LEN             129
34 #define DM_UUID_PREFIX          "CRYPT-"
35 #define DM_UUID_PREFIX_LEN      6
36 #define DM_CRYPT_TARGET         "crypt"
37 #define RETRY_COUNT             5
38
39 /* Set if dm-crypt version was probed */
40 static int _dm_crypt_checked = 0;
41 static uint32_t _dm_crypt_flags = 0;
42
43 static int _dm_use_count = 0;
44 static struct crypt_device *_context = NULL;
45
46 /* Check if we have DM flag to instruct kernel to force wipe buffers */
47 #if !HAVE_DECL_DM_TASK_SECURE_DATA
48 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
49 #endif
50
51 /* Compatibility for old device-mapper without udev support */
52 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
53 #define CRYPT_TEMP_UDEV_FLAGS   DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
54                                 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
55                                 DM_UDEV_DISABLE_OTHER_RULES_FLAG
56 #define _dm_task_set_cookie     dm_task_set_cookie
57 #define _dm_udev_wait           dm_udev_wait
58 #else
59 #define CRYPT_TEMP_UDEV_FLAGS   0
60 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
61 static int _dm_udev_wait(uint32_t cookie) { return 0; };
62 #endif
63
64 static int _dm_use_udev(void)
65 {
66 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
67         return dm_udev_get_sync_support();
68 #else
69         return 0;
70 #endif
71 }
72
73 __attribute__((format(printf, 4, 5)))
74 static void set_dm_error(int level,
75                          const char *file __attribute__((unused)),
76                          int line __attribute__((unused)),
77                          const char *f, ...)
78 {
79         char *msg = NULL;
80         va_list va;
81
82         va_start(va, f);
83         if (vasprintf(&msg, f, va) > 0) {
84                 if (level < 4) {
85                         log_err(_context, msg);
86                         log_err(_context, "\n");
87                 } else
88                         log_dbg(msg);
89         }
90         free(msg);
91         va_end(va);
92 }
93
94 static int _dm_simple(int task, const char *name, int udev_wait);
95
96 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
97                                  unsigned crypt_min, unsigned crypt_patch)
98 {
99         unsigned dm_maj, dm_min, dm_patch;
100
101         if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
102                 dm_maj = dm_min = dm_patch = 0;
103
104         log_dbg("Detected dm-crypt version %i.%i.%i, dm-ioctl version %u.%u.%u.",
105                 crypt_maj, crypt_min, crypt_patch, dm_maj, dm_min, dm_patch);
106
107         if (crypt_maj >= 1 && crypt_min >= 2)
108                 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
109         else
110                 log_dbg("Suspend and resume disabled, no wipe key support.");
111
112         if (crypt_maj >= 1 && crypt_min >= 10)
113                 _dm_crypt_flags |= DM_LMK_SUPPORTED;
114
115         if (dm_maj >= 4 && dm_min >= 20)
116                 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
117
118         /* not perfect, 2.6.33 supports with 1.7.0 */
119         if (crypt_maj >= 1 && crypt_min >= 8)
120                 _dm_crypt_flags |= DM_PLAIN64_SUPPORTED;
121
122         /* Repeat test if dm-crypt is not present */
123         if (crypt_maj > 0)
124                 _dm_crypt_checked = 1;
125 }
126
127 static int _dm_check_versions(void)
128 {
129         struct dm_task *dmt;
130         struct dm_versions *target, *last_target;
131         char dm_version[16];
132
133         if (_dm_crypt_checked)
134                 return 1;
135
136         /* FIXME: add support to DM so it forces crypt target module load here */
137         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
138                 return 0;
139
140         if (!dm_task_run(dmt)) {
141                 dm_task_destroy(dmt);
142                 return 0;
143         }
144
145         if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
146                 dm_task_destroy(dmt);
147                 return 0;
148         }
149
150         target = dm_task_get_versions(dmt);
151         do {
152                 last_target = target;
153                 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
154                         _dm_set_crypt_compat(dm_version,
155                                              (unsigned)target->version[0],
156                                              (unsigned)target->version[1],
157                                              (unsigned)target->version[2]);
158                 }
159                 target = (struct dm_versions *)((char *) target + target->next);
160         } while (last_target != target);
161
162         dm_task_destroy(dmt);
163         return 1;
164 }
165
166 uint32_t dm_flags(void)
167 {
168         if (!_dm_crypt_checked)
169                 _dm_check_versions();
170
171         return _dm_crypt_flags;
172 }
173
174 int dm_init(struct crypt_device *context, int check_kernel)
175 {
176         if (!_dm_use_count++) {
177                 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
178                         check_kernel ? "" : " (NO kernel check requested)",
179                         _dm_use_udev() ? "en" : "dis");
180                 if (check_kernel && !_dm_check_versions()) {
181                         log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
182                         return -1;
183                 }
184                 if (getuid() || geteuid())
185                         log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
186                 dm_log_init(set_dm_error);
187                 dm_log_init_verbose(10);
188         }
189
190         // FIXME: global context is not safe
191         if (context)
192                 _context = context;
193
194         return 1;       /* unsafe memory */
195 }
196
197 void dm_exit(void)
198 {
199         if (_dm_use_count && (!--_dm_use_count)) {
200                 log_dbg("Releasing device-mapper backend.");
201                 dm_log_init_verbose(0);
202                 dm_log_init(NULL);
203                 dm_lib_release();
204                 _context = NULL;
205         }
206 }
207
208 /* Return path to DM device */
209 char *dm_device_path(const char *prefix, int major, int minor)
210 {
211         struct dm_task *dmt;
212         const char *name;
213         char path[PATH_MAX];
214
215         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
216                 return NULL;
217         if (!dm_task_set_minor(dmt, minor) ||
218             !dm_task_set_major(dmt, major) ||
219             !dm_task_run(dmt) ||
220             !(name = dm_task_get_name(dmt))) {
221                 dm_task_destroy(dmt);
222                 return NULL;
223         }
224
225         if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
226                 path[0] = '\0';
227
228         dm_task_destroy(dmt);
229
230         return strdup(path);
231 }
232
233 static void hex_key(char *hexkey, size_t key_size, const char *key)
234 {
235         unsigned i;
236
237         for(i = 0; i < key_size; i++)
238                 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
239 }
240
241 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
242                         const char *cipher, struct volume_key *vk)
243 {
244         char *params;
245         char *hexkey;
246
247         hexkey = crypt_safe_alloc(vk->keylength * 2 + 1);
248         if (!hexkey)
249                 return NULL;
250
251         hex_key(hexkey, vk->keylength, vk->key);
252
253         params = crypt_safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
254         if (!params)
255                 goto out;
256
257         sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
258                 cipher, hexkey, skip, device, offset);
259
260 out:
261         crypt_safe_free(hexkey);
262         return params;
263 }
264
265 /* DM helpers */
266 static int _dm_simple(int task, const char *name, int udev_wait)
267 {
268         int r = 0;
269         struct dm_task *dmt;
270         uint32_t cookie = 0;
271
272         if (!_dm_use_udev())
273                 udev_wait = 0;
274
275         if (!(dmt = dm_task_create(task)))
276                 return 0;
277
278         if (name && !dm_task_set_name(dmt, name))
279                 goto out;
280
281         if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
282                 goto out;
283
284         r = dm_task_run(dmt);
285
286         if (udev_wait)
287                 (void)_dm_udev_wait(cookie);
288
289       out:
290         dm_task_destroy(dmt);
291         return r;
292 }
293
294 static int _error_device(const char *name, size_t size)
295 {
296         struct dm_task *dmt;
297         int r = 0;
298
299         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
300                 return 0;
301
302         if (!dm_task_set_name(dmt, name))
303                 goto error;
304
305         if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
306                 goto error;
307
308         if (!dm_task_set_ro(dmt))
309                 goto error;
310
311         if (!dm_task_no_open_count(dmt))
312                 goto error;
313
314         if (!dm_task_run(dmt))
315                 goto error;
316
317         if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
318                 _dm_simple(DM_DEVICE_CLEAR, name, 0);
319                 goto error;
320         }
321
322         r = 1;
323
324 error:
325         dm_task_destroy(dmt);
326         return r;
327 }
328
329 int dm_remove_device(const char *name, int force, uint64_t size)
330 {
331         int r = -EINVAL;
332         int retries = force ? RETRY_COUNT : 1;
333         int error_target = 0;
334
335         if (!name || (force && !size))
336                 return -EINVAL;
337
338         do {
339                 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
340                 if (--retries && r) {
341                         log_dbg("WARNING: other process locked internal device %s, %s.",
342                                 name, retries ? "retrying remove" : "giving up");
343                         if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
344                                 debug_processes_using_device(name);
345                         sleep(1);
346                         if (force && !error_target) {
347                                 /* If force flag is set, replace device with error, read-only target.
348                                  * it should stop processes from reading it and also removed underlying
349                                  * device from mapping, so it is usable again.
350                                  * Force flag should be used only for temporary devices, which are
351                                  * intended to work inside cryptsetup only!
352                                  * Anyway, if some process try to read temporary cryptsetup device,
353                                  * it is bug - no other process should try touch it (e.g. udev).
354                                  */
355                                 _error_device(name, size);
356                                 error_target = 1;
357                         }
358                 }
359         } while (r == -EINVAL && retries);
360
361         dm_task_update_nodes();
362
363         return r;
364 }
365
366 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
367 /*
368  * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
369  * CRYPT-PLAIN-name
370  * CRYPT-LUKS1-00000000000000000000000000000000-name
371  * CRYPT-TEMP-name
372  */
373 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
374 {
375         char *ptr, uuid2[UUID_LEN] = {0};
376         uuid_t uu;
377         unsigned i = 0;
378
379         /* Remove '-' chars */
380         if (uuid && !uuid_parse(uuid, uu)) {
381                 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
382                         if (uuid[i] != '-') {
383                                 *ptr = uuid[i];
384                                 ptr++;
385                         }
386         }
387
388         i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
389                 type ?: "", type ? "-" : "",
390                 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
391                 name);
392
393         log_dbg("DM-UUID is %s", buf);
394         if (i >= buflen)
395                 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
396 }
397
398 int dm_create_device(const char *name,
399                      const char *type,
400                      struct crypt_dm_active_device *dmd,
401                      int reload)
402 {
403         struct dm_task *dmt = NULL;
404         struct dm_info dmi;
405         char *params = NULL;
406         char *error = NULL;
407         char dev_uuid[DM_UUID_LEN] = {0};
408         int r = -EINVAL;
409         uint32_t read_ahead = 0;
410         uint32_t cookie = 0;
411         uint16_t udev_flags = 0;
412
413         params = get_params(dmd->device, dmd->iv_offset, dmd->offset,
414                             dmd->cipher, dmd->vk);
415         if (!params)
416                 goto out_no_removal;
417
418         if (type && !strncmp(type, "TEMP", 4))
419                 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
420
421         /* All devices must have DM_UUID, only resize on old device is exception */
422         if (reload) {
423                 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
424                         goto out_no_removal;
425
426                 if (!dm_task_set_name(dmt, name))
427                         goto out_no_removal;
428         } else {
429                 dm_prepare_uuid(name, type, dmd->uuid, dev_uuid, sizeof(dev_uuid));
430
431                 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
432                         goto out_no_removal;
433
434                 if (!dm_task_set_name(dmt, name))
435                         goto out_no_removal;
436
437                 if (!dm_task_set_uuid(dmt, dev_uuid))
438                         goto out_no_removal;
439
440                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
441                         goto out_no_removal;
442         }
443
444         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
445                 goto out_no_removal;
446         if ((dmd->flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
447                 goto out_no_removal;
448         if (!dm_task_add_target(dmt, 0, dmd->size, DM_CRYPT_TARGET, params))
449                 goto out_no_removal;
450
451 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
452         if (device_read_ahead(dmd->device, &read_ahead) &&
453             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
454                 goto out_no_removal;
455 #endif
456
457         if (!dm_task_run(dmt))
458                 goto out_no_removal;
459
460         if (reload) {
461                 dm_task_destroy(dmt);
462                 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
463                         goto out;
464                 if (!dm_task_set_name(dmt, name))
465                         goto out;
466                 if (dmd->uuid && !dm_task_set_uuid(dmt, dev_uuid))
467                         goto out;
468                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
469                         goto out;
470                 if (!dm_task_run(dmt))
471                         goto out;
472         }
473
474         if (!dm_task_get_info(dmt, &dmi))
475                 goto out;
476
477         r = 0;
478 out:
479         if (_dm_use_udev()) {
480                 (void)_dm_udev_wait(cookie);
481                 cookie = 0;
482         }
483
484         if (r < 0 && !reload) {
485                 if (get_error())
486                         error = strdup(get_error());
487
488                 dm_remove_device(name, 0, 0);
489
490                 if (error) {
491                         set_error(error);
492                         free(error);
493                 }
494         }
495
496 out_no_removal:
497         if (cookie && _dm_use_udev())
498                 (void)_dm_udev_wait(cookie);
499
500         if (params)
501                 crypt_safe_free(params);
502         if (dmt)
503                 dm_task_destroy(dmt);
504
505         dm_task_update_nodes();
506         return r;
507 }
508
509 static int dm_status_dmi(const char *name, struct dm_info *dmi)
510 {
511         struct dm_task *dmt;
512         uint64_t start, length;
513         char *target_type, *params;
514         void *next = NULL;
515         int r = -EINVAL;
516
517         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
518                 goto out;
519
520         if (!dm_task_set_name(dmt, name))
521                 goto out;
522
523         if (!dm_task_run(dmt))
524                 goto out;
525
526         if (!dm_task_get_info(dmt, dmi))
527                 goto out;
528
529         if (!dmi->exists) {
530                 r = -ENODEV;
531                 goto out;
532         }
533
534         next = dm_get_next_target(dmt, next, &start, &length,
535                                   &target_type, &params);
536         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
537             start != 0 || next)
538                 r = -EINVAL;
539         else
540                 r = 0;
541 out:
542         if (dmt)
543                 dm_task_destroy(dmt);
544
545         return r;
546 }
547
548 int dm_status_device(const char *name)
549 {
550         int r;
551         struct dm_info dmi;
552
553         r = dm_status_dmi(name, &dmi);
554         if (r < 0)
555                 return r;
556
557         return (dmi.open_count > 0);
558 }
559
560 int dm_status_suspended(const char *name)
561 {
562         int r;
563         struct dm_info dmi;
564
565         r = dm_status_dmi(name, &dmi);
566         if (r < 0)
567                 return r;
568
569         return dmi.suspended ? 1 : 0;
570 }
571
572 int dm_query_device(const char *name, uint32_t get_flags,
573                     struct crypt_dm_active_device *dmd)
574 {
575         struct dm_task *dmt;
576         struct dm_info dmi;
577         uint64_t start, length, val64;
578         char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3];
579         const char *tmp_uuid;
580         void *next = NULL;
581         unsigned int i;
582         int r = -EINVAL;
583
584         memset(dmd, 0, sizeof(*dmd));
585
586         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
587                 goto out;
588         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
589                 goto out;
590         if (!dm_task_set_name(dmt, name))
591                 goto out;
592         r = -ENODEV;
593         if (!dm_task_run(dmt))
594                 goto out;
595
596         r = -EINVAL;
597         if (!dm_task_get_info(dmt, &dmi))
598                 goto out;
599
600         if (!dmi.exists) {
601                 r = -ENODEV;
602                 goto out;
603         }
604
605         tmp_uuid = dm_task_get_uuid(dmt);
606
607         next = dm_get_next_target(dmt, next, &start, &length,
608                                   &target_type, &params);
609         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
610             start != 0 || next)
611                 goto out;
612
613         dmd->size = length;
614
615         rcipher = strsep(&params, " ");
616         /* cipher */
617         if (get_flags & DM_ACTIVE_CIPHER)
618                 dmd->cipher = strdup(rcipher);
619
620         /* skip */
621         key_ = strsep(&params, " ");
622         if (!params)
623                 goto out;
624         val64 = strtoull(params, &params, 10);
625         if (*params != ' ')
626                 goto out;
627         params++;
628
629         dmd->iv_offset = val64;
630
631         /* device */
632         rdevice = strsep(&params, " ");
633         if (get_flags & DM_ACTIVE_DEVICE)
634                 dmd->device = crypt_lookup_dev(rdevice);
635
636         /*offset */
637         if (!params)
638                 goto out;
639         val64 = strtoull(params, &params, 10);
640         if (*params)
641                 goto out;
642         dmd->offset = val64;
643
644         if (get_flags & DM_ACTIVE_KEY) {
645                 dmd->vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
646                 if (!dmd->vk) {
647                         r = -ENOMEM;
648                         goto out;
649                 }
650
651                 buffer[2] = '\0';
652                 for(i = 0; i < dmd->vk->keylength; i++) {
653                         memcpy(buffer, &key_[i * 2], 2);
654                         dmd->vk->key[i] = strtoul(buffer, &endp, 16);
655                         if (endp != &buffer[2]) {
656                                 crypt_free_volume_key(dmd->vk);
657                                 dmd->vk = NULL;
658                                 r = -EINVAL;
659                                 goto out;
660                         }
661                 }
662         }
663         memset(key_, 0, strlen(key_));
664
665         if (dmi.read_only)
666                 dmd->flags |= CRYPT_ACTIVATE_READONLY;
667
668         if (!tmp_uuid)
669                 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
670         else if (get_flags & DM_ACTIVE_UUID) {
671                 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
672                         dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
673         }
674
675         r = (dmi.open_count > 0);
676 out:
677         if (dmt)
678                 dm_task_destroy(dmt);
679
680         return r;
681 }
682
683 static int _dm_message(const char *name, const char *msg)
684 {
685         int r = 0;
686         struct dm_task *dmt;
687
688         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
689                 return 0;
690
691         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
692                 goto out;
693
694         if (name && !dm_task_set_name(dmt, name))
695                 goto out;
696
697         if (!dm_task_set_sector(dmt, (uint64_t) 0))
698                 goto out;
699
700         if (!dm_task_set_message(dmt, msg))
701                 goto out;
702
703         r = dm_task_run(dmt);
704
705       out:
706         dm_task_destroy(dmt);
707         return r;
708 }
709
710 int dm_suspend_and_wipe_key(const char *name)
711 {
712         if (!_dm_check_versions())
713                 return -ENOTSUP;
714
715         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
716                 return -ENOTSUP;
717
718         if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
719                 return -EINVAL;
720
721         if (!_dm_message(name, "key wipe")) {
722                 _dm_simple(DM_DEVICE_RESUME, name, 1);
723                 return -EINVAL;
724         }
725
726         return 0;
727 }
728
729 int dm_resume_and_reinstate_key(const char *name,
730                                 size_t key_size,
731                                 const char *key)
732 {
733         int msg_size = key_size * 2 + 10; // key set <key>
734         char *msg;
735         int r = 0;
736
737         if (!_dm_check_versions())
738                 return -ENOTSUP;
739
740         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
741                 return -ENOTSUP;
742
743         msg = crypt_safe_alloc(msg_size);
744         if (!msg)
745                 return -ENOMEM;
746
747         memset(msg, 0, msg_size);
748         strcpy(msg, "key set ");
749         hex_key(&msg[8], key_size, key);
750
751         if (!_dm_message(name, msg) ||
752             !_dm_simple(DM_DEVICE_RESUME, name, 1))
753                 r = -EINVAL;
754
755         crypt_safe_free(msg);
756         return r;
757 }
758
759 const char *dm_get_dir(void)
760 {
761         return dm_dir();
762 }
763
764 int dm_is_dm_device(int major, int minor)
765 {
766         return dm_is_dm_major((uint32_t)major);
767 }
768
769 int dm_is_dm_kernel_name(const char *name)
770 {
771         return strncmp(name, "dm-", 3) ? 0 : 1;
772 }
773
774 int dm_check_segment(const char *name, uint64_t offset, uint64_t size)
775 {
776         struct crypt_dm_active_device dmd;
777         int r;
778
779         log_dbg("Checking segments for device %s.", name);
780
781         r = dm_query_device(name, 0, &dmd);
782         if (r < 0)
783                 return r;
784
785         if (offset >= (dmd.offset + dmd.size) || (offset + size) <= dmd.offset)
786                 r = 0;
787         else
788                 r = -EBUSY;
789
790         log_dbg("seg: %" PRIu64 " - %" PRIu64 ", new %" PRIu64 " - %" PRIu64 "%s",
791                dmd.offset, dmd.offset + dmd.size, offset, offset + size,
792                r ? " (overlapping)" : " (ok)");
793
794         return r;
795 }