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

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

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

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

3
#include <errno.h>
4
#include <getopt.h>
5
#include <mntent.h>
6
#include <sys/mman.h>
7
#include <sys/stat.h>
8
#include <sys/types.h>
9
#include <unistd.h>
10

11
#include "sd-device.h"
12
#include "sd-json.h"
13
#include "sd-messages.h"
14

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

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

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

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

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

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

UNCOV
151
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(passphrase_type, PassphraseType);
×
152

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

159
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(token_type, TokenType);
46✔
160

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

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

175
        assert(option);
146✔
176

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

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

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

189
        } else if ((val = startswith(option, "size="))) {
145✔
190

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

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

UNCOV
202
                arg_key_size /= 8;
×
203

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

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

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

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

220
        } else if ((val = startswith(option, "key-slot=")) ||
145✔
221
                   (val = startswith(option, "keyslot="))) {
143✔
222

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

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

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

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

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

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

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

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

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

257
                arg_keyfile_erase = r;
2✔
258

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

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

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

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

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

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

283
        } else if ((val = startswith(option, "tries="))) {
124✔
284

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

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

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

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

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

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

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

365
        } else if ((val = startswith(option, "offset="))) {
116✔
366

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

371
        } else if ((val = startswith(option, "skip="))) {
116✔
372

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

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

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

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

UNCOV
390
                        arg_pkcs11_uri_auto = false;
×
391
                }
392

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

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

UNCOV
403
                        arg_fido2_device_auto = false;
×
404
                }
405

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
476
                        arg_tpm2_device_auto = false;
×
477
                }
478

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

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

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

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

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

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

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

UNCOV
503
                arg_tpm2_pin = r;
×
504

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

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

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

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

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

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

UNCOV
532
                arg_tpm2_measure_pcr = pcr;
×
533

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

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

539
                l = strv_split(optarg, ":");
×
540
                if (!l)
×
UNCOV
541
                        return log_oom();
×
542

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

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

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

557
        } else if ((val = startswith(option, "try-empty-password="))) {
65✔
558

559
                r = parse_boolean(val);
1✔
560
                if (r < 0) {
1✔
561
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
×
UNCOV
562
                        return 0;
×
563
                }
564

565
                arg_try_empty_password = r;
1✔
566

567
        } else if (streq(option, "try-empty-password"))
64✔
568
                arg_try_empty_password = true;
1✔
569
        else if ((val = startswith(option, "headless="))) {
63✔
570

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

577
                SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_HEADLESS, r);
55✔
578
        } else if (streq(option, "headless"))
8✔
579
                arg_ask_password_flags |= ASK_PASSWORD_HEADLESS;
7✔
580

581
        else if ((val = startswith(option, "token-timeout="))) {
1✔
582

583
                r = parse_sec_fix_0(val, &arg_token_timeout_usec);
×
UNCOV
584
                if (r < 0)
×
585
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
116✔
586

587
        } else if ((val = startswith(option, "link-volume-key="))) {
1✔
588
#ifdef HAVE_CRYPT_SET_KEYRING_TO_LINK
589
                _cleanup_free_ char *keyring = NULL, *key_type = NULL, *key_description = NULL;
×
UNCOV
590
                const char *sep;
×
591

592
                /* Stick with cryptsetup --link-vk-to-keyring format
593
                 * <keyring_description>::%<key_type>:<key_description>,
594
                 * where %<key_type> is optional and defaults to 'user'.
595
                 */
596
                sep = strstr(val, "::");
×
597
                if (!sep)
×
UNCOV
598
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse link-volume-key= option value: %s", val);
×
599

600
                /* cryptsetup (cli) supports <keyring_description> passed in various formats:
601
                 * - well-known keyrings prefixed with '@' (@u user, @s session, etc)
602
                 * - text descriptions prefixed with "%:" or "%keyring:".
603
                 * - text description with no prefix.
604
                 * - numeric keyring id (ignored in current patch set). */
605
                keyring = strndup(val, sep - val);
×
606
                if (!keyring)
×
UNCOV
607
                        return log_oom();
×
608

609
                /* add type prefix if missing (crypt_set_keyring_to_link() expects it) */
610
                if (!IN_SET(*keyring, '@', '%'))
×
611
                        if (!strprepend(&keyring, "%:"))
×
UNCOV
612
                                return log_oom();
×
613

UNCOV
614
                sep += 2;
×
615

616
                /* %<key_type> is optional (and defaults to 'user') */
UNCOV
617
                if (*sep == '%') {
×
618
                        /* must be separated by colon */
619
                        const char *c = strchr(sep, ':');
×
620
                        if (!c)
×
UNCOV
621
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse link-volume-key= option value: %s", val);
×
622

623
                        key_type = strndup(sep + 1, c - sep - 1);
×
624
                        if (!key_type)
×
UNCOV
625
                                return log_oom();
×
626

UNCOV
627
                        sep = c + 1;
×
628
                }
629

630
                key_description = strdup(sep);
×
631
                if (!key_description)
×
UNCOV
632
                        return log_oom();
×
633

634
                free_and_replace(arg_link_keyring, keyring);
×
635
                free_and_replace(arg_link_key_type, key_type);
×
UNCOV
636
                free_and_replace(arg_link_key_description, key_description);
×
637
#else
638
                log_error("Build lacks libcryptsetup support for linking volume keys in user specified kernel keyrings upon device activation, ignoring: %s", option);
639
#endif
640
        } else if (!streq(option, "x-initrd.attach"))
1✔
UNCOV
641
                log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
×
642

643
        return 0;
644
}
645

646
static int parse_crypt_config(const char *options) {
71✔
647
        assert(options);
71✔
648

649
        for (;;) {
363✔
650
                _cleanup_free_ char *word = NULL;
146✔
651
                int r;
217✔
652

653
                r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
217✔
654
                if (r < 0)
217✔
UNCOV
655
                        return log_error_errno(r, "Failed to parse options: %m");
×
656
                if (r == 0)
217✔
657
                        break;
658

659
                r = parse_one_option(word);
146✔
660
                if (r < 0)
146✔
661
                        return r;
662
        }
663

664
        /* sanity-check options */
665
        if (arg_type && !streq(arg_type, CRYPT_PLAIN)) {
71✔
666
                if (arg_offset != 0)
17✔
UNCOV
667
                      log_warning("offset= ignored with type %s", arg_type);
×
668
                if (arg_skip != 0)
17✔
UNCOV
669
                      log_warning("skip= ignored with type %s", arg_type);
×
670
        }
671

672
        if (arg_pkcs11_uri || arg_pkcs11_uri_auto) {
71✔
673
                /* If password-cache was not configured explicitly, default to no cache for PKCS#11 */
674
                if (!arg_password_cache_set)
6✔
675
                        arg_ask_password_flags &= ~(ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE);
6✔
676

677
                /* This prevents future backward-compatibility issues if we decide to allow caching for PKCS#11 */
678
                if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_ACCEPT_CACHED))
6✔
UNCOV
679
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
680
                                               "Password cache is not supported for PKCS#11 security tokens.");
681
        }
682

683
        return 0;
684
}
685

686
static char* disk_description(const char *path) {
10✔
687
        static const char name_fields[] =
10✔
688
                "DM_NAME\0"
689
                "ID_MODEL_FROM_DATABASE\0"
690
                "ID_MODEL\0";
691

692
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
10✔
693
        const char *name;
10✔
694
        struct stat st;
10✔
695

696
        assert(path);
10✔
697

698
        if (stat(path, &st) < 0)
10✔
699
                return NULL;
700

701
        if (!S_ISBLK(st.st_mode))
10✔
702
                return NULL;
703

UNCOV
704
        if (sd_device_new_from_stat_rdev(&device, &st) < 0)
×
705
                return NULL;
706

707
        if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) {
×
708
                _cleanup_free_ char *unescaped = NULL;
×
UNCOV
709
                ssize_t l;
×
710

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

715
                l = cunescape(name, UNESCAPE_RELAX, &unescaped);
×
716
                if (l < 0) {
×
717
                        log_debug_errno(l, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
×
UNCOV
718
                        return NULL;
×
719
                }
720

721
                if (!isempty(unescaped) && !string_has_cc(unescaped, NULL))
×
UNCOV
722
                        return TAKE_PTR(unescaped);
×
723
        }
724

725
        /* These need no unescaping. */
726
        NULSTR_FOREACH(i, name_fields)
×
727
                if (sd_device_get_property_value(device, i, &name) >= 0 &&
×
728
                    !isempty(name))
×
UNCOV
729
                        return strdup(name);
×
730

731
        return NULL;
732
}
733

734
static char *disk_mount_point(const char *label) {
10✔
735
        _cleanup_free_ char *device = NULL;
10✔
736
        _cleanup_endmntent_ FILE *f = NULL;
10✔
737
        struct mntent *m;
10✔
738

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

741
        device = strjoin("/dev/mapper/", label);
10✔
742
        if (!device)
10✔
743
                return NULL;
744

745
        f = setmntent(fstab_path(), "re");
20✔
746
        if (!f)
10✔
747
                return NULL;
748

749
        while ((m = getmntent(f)))
10✔
750
                if (path_equal(m->mnt_fsname, device))
×
UNCOV
751
                        return strdup(m->mnt_dir);
×
752

753
        return NULL;
754
}
755

756
static char *friendly_disk_name(const char *src, const char *vol) {
10✔
757
        _cleanup_free_ char *description = NULL, *mount_point = NULL;
10✔
758
        char *name_buffer = NULL;
10✔
759
        int r;
10✔
760

761
        assert(src);
10✔
762
        assert(vol);
10✔
763

764
        description = disk_description(src);
10✔
765
        mount_point = disk_mount_point(vol);
10✔
766

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

771
        if (mount_point && description)
10✔
772
                r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point);
×
773
        else if (mount_point)
×
UNCOV
774
                r = asprintf(&name_buffer, "%s on %s", vol, mount_point);
×
775
        else if (description)
10✔
UNCOV
776
                r = asprintf(&name_buffer, "%s (%s)", description, vol);
×
777
        else
778
                return strdup(vol);
10✔
UNCOV
779
        if (r < 0)
×
780
                return NULL;
781

UNCOV
782
        return name_buffer;
×
783
}
784

