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

systemd / systemd / 20355307520

18 Dec 2025 09:15PM UTC coverage: 72.578% (-0.1%) from 72.709%
20355307520

push

github

DaanDeMeyer
mkosi: update debian commit reference to d9f2aa170

* d9f2aa1704 Install systemd-tpm2-generator.8 only for UEFI builds
* ac1c7d8048 Drop dependencies on libcap-dev, no longer used since v259
* c36e5871ca Do not install systemd-sysv-generator.8 in upstream build
* bac0cca0e8 Install new files for upstream build
* 2855fb1302 Update changelog for 259-1 release

309322 of 426195 relevant lines covered (72.58%)

1149469.57 hits per line

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

85.71
/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 "log.h"
11
#include "parse-util.h"
12
#include "string-util.h"
13
#include "strv.h"
14

15
#if HAVE_LIBCRYPTSETUP
16
static void *cryptsetup_dl = NULL;
17

18
DLSYM_PROTOTYPE(crypt_activate_by_passphrase) = NULL;
19
DLSYM_PROTOTYPE(crypt_activate_by_signed_key) = NULL;
20
DLSYM_PROTOTYPE(crypt_activate_by_volume_key) = NULL;
21
DLSYM_PROTOTYPE(crypt_deactivate_by_name) = NULL;
22
DLSYM_PROTOTYPE(crypt_format) = NULL;
23
DLSYM_PROTOTYPE(crypt_free) = NULL;
24
DLSYM_PROTOTYPE(crypt_get_cipher) = NULL;
25
DLSYM_PROTOTYPE(crypt_get_cipher_mode) = NULL;
26
DLSYM_PROTOTYPE(crypt_get_data_offset) = NULL;
27
DLSYM_PROTOTYPE(crypt_get_device_name) = NULL;
28
DLSYM_PROTOTYPE(crypt_get_dir) = NULL;
29
DLSYM_PROTOTYPE(crypt_get_type) = NULL;
30
DLSYM_PROTOTYPE(crypt_get_uuid) = NULL;
31
DLSYM_PROTOTYPE(crypt_get_verity_info) = NULL;
32
DLSYM_PROTOTYPE(crypt_get_volume_key_size) = NULL;
33
DLSYM_PROTOTYPE(crypt_header_restore) = NULL;
34
DLSYM_PROTOTYPE(crypt_init) = NULL;
35
DLSYM_PROTOTYPE(crypt_init_by_name) = NULL;
36
DLSYM_PROTOTYPE(crypt_keyslot_add_by_volume_key) = NULL;
37
DLSYM_PROTOTYPE(crypt_keyslot_destroy) = NULL;
38
DLSYM_PROTOTYPE(crypt_keyslot_max) = NULL;
39
DLSYM_PROTOTYPE(crypt_load) = NULL;
40
DLSYM_PROTOTYPE(crypt_metadata_locking) = NULL;
41
DLSYM_PROTOTYPE(crypt_reencrypt_init_by_passphrase) = NULL;
42
#if HAVE_CRYPT_REENCRYPT_RUN
43
DLSYM_PROTOTYPE(crypt_reencrypt_run);
44
#else
45
DLSYM_PROTOTYPE(crypt_reencrypt);
46
#endif
47
DLSYM_PROTOTYPE(crypt_resize) = NULL;
48
DLSYM_PROTOTYPE(crypt_resume_by_volume_key) = NULL;
49
DLSYM_PROTOTYPE(crypt_set_data_device) = NULL;
50
DLSYM_PROTOTYPE(crypt_set_data_offset) = NULL;
51
DLSYM_PROTOTYPE(crypt_set_debug_level) = NULL;
52
DLSYM_PROTOTYPE(crypt_set_log_callback) = NULL;
53
DLSYM_PROTOTYPE(crypt_set_metadata_size) = NULL;
54
DLSYM_PROTOTYPE(crypt_set_pbkdf_type) = NULL;
55
DLSYM_PROTOTYPE(crypt_suspend) = NULL;
56
DLSYM_PROTOTYPE(crypt_token_json_get) = NULL;
57
DLSYM_PROTOTYPE(crypt_token_json_set) = NULL;
58
#if HAVE_CRYPT_TOKEN_MAX
59
DLSYM_PROTOTYPE(crypt_token_max) = NULL;
60
#else
61
int crypt_token_max(_unused_ const char *type) {
62
    assert(streq(type, CRYPT_LUKS2));
63

64
    return 32;
65
}
66
#endif
67
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
68
DLSYM_PROTOTYPE(crypt_token_set_external_path) = NULL;
69
#endif
70
DLSYM_PROTOTYPE(crypt_token_status) = NULL;
71
DLSYM_PROTOTYPE(crypt_volume_key_get) = NULL;
72
DLSYM_PROTOTYPE(crypt_volume_key_keyring) = NULL;
73
DLSYM_PROTOTYPE(crypt_wipe) = NULL;
74
DLSYM_PROTOTYPE(crypt_get_integrity_info) = NULL;
75

