• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23624534405

26 Mar 2026 07:08PM UTC coverage: 72.113% (-0.2%) from 72.361%
23624534405

push

github

web-flow
Make imds networking unlocked by default (#41359)

316838 of 439364 relevant lines covered (72.11%)

1174324.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.86
/src/cryptsetup/cryptsetup.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <getopt.h>
4
#include <sys/mman.h>
5
#include <sys/stat.h>
6
#include <unistd.h>
7

8
#include "sd-device.h"
9
#include "sd-event.h"
10
#include "sd-json.h"
11
#include "sd-messages.h"
12

13
#include "alloc-util.h"
14
#include "argv-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 "efi-api.h"
23
#include "efi-loader.h"
24
#include "efivars.h"
25
#include "env-util.h"
26
#include "errno-util.h"
27
#include "escape.h"
28
#include "extract-word.h"
29
#include "fileio.h"
30
#include "fs-util.h"
31
#include "hexdecoct.h"
32
#include "json-util.h"
33
#include "libfido2-util.h"
34
#include "libmount-util.h"
35
#include "log.h"
36
#include "main-func.h"
37
#include "memory-util.h"
38
#include "nulstr-util.h"
39
#include "parse-util.h"
40
#include "path-util.h"
41
#include "pkcs11-util.h"
42
#include "pretty-print.h"
43
#include "process-util.h"
44
#include "random-util.h"
45
#include "string-table.h"
46
#include "string-util.h"
47
#include "strv.h"
48
#include "time-util.h"
49
#include "tpm2-pcr.h"
50
#include "tpm2-util.h"
51
#include "verbs.h"
52

53
/* internal helper */
54
#define ANY_LUKS "LUKS"
55
/* as in src/cryptsetup.h */
56
#define CRYPT_SECTOR_SIZE 512U
57
#define CRYPT_MAX_SECTOR_SIZE 4096U
58

59
typedef enum PassphraseType {
60
        PASSPHRASE_NONE,
61
        PASSPHRASE_REGULAR = 1 << 0,
62
        PASSPHRASE_RECOVERY_KEY = 1 << 1,
63
        PASSPHRASE_BOTH = PASSPHRASE_REGULAR|PASSPHRASE_RECOVERY_KEY,
64
        _PASSPHRASE_TYPE_MAX,
65
        _PASSPHRASE_TYPE_INVALID = -1,
66
} PassphraseType;
67

68
typedef enum TokenType {
69
        TOKEN_TPM2,
70
        TOKEN_FIDO2,
71
        TOKEN_PKCS11,
72
        _TOKEN_TYPE_MAX,
73
        _TOKEN_TYPE_INVALID = -EINVAL,
74
} TokenType;
75

76
static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT, CRYPT_BITLK or CRYPT_PLAIN */
77
static char *arg_cipher = NULL;
78
static unsigned arg_key_size = 0;
79
static unsigned arg_sector_size = CRYPT_SECTOR_SIZE;
80
static int arg_key_slot = CRYPT_ANY_SLOT;
81
static unsigned arg_keyfile_size = 0;
82
static uint64_t arg_keyfile_offset = 0;
83
static bool arg_keyfile_erase = false;
84
static bool arg_try_empty_password = false;
85
static char *arg_hash = NULL;
86
static char *arg_header = NULL;
87
static unsigned arg_tries = 3;
88
static bool arg_readonly = false;
89
static bool arg_verify = false;
90
static bool arg_password_cache_set = false; /* Not the actual argument value, just an indicator that some value is set */
91
static AskPasswordFlags arg_ask_password_flags = ASK_PASSWORD_ACCEPT_CACHED | ASK_PASSWORD_PUSH_CACHE;
92
static bool arg_discards = false;
93
static bool arg_same_cpu_crypt = false;
94
static bool arg_submit_from_crypt_cpus = false;
95
static bool arg_no_read_workqueue = false;
96
static bool arg_no_write_workqueue = false;
97
static bool arg_tcrypt_hidden = false;
98
static bool arg_tcrypt_system = false;
99
static bool arg_tcrypt_veracrypt = false;
100
static uint32_t arg_tcrypt_veracrypt_pim = 0;
101
static char **arg_tcrypt_keyfiles = NULL;
102
static uint64_t arg_offset = 0;
103
static uint64_t arg_skip = 0;
104
static usec_t arg_timeout = USEC_INFINITY;
105
static char *arg_pkcs11_uri = NULL;
106
static bool arg_pkcs11_uri_auto = false;
107
static char *arg_fido2_device = NULL;
108
static bool arg_fido2_device_auto = false;
109
static void *arg_fido2_cid = NULL;
110
static size_t arg_fido2_cid_size = 0;
111
static char *arg_fido2_rp_id = NULL;
112
/* For now and for compatibility, if the user explicitly configured FIDO2 support and we do
113
 * not read FIDO2 metadata off the LUKS2 header, default to the systemd 248 logic, where we
114
 * use PIN + UP when needed, and do not configure UV at all. */
115
static Fido2EnrollFlags arg_fido2_manual_flags = FIDO2ENROLL_PIN_IF_NEEDED | FIDO2ENROLL_UP_IF_NEEDED | FIDO2ENROLL_UV_OMIT;
116
static char *arg_tpm2_device = NULL; /* These and the following fields are about locking an encrypted volume to the local TPM */
117
static bool arg_tpm2_device_auto = false;
118
static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
119
static char *arg_tpm2_signature = NULL;
120
static bool arg_tpm2_pin = false;
121
static char *arg_tpm2_pcrlock = NULL;
122
static usec_t arg_token_timeout_usec = 30*USEC_PER_SEC;
123
static unsigned arg_tpm2_measure_pcr = UINT_MAX; /* This and the following field is about measuring the unlocked volume key to the local TPM */
124
static char **arg_tpm2_measure_banks = NULL;
125
static char *arg_tpm2_measure_keyslot_nvpcr = NULL;
126
static char *arg_link_keyring = NULL;
127
static char *arg_link_key_type = NULL;
128
static char *arg_link_key_description = NULL;
129
static char *arg_fixate_volume_key = NULL;
130

131
STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep);
135✔
132
STATIC_DESTRUCTOR_REGISTER(arg_hash, freep);
135✔
133
STATIC_DESTRUCTOR_REGISTER(arg_header, freep);
135✔
134
STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep);
135✔
135
STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_uri, freep);
135✔
136
STATIC_DESTRUCTOR_REGISTER(arg_fido2_device, freep);
135✔
137
STATIC_DESTRUCTOR_REGISTER(arg_fido2_cid, freep);
135✔
138
STATIC_DESTRUCTOR_REGISTER(arg_fido2_rp_id, freep);
135✔
139
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
135✔
140
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_signature, freep);
135✔
141
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_measure_banks, strv_freep);
135✔
142
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_measure_keyslot_nvpcr, freep);
135✔
143
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_pcrlock, freep);
135✔
144
STATIC_DESTRUCTOR_REGISTER(arg_link_keyring, freep);
135✔
145
STATIC_DESTRUCTOR_REGISTER(arg_link_key_type, freep);
135✔
146
STATIC_DESTRUCTOR_REGISTER(arg_link_key_description, freep);
135✔
147
STATIC_DESTRUCTOR_REGISTER(arg_fixate_volume_key, freep);
135✔
148

149
static const char* const passphrase_type_table[_PASSPHRASE_TYPE_MAX] = {
150
        [PASSPHRASE_REGULAR]      = "passphrase",
151
        [PASSPHRASE_RECOVERY_KEY] = "recovery key",
152
        [PASSPHRASE_BOTH]         = "passphrase or recovery key",
153
};
154

155
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(passphrase_type, PassphraseType);
×
156

157
static const char* const token_type_table[_TOKEN_TYPE_MAX] = {
158
        [TOKEN_TPM2]   = "tpm2",
159
        [TOKEN_FIDO2]  = "fido2",
160
        [TOKEN_PKCS11] = "pkcs11",
161
};
162

163
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(token_type, TokenType);
51✔
164

165
/* Options Debian's crypttab knows we don't:
166
    check=
167
    checkargs=
168
    noearly
169
    loud
170
    quiet
171
    keyscript=
172
    initramfs
173
*/
174

175
static int parse_one_option(const char *option) {
149✔
176
        const char *val;
149✔
177
        int r;
149✔
178

179
        assert(option);
149✔
180

181
        /* Handled outside of this tool */
182
        if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout"))
149✔
183
                return 0;
1✔
184

185
        if (startswith(option, "keyfile-timeout="))
148✔
186
                return 0;
187

188
        if ((val = startswith(option, "cipher="))) {
148✔
189
                r = free_and_strdup(&arg_cipher, val);
×
190
                if (r < 0)
×
191
                        return log_oom();
×
192

193
        } else if ((val = startswith(option, "size="))) {
148✔
194

195
                r = safe_atou(val, &arg_key_size);
×
196
                if (r < 0) {
×
197
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
198
                        return 0;
×
199
                }
200

201
                if (arg_key_size % 8) {
×
202
                        log_warning("size= not a multiple of 8, ignoring.");
×
203
                        return 0;
×
204
                }
205

206
                arg_key_size /= 8;
×
207

208
        } else if ((val = startswith(option, "sector-size="))) {
148✔
209

210
                r = safe_atou(val, &arg_sector_size);
×
211
                if (r < 0) {
×
212
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
213
                        return 0;
×
214
                }
215

216
                if (arg_sector_size % 2) {
×
217
                        log_warning("sector-size= not a multiple of 2, ignoring.");
×
218
                        return 0;
×
219
                }
220

221
                if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE)
×
222
                        log_warning("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE);
×
223

224
        } else if ((val = startswith(option, "key-slot=")) ||
148✔
225
                   (val = startswith(option, "keyslot="))) {
146✔
226

227
                arg_type = ANY_LUKS;
2✔
228
                r = safe_atoi(val, &arg_key_slot);
2✔
229
                if (r < 0)
2✔
230
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
231

232
        } else if ((val = startswith(option, "tcrypt-keyfile="))) {
146✔
233

234
                arg_type = CRYPT_TCRYPT;
×
235
                if (path_is_absolute(val)) {
×
236
                        if (strv_extend(&arg_tcrypt_keyfiles, val) < 0)
×
237
                                return log_oom();
×
238
                } else
239
                        log_warning("Key file path \"%s\" is not absolute, ignoring.", val);
×
240

241
        } else if ((val = startswith(option, "keyfile-size="))) {
146✔
242

243
                r = safe_atou(val, &arg_keyfile_size);
3✔
244
                if (r < 0)
3✔
245
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
246

247
        } else if ((val = startswith(option, "keyfile-offset="))) {
143✔
248

249
                r = safe_atou64(val, &arg_keyfile_offset);
5✔
250
                if (r < 0)
5✔
251
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
252

253
        } else if ((val = startswith(option, "keyfile-erase="))) {
138✔
254

255
                r = parse_boolean(val);
2✔
256
                if (r < 0) {
2✔
257
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
258
                        return 0;
×
259
                }
260

261
                arg_keyfile_erase = r;
2✔
262

263
        } else if (streq(option, "keyfile-erase"))
136✔
264
                arg_keyfile_erase = true;
×
265

266
        else if ((val = startswith(option, "hash="))) {
136✔
267
                r = free_and_strdup(&arg_hash, val);
×
268
                if (r < 0)
×
269
                        return log_oom();
×
270

271
        } else if ((val = startswith(option, "header="))) {
136✔
272
                if (!arg_type || !STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT))
9✔
273
                        arg_type = ANY_LUKS;
7✔
274

275
                if (!path_is_absolute(val))
9✔
276
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
277
                                               "Header path \"%s\" is not absolute, refusing.", val);
278

279
                if (arg_header)
9✔
280
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
281
                                               "Duplicate header= option, refusing.");
282

283
                arg_header = strdup(val);
9✔
284
                if (!arg_header)
9✔
285
                        return log_oom();
×
286

287
        } else if ((val = startswith(option, "tries="))) {
127✔
288

289
                r = safe_atou(val, &arg_tries);
×
290
                if (r < 0)
×
291
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
149✔
292

293
        } else if (STR_IN_SET(option, "readonly", "read-only"))
127✔
294
                arg_readonly = true;
×
295
        else if (streq(option, "verify"))
127✔
296
                arg_verify = true;
×
297
        else if ((val = startswith(option, "password-echo="))) {
127✔
298
                if (streq(val, "masked"))
×
299
                        arg_ask_password_flags &= ~(ASK_PASSWORD_ECHO|ASK_PASSWORD_SILENT);
×
300
                else {
301
                        r = parse_boolean(val);
×
302
                        if (r < 0) {
×
303
                                log_warning_errno(r, "Invalid password-echo= option \"%s\", ignoring.", val);
×
304
                                return 0;
×
305
                        }
306

307
                        SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_ECHO, r);
×
308
                        SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_SILENT, !r);
×
309
                }
310
        } else if ((val = startswith(option, "password-cache="))) {
127✔
311
                arg_password_cache_set = true;
×
312

313
                if (streq(val, "read-only")) {
×
314
                        arg_ask_password_flags |= ASK_PASSWORD_ACCEPT_CACHED;
×
315
                        arg_ask_password_flags &= ~ASK_PASSWORD_PUSH_CACHE;
×
316
                } else {
317
                        r = parse_boolean(val);
×
318
                        if (r < 0) {
×
319
                                log_warning_errno(r, "Invalid password-cache= option \"%s\", ignoring.", val);
×
320
                                return 0;
×
321
                        }
322

323
                        SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE, r);
×
324
                }
325
        } else if (STR_IN_SET(option, "allow-discards", "discard"))
127✔
326
                arg_discards = true;
×
327
        else if (streq(option, "same-cpu-crypt"))
127✔
328
                arg_same_cpu_crypt = true;
×
329
        else if (streq(option, "submit-from-crypt-cpus"))
127✔
330
                arg_submit_from_crypt_cpus = true;
×
331
        else if (streq(option, "no-read-workqueue"))
127✔
332
                arg_no_read_workqueue = true;
×
333
        else if (streq(option, "no-write-workqueue"))
127✔
334
                arg_no_write_workqueue = true;
×
335
        else if (streq(option, "luks"))
127✔
336
                arg_type = ANY_LUKS;
8✔
337
        else if (streq(option, "bitlk"))
