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

systemd / systemd / 19020191358

02 Nov 2025 05:04PM UTC coverage: 72.222% (-0.02%) from 72.241%
19020191358

push

github

web-flow
Enhance docs for ukify and direct kernel boots (#39516)

305246 of 422650 relevant lines covered (72.22%)

1085243.28 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

74
static void cryptsetup_log_glue(int level, const char *msg, void *usrptr) {
27,454✔
75

76
        switch (level) {
27,454✔
77
        case CRYPT_LOG_NORMAL:
78
                level = LOG_NOTICE;
79
                break;
80
        case CRYPT_LOG_ERROR:
13✔
81
                level = LOG_ERR;
13✔
82
                break;
13✔
83
        case CRYPT_LOG_VERBOSE:
×
84
                level = LOG_INFO;
×
85
                break;
×
86
        case CRYPT_LOG_DEBUG:
27,441✔
87
                level = LOG_DEBUG;
27,441✔
88
                break;
27,441✔
89
        default:
90
                log_error("Unknown libcryptsetup log level: %d", level);
×
91
                level = LOG_ERR;
92
        }
93

94
        log_full(level, "%s", msg);
27,454✔
95
}
27,454✔
96

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

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

110
        sym_crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
1,383✔
111
        sym_crypt_set_debug_level(DEBUG_LOGGING ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
1,404✔
112
}
113

114
int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd) {
45✔
115

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

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

127
        int r;
45✔
128

129
        /* Sets a minimal PKBDF in case we already have a high entropy key. */
130

131
        r = dlopen_cryptsetup();
45✔
132
        if (r < 0)
45✔
133
                return r;
134

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

139
        return 0;
140
}
141

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

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

152
        assert(cd);
5,621✔
153

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

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

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

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

174
        if (verify_type) {
403✔
175
                sd_json_variant *w;
240✔
176

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

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

185
        if (ret)
179✔
186
                *ret = TAKE_PTR(v);
179✔
187

188
        return 0;
189
}
190

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

195
        r = dlopen_cryptsetup();
45✔
196
        if (r < 0)
45✔
197
                return r;
198

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

203
        log_debug("Adding token text <%s>", text);
45✔
204

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

209
        return 0;
210
}
211
#endif
212

213
int dlopen_cryptsetup(void) {
7,253✔
214
#if HAVE_LIBCRYPTSETUP
215
        int r;
7,253✔
216

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

222
        ELF_NOTE_DLOPEN("cryptsetup",
7,253✔
223
                        "Support for disk encryption, integrity, and authentication",
224
                        ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
225
                        "libcryptsetup.so.12");
226

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

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

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

299
        return 1;
300
#else
301
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "cryptsetup support is not compiled in.");
302
#endif
303
}
304

305
int cryptsetup_get_keyslot_from_token(sd_json_variant *v) {
16✔
306
        int keyslot, r;
16✔
307
        sd_json_variant *w;
16✔
308

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

313
        w = sd_json_variant_by_key(v, "keyslots");
16✔
314
        if (!w)
16✔
315
                return -ENOENT;
16✔
316
        if (!sd_json_variant_is_array(w) || sd_json_variant_elements(w) != 1)
16✔
317
                return -EMEDIUMTYPE;
×
318

319
        w = sd_json_variant_by_index(w, 0);
16✔
320
        if (!w)
16✔
321
                return -ENOENT;
322
        if (!sd_json_variant_is_string(w))
16✔
323
                return -EMEDIUMTYPE;
324

325
        r = safe_atoi(sd_json_variant_string(w), &keyslot);
16✔
326
        if (r < 0)
16✔
327
                return r;
328
        if (keyslot < 0)
16✔
329
                return -EINVAL;
×
330

331
        return keyslot;
332
}
333

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