76
static void cryptsetup_log_glue(int level, const char *msg, void *usrptr) {
30,069✔
77

78
        switch (level) {
30,069✔
79
        case CRYPT_LOG_NORMAL:
80
                level = LOG_NOTICE;
81
                break;
82
        case CRYPT_LOG_ERROR:
17✔
83
                level = LOG_ERR;
17✔
84
                break;
17✔
85
        case CRYPT_LOG_VERBOSE:
×
86
                level = LOG_INFO;
×
87
                break;
×
88
        case CRYPT_LOG_DEBUG:
30,052✔
89
                level = LOG_DEBUG;
30,052✔
90
                break;
30,052✔
91
        default:
92
                log_error("Unknown libcryptsetup log level: %d", level);
×
93
                level = LOG_ERR;
94
        }
95

96
        log_full(level, "%s", msg);
30,069✔
97
}
30,069✔
98

99
void cryptsetup_enable_logging(struct crypt_device *cd) {
11,951✔
100
        /* It's OK to call this with a NULL parameter, in which case libcryptsetup will set the default log
101
         * function.
102
         *
103
         * Note that this is also called from dlopen_cryptsetup(), which we call here too. Sounds like an
104
         * endless loop, but isn't because we break it via the check for 'cryptsetup_dl' early in
105
         * dlopen_cryptsetup(). */
106

107
        if (dlopen_cryptsetup() < 0)
11,951✔
108
                return; /* If this fails, let's gracefully ignore the issue, this is just debug logging after
109
                         * all, and if this failed we already generated a debug log message that should help
110
                         * to track things down. */
111

112
        sym_crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
11,951✔
113
        sym_crypt_set_debug_level(DEBUG_LOGGING ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
12,021✔
114
}
115

116
int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd) {
45✔
117

118
        /* With CRYPT_PBKDF_NO_BENCHMARK flag set .time_ms member is ignored
119
         * while .iterations must be set at least to recommended minimum value. */
120

121
        static const struct crypt_pbkdf_type minimal_pbkdf = {
45✔
122
                .hash = "sha512",
123
                .type = CRYPT_KDF_PBKDF2,
124
                .iterations = 1000, /* recommended minimum count for pbkdf2
125
                                     * according to NIST SP 800-132, ch. 5.2 */
126
                .flags = CRYPT_PBKDF_NO_BENCHMARK
127
        };
128

129
        int r;
45✔
130

131
        /* Sets a minimal PKBDF in case we already have a high entropy key. */
132

133
        r = dlopen_cryptsetup();
45✔
134
        if (r < 0)
45✔
135
                return r;
136

137
        r = sym_crypt_set_pbkdf_type(cd, &minimal_pbkdf);
45✔
138
        if (r < 0)
45✔
139
                return r;
×
140

141
        return 0;
142
}
143