785
static PassphraseType check_registered_passwords(struct crypt_device *cd) {
14✔
786
        _cleanup_free_ bool *slots = NULL;
14✔
787
        int slot_max;
14✔
788
        PassphraseType passphrase_type = PASSPHRASE_NONE;
14✔
789

790
        assert(cd);
14✔
791

792
        if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2)) {
14✔
793
                log_debug("%s: not a LUKS2 device, only passphrases are supported", crypt_get_device_name(cd));
×
UNCOV
794
                return PASSPHRASE_REGULAR;
×
795
        }
796

797
        /* Search all used slots */
798
        assert_se((slot_max = crypt_keyslot_max(CRYPT_LUKS2)) > 0);
14✔
799
        slots = new(bool, slot_max);
14✔
800
        if (!slots)
14✔
UNCOV
801
                return log_oom();
×
802

803
        for (int slot = 0; slot < slot_max; slot++)
462✔
804
                slots[slot] = IN_SET(crypt_keyslot_status(cd, slot), CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST);
448✔
805

806
        /* Iterate all LUKS2 tokens and keep track of all their slots */
807
        for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
462✔
808
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
448✔
809
                const char *type;
448✔
810
                sd_json_variant *w, *z;
448✔
811
                int tk;
448✔
812

813
                tk = cryptsetup_get_token_as_json(cd, token, NULL, &v);
448✔
814
                if (IN_SET(tk, -ENOENT, -EINVAL))
448✔
815
                        continue;
439✔
816
                if (tk < 0) {
9✔
817
                        log_warning_errno(tk, "Failed to read JSON token data, ignoring: %m");
×
UNCOV
818
                        continue;
×
819
                }
820

821
                w = sd_json_variant_by_key(v, "type");
9✔
822
                if (!w || !sd_json_variant_is_string(w)) {
9✔
823
                        log_warning("Token JSON data lacks type field, ignoring.");
×
UNCOV
824
                        continue;
×
825
                }
826

827
                type = sd_json_variant_string(w);
9✔
828
                if (STR_IN_SET(type, "systemd-recovery", "systemd-pkcs11", "systemd-fido2", "systemd-tpm2")) {
9✔
829

830
                        /* At least exists one recovery key */
831
                        if (streq(type, "systemd-recovery"))
9✔
UNCOV
832
                                passphrase_type |= PASSPHRASE_RECOVERY_KEY;
×
833

834
                        w = sd_json_variant_by_key(v, "keyslots");
9✔
835
                        if (!w || !sd_json_variant_is_array(w)) {
9✔
836
                                log_warning("Token JSON data lacks keyslots field, ignoring.");
×
UNCOV
837
                                continue;
×
838
                        }
839

840
                        JSON_VARIANT_ARRAY_FOREACH(z, w) {
18✔
841
                                unsigned u;
9✔
842
                                int at;
9✔
843

844
                                if (!sd_json_variant_is_string(z)) {
9✔
845
                                        log_warning("Token JSON data's keyslot field is not an array of strings, ignoring.");
×
UNCOV
846
                                        continue;
×
847
                                }
848

849
                                at = safe_atou(sd_json_variant_string(z), &u);
9✔
850
                                if (at < 0) {
9✔
851
                                        log_warning_errno(at, "Token JSON data's keyslot field is not an integer formatted as string, ignoring.");
×
UNCOV
852
                                        continue;
×
853
                                }
854

855
                                if (u >= (unsigned) slot_max) {
9✔
856
                                        log_warning_errno(at, "Token JSON data's keyslot field exceeds the maximum value allowed, ignoring.");
×
UNCOV
857
                                        continue;
×
858
                                }
859

860
                                slots[u] = false;
9✔
861
                        }
862
                }
863
        }
864

865
        /* Check if any of the slots is not referenced by systemd tokens */
866
        for (int slot = 0; slot < slot_max; slot++)
14✔
867
                if (slots[slot]) {
14✔
868
                        passphrase_type |= PASSPHRASE_REGULAR;
14✔
869
                        break;
14✔
870
                }
871

872
        /* All the slots are referenced by systemd tokens, so if a recovery key is not enrolled,
873
         * we will not be able to enter a passphrase. */
874
        return passphrase_type;
875
}
876

877
static int get_password(
14✔
878
                const char *vol,
879
                const char *src,
880
                usec_t until,
881
                bool ignore_cached,
882
                PassphraseType passphrase_type,
883
                char ***ret) {
884

885
        _cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL, *id = NULL;
14✔
UNCOV
886
        _cleanup_strv_free_erase_ char **passwords = NULL;
×
887
        AskPasswordFlags flags = arg_ask_password_flags;
14✔
888
        int r;
14✔
889

890
        assert(vol);
14✔
891
        assert(src);
14✔
892
        assert(ret);
14✔
893

894
        if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_HEADLESS))
14✔
895
                return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Password querying disabled via 'headless' option.");
14✔
896

897
        friendly = friendly_disk_name(src, vol);
×
898
        if (!friendly)
×
UNCOV
899
                return log_oom();
×
900

901
        if (asprintf(&text, "Please enter %s for disk %s:", passphrase_type_to_string(passphrase_type), friendly) < 0)
×
UNCOV
902
                return log_oom();
×
903

904
        disk_path = cescape(src);
×
905
        if (!disk_path)
×
UNCOV
906
                return log_oom();
×
907

908
        id = strjoin("cryptsetup:", disk_path);
×
909
        if (!id)
×
UNCOV
910
                return log_oom();
×
911

UNCOV
912
        AskPasswordRequest req = {
×
913
                .tty_fd = -EBADF,
914
                .message = text,
915
                .icon = "drive-harddisk",
916
                .id = id,
917
                .keyring = "cryptsetup",
918
                .credential = "cryptsetup.passphrase",
919
                .until = until,
920
                .hup_fd = -EBADF,
921
        };
922

923
        if (ignore_cached)
×
UNCOV
924
                flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
×
925

926
        r = ask_password_auto(&req, flags, &passwords);
×
927
        if (r < 0)
×
UNCOV
928
                return log_error_errno(r, "Failed to query password: %m");
×
929

930
        if (arg_verify) {
×
UNCOV
931
                _cleanup_strv_free_erase_ char **passwords2 = NULL;
×
932

UNCOV
933
                assert(strv_length(passwords) == 1);
×
934

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

939
                free(id);
×
940
                id = strjoin("cryptsetup-verification:", disk_path);
×
941
                if (!id)
×
UNCOV
942
                        return log_oom();
×
943

944
                req.message = text;
×
UNCOV
945
                req.id = id;
×
946

947
                r = ask_password_auto(&req, flags, &passwords2);
×
948
                if (r < 0)
×
UNCOV
949
                        return log_error_errno(r, "Failed to query verification password: %m");
×
950

UNCOV
951
                assert(strv_length(passwords2) == 1);
×
952

953
                if (!streq(passwords[0], passwords2[0]))
×
UNCOV
954
                        return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN),
×
955
                                                 "Passwords did not match, retrying.");
956
        }
957

UNCOV
958
        strv_uniq(passwords);
×
959

960
        STRV_FOREACH(p, passwords) {
×
UNCOV
961
                char *c;
×
962

963
                if (strlen(*p)+1 >= arg_key_size)
×
UNCOV
964
                        continue;
×
965

966
                /* Pad password if necessary */
967
                c = new(char, arg_key_size);
×
UNCOV
968
                if (!c)
×
969
                        return log_oom();
14✔
970

971
                strncpy(c, *p, arg_key_size);
×
972
                erase_and_free(*p);
×
UNCOV
973
                *p = TAKE_PTR(c);
×
974
        }
975

UNCOV
976
        *ret = TAKE_PTR(passwords);
×
977

UNCOV
978
        return 0;
×
979
}
980

UNCOV
981
static int measure_volume_key(
×
982
                struct crypt_device *cd,
983
                const char *name,
984
                const void *volume_key,
985
                size_t volume_key_size) {
986

UNCOV
987
        int r;
×
988

989
        assert(cd);
×
990
        assert(name);
×
991
        assert(volume_key);
×
UNCOV
992
        assert(volume_key_size > 0);
×
993

994
        if (arg_tpm2_measure_pcr == UINT_MAX) {
×
995
                log_debug("Not measuring volume key, deactivated.");
×
UNCOV
996
                return 0;
×
997
        }
998

999
        r = efi_measured_uki(LOG_WARNING);
×
UNCOV
1000
        if (r < 0)
×
1001
                return r;
1002
        if (r == 0) {
×
1003
                log_debug("Kernel stub did not measure kernel image into the expected PCR, skipping userspace measurement, too.");
×
UNCOV
1004
                return 0;
×
1005
        }
1006

1007
#if HAVE_TPM2
1008
        _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL;
×
1009
        r = tpm2_context_new_or_warn(arg_tpm2_device, &c);
×
UNCOV
1010
        if (r < 0)
×
1011
                return r;
1012

1013
        _cleanup_strv_free_ char **l = NULL;
×
1014
        if (strv_isempty(arg_tpm2_measure_banks)) {
×
1015
                r = tpm2_get_good_pcr_banks_strv(c, UINT32_C(1) << arg_tpm2_measure_pcr, &l);
×
1016
                if (r < 0)
×
UNCOV
1017
                        return log_error_errno(r, "Could not verify pcr banks: %m");
×
1018
        }
1019

1020
        _cleanup_free_ char *joined = strv_join(l ?: arg_tpm2_measure_banks, ", ");
×
1021
        if (!joined)
×
UNCOV
1022
                return log_oom();
×
1023

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

1028
        _cleanup_free_ char *escaped = NULL;
×
1029
        escaped = xescape(name, ":"); /* avoid ambiguity around ":" once we join things below */
×
1030
        if (!escaped)
×
UNCOV
1031
                return log_oom();
×
1032

1033
        _cleanup_free_ char *s = NULL;
×
1034
        s = strjoin("cryptsetup:", escaped, ":", strempty(crypt_get_uuid(cd)));
×
1035
        if (!s)
×
UNCOV
1036
                return log_oom();
×
1037

1038
        r = tpm2_extend_bytes(c, l ?: arg_tpm2_measure_banks, arg_tpm2_measure_pcr, s, SIZE_MAX, volume_key, volume_key_size, TPM2_EVENT_VOLUME_KEY, s);
×
1039
        if (r < 0)
×
UNCOV
1040
                return log_error_errno(r, "Could not extend PCR: %m");
×
1041

UNCOV
1042
        log_struct(LOG_INFO,
×
1043
                   LOG_MESSAGE_ID(SD_MESSAGE_TPM_PCR_EXTEND_STR),
1044
                   LOG_MESSAGE("Successfully extended PCR index %u with '%s' and volume key (banks %s).", arg_tpm2_measure_pcr, s, joined),
1045
                   LOG_ITEM("MEASURING=%s", s),
1046
                   LOG_ITEM("PCR=%u", arg_tpm2_measure_pcr),
1047
                   LOG_ITEM("BANKS=%s", joined));
1048

1049
        return 0;
1050
#else
1051
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring.");
1052
#endif
1053
}
1054

