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

systemd / systemd / 20669300594

02 Jan 2026 09:00PM UTC coverage: 72.677% (-0.02%) from 72.697%
20669300594

push

github

web-flow
clang-tidy: Enable more warnings (#39910)

25 of 27 new or added lines in 3 files covered. (92.59%)

5655 existing lines in 111 files now uncovered.

310023 of 426578 relevant lines covered (72.68%)

1136999.43 hits per line

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

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

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

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

13
#include "alloc-util.h"
14
#include "argv-util.h"
15
#include "ask-password-api.h"
16
#include "build.h"
17
#include "cryptsetup-fido2.h"
18
#include "cryptsetup-keyfile.h"
19
#include "cryptsetup-pkcs11.h"
20
#include "cryptsetup-tpm2.h"
21
#include "cryptsetup-util.h"
22
#include "efi-api.h"
23
#include "efi-loader.h"
24
#include "efivars.h"
25
#include "env-util.h"
26
#include "errno-util.h"
27
#include "escape.h"
28
#include "extract-word.h"
29
#include "fileio.h"
30
#include "fs-util.h"
31
#include "hexdecoct.h"
32
#include "json-util.h"
33
#include "libfido2-util.h"
34
#include "libmount-util.h"
35
#include "log.h"
36
#include "main-func.h"
37
#include "memory-util.h"
38
#include "nulstr-util.h"
39
#include "parse-util.h"
40
#include "path-util.h"
41
#include "pkcs11-util.h"
42
#include "pretty-print.h"
43
#include "random-util.h"
44
#include "string-table.h"
45
#include "string-util.h"
46
#include "strv.h"
47
#include "time-util.h"
48
#include "tpm2-pcr.h"
49
#include "tpm2-util.h"
50
#include "verbs.h"
51

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

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

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

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

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

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

152
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(passphrase_type, PassphraseType);
×
153

154
static const char* const token_type_table[_TOKEN_TYPE_MAX] = {
155
        [TOKEN_TPM2]   = "tpm2",
156
        [TOKEN_FIDO2]  = "fido2",
157
        [TOKEN_PKCS11] = "pkcs11",
158
};
159

160
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(token_type, TokenType);
45✔
161

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

172
static int parse_one_option(const char *option) {
145✔
173
        const char *val;
145✔
174
        int r;
145✔
175

176
        assert(option);
145✔
177

178
        /* Handled outside of this tool */
179
        if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout"))
145✔
180
                return 0;
1✔
181

182
        if (startswith(option, "keyfile-timeout="))
144✔
183
                return 0;
184

185
        if ((val = startswith(option, "cipher="))) {
144✔
186
                r = free_and_strdup(&arg_cipher, val);
×
187
                if (r < 0)
×
188
                        return log_oom();
×
189

190
        } else if ((val = startswith(option, "size="))) {
144✔
191

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

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

203
                arg_key_size /= 8;
×
204

205
        } else if ((val = startswith(option, "sector-size="))) {
144✔
206

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

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

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

221
        } else if ((val = startswith(option, "key-slot=")) ||
144✔
222
                   (val = startswith(option, "keyslot="))) {
142✔
223

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

229
        } else if ((val = startswith(option, "tcrypt-keyfile="))) {
142✔
230

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

238
        } else if ((val = startswith(option, "keyfile-size="))) {
142✔
239

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

244
        } else if ((val = startswith(option, "keyfile-offset="))) {
139✔
245

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

250
        } else if ((val = startswith(option, "keyfile-erase="))) {
134✔
251

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

258
                arg_keyfile_erase = r;
2✔
259

260
        } else if (streq(option, "keyfile-erase"))
132✔
261
                arg_keyfile_erase = true;
×
262

263
        else if ((val = startswith(option, "hash="))) {
132✔
264
                r = free_and_strdup(&arg_hash, val);
×
265
                if (r < 0)
×
266
                        return log_oom();
×
267

268
        } else if ((val = startswith(option, "header="))) {
132✔
269
                if (!arg_type || !STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT))
9✔
270
                        arg_type = ANY_LUKS;
7✔
271

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

276
                if (arg_header)
9✔
277
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
278
                                               "Duplicate header= option, refusing.");
279

280
                arg_header = strdup(val);
9✔
281
                if (!arg_header)
9✔
282
                        return log_oom();
×
283

284
        } else if ((val = startswith(option, "tries="))) {
123✔
285

286
                r = safe_atou(val, &arg_tries);
×
287
                if (r < 0)
×
288
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
145✔
289

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

304
                        SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_ECHO, r);
×
305
                        SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_SILENT, !r);
×
306
                }
307
        } else if ((val = startswith(option, "password-cache="))) {
123✔
308
                arg_password_cache_set = true;
×
309

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

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

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

359
                r = parse_sec_fix_0(val, &arg_timeout);
×
UNCOV
360
                if (r < 0)
×
UNCOV
361
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
362

363
        } else if ((val = startswith(option, "offset="))) {
115✔
364

UNCOV
365
                r = safe_atou64(val, &arg_offset);
×
UNCOV
366
                if (r < 0)
×
UNCOV
367
                        return log_error_errno(r, "Failed to parse %s: %m", option);
×
368

369
        } else if ((val = startswith(option, "skip="))) {
115✔
370

UNCOV
371
                r = safe_atou64(val, &arg_skip);
×
UNCOV
372
                if (r < 0)
×
UNCOV
373
                        return log_error_errno(r, "Failed to parse %s: %m", option);
×
374

375
        } else if ((val = startswith(option, "pkcs11-uri="))) {
115✔
376

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

384
                        r = free_and_strdup(&arg_pkcs11_uri, val);
×
385
                        if (r < 0)
×
UNCOV
386
                                return log_oom();
×
387

388
                        arg_pkcs11_uri_auto = false;
×
389
                }
390

391
        } else if ((val = startswith(option, "fido2-device="))) {
109✔
392

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

401
                        arg_fido2_device_auto = false;
×
402
                }
403

404
        } else if ((val = startswith(option, "fido2-cid="))) {
109✔
405

UNCOV
406
                if (streq(val, "auto"))
×
UNCOV
407
                        arg_fido2_cid = mfree(arg_fido2_cid);
×
408
                else {
409
                        _cleanup_free_ void *cid = NULL;
×
410
                        size_t cid_size;
×
411

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

416
                        free(arg_fido2_cid);
×
417
                        arg_fido2_cid = TAKE_PTR(cid);
×
UNCOV
418
                        arg_fido2_cid_size = cid_size;
×
419
                }
420

421
                /* Turn on FIDO2 as side-effect, if not turned on yet. */
UNCOV
422
                if (!arg_fido2_device && !arg_fido2_device_auto)
×
UNCOV
423
                        arg_fido2_device_auto = true;
×
424

425
        } else if ((val = startswith(option, "fido2-rp="))) {
109✔
426

UNCOV
427
                r = free_and_strdup(&arg_fido2_rp_id, val);
×
UNCOV
428
                if (r < 0)
×
UNCOV
429
                        return log_oom();
×
430

431
        } else if ((val = startswith(option, "fido2-pin="))) {
109✔
432

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

439
                arg_fido2_manual_flags &= ~FIDO2ENROLL_PIN_IF_NEEDED;
×
UNCOV
440
                SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_PIN, r);
×
441

442
        } else if ((val = startswith(option, "fido2-up="))) {
109✔
443

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

450
                arg_fido2_manual_flags &= ~FIDO2ENROLL_UP_IF_NEEDED;
×
UNCOV
451
                SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_UP, r);
×
452

453
        } else if ((val = startswith(option, "fido2-uv="))) {
109✔
454

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

461
                arg_fido2_manual_flags &= ~FIDO2ENROLL_UV_OMIT;
×
UNCOV
462
                SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_UV, r);
×
463

464
        } else if ((val = startswith(option, "tpm2-device="))) {
109✔
465

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

474
                        arg_tpm2_device_auto = false;
×
475
                }
476

477
        } else if ((val = startswith(option, "tpm2-pcrs="))) {
70✔
478

UNCOV
479
                r = tpm2_parse_pcr_argument_to_mask(val, &arg_tpm2_pcr_mask);
×
UNCOV
480
                if (r < 0)
×
481
                        return r;
482

483
        } else if ((val = startswith(option, "tpm2-signature="))) {
70✔
484

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

489
                r = free_and_strdup(&arg_tpm2_signature, val);
1✔
490
                if (r < 0)
1✔
UNCOV
491
                        return log_oom();
×
492

493
        } else if ((val = startswith(option, "tpm2-pin="))) {
69✔
494

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

501
                arg_tpm2_pin = r;
×
502

503
        } else if ((val = startswith(option, "tpm2-pcrlock="))) {
69✔
504

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

509
                r = free_and_strdup(&arg_tpm2_pcrlock, val);
5✔
510
                if (r < 0)
5✔
UNCOV
511
                        return log_oom();
×
512

513
        } else if ((val = startswith(option, "tpm2-measure-pcr="))) {
64✔
514
                unsigned pcr;
×
515

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

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

530
                arg_tpm2_measure_pcr = pcr;
×
531

532
        } else if ((val = startswith(option, "tpm2-measure-bank="))) {
64✔
533

534
#if HAVE_OPENSSL
UNCOV
535
                _cleanup_strv_free_ char **l = NULL;
×
536

UNCOV
537
                l = strv_split(val, ":");
×
538
                if (!l)
×
UNCOV
539
                        return log_oom();
×
540

541
                STRV_FOREACH(i, l) {
×
542
                        const EVP_MD *implementation;
×
543

544
                        implementation = EVP_get_digestbyname(*i);
×
545
                        if (!implementation)
×
UNCOV
546
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown bank '%s', refusing.", val);
×
547

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

555
        } else if ((val = startswith(option, "tpm2-measure-keyslot-nvpcr="))) {
64✔
556

UNCOV
557
                if (isempty(val)) {
×
UNCOV
558
                        arg_tpm2_measure_keyslot_nvpcr = mfree(arg_tpm2_measure_keyslot_nvpcr);
×
UNCOV
559
                        return 0;
×
560
                }
561

562
                if (!tpm2_nvpcr_name_is_valid(val)) {
×
UNCOV
563
                        log_warning("Invalid NvPCR name, ignoring: %s", option);
×
UNCOV
564
                        return 0;
×
565
                }
566

567
                if (free_and_strdup(&arg_tpm2_measure_keyslot_nvpcr, val) < 0)
×
UNCOV
568
                        return log_oom();
×
569

570
        } else if ((val = startswith(option, "try-empty-password="))) {
64✔
571

572
                r = parse_boolean(val);
1✔
573
                if (r < 0) {
1✔
UNCOV
574
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
UNCOV
575
                        return 0;
×
576
                }
577

578
                arg_try_empty_password = r;
1✔
579

580
        } else if (streq(option, "try-empty-password"))
63✔
581
                arg_try_empty_password = true;
1✔
582
        else if ((val = startswith(option, "headless="))) {
62✔
583

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

590
                SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_HEADLESS, r);
55✔
591
        } else if (streq(option, "headless"))
7✔
592
                arg_ask_password_flags |= ASK_PASSWORD_HEADLESS;
7✔
593

UNCOV
594
        else if ((val = startswith(option, "token-timeout="))) {
×
595

UNCOV
596
                r = parse_sec_fix_0(val, &arg_token_timeout_usec);
×
597
                if (r < 0)
×
598
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
115✔
599

600
        } else if ((val = startswith(option, "link-volume-key="))) {
×
601
#if HAVE_CRYPT_SET_KEYRING_TO_LINK
UNCOV
602
                _cleanup_free_ char *keyring = NULL, *key_type = NULL, *key_description = NULL;
×
603
                const char *sep;
×
604

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

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

622
                /* add type prefix if missing (crypt_set_keyring_to_link() expects it) */
623
                if (!IN_SET(*keyring, '@', '%'))
×
UNCOV
624
                        if (!strprepend(&keyring, "%:"))
×
UNCOV
625
                                return log_oom();
×
626

627
                sep += 2;
×
628

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

636
                        key_type = strndup(sep + 1, c - sep - 1);
×
637
                        if (!key_type)
×
UNCOV
638
                                return log_oom();
×
639

640
                        sep = c + 1;
×
641
                }
642

643
                key_description = strdup(sep);
×
UNCOV
644
                if (!key_description)