144
int cryptsetup_get_token_as_json(
5,621✔
145
                struct crypt_device *cd,
146
                int idx,
147
                const char *verify_type,
148
                sd_json_variant **ret) {
149

150
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
5,621✔
151
        const char *text;
5,621✔
152
        int r;
5,621✔
153

154
        assert(cd);
5,621✔
155

156
        /* Extracts and parses the LUKS2 JSON token data from a LUKS2 device. Optionally verifies the type of
157
         * the token. Returns:
158
         *
159
         *      -EINVAL → token index out of range or "type" field missing
160
         *      -ENOENT → token doesn't exist
161
         * -EMEDIUMTYPE → "verify_type" specified and doesn't match token's type
162
         */
163

164
        r = dlopen_cryptsetup();
5,621✔
165
        if (r < 0)
5,621✔
166
                return r;
167

168
        r = sym_crypt_token_json_get(cd, idx, &text);
5,621✔
169
        if (r < 0)
5,621✔
170
                return r;
171

172
        r = sd_json_parse(text, 0, &v, NULL, NULL);
403✔
173
        if (r < 0)
403✔
174
                return r;
175

176
        if (verify_type) {
403✔
177
                sd_json_variant *w;
240✔
178

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

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

187
        if (ret)
179✔
188
                *ret = TAKE_PTR(v);
179✔
189

190
        return 0;
191
}
192

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

197
        r = dlopen_cryptsetup();
45✔
198
        if (r < 0)
45✔
199
                return r;
200

201
        r = sd_json_variant_format(v, 0, &text);
45✔
202
        if (r < 0)
45✔
203
                return log_debug_errno(r, "Failed to format token data for LUKS: %m");
×
204

205
        log_debug("Adding token text <%s>", text);
45✔
206

207
        r = sym_crypt_token_json_set(cd, CRYPT_ANY_TOKEN, text);
45✔
208
        if (r < 0)
45✔
209
                return log_debug_errno(r, "Failed to write token data to LUKS: %m");
×
210

211
        return 0;
212
}
213
#endif
214