1055
static int log_external_activation(int r, const char *volume) {
×
UNCOV
1056
        assert(volume);
×
1057

1058
        log_notice_errno(r, "Volume '%s' has been activated externally while we have been trying to activate it.", volume);
×
UNCOV
1059
        return 0;
×
1060
}
1061

UNCOV
1062
static int measured_crypt_activate_by_volume_key(
×
1063
                struct crypt_device *cd,
1064
                const char *name,
1065
                const void *volume_key,
1066
                size_t volume_key_size,
1067
                uint32_t flags) {
1068

UNCOV
1069
        int r;
×
1070

1071
        assert(cd);
×
UNCOV
1072
        assert(name);
×
1073

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

1076
        r = crypt_activate_by_volume_key(cd, name, volume_key, volume_key_size, flags);
×
1077
        if (r == -EEXIST) /* volume is already active */
×
1078
                return log_external_activation(r, name);
×
UNCOV
1079
        if (r < 0)
×
1080
                return r;
1081

1082
        if (volume_key_size == 0) {
×
1083
                log_debug("Not measuring volume key, none specified.");
×
UNCOV
1084
                return r;
×
1085
        }
1086

1087
        (void) measure_volume_key(cd, name, volume_key, volume_key_size); /* OK if fails */
×
UNCOV
1088
        return r;
×
1089
}
1090

1091
static int measured_crypt_activate_by_passphrase(
25✔
1092
                struct crypt_device *cd,
1093
                const char *name,
1094
                int keyslot,
1095
                const char *passphrase,
1096
                size_t passphrase_size,
1097
                uint32_t flags) {
1098

1099
        _cleanup_(erase_and_freep) void *vk = NULL;
25✔
1100
        size_t vks;
25✔
1101
        int r;
25✔
1102

1103
        assert(cd);
25✔
1104

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

1110
        if (arg_tpm2_measure_pcr == UINT_MAX) {
25✔
1111
                log_debug("Not measuring volume key, deactivated.");
25✔
1112
                goto shortcut;
25✔
1113
        }
1114

1115
        r = crypt_get_volume_key_size(cd);
×
UNCOV
1116
        if (r < 0)
×
1117
                return r;
1118
        if (r == 0) {
×
1119
                log_debug("Not measuring volume key, none defined.");
×
UNCOV
1120
                goto shortcut;
×
1121
        }
1122

1123
        vk = malloc(vks = r);
×
UNCOV
1124
        if (!vk)
×
1125
                return -ENOMEM;
1126

1127
        r = crypt_volume_key_get(cd, keyslot, vk, &vks, passphrase, passphrase_size);
×
UNCOV
1128
        if (r < 0)
×
1129
                return r;
1130

UNCOV
1131
        return measured_crypt_activate_by_volume_key(cd, name, vk, vks, flags);
×
1132

1133
shortcut:
25✔
1134
        r = crypt_activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags);
25✔
1135
        if (r == -EEXIST) /* volume is already active */
25✔
UNCOV
1136
                return log_external_activation(r, name);
×
1137
        return r;
1138
}
1139

UNCOV
1140
static int attach_tcrypt(
×
1141
                struct crypt_device *cd,
1142
                const char *name,
1143
                TokenType token_type,
1144
                const char *key_file,
1145
                const struct iovec *key_data,
1146
                char **passwords,
1147
                uint32_t flags) {
1148

1149
        int r = 0;
×
1150
        _cleanup_(erase_and_freep) char *passphrase = NULL;
×
UNCOV
1151
        struct crypt_params_tcrypt params = {
×
1152
                .flags = CRYPT_TCRYPT_LEGACY_MODES,
1153
                .keyfiles = (const char **)arg_tcrypt_keyfiles,
UNCOV
1154
                .keyfiles_count = strv_length(arg_tcrypt_keyfiles)
×
1155
        };
1156

1157
        assert(cd);
×
1158
        assert(name);
×
UNCOV
1159
        assert(key_file || key_data || !strv_isempty(passwords));
×
1160

UNCOV
1161
        if (token_type >= 0)
×
1162
                /* Ask for a regular password */
UNCOV
1163
                return log_error_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1164
                                       "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11/fido2/tpm2 support.");
1165

1166
        if (arg_tcrypt_hidden)
×
UNCOV
1167
                params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
×
1168

1169
        if (arg_tcrypt_system)
×
UNCOV
1170
                params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER;
×
1171

1172
        if (arg_tcrypt_veracrypt)
×
UNCOV
1173
                params.flags |= CRYPT_TCRYPT_VERA_MODES;
×
1174

1175
        if (arg_tcrypt_veracrypt && arg_tcrypt_veracrypt_pim != 0)
×
UNCOV
1176
                params.veracrypt_pim = arg_tcrypt_veracrypt_pim;
×
1177

1178
        if (key_data) {
×
1179
                params.passphrase = key_data->iov_base;
×
1180
                params.passphrase_size = key_data->iov_len;
×
1181
                r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
1182
        } else if (key_file) {
×
1183
                r = read_one_line_file(key_file, &passphrase);
×
1184
                if (r < 0) {
×
1185
                        log_error_errno(r, "Failed to read password file '%s': %m", key_file);
×
UNCOV
1186
                        return -EAGAIN; /* log with the actual error, but return EAGAIN */
×
1187
                }
1188
                params.passphrase = passphrase;
×
1189
                params.passphrase_size = strlen(passphrase);
×
UNCOV
1190
                r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
1191
        } else {
1192
                r = -EINVAL;
1193
                STRV_FOREACH(p, passwords){
×
1194
                        params.passphrase = *p;
×
1195
                        params.passphrase_size = strlen(*p);
×
1196
                        r = crypt_load(cd, CRYPT_TCRYPT, &params);
×
UNCOV
1197
                        if (r >= 0)
×
1198
                                break;
1199
                }
1200
        }
1201

1202
        if (r < 0) {
×
1203
                if (r == -EPERM) {
×
1204
                        if (key_data)
×
1205
                                log_error_errno(r, "Failed to activate using discovered key. (Key not correct?)");
×
1206
                        else if (key_file)
×
UNCOV
1207
                                log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file);
×
1208
                        else
UNCOV
1209
                                log_error_errno(r, "Failed to activate using supplied passwords.");
×
1210

UNCOV
1211
                        return r;
×
1212
                }
1213

UNCOV
1214
                return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd));
×
1215
        }
1216

1217
        r = measured_crypt_activate_by_volume_key(cd, name, NULL, 0, flags);
×
1218
        if (r < 0)
×
UNCOV
1219
                return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd));
×
1220

1221
        return 0;
1222
}
1223

1224
static char *make_bindname(const char *volume, TokenType token_type) {
45✔
1225
        const char *token_type_name = token_type_to_string(token_type), *suffix;
45✔
1226
        char *bindname;
45✔
1227
        int r;
45✔
1228

1229
        switch (token_type) {
45✔
1230

1231
        case TOKEN_FIDO2:
1232
                suffix = "-salt";
1233
                break;
1234

1235
        default:
1236
                suffix = NULL;
45✔
1237
        }
1238

1239
        r = asprintf(&bindname,
45✔
1240
                     "@%" PRIx64"/cryptsetup%s%s%s/%s",
1241
                     random_u64(),
1242
                     token_type_name ? "-" : "",
1243
                     strempty(token_type_name),
1244
                     strempty(suffix),
1245
                     volume);
1246
        if (r < 0)
45✔
1247
                return NULL;
45✔
1248

1249
        return bindname;
45✔
1250
}
1251

UNCOV
1252
static int make_security_device_monitor(
×
1253
                sd_event **ret_event,
1254
                sd_device_monitor **ret_monitor) {
1255
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
1256
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
UNCOV
1257
        int r;
×
1258

1259
        assert(ret_event);
×
UNCOV
1260
        assert(ret_monitor);
×
1261

1262
        /* Waits for a device with "security-device" tag to show up in udev */
UNCOV
1263
        log_debug("Creating device monitor for tag 'security-device' with timeout %s",
×
1264
                  FORMAT_TIMESPAN(arg_token_timeout_usec, 1*USEC_PER_SEC));
1265

1266
        r = sd_event_default(&event);
×
1267
        if (r < 0)
×
UNCOV
1268
                return log_error_errno(r, "Failed to allocate event loop: %m");
×
1269

1270
        r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
×
1271
        if (r < 0)
×
UNCOV
1272
                return log_error_errno(r, "Failed to install timeout event source: %m");
×
1273

1274
        r = sd_device_monitor_new(&monitor);
×
1275
        if (r < 0)
×
UNCOV
1276
                return log_error_errno(r, "Failed to allocate device monitor: %m");
×
1277

UNCOV
1278
        (void) sd_device_monitor_set_description(monitor, "security-device");
×
1279

1280
        r = sd_device_monitor_filter_add_match_tag(monitor, "security-device");
×
1281
        if (r < 0)
×
UNCOV
1282
                return log_error_errno(r, "Failed to configure device monitor: %m");
×
1283

1284
        r = sd_device_monitor_attach_event(monitor, event);
×
1285
        if (r < 0)
×
UNCOV
1286
                return log_error_errno(r, "Failed to attach device monitor: %m");
×
1287

1288
        r = sd_device_monitor_start(monitor, NULL, NULL);
×
1289
        if (r < 0)
×
UNCOV
1290
                return log_error_errno(r, "Failed to start device monitor: %m");
×
1291

1292
        *ret_event = TAKE_PTR(event);
×
1293
        *ret_monitor = TAKE_PTR(monitor);
×
UNCOV
1294
        return 0;
×
1295
}
1296