×
UNCOV
645
                        return log_oom();
×
646

647
                free_and_replace(arg_link_keyring, keyring);
×
648
                free_and_replace(arg_link_key_type, key_type);
×
UNCOV
649
                free_and_replace(arg_link_key_description, key_description);
×
650
#else
651
                log_error("Build lacks libcryptsetup support for linking volume keys in user specified kernel keyrings upon device activation, ignoring: %s", option);
652
#endif
UNCOV
653
        } else if (!streq(option, "x-initrd.attach"))
×
UNCOV
654
                log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
×
655

656
        return 0;
657
}
658

659
static int parse_crypt_config(const char *options) {
70✔
660
        assert(options);
70✔
661

662
        for (;;) {
360✔
663
                _cleanup_free_ char *word = NULL;
145✔
664
                int r;
215✔
665

666
                r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
215✔
667
                if (r < 0)
215✔
UNCOV
668
                        return log_error_errno(r, "Failed to parse options: %m");
×
669
                if (r == 0)
215✔
670
                        break;
671

672
                r = parse_one_option(word);
145✔
673
                if (r < 0)
145✔
674
                        return r;
675
        }
676

677
        /* sanity-check options */
678
        if (arg_type && !streq(arg_type, CRYPT_PLAIN)) {
70✔
679
                if (arg_offset != 0)
17✔
UNCOV
680
                      log_warning("offset= ignored with type %s", arg_type);
×
681
                if (arg_skip != 0)
17✔
UNCOV
682
                      log_warning("skip= ignored with type %s", arg_type);
×
683
        }
684

685
        if (arg_pkcs11_uri || arg_pkcs11_uri_auto) {
70✔
686
                /* If password-cache was not configured explicitly, default to no cache for PKCS#11 */
687
                if (!arg_password_cache_set)
6✔
688
                        arg_ask_password_flags &= ~(ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE);
6✔
689

690
                /* This prevents future backward-compatibility issues if we decide to allow caching for PKCS#11 */
691
                if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_ACCEPT_CACHED))
6✔
UNCOV
692
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
693
                                               "Password cache is not supported for PKCS#11 security tokens.");
694
        }
695

696
        return 0;
697
}
698

699
static char* disk_description(const char *path) {
10✔
700
        static const char name_fields[] =
10✔
701
                "DM_NAME\0"
702
                "ID_MODEL_FROM_DATABASE\0"
703
                "ID_MODEL\0";
704

705
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
10✔
706
        const char *name;
10✔
707
        struct stat st;
10✔
708

709
        assert(path);
10✔
710

711
        if (stat(path, &st) < 0)
10✔
712
                return NULL;
713

714
        if (!S_ISBLK(st.st_mode))
10✔
715
                return NULL;
716

UNCOV
717
        if (sd_device_new_from_stat_rdev(&device, &st) < 0)
×
718
                return NULL;
719

720
        if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) {
×
UNCOV
721
                _cleanup_free_ char *unescaped = NULL;
×
UNCOV
722
                ssize_t l;
×
723

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

UNCOV
728
                l = cunescape(name, UNESCAPE_RELAX, &unescaped);
×
UNCOV
729
                if (l < 0) {
×
UNCOV
730
                        log_debug_errno(l, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
×
731
                        return NULL;
×
732
                }
733

734
                if (!isempty(unescaped) && !string_has_cc(unescaped, NULL))
×
UNCOV
735
                        return TAKE_PTR(unescaped);
×
736
        }
737

738
        /* These need no unescaping. */
UNCOV
739
        NULSTR_FOREACH(i, name_fields)
×
UNCOV
740
                if (sd_device_get_property_value(device, i, &name) >= 0 &&
×
UNCOV
741
                    !isempty(name))
×
742
                        return strdup(name);
×
743

744
        return NULL;
745
}
746

747
static char* disk_mount_point(const char *label) {
10✔
748
        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
10✔
UNCOV
749
        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
×
750
        _cleanup_free_ char *device = NULL;
10✔
751
        int r;
10✔
752

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

755
        assert(label);
10✔
756

757
        device = strjoin("/dev/mapper/", label);
10✔
758
        if (!device)
10✔
759
                return NULL;
760

761
        r = libmount_parse_fstab(&table, &iter);
10✔
762
        if (r < 0)
10✔
763
                return NULL;
764

UNCOV
765
        for (;;) {
×
766
                struct libmnt_fs *fs;
10✔
767

768
                r = sym_mnt_table_next_fs(table, iter, &fs);
10✔
769
                if (r != 0)
10✔
770
                        return NULL;
10✔
771

UNCOV
772
                if (path_equal(sym_mnt_fs_get_source(fs), device)) {
×
UNCOV
773
                        const char *target = sym_mnt_fs_get_target(fs);
×
UNCOV
774
                        if (target)
×
775
                                return strdup(target);
×
776
                }
777
        }
778
}
779

780
static char *friendly_disk_name(const char *src, const char *vol) {
10✔
781
        _cleanup_free_ char *description = NULL, *mount_point = NULL;
10✔
782
        char *name_buffer = NULL;
10✔
783
        int r;
10✔
784

785
        assert(src);
10✔
786
        assert(vol);
10✔
787

788
        description = disk_description(src);
10✔
789
        mount_point = disk_mount_point(vol);
10✔
790

791
        /* If the description string is simply the volume name, then let's not show this twice */
792
        if (description && streq(vol, description))
10✔
UNCOV
793
                description = mfree(description);
×
794

795
        if (mount_point && description)
10✔
796
                r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point);
×
UNCOV
797
        else if (mount_point)
×
UNCOV
798
                r = asprintf(&name_buffer, "%s on %s", vol, mount_point);
×
799
        else if (description)
10✔
800
                r = asprintf(&name_buffer, "%s (%s)", description, vol);
×
801
        else
802
                return strdup(vol);
10✔
803
        if (r < 0)
×
804
                return NULL;
805

806
        return name_buffer;
×
807
}
808

809
static PassphraseType check_registered_passwords(struct crypt_device *cd) {
14✔
810
        _cleanup_free_ bool *slots = NULL;
14✔
811
        int slot_max;
14✔
812
        PassphraseType passphrase_type = PASSPHRASE_NONE;
14✔
813

814
        assert(cd);
14✔
815

816
        if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2)) {
14✔
UNCOV
817
                log_debug("%s: not a LUKS2 device, only passphrases are supported", crypt_get_device_name(cd));
×
UNCOV
818
                return PASSPHRASE_REGULAR;
×
819
        }
820

821
        /* Search all used slots */
822
        assert_se((slot_max = crypt_keyslot_max(CRYPT_LUKS2)) > 0);
14✔
823
        slots = new(bool, slot_max);
14✔
824
        if (!slots)
14✔
UNCOV
825
                return log_oom();
×
826

827
        for (int slot = 0; slot < slot_max; slot++)
462✔
828
                slots[slot] = IN_SET(crypt_keyslot_status(cd, slot), CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST);
448✔
829

830
        /* Iterate all LUKS2 tokens and keep track of all their slots */
831
        for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
462✔
832
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
448✔
833
                const char *type;
448✔
834
                sd_json_variant *w, *z;
448✔
835
                int tk;
448✔
836

837
                tk = cryptsetup_get_token_as_json(cd, token, NULL, &v);
448✔
838
                if (IN_SET(tk, -ENOENT, -EINVAL))
448✔
839
                        continue;
439✔
840
                if (tk < 0) {
9✔
UNCOV
841
                        log_warning_errno(tk, "Failed to read JSON token data, ignoring: %m");
×
UNCOV
842
                        continue;
×
843
                }
844

845
                w = sd_json_variant_by_key(v, "type");
9✔
846
                if (!w || !sd_json_variant_is_string(w)) {
9✔
UNCOV
847
                        log_warning("Token JSON data lacks type field, ignoring.");
×
UNCOV
848
                        continue;
×
849
                }
850

851
                type = sd_json_variant_string(w);
9✔
852
                if (STR_IN_SET(type, "systemd-recovery", "systemd-pkcs11", "systemd-fido2", "systemd-tpm2")) {
9✔
853

854
                        /* At least exists one recovery key */
855
                        if (streq(type, "systemd-recovery"))
9✔
UNCOV
856
                                passphrase_type |= PASSPHRASE_RECOVERY_KEY;
×
857

858
                        w = sd_json_variant_by_key(v, "keyslots");
9✔
859
                        if (!w || !sd_json_variant_is_array(w)) {
9✔
UNCOV
860
                                log_warning("Token JSON data lacks keyslots field, ignoring.");
×
UNCOV
861
                                continue;
×
862
                        }
863

864
                        JSON_VARIANT_ARRAY_FOREACH(z, w) {
18✔
865
                                unsigned u;
9✔
866
                                int at;
9✔
867

868
                                if (!sd_json_variant_is_string(z)) {
9✔
UNCOV
869
                                        log_warning("Token JSON data's keyslot field is not an array of strings, ignoring.");
×
UNCOV
870
                                        continue;
×
871
                                }
872

873
                                at = safe_atou(sd_json_variant_string(z), &u);
9✔
874
                                if (at < 0) {
9✔
UNCOV
875
                                        log_warning_errno(at, "Token JSON data's keyslot field is not an integer formatted as string, ignoring.");
×
UNCOV
876
                                        continue;
×
877
                                }
878

879
                                if (u >= (unsigned) slot_max) {
9✔
UNCOV
880
                                        log_warning_errno(at, "Token JSON data's keyslot field exceeds the maximum value allowed, ignoring.");
×
UNCOV
881
                                        continue;
×
882
                                }
883

884
                                slots[u] = false;
9✔
885
                        }
886
                }
887
        }
888

889
        /* Check if any of the slots is not referenced by systemd tokens */
890
        for (int slot = 0; slot < slot_max; slot++)
14✔
891
                if (slots[slot]) {
14✔
892
                        passphrase_type |= PASSPHRASE_REGULAR;
14✔
893
                        break;
14✔
894
                }
895

896
        /* All the slots are referenced by systemd tokens, so if a recovery key is not enrolled,
897
         * we will not be able to enter a passphrase. */
898
        return passphrase_type;
899
}
900

901
static int get_password(
14✔
902
                const char *vol,
903
                const char *src,
904
                usec_t until,
905
                bool ignore_cached,
906
                PassphraseType passphrase_type,
907
                char ***ret) {
908

909
        _cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL, *id = NULL;
14✔
UNCOV
910
        _cleanup_strv_free_erase_ char **passwords = NULL;
×
911
        AskPasswordFlags flags = arg_ask_password_flags;
14✔
912
        int r;
14✔
913

914
        assert(vol);
14✔
915
        assert(src);
14✔
916
        assert(ret);
14✔
917

918
        if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_HEADLESS))
14✔
919
                return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Password querying disabled via 'headless' option.");
14✔
920

UNCOV
921
        friendly = friendly_disk_name(src, vol);
×
UNCOV
922
        if (!friendly)
×
UNCOV
923
                return log_oom();
×
924

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

928
        disk_path = cescape(src);
×
929
        if (!disk_path)
×
UNCOV
930
                return log_oom();
×
931

932
        id = strjoin("cryptsetup:", disk_path);
×
933
        if (!id)
×
UNCOV
934
                return log_oom();
×
935

936
        AskPasswordRequest req = {
×
937
                .tty_fd = -EBADF,
938
                .message = text,
939
                .icon = "drive-harddisk",
940
                .id = id,
941
                .keyring = "cryptsetup",
942
                .credential = "cryptsetup.passphrase",
943
                .until = until,
944
                .hup_fd = -EBADF,
945
        };
946

UNCOV
947
        if (ignore_cached)
×
UNCOV
948
                flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
×
949

950
        r = ask_password_auto(&req, flags, &passwords);
×
951
        if (r < 0)
×
UNCOV
952
                return log_error_errno(r, "Failed to query password: %m");
×
953

