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

systemd / systemd / 25834419893

13 May 2026 10:51PM UTC coverage: 72.584% (+0.07%) from 72.513%
25834419893

push

github

bluca
nsresourced: re-link GID delegation file after atomic UID file write

userns_registry_remove() restores a sub-delegated UID range by writing
the previous owner's data to u<UID>.delegate with WRITE_STRING_FILE_ATOMIC.
Atomic writes go via a temp file and rename, which replaces the directory
entry with a fresh inode and severs the hardlink to g<GID>.delegate. The
stale GID side then keeps pointing at the prior inode with outdated owner
and ancestor data, so subsequent lookups via GID return wrong results.

Re-create the hardlink after the atomic write so the two views stay in
sync, matching what userns_registry_store() already does after writing
a new delegation.

5 of 8 new or added lines in 1 file covered. (62.5%)

4652 existing lines in 93 files now uncovered.

328958 of 453210 relevant lines covered (72.58%)

1305869.89 hits per line

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

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

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

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

12
#include "alloc-util.h"
13
#include "ask-password-api.h"
14
#include "build.h"
15
#include "crypto-util.h"
16
#include "cryptsetup-fido2.h"
17
#include "cryptsetup-keyfile.h"
18
#include "cryptsetup-pkcs11.h"
19
#include "cryptsetup-tpm2.h"
20
#include "cryptsetup-util.h"
21
#include "efi-api.h"
22
#include "efi-loader.h"
23
#include "efivars.h"
24
#include "env-util.h"
25
#include "errno-util.h"
26
#include "escape.h"
27
#include "extract-word.h"
28
#include "fileio.h"
29
#include "format-table.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 "options.h"
40
#include "parse-util.h"
41
#include "path-util.h"
42
#include "pkcs11-util.h"
43
#include "pretty-print.h"
44
#include "process-util.h"
45
#include "random-util.h"
46
#include "string-table.h"
47
#include "string-util.h"
48
#include "strv.h"
49
#include "time-util.h"
50
#include "tpm2-pcr.h"
51
#include "tpm2-util.h"
52
#include "verbs.h"
53

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

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

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

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

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

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

156
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(passphrase_type, PassphraseType);
×
157

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

164
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(token_type, TokenType);
53✔
165

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

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

180
        assert(option);
153✔
181

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

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

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

194
        } else if ((val = startswith(option, "size="))) {
152✔
195

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

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

207
                arg_key_size /= 8;
×
208

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

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

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

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

225
        } else if ((val = startswith(option, "key-slot=")) ||
152✔
226
                   (val = startswith(option, "keyslot="))) {
150✔
227

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

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

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

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

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

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

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

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

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

262
                arg_keyfile_erase = r;
2✔
263

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

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

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

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

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

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

288
        } else if ((val = startswith(option, "tries="))) {
131✔
289

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

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

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

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

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

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

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

367
        } else if ((val = startswith(option, "offset="))) {
123✔
368

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

373
        } else if ((val = startswith(option, "skip="))) {
123✔
374

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

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

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

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

392
                        arg_pkcs11_uri_auto = false;
×
393
                }
394

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

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

405
                        arg_fido2_device_auto = false;
×
406
                }
407

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

478
                        arg_tpm2_device_auto = false;
×
479
                }
480

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

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

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

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

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

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

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

505
                arg_tpm2_pin = r;
×
506

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

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

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

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

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

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

534
                arg_tpm2_measure_pcr = pcr;
×
535

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

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

541
                r = dlopen_libcrypto(LOG_ERR);
×
542
                if (r < 0)
×
543
                        return r;
544

545
                l = strv_split(val, ":");
×
546
                if (!l)
×
547
                        return log_oom();
×
548

549
                STRV_FOREACH(i, l) {
×
550
                        const EVP_MD *implementation;
×
551

552
                        implementation = sym_EVP_get_digestbyname(*i);
×
553
                        if (!implementation)
×
554
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown bank '%s', refusing.", val);
×
555

556
                        if (strv_extend(&arg_tpm2_measure_banks, sym_EVP_MD_get0_name(implementation)) < 0)
×
557
                                return log_oom();
×
558
                }
