• 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

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

3
#include <stdlib.h>
4

5
#include "sd-json.h"
6

7
#include "alloc-util.h"
8
#include "cryptsetup-util.h"
9
#include "dlfcn-util.h"
10
#include "escape.h"
11
#include "hexdecoct.h"
12
#include "hmac.h"
13
#include "log.h"
14
#include "parse-util.h"
15
#include "string-util.h"
16
#include "strv.h"
17

18
#if HAVE_LIBCRYPTSETUP
19
DLSYM_PROTOTYPE(crypt_activate_by_passphrase) = NULL;
20
DLSYM_PROTOTYPE(crypt_activate_by_signed_key) = NULL;
21
DLSYM_PROTOTYPE(crypt_activate_by_token_pin) = NULL;
22
DLSYM_PROTOTYPE(crypt_activate_by_volume_key) = NULL;
23
DLSYM_PROTOTYPE(crypt_deactivate) = NULL;
24
DLSYM_PROTOTYPE(crypt_deactivate_by_name) = NULL;
25
DLSYM_PROTOTYPE(crypt_format) = NULL;
26
DLSYM_PROTOTYPE(crypt_free) = NULL;
27
DLSYM_PROTOTYPE(crypt_get_cipher) = NULL;
28
DLSYM_PROTOTYPE(crypt_get_cipher_mode) = NULL;
29
DLSYM_PROTOTYPE(crypt_get_data_offset) = NULL;
30
DLSYM_PROTOTYPE(crypt_get_device_name) = NULL;
31
DLSYM_PROTOTYPE(crypt_get_dir) = NULL;
32
DLSYM_PROTOTYPE(crypt_get_type) = NULL;
33
DLSYM_PROTOTYPE(crypt_get_uuid) = NULL;
34
DLSYM_PROTOTYPE(crypt_get_verity_info) = NULL;
35
DLSYM_PROTOTYPE(crypt_get_volume_key_size) = NULL;
36
DLSYM_PROTOTYPE(crypt_header_restore) = NULL;
37
DLSYM_PROTOTYPE(crypt_init) = NULL;
38
DLSYM_PROTOTYPE(crypt_init_by_name) = NULL;
39
DLSYM_PROTOTYPE(crypt_init_data_device) = NULL;
40
DLSYM_PROTOTYPE(crypt_keyslot_add_by_volume_key) = NULL;
41
DLSYM_PROTOTYPE(crypt_keyslot_destroy) = NULL;
42
DLSYM_PROTOTYPE(crypt_keyslot_max) = NULL;
43
DLSYM_PROTOTYPE(crypt_keyslot_status) = NULL;
44
DLSYM_PROTOTYPE(crypt_load) = NULL;
45
DLSYM_PROTOTYPE(crypt_logf) = NULL;
46
DLSYM_PROTOTYPE(crypt_metadata_locking) = NULL;
47
DLSYM_PROTOTYPE(crypt_persistent_flags_get) = NULL;
48
DLSYM_PROTOTYPE(crypt_persistent_flags_set) = NULL;
49
DLSYM_PROTOTYPE(crypt_reencrypt_init_by_passphrase) = NULL;
50
DLSYM_PROTOTYPE(crypt_reencrypt_run);
51
DLSYM_PROTOTYPE(crypt_resize) = NULL;
52
DLSYM_PROTOTYPE(crypt_resume_by_volume_key) = NULL;
53
DLSYM_PROTOTYPE(crypt_set_data_device) = NULL;
54
DLSYM_PROTOTYPE(crypt_set_data_offset) = NULL;
55
DLSYM_PROTOTYPE(crypt_set_debug_level) = NULL;
56
static int missing_crypt_set_keyring_to_link(
×
57
                struct crypt_device *cd,
58
                const char *key_description,
59
                const char *old_key_description,
60
                const char *key_type_desc,
61
                const char *keyring_to_link_vk) {
62
        return -ENOSYS;
×
63
}
64
DLSYM_PROTOTYPE(crypt_set_keyring_to_link) = missing_crypt_set_keyring_to_link;
65
DLSYM_PROTOTYPE(crypt_set_log_callback) = NULL;
66
DLSYM_PROTOTYPE(crypt_set_metadata_size) = NULL;
67
DLSYM_PROTOTYPE(crypt_set_pbkdf_type) = NULL;
68
DLSYM_PROTOTYPE(crypt_status) = NULL;
69
DLSYM_PROTOTYPE(crypt_suspend) = NULL;
70
DLSYM_PROTOTYPE(crypt_token_external_path) = NULL;
71
DLSYM_PROTOTYPE(crypt_token_json_get) = NULL;
72
DLSYM_PROTOTYPE(crypt_token_json_set) = NULL;
73
DLSYM_PROTOTYPE(crypt_token_max) = NULL;
74
static int missing_crypt_token_set_external_path(const char *path) {
×
75
        return -ENOSYS;
×
76
}
77
DLSYM_PROTOTYPE(crypt_token_set_external_path) = missing_crypt_token_set_external_path;
78
DLSYM_PROTOTYPE(crypt_token_status) = NULL;
79
DLSYM_PROTOTYPE(crypt_volume_key_get) = NULL;
80
DLSYM_PROTOTYPE(crypt_volume_key_keyring) = NULL;
81
DLSYM_PROTOTYPE(crypt_wipe) = NULL;
82
DLSYM_PROTOTYPE(crypt_get_integrity_info) = NULL;
83