954
        if (arg_verify) {
×
955
                _cleanup_strv_free_erase_ char **passwords2 = NULL;
×
956

957
                assert(strv_length(passwords) == 1);
×
958

UNCOV
959
                text = mfree(text);
×
960
                if (asprintf(&text, "Please enter %s for disk %s (verification):", passphrase_type_to_string(passphrase_type), friendly) < 0)
×
UNCOV
961
                        return log_oom();
×
962

963
                free(id);
×
964
                id = strjoin("cryptsetup-verification:", disk_path);
×
UNCOV
965
                if (!id)
×
966
                        return log_oom();
×
967

968
                req.message = text;
×
969
                req.id = id;
×
970

971
                r = ask_password_auto(&req, flags, &passwords2);
×
972
                if (r < 0)
×
UNCOV
973
                        return log_error_errno(r, "Failed to query verification password: %m");
×
974

975
                assert(strv_length(passwords2) == 1);
×
976

UNCOV
977
                if (!streq(passwords[0], passwords2[0]))
×
978
                        return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN),
×
979
                                                 "Passwords did not match, retrying.");
980
        }
981

UNCOV
982
        strv_uniq(passwords);
×
983

UNCOV
984
        STRV_FOREACH(p, passwords) {
×
985
                char *c;
×
986

987
                if (strlen(*p)+1 >= arg_key_size)
×
988
                        continue;
×
989

990
                /* Pad password if necessary */
991
                c = new(char, arg_key_size);
×
UNCOV
992
                if (!c)
×
993
                        return log_oom();
14✔
994

995
                strncpy(c, *p, arg_key_size);
×
UNCOV
996
                erase_and_free(*p);
×
UNCOV
997
                *p = TAKE_PTR(c);
×
998
        }
999

1000
        *ret = TAKE_PTR(passwords);
×
1001

UNCOV
1002
        return 0;
×
1003
}
1004

1005
static int measure_volume_key(
×
1006
                struct crypt_device *cd,
1007
                const char *name,
1008
                const void *volume_key,
1009
                size_t volume_key_size) {
1010

UNCOV
1011
        int r;
×
1012

UNCOV
1013
        assert(cd);
×
1014
        assert(name);
×
UNCOV
1015
        assert(volume_key);
×
1016
        assert(volume_key_size > 0);
×
1017

1018
        if (arg_tpm2_measure_pcr == UINT_MAX) {
×
1019
                log_debug("Not measuring volume key, deactivated.");
×
UNCOV
1020
                return 0;
×
1021
        }
1022

1023
        r = efi_measured_uki(LOG_WARNING);
×
UNCOV
1024
        if (r < 0)
×
1025
                return r;
1026
        if (r == 0) {
×
1027
                log_debug("Kernel stub did not measure kernel image into the expected PCR, skipping userspace volume key measurement, too.");
×
UNCOV
1028
                return 0;
×
1029
        }
1030

1031
#if HAVE_TPM2
UNCOV
1032
        _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL;
×
UNCOV
1033
        r = tpm2_context_new_or_warn(arg_tpm2_device, &c);
×
UNCOV
1034
        if (r < 0)
×
1035
                return r;
1036

1037
        _cleanup_strv_free_ char **l = NULL;
×
UNCOV
1038
        if (strv_isempty(arg_tpm2_measure_banks)) {
×
UNCOV
1039
                r = tpm2_get_good_pcr_banks_strv(c, UINT32_C(1) << arg_tpm2_measure_pcr, &l);
×
1040
                if (r < 0)
×
1041
                        return log_error_errno(r, "Could not verify pcr banks: %m");
×
1042
        }
1043

1044
        _cleanup_free_ char *joined = strv_join(l ?: arg_tpm2_measure_banks, ", ");
×
UNCOV
1045
        if (!joined)
×
UNCOV
1046
                return log_oom();
×
1047

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

UNCOV
1052
        _cleanup_free_ char *escaped = NULL;
×
UNCOV
1053
        escaped = xescape(name, ":"); /* avoid ambiguity around ":" once we join things below */
×
UNCOV
1054
        if (!escaped)
×
1055
                return log_oom();
×
1056

1057
        _cleanup_free_ char *s = NULL;
×
1058
        s = strjoin("cryptsetup:", escaped, ":", strempty(crypt_get_uuid(cd)));
×
UNCOV
1059
        if (!s)
×
1060
                return log_oom();
×
1061

1062
        r = tpm2_pcr_extend_bytes(c, l ?: arg_tpm2_measure_banks, arg_tpm2_measure_pcr, &IOVEC_MAKE_STRING(s), &IOVEC_MAKE(volume_key, volume_key_size), TPM2_EVENT_VOLUME_KEY, s);
×
1063
        if (r < 0)
×
UNCOV
1064
                return log_error_errno(r, "Could not extend PCR: %m");
×
1065

1066
        log_struct(LOG_INFO,
×
1067
                   LOG_MESSAGE_ID(SD_MESSAGE_TPM_PCR_EXTEND_STR),
1068
                   LOG_MESSAGE("Successfully extended PCR index %u with '%s' and volume key (banks %s).", arg_tpm2_measure_pcr, s, joined),
1069
                   LOG_ITEM("MEASURING=%s", s),
1070
                   LOG_ITEM("PCR=%u", arg_tpm2_measure_pcr),
1071
                   LOG_ITEM("BANKS=%s", joined));
1072

1073
        return 0;
1074
#else
1075
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring volume key.");
1076
#endif
1077
}
1078

1079
static int measure_keyslot(
21✔
1080
                struct crypt_device *cd,
1081
                const char *name,
1082
                const char *mechanism,
1083
                int keyslot) {
1084

1085
        int r;
21✔
1086

1087
        assert(cd);
21✔
1088
        assert(name);
21✔
1089

1090
        if (!arg_tpm2_measure_keyslot_nvpcr) {
21✔
1091
                log_debug("Not measuring unlock keyslot, deactivated.");
21✔
1092
                return 0;
21✔
1093
        }
1094

UNCOV
1095
        r = efi_measured_uki(LOG_WARNING);
×
UNCOV
1096
        if (r < 0)
×
1097
                return r;
1098
        if (r == 0) {
×
1099
                log_debug("Kernel stub did not measure kernel image into the expected PCR, skipping userspace key slot measurement, too.");
×
UNCOV
1100
                return 0;
×
1101
        }
1102

1103
#if HAVE_TPM2
UNCOV
1104
        _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL;
×
UNCOV
1105
        r = tpm2_context_new_or_warn(arg_tpm2_device, &c);
×
UNCOV
1106
        if (r < 0)
×
1107
                return r;
1108

1109
        _cleanup_free_ char *escaped = NULL;
×
UNCOV
1110
        escaped = xescape(name, ":"); /* avoid ambiguity around ":" once we join things below */
×
UNCOV
1111
        if (!escaped)
×
1112
                return log_oom();
×
1113

1114
        _cleanup_free_ char *k = NULL;
×
1115
        if (keyslot >= 0 && asprintf(&k, "%i", keyslot) < 0)
×
UNCOV
1116
                return log_oom();
×
1117

1118
        _cleanup_free_ char *s = NULL;
×
1119
        s = strjoin("cryptsetup-keyslot:", escaped, ":", strempty(crypt_get_uuid(cd)), ":", strempty(mechanism), ":", strempty(k));
×
UNCOV
1120
        if (!s)
×
1121
                return log_oom();
×
1122

1123
        r = tpm2_nvpcr_extend_bytes(c, /* session= */ NULL, arg_tpm2_measure_keyslot_nvpcr, &IOVEC_MAKE_STRING(s), /* secret= */ NULL, TPM2_EVENT_KEYSLOT, s);
×
1124
        if (r == -ENETDOWN) {
×
1125
                /* NvPCR is not initialized yet. Do so now. */
1126
                _cleanup_(iovec_done_erase) struct iovec anchor_secret = {};
×
1127
                r = tpm2_nvpcr_acquire_anchor_secret(&anchor_secret, /* sync_secondary= */ false);
×
UNCOV
1128
                if (r < 0)
×
1129
                        return r;
1130

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

1135
                r = tpm2_nvpcr_extend_bytes(c, /* session= */ NULL, arg_tpm2_measure_keyslot_nvpcr, &IOVEC_MAKE_STRING(s), /* secret= */ NULL, TPM2_EVENT_KEYSLOT, s);
×
1136
        }
UNCOV
1137
        if (r < 0)
×
1138
                return log_error_errno(r, "Could not extend NvPCR: %m");
×
1139

1140
        log_struct(LOG_INFO,
×
1141
                   "MESSAGE_ID=" SD_MESSAGE_TPM_NVPCR_EXTEND_STR,
1142
                   LOG_MESSAGE("Successfully extended NvPCR index '%s' with '%s'.", arg_tpm2_measure_keyslot_nvpcr, s),
1143
                   "MEASURING=%s", s,
1144
                   "NVPCR=%s", arg_tpm2_measure_keyslot_nvpcr);
1145

1146
        return 0;
1147
#else
1148
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring keyslot.");
1149
#endif
1150
}
1151

UNCOV
1152
static int log_external_activation(int r, const char *volume) {
×
UNCOV
1153
        assert(volume);
×
1154

1155
        log_notice_errno(r, "Volume '%s' has been activated externally while we have been trying to activate it.", volume);
×
1156
        return 0;
×
1157
}
1158

1159
static int measured_crypt_activate_by_volume_key(
×
1160
                struct crypt_device *cd,
1161
                const char *name,
1162
                const char *mechanism,
1163
                int keyslot,
1164
                const void *volume_key,
1165
                size_t volume_key_size,
1166
                uint32_t flags) {
1167

UNCOV
1168
        int r;
×
1169

UNCOV
1170
        assert(cd);
×
1171
        assert(name);
×
1172

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

UNCOV
1175
        r = crypt_activate_by_volume_key(cd, name, volume_key, volume_key_size, flags);
×
UNCOV
1176
        if (r == -EEXIST) /* volume is already active */
×
UNCOV
1177
                return log_external_activation(r, name);
×
1178
        if (r < 0)
×
1179
                return r;
1180

1181
        if (volume_key_size > 0)
×
UNCOV
1182
                (void) measure_volume_key(cd, name, volume_key, volume_key_size); /* OK if fails */
×
1183
        else
1184
                log_debug("Not measuring volume key, none specified.");
×
1185

UNCOV
1186
        (void) measure_keyslot(cd, name, mechanism, keyslot); /* ditto */
×
1187
        return r;
×
1188
}
1189

1190
static int measured_crypt_activate_by_passphrase(
24✔
1191
                struct crypt_device *cd,
1192
                const char *name,
1193
                const char *mechanism,
1194
                int keyslot,
1195
                const char *passphrase,
1196
                size_t passphrase_size,
1197
                uint32_t flags) {
1198

1199
        _cleanup_(erase_and_freep) void *vk = NULL;
24✔
1200
        size_t vks;
24✔
1201
        int r;
24✔
1202

1203
        assert(cd);
24✔
1204

1205
        /* A wrapper around crypt_activate_by_passphrase() which also measures to a PCR if that's
1206
         * requested. Note that we need the volume key for the measurement, and
1207
         * crypt_activate_by_passphrase() doesn't give us access to this. Hence, we operate indirectly, and
1208
         * retrieve the volume key first, and then activate through that. */
1209

1210
        if (arg_tpm2_measure_pcr == UINT_MAX) {
24✔
1211
                log_debug("Not measuring volume key, deactivated.");
24✔
1212
                goto shortcut;
24✔
1213
        }
1214

UNCOV
1215
        r = crypt_get_volume_key_size(cd);
×
UNCOV
1216
        if (r < 0)
×
1217
                return r;
1218
        if (r == 0) {
×
1219
                log_debug("Not measuring volume key, none defined.");
×
UNCOV
1220
                goto shortcut;
×
1221
        }
1222

1223
        vk = malloc(vks = r);
×
UNCOV
1224
        if (!vk)
×
1225
                return -ENOMEM;
1226

1227
        keyslot = crypt_volume_key_get(cd, keyslot, vk, &vks, passphrase, passphrase_size);
×
UNCOV
1228
        if (keyslot < 0)
×
1229
                return keyslot;
1230

1231
        return measured_crypt_activate_by_volume_key(cd, name, mechanism, keyslot, vk, vks, flags);
×
1232

1233
shortcut:
24✔
1234
        keyslot = crypt_activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags);
24✔
1235
        if (keyslot == -EEXIST) /* volume is already active */
24✔
UNCOV
1236
                return log_external_activation(keyslot, name);
×
1237
        if (keyslot < 0)
24✔
1238
                return keyslot;
1239

1240
        (void) measure_keyslot(cd, name, mechanism, keyslot);
21✔
1241
        return keyslot;
1242
}
1243