559
#else
560
                log_error("Build lacks OpenSSL support, cannot measure to PCR banks, ignoring: %s", option);
561
#endif
562

563
        } else if ((val = startswith(option, "tpm2-measure-keyslot-nvpcr="))) {
70✔
564

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

577
                if (free_and_strdup(&arg_tpm2_measure_keyslot_nvpcr, val) < 0)
×
578
                        return log_oom();
×
579

580
        } else if ((val = startswith(option, "try-empty-password="))) {
70✔
581

582
                r = parse_boolean(val);
1✔
583
                if (r < 0) {
1✔
584
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
585
                        return 0;
586
                }
587

588
                arg_try_empty_password = r;
1✔
589

590
        } else if (streq(option, "try-empty-password"))
69✔
591
                arg_try_empty_password = true;
1✔
592
        else if ((val = startswith(option, "headless="))) {
68✔
593

594
                r = parse_boolean(val);
57✔
595
                if (r < 0) {
57✔
596
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
597
                        return 0;
598
                }
599

600
                SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_HEADLESS, r);
57✔
601
        } else if (streq(option, "headless"))
11✔
602
                arg_ask_password_flags |= ASK_PASSWORD_HEADLESS;
7✔
603

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

606
                r = parse_sec_fix_0(val, &arg_token_timeout_usec);
×
607
                if (r < 0)
×
608
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
123✔
609

610
        } else if ((val = startswith(option, "link-volume-key="))) {
4✔
UNCOV
611
                _cleanup_free_ char *keyring = NULL, *key_type = NULL, *key_description = NULL;
×
612
                const char *sep;
×
613

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

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

631
                /* add type prefix if missing (crypt_set_keyring_to_link() expects it) */
UNCOV
632
                if (!IN_SET(*keyring, '@', '%'))
×
633
                        if (!strprepend(&keyring, "%:"))
×
634
                                return log_oom();
×
635

UNCOV
636
                sep += 2;
×
637

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

UNCOV
645
                        key_type = strndup(sep + 1, c - sep - 1);
×
646
                        if (!key_type)
×
647
                                return log_oom();
×
648

UNCOV
649
                        sep = c + 1;
×
650
                }
651

UNCOV
652
                key_description = strdup(sep);
×
653
                if (!key_description)
×
654
                        return log_oom();
×
655

UNCOV
656
                free_and_replace(arg_link_keyring, keyring);
×
657
                free_and_replace(arg_link_key_type, key_type);
×
658
                free_and_replace(arg_link_key_description, key_description);
×
659
        } else if ((val = startswith(option, "fixate-volume-key="))) {
4✔
660
                r = free_and_strdup(&arg_fixate_volume_key, val);
4✔
661
                if (r < 0)
4✔
UNCOV
662
                        return log_oom();
×
663

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

667
        return 0;
668
}
669

670
static int parse_crypt_config(const char *options) {
76✔
671
        assert(options);
76✔
672

673
        for (;;) {
382✔
674
                _cleanup_free_ char *word = NULL;
153✔
675
                int r;
229✔
676

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

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

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

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

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

707
        return 0;
708
}
709

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

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

720
        assert(path);
12✔
721

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

725
        if (!S_ISBLK(st.st_mode))
12✔
726
                return NULL;
727

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

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

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

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

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

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

755
        return NULL;
756
}
757

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

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

766
        assert(label);
12✔
767

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

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

UNCOV
776
        for (;;) {
×
777
                struct libmnt_fs *fs;
12✔
778

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

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

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

796
        assert(src);
12✔
797
        assert(vol);
12✔
798

799
        description = disk_description(src);
12✔
800
        mount_point = disk_mount_point(vol);
12✔
801

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

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

UNCOV
817
        return name_buffer;
×
818
}
819

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

825
        assert(cd);
15✔
826

