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

systemd / systemd / 20669300594

02 Jan 2026 09:00PM UTC coverage: 72.677% (-0.02%) from 72.697%
20669300594

push

github

web-flow
clang-tidy: Enable more warnings (#39910)

25 of 27 new or added lines in 3 files covered. (92.59%)

5655 existing lines in 111 files now uncovered.

310023 of 426578 relevant lines covered (72.68%)

1136999.43 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
DLSYM_PROTOTYPE(crypt_reencrypt_run);
43
DLSYM_PROTOTYPE(crypt_resize) = NULL;
44
DLSYM_PROTOTYPE(crypt_resume_by_volume_key) = NULL;
45
DLSYM_PROTOTYPE(crypt_set_data_device) = NULL;
46
DLSYM_PROTOTYPE(crypt_set_data_offset) = NULL;
47
DLSYM_PROTOTYPE(crypt_set_debug_level) = NULL;
48
DLSYM_PROTOTYPE(crypt_set_log_callback) = NULL;
49
DLSYM_PROTOTYPE(crypt_set_metadata_size) = NULL;
50
DLSYM_PROTOTYPE(crypt_set_pbkdf_type) = NULL;
51
DLSYM_PROTOTYPE(crypt_suspend) = NULL;
52
DLSYM_PROTOTYPE(crypt_token_json_get) = NULL;
53
DLSYM_PROTOTYPE(crypt_token_json_set) = NULL;
54
DLSYM_PROTOTYPE(crypt_token_max) = NULL;
55
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
56
DLSYM_PROTOTYPE(crypt_token_set_external_path) = NULL;
57
#endif
58
DLSYM_PROTOTYPE(crypt_token_status) = NULL;
59
DLSYM_PROTOTYPE(crypt_volume_key_get) = NULL;
60
DLSYM_PROTOTYPE(crypt_volume_key_keyring) = NULL;
61
DLSYM_PROTOTYPE(crypt_wipe) = NULL;
62
DLSYM_PROTOTYPE(crypt_get_integrity_info) = NULL;
63

64
static void cryptsetup_log_glue(int level, const char *msg, void *usrptr) {
30,124✔
65

66
        switch (level) {
30,124✔
67
        case CRYPT_LOG_NORMAL:
68
                level = LOG_NOTICE;
69
                break;
70
        case CRYPT_LOG_ERROR:
17✔
71
                level = LOG_ERR;
17✔
72
                break;
17✔
UNCOV
73
        case CRYPT_LOG_VERBOSE:
×
UNCOV
74
                level = LOG_INFO;
×
UNCOV
75
                break;
×
76
        case CRYPT_LOG_DEBUG:
30,107✔
77
                level = LOG_DEBUG;
30,107✔
78
                break;
30,107✔
79
        default:
UNCOV
80
                log_error("Unknown libcryptsetup log level: %d", level);
×
81
                level = LOG_ERR;
82
        }
83

84
        log_full(level, "%s", msg);
30,124✔
85
}
30,124✔
86

87
void cryptsetup_enable_logging(struct crypt_device *cd) {
11,689✔
88
        /* It's OK to call this with a NULL parameter, in which case libcryptsetup will set the default log
89
         * function.
90
         *
91
         * Note that this is also called from dlopen_cryptsetup(), which we call here too. Sounds like an
92
         * endless loop, but isn't because we break it via the check for 'cryptsetup_dl' early in
93
         * dlopen_cryptsetup(). */
94

95
        if (dlopen_cryptsetup() < 0)
11,689✔
96
                return; /* If this fails, let's gracefully ignore the issue, this is just debug logging after
97
                         * all, and if this failed we already generated a debug log message that should help
98
                         * to track things down. */
99

100
        sym_crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
11,689✔
101
        sym_crypt_set_debug_level(DEBUG_LOGGING ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
11,759✔
102
}
103

104
int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd) {
45✔
105

106
        /* With CRYPT_PBKDF_NO_BENCHMARK flag set .time_ms member is ignored
107
         * while .iterations must be set at least to recommended minimum value. */
108

109
        static const struct crypt_pbkdf_type minimal_pbkdf = {
45✔
110
                .hash = "sha512",
111
                .type = CRYPT_KDF_PBKDF2,
112
                .iterations = 1000, /* recommended minimum count for pbkdf2
113
                                     * according to NIST SP 800-132, ch. 5.2 */
114
                .flags = CRYPT_PBKDF_NO_BENCHMARK
115
        };
116

117
        int r;
45✔
118

119
        /* Sets a minimal PKBDF in case we already have a high entropy key. */
120

121
        r = dlopen_cryptsetup();
45✔
122
        if (r < 0)
45✔
123
                return r;
124

125
        r = sym_crypt_set_pbkdf_type(cd, &minimal_pbkdf);
45✔
126
        if (r < 0)
45✔
UNCOV
127
                return r;
×
128

129
        return 0;
130
}
131

132
int cryptsetup_get_token_as_json(
5,621✔
133
                struct crypt_device *cd,
134
                int idx,
135
                const char *verify_type,
136
                sd_json_variant **ret) {
137

138
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
5,621✔
139
        const char *text;
5,621✔
140
        int r;
5,621✔
141

142
        assert(cd);
5,621✔
143

144
        /* Extracts and parses the LUKS2 JSON token data from a LUKS2 device. Optionally verifies the type of
145
         * the token. Returns:
146
         *
147
         *      -EINVAL → token index out of range or "type" field missing
148
         *      -ENOENT → token doesn't exist
149
         * -EMEDIUMTYPE → "verify_type" specified and doesn't match token's type
150
         */
151

152
        r = dlopen_cryptsetup();
5,621✔
153
        if (r < 0)
5,621✔
154
                return r;
155

156
        r = sym_crypt_token_json_get(cd, idx, &text);
5,621✔
157
        if (r < 0)
5,621✔
158
                return r;
159

160
        r = sd_json_parse(text, 0, &v, NULL, NULL);
403✔
161
        if (r < 0)
403✔
162
                return r;
163

164
        if (verify_type) {
403✔
165
                sd_json_variant *w;
240✔
166

167
                w = sd_json_variant_by_key(v, "type");
240✔
168
                if (!w)
240✔
169
                        return -EINVAL;
170

171
                if (!streq_ptr(sd_json_variant_string(w), verify_type))
240✔
172
                        return -EMEDIUMTYPE;
173
        }
174

175
        if (ret)
179✔
176
                *ret = TAKE_PTR(v);
179✔
177

178
        return 0;
179
}
180

181
int cryptsetup_add_token_json(struct crypt_device *cd, sd_json_variant *v) {
45✔
182
        _cleanup_free_ char *text = NULL;
45✔
183
        int r;
45✔
184

185
        r = dlopen_cryptsetup();
45✔
186
        if (r < 0)
45✔
187
                return r;
188

189
        r = sd_json_variant_format(v, 0, &text);
45✔
190
        if (r < 0)
45✔
UNCOV
191
                return log_debug_errno(r, "Failed to format token data for LUKS: %m");
×
192

193
        log_debug("Adding token text <%s>", text);
45✔
194

195
        r = sym_crypt_token_json_set(cd, CRYPT_ANY_TOKEN, text);
45✔
196
        if (r < 0)
45✔
UNCOV
197
                return log_debug_errno(r, "Failed to write token data to LUKS: %m");
×
198

199
        return 0;
200
}
201
#endif
202

203
int dlopen_cryptsetup(void) {
29,394✔
204
#if HAVE_LIBCRYPTSETUP
205
        int r;
29,394✔
206

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

212
        ELF_NOTE_DLOPEN("cryptsetup",
29,394✔
213
                        "Support for disk encryption, integrity, and authentication",
214
                        ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
215
                        "libcryptsetup.so.12");
216

217
        r = dlopen_many_sym_or_warn(
29,394✔
218
                        &cryptsetup_dl, "libcryptsetup.so.12", LOG_DEBUG,
219
                        DLSYM_ARG(crypt_activate_by_passphrase),
220
                        DLSYM_ARG(crypt_activate_by_signed_key),
221
                        DLSYM_ARG(crypt_activate_by_volume_key),
222
                        DLSYM_ARG(crypt_deactivate_by_name),
223
                        DLSYM_ARG(crypt_format),
224
                        DLSYM_ARG(crypt_free),
225
                        DLSYM_ARG(crypt_get_cipher),
226
                        DLSYM_ARG(crypt_get_cipher_mode),
227
                        DLSYM_ARG(crypt_get_data_offset),
228
                        DLSYM_ARG(crypt_get_device_name),
229
                        DLSYM_ARG(crypt_get_dir),
230
                        DLSYM_ARG(crypt_get_type),
231
                        DLSYM_ARG(crypt_get_uuid),
232
                        DLSYM_ARG(crypt_get_verity_info),
233
                        DLSYM_ARG(crypt_get_volume_key_size),
234
                        DLSYM_ARG(crypt_header_restore),
235
                        DLSYM_ARG(crypt_init),
236
                        DLSYM_ARG(crypt_init_by_name),
237
                        DLSYM_ARG(crypt_keyslot_add_by_volume_key),
238
                        DLSYM_ARG(crypt_keyslot_destroy),
239
                        DLSYM_ARG(crypt_keyslot_max),
240
                        DLSYM_ARG(crypt_load),
241
                        DLSYM_ARG(crypt_metadata_locking),
242
                        DLSYM_ARG(crypt_reencrypt_init_by_passphrase),
243
                        DLSYM_ARG(crypt_reencrypt_run),
244
                        DLSYM_ARG(crypt_resize),
245
                        DLSYM_ARG(crypt_resume_by_volume_key),
246
                        DLSYM_ARG(crypt_set_data_device),
247
                        DLSYM_ARG(crypt_set_data_offset),
248
                        DLSYM_ARG(crypt_set_debug_level),
249
                        DLSYM_ARG(crypt_set_log_callback),
250
                        DLSYM_ARG(crypt_set_metadata_size),
251
                        DLSYM_ARG(crypt_set_pbkdf_type),
252
                        DLSYM_ARG(crypt_suspend),
253
                        DLSYM_ARG(crypt_token_json_get),
254
                        DLSYM_ARG(crypt_token_json_set),
255
                        DLSYM_ARG(crypt_token_max),
256
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
257
                        DLSYM_ARG(crypt_token_set_external_path),
258
#endif
259
                        DLSYM_ARG(crypt_token_status),
260
                        DLSYM_ARG(crypt_volume_key_get),
261
                        DLSYM_ARG(crypt_volume_key_keyring),
262
                        DLSYM_ARG(crypt_wipe),
263
                        DLSYM_ARG(crypt_get_integrity_info));
264
        if (r <= 0)
29,394✔
265
                return r;
266

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

274
        const char *e = secure_getenv("SYSTEMD_CRYPTSETUP_TOKEN_PATH");
10,829✔
275
        if (e) {
10,829✔
276
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
UNCOV
277
                r = sym_crypt_token_set_external_path(e);
×
UNCOV
278
                if (r < 0)
×
UNCOV
279
                        log_debug_errno(r, "Failed to set the libcryptsetup external token path to '%s', ignoring: %m", e);
×
280
#else
281
                log_debug("libcryptsetup version does not support setting the external token path, not setting it to '%s'.", e);
282
#endif
283
        }
284

285
        return 1;
286
#else
287
        return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "cryptsetup support is not compiled in.");
288
#endif
289
}
290

291
int cryptsetup_get_keyslot_from_token(sd_json_variant *v) {
16✔
292
        int keyslot, r;
16✔
293
        sd_json_variant *w;
16✔
294

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

299
        w = sd_json_variant_by_key(v, "keyslots");
16✔
300
        if (!w)
16✔
301
                return -ENOENT;
16✔
302
        if (!sd_json_variant_is_array(w) || sd_json_variant_elements(w) != 1)
16✔
UNCOV
303
                return -EMEDIUMTYPE;
×
304

305
        w = sd_json_variant_by_index(w, 0);
16✔
306
        if (!w)
16✔
307
                return -ENOENT;
308
        if (!sd_json_variant_is_string(w))
16✔
309
                return -EMEDIUMTYPE;
310

311
        r = safe_atoi(sd_json_variant_string(w), &keyslot);
16✔
312
        if (r < 0)
16✔
313
                return r;
314
        if (keyslot < 0)
16✔
UNCOV
315
                return -EINVAL;
×
316

317
        return keyslot;
318
}
319

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