UNCOV
1244
static int attach_tcrypt(
×
1245
                struct crypt_device *cd,
1246
                const char *name,
1247
                TokenType token_type,
1248
                const char *key_file,
1249
                const struct iovec *key_data,
1250
                char **passwords,
1251
                uint32_t flags) {
1252

UNCOV
1253
        int r = 0;
×
UNCOV
1254
        _cleanup_(erase_and_freep) char *passphrase = NULL;
×
UNCOV
1255
        struct crypt_params_tcrypt params = {
×
1256
                .flags = CRYPT_TCRYPT_LEGACY_MODES,
1257
                .keyfiles = (const char **)arg_tcrypt_keyfiles,
1258
                .keyfiles_count = strv_length(arg_tcrypt_keyfiles)
×
1259
        };
1260

1261
        assert(cd);
×
UNCOV
1262
        assert(name);
×
UNCOV
1263
        assert(key_file || key_data || !strv_isempty(passwords));
×
1264

1265
        if (token_type >= 0)
×
1266
                /* Ask for a regular password */
UNCOV
1267
                return log_error_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1268
                                       "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11/fido2/tpm2 support.");
1269

1270
        if (arg_tcrypt_hidden)
×
UNCOV
1271
                params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
×
1272

1273
        if (arg_tcrypt_system)
×
1274
                params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER;
×
1275

1276
        if (arg_tcrypt_veracrypt)
×
1277
                params.flags |= CRYPT_TCRYPT_VERA_MODES;
×
1278

1279
        if (arg_tcrypt_veracrypt && arg_tcrypt_veracrypt_pim != 0)
×
1280
                params.veracrypt_pim = arg_tcrypt_veracrypt_pim;
×
1281

1282
        if (key_data) {
×
1283
                params.passphrase = key_data->iov_base;
×
UNCOV
1284
                params.passphrase_size = key_data->iov_len;
×
1285
                r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
1286
        } else if (key_file) {
×
1287
                r = read_one_line_file(key_file, &passphrase);
×
1288
                if (r < 0) {
×
1289
                        log_error_errno(r, "Failed to read password file '%s': %m", key_file);
×
1290
                        return -EAGAIN; /* log with the actual error, but return EAGAIN */
×
1291
                }
1292
                params.passphrase = passphrase;
×
1293
                params.passphrase_size = strlen(passphrase);
×
UNCOV
1294
                r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
1295
        } else {
1296
                r = -EINVAL;
1297
                STRV_FOREACH(p, passwords){
×
UNCOV
1298
                        params.passphrase = *p;
×
UNCOV
1299
                        params.passphrase_size = strlen(*p);
×
1300
                        r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
1301
                        if (r >= 0)
×
1302
                                break;
1303
                }
1304
        }
1305

UNCOV
1306
        if (r < 0) {
×
UNCOV
1307
                if (r == -EPERM) {
×
UNCOV
1308
                        if (key_data)
×
1309
                                log_error_errno(r, "Failed to activate using discovered key. (Key not correct?)");
×
1310
                        else if (key_file)
×
1311
                                log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file);
×
1312
                        else
1313
                                log_error_errno(r, "Failed to activate using supplied passwords.");
×
1314

UNCOV
1315
                        return r;
×
1316
                }
1317

1318
                return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd));
×
1319
        }
1320

1321
        r = measured_crypt_activate_by_volume_key(
×
1322
                        cd,
1323
                        name,
1324
                        /* mechanism= */ NULL,
1325
                        /* keyslot= */ -1,
1326
                        /* volume_key= */ NULL,
1327
                        /* volume_key_size= */ 0,
1328
                        flags);
UNCOV
1329
        if (r < 0)
×
UNCOV
1330
                return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd));
×
1331

1332
        return 0;
1333
}
1334

1335
static char *make_bindname(const char *volume, TokenType token_type) {
44✔
1336
        const char *token_type_name = token_type_to_string(token_type), *suffix;
44✔
1337
        char *bindname;
44✔
1338
        int r;
44✔
1339

1340
        switch (token_type) {
44✔
1341

1342
        case TOKEN_FIDO2:
1343
                suffix = "-salt";
1344
                break;
1345

1346
        default:
1347
                suffix = NULL;
44✔
1348
        }
1349

1350
        r = asprintf(&bindname,
44✔
1351
                     "@%" PRIx64"/cryptsetup%s%s%s/%s",
1352
                     random_u64(),
1353
                     token_type_name ? "-" : "",
1354
                     strempty(token_type_name),
1355
                     strempty(suffix),
1356
                     volume);
1357
        if (r < 0)
44✔
1358
                return NULL;
44✔
1359

1360
        return bindname;
44✔
1361
}
1362

UNCOV
1363
static int make_security_device_monitor(
×
1364
                sd_event **ret_event,
1365
                sd_device_monitor **ret_monitor) {
1366
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
UNCOV
1367
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
UNCOV
1368
        int r;
×
1369

1370
        assert(ret_event);
×
1371
        assert(ret_monitor);
×
1372

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

1377
        r = sd_event_default(&event);
×
UNCOV
1378
        if (r < 0)
×
UNCOV
1379
                return log_error_errno(r, "Failed to allocate event loop: %m");
×
1380

1381
        r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
×
1382
        if (r < 0)
×
UNCOV
1383
                return log_error_errno(r, "Failed to install timeout event source: %m");
×
1384

1385
        r = sd_device_monitor_new(&monitor);
×
1386
        if (r < 0)
×
UNCOV
1387
                return log_error_errno(r, "Failed to allocate device monitor: %m");
×
1388

1389
        (void) sd_device_monitor_set_description(monitor, "security-device");
×
1390

UNCOV
1391
        r = sd_device_monitor_filter_add_match_tag(monitor, "security-device");
×
1392
        if (r < 0)
×
UNCOV
1393
                return log_error_errno(r, "Failed to configure device monitor: %m");
×
1394

1395
        r = sd_device_monitor_attach_event(monitor, event);
×
1396
        if (r < 0)
×
UNCOV
1397
                return log_error_errno(r, "Failed to attach device monitor: %m");
×
1398

1399
        r = sd_device_monitor_start(monitor, NULL, NULL);
×
1400
        if (r < 0)
×
UNCOV
1401
                return log_error_errno(r, "Failed to start device monitor: %m");
×
1402

1403
        *ret_event = TAKE_PTR(event);
×
1404
        *ret_monitor = TAKE_PTR(monitor);
×
UNCOV
1405
        return 0;
×
1406
}
1407

1408
static int run_security_device_monitor(
×
1409
                sd_event *event,
1410
                sd_device_monitor *monitor) {
1411
        bool processed = false;
×
UNCOV
1412
        int r;
×
1413

1414
        assert(event);
×
1415
        assert(monitor);
×
1416

1417
        /* Runs the event loop for the device monitor until either something happens, or the timeout is
1418
         * hit. */
1419

UNCOV
1420
        for (;;) {
×
UNCOV
1421
                int x;
×
1422

1423
                r = sd_event_get_exit_code(event, &x);
×
1424
                if (r < 0) {
×
UNCOV
1425
                        if (r != -ENODATA)
×
1426
                                return log_error_errno(r, "Failed to query exit code from event loop: %m");
×
1427

1428
                        /* On ENODATA we aren't told to exit yet. */
1429
                } else {
UNCOV
1430
                        assert(x == -ETIMEDOUT);
×
UNCOV
1431
                        return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1432
                                                "Timed out waiting for security device, aborting security device based authentication attempt.");
1433
                }
1434

1435
                /* Wait for one event, and then eat all subsequent events until there are no further ones */
UNCOV
1436
                r = sd_event_run(event, processed ? 0 : UINT64_MAX);
×
UNCOV
1437
                if (r < 0)
×
UNCOV
1438
                        return log_error_errno(r, "Failed to run event loop: %m");
×
1439
                if (r == 0) /* no events queued anymore */
×
1440
                        return 0;
1441

1442
                processed = true;
×
1443
        }
1444
}
1445

1446
static bool use_token_plugins(void) {
60✔
1447

1448
#if HAVE_TPM2
1449
        /* Currently, there's no way for us to query the volume key when plugins are used. Hence don't use
1450
         * plugins, if measurement has been requested. */
1451
        if (arg_tpm2_measure_pcr != UINT_MAX)
60✔
1452
                return false;
1453
        if (arg_tpm2_measure_keyslot_nvpcr)
60✔
1454
                return false;
1455
#endif
1456

1457
        /* Disable tokens if we're in FIDO2 mode with manual parameters. */
1458
        if (arg_fido2_cid)
60✔
1459
                return false;
1460

1461
#if HAVE_LIBCRYPTSETUP_PLUGINS
1462
        int r;
60✔
1463

1464
        /* Permit a way to disable libcryptsetup token module support, for debugging purposes. */
1465
        r = getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE");
60✔
1466
        if (r < 0 && r != -ENXIO)
60✔
UNCOV
1467
                log_debug_errno(r, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE env var: %m");
×
1468
        if (r == 0)
60✔
1469
                return false;
1470

1471
        return crypt_token_external_path();
58✔
1472
#else
1473
        return false;
1474
#endif
1475
}
1476

1477
#if HAVE_LIBCRYPTSETUP_PLUGINS
1478
static int acquire_pins_from_env_variable(char ***ret_pins) {
21✔
1479
        _cleanup_(erase_and_freep) char *envpin = NULL;
21✔
1480
        _cleanup_strv_free_erase_ char **pins = NULL;
21✔
1481
        int r;
21✔
1482

1483
        assert(ret_pins);
21✔
1484

1485
        r = getenv_steal_erase("PIN", &envpin);
21✔
1486
        if (r < 0)
21✔
UNCOV
1487
                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
×
1488
        if (r > 0) {
21✔
1489
                pins = strv_new(envpin);
16✔
1490
                if (!pins)
16✔
UNCOV
1491
                        return log_oom();
×
1492
        }
1493

1494
        *ret_pins = TAKE_PTR(pins);
21✔
1495

1496
        return 0;
21✔
1497
}
1498
#endif
1499

1500
static int crypt_activate_by_token_pin_ask_password(
58✔
1501
                struct crypt_device *cd,
1502
                const char *name,
1503
                const char *type,
1504
                usec_t until,
1505
                void *userdata,
1506
                uint32_t activation_flags,
1507
                const char *message,
1508
                const char *keyring,
1509
                const char *credential) {
1510

1511
#if HAVE_LIBCRYPTSETUP_PLUGINS
1512
        AskPasswordFlags flags = arg_ask_password_flags;
58✔
1513
        _cleanup_strv_free_erase_ char **pins = NULL;
58✔
1514
        int r;
58✔
1515

1516
        r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, /* pin= */ NULL, /* pin_size= */ 0, userdata, activation_flags);
58✔
1517
        if (r > 0) /* returns unlocked keyslot id on success */
58✔
1518
                return 0;
1519
        if (r == -EEXIST) /* volume is already active */
35✔
UNCOV
1520
                return log_external_activation(r, name);
×
1521
        if (r != -ENOANO) /* needs pin or pin is wrong */
35✔
1522
                return r;
1523

1524
        r = acquire_pins_from_env_variable(&pins);
21✔
1525
        if (r < 0)
21✔
1526
                return r;
1527

1528
        STRV_FOREACH(p, pins) {
25✔
1529
                r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), userdata, activation_flags);
16✔
1530
                if (r > 0) /* returns unlocked keyslot id on success */
16✔
1531
                        return 0;
1532
                if (r == -EEXIST) /* volume is already active */
5✔
UNCOV
1533
                        return log_external_activation(r, name);
×
1534
                if (r != -ENOANO) /* needs pin or pin is wrong */
5✔
1535
                        return r;
1536
        }
1537

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

UNCOV
1541
        for (;;) {
×
UNCOV
1542
                pins = strv_free_erase(pins);
×
1543

1544
                AskPasswordRequest req = {
×
1545
                        .tty_fd = -EBADF,
1546
                        .message = message,
1547
                        .icon = "drive-harddisk",
1548
                        .keyring = keyring,
1549
                        .credential = credential,
1550
                        .until = until,
1551
                        .hup_fd = -EBADF,
1552
                };
1553

UNCOV
1554
                r = ask_password_auto(&req, flags, &pins);
×
UNCOV
1555
                if (r < 0)
×
UNCOV
1556
                        return r;
×
1557

1558
                STRV_FOREACH(p, pins) {
×
1559
                        r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), userdata, activation_flags);