827
        if (!streq_ptr(sym_crypt_get_type(cd), CRYPT_LUKS2)) {
15✔
UNCOV
828
                log_debug("%s: not a LUKS2 device, only passphrases are supported", sym_crypt_get_device_name(cd));
×
829
                return PASSPHRASE_REGULAR;
830
        }
831

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

838
        for (int slot = 0; slot < slot_max; slot++)
495✔
839
                slots[slot] = IN_SET(sym_crypt_keyslot_status(cd, slot), CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST);
480✔
840

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

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

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

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

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

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

875
                        JSON_VARIANT_ARRAY_FOREACH(z, w) {
20✔
876
                                unsigned u;
10✔
877
                                int at;
10✔
878

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

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

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

895
                                slots[u] = false;
10✔
896
                        }
897
                }
898
        }
899

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

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

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

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

925
        assert(vol);
15✔
926
        assert(src);
15✔
927
        assert(ret);
15✔
928

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

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

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

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

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

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

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

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

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

UNCOV
968
                assert(strv_length(passwords) == 1);
×
969

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

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

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

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

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

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

993
        strv_uniq(passwords);
×
994

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

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

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

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

1011
        *ret = TAKE_PTR(passwords);
×
1012

UNCOV
1013
        return 0;
×
1014
}
1015

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

UNCOV
1022
        int r;
×
1023

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

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

1034
        r = efi_measured_os(LOG_WARNING);
×
1035
        if (r < 0)
×
1036
                return r;
UNCOV
1037
        if (r == 0) {
×
1038
                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.");
×
1039
                return 0;
1040
        }
1041

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

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

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

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

UNCOV
1063
        _cleanup_free_ char *prefix = NULL;
×
1064

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

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

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

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

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

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

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

1112
#if HAVE_TPM2
UNCOV
1113
        r = efi_measured_os(LOG_WARNING);
×
UNCOV
1114
        if (r < 0)
×
1115
                return r;
UNCOV
1116
        if (r == 0) {
×
1117
                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.");
×
1118
                return 0;
1119
        }
1120

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

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

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

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

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

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

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

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

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

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

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

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

1185
        int r;
4✔
1186

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

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

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

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

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

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

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

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

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

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

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

1244
        assert(cd);
32✔
1245

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

1251
        if (arg_tpm2_measure_pcr == UINT_MAX && !arg_fixate_volume_key)
32✔
1252
                goto shortcut;
28✔
1253

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1354
                        return r;
1355
                }
1356

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

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

1371
        return 0;
1372
}
1373

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

1379
        switch (token_type) {
52✔
1380

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

1385
        default:
1386
                suffix = NULL;
52✔
1387
        }
1388

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

1399
        return bindname;
52✔
1400
}
1401

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1453
        assert(event);
×
1454
        assert(monitor);
×
1455

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

UNCOV
1459
        for (;;) {
×
UNCOV
1460
                int x;
×
1461

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

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

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

1481
                processed = true;
×
1482
        }
1483
}
1484

1485
static bool use_token_plugins(void) {
60✔
1486

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

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

1503
#if HAVE_LIBCRYPTSETUP_PLUGINS
1504
        int r;
60✔
1505

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

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

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

1525
        assert(ret_pins);
21✔
1526

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

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

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

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

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

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

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

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

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

UNCOV
1583
        for (;;) {
×
UNCOV
1584
                pins = strv_free_erase(pins);
×
1585

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

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

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

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

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

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

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

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

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

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

1662
        friendly = friendly_disk_name(sym_crypt_get_device_name(cd), name);
×
1663
        if (!friendly)
×
UNCOV
1664
                return log_oom();
×
1665

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

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

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

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

1711
                        assert(!event);
×
1712

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

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

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

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

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

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

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

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

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

1766
        return 0;
1767
}
1768

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

1777
#if HAVE_LIBCRYPTSETUP_PLUGINS
UNCOV
1778
        int r;
×
1779

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

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

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

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

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

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

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

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

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

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

1846
        friendly = friendly_disk_name(sym_crypt_get_device_name(cd), name);
×
1847
        if (!friendly)
×
UNCOV
1848
                return log_oom();
×
1849

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

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

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

1880
                        assert(!event);
×
1881

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

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

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

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

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

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

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

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

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

1943
        return 0;
1944
}
1945

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2036
        assert(cd);