UNCOV
1297
static int run_security_device_monitor(
×
1298
                sd_event *event,
1299
                sd_device_monitor *monitor) {
1300
        bool processed = false;
×
UNCOV
1301
        int r;
×
1302

1303
        assert(event);
×
UNCOV
1304
        assert(monitor);
×
1305

1306
        /* Runs the event loop for the device monitor until either something happens, or the timeout is
1307
         * hit. */
1308

1309
        for (;;) {
×
UNCOV
1310
                int x;
×
1311

1312
                r = sd_event_get_exit_code(event, &x);
×
1313
                if (r < 0) {
×
1314
                        if (r != -ENODATA)
×
UNCOV
1315
                                return log_error_errno(r, "Failed to query exit code from event loop: %m");
×
1316

1317
                        /* On ENODATA we aren't told to exit yet. */
1318
                } else {
1319
                        assert(x == -ETIMEDOUT);
×
UNCOV
1320
                        return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1321
                                                "Timed out waiting for security device, aborting security device based authentication attempt.");
1322
                }
1323

1324
                /* Wait for one event, and then eat all subsequent events until there are no further ones */
1325
                r = sd_event_run(event, processed ? 0 : UINT64_MAX);
×
1326
                if (r < 0)
×
1327
                        return log_error_errno(r, "Failed to run event loop: %m");
×
UNCOV
1328
                if (r == 0) /* no events queued anymore */
×
1329
                        return 0;
1330

UNCOV
1331
                processed = true;
×
1332
        }
1333
}
1334

1335
static bool use_token_plugins(void) {
60✔
1336

1337
#if HAVE_TPM2
1338
        /* Currently, there's no way for us to query the volume key when plugins are used. Hence don't use
1339
         * plugins, if measurement has been requested. */
1340
        if (arg_tpm2_measure_pcr != UINT_MAX)
60✔
1341
                return false;
1342
#endif
1343

1344
        /* Disable tokens if we're in FIDO2 mode with manual parameters. */
1345
        if (arg_fido2_cid)
60✔
1346
                return false;
1347

1348
#if HAVE_LIBCRYPTSETUP_PLUGINS
1349
        int r;
60✔
1350

1351
        /* Permit a way to disable libcryptsetup token module support, for debugging purposes. */
1352
        r = getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE");
60✔
1353
        if (r < 0 && r != -ENXIO)
60✔
UNCOV
1354
                log_debug_errno(r, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE env var: %m");
×
1355
        if (r == 0)
60✔
1356
                return false;
1357

1358
        return crypt_token_external_path();
58✔
1359
#else
1360
        return false;
1361
#endif
1362
}
1363

1364
#if HAVE_LIBCRYPTSETUP_PLUGINS
1365
static int acquire_pins_from_env_variable(char ***ret_pins) {
21✔
1366
        _cleanup_(erase_and_freep) char *envpin = NULL;
21✔
1367
        _cleanup_strv_free_erase_ char **pins = NULL;
21✔
1368
        int r;
21✔
1369

1370
        assert(ret_pins);
21✔
1371

1372
        r = getenv_steal_erase("PIN", &envpin);
21✔
1373
        if (r < 0)
21✔
UNCOV
1374
                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
×
1375
        if (r > 0) {
21✔
1376
                pins = strv_new(envpin);
16✔
1377
                if (!pins)
16✔
UNCOV
1378
                        return log_oom();
×
1379
        }
1380

1381
        *ret_pins = TAKE_PTR(pins);
21✔
1382

1383
        return 0;
21✔
1384
}
1385
#endif
1386

1387
static int crypt_activate_by_token_pin_ask_password(
58✔
1388
                struct crypt_device *cd,
1389
                const char *name,
1390
                const char *type,
1391
                usec_t until,
1392
                void *userdata,
1393
                uint32_t activation_flags,
1394
                const char *message,
1395
                const char *keyring,
1396
                const char *credential) {
1397

1398
#if HAVE_LIBCRYPTSETUP_PLUGINS
1399
        AskPasswordFlags flags = arg_ask_password_flags;
58✔
1400
        _cleanup_strv_free_erase_ char **pins = NULL;
58✔
1401
        int r;
58✔
1402

1403
        r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, /* pin=*/ NULL, /* pin_size= */ 0, userdata, activation_flags);
58✔
1404
        if (r > 0) /* returns unlocked keyslot id on success */
58✔
1405
                return 0;
1406
        if (r == -EEXIST) /* volume is already active */
35✔
UNCOV
1407
                return log_external_activation(r, name);
×
1408
        if (r != -ENOANO) /* needs pin or pin is wrong */
35✔
1409
                return r;
1410

1411
        r = acquire_pins_from_env_variable(&pins);
21✔
1412
        if (r < 0)
21✔
1413
                return r;
1414

1415
        STRV_FOREACH(p, pins) {
21✔
1416
                r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), userdata, activation_flags);
16✔
1417
                if (r > 0) /* returns unlocked keyslot id on success */
16✔
1418
                        return 0;
1419
                if (r == -EEXIST) /* volume is already active */
5✔
UNCOV
1420
                        return log_external_activation(r, name);
×
1421
                if (r != -ENOANO) /* needs pin or pin is wrong */
5✔
1422
                        return r;
1423
        }
1424

1425
        if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_HEADLESS))
5✔
1426
                return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the '$PIN' environment variable.");
5✔
1427

1428
        for (;;) {
×
UNCOV
1429
                pins = strv_free_erase(pins);
×
1430

UNCOV
1431
                AskPasswordRequest req = {
×
1432
                        .tty_fd = -EBADF,
1433
                        .message = message,
1434
                        .icon = "drive-harddisk",
1435
                        .keyring = keyring,
1436
                        .credential = credential,
1437
                        .until = until,
1438
                        .hup_fd = -EBADF,
1439
                };
1440

1441
                r = ask_password_auto(&req, flags, &pins);
×
1442
                if (r < 0)
×
UNCOV
1443
                        return r;
×
1444

1445
                STRV_FOREACH(p, pins) {
×
1446
                        r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), userdata, activation_flags);
×
UNCOV
1447
                        if (r > 0) /* returns unlocked keyslot id on success */
×
1448
                                return 0;
1449
                        if (r == -EEXIST) /* volume is already active */
×
1450
                                return log_external_activation(r, name);
×
UNCOV
1451
                        if (r != -ENOANO) /* needs pin or pin is wrong */
×
1452
                                return r;
1453
                }
1454

UNCOV
1455
                flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
×
1456
        }
1457
        return r;
1458
#else
1459
        return -EOPNOTSUPP;
1460
#endif
1461
}
1462

UNCOV
1463
static int attach_luks2_by_fido2_via_plugin(
×
1464
                struct crypt_device *cd,
1465
                const char *name,
1466
                usec_t until,
1467
                void *userdata,
1468
                uint32_t activation_flags) {
1469

UNCOV
1470
        return crypt_activate_by_token_pin_ask_password(
×
1471
                        cd,
1472
                        name,
1473
                        "systemd-fido2",
1474
                        until,
1475
                        userdata,
1476
                        activation_flags,
1477
                        "Please enter security token PIN:",
1478
                        "fido2-pin",
1479
                        "cryptsetup.fido2-pin");
1480
}
1481

UNCOV
1482
static int attach_luks_or_plain_or_bitlk_by_fido2(
×
1483
                struct crypt_device *cd,
1484
                const char *name,
1485
                const char *key_file,
1486
                const struct iovec *key_data,
1487
                usec_t until,
1488
                uint32_t flags,
1489
                bool pass_volume_key) {
1490

1491
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
1492
        _cleanup_(erase_and_freep) void *decrypted_key = NULL;
×
1493
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
1494
        size_t decrypted_key_size;
×
1495
        _cleanup_free_ char *friendly = NULL;
×
1496
        int keyslot = arg_key_slot, r;
×
UNCOV
1497
        bool use_libcryptsetup_plugin = use_token_plugins();
×
1498

1499
        assert(cd);
×
1500
        assert(name);
×
UNCOV
1501
        assert(arg_fido2_device || arg_fido2_device_auto);
×
1502

1503
        if (arg_fido2_cid && !key_file && !iovec_is_set(key_data))
×
UNCOV
1504
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1505
                                        "FIDO2 mode with manual parameters selected, but no keyfile specified, refusing.");
1506

1507
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
×
1508
        if (!friendly)
×
UNCOV
1509
                return log_oom();
×
1510

1511
        for (;;) {
×
1512
                if (use_libcryptsetup_plugin && !arg_fido2_cid) {
×
1513
                        r = attach_luks2_by_fido2_via_plugin(cd, name, until, arg_fido2_device, flags);
×
1514
                        if (IN_SET(r, -ENOTUNIQ, -ENXIO, -ENOENT))
×
UNCOV
1515
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1516
                                                       "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1517

1518
                } else {
1519
                        if (arg_fido2_cid)
×
UNCOV
1520
                                r = acquire_fido2_key(
×
1521
                                                name,
1522
                                                friendly,
1523
                                                arg_fido2_device,
1524
                                                arg_fido2_rp_id,
1525
                                                arg_fido2_cid, arg_fido2_cid_size,
1526
                                                key_file, arg_keyfile_size, arg_keyfile_offset,
1527
                                                key_data,
1528
                                                until,
1529
                                                arg_fido2_manual_flags,
1530
                                                "cryptsetup.fido2-pin",
1531
                                                arg_ask_password_flags,
1532
                                                &decrypted_key,
1533
                                                &decrypted_key_size);
1534
                        else
UNCOV
1535
                                r = acquire_fido2_key_auto(
×
1536
                                                cd,
1537
                                                name,
1538
                                                friendly,
1539
                                                arg_fido2_device,
1540
                                                until,
1541
                                                "cryptsetup.fido2-pin",
1542
                                                arg_ask_password_flags,
1543
                                                &decrypted_key,
1544
                                                &decrypted_key_size);
UNCOV
1545
                        if (r >= 0)
×
1546
                                break;
1547
                }
1548

UNCOV
1549
                if (r != -EAGAIN) /* EAGAIN means: token not found */
×
1550
                        return r;
1551

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

UNCOV
1556
                        assert(!event);
×
1557

1558
                        r = make_security_device_monitor(&event, &monitor);
×
UNCOV
1559
                        if (r < 0)
×
1560
                                return r;
1561

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

1564
                        /* Let's immediately rescan in case the token appeared in the time we needed
1565
                         * to create and configure the monitor */
UNCOV
1566
                        continue;
×
1567
                }
1568

1569
                r = run_security_device_monitor(event, monitor);
×
UNCOV
1570
                if (r < 0)
×
1571
                        return r;
1572

UNCOV
1573
                log_debug("Got one or more potentially relevant udev events, rescanning FIDO2...");
×
1574
        }
1575

1576
        if (pass_volume_key)
×
UNCOV
1577
                r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags);
×
1578
        else {
1579
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
×
UNCOV
1580
                ssize_t base64_encoded_size;
×
1581

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

1584
                base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
×
1585
                if (base64_encoded_size < 0)
×
UNCOV
1586
                        return log_oom();
×
1587

UNCOV
1588
                r = measured_crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, base64_encoded_size, flags);