×
UNCOV
1560
                        if (r > 0) /* returns unlocked keyslot id on success */
×
1561
                                return 0;
1562
                        if (r == -EEXIST) /* volume is already active */
×
1563
                                return log_external_activation(r, name);
×
UNCOV
1564
                        if (r != -ENOANO) /* needs pin or pin is wrong */
×
1565
                                return r;
1566
                }
1567

UNCOV
1568
                flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
×
1569
        }
1570
        return r;
1571
#else
1572
        return -EOPNOTSUPP;
1573
#endif
1574
}
1575

UNCOV
1576
static int attach_luks2_by_fido2_via_plugin(
×
1577
                struct crypt_device *cd,
1578
                const char *name,
1579
                usec_t until,
1580
                void *userdata,
1581
                uint32_t activation_flags) {
1582

UNCOV
1583
        return crypt_activate_by_token_pin_ask_password(
×
1584
                        cd,
1585
                        name,
1586
                        "systemd-fido2",
1587
                        until,
1588
                        userdata,
1589
                        activation_flags,
1590
                        "Please enter security token PIN:",
1591
                        "fido2-pin",
1592
                        "cryptsetup.fido2-pin");
1593
}
1594

UNCOV
1595
static int attach_luks_or_plain_or_bitlk_by_fido2(
×
1596
                struct crypt_device *cd,
1597
                const char *name,
1598
                const char *key_file,
1599
                const struct iovec *key_data,
1600
                usec_t until,
1601
                uint32_t flags,
1602
                bool pass_volume_key) {
1603

UNCOV
1604
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
UNCOV
1605
        _cleanup_(erase_and_freep) void *decrypted_key = NULL;
×
UNCOV
1606
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
1607
        size_t decrypted_key_size;
×
1608
        _cleanup_free_ char *friendly = NULL;
×
1609
        int keyslot = arg_key_slot, r;
×
1610
        bool use_libcryptsetup_plugin = use_token_plugins();
×
1611

1612
        assert(cd);
×
1613
        assert(name);
×
UNCOV
1614
        assert(arg_fido2_device || arg_fido2_device_auto);
×
1615

1616
        if (arg_fido2_cid && !key_file && !iovec_is_set(key_data))
×
1617
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1618
                                        "FIDO2 mode with manual parameters selected, but no keyfile specified, refusing.");
1619

1620
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
×
UNCOV
1621
        if (!friendly)
×
UNCOV
1622
                return log_oom();
×
1623

1624
        for (;;) {
×
1625
                if (use_libcryptsetup_plugin && !arg_fido2_cid) {
×
UNCOV
1626
                        r = attach_luks2_by_fido2_via_plugin(cd, name, until, arg_fido2_device, flags);
×
1627
                        if (IN_SET(r, -ENOTUNIQ, -ENXIO, -ENOENT))
×
1628
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1629
                                                       "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1630

1631
                } else {
UNCOV
1632
                        if (arg_fido2_cid)
×
UNCOV
1633
                                r = acquire_fido2_key(
×
1634
                                                name,
1635
                                                friendly,
1636
                                                arg_fido2_device,
1637
                                                arg_fido2_rp_id,
1638
                                                arg_fido2_cid, arg_fido2_cid_size,
1639
                                                key_file, arg_keyfile_size, arg_keyfile_offset,
1640
                                                key_data,
1641
                                                until,
1642
                                                arg_fido2_manual_flags,
1643
                                                "cryptsetup.fido2-pin",
1644
                                                arg_ask_password_flags,
1645
                                                &decrypted_key,
1646
                                                &decrypted_key_size);
1647
                        else
UNCOV
1648
                                r = acquire_fido2_key_auto(
×
1649
                                                cd,
1650
                                                name,
1651
                                                friendly,
1652
                                                arg_fido2_device,
1653
                                                until,
1654
                                                "cryptsetup.fido2-pin",
1655
                                                arg_ask_password_flags,
1656
                                                &decrypted_key,
1657
                                                &decrypted_key_size);
UNCOV
1658
                        if (r >= 0)
×
1659
                                break;
1660
                }
1661

UNCOV
1662
                if (r != -EAGAIN) /* EAGAIN means: token not found */
×
1663
                        return r;
1664

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

UNCOV
1669
                        assert(!event);
×
1670

UNCOV
1671
                        r = make_security_device_monitor(&event, &monitor);
×
1672
                        if (r < 0)
×
1673
                                return r;
1674

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

1677
                        /* Let's immediately rescan in case the token appeared in the time we needed
1678
                         * to create and configure the monitor */
UNCOV
1679
                        continue;
×
1680
                }
1681

1682
                r = run_security_device_monitor(event, monitor);
×
UNCOV
1683
                if (r < 0)
×
1684
                        return r;
1685

1686
                log_debug("Got one or more potentially relevant udev events, rescanning FIDO2...");
×
1687
        }
1688

1689
        if (pass_volume_key)
×
UNCOV
1690
                r = measured_crypt_activate_by_volume_key(
×
1691
                                cd,
1692
                                name,
1693
                                "fido2",
1694
                                /* keyslot= */ -1,
1695
                                decrypted_key,
1696
                                decrypted_key_size,
1697
                                flags);
1698
        else {
UNCOV
1699
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
×
UNCOV
1700
                ssize_t base64_encoded_size;
×
1701

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

UNCOV
1704
                base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
×
UNCOV
1705
                if (base64_encoded_size < 0)
×
UNCOV
1706
                        return log_oom();
×
1707

1708
                r = measured_crypt_activate_by_passphrase(
×
1709
                                cd,
1710
                                name,
1711
                                "fido2",
1712
                                keyslot,
1713
                                base64_encoded,
1714
                                base64_encoded_size,
1715
                                flags);
1716
        }
UNCOV
1717
        if (r == -EPERM) {
×
UNCOV
1718
                log_error_errno(r, "Failed to activate with FIDO2 decrypted key. (Key incorrect?)");
×
UNCOV
1719
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
1720
        }
1721
        if (r < 0)
×
1722
                return log_error_errno(r, "Failed to activate with FIDO2 acquired key: %m");
×
1723

1724
        return 0;
1725
}
1726

UNCOV
1727
static int attach_luks2_by_pkcs11_via_plugin(
×
1728
                struct crypt_device *cd,
1729
                const char *name,
1730
                const char *friendly_name,
1731
                usec_t until,
1732
                const char *askpw_credential,
1733
                uint32_t flags) {
1734

1735
#if HAVE_LIBCRYPTSETUP_PLUGINS
UNCOV
1736
        int r;
×
1737

UNCOV
1738
        if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2))
×
1739
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Automatic PKCS#11 metadata requires LUKS2 device.");
×
1740

1741
        systemd_pkcs11_plugin_params params = {
×
1742
                .friendly_name = friendly_name,
1743
                .until = until,
1744
                .askpw_credential = askpw_credential,
1745
                .askpw_flags = arg_ask_password_flags,
1746
        };
1747

UNCOV
1748
        r = crypt_activate_by_token_pin(cd, name, "systemd-pkcs11", CRYPT_ANY_TOKEN, NULL, 0, &params, flags);
×
UNCOV
1749
        if (r > 0) /* returns unlocked keyslot id on success */
×
1750
                r = 0;
1751
        if (r == -EEXIST) /* volume is already active */
×
1752
                r = log_external_activation(r, name);
×
1753

1754
        return r;
1755
#else
1756
        return -EOPNOTSUPP;
1757
#endif
1758
}
1759

UNCOV
1760
static int attach_luks_or_plain_or_bitlk_by_pkcs11(
×
1761
                struct crypt_device *cd,
1762
                const char *name,
1763
                const char *key_file,
1764
                const struct iovec *key_data,
1765
                usec_t until,
1766
                uint32_t flags,
1767
                bool pass_volume_key) {
1768

UNCOV
1769
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
UNCOV
1770
        _cleanup_free_ char *friendly = NULL, *discovered_uri = NULL;
×
UNCOV
1771
        size_t decrypted_key_size = 0, discovered_key_size = 0;
×
1772
        _cleanup_(erase_and_freep) void *decrypted_key = NULL;
×
1773
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
1774
        _cleanup_free_ void *discovered_key = NULL;
×
1775
        struct iovec discovered_key_data = {};
×
1776
        int keyslot = arg_key_slot, r;
×
1777
        const char *uri = NULL;
×
1778
        bool use_libcryptsetup_plugin = use_token_plugins();
×
1779

1780
        assert(cd);
×
1781
        assert(name);
×
UNCOV
1782
        assert(arg_pkcs11_uri || arg_pkcs11_uri_auto);
×
1783

1784
        if (arg_pkcs11_uri_auto) {
×
1785
                if (!use_libcryptsetup_plugin) {
×
UNCOV
1786
                        r = find_pkcs11_auto_data(cd, &discovered_uri, &discovered_key, &discovered_key_size, &keyslot);
×
1787
                        if (IN_SET(r, -ENOTUNIQ, -ENXIO))
×
1788
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1789
                                                       "Automatic PKCS#11 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1790
                        if (r < 0)
×
1791
                                return r;
1792

1793
                        uri = discovered_uri;
×
UNCOV
1794
                        discovered_key_data = IOVEC_MAKE(discovered_key, discovered_key_size);
×
UNCOV
1795
                        key_data = &discovered_key_data;
×
1796
                }
1797
        } else {
1798
                uri = arg_pkcs11_uri;
×
1799

UNCOV
1800
                if (!key_file && !iovec_is_set(key_data))
×
1801
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 mode selected but no key file specified, refusing.");
×
1802
        }
1803

1804
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
×
UNCOV
1805
        if (!friendly)
×
UNCOV
1806
                return log_oom();
×
1807

1808
        for (;;) {
×
1809
                if (use_libcryptsetup_plugin && arg_pkcs11_uri_auto)
×
UNCOV
1810
                        r = attach_luks2_by_pkcs11_via_plugin(
×
1811
                                        cd,
1812
                                        name,
1813
                                        friendly,
1814
                                        until,
1815
                                        "cryptsetup.pkcs11-pin",
1816
                                        flags);
1817
                else {
UNCOV
1818
                        r = decrypt_pkcs11_key(
×
1819
                                        name,
1820
                                        friendly,
1821
                                        uri,
1822
                                        key_file, arg_keyfile_size, arg_keyfile_offset,
1823
                                        key_data,
1824
                                        until,
1825
                                        arg_ask_password_flags,
1826
                                        &decrypted_key, &decrypted_key_size);
UNCOV
1827
                        if (r >= 0)
×
1828
                                break;
1829
                }
1830

UNCOV
1831
                if (r != -EAGAIN) /* EAGAIN means: token not found */
×
1832
                        return r;
1833

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

UNCOV
1838
                        assert(!event);
×
1839

UNCOV
1840
                        r = make_security_device_monitor(&event, &monitor);
×
1841
                        if (r < 0)
×
1842
                                return r;
1843

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

1847
                        /* Let's immediately rescan in case the token appeared in the time we needed
1848
                         * to create and configure the monitor */
UNCOV
1849
                        continue;
×
1850
                }
1851

1852
                r = run_security_device_monitor(event, monitor);
×
UNCOV
1853
                if (r < 0)
×
1854
                        return r;
1855

1856
                log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11...");
×
1857
        }
UNCOV
1858
        assert(decrypted_key);
×
1859

UNCOV
1860
        if (pass_volume_key)
×
1861
                r = measured_crypt_activate_by_volume_key(
×
1862
                                cd,
1863
                                name,
1864
                                "pkcs11",
1865
                                /* keyslot= */ -1,
1866
                                decrypted_key,
1867
                                decrypted_key_size,
1868
                                flags);
1869
        else {
UNCOV
1870
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
×
UNCOV
1871
                ssize_t base64_encoded_size;
×
1872

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

UNCOV
1881
                base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
×
UNCOV
1882
                if (base64_encoded_size < 0)
×
UNCOV
1883
                        return log_oom();
×
1884

1885
                r = measured_crypt_activate_by_passphrase(
×
1886
                                cd,
1887
                                name,
1888
                                "pkcs11",
1889
                                keyslot,
1890
                                base64_encoded,
1891
                                base64_encoded_size,
1892
                                flags);
1893
        }