84
static void cryptsetup_log_glue(int level, const char *msg, void *usrptr) {
37,932✔
85

86
        switch (level) {
37,932✔
87
        case CRYPT_LOG_NORMAL:
88
                level = LOG_NOTICE;
89
                break;
90
        case CRYPT_LOG_ERROR:
44✔
91
                level = LOG_ERR;
44✔
92
                break;
44✔
93
        case CRYPT_LOG_VERBOSE:
4✔
94
                level = LOG_INFO;
4✔
95
                break;
4✔
96
        case CRYPT_LOG_DEBUG:
37,884✔
97
                level = LOG_DEBUG;
37,884✔
98
                break;
37,884✔
99
        default:
100
                log_error("Unknown libcryptsetup log level: %d", level);
×
101
                level = LOG_ERR;
102
        }
103

104
        log_full(level, "%s", msg);
37,932✔
105
}
37,932✔
106

107
void cryptsetup_enable_logging(struct crypt_device *cd) {
15,470✔
108
        /* It's OK to call this with a NULL parameter, in which case libcryptsetup will set the default log
109
         * function.
110
         *
111
         * Note that this is also called from dlopen_cryptsetup(), which we call here too. Sounds like an
112
         * endless loop, but isn't because we break it via the check for 'cryptsetup_dl' early in
113
         * dlopen_cryptsetup(). */
114

115
        if (dlopen_cryptsetup(LOG_DEBUG) < 0)
15,470✔
116
                return; /* If this fails, let's gracefully ignore the issue, this is just debug logging after
117
                         * all, and if this failed we already generated a debug log message that should help
118
                         * to track things down. */
119

120
        sym_crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
15,470✔
121
        sym_crypt_set_debug_level(DEBUG_LOGGING ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
15,539✔
122
}
123

124
int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd) {
51✔
125

126
        /* With CRYPT_PBKDF_NO_BENCHMARK flag set .time_ms member is ignored
127
         * while .iterations must be set at least to recommended minimum value. */
128

129
        static const struct crypt_pbkdf_type minimal_pbkdf = {
51✔
130
                .hash = "sha512",
131
                .type = CRYPT_KDF_PBKDF2,
132
                .iterations = 1000, /* recommended minimum count for pbkdf2
133
                                     * according to NIST SP 800-132, ch. 5.2 */
134
                .flags = CRYPT_PBKDF_NO_BENCHMARK
135
        };
136

137
        int r;
51✔
138

139
        /* Sets a minimal PKBDF in case we already have a high entropy key. */
140

141
        r = sym_crypt_set_pbkdf_type(cd, &minimal_pbkdf);
51✔
142
        if (r < 0)
51✔
143
                return r;
×
144

145
        return 0;
146
}
147

