Fixed the build error for riscv64 arch using gcc 13
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
1 /*
2  * libdevmapper - device-mapper backend for cryptsetup
3  *
4  * Copyright (C) 2004 Jana Saout <jana@saout.de>
5  * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2021 Milan Broz
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <ctype.h>
27 #include <dirent.h>
28 #include <errno.h>
29 #include <libdevmapper.h>
30 #include <linux/fs.h>
31 #include <uuid/uuid.h>
32 #include <sys/stat.h>
33 #ifdef HAVE_SYS_SYSMACROS_H
34 # include <sys/sysmacros.h>     /* for major, minor */
35 #endif
36 #include <assert.h>
37 #include "internal.h"
38
39 #define DM_UUID_LEN             129
40 #define DM_BY_ID_PREFIX         "dm-uuid-"
41 #define DM_BY_ID_PREFIX_LEN     8
42 #define DM_UUID_PREFIX          "CRYPT-"
43 #define DM_UUID_PREFIX_LEN      6
44 #define DM_CRYPT_TARGET         "crypt"
45 #define DM_VERITY_TARGET        "verity"
46 #define DM_INTEGRITY_TARGET     "integrity"
47 #define DM_LINEAR_TARGET        "linear"
48 #define DM_ERROR_TARGET         "error"
49 #define DM_ZERO_TARGET          "zero"
50 #define RETRY_COUNT             5
51
52 /* Set if DM target versions were probed */
53 static bool _dm_ioctl_checked = false;
54 static bool _dm_crypt_checked = false;
55 static bool _dm_verity_checked = false;
56 static bool _dm_integrity_checked = false;
57
58 static int _quiet_log = 0;
59 static uint32_t _dm_flags = 0;
60
61 static struct crypt_device *_context = NULL;
62 static int _dm_use_count = 0;
63
64 /* Check if we have DM flag to instruct kernel to force wipe buffers */
65 #if !HAVE_DECL_DM_TASK_SECURE_DATA
66 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
67 #endif
68
69 /* Compatibility for old device-mapper without udev support */
70 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
71 #define CRYPT_TEMP_UDEV_FLAGS   DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
72                                 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
73                                 DM_UDEV_DISABLE_OTHER_RULES_FLAG
74 #define _dm_task_set_cookie     dm_task_set_cookie
75 #define _dm_udev_wait           dm_udev_wait
76 #else
77 #define CRYPT_TEMP_UDEV_FLAGS   0
78 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
79 static int _dm_udev_wait(uint32_t cookie) { return 0; };
80 #endif
81
82 static int _dm_use_udev(void)
83 {
84 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
85         return dm_udev_get_sync_support();
86 #else
87         return 0;
88 #endif
89 }
90
91 __attribute__((format(printf, 4, 5)))
92 static void set_dm_error(int level,
93                          const char *file __attribute__((unused)),
94                          int line __attribute__((unused)),
95                          const char *f, ...)
96 {
97         char *msg = NULL;
98         va_list va;
99
100         va_start(va, f);
101         if (vasprintf(&msg, f, va) > 0) {
102                 if (level < 4 && !_quiet_log) {
103                         log_err(_context, "%s", msg);
104                 } else {
105                         /* We do not use DM visual stack backtrace here */
106                         if (strncmp(msg, "<backtrace>", 11))
107                                 log_dbg(_context, "%s", msg);
108                 }
109         }
110         free(msg);
111         va_end(va);
112 }
113
114 static int _dm_satisfies_version(unsigned target_maj, unsigned target_min, unsigned target_patch,
115                                  unsigned actual_maj, unsigned actual_min, unsigned actual_patch)
116 {
117         if (actual_maj > target_maj)
118                 return 1;
119
120         if (actual_maj == target_maj && actual_min > target_min)
121                 return 1;
122
123         if (actual_maj == target_maj && actual_min == target_min && actual_patch >= target_patch)
124                 return 1;
125
126         return 0;
127 }
128
129 static void _dm_set_crypt_compat(struct crypt_device *cd,
130                                  unsigned crypt_maj,
131                                  unsigned crypt_min,
132                                  unsigned crypt_patch)
133 {
134         if (_dm_crypt_checked || crypt_maj == 0)
135                 return;
136
137         log_dbg(cd, "Detected dm-crypt version %i.%i.%i.",
138                 crypt_maj, crypt_min, crypt_patch);
139
140         if (_dm_satisfies_version(1, 2, 0, crypt_maj, crypt_min, crypt_patch))
141                 _dm_flags |= DM_KEY_WIPE_SUPPORTED;
142         else
143                 log_dbg(cd, "Suspend and resume disabled, no wipe key support.");
144
145         if (_dm_satisfies_version(1, 10, 0, crypt_maj, crypt_min, crypt_patch))
146                 _dm_flags |= DM_LMK_SUPPORTED;
147
148         /* not perfect, 2.6.33 supports with 1.7.0 */
149         if (_dm_satisfies_version(1, 8, 0, crypt_maj, crypt_min, crypt_patch))
150                 _dm_flags |= DM_PLAIN64_SUPPORTED;
151
152         if (_dm_satisfies_version(1, 11, 0, crypt_maj, crypt_min, crypt_patch))
153                 _dm_flags |= DM_DISCARDS_SUPPORTED;
154
155         if (_dm_satisfies_version(1, 13, 0, crypt_maj, crypt_min, crypt_patch))
156                 _dm_flags |= DM_TCW_SUPPORTED;
157
158         if (_dm_satisfies_version(1, 14, 0, crypt_maj, crypt_min, crypt_patch)) {
159                 _dm_flags |= DM_SAME_CPU_CRYPT_SUPPORTED;
160                 _dm_flags |= DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED;
161         }
162
163         if (_dm_satisfies_version(1, 18, 1, crypt_maj, crypt_min, crypt_patch))
164                 _dm_flags |= DM_KERNEL_KEYRING_SUPPORTED;
165
166         if (_dm_satisfies_version(1, 17, 0, crypt_maj, crypt_min, crypt_patch)) {
167                 _dm_flags |= DM_SECTOR_SIZE_SUPPORTED;
168                 _dm_flags |= DM_CAPI_STRING_SUPPORTED;
169         }
170
171         if (_dm_satisfies_version(1, 19, 0, crypt_maj, crypt_min, crypt_patch))
172                 _dm_flags |= DM_BITLK_EBOIV_SUPPORTED;
173
174         if (_dm_satisfies_version(1, 20, 0, crypt_maj, crypt_min, crypt_patch))
175                 _dm_flags |= DM_BITLK_ELEPHANT_SUPPORTED;
176
177         if (_dm_satisfies_version(1, 22, 0, crypt_maj, crypt_min, crypt_patch))
178                 _dm_flags |= DM_CRYPT_NO_WORKQUEUE_SUPPORTED;
179
180         _dm_crypt_checked = true;
181 }
182
183 static void _dm_set_verity_compat(struct crypt_device *cd,
184                                   unsigned verity_maj,
185                                   unsigned verity_min,
186                                   unsigned verity_patch)
187 {
188         if (_dm_verity_checked || verity_maj == 0)
189                 return;
190
191         log_dbg(cd, "Detected dm-verity version %i.%i.%i.",
192                 verity_maj, verity_min, verity_patch);
193
194         _dm_flags |= DM_VERITY_SUPPORTED;
195
196         /*
197          * ignore_corruption, restart_on corruption is available since 1.2 (kernel 4.1)
198          * ignore_zero_blocks since 1.3 (kernel 4.5)
199          * (but some dm-verity targets 1.2 don't support it)
200          * FEC is added in 1.3 as well.
201          * Check at most once is added in 1.4 (kernel 4.17).
202          */
203         if (_dm_satisfies_version(1, 3, 0, verity_maj, verity_min, verity_patch)) {
204                 _dm_flags |= DM_VERITY_ON_CORRUPTION_SUPPORTED;
205                 _dm_flags |= DM_VERITY_FEC_SUPPORTED;
206         }
207
208         if (_dm_satisfies_version(1, 5, 0, verity_maj, verity_min, verity_patch))
209                 _dm_flags |= DM_VERITY_SIGNATURE_SUPPORTED;
210
211         if (_dm_satisfies_version(1, 7, 0, verity_maj, verity_min, verity_patch))
212                 _dm_flags |= DM_VERITY_PANIC_CORRUPTION_SUPPORTED;
213
214         _dm_verity_checked = true;
215 }
216
217 static void _dm_set_integrity_compat(struct crypt_device *cd,
218                                      unsigned integrity_maj,
219                                      unsigned integrity_min,
220                                      unsigned integrity_patch)
221 {
222         if (_dm_integrity_checked || integrity_maj == 0)
223                 return;
224
225         log_dbg(cd, "Detected dm-integrity version %i.%i.%i.",
226                 integrity_maj, integrity_min, integrity_patch);
227
228         _dm_flags |= DM_INTEGRITY_SUPPORTED;
229
230         if (_dm_satisfies_version(1, 2, 0, integrity_maj, integrity_min, integrity_patch))
231                 _dm_flags |= DM_INTEGRITY_RECALC_SUPPORTED;
232
233         if (_dm_satisfies_version(1, 3, 0, integrity_maj, integrity_min, integrity_patch))
234                 _dm_flags |= DM_INTEGRITY_BITMAP_SUPPORTED;
235
236         if (_dm_satisfies_version(1, 4, 0, integrity_maj, integrity_min, integrity_patch))
237                 _dm_flags |= DM_INTEGRITY_FIX_PADDING_SUPPORTED;
238
239         if (_dm_satisfies_version(1, 6, 0, integrity_maj, integrity_min, integrity_patch))
240                 _dm_flags |= DM_INTEGRITY_DISCARDS_SUPPORTED;
241
242         if (_dm_satisfies_version(1, 7, 0, integrity_maj, integrity_min, integrity_patch))
243                 _dm_flags |= DM_INTEGRITY_FIX_HMAC_SUPPORTED;
244
245         _dm_integrity_checked = true;
246 }
247
248 /* We use this for loading target module */
249 static void _dm_check_target(dm_target_type target_type)
250 {
251 #if HAVE_DECL_DM_DEVICE_GET_TARGET_VERSION
252         struct dm_task *dmt;
253         const char *target_name = NULL;
254
255         if (!(_dm_flags & DM_GET_TARGET_VERSION_SUPPORTED))
256                 return;
257
258         if (target_type == DM_CRYPT)
259                 target_name = DM_CRYPT_TARGET;
260         else if (target_type == DM_VERITY)
261                 target_name = DM_VERITY_TARGET;
262         else if (target_type == DM_INTEGRITY)
263                 target_name = DM_INTEGRITY_TARGET;
264         else
265                 return;
266
267         if (!(dmt = dm_task_create(DM_DEVICE_GET_TARGET_VERSION)))
268                 goto out;
269
270         if (!dm_task_set_name(dmt, target_name))
271                 goto out;
272
273         if (!dm_task_run(dmt))
274                 goto out;
275 out:
276         if (dmt)
277                 dm_task_destroy(dmt);
278 #endif
279 }
280
281 static int _dm_check_versions(struct crypt_device *cd, dm_target_type target_type)
282 {
283         struct dm_task *dmt;
284         struct dm_versions *target, *last_target;
285         char dm_version[16];
286         unsigned dm_maj, dm_min, dm_patch;
287         int r = 0;
288
289         if ((target_type == DM_CRYPT     && _dm_crypt_checked) ||
290             (target_type == DM_VERITY    && _dm_verity_checked) ||
291             (target_type == DM_INTEGRITY && _dm_integrity_checked) ||
292             (target_type == DM_LINEAR) || (target_type == DM_ZERO) ||
293             (_dm_crypt_checked && _dm_verity_checked && _dm_integrity_checked))
294                 return 1;
295
296         /* Shut up DM while checking */
297         _quiet_log = 1;
298
299         _dm_check_target(target_type);
300
301         /* FIXME: add support to DM so it forces crypt target module load here */
302         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
303                 goto out;
304
305         if (!dm_task_run(dmt))
306                 goto out;
307
308         if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version)))
309                 goto out;
310
311         if (!_dm_ioctl_checked) {
312                 if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
313                         goto out;
314                 log_dbg(cd, "Detected dm-ioctl version %u.%u.%u.", dm_maj, dm_min, dm_patch);
315
316                 if (_dm_satisfies_version(4, 20, 0, dm_maj, dm_min, dm_patch))
317                         _dm_flags |= DM_SECURE_SUPPORTED;
318 #if HAVE_DECL_DM_TASK_DEFERRED_REMOVE
319                 if (_dm_satisfies_version(4, 27, 0, dm_maj, dm_min, dm_patch))
320                         _dm_flags |= DM_DEFERRED_SUPPORTED;
321 #endif
322 #if HAVE_DECL_DM_DEVICE_GET_TARGET_VERSION
323                 if (_dm_satisfies_version(4, 41, 0, dm_maj, dm_min, dm_patch))
324                         _dm_flags |= DM_GET_TARGET_VERSION_SUPPORTED;
325 #endif
326         }
327
328         target = dm_task_get_versions(dmt);
329         do {
330                 last_target = target;
331                 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
332                         _dm_set_crypt_compat(cd, (unsigned)target->version[0],
333                                              (unsigned)target->version[1],
334                                              (unsigned)target->version[2]);
335                 } else if (!strcmp(DM_VERITY_TARGET, target->name)) {
336                         _dm_set_verity_compat(cd, (unsigned)target->version[0],
337                                               (unsigned)target->version[1],
338                                               (unsigned)target->version[2]);
339                 } else if (!strcmp(DM_INTEGRITY_TARGET, target->name)) {
340                         _dm_set_integrity_compat(cd, (unsigned)target->version[0],
341                                                  (unsigned)target->version[1],
342                                                  (unsigned)target->version[2]);
343                 }
344                 target = (struct dm_versions *)((char *) target + target->next);
345         } while (last_target != target);
346
347         r = 1;
348         if (!_dm_ioctl_checked)
349                 log_dbg(cd, "Device-mapper backend running with UDEV support %sabled.",
350                         _dm_use_udev() ? "en" : "dis");
351
352         _dm_ioctl_checked = true;
353 out:
354         if (dmt)
355                 dm_task_destroy(dmt);
356
357         _quiet_log = 0;
358         return r;
359 }
360
361 int dm_flags(struct crypt_device *cd, dm_target_type target, uint32_t *flags)
362 {
363         _dm_check_versions(cd, target);
364         *flags = _dm_flags;
365
366         if (target == DM_UNKNOWN &&
367             _dm_crypt_checked && _dm_verity_checked && _dm_integrity_checked)
368                 return 0;
369
370         if ((target == DM_CRYPT     && _dm_crypt_checked) ||
371             (target == DM_VERITY    && _dm_verity_checked) ||
372             (target == DM_INTEGRITY && _dm_integrity_checked) ||
373             (target == DM_LINEAR) || (target == DM_ZERO)) /* nothing to check */
374                 return 0;
375
376         return -ENODEV;
377 }
378
379 /* This doesn't run any kernel checks, just set up userspace libdevmapper */
380 void dm_backend_init(struct crypt_device *cd)
381 {
382         if (!_dm_use_count++) {
383                 log_dbg(cd, "Initialising device-mapper backend library.");
384                 dm_log_init(set_dm_error);
385                 dm_log_init_verbose(10);
386         }
387 }
388
389 void dm_backend_exit(struct crypt_device *cd)
390 {
391         if (_dm_use_count && (!--_dm_use_count)) {
392                 log_dbg(cd, "Releasing device-mapper backend.");
393                 dm_log_init_verbose(0);
394                 dm_log_init(NULL);
395                 dm_lib_release();
396         }
397 }
398
399 /*
400  * libdevmapper is not context friendly, switch context on every DM call.
401  * FIXME: this is not safe if called in parallel but neither is DM lib.
402  */
403 static int dm_init_context(struct crypt_device *cd, dm_target_type target)
404 {
405         _context = cd;
406         if (!_dm_check_versions(cd, target)) {
407                 if (getuid() || geteuid())
408                         log_err(cd, _("Cannot initialize device-mapper, "
409                                       "running as non-root user."));
410                 else
411                         log_err(cd, _("Cannot initialize device-mapper. "
412                                       "Is dm_mod kernel module loaded?"));
413                 _context = NULL;
414                 return -ENOTSUP;
415         }
416         return 0;
417 }
418 static void dm_exit_context(void)
419 {
420         _context = NULL;
421 }
422
423 /* Return path to DM device */
424 char *dm_device_path(const char *prefix, int major, int minor)
425 {
426         struct dm_task *dmt;
427         const char *name;
428         char path[PATH_MAX];
429
430         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
431                 return NULL;
432         if (!dm_task_set_minor(dmt, minor) ||
433             !dm_task_set_major(dmt, major) ||
434             !dm_task_no_flush(dmt) ||
435             !dm_task_run(dmt) ||
436             !(name = dm_task_get_name(dmt))) {
437                 dm_task_destroy(dmt);
438                 return NULL;
439         }
440
441         if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
442                 path[0] = '\0';
443
444         dm_task_destroy(dmt);
445
446         return strdup(path);
447 }
448
449 char *dm_device_name(const char *path)
450 {
451         struct stat st;
452
453         if (stat(path, &st) < 0 || !S_ISBLK(st.st_mode))
454                 return NULL;
455
456         return dm_device_path(NULL, major(st.st_rdev), minor(st.st_rdev));
457 }
458
459 static void hex_key(char *hexkey, size_t key_size, const char *key)
460 {
461         unsigned i;
462
463         for(i = 0; i < key_size; i++)
464                 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
465 }
466
467 static size_t int_log10(uint64_t x)
468 {
469         uint64_t r = 0;
470         for (x /= 10; x > 0; x /= 10)
471                 r++;
472         return r;
473 }
474
475 #define CLEN    64   /* 2*MAX_CIPHER_LEN */
476 #define CLENS  "63"  /* for sscanf length + '\0' */
477 #define CAPIL  144   /* should be enough to fit whole capi string */
478 #define CAPIS "143"  /* for sscanf of crypto API string + 16  + \0 */
479
480 static int cipher_c2dm(const char *org_c, const char *org_i, unsigned tag_size,
481                        char *c_dm, int c_dm_size,
482                        char *i_dm, int i_dm_size)
483 {
484         int c_size = 0, i_size = 0, i;
485         char cipher[CLEN], mode[CLEN], iv[CLEN+1], tmp[CLEN];
486         char capi[CAPIL];
487
488         if (!c_dm || !c_dm_size || !i_dm || !i_dm_size)
489                 return -EINVAL;
490
491         i = sscanf(org_c, "%" CLENS "[^-]-%" CLENS "s", cipher, tmp);
492         if (i != 2)
493                 return -EINVAL;
494
495         i = sscanf(tmp, "%" CLENS "[^-]-%" CLENS "s", mode, iv);
496         if (i == 1) {
497                 memset(iv, 0, sizeof(iv));
498                 strncpy(iv, mode, sizeof(iv)-1);
499                 *mode = '\0';
500                 if (snprintf(capi, sizeof(capi), "%s", cipher) < 0)
501                         return -EINVAL;
502         } else if (i == 2) {
503                 if (snprintf(capi, sizeof(capi), "%s(%s)", mode, cipher) < 0)
504                         return -EINVAL;
505         } else
506                 return -EINVAL;
507
508         if (!org_i) {
509                 /* legacy mode: CIPHER-MODE-IV*/
510                 i_size = snprintf(i_dm, i_dm_size, "%s", "");
511                 c_size = snprintf(c_dm, c_dm_size, "%s", org_c);
512         } else if (!strcmp(org_i, "none")) {
513                 /* IV only: capi:MODE(CIPHER)-IV */
514                 i_size = snprintf(i_dm, i_dm_size, " integrity:%u:none", tag_size);
515                 c_size = snprintf(c_dm, c_dm_size, "capi:%s-%s", capi, iv);
516         } else if (!strcmp(org_i, "aead") && !strcmp(mode, "ccm")) {
517                 /* CCM AEAD: capi:rfc4309(MODE(CIPHER))-IV */
518                 i_size = snprintf(i_dm, i_dm_size, " integrity:%u:aead", tag_size);
519                 c_size = snprintf(c_dm, c_dm_size, "capi:rfc4309(%s)-%s", capi, iv);
520         } else if (!strcmp(org_i, "aead")) {
521                 /* AEAD: capi:MODE(CIPHER))-IV */
522                 i_size = snprintf(i_dm, i_dm_size, " integrity:%u:aead", tag_size);
523                 c_size = snprintf(c_dm, c_dm_size, "capi:%s-%s", capi, iv);
524         } else if (!strcmp(org_i, "poly1305")) {
525                 /* POLY1305 AEAD: capi:rfc7539(MODE(CIPHER),POLY1305)-IV */
526                 i_size = snprintf(i_dm, i_dm_size, " integrity:%u:aead", tag_size);
527                 c_size = snprintf(c_dm, c_dm_size, "capi:rfc7539(%s,poly1305)-%s", capi, iv);
528         } else {
529                 /* other AEAD: capi:authenc(<AUTH>,MODE(CIPHER))-IV */
530                 i_size = snprintf(i_dm, i_dm_size, " integrity:%u:aead", tag_size);
531                 c_size = snprintf(c_dm, c_dm_size, "capi:authenc(%s,%s)-%s", org_i, capi, iv);
532         }
533
534         if (c_size < 0 || c_size == c_dm_size)
535                 return -EINVAL;
536         if (i_size < 0 || i_size == i_dm_size)
537                 return -EINVAL;
538
539         return 0;
540 }
541
542 static int cipher_dm2c(char **org_c, char **org_i, const char *c_dm, const char *i_dm)
543 {
544         char cipher[CLEN], mode[CLEN], iv[CLEN], auth[CLEN];
545         char tmp[CAPIL], dmcrypt_tmp[CAPIL*2], capi[CAPIL+1];
546         size_t len;
547         int i;
548
549         if (!c_dm)
550                 return -EINVAL;
551
552         /* legacy mode */
553         if (strncmp(c_dm, "capi:", 4)) {
554                 if (!(*org_c = strdup(c_dm)))
555                         return -ENOMEM;
556                 *org_i = NULL;
557                 return 0;
558         }
559
560         /* modes with capi: prefix */
561         i = sscanf(c_dm, "capi:%" CAPIS "[^-]-%" CLENS "s", tmp, iv);
562         if (i != 2)
563                 return -EINVAL;
564
565         len = strlen(tmp);
566         if (len < 2)
567                 return -EINVAL;
568
569         if (tmp[len-1] == ')')
570                 tmp[len-1] = '\0';
571
572         if (sscanf(tmp, "rfc4309(%" CAPIS "s", capi) == 1) {
573                 if (!(*org_i = strdup("aead")))
574                         return -ENOMEM;
575         } else if (sscanf(tmp, "rfc7539(%" CAPIS "[^,],%" CLENS "s", capi, auth) == 2) {
576                 if (!(*org_i = strdup(auth)))
577                         return -ENOMEM;
578         } else if (sscanf(tmp, "authenc(%" CLENS "[^,],%" CAPIS "s", auth, capi) == 2) {
579                 if (!(*org_i = strdup(auth)))
580                         return -ENOMEM;
581         } else {
582                 if (i_dm) {
583                         if (!(*org_i = strdup(i_dm)))
584                                 return -ENOMEM;
585                 } else
586                         *org_i = NULL;
587                 memset(capi, 0, sizeof(capi));
588                 strncpy(capi, tmp, sizeof(capi)-1);
589         }
590
591         i = sscanf(capi, "%" CLENS "[^(](%" CLENS "[^)])", mode, cipher);
592         if (i == 2)
593                 i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s-%s", cipher, mode, iv);
594         else
595                 i = snprintf(dmcrypt_tmp, sizeof(dmcrypt_tmp), "%s-%s", capi, iv);
596         if (i < 0 || (size_t)i >= sizeof(dmcrypt_tmp)) {
597                 free(*org_i);
598                 *org_i = NULL;
599                 return -EINVAL;
600         }
601
602         if (!(*org_c = strdup(dmcrypt_tmp))) {
603                 free(*org_i);
604                 *org_i = NULL;
605                 return -ENOMEM;
606         }
607
608         return 0;
609 }
610
611 static char *_uf(char *buf, size_t buf_size, const char *s, unsigned u)
612 {
613         size_t r = snprintf(buf, buf_size, " %s:%u", s, u);
614         assert(r > 0 && r < buf_size);
615         return buf;
616 }
617
618 /* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt */
619 static char *get_dm_crypt_params(const struct dm_target *tgt, uint32_t flags)
620 {
621         int r, max_size, null_cipher = 0, num_options = 0, keystr_len = 0;
622         char *params = NULL, *hexkey = NULL;
623         char sector_feature[32], features[512], integrity_dm[256], cipher_dm[256];
624
625         if (!tgt)
626                 return NULL;
627
628         r = cipher_c2dm(tgt->u.crypt.cipher, tgt->u.crypt.integrity, tgt->u.crypt.tag_size,
629                         cipher_dm, sizeof(cipher_dm), integrity_dm, sizeof(integrity_dm));
630         if (r < 0)
631                 return NULL;
632
633         if (flags & CRYPT_ACTIVATE_ALLOW_DISCARDS)
634                 num_options++;
635         if (flags & CRYPT_ACTIVATE_SAME_CPU_CRYPT)
636                 num_options++;
637         if (flags & CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS)
638                 num_options++;
639         if (flags & CRYPT_ACTIVATE_NO_READ_WORKQUEUE)
640                 num_options++;
641         if (flags & CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE)
642                 num_options++;
643         if (flags & CRYPT_ACTIVATE_IV_LARGE_SECTORS)
644                 num_options++;
645         if (tgt->u.crypt.integrity)
646                 num_options++;
647         if (tgt->u.crypt.sector_size != SECTOR_SIZE)
648                 num_options++;
649
650         if (num_options) { /* MAX length  int32 + 15 + 15 + 23 + 18 + 19 + 17 + 13 + int32 + integrity_str */
651                 r = snprintf(features, sizeof(features), " %d%s%s%s%s%s%s%s%s", num_options,
652                 (flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) ? " allow_discards" : "",
653                 (flags & CRYPT_ACTIVATE_SAME_CPU_CRYPT) ? " same_cpu_crypt" : "",
654                 (flags & CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS) ? " submit_from_crypt_cpus" : "",
655                 (flags & CRYPT_ACTIVATE_NO_READ_WORKQUEUE) ? " no_read_workqueue" : "",
656                 (flags & CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE) ? " no_write_workqueue" : "",
657                 (flags & CRYPT_ACTIVATE_IV_LARGE_SECTORS) ? " iv_large_sectors" : "",
658                 (tgt->u.crypt.sector_size != SECTOR_SIZE) ?
659                         _uf(sector_feature, sizeof(sector_feature), "sector_size", tgt->u.crypt.sector_size) : "",
660                 integrity_dm);
661                 if (r < 0 || (size_t)r >= sizeof(features))
662                         goto out;
663         } else
664                 *features = '\0';
665
666         if (crypt_is_cipher_null(cipher_dm))
667                 null_cipher = 1;
668
669         if (null_cipher)
670                 hexkey = crypt_safe_alloc(2);
671         else if (flags & CRYPT_ACTIVATE_KEYRING_KEY) {
672                 keystr_len = strlen(tgt->u.crypt.vk->key_description) + int_log10(tgt->u.crypt.vk->keylength) + 10;
673                 hexkey = crypt_safe_alloc(keystr_len);
674         } else
675                 hexkey = crypt_safe_alloc(tgt->u.crypt.vk->keylength * 2 + 1);
676
677         if (!hexkey)
678                 goto out;
679
680         if (null_cipher)
681                 strncpy(hexkey, "-", 2);
682         else if (flags & CRYPT_ACTIVATE_KEYRING_KEY) {
683                 r = snprintf(hexkey, keystr_len, ":%zu:logon:%s", tgt->u.crypt.vk->keylength, tgt->u.crypt.vk->key_description);
684                 if (r < 0 || r >= keystr_len)
685                         goto out;
686         } else
687                 hex_key(hexkey, tgt->u.crypt.vk->keylength, tgt->u.crypt.vk->key);
688
689         max_size = strlen(hexkey) + strlen(cipher_dm) +
690                    strlen(device_block_path(tgt->data_device)) +
691                    strlen(features) + 64;
692         params = crypt_safe_alloc(max_size);
693         if (!params)
694                 goto out;
695
696         r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
697                      cipher_dm, hexkey, tgt->u.crypt.iv_offset,
698                      device_block_path(tgt->data_device), tgt->u.crypt.offset,
699                      features);
700         if (r < 0 || r >= max_size) {
701                 crypt_safe_free(params);
702                 params = NULL;
703         }
704 out:
705         crypt_safe_free(hexkey);
706         return params;
707 }
708
709 /* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity */
710 static char *get_dm_verity_params(const struct dm_target *tgt, uint32_t flags)
711 {
712         int max_size, max_fec_size, max_verify_size, r, num_options = 0;
713         struct crypt_params_verity *vp;
714         char *params = NULL, *hexroot = NULL, *hexsalt = NULL;
715         char features[256], *fec_features = NULL, *verity_verify_args = NULL;
716
717         if (!tgt || !tgt->u.verity.vp)
718                 return NULL;
719
720         vp = tgt->u.verity.vp;
721
722         /* These flags are not compatible */
723         if ((flags & CRYPT_ACTIVATE_RESTART_ON_CORRUPTION) &&
724             (flags & CRYPT_ACTIVATE_PANIC_ON_CORRUPTION))
725                 flags &= ~CRYPT_ACTIVATE_RESTART_ON_CORRUPTION;
726         if ((flags & CRYPT_ACTIVATE_IGNORE_CORRUPTION) &&
727             (flags & (CRYPT_ACTIVATE_RESTART_ON_CORRUPTION|CRYPT_ACTIVATE_PANIC_ON_CORRUPTION)))
728                 flags &= ~CRYPT_ACTIVATE_IGNORE_CORRUPTION;
729
730         if (flags & CRYPT_ACTIVATE_IGNORE_CORRUPTION)
731                 num_options++;
732         if (flags & CRYPT_ACTIVATE_RESTART_ON_CORRUPTION)
733                 num_options++;
734         if (flags & CRYPT_ACTIVATE_PANIC_ON_CORRUPTION)
735                 num_options++;
736         if (flags & CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS)
737                 num_options++;
738         if (flags & CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE)
739                 num_options++;
740
741         max_fec_size = (tgt->u.verity.fec_device ? strlen(device_block_path(tgt->u.verity.fec_device)) : 0) + 256;
742         fec_features = crypt_safe_alloc(max_fec_size);
743         if (!fec_features)
744                 goto out;
745
746         if (tgt->u.verity.fec_device) {  /* MAX length 21 + path + 11 + int64 + 12 + int64 + 11 + int32 */
747                 num_options += 8;
748                 r = snprintf(fec_features, max_fec_size,
749                          " use_fec_from_device %s fec_start %" PRIu64 " fec_blocks %" PRIu64 " fec_roots %" PRIu32,
750                          device_block_path(tgt->u.verity.fec_device), tgt->u.verity.fec_offset,
751                          tgt->u.verity.fec_blocks, vp->fec_roots);
752                 if (r < 0 || r >= max_fec_size)
753                         goto out;
754         } else
755                 *fec_features = '\0';
756
757         max_verify_size = (tgt->u.verity.root_hash_sig_key_desc ? strlen(tgt->u.verity.root_hash_sig_key_desc) : 0) + 32;
758         verity_verify_args = crypt_safe_alloc(max_verify_size);
759         if (!verity_verify_args)
760                 goto out;
761         if (tgt->u.verity.root_hash_sig_key_desc) {  /* MAX length 24 + key_str */
762                 num_options += 2;
763                 r = snprintf(verity_verify_args, max_verify_size,
764                                 " root_hash_sig_key_desc %s", tgt->u.verity.root_hash_sig_key_desc);
765                 if (r < 0 || r >= max_verify_size)
766                         goto out;
767         } else
768                 *verity_verify_args = '\0';
769
770         if (num_options) {  /* MAX length int32 + 18 + 22 + 20 + 19 + 19 */
771                 r = snprintf(features, sizeof(features), " %d%s%s%s%s%s", num_options,
772                 (flags & CRYPT_ACTIVATE_IGNORE_CORRUPTION) ? " ignore_corruption" : "",
773                 (flags & CRYPT_ACTIVATE_RESTART_ON_CORRUPTION) ? " restart_on_corruption" : "",
774                 (flags & CRYPT_ACTIVATE_PANIC_ON_CORRUPTION) ? " panic_on_corruption" : "",
775                 (flags & CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS) ? " ignore_zero_blocks" : "",
776                 (flags & CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE) ? " check_at_most_once" : "");
777                 if (r < 0 || (size_t)r >= sizeof(features))
778                         goto out;
779         } else
780                 *features = '\0';
781
782         hexroot = crypt_safe_alloc(tgt->u.verity.root_hash_size * 2 + 1);
783         if (!hexroot)
784                 goto out;
785         hex_key(hexroot, tgt->u.verity.root_hash_size, tgt->u.verity.root_hash);
786
787         hexsalt = crypt_safe_alloc(vp->salt_size ? vp->salt_size * 2 + 1 : 2);
788         if (!hexsalt)
789                 goto out;
790         if (vp->salt_size)
791                 hex_key(hexsalt, vp->salt_size, vp->salt);
792         else
793                 strncpy(hexsalt, "-", 2);
794
795         max_size = strlen(hexroot) + strlen(hexsalt) +
796                    strlen(device_block_path(tgt->data_device)) +
797                    strlen(device_block_path(tgt->u.verity.hash_device)) +
798                    strlen(vp->hash_name) + strlen(features) + strlen(fec_features) + 128 +
799                    strlen(verity_verify_args);
800
801         params = crypt_safe_alloc(max_size);
802         if (!params)
803                 goto out;
804
805         r = snprintf(params, max_size,
806                      "%u %s %s %u %u %" PRIu64 " %" PRIu64 " %s %s %s%s%s%s",
807                      vp->hash_type, device_block_path(tgt->data_device),
808                      device_block_path(tgt->u.verity.hash_device),
809                      vp->data_block_size, vp->hash_block_size,
810                      vp->data_size, tgt->u.verity.hash_offset,
811                      vp->hash_name, hexroot, hexsalt, features, fec_features,
812                      verity_verify_args);
813         if (r < 0 || r >= max_size) {
814                 crypt_safe_free(params);
815                 params = NULL;
816         }
817 out:
818         crypt_safe_free(fec_features);
819         crypt_safe_free(verity_verify_args);
820         crypt_safe_free(hexroot);
821         crypt_safe_free(hexsalt);
822         return params;
823 }
824
825 static char *get_dm_integrity_params(const struct dm_target *tgt, uint32_t flags)
826 {
827         int r, max_size, max_integrity, max_journal_integrity, max_journal_crypt, num_options = 0;
828         char *params_out = NULL, *params, *hexkey, mode, feature[6][32];
829         char *features, *integrity, *journal_integrity, *journal_crypt;
830
831         if (!tgt)
832                 return NULL;
833
834         max_integrity = (tgt->u.integrity.integrity && tgt->u.integrity.vk ? tgt->u.integrity.vk->keylength * 2 : 0) +
835                 (tgt->u.integrity.integrity ? strlen(tgt->u.integrity.integrity) : 0) + 32;
836         max_journal_integrity = (tgt->u.integrity.journal_integrity && tgt->u.integrity.journal_integrity_key ?
837                 tgt->u.integrity.journal_integrity_key->keylength * 2 : 0) +
838                 (tgt->u.integrity.journal_integrity ? strlen(tgt->u.integrity.journal_integrity) : 0) + 32;
839         max_journal_crypt = (tgt->u.integrity.journal_crypt && tgt->u.integrity.journal_crypt_key ?
840                 tgt->u.integrity.journal_crypt_key->keylength * 2 : 0) +
841                 (tgt->u.integrity.journal_crypt ? strlen(tgt->u.integrity.journal_crypt) : 0) + 32;
842         max_size = strlen(device_block_path(tgt->data_device)) +
843                 (tgt->u.integrity.meta_device ? strlen(device_block_path(tgt->u.integrity.meta_device)) : 0) +
844                 max_integrity + max_journal_integrity + max_journal_crypt + 512;
845
846         params = crypt_safe_alloc(max_size);
847         features = crypt_safe_alloc(max_size);
848         integrity = crypt_safe_alloc(max_integrity);
849         journal_integrity = crypt_safe_alloc(max_journal_integrity);
850         journal_crypt = crypt_safe_alloc(max_journal_crypt);
851         if (!params || !features || !integrity || !journal_integrity || !journal_crypt)
852                 goto out;
853
854         if (tgt->u.integrity.integrity) { /* MAX length 16 + str_integrity +  str_key */
855                 num_options++;
856
857                 if (tgt->u.integrity.vk) {
858                         hexkey = crypt_safe_alloc(tgt->u.integrity.vk->keylength * 2 + 1);
859                         if (!hexkey)
860                                 goto out;
861                         hex_key(hexkey, tgt->u.integrity.vk->keylength, tgt->u.integrity.vk->key);
862                 } else
863                         hexkey = NULL;
864
865                 r = snprintf(integrity, max_integrity, " internal_hash:%s%s%s",
866                          tgt->u.integrity.integrity, hexkey ? ":" : "", hexkey ?: "");
867                 crypt_safe_free(hexkey);
868                 if (r < 0 || r >= max_integrity)
869                         goto out;
870         }
871
872         if (tgt->u.integrity.journal_integrity) { /* MAX length 14 + str_journal_integrity + str_key */
873                 num_options++;
874
875                 if (tgt->u.integrity.journal_integrity_key) {
876                         hexkey = crypt_safe_alloc(tgt->u.integrity.journal_integrity_key->keylength * 2 + 1);
877                         if (!hexkey)
878                                 goto out;
879                         hex_key(hexkey, tgt->u.integrity.journal_integrity_key->keylength,
880                                 tgt->u.integrity.journal_integrity_key->key);
881                 } else
882                         hexkey = NULL;
883
884                 r = snprintf(journal_integrity, max_journal_integrity, " journal_mac:%s%s%s",
885                          tgt->u.integrity.journal_integrity, hexkey ? ":" : "", hexkey ?: "");
886                 crypt_safe_free(hexkey);
887                 if (r < 0 || r >= max_journal_integrity)
888                         goto out;
889         }
890
891         if (tgt->u.integrity.journal_crypt) { /* MAX length 15 + str_journal_crypt + str_key */
892                 num_options++;
893
894                 if (tgt->u.integrity.journal_crypt_key) {
895                         hexkey = crypt_safe_alloc(tgt->u.integrity.journal_crypt_key->keylength * 2 + 1);
896                         if (!hexkey)
897                                 goto out;
898                         hex_key(hexkey, tgt->u.integrity.journal_crypt_key->keylength,
899                                 tgt->u.integrity.journal_crypt_key->key);
900                 } else
901                         hexkey = NULL;
902
903                 r = snprintf(journal_crypt, max_journal_crypt, " journal_crypt:%s%s%s",
904                          tgt->u.integrity.journal_crypt, hexkey ? ":" : "", hexkey ?: "");
905                 crypt_safe_free(hexkey);
906                 if (r < 0 || r >= max_journal_crypt)
907                         goto out;
908         }
909
910         if (tgt->u.integrity.journal_size)
911                 num_options++;
912         if (tgt->u.integrity.journal_watermark)
913                 num_options++;
914         if (tgt->u.integrity.journal_commit_time)
915                 num_options++;
916         if (tgt->u.integrity.interleave_sectors)
917                 num_options++;
918         if (tgt->u.integrity.sector_size)
919                 num_options++;
920         if (tgt->u.integrity.buffer_sectors)
921                 num_options++;
922         if (tgt->u.integrity.fix_padding)
923                 num_options++;
924         if (tgt->u.integrity.fix_hmac)
925                 num_options++;
926         if (tgt->u.integrity.legacy_recalc)
927                 num_options++;
928         if (tgt->u.integrity.meta_device)
929                 num_options++;
930         if (flags & CRYPT_ACTIVATE_RECALCULATE)
931                 num_options++;
932         if (flags & CRYPT_ACTIVATE_ALLOW_DISCARDS)
933                 num_options++;
934
935         r = snprintf(features, max_size, "%d%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", num_options,
936                 tgt->u.integrity.journal_size ? _uf(feature[0], sizeof(feature[0]), /* MAX length 17 + int32 */
937                         "journal_sectors", (unsigned)(tgt->u.integrity.journal_size / SECTOR_SIZE)) : "",
938                 tgt->u.integrity.journal_watermark ? _uf(feature[1], sizeof(feature[1]), /* MAX length 19 + int32 */
939                          /* bitmap overloaded values */
940                          (flags & CRYPT_ACTIVATE_NO_JOURNAL_BITMAP) ? "sectors_per_bit" : "journal_watermark",
941                          tgt->u.integrity.journal_watermark) : "",
942                 tgt->u.integrity.journal_commit_time ? _uf(feature[2], sizeof(feature[2]), /* MAX length 23 + int32 */
943                          /* bitmap overloaded values */
944                          (flags & CRYPT_ACTIVATE_NO_JOURNAL_BITMAP) ? "bitmap_flush_interval" : "commit_time",
945                          tgt->u.integrity.journal_commit_time) : "",
946                 tgt->u.integrity.interleave_sectors ? _uf(feature[3], sizeof(feature[3]), /* MAX length 20 + int32 */
947                         "interleave_sectors", tgt->u.integrity.interleave_sectors) : "",
948                 tgt->u.integrity.sector_size ? _uf(feature[4], sizeof(feature[4]), /* MAX length 12 + int32 */
949                         "block_size", tgt->u.integrity.sector_size) : "",
950                 tgt->u.integrity.buffer_sectors ? _uf(feature[5], sizeof(feature[5]), /* MAX length 16 + int32 */
951                         "buffer_sectors", tgt->u.integrity.buffer_sectors) : "",
952                 tgt->u.integrity.integrity ? integrity : "",
953                 tgt->u.integrity.journal_integrity ? journal_integrity : "",
954                 tgt->u.integrity.journal_crypt ? journal_crypt : "",
955                 tgt->u.integrity.fix_padding ?  " fix_padding" : "", /* MAX length 12 */
956                 tgt->u.integrity.fix_hmac ?  " fix_hmac" : "", /* MAX length 9 */
957                 tgt->u.integrity.legacy_recalc ? " legacy_recalculate" : "", /* MAX length 19 */
958                 flags & CRYPT_ACTIVATE_RECALCULATE ? " recalculate" : "", /* MAX length 12 */
959                 flags & CRYPT_ACTIVATE_ALLOW_DISCARDS ? " allow_discards" : "", /* MAX length 15 */
960                 tgt->u.integrity.meta_device ? " meta_device:" : "", /* MAX length 13 + str_device */
961                 tgt->u.integrity.meta_device ? device_block_path(tgt->u.integrity.meta_device) : "");
962         if (r < 0 || r >= max_size)
963                 goto out;
964
965         if (flags & CRYPT_ACTIVATE_NO_JOURNAL_BITMAP)
966                 mode = 'B';
967         else if (flags & CRYPT_ACTIVATE_RECOVERY)
968                 mode = 'R';
969         else if (flags & CRYPT_ACTIVATE_NO_JOURNAL)
970                 mode = 'D';
971         else
972                 mode = 'J';
973
974         r = snprintf(params, max_size, "%s %" PRIu64 " %d %c %s",
975                      device_block_path(tgt->data_device), tgt->u.integrity.offset,
976                      tgt->u.integrity.tag_size, mode, features);
977         if (r < 0 || r >= max_size)
978                 goto out;
979
980         params_out = params;
981 out:
982         crypt_safe_free(features);
983         crypt_safe_free(integrity);
984         crypt_safe_free(journal_integrity);
985         crypt_safe_free(journal_crypt);
986         if (!params_out)
987                 crypt_safe_free(params);
988
989         return params_out;
990 }
991
992 static char *get_dm_linear_params(const struct dm_target *tgt, uint32_t flags)
993 {
994         char *params;
995         int r;
996         int max_size = strlen(device_block_path(tgt->data_device)) + int_log10(tgt->u.linear.offset) + 3;
997
998         params = crypt_safe_alloc(max_size);
999         if (!params)
1000                 return NULL;
1001
1002         r = snprintf(params, max_size, "%s %" PRIu64,
1003                      device_block_path(tgt->data_device), tgt->u.linear.offset);
1004
1005         if (r < 0 || r >= max_size) {
1006                 crypt_safe_free(params);
1007                 params = NULL;
1008         }
1009
1010         return params;
1011 }
1012
1013 static char *get_dm_zero_params(const struct dm_target *tgt, uint32_t flags)
1014 {
1015         char *params = crypt_safe_alloc(1);
1016         if (!params)
1017                 return NULL;
1018
1019         params[0] = 0;
1020         return params;
1021 }
1022
1023 /* DM helpers */
1024 static int _dm_remove(const char *name, int udev_wait, int deferred)
1025 {
1026         int r = 0;
1027         struct dm_task *dmt;
1028         uint32_t cookie = 0;
1029
1030         if (!_dm_use_udev())
1031                 udev_wait = 0;
1032
1033         if (!(dmt = dm_task_create(DM_DEVICE_REMOVE)))
1034                 return 0;
1035
1036         if (!dm_task_set_name(dmt, name))
1037                 goto out;
1038
1039 #if HAVE_DECL_DM_TASK_RETRY_REMOVE
1040         if (!dm_task_retry_remove(dmt))
1041                 goto out;
1042 #endif
1043 #if HAVE_DECL_DM_TASK_DEFERRED_REMOVE
1044         if (deferred && !dm_task_deferred_remove(dmt))
1045                 goto out;
1046 #endif
1047         if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, DM_UDEV_DISABLE_LIBRARY_FALLBACK))
1048                 goto out;
1049
1050         r = dm_task_run(dmt);
1051
1052         if (udev_wait)
1053                 (void)_dm_udev_wait(cookie);
1054 out:
1055         dm_task_destroy(dmt);
1056         return r;
1057 }
1058
1059 static int _dm_simple(int task, const char *name, uint32_t dmflags)
1060 {
1061         int r = 0;
1062         struct dm_task *dmt;
1063
1064         if (!(dmt = dm_task_create(task)))
1065                 return 0;
1066
1067         if (name && !dm_task_set_name(dmt, name))
1068                 goto out;
1069
1070         if (task == DM_DEVICE_SUSPEND &&
1071             (dmflags & DM_SUSPEND_SKIP_LOCKFS) && !dm_task_skip_lockfs(dmt))
1072                 goto out;
1073
1074         if (task == DM_DEVICE_SUSPEND &&
1075             (dmflags & DM_SUSPEND_NOFLUSH) && !dm_task_no_flush(dmt))
1076                 goto out;
1077
1078         r = dm_task_run(dmt);
1079 out:
1080         dm_task_destroy(dmt);
1081         return r;
1082 }
1083
1084 static int _dm_resume_device(const char *name, uint32_t flags);
1085
1086 static int _error_device(const char *name, size_t size)
1087 {
1088         struct dm_task *dmt;
1089         int r = 0;
1090
1091         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
1092                 return 0;
1093
1094         if (!dm_task_set_name(dmt, name))
1095                 goto error;
1096
1097         if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
1098                 goto error;
1099
1100         if (!dm_task_set_ro(dmt))
1101                 goto error;
1102
1103         if (!dm_task_no_open_count(dmt))
1104                 goto error;
1105
1106         if (!dm_task_run(dmt))
1107                 goto error;
1108
1109         if (_dm_resume_device(name, 0)) {
1110                 _dm_simple(DM_DEVICE_CLEAR, name, 0);
1111                 goto error;
1112         }
1113
1114         r = 1;
1115
1116 error:
1117         dm_task_destroy(dmt);
1118         return r;
1119 }
1120
1121 int dm_error_device(struct crypt_device *cd, const char *name)
1122 {
1123         int r;
1124         struct crypt_dm_active_device dmd;
1125
1126         if (!name)
1127                 return -EINVAL;
1128
1129         if (dm_init_context(cd, DM_UNKNOWN))
1130                 return -ENOTSUP;
1131
1132         if ((dm_query_device(cd, name, 0, &dmd) >= 0) && _error_device(name, dmd.size))
1133                 r = 0;
1134         else
1135                 r = -EINVAL;
1136
1137         dm_targets_free(cd, &dmd);
1138
1139         dm_exit_context();
1140
1141         return r;
1142 }
1143
1144 int dm_clear_device(struct crypt_device *cd, const char *name)
1145 {
1146         int r;
1147
1148         if (!name)
1149                 return -EINVAL;
1150
1151         if (dm_init_context(cd, DM_UNKNOWN))
1152                 return -ENOTSUP;
1153
1154         if (_dm_simple(DM_DEVICE_CLEAR, name, 0))
1155                 r = 0;
1156         else
1157                 r = -EINVAL;
1158
1159         dm_exit_context();
1160
1161         return r;
1162 }
1163
1164 int dm_remove_device(struct crypt_device *cd, const char *name, uint32_t flags)
1165 {
1166         struct crypt_dm_active_device dmd = {};
1167         int r = -EINVAL;
1168         int retries = (flags & CRYPT_DEACTIVATE_FORCE) ? RETRY_COUNT : 1;
1169         int deferred = (flags & CRYPT_DEACTIVATE_DEFERRED) ? 1 : 0;
1170         int error_target = 0;
1171         uint32_t dmt_flags;
1172
1173         if (!name)
1174                 return -EINVAL;
1175
1176         if (dm_init_context(cd, DM_UNKNOWN))
1177                 return -ENOTSUP;
1178
1179         if (deferred && !dm_flags(cd, DM_UNKNOWN, &dmt_flags) && !(dmt_flags & DM_DEFERRED_SUPPORTED)) {
1180                 log_err(cd, _("Requested deferred flag is not supported."));
1181                 dm_exit_context();
1182                 return -ENOTSUP;
1183         }
1184
1185         do {
1186                 r = _dm_remove(name, 1, deferred) ? 0 : -EINVAL;
1187                 if (--retries && r) {
1188                         log_dbg(cd, "WARNING: other process locked internal device %s, %s.",
1189                                 name, retries ? "retrying remove" : "giving up");
1190                         sleep(1);
1191                         if ((flags & CRYPT_DEACTIVATE_FORCE) && !error_target) {
1192                                 /* If force flag is set, replace device with error, read-only target.
1193                                  * it should stop processes from reading it and also removed underlying
1194                                  * device from mapping, so it is usable again.
1195                                  * Anyway, if some process try to read temporary cryptsetup device,
1196                                  * it is bug - no other process should try touch it (e.g. udev).
1197                                  */
1198                                 if (!dm_query_device(cd, name, 0, &dmd)) {
1199                                         _error_device(name, dmd.size);
1200                                         error_target = 1;
1201                                 }
1202                         }
1203                 }
1204         } while (r == -EINVAL && retries);
1205
1206         dm_task_update_nodes();
1207         dm_exit_context();
1208
1209         return r;
1210 }
1211
1212 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
1213 /*
1214  * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
1215  * CRYPT-PLAIN-name
1216  * CRYPT-LUKS1-00000000000000000000000000000000-name
1217  * CRYPT-TEMP-name
1218  */
1219 static int dm_prepare_uuid(struct crypt_device *cd, const char *name, const char *type,
1220                             const char *uuid, char *buf, size_t buflen)
1221 {
1222         char *ptr, uuid2[UUID_LEN] = {0};
1223         uuid_t uu;
1224         int i = 0;
1225
1226         /* Remove '-' chars */
1227         if (uuid) {
1228                 if (uuid_parse(uuid, uu) < 0) {
1229                         log_dbg(cd, "Requested UUID %s has invalid format.", uuid);
1230                         return 0;
1231                 }
1232
1233                 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
1234                         if (uuid[i] != '-') {
1235                                 *ptr = uuid[i];
1236                                 ptr++;
1237                         }
1238         }
1239
1240         i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
1241                 type ?: "", type ? "-" : "",
1242                 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
1243                 name);
1244         if (i < 0)
1245                 return 0;
1246
1247         log_dbg(cd, "DM-UUID is %s", buf);
1248         if ((size_t)i >= buflen)
1249                 log_err(cd, _("DM-UUID for device %s was truncated."), name);
1250
1251         return 1;
1252 }
1253
1254 int lookup_dm_dev_by_uuid(struct crypt_device *cd, const char *uuid, const char *type)
1255 {
1256         int r;
1257         char *c;
1258         char dev_uuid[DM_UUID_LEN + DM_BY_ID_PREFIX_LEN] = DM_BY_ID_PREFIX;
1259
1260         if (!dm_prepare_uuid(cd, "", type, uuid, dev_uuid + DM_BY_ID_PREFIX_LEN, DM_UUID_LEN))
1261                 return -EINVAL;
1262
1263         c = strrchr(dev_uuid, '-');
1264         if (!c)
1265                 return -EINVAL;
1266
1267         /* cut of dm name */
1268         *c = '\0';
1269
1270         r = lookup_by_disk_id(dev_uuid);
1271         if (r == -ENOENT) {
1272                 log_dbg(cd, "Search by disk id not available. Using sysfs instead.");
1273                 r = lookup_by_sysfs_uuid_field(dev_uuid + DM_BY_ID_PREFIX_LEN, DM_UUID_LEN);
1274         }
1275
1276         return r;
1277 }
1278
1279 static int _add_dm_targets(struct dm_task *dmt, struct crypt_dm_active_device *dmd)
1280 {
1281         const char *target;
1282         struct dm_target *tgt = &dmd->segment;
1283
1284         do {
1285                 switch (tgt->type) {
1286                 case DM_CRYPT:
1287                         target = DM_CRYPT_TARGET;
1288                         break;
1289                 case DM_VERITY:
1290                         target = DM_VERITY_TARGET;
1291                         break;
1292                 case DM_INTEGRITY:
1293                         target = DM_INTEGRITY_TARGET;
1294                         break;
1295                 case DM_LINEAR:
1296                         target = DM_LINEAR_TARGET;
1297                         break;
1298                 case DM_ZERO:
1299                         target = DM_ZERO_TARGET;
1300                         break;
1301                 default:
1302                         return -ENOTSUP;
1303                 }
1304
1305                 if (!dm_task_add_target(dmt, tgt->offset, tgt->size, target, tgt->params))
1306                         return -EINVAL;
1307
1308                 tgt = tgt->next;
1309         } while (tgt);
1310
1311         return 0;
1312 }
1313
1314 static void _destroy_dm_targets_params(struct crypt_dm_active_device *dmd)
1315 {
1316         struct dm_target *t = &dmd->segment;
1317
1318         do {
1319                 crypt_safe_free(t->params);
1320                 t->params = NULL;
1321                 t = t->next;
1322         } while (t);
1323 }
1324
1325 static int _create_dm_targets_params(struct crypt_dm_active_device *dmd)
1326 {
1327         int r;
1328         struct dm_target *tgt = &dmd->segment;
1329
1330         do {
1331                 if (tgt->type == DM_CRYPT)
1332                         tgt->params = get_dm_crypt_params(tgt, dmd->flags);
1333                 else if (tgt->type == DM_VERITY)
1334                         tgt->params = get_dm_verity_params(tgt, dmd->flags);
1335                 else if (tgt->type == DM_INTEGRITY)
1336                         tgt->params = get_dm_integrity_params(tgt, dmd->flags);
1337                 else if (tgt->type == DM_LINEAR)
1338                         tgt->params = get_dm_linear_params(tgt, dmd->flags);
1339                 else if (tgt->type == DM_ZERO)
1340                         tgt->params = get_dm_zero_params(tgt, dmd->flags);
1341                 else {
1342                         r = -ENOTSUP;
1343                         goto err;
1344                 }
1345
1346                 if (!tgt->params) {
1347                         r = -EINVAL;
1348                         goto err;
1349                 }
1350                 tgt = tgt->next;
1351         } while (tgt);
1352
1353         return 0;
1354 err:
1355         _destroy_dm_targets_params(dmd);
1356         return r;
1357 }
1358
1359 static bool dm_device_exists(struct crypt_device *cd, const char *name)
1360 {
1361         int r = dm_status_device(cd, name);
1362         return (r >= 0 || r == -EEXIST);
1363 }
1364
1365 static int _dm_create_device(struct crypt_device *cd, const char *name, const char *type,
1366                              const char *uuid, struct crypt_dm_active_device *dmd)
1367 {
1368         struct dm_task *dmt = NULL;
1369         struct dm_info dmi;
1370         char dev_uuid[DM_UUID_LEN] = {0};
1371         int r = -EINVAL;
1372         uint32_t cookie = 0, read_ahead = 0;
1373         uint16_t udev_flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK;
1374
1375         if (dmd->flags & CRYPT_ACTIVATE_PRIVATE)
1376                 udev_flags |= CRYPT_TEMP_UDEV_FLAGS;
1377
1378         /* All devices must have DM_UUID, only resize on old device is exception */
1379         if (!dm_prepare_uuid(cd, name, type, dmd->uuid, dev_uuid, sizeof(dev_uuid)))
1380                 goto out;
1381
1382         if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
1383                 goto out;
1384
1385         if (!dm_task_set_name(dmt, name))
1386                 goto out;
1387
1388         if (!dm_task_set_uuid(dmt, dev_uuid))
1389                 goto out;
1390
1391         if (!dm_task_secure_data(dmt))
1392                 goto out;
1393         if ((dmd->flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
1394                 goto out;
1395
1396         r = _create_dm_targets_params(dmd);
1397         if (r)
1398                 goto out;
1399
1400         r = _add_dm_targets(dmt, dmd);
1401         if (r)
1402                 goto out;
1403
1404         r = -EINVAL;
1405
1406 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
1407         if (device_read_ahead(dmd->segment.data_device, &read_ahead) &&
1408             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
1409                 goto out;
1410 #endif
1411         if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
1412                 goto out;
1413
1414         if (!dm_task_run(dmt)) {
1415                 if (dm_device_exists(cd, name))
1416                         r = -EEXIST;
1417                 goto out;
1418         }
1419
1420         if (dm_task_get_info(dmt, &dmi))
1421                 r = 0;
1422
1423         if (_dm_use_udev()) {
1424                 (void)_dm_udev_wait(cookie);
1425                 cookie = 0;
1426         }
1427
1428         if (r < 0)
1429                 _dm_remove(name, 1, 0);
1430
1431 out:
1432         if (cookie && _dm_use_udev())
1433                 (void)_dm_udev_wait(cookie);
1434
1435         if (dmt)
1436                 dm_task_destroy(dmt);
1437
1438         dm_task_update_nodes();
1439
1440         /* If code just loaded target module, update versions */
1441         _dm_check_versions(cd, dmd->segment.type);
1442
1443         _destroy_dm_targets_params(dmd);
1444
1445         return r;
1446 }
1447
1448 static int _dm_resume_device(const char *name, uint32_t dmflags)
1449 {
1450         struct dm_task *dmt;
1451         int r = -EINVAL;
1452         uint32_t cookie = 0;
1453         uint16_t udev_flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK;
1454
1455         if (dmflags & DM_RESUME_PRIVATE)
1456                 udev_flags |= CRYPT_TEMP_UDEV_FLAGS;
1457
1458         if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
1459                 return r;
1460
1461         if (!dm_task_set_name(dmt, name))
1462                 goto out;
1463
1464         if ((dmflags & DM_SUSPEND_SKIP_LOCKFS) && !dm_task_skip_lockfs(dmt))
1465                 goto out;
1466
1467         if ((dmflags & DM_SUSPEND_NOFLUSH) && !dm_task_no_flush(dmt))
1468                 goto out;
1469
1470         if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
1471                 goto out;
1472
1473         if (dm_task_run(dmt))
1474                 r = 0;
1475 out:
1476         if (cookie && _dm_use_udev())
1477                 (void)_dm_udev_wait(cookie);
1478
1479         dm_task_destroy(dmt);
1480
1481         dm_task_update_nodes();
1482
1483         return r;
1484 }
1485
1486 static int _dm_reload_device(struct crypt_device *cd, const char *name,
1487                              struct crypt_dm_active_device *dmd)
1488 {
1489         int r = -EINVAL;
1490         struct dm_task *dmt = NULL;
1491         uint32_t read_ahead = 0;
1492
1493         /* All devices must have DM_UUID, only resize on old device is exception */
1494         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
1495                 goto out;
1496
1497         if (!dm_task_set_name(dmt, name))
1498                 goto out;
1499
1500         if (!dm_task_secure_data(dmt))
1501                 goto out;
1502         if ((dmd->flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
1503                 goto out;
1504
1505         r = _create_dm_targets_params(dmd);
1506         if (r)
1507                 goto out;
1508
1509         r = _add_dm_targets(dmt, dmd);
1510         if (r)
1511                 goto out;
1512
1513         r = -EINVAL;
1514
1515 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
1516         if (device_read_ahead(dmd->segment.data_device, &read_ahead) &&
1517             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
1518                 goto out;
1519 #endif
1520
1521         if (dm_task_run(dmt))
1522                 r = 0;
1523 out:
1524         if (dmt)
1525                 dm_task_destroy(dmt);
1526
1527         /* If code just loaded target module, update versions */
1528         _dm_check_versions(cd, dmd->segment.type);
1529
1530         _destroy_dm_targets_params(dmd);
1531
1532         return r;
1533 }
1534
1535 static void crypt_free_verity_params(struct crypt_params_verity *vp)
1536 {
1537         if (!vp)
1538                 return;
1539
1540         free(CONST_CAST(void*)vp->hash_name);
1541         free(CONST_CAST(void*)vp->data_device);
1542         free(CONST_CAST(void*)vp->hash_device);
1543         free(CONST_CAST(void*)vp->fec_device);
1544         free(CONST_CAST(void*)vp->salt);
1545         free(vp);
1546 }
1547
1548 static void _dm_target_free_query_path(struct crypt_device *cd, struct dm_target *tgt)
1549 {
1550         switch(tgt->type) {
1551         case DM_CRYPT:
1552                 crypt_free_volume_key(tgt->u.crypt.vk);
1553                 free(CONST_CAST(void*)tgt->u.crypt.cipher);
1554                 break;
1555         case DM_INTEGRITY:
1556                 free(CONST_CAST(void*)tgt->u.integrity.integrity);
1557                 crypt_free_volume_key(tgt->u.integrity.vk);
1558
1559                 free(CONST_CAST(void*)tgt->u.integrity.journal_integrity);
1560                 crypt_free_volume_key(tgt->u.integrity.journal_integrity_key);
1561
1562                 free(CONST_CAST(void*)tgt->u.integrity.journal_crypt);
1563                 crypt_free_volume_key(tgt->u.integrity.journal_crypt_key);
1564
1565                 device_free(cd, tgt->u.integrity.meta_device);
1566                 break;
1567         case DM_VERITY:
1568                 crypt_free_verity_params(tgt->u.verity.vp);
1569                 device_free(cd, tgt->u.verity.hash_device);
1570                 free(CONST_CAST(void*)tgt->u.verity.root_hash);
1571                 free(CONST_CAST(void*)tgt->u.verity.root_hash_sig_key_desc);
1572                 /* fall through */
1573         case DM_LINEAR:
1574                 /* fall through */
1575         case DM_ERROR:
1576                 /* fall through */
1577         case DM_ZERO:
1578                 break;
1579         default:
1580                 log_err(cd, _("Unknown dm target type."));
1581                 return;
1582         }
1583
1584         device_free(cd, tgt->data_device);
1585 }
1586
1587 static void _dm_target_erase(struct crypt_device *cd, struct dm_target *tgt)
1588 {
1589         if (tgt->direction == TARGET_QUERY)
1590                 _dm_target_free_query_path(cd, tgt);
1591
1592         if (tgt->type == DM_CRYPT)
1593                 free(CONST_CAST(void*)tgt->u.crypt.integrity);
1594 }
1595
1596 void dm_targets_free(struct crypt_device *cd, struct crypt_dm_active_device *dmd)
1597 {
1598         struct dm_target *t = &dmd->segment, *next = t->next;
1599
1600         _dm_target_erase(cd, t);
1601
1602         while (next) {
1603                 t = next;
1604                 next = t->next;
1605                 _dm_target_erase(cd, t);
1606                 free(t);
1607         }
1608
1609         memset(&dmd->segment, 0, sizeof(dmd->segment));
1610 }
1611
1612 int dm_targets_allocate(struct dm_target *first, unsigned count)
1613 {
1614         if (!first || first->next || !count)
1615                 return -EINVAL;
1616
1617         while (--count) {
1618                 first->next = crypt_zalloc(sizeof(*first));
1619                 if (!first->next)
1620                         return -ENOMEM;
1621                 first = first->next;
1622         }
1623
1624         return 0;
1625 }
1626
1627 static int check_retry(struct crypt_device *cd, uint32_t *dmd_flags, uint32_t dmt_flags)
1628 {
1629         int ret = 0;
1630
1631         /* If discard not supported try to load without discard */
1632         if ((*dmd_flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) &&
1633             !(dmt_flags & DM_DISCARDS_SUPPORTED)) {
1634                 log_dbg(cd, "Discard/TRIM is not supported");
1635                 *dmd_flags = *dmd_flags & ~CRYPT_ACTIVATE_ALLOW_DISCARDS;
1636                 ret = 1;
1637         }
1638
1639         /* If kernel keyring is not supported load key directly in dm-crypt */
1640         if ((*dmd_flags & CRYPT_ACTIVATE_KEYRING_KEY) &&
1641             !(dmt_flags & DM_KERNEL_KEYRING_SUPPORTED)) {
1642                 log_dbg(cd, "dm-crypt does not support kernel keyring");
1643                 *dmd_flags = *dmd_flags & ~CRYPT_ACTIVATE_KEYRING_KEY;
1644                 ret = 1;
1645         }
1646
1647         /* Drop performance options if not supported */
1648         if ((*dmd_flags & (CRYPT_ACTIVATE_SAME_CPU_CRYPT | CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS)) &&
1649             !(dmt_flags & (DM_SAME_CPU_CRYPT_SUPPORTED | DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED))) {
1650                 log_dbg(cd, "dm-crypt does not support performance options");
1651                 *dmd_flags = *dmd_flags & ~(CRYPT_ACTIVATE_SAME_CPU_CRYPT | CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS);
1652                 ret = 1;
1653         }
1654
1655         /* Drop no workqueue options if not supported */
1656         if ((*dmd_flags & (CRYPT_ACTIVATE_NO_READ_WORKQUEUE | CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE)) &&
1657             !(dmt_flags & DM_CRYPT_NO_WORKQUEUE_SUPPORTED)) {
1658                 log_dbg(cd, "dm-crypt does not support performance options");
1659                 *dmd_flags = *dmd_flags & ~(CRYPT_ACTIVATE_NO_READ_WORKQUEUE | CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE);
1660                 ret = 1;
1661         }
1662
1663         return ret;
1664 }
1665
1666 int dm_create_device(struct crypt_device *cd, const char *name,
1667                      const char *type,
1668                      struct crypt_dm_active_device *dmd)
1669 {
1670         uint32_t dmt_flags = 0;
1671         int r = -EINVAL;
1672
1673         if (!type || !dmd)
1674                 return -EINVAL;
1675
1676         if (dm_init_context(cd, dmd->segment.type))
1677                 return -ENOTSUP;
1678
1679         r = _dm_create_device(cd, name, type, dmd->uuid, dmd);
1680
1681         if (r < 0 && dm_flags(cd, dmd->segment.type, &dmt_flags))
1682                 goto out;
1683
1684         if (r && (dmd->segment.type == DM_CRYPT || dmd->segment.type == DM_LINEAR || dmd->segment.type == DM_ZERO) &&
1685                 check_retry(cd, &dmd->flags, dmt_flags)) {
1686                 log_dbg(cd, "Retrying open without incompatible options.");
1687                 r = _dm_create_device(cd, name, type, dmd->uuid, dmd);
1688         }
1689
1690         /*
1691          * Print warning if activating dm-crypt cipher_null device unless it's reencryption helper or
1692          * keyslot encryption helper device (LUKS1 cipher_null devices).
1693          */
1694         if (!r && !(dmd->flags & CRYPT_ACTIVATE_PRIVATE) && single_segment(dmd) && dmd->segment.type == DM_CRYPT &&
1695             crypt_is_cipher_null(dmd->segment.u.crypt.cipher))
1696                 log_dbg(cd, "Activated dm-crypt device with cipher_null. Device is not encrypted.");
1697
1698         if (r == -EINVAL &&
1699             dmd->flags & (CRYPT_ACTIVATE_SAME_CPU_CRYPT|CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS) &&
1700             !(dmt_flags & (DM_SAME_CPU_CRYPT_SUPPORTED|DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED)))
1701                 log_err(cd, _("Requested dm-crypt performance options are not supported."));
1702
1703         if (r == -EINVAL &&
1704             dmd->flags & (CRYPT_ACTIVATE_NO_READ_WORKQUEUE | CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE) &&
1705             !(dmt_flags & DM_CRYPT_NO_WORKQUEUE_SUPPORTED))
1706                 log_err(cd, _("Requested dm-crypt performance options are not supported."));
1707
1708         if (r == -EINVAL && dmd->flags & (CRYPT_ACTIVATE_IGNORE_CORRUPTION|
1709                                           CRYPT_ACTIVATE_RESTART_ON_CORRUPTION|
1710                                           CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS|
1711                                           CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE) &&
1712             !(dmt_flags & DM_VERITY_ON_CORRUPTION_SUPPORTED))
1713                 log_err(cd, _("Requested dm-verity data corruption handling options are not supported."));
1714
1715         if (r == -EINVAL && dmd->flags & CRYPT_ACTIVATE_PANIC_ON_CORRUPTION &&
1716             !(dmt_flags & DM_VERITY_PANIC_CORRUPTION_SUPPORTED))
1717                 log_err(cd, _("Requested dm-verity data corruption handling options are not supported."));
1718
1719         if (r == -EINVAL && dmd->segment.type == DM_VERITY &&
1720             dmd->segment.u.verity.fec_device && !(dmt_flags & DM_VERITY_FEC_SUPPORTED))
1721                 log_err(cd, _("Requested dm-verity FEC options are not supported."));
1722
1723         if (r == -EINVAL && dmd->segment.type == DM_CRYPT) {
1724                 if (dmd->segment.u.crypt.integrity && !(dmt_flags & DM_INTEGRITY_SUPPORTED))
1725                         log_err(cd, _("Requested data integrity options are not supported."));
1726                 if (dmd->segment.u.crypt.sector_size != SECTOR_SIZE && !(dmt_flags & DM_SECTOR_SIZE_SUPPORTED))
1727                         log_err(cd, _("Requested sector_size option is not supported."));
1728         }
1729
1730         if (r == -EINVAL && dmd->segment.type == DM_INTEGRITY && (dmd->flags & CRYPT_ACTIVATE_RECALCULATE) &&
1731             !(dmt_flags & DM_INTEGRITY_RECALC_SUPPORTED))
1732                 log_err(cd, _("Requested automatic recalculation of integrity tags is not supported."));
1733
1734         if (r == -EINVAL && dmd->segment.type == DM_INTEGRITY && (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) &&
1735             !(dmt_flags & DM_INTEGRITY_DISCARDS_SUPPORTED))
1736                 log_err(cd, _("Discard/TRIM is not supported."));
1737
1738         if (r == -EINVAL && dmd->segment.type == DM_INTEGRITY && (dmd->flags & CRYPT_ACTIVATE_NO_JOURNAL_BITMAP) &&
1739             !(dmt_flags & DM_INTEGRITY_BITMAP_SUPPORTED))
1740                 log_err(cd, _("Requested dm-integrity bitmap mode is not supported."));
1741 out:
1742         dm_exit_context();
1743         return r;
1744 }
1745
1746 int dm_reload_device(struct crypt_device *cd, const char *name,
1747                      struct crypt_dm_active_device *dmd, uint32_t dmflags, unsigned resume)
1748 {
1749         int r;
1750         uint32_t dmt_flags;
1751
1752         if (!dmd)
1753                 return -EINVAL;
1754
1755         if (dm_init_context(cd, dmd->segment.type))
1756                 return -ENOTSUP;
1757
1758         if (dm_flags(cd, DM_INTEGRITY, &dmt_flags) || !(dmt_flags & DM_INTEGRITY_RECALC_SUPPORTED))
1759                 dmd->flags &= ~CRYPT_ACTIVATE_RECALCULATE;
1760
1761         r = _dm_reload_device(cd, name, dmd);
1762
1763         if (r == -EINVAL && (dmd->segment.type == DM_CRYPT || dmd->segment.type == DM_LINEAR)) {
1764                 if ((dmd->flags & (CRYPT_ACTIVATE_SAME_CPU_CRYPT|CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS)) &&
1765                     !dm_flags(cd, DM_CRYPT, &dmt_flags) && !(dmt_flags & (DM_SAME_CPU_CRYPT_SUPPORTED | DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED)))
1766                         log_err(cd, _("Requested dm-crypt performance options are not supported."));
1767                 if ((dmd->flags & (CRYPT_ACTIVATE_NO_READ_WORKQUEUE | CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE)) &&
1768                     !dm_flags(cd, DM_CRYPT, &dmt_flags) && !(dmt_flags & DM_CRYPT_NO_WORKQUEUE_SUPPORTED))
1769                         log_err(cd, _("Requested dm-crypt performance options are not supported."));
1770                 if ((dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) &&
1771                     !dm_flags(cd, DM_CRYPT, &dmt_flags) && !(dmt_flags & DM_DISCARDS_SUPPORTED))
1772                         log_err(cd, _("Discard/TRIM is not supported."));
1773                 if ((dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) &&
1774                     !dm_flags(cd, DM_INTEGRITY, &dmt_flags) && !(dmt_flags & DM_INTEGRITY_DISCARDS_SUPPORTED))
1775                         log_err(cd, _("Discard/TRIM is not supported."));
1776         }
1777
1778         if (!r && resume)
1779                 r = _dm_resume_device(name, dmflags | act2dmflags(dmd->flags));
1780
1781         dm_exit_context();
1782         return r;
1783 }
1784
1785 static int dm_status_dmi(const char *name, struct dm_info *dmi,
1786                           const char *target, char **status_line)
1787 {
1788         struct dm_task *dmt;
1789         uint64_t start, length;
1790         char *target_type, *params = NULL;
1791         int r = -EINVAL;
1792
1793         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
1794                 goto out;
1795
1796         if (!dm_task_no_flush(dmt))
1797                 goto out;
1798
1799         if (!dm_task_set_name(dmt, name))
1800                 goto out;
1801
1802         if (!dm_task_run(dmt))
1803                 goto out;
1804
1805         if (!dm_task_get_info(dmt, dmi))
1806                 goto out;
1807
1808         if (!dmi->exists) {
1809                 r = -ENODEV;
1810                 goto out;
1811         }
1812
1813         r = -EEXIST;
1814         dm_get_next_target(dmt, NULL, &start, &length,
1815                            &target_type, &params);
1816
1817         if (!target_type || start != 0)
1818                 goto out;
1819
1820         if (target && strcmp(target_type, target))
1821                 goto out;
1822
1823         /* for target == NULL check all supported */
1824         if (!target && (strcmp(target_type, DM_CRYPT_TARGET) &&
1825                         strcmp(target_type, DM_VERITY_TARGET) &&
1826                         strcmp(target_type, DM_INTEGRITY_TARGET) &&
1827                         strcmp(target_type, DM_LINEAR_TARGET) &&
1828                         strcmp(target_type, DM_ZERO_TARGET) &&
1829                         strcmp(target_type, DM_ERROR_TARGET)))
1830                 goto out;
1831         r = 0;
1832 out:
1833         if (!r && status_line && !(*status_line = strdup(params)))
1834                 r = -ENOMEM;
1835
1836         if (dmt)
1837                 dm_task_destroy(dmt);
1838
1839         return r;
1840 }
1841
1842 int dm_status_device(struct crypt_device *cd, const char *name)
1843 {
1844         int r;
1845         struct dm_info dmi;
1846         struct stat st;
1847
1848         /* libdevmapper is too clever and handles
1849          * path argument differently with error.
1850          * Fail early here if parameter is non-existent path.
1851          */
1852         if (strchr(name, '/') && stat(name, &st) < 0)
1853                 return -ENODEV;
1854
1855         if (dm_init_context(cd, DM_UNKNOWN))
1856                 return -ENOTSUP;
1857         r = dm_status_dmi(name, &dmi, NULL, NULL);
1858         dm_exit_context();
1859
1860         if (r < 0)
1861                 return r;
1862
1863         return (dmi.open_count > 0) ? 1 : 0;
1864 }
1865
1866 int dm_status_suspended(struct crypt_device *cd, const char *name)
1867 {
1868         int r;
1869         struct dm_info dmi;
1870
1871         if (dm_init_context(cd, DM_UNKNOWN))
1872                 return -ENOTSUP;
1873         r = dm_status_dmi(name, &dmi, NULL, NULL);
1874         dm_exit_context();
1875
1876         if (r < 0)
1877                 return r;
1878
1879         return dmi.suspended ? 1 : 0;
1880 }
1881
1882 static int _dm_status_verity_ok(struct crypt_device *cd, const char *name)
1883 {
1884         int r;
1885         struct dm_info dmi;
1886         char *status_line = NULL;
1887
1888         r = dm_status_dmi(name, &dmi, DM_VERITY_TARGET, &status_line);
1889         if (r < 0 || !status_line) {
1890                 free(status_line);
1891                 return r;
1892         }
1893
1894         log_dbg(cd, "Verity volume %s status is %s.", name, status_line ?: "");
1895         r = status_line[0] == 'V' ? 1 : 0;
1896         free(status_line);
1897
1898         return r;
1899 }
1900
1901 int dm_status_verity_ok(struct crypt_device *cd, const char *name)
1902 {
1903         int r;
1904
1905         if (dm_init_context(cd, DM_VERITY))
1906                 return -ENOTSUP;
1907         r = _dm_status_verity_ok(cd, name);
1908         dm_exit_context();
1909         return r;
1910 }
1911
1912 int dm_status_integrity_failures(struct crypt_device *cd, const char *name, uint64_t *count)
1913 {
1914         int r;
1915         struct dm_info dmi;
1916         char *status_line = NULL;
1917
1918         if (dm_init_context(cd, DM_INTEGRITY))
1919                 return -ENOTSUP;
1920
1921         r = dm_status_dmi(name, &dmi, DM_INTEGRITY_TARGET, &status_line);
1922         if (r < 0 || !status_line) {
1923                 free(status_line);
1924                 dm_exit_context();
1925                 return r;
1926         }
1927
1928         log_dbg(cd, "Integrity volume %s failure status is %s.", name, status_line ?: "");
1929         *count = strtoull(status_line, NULL, 10);
1930         free(status_line);
1931         dm_exit_context();
1932
1933         return 0;
1934 }
1935
1936 /* FIXME use hex wrapper, user val wrappers for line parsing */
1937 static int _dm_target_query_crypt(struct crypt_device *cd, uint32_t get_flags,
1938                                   char *params, struct dm_target *tgt,
1939                                   uint32_t *act_flags)
1940 {
1941         uint64_t val64;
1942         char *rcipher, *rintegrity, *key_, *rdevice, *endp, buffer[3], *arg, *key_desc;
1943         unsigned int i, val;
1944         int r;
1945         size_t key_size;
1946         struct device *data_device = NULL;
1947         char *cipher = NULL, *integrity = NULL;
1948         struct volume_key *vk = NULL;
1949
1950         tgt->type = DM_CRYPT;
1951         tgt->direction = TARGET_QUERY;
1952         tgt->u.crypt.sector_size = SECTOR_SIZE;
1953
1954         r = -EINVAL;
1955
1956         rcipher = strsep(&params, " ");
1957         rintegrity = NULL;
1958
1959         /* skip */
1960         key_ = strsep(&params, " ");
1961         if (!params)
1962                 goto err;
1963         val64 = strtoull(params, &params, 10);
1964         if (*params != ' ')
1965                 goto err;
1966         params++;
1967
1968         tgt->u.crypt.iv_offset = val64;
1969
1970         /* device */
1971         rdevice = strsep(&params, " ");
1972         if (get_flags & DM_ACTIVE_DEVICE) {
1973                 arg = crypt_lookup_dev(rdevice);
1974                 r = device_alloc(cd, &data_device, arg);
1975                 free(arg);
1976                 if (r < 0 && r != -ENOTBLK)
1977                         goto err;
1978         }
1979
1980         r = -EINVAL;
1981
1982         /*offset */
1983         if (!params)
1984                 goto err;
1985         val64 = strtoull(params, &params, 10);
1986         tgt->u.crypt.offset = val64;
1987
1988         tgt->u.crypt.tag_size = 0;
1989
1990         /* Features section, available since crypt target version 1.11 */
1991         if (*params) {
1992                 if (*params != ' ')
1993                         goto err;
1994                 params++;
1995
1996                 /* Number of arguments */
1997                 val64 = strtoull(params, &params, 10);
1998                 if (*params != ' ')
1999                         goto err;
2000                 params++;
2001
2002                 for (i = 0; i < val64; i++) {
2003                         if (!params)
2004                                 goto err;
2005                         arg = strsep(&params, " ");
2006                         if (!strcasecmp(arg, "allow_discards"))
2007                                 *act_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
2008                         else if (!strcasecmp(arg, "same_cpu_crypt"))
2009                                 *act_flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
2010                         else if (!strcasecmp(arg, "submit_from_crypt_cpus"))
2011                                 *act_flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
2012                         else if (!strcasecmp(arg, "no_read_workqueue"))
2013                                 *act_flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
2014                         else if (!strcasecmp(arg, "no_write_workqueue"))
2015                                 *act_flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
2016                         else if (!strcasecmp(arg, "iv_large_sectors"))
2017                                 *act_flags |= CRYPT_ACTIVATE_IV_LARGE_SECTORS;
2018                         else if (sscanf(arg, "integrity:%u:", &val) == 1) {
2019                                 tgt->u.crypt.tag_size = val;
2020                                 rintegrity = strchr(arg + strlen("integrity:"), ':');
2021                                 if (!rintegrity)
2022                                         goto err;
2023                                 rintegrity++;
2024                         } else if (sscanf(arg, "sector_size:%u", &val) == 1) {
2025                                 tgt->u.crypt.sector_size = val;
2026                         } else /* unknown option */
2027                                 goto err;
2028                 }
2029
2030                 /* All parameters should be processed */
2031                 if (params)
2032                         goto err;
2033         }
2034
2035         /* cipher */
2036         if (get_flags & DM_ACTIVE_CRYPT_CIPHER) {
2037                 r = cipher_dm2c(CONST_CAST(char**)&cipher,
2038                                 CONST_CAST(char**)&integrity,
2039                                 rcipher, rintegrity);
2040                 if (r < 0)
2041                         goto err;
2042         }
2043
2044         r = -EINVAL;
2045
2046         if (key_[0] == ':')
2047                 *act_flags |= CRYPT_ACTIVATE_KEYRING_KEY;
2048
2049         if (get_flags & DM_ACTIVE_CRYPT_KEYSIZE) {
2050                 /* we will trust kernel the key_string is in expected format */
2051                 if (key_[0] == ':') {
2052                         if (sscanf(key_ + 1, "%zu", &key_size) != 1)
2053                                 goto err;
2054                 } else
2055                         key_size = strlen(key_) / 2;
2056
2057                 vk = crypt_alloc_volume_key(key_size, NULL);
2058                 if (!vk) {
2059                         r = -ENOMEM;
2060                         goto err;
2061                 }
2062
2063                 if (get_flags & DM_ACTIVE_CRYPT_KEY) {
2064                         if (key_[0] == ':') {
2065                                 /* :<key_size>:<key_type>:<key_description> */
2066                                 key_desc = NULL;
2067                                 endp = strpbrk(key_ + 1, ":");
2068                                 if (endp)
2069                                         key_desc = strpbrk(endp + 1, ":");
2070                                 if (!key_desc) {
2071                                         r = -ENOMEM;
2072                                         goto err;
2073                                 }
2074                                 key_desc++;
2075                                 crypt_volume_key_set_description(vk, key_desc);
2076                         } else {
2077                                 buffer[2] = '\0';
2078                                 for(i = 0; i < vk->keylength; i++) {
2079                                         memcpy(buffer, &key_[i * 2], 2);
2080                                         vk->key[i] = strtoul(buffer, &endp, 16);
2081                                         if (endp != &buffer[2]) {
2082                                                 r = -EINVAL;
2083                                                 goto err;
2084                                         }
2085                                 }
2086                         }
2087                 }
2088         }
2089         memset(key_, 0, strlen(key_));
2090
2091         if (cipher)
2092                 tgt->u.crypt.cipher = cipher;
2093         if (integrity)
2094                 tgt->u.crypt.integrity = integrity;
2095         if (data_device)
2096                 tgt->data_device = data_device;
2097         if (vk)
2098                 tgt->u.crypt.vk = vk;
2099         return 0;
2100 err:
2101         free(cipher);
2102         free(integrity);
2103         device_free(cd, data_device);
2104         crypt_free_volume_key(vk);
2105         return r;
2106 }
2107
2108 static int _dm_target_query_verity(struct crypt_device *cd,
2109                                    uint32_t get_flags,
2110                                    char *params,
2111                                    struct dm_target *tgt,
2112                                    uint32_t *act_flags)
2113 {
2114         struct crypt_params_verity *vp = NULL;
2115         uint32_t val32;
2116         uint64_t val64;
2117         ssize_t len;
2118         char *str, *str2, *arg;
2119         unsigned int i, features;
2120         int r;
2121         struct device *data_device = NULL, *hash_device = NULL, *fec_device = NULL;
2122         char *hash_name = NULL, *root_hash = NULL, *salt = NULL, *fec_dev_str = NULL;
2123         char *root_hash_sig_key_desc = NULL;
2124
2125         if (get_flags & DM_ACTIVE_VERITY_PARAMS) {
2126                 vp = crypt_zalloc(sizeof(*vp));
2127                 if (!vp)
2128                         return -ENOMEM;
2129         }
2130
2131         tgt->type = DM_VERITY;
2132         tgt->direction = TARGET_QUERY;
2133         tgt->u.verity.vp = vp;
2134
2135         /* version */
2136         val32 = strtoul(params, &params, 10);
2137         if (*params != ' ')
2138                 return -EINVAL;
2139         if (vp)
2140                 vp->hash_type = val32;
2141         params++;
2142
2143         /* data device */
2144         str = strsep(&params, " ");
2145         if (!params)
2146                 return -EINVAL;
2147         if (get_flags & DM_ACTIVE_DEVICE) {
2148                 str2 = crypt_lookup_dev(str);
2149                 r = device_alloc(cd, &data_device, str2);
2150                 free(str2);
2151                 if (r < 0 && r != -ENOTBLK)
2152                         return r;
2153         }
2154
2155         r = -EINVAL;
2156
2157         /* hash device */
2158         str = strsep(&params, " ");
2159         if (!params)
2160                 goto err;
2161         if (get_flags & DM_ACTIVE_VERITY_HASH_DEVICE) {
2162                 str2 = crypt_lookup_dev(str);
2163                 r = device_alloc(cd, &hash_device, str2);
2164                 free(str2);
2165                 if (r < 0 && r != -ENOTBLK)
2166                         goto err;
2167         }
2168
2169         r = -EINVAL;
2170
2171         /* data block size*/
2172         val32 = strtoul(params, &params, 10);
2173         if (*params != ' ')
2174                 goto err;
2175         if (vp)
2176                 vp->data_block_size = val32;
2177         params++;
2178
2179         /* hash block size */
2180         val32 = strtoul(params, &params, 10);
2181         if (*params != ' ')
2182                 goto err;
2183         if (vp)
2184                 vp->hash_block_size = val32;
2185         params++;
2186
2187         /* data blocks */
2188         val64 = strtoull(params, &params, 10);
2189         if (*params != ' ')
2190                 goto err;
2191         if (vp)
2192                 vp->data_size = val64;
2193         params++;
2194
2195         /* hash start */
2196         val64 = strtoull(params, &params, 10);
2197         if (*params != ' ')
2198                 goto err;
2199         tgt->u.verity.hash_offset = val64;
2200         params++;
2201
2202         /* hash algorithm */
2203         str = strsep(&params, " ");
2204         if (!params)
2205                 goto err;
2206         if (vp) {
2207                 hash_name = strdup(str);
2208                 if (!hash_name) {
2209                         r = -ENOMEM;
2210                         goto err;
2211                 }
2212         }
2213
2214         /* root digest */
2215         str = strsep(&params, " ");
2216         if (!params)
2217                 goto err;
2218         len = crypt_hex_to_bytes(str, &str2, 0);
2219         if (len < 0) {
2220                 r = len;
2221                 goto err;
2222         }
2223         tgt->u.verity.root_hash_size = len;
2224         if (get_flags & DM_ACTIVE_VERITY_ROOT_HASH)
2225                 root_hash = str2;
2226         else
2227                 free(str2);
2228
2229         /* salt */
2230         str = strsep(&params, " ");
2231         if (vp) {
2232                 if (!strcmp(str, "-")) {
2233                         vp->salt_size = 0;
2234                         vp->salt = NULL;
2235                 } else {
2236                         len = crypt_hex_to_bytes(str, &str2, 0);
2237                         if (len < 0) {
2238                                 r = len;
2239                                 goto err;
2240                         }
2241                         vp->salt_size = len;
2242                         salt = str2;
2243                 }
2244         }
2245
2246         r = -EINVAL;
2247
2248         /* Features section, available since verity target version 1.3 */
2249         if (params) {
2250                 /* Number of arguments */
2251                 val64 = strtoull(params, &params, 10);
2252                 if (*params != ' ')
2253                         goto err;
2254                 params++;
2255
2256                 features = (int)val64;
2257                 for (i = 0; i < features; i++) {
2258                         r = -EINVAL;
2259                         if (!params)
2260                                 goto err;
2261                         arg = strsep(&params, " ");
2262                         if (!strcasecmp(arg, "ignore_corruption"))
2263                                 *act_flags |= CRYPT_ACTIVATE_IGNORE_CORRUPTION;
2264                         else if (!strcasecmp(arg, "restart_on_corruption"))
2265                                 *act_flags |= CRYPT_ACTIVATE_RESTART_ON_CORRUPTION;
2266                         else if (!strcasecmp(arg, "panic_on_corruption"))
2267                                 *act_flags |= CRYPT_ACTIVATE_PANIC_ON_CORRUPTION;
2268                         else if (!strcasecmp(arg, "ignore_zero_blocks"))
2269                                 *act_flags |= CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS;
2270                         else if (!strcasecmp(arg, "check_at_most_once"))
2271                                 *act_flags |= CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE;
2272                         else if (!strcasecmp(arg, "use_fec_from_device")) {
2273                                 str = strsep(&params, " ");
2274                                 str2 = crypt_lookup_dev(str);
2275                                 if (get_flags & DM_ACTIVE_VERITY_HASH_DEVICE) {
2276                                         r = device_alloc(cd, &fec_device, str2);
2277                                         if (r < 0 && r != -ENOTBLK) {
2278                                                 free(str2);
2279                                                 goto err;
2280                                         }
2281                                 }
2282                                 if (vp) {
2283                                         free(fec_dev_str);
2284                                         fec_dev_str = str2;
2285                                 } else
2286                                         free(str2);
2287                                 i++;
2288                         } else if (!strcasecmp(arg, "fec_start")) {
2289                                 val64 = strtoull(params, &params, 10);
2290                                 if (*params)
2291                                         params++;
2292                                 tgt->u.verity.fec_offset = val64;
2293                                 if (vp)
2294                                         vp->fec_area_offset = val64 * vp->hash_block_size;
2295                                 i++;
2296                         } else if (!strcasecmp(arg, "fec_blocks")) {
2297                                 val64 = strtoull(params, &params, 10);
2298                                 if (*params)
2299                                         params++;
2300                                 tgt->u.verity.fec_blocks = val64;
2301                                 i++;
2302                         } else if (!strcasecmp(arg, "fec_roots")) {
2303                                 val32 = strtoul(params, &params, 10);
2304                                 if (*params)
2305                                         params++;
2306                                 if (vp)
2307                                         vp->fec_roots = val32;
2308                                 i++;
2309                         } else if (!strcasecmp(arg, "root_hash_sig_key_desc")) {
2310                                 str = strsep(&params, " ");
2311                                 if (!str)
2312                                         goto err;
2313                                 if (!root_hash_sig_key_desc) {
2314                                         root_hash_sig_key_desc = strdup(str);
2315                                         if (!root_hash_sig_key_desc) {
2316                                                 r = -ENOMEM;
2317                                                 goto err;
2318                                         }
2319                                 }
2320                                 i++;
2321                                 if (vp)
2322                                         vp->flags |= CRYPT_VERITY_ROOT_HASH_SIGNATURE;
2323                         } else /* unknown option */
2324                                 goto err;
2325                 }
2326
2327                 /* All parameters should be processed */
2328                 if (params && *params) {
2329                         r = -EINVAL;
2330                         goto err;
2331                 }
2332         }
2333
2334         if (data_device)
2335                 tgt->data_device = data_device;
2336         if (hash_device)
2337                 tgt->u.verity.hash_device = hash_device;
2338         if (fec_device)
2339                 tgt->u.verity.fec_device = fec_device;
2340         if (root_hash)
2341                 tgt->u.verity.root_hash = root_hash;
2342         if (vp && hash_name)
2343                 vp->hash_name = hash_name;
2344         if (vp && salt)
2345                 vp->salt = salt;
2346         if (vp && fec_dev_str)
2347                 vp->fec_device = fec_dev_str;
2348         if (root_hash_sig_key_desc)
2349                 tgt->u.verity.root_hash_sig_key_desc = root_hash_sig_key_desc;
2350
2351         return 0;
2352 err:
2353         device_free(cd, data_device);
2354         device_free(cd, hash_device);
2355         device_free(cd, fec_device);
2356         free(root_hash_sig_key_desc);
2357         free(root_hash);
2358         free(hash_name);
2359         free(salt);
2360         free(fec_dev_str);
2361         free(vp);
2362         return r;
2363 }
2364
2365 static int _dm_target_query_integrity(struct crypt_device *cd,
2366                              uint32_t get_flags,
2367                              char *params,
2368                              struct dm_target *tgt,
2369                              uint32_t *act_flags)
2370 {
2371         uint32_t val32;
2372         uint64_t val64;
2373         char c, *str, *str2, *arg;
2374         unsigned int i, features, val;
2375         ssize_t len;
2376         int r;
2377         struct device *data_device = NULL, *meta_device = NULL;
2378         char *integrity = NULL, *journal_crypt = NULL, *journal_integrity = NULL;
2379         struct volume_key *vk = NULL;
2380
2381         tgt->type = DM_INTEGRITY;
2382         tgt->direction = TARGET_QUERY;
2383
2384         /* data device */
2385         str = strsep(&params, " ");
2386         if (get_flags & DM_ACTIVE_DEVICE) {
2387                 str2 = crypt_lookup_dev(str);
2388                 r = device_alloc(cd, &data_device, str2);
2389                 free(str2);
2390                 if (r < 0 && r != -ENOTBLK)
2391                         return r;
2392         }
2393
2394         r = -EINVAL;
2395
2396         /*offset */
2397         if (!params)
2398                 goto err;
2399         val64 = strtoull(params, &params, 10);
2400         if (!*params || *params != ' ')
2401                 goto err;
2402         tgt->u.integrity.offset = val64;
2403
2404         /* tag size*/
2405         val32 = strtoul(params, &params, 10);
2406         tgt->u.integrity.tag_size = val32;
2407         if (!*params || *params != ' ')
2408                 goto err;
2409
2410         /* journal */
2411         c = toupper(*(++params));
2412         if (!*params || *(++params) != ' ' || (c != 'D' && c != 'J' && c != 'R' && c != 'B'))
2413                 goto err;
2414         if (c == 'D')
2415                 *act_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
2416         if (c == 'R')
2417                 *act_flags |= CRYPT_ACTIVATE_RECOVERY;
2418         if (c == 'B') {
2419                 *act_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
2420                 *act_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
2421         }
2422
2423         tgt->u.integrity.sector_size = SECTOR_SIZE;
2424
2425         /* Features section */
2426         if (params) {
2427                 /* Number of arguments */
2428                 val64 = strtoull(params, &params, 10);
2429                 if (*params != ' ')
2430                         goto err;
2431                 params++;
2432
2433                 features = (int)val64;
2434                 for (i = 0; i < features; i++) {
2435                         r = -EINVAL;
2436                         if (!params)
2437                                 goto err;
2438                         arg = strsep(&params, " ");
2439                         if (sscanf(arg, "journal_sectors:%u", &val) == 1)
2440                                 tgt->u.integrity.journal_size = val * SECTOR_SIZE;
2441                         else if (sscanf(arg, "journal_watermark:%u", &val) == 1)
2442                                 tgt->u.integrity.journal_watermark = val;
2443                         else if (sscanf(arg, "sectors_per_bit:%" PRIu64, &val64) == 1) {
2444                                 if (val64 > UINT_MAX)
2445                                         goto err;
2446                                 /* overloaded value for bitmap mode */
2447                                 tgt->u.integrity.journal_watermark = (unsigned int)val64;
2448                         } else if (sscanf(arg, "commit_time:%u", &val) == 1)
2449                                 tgt->u.integrity.journal_commit_time = val;
2450                         else if (sscanf(arg, "bitmap_flush_interval:%u", &val) == 1)
2451                                 /* overloaded value for bitmap mode */
2452                                 tgt->u.integrity.journal_commit_time = val;
2453                         else if (sscanf(arg, "interleave_sectors:%u", &val) == 1)
2454                                 tgt->u.integrity.interleave_sectors = val;
2455                         else if (sscanf(arg, "block_size:%u", &val) == 1)
2456                                 tgt->u.integrity.sector_size = val;
2457                         else if (sscanf(arg, "buffer_sectors:%u", &val) == 1)
2458                                 tgt->u.integrity.buffer_sectors = val;
2459                         else if (!strncmp(arg, "internal_hash:", 14) && !integrity) {
2460                                 str = &arg[14];
2461                                 arg = strsep(&str, ":");
2462                                 if (get_flags & DM_ACTIVE_INTEGRITY_PARAMS) {
2463                                         integrity = strdup(arg);
2464                                         if (!integrity) {
2465                                                 r = -ENOMEM;
2466                                                 goto err;
2467                                         }
2468                                 }
2469
2470                                 if (str) {
2471                                         len = crypt_hex_to_bytes(str, &str2, 1);
2472                                         if (len < 0) {
2473                                                 r = len;
2474                                                 goto err;
2475                                         }
2476
2477                                         r = 0;
2478                                         if (get_flags & DM_ACTIVE_CRYPT_KEY) {
2479                                                 vk = crypt_alloc_volume_key(len, str2);
2480                                                 if (!vk)
2481                                                         r = -ENOMEM;
2482                                         } else if (get_flags & DM_ACTIVE_CRYPT_KEYSIZE) {
2483                                                 vk = crypt_alloc_volume_key(len, NULL);
2484                                                 if (!vk)
2485                                                         r = -ENOMEM;
2486                                         }
2487                                         crypt_safe_free(str2);
2488                                         if (r < 0)
2489                                                 goto err;
2490                                 }
2491                         } else if (!strncmp(arg, "meta_device:", 12) && !meta_device) {
2492                                 if (get_flags & DM_ACTIVE_DEVICE) {
2493                                         str = crypt_lookup_dev(&arg[12]);
2494                                         r = device_alloc(cd, &meta_device, str);
2495                                         free(str);
2496                                         if (r < 0 && r != -ENOTBLK)
2497                                                 goto err;
2498                                 }
2499                         } else if (!strncmp(arg, "journal_crypt:", 14) && !journal_crypt) {
2500                                 str = &arg[14];
2501                                 arg = strsep(&str, ":");
2502                                 if (get_flags & DM_ACTIVE_INTEGRITY_PARAMS) {
2503                                         journal_crypt = strdup(arg);
2504                                         if (!journal_crypt) {
2505                                                 r = -ENOMEM;
2506                                                 goto err;
2507                                         }
2508                                 }
2509                         } else if (!strncmp(arg, "journal_mac:", 12) && !journal_integrity) {
2510                                 str = &arg[12];
2511                                 arg = strsep(&str, ":");
2512                                 if (get_flags & DM_ACTIVE_INTEGRITY_PARAMS) {
2513                                         journal_integrity = strdup(arg);
2514                                         if (!journal_integrity) {
2515                                                 r = -ENOMEM;
2516                                                 goto err;
2517                                         }
2518                                 }
2519                         } else if (!strcmp(arg, "recalculate")) {
2520                                 *act_flags |= CRYPT_ACTIVATE_RECALCULATE;
2521                         } else if (!strcmp(arg, "fix_padding")) {
2522                                 tgt->u.integrity.fix_padding = true;
2523                         } else if (!strcmp(arg, "fix_hmac")) {
2524                                 tgt->u.integrity.fix_hmac = true;
2525                         } else if (!strcmp(arg, "legacy_recalculate")) {
2526                                 tgt->u.integrity.legacy_recalc = true;
2527                         } else if (!strcmp(arg, "allow_discards")) {
2528                                 *act_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
2529                         } else /* unknown option */
2530                                 goto err;
2531                 }
2532
2533                 /* All parameters should be processed */
2534                 if (params && *params) {
2535                         r = -EINVAL;
2536                         goto err;
2537                 }
2538         }
2539
2540         if (data_device)
2541                 tgt->data_device = data_device;
2542         if (meta_device)
2543                 tgt->u.integrity.meta_device = meta_device;
2544         if (integrity)
2545                 tgt->u.integrity.integrity = integrity;
2546         if (journal_crypt)
2547                 tgt->u.integrity.journal_crypt = journal_crypt;
2548         if (journal_integrity)
2549                 tgt->u.integrity.journal_integrity = journal_integrity;
2550         if (vk)
2551                 tgt->u.integrity.vk = vk;
2552         return 0;
2553 err:
2554         device_free(cd, data_device);
2555         device_free(cd, meta_device);
2556         free(integrity);
2557         free(journal_crypt);
2558         free(journal_integrity);
2559         crypt_free_volume_key(vk);
2560         return r;
2561 }
2562
2563 static int _dm_target_query_linear(struct crypt_device *cd, struct dm_target *tgt,
2564                                    uint32_t get_flags, char *params)
2565 {
2566         uint64_t val64;
2567         char *rdevice, *arg;
2568         int r;
2569         struct device *device = NULL;
2570
2571         /* device */
2572         rdevice = strsep(&params, " ");
2573         if (get_flags & DM_ACTIVE_DEVICE) {
2574                 arg = crypt_lookup_dev(rdevice);
2575                 r = device_alloc(cd, &device, arg);
2576                 free(arg);
2577                 if (r < 0 && r != -ENOTBLK)
2578                         return r;
2579         }
2580
2581         r = -EINVAL;
2582
2583         /*offset */
2584         if (!params)
2585                 goto err;
2586         val64 = strtoull(params, &params, 10);
2587
2588         /* params should be empty now */
2589         if (*params)
2590                 goto err;
2591
2592         tgt->type = DM_LINEAR;
2593         tgt->direction = TARGET_QUERY;
2594         tgt->data_device = device;
2595         tgt->u.linear.offset = val64;
2596
2597         return 0;
2598 err:
2599         device_free(cd, device);
2600         return r;
2601 }
2602
2603 static int _dm_target_query_error(struct crypt_device *cd, struct dm_target *tgt)
2604 {
2605         tgt->type = DM_ERROR;
2606         tgt->direction = TARGET_QUERY;
2607
2608         return 0;
2609 }
2610
2611 static int _dm_target_query_zero(struct crypt_device *cd, struct dm_target *tgt)
2612 {
2613         tgt->type = DM_ZERO;
2614         tgt->direction = TARGET_QUERY;
2615
2616         return 0;
2617 }
2618
2619 /*
2620  * on error retval has to be negative
2621  *
2622  * also currently any _dm_target_query fn does not perform cleanup on error
2623  */
2624 static int dm_target_query(struct crypt_device *cd, struct dm_target *tgt, const uint64_t *start,
2625                     const uint64_t *length, const char *target_type,
2626                     char *params, uint32_t get_flags, uint32_t *act_flags)
2627 {
2628         int r = -ENOTSUP;
2629
2630         if (!strcmp(target_type, DM_CRYPT_TARGET))
2631                 r = _dm_target_query_crypt(cd, get_flags, params, tgt, act_flags);
2632         else if (!strcmp(target_type, DM_VERITY_TARGET))
2633                 r = _dm_target_query_verity(cd, get_flags, params, tgt, act_flags);
2634         else if (!strcmp(target_type, DM_INTEGRITY_TARGET))
2635                 r = _dm_target_query_integrity(cd, get_flags, params, tgt, act_flags);
2636         else if (!strcmp(target_type, DM_LINEAR_TARGET))
2637                 r = _dm_target_query_linear(cd, tgt, get_flags, params);
2638         else if (!strcmp(target_type, DM_ERROR_TARGET))
2639                 r = _dm_target_query_error(cd, tgt);
2640         else if (!strcmp(target_type, DM_ZERO_TARGET))
2641                 r = _dm_target_query_zero(cd, tgt);
2642
2643         if (!r) {
2644                 tgt->offset = *start;
2645                 tgt->size = *length;
2646         }
2647
2648         return r;
2649 }
2650
2651 static int _dm_query_device(struct crypt_device *cd, const char *name,
2652                     uint32_t get_flags, struct crypt_dm_active_device *dmd)
2653 {
2654         struct dm_target *t;
2655         struct dm_task *dmt;
2656         struct dm_info dmi;
2657         uint64_t start, length;
2658         char *target_type, *params;
2659         const char *tmp_uuid;
2660         void *next = NULL;
2661         int r = -EINVAL;
2662
2663         t = &dmd->segment;
2664
2665         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
2666                 return r;
2667         if (!dm_task_secure_data(dmt))
2668                 goto out;
2669         if (!dm_task_set_name(dmt, name))
2670                 goto out;
2671         r = -ENODEV;
2672         if (!dm_task_run(dmt))
2673                 goto out;
2674
2675         r = -EINVAL;
2676         if (!dm_task_get_info(dmt, &dmi))
2677                 goto out;
2678
2679         if (!dmi.exists) {
2680                 r = -ENODEV;
2681                 goto out;
2682         }
2683
2684         if (dmi.target_count <= 0) {
2685                 r = -EINVAL;
2686                 goto out;
2687         }
2688
2689         /* Never allow to return empty key */
2690         if ((get_flags & DM_ACTIVE_CRYPT_KEY) && dmi.suspended) {
2691                 log_dbg(cd, "Cannot read volume key while suspended.");
2692                 r = -EINVAL;
2693                 goto out;
2694         }
2695
2696         r = dm_targets_allocate(&dmd->segment, dmi.target_count);
2697         if (r)
2698                 goto out;
2699
2700         do {
2701                 next = dm_get_next_target(dmt, next, &start, &length,
2702                                           &target_type, &params);
2703
2704                 r = dm_target_query(cd, t, &start, &length, target_type, params, get_flags, &dmd->flags);
2705                 if (!r && t->type == DM_VERITY) {
2706                         r = _dm_status_verity_ok(cd, name);
2707                         if (r == 0)
2708                                 dmd->flags |= CRYPT_ACTIVATE_CORRUPTED;
2709                 }
2710
2711                 if (r < 0) {
2712                         if (r != -ENOTSUP)
2713                                 log_err(cd, _("Failed to query dm-%s segment."), target_type);
2714                         goto out;
2715                 }
2716
2717                 dmd->size += length;
2718                 t = t->next;
2719         } while (next && t);
2720
2721         if (dmi.read_only)
2722                 dmd->flags |= CRYPT_ACTIVATE_READONLY;
2723
2724         if (dmi.suspended)
2725                 dmd->flags |= CRYPT_ACTIVATE_SUSPENDED;
2726
2727         tmp_uuid = dm_task_get_uuid(dmt);
2728         if (!tmp_uuid)
2729                 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
2730         else if (get_flags & DM_ACTIVE_UUID) {
2731                 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
2732                         dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
2733         }
2734
2735         dmd->holders = 0;
2736 #if (HAVE_DECL_DM_DEVICE_HAS_HOLDERS && HAVE_DECL_DM_DEVICE_HAS_MOUNTED_FS)
2737         if (get_flags & DM_ACTIVE_HOLDERS)
2738                 dmd->holders = (dm_device_has_mounted_fs(dmi.major, dmi.minor) ||
2739                                 dm_device_has_holders(dmi.major, dmi.minor));
2740 #endif
2741
2742         r = (dmi.open_count > 0);
2743 out:
2744         if (dmt)
2745                 dm_task_destroy(dmt);
2746
2747         if (r < 0)
2748                 dm_targets_free(cd, dmd);
2749
2750         return r;
2751 }
2752
2753 int dm_query_device(struct crypt_device *cd, const char *name,
2754                     uint32_t get_flags, struct crypt_dm_active_device *dmd)
2755 {
2756         int r;
2757
2758         if (!dmd)
2759                 return -EINVAL;
2760
2761         memset(dmd, 0, sizeof(*dmd));
2762
2763         if (dm_init_context(cd, DM_UNKNOWN))
2764                 return -ENOTSUP;
2765
2766         r = _dm_query_device(cd, name, get_flags, dmd);
2767
2768         dm_exit_context();
2769         return r;
2770 }
2771
2772 static int _process_deps(struct crypt_device *cd, const char *prefix, struct dm_deps *deps, char **names, size_t names_offset, size_t names_length)
2773 {
2774 #if HAVE_DECL_DM_DEVICE_GET_NAME
2775         struct crypt_dm_active_device dmd;
2776         char dmname[PATH_MAX];
2777         unsigned i;
2778         int r, major, minor, count = 0;
2779
2780         if (!prefix || !deps)
2781                 return -EINVAL;
2782
2783         for (i = 0; i < deps->count; i++) {
2784                 major = major(deps->device[i]);
2785                 if (!dm_is_dm_major(major))
2786                         continue;
2787
2788                 minor = minor(deps->device[i]);
2789                 if (!dm_device_get_name(major, minor, 0, dmname, PATH_MAX))
2790                         return -EINVAL;
2791
2792                 memset(&dmd, 0, sizeof(dmd));
2793                 r = _dm_query_device(cd, dmname, DM_ACTIVE_UUID, &dmd);
2794                 if (r < 0)
2795                         continue;
2796
2797                 if (!dmd.uuid ||
2798                     strncmp(prefix, dmd.uuid, strlen(prefix)) ||
2799                     crypt_string_in(dmname, names, names_length))
2800                         *dmname = '\0';
2801
2802                 dm_targets_free(cd, &dmd);
2803                 free(CONST_CAST(void*)dmd.uuid);
2804
2805                 if ((size_t)count >= (names_length - names_offset))
2806                         return -ENOMEM;
2807
2808                 if (*dmname && !(names[names_offset + count++] = strdup(dmname)))
2809                         return -ENOMEM;
2810         }
2811
2812         return count;
2813 #else
2814         return -EINVAL;
2815 #endif
2816 }
2817
2818 int dm_device_deps(struct crypt_device *cd, const char *name, const char *prefix, char **names, size_t names_length)
2819 {
2820         struct dm_task *dmt;
2821         struct dm_info dmi;
2822         struct dm_deps *deps;
2823         int r = -EINVAL;
2824         size_t i, last = 0, offset = 0;
2825
2826         if (!name || !names_length || !names)
2827                 return -EINVAL;
2828
2829         if (dm_init_context(cd, DM_UNKNOWN))
2830                 return -ENOTSUP;
2831
2832         while (name) {
2833                 if (!(dmt = dm_task_create(DM_DEVICE_DEPS)))
2834                         goto out;
2835                 if (!dm_task_set_name(dmt, name))
2836                         goto out;
2837
2838                 r = -ENODEV;
2839                 if (!dm_task_run(dmt))
2840                         goto out;
2841
2842                 r = -EINVAL;
2843                 if (!dm_task_get_info(dmt, &dmi))
2844                         goto out;
2845                 if (!(deps = dm_task_get_deps(dmt)))
2846                         goto out;
2847
2848                 r = -ENODEV;
2849                 if (!dmi.exists)
2850                         goto out;
2851
2852                 r = _process_deps(cd, prefix, deps, names, offset, names_length - 1);
2853                 if (r < 0)
2854                         goto out;
2855
2856                 dm_task_destroy(dmt);
2857                 dmt = NULL;
2858
2859                 offset += r;
2860                 name = names[last++];
2861         }
2862
2863         r = 0;
2864 out:
2865         if (r < 0) {
2866                 for (i = 0; i < names_length - 1; i++)
2867                         free(names[i]);
2868                 *names = NULL;
2869         }
2870
2871         if (dmt)
2872                 dm_task_destroy(dmt);
2873
2874         dm_exit_context();
2875         return r;
2876 }
2877
2878 static int _dm_message(const char *name, const char *msg)
2879 {
2880         int r = 0;
2881         struct dm_task *dmt;
2882
2883         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
2884                 return 0;
2885
2886         if (!dm_task_secure_data(dmt))
2887                 goto out;
2888
2889         if (name && !dm_task_set_name(dmt, name))
2890                 goto out;
2891
2892         if (!dm_task_set_sector(dmt, (uint64_t) 0))
2893                 goto out;
2894
2895         if (!dm_task_set_message(dmt, msg))
2896                 goto out;
2897
2898         r = dm_task_run(dmt);
2899 out:
2900         dm_task_destroy(dmt);
2901         return r;
2902 }
2903
2904 int dm_suspend_device(struct crypt_device *cd, const char *name, uint32_t dmflags)
2905 {
2906         uint32_t dmt_flags;
2907         int r = -ENOTSUP;
2908
2909         if (dm_init_context(cd, DM_UNKNOWN))
2910                 return r;
2911
2912         if (dmflags & DM_SUSPEND_WIPE_KEY) {
2913                 if (dm_flags(cd, DM_CRYPT, &dmt_flags))
2914                         goto out;
2915
2916                 if (!(dmt_flags & DM_KEY_WIPE_SUPPORTED))
2917                         goto out;
2918         }
2919
2920         r = -EINVAL;
2921
2922         if (!_dm_simple(DM_DEVICE_SUSPEND, name, dmflags))
2923                 goto out;
2924
2925         if (dmflags & DM_SUSPEND_WIPE_KEY) {
2926                 if (!_dm_message(name, "key wipe")) {
2927                         _dm_resume_device(name, 0);
2928                         goto out;
2929                 }
2930         }
2931
2932         r = 0;
2933 out:
2934         dm_exit_context();
2935         return r;
2936 }
2937
2938 int dm_resume_device(struct crypt_device *cd, const char *name, uint32_t dmflags)
2939 {
2940         int r;
2941
2942         if (dm_init_context(cd, DM_UNKNOWN))
2943                 return -ENOTSUP;
2944
2945         r = _dm_resume_device(name, dmflags);
2946
2947         dm_exit_context();
2948
2949         return r;
2950 }
2951
2952 int dm_resume_and_reinstate_key(struct crypt_device *cd, const char *name,
2953                                 const struct volume_key *vk)
2954 {
2955         uint32_t dmt_flags;
2956         int msg_size;
2957         char *msg = NULL;
2958         int r = -ENOTSUP;
2959
2960         if (dm_init_context(cd, DM_CRYPT) || dm_flags(cd, DM_CRYPT, &dmt_flags))
2961                 return -ENOTSUP;
2962
2963         if (!(dmt_flags & DM_KEY_WIPE_SUPPORTED))
2964                 goto out;
2965
2966         if (!vk->keylength)
2967                 msg_size = 11; // key set -
2968         else if (vk->key_description)
2969                 msg_size = strlen(vk->key_description) + int_log10(vk->keylength) + 18;
2970         else
2971                 msg_size = vk->keylength * 2 + 10; // key set <key>
2972
2973         msg = crypt_safe_alloc(msg_size);
2974         if (!msg) {
2975                 r = -ENOMEM;
2976                 goto out;
2977         }
2978
2979         strcpy(msg, "key set ");
2980         if (!vk->keylength)
2981                 snprintf(msg + 8, msg_size - 8, "-");
2982         else if (vk->key_description)
2983                 snprintf(msg + 8, msg_size - 8, ":%zu:logon:%s", vk->keylength, vk->key_description);
2984         else
2985                 hex_key(&msg[8], vk->keylength, vk->key);
2986
2987         if (!_dm_message(name, msg) ||
2988             _dm_resume_device(name, 0)) {
2989                 r = -EINVAL;
2990                 goto out;
2991         }
2992         r = 0;
2993 out:
2994         crypt_safe_free(msg);
2995         dm_exit_context();
2996         return r;
2997 }
2998
2999 const char *dm_get_dir(void)
3000 {
3001         return dm_dir();
3002 }
3003
3004 int dm_is_dm_device(int major)
3005 {
3006         return dm_is_dm_major((uint32_t)major);
3007 }
3008
3009 int dm_is_dm_kernel_name(const char *name)
3010 {
3011         return strncmp(name, "dm-", 3) ? 0 : 1;
3012 }
3013
3014 int dm_crypt_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
3015         struct device *data_device, struct volume_key *vk, const char *cipher,
3016         uint64_t iv_offset, uint64_t data_offset, const char *integrity, uint32_t tag_size,
3017         uint32_t sector_size)
3018 {
3019         int r = -EINVAL;
3020
3021         /* free on error */
3022         char *dm_integrity = NULL;
3023
3024         if (tag_size) {
3025                 /* Space for IV metadata only */
3026                 dm_integrity = strdup(integrity ?: "none");
3027                 if (!dm_integrity) {
3028                         r = -ENOMEM;
3029                         goto err;
3030                 }
3031         }
3032
3033         tgt->data_device = data_device;
3034
3035         tgt->type = DM_CRYPT;
3036         tgt->direction = TARGET_SET;
3037         tgt->u.crypt.vk = vk;
3038         tgt->offset = seg_offset;
3039         tgt->size = seg_size;
3040
3041         tgt->u.crypt.cipher = cipher;
3042         tgt->u.crypt.integrity = dm_integrity;
3043         tgt->u.crypt.iv_offset = iv_offset;
3044         tgt->u.crypt.offset = data_offset;
3045         tgt->u.crypt.tag_size = tag_size;
3046         tgt->u.crypt.sector_size = sector_size;
3047
3048         return 0;
3049 err:
3050         free(dm_integrity);
3051
3052         return r;
3053 }
3054
3055 int dm_verity_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
3056         struct device *data_device, struct device *hash_device, struct device *fec_device,
3057         const char *root_hash, uint32_t root_hash_size, const char* root_hash_sig_key_desc,
3058         uint64_t hash_offset_block, uint64_t fec_blocks, struct crypt_params_verity *vp)
3059 {
3060         if (!data_device || !hash_device || !vp)
3061                 return -EINVAL;
3062
3063         tgt->type = DM_VERITY;
3064         tgt->direction = TARGET_SET;
3065         tgt->offset = seg_offset;
3066         tgt->size = seg_size;
3067         tgt->data_device = data_device;
3068
3069         tgt->u.verity.hash_device = hash_device;
3070         tgt->u.verity.fec_device = fec_device;
3071         tgt->u.verity.root_hash = root_hash;
3072         tgt->u.verity.root_hash_size = root_hash_size;
3073         tgt->u.verity.root_hash_sig_key_desc = root_hash_sig_key_desc;
3074         tgt->u.verity.hash_offset = hash_offset_block;
3075         tgt->u.verity.fec_offset = vp->fec_area_offset / vp->hash_block_size;
3076         tgt->u.verity.fec_blocks = fec_blocks;
3077         tgt->u.verity.vp = vp;
3078
3079         return 0;
3080 }
3081
3082 int dm_integrity_target_set(struct crypt_device *cd,
3083                         struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
3084                         struct device *meta_device,
3085                         struct device *data_device, uint64_t tag_size, uint64_t offset,
3086                         uint32_t sector_size, struct volume_key *vk,
3087                         struct volume_key *journal_crypt_key, struct volume_key *journal_mac_key,
3088                         const struct crypt_params_integrity *ip)
3089 {
3090         uint32_t dmi_flags;
3091
3092         if (!data_device)
3093                 return -EINVAL;
3094
3095         _dm_check_versions(cd, DM_INTEGRITY);
3096
3097         tgt->type = DM_INTEGRITY;
3098         tgt->direction = TARGET_SET;
3099         tgt->offset = seg_offset;
3100         tgt->size = seg_size;
3101         tgt->data_device = data_device;
3102         if (meta_device != data_device)
3103                 tgt->u.integrity.meta_device = meta_device;
3104         tgt->u.integrity.tag_size = tag_size;
3105         tgt->u.integrity.offset = offset;
3106         tgt->u.integrity.sector_size = sector_size;
3107
3108         tgt->u.integrity.vk = vk;
3109         tgt->u.integrity.journal_crypt_key = journal_crypt_key;
3110         tgt->u.integrity.journal_integrity_key = journal_mac_key;
3111
3112         if (!dm_flags(cd, DM_INTEGRITY, &dmi_flags) &&
3113             (dmi_flags & DM_INTEGRITY_FIX_PADDING_SUPPORTED) &&
3114             !(crypt_get_compatibility(cd) & CRYPT_COMPAT_LEGACY_INTEGRITY_PADDING))
3115                 tgt->u.integrity.fix_padding = true;
3116
3117         if (!dm_flags(cd, DM_INTEGRITY, &dmi_flags) &&
3118             (dmi_flags & DM_INTEGRITY_FIX_HMAC_SUPPORTED) &&
3119             !(crypt_get_compatibility(cd) & CRYPT_COMPAT_LEGACY_INTEGRITY_HMAC))
3120                 tgt->u.integrity.fix_hmac = true;
3121
3122         /* This flag can be backported, just try to set it always */
3123         if (crypt_get_compatibility(cd) & CRYPT_COMPAT_LEGACY_INTEGRITY_RECALC)
3124                 tgt->u.integrity.legacy_recalc = true;
3125
3126         if (ip) {
3127                 tgt->u.integrity.journal_size = ip->journal_size;
3128                 tgt->u.integrity.journal_watermark = ip->journal_watermark;
3129                 tgt->u.integrity.journal_commit_time = ip->journal_commit_time;
3130                 tgt->u.integrity.interleave_sectors = ip->interleave_sectors;
3131                 tgt->u.integrity.buffer_sectors = ip->buffer_sectors;
3132                 tgt->u.integrity.journal_integrity = ip->journal_integrity;
3133                 tgt->u.integrity.journal_crypt = ip->journal_crypt;
3134                 tgt->u.integrity.integrity = ip->integrity;
3135         }
3136
3137         return 0;
3138 }
3139
3140 int dm_linear_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
3141         struct device *data_device, uint64_t data_offset)
3142 {
3143         if (!data_device)
3144                 return -EINVAL;
3145
3146         tgt->type = DM_LINEAR;
3147         tgt->direction = TARGET_SET;
3148         tgt->offset = seg_offset;
3149         tgt->size = seg_size;
3150         tgt->data_device = data_device;
3151
3152         tgt->u.linear.offset = data_offset;
3153
3154         return 0;
3155 }
3156
3157 int dm_zero_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size)
3158 {
3159         tgt->type = DM_ZERO;
3160         tgt->direction = TARGET_SET;
3161         tgt->offset = seg_offset;
3162         tgt->size = seg_size;
3163
3164         return 0;
3165 }