UNCOV
1894
        if (r == -EPERM) {
×
UNCOV
1895
                log_error_errno(r, "Failed to activate with PKCS#11 decrypted key. (Key incorrect?)");
×
UNCOV
1896
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
1897
        }
1898
        if (r < 0)
×
1899
                return log_error_errno(r, "Failed to activate with PKCS#11 acquired key: %m");
×
1900

1901
        return 0;
1902
}
1903

UNCOV
1904
static int make_tpm2_device_monitor(
×
1905
                sd_event **ret_event,
1906
                sd_device_monitor **ret_monitor) {
1907

UNCOV
1908
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
UNCOV
1909
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
UNCOV
1910
        int r;
×
1911

1912
        assert(ret_event);
×
1913
        assert(ret_monitor);
×
1914

1915
        r = sd_event_default(&event);
×
1916
        if (r < 0)
×
UNCOV
1917
                return log_error_errno(r, "Failed to allocate event loop: %m");
×
1918

1919
        r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
×
1920
        if (r < 0)
×
UNCOV
1921
                return log_error_errno(r, "Failed to install timeout event source: %m");
×
1922

1923
        r = sd_device_monitor_new(&monitor);
×
1924
        if (r < 0)
×
UNCOV
1925
                return log_error_errno(r, "Failed to allocate device monitor: %m");
×
1926

1927
        (void) sd_device_monitor_set_description(monitor, "tpmrm");
×
1928

UNCOV
1929
        r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "tpmrm", NULL);
×
1930
        if (r < 0)
×
UNCOV
1931
                return log_error_errno(r, "Failed to configure device monitor: %m");
×
1932

1933
        r = sd_device_monitor_attach_event(monitor, event);
×
1934
        if (r < 0)
×
UNCOV
1935
                return log_error_errno(r, "Failed to attach device monitor: %m");
×
1936

1937
        r = sd_device_monitor_start(monitor, NULL, NULL);
×
1938
        if (r < 0)
×
UNCOV
1939
                return log_error_errno(r, "Failed to start device monitor: %m");
×
1940

1941
        *ret_event = TAKE_PTR(event);
×
1942
        *ret_monitor = TAKE_PTR(monitor);
×
UNCOV
1943
        return 0;
×
1944
}
1945

1946
static int attach_luks2_by_tpm2_via_plugin(
10✔
1947
                struct crypt_device *cd,
1948
                const char *name,
1949
                usec_t until,
1950
                uint32_t flags) {
1951

1952
#if HAVE_LIBCRYPTSETUP_PLUGINS
1953
        systemd_tpm2_plugin_params params = {
10✔
1954
                .search_pcr_mask = arg_tpm2_pcr_mask,
1955
                .device = arg_tpm2_device,
1956
                .signature_path = arg_tpm2_signature,
1957
                .pcrlock_path = arg_tpm2_pcrlock,
1958
        };
1959

1960
        if (!use_token_plugins())
10✔
1961
                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1✔
1962
                                       "libcryptsetup has external plugins support disabled.");
1963

1964
        return crypt_activate_by_token_pin_ask_password(
9✔
1965
                        cd,
1966
                        name,
1967
                        "systemd-tpm2",
1968
                        until,
1969
                        &params,
1970
                        flags,
1971
                        "Please enter TPM2 PIN:",
1972
                        "tpm2-pin",
1973
                        "cryptsetup.tpm2-pin");
1974
#else
1975
        return -EOPNOTSUPP;
1976
#endif
1977
}
1978

1979
static int attach_luks_or_plain_or_bitlk_by_tpm2(
10✔
1980
                struct crypt_device *cd,
1981
                const char *name,
1982
                const char *key_file,
1983
                const struct iovec *key_data,
1984
                usec_t until,
1985
                uint32_t flags,
1986
                bool pass_volume_key) {
1987

1988
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
10✔
UNCOV
1989
        _cleanup_(iovec_done_erase) struct iovec decrypted_key = {};
×
1990
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
10✔
1991
        _cleanup_free_ char *friendly = NULL;
10✔
1992
        int keyslot = arg_key_slot, r;
10✔
1993

1994
        assert(cd);
10✔
1995
        assert(name);
10✔
1996
        assert(arg_tpm2_device || arg_tpm2_device_auto);
10✔
1997

1998
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
10✔
1999
        if (!friendly)
10✔
UNCOV
2000
                return log_oom();
×
2001

2002
        for (;;) {
10✔
2003
                if (key_file || iovec_is_set(key_data)) {
10✔
2004
                        /* If key data is specified, use that */
2005

UNCOV
2006
                        r = acquire_tpm2_key(
×
2007
                                        name,
2008
                                        arg_tpm2_device,
2009
                                        arg_tpm2_pcr_mask == UINT32_MAX ? TPM2_PCR_MASK_DEFAULT_LEGACY : arg_tpm2_pcr_mask,
×
2010
                                        UINT16_MAX,
2011
                                        /* pubkey= */ NULL,
2012
                                        /* pubkey_pcr_mask= */ 0,
2013
                                        /* signature_path= */ NULL,
2014
                                        /* pcrlock_path= */ NULL,
2015
                                        /* primary_alg= */ 0,
2016
                                        key_file, arg_keyfile_size, arg_keyfile_offset,
2017
                                        key_data, /* n_blobs= */ 1,
2018
                                        /* policy_hash= */ NULL, /* we don't know the policy hash */
2019
                                        /* n_policy_hash= */ 0,
2020
                                        /* salt= */ NULL,
2021
                                        /* srk= */ NULL,
2022
                                        /* pcrlock_nv= */ NULL,
2023
                                        arg_tpm2_pin ? TPM2_FLAGS_USE_PIN : 0,
2024
                                        until,
2025
                                        "cryptsetup.tpm2-pin",
2026
                                        arg_ask_password_flags,
2027
                                        &decrypted_key);
UNCOV
2028
                        if (r >= 0)
×
2029
                                break;
UNCOV
2030
                        if (IN_SET(r, -EACCES, -ENOLCK))
×
2031
                                return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
×
UNCOV
2032
                        if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */
×
2033
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking.");
×
2034
                        /* EAGAIN means: no tpm2 chip found */
2035
                        if (r != -EAGAIN) {
×
2036
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
×
UNCOV
2037
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
×
2038
                        }
2039
                } else {
2040
                        r = attach_luks2_by_tpm2_via_plugin(cd, name, until, flags);
10✔
2041
                        if (r >= 0)
10✔
2042
                                return 0;
2043
                        /* EAGAIN     means: no tpm2 chip found
2044
                         * EOPNOTSUPP means: no libcryptsetup plugins support */
2045
                        if (r == -ENXIO)
10✔
UNCOV
2046
                                return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
×
2047
                                                        "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking.");
2048
                        if (r == -ENOENT)
10✔
2049
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
3✔
2050
                                                       "No TPM2 metadata enrolled in LUKS2 header or TPM2 support not available, falling back to traditional unlocking.");
2051
                        if (!IN_SET(r, -EOPNOTSUPP, -EAGAIN)) {
7✔
2052
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
6✔
2053
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
6✔
2054
                        }
2055
                }
2056

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

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

2065
                        for (;;) {
1✔
UNCOV
2066
                                _cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {};
×
2067
                                struct iovec *blobs = NULL, *policy_hash = NULL;
1✔
2068
                                uint32_t hash_pcr_mask, pubkey_pcr_mask;
1✔
2069
                                size_t n_blobs = 0, n_policy_hash = 0;
1✔
2070
                                uint16_t pcr_bank, primary_alg;
1✔
2071
                                TPM2Flags tpm2_flags;
1✔
2072

2073
                                CLEANUP_ARRAY(blobs, n_blobs, iovec_array_free);
1✔
2074
                                CLEANUP_ARRAY(policy_hash, n_policy_hash, iovec_array_free);
1✔
2075

2076
                                r = find_tpm2_auto_data(
1✔
2077
                                                cd,
2078
                                                arg_tpm2_pcr_mask, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */
2079
                                                token, /* search for the token with this index, or any later index than this */
2080
                                                &hash_pcr_mask,
2081
                                                &pcr_bank,
2082
                                                &pubkey,
2083
                                                &pubkey_pcr_mask,
2084
                                                &primary_alg,
2085
                                                &blobs,
2086
                                                &n_blobs,
2087
                                                &policy_hash,
2088
                                                &n_policy_hash,
2089
                                                &salt,
2090
                                                &srk,
2091
                                                &pcrlock_nv,
2092
                                                &tpm2_flags,
2093
                                                &keyslot,
2094
                                                &token);
2095
                                if (r == -ENXIO)
1✔
2096
                                        /* No further TPM2 tokens found in the LUKS2 header. */
UNCOV
2097
                                        return log_full_errno(found_some ? LOG_NOTICE : LOG_DEBUG,
×
2098
                                                              SYNTHETIC_ERRNO(EAGAIN),
2099
                                                              found_some
2100
                                                              ? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking."
2101
                                                              : "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking.");
2102
                                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
1✔
2103
                                        /* TPM2 support not compiled in? */
UNCOV
2104
                                        return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
2105
                                                               "TPM2 support not available, falling back to traditional unlocking.");
2106
                                if (r < 0)
1✔
2107
                                        return r;
2108

2109
                                found_some = true;
1✔
2110

2111
                                r = acquire_tpm2_key(
1✔
2112
                                                name,
2113
                                                arg_tpm2_device,
2114
                                                hash_pcr_mask,
2115
                                                pcr_bank,
2116
                                                &pubkey,
2117
                                                pubkey_pcr_mask,
2118
                                                arg_tpm2_signature,
2119
                                                arg_tpm2_pcrlock,
2120
                                                primary_alg,
2121
                                                /* key_file= */ NULL, /* key_file_size= */ 0, /* key_file_offset= */ 0, /* no key file */
2122
                                                blobs,
2123
                                                n_blobs,
2124
                                                policy_hash,
2125
                                                n_policy_hash,
2126
                                                &salt,
2127
                                                &srk,
2128
                                                &pcrlock_nv,
2129
                                                tpm2_flags,
2130
                                                until,
2131
                                                "cryptsetup.tpm2-pin",
2132
                                                arg_ask_password_flags,
2133
                                                &decrypted_key);
2134
                                if (IN_SET(r, -EACCES, -ENOLCK))
1✔
UNCOV
2135
                                        return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
×
2136
                                if (r != -EPERM)
1✔
2137
                                        break;
2138

UNCOV
2139
                                token++; /* try a different token next time */
×
2140
                        }
2141

2142
                        if (r >= 0)
1✔
2143
                                break;
2144
                        /* EAGAIN means: no tpm2 chip found */
UNCOV
2145
                        if (r != -EAGAIN) {
×
UNCOV
2146
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
×
UNCOV
2147
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
×
2148
                        }
2149
                }
2150

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

UNCOV
2155
                        assert(!event);
×
2156

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

2161
                        r = make_tpm2_device_monitor(&event, &monitor);
×
UNCOV
2162
                        if (r < 0)
×
2163
                                return r;
2164

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

2167
                        /* Let's immediately rescan in case the device appeared in the time we needed
2168
                         * to create and configure the monitor */
UNCOV
2169
                        continue;
×
2170
                }
2171

2172
                r = run_security_device_monitor(event, monitor);
×
UNCOV
2173
                if (r < 0)
×
2174
                        return r;
2175

2176
                log_debug("Got one or more potentially relevant udev events, rescanning for TPM2...");
×
2177
        }
2178

2179
        if (pass_volume_key)
1✔
UNCOV
2180
                r = measured_crypt_activate_by_volume_key(
×
2181
                                cd,
2182
                                name,
2183
                                "tpm2",
2184
                                /* keyslot= */ -1,
UNCOV
2185
                                decrypted_key.iov_base,
×
2186
                                decrypted_key.iov_len,
2187
                                flags);
2188
        else {
2189
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1✔
2190
                ssize_t base64_encoded_size;
1✔
2191

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

2194
                base64_encoded_size = base64mem(decrypted_key.iov_base, decrypted_key.iov_len, &base64_encoded);
1✔
2195
                if (base64_encoded_size < 0)
1✔
UNCOV
2196
                        return log_oom();
×
2197

2198
                r = measured_crypt_activate_by_passphrase(
1✔
2199
                                cd,
2200
                                name,
2201
                                "tpm2",
2202
                                keyslot,
2203
                                base64_encoded,
2204
                                base64_encoded_size,
2205
                                flags);
2206
        }