12✔
2037
        assert(name);
12✔
2038
        assert(arg_tpm2_device || arg_tpm2_device_auto);
12✔
2039

2040
        friendly = friendly_disk_name(sym_crypt_get_device_name(cd), name);
12✔
2041
        if (!friendly)
12✔
UNCOV
2042
                return log_oom();
×
2043

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

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

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

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

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

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

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

2151
                                found_some = true;
1✔
2152

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

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

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

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

2197
                        assert(!event);
×
2198

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

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

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

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

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

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

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

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

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

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

2256
        return 0;
2257
}
2258

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

2266
        int r;
3✔
2267

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

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

2283
        return 0;
2284
}
2285

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

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

2298
        assert(cd);
28✔
2299
        assert(name);
28✔
2300
        assert(key_file);
28✔
2301

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

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

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

2336
        return 0;
2337
}
2338

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

UNCOV
2346
        int r;
×
2347

UNCOV
2348
        assert(cd);
×
UNCOV
2349
        assert(name);
×
2350

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

2367
        return 0;
2368
}
2369

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

2380
        bool pass_volume_key = false;
43✔
2381
        int r;
43✔
2382

2383
        assert(cd);
43✔
2384
        assert(name);
43✔
2385

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

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

UNCOV
2405
                if (arg_cipher) {
×
UNCOV
2406
                        size_t l;
×
2407

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

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

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

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

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

2433
        log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
43✔
2434
                 sym_crypt_get_cipher(cd),
2435
                 sym_crypt_get_cipher_mode(cd),
2436
                 sym_crypt_get_volume_key_size(cd)*8,
2437
                 sym_crypt_get_device_name(cd));
2438

2439
        if (token_type == TOKEN_TPM2)
43✔
2440
                return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, until, flags, pass_volume_key);
12✔
2441
        if (token_type == TOKEN_FIDO2)
31✔
UNCOV
2442
                return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, until, flags, pass_volume_key);
×
2443
        if (token_type == TOKEN_PKCS11)
31✔
UNCOV
2444
                return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, until, flags, pass_volume_key);
×
2445
        if (key_data)
31✔
2446
                return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, flags, pass_volume_key);
3✔
2447
        if (key_file)
28✔
2448
                return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key);
28✔
2449

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

UNCOV
2453
static int help(void) {
×
2454
        _cleanup_free_ char *link = NULL;
×
UNCOV
2455
        _cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
×
UNCOV
2456
        int r;
×
2457

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

2462
        r = verbs_get_help_table(&verbs);
×
2463
        if (r < 0)
×
2464
                return r;
2465

2466
        r = option_parser_get_help_table(&options);
×
2467
        if (r < 0)
×
2468
                return r;
2469

2470
        (void) table_sync_column_widths(0, verbs, options);
×
2471

UNCOV
2472
        printf("%s [OPTIONS...] {COMMAND} ...\n\n"
×
2473
               "%sAttach or detach an encrypted block device.%s\n"
2474
               "\n%sCommands:%s\n",
2475
               program_invocation_short_name,
2476
               ansi_highlight(),
2477
               ansi_normal(),
2478
               ansi_underline(),
2479
               ansi_normal());
2480

UNCOV
2481
        r = table_print_or_warn(verbs);
×
UNCOV
2482
        if (r < 0)
×
2483
                return r;
2484

2485
        printf("\n%sOptions:%s\n",
×
2486
               ansi_underline(),
2487
               ansi_normal());
2488

2489
        r = table_print_or_warn(options);
×
UNCOV
2490
        if (r < 0)
×
2491
                return r;
2492

2493
        printf("\nSee the %s for details.\n", link);
×
2494
        return 0;
2495
}
2496

2497
VERB_COMMON_HELP_HIDDEN(help);
×
2498