×
1589
        }
1590
        if (r == -EPERM) {
×
1591
                log_error_errno(r, "Failed to activate with FIDO2 decrypted key. (Key incorrect?)");
×
UNCOV
1592
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
1593
        }
1594
        if (r < 0)
×
UNCOV
1595
                return log_error_errno(r, "Failed to activate with FIDO2 acquired key: %m");
×
1596

1597
        return 0;
1598
}
1599

UNCOV
1600
static int attach_luks2_by_pkcs11_via_plugin(
×
1601
                struct crypt_device *cd,
1602
                const char *name,
1603
                const char *friendly_name,
1604
                usec_t until,
1605
                const char *askpw_credential,
1606
                uint32_t flags) {
1607

1608
#if HAVE_LIBCRYPTSETUP_PLUGINS
UNCOV
1609
        int r;
×
1610

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

UNCOV
1614
        systemd_pkcs11_plugin_params params = {
×
1615
                .friendly_name = friendly_name,
1616
                .until = until,
1617
                .askpw_credential = askpw_credential,
1618
                .askpw_flags = arg_ask_password_flags,
1619
        };
1620

1621
        r = crypt_activate_by_token_pin(cd, name, "systemd-pkcs11", CRYPT_ANY_TOKEN, NULL, 0, &params, flags);
×
UNCOV
1622
        if (r > 0) /* returns unlocked keyslot id on success */
×
1623
                r = 0;
1624
        if (r == -EEXIST) /* volume is already active */
×
UNCOV
1625
                r = log_external_activation(r, name);
×
1626

1627
        return r;
1628
#else
1629
        return -EOPNOTSUPP;
1630
#endif
1631
}
1632

UNCOV
1633
static int attach_luks_or_plain_or_bitlk_by_pkcs11(
×
1634
                struct crypt_device *cd,
1635
                const char *name,
1636
                const char *key_file,
1637
                const struct iovec *key_data,
1638
                usec_t until,
1639
                uint32_t flags,
1640
                bool pass_volume_key) {
1641

1642
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
1643
        _cleanup_free_ char *friendly = NULL, *discovered_uri = NULL;
×
1644
        size_t decrypted_key_size = 0, discovered_key_size = 0;
×
1645
        _cleanup_(erase_and_freep) void *decrypted_key = NULL;
×
1646
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
1647
        _cleanup_free_ void *discovered_key = NULL;
×
1648
        struct iovec discovered_key_data = {};
×
1649
        int keyslot = arg_key_slot, r;
×
1650
        const char *uri = NULL;
×
UNCOV
1651
        bool use_libcryptsetup_plugin = use_token_plugins();
×
1652

1653
        assert(cd);
×
1654
        assert(name);
×
UNCOV
1655
        assert(arg_pkcs11_uri || arg_pkcs11_uri_auto);
×
1656

1657
        if (arg_pkcs11_uri_auto) {
×
1658
                if (!use_libcryptsetup_plugin) {
×
1659
                        r = find_pkcs11_auto_data(cd, &discovered_uri, &discovered_key, &discovered_key_size, &keyslot);
×
1660
                        if (IN_SET(r, -ENOTUNIQ, -ENXIO))
×
UNCOV
1661
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1662
                                                       "Automatic PKCS#11 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
UNCOV
1663
                        if (r < 0)
×
1664
                                return r;
1665

1666
                        uri = discovered_uri;
×
1667
                        discovered_key_data = IOVEC_MAKE(discovered_key, discovered_key_size);
×
UNCOV
1668
                        key_data = &discovered_key_data;
×
1669
                }
1670
        } else {
UNCOV
1671
                uri = arg_pkcs11_uri;
×
1672

1673
                if (!key_file && !iovec_is_set(key_data))
×
UNCOV
1674
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 mode selected but no key file specified, refusing.");
×
1675
        }
1676

1677
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
×
1678
        if (!friendly)
×
UNCOV
1679
                return log_oom();
×
1680

1681
        for (;;) {
×
1682
                if (use_libcryptsetup_plugin && arg_pkcs11_uri_auto)
×
UNCOV
1683
                        r = attach_luks2_by_pkcs11_via_plugin(
×
1684
                                        cd,
1685
                                        name,
1686
                                        friendly,
1687
                                        until,
1688
                                        "cryptsetup.pkcs11-pin",
1689
                                        flags);
1690
                else {
UNCOV
1691
                        r = decrypt_pkcs11_key(
×
1692
                                        name,
1693
                                        friendly,
1694
                                        uri,
1695
                                        key_file, arg_keyfile_size, arg_keyfile_offset,
1696
                                        key_data,
1697
                                        until,
1698
                                        arg_ask_password_flags,
1699
                                        &decrypted_key, &decrypted_key_size);
UNCOV
1700
                        if (r >= 0)
×
1701
                                break;
1702
                }
1703

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

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

UNCOV
1711
                        assert(!event);
×
1712

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

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

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

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

UNCOV
1729
                log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11...");
×
1730
        }
UNCOV
1731
        assert(decrypted_key);
×
1732

1733
        if (pass_volume_key)
×
UNCOV
1734
                r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags);
×
1735
        else {
1736
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
×
UNCOV
1737
                ssize_t base64_encoded_size;
×
1738

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

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

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

1760
        return 0;
1761
}
1762

UNCOV
1763
static int make_tpm2_device_monitor(
×
1764
                sd_event **ret_event,
1765
                sd_device_monitor **ret_monitor) {
1766

1767
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
×
1768
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
×
UNCOV
1769
        int r;
×
1770

1771
        assert(ret_event);
×
UNCOV
1772
        assert(ret_monitor);
×
1773

1774
        r = sd_event_default(&event);
×
1775
        if (r < 0)
×
UNCOV
1776
                return log_error_errno(r, "Failed to allocate event loop: %m");
×
1777

1778
        r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
×
1779
        if (r < 0)
×
UNCOV
1780
                return log_error_errno(r, "Failed to install timeout event source: %m");
×
1781

1782
        r = sd_device_monitor_new(&monitor);
×
1783
        if (r < 0)
×
UNCOV
1784
                return log_error_errno(r, "Failed to allocate device monitor: %m");
×
1785

UNCOV
1786
        (void) sd_device_monitor_set_description(monitor, "tpmrm");
×
1787

1788
        r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "tpmrm", NULL);
×
1789
        if (r < 0)
×
UNCOV
1790
                return log_error_errno(r, "Failed to configure device monitor: %m");
×
1791

1792
        r = sd_device_monitor_attach_event(monitor, event);
×
1793
        if (r < 0)
×
UNCOV
1794
                return log_error_errno(r, "Failed to attach device monitor: %m");
×
1795

1796
        r = sd_device_monitor_start(monitor, NULL, NULL);
×
1797
        if (r < 0)
×
UNCOV
1798
                return log_error_errno(r, "Failed to start device monitor: %m");
×
1799

1800
        *ret_event = TAKE_PTR(event);
×
1801
        *ret_monitor = TAKE_PTR(monitor);
×
UNCOV
1802
        return 0;
×
1803
}
1804

1805
static int attach_luks2_by_tpm2_via_plugin(
10✔
1806
                struct crypt_device *cd,
1807
                const char *name,
1808
                usec_t until,
1809
                uint32_t flags) {
1810

1811
#if HAVE_LIBCRYPTSETUP_PLUGINS
1812
        systemd_tpm2_plugin_params params = {
10✔
1813
                .search_pcr_mask = arg_tpm2_pcr_mask,
1814
                .device = arg_tpm2_device,
1815
                .signature_path = arg_tpm2_signature,
1816
                .pcrlock_path = arg_tpm2_pcrlock,
1817
        };
1818

1819
        if (!use_token_plugins())
10✔
1820
                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1✔
1821
                                       "libcryptsetup has external plugins support disabled.");
1822

1823
        return crypt_activate_by_token_pin_ask_password(
9✔
1824
                        cd,
1825
                        name,
1826
                        "systemd-tpm2",
1827
                        until,
1828
                        &params,
1829
                        flags,
1830
                        "Please enter TPM2 PIN:",
1831
                        "tpm2-pin",
1832
                        "cryptsetup.tpm2-pin");
1833
#else
1834
        return -EOPNOTSUPP;
1835
#endif
1836
}
1837

1838
static int attach_luks_or_plain_or_bitlk_by_tpm2(
10✔
1839
                struct crypt_device *cd,
1840
                const char *name,
1841
                const char *key_file,
1842
                const struct iovec *key_data,
1843
                usec_t until,
1844
                uint32_t flags,
1845
                bool pass_volume_key) {
1846

1847
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
10✔
UNCOV
1848
        _cleanup_(iovec_done_erase) struct iovec decrypted_key = {};
×
1849
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
10✔
1850
        _cleanup_free_ char *friendly = NULL;
10✔
1851
        int keyslot = arg_key_slot, r;
10✔
1852

1853
        assert(cd);
10✔
1854
        assert(name);
10✔
1855
        assert(arg_tpm2_device || arg_tpm2_device_auto);
10✔
1856

1857
        friendly = friendly_disk_name(crypt_get_device_name(cd), name);
10✔
1858
        if (!friendly)
10✔
UNCOV
1859
                return log_oom();
×
1860

1861
        for (;;) {
10✔
1862
                if (key_file || iovec_is_set(key_data)) {
10✔
1863
                        /* If key data is specified, use that */
1864

UNCOV
1865
                        r = acquire_tpm2_key(
×
1866
                                        name,
1867
                                        arg_tpm2_device,
UNCOV
1868
                                        arg_tpm2_pcr_mask == UINT32_MAX ? TPM2_PCR_MASK_DEFAULT_LEGACY : arg_tpm2_pcr_mask,
×
1869
                                        UINT16_MAX,
1870
                                        /* pubkey= */ NULL,
1871
                                        /* pubkey_pcr_mask= */ 0,
1872
                                        /* signature_path= */ NULL,
1873
                                        /* pcrlock_path= */ NULL,
1874
                                        /* primary_alg= */ 0,
1875
                                        key_file, arg_keyfile_size, arg_keyfile_offset,
1876
                                        key_data, /* n_blobs= */ 1,
1877
                                        /* policy_hash= */ NULL, /* we don't know the policy hash */
1878
                                        /* n_policy_hash= */ 0,
1879
                                        /* salt= */ NULL,
1880
                                        /* srk= */ NULL,
1881
                                        /* pcrlock_nv= */ NULL,
1882
                                        arg_tpm2_pin ? TPM2_FLAGS_USE_PIN : 0,
1883
                                        until,
1884
                                        "cryptsetup.tpm2-pin",
1885
                                        arg_ask_password_flags,
1886
                                        &decrypted_key);
UNCOV
1887
                        if (r >= 0)
×
1888
                                break;
1889
                        if (IN_SET(r, -EACCES, -ENOLCK))
×
1890
                                return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
×
1891
                        if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */
×
UNCOV
1892
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking.");
×
1893
                        /* EAGAIN means: no tpm2 chip found */
1894
                        if (r != -EAGAIN) {
×
1895
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
×
UNCOV
1896
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
×
1897
                        }
1898
                } else {
1899
                        r = attach_luks2_by_tpm2_via_plugin(cd, name, until, flags);
10✔
1900
                        if (r >= 0)
10✔
1901
                                return 0;
1902
                        /* EAGAIN     means: no tpm2 chip found
1903
                         * EOPNOTSUPP means: no libcryptsetup plugins support */
1904
                        if (r == -ENXIO)
10✔
UNCOV
1905
                                return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1906
                                                        "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking.");
1907
                        if (r == -ENOENT)
10✔
1908
                                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
3✔
1909
                                                       "No TPM2 metadata enrolled in LUKS2 header or TPM2 support not available, falling back to traditional unlocking.");
1910
                        if (!IN_SET(r, -EOPNOTSUPP, -EAGAIN)) {
7✔
1911
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
6✔
1912
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
6✔
1913
                        }
1914
                }