2207
        if (r == -EPERM) {
1✔
UNCOV
2208
                log_error_errno(r, "Failed to activate with TPM2 decrypted key. (Key incorrect?)");
×
UNCOV
2209
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
2210
        }
2211
        if (r < 0)
1✔
2212
                return log_error_errno(r, "Failed to activate with TPM2 acquired key: %m");
×
2213

2214
        return 0;
2215
}
2216

2217
static int attach_luks_or_plain_or_bitlk_by_key_data(
3✔
2218
                struct crypt_device *cd,
2219
                const char *name,
2220
                const struct iovec *key_data,
2221
                uint32_t flags,
2222
                bool pass_volume_key) {
2223

2224
        int r;
3✔
2225

2226
        assert(cd);
3✔
2227
        assert(name);
3✔
2228
        assert(key_data);
3✔
2229

2230
        if (pass_volume_key)
3✔
UNCOV
2231
                r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, key_data->iov_base, key_data->iov_len, flags);
×
2232
        else
2233
                r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, key_data->iov_base, key_data->iov_len, flags);
3✔
2234
        if (r == -EPERM) {
3✔
UNCOV
2235
                log_error_errno(r, "Failed to activate. (Key incorrect?)");
×
UNCOV
2236
                return -EAGAIN; /* Log actual error, but return EAGAIN */
×
2237
        }
2238
        if (r < 0)
3✔
2239
                return log_error_errno(r, "Failed to activate: %m");
×
2240

2241
        return 0;
2242
}
2243

2244
static int attach_luks_or_plain_or_bitlk_by_key_file(
20✔
2245
                struct crypt_device *cd,
2246
                const char *name,
2247
                const char *key_file,
2248
                uint32_t flags,
2249
                bool pass_volume_key) {
2250

2251
        _cleanup_(erase_and_freep) char *kfdata = NULL;
20✔
2252
        _cleanup_free_ char *bindname = NULL;
20✔
2253
        size_t kfsize;
20✔
2254
        int r;
20✔
2255

2256
        assert(cd);
20✔
2257
        assert(name);
20✔
2258
        assert(key_file);
20✔
2259

2260
        /* If we read the key via AF_UNIX, make this client recognizable */
2261
        bindname = make_bindname(name, /* token_type= */ _TOKEN_TYPE_INVALID);
20✔
2262
        if (!bindname)
20✔
UNCOV
2263
                return log_oom();
×
2264

2265
        r = read_full_file_full(
23✔
2266
                        AT_FDCWD, key_file,
2267
                        arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset,
20✔
2268
                        arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size,
20✔
2269
                        READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
2270
                        bindname,
2271
                        &kfdata, &kfsize);
2272
        if (r == -E2BIG) {
20✔
UNCOV
2273
                log_error_errno(r, "Failed to activate, key file '%s' too large.", key_file);
×
UNCOV
2274
                return -EAGAIN;
×
2275
        }
2276
        if (r == -ENOENT) {
20✔
2277
                log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file);
×
UNCOV
2278
                return -EAGAIN; /* Log actual error, but return EAGAIN */
×
2279
        }
2280
        if (r < 0)
20✔
2281
                return log_error_errno(r, "Failed to read key file '%s': %m", key_file);
×
2282

2283
        if (pass_volume_key)
20✔
2284
                r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, kfdata, kfsize, flags);
×
2285
        else
2286
                r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, kfdata, kfsize, flags);
20✔
2287
        if (r == -EPERM) {
20✔
2288
                log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file);
3✔
2289
                return -EAGAIN; /* Log actual error, but return EAGAIN */
3✔
2290
        }
2291
        if (r < 0)
17✔
UNCOV
2292
                return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file);
×
2293

2294
        return 0;
2295
}
2296

UNCOV
2297
static int attach_luks_or_plain_or_bitlk_by_passphrase(
×
2298
                struct crypt_device *cd,
2299
                const char *name,
2300
                char **passwords,
2301
                uint32_t flags,
2302
                bool pass_volume_key) {
2303

UNCOV
2304
        int r;
×
2305

UNCOV
2306
        assert(cd);
×
2307
        assert(name);
×
2308

2309
        r = -EINVAL;
2310
        STRV_FOREACH(p, passwords) {
×
UNCOV
2311
                if (pass_volume_key)
×
UNCOV
2312
                        r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, *p, arg_key_size, flags);
×
2313
                else
2314
                        r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, *p, strlen(*p), flags);
×
2315
                if (r >= 0)
×
2316
                        break;
2317
        }
2318
        if (r == -EPERM) {
×
UNCOV
2319
                log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)");
×
UNCOV
2320
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
2321
        }
2322
        if (r < 0)
×
2323
                return log_error_errno(r, "Failed to activate with specified passphrase: %m");
×
2324

2325
        return 0;
2326
}
2327

2328
static int attach_luks_or_plain_or_bitlk(
33✔
2329
                struct crypt_device *cd,
2330
                const char *name,
2331
                TokenType token_type,
2332
                const char *key_file,
2333
                const struct iovec *key_data,
2334
                char **passwords,
2335
                uint32_t flags,
2336
                usec_t until) {
2337

2338
        bool pass_volume_key = false;
33✔
2339
        int r;
33✔
2340

2341
        assert(cd);
33✔
2342
        assert(name);
33✔
2343

2344
        if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) {
33✔
UNCOV
2345
                struct crypt_params_plain params = {
×
2346
                        .offset = arg_offset,
2347
                        .skip = arg_skip,
2348
                        .sector_size = arg_sector_size,
2349
                };
UNCOV
2350
                const char *cipher, *cipher_mode;
×
UNCOV
2351
                _cleanup_free_ char *truncated_cipher = NULL;
×
2352

2353
                if (streq_ptr(arg_hash, "plain"))
×
2354
                        /* plain isn't a real hash type. it just means "use no hash" */
2355
                        params.hash = NULL;
2356
                else if (arg_hash)
×
UNCOV
2357
                        params.hash = arg_hash;
×
UNCOV
2358
                else if (!key_file)
×
2359
                        /* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key
2360
                         * file is provided */
2361
                        params.hash = "ripemd160";
×
2362

UNCOV
2363
                if (arg_cipher) {
×
2364
                        size_t l;
×
2365

2366
                        l = strcspn(arg_cipher, "-");
×
2367
                        truncated_cipher = strndup(arg_cipher, l);
×
UNCOV
2368
                        if (!truncated_cipher)
×
2369
                                return log_oom();
×
2370

2371
                        cipher = truncated_cipher;
×
2372
                        cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain";
×
2373
                } else {
2374
                        cipher = "aes";
2375
                        cipher_mode = "cbc-essiv:sha256";
2376
                }
2377

2378
                /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */
UNCOV
2379
                arg_keyfile_size = arg_key_size;
×
2380

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

2387
                /* hash == NULL implies the user passed "plain" */
2388
                pass_volume_key = !params.hash;
×
2389
        }
2390

2391
        log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
33✔
2392
                 crypt_get_cipher(cd),
2393
                 crypt_get_cipher_mode(cd),
2394
                 crypt_get_volume_key_size(cd)*8,
2395
                 crypt_get_device_name(cd));
2396

2397
        if (token_type == TOKEN_TPM2)
33✔
2398
                return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, until, flags, pass_volume_key);
10✔
2399
        if (token_type == TOKEN_FIDO2)
23✔
UNCOV
2400
                return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, until, flags, pass_volume_key);
×
2401
        if (token_type == TOKEN_PKCS11)
23✔
UNCOV
2402
                return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, until, flags, pass_volume_key);
×
2403
        if (key_data)
23✔
2404
                return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, flags, pass_volume_key);
3✔
2405
        if (key_file)
20✔
2406
                return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key);
20✔
2407

UNCOV
2408
        return attach_luks_or_plain_or_bitlk_by_passphrase(cd, name, passwords, flags, pass_volume_key);
×
2409
}
2410

2411
static int help(void) {
×
UNCOV
2412
        _cleanup_free_ char *link = NULL;
×
UNCOV
2413
        int r;
×
2414

2415
        r = terminal_urlify_man("systemd-cryptsetup", "8", &link);
×
2416
        if (r < 0)
×
UNCOV
2417
                return log_oom();
×
2418

2419
        printf("%1$s attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG]\n"
×
2420
               "%1$s detach VOLUME\n\n"
2421
               "%2$sAttach or detach an encrypted block device.%3$s\n\n"
2422
               "  -h --help            Show this help\n"
2423
               "     --version         Show package version\n"
2424
               "\nSee the %4$s for details.\n",
2425
               program_invocation_short_name,
2426
               ansi_highlight(),
2427
               ansi_normal(),
2428
               link);
2429

2430
        return 0;
2431
}
2432

2433
static int parse_argv(int argc, char *argv[]) {
125✔
2434
        enum {
125✔
2435
                ARG_VERSION = 0x100,
2436
        };
2437

2438
        static const struct option options[] = {
125✔
2439
                { "help",                         no_argument,       NULL, 'h'                       },
2440
                { "version",                      no_argument,       NULL, ARG_VERSION               },
2441
                {}
2442
        };
2443

2444
        int c;
125✔
2445

2446
        assert(argc >= 0);
125✔
2447
        assert(argv);
125✔
2448

2449
        if (argv_looks_like_help(argc, argv))
125✔
UNCOV
2450
                return help();
×
2451

2452
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
125✔
2453
                switch (c) {
×
2454

UNCOV
2455
                case 'h':
×
2456
                        return help();
×
2457

2458
                case ARG_VERSION:
×
2459
                        return version();
×
2460

2461
                case '?':
2462
                        return -EINVAL;
2463

UNCOV
2464
                default:
×
UNCOV
2465
                        assert_not_reached();
×
2466
                }
2467

2468
        return 1;
2469
}
2470

2471
static uint32_t determine_flags(void) {
70✔
2472
        uint32_t flags = 0;
70✔
2473

2474
        if (arg_readonly)
70✔
UNCOV
2475
                flags |= CRYPT_ACTIVATE_READONLY;
×
2476

2477
        if (arg_discards)
70✔
2478
                flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
×
2479

2480
        if (arg_same_cpu_crypt)
70✔
2481
                flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
×
2482

2483
        if (arg_submit_from_crypt_cpus)
70✔
2484
                flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
×
2485

2486
        if (arg_no_read_workqueue)
70✔
2487
                flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
×
2488

2489
        if (arg_no_write_workqueue)
70✔
2490
                flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
×
2491

2492
        /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
2493
        /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
2494
        flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF;
70✔
2495

2496
        return flags;
70✔
2497
}
2498

2499
static void remove_and_erasep(const char **p) {
70✔
2500
        int r;
70✔
2501

2502
        if (!*p)
70✔
2503
                return;
2504

2505
        r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE);
2✔
2506
        if (r < 0 && r != -ENOENT)
2✔
UNCOV
2507
                log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p);
×
2508
}
2509

2510
static TokenType determine_token_type(void) {
47✔
2511
        if (arg_tpm2_device || arg_tpm2_device_auto)
47✔
2512
                return TOKEN_TPM2;
2513
        if (arg_fido2_device || arg_fido2_device_auto)
37✔
2514
                return TOKEN_FIDO2;
2515
        if (arg_pkcs11_uri || arg_pkcs11_uri_auto)
37✔
UNCOV
2516
                return TOKEN_PKCS11;
×
2517

2518
        return _TOKEN_TYPE_INVALID;
2519
}
2520

2521
static int discover_key(const char *key_file, const char *volume, TokenType token_type, struct iovec *ret_key_data) {
24✔
2522
        _cleanup_free_ char *bindname = NULL;
24✔
2523
        const char *token_type_name;
24✔
2524
        int r;
24✔
2525

2526
        assert(key_file);
24✔
2527
        assert(volume);
24✔
2528
        assert(ret_key_data);
24✔
2529

2530
        bindname = make_bindname(volume, token_type);
24✔
2531
        if (!bindname)
24✔
UNCOV
2532
                return log_oom();
×
2533

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

2539
        token_type_name = token_type_to_string(token_type);
1✔
2540
        if (token_type_name)
1✔
UNCOV
2541
                log_debug("Automatically discovered encrypted key for volume '%s' (token type: %s).", volume, token_type_name);
×
2542
        else
2543
                log_debug("Automatically discovered key for volume '%s'.", volume);
1✔
2544

2545
        return r;
2546
}
2547