119✔
338
                arg_type = CRYPT_BITLK;
×
339
        else if (streq(option, "tcrypt"))
119✔
340
                arg_type = CRYPT_TCRYPT;
×
341
        else if (STR_IN_SET(option, "tcrypt-hidden", "tcrypthidden")) {
119✔
342
                arg_type = CRYPT_TCRYPT;
×
343
                arg_tcrypt_hidden = true;
×
344
        } else if (streq(option, "tcrypt-system")) {
119✔
345
                arg_type = CRYPT_TCRYPT;
×
346
                arg_tcrypt_system = true;
×
347
        } else if (STR_IN_SET(option, "tcrypt-veracrypt", "veracrypt")) {
119✔
348
                arg_type = CRYPT_TCRYPT;
×
349
                arg_tcrypt_veracrypt = true;
×
350
        } else if ((val = startswith(option, "veracrypt-pim="))) {
119✔
351

352
                r = safe_atou32(val, &arg_tcrypt_veracrypt_pim);
×
353
                if (r < 0) {
×
354
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
355
                        return 0;
×
356
                }
357
        } else if (STR_IN_SET(option, "plain", "swap", "tmp") ||
238✔
358
                   startswith(option, "tmp="))
119✔
359
                arg_type = CRYPT_PLAIN;
×
360
        else if ((val = startswith(option, "timeout="))) {
119✔
361

362
                r = parse_sec_fix_0(val, &arg_timeout);
×
363
                if (r < 0)
×
364
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
365

366
        } else if ((val = startswith(option, "offset="))) {
119✔
367

368
                r = safe_atou64(val, &arg_offset);
×
369
                if (r < 0)
×
370
                        return log_error_errno(r, "Failed to parse %s: %m", option);
×
371

372
        } else if ((val = startswith(option, "skip="))) {
119✔
373

374
                r = safe_atou64(val, &arg_skip);
×
375
                if (r < 0)
×
376
                        return log_error_errno(r, "Failed to parse %s: %m", option);
×
377

378
        } else if ((val = startswith(option, "pkcs11-uri="))) {
119✔
379

380
                if (streq(val, "auto")) {
6✔
381
                        arg_pkcs11_uri = mfree(arg_pkcs11_uri);
6✔
382
                        arg_pkcs11_uri_auto = true;
6✔
383
                } else {
384
                        if (!pkcs11_uri_valid(val))
×
385
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "pkcs11-uri= parameter expects a PKCS#11 URI, refusing.");
×
386

387
                        r = free_and_strdup(&arg_pkcs11_uri, val);
×
388
                        if (r < 0)
×
389
                                return log_oom();
×
390

391
                        arg_pkcs11_uri_auto = false;
×
392
                }
393

394
        } else if ((val = startswith(option, "fido2-device="))) {
113✔
395

396
                if (streq(val, "auto")) {
×
397
                        arg_fido2_device = mfree(arg_fido2_device);
×
398
                        arg_fido2_device_auto = true;
×
399
                } else {
400
                        r = free_and_strdup(&arg_fido2_device, val);
×
401
                        if (r < 0)
×
402
                                return log_oom();
×
403

404
                        arg_fido2_device_auto = false;
×
405
                }
406

407
        } else if ((val = startswith(option, "fido2-cid="))) {
113✔
408

409
                if (streq(val, "auto"))
×
410
                        arg_fido2_cid = mfree(arg_fido2_cid);
×
411
                else {
412
                        _cleanup_free_ void *cid = NULL;
×
413
                        size_t cid_size;
×
414

415
                        r = unbase64mem(val, &cid, &cid_size);
×
416
                        if (r < 0)
×
417
                                return log_error_errno(r, "Failed to decode FIDO2 CID data: %m");
×
418

419
                        free(arg_fido2_cid);
×
420
                        arg_fido2_cid = TAKE_PTR(cid);
×
421
                        arg_fido2_cid_size = cid_size;
×
422
                }
423

424
                /* Turn on FIDO2 as side-effect, if not turned on yet. */
425
                if (!arg_fido2_device && !arg_fido2_device_auto)
×
426
                        arg_fido2_device_auto = true;
×
427

428
        } else if ((val = startswith(option, "fido2-rp="))) {
113✔
429

430
                r = free_and_strdup(&arg_fido2_rp_id, val);
×
431
                if (r < 0)
×
432
                        return log_oom();
×
433

434
        } else if ((val = startswith(option, "fido2-pin="))) {
113✔
435

436
                r = parse_boolean(val);
×
437
                if (r < 0) {
×
438
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
439
                        return 0;
×
440
                }
441

442
                arg_fido2_manual_flags &= ~FIDO2ENROLL_PIN_IF_NEEDED;
×
443
                SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_PIN, r);
×
444

445
        } else if ((val = startswith(option, "fido2-up="))) {
113✔
446

447
                r = parse_boolean(val);
×
448
                if (r < 0) {
×
449
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
450
                        return 0;
×
451
                }
452

453
                arg_fido2_manual_flags &= ~FIDO2ENROLL_UP_IF_NEEDED;
×
454
                SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_UP, r);
×
455

456
        } else if ((val = startswith(option, "fido2-uv="))) {
113✔
457

458
                r = parse_boolean(val);
×
459
                if (r < 0) {
×
460
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
461
                        return 0;
×
462
                }
463

464
                arg_fido2_manual_flags &= ~FIDO2ENROLL_UV_OMIT;
×
465
                SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_UV, r);
×
466

467
        } else if ((val = startswith(option, "tpm2-device="))) {
113✔
468

469
                if (streq(val, "auto")) {
39✔
470
                        arg_tpm2_device = mfree(arg_tpm2_device);
39✔
471
                        arg_tpm2_device_auto = true;
39✔
472
                } else {
473
                        r = free_and_strdup(&arg_tpm2_device, val);
×
474
                        if (r < 0)
×
475
                                return log_oom();
×
476

477
                        arg_tpm2_device_auto = false;
×
478
                }
479

480
        } else if ((val = startswith(option, "tpm2-pcrs="))) {
74✔
481

482
                r = tpm2_parse_pcr_argument_to_mask(val, &arg_tpm2_pcr_mask);
×
483
                if (r < 0)
×
484
                        return r;
485

486
        } else if ((val = startswith(option, "tpm2-signature="))) {
74✔
487

488
                if (!path_is_absolute(val))
1✔
489
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
490
                                               "TPM2 signature path \"%s\" is not absolute, refusing.", val);
491

492
                r = free_and_strdup(&arg_tpm2_signature, val);
1✔
493
                if (r < 0)
1✔
494
                        return log_oom();
×
495

496
        } else if ((val = startswith(option, "tpm2-pin="))) {
73✔
497

498
                r = parse_boolean(val);
×
499
                if (r < 0) {
×
500
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
501
                        return 0;
×
502
                }
503

504
                arg_tpm2_pin = r;
×
505

506
        } else if ((val = startswith(option, "tpm2-pcrlock="))) {
73✔
507

508
                if (!path_is_absolute(val))
5✔
509
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
510
                                               "TPM2 pcrlock policy path \"%s\" is not absolute, refusing.", val);
511

512
                r = free_and_strdup(&arg_tpm2_pcrlock, val);
5✔
513
                if (r < 0)
5✔
514
                        return log_oom();
×
515

516
        } else if ((val = startswith(option, "tpm2-measure-pcr="))) {
68✔
517
                unsigned pcr;
×
518

519
                r = safe_atou(val, &pcr);
×
520
                if (r < 0) {
×
521
                        r = parse_boolean(val);
×
522
                        if (r < 0) {
×
523
                                log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
524
                                return 0;
×
525
                        }
526

527
                        pcr = r ? TPM2_PCR_SYSTEM_IDENTITY : UINT_MAX;
×
528
                } else if (!TPM2_PCR_INDEX_VALID(pcr)) {
×
529
                        log_warning("Selected TPM index for measurement %u outside of allowed range 0…%u, ignoring.", pcr, TPM2_PCRS_MAX-1);
×
530
                        return 0;
×
531
                }
532

533
                arg_tpm2_measure_pcr = pcr;
×
534

535
        } else if ((val = startswith(option, "tpm2-measure-bank="))) {
68✔
536

537
#if HAVE_OPENSSL
538
                _cleanup_strv_free_ char **l = NULL;
×
539

540
                l = strv_split(val, ":");
×
541
                if (!l)
×
542
                        return log_oom();
×
543

544
                STRV_FOREACH(i, l) {
×
545
                        const EVP_MD *implementation;
×
546

547
                        implementation = EVP_get_digestbyname(*i);
×
548
                        if (!implementation)
×
549
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown bank '%s', refusing.", val);
×
550

551
                        if (strv_extend(&arg_tpm2_measure_banks, EVP_MD_name(implementation)) < 0)
×
552
                                return log_oom();
×
553
                }
554
#else
555
                log_error("Build lacks OpenSSL support, cannot measure to PCR banks, ignoring: %s", option);
556
#endif
557

558
        } else if ((val = startswith(option, "tpm2-measure-keyslot-nvpcr="))) {
68✔
559

560
                r = isempty(val) ? false : parse_boolean(val);
×
561
                if (r == 0) {
×
562
                        arg_tpm2_measure_keyslot_nvpcr = mfree(arg_tpm2_measure_keyslot_nvpcr);
×
563
                        return 0;
×
564
                }
565
                if (r > 0)
×
566
                        val = "cryptsetup";
567
                else if (!tpm2_nvpcr_name_is_valid(val)) {
×
568
                        log_warning("Invalid NvPCR name, ignoring: %s", option);
×
569
                        return 0;
×
570
                }
571

572
                if (free_and_strdup(&arg_tpm2_measure_keyslot_nvpcr, val) < 0)
×
573
                        return log_oom();
×
574

575
        } else if ((val = startswith(option, "try-empty-password="))) {
68✔
576

577
                r = parse_boolean(val);
1✔
578
                if (r < 0) {
1✔
579
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
580
                        return 0;
×
581
                }
582

583
                arg_try_empty_password = r;
1✔
584

585
        } else if (streq(option, "try-empty-password"))
67✔
586
                arg_try_empty_password = true;
1✔
587
        else if ((val = startswith(option, "headless="))) {
66✔
588

589
                r = parse_boolean(val);
55✔
590
                if (r < 0) {
55✔
591
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
592
                        return 0;
×
593
                }
594

595
                SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_HEADLESS, r);
55✔
596
        } else if (streq(option, "headless"))
11✔
597
                arg_ask_password_flags |= ASK_PASSWORD_HEADLESS;
7✔
598

599
        else if ((val = startswith(option, "token-timeout="))) {
4✔
600

601
                r = parse_sec_fix_0(val, &arg_token_timeout_usec);
×
602
                if (r < 0)
×
603
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
119✔
604

605
        } else if ((val = startswith(option, "link-volume-key="))) {
4✔
606
#if HAVE_CRYPT_SET_KEYRING_TO_LINK
607
                _cleanup_free_ char *keyring = NULL, *key_type = NULL, *key_description = NULL;
×
608
                const char *sep;
×
609

610
                /* Stick with cryptsetup --link-vk-to-keyring format
611
                 * <keyring_description>::%<key_type>:<key_description>,
612
                 * where %<key_type> is optional and defaults to 'user'.
613
                 */
614
                sep = strstr(val, "::");
×
615
                if (!sep)
×
616
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse link-volume-key= option value: %s", val);
×
617

618
                /* cryptsetup (cli) supports <keyring_description> passed in various formats:
619
                 * - well-known keyrings prefixed with '@' (@u user, @s session, etc)
620
                 * - text descriptions prefixed with "%:" or "%keyring:".
621
                 * - text description with no prefix.
622
                 * - numeric keyring id (ignored in current patch set). */
623
                keyring = strndup(val, sep - val);
×
624
                if (!keyring)
×
625
                        return log_oom();
×
626

627
                /* add type prefix if missing (crypt_set_keyring_to_link() expects it) */
628
                if (!IN_SET(*keyring, '@', '%'))
×
629
                        if (!strprepend(&keyring, "%:"))
×
630
                                return log_oom();
×
631

632
                sep += 2;
×
633

634
                /* %<key_type> is optional (and defaults to 'user') */
635
                if (*sep == '%') {
×
636
                        /* must be separated by colon */
637
                        const char *c = strchr(sep, ':');
×
638
                        if (!c)
×
639
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse link-volume-key= option value: %s", val);
×
640

641
                        key_type = strndup(sep + 1, c - sep - 1);
×
642
                        if (!key_type)
×
643
                                return log_oom();
×
644

645
                        sep = c + 1;
×
646
                }
647

648
                key_description = strdup(sep);
×
649
                if (!key_description)
×
650
                        return log_oom();
×
651

652
                free_and_replace(arg_link_keyring, keyring);
×
653
                free_and_replace(arg_link_key_type, key_type);
×
654
                free_and_replace(arg_link_key_description, key_description);
×
655
#else
656
                log_error("Build lacks libcryptsetup support for linking volume keys in user specified kernel keyrings upon device activation, ignoring: %s", option);
657
#endif
658
        } else if ((val = startswith(option, "fixate-volume-key="))) {
4✔
659
                r = free_and_strdup(&arg_fixate_volume_key, val);
4✔
660
                if (r < 0)
4✔
661
                        return log_oom();
×
662

663
        } else if (!streq(option, "x-initrd.attach"))
×
664
                log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
×
665

666
        return 0;
667
}
668

669
static int parse_crypt_config(const char *options) {
74✔
670
        assert(options);
74✔
671

672
        for (;;) {
372✔
673
                _cleanup_free_ char *word = NULL;
149✔
674
                int r;
223✔
675

676
                r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
223✔
677
                if (r < 0)
223✔
678
                        return log_error_errno(r, "Failed to parse options: %m");
×
679
                if (r == 0)
223✔
680
                        break;
681

682
                r = parse_one_option(word);
149✔
683
                if (r < 0)
149✔
684
                        return r;
685
        }
686

687
        /* sanity-check options */
688
        if (arg_type && !streq(arg_type, CRYPT_PLAIN)) {
74✔
689
                if (arg_offset != 0)
17✔
690
                      log_warning("offset= ignored with type %s", arg_type);
×
691
                if (arg_skip != 0)
17✔
692
                      log_warning("skip= ignored with type %s", arg_type);
×
693
        }
694

695
        if (arg_pkcs11_uri || arg_pkcs11_uri_auto) {
74✔
696
                /* If password-cache was not configured explicitly, default to no cache for PKCS#11 */
697
                if (!arg_password_cache_set)
6✔
698
                        arg_ask_password_flags &= ~(ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE);
6✔
699

700
                /* This prevents future backward-compatibility issues if we decide to allow caching for PKCS#11 */
701
                if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_ACCEPT_CACHED))