2499
static int parse_argv(int argc, char *argv[], char ***ret_args) {
142✔
2500
        assert(argc >= 0);
142✔
2501
        assert(argv);
142✔
2502
        assert(ret_args);
142✔
2503

2504
        OptionParser opts = { argc, argv };
142✔
2505

2506
        FOREACH_OPTION_OR_RETURN(c, &opts)
142✔
UNCOV
2507
                switch (c) {
×
2508

UNCOV
2509
                OPTION_COMMON_HELP:
×
UNCOV
2510
                        return help();
×
2511

UNCOV
2512
                OPTION_COMMON_VERSION:
×
2513
                        return version();
×
2514
                }
2515

2516
        *ret_args = option_parser_get_args(&opts);
142✔
2517
        return 1;
142✔
2518
}
2519

2520
static uint32_t determine_flags(void) {
78✔
2521
        uint32_t flags = 0;
78✔
2522

2523
        if (arg_readonly)
78✔
UNCOV
2524
                flags |= CRYPT_ACTIVATE_READONLY;
×
2525

2526
        if (arg_discards)
78✔
UNCOV
2527
                flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
×
2528

2529
        if (arg_same_cpu_crypt)
78✔
UNCOV
2530
                flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
×
2531

2532
        if (arg_submit_from_crypt_cpus)
78✔
UNCOV
2533
                flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
×
2534

2535
        if (arg_no_read_workqueue)
78✔
UNCOV
2536
                flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
×
2537

2538
        if (arg_no_write_workqueue)
78✔
UNCOV
2539
                flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
×
2540

2541
        /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
2542
        /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
2543
        flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF;
78✔
2544

2545
        return flags;
78✔
2546
}
2547

2548
static void remove_and_erasep(const char **p) {
78✔
2549
        int r;
78✔
2550

2551
        assert(p);
78✔
2552

2553
        if (!*p)
78✔
2554
                return;
2555

2556
        r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE);
2✔
2557
        if (r < 0 && r != -ENOENT)
2✔
UNCOV
2558
                log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p);
×
2559
}
2560

2561
static TokenType determine_token_type(void) {
58✔
2562
        if (arg_tpm2_device || arg_tpm2_device_auto)
58✔
2563
                return TOKEN_TPM2;
2564
        if (arg_fido2_device || arg_fido2_device_auto)
46✔
2565
                return TOKEN_FIDO2;
2566
        if (arg_pkcs11_uri || arg_pkcs11_uri_auto)
46✔
UNCOV
2567
                return TOKEN_PKCS11;
×
2568

2569
        return _TOKEN_TYPE_INVALID;
2570
}
2571

2572
static int discover_key(const char *key_file, const char *volume, TokenType token_type, struct iovec *ret_key_data) {
24✔
2573
        _cleanup_free_ char *bindname = NULL;
24✔
2574
        const char *token_type_name;
24✔
2575
        int r;
24✔
2576

2577
        assert(key_file);
24✔
2578
        assert(volume);
24✔
2579
        assert(ret_key_data);
24✔
2580

2581
        bindname = make_bindname(volume, token_type);
24✔
2582
        if (!bindname)
24✔
UNCOV
2583
                return log_oom();
×
2584

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

2590
        token_type_name = token_type_to_string(token_type);
1✔
2591
        if (token_type_name)
1✔
UNCOV
2592
                log_debug("Automatically discovered encrypted key for volume '%s' (token type: %s).", volume, token_type_name);
×
2593
        else
2594
                log_debug("Automatically discovered key for volume '%s'.", volume);
1✔
2595

2596
        return r;
2597
}
2598

2599
VERB(verb_attach, "attach", "VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG]", 3, 5, 0,
2600
     "Attach an encrypted block device");