2548
static int verb_attach(int argc, char *argv[], void *userdata) {
70✔
2549
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
70✔
2550
        _unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL;
70✔
2551
        crypt_status_info status;
70✔
2552
        uint32_t flags = 0;
70✔
2553
        unsigned tries;
70✔
2554
        usec_t until;
70✔
2555
        PassphraseType passphrase_type = PASSPHRASE_NONE;
70✔
2556
        int r;
70✔
2557

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

2560
        assert(argc >= 3 && argc <= 5);
70✔
2561

2562
        const char *volume = ASSERT_PTR(argv[1]),
70✔
2563
                *source = ASSERT_PTR(argv[2]),
70✔
2564
                *key_file = argc >= 4 ? mangle_none(argv[3]) : NULL,
70✔
2565
                *config = argc >= 5 ? mangle_none(argv[4]) : NULL;
70✔
2566

2567
        if (!filename_is_valid(volume))
70✔
UNCOV
2568
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
2569

2570
        if (key_file && !path_is_absolute(key_file)) {
70✔
UNCOV
2571
                log_warning("Password file path '%s' is not absolute. Ignoring.", key_file);
×
2572
                key_file = NULL;
2573
        }
2574

2575
        if (config) {
70✔
2576
                r = parse_crypt_config(config);
70✔
2577
                if (r < 0)
70✔
2578
                        return r;
2579
        }
2580

2581
        log_debug("%s %s ← %s type=%s cipher=%s", __func__,
193✔
2582
                  volume, source, strempty(arg_type), strempty(arg_cipher));
2583

2584
        /* A delicious drop of snake oil */
2585
        (void) mlockall(MCL_FUTURE);
70✔
2586

2587
        if (key_file && arg_keyfile_erase)
70✔
2588
                destroy_key_file = key_file; /* let's get this baby erased when we leave */
2✔
2589

2590
        if (arg_header) {
70✔
2591
                if (streq_ptr(arg_type, CRYPT_TCRYPT)){
9✔
UNCOV
2592
                        log_debug("tcrypt header: %s", arg_header);
×
UNCOV
2593
                        r = crypt_init_data_device(&cd, arg_header, source);
×
2594
                } else {
2595
                        log_debug("LUKS header: %s", arg_header);
9✔
2596
                        r = crypt_init(&cd, arg_header);
9✔
2597
                }
2598
        } else
2599
                r = crypt_init(&cd, source);
61✔
2600
        if (r < 0)
70✔
UNCOV
2601
                return log_error_errno(r, "crypt_init() failed: %m");
×
2602

2603
        cryptsetup_enable_logging(cd);
70✔
2604

2605
        status = crypt_status(cd, volume);
70✔
2606
        if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
70✔
UNCOV
2607
                log_info("Volume %s already active.", volume);
×
UNCOV
2608
                return 0;
×
2609
        }
2610

2611
        flags = determine_flags();
70✔
2612

2613
        until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
70✔
UNCOV
2614
        if (until == USEC_INFINITY)
×
2615
                until = 0;
70✔
2616

2617
        if (arg_key_size == 0)
70✔
2618
                arg_key_size = 256U / 8U;
70✔
2619

2620
        if (key_file) {
70✔
2621
                struct stat st;
20✔
2622

2623
                /* Ideally we'd do this on the open fd, but since this is just a warning it's OK to do this
2624
                 * in two steps. */
2625
                if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005))
20✔
UNCOV
2626
                        log_warning("Key file %s is world-readable. This is not a good idea!", key_file);
×
2627
        }
2628

2629
        if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) {
70✔
2630
                r = crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL);
70✔
2631
                if (r < 0)
70✔
2632
                        return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd));
35✔
2633

2634
/* since cryptsetup 2.7.0 (Jan 2024) */
2635
#if HAVE_CRYPT_SET_KEYRING_TO_LINK
2636
                if (arg_link_key_description) {
70✔
UNCOV
2637
                        r = crypt_set_keyring_to_link(cd, arg_link_key_description, NULL, arg_link_key_type, arg_link_keyring);
×
UNCOV
2638
                        if (r < 0)
×
UNCOV
2639
                                log_warning_errno(r, "Failed to set keyring or key description to link volume key in, ignoring: %m");
×
2640
                }
2641
#endif
2642

2643
                if (arg_header) {
70✔
2644
                        r = crypt_set_data_device(cd, source);
9✔
2645
                        if (r < 0)
9✔
UNCOV
2646
                                return log_error_errno(r, "Failed to set LUKS data device %s: %m", source);
×
2647
                }
2648

2649
                /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */
2650
                if (!key_file && use_token_plugins()) {
70✔
2651
                        r = crypt_activate_by_token_pin_ask_password(
49✔
2652
                                        cd,
2653
                                        volume,
2654
                                        /* type= */ NULL,
2655
                                        until,
2656
                                        /* userdata= */ NULL,
2657
                                        flags,
2658
                                        "Please enter LUKS2 token PIN:",
2659
                                        "luks2-pin",
2660
                                        "cryptsetup.luks2-pin");
2661
                        if (r >= 0) {
49✔
2662
                                log_debug("Volume %s activated with a LUKS token.", volume);
35✔
2663
                                return 0;
35✔
2664
                        }
2665

2666
                        log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd));
14✔
2667
                }
2668
        }
2669

2670
        if (streq_ptr(arg_type, CRYPT_BITLK)) {
35✔
UNCOV
2671
                r = crypt_load(cd, CRYPT_BITLK, NULL);
×
UNCOV
2672
                if (r < 0)
×
UNCOV
2673
                        return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd));
×
2674
        }
2675

2676
        bool use_cached_passphrase = true, try_discover_key = !key_file;
35✔
2677
        const char *discovered_key_fn = strjoina(volume, ".key");
175✔
2678
        _cleanup_strv_free_erase_ char **passwords = NULL;
35✔
2679
        for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
47✔
2680
                _cleanup_(iovec_done_erase) struct iovec discovered_key_data = {};
47✔
2681
                const struct iovec *key_data = NULL;
47✔
2682
                TokenType token_type = determine_token_type();
47✔
2683

2684
                log_debug("Beginning attempt %u to unlock.", tries);
47✔
2685

2686
                /* When we were able to acquire multiple keys, let's always process them in this order:
2687
                 *
2688
                 *    1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip
2689
                 *    2. The configured or discovered key, of which both are exclusive and optional
2690
                 *    3. The empty password, in case arg_try_empty_password is set
2691
                 *    4. We enquire the user for a password
2692
                 */
2693

2694
                if (try_discover_key) {
47✔
2695
                        r = discover_key(discovered_key_fn, volume, token_type, &discovered_key_data);
24✔
2696
                        if (r < 0)
24✔
2697
                                return r;
2698
                        if (r > 0)
24✔
2699
                                key_data = &discovered_key_data;
1✔
2700
                }
2701

2702
                if (token_type < 0 && !key_file && !key_data && !passwords) {
47✔
2703

2704
                        /* If we have nothing to try anymore, then acquire a new password */
2705

2706
                        if (arg_try_empty_password) {
16✔
2707
                                /* Hmm, let's try an empty password now, but only once */
2708
                                arg_try_empty_password = false;
2✔
2709
                                key_data = &iovec_empty;
2✔
2710
                        } else {
2711
                                /* Ask the user for a passphrase or recovery key only as last resort, if we
2712
                                 * have nothing else to check for */
2713
                                if (passphrase_type == PASSPHRASE_NONE) {
14✔
2714
                                        passphrase_type = check_registered_passwords(cd);
14✔
2715
                                        if (passphrase_type == PASSPHRASE_NONE)
14✔
UNCOV
2716
                                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No passphrase or recovery key registered.");
×
2717
                                }
2718

2719
                                r = get_password(
14✔
2720
                                                volume,
2721
                                                source,
2722
                                                until,
2723
                                                /* ignore_cached= */ !use_cached_passphrase || arg_verify,
14✔
2724
                                                passphrase_type,
2725
                                                &passwords);
2726
                                use_cached_passphrase = false;
14✔
2727
                                if (r == -EAGAIN)
14✔
UNCOV
2728
                                        continue;
×
2729
                                if (r < 0)
14✔
2730
                                        return r;
2731
                        }
2732
                }
2733

2734
                if (streq_ptr(arg_type, CRYPT_TCRYPT))
33✔
UNCOV
2735
                        r = attach_tcrypt(cd, volume, token_type, key_file, key_data, passwords, flags);
×
2736
                else
2737
                        r = attach_luks_or_plain_or_bitlk(cd, volume, token_type, key_file, key_data, passwords, flags, until);
33✔
2738
                if (r >= 0)
33✔
2739
                        break;
2740
                if (r != -EAGAIN)
12✔
2741
                        return r;
2742

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

2746
                if (token_type == TOKEN_TPM2) {
12✔
2747
                        arg_tpm2_device = mfree(arg_tpm2_device);
9✔
2748
                        arg_tpm2_device_auto = false;
9✔
2749
                        continue;
9✔
2750
                }
2751

2752
                if (token_type == TOKEN_FIDO2) {
3✔
UNCOV
2753
                        arg_fido2_device = mfree(arg_fido2_device);
×
UNCOV
2754
                        arg_fido2_device_auto = false;
×
UNCOV
2755
                        continue;
×
2756
                }
2757

2758
                if (token_type == TOKEN_PKCS11) {
3✔
UNCOV
2759
                        arg_pkcs11_uri = mfree(arg_pkcs11_uri);
×
UNCOV
2760
                        arg_pkcs11_uri_auto = false;
×
2761
                        continue;
×
2762
                }
2763

2764
                if (try_discover_key) {
3✔
UNCOV
2765
                        try_discover_key = false;
×
UNCOV
2766
                        continue;
×
2767
                }
2768

2769
                if (key_file) {
3✔
2770
                        key_file = NULL;
3✔
2771
                        continue;
3✔
2772
                }
2773

2774
                if (passwords) {
×
UNCOV
2775
                        passwords = strv_free_erase(passwords);
×
UNCOV
2776
                        continue;
×
2777
                }
2778

UNCOV
2779
                log_debug("Prepared for next attempt to unlock.");
×
2780
        }
2781

2782
        if (arg_tries != 0 && tries >= arg_tries)
21✔
2783
                return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up.");
×
2784

2785
        return 0;
2786
}
2787

2788
static int verb_detach(int argc, char *argv[], void *userdata) {
55✔
2789
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
55✔
2790
        const char *volume = ASSERT_PTR(argv[1]);
55✔
2791
        int r;
55✔
2792

2793
        assert(argc == 2);
55✔
2794

2795
        if (!filename_is_valid(volume))
55✔
UNCOV
2796
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
2797

2798
        r = crypt_init_by_name(&cd, volume);
55✔
2799
        if (r == -ENODEV) {
55✔
UNCOV
2800
                log_info("Volume %s already inactive.", volume);
×
UNCOV
2801
                return 0;
×
2802
        }
2803
        if (r < 0)
55✔
2804
                return log_error_errno(r, "crypt_init_by_name() for volume '%s' failed: %m", volume);
×
2805

2806
        cryptsetup_enable_logging(cd);
55✔
2807

2808
        r = crypt_deactivate(cd, volume);
55✔
2809
        if (r < 0)
55✔
UNCOV
2810
                return log_error_errno(r, "Failed to deactivate '%s': %m", volume);
×
2811

2812
        return 0;
2813
}
2814

2815
static int run(int argc, char *argv[]) {
125✔
2816
        int r;
125✔
2817

2818
        log_setup();
125✔
2819

2820
        umask(0022);
125✔
2821

2822
        r = parse_argv(argc, argv);
125✔
2823
        if (r <= 0)
125✔
2824
                return r;
2825

2826
        cryptsetup_enable_logging(NULL);
125✔
2827

2828
        static const Verb verbs[] = {
125✔
2829
                { "attach", 3, 5, 0, verb_attach },
2830
                { "detach", 2, 2, 0, verb_detach },
2831
                {}
2832
        };
2833

2834
        return dispatch_verb(argc, argv, verbs, NULL);
125✔
2835
}
2836

2837
DEFINE_MAIN_FUNCTION(run);
125✔
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