]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
e23a0ce8 | 2 | |
7f4e0805 | 3 | #include <errno.h> |
07630cea | 4 | #include <mntent.h> |
07630cea | 5 | #include <sys/mman.h> |
ca78ad1d ZJS |
6 | #include <sys/stat.h> |
7 | #include <sys/types.h> | |
8 | #include <unistd.h> | |
e23a0ce8 | 9 | |
4f5dd394 LP |
10 | #include "sd-device.h" |
11 | ||
b5efdb8a | 12 | #include "alloc-util.h" |
4f5dd394 | 13 | #include "ask-password-api.h" |
2bc5c425 | 14 | #include "cryptsetup-fido2.h" |
1e2f3230 | 15 | #include "cryptsetup-keyfile.h" |
08669709 | 16 | #include "cryptsetup-pkcs11.h" |
18843ecc | 17 | #include "cryptsetup-tpm2.h" |
7407f689 | 18 | #include "cryptsetup-util.h" |
4f5dd394 LP |
19 | #include "device-util.h" |
20 | #include "escape.h" | |
8cf3ca80 | 21 | #include "fileio.h" |
d3d49e76 | 22 | #include "fs-util.h" |
ed4ad488 | 23 | #include "fstab-util.h" |
08669709 | 24 | #include "hexdecoct.h" |
2bc5c425 | 25 | #include "libfido2-util.h" |
e23a0ce8 | 26 | #include "log.h" |
3a40f366 | 27 | #include "main-func.h" |
7407f689 | 28 | #include "memory-util.h" |
4349cd7c | 29 | #include "mount-util.h" |
d8b4d14d | 30 | #include "nulstr-util.h" |
6bedfcbb | 31 | #include "parse-util.h" |
9eb977db | 32 | #include "path-util.h" |
08669709 | 33 | #include "pkcs11-util.h" |
d8b4d14d | 34 | #include "pretty-print.h" |
e2c2f868 | 35 | #include "random-util.h" |
07630cea | 36 | #include "string-util.h" |
21bc923a | 37 | #include "strv.h" |
18843ecc | 38 | #include "tpm2-util.h" |
7f4e0805 | 39 | |
b3b4ebab OK |
40 | /* internal helper */ |
41 | #define ANY_LUKS "LUKS" | |
a9fc6406 DJL |
42 | /* as in src/cryptsetup.h */ |
43 | #define CRYPT_SECTOR_SIZE 512 | |
44 | #define CRYPT_MAX_SECTOR_SIZE 4096 | |
b3b4ebab | 45 | |
6cc27c29 | 46 | static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT, CRYPT_BITLK or CRYPT_PLAIN */ |
f75cac37 LP |
47 | static char *arg_cipher = NULL; |
48 | static unsigned arg_key_size = 0; | |
a9fc6406 | 49 | static unsigned arg_sector_size = CRYPT_SECTOR_SIZE; |
f75cac37 LP |
50 | static int arg_key_slot = CRYPT_ANY_SLOT; |
51 | static unsigned arg_keyfile_size = 0; | |
dc0a3555 | 52 | static uint64_t arg_keyfile_offset = 0; |
d3d49e76 | 53 | static bool arg_keyfile_erase = false; |
0ba6f85e | 54 | static bool arg_try_empty_password = false; |
f75cac37 | 55 | static char *arg_hash = NULL; |
7376e835 | 56 | static char *arg_header = NULL; |
f75cac37 LP |
57 | static unsigned arg_tries = 3; |
58 | static bool arg_readonly = false; | |
59 | static bool arg_verify = false; | |
1fa94a31 | 60 | static bool arg_silent = false; |
f75cac37 | 61 | static bool arg_discards = false; |
2c65512e YW |
62 | static bool arg_same_cpu_crypt = false; |
63 | static bool arg_submit_from_crypt_cpus = false; | |
227acf00 JU |
64 | static bool arg_no_read_workqueue = false; |
65 | static bool arg_no_write_workqueue = false; | |
f75cac37 LP |
66 | static bool arg_tcrypt_hidden = false; |
67 | static bool arg_tcrypt_system = false; | |
52028838 | 68 | static bool arg_tcrypt_veracrypt = false; |
f75cac37 | 69 | static char **arg_tcrypt_keyfiles = NULL; |
4eac2773 MP |
70 | static uint64_t arg_offset = 0; |
71 | static uint64_t arg_skip = 0; | |
0864d311 | 72 | static usec_t arg_timeout = USEC_INFINITY; |
08669709 | 73 | static char *arg_pkcs11_uri = NULL; |
b997d111 | 74 | static bool arg_pkcs11_uri_auto = false; |
2bc5c425 LP |
75 | static char *arg_fido2_device = NULL; |
76 | static bool arg_fido2_device_auto = false; | |
77 | static void *arg_fido2_cid = NULL; | |
78 | static size_t arg_fido2_cid_size = 0; | |
79 | static char *arg_fido2_rp_id = NULL; | |
18843ecc LP |
80 | static char *arg_tpm2_device = NULL; |
81 | static bool arg_tpm2_device_auto = false; | |
82 | static uint32_t arg_tpm2_pcr_mask = UINT32_MAX; | |
cd5f57bd | 83 | static bool arg_headless = false; |
7f4e0805 | 84 | |
3a40f366 YW |
85 | STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep); |
86 | STATIC_DESTRUCTOR_REGISTER(arg_hash, freep); | |
87 | STATIC_DESTRUCTOR_REGISTER(arg_header, freep); | |
88 | STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep); | |
08669709 | 89 | STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_uri, freep); |
2bc5c425 LP |
90 | STATIC_DESTRUCTOR_REGISTER(arg_fido2_device, freep); |
91 | STATIC_DESTRUCTOR_REGISTER(arg_fido2_cid, freep); | |
92 | STATIC_DESTRUCTOR_REGISTER(arg_fido2_rp_id, freep); | |
18843ecc | 93 | STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep); |
3a40f366 | 94 | |
1fc76335 LP |
95 | /* Options Debian's crypttab knows we don't: |
96 | ||
1fc76335 LP |
97 | check= |
98 | checkargs= | |
8ced40c0 LP |
99 | noearly |
100 | loud | |
101 | quiet | |
1fc76335 | 102 | keyscript= |
8ced40c0 | 103 | initramfs |
1fc76335 LP |
104 | */ |
105 | ||
7f4e0805 | 106 | static int parse_one_option(const char *option) { |
fb4650aa ZJS |
107 | const char *val; |
108 | int r; | |
109 | ||
7f4e0805 LP |
110 | assert(option); |
111 | ||
112 | /* Handled outside of this tool */ | |
50d2eba2 | 113 | if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout")) |
114 | return 0; | |
115 | ||
116 | if (startswith(option, "keyfile-timeout=")) | |
7f4e0805 LP |
117 | return 0; |
118 | ||
fb4650aa ZJS |
119 | if ((val = startswith(option, "cipher="))) { |
120 | r = free_and_strdup(&arg_cipher, val); | |
121 | if (r < 0) | |
4b93637f | 122 | return log_oom(); |
7f4e0805 | 123 | |
fb4650aa | 124 | } else if ((val = startswith(option, "size="))) { |
7f4e0805 | 125 | |
fb4650aa ZJS |
126 | r = safe_atou(val, &arg_key_size); |
127 | if (r < 0) { | |
128 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
129 | return 0; |
130 | } | |
131 | ||
6131a78b DH |
132 | if (arg_key_size % 8) { |
133 | log_error("size= not a multiple of 8, ignoring."); | |
134 | return 0; | |
135 | } | |
136 | ||
137 | arg_key_size /= 8; | |
138 | ||
a9fc6406 DJL |
139 | } else if ((val = startswith(option, "sector-size="))) { |
140 | ||
a9fc6406 DJL |
141 | r = safe_atou(val, &arg_sector_size); |
142 | if (r < 0) { | |
143 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
144 | return 0; | |
145 | } | |
146 | ||
147 | if (arg_sector_size % 2) { | |
148 | log_error("sector-size= not a multiple of 2, ignoring."); | |
149 | return 0; | |
150 | } | |
151 | ||
152 | if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE) { | |
153 | log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE); | |
154 | return 0; | |
155 | } | |
a9fc6406 | 156 | |
8ced40c0 LP |
157 | } else if ((val = startswith(option, "key-slot=")) || |
158 | (val = startswith(option, "keyslot="))) { | |
b4a11878 | 159 | |
b3b4ebab | 160 | arg_type = ANY_LUKS; |
fb4650aa ZJS |
161 | r = safe_atoi(val, &arg_key_slot); |
162 | if (r < 0) { | |
163 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
b4a11878 CS |
164 | return 0; |
165 | } | |
166 | ||
fb4650aa | 167 | } else if ((val = startswith(option, "tcrypt-keyfile="))) { |
8cf3ca80 | 168 | |
f75cac37 | 169 | arg_type = CRYPT_TCRYPT; |
fb4650aa ZJS |
170 | if (path_is_absolute(val)) { |
171 | if (strv_extend(&arg_tcrypt_keyfiles, val) < 0) | |
4b93637f LP |
172 | return log_oom(); |
173 | } else | |
fb4650aa | 174 | log_error("Key file path \"%s\" is not absolute. Ignoring.", val); |
8cf3ca80 | 175 | |
fb4650aa | 176 | } else if ((val = startswith(option, "keyfile-size="))) { |
4271d823 | 177 | |
fb4650aa ZJS |
178 | r = safe_atou(val, &arg_keyfile_size); |
179 | if (r < 0) { | |
180 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
4271d823 TG |
181 | return 0; |
182 | } | |
183 | ||
fb4650aa | 184 | } else if ((val = startswith(option, "keyfile-offset="))) { |
880a599e | 185 | |
d90874b4 | 186 | r = safe_atou64(val, &arg_keyfile_offset); |
fb4650aa ZJS |
187 | if (r < 0) { |
188 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
880a599e TG |
189 | return 0; |
190 | } | |
191 | ||
d3d49e76 LP |
192 | } else if ((val = startswith(option, "keyfile-erase="))) { |
193 | ||
194 | r = parse_boolean(val); | |
195 | if (r < 0) { | |
196 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
197 | return 0; | |
198 | } | |
199 | ||
200 | arg_keyfile_erase = r; | |
201 | ||
202 | } else if (streq(option, "keyfile-erase")) | |
203 | arg_keyfile_erase = true; | |
204 | ||
205 | else if ((val = startswith(option, "hash="))) { | |
fb4650aa ZJS |
206 | r = free_and_strdup(&arg_hash, val); |
207 | if (r < 0) | |
4b93637f | 208 | return log_oom(); |
7f4e0805 | 209 | |
fb4650aa | 210 | } else if ((val = startswith(option, "header="))) { |
b3b4ebab | 211 | arg_type = ANY_LUKS; |
7376e835 | 212 | |
baaa35ad ZJS |
213 | if (!path_is_absolute(val)) |
214 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
215 | "Header path \"%s\" is not absolute, refusing.", val); | |
7376e835 | 216 | |
baaa35ad ZJS |
217 | if (arg_header) |
218 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
219 | "Duplicate header= option, refusing."); | |
7376e835 | 220 | |
fb4650aa | 221 | arg_header = strdup(val); |
7376e835 AC |
222 | if (!arg_header) |
223 | return log_oom(); | |
224 | ||
fb4650aa | 225 | } else if ((val = startswith(option, "tries="))) { |
7f4e0805 | 226 | |
fb4650aa ZJS |
227 | r = safe_atou(val, &arg_tries); |
228 | if (r < 0) { | |
229 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
230 | return 0; |
231 | } | |
232 | ||
f75cac37 LP |
233 | } else if (STR_IN_SET(option, "readonly", "read-only")) |
234 | arg_readonly = true; | |
7f4e0805 | 235 | else if (streq(option, "verify")) |
f75cac37 | 236 | arg_verify = true; |
1fa94a31 SB |
237 | else if (streq(option, "silent")) |
238 | arg_silent = true; | |
f75cac37 LP |
239 | else if (STR_IN_SET(option, "allow-discards", "discard")) |
240 | arg_discards = true; | |
2c65512e YW |
241 | else if (streq(option, "same-cpu-crypt")) |
242 | arg_same_cpu_crypt = true; | |
243 | else if (streq(option, "submit-from-crypt-cpus")) | |
244 | arg_submit_from_crypt_cpus = true; | |
227acf00 JU |
245 | else if (streq(option, "no-read-workqueue")) |
246 | arg_no_read_workqueue = true; | |
247 | else if (streq(option, "no-write-workqueue")) | |
248 | arg_no_write_workqueue = true; | |
260ab287 | 249 | else if (streq(option, "luks")) |
b3b4ebab | 250 | arg_type = ANY_LUKS; |
6cc27c29 MF |
251 | /* since cryptsetup 2.3.0 (Feb 2020) */ |
252 | #ifdef CRYPT_BITLK | |
253 | else if (streq(option, "bitlk")) | |
254 | arg_type = CRYPT_BITLK; | |
255 | #endif | |
8cf3ca80 | 256 | else if (streq(option, "tcrypt")) |
f75cac37 | 257 | arg_type = CRYPT_TCRYPT; |
8ced40c0 | 258 | else if (STR_IN_SET(option, "tcrypt-hidden", "tcrypthidden")) { |
f75cac37 LP |
259 | arg_type = CRYPT_TCRYPT; |
260 | arg_tcrypt_hidden = true; | |
8cf3ca80 | 261 | } else if (streq(option, "tcrypt-system")) { |
f75cac37 LP |
262 | arg_type = CRYPT_TCRYPT; |
263 | arg_tcrypt_system = true; | |
8ced40c0 | 264 | } else if (STR_IN_SET(option, "tcrypt-veracrypt", "veracrypt")) { |
52028838 GH |
265 | arg_type = CRYPT_TCRYPT; |
266 | arg_tcrypt_veracrypt = true; | |
53ac130b LP |
267 | } else if (STR_IN_SET(option, "plain", "swap", "tmp") || |
268 | startswith(option, "tmp=")) | |
f75cac37 | 269 | arg_type = CRYPT_PLAIN; |
fb4650aa | 270 | else if ((val = startswith(option, "timeout="))) { |
7f4e0805 | 271 | |
0004f698 | 272 | r = parse_sec_fix_0(val, &arg_timeout); |
fb4650aa ZJS |
273 | if (r < 0) { |
274 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
275 | return 0; |
276 | } | |
277 | ||
fb4650aa | 278 | } else if ((val = startswith(option, "offset="))) { |
4eac2773 | 279 | |
fb4650aa ZJS |
280 | r = safe_atou64(val, &arg_offset); |
281 | if (r < 0) | |
282 | return log_error_errno(r, "Failed to parse %s: %m", option); | |
4eac2773 | 283 | |
fb4650aa | 284 | } else if ((val = startswith(option, "skip="))) { |
4eac2773 | 285 | |
fb4650aa ZJS |
286 | r = safe_atou64(val, &arg_skip); |
287 | if (r < 0) | |
288 | return log_error_errno(r, "Failed to parse %s: %m", option); | |
4eac2773 | 289 | |
08669709 LP |
290 | } else if ((val = startswith(option, "pkcs11-uri="))) { |
291 | ||
b997d111 LP |
292 | if (streq(val, "auto")) { |
293 | arg_pkcs11_uri = mfree(arg_pkcs11_uri); | |
294 | arg_pkcs11_uri_auto = true; | |
295 | } else { | |
296 | if (!pkcs11_uri_valid(val)) | |
297 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "pkcs11-uri= parameter expects a PKCS#11 URI, refusing"); | |
08669709 | 298 | |
b997d111 LP |
299 | r = free_and_strdup(&arg_pkcs11_uri, val); |
300 | if (r < 0) | |
301 | return log_oom(); | |
302 | ||
303 | arg_pkcs11_uri_auto = false; | |
304 | } | |
08669709 | 305 | |
2bc5c425 LP |
306 | } else if ((val = startswith(option, "fido2-device="))) { |
307 | ||
308 | if (streq(val, "auto")) { | |
309 | arg_fido2_device = mfree(arg_fido2_device); | |
310 | arg_fido2_device_auto = true; | |
311 | } else { | |
312 | r = free_and_strdup(&arg_fido2_device, val); | |
313 | if (r < 0) | |
314 | return log_oom(); | |
315 | ||
316 | arg_fido2_device_auto = false; | |
317 | } | |
318 | ||
319 | } else if ((val = startswith(option, "fido2-cid="))) { | |
320 | ||
321 | if (streq(val, "auto")) | |
322 | arg_fido2_cid = mfree(arg_fido2_cid); | |
323 | else { | |
324 | _cleanup_free_ void *cid = NULL; | |
325 | size_t cid_size; | |
326 | ||
f5fbe71d | 327 | r = unbase64mem(val, SIZE_MAX, &cid, &cid_size); |
2bc5c425 LP |
328 | if (r < 0) |
329 | return log_error_errno(r, "Failed to decode FIDO2 CID data: %m"); | |
330 | ||
331 | free(arg_fido2_cid); | |
332 | arg_fido2_cid = TAKE_PTR(cid); | |
333 | arg_fido2_cid_size = cid_size; | |
334 | } | |
335 | ||
336 | /* Turn on FIDO2 as side-effect, if not turned on yet. */ | |
337 | if (!arg_fido2_device && !arg_fido2_device_auto) | |
338 | arg_fido2_device_auto = true; | |
339 | ||
340 | } else if ((val = startswith(option, "fido2-rp="))) { | |
341 | ||
342 | r = free_and_strdup(&arg_fido2_rp_id, val); | |
343 | if (r < 0) | |
344 | return log_oom(); | |
345 | ||
18843ecc LP |
346 | } else if ((val = startswith(option, "tpm2-device="))) { |
347 | ||
348 | if (streq(val, "auto")) { | |
349 | arg_tpm2_device = mfree(arg_tpm2_device); | |
350 | arg_tpm2_device_auto = true; | |
351 | } else { | |
352 | r = free_and_strdup(&arg_tpm2_device, val); | |
353 | if (r < 0) | |
354 | return log_oom(); | |
355 | ||
356 | arg_tpm2_device_auto = false; | |
357 | } | |
358 | ||
359 | } else if ((val = startswith(option, "tpm2-pcrs="))) { | |
360 | ||
361 | if (isempty(val)) | |
362 | arg_tpm2_pcr_mask = 0; | |
363 | else { | |
364 | uint32_t mask; | |
365 | ||
366 | r = tpm2_parse_pcrs(val, &mask); | |
367 | if (r < 0) | |
368 | return r; | |
369 | ||
370 | if (arg_tpm2_pcr_mask == UINT32_MAX) | |
371 | arg_tpm2_pcr_mask = mask; | |
372 | else | |
373 | arg_tpm2_pcr_mask |= mask; | |
374 | } | |
375 | ||
0ba6f85e LP |
376 | } else if ((val = startswith(option, "try-empty-password="))) { |
377 | ||
378 | r = parse_boolean(val); | |
379 | if (r < 0) { | |
380 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
381 | return 0; | |
382 | } | |
383 | ||
384 | arg_try_empty_password = r; | |
385 | ||
386 | } else if (streq(option, "try-empty-password")) | |
387 | arg_try_empty_password = true; | |
cd5f57bd LB |
388 | else if ((val = startswith(option, "headless="))) { |
389 | ||
390 | r = parse_boolean(val); | |
391 | if (r < 0) { | |
392 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
393 | return 0; | |
394 | } | |
395 | ||
396 | arg_headless = r; | |
397 | } else if (streq(option, "headless")) | |
398 | arg_headless = true; | |
0ba6f85e LP |
399 | |
400 | else if (!streq(option, "x-initrd.attach")) | |
fb4650aa | 401 | log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option); |
7f4e0805 LP |
402 | |
403 | return 0; | |
404 | } | |
405 | ||
406 | static int parse_options(const char *options) { | |
7f4e0805 LP |
407 | assert(options); |
408 | ||
dd2fff3a ZJS |
409 | for (;;) { |
410 | _cleanup_free_ char *word = NULL; | |
411 | int r; | |
412 | ||
7bb553bb | 413 | r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS); |
dd2fff3a | 414 | if (r < 0) |
be36bc1e | 415 | return log_error_errno(r, "Failed to parse options: %m"); |
dd2fff3a ZJS |
416 | if (r == 0) |
417 | break; | |
7f4e0805 | 418 | |
dd2fff3a | 419 | r = parse_one_option(word); |
7f4e0805 LP |
420 | if (r < 0) |
421 | return r; | |
422 | } | |
423 | ||
4eac2773 | 424 | /* sanity-check options */ |
9c5253ff LP |
425 | if (arg_type && !streq(arg_type, CRYPT_PLAIN)) { |
426 | if (arg_offset != 0) | |
4eac2773 | 427 | log_warning("offset= ignored with type %s", arg_type); |
9c5253ff | 428 | if (arg_skip != 0) |
4eac2773 MP |
429 | log_warning("skip= ignored with type %s", arg_type); |
430 | } | |
431 | ||
7f4e0805 LP |
432 | return 0; |
433 | } | |
434 | ||
1ca208fb | 435 | static char* disk_description(const char *path) { |
f75cac37 | 436 | static const char name_fields[] = |
74b1c371 LP |
437 | "DM_NAME\0" |
438 | "ID_MODEL_FROM_DATABASE\0" | |
f75cac37 | 439 | "ID_MODEL\0"; |
74b1c371 | 440 | |
4afd3348 | 441 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
2c740afd | 442 | const char *i, *name; |
b1a2da0a | 443 | struct stat st; |
fadd34dd | 444 | int r; |
b1a2da0a LP |
445 | |
446 | assert(path); | |
447 | ||
448 | if (stat(path, &st) < 0) | |
449 | return NULL; | |
450 | ||
451 | if (!S_ISBLK(st.st_mode)) | |
452 | return NULL; | |
453 | ||
930aa88f | 454 | if (sd_device_new_from_stat_rdev(&device, &st) < 0) |
1ca208fb | 455 | return NULL; |
b1a2da0a | 456 | |
fadd34dd LP |
457 | if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) { |
458 | _cleanup_free_ char *unescaped = NULL; | |
459 | ||
460 | /* ID_PART_ENTRY_NAME uses \x style escaping, using libblkid's blkid_encode_string(). Let's | |
461 | * reverse this here to make the string more human friendly in case people embed spaces or | |
462 | * other weird stuff. */ | |
463 | ||
464 | r = cunescape(name, UNESCAPE_RELAX, &unescaped); | |
465 | if (r < 0) { | |
466 | log_debug_errno(r, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m"); | |
467 | return NULL; | |
468 | } | |
469 | ||
470 | if (!isempty(unescaped) && !string_has_cc(unescaped, NULL)) | |
471 | return TAKE_PTR(unescaped); | |
472 | } | |
473 | ||
474 | /* These need no unescaping. */ | |
2c740afd YW |
475 | NULSTR_FOREACH(i, name_fields) |
476 | if (sd_device_get_property_value(device, i, &name) >= 0 && | |
477 | !isempty(name)) | |
1ca208fb | 478 | return strdup(name); |
b1a2da0a | 479 | |
1ca208fb | 480 | return NULL; |
b1a2da0a LP |
481 | } |
482 | ||
b61e476f | 483 | static char *disk_mount_point(const char *label) { |
e7d90b71 | 484 | _cleanup_free_ char *device = NULL; |
5862d652 | 485 | _cleanup_endmntent_ FILE *f = NULL; |
b61e476f LP |
486 | struct mntent *m; |
487 | ||
488 | /* Yeah, we don't support native systemd unit files here for now */ | |
489 | ||
090685b5 LP |
490 | device = strjoin("/dev/mapper/", label); |
491 | if (!device) | |
5862d652 | 492 | return NULL; |
b61e476f | 493 | |
ed4ad488 | 494 | f = setmntent(fstab_path(), "re"); |
e0295d26 | 495 | if (!f) |
5862d652 | 496 | return NULL; |
b61e476f LP |
497 | |
498 | while ((m = getmntent(f))) | |
5862d652 ZJS |
499 | if (path_equal(m->mnt_fsname, device)) |
500 | return strdup(m->mnt_dir); | |
b61e476f | 501 | |
5862d652 | 502 | return NULL; |
b61e476f LP |
503 | } |
504 | ||
08669709 LP |
505 | static char *friendly_disk_name(const char *src, const char *vol) { |
506 | _cleanup_free_ char *description = NULL, *mount_point = NULL; | |
507 | char *name_buffer = NULL; | |
508 | int r; | |
e7d90b71 | 509 | |
e51b9486 | 510 | assert(src); |
08669709 | 511 | assert(vol); |
e7d90b71 | 512 | |
e51b9486 HH |
513 | description = disk_description(src); |
514 | mount_point = disk_mount_point(vol); | |
515 | ||
08669709 | 516 | /* If the description string is simply the volume name, then let's not show this twice */ |
ece174c5 | 517 | if (description && streq(vol, description)) |
97b11eed | 518 | description = mfree(description); |
e51b9486 HH |
519 | |
520 | if (mount_point && description) | |
521 | r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point); | |
522 | else if (mount_point) | |
523 | r = asprintf(&name_buffer, "%s on %s", vol, mount_point); | |
524 | else if (description) | |
525 | r = asprintf(&name_buffer, "%s (%s)", description, vol); | |
08669709 LP |
526 | else |
527 | return strdup(vol); | |
e51b9486 | 528 | if (r < 0) |
08669709 LP |
529 | return NULL; |
530 | ||
531 | return name_buffer; | |
532 | } | |
533 | ||
534 | static int get_password( | |
535 | const char *vol, | |
536 | const char *src, | |
537 | usec_t until, | |
538 | bool accept_cached, | |
539 | char ***ret) { | |
540 | ||
541 | _cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL; | |
542 | _cleanup_strv_free_erase_ char **passwords = NULL; | |
543 | char **p, *id; | |
544 | int r = 0; | |
1fa94a31 | 545 | AskPasswordFlags flags = ASK_PASSWORD_PUSH_CACHE | (arg_silent*ASK_PASSWORD_SILENT); |
08669709 LP |
546 | |
547 | assert(vol); | |
548 | assert(src); | |
549 | assert(ret); | |
550 | ||
cd5f57bd LB |
551 | if (arg_headless) |
552 | return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Password querying disabled via 'headless' option."); | |
553 | ||
08669709 LP |
554 | friendly = friendly_disk_name(src, vol); |
555 | if (!friendly) | |
e51b9486 HH |
556 | return log_oom(); |
557 | ||
08669709 LP |
558 | if (asprintf(&text, "Please enter passphrase for disk %s:", friendly) < 0) |
559 | return log_oom(); | |
e51b9486 | 560 | |
08669709 LP |
561 | disk_path = cescape(src); |
562 | if (!disk_path) | |
e7d90b71 JJ |
563 | return log_oom(); |
564 | ||
ea7e7c1e | 565 | id = strjoina("cryptsetup:", disk_path); |
9fa1de96 | 566 | |
8806bb4b | 567 | r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", "cryptsetup.passphrase", until, |
1fa94a31 | 568 | flags | (accept_cached*ASK_PASSWORD_ACCEPT_CACHED), |
ab84f5b9 | 569 | &passwords); |
23bbb0de MS |
570 | if (r < 0) |
571 | return log_error_errno(r, "Failed to query password: %m"); | |
e7d90b71 | 572 | |
f75cac37 | 573 | if (arg_verify) { |
ab84f5b9 ZJS |
574 | _cleanup_strv_free_erase_ char **passwords2 = NULL; |
575 | ||
1602b008 | 576 | assert(strv_length(passwords) == 1); |
e7d90b71 | 577 | |
08669709 | 578 | if (asprintf(&text, "Please enter passphrase for disk %s (verification):", friendly) < 0) |
ab84f5b9 | 579 | return log_oom(); |
e7d90b71 | 580 | |
ea7e7c1e | 581 | id = strjoina("cryptsetup-verification:", disk_path); |
9fa1de96 | 582 | |
1fa94a31 | 583 | r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", "cryptsetup.passphrase", until, flags, &passwords2); |
ab84f5b9 ZJS |
584 | if (r < 0) |
585 | return log_error_errno(r, "Failed to query verification password: %m"); | |
e7d90b71 JJ |
586 | |
587 | assert(strv_length(passwords2) == 1); | |
588 | ||
8bc6ade7 LP |
589 | if (!streq(passwords[0], passwords2[0])) |
590 | return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN), | |
591 | "Passwords did not match, retrying."); | |
e7d90b71 JJ |
592 | } |
593 | ||
1602b008 | 594 | strv_uniq(passwords); |
e7d90b71 | 595 | |
1602b008 | 596 | STRV_FOREACH(p, passwords) { |
e7d90b71 JJ |
597 | char *c; |
598 | ||
f75cac37 | 599 | if (strlen(*p)+1 >= arg_key_size) |
e7d90b71 JJ |
600 | continue; |
601 | ||
602 | /* Pad password if necessary */ | |
1602b008 | 603 | c = new(char, arg_key_size); |
ab84f5b9 ZJS |
604 | if (!c) |
605 | return log_oom(); | |
e7d90b71 | 606 | |
f75cac37 | 607 | strncpy(c, *p, arg_key_size); |
d3ad474f LP |
608 | erase_and_free(*p); |
609 | *p = TAKE_PTR(c); | |
e7d90b71 JJ |
610 | } |
611 | ||
ae2a15bc | 612 | *ret = TAKE_PTR(passwords); |
1602b008 | 613 | |
ab84f5b9 | 614 | return 0; |
e7d90b71 JJ |
615 | } |
616 | ||
1602b008 LP |
617 | static int attach_tcrypt( |
618 | struct crypt_device *cd, | |
619 | const char *name, | |
620 | const char *key_file, | |
7407f689 LP |
621 | const void *key_data, |
622 | size_t key_data_size, | |
1602b008 LP |
623 | char **passwords, |
624 | uint32_t flags) { | |
625 | ||
8cf3ca80 | 626 | int r = 0; |
d3ad474f | 627 | _cleanup_(erase_and_freep) char *passphrase = NULL; |
8cf3ca80 JJ |
628 | struct crypt_params_tcrypt params = { |
629 | .flags = CRYPT_TCRYPT_LEGACY_MODES, | |
f75cac37 LP |
630 | .keyfiles = (const char **)arg_tcrypt_keyfiles, |
631 | .keyfiles_count = strv_length(arg_tcrypt_keyfiles) | |
8cf3ca80 JJ |
632 | }; |
633 | ||
634 | assert(cd); | |
635 | assert(name); | |
7407f689 | 636 | assert(key_file || key_data || !strv_isempty(passwords)); |
8cf3ca80 | 637 | |
18843ecc | 638 | if (arg_pkcs11_uri || arg_pkcs11_uri_auto || arg_fido2_device || arg_fido2_device_auto || arg_tpm2_device || arg_tpm2_device_auto) |
e514aa1e FS |
639 | /* Ask for a regular password */ |
640 | return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), | |
18843ecc | 641 | "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11/fido2/tpm2 support."); |
08669709 | 642 | |
f75cac37 | 643 | if (arg_tcrypt_hidden) |
8cf3ca80 JJ |
644 | params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER; |
645 | ||
f75cac37 | 646 | if (arg_tcrypt_system) |
8cf3ca80 JJ |
647 | params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER; |
648 | ||
52028838 GH |
649 | if (arg_tcrypt_veracrypt) |
650 | params.flags |= CRYPT_TCRYPT_VERA_MODES; | |
52028838 | 651 | |
7407f689 LP |
652 | if (key_data) { |
653 | params.passphrase = key_data; | |
654 | params.passphrase_size = key_data_size; | |
655 | } else { | |
656 | if (key_file) { | |
657 | r = read_one_line_file(key_file, &passphrase); | |
658 | if (r < 0) { | |
659 | log_error_errno(r, "Failed to read password file '%s': %m", key_file); | |
660 | return -EAGAIN; /* log with the actual error, but return EAGAIN */ | |
661 | } | |
8cf3ca80 | 662 | |
7407f689 LP |
663 | params.passphrase = passphrase; |
664 | } else | |
665 | params.passphrase = passwords[0]; | |
666 | ||
667 | params.passphrase_size = strlen(params.passphrase); | |
668 | } | |
8cf3ca80 JJ |
669 | |
670 | r = crypt_load(cd, CRYPT_TCRYPT, ¶ms); | |
671 | if (r < 0) { | |
7407f689 | 672 | if (r == -EPERM) { |
cb6c9283 | 673 | if (key_data) |
7407f689 | 674 | log_error_errno(r, "Failed to activate using discovered key. (Key not correct?)"); |
7407f689 | 675 | |
cb6c9283 | 676 | if (key_file) |
7407f689 | 677 | log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file); |
cb6c9283 LP |
678 | |
679 | return -EAGAIN; /* log the actual error, but return EAGAIN */ | |
6f177c7d LP |
680 | } |
681 | ||
682 | return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd)); | |
8cf3ca80 JJ |
683 | } |
684 | ||
6f177c7d LP |
685 | r = crypt_activate_by_volume_key(cd, name, NULL, 0, flags); |
686 | if (r < 0) | |
687 | return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd)); | |
688 | ||
689 | return 0; | |
8cf3ca80 JJ |
690 | } |
691 | ||
e2c2f868 LP |
692 | static char *make_bindname(const char *volume) { |
693 | char *s; | |
694 | ||
695 | if (asprintf(&s, "@%" PRIx64"/cryptsetup/%s", random_u64(), volume) < 0) | |
696 | return NULL; | |
697 | ||
698 | return s; | |
699 | } | |
700 | ||
8414cd48 LP |
701 | static int make_security_device_monitor(sd_event *event, sd_device_monitor **ret) { |
702 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
703 | int r; | |
704 | ||
705 | assert(ret); | |
706 | ||
707 | r = sd_device_monitor_new(&monitor); | |
708 | if (r < 0) | |
709 | return log_error_errno(r, "Failed to allocate device monitor: %m"); | |
710 | ||
711 | r = sd_device_monitor_filter_add_match_tag(monitor, "security-device"); | |
712 | if (r < 0) | |
713 | return log_error_errno(r, "Failed to configure device monitor: %m"); | |
714 | ||
715 | r = sd_device_monitor_attach_event(monitor, event); | |
716 | if (r < 0) | |
717 | return log_error_errno(r, "Failed to attach device monitor: %m"); | |
718 | ||
719 | r = sd_device_monitor_start(monitor, NULL, NULL); | |
720 | if (r < 0) | |
721 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
722 | ||
723 | *ret = TAKE_PTR(monitor); | |
724 | return 0; | |
725 | } | |
726 | ||
2bc5c425 LP |
727 | static int attach_luks_or_plain_or_bitlk_by_fido2( |
728 | struct crypt_device *cd, | |
729 | const char *name, | |
730 | const char *key_file, | |
731 | const void *key_data, | |
732 | size_t key_data_size, | |
733 | usec_t until, | |
734 | uint32_t flags, | |
735 | bool pass_volume_key) { | |
736 | ||
737 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
738 | _cleanup_(erase_and_freep) void *decrypted_key = NULL; | |
739 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
740 | _cleanup_free_ void *discovered_salt = NULL, *discovered_cid = NULL; | |
741 | size_t discovered_salt_size, discovered_cid_size, cid_size, decrypted_key_size; | |
742 | _cleanup_free_ char *friendly = NULL, *discovered_rp_id = NULL; | |
743 | int keyslot = arg_key_slot, r; | |
744 | const char *rp_id; | |
745 | const void *cid; | |
cde2f860 | 746 | Fido2EnrollFlags required; |
2bc5c425 LP |
747 | |
748 | assert(cd); | |
749 | assert(name); | |
750 | assert(arg_fido2_device || arg_fido2_device_auto); | |
751 | ||
752 | if (arg_fido2_cid) { | |
753 | if (!key_file && !key_data) | |
8205c151 ZJS |
754 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
755 | "FIDO2 mode with manual parameters selected, but no keyfile specified, refusing."); | |
2bc5c425 LP |
756 | |
757 | rp_id = arg_fido2_rp_id; | |
758 | cid = arg_fido2_cid; | |
759 | cid_size = arg_fido2_cid_size; | |
8205c151 | 760 | |
3cc00ba6 LP |
761 | /* For now and for compatibility, if the user explicitly configured FIDO2 support and we do |
762 | * not read FIDO2 metadata off the LUKS2 header, default to the systemd 248 logic, where we | |
763 | * use PIN + UP when needed, and do not configure UV at all. Eventually, we should make this | |
764 | * explicitly configurable. */ | |
765 | required = FIDO2ENROLL_PIN_IF_NEEDED | FIDO2ENROLL_UP_IF_NEEDED | FIDO2ENROLL_UV_OMIT; | |
2bc5c425 LP |
766 | } else { |
767 | r = find_fido2_auto_data( | |
768 | cd, | |
769 | &discovered_rp_id, | |
770 | &discovered_salt, | |
771 | &discovered_salt_size, | |
772 | &discovered_cid, | |
773 | &discovered_cid_size, | |
cde2f860 LB |
774 | &keyslot, |
775 | &required); | |
2bc5c425 LP |
776 | |
777 | if (IN_SET(r, -ENOTUNIQ, -ENXIO)) | |
778 | return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), | |
779 | "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking."); | |
780 | if (r < 0) | |
781 | return r; | |
782 | ||
9d59f5b2 | 783 | if ((required & (FIDO2ENROLL_PIN | FIDO2ENROLL_UP | FIDO2ENROLL_UV)) && arg_headless) |
cde2f860 | 784 | return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), |
06f08719 | 785 | "Local verification is required to unlock this volume, but the 'headless' parameter was set."); |
cde2f860 | 786 | |
2bc5c425 LP |
787 | rp_id = discovered_rp_id; |
788 | key_data = discovered_salt; | |
789 | key_data_size = discovered_salt_size; | |
790 | cid = discovered_cid; | |
791 | cid_size = discovered_cid_size; | |
792 | } | |
793 | ||
794 | friendly = friendly_disk_name(crypt_get_device_name(cd), name); | |
795 | if (!friendly) | |
796 | return log_oom(); | |
797 | ||
798 | for (;;) { | |
799 | bool processed = false; | |
800 | ||
801 | r = acquire_fido2_key( | |
802 | name, | |
803 | friendly, | |
804 | arg_fido2_device, | |
805 | rp_id, | |
806 | cid, cid_size, | |
807 | key_file, arg_keyfile_size, arg_keyfile_offset, | |
808 | key_data, key_data_size, | |
809 | until, | |
cd5f57bd | 810 | arg_headless, |
cde2f860 | 811 | required, |
4858bc0d SB |
812 | &decrypted_key, &decrypted_key_size, |
813 | arg_silent); | |
2bc5c425 LP |
814 | if (r >= 0) |
815 | break; | |
816 | if (r != -EAGAIN) /* EAGAIN means: token not found */ | |
817 | return r; | |
818 | ||
819 | if (!monitor) { | |
820 | /* We didn't find the token. In this case, watch for it via udev. Let's | |
821 | * create an event loop and monitor first. */ | |
822 | ||
823 | assert(!event); | |
824 | ||
825 | r = sd_event_default(&event); | |
826 | if (r < 0) | |
827 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
828 | ||
829 | r = make_security_device_monitor(event, &monitor); | |
830 | if (r < 0) | |
831 | return r; | |
832 | ||
833 | log_notice("Security token not present for unlocking volume %s, please plug it in.", friendly); | |
834 | ||
835 | /* Let's immediately rescan in case the token appeared in the time we needed | |
836 | * to create and configure the monitor */ | |
837 | continue; | |
838 | } | |
839 | ||
840 | for (;;) { | |
841 | /* Wait for one event, and then eat all subsequent events until there are no | |
842 | * further ones */ | |
843 | r = sd_event_run(event, processed ? 0 : UINT64_MAX); | |
844 | if (r < 0) | |
845 | return log_error_errno(r, "Failed to run event loop: %m"); | |
846 | if (r == 0) | |
847 | break; | |
848 | ||
849 | processed = true; | |
850 | } | |
851 | ||
852 | log_debug("Got one or more potentially relevant udev events, rescanning FIDO2..."); | |
853 | } | |
854 | ||
855 | if (pass_volume_key) | |
856 | r = crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags); | |
857 | else { | |
858 | _cleanup_(erase_and_freep) char *base64_encoded = NULL; | |
859 | ||
860 | /* Before using this key as passphrase we base64 encode it, for compat with homed */ | |
861 | ||
862 | r = base64mem(decrypted_key, decrypted_key_size, &base64_encoded); | |
863 | if (r < 0) | |
864 | return log_oom(); | |
865 | ||
866 | r = crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, strlen(base64_encoded), flags); | |
867 | } | |
868 | if (r == -EPERM) { | |
869 | log_error_errno(r, "Failed to activate with FIDO2 decrypted key. (Key incorrect?)"); | |
870 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
871 | } | |
872 | if (r < 0) | |
873 | return log_error_errno(r, "Failed to activate with FIDO2 acquired key: %m"); | |
874 | ||
875 | return 0; | |
876 | } | |
877 | ||
b8c80b56 LP |
878 | static int attach_luks_or_plain_or_bitlk_by_pkcs11( |
879 | struct crypt_device *cd, | |
880 | const char *name, | |
881 | const char *key_file, | |
882 | const void *key_data, | |
883 | size_t key_data_size, | |
884 | usec_t until, | |
885 | uint32_t flags, | |
886 | bool pass_volume_key) { | |
887 | ||
888 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
889 | _cleanup_free_ char *friendly = NULL, *discovered_uri = NULL; | |
890 | size_t decrypted_key_size = 0, discovered_key_size = 0; | |
891 | _cleanup_(erase_and_freep) void *decrypted_key = NULL; | |
892 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
893 | _cleanup_free_ void *discovered_key = NULL; | |
894 | int keyslot = arg_key_slot, r; | |
895 | const char *uri; | |
896 | ||
897 | assert(cd); | |
898 | assert(name); | |
899 | assert(arg_pkcs11_uri || arg_pkcs11_uri_auto); | |
900 | ||
901 | if (arg_pkcs11_uri_auto) { | |
902 | r = find_pkcs11_auto_data(cd, &discovered_uri, &discovered_key, &discovered_key_size, &keyslot); | |
903 | if (IN_SET(r, -ENOTUNIQ, -ENXIO)) | |
904 | return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), | |
905 | "Automatic PKCS#11 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking."); | |
906 | if (r < 0) | |
907 | return r; | |
908 | ||
909 | uri = discovered_uri; | |
910 | key_data = discovered_key; | |
911 | key_data_size = discovered_key_size; | |
912 | } else { | |
913 | uri = arg_pkcs11_uri; | |
914 | ||
915 | if (!key_file && !key_data) | |
916 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 mode selected but no key file specified, refusing."); | |
917 | } | |
918 | ||
919 | friendly = friendly_disk_name(crypt_get_device_name(cd), name); | |
920 | if (!friendly) | |
921 | return log_oom(); | |
922 | ||
923 | for (;;) { | |
924 | bool processed = false; | |
925 | ||
926 | r = decrypt_pkcs11_key( | |
927 | name, | |
928 | friendly, | |
929 | uri, | |
930 | key_file, arg_keyfile_size, arg_keyfile_offset, | |
931 | key_data, key_data_size, | |
932 | until, | |
cd5f57bd | 933 | arg_headless, |
b8c80b56 LP |
934 | &decrypted_key, &decrypted_key_size); |
935 | if (r >= 0) | |
936 | break; | |
937 | if (r != -EAGAIN) /* EAGAIN means: token not found */ | |
938 | return r; | |
939 | ||
940 | if (!monitor) { | |
941 | /* We didn't find the token. In this case, watch for it via udev. Let's | |
942 | * create an event loop and monitor first. */ | |
943 | ||
944 | assert(!event); | |
945 | ||
946 | r = sd_event_default(&event); | |
947 | if (r < 0) | |
948 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
949 | ||
950 | r = make_security_device_monitor(event, &monitor); | |
951 | if (r < 0) | |
952 | return r; | |
953 | ||
954 | log_notice("Security token %s not present for unlocking volume %s, please plug it in.", | |
955 | uri, friendly); | |
956 | ||
957 | /* Let's immediately rescan in case the token appeared in the time we needed | |
958 | * to create and configure the monitor */ | |
959 | continue; | |
960 | } | |
961 | ||
962 | for (;;) { | |
963 | /* Wait for one event, and then eat all subsequent events until there are no | |
964 | * further ones */ | |
965 | r = sd_event_run(event, processed ? 0 : UINT64_MAX); | |
966 | if (r < 0) | |
967 | return log_error_errno(r, "Failed to run event loop: %m"); | |
968 | if (r == 0) | |
969 | break; | |
970 | ||
971 | processed = true; | |
972 | } | |
973 | ||
974 | log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11..."); | |
975 | } | |
976 | ||
977 | if (pass_volume_key) | |
978 | r = crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags); | |
979 | else { | |
980 | _cleanup_(erase_and_freep) char *base64_encoded = NULL; | |
981 | ||
982 | /* Before using this key as passphrase we base64 encode it. Why? For compatibility | |
983 | * with homed's PKCS#11 hookup: there we want to use the key we acquired through | |
984 | * PKCS#11 for other authentication/decryption mechanisms too, and some of them do | |
985 | * not not take arbitrary binary blobs, but require NUL-terminated strings — most | |
986 | * importantly UNIX password hashes. Hence, for compatibility we want to use a string | |
987 | * without embedded NUL here too, and that's easiest to generate from a binary blob | |
988 | * via base64 encoding. */ | |
989 | ||
990 | r = base64mem(decrypted_key, decrypted_key_size, &base64_encoded); | |
991 | if (r < 0) | |
992 | return log_oom(); | |
993 | ||
994 | r = crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, strlen(base64_encoded), flags); | |
995 | } | |
996 | if (r == -EPERM) { | |
997 | log_error_errno(r, "Failed to activate with PKCS#11 decrypted key. (Key incorrect?)"); | |
998 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
999 | } | |
1000 | if (r < 0) | |
1001 | return log_error_errno(r, "Failed to activate with PKCS#11 acquired key: %m"); | |
1002 | ||
1003 | return 0; | |
1004 | } | |
1005 | ||
18843ecc LP |
1006 | static int make_tpm2_device_monitor(sd_event *event, sd_device_monitor **ret) { |
1007 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
1008 | int r; | |
1009 | ||
1010 | assert(ret); | |
1011 | ||
1012 | r = sd_device_monitor_new(&monitor); | |
1013 | if (r < 0) | |
1014 | return log_error_errno(r, "Failed to allocate device monitor: %m"); | |
1015 | ||
1016 | r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "tpmrm", NULL); | |
1017 | if (r < 0) | |
1018 | return log_error_errno(r, "Failed to configure device monitor: %m"); | |
1019 | ||
1020 | r = sd_device_monitor_attach_event(monitor, event); | |
1021 | if (r < 0) | |
1022 | return log_error_errno(r, "Failed to attach device monitor: %m"); | |
1023 | ||
1024 | r = sd_device_monitor_start(monitor, NULL, NULL); | |
1025 | if (r < 0) | |
1026 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
1027 | ||
1028 | *ret = TAKE_PTR(monitor); | |
1029 | return 0; | |
1030 | } | |
1031 | ||
1032 | static int attach_luks_or_plain_or_bitlk_by_tpm2( | |
1033 | struct crypt_device *cd, | |
1034 | const char *name, | |
1035 | const char *key_file, | |
1036 | const void *key_data, | |
1037 | size_t key_data_size, | |
1038 | usec_t until, | |
1039 | uint32_t flags, | |
1040 | bool pass_volume_key) { | |
1041 | ||
1042 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
1043 | _cleanup_(erase_and_freep) void *decrypted_key = NULL; | |
1044 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
1045 | _cleanup_free_ char *friendly = NULL; | |
1046 | int keyslot = arg_key_slot, r; | |
050f05c2 | 1047 | size_t decrypted_key_size = 0; /* avoid false maybe-uninitialized warning */ |
18843ecc LP |
1048 | |
1049 | assert(cd); | |
1050 | assert(name); | |
1051 | assert(arg_tpm2_device || arg_tpm2_device_auto); | |
1052 | ||
1053 | friendly = friendly_disk_name(crypt_get_device_name(cd), name); | |
1054 | if (!friendly) | |
1055 | return log_oom(); | |
1056 | ||
1057 | for (;;) { | |
1058 | bool processed = false; | |
1059 | ||
1060 | if (key_file || key_data) { | |
1061 | /* If key data is specified, use that */ | |
1062 | ||
1063 | r = acquire_tpm2_key( | |
1064 | name, | |
1065 | arg_tpm2_device, | |
1066 | arg_tpm2_pcr_mask == UINT32_MAX ? TPM2_PCR_MASK_DEFAULT : arg_tpm2_pcr_mask, | |
1067 | key_file, arg_keyfile_size, arg_keyfile_offset, | |
1068 | key_data, key_data_size, | |
1069 | NULL, 0, /* we don't know the policy hash */ | |
1070 | &decrypted_key, &decrypted_key_size); | |
1071 | if (r >= 0) | |
1072 | break; | |
1073 | if (r != -EAGAIN) /* EAGAIN means: no tpm2 chip found */ | |
1074 | return r; | |
1075 | } else { | |
1076 | _cleanup_free_ void *blob = NULL, *policy_hash = NULL; | |
1077 | size_t blob_size, policy_hash_size; | |
1078 | bool found_some = false; | |
1079 | int token = 0; /* first token to look at */ | |
1080 | ||
1081 | /* If no key data is specified, look for it in the header. In order to support | |
1082 | * software upgrades we'll iterate through all suitable tokens, maybe one of them | |
1083 | * works. */ | |
1084 | ||
1085 | for (;;) { | |
1086 | uint32_t pcr_mask; | |
1087 | ||
1088 | r = find_tpm2_auto_data( | |
1089 | cd, | |
1090 | arg_tpm2_pcr_mask, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */ | |
1091 | token, /* search for the token with this index, or any later index than this */ | |
1092 | &pcr_mask, | |
1093 | &blob, &blob_size, | |
1094 | &policy_hash, &policy_hash_size, | |
1095 | &keyslot, | |
1096 | &token); | |
c26f7dd9 | 1097 | if (r == -ENXIO) |
45861042 | 1098 | /* No further TPM2 tokens found in the LUKS2 header.*/ |
c26f7dd9 ZJS |
1099 | return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), |
1100 | found_some | |
1101 | ? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking." | |
1102 | : "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking."); | |
18843ecc LP |
1103 | if (r < 0) |
1104 | return r; | |
1105 | ||
1106 | found_some = true; | |
1107 | ||
1108 | r = acquire_tpm2_key( | |
1109 | name, | |
1110 | arg_tpm2_device, | |
1111 | pcr_mask, | |
1112 | NULL, 0, 0, /* no key file */ | |
1113 | blob, blob_size, | |
1114 | policy_hash, policy_hash_size, | |
1115 | &decrypted_key, &decrypted_key_size); | |
1116 | if (r != -EPERM) | |
1117 | break; | |
1118 | ||
1119 | token++; /* try a different token next time */ | |
1120 | } | |
1121 | ||
1122 | if (r >= 0) | |
1123 | break; | |
1124 | if (r != -EAGAIN) /* EAGAIN means: no tpm2 chip found */ | |
1125 | return r; | |
1126 | } | |
c26f7dd9 | 1127 | assert(decrypted_key); |
18843ecc LP |
1128 | |
1129 | if (!monitor) { | |
1130 | /* We didn't find the TPM2 device. In this case, watch for it via udev. Let's create | |
1131 | * an event loop and monitor first. */ | |
1132 | ||
1133 | assert(!event); | |
1134 | ||
1135 | r = sd_event_default(&event); | |
1136 | if (r < 0) | |
1137 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
1138 | ||
1139 | r = make_tpm2_device_monitor(event, &monitor); | |
1140 | if (r < 0) | |
1141 | return r; | |
1142 | ||
1143 | log_info("TPM2 device not present for unlocking %s, waiting for it to become available.", friendly); | |
1144 | ||
1145 | /* Let's immediately rescan in case the device appeared in the time we needed | |
1146 | * to create and configure the monitor */ | |
1147 | continue; | |
1148 | } | |
1149 | ||
1150 | for (;;) { | |
1151 | /* Wait for one event, and then eat all subsequent events until there are no | |
1152 | * further ones */ | |
1153 | r = sd_event_run(event, processed ? 0 : UINT64_MAX); | |
1154 | if (r < 0) | |
1155 | return log_error_errno(r, "Failed to run event loop: %m"); | |
1156 | if (r == 0) | |
1157 | break; | |
1158 | ||
1159 | processed = true; | |
1160 | } | |
1161 | ||
1162 | log_debug("Got one or more potentially relevant udev events, rescanning for TPM2..."); | |
1163 | } | |
1164 | ||
1165 | if (pass_volume_key) | |
1166 | r = crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags); | |
1167 | else { | |
1168 | _cleanup_(erase_and_freep) char *base64_encoded = NULL; | |
1169 | ||
1170 | /* Before using this key as passphrase we base64 encode it, for compat with homed */ | |
1171 | ||
1172 | r = base64mem(decrypted_key, decrypted_key_size, &base64_encoded); | |
1173 | if (r < 0) | |
1174 | return log_oom(); | |
1175 | ||
1176 | r = crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, strlen(base64_encoded), flags); | |
1177 | } | |
1178 | if (r == -EPERM) { | |
1179 | log_error_errno(r, "Failed to activate with TPM2 decrypted key. (Key incorrect?)"); | |
1180 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
1181 | } | |
1182 | if (r < 0) | |
1183 | return log_error_errno(r, "Failed to activate with TPM2 acquired key: %m"); | |
1184 | ||
1185 | return 0; | |
1186 | } | |
1187 | ||
b8c80b56 LP |
1188 | static int attach_luks_or_plain_or_bitlk_by_key_data( |
1189 | struct crypt_device *cd, | |
1190 | const char *name, | |
1191 | const void *key_data, | |
1192 | size_t key_data_size, | |
1193 | uint32_t flags, | |
1194 | bool pass_volume_key) { | |
1195 | ||
1196 | int r; | |
1197 | ||
1198 | assert(cd); | |
1199 | assert(name); | |
1200 | assert(key_data); | |
1201 | ||
1202 | if (pass_volume_key) | |
1203 | r = crypt_activate_by_volume_key(cd, name, key_data, key_data_size, flags); | |
1204 | else | |
1205 | r = crypt_activate_by_passphrase(cd, name, arg_key_slot, key_data, key_data_size, flags); | |
1206 | if (r == -EPERM) { | |
1207 | log_error_errno(r, "Failed to activate. (Key incorrect?)"); | |
1208 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
1209 | } | |
1210 | if (r < 0) | |
1211 | return log_error_errno(r, "Failed to activate: %m"); | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | static int attach_luks_or_plain_or_bitlk_by_key_file( | |
1217 | struct crypt_device *cd, | |
1218 | const char *name, | |
1219 | const char *key_file, | |
1220 | uint32_t flags, | |
1221 | bool pass_volume_key) { | |
1222 | ||
1223 | _cleanup_(erase_and_freep) char *kfdata = NULL; | |
1224 | _cleanup_free_ char *bindname = NULL; | |
1225 | size_t kfsize; | |
1226 | int r; | |
1227 | ||
1228 | assert(cd); | |
1229 | assert(name); | |
1230 | assert(key_file); | |
1231 | ||
1232 | /* If we read the key via AF_UNIX, make this client recognizable */ | |
1233 | bindname = make_bindname(name); | |
1234 | if (!bindname) | |
1235 | return log_oom(); | |
1236 | ||
1237 | r = read_full_file_full( | |
1238 | AT_FDCWD, key_file, | |
1239 | arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset, | |
1240 | arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size, | |
1241 | READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET, | |
1242 | bindname, | |
1243 | &kfdata, &kfsize); | |
1244 | if (r == -ENOENT) { | |
1245 | log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file); | |
1246 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
1247 | } | |
1248 | ||
1249 | if (pass_volume_key) | |
1250 | r = crypt_activate_by_volume_key(cd, name, kfdata, kfsize, flags); | |
1251 | else | |
1252 | r = crypt_activate_by_passphrase(cd, name, arg_key_slot, kfdata, kfsize, flags); | |
1253 | if (r == -EPERM) { | |
1254 | log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file); | |
1255 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
1256 | } | |
1257 | if (r < 0) | |
1258 | return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file); | |
1259 | ||
1260 | return 0; | |
1261 | } | |
1262 | ||
1263 | static int attach_luks_or_plain_or_bitlk_by_passphrase( | |
1264 | struct crypt_device *cd, | |
1265 | const char *name, | |
1266 | char **passwords, | |
1267 | uint32_t flags, | |
1268 | bool pass_volume_key) { | |
1269 | ||
1270 | char **p; | |
1271 | int r; | |
1272 | ||
1273 | assert(cd); | |
1274 | assert(name); | |
1275 | ||
1276 | r = -EINVAL; | |
1277 | STRV_FOREACH(p, passwords) { | |
1278 | if (pass_volume_key) | |
1279 | r = crypt_activate_by_volume_key(cd, name, *p, arg_key_size, flags); | |
1280 | else | |
1281 | r = crypt_activate_by_passphrase(cd, name, arg_key_slot, *p, strlen(*p), flags); | |
1282 | if (r >= 0) | |
1283 | break; | |
1284 | } | |
1285 | if (r == -EPERM) { | |
1286 | log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)"); | |
1287 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
1288 | } | |
1289 | if (r < 0) | |
1290 | return log_error_errno(r, "Failed to activate with specified passphrase: %m"); | |
1291 | ||
1292 | return 0; | |
1293 | } | |
1294 | ||
6cc27c29 | 1295 | static int attach_luks_or_plain_or_bitlk( |
9c5253ff LP |
1296 | struct crypt_device *cd, |
1297 | const char *name, | |
1298 | const char *key_file, | |
7407f689 LP |
1299 | const void *key_data, |
1300 | size_t key_data_size, | |
9c5253ff | 1301 | char **passwords, |
08669709 LP |
1302 | uint32_t flags, |
1303 | usec_t until) { | |
9c5253ff | 1304 | |
10fb4e35 | 1305 | bool pass_volume_key = false; |
b8c80b56 | 1306 | int r; |
10fb4e35 JJ |
1307 | |
1308 | assert(cd); | |
1309 | assert(name); | |
10fb4e35 | 1310 | |
2e4beb87 | 1311 | if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) { |
4eac2773 MP |
1312 | struct crypt_params_plain params = { |
1313 | .offset = arg_offset, | |
1314 | .skip = arg_skip, | |
a9fc6406 | 1315 | .sector_size = arg_sector_size, |
4eac2773 | 1316 | }; |
10fb4e35 JJ |
1317 | const char *cipher, *cipher_mode; |
1318 | _cleanup_free_ char *truncated_cipher = NULL; | |
1319 | ||
b8c80b56 | 1320 | if (streq_ptr(arg_hash, "plain")) |
10fb4e35 | 1321 | /* plain isn't a real hash type. it just means "use no hash" */ |
b8c80b56 LP |
1322 | params.hash = NULL; |
1323 | else if (arg_hash) | |
1324 | params.hash = arg_hash; | |
1325 | else if (!key_file) | |
1326 | /* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key | |
1327 | * file is provided */ | |
10fb4e35 JJ |
1328 | params.hash = "ripemd160"; |
1329 | ||
f75cac37 | 1330 | if (arg_cipher) { |
10fb4e35 JJ |
1331 | size_t l; |
1332 | ||
f75cac37 LP |
1333 | l = strcspn(arg_cipher, "-"); |
1334 | truncated_cipher = strndup(arg_cipher, l); | |
10fb4e35 JJ |
1335 | if (!truncated_cipher) |
1336 | return log_oom(); | |
1337 | ||
1338 | cipher = truncated_cipher; | |
f75cac37 | 1339 | cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain"; |
10fb4e35 JJ |
1340 | } else { |
1341 | cipher = "aes"; | |
1342 | cipher_mode = "cbc-essiv:sha256"; | |
1343 | } | |
1344 | ||
aed68083 | 1345 | /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */ |
6131a78b | 1346 | arg_keyfile_size = arg_key_size; |
10fb4e35 | 1347 | |
30747265 | 1348 | /* In contrast to what the name crypt_format() might suggest this doesn't actually format |
aed68083 | 1349 | * anything, it just configures encryption parameters when used for plain mode. */ |
1602b008 | 1350 | r = crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, ¶ms); |
2e4beb87 MB |
1351 | if (r < 0) |
1352 | return log_error_errno(r, "Loading of cryptographic parameters failed: %m"); | |
10fb4e35 JJ |
1353 | |
1354 | /* hash == NULL implies the user passed "plain" */ | |
b8c80b56 | 1355 | pass_volume_key = !params.hash; |
10fb4e35 | 1356 | } |
10fb4e35 JJ |
1357 | |
1358 | log_info("Set cipher %s, mode %s, key size %i bits for device %s.", | |
1359 | crypt_get_cipher(cd), | |
1360 | crypt_get_cipher_mode(cd), | |
1361 | crypt_get_volume_key_size(cd)*8, | |
1362 | crypt_get_device_name(cd)); | |
1363 | ||
18843ecc LP |
1364 | if (arg_tpm2_device || arg_tpm2_device_auto) |
1365 | return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key); | |
2bc5c425 LP |
1366 | if (arg_fido2_device || arg_fido2_device_auto) |
1367 | return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key); | |
b8c80b56 LP |
1368 | if (arg_pkcs11_uri || arg_pkcs11_uri_auto) |
1369 | return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key); | |
1370 | if (key_data) | |
1371 | return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, key_data_size, flags, pass_volume_key); | |
1372 | if (key_file) | |
1373 | return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key); | |
10fb4e35 | 1374 | |
b8c80b56 | 1375 | return attach_luks_or_plain_or_bitlk_by_passphrase(cd, name, passwords, flags, pass_volume_key); |
10fb4e35 JJ |
1376 | } |
1377 | ||
dd5e696d | 1378 | static int help(void) { |
37ec0fdd LP |
1379 | _cleanup_free_ char *link = NULL; |
1380 | int r; | |
1381 | ||
1382 | r = terminal_urlify_man("systemd-cryptsetup@.service", "8", &link); | |
1383 | if (r < 0) | |
1384 | return log_oom(); | |
dd5e696d LP |
1385 | |
1386 | printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n" | |
1387 | "%s detach VOLUME\n\n" | |
37ec0fdd | 1388 | "Attaches or detaches an encrypted block device.\n" |
bc556335 DDM |
1389 | "\nSee the %s for details.\n", |
1390 | program_invocation_short_name, | |
1391 | program_invocation_short_name, | |
1392 | link); | |
dd5e696d LP |
1393 | |
1394 | return 0; | |
1395 | } | |
1396 | ||
d5d1ae15 LP |
1397 | static uint32_t determine_flags(void) { |
1398 | uint32_t flags = 0; | |
1399 | ||
1400 | if (arg_readonly) | |
1401 | flags |= CRYPT_ACTIVATE_READONLY; | |
1402 | ||
1403 | if (arg_discards) | |
1404 | flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS; | |
1405 | ||
1406 | if (arg_same_cpu_crypt) | |
1407 | flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT; | |
1408 | ||
1409 | if (arg_submit_from_crypt_cpus) | |
1410 | flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS; | |
1411 | ||
227acf00 JU |
1412 | if (arg_no_read_workqueue) |
1413 | flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE; | |
1414 | ||
1415 | if (arg_no_write_workqueue) | |
1416 | flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE; | |
1417 | ||
408c81f6 MS |
1418 | #ifdef CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF |
1419 | /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */ | |
1420 | /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */ | |
1421 | flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF; | |
1422 | #endif | |
1423 | ||
d5d1ae15 LP |
1424 | return flags; |
1425 | } | |
1426 | ||
d3d49e76 LP |
1427 | static void remove_and_erasep(const char **p) { |
1428 | int r; | |
1429 | ||
1430 | if (!*p) | |
1431 | return; | |
1432 | ||
1433 | r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE); | |
1434 | if (r < 0 && r != -ENOENT) | |
1435 | log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p); | |
1436 | } | |
1437 | ||
3a40f366 | 1438 | static int run(int argc, char *argv[]) { |
294bd454 | 1439 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
da2268f9 | 1440 | const char *verb; |
3a40f366 | 1441 | int r; |
e23a0ce8 | 1442 | |
3a40f366 YW |
1443 | if (argc <= 1) |
1444 | return help(); | |
dd5e696d | 1445 | |
d7a0f1f4 FS |
1446 | if (argc < 3) |
1447 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1448 | "This program requires at least two arguments."); | |
e23a0ce8 | 1449 | |
d2acb93d | 1450 | log_setup(); |
e23a0ce8 | 1451 | |
2f678640 | 1452 | cryptsetup_enable_logging(NULL); |
568a8404 | 1453 | |
4c12626c LP |
1454 | umask(0022); |
1455 | ||
da2268f9 LP |
1456 | verb = argv[1]; |
1457 | ||
1458 | if (streq(verb, "attach")) { | |
d3d49e76 | 1459 | _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL; |
7407f689 | 1460 | _cleanup_(erase_and_freep) void *key_data = NULL; |
da2268f9 LP |
1461 | const char *volume, *source, *key_file, *options; |
1462 | crypt_status_info status; | |
7407f689 | 1463 | size_t key_data_size = 0; |
da2268f9 LP |
1464 | uint32_t flags = 0; |
1465 | unsigned tries; | |
1466 | usec_t until; | |
7f4e0805 | 1467 | |
b61e476f LP |
1468 | /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */ |
1469 | ||
0ffff81a LP |
1470 | if (argc < 4) |
1471 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach requires at least two arguments."); | |
7f4e0805 | 1472 | |
da2268f9 LP |
1473 | volume = argv[2]; |
1474 | source = argv[3]; | |
1475 | key_file = argc >= 5 && !STR_IN_SET(argv[4], "", "-", "none") ? argv[4] : NULL; | |
1476 | options = argc >= 6 && !STR_IN_SET(argv[5], "", "-", "none") ? argv[5] : NULL; | |
7407f689 | 1477 | |
da2268f9 LP |
1478 | if (!filename_is_valid(volume)) |
1479 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume); | |
1480 | ||
1481 | if (key_file && !path_is_absolute(key_file)) { | |
1482 | log_warning("Password file path '%s' is not absolute. Ignoring.", key_file); | |
1483 | key_file = NULL; | |
7f4e0805 LP |
1484 | } |
1485 | ||
da2268f9 LP |
1486 | if (options) { |
1487 | r = parse_options(options); | |
3a40f366 YW |
1488 | if (r < 0) |
1489 | return r; | |
74b1c371 | 1490 | } |
e23a0ce8 | 1491 | |
7bb553bb | 1492 | log_debug("%s %s ← %s type=%s cipher=%s", __func__, |
da2268f9 | 1493 | volume, source, strempty(arg_type), strempty(arg_cipher)); |
7bb553bb | 1494 | |
b853f6e9 | 1495 | /* A delicious drop of snake oil */ |
9c5253ff | 1496 | (void) mlockall(MCL_FUTURE); |
b853f6e9 | 1497 | |
7407f689 | 1498 | if (!key_file) { |
e060ed32 | 1499 | _cleanup_free_ char *bindname = NULL; |
7407f689 LP |
1500 | const char *fn; |
1501 | ||
da2268f9 | 1502 | bindname = make_bindname(volume); |
e060ed32 LP |
1503 | if (!bindname) |
1504 | return log_oom(); | |
1505 | ||
7407f689 LP |
1506 | /* If a key file is not explicitly specified, search for a key in a well defined |
1507 | * search path, and load it. */ | |
1508 | ||
da2268f9 | 1509 | fn = strjoina(volume, ".key"); |
e060ed32 LP |
1510 | r = find_key_file( |
1511 | fn, | |
1512 | STRV_MAKE("/etc/cryptsetup-keys.d", "/run/cryptsetup-keys.d"), | |
1513 | bindname, | |
1514 | &key_data, &key_data_size); | |
7407f689 LP |
1515 | if (r < 0) |
1516 | return r; | |
1517 | if (r > 0) | |
da2268f9 | 1518 | log_debug("Automatically discovered key for volume '%s'.", volume); |
7407f689 | 1519 | } else if (arg_keyfile_erase) |
d3d49e76 LP |
1520 | destroy_key_file = key_file; /* let's get this baby erased when we leave */ |
1521 | ||
7376e835 AC |
1522 | if (arg_header) { |
1523 | log_debug("LUKS header: %s", arg_header); | |
5f4bfe56 | 1524 | r = crypt_init(&cd, arg_header); |
7376e835 | 1525 | } else |
da2268f9 | 1526 | r = crypt_init(&cd, source); |
3a40f366 YW |
1527 | if (r < 0) |
1528 | return log_error_errno(r, "crypt_init() failed: %m"); | |
e23a0ce8 | 1529 | |
efc3b12f | 1530 | cryptsetup_enable_logging(cd); |
7f4e0805 | 1531 | |
da2268f9 | 1532 | status = crypt_status(cd, volume); |
3742095b | 1533 | if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) { |
da2268f9 | 1534 | log_info("Volume %s already active.", volume); |
3a40f366 | 1535 | return 0; |
7f4e0805 LP |
1536 | } |
1537 | ||
d5d1ae15 | 1538 | flags = determine_flags(); |
2c65512e | 1539 | |
496db330 YW |
1540 | until = usec_add(now(CLOCK_MONOTONIC), arg_timeout); |
1541 | if (until == USEC_INFINITY) | |
7dcda352 | 1542 | until = 0; |
260ab287 | 1543 | |
6131a78b | 1544 | arg_key_size = (arg_key_size > 0 ? arg_key_size : (256 / 8)); |
e2d480b9 | 1545 | |
10fb4e35 JJ |
1546 | if (key_file) { |
1547 | struct stat st; | |
e2d480b9 | 1548 | |
10fb4e35 JJ |
1549 | /* Ideally we'd do this on the open fd, but since this is just a |
1550 | * warning it's OK to do this in two steps. */ | |
3f4d56a0 MP |
1551 | if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005)) |
1552 | log_warning("Key file %s is world-readable. This is not a good idea!", key_file); | |
e2d480b9 LP |
1553 | } |
1554 | ||
6930d069 LP |
1555 | if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) { |
1556 | r = crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL); | |
ea9a9d49 MB |
1557 | if (r < 0) |
1558 | return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd)); | |
1559 | ||
1560 | if (arg_header) { | |
da2268f9 | 1561 | r = crypt_set_data_device(cd, source); |
ea9a9d49 | 1562 | if (r < 0) |
da2268f9 | 1563 | return log_error_errno(r, "Failed to set LUKS data device %s: %m", source); |
ea9a9d49 | 1564 | } |
d90874b4 | 1565 | |
894bb3ca | 1566 | /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */ |
7407f689 | 1567 | if (!key_file && !key_data) { |
da2268f9 | 1568 | r = crypt_activate_by_token(cd, volume, CRYPT_ANY_TOKEN, NULL, flags); |
894bb3ca | 1569 | if (r >= 0) { |
da2268f9 | 1570 | log_debug("Volume %s activated with LUKS token id %i.", volume, r); |
894bb3ca MB |
1571 | return 0; |
1572 | } | |
1573 | ||
1574 | log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd)); | |
1575 | } | |
ea9a9d49 MB |
1576 | } |
1577 | ||
6cc27c29 MF |
1578 | /* since cryptsetup 2.3.0 (Feb 2020) */ |
1579 | #ifdef CRYPT_BITLK | |
5af39ac8 | 1580 | if (streq_ptr(arg_type, CRYPT_BITLK)) { |
6cc27c29 MF |
1581 | r = crypt_load(cd, CRYPT_BITLK, NULL); |
1582 | if (r < 0) | |
1583 | return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd)); | |
1584 | } | |
1585 | #endif | |
1586 | ||
f75cac37 | 1587 | for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) { |
ab84f5b9 | 1588 | _cleanup_strv_free_erase_ char **passwords = NULL; |
260ab287 | 1589 | |
0ba6f85e LP |
1590 | /* When we were able to acquire multiple keys, let's always process them in this order: |
1591 | * | |
18843ecc | 1592 | * 1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip |
0ba6f85e LP |
1593 | * 2. The discovered key: i.e. key_data + key_data_size |
1594 | * 3. The configured key: i.e. key_file + arg_keyfile_offset + arg_keyfile_size | |
1595 | * 4. The empty password, in case arg_try_empty_password is set | |
1596 | * 5. We enquire the user for a password | |
1597 | */ | |
1598 | ||
18843ecc | 1599 | if (!key_file && !key_data && !arg_pkcs11_uri && !arg_pkcs11_uri_auto && !arg_fido2_device && !arg_fido2_device_auto && !arg_tpm2_device && !arg_tpm2_device_auto) { |
0ba6f85e LP |
1600 | |
1601 | if (arg_try_empty_password) { | |
1602 | /* Hmm, let's try an empty password now, but only once */ | |
1603 | arg_try_empty_password = false; | |
1604 | ||
1605 | key_data = strdup(""); | |
1606 | if (!key_data) | |
1607 | return log_oom(); | |
1608 | ||
1609 | key_data_size = 0; | |
1610 | } else { | |
1611 | /* Ask the user for a passphrase only as last resort, if we have | |
1612 | * nothing else to check for */ | |
1613 | ||
da2268f9 | 1614 | r = get_password(volume, source, until, tries == 0 && !arg_verify, &passwords); |
0ba6f85e LP |
1615 | if (r == -EAGAIN) |
1616 | continue; | |
1617 | if (r < 0) | |
1618 | return r; | |
1619 | } | |
260ab287 LP |
1620 | } |
1621 | ||
f75cac37 | 1622 | if (streq_ptr(arg_type, CRYPT_TCRYPT)) |
da2268f9 | 1623 | r = attach_tcrypt(cd, volume, key_file, key_data, key_data_size, passwords, flags); |
8cf3ca80 | 1624 | else |
da2268f9 | 1625 | r = attach_luks_or_plain_or_bitlk(cd, volume, key_file, key_data, key_data_size, passwords, flags, until); |
5f4bfe56 | 1626 | if (r >= 0) |
260ab287 | 1627 | break; |
6f177c7d LP |
1628 | if (r != -EAGAIN) |
1629 | return r; | |
260ab287 | 1630 | |
2424fb7e LP |
1631 | /* Key not correct? Let's try again! */ |
1632 | ||
6f177c7d | 1633 | key_file = NULL; |
7407f689 LP |
1634 | key_data = erase_and_free(key_data); |
1635 | key_data_size = 0; | |
2424fb7e | 1636 | arg_pkcs11_uri = mfree(arg_pkcs11_uri); |
b997d111 | 1637 | arg_pkcs11_uri_auto = false; |
2bc5c425 LP |
1638 | arg_fido2_device = mfree(arg_fido2_device); |
1639 | arg_fido2_device_auto = false; | |
18843ecc LP |
1640 | arg_tpm2_device = mfree(arg_tpm2_device); |
1641 | arg_tpm2_device_auto = false; | |
7f4e0805 LP |
1642 | } |
1643 | ||
0ffff81a LP |
1644 | if (arg_tries != 0 && tries >= arg_tries) |
1645 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up."); | |
7f4e0805 | 1646 | |
da2268f9 LP |
1647 | } else if (streq(verb, "detach")) { |
1648 | const char *volume; | |
1649 | ||
1650 | volume = argv[2]; | |
7f4e0805 | 1651 | |
da2268f9 LP |
1652 | if (!filename_is_valid(volume)) |
1653 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume); | |
7407f689 | 1654 | |
da2268f9 | 1655 | r = crypt_init_by_name(&cd, volume); |
5f4bfe56 | 1656 | if (r == -ENODEV) { |
da2268f9 | 1657 | log_info("Volume %s already inactive.", volume); |
3a40f366 | 1658 | return 0; |
7f4e0805 | 1659 | } |
3a40f366 YW |
1660 | if (r < 0) |
1661 | return log_error_errno(r, "crypt_init_by_name() failed: %m"); | |
7f4e0805 | 1662 | |
efc3b12f | 1663 | cryptsetup_enable_logging(cd); |
7f4e0805 | 1664 | |
da2268f9 | 1665 | r = crypt_deactivate(cd, volume); |
3a40f366 YW |
1666 | if (r < 0) |
1667 | return log_error_errno(r, "Failed to deactivate: %m"); | |
e23a0ce8 | 1668 | |
0ffff81a | 1669 | } else |
da2268f9 | 1670 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", verb); |
e23a0ce8 | 1671 | |
3a40f366 | 1672 | return 0; |
e23a0ce8 | 1673 | } |
3a40f366 YW |
1674 | |
1675 | DEFINE_MAIN_FUNCTION(run); |