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

systemd / systemd / 29132483780

10 Jul 2026 08:43PM UTC coverage: 72.912% (+0.2%) from 72.702%
29132483780

push

github

bluca
man: run forgotten 'update-man-rules'

344600 of 472622 relevant lines covered (72.91%)

1365091.83 hits per line

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

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

3
#include "sd-json.h"
4

5
#include "alloc-util.h"
6
#include "ask-password-api.h"
7
#include "cryptsetup-tpm2.h"
8
#include "cryptsetup-util.h"
9
#include "env-util.h"
10
#include "fileio.h"
11
#include "hexdecoct.h"
12
#include "log.h"
13
#include "random-util.h"
14
#include "strv.h"
15
#include "tpm2-util.h"
16

17
#if HAVE_LIBCRYPTSETUP && HAVE_TPM2
18
static int get_pin(
×
19
                usec_t until,
20
                const char *askpw_credential,
21
                AskPasswordFlags askpw_flags,
22
                char **ret_pin_str) {
23
        _cleanup_(erase_and_freep) char *pin_str = NULL;
×
24
        _cleanup_strv_free_erase_ char **pin = NULL;
×
25
        int r;
×
26

27
        assert(ret_pin_str);
×
28

29
        r = getenv_steal_erase("PIN", &pin_str);
×
30
        if (r < 0)
×
31
                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
×
32
        if (!r) {
×
33
                if (FLAGS_SET(askpw_flags, ASK_PASSWORD_HEADLESS))
×
34
                        return log_error_errno(
×
35
                                        SYNTHETIC_ERRNO(ENOPKG),
36
                                        "PIN querying disabled via 'headless' option. "
37
                                        "Use the '$PIN' environment variable.");
38

39
                AskPasswordRequest req = {
×
40
                        .tty_fd = -EBADF,
41
                        .message = "Please enter TPM2 PIN:",
42
                        .icon = "drive-harddisk",
43
                        .keyring = "tpm2-pin",
44
                        .credential = askpw_credential,
45
                        .until = until,
46
                        .hup_fd = -EBADF,
47
                };
48

49
                pin = strv_free_erase(pin);
×
50
                r = ask_password_auto(&req, askpw_flags, &pin);
×
51
                if (r < 0)
×
52
                        return log_error_errno(r, "Failed to ask for user pin: %m");
×
53
                assert(strv_length(pin) == 1);
×
54

55
                pin_str = strdup(pin[0]);
×
56
                if (!pin_str)
×
57
                        return log_oom();
×
58
        }
59

60
        *ret_pin_str = TAKE_PTR(pin_str);
×
61

62
        return r;
×
63
}
64
#endif
65

