/src/cryptsetup/lib/setup.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * libcryptsetup - cryptsetup library |
4 | | * |
5 | | * Copyright (C) 2004 Jana Saout <jana@saout.de> |
6 | | * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org> |
7 | | * Copyright (C) 2009-2026 Red Hat, Inc. All rights reserved. |
8 | | * Copyright (C) 2009-2026 Milan Broz |
9 | | */ |
10 | | |
11 | | #include <string.h> |
12 | | #include <stdio.h> |
13 | | #include <stdlib.h> |
14 | | #include <stdarg.h> |
15 | | #if HAVE_SYS_UTSNAME_H |
16 | | #include <sys/utsname.h> |
17 | | #endif |
18 | | #include <errno.h> |
19 | | |
20 | | #include "libcryptsetup.h" |
21 | | #include "luks1/luks.h" |
22 | | #include "luks2/luks2.h" |
23 | | #include "loopaes/loopaes.h" |
24 | | #include "verity/verity.h" |
25 | | #include "tcrypt/tcrypt.h" |
26 | | #include "integrity/integrity.h" |
27 | | #include "bitlk/bitlk.h" |
28 | | #include "fvault2/fvault2.h" |
29 | | #include "utils_device_locking.h" |
30 | | #include "internal.h" |
31 | | #include "keyslot_context.h" |
32 | | #include "luks2/hw_opal/hw_opal.h" |
33 | | |
34 | 0 | #define CRYPT_CD_UNRESTRICTED (1 << 0) |
35 | 0 | #define CRYPT_CD_QUIET (1 << 1) |
36 | | |
37 | | struct crypt_device { |
38 | | char *type; |
39 | | |
40 | | struct device *device; |
41 | | struct device *metadata_device; |
42 | | |
43 | | struct volume_key *volume_key; |
44 | | int rng_type; |
45 | | uint32_t compatibility; |
46 | | struct crypt_pbkdf_type pbkdf; |
47 | | |
48 | | /* global context scope settings */ |
49 | | unsigned key_in_keyring:1; |
50 | | |
51 | | bool link_vk_to_keyring; |
52 | | int32_t keyring_to_link_vk; |
53 | | const char *user_key_name1; |
54 | | const char *user_key_name2; |
55 | | key_type_t keyring_key_type; |
56 | | |
57 | | const char *keyring_description; |
58 | | key_serial_t keyring_id; |
59 | | |
60 | | uint64_t data_offset; |
61 | | uint64_t metadata_size; /* Used in LUKS2 format */ |
62 | | uint64_t keyslots_size; /* Used in LUKS2 format */ |
63 | | |
64 | | /* Workaround for OOM during parallel activation (like in systemd) */ |
65 | | bool memory_hard_pbkdf_lock_enabled; |
66 | | struct crypt_lock_handle *pbkdf_memory_hard_lock; |
67 | | |
68 | | union { |
69 | | struct { /* used in CRYPT_LUKS1 */ |
70 | | struct luks_phdr hdr; |
71 | | char *cipher_spec; |
72 | | } luks1; |
73 | | struct { /* used in CRYPT_LUKS2 */ |
74 | | struct luks2_hdr hdr; |
75 | | char cipher[MAX_CIPHER_LEN]; /* only for compatibility */ |
76 | | char cipher_mode[MAX_CIPHER_LEN]; /* only for compatibility */ |
77 | | char *keyslot_cipher; |
78 | | unsigned int keyslot_key_size; |
79 | | struct luks2_reencrypt *rh; |
80 | | } luks2; |
81 | | struct { /* used in CRYPT_PLAIN */ |
82 | | struct crypt_params_plain hdr; |
83 | | char *cipher_spec; |
84 | | char *cipher; |
85 | | const char *cipher_mode; |
86 | | unsigned int key_size; |
87 | | } plain; |
88 | | struct { /* used in CRYPT_LOOPAES */ |
89 | | struct crypt_params_loopaes hdr; |
90 | | char *cipher_spec; |
91 | | char *cipher; |
92 | | const char *cipher_mode; |
93 | | unsigned int key_size; |
94 | | } loopaes; |
95 | | struct { /* used in CRYPT_VERITY */ |
96 | | struct crypt_params_verity hdr; |
97 | | const char *root_hash; |
98 | | unsigned int root_hash_size; |
99 | | char *uuid; |
100 | | struct device *fec_device; |
101 | | } verity; |
102 | | struct { /* used in CRYPT_TCRYPT */ |
103 | | struct crypt_params_tcrypt params; |
104 | | struct tcrypt_phdr hdr; |
105 | | } tcrypt; |
106 | | struct { /* used in CRYPT_INTEGRITY */ |
107 | | struct crypt_params_integrity params; |
108 | | struct volume_key *journal_mac_key; |
109 | | struct volume_key *journal_crypt_key; |
110 | | uint32_t sb_flags; |
111 | | } integrity; |
112 | | struct { /* used in CRYPT_BITLK */ |
113 | | struct bitlk_metadata params; |
114 | | char *cipher_spec; |
115 | | } bitlk; |
116 | | struct { /* used in CRYPT_FVAULT2 */ |
117 | | struct fvault2_params params; |
118 | | } fvault2; |
119 | | struct { /* used if initialized without header by name */ |
120 | | char *active_name; |
121 | | /* buffers, must refresh from kernel on every query */ |
122 | | char cipher_spec[MAX_CIPHER_LEN*2+1]; |
123 | | char cipher[MAX_CIPHER_LEN]; |
124 | | char integrity_spec[MAX_INTEGRITY_LEN]; |
125 | | const char *cipher_mode; |
126 | | unsigned int key_size; |
127 | | uint32_t sector_size; |
128 | | } none; |
129 | | } u; |
130 | | |
131 | | /* callbacks definitions */ |
132 | | void (*log)(int level, const char *msg, void *usrptr); |
133 | | void *log_usrptr; |
134 | | int (*confirm)(const char *msg, void *usrptr); |
135 | | void *confirm_usrptr; |
136 | | }; |
137 | | |
138 | | /* Just to suppress redundant messages about crypto backend */ |
139 | | static int _crypto_logged = 0; |
140 | | |
141 | | /* Log helper */ |
142 | | static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL; |
143 | | static void *_default_log_usrptr = NULL; |
144 | | static int _debug_level = 0; |
145 | | |
146 | | /* Library can do metadata locking */ |
147 | | static int _metadata_locking = 1; |
148 | | |
149 | | /* Library scope detection for kernel keyring support */ |
150 | | static int _kernel_keyring_supported; |
151 | | |
152 | | /* Library allowed to use kernel keyring for loading VK in kernel crypto layer */ |
153 | | static int _vk_via_keyring = 1; |
154 | | |
155 | | void crypt_set_debug_level(int level) |
156 | 0 | { |
157 | 0 | _debug_level = level; |
158 | 0 | } |
159 | | |
160 | | int crypt_get_debug_level(void) |
161 | 0 | { |
162 | 0 | return _debug_level; |
163 | 0 | } |
164 | | |
165 | | void crypt_log(struct crypt_device *cd, int level, const char *msg) |
166 | 258k | { |
167 | 258k | if (!msg) |
168 | 0 | return; |
169 | | |
170 | 258k | if (level < _debug_level) |
171 | 254k | return; |
172 | | |
173 | 3.64k | if (cd && cd->log) |
174 | 0 | cd->log(level, msg, cd->log_usrptr); |
175 | 3.64k | else if (_default_log) |
176 | 1.97k | _default_log(level, msg, _default_log_usrptr); |
177 | | /* Default to stdout/stderr if there is no callback. */ |
178 | 1.66k | else |
179 | 1.66k | fprintf(level == CRYPT_LOG_ERROR ? stderr : stdout, "%s", msg); |
180 | 3.64k | } |
181 | | |
182 | | __attribute__((format(printf, 3, 4))) |
183 | | void crypt_logf(struct crypt_device *cd, int level, const char *format, ...) |
184 | 258k | { |
185 | 258k | va_list argp; |
186 | 258k | char target[LOG_MAX_LEN + 2]; |
187 | 258k | int len; |
188 | | |
189 | 258k | va_start(argp, format); |
190 | | |
191 | 258k | len = vsnprintf(&target[0], LOG_MAX_LEN, format, argp); |
192 | 258k | if (len > 0 && len < LOG_MAX_LEN) { |
193 | | /* All verbose and error messages in tools end with EOL. */ |
194 | 258k | if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR || |
195 | 254k | level == CRYPT_LOG_DEBUG || level == CRYPT_LOG_DEBUG_JSON) |
196 | 258k | strncat(target, "\n", LOG_MAX_LEN); |
197 | | |
198 | 258k | crypt_log(cd, level, target); |
199 | 258k | } |
200 | | |
201 | 258k | va_end(argp); |
202 | 258k | } |
203 | | |
204 | | static const char *mdata_device_path(struct crypt_device *cd) |
205 | 19.4k | { |
206 | 19.4k | return device_path(cd->metadata_device ?: cd->device); |
207 | 19.4k | } |
208 | | |
209 | | static const char *data_device_path(struct crypt_device *cd) |
210 | 0 | { |
211 | 0 | return device_path(cd->device); |
212 | 0 | } |
213 | | |
214 | | /* internal only */ |
215 | | struct device *crypt_metadata_device(struct crypt_device *cd) |
216 | 49.2k | { |
217 | 49.2k | return cd->metadata_device ?: cd->device; |
218 | 49.2k | } |
219 | | |
220 | | struct device *crypt_data_device(struct crypt_device *cd) |
221 | 3.66k | { |
222 | 3.66k | return cd->device; |
223 | 3.66k | } |
224 | | |
225 | | uint64_t crypt_get_metadata_size_bytes(struct crypt_device *cd) |
226 | 0 | { |
227 | 0 | assert(cd); |
228 | 0 | return cd->metadata_size; |
229 | 0 | } |
230 | | |
231 | | uint64_t crypt_get_keyslots_size_bytes(struct crypt_device *cd) |
232 | 0 | { |
233 | 0 | assert(cd); |
234 | 0 | return cd->keyslots_size; |
235 | 0 | } |
236 | | |
237 | | uint64_t crypt_get_data_offset_sectors(struct crypt_device *cd) |
238 | 0 | { |
239 | 0 | assert(cd); |
240 | 0 | return cd->data_offset; |
241 | 0 | } |
242 | | |
243 | | int crypt_opal_supported(struct crypt_device *cd, struct device *opal_device) |
244 | 0 | { |
245 | 0 | int r; |
246 | |
|
247 | 0 | assert(cd); |
248 | 0 | assert(opal_device); |
249 | |
|
250 | 0 | r = opal_supported(cd, opal_device); |
251 | 0 | if (r <= 0) { |
252 | 0 | if (r == -ENOTSUP) |
253 | 0 | log_err(cd, _("OPAL support is disabled in libcryptsetup.")); |
254 | 0 | else |
255 | 0 | log_err(cd, _("Device %s or kernel does not support OPAL encryption."), |
256 | 0 | device_path(opal_device)); |
257 | 0 | r = -EINVAL; |
258 | 0 | } else |
259 | 0 | r = 0; |
260 | |
|
261 | 0 | return r; |
262 | 0 | } |
263 | | |
264 | | int init_crypto(struct crypt_device *ctx) |
265 | 18.6k | { |
266 | 18.6k | #if HAVE_SYS_UTSNAME_H |
267 | 18.6k | struct utsname uts; |
268 | 18.6k | #endif |
269 | 18.6k | int r; |
270 | | |
271 | 18.6k | r = crypt_random_init(ctx); |
272 | 18.6k | if (r < 0) { |
273 | 0 | log_err(ctx, _("Cannot initialize crypto RNG backend.")); |
274 | 0 | return r; |
275 | 0 | } |
276 | | |
277 | 18.6k | r = crypt_backend_init(); |
278 | 18.6k | if (r < 0) |
279 | 0 | log_err(ctx, _("Cannot initialize crypto backend.")); |
280 | | |
281 | 18.6k | if (!r && !_crypto_logged) { |
282 | 3 | log_dbg(ctx, "Crypto backend (%s%s) initialized in cryptsetup library version %s.", |
283 | 3 | crypt_backend_version(), crypt_argon2_version(), PACKAGE_VERSION); |
284 | | |
285 | 3 | #if HAVE_SYS_UTSNAME_H |
286 | 3 | if (!uname(&uts)) |
287 | 3 | log_dbg(ctx, "Detected kernel %s %s %s.", |
288 | 3 | uts.sysname, uts.release, uts.machine); |
289 | 3 | #endif |
290 | 3 | _crypto_logged = 1; |
291 | 3 | } |
292 | | |
293 | 18.6k | return r; |
294 | 18.6k | } |
295 | | |
296 | | static int process_key(struct crypt_device *cd, const char *hash_name, |
297 | | size_t key_size, const char *pass, size_t passLen, |
298 | | struct volume_key **vk) |
299 | 0 | { |
300 | 0 | int r; |
301 | 0 | void *key = NULL; |
302 | |
|
303 | 0 | if (!key_size) |
304 | 0 | return -EINVAL; |
305 | | |
306 | 0 | if (hash_name) { |
307 | 0 | key = crypt_safe_alloc(key_size); |
308 | 0 | if (!key) |
309 | 0 | return -ENOMEM; |
310 | | |
311 | 0 | r = crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen); |
312 | 0 | if (r < 0) { |
313 | 0 | if (r == -ENOENT) |
314 | 0 | log_err(cd, _("Hash algorithm %s not supported."), |
315 | 0 | hash_name); |
316 | 0 | else |
317 | 0 | log_err(cd, _("Key processing error (using hash %s)."), |
318 | 0 | hash_name); |
319 | 0 | crypt_safe_free(key); |
320 | 0 | return -EINVAL; |
321 | 0 | } |
322 | 0 | *vk = crypt_alloc_volume_key_by_safe_alloc(&key); |
323 | 0 | } else if (passLen >= key_size) { |
324 | 0 | *vk = crypt_alloc_volume_key(key_size, pass); |
325 | 0 | } else { |
326 | 0 | key = crypt_safe_alloc(key_size); |
327 | 0 | if (!key) |
328 | 0 | return -ENOMEM; |
329 | | |
330 | 0 | crypt_safe_memcpy(key, pass, passLen); |
331 | |
|
332 | 0 | *vk = crypt_alloc_volume_key_by_safe_alloc(&key); |
333 | 0 | } |
334 | | |
335 | 0 | r = *vk ? 0 : -ENOMEM; |
336 | |
|
337 | 0 | crypt_safe_free(key); |
338 | |
|
339 | 0 | return r; |
340 | 0 | } |
341 | | |
342 | | static int isPLAIN(const char *type) |
343 | 12.2k | { |
344 | 12.2k | return (type && !strcmp(CRYPT_PLAIN, type)); |
345 | 12.2k | } |
346 | | |
347 | | static int isLUKS1(const char *type) |
348 | 41.9k | { |
349 | 41.9k | return (type && !strcmp(CRYPT_LUKS1, type)); |
350 | 41.9k | } |
351 | | |
352 | | static int isLUKS2(const char *type) |
353 | 36.5k | { |
354 | 36.5k | return (type && !strcmp(CRYPT_LUKS2, type)); |
355 | 36.5k | } |
356 | | |
357 | | static int isLUKS(const char *type) |
358 | 0 | { |
359 | 0 | return (isLUKS2(type) || isLUKS1(type)); |
360 | 0 | } |
361 | | |
362 | | static int isLOOPAES(const char *type) |
363 | 10.4k | { |
364 | 10.4k | return (type && !strcmp(CRYPT_LOOPAES, type)); |
365 | 10.4k | } |
366 | | |
367 | | static int isVERITY(const char *type) |
368 | 15.0k | { |
369 | 15.0k | return (type && !strcmp(CRYPT_VERITY, type)); |
370 | 15.0k | } |
371 | | |
372 | | static int isTCRYPT(const char *type) |
373 | 3.97k | { |
374 | 3.97k | return (type && !strcmp(CRYPT_TCRYPT, type)); |
375 | 3.97k | } |
376 | | |
377 | | static int isINTEGRITY(const char *type) |
378 | 13.8k | { |
379 | 13.8k | return (type && !strcmp(CRYPT_INTEGRITY, type)); |
380 | 13.8k | } |
381 | | |
382 | | static int isBITLK(const char *type) |
383 | 12.8k | { |
384 | 12.8k | return (type && !strcmp(CRYPT_BITLK, type)); |
385 | 12.8k | } |
386 | | |
387 | | static int isFVAULT2(const char *type) |
388 | 1.71k | { |
389 | 1.71k | return (type && !strcmp(CRYPT_FVAULT2, type)); |
390 | 1.71k | } |
391 | | |
392 | | static int _onlyLUKS(struct crypt_device *cd, uint32_t cdflags, uint32_t mask) |
393 | 0 | { |
394 | 0 | int r = 0; |
395 | |
|
396 | 0 | if (cd && !cd->type) { |
397 | 0 | if (!(cdflags & CRYPT_CD_QUIET)) |
398 | 0 | log_err(cd, _("Cannot determine device type. Incompatible activation of device?")); |
399 | 0 | r = -EINVAL; |
400 | 0 | } |
401 | |
|
402 | 0 | if (!cd || !isLUKS(cd->type)) { |
403 | 0 | if (!(cdflags & CRYPT_CD_QUIET)) |
404 | 0 | log_err(cd, _("This operation is supported only for LUKS device.")); |
405 | 0 | r = -EINVAL; |
406 | 0 | } |
407 | |
|
408 | 0 | if (r || (cdflags & CRYPT_CD_UNRESTRICTED) || isLUKS1(cd->type)) |
409 | 0 | return r; |
410 | | |
411 | 0 | return LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, mask, cdflags & CRYPT_CD_QUIET); |
412 | 0 | } |
413 | | |
414 | | static int onlyLUKSunrestricted(struct crypt_device *cd) |
415 | 0 | { |
416 | 0 | return _onlyLUKS(cd, CRYPT_CD_UNRESTRICTED, 0); |
417 | 0 | } |
418 | | |
419 | | static int onlyLUKSnoRequirements(struct crypt_device *cd) |
420 | 0 | { |
421 | 0 | return _onlyLUKS(cd, 0, 0); |
422 | 0 | } |
423 | | |
424 | | static int onlyLUKS(struct crypt_device *cd) |
425 | 0 | { |
426 | 0 | return _onlyLUKS(cd, 0, CRYPT_REQUIREMENT_OPAL | CRYPT_REQUIREMENT_INLINE_HW_TAGS); |
427 | 0 | } |
428 | | |
429 | | static int _onlyLUKS2(struct crypt_device *cd, uint32_t cdflags, uint32_t mask) |
430 | 0 | { |
431 | 0 | int r = 0; |
432 | |
|
433 | 0 | if (cd && !cd->type) { |
434 | 0 | if (!(cdflags & CRYPT_CD_QUIET)) |
435 | 0 | log_err(cd, _("Cannot determine device type. Incompatible activation of device?")); |
436 | 0 | r = -EINVAL; |
437 | 0 | } |
438 | |
|
439 | 0 | if (!cd || !isLUKS2(cd->type)) { |
440 | 0 | if (!(cdflags & CRYPT_CD_QUIET)) |
441 | 0 | log_err(cd, _("This operation is supported only for LUKS2 device.")); |
442 | 0 | r = -EINVAL; |
443 | 0 | } |
444 | |
|
445 | 0 | if (r || (cdflags & CRYPT_CD_UNRESTRICTED)) |
446 | 0 | return r; |
447 | | |
448 | 0 | return LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, mask, cdflags & CRYPT_CD_QUIET); |
449 | 0 | } |
450 | | |
451 | | static int onlyLUKS2unrestricted(struct crypt_device *cd) |
452 | 0 | { |
453 | 0 | return _onlyLUKS2(cd, CRYPT_CD_UNRESTRICTED, 0); |
454 | 0 | } |
455 | | |
456 | | /* Internal only */ |
457 | | int onlyLUKS2(struct crypt_device *cd) |
458 | 0 | { |
459 | 0 | return _onlyLUKS2(cd, 0, CRYPT_REQUIREMENT_OPAL | CRYPT_REQUIREMENT_INLINE_HW_TAGS); |
460 | 0 | } |
461 | | |
462 | | /* Internal only */ |
463 | | int onlyLUKS2reencrypt(struct crypt_device *cd) |
464 | 0 | { |
465 | 0 | return _onlyLUKS2(cd, 0, CRYPT_REQUIREMENT_ONLINE_REENCRYPT); |
466 | 0 | } |
467 | | |
468 | | static void crypt_set_null_type(struct crypt_device *cd) |
469 | 12.2k | { |
470 | 12.2k | free(cd->type); |
471 | 12.2k | cd->type = NULL; |
472 | 12.2k | cd->data_offset = 0; |
473 | 12.2k | cd->metadata_size = 0; |
474 | 12.2k | cd->keyslots_size = 0; |
475 | 12.2k | crypt_safe_memzero(&cd->u, sizeof(cd->u)); |
476 | 12.2k | } |
477 | | |
478 | | static void crypt_reset_null_type(struct crypt_device *cd) |
479 | 11.6k | { |
480 | 11.6k | if (cd->type) |
481 | 0 | return; |
482 | | |
483 | 11.6k | free(cd->u.none.active_name); |
484 | 11.6k | cd->u.none.active_name = NULL; |
485 | 11.6k | } |
486 | | |
487 | | /* keyslot helpers */ |
488 | | static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot) |
489 | 0 | { |
490 | 0 | crypt_keyslot_info ki; |
491 | |
|
492 | 0 | if (*keyslot == CRYPT_ANY_SLOT) { |
493 | 0 | if (isLUKS1(cd->type)) |
494 | 0 | *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr); |
495 | 0 | else |
496 | 0 | *keyslot = LUKS2_keyslot_find_empty(cd, &cd->u.luks2.hdr, 0); |
497 | 0 | if (*keyslot < 0) { |
498 | 0 | log_err(cd, _("All key slots full.")); |
499 | 0 | return -EINVAL; |
500 | 0 | } |
501 | 0 | } |
502 | | |
503 | 0 | if (isLUKS1(cd->type)) |
504 | 0 | ki = LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot); |
505 | 0 | else |
506 | 0 | ki = LUKS2_keyslot_info(&cd->u.luks2.hdr, *keyslot); |
507 | 0 | switch (ki) { |
508 | 0 | case CRYPT_SLOT_INVALID: |
509 | 0 | log_err(cd, _("Key slot %d is invalid, please select between 0 and %d."), |
510 | 0 | *keyslot, crypt_keyslot_max(cd->type) - 1); |
511 | 0 | return -EINVAL; |
512 | 0 | case CRYPT_SLOT_INACTIVE: |
513 | 0 | break; |
514 | 0 | default: |
515 | 0 | log_err(cd, _("Key slot %d is full, please select another one."), |
516 | 0 | *keyslot); |
517 | 0 | return -EINVAL; |
518 | 0 | } |
519 | | |
520 | 0 | log_dbg(cd, "Selected keyslot %d.", *keyslot); |
521 | 0 | return 0; |
522 | 0 | } |
523 | | |
524 | | int PLAIN_activate(struct crypt_device *cd, |
525 | | const char *name, |
526 | | struct volume_key *vk, |
527 | | uint64_t size, |
528 | | uint32_t flags) |
529 | 0 | { |
530 | 0 | int r; |
531 | 0 | struct crypt_dm_active_device dmd = { |
532 | 0 | .flags = flags, |
533 | 0 | .size = size, |
534 | 0 | }; |
535 | |
|
536 | 0 | log_dbg(cd, "Trying to activate PLAIN device %s using cipher %s.", |
537 | 0 | name, crypt_get_cipher_spec(cd)); |
538 | |
|
539 | 0 | if (MISALIGNED(size, device_block_size(cd, crypt_data_device(cd)) >> SECTOR_SHIFT)) { |
540 | 0 | log_err(cd, _("Device size is not aligned to device logical block size.")); |
541 | 0 | return -EINVAL; |
542 | 0 | } |
543 | | |
544 | 0 | r = dm_crypt_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd), |
545 | 0 | vk, crypt_get_cipher_spec(cd), crypt_get_iv_offset(cd), |
546 | 0 | crypt_get_data_offset(cd), NULL, 0, 0, crypt_get_sector_size(cd)); |
547 | 0 | if (r < 0) |
548 | 0 | return r; |
549 | | |
550 | 0 | r = create_or_reload_device(cd, name, CRYPT_PLAIN, &dmd); |
551 | |
|
552 | 0 | dm_targets_free(cd, &dmd); |
553 | 0 | return r; |
554 | 0 | } |
555 | | |
556 | | int crypt_confirm(struct crypt_device *cd, const char *msg) |
557 | 0 | { |
558 | 0 | if (!cd || !cd->confirm) |
559 | 0 | return 1; |
560 | 0 | else |
561 | 0 | return cd->confirm(msg, cd->confirm_usrptr); |
562 | 0 | } |
563 | | |
564 | | void crypt_set_log_callback(struct crypt_device *cd, |
565 | | void (*log)(int level, const char *msg, void *usrptr), |
566 | | void *usrptr) |
567 | 2.30k | { |
568 | 2.30k | if (!cd) { |
569 | 2.30k | _default_log = log; |
570 | 2.30k | _default_log_usrptr = usrptr; |
571 | 2.30k | } else { |
572 | 0 | cd->log = log; |
573 | 0 | cd->log_usrptr = usrptr; |
574 | 0 | } |
575 | 2.30k | } |
576 | | |
577 | | void crypt_set_confirm_callback(struct crypt_device *cd, |
578 | | int (*confirm)(const char *msg, void *usrptr), |
579 | | void *usrptr) |
580 | 0 | { |
581 | 0 | if (cd) { |
582 | 0 | cd->confirm = confirm; |
583 | 0 | cd->confirm_usrptr = usrptr; |
584 | 0 | } |
585 | 0 | } |
586 | | |
587 | | const char *crypt_get_dir(void) |
588 | 0 | { |
589 | 0 | return dm_get_dir(); |
590 | 0 | } |
591 | | |
592 | | int crypt_init(struct crypt_device **cd, const char *device) |
593 | 7.72k | { |
594 | 7.72k | struct crypt_device *h = NULL; |
595 | 7.72k | int r; |
596 | | |
597 | 7.72k | if (!cd) |
598 | 0 | return -EINVAL; |
599 | | |
600 | 7.72k | log_dbg(NULL, "Allocating context for crypt device %s.", device ?: "(none)"); |
601 | | #if !HAVE_DECL_O_CLOEXEC |
602 | | log_dbg(NULL, "Running without O_CLOEXEC."); |
603 | | #endif |
604 | | |
605 | 7.72k | if (!(h = crypt_zalloc(sizeof(struct crypt_device)))) |
606 | 0 | return -ENOMEM; |
607 | | |
608 | 7.72k | r = device_alloc(NULL, &h->device, device); |
609 | 7.72k | if (r < 0) { |
610 | 0 | free(h); |
611 | 0 | return r; |
612 | 0 | } |
613 | | |
614 | 7.72k | dm_backend_init(NULL); |
615 | | |
616 | 7.72k | h->rng_type = crypt_random_default_key_rng(); |
617 | | |
618 | 7.72k | *cd = h; |
619 | 7.72k | return 0; |
620 | 7.72k | } |
621 | | |
622 | | static int crypt_check_data_device_size(struct crypt_device *cd) |
623 | 0 | { |
624 | 0 | int r; |
625 | 0 | uint64_t size, size_min; |
626 | | |
627 | | /* Check data device size, require at least header or one sector */ |
628 | 0 | size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE; |
629 | |
|
630 | 0 | r = device_size(cd->device, &size); |
631 | 0 | if (r < 0) |
632 | 0 | return r; |
633 | | |
634 | 0 | if (size < size_min) { |
635 | 0 | log_err(cd, _("Header detected but device %s is too small."), |
636 | 0 | device_path(cd->device)); |
637 | 0 | return -EINVAL; |
638 | 0 | } |
639 | | |
640 | 0 | return r; |
641 | 0 | } |
642 | | |
643 | | static int _crypt_set_data_device(struct crypt_device *cd, const char *device) |
644 | 0 | { |
645 | 0 | struct device *dev = NULL; |
646 | 0 | int r; |
647 | |
|
648 | 0 | r = device_alloc(cd, &dev, device); |
649 | 0 | if (r < 0) |
650 | 0 | return r; |
651 | | |
652 | 0 | if (!cd->metadata_device) { |
653 | 0 | cd->metadata_device = cd->device; |
654 | 0 | } else |
655 | 0 | device_free(cd, cd->device); |
656 | |
|
657 | 0 | cd->device = dev; |
658 | |
|
659 | 0 | r = crypt_check_data_device_size(cd); |
660 | 0 | if (!r && isLUKS2(cd->type)) |
661 | 0 | device_set_block_size(crypt_data_device(cd), LUKS2_get_sector_size(&cd->u.luks2.hdr)); |
662 | |
|
663 | 0 | return r; |
664 | 0 | } |
665 | | |
666 | | int crypt_set_data_device(struct crypt_device *cd, const char *device) |
667 | 0 | { |
668 | | /* metadata device must be set */ |
669 | 0 | if (!cd || !cd->device || !device) |
670 | 0 | return -EINVAL; |
671 | | |
672 | 0 | log_dbg(cd, "Setting ciphertext data device to %s.", device ?: "(none)"); |
673 | |
|
674 | 0 | if (!isLUKS1(cd->type) && !isLUKS2(cd->type) && !isVERITY(cd->type) && |
675 | 0 | !isINTEGRITY(cd->type) && !isTCRYPT(cd->type)) { |
676 | 0 | log_err(cd, _("This operation is not supported for this device type.")); |
677 | 0 | return -EINVAL; |
678 | 0 | } |
679 | | |
680 | 0 | if (isLUKS2(cd->type) && crypt_get_luks2_reencrypt(cd)) { |
681 | 0 | log_err(cd, _("Illegal operation with reencryption in-progress.")); |
682 | 0 | return -EINVAL; |
683 | 0 | } |
684 | | |
685 | 0 | return _crypt_set_data_device(cd, device); |
686 | 0 | } |
687 | | |
688 | | int crypt_init_data_device(struct crypt_device **cd, const char *device, const char *data_device) |
689 | 0 | { |
690 | 0 | int r; |
691 | |
|
692 | 0 | if (!cd) |
693 | 0 | return -EINVAL; |
694 | | |
695 | 0 | r = crypt_init(cd, device); |
696 | 0 | if (r || !data_device || !strcmp(device, data_device)) |
697 | 0 | return r; |
698 | | |
699 | 0 | log_dbg(NULL, "Setting ciphertext data device to %s.", data_device); |
700 | 0 | r = _crypt_set_data_device(*cd, data_device); |
701 | 0 | if (r) { |
702 | 0 | crypt_free(*cd); |
703 | 0 | *cd = NULL; |
704 | 0 | } |
705 | |
|
706 | 0 | return r; |
707 | 0 | } |
708 | | |
709 | | static void crypt_free_type(struct crypt_device *cd, const char *force_type) |
710 | 12.2k | { |
711 | 12.2k | const char *type = force_type ?: cd->type; |
712 | | |
713 | 12.2k | if (isPLAIN(type)) { |
714 | 0 | free(CONST_CAST(void*)cd->u.plain.hdr.hash); |
715 | 0 | free(cd->u.plain.cipher); |
716 | 0 | free(cd->u.plain.cipher_spec); |
717 | 12.2k | } else if (isLUKS2(type)) { |
718 | 1.73k | LUKS2_reencrypt_free(cd, cd->u.luks2.rh); |
719 | 1.73k | LUKS2_hdr_free(cd, &cd->u.luks2.hdr); |
720 | 1.73k | free(cd->u.luks2.keyslot_cipher); |
721 | 10.4k | } else if (isLUKS1(type)) { |
722 | 22 | free(cd->u.luks1.cipher_spec); |
723 | 10.4k | } else if (isLOOPAES(type)) { |
724 | 0 | free(CONST_CAST(void*)cd->u.loopaes.hdr.hash); |
725 | 0 | free(cd->u.loopaes.cipher); |
726 | 0 | free(cd->u.loopaes.cipher_spec); |
727 | 10.4k | } else if (isVERITY(type)) { |
728 | 561 | free(CONST_CAST(void*)cd->u.verity.hdr.hash_name); |
729 | 561 | free(CONST_CAST(void*)cd->u.verity.hdr.data_device); |
730 | 561 | free(CONST_CAST(void*)cd->u.verity.hdr.hash_device); |
731 | 561 | free(CONST_CAST(void*)cd->u.verity.hdr.fec_device); |
732 | 561 | free(CONST_CAST(void*)cd->u.verity.hdr.salt); |
733 | 561 | free(CONST_CAST(void*)cd->u.verity.root_hash); |
734 | 561 | free(cd->u.verity.uuid); |
735 | 561 | device_free(cd, cd->u.verity.fec_device); |
736 | 9.91k | } else if (isINTEGRITY(type)) { |
737 | 539 | free(CONST_CAST(void*)cd->u.integrity.params.integrity); |
738 | 539 | free(CONST_CAST(void*)cd->u.integrity.params.journal_integrity); |
739 | 539 | free(CONST_CAST(void*)cd->u.integrity.params.journal_crypt); |
740 | 539 | crypt_free_volume_key(cd->u.integrity.journal_crypt_key); |
741 | 539 | crypt_free_volume_key(cd->u.integrity.journal_mac_key); |
742 | 9.37k | } else if (isBITLK(type)) { |
743 | 1.71k | free(cd->u.bitlk.cipher_spec); |
744 | 1.71k | BITLK_bitlk_metadata_free(&cd->u.bitlk.params); |
745 | 7.65k | } else if (!type) { |
746 | 5.93k | free(cd->u.none.active_name); |
747 | 5.93k | cd->u.none.active_name = NULL; |
748 | 5.93k | } |
749 | | |
750 | 12.2k | crypt_set_null_type(cd); |
751 | 12.2k | } |
752 | | |
753 | | /* internal only */ |
754 | | struct crypt_pbkdf_type *crypt_get_pbkdf(struct crypt_device *cd) |
755 | 3.47k | { |
756 | 3.47k | return &cd->pbkdf; |
757 | 3.47k | } |
758 | | |
759 | | /* |
760 | | * crypt_load() helpers |
761 | | */ |
762 | | static int _crypt_load_luks2(struct crypt_device *cd, int reload, int repair) |
763 | 5.42k | { |
764 | 5.42k | int r; |
765 | 5.42k | char *type = NULL; |
766 | 5.42k | struct luks2_hdr hdr2 = {}; |
767 | | |
768 | 5.42k | log_dbg(cd, "%soading LUKS2 header (repair %sabled).", reload ? "Rel" : "L", repair ? "en" : "dis"); |
769 | | |
770 | 5.42k | r = LUKS2_hdr_read(cd, &hdr2, repair); |
771 | 5.42k | if (r) |
772 | 3.68k | return r; |
773 | | |
774 | 1.73k | if (!reload) { |
775 | 1.73k | type = strdup(CRYPT_LUKS2); |
776 | 1.73k | if (!type) { |
777 | 0 | r = -ENOMEM; |
778 | 0 | goto out; |
779 | 0 | } |
780 | 1.73k | } |
781 | | |
782 | 1.73k | if (verify_pbkdf_params(cd, &cd->pbkdf)) { |
783 | 1.73k | r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2); |
784 | 1.73k | if (r) |
785 | 0 | goto out; |
786 | 1.73k | } |
787 | | |
788 | 1.73k | if (reload) { |
789 | 0 | LUKS2_hdr_free(cd, &cd->u.luks2.hdr); |
790 | 0 | free(cd->u.luks2.keyslot_cipher); |
791 | 0 | } else |
792 | 1.73k | cd->type = type; |
793 | | |
794 | 1.73k | r = 0; |
795 | 1.73k | memcpy(&cd->u.luks2.hdr, &hdr2, sizeof(hdr2)); |
796 | 1.73k | cd->u.luks2.keyslot_cipher = NULL; |
797 | 1.73k | cd->u.luks2.rh = NULL; |
798 | | |
799 | 1.73k | out: |
800 | 1.73k | if (r) { |
801 | 0 | free(type); |
802 | 0 | LUKS2_hdr_free(cd, &hdr2); |
803 | 0 | } |
804 | 1.73k | return r; |
805 | 1.73k | } |
806 | | |
807 | | static void _luks2_rollback(struct crypt_device *cd) |
808 | 0 | { |
809 | 0 | if (!cd || !isLUKS2(cd->type)) |
810 | 0 | return; |
811 | | |
812 | 0 | if (LUKS2_hdr_rollback(cd, &cd->u.luks2.hdr)) { |
813 | 0 | log_err(cd, _("Failed to rollback LUKS2 metadata in memory.")); |
814 | 0 | return; |
815 | 0 | } |
816 | | |
817 | 0 | free(cd->u.luks2.keyslot_cipher); |
818 | 0 | cd->u.luks2.keyslot_cipher = NULL; |
819 | 0 | } |
820 | | |
821 | | static int _crypt_load_luks(struct crypt_device *cd, const char *requested_type, |
822 | | bool quiet, bool repair) |
823 | 7.16k | { |
824 | 7.16k | char *cipher_spec; |
825 | 7.16k | struct luks_phdr hdr = {}; |
826 | 7.16k | int r, version; |
827 | | |
828 | 7.16k | r = init_crypto(cd); |
829 | 7.16k | if (r < 0) |
830 | 0 | return r; |
831 | | |
832 | | /* This will return 0 if primary LUKS2 header is damaged */ |
833 | 7.16k | version = LUKS2_hdr_version_unlocked(cd, NULL); |
834 | | |
835 | 7.16k | if ((isLUKS1(requested_type) && version == 2) || |
836 | 7.16k | (isLUKS2(requested_type) && version == 1)) |
837 | 3 | return -EINVAL; |
838 | | |
839 | 7.15k | if (requested_type) |
840 | 7.15k | version = 0; |
841 | | |
842 | 7.15k | if (isLUKS1(requested_type) || version == 1) { |
843 | 1.73k | if (isLUKS2(cd->type)) { |
844 | 0 | log_dbg(cd, "Context is already initialized to type %s", cd->type); |
845 | 0 | return -EINVAL; |
846 | 0 | } |
847 | | |
848 | 1.73k | if (verify_pbkdf_params(cd, &cd->pbkdf)) { |
849 | 1.73k | r = init_pbkdf_type(cd, NULL, CRYPT_LUKS1); |
850 | 1.73k | if (r) |
851 | 0 | return r; |
852 | 1.73k | } |
853 | | |
854 | 1.73k | r = LUKS_read_phdr(&hdr, !quiet, repair, cd); |
855 | 1.73k | if (r) |
856 | 1.71k | goto out; |
857 | | |
858 | 22 | if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1))) { |
859 | 0 | r = -ENOMEM; |
860 | 0 | goto out; |
861 | 0 | } |
862 | | |
863 | | /* Set hash to the same as in the loaded header */ |
864 | 22 | if (!cd->pbkdf.hash || strcmp(cd->pbkdf.hash, hdr.hashSpec)) { |
865 | 15 | free(CONST_CAST(void*)cd->pbkdf.hash); |
866 | 15 | cd->pbkdf.hash = strdup(hdr.hashSpec); |
867 | 15 | if (!cd->pbkdf.hash) { |
868 | 0 | r = -ENOMEM; |
869 | 0 | goto out; |
870 | 0 | } |
871 | 15 | } |
872 | | |
873 | 22 | if (asprintf(&cipher_spec, "%s-%s", hdr.cipherName, hdr.cipherMode) < 0) { |
874 | 0 | r = -ENOMEM; |
875 | 0 | goto out; |
876 | 0 | } |
877 | | |
878 | 22 | free(cd->u.luks1.cipher_spec); |
879 | 22 | cd->u.luks1.cipher_spec = cipher_spec; |
880 | | |
881 | 22 | memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr)); |
882 | 5.42k | } else if (isLUKS2(requested_type) || version == 2 || version == 0) { |
883 | 5.42k | if (isLUKS1(cd->type)) { |
884 | 0 | log_dbg(cd, "Context is already initialized to type %s", cd->type); |
885 | 0 | return -EINVAL; |
886 | 0 | } |
887 | | |
888 | | /* |
889 | | * Current LUKS2 repair just overrides blkid probes |
890 | | * and perform auto-recovery if possible. This is safe |
891 | | * unless future LUKS2 repair code do something more |
892 | | * sophisticated. In such case we would need to check |
893 | | * for LUKS2 requirements and decide if it's safe to |
894 | | * perform repair. |
895 | | */ |
896 | 5.42k | r = _crypt_load_luks2(cd, cd->type != NULL, repair); |
897 | 5.42k | if (!r) |
898 | 1.73k | device_set_block_size(crypt_data_device(cd), LUKS2_get_sector_size(&cd->u.luks2.hdr)); |
899 | 3.68k | else if (!quiet) |
900 | 0 | log_err(cd, _("Device %s is not a valid LUKS device."), mdata_device_path(cd)); |
901 | 5.42k | } else { |
902 | 0 | if (version > 2) |
903 | 0 | log_err(cd, _("Unsupported LUKS version %d."), version); |
904 | 0 | r = -EINVAL; |
905 | 0 | } |
906 | 7.15k | out: |
907 | 7.15k | crypt_safe_memzero(&hdr, sizeof(hdr)); |
908 | | |
909 | 7.15k | return r; |
910 | 7.15k | } |
911 | | |
912 | | static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params) |
913 | 0 | { |
914 | 0 | int r; |
915 | |
|
916 | 0 | if (!params) |
917 | 0 | return -EINVAL; |
918 | | |
919 | 0 | r = init_crypto(cd); |
920 | 0 | if (r < 0) |
921 | 0 | return r; |
922 | | |
923 | 0 | memcpy(&cd->u.tcrypt.params, params, sizeof(*params)); |
924 | |
|
925 | 0 | r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params); |
926 | |
|
927 | 0 | cd->u.tcrypt.params.passphrase = NULL; |
928 | 0 | cd->u.tcrypt.params.passphrase_size = 0; |
929 | 0 | cd->u.tcrypt.params.keyfiles = NULL; |
930 | 0 | cd->u.tcrypt.params.keyfiles_count = 0; |
931 | 0 | cd->u.tcrypt.params.veracrypt_pim = 0; |
932 | |
|
933 | 0 | if (r < 0) |
934 | 0 | goto out; |
935 | | |
936 | 0 | if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT))) |
937 | 0 | r = -ENOMEM; |
938 | 0 | out: |
939 | 0 | if (r < 0) |
940 | 0 | crypt_free_type(cd, CRYPT_TCRYPT); |
941 | 0 | return r; |
942 | 0 | } |
943 | | |
944 | | static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params) |
945 | 561 | { |
946 | 561 | int r; |
947 | 561 | uint64_t sb_offset = 0; |
948 | | |
949 | 561 | r = init_crypto(cd); |
950 | 561 | if (r < 0) |
951 | 0 | return r; |
952 | | |
953 | 561 | if (params && params->flags & CRYPT_VERITY_NO_HEADER) |
954 | 0 | return -EINVAL; |
955 | | |
956 | 561 | if (params) |
957 | 0 | sb_offset = params->hash_area_offset; |
958 | | |
959 | 561 | r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr); |
960 | 561 | if (r < 0) |
961 | 539 | goto out; |
962 | | |
963 | 22 | if (!cd->type && !(cd->type = strdup(CRYPT_VERITY))) { |
964 | 0 | r = -ENOMEM; |
965 | 0 | goto out; |
966 | 0 | } |
967 | | |
968 | 22 | if (params) |
969 | 0 | cd->u.verity.hdr.flags = params->flags; |
970 | | |
971 | | /* Hash availability checked in sb load */ |
972 | 22 | cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name); |
973 | 22 | if (cd->u.verity.root_hash_size > 4096) { |
974 | 0 | r = -EINVAL; |
975 | 0 | goto out; |
976 | 0 | } |
977 | | |
978 | 22 | if (params && params->data_device && |
979 | 0 | (r = crypt_set_data_device(cd, params->data_device)) < 0) |
980 | 0 | goto out; |
981 | | |
982 | 22 | if (params && params->fec_device) { |
983 | 0 | r = device_alloc(cd, &cd->u.verity.fec_device, params->fec_device); |
984 | 0 | if (r < 0) |
985 | 0 | goto out; |
986 | 0 | cd->u.verity.hdr.fec_area_offset = params->fec_area_offset; |
987 | 0 | cd->u.verity.hdr.fec_roots = params->fec_roots; |
988 | 0 | } |
989 | 561 | out: |
990 | 561 | if (r < 0) |
991 | 539 | crypt_free_type(cd, CRYPT_VERITY); |
992 | 561 | return r; |
993 | 22 | } |
994 | | |
995 | | static int _crypt_load_integrity(struct crypt_device *cd, |
996 | | struct crypt_params_integrity *params) |
997 | 539 | { |
998 | 539 | int r; |
999 | | |
1000 | 539 | r = init_crypto(cd); |
1001 | 539 | if (r < 0) |
1002 | 0 | return r; |
1003 | | |
1004 | 539 | r = INTEGRITY_read_sb(cd, &cd->u.integrity.params, &cd->u.integrity.sb_flags); |
1005 | 539 | if (r < 0) |
1006 | 529 | goto out; |
1007 | | |
1008 | | // FIXME: add checks for fields in integrity sb vs params |
1009 | | |
1010 | 10 | r = -ENOMEM; |
1011 | 10 | if (params) { |
1012 | 0 | cd->u.integrity.params.journal_watermark = params->journal_watermark; |
1013 | 0 | cd->u.integrity.params.journal_commit_time = params->journal_commit_time; |
1014 | 0 | cd->u.integrity.params.buffer_sectors = params->buffer_sectors; |
1015 | 0 | if (params->integrity && |
1016 | 0 | !(cd->u.integrity.params.integrity = strdup(params->integrity))) |
1017 | 0 | goto out; |
1018 | 0 | cd->u.integrity.params.integrity_key_size = params->integrity_key_size; |
1019 | 0 | if (params->journal_integrity && |
1020 | 0 | !(cd->u.integrity.params.journal_integrity = strdup(params->journal_integrity))) |
1021 | 0 | goto out; |
1022 | 0 | if (params->journal_crypt && |
1023 | 0 | !(cd->u.integrity.params.journal_crypt = strdup(params->journal_crypt))) |
1024 | 0 | goto out; |
1025 | | |
1026 | 0 | if (params->journal_crypt_key) { |
1027 | 0 | cd->u.integrity.journal_crypt_key = |
1028 | 0 | crypt_alloc_volume_key(params->journal_crypt_key_size, |
1029 | 0 | params->journal_crypt_key); |
1030 | 0 | if (!cd->u.integrity.journal_crypt_key) |
1031 | 0 | goto out; |
1032 | 0 | } |
1033 | 0 | if (params->journal_integrity_key) { |
1034 | 0 | cd->u.integrity.journal_mac_key = |
1035 | 0 | crypt_alloc_volume_key(params->journal_integrity_key_size, |
1036 | 0 | params->journal_integrity_key); |
1037 | 0 | if (!cd->u.integrity.journal_mac_key) |
1038 | 0 | goto out; |
1039 | 0 | } |
1040 | 0 | } |
1041 | | |
1042 | 10 | if (!cd->type && !(cd->type = strdup(CRYPT_INTEGRITY))) |
1043 | 0 | goto out; |
1044 | 10 | r = 0; |
1045 | 539 | out: |
1046 | 539 | if (r < 0) |
1047 | 529 | crypt_free_type(cd, CRYPT_INTEGRITY); |
1048 | 539 | return r; |
1049 | 10 | } |
1050 | | |
1051 | | static int _crypt_load_bitlk(struct crypt_device *cd) |
1052 | 1.71k | { |
1053 | 1.71k | int r; |
1054 | | |
1055 | 1.71k | r = init_crypto(cd); |
1056 | 1.71k | if (r < 0) |
1057 | 0 | return r; |
1058 | | |
1059 | 1.71k | r = BITLK_read_sb(cd, &cd->u.bitlk.params); |
1060 | 1.71k | if (r < 0) |
1061 | 1.71k | goto out; |
1062 | | |
1063 | 0 | if (asprintf(&cd->u.bitlk.cipher_spec, "%s-%s", |
1064 | 0 | cd->u.bitlk.params.cipher, cd->u.bitlk.params.cipher_mode) < 0) { |
1065 | 0 | cd->u.bitlk.cipher_spec = NULL; |
1066 | 0 | r = -ENOMEM; |
1067 | 0 | goto out; |
1068 | 0 | } |
1069 | | |
1070 | 0 | if (!cd->type && !(cd->type = strdup(CRYPT_BITLK))) { |
1071 | 0 | r = -ENOMEM; |
1072 | 0 | goto out; |
1073 | 0 | } |
1074 | | |
1075 | 0 | device_set_block_size(crypt_data_device(cd), cd->u.bitlk.params.sector_size); |
1076 | 1.71k | out: |
1077 | 1.71k | if (r < 0) |
1078 | 1.71k | crypt_free_type(cd, CRYPT_BITLK); |
1079 | 1.71k | return r; |
1080 | 0 | } |
1081 | | |
1082 | | static int _crypt_load_fvault2(struct crypt_device *cd) |
1083 | 1.71k | { |
1084 | 1.71k | int r; |
1085 | | |
1086 | 1.71k | r = init_crypto(cd); |
1087 | 1.71k | if (r < 0) |
1088 | 0 | return r; |
1089 | | |
1090 | 1.71k | r = FVAULT2_read_metadata(cd, &cd->u.fvault2.params); |
1091 | 1.71k | if (r < 0) |
1092 | 1.71k | goto out; |
1093 | | |
1094 | 0 | if (!cd->type && !(cd->type = strdup(CRYPT_FVAULT2))) |
1095 | 0 | r = -ENOMEM; |
1096 | 1.71k | out: |
1097 | 1.71k | if (r < 0) |
1098 | 1.71k | crypt_free_type(cd, CRYPT_FVAULT2); |
1099 | 1.71k | return r; |
1100 | 0 | } |
1101 | | |
1102 | | int crypt_load(struct crypt_device *cd, |
1103 | | const char *requested_type, |
1104 | | void *params) |
1105 | 11.6k | { |
1106 | 11.6k | int r; |
1107 | | |
1108 | 11.6k | if (!cd) |
1109 | 0 | return -EINVAL; |
1110 | | |
1111 | 11.6k | log_dbg(cd, "Trying to load %s crypt type from device %s.", |
1112 | 11.6k | requested_type ?: "any", mdata_device_path(cd) ?: "(none)"); |
1113 | | |
1114 | 11.6k | if (!crypt_metadata_device(cd)) |
1115 | 0 | return -EINVAL; |
1116 | | |
1117 | 11.6k | crypt_reset_null_type(cd); |
1118 | 11.6k | cd->data_offset = 0; |
1119 | 11.6k | cd->metadata_size = 0; |
1120 | 11.6k | cd->keyslots_size = 0; |
1121 | | |
1122 | 11.6k | if (!requested_type || isLUKS1(requested_type) || isLUKS2(requested_type)) { |
1123 | 7.16k | if (cd->type && !isLUKS1(cd->type) && !isLUKS2(cd->type)) { |
1124 | 0 | log_dbg(cd, "Context is already initialized to type %s", cd->type); |
1125 | 0 | return -EINVAL; |
1126 | 0 | } |
1127 | | |
1128 | 7.16k | r = _crypt_load_luks(cd, requested_type, true, false); |
1129 | 7.16k | } else if (isVERITY(requested_type)) { |
1130 | 561 | if (cd->type && !isVERITY(cd->type)) { |
1131 | 0 | log_dbg(cd, "Context is already initialized to type %s", cd->type); |
1132 | 0 | return -EINVAL; |
1133 | 0 | } |
1134 | 561 | r = _crypt_load_verity(cd, params); |
1135 | 3.97k | } else if (isTCRYPT(requested_type)) { |
1136 | 0 | if (cd->type && !isTCRYPT(cd->type)) { |
1137 | 0 | log_dbg(cd, "Context is already initialized to type %s", cd->type); |
1138 | 0 | return -EINVAL; |
1139 | 0 | } |
1140 | 0 | r = _crypt_load_tcrypt(cd, params); |
1141 | 3.97k | } else if (isINTEGRITY(requested_type)) { |
1142 | 539 | if (cd->type && !isINTEGRITY(cd->type)) { |
1143 | 0 | log_dbg(cd, "Context is already initialized to type %s", cd->type); |
1144 | 0 | return -EINVAL; |
1145 | 0 | } |
1146 | 539 | r = _crypt_load_integrity(cd, params); |
1147 | 3.43k | } else if (isBITLK(requested_type)) { |
1148 | 1.71k | if (cd->type && !isBITLK(cd->type)) { |
1149 | 0 | log_dbg(cd, "Context is already initialized to type %s", cd->type); |
1150 | 0 | return -EINVAL; |
1151 | 0 | } |
1152 | 1.71k | r = _crypt_load_bitlk(cd); |
1153 | 1.71k | } else if (isFVAULT2(requested_type)) { |
1154 | 1.71k | if (cd->type && !isFVAULT2(cd->type)) { |
1155 | 0 | log_dbg(cd, "Context is already initialized to type %s", cd->type); |
1156 | 0 | return -EINVAL; |
1157 | 0 | } |
1158 | 1.71k | r = _crypt_load_fvault2(cd); |
1159 | 1.71k | } else |
1160 | 0 | return -EINVAL; |
1161 | | |
1162 | 11.6k | return r; |
1163 | 11.6k | } |
1164 | | |
1165 | | /* |
1166 | | * crypt_init() helpers |
1167 | | */ |
1168 | | static int _init_by_name_crypt_none(struct crypt_device *cd) |
1169 | 0 | { |
1170 | 0 | int r; |
1171 | 0 | char _mode[MAX_CIPHER_LEN]; |
1172 | 0 | struct crypt_dm_active_device dmd; |
1173 | 0 | struct dm_target *tgt = &dmd.segment; |
1174 | |
|
1175 | 0 | if (cd->type || !cd->u.none.active_name) |
1176 | 0 | return -EINVAL; |
1177 | | |
1178 | 0 | r = dm_query_device(cd, cd->u.none.active_name, |
1179 | 0 | DM_ACTIVE_CRYPT_CIPHER | |
1180 | 0 | DM_ACTIVE_CRYPT_KEYSIZE, &dmd); |
1181 | 0 | if (r < 0) |
1182 | 0 | return r; |
1183 | 0 | if (!single_segment(&dmd) || tgt->type != DM_CRYPT) |
1184 | 0 | r = -EINVAL; |
1185 | 0 | if (r >= 0) |
1186 | 0 | r = crypt_parse_name_and_mode(tgt->u.crypt.cipher, |
1187 | 0 | cd->u.none.cipher, NULL, |
1188 | 0 | _mode); |
1189 | |
|
1190 | 0 | if (!r) { |
1191 | 0 | r = snprintf(cd->u.none.cipher_spec, sizeof(cd->u.none.cipher_spec), |
1192 | 0 | "%s-%s", cd->u.none.cipher, _mode); |
1193 | 0 | if (r < 0 || (size_t)r >= sizeof(cd->u.none.cipher_spec)) |
1194 | 0 | r = -EINVAL; |
1195 | 0 | else { |
1196 | 0 | cd->u.none.cipher_mode = cd->u.none.cipher_spec + strlen(cd->u.none.cipher) + 1; |
1197 | 0 | cd->u.none.key_size = crypt_volume_key_length(tgt->u.crypt.vk); |
1198 | 0 | r = 0; |
1199 | 0 | } |
1200 | 0 | } |
1201 | |
|
1202 | 0 | if (!r && tgt->u.crypt.integrity) { |
1203 | 0 | r = snprintf(cd->u.none.integrity_spec, sizeof(cd->u.none.integrity_spec), |
1204 | 0 | "%s", tgt->u.crypt.integrity); |
1205 | 0 | if (r < 0 || (size_t)r >= sizeof(cd->u.none.integrity_spec)) |
1206 | 0 | r = -EINVAL; |
1207 | 0 | else |
1208 | 0 | r = 0; |
1209 | 0 | } |
1210 | |
|
1211 | 0 | cd->u.none.sector_size = tgt->u.crypt.sector_size; |
1212 | |
|
1213 | 0 | dm_targets_free(cd, &dmd); |
1214 | 0 | return r; |
1215 | 0 | } |
1216 | | |
1217 | | static const char *LUKS_UUID(struct crypt_device *cd) |
1218 | 0 | { |
1219 | 0 | if (!cd) |
1220 | 0 | return NULL; |
1221 | 0 | else if (isLUKS1(cd->type)) |
1222 | 0 | return cd->u.luks1.hdr.uuid; |
1223 | 0 | else if (isLUKS2(cd->type)) |
1224 | 0 | return cd->u.luks2.hdr.uuid; |
1225 | | |
1226 | 0 | return NULL; |
1227 | 0 | } |
1228 | | |
1229 | | static int _init_by_name_crypt(struct crypt_device *cd, const char *name) |
1230 | 0 | { |
1231 | 0 | bool found = false; |
1232 | 0 | char **dep, *cipher_spec = NULL, cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN]; |
1233 | 0 | char deps_uuid_prefix[40], *deps[MAX_DM_DEPS+1] = {}; |
1234 | 0 | const char *dev; |
1235 | 0 | char *iname = NULL; |
1236 | 0 | int key_nums, r; |
1237 | 0 | struct crypt_dm_active_device dmd, dmdi = {}, dmdep = {}; |
1238 | 0 | struct dm_target *tgt = &dmd.segment, *tgti = &dmdi.segment; |
1239 | |
|
1240 | 0 | r = dm_query_device(cd, name, |
1241 | 0 | DM_ACTIVE_DEVICE | |
1242 | 0 | DM_ACTIVE_UUID | |
1243 | 0 | DM_ACTIVE_CRYPT_CIPHER | |
1244 | 0 | DM_ACTIVE_CRYPT_KEYSIZE, &dmd); |
1245 | 0 | if (r < 0) |
1246 | 0 | return r; |
1247 | | |
1248 | 0 | if (tgt->type != DM_CRYPT && tgt->type != DM_LINEAR) { |
1249 | 0 | log_dbg(cd, "Unsupported device table detected in %s.", name); |
1250 | 0 | r = -EINVAL; |
1251 | 0 | goto out; |
1252 | 0 | } |
1253 | | |
1254 | 0 | r = -EINVAL; |
1255 | |
|
1256 | 0 | if (dmd.uuid) { |
1257 | 0 | r = snprintf(deps_uuid_prefix, sizeof(deps_uuid_prefix), CRYPT_SUBDEV "-%.32s", dmd.uuid + 6); |
1258 | 0 | if (r < 0 || (size_t)r != (sizeof(deps_uuid_prefix) - 1)) |
1259 | 0 | r = -EINVAL; |
1260 | 0 | } |
1261 | |
|
1262 | 0 | if (r >= 0) { |
1263 | 0 | r = dm_device_deps(cd, name, deps_uuid_prefix, deps, ARRAY_SIZE(deps)); |
1264 | 0 | if (r) |
1265 | 0 | goto out; |
1266 | 0 | } |
1267 | | |
1268 | 0 | r = crypt_parse_name_and_mode(tgt->type == DM_LINEAR ? "null" : tgt->u.crypt.cipher, cipher, |
1269 | 0 | &key_nums, cipher_mode); |
1270 | 0 | if (r < 0) { |
1271 | | /* Allow crypt null context with unknown cipher string */ |
1272 | 0 | if (tgt->type == DM_CRYPT && !tgt->u.crypt.integrity) { |
1273 | 0 | crypt_set_null_type(cd); |
1274 | 0 | r = 0; |
1275 | 0 | goto out; |
1276 | 0 | } |
1277 | 0 | log_err(cd, _("No known cipher specification pattern detected for active device %s."), name); |
1278 | 0 | goto out; |
1279 | 0 | } |
1280 | | |
1281 | 0 | dep = deps; |
1282 | |
|
1283 | 0 | if (tgt->type == DM_CRYPT && tgt->u.crypt.tag_size && |
1284 | 0 | (iname = dm_get_active_iname(cd, name))) { |
1285 | |
|
1286 | 0 | r = dm_query_device(cd, iname, DM_ACTIVE_DEVICE, &dmdi); |
1287 | 0 | free(iname); |
1288 | 0 | if (r < 0) |
1289 | 0 | goto out; |
1290 | | /* |
1291 | | * Data device for crypt with integrity is not dm-integrity device, |
1292 | | * but always the device underlying dm-integrity. |
1293 | | */ |
1294 | 0 | device_free(cd, cd->device); |
1295 | 0 | MOVE_REF(cd->device, tgti->data_device); |
1296 | 0 | } |
1297 | | |
1298 | | /* do not try to lookup LUKS2 header in detached header mode */ |
1299 | 0 | if (dmd.uuid && !cd->metadata_device && !found) { |
1300 | 0 | while (*dep && !found) { |
1301 | 0 | r = dm_query_device(cd, *dep, DM_ACTIVE_DEVICE, &dmdep); |
1302 | 0 | if (r < 0) |
1303 | 0 | goto out; |
1304 | | |
1305 | 0 | tgt = &dmdep.segment; |
1306 | |
|
1307 | 0 | while (tgt && !found) { |
1308 | 0 | dev = device_path(tgt->data_device); |
1309 | 0 | if (!dev) { |
1310 | 0 | tgt = tgt->next; |
1311 | 0 | continue; |
1312 | 0 | } |
1313 | 0 | if (!strstr(dev, dm_get_dir()) || |
1314 | 0 | !crypt_string_in(dev + strlen(dm_get_dir()) + 1, deps, ARRAY_SIZE(deps))) { |
1315 | 0 | device_free(cd, cd->device); |
1316 | 0 | MOVE_REF(cd->device, tgt->data_device); |
1317 | 0 | found = true; |
1318 | 0 | } |
1319 | 0 | tgt = tgt->next; |
1320 | 0 | } |
1321 | 0 | dep++; |
1322 | 0 | dm_targets_free(cd, &dmdep); |
1323 | 0 | } |
1324 | 0 | } |
1325 | | |
1326 | 0 | if (asprintf(&cipher_spec, "%s-%s", cipher, cipher_mode) < 0) { |
1327 | 0 | cipher_spec = NULL; |
1328 | 0 | r = -ENOMEM; |
1329 | 0 | goto out; |
1330 | 0 | } |
1331 | | |
1332 | 0 | tgt = &dmd.segment; |
1333 | 0 | r = 0; |
1334 | |
|
1335 | 0 | if (isPLAIN(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) { |
1336 | 0 | cd->u.plain.hdr.hash = NULL; /* no way to get this */ |
1337 | 0 | cd->u.plain.hdr.offset = tgt->u.crypt.offset; |
1338 | 0 | cd->u.plain.hdr.skip = tgt->u.crypt.iv_offset; |
1339 | 0 | cd->u.plain.hdr.sector_size = tgt->u.crypt.sector_size; |
1340 | 0 | cd->u.plain.key_size = crypt_volume_key_length(tgt->u.crypt.vk); |
1341 | 0 | cd->u.plain.cipher = strdup(cipher); |
1342 | 0 | if (!cd->u.plain.cipher) { |
1343 | 0 | r = -ENOMEM; |
1344 | 0 | goto out; |
1345 | 0 | } |
1346 | 0 | MOVE_REF(cd->u.plain.cipher_spec, cipher_spec); |
1347 | 0 | cd->u.plain.cipher_mode = cd->u.plain.cipher_spec + strlen(cipher) + 1; |
1348 | 0 | if (dmd.flags & CRYPT_ACTIVATE_KEYRING_KEY) |
1349 | 0 | crypt_set_key_in_keyring(cd, 1); |
1350 | 0 | } else if (isLOOPAES(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) { |
1351 | 0 | cd->u.loopaes.hdr.offset = tgt->u.crypt.offset; |
1352 | 0 | cd->u.loopaes.cipher = strdup(cipher); |
1353 | 0 | if (!cd->u.loopaes.cipher) { |
1354 | 0 | r = -ENOMEM; |
1355 | 0 | goto out; |
1356 | 0 | } |
1357 | 0 | MOVE_REF(cd->u.loopaes.cipher_spec, cipher_spec); |
1358 | 0 | cd->u.loopaes.cipher_mode = cd->u.loopaes.cipher_spec + strlen(cipher) + 1; |
1359 | | /* version 3 uses last key for IV */ |
1360 | 0 | if (crypt_volume_key_length(tgt->u.crypt.vk) % key_nums) |
1361 | 0 | key_nums++; |
1362 | 0 | cd->u.loopaes.key_size = crypt_volume_key_length(tgt->u.crypt.vk) / key_nums; |
1363 | 0 | } else if (isLUKS1(cd->type) || isLUKS2(cd->type)) { |
1364 | 0 | if (crypt_metadata_device(cd)) { |
1365 | 0 | r = _crypt_load_luks(cd, cd->type, true, false); |
1366 | 0 | if (r < 0) { |
1367 | 0 | log_dbg(cd, "LUKS device header does not match active device."); |
1368 | 0 | crypt_set_null_type(cd); |
1369 | 0 | device_close(cd, cd->metadata_device); |
1370 | 0 | device_close(cd, cd->device); |
1371 | 0 | r = 0; |
1372 | 0 | goto out; |
1373 | 0 | } |
1374 | | /* check whether UUIDs match each other */ |
1375 | 0 | r = dm_uuid_cmp(dmd.uuid, LUKS_UUID(cd)); |
1376 | 0 | if (r < 0) { |
1377 | 0 | log_dbg(cd, "LUKS device header uuid: %s mismatches DM returned uuid %s", |
1378 | 0 | LUKS_UUID(cd), dmd.uuid); |
1379 | 0 | crypt_free_type(cd, NULL); |
1380 | 0 | r = 0; |
1381 | 0 | goto out; |
1382 | 0 | } |
1383 | 0 | } else { |
1384 | 0 | log_dbg(cd, "LUKS device header not available."); |
1385 | 0 | crypt_set_null_type(cd); |
1386 | 0 | r = 0; |
1387 | 0 | } |
1388 | 0 | } else if (isTCRYPT(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) { |
1389 | 0 | r = TCRYPT_init_by_name(cd, name, dmd.uuid, tgt, &cd->device, |
1390 | 0 | &cd->u.tcrypt.params, &cd->u.tcrypt.hdr); |
1391 | 0 | } else if (isBITLK(cd->type)) { |
1392 | 0 | r = _crypt_load_bitlk(cd); |
1393 | 0 | if (r < 0) { |
1394 | 0 | log_dbg(cd, "BITLK device header not available."); |
1395 | 0 | crypt_set_null_type(cd); |
1396 | 0 | r = 0; |
1397 | 0 | } |
1398 | 0 | } else if (isFVAULT2(cd->type)) { |
1399 | 0 | r = _crypt_load_fvault2(cd); |
1400 | 0 | if (r < 0) { |
1401 | 0 | log_dbg(cd, "FVAULT2 device header not available."); |
1402 | 0 | crypt_set_null_type(cd); |
1403 | 0 | r = 0; |
1404 | 0 | } |
1405 | 0 | } |
1406 | 0 | out: |
1407 | 0 | dm_targets_free(cd, &dmd); |
1408 | 0 | dm_targets_free(cd, &dmdi); |
1409 | 0 | dm_targets_free(cd, &dmdep); |
1410 | 0 | free(CONST_CAST(void*)dmd.uuid); |
1411 | 0 | free(cipher_spec); |
1412 | 0 | dep = deps; |
1413 | 0 | while (*dep) |
1414 | 0 | free(*dep++); |
1415 | 0 | return r; |
1416 | 0 | } |
1417 | | |
1418 | | static int _init_by_name_verity(struct crypt_device *cd, const char *name) |
1419 | 0 | { |
1420 | 0 | struct crypt_dm_active_device dmd; |
1421 | 0 | struct dm_target *tgt = &dmd.segment; |
1422 | 0 | int r; |
1423 | |
|
1424 | 0 | r = dm_query_device(cd, name, |
1425 | 0 | DM_ACTIVE_DEVICE | |
1426 | 0 | DM_ACTIVE_VERITY_HASH_DEVICE | |
1427 | 0 | DM_ACTIVE_VERITY_ROOT_HASH | |
1428 | 0 | DM_ACTIVE_VERITY_PARAMS, &dmd); |
1429 | 0 | if (r < 0) |
1430 | 0 | return r; |
1431 | 0 | if (!single_segment(&dmd) || tgt->type != DM_VERITY) { |
1432 | 0 | log_dbg(cd, "Unsupported device table detected in %s.", name); |
1433 | 0 | r = -EINVAL; |
1434 | 0 | goto out; |
1435 | 0 | } |
1436 | 0 | if (r > 0) |
1437 | 0 | r = 0; |
1438 | |
|
1439 | 0 | if (isVERITY(cd->type)) { |
1440 | 0 | cd->u.verity.uuid = NULL; // FIXME |
1441 | 0 | cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME |
1442 | 0 | cd->u.verity.hdr.data_size = tgt->u.verity.vp->data_size; |
1443 | 0 | cd->u.verity.root_hash_size = tgt->u.verity.root_hash_size; |
1444 | 0 | MOVE_REF(cd->u.verity.hdr.hash_name, tgt->u.verity.vp->hash_name); |
1445 | 0 | cd->u.verity.hdr.data_device = NULL; |
1446 | 0 | cd->u.verity.hdr.hash_device = NULL; |
1447 | 0 | cd->u.verity.hdr.data_block_size = tgt->u.verity.vp->data_block_size; |
1448 | 0 | cd->u.verity.hdr.hash_block_size = tgt->u.verity.vp->hash_block_size; |
1449 | 0 | cd->u.verity.hdr.hash_area_offset = tgt->u.verity.hash_offset; |
1450 | 0 | cd->u.verity.hdr.fec_area_offset = tgt->u.verity.fec_offset; |
1451 | 0 | cd->u.verity.hdr.hash_type = tgt->u.verity.vp->hash_type; |
1452 | 0 | cd->u.verity.hdr.flags = tgt->u.verity.vp->flags; |
1453 | 0 | cd->u.verity.hdr.salt_size = tgt->u.verity.vp->salt_size; |
1454 | 0 | MOVE_REF(cd->u.verity.hdr.salt, tgt->u.verity.vp->salt); |
1455 | 0 | MOVE_REF(cd->u.verity.hdr.fec_device, tgt->u.verity.vp->fec_device); |
1456 | 0 | cd->u.verity.hdr.fec_roots = tgt->u.verity.vp->fec_roots; |
1457 | 0 | MOVE_REF(cd->u.verity.fec_device, tgt->u.verity.fec_device); |
1458 | 0 | MOVE_REF(cd->metadata_device, tgt->u.verity.hash_device); |
1459 | 0 | MOVE_REF(cd->u.verity.root_hash, tgt->u.verity.root_hash); |
1460 | 0 | } |
1461 | 0 | out: |
1462 | 0 | dm_targets_free(cd, &dmd); |
1463 | 0 | return r; |
1464 | 0 | } |
1465 | | |
1466 | | static int _init_by_name_integrity(struct crypt_device *cd, const char *name) |
1467 | 0 | { |
1468 | 0 | struct crypt_dm_active_device dmd; |
1469 | 0 | struct dm_target *tgt = &dmd.segment; |
1470 | 0 | int r; |
1471 | |
|
1472 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | |
1473 | 0 | DM_ACTIVE_CRYPT_KEY | |
1474 | 0 | DM_ACTIVE_CRYPT_KEYSIZE | |
1475 | 0 | DM_ACTIVE_INTEGRITY_PARAMS, &dmd); |
1476 | 0 | if (r < 0) |
1477 | 0 | return r; |
1478 | 0 | if (!single_segment(&dmd) || tgt->type != DM_INTEGRITY) { |
1479 | 0 | log_dbg(cd, "Unsupported device table detected in %s.", name); |
1480 | 0 | r = -EINVAL; |
1481 | 0 | goto out; |
1482 | 0 | } |
1483 | 0 | if (r > 0) |
1484 | 0 | r = 0; |
1485 | |
|
1486 | 0 | if (isINTEGRITY(cd->type)) { |
1487 | 0 | cd->u.integrity.params.tag_size = tgt->u.integrity.tag_size; |
1488 | 0 | cd->u.integrity.params.sector_size = tgt->u.integrity.sector_size; |
1489 | 0 | cd->u.integrity.params.journal_size = tgt->u.integrity.journal_size; |
1490 | 0 | cd->u.integrity.params.journal_watermark = tgt->u.integrity.journal_watermark; |
1491 | 0 | cd->u.integrity.params.journal_commit_time = tgt->u.integrity.journal_commit_time; |
1492 | 0 | cd->u.integrity.params.interleave_sectors = tgt->u.integrity.interleave_sectors; |
1493 | 0 | cd->u.integrity.params.buffer_sectors = tgt->u.integrity.buffer_sectors; |
1494 | 0 | MOVE_REF(cd->u.integrity.params.integrity, tgt->u.integrity.integrity); |
1495 | 0 | MOVE_REF(cd->u.integrity.params.journal_integrity, tgt->u.integrity.journal_integrity); |
1496 | 0 | MOVE_REF(cd->u.integrity.params.journal_crypt, tgt->u.integrity.journal_crypt); |
1497 | |
|
1498 | 0 | if (tgt->u.integrity.vk) |
1499 | 0 | cd->u.integrity.params.integrity_key_size = crypt_volume_key_length(tgt->u.integrity.vk); |
1500 | 0 | if (tgt->u.integrity.journal_integrity_key) |
1501 | 0 | cd->u.integrity.params.journal_integrity_key_size = crypt_volume_key_length(tgt->u.integrity.journal_integrity_key); |
1502 | 0 | if (tgt->u.integrity.journal_crypt_key) |
1503 | 0 | cd->u.integrity.params.journal_crypt_key_size = crypt_volume_key_length(tgt->u.integrity.journal_crypt_key); |
1504 | 0 | MOVE_REF(cd->metadata_device, tgt->u.integrity.meta_device); |
1505 | 0 | } |
1506 | 0 | out: |
1507 | 0 | dm_targets_free(cd, &dmd); |
1508 | 0 | return r; |
1509 | 0 | } |
1510 | | |
1511 | | int crypt_init_by_name_and_header(struct crypt_device **cd, |
1512 | | const char *name, |
1513 | | const char *header_device) |
1514 | 0 | { |
1515 | 0 | crypt_status_info ci; |
1516 | 0 | struct crypt_dm_active_device dmd; |
1517 | 0 | struct dm_target *tgt = &dmd.segment; |
1518 | 0 | const char *type = NULL; |
1519 | 0 | int r; |
1520 | |
|
1521 | 0 | if (!cd || !name) |
1522 | 0 | return -EINVAL; |
1523 | | |
1524 | 0 | log_dbg(NULL, "Allocating crypt device context by device %s.", name); |
1525 | |
|
1526 | 0 | ci = crypt_status(NULL, name); |
1527 | 0 | if (ci == CRYPT_INVALID) |
1528 | 0 | return -ENODEV; |
1529 | | |
1530 | 0 | if (ci < CRYPT_ACTIVE) { |
1531 | 0 | log_err(NULL, _("Device %s is not active."), name); |
1532 | 0 | return -ENODEV; |
1533 | 0 | } |
1534 | | |
1535 | 0 | r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd); |
1536 | 0 | if (r < 0) |
1537 | 0 | return r; |
1538 | | |
1539 | 0 | *cd = NULL; |
1540 | |
|
1541 | 0 | if (header_device) { |
1542 | 0 | r = crypt_init(cd, header_device); |
1543 | 0 | } else { |
1544 | 0 | r = crypt_init(cd, device_path(tgt->data_device)); |
1545 | | |
1546 | | /* Underlying device disappeared but mapping still active */ |
1547 | 0 | if (!tgt->data_device || r == -ENOTBLK) |
1548 | 0 | log_verbose(NULL, _("Underlying device for crypt device %s disappeared."), |
1549 | 0 | name); |
1550 | | |
1551 | | /* Underlying device is not readable but crypt mapping exists */ |
1552 | 0 | if (r == -ENOTBLK) |
1553 | 0 | r = crypt_init(cd, NULL); |
1554 | 0 | } |
1555 | |
|
1556 | 0 | if (r < 0) |
1557 | 0 | goto out; |
1558 | | |
1559 | 0 | if (dmd.uuid) { |
1560 | 0 | if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1)) |
1561 | 0 | type = CRYPT_PLAIN; |
1562 | 0 | else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1)) |
1563 | 0 | type = CRYPT_LOOPAES; |
1564 | 0 | else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1)) |
1565 | 0 | type = CRYPT_LUKS1; |
1566 | 0 | else if (!strncmp(CRYPT_LUKS2, dmd.uuid, sizeof(CRYPT_LUKS2)-1)) |
1567 | 0 | type = CRYPT_LUKS2; |
1568 | 0 | else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1)) |
1569 | 0 | type = CRYPT_VERITY; |
1570 | 0 | else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1)) |
1571 | 0 | type = CRYPT_TCRYPT; |
1572 | 0 | else if (!strncmp(CRYPT_INTEGRITY, dmd.uuid, sizeof(CRYPT_INTEGRITY)-1)) |
1573 | 0 | type = CRYPT_INTEGRITY; |
1574 | 0 | else if (!strncmp(CRYPT_BITLK, dmd.uuid, sizeof(CRYPT_BITLK)-1)) |
1575 | 0 | type = CRYPT_BITLK; |
1576 | 0 | else if (!strncmp(CRYPT_FVAULT2, dmd.uuid, sizeof(CRYPT_FVAULT2)-1)) |
1577 | 0 | type = CRYPT_FVAULT2; |
1578 | 0 | else |
1579 | 0 | log_dbg(NULL, "Unknown UUID set, some parameters are not set."); |
1580 | 0 | } else |
1581 | 0 | log_dbg(NULL, "Active device has no UUID set, some parameters are not set."); |
1582 | |
|
1583 | 0 | if (type) { |
1584 | 0 | (*cd)->type = strdup(type); |
1585 | 0 | if (!(*cd)->type) { |
1586 | 0 | r = -ENOMEM; |
1587 | 0 | goto out; |
1588 | 0 | } |
1589 | 0 | } |
1590 | | |
1591 | 0 | if (header_device) { |
1592 | 0 | r = crypt_set_data_device(*cd, device_path(tgt->data_device)); |
1593 | 0 | if (r < 0) |
1594 | 0 | goto out; |
1595 | 0 | } |
1596 | | |
1597 | | /* Try to initialize basic parameters from active device */ |
1598 | | |
1599 | 0 | if (tgt->type == DM_CRYPT || tgt->type == DM_LINEAR) |
1600 | 0 | r = _init_by_name_crypt(*cd, name); |
1601 | 0 | else if (tgt->type == DM_VERITY) |
1602 | 0 | r = _init_by_name_verity(*cd, name); |
1603 | 0 | else if (tgt->type == DM_INTEGRITY) |
1604 | 0 | r = _init_by_name_integrity(*cd, name); |
1605 | 0 | out: |
1606 | 0 | if (r == 0 && !(*cd)->type) { |
1607 | | /* For anonymous device (no header found) remember initialized name */ |
1608 | 0 | (*cd)->u.none.active_name = strdup(name); |
1609 | 0 | if (!(*cd)->u.none.active_name) |
1610 | 0 | r = -ENOMEM; |
1611 | 0 | } |
1612 | |
|
1613 | 0 | if (r < 0) { |
1614 | 0 | crypt_free(*cd); |
1615 | 0 | *cd = NULL; |
1616 | 0 | } |
1617 | |
|
1618 | 0 | free(CONST_CAST(void*)dmd.uuid); |
1619 | 0 | dm_targets_free(NULL, &dmd); |
1620 | 0 | return r; |
1621 | 0 | } |
1622 | | |
1623 | | int crypt_init_by_name(struct crypt_device **cd, const char *name) |
1624 | 0 | { |
1625 | 0 | return crypt_init_by_name_and_header(cd, name, NULL); |
1626 | 0 | } |
1627 | | |
1628 | | /* |
1629 | | * crypt_format() helpers |
1630 | | */ |
1631 | | static int _crypt_format_plain(struct crypt_device *cd, |
1632 | | const char *cipher, |
1633 | | const char *cipher_mode, |
1634 | | const char *uuid, |
1635 | | size_t volume_key_size, |
1636 | | struct crypt_params_plain *params) |
1637 | 0 | { |
1638 | 0 | unsigned int sector_size = params ? params->sector_size : SECTOR_SIZE; |
1639 | 0 | uint64_t dev_size; |
1640 | |
|
1641 | 0 | if (!cipher || !cipher_mode) { |
1642 | 0 | log_err(cd, _("Invalid plain crypt parameters.")); |
1643 | 0 | return -EINVAL; |
1644 | 0 | } |
1645 | | |
1646 | 0 | if (volume_key_size > 1024) { |
1647 | 0 | log_err(cd, _("Invalid key size.")); |
1648 | 0 | return -EINVAL; |
1649 | 0 | } |
1650 | | |
1651 | 0 | if (uuid) { |
1652 | 0 | log_err(cd, _("UUID is not supported for this crypt type.")); |
1653 | 0 | return -EINVAL; |
1654 | 0 | } |
1655 | | |
1656 | 0 | if (cd->metadata_device) { |
1657 | 0 | log_err(cd, _("Detached metadata device is not supported for this crypt type.")); |
1658 | 0 | return -EINVAL; |
1659 | 0 | } |
1660 | | |
1661 | | /* For compatibility with old params structure */ |
1662 | 0 | if (!sector_size) |
1663 | 0 | sector_size = SECTOR_SIZE; |
1664 | |
|
1665 | 0 | if (sector_size < SECTOR_SIZE || sector_size > MAX_SECTOR_SIZE || |
1666 | 0 | NOTPOW2(sector_size)) { |
1667 | 0 | log_err(cd, _("Unsupported encryption sector size.")); |
1668 | 0 | return -EINVAL; |
1669 | 0 | } |
1670 | | |
1671 | 0 | if (sector_size > SECTOR_SIZE && !device_size(cd->device, &dev_size)) { |
1672 | 0 | if (params && params->offset) { |
1673 | 0 | if (params->offset > (UINT64_MAX / SECTOR_SIZE)) |
1674 | 0 | return -EINVAL; |
1675 | 0 | if (dev_size < (params->offset * SECTOR_SIZE)) |
1676 | 0 | return -EINVAL; |
1677 | 0 | dev_size -= (params->offset * SECTOR_SIZE); |
1678 | 0 | } |
1679 | 0 | if (dev_size % sector_size) { |
1680 | 0 | log_err(cd, _("Device size is not aligned to requested sector size.")); |
1681 | 0 | return -EINVAL; |
1682 | 0 | } |
1683 | 0 | device_set_block_size(crypt_data_device(cd), sector_size); |
1684 | 0 | } |
1685 | | |
1686 | 0 | if (!(cd->type = strdup(CRYPT_PLAIN))) |
1687 | 0 | return -ENOMEM; |
1688 | | |
1689 | 0 | cd->u.plain.key_size = volume_key_size; |
1690 | 0 | cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL); |
1691 | 0 | if (!cd->volume_key) |
1692 | 0 | return -ENOMEM; |
1693 | | |
1694 | 0 | if (asprintf(&cd->u.plain.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) { |
1695 | 0 | cd->u.plain.cipher_spec = NULL; |
1696 | 0 | return -ENOMEM; |
1697 | 0 | } |
1698 | 0 | cd->u.plain.cipher = strdup(cipher); |
1699 | 0 | if (!cd->u.plain.cipher) |
1700 | 0 | return -ENOMEM; |
1701 | | |
1702 | 0 | cd->u.plain.cipher_mode = cd->u.plain.cipher_spec + strlen(cipher) + 1; |
1703 | |
|
1704 | 0 | if (params && params->hash) { |
1705 | 0 | cd->u.plain.hdr.hash = strdup(params->hash); |
1706 | 0 | if (!cd->u.plain.hdr.hash) { |
1707 | 0 | free(cd->u.plain.cipher); |
1708 | 0 | cd->u.plain.cipher = NULL; |
1709 | 0 | return -ENOMEM; |
1710 | 0 | } |
1711 | 0 | } |
1712 | | |
1713 | 0 | cd->u.plain.hdr.offset = params ? params->offset : 0; |
1714 | 0 | cd->u.plain.hdr.skip = params ? params->skip : 0; |
1715 | 0 | cd->u.plain.hdr.size = params ? params->size : 0; |
1716 | 0 | cd->u.plain.hdr.sector_size = sector_size; |
1717 | | |
1718 | |
|
1719 | 0 | return 0; |
1720 | 0 | } |
1721 | | |
1722 | | static int _crypt_format_luks1(struct crypt_device *cd, |
1723 | | const char *cipher, |
1724 | | const char *cipher_mode, |
1725 | | const char *uuid, |
1726 | | const char *volume_key, |
1727 | | size_t volume_key_size, |
1728 | | struct crypt_params_luks1 *params) |
1729 | 0 | { |
1730 | 0 | int r; |
1731 | 0 | unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT; |
1732 | 0 | unsigned long alignment_offset = 0; |
1733 | 0 | uint64_t dev_size; |
1734 | |
|
1735 | 0 | if (!cipher || !cipher_mode) |
1736 | 0 | return -EINVAL; |
1737 | | |
1738 | 0 | if (!crypt_metadata_device(cd)) { |
1739 | 0 | log_err(cd, _("Can't format LUKS without device.")); |
1740 | 0 | return -EINVAL; |
1741 | 0 | } |
1742 | | |
1743 | 0 | if (device_is_zoned(crypt_metadata_device(cd)) > 0) { |
1744 | 0 | log_err(cd, _("Zoned device %s cannot be used for LUKS header."), |
1745 | 0 | device_path(crypt_metadata_device(cd))); |
1746 | 0 | return -EINVAL; |
1747 | 0 | } |
1748 | | |
1749 | 0 | if (params && cd->data_offset && params->data_alignment && |
1750 | 0 | (cd->data_offset % params->data_alignment)) { |
1751 | 0 | log_err(cd, _("Requested data alignment is not compatible with data offset.")); |
1752 | 0 | return -EINVAL; |
1753 | 0 | } |
1754 | | |
1755 | 0 | if (!(cd->type = strdup(CRYPT_LUKS1))) |
1756 | 0 | return -ENOMEM; |
1757 | | |
1758 | 0 | if (volume_key) |
1759 | 0 | cd->volume_key = crypt_alloc_volume_key(volume_key_size, |
1760 | 0 | volume_key); |
1761 | 0 | else |
1762 | 0 | cd->volume_key = crypt_generate_volume_key(cd, volume_key_size, KEY_QUALITY_KEY); |
1763 | |
|
1764 | 0 | if (!cd->volume_key) |
1765 | 0 | return -ENOMEM; |
1766 | | |
1767 | 0 | if (verify_pbkdf_params(cd, &cd->pbkdf)) { |
1768 | 0 | r = init_pbkdf_type(cd, NULL, CRYPT_LUKS1); |
1769 | 0 | if (r) |
1770 | 0 | return r; |
1771 | 0 | } |
1772 | | |
1773 | 0 | if (params && params->hash && strcmp(params->hash, cd->pbkdf.hash)) { |
1774 | 0 | free(CONST_CAST(void*)cd->pbkdf.hash); |
1775 | 0 | cd->pbkdf.hash = strdup(params->hash); |
1776 | 0 | if (!cd->pbkdf.hash) |
1777 | 0 | return -ENOMEM; |
1778 | 0 | } |
1779 | | |
1780 | 0 | if (params && params->data_device) { |
1781 | 0 | if (!cd->metadata_device) |
1782 | 0 | cd->metadata_device = cd->device; |
1783 | 0 | else |
1784 | 0 | device_free(cd, cd->device); |
1785 | 0 | cd->device = NULL; |
1786 | 0 | if (device_alloc(cd, &cd->device, params->data_device) < 0) |
1787 | 0 | return -ENOMEM; |
1788 | 0 | } |
1789 | | |
1790 | 0 | if (device_is_dax(crypt_data_device(cd)) > 0) |
1791 | 0 | log_std(cd, _("WARNING: DAX device can corrupt data as it does not guarantee atomic sector updates.\n")); |
1792 | |
|
1793 | 0 | if (params && cd->metadata_device) { |
1794 | | /* For detached header the alignment is used directly as data offset */ |
1795 | 0 | if (!cd->data_offset) |
1796 | 0 | cd->data_offset = params->data_alignment; |
1797 | 0 | required_alignment = params->data_alignment * SECTOR_SIZE; |
1798 | 0 | } else if (params && params->data_alignment) { |
1799 | 0 | required_alignment = params->data_alignment * SECTOR_SIZE; |
1800 | 0 | } else |
1801 | 0 | device_topology_alignment(cd, cd->device, |
1802 | 0 | &required_alignment, |
1803 | 0 | &alignment_offset, DEFAULT_DISK_ALIGNMENT); |
1804 | |
|
1805 | 0 | r = crypt_check_cipher(cd, volume_key_size, cipher, cipher_mode); |
1806 | 0 | if (r < 0) |
1807 | 0 | return r; |
1808 | | |
1809 | 0 | r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode, |
1810 | 0 | cd->pbkdf.hash, uuid, |
1811 | 0 | cd->data_offset * SECTOR_SIZE, |
1812 | 0 | alignment_offset, required_alignment, cd); |
1813 | 0 | if (r < 0) |
1814 | 0 | return r; |
1815 | | |
1816 | 0 | r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL); |
1817 | 0 | if (r < 0) |
1818 | 0 | return r; |
1819 | | |
1820 | | |
1821 | 0 | if (asprintf(&cd->u.luks1.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) { |
1822 | 0 | cd->u.luks1.cipher_spec = NULL; |
1823 | 0 | return -ENOMEM; |
1824 | 0 | } |
1825 | | |
1826 | 0 | r = LUKS_wipe_header_areas(&cd->u.luks1.hdr, cd); |
1827 | 0 | if (r < 0) { |
1828 | 0 | free(cd->u.luks1.cipher_spec); |
1829 | 0 | log_err(cd, _("Cannot wipe header on device %s."), |
1830 | 0 | mdata_device_path(cd)); |
1831 | 0 | return r; |
1832 | 0 | } |
1833 | | |
1834 | 0 | r = LUKS_write_phdr(&cd->u.luks1.hdr, cd); |
1835 | 0 | if (r) { |
1836 | 0 | free(cd->u.luks1.cipher_spec); |
1837 | 0 | return r; |
1838 | 0 | } |
1839 | | |
1840 | 0 | if (!device_size(crypt_data_device(cd), &dev_size) && |
1841 | 0 | dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE)) |
1842 | 0 | log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"), |
1843 | 0 | device_path(crypt_data_device(cd))); |
1844 | |
|
1845 | 0 | return 0; |
1846 | 0 | } |
1847 | | |
1848 | | static int LUKS2_check_encryption_params(struct crypt_device *cd, |
1849 | | const char *cipher, |
1850 | | const char *cipher_mode, |
1851 | | const char *integrity, |
1852 | | size_t required_integrity_key_size, |
1853 | | size_t volume_key_size, |
1854 | | const struct crypt_params_luks2 *params, |
1855 | | const char **ret_integrity, |
1856 | | size_t *ret_integrity_key_size) |
1857 | 0 | { |
1858 | 0 | int r, integrity_key_size = 0; |
1859 | |
|
1860 | 0 | assert(cipher); |
1861 | 0 | assert(cipher_mode); |
1862 | 0 | assert(ret_integrity); |
1863 | |
|
1864 | 0 | if (integrity) { |
1865 | 0 | if (params->integrity_params) { |
1866 | | /* Standalone dm-integrity must not be used */ |
1867 | 0 | if (params->integrity_params->integrity) |
1868 | 0 | return -EINVAL; |
1869 | | /* FIXME: journal encryption and MAC is here not yet supported */ |
1870 | 0 | if (params->integrity_params->journal_crypt || |
1871 | 0 | params->integrity_params->journal_integrity) |
1872 | 0 | return -ENOTSUP; |
1873 | 0 | } |
1874 | 0 | if (!INTEGRITY_tag_size(integrity, cipher, cipher_mode)) { |
1875 | | /* merge "none" string into NULL to make branching logic is easier */ |
1876 | 0 | if (!strcmp(integrity, "none")) |
1877 | 0 | integrity = NULL; |
1878 | 0 | else |
1879 | 0 | return -EINVAL; |
1880 | 0 | } |
1881 | 0 | integrity_key_size = INTEGRITY_key_size(integrity, required_integrity_key_size); |
1882 | 0 | if ((integrity_key_size < 0) || (integrity_key_size >= (int)volume_key_size)) { |
1883 | 0 | log_err(cd, _("Volume key is too small for encryption with integrity extensions.")); |
1884 | 0 | return -EINVAL; |
1885 | 0 | } |
1886 | 0 | if (integrity_key_size && integrity_key_size < LUKS2_MIN_INTEGRITY_KEY_BYTES) { |
1887 | 0 | log_err(cd, _("Integrity key size is too small.")); |
1888 | 0 | return -EINVAL; |
1889 | 0 | } |
1890 | 0 | } |
1891 | | |
1892 | | /* FIXME: allow this later also for normal ciphers (check AF_ALG availability. */ |
1893 | 0 | if (integrity && integrity_key_size == 0) { |
1894 | 0 | r = crypt_cipher_check_kernel(cipher, cipher_mode, integrity, volume_key_size); |
1895 | 0 | if (r < 0 && r != -ENOTSUP) { |
1896 | 0 | log_err(cd, _("Cipher %s-%s (key size %zd bits) is not available."), |
1897 | 0 | cipher, cipher_mode, volume_key_size * 8); |
1898 | 0 | return r; |
1899 | 0 | } |
1900 | 0 | } |
1901 | | |
1902 | 0 | if ((!integrity || integrity_key_size) && !crypt_cipher_wrapped_key(cipher, cipher_mode) && |
1903 | 0 | !INTEGRITY_tag_size(NULL, cipher, cipher_mode)) { |
1904 | 0 | r = crypt_check_cipher(cd, volume_key_size - integrity_key_size, |
1905 | 0 | cipher, cipher_mode); |
1906 | 0 | if (r < 0) |
1907 | 0 | return r; |
1908 | 0 | } |
1909 | | |
1910 | 0 | *ret_integrity = integrity; |
1911 | 0 | if (ret_integrity_key_size) |
1912 | 0 | *ret_integrity_key_size = required_integrity_key_size ? integrity_key_size : 0; |
1913 | |
|
1914 | 0 | return 0; |
1915 | 0 | } |
1916 | | |
1917 | | static int LUKS2_check_encryption_sector(struct crypt_device *cd, uint64_t device_size_bytes, |
1918 | | uint64_t data_offset_bytes, uint32_t sector_size, bool modify_sector_size, |
1919 | | bool verify_data_area_alignment, uint32_t *ret_sector_size) |
1920 | 0 | { |
1921 | 0 | uint64_t dmc_flags; |
1922 | |
|
1923 | 0 | assert(ret_sector_size); |
1924 | |
|
1925 | 0 | if (sector_size < SECTOR_SIZE || sector_size > MAX_SECTOR_SIZE || |
1926 | 0 | NOTPOW2(sector_size)) { |
1927 | 0 | log_err(cd, _("Unsupported encryption sector size.")); |
1928 | 0 | return -EINVAL; |
1929 | 0 | } |
1930 | | |
1931 | 0 | if (sector_size != SECTOR_SIZE && !dm_flags(cd, DM_CRYPT, &dmc_flags) && |
1932 | 0 | !(dmc_flags & DM_SECTOR_SIZE_SUPPORTED)) { |
1933 | 0 | if (modify_sector_size) { |
1934 | 0 | log_dbg(cd, "dm-crypt does not support encryption sector size option. Reverting to 512 bytes."); |
1935 | 0 | sector_size = SECTOR_SIZE; |
1936 | 0 | } else |
1937 | 0 | log_std(cd, _("WARNING: The device activation will fail, dm-crypt is missing " |
1938 | 0 | "support for requested encryption sector size.\n")); |
1939 | 0 | } |
1940 | |
|
1941 | 0 | if (modify_sector_size) { |
1942 | 0 | if (data_offset_bytes && MISALIGNED(data_offset_bytes, sector_size)) { |
1943 | 0 | log_dbg(cd, "Data offset not aligned to sector size. Reverting to 512 bytes."); |
1944 | 0 | sector_size = SECTOR_SIZE; |
1945 | 0 | } else if (MISALIGNED(device_size_bytes - data_offset_bytes, sector_size)) { |
1946 | | /* underflow does not affect misalignment checks */ |
1947 | 0 | log_dbg(cd, "Device size is not aligned to sector size. Reverting to 512 bytes."); |
1948 | 0 | sector_size = SECTOR_SIZE; |
1949 | 0 | } |
1950 | 0 | } |
1951 | | |
1952 | | /* underflow does not affect misalignment checks */ |
1953 | 0 | if (verify_data_area_alignment && |
1954 | 0 | sector_size > SECTOR_SIZE && |
1955 | 0 | MISALIGNED(device_size_bytes - data_offset_bytes, sector_size)) { |
1956 | 0 | log_err(cd, _("Device size is not aligned to requested sector size.")); |
1957 | 0 | return -EINVAL; |
1958 | 0 | } |
1959 | | |
1960 | 0 | *ret_sector_size = sector_size; |
1961 | |
|
1962 | 0 | return 0; |
1963 | 0 | } |
1964 | | |
1965 | | static int _crypt_format_luks2(struct crypt_device *cd, |
1966 | | const char *cipher, |
1967 | | const char *cipher_mode, |
1968 | | const char *uuid, |
1969 | | const char *volume_key, |
1970 | | size_t volume_key_size, |
1971 | | struct crypt_params_luks2 *params, |
1972 | | bool sector_size_autodetect, bool integrity_inline) |
1973 | 0 | { |
1974 | 0 | int r; |
1975 | 0 | unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT; |
1976 | 0 | unsigned long alignment_offset = 0; |
1977 | 0 | unsigned int sector_size; |
1978 | 0 | char cipher_spec[2*MAX_CAPI_ONE_LEN]; |
1979 | 0 | const char *integrity = params ? params->integrity : NULL; |
1980 | 0 | size_t integrity_key_size = 0; /* only for independent, separate key in HMAC */ |
1981 | 0 | struct volume_key *integrity_key = NULL; |
1982 | 0 | uint64_t data_offset_bytes, dev_size, metadata_size_bytes, keyslots_size_bytes; |
1983 | |
|
1984 | 0 | cd->u.luks2.hdr.jobj = NULL; |
1985 | 0 | cd->u.luks2.keyslot_cipher = NULL; |
1986 | |
|
1987 | 0 | if (!cipher || !cipher_mode) |
1988 | 0 | return -EINVAL; |
1989 | | |
1990 | 0 | if (!crypt_metadata_device(cd)) { |
1991 | 0 | log_err(cd, _("Can't format LUKS without device.")); |
1992 | 0 | return -EINVAL; |
1993 | 0 | } |
1994 | | |
1995 | 0 | if (device_is_zoned(crypt_metadata_device(cd)) > 0) { |
1996 | 0 | log_err(cd, _("Zoned device %s cannot be used for LUKS header."), |
1997 | 0 | device_path(crypt_metadata_device(cd))); |
1998 | 0 | return -EINVAL; |
1999 | 0 | } |
2000 | | |
2001 | 0 | if (params && cd->data_offset && params->data_alignment && |
2002 | 0 | (cd->data_offset % params->data_alignment)) { |
2003 | 0 | log_err(cd, _("Requested data alignment is not compatible with data offset.")); |
2004 | 0 | return -EINVAL; |
2005 | 0 | } |
2006 | | |
2007 | 0 | if (params && params->sector_size) |
2008 | 0 | sector_size_autodetect = false; |
2009 | |
|
2010 | 0 | if (params && params->data_device) { |
2011 | 0 | if (!cd->metadata_device) |
2012 | 0 | cd->metadata_device = cd->device; |
2013 | 0 | else |
2014 | 0 | device_free(cd, cd->device); |
2015 | 0 | cd->device = NULL; |
2016 | 0 | if (device_alloc(cd, &cd->device, params->data_device) < 0) |
2017 | 0 | return -ENOMEM; |
2018 | 0 | } |
2019 | | |
2020 | 0 | if (device_is_dax(crypt_data_device(cd)) > 0) |
2021 | 0 | log_std(cd, _("WARNING: DAX device can corrupt data as it does not guarantee atomic sector updates.\n")); |
2022 | |
|
2023 | 0 | if (sector_size_autodetect) { |
2024 | 0 | sector_size = device_optimal_encryption_sector_size(cd, crypt_data_device(cd)); |
2025 | 0 | log_dbg(cd, "Auto-detected optimal encryption sector size for device %s is %d bytes.", |
2026 | 0 | device_path(crypt_data_device(cd)), sector_size); |
2027 | 0 | } else |
2028 | 0 | sector_size = params ? params->sector_size : SECTOR_SIZE; |
2029 | |
|
2030 | 0 | r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL); |
2031 | 0 | if (r < 0) |
2032 | 0 | return r; |
2033 | | |
2034 | 0 | if (!(cd->type = strdup(CRYPT_LUKS2))) |
2035 | 0 | return -ENOMEM; |
2036 | | |
2037 | 0 | if (volume_key) |
2038 | 0 | cd->volume_key = crypt_alloc_volume_key(volume_key_size, |
2039 | 0 | volume_key); |
2040 | 0 | else |
2041 | 0 | cd->volume_key = crypt_generate_volume_key(cd, volume_key_size, KEY_QUALITY_KEY); |
2042 | |
|
2043 | 0 | if (!cd->volume_key) |
2044 | 0 | return -ENOMEM; |
2045 | | |
2046 | 0 | if (params && params->pbkdf) |
2047 | 0 | r = crypt_set_pbkdf_type(cd, params->pbkdf); |
2048 | 0 | else if (verify_pbkdf_params(cd, &cd->pbkdf)) |
2049 | 0 | r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2); |
2050 | |
|
2051 | 0 | if (r < 0) |
2052 | 0 | return r; |
2053 | | |
2054 | 0 | if (params && cd->metadata_device) { |
2055 | | /* For detached header the alignment is used directly as data offset */ |
2056 | 0 | if (!cd->data_offset) |
2057 | 0 | cd->data_offset = params->data_alignment; |
2058 | 0 | required_alignment = params->data_alignment * SECTOR_SIZE; |
2059 | 0 | } else if (params && params->data_alignment) { |
2060 | 0 | required_alignment = params->data_alignment * SECTOR_SIZE; |
2061 | 0 | } else |
2062 | 0 | device_topology_alignment(cd, cd->device, |
2063 | 0 | &required_alignment, |
2064 | 0 | &alignment_offset, DEFAULT_DISK_ALIGNMENT); |
2065 | |
|
2066 | 0 | if (params && params->integrity_params && params->integrity_params->integrity_key_size) |
2067 | 0 | integrity_key_size = params->integrity_params->integrity_key_size; |
2068 | |
|
2069 | 0 | r = LUKS2_check_encryption_params(cd, cipher, cipher_mode, integrity, integrity_key_size, |
2070 | 0 | volume_key_size, params, &integrity, &integrity_key_size); |
2071 | 0 | if (r < 0) |
2072 | 0 | goto out; |
2073 | | |
2074 | 0 | r = device_size(crypt_data_device(cd), &dev_size); |
2075 | 0 | if (r < 0) |
2076 | 0 | goto out; |
2077 | | |
2078 | 0 | r = LUKS2_hdr_get_storage_params(cd, alignment_offset, required_alignment, |
2079 | 0 | &metadata_size_bytes, &keyslots_size_bytes, &data_offset_bytes); |
2080 | 0 | if (r < 0) |
2081 | 0 | goto out; |
2082 | | |
2083 | 0 | r = LUKS2_check_encryption_sector(cd, dev_size, data_offset_bytes, sector_size, |
2084 | 0 | sector_size_autodetect, integrity == NULL, |
2085 | 0 | §or_size); |
2086 | 0 | if (r < 0) |
2087 | 0 | goto out; |
2088 | | |
2089 | 0 | if (*cipher_mode != '\0') |
2090 | 0 | r = snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode); |
2091 | 0 | else |
2092 | 0 | r = snprintf(cipher_spec, sizeof(cipher_spec), "%s", cipher); |
2093 | 0 | if (r < 0 || (size_t)r >= sizeof(cipher_spec)) { |
2094 | 0 | r = -EINVAL; |
2095 | 0 | goto out; |
2096 | 0 | } |
2097 | | |
2098 | 0 | r = LUKS2_generate_hdr(cd, &cd->u.luks2.hdr, cd->volume_key, |
2099 | 0 | cipher_spec, |
2100 | 0 | integrity, integrity_key_size, |
2101 | 0 | uuid, |
2102 | 0 | sector_size, |
2103 | 0 | data_offset_bytes, |
2104 | 0 | metadata_size_bytes, keyslots_size_bytes, |
2105 | 0 | 0, 0, 0); |
2106 | 0 | if (r < 0) |
2107 | 0 | goto out; |
2108 | | |
2109 | 0 | if (integrity_inline) { |
2110 | 0 | log_dbg(cd, "Adding LUKS2 inline HW tags requirement flag."); |
2111 | 0 | r = LUKS2_config_set_requirement_version(cd, &cd->u.luks2.hdr, |
2112 | 0 | CRYPT_REQUIREMENT_INLINE_HW_TAGS, 1, false); |
2113 | 0 | if (r < 0) |
2114 | 0 | goto out; |
2115 | 0 | } |
2116 | | |
2117 | 0 | if (params && (params->label || params->subsystem)) { |
2118 | 0 | r = LUKS2_hdr_labels(cd, &cd->u.luks2.hdr, |
2119 | 0 | params->label, params->subsystem, 0); |
2120 | 0 | if (r < 0) |
2121 | 0 | goto out; |
2122 | 0 | } |
2123 | | |
2124 | 0 | device_set_block_size(crypt_data_device(cd), sector_size); |
2125 | |
|
2126 | 0 | r = LUKS2_wipe_header_areas(cd, &cd->u.luks2.hdr); |
2127 | 0 | if (r < 0) { |
2128 | 0 | log_err(cd, _("Cannot wipe header on device %s."), |
2129 | 0 | mdata_device_path(cd)); |
2130 | 0 | if (dev_size < LUKS2_hdr_and_areas_size(&cd->u.luks2.hdr)) |
2131 | 0 | log_err(cd, _("Device %s is too small."), device_path(crypt_metadata_device(cd))); |
2132 | 0 | goto out; |
2133 | 0 | } |
2134 | | |
2135 | | /* Wipe integrity superblock and create integrity superblock */ |
2136 | 0 | if (crypt_get_integrity_tag_size(cd)) { |
2137 | 0 | r = crypt_wipe_device(cd, crypt_data_device(cd), CRYPT_WIPE_ZERO, |
2138 | 0 | crypt_get_data_offset(cd) * SECTOR_SIZE, |
2139 | 0 | 8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL); |
2140 | 0 | if (r < 0) { |
2141 | 0 | if (r == -EBUSY) |
2142 | 0 | log_err(cd, _("Cannot format device %s in use."), |
2143 | 0 | data_device_path(cd)); |
2144 | 0 | else if (r == -EACCES) { |
2145 | 0 | log_err(cd, _("Cannot format device %s, permission denied."), |
2146 | 0 | data_device_path(cd)); |
2147 | 0 | r = -EINVAL; |
2148 | 0 | } else |
2149 | 0 | log_err(cd, _("Cannot wipe header on device %s."), |
2150 | 0 | data_device_path(cd)); |
2151 | |
|
2152 | 0 | goto out; |
2153 | 0 | } |
2154 | 0 | } |
2155 | | |
2156 | | /* Format underlying virtual dm-integrity device */ |
2157 | 0 | if (!integrity_inline && crypt_get_integrity_tag_size(cd)) { |
2158 | 0 | if (integrity_key_size) { |
2159 | 0 | integrity_key = crypt_alloc_volume_key(integrity_key_size, |
2160 | 0 | crypt_volume_key_get_key(cd->volume_key) + volume_key_size - integrity_key_size); |
2161 | 0 | if (!integrity_key) { |
2162 | 0 | r = -ENOMEM; |
2163 | 0 | goto out; |
2164 | 0 | } |
2165 | 0 | } |
2166 | 0 | r = INTEGRITY_format(cd, params ? params->integrity_params : NULL, |
2167 | 0 | integrity_key, NULL, NULL, 0, NULL, false); |
2168 | 0 | if (r) |
2169 | 0 | log_err(cd, _("Cannot format integrity for device %s."), |
2170 | 0 | data_device_path(cd)); |
2171 | 0 | crypt_free_volume_key(integrity_key); |
2172 | 0 | } |
2173 | | |
2174 | 0 | if (r < 0) |
2175 | 0 | goto out; |
2176 | | |
2177 | | /* override sequence id check with format */ |
2178 | 0 | r = LUKS2_hdr_write_force(cd, &cd->u.luks2.hdr); |
2179 | 0 | if (r < 0) { |
2180 | 0 | if (r == -EBUSY) |
2181 | 0 | log_err(cd, _("Cannot format device %s in use."), |
2182 | 0 | mdata_device_path(cd)); |
2183 | 0 | else if (r == -EACCES) { |
2184 | 0 | log_err(cd, _("Cannot format device %s, permission denied."), |
2185 | 0 | mdata_device_path(cd)); |
2186 | 0 | r = -EINVAL; |
2187 | 0 | } else |
2188 | 0 | log_err(cd, _("Cannot format device %s."), |
2189 | 0 | mdata_device_path(cd)); |
2190 | 0 | } |
2191 | |
|
2192 | 0 | out: |
2193 | 0 | if (r) { |
2194 | 0 | LUKS2_hdr_free(cd, &cd->u.luks2.hdr); |
2195 | 0 | return r; |
2196 | 0 | } |
2197 | | |
2198 | | /* Device size can be larger now if it is a file container */ |
2199 | 0 | if (!device_size(crypt_data_device(cd), &dev_size) && |
2200 | 0 | dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE)) |
2201 | 0 | log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"), |
2202 | 0 | device_path(crypt_data_device(cd))); |
2203 | |
|
2204 | 0 | return 0; |
2205 | 0 | } |
2206 | | |
2207 | | static int opal_topology_alignment(struct crypt_device *cd, |
2208 | | uint64_t partition_offset_sectors, |
2209 | | uint64_t data_offset_sectors, |
2210 | | uint64_t required_alignment_sectors, |
2211 | | uint64_t default_alignment_bytes, |
2212 | | uint64_t *ret_alignment_offset_bytes, |
2213 | | uint64_t *ret_alignment_bytes, |
2214 | | uint32_t *ret_opal_block_bytes, |
2215 | | uint64_t *ret_opal_alignment_granularity_blocks) |
2216 | 0 | { |
2217 | 0 | bool opal_align; |
2218 | 0 | int r; |
2219 | 0 | uint32_t opal_block_bytes, device_block_bytes; |
2220 | 0 | uint64_t opal_alignment_granularity_blocks, opal_lowest_lba_blocks; |
2221 | |
|
2222 | 0 | assert(cd); |
2223 | 0 | assert(ret_alignment_offset_bytes); |
2224 | 0 | assert(ret_alignment_bytes); |
2225 | 0 | assert(ret_opal_block_bytes); |
2226 | 0 | assert(ret_opal_alignment_granularity_blocks); |
2227 | |
|
2228 | 0 | r = opal_geometry(cd, crypt_data_device(cd), &opal_align, &opal_block_bytes, |
2229 | 0 | &opal_alignment_granularity_blocks, &opal_lowest_lba_blocks); |
2230 | 0 | if (r) { |
2231 | 0 | log_err(cd, _("Cannot get OPAL alignment parameters.")); |
2232 | 0 | return -EINVAL; |
2233 | 0 | } |
2234 | | |
2235 | 0 | device_block_bytes = device_block_size(cd, crypt_data_device(cd)); |
2236 | |
|
2237 | 0 | log_dbg(cd, "OPAL geometry: alignment: '%c', logical block size: %" PRIu32 "/%" PRIu32 |
2238 | 0 | ", alignment granularity: %" PRIu64 ", lowest aligned LBA: %" PRIu64, |
2239 | 0 | opal_align ? 'y' : 'n', opal_block_bytes, device_block_bytes, |
2240 | 0 | opal_alignment_granularity_blocks, opal_lowest_lba_blocks); |
2241 | |
|
2242 | 0 | if (opal_block_bytes < SECTOR_SIZE || NOTPOW2(opal_block_bytes)) { |
2243 | 0 | log_err(cd, _("Bogus OPAL logical block size.")); |
2244 | 0 | return -EINVAL; |
2245 | 0 | } |
2246 | | |
2247 | 0 | if (device_block_bytes != opal_block_bytes) { |
2248 | 0 | log_err(cd, _("Bogus OPAL logical block size differs from device block size.")); |
2249 | 0 | return -EINVAL; |
2250 | 0 | } |
2251 | | |
2252 | 0 | if (data_offset_sectors && |
2253 | 0 | MISALIGNED(data_offset_sectors + partition_offset_sectors, opal_block_bytes / SECTOR_SIZE)) { |
2254 | 0 | log_err(cd, _("Requested data offset is not compatible with OPAL block size.")); |
2255 | 0 | return -EINVAL; |
2256 | 0 | } |
2257 | | |
2258 | | /* Data offset has priority over data alignment parameter */ |
2259 | 0 | if (!data_offset_sectors && |
2260 | 0 | MISALIGNED(required_alignment_sectors, opal_block_bytes / SECTOR_SIZE)) { |
2261 | 0 | log_err(cd, _("Requested data alignment is not compatible with OPAL alignment.")); |
2262 | 0 | return -EINVAL; |
2263 | 0 | } |
2264 | | |
2265 | 0 | if (!opal_align) { |
2266 | | /* For detached header the alignment is used directly as data offset */ |
2267 | 0 | if (required_alignment_sectors || cd->metadata_device) |
2268 | 0 | *ret_alignment_bytes = required_alignment_sectors * SECTOR_SIZE; |
2269 | 0 | else |
2270 | 0 | *ret_alignment_bytes = default_alignment_bytes; |
2271 | 0 | *ret_alignment_offset_bytes = 0; |
2272 | 0 | *ret_opal_block_bytes = opal_block_bytes; |
2273 | 0 | *ret_opal_alignment_granularity_blocks = 1; |
2274 | 0 | return 0; |
2275 | 0 | } |
2276 | | |
2277 | 0 | if (data_offset_sectors) { |
2278 | 0 | if (MISALIGNED((((data_offset_sectors + partition_offset_sectors) * SECTOR_SIZE) / opal_block_bytes) - opal_lowest_lba_blocks, |
2279 | 0 | opal_alignment_granularity_blocks)) { |
2280 | | // FIXME: Add hint to user on how to fix it |
2281 | 0 | log_err(cd, _("Data offset does not satisfy OPAL alignment requirements.")); |
2282 | 0 | return -EINVAL; |
2283 | 0 | } |
2284 | | |
2285 | 0 | *ret_alignment_offset_bytes = 0; |
2286 | 0 | *ret_alignment_bytes = 0; |
2287 | 0 | *ret_opal_block_bytes = opal_block_bytes; |
2288 | 0 | *ret_opal_alignment_granularity_blocks = opal_alignment_granularity_blocks; |
2289 | |
|
2290 | 0 | return 0; |
2291 | 0 | } |
2292 | | |
2293 | 0 | if (MISALIGNED(required_alignment_sectors * SECTOR_SIZE, opal_block_bytes * opal_alignment_granularity_blocks)) { |
2294 | 0 | log_err(cd, _("Requested data alignment does not satisfy locking range alignment requirements.")); |
2295 | 0 | return -EINVAL; |
2296 | 0 | } |
2297 | | |
2298 | | /* For detached header the alignment is used directly as data offset */ |
2299 | 0 | if (required_alignment_sectors || cd->metadata_device) |
2300 | 0 | *ret_alignment_bytes = required_alignment_sectors * SECTOR_SIZE; |
2301 | 0 | else |
2302 | 0 | *ret_alignment_bytes = size_round_up(default_alignment_bytes, opal_block_bytes * opal_alignment_granularity_blocks); |
2303 | | |
2304 | | /* data offset is not set, calculate proper alignment */ |
2305 | 0 | *ret_alignment_offset_bytes = (partition_offset_sectors * SECTOR_SIZE) % (opal_block_bytes * opal_alignment_granularity_blocks); |
2306 | 0 | if (*ret_alignment_offset_bytes) |
2307 | 0 | *ret_alignment_offset_bytes = opal_block_bytes * opal_alignment_granularity_blocks - *ret_alignment_offset_bytes; |
2308 | |
|
2309 | 0 | if (*ret_alignment_offset_bytes) |
2310 | 0 | log_dbg(cd, "Compensating misaligned partition offset by %" PRIu64 "bytes.", |
2311 | 0 | *ret_alignment_offset_bytes); |
2312 | |
|
2313 | 0 | *ret_alignment_offset_bytes += (opal_lowest_lba_blocks * opal_block_bytes); |
2314 | 0 | *ret_opal_block_bytes = opal_block_bytes; |
2315 | 0 | *ret_opal_alignment_granularity_blocks = opal_alignment_granularity_blocks; |
2316 | |
|
2317 | 0 | log_dbg(cd, "OPAL alignment (%" PRIu32 "/%" PRIu64 "), offset = %" PRIu64 ". Required alignment is %" PRIu64 ".", |
2318 | 0 | opal_block_bytes, opal_alignment_granularity_blocks, *ret_alignment_offset_bytes, *ret_alignment_bytes); |
2319 | |
|
2320 | 0 | return 0; |
2321 | 0 | } |
2322 | | |
2323 | | int crypt_format_luks2_opal(struct crypt_device *cd, |
2324 | | const char *cipher, |
2325 | | const char *cipher_mode, |
2326 | | const char *uuid, |
2327 | | const char *volume_keys, |
2328 | | size_t volume_keys_size, |
2329 | | struct crypt_params_luks2 *params, |
2330 | | struct crypt_params_hw_opal *opal_params) |
2331 | 0 | { |
2332 | 0 | bool opal_range_reset = false, subsystem_overridden = false, sector_size_autodetect = cipher != NULL; |
2333 | 0 | int r; |
2334 | 0 | char cipher_spec[128]; |
2335 | 0 | const char *integrity = params ? params->integrity : NULL; |
2336 | 0 | size_t integrity_key_size = 0; /* only for independent, separate key in HMAC */ |
2337 | 0 | struct volume_key *integrity_key = NULL; |
2338 | 0 | uint8_t opal_requirement_version; |
2339 | 0 | uint32_t sector_size, opal_block_bytes, opal_segment_number = 1; /* We'll use the partition number if available later */ |
2340 | 0 | uint64_t alignment_offset_bytes, data_offset_bytes, device_size_bytes, opal_alignment_granularity_blocks, |
2341 | 0 | partition_offset_sectors, range_offset_blocks, range_size_bytes, |
2342 | 0 | required_alignment_bytes, metadata_size_bytes, keyslots_size_bytes, |
2343 | 0 | provided_data_sectors; |
2344 | 0 | struct volume_key *user_key = NULL; |
2345 | 0 | struct crypt_lock_handle *opal_lh = NULL; |
2346 | |
|
2347 | 0 | if (!cd || !params || !opal_params || |
2348 | 0 | !opal_params->admin_key || !opal_params->admin_key_size || !opal_params->user_key_size) |
2349 | 0 | return -EINVAL; |
2350 | | |
2351 | 0 | if (cd->type) { |
2352 | 0 | log_dbg(cd, "Context already formatted as %s.", cd->type); |
2353 | 0 | return -EINVAL; |
2354 | 0 | } |
2355 | | |
2356 | 0 | log_dbg(cd, "Formatting device %s as type LUKS2 with OPAL HW encryption.", mdata_device_path(cd) ?: "(none)"); |
2357 | |
|
2358 | 0 | r = init_crypto(cd); |
2359 | 0 | if (r < 0) |
2360 | 0 | return r; |
2361 | | |
2362 | 0 | if (volume_keys_size < opal_params->user_key_size) |
2363 | 0 | return -EINVAL; |
2364 | | |
2365 | 0 | if (cipher && (volume_keys_size == opal_params->user_key_size)) |
2366 | 0 | return -EINVAL; |
2367 | | |
2368 | 0 | if (!crypt_metadata_device(cd)) { |
2369 | 0 | log_err(cd, _("Can't format LUKS without device.")); |
2370 | 0 | return -EINVAL; |
2371 | 0 | } |
2372 | | |
2373 | 0 | if (params->data_alignment && |
2374 | 0 | MISALIGNED(cd->data_offset, params->data_alignment)) { |
2375 | 0 | log_err(cd, _("Requested data alignment is not compatible with data offset.")); |
2376 | 0 | return -EINVAL; |
2377 | 0 | } |
2378 | | |
2379 | 0 | if (params->data_device) { |
2380 | 0 | if (!cd->metadata_device) |
2381 | 0 | cd->metadata_device = cd->device; |
2382 | 0 | else |
2383 | 0 | device_free(cd, cd->device); |
2384 | 0 | cd->device = NULL; |
2385 | 0 | if (device_alloc(cd, &cd->device, params->data_device) < 0) |
2386 | 0 | return -ENOMEM; |
2387 | 0 | } |
2388 | | |
2389 | 0 | r = crypt_opal_supported(cd, crypt_data_device(cd)); |
2390 | 0 | if (r < 0) |
2391 | 0 | return r; |
2392 | | |
2393 | 0 | if (params->sector_size) |
2394 | 0 | sector_size_autodetect = false; |
2395 | |
|
2396 | 0 | partition_offset_sectors = crypt_dev_partition_offset(device_path(crypt_data_device(cd))); |
2397 | |
|
2398 | 0 | r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL); |
2399 | 0 | if (r < 0) |
2400 | 0 | return r; |
2401 | | |
2402 | | /* |
2403 | | * Check both data and metadata devices for exclusive access since |
2404 | | * we don't want to setup locking range on already used partition. |
2405 | | */ |
2406 | 0 | if (crypt_metadata_device(cd) != crypt_data_device(cd)) { |
2407 | 0 | r = device_check_access(cd, crypt_data_device(cd), DEV_EXCL); |
2408 | 0 | if (r < 0) |
2409 | 0 | return r; |
2410 | 0 | } |
2411 | | |
2412 | 0 | if (!(cd->type = strdup(CRYPT_LUKS2))) |
2413 | 0 | return -ENOMEM; |
2414 | | |
2415 | 0 | if (volume_keys) |
2416 | 0 | cd->volume_key = crypt_alloc_volume_key(volume_keys_size, volume_keys); |
2417 | 0 | else |
2418 | 0 | cd->volume_key = crypt_generate_volume_key(cd, volume_keys_size, KEY_QUALITY_KEY); |
2419 | |
|
2420 | 0 | if (!cd->volume_key) { |
2421 | 0 | r = -ENOMEM; |
2422 | 0 | goto out; |
2423 | 0 | } |
2424 | | |
2425 | 0 | if (cipher) { |
2426 | 0 | user_key = crypt_alloc_volume_key(opal_params->user_key_size, crypt_volume_key_get_key(cd->volume_key)); |
2427 | 0 | if (!user_key) { |
2428 | 0 | r = -ENOMEM; |
2429 | 0 | goto out; |
2430 | 0 | } |
2431 | 0 | } |
2432 | | |
2433 | 0 | r = 0; |
2434 | 0 | if (params->pbkdf) |
2435 | 0 | r = crypt_set_pbkdf_type(cd, params->pbkdf); |
2436 | 0 | else if (verify_pbkdf_params(cd, &cd->pbkdf)) |
2437 | 0 | r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2); |
2438 | |
|
2439 | 0 | if (r < 0) |
2440 | 0 | goto out; |
2441 | | |
2442 | 0 | if (cd->metadata_device && !cd->data_offset) |
2443 | | /* For detached header the alignment is used directly as data offset */ |
2444 | 0 | cd->data_offset = params->data_alignment; |
2445 | |
|
2446 | 0 | r = opal_topology_alignment(cd, partition_offset_sectors, |
2447 | 0 | cd->data_offset, params->data_alignment, |
2448 | 0 | DEFAULT_DISK_ALIGNMENT, &alignment_offset_bytes, &required_alignment_bytes, |
2449 | 0 | &opal_block_bytes, &opal_alignment_granularity_blocks); |
2450 | 0 | if (r < 0) |
2451 | 0 | goto out; |
2452 | | |
2453 | 0 | if (sector_size_autodetect) { |
2454 | 0 | sector_size = device_optimal_encryption_sector_size(cd, crypt_data_device(cd)); |
2455 | 0 | if ((opal_block_bytes * opal_alignment_granularity_blocks) > sector_size) |
2456 | 0 | sector_size = opal_block_bytes * opal_alignment_granularity_blocks; |
2457 | 0 | if (sector_size > MAX_SECTOR_SIZE) |
2458 | 0 | sector_size = MAX_SECTOR_SIZE; |
2459 | 0 | log_dbg(cd, "Auto-detected optimal encryption sector size for device %s is %d bytes.", |
2460 | 0 | device_path(crypt_data_device(cd)), sector_size); |
2461 | 0 | } else |
2462 | 0 | sector_size = params->sector_size; |
2463 | | |
2464 | | /* To ensure it is obvious and explicit that OPAL is being used, set the |
2465 | | * subsystem tag if the user hasn't passed one. */ |
2466 | 0 | if (!params->subsystem) { |
2467 | 0 | params->subsystem = "HW-OPAL"; |
2468 | 0 | subsystem_overridden = true; |
2469 | 0 | } |
2470 | | |
2471 | | /* We need to give the drive a segment number - use the partition number if there is |
2472 | | * one, otherwise the first valid (1) number if it's a single-volume setup */ |
2473 | 0 | r = crypt_dev_get_partition_number(device_path(crypt_data_device(cd))); |
2474 | 0 | if (r > 0) |
2475 | 0 | opal_segment_number = r; |
2476 | |
|
2477 | 0 | if (cipher) { |
2478 | 0 | if (params->integrity_params && params->integrity_params->integrity_key_size) |
2479 | 0 | integrity_key_size = params->integrity_params->integrity_key_size; |
2480 | |
|
2481 | 0 | r = LUKS2_check_encryption_params(cd, cipher, cipher_mode, integrity, 0, |
2482 | 0 | volume_keys_size - opal_params->user_key_size, |
2483 | 0 | params, &integrity, &integrity_key_size); |
2484 | 0 | if (r < 0) |
2485 | 0 | goto out; |
2486 | 0 | } |
2487 | | |
2488 | 0 | r = device_size(crypt_data_device(cd), &device_size_bytes); |
2489 | 0 | if (r < 0) |
2490 | 0 | goto out; |
2491 | | |
2492 | 0 | r = LUKS2_hdr_get_storage_params(cd, alignment_offset_bytes, required_alignment_bytes, |
2493 | 0 | &metadata_size_bytes, &keyslots_size_bytes, &data_offset_bytes); |
2494 | 0 | if (r < 0) |
2495 | 0 | goto out; |
2496 | | |
2497 | 0 | r = -EINVAL; |
2498 | 0 | if (device_size_bytes < data_offset_bytes && !cd->metadata_device) { |
2499 | 0 | log_err(cd, _("Device %s is too small."), device_path(crypt_data_device(cd))); |
2500 | 0 | goto out; |
2501 | 0 | } |
2502 | | |
2503 | 0 | device_size_bytes -= data_offset_bytes; |
2504 | 0 | range_size_bytes = device_size_bytes - (device_size_bytes % (opal_block_bytes * opal_alignment_granularity_blocks)); |
2505 | 0 | if (!range_size_bytes) |
2506 | 0 | goto out; |
2507 | | |
2508 | 0 | if (device_size_bytes != range_size_bytes) |
2509 | 0 | log_err(cd, _("Compensating device size by %" PRIu64 " sectors to align it with OPAL alignment granularity."), |
2510 | 0 | (device_size_bytes - range_size_bytes) / SECTOR_SIZE); |
2511 | |
|
2512 | 0 | if (cipher) { |
2513 | 0 | r = LUKS2_check_encryption_sector(cd, range_size_bytes, data_offset_bytes, sector_size, |
2514 | 0 | sector_size_autodetect, integrity == NULL, |
2515 | 0 | §or_size); |
2516 | 0 | if (r < 0) |
2517 | 0 | goto out; |
2518 | | |
2519 | 0 | if (*cipher_mode != '\0') |
2520 | 0 | r = snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode); |
2521 | 0 | else |
2522 | 0 | r = snprintf(cipher_spec, sizeof(cipher_spec), "%s", cipher); |
2523 | 0 | if (r < 0 || (size_t)r >= sizeof(cipher_spec)) { |
2524 | 0 | r = -EINVAL; |
2525 | 0 | goto out; |
2526 | 0 | } |
2527 | 0 | } |
2528 | | |
2529 | 0 | r = LUKS2_generate_hdr(cd, &cd->u.luks2.hdr, cd->volume_key, |
2530 | 0 | cipher ? cipher_spec : NULL, |
2531 | 0 | integrity, integrity_key_size, |
2532 | 0 | uuid, |
2533 | 0 | sector_size, |
2534 | 0 | data_offset_bytes, |
2535 | 0 | metadata_size_bytes, keyslots_size_bytes, |
2536 | 0 | range_size_bytes, |
2537 | 0 | opal_segment_number, |
2538 | 0 | opal_params->user_key_size); |
2539 | 0 | if (r < 0) |
2540 | 0 | goto out; |
2541 | | |
2542 | 0 | if (params->label || params->subsystem) { |
2543 | 0 | r = LUKS2_hdr_labels(cd, &cd->u.luks2.hdr, |
2544 | 0 | params->label, params->subsystem, 0); |
2545 | 0 | if (r < 0) |
2546 | 0 | goto out; |
2547 | 0 | } |
2548 | | |
2549 | 0 | device_set_block_size(crypt_data_device(cd), sector_size); |
2550 | |
|
2551 | 0 | r = LUKS2_wipe_header_areas(cd, &cd->u.luks2.hdr); |
2552 | 0 | if (r < 0) { |
2553 | 0 | log_err(cd, _("Cannot wipe header on device %s."), |
2554 | 0 | mdata_device_path(cd)); |
2555 | 0 | if (device_size_bytes < LUKS2_hdr_and_areas_size(&cd->u.luks2.hdr)) |
2556 | 0 | log_err(cd, _("Device %s is too small."), device_path(crypt_metadata_device(cd))); |
2557 | 0 | goto out; |
2558 | 0 | } |
2559 | | |
2560 | 0 | range_offset_blocks = (data_offset_bytes + partition_offset_sectors * SECTOR_SIZE) / opal_block_bytes; |
2561 | |
|
2562 | 0 | r = opal_exclusive_lock(cd, crypt_data_device(cd), &opal_lh); |
2563 | 0 | if (r < 0) { |
2564 | 0 | log_err(cd, _("Failed to acquire OPAL lock on device %s."), device_path(crypt_data_device(cd))); |
2565 | 0 | goto out; |
2566 | 0 | } |
2567 | | |
2568 | 0 | r = opal_setup_ranges(cd, crypt_data_device(cd), user_key ?: cd->volume_key, |
2569 | 0 | range_offset_blocks, range_size_bytes / opal_block_bytes, |
2570 | 0 | opal_block_bytes, opal_segment_number, |
2571 | 0 | opal_params->admin_key, opal_params->admin_key_size, |
2572 | 0 | !!(cd->compatibility & CRYPT_COMPAT_DISABLE_HW_OPAL_SUM), |
2573 | 0 | &opal_requirement_version); |
2574 | 0 | if (r < 0) { |
2575 | 0 | if (r == -EPERM) |
2576 | 0 | log_err(cd, _("Incorrect OPAL Admin key.")); |
2577 | 0 | else |
2578 | 0 | log_err(cd, _("Cannot setup OPAL segment.")); |
2579 | 0 | goto out; |
2580 | 0 | } |
2581 | | |
2582 | 0 | opal_range_reset = true; |
2583 | |
|
2584 | 0 | log_dbg(cd, "Adding LUKS2 OPAL requirement flag (version: %u).", opal_requirement_version); |
2585 | 0 | r = LUKS2_config_set_requirement_version(cd, &cd->u.luks2.hdr, CRYPT_REQUIREMENT_OPAL, |
2586 | 0 | opal_requirement_version, false); |
2587 | 0 | if (r < 0) |
2588 | 0 | goto out; |
2589 | | |
2590 | | /* integrity metadata goes in unlocked OPAL locking range */ |
2591 | 0 | if (crypt_get_integrity_tag_size(cd)) { |
2592 | 0 | r = opal_unlock(cd, crypt_data_device(cd), opal_segment_number, user_key ?: cd->volume_key); |
2593 | 0 | if (r < 0) |
2594 | 0 | goto out; |
2595 | | |
2596 | 0 | r = crypt_wipe_device(cd, crypt_data_device(cd), CRYPT_WIPE_ZERO, |
2597 | 0 | crypt_get_data_offset(cd) * SECTOR_SIZE, |
2598 | 0 | 8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL); |
2599 | 0 | if (r < 0) { |
2600 | 0 | if (r == -EBUSY) |
2601 | 0 | log_err(cd, _("Cannot format device %s in use."), |
2602 | 0 | data_device_path(cd)); |
2603 | 0 | else if (r == -EACCES) { |
2604 | 0 | log_err(cd, _("Cannot format device %s, permission denied."), |
2605 | 0 | data_device_path(cd)); |
2606 | 0 | r = -EINVAL; |
2607 | 0 | } else |
2608 | 0 | log_err(cd, _("Cannot wipe header on device %s."), |
2609 | 0 | data_device_path(cd)); |
2610 | |
|
2611 | 0 | goto out; |
2612 | 0 | } |
2613 | | |
2614 | 0 | if (integrity_key_size) { |
2615 | 0 | integrity_key = crypt_alloc_volume_key(integrity_key_size, |
2616 | 0 | crypt_volume_key_get_key(cd->volume_key) + volume_keys_size - integrity_key_size); |
2617 | |
|
2618 | 0 | if (!integrity_key) { |
2619 | 0 | r = -ENOMEM; |
2620 | 0 | goto out; |
2621 | 0 | } |
2622 | 0 | } |
2623 | | |
2624 | 0 | r = INTEGRITY_format(cd, params->integrity_params, integrity_key, NULL, NULL, |
2625 | | /* |
2626 | | * Create reduced dm-integrity device only if locking range size does |
2627 | | * not match device size. |
2628 | | */ |
2629 | 0 | device_size_bytes != range_size_bytes ? range_size_bytes / SECTOR_SIZE : 0, NULL, false); |
2630 | 0 | if (r) |
2631 | 0 | log_err(cd, _("Cannot format integrity for device %s."), |
2632 | 0 | data_device_path(cd)); |
2633 | |
|
2634 | 0 | crypt_free_volume_key(integrity_key); |
2635 | 0 | if (r < 0) |
2636 | 0 | goto out; |
2637 | | |
2638 | 0 | r = INTEGRITY_data_sectors(cd, crypt_data_device(cd), |
2639 | 0 | crypt_get_data_offset(cd) * SECTOR_SIZE, |
2640 | 0 | &provided_data_sectors); |
2641 | 0 | if (r < 0) |
2642 | 0 | goto out; |
2643 | | |
2644 | 0 | if (!LUKS2_segment_set_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, |
2645 | 0 | &(uint64_t) {provided_data_sectors * SECTOR_SIZE})) { |
2646 | 0 | r = -EINVAL; |
2647 | 0 | goto out; |
2648 | 0 | } |
2649 | | |
2650 | 0 | r = opal_lock(cd, crypt_data_device(cd), opal_segment_number); |
2651 | 0 | if (r < 0) |
2652 | 0 | goto out; |
2653 | 0 | } |
2654 | | |
2655 | | /* override sequence id check with format */ |
2656 | 0 | r = LUKS2_hdr_write_force(cd, &cd->u.luks2.hdr); |
2657 | 0 | if (r < 0) { |
2658 | 0 | if (r == -EBUSY) |
2659 | 0 | log_err(cd, _("Cannot format device %s in use."), |
2660 | 0 | mdata_device_path(cd)); |
2661 | 0 | else if (r == -EACCES) { |
2662 | 0 | log_err(cd, _("Cannot format device %s, permission denied."), |
2663 | 0 | mdata_device_path(cd)); |
2664 | 0 | r = -EINVAL; |
2665 | 0 | } else if (r == -EIO) { |
2666 | 0 | log_err(cd, _("Cannot format device %s, OPAL device seems to be fully write-protected now."), |
2667 | 0 | mdata_device_path(cd)); |
2668 | 0 | log_err(cd, _("This is perhaps a bug in firmware. Run OPAL PSID reset and reconnect for recovery.")); |
2669 | 0 | } else |
2670 | 0 | log_err(cd, _("Cannot format device %s."), |
2671 | 0 | mdata_device_path(cd)); |
2672 | 0 | } |
2673 | |
|
2674 | 0 | out: |
2675 | 0 | crypt_free_volume_key(user_key); |
2676 | |
|
2677 | 0 | if (subsystem_overridden) |
2678 | 0 | params->subsystem = NULL; |
2679 | |
|
2680 | 0 | if (r >= 0) { |
2681 | 0 | opal_exclusive_unlock(cd, opal_lh); |
2682 | 0 | return 0; |
2683 | 0 | } |
2684 | | |
2685 | 0 | if (opal_range_reset && |
2686 | 0 | (opal_reset_segment(cd, crypt_data_device(cd), opal_segment_number, |
2687 | 0 | opal_params->admin_key, opal_params->admin_key_size) < 0)) |
2688 | 0 | log_err(cd, _("Locking range %d reset on device %s failed."), |
2689 | 0 | opal_segment_number, device_path(crypt_data_device(cd))); |
2690 | |
|
2691 | 0 | opal_exclusive_unlock(cd, opal_lh); |
2692 | 0 | LUKS2_hdr_free(cd, &cd->u.luks2.hdr); |
2693 | |
|
2694 | 0 | crypt_set_null_type(cd); |
2695 | 0 | crypt_free_volume_key(cd->volume_key); |
2696 | 0 | cd->volume_key = NULL; |
2697 | |
|
2698 | 0 | return r; |
2699 | 0 | } |
2700 | | |
2701 | | static int _crypt_format_loopaes(struct crypt_device *cd, |
2702 | | const char *cipher, |
2703 | | const char *uuid, |
2704 | | size_t volume_key_size, |
2705 | | struct crypt_params_loopaes *params) |
2706 | 0 | { |
2707 | 0 | if (!crypt_metadata_device(cd)) { |
2708 | 0 | log_err(cd, _("Can't format LOOPAES without device.")); |
2709 | 0 | return -EINVAL; |
2710 | 0 | } |
2711 | | |
2712 | 0 | if (volume_key_size > 1024) { |
2713 | 0 | log_err(cd, _("Invalid key size.")); |
2714 | 0 | return -EINVAL; |
2715 | 0 | } |
2716 | | |
2717 | 0 | if (uuid) { |
2718 | 0 | log_err(cd, _("UUID is not supported for this crypt type.")); |
2719 | 0 | return -EINVAL; |
2720 | 0 | } |
2721 | | |
2722 | 0 | if (cd->metadata_device) { |
2723 | 0 | log_err(cd, _("Detached metadata device is not supported for this crypt type.")); |
2724 | 0 | return -EINVAL; |
2725 | 0 | } |
2726 | | |
2727 | 0 | if (!(cd->type = strdup(CRYPT_LOOPAES))) |
2728 | 0 | return -ENOMEM; |
2729 | | |
2730 | 0 | cd->u.loopaes.key_size = volume_key_size; |
2731 | |
|
2732 | 0 | cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER); |
2733 | 0 | if (!cd->u.loopaes.cipher) |
2734 | 0 | return -ENOMEM; |
2735 | | |
2736 | 0 | if (params && params->hash) { |
2737 | 0 | cd->u.loopaes.hdr.hash = strdup(params->hash); |
2738 | 0 | if (!cd->u.loopaes.hdr.hash) { |
2739 | 0 | free(cd->u.loopaes.cipher); |
2740 | 0 | cd->u.loopaes.cipher = NULL; |
2741 | 0 | return -ENOMEM; |
2742 | 0 | } |
2743 | 0 | } |
2744 | | |
2745 | 0 | cd->u.loopaes.hdr.offset = params ? params->offset : 0; |
2746 | 0 | cd->u.loopaes.hdr.skip = params ? params->skip : 0; |
2747 | |
|
2748 | 0 | return 0; |
2749 | 0 | } |
2750 | | |
2751 | | static int _crypt_format_verity(struct crypt_device *cd, |
2752 | | const char *uuid, |
2753 | | struct crypt_params_verity *params) |
2754 | 0 | { |
2755 | 0 | int r = 0, hash_size; |
2756 | 0 | uint64_t data_device_size, hash_blocks_size; |
2757 | 0 | struct device *fec_device = NULL; |
2758 | 0 | char *fec_device_path = NULL, *hash_name = NULL, *root_hash = NULL, *salt = NULL; |
2759 | |
|
2760 | 0 | if (!crypt_metadata_device(cd)) { |
2761 | 0 | log_err(cd, _("Can't format VERITY without device.")); |
2762 | 0 | return -EINVAL; |
2763 | 0 | } |
2764 | | |
2765 | 0 | if (!params) |
2766 | 0 | return -EINVAL; |
2767 | | |
2768 | 0 | if (!params->data_device && !cd->metadata_device) |
2769 | 0 | return -EINVAL; |
2770 | | |
2771 | 0 | if (params->hash_type > VERITY_MAX_HASH_TYPE) { |
2772 | 0 | log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type); |
2773 | 0 | return -EINVAL; |
2774 | 0 | } |
2775 | | |
2776 | 0 | if (VERITY_BLOCK_SIZE_OK(params->data_block_size) || |
2777 | 0 | VERITY_BLOCK_SIZE_OK(params->hash_block_size)) { |
2778 | 0 | log_err(cd, _("Unsupported VERITY block size.")); |
2779 | 0 | return -EINVAL; |
2780 | 0 | } |
2781 | | |
2782 | 0 | if (MISALIGNED_512(params->hash_area_offset)) { |
2783 | 0 | log_err(cd, _("Unsupported VERITY hash offset.")); |
2784 | 0 | return -EINVAL; |
2785 | 0 | } |
2786 | | |
2787 | 0 | if (MISALIGNED_512(params->fec_area_offset)) { |
2788 | 0 | log_err(cd, _("Unsupported VERITY FEC offset.")); |
2789 | 0 | return -EINVAL; |
2790 | 0 | } |
2791 | | |
2792 | 0 | if (!(cd->type = strdup(CRYPT_VERITY))) |
2793 | 0 | return -ENOMEM; |
2794 | | |
2795 | 0 | if (params->data_device) { |
2796 | 0 | r = crypt_set_data_device(cd, params->data_device); |
2797 | 0 | if (r) |
2798 | 0 | return r; |
2799 | 0 | } |
2800 | | |
2801 | 0 | if (!params->data_size) { |
2802 | 0 | r = device_size(cd->device, &data_device_size); |
2803 | 0 | if (r < 0) |
2804 | 0 | return r; |
2805 | | |
2806 | 0 | cd->u.verity.hdr.data_size = data_device_size / params->data_block_size; |
2807 | 0 | } else |
2808 | 0 | cd->u.verity.hdr.data_size = params->data_size; |
2809 | | |
2810 | 0 | if (device_is_identical(crypt_metadata_device(cd), crypt_data_device(cd)) > 0 && |
2811 | 0 | (cd->u.verity.hdr.data_size * params->data_block_size) > params->hash_area_offset) { |
2812 | 0 | log_err(cd, _("Data area overlaps with hash area.")); |
2813 | 0 | return -EINVAL; |
2814 | 0 | } |
2815 | | |
2816 | 0 | hash_size = crypt_hash_size(params->hash_name); |
2817 | 0 | if (hash_size <= 0) { |
2818 | 0 | log_err(cd, _("Hash algorithm %s not supported."), |
2819 | 0 | params->hash_name); |
2820 | 0 | return -EINVAL; |
2821 | 0 | } |
2822 | 0 | cd->u.verity.root_hash_size = hash_size; |
2823 | |
|
2824 | 0 | if (params->fec_device) { |
2825 | 0 | fec_device_path = strdup(params->fec_device); |
2826 | 0 | if (!fec_device_path) |
2827 | 0 | return -ENOMEM; |
2828 | 0 | r = device_alloc(cd, &fec_device, params->fec_device); |
2829 | 0 | if (r < 0) { |
2830 | 0 | r = -ENOMEM; |
2831 | 0 | goto out; |
2832 | 0 | } |
2833 | | |
2834 | 0 | hash_blocks_size = VERITY_hash_blocks(cd, params) * params->hash_block_size; |
2835 | 0 | if (device_is_identical(crypt_metadata_device(cd), fec_device) > 0 && |
2836 | 0 | (params->hash_area_offset + hash_blocks_size) > params->fec_area_offset) { |
2837 | 0 | log_err(cd, _("Hash area overlaps with FEC area.")); |
2838 | 0 | r = -EINVAL; |
2839 | 0 | goto out; |
2840 | 0 | } |
2841 | | |
2842 | 0 | if (device_is_identical(crypt_data_device(cd), fec_device) > 0 && |
2843 | 0 | (cd->u.verity.hdr.data_size * params->data_block_size) > params->fec_area_offset) { |
2844 | 0 | log_err(cd, _("Data area overlaps with FEC area.")); |
2845 | 0 | r = -EINVAL; |
2846 | 0 | goto out; |
2847 | 0 | } |
2848 | 0 | } |
2849 | | |
2850 | 0 | root_hash = malloc(cd->u.verity.root_hash_size); |
2851 | 0 | hash_name = strdup(params->hash_name); |
2852 | 0 | salt = malloc(params->salt_size); |
2853 | |
|
2854 | 0 | if (!root_hash || !hash_name || !salt) { |
2855 | 0 | r = -ENOMEM; |
2856 | 0 | goto out; |
2857 | 0 | } |
2858 | | |
2859 | 0 | cd->u.verity.hdr.flags = params->flags; |
2860 | 0 | cd->u.verity.root_hash = root_hash; |
2861 | 0 | cd->u.verity.hdr.hash_name = hash_name; |
2862 | 0 | cd->u.verity.hdr.data_device = NULL; |
2863 | 0 | cd->u.verity.fec_device = fec_device; |
2864 | 0 | cd->u.verity.hdr.fec_device = fec_device_path; |
2865 | 0 | cd->u.verity.hdr.fec_roots = params->fec_roots; |
2866 | 0 | cd->u.verity.hdr.data_block_size = params->data_block_size; |
2867 | 0 | cd->u.verity.hdr.hash_block_size = params->hash_block_size; |
2868 | 0 | cd->u.verity.hdr.hash_area_offset = params->hash_area_offset; |
2869 | 0 | cd->u.verity.hdr.fec_area_offset = params->fec_area_offset; |
2870 | 0 | cd->u.verity.hdr.hash_type = params->hash_type; |
2871 | 0 | cd->u.verity.hdr.flags = params->flags; |
2872 | 0 | cd->u.verity.hdr.salt_size = params->salt_size; |
2873 | 0 | cd->u.verity.hdr.salt = salt; |
2874 | |
|
2875 | 0 | if (params->salt) |
2876 | 0 | memcpy(salt, params->salt, params->salt_size); |
2877 | 0 | else |
2878 | 0 | r = crypt_random_get(cd, salt, params->salt_size, CRYPT_RND_SALT); |
2879 | 0 | if (r) |
2880 | 0 | goto out; |
2881 | | |
2882 | 0 | if (params->flags & CRYPT_VERITY_CREATE_HASH) { |
2883 | 0 | r = VERITY_create(cd, &cd->u.verity.hdr, |
2884 | 0 | cd->u.verity.root_hash, cd->u.verity.root_hash_size); |
2885 | 0 | if (!r && params->fec_device) |
2886 | 0 | r = VERITY_FEC_process(cd, &cd->u.verity.hdr, cd->u.verity.fec_device, 0, NULL); |
2887 | 0 | if (r) |
2888 | 0 | goto out; |
2889 | 0 | } |
2890 | | |
2891 | 0 | if (!(params->flags & CRYPT_VERITY_NO_HEADER)) { |
2892 | 0 | if (uuid) { |
2893 | 0 | if (!(cd->u.verity.uuid = strdup(uuid))) |
2894 | 0 | r = -ENOMEM; |
2895 | 0 | } else |
2896 | 0 | r = VERITY_UUID_generate(&cd->u.verity.uuid); |
2897 | |
|
2898 | 0 | if (!r) |
2899 | 0 | r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset, |
2900 | 0 | cd->u.verity.uuid, |
2901 | 0 | &cd->u.verity.hdr); |
2902 | 0 | } |
2903 | |
|
2904 | 0 | out: |
2905 | 0 | if (r) { |
2906 | 0 | device_free(cd, fec_device); |
2907 | 0 | free(root_hash); |
2908 | 0 | free(hash_name); |
2909 | 0 | free(fec_device_path); |
2910 | 0 | free(salt); |
2911 | 0 | } |
2912 | |
|
2913 | 0 | return r; |
2914 | 0 | } |
2915 | | |
2916 | | static int _crypt_format_integrity(struct crypt_device *cd, |
2917 | | const char *uuid, |
2918 | | struct crypt_params_integrity *params, |
2919 | | const char *integrity_key, size_t integrity_key_size, |
2920 | | bool integrity_inline) |
2921 | 0 | { |
2922 | 0 | int r; |
2923 | 0 | uint32_t integrity_tag_size; |
2924 | 0 | char *integrity = NULL, *journal_integrity = NULL, *journal_crypt = NULL; |
2925 | 0 | struct volume_key *journal_crypt_key = NULL, *journal_mac_key = NULL, *ik = NULL; |
2926 | |
|
2927 | 0 | if (!params) |
2928 | 0 | return -EINVAL; |
2929 | | |
2930 | 0 | if (uuid) { |
2931 | 0 | log_err(cd, _("UUID is not supported for this crypt type.")); |
2932 | 0 | return -EINVAL; |
2933 | 0 | } |
2934 | | |
2935 | 0 | if (integrity_key_size && integrity_key_size != params->integrity_key_size) { |
2936 | 0 | log_err(cd, _("Integrity key size mismatch.")); |
2937 | 0 | return -EINVAL; |
2938 | 0 | } |
2939 | | |
2940 | 0 | r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL); |
2941 | 0 | if (r < 0) |
2942 | 0 | return r; |
2943 | | |
2944 | | /* Wipe first 8 sectors - fs magic numbers etc. */ |
2945 | 0 | r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO, 0, |
2946 | 0 | 8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL); |
2947 | 0 | if (r < 0) { |
2948 | 0 | log_err(cd, _("Cannot wipe header on device %s."), |
2949 | 0 | mdata_device_path(cd)); |
2950 | 0 | return r; |
2951 | 0 | } |
2952 | | |
2953 | 0 | if (!(cd->type = strdup(CRYPT_INTEGRITY))) |
2954 | 0 | return -ENOMEM; |
2955 | | |
2956 | 0 | if (params->journal_crypt_key) { |
2957 | 0 | journal_crypt_key = crypt_alloc_volume_key(params->journal_crypt_key_size, |
2958 | 0 | params->journal_crypt_key); |
2959 | 0 | if (!journal_crypt_key) |
2960 | 0 | return -ENOMEM; |
2961 | 0 | } |
2962 | | |
2963 | 0 | if (params->journal_integrity_key) { |
2964 | 0 | journal_mac_key = crypt_alloc_volume_key(params->journal_integrity_key_size, |
2965 | 0 | params->journal_integrity_key); |
2966 | 0 | if (!journal_mac_key) { |
2967 | 0 | r = -ENOMEM; |
2968 | 0 | goto out; |
2969 | 0 | } |
2970 | 0 | } |
2971 | | |
2972 | 0 | if (params->integrity && !(integrity = strdup(params->integrity))) { |
2973 | 0 | r = -ENOMEM; |
2974 | 0 | goto out; |
2975 | 0 | } |
2976 | 0 | if (params->journal_integrity && !(journal_integrity = strdup(params->journal_integrity))) { |
2977 | 0 | r = -ENOMEM; |
2978 | 0 | goto out; |
2979 | 0 | } |
2980 | 0 | if (params->journal_crypt && !(journal_crypt = strdup(params->journal_crypt))) { |
2981 | 0 | r = -ENOMEM; |
2982 | 0 | goto out; |
2983 | 0 | } |
2984 | | |
2985 | 0 | integrity_tag_size = INTEGRITY_hash_tag_size(integrity); |
2986 | 0 | if (integrity_tag_size > 0 && params->tag_size && integrity_tag_size != params->tag_size) |
2987 | 0 | log_std(cd, _("WARNING: Requested tag size %d bytes differs from %s size output (%d bytes).\n"), |
2988 | 0 | params->tag_size, integrity, integrity_tag_size); |
2989 | |
|
2990 | 0 | if (params->tag_size) |
2991 | 0 | integrity_tag_size = params->tag_size; |
2992 | |
|
2993 | 0 | cd->u.integrity.journal_crypt_key = journal_crypt_key; |
2994 | 0 | cd->u.integrity.journal_mac_key = journal_mac_key; |
2995 | 0 | cd->u.integrity.params.journal_size = params->journal_size; |
2996 | 0 | cd->u.integrity.params.journal_watermark = params->journal_watermark; |
2997 | 0 | cd->u.integrity.params.journal_commit_time = params->journal_commit_time; |
2998 | 0 | cd->u.integrity.params.interleave_sectors = params->interleave_sectors; |
2999 | 0 | cd->u.integrity.params.buffer_sectors = params->buffer_sectors; |
3000 | 0 | cd->u.integrity.params.sector_size = params->sector_size; |
3001 | 0 | cd->u.integrity.params.tag_size = integrity_tag_size; |
3002 | 0 | cd->u.integrity.params.integrity = integrity; |
3003 | 0 | cd->u.integrity.params.journal_integrity = journal_integrity; |
3004 | 0 | cd->u.integrity.params.journal_crypt = journal_crypt; |
3005 | |
|
3006 | 0 | if (params->integrity_key_size) { |
3007 | 0 | if (!integrity_key) |
3008 | 0 | ik = crypt_generate_volume_key(cd, params->integrity_key_size, KEY_QUALITY_EMPTY); |
3009 | 0 | else |
3010 | 0 | ik = crypt_alloc_volume_key(params->integrity_key_size, integrity_key); |
3011 | 0 | if (!ik) { |
3012 | 0 | r = -ENOMEM; |
3013 | 0 | goto out; |
3014 | 0 | } |
3015 | 0 | } |
3016 | | |
3017 | 0 | r = INTEGRITY_format(cd, params, ik, cd->u.integrity.journal_crypt_key, |
3018 | 0 | cd->u.integrity.journal_mac_key, 0, &cd->u.integrity.sb_flags, |
3019 | 0 | integrity_inline); |
3020 | 0 | if (r) |
3021 | 0 | log_err(cd, _("Cannot format integrity for device %s."), mdata_device_path(cd)); |
3022 | |
|
3023 | 0 | crypt_free_volume_key(ik); |
3024 | 0 | out: |
3025 | 0 | if (r) { |
3026 | 0 | crypt_free_volume_key(journal_crypt_key); |
3027 | 0 | crypt_free_volume_key(journal_mac_key); |
3028 | 0 | free(integrity); |
3029 | 0 | free(journal_integrity); |
3030 | 0 | free(journal_crypt); |
3031 | 0 | } |
3032 | |
|
3033 | 0 | return r; |
3034 | 0 | } |
3035 | | |
3036 | | int crypt_format_inline(struct crypt_device *cd, |
3037 | | const char *type, |
3038 | | const char *cipher, |
3039 | | const char *cipher_mode, |
3040 | | const char *uuid, |
3041 | | const char *volume_key, |
3042 | | size_t volume_key_size, |
3043 | | void *params) |
3044 | 0 | { |
3045 | 0 | struct crypt_params_luks2 *lparams; |
3046 | 0 | const struct crypt_params_integrity *iparams; |
3047 | 0 | uint32_t device_tag_size, required_tag_size; |
3048 | 0 | struct device *idevice; |
3049 | 0 | size_t sector_size, required_sector_size; |
3050 | 0 | int r; |
3051 | |
|
3052 | 0 | if (!cd || !params) |
3053 | 0 | return -EINVAL; |
3054 | | |
3055 | 0 | if (cd->type) { |
3056 | 0 | log_dbg(cd, "Context already formatted as %s.", cd->type); |
3057 | 0 | return -EINVAL; |
3058 | 0 | } |
3059 | | |
3060 | 0 | log_dbg(cd, "Formatting device %s as type %s with inline tags.", mdata_device_path(cd) ?: "(none)", type); |
3061 | |
|
3062 | 0 | crypt_reset_null_type(cd); |
3063 | |
|
3064 | 0 | r = init_crypto(cd); |
3065 | 0 | if (r < 0) |
3066 | 0 | return r; |
3067 | | |
3068 | 0 | if (isINTEGRITY(type)) { |
3069 | 0 | lparams = NULL; |
3070 | 0 | iparams = params; |
3071 | 0 | idevice = crypt_metadata_device(cd); |
3072 | 0 | required_sector_size = iparams->sector_size; |
3073 | 0 | required_tag_size = iparams->tag_size; |
3074 | | |
3075 | | /* Unused in standalone integrity */ |
3076 | 0 | if (cipher || cipher_mode) |
3077 | 0 | return -EINVAL; |
3078 | 0 | } else if (isLUKS2(type)) { |
3079 | 0 | lparams = params; |
3080 | 0 | iparams = lparams->integrity_params; |
3081 | |
|
3082 | 0 | if (lparams->data_device) { |
3083 | 0 | if (!cd->metadata_device) |
3084 | 0 | cd->metadata_device = cd->device; |
3085 | 0 | else |
3086 | 0 | device_free(cd, cd->device); |
3087 | 0 | cd->device = NULL; |
3088 | 0 | if (device_alloc(cd, &cd->device, lparams->data_device) < 0) |
3089 | 0 | return -ENOMEM; |
3090 | 0 | } |
3091 | | |
3092 | 0 | idevice = crypt_data_device(cd); |
3093 | 0 | required_sector_size = lparams->sector_size; |
3094 | |
|
3095 | 0 | if (!lparams->integrity || !idevice) |
3096 | 0 | return -EINVAL; |
3097 | | |
3098 | 0 | required_tag_size = INTEGRITY_tag_size(lparams->integrity, cipher, cipher_mode); |
3099 | 0 | } else { |
3100 | 0 | log_err(cd, _("Unknown or unsupported device type %s requested."), type); |
3101 | 0 | return -EINVAL; |
3102 | 0 | } |
3103 | | |
3104 | | /* In inline mode journal will be never used, check that params are not set */ |
3105 | 0 | if (iparams && (iparams->journal_size || iparams->journal_watermark || iparams->journal_commit_time || |
3106 | 0 | iparams->interleave_sectors || iparams->journal_integrity || iparams->journal_integrity_key || |
3107 | 0 | iparams->journal_integrity_key_size || iparams->journal_crypt || iparams->journal_crypt_key || |
3108 | 0 | iparams->journal_integrity_key_size)) |
3109 | 0 | return -EINVAL; |
3110 | | |
3111 | 0 | r = device_is_nop_dif(idevice, &device_tag_size); |
3112 | 0 | if (r < 0) |
3113 | 0 | return r; |
3114 | | |
3115 | 0 | if (!r) { |
3116 | 0 | log_err(cd, _("Device %s does not provide inline integrity data fields."), mdata_device_path(cd)); |
3117 | 0 | return -EINVAL; |
3118 | 0 | } |
3119 | | |
3120 | | /* We can get device_tag_size = 0 as kernel provides this info only for some block devices */ |
3121 | 0 | if (device_tag_size > 0 && device_tag_size < required_tag_size) { |
3122 | 0 | log_err(cd, _("Inline tag size %" PRIu32 " [bytes] is larger than %" PRIu32 " provided by device %s."), |
3123 | 0 | required_tag_size, device_tag_size, mdata_device_path(cd)); |
3124 | 0 | return -EINVAL; |
3125 | 0 | } |
3126 | 0 | log_dbg(cd, "Inline integrity is supported (%" PRIu32 ").", device_tag_size); |
3127 | | |
3128 | | /* Inline must use sectors size as hardware device */ |
3129 | 0 | sector_size = device_block_size(cd, idevice); |
3130 | 0 | if (!sector_size) |
3131 | 0 | return -EINVAL; |
3132 | | |
3133 | | /* No autodetection, use device sector size */ |
3134 | 0 | if (isLUKS2(type) && lparams && !required_sector_size) |
3135 | 0 | lparams->sector_size = sector_size; |
3136 | 0 | else if (sector_size != required_sector_size) { |
3137 | 0 | log_err(cd, _("Sector must be the same as device hardware sector (%zu bytes)."), sector_size); |
3138 | 0 | return -EINVAL; |
3139 | 0 | } |
3140 | | |
3141 | 0 | if (isINTEGRITY(type)) |
3142 | 0 | r = _crypt_format_integrity(cd, uuid, params, volume_key, volume_key_size, true); |
3143 | 0 | else if (isLUKS2(type)) |
3144 | 0 | r = _crypt_format_luks2(cd, cipher, cipher_mode, |
3145 | 0 | uuid, volume_key, volume_key_size, params, false, true); |
3146 | 0 | else |
3147 | 0 | r = -EINVAL; |
3148 | |
|
3149 | 0 | if (r < 0) { |
3150 | 0 | crypt_set_null_type(cd); |
3151 | 0 | crypt_free_volume_key(cd->volume_key); |
3152 | 0 | cd->volume_key = NULL; |
3153 | 0 | } |
3154 | |
|
3155 | 0 | return r; |
3156 | 0 | } |
3157 | | |
3158 | | static int _crypt_format(struct crypt_device *cd, |
3159 | | const char *type, |
3160 | | const char *cipher, |
3161 | | const char *cipher_mode, |
3162 | | const char *uuid, |
3163 | | const char *volume_key, |
3164 | | size_t volume_key_size, |
3165 | | void *params, |
3166 | | bool sector_size_autodetect) |
3167 | 0 | { |
3168 | 0 | int r; |
3169 | |
|
3170 | 0 | if (!cd || !type) |
3171 | 0 | return -EINVAL; |
3172 | | |
3173 | 0 | if (cd->type) { |
3174 | 0 | log_dbg(cd, "Context already formatted as %s.", cd->type); |
3175 | 0 | return -EINVAL; |
3176 | 0 | } |
3177 | | |
3178 | 0 | log_dbg(cd, "Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type); |
3179 | |
|
3180 | 0 | crypt_reset_null_type(cd); |
3181 | |
|
3182 | 0 | r = init_crypto(cd); |
3183 | 0 | if (r < 0) |
3184 | 0 | return r; |
3185 | | |
3186 | 0 | if (isPLAIN(type)) |
3187 | 0 | r = _crypt_format_plain(cd, cipher, cipher_mode, |
3188 | 0 | uuid, volume_key_size, params); |
3189 | 0 | else if (isLUKS1(type)) |
3190 | 0 | r = _crypt_format_luks1(cd, cipher, cipher_mode, |
3191 | 0 | uuid, volume_key, volume_key_size, params); |
3192 | 0 | else if (isLUKS2(type)) |
3193 | 0 | r = _crypt_format_luks2(cd, cipher, cipher_mode, |
3194 | 0 | uuid, volume_key, volume_key_size, params, sector_size_autodetect, false); |
3195 | 0 | else if (isLOOPAES(type)) |
3196 | 0 | r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params); |
3197 | 0 | else if (isVERITY(type)) |
3198 | 0 | r = _crypt_format_verity(cd, uuid, params); |
3199 | 0 | else if (isINTEGRITY(type)) |
3200 | 0 | r = _crypt_format_integrity(cd, uuid, params, volume_key, volume_key_size, false); |
3201 | 0 | else { |
3202 | 0 | log_err(cd, _("Unknown or unsupported device type %s requested."), type); |
3203 | 0 | r = -EINVAL; |
3204 | 0 | } |
3205 | |
|
3206 | 0 | if (r < 0) { |
3207 | 0 | crypt_set_null_type(cd); |
3208 | 0 | crypt_free_volume_key(cd->volume_key); |
3209 | 0 | cd->volume_key = NULL; |
3210 | 0 | } |
3211 | |
|
3212 | 0 | return r; |
3213 | 0 | } |
3214 | | |
3215 | | CRYPT_SYMBOL_EXPORT_NEW(int, crypt_format, 2, 4, |
3216 | | /* crypt_format parameters follows */ |
3217 | | struct crypt_device *cd, |
3218 | | const char *type, |
3219 | | const char *cipher, |
3220 | | const char *cipher_mode, |
3221 | | const char *uuid, |
3222 | | const char *volume_key, |
3223 | | size_t volume_key_size, |
3224 | | void *params) |
3225 | 0 | { |
3226 | 0 | return _crypt_format(cd, type, cipher, cipher_mode, uuid, volume_key, volume_key_size, params, true); |
3227 | 0 | } |
3228 | | |
3229 | | |
3230 | | CRYPT_SYMBOL_EXPORT_OLD(int, crypt_format, 2, 0, |
3231 | | /* crypt_format parameters follows */ |
3232 | | struct crypt_device *cd, |
3233 | | const char *type, |
3234 | | const char *cipher, |
3235 | | const char *cipher_mode, |
3236 | | const char *uuid, |
3237 | | const char *volume_key, |
3238 | | size_t volume_key_size, |
3239 | | void *params) |
3240 | 0 | { |
3241 | 0 | return _crypt_format(cd, type, cipher, cipher_mode, uuid, volume_key, volume_key_size, params, false); |
3242 | 0 | } |
3243 | | |
3244 | | int crypt_repair(struct crypt_device *cd, |
3245 | | const char *requested_type, |
3246 | | void *params __attribute__((unused))) |
3247 | 0 | { |
3248 | 0 | int r; |
3249 | |
|
3250 | 0 | if (!cd) |
3251 | 0 | return -EINVAL; |
3252 | | |
3253 | 0 | log_dbg(cd, "Trying to repair %s crypt type from device %s.", |
3254 | 0 | requested_type ?: "any", mdata_device_path(cd) ?: "(none)"); |
3255 | |
|
3256 | 0 | if (!crypt_metadata_device(cd)) |
3257 | 0 | return -EINVAL; |
3258 | | |
3259 | 0 | if (requested_type && !isLUKS(requested_type)) |
3260 | 0 | return -EINVAL; |
3261 | | |
3262 | | /* Load with repair */ |
3263 | 0 | r = _crypt_load_luks(cd, requested_type, false, true); |
3264 | 0 | if (r < 0) |
3265 | 0 | return r; |
3266 | | |
3267 | | /* cd->type and header must be set in context */ |
3268 | 0 | r = crypt_check_data_device_size(cd); |
3269 | 0 | if (r < 0) |
3270 | 0 | crypt_set_null_type(cd); |
3271 | |
|
3272 | 0 | return r; |
3273 | 0 | } |
3274 | | |
3275 | | /* compare volume keys */ |
3276 | | static int _compare_volume_keys(struct volume_key *svk, struct volume_key *tvk) |
3277 | 0 | { |
3278 | 0 | if (svk == tvk) |
3279 | 0 | return 0; |
3280 | | |
3281 | 0 | if (!svk || !tvk) |
3282 | 0 | return 1; |
3283 | | |
3284 | 0 | if (crypt_volume_key_length(svk) != crypt_volume_key_length(tvk)) |
3285 | 0 | return 1; |
3286 | | |
3287 | | /* No switch between keyring and direct key specification */ |
3288 | 0 | if ((!crypt_volume_key_description(svk) && crypt_volume_key_description(tvk)) || |
3289 | 0 | (crypt_volume_key_description(svk) && !crypt_volume_key_description(tvk)) || |
3290 | 0 | (!crypt_volume_key_is_set(svk) && crypt_volume_key_is_set(tvk)) || |
3291 | 0 | (crypt_volume_key_is_set(svk) && !crypt_volume_key_is_set(tvk))) |
3292 | 0 | return 1; |
3293 | | |
3294 | 0 | if (crypt_volume_key_description(svk) && |
3295 | 0 | (crypt_volume_key_kernel_key_type(svk) != crypt_volume_key_kernel_key_type(tvk) || |
3296 | 0 | strcmp(crypt_volume_key_description(svk), crypt_volume_key_description(tvk)))) |
3297 | 0 | return 1; |
3298 | | |
3299 | 0 | if (crypt_volume_key_is_set(svk) && |
3300 | 0 | crypt_backend_memeq(crypt_volume_key_get_key(svk), |
3301 | 0 | crypt_volume_key_get_key(tvk), |
3302 | 0 | crypt_volume_key_length(svk))) |
3303 | 0 | return 1; |
3304 | | |
3305 | 0 | return 0; |
3306 | 0 | } |
3307 | | |
3308 | | static int _compare_volume_keys_luks2(struct volume_key *svk, struct volume_key *tvk) |
3309 | 0 | { |
3310 | 0 | if (svk == tvk) |
3311 | 0 | return 0; |
3312 | | |
3313 | 0 | if (!svk || !tvk) |
3314 | 0 | return 1; |
3315 | | |
3316 | 0 | if (crypt_volume_key_length(svk) != crypt_volume_key_length(tvk)) |
3317 | 0 | return 1; |
3318 | | |
3319 | 0 | if ((!crypt_volume_key_is_set(svk) && !crypt_volume_key_description(svk)) || |
3320 | 0 | (!crypt_volume_key_is_set(tvk) && !crypt_volume_key_description(tvk))) |
3321 | 0 | return 1; |
3322 | | |
3323 | 0 | if (crypt_volume_key_is_set(svk) && crypt_volume_key_is_set(tvk) && |
3324 | 0 | crypt_backend_memeq(crypt_volume_key_get_key(svk), |
3325 | 0 | crypt_volume_key_get_key(tvk), |
3326 | 0 | crypt_volume_key_length(svk))) |
3327 | 0 | return 1; |
3328 | | |
3329 | 0 | if (crypt_volume_key_description(svk) && crypt_volume_key_description(tvk)) |
3330 | 0 | return (crypt_volume_key_kernel_key_type(svk) != crypt_volume_key_kernel_key_type(tvk) || |
3331 | 0 | strcmp(crypt_volume_key_description(svk), crypt_volume_key_description(tvk))); |
3332 | | |
3333 | 0 | return 0; |
3334 | 0 | } |
3335 | | |
3336 | | static int _compare_device_types(struct crypt_device *cd, |
3337 | | const struct crypt_dm_active_device *src, |
3338 | | const struct crypt_dm_active_device *tgt) |
3339 | 0 | { |
3340 | 0 | if (!tgt->uuid) { |
3341 | 0 | log_dbg(cd, "Missing device uuid in target device."); |
3342 | 0 | return -EINVAL; |
3343 | 0 | } |
3344 | | |
3345 | | /* |
3346 | | * FIXME: The CRYPT_SUBDEV prefix should be enough but we need |
3347 | | * to keep INTEGRITY- for dm-integrity subdevices opened with |
3348 | | * cryptsetup version < 2.8.0. Drop the INTEGRITY condition |
3349 | | * in next Y release. |
3350 | | */ |
3351 | 0 | if (isLUKS2(cd->type) && |
3352 | 0 | (!strncmp("INTEGRITY-", tgt->uuid, strlen("INTEGRITY-")) || |
3353 | 0 | !strncmp(CRYPT_SUBDEV, tgt->uuid, strlen(CRYPT_SUBDEV)))) { |
3354 | 0 | if (dm_uuid_cmp(tgt->uuid, src->uuid)) { |
3355 | 0 | log_dbg(cd, "LUKS UUID mismatch."); |
3356 | 0 | return -EINVAL; |
3357 | 0 | } |
3358 | 0 | } else if (isLUKS(cd->type)) { |
3359 | 0 | if (!src->uuid || strncmp(cd->type, tgt->uuid, strlen(cd->type)) || |
3360 | 0 | dm_uuid_cmp(tgt->uuid, src->uuid)) { |
3361 | 0 | log_dbg(cd, "LUKS UUID mismatch."); |
3362 | 0 | return -EINVAL; |
3363 | 0 | } |
3364 | 0 | } else if (isPLAIN(cd->type) || isLOOPAES(cd->type)) { |
3365 | 0 | if (strncmp(cd->type, tgt->uuid, strlen(cd->type))) { |
3366 | 0 | log_dbg(cd, "Unexpected uuid prefix %s in target device.", tgt->uuid); |
3367 | 0 | return -EINVAL; |
3368 | 0 | } |
3369 | 0 | } else if (!isINTEGRITY(cd->type)) { |
3370 | 0 | log_dbg(cd, "Unsupported device type %s for reload.", cd->type ?: "<empty>"); |
3371 | 0 | return -ENOTSUP; |
3372 | 0 | } |
3373 | | |
3374 | 0 | return 0; |
3375 | 0 | } |
3376 | | |
3377 | | static int _compare_crypt_devices(struct crypt_device *cd, |
3378 | | const struct dm_target *src, |
3379 | | const struct dm_target *tgt) |
3380 | 0 | { |
3381 | 0 | char *src_cipher = NULL, *src_integrity = NULL; |
3382 | 0 | int r = -EINVAL; |
3383 | | |
3384 | | /* for crypt devices keys are mandatory */ |
3385 | 0 | if (!src->u.crypt.vk || !tgt->u.crypt.vk) |
3386 | 0 | return -EINVAL; |
3387 | | |
3388 | | /* CIPHER checks */ |
3389 | 0 | if (!src->u.crypt.cipher || !tgt->u.crypt.cipher) |
3390 | 0 | return -EINVAL; |
3391 | | |
3392 | | /* |
3393 | | * dm_query_target converts capi cipher specification to dm-crypt format. |
3394 | | * We need to do same for cipher specification requested in source |
3395 | | * device. |
3396 | | */ |
3397 | 0 | if (crypt_capi_to_cipher(&src_cipher, &src_integrity, src->u.crypt.cipher, src->u.crypt.integrity)) |
3398 | 0 | return -EINVAL; |
3399 | | |
3400 | 0 | if (strcmp(src_cipher, tgt->u.crypt.cipher)) { |
3401 | 0 | log_dbg(cd, "Cipher specs do not match."); |
3402 | 0 | goto out; |
3403 | 0 | } |
3404 | | |
3405 | 0 | if (crypt_volume_key_length(tgt->u.crypt.vk) == 0 && crypt_is_cipher_null(tgt->u.crypt.cipher)) |
3406 | 0 | log_dbg(cd, "Existing device uses cipher null. Skipping key comparison."); |
3407 | 0 | else if (cd && isLUKS2(cd->type)) { |
3408 | 0 | if (_compare_volume_keys_luks2(src->u.crypt.vk, tgt->u.crypt.vk)) { |
3409 | 0 | log_dbg(cd, "Keys in LUKS2 context and target device do not match."); |
3410 | 0 | goto out; |
3411 | 0 | } |
3412 | 0 | } else if (_compare_volume_keys(src->u.crypt.vk, tgt->u.crypt.vk)) { |
3413 | 0 | log_dbg(cd, "Keys in context and target device do not match."); |
3414 | 0 | goto out; |
3415 | 0 | } |
3416 | | |
3417 | 0 | if (crypt_strcmp(src_integrity, tgt->u.crypt.integrity)) { |
3418 | 0 | log_dbg(cd, "Integrity parameters do not match."); |
3419 | 0 | goto out; |
3420 | 0 | } |
3421 | | |
3422 | 0 | if (src->u.crypt.offset != tgt->u.crypt.offset || |
3423 | 0 | src->u.crypt.sector_size != tgt->u.crypt.sector_size || |
3424 | 0 | src->u.crypt.iv_offset != tgt->u.crypt.iv_offset || |
3425 | 0 | src->u.crypt.tag_size != tgt->u.crypt.tag_size) { |
3426 | 0 | log_dbg(cd, "Integer parameters do not match."); |
3427 | 0 | goto out; |
3428 | 0 | } |
3429 | | |
3430 | 0 | if (device_is_identical(src->data_device, tgt->data_device) <= 0) |
3431 | 0 | log_dbg(cd, "Data devices do not match."); |
3432 | 0 | else |
3433 | 0 | r = 0; |
3434 | |
|
3435 | 0 | out: |
3436 | 0 | free(src_cipher); |
3437 | 0 | free(src_integrity); |
3438 | |
|
3439 | 0 | return r; |
3440 | 0 | } |
3441 | | |
3442 | | static int _compare_integrity_devices(struct crypt_device *cd, |
3443 | | const struct dm_target *src, |
3444 | | const struct dm_target *tgt) |
3445 | 0 | { |
3446 | | /* |
3447 | | * some parameters may be implicit (and set in dm-integrity ctor) |
3448 | | * |
3449 | | * journal_size |
3450 | | * journal_watermark |
3451 | | * journal_commit_time |
3452 | | * buffer_sectors |
3453 | | * interleave_sectors |
3454 | | */ |
3455 | | |
3456 | | /* check remaining integer values that makes sense */ |
3457 | 0 | if (src->u.integrity.tag_size != tgt->u.integrity.tag_size || |
3458 | 0 | src->u.integrity.offset != tgt->u.integrity.offset || |
3459 | 0 | src->u.integrity.sector_size != tgt->u.integrity.sector_size) { |
3460 | 0 | log_dbg(cd, "Integer parameters do not match."); |
3461 | 0 | return -EINVAL; |
3462 | 0 | } |
3463 | | |
3464 | 0 | if (crypt_strcmp(src->u.integrity.integrity, tgt->u.integrity.integrity) || |
3465 | 0 | crypt_strcmp(src->u.integrity.journal_integrity, tgt->u.integrity.journal_integrity) || |
3466 | 0 | crypt_strcmp(src->u.integrity.journal_crypt, tgt->u.integrity.journal_crypt)) { |
3467 | 0 | log_dbg(cd, "Journal parameters do not match."); |
3468 | 0 | return -EINVAL; |
3469 | 0 | } |
3470 | | |
3471 | | /* unfortunately dm-integrity doesn't support keyring */ |
3472 | 0 | if (_compare_volume_keys(src->u.integrity.vk, tgt->u.integrity.vk) || |
3473 | 0 | _compare_volume_keys(src->u.integrity.journal_integrity_key, tgt->u.integrity.journal_integrity_key) || |
3474 | 0 | _compare_volume_keys(src->u.integrity.journal_crypt_key, tgt->u.integrity.journal_crypt_key)) { |
3475 | 0 | log_dbg(cd, "Journal keys do not match."); |
3476 | 0 | return -EINVAL; |
3477 | 0 | } |
3478 | | |
3479 | 0 | if (device_is_identical(src->data_device, tgt->data_device) <= 0) { |
3480 | 0 | log_dbg(cd, "Data devices do not match."); |
3481 | 0 | return -EINVAL; |
3482 | 0 | } |
3483 | | |
3484 | 0 | return 0; |
3485 | 0 | } |
3486 | | |
3487 | | int crypt_compare_dm_devices(struct crypt_device *cd, |
3488 | | const struct crypt_dm_active_device *src, |
3489 | | const struct crypt_dm_active_device *tgt) |
3490 | 0 | { |
3491 | 0 | int r; |
3492 | 0 | const struct dm_target *s, *t; |
3493 | |
|
3494 | 0 | if (!src || !tgt) |
3495 | 0 | return -EINVAL; |
3496 | | |
3497 | 0 | r = _compare_device_types(cd, src, tgt); |
3498 | 0 | if (r) |
3499 | 0 | return r; |
3500 | | |
3501 | 0 | s = &src->segment; |
3502 | 0 | t = &tgt->segment; |
3503 | |
|
3504 | 0 | while (s || t) { |
3505 | 0 | if (!s || !t) { |
3506 | 0 | log_dbg(cd, "segments count mismatch."); |
3507 | 0 | return -EINVAL; |
3508 | 0 | } |
3509 | 0 | if (s->type != t->type) { |
3510 | 0 | log_dbg(cd, "segment type mismatch."); |
3511 | 0 | r = -EINVAL; |
3512 | 0 | break; |
3513 | 0 | } |
3514 | | |
3515 | 0 | switch (s->type) { |
3516 | 0 | case DM_CRYPT: |
3517 | 0 | r = _compare_crypt_devices(cd, s, t); |
3518 | 0 | break; |
3519 | 0 | case DM_INTEGRITY: |
3520 | 0 | r = _compare_integrity_devices(cd, s, t); |
3521 | 0 | break; |
3522 | 0 | case DM_LINEAR: |
3523 | 0 | r = (s->u.linear.offset == t->u.linear.offset) ? 0 : -EINVAL; |
3524 | 0 | break; |
3525 | 0 | default: |
3526 | 0 | r = -ENOTSUP; |
3527 | 0 | } |
3528 | | |
3529 | 0 | if (r) |
3530 | 0 | break; |
3531 | | |
3532 | 0 | s = s->next; |
3533 | 0 | t = t->next; |
3534 | 0 | } |
3535 | | |
3536 | 0 | return r; |
3537 | 0 | } |
3538 | | |
3539 | | static int _reload_device(struct crypt_device *cd, const char *name, |
3540 | | struct crypt_dm_active_device *sdmd, uint64_t dmflags) |
3541 | 0 | { |
3542 | 0 | int r; |
3543 | 0 | struct crypt_dm_active_device tdmd; |
3544 | 0 | struct dm_target *src, *tgt = &tdmd.segment; |
3545 | |
|
3546 | 0 | assert(cd); |
3547 | 0 | assert(sdmd); |
3548 | |
|
3549 | 0 | if (!cd->type || !name || !(sdmd->flags & CRYPT_ACTIVATE_REFRESH)) |
3550 | 0 | return -EINVAL; |
3551 | | |
3552 | 0 | src = &sdmd->segment; |
3553 | |
|
3554 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER | |
3555 | 0 | DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE | |
3556 | 0 | DM_ACTIVE_CRYPT_KEY | DM_ACTIVE_INTEGRITY_PARAMS | |
3557 | 0 | DM_ACTIVE_JOURNAL_CRYPT_KEY | DM_ACTIVE_JOURNAL_MAC_KEY, &tdmd); |
3558 | 0 | if (r < 0) { |
3559 | 0 | log_err(cd, _("Device %s is not active."), name); |
3560 | 0 | return -EINVAL; |
3561 | 0 | } |
3562 | | |
3563 | 0 | if (!single_segment(&tdmd) || |
3564 | 0 | (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY) || |
3565 | 0 | (tgt->type == DM_CRYPT && tgt->u.crypt.tag_size)) { |
3566 | 0 | r = -ENOTSUP; |
3567 | 0 | log_err(cd, _("Unsupported parameters on device %s."), name); |
3568 | 0 | goto out; |
3569 | 0 | } |
3570 | | |
3571 | 0 | r = crypt_compare_dm_devices(cd, sdmd, &tdmd); |
3572 | 0 | if (r) { |
3573 | 0 | log_err(cd, _("Mismatching parameters on device %s."), name); |
3574 | 0 | goto out; |
3575 | 0 | } |
3576 | | |
3577 | | /* Changing read only flag for active device makes no sense */ |
3578 | 0 | if (tdmd.flags & CRYPT_ACTIVATE_READONLY) |
3579 | 0 | sdmd->flags |= CRYPT_ACTIVATE_READONLY; |
3580 | 0 | else |
3581 | 0 | sdmd->flags &= ~CRYPT_ACTIVATE_READONLY; |
3582 | | |
3583 | | /* |
3584 | | * Only LUKS2 allows altering between volume key |
3585 | | * passed by hexbyte representation and reference |
3586 | | * to kernel keyring service. |
3587 | | * |
3588 | | * To make it easier pass src key directly after |
3589 | | * it was properly verified in crypt_compare_dm_devices |
3590 | | * call above. |
3591 | | */ |
3592 | 0 | if (isLUKS2(cd->type) && tgt->type == DM_CRYPT && src->u.crypt.vk) { |
3593 | 0 | crypt_free_volume_key(tgt->u.crypt.vk); |
3594 | 0 | tgt->u.crypt.vk = src->u.crypt.vk; |
3595 | 0 | } |
3596 | |
|
3597 | 0 | if (tgt->type == DM_CRYPT) |
3598 | 0 | r = device_block_adjust(cd, src->data_device, DEV_OK, |
3599 | 0 | src->u.crypt.offset, &sdmd->size, NULL); |
3600 | 0 | else if (tgt->type == DM_INTEGRITY) |
3601 | 0 | r = device_block_adjust(cd, src->data_device, DEV_OK, |
3602 | 0 | src->u.integrity.offset, &sdmd->size, NULL); |
3603 | 0 | else |
3604 | 0 | r = -EINVAL; |
3605 | |
|
3606 | 0 | if (r) |
3607 | 0 | goto out; |
3608 | | |
3609 | 0 | tdmd.flags = sdmd->flags; |
3610 | 0 | tgt->size = tdmd.size = sdmd->size; |
3611 | |
|
3612 | 0 | r = dm_reload_device(cd, name, &tdmd, dmflags, 1); |
3613 | 0 | out: |
3614 | | /* otherwise dm_targets_free would free src key */ |
3615 | 0 | if (tgt->type == DM_CRYPT && src->u.crypt.vk == tgt->u.crypt.vk) |
3616 | 0 | tgt->u.crypt.vk = NULL; |
3617 | |
|
3618 | 0 | dm_targets_free(cd, &tdmd); |
3619 | 0 | free(CONST_CAST(void*)tdmd.uuid); |
3620 | |
|
3621 | 0 | return r; |
3622 | 0 | } |
3623 | | |
3624 | | static int _reload_device_with_integrity(struct crypt_device *cd, |
3625 | | const char *name, |
3626 | | const char *iname, |
3627 | | const char *ipath, |
3628 | | struct crypt_dm_active_device *sdmd, |
3629 | | struct crypt_dm_active_device *sdmdi) |
3630 | 0 | { |
3631 | 0 | int r; |
3632 | 0 | struct crypt_dm_active_device tdmd, tdmdi = {}; |
3633 | 0 | struct dm_target *src, *srci, *tgt = &tdmd.segment, *tgti = &tdmdi.segment; |
3634 | 0 | struct device *data_device = NULL; |
3635 | 0 | bool clear = false; |
3636 | |
|
3637 | 0 | assert(cd); |
3638 | 0 | assert(sdmd); |
3639 | 0 | assert(sdmdi); |
3640 | |
|
3641 | 0 | if (!cd->type || !name || !iname || !(sdmd->flags & CRYPT_ACTIVATE_REFRESH)) |
3642 | 0 | return -EINVAL; |
3643 | | |
3644 | 0 | src = &sdmd->segment; |
3645 | 0 | srci = &sdmdi->segment; |
3646 | |
|
3647 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER | |
3648 | 0 | DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE | |
3649 | 0 | DM_ACTIVE_CRYPT_KEY, &tdmd); |
3650 | 0 | if (r < 0) { |
3651 | 0 | log_err(cd, _("Device %s is not active."), name); |
3652 | 0 | return -EINVAL; |
3653 | 0 | } |
3654 | | |
3655 | 0 | if (!single_segment(&tdmd) || tgt->type != DM_CRYPT || !tgt->u.crypt.tag_size) { |
3656 | 0 | log_err(cd, _("Unsupported parameters on device %s."), name); |
3657 | 0 | r = -ENOTSUP; |
3658 | 0 | goto out; |
3659 | 0 | } |
3660 | | |
3661 | 0 | r = dm_query_device(cd, iname, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &tdmdi); |
3662 | 0 | if (r < 0) { |
3663 | 0 | log_err(cd, _("Device %s is not active."), iname); |
3664 | 0 | r = -EINVAL; |
3665 | 0 | goto out; |
3666 | 0 | } |
3667 | | |
3668 | 0 | if (!single_segment(&tdmdi) || tgti->type != DM_INTEGRITY) { |
3669 | 0 | log_err(cd, _("Unsupported parameters on device %s."), iname); |
3670 | 0 | r = -ENOTSUP; |
3671 | 0 | goto out; |
3672 | 0 | } |
3673 | | |
3674 | 0 | r = crypt_compare_dm_devices(cd, sdmdi, &tdmdi); |
3675 | 0 | if (r) { |
3676 | 0 | log_err(cd, _("Mismatching parameters on device %s."), iname); |
3677 | 0 | goto out; |
3678 | 0 | } |
3679 | | |
3680 | | /* unsupported underneath dm-crypt with auth. encryption */ |
3681 | 0 | if (sdmdi->segment.u.integrity.meta_device || tdmdi.segment.u.integrity.meta_device) { |
3682 | 0 | r = -ENOTSUP; |
3683 | 0 | goto out; |
3684 | 0 | } |
3685 | | |
3686 | 0 | r = device_alloc(cd, &data_device, ipath); |
3687 | 0 | if (r < 0) |
3688 | 0 | goto out; |
3689 | | |
3690 | 0 | r = device_block_adjust(cd, srci->data_device, DEV_OK, |
3691 | 0 | srci->u.integrity.offset, &sdmdi->size, NULL); |
3692 | 0 | if (r) |
3693 | 0 | goto out; |
3694 | | |
3695 | 0 | src->data_device = data_device; |
3696 | |
|
3697 | 0 | r = crypt_compare_dm_devices(cd, sdmd, &tdmd); |
3698 | 0 | if (r) { |
3699 | 0 | log_err(cd, _("Crypt devices mismatch.")); |
3700 | 0 | goto out; |
3701 | 0 | } |
3702 | | |
3703 | | /* Changing read only flag for active device makes no sense */ |
3704 | 0 | if (tdmd.flags & CRYPT_ACTIVATE_READONLY) |
3705 | 0 | sdmd->flags |= CRYPT_ACTIVATE_READONLY; |
3706 | 0 | else |
3707 | 0 | sdmd->flags &= ~CRYPT_ACTIVATE_READONLY; |
3708 | |
|
3709 | 0 | if (tdmdi.flags & CRYPT_ACTIVATE_READONLY) |
3710 | 0 | sdmdi->flags |= CRYPT_ACTIVATE_READONLY; |
3711 | 0 | else |
3712 | 0 | sdmdi->flags &= ~CRYPT_ACTIVATE_READONLY; |
3713 | | |
3714 | | /* |
3715 | | * To make it easier pass src key directly after |
3716 | | * it was properly verified in crypt_compare_dm_devices |
3717 | | * call above. |
3718 | | */ |
3719 | 0 | crypt_free_volume_key(tgt->u.crypt.vk); |
3720 | 0 | tgt->u.crypt.vk = src->u.crypt.vk; |
3721 | |
|
3722 | 0 | r = device_block_adjust(cd, src->data_device, DEV_OK, |
3723 | 0 | src->u.crypt.offset, &sdmd->size, NULL); |
3724 | 0 | if (r) |
3725 | 0 | goto out; |
3726 | | |
3727 | 0 | tdmd.flags = sdmd->flags; |
3728 | 0 | tdmd.size = sdmd->size; |
3729 | |
|
3730 | 0 | if ((r = dm_reload_device(cd, iname, sdmdi, 0, 0))) { |
3731 | 0 | log_err(cd, _("Failed to reload device %s."), iname); |
3732 | 0 | goto out; |
3733 | 0 | } |
3734 | | |
3735 | 0 | if ((r = dm_reload_device(cd, name, &tdmd, 0, 0))) { |
3736 | 0 | log_err(cd, _("Failed to reload device %s."), name); |
3737 | 0 | clear = true; |
3738 | 0 | goto out; |
3739 | 0 | } |
3740 | | |
3741 | 0 | if ((r = dm_suspend_device(cd, name, 0))) { |
3742 | 0 | log_err(cd, _("Failed to suspend device %s."), name); |
3743 | 0 | clear = true; |
3744 | 0 | goto out; |
3745 | 0 | } |
3746 | | |
3747 | 0 | if ((r = dm_suspend_device(cd, iname, 0))) { |
3748 | 0 | log_err(cd, _("Failed to suspend device %s."), iname); |
3749 | 0 | clear = true; |
3750 | 0 | goto out; |
3751 | 0 | } |
3752 | | |
3753 | 0 | if ((r = dm_resume_device(cd, iname, act2dmflags(sdmdi->flags)))) { |
3754 | 0 | log_err(cd, _("Failed to resume device %s."), iname); |
3755 | 0 | clear = true; |
3756 | 0 | goto out; |
3757 | 0 | } |
3758 | | |
3759 | 0 | r = dm_resume_device(cd, name, act2dmflags(tdmd.flags)); |
3760 | 0 | if (!r) |
3761 | 0 | goto out; |
3762 | | |
3763 | | /* |
3764 | | * This is worst case scenario. We have active underlying dm-integrity device with |
3765 | | * new table but dm-crypt resume failed for some reason. Tear everything down and |
3766 | | * burn it for good. |
3767 | | */ |
3768 | | |
3769 | 0 | log_err(cd, _("Fatal error while reloading device %s (on top of device %s)."), name, iname); |
3770 | |
|
3771 | 0 | if (dm_error_device(cd, name)) |
3772 | 0 | log_err(cd, _("Failed to switch device %s to dm-error."), name); |
3773 | 0 | if (dm_error_device(cd, iname)) |
3774 | 0 | log_err(cd, _("Failed to switch device %s to dm-error."), iname); |
3775 | 0 | out: |
3776 | 0 | if (clear) { |
3777 | 0 | dm_clear_device(cd, name); |
3778 | 0 | dm_clear_device(cd, iname); |
3779 | |
|
3780 | 0 | if (dm_status_suspended(cd, name) > 0) |
3781 | 0 | dm_resume_device(cd, name, 0); |
3782 | 0 | if (dm_status_suspended(cd, iname) > 0) |
3783 | 0 | dm_resume_device(cd, iname, 0); |
3784 | 0 | } |
3785 | | |
3786 | | /* otherwise dm_targets_free would free src key */ |
3787 | 0 | if (tgt->u.crypt.vk == src->u.crypt.vk) |
3788 | 0 | tgt->u.crypt.vk = NULL; |
3789 | 0 | dm_targets_free(cd, &tdmd); |
3790 | 0 | dm_targets_free(cd, &tdmdi); |
3791 | 0 | free(CONST_CAST(void*)tdmdi.uuid); |
3792 | 0 | free(CONST_CAST(void*)tdmd.uuid); |
3793 | 0 | device_free(cd, data_device); |
3794 | |
|
3795 | 0 | return r; |
3796 | 0 | } |
3797 | | |
3798 | | int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size) |
3799 | 0 | { |
3800 | 0 | struct crypt_dm_active_device dmdq, dmd = {}; |
3801 | 0 | struct dm_target *tgt = &dmdq.segment; |
3802 | 0 | struct crypt_params_integrity params = {}; |
3803 | 0 | uint64_t supported_flags = 0, dmflags = 0; |
3804 | 0 | uint64_t old_size; |
3805 | 0 | int r; |
3806 | | |
3807 | | /* Device context type must be initialized */ |
3808 | 0 | if (!cd || !cd->type || !name) |
3809 | 0 | return -EINVAL; |
3810 | | |
3811 | 0 | if (isTCRYPT(cd->type) || isBITLK(cd->type)) { |
3812 | 0 | log_err(cd, _("This operation is not supported for this device type.")); |
3813 | 0 | return -ENOTSUP; |
3814 | 0 | } |
3815 | | |
3816 | 0 | if (isLUKS2(cd->type) && !LUKS2_segments_dynamic_size(&cd->u.luks2.hdr)) { |
3817 | 0 | log_err(cd, _("Can not resize LUKS2 device with static size.")); |
3818 | 0 | return -EINVAL; |
3819 | 0 | } |
3820 | | |
3821 | 0 | if (isLUKS2(cd->type) && crypt_get_integrity_tag_size(cd)) { |
3822 | 0 | log_err(cd, _("Resize of LUKS2 device with integrity protection is not supported.")); |
3823 | 0 | return -ENOTSUP; |
3824 | 0 | } |
3825 | | |
3826 | 0 | if (new_size) |
3827 | 0 | log_dbg(cd, "Resizing device %s to %" PRIu64 " sectors.", name, new_size); |
3828 | 0 | else |
3829 | 0 | log_dbg(cd, "Resizing device %s to underlying device size.", name); |
3830 | |
|
3831 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_CRYPT_KEYSIZE | DM_ACTIVE_CRYPT_KEY | |
3832 | 0 | DM_ACTIVE_INTEGRITY_PARAMS | DM_ACTIVE_JOURNAL_CRYPT_KEY | |
3833 | 0 | DM_ACTIVE_JOURNAL_MAC_KEY, &dmdq); |
3834 | 0 | if (r < 0) { |
3835 | 0 | log_err(cd, _("Device %s is not active."), name); |
3836 | 0 | return -EINVAL; |
3837 | 0 | } |
3838 | 0 | if (!single_segment(&dmdq) || (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY)) { |
3839 | 0 | log_dbg(cd, "Unsupported device table detected in %s.", name); |
3840 | 0 | r = -EINVAL; |
3841 | 0 | goto out; |
3842 | 0 | } |
3843 | | |
3844 | 0 | if ((dmdq.flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_key_in_keyring(cd)) { |
3845 | 0 | r = -EPERM; |
3846 | 0 | goto out; |
3847 | 0 | } |
3848 | | |
3849 | 0 | if (crypt_key_in_keyring(cd)) { |
3850 | 0 | if (isLUKS2(cd->type)) |
3851 | 0 | r = LUKS2_key_description_by_segment(cd, &cd->u.luks2.hdr, |
3852 | 0 | tgt->u.crypt.vk, CRYPT_DEFAULT_SEGMENT); |
3853 | 0 | else if (isPLAIN(cd->type)) |
3854 | 0 | r = 0; /* key description was set on table load */ |
3855 | 0 | else |
3856 | 0 | r = -EINVAL; |
3857 | 0 | if (r < 0) |
3858 | 0 | goto out; |
3859 | | |
3860 | 0 | dmdq.flags |= CRYPT_ACTIVATE_KEYRING_KEY; |
3861 | 0 | } |
3862 | | |
3863 | 0 | if (crypt_loop_device(crypt_get_device_name(cd))) { |
3864 | 0 | log_dbg(cd, "Trying to resize underlying loop device %s.", |
3865 | 0 | crypt_get_device_name(cd)); |
3866 | | /* Here we always use default size not new_size */ |
3867 | 0 | if (crypt_loop_resize(crypt_get_device_name(cd))) |
3868 | 0 | log_err(cd, _("Cannot resize loop device.")); |
3869 | 0 | } |
3870 | | |
3871 | | |
3872 | | /* |
3873 | | * Integrity device metadata are maintained by the kernel. We need to |
3874 | | * reload the device (with the same parameters) and let the kernel |
3875 | | * calculate the maximum size of integrity device and store it in the |
3876 | | * superblock. |
3877 | | */ |
3878 | 0 | if (!new_size && tgt->type == DM_INTEGRITY) { |
3879 | 0 | r = INTEGRITY_data_sectors(cd, crypt_metadata_device(cd), |
3880 | 0 | crypt_get_data_offset(cd) * SECTOR_SIZE, &old_size); |
3881 | 0 | if (r < 0) |
3882 | 0 | goto out; |
3883 | | |
3884 | 0 | dmd.size = dmdq.size; |
3885 | 0 | dmd.flags = dmdq.flags | CRYPT_ACTIVATE_REFRESH | CRYPT_ACTIVATE_PRIVATE; |
3886 | |
|
3887 | 0 | r = crypt_get_integrity_info(cd, ¶ms); |
3888 | 0 | if (r) |
3889 | 0 | goto out; |
3890 | | |
3891 | 0 | r = dm_integrity_target_set(cd, &dmd.segment, 0, dmdq.segment.size, |
3892 | 0 | crypt_metadata_device(cd), crypt_data_device(cd), |
3893 | 0 | crypt_get_integrity_tag_size(cd), crypt_get_data_offset(cd), |
3894 | 0 | crypt_get_sector_size(cd), tgt->u.integrity.vk, tgt->u.integrity.journal_crypt_key, |
3895 | 0 | tgt->u.integrity.journal_integrity_key, ¶ms); |
3896 | 0 | if (r) |
3897 | 0 | goto out; |
3898 | | /* Backend device cannot be smaller here, device_block_adjust() will fail if so. */ |
3899 | 0 | r = _reload_device(cd, name, &dmd, DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH); |
3900 | 0 | if (r) |
3901 | 0 | goto out; |
3902 | | |
3903 | 0 | r = INTEGRITY_data_sectors(cd, crypt_metadata_device(cd), |
3904 | 0 | crypt_get_data_offset(cd) * SECTOR_SIZE, &new_size); |
3905 | 0 | if (r < 0) |
3906 | 0 | goto out; |
3907 | 0 | log_dbg(cd, "Maximum integrity device size from kernel %" PRIu64, new_size); |
3908 | |
|
3909 | 0 | if (old_size == new_size && new_size == dmdq.size && |
3910 | 0 | !dm_flags(cd, tgt->type, &supported_flags) && |
3911 | 0 | !(supported_flags & DM_INTEGRITY_RESIZE_SUPPORTED)) |
3912 | 0 | log_std(cd, _("WARNING: Maximum size already set or kernel doesn't support resize.\n")); |
3913 | 0 | } |
3914 | | |
3915 | 0 | r = device_block_adjust(cd, crypt_data_device(cd), DEV_OK, |
3916 | 0 | crypt_get_data_offset(cd), &new_size, &dmdq.flags); |
3917 | 0 | if (r) |
3918 | 0 | goto out; |
3919 | | |
3920 | 0 | if (MISALIGNED(new_size, (tgt->type == DM_CRYPT ? tgt->u.crypt.sector_size : tgt->u.integrity.sector_size) >> SECTOR_SHIFT)) { |
3921 | 0 | log_err(cd, _("Device size is not aligned to requested sector size.")); |
3922 | 0 | r = -EINVAL; |
3923 | 0 | goto out; |
3924 | 0 | } |
3925 | | |
3926 | 0 | if (MISALIGNED(new_size, device_block_size(cd, crypt_data_device(cd)) >> SECTOR_SHIFT)) { |
3927 | 0 | log_err(cd, _("Device size is not aligned to device logical block size.")); |
3928 | 0 | r = -EINVAL; |
3929 | 0 | goto out; |
3930 | 0 | } |
3931 | | |
3932 | 0 | dmd.uuid = crypt_get_uuid(cd); |
3933 | 0 | dmd.size = new_size; |
3934 | 0 | dmd.flags = dmdq.flags | CRYPT_ACTIVATE_REFRESH; |
3935 | |
|
3936 | 0 | if (tgt->type == DM_CRYPT) { |
3937 | 0 | r = dm_crypt_target_set(&dmd.segment, 0, new_size, crypt_data_device(cd), |
3938 | 0 | tgt->u.crypt.vk, crypt_get_cipher_spec(cd), |
3939 | 0 | crypt_get_iv_offset(cd), crypt_get_data_offset(cd), |
3940 | 0 | crypt_get_integrity(cd), crypt_get_integrity_key_size(cd, true), crypt_get_integrity_tag_size(cd), |
3941 | 0 | crypt_get_sector_size(cd)); |
3942 | 0 | if (r < 0) |
3943 | 0 | goto out; |
3944 | 0 | } else if (tgt->type == DM_INTEGRITY) { |
3945 | 0 | r = crypt_get_integrity_info(cd, ¶ms); |
3946 | 0 | if (r) |
3947 | 0 | goto out; |
3948 | | |
3949 | 0 | r = dm_integrity_target_set(cd, &dmd.segment, 0, new_size, |
3950 | 0 | crypt_metadata_device(cd), crypt_data_device(cd), |
3951 | 0 | crypt_get_integrity_tag_size(cd), crypt_get_data_offset(cd), |
3952 | 0 | crypt_get_sector_size(cd), tgt->u.integrity.vk, tgt->u.integrity.journal_crypt_key, |
3953 | 0 | tgt->u.integrity.journal_integrity_key, ¶ms); |
3954 | 0 | if (r) |
3955 | 0 | goto out; |
3956 | 0 | } |
3957 | | |
3958 | 0 | if (new_size == dmdq.size) { |
3959 | 0 | log_dbg(cd, "Device has already requested size %" PRIu64 |
3960 | 0 | " sectors.", dmdq.size); |
3961 | 0 | r = 0; |
3962 | 0 | } else { |
3963 | 0 | if (isTCRYPT(cd->type)) |
3964 | 0 | r = -ENOTSUP; |
3965 | 0 | else if (isLUKS2(cd->type)) |
3966 | 0 | r = LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, 0, 0); |
3967 | |
|
3968 | 0 | if (!r) { |
3969 | | /* Skip flush and lockfs if extending device */ |
3970 | 0 | if (new_size > dmdq.size) |
3971 | 0 | dmflags = DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH; |
3972 | 0 | r = _reload_device(cd, name, &dmd, dmflags); |
3973 | 0 | } |
3974 | |
|
3975 | 0 | if (r && tgt->type == DM_INTEGRITY && |
3976 | 0 | !dm_flags(cd, tgt->type, &supported_flags) && |
3977 | 0 | !(supported_flags & DM_INTEGRITY_RESIZE_SUPPORTED)) |
3978 | 0 | log_err(cd, _("Resize failed, the kernel doesn't support it.")); |
3979 | 0 | } |
3980 | 0 | out: |
3981 | 0 | dm_targets_free(cd, &dmd); |
3982 | 0 | dm_targets_free(cd, &dmdq); |
3983 | |
|
3984 | 0 | return r; |
3985 | 0 | } |
3986 | | |
3987 | | int crypt_set_uuid(struct crypt_device *cd, const char *uuid) |
3988 | 0 | { |
3989 | 0 | const char *active_uuid; |
3990 | 0 | int r; |
3991 | |
|
3992 | 0 | log_dbg(cd, "%s device uuid.", uuid ? "Setting new" : "Refreshing"); |
3993 | |
|
3994 | 0 | if ((r = onlyLUKS(cd))) |
3995 | 0 | return r; |
3996 | | |
3997 | 0 | active_uuid = crypt_get_uuid(cd); |
3998 | |
|
3999 | 0 | if (uuid && active_uuid && !strncmp(uuid, active_uuid, UUID_STRING_L)) { |
4000 | 0 | log_dbg(cd, "UUID is the same as requested (%s) for device %s.", |
4001 | 0 | uuid, mdata_device_path(cd)); |
4002 | 0 | return 0; |
4003 | 0 | } |
4004 | | |
4005 | 0 | if (uuid) |
4006 | 0 | log_dbg(cd, "Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd)); |
4007 | 0 | else |
4008 | 0 | log_dbg(cd, "Requested new UUID refresh for %s.", mdata_device_path(cd)); |
4009 | |
|
4010 | 0 | if (!crypt_confirm(cd, _("Do you really want to change UUID of device?"))) |
4011 | 0 | return -EPERM; |
4012 | | |
4013 | 0 | if (isLUKS1(cd->type)) |
4014 | 0 | return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd); |
4015 | 0 | else |
4016 | 0 | return LUKS2_hdr_uuid(cd, &cd->u.luks2.hdr, uuid); |
4017 | 0 | } |
4018 | | |
4019 | | int crypt_set_label(struct crypt_device *cd, const char *label, const char *subsystem) |
4020 | 0 | { |
4021 | 0 | int r; |
4022 | |
|
4023 | 0 | log_dbg(cd, "Setting new labels."); |
4024 | |
|
4025 | 0 | if ((r = onlyLUKS2(cd))) |
4026 | 0 | return r; |
4027 | | |
4028 | 0 | return LUKS2_hdr_labels(cd, &cd->u.luks2.hdr, label, subsystem, 1); |
4029 | 0 | } |
4030 | | |
4031 | | const char *crypt_get_label(struct crypt_device *cd) |
4032 | 0 | { |
4033 | 0 | if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0)) |
4034 | 0 | return NULL; |
4035 | | |
4036 | 0 | return cd->u.luks2.hdr.label; |
4037 | 0 | } |
4038 | | |
4039 | | const char *crypt_get_subsystem(struct crypt_device *cd) |
4040 | 0 | { |
4041 | 0 | if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0)) |
4042 | 0 | return NULL; |
4043 | | |
4044 | 0 | return cd->u.luks2.hdr.subsystem; |
4045 | 0 | } |
4046 | | |
4047 | | int crypt_header_backup(struct crypt_device *cd, |
4048 | | const char *requested_type, |
4049 | | const char *backup_file) |
4050 | 0 | { |
4051 | 0 | int r; |
4052 | |
|
4053 | 0 | if (requested_type && !isLUKS(requested_type)) |
4054 | 0 | return -EINVAL; |
4055 | | |
4056 | 0 | if (!backup_file) |
4057 | 0 | return -EINVAL; |
4058 | | |
4059 | | /* Load with repair */ |
4060 | 0 | r = _crypt_load_luks(cd, requested_type, false, false); |
4061 | 0 | if (r < 0) |
4062 | 0 | return r; |
4063 | | |
4064 | 0 | log_dbg(cd, "Requested header backup of device %s (%s) to " |
4065 | 0 | "file %s.", mdata_device_path(cd), requested_type ?: "any type", backup_file); |
4066 | |
|
4067 | 0 | if (isLUKS1(cd->type) && (!requested_type || isLUKS1(requested_type))) |
4068 | 0 | r = LUKS_hdr_backup(backup_file, cd); |
4069 | 0 | else if (isLUKS2(cd->type) && (!requested_type || isLUKS2(requested_type))) |
4070 | 0 | r = LUKS2_hdr_backup(cd, &cd->u.luks2.hdr, backup_file); |
4071 | 0 | else |
4072 | 0 | r = -EINVAL; |
4073 | |
|
4074 | 0 | return r; |
4075 | 0 | } |
4076 | | |
4077 | | int crypt_header_restore(struct crypt_device *cd, |
4078 | | const char *requested_type, |
4079 | | const char *backup_file) |
4080 | 0 | { |
4081 | 0 | struct luks_phdr hdr1; |
4082 | 0 | struct luks2_hdr hdr2; |
4083 | 0 | int r, version; |
4084 | |
|
4085 | 0 | if (requested_type && !isLUKS(requested_type)) |
4086 | 0 | return -EINVAL; |
4087 | | |
4088 | 0 | if (!cd || (cd->type && !isLUKS(cd->type)) || !backup_file) |
4089 | 0 | return -EINVAL; |
4090 | | |
4091 | 0 | r = init_crypto(cd); |
4092 | 0 | if (r < 0) |
4093 | 0 | return r; |
4094 | | |
4095 | 0 | log_dbg(cd, "Requested header restore to device %s (%s) from " |
4096 | 0 | "file %s.", mdata_device_path(cd), requested_type ?: "any type", backup_file); |
4097 | |
|
4098 | 0 | version = LUKS2_hdr_version_unlocked(cd, backup_file); |
4099 | 0 | if (!version || |
4100 | 0 | (requested_type && version == 1 && !isLUKS1(requested_type)) || |
4101 | 0 | (requested_type && version == 2 && !isLUKS2(requested_type))) { |
4102 | 0 | log_err(cd, _("Header backup file does not contain compatible LUKS header.")); |
4103 | 0 | return -EINVAL; |
4104 | 0 | } |
4105 | | |
4106 | 0 | memset(&hdr2, 0, sizeof(hdr2)); |
4107 | |
|
4108 | 0 | if (!cd->type) { |
4109 | 0 | if (version == 1) |
4110 | 0 | r = LUKS_hdr_restore(backup_file, &hdr1, cd); |
4111 | 0 | else |
4112 | 0 | r = LUKS2_hdr_restore(cd, &hdr2, backup_file); |
4113 | |
|
4114 | 0 | crypt_safe_memzero(&hdr1, sizeof(hdr1)); |
4115 | 0 | crypt_safe_memzero(&hdr2, sizeof(hdr2)); |
4116 | 0 | } else if (isLUKS2(cd->type) && (!requested_type || isLUKS2(requested_type))) { |
4117 | 0 | r = LUKS2_hdr_restore(cd, &cd->u.luks2.hdr, backup_file); |
4118 | 0 | if (r) |
4119 | 0 | (void) _crypt_load_luks2(cd, 1, 0); |
4120 | 0 | } else if (isLUKS1(cd->type) && (!requested_type || isLUKS1(requested_type))) |
4121 | 0 | r = LUKS_hdr_restore(backup_file, &cd->u.luks1.hdr, cd); |
4122 | 0 | else |
4123 | 0 | r = -EINVAL; |
4124 | |
|
4125 | 0 | if (!r) |
4126 | 0 | r = _crypt_load_luks(cd, version == 1 ? CRYPT_LUKS1 : CRYPT_LUKS2, false, true); |
4127 | |
|
4128 | 0 | return r; |
4129 | 0 | } |
4130 | | |
4131 | | int crypt_header_is_detached(struct crypt_device *cd) |
4132 | 0 | { |
4133 | 0 | int r; |
4134 | |
|
4135 | 0 | if (!cd || (cd->type && !isLUKS(cd->type))) |
4136 | 0 | return -EINVAL; |
4137 | | |
4138 | 0 | r = device_is_identical(crypt_data_device(cd), crypt_metadata_device(cd)); |
4139 | 0 | if (r < 0) { |
4140 | 0 | log_dbg(cd, "Failed to compare data and metadata devices path."); |
4141 | 0 | return r; |
4142 | 0 | } |
4143 | | |
4144 | 0 | return r ? 0 : 1; |
4145 | 0 | } |
4146 | | |
4147 | | static void crypt_unlink_keyring_from_thread_keyring(struct crypt_device *cd, |
4148 | | key_serial_t keyring_id) |
4149 | 0 | { |
4150 | 0 | log_dbg(cd, "Unlinking keyring (id: %" PRIi32 ") from thread keyring.", keyring_id); |
4151 | |
|
4152 | 0 | if (keyring_unlink_key_from_thread_keyring(keyring_id)) |
4153 | 0 | log_dbg(cd, "keyring_unlink_key_from_thread_keyring failed with errno %d.", errno); |
4154 | 0 | } |
4155 | | |
4156 | | void crypt_free(struct crypt_device *cd) |
4157 | 7.72k | { |
4158 | 7.72k | if (!cd) |
4159 | 0 | return; |
4160 | | |
4161 | 7.72k | log_dbg(cd, "Releasing crypt device %s context.", mdata_device_path(cd) ?: "empty"); |
4162 | | |
4163 | 7.72k | dm_backend_exit(cd); |
4164 | 7.72k | crypt_free_volume_key(cd->volume_key); |
4165 | | |
4166 | 7.72k | if (cd->keyring_description) { |
4167 | 0 | crypt_unlink_keyring_from_thread_keyring(cd, cd->keyring_id); |
4168 | 0 | free(CONST_CAST(void*)cd->keyring_description); |
4169 | 0 | } |
4170 | | |
4171 | 7.72k | crypt_free_type(cd, NULL); |
4172 | | |
4173 | 7.72k | device_free(cd, cd->device); |
4174 | 7.72k | device_free(cd, cd->metadata_device); |
4175 | | |
4176 | 7.72k | free(CONST_CAST(void*)cd->pbkdf.type); |
4177 | 7.72k | free(CONST_CAST(void*)cd->pbkdf.hash); |
4178 | 7.72k | free(CONST_CAST(void*)cd->user_key_name1); |
4179 | 7.72k | free(CONST_CAST(void*)cd->user_key_name2); |
4180 | | |
4181 | | /* Some structures can contain keys (TCRYPT), wipe it */ |
4182 | 7.72k | crypt_safe_memzero(cd, sizeof(*cd)); |
4183 | 7.72k | free(cd); |
4184 | 7.72k | } |
4185 | | |
4186 | | int crypt_suspend(struct crypt_device *cd, |
4187 | | const char *name) |
4188 | 0 | { |
4189 | 0 | bool dm_opal_uuid; |
4190 | 0 | crypt_status_info ci; |
4191 | 0 | int r; |
4192 | 0 | struct crypt_dm_active_device dmd, dmdi = {}; |
4193 | 0 | uint32_t opal_segment_number = 1; |
4194 | 0 | uint64_t dmflags = DM_SUSPEND_WIPE_KEY; |
4195 | 0 | struct dm_target *tgt = &dmd.segment; |
4196 | 0 | char *iname = NULL; |
4197 | 0 | struct crypt_lock_handle *opal_lh = NULL; |
4198 | |
|
4199 | 0 | if (!cd || !name) |
4200 | 0 | return -EINVAL; |
4201 | | |
4202 | 0 | log_dbg(cd, "Suspending volume %s.", name); |
4203 | |
|
4204 | 0 | if (cd->type && ((r = onlyLUKS(cd)) < 0)) |
4205 | 0 | return r; |
4206 | | |
4207 | 0 | ci = crypt_status(cd, name); |
4208 | 0 | if (ci < CRYPT_ACTIVE) { |
4209 | 0 | log_err(cd, _("Volume %s is not active."), name); |
4210 | 0 | return -EINVAL; |
4211 | 0 | } |
4212 | | |
4213 | 0 | r = dm_query_device(cd, name, |
4214 | 0 | DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEY | DM_ACTIVE_CRYPT_KEYSIZE, |
4215 | 0 | &dmd); |
4216 | 0 | if (r < 0) |
4217 | 0 | return r; |
4218 | | |
4219 | 0 | log_dbg(cd, "Checking if active device %s has UUID type LUKS.", name); |
4220 | |
|
4221 | 0 | r = dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS2); |
4222 | 0 | if (r < 0) |
4223 | 0 | r = dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS1); |
4224 | |
|
4225 | 0 | if (r < 0) { |
4226 | 0 | log_err(cd, _("This operation is supported only for LUKS device.")); |
4227 | 0 | goto out; |
4228 | 0 | } |
4229 | | |
4230 | 0 | r = -EINVAL; |
4231 | |
|
4232 | 0 | if (isLUKS2(cd->type) && dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS2)) { |
4233 | 0 | log_dbg(cd, "LUKS device header type: %s mismatches DM device type.", cd->type); |
4234 | 0 | goto out; |
4235 | 0 | } |
4236 | | |
4237 | 0 | if (isLUKS1(cd->type) && dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS1)) { |
4238 | 0 | log_dbg(cd, "LUKS device header type: %s mismatches DM device type.", cd->type); |
4239 | 0 | goto out; |
4240 | 0 | } |
4241 | | |
4242 | | /* check if active device has LUKS2-OPAL dm uuid prefix */ |
4243 | 0 | dm_opal_uuid = !dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS2_HW_OPAL); |
4244 | |
|
4245 | 0 | if (!dm_opal_uuid && isLUKS2(cd->type) && |
4246 | 0 | LUKS2_segment_is_hw_opal(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)) |
4247 | 0 | goto out; |
4248 | | |
4249 | 0 | if (cd->type && (r = dm_uuid_cmp(dmd.uuid, LUKS_UUID(cd))) < 0) { |
4250 | 0 | log_dbg(cd, "LUKS device header uuid: %s mismatches DM returned uuid %s", |
4251 | 0 | LUKS_UUID(cd), dmd.uuid); |
4252 | 0 | goto out; |
4253 | 0 | } |
4254 | | |
4255 | | /* check UUID of integrity device underneath crypt device */ |
4256 | 0 | if (crypt_get_integrity_tag_size(cd)) |
4257 | 0 | iname = dm_get_active_iname(cd, name); |
4258 | |
|
4259 | 0 | r = dm_status_suspended(cd, name); |
4260 | 0 | if (r < 0) |
4261 | 0 | goto out; |
4262 | | |
4263 | 0 | if (r) { |
4264 | 0 | log_err(cd, _("Volume %s is already suspended."), name); |
4265 | 0 | r = -EINVAL; |
4266 | 0 | goto out; |
4267 | 0 | } |
4268 | | |
4269 | 0 | if (dm_opal_uuid && crypt_data_device(cd)) { |
4270 | 0 | if (isLUKS2(cd->type)) { |
4271 | 0 | r = LUKS2_get_opal_segment_number(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, &opal_segment_number); |
4272 | 0 | if (r < 0) |
4273 | 0 | goto out; |
4274 | 0 | } else { |
4275 | | /* Guess OPAL range number for LUKS2-OPAL device with missing header */ |
4276 | 0 | r = crypt_dev_get_partition_number(device_path(crypt_data_device(cd))); |
4277 | 0 | if (r > 0) |
4278 | 0 | opal_segment_number = r; |
4279 | 0 | } |
4280 | 0 | } |
4281 | | |
4282 | | /* we can't simply wipe wrapped keys. HW OPAL only encryption does not use dm-crypt target */ |
4283 | 0 | if (crypt_cipher_wrapped_key(crypt_get_cipher(cd), crypt_get_cipher_mode(cd)) || |
4284 | 0 | (dm_opal_uuid && tgt->type == DM_LINEAR)) |
4285 | 0 | dmflags &= ~DM_SUSPEND_WIPE_KEY; |
4286 | |
|
4287 | 0 | r = dm_suspend_device(cd, name, dmflags); |
4288 | 0 | if (r) { |
4289 | 0 | if (r == -ENOTSUP) |
4290 | 0 | log_err(cd, _("Suspend is not supported for device %s."), name); |
4291 | 0 | else |
4292 | 0 | log_err(cd, _("Error during suspending device %s."), name); |
4293 | 0 | goto out; |
4294 | 0 | } |
4295 | | |
4296 | | /* Suspend integrity device underneath; keep crypt suspended if it fails */ |
4297 | 0 | if (iname) { |
4298 | 0 | r = dm_suspend_device(cd, iname, 0); |
4299 | 0 | if (r) |
4300 | 0 | log_err(cd, _("Error during suspending device %s."), iname); |
4301 | 0 | } |
4302 | |
|
4303 | 0 | if (single_segment(&dmd) && tgt->type == DM_CRYPT) |
4304 | 0 | crypt_volume_key_drop_kernel_key(cd, tgt->u.crypt.vk); |
4305 | |
|
4306 | 0 | if (dm_opal_uuid && crypt_data_device(cd)) { |
4307 | 0 | r = opal_exclusive_lock(cd, crypt_data_device(cd), &opal_lh); |
4308 | 0 | if (r < 0) { |
4309 | 0 | log_err(cd, _("Failed to acquire OPAL lock on device %s."), device_path(crypt_data_device(cd))); |
4310 | 0 | goto out; |
4311 | 0 | } |
4312 | 0 | } |
4313 | | |
4314 | 0 | if (dm_opal_uuid && (!crypt_data_device(cd) || opal_lock(cd, crypt_data_device(cd), opal_segment_number))) |
4315 | 0 | log_err(cd, _("Device %s was suspended but hardware OPAL device cannot be locked."), name); |
4316 | 0 | out: |
4317 | 0 | opal_exclusive_unlock(cd, opal_lh); |
4318 | 0 | free(iname); |
4319 | 0 | dm_targets_free(cd, &dmd); |
4320 | 0 | dm_targets_free(cd, &dmdi); |
4321 | 0 | free(CONST_CAST(void*)dmd.uuid); |
4322 | 0 | free(CONST_CAST(void*)dmdi.uuid); |
4323 | 0 | return r; |
4324 | 0 | } |
4325 | | |
4326 | | static int resume_luks1_by_volume_key(struct crypt_device *cd, |
4327 | | struct volume_key *vk, |
4328 | | const char *name) |
4329 | 0 | { |
4330 | 0 | int r; |
4331 | 0 | struct volume_key *zerokey = NULL; |
4332 | |
|
4333 | 0 | assert(vk && crypt_volume_key_get_id(vk) == 0); |
4334 | 0 | assert(name); |
4335 | |
|
4336 | 0 | if (crypt_is_cipher_null(crypt_get_cipher_spec(cd))) { |
4337 | 0 | zerokey = crypt_alloc_volume_key(0, NULL); |
4338 | 0 | if (!zerokey) |
4339 | 0 | return -ENOMEM; |
4340 | 0 | vk = zerokey; |
4341 | 0 | } |
4342 | | |
4343 | 0 | r = dm_resume_and_reinstate_key(cd, name, vk); |
4344 | |
|
4345 | 0 | if (r == -ENOTSUP) |
4346 | 0 | log_err(cd, _("Resume is not supported for device %s."), name); |
4347 | 0 | else if (r) |
4348 | 0 | log_err(cd, _("Error during resuming device %s."), name); |
4349 | |
|
4350 | 0 | crypt_free_volume_key(zerokey); |
4351 | |
|
4352 | 0 | return r; |
4353 | 0 | } |
4354 | | |
4355 | | static bool unlink_key_from_keyring(struct crypt_device *cd, key_serial_t kid, key_serial_t keyring_id) |
4356 | 0 | { |
4357 | 0 | log_dbg(cd, "Unlinking volume key (id: %" PRIi32 ") from kernel keyring (id: %" PRIi32 ").", |
4358 | 0 | kid, keyring_id); |
4359 | |
|
4360 | 0 | if (!keyring_unlink_key_from_keyring(kid, keyring_id)) |
4361 | 0 | return true; |
4362 | | |
4363 | 0 | log_dbg(cd, "keyring_unlink_key_from_keyring failed with errno %d.", errno); |
4364 | |
|
4365 | 0 | return false; |
4366 | 0 | } |
4367 | | |
4368 | | /* internal only */ |
4369 | | void crypt_unlink_key_from_keyring(struct crypt_device *cd, |
4370 | | key_serial_t key_id) |
4371 | 0 | { |
4372 | 0 | (void)unlink_key_from_keyring(cd, key_id, cd->keyring_id); |
4373 | 0 | } |
4374 | | |
4375 | | static void crypt_unlink_key_from_custom_keyring(struct crypt_device *cd, key_serial_t kid) |
4376 | 0 | { |
4377 | 0 | assert(cd); |
4378 | 0 | assert(cd->keyring_to_link_vk); |
4379 | | |
4380 | |
|
4381 | 0 | if (unlink_key_from_keyring(cd, kid, cd->keyring_to_link_vk)) |
4382 | 0 | return; |
4383 | | |
4384 | 0 | log_err(cd, _("Failed to unlink volume key from user specified keyring.")); |
4385 | 0 | } |
4386 | | |
4387 | | static key_serial_t crypt_single_volume_key_load_in_custom_keyring(struct crypt_device *cd, |
4388 | | struct volume_key *vk, |
4389 | | const char *user_key_name) |
4390 | 0 | { |
4391 | 0 | key_serial_t kid; |
4392 | 0 | const char *type_name; |
4393 | |
|
4394 | 0 | assert(cd); |
4395 | 0 | assert(cd->link_vk_to_keyring); |
4396 | |
|
4397 | 0 | if (!vk || !(type_name = key_type_name(cd->keyring_key_type))) |
4398 | 0 | return -EINVAL; |
4399 | | |
4400 | 0 | log_dbg(cd, "Linking volume key (type %s, name %s) to the specified keyring", |
4401 | 0 | type_name, user_key_name); |
4402 | |
|
4403 | 0 | kid = keyring_add_key_to_keyring(cd->keyring_key_type, user_key_name, |
4404 | 0 | crypt_volume_key_get_key(vk), |
4405 | 0 | crypt_volume_key_length(vk), |
4406 | 0 | cd->keyring_to_link_vk); |
4407 | 0 | if (kid <= 0) |
4408 | 0 | log_dbg(cd, "The keyring_add_key_to_keyring function failed (error %d).", errno); |
4409 | |
|
4410 | 0 | return kid; |
4411 | 0 | } |
4412 | | |
4413 | | static int crypt_volume_key_load_in_custom_keyring(struct crypt_device *cd, |
4414 | | struct volume_key *vk, |
4415 | | key_serial_t *kid1_out, |
4416 | | key_serial_t *kid2_out) |
4417 | 0 | { |
4418 | 0 | key_serial_t kid1, kid2 = 0; |
4419 | |
|
4420 | 0 | assert(cd); |
4421 | 0 | assert(cd->link_vk_to_keyring); |
4422 | 0 | assert(cd->user_key_name1); |
4423 | |
|
4424 | 0 | if (!vk || !key_type_name(cd->keyring_key_type)) |
4425 | 0 | return -EINVAL; |
4426 | | |
4427 | 0 | kid1 = crypt_single_volume_key_load_in_custom_keyring(cd, vk, cd->user_key_name1); |
4428 | 0 | if (kid1 <= 0) |
4429 | 0 | return -EINVAL; |
4430 | | |
4431 | 0 | vk = crypt_volume_key_next(vk); |
4432 | 0 | if (vk) { |
4433 | 0 | assert(cd->user_key_name2); |
4434 | 0 | kid2 = crypt_single_volume_key_load_in_custom_keyring(cd, vk, cd->user_key_name2); |
4435 | 0 | if (kid2 <= 0) { |
4436 | 0 | crypt_unlink_key_from_custom_keyring(cd, kid1); |
4437 | 0 | return -EINVAL; |
4438 | 0 | } |
4439 | 0 | } |
4440 | | |
4441 | 0 | *kid2_out = kid2; |
4442 | 0 | *kid1_out = kid1; |
4443 | 0 | return 0; |
4444 | 0 | } |
4445 | | |
4446 | | static int resume_luks2_by_volume_key(struct crypt_device *cd, |
4447 | | int digest, |
4448 | | struct volume_key *vk, |
4449 | | const char *name) |
4450 | 0 | { |
4451 | 0 | bool use_keyring; |
4452 | 0 | int r, enc_type; |
4453 | 0 | uint32_t opal_segment_number; |
4454 | 0 | struct volume_key *p_crypt = vk, *p_opal = NULL, *zerokey = NULL, *crypt_key = NULL, *opal_key = NULL; |
4455 | 0 | char *iname = NULL; |
4456 | 0 | struct crypt_lock_handle *opal_lh = NULL; |
4457 | 0 | key_serial_t kid1 = 0, kid2 = 0; |
4458 | |
|
4459 | 0 | assert(digest >= 0); |
4460 | 0 | assert(vk && crypt_volume_key_get_id(vk) == digest); |
4461 | 0 | assert(name); |
4462 | |
|
4463 | 0 | enc_type = crypt_get_hw_encryption_type(cd); |
4464 | 0 | if (enc_type < 0) |
4465 | 0 | return enc_type; |
4466 | | |
4467 | 0 | use_keyring = crypt_use_keyring_for_vk(cd); |
4468 | |
|
4469 | 0 | if (enc_type == CRYPT_OPAL_HW_ONLY || enc_type == CRYPT_SW_AND_OPAL_HW) { |
4470 | 0 | r = LUKS2_get_opal_segment_number(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, |
4471 | 0 | &opal_segment_number); |
4472 | 0 | if (r < 0) |
4473 | 0 | return r; |
4474 | | |
4475 | 0 | r = LUKS2_split_crypt_and_opal_keys(cd, &cd->u.luks2.hdr, |
4476 | 0 | vk, &crypt_key, |
4477 | 0 | &opal_key); |
4478 | 0 | if (r < 0) |
4479 | 0 | return r; |
4480 | | |
4481 | 0 | p_crypt = crypt_key; |
4482 | 0 | p_opal = opal_key ?: vk; |
4483 | 0 | } |
4484 | | |
4485 | 0 | if (enc_type != CRYPT_OPAL_HW_ONLY && crypt_is_cipher_null(crypt_get_cipher_spec(cd))) { |
4486 | 0 | zerokey = crypt_alloc_volume_key(0, NULL); |
4487 | 0 | if (!zerokey) { |
4488 | 0 | r = -ENOMEM; |
4489 | 0 | goto out; |
4490 | 0 | } |
4491 | 0 | p_crypt = zerokey; |
4492 | 0 | use_keyring = false; |
4493 | 0 | } |
4494 | | |
4495 | 0 | if (use_keyring) { |
4496 | 0 | if (p_crypt) { |
4497 | 0 | r = LUKS2_volume_key_load_in_keyring_by_digest(cd, p_crypt, digest); |
4498 | 0 | if (r < 0) |
4499 | 0 | goto out; |
4500 | 0 | } |
4501 | | |
4502 | | /* upload volume key in custom keyring if requested */ |
4503 | 0 | if (cd->link_vk_to_keyring) { |
4504 | 0 | r = crypt_volume_key_load_in_custom_keyring(cd, vk, &kid1, &kid2); |
4505 | 0 | if (r < 0) { |
4506 | 0 | log_err(cd, _("Failed to link volume key in user defined keyring.")); |
4507 | 0 | goto out; |
4508 | 0 | } |
4509 | 0 | } |
4510 | 0 | } |
4511 | | |
4512 | 0 | if (p_opal) { |
4513 | 0 | r = opal_exclusive_lock(cd, crypt_data_device(cd), &opal_lh); |
4514 | 0 | if (r < 0) { |
4515 | 0 | log_err(cd, _("Failed to acquire OPAL lock on device %s."), device_path(crypt_data_device(cd))); |
4516 | 0 | goto out; |
4517 | 0 | } |
4518 | | |
4519 | 0 | r = opal_unlock(cd, crypt_data_device(cd), opal_segment_number, p_opal); |
4520 | 0 | if (r < 0) { |
4521 | 0 | p_opal = NULL; /* do not lock on error path */ |
4522 | 0 | goto out; |
4523 | 0 | } |
4524 | 0 | } |
4525 | | |
4526 | 0 | if (crypt_get_integrity_tag_size(cd) && |
4527 | 0 | (iname = dm_get_active_iname(cd, name))) { |
4528 | 0 | r = dm_resume_device(cd, iname, 0); |
4529 | 0 | if (r) |
4530 | 0 | log_err(cd, _("Error during resuming device %s."), iname); |
4531 | 0 | free(iname); |
4532 | 0 | } |
4533 | |
|
4534 | 0 | if (enc_type == CRYPT_OPAL_HW_ONLY) |
4535 | 0 | r = dm_resume_device(cd, name, 0); |
4536 | 0 | else |
4537 | 0 | r = dm_resume_and_reinstate_key(cd, name, p_crypt); |
4538 | |
|
4539 | 0 | if (r == -ENOTSUP) |
4540 | 0 | log_err(cd, _("Resume is not supported for device %s."), name); |
4541 | 0 | else if (r) |
4542 | 0 | log_err(cd, _("Error during resuming device %s."), name); |
4543 | |
|
4544 | 0 | out: |
4545 | 0 | if (r < 0) { |
4546 | 0 | crypt_drop_uploaded_keyring_key(cd, p_crypt); |
4547 | 0 | if (cd->link_vk_to_keyring && kid1) |
4548 | 0 | crypt_unlink_key_from_custom_keyring(cd, kid1); |
4549 | 0 | if (cd->link_vk_to_keyring && kid2) |
4550 | 0 | crypt_unlink_key_from_custom_keyring(cd, kid2); |
4551 | 0 | } |
4552 | |
|
4553 | 0 | if (r < 0 && p_opal) |
4554 | 0 | opal_lock(cd, crypt_data_device(cd), opal_segment_number); |
4555 | |
|
4556 | 0 | opal_exclusive_unlock(cd, opal_lh); |
4557 | 0 | crypt_free_volume_key(zerokey); |
4558 | 0 | crypt_free_volume_key(opal_key); |
4559 | 0 | crypt_free_volume_key(crypt_key); |
4560 | |
|
4561 | 0 | return r; |
4562 | 0 | } |
4563 | | |
4564 | | /* key must be properly verified */ |
4565 | | static int resume_by_volume_key(struct crypt_device *cd, |
4566 | | struct volume_key *vk, |
4567 | | const char *name) |
4568 | 0 | { |
4569 | 0 | assert(cd); |
4570 | |
|
4571 | 0 | if (isLUKS2(cd->type)) |
4572 | 0 | return resume_luks2_by_volume_key(cd, |
4573 | 0 | LUKS2_digest_by_segment(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT), |
4574 | 0 | vk, name); |
4575 | | |
4576 | 0 | if (isLUKS1(cd->type)) |
4577 | 0 | return resume_luks1_by_volume_key(cd, vk, name); |
4578 | | |
4579 | 0 | return -EINVAL; |
4580 | 0 | } |
4581 | | |
4582 | | int crypt_resume_by_keyslot_context(struct crypt_device *cd, |
4583 | | const char *name, |
4584 | | int keyslot, |
4585 | | struct crypt_keyslot_context *kc) |
4586 | 0 | { |
4587 | 0 | int r; |
4588 | 0 | struct volume_key *vk = NULL; |
4589 | 0 | int unlocked_keyslot = -EINVAL; |
4590 | 0 | struct crypt_dm_active_device dmd = {}; |
4591 | |
|
4592 | 0 | if (!name) |
4593 | 0 | return -EINVAL; |
4594 | | |
4595 | 0 | log_dbg(cd, "Resuming volume %s [keyslot %d] using %s.", name, keyslot, keyslot_context_type_string(kc)); |
4596 | |
|
4597 | 0 | if ((r = onlyLUKS(cd))) |
4598 | 0 | return r; |
4599 | | |
4600 | 0 | r = dm_status_suspended(cd, name); |
4601 | 0 | if (r < 0) |
4602 | 0 | return r; |
4603 | | |
4604 | 0 | if (!r) { |
4605 | 0 | log_err(cd, _("Volume %s is not suspended."), name); |
4606 | 0 | return -EINVAL; |
4607 | 0 | } |
4608 | | |
4609 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd); |
4610 | 0 | if (r < 0) |
4611 | 0 | return r; |
4612 | | |
4613 | 0 | r = dm_uuid_cmp(dmd.uuid, LUKS_UUID(cd)); |
4614 | 0 | if (r < 0) { |
4615 | 0 | log_dbg(cd, "LUKS device header uuid: %s mismatches DM returned uuid %s", |
4616 | 0 | LUKS_UUID(cd), dmd.uuid); |
4617 | 0 | goto out; |
4618 | 0 | } |
4619 | | |
4620 | 0 | if (isLUKS1(cd->type) && kc->get_luks1_volume_key) |
4621 | 0 | r = kc->get_luks1_volume_key(cd, kc, keyslot, &vk); |
4622 | 0 | else if (isLUKS2(cd->type) && kc->get_luks2_volume_key) |
4623 | 0 | r = kc->get_luks2_volume_key(cd, kc, keyslot, &vk); |
4624 | 0 | else |
4625 | 0 | r = -EINVAL; |
4626 | 0 | if (r < 0) |
4627 | 0 | goto out; |
4628 | 0 | unlocked_keyslot = r; |
4629 | |
|
4630 | 0 | if (isLUKS1(cd->type)) { |
4631 | 0 | r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk); |
4632 | 0 | crypt_volume_key_set_id(vk, 0); |
4633 | 0 | } else if (isLUKS2(cd->type)) { |
4634 | 0 | r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk); |
4635 | 0 | crypt_volume_key_set_id(vk, r); |
4636 | 0 | } else |
4637 | 0 | r = -EINVAL; |
4638 | 0 | if (r < 0) |
4639 | 0 | goto out; |
4640 | | |
4641 | 0 | r = resume_by_volume_key(cd, vk, name); |
4642 | 0 | out: |
4643 | 0 | crypt_free_volume_key(vk); |
4644 | 0 | free(CONST_CAST(void*)dmd.uuid); |
4645 | |
|
4646 | 0 | return r < 0 ? r : unlocked_keyslot; |
4647 | 0 | } |
4648 | | |
4649 | | int crypt_resume_by_passphrase(struct crypt_device *cd, |
4650 | | const char *name, |
4651 | | int keyslot, |
4652 | | const char *passphrase, |
4653 | | size_t passphrase_size) |
4654 | 0 | { |
4655 | 0 | int r; |
4656 | 0 | struct crypt_keyslot_context kc = {}; |
4657 | |
|
4658 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size); |
4659 | 0 | r = crypt_resume_by_keyslot_context(cd, name, keyslot, &kc); |
4660 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
4661 | |
|
4662 | 0 | return r; |
4663 | 0 | } |
4664 | | |
4665 | | int crypt_resume_by_keyfile_device_offset(struct crypt_device *cd, |
4666 | | const char *name, |
4667 | | int keyslot, |
4668 | | const char *keyfile, |
4669 | | size_t keyfile_size, |
4670 | | uint64_t keyfile_offset) |
4671 | 0 | { |
4672 | 0 | int r; |
4673 | 0 | struct crypt_keyslot_context kc = {}; |
4674 | |
|
4675 | 0 | crypt_keyslot_context_init_by_keyfile_internal(&kc, keyfile, keyfile_size, keyfile_offset); |
4676 | 0 | r = crypt_resume_by_keyslot_context(cd, name, keyslot, &kc); |
4677 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
4678 | |
|
4679 | 0 | return r; |
4680 | 0 | } |
4681 | | |
4682 | | int crypt_resume_by_keyfile(struct crypt_device *cd, |
4683 | | const char *name, |
4684 | | int keyslot, |
4685 | | const char *keyfile, |
4686 | | size_t keyfile_size) |
4687 | 0 | { |
4688 | 0 | return crypt_resume_by_keyfile_device_offset(cd, name, keyslot, |
4689 | 0 | keyfile, keyfile_size, 0); |
4690 | 0 | } |
4691 | | |
4692 | | int crypt_resume_by_keyfile_offset(struct crypt_device *cd, |
4693 | | const char *name, |
4694 | | int keyslot, |
4695 | | const char *keyfile, |
4696 | | size_t keyfile_size, |
4697 | | size_t keyfile_offset) |
4698 | 0 | { |
4699 | 0 | return crypt_resume_by_keyfile_device_offset(cd, name, keyslot, |
4700 | 0 | keyfile, keyfile_size, keyfile_offset); |
4701 | 0 | } |
4702 | | |
4703 | | int crypt_resume_by_volume_key(struct crypt_device *cd, |
4704 | | const char *name, |
4705 | | const char *volume_key, |
4706 | | size_t volume_key_size) |
4707 | 0 | { |
4708 | 0 | int r; |
4709 | 0 | struct crypt_keyslot_context kc = {}; |
4710 | |
|
4711 | 0 | crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size); |
4712 | 0 | r = crypt_resume_by_keyslot_context(cd, name, CRYPT_ANY_SLOT /* unused */, &kc); |
4713 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
4714 | |
|
4715 | 0 | if (r == -EPERM || r == -ENOENT) |
4716 | 0 | log_err(cd, _("Volume key does not match the volume.")); |
4717 | |
|
4718 | 0 | return r; |
4719 | 0 | } |
4720 | | |
4721 | | int crypt_resume_by_token_pin(struct crypt_device *cd, const char *name, |
4722 | | const char *type, int token, const char *pin, size_t pin_size, |
4723 | | void *usrptr) |
4724 | 0 | { |
4725 | 0 | int r; |
4726 | 0 | struct crypt_keyslot_context kc = {}; |
4727 | |
|
4728 | 0 | crypt_keyslot_context_init_by_token_internal(&kc, token, type, pin, pin_size, usrptr); |
4729 | 0 | r = crypt_resume_by_keyslot_context(cd, name, CRYPT_ANY_SLOT, &kc); |
4730 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
4731 | |
|
4732 | 0 | return r; |
4733 | 0 | } |
4734 | | |
4735 | | /* |
4736 | | * Keyslot manipulation |
4737 | | */ |
4738 | | int crypt_keyslot_add_by_passphrase(struct crypt_device *cd, |
4739 | | int keyslot, // -1 any |
4740 | | const char *passphrase, |
4741 | | size_t passphrase_size, |
4742 | | const char *new_passphrase, |
4743 | | size_t new_passphrase_size) |
4744 | 0 | { |
4745 | 0 | int r; |
4746 | 0 | struct crypt_keyslot_context kc = {}, new_kc = {}; |
4747 | |
|
4748 | 0 | if (!passphrase || !new_passphrase) |
4749 | 0 | return -EINVAL; |
4750 | | |
4751 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size); |
4752 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&new_kc, new_passphrase, new_passphrase_size); |
4753 | |
|
4754 | 0 | r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0); |
4755 | |
|
4756 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
4757 | 0 | crypt_keyslot_context_destroy_internal(&new_kc); |
4758 | |
|
4759 | 0 | return r; |
4760 | 0 | } |
4761 | | |
4762 | | int crypt_keyslot_change_by_passphrase(struct crypt_device *cd, |
4763 | | int keyslot_old, |
4764 | | int keyslot_new, |
4765 | | const char *passphrase, |
4766 | | size_t passphrase_size, |
4767 | | const char *new_passphrase, |
4768 | | size_t new_passphrase_size) |
4769 | 0 | { |
4770 | 0 | bool keyslot_swap = false; |
4771 | 0 | int digest = -1, r; |
4772 | 0 | struct luks2_keyslot_params params; |
4773 | 0 | struct volume_key *vk = NULL; |
4774 | |
|
4775 | 0 | if (!passphrase || !new_passphrase) |
4776 | 0 | return -EINVAL; |
4777 | | |
4778 | 0 | log_dbg(cd, "Changing passphrase from old keyslot %d to new %d.", |
4779 | 0 | keyslot_old, keyslot_new); |
4780 | |
|
4781 | 0 | if ((r = onlyLUKS(cd))) |
4782 | 0 | return r; |
4783 | | |
4784 | 0 | if (isLUKS1(cd->type)) |
4785 | 0 | r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size, |
4786 | 0 | &cd->u.luks1.hdr, &vk, cd); |
4787 | 0 | else if (isLUKS2(cd->type)) { |
4788 | 0 | r = LUKS2_keyslot_open(cd, keyslot_old, CRYPT_ANY_SEGMENT, passphrase, passphrase_size, &vk); |
4789 | | /* will fail for keyslots w/o digest. fix if supported in a future */ |
4790 | 0 | if (r >= 0) { |
4791 | 0 | digest = LUKS2_digest_by_keyslot(&cd->u.luks2.hdr, r); |
4792 | 0 | if (digest < 0) |
4793 | 0 | r = -EINVAL; |
4794 | 0 | } |
4795 | 0 | } else |
4796 | 0 | r = -EINVAL; |
4797 | 0 | if (r < 0) |
4798 | 0 | goto out; |
4799 | | |
4800 | 0 | if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) { |
4801 | 0 | log_dbg(cd, "Keyslot mismatch."); |
4802 | 0 | goto out; |
4803 | 0 | } |
4804 | 0 | keyslot_old = r; |
4805 | |
|
4806 | 0 | if (isLUKS1(cd->type)) { |
4807 | 0 | if (keyslot_new == CRYPT_ANY_SLOT) { |
4808 | 0 | keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr); |
4809 | 0 | if (keyslot_new < 0) |
4810 | 0 | keyslot_new = keyslot_old; |
4811 | 0 | } |
4812 | 0 | } else if (isLUKS2(cd->type)) { |
4813 | | /* If there is a free keyslot (both id and binary area) avoid in-place keyslot area overwrite */ |
4814 | 0 | if (keyslot_new == CRYPT_ANY_SLOT || keyslot_new == keyslot_old) { |
4815 | 0 | keyslot_new = LUKS2_keyslot_find_empty(cd, &cd->u.luks2.hdr, crypt_volume_key_length(vk)); |
4816 | 0 | if (keyslot_new < 0) |
4817 | 0 | keyslot_new = keyslot_old; |
4818 | 0 | else |
4819 | 0 | keyslot_swap = true; |
4820 | 0 | } |
4821 | 0 | } |
4822 | 0 | log_dbg(cd, "Key change, old slot %d, new slot %d.", keyslot_old, keyslot_new); |
4823 | |
|
4824 | 0 | if (isLUKS1(cd->type)) { |
4825 | 0 | if (keyslot_old == keyslot_new) { |
4826 | 0 | log_dbg(cd, "Key slot %d is going to be overwritten.", keyslot_old); |
4827 | 0 | (void)crypt_keyslot_destroy(cd, keyslot_old); |
4828 | 0 | } |
4829 | 0 | r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size, |
4830 | 0 | &cd->u.luks1.hdr, vk, cd); |
4831 | 0 | } else if (isLUKS2(cd->type)) { |
4832 | 0 | r = LUKS2_keyslot_params_default(cd, &cd->u.luks2.hdr, ¶ms); |
4833 | 0 | if (r) |
4834 | 0 | goto out; |
4835 | | |
4836 | 0 | if (keyslot_old != keyslot_new) { |
4837 | 0 | r = LUKS2_digest_assign(cd, &cd->u.luks2.hdr, keyslot_new, digest, 1, 0); |
4838 | 0 | if (r < 0) |
4839 | 0 | goto out; |
4840 | 0 | r = LUKS2_token_assignment_copy(cd, &cd->u.luks2.hdr, keyslot_old, keyslot_new, 0); |
4841 | 0 | if (r < 0) |
4842 | 0 | goto out; |
4843 | 0 | } else |
4844 | 0 | log_dbg(cd, "Key slot %d is going to be overwritten.", keyslot_old); |
4845 | | |
4846 | 0 | r = LUKS2_keyslot_store(cd, &cd->u.luks2.hdr, |
4847 | 0 | keyslot_new, new_passphrase, |
4848 | 0 | new_passphrase_size, vk, ¶ms); |
4849 | 0 | if (r < 0) |
4850 | 0 | goto out; |
4851 | | |
4852 | | /* Swap old & new so the final keyslot number remains */ |
4853 | 0 | if (keyslot_swap && keyslot_old != keyslot_new) { |
4854 | 0 | r = LUKS2_keyslot_swap(cd, &cd->u.luks2.hdr, keyslot_old, keyslot_new); |
4855 | 0 | if (r < 0) |
4856 | 0 | goto out; |
4857 | | |
4858 | | /* Swap slot id */ |
4859 | 0 | r = keyslot_old; |
4860 | 0 | keyslot_old = keyslot_new; |
4861 | 0 | keyslot_new = r; |
4862 | 0 | } |
4863 | 0 | } else |
4864 | 0 | r = -EINVAL; |
4865 | | |
4866 | 0 | if (r >= 0 && keyslot_old != keyslot_new) |
4867 | 0 | r = crypt_keyslot_destroy(cd, keyslot_old); |
4868 | |
|
4869 | 0 | if (r < 0) |
4870 | 0 | log_err(cd, _("Failed to swap new key slot.")); |
4871 | 0 | out: |
4872 | 0 | crypt_free_volume_key(vk); |
4873 | 0 | if (r < 0) { |
4874 | 0 | _luks2_rollback(cd); |
4875 | 0 | return r; |
4876 | 0 | } |
4877 | 0 | return keyslot_new; |
4878 | 0 | } |
4879 | | |
4880 | | int crypt_keyslot_add_by_keyfile_device_offset(struct crypt_device *cd, |
4881 | | int keyslot, |
4882 | | const char *keyfile, |
4883 | | size_t keyfile_size, |
4884 | | uint64_t keyfile_offset, |
4885 | | const char *new_keyfile, |
4886 | | size_t new_keyfile_size, |
4887 | | uint64_t new_keyfile_offset) |
4888 | 0 | { |
4889 | 0 | int r; |
4890 | 0 | struct crypt_keyslot_context kc = {}, new_kc = {}; |
4891 | |
|
4892 | 0 | if (!keyfile || !new_keyfile) |
4893 | 0 | return -EINVAL; |
4894 | | |
4895 | 0 | crypt_keyslot_context_init_by_keyfile_internal(&kc, keyfile, keyfile_size, keyfile_offset); |
4896 | 0 | crypt_keyslot_context_init_by_keyfile_internal(&new_kc, new_keyfile, new_keyfile_size, new_keyfile_offset); |
4897 | |
|
4898 | 0 | r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0); |
4899 | |
|
4900 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
4901 | 0 | crypt_keyslot_context_destroy_internal(&new_kc); |
4902 | |
|
4903 | 0 | return r; |
4904 | 0 | } |
4905 | | |
4906 | | int crypt_keyslot_add_by_keyfile(struct crypt_device *cd, |
4907 | | int keyslot, |
4908 | | const char *keyfile, |
4909 | | size_t keyfile_size, |
4910 | | const char *new_keyfile, |
4911 | | size_t new_keyfile_size) |
4912 | 0 | { |
4913 | 0 | return crypt_keyslot_add_by_keyfile_device_offset(cd, keyslot, |
4914 | 0 | keyfile, keyfile_size, 0, |
4915 | 0 | new_keyfile, new_keyfile_size, 0); |
4916 | 0 | } |
4917 | | |
4918 | | int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd, |
4919 | | int keyslot, |
4920 | | const char *keyfile, |
4921 | | size_t keyfile_size, |
4922 | | size_t keyfile_offset, |
4923 | | const char *new_keyfile, |
4924 | | size_t new_keyfile_size, |
4925 | | size_t new_keyfile_offset) |
4926 | 0 | { |
4927 | 0 | return crypt_keyslot_add_by_keyfile_device_offset(cd, keyslot, |
4928 | 0 | keyfile, keyfile_size, keyfile_offset, |
4929 | 0 | new_keyfile, new_keyfile_size, new_keyfile_offset); |
4930 | 0 | } |
4931 | | |
4932 | | int crypt_keyslot_add_by_volume_key(struct crypt_device *cd, |
4933 | | int keyslot, |
4934 | | const char *volume_key, |
4935 | | size_t volume_key_size, |
4936 | | const char *passphrase, |
4937 | | size_t passphrase_size) |
4938 | 0 | { |
4939 | 0 | int r; |
4940 | 0 | struct crypt_keyslot_context kc = {}, new_kc = {}; |
4941 | |
|
4942 | 0 | if (!passphrase) |
4943 | 0 | return -EINVAL; |
4944 | | |
4945 | 0 | crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size); |
4946 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&new_kc, passphrase, passphrase_size); |
4947 | |
|
4948 | 0 | r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0); |
4949 | |
|
4950 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
4951 | 0 | crypt_keyslot_context_destroy_internal(&new_kc); |
4952 | |
|
4953 | 0 | return r; |
4954 | 0 | } |
4955 | | |
4956 | | int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot) |
4957 | 0 | { |
4958 | 0 | crypt_keyslot_info ki; |
4959 | 0 | int r; |
4960 | |
|
4961 | 0 | log_dbg(cd, "Destroying keyslot %d.", keyslot); |
4962 | |
|
4963 | 0 | if ((r = onlyLUKSunrestricted(cd))) |
4964 | 0 | return r; |
4965 | | |
4966 | 0 | ki = crypt_keyslot_status(cd, keyslot); |
4967 | 0 | if (ki == CRYPT_SLOT_INVALID) { |
4968 | 0 | log_err(cd, _("Key slot %d is invalid."), keyslot); |
4969 | 0 | return -EINVAL; |
4970 | 0 | } |
4971 | | |
4972 | 0 | if (isLUKS1(cd->type)) { |
4973 | 0 | if (ki == CRYPT_SLOT_INACTIVE) { |
4974 | 0 | log_err(cd, _("Keyslot %d is not active."), keyslot); |
4975 | 0 | return -EINVAL; |
4976 | 0 | } |
4977 | 0 | return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd); |
4978 | 0 | } |
4979 | | |
4980 | 0 | return LUKS2_keyslot_wipe(cd, &cd->u.luks2.hdr, keyslot); |
4981 | 0 | } |
4982 | | |
4983 | | static int _check_header_data_overlap(struct crypt_device *cd, const char *name) |
4984 | 0 | { |
4985 | 0 | if (!name || !isLUKS(cd->type)) |
4986 | 0 | return 0; |
4987 | | |
4988 | 0 | if (device_is_identical(crypt_data_device(cd), crypt_metadata_device(cd)) <= 0) |
4989 | 0 | return 0; |
4990 | | |
4991 | | /* FIXME: check real header size */ |
4992 | 0 | if (crypt_get_data_offset(cd) == 0) { |
4993 | 0 | log_err(cd, _("Device header overlaps with data area.")); |
4994 | 0 | return -EINVAL; |
4995 | 0 | } |
4996 | | |
4997 | 0 | return 0; |
4998 | 0 | } |
4999 | | |
5000 | | static int check_devices(struct crypt_device *cd, const char *name, const char *iname, uint32_t *flags) |
5001 | 0 | { |
5002 | 0 | int r; |
5003 | |
|
5004 | 0 | if (!flags || !name) |
5005 | 0 | return -EINVAL; |
5006 | | |
5007 | 0 | if (iname) { |
5008 | 0 | r = dm_status_device(cd, iname); |
5009 | 0 | if (r >= 0 && !(*flags & CRYPT_ACTIVATE_REFRESH)) |
5010 | 0 | return -EBUSY; |
5011 | 0 | if (r < 0 && r != -ENODEV) |
5012 | 0 | return r; |
5013 | 0 | if (r == -ENODEV) |
5014 | 0 | *flags &= ~CRYPT_ACTIVATE_REFRESH; |
5015 | 0 | } |
5016 | | |
5017 | 0 | r = dm_status_device(cd, name); |
5018 | 0 | if (r >= 0 && !(*flags & CRYPT_ACTIVATE_REFRESH)) |
5019 | 0 | return -EBUSY; |
5020 | 0 | if (r < 0 && r != -ENODEV) |
5021 | 0 | return r; |
5022 | 0 | if (r == -ENODEV) |
5023 | 0 | *flags &= ~CRYPT_ACTIVATE_REFRESH; |
5024 | |
|
5025 | 0 | return 0; |
5026 | 0 | } |
5027 | | |
5028 | | static int _create_device_with_integrity(struct crypt_device *cd, |
5029 | | const char *type, const char *name, const char *iname, |
5030 | | const char *ipath, struct crypt_dm_active_device *dmd, |
5031 | | struct crypt_dm_active_device *dmdi) |
5032 | 0 | { |
5033 | 0 | int r; |
5034 | 0 | enum devcheck device_check; |
5035 | 0 | struct dm_target *tgt; |
5036 | 0 | struct device *device = NULL; |
5037 | |
|
5038 | 0 | if (!single_segment(dmd)) |
5039 | 0 | return -EINVAL; |
5040 | | |
5041 | 0 | tgt = &dmd->segment; |
5042 | 0 | if (tgt->type != DM_CRYPT) |
5043 | 0 | return -EINVAL; |
5044 | | |
5045 | 0 | device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL; |
5046 | |
|
5047 | 0 | r = INTEGRITY_activate_dmd_device(cd, iname, CRYPT_SUBDEV, dmdi, 0); |
5048 | 0 | if (r) |
5049 | 0 | return r; |
5050 | | |
5051 | 0 | r = device_alloc(cd, &device, ipath); |
5052 | 0 | if (r < 0) |
5053 | 0 | goto out; |
5054 | 0 | tgt->data_device = device; |
5055 | |
|
5056 | 0 | r = device_block_adjust(cd, tgt->data_device, device_check, |
5057 | 0 | tgt->u.crypt.offset, &dmd->size, &dmd->flags); |
5058 | |
|
5059 | 0 | if (!r) |
5060 | 0 | r = dm_create_device(cd, name, type, dmd); |
5061 | 0 | out: |
5062 | 0 | if (r < 0) |
5063 | 0 | dm_remove_device(cd, iname, 0); |
5064 | |
|
5065 | 0 | device_free(cd, device); |
5066 | 0 | return r; |
5067 | 0 | } |
5068 | | |
5069 | | static int kernel_keyring_support(void) |
5070 | 0 | { |
5071 | 0 | static unsigned _checked = 0; |
5072 | |
|
5073 | 0 | if (!_checked) { |
5074 | 0 | _kernel_keyring_supported = keyring_check(); |
5075 | 0 | _checked = 1; |
5076 | 0 | } |
5077 | |
|
5078 | 0 | return _kernel_keyring_supported; |
5079 | 0 | } |
5080 | | |
5081 | | static int dmcrypt_keyring_bug(void) |
5082 | 0 | { |
5083 | 0 | uint64_t kversion; |
5084 | |
|
5085 | 0 | if (kernel_version(&kversion)) |
5086 | 0 | return 1; |
5087 | 0 | return kversion < compact_version(4,15,0,0); |
5088 | 0 | } |
5089 | | |
5090 | | int create_or_reload_device(struct crypt_device *cd, const char *name, |
5091 | | const char *type, struct crypt_dm_active_device *dmd) |
5092 | 0 | { |
5093 | 0 | int r; |
5094 | 0 | enum devcheck device_check; |
5095 | 0 | struct dm_target *tgt; |
5096 | 0 | uint64_t offset, dmflags = 0; |
5097 | |
|
5098 | 0 | if (!type || !name || !single_segment(dmd)) |
5099 | 0 | return -EINVAL; |
5100 | | |
5101 | 0 | tgt = &dmd->segment; |
5102 | 0 | if (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY && tgt->type != DM_LINEAR) |
5103 | 0 | return -EINVAL; |
5104 | | |
5105 | | /* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */ |
5106 | 0 | r = check_devices(cd, name, NULL, &dmd->flags); |
5107 | 0 | if (r) |
5108 | 0 | return r; |
5109 | | |
5110 | 0 | if (dmd->flags & CRYPT_ACTIVATE_REFRESH) { |
5111 | | /* Refresh and recalculate means increasing dm-integrity device */ |
5112 | 0 | if (tgt->type == DM_INTEGRITY && dmd->flags & CRYPT_ACTIVATE_RECALCULATE) |
5113 | 0 | dmflags = DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH; |
5114 | 0 | r = _reload_device(cd, name, dmd, dmflags); |
5115 | 0 | } else { |
5116 | 0 | if (tgt->type == DM_CRYPT || tgt->type == DM_LINEAR) { |
5117 | 0 | device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL; |
5118 | 0 | offset = tgt->type == DM_CRYPT ? tgt->u.crypt.offset : tgt->u.linear.offset; |
5119 | |
|
5120 | 0 | r = device_block_adjust(cd, tgt->data_device, device_check, |
5121 | 0 | offset, &dmd->size, &dmd->flags); |
5122 | 0 | if (!r) { |
5123 | 0 | tgt->size = dmd->size; |
5124 | 0 | r = dm_create_device(cd, name, type, dmd); |
5125 | 0 | } |
5126 | 0 | } else if (tgt->type == DM_INTEGRITY) { |
5127 | 0 | r = device_block_adjust(cd, tgt->data_device, DEV_EXCL, |
5128 | 0 | tgt->u.integrity.offset, NULL, &dmd->flags); |
5129 | 0 | if (r) |
5130 | 0 | return r; |
5131 | | |
5132 | 0 | if (tgt->u.integrity.meta_device) { |
5133 | 0 | r = device_block_adjust(cd, tgt->u.integrity.meta_device, DEV_EXCL, 0, NULL, NULL); |
5134 | 0 | if (r) |
5135 | 0 | return r; |
5136 | 0 | } |
5137 | | |
5138 | 0 | r = dm_create_device(cd, name, type, dmd); |
5139 | 0 | } |
5140 | 0 | } |
5141 | | |
5142 | 0 | return r; |
5143 | 0 | } |
5144 | | |
5145 | | int create_or_reload_device_with_integrity(struct crypt_device *cd, const char *name, |
5146 | | const char *type, struct crypt_dm_active_device *dmd, |
5147 | | struct crypt_dm_active_device *dmdi) |
5148 | 0 | { |
5149 | 0 | int r; |
5150 | 0 | char *iname = NULL, *ipath = NULL; |
5151 | |
|
5152 | 0 | if (!type || !name || !dmd || !dmdi) |
5153 | 0 | return -EINVAL; |
5154 | | |
5155 | 0 | r = dm_get_iname(name, &iname, false); |
5156 | 0 | if (r) |
5157 | 0 | goto out; |
5158 | | |
5159 | 0 | r = dm_get_iname(name, &ipath, true); |
5160 | 0 | if (r) |
5161 | 0 | goto out; |
5162 | | |
5163 | | /* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */ |
5164 | 0 | r = check_devices(cd, name, iname, &dmd->flags); |
5165 | 0 | if (r) |
5166 | 0 | goto out; |
5167 | | |
5168 | 0 | if (dmd->flags & CRYPT_ACTIVATE_REFRESH) |
5169 | 0 | r = _reload_device_with_integrity(cd, name, iname, ipath, dmd, dmdi); |
5170 | 0 | else |
5171 | 0 | r = _create_device_with_integrity(cd, type, name, iname, ipath, dmd, dmdi); |
5172 | 0 | out: |
5173 | 0 | free(ipath); |
5174 | 0 | free(iname); |
5175 | |
|
5176 | 0 | return r; |
5177 | 0 | } |
5178 | | |
5179 | | static int load_all_keys(struct crypt_device *cd, struct volume_key *vks) |
5180 | 0 | { |
5181 | 0 | int r; |
5182 | 0 | struct volume_key *vk = vks; |
5183 | |
|
5184 | 0 | while (vk) { |
5185 | 0 | r = LUKS2_volume_key_load_in_keyring_by_digest(cd, vk, crypt_volume_key_get_id(vk)); |
5186 | 0 | if (r < 0) |
5187 | 0 | return r; |
5188 | 0 | vk = crypt_volume_key_next(vk); |
5189 | 0 | } |
5190 | | |
5191 | 0 | return 0; |
5192 | 0 | } |
5193 | | |
5194 | | #if USE_LUKS2_REENCRYPTION |
5195 | | static int _activate_reencrypt_device_by_vk(struct crypt_device *cd, |
5196 | | struct luks2_hdr *hdr, |
5197 | | const char *name, |
5198 | | struct volume_key *vks, |
5199 | | uint32_t flags) |
5200 | 0 | { |
5201 | 0 | bool dynamic_size; |
5202 | 0 | crypt_reencrypt_info ri; |
5203 | 0 | uint64_t minimal_size, device_size; |
5204 | 0 | int r = 0; |
5205 | 0 | struct crypt_lock_handle *reencrypt_lock = NULL; |
5206 | 0 | struct volume_key *vk; |
5207 | |
|
5208 | 0 | assert(hdr); |
5209 | 0 | assert(vks); |
5210 | |
|
5211 | 0 | r = LUKS2_reencrypt_lock(cd, &reencrypt_lock); |
5212 | 0 | if (r) { |
5213 | 0 | if (r == -EBUSY) |
5214 | 0 | log_err(cd, _("Reencryption in-progress. Cannot activate device.")); |
5215 | 0 | else |
5216 | 0 | log_err(cd, _("Failed to get reencryption lock.")); |
5217 | 0 | return r; |
5218 | 0 | } |
5219 | | |
5220 | 0 | if ((r = crypt_load(cd, CRYPT_LUKS2, NULL))) |
5221 | 0 | goto out; |
5222 | | |
5223 | 0 | ri = LUKS2_reencrypt_status(hdr); |
5224 | 0 | if (ri == CRYPT_REENCRYPT_INVALID) { |
5225 | 0 | r = -EINVAL; |
5226 | 0 | goto out; |
5227 | 0 | } |
5228 | | |
5229 | 0 | if (ri > CRYPT_REENCRYPT_NONE) { |
5230 | | /* it's sufficient to force re-verify the reencrypt digest only */ |
5231 | 0 | r = LUKS2_reencrypt_digest_verify(cd, &cd->u.luks2.hdr, vks); |
5232 | 0 | if (r < 0) |
5233 | 0 | goto out; |
5234 | | |
5235 | 0 | if (ri == CRYPT_REENCRYPT_CRASH) { |
5236 | 0 | r = LUKS2_reencrypt_locked_recovery_by_vks(cd, vks); |
5237 | 0 | if (r < 0) { |
5238 | 0 | log_err(cd, _("LUKS2 reencryption recovery using volume key(s) failed.")); |
5239 | 0 | goto out; |
5240 | 0 | } |
5241 | | |
5242 | 0 | ri = LUKS2_reencrypt_status(hdr); |
5243 | 0 | } |
5244 | 0 | } |
5245 | | |
5246 | | /* recovery finished reencryption or it was already finished after metadata reload */ |
5247 | 0 | if (ri == CRYPT_REENCRYPT_NONE) { |
5248 | 0 | vk = crypt_volume_key_by_id(vks, LUKS2_digest_by_segment(hdr, CRYPT_DEFAULT_SEGMENT)); |
5249 | 0 | if (!vk) { |
5250 | 0 | r = -EPERM; |
5251 | 0 | goto out; |
5252 | 0 | } |
5253 | | |
5254 | 0 | r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk); |
5255 | 0 | if (r >= 0) |
5256 | 0 | r = LUKS2_activate(cd, name, vk, NULL, flags); |
5257 | 0 | goto out; |
5258 | 0 | } |
5259 | 0 | if (ri > CRYPT_REENCRYPT_CLEAN) { |
5260 | 0 | r = -EINVAL; |
5261 | 0 | goto out; |
5262 | 0 | } |
5263 | | |
5264 | 0 | if ((r = LUKS2_get_data_size(hdr, &minimal_size, &dynamic_size))) |
5265 | 0 | goto out; |
5266 | | |
5267 | 0 | log_dbg(cd, "Entering clean reencryption state mode."); |
5268 | |
|
5269 | 0 | r = LUKS2_reencrypt_check_device_size(cd, hdr, minimal_size, &device_size, |
5270 | 0 | !(flags & CRYPT_ACTIVATE_SHARED), |
5271 | 0 | dynamic_size); |
5272 | 0 | if (r < 0) |
5273 | 0 | goto out; |
5274 | 0 | r = LUKS2_activate_multi(cd, name, vks, device_size >> SECTOR_SHIFT, flags); |
5275 | 0 | out: |
5276 | 0 | LUKS2_reencrypt_unlock(cd, reencrypt_lock); |
5277 | |
|
5278 | 0 | return r; |
5279 | 0 | } |
5280 | | |
5281 | | /* |
5282 | | * Activation/deactivation of a device |
5283 | | */ |
5284 | | static int _activate_luks2_by_volume_key(struct crypt_device *cd, |
5285 | | const char *name, |
5286 | | struct volume_key *vk, |
5287 | | struct volume_key *external_key, |
5288 | | uint32_t flags) |
5289 | 0 | { |
5290 | 0 | int r; |
5291 | 0 | crypt_reencrypt_info ri; |
5292 | 0 | ri = LUKS2_reencrypt_status(&cd->u.luks2.hdr); |
5293 | 0 | if (ri == CRYPT_REENCRYPT_INVALID) |
5294 | 0 | return -EINVAL; |
5295 | | |
5296 | 0 | if (ri > CRYPT_REENCRYPT_NONE) { |
5297 | | /* reencryption must reverify keys after taking the reencryption lock and reloading metadata */ |
5298 | 0 | r = _activate_reencrypt_device_by_vk(cd, &cd->u.luks2.hdr, name, vk, flags); |
5299 | 0 | } else { |
5300 | | /* hw-opal data segment type does not require volume key for activation */ |
5301 | 0 | assert(!vk || crypt_volume_key_get_id(vk) == LUKS2_digest_by_segment(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)); |
5302 | 0 | r = LUKS2_activate(cd, name, vk, external_key, flags); |
5303 | 0 | } |
5304 | |
|
5305 | 0 | return r; |
5306 | 0 | } |
5307 | | #else |
5308 | | static int _activate_luks2_by_volume_key(struct crypt_device *cd, |
5309 | | const char *name, |
5310 | | struct volume_key *vk, |
5311 | | struct volume_key *external_key, |
5312 | | uint32_t flags) |
5313 | | { |
5314 | | int r; |
5315 | | crypt_reencrypt_info ri; |
5316 | | ri = LUKS2_reencrypt_status(&cd->u.luks2.hdr); |
5317 | | if (ri == CRYPT_REENCRYPT_INVALID) |
5318 | | return -EINVAL; |
5319 | | |
5320 | | if (ri > CRYPT_REENCRYPT_NONE) { |
5321 | | log_err(cd, _("This operation is not supported for this device type.")); |
5322 | | r = -ENOTSUP; |
5323 | | } else { |
5324 | | assert(crypt_volume_key_get_id(vk) == LUKS2_digest_by_segment(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)); |
5325 | | r = LUKS2_activate(cd, name, vk, external_key, flags); |
5326 | | } |
5327 | | |
5328 | | return r; |
5329 | | } |
5330 | | #endif |
5331 | | |
5332 | | static int _activate_loopaes(struct crypt_device *cd, |
5333 | | const char *name, |
5334 | | const char *buffer, |
5335 | | size_t buffer_size, |
5336 | | uint32_t flags) |
5337 | 0 | { |
5338 | 0 | int r; |
5339 | 0 | unsigned int key_count = 0; |
5340 | 0 | struct volume_key *vk = NULL; |
5341 | 0 | char *buffer_copy; |
5342 | |
|
5343 | 0 | buffer_copy = crypt_safe_alloc(buffer_size); |
5344 | 0 | if (!buffer_copy) |
5345 | 0 | return -ENOMEM; |
5346 | 0 | crypt_safe_memcpy(buffer_copy, buffer, buffer_size); |
5347 | |
|
5348 | 0 | r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count, |
5349 | 0 | buffer_copy, buffer_size); |
5350 | 0 | crypt_safe_free(buffer_copy); |
5351 | |
|
5352 | 0 | if (!r && name) |
5353 | 0 | r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher, key_count, |
5354 | 0 | vk, flags); |
5355 | |
|
5356 | 0 | crypt_free_volume_key(vk); |
5357 | |
|
5358 | 0 | return r; |
5359 | 0 | } |
5360 | | |
5361 | | static int _activate_check_status(struct crypt_device *cd, const char *name, unsigned reload) |
5362 | 0 | { |
5363 | 0 | int r; |
5364 | |
|
5365 | 0 | if (!name) |
5366 | 0 | return 0; |
5367 | | |
5368 | 0 | r = dm_status_device(cd, name); |
5369 | |
|
5370 | 0 | if (r >= 0 && reload) |
5371 | 0 | return 0; |
5372 | | |
5373 | 0 | if (r >= 0 || r == -EEXIST) { |
5374 | 0 | log_err(cd, _("Device %s already exists."), name); |
5375 | 0 | return -EEXIST; |
5376 | 0 | } |
5377 | | |
5378 | 0 | if (r == -ENODEV) |
5379 | 0 | return 0; |
5380 | | |
5381 | 0 | log_err(cd, _("Cannot use device %s, name is invalid or still in use."), name); |
5382 | 0 | return r; |
5383 | 0 | } |
5384 | | |
5385 | | static int _verify_reencrypt_keys(struct crypt_device *cd, struct volume_key *vks) |
5386 | 0 | { |
5387 | 0 | int r; |
5388 | |
|
5389 | 0 | assert(cd && (isLUKS2(cd->type))); |
5390 | |
|
5391 | 0 | r = LUKS2_reencrypt_digest_verify(cd, &cd->u.luks2.hdr, vks); |
5392 | 0 | if (r == -EPERM || r == -ENOENT || r == -EINVAL) |
5393 | 0 | log_err(cd, _("Reencryption volume keys do not match the volume.")); |
5394 | |
|
5395 | 0 | return r; |
5396 | 0 | } |
5397 | | |
5398 | | static int _verify_key(struct crypt_device *cd, |
5399 | | bool unbound_key, |
5400 | | struct volume_key *vk) |
5401 | 0 | { |
5402 | 0 | int r = -EINVAL; |
5403 | |
|
5404 | 0 | assert(cd); |
5405 | |
|
5406 | 0 | if (isPLAIN(cd->type)) { |
5407 | 0 | if (vk && crypt_volume_key_length(vk) == cd->u.plain.key_size) { |
5408 | 0 | r = KEY_VERIFIED; |
5409 | 0 | } else |
5410 | 0 | log_err(cd, _("Incorrect volume key specified for plain device.")); |
5411 | 0 | } else if (isLUKS1(cd->type)) { |
5412 | 0 | if (!vk) |
5413 | 0 | return -EINVAL; |
5414 | | |
5415 | 0 | r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk); |
5416 | 0 | } else if (isLUKS2(cd->type)) { |
5417 | 0 | if (!vk) |
5418 | 0 | return -EINVAL; |
5419 | | |
5420 | 0 | if (unbound_key) |
5421 | 0 | r = LUKS2_digest_verify_by_any_matching(cd, vk, /* exclude_default_segment= */ false); |
5422 | 0 | else |
5423 | 0 | r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk); |
5424 | 0 | } else if (isVERITY(cd->type)) |
5425 | 0 | r = KEY_VERIFIED; |
5426 | 0 | else if (isTCRYPT(cd->type)) |
5427 | 0 | r = KEY_VERIFIED; |
5428 | 0 | else if (isINTEGRITY(cd->type)) |
5429 | 0 | r = KEY_VERIFIED; |
5430 | 0 | else if (isBITLK(cd->type)) |
5431 | 0 | r = KEY_VERIFIED; |
5432 | 0 | else if (isFVAULT2(cd->type)) { |
5433 | 0 | if (vk && crypt_volume_key_length(vk) == FVAULT2_volume_key_size()) |
5434 | 0 | r = KEY_VERIFIED; |
5435 | 0 | } else |
5436 | 0 | log_err(cd, _("Device type is not properly initialized.")); |
5437 | | |
5438 | 0 | if (r >= KEY_VERIFIED) |
5439 | 0 | crypt_volume_key_set_id(vk, r); |
5440 | |
|
5441 | 0 | return r > 0 ? 0 : r; |
5442 | 0 | } |
5443 | | |
5444 | | /* activation/deactivation of device mapping */ |
5445 | | static int _activate_by_volume_key(struct crypt_device *cd, |
5446 | | const char *name, |
5447 | | struct volume_key *vk, |
5448 | | struct volume_key *external_key, |
5449 | | uint32_t flags) |
5450 | 0 | { |
5451 | 0 | int r; |
5452 | |
|
5453 | 0 | assert(cd); |
5454 | 0 | assert(name); |
5455 | |
|
5456 | 0 | r = _check_header_data_overlap(cd, name); |
5457 | 0 | if (r < 0) |
5458 | 0 | return r; |
5459 | | |
5460 | | /* use key directly, no hash */ |
5461 | 0 | if (isPLAIN(cd->type)) { |
5462 | 0 | assert(!external_key); |
5463 | 0 | assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED); |
5464 | |
|
5465 | 0 | r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags); |
5466 | 0 | } else if (isLUKS1(cd->type)) { |
5467 | 0 | assert(!external_key); |
5468 | 0 | assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED); |
5469 | |
|
5470 | 0 | r = LUKS1_activate(cd, name, vk, flags); |
5471 | 0 | } else if (isLUKS2(cd->type)) { |
5472 | 0 | r = _activate_luks2_by_volume_key(cd, name, vk, external_key, flags); |
5473 | 0 | } else if (isVERITY(cd->type)) { |
5474 | 0 | assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED); |
5475 | 0 | r = VERITY_activate(cd, name, vk, external_key, cd->u.verity.fec_device, |
5476 | 0 | &cd->u.verity.hdr, flags); |
5477 | 0 | } else if (isTCRYPT(cd->type)) { |
5478 | 0 | assert(!external_key); |
5479 | 0 | r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr, |
5480 | 0 | &cd->u.tcrypt.params, flags); |
5481 | 0 | } else if (isINTEGRITY(cd->type)) { |
5482 | 0 | assert(!external_key); |
5483 | 0 | assert(!vk || crypt_volume_key_get_id(vk) == KEY_VERIFIED); |
5484 | 0 | r = INTEGRITY_activate(cd, name, &cd->u.integrity.params, vk, |
5485 | 0 | cd->u.integrity.journal_crypt_key, |
5486 | 0 | cd->u.integrity.journal_mac_key, flags, |
5487 | 0 | cd->u.integrity.sb_flags); |
5488 | 0 | } else if (isBITLK(cd->type)) { |
5489 | 0 | assert(!external_key); |
5490 | 0 | assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED); |
5491 | 0 | r = BITLK_activate_by_volume_key(cd, name, vk, &cd->u.bitlk.params, flags); |
5492 | 0 | } else if (isFVAULT2(cd->type)) { |
5493 | 0 | assert(!external_key); |
5494 | 0 | assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED); |
5495 | 0 | r = FVAULT2_activate_by_volume_key(cd, name, vk, &cd->u.fvault2.params, flags); |
5496 | 0 | } else { |
5497 | 0 | log_err(cd, _("Device type is not properly initialized.")); |
5498 | 0 | r = -EINVAL; |
5499 | 0 | } |
5500 | |
|
5501 | 0 | return r; |
5502 | 0 | } |
5503 | | |
5504 | | int crypt_activate_by_keyslot_context(struct crypt_device *cd, |
5505 | | const char *name, |
5506 | | int keyslot, |
5507 | | struct crypt_keyslot_context *kc, |
5508 | | int additional_keyslot, |
5509 | | struct crypt_keyslot_context *additional_kc, |
5510 | | uint32_t flags) |
5511 | 0 | { |
5512 | 0 | bool use_keyring, luks2_reencryption = false; |
5513 | 0 | struct volume_key *p_ext_key, *crypt_key = NULL, *opal_key = NULL, *vk = NULL, |
5514 | 0 | *vk_sign = NULL, *p_crypt = NULL; |
5515 | 0 | size_t passphrase_size; |
5516 | 0 | const char *passphrase = NULL; |
5517 | 0 | int unlocked_keyslot, r = -EINVAL; |
5518 | 0 | key_serial_t kid1 = 0, kid2 = 0; |
5519 | 0 | struct luks2_hdr *hdr = &cd->u.luks2.hdr; |
5520 | |
|
5521 | 0 | if (!cd || !kc) |
5522 | 0 | return -EINVAL; |
5523 | | |
5524 | 0 | log_dbg(cd, "%s volume %s [keyslot %d] using %s.", |
5525 | 0 | name ? "Activating" : "Checking", name ?: "passphrase", keyslot, keyslot_context_type_string(kc)); |
5526 | 0 | if (!name && (flags & CRYPT_ACTIVATE_REFRESH)) |
5527 | 0 | return -EINVAL; |
5528 | 0 | if ((flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_use_keyring_for_vk(cd)) |
5529 | 0 | return -EINVAL; |
5530 | 0 | if ((flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) && name) |
5531 | 0 | return -EINVAL; |
5532 | 0 | if (!additional_kc && (additional_keyslot != CRYPT_ANY_SLOT)) |
5533 | 0 | return -EINVAL; |
5534 | 0 | if ((kc->type == CRYPT_KC_TYPE_KEYRING) && !kernel_keyring_support()) { |
5535 | 0 | log_err(cd, _("Kernel keyring is not supported by the kernel.")); |
5536 | 0 | return -EINVAL; |
5537 | 0 | } |
5538 | 0 | if ((kc->type == CRYPT_KC_TYPE_SIGNED_KEY) && !kernel_keyring_support()) { |
5539 | 0 | log_err(cd, _("Kernel keyring missing: required for passing signature to kernel.")); |
5540 | 0 | return -EINVAL; |
5541 | 0 | } |
5542 | 0 | r = _check_header_data_overlap(cd, name); |
5543 | 0 | if (r < 0) |
5544 | 0 | return r; |
5545 | 0 | r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH); |
5546 | 0 | if (r < 0) |
5547 | 0 | return r; |
5548 | | |
5549 | 0 | if (kc->get_passphrase && kc->type != CRYPT_KC_TYPE_TOKEN && |
5550 | 0 | isLOOPAES(cd->type)) { |
5551 | 0 | r = kc->get_passphrase(cd, kc, &passphrase, &passphrase_size); |
5552 | 0 | if (r < 0) |
5553 | 0 | return r; |
5554 | | |
5555 | 0 | return _activate_loopaes(cd, name, passphrase, passphrase_size, flags); |
5556 | 0 | } |
5557 | | |
5558 | 0 | if (flags & CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF) |
5559 | 0 | cd->memory_hard_pbkdf_lock_enabled = true; |
5560 | | |
5561 | | /* acquire the volume key(s) */ |
5562 | 0 | r = -EINVAL; |
5563 | 0 | if (isLUKS1(cd->type)) { |
5564 | 0 | if (kc->get_luks1_volume_key) |
5565 | 0 | r = kc->get_luks1_volume_key(cd, kc, keyslot, &vk); |
5566 | 0 | } else if (isLUKS2(cd->type)) { |
5567 | 0 | if (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) { |
5568 | 0 | if (kc->get_luks2_key) |
5569 | 0 | r = kc->get_luks2_key(cd, kc, keyslot, CRYPT_ANY_SEGMENT, &vk); |
5570 | 0 | } else { |
5571 | 0 | switch (LUKS2_reencrypt_status(hdr)) { |
5572 | 0 | case CRYPT_REENCRYPT_NONE: |
5573 | 0 | if (kc->get_luks2_volume_key) |
5574 | 0 | r = kc->get_luks2_volume_key(cd, kc, keyslot, &vk); |
5575 | 0 | break; |
5576 | 0 | case CRYPT_REENCRYPT_CLEAN: /* fall-through */ |
5577 | 0 | case CRYPT_REENCRYPT_CRASH: |
5578 | 0 | luks2_reencryption = true; |
5579 | 0 | r = LUKS2_keyslot_context_open_all_segments(cd, keyslot, additional_keyslot, kc, additional_kc, &vk); |
5580 | | /* fall-through */ |
5581 | 0 | default: |
5582 | 0 | break; |
5583 | 0 | } |
5584 | 0 | } |
5585 | 0 | } else if (isTCRYPT(cd->type)) { |
5586 | 0 | r = 0; |
5587 | 0 | } else if (name && isPLAIN(cd->type)) { |
5588 | 0 | if (kc->type == CRYPT_KC_TYPE_VK_KEYRING) { |
5589 | 0 | vk = crypt_alloc_volume_key(cd->u.plain.key_size, NULL); |
5590 | 0 | if (!vk) |
5591 | 0 | return -ENOMEM; |
5592 | 0 | r = crypt_volume_key_set_description_by_name(vk, kc->u.vk_kr.key_description); |
5593 | 0 | if (r < 0) |
5594 | 0 | log_err(cd, _("Cannot use keyring key %s."), kc->u.vk_kr.key_description); |
5595 | 0 | } else if (kc->get_passphrase && kc->type != CRYPT_KC_TYPE_TOKEN) { |
5596 | 0 | r = kc->get_passphrase(cd, kc, &passphrase, &passphrase_size); |
5597 | 0 | if (r < 0) |
5598 | 0 | return r; |
5599 | 0 | r = process_key(cd, cd->u.plain.hdr.hash, |
5600 | 0 | cd->u.plain.key_size, |
5601 | 0 | passphrase, passphrase_size, &vk); |
5602 | 0 | } else if (kc->get_plain_volume_key) |
5603 | 0 | r = kc->get_plain_volume_key(cd, kc, &vk); |
5604 | 0 | } else if (isBITLK(cd->type)) { |
5605 | 0 | if (kc->get_bitlk_volume_key && (name || kc->type != CRYPT_KC_TYPE_KEY)) |
5606 | 0 | r = kc->get_bitlk_volume_key(cd, kc, &cd->u.bitlk.params, &vk); |
5607 | 0 | } else if (isFVAULT2(cd->type)) { |
5608 | 0 | if (kc->get_fvault2_volume_key) |
5609 | 0 | r = kc->get_fvault2_volume_key(cd, kc, &cd->u.fvault2.params, &vk); |
5610 | 0 | } else if (isVERITY(cd->type) && (name || kc->type != CRYPT_KC_TYPE_SIGNED_KEY)) { |
5611 | 0 | if (kc->get_verity_volume_key) |
5612 | 0 | r = kc->get_verity_volume_key(cd, kc, &vk, &vk_sign); |
5613 | 0 | if (r >= 0) |
5614 | 0 | r = VERITY_verify_params(cd, &cd->u.verity.hdr, vk_sign != NULL, |
5615 | 0 | cd->u.verity.fec_device, vk); |
5616 | |
|
5617 | 0 | free(CONST_CAST(void*)cd->u.verity.root_hash); |
5618 | 0 | cd->u.verity.root_hash = NULL; |
5619 | 0 | flags |= CRYPT_ACTIVATE_READONLY; |
5620 | 0 | } else if (isINTEGRITY(cd->type)) { |
5621 | 0 | if (kc->get_integrity_volume_key) |
5622 | 0 | r = kc->get_integrity_volume_key(cd, kc, &vk); |
5623 | 0 | } |
5624 | 0 | if (r < 0 && (r != -ENOENT || kc->type != CRYPT_KC_TYPE_KEY)) |
5625 | 0 | goto out; |
5626 | 0 | unlocked_keyslot = r; |
5627 | |
|
5628 | 0 | if (r == -ENOENT && isLUKS(cd->type) && cd->volume_key) { |
5629 | 0 | vk = crypt_alloc_volume_key(crypt_volume_key_length(cd->volume_key), |
5630 | 0 | crypt_volume_key_get_key(cd->volume_key)); |
5631 | 0 | r = vk ? 0 : -ENOMEM; |
5632 | 0 | } |
5633 | 0 | if (r == -ENOENT && isINTEGRITY(cd->type)) |
5634 | 0 | r = 0; |
5635 | |
|
5636 | 0 | if (r < 0) |
5637 | 0 | goto out; |
5638 | | |
5639 | 0 | if (luks2_reencryption) |
5640 | 0 | r = _verify_reencrypt_keys(cd, vk); |
5641 | 0 | else |
5642 | 0 | r = _verify_key(cd, flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY, vk); |
5643 | |
|
5644 | 0 | if (r < 0) |
5645 | 0 | goto out; |
5646 | | |
5647 | 0 | if (isLUKS2(cd->type)) { |
5648 | | /* split the key only if we do activation */ |
5649 | 0 | if (name && LUKS2_segment_is_hw_opal(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)) { |
5650 | 0 | r = LUKS2_split_crypt_and_opal_keys(cd, &cd->u.luks2.hdr, |
5651 | 0 | vk, &crypt_key, |
5652 | 0 | &opal_key); |
5653 | 0 | if (r < 0) |
5654 | 0 | goto out; |
5655 | | |
5656 | | /* copy volume key digest id in crypt subkey */ |
5657 | 0 | crypt_volume_key_set_id(crypt_key, crypt_volume_key_get_id(vk)); |
5658 | |
|
5659 | 0 | p_crypt = crypt_key; |
5660 | 0 | p_ext_key = opal_key ?: vk; |
5661 | 0 | } else { |
5662 | 0 | p_crypt = vk; |
5663 | 0 | p_ext_key = NULL; |
5664 | 0 | } |
5665 | | |
5666 | 0 | if (!crypt_use_keyring_for_vk(cd)) |
5667 | 0 | use_keyring = false; |
5668 | 0 | else |
5669 | | /* Force keyring use for activation of LUKS2 device in reencryption */ |
5670 | 0 | use_keyring = (name && (luks2_reencryption || !crypt_is_cipher_null(crypt_get_cipher(cd)))) || |
5671 | 0 | (flags & CRYPT_ACTIVATE_KEYRING_KEY); |
5672 | |
|
5673 | 0 | if (use_keyring) { |
5674 | | /* upload dm-crypt part of volume key in thread keyring if requested */ |
5675 | 0 | if (p_crypt) { |
5676 | 0 | r = load_all_keys(cd, p_crypt); |
5677 | 0 | if (r < 0) |
5678 | 0 | goto out; |
5679 | 0 | flags |= CRYPT_ACTIVATE_KEYRING_KEY; |
5680 | 0 | } |
5681 | | |
5682 | | /* upload the volume key in custom user keyring if requested */ |
5683 | 0 | if (cd->link_vk_to_keyring) { |
5684 | 0 | r = crypt_volume_key_load_in_custom_keyring(cd, vk, &kid1, &kid2); |
5685 | 0 | if (r < 0) { |
5686 | 0 | log_err(cd, _("Failed to link volume key in user defined keyring.")); |
5687 | 0 | goto out; |
5688 | 0 | } |
5689 | 0 | } |
5690 | 0 | } |
5691 | 0 | } else { |
5692 | 0 | p_crypt = vk; |
5693 | 0 | p_ext_key = vk_sign; |
5694 | 0 | } |
5695 | | |
5696 | 0 | if (name) |
5697 | 0 | r = _activate_by_volume_key(cd, name, p_crypt, p_ext_key, flags); |
5698 | |
|
5699 | 0 | if (r >= 0 && unlocked_keyslot >= 0) |
5700 | 0 | r = unlocked_keyslot; |
5701 | 0 | out: |
5702 | 0 | if (r < 0) { |
5703 | 0 | crypt_drop_uploaded_keyring_key(cd, vk); |
5704 | 0 | crypt_drop_uploaded_keyring_key(cd, crypt_key); |
5705 | 0 | if (cd->link_vk_to_keyring && kid1) |
5706 | 0 | crypt_unlink_key_from_custom_keyring(cd, kid1); |
5707 | 0 | if (cd->link_vk_to_keyring && kid2) |
5708 | 0 | crypt_unlink_key_from_custom_keyring(cd, kid2); |
5709 | 0 | } |
5710 | |
|
5711 | 0 | crypt_free_volume_key(vk); |
5712 | 0 | crypt_free_volume_key(crypt_key); |
5713 | 0 | crypt_free_volume_key(opal_key); |
5714 | 0 | crypt_free_volume_key(vk_sign); |
5715 | 0 | return r; |
5716 | 0 | } |
5717 | | |
5718 | | int crypt_activate_by_passphrase(struct crypt_device *cd, |
5719 | | const char *name, |
5720 | | int keyslot, |
5721 | | const char *passphrase, |
5722 | | size_t passphrase_size, |
5723 | | uint32_t flags) |
5724 | 0 | { |
5725 | 0 | int r; |
5726 | 0 | struct crypt_keyslot_context kc = {}; |
5727 | |
|
5728 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size); |
5729 | 0 | r = crypt_activate_by_keyslot_context(cd, name, keyslot, &kc, CRYPT_ANY_SLOT, &kc, flags); |
5730 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
5731 | |
|
5732 | 0 | return r; |
5733 | 0 | } |
5734 | | |
5735 | | int crypt_activate_by_keyfile_device_offset(struct crypt_device *cd, |
5736 | | const char *name, |
5737 | | int keyslot, |
5738 | | const char *keyfile, |
5739 | | size_t keyfile_size, |
5740 | | uint64_t keyfile_offset, |
5741 | | uint32_t flags) |
5742 | 0 | { |
5743 | 0 | int r; |
5744 | 0 | struct crypt_keyslot_context kc = {}; |
5745 | |
|
5746 | 0 | crypt_keyslot_context_init_by_keyfile_internal(&kc, keyfile, keyfile_size, keyfile_offset); |
5747 | 0 | r = crypt_activate_by_keyslot_context(cd, name, keyslot, &kc, CRYPT_ANY_SLOT, &kc, flags); |
5748 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
5749 | |
|
5750 | 0 | return r; |
5751 | 0 | } |
5752 | | |
5753 | | int crypt_activate_by_keyfile(struct crypt_device *cd, |
5754 | | const char *name, |
5755 | | int keyslot, |
5756 | | const char *keyfile, |
5757 | | size_t keyfile_size, |
5758 | | uint32_t flags) |
5759 | 0 | { |
5760 | 0 | return crypt_activate_by_keyfile_device_offset(cd, name, keyslot, keyfile, |
5761 | 0 | keyfile_size, 0, flags); |
5762 | 0 | } |
5763 | | |
5764 | | int crypt_activate_by_keyfile_offset(struct crypt_device *cd, |
5765 | | const char *name, |
5766 | | int keyslot, |
5767 | | const char *keyfile, |
5768 | | size_t keyfile_size, |
5769 | | size_t keyfile_offset, |
5770 | | uint32_t flags) |
5771 | 0 | { |
5772 | 0 | return crypt_activate_by_keyfile_device_offset(cd, name, keyslot, keyfile, |
5773 | 0 | keyfile_size, keyfile_offset, flags); |
5774 | 0 | } |
5775 | | |
5776 | | int crypt_activate_by_volume_key(struct crypt_device *cd, |
5777 | | const char *name, |
5778 | | const char *volume_key, |
5779 | | size_t volume_key_size, |
5780 | | uint32_t flags) |
5781 | 0 | { |
5782 | 0 | int r; |
5783 | 0 | struct crypt_keyslot_context kc = {}; |
5784 | |
|
5785 | 0 | crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size); |
5786 | 0 | r = crypt_activate_by_keyslot_context(cd, name, CRYPT_ANY_SLOT /* unused */, &kc, CRYPT_ANY_SLOT, &kc, flags); |
5787 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
5788 | |
|
5789 | 0 | return r; |
5790 | 0 | } |
5791 | | |
5792 | | int crypt_activate_by_signed_key(struct crypt_device *cd, |
5793 | | const char *name, |
5794 | | const char *volume_key, |
5795 | | size_t volume_key_size, |
5796 | | const char *signature, |
5797 | | size_t signature_size, |
5798 | | uint32_t flags) |
5799 | 0 | { |
5800 | 0 | int r; |
5801 | 0 | struct crypt_keyslot_context kc = {}; |
5802 | |
|
5803 | 0 | if (!cd || !isVERITY(cd->type)) |
5804 | 0 | return -EINVAL; |
5805 | | |
5806 | 0 | if (!volume_key || !volume_key_size || (!name && signature)) { |
5807 | 0 | log_err(cd, _("Incorrect root hash specified for verity device.")); |
5808 | 0 | return -EINVAL; |
5809 | 0 | } |
5810 | | |
5811 | 0 | if (signature) |
5812 | 0 | crypt_keyslot_context_init_by_signed_key_internal(&kc, volume_key, volume_key_size, |
5813 | 0 | signature, signature_size); |
5814 | 0 | else |
5815 | 0 | crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size); |
5816 | 0 | r = crypt_activate_by_keyslot_context(cd, name, -2 /* unused */, &kc, CRYPT_ANY_SLOT, NULL, flags); |
5817 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
5818 | |
|
5819 | 0 | return r; |
5820 | 0 | } |
5821 | | |
5822 | | int crypt_deactivate_by_name(struct crypt_device *cd, const char *name, uint32_t flags) |
5823 | 0 | { |
5824 | 0 | struct crypt_device *fake_cd = NULL; |
5825 | 0 | struct luks2_hdr *hdr2 = NULL; |
5826 | 0 | struct crypt_dm_active_device dmd = {}; |
5827 | 0 | int r; |
5828 | 0 | uint64_t get_flags = DM_ACTIVE_DEVICE | DM_ACTIVE_UUID | DM_ACTIVE_HOLDERS; |
5829 | |
|
5830 | 0 | if (!name) |
5831 | 0 | return -EINVAL; |
5832 | | |
5833 | 0 | if ((flags & CRYPT_DEACTIVATE_DEFERRED) && (flags & CRYPT_DEACTIVATE_DEFERRED_CANCEL)) |
5834 | 0 | return -EINVAL; |
5835 | | |
5836 | 0 | log_dbg(cd, "Deactivating volume %s.", name); |
5837 | |
|
5838 | 0 | if (!cd) { |
5839 | 0 | r = crypt_init_by_name(&fake_cd, name); |
5840 | 0 | if (r < 0) |
5841 | 0 | return r; |
5842 | 0 | cd = fake_cd; |
5843 | 0 | } |
5844 | | |
5845 | 0 | if (flags & (CRYPT_DEACTIVATE_DEFERRED | CRYPT_DEACTIVATE_DEFERRED_CANCEL)) { |
5846 | 0 | r = crypt_get_hw_encryption_type(cd); |
5847 | 0 | if (r == CRYPT_SW_AND_OPAL_HW || r == CRYPT_OPAL_HW_ONLY) { |
5848 | 0 | log_err(cd, _("OPAL does not support deferred deactivation.")); |
5849 | 0 | return -EINVAL; |
5850 | 0 | } |
5851 | 0 | } |
5852 | | |
5853 | | /* skip holders detection and early abort when some flags raised */ |
5854 | 0 | if (flags & (CRYPT_DEACTIVATE_FORCE | CRYPT_DEACTIVATE_DEFERRED | CRYPT_DEACTIVATE_DEFERRED_CANCEL)) |
5855 | 0 | get_flags &= ~DM_ACTIVE_HOLDERS; |
5856 | |
|
5857 | 0 | switch (crypt_status(cd, name)) { |
5858 | 0 | case CRYPT_ACTIVE: |
5859 | 0 | case CRYPT_BUSY: |
5860 | 0 | r = dm_query_device(cd, name, get_flags, &dmd); |
5861 | 0 | if (r >= 0) { |
5862 | 0 | if (dmd.holders) { |
5863 | 0 | log_err(cd, _("Device %s is still in use."), name); |
5864 | 0 | r = -EBUSY; |
5865 | 0 | break; |
5866 | 0 | } |
5867 | 0 | } |
5868 | | |
5869 | | /* For detached header case or missing metadata we need to check for OPAL2 devices |
5870 | | * from DM UUID */ |
5871 | 0 | if (dmd.uuid && (flags & (CRYPT_DEACTIVATE_DEFERRED | CRYPT_DEACTIVATE_DEFERRED_CANCEL)) && |
5872 | 0 | !strncmp(CRYPT_LUKS2_HW_OPAL, dmd.uuid, sizeof(CRYPT_LUKS2_HW_OPAL)-1)) { |
5873 | 0 | log_err(cd, _("OPAL does not support deferred deactivation.")); |
5874 | 0 | r = -EINVAL; |
5875 | 0 | break; |
5876 | 0 | } |
5877 | | |
5878 | 0 | if (flags & CRYPT_DEACTIVATE_DEFERRED_CANCEL) { |
5879 | 0 | r = dm_cancel_deferred_removal(name); |
5880 | 0 | if (r < 0) |
5881 | 0 | log_err(cd, _("Could not cancel deferred remove from device %s."), name); |
5882 | 0 | break; |
5883 | 0 | } |
5884 | | |
5885 | 0 | hdr2 = crypt_get_hdr(cd, CRYPT_LUKS2); |
5886 | |
|
5887 | 0 | if ((dmd.uuid && !strncmp(CRYPT_LUKS2, dmd.uuid, sizeof(CRYPT_LUKS2)-1)) || hdr2) |
5888 | 0 | r = LUKS2_deactivate(cd, name, hdr2, &dmd, flags); |
5889 | 0 | else if (isTCRYPT(cd->type)) |
5890 | 0 | r = TCRYPT_deactivate(cd, name, flags); |
5891 | 0 | else |
5892 | 0 | r = dm_remove_device(cd, name, flags); |
5893 | 0 | if (r < 0 && crypt_status(cd, name) == CRYPT_BUSY) { |
5894 | 0 | log_err(cd, _("Device %s is still in use."), name); |
5895 | 0 | r = -EBUSY; |
5896 | 0 | } |
5897 | 0 | break; |
5898 | 0 | case CRYPT_INACTIVE: |
5899 | 0 | log_err(cd, _("Device %s is not active."), name); |
5900 | 0 | r = -ENODEV; |
5901 | 0 | break; |
5902 | 0 | default: |
5903 | 0 | log_err(cd, _("Invalid device %s."), name); |
5904 | 0 | r = -EINVAL; |
5905 | 0 | } |
5906 | | |
5907 | 0 | dm_targets_free(cd, &dmd); |
5908 | 0 | free(CONST_CAST(void*)dmd.uuid); |
5909 | 0 | crypt_free(fake_cd); |
5910 | |
|
5911 | 0 | return r; |
5912 | 0 | } |
5913 | | |
5914 | | int crypt_deactivate(struct crypt_device *cd, const char *name) |
5915 | 0 | { |
5916 | 0 | return crypt_deactivate_by_name(cd, name, 0); |
5917 | 0 | } |
5918 | | |
5919 | | int crypt_get_active_device(struct crypt_device *cd, const char *name, |
5920 | | struct crypt_active_device *cad) |
5921 | 0 | { |
5922 | 0 | int r; |
5923 | 0 | struct crypt_dm_active_device dmd, dmdi = {}; |
5924 | 0 | char *iname = NULL; |
5925 | 0 | struct dm_target *tgt = &dmd.segment; |
5926 | 0 | uint64_t min_offset = UINT64_MAX; |
5927 | |
|
5928 | 0 | if (!cd || !name || !cad) |
5929 | 0 | return -EINVAL; |
5930 | | |
5931 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_DEVICE, &dmd); |
5932 | 0 | if (r < 0) |
5933 | 0 | return r; |
5934 | | |
5935 | | /* |
5936 | | * For integrity and LUKS2 (and detached header where context is NULL) |
5937 | | * we need flags from underlying dm-integrity device. |
5938 | | * This check must be skipped for non-LUKS2 integrity device. |
5939 | | */ |
5940 | 0 | if ((isLUKS2(cd->type) || !cd->type) && crypt_get_integrity_tag_size(cd)) { |
5941 | 0 | if ((iname = dm_get_active_iname(cd, name))) { |
5942 | 0 | if (dm_query_device(cd, iname, 0, &dmdi) >= 0) |
5943 | 0 | dmd.flags |= dmdi.flags; |
5944 | 0 | free(iname); |
5945 | 0 | } else |
5946 | 0 | dmd.flags |= (CRYPT_ACTIVATE_NO_JOURNAL | CRYPT_ACTIVATE_INLINE_MODE); |
5947 | 0 | } |
5948 | |
|
5949 | 0 | if (cd && isTCRYPT(cd->type)) { |
5950 | 0 | cad->offset = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params); |
5951 | 0 | cad->iv_offset = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params); |
5952 | 0 | } else { |
5953 | 0 | while (tgt) { |
5954 | 0 | if (tgt->type == DM_CRYPT && (min_offset > tgt->u.crypt.offset)) { |
5955 | 0 | min_offset = tgt->u.crypt.offset; |
5956 | 0 | cad->iv_offset = tgt->u.crypt.iv_offset; |
5957 | 0 | } else if (tgt->type == DM_INTEGRITY && (min_offset > tgt->u.integrity.offset)) { |
5958 | 0 | min_offset = tgt->u.integrity.offset; |
5959 | 0 | cad->iv_offset = 0; |
5960 | 0 | } else if (tgt->type == DM_LINEAR && (min_offset > tgt->u.linear.offset)) { |
5961 | 0 | min_offset = tgt->u.linear.offset; |
5962 | 0 | cad->iv_offset = 0; |
5963 | 0 | } |
5964 | 0 | tgt = tgt->next; |
5965 | 0 | } |
5966 | 0 | } |
5967 | |
|
5968 | 0 | if (min_offset != UINT64_MAX) |
5969 | 0 | cad->offset = min_offset; |
5970 | |
|
5971 | 0 | cad->size = dmd.size; |
5972 | 0 | cad->flags = dmd.flags; |
5973 | |
|
5974 | 0 | r = 0; |
5975 | 0 | dm_targets_free(cd, &dmd); |
5976 | 0 | dm_targets_free(cd, &dmdi); |
5977 | |
|
5978 | 0 | return r; |
5979 | 0 | } |
5980 | | |
5981 | | uint64_t crypt_get_active_integrity_failures(struct crypt_device *cd, const char *name) |
5982 | 0 | { |
5983 | 0 | struct crypt_dm_active_device dmd; |
5984 | 0 | uint64_t failures = 0; |
5985 | |
|
5986 | 0 | if (!name) |
5987 | 0 | return 0; |
5988 | | |
5989 | | /* LUKS2 / dm-crypt does not provide this count. */ |
5990 | 0 | if (dm_query_device(cd, name, 0, &dmd) < 0) |
5991 | 0 | return 0; |
5992 | | |
5993 | 0 | if (single_segment(&dmd) && dmd.segment.type == DM_INTEGRITY) |
5994 | 0 | (void)dm_status_integrity_failures(cd, name, &failures); |
5995 | |
|
5996 | 0 | dm_targets_free(cd, &dmd); |
5997 | |
|
5998 | 0 | return failures; |
5999 | 0 | } |
6000 | | |
6001 | | /* |
6002 | | * Volume key handling |
6003 | | */ |
6004 | | int crypt_volume_key_get(struct crypt_device *cd, |
6005 | | int keyslot, |
6006 | | char *volume_key, |
6007 | | size_t *volume_key_size, |
6008 | | const char *passphrase, |
6009 | | size_t passphrase_size) |
6010 | 0 | { |
6011 | 0 | int r; |
6012 | 0 | struct crypt_keyslot_context kc = {}; |
6013 | |
|
6014 | 0 | if (!passphrase) |
6015 | 0 | return crypt_volume_key_get_by_keyslot_context(cd, keyslot, volume_key, volume_key_size, NULL); |
6016 | | |
6017 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size); |
6018 | |
|
6019 | 0 | r = crypt_volume_key_get_by_keyslot_context(cd, keyslot, volume_key, volume_key_size, &kc); |
6020 | |
|
6021 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
6022 | |
|
6023 | 0 | return r; |
6024 | 0 | } |
6025 | | |
6026 | | int crypt_volume_key_get_by_keyslot_context(struct crypt_device *cd, |
6027 | | int keyslot, |
6028 | | char *volume_key, |
6029 | | size_t *volume_key_size, |
6030 | | struct crypt_keyslot_context *kc) |
6031 | 0 | { |
6032 | 0 | size_t passphrase_size; |
6033 | 0 | int key_len, r; |
6034 | 0 | const char *passphrase = NULL; |
6035 | 0 | struct volume_key *vk = NULL; |
6036 | |
|
6037 | 0 | if (!cd || !volume_key || !volume_key_size || |
6038 | 0 | (!kc && !isLUKS(cd->type) && !isTCRYPT(cd->type) && !isVERITY(cd->type) && !isBITLK(cd->type))) |
6039 | 0 | return -EINVAL; |
6040 | | |
6041 | 0 | if (isLUKS2(cd->type) && keyslot != CRYPT_ANY_SLOT) |
6042 | 0 | key_len = LUKS2_get_keyslot_stored_key_size(&cd->u.luks2.hdr, keyslot); |
6043 | 0 | else |
6044 | 0 | key_len = crypt_get_volume_key_size(cd); |
6045 | |
|
6046 | 0 | if (key_len < 0) |
6047 | 0 | return -EINVAL; |
6048 | | |
6049 | 0 | if (key_len > (int)*volume_key_size) { |
6050 | 0 | log_err(cd, _("Volume key buffer too small.")); |
6051 | 0 | return -ENOMEM; |
6052 | 0 | } |
6053 | | |
6054 | 0 | if (kc && (!kc->get_passphrase || kc->type == CRYPT_KC_TYPE_KEY)) |
6055 | 0 | return -EINVAL; |
6056 | | |
6057 | 0 | r = -EINVAL; |
6058 | |
|
6059 | 0 | if (isLUKS2(cd->type)) { |
6060 | 0 | if (kc && !kc->get_luks2_key) |
6061 | 0 | log_err(cd, _("Cannot retrieve volume key for LUKS2 device.")); |
6062 | 0 | else if (!kc) |
6063 | 0 | r = -ENOENT; |
6064 | 0 | else |
6065 | 0 | r = kc->get_luks2_key(cd, kc, keyslot, |
6066 | 0 | keyslot == CRYPT_ANY_SLOT ? CRYPT_DEFAULT_SEGMENT : CRYPT_ANY_SEGMENT, |
6067 | 0 | &vk); |
6068 | 0 | } else if (isLUKS1(cd->type)) { |
6069 | 0 | if (kc && !kc->get_luks1_volume_key) |
6070 | 0 | log_err(cd, _("Cannot retrieve volume key for LUKS1 device.")); |
6071 | 0 | else if (!kc) |
6072 | 0 | r = -ENOENT; |
6073 | 0 | else |
6074 | 0 | r = kc->get_luks1_volume_key(cd, kc, keyslot, &vk); |
6075 | 0 | } else if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) { |
6076 | 0 | if (kc && kc->get_passphrase && kc->type != CRYPT_KC_TYPE_TOKEN) { |
6077 | 0 | r = kc->get_passphrase(cd, kc, &passphrase, &passphrase_size); |
6078 | 0 | if (r < 0) |
6079 | 0 | return r; |
6080 | 0 | r = process_key(cd, cd->u.plain.hdr.hash, key_len, |
6081 | 0 | passphrase, passphrase_size, &vk); |
6082 | 0 | } |
6083 | 0 | if (r < 0) |
6084 | 0 | log_err(cd, _("Cannot retrieve volume key for plain device.")); |
6085 | 0 | } else if (isVERITY(cd->type)) { |
6086 | | /* volume_key == root hash */ |
6087 | 0 | if (cd->u.verity.root_hash) { |
6088 | 0 | crypt_safe_memcpy(volume_key, cd->u.verity.root_hash, cd->u.verity.root_hash_size); |
6089 | 0 | *volume_key_size = cd->u.verity.root_hash_size; |
6090 | 0 | r = 0; |
6091 | 0 | } else |
6092 | 0 | log_err(cd, _("Cannot retrieve root hash for verity device.")); |
6093 | 0 | } else if (isTCRYPT(cd->type)) { |
6094 | 0 | r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk); |
6095 | 0 | } else if (isBITLK(cd->type)) { |
6096 | 0 | if (kc && kc->get_bitlk_volume_key) |
6097 | 0 | r = kc->get_bitlk_volume_key(cd, kc, &cd->u.bitlk.params, &vk); |
6098 | 0 | else if (!kc) |
6099 | 0 | r = BITLK_get_volume_key(cd, NULL, 0, &cd->u.bitlk.params, &vk); |
6100 | 0 | if (r < 0) |
6101 | 0 | log_err(cd, _("Cannot retrieve volume key for BITLK device.")); |
6102 | 0 | } else if (isFVAULT2(cd->type)) { |
6103 | 0 | if (kc && kc->get_fvault2_volume_key) |
6104 | 0 | r = kc->get_fvault2_volume_key(cd, kc, &cd->u.fvault2.params, &vk); |
6105 | 0 | if (r < 0) |
6106 | 0 | log_err(cd, _("Cannot retrieve volume key for FVAULT2 device.")); |
6107 | 0 | } else |
6108 | 0 | log_err(cd, _("This operation is not supported for %s crypt device."), cd->type ?: "(none)"); |
6109 | | |
6110 | 0 | if (r == -ENOENT && isLUKS(cd->type) && cd->volume_key) { |
6111 | 0 | vk = crypt_alloc_volume_key(crypt_volume_key_length(cd->volume_key), |
6112 | 0 | crypt_volume_key_get_key(cd->volume_key)); |
6113 | 0 | r = vk ? 0 : -ENOMEM; |
6114 | 0 | } |
6115 | |
|
6116 | 0 | if (r >= 0 && vk) { |
6117 | 0 | crypt_safe_memcpy(volume_key, crypt_volume_key_get_key(vk), crypt_volume_key_length(vk)); |
6118 | 0 | *volume_key_size = crypt_volume_key_length(vk); |
6119 | 0 | } |
6120 | |
|
6121 | 0 | crypt_free_volume_key(vk); |
6122 | 0 | return r; |
6123 | 0 | } |
6124 | | |
6125 | | int crypt_volume_key_verify(struct crypt_device *cd, |
6126 | | const char *volume_key, |
6127 | | size_t volume_key_size) |
6128 | 0 | { |
6129 | 0 | struct volume_key *vk; |
6130 | 0 | int r; |
6131 | |
|
6132 | 0 | if ((r = onlyLUKSunrestricted(cd))) |
6133 | 0 | return r; |
6134 | | |
6135 | 0 | vk = crypt_alloc_volume_key(volume_key_size, volume_key); |
6136 | 0 | if (!vk) |
6137 | 0 | return -ENOMEM; |
6138 | | |
6139 | 0 | if (isLUKS1(cd->type)) |
6140 | 0 | r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk); |
6141 | 0 | else if (isLUKS2(cd->type)) |
6142 | 0 | r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk); |
6143 | 0 | else |
6144 | 0 | r = -EINVAL; |
6145 | |
|
6146 | 0 | crypt_free_volume_key(vk); |
6147 | |
|
6148 | 0 | return r >= 0 ? 0 : r; |
6149 | 0 | } |
6150 | | |
6151 | | /* |
6152 | | * RNG and memory locking |
6153 | | */ |
6154 | | void crypt_set_rng_type(struct crypt_device *cd, int rng_type) |
6155 | 0 | { |
6156 | 0 | if (!cd) |
6157 | 0 | return; |
6158 | | |
6159 | 0 | switch (rng_type) { |
6160 | 0 | case CRYPT_RNG_URANDOM: |
6161 | 0 | case CRYPT_RNG_RANDOM: |
6162 | 0 | log_dbg(cd, "RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom"); |
6163 | 0 | cd->rng_type = rng_type; |
6164 | 0 | } |
6165 | 0 | } |
6166 | | |
6167 | | int crypt_get_rng_type(struct crypt_device *cd) |
6168 | 0 | { |
6169 | 0 | if (!cd) |
6170 | 0 | return -EINVAL; |
6171 | | |
6172 | 0 | return cd->rng_type; |
6173 | 0 | } |
6174 | | |
6175 | | int crypt_memory_lock(struct crypt_device *cd, int lock) |
6176 | 0 | { |
6177 | 0 | UNUSED(cd); |
6178 | 0 | UNUSED(lock); |
6179 | |
|
6180 | 0 | return 0; |
6181 | 0 | } |
6182 | | |
6183 | | void crypt_set_compatibility(struct crypt_device *cd, uint32_t flags) |
6184 | 0 | { |
6185 | 0 | if (cd) |
6186 | 0 | cd->compatibility = flags; |
6187 | 0 | } |
6188 | | |
6189 | | uint32_t crypt_get_compatibility(struct crypt_device *cd) |
6190 | 0 | { |
6191 | 0 | if (cd) |
6192 | 0 | return cd->compatibility; |
6193 | | |
6194 | 0 | return 0; |
6195 | 0 | } |
6196 | | |
6197 | | /* |
6198 | | * Reporting |
6199 | | */ |
6200 | | crypt_status_info crypt_status(struct crypt_device *cd, const char *name) |
6201 | 0 | { |
6202 | 0 | int r; |
6203 | |
|
6204 | 0 | if (!name) |
6205 | 0 | return CRYPT_INVALID; |
6206 | | |
6207 | 0 | if (!cd) |
6208 | 0 | dm_backend_init(cd); |
6209 | |
|
6210 | 0 | r = dm_status_device(cd, name); |
6211 | |
|
6212 | 0 | if (!cd) |
6213 | 0 | dm_backend_exit(cd); |
6214 | |
|
6215 | 0 | if (r < 0 && r != -ENODEV) |
6216 | 0 | return CRYPT_INVALID; |
6217 | | |
6218 | 0 | if (r == 0) |
6219 | 0 | return CRYPT_ACTIVE; |
6220 | | |
6221 | 0 | if (r > 0) |
6222 | 0 | return CRYPT_BUSY; |
6223 | | |
6224 | 0 | return CRYPT_INACTIVE; |
6225 | 0 | } |
6226 | | |
6227 | | static int _luks_dump(struct crypt_device *cd) |
6228 | 0 | { |
6229 | 0 | int i; |
6230 | |
|
6231 | 0 | log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd)); |
6232 | 0 | log_std(cd, "Version: \t%" PRIu16 "\n", cd->u.luks1.hdr.version); |
6233 | 0 | log_std(cd, "Cipher name: \t%s\n", cd->u.luks1.hdr.cipherName); |
6234 | 0 | log_std(cd, "Cipher mode: \t%s\n", cd->u.luks1.hdr.cipherMode); |
6235 | 0 | log_std(cd, "Hash spec: \t%s\n", cd->u.luks1.hdr.hashSpec); |
6236 | 0 | log_std(cd, "Payload offset:\t%" PRIu32 "\n", cd->u.luks1.hdr.payloadOffset); |
6237 | 0 | log_std(cd, "MK bits: \t%" PRIu32 "\n", cd->u.luks1.hdr.keyBytes * 8); |
6238 | 0 | log_std(cd, "MK digest: \t"); |
6239 | 0 | crypt_log_hex(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ", 0, NULL); |
6240 | 0 | log_std(cd, "\n"); |
6241 | 0 | log_std(cd, "MK salt: \t"); |
6242 | 0 | crypt_log_hex(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ", 0, NULL); |
6243 | 0 | log_std(cd, "\n \t"); |
6244 | 0 | crypt_log_hex(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ", 0, NULL); |
6245 | 0 | log_std(cd, "\n"); |
6246 | 0 | log_std(cd, "MK iterations: \t%" PRIu32 "\n", cd->u.luks1.hdr.mkDigestIterations); |
6247 | 0 | log_std(cd, "UUID: \t%s\n\n", cd->u.luks1.hdr.uuid); |
6248 | 0 | for(i = 0; i < LUKS_NUMKEYS; i++) { |
6249 | 0 | if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) { |
6250 | 0 | log_std(cd, "Key Slot %d: ENABLED\n",i); |
6251 | 0 | log_std(cd, "\tIterations: \t%" PRIu32 "\n", |
6252 | 0 | cd->u.luks1.hdr.keyblock[i].passwordIterations); |
6253 | 0 | log_std(cd, "\tSalt: \t"); |
6254 | 0 | crypt_log_hex(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt, |
6255 | 0 | LUKS_SALTSIZE/2, " ", 0, NULL); |
6256 | 0 | log_std(cd, "\n\t \t"); |
6257 | 0 | crypt_log_hex(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt + |
6258 | 0 | LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ", 0, NULL); |
6259 | 0 | log_std(cd, "\n"); |
6260 | |
|
6261 | 0 | log_std(cd, "\tKey material offset:\t%" PRIu32 "\n", |
6262 | 0 | cd->u.luks1.hdr.keyblock[i].keyMaterialOffset); |
6263 | 0 | log_std(cd, "\tAF stripes: \t%" PRIu32 "\n", |
6264 | 0 | cd->u.luks1.hdr.keyblock[i].stripes); |
6265 | 0 | } |
6266 | 0 | else |
6267 | 0 | log_std(cd, "Key Slot %d: DISABLED\n", i); |
6268 | 0 | } |
6269 | 0 | return 0; |
6270 | 0 | } |
6271 | | |
6272 | | int crypt_dump(struct crypt_device *cd) |
6273 | 0 | { |
6274 | 0 | if (!cd) |
6275 | 0 | return -EINVAL; |
6276 | 0 | if (isLUKS1(cd->type)) |
6277 | 0 | return _luks_dump(cd); |
6278 | 0 | else if (isLUKS2(cd->type)) |
6279 | 0 | return LUKS2_hdr_dump(cd, &cd->u.luks2.hdr); |
6280 | 0 | else if (isVERITY(cd->type)) |
6281 | 0 | return VERITY_dump(cd, &cd->u.verity.hdr, |
6282 | 0 | cd->u.verity.root_hash, cd->u.verity.root_hash_size, |
6283 | 0 | cd->u.verity.fec_device); |
6284 | 0 | else if (isTCRYPT(cd->type)) |
6285 | 0 | return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params); |
6286 | 0 | else if (isINTEGRITY(cd->type)) |
6287 | 0 | return INTEGRITY_dump(cd, crypt_data_device(cd), 0); |
6288 | 0 | else if (isBITLK(cd->type)) |
6289 | 0 | return BITLK_dump(cd, crypt_data_device(cd), &cd->u.bitlk.params); |
6290 | 0 | else if (isFVAULT2(cd->type)) |
6291 | 0 | return FVAULT2_dump(cd, crypt_data_device(cd), &cd->u.fvault2.params); |
6292 | | |
6293 | 0 | log_err(cd, _("Dump operation is not supported for this device type.")); |
6294 | 0 | return -EINVAL; |
6295 | 0 | } |
6296 | | |
6297 | | int crypt_dump_json(struct crypt_device *cd, const char **json, uint32_t flags) |
6298 | 0 | { |
6299 | 0 | if (!cd || flags) |
6300 | 0 | return -EINVAL; |
6301 | 0 | if (isLUKS2(cd->type)) |
6302 | 0 | return LUKS2_hdr_dump_json(cd, &cd->u.luks2.hdr, json); |
6303 | | |
6304 | 0 | log_err(cd, _("Dump operation is not supported for this device type.")); |
6305 | 0 | return -EINVAL; |
6306 | 0 | } |
6307 | | |
6308 | | /* internal only */ |
6309 | | const char *crypt_get_cipher_spec(struct crypt_device *cd) |
6310 | 0 | { |
6311 | 0 | if (!cd) |
6312 | 0 | return NULL; |
6313 | 0 | else if (isLUKS2(cd->type)) |
6314 | 0 | return LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT); |
6315 | 0 | else if (isLUKS1(cd->type)) |
6316 | 0 | return cd->u.luks1.cipher_spec; |
6317 | 0 | else if (isPLAIN(cd->type)) |
6318 | 0 | return cd->u.plain.cipher_spec; |
6319 | 0 | else if (isLOOPAES(cd->type)) |
6320 | 0 | return cd->u.loopaes.cipher_spec; |
6321 | 0 | else if (isBITLK(cd->type)) |
6322 | 0 | return cd->u.bitlk.cipher_spec; |
6323 | 0 | else if (!cd->type && !_init_by_name_crypt_none(cd)) |
6324 | 0 | return cd->u.none.cipher_spec; |
6325 | | |
6326 | 0 | return NULL; |
6327 | 0 | } |
6328 | | |
6329 | | const char *crypt_get_cipher(struct crypt_device *cd) |
6330 | 0 | { |
6331 | 0 | if (!cd) |
6332 | 0 | return NULL; |
6333 | | |
6334 | 0 | if (isPLAIN(cd->type)) |
6335 | 0 | return cd->u.plain.cipher; |
6336 | | |
6337 | 0 | if (isLUKS1(cd->type)) |
6338 | 0 | return cd->u.luks1.hdr.cipherName; |
6339 | | |
6340 | 0 | if (isLUKS2(cd->type)) { |
6341 | 0 | if (crypt_parse_name_and_mode(LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT), |
6342 | 0 | cd->u.luks2.cipher, NULL, cd->u.luks2.cipher_mode)) |
6343 | 0 | return NULL; |
6344 | 0 | return cd->u.luks2.cipher; |
6345 | 0 | } |
6346 | | |
6347 | 0 | if (isLOOPAES(cd->type)) |
6348 | 0 | return cd->u.loopaes.cipher; |
6349 | | |
6350 | 0 | if (isTCRYPT(cd->type)) |
6351 | 0 | return cd->u.tcrypt.params.cipher; |
6352 | | |
6353 | 0 | if (isBITLK(cd->type)) |
6354 | 0 | return cd->u.bitlk.params.cipher; |
6355 | | |
6356 | 0 | if (isFVAULT2(cd->type)) |
6357 | 0 | return cd->u.fvault2.params.cipher; |
6358 | | |
6359 | 0 | if (!cd->type && !_init_by_name_crypt_none(cd)) |
6360 | 0 | return cd->u.none.cipher; |
6361 | | |
6362 | 0 | return NULL; |
6363 | 0 | } |
6364 | | |
6365 | | const char *crypt_get_cipher_mode(struct crypt_device *cd) |
6366 | 0 | { |
6367 | 0 | if (!cd) |
6368 | 0 | return NULL; |
6369 | | |
6370 | 0 | if (isPLAIN(cd->type)) |
6371 | 0 | return cd->u.plain.cipher_mode; |
6372 | | |
6373 | 0 | if (isLUKS1(cd->type)) |
6374 | 0 | return cd->u.luks1.hdr.cipherMode; |
6375 | | |
6376 | 0 | if (isLUKS2(cd->type)) { |
6377 | 0 | if (crypt_parse_name_and_mode(LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT), |
6378 | 0 | cd->u.luks2.cipher, NULL, cd->u.luks2.cipher_mode)) |
6379 | 0 | return NULL; |
6380 | 0 | return cd->u.luks2.cipher_mode; |
6381 | 0 | } |
6382 | | |
6383 | 0 | if (isLOOPAES(cd->type)) |
6384 | 0 | return cd->u.loopaes.cipher_mode; |
6385 | | |
6386 | 0 | if (isTCRYPT(cd->type)) |
6387 | 0 | return cd->u.tcrypt.params.mode; |
6388 | | |
6389 | 0 | if (isBITLK(cd->type)) |
6390 | 0 | return cd->u.bitlk.params.cipher_mode; |
6391 | | |
6392 | 0 | if (isFVAULT2(cd->type)) |
6393 | 0 | return cd->u.fvault2.params.cipher_mode; |
6394 | | |
6395 | 0 | if (!cd->type && !_init_by_name_crypt_none(cd)) |
6396 | 0 | return cd->u.none.cipher_mode; |
6397 | | |
6398 | 0 | return NULL; |
6399 | 0 | } |
6400 | | |
6401 | | /* INTERNAL only */ |
6402 | | const char *crypt_get_integrity(struct crypt_device *cd) |
6403 | 0 | { |
6404 | 0 | if (!cd) |
6405 | 0 | return NULL; |
6406 | | |
6407 | 0 | if (isINTEGRITY(cd->type)) |
6408 | 0 | return cd->u.integrity.params.integrity; |
6409 | | |
6410 | 0 | if (isLUKS2(cd->type)) |
6411 | 0 | return LUKS2_get_integrity(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT); |
6412 | | |
6413 | 0 | if (!cd->type && *cd->u.none.integrity_spec) |
6414 | 0 | return cd->u.none.integrity_spec; |
6415 | | |
6416 | 0 | return NULL; |
6417 | 0 | } |
6418 | | |
6419 | | /* INTERNAL only */ |
6420 | | int crypt_get_integrity_key_size(struct crypt_device *cd, bool dm_compat) |
6421 | 0 | { |
6422 | 0 | int key_size = 0; |
6423 | |
|
6424 | 0 | if (isLUKS2(cd->type)) { |
6425 | 0 | key_size = INTEGRITY_key_size(crypt_get_integrity(cd), |
6426 | 0 | LUKS2_get_integrity_key_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)); |
6427 | 0 | if (dm_compat && key_size > 0 && |
6428 | 0 | key_size == INTEGRITY_key_size(crypt_get_integrity(cd), 0)) |
6429 | 0 | return 0; |
6430 | 0 | } |
6431 | | |
6432 | 0 | if (isINTEGRITY(cd->type) || !cd->type) |
6433 | 0 | key_size = INTEGRITY_key_size(crypt_get_integrity(cd), 0); |
6434 | |
|
6435 | 0 | return key_size > 0 ? key_size : 0; |
6436 | 0 | } |
6437 | | |
6438 | | /* INTERNAL only */ |
6439 | | int crypt_get_integrity_tag_size(struct crypt_device *cd) |
6440 | 0 | { |
6441 | 0 | if (isINTEGRITY(cd->type)) |
6442 | 0 | return cd->u.integrity.params.tag_size; |
6443 | | |
6444 | 0 | if (isLUKS2(cd->type) || !cd->type) |
6445 | 0 | return INTEGRITY_tag_size(crypt_get_integrity(cd), |
6446 | 0 | crypt_get_cipher(cd), |
6447 | 0 | crypt_get_cipher_mode(cd)); |
6448 | 0 | return 0; |
6449 | 0 | } |
6450 | | |
6451 | | int crypt_get_sector_size(struct crypt_device *cd) |
6452 | 0 | { |
6453 | 0 | if (!cd) |
6454 | 0 | return SECTOR_SIZE; |
6455 | | |
6456 | 0 | if (isPLAIN(cd->type)) |
6457 | 0 | return cd->u.plain.hdr.sector_size; |
6458 | | |
6459 | 0 | if (isINTEGRITY(cd->type)) |
6460 | 0 | return cd->u.integrity.params.sector_size; |
6461 | | |
6462 | 0 | if (isLUKS2(cd->type)) |
6463 | 0 | return LUKS2_get_sector_size(&cd->u.luks2.hdr); |
6464 | | |
6465 | 0 | if (!cd->type && cd->u.none.sector_size) |
6466 | 0 | return cd->u.none.sector_size; |
6467 | | |
6468 | 0 | return SECTOR_SIZE; |
6469 | 0 | } |
6470 | | |
6471 | | const char *crypt_get_uuid(struct crypt_device *cd) |
6472 | 0 | { |
6473 | 0 | if (!cd) |
6474 | 0 | return NULL; |
6475 | | |
6476 | 0 | if (isLUKS1(cd->type)) |
6477 | 0 | return cd->u.luks1.hdr.uuid; |
6478 | | |
6479 | 0 | if (isLUKS2(cd->type)) |
6480 | 0 | return cd->u.luks2.hdr.uuid; |
6481 | | |
6482 | 0 | if (isVERITY(cd->type)) |
6483 | 0 | return cd->u.verity.uuid; |
6484 | | |
6485 | 0 | if (isBITLK(cd->type)) |
6486 | 0 | return cd->u.bitlk.params.guid; |
6487 | | |
6488 | 0 | if (isFVAULT2(cd->type)) |
6489 | 0 | return cd->u.fvault2.params.family_uuid; |
6490 | | |
6491 | 0 | return NULL; |
6492 | 0 | } |
6493 | | |
6494 | | const char *crypt_get_device_name(struct crypt_device *cd) |
6495 | 0 | { |
6496 | 0 | const char *path; |
6497 | |
|
6498 | 0 | if (!cd) |
6499 | 0 | return NULL; |
6500 | | |
6501 | 0 | path = device_block_path(cd->device); |
6502 | 0 | if (!path) |
6503 | 0 | path = device_path(cd->device); |
6504 | |
|
6505 | 0 | return path; |
6506 | 0 | } |
6507 | | |
6508 | | const char *crypt_get_metadata_device_name(struct crypt_device *cd) |
6509 | 0 | { |
6510 | 0 | const char *path; |
6511 | |
|
6512 | 0 | if (!cd || !cd->metadata_device) |
6513 | 0 | return NULL; |
6514 | | |
6515 | 0 | path = device_block_path(cd->metadata_device); |
6516 | 0 | if (!path) |
6517 | 0 | path = device_path(cd->metadata_device); |
6518 | |
|
6519 | 0 | return path; |
6520 | 0 | } |
6521 | | |
6522 | | int crypt_get_volume_key_size(struct crypt_device *cd) |
6523 | 0 | { |
6524 | 0 | int r; |
6525 | |
|
6526 | 0 | if (!cd) |
6527 | 0 | return 0; |
6528 | | |
6529 | 0 | if (isPLAIN(cd->type)) |
6530 | 0 | return cd->u.plain.key_size; |
6531 | | |
6532 | 0 | if (isLUKS1(cd->type)) |
6533 | 0 | return cd->u.luks1.hdr.keyBytes; |
6534 | | |
6535 | 0 | if (isLUKS2(cd->type)) { |
6536 | 0 | r = LUKS2_get_volume_key_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT); |
6537 | 0 | if (r < 0 && cd->volume_key) |
6538 | 0 | r = crypt_volume_key_length(cd->volume_key); |
6539 | 0 | return r < 0 ? 0 : r; |
6540 | 0 | } |
6541 | | |
6542 | 0 | if (isLOOPAES(cd->type)) |
6543 | 0 | return cd->u.loopaes.key_size; |
6544 | | |
6545 | 0 | if (isVERITY(cd->type)) |
6546 | 0 | return cd->u.verity.root_hash_size; |
6547 | | |
6548 | 0 | if (isTCRYPT(cd->type)) |
6549 | 0 | return cd->u.tcrypt.params.key_size; |
6550 | | |
6551 | 0 | if (isBITLK(cd->type)) |
6552 | 0 | return cd->u.bitlk.params.key_size / 8; |
6553 | | |
6554 | 0 | if (isFVAULT2(cd->type)) |
6555 | 0 | return cd->u.fvault2.params.key_size; |
6556 | | |
6557 | 0 | if (!cd->type && !_init_by_name_crypt_none(cd)) |
6558 | 0 | return cd->u.none.key_size; |
6559 | | |
6560 | 0 | return 0; |
6561 | 0 | } |
6562 | | |
6563 | | int crypt_get_old_volume_key_size(struct crypt_device *cd) |
6564 | 0 | { |
6565 | 0 | int r = _onlyLUKS2(cd, CRYPT_CD_QUIET, |
6566 | 0 | CRYPT_REQUIREMENT_ONLINE_REENCRYPT | CRYPT_REQUIREMENT_OPAL); |
6567 | |
|
6568 | 0 | if (r < 0) |
6569 | 0 | return 0; |
6570 | | |
6571 | 0 | r = LUKS2_get_old_volume_key_size(&cd->u.luks2.hdr); |
6572 | |
|
6573 | 0 | return r < 0 ? 0 : r; |
6574 | 0 | } |
6575 | | |
6576 | | int crypt_get_hw_encryption_key_size(struct crypt_device *cd) |
6577 | 0 | { |
6578 | 0 | if (!cd || !isLUKS2(cd->type)) |
6579 | 0 | return 0; |
6580 | | |
6581 | 0 | return LUKS2_get_opal_key_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT); |
6582 | 0 | } |
6583 | | |
6584 | | int crypt_keyslot_get_key_size(struct crypt_device *cd, int keyslot) |
6585 | 0 | { |
6586 | 0 | if (!cd || !isLUKS(cd->type)) |
6587 | 0 | return -EINVAL; |
6588 | | |
6589 | 0 | if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type)) |
6590 | 0 | return -EINVAL; |
6591 | | |
6592 | 0 | if (isLUKS1(cd->type)) |
6593 | 0 | return cd->u.luks1.hdr.keyBytes; |
6594 | | |
6595 | 0 | if (isLUKS2(cd->type)) |
6596 | 0 | return LUKS2_get_keyslot_stored_key_size(&cd->u.luks2.hdr, keyslot); |
6597 | | |
6598 | 0 | return -EINVAL; |
6599 | 0 | } |
6600 | | |
6601 | | int crypt_keyslot_set_encryption(struct crypt_device *cd, |
6602 | | const char *cipher, |
6603 | | size_t key_size) |
6604 | 0 | { |
6605 | 0 | char *tmp; |
6606 | |
|
6607 | 0 | if (!cd || !cipher || !key_size || !isLUKS2(cd->type)) |
6608 | 0 | return -EINVAL; |
6609 | | |
6610 | 0 | if (LUKS2_keyslot_cipher_incompatible(cd, cipher)) |
6611 | 0 | return -EINVAL; |
6612 | | |
6613 | 0 | if (!(tmp = strdup(cipher))) |
6614 | 0 | return -ENOMEM; |
6615 | | |
6616 | 0 | free(cd->u.luks2.keyslot_cipher); |
6617 | 0 | cd->u.luks2.keyslot_cipher = tmp; |
6618 | 0 | cd->u.luks2.keyslot_key_size = key_size; |
6619 | |
|
6620 | 0 | return 0; |
6621 | 0 | } |
6622 | | |
6623 | | const char *crypt_keyslot_get_encryption(struct crypt_device *cd, int keyslot, size_t *key_size) |
6624 | 0 | { |
6625 | 0 | const char *cipher; |
6626 | |
|
6627 | 0 | if (!cd || !isLUKS(cd->type) || !key_size) |
6628 | 0 | return NULL; |
6629 | | |
6630 | 0 | if (isLUKS1(cd->type)) { |
6631 | 0 | if (keyslot != CRYPT_ANY_SLOT && |
6632 | 0 | LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot) < CRYPT_SLOT_ACTIVE) |
6633 | 0 | return NULL; |
6634 | 0 | *key_size = crypt_get_volume_key_size(cd); |
6635 | 0 | return cd->u.luks1.cipher_spec; |
6636 | 0 | } |
6637 | | |
6638 | 0 | if (keyslot != CRYPT_ANY_SLOT) |
6639 | 0 | return LUKS2_get_keyslot_cipher(&cd->u.luks2.hdr, keyslot, key_size); |
6640 | | |
6641 | | /* Keyslot encryption was set through crypt_keyslot_set_encryption() */ |
6642 | 0 | if (cd->u.luks2.keyslot_cipher) { |
6643 | 0 | *key_size = cd->u.luks2.keyslot_key_size; |
6644 | 0 | return cd->u.luks2.keyslot_cipher; |
6645 | 0 | } |
6646 | | |
6647 | 0 | if (LUKS2_segment_is_hw_opal(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)) { |
6648 | | /* Fallback to default LUKS2 keyslot encryption */ |
6649 | 0 | *key_size = DEFAULT_LUKS2_KEYSLOT_KEYBITS / 8; |
6650 | 0 | return DEFAULT_LUKS2_KEYSLOT_CIPHER; |
6651 | 0 | } |
6652 | | |
6653 | | /* Try to reuse volume encryption parameters */ |
6654 | 0 | cipher = LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT); |
6655 | 0 | if (!LUKS2_keyslot_cipher_incompatible(cd, cipher)) { |
6656 | 0 | *key_size = crypt_get_volume_key_size(cd); |
6657 | 0 | if (*key_size) |
6658 | 0 | return cipher; |
6659 | 0 | } |
6660 | | |
6661 | | /* Fallback to default LUKS2 keyslot encryption */ |
6662 | 0 | *key_size = DEFAULT_LUKS2_KEYSLOT_KEYBITS / 8; |
6663 | 0 | return DEFAULT_LUKS2_KEYSLOT_CIPHER; |
6664 | 0 | } |
6665 | | |
6666 | | int crypt_keyslot_get_pbkdf(struct crypt_device *cd, int keyslot, struct crypt_pbkdf_type *pbkdf) |
6667 | 0 | { |
6668 | 0 | if (!cd || !pbkdf || keyslot == CRYPT_ANY_SLOT) |
6669 | 0 | return -EINVAL; |
6670 | | |
6671 | 0 | if (isLUKS1(cd->type)) |
6672 | 0 | return LUKS_keyslot_pbkdf(&cd->u.luks1.hdr, keyslot, pbkdf); |
6673 | 0 | else if (isLUKS2(cd->type)) |
6674 | 0 | return LUKS2_keyslot_pbkdf(&cd->u.luks2.hdr, keyslot, pbkdf); |
6675 | | |
6676 | 0 | return -EINVAL; |
6677 | 0 | } |
6678 | | |
6679 | | int crypt_set_data_offset(struct crypt_device *cd, uint64_t data_offset) |
6680 | 0 | { |
6681 | 0 | if (!cd) |
6682 | 0 | return -EINVAL; |
6683 | 0 | if (data_offset % (MAX_SECTOR_SIZE >> SECTOR_SHIFT)) { |
6684 | 0 | log_err(cd, _("Data offset is not multiple of %u bytes."), MAX_SECTOR_SIZE); |
6685 | 0 | return -EINVAL; |
6686 | 0 | } |
6687 | | |
6688 | 0 | cd->data_offset = data_offset; |
6689 | 0 | log_dbg(cd, "Data offset set to %" PRIu64 " (512-byte) sectors.", data_offset); |
6690 | |
|
6691 | 0 | return 0; |
6692 | 0 | } |
6693 | | |
6694 | | int crypt_set_metadata_size(struct crypt_device *cd, |
6695 | | uint64_t metadata_size, |
6696 | | uint64_t keyslots_size) |
6697 | 0 | { |
6698 | 0 | if (!cd) |
6699 | 0 | return -EINVAL; |
6700 | | |
6701 | 0 | if (cd->type && !isLUKS2(cd->type)) |
6702 | 0 | return -EINVAL; |
6703 | | |
6704 | 0 | if (metadata_size && LUKS2_check_metadata_area_size(metadata_size)) |
6705 | 0 | return -EINVAL; |
6706 | | |
6707 | 0 | if (keyslots_size && LUKS2_check_keyslots_area_size(keyslots_size)) |
6708 | 0 | return -EINVAL; |
6709 | | |
6710 | 0 | cd->metadata_size = metadata_size; |
6711 | 0 | cd->keyslots_size = keyslots_size; |
6712 | |
|
6713 | 0 | return 0; |
6714 | 0 | } |
6715 | | |
6716 | | int crypt_get_metadata_size(struct crypt_device *cd, |
6717 | | uint64_t *metadata_size, |
6718 | | uint64_t *keyslots_size) |
6719 | 0 | { |
6720 | 0 | uint64_t msize, ksize; |
6721 | |
|
6722 | 0 | if (!cd) |
6723 | 0 | return -EINVAL; |
6724 | | |
6725 | 0 | if (!cd->type) { |
6726 | 0 | msize = cd->metadata_size; |
6727 | 0 | ksize = cd->keyslots_size; |
6728 | 0 | } else if (isLUKS1(cd->type)) { |
6729 | 0 | msize = LUKS_ALIGN_KEYSLOTS; |
6730 | 0 | ksize = LUKS_device_sectors(&cd->u.luks1.hdr) * SECTOR_SIZE - msize; |
6731 | 0 | } else if (isLUKS2(cd->type)) { |
6732 | 0 | msize = LUKS2_metadata_size(&cd->u.luks2.hdr); |
6733 | 0 | ksize = LUKS2_keyslots_size(&cd->u.luks2.hdr); |
6734 | 0 | } else |
6735 | 0 | return -EINVAL; |
6736 | | |
6737 | 0 | if (metadata_size) |
6738 | 0 | *metadata_size = msize; |
6739 | 0 | if (keyslots_size) |
6740 | 0 | *keyslots_size = ksize; |
6741 | |
|
6742 | 0 | return 0; |
6743 | 0 | } |
6744 | | |
6745 | | uint64_t crypt_get_data_offset(struct crypt_device *cd) |
6746 | 0 | { |
6747 | 0 | if (!cd) |
6748 | 0 | return 0; |
6749 | | |
6750 | 0 | if (isPLAIN(cd->type)) |
6751 | 0 | return cd->u.plain.hdr.offset; |
6752 | | |
6753 | 0 | if (isLUKS1(cd->type)) |
6754 | 0 | return cd->u.luks1.hdr.payloadOffset; |
6755 | | |
6756 | 0 | if (isLUKS2(cd->type)) |
6757 | 0 | return LUKS2_get_data_offset(&cd->u.luks2.hdr); |
6758 | | |
6759 | 0 | if (isLOOPAES(cd->type)) |
6760 | 0 | return cd->u.loopaes.hdr.offset; |
6761 | | |
6762 | 0 | if (isTCRYPT(cd->type)) |
6763 | 0 | return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params); |
6764 | | |
6765 | 0 | if (isBITLK(cd->type)) |
6766 | 0 | return cd->u.bitlk.params.volume_header_size / SECTOR_SIZE; |
6767 | | |
6768 | 0 | if (isFVAULT2(cd->type)) |
6769 | 0 | return cd->u.fvault2.params.log_vol_off / SECTOR_SIZE; |
6770 | | |
6771 | 0 | return cd->data_offset; |
6772 | 0 | } |
6773 | | |
6774 | | uint64_t crypt_get_iv_offset(struct crypt_device *cd) |
6775 | 0 | { |
6776 | 0 | if (!cd) |
6777 | 0 | return 0; |
6778 | | |
6779 | 0 | if (isPLAIN(cd->type)) |
6780 | 0 | return cd->u.plain.hdr.skip; |
6781 | | |
6782 | 0 | if (isLOOPAES(cd->type)) |
6783 | 0 | return cd->u.loopaes.hdr.skip; |
6784 | | |
6785 | 0 | if (isTCRYPT(cd->type)) |
6786 | 0 | return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params); |
6787 | | |
6788 | 0 | return 0; |
6789 | 0 | } |
6790 | | |
6791 | | crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot) |
6792 | 0 | { |
6793 | 0 | if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0) < 0) |
6794 | 0 | return CRYPT_SLOT_INVALID; |
6795 | | |
6796 | 0 | if (isLUKS1(cd->type)) |
6797 | 0 | return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot); |
6798 | 0 | else if(isLUKS2(cd->type)) |
6799 | 0 | return LUKS2_keyslot_info(&cd->u.luks2.hdr, keyslot); |
6800 | | |
6801 | 0 | return CRYPT_SLOT_INVALID; |
6802 | 0 | } |
6803 | | |
6804 | | int crypt_keyslot_max(const char *type) |
6805 | 0 | { |
6806 | 0 | if (isLUKS1(type)) |
6807 | 0 | return LUKS_NUMKEYS; |
6808 | | |
6809 | 0 | if (isLUKS2(type)) |
6810 | 0 | return LUKS2_KEYSLOTS_MAX; |
6811 | | |
6812 | 0 | return -EINVAL; |
6813 | 0 | } |
6814 | | |
6815 | | int crypt_keyslot_area(struct crypt_device *cd, |
6816 | | int keyslot, |
6817 | | uint64_t *offset, |
6818 | | uint64_t *length) |
6819 | 0 | { |
6820 | 0 | if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0) || !offset || !length) |
6821 | 0 | return -EINVAL; |
6822 | | |
6823 | 0 | if (isLUKS2(cd->type)) |
6824 | 0 | return LUKS2_keyslot_area(&cd->u.luks2.hdr, keyslot, offset, length); |
6825 | | |
6826 | 0 | return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length); |
6827 | 0 | } |
6828 | | |
6829 | | crypt_keyslot_priority crypt_keyslot_get_priority(struct crypt_device *cd, int keyslot) |
6830 | 0 | { |
6831 | 0 | if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0)) |
6832 | 0 | return CRYPT_SLOT_PRIORITY_INVALID; |
6833 | | |
6834 | 0 | if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type)) |
6835 | 0 | return CRYPT_SLOT_PRIORITY_INVALID; |
6836 | | |
6837 | 0 | if (isLUKS2(cd->type)) |
6838 | 0 | return LUKS2_keyslot_priority_get(&cd->u.luks2.hdr, keyslot); |
6839 | | |
6840 | 0 | return CRYPT_SLOT_PRIORITY_NORMAL; |
6841 | 0 | } |
6842 | | |
6843 | | int crypt_keyslot_set_priority(struct crypt_device *cd, int keyslot, crypt_keyslot_priority priority) |
6844 | 0 | { |
6845 | 0 | int r; |
6846 | |
|
6847 | 0 | log_dbg(cd, "Setting keyslot %d to priority %d.", keyslot, priority); |
6848 | |
|
6849 | 0 | if (priority == CRYPT_SLOT_PRIORITY_INVALID) |
6850 | 0 | return -EINVAL; |
6851 | | |
6852 | 0 | if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type)) |
6853 | 0 | return -EINVAL; |
6854 | | |
6855 | 0 | if ((r = onlyLUKS2(cd))) |
6856 | 0 | return r; |
6857 | | |
6858 | 0 | return LUKS2_keyslot_priority_set(cd, &cd->u.luks2.hdr, keyslot, priority, 1); |
6859 | 0 | } |
6860 | | |
6861 | | const char *crypt_get_type(struct crypt_device *cd) |
6862 | 4.01k | { |
6863 | 4.01k | return cd ? cd->type : NULL; |
6864 | 4.01k | } |
6865 | | |
6866 | | const char *crypt_get_default_type(void) |
6867 | 0 | { |
6868 | 0 | return DEFAULT_LUKS_FORMAT; |
6869 | 0 | } |
6870 | | |
6871 | | int crypt_get_type_defaults(const char *type, struct crypt_type_defaults *defaults) |
6872 | 0 | { |
6873 | 0 | if (!type || !isLUKS(type) || !defaults) |
6874 | 0 | return -EINVAL; |
6875 | | |
6876 | 0 | memset(defaults, 0, sizeof(*defaults)); |
6877 | |
|
6878 | 0 | defaults->cipher = DEFAULT_LUKS1_CIPHER; |
6879 | 0 | defaults->cipher_mode = DEFAULT_LUKS1_MODE; |
6880 | 0 | defaults->hash = DEFAULT_LUKS1_HASH; |
6881 | 0 | defaults->key_size = DEFAULT_LUKS1_KEYBITS; |
6882 | |
|
6883 | 0 | if (isLUKS2(type)) { |
6884 | 0 | defaults->integrity = "hmac-sha256"; |
6885 | 0 | defaults->tag_size = 32; |
6886 | 0 | } |
6887 | 0 | return 0; |
6888 | 0 | } |
6889 | | |
6890 | | int crypt_get_hw_encryption_type(struct crypt_device *cd) |
6891 | 0 | { |
6892 | 0 | if (!cd) |
6893 | 0 | return -EINVAL; |
6894 | | |
6895 | 0 | if (isLUKS2(cd->type)) { |
6896 | 0 | if (LUKS2_segment_is_hw_opal_crypt(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)) |
6897 | 0 | return CRYPT_SW_AND_OPAL_HW; |
6898 | 0 | else if (LUKS2_segment_is_hw_opal_only(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)) |
6899 | 0 | return CRYPT_OPAL_HW_ONLY; |
6900 | 0 | } |
6901 | | |
6902 | 0 | return CRYPT_SW_ONLY; |
6903 | 0 | } |
6904 | | |
6905 | | int crypt_get_hw_opal_sum_enabled(struct crypt_device* cd) |
6906 | 0 | { |
6907 | 0 | uint8_t version; |
6908 | |
|
6909 | 0 | if (!cd) |
6910 | 0 | return -EINVAL; |
6911 | | |
6912 | 0 | if (!isLUKS2(cd->type)) |
6913 | 0 | return -ENOTSUP; |
6914 | | |
6915 | | /* No Opal flag present */ |
6916 | 0 | if (LUKS2_config_get_opal_version(&cd->u.luks2.hdr, &version) < 0) |
6917 | 0 | return -ENOTSUP; |
6918 | | |
6919 | 0 | return version > 1 ? 1 : 0; |
6920 | 0 | } |
6921 | | |
6922 | | int crypt_get_verity_info(struct crypt_device *cd, |
6923 | | struct crypt_params_verity *vp) |
6924 | 0 | { |
6925 | 0 | if (!cd || !isVERITY(cd->type) || !vp) |
6926 | 0 | return -EINVAL; |
6927 | | |
6928 | 0 | vp->data_device = device_path(cd->device); |
6929 | 0 | vp->hash_device = mdata_device_path(cd); |
6930 | 0 | vp->fec_device = device_path(cd->u.verity.fec_device); |
6931 | 0 | vp->fec_area_offset = cd->u.verity.hdr.fec_area_offset; |
6932 | 0 | vp->fec_roots = cd->u.verity.hdr.fec_roots; |
6933 | 0 | vp->hash_name = cd->u.verity.hdr.hash_name; |
6934 | 0 | vp->salt = cd->u.verity.hdr.salt; |
6935 | 0 | vp->salt_size = cd->u.verity.hdr.salt_size; |
6936 | 0 | vp->data_block_size = cd->u.verity.hdr.data_block_size; |
6937 | 0 | vp->hash_block_size = cd->u.verity.hdr.hash_block_size; |
6938 | 0 | vp->data_size = cd->u.verity.hdr.data_size; |
6939 | 0 | vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset; |
6940 | 0 | vp->hash_type = cd->u.verity.hdr.hash_type; |
6941 | 0 | vp->flags = cd->u.verity.hdr.flags & (CRYPT_VERITY_NO_HEADER | CRYPT_VERITY_ROOT_HASH_SIGNATURE); |
6942 | 0 | return 0; |
6943 | 0 | } |
6944 | | |
6945 | | int crypt_get_verity_repaired(struct crypt_device *cd, const char *name, |
6946 | | uint64_t *repaired) |
6947 | | |
6948 | 0 | { |
6949 | 0 | if (!cd || !isVERITY(cd->type) || !name || !repaired) |
6950 | 0 | return -EINVAL; |
6951 | | |
6952 | 0 | return dm_status_verity_repaired(cd, name, repaired); |
6953 | 0 | } |
6954 | | |
6955 | | int crypt_get_integrity_info(struct crypt_device *cd, |
6956 | | struct crypt_params_integrity *ip) |
6957 | 0 | { |
6958 | 0 | if (!cd || !ip) |
6959 | 0 | return -EINVAL; |
6960 | | |
6961 | 0 | if (isINTEGRITY(cd->type)) { |
6962 | 0 | ip->journal_size = cd->u.integrity.params.journal_size; |
6963 | 0 | ip->journal_watermark = cd->u.integrity.params.journal_watermark; |
6964 | 0 | ip->journal_commit_time = cd->u.integrity.params.journal_commit_time; |
6965 | 0 | ip->interleave_sectors = cd->u.integrity.params.interleave_sectors; |
6966 | 0 | ip->tag_size = cd->u.integrity.params.tag_size; |
6967 | 0 | ip->sector_size = cd->u.integrity.params.sector_size; |
6968 | 0 | ip->buffer_sectors = cd->u.integrity.params.buffer_sectors; |
6969 | |
|
6970 | 0 | ip->integrity = cd->u.integrity.params.integrity; |
6971 | 0 | ip->integrity_key_size = crypt_get_integrity_key_size(cd, false); |
6972 | |
|
6973 | 0 | ip->journal_integrity = cd->u.integrity.params.journal_integrity; |
6974 | 0 | ip->journal_integrity_key_size = cd->u.integrity.params.journal_integrity_key_size; |
6975 | 0 | ip->journal_integrity_key = NULL; |
6976 | |
|
6977 | 0 | ip->journal_crypt = cd->u.integrity.params.journal_crypt; |
6978 | 0 | ip->journal_crypt_key_size = cd->u.integrity.params.journal_crypt_key_size; |
6979 | 0 | ip->journal_crypt_key = NULL; |
6980 | 0 | return 0; |
6981 | 0 | } else if (isLUKS2(cd->type)) { |
6982 | 0 | ip->journal_size = 0; // FIXME |
6983 | 0 | ip->journal_watermark = 0; // FIXME |
6984 | 0 | ip->journal_commit_time = 0; // FIXME |
6985 | 0 | ip->interleave_sectors = 0; // FIXME |
6986 | 0 | ip->sector_size = crypt_get_sector_size(cd); |
6987 | 0 | ip->buffer_sectors = 0; // FIXME |
6988 | |
|
6989 | 0 | ip->integrity = LUKS2_get_integrity(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT); |
6990 | 0 | ip->integrity_key_size = crypt_get_integrity_key_size(cd, false); |
6991 | 0 | ip->tag_size = INTEGRITY_tag_size(ip->integrity, crypt_get_cipher(cd), crypt_get_cipher_mode(cd)); |
6992 | |
|
6993 | 0 | ip->journal_integrity = NULL; |
6994 | 0 | ip->journal_integrity_key_size = 0; |
6995 | 0 | ip->journal_integrity_key = NULL; |
6996 | |
|
6997 | 0 | ip->journal_crypt = NULL; |
6998 | 0 | ip->journal_crypt_key_size = 0; |
6999 | 0 | ip->journal_crypt_key = NULL; |
7000 | 0 | return 0; |
7001 | 0 | } else if (!cd->type) { |
7002 | 0 | memset(ip, 0, sizeof(*ip)); |
7003 | 0 | ip->integrity = crypt_get_integrity(cd); |
7004 | 0 | ip->integrity_key_size = crypt_get_integrity_key_size(cd, false); |
7005 | 0 | ip->tag_size = crypt_get_integrity_tag_size(cd); |
7006 | 0 | } |
7007 | | |
7008 | 0 | return -ENOTSUP; |
7009 | 0 | } |
7010 | | |
7011 | | int crypt_convert(struct crypt_device *cd, |
7012 | | const char *type, |
7013 | | void *params) |
7014 | 0 | { |
7015 | 0 | struct luks_phdr hdr1; |
7016 | 0 | struct luks2_hdr hdr2; |
7017 | 0 | int r; |
7018 | |
|
7019 | 0 | if (!type) |
7020 | 0 | return -EINVAL; |
7021 | | |
7022 | 0 | log_dbg(cd, "Converting LUKS device to type %s", type); |
7023 | |
|
7024 | 0 | if ((r = onlyLUKSnoRequirements(cd))) |
7025 | 0 | return r; |
7026 | | |
7027 | 0 | if (isLUKS1(cd->type) && isLUKS2(type)) |
7028 | 0 | r = LUKS2_luks1_to_luks2(cd, &cd->u.luks1.hdr, &hdr2); |
7029 | 0 | else if (isLUKS2(cd->type) && isLUKS1(type)) |
7030 | 0 | r = LUKS2_luks2_to_luks1(cd, &cd->u.luks2.hdr, &hdr1); |
7031 | 0 | else |
7032 | 0 | return -EINVAL; |
7033 | | |
7034 | 0 | if (r < 0) { |
7035 | | /* in-memory header may be invalid after failed conversion */ |
7036 | 0 | _luks2_rollback(cd); |
7037 | 0 | if (r == -EBUSY) |
7038 | 0 | log_err(cd, _("Cannot convert device %s which is still in use."), mdata_device_path(cd)); |
7039 | 0 | return r; |
7040 | 0 | } |
7041 | | |
7042 | 0 | crypt_free_type(cd, NULL); |
7043 | |
|
7044 | 0 | return crypt_load(cd, type, params); |
7045 | 0 | } |
7046 | | |
7047 | | /* Internal access function to header pointer */ |
7048 | | void *crypt_get_hdr(struct crypt_device *cd, const char *type) |
7049 | 0 | { |
7050 | 0 | assert(cd); |
7051 | 0 | assert(type); |
7052 | | |
7053 | | /* If requested type differs, ignore it */ |
7054 | 0 | if (!cd->type || strcmp(cd->type, type)) |
7055 | 0 | return NULL; |
7056 | | |
7057 | 0 | if (isPLAIN(cd->type)) |
7058 | 0 | return &cd->u.plain; |
7059 | | |
7060 | 0 | if (isLUKS1(cd->type)) |
7061 | 0 | return &cd->u.luks1.hdr; |
7062 | | |
7063 | 0 | if (isLUKS2(type)) |
7064 | 0 | return &cd->u.luks2.hdr; |
7065 | | |
7066 | 0 | if (isLOOPAES(cd->type)) |
7067 | 0 | return &cd->u.loopaes; |
7068 | | |
7069 | 0 | if (isVERITY(cd->type)) |
7070 | 0 | return &cd->u.verity; |
7071 | | |
7072 | 0 | if (isTCRYPT(cd->type)) |
7073 | 0 | return &cd->u.tcrypt; |
7074 | | |
7075 | 0 | return NULL; |
7076 | 0 | } |
7077 | | |
7078 | | /* internal only */ |
7079 | | struct luks2_reencrypt *crypt_get_luks2_reencrypt(struct crypt_device *cd) |
7080 | 0 | { |
7081 | 0 | return cd->u.luks2.rh; |
7082 | 0 | } |
7083 | | |
7084 | | /* internal only */ |
7085 | | void crypt_set_luks2_reencrypt(struct crypt_device *cd, struct luks2_reencrypt *rh) |
7086 | 0 | { |
7087 | 0 | cd->u.luks2.rh = rh; |
7088 | 0 | } |
7089 | | |
7090 | | /* |
7091 | | * Token handling |
7092 | | */ |
7093 | | int crypt_activate_by_token_pin(struct crypt_device *cd, const char *name, |
7094 | | const char *type, int token, const char *pin, size_t pin_size, |
7095 | | void *usrptr, uint32_t flags) |
7096 | 0 | { |
7097 | 0 | int r; |
7098 | 0 | struct crypt_keyslot_context kc = {}; |
7099 | |
|
7100 | 0 | crypt_keyslot_context_init_by_token_internal(&kc, token, type, pin, pin_size, usrptr); |
7101 | 0 | r = crypt_activate_by_keyslot_context(cd, name, CRYPT_ANY_SLOT, &kc, CRYPT_ANY_SLOT, &kc, flags); |
7102 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
7103 | |
|
7104 | 0 | return r; |
7105 | 0 | } |
7106 | | |
7107 | | int crypt_activate_by_token(struct crypt_device *cd, |
7108 | | const char *name, int token, void *usrptr, uint32_t flags) |
7109 | 0 | { |
7110 | 0 | return crypt_activate_by_token_pin(cd, name, NULL, token, NULL, 0, usrptr, flags); |
7111 | 0 | } |
7112 | | |
7113 | | int crypt_token_json_get(struct crypt_device *cd, int token, const char **json) |
7114 | 0 | { |
7115 | 0 | int r; |
7116 | |
|
7117 | 0 | if (!json) |
7118 | 0 | return -EINVAL; |
7119 | | |
7120 | 0 | log_dbg(cd, "Requesting JSON for token %d.", token); |
7121 | |
|
7122 | 0 | if ((r = onlyLUKS2unrestricted(cd))) |
7123 | 0 | return r; |
7124 | | |
7125 | 0 | return LUKS2_token_json_get(&cd->u.luks2.hdr, token, json) ?: token; |
7126 | 0 | } |
7127 | | |
7128 | | int crypt_token_json_set(struct crypt_device *cd, int token, const char *json) |
7129 | 0 | { |
7130 | 0 | int r; |
7131 | |
|
7132 | 0 | log_dbg(cd, "Updating JSON for token %d.", token); |
7133 | |
|
7134 | 0 | if ((r = onlyLUKS2(cd))) |
7135 | 0 | return r; |
7136 | | |
7137 | 0 | return LUKS2_token_create(cd, &cd->u.luks2.hdr, token, json, 1); |
7138 | 0 | } |
7139 | | |
7140 | | crypt_token_info crypt_token_status(struct crypt_device *cd, int token, const char **type) |
7141 | 0 | { |
7142 | 0 | if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0)) |
7143 | 0 | return CRYPT_TOKEN_INVALID; |
7144 | | |
7145 | 0 | return LUKS2_token_status(cd, &cd->u.luks2.hdr, token, type); |
7146 | 0 | } |
7147 | | |
7148 | | int crypt_token_max(const char *type) |
7149 | 0 | { |
7150 | 0 | if (isLUKS2(type)) |
7151 | 0 | return LUKS2_TOKENS_MAX; |
7152 | | |
7153 | 0 | return -EINVAL; |
7154 | 0 | } |
7155 | | |
7156 | | int crypt_token_luks2_keyring_get(struct crypt_device *cd, |
7157 | | int token, |
7158 | | struct crypt_token_params_luks2_keyring *params) |
7159 | 0 | { |
7160 | 0 | crypt_token_info token_info; |
7161 | 0 | const char *type; |
7162 | 0 | int r; |
7163 | |
|
7164 | 0 | if (!params) |
7165 | 0 | return -EINVAL; |
7166 | | |
7167 | 0 | log_dbg(cd, "Requesting LUKS2 keyring token %d.", token); |
7168 | |
|
7169 | 0 | if ((r = onlyLUKS2unrestricted(cd))) |
7170 | 0 | return r; |
7171 | | |
7172 | 0 | token_info = LUKS2_token_status(cd, &cd->u.luks2.hdr, token, &type); |
7173 | 0 | switch (token_info) { |
7174 | 0 | case CRYPT_TOKEN_INVALID: |
7175 | 0 | log_dbg(cd, "Token %d is invalid.", token); |
7176 | 0 | return -EINVAL; |
7177 | 0 | case CRYPT_TOKEN_INACTIVE: |
7178 | 0 | log_dbg(cd, "Token %d is inactive.", token); |
7179 | 0 | return -EINVAL; |
7180 | 0 | case CRYPT_TOKEN_INTERNAL: |
7181 | 0 | if (!strcmp(type, LUKS2_TOKEN_KEYRING)) |
7182 | 0 | break; |
7183 | | /* Fall through */ |
7184 | 0 | case CRYPT_TOKEN_INTERNAL_UNKNOWN: |
7185 | 0 | case CRYPT_TOKEN_EXTERNAL: |
7186 | 0 | case CRYPT_TOKEN_EXTERNAL_UNKNOWN: |
7187 | 0 | log_dbg(cd, "Token %d has unexpected type %s.", token, type); |
7188 | 0 | return -EINVAL; |
7189 | 0 | } |
7190 | | |
7191 | 0 | return LUKS2_token_keyring_get(&cd->u.luks2.hdr, token, params); |
7192 | 0 | } |
7193 | | |
7194 | | int crypt_token_luks2_keyring_set(struct crypt_device *cd, |
7195 | | int token, |
7196 | | const struct crypt_token_params_luks2_keyring *params) |
7197 | 0 | { |
7198 | 0 | int r; |
7199 | 0 | char json[4096]; |
7200 | |
|
7201 | 0 | if (!params || !params->key_description) |
7202 | 0 | return -EINVAL; |
7203 | | |
7204 | 0 | log_dbg(cd, "Creating new LUKS2 keyring token (%d).", token); |
7205 | |
|
7206 | 0 | if ((r = onlyLUKS2(cd))) |
7207 | 0 | return r; |
7208 | | |
7209 | 0 | r = LUKS2_token_keyring_json(json, sizeof(json), params); |
7210 | 0 | if (r < 0) |
7211 | 0 | return r; |
7212 | | |
7213 | 0 | return LUKS2_token_create(cd, &cd->u.luks2.hdr, token, json, 1); |
7214 | 0 | } |
7215 | | |
7216 | | int crypt_token_assign_keyslot(struct crypt_device *cd, int token, int keyslot) |
7217 | 0 | { |
7218 | 0 | int r; |
7219 | |
|
7220 | 0 | if ((r = onlyLUKS2(cd))) |
7221 | 0 | return r; |
7222 | | |
7223 | 0 | if (token == CRYPT_ANY_TOKEN) |
7224 | 0 | return -EINVAL; |
7225 | | |
7226 | 0 | return LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot, token, 1, 1); |
7227 | 0 | } |
7228 | | |
7229 | | int crypt_token_unassign_keyslot(struct crypt_device *cd, int token, int keyslot) |
7230 | 0 | { |
7231 | 0 | int r; |
7232 | |
|
7233 | 0 | if ((r = onlyLUKS2(cd))) |
7234 | 0 | return r; |
7235 | | |
7236 | 0 | if (token == CRYPT_ANY_TOKEN) |
7237 | 0 | return -EINVAL; |
7238 | | |
7239 | 0 | return LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot, token, 0, 1); |
7240 | 0 | } |
7241 | | |
7242 | | int crypt_token_is_assigned(struct crypt_device *cd, int token, int keyslot) |
7243 | 0 | { |
7244 | 0 | int r; |
7245 | |
|
7246 | 0 | if ((r = _onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))) |
7247 | 0 | return r; |
7248 | | |
7249 | 0 | return LUKS2_token_is_assigned(&cd->u.luks2.hdr, keyslot, token); |
7250 | 0 | } |
7251 | | |
7252 | | /* Internal only */ |
7253 | | int crypt_metadata_locking_enabled(void) |
7254 | 58.3k | { |
7255 | 58.3k | return _metadata_locking; |
7256 | 58.3k | } |
7257 | | |
7258 | | int crypt_metadata_locking(struct crypt_device *cd __attribute__((unused)), int enable) |
7259 | 0 | { |
7260 | 0 | if (enable && !_metadata_locking) |
7261 | 0 | return -EPERM; |
7262 | | |
7263 | 0 | _metadata_locking = enable ? 1 : 0; |
7264 | 0 | return 0; |
7265 | 0 | } |
7266 | | |
7267 | | int crypt_persistent_flags_set(struct crypt_device *cd, crypt_flags_type type, uint32_t flags) |
7268 | 0 | { |
7269 | 0 | int r; |
7270 | |
|
7271 | 0 | if ((r = onlyLUKS2(cd))) |
7272 | 0 | return r; |
7273 | | |
7274 | 0 | if (type == CRYPT_FLAGS_ACTIVATION) |
7275 | 0 | return LUKS2_config_set_flags(cd, &cd->u.luks2.hdr, flags); |
7276 | | |
7277 | 0 | if (type == CRYPT_FLAGS_REQUIREMENTS) |
7278 | 0 | return LUKS2_config_set_requirements(cd, &cd->u.luks2.hdr, flags, true); |
7279 | | |
7280 | 0 | return -EINVAL; |
7281 | 0 | } |
7282 | | |
7283 | | int crypt_persistent_flags_get(struct crypt_device *cd, crypt_flags_type type, uint32_t *flags) |
7284 | 0 | { |
7285 | 0 | int r; |
7286 | |
|
7287 | 0 | if (!flags) |
7288 | 0 | return -EINVAL; |
7289 | | |
7290 | 0 | if ((r = onlyLUKS2unrestricted(cd))) |
7291 | 0 | return r; |
7292 | | |
7293 | 0 | if (type == CRYPT_FLAGS_ACTIVATION) |
7294 | 0 | return LUKS2_config_get_flags(cd, &cd->u.luks2.hdr, flags); |
7295 | | |
7296 | 0 | if (type == CRYPT_FLAGS_REQUIREMENTS) { |
7297 | 0 | LUKS2_config_get_requirements(cd, &cd->u.luks2.hdr, flags); |
7298 | 0 | return 0; |
7299 | 0 | } |
7300 | | |
7301 | 0 | return -EINVAL; |
7302 | 0 | } |
7303 | | |
7304 | | static int update_volume_key_segment_digest(struct crypt_device *cd, struct luks2_hdr *hdr, int digest, int commit) |
7305 | 0 | { |
7306 | 0 | int r; |
7307 | | |
7308 | | /* Remove any assignments in memory */ |
7309 | 0 | r = LUKS2_digest_segment_assign(cd, hdr, CRYPT_DEFAULT_SEGMENT, CRYPT_ANY_DIGEST, 0, 0); |
7310 | 0 | if (r) |
7311 | 0 | return r; |
7312 | | |
7313 | | /* Assign it to the specific digest */ |
7314 | 0 | return LUKS2_digest_segment_assign(cd, hdr, CRYPT_DEFAULT_SEGMENT, digest, 1, commit); |
7315 | 0 | } |
7316 | | |
7317 | | static int verify_and_update_segment_digest(struct crypt_device *cd, |
7318 | | struct luks2_hdr *hdr, int keyslot, struct crypt_keyslot_context *kc) |
7319 | 0 | { |
7320 | 0 | int digest, r; |
7321 | 0 | struct volume_key *vk = NULL; |
7322 | |
|
7323 | 0 | assert(kc); |
7324 | 0 | assert(kc->get_luks2_key); |
7325 | 0 | assert(keyslot >= 0); |
7326 | |
|
7327 | 0 | r = kc->get_luks2_key(cd, kc, keyslot, CRYPT_ANY_SEGMENT, &vk); |
7328 | 0 | if (r < 0) |
7329 | 0 | return r; |
7330 | | |
7331 | | /* check volume_key (param) digest matches keyslot digest */ |
7332 | 0 | r = LUKS2_digest_verify(cd, hdr, vk, keyslot); |
7333 | 0 | if (r < 0) |
7334 | 0 | goto out; |
7335 | 0 | digest = r; |
7336 | | |
7337 | | /* nothing to do, volume key in keyslot is already assigned to default segment */ |
7338 | 0 | r = LUKS2_digest_verify_by_segment(cd, hdr, CRYPT_DEFAULT_SEGMENT, vk); |
7339 | 0 | if (r >= 0) |
7340 | 0 | goto out; |
7341 | | |
7342 | | /* FIXME: check new volume key is usable with current default segment */ |
7343 | | |
7344 | 0 | r = update_volume_key_segment_digest(cd, &cd->u.luks2.hdr, digest, 1); |
7345 | 0 | if (r) |
7346 | 0 | log_err(cd, _("Failed to assign keyslot %u as the new volume key."), keyslot); |
7347 | 0 | out: |
7348 | 0 | crypt_free_volume_key(vk); |
7349 | |
|
7350 | 0 | return r < 0 ? r : keyslot; |
7351 | 0 | } |
7352 | | |
7353 | | static int luks2_keyslot_add_by_verified_volume_key(struct crypt_device *cd, |
7354 | | int keyslot_new, |
7355 | | const char *new_passphrase, |
7356 | | size_t new_passphrase_size, |
7357 | | struct volume_key *vk) |
7358 | 0 | { |
7359 | 0 | int r; |
7360 | 0 | struct luks2_keyslot_params params; |
7361 | |
|
7362 | 0 | assert(cd); |
7363 | 0 | assert(keyslot_new >= 0); |
7364 | 0 | assert(new_passphrase); |
7365 | 0 | assert(vk); |
7366 | 0 | assert(crypt_volume_key_get_id(vk) >= 0); |
7367 | |
|
7368 | 0 | r = LUKS2_keyslot_params_default(cd, &cd->u.luks2.hdr, ¶ms); |
7369 | 0 | if (r < 0) { |
7370 | 0 | log_err(cd, _("Failed to initialize default LUKS2 keyslot parameters.")); |
7371 | 0 | return r; |
7372 | 0 | } |
7373 | | |
7374 | 0 | r = LUKS2_digest_assign(cd, &cd->u.luks2.hdr, keyslot_new, crypt_volume_key_get_id(vk), 1, 0); |
7375 | 0 | if (r < 0) { |
7376 | 0 | log_err(cd, _("Failed to assign keyslot %d to digest."), keyslot_new); |
7377 | 0 | return r; |
7378 | 0 | } |
7379 | | |
7380 | 0 | r = LUKS2_keyslot_store(cd, &cd->u.luks2.hdr, keyslot_new, |
7381 | 0 | CONST_CAST(char*)new_passphrase, |
7382 | 0 | new_passphrase_size, vk, ¶ms); |
7383 | |
|
7384 | 0 | return r < 0 ? r : keyslot_new; |
7385 | 0 | } |
7386 | | |
7387 | | static int luks2_keyslot_add_by_volume_key(struct crypt_device *cd, |
7388 | | int keyslot_new, |
7389 | | const char *new_passphrase, |
7390 | | size_t new_passphrase_size, |
7391 | | struct volume_key *vk) |
7392 | 0 | { |
7393 | 0 | int r; |
7394 | |
|
7395 | 0 | assert(cd); |
7396 | 0 | assert(keyslot_new >= 0); |
7397 | 0 | assert(new_passphrase); |
7398 | 0 | assert(vk); |
7399 | |
|
7400 | 0 | r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk); |
7401 | 0 | if (r >= 0) |
7402 | 0 | crypt_volume_key_set_id(vk, r); |
7403 | |
|
7404 | 0 | if (r < 0) { |
7405 | 0 | log_err(cd, _("Volume key does not match the volume.")); |
7406 | 0 | return r; |
7407 | 0 | } |
7408 | | |
7409 | 0 | return luks2_keyslot_add_by_verified_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk); |
7410 | 0 | } |
7411 | | |
7412 | | static int luks1_keyslot_add_by_volume_key(struct crypt_device *cd, |
7413 | | int keyslot_new, |
7414 | | const char *new_passphrase, |
7415 | | size_t new_passphrase_size, |
7416 | | struct volume_key *vk) |
7417 | 0 | { |
7418 | 0 | int r; |
7419 | |
|
7420 | 0 | assert(cd); |
7421 | 0 | assert(keyslot_new >= 0); |
7422 | 0 | assert(new_passphrase); |
7423 | 0 | assert(vk); |
7424 | |
|
7425 | 0 | r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk); |
7426 | 0 | if (r < 0) { |
7427 | 0 | log_err(cd, _("Volume key does not match the volume.")); |
7428 | 0 | return r; |
7429 | 0 | } |
7430 | | |
7431 | 0 | r = LUKS_set_key(keyslot_new, CONST_CAST(char*)new_passphrase, |
7432 | 0 | new_passphrase_size, &cd->u.luks1.hdr, vk, cd); |
7433 | |
|
7434 | 0 | return r < 0 ? r : keyslot_new; |
7435 | 0 | } |
7436 | | |
7437 | | static int keyslot_add_by_key(struct crypt_device *cd, |
7438 | | bool is_luks1, |
7439 | | int keyslot_new, |
7440 | | const char *new_passphrase, |
7441 | | size_t new_passphrase_size, |
7442 | | struct volume_key *vk, |
7443 | | uint32_t flags) |
7444 | 0 | { |
7445 | 0 | int r, digest; |
7446 | |
|
7447 | 0 | assert(cd); |
7448 | 0 | assert(keyslot_new >= 0); |
7449 | 0 | assert(new_passphrase); |
7450 | 0 | assert(vk); |
7451 | |
|
7452 | 0 | if (is_luks1) { |
7453 | 0 | if (flags) |
7454 | 0 | return -EINVAL; |
7455 | 0 | return luks1_keyslot_add_by_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk); |
7456 | 0 | } |
7457 | | |
7458 | | /* if passed key matches volume key digest tear down new vk flag */ |
7459 | 0 | if (flags & CRYPT_VOLUME_KEY_SET) { |
7460 | 0 | digest = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk); |
7461 | 0 | if (digest >= 0) |
7462 | 0 | flags &= ~CRYPT_VOLUME_KEY_SET; |
7463 | 0 | else if (digest != -EPERM) /* Anything other than -EPERM suggests broken metadata. Abort */ |
7464 | 0 | return digest; |
7465 | 0 | } |
7466 | | |
7467 | | /* |
7468 | | * Drop CRYPT_VOLUME_KEY_DIGEST_REUSE flag if used without CRYPT_VOLUME_KEY_SET |
7469 | | * or CRYPT_VOLUME_KEY_NO_SEGMENT flags. The standalone CRYPT_VOLUME_KEY_DIGEST_REUSE flag |
7470 | | * is otherwise equivalent to adding new keyslot with current volume key. |
7471 | | */ |
7472 | 0 | if ((flags & CRYPT_VOLUME_KEY_DIGEST_REUSE) && |
7473 | 0 | !(flags & (CRYPT_VOLUME_KEY_SET | CRYPT_VOLUME_KEY_NO_SEGMENT))) |
7474 | 0 | flags &= ~CRYPT_VOLUME_KEY_DIGEST_REUSE; |
7475 | |
|
7476 | 0 | if (!flags) |
7477 | 0 | return luks2_keyslot_add_by_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk); |
7478 | | |
7479 | 0 | digest = -ENOENT; |
7480 | | /* check if passed key matches any existing unbound digest */ |
7481 | 0 | if (flags & CRYPT_VOLUME_KEY_DIGEST_REUSE) |
7482 | 0 | digest = LUKS2_digest_verify_by_any_matching(cd, vk, /* exclude_default_segment= */ true); |
7483 | | |
7484 | | /* Anything other than -EPERM or -ENOENT suggests broken metadata. Abort */ |
7485 | 0 | if (digest < 0 && digest != -ENOENT && digest != -EPERM) |
7486 | 0 | return digest; |
7487 | | |
7488 | | /* no segment flag or new vk flag requires new key digest */ |
7489 | 0 | if (digest < 0 && (flags & (CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_SET))) |
7490 | 0 | digest = LUKS2_digest_create(cd, "pbkdf2", &cd->u.luks2.hdr, vk); |
7491 | |
|
7492 | 0 | r = digest; |
7493 | 0 | if (r < 0) |
7494 | 0 | return r; |
7495 | | |
7496 | 0 | crypt_volume_key_set_id(vk, digest); |
7497 | |
|
7498 | 0 | if (flags & CRYPT_VOLUME_KEY_SET) { |
7499 | 0 | r = update_volume_key_segment_digest(cd, &cd->u.luks2.hdr, digest, 0); |
7500 | 0 | if (r < 0) |
7501 | 0 | log_err(cd, _("Failed to assign keyslot %u as the new volume key."), keyslot_new); |
7502 | 0 | } |
7503 | |
|
7504 | 0 | if (r >= 0) |
7505 | 0 | r = luks2_keyslot_add_by_verified_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk); |
7506 | |
|
7507 | 0 | return r < 0 ? r : keyslot_new; |
7508 | 0 | } |
7509 | | |
7510 | | int crypt_keyslot_add_by_key(struct crypt_device *cd, |
7511 | | int keyslot, |
7512 | | const char *volume_key, |
7513 | | size_t volume_key_size, |
7514 | | const char *passphrase, |
7515 | | size_t passphrase_size, |
7516 | | uint32_t flags) |
7517 | 0 | { |
7518 | 0 | int r; |
7519 | 0 | struct crypt_keyslot_context kc = {}, new_kc = {}; |
7520 | |
|
7521 | 0 | if (!passphrase || ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) && |
7522 | 0 | (flags & CRYPT_VOLUME_KEY_SET))) |
7523 | 0 | return -EINVAL; |
7524 | | |
7525 | 0 | if ((r = onlyLUKS(cd)) < 0) |
7526 | 0 | return r; |
7527 | | |
7528 | 0 | if ((flags & CRYPT_VOLUME_KEY_SET) && crypt_keyslot_status(cd, keyslot) > CRYPT_SLOT_INACTIVE && |
7529 | 0 | isLUKS2(cd->type)) { |
7530 | 0 | if (volume_key) |
7531 | 0 | crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size); |
7532 | 0 | else |
7533 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size); |
7534 | |
|
7535 | 0 | r = verify_and_update_segment_digest(cd, &cd->u.luks2.hdr, keyslot, &kc); |
7536 | |
|
7537 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
7538 | |
|
7539 | 0 | return r; |
7540 | 0 | } |
7541 | | |
7542 | 0 | crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size); |
7543 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&new_kc, passphrase, passphrase_size); |
7544 | |
|
7545 | 0 | r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, flags); |
7546 | |
|
7547 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
7548 | 0 | crypt_keyslot_context_destroy_internal(&new_kc); |
7549 | |
|
7550 | 0 | return r; |
7551 | 0 | } |
7552 | | |
7553 | | int crypt_keyslot_add_by_keyslot_context(struct crypt_device *cd, |
7554 | | int keyslot_existing, |
7555 | | struct crypt_keyslot_context *kc, |
7556 | | int keyslot_new, |
7557 | | struct crypt_keyslot_context *new_kc, |
7558 | | uint32_t flags) |
7559 | 0 | { |
7560 | 0 | bool is_luks1; |
7561 | 0 | int active_slots, r; |
7562 | 0 | const char *new_passphrase; |
7563 | 0 | size_t new_passphrase_size; |
7564 | 0 | struct volume_key *vk = NULL; |
7565 | |
|
7566 | 0 | if (!kc || ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) && |
7567 | 0 | (flags & CRYPT_VOLUME_KEY_SET))) |
7568 | 0 | return -EINVAL; |
7569 | | |
7570 | 0 | r = flags ? onlyLUKS2(cd) : onlyLUKS(cd); |
7571 | 0 | if (r) |
7572 | 0 | return r; |
7573 | | |
7574 | 0 | if ((flags & CRYPT_VOLUME_KEY_SET) && crypt_keyslot_status(cd, keyslot_existing) > CRYPT_SLOT_INACTIVE) |
7575 | 0 | return verify_and_update_segment_digest(cd, &cd->u.luks2.hdr, keyslot_existing, kc); |
7576 | | |
7577 | 0 | if (!new_kc || !new_kc->get_passphrase) |
7578 | 0 | return -EINVAL; |
7579 | | |
7580 | 0 | log_dbg(cd, "Adding new keyslot %d by %s%s, volume key provided by %s (%d).", |
7581 | 0 | keyslot_new, keyslot_context_type_string(new_kc), |
7582 | 0 | (flags & CRYPT_VOLUME_KEY_NO_SEGMENT) ? " unassigned to a crypt segment" : "", |
7583 | 0 | keyslot_context_type_string(kc), keyslot_existing); |
7584 | |
|
7585 | 0 | r = keyslot_verify_or_find_empty(cd, &keyslot_new); |
7586 | 0 | if (r < 0) |
7587 | 0 | return r; |
7588 | | |
7589 | 0 | is_luks1 = isLUKS1(cd->type); |
7590 | 0 | if (is_luks1) |
7591 | 0 | active_slots = LUKS_keyslot_active_count(&cd->u.luks1.hdr); |
7592 | 0 | else |
7593 | 0 | active_slots = LUKS2_keyslot_active_count(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT); |
7594 | |
|
7595 | 0 | if (active_slots < 0) |
7596 | 0 | return -EINVAL; |
7597 | | |
7598 | 0 | if (active_slots == 0 && kc->type != CRYPT_KC_TYPE_KEY) |
7599 | 0 | r = -ENOENT; |
7600 | 0 | else if (is_luks1 && kc->get_luks1_volume_key) |
7601 | 0 | r = kc->get_luks1_volume_key(cd, kc, keyslot_existing, &vk); |
7602 | 0 | else if (!is_luks1 && kc->get_luks2_volume_key) |
7603 | 0 | r = kc->get_luks2_volume_key(cd, kc, keyslot_existing, &vk); |
7604 | 0 | else |
7605 | 0 | return -EINVAL; |
7606 | | |
7607 | 0 | if (r == -ENOENT) { |
7608 | 0 | if ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) && kc->type == CRYPT_KC_TYPE_KEY) { |
7609 | 0 | if (!(vk = crypt_generate_volume_key(cd, kc->u.k.volume_key_size, KEY_QUALITY_KEY))) |
7610 | 0 | return -ENOMEM; |
7611 | 0 | r = 0; |
7612 | 0 | } else if (cd->volume_key) { |
7613 | 0 | if (!(vk = crypt_alloc_volume_key(crypt_volume_key_length(cd->volume_key), |
7614 | 0 | crypt_volume_key_get_key(cd->volume_key)))) |
7615 | 0 | return -ENOMEM; |
7616 | 0 | r = 0; |
7617 | 0 | } else if (active_slots == 0) { |
7618 | 0 | log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided.")); |
7619 | 0 | r = -EINVAL; |
7620 | 0 | } |
7621 | 0 | } |
7622 | | |
7623 | 0 | if (r < 0) |
7624 | 0 | return r; |
7625 | | |
7626 | 0 | r = new_kc->get_passphrase(cd, new_kc, &new_passphrase, &new_passphrase_size); |
7627 | | /* If new keyslot context is token just assign it to new keyslot */ |
7628 | 0 | if (r >= 0 && new_kc->type == CRYPT_KC_TYPE_TOKEN && !is_luks1) |
7629 | 0 | r = LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot_new, new_kc->u.t.id, 1, 0); |
7630 | 0 | if (r >= 0) |
7631 | 0 | r = keyslot_add_by_key(cd, is_luks1, keyslot_new, new_passphrase, new_passphrase_size, vk, flags); |
7632 | |
|
7633 | 0 | crypt_free_volume_key(vk); |
7634 | |
|
7635 | 0 | if (r < 0) { |
7636 | 0 | _luks2_rollback(cd); |
7637 | 0 | return r; |
7638 | 0 | } |
7639 | | |
7640 | 0 | return keyslot_new; |
7641 | 0 | } |
7642 | | |
7643 | | /* |
7644 | | * Keyring handling |
7645 | | */ |
7646 | | int crypt_use_keyring_for_vk(struct crypt_device *cd) |
7647 | 0 | { |
7648 | 0 | uint64_t dmc_flags; |
7649 | | |
7650 | | /* dm backend must be initialized */ |
7651 | 0 | if (!cd) |
7652 | 0 | return 0; |
7653 | | |
7654 | 0 | if (!isPLAIN(cd->type) && !isLUKS2(cd->type)) |
7655 | 0 | return 0; |
7656 | | |
7657 | 0 | if (!_vk_via_keyring || !kernel_keyring_support()) |
7658 | 0 | return 0; |
7659 | | |
7660 | 0 | if (dm_flags(cd, DM_CRYPT, &dmc_flags)) |
7661 | 0 | return dmcrypt_keyring_bug() ? 0 : 1; |
7662 | | |
7663 | 0 | return (dmc_flags & DM_KERNEL_KEYRING_SUPPORTED); |
7664 | 0 | } |
7665 | | |
7666 | | int crypt_volume_key_keyring(struct crypt_device *cd __attribute__((unused)), int enable) |
7667 | 0 | { |
7668 | 0 | _vk_via_keyring = enable ? 1 : 0; |
7669 | 0 | return 0; |
7670 | 0 | } |
7671 | | |
7672 | | /* internal only */ |
7673 | | int crypt_volume_key_load_in_keyring(struct crypt_device *cd, struct volume_key *vk) |
7674 | 0 | { |
7675 | 0 | key_serial_t keyring_id; |
7676 | 0 | char *keyring_description; |
7677 | 0 | char rnd[4]; |
7678 | 0 | const char *uuid; |
7679 | |
|
7680 | 0 | if (!vk || !cd) |
7681 | 0 | return -EINVAL; |
7682 | | |
7683 | 0 | if (!crypt_volume_key_description(vk)) { |
7684 | 0 | log_dbg(cd, "Invalid key description"); |
7685 | 0 | return -EINVAL; |
7686 | 0 | } |
7687 | | |
7688 | 0 | if (!cd->keyring_description) { |
7689 | 0 | uuid = crypt_get_uuid(cd); |
7690 | 0 | if (!uuid) |
7691 | 0 | return -EINVAL; |
7692 | | |
7693 | 0 | if (crypt_random_get(cd, rnd, sizeof(rnd), CRYPT_RND_NORMAL) < 0) |
7694 | 0 | return -EINVAL; |
7695 | | |
7696 | 0 | if (asprintf(&keyring_description, "cryptsetup-%.8s-%02x%02x%02x%02x", |
7697 | 0 | uuid, (unsigned char)rnd[0], (unsigned char)rnd[1], |
7698 | 0 | (unsigned char)rnd[2], (unsigned char)rnd[3]) < 0) |
7699 | 0 | return -ENOMEM; |
7700 | | |
7701 | 0 | log_dbg(cd, "Loading key (type keyring, name %s) in thread keyring.", keyring_description); |
7702 | 0 | keyring_id = keyring_add_key_in_thread_keyring(KEYRING_KEY, keyring_description, NULL, 0); |
7703 | 0 | if (keyring_id < 0) { |
7704 | 0 | free(keyring_description); |
7705 | 0 | log_dbg(cd, "keyring_add_key_in_thread_keyring failed (error %d)", errno); |
7706 | 0 | log_err(cd, _("Failed to load key in kernel keyring.")); |
7707 | 0 | return -EINVAL; |
7708 | 0 | } |
7709 | 0 | cd->keyring_id = keyring_id; |
7710 | 0 | cd->keyring_description = keyring_description; |
7711 | 0 | } |
7712 | | |
7713 | 0 | log_dbg(cd, "Loading key (type logon, name %s) in %s keyring.", |
7714 | 0 | crypt_volume_key_description(vk), cd->keyring_description); |
7715 | |
|
7716 | 0 | if (crypt_volume_key_upload_kernel_key(vk, cd->keyring_id)) { |
7717 | 0 | crypt_set_key_in_keyring(cd, 1); |
7718 | 0 | return 0; |
7719 | 0 | } else { |
7720 | 0 | log_dbg(cd, "keyring_add_key_to_keyring failed (error %d)", errno); |
7721 | 0 | log_err(cd, _("Failed to load key in kernel keyring.")); |
7722 | 0 | return -EINVAL; |
7723 | 0 | } |
7724 | 0 | } |
7725 | | |
7726 | | /* internal only */ |
7727 | | int crypt_keyring_get_user_key(struct crypt_device *cd, |
7728 | | const char *key_description, |
7729 | | char **key, |
7730 | | size_t *key_size) |
7731 | 0 | { |
7732 | 0 | int r; |
7733 | 0 | key_serial_t kid; |
7734 | |
|
7735 | 0 | if (!key_description || !key || !key_size) |
7736 | 0 | return -EINVAL; |
7737 | | |
7738 | 0 | log_dbg(cd, "Requesting key %s (user type)", key_description); |
7739 | |
|
7740 | 0 | kid = keyring_request_key_id(USER_KEY, key_description); |
7741 | 0 | if (kid == -ENOTSUP) { |
7742 | 0 | log_dbg(cd, "Kernel keyring features disabled."); |
7743 | 0 | return -ENOTSUP; |
7744 | 0 | } else if (kid < 0) { |
7745 | 0 | log_dbg(cd, "keyring_request_key_id failed with errno %d.", errno); |
7746 | 0 | return -EINVAL; |
7747 | 0 | } |
7748 | | |
7749 | 0 | log_dbg(cd, "Reading content of kernel key (id %" PRIi32 ").", kid); |
7750 | |
|
7751 | 0 | r = keyring_read_key(kid, key, key_size); |
7752 | 0 | if (r < 0) |
7753 | 0 | log_dbg(cd, "keyring_read_key failed with errno %d.", errno); |
7754 | |
|
7755 | 0 | return r; |
7756 | 0 | } |
7757 | | |
7758 | | /* internal only */ |
7759 | | int crypt_keyring_get_key_by_name(struct crypt_device *cd, |
7760 | | const char *key_description, |
7761 | | char **key, |
7762 | | size_t *key_size) |
7763 | 0 | { |
7764 | 0 | int r; |
7765 | 0 | key_serial_t kid; |
7766 | |
|
7767 | 0 | if (!key_description || !key || !key_size) |
7768 | 0 | return -EINVAL; |
7769 | | |
7770 | 0 | log_dbg(cd, "Searching for kernel key by name %s.", key_description); |
7771 | |
|
7772 | 0 | kid = keyring_find_key_id_by_name(key_description); |
7773 | 0 | if (kid == 0) { |
7774 | 0 | log_dbg(cd, "keyring_find_key_id_by_name failed with errno %d.", errno); |
7775 | 0 | return -ENOENT; |
7776 | 0 | } |
7777 | | |
7778 | 0 | log_dbg(cd, "Reading content of kernel key (id %" PRIi32 ").", kid); |
7779 | |
|
7780 | 0 | r = keyring_read_key(kid, key, key_size); |
7781 | 0 | if (r < 0) |
7782 | 0 | log_dbg(cd, "keyring_read_key failed with errno %d.", errno); |
7783 | |
|
7784 | 0 | return r; |
7785 | 0 | } |
7786 | | |
7787 | | int crypt_keyring_get_keysize_by_name(struct crypt_device *cd, |
7788 | | const char *key_description, |
7789 | | size_t *r_key_size) |
7790 | 0 | { |
7791 | 0 | int r; |
7792 | 0 | key_serial_t kid; |
7793 | |
|
7794 | 0 | if (!key_description || !r_key_size) |
7795 | 0 | return -EINVAL; |
7796 | | |
7797 | 0 | log_dbg(cd, "Searching for kernel key by name %s.", key_description); |
7798 | |
|
7799 | 0 | kid = keyring_find_key_id_by_name(key_description); |
7800 | 0 | if (kid == -ENOTSUP) { |
7801 | 0 | log_dbg(cd, "Kernel keyring features disabled."); |
7802 | 0 | return -ENOTSUP; |
7803 | 0 | } else if (kid < 0) { |
7804 | 0 | log_dbg(cd, "keyring_find_key_id_by_name failed with errno %d.", errno); |
7805 | 0 | return -EINVAL; |
7806 | 0 | } |
7807 | 0 | else if (kid == 0) { |
7808 | 0 | log_dbg(cd, "keyring_find_key_id_by_name failed with errno %d.", ENOENT); |
7809 | 0 | return -ENOENT; |
7810 | 0 | } |
7811 | | |
7812 | 0 | log_dbg(cd, "Reading content of kernel key (id %" PRIi32 ").", kid); |
7813 | |
|
7814 | 0 | r = keyring_read_keysize(kid, r_key_size); |
7815 | 0 | if (r < 0) |
7816 | 0 | log_dbg(cd, "keyring_read_keysize failed with errno %d.", errno); |
7817 | |
|
7818 | 0 | return r; |
7819 | 0 | } |
7820 | | |
7821 | | /* internal only */ |
7822 | | int crypt_key_in_keyring(struct crypt_device *cd) |
7823 | 0 | { |
7824 | 0 | return cd ? cd->key_in_keyring : 0; |
7825 | 0 | } |
7826 | | |
7827 | | /* internal only */ |
7828 | | void crypt_set_key_in_keyring(struct crypt_device *cd, unsigned key_in_keyring) |
7829 | 0 | { |
7830 | 0 | if (!cd) |
7831 | 0 | return; |
7832 | | |
7833 | 0 | cd->key_in_keyring = key_in_keyring; |
7834 | 0 | } |
7835 | | |
7836 | | void crypt_unlink_key_by_description_from_keyring(struct crypt_device *cd, |
7837 | | const char *key_description, |
7838 | | key_type_t ktype) |
7839 | 0 | { |
7840 | 0 | key_serial_t kid; |
7841 | 0 | const char *type_name = key_type_name(ktype); |
7842 | |
|
7843 | 0 | if (!key_description || !type_name) |
7844 | 0 | return; |
7845 | | |
7846 | 0 | log_dbg(cd, "Requesting kernel key %s (type %s).", key_description, type_name); |
7847 | |
|
7848 | 0 | crypt_set_key_in_keyring(cd, 0); |
7849 | |
|
7850 | 0 | kid = keyring_request_key_id(ktype, key_description); |
7851 | 0 | if (kid == -ENOTSUP) { |
7852 | 0 | log_dbg(cd, "Kernel keyring features disabled."); |
7853 | 0 | return; |
7854 | 0 | } else if (kid < 0) { |
7855 | 0 | log_dbg(cd, "keyring_request_key_id failed with errno %d.", errno); |
7856 | 0 | return; |
7857 | 0 | } |
7858 | | |
7859 | 0 | crypt_unlink_key_from_keyring(cd, kid); |
7860 | 0 | } |
7861 | | |
7862 | | int crypt_set_keyring_to_link(struct crypt_device *cd, const char *key_description, |
7863 | | const char *old_key_description, |
7864 | | const char *key_type_desc, const char *keyring_to_link_vk) |
7865 | 0 | { |
7866 | 0 | key_type_t key_type = USER_KEY; |
7867 | 0 | const char *name1 = NULL, *name2 = NULL; |
7868 | 0 | int32_t id = 0; |
7869 | 0 | int r, ri; |
7870 | 0 | struct luks2_hdr *hdr; |
7871 | 0 | unsigned user_descriptions_count, vks_count = 1; |
7872 | |
|
7873 | 0 | if (!cd || ((!key_description && !old_key_description) && (keyring_to_link_vk || key_type_desc)) || |
7874 | 0 | ((key_description || old_key_description) && !keyring_to_link_vk)) |
7875 | 0 | return -EINVAL; |
7876 | | |
7877 | 0 | hdr = crypt_get_hdr(cd, CRYPT_LUKS2); |
7878 | | |
7879 | | /* if only one key description is supplied, force it to be the first one */ |
7880 | 0 | if (!key_description && old_key_description) |
7881 | 0 | return -EINVAL; |
7882 | | |
7883 | 0 | if ((r = _onlyLUKS2(cd, 0, CRYPT_REQUIREMENT_OPAL | CRYPT_REQUIREMENT_ONLINE_REENCRYPT))) |
7884 | 0 | return r; |
7885 | | |
7886 | 0 | if (key_type_desc) |
7887 | 0 | key_type = key_type_by_name(key_type_desc); |
7888 | 0 | if (key_type != LOGON_KEY && key_type != USER_KEY) |
7889 | 0 | return -EINVAL; |
7890 | | |
7891 | 0 | ri = crypt_reencrypt_status(cd, NULL); |
7892 | 0 | if (ri > CRYPT_REENCRYPT_NONE && ri < CRYPT_REENCRYPT_INVALID) |
7893 | 0 | vks_count = LUKS2_reencrypt_vks_count(hdr); |
7894 | |
|
7895 | 0 | user_descriptions_count = (key_description ? 1 : 0) + (old_key_description ? 1 : 0); |
7896 | 0 | if (user_descriptions_count != 0 && vks_count > user_descriptions_count) |
7897 | 0 | return -ESRCH; |
7898 | | |
7899 | 0 | if (keyring_to_link_vk) { |
7900 | 0 | id = keyring_find_keyring_id_by_name(keyring_to_link_vk); |
7901 | 0 | if (id == 0) { |
7902 | 0 | log_err(cd, _("Could not find keyring described by \"%s\"."), keyring_to_link_vk); |
7903 | 0 | return -EINVAL; |
7904 | 0 | } |
7905 | 0 | if (key_description && !(name1 = strdup(key_description))) |
7906 | 0 | return -ENOMEM; |
7907 | 0 | if (old_key_description && !(name2 = strdup(old_key_description))) { |
7908 | 0 | free(CONST_CAST(void*)name1); |
7909 | 0 | return -ENOMEM; |
7910 | 0 | } |
7911 | 0 | } |
7912 | | |
7913 | 0 | cd->keyring_key_type = key_type; |
7914 | |
|
7915 | 0 | free(CONST_CAST(void*)cd->user_key_name1); |
7916 | 0 | free(CONST_CAST(void*)cd->user_key_name2); |
7917 | 0 | cd->user_key_name1 = name1; |
7918 | 0 | cd->user_key_name2 = name2; |
7919 | 0 | cd->keyring_to_link_vk = id; |
7920 | 0 | cd->link_vk_to_keyring = id != 0; |
7921 | |
|
7922 | 0 | return 0; |
7923 | 0 | } |
7924 | | |
7925 | | /* internal only */ |
7926 | | void crypt_drop_uploaded_keyring_key(struct crypt_device *cd, struct volume_key *vks) |
7927 | 0 | { |
7928 | 0 | struct volume_key *vk = vks; |
7929 | |
|
7930 | 0 | while (vk) { |
7931 | 0 | crypt_volume_key_drop_uploaded_kernel_key(cd, vk); |
7932 | 0 | vk = crypt_volume_key_next(vk); |
7933 | 0 | } |
7934 | 0 | } |
7935 | | |
7936 | | int crypt_activate_by_keyring(struct crypt_device *cd, |
7937 | | const char *name, |
7938 | | const char *key_description, |
7939 | | int keyslot, |
7940 | | uint32_t flags) |
7941 | 0 | { |
7942 | 0 | int r; |
7943 | 0 | struct crypt_keyslot_context kc = {}; |
7944 | |
|
7945 | 0 | if (!cd || !key_description) |
7946 | 0 | return -EINVAL; |
7947 | | |
7948 | 0 | crypt_keyslot_context_init_by_keyring_internal(&kc, key_description); |
7949 | 0 | r = crypt_activate_by_keyslot_context(cd, name, keyslot, &kc, CRYPT_ANY_SLOT, &kc, flags); |
7950 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
7951 | |
|
7952 | 0 | return r; |
7953 | 0 | } |
7954 | | |
7955 | | /* |
7956 | | * Workaround for serialization of parallel activation and memory-hard PBKDF |
7957 | | * In specific situation (systemd activation) this causes OOM killer activation. |
7958 | | * For now, let's provide this ugly way to serialize unlocking of devices. |
7959 | | */ |
7960 | | int crypt_serialize_lock(struct crypt_device *cd) |
7961 | 0 | { |
7962 | 0 | if (!cd->memory_hard_pbkdf_lock_enabled) |
7963 | 0 | return 0; |
7964 | | |
7965 | 0 | log_dbg(cd, "Taking global memory-hard access serialization lock."); |
7966 | 0 | if (crypt_write_lock(cd, "memory-hard-access", true, &cd->pbkdf_memory_hard_lock)) { |
7967 | 0 | log_err(cd, _("Failed to acquire global memory-hard access serialization lock.")); |
7968 | 0 | cd->pbkdf_memory_hard_lock = NULL; |
7969 | 0 | return -EINVAL; |
7970 | 0 | } |
7971 | | |
7972 | 0 | return 0; |
7973 | 0 | } |
7974 | | |
7975 | | void crypt_serialize_unlock(struct crypt_device *cd) |
7976 | 0 | { |
7977 | 0 | if (!cd->memory_hard_pbkdf_lock_enabled) |
7978 | 0 | return; |
7979 | | |
7980 | 0 | crypt_unlock_internal(cd, cd->pbkdf_memory_hard_lock); |
7981 | 0 | cd->pbkdf_memory_hard_lock = NULL; |
7982 | 0 | } |
7983 | | |
7984 | | crypt_reencrypt_info crypt_reencrypt_status(struct crypt_device *cd, |
7985 | | struct crypt_params_reencrypt *params) |
7986 | 0 | { |
7987 | 0 | if (params) |
7988 | 0 | memset(params, 0, sizeof(*params)); |
7989 | |
|
7990 | 0 | if (!cd || !isLUKS(cd->type)) |
7991 | 0 | return CRYPT_REENCRYPT_INVALID; |
7992 | | |
7993 | 0 | if (isLUKS1(cd->type)) |
7994 | 0 | return CRYPT_REENCRYPT_NONE; |
7995 | | |
7996 | 0 | if (_onlyLUKS2(cd, CRYPT_CD_QUIET, CRYPT_REQUIREMENT_ONLINE_REENCRYPT)) |
7997 | 0 | return CRYPT_REENCRYPT_INVALID; |
7998 | | |
7999 | 0 | return LUKS2_reencrypt_get_params(&cd->u.luks2.hdr, params); |
8000 | 0 | } |
8001 | | |
8002 | | static void __attribute__((destructor)) libcryptsetup_exit(void) |
8003 | 0 | { |
8004 | 0 | crypt_token_unload_external_all(NULL); |
8005 | |
|
8006 | 0 | crypt_backend_destroy(); |
8007 | 0 | crypt_random_exit(); |
8008 | 0 | } |