2601
static int verb_attach(int argc, char *argv[], uintptr_t _data, void *userdata) {
78✔
2602
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
78✔
2603
        _unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL;
78✔
2604
        crypt_status_info status;
78✔
2605
        uint32_t flags = 0;
78✔
2606
        unsigned tries;
78✔
2607
        usec_t until;
78✔
2608
        PassphraseType passphrase_type = PASSPHRASE_NONE;
78✔
2609
        int r;
78✔
2610

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

2613
        assert(argc >= 3 && argc <= 5);
78✔
2614

2615
        const char *volume = ASSERT_PTR(argv[1]),
78✔
2616
                *source = ASSERT_PTR(argv[2]),
78✔
2617
                *key_file = argc >= 4 ? mangle_none(argv[3]) : NULL,
78✔
2618
                *config = argc >= 5 ? mangle_none(argv[4]) : NULL;
78✔
2619

2620
        if (!filename_is_valid(volume))
78✔
UNCOV
2621
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
2622

2623
        if (key_file && !path_is_absolute(key_file)) {
78✔
UNCOV
2624
                log_warning("Password file path '%s' is not absolute. Ignoring.", key_file);
×
2625
                key_file = NULL;
2626
        }
2627

2628
        if (config) {
78✔
2629
                r = parse_crypt_config(config);
76✔
2630
                if (r < 0)
76✔
2631
                        return r;
2632
        }
2633

2634
        log_debug("%s %s ← %s type=%s cipher=%s", __func__,
217✔
2635
                  volume, source, strempty(arg_type), strempty(arg_cipher));
2636

2637
        /* A delicious drop of snake oil */
2638
        (void) safe_mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT);
78✔
2639

2640
        if (key_file && arg_keyfile_erase)
78✔
2641
                destroy_key_file = key_file; /* let's get this baby erased when we leave */
2✔
2642

2643
        if (arg_header) {
78✔
2644
                if (streq_ptr(arg_type, CRYPT_TCRYPT)){
9✔
UNCOV
2645
                        log_debug("tcrypt header: %s", arg_header);
×
UNCOV
2646
                        r = sym_crypt_init_data_device(&cd, arg_header, source);
×
2647
                } else {
2648
                        log_debug("LUKS header: %s", arg_header);
9✔
2649
                        r = sym_crypt_init(&cd, arg_header);
9✔
2650
                }
2651
        } else
2652
                r = sym_crypt_init(&cd, source);
69✔
2653
        if (r < 0)
78✔
UNCOV
2654
                return log_error_errno(r, "crypt_init() failed: %m");
×
2655

2656
        cryptsetup_enable_logging(cd);
78✔
2657

2658
        status = sym_crypt_status(cd, volume);
78✔
2659
        if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
78✔
UNCOV
2660
                log_info("Volume %s already active.", volume);
×
2661
                return 0;
2662
        }
2663

2664
        flags = determine_flags();
78✔
2665

2666
        until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
78✔
UNCOV
2667
        if (until == USEC_INFINITY)
×
2668
                until = 0;
78✔
2669

2670
        if (arg_key_size == 0)
78✔
2671
                arg_key_size = 256U / 8U;
78✔
2672

2673
        if (key_file) {
78✔
2674
                struct stat st;
28✔
2675

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

2682
        if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) {
78✔
2683
                r = sym_crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL);
78✔
2684
                if (r < 0)
78✔
2685
                        return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", sym_crypt_get_device_name(cd));
35✔
2686

2687
                /* since cryptsetup 2.7.0 (Jan 2024) */
2688
                if (arg_link_key_description) {
78✔
UNCOV
2689
                        r = sym_crypt_set_keyring_to_link(cd, arg_link_key_description, NULL, arg_link_key_type, arg_link_keyring);
×
UNCOV
2690
                        if (r == -ENOSYS)
×
UNCOV
2691
                                log_warning("Loaded libcryptsetup does not support linking volume keys in user specified kernel keyrings upon device activation, ignoring.");
×
UNCOV
2692
                        else if (r < 0)
×
UNCOV
2693
                                log_warning_errno(r, "Failed to set keyring or key description to link volume key in, ignoring: %m");
×
2694
                }
2695

2696
                if (arg_header) {
78✔
2697
                        r = sym_crypt_set_data_device(cd, source);
9✔
2698
                        if (r < 0)
9✔
UNCOV
2699
                                return log_error_errno(r, "Failed to set LUKS data device %s: %m", source);
×
2700
                }