215
int dlopen_cryptsetup(void) {
29,908✔
216
#if HAVE_LIBCRYPTSETUP
217
        int r;
29,908✔
218

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

224
        ELF_NOTE_DLOPEN("cryptsetup",
29,908✔
225
                        "Support for disk encryption, integrity, and authentication",
226
                        ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
227
                        "libcryptsetup.so.12");
228

229
        r = dlopen_many_sym_or_warn(
29,908✔
230
                        &cryptsetup_dl, "libcryptsetup.so.12", LOG_DEBUG,
231
                        DLSYM_ARG(crypt_activate_by_passphrase),
232
                        DLSYM_ARG(crypt_activate_by_signed_key),
233
                        DLSYM_ARG(crypt_activate_by_volume_key),
234
                        DLSYM_ARG(crypt_deactivate_by_name),
235
                        DLSYM_ARG(crypt_format),
236
                        DLSYM_ARG(crypt_free),
237
                        DLSYM_ARG(crypt_get_cipher),
238
                        DLSYM_ARG(crypt_get_cipher_mode),
239
                        DLSYM_ARG(crypt_get_data_offset),
240
                        DLSYM_ARG(crypt_get_device_name),
241
                        DLSYM_ARG(crypt_get_dir),
242
                        DLSYM_ARG(crypt_get_type),
243
                        DLSYM_ARG(crypt_get_uuid),
244
                        DLSYM_ARG(crypt_get_verity_info),
245
                        DLSYM_ARG(crypt_get_volume_key_size),
246
                        DLSYM_ARG(crypt_header_restore),
247
                        DLSYM_ARG(crypt_init),
248
                        DLSYM_ARG(crypt_init_by_name),
249
                        DLSYM_ARG(crypt_keyslot_add_by_volume_key),
250
                        DLSYM_ARG(crypt_keyslot_destroy),
251
                        DLSYM_ARG(crypt_keyslot_max),
252
                        DLSYM_ARG(crypt_load),
253
                        DLSYM_ARG(crypt_metadata_locking),
254
                        DLSYM_ARG(crypt_reencrypt_init_by_passphrase),
255
#if HAVE_CRYPT_REENCRYPT_RUN
256
                        DLSYM_ARG(crypt_reencrypt_run),
257
#else
258
                        DLSYM_ARG(crypt_reencrypt),
259
#endif
260
                        DLSYM_ARG(crypt_resize),
261
                        DLSYM_ARG(crypt_resume_by_volume_key),
262
                        DLSYM_ARG(crypt_set_data_device),
263
                        DLSYM_ARG(crypt_set_data_offset),
264
                        DLSYM_ARG(crypt_set_debug_level),
265
                        DLSYM_ARG(crypt_set_log_callback),
266
                        DLSYM_ARG(crypt_set_metadata_size),
267
                        DLSYM_ARG(crypt_set_pbkdf_type),
268
                        DLSYM_ARG(crypt_suspend),
269
                        DLSYM_ARG(crypt_token_json_get),
270
                        DLSYM_ARG(crypt_token_json_set),
271
#if HAVE_CRYPT_TOKEN_MAX
272
                        DLSYM_ARG(crypt_token_max),
273
#endif
274
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
275
                        DLSYM_ARG(crypt_token_set_external_path),
276
#endif
277
                        DLSYM_ARG(crypt_token_status),
278
                        DLSYM_ARG(crypt_volume_key_get),
279
                        DLSYM_ARG(crypt_volume_key_keyring),
280
                        DLSYM_ARG(crypt_wipe),
281
                        DLSYM_ARG(crypt_get_integrity_info));
282
        if (r <= 0)
29,908✔
283
                return r;
284

285
        /* Redirect the default logging calls of libcryptsetup to our own logging infra. (Note that
286
         * libcryptsetup also maintains per-"struct crypt_device" log functions, which we'll also set
287
         * whenever allocating a "struct crypt_device" context. Why set both? To be defensive: maybe some
288
         * other code loaded into this process also changes the global log functions of libcryptsetup, who
289
         * knows? And if so, we still want our own objects to log via our own infra, at the very least.) */
290
        cryptsetup_enable_logging(NULL);
11,086✔
291

292
        const char *e = secure_getenv("SYSTEMD_CRYPTSETUP_TOKEN_PATH");
11,086✔
293
        if (e) {
11,086✔
294
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
295
                r = sym_crypt_token_set_external_path(e);
×
296
                if (r < 0)
×
297
                        log_debug_errno(r, "Failed to set the libcryptsetup external token path to '%s', ignoring: %m", e);
×
298
#else
299
                log_debug("libcryptsetup version does not support setting the external token path, not setting it to '%s'.", e);
300
#endif
301
        }
302

303
        return 1;
304
#else
305
        return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "cryptsetup support is not compiled in.");
306
#endif
307
}
308

309
int cryptsetup_get_keyslot_from_token(sd_json_variant *v) {
16✔
310
        int keyslot, r;
16✔
311
        sd_json_variant *w;
16✔
312

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

317
        w = sd_json_variant_by_key(v, "keyslots");
16✔
318
        if (!w)
16✔
319
                return -ENOENT;
16✔
320
        if (!sd_json_variant_is_array(w) || sd_json_variant_elements(w) != 1)
16✔
321
                return -EMEDIUMTYPE;
×
322

323
        w = sd_json_variant_by_index(w, 0);
16✔
324
        if (!w)
16✔
325
                return -ENOENT;
326
        if (!sd_json_variant_is_string(w))
16✔
327
                return -EMEDIUMTYPE;
328

329
        r = safe_atoi(sd_json_variant_string(w), &keyslot);
16✔
330
        if (r < 0)
16✔
331
                return r;
332
        if (keyslot < 0)
16✔
333
                return -EINVAL;
×
334

335
        return keyslot;
336
}
337

338
const char* mangle_none(const char *s) {
190✔
339
        /* A helper that turns cryptsetup/integritysetup/veritysetup "options" strings into NULL if they are effectively empty */
340
        return isempty(s) || STR_IN_SET(s, "-", "none") ? NULL : s;
380✔
341
}
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