6✔
702
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
703
                                               "Password cache is not supported for PKCS#11 security tokens.");
704
        }
705

706
        return 0;
707
}
708

709
static char* disk_description(const char *path) {
10✔
710
        static const char name_fields[] =
10✔
711
                "DM_NAME\0"
712
                "ID_MODEL_FROM_DATABASE\0"
713
                "ID_MODEL\0";
714

715
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
10✔
716
        const char *name;
10✔
717
        struct stat st;
10✔
718

719
        assert(path);
10✔
720

721
        if (stat(path, &st) < 0)
10✔
722
                return NULL;
723

724
        if (!S_ISBLK(st.st_mode))
10✔
725
                return NULL;
726

727
        if (sd_device_new_from_stat_rdev(&device, &st) < 0)
×
728
                return NULL;
729

730
        if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) {
×
731
                _cleanup_free_ char *unescaped = NULL;
×
732
                ssize_t l;
×
733

734
                /* ID_PART_ENTRY_NAME uses \x style escaping, using libblkid's blkid_encode_string(). Let's
735
                 * reverse this here to make the string more human friendly in case people embed spaces or
736
                 * other weird stuff. */
737

738
                l = cunescape(name, UNESCAPE_RELAX, &unescaped);
×
739
                if (l < 0) {
×
740
                        log_debug_errno(l, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
×
741
                        return NULL;
×
742
                }
743

744
                if (!isempty(unescaped) && !string_has_cc(unescaped, NULL))
×
745
                        return TAKE_PTR(unescaped);
×
746
        }
747

748
        /* These need no unescaping. */
749
        NULSTR_FOREACH(i, name_fields)
×
750
                if (sd_device_get_property_value(device, i, &name) >= 0 &&
×
751
                    !isempty(name))
×
752
                        return strdup(name);
×
753

754
        return NULL;
755
}
756

757
static char* disk_mount_point(const char *label) {
10✔
758
        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
10✔
759
        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
×
760
        _cleanup_free_ char *device = NULL;
10✔
761
        int r;
10✔
762

763
        /* Yeah, we don't support native systemd unit files here for now */
764

765
        assert(label);
10✔
766

767
        device = strjoin("/dev/mapper/", label);
10✔
768
        if (!device)
10✔
769
                return NULL;
770

771
        r = libmount_parse_fstab(&table, &iter);
10✔
772
        if (r < 0)
10✔
773
                return NULL;
774

775
        for (;;) {
×
776
                struct libmnt_fs *fs;
10✔
777

778
                r = sym_mnt_table_next_fs(table, iter, &fs);
10✔
779
                if (r != 0)
10✔
780
                        return NULL;
10✔
781

782
                if (path_equal(sym_mnt_fs_get_source(fs), device)) {
×
783
                        const char *target = sym_mnt_fs_get_target(fs);
×
784
                        if (target)
×
785
                                return strdup(target);
×
786
                }
787
        }
788
}
789

790
static char *friendly_disk_name(const char *src, const char *vol) {
10✔
791
        _cleanup_free_ char *description = NULL, *mount_point = NULL;
10✔
792
        char *name_buffer = NULL;
10✔
793
        int r;
10✔
794

795
        assert(src);
10✔
796
        assert(vol);
10✔
797

798
        description = disk_description(src);
10✔
799
        mount_point = disk_mount_point(vol);
10✔
800

801
        /* If the description string is simply the volume name, then let's not show this twice */
802
        if (description && streq(vol, description))
10✔
803
                description = mfree(description);
×
804

805
        if (mount_point && description)
10✔
806
                r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point);
×
807
        else if (mount_point)
×
808
                r = asprintf(&name_buffer, "%s on %s", vol, mount_point);
×
809
        else if (description)
10✔
810
                r = asprintf(&name_buffer, "%s (%s)", description, vol);
×
811
        else
812
                return strdup(vol);
10✔
813
        if (r < 0)
×
814
                return NULL;
815

816
        return name_buffer;
×
817
}
818

819
static PassphraseType check_registered_passwords(struct crypt_device *cd) {
14✔
820
        _cleanup_free_ bool *slots = NULL;
14✔
821
        int slot_max;
14✔
822
        PassphraseType passphrase_type = PASSPHRASE_NONE;
14✔
823

824
        assert(cd);
14✔
825

826
        if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2)) {
14✔
827
                log_debug("%s: not a LUKS2 device, only passphrases are supported", crypt_get_device_name(cd));
×
828
                return PASSPHRASE_REGULAR;
×
829
        }
830

831
        /* Search all used slots */
832
        assert_se((slot_max = crypt_keyslot_max(CRYPT_LUKS2)) > 0);
14✔
833
        slots = new(bool, slot_max);
14✔
834
        if (!slots)
14✔
835
                return log_oom();
×
836

837
        for (int slot = 0; slot < slot_max; slot++)
462✔
838
                slots[slot] = IN_SET(crypt_keyslot_status(cd, slot), CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST);
448✔
839

840
        /* Iterate all LUKS2 tokens and keep track of all their slots */
841
        for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
462✔
842
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
448✔
843
                const char *type;
448✔
844
                sd_json_variant *w, *z;
448✔
845
                int tk;
448✔
846

847
                tk = cryptsetup_get_token_as_json(cd, token, NULL, &v);
448✔
848
                if (IN_SET(tk, -ENOENT, -EINVAL))
448✔
849
                        continue;
439✔
850
                if (tk < 0) {
9✔
851
                        log_warning_errno(tk, "Failed to read JSON token data, ignoring: %m");
×
852
                        continue;
×
853
                }
854

855
                w = sd_json_variant_by_key(v, "type");
9✔
856
                if (!w || !sd_json_variant_is_string(w)) {
9✔
857
                        log_warning("Token JSON data lacks type field, ignoring.");
×
858
                        continue;
×
859
                }
860

861
                type = sd_json_variant_string(w);
9✔
862
                if (STR_IN_SET(type, "systemd-recovery", "systemd-pkcs11", "systemd-fido2", "systemd-tpm2")) {
9✔
863

864
                        /* At least exists one recovery key */
865
                        if (streq(type, "systemd-recovery"))
9✔
866
                                passphrase_type |= PASSPHRASE_RECOVERY_KEY;
×
867

868
                        w = sd_json_variant_by_key(v, "keyslots");
9✔
869
                        if (!w || !sd_json_variant_is_array(w)) {
9✔
870
                                log_warning("Token JSON data lacks keyslots field, ignoring.");
×
871
                                continue;
×
872
                        }
873

874
                        JSON_VARIANT_ARRAY_FOREACH(z, w) {
18✔
875
                                unsigned u;
9✔
876
                                int at;
9✔
877

878
                                if (!sd_json_variant_is_string(z)) {
9✔
879
                                        log_warning("Token JSON data's keyslot field is not an array of strings, ignoring.");
×
880
                                        continue;
×
881
                                }
882

883
                                at = safe_atou(sd_json_variant_string(z), &u);
9✔
884
                                if (at < 0) {
9✔
885
                                        log_warning_errno(at, "Token JSON data's keyslot field is not an integer formatted as string, ignoring.");
×
886
                                        continue;
×
887
                                }
888

889
                                if (u >= (unsigned) slot_max) {
9✔
890
                                        log_warning_errno(at, "Token JSON data's keyslot field exceeds the maximum value allowed, ignoring.");
×
891
                                        continue;
×
892
                                }
893

894
                                slots[u] = false;
9✔
895
                        }
896
                }
897
        }
898

899
        /* Check if any of the slots is not referenced by systemd tokens */
900
        for (int slot = 0; slot < slot_max; slot++)
14✔
901
                if (slots[slot]) {
14✔
902
                        passphrase_type |= PASSPHRASE_REGULAR;
14✔
903
                        break;
14✔
904
                }
905

906
        /* All the slots are referenced by systemd tokens, so if a recovery key is not enrolled,
907
         * we will not be able to enter a passphrase. */
908
        return passphrase_type;
909
}
910

911
static int get_password(
14✔
912
                const char *vol,
913
                const char *src,
914
                usec_t until,
915
                bool ignore_cached,
916
                PassphraseType passphrase_type,
917
                char ***ret) {
918

919
        _cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL, *id = NULL;
14✔
920
        _cleanup_strv_free_erase_ char **passwords = NULL;
×
921
        AskPasswordFlags flags = arg_ask_password_flags;
14✔
922
        int r;
14✔
923

924
        assert(vol);
14✔
925
        assert(src);
14✔
926
        assert(ret);
14✔
927

928
        if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_HEADLESS))
14✔
929
                return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Password querying disabled via 'headless' option.");
14✔
930

931
        friendly = friendly_disk_name(src, vol);
×
932
        if (!friendly)
×
933
                return log_oom();
×
934

935
        if (asprintf(&text, "Please enter %s for disk %s:", passphrase_type_to_string(passphrase_type), friendly) < 0)
×
936
                return log_oom();
×
937

938
        disk_path = cescape(src);
×
939
        if (!disk_path)
×
940
                return log_oom();
×
941

942
        id = strjoin("cryptsetup:", disk_path);
×
943
        if (!id)
×
944
                return log_oom();
×
945

946
        AskPasswordRequest req = {
×
947
                .tty_fd = -EBADF,
948
                .message = text,
949
                .icon = "drive-harddisk",
950
                .id = id,
951
                .keyring = "cryptsetup",
952
                .credential = "cryptsetup.passphrase",
953
                .until = until,
954
                .hup_fd = -EBADF,
955
        };
956

957
        if (ignore_cached)
×
958
                flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
×
959

960
        r = ask_password_auto(&req, flags, &passwords);
×
961
        if (r < 0)
×
962
                return log_error_errno(r, "Failed to query password: %m");
×
963

964
        if (arg_verify) {
×
965
                _cleanup_strv_free_erase_ char **passwords2 = NULL;
×
966

967
                assert(strv_length(passwords) == 1);
×
968

969
                text = mfree(text);
×
970
                if (asprintf(&text, "Please enter %s for disk %s (verification):", passphrase_type_to_string(passphrase_type), friendly) < 0)
×
971
                        return log_oom();
×
972

973
                free(id);
×
974
                id = strjoin("cryptsetup-verification:", disk_path);
×
975
                if (!id)
×
976
                        return log_oom();
×
977

978
                req.message = text;
×
979
                req.id = id;
×
980

981
                r = ask_password_auto(&req, flags, &passwords2);
×
982
                if (r < 0)
×
983
                        return log_error_errno(r, "Failed to query verification password: %m");
×
984

985
                assert(strv_length(passwords2) == 1);
×
986

987
                if (!streq(passwords[0], passwords2[0]))
×
988
                        return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN),
×
989
                                                 "Passwords did not match, retrying.");
990
        }
991

992
        strv_uniq(passwords);
×
993

994
        STRV_FOREACH(p, passwords) {
×
995
                char *c;
×
996

997
                if (strlen(*p)+1 >= arg_key_size)
×
998
                        continue;
×
999

1000
                /* Pad password if necessary */
1001
                c = new(char, arg_key_size);
×
1002
                if (!c)
×
1003
                        return log_oom();
14✔
1004

1005
                strncpy(c, *p, arg_key_size);
×
1006
                erase_and_free(*p);
×
1007
                *p = TAKE_PTR(c);
×
1008
        }
1009

1010
        *ret = TAKE_PTR(passwords);
×
1011

1012
        return 0;
×
1013
}
1014

1015
static int measure_volume_key(
×
1016
                struct crypt_device *cd,
1017
                const char *name,
1018
                const void *volume_key,
1019
                size_t volume_key_size) {
1020

1021
        int r;
×
1022

1023
        assert(cd);
×
1024
        assert(name);
×
1025
        assert(volume_key);
×
1026
        assert(volume_key_size > 0);
×
1027

1028
        if (arg_tpm2_measure_pcr == UINT_MAX) {
×
1029
                log_debug("Not measuring volume key, deactivated.");
×
1030
                return 0;
×
1031
        }
1032

1033
        r = efi_measured_os(LOG_WARNING);
×
1034
        if (r < 0)
×
1035
                return r;
1036
        if (r == 0) {
×
1037
                log_debug("OS measurements not explicitly requested and kernel stub did not measure kernel image into the expected PCR, skipping userspace volume key measurement, too.");
×
1038
                return 0;
×
1039
        }
1040

1041
#if HAVE_TPM2
1042
        _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL;
×
1043
        r = tpm2_context_new_or_warn(arg_tpm2_device, &c);
×
1044
        if (r < 0)
×
1045
                return r;
1046

1047
        _cleanup_strv_free_ char **l = NULL;
×
1048
        if (strv_isempty(arg_tpm2_measure_banks)) {
×
1049
                r = tpm2_get_good_pcr_banks_strv(c, UINT32_C(1) << arg_tpm2_measure_pcr, &l);
×
1050
                if (r < 0)
×
1051
                        return log_error_errno(r, "Could not verify pcr banks: %m");
×
1052
        }
1053

1054
        _cleanup_free_ char *joined = strv_join(l ?: arg_tpm2_measure_banks, ", ");
×
1055
        if (!joined)
×
1056
                return log_oom();
×
1057

1058
        /* Note: we don't directly measure the volume key, it might be a security problem to send an
1059
         * unprotected direct hash of the secret volume key over the wire to the TPM. Hence let's instead
1060
         * send a HMAC signature instead. */
1061

1062
        _cleanup_free_ char *prefix = NULL;
×
1063

1064
        /* Note: what is extended to the SHA256 bank here must match the expected hash of 'fixate-volume-key='
1065
         * calculated by cryptsetup_get_volume_key_id(). */
1066
        r = cryptsetup_get_volume_key_prefix(cd, name, &prefix);
×
1067
        if (r)
×
1068
                return log_error_errno(r, "Could not verify pcr banks: %m");
×
1069

1070
        r = tpm2_pcr_extend_bytes(
×
1071
                        c,
1072
                        /* banks= */ l ?: arg_tpm2_measure_banks,
×
1073
                        /* pcr_index = */ arg_tpm2_measure_pcr,
1074
                        /* data = */ &IOVEC_MAKE_STRING(prefix),
×
1075
                        /* secret = */ &IOVEC_MAKE(volume_key, volume_key_size),
×
1076
                        /* event_type = */ TPM2_EVENT_VOLUME_KEY,
1077
                        /* description = */ prefix);
1078
        if (r < 0)
×
1079
                return log_error_errno(r, "Could not extend PCR: %m");
×
1080

1081
        log_struct(LOG_INFO,
×
1082
                   LOG_MESSAGE_ID(SD_MESSAGE_TPM_PCR_EXTEND_STR),
1083
                   LOG_MESSAGE("Successfully extended PCR index %u with '%s' and volume key (banks %s).", arg_tpm2_measure_pcr, prefix, joined),
1084
                   LOG_ITEM("MEASURING=%s", prefix),
1085
                   LOG_ITEM("PCR=%u", arg_tpm2_measure_pcr),
1086
                   LOG_ITEM("BANKS=%s", joined));
1087

1088
        return 0;
1089
#else
1090
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring volume key.");
1091
#endif
1092
}
1093