2701

2702
                /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */
2703
                if (!key_file && use_token_plugins()) {
78✔
2704
                        r = crypt_activate_by_token_pin_ask_password(
49✔
2705
                                        cd,
2706
                                        volume,
2707
                                        /* type= */ NULL,
2708
                                        until,
2709
                                        /* userdata= */ NULL,
2710
                                        flags,
2711
                                        "Please enter LUKS2 token PIN:",
2712
                                        "luks2-pin",
2713
                                        "cryptsetup.luks2-pin");
2714
                        if (r >= 0) {
49✔
2715
                                log_debug("Volume %s activated with a LUKS token.", volume);
35✔
2716
                                return 0;
2717
                        }
2718

2719
                        log_debug_errno(r, "Token activation unsuccessful for device %s: %m", sym_crypt_get_device_name(cd));
14✔
2720
                }
2721
        }
2722

2723
        if (streq_ptr(arg_type, CRYPT_BITLK)) {
43✔
UNCOV
2724
                r = sym_crypt_load(cd, CRYPT_BITLK, NULL);
×
UNCOV
2725
                if (r < 0)
×
UNCOV
2726
                        return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", sym_crypt_get_device_name(cd));
×
2727
        }
2728

2729
        bool use_cached_passphrase = true, try_discover_key = !key_file;
43✔
2730
        const char *discovered_key_fn = strjoina(volume, ".key");
215✔
2731
        _cleanup_strv_free_erase_ char **passwords = NULL;
43✔
2732
        for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
58✔
2733
                _cleanup_(iovec_done_erase) struct iovec discovered_key_data = {};
58✔
2734
                const struct iovec *key_data = NULL;
58✔
2735
                TokenType token_type = determine_token_type();
58✔
2736

2737
                log_debug("Beginning attempt %u to unlock.", tries);
58✔
2738

2739
                /* When we were able to acquire multiple keys, let's always process them in this order:
2740
                 *
2741
                 *    1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip
2742
                 *    2. The configured or discovered key, of which both are exclusive and optional
2743
                 *    3. The empty password, in case arg_try_empty_password is set
2744
                 *    4. We enquire the user for a password
2745
                 */
2746

2747
                if (try_discover_key) {
58✔
2748
                        r = discover_key(discovered_key_fn, volume, token_type, &discovered_key_data);
24✔
2749
                        if (r < 0)
24✔
2750
                                return r;
2751
                        if (r > 0)
24✔
2752
                                key_data = &discovered_key_data;
1✔
2753
                }
2754

2755
                if (token_type < 0 && !key_file && !key_data && !passwords) {
58✔
2756

2757
                        /* If we have nothing to try anymore, then acquire a new password */
2758

2759
                        if (arg_try_empty_password) {
17✔
2760
                                /* Hmm, let's try an empty password now, but only once */
2761
                                arg_try_empty_password = false;
2✔
2762
                                key_data = &iovec_empty;
2✔
2763
                        } else {
2764
                                /* Ask the user for a passphrase or recovery key only as last resort, if we
2765
                                 * have nothing else to check for */
2766
                                if (passphrase_type == PASSPHRASE_NONE) {
15✔
2767
                                        passphrase_type = check_registered_passwords(cd);
15✔
2768
                                        if (passphrase_type == PASSPHRASE_NONE)
15✔
UNCOV
2769
                                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No passphrase or recovery key registered.");
×
2770
                                }
2771

2772
                                r = get_password(
15✔
2773
                                                volume,
2774
                                                source,
2775
                                                until,
2776
                                                /* ignore_cached= */ !use_cached_passphrase || arg_verify,
15✔
2777
                                                passphrase_type,
2778
                                                &passwords);
2779
                                use_cached_passphrase = false;
15✔
2780
                                if (r == -EAGAIN)
15✔
UNCOV
2781
                                        continue;
×
2782
                                if (r < 0)
15✔
2783
                                        return r;
2784
                        }
2785
                }
2786

2787
                if (streq_ptr(arg_type, CRYPT_TCRYPT))