148
int cryptsetup_get_token_as_json(
6,392✔
149
                struct crypt_device *cd,
150
                int idx,
151
                const char *verify_type,
152
                sd_json_variant **ret) {
153

154
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
6,392✔
155
        const char *text;
6,392✔
156
        int r;
6,392✔
157

158
        assert(cd);
6,392✔
159

160
        /* Extracts and parses the LUKS2 JSON token data from a LUKS2 device. Optionally verifies the type of
161
         * the token. Returns:
162
         *
163
         *      -EINVAL → token index out of range or "type" field missing
164
         *      -ENOENT → token doesn't exist
165
         * -EMEDIUMTYPE → "verify_type" specified and doesn't match token's type
166
         *     -EUCLEAN → token JSON data cannot be parsed
167
         */
168

169
        r = sym_crypt_token_json_get(cd, idx, &text);
6,392✔
170
        if (r < 0)
6,392✔
171
                return r;
172

173
        r = sd_json_parse(text, 0, &v, NULL, NULL);
425✔
174
        if (r < 0)
425✔
175
                return r == -ENOMEM ? r : -EUCLEAN; /* Report unparseable token data in a single way for callers to skip */
×
176

177
        if (verify_type) {
425✔
178
                sd_json_variant *w;
255✔
179

180
                w = sd_json_variant_by_key(v, "type");
255✔
181
                if (!w)
255✔
182
                        return -EINVAL;
183

184
                if (!streq_ptr(sd_json_variant_string(w), verify_type))
255✔
185
                        return -EMEDIUMTYPE;
186
        }
187

188
        if (ret)
192✔
189
                *ret = TAKE_PTR(v);
192✔
190

191
        return 0;
192
}
193

194
int cryptsetup_add_token_json(struct crypt_device *cd, sd_json_variant *v) {
51✔
195
        _cleanup_free_ char *text = NULL;
51✔
196
        int r;
51✔
197

198
        r = sd_json_variant_format(v, 0, &text);
51✔
199
        if (r < 0)
51✔
200
                return log_debug_errno(r, "Failed to format token data for LUKS: %m");
×
201

202
        log_debug("Adding token text <%s>", text);
51✔
203

204
        r = sym_crypt_token_json_set(cd, CRYPT_ANY_TOKEN, text);
51✔
205
        if (r < 0)
51✔
206
                return log_debug_errno(r, "Failed to write token data to LUKS: %m");
×
207

208
        return 0;
209
}
210

211
int cryptsetup_get_volume_key_prefix(
7✔
212
                struct crypt_device *cd,
213
                const char *volume_name,
214
                char **ret) {
215

216
        _cleanup_free_ char *volume = NULL;
7✔
217
        const char *uuid;
7✔
218
        char *s;
7✔
219

220
        assert(ret);
7✔
221

222
        uuid = sym_crypt_get_uuid(cd);
7✔
223
        if (!uuid)
7✔
224
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get LUKS UUID.");
×
225

226
        if (volume_name)
7✔
227
                /* avoid ambiguity around ":" once we join things below */
228
                volume = xescape(volume_name, ":");
7✔
229
        else
230
                volume = strjoin("luks-", uuid);
×
231
        if (!volume)
7✔
232
                return log_oom_debug();
×
233

234
        s = strjoin("cryptsetup:", volume, ":", uuid);
7✔
235
        if (!s)
7✔
236
                return log_oom_debug();
×
237

238
        *ret = s;
7✔
239

240
        return 0;
7✔
241
}
242