1094
static int measure_keyslot(
23✔
1095
                struct crypt_device *cd,
1096
                const char *name,
1097
                const char *mechanism,
1098
                int keyslot) {
1099

1100
#if HAVE_TPM2
1101
        int r;
23✔
1102
#endif
1103
        assert(cd);
23✔
1104
        assert(name);
23✔
1105

1106
        if (!arg_tpm2_measure_keyslot_nvpcr) {
23✔
1107
                log_debug("Not measuring unlock keyslot, deactivated.");
23✔
1108
                return 0;
23✔
1109
        }
1110

1111
#if HAVE_TPM2
1112
        r = efi_measured_os(LOG_WARNING);
×
1113
        if (r < 0)
×
1114
                return r;
1115
        if (r == 0) {
×
1116
                log_debug("OS measurements not explicitly requested and kernel stub did not measure kernel image into the expected PCR, skipping userspace key slot measurement, too.");
×
1117
                return 0;
×
1118
        }
1119

1120
        _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL;
×
1121
        r = tpm2_context_new_or_warn(arg_tpm2_device, &c);
×
1122
        if (r < 0)
×
1123
                return r;
1124

1125
        _cleanup_free_ char *escaped = NULL;
×
1126
        escaped = xescape(name, ":"); /* avoid ambiguity around ":" once we join things below */
×
1127
        if (!escaped)
×
1128
                return log_oom();
×
1129

1130
        _cleanup_free_ char *k = NULL;
×
1131
        if (keyslot >= 0 && asprintf(&k, "%i", keyslot) < 0)
×
1132
                return log_oom();
×
1133

1134
        _cleanup_free_ char *s = NULL;
×
1135
        s = strjoin("cryptsetup-keyslot:", escaped, ":", strempty(crypt_get_uuid(cd)), ":", strempty(mechanism), ":", strempty(k));
×
1136
        if (!s)
×
1137
                return log_oom();
×
1138

1139
        r = tpm2_nvpcr_extend_bytes(c, /* session= */ NULL, arg_tpm2_measure_keyslot_nvpcr, &IOVEC_MAKE_STRING(s), /* secret= */ NULL, TPM2_EVENT_KEYSLOT, s);
×
1140
        if (r == -ENETDOWN) {
×
1141
                /* NvPCR is not initialized yet. Do so now. */
1142
                _cleanup_(iovec_done_erase) struct iovec anchor_secret = {};
×
1143
                r = tpm2_nvpcr_acquire_anchor_secret(&anchor_secret, /* sync_secondary= */ false);
×
1144
                if (r < 0)
×
1145
                        return r;
1146

1147
                r = tpm2_nvpcr_initialize(c, /* session= */ NULL, arg_tpm2_measure_keyslot_nvpcr, &anchor_secret);
×
1148
                if (r < 0)
×
1149
                        return log_error_errno(r, "Failed to extend NvPCR index '%s' with anchor secret: %m", name);
×
1150

1151
                r = tpm2_nvpcr_extend_bytes(c, /* session= */ NULL, arg_tpm2_measure_keyslot_nvpcr, &IOVEC_MAKE_STRING(s), /* secret= */ NULL, TPM2_EVENT_KEYSLOT, s);
×
1152
        }
1153
        if (r < 0)
×
1154
                return log_error_errno(r, "Could not extend NvPCR: %m");
×
1155

1156
        log_struct(LOG_INFO,
×
1157
                   "MESSAGE_ID=" SD_MESSAGE_TPM_NVPCR_EXTEND_STR,
1158
                   LOG_MESSAGE("Successfully extended NvPCR index '%s' with '%s'.", arg_tpm2_measure_keyslot_nvpcr, s),
1159
                   "MEASURING=%s", s,
1160
                   "NVPCR=%s", arg_tpm2_measure_keyslot_nvpcr);
1161

1162
        return 0;
1163
#else
1164
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring keyslot.");
1165
#endif
1166
}
1167

1168
static int log_external_activation(int r, const char *volume) {
×
1169
        assert(volume);
×
1170

1171
        log_notice_errno(r, "Volume '%s' has been activated externally while we have been trying to activate it.", volume);
×
1172
        return 0;
×
1173
}
1174

1175
static int measured_crypt_activate_by_volume_key(
4✔
1176
                struct crypt_device *cd,
1177
                const char *name,
1178
                const char *mechanism,
1179
                int keyslot,
1180
                const void *volume_key,
1181
                size_t volume_key_size,
1182
                uint32_t flags) {
1183

1184
        int r;
4✔
1185

1186
        assert(cd);
4✔
1187
        assert(name);
4✔
1188

1189
        /* A wrapper around crypt_activate_by_volume_key() which also measures to a PCR if that's requested. */
1190

1191
        /* First, check if volume key digest matches the expectation. */
1192
        if (arg_fixate_volume_key) {
4✔
1193
                 _cleanup_free_ char *key_id = NULL;
4✔
1194

1195
                 r = cryptsetup_get_volume_key_id(
4✔
1196
                                 cd,
1197
                                 /* volume_name= */ name,
1198
                                 /* volume_key= */ volume_key,
1199
                                 /* volume_key_size= */ volume_key_size,
1200
                                 /* ret= */ &key_id);
1201
                 if (r < 0)
4✔
1202
                         return log_error_errno(r, "Failed to get volume key id.");
×
1203

1204
                 if (!streq(arg_fixate_volume_key, key_id))
4✔
1205
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2✔
1206
                                                "Volume key id: '%s' does not match the expectation: '%s'.",
1207
                                                key_id, arg_fixate_volume_key);
1208
        }
1209

1210
        r = crypt_activate_by_volume_key(cd, name, volume_key, volume_key_size, flags);
2✔
1211
        if (r == -EEXIST) /* volume is already active */
2✔
1212
                return log_external_activation(r, name);
×
1213
        if (r < 0)
2✔
1214
                return r;
1215

1216
        if (arg_tpm2_measure_pcr == UINT_MAX) {
2✔
1217
                log_debug("Not measuring volume key, deactivated.");
2✔
1218
                return 0;
2✔
1219
        }
1220

1221
        if (volume_key_size > 0)
×
1222
                (void) measure_volume_key(cd, name, volume_key, volume_key_size); /* OK if fails */
×
1223
        else
1224
                log_debug("Not measuring volume key, none specified.");
×
1225

1226
        (void) measure_keyslot(cd, name, mechanism, keyslot); /* ditto */
×
1227
        return r;
×
1228
}
1229

1230
static int measured_crypt_activate_by_passphrase(
30✔
1231
                struct crypt_device *cd,
1232
                const char *name,
1233
                const char *mechanism,
1234
                int keyslot,
1235
                const char *passphrase,
1236
                size_t passphrase_size,
1237
                uint32_t flags) {
1238

1239
        _cleanup_(erase_and_freep) void *vk = NULL;
30✔
1240
        size_t vks;
30✔
1241
        int r;
30✔
1242

1243
        assert(cd);
30✔
1244

1245
        /* A wrapper around crypt_activate_by_passphrase() which also measures to a PCR if that's
1246
         * requested. Note that we may need the volume key for the measurement and/or for the comparison, and
1247
         * crypt_activate_by_passphrase() doesn't give us access to this. Hence, we operate indirectly, and
1248
         * retrieve the volume key first, and then activate through that. */
1249

1250
        if (arg_tpm2_measure_pcr == UINT_MAX && !arg_fixate_volume_key)
30✔
1251
                goto shortcut;
26✔
1252

1253
        r = crypt_get_volume_key_size(cd);
4✔
1254
        if (r < 0)
4✔
1255
                return r;
1256
        if (r == 0) {
4✔
1257
                log_debug("Not measuring volume key, none defined.");
×
1258
                goto shortcut;
×
1259
        }
1260

1261
        vk = malloc(vks = r);
4✔
1262
        if (!vk)
4✔
1263
                return -ENOMEM;
1264

1265
        keyslot = crypt_volume_key_get(cd, keyslot, vk, &vks, passphrase, passphrase_size);
4✔
1266
        if (keyslot < 0)
4✔
1267
                return keyslot;
1268

1269
        return measured_crypt_activate_by_volume_key(cd, name, mechanism, keyslot, vk, vks, flags);
4✔
1270

1271
shortcut:
26✔
1272
        keyslot = crypt_activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags);
26✔
1273
        if (keyslot == -EEXIST) /* volume is already active */
26✔
1274
                return log_external_activation(keyslot, name);
×
1275
        if (keyslot < 0)
26✔
1276
                return keyslot;
1277

1278
        (void) measure_keyslot(cd, name, mechanism, keyslot);
23✔
1279
        return keyslot;
1280
}
1281

1282
static int attach_tcrypt(
×
1283
                struct crypt_device *cd,
1284
                const char *name,
1285
                TokenType token_type,
1286
                const char *key_file,
1287
                const struct iovec *key_data,
1288
                char **passwords,
1289
                uint32_t flags) {
1290

1291
        int r = 0;
×
1292
        _cleanup_(erase_and_freep) char *passphrase = NULL;
×
1293
        struct crypt_params_tcrypt params = {
×
1294
                .flags = CRYPT_TCRYPT_LEGACY_MODES,
1295
                .keyfiles = (const char **)arg_tcrypt_keyfiles,
1296
                .keyfiles_count = strv_length(arg_tcrypt_keyfiles)
×
1297
        };
1298

1299
        assert(cd);
×
1300
        assert(name);
×
1301
        assert(key_file || key_data || !strv_isempty(passwords));
×
1302

1303
        if (token_type >= 0)
×
1304
                /* Ask for a regular password */
1305
                return log_error_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1306
                                       "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11/fido2/tpm2 support.");
1307

1308
        if (arg_tcrypt_hidden)
×
1309
                params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
×
1310

1311
        if (arg_tcrypt_system)
×
1312
                params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER;
×
1313

1314
        if (arg_tcrypt_veracrypt)
×
1315
                params.flags |= CRYPT_TCRYPT_VERA_MODES;
×
1316

1317
        if (arg_tcrypt_veracrypt && arg_tcrypt_veracrypt_pim != 0)
×
1318
                params.veracrypt_pim = arg_tcrypt_veracrypt_pim;
×
1319

1320
        if (key_data) {
×
1321
                params.passphrase = key_data->iov_base;
×
1322
                params.passphrase_size = key_data->iov_len;
×
1323
                r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
1324
        } else if (key_file) {
×
1325
                r = read_one_line_file(key_file, &passphrase);
×
1326
                if (r < 0) {
×
1327
                        log_error_errno(r, "Failed to read password file '%s': %m", key_file);
×
1328
                        return -EAGAIN; /* log with the actual error, but return EAGAIN */
×
1329
                }
1330
                params.passphrase = passphrase;
×
1331
                params.passphrase_size = strlen(passphrase);
×
1332
                r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
1333
        } else {
1334
                r = -EINVAL;
1335
                STRV_FOREACH(p, passwords){
×
1336
                        params.passphrase = *p;
×
1337
                        params.passphrase_size = strlen(*p);
×
1338
                        r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
1339
                        if (r >= 0)
×
1340
                                break;
1341
                }
1342
        }
1343

1344
        if (r < 0) {
×
1345
                if (r == -EPERM) {
×
1346
                        if (key_data)
×
1347
                                log_error_errno(r, "Failed to activate using discovered key. (Key not correct?)");
×
1348
                        else if (key_file)
×
1349
                                log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file);
×
1350
                        else
1351
                                log_error_errno(r, "Failed to activate using supplied passwords.");
×
1352

1353
                        return r;
×
1354
                }
1355

1356
                return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd));
×
1357
        }
1358

1359
        r = measured_crypt_activate_by_volume_key(
×
1360
                        cd,
1361
                        name,
1362
                        /* mechanism= */ NULL,
1363
                        /* keyslot= */ -1,
1364
                        /* volume_key= */ NULL,
1365
                        /* volume_key_size= */ 0,
1366
                        flags);
1367
        if (r < 0)
×
1368
                return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd));
×
1369

1370
        return 0;
1371
}
1372

1373
static char *make_bindname(const char *volume, TokenType token_type) {
50✔
1374
        const char *token_type_name = token_type_to_string(token_type), *suffix;
50✔
1375
        char *bindname;
50✔
1376
        int r;
50✔
1377

1378
        switch (token_type) {
50✔
1379

1380
        case TOKEN_FIDO2:
1381
                suffix = "-salt";
1382
                break;
1383

1384
        default:
1385
                suffix = NULL;
50✔
1386
        }
1387

1388
        r = asprintf(&bindname,
50✔
1389
                     "@%" PRIx64"/cryptsetup%s%s%s/%s",
1390
                     random_u64(),
1391
                     token_type_name ? "-" : "",
1392
                     strempty(token_type_name),
1393
                     strempty(suffix),
1394
                     volume);
1395
        if (r < 0)
50✔
1396
                return NULL;
50✔
1397

1398
        return bindname;
50✔
1399
}
1400