66
int acquire_tpm2_key(
12✔
67
                const char *volume_name,
68
                const char *device,
69
                uint32_t hash_pcr_mask,
70
                uint16_t pcr_bank,
71
                const struct iovec *pubkey,
72
                const char *pubkey_policy_ref,
73
                uint32_t pubkey_pcr_mask,
74
                const char *signature_path,
75
                const char *pcrlock_path,
76
                uint16_t primary_alg,
77
                const char *key_file,
78
                size_t key_file_size,
79
                uint64_t key_file_offset,
80
                const struct iovec blobs[],
81
                size_t n_blobs,
82
                const struct iovec policy_hash[],
83
                size_t n_policy_hash,
84
                const struct iovec *salt,
85
                const struct iovec *srk,
86
                const struct iovec *pcrlock_nv,
87
                TPM2Flags flags,
88
                usec_t until,
89
                const char *askpw_credential,
90
                AskPasswordFlags askpw_flags,
91
                struct iovec *ret_decrypted_key) {
92

93
#if HAVE_LIBCRYPTSETUP && HAVE_TPM2
94
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *signature_json = NULL;
12✔
95
        _cleanup_(iovec_done) struct iovec loaded_blob = {};
12✔
96
        _cleanup_free_ char *auto_device = NULL;
12✔
97
        int r;
12✔
98

99
        assert(iovec_is_valid(salt));
12✔
100

101
        if (!device) {
12✔
102
                r = tpm2_find_device_auto(&auto_device);
12✔
103
                if (r == -ENODEV)
12✔
104
                        return -EAGAIN; /* Tell the caller to wait for a TPM2 device to show up */
105
                if (r < 0)
12✔
106
                        return log_error_errno(r, "Could not find TPM2 device: %m");
×
107

108
                device = auto_device;
12✔
109
        }
110

111
        if (n_blobs == 0) {
12✔
112
                _cleanup_free_ char *bindname = NULL;
2✔
113

114
                /* If we read the salt via AF_UNIX, make this client recognizable */
115
                if (asprintf(&bindname, "@%" PRIx64"/cryptsetup-tpm2/%s", random_u64(), volume_name) < 0)
2✔
116
                        return log_oom();
×
117

118
                r = read_full_file_full(
6✔
119
                                AT_FDCWD, key_file,
120
                                key_file_offset == 0 ? UINT64_MAX : key_file_offset,
121
                                key_file_size == 0 ? SIZE_MAX : key_file_size,
122
                                READ_FULL_FILE_CONNECT_SOCKET,
123
                                bindname,
124
                                (char**) &loaded_blob.iov_base, &loaded_blob.iov_len);
125
                if (r < 0)
2✔
126
                        return r;
127

128
                blobs = &loaded_blob;
2✔
129
                n_blobs = 1;
2✔
130
        }
131

132
        if (pubkey_pcr_mask != 0) {
12✔
133
                r = tpm2_load_pcr_signature(signature_path, &signature_json);
3✔
134
                if (r < 0)
3✔
135
                        return log_error_errno(r, "Failed to load pcr signature: %m");
×
136
        }
137

138
        _cleanup_(tpm2_pcrlock_policy_done) Tpm2PCRLockPolicy pcrlock_policy = {};
12✔
139

140
        if (FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK)) {
12✔
141
                r = tpm2_pcrlock_policy_load(pcrlock_path, &pcrlock_policy);
2✔
142
                if (r < 0)
2✔
143
                        return r;
144
                if (r == 0) {
2✔
145
                        /* Not found? Then search among passed credentials */
146
                        r = tpm2_pcrlock_policy_from_credentials(srk, pcrlock_nv, &pcrlock_policy);
×
147
                        if (r < 0)
×
148
                                return r;
149
                        if (r == 0)
×
150
                                return log_error_errno(SYNTHETIC_ERRNO(EREMOTE), "Couldn't find pcrlock policy for volume.");
×
151
                }
152
        }
153

154
        _cleanup_(tpm2_context_unrefp) Tpm2Context *tpm2_context = NULL;
×
155
        r = tpm2_context_new_or_warn(device, &tpm2_context);
12✔
156
        if (r < 0)
12✔
157
                return r;
158

159
        if (!(flags & TPM2_FLAGS_USE_PIN)) {
12✔
160
                r = tpm2_unseal(tpm2_context,
22✔
161
                                hash_pcr_mask,
162
                                pcr_bank,
163
                                pubkey,
164
                                pubkey_policy_ref,
165
                                pubkey_pcr_mask,
166
                                signature_json,
167
                                /* pin= */ NULL,
168
                                FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK) ? &pcrlock_policy : NULL,
169
                                primary_alg,
170
                                blobs,
171
                                n_blobs,
172
                                policy_hash,
173
                                n_policy_hash,
174
                                srk,
175
                                ret_decrypted_key);
176
                if (r == -EREMOTE)
12✔
177
                        return log_warning_errno(r, "TPM key integrity check failed. Key enrolled in superblock most likely does not belong to this TPM.");
1✔
178
                if (r == -EADDRNOTAVAIL)
11✔
179
                        return log_warning_errno(r, "NV index referenced by token is missing, unwritten, or unusable, it could be for another system.");
×
180
                if (ERRNO_IS_NEG_TPM2_UNSEAL_BAD_PCR(r)) {
11✔
181
                        log_warning_errno(r, "TPM policy does not match current system state. Either system has been tempered with or policy out-of-date: %m");
12✔
182
                        /* Normalize to -EPERM so callers don't confuse it with -ENOANO's "needs PIN" meaning. */
183
                        return -EPERM;
184
                }
185
                if (r == -ENOSTR)
11✔
186
                        return log_warning_errno(r, "No signature for current PCR policy in TPM2 signature JSON, token does not apply to current boot state: %m");
1✔
187
                if (r < 0)
10✔
188
                        return log_error_errno(r, "Failed to unseal secret using TPM2: %m");
2✔
189

190
                return r;
191
        }
192