43✔
UNCOV
2788
                        r = attach_tcrypt(cd, volume, token_type, key_file, key_data, passwords, flags);
×
2789
                else
2790
                        r = attach_luks_or_plain_or_bitlk(cd, volume, token_type, key_file, key_data, passwords, flags, until);
43✔
2791
                if (r >= 0)
43✔
2792
                        break;
2793
                if (r != -EAGAIN)
17✔
2794
                        return r;
2795

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

2799
                if (token_type == TOKEN_TPM2) {
15✔
2800
                        arg_tpm2_device = mfree(arg_tpm2_device);
11✔
2801
                        arg_tpm2_device_auto = false;
11✔
2802
                        continue;
11✔
2803
                }
2804

2805
                if (token_type == TOKEN_FIDO2) {
4✔
UNCOV
2806
                        arg_fido2_device = mfree(arg_fido2_device);
×
UNCOV
2807
                        arg_fido2_device_auto = false;
×
UNCOV
2808
                        continue;
×
2809
                }
2810

2811
                if (token_type == TOKEN_PKCS11) {
4✔
2812
                        arg_pkcs11_uri = mfree(arg_pkcs11_uri);
×
UNCOV
2813
                        arg_pkcs11_uri_auto = false;
×
UNCOV
2814
                        continue;
×
2815
                }
2816

2817
                if (try_discover_key) {
4✔
2818
                        try_discover_key = false;
×
UNCOV
2819
                        continue;
×
2820
                }
2821

2822
                if (key_file) {
4✔
2823
                        key_file = NULL;
4✔
2824
                        continue;
4✔
2825
                }
2826

UNCOV
2827
                if (passwords) {
×
UNCOV
2828
                        passwords = strv_free_erase(passwords);
×
UNCOV
2829
                        continue;
×
2830
                }
2831

2832
                log_debug("Prepared for next attempt to unlock.");
×
2833
        }
2834

2835
        if (arg_tries != 0 && tries >= arg_tries)
26✔
2836
                return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up.");
×
2837

2838
        return 0;
2839
}
2840

2841
VERB(verb_detach, "detach", "VOLUME", 2, 2, 0,
2842
     "Detach an encrypted block device");
2843
static int verb_detach(int argc, char *argv[], uintptr_t _data, void *userdata) {
64✔
2844
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
64✔
2845
        const char *volume = ASSERT_PTR(argv[1]);
64✔
2846
        int r;
64✔
2847

2848
        assert(argc == 2);
64✔
2849

2850
        if (!filename_is_valid(volume))
64✔
UNCOV
2851
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
2852

2853
        r = sym_crypt_init_by_name(&cd, volume);
64✔
2854
        if (r == -ENODEV) {
64✔
2855
                log_info("Volume %s already inactive.", volume);
3✔
2856
                return 0;
2857
        }
2858
        if (r < 0)
61✔
UNCOV
2859
                return log_error_errno(r, "crypt_init_by_name() for volume '%s' failed: %m", volume);
×
2860

2861
        cryptsetup_enable_logging(cd);
61✔
2862

2863
        r = sym_crypt_deactivate(cd, volume);
61✔
2864
        if (r < 0)
61✔
UNCOV
2865
                return log_error_errno(r, "Failed to deactivate '%s': %m", volume);
×
2866

2867
        return 0;
2868
}
2869

2870
static int run(int argc, char *argv[]) {
142✔
2871
        int r;
142✔
2872

2873
        log_setup();
142✔
2874

2875
        umask(0022);
142✔
2876

2877
        char **args = NULL;
142✔
2878
        r = parse_argv(argc, argv, &args);
142✔
2879
        if (r <= 0)
142✔
2880
                return r;
142✔
2881

2882
        r = dlopen_cryptsetup(LOG_ERR);
142✔
2883
        if (r < 0)
142✔
2884
                return r;
2885

2886
        return dispatch_verb_with_args(args, NULL);
142✔
2887
}
2888

2889
DEFINE_MAIN_FUNCTION(run);
142✔
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