1401
static int make_security_device_monitor(
×
1402
                sd_event **ret_event,
1403
                sd_device_monitor **ret_monitor) {
1404
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
1405
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
1406
        int r;
×
1407

1408
        assert(ret_event);
×
1409
        assert(ret_monitor);
×
1410

1411
        /* Waits for a device with "security-device" tag to show up in udev */
1412
        log_debug("Creating device monitor for tag 'security-device' with timeout %s",
×
1413
                  FORMAT_TIMESPAN(arg_token_timeout_usec, 1*USEC_PER_SEC));
1414

1415
        r = sd_event_default(&event);
×
1416
        if (r < 0)
×
1417
                return log_error_errno(r, "Failed to allocate event loop: %m");
×
1418

1419
        r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
×
1420
        if (r < 0)
×
1421
                return log_error_errno(r, "Failed to install timeout event source: %m");
×
1422

1423
        r = sd_device_monitor_new(&monitor);
×
1424
        if (r < 0)
×
1425
                return log_error_errno(r, "Failed to allocate device monitor: %m");
×
1426

1427
        (void) sd_device_monitor_set_description(monitor, "security-device");
×
1428

1429
        r = sd_device_monitor_filter_add_match_tag(monitor, "security-device");
×
1430
        if (r < 0)
×
1431
                return log_error_errno(r, "Failed to configure device monitor: %m");
×
1432

1433
        r = sd_device_monitor_attach_event(monitor, event);
×
1434
        if (r < 0)
×
1435
                return log_error_errno(r, "Failed to attach device monitor: %m");
×
1436

1437
        r = sd_device_monitor_start(monitor, NULL, NULL);
×
1438
        if (r < 0)
×
1439
                return log_error_errno(r, "Failed to start device monitor: %m");
×
1440

1441
        *ret_event = TAKE_PTR(event);
×
1442
        *ret_monitor = TAKE_PTR(monitor);
×
1443
        return 0;
×
1444
}
1445

1446
static int run_security_device_monitor(
×
1447
                sd_event *event,
1448
                sd_device_monitor *monitor) {
1449
        bool processed = false;
×
1450
        int r;
×
1451

1452
        assert(event);
×
1453
        assert(monitor);
×
1454

1455
        /* Runs the event loop for the device monitor until either something happens, or the timeout is
1456
         * hit. */
1457

1458
        for (;;) {
×
1459
                int x;
×
1460

1461
                r = sd_event_get_exit_code(event, &x);
×
1462
                if (r < 0) {
×
1463
                        if (r != -ENODATA)
×
1464
                                return log_error_errno(r, "Failed to query exit code from event loop: %m");
×
1465

1466
                        /* On ENODATA we aren't told to exit yet. */
1467
                } else {
1468
                        assert(x == -ETIMEDOUT);
×
1469
                        return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1470
                                                "Timed out waiting for security device, aborting security device based authentication attempt.");
1471
                }
1472

1473
                /* Wait for one event, and then eat all subsequent events until there are no further ones */
1474
                r = sd_event_run(event, processed ? 0 : UINT64_MAX);
×
1475
                if (r < 0)
×
1476
                        return log_error_errno(r, "Failed to run event loop: %m");
×
1477
                if (r == 0) /* no events queued anymore */
×
1478
                        return 0;
1479

1480
                processed = true;
×
1481
        }
1482
}
1483

1484
static bool use_token_plugins(void) {
60✔
1485

1486
#if HAVE_TPM2
1487
        /* Currently, there's no way for us to query the volume key when plugins are used. Hence don't use
1488
         * plugins, if measurement has been requested. */
1489
        if (arg_tpm2_measure_pcr != UINT_MAX)
60✔
1490
                return false;
1491
        if (arg_tpm2_measure_keyslot_nvpcr)
60✔
1492
                return false;
1493
        /* Volume key is also needed if the expected key id is set */
1494
        if (arg_fixate_volume_key)
60✔
1495
                return false;
1496
#endif
1497

1498
        /* Disable tokens if we're in FIDO2 mode with manual parameters. */
1499
        if (arg_fido2_cid)
60✔
1500
                return false;
1501

1502
#if HAVE_LIBCRYPTSETUP_PLUGINS
1503
        int r;
60✔
1504

1505
        /* Permit a way to disable libcryptsetup token module support, for debugging purposes. */
1506
        r = getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE");
60✔
1507
        if (r < 0 && r != -ENXIO)
60✔
1508
                log_debug_errno(r, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE env var: %m");
×
1509
        if (r == 0)
60✔
1510
                return false;
1511

1512
        return crypt_token_external_path();
58✔
1513
#else
1514
        return false;
1515
#endif
1516
}
1517

1518
#if HAVE_LIBCRYPTSETUP_PLUGINS
1519
static int acquire_pins_from_env_variable(char ***ret_pins) {
21✔
1520
        _cleanup_(erase_and_freep) char *envpin = NULL;
21✔
1521
        _cleanup_strv_free_erase_ char **pins = NULL;
21✔
1522
        int r;
21✔
1523

1524
        assert(ret_pins);
21✔
1525

1526
        r = getenv_steal_erase("PIN", &envpin);
21✔
1527
        if (r < 0)
21✔
1528
                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
×
1529
        if (r > 0) {
21✔
1530
                pins = strv_new(envpin);
16✔
1531
                if (!pins)
16✔
1532
                        return log_oom();
×
1533
        }
1534

1535
        *ret_pins = TAKE_PTR(pins);
21✔
1536

1537
        return 0;
21✔
1538
}
1539
#endif
1540

1541
static int crypt_activate_by_token_pin_ask_password(
58✔
1542
                struct crypt_device *cd,
1543
                const char *name,
1544
                const char *type,
1545
                usec_t until,
1546
                void *userdata,
1547
                uint32_t activation_flags,
1548
                const char *message,
1549
                const char *keyring,
1550
                const char *credential) {
1551

1552
#if HAVE_LIBCRYPTSETUP_PLUGINS
1553
        AskPasswordFlags flags = arg_ask_password_flags;
58✔
1554
        _cleanup_strv_free_erase_ char **pins = NULL;
58✔
1555
        int r;
58✔
1556

1557
        r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, /* pin= */ NULL, /* pin_size= */ 0, userdata, activation_flags);
58✔
1558
        if (r > 0) /* returns unlocked keyslot id on success */
58✔
1559
                return 0;
1560
        if (r == -EEXIST) /* volume is already active */
35✔
1561
                return log_external_activation(r, name);
×
1562
        if (r != -ENOANO) /* needs pin or pin is wrong */
35✔
1563
                return r;
1564

1565
        r = acquire_pins_from_env_variable(&pins);
21✔
1566
        if (r < 0)
21✔
1567
                return r;
1568

1569
        STRV_FOREACH(p, pins) {
25✔
1570
                r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), userdata, activation_flags);
16✔
1571
                if (r > 0) /* returns unlocked keyslot id on success */
16✔
1572
                        return 0;
1573
                if (r == -EEXIST) /* volume is already active */
5✔
1574
                        return log_external_activation(r, name);
×
1575
                if (r != -ENOANO) /* needs pin or pin is wrong */
5✔
1576
                        return r;
1577
        }
1578

1579
        if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_HEADLESS))
9✔
1580
                return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the '$PIN' environment variable.");
9✔
1581

1582
        for (;;) {
×
1583
                pins = strv_free_erase(pins);
×
1584

1585
                AskPasswordRequest req = {
×
1586
                        .tty_fd = -EBADF,
1587
                        .message = message,
1588
                        .icon = "drive-harddisk",
1589
                        .keyring = keyring,
1590
                        .credential = credential,
1591
                        .until = until,
1592
                        .hup_fd = -EBADF,
1593
                };
1594

1595
                r = ask_password_auto(&req, flags, &pins);
×
1596
                if (r < 0)
×
1597
                        return r;
×
1598

1599
                STRV_FOREACH(p, pins) {
×
1600
                        r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), userdata, activation_flags);
×
1601
                        if (r > 0) /* returns unlocked keyslot id on success */
×
1602
                                return 0;
1603
                        if (r == -EEXIST) /* volume is already active */
×
1604
                                return log_external_activation(r, name);
×
1605
                        if (r != -ENOANO) /* needs pin or pin is wrong */
×
1606
                                return r;
1607
                }
1608

1609
                flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
×
1610
        }
1611
        return r;
1612
#else
1613
        return -EOPNOTSUPP;
1614
#endif
1615
}
1616

1617
static int attach_luks2_by_fido2_via_plugin(
×
1618
                struct crypt_device *cd,
1619
                const char *name,
1620
                usec_t until,
1621
                void *userdata,
1622
                uint32_t activation_flags) {
1623

1624
        return crypt_activate_by_token_pin_ask_password(
×
1625
                        cd,
1626
                        name,
1627
                        "systemd-fido2",
1628
                        until,
1629
                        userdata,
1630
                        activation_flags,
1631
                        "Please enter security token PIN:",
1632
                        "fido2-pin",
1633
                        "cryptsetup.fido2-pin");
1634
}
1635

1636
static int attach_luks_or_plain_or_bitlk_by_fido2(
×
1637
                struct crypt_device *cd,
1638
                const char *name,
1639
                const char *key_file,
1640
                const struct iovec *key_data,
1641
                usec_t until,
1642
                uint32_t flags,
1643
                bool pass_volume_key) {
1644

1645
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
1646
        _cleanup_(erase_and_freep) void *decrypted_key = NULL;
×
1647
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
1648
        size_t decrypted_key_size;
×
1649
        _cleanup_free_ char *friendly = NULL;
×
1650
        int keyslot = arg_key_slot, r;
×
1651
        bool use_libcryptsetup_plugin = use_token_plugins();
×
1652

1653
        assert(cd);
×
1654
        assert(name);
×
1655
        assert(arg_fido2_device || arg_fido2_device_auto);
×
1656

1657
        if (arg_fido2_cid && !key_file && !iovec_is_set(key_data))
×
1658
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1659
                                        "FIDO2 mode with manual parameters selected, but no keyfile specified, refusing.");
1660

1661
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
×
1662
        if (!friendly)
×
1663
                return log_oom();
×
1664

1665
        for (;;) {
×
1666
                if (use_libcryptsetup_plugin && !arg_fido2_cid) {
×
1667
                        r = attach_luks2_by_fido2_via_plugin(cd, name, until, arg_fido2_device, flags);
×
1668
                        if (IN_SET(r, -ENOTUNIQ, -ENXIO, -ENOENT))
×
1669
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1670
                                                       "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1671

1672
                } else {
1673
                        if (arg_fido2_cid)
×
1674
                                r = acquire_fido2_key(
×
1675
                                                name,
1676
                                                friendly,
1677
                                                arg_fido2_device,
1678
                                                arg_fido2_rp_id,
1679
                                                arg_fido2_cid, arg_fido2_cid_size,
1680
                                                key_file, arg_keyfile_size, arg_keyfile_offset,
1681
                                                key_data,
1682
                                                until,
1683
                                                arg_fido2_manual_flags,
1684
                                                "cryptsetup.fido2-pin",
1685
                                                arg_ask_password_flags,
1686
                                                &decrypted_key,
1687
                                                &decrypted_key_size);
1688
                        else
1689
                                r = acquire_fido2_key_auto(
×
1690
                                                cd,
1691
                                                name,
1692
                                                friendly,
1693
                                                arg_fido2_device,
1694
                                                until,
1695
                                                "cryptsetup.fido2-pin",
1696
                                                arg_ask_password_flags,
1697
                                                &decrypted_key,
1698
                                                &decrypted_key_size);
1699
                        if (r >= 0)
×
1700
                                break;
1701
                }
1702

1703
                if (r != -EAGAIN) /* EAGAIN means: token not found */
×
1704
                        return r;
1705

1706
                if (!monitor) {
×
1707
                        /* We didn't find the token. In this case, watch for it via udev. Let's
1708
                         * create an event loop and monitor first. */
1709

1710
                        assert(!event);
×
1711

1712
                        r = make_security_device_monitor(&event, &monitor);
×
1713
                        if (r < 0)
×
1714
                                return r;
1715

1716
                        log_notice("Security token not present for unlocking volume %s, please plug it in.", friendly);
×
1717

1718
                        /* Let's immediately rescan in case the token appeared in the time we needed
1719
                         * to create and configure the monitor */
1720
                        continue;
×
1721
                }
1722

1723
                r = run_security_device_monitor(event, monitor);
×
1724
                if (r < 0)
×
1725
                        return r;
1726

1727
                log_debug("Got one or more potentially relevant udev events, rescanning FIDO2...");
×
1728
        }
1729

1730
        if (pass_volume_key)
×
1731
                r = measured_crypt_activate_by_volume_key(
×
1732
                                cd,
1733
                                name,
1734
                                "fido2",
1735
                                /* keyslot= */ -1,
1736
                                decrypted_key,
1737
                                decrypted_key_size,
1738
                                flags);
1739
        else {
1740
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
×
1741
                ssize_t base64_encoded_size;
×
1742

1743
                /* Before using this key as passphrase we base64 encode it, for compat with homed */
1744

1745
                base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
×
1746
                if (base64_encoded_size < 0)
×
1747
                        return log_oom();
×
1748

1749
                r = measured_crypt_activate_by_passphrase(
×
1750
                                cd,
1751
                                name,
1752
                                "fido2",
1753
                                keyslot,
1754
                                base64_encoded,
1755
                                base64_encoded_size,
1756
                                flags);
1757
        }
1758
        if (r == -EPERM) {
×
1759
                log_error_errno(r, "Failed to activate with FIDO2 decrypted key. (Key incorrect?)");
×
1760
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
1761
        }
1762
        if (r < 0)
×
1763
                return log_error_errno(r, "Failed to activate with FIDO2 acquired key: %m");
×
1764

1765
        return 0;
1766
}
1767

1768
static int attach_luks2_by_pkcs11_via_plugin(
×
1769
                struct crypt_device *cd,
1770
                const char *name,
1771
                const char *friendly_name,
1772
                usec_t until,
1773
                const char *askpw_credential,
1774
                uint32_t flags) {
1775

1776
#if HAVE_LIBCRYPTSETUP_PLUGINS
1777
        int r;
×
1778

1779
        if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2))
×
1780
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Automatic PKCS#11 metadata requires LUKS2 device.");
×
1781

1782
        systemd_pkcs11_plugin_params params = {
×
1783
                .friendly_name = friendly_name,
1784
                .until = until,
1785
                .askpw_credential = askpw_credential,
1786
                .askpw_flags = arg_ask_password_flags,
1787
        };