193
        for (int i = 5;; i--) {
×
194
                _cleanup_(erase_and_freep) char *pin_str = NULL, *b64_salted_pin = NULL;
×
195

196
                if (i <= 0)
×
197
                        return -EACCES;
198

199
                r = get_pin(until, askpw_credential, askpw_flags, &pin_str);
×
200
                if (r < 0)
×
201
                        return r;
202

203
                askpw_flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
×
204

205
                if (iovec_is_set(salt)) {
×
206
                        uint8_t salted_pin[SHA256_DIGEST_SIZE] = {};
×
207
                        CLEANUP_ERASE(salted_pin);
×
208

209
                        r = tpm2_util_pbkdf2_hmac_sha256(pin_str, strlen(pin_str), salt->iov_base, salt->iov_len, salted_pin);
×
210
                        if (r < 0)
×
211
                                return log_error_errno(r, "Failed to perform PBKDF2: %m");
×
212

213
                        r = base64mem(salted_pin, sizeof(salted_pin), &b64_salted_pin);
×
214
                        if (r < 0)
×
215
                                return log_error_errno(r, "Failed to base64 encode salted pin: %m");
×
216
                } else
217
                        /* no salting needed, backwards compat with non-salted pins */
218
                        b64_salted_pin = TAKE_PTR(pin_str);
×
219

220
                r = tpm2_unseal(tpm2_context,
×
221
                                hash_pcr_mask,
222
                                pcr_bank,
223
                                pubkey,
224
                                pubkey_policy_ref,
225
                                pubkey_pcr_mask,
226
                                signature_json,
227
                                b64_salted_pin,
228
                                FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK) ? &pcrlock_policy : NULL,
229
                                primary_alg,
230
                                blobs,
231
                                n_blobs,
232
                                policy_hash,
233
                                n_policy_hash,
234
                                srk,
235
                                ret_decrypted_key);
236
                if (r == -EREMOTE)
×
237
                        return log_warning_errno(r, "TPM key integrity check failed. Key enrolled in superblock most likely does not belong to this TPM.");
×
238
                if (r == -EADDRNOTAVAIL)
×
239
                        return log_warning_errno(r, "NV index referenced by token is missing, unwritten, or unusable, it could be for another system.");
×
240
                if (ERRNO_IS_NEG_TPM2_UNSEAL_BAD_PCR(r)) {
×
241
                        log_warning_errno(r, "TPM policy does not match current system state. Either system has been tempered with or policy out-of-date: %m");
×
242
                        /* Normalize to -EPERM so callers don't confuse it with -ENOANO's "needs PIN" meaning. */
243
                        return -EPERM;
244
                }
245
                if (r == -ENOSTR)
×
246
                        return log_warning_errno(r, "No signature for current PCR policy in TPM2 signature JSON, token does not apply to current boot state: %m");
×
247
                if (r == -ENOLCK)
×
248
                        return log_error_errno(r, "TPM is in dictionary attack lock-out mode.");
×
249
                if (r == -EILSEQ) {
×
250
                        log_warning_errno(r, "Bad PIN.");
×
251
                        continue;
×
252
                }
253
                if (r < 0)
×
254
                        return log_error_errno(r, "Failed to unseal secret using TPM2: %m");
×
255

256
                return r;
257
        }
258
#else
259
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support not available.");
260
#endif
261
}
262