1915

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

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

1924
                        for (;;) {
1✔
UNCOV
1925
                                _cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {};
×
1926
                                struct iovec *blobs = NULL, *policy_hash = NULL;
1✔
1927
                                uint32_t hash_pcr_mask, pubkey_pcr_mask;
1✔
1928
                                size_t n_blobs = 0, n_policy_hash = 0;
1✔
1929
                                uint16_t pcr_bank, primary_alg;
1✔
1930
                                TPM2Flags tpm2_flags;
1✔
1931

1932
                                CLEANUP_ARRAY(blobs, n_blobs, iovec_array_free);
1✔
1933
                                CLEANUP_ARRAY(policy_hash, n_policy_hash, iovec_array_free);
1✔
1934

1935
                                r = find_tpm2_auto_data(
1✔
1936
                                                cd,
1937
                                                arg_tpm2_pcr_mask, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */
1938
                                                token, /* search for the token with this index, or any later index than this */
1939
                                                &hash_pcr_mask,
1940
                                                &pcr_bank,
1941
                                                &pubkey,
1942
                                                &pubkey_pcr_mask,
1943
                                                &primary_alg,
1944
                                                &blobs,
1945
                                                &n_blobs,
1946
                                                &policy_hash,
1947
                                                &n_policy_hash,
1948
                                                &salt,
1949
                                                &srk,
1950
                                                &pcrlock_nv,
1951
                                                &tpm2_flags,
1952
                                                &keyslot,
1953
                                                &token);
1954
                                if (r == -ENXIO)
1✔
1955
                                        /* No further TPM2 tokens found in the LUKS2 header. */
UNCOV
1956
                                        return log_full_errno(found_some ? LOG_NOTICE : LOG_DEBUG,
×
1957
                                                              SYNTHETIC_ERRNO(EAGAIN),
1958
                                                              found_some
1959
                                                              ? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking."
1960
                                                              : "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking.");
1961
                                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
1✔
1962
                                        /* TPM2 support not compiled in? */
UNCOV
1963
                                        return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
×
1964
                                                               "TPM2 support not available, falling back to traditional unlocking.");
1965
                                if (r < 0)
1✔
1966
                                        return r;
1967

1968
                                found_some = true;
1✔
1969

1970
                                r = acquire_tpm2_key(
1✔
1971
                                                name,
1972
                                                arg_tpm2_device,
1973
                                                hash_pcr_mask,
1974
                                                pcr_bank,
1975
                                                &pubkey,
1976
                                                pubkey_pcr_mask,
1977
                                                arg_tpm2_signature,
1978
                                                arg_tpm2_pcrlock,
1979
                                                primary_alg,
1980
                                                /* key_file= */ NULL, /* key_file_size= */ 0, /* key_file_offset= */ 0, /* no key file */
1981
                                                blobs,
1982
                                                n_blobs,
1983
                                                policy_hash,
1984
                                                n_policy_hash,
1985
                                                &salt,
1986
                                                &srk,
1987
                                                &pcrlock_nv,
1988
                                                tpm2_flags,
1989
                                                until,
1990
                                                "cryptsetup.tpm2-pin",
1991
                                                arg_ask_password_flags,
1992
                                                &decrypted_key);
1993
                                if (IN_SET(r, -EACCES, -ENOLCK))
1✔
UNCOV
1994
                                        return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
×
1995
                                if (r != -EPERM)
1✔
1996
                                        break;
1997

UNCOV
1998
                                token++; /* try a different token next time */
×
1999
                        }
2000

2001
                        if (r >= 0)
1✔
2002
                                break;
2003
                        /* EAGAIN means: no tpm2 chip found */
2004
                        if (r != -EAGAIN) {
×
2005
                                log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
×
UNCOV
2006
                                return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
×
2007
                        }
2008
                }
2009

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

UNCOV
2014
                        assert(!event);
×
2015

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

2020
                        r = make_tpm2_device_monitor(&event, &monitor);
×
UNCOV
2021
                        if (r < 0)
×
2022
                                return r;
2023

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

2026
                        /* Let's immediately rescan in case the device appeared in the time we needed
2027
                         * to create and configure the monitor */
UNCOV
2028
                        continue;
×
2029
                }
2030

2031
                r = run_security_device_monitor(event, monitor);
×
UNCOV
2032
                if (r < 0)
×
2033
                        return r;
2034

UNCOV
2035
                log_debug("Got one or more potentially relevant udev events, rescanning for TPM2...");
×
2036
        }
2037

2038
        if (pass_volume_key)
1✔
UNCOV
2039
                r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key.iov_base, decrypted_key.iov_len, flags);
×
2040
        else {
2041
                _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1✔
2042
                ssize_t base64_encoded_size;
1✔
2043

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

2046
                base64_encoded_size = base64mem(decrypted_key.iov_base, decrypted_key.iov_len, &base64_encoded);
1✔
2047
                if (base64_encoded_size < 0)
1✔
UNCOV
2048
                        return log_oom();
×
2049

2050
                r = measured_crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, base64_encoded_size, flags);
1✔
2051
        }
2052
        if (r == -EPERM) {
1✔
2053
                log_error_errno(r, "Failed to activate with TPM2 decrypted key. (Key incorrect?)");
×
UNCOV
2054
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
2055
        }
2056
        if (r < 0)
1✔
UNCOV
2057
                return log_error_errno(r, "Failed to activate with TPM2 acquired key: %m");
×
2058

2059
        return 0;
2060
}
2061

2062
static int attach_luks_or_plain_or_bitlk_by_key_data(
3✔
2063
                struct crypt_device *cd,
2064
                const char *name,
2065
                const struct iovec *key_data,
2066
                uint32_t flags,
2067
                bool pass_volume_key) {
2068

2069
        int r;
3✔
2070

2071
        assert(cd);
3✔
2072
        assert(name);
3✔
2073
        assert(key_data);
3✔
2074

2075
        if (pass_volume_key)
3✔
UNCOV
2076
                r = measured_crypt_activate_by_volume_key(cd, name, key_data->iov_base, key_data->iov_len, flags);
×
2077
        else
2078
                r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, key_data->iov_base, key_data->iov_len, flags);
3✔
2079
        if (r == -EPERM) {
3✔
2080
                log_error_errno(r, "Failed to activate. (Key incorrect?)");
×
UNCOV
2081
                return -EAGAIN; /* Log actual error, but return EAGAIN */
×
2082
        }
2083
        if (r < 0)
3✔
UNCOV
2084
                return log_error_errno(r, "Failed to activate: %m");
×
2085

2086
        return 0;
2087
}
2088

2089
static int attach_luks_or_plain_or_bitlk_by_key_file(
21✔
2090
                struct crypt_device *cd,
2091
                const char *name,
2092
                const char *key_file,
2093
                uint32_t flags,
2094
                bool pass_volume_key) {
2095

2096
        _cleanup_(erase_and_freep) char *kfdata = NULL;
21✔
2097
        _cleanup_free_ char *bindname = NULL;
21✔
2098
        size_t kfsize;
21✔
2099
        int r;
21✔
2100

2101
        assert(cd);
21✔
2102
        assert(name);
21✔
2103
        assert(key_file);
21✔
2104

2105
        /* If we read the key via AF_UNIX, make this client recognizable */
2106
        bindname = make_bindname(name, /* token_type= */ _TOKEN_TYPE_INVALID);
21✔
2107
        if (!bindname)
21✔
UNCOV
2108
                return log_oom();
×
2109

2110
        r = read_full_file_full(
24✔
2111
                        AT_FDCWD, key_file,
2112
                        arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset,
21✔
2113
                        arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size,
21✔
2114
                        READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
2115
                        bindname,
2116
                        &kfdata, &kfsize);
2117
        if (r == -E2BIG) {
21✔
2118
                log_error_errno(r, "Failed to activate, key file '%s' too large.", key_file);
×
UNCOV
2119
                return -EAGAIN;
×
2120
        }
2121
        if (r == -ENOENT) {
21✔
2122
                log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file);
×
UNCOV
2123
                return -EAGAIN; /* Log actual error, but return EAGAIN */
×
2124
        }
2125
        if (r < 0)
21✔
UNCOV
2126
                return log_error_errno(r, "Failed to read key file '%s': %m", key_file);
×
2127

2128
        if (pass_volume_key)
21✔
UNCOV
2129
                r = measured_crypt_activate_by_volume_key(cd, name, kfdata, kfsize, flags);
×
2130
        else
2131
                r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, kfdata, kfsize, flags);
21✔
2132
        if (r == -EPERM) {
21✔
2133
                log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file);
3✔
2134
                return -EAGAIN; /* Log actual error, but return EAGAIN */
3✔
2135
        }
2136
        if (r < 0)
18✔
UNCOV
2137
                return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file);
×
2138

2139
        return 0;
2140
}
2141

UNCOV
2142
static int attach_luks_or_plain_or_bitlk_by_passphrase(
×
2143
                struct crypt_device *cd,
2144
                const char *name,
2145
                char **passwords,
2146
                uint32_t flags,
2147
                bool pass_volume_key) {
2148

UNCOV
2149
        int r;
×
2150

2151
        assert(cd);
×
UNCOV
2152
        assert(name);
×
2153

2154
        r = -EINVAL;
2155
        STRV_FOREACH(p, passwords) {
×
2156
                if (pass_volume_key)
×
UNCOV
2157
                        r = measured_crypt_activate_by_volume_key(cd, name, *p, arg_key_size, flags);
×
2158
                else
2159
                        r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, *p, strlen(*p), flags);
×
UNCOV
2160
                if (r >= 0)
×
2161
                        break;
2162
        }
2163
        if (r == -EPERM) {
×
2164
                log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)");
×
UNCOV
2165
                return -EAGAIN; /* log actual error, but return EAGAIN */
×
2166
        }
2167
        if (r < 0)
×
UNCOV
2168
                return log_error_errno(r, "Failed to activate with specified passphrase: %m");
×
2169

2170
        return 0;
2171
}
2172

2173
static int attach_luks_or_plain_or_bitlk(
34✔
2174
                struct crypt_device *cd,
2175
                const char *name,
2176
                TokenType token_type,
2177
                const char *key_file,
2178
                const struct iovec *key_data,
2179
                char **passwords,
2180
                uint32_t flags,
2181
                usec_t until) {
2182

2183
        bool pass_volume_key = false;
34✔
2184
        int r;
34✔
2185

2186
        assert(cd);
34✔
2187
        assert(name);
34✔
2188

2189
        if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) {
34✔
UNCOV
2190
                struct crypt_params_plain params = {
×
2191
                        .offset = arg_offset,
2192
                        .skip = arg_skip,
2193
                        .sector_size = arg_sector_size,
2194
                };
2195
                const char *cipher, *cipher_mode;
×
UNCOV
2196
                _cleanup_free_ char *truncated_cipher = NULL;
×
2197

UNCOV
2198
                if (streq_ptr(arg_hash, "plain"))
×
2199
                        /* plain isn't a real hash type. it just means "use no hash" */
2200
                        params.hash = NULL;
2201
                else if (arg_hash)
×
2202
                        params.hash = arg_hash;
×
UNCOV
2203
                else if (!key_file)
×
2204
                        /* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key
2205
                         * file is provided */
UNCOV
2206
                        params.hash = "ripemd160";
×
2207

2208
                if (arg_cipher) {
×
UNCOV
2209
                        size_t l;
×
2210

2211
                        l = strcspn(arg_cipher, "-");
×
2212
                        truncated_cipher = strndup(arg_cipher, l);
×
2213
                        if (!truncated_cipher)
×
UNCOV
2214
                                return log_oom();
×
2215

2216
                        cipher = truncated_cipher;
×
UNCOV
2217
                        cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain";
×
2218
                } else {
2219
                        cipher = "aes";
2220
                        cipher_mode = "cbc-essiv:sha256";
2221
                }
2222

2223
                /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */
UNCOV
2224
                arg_keyfile_size = arg_key_size;
×
2225

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

2232
                /* hash == NULL implies the user passed "plain" */
UNCOV
2233
                pass_volume_key = !params.hash;
×
2234
        }
2235

2236
        log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
34✔
2237
                 crypt_get_cipher(cd),
2238
                 crypt_get_cipher_mode(cd),
2239
                 crypt_get_volume_key_size(cd)*8,
2240
                 crypt_get_device_name(cd));
2241

2242
        if (token_type == TOKEN_TPM2)
34✔
2243
                return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, until, flags, pass_volume_key);
10✔
2244
        if (token_type == TOKEN_FIDO2)
24✔
UNCOV
2245
                return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, until, flags, pass_volume_key);
×
2246
        if (token_type == TOKEN_PKCS11)
24✔
UNCOV
2247
                return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, until, flags, pass_volume_key);
×
2248
        if (key_data)
24✔
2249
                return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, flags, pass_volume_key);
3✔
2250
        if (key_file)
21✔
2251
                return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key);
21✔
2252

UNCOV
2253
        return attach_luks_or_plain_or_bitlk_by_passphrase(cd, name, passwords, flags, pass_volume_key);
×
2254
}
2255

2256
static int help(void) {
×
2257
        _cleanup_free_ char *link = NULL;
×
UNCOV
2258
        int r;
×
2259

2260
        r = terminal_urlify_man("systemd-cryptsetup", "8", &link);
×
2261
        if (r < 0)
×
UNCOV
2262
                return log_oom();
×
2263

UNCOV
2264
        printf("%1$s attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG]\n"
×
2265
               "%1$s detach VOLUME\n\n"
2266
               "%2$sAttach or detach an encrypted block device.%3$s\n\n"
2267
               "  -h --help            Show this help\n"
2268
               "     --version         Show package version\n"
2269
               "\nSee the %4$s for details.\n",
2270
               program_invocation_short_name,
2271
               ansi_highlight(),
2272
               ansi_normal(),
2273
               link);
2274

2275
        return 0;
2276
}
2277

2278
static int parse_argv(int argc, char *argv[]) {
126✔
2279
        enum {
126✔
2280
                ARG_VERSION = 0x100,
2281
        };
2282

2283
        static const struct option options[] = {
126✔
2284
                { "help",                         no_argument,       NULL, 'h'                       },
2285
                { "version",                      no_argument,       NULL, ARG_VERSION               },
2286
                {}
2287
        };
2288

2289
        int c;
126✔
2290

2291
        assert(argc >= 0);
126✔
2292
        assert(argv);
126✔
2293

2294
        if (argv_looks_like_help(argc, argv))
126✔
UNCOV
2295
                return help();
×
2296

2297
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
126✔
UNCOV
2298
                switch (c) {
×
2299

2300
                case 'h':
×
UNCOV
2301
                        return help();
×
2302

2303
                case ARG_VERSION:
×
UNCOV
2304
                        return version();
×
2305

2306
                case '?':
2307
                        return -EINVAL;
2308

2309
                default:
×
UNCOV
2310
                        assert_not_reached();
×
2311
                }
2312

2313
        return 1;
2314
}
2315

2316
static uint32_t determine_flags(void) {
71✔
2317
        uint32_t flags = 0;
71✔
2318

2319
        if (arg_readonly)
71✔
UNCOV
2320
                flags |= CRYPT_ACTIVATE_READONLY;
×
2321

2322
        if (arg_discards)
71✔
UNCOV
2323
                flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
×
2324

2325
        if (arg_same_cpu_crypt)
71✔
UNCOV
2326
                flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
×
2327

2328
        if (arg_submit_from_crypt_cpus)
71✔
UNCOV
2329
                flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
×
2330

2331
        if (arg_no_read_workqueue)
71✔
UNCOV
2332
                flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
×
2333

2334
        if (arg_no_write_workqueue)
71✔
UNCOV
2335
                flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
×
2336

2337
#ifdef CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF
2338
        /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
2339
        /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
2340
        flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF;
71✔
2341
#endif
2342

2343
        return flags;
71✔
2344
}
2345

2346
static void remove_and_erasep(const char **p) {
71✔
2347
        int r;
71✔
2348

2349
        if (!*p)
71✔
2350
                return;
2351

2352
        r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE);
2✔
2353
        if (r < 0 && r != -ENOENT)
2✔
UNCOV
2354
                log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p);
×
2355
}
2356

2357
static TokenType determine_token_type(void) {
48✔
2358
        if (arg_tpm2_device || arg_tpm2_device_auto)
48✔
2359
                return TOKEN_TPM2;
2360
        if (arg_fido2_device || arg_fido2_device_auto)
38✔
2361
                return TOKEN_FIDO2;
2362
        if (arg_pkcs11_uri || arg_pkcs11_uri_auto)
38✔
UNCOV
2363
                return TOKEN_PKCS11;
×
2364

2365
        return _TOKEN_TYPE_INVALID;
2366
}
2367

2368
static int discover_key(const char *key_file, const char *volume, TokenType token_type, struct iovec *ret_key_data) {
24✔
2369
        _cleanup_free_ char *bindname = NULL;
24✔
2370
        const char *token_type_name;
24✔
2371
        int r;
24✔
2372

2373
        assert(key_file);
24✔
2374
        assert(volume);
24✔
2375
        assert(ret_key_data);
24✔
2376

2377
        bindname = make_bindname(volume, token_type);
24✔
2378
        if (!bindname)
24✔
UNCOV
2379
                return log_oom();
×
2380

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

2386
        token_type_name = token_type_to_string(token_type);
1✔
2387
        if (token_type_name)
1✔
UNCOV
2388
                log_debug("Automatically discovered encrypted key for volume '%s' (token type: %s).", volume, token_type_name);
×
2389
        else
2390
                log_debug("Automatically discovered key for volume '%s'.", volume);
1✔
2391

2392
        return r;
2393
}
2394

2395
static int verb_attach(int argc, char *argv[], void *userdata) {
71✔
2396
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
71✔
2397
        _unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL;
71✔
2398
        crypt_status_info status;
71✔
2399
        uint32_t flags = 0;
71✔
2400
        unsigned tries;
71✔
2401
        usec_t until;
71✔
2402
        PassphraseType passphrase_type = PASSPHRASE_NONE;
71✔
2403
        int r;
71✔
2404

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

2407
        assert(argc >= 3 && argc <= 5);
71✔
2408

2409
        const char *volume = ASSERT_PTR(argv[1]),
71✔
2410
                *source = ASSERT_PTR(argv[2]),
71✔
2411
                *key_file = argc >= 4 ? mangle_none(argv[3]) : NULL,
71✔
2412
                *config = argc >= 5 ? mangle_none(argv[4]) : NULL;
71✔
2413

2414
        if (!filename_is_valid(volume))
71✔
UNCOV
2415
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
×
2416

2417
        if (key_file && !path_is_absolute(key_file)) {
71✔
UNCOV
2418
                log_warning("Password file path '%s' is not absolute. Ignoring.", key_file);
×
2419
                key_file = NULL;
2420
        }
2421

2422
        if (config) {
71✔
2423
                r = parse_crypt_config(config);
71✔
2424
                if (r < 0)
71✔
2425
                        return r;
2426
        }
2427

2428
        log_debug("%s %s ← %s type=%s cipher=%s", __func__,
196✔
2429
                  volume, source, strempty(arg_type), strempty(arg_cipher));
2430

2431
        /* A delicious drop of snake oil */
2432
        (void) mlockall(MCL_FUTURE);
71✔
2433

2434
        if (key_file && arg_keyfile_erase)
71✔
2435
                destroy_key_file = key_file; /* let's get this baby erased when we leave */
2✔
2436

2437
        if (arg_header) {
71✔
2438
                if (streq_ptr(arg_type, CRYPT_TCRYPT)){
9✔
2439
                        log_debug("tcrypt header: %s", arg_header);
×
UNCOV
2440
                        r = crypt_init_data_device(&cd, arg_header, source);
×
2441
                } else {
2442
                        log_debug("LUKS header: %s", arg_header);
9✔
2443
                        r = crypt_init(&cd, arg_header);
9✔
2444
                }
2445
        } else
2446
                r = crypt_init(&cd, source);
62✔
2447
        if (r < 0)
71✔
UNCOV
2448
                return log_error_errno(r, "crypt_init() failed: %m");
×
2449

2450
        cryptsetup_enable_logging(cd);
71✔
2451

2452
        status = crypt_status(cd, volume);
71✔
2453
        if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
71✔
2454
                log_info("Volume %s already active.", volume);
×
UNCOV
2455
                return 0;
×
2456
        }
2457

2458
        flags = determine_flags();
71✔
2459

2460
        until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
71✔
UNCOV
2461
        if (until == USEC_INFINITY)
×
2462
                until = 0;
71✔
2463

2464
        if (arg_key_size == 0)
71✔
2465
                arg_key_size = 256U / 8U;
71✔
2466

2467
        if (key_file) {
71✔
2468
                struct stat st;
21✔
2469

2470
                /* Ideally we'd do this on the open fd, but since this is just a warning it's OK to do this
2471
                 * in two steps. */
2472
                if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005))
21✔
2473
                        log_warning("Key file %s is world-readable. This is not a good idea!", key_file);
1✔
2474
        }
2475

2476
        if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) {
71✔
2477
                r = crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL);
71✔
2478
                if (r < 0)
71✔
2479
                        return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd));
35✔
2480

2481
/* since cryptsetup 2.7.0 (Jan 2024) */
2482
#if HAVE_CRYPT_SET_KEYRING_TO_LINK
2483
                if (arg_link_key_description) {
71✔
2484
                        r = crypt_set_keyring_to_link(cd, arg_link_key_description, NULL, arg_link_key_type, arg_link_keyring);
×
2485
                        if (r < 0)
×
UNCOV
2486
                                log_warning_errno(r, "Failed to set keyring or key description to link volume key in, ignoring: %m");
×
2487
                }
2488
#endif
2489

2490
                if (arg_header) {
71✔
2491
                        r = crypt_set_data_device(cd, source);
9✔
2492
                        if (r < 0)
9✔
UNCOV
2493
                                return log_error_errno(r, "Failed to set LUKS data device %s: %m", source);
×
2494
                }
2495

2496
                /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */
2497
                if (!key_file && use_token_plugins()) {
71✔
2498
                        r = crypt_activate_by_token_pin_ask_password(
49✔
2499
                                        cd,
2500
                                        volume,
2501
                                        /* type= */ NULL,
2502
                                        until,
2503
                                        /* userdata= */ NULL,
2504
                                        flags,
2505
                                        "Please enter LUKS2 token PIN:",
2506
                                        "luks2-pin",
2507
                                        "cryptsetup.luks2-pin");
2508
                        if (r >= 0) {
49✔
2509
                                log_debug("Volume %s activated with a LUKS token.", volume);
35✔
2510
                                return 0;
35✔
2511
                        }
2512

2513
                        log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd));
14✔
2514
                }
2515
        }
2516

2517
/* since cryptsetup 2.3.0 (Feb 2020) */
2518
#ifdef CRYPT_BITLK
2519
        if (streq_ptr(arg_type, CRYPT_BITLK)) {
36✔
2520
                r = crypt_load(cd, CRYPT_BITLK, NULL);
×
2521
                if (r < 0)
×
UNCOV
2522
                        return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd));
×
2523
        }
2524
#endif
2525

2526
        bool use_cached_passphrase = true, try_discover_key = !key_file;
36✔
2527
        const char *discovered_key_fn = strjoina(volume, ".key");
180✔
2528
        _cleanup_strv_free_erase_ char **passwords = NULL;
36✔
2529
        for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
48✔
2530
                _cleanup_(iovec_done_erase) struct iovec discovered_key_data = {};
48✔
2531
                const struct iovec *key_data = NULL;
48✔
2532
                TokenType token_type = determine_token_type();
48✔
2533

2534
                log_debug("Beginning attempt %u to unlock.", tries);
48✔
2535

2536
                /* When we were able to acquire multiple keys, let's always process them in this order:
2537
                 *
2538
                 *    1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip
2539
                 *    2. The configured or discovered key, of which both are exclusive and optional
2540
                 *    3. The empty password, in case arg_try_empty_password is set
2541
                 *    4. We enquire the user for a password
2542
                 */
2543

2544
                if (try_discover_key) {
48✔
2545
                        r = discover_key(discovered_key_fn, volume, token_type, &discovered_key_data);
24✔
2546
                        if (r < 0)
24✔
2547
                                return r;
2548
                        if (r > 0)
24✔
2549
                                key_data = &discovered_key_data;
1✔
2550
                }
2551

2552
                if (token_type < 0 && !key_file && !key_data && !passwords) {
48✔
2553

2554
                        /* If we have nothing to try anymore, then acquire a new password */
2555

2556
                        if (arg_try_empty_password) {
16✔
2557
                                /* Hmm, let's try an empty password now, but only once */
2558
                                arg_try_empty_password = false;
2✔
2559
                                key_data = &iovec_empty;
2✔
2560
                        } else {
2561
                                /* Ask the user for a passphrase or recovery key only as last resort, if we
2562
                                 * have nothing else to check for */
2563
                                if (passphrase_type == PASSPHRASE_NONE) {
14✔
2564
                                        passphrase_type = check_registered_passwords(cd);
14✔
2565
                                        if (passphrase_type == PASSPHRASE_NONE)
14✔
UNCOV
2566
                                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No passphrase or recovery key registered.");
×
2567
                                }
2568

2569
                                r = get_password(
14✔
2570
                                                volume,
2571
                                                source,
2572
                                                until,
2573
                                                /* ignore_cached= */ !use_cached_passphrase || arg_verify,
14✔
2574
                                                passphrase_type,
2575
                                                &passwords);
2576
                                use_cached_passphrase = false;
14✔
2577
                                if (r == -EAGAIN)
14✔
UNCOV
2578
                                        continue;
×
2579
                                if (r < 0)
14✔
2580
                                        return r;
2581
                        }
2582
                }
2583

2584
                if (streq_ptr(arg_type, CRYPT_TCRYPT))
34✔
UNCOV
2585
                        r = attach_tcrypt(cd, volume, token_type, key_file, key_data, passwords, flags);
×
2586
                else
2587
                        r = attach_luks_or_plain_or_bitlk(cd, volume, token_type, key_file, key_data, passwords, flags, until);
34✔
2588
                if (r >= 0)
34✔
2589
                        break;
2590
                if (r != -EAGAIN)
12✔
2591
                        return r;
2592

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

2596
                if (token_type == TOKEN_TPM2) {
12✔
2597
                        arg_tpm2_device = mfree(arg_tpm2_device);
9✔
2598
                        arg_tpm2_device_auto = false;
9✔
2599
                        continue;
9✔
2600
                }
2601

2602
                if (token_type == TOKEN_FIDO2) {
3✔
2603
                        arg_fido2_device = mfree(arg_fido2_device);
×
2604
                        arg_fido2_device_auto = false;
×
UNCOV
2605
                        continue;
×
2606
                }
2607

2608
                if (token_type == TOKEN_PKCS11) {
3✔
2609
                        arg_pkcs11_uri = mfree(arg_pkcs11_uri);
×
2610
                        arg_pkcs11_uri_auto = false;
×
UNCOV
2611
                        continue;
×
2612
                }
2613

2614
                if (try_discover_key) {
3✔
2615
                        try_discover_key = false;
×
UNCOV
2616
                        continue;
×
2617
                }
2618

2619
                if (key_file) {
3✔
2620
                        key_file = NULL;
3✔
2621
                        continue;
3✔
2622
                }
2623

2624
                if (passwords) {
×
2625
                        passwords = strv_free_erase(passwords);
×
UNCOV
2626
                        continue;
×
2627
                }
2628

UNCOV
2629
                log_debug("Prepared for next attempt to unlock.");
×
2630
        }
2631

2632
        if (arg_tries != 0 && tries >= arg_tries)
22✔
UNCOV
2633
                return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up.");
×
2634

2635
        return 0;
2636
}
2637

2638
static int verb_detach(int argc, char *argv[], void *userdata) {
55✔
2639
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
55✔
2640
        const char *volume = ASSERT_PTR(argv[1]);
55✔
2641
        int r;
55✔
2642

2643
        assert(argc == 2);
55✔
2644

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

2648
        r = crypt_init_by_name(&cd, volume);
55✔
2649
        if (r == -ENODEV) {
55✔
2650
                log_info("Volume %s already inactive.", volume);
×
UNCOV
2651
                return 0;
×
2652
        }
2653
        if (r < 0)
55✔
UNCOV
2654
                return log_error_errno(r, "crypt_init_by_name() for volume '%s' failed: %m", volume);
×
2655

2656
        cryptsetup_enable_logging(cd);
55✔
2657

2658
        r = crypt_deactivate(cd, volume);
55✔
2659
        if (r < 0)
55✔
UNCOV
2660
                return log_error_errno(r, "Failed to deactivate '%s': %m", volume);
×
2661

2662
        return 0;
2663
}
2664

2665
static int run(int argc, char *argv[]) {
126✔
2666
        int r;
126✔
2667

2668
        log_setup();
126✔
2669

2670
        umask(0022);
126✔
2671

2672
        r = parse_argv(argc, argv);
126✔
2673
        if (r <= 0)
126✔
2674
                return r;
2675

2676
        cryptsetup_enable_logging(NULL);
126✔
2677

2678
        static const Verb verbs[] = {
126✔
2679
                { "attach", 3, 5, 0, verb_attach },
2680
                { "detach", 2, 2, 0, verb_detach },
2681
                {}
2682
        };
2683

2684
        return dispatch_verb(argc, argv, verbs, NULL);
126✔
2685
}
2686

2687
DEFINE_MAIN_FUNCTION(run);
126✔
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