1788

1789
        r = crypt_activate_by_token_pin(cd, name, "systemd-pkcs11", CRYPT_ANY_TOKEN, NULL, 0, &params, flags);
×
1790
        if (r > 0) /* returns unlocked keyslot id on success */
×
1791
                r = 0;
1792
        if (r == -EEXIST) /* volume is already active */
×
1793
                r = log_external_activation(r, name);
×
1794

1795
        return r;
1796
#else
1797
        return -EOPNOTSUPP;
1798
#endif
1799
}
1800

1801
static int attach_luks_or_plain_or_bitlk_by_pkcs11(
×
1802
                struct crypt_device *cd,
1803
                const char *name,
1804
                const char *key_file,
1805
                const struct iovec *key_data,
1806
                usec_t until,
1807
                uint32_t flags,
1808
                bool pass_volume_key) {
1809

1810
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
1811
        _cleanup_free_ char *friendly = NULL, *discovered_uri = NULL;
×
1812
        size_t decrypted_key_size = 0, discovered_key_size = 0;
×
1813
        _cleanup_(erase_and_freep) void *decrypted_key = NULL;
×
1814
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
1815
        _cleanup_free_ void *discovered_key = NULL;
×
1816
        struct iovec discovered_key_data = {};
×
1817
        int keyslot = arg_key_slot, r;
×
1818
        const char *uri = NULL;
×
1819
        bool use_libcryptsetup_plugin = use_token_plugins();
×
1820

1821
        assert(cd);
×
1822
        assert(name);
×
1823
        assert(arg_pkcs11_uri || arg_pkcs11_uri_auto);
×
1824

1825
        if (arg_pkcs11_uri_auto) {
×
1826
                if (!use_libcryptsetup_plugin) {
×
1827
                        r = find_pkcs11_auto_data(cd, &discovered_uri, &discovered_key, &discovered_key_size, &keyslot);
×
1828
                        if (IN_SET(r, -ENOTUNIQ, -ENXIO))
×
1829
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1830
                                                       "Automatic PKCS#11 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1831
                        if (r < 0)
×
1832
                                return r;
1833

1834
                        uri = discovered_uri;
×
1835
                        discovered_key_data = IOVEC_MAKE(discovered_key, discovered_key_size);
×
1836
                        key_data = &discovered_key_data;
×
1837
                }
1838
        } else {
1839
                uri = arg_pkcs11_uri;
×
1840

1841
                if (!key_file && !iovec_is_set(key_data))
×
1842
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 mode selected but no key file specified, refusing.");
×
1843
        }
1844

1845
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
×
1846
        if (!friendly)
×
1847
                return log_oom();
×
1848

1849
        for (;;) {
×
1850
                if (use_libcryptsetup_plugin && arg_pkcs11_uri_auto)
×
1851
                        r = attach_luks2_by_pkcs11_via_plugin(
×
1852
                                        cd,
1853
                                        name,
1854
                                        friendly,
1855
                                        until,
1856
                                        "cryptsetup.pkcs11-pin",
1857
                                        flags);
1858
                else {
1859
                        r = decrypt_pkcs11_key(
×
1860
                                        name,
1861
                                        friendly,
1862
                                        uri,
1863
                                        key_file, arg_keyfile_size, arg_keyfile_offset,
1864
                                        key_data,
1865
                                        until,
1866
                                        arg_ask_password_flags,
1867
                                        &decrypted_key, &decrypted_key_size);
1868
                        if (r >= 0)
×
1869
                                break;
1870
                }
1871

1872
                if (r != -EAGAIN) /* EAGAIN means: token not found */
×
1873
                        return r;
1874

1875
                if (!monitor) {
×
1876
                        /* We didn't find the token. In this case, watch for it via udev. Let's
1877
                         * create an event loop and monitor first. */
1878

1879
                        assert(!event);
×
1880

1881
                        r = make_security_device_monitor(&event, &monitor);
×
1882
                        if (r < 0)
×
1883
                                return r;
1884

1885
                        log_notice("Security token%s%s not present for unlocking volume %s, please plug it in.",
×
1886
                                   uri ? " " : "", strempty(uri), friendly);
1887

1888
                        /* Let's immediately rescan in case the token appeared in the time we needed
1889
                         * to create and configure the monitor */
1890
                        continue;
×
1891
                }
1892

1893
                r = run_security_device_monitor(event, monitor);
×
1894
                if (r < 0)
×
1895
                        return r;
1896

1897
                log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11...");
×
1898
        }
1899
        assert(decrypted_key);
×
1900

1901
        if (pass_volume_key)
×
1902
                r = measured_crypt_activate_by_volume_key(
×
1903
                                cd,
1904
                                name,
1905
                                "pkcs11",
1906
                                /* keyslot= */ -1,
1907
                                decrypted_key,
1908
                                decrypted_key_size,
1909
                                flags);
1910
        else {
1911
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
×
1912
                ssize_t base64_encoded_size;
×
1913

1914
                /* Before using this key as passphrase we base64 encode it. Why? For compatibility
1915
                 * with homed's PKCS#11 hookup: there we want to use the key we acquired through
1916
                 * PKCS#11 for other authentication/decryption mechanisms too, and some of them do
1917
                 * not take arbitrary binary blobs, but require NUL-terminated strings — most
1918
                 * importantly UNIX password hashes. Hence, for compatibility we want to use a string
1919
                 * without embedded NUL here too, and that's easiest to generate from a binary blob
1920
                 * via base64 encoding. */
1921

1922
                base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
×
1923
                if (base64_encoded_size < 0)
×
1924
                        return log_oom();
×
1925

1926
                r = measured_crypt_activate_by_passphrase(
×
1927
                                cd,
1928
                                name,
1929
                                "pkcs11",
1930
                                keyslot,
1931
                                base64_encoded,
1932
                                base64_encoded_size,
1933
                                flags);
1934
        }
1935
        if (r == -EPERM) {
×
1936
                log_error_errno(r, "Failed to activate with PKCS#11 decrypted key. (Key incorrect?)");
×
1937
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
1938
        }
1939
        if (r < 0)
×
1940
                return log_error_errno(r, "Failed to activate with PKCS#11 acquired key: %m");
×
1941

1942
        return 0;
1943
}
1944

1945
static int make_tpm2_device_monitor(
×
1946
                sd_event **ret_event,
1947
                sd_device_monitor **ret_monitor) {
1948

1949
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
1950
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
1951
        int r;
×
1952

1953
        assert(ret_event);
×
1954
        assert(ret_monitor);
×
1955

1956
        r = sd_event_default(&event);
×
1957
        if (r < 0)
×
1958
                return log_error_errno(r, "Failed to allocate event loop: %m");
×
1959

1960
        r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
×
1961
        if (r < 0)
×
1962
                return log_error_errno(r, "Failed to install timeout event source: %m");
×
1963

1964
        r = sd_device_monitor_new(&monitor);
×
1965
        if (r < 0)
×
1966
                return log_error_errno(r, "Failed to allocate device monitor: %m");
×
1967

1968
        (void) sd_device_monitor_set_description(monitor, "tpmrm");
×
1969

1970
        r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "tpmrm", NULL);
×
1971
        if (r < 0)
×
1972
                return log_error_errno(r, "Failed to configure device monitor: %m");
×
1973

1974
        r = sd_device_monitor_attach_event(monitor, event);
×
1975
        if (r < 0)
×
1976
                return log_error_errno(r, "Failed to attach device monitor: %m");
×
1977

1978
        r = sd_device_monitor_start(monitor, NULL, NULL);
×
1979
        if (r < 0)
×
1980
                return log_error_errno(r, "Failed to start device monitor: %m");
×
1981

1982
        *ret_event = TAKE_PTR(event);
×
1983
        *ret_monitor = TAKE_PTR(monitor);
×
1984
        return 0;
×
1985
}
1986

1987
static int attach_luks2_by_tpm2_via_plugin(
10✔
1988
                struct crypt_device *cd,
1989
                const char *name,
1990
                usec_t until,
1991
                uint32_t flags) {
1992

1993
#if HAVE_LIBCRYPTSETUP_PLUGINS
1994
        systemd_tpm2_plugin_params params = {
10✔
1995
                .search_pcr_mask = arg_tpm2_pcr_mask,
1996
                .device = arg_tpm2_device,
1997
                .signature_path = arg_tpm2_signature,
1998
                .pcrlock_path = arg_tpm2_pcrlock,
1999
        };
2000

2001
        if (!use_token_plugins())
10✔
2002
                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1✔
2003
                                       "libcryptsetup has external plugins support disabled.");
2004

2005
        return crypt_activate_by_token_pin_ask_password(
9✔
2006
                        cd,
2007
                        name,
2008
                        "systemd-tpm2",
2009
                        until,
2010
                        &params,
2011
                        flags,
2012
                        "Please enter TPM2 PIN:",
2013
                        "tpm2-pin",
2014
                        "cryptsetup.tpm2-pin");
2015
#else
2016
        return -EOPNOTSUPP;
2017
#endif
2018
}
2019

2020
static int attach_luks_or_plain_or_bitlk_by_tpm2(
10✔
2021
                struct crypt_device *cd,
2022
                const char *name,
2023
                const char *key_file,
2024
                const struct iovec *key_data,
2025
                usec_t until,
2026
                uint32_t flags,
2027
                bool pass_volume_key) {
2028

2029
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
10✔
2030
        _cleanup_(iovec_done_erase) struct iovec decrypted_key = {};
×
2031
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
10✔
2032
        _cleanup_free_ char *friendly = NULL;
10✔
2033
        int keyslot = arg_key_slot, r;
10✔
2034

2035
        assert(cd);
10✔
2036
        assert(name);
10✔
2037
        assert(arg_tpm2_device || arg_tpm2_device_auto);
10✔
2038

2039
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
10✔
2040
        if (!friendly)
10✔
2041
                return log_oom();
×
2042

2043
        for (;;) {
10✔
2044
                if (key_file || iovec_is_set(key_data)) {
10✔
2045
                        /* If key data is specified, use that */
2046

2047
                        r = acquire_tpm2_key(
×
2048
                                        name,
2049
                                        arg_tpm2_device,
2050
                                        arg_tpm2_pcr_mask == UINT32_MAX ? TPM2_PCR_MASK_DEFAULT_LEGACY : arg_tpm2_pcr_mask,
×
2051
                                        UINT16_MAX,
2052
                                        /* pubkey= */ NULL,
2053
                                        /* pubkey_pcr_mask= */ 0,
2054
                                        /* signature_path= */ NULL,
2055
                                        /* pcrlock_path= */ NULL,
2056
                                        /* primary_alg= */ 0,
2057
                                        key_file, arg_keyfile_size, arg_keyfile_offset,
2058
                                        key_data, /* n_blobs= */ 1,
2059
                                        /* policy_hash= */ NULL, /* we don't know the policy hash */
2060
                                        /* n_policy_hash= */ 0,
2061
                                        /* salt= */ NULL,
2062
                                        /* srk= */ NULL,
2063
                                        /* pcrlock_nv= */ NULL,
2064
                                        arg_tpm2_pin ? TPM2_FLAGS_USE_PIN : 0,
2065
                                        until,
2066
                                        "cryptsetup.tpm2-pin",
2067
                                        arg_ask_password_flags,
2068
                                        &decrypted_key);
2069
                        if (r >= 0)
×
2070
                                break;
2071
                        if (IN_SET(r, -EACCES, -ENOLCK))
×
2072
                                return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
×
2073
                        if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */
×
2074
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking.");
×
2075
                        /* EAGAIN means: no tpm2 chip found */
2076
                        if (r != -EAGAIN) {
×
2077
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
×
2078
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
×
2079
                        }
2080
                } else {
2081
                        r = attach_luks2_by_tpm2_via_plugin(cd, name, until, flags);
10✔
2082
                        if (r >= 0)
10✔
2083
                                return 0;
2084
                        /* EAGAIN     means: no tpm2 chip found
2085
                         * EOPNOTSUPP means: no libcryptsetup plugins support */
2086
                        if (r == -ENXIO)
10✔
2087
                                return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
×
2088
                                                        "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking.");
2089
                        if (r == -ENOENT)
10✔
2090
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
3✔
2091
                                                       "No TPM2 metadata enrolled in LUKS2 header or TPM2 support not available, falling back to traditional unlocking.");
2092
                        if (!IN_SET(r, -EOPNOTSUPP, -EAGAIN)) {
7✔
2093
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
6✔
2094
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
6✔
2095
                        }
2096
                }
2097