263
int find_tpm2_auto_data(
10✔
264
                struct crypt_device *cd,
265
                uint32_t search_pcr_mask,
266
                int start_token,
267
                uint32_t *ret_hash_pcr_mask,
268
                uint16_t *ret_pcr_bank,
269
                struct iovec *ret_pubkey,
270
                char **ret_pubkey_policy_ref,
271
                uint32_t *ret_pubkey_pcr_mask,
272
                uint16_t *ret_primary_alg,
273
                struct iovec **ret_blobs,
274
                size_t *ret_n_blobs,
275
                struct iovec **ret_policy_hash,
276
                size_t *ret_n_policy_hash,
277
                struct iovec *ret_salt,
278
                struct iovec *ret_srk,
279
                struct iovec *ret_pcrlock_nv,
280
                TPM2Flags *ret_flags,
281
                int *ret_keyslot,
282
                int *ret_token) {
283

284
#if HAVE_LIBCRYPTSETUP && HAVE_TPM2
285
        int r, token;
10✔
286

287
        assert(cd);
10✔
288
        assert(ret_hash_pcr_mask);
10✔
289
        assert(ret_pcrlock_nv);
10✔
290
        assert(ret_pubkey);
10✔
291
        assert(ret_pubkey_policy_ref);
10✔
292
        assert(ret_pubkey_pcr_mask);
10✔
293
        assert(ret_primary_alg);
10✔
294
        assert(ret_blobs);
10✔
295
        assert(ret_n_blobs);
10✔
296
        assert(ret_policy_hash);
10✔
297
        assert(ret_n_policy_hash);
10✔
298
        assert(ret_salt);
10✔
299
        assert(ret_srk);
10✔
300
        assert(ret_pcrlock_nv);
10✔
301
        assert(ret_flags);
10✔
302
        assert(ret_keyslot);
10✔
303
        assert(ret_token);
10✔
304

305
        for (token = start_token; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
16✔
306
                _cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {};
16✔
307
                _cleanup_free_ char *pubkey_policy_ref = NULL;
16✔
308
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
16✔
309
                struct iovec *blobs = NULL, *policy_hash = NULL;
16✔
310
                size_t n_blobs = 0, n_policy_hash = 0;
16✔
311
                uint32_t hash_pcr_mask, pubkey_pcr_mask;
16✔
312
                uint16_t pcr_bank, primary_alg;
16✔
313
                TPM2Flags flags;
16✔
314
                int keyslot;
16✔
315

316
                CLEANUP_ARRAY(blobs, n_blobs, iovec_array_free);
16✔
317
                CLEANUP_ARRAY(policy_hash, n_policy_hash, iovec_array_free);
16✔
318

319
                r = cryptsetup_get_token_as_json(cd, token, "systemd-tpm2", &v);
16✔
320
                if (ERRNO_IS_NEG_CRYPTSETUP_TOKEN_SKIP(r))
16✔
321
                        continue;
6✔
322
                if (r < 0)
10✔
323
                        return log_error_errno(r, "Failed to read JSON token data off disk: %m");
×
324

325
                r = tpm2_parse_luks2_json(
10✔
326
                                v,
327
                                &keyslot,
328
                                &hash_pcr_mask,
329
                                &pcr_bank,
330
                                &pubkey,
331
                                &pubkey_policy_ref,
332
                                &pubkey_pcr_mask,
333
                                &primary_alg,
334
                                &blobs,
335
                                &n_blobs,
336
                                &policy_hash,
337
                                &n_policy_hash,
338
                                &salt,
339
                                &srk,
340
                                &pcrlock_nv,
341
                                &flags);
342
                if (r == -EUCLEAN) /* Gracefully handle issues in JSON fields not owned by us */
10✔
343
                        continue;
×
344
                if (r < 0)
10✔
345
                        return log_error_errno(r, "Failed to parse TPM2 JSON data: %m");
×
346

347
                if (search_pcr_mask == UINT32_MAX ||
10✔
348
                    search_pcr_mask == hash_pcr_mask) {
×
349

350
                        if (start_token <= 0)
10✔
351
                                log_info("Automatically discovered security TPM2 token unlocks volume.");
8✔
352

353
                        *ret_hash_pcr_mask = hash_pcr_mask;
10✔
354
                        *ret_pcr_bank = pcr_bank;
10✔
355
                        *ret_pubkey = TAKE_STRUCT(pubkey);
10✔
356
                        *ret_pubkey_policy_ref = TAKE_PTR(pubkey_policy_ref);
10✔
357
                        *ret_pubkey_pcr_mask = pubkey_pcr_mask;
10✔
358
                        *ret_primary_alg = primary_alg;
10✔
359
                        *ret_blobs = TAKE_PTR(blobs);
10✔
360
                        *ret_n_blobs = n_blobs;
10✔
361
                        *ret_policy_hash = TAKE_PTR(policy_hash);
10✔
362
                        *ret_n_policy_hash = n_policy_hash;
10✔
363
                        *ret_salt = TAKE_STRUCT(salt);
10✔
364
                        *ret_keyslot = keyslot;
10✔
365
                        *ret_token = token;
10✔
366
                        *ret_srk = TAKE_STRUCT(srk);
10✔
367
                        *ret_pcrlock_nv = TAKE_STRUCT(pcrlock_nv);
10✔
368
                        *ret_flags = flags;
10✔
369
                        return 0;
10✔
370
                }
371

372
                /* PCR mask doesn't match what is configured, ignore this entry, let's see next */
373
        }
374

375
        return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No valid TPM2 token data found.");
×
376
#else
377
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support not available.");
378
#endif
379
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc