Allow empty cipher (cipher_null).
[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-2012, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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         if (crypt_maj >= 1 && crypt_min >= 11)
123                 _dm_crypt_flags |= DM_DISCARDS_SUPPORTED;
124
125         /* Repeat test if dm-crypt is not present */
126         if (crypt_maj > 0)
127                 _dm_crypt_checked = 1;
128 }
129
130 static int _dm_check_versions(void)
131 {
132         struct dm_task *dmt;
133         struct dm_versions *target, *last_target;
134         char dm_version[16];
135
136         if (_dm_crypt_checked)
137                 return 1;
138
139         /* FIXME: add support to DM so it forces crypt target module load here */
140         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
141                 return 0;
142
143         if (!dm_task_run(dmt)) {
144                 dm_task_destroy(dmt);
145                 return 0;
146         }
147
148         if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
149                 dm_task_destroy(dmt);
150                 return 0;
151         }
152
153         target = dm_task_get_versions(dmt);
154         do {
155                 last_target = target;
156                 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
157                         _dm_set_crypt_compat(dm_version,
158                                              (unsigned)target->version[0],
159                                              (unsigned)target->version[1],
160                                              (unsigned)target->version[2]);
161                 }
162                 target = (struct dm_versions *)((char *) target + target->next);
163         } while (last_target != target);
164
165         dm_task_destroy(dmt);
166         return 1;
167 }
168
169 uint32_t dm_flags(void)
170 {
171         if (!_dm_crypt_checked)
172                 _dm_check_versions();
173
174         return _dm_crypt_flags;
175 }
176
177 int dm_init(struct crypt_device *context, int check_kernel)
178 {
179         if (!_dm_use_count++) {
180                 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
181                         check_kernel ? "" : " (NO kernel check requested)",
182                         _dm_use_udev() ? "en" : "dis");
183                 if (check_kernel && !_dm_check_versions()) {
184                         log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
185                         return -1;
186                 }
187                 if (getuid() || geteuid())
188                         log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
189                 dm_log_init(set_dm_error);
190                 dm_log_init_verbose(10);
191         }
192
193         // FIXME: global context is not safe
194         if (context)
195                 _context = context;
196
197         return 1;       /* unsafe memory */
198 }
199
200 void dm_exit(void)
201 {
202         if (_dm_use_count && (!--_dm_use_count)) {
203                 log_dbg("Releasing device-mapper backend.");
204                 dm_log_init_verbose(0);
205                 dm_log_init(NULL);
206                 dm_lib_release();
207                 _context = NULL;
208         }
209 }
210
211 /* Return path to DM device */
212 char *dm_device_path(const char *prefix, int major, int minor)
213 {
214         struct dm_task *dmt;
215         const char *name;
216         char path[PATH_MAX];
217
218         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
219                 return NULL;
220         if (!dm_task_set_minor(dmt, minor) ||
221             !dm_task_set_major(dmt, major) ||
222             !dm_task_run(dmt) ||
223             !(name = dm_task_get_name(dmt))) {
224                 dm_task_destroy(dmt);
225                 return NULL;
226         }
227
228         if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
229                 path[0] = '\0';
230
231         dm_task_destroy(dmt);
232
233         return strdup(path);
234 }
235
236 static void hex_key(char *hexkey, size_t key_size, const char *key)
237 {
238         unsigned i;
239
240         for(i = 0; i < key_size; i++)
241                 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
242 }
243
244 static char *get_params(struct crypt_dm_active_device *dmd)
245 {
246         int r, max_size, null_cipher = 0;
247         char *params, *hexkey;
248         const char *features = "";
249
250         if (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) {
251                 if (dm_flags() & DM_DISCARDS_SUPPORTED) {
252                         features = " 1 allow_discards";
253                         log_dbg("Discard/TRIM is allowed.");
254                 } else
255                         log_dbg("Discard/TRIM is not supported by the kernel.");
256         }
257
258         if (!strncmp(dmd->cipher, "cipher_null-", 12))
259                 null_cipher = 1;
260
261         hexkey = crypt_safe_alloc(null_cipher ? 2 : (dmd->vk->keylength * 2 + 1));
262         if (!hexkey)
263                 return NULL;
264
265         if (null_cipher)
266                 strncpy(hexkey, "-", 2);
267         else
268                 hex_key(hexkey, dmd->vk->keylength, dmd->vk->key);
269
270         max_size = strlen(hexkey) + strlen(dmd->cipher) +
271                    strlen(dmd->device) + strlen(features) + 64;
272         params = crypt_safe_alloc(max_size);
273         if (!params)
274                 goto out;
275
276         r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
277                      dmd->cipher, hexkey, dmd->iv_offset, dmd->device,
278                      dmd->offset, features);
279         if (r < 0 || r >= max_size) {
280                 crypt_safe_free(params);
281                 params = NULL;
282         }
283 out:
284         crypt_safe_free(hexkey);
285         return params;
286 }
287
288 /* DM helpers */
289 static int _dm_simple(int task, const char *name, int udev_wait)
290 {
291         int r = 0;
292         struct dm_task *dmt;
293         uint32_t cookie = 0;
294
295         if (!_dm_use_udev())
296                 udev_wait = 0;
297
298         if (!(dmt = dm_task_create(task)))
299                 return 0;
300
301         if (name && !dm_task_set_name(dmt, name))
302                 goto out;
303
304 #if HAVE_DECL_DM_TASK_RETRY_REMOVE
305         /* Used only in DM_DEVICE_REMOVE */
306         if (name && !dm_task_retry_remove(dmt))
307                 goto out;
308 #endif
309         if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
310                 goto out;
311
312         r = dm_task_run(dmt);
313
314         if (udev_wait)
315                 (void)_dm_udev_wait(cookie);
316
317       out:
318         dm_task_destroy(dmt);
319         return r;
320 }
321
322 static int _error_device(const char *name, size_t size)
323 {
324         struct dm_task *dmt;
325         int r = 0;
326
327         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
328                 return 0;
329
330         if (!dm_task_set_name(dmt, name))
331                 goto error;
332
333         if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
334                 goto error;
335
336         if (!dm_task_set_ro(dmt))
337                 goto error;
338
339         if (!dm_task_no_open_count(dmt))
340                 goto error;
341
342         if (!dm_task_run(dmt))
343                 goto error;
344
345         if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
346                 _dm_simple(DM_DEVICE_CLEAR, name, 0);
347                 goto error;
348         }
349
350         r = 1;
351
352 error:
353         dm_task_destroy(dmt);
354         return r;
355 }
356
357 int dm_remove_device(const char *name, int force, uint64_t size)
358 {
359         int r = -EINVAL;
360         int retries = force ? RETRY_COUNT : 1;
361         int error_target = 0;
362
363         if (!name || (force && !size))
364                 return -EINVAL;
365
366         do {
367                 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
368                 if (--retries && r) {
369                         log_dbg("WARNING: other process locked internal device %s, %s.",
370                                 name, retries ? "retrying remove" : "giving up");
371                         if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
372                                 debug_processes_using_device(name);
373                         sleep(1);
374                         if (force && !error_target) {
375                                 /* If force flag is set, replace device with error, read-only target.
376                                  * it should stop processes from reading it and also removed underlying
377                                  * device from mapping, so it is usable again.
378                                  * Force flag should be used only for temporary devices, which are
379                                  * intended to work inside cryptsetup only!
380                                  * Anyway, if some process try to read temporary cryptsetup device,
381                                  * it is bug - no other process should try touch it (e.g. udev).
382                                  */
383                                 _error_device(name, size);
384                                 error_target = 1;
385                         }
386                 }
387         } while (r == -EINVAL && retries);
388
389         dm_task_update_nodes();
390
391         return r;
392 }
393
394 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
395 /*
396  * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
397  * CRYPT-PLAIN-name
398  * CRYPT-LUKS1-00000000000000000000000000000000-name
399  * CRYPT-TEMP-name
400  */
401 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
402 {
403         char *ptr, uuid2[UUID_LEN] = {0};
404         uuid_t uu;
405         unsigned i = 0;
406
407         /* Remove '-' chars */
408         if (uuid && !uuid_parse(uuid, uu)) {
409                 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
410                         if (uuid[i] != '-') {
411                                 *ptr = uuid[i];
412                                 ptr++;
413                         }
414         }
415
416         i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
417                 type ?: "", type ? "-" : "",
418                 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
419                 name);
420
421         log_dbg("DM-UUID is %s", buf);
422         if (i >= buflen)
423                 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
424 }
425
426 int dm_create_device(const char *name,
427                      const char *type,
428                      struct crypt_dm_active_device *dmd,
429                      int reload)
430 {
431         struct dm_task *dmt = NULL;
432         struct dm_info dmi;
433         char *params = NULL;
434         char dev_uuid[DM_UUID_LEN] = {0};
435         int r = -EINVAL;
436         uint32_t read_ahead = 0;
437         uint32_t cookie = 0;
438         uint16_t udev_flags = 0;
439
440         params = get_params(dmd);
441         if (!params)
442                 goto out_no_removal;
443
444         if (dmd->flags & CRYPT_ACTIVATE_PRIVATE)
445                 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
446
447         /* All devices must have DM_UUID, only resize on old device is exception */
448         if (reload) {
449                 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
450                         goto out_no_removal;
451
452                 if (!dm_task_set_name(dmt, name))
453                         goto out_no_removal;
454         } else {
455                 dm_prepare_uuid(name, type, dmd->uuid, dev_uuid, sizeof(dev_uuid));
456
457                 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
458                         goto out_no_removal;
459
460                 if (!dm_task_set_name(dmt, name))
461                         goto out_no_removal;
462
463                 if (!dm_task_set_uuid(dmt, dev_uuid))
464                         goto out_no_removal;
465
466                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
467                         goto out_no_removal;
468         }
469
470         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
471                 goto out_no_removal;
472         if ((dmd->flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
473                 goto out_no_removal;
474         if (!dm_task_add_target(dmt, 0, dmd->size, DM_CRYPT_TARGET, params))
475                 goto out_no_removal;
476
477 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
478         if (device_read_ahead(dmd->device, &read_ahead) &&
479             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
480                 goto out_no_removal;
481 #endif
482
483         if (!dm_task_run(dmt))
484                 goto out_no_removal;
485
486         if (reload) {
487                 dm_task_destroy(dmt);
488                 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
489                         goto out;
490                 if (!dm_task_set_name(dmt, name))
491                         goto out;
492                 if (dmd->uuid && !dm_task_set_uuid(dmt, dev_uuid))
493                         goto out;
494                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
495                         goto out;
496                 if (!dm_task_run(dmt))
497                         goto out;
498         }
499
500         if (!dm_task_get_info(dmt, &dmi))
501                 goto out;
502
503         r = 0;
504 out:
505         if (_dm_use_udev()) {
506                 (void)_dm_udev_wait(cookie);
507                 cookie = 0;
508         }
509
510         if (r < 0 && !reload)
511                 dm_remove_device(name, 0, 0);
512
513 out_no_removal:
514         if (cookie && _dm_use_udev())
515                 (void)_dm_udev_wait(cookie);
516
517         if (params)
518                 crypt_safe_free(params);
519         if (dmt)
520                 dm_task_destroy(dmt);
521
522         dm_task_update_nodes();
523         return r;
524 }
525
526 static int dm_status_dmi(const char *name, struct dm_info *dmi)
527 {
528         struct dm_task *dmt;
529         uint64_t start, length;
530         char *target_type, *params;
531         void *next = NULL;
532         int r = -EINVAL;
533
534         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
535                 goto out;
536
537         if (!dm_task_set_name(dmt, name))
538                 goto out;
539
540         if (!dm_task_run(dmt))
541                 goto out;
542
543         if (!dm_task_get_info(dmt, dmi))
544                 goto out;
545
546         if (!dmi->exists) {
547                 r = -ENODEV;
548                 goto out;
549         }
550
551         next = dm_get_next_target(dmt, next, &start, &length,
552                                   &target_type, &params);
553         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
554             start != 0 || next)
555                 r = -EINVAL;
556         else
557                 r = 0;
558 out:
559         if (dmt)
560                 dm_task_destroy(dmt);
561
562         return r;
563 }
564
565 int dm_status_device(const char *name)
566 {
567         int r;
568         struct dm_info dmi;
569
570         r = dm_status_dmi(name, &dmi);
571         if (r < 0)
572                 return r;
573
574         return (dmi.open_count > 0);
575 }
576
577 int dm_status_suspended(const char *name)
578 {
579         int r;
580         struct dm_info dmi;
581
582         r = dm_status_dmi(name, &dmi);
583         if (r < 0)
584                 return r;
585
586         return dmi.suspended ? 1 : 0;
587 }
588
589 int dm_query_device(const char *name, uint32_t get_flags,
590                     struct crypt_dm_active_device *dmd)
591 {
592         struct dm_task *dmt;
593         struct dm_info dmi;
594         uint64_t start, length, val64;
595         char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
596         const char *tmp_uuid;
597         void *next = NULL;
598         unsigned int i;
599         int r = -EINVAL;
600
601         memset(dmd, 0, sizeof(*dmd));
602
603         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
604                 goto out;
605         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
606                 goto out;
607         if (!dm_task_set_name(dmt, name))
608                 goto out;
609         r = -ENODEV;
610         if (!dm_task_run(dmt))
611                 goto out;
612
613         r = -EINVAL;
614         if (!dm_task_get_info(dmt, &dmi))
615                 goto out;
616
617         if (!dmi.exists) {
618                 r = -ENODEV;
619                 goto out;
620         }
621
622         tmp_uuid = dm_task_get_uuid(dmt);
623
624         next = dm_get_next_target(dmt, next, &start, &length,
625                                   &target_type, &params);
626         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
627             start != 0 || next)
628                 goto out;
629
630         dmd->size = length;
631
632         rcipher = strsep(&params, " ");
633         /* cipher */
634         if (get_flags & DM_ACTIVE_CIPHER)
635                 dmd->cipher = strdup(rcipher);
636
637         /* skip */
638         key_ = strsep(&params, " ");
639         if (!params)
640                 goto out;
641         val64 = strtoull(params, &params, 10);
642         if (*params != ' ')
643                 goto out;
644         params++;
645
646         dmd->iv_offset = val64;
647
648         /* device */
649         rdevice = strsep(&params, " ");
650         if (get_flags & DM_ACTIVE_DEVICE)
651                 dmd->device = crypt_lookup_dev(rdevice);
652
653         /*offset */
654         if (!params)
655                 goto out;
656         val64 = strtoull(params, &params, 10);
657         dmd->offset = val64;
658
659         /* Features section, available since crypt target version 1.11 */
660         if (*params) {
661                 if (*params != ' ')
662                         goto out;
663                 params++;
664
665                 /* Number of arguments */
666                 val64 = strtoull(params, &params, 10);
667                 if (*params != ' ')
668                         goto out;
669                 params++;
670
671                 for (i = 0; i < val64; i++) {
672                         if (!params)
673                                 goto out;
674                         arg = strsep(&params, " ");
675                         if (!strcasecmp(arg, "allow_discards"))
676                                 dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
677                         else /* unknown option */
678                                 goto out;
679                 }
680
681                 /* All parameters shold be processed */
682                 if (params)
683                         goto out;
684         }
685
686         /* Never allow to return empty key */
687         if ((get_flags & DM_ACTIVE_KEY) && dmi.suspended) {
688                 log_dbg("Cannot read volume key while suspended.");
689                 r = -EINVAL;
690                 goto out;
691         }
692
693         if (get_flags & DM_ACTIVE_KEYSIZE) {
694                 dmd->vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
695                 if (!dmd->vk) {
696                         r = -ENOMEM;
697                         goto out;
698                 }
699
700                 if (get_flags & DM_ACTIVE_KEY) {
701                         buffer[2] = '\0';
702                         for(i = 0; i < dmd->vk->keylength; i++) {
703                                 memcpy(buffer, &key_[i * 2], 2);
704                                 dmd->vk->key[i] = strtoul(buffer, &endp, 16);
705                                 if (endp != &buffer[2]) {
706                                         crypt_free_volume_key(dmd->vk);
707                                         dmd->vk = NULL;
708                                         r = -EINVAL;
709                                         goto out;
710                                 }
711                         }
712                 }
713         }
714         memset(key_, 0, strlen(key_));
715
716         if (dmi.read_only)
717                 dmd->flags |= CRYPT_ACTIVATE_READONLY;
718
719         if (!tmp_uuid)
720                 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
721         else if (get_flags & DM_ACTIVE_UUID) {
722                 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
723                         dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
724         }
725
726         r = (dmi.open_count > 0);
727 out:
728         if (dmt)
729                 dm_task_destroy(dmt);
730
731         return r;
732 }
733
734 static int _dm_message(const char *name, const char *msg)
735 {
736         int r = 0;
737         struct dm_task *dmt;
738
739         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
740                 return 0;
741
742         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
743                 goto out;
744
745         if (name && !dm_task_set_name(dmt, name))
746                 goto out;
747
748         if (!dm_task_set_sector(dmt, (uint64_t) 0))
749                 goto out;
750
751         if (!dm_task_set_message(dmt, msg))
752                 goto out;
753
754         r = dm_task_run(dmt);
755
756       out:
757         dm_task_destroy(dmt);
758         return r;
759 }
760
761 int dm_suspend_and_wipe_key(const char *name)
762 {
763         if (!_dm_check_versions())
764                 return -ENOTSUP;
765
766         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
767                 return -ENOTSUP;
768
769         if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
770                 return -EINVAL;
771
772         if (!_dm_message(name, "key wipe")) {
773                 _dm_simple(DM_DEVICE_RESUME, name, 1);
774                 return -EINVAL;
775         }
776
777         return 0;
778 }
779
780 int dm_resume_and_reinstate_key(const char *name,
781                                 size_t key_size,
782                                 const char *key)
783 {
784         int msg_size = key_size * 2 + 10; // key set <key>
785         char *msg;
786         int r = 0;
787
788         if (!_dm_check_versions())
789                 return -ENOTSUP;
790
791         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
792                 return -ENOTSUP;
793
794         msg = crypt_safe_alloc(msg_size);
795         if (!msg)
796                 return -ENOMEM;
797
798         memset(msg, 0, msg_size);
799         strcpy(msg, "key set ");
800         hex_key(&msg[8], key_size, key);
801
802         if (!_dm_message(name, msg) ||
803             !_dm_simple(DM_DEVICE_RESUME, name, 1))
804                 r = -EINVAL;
805
806         crypt_safe_free(msg);
807         return r;
808 }
809
810 const char *dm_get_dir(void)
811 {
812         return dm_dir();
813 }
814
815 int dm_is_dm_device(int major, int minor)
816 {
817         return dm_is_dm_major((uint32_t)major);
818 }
819
820 int dm_is_dm_kernel_name(const char *name)
821 {
822         return strncmp(name, "dm-", 3) ? 0 : 1;
823 }
824
825 int dm_check_segment(const char *name, uint64_t offset, uint64_t size)
826 {
827         struct crypt_dm_active_device dmd;
828         int r;
829
830         log_dbg("Checking segments for device %s.", name);
831
832         r = dm_query_device(name, 0, &dmd);
833         if (r < 0)
834                 return r;
835
836         if (offset >= (dmd.offset + dmd.size) || (offset + size) <= dmd.offset)
837                 r = 0;
838         else
839                 r = -EBUSY;
840
841         log_dbg("seg: %" PRIu64 " - %" PRIu64 ", new %" PRIu64 " - %" PRIu64 "%s",
842                dmd.offset, dmd.offset + dmd.size, offset, offset + size,
843                r ? " (overlapping)" : " (ok)");
844
845         return r;
846 }