243
/* The hash must match what measure_volume_key() extends to the SHA256 bank of the TPM2. */
244
int cryptsetup_get_volume_key_id(
7✔
245
                struct crypt_device *cd,
246
                const char *volume_name,
247
                const void *volume_key,
248
                size_t volume_key_size,
249
                char **ret) {
250

251
        _cleanup_free_ char *prefix = NULL;
7✔
252
        uint8_t digest[SHA256_DIGEST_SIZE];
7✔
253
        char *hex;
7✔
254
        int r;
7✔
255

256
        assert(ret);
7✔
257

258
        r = cryptsetup_get_volume_key_prefix(cd, volume_name, &prefix);
7✔
259
        if (r < 0)
7✔
260
                return log_debug_errno(r, "Failed to get LUKS volume key prefix.");
×
261

262
        hmac_sha256(volume_key, volume_key_size, prefix, strlen(prefix), digest);
7✔
263

264
        hex = hexmem(digest, sizeof(digest));
7✔
265
        if (!hex)
7✔
266
                return log_oom_debug();
×
267

268
        *ret = hex;
7✔
269

270
        return 0;
7✔
271
}
272
#endif
273

274
int dlopen_cryptsetup(int log_level) {
33,484✔
275
#if HAVE_LIBCRYPTSETUP
276
        static void *cryptsetup_dl = NULL;
33,484✔
277
        int r;
33,484✔
278

279
        /* libcryptsetup added crypt_reencrypt() in 2.2.0, and marked it obsolete in 2.4.0, replacing it with
280
         * crypt_reencrypt_run(), which takes one extra argument but is otherwise identical. The old call is
281
         * still available though, and given we want to support 2.2.0 for a while longer, we'll use the old
282
         * symbol if the new one is not available. */
283

284
        LIBCRYPTSETUP_NOTE(suggested);
33,484✔
285

286
        r = dlopen_many_sym_or_warn(
33,484✔
287
                        &cryptsetup_dl, "libcryptsetup.so.12", log_level,
288
                        DLSYM_ARG(crypt_activate_by_passphrase),
289
                        DLSYM_ARG(crypt_activate_by_signed_key),
290
                        DLSYM_ARG(crypt_activate_by_token_pin),
291
                        DLSYM_ARG(crypt_activate_by_volume_key),
292
                        DLSYM_ARG(crypt_deactivate),
293
                        DLSYM_ARG(crypt_deactivate_by_name),
294
                        DLSYM_ARG(crypt_format),
295
                        DLSYM_ARG(crypt_free),
296
                        DLSYM_ARG(crypt_get_cipher),
297
                        DLSYM_ARG(crypt_get_cipher_mode),
298
                        DLSYM_ARG(crypt_get_data_offset),
299
                        DLSYM_ARG(crypt_get_device_name),
300
                        DLSYM_ARG(crypt_get_dir),
301
                        DLSYM_ARG(crypt_get_type),
302
                        DLSYM_ARG(crypt_get_uuid),
303
                        DLSYM_ARG(crypt_get_verity_info),
304
                        DLSYM_ARG(crypt_get_volume_key_size),
305
                        DLSYM_ARG(crypt_header_restore),
306
                        DLSYM_ARG(crypt_init),
307
                        DLSYM_ARG(crypt_init_by_name),
308
                        DLSYM_ARG(crypt_init_data_device),
309
                        DLSYM_ARG(crypt_keyslot_add_by_volume_key),
310
                        DLSYM_ARG(crypt_keyslot_destroy),
311
                        DLSYM_ARG(crypt_keyslot_max),
312
                        DLSYM_ARG(crypt_keyslot_status),
313
                        DLSYM_ARG(crypt_load),
314
                        DLSYM_ARG(crypt_logf),
315
                        DLSYM_ARG(crypt_metadata_locking),
316
                        DLSYM_ARG(crypt_persistent_flags_get),
317
                        DLSYM_ARG(crypt_persistent_flags_set),
318
                        DLSYM_ARG(crypt_reencrypt_init_by_passphrase),
319
                        DLSYM_ARG(crypt_reencrypt_run),
320
                        DLSYM_ARG(crypt_resize),
321
                        DLSYM_ARG(crypt_resume_by_volume_key),
322
                        DLSYM_ARG(crypt_set_data_device),
323
                        DLSYM_ARG(crypt_set_data_offset),
324
                        DLSYM_ARG(crypt_set_debug_level),
325
                        DLSYM_ARG(crypt_set_log_callback),
326
                        DLSYM_ARG(crypt_set_metadata_size),
327
                        DLSYM_ARG(crypt_set_pbkdf_type),
328
                        DLSYM_ARG(crypt_status),
329
                        DLSYM_ARG(crypt_suspend),
330
                        DLSYM_ARG(crypt_token_external_path),
331
                        DLSYM_ARG(crypt_token_json_get),
332
                        DLSYM_ARG(crypt_token_json_set),
333
                        DLSYM_ARG(crypt_token_max),
334
                        DLSYM_ARG(crypt_token_status),
335
                        DLSYM_ARG(crypt_volume_key_get),
336
                        DLSYM_ARG(crypt_volume_key_keyring),
337
                        DLSYM_ARG(crypt_wipe),
338
                        DLSYM_ARG(crypt_get_integrity_info));
339
        if (r <= 0)
33,484✔
340
                return r;
341

342
        /* Optional symbols: present in libcryptsetup 2.7+ only. If unresolved, the prototype keeps its
343
         * static initializer pointing at a fallback that returns -ENOSYS, so call sites can invoke the
344
         * symbol unconditionally. */
345
        DLSYM_OPTIONAL(cryptsetup_dl, crypt_set_keyring_to_link);
15,063✔
346
        DLSYM_OPTIONAL(cryptsetup_dl, crypt_token_set_external_path);
15,063✔
347

348
        /* Redirect the default logging calls of libcryptsetup to our own logging infra. (Note that
349
         * libcryptsetup also maintains per-"struct crypt_device" log functions, which we'll also set
350
         * whenever allocating a "struct crypt_device" context. Why set both? To be defensive: maybe some
351
         * other code loaded into this process also changes the global log functions of libcryptsetup, who
352
         * knows? And if so, we still want our own objects to log via our own infra, at the very least.) */
353
        cryptsetup_enable_logging(NULL);
15,063✔
354

355
        const char *e = secure_getenv("SYSTEMD_CRYPTSETUP_TOKEN_PATH");
15,063✔
356
        if (e) {
15,063✔
357
                r = sym_crypt_token_set_external_path(e);
×
358
                if (r == -ENOSYS)
×
359
                        log_debug("Loaded libcryptsetup does not support setting the external token path, not setting it to '%s'.", e);
×
360
                else if (r < 0)
×
361
                        log_debug_errno(r, "Failed to set the libcryptsetup external token path to '%s', ignoring: %m", e);
×
362
        }
363

364
        return 1;
365
#else
366
        return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
367
                              "libcryptsetup support is not compiled in.");