2098
                if (r == -EOPNOTSUPP) { /* Plugin not available, let's process TPM2 stuff right here instead */
1✔
2099
                        bool found_some = false;
1✔
2100
                        int token = 0; /* first token to look at */
1✔
2101

2102
                        /* If no key data is specified, look for it in the header. In order to support
2103
                         * software upgrades we'll iterate through all suitable tokens, maybe one of them
2104
                         * works. */
2105

2106
                        for (;;) {
1✔
2107
                                _cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {};
×
2108
                                struct iovec *blobs = NULL, *policy_hash = NULL;
1✔
2109
                                uint32_t hash_pcr_mask, pubkey_pcr_mask;
1✔
2110
                                size_t n_blobs = 0, n_policy_hash = 0;
1✔
2111
                                uint16_t pcr_bank, primary_alg;
1✔
2112
                                TPM2Flags tpm2_flags;
1✔
2113

2114
                                CLEANUP_ARRAY(blobs, n_blobs, iovec_array_free);
1✔
2115
                                CLEANUP_ARRAY(policy_hash, n_policy_hash, iovec_array_free);
1✔
2116

2117
                                r = find_tpm2_auto_data(
1✔
2118
                                                cd,
2119
                                                arg_tpm2_pcr_mask, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */
2120
                                                token, /* search for the token with this index, or any later index than this */
2121
                                                &hash_pcr_mask,
2122
                                                &pcr_bank,
2123
                                                &pubkey,
2124
                                                &pubkey_pcr_mask,
2125
                                                &primary_alg,
2126
                                                &blobs,
2127
                                                &n_blobs,
2128
                                                &policy_hash,
2129
                                                &n_policy_hash,
2130
                                                &salt,
2131
                                                &srk,
2132
                                                &pcrlock_nv,
2133
                                                &tpm2_flags,
2134
                                                &keyslot,
2135
                                                &token);
2136
                                if (r == -ENXIO)
1✔
2137
                                        /* No further TPM2 tokens found in the LUKS2 header. */
2138
                                        return log_full_errno(found_some ? LOG_NOTICE : LOG_DEBUG,
×
2139
                                                              SYNTHETIC_ERRNO(EAGAIN),
2140
                                                              found_some
2141
                                                              ? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking."
2142
                                                              : "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking.");
2143
                                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
1✔
2144
                                        /* TPM2 support not compiled in? */
2145
                                        return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
2146
                                                               "TPM2 support not available, falling back to traditional unlocking.");
2147
                                if (r < 0)
1✔
2148
                                        return r;
2149

2150
                                found_some = true;
1✔
2151

2152
                                r = acquire_tpm2_key(
1✔
2153
                                                name,
2154
                                                arg_tpm2_device,
2155
                                                hash_pcr_mask,
2156
                                                pcr_bank,
2157
                                                &pubkey,
2158
                                                pubkey_pcr_mask,
2159
                                                arg_tpm2_signature,
2160
                                                arg_tpm2_pcrlock,
2161
                                                primary_alg,
2162
                                                /* key_file= */ NULL, /* key_file_size= */ 0, /* key_file_offset= */ 0, /* no key file */
2163
                                                blobs,
2164
                                                n_blobs,
2165
                                                policy_hash,
2166
                                                n_policy_hash,
2167
                                                &salt,
2168
                                                &srk,
2169
                                                &pcrlock_nv,
2170
                                                tpm2_flags,
2171
                                                until,
2172
                                                "cryptsetup.tpm2-pin",
2173
                                                arg_ask_password_flags,
2174
                                                &decrypted_key);
2175
                                if (IN_SET(r, -EACCES, -ENOLCK))
1✔
2176
                                        return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
×
2177
                                if (r != -EPERM)
1✔
2178
                                        break;
2179

2180
                                token++; /* try a different token next time */
×
2181
                        }
2182

2183
                        if (r >= 0)
1✔
2184
                                break;
2185
                        /* EAGAIN means: no tpm2 chip found */
2186
                        if (r != -EAGAIN) {
×
2187
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
×
2188
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
×
2189
                        }
2190
                }
2191

2192
                if (!monitor) {
×
2193
                        /* We didn't find the TPM2 device. In this case, watch for it via udev. Let's create
2194
                         * an event loop and monitor first. */
2195

2196
                        assert(!event);
×
2197

2198
                        if (is_efi_boot() && !efi_has_tpm2())
×
2199
                                return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
×
2200
                                                        "No TPM2 hardware discovered and EFI firmware does not see it either, falling back to traditional unlocking.");
2201

2202
                        r = make_tpm2_device_monitor(&event, &monitor);
×
2203
                        if (r < 0)
×
2204
                                return r;
2205

2206
                        log_info("TPM2 device not present for unlocking %s, waiting for it to become available.", friendly);
×
2207

2208
                        /* Let's immediately rescan in case the device appeared in the time we needed
2209
                         * to create and configure the monitor */
2210
                        continue;
×
2211
                }
2212

2213
                r = run_security_device_monitor(event, monitor);
×
2214
                if (r < 0)
×
2215
                        return r;
2216

2217
                log_debug("Got one or more potentially relevant udev events, rescanning for TPM2...");
×
2218
        }
2219

2220
        if (pass_volume_key)
1✔
2221
                r = measured_crypt_activate_by_volume_key(
×
2222
                                cd,
2223
                                name,
2224
                                "tpm2",
2225
                                /* keyslot= */ -1,
2226
                                decrypted_key.iov_base,
×
2227
                                decrypted_key.iov_len,
2228
                                flags);
2229
        else {
2230
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1✔
2231
                ssize_t base64_encoded_size;
1✔
2232

2233
                /* Before using this key as passphrase we base64 encode it, for compat with homed */
2234

2235
                base64_encoded_size = base64mem(decrypted_key.iov_base, decrypted_key.iov_len, &base64_encoded);
1✔
2236
                if (base64_encoded_size < 0)
1✔
2237
                        return log_oom();
×
2238

2239
                r = measured_crypt_activate_by_passphrase(
1✔
2240
                                cd,
2241
                                name,
2242
                                "tpm2",
2243
                                keyslot,
2244
                                base64_encoded,
2245
                                base64_encoded_size,
2246
                                flags);
2247
        }
2248
        if (r == -EPERM) {
1✔
2249
                log_error_errno(r, "Failed to activate with TPM2 decrypted key. (Key incorrect?)");
×
2250
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
2251
        }
2252
        if (r < 0)
1✔
2253
                return log_error_errno(r, "Failed to activate with TPM2 acquired key: %m");
×
2254

2255
        return 0;
2256
}
2257

2258
static int attach_luks_or_plain_or_bitlk_by_key_data(
3✔
2259
                struct crypt_device *cd,
2260
                const char *name,
2261
                const struct iovec *key_data,
2262
                uint32_t flags,
2263
                bool pass_volume_key) {
2264

2265
        int r;
3✔
2266

2267
        assert(cd);
3✔
2268
        assert(name);
3✔
2269
        assert(key_data);
3✔
2270

2271
        if (pass_volume_key)
3✔
2272
                r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, key_data->iov_base, key_data->iov_len, flags);
×
2273
        else
2274
                r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, key_data->iov_base, key_data->iov_len, flags);
3✔
2275
        if (r == -EPERM) {
3✔
2276
                log_error_errno(r, "Failed to activate. (Key incorrect?)");
×
2277
                return -EAGAIN; /* Log actual error, but return EAGAIN */
×
2278
        }
2279
        if (r < 0)
3✔
2280
                return log_error_errno(r, "Failed to activate: %m");
×
2281

2282
        return 0;
2283
}
2284

2285
static int attach_luks_or_plain_or_bitlk_by_key_file(
26✔
2286
                struct crypt_device *cd,
2287
                const char *name,
2288
                const char *key_file,
2289
                uint32_t flags,
2290
                bool pass_volume_key) {
2291

2292
        _cleanup_(erase_and_freep) char *kfdata = NULL;
26✔
2293
        _cleanup_free_ char *bindname = NULL;
26✔
2294
        size_t kfsize;
26✔
2295
        int r;
26✔
2296

2297
        assert(cd);
26✔
2298
        assert(name);
26✔
2299
        assert(key_file);
26✔
2300

2301
        /* If we read the key via AF_UNIX, make this client recognizable */
2302
        bindname = make_bindname(name, /* token_type= */ _TOKEN_TYPE_INVALID);
26✔
2303
        if (!bindname)
26✔
2304
                return log_oom();
×
2305

2306
        r = read_full_file_full(
29✔
2307
                        AT_FDCWD, key_file,
2308
                        arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset,
26✔
2309
                        arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size,
26✔
2310
                        READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
2311
                        bindname,
2312
                        &kfdata, &kfsize);
2313
        if (r == -E2BIG) {
26✔
2314
                log_error_errno(r, "Failed to activate, key file '%s' too large.", key_file);
×
2315
                return -EAGAIN;
×
2316
        }
2317
        if (r == -ENOENT) {
26✔
2318
                log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file);
×
2319
                return -EAGAIN; /* Log actual error, but return EAGAIN */
×
2320
        }
2321
        if (r < 0)
26✔
2322
                return log_error_errno(r, "Failed to read key file '%s': %m", key_file);
×
2323

2324
        if (pass_volume_key)
26✔
2325
                r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, kfdata, kfsize, flags);
×
2326
        else
2327
                r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, kfdata, kfsize, flags);
26✔
2328
        if (r == -EPERM) {
26✔
2329
                log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file);
3✔
2330
                return -EAGAIN; /* Log actual error, but return EAGAIN */
3✔
2331
        }
2332
        if (r < 0)
23✔
2333
                return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file);
2✔
2334

2335
        return 0;
2336
}
2337

2338
static int attach_luks_or_plain_or_bitlk_by_passphrase(
×
2339
                struct crypt_device *cd,
2340
                const char *name,
2341
                char **passwords,
2342
                uint32_t flags,
2343
                bool pass_volume_key) {
2344

2345
        int r;
×
2346

2347
        assert(cd);
×
2348
        assert(name);
×
2349

2350
        r = -EINVAL;
2351
        STRV_FOREACH(p, passwords) {
×
2352
                if (pass_volume_key)
×
2353
                        r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, *p, arg_key_size, flags);
×
2354
                else
2355
                        r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, *p, strlen(*p), flags);
×
2356
                if (r >= 0)
×
2357
                        break;
2358
        }
2359
        if (r == -EPERM) {
×
2360
                log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)");
×
2361
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
2362
        }
2363
        if (r < 0)
×
2364
                return log_error_errno(r, "Failed to activate with specified passphrase: %m");
×
2365

2366
        return 0;
2367
}
2368

2369
static int attach_luks_or_plain_or_bitlk(
39✔
2370
                struct crypt_device *cd,
2371
                const char *name,
2372
                TokenType token_type,
2373
                const char *key_file,
2374
                const struct iovec *key_data,
2375
                char **passwords,
2376
                uint32_t flags,
2377
                usec_t until) {
2378

2379
        bool pass_volume_key = false;
39✔
2380
        int r;
39✔
2381

2382
        assert(cd);
39✔
2383
        assert(name);
39✔
2384

2385
        if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) {
39✔
2386
                struct crypt_params_plain params = {
×
2387
                        .offset = arg_offset,
2388
                        .skip = arg_skip,
2389
                        .sector_size = arg_sector_size,
2390
                };
2391
                const char *cipher, *cipher_mode;
×
2392
                _cleanup_free_ char *truncated_cipher = NULL;
×
2393

2394
                if (streq_ptr(arg_hash, "plain"))
×
2395
                        /* plain isn't a real hash type. it just means "use no hash" */
2396
                        params.hash = NULL;
2397
                else if (arg_hash)
×
2398
                        params.hash = arg_hash;
×
2399
                else if (!key_file)
×
2400
                        /* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key
2401
                         * file is provided */
2402
                        params.hash = "ripemd160";
×
2403

2404
                if (arg_cipher) {
×
2405
                        size_t l;
×
2406

2407
                        l = strcspn(arg_cipher, "-");
×
2408
                        truncated_cipher = strndup(arg_cipher, l);
×
2409
                        if (!truncated_cipher)
×
2410
                                return log_oom();
×
2411

2412
                        cipher = truncated_cipher;
×
2413
                        cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain";
×
2414
                } else {
2415
                        cipher = "aes";
2416
                        cipher_mode = "cbc-essiv:sha256";
2417
                }
2418

2419
                /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */
2420
                arg_keyfile_size = arg_key_size;
×
2421

2422
                /* In contrast to what the name crypt_format() might suggest this doesn't actually format
2423
                 * anything, it just configures encryption parameters when used for plain mode. */
2424
                r = crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, &params);
×
2425
                if (r < 0)
×
2426
                        return log_error_errno(r, "Loading of cryptographic parameters failed: %m");
×
2427

2428
                /* hash == NULL implies the user passed "plain" */
2429
                pass_volume_key = !params.hash;
×
2430
        }
2431

2432
        log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
39✔
2433
                 crypt_get_cipher(cd),
2434
                 crypt_get_cipher_mode(cd),
2435
                 crypt_get_volume_key_size(cd)*8,
2436
                 crypt_get_device_name(cd));
2437

2438
        if (token_type == TOKEN_TPM2)
39✔
2439
                return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, until, flags, pass_volume_key);
10✔
2440
        if (token_type == TOKEN_FIDO2)
29✔
2441
                return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, until, flags, pass_volume_key);
×
2442
        if (token_type == TOKEN_PKCS11)
29✔
2443
                return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, until, flags, pass_volume_key);
×
2444
        if (key_data)
29✔
2445
                return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, flags, pass_volume_key);
3✔
2446
        if (key_file)
26✔
2447
                return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key);
26✔
2448

2449
        return attach_luks_or_plain_or_bitlk_by_passphrase(cd, name, passwords, flags, pass_volume_key);
×
2450
}
2451

2452
static int help(void) {
×
2453
        _cleanup_free_ char *link = NULL;
×
2454
        int r;
×
2455

2456
        r = terminal_urlify_man("systemd-cryptsetup", "8", &link);
×
2457
        if (r < 0)
×
2458
                return log_oom();
×
2459

2460
        printf("%1$s attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG]\n"
×
2461
               "%1$s detach VOLUME\n\n"
2462
               "%2$sAttach or detach an encrypted block device.%3$s\n\n"
2463
               "  -h --help            Show this help\n"
2464
               "     --version         Show package version\n"
2465
               "\nSee the %4$s for details.\n",
2466
               program_invocation_short_name,
2467
               ansi_highlight(),
2468
               ansi_normal(),
2469
               link);
2470

2471
        return 0;
2472
}
2473

2474
static int parse_argv(int argc, char *argv[]) {
135✔
2475
        enum {
135✔
2476
                ARG_VERSION = 0x100,
2477
        };
2478

2479
        static const struct option options[] = {
135✔
2480
                { "help",                         no_argument,       NULL, 'h'                       },
2481
                { "version",                      no_argument,       NULL, ARG_VERSION               },
2482
                {}
2483
        };
2484

2485
        int c;
135✔
2486

2487
        assert(argc >= 0);
135✔
2488
        assert(argv);
135✔
2489

2490
        if (argv_looks_like_help(argc, argv))
135✔
2491
                return help();
×
2492

2493
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
135✔
2494
                switch (c) {
×
2495

2496
                case 'h':
×
2497
                        return help();
×
2498

2499
                case ARG_VERSION:
×
2500
                        return version();
×
2501

2502
                case '?':
2503
                        return -EINVAL;
2504

2505
                default:
×
2506
                        assert_not_reached();
×
2507
                }
2508

2509
        return 1;
2510
}
2511

2512
static uint32_t determine_flags(void) {
76✔
2513
        uint32_t flags = 0;
76✔
2514

2515
        if (arg_readonly)
76✔
2516
                flags |= CRYPT_ACTIVATE_READONLY;
×
2517

2518
        if (arg_discards)
76✔
2519
                flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
×
2520

2521
        if (arg_same_cpu_crypt)
76✔
2522
                flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
×
2523

2524
        if (arg_submit_from_crypt_cpus)
76✔
2525
                flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
×
2526

2527
        if (arg_no_read_workqueue)
76✔
2528
                flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
×
2529

2530
        if (arg_no_write_workqueue)
76✔
2531
                flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
×
2532

2533
        /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
2534
        /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
2535
        flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF;
76✔
2536

2537
        return flags;
76✔
2538
}
2539

2540
static void remove_and_erasep(const char **p) {
76✔
2541
        int r;
76✔
2542

2543
        assert(p);
76✔
2544

2545
        if (!*p)
76✔
2546
                return;
2547

2548
        r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE);
2✔
2549
        if (r < 0 && r != -ENOENT)
2✔
2550
                log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p);
×
2551
}
2552

2553
static TokenType determine_token_type(void) {
53✔
2554
        if (arg_tpm2_device || arg_tpm2_device_auto)
53✔
2555
                return TOKEN_TPM2;
2556
        if (arg_fido2_device || arg_fido2_device_auto)
43✔
2557
                return TOKEN_FIDO2;
2558
        if (arg_pkcs11_uri || arg_pkcs11_uri_auto)
43✔
2559
                return TOKEN_PKCS11;
×
2560

2561
        return _TOKEN_TYPE_INVALID;
2562
}
2563

2564
static int discover_key(const char *key_file, const char *volume, TokenType token_type, struct iovec *ret_key_data) {
24✔
2565
        _cleanup_free_ char *bindname = NULL;
24✔
2566
        const char *token_type_name;
24✔
2567
        int r;
24✔
2568

2569
        assert(key_file);
24✔
2570
        assert(volume);
24✔
2571
        assert(ret_key_data);
24✔
2572

2573
        bindname = make_bindname(volume, token_type);
24✔
2574
        if (!bindname)
24✔
2575
                return log_oom();
×
2576

2577
        /* If a key file is not explicitly specified, search for a key in a well defined search path, and load it. */
2578
        r = find_key_file(key_file, STRV_MAKE("/etc/cryptsetup-keys.d", "/run/cryptsetup-keys.d"), bindname, ret_key_data);
24✔
2579
        if (r <= 0)
24✔
2580
                return r;
2581

2582
        token_type_name = token_type_to_string(token_type);
1✔
2583
        if (token_type_name)
1✔
2584
                log_debug("Automatically discovered encrypted key for volume '%s' (token type: %s).", volume, token_type_name);
×
2585
        else
2586
                log_debug("Automatically discovered key for volume '%s'.", volume);
1✔
2587

2588
        return r;
2589
}
2590

2591
static int verb_attach(int argc, char *argv[], uintptr_t _data, void *userdata) {
76✔
2592
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
76✔
2593
        _unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL;
76✔
2594
        crypt_status_info status;
76✔
2595
        uint32_t flags = 0;
76✔
2596
        unsigned tries;
76✔
2597
        usec_t until;
76✔
2598
        PassphraseType passphrase_type = PASSPHRASE_NONE;
76✔
2599
        int r;
76✔
2600

2601
        /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG] */
2602

2603
        assert(argc >= 3 && argc <= 5);
76✔
2604

2605
        const char *volume = ASSERT_PTR(argv[1]),
76✔
2606
                *source = ASSERT_PTR(argv[2]),
76✔
2607
                *key_file = argc >= 4 ? mangle_none(argv[3]) : NULL,
76✔
2608
                *config = argc >= 5 ? mangle_none(argv[4]) : NULL;
76✔
2609

2610
        if (!filename_is_valid(volume))
76✔
2611
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
2612

2613
        if (key_file && !path_is_absolute(key_file)) {
76✔
2614
                log_warning("Password file path '%s' is not absolute. Ignoring.", key_file);
×
2615
                key_file = NULL;
2616
        }
2617

2618
        if (config) {
76✔
2619
                r = parse_crypt_config(config);
74✔
2620
                if (r < 0)
74✔
2621
                        return r;
2622
        }
2623

2624
        log_debug("%s %s ← %s type=%s cipher=%s", __func__,
211✔
2625
                  volume, source, strempty(arg_type), strempty(arg_cipher));
2626

2627
        /* A delicious drop of snake oil */
2628
        (void) safe_mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT);
76✔
2629

2630
        if (key_file && arg_keyfile_erase)
76✔
2631
                destroy_key_file = key_file; /* let's get this baby erased when we leave */
2✔
2632

2633
        if (arg_header) {
76✔
2634
                if (streq_ptr(arg_type, CRYPT_TCRYPT)){
9✔
2635
                        log_debug("tcrypt header: %s", arg_header);
×
2636
                        r = crypt_init_data_device(&cd, arg_header, source);
×
2637
                } else {
2638
                        log_debug("LUKS header: %s", arg_header);
9✔
2639
                        r = crypt_init(&cd, arg_header);
9✔
2640
                }
2641
        } else
2642
                r = crypt_init(&cd, source);
67✔
2643
        if (r < 0)
76✔
2644
                return log_error_errno(r, "crypt_init() failed: %m");
×
2645

2646
        cryptsetup_enable_logging(cd);
76✔
2647

2648
        status = crypt_status(cd, volume);
76✔
2649
        if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
76✔
2650
                log_info("Volume %s already active.", volume);
×
2651
                return 0;
×
2652
        }
2653

2654
        flags = determine_flags();
76✔
2655

2656
        until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
76✔
2657
        if (until == USEC_INFINITY)
×
2658
                until = 0;
76✔
2659

2660
        if (arg_key_size == 0)
76✔
2661
                arg_key_size = 256U / 8U;
76✔
2662

2663
        if (key_file) {
76✔
2664
                struct stat st;
26✔
2665

2666
                /* Ideally we'd do this on the open fd, but since this is just a warning it's OK to do this
2667
                 * in two steps. */
2668
                if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005))
26✔
2669
                        log_warning("Key file %s is world-readable. This is not a good idea!", key_file);
6✔
2670
        }
2671

2672
        if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) {
76✔
2673
                r = crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL);
76✔
2674
                if (r < 0)
76✔
2675
                        return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd));
35✔
2676

2677
/* since cryptsetup 2.7.0 (Jan 2024) */
2678
#if HAVE_CRYPT_SET_KEYRING_TO_LINK
2679
                if (arg_link_key_description) {
76✔
2680
                        r = crypt_set_keyring_to_link(cd, arg_link_key_description, NULL, arg_link_key_type, arg_link_keyring);
×
2681
                        if (r < 0)
×
2682
                                log_warning_errno(r, "Failed to set keyring or key description to link volume key in, ignoring: %m");
×
2683
                }
2684
#endif
2685

2686
                if (arg_header) {
76✔
2687
                        r = crypt_set_data_device(cd, source);
9✔
2688
                        if (r < 0)
9✔
2689
                                return log_error_errno(r, "Failed to set LUKS data device %s: %m", source);
×
2690
                }
2691

2692
                /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */
2693
                if (!key_file && use_token_plugins()) {
76✔
2694
                        r = crypt_activate_by_token_pin_ask_password(
49✔
2695
                                        cd,
2696
                                        volume,
2697
                                        /* type= */ NULL,
2698
                                        until,
2699
                                        /* userdata= */ NULL,
2700
                                        flags,
2701
                                        "Please enter LUKS2 token PIN:",
2702
                                        "luks2-pin",
2703
                                        "cryptsetup.luks2-pin");
2704
                        if (r >= 0) {
49✔
2705
                                log_debug("Volume %s activated with a LUKS token.", volume);
35✔
2706
                                return 0;
35✔
2707
                        }
2708

2709
                        log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd));
14✔
2710
                }
2711
        }
2712

2713
        if (streq_ptr(arg_type, CRYPT_BITLK)) {
41✔
2714
                r = crypt_load(cd, CRYPT_BITLK, NULL);
×
2715
                if (r < 0)
×
2716
                        return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd));
×
2717
        }
2718

2719
        bool use_cached_passphrase = true, try_discover_key = !key_file;
41✔
2720
        const char *discovered_key_fn = strjoina(volume, ".key");
205✔
2721
        _cleanup_strv_free_erase_ char **passwords = NULL;
41✔
2722
        for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
53✔
2723
                _cleanup_(iovec_done_erase) struct iovec discovered_key_data = {};
53✔
2724
                const struct iovec *key_data = NULL;
53✔
2725
                TokenType token_type = determine_token_type();
53✔
2726

2727
                log_debug("Beginning attempt %u to unlock.", tries);
53✔
2728

2729
                /* When we were able to acquire multiple keys, let's always process them in this order:
2730
                 *
2731
                 *    1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip
2732
                 *    2. The configured or discovered key, of which both are exclusive and optional
2733
                 *    3. The empty password, in case arg_try_empty_password is set
2734
                 *    4. We enquire the user for a password
2735
                 */
2736

2737
                if (try_discover_key) {
53✔
2738
                        r = discover_key(discovered_key_fn, volume, token_type, &discovered_key_data);
24✔
2739
                        if (r < 0)
24✔
2740
                                return r;
2741
                        if (r > 0)
24✔
2742
                                key_data = &discovered_key_data;
1✔
2743
                }
2744

2745
                if (token_type < 0 && !key_file && !key_data && !passwords) {
53✔
2746

2747
                        /* If we have nothing to try anymore, then acquire a new password */
2748

2749
                        if (arg_try_empty_password) {
16✔
2750
                                /* Hmm, let's try an empty password now, but only once */
2751
                                arg_try_empty_password = false;
2✔
2752
                                key_data = &iovec_empty;
2✔
2753
                        } else {
2754
                                /* Ask the user for a passphrase or recovery key only as last resort, if we
2755
                                 * have nothing else to check for */
2756
                                if (passphrase_type == PASSPHRASE_NONE) {
14✔
2757
                                        passphrase_type = check_registered_passwords(cd);
14✔
2758
                                        if (passphrase_type == PASSPHRASE_NONE)
14✔
2759
                                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No passphrase or recovery key registered.");
×
2760
                                }
2761

2762
                                r = get_password(
14✔
2763
                                                volume,
2764
                                                source,
2765
                                                until,
2766
                                                /* ignore_cached= */ !use_cached_passphrase || arg_verify,
14✔
2767
                                                passphrase_type,
2768
                                                &passwords);
2769
                                use_cached_passphrase = false;
14✔
2770
                                if (r == -EAGAIN)
14✔
2771
                                        continue;
×
2772
                                if (r < 0)
14✔
2773
                                        return r;
2774
                        }
2775
                }
2776

2777
                if (streq_ptr(arg_type, CRYPT_TCRYPT))
39✔
2778
                        r = attach_tcrypt(cd, volume, token_type, key_file, key_data, passwords, flags);
×
2779
                else
2780
                        r = attach_luks_or_plain_or_bitlk(cd, volume, token_type, key_file, key_data, passwords, flags, until);
39✔
2781
                if (r >= 0)
39✔
2782
                        break;
2783
                if (r != -EAGAIN)
14✔
2784
                        return r;
2785

2786
                /* Key not correct? Let's try again, but let's invalidate one of the passed fields, so that
2787
                 * we fall back to the next best thing. */
2788

2789
                if (token_type == TOKEN_TPM2) {
12✔
2790
                        arg_tpm2_device = mfree(arg_tpm2_device);
9✔
2791
                        arg_tpm2_device_auto = false;
9✔
2792
                        continue;
9✔
2793
                }
2794

2795
                if (token_type == TOKEN_FIDO2) {
3✔
2796
                        arg_fido2_device = mfree(arg_fido2_device);
×
2797
                        arg_fido2_device_auto = false;
×
2798
                        continue;
×
2799
                }
2800

2801
                if (token_type == TOKEN_PKCS11) {
3✔
2802
                        arg_pkcs11_uri = mfree(arg_pkcs11_uri);
×
2803
                        arg_pkcs11_uri_auto = false;
×
2804
                        continue;
×
2805
                }
2806

2807
                if (try_discover_key) {
3✔
2808
                        try_discover_key = false;
×
2809
                        continue;
×
2810
                }
2811

2812
                if (key_file) {
3✔
2813
                        key_file = NULL;
3✔
2814
                        continue;
3✔
2815
                }
2816

2817
                if (passwords) {
×
2818
                        passwords = strv_free_erase(passwords);
×
2819
                        continue;
×
2820
                }
2821

2822
                log_debug("Prepared for next attempt to unlock.");
×
2823
        }
2824

2825
        if (arg_tries != 0 && tries >= arg_tries)
25✔
2826
                return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up.");
×
2827

2828
        return 0;
2829
}
2830

2831
static int verb_detach(int argc, char *argv[], uintptr_t _data, void *userdata) {
59✔
2832
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
59✔
2833
        const char *volume = ASSERT_PTR(argv[1]);
59✔
2834
        int r;
59✔
2835

2836
        assert(argc == 2);
59✔
2837

2838
        if (!filename_is_valid(volume))
59✔
2839
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
2840

2841
        r = crypt_init_by_name(&cd, volume);
59✔
2842
        if (r == -ENODEV) {
59✔
2843
                log_info("Volume %s already inactive.", volume);
×
2844
                return 0;
×
2845
        }
2846
        if (r < 0)
59✔
2847
                return log_error_errno(r, "crypt_init_by_name() for volume '%s' failed: %m", volume);
×
2848

2849
        cryptsetup_enable_logging(cd);
59✔
2850

2851
        r = crypt_deactivate(cd, volume);
59✔
2852
        if (r < 0)
59✔
2853
                return log_error_errno(r, "Failed to deactivate '%s': %m", volume);
×
2854

2855
        return 0;
2856
}
2857

2858
static int run(int argc, char *argv[]) {
135✔
2859
        int r;
135✔
2860

2861
        log_setup();
135✔
2862

2863
        umask(0022);
135✔
2864

2865
        r = parse_argv(argc, argv);
135✔
2866
        if (r <= 0)
135✔
2867
                return r;
2868

2869
        cryptsetup_enable_logging(NULL);
135✔
2870

2871
        static const Verb verbs[] = {
135✔
2872
                { "attach", 3, 5, 0, verb_attach },
2873
                { "detach", 2, 2, 0, verb_detach },
2874
                {}
2875
        };
2876

2877
        return dispatch_verb(argc, argv, verbs, NULL);
135✔
2878
}
2879

2880
DEFINE_MAIN_FUNCTION(run);
135✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc