]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup.c
ask-password: rework how we pass request meta info when asking passwords
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <mntent.h>
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 #include "sd-device.h"
12 #include "sd-messages.h"
13
14 #include "alloc-util.h"
15 #include "ask-password-api.h"
16 #include "build.h"
17 #include "cryptsetup-fido2.h"
18 #include "cryptsetup-keyfile.h"
19 #include "cryptsetup-pkcs11.h"
20 #include "cryptsetup-tpm2.h"
21 #include "cryptsetup-util.h"
22 #include "device-util.h"
23 #include "efi-api.h"
24 #include "efi-loader.h"
25 #include "env-util.h"
26 #include "escape.h"
27 #include "fileio.h"
28 #include "fs-util.h"
29 #include "fstab-util.h"
30 #include "hexdecoct.h"
31 #include "libfido2-util.h"
32 #include "log.h"
33 #include "main-func.h"
34 #include "memory-util.h"
35 #include "mount-util.h"
36 #include "nulstr-util.h"
37 #include "parse-util.h"
38 #include "path-util.h"
39 #include "pkcs11-util.h"
40 #include "pretty-print.h"
41 #include "process-util.h"
42 #include "random-util.h"
43 #include "string-table.h"
44 #include "strv.h"
45 #include "tpm2-pcr.h"
46 #include "tpm2-util.h"
47
48 /* internal helper */
49 #define ANY_LUKS "LUKS"
50 /* as in src/cryptsetup.h */
51 #define CRYPT_SECTOR_SIZE 512U
52 #define CRYPT_MAX_SECTOR_SIZE 4096U
53
54 typedef enum PassphraseType {
55 PASSPHRASE_NONE,
56 PASSPHRASE_REGULAR = 1 << 0,
57 PASSPHRASE_RECOVERY_KEY = 1 << 1,
58 PASSPHRASE_BOTH = PASSPHRASE_REGULAR|PASSPHRASE_RECOVERY_KEY,
59 _PASSPHRASE_TYPE_MAX,
60 _PASSPHRASE_TYPE_INVALID = -1,
61 } PassphraseType;
62
63 static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT, CRYPT_BITLK or CRYPT_PLAIN */
64 static char *arg_cipher = NULL;
65 static unsigned arg_key_size = 0;
66 static unsigned arg_sector_size = CRYPT_SECTOR_SIZE;
67 static int arg_key_slot = CRYPT_ANY_SLOT;
68 static unsigned arg_keyfile_size = 0;
69 static uint64_t arg_keyfile_offset = 0;
70 static bool arg_keyfile_erase = false;
71 static bool arg_try_empty_password = false;
72 static char *arg_hash = NULL;
73 static char *arg_header = NULL;
74 static unsigned arg_tries = 3;
75 static bool arg_readonly = false;
76 static bool arg_verify = false;
77 static AskPasswordFlags arg_ask_password_flags = 0;
78 static bool arg_discards = false;
79 static bool arg_same_cpu_crypt = false;
80 static bool arg_submit_from_crypt_cpus = false;
81 static bool arg_no_read_workqueue = false;
82 static bool arg_no_write_workqueue = false;
83 static bool arg_tcrypt_hidden = false;
84 static bool arg_tcrypt_system = false;
85 static bool arg_tcrypt_veracrypt = false;
86 static uint32_t arg_tcrypt_veracrypt_pim = 0;
87 static char **arg_tcrypt_keyfiles = NULL;
88 static uint64_t arg_offset = 0;
89 static uint64_t arg_skip = 0;
90 static usec_t arg_timeout = USEC_INFINITY;
91 static char *arg_pkcs11_uri = NULL;
92 static bool arg_pkcs11_uri_auto = false;
93 static char *arg_fido2_device = NULL;
94 static bool arg_fido2_device_auto = false;
95 static void *arg_fido2_cid = NULL;
96 static size_t arg_fido2_cid_size = 0;
97 static char *arg_fido2_rp_id = NULL;
98 static char *arg_tpm2_device = NULL; /* These and the following fields are about locking an encrypted volume to the local TPM */
99 static bool arg_tpm2_device_auto = false;
100 static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
101 static char *arg_tpm2_signature = NULL;
102 static bool arg_tpm2_pin = false;
103 static char *arg_tpm2_pcrlock = NULL;
104 static bool arg_headless = false;
105 static usec_t arg_token_timeout_usec = 30*USEC_PER_SEC;
106 static unsigned arg_tpm2_measure_pcr = UINT_MAX; /* This and the following field is about measuring the unlocked volume key to the local TPM */
107 static char **arg_tpm2_measure_banks = NULL;
108 static char *arg_link_keyring = NULL;
109 static char *arg_link_key_type = NULL;
110 static char *arg_link_key_description = NULL;
111
112 STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep);
113 STATIC_DESTRUCTOR_REGISTER(arg_hash, freep);
114 STATIC_DESTRUCTOR_REGISTER(arg_header, freep);
115 STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep);
116 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_uri, freep);
117 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device, freep);
118 STATIC_DESTRUCTOR_REGISTER(arg_fido2_cid, freep);
119 STATIC_DESTRUCTOR_REGISTER(arg_fido2_rp_id, freep);
120 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
121 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_signature, freep);
122 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_measure_banks, strv_freep);
123 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_pcrlock, freep);
124 STATIC_DESTRUCTOR_REGISTER(arg_link_keyring, freep);
125 STATIC_DESTRUCTOR_REGISTER(arg_link_key_type, freep);
126 STATIC_DESTRUCTOR_REGISTER(arg_link_key_description, freep);
127
128 static const char* const passphrase_type_table[_PASSPHRASE_TYPE_MAX] = {
129 [PASSPHRASE_REGULAR] = "passphrase",
130 [PASSPHRASE_RECOVERY_KEY] = "recovery key",
131 [PASSPHRASE_BOTH] = "passphrase or recovery key",
132 };
133
134 const char* passphrase_type_to_string(PassphraseType t);
135 PassphraseType passphrase_type_from_string(const char *s);
136
137 DEFINE_STRING_TABLE_LOOKUP(passphrase_type, PassphraseType);
138
139 /* Options Debian's crypttab knows we don't:
140 check=
141 checkargs=
142 noearly
143 loud
144 quiet
145 keyscript=
146 initramfs
147 */
148
149 static int parse_one_option(const char *option) {
150 const char *val;
151 int r;
152
153 assert(option);
154
155 /* Handled outside of this tool */
156 if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout"))
157 return 0;
158
159 if (startswith(option, "keyfile-timeout="))
160 return 0;
161
162 if ((val = startswith(option, "cipher="))) {
163 r = free_and_strdup(&arg_cipher, val);
164 if (r < 0)
165 return log_oom();
166
167 } else if ((val = startswith(option, "size="))) {
168
169 r = safe_atou(val, &arg_key_size);
170 if (r < 0) {
171 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
172 return 0;
173 }
174
175 if (arg_key_size % 8) {
176 log_warning("size= not a multiple of 8, ignoring.");
177 return 0;
178 }
179
180 arg_key_size /= 8;
181
182 } else if ((val = startswith(option, "sector-size="))) {
183
184 r = safe_atou(val, &arg_sector_size);
185 if (r < 0) {
186 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
187 return 0;
188 }
189
190 if (arg_sector_size % 2) {
191 log_warning("sector-size= not a multiple of 2, ignoring.");
192 return 0;
193 }
194
195 if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE)
196 log_warning("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE);
197
198 } else if ((val = startswith(option, "key-slot=")) ||
199 (val = startswith(option, "keyslot="))) {
200
201 arg_type = ANY_LUKS;
202 r = safe_atoi(val, &arg_key_slot);
203 if (r < 0)
204 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
205
206 } else if ((val = startswith(option, "tcrypt-keyfile="))) {
207
208 arg_type = CRYPT_TCRYPT;
209 if (path_is_absolute(val)) {
210 if (strv_extend(&arg_tcrypt_keyfiles, val) < 0)
211 return log_oom();
212 } else
213 log_warning("Key file path \"%s\" is not absolute, ignoring.", val);
214
215 } else if ((val = startswith(option, "keyfile-size="))) {
216
217 r = safe_atou(val, &arg_keyfile_size);
218 if (r < 0)
219 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
220
221 } else if ((val = startswith(option, "keyfile-offset="))) {
222
223 r = safe_atou64(val, &arg_keyfile_offset);
224 if (r < 0)
225 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
226
227 } else if ((val = startswith(option, "keyfile-erase="))) {
228
229 r = parse_boolean(val);
230 if (r < 0) {
231 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
232 return 0;
233 }
234
235 arg_keyfile_erase = r;
236
237 } else if (streq(option, "keyfile-erase"))
238 arg_keyfile_erase = true;
239
240 else if ((val = startswith(option, "hash="))) {
241 r = free_and_strdup(&arg_hash, val);
242 if (r < 0)
243 return log_oom();
244
245 } else if ((val = startswith(option, "header="))) {
246 if (!arg_type || !STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT))
247 arg_type = ANY_LUKS;
248
249 if (!path_is_absolute(val))
250 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
251 "Header path \"%s\" is not absolute, refusing.", val);
252
253 if (arg_header)
254 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
255 "Duplicate header= option, refusing.");
256
257 arg_header = strdup(val);
258 if (!arg_header)
259 return log_oom();
260
261 } else if ((val = startswith(option, "tries="))) {
262
263 r = safe_atou(val, &arg_tries);
264 if (r < 0)
265 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
266
267 } else if (STR_IN_SET(option, "readonly", "read-only"))
268 arg_readonly = true;
269 else if (streq(option, "verify"))
270 arg_verify = true;
271 else if ((val = startswith(option, "password-echo="))) {
272 if (streq(val, "masked"))
273 arg_ask_password_flags &= ~(ASK_PASSWORD_ECHO|ASK_PASSWORD_SILENT);
274 else {
275 r = parse_boolean(val);
276 if (r < 0) {
277 log_warning_errno(r, "Invalid password-echo= option \"%s\", ignoring.", val);
278 return 0;
279 }
280
281 SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_ECHO, r);
282 SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_SILENT, !r);
283 }
284 } else if (STR_IN_SET(option, "allow-discards", "discard"))
285 arg_discards = true;
286 else if (streq(option, "same-cpu-crypt"))
287 arg_same_cpu_crypt = true;
288 else if (streq(option, "submit-from-crypt-cpus"))
289 arg_submit_from_crypt_cpus = true;
290 else if (streq(option, "no-read-workqueue"))
291 arg_no_read_workqueue = true;
292 else if (streq(option, "no-write-workqueue"))
293 arg_no_write_workqueue = true;
294 else if (streq(option, "luks"))
295 arg_type = ANY_LUKS;
296 /* since cryptsetup 2.3.0 (Feb 2020) */
297 #ifdef CRYPT_BITLK
298 else if (streq(option, "bitlk"))
299 arg_type = CRYPT_BITLK;
300 #endif
301 else if (streq(option, "tcrypt"))
302 arg_type = CRYPT_TCRYPT;
303 else if (STR_IN_SET(option, "tcrypt-hidden", "tcrypthidden")) {
304 arg_type = CRYPT_TCRYPT;
305 arg_tcrypt_hidden = true;
306 } else if (streq(option, "tcrypt-system")) {
307 arg_type = CRYPT_TCRYPT;
308 arg_tcrypt_system = true;
309 } else if (STR_IN_SET(option, "tcrypt-veracrypt", "veracrypt")) {
310 arg_type = CRYPT_TCRYPT;
311 arg_tcrypt_veracrypt = true;
312 } else if ((val = startswith(option, "veracrypt-pim="))) {
313
314 r = safe_atou32(val, &arg_tcrypt_veracrypt_pim);
315 if (r < 0) {
316 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
317 return 0;
318 }
319 } else if (STR_IN_SET(option, "plain", "swap", "tmp") ||
320 startswith(option, "tmp="))
321 arg_type = CRYPT_PLAIN;
322 else if ((val = startswith(option, "timeout="))) {
323
324 r = parse_sec_fix_0(val, &arg_timeout);
325 if (r < 0)
326 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
327
328 } else if ((val = startswith(option, "offset="))) {
329
330 r = safe_atou64(val, &arg_offset);
331 if (r < 0)
332 return log_error_errno(r, "Failed to parse %s: %m", option);
333
334 } else if ((val = startswith(option, "skip="))) {
335
336 r = safe_atou64(val, &arg_skip);
337 if (r < 0)
338 return log_error_errno(r, "Failed to parse %s: %m", option);
339
340 } else if ((val = startswith(option, "pkcs11-uri="))) {
341
342 if (streq(val, "auto")) {
343 arg_pkcs11_uri = mfree(arg_pkcs11_uri);
344 arg_pkcs11_uri_auto = true;
345 } else {
346 if (!pkcs11_uri_valid(val))
347 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "pkcs11-uri= parameter expects a PKCS#11 URI, refusing");
348
349 r = free_and_strdup(&arg_pkcs11_uri, val);
350 if (r < 0)
351 return log_oom();
352
353 arg_pkcs11_uri_auto = false;
354 }
355
356 } else if ((val = startswith(option, "fido2-device="))) {
357
358 if (streq(val, "auto")) {
359 arg_fido2_device = mfree(arg_fido2_device);
360 arg_fido2_device_auto = true;
361 } else {
362 r = free_and_strdup(&arg_fido2_device, val);
363 if (r < 0)
364 return log_oom();
365
366 arg_fido2_device_auto = false;
367 }
368
369 } else if ((val = startswith(option, "fido2-cid="))) {
370
371 if (streq(val, "auto"))
372 arg_fido2_cid = mfree(arg_fido2_cid);
373 else {
374 _cleanup_free_ void *cid = NULL;
375 size_t cid_size;
376
377 r = unbase64mem(val, &cid, &cid_size);
378 if (r < 0)
379 return log_error_errno(r, "Failed to decode FIDO2 CID data: %m");
380
381 free(arg_fido2_cid);
382 arg_fido2_cid = TAKE_PTR(cid);
383 arg_fido2_cid_size = cid_size;
384 }
385
386 /* Turn on FIDO2 as side-effect, if not turned on yet. */
387 if (!arg_fido2_device && !arg_fido2_device_auto)
388 arg_fido2_device_auto = true;
389
390 } else if ((val = startswith(option, "fido2-rp="))) {
391
392 r = free_and_strdup(&arg_fido2_rp_id, val);
393 if (r < 0)
394 return log_oom();
395
396 } else if ((val = startswith(option, "tpm2-device="))) {
397
398 if (streq(val, "auto")) {
399 arg_tpm2_device = mfree(arg_tpm2_device);
400 arg_tpm2_device_auto = true;
401 } else {
402 r = free_and_strdup(&arg_tpm2_device, val);
403 if (r < 0)
404 return log_oom();
405
406 arg_tpm2_device_auto = false;
407 }
408
409 } else if ((val = startswith(option, "tpm2-pcrs="))) {
410
411 r = tpm2_parse_pcr_argument_to_mask(val, &arg_tpm2_pcr_mask);
412 if (r < 0)
413 return r;
414
415 } else if ((val = startswith(option, "tpm2-signature="))) {
416
417 if (!path_is_absolute(val))
418 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
419 "TPM2 signature path \"%s\" is not absolute, refusing.", val);
420
421 r = free_and_strdup(&arg_tpm2_signature, val);
422 if (r < 0)
423 return log_oom();
424
425 } else if ((val = startswith(option, "tpm2-pin="))) {
426
427 r = parse_boolean(val);
428 if (r < 0) {
429 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
430 return 0;
431 }
432
433 arg_tpm2_pin = r;
434
435 } else if ((val = startswith(option, "tpm2-pcrlock="))) {
436
437 if (!path_is_absolute(val))
438 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
439 "TPM2 pcrlock policy path \"%s\" is not absolute, refusing.", val);
440
441 r = free_and_strdup(&arg_tpm2_pcrlock, val);
442 if (r < 0)
443 return log_oom();
444
445 } else if ((val = startswith(option, "tpm2-measure-pcr="))) {
446 unsigned pcr;
447
448 r = safe_atou(val, &pcr);
449 if (r < 0) {
450 r = parse_boolean(val);
451 if (r < 0) {
452 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
453 return 0;
454 }
455
456 pcr = r ? TPM2_PCR_SYSTEM_IDENTITY : UINT_MAX;
457 } else if (!TPM2_PCR_INDEX_VALID(pcr)) {
458 log_warning("Selected TPM index for measurement %u outside of allowed range 0…%u, ignoring.", pcr, TPM2_PCRS_MAX-1);
459 return 0;
460 }
461
462 arg_tpm2_measure_pcr = pcr;
463
464 } else if ((val = startswith(option, "tpm2-measure-bank="))) {
465
466 #if HAVE_OPENSSL
467 _cleanup_strv_free_ char **l = NULL;
468
469 l = strv_split(optarg, ":");
470 if (!l)
471 return log_oom();
472
473 STRV_FOREACH(i, l) {
474 const EVP_MD *implementation;
475
476 implementation = EVP_get_digestbyname(*i);
477 if (!implementation)
478 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown bank '%s', refusing.", val);
479
480 if (strv_extend(&arg_tpm2_measure_banks, EVP_MD_name(implementation)) < 0)
481 return log_oom();
482 }
483 #else
484 log_error("Build lacks OpenSSL support, cannot measure to PCR banks, ignoring: %s", option);
485 #endif
486
487 } else if ((val = startswith(option, "try-empty-password="))) {
488
489 r = parse_boolean(val);
490 if (r < 0) {
491 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
492 return 0;
493 }
494
495 arg_try_empty_password = r;
496
497 } else if (streq(option, "try-empty-password"))
498 arg_try_empty_password = true;
499 else if ((val = startswith(option, "headless="))) {
500
501 r = parse_boolean(val);
502 if (r < 0) {
503 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
504 return 0;
505 }
506
507 arg_headless = r;
508 } else if (streq(option, "headless"))
509 arg_headless = true;
510
511 else if ((val = startswith(option, "token-timeout="))) {
512
513 r = parse_sec_fix_0(val, &arg_token_timeout_usec);
514 if (r < 0)
515 log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
516
517 } else if ((val = startswith(option, "link-volume-key="))) {
518 #ifdef HAVE_CRYPT_SET_KEYRING_TO_LINK
519 const char *sep, *c;
520 _cleanup_free_ char *keyring = NULL, *key_type = NULL, *key_description = NULL;
521
522 /* Stick with cryptsetup --link-vk-to-keyring format
523 * <keyring_description>::%<key_type>:<key_description>,
524 * where %<key_type> is optional and defaults to 'user'.
525 */
526 if (!(sep = strstr(val, "::")))
527 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse link-volume-key= option value: %m");
528
529 /* cryptsetup (cli) supports <keyring_description> passed in various formats:
530 * - well-known keyrings prefixed with '@' (@u user, @s session, etc)
531 * - text descriptions prefixed with "%:" or "%keyring:".
532 * - text description with no prefix.
533 * - numeric keyring id (ignored in current patch set). */
534 if (*val == '@' || *val == '%')
535 keyring = strndup(val, sep - val);
536 else
537 /* add type prefix if missing (crypt_set_keyring_to_link() expects it) */
538 keyring = strnappend("%:", val, sep - val);
539 if (!keyring)
540 return log_oom();
541
542 sep += 2;
543
544 /* %<key_type> is optional (and defaults to 'user') */
545 if (*sep == '%') {
546 /* must be separated by colon */
547 if (!(c = strchr(sep, ':')))
548 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse link-volume-key= option value: %m");
549
550 key_type = strndup(sep + 1, c - sep - 1);
551 if (!key_type)
552 return log_oom();
553
554 sep = c + 1;
555 }
556
557 key_description = strdup(sep);
558 if (!key_description)
559 return log_oom();
560
561 free_and_replace(arg_link_keyring, keyring);
562 free_and_replace(arg_link_key_type, key_type);
563 free_and_replace(arg_link_key_description, key_description);
564 #else
565 log_error("Build lacks libcryptsetup support for linking volume keys in user specified kernel keyrings upon device activation, ignoring: %s", option);
566 #endif
567 } else if (!streq(option, "x-initrd.attach"))
568 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
569
570 return 0;
571 }
572
573 static int parse_crypt_config(const char *options) {
574 assert(options);
575
576 for (;;) {
577 _cleanup_free_ char *word = NULL;
578 int r;
579
580 r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
581 if (r < 0)
582 return log_error_errno(r, "Failed to parse options: %m");
583 if (r == 0)
584 break;
585
586 r = parse_one_option(word);
587 if (r < 0)
588 return r;
589 }
590
591 /* sanity-check options */
592 if (arg_type && !streq(arg_type, CRYPT_PLAIN)) {
593 if (arg_offset != 0)
594 log_warning("offset= ignored with type %s", arg_type);
595 if (arg_skip != 0)
596 log_warning("skip= ignored with type %s", arg_type);
597 }
598
599 return 0;
600 }
601
602 static char* disk_description(const char *path) {
603 static const char name_fields[] =
604 "DM_NAME\0"
605 "ID_MODEL_FROM_DATABASE\0"
606 "ID_MODEL\0";
607
608 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
609 const char *name;
610 struct stat st;
611
612 assert(path);
613
614 if (stat(path, &st) < 0)
615 return NULL;
616
617 if (!S_ISBLK(st.st_mode))
618 return NULL;
619
620 if (sd_device_new_from_stat_rdev(&device, &st) < 0)
621 return NULL;
622
623 if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) {
624 _cleanup_free_ char *unescaped = NULL;
625 ssize_t l;
626
627 /* ID_PART_ENTRY_NAME uses \x style escaping, using libblkid's blkid_encode_string(). Let's
628 * reverse this here to make the string more human friendly in case people embed spaces or
629 * other weird stuff. */
630
631 l = cunescape(name, UNESCAPE_RELAX, &unescaped);
632 if (l < 0) {
633 log_debug_errno(l, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
634 return NULL;
635 }
636
637 if (!isempty(unescaped) && !string_has_cc(unescaped, NULL))
638 return TAKE_PTR(unescaped);
639 }
640
641 /* These need no unescaping. */
642 NULSTR_FOREACH(i, name_fields)
643 if (sd_device_get_property_value(device, i, &name) >= 0 &&
644 !isempty(name))
645 return strdup(name);
646
647 return NULL;
648 }
649
650 static char *disk_mount_point(const char *label) {
651 _cleanup_free_ char *device = NULL;
652 _cleanup_endmntent_ FILE *f = NULL;
653 struct mntent *m;
654
655 /* Yeah, we don't support native systemd unit files here for now */
656
657 device = strjoin("/dev/mapper/", label);
658 if (!device)
659 return NULL;
660
661 f = setmntent(fstab_path(), "re");
662 if (!f)
663 return NULL;
664
665 while ((m = getmntent(f)))
666 if (path_equal(m->mnt_fsname, device))
667 return strdup(m->mnt_dir);
668
669 return NULL;
670 }
671
672 static char *friendly_disk_name(const char *src, const char *vol) {
673 _cleanup_free_ char *description = NULL, *mount_point = NULL;
674 char *name_buffer = NULL;
675 int r;
676
677 assert(src);
678 assert(vol);
679
680 description = disk_description(src);
681 mount_point = disk_mount_point(vol);
682
683 /* If the description string is simply the volume name, then let's not show this twice */
684 if (description && streq(vol, description))
685 description = mfree(description);
686
687 if (mount_point && description)
688 r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point);
689 else if (mount_point)
690 r = asprintf(&name_buffer, "%s on %s", vol, mount_point);
691 else if (description)
692 r = asprintf(&name_buffer, "%s (%s)", description, vol);
693 else
694 return strdup(vol);
695 if (r < 0)
696 return NULL;
697
698 return name_buffer;
699 }
700
701 static PassphraseType check_registered_passwords(struct crypt_device *cd) {
702 _cleanup_free_ bool *slots = NULL;
703 int slot_max;
704 PassphraseType passphrase_type = PASSPHRASE_NONE;
705
706 assert(cd);
707
708 if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2)) {
709 log_debug("%s: not a LUKS2 device, only passphrases are supported", crypt_get_device_name(cd));
710 return PASSPHRASE_REGULAR;
711 }
712
713 /* Search all used slots */
714 assert_se((slot_max = crypt_keyslot_max(CRYPT_LUKS2)) > 0);
715 slots = new(bool, slot_max);
716 if (!slots)
717 return log_oom();
718
719 for (int slot = 0; slot < slot_max; slot++)
720 slots[slot] = IN_SET(crypt_keyslot_status(cd, slot), CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST);
721
722 /* Iterate all LUKS2 tokens and keep track of all their slots */
723 for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
724 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
725 const char *type;
726 JsonVariant *w, *z;
727 int tk;
728
729 tk = cryptsetup_get_token_as_json(cd, token, NULL, &v);
730 if (IN_SET(tk, -ENOENT, -EINVAL))
731 continue;
732 if (tk < 0) {
733 log_warning_errno(tk, "Failed to read JSON token data, ignoring: %m");
734 continue;
735 }
736
737 w = json_variant_by_key(v, "type");
738 if (!w || !json_variant_is_string(w)) {
739 log_warning("Token JSON data lacks type field, ignoring.");
740 continue;
741 }
742
743 type = json_variant_string(w);
744 if (STR_IN_SET(type, "systemd-recovery", "systemd-pkcs11", "systemd-fido2", "systemd-tpm2")) {
745
746 /* At least exists one recovery key */
747 if (streq(type, "systemd-recovery"))
748 passphrase_type |= PASSPHRASE_RECOVERY_KEY;
749
750 w = json_variant_by_key(v, "keyslots");
751 if (!w || !json_variant_is_array(w)) {
752 log_warning("Token JSON data lacks keyslots field, ignoring.");
753 continue;
754 }
755
756 JSON_VARIANT_ARRAY_FOREACH(z, w) {
757 unsigned u;
758 int at;
759
760 if (!json_variant_is_string(z)) {
761 log_warning("Token JSON data's keyslot field is not an array of strings, ignoring.");
762 continue;
763 }
764
765 at = safe_atou(json_variant_string(z), &u);
766 if (at < 0) {
767 log_warning_errno(at, "Token JSON data's keyslot field is not an integer formatted as string, ignoring.");
768 continue;
769 }
770
771 if (u >= (unsigned) slot_max) {
772 log_warning_errno(at, "Token JSON data's keyslot field exceeds the maximum value allowed, ignoring.");
773 continue;
774 }
775
776 slots[u] = false;
777 }
778 }
779 }
780
781 /* Check if any of the slots is not referenced by systemd tokens */
782 for (int slot = 0; slot < slot_max; slot++)
783 if (slots[slot]) {
784 passphrase_type |= PASSPHRASE_REGULAR;
785 break;
786 }
787
788 /* All the slots are referenced by systemd tokens, so if a recovery key is not enrolled,
789 * we will not be able to enter a passphrase. */
790 return passphrase_type;
791 }
792
793 static int get_password(
794 const char *vol,
795 const char *src,
796 usec_t until,
797 bool accept_cached,
798 PassphraseType passphrase_type,
799 char ***ret) {
800
801 _cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL, *id = NULL;
802 _cleanup_strv_free_erase_ char **passwords = NULL;
803 AskPasswordFlags flags = arg_ask_password_flags | ASK_PASSWORD_PUSH_CACHE;
804 int r;
805
806 assert(vol);
807 assert(src);
808 assert(ret);
809
810 if (arg_headless)
811 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Password querying disabled via 'headless' option.");
812
813 friendly = friendly_disk_name(src, vol);
814 if (!friendly)
815 return log_oom();
816
817 if (asprintf(&text, "Please enter %s for disk %s:", passphrase_type_to_string(passphrase_type), friendly) < 0)
818 return log_oom();
819
820 disk_path = cescape(src);
821 if (!disk_path)
822 return log_oom();
823
824 id = strjoin("cryptsetup:", disk_path);
825 if (!id)
826 return log_oom();
827
828 AskPasswordRequest req = {
829 .message = text,
830 .icon = "drive-harddisk",
831 .id = id,
832 .keyring = "cryptsetup",
833 .credential = "cryptsetup.passphrase",
834 };
835
836 r = ask_password_auto(
837 &req,
838 until,
839 flags | (accept_cached*ASK_PASSWORD_ACCEPT_CACHED),
840 &passwords);
841 if (r < 0)
842 return log_error_errno(r, "Failed to query password: %m");
843
844 if (arg_verify) {
845 _cleanup_strv_free_erase_ char **passwords2 = NULL;
846
847 assert(strv_length(passwords) == 1);
848
849 text = mfree(text);
850 if (asprintf(&text, "Please enter %s for disk %s (verification):", passphrase_type_to_string(passphrase_type), friendly) < 0)
851 return log_oom();
852
853 free(id);
854 id = strjoin("cryptsetup-verification:", disk_path);
855 if (!id)
856 return log_oom();
857
858 req.message = text;
859 req.id = id;
860
861 r = ask_password_auto(&req, until, flags, &passwords2);
862 if (r < 0)
863 return log_error_errno(r, "Failed to query verification password: %m");
864
865 assert(strv_length(passwords2) == 1);
866
867 if (!streq(passwords[0], passwords2[0]))
868 return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN),
869 "Passwords did not match, retrying.");
870 }
871
872 strv_uniq(passwords);
873
874 STRV_FOREACH(p, passwords) {
875 char *c;
876
877 if (strlen(*p)+1 >= arg_key_size)
878 continue;
879
880 /* Pad password if necessary */
881 c = new(char, arg_key_size);
882 if (!c)
883 return log_oom();
884
885 strncpy(c, *p, arg_key_size);
886 erase_and_free(*p);
887 *p = TAKE_PTR(c);
888 }
889
890 *ret = TAKE_PTR(passwords);
891
892 return 0;
893 }
894
895 static int measure_volume_key(
896 struct crypt_device *cd,
897 const char *name,
898 const void *volume_key,
899 size_t volume_key_size) {
900
901 int r;
902
903 assert(cd);
904 assert(name);
905 assert(volume_key);
906 assert(volume_key_size > 0);
907
908 if (arg_tpm2_measure_pcr == UINT_MAX) {
909 log_debug("Not measuring volume key, deactivated.");
910 return 0;
911 }
912
913 r = efi_measured_uki(LOG_WARNING);
914 if (r < 0)
915 return r;
916 if (r == 0) {
917 log_debug("Kernel stub did not measure kernel image into the expected PCR, skipping userspace measurement, too.");
918 return 0;
919 }
920
921 #if HAVE_TPM2
922 _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL;
923 r = tpm2_context_new(arg_tpm2_device, &c);
924 if (r < 0)
925 return log_error_errno(r, "Failed to create TPM2 context: %m");
926
927 _cleanup_strv_free_ char **l = NULL;
928 if (strv_isempty(arg_tpm2_measure_banks)) {
929 r = tpm2_get_good_pcr_banks_strv(c, UINT32_C(1) << arg_tpm2_measure_pcr, &l);
930 if (r < 0)
931 return log_error_errno(r, "Could not verify pcr banks: %m");
932 }
933
934 _cleanup_free_ char *joined = strv_join(l ?: arg_tpm2_measure_banks, ", ");
935 if (!joined)
936 return log_oom();
937
938 /* Note: we don't directly measure the volume key, it might be a security problem to send an
939 * unprotected direct hash of the secret volume key over the wire to the TPM. Hence let's instead
940 * send a HMAC signature instead. */
941
942 _cleanup_free_ char *escaped = NULL;
943 escaped = xescape(name, ":"); /* avoid ambiguity around ":" once we join things below */
944 if (!escaped)
945 return log_oom();
946
947 _cleanup_free_ char *s = NULL;
948 s = strjoin("cryptsetup:", escaped, ":", strempty(crypt_get_uuid(cd)));
949 if (!s)
950 return log_oom();
951
952 r = tpm2_extend_bytes(c, l ?: arg_tpm2_measure_banks, arg_tpm2_measure_pcr, s, SIZE_MAX, volume_key, volume_key_size, TPM2_EVENT_VOLUME_KEY, s);
953 if (r < 0)
954 return log_error_errno(r, "Could not extend PCR: %m");
955
956 log_struct(LOG_INFO,
957 "MESSAGE_ID=" SD_MESSAGE_TPM_PCR_EXTEND_STR,
958 LOG_MESSAGE("Successfully extended PCR index %u with '%s' and volume key (banks %s).", arg_tpm2_measure_pcr, s, joined),
959 "MEASURING=%s", s,
960 "PCR=%u", arg_tpm2_measure_pcr,
961 "BANKS=%s", joined);
962
963 return 0;
964 #else
965 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring.");
966 #endif
967 }
968
969 static int measured_crypt_activate_by_volume_key(
970 struct crypt_device *cd,
971 const char *name,
972 const void *volume_key,
973 size_t volume_key_size,
974 uint32_t flags) {
975
976 int r;
977
978 assert(cd);
979 assert(name);
980
981 /* A wrapper around crypt_activate_by_volume_key() which also measures to a PCR if that's requested. */
982
983 r = crypt_activate_by_volume_key(cd, name, volume_key, volume_key_size, flags);
984 if (r < 0)
985 return r;
986
987 if (volume_key_size == 0) {
988 log_debug("Not measuring volume key, none specified.");
989 return r;
990 }
991
992 (void) measure_volume_key(cd, name, volume_key, volume_key_size); /* OK if fails */
993 return r;
994 }
995
996 static int measured_crypt_activate_by_passphrase(
997 struct crypt_device *cd,
998 const char *name,
999 int keyslot,
1000 const char *passphrase,
1001 size_t passphrase_size,
1002 uint32_t flags) {
1003
1004 _cleanup_(erase_and_freep) void *vk = NULL;
1005 size_t vks;
1006 int r;
1007
1008 assert(cd);
1009
1010 /* A wrapper around crypt_activate_by_passphrase() which also measures to a PCR if that's
1011 * requested. Note that we need the volume key for the measurement, and
1012 * crypt_activate_by_passphrase() doesn't give us access to this. Hence, we operate indirectly, and
1013 * retrieve the volume key first, and then activate through that. */
1014
1015 if (arg_tpm2_measure_pcr == UINT_MAX) {
1016 log_debug("Not measuring volume key, deactivated.");
1017 goto shortcut;
1018 }
1019
1020 r = crypt_get_volume_key_size(cd);
1021 if (r < 0)
1022 return r;
1023 if (r == 0) {
1024 log_debug("Not measuring volume key, none defined.");
1025 goto shortcut;
1026 }
1027
1028 vk = malloc(vks = r);
1029 if (!vk)
1030 return -ENOMEM;
1031
1032 r = crypt_volume_key_get(cd, keyslot, vk, &vks, passphrase, passphrase_size);
1033 if (r < 0)
1034 return r;
1035
1036 return measured_crypt_activate_by_volume_key(cd, name, vk, vks, flags);
1037
1038 shortcut:
1039 return crypt_activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags);
1040 }
1041
1042 static int attach_tcrypt(
1043 struct crypt_device *cd,
1044 const char *name,
1045 const char *key_file,
1046 const void *key_data,
1047 size_t key_data_size,
1048 char **passwords,
1049 uint32_t flags) {
1050
1051 int r = 0;
1052 _cleanup_(erase_and_freep) char *passphrase = NULL;
1053 struct crypt_params_tcrypt params = {
1054 .flags = CRYPT_TCRYPT_LEGACY_MODES,
1055 .keyfiles = (const char **)arg_tcrypt_keyfiles,
1056 .keyfiles_count = strv_length(arg_tcrypt_keyfiles)
1057 };
1058
1059 assert(cd);
1060 assert(name);
1061 assert(key_file || key_data || !strv_isempty(passwords));
1062
1063 if (arg_pkcs11_uri || arg_pkcs11_uri_auto || arg_fido2_device || arg_fido2_device_auto || arg_tpm2_device || arg_tpm2_device_auto)
1064 /* Ask for a regular password */
1065 return log_error_errno(SYNTHETIC_ERRNO(EAGAIN),
1066 "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11/fido2/tpm2 support.");
1067
1068 if (arg_tcrypt_hidden)
1069 params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
1070
1071 if (arg_tcrypt_system)
1072 params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER;
1073
1074 if (arg_tcrypt_veracrypt)
1075 params.flags |= CRYPT_TCRYPT_VERA_MODES;
1076
1077 if (arg_tcrypt_veracrypt && arg_tcrypt_veracrypt_pim != 0)
1078 params.veracrypt_pim = arg_tcrypt_veracrypt_pim;
1079
1080 if (key_data) {
1081 params.passphrase = key_data;
1082 params.passphrase_size = key_data_size;
1083 r = crypt_load(cd, CRYPT_TCRYPT, &params);
1084 } else if (key_file) {
1085 r = read_one_line_file(key_file, &passphrase);
1086 if (r < 0) {
1087 log_error_errno(r, "Failed to read password file '%s': %m", key_file);
1088 return -EAGAIN; /* log with the actual error, but return EAGAIN */
1089 }
1090 params.passphrase = passphrase;
1091 params.passphrase_size = strlen(passphrase);
1092 r = crypt_load(cd, CRYPT_TCRYPT, &params);
1093 } else {
1094 r = -EINVAL;
1095 STRV_FOREACH(p, passwords){
1096 params.passphrase = *p;
1097 params.passphrase_size = strlen(*p);
1098 r = crypt_load(cd, CRYPT_TCRYPT, &params);
1099 if (r >= 0)
1100 break;
1101 }
1102 }
1103
1104 if (r < 0) {
1105 if (r == -EPERM) {
1106 if (key_data)
1107 log_error_errno(r, "Failed to activate using discovered key. (Key not correct?)");
1108 else if (key_file)
1109 log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file);
1110 else
1111 log_error_errno(r, "Failed to activate using supplied passwords.");
1112
1113 return r;
1114 }
1115
1116 return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd));
1117 }
1118
1119 r = measured_crypt_activate_by_volume_key(cd, name, NULL, 0, flags);
1120 if (r < 0)
1121 return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd));
1122
1123 return 0;
1124 }
1125
1126 static char *make_bindname(const char *volume) {
1127 char *s;
1128
1129 if (asprintf(&s, "@%" PRIx64"/cryptsetup/%s", random_u64(), volume) < 0)
1130 return NULL;
1131
1132 return s;
1133 }
1134
1135 static int make_security_device_monitor(
1136 sd_event **ret_event,
1137 sd_device_monitor **ret_monitor) {
1138 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1139 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1140 int r;
1141
1142 assert(ret_event);
1143 assert(ret_monitor);
1144
1145 /* Waits for a device with "security-device" tag to show up in udev */
1146
1147 r = sd_event_default(&event);
1148 if (r < 0)
1149 return log_error_errno(r, "Failed to allocate event loop: %m");
1150
1151 r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
1152 if (r < 0)
1153 return log_error_errno(r, "Failed to install timeout event source: %m");
1154
1155 r = sd_device_monitor_new(&monitor);
1156 if (r < 0)
1157 return log_error_errno(r, "Failed to allocate device monitor: %m");
1158
1159 (void) sd_device_monitor_set_description(monitor, "security-device");
1160
1161 r = sd_device_monitor_filter_add_match_tag(monitor, "security-device");
1162 if (r < 0)
1163 return log_error_errno(r, "Failed to configure device monitor: %m");
1164
1165 r = sd_device_monitor_attach_event(monitor, event);
1166 if (r < 0)
1167 return log_error_errno(r, "Failed to attach device monitor: %m");
1168
1169 r = sd_device_monitor_start(monitor, NULL, NULL);
1170 if (r < 0)
1171 return log_error_errno(r, "Failed to start device monitor: %m");
1172
1173 *ret_event = TAKE_PTR(event);
1174 *ret_monitor = TAKE_PTR(monitor);
1175 return 0;
1176 }
1177
1178 static int run_security_device_monitor(
1179 sd_event *event,
1180 sd_device_monitor *monitor) {
1181 bool processed = false;
1182 int r;
1183
1184 assert(event);
1185 assert(monitor);
1186
1187 /* Runs the event loop for the device monitor until either something happens, or the time-out is
1188 * hit. */
1189
1190 for (;;) {
1191 int x;
1192
1193 r = sd_event_get_exit_code(event, &x);
1194 if (r < 0) {
1195 if (r != -ENODATA)
1196 return log_error_errno(r, "Failed to query exit code from event loop: %m");
1197
1198 /* On ENODATA we aren't told to exit yet. */
1199 } else {
1200 assert(x == -ETIMEDOUT);
1201 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
1202 "Timed out waiting for security device, aborting security device based authentication attempt.");
1203 }
1204
1205 /* Wait for one event, and then eat all subsequent events until there are no further ones */
1206 r = sd_event_run(event, processed ? 0 : UINT64_MAX);
1207 if (r < 0)
1208 return log_error_errno(r, "Failed to run event loop: %m");
1209 if (r == 0) /* no events queued anymore */
1210 return 0;
1211
1212 processed = true;
1213 }
1214 }
1215
1216 static bool libcryptsetup_plugins_support(void) {
1217
1218 #if HAVE_TPM2
1219 /* Currently, there's no way for us to query the volume key when plugins are used. Hence don't use
1220 * plugins, if measurement has been requested. */
1221 if (arg_tpm2_measure_pcr != UINT_MAX)
1222 return false;
1223 #endif
1224
1225 #if HAVE_LIBCRYPTSETUP_PLUGINS
1226 int r;
1227
1228 /* Permit a way to disable libcryptsetup token module support, for debugging purposes. */
1229 r = getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE");
1230 if (r < 0 && r != -ENXIO)
1231 log_debug_errno(r, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE env var: %m");
1232 if (r == 0)
1233 return false;
1234
1235 return crypt_token_external_path();
1236 #else
1237 return false;
1238 #endif
1239 }
1240
1241 #if HAVE_LIBCRYPTSETUP_PLUGINS
1242 static int acquire_pins_from_env_variable(char ***ret_pins) {
1243 _cleanup_(erase_and_freep) char *envpin = NULL;
1244 _cleanup_strv_free_erase_ char **pins = NULL;
1245 int r;
1246
1247 assert(ret_pins);
1248
1249 r = getenv_steal_erase("PIN", &envpin);
1250 if (r < 0)
1251 return log_error_errno(r, "Failed to acquire PIN from environment: %m");
1252 if (r > 0) {
1253 pins = strv_new(envpin);
1254 if (!pins)
1255 return log_oom();
1256 }
1257
1258 *ret_pins = TAKE_PTR(pins);
1259
1260 return 0;
1261 }
1262 #endif
1263
1264 static int crypt_activate_by_token_pin_ask_password(
1265 struct crypt_device *cd,
1266 const char *name,
1267 const char *type,
1268 usec_t until,
1269 bool headless,
1270 void *userdata,
1271 uint32_t activation_flags,
1272 const char *message,
1273 const char *keyring,
1274 const char *credential) {
1275
1276 #if HAVE_LIBCRYPTSETUP_PLUGINS
1277 AskPasswordFlags flags = arg_ask_password_flags | ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_ACCEPT_CACHED;
1278 _cleanup_strv_free_erase_ char **pins = NULL;
1279 int r;
1280
1281 r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, /* pin=*/ NULL, /* pin_size= */ 0, userdata, activation_flags);
1282 if (r > 0) /* returns unlocked keyslot id on success */
1283 return 0;
1284 if (r != -ENOANO) /* needs pin or pin is wrong */
1285 return r;
1286
1287 r = acquire_pins_from_env_variable(&pins);
1288 if (r < 0)
1289 return r;
1290
1291 STRV_FOREACH(p, pins) {
1292 r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), userdata, activation_flags);
1293 if (r > 0) /* returns unlocked keyslot id on success */
1294 return 0;
1295 if (r != -ENOANO) /* needs pin or pin is wrong */
1296 return r;
1297 }
1298
1299 if (headless)
1300 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the '$PIN' environment variable.");
1301
1302 for (;;) {
1303 pins = strv_free_erase(pins);
1304
1305 AskPasswordRequest req = {
1306 .message = message,
1307 .icon = "drive-harddisk",
1308 .keyring = keyring,
1309 .credential = credential,
1310 };
1311
1312 r = ask_password_auto(&req, until, flags, &pins);
1313 if (r < 0)
1314 return r;
1315
1316 STRV_FOREACH(p, pins) {
1317 r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), userdata, activation_flags);
1318 if (r > 0) /* returns unlocked keyslot id on success */
1319 return 0;
1320 if (r != -ENOANO) /* needs pin or pin is wrong */
1321 return r;
1322 }
1323
1324 flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
1325 }
1326 return r;
1327 #else
1328 return -EOPNOTSUPP;
1329 #endif
1330 }
1331
1332 static int attach_luks2_by_fido2_via_plugin(
1333 struct crypt_device *cd,
1334 const char *name,
1335 usec_t until,
1336 bool headless,
1337 void *userdata,
1338 uint32_t activation_flags) {
1339
1340 return crypt_activate_by_token_pin_ask_password(
1341 cd,
1342 name,
1343 "systemd-fido2",
1344 until,
1345 headless,
1346 userdata,
1347 activation_flags,
1348 "Please enter security token PIN:",
1349 "fido2-pin",
1350 "cryptsetup.fido2-pin");
1351 }
1352
1353 static int attach_luks_or_plain_or_bitlk_by_fido2(
1354 struct crypt_device *cd,
1355 const char *name,
1356 const char *key_file,
1357 const void *key_data,
1358 size_t key_data_size,
1359 usec_t until,
1360 uint32_t flags,
1361 bool pass_volume_key) {
1362
1363 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1364 _cleanup_(erase_and_freep) void *decrypted_key = NULL;
1365 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1366 size_t decrypted_key_size, cid_size = 0;
1367 _cleanup_free_ char *friendly = NULL;
1368 int keyslot = arg_key_slot, r;
1369 const char *rp_id = NULL;
1370 const void *cid = NULL;
1371 Fido2EnrollFlags required;
1372 bool use_libcryptsetup_plugin = libcryptsetup_plugins_support();
1373
1374 assert(cd);
1375 assert(name);
1376 assert(arg_fido2_device || arg_fido2_device_auto);
1377
1378 if (arg_fido2_cid) {
1379 if (!key_file && !key_data)
1380 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1381 "FIDO2 mode with manual parameters selected, but no keyfile specified, refusing.");
1382
1383 rp_id = arg_fido2_rp_id;
1384 cid = arg_fido2_cid;
1385 cid_size = arg_fido2_cid_size;
1386
1387 /* For now and for compatibility, if the user explicitly configured FIDO2 support and we do
1388 * not read FIDO2 metadata off the LUKS2 header, default to the systemd 248 logic, where we
1389 * use PIN + UP when needed, and do not configure UV at all. Eventually, we should make this
1390 * explicitly configurable. */
1391 required = FIDO2ENROLL_PIN_IF_NEEDED | FIDO2ENROLL_UP_IF_NEEDED | FIDO2ENROLL_UV_OMIT;
1392 }
1393
1394 friendly = friendly_disk_name(crypt_get_device_name(cd), name);
1395 if (!friendly)
1396 return log_oom();
1397
1398 for (;;) {
1399 if (use_libcryptsetup_plugin && !arg_fido2_cid) {
1400 r = attach_luks2_by_fido2_via_plugin(cd, name, until, arg_headless, arg_fido2_device, flags);
1401 if (IN_SET(r, -ENOTUNIQ, -ENXIO, -ENOENT))
1402 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
1403 "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1404
1405 } else {
1406 if (cid)
1407 r = acquire_fido2_key(
1408 name,
1409 friendly,
1410 arg_fido2_device,
1411 rp_id,
1412 cid, cid_size,
1413 key_file, arg_keyfile_size, arg_keyfile_offset,
1414 key_data, key_data_size,
1415 until,
1416 arg_headless,
1417 required,
1418 &decrypted_key, &decrypted_key_size,
1419 arg_ask_password_flags);
1420 else
1421 r = acquire_fido2_key_auto(
1422 cd,
1423 name,
1424 friendly,
1425 arg_fido2_device,
1426 until,
1427 arg_headless,
1428 &decrypted_key, &decrypted_key_size,
1429 arg_ask_password_flags);
1430 if (r >= 0)
1431 break;
1432 }
1433
1434 if (r != -EAGAIN) /* EAGAIN means: token not found */
1435 return r;
1436
1437 if (!monitor) {
1438 /* We didn't find the token. In this case, watch for it via udev. Let's
1439 * create an event loop and monitor first. */
1440
1441 assert(!event);
1442
1443 r = make_security_device_monitor(&event, &monitor);
1444 if (r < 0)
1445 return r;
1446
1447 log_notice("Security token not present for unlocking volume %s, please plug it in.", friendly);
1448
1449 /* Let's immediately rescan in case the token appeared in the time we needed
1450 * to create and configure the monitor */
1451 continue;
1452 }
1453
1454 r = run_security_device_monitor(event, monitor);
1455 if (r < 0)
1456 return r;
1457
1458 log_debug("Got one or more potentially relevant udev events, rescanning FIDO2...");
1459 }
1460
1461 if (pass_volume_key)
1462 r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags);
1463 else {
1464 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1465 ssize_t base64_encoded_size;
1466
1467 /* Before using this key as passphrase we base64 encode it, for compat with homed */
1468
1469 base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
1470 if (base64_encoded_size < 0)
1471 return log_oom();
1472
1473 r = measured_crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, base64_encoded_size, flags);
1474 }
1475 if (r == -EPERM) {
1476 log_error_errno(r, "Failed to activate with FIDO2 decrypted key. (Key incorrect?)");
1477 return -EAGAIN; /* log actual error, but return EAGAIN */
1478 }
1479 if (r < 0)
1480 return log_error_errno(r, "Failed to activate with FIDO2 acquired key: %m");
1481
1482 return 0;
1483 }
1484
1485 static int attach_luks2_by_pkcs11_via_plugin(
1486 struct crypt_device *cd,
1487 const char *name,
1488 const char *friendly_name,
1489 usec_t until,
1490 bool headless,
1491 uint32_t flags) {
1492
1493 #if HAVE_LIBCRYPTSETUP_PLUGINS
1494 int r;
1495
1496 if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2))
1497 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Automatic PKCS#11 metadata requires LUKS2 device.");
1498
1499 systemd_pkcs11_plugin_params params = {
1500 .friendly_name = friendly_name,
1501 .until = until,
1502 .headless = headless,
1503 .askpw_flags = arg_ask_password_flags,
1504 };
1505
1506 r = crypt_activate_by_token_pin(cd, name, "systemd-pkcs11", CRYPT_ANY_TOKEN, NULL, 0, &params, flags);
1507 if (r > 0) /* returns unlocked keyslot id on success */
1508 r = 0;
1509
1510 return r;
1511 #else
1512 return -EOPNOTSUPP;
1513 #endif
1514 }
1515
1516 static int attach_luks_or_plain_or_bitlk_by_pkcs11(
1517 struct crypt_device *cd,
1518 const char *name,
1519 const char *key_file,
1520 const void *key_data,
1521 size_t key_data_size,
1522 usec_t until,
1523 uint32_t flags,
1524 bool pass_volume_key) {
1525
1526 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1527 _cleanup_free_ char *friendly = NULL, *discovered_uri = NULL;
1528 size_t decrypted_key_size = 0, discovered_key_size = 0;
1529 _cleanup_(erase_and_freep) void *decrypted_key = NULL;
1530 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1531 _cleanup_free_ void *discovered_key = NULL;
1532 int keyslot = arg_key_slot, r;
1533 const char *uri = NULL;
1534 bool use_libcryptsetup_plugin = libcryptsetup_plugins_support();
1535
1536 assert(cd);
1537 assert(name);
1538 assert(arg_pkcs11_uri || arg_pkcs11_uri_auto);
1539
1540 if (arg_pkcs11_uri_auto) {
1541 if (!use_libcryptsetup_plugin) {
1542 r = find_pkcs11_auto_data(cd, &discovered_uri, &discovered_key, &discovered_key_size, &keyslot);
1543 if (IN_SET(r, -ENOTUNIQ, -ENXIO))
1544 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
1545 "Automatic PKCS#11 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1546 if (r < 0)
1547 return r;
1548
1549 uri = discovered_uri;
1550 key_data = discovered_key;
1551 key_data_size = discovered_key_size;
1552 }
1553 } else {
1554 uri = arg_pkcs11_uri;
1555
1556 if (!key_file && !key_data)
1557 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 mode selected but no key file specified, refusing.");
1558 }
1559
1560 friendly = friendly_disk_name(crypt_get_device_name(cd), name);
1561 if (!friendly)
1562 return log_oom();
1563
1564 for (;;) {
1565 if (use_libcryptsetup_plugin && arg_pkcs11_uri_auto)
1566 r = attach_luks2_by_pkcs11_via_plugin(cd, name, friendly, until, arg_headless, flags);
1567 else {
1568 r = decrypt_pkcs11_key(
1569 name,
1570 friendly,
1571 uri,
1572 key_file, arg_keyfile_size, arg_keyfile_offset,
1573 key_data, key_data_size,
1574 until,
1575 arg_headless,
1576 &decrypted_key, &decrypted_key_size);
1577 if (r >= 0)
1578 break;
1579 }
1580
1581 if (r != -EAGAIN) /* EAGAIN means: token not found */
1582 return r;
1583
1584 if (!monitor) {
1585 /* We didn't find the token. In this case, watch for it via udev. Let's
1586 * create an event loop and monitor first. */
1587
1588 assert(!event);
1589
1590 r = make_security_device_monitor(&event, &monitor);
1591 if (r < 0)
1592 return r;
1593
1594 log_notice("Security token%s%s not present for unlocking volume %s, please plug it in.",
1595 uri ? " " : "", strempty(uri), friendly);
1596
1597 /* Let's immediately rescan in case the token appeared in the time we needed
1598 * to create and configure the monitor */
1599 continue;
1600 }
1601
1602 r = run_security_device_monitor(event, monitor);
1603 if (r < 0)
1604 return r;
1605
1606 log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11...");
1607 }
1608 assert(decrypted_key);
1609
1610 if (pass_volume_key)
1611 r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags);
1612 else {
1613 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1614 ssize_t base64_encoded_size;
1615
1616 /* Before using this key as passphrase we base64 encode it. Why? For compatibility
1617 * with homed's PKCS#11 hookup: there we want to use the key we acquired through
1618 * PKCS#11 for other authentication/decryption mechanisms too, and some of them do
1619 * not take arbitrary binary blobs, but require NUL-terminated strings — most
1620 * importantly UNIX password hashes. Hence, for compatibility we want to use a string
1621 * without embedded NUL here too, and that's easiest to generate from a binary blob
1622 * via base64 encoding. */
1623
1624 base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
1625 if (base64_encoded_size < 0)
1626 return log_oom();
1627
1628 r = measured_crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, base64_encoded_size, flags);
1629 }
1630 if (r == -EPERM) {
1631 log_error_errno(r, "Failed to activate with PKCS#11 decrypted key. (Key incorrect?)");
1632 return -EAGAIN; /* log actual error, but return EAGAIN */
1633 }
1634 if (r < 0)
1635 return log_error_errno(r, "Failed to activate with PKCS#11 acquired key: %m");
1636
1637 return 0;
1638 }
1639
1640 static int make_tpm2_device_monitor(
1641 sd_event **ret_event,
1642 sd_device_monitor **ret_monitor) {
1643
1644 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1645 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1646 int r;
1647
1648 assert(ret_event);
1649 assert(ret_monitor);
1650
1651 r = sd_event_default(&event);
1652 if (r < 0)
1653 return log_error_errno(r, "Failed to allocate event loop: %m");
1654
1655 r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
1656 if (r < 0)
1657 return log_error_errno(r, "Failed to install timeout event source: %m");
1658
1659 r = sd_device_monitor_new(&monitor);
1660 if (r < 0)
1661 return log_error_errno(r, "Failed to allocate device monitor: %m");
1662
1663 (void) sd_device_monitor_set_description(monitor, "tpmrm");
1664
1665 r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "tpmrm", NULL);
1666 if (r < 0)
1667 return log_error_errno(r, "Failed to configure device monitor: %m");
1668
1669 r = sd_device_monitor_attach_event(monitor, event);
1670 if (r < 0)
1671 return log_error_errno(r, "Failed to attach device monitor: %m");
1672
1673 r = sd_device_monitor_start(monitor, NULL, NULL);
1674 if (r < 0)
1675 return log_error_errno(r, "Failed to start device monitor: %m");
1676
1677 *ret_event = TAKE_PTR(event);
1678 *ret_monitor = TAKE_PTR(monitor);
1679 return 0;
1680 }
1681
1682 static bool use_token_plugins(void) {
1683 int r;
1684
1685 /* Disable tokens if we shall measure, since we won't get access to the volume key then. */
1686 if (arg_tpm2_measure_pcr != UINT_MAX)
1687 return false;
1688
1689 r = getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE");
1690 if (r < 0 && r != -ENXIO)
1691 log_debug_errno(r, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE value, ignoring: %m");
1692
1693 return r != 0;
1694 }
1695
1696 static int attach_luks2_by_tpm2_via_plugin(
1697 struct crypt_device *cd,
1698 const char *name,
1699 usec_t until,
1700 bool headless,
1701 uint32_t flags) {
1702
1703 #if HAVE_LIBCRYPTSETUP_PLUGINS
1704 systemd_tpm2_plugin_params params = {
1705 .search_pcr_mask = arg_tpm2_pcr_mask,
1706 .device = arg_tpm2_device,
1707 .signature_path = arg_tpm2_signature,
1708 .pcrlock_path = arg_tpm2_pcrlock,
1709 };
1710
1711 if (!libcryptsetup_plugins_support())
1712 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1713 "Libcryptsetup has external plugins support disabled.");
1714
1715 return crypt_activate_by_token_pin_ask_password(
1716 cd,
1717 name,
1718 "systemd-tpm2",
1719 until,
1720 headless,
1721 &params,
1722 flags,
1723 "Please enter TPM2 PIN:",
1724 "tpm2-pin",
1725 "cryptsetup.tpm2-pin");
1726 #else
1727 return -EOPNOTSUPP;
1728 #endif
1729 }
1730
1731 static int attach_luks_or_plain_or_bitlk_by_tpm2(
1732 struct crypt_device *cd,
1733 const char *name,
1734 const char *key_file,
1735 const struct iovec *key_data,
1736 usec_t until,
1737 uint32_t flags,
1738 bool pass_volume_key) {
1739
1740 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1741 _cleanup_(iovec_done_erase) struct iovec decrypted_key = {};
1742 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1743 _cleanup_free_ char *friendly = NULL;
1744 int keyslot = arg_key_slot, r;
1745
1746 assert(cd);
1747 assert(name);
1748 assert(arg_tpm2_device || arg_tpm2_device_auto);
1749
1750 friendly = friendly_disk_name(crypt_get_device_name(cd), name);
1751 if (!friendly)
1752 return log_oom();
1753
1754 for (;;) {
1755 if (key_file || iovec_is_set(key_data)) {
1756 /* If key data is specified, use that */
1757
1758 r = acquire_tpm2_key(
1759 name,
1760 arg_tpm2_device,
1761 arg_tpm2_pcr_mask == UINT32_MAX ? TPM2_PCR_MASK_DEFAULT : arg_tpm2_pcr_mask,
1762 UINT16_MAX,
1763 /* pubkey= */ NULL,
1764 /* pubkey_pcr_mask= */ 0,
1765 /* signature_path= */ NULL,
1766 /* pcrlock_path= */ NULL,
1767 /* primary_alg= */ 0,
1768 key_file, arg_keyfile_size, arg_keyfile_offset,
1769 key_data,
1770 /* policy_hash= */ NULL, /* we don't know the policy hash */
1771 /* salt= */ NULL,
1772 /* srk= */ NULL,
1773 /* pcrlock_nv= */ NULL,
1774 arg_tpm2_pin ? TPM2_FLAGS_USE_PIN : 0,
1775 until,
1776 arg_headless,
1777 arg_ask_password_flags,
1778 &decrypted_key);
1779 if (r >= 0)
1780 break;
1781 if (IN_SET(r, -EACCES, -ENOLCK))
1782 return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
1783 if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */
1784 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking.");
1785 /* EAGAIN means: no tpm2 chip found */
1786 if (r != -EAGAIN) {
1787 log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
1788 return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
1789 }
1790 } else {
1791 r = attach_luks2_by_tpm2_via_plugin(cd, name, until, arg_headless, flags);
1792 if (r >= 0)
1793 return 0;
1794 /* EAGAIN means: no tpm2 chip found
1795 * EOPNOTSUPP means: no libcryptsetup plugins support */
1796 if (r == -ENXIO)
1797 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
1798 "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking.");
1799 if (r == -ENOENT)
1800 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
1801 "No TPM2 metadata enrolled in LUKS2 header or TPM2 support not available, falling back to traditional unlocking.");
1802 if (!IN_SET(r, -EOPNOTSUPP, -EAGAIN)) {
1803 log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
1804 return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
1805 }
1806 }
1807
1808 if (r == -EOPNOTSUPP) { /* Plugin not available, let's process TPM2 stuff right here instead */
1809 bool found_some = false;
1810 int token = 0; /* first token to look at */
1811
1812 /* If no key data is specified, look for it in the header. In order to support
1813 * software upgrades we'll iterate through all suitable tokens, maybe one of them
1814 * works. */
1815
1816 for (;;) {
1817 _cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {};
1818 _cleanup_(iovec_done) struct iovec blob = {}, policy_hash = {};
1819 uint32_t hash_pcr_mask, pubkey_pcr_mask;
1820 uint16_t pcr_bank, primary_alg;
1821 TPM2Flags tpm2_flags;
1822
1823 r = find_tpm2_auto_data(
1824 cd,
1825 arg_tpm2_pcr_mask, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */
1826 token, /* search for the token with this index, or any later index than this */
1827 &hash_pcr_mask,
1828 &pcr_bank,
1829 &pubkey,
1830 &pubkey_pcr_mask,
1831 &primary_alg,
1832 &blob,
1833 &policy_hash,
1834 &salt,
1835 &srk,
1836 &pcrlock_nv,
1837 &tpm2_flags,
1838 &keyslot,
1839 &token);
1840 if (r == -ENXIO)
1841 /* No further TPM2 tokens found in the LUKS2 header. */
1842 return log_full_errno(found_some ? LOG_NOTICE : LOG_DEBUG,
1843 SYNTHETIC_ERRNO(EAGAIN),
1844 found_some
1845 ? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking."
1846 : "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking.");
1847 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
1848 /* TPM2 support not compiled in? */
1849 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
1850 "TPM2 support not available, falling back to traditional unlocking.");
1851 if (r < 0)
1852 return r;
1853
1854 found_some = true;
1855
1856 r = acquire_tpm2_key(
1857 name,
1858 arg_tpm2_device,
1859 hash_pcr_mask,
1860 pcr_bank,
1861 &pubkey,
1862 pubkey_pcr_mask,
1863 arg_tpm2_signature,
1864 arg_tpm2_pcrlock,
1865 primary_alg,
1866 /* key_file= */ NULL, /* key_file_size= */ 0, /* key_file_offset= */ 0, /* no key file */
1867 &blob,
1868 &policy_hash,
1869 &salt,
1870 &srk,
1871 &pcrlock_nv,
1872 tpm2_flags,
1873 until,
1874 arg_headless,
1875 arg_ask_password_flags,
1876 &decrypted_key);
1877 if (IN_SET(r, -EACCES, -ENOLCK))
1878 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
1879 if (r != -EPERM)
1880 break;
1881
1882 token++; /* try a different token next time */
1883 }
1884
1885 if (r >= 0)
1886 break;
1887 /* EAGAIN means: no tpm2 chip found */
1888 if (r != -EAGAIN) {
1889 log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
1890 return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
1891 }
1892 }
1893
1894 if (!monitor) {
1895 /* We didn't find the TPM2 device. In this case, watch for it via udev. Let's create
1896 * an event loop and monitor first. */
1897
1898 assert(!event);
1899
1900 if (is_efi_boot() && !efi_has_tpm2())
1901 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
1902 "No TPM2 hardware discovered and EFI firmware does not see it either, falling back to traditional unlocking.");
1903
1904 r = make_tpm2_device_monitor(&event, &monitor);
1905 if (r < 0)
1906 return r;
1907
1908 log_info("TPM2 device not present for unlocking %s, waiting for it to become available.", friendly);
1909
1910 /* Let's immediately rescan in case the device appeared in the time we needed
1911 * to create and configure the monitor */
1912 continue;
1913 }
1914
1915 r = run_security_device_monitor(event, monitor);
1916 if (r < 0)
1917 return r;
1918
1919 log_debug("Got one or more potentially relevant udev events, rescanning for TPM2...");
1920 }
1921
1922 if (pass_volume_key)
1923 r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key.iov_base, decrypted_key.iov_len, flags);
1924 else {
1925 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1926 ssize_t base64_encoded_size;
1927
1928 /* Before using this key as passphrase we base64 encode it, for compat with homed */
1929
1930 base64_encoded_size = base64mem(decrypted_key.iov_base, decrypted_key.iov_len, &base64_encoded);
1931 if (base64_encoded_size < 0)
1932 return log_oom();
1933
1934 r = measured_crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, base64_encoded_size, flags);
1935 }
1936 if (r == -EPERM) {
1937 log_error_errno(r, "Failed to activate with TPM2 decrypted key. (Key incorrect?)");
1938 return -EAGAIN; /* log actual error, but return EAGAIN */
1939 }
1940 if (r < 0)
1941 return log_error_errno(r, "Failed to activate with TPM2 acquired key: %m");
1942
1943 return 0;
1944 }
1945
1946 static int attach_luks_or_plain_or_bitlk_by_key_data(
1947 struct crypt_device *cd,
1948 const char *name,
1949 const void *key_data,
1950 size_t key_data_size,
1951 uint32_t flags,
1952 bool pass_volume_key) {
1953
1954 int r;
1955
1956 assert(cd);
1957 assert(name);
1958 assert(key_data);
1959
1960 if (pass_volume_key)
1961 r = measured_crypt_activate_by_volume_key(cd, name, key_data, key_data_size, flags);
1962 else
1963 r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, key_data, key_data_size, flags);
1964 if (r == -EPERM) {
1965 log_error_errno(r, "Failed to activate. (Key incorrect?)");
1966 return -EAGAIN; /* Log actual error, but return EAGAIN */
1967 }
1968 if (r < 0)
1969 return log_error_errno(r, "Failed to activate: %m");
1970
1971 return 0;
1972 }
1973
1974 static int attach_luks_or_plain_or_bitlk_by_key_file(
1975 struct crypt_device *cd,
1976 const char *name,
1977 const char *key_file,
1978 uint32_t flags,
1979 bool pass_volume_key) {
1980
1981 _cleanup_(erase_and_freep) char *kfdata = NULL;
1982 _cleanup_free_ char *bindname = NULL;
1983 size_t kfsize;
1984 int r;
1985
1986 assert(cd);
1987 assert(name);
1988 assert(key_file);
1989
1990 /* If we read the key via AF_UNIX, make this client recognizable */
1991 bindname = make_bindname(name);
1992 if (!bindname)
1993 return log_oom();
1994
1995 r = read_full_file_full(
1996 AT_FDCWD, key_file,
1997 arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset,
1998 arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size,
1999 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
2000 bindname,
2001 &kfdata, &kfsize);
2002 if (r == -E2BIG) {
2003 log_error_errno(r, "Failed to activate, key file '%s' too large.", key_file);
2004 return -EAGAIN;
2005 }
2006 if (r == -ENOENT) {
2007 log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file);
2008 return -EAGAIN; /* Log actual error, but return EAGAIN */
2009 }
2010 if (r < 0)
2011 return log_error_errno(r, "Failed to read key file '%s': %m", key_file);
2012
2013 if (pass_volume_key)
2014 r = measured_crypt_activate_by_volume_key(cd, name, kfdata, kfsize, flags);
2015 else
2016 r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, kfdata, kfsize, flags);
2017 if (r == -EPERM) {
2018 log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file);
2019 return -EAGAIN; /* Log actual error, but return EAGAIN */
2020 }
2021 if (r < 0)
2022 return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file);
2023
2024 return 0;
2025 }
2026
2027 static int attach_luks_or_plain_or_bitlk_by_passphrase(
2028 struct crypt_device *cd,
2029 const char *name,
2030 char **passwords,
2031 uint32_t flags,
2032 bool pass_volume_key) {
2033
2034 int r;
2035
2036 assert(cd);
2037 assert(name);
2038
2039 r = -EINVAL;
2040 STRV_FOREACH(p, passwords) {
2041 if (pass_volume_key)
2042 r = measured_crypt_activate_by_volume_key(cd, name, *p, arg_key_size, flags);
2043 else
2044 r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, *p, strlen(*p), flags);
2045 if (r >= 0)
2046 break;
2047 }
2048 if (r == -EPERM) {
2049 log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)");
2050 return -EAGAIN; /* log actual error, but return EAGAIN */
2051 }
2052 if (r < 0)
2053 return log_error_errno(r, "Failed to activate with specified passphrase: %m");
2054
2055 return 0;
2056 }
2057
2058 static int attach_luks_or_plain_or_bitlk(
2059 struct crypt_device *cd,
2060 const char *name,
2061 const char *key_file,
2062 const void *key_data,
2063 size_t key_data_size,
2064 char **passwords,
2065 uint32_t flags,
2066 usec_t until) {
2067
2068 bool pass_volume_key = false;
2069 int r;
2070
2071 assert(cd);
2072 assert(name);
2073
2074 if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) {
2075 struct crypt_params_plain params = {
2076 .offset = arg_offset,
2077 .skip = arg_skip,
2078 .sector_size = arg_sector_size,
2079 };
2080 const char *cipher, *cipher_mode;
2081 _cleanup_free_ char *truncated_cipher = NULL;
2082
2083 if (streq_ptr(arg_hash, "plain"))
2084 /* plain isn't a real hash type. it just means "use no hash" */
2085 params.hash = NULL;
2086 else if (arg_hash)
2087 params.hash = arg_hash;
2088 else if (!key_file)
2089 /* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key
2090 * file is provided */
2091 params.hash = "ripemd160";
2092
2093 if (arg_cipher) {
2094 size_t l;
2095
2096 l = strcspn(arg_cipher, "-");
2097 truncated_cipher = strndup(arg_cipher, l);
2098 if (!truncated_cipher)
2099 return log_oom();
2100
2101 cipher = truncated_cipher;
2102 cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain";
2103 } else {
2104 cipher = "aes";
2105 cipher_mode = "cbc-essiv:sha256";
2106 }
2107
2108 /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */
2109 arg_keyfile_size = arg_key_size;
2110
2111 /* In contrast to what the name crypt_format() might suggest this doesn't actually format
2112 * anything, it just configures encryption parameters when used for plain mode. */
2113 r = crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, &params);
2114 if (r < 0)
2115 return log_error_errno(r, "Loading of cryptographic parameters failed: %m");
2116
2117 /* hash == NULL implies the user passed "plain" */
2118 pass_volume_key = !params.hash;
2119 }
2120
2121 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
2122 crypt_get_cipher(cd),
2123 crypt_get_cipher_mode(cd),
2124 crypt_get_volume_key_size(cd)*8,
2125 crypt_get_device_name(cd));
2126
2127 if (arg_tpm2_device || arg_tpm2_device_auto)
2128 return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, &IOVEC_MAKE(key_data, key_data_size), until, flags, pass_volume_key);
2129 if (arg_fido2_device || arg_fido2_device_auto)
2130 return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key);
2131 if (arg_pkcs11_uri || arg_pkcs11_uri_auto)
2132 return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key);
2133 if (key_data)
2134 return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, key_data_size, flags, pass_volume_key);
2135 if (key_file)
2136 return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key);
2137
2138 return attach_luks_or_plain_or_bitlk_by_passphrase(cd, name, passwords, flags, pass_volume_key);
2139 }
2140
2141 static int help(void) {
2142 _cleanup_free_ char *link = NULL;
2143 int r;
2144
2145 r = terminal_urlify_man("systemd-cryptsetup", "8", &link);
2146 if (r < 0)
2147 return log_oom();
2148
2149 printf("%1$s attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG]\n"
2150 "%1$s detach VOLUME\n\n"
2151 "%2$sAttach or detach an encrypted block device.%3$s\n\n"
2152 " -h --help Show this help\n"
2153 " --version Show package version\n"
2154 "\nSee the %4$s for details.\n",
2155 program_invocation_short_name,
2156 ansi_highlight(),
2157 ansi_normal(),
2158 link);
2159
2160 return 0;
2161 }
2162
2163 static int parse_argv(int argc, char *argv[]) {
2164 enum {
2165 ARG_VERSION = 0x100,
2166 };
2167
2168 static const struct option options[] = {
2169 { "help", no_argument, NULL, 'h' },
2170 { "version", no_argument, NULL, ARG_VERSION },
2171 {}
2172 };
2173
2174 int c;
2175
2176 assert(argc >= 0);
2177 assert(argv);
2178
2179 if (argv_looks_like_help(argc, argv))
2180 return help();
2181
2182 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
2183 switch (c) {
2184
2185 case 'h':
2186 return help();
2187
2188 case ARG_VERSION:
2189 return version();
2190
2191 case '?':
2192 return -EINVAL;
2193
2194 default:
2195 assert_not_reached();
2196 }
2197
2198 return 1;
2199 }
2200
2201 static uint32_t determine_flags(void) {
2202 uint32_t flags = 0;
2203
2204 if (arg_readonly)
2205 flags |= CRYPT_ACTIVATE_READONLY;
2206
2207 if (arg_discards)
2208 flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
2209
2210 if (arg_same_cpu_crypt)
2211 flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
2212
2213 if (arg_submit_from_crypt_cpus)
2214 flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
2215
2216 if (arg_no_read_workqueue)
2217 flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
2218
2219 if (arg_no_write_workqueue)
2220 flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
2221
2222 #ifdef CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF
2223 /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
2224 /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
2225 flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF;
2226 #endif
2227
2228 return flags;
2229 }
2230
2231 static void remove_and_erasep(const char **p) {
2232 int r;
2233
2234 if (!*p)
2235 return;
2236
2237 r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE);
2238 if (r < 0 && r != -ENOENT)
2239 log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p);
2240 }
2241
2242 static int run(int argc, char *argv[]) {
2243 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
2244 const char *verb;
2245 int r;
2246
2247 log_setup();
2248
2249 umask(0022);
2250
2251 r = parse_argv(argc, argv);
2252 if (r <= 0)
2253 return r;
2254
2255 cryptsetup_enable_logging(NULL);
2256
2257 if (argc - optind < 2)
2258 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2259 "This program requires at least two arguments.");
2260 verb = ASSERT_PTR(argv[optind]);
2261
2262 if (streq(verb, "attach")) {
2263 _unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL;
2264 _cleanup_(erase_and_freep) void *key_data = NULL;
2265 crypt_status_info status;
2266 size_t key_data_size = 0;
2267 uint32_t flags = 0;
2268 unsigned tries;
2269 usec_t until;
2270 PassphraseType passphrase_type = PASSPHRASE_NONE;
2271
2272 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG] */
2273
2274 if (argc - optind < 3)
2275 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach requires at least two arguments.");
2276 if (argc - optind >= 6)
2277 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach does not accept more than four arguments.");
2278
2279 const char *volume = ASSERT_PTR(argv[optind + 1]),
2280 *source = ASSERT_PTR(argv[optind + 2]),
2281 *key_file = argc - optind >= 4 ? mangle_none(argv[optind + 3]) : NULL,
2282 *config = argc - optind >= 5 ? mangle_none(argv[optind + 4]) : NULL;
2283
2284 if (!filename_is_valid(volume))
2285 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
2286
2287 if (key_file && !path_is_absolute(key_file)) {
2288 log_warning("Password file path '%s' is not absolute. Ignoring.", key_file);
2289 key_file = NULL;
2290 }
2291
2292 if (config) {
2293 r = parse_crypt_config(config);
2294 if (r < 0)
2295 return r;
2296 }
2297
2298 log_debug("%s %s ← %s type=%s cipher=%s", __func__,
2299 volume, source, strempty(arg_type), strempty(arg_cipher));
2300
2301 /* A delicious drop of snake oil */
2302 (void) mlockall(MCL_FUTURE);
2303
2304 if (!key_file) {
2305 _cleanup_free_ char *bindname = NULL;
2306 const char *fn;
2307
2308 bindname = make_bindname(volume);
2309 if (!bindname)
2310 return log_oom();
2311
2312 /* If a key file is not explicitly specified, search for a key in a well defined
2313 * search path, and load it. */
2314
2315 fn = strjoina(volume, ".key");
2316 r = find_key_file(
2317 fn,
2318 STRV_MAKE("/etc/cryptsetup-keys.d", "/run/cryptsetup-keys.d"),
2319 bindname,
2320 &key_data, &key_data_size);
2321 if (r < 0)
2322 return r;
2323 if (r > 0)
2324 log_debug("Automatically discovered key for volume '%s'.", volume);
2325 } else if (arg_keyfile_erase)
2326 destroy_key_file = key_file; /* let's get this baby erased when we leave */
2327
2328 if (arg_header) {
2329 if (streq_ptr(arg_type, CRYPT_TCRYPT)){
2330 log_debug("tcrypt header: %s", arg_header);
2331 r = crypt_init_data_device(&cd, arg_header, source);
2332 } else {
2333 log_debug("LUKS header: %s", arg_header);
2334 r = crypt_init(&cd, arg_header);
2335 }
2336 } else
2337 r = crypt_init(&cd, source);
2338 if (r < 0)
2339 return log_error_errno(r, "crypt_init() failed: %m");
2340
2341 cryptsetup_enable_logging(cd);
2342
2343 status = crypt_status(cd, volume);
2344 if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
2345 log_info("Volume %s already active.", volume);
2346 return 0;
2347 }
2348
2349 flags = determine_flags();
2350
2351 until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
2352 if (until == USEC_INFINITY)
2353 until = 0;
2354
2355 if (arg_key_size == 0)
2356 arg_key_size = 256U / 8U;
2357
2358 if (key_file) {
2359 struct stat st;
2360
2361 /* Ideally we'd do this on the open fd, but since this is just a
2362 * warning it's OK to do this in two steps. */
2363 if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005))
2364 log_warning("Key file %s is world-readable. This is not a good idea!", key_file);
2365 }
2366
2367 if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) {
2368 r = crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL);
2369 if (r < 0)
2370 return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd));
2371
2372 /* since cryptsetup 2.7.0 (Jan 2024) */
2373 #if HAVE_CRYPT_SET_KEYRING_TO_LINK
2374 if (arg_link_key_description) {
2375 r = crypt_set_keyring_to_link(cd, arg_link_key_description, NULL, arg_link_key_type, arg_link_keyring);
2376 if (r < 0)
2377 log_warning_errno(r, "Failed to set keyring or key description to link volume key in, ignoring: %m");
2378 }
2379 #endif
2380
2381 if (arg_header) {
2382 r = crypt_set_data_device(cd, source);
2383 if (r < 0)
2384 return log_error_errno(r, "Failed to set LUKS data device %s: %m", source);
2385 }
2386
2387 /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */
2388 if (!key_file && !key_data && use_token_plugins()) {
2389 r = crypt_activate_by_token_pin_ask_password(
2390 cd,
2391 volume,
2392 /* type= */ NULL,
2393 until,
2394 arg_headless,
2395 /* userdata= */ NULL,
2396 flags,
2397 "Please enter LUKS2 token PIN:",
2398 "luks2-pin",
2399 "cryptsetup.luks2-pin");
2400 if (r >= 0) {
2401 log_debug("Volume %s activated with LUKS token id %i.", volume, r);
2402 return 0;
2403 }
2404
2405 log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd));
2406 }
2407 }
2408
2409 /* since cryptsetup 2.3.0 (Feb 2020) */
2410 #ifdef CRYPT_BITLK
2411 if (streq_ptr(arg_type, CRYPT_BITLK)) {
2412 r = crypt_load(cd, CRYPT_BITLK, NULL);
2413 if (r < 0)
2414 return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd));
2415 }
2416 #endif
2417
2418 for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
2419 _cleanup_strv_free_erase_ char **passwords = NULL;
2420
2421 /* When we were able to acquire multiple keys, let's always process them in this order:
2422 *
2423 * 1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip
2424 * 2. The discovered key: i.e. key_data + key_data_size
2425 * 3. The configured key: i.e. key_file + arg_keyfile_offset + arg_keyfile_size
2426 * 4. The empty password, in case arg_try_empty_password is set
2427 * 5. We enquire the user for a password
2428 */
2429
2430 if (!key_file && !key_data && !arg_pkcs11_uri && !arg_pkcs11_uri_auto && !arg_fido2_device && !arg_fido2_device_auto && !arg_tpm2_device && !arg_tpm2_device_auto) {
2431
2432 if (arg_try_empty_password) {
2433 /* Hmm, let's try an empty password now, but only once */
2434 arg_try_empty_password = false;
2435
2436 key_data = strdup("");
2437 if (!key_data)
2438 return log_oom();
2439
2440 key_data_size = 0;
2441 } else {
2442 /* Ask the user for a passphrase or recovery key only as last resort, if we have
2443 * nothing else to check for */
2444 if (passphrase_type == PASSPHRASE_NONE) {
2445 passphrase_type = check_registered_passwords(cd);
2446 if (passphrase_type == PASSPHRASE_NONE)
2447 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No passphrase or recovery key registered.");
2448 }
2449
2450 r = get_password(volume, source, until, tries == 0 && !arg_verify, passphrase_type, &passwords);
2451 if (r == -EAGAIN)
2452 continue;
2453 if (r < 0)
2454 return r;
2455 }
2456 }
2457
2458 if (streq_ptr(arg_type, CRYPT_TCRYPT))
2459 r = attach_tcrypt(cd, volume, key_file, key_data, key_data_size, passwords, flags);
2460 else
2461 r = attach_luks_or_plain_or_bitlk(cd, volume, key_file, key_data, key_data_size, passwords, flags, until);
2462 if (r >= 0)
2463 break;
2464 if (r != -EAGAIN)
2465 return r;
2466
2467 /* Key not correct? Let's try again! */
2468
2469 key_file = NULL;
2470 key_data = erase_and_free(key_data);
2471 key_data_size = 0;
2472 arg_pkcs11_uri = mfree(arg_pkcs11_uri);
2473 arg_pkcs11_uri_auto = false;
2474 arg_fido2_device = mfree(arg_fido2_device);
2475 arg_fido2_device_auto = false;
2476 arg_tpm2_device = mfree(arg_tpm2_device);
2477 arg_tpm2_device_auto = false;
2478 }
2479
2480 if (arg_tries != 0 && tries >= arg_tries)
2481 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up.");
2482
2483 } else if (streq(verb, "detach")) {
2484 const char *volume = ASSERT_PTR(argv[optind + 1]);
2485
2486 if (argc - optind >= 3)
2487 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach does not accept more than one argument.");
2488
2489 if (!filename_is_valid(volume))
2490 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
2491
2492 r = crypt_init_by_name(&cd, volume);
2493 if (r == -ENODEV) {
2494 log_info("Volume %s already inactive.", volume);
2495 return 0;
2496 }
2497 if (r < 0)
2498 return log_error_errno(r, "crypt_init_by_name() for volume '%s' failed: %m", volume);
2499
2500 cryptsetup_enable_logging(cd);
2501
2502 r = crypt_deactivate(cd, volume);
2503 if (r < 0)
2504 return log_error_errno(r, "Failed to deactivate '%s': %m", volume);
2505
2506 } else
2507 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", verb);
2508
2509 return 0;
2510 }
2511
2512 DEFINE_MAIN_FUNCTION(run);