Debian Codegraph @

src/shared/cryptsetup-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "sd-dlopen.h"
6 #include "sd-json.h"
7
8 #include "alloc-util.h"
9 #include "cryptsetup-util.h"
10 #include "dlfcn-util.h"
11 #include "escape.h"
12 #include "hexdecoct.h"
13 #include "hmac.h"
14 #include "log.h"
15 #include "parse-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18
19 #if HAVE_LIBCRYPTSETUP
20 DLSYM_PROTOTYPE(crypt_activate_by_passphrase) = NULL;
21 DLSYM_PROTOTYPE(crypt_activate_by_signed_key) = NULL;
22 DLSYM_PROTOTYPE(crypt_activate_by_token_pin) = NULL;
23 DLSYM_PROTOTYPE(crypt_activate_by_volume_key) = NULL;
24 DLSYM_PROTOTYPE(crypt_deactivate) = NULL;
25 DLSYM_PROTOTYPE(crypt_deactivate_by_name) = NULL;
26 DLSYM_PROTOTYPE(crypt_format) = NULL;
27 DLSYM_PROTOTYPE(crypt_free) = NULL;
28 DLSYM_PROTOTYPE(crypt_get_cipher) = NULL;
29 DLSYM_PROTOTYPE(crypt_get_cipher_mode) = NULL;
30 DLSYM_PROTOTYPE(crypt_get_data_offset) = NULL;
31 DLSYM_PROTOTYPE(crypt_get_device_name) = NULL;
32 DLSYM_PROTOTYPE(crypt_get_dir) = NULL;
33 DLSYM_PROTOTYPE(crypt_get_type) = NULL;
34 DLSYM_PROTOTYPE(crypt_get_uuid) = NULL;
35 DLSYM_PROTOTYPE(crypt_get_verity_info) = NULL;
36 DLSYM_PROTOTYPE(crypt_get_volume_key_size) = NULL;
37 DLSYM_PROTOTYPE(crypt_header_restore) = NULL;
38 DLSYM_PROTOTYPE(crypt_init) = NULL;
39 DLSYM_PROTOTYPE(crypt_init_by_name) = NULL;
40 DLSYM_PROTOTYPE(crypt_init_data_device) = NULL;
41 DLSYM_PROTOTYPE(crypt_keyslot_add_by_volume_key) = NULL;
42 DLSYM_PROTOTYPE(crypt_keyslot_destroy) = NULL;
43 DLSYM_PROTOTYPE(crypt_keyslot_max) = NULL;
44 DLSYM_PROTOTYPE(crypt_keyslot_status) = NULL;
45 DLSYM_PROTOTYPE(crypt_load) = NULL;
46 DLSYM_PROTOTYPE(crypt_logf) = NULL;
47 DLSYM_PROTOTYPE(crypt_metadata_locking) = NULL;
48 DLSYM_PROTOTYPE(crypt_persistent_flags_get) = NULL;
49 DLSYM_PROTOTYPE(crypt_persistent_flags_set) = NULL;
50 DLSYM_PROTOTYPE(crypt_reencrypt_init_by_passphrase) = NULL;
51 DLSYM_PROTOTYPE(crypt_reencrypt_run);
52 DLSYM_PROTOTYPE(crypt_resize) = NULL;
53 DLSYM_PROTOTYPE(crypt_resume_by_volume_key) = NULL;
54 DLSYM_PROTOTYPE(crypt_set_data_device) = NULL;
55 DLSYM_PROTOTYPE(crypt_set_data_offset) = NULL;
56 DLSYM_PROTOTYPE(crypt_set_debug_level) = NULL;
57 static int missing_crypt_set_keyring_to_link(
58 struct crypt_device *cd,
59 const char *key_description,
60 const char *old_key_description,
61 const char *key_type_desc,
62 const char *keyring_to_link_vk) {
63 return -ENOSYS;
64 }
65 DLSYM_PROTOTYPE(crypt_set_keyring_to_link) = missing_crypt_set_keyring_to_link;
66 DLSYM_PROTOTYPE(crypt_set_log_callback) = NULL;
67 DLSYM_PROTOTYPE(crypt_set_metadata_size) = NULL;
68 DLSYM_PROTOTYPE(crypt_set_pbkdf_type) = NULL;
69 DLSYM_PROTOTYPE(crypt_status) = NULL;
70 DLSYM_PROTOTYPE(crypt_suspend) = NULL;
71 DLSYM_PROTOTYPE(crypt_token_external_path) = NULL;
72 DLSYM_PROTOTYPE(crypt_token_json_get) = NULL;
73 DLSYM_PROTOTYPE(crypt_token_json_set) = NULL;
74 DLSYM_PROTOTYPE(crypt_token_max) = NULL;
75 static int missing_crypt_token_set_external_path(const char *path) {
76 return -ENOSYS;
77 }
78 DLSYM_PROTOTYPE(crypt_token_set_external_path) = missing_crypt_token_set_external_path;
79 DLSYM_PROTOTYPE(crypt_token_status) = NULL;
80 DLSYM_PROTOTYPE(crypt_volume_key_get) = NULL;
81 DLSYM_PROTOTYPE(crypt_volume_key_keyring) = NULL;
82 DLSYM_PROTOTYPE(crypt_wipe) = NULL;
83 DLSYM_PROTOTYPE(crypt_get_integrity_info) = NULL;
84
85 static void cryptsetup_log_glue(int level, const char *msg, void *usrptr) {
86
87 switch (level) {
88 case CRYPT_LOG_NORMAL:
89 level = LOG_NOTICE;
90 break;
91 case CRYPT_LOG_ERROR:
92 level = LOG_ERR;
93 break;
94 case CRYPT_LOG_VERBOSE:
95 level = LOG_INFO;
96 break;
97 case CRYPT_LOG_DEBUG:
98 level = LOG_DEBUG;
99 break;
100 default:
101 log_error("Unknown libcryptsetup log level: %d", level);
102 level = LOG_ERR;
103 }
104
105 log_full(level, "%s", msg);
106 }
107
108 void cryptsetup_enable_logging(struct crypt_device *cd) {
109 /* It's OK to call this with a NULL parameter, in which case libcryptsetup will set the default log
110 * function.
111 *
112 * Note that this is also called from dlopen_cryptsetup(), which we call here too. Sounds like an
113 * endless loop, but isn't because we break it via the check for 'cryptsetup_dl' early in
114 * dlopen_cryptsetup(). */
115
116 if (dlopen_cryptsetup(LOG_DEBUG) < 0)
117 return; /* If this fails, let's gracefully ignore the issue, this is just debug logging after
118 * all, and if this failed we already generated a debug log message that should help
119 * to track things down. */
120
121 sym_crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
122 sym_crypt_set_debug_level(DEBUG_LOGGING ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
123 }
124
125 int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd) {
126
127 /* With CRYPT_PBKDF_NO_BENCHMARK flag set .time_ms member is ignored
128 * while .iterations must be set at least to recommended minimum value. */
129
130 static const struct crypt_pbkdf_type minimal_pbkdf = {
131 .hash = "sha512",
132 .type = CRYPT_KDF_PBKDF2,
133 .iterations = 1000, /* recommended minimum count for pbkdf2
134 * according to NIST SP 800-132, ch. 5.2 */
135 .flags = CRYPT_PBKDF_NO_BENCHMARK
136 };
137
138 int r;
139
140 /* Sets a minimal PKBDF in case we already have a high entropy key. */
141
142 r = sym_crypt_set_pbkdf_type(cd, &minimal_pbkdf);
143 if (r < 0)
144 return r;
145
146 return 0;
147 }
148
149 int cryptsetup_get_token_as_json(
150 struct crypt_device *cd,
151 int idx,
152 const char *verify_type,
153 sd_json_variant **ret) {
154
155 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
156 const char *text;
157 int r;
158
159 assert(cd);
160
161 /* Extracts and parses the LUKS2 JSON token data from a LUKS2 device. Optionally verifies the type of
162 * the token. Returns:
163 *
164 * -EINVAL → token index out of range or "type" field missing
165 * -ENOENT → token doesn't exist
166 * -EMEDIUMTYPE → "verify_type" specified and doesn't match token's type
167 */
168
169 r = sym_crypt_token_json_get(cd, idx, &text);
170 if (r < 0)
171 return r;
172
173 r = sd_json_parse(text, 0, &v, NULL, NULL);
174 if (r < 0)
175 return r;
176
177 if (verify_type) {
178 sd_json_variant *w;
179
180 w = sd_json_variant_by_key(v, "type");
181 if (!w)
182 return -EINVAL;
183
184 if (!streq_ptr(sd_json_variant_string(w), verify_type))
185 return -EMEDIUMTYPE;
186 }
187
188 if (ret)
189 *ret = TAKE_PTR(v);
190
191 return 0;
192 }
193
194 int cryptsetup_add_token_json(struct crypt_device *cd, sd_json_variant *v) {
195 _cleanup_free_ char *text = NULL;
196 int r;
197
198 r = sd_json_variant_format(v, 0, &text);
199 if (r < 0)
200 return log_debug_errno(r, "Failed to format token data for LUKS: %m");
201
202 log_debug("Adding token text <%s>", text);
203
204 r = sym_crypt_token_json_set(cd, CRYPT_ANY_TOKEN, text);
205 if (r < 0)
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(
212 struct crypt_device *cd,
213 const char *volume_name,
214 char **ret) {
215
216 _cleanup_free_ char *volume = NULL;
217 const char *uuid;
218 char *s;
219
220 assert(ret);
221
222 uuid = sym_crypt_get_uuid(cd);
223 if (!uuid)
224 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get LUKS UUID.");
225
226 if (volume_name)
227 /* avoid ambiguity around ":" once we join things below */
228 volume = xescape(volume_name, ":");
229 else
230 volume = strjoin("luks-", uuid);
231 if (!volume)
232 return log_oom_debug();
233
234 s = strjoin("cryptsetup:", volume, ":", uuid);
235 if (!s)
236 return log_oom_debug();
237
238 *ret = s;
239
240 return 0;
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(
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;
252 uint8_t digest[SHA256_DIGEST_SIZE];
253 char *hex;
254 int r;
255
256 assert(ret);
257
258 r = cryptsetup_get_volume_key_prefix(cd, volume_name, &prefix);
259 if (r < 0)
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);
263
264 hex = hexmem(digest, sizeof(digest));
265 if (!hex)
266 return log_oom_debug();
267
268 *ret = hex;
269
270 return 0;
271 }
272 #endif
273
274 int dlopen_cryptsetup(int log_level) {
275 #if HAVE_LIBCRYPTSETUP
276 static void *cryptsetup_dl = NULL;
277 int r;
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 CRYPTSETUP_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED);
285
286 r = dlopen_many_sym_or_warn(
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)
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);
346 DLSYM_OPTIONAL(cryptsetup_dl, crypt_token_set_external_path);
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);
354
355 const char *e = secure_getenv("SYSTEMD_CRYPTSETUP_TOKEN_PATH");
356 if (e) {
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) {
372 int keyslot, r;
373 sd_json_variant *w;
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");
380 if (!w)
381 return -ENOENT;
382 if (!sd_json_variant_is_array(w) || sd_json_variant_elements(w) != 1)
383 return -EMEDIUMTYPE;
384
385 w = sd_json_variant_by_index(w, 0);
386 if (!w)
387 return -ENOENT;
388 if (!sd_json_variant_is_string(w))
389 return -EMEDIUMTYPE;
390
391 r = safe_atoi(sd_json_variant_string(w), &keyslot);
392 if (r < 0)
393 return r;
394 if (keyslot < 0)
395 return -EINVAL;
396
397 return keyslot;
398 }
399
400 const char* mangle_none(const char *s) {
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;
403 }