368
#endif
369
}
370

371
int cryptsetup_get_keyslot_from_token(sd_json_variant *v) {
22✔
372
        int keyslot, r;
22✔
373
        sd_json_variant *w;
22✔
374

375
        /* Parses the "keyslots" field of a LUKS2 token object. The field can be an array, but here we assume
376
         * that it contains a single element only, since that's the only way we ever generate it
377
         * ourselves. */
378

379
        w = sd_json_variant_by_key(v, "keyslots");
22✔
380
        if (!w)
22✔
381
                return -ENOENT;
22✔
382
        if (!sd_json_variant_is_array(w) || sd_json_variant_elements(w) != 1)
22✔
383
                return -EMEDIUMTYPE;
384

385
        w = sd_json_variant_by_index(w, 0);
22✔
386
        if (!w)
22✔
387
                return -ENOENT;
388
        if (!sd_json_variant_is_string(w))
22✔
389
                return -EMEDIUMTYPE;
390

391
        r = safe_atoi(sd_json_variant_string(w), &keyslot);
22✔
392
        if (r < 0)
22✔
393
                return r;
394
        if (keyslot < 0)
22✔
395
                return -EINVAL;
×
396

397
        return keyslot;
398
}
399

400
const char* mangle_none(const char *s) {
214✔
401
        /* A helper that turns cryptsetup/integritysetup/veritysetup "options" strings into NULL if they are effectively empty */
402
        return isempty(s) || STR_IN_SET(s, "-", "none") ? NULL : s;
428✔
403
}
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