]>
Commit | Line | Data |
---|---|---|
70a5db58 LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <libfdisk.h> | |
4 | #include <linux/loop.h> | |
5 | #include <poll.h> | |
6 | #include <sys/file.h> | |
7 | #include <sys/ioctl.h> | |
8 | ||
9 | #include "blkid-util.h" | |
10 | #include "blockdev-util.h" | |
0be94a19 | 11 | #include "btrfs-util.h" |
70a5db58 LP |
12 | #include "chattr-util.h" |
13 | #include "dm-util.h" | |
14 | #include "errno-util.h" | |
15 | #include "fd-util.h" | |
16 | #include "fileio.h" | |
17 | #include "fs-util.h" | |
18 | #include "fsck-util.h" | |
c07bf7a4 | 19 | #include "home-util.h" |
70a5db58 LP |
20 | #include "homework-luks.h" |
21 | #include "homework-mount.h" | |
22 | #include "id128-util.h" | |
23 | #include "io-util.h" | |
24 | #include "memory-util.h" | |
25 | #include "missing_magic.h" | |
26 | #include "mkdir.h" | |
27 | #include "mount-util.h" | |
28 | #include "openssl-util.h" | |
29 | #include "parse-util.h" | |
30 | #include "path-util.h" | |
31 | #include "process-util.h" | |
32 | #include "random-util.h" | |
33 | #include "resize-fs.h" | |
34 | #include "stat-util.h" | |
35 | #include "strv.h" | |
36 | #include "tmpfile-util.h" | |
37 | ||
38 | /* Round down to the nearest 1K size. Note that Linux generally handles block devices with 512 blocks only, | |
39 | * but actually doesn't accept uneven numbers in many cases. To avoid any confusion around this we'll | |
40 | * strictly round disk sizes down to the next 1K boundary.*/ | |
41 | #define DISK_SIZE_ROUND_DOWN(x) ((x) & ~UINT64_C(1023)) | |
42 | ||
70a5db58 LP |
43 | static int probe_file_system_by_fd( |
44 | int fd, | |
45 | char **ret_fstype, | |
46 | sd_id128_t *ret_uuid) { | |
47 | ||
48 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; | |
49 | _cleanup_free_ char *s = NULL; | |
50 | const char *fstype = NULL, *uuid = NULL; | |
51 | sd_id128_t id; | |
52 | int r; | |
53 | ||
54 | assert(fd >= 0); | |
55 | assert(ret_fstype); | |
56 | assert(ret_uuid); | |
57 | ||
58 | b = blkid_new_probe(); | |
59 | if (!b) | |
60 | return -ENOMEM; | |
61 | ||
62 | errno = 0; | |
63 | r = blkid_probe_set_device(b, fd, 0, 0); | |
64 | if (r != 0) | |
65 | return errno > 0 ? -errno : -ENOMEM; | |
66 | ||
67 | (void) blkid_probe_enable_superblocks(b, 1); | |
68 | (void) blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_UUID); | |
69 | ||
70 | errno = 0; | |
71 | r = blkid_do_safeprobe(b); | |
72 | if (IN_SET(r, -2, 1)) /* nothing found or ambiguous result */ | |
73 | return -ENOPKG; | |
74 | if (r != 0) | |
75 | return errno > 0 ? -errno : -EIO; | |
76 | ||
77 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
78 | if (!fstype) | |
79 | return -ENOPKG; | |
80 | ||
81 | (void) blkid_probe_lookup_value(b, "UUID", &uuid, NULL); | |
82 | if (!uuid) | |
83 | return -ENOPKG; | |
84 | ||
85 | r = sd_id128_from_string(uuid, &id); | |
86 | if (r < 0) | |
87 | return r; | |
88 | ||
89 | s = strdup(fstype); | |
90 | if (!s) | |
91 | return -ENOMEM; | |
92 | ||
93 | *ret_fstype = TAKE_PTR(s); | |
94 | *ret_uuid = id; | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | static int probe_file_system_by_path(const char *path, char **ret_fstype, sd_id128_t *ret_uuid) { | |
100 | _cleanup_close_ int fd = -1; | |
101 | ||
102 | fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
103 | if (fd < 0) | |
104 | return -errno; | |
105 | ||
106 | return probe_file_system_by_fd(fd, ret_fstype, ret_uuid); | |
107 | } | |
108 | ||
109 | static int block_get_size_by_fd(int fd, uint64_t *ret) { | |
110 | struct stat st; | |
111 | ||
112 | assert(fd >= 0); | |
113 | assert(ret); | |
114 | ||
115 | if (fstat(fd, &st) < 0) | |
116 | return -errno; | |
117 | ||
118 | if (!S_ISBLK(st.st_mode)) | |
119 | return -ENOTBLK; | |
120 | ||
121 | if (ioctl(fd, BLKGETSIZE64, ret) < 0) | |
122 | return -errno; | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | static int block_get_size_by_path(const char *path, uint64_t *ret) { | |
128 | _cleanup_close_ int fd = -1; | |
129 | ||
130 | fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
131 | if (fd < 0) | |
132 | return -errno; | |
133 | ||
134 | return block_get_size_by_fd(fd, ret); | |
135 | } | |
136 | ||
137 | static int run_fsck(const char *node, const char *fstype) { | |
138 | int r, exit_status; | |
139 | pid_t fsck_pid; | |
140 | ||
141 | assert(node); | |
142 | assert(fstype); | |
143 | ||
144 | r = fsck_exists(fstype); | |
145 | if (r < 0) | |
146 | return log_error_errno(r, "Failed to check if fsck for file system %s exists: %m", fstype); | |
147 | if (r == 0) { | |
148 | log_warning("No fsck for file system %s installed, ignoring.", fstype); | |
149 | return 0; | |
150 | } | |
151 | ||
152 | r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_STDOUT_TO_STDERR, &fsck_pid); | |
153 | if (r < 0) | |
154 | return r; | |
155 | if (r == 0) { | |
156 | /* Child */ | |
157 | execl("/sbin/fsck", "/sbin/fsck", "-aTl", node, NULL); | |
158 | log_error_errno(errno, "Failed to execute fsck: %m"); | |
159 | _exit(FSCK_OPERATIONAL_ERROR); | |
160 | } | |
161 | ||
162 | exit_status = wait_for_terminate_and_check("fsck", fsck_pid, WAIT_LOG_ABNORMAL); | |
163 | if (exit_status < 0) | |
164 | return exit_status; | |
165 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != 0) { | |
166 | log_warning("fsck failed with exit status %i.", exit_status); | |
167 | ||
168 | if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0) | |
169 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "File system is corrupted, refusing."); | |
170 | ||
171 | log_warning("Ignoring fsck error."); | |
172 | } | |
173 | ||
174 | log_info("File system check completed."); | |
175 | ||
176 | return 1; | |
177 | } | |
178 | ||
179 | static int luks_try_passwords( | |
180 | struct crypt_device *cd, | |
181 | char **passwords, | |
182 | void *volume_key, | |
183 | size_t *volume_key_size) { | |
184 | ||
185 | char **pp; | |
186 | int r; | |
187 | ||
188 | assert(cd); | |
189 | ||
190 | STRV_FOREACH(pp, passwords) { | |
191 | size_t vks = *volume_key_size; | |
192 | ||
193 | r = crypt_volume_key_get( | |
194 | cd, | |
195 | CRYPT_ANY_SLOT, | |
196 | volume_key, | |
197 | &vks, | |
198 | *pp, | |
199 | strlen(*pp)); | |
200 | if (r >= 0) { | |
201 | *volume_key_size = vks; | |
202 | return 0; | |
203 | } | |
204 | ||
205 | log_debug_errno(r, "Password %zu didn't work for unlocking LUKS superblock: %m", (size_t) (pp - passwords)); | |
206 | } | |
207 | ||
208 | return -ENOKEY; | |
209 | } | |
210 | ||
211 | static int luks_setup( | |
212 | const char *node, | |
213 | const char *dm_name, | |
214 | sd_id128_t uuid, | |
215 | const char *cipher, | |
216 | const char *cipher_mode, | |
217 | uint64_t volume_key_size, | |
218 | char **passwords, | |
219 | char **pkcs11_decrypted_passwords, | |
220 | bool discard, | |
221 | struct crypt_device **ret, | |
222 | sd_id128_t *ret_found_uuid, | |
223 | void **ret_volume_key, | |
224 | size_t *ret_volume_key_size) { | |
225 | ||
226 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; | |
227 | _cleanup_(erase_and_freep) void *vk = NULL; | |
228 | sd_id128_t p; | |
229 | size_t vks; | |
230 | int r; | |
231 | ||
232 | assert(node); | |
233 | assert(dm_name); | |
234 | assert(ret); | |
235 | ||
236 | r = crypt_init(&cd, node); | |
237 | if (r < 0) | |
238 | return log_error_errno(r, "Failed to allocate libcryptsetup context: %m"); | |
239 | ||
240 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); | |
241 | ||
242 | r = crypt_load(cd, CRYPT_LUKS2, NULL); | |
243 | if (r < 0) | |
244 | return log_error_errno(r, "Failed to load LUKS superblock: %m"); | |
245 | ||
246 | r = crypt_get_volume_key_size(cd); | |
247 | if (r <= 0) | |
248 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine LUKS volume key size"); | |
249 | vks = (size_t) r; | |
250 | ||
251 | if (!sd_id128_is_null(uuid) || ret_found_uuid) { | |
252 | const char *s; | |
253 | ||
254 | s = crypt_get_uuid(cd); | |
255 | if (!s) | |
256 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has no UUID."); | |
257 | ||
258 | r = sd_id128_from_string(s, &p); | |
259 | if (r < 0) | |
260 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has invalid UUID."); | |
261 | ||
262 | /* Check that the UUID matches, if specified */ | |
263 | if (!sd_id128_is_null(uuid) && | |
264 | !sd_id128_equal(uuid, p)) | |
265 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has wrong UUID."); | |
266 | } | |
267 | ||
268 | if (cipher && !streq_ptr(cipher, crypt_get_cipher(cd))) | |
269 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock declares wrong cipher."); | |
270 | ||
271 | if (cipher_mode && !streq_ptr(cipher_mode, crypt_get_cipher_mode(cd))) | |
272 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock declares wrong cipher mode."); | |
273 | ||
274 | if (volume_key_size != UINT64_MAX && vks != volume_key_size) | |
275 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock declares wrong volume key size."); | |
276 | ||
277 | vk = malloc(vks); | |
278 | if (!vk) | |
279 | return log_oom(); | |
280 | ||
281 | r = luks_try_passwords(cd, pkcs11_decrypted_passwords, vk, &vks); | |
282 | if (r == -ENOKEY) { | |
283 | r = luks_try_passwords(cd, passwords, vk, &vks); | |
284 | if (r == -ENOKEY) | |
285 | return log_error_errno(r, "No valid password for LUKS superblock."); | |
286 | } | |
287 | if (r < 0) | |
288 | return log_error_errno(r, "Failed to unlocks LUKS superblock: %m"); | |
289 | ||
290 | r = crypt_activate_by_volume_key( | |
291 | cd, | |
292 | dm_name, | |
293 | vk, vks, | |
294 | discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0); | |
295 | if (r < 0) | |
296 | return log_error_errno(r, "Failed to unlock LUKS superblock: %m"); | |
297 | ||
298 | log_info("Setting up LUKS device /dev/mapper/%s completed.", dm_name); | |
299 | ||
300 | *ret = TAKE_PTR(cd); | |
301 | ||
302 | if (ret_found_uuid) /* Return the UUID actually found if the caller wants to know */ | |
303 | *ret_found_uuid = p; | |
304 | if (ret_volume_key) | |
305 | *ret_volume_key = TAKE_PTR(vk); | |
306 | if (ret_volume_key_size) | |
307 | *ret_volume_key_size = vks; | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | static int luks_open( | |
313 | const char *dm_name, | |
314 | char **passwords, | |
315 | char **pkcs11_decrypted_passwords, | |
316 | struct crypt_device **ret, | |
317 | sd_id128_t *ret_found_uuid, | |
318 | void **ret_volume_key, | |
319 | size_t *ret_volume_key_size) { | |
320 | ||
321 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; | |
322 | _cleanup_(erase_and_freep) void *vk = NULL; | |
323 | sd_id128_t p; | |
324 | size_t vks; | |
325 | int r; | |
326 | ||
327 | assert(dm_name); | |
328 | assert(ret); | |
329 | ||
330 | /* Opens a LUKS device that is already set up. Re-validates the password while doing so (which also | |
331 | * provides us with the volume key, which we want). */ | |
332 | ||
333 | r = crypt_init_by_name(&cd, dm_name); | |
334 | if (r < 0) | |
335 | return log_error_errno(r, "Failed to initialize cryptsetup context for %s: %m", dm_name); | |
336 | ||
337 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); | |
338 | ||
339 | r = crypt_load(cd, CRYPT_LUKS2, NULL); | |
340 | if (r < 0) | |
341 | return log_error_errno(r, "Failed to load LUKS superblock: %m"); | |
342 | ||
343 | r = crypt_get_volume_key_size(cd); | |
344 | if (r <= 0) | |
345 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine LUKS volume key size"); | |
346 | vks = (size_t) r; | |
347 | ||
348 | if (ret_found_uuid) { | |
349 | const char *s; | |
350 | ||
351 | s = crypt_get_uuid(cd); | |
352 | if (!s) | |
353 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has no UUID."); | |
354 | ||
355 | r = sd_id128_from_string(s, &p); | |
356 | if (r < 0) | |
357 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has invalid UUID."); | |
358 | } | |
359 | ||
360 | vk = malloc(vks); | |
361 | if (!vk) | |
362 | return log_oom(); | |
363 | ||
364 | r = luks_try_passwords(cd, pkcs11_decrypted_passwords, vk, &vks); | |
365 | if (r == -ENOKEY) { | |
366 | r = luks_try_passwords(cd, passwords, vk, &vks); | |
367 | if (r == -ENOKEY) | |
368 | return log_error_errno(r, "No valid password for LUKS superblock."); | |
369 | } | |
370 | if (r < 0) | |
371 | return log_error_errno(r, "Failed to unlocks LUKS superblock: %m"); | |
372 | ||
373 | log_info("Discovered used LUKS device /dev/mapper/%s, and validated password.", dm_name); | |
374 | ||
375 | /* This is needed so that crypt_resize() can operate correctly for pre-existing LUKS devices. We need | |
376 | * to tell libcryptsetup the volume key explicitly, so that it is in the kernel keyring. */ | |
377 | r = crypt_activate_by_volume_key(cd, NULL, vk, vks, CRYPT_ACTIVATE_KEYRING_KEY); | |
378 | if (r < 0) | |
379 | return log_error_errno(r, "Failed to upload volume key again: %m"); | |
380 | ||
381 | log_info("Successfully re-activated LUKS device."); | |
382 | ||
383 | *ret = TAKE_PTR(cd); | |
384 | ||
385 | if (ret_found_uuid) | |
386 | *ret_found_uuid = p; | |
387 | if (ret_volume_key) | |
388 | *ret_volume_key = TAKE_PTR(vk); | |
389 | if (ret_volume_key_size) | |
390 | *ret_volume_key_size = vks; | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
395 | static int fs_validate( | |
396 | const char *dm_node, | |
397 | sd_id128_t uuid, | |
398 | char **ret_fstype, | |
399 | sd_id128_t *ret_found_uuid) { | |
400 | ||
401 | _cleanup_free_ char *fstype = NULL; | |
402 | sd_id128_t u; | |
403 | int r; | |
404 | ||
405 | assert(dm_node); | |
406 | assert(ret_fstype); | |
407 | ||
408 | r = probe_file_system_by_path(dm_node, &fstype, &u); | |
409 | if (r < 0) | |
410 | return log_error_errno(r, "Failed to probe file system: %m"); | |
411 | ||
412 | /* Limit the set of supported file systems a bit, as protection against little tested kernel file | |
413 | * systems. Also, we only support the resize ioctls for these file systems. */ | |
414 | if (!supported_fstype(fstype)) | |
415 | return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "Image contains unsupported file system: %s", strna(fstype)); | |
416 | ||
417 | if (!sd_id128_is_null(uuid) && | |
418 | !sd_id128_equal(uuid, u)) | |
419 | return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "File system has wrong UUID."); | |
420 | ||
421 | log_info("Probing file system completed (found %s).", fstype); | |
422 | ||
423 | *ret_fstype = TAKE_PTR(fstype); | |
424 | ||
425 | if (ret_found_uuid) /* Return the UUID actually found if the caller wants to know */ | |
426 | *ret_found_uuid = u; | |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
431 | static int make_dm_names(const char *user_name, char **ret_dm_name, char **ret_dm_node) { | |
432 | _cleanup_free_ char *name = NULL, *node = NULL; | |
433 | ||
434 | assert(user_name); | |
435 | assert(ret_dm_name); | |
436 | assert(ret_dm_node); | |
437 | ||
438 | name = strjoin("home-", user_name); | |
439 | if (!name) | |
440 | return log_oom(); | |
441 | ||
442 | node = path_join("/dev/mapper/", name); | |
443 | if (!node) | |
444 | return log_oom(); | |
445 | ||
446 | *ret_dm_name = TAKE_PTR(name); | |
447 | *ret_dm_node = TAKE_PTR(node); | |
448 | return 0; | |
449 | } | |
450 | ||
451 | static int luks_validate( | |
452 | int fd, | |
453 | const char *label, | |
454 | sd_id128_t partition_uuid, | |
455 | sd_id128_t *ret_partition_uuid, | |
456 | uint64_t *ret_offset, | |
457 | uint64_t *ret_size) { | |
458 | ||
459 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; | |
460 | sd_id128_t found_partition_uuid = SD_ID128_NULL; | |
461 | const char *fstype = NULL, *pttype = NULL; | |
462 | blkid_loff_t offset = 0, size = 0; | |
463 | blkid_partlist pl; | |
464 | bool found = false; | |
465 | int r, i, n; | |
466 | ||
467 | assert(fd >= 0); | |
468 | assert(label); | |
469 | assert(ret_offset); | |
470 | assert(ret_size); | |
471 | ||
472 | b = blkid_new_probe(); | |
473 | if (!b) | |
474 | return -ENOMEM; | |
475 | ||
476 | errno = 0; | |
477 | r = blkid_probe_set_device(b, fd, 0, 0); | |
478 | if (r != 0) | |
479 | return errno > 0 ? -errno : -ENOMEM; | |
480 | ||
481 | (void) blkid_probe_enable_superblocks(b, 1); | |
482 | (void) blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); | |
483 | (void) blkid_probe_enable_partitions(b, 1); | |
484 | (void) blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); | |
485 | ||
486 | errno = 0; | |
487 | r = blkid_do_safeprobe(b); | |
488 | if (IN_SET(r, -2, 1)) /* nothing found or ambiguous result */ | |
489 | return -ENOPKG; | |
490 | if (r != 0) | |
491 | return errno > 0 ? -errno : -EIO; | |
492 | ||
493 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
494 | if (streq_ptr(fstype, "crypto_LUKS")) { | |
495 | /* Directly a LUKS image */ | |
496 | *ret_offset = 0; | |
497 | *ret_size = UINT64_MAX; /* full disk */ | |
498 | *ret_partition_uuid = SD_ID128_NULL; | |
499 | return 0; | |
500 | } else if (fstype) | |
501 | return -ENOPKG; | |
502 | ||
503 | (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); | |
504 | if (!streq_ptr(pttype, "gpt")) | |
505 | return -ENOPKG; | |
506 | ||
507 | errno = 0; | |
508 | pl = blkid_probe_get_partitions(b); | |
509 | if (!pl) | |
510 | return errno > 0 ? -errno : -ENOMEM; | |
511 | ||
512 | errno = 0; | |
513 | n = blkid_partlist_numof_partitions(pl); | |
514 | if (n < 0) | |
515 | return errno > 0 ? -errno : -EIO; | |
516 | ||
517 | for (i = 0; i < n; i++) { | |
518 | blkid_partition pp; | |
519 | sd_id128_t id; | |
520 | const char *sid; | |
521 | ||
522 | errno = 0; | |
523 | pp = blkid_partlist_get_partition(pl, i); | |
524 | if (!pp) | |
525 | return errno > 0 ? -errno : -EIO; | |
526 | ||
527 | if (!streq_ptr(blkid_partition_get_type_string(pp), "773f91ef-66d4-49b5-bd83-d683bf40ad16")) | |
528 | continue; | |
529 | ||
530 | if (!streq_ptr(blkid_partition_get_name(pp), label)) | |
531 | continue; | |
532 | ||
533 | sid = blkid_partition_get_uuid(pp); | |
534 | if (sid) { | |
535 | r = sd_id128_from_string(sid, &id); | |
536 | if (r < 0) | |
537 | log_debug_errno(r, "Couldn't parse partition UUID %s, weird: %m", sid); | |
538 | ||
539 | if (!sd_id128_is_null(partition_uuid) && !sd_id128_equal(id, partition_uuid)) | |
540 | continue; | |
541 | } | |
542 | ||
543 | if (found) | |
544 | return -ENOPKG; | |
545 | ||
546 | offset = blkid_partition_get_start(pp); | |
547 | size = blkid_partition_get_size(pp); | |
548 | found_partition_uuid = id; | |
549 | ||
550 | found = true; | |
551 | } | |
552 | ||
553 | if (!found) | |
554 | return -ENOPKG; | |
555 | ||
556 | if (offset < 0) | |
557 | return -EINVAL; | |
558 | if ((uint64_t) offset > UINT64_MAX / 512U) | |
559 | return -EINVAL; | |
560 | if (size <= 0) | |
561 | return -EINVAL; | |
562 | if ((uint64_t) size > UINT64_MAX / 512U) | |
563 | return -EINVAL; | |
564 | ||
565 | *ret_offset = offset * 512U; | |
566 | *ret_size = size * 512U; | |
567 | *ret_partition_uuid = found_partition_uuid; | |
568 | ||
569 | return 0; | |
570 | } | |
571 | ||
572 | static int crypt_device_to_evp_cipher(struct crypt_device *cd, const EVP_CIPHER **ret) { | |
573 | _cleanup_free_ char *cipher_name = NULL; | |
574 | const char *cipher, *cipher_mode, *e; | |
575 | size_t key_size, key_bits; | |
576 | const EVP_CIPHER *cc; | |
577 | int r; | |
578 | ||
579 | assert(cd); | |
580 | ||
581 | /* Let's find the right OpenSSL EVP_CIPHER object that matches the encryption settings of the LUKS | |
582 | * device */ | |
583 | ||
584 | cipher = crypt_get_cipher(cd); | |
585 | if (!cipher) | |
586 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot get cipher from LUKS device."); | |
587 | ||
588 | cipher_mode = crypt_get_cipher_mode(cd); | |
589 | if (!cipher_mode) | |
590 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot get cipher mode from LUKS device."); | |
591 | ||
592 | e = strchr(cipher_mode, '-'); | |
593 | if (e) | |
594 | cipher_mode = strndupa(cipher_mode, e - cipher_mode); | |
595 | ||
596 | r = crypt_get_volume_key_size(cd); | |
597 | if (r <= 0) | |
598 | return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), "Cannot get volume key size from LUKS device."); | |
599 | ||
600 | key_size = r; | |
601 | key_bits = key_size * 8; | |
602 | if (streq(cipher_mode, "xts")) | |
603 | key_bits /= 2; | |
604 | ||
605 | if (asprintf(&cipher_name, "%s-%zu-%s", cipher, key_bits, cipher_mode) < 0) | |
606 | return log_oom(); | |
607 | ||
608 | cc = EVP_get_cipherbyname(cipher_name); | |
609 | if (!cc) | |
610 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Selected cipher mode '%s' not supported, can't encrypt JSON record.", cipher_name); | |
611 | ||
612 | /* Verify that our key length calculations match what OpenSSL thinks */ | |
613 | r = EVP_CIPHER_key_length(cc); | |
614 | if (r < 0 || (uint64_t) r != key_size) | |
80ace4f2 | 615 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Key size of selected cipher doesn't meet our expectations."); |
70a5db58 LP |
616 | |
617 | *ret = cc; | |
618 | return 0; | |
619 | } | |
620 | ||
621 | static int luks_validate_home_record( | |
622 | struct crypt_device *cd, | |
623 | UserRecord *h, | |
624 | const void *volume_key, | |
625 | char ***pkcs11_decrypted_passwords, | |
626 | UserRecord **ret_luks_home_record) { | |
627 | ||
628 | int r, token; | |
629 | ||
630 | assert(cd); | |
631 | assert(h); | |
632 | ||
633 | for (token = 0;; token++) { | |
634 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *rr = NULL; | |
635 | _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL; | |
636 | _cleanup_(user_record_unrefp) UserRecord *lhr = NULL; | |
637 | _cleanup_free_ void *encrypted = NULL, *iv = NULL; | |
638 | size_t decrypted_size, encrypted_size, iv_size; | |
639 | int decrypted_size_out1, decrypted_size_out2; | |
640 | _cleanup_free_ char *decrypted = NULL; | |
641 | const char *text, *type; | |
642 | crypt_token_info state; | |
643 | JsonVariant *jr, *jiv; | |
644 | unsigned line, column; | |
645 | const EVP_CIPHER *cc; | |
646 | ||
647 | state = crypt_token_status(cd, token, &type); | |
648 | if (state == CRYPT_TOKEN_INACTIVE) /* First unconfigured token, give up */ | |
649 | break; | |
650 | if (IN_SET(state, CRYPT_TOKEN_INTERNAL, CRYPT_TOKEN_INTERNAL_UNKNOWN, CRYPT_TOKEN_EXTERNAL)) | |
651 | continue; | |
652 | if (state != CRYPT_TOKEN_EXTERNAL_UNKNOWN) | |
653 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected token state of token %i: %i", token, (int) state); | |
654 | ||
655 | if (!streq(type, "systemd-homed")) | |
656 | continue; | |
657 | ||
658 | r = crypt_token_json_get(cd, token, &text); | |
659 | if (r < 0) | |
660 | return log_error_errno(r, "Failed to read LUKS token %i: %m", token); | |
661 | ||
662 | r = json_parse(text, JSON_PARSE_SENSITIVE, &v, &line, &column); | |
663 | if (r < 0) | |
664 | return log_error_errno(r, "Failed to parse LUKS token JSON data %u:%u: %m", line, column); | |
665 | ||
666 | jr = json_variant_by_key(v, "record"); | |
667 | if (!jr) | |
668 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "LUKS token lacks 'record' field."); | |
669 | jiv = json_variant_by_key(v, "iv"); | |
670 | if (!jiv) | |
671 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "LUKS token lacks 'iv' field."); | |
672 | ||
673 | r = json_variant_unbase64(jr, &encrypted, &encrypted_size); | |
674 | if (r < 0) | |
675 | return log_error_errno(r, "Failed to base64 decode record: %m"); | |
676 | ||
677 | r = json_variant_unbase64(jiv, &iv, &iv_size); | |
678 | if (r < 0) | |
679 | return log_error_errno(r, "Failed to base64 decode IV: %m"); | |
680 | ||
681 | r = crypt_device_to_evp_cipher(cd, &cc); | |
682 | if (r < 0) | |
683 | return r; | |
684 | if (iv_size > INT_MAX || EVP_CIPHER_iv_length(cc) != (int) iv_size) | |
685 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "IV size doesn't match."); | |
686 | ||
687 | context = EVP_CIPHER_CTX_new(); | |
688 | if (!context) | |
689 | return log_oom(); | |
690 | ||
691 | if (EVP_DecryptInit_ex(context, cc, NULL, volume_key, iv) != 1) | |
692 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize decryption context."); | |
693 | ||
694 | decrypted_size = encrypted_size + EVP_CIPHER_key_length(cc) * 2; | |
695 | decrypted = new(char, decrypted_size); | |
696 | if (!decrypted) | |
697 | return log_oom(); | |
698 | ||
699 | if (EVP_DecryptUpdate(context, (uint8_t*) decrypted, &decrypted_size_out1, encrypted, encrypted_size) != 1) | |
700 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to decrypt JSON record."); | |
701 | ||
702 | assert((size_t) decrypted_size_out1 <= decrypted_size); | |
703 | ||
704 | if (EVP_DecryptFinal_ex(context, (uint8_t*) decrypted + decrypted_size_out1, &decrypted_size_out2) != 1) | |
705 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finish decryption of JSON record."); | |
706 | ||
707 | assert((size_t) decrypted_size_out1 + (size_t) decrypted_size_out2 < decrypted_size); | |
708 | decrypted_size = (size_t) decrypted_size_out1 + (size_t) decrypted_size_out2; | |
709 | ||
710 | if (memchr(decrypted, 0, decrypted_size)) | |
711 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Inner NUL byte in JSON record, refusing."); | |
712 | ||
713 | decrypted[decrypted_size] = 0; | |
714 | ||
715 | r = json_parse(decrypted, JSON_PARSE_SENSITIVE, &rr, NULL, NULL); | |
716 | if (r < 0) | |
717 | return log_error_errno(r, "Failed to parse decrypted JSON record, refusing."); | |
718 | ||
719 | lhr = user_record_new(); | |
720 | if (!lhr) | |
721 | return log_oom(); | |
722 | ||
723 | r = user_record_load(lhr, rr, USER_RECORD_LOAD_EMBEDDED); | |
724 | if (r < 0) | |
725 | return log_error_errno(r, "Failed to parse user record: %m"); | |
726 | ||
727 | if (!user_record_compatible(h, lhr)) | |
728 | return log_error_errno(SYNTHETIC_ERRNO(EREMCHG), "LUKS home record not compatible with host record, refusing."); | |
729 | ||
c8f145ad | 730 | r = user_record_authenticate(lhr, h, pkcs11_decrypted_passwords, /* strict_verify= */ true); |
70a5db58 LP |
731 | if (r < 0) |
732 | return r; | |
c8f145ad | 733 | assert(r > 0); /* Insist that a password was verified */ |
70a5db58 LP |
734 | |
735 | *ret_luks_home_record = TAKE_PTR(lhr); | |
736 | return 0; | |
737 | } | |
738 | ||
739 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Couldn't find home record in LUKS2 header, refusing."); | |
740 | } | |
741 | ||
742 | static int format_luks_token_text( | |
743 | struct crypt_device *cd, | |
744 | UserRecord *hr, | |
745 | const void *volume_key, | |
746 | char **ret) { | |
747 | ||
748 | int r, encrypted_size_out1 = 0, encrypted_size_out2 = 0, iv_size, key_size; | |
749 | _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL; | |
750 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
751 | _cleanup_free_ void *iv = NULL, *encrypted = NULL; | |
752 | size_t text_length, encrypted_size; | |
753 | _cleanup_free_ char *text = NULL; | |
754 | const EVP_CIPHER *cc; | |
755 | ||
756 | assert(cd); | |
757 | assert(hr); | |
758 | assert(volume_key); | |
759 | assert(ret); | |
760 | ||
761 | r = crypt_device_to_evp_cipher(cd, &cc); | |
762 | if (r < 0) | |
763 | return r; | |
764 | ||
765 | key_size = EVP_CIPHER_key_length(cc); | |
766 | iv_size = EVP_CIPHER_iv_length(cc); | |
767 | ||
768 | if (iv_size > 0) { | |
769 | iv = malloc(iv_size); | |
770 | if (!iv) | |
771 | return log_oom(); | |
772 | ||
773 | r = genuine_random_bytes(iv, iv_size, RANDOM_BLOCK); | |
774 | if (r < 0) | |
775 | return log_error_errno(r, "Failed to generate IV: %m"); | |
776 | } | |
777 | ||
778 | context = EVP_CIPHER_CTX_new(); | |
779 | if (!context) | |
780 | return log_oom(); | |
781 | ||
782 | if (EVP_EncryptInit_ex(context, cc, NULL, volume_key, iv) != 1) | |
783 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize encryption context."); | |
784 | ||
785 | r = json_variant_format(hr->json, 0, &text); | |
786 | if (r < 0) | |
80ace4f2 | 787 | return log_error_errno(r, "Failed to format user record for LUKS: %m"); |
70a5db58 LP |
788 | |
789 | text_length = strlen(text); | |
790 | encrypted_size = text_length + 2*key_size - 1; | |
791 | ||
792 | encrypted = malloc(encrypted_size); | |
793 | if (!encrypted) | |
794 | return log_oom(); | |
795 | ||
796 | if (EVP_EncryptUpdate(context, encrypted, &encrypted_size_out1, (uint8_t*) text, text_length) != 1) | |
797 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to encrypt JSON record."); | |
798 | ||
799 | assert((size_t) encrypted_size_out1 <= encrypted_size); | |
800 | ||
801 | if (EVP_EncryptFinal_ex(context, (uint8_t*) encrypted + encrypted_size_out1, &encrypted_size_out2) != 1) | |
802 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finish encryption of JSON record. "); | |
803 | ||
804 | assert((size_t) encrypted_size_out1 + (size_t) encrypted_size_out2 <= encrypted_size); | |
805 | ||
806 | r = json_build(&v, | |
807 | JSON_BUILD_OBJECT( | |
808 | JSON_BUILD_PAIR("type", JSON_BUILD_STRING("systemd-homed")), | |
809 | JSON_BUILD_PAIR("keyslots", JSON_BUILD_EMPTY_ARRAY), | |
810 | JSON_BUILD_PAIR("record", JSON_BUILD_BASE64(encrypted, encrypted_size_out1 + encrypted_size_out2)), | |
811 | JSON_BUILD_PAIR("iv", JSON_BUILD_BASE64(iv, iv_size)))); | |
812 | if (r < 0) | |
813 | return log_error_errno(r, "Failed to prepare LUKS JSON token object: %m"); | |
814 | ||
815 | r = json_variant_format(v, 0, ret); | |
816 | if (r < 0) | |
817 | return log_error_errno(r, "Failed to format encrypted user record for LUKS: %m"); | |
818 | ||
819 | return 0; | |
820 | } | |
821 | ||
822 | int home_store_header_identity_luks( | |
823 | UserRecord *h, | |
824 | HomeSetup *setup, | |
825 | UserRecord *old_home) { | |
826 | ||
827 | _cleanup_(user_record_unrefp) UserRecord *header_home = NULL; | |
828 | _cleanup_free_ char *text = NULL; | |
829 | int token = 0, r; | |
830 | ||
831 | assert(h); | |
832 | ||
833 | if (!setup->crypt_device) | |
834 | return 0; | |
835 | ||
836 | assert(setup->volume_key); | |
837 | ||
838 | /* Let's store the user's identity record in the LUKS2 "token" header data fields, in an encrypted | |
839 | * fashion. Why that? If we'd rely on the record being embedded in the payload file system itself we | |
840 | * would have to mount the file system before we can validate the JSON record, its signatures and | |
841 | * whether it matches what we are looking for. However, kernel file system implementations are | |
842 | * generally not ready to be used on untrusted media. Hence let's store the record independently of | |
843 | * the file system, so that we can validate it first, and only then mount the file system. To keep | |
844 | * things simple we use the same encryption settings for this record as for the file system itself. */ | |
845 | ||
846 | r = user_record_clone(h, USER_RECORD_EXTRACT_EMBEDDED, &header_home); | |
847 | if (r < 0) | |
848 | return log_error_errno(r, "Failed to determine new header record: %m"); | |
849 | ||
850 | if (old_home && user_record_equal(old_home, header_home)) { | |
851 | log_debug("Not updating header home record."); | |
852 | return 0; | |
853 | } | |
854 | ||
855 | r = format_luks_token_text(setup->crypt_device, header_home, setup->volume_key, &text); | |
856 | if (r < 0) | |
857 | return r; | |
858 | ||
859 | for (;; token++) { | |
860 | crypt_token_info state; | |
861 | const char *type; | |
862 | ||
863 | state = crypt_token_status(setup->crypt_device, token, &type); | |
864 | if (state == CRYPT_TOKEN_INACTIVE) /* First unconfigured token, we are done */ | |
865 | break; | |
866 | if (IN_SET(state, CRYPT_TOKEN_INTERNAL, CRYPT_TOKEN_INTERNAL_UNKNOWN, CRYPT_TOKEN_EXTERNAL)) | |
867 | continue; /* Not ours */ | |
868 | if (state != CRYPT_TOKEN_EXTERNAL_UNKNOWN) | |
869 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected token state of token %i: %i", token, (int) state); | |
870 | ||
871 | if (!streq(type, "systemd-homed")) | |
872 | continue; | |
873 | ||
874 | r = crypt_token_json_set(setup->crypt_device, token, text); | |
875 | if (r < 0) | |
876 | return log_error_errno(r, "Failed to set JSON token for slot %i: %m", token); | |
877 | ||
878 | /* Now, let's free the text so that for all further matching tokens we all crypt_json_token_set() | |
879 | * with a NULL text in order to invalidate the tokens. */ | |
880 | text = mfree(text); | |
881 | token++; | |
882 | } | |
883 | ||
884 | if (text) | |
885 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Didn't find any record token to update."); | |
886 | ||
887 | log_info("Wrote LUKS header user record."); | |
888 | ||
889 | return 1; | |
890 | } | |
891 | ||
28a7f106 | 892 | int run_fitrim(int root_fd) { |
70a5db58 LP |
893 | char buf[FORMAT_BYTES_MAX]; |
894 | struct fstrim_range range = { | |
895 | .len = UINT64_MAX, | |
896 | }; | |
897 | ||
898 | /* If discarding is on, discard everything right after mounting, so that the discard setting takes | |
28a7f106 | 899 | * effect on activation. (Also, optionally, trim on logout) */ |
70a5db58 LP |
900 | |
901 | assert(root_fd >= 0); | |
902 | ||
903 | if (ioctl(root_fd, FITRIM, &range) < 0) { | |
28a7f106 | 904 | if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EBADF) { |
70a5db58 LP |
905 | log_debug_errno(errno, "File system does not support FITRIM, not trimming."); |
906 | return 0; | |
907 | } | |
908 | ||
909 | return log_warning_errno(errno, "Failed to invoke FITRIM, ignoring: %m"); | |
910 | } | |
911 | ||
912 | log_info("Discarded unused %s.", | |
913 | format_bytes(buf, sizeof(buf), range.len)); | |
914 | return 1; | |
915 | } | |
916 | ||
28a7f106 LP |
917 | int run_fitrim_by_path(const char *root_path) { |
918 | _cleanup_close_ int root_fd = -1; | |
919 | ||
920 | root_fd = open(root_path, O_RDONLY|O_DIRECTORY|O_CLOEXEC); | |
921 | if (root_fd < 0) | |
922 | return log_error_errno(errno, "Failed to open file system '%s' for trimming: %m", root_path); | |
923 | ||
924 | return run_fitrim(root_fd); | |
925 | } | |
926 | ||
927 | int run_fallocate(int backing_fd, const struct stat *st) { | |
70a5db58 | 928 | char buf[FORMAT_BYTES_MAX]; |
28a7f106 | 929 | struct stat stbuf; |
70a5db58 LP |
930 | |
931 | assert(backing_fd >= 0); | |
70a5db58 LP |
932 | |
933 | /* If discarding is off, let's allocate the whole image before mounting, so that the setting takes | |
934 | * effect on activation */ | |
935 | ||
28a7f106 LP |
936 | if (!st) { |
937 | if (fstat(backing_fd, &stbuf) < 0) | |
938 | return log_error_errno(errno, "Failed to fstat(): %m"); | |
939 | ||
940 | st = &stbuf; | |
941 | } | |
942 | ||
70a5db58 LP |
943 | if (!S_ISREG(st->st_mode)) |
944 | return 0; | |
945 | ||
946 | if (st->st_blocks >= DIV_ROUND_UP(st->st_size, 512)) { | |
947 | log_info("Backing file is fully allocated already."); | |
948 | return 0; | |
949 | } | |
950 | ||
951 | if (fallocate(backing_fd, FALLOC_FL_KEEP_SIZE, 0, st->st_size) < 0) { | |
952 | ||
953 | if (ERRNO_IS_NOT_SUPPORTED(errno)) { | |
954 | log_debug_errno(errno, "fallocate() not supported on file system, ignoring."); | |
955 | return 0; | |
956 | } | |
957 | ||
958 | if (ERRNO_IS_DISK_SPACE(errno)) { | |
959 | log_debug_errno(errno, "Not enough disk space to fully allocate home."); | |
960 | return -ENOSPC; /* make recognizable */ | |
961 | } | |
962 | ||
963 | return log_error_errno(errno, "Failed to allocate backing file blocks: %m"); | |
964 | } | |
965 | ||
966 | log_info("Allocated additional %s.", | |
967 | format_bytes(buf, sizeof(buf), (DIV_ROUND_UP(st->st_size, 512) - st->st_blocks) * 512)); | |
968 | return 1; | |
969 | } | |
970 | ||
28a7f106 LP |
971 | int run_fallocate_by_path(const char *backing_path) { |
972 | _cleanup_close_ int backing_fd = -1; | |
973 | ||
974 | backing_fd = open(backing_path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
975 | if (backing_fd < 0) | |
976 | return log_error_errno(errno, "Failed to open '%s' for fallocate(): %m", backing_path); | |
977 | ||
978 | return run_fallocate(backing_fd, NULL); | |
979 | } | |
980 | ||
70a5db58 LP |
981 | int home_prepare_luks( |
982 | UserRecord *h, | |
983 | bool already_activated, | |
984 | const char *force_image_path, | |
985 | char ***pkcs11_decrypted_passwords, | |
986 | HomeSetup *setup, | |
987 | UserRecord **ret_luks_home) { | |
988 | ||
989 | sd_id128_t found_partition_uuid, found_luks_uuid, found_fs_uuid; | |
990 | _cleanup_(user_record_unrefp) UserRecord *luks_home = NULL; | |
991 | _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL; | |
992 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; | |
993 | _cleanup_(erase_and_freep) void *volume_key = NULL; | |
994 | bool dm_activated = false, mounted = false; | |
995 | _cleanup_close_ int root_fd = -1; | |
996 | size_t volume_key_size = 0; | |
997 | uint64_t offset, size; | |
998 | int r; | |
999 | ||
1000 | assert(h); | |
1001 | assert(setup); | |
1002 | assert(setup->dm_name); | |
1003 | assert(setup->dm_node); | |
1004 | ||
1005 | assert(user_record_storage(h) == USER_LUKS); | |
1006 | ||
1007 | if (already_activated) { | |
1008 | struct loop_info64 info; | |
1009 | const char *n; | |
1010 | ||
1011 | r = luks_open(setup->dm_name, | |
1012 | h->password, | |
1013 | pkcs11_decrypted_passwords ? *pkcs11_decrypted_passwords : NULL, | |
1014 | &cd, | |
1015 | &found_luks_uuid, | |
1016 | &volume_key, | |
1017 | &volume_key_size); | |
1018 | if (r < 0) | |
1019 | return r; | |
1020 | ||
1021 | r = luks_validate_home_record(cd, h, volume_key, pkcs11_decrypted_passwords, &luks_home); | |
1022 | if (r < 0) | |
1023 | return r; | |
1024 | ||
1025 | n = crypt_get_device_name(cd); | |
1026 | if (!n) | |
1027 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine backing device for DM %s.", setup->dm_name); | |
1028 | ||
1029 | r = loop_device_open(n, O_RDWR, &loop); | |
1030 | if (r < 0) | |
1031 | return log_error_errno(r, "Failed to open loopback device %s: %m", n); | |
1032 | ||
1033 | if (ioctl(loop->fd, LOOP_GET_STATUS64, &info) < 0) { | |
1034 | _cleanup_free_ char *sysfs = NULL; | |
1035 | struct stat st; | |
1036 | ||
1037 | if (!IN_SET(errno, ENOTTY, EINVAL)) | |
1038 | return log_error_errno(errno, "Failed to get block device metrics of %s: %m", n); | |
1039 | ||
1040 | if (ioctl(loop->fd, BLKGETSIZE64, &size) < 0) | |
1041 | return log_error_errno(r, "Failed to read block device size of %s: %m", n); | |
1042 | ||
1043 | if (fstat(loop->fd, &st) < 0) | |
1044 | return log_error_errno(r, "Failed to stat block device %s: %m", n); | |
1045 | assert(S_ISBLK(st.st_mode)); | |
1046 | ||
1047 | if (asprintf(&sysfs, "/sys/dev/block/%u:%u/partition", major(st.st_rdev), minor(st.st_rdev)) < 0) | |
1048 | return log_oom(); | |
1049 | ||
1050 | if (access(sysfs, F_OK) < 0) { | |
1051 | if (errno != ENOENT) | |
1052 | return log_error_errno(errno, "Failed to determine whether %s exists: %m", sysfs); | |
1053 | ||
1054 | offset = 0; | |
1055 | } else { | |
1056 | _cleanup_free_ char *buffer = NULL; | |
1057 | ||
1058 | if (asprintf(&sysfs, "/sys/dev/block/%u:%u/start", major(st.st_rdev), minor(st.st_rdev)) < 0) | |
1059 | return log_oom(); | |
1060 | ||
1061 | r = read_one_line_file(sysfs, &buffer); | |
1062 | if (r < 0) | |
1063 | return log_error_errno(r, "Failed to read partition start offset: %m"); | |
1064 | ||
1065 | r = safe_atou64(buffer, &offset); | |
1066 | if (r < 0) | |
1067 | return log_error_errno(r, "Failed to parse partition start offset: %m"); | |
1068 | ||
1069 | if (offset > UINT64_MAX / 512U) | |
1070 | return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "Offset too large for 64 byte range, refusing."); | |
1071 | ||
1072 | offset *= 512U; | |
1073 | } | |
1074 | } else { | |
1075 | offset = info.lo_offset; | |
1076 | size = info.lo_sizelimit; | |
1077 | } | |
1078 | ||
1079 | found_partition_uuid = found_fs_uuid = SD_ID128_NULL; | |
1080 | ||
1081 | log_info("Discovered used loopback device %s.", loop->node); | |
1082 | ||
1083 | root_fd = open(user_record_home_directory(h), O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); | |
1084 | if (root_fd < 0) { | |
1085 | r = log_error_errno(r, "Failed to open home directory: %m"); | |
1086 | goto fail; | |
1087 | } | |
1088 | } else { | |
1089 | _cleanup_free_ char *fstype = NULL, *subdir = NULL; | |
1090 | _cleanup_close_ int fd = -1; | |
1091 | const char *ip; | |
1092 | struct stat st; | |
1093 | ||
1094 | ip = force_image_path ?: user_record_image_path(h); | |
1095 | ||
1096 | subdir = path_join("/run/systemd/user-home-mount/", user_record_user_name_and_realm(h)); | |
1097 | if (!subdir) | |
1098 | return log_oom(); | |
1099 | ||
1100 | fd = open(ip, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
1101 | if (fd < 0) | |
1102 | return log_error_errno(errno, "Failed to open image file %s: %m", ip); | |
1103 | ||
1104 | if (fstat(fd, &st) < 0) | |
1105 | return log_error_errno(errno, "Failed to fstat() image file: %m"); | |
1106 | if (!S_ISREG(st.st_mode) && !S_ISBLK(st.st_mode)) | |
1107 | return log_error_errno(errno, "Image file %s is not a regular file or block device: %m", ip); | |
1108 | ||
1109 | r = luks_validate(fd, user_record_user_name_and_realm(h), h->partition_uuid, &found_partition_uuid, &offset, &size); | |
1110 | if (r < 0) | |
1111 | return log_error_errno(r, "Failed to validate disk label: %m"); | |
1112 | ||
1113 | if (!user_record_luks_discard(h)) { | |
1114 | r = run_fallocate(fd, &st); | |
1115 | if (r < 0) | |
1116 | return r; | |
1117 | } | |
1118 | ||
1119 | r = loop_device_make(fd, O_RDWR, offset, size, 0, &loop); | |
1120 | if (r == -ENOENT) { | |
1121 | log_error_errno(r, "Loopback block device support is not available on this system."); | |
1122 | return -ENOLINK; /* make recognizable */ | |
1123 | } | |
1124 | if (r < 0) | |
1125 | return log_error_errno(r, "Failed to allocate loopback context: %m"); | |
1126 | ||
1127 | log_info("Setting up loopback device %s completed.", loop->node ?: ip); | |
1128 | ||
1129 | r = luks_setup(loop->node ?: ip, | |
1130 | setup->dm_name, | |
1131 | h->luks_uuid, | |
1132 | h->luks_cipher, | |
1133 | h->luks_cipher_mode, | |
1134 | h->luks_volume_key_size, | |
1135 | h->password, | |
1136 | pkcs11_decrypted_passwords ? *pkcs11_decrypted_passwords : NULL, | |
28a7f106 | 1137 | user_record_luks_discard(h) || user_record_luks_offline_discard(h), |
70a5db58 LP |
1138 | &cd, |
1139 | &found_luks_uuid, | |
1140 | &volume_key, | |
1141 | &volume_key_size); | |
1142 | if (r < 0) | |
1143 | return r; | |
1144 | ||
1145 | dm_activated = true; | |
1146 | ||
1147 | r = luks_validate_home_record(cd, h, volume_key, pkcs11_decrypted_passwords, &luks_home); | |
1148 | if (r < 0) | |
1149 | goto fail; | |
1150 | ||
1151 | r = fs_validate(setup->dm_node, h->file_system_uuid, &fstype, &found_fs_uuid); | |
1152 | if (r < 0) | |
1153 | goto fail; | |
1154 | ||
1155 | r = run_fsck(setup->dm_node, fstype); | |
1156 | if (r < 0) | |
1157 | goto fail; | |
1158 | ||
6a220cdb | 1159 | r = home_unshare_and_mount(setup->dm_node, fstype, user_record_luks_discard(h), user_record_mount_flags(h)); |
70a5db58 LP |
1160 | if (r < 0) |
1161 | goto fail; | |
1162 | ||
1163 | mounted = true; | |
1164 | ||
1165 | root_fd = open(subdir, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); | |
1166 | if (root_fd < 0) { | |
1167 | r = log_error_errno(r, "Failed to open home directory: %m"); | |
1168 | goto fail; | |
1169 | } | |
1170 | ||
1171 | if (user_record_luks_discard(h)) | |
1172 | (void) run_fitrim(root_fd); | |
28a7f106 LP |
1173 | |
1174 | setup->image_fd = TAKE_FD(fd); | |
1175 | setup->do_offline_fallocate = !(setup->do_offline_fitrim = user_record_luks_offline_discard(h)); | |
70a5db58 LP |
1176 | } |
1177 | ||
1178 | setup->loop = TAKE_PTR(loop); | |
1179 | setup->crypt_device = TAKE_PTR(cd); | |
1180 | setup->root_fd = TAKE_FD(root_fd); | |
1181 | setup->found_partition_uuid = found_partition_uuid; | |
1182 | setup->found_luks_uuid = found_luks_uuid; | |
1183 | setup->found_fs_uuid = found_fs_uuid; | |
1184 | setup->partition_offset = offset; | |
1185 | setup->partition_size = size; | |
1186 | setup->volume_key = TAKE_PTR(volume_key); | |
1187 | setup->volume_key_size = volume_key_size; | |
1188 | ||
1189 | setup->undo_mount = mounted; | |
1190 | setup->undo_dm = dm_activated; | |
1191 | ||
1192 | if (ret_luks_home) | |
1193 | *ret_luks_home = TAKE_PTR(luks_home); | |
1194 | ||
1195 | return 0; | |
1196 | ||
1197 | fail: | |
1198 | if (mounted) | |
1199 | (void) umount_verbose("/run/systemd/user-home-mount"); | |
1200 | ||
1201 | if (dm_activated) | |
1202 | (void) crypt_deactivate(cd, setup->dm_name); | |
1203 | ||
1204 | return r; | |
1205 | } | |
1206 | ||
1207 | static void print_size_summary(uint64_t host_size, uint64_t encrypted_size, struct statfs *sfs) { | |
1208 | char buffer1[FORMAT_BYTES_MAX], buffer2[FORMAT_BYTES_MAX], buffer3[FORMAT_BYTES_MAX], buffer4[FORMAT_BYTES_MAX]; | |
1209 | ||
1210 | assert(sfs); | |
1211 | ||
1212 | log_info("Image size is %s, file system size is %s, file system payload size is %s, file system free is %s.", | |
1213 | format_bytes(buffer1, sizeof(buffer1), host_size), | |
1214 | format_bytes(buffer2, sizeof(buffer2), encrypted_size), | |
1215 | format_bytes(buffer3, sizeof(buffer3), (uint64_t) sfs->f_blocks * (uint64_t) sfs->f_frsize), | |
1216 | format_bytes(buffer4, sizeof(buffer4), (uint64_t) sfs->f_bfree * (uint64_t) sfs->f_frsize)); | |
1217 | } | |
1218 | ||
1219 | int home_activate_luks( | |
1220 | UserRecord *h, | |
1221 | char ***pkcs11_decrypted_passwords, | |
1222 | UserRecord **ret_home) { | |
1223 | ||
1224 | _cleanup_(user_record_unrefp) UserRecord *new_home = NULL, *luks_home_record = NULL; | |
1225 | _cleanup_(home_setup_undo) HomeSetup setup = HOME_SETUP_INIT; | |
1226 | uint64_t host_size, encrypted_size; | |
1227 | const char *hdo, *hd; | |
1228 | struct statfs sfs; | |
1229 | int r; | |
1230 | ||
1231 | assert(h); | |
1232 | assert(user_record_storage(h) == USER_LUKS); | |
1233 | assert(ret_home); | |
1234 | ||
1235 | assert_se(hdo = user_record_home_directory(h)); | |
1236 | hd = strdupa(hdo); /* copy the string out, since it might change later in the home record object */ | |
1237 | ||
1238 | r = make_dm_names(h->user_name, &setup.dm_name, &setup.dm_node); | |
1239 | if (r < 0) | |
1240 | return r; | |
1241 | ||
1242 | r = access(setup.dm_node, F_OK); | |
1243 | if (r < 0) { | |
1244 | if (errno != ENOENT) | |
1245 | return log_error_errno(errno, "Failed to determine whether %s exists: %m", setup.dm_node); | |
1246 | } else | |
1247 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Device mapper device %s already exists, refusing.", setup.dm_node); | |
1248 | ||
1249 | r = home_prepare_luks( | |
1250 | h, | |
1251 | false, | |
1252 | NULL, | |
1253 | pkcs11_decrypted_passwords, | |
1254 | &setup, | |
1255 | &luks_home_record); | |
1256 | if (r < 0) | |
1257 | return r; | |
1258 | ||
1259 | r = block_get_size_by_fd(setup.loop->fd, &host_size); | |
1260 | if (r < 0) | |
1261 | return log_error_errno(r, "Failed to get loopback block device size: %m"); | |
1262 | ||
1263 | r = block_get_size_by_path(setup.dm_node, &encrypted_size); | |
1264 | if (r < 0) | |
1265 | return log_error_errno(r, "Failed to get LUKS block device size: %m"); | |
1266 | ||
1267 | r = home_refresh( | |
1268 | h, | |
1269 | &setup, | |
1270 | luks_home_record, | |
1271 | pkcs11_decrypted_passwords, | |
1272 | &sfs, | |
1273 | &new_home); | |
1274 | if (r < 0) | |
1275 | return r; | |
1276 | ||
1277 | r = home_extend_embedded_identity(new_home, h, &setup); | |
1278 | if (r < 0) | |
1279 | return r; | |
1280 | ||
1281 | setup.root_fd = safe_close(setup.root_fd); | |
1282 | ||
1283 | r = home_move_mount(user_record_user_name_and_realm(h), hd); | |
1284 | if (r < 0) | |
1285 | return r; | |
1286 | ||
1287 | setup.undo_mount = false; | |
28a7f106 | 1288 | setup.do_offline_fitrim = false; |
70a5db58 LP |
1289 | |
1290 | loop_device_relinquish(setup.loop); | |
1291 | ||
1292 | r = dm_deferred_remove(setup.dm_name); | |
1293 | if (r < 0) | |
80ace4f2 | 1294 | log_warning_errno(r, "Failed to relinquish DM device, ignoring: %m"); |
70a5db58 LP |
1295 | |
1296 | setup.undo_dm = false; | |
28a7f106 | 1297 | setup.do_offline_fallocate = false; |
70a5db58 LP |
1298 | |
1299 | log_info("Everything completed."); | |
1300 | ||
1301 | print_size_summary(host_size, encrypted_size, &sfs); | |
1302 | ||
1303 | *ret_home = TAKE_PTR(new_home); | |
1304 | return 1; | |
1305 | } | |
1306 | ||
1307 | int home_deactivate_luks(UserRecord *h) { | |
1308 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; | |
1309 | _cleanup_free_ char *dm_name = NULL, *dm_node = NULL; | |
28a7f106 | 1310 | bool we_detached; |
70a5db58 LP |
1311 | int r; |
1312 | ||
1313 | /* Note that the DM device and loopback device are set to auto-detach, hence strictly speaking we | |
1314 | * don't have to explicitly have to detach them. However, we do that nonetheless (in case of the DM | |
1315 | * device), to avoid races: by explicitly detaching them we know when the detaching is complete. We | |
1316 | * don't bother about the loopback device because unlike the DM device it doesn't have a fixed | |
1317 | * name. */ | |
1318 | ||
1319 | r = make_dm_names(h->user_name, &dm_name, &dm_node); | |
1320 | if (r < 0) | |
1321 | return r; | |
1322 | ||
1323 | r = crypt_init_by_name(&cd, dm_name); | |
1324 | if (IN_SET(r, -ENODEV, -EINVAL, -ENOENT)) { | |
28a7f106 LP |
1325 | log_debug_errno(r, "LUKS device %s has already been detached.", dm_name); |
1326 | we_detached = false; | |
70a5db58 LP |
1327 | } else if (r < 0) |
1328 | return log_error_errno(r, "Failed to initialize cryptsetup context for %s: %m", dm_name); | |
28a7f106 LP |
1329 | else { |
1330 | log_info("Discovered used LUKS device %s.", dm_node); | |
1331 | ||
1332 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); | |
1333 | ||
1334 | r = crypt_deactivate(cd, dm_name); | |
1335 | if (IN_SET(r, -ENODEV, -EINVAL, -ENOENT)) { | |
1336 | log_debug_errno(r, "LUKS device %s is already detached.", dm_node); | |
1337 | we_detached = false; | |
1338 | } else if (r < 0) | |
1339 | return log_info_errno(r, "LUKS device %s couldn't be deactivated: %m", dm_node); | |
1340 | else { | |
1341 | log_info("LUKS device detaching completed."); | |
1342 | we_detached = true; | |
1343 | } | |
1344 | } | |
70a5db58 | 1345 | |
28a7f106 LP |
1346 | if (user_record_luks_offline_discard(h)) |
1347 | log_debug("Not allocating on logout."); | |
1348 | else | |
1349 | (void) run_fallocate_by_path(user_record_image_path(h)); | |
70a5db58 | 1350 | |
28a7f106 LP |
1351 | return we_detached; |
1352 | } | |
70a5db58 | 1353 | |
28a7f106 LP |
1354 | int home_trim_luks(UserRecord *h) { |
1355 | assert(h); | |
1356 | ||
1357 | if (!user_record_luks_offline_discard(h)) { | |
1358 | log_debug("Not trimming on logout."); | |
1359 | return 0; | |
1360 | } | |
70a5db58 | 1361 | |
28a7f106 LP |
1362 | (void) run_fitrim_by_path(user_record_home_directory(h)); |
1363 | return 0; | |
70a5db58 LP |
1364 | } |
1365 | ||
1366 | static int run_mkfs( | |
1367 | const char *node, | |
1368 | const char *fstype, | |
1369 | const char *label, | |
1370 | sd_id128_t uuid, | |
1371 | bool discard) { | |
1372 | ||
1373 | int r; | |
1374 | ||
1375 | assert(node); | |
1376 | assert(fstype); | |
1377 | assert(label); | |
1378 | ||
1379 | r = mkfs_exists(fstype); | |
1380 | if (r < 0) | |
1381 | return log_error_errno(r, "Failed to check if mkfs for file system %s exists: %m", fstype); | |
1382 | if (r == 0) | |
80ace4f2 | 1383 | return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "No mkfs for file system %s installed.", fstype); |
70a5db58 LP |
1384 | |
1385 | r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR, NULL); | |
1386 | if (r < 0) | |
1387 | return r; | |
1388 | if (r == 0) { | |
1389 | const char *mkfs; | |
1390 | char suuid[37]; | |
1391 | ||
1392 | /* Child */ | |
1393 | ||
1394 | mkfs = strjoina("mkfs.", fstype); | |
1395 | id128_to_uuid_string(uuid, suuid); | |
1396 | ||
1397 | if (streq(fstype, "ext4")) | |
1398 | execlp(mkfs, mkfs, | |
1399 | "-L", label, | |
1400 | "-U", suuid, | |
1401 | "-I", "256", | |
1402 | "-O", "has_journal", | |
1403 | "-m", "0", | |
1404 | "-E", discard ? "lazy_itable_init=1,discard" : "lazy_itable_init=1,nodiscard", | |
1405 | node, NULL); | |
1406 | else if (streq(fstype, "btrfs")) { | |
1407 | if (discard) | |
1408 | execlp(mkfs, mkfs, "-L", label, "-U", suuid, node, NULL); | |
1409 | else | |
1410 | execlp(mkfs, mkfs, "-L", label, "-U", suuid, "--nodiscard", node, NULL); | |
1411 | } else if (streq(fstype, "xfs")) { | |
1412 | const char *j; | |
1413 | ||
1414 | j = strjoina("uuid=", suuid); | |
1415 | if (discard) | |
1416 | execlp(mkfs, mkfs, "-L", label, "-m", j, "-m", "reflink=1", node, NULL); | |
1417 | else | |
1418 | execlp(mkfs, mkfs, "-L", label, "-m", j, "-m", "reflink=1", "-K", node, NULL); | |
1419 | } else { | |
1420 | log_error("Cannot make file system: %s", fstype); | |
1421 | _exit(EXIT_FAILURE); | |
1422 | } | |
1423 | ||
1424 | log_error_errno(errno, "Failed to execute %s: %m", mkfs); | |
1425 | _exit(EXIT_FAILURE); | |
1426 | } | |
1427 | ||
1428 | return 0; | |
1429 | } | |
1430 | ||
1431 | static struct crypt_pbkdf_type* build_good_pbkdf(struct crypt_pbkdf_type *buffer, UserRecord *hr) { | |
1432 | assert(buffer); | |
1433 | assert(hr); | |
1434 | ||
1435 | *buffer = (struct crypt_pbkdf_type) { | |
1436 | .hash = user_record_luks_pbkdf_hash_algorithm(hr), | |
1437 | .type = user_record_luks_pbkdf_type(hr), | |
1438 | .time_ms = user_record_luks_pbkdf_time_cost_usec(hr) / USEC_PER_MSEC, | |
1439 | .max_memory_kb = user_record_luks_pbkdf_memory_cost(hr) / 1024, | |
1440 | .parallel_threads = user_record_luks_pbkdf_parallel_threads(hr), | |
1441 | }; | |
1442 | ||
1443 | return buffer; | |
1444 | } | |
1445 | ||
1446 | static struct crypt_pbkdf_type* build_minimal_pbkdf(struct crypt_pbkdf_type *buffer, UserRecord *hr) { | |
1447 | assert(buffer); | |
1448 | assert(hr); | |
1449 | ||
1450 | /* For PKCS#11 derived keys (which are generated randomly and are of high quality already) we use a | |
1451 | * minimal PBKDF */ | |
1452 | *buffer = (struct crypt_pbkdf_type) { | |
1453 | .hash = user_record_luks_pbkdf_hash_algorithm(hr), | |
1454 | .type = CRYPT_KDF_PBKDF2, | |
1455 | .iterations = 1, | |
1456 | .time_ms = 1, | |
1457 | }; | |
1458 | ||
1459 | return buffer; | |
1460 | } | |
1461 | ||
1462 | static int luks_format( | |
1463 | const char *node, | |
1464 | const char *dm_name, | |
1465 | sd_id128_t uuid, | |
1466 | const char *label, | |
1467 | char **pkcs11_decrypted_passwords, | |
1468 | char **effective_passwords, | |
1469 | bool discard, | |
1470 | UserRecord *hr, | |
1471 | struct crypt_device **ret) { | |
1472 | ||
1473 | _cleanup_(user_record_unrefp) UserRecord *reduced = NULL; | |
1474 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; | |
1475 | _cleanup_(erase_and_freep) void *volume_key = NULL; | |
1476 | struct crypt_pbkdf_type good_pbkdf, minimal_pbkdf; | |
1477 | _cleanup_free_ char *text = NULL; | |
1478 | size_t volume_key_size; | |
1479 | char suuid[37], **pp; | |
1480 | int slot = 0, r; | |
1481 | ||
1482 | assert(node); | |
1483 | assert(dm_name); | |
1484 | assert(hr); | |
1485 | assert(ret); | |
1486 | ||
1487 | r = crypt_init(&cd, node); | |
1488 | if (r < 0) | |
1489 | return log_error_errno(r, "Failed to allocate libcryptsetup context: %m"); | |
1490 | ||
1491 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); | |
1492 | ||
1493 | /* Normally we'd, just leave volume key generation to libcryptsetup. However, we can't, since we | |
1494 | * can't extract the volume key from the library again, but we need it in order to encrypt the JSON | |
1495 | * record. Hence, let's generate it on our own, so that we can keep track of it. */ | |
1496 | ||
1497 | volume_key_size = user_record_luks_volume_key_size(hr); | |
1498 | volume_key = malloc(volume_key_size); | |
1499 | if (!volume_key) | |
1500 | return log_oom(); | |
1501 | ||
1502 | r = genuine_random_bytes(volume_key, volume_key_size, RANDOM_BLOCK); | |
1503 | if (r < 0) | |
1504 | return log_error_errno(r, "Failed to generate volume key: %m"); | |
1505 | ||
1506 | #if HAVE_CRYPT_SET_METADATA_SIZE | |
1507 | /* Increase the metadata space to 4M, the largest LUKS2 supports */ | |
1508 | r = crypt_set_metadata_size(cd, 4096U*1024U, 0); | |
1509 | if (r < 0) | |
1510 | return log_error_errno(r, "Failed to change LUKS2 metadata size: %m"); | |
1511 | #endif | |
1512 | ||
1513 | build_good_pbkdf(&good_pbkdf, hr); | |
1514 | build_minimal_pbkdf(&minimal_pbkdf, hr); | |
1515 | ||
1516 | r = crypt_format(cd, | |
1517 | CRYPT_LUKS2, | |
1518 | user_record_luks_cipher(hr), | |
1519 | user_record_luks_cipher_mode(hr), | |
1520 | id128_to_uuid_string(uuid, suuid), | |
1521 | volume_key, | |
1522 | volume_key_size, | |
1523 | &(struct crypt_params_luks2) { | |
1524 | .label = label, | |
1525 | .subsystem = "systemd-home", | |
1526 | .sector_size = 512U, | |
1527 | .pbkdf = &good_pbkdf, | |
1528 | }); | |
1529 | if (r < 0) | |
1530 | return log_error_errno(r, "Failed to format LUKS image: %m"); | |
1531 | ||
1532 | log_info("LUKS formatting completed."); | |
1533 | ||
1534 | STRV_FOREACH(pp, effective_passwords) { | |
1535 | ||
1536 | if (strv_contains(pkcs11_decrypted_passwords, *pp)) { | |
1537 | log_debug("Using minimal PBKDF for slot %i", slot); | |
1538 | r = crypt_set_pbkdf_type(cd, &minimal_pbkdf); | |
1539 | } else { | |
1540 | log_debug("Using good PBKDF for slot %i", slot); | |
1541 | r = crypt_set_pbkdf_type(cd, &good_pbkdf); | |
1542 | } | |
1543 | if (r < 0) | |
1544 | return log_error_errno(r, "Failed to tweak PBKDF for slot %i: %m", slot); | |
1545 | ||
1546 | r = crypt_keyslot_add_by_volume_key( | |
1547 | cd, | |
1548 | slot, | |
1549 | volume_key, | |
1550 | volume_key_size, | |
1551 | *pp, | |
1552 | strlen(*pp)); | |
1553 | if (r < 0) | |
1554 | return log_error_errno(r, "Failed to set up LUKS password for slot %i: %m", slot); | |
1555 | ||
1556 | log_info("Writing password to LUKS keyslot %i completed.", slot); | |
1557 | slot++; | |
1558 | } | |
1559 | ||
1560 | r = crypt_activate_by_volume_key( | |
1561 | cd, | |
1562 | dm_name, | |
1563 | volume_key, | |
1564 | volume_key_size, | |
1565 | discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0); | |
1566 | if (r < 0) | |
1567 | return log_error_errno(r, "Failed to activate LUKS superblock: %m"); | |
1568 | ||
1569 | log_info("LUKS activation by volume key succeeded."); | |
1570 | ||
1571 | r = user_record_clone(hr, USER_RECORD_EXTRACT_EMBEDDED, &reduced); | |
1572 | if (r < 0) | |
1573 | return log_error_errno(r, "Failed to prepare home record for LUKS: %m"); | |
1574 | ||
1575 | r = format_luks_token_text(cd, reduced, volume_key, &text); | |
1576 | if (r < 0) | |
1577 | return r; | |
1578 | ||
1579 | r = crypt_token_json_set(cd, CRYPT_ANY_TOKEN, text); | |
1580 | if (r < 0) | |
1581 | return log_error_errno(r, "Failed to set LUKS JSON token: %m"); | |
1582 | ||
1583 | log_info("Writing user record as LUKS token completed."); | |
1584 | ||
1585 | if (ret) | |
1586 | *ret = TAKE_PTR(cd); | |
1587 | ||
1588 | return 0; | |
1589 | } | |
1590 | ||
1591 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_context*, fdisk_unref_context); | |
1592 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_partition*, fdisk_unref_partition); | |
1593 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_parttype*, fdisk_unref_parttype); | |
1594 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_table*, fdisk_unref_table); | |
1595 | ||
1596 | static int make_partition_table( | |
1597 | int fd, | |
1598 | const char *label, | |
1599 | sd_id128_t uuid, | |
1600 | uint64_t *ret_offset, | |
1601 | uint64_t *ret_size, | |
1602 | sd_id128_t *ret_disk_uuid) { | |
1603 | ||
1604 | _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *p = NULL, *q = NULL; | |
1605 | _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL; | |
1606 | _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; | |
1607 | _cleanup_free_ char *path = NULL, *disk_uuid_as_string = NULL; | |
1608 | uint64_t offset, size; | |
1609 | sd_id128_t disk_uuid; | |
1610 | char uuids[37]; | |
1611 | int r; | |
1612 | ||
1613 | assert(fd >= 0); | |
1614 | assert(label); | |
1615 | assert(ret_offset); | |
1616 | assert(ret_size); | |
1617 | ||
1618 | t = fdisk_new_parttype(); | |
1619 | if (!t) | |
1620 | return log_oom(); | |
1621 | ||
1622 | r = fdisk_parttype_set_typestr(t, "773f91ef-66d4-49b5-bd83-d683bf40ad16"); | |
1623 | if (r < 0) | |
1624 | return log_error_errno(r, "Failed to initialize partition type: %m"); | |
1625 | ||
1626 | c = fdisk_new_context(); | |
1627 | if (!c) | |
1628 | return log_oom(); | |
1629 | ||
1630 | if (asprintf(&path, "/proc/self/fd/%i", fd) < 0) | |
1631 | return log_oom(); | |
1632 | ||
1633 | r = fdisk_assign_device(c, path, 0); | |
1634 | if (r < 0) | |
1635 | return log_error_errno(r, "Failed to open device: %m"); | |
1636 | ||
1637 | r = fdisk_create_disklabel(c, "gpt"); | |
1638 | if (r < 0) | |
80ace4f2 | 1639 | return log_error_errno(r, "Failed to create GPT disk label: %m"); |
70a5db58 LP |
1640 | |
1641 | p = fdisk_new_partition(); | |
1642 | if (!p) | |
1643 | return log_oom(); | |
1644 | ||
1645 | r = fdisk_partition_set_type(p, t); | |
1646 | if (r < 0) | |
1647 | return log_error_errno(r, "Failed to set partition type: %m"); | |
1648 | ||
1649 | r = fdisk_partition_start_follow_default(p, 1); | |
1650 | if (r < 0) | |
1651 | return log_error_errno(r, "Failed to place partition at beginning of space: %m"); | |
1652 | ||
1653 | r = fdisk_partition_partno_follow_default(p, 1); | |
1654 | if (r < 0) | |
1655 | return log_error_errno(r, "Failed to place partition at first free partition index: %m"); | |
1656 | ||
1657 | r = fdisk_partition_end_follow_default(p, 1); | |
1658 | if (r < 0) | |
1659 | return log_error_errno(r, "Failed to make partition cover all free space: %m"); | |
1660 | ||
1661 | r = fdisk_partition_set_name(p, label); | |
1662 | if (r < 0) | |
1663 | return log_error_errno(r, "Failed to set partition name: %m"); | |
1664 | ||
1665 | r = fdisk_partition_set_uuid(p, id128_to_uuid_string(uuid, uuids)); | |
1666 | if (r < 0) | |
1667 | return log_error_errno(r, "Failed to set partition UUID: %m"); | |
1668 | ||
1669 | r = fdisk_add_partition(c, p, NULL); | |
1670 | if (r < 0) | |
1671 | return log_error_errno(r, "Failed to add partition: %m"); | |
1672 | ||
1673 | r = fdisk_write_disklabel(c); | |
1674 | if (r < 0) | |
1675 | return log_error_errno(r, "Failed to write disk label: %m"); | |
1676 | ||
1677 | r = fdisk_get_disklabel_id(c, &disk_uuid_as_string); | |
1678 | if (r < 0) | |
1679 | return log_error_errno(r, "Failed to determine disk label UUID: %m"); | |
1680 | ||
1681 | r = sd_id128_from_string(disk_uuid_as_string, &disk_uuid); | |
1682 | if (r < 0) | |
1683 | return log_error_errno(r, "Failed to parse disk label UUID: %m"); | |
1684 | ||
1685 | r = fdisk_get_partition(c, 0, &q); | |
1686 | if (r < 0) | |
1687 | return log_error_errno(r, "Failed to read created partition metadata: %m"); | |
1688 | ||
1689 | assert(fdisk_partition_has_start(q)); | |
1690 | offset = fdisk_partition_get_start(q); | |
1691 | if (offset > UINT64_MAX / 512U) | |
1692 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Partition offset too large."); | |
1693 | ||
1694 | assert(fdisk_partition_has_size(q)); | |
1695 | size = fdisk_partition_get_size(q); | |
1696 | if (size > UINT64_MAX / 512U) | |
1697 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Partition size too large."); | |
1698 | ||
1699 | *ret_offset = offset * 512U; | |
1700 | *ret_size = size * 512U; | |
1701 | *ret_disk_uuid = disk_uuid; | |
1702 | ||
1703 | return 0; | |
1704 | } | |
1705 | ||
1706 | static bool supported_fs_size(const char *fstype, uint64_t host_size) { | |
1707 | uint64_t m; | |
1708 | ||
1709 | m = minimal_size_by_fs_name(fstype); | |
1710 | if (m == UINT64_MAX) | |
1711 | return false; | |
1712 | ||
1713 | return host_size >= m; | |
1714 | } | |
1715 | ||
1716 | static int wait_for_devlink(const char *path) { | |
1717 | _cleanup_close_ int inotify_fd = -1; | |
1718 | usec_t until; | |
1719 | int r; | |
1720 | ||
201632e3 | 1721 | /* let's wait for a device link to show up in /dev, with a timeout. This is good to do since we |
70a5db58 LP |
1722 | * return a /dev/disk/by-uuid/… link to our callers and they likely want to access it right-away, |
1723 | * hence let's wait until udev has caught up with our changes, and wait for the symlink to be | |
1724 | * created. */ | |
1725 | ||
1726 | until = usec_add(now(CLOCK_MONOTONIC), 45 * USEC_PER_SEC); | |
1727 | ||
1728 | for (;;) { | |
1729 | _cleanup_free_ char *dn = NULL; | |
1730 | usec_t w; | |
1731 | ||
1732 | if (laccess(path, F_OK) < 0) { | |
1733 | if (errno != ENOENT) | |
1734 | return log_error_errno(errno, "Failed to determine whether %s exists: %m", path); | |
1735 | } else | |
1736 | return 0; /* Found it */ | |
1737 | ||
1738 | if (inotify_fd < 0) { | |
1739 | /* We need to wait for the device symlink to show up, let's create an inotify watch for it */ | |
1740 | inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
1741 | if (inotify_fd < 0) | |
1742 | return log_error_errno(errno, "Failed to allocate inotify fd: %m"); | |
1743 | } | |
1744 | ||
1745 | dn = dirname_malloc(path); | |
1746 | for (;;) { | |
1747 | if (!dn) | |
1748 | return log_oom(); | |
1749 | ||
1750 | log_info("Watching %s", dn); | |
1751 | ||
1752 | if (inotify_add_watch(inotify_fd, dn, IN_CREATE|IN_MOVED_TO|IN_ONLYDIR|IN_DELETE_SELF|IN_MOVE_SELF) < 0) { | |
1753 | if (errno != ENOENT) | |
1754 | return log_error_errno(errno, "Failed to add watch on %s: %m", dn); | |
1755 | } else | |
1756 | break; | |
1757 | ||
1758 | if (empty_or_root(dn)) | |
1759 | break; | |
1760 | ||
1761 | dn = dirname_malloc(dn); | |
1762 | } | |
1763 | ||
1764 | w = now(CLOCK_MONOTONIC); | |
1765 | if (w >= until) | |
1766 | return log_error_errno(SYNTHETIC_ERRNO(ETIMEDOUT), "Device link %s still hasn't shown up, giving up.", path); | |
1767 | ||
1768 | r = fd_wait_for_event(inotify_fd, POLLIN, usec_sub_unsigned(until, w)); | |
1769 | if (r < 0) | |
1770 | return log_error_errno(r, "Failed to watch inotify: %m"); | |
1771 | ||
1772 | (void) flush_fd(inotify_fd); | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | static int calculate_disk_size(UserRecord *h, const char *parent_dir, uint64_t *ret) { | |
1777 | char buf[FORMAT_BYTES_MAX]; | |
1778 | struct statfs sfs; | |
1779 | uint64_t m; | |
1780 | ||
1781 | assert(h); | |
1782 | assert(parent_dir); | |
1783 | assert(ret); | |
1784 | ||
1785 | if (h->disk_size != UINT64_MAX) { | |
1786 | *ret = DISK_SIZE_ROUND_DOWN(h->disk_size); | |
1787 | return 0; | |
1788 | } | |
1789 | ||
1790 | if (statfs(parent_dir, &sfs) < 0) | |
1791 | return log_error_errno(errno, "statfs() on %s failed: %m", parent_dir); | |
1792 | ||
1793 | m = sfs.f_bsize * sfs.f_bavail; | |
1794 | ||
1795 | if (h->disk_size_relative == UINT64_MAX) { | |
1796 | ||
1797 | if (m > UINT64_MAX / USER_DISK_SIZE_DEFAULT_PERCENT) | |
1798 | return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Disk size too large."); | |
1799 | ||
1800 | *ret = DISK_SIZE_ROUND_DOWN(m * USER_DISK_SIZE_DEFAULT_PERCENT / 100); | |
1801 | ||
1802 | log_info("Sizing home to %u%% of available disk space, which is %s.", | |
1803 | USER_DISK_SIZE_DEFAULT_PERCENT, | |
1804 | format_bytes(buf, sizeof(buf), *ret)); | |
1805 | } else { | |
1806 | *ret = DISK_SIZE_ROUND_DOWN((uint64_t) ((double) m * (double) h->disk_size_relative / (double) UINT32_MAX)); | |
1807 | ||
1808 | log_info("Sizing home to %" PRIu64 ".%01" PRIu64 "%% of available disk space, which is %s.", | |
1809 | (h->disk_size_relative * 100) / UINT32_MAX, | |
1810 | ((h->disk_size_relative * 1000) / UINT32_MAX) % 10, | |
1811 | format_bytes(buf, sizeof(buf), *ret)); | |
1812 | } | |
1813 | ||
1814 | if (*ret < USER_DISK_SIZE_MIN) | |
1815 | *ret = USER_DISK_SIZE_MIN; | |
1816 | ||
1817 | return 0; | |
1818 | } | |
1819 | ||
e46f877c LP |
1820 | static int home_truncate( |
1821 | UserRecord *h, | |
1822 | int fd, | |
1823 | const char *path, | |
1824 | uint64_t size) { | |
1825 | ||
1826 | bool trunc; | |
1827 | int r; | |
1828 | ||
1829 | assert(h); | |
1830 | assert(fd >= 0); | |
1831 | assert(path); | |
1832 | ||
1833 | trunc = user_record_luks_discard(h); | |
1834 | if (!trunc) { | |
1835 | r = fallocate(fd, 0, 0, size); | |
1836 | if (r < 0 && ERRNO_IS_NOT_SUPPORTED(errno)) { | |
1837 | /* Some file systems do not support fallocate(), let's gracefully degrade | |
1838 | * (ZFS, reiserfs, …) and fall back to truncation */ | |
1839 | log_notice_errno(errno, "Backing file system does not support fallocate(), falling back to ftruncate(), i.e. implicitly using non-discard mode."); | |
1840 | trunc = true; | |
1841 | } | |
1842 | } | |
1843 | ||
1844 | if (trunc) | |
1845 | r = ftruncate(fd, size); | |
1846 | ||
1847 | if (r < 0) { | |
1848 | if (ERRNO_IS_DISK_SPACE(errno)) { | |
1849 | log_error_errno(errno, "Not enough disk space to allocate home."); | |
1850 | return -ENOSPC; /* make recognizable */ | |
1851 | } | |
1852 | ||
1853 | return log_error_errno(errno, "Failed to truncate home image %s: %m", path); | |
1854 | } | |
1855 | ||
1856 | return 0; | |
1857 | } | |
1858 | ||
70a5db58 LP |
1859 | int home_create_luks( |
1860 | UserRecord *h, | |
1861 | char **pkcs11_decrypted_passwords, | |
1862 | char **effective_passwords, | |
1863 | UserRecord **ret_home) { | |
1864 | ||
1865 | _cleanup_free_ char *dm_name = NULL, *dm_node = NULL, *subdir = NULL, *disk_uuid_path = NULL, *temporary_image_path = NULL; | |
1866 | uint64_t host_size, encrypted_size, partition_offset, partition_size; | |
1867 | bool image_created = false, dm_activated = false, mounted = false; | |
1868 | _cleanup_(user_record_unrefp) UserRecord *new_home = NULL; | |
1869 | sd_id128_t partition_uuid, fs_uuid, luks_uuid, disk_uuid; | |
1870 | _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL; | |
1871 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; | |
1872 | _cleanup_close_ int image_fd = -1, root_fd = -1; | |
1873 | const char *fstype, *ip; | |
1874 | struct statfs sfs; | |
1875 | int r; | |
1876 | ||
1877 | assert(h); | |
1878 | assert(h->storage < 0 || h->storage == USER_LUKS); | |
1879 | assert(ret_home); | |
1880 | ||
1881 | assert_se(ip = user_record_image_path(h)); | |
1882 | ||
1883 | fstype = user_record_file_system_type(h); | |
1884 | if (!supported_fstype(fstype)) | |
1885 | return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "Unsupported file system type: %s", h->file_system_type); | |
1886 | ||
1887 | if (sd_id128_is_null(h->partition_uuid)) { | |
1888 | r = sd_id128_randomize(&partition_uuid); | |
1889 | if (r < 0) | |
1890 | return log_error_errno(r, "Failed to acquire partition UUID: %m"); | |
1891 | } else | |
1892 | partition_uuid = h->partition_uuid; | |
1893 | ||
1894 | if (sd_id128_is_null(h->luks_uuid)) { | |
1895 | r = sd_id128_randomize(&luks_uuid); | |
1896 | if (r < 0) | |
1897 | return log_error_errno(r, "Failed to acquire LUKS UUID: %m"); | |
1898 | } else | |
1899 | luks_uuid = h->luks_uuid; | |
1900 | ||
1901 | if (sd_id128_is_null(h->file_system_uuid)) { | |
1902 | r = sd_id128_randomize(&fs_uuid); | |
1903 | if (r < 0) | |
1904 | return log_error_errno(r, "Failed to acquire file system UUID: %m"); | |
1905 | } else | |
1906 | fs_uuid = h->file_system_uuid; | |
1907 | ||
1908 | r = make_dm_names(h->user_name, &dm_name, &dm_node); | |
1909 | if (r < 0) | |
1910 | return r; | |
1911 | ||
1912 | r = access(dm_node, F_OK); | |
1913 | if (r < 0) { | |
1914 | if (errno != ENOENT) | |
1915 | return log_error_errno(errno, "Failed to determine whether %s exists: %m", dm_node); | |
1916 | } else | |
1917 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Device mapper device %s already exists, refusing.", dm_node); | |
1918 | ||
1919 | if (path_startswith(ip, "/dev/")) { | |
1920 | _cleanup_free_ char *sysfs = NULL; | |
1921 | uint64_t block_device_size; | |
1922 | struct stat st; | |
1923 | ||
1924 | /* Let's place the home directory on a real device, i.e. an USB stick or such */ | |
1925 | ||
1926 | image_fd = open(ip, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
1927 | if (image_fd < 0) | |
1928 | return log_error_errno(errno, "Failed to open device %s: %m", ip); | |
1929 | ||
1930 | if (fstat(image_fd, &st) < 0) | |
1931 | return log_error_errno(errno, "Failed to stat device %s: %m", ip); | |
1932 | if (!S_ISBLK(st.st_mode)) | |
1933 | return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Device is not a block device, refusing."); | |
1934 | ||
1935 | if (asprintf(&sysfs, "/sys/dev/block/%u:%u/partition", major(st.st_rdev), minor(st.st_rdev)) < 0) | |
1936 | return log_oom(); | |
1937 | if (access(sysfs, F_OK) < 0) { | |
1938 | if (errno != ENOENT) | |
1939 | return log_error_errno(errno, "Failed to check whether %s exists: %m", sysfs); | |
1940 | } else | |
1941 | return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Operating on partitions is currently not supported, sorry. Please specify a top-level block device."); | |
1942 | ||
1943 | if (flock(image_fd, LOCK_EX) < 0) /* make sure udev doesn't read from it while we operate on the device */ | |
1944 | return log_error_errno(errno, "Failed to lock block device %s: %m", ip); | |
1945 | ||
1946 | if (ioctl(image_fd, BLKGETSIZE64, &block_device_size) < 0) | |
1947 | return log_error_errno(errno, "Failed to read block device size: %m"); | |
1948 | ||
1949 | if (h->disk_size == UINT64_MAX) { | |
1950 | ||
1951 | /* If a relative disk size is requested, apply it relative to the block device size */ | |
1952 | if (h->disk_size_relative < UINT32_MAX) | |
1953 | host_size = CLAMP(DISK_SIZE_ROUND_DOWN(block_device_size * h->disk_size_relative / UINT32_MAX), | |
1954 | USER_DISK_SIZE_MIN, USER_DISK_SIZE_MAX); | |
1955 | else | |
1956 | host_size = block_device_size; /* Otherwise, take the full device */ | |
1957 | ||
1958 | } else if (h->disk_size > block_device_size) | |
1959 | return log_error_errno(SYNTHETIC_ERRNO(EMSGSIZE), "Selected disk size larger than backing block device, refusing."); | |
1960 | else | |
1961 | host_size = DISK_SIZE_ROUND_DOWN(h->disk_size); | |
1962 | ||
1963 | if (!supported_fs_size(fstype, host_size)) | |
1964 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Selected file system size too small for %s.", h->file_system_type); | |
1965 | ||
1966 | /* After creation we should reference this partition by its UUID instead of the block | |
1967 | * device. That's preferable since the user might have specified a device node such as | |
1968 | * /dev/sdb to us, which might look very different when replugged. */ | |
1969 | if (asprintf(&disk_uuid_path, "/dev/disk/by-uuid/" SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(luks_uuid)) < 0) | |
1970 | return log_oom(); | |
1971 | ||
28a7f106 LP |
1972 | if (user_record_luks_discard(h) || user_record_luks_offline_discard(h)) { |
1973 | /* If we want online or offline discard, discard once before we start using things. */ | |
1974 | ||
70a5db58 LP |
1975 | if (ioctl(image_fd, BLKDISCARD, (uint64_t[]) { 0, block_device_size }) < 0) |
1976 | log_full_errno(errno == EOPNOTSUPP ? LOG_DEBUG : LOG_WARNING, errno, | |
1977 | "Failed to issue full-device BLKDISCARD on device, ignoring: %m"); | |
1978 | else | |
1979 | log_info("Full device discard completed."); | |
1980 | } | |
1981 | } else { | |
1982 | _cleanup_free_ char *parent = NULL; | |
1983 | ||
1984 | parent = dirname_malloc(ip); | |
1985 | if (!parent) | |
1986 | return log_oom(); | |
1987 | ||
1988 | r = mkdir_p(parent, 0755); | |
1989 | if (r < 0) | |
1990 | return log_error_errno(r, "Failed to create parent directory %s: %m", parent); | |
1991 | ||
1992 | r = calculate_disk_size(h, parent, &host_size); | |
1993 | if (r < 0) | |
1994 | return r; | |
1995 | ||
1996 | if (!supported_fs_size(fstype, host_size)) | |
1997 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Selected file system size too small for %s.", h->file_system_type); | |
1998 | ||
1999 | r = tempfn_random(ip, "homework", &temporary_image_path); | |
2000 | if (r < 0) | |
2001 | return log_error_errno(r, "Failed to derive temporary file name for %s: %m", ip); | |
2002 | ||
2003 | image_fd = open(temporary_image_path, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600); | |
2004 | if (image_fd < 0) | |
2005 | return log_error_errno(errno, "Failed to create home image %s: %m", temporary_image_path); | |
2006 | ||
2007 | image_created = true; | |
2008 | ||
2009 | r = chattr_fd(image_fd, FS_NOCOW_FL, FS_NOCOW_FL, NULL); | |
2010 | if (r < 0) | |
2011 | log_warning_errno(r, "Failed to set file attributes on %s, ignoring: %m", temporary_image_path); | |
2012 | ||
e46f877c LP |
2013 | r = home_truncate(h, image_fd, temporary_image_path, host_size); |
2014 | if (r < 0) | |
70a5db58 | 2015 | goto fail; |
70a5db58 LP |
2016 | |
2017 | log_info("Allocating image file completed."); | |
2018 | } | |
2019 | ||
2020 | r = make_partition_table( | |
2021 | image_fd, | |
2022 | user_record_user_name_and_realm(h), | |
2023 | partition_uuid, | |
2024 | &partition_offset, | |
2025 | &partition_size, | |
2026 | &disk_uuid); | |
2027 | if (r < 0) | |
2028 | goto fail; | |
2029 | ||
2030 | log_info("Writing of partition table completed."); | |
2031 | ||
2032 | r = loop_device_make(image_fd, O_RDWR, partition_offset, partition_size, 0, &loop); | |
2033 | if (r < 0) { | |
2034 | if (r == -ENOENT) { /* this means /dev/loop-control doesn't exist, i.e. we are in a container | |
2035 | * or similar and loopback bock devices are not available, return a | |
2036 | * recognizable error in this case. */ | |
2037 | log_error_errno(r, "Loopback block device support is not available on this system."); | |
2038 | r = -ENOLINK; | |
2039 | goto fail; | |
2040 | } | |
2041 | ||
2042 | log_error_errno(r, "Failed to set up loopback device for %s: %m", temporary_image_path); | |
2043 | goto fail; | |
2044 | } | |
2045 | ||
2046 | r = loop_device_flock(loop, LOCK_EX); /* make sure udev won't read before we are done */ | |
2047 | if (r < 0) { | |
2048 | log_error_errno(r, "Failed to take lock on loop device: %m"); | |
2049 | goto fail; | |
2050 | } | |
2051 | ||
2052 | log_info("Setting up loopback device %s completed.", loop->node ?: ip); | |
2053 | ||
2054 | r = luks_format(loop->node, | |
2055 | dm_name, | |
2056 | luks_uuid, | |
2057 | user_record_user_name_and_realm(h), | |
2058 | pkcs11_decrypted_passwords, | |
2059 | effective_passwords, | |
28a7f106 | 2060 | user_record_luks_discard(h) || user_record_luks_offline_discard(h), |
70a5db58 LP |
2061 | h, |
2062 | &cd); | |
2063 | if (r < 0) | |
2064 | goto fail; | |
2065 | ||
2066 | dm_activated = true; | |
2067 | ||
2068 | r = block_get_size_by_path(dm_node, &encrypted_size); | |
2069 | if (r < 0) { | |
2070 | log_error_errno(r, "Failed to get encrypted block device size: %m"); | |
2071 | goto fail; | |
2072 | } | |
2073 | ||
2074 | log_info("Setting up LUKS device %s completed.", dm_node); | |
2075 | ||
2076 | r = run_mkfs(dm_node, fstype, user_record_user_name_and_realm(h), fs_uuid, user_record_luks_discard(h)); | |
2077 | if (r < 0) | |
2078 | goto fail; | |
2079 | ||
2080 | log_info("Formatting file system completed."); | |
2081 | ||
6a220cdb | 2082 | r = home_unshare_and_mount(dm_node, fstype, user_record_luks_discard(h), user_record_mount_flags(h)); |
70a5db58 LP |
2083 | if (r < 0) |
2084 | goto fail; | |
2085 | ||
2086 | mounted = true; | |
2087 | ||
2088 | subdir = path_join("/run/systemd/user-home-mount/", user_record_user_name_and_realm(h)); | |
2089 | if (!subdir) { | |
2090 | r = log_oom(); | |
2091 | goto fail; | |
2092 | } | |
2093 | ||
0be94a19 LP |
2094 | /* Prefer using a btrfs subvolume if we can, fall back to directory otherwise */ |
2095 | r = btrfs_subvol_make_fallback(subdir, 0700); | |
2096 | if (r < 0) { | |
2097 | log_error_errno(r, "Failed to create user directory in mounted image file: %m"); | |
70a5db58 LP |
2098 | goto fail; |
2099 | } | |
2100 | ||
2101 | root_fd = open(subdir, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); | |
2102 | if (root_fd < 0) { | |
2103 | r = log_error_errno(errno, "Failed to open user directory in mounted image file: %m"); | |
2104 | goto fail; | |
2105 | } | |
2106 | ||
2107 | r = home_populate(h, root_fd); | |
2108 | if (r < 0) | |
2109 | goto fail; | |
2110 | ||
2111 | r = home_sync_and_statfs(root_fd, &sfs); | |
2112 | if (r < 0) | |
2113 | goto fail; | |
2114 | ||
2115 | r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG, &new_home); | |
2116 | if (r < 0) { | |
2117 | log_error_errno(r, "Failed to clone record: %m"); | |
2118 | goto fail; | |
2119 | } | |
2120 | ||
2121 | r = user_record_add_binding( | |
2122 | new_home, | |
2123 | USER_LUKS, | |
2124 | disk_uuid_path ?: ip, | |
2125 | partition_uuid, | |
2126 | luks_uuid, | |
2127 | fs_uuid, | |
2128 | crypt_get_cipher(cd), | |
2129 | crypt_get_cipher_mode(cd), | |
2130 | luks_volume_key_size_convert(cd), | |
2131 | fstype, | |
2132 | NULL, | |
2133 | h->uid, | |
2134 | (gid_t) h->uid); | |
2135 | if (r < 0) { | |
2136 | log_error_errno(r, "Failed to add binding to record: %m"); | |
2137 | goto fail; | |
2138 | } | |
2139 | ||
28a7f106 LP |
2140 | if (user_record_luks_offline_discard(h)) { |
2141 | r = run_fitrim(root_fd); | |
2142 | if (r < 0) | |
2143 | goto fail; | |
2144 | } | |
2145 | ||
70a5db58 LP |
2146 | root_fd = safe_close(root_fd); |
2147 | ||
2148 | r = umount_verbose("/run/systemd/user-home-mount"); | |
2149 | if (r < 0) | |
2150 | goto fail; | |
2151 | ||
2152 | mounted = false; | |
2153 | ||
2154 | r = crypt_deactivate(cd, dm_name); | |
2155 | if (r < 0) { | |
2156 | log_error_errno(r, "Failed to deactivate LUKS device: %m"); | |
2157 | goto fail; | |
2158 | } | |
2159 | ||
2160 | dm_activated = false; | |
2161 | ||
2162 | loop = loop_device_unref(loop); | |
2163 | ||
28a7f106 LP |
2164 | if (!user_record_luks_offline_discard(h)) { |
2165 | r = run_fallocate(image_fd, NULL /* refresh stat() data */); | |
2166 | if (r < 0) | |
2167 | goto fail; | |
2168 | } | |
2169 | ||
70a5db58 LP |
2170 | if (disk_uuid_path) |
2171 | (void) ioctl(image_fd, BLKRRPART, 0); | |
2172 | ||
2173 | /* Let's close the image fd now. If we are operating on a real block device this will release the BSD | |
2174 | * lock that ensures udev doesn't interfere with what we are doing */ | |
2175 | image_fd = safe_close(image_fd); | |
2176 | ||
2177 | if (temporary_image_path) { | |
2178 | if (rename(temporary_image_path, ip) < 0) { | |
2179 | log_error_errno(errno, "Failed to rename image file: %m"); | |
2180 | goto fail; | |
2181 | } | |
2182 | ||
2183 | log_info("Moved image file into place."); | |
2184 | } | |
2185 | ||
2186 | if (disk_uuid_path) | |
2187 | (void) wait_for_devlink(disk_uuid_path); | |
2188 | ||
2189 | log_info("Everything completed."); | |
2190 | ||
2191 | print_size_summary(host_size, encrypted_size, &sfs); | |
2192 | ||
2193 | *ret_home = TAKE_PTR(new_home); | |
2194 | return 0; | |
2195 | ||
2196 | fail: | |
2197 | /* Let's close all files before we unmount the file system, to avoid EBUSY */ | |
2198 | root_fd = safe_close(root_fd); | |
2199 | ||
2200 | if (mounted) | |
2201 | (void) umount_verbose("/run/systemd/user-home-mount"); | |
2202 | ||
2203 | if (dm_activated) | |
2204 | (void) crypt_deactivate(cd, dm_name); | |
2205 | ||
2206 | loop = loop_device_unref(loop); | |
2207 | ||
2208 | if (image_created) | |
2209 | (void) unlink(temporary_image_path); | |
2210 | ||
2211 | return r; | |
2212 | } | |
2213 | ||
2214 | int home_validate_update_luks(UserRecord *h, HomeSetup *setup) { | |
2215 | _cleanup_free_ char *dm_name = NULL, *dm_node = NULL; | |
2216 | int r; | |
2217 | ||
2218 | assert(h); | |
2219 | assert(setup); | |
2220 | ||
2221 | r = make_dm_names(h->user_name, &dm_name, &dm_node); | |
2222 | if (r < 0) | |
2223 | return r; | |
2224 | ||
2225 | r = access(dm_node, F_OK); | |
2226 | if (r < 0 && errno != ENOENT) | |
2227 | return log_error_errno(errno, "Failed to determine whether %s exists: %m", dm_node); | |
2228 | ||
2229 | free_and_replace(setup->dm_name, dm_name); | |
2230 | free_and_replace(setup->dm_node, dm_node); | |
2231 | ||
2232 | return r >= 0; | |
2233 | } | |
2234 | ||
2235 | enum { | |
2236 | CAN_RESIZE_ONLINE, | |
2237 | CAN_RESIZE_OFFLINE, | |
2238 | }; | |
2239 | ||
2240 | static int can_resize_fs(int fd, uint64_t old_size, uint64_t new_size) { | |
2241 | struct statfs sfs; | |
2242 | ||
2243 | assert(fd >= 0); | |
2244 | ||
2245 | /* Filter out bogus requests early */ | |
2246 | if (old_size == 0 || old_size == UINT64_MAX || | |
2247 | new_size == 0 || new_size == UINT64_MAX) | |
2248 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid resize parameters."); | |
2249 | ||
2250 | if ((old_size & 511) != 0 || (new_size & 511) != 0) | |
2251 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Resize parameters not multiple of 512."); | |
2252 | ||
2253 | if (fstatfs(fd, &sfs) < 0) | |
2254 | return log_error_errno(errno, "Failed to fstatfs() file system: %m"); | |
2255 | ||
2256 | if (is_fs_type(&sfs, BTRFS_SUPER_MAGIC)) { | |
2257 | ||
2258 | if (new_size < BTRFS_MINIMAL_SIZE) | |
2259 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "New file system size too small for btrfs (needs to be 256M at least."); | |
2260 | ||
2261 | /* btrfs can grow and shrink online */ | |
2262 | ||
2263 | } else if (is_fs_type(&sfs, XFS_SB_MAGIC)) { | |
2264 | ||
2265 | if (new_size < XFS_MINIMAL_SIZE) | |
2266 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "New file system size too small for xfs (needs to be 14M at least)."); | |
2267 | ||
2268 | /* XFS can grow, but not shrink */ | |
2269 | if (new_size < old_size) | |
2270 | return log_error_errno(SYNTHETIC_ERRNO(EMSGSIZE), "Shrinking this type of file system is not supported."); | |
2271 | ||
2272 | } else if (is_fs_type(&sfs, EXT4_SUPER_MAGIC)) { | |
2273 | ||
2274 | if (new_size < EXT4_MINIMAL_SIZE) | |
2275 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "New file system size too small for ext4 (needs to be 1M at least)."); | |
2276 | ||
2277 | /* ext4 can grow online, and shrink offline */ | |
2278 | if (new_size < old_size) | |
2279 | return CAN_RESIZE_OFFLINE; | |
2280 | ||
2281 | } else | |
2282 | return log_error_errno(SYNTHETIC_ERRNO(ESOCKTNOSUPPORT), "Resizing this type of file system is not supported."); | |
2283 | ||
2284 | return CAN_RESIZE_ONLINE; | |
2285 | } | |
2286 | ||
6a220cdb | 2287 | static int ext4_offline_resize_fs(HomeSetup *setup, uint64_t new_size, bool discard, unsigned long flags) { |
70a5db58 LP |
2288 | _cleanup_free_ char *size_str = NULL; |
2289 | bool re_open = false, re_mount = false; | |
2290 | pid_t resize_pid, fsck_pid; | |
2291 | int r, exit_status; | |
2292 | ||
2293 | assert(setup); | |
2294 | assert(setup->dm_node); | |
2295 | ||
2296 | /* First, unmount the file system */ | |
2297 | if (setup->root_fd >= 0) { | |
2298 | setup->root_fd = safe_close(setup->root_fd); | |
2299 | re_open = true; | |
2300 | } | |
2301 | ||
2302 | if (setup->undo_mount) { | |
2303 | r = umount_verbose("/run/systemd/user-home-mount"); | |
2304 | if (r < 0) | |
2305 | return r; | |
2306 | ||
2307 | setup->undo_mount = false; | |
2308 | re_mount = true; | |
2309 | } | |
2310 | ||
80ace4f2 | 2311 | log_info("Temporary unmounting of file system completed."); |
70a5db58 LP |
2312 | |
2313 | /* resize2fs requires that the file system is force checked first, do so. */ | |
2314 | r = safe_fork("(e2fsck)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_STDOUT_TO_STDERR, &fsck_pid); | |
2315 | if (r < 0) | |
2316 | return r; | |
2317 | if (r == 0) { | |
2318 | /* Child */ | |
2319 | execlp("e2fsck" ,"e2fsck", "-fp", setup->dm_node, NULL); | |
2320 | log_error_errno(errno, "Failed to execute e2fsck: %m"); | |
2321 | _exit(EXIT_FAILURE); | |
2322 | } | |
2323 | ||
2324 | exit_status = wait_for_terminate_and_check("e2fsck", fsck_pid, WAIT_LOG_ABNORMAL); | |
2325 | if (exit_status < 0) | |
2326 | return exit_status; | |
2327 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != 0) { | |
2328 | log_warning("e2fsck failed with exit status %i.", exit_status); | |
2329 | ||
2330 | if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0) | |
2331 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "File system is corrupted, refusing."); | |
2332 | ||
2333 | log_warning("Ignoring fsck error."); | |
2334 | } | |
2335 | ||
2336 | log_info("Forced file system check completed."); | |
2337 | ||
2338 | /* We use 512 sectors here, because resize2fs doesn't do byte sizes */ | |
2339 | if (asprintf(&size_str, "%" PRIu64 "s", new_size / 512) < 0) | |
2340 | return log_oom(); | |
2341 | ||
2342 | /* Resize the thing */ | |
2343 | r = safe_fork("(e2resize)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR, &resize_pid); | |
2344 | if (r < 0) | |
2345 | return r; | |
2346 | if (r == 0) { | |
2347 | /* Child */ | |
2348 | execlp("resize2fs" ,"resize2fs", setup->dm_node, size_str, NULL); | |
2349 | log_error_errno(errno, "Failed to execute resize2fs: %m"); | |
2350 | _exit(EXIT_FAILURE); | |
2351 | } | |
2352 | ||
2353 | log_info("Offline file system resize completed."); | |
2354 | ||
2355 | /* Re-establish mounts and reopen the directory */ | |
2356 | if (re_mount) { | |
6a220cdb | 2357 | r = home_mount_node(setup->dm_node, "ext4", discard, flags); |
70a5db58 LP |
2358 | if (r < 0) |
2359 | return r; | |
2360 | ||
2361 | setup->undo_mount = true; | |
2362 | } | |
2363 | ||
2364 | if (re_open) { | |
2365 | setup->root_fd = open("/run/systemd/user-home-mount", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); | |
2366 | if (setup->root_fd < 0) | |
2367 | return log_error_errno(errno, "Failed to reopen file system: %m"); | |
2368 | } | |
2369 | ||
2370 | log_info("File system mounted again."); | |
2371 | ||
2372 | return 0; | |
2373 | } | |
2374 | ||
2375 | static int prepare_resize_partition( | |
2376 | int fd, | |
2377 | uint64_t partition_offset, | |
2378 | uint64_t old_partition_size, | |
2379 | uint64_t new_partition_size, | |
2380 | sd_id128_t *ret_disk_uuid, | |
2381 | struct fdisk_table **ret_table) { | |
2382 | ||
2383 | _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; | |
2384 | _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL; | |
2385 | _cleanup_free_ char *path = NULL, *disk_uuid_as_string = NULL; | |
2386 | size_t n_partitions, i; | |
2387 | sd_id128_t disk_uuid; | |
2388 | bool found = false; | |
2389 | int r; | |
2390 | ||
2391 | assert(fd >= 0); | |
2392 | assert(ret_disk_uuid); | |
2393 | assert(ret_table); | |
2394 | ||
2395 | assert((partition_offset & 511) == 0); | |
2396 | assert((old_partition_size & 511) == 0); | |
2397 | assert((new_partition_size & 511) == 0); | |
2398 | assert(UINT64_MAX - old_partition_size >= partition_offset); | |
2399 | assert(UINT64_MAX - new_partition_size >= partition_offset); | |
2400 | ||
2401 | if (partition_offset == 0) { | |
2402 | /* If the offset is at the beginning we assume no partition table, let's exit early. */ | |
2403 | log_debug("Not rewriting partition table, operating on naked device."); | |
2404 | *ret_disk_uuid = SD_ID128_NULL; | |
2405 | *ret_table = NULL; | |
2406 | return 0; | |
2407 | } | |
2408 | ||
2409 | c = fdisk_new_context(); | |
2410 | if (!c) | |
2411 | return log_oom(); | |
2412 | ||
2413 | if (asprintf(&path, "/proc/self/fd/%i", fd) < 0) | |
2414 | return log_oom(); | |
2415 | ||
2416 | r = fdisk_assign_device(c, path, 0); | |
2417 | if (r < 0) | |
2418 | return log_error_errno(r, "Failed to open device: %m"); | |
2419 | ||
2420 | if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) | |
2421 | return log_error_errno(SYNTHETIC_ERRNO(ENOMEDIUM), "Disk has no GPT partition table."); | |
2422 | ||
2423 | r = fdisk_get_disklabel_id(c, &disk_uuid_as_string); | |
2424 | if (r < 0) | |
2425 | return log_error_errno(r, "Failed to acquire disk UUID: %m"); | |
2426 | ||
2427 | r = sd_id128_from_string(disk_uuid_as_string, &disk_uuid); | |
2428 | if (r < 0) | |
2429 | return log_error_errno(r, "Failed parse disk UUID: %m"); | |
2430 | ||
2431 | r = fdisk_get_partitions(c, &t); | |
2432 | if (r < 0) | |
2433 | return log_error_errno(r, "Failed to acquire partition table: %m"); | |
2434 | ||
2435 | n_partitions = fdisk_table_get_nents(t); | |
2436 | for (i = 0; i < n_partitions; i++) { | |
2437 | struct fdisk_partition *p; | |
2438 | ||
2439 | p = fdisk_table_get_partition(t, i); | |
2440 | if (!p) | |
2441 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m"); | |
2442 | ||
2443 | if (fdisk_partition_is_used(p) <= 0) | |
2444 | continue; | |
2445 | if (fdisk_partition_has_start(p) <= 0 || fdisk_partition_has_size(p) <= 0 || fdisk_partition_has_end(p) <= 0) | |
2446 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found partition without a size."); | |
2447 | ||
2448 | if (fdisk_partition_get_start(p) == partition_offset / 512U && | |
2449 | fdisk_partition_get_size(p) == old_partition_size / 512U) { | |
2450 | ||
2451 | if (found) | |
2452 | return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ), "Partition found twice, refusing."); | |
2453 | ||
2454 | /* Found our partition, now patch it */ | |
2455 | r = fdisk_partition_size_explicit(p, 1); | |
2456 | if (r < 0) | |
2457 | return log_error_errno(r, "Failed to enable explicit partition size: %m"); | |
2458 | ||
2459 | r = fdisk_partition_set_size(p, new_partition_size / 512U); | |
2460 | if (r < 0) | |
2461 | return log_error_errno(r, "Failed to change partition size: %m"); | |
2462 | ||
2463 | found = true; | |
2464 | continue; | |
2465 | ||
2466 | } else { | |
2467 | if (fdisk_partition_get_start(p) < partition_offset + new_partition_size / 512U && | |
2468 | fdisk_partition_get_end(p) >= partition_offset / 512) | |
2469 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Can't extend, conflicting partition found."); | |
2470 | } | |
2471 | } | |
2472 | ||
2473 | if (!found) | |
2474 | return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to find matching partition to resize."); | |
2475 | ||
2476 | *ret_table = TAKE_PTR(t); | |
2477 | *ret_disk_uuid = disk_uuid; | |
2478 | ||
2479 | return 1; | |
2480 | } | |
2481 | ||
2482 | static int ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *userdata) { | |
2483 | char *result; | |
2484 | ||
2485 | assert(c); | |
2486 | ||
2487 | switch (fdisk_ask_get_type(ask)) { | |
2488 | ||
2489 | case FDISK_ASKTYPE_STRING: | |
2490 | result = new(char, 37); | |
2491 | if (!result) | |
2492 | return log_oom(); | |
2493 | ||
2494 | fdisk_ask_string_set_result(ask, id128_to_uuid_string(*(sd_id128_t*) userdata, result)); | |
2495 | break; | |
2496 | ||
2497 | default: | |
2498 | log_debug("Unexpected question from libfdisk, ignoring."); | |
2499 | } | |
2500 | ||
2501 | return 0; | |
2502 | } | |
2503 | ||
2504 | static int apply_resize_partition(int fd, sd_id128_t disk_uuids, struct fdisk_table *t) { | |
2505 | _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; | |
2506 | _cleanup_free_ void *two_zero_lbas = NULL; | |
2507 | _cleanup_free_ char *path = NULL; | |
2508 | ssize_t n; | |
2509 | int r; | |
2510 | ||
2511 | assert(fd >= 0); | |
2512 | ||
2513 | if (!t) /* no partition table to apply, exit early */ | |
2514 | return 0; | |
2515 | ||
2516 | two_zero_lbas = malloc0(1024U); | |
2517 | if (!two_zero_lbas) | |
2518 | return log_oom(); | |
2519 | ||
2520 | /* libfdisk appears to get confused by the existing PMBR. Let's explicitly flush it out. */ | |
2521 | n = pwrite(fd, two_zero_lbas, 1024U, 0); | |
2522 | if (n < 0) | |
2523 | return log_error_errno(errno, "Failed to wipe partition table: %m"); | |
2524 | if (n != 1024) | |
80ace4f2 | 2525 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while wiping partition table."); |
70a5db58 LP |
2526 | |
2527 | c = fdisk_new_context(); | |
2528 | if (!c) | |
2529 | return log_oom(); | |
2530 | ||
2531 | if (asprintf(&path, "/proc/self/fd/%i", fd) < 0) | |
2532 | return log_oom(); | |
2533 | ||
2534 | r = fdisk_assign_device(c, path, 0); | |
2535 | if (r < 0) | |
2536 | return log_error_errno(r, "Failed to open device: %m"); | |
2537 | ||
2538 | r = fdisk_create_disklabel(c, "gpt"); | |
2539 | if (r < 0) | |
2540 | return log_error_errno(r, "Failed to create GPT disk label: %m"); | |
2541 | ||
2542 | r = fdisk_apply_table(c, t); | |
2543 | if (r < 0) | |
2544 | return log_error_errno(r, "Failed to apply partition table: %m"); | |
2545 | ||
2546 | r = fdisk_set_ask(c, ask_cb, &disk_uuids); | |
2547 | if (r < 0) | |
2548 | return log_error_errno(r, "Failed to set libfdisk query function: %m"); | |
2549 | ||
2550 | r = fdisk_set_disklabel_id(c); | |
2551 | if (r < 0) | |
2552 | return log_error_errno(r, "Failed to change disklabel ID: %m"); | |
2553 | ||
2554 | r = fdisk_write_disklabel(c); | |
2555 | if (r < 0) | |
2556 | return log_error_errno(r, "Failed to write disk label: %m"); | |
2557 | ||
2558 | return 1; | |
2559 | } | |
2560 | ||
2561 | int home_resize_luks( | |
2562 | UserRecord *h, | |
2563 | bool already_activated, | |
2564 | char ***pkcs11_decrypted_passwords, | |
2565 | HomeSetup *setup, | |
2566 | UserRecord **ret_home) { | |
2567 | ||
2568 | char buffer1[FORMAT_BYTES_MAX], buffer2[FORMAT_BYTES_MAX], buffer3[FORMAT_BYTES_MAX], | |
2569 | buffer4[FORMAT_BYTES_MAX], buffer5[FORMAT_BYTES_MAX], buffer6[FORMAT_BYTES_MAX]; | |
2570 | uint64_t old_image_size, new_image_size, old_fs_size, new_fs_size, crypto_offset, new_partition_size; | |
2571 | _cleanup_(user_record_unrefp) UserRecord *header_home = NULL, *embedded_home = NULL, *new_home = NULL; | |
2572 | _cleanup_(fdisk_unref_tablep) struct fdisk_table *table = NULL; | |
2573 | _cleanup_free_ char *whole_disk = NULL; | |
2574 | _cleanup_close_ int image_fd = -1; | |
2575 | sd_id128_t disk_uuid; | |
2576 | const char *ip, *ipo; | |
2577 | struct statfs sfs; | |
2578 | struct stat st; | |
2579 | int r, resize_type; | |
2580 | ||
2581 | assert(h); | |
2582 | assert(user_record_storage(h) == USER_LUKS); | |
2583 | assert(setup); | |
2584 | assert(ret_home); | |
2585 | ||
2586 | assert_se(ipo = user_record_image_path(h)); | |
2587 | ip = strdupa(ipo); /* copy out since original might change later in home record object */ | |
2588 | ||
2589 | image_fd = open(ip, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
2590 | if (image_fd < 0) | |
2591 | return log_error_errno(errno, "Failed to open image file %s: %m", ip); | |
2592 | ||
2593 | if (fstat(image_fd, &st) < 0) | |
2594 | return log_error_errno(errno, "Failed to stat image file %s: %m", ip); | |
2595 | if (S_ISBLK(st.st_mode)) { | |
2596 | dev_t parent; | |
2597 | ||
2598 | r = block_get_whole_disk(st.st_rdev, &parent); | |
2599 | if (r < 0) | |
2600 | return log_error_errno(r, "Failed to acquire whole block device for %s: %m", ip); | |
2601 | if (r > 0) { | |
2602 | /* If we shall resize a file system on a partition device, then let's figure out the | |
2603 | * whole disk device and operate on that instead, since we need to rewrite the | |
2604 | * partition table to resize the partition. */ | |
2605 | ||
2606 | log_info("Operating on partition device %s, using parent device.", ip); | |
2607 | ||
2608 | r = device_path_make_major_minor(st.st_mode, parent, &whole_disk); | |
2609 | if (r < 0) | |
2610 | return log_error_errno(r, "Failed to derive whole disk path for %s: %m", ip); | |
2611 | ||
2612 | safe_close(image_fd); | |
2613 | ||
2614 | image_fd = open(whole_disk, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
2615 | if (image_fd < 0) | |
2616 | return log_error_errno(errno, "Failed to open whole block device %s: %m", whole_disk); | |
2617 | ||
2618 | if (fstat(image_fd, &st) < 0) | |
2619 | return log_error_errno(errno, "Failed to stat whole block device %s: %m", whole_disk); | |
2620 | if (!S_ISBLK(st.st_mode)) | |
2621 | return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Whole block device %s is not actually a block device, refusing.", whole_disk); | |
2622 | } else | |
2623 | log_info("Operating on whole block device %s.", ip); | |
2624 | ||
2625 | if (ioctl(image_fd, BLKGETSIZE64, &old_image_size) < 0) | |
2626 | return log_error_errno(errno, "Failed to determine size of original block device: %m"); | |
2627 | ||
2628 | if (flock(image_fd, LOCK_EX) < 0) /* make sure udev doesn't read from it while we operate on the device */ | |
2629 | return log_error_errno(errno, "Failed to lock block device %s: %m", ip); | |
2630 | ||
2631 | new_image_size = old_image_size; /* we can't resize physical block devices */ | |
2632 | } else { | |
2633 | r = stat_verify_regular(&st); | |
2634 | if (r < 0) | |
80ace4f2 | 2635 | return log_error_errno(r, "Image %s is not a block device nor regular file: %m", ip); |
70a5db58 LP |
2636 | |
2637 | old_image_size = st.st_size; | |
2638 | ||
2639 | /* Note an asymetry here: when we operate on loopback files the specified disk size we get we | |
2640 | * apply onto the loopback file as a whole. When we operate on block devices we instead apply | |
2641 | * to the partition itself only. */ | |
2642 | ||
2643 | new_image_size = DISK_SIZE_ROUND_DOWN(h->disk_size); | |
2644 | if (new_image_size == old_image_size) { | |
2645 | log_info("Image size already matching, skipping operation."); | |
2646 | return 0; | |
2647 | } | |
2648 | } | |
2649 | ||
2650 | r = home_prepare_luks(h, already_activated, whole_disk, pkcs11_decrypted_passwords, setup, &header_home); | |
2651 | if (r < 0) | |
2652 | return r; | |
2653 | ||
2654 | r = home_load_embedded_identity(h, setup->root_fd, header_home, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL, pkcs11_decrypted_passwords, &embedded_home, &new_home); | |
2655 | if (r < 0) | |
2656 | return r; | |
2657 | ||
2658 | log_info("offset = %" PRIu64 ", size = %" PRIu64 ", image = %" PRIu64, setup->partition_offset, setup->partition_size, old_image_size); | |
2659 | ||
2660 | if ((UINT64_MAX - setup->partition_offset) < setup->partition_size || | |
2661 | setup->partition_offset + setup->partition_size > old_image_size) | |
2662 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Old partition doesn't fit in backing storage, refusing."); | |
2663 | ||
2664 | if (S_ISREG(st.st_mode)) { | |
2665 | uint64_t partition_table_extra; | |
2666 | ||
2667 | partition_table_extra = old_image_size - setup->partition_size; | |
2668 | if (new_image_size <= partition_table_extra) | |
2669 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "New size smaller than partition table metadata."); | |
2670 | ||
2671 | new_partition_size = new_image_size - partition_table_extra; | |
2672 | } else { | |
2673 | assert(S_ISBLK(st.st_mode)); | |
2674 | ||
2675 | new_partition_size = DISK_SIZE_ROUND_DOWN(h->disk_size); | |
2676 | if (new_partition_size == setup->partition_size) { | |
2677 | log_info("Partition size already matching, skipping operation."); | |
2678 | return 0; | |
2679 | } | |
2680 | } | |
2681 | ||
2682 | if ((UINT64_MAX - setup->partition_offset) < new_partition_size || | |
2683 | setup->partition_offset + new_partition_size > new_image_size) | |
2684 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "New partition doesn't fit into backing storage, refusing."); | |
2685 | ||
2686 | crypto_offset = crypt_get_data_offset(setup->crypt_device); | |
2687 | if (setup->partition_size / 512U <= crypto_offset) | |
2688 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Weird, old crypto payload offset doesn't actually fit in partition size?"); | |
2689 | if (new_partition_size / 512U <= crypto_offset) | |
2690 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "New size smaller than crypto payload offset?"); | |
2691 | ||
2692 | old_fs_size = (setup->partition_size / 512U - crypto_offset) * 512U; | |
2693 | new_fs_size = (new_partition_size / 512U - crypto_offset) * 512U; | |
2694 | ||
2695 | /* Before we start doing anything, let's figure out if we actually can */ | |
2696 | resize_type = can_resize_fs(setup->root_fd, old_fs_size, new_fs_size); | |
2697 | if (resize_type < 0) | |
2698 | return resize_type; | |
2699 | if (resize_type == CAN_RESIZE_OFFLINE && already_activated) | |
2700 | return log_error_errno(SYNTHETIC_ERRNO(ETXTBSY), "File systems of this type can only be resized offline, but is currently online."); | |
2701 | ||
2702 | log_info("Ready to resize image size %s → %s, partition size %s → %s, file system size %s → %s.", | |
2703 | format_bytes(buffer1, sizeof(buffer1), old_image_size), | |
2704 | format_bytes(buffer2, sizeof(buffer2), new_image_size), | |
2705 | format_bytes(buffer3, sizeof(buffer3), setup->partition_size), | |
2706 | format_bytes(buffer4, sizeof(buffer4), new_partition_size), | |
2707 | format_bytes(buffer5, sizeof(buffer5), old_fs_size), | |
2708 | format_bytes(buffer6, sizeof(buffer6), new_fs_size)); | |
2709 | ||
2710 | r = prepare_resize_partition( | |
2711 | image_fd, | |
2712 | setup->partition_offset, | |
2713 | setup->partition_size, | |
2714 | new_partition_size, | |
2715 | &disk_uuid, | |
2716 | &table); | |
2717 | if (r < 0) | |
2718 | return r; | |
2719 | ||
2720 | if (new_fs_size > old_fs_size) { | |
2721 | ||
2722 | if (S_ISREG(st.st_mode)) { | |
2723 | /* Grow file size */ | |
e46f877c LP |
2724 | r = home_truncate(h, image_fd, ip, new_image_size); |
2725 | if (r < 0) | |
2726 | return r; | |
70a5db58 LP |
2727 | |
2728 | log_info("Growing of image file completed."); | |
2729 | } | |
2730 | ||
2731 | /* Make sure loopback device sees the new bigger size */ | |
2732 | r = loop_device_refresh_size(setup->loop, UINT64_MAX, new_partition_size); | |
2733 | if (r == -ENOTTY) | |
2734 | log_debug_errno(r, "Device is not a loopback device, not refreshing size."); | |
2735 | else if (r < 0) | |
2736 | return log_error_errno(r, "Failed to refresh loopback device size: %m"); | |
2737 | else | |
2738 | log_info("Refreshing loop device size completed."); | |
2739 | ||
2740 | r = apply_resize_partition(image_fd, disk_uuid, table); | |
2741 | if (r < 0) | |
2742 | return r; | |
2743 | if (r > 0) | |
2744 | log_info("Growing of partition completed."); | |
2745 | ||
2746 | if (ioctl(image_fd, BLKRRPART, 0) < 0) | |
2747 | log_debug_errno(errno, "BLKRRPART failed on block device, ignoring: %m"); | |
2748 | ||
2749 | /* Tell LUKS about the new bigger size too */ | |
2750 | r = crypt_resize(setup->crypt_device, setup->dm_name, new_fs_size / 512U); | |
2751 | if (r < 0) | |
2752 | return log_error_errno(r, "Failed to grow LUKS device: %m"); | |
2753 | ||
2754 | log_info("LUKS device growing completed."); | |
2755 | } else { | |
2756 | r = home_store_embedded_identity(new_home, setup->root_fd, h->uid, embedded_home); | |
2757 | if (r < 0) | |
2758 | return r; | |
2759 | ||
2760 | if (S_ISREG(st.st_mode)) { | |
2761 | if (user_record_luks_discard(h)) | |
2762 | /* Before we shrink, let's trim the file system, so that we need less space on disk during the shrinking */ | |
2763 | (void) run_fitrim(setup->root_fd); | |
2764 | else { | |
2765 | /* If discard is off, let's ensure all backing blocks are allocated, so that our resize operation doesn't fail half-way */ | |
2766 | r = run_fallocate(image_fd, &st); | |
2767 | if (r < 0) | |
2768 | return r; | |
2769 | } | |
2770 | } | |
2771 | } | |
2772 | ||
2773 | /* Now resize the file system */ | |
2774 | if (resize_type == CAN_RESIZE_ONLINE) | |
2775 | r = resize_fs(setup->root_fd, new_fs_size, NULL); | |
2776 | else | |
6a220cdb | 2777 | r = ext4_offline_resize_fs(setup, new_fs_size, user_record_luks_discard(h), user_record_mount_flags(h)); |
70a5db58 LP |
2778 | if (r < 0) |
2779 | return log_error_errno(r, "Failed to resize file system: %m"); | |
2780 | ||
2781 | log_info("File system resizing completed."); | |
2782 | ||
2783 | /* Immediately sync afterwards */ | |
2784 | r = home_sync_and_statfs(setup->root_fd, NULL); | |
2785 | if (r < 0) | |
2786 | return r; | |
2787 | ||
2788 | if (new_fs_size < old_fs_size) { | |
2789 | ||
2790 | /* Shrink the LUKS device now, matching the new file system size */ | |
2791 | r = crypt_resize(setup->crypt_device, setup->dm_name, new_fs_size / 512); | |
2792 | if (r < 0) | |
2793 | return log_error_errno(r, "Failed to shrink LUKS device: %m"); | |
2794 | ||
2795 | log_info("LUKS device shrinking completed."); | |
2796 | ||
2797 | if (S_ISREG(st.st_mode)) { | |
2798 | /* Shrink the image file */ | |
2799 | if (ftruncate(image_fd, new_image_size) < 0) | |
2800 | return log_error_errno(errno, "Failed to shrink image file %s: %m", ip); | |
2801 | ||
2802 | log_info("Shrinking of image file completed."); | |
2803 | } | |
2804 | ||
2805 | /* Refresh the loop devices size */ | |
2806 | r = loop_device_refresh_size(setup->loop, UINT64_MAX, new_partition_size); | |
2807 | if (r == -ENOTTY) | |
2808 | log_debug_errno(r, "Device is not a loopback device, not refreshing size."); | |
2809 | else if (r < 0) | |
2810 | return log_error_errno(r, "Failed to refresh loopback device size: %m"); | |
2811 | else | |
2812 | log_info("Refreshing loop device size completed."); | |
2813 | ||
2814 | r = apply_resize_partition(image_fd, disk_uuid, table); | |
2815 | if (r < 0) | |
2816 | return r; | |
2817 | if (r > 0) | |
2818 | log_info("Shrinking of partition completed."); | |
2819 | ||
2820 | if (ioctl(image_fd, BLKRRPART, 0) < 0) | |
2821 | log_debug_errno(errno, "BLKRRPART failed on block device, ignoring: %m"); | |
2822 | } else { | |
2823 | r = home_store_embedded_identity(new_home, setup->root_fd, h->uid, embedded_home); | |
2824 | if (r < 0) | |
2825 | return r; | |
2826 | } | |
2827 | ||
2828 | r = home_store_header_identity_luks(new_home, setup, header_home); | |
2829 | if (r < 0) | |
2830 | return r; | |
2831 | ||
2832 | r = home_extend_embedded_identity(new_home, h, setup); | |
2833 | if (r < 0) | |
2834 | return r; | |
2835 | ||
2836 | if (user_record_luks_discard(h)) | |
2837 | (void) run_fitrim(setup->root_fd); | |
2838 | ||
2839 | r = home_sync_and_statfs(setup->root_fd, &sfs); | |
2840 | if (r < 0) | |
2841 | return r; | |
2842 | ||
2843 | r = home_setup_undo(setup); | |
2844 | if (r < 0) | |
2845 | return r; | |
2846 | ||
2847 | log_info("Everything completed."); | |
2848 | ||
2849 | print_size_summary(new_image_size, new_fs_size, &sfs); | |
2850 | ||
2851 | *ret_home = TAKE_PTR(new_home); | |
2852 | return 0; | |
2853 | } | |
2854 | ||
2855 | int home_passwd_luks( | |
2856 | UserRecord *h, | |
2857 | HomeSetup *setup, | |
2858 | char **pkcs11_decrypted_passwords, /* the passwords acquired via PKCS#11 security tokens */ | |
2859 | char **effective_passwords /* new passwords */) { | |
2860 | ||
2861 | size_t volume_key_size, i, max_key_slots, n_effective; | |
2862 | _cleanup_(erase_and_freep) void *volume_key = NULL; | |
2863 | struct crypt_pbkdf_type good_pbkdf, minimal_pbkdf; | |
2864 | const char *type; | |
2865 | int r; | |
2866 | ||
2867 | assert(h); | |
2868 | assert(user_record_storage(h) == USER_LUKS); | |
2869 | assert(setup); | |
2870 | ||
2871 | type = crypt_get_type(setup->crypt_device); | |
2872 | if (!type) | |
2873 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine crypto device type."); | |
2874 | ||
2875 | r = crypt_keyslot_max(type); | |
2876 | if (r <= 0) | |
2877 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine number of key slots."); | |
2878 | max_key_slots = r; | |
2879 | ||
2880 | r = crypt_get_volume_key_size(setup->crypt_device); | |
2881 | if (r <= 0) | |
2882 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine volume key size."); | |
2883 | volume_key_size = (size_t) r; | |
2884 | ||
2885 | volume_key = malloc(volume_key_size); | |
2886 | if (!volume_key) | |
2887 | return log_oom(); | |
2888 | ||
2889 | r = luks_try_passwords(setup->crypt_device, pkcs11_decrypted_passwords, volume_key, &volume_key_size); | |
2890 | if (r == -ENOKEY) { | |
2891 | r = luks_try_passwords(setup->crypt_device, h->password, volume_key, &volume_key_size); | |
2892 | if (r == -ENOKEY) | |
2893 | return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Failed to unlock LUKS superblock with supplied passwords."); | |
2894 | } | |
2895 | if (r < 0) | |
2896 | return log_error_errno(r, "Failed to unlocks LUKS superblock: %m"); | |
2897 | ||
2898 | n_effective = strv_length(effective_passwords); | |
2899 | ||
2900 | build_good_pbkdf(&good_pbkdf, h); | |
2901 | build_minimal_pbkdf(&minimal_pbkdf, h); | |
2902 | ||
2903 | for (i = 0; i < max_key_slots; i++) { | |
2904 | r = crypt_keyslot_destroy(setup->crypt_device, i); | |
2905 | if (r < 0 && !IN_SET(r, -ENOENT, -EINVAL)) /* Returns EINVAL or ENOENT if there's no key in this slot already */ | |
2906 | return log_error_errno(r, "Failed to destroy LUKS password: %m"); | |
2907 | ||
2908 | if (i >= n_effective) { | |
2909 | if (r >= 0) | |
2910 | log_info("Destroyed LUKS key slot %zu.", i); | |
2911 | continue; | |
2912 | } | |
2913 | ||
2914 | if (strv_find(pkcs11_decrypted_passwords, effective_passwords[i])) { | |
2915 | log_debug("Using minimal PBKDF for slot %zu", i); | |
2916 | r = crypt_set_pbkdf_type(setup->crypt_device, &minimal_pbkdf); | |
2917 | } else { | |
2918 | log_debug("Using good PBKDF for slot %zu", i); | |
2919 | r = crypt_set_pbkdf_type(setup->crypt_device, &good_pbkdf); | |
2920 | } | |
2921 | if (r < 0) | |
2922 | return log_error_errno(r, "Failed to tweak PBKDF for slot %zu: %m", i); | |
2923 | ||
2924 | r = crypt_keyslot_add_by_volume_key( | |
2925 | setup->crypt_device, | |
2926 | i, | |
2927 | volume_key, | |
2928 | volume_key_size, | |
2929 | effective_passwords[i], | |
2930 | strlen(effective_passwords[i])); | |
2931 | if (r < 0) | |
2932 | return log_error_errno(r, "Failed to set up LUKS password: %m"); | |
2933 | ||
2934 | log_info("Updated LUKS key slot %zu.", i); | |
2935 | } | |
2936 | ||
2937 | return 1; | |
2938 | } | |
2939 | ||
2940 | int home_lock_luks(UserRecord *h) { | |
2941 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; | |
2942 | _cleanup_free_ char *dm_name = NULL, *dm_node = NULL; | |
2943 | _cleanup_close_ int root_fd = -1; | |
2944 | const char *p; | |
2945 | int r; | |
2946 | ||
2947 | assert(h); | |
2948 | ||
2949 | assert_se(p = user_record_home_directory(h)); | |
2950 | root_fd = open(p, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); | |
2951 | if (root_fd < 0) | |
2952 | return log_error_errno(errno, "Failed to open home directory: %m"); | |
2953 | ||
2954 | r = make_dm_names(h->user_name, &dm_name, &dm_node); | |
2955 | if (r < 0) | |
2956 | return r; | |
2957 | ||
2958 | r = crypt_init_by_name(&cd, dm_name); | |
2959 | if (r < 0) | |
2960 | return log_error_errno(r, "Failed to initialize cryptsetup context for %s: %m", dm_name); | |
2961 | ||
2962 | log_info("Discovered used LUKS device %s.", dm_node); | |
2963 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); | |
2964 | ||
2965 | if (syncfs(root_fd) < 0) /* Snake oil, but let's better be safe than sorry */ | |
2966 | return log_error_errno(errno, "Failed to synchronize file system %s: %m", p); | |
2967 | ||
2968 | root_fd = safe_close(root_fd); | |
2969 | ||
2970 | log_info("File system synchronized."); | |
2971 | ||
2972 | /* Note that we don't invoke FIFREEZE here, it appears libcryptsetup/device-mapper already does that on its own for us */ | |
2973 | ||
2974 | r = crypt_suspend(cd, dm_name); | |
2975 | if (r < 0) | |
2976 | return log_error_errno(r, "Failed to suspend cryptsetup device: %s: %m", dm_node); | |
2977 | ||
2978 | log_info("LUKS device suspended."); | |
2979 | return 0; | |
2980 | } | |
2981 | ||
2982 | static int luks_try_resume( | |
2983 | struct crypt_device *cd, | |
2984 | const char *dm_name, | |
2985 | char **password) { | |
2986 | ||
2987 | char **pp; | |
2988 | int r; | |
2989 | ||
2990 | assert(cd); | |
2991 | assert(dm_name); | |
2992 | ||
2993 | STRV_FOREACH(pp, password) { | |
2994 | r = crypt_resume_by_passphrase( | |
2995 | cd, | |
2996 | dm_name, | |
2997 | CRYPT_ANY_SLOT, | |
2998 | *pp, | |
2999 | strlen(*pp)); | |
3000 | if (r >= 0) { | |
3001 | log_info("Resumed LUKS device %s.", dm_name); | |
3002 | return 0; | |
3003 | } | |
3004 | ||
3005 | log_debug_errno(r, "Password %zu didn't work for resuming device: %m", (size_t) (pp - password)); | |
3006 | } | |
3007 | ||
3008 | return -ENOKEY; | |
3009 | } | |
3010 | ||
3011 | int home_unlock_luks(UserRecord *h, char ***pkcs11_decrypted_passwords) { | |
3012 | _cleanup_free_ char *dm_name = NULL, *dm_node = NULL; | |
3013 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; | |
3014 | int r; | |
3015 | ||
3016 | assert(h); | |
3017 | ||
3018 | r = make_dm_names(h->user_name, &dm_name, &dm_node); | |
3019 | if (r < 0) | |
3020 | return r; | |
3021 | ||
3022 | r = crypt_init_by_name(&cd, dm_name); | |
3023 | if (r < 0) | |
3024 | return log_error_errno(r, "Failed to initialize cryptsetup context for %s: %m", dm_name); | |
3025 | ||
3026 | log_info("Discovered used LUKS device %s.", dm_node); | |
3027 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); | |
3028 | ||
3029 | r = luks_try_resume(cd, dm_name, pkcs11_decrypted_passwords ? *pkcs11_decrypted_passwords : NULL); | |
3030 | if (r == -ENOKEY) { | |
3031 | r = luks_try_resume(cd, dm_name, h->password); | |
3032 | if (r == -ENOKEY) | |
3033 | return log_error_errno(r, "No valid password for LUKS superblock."); | |
3034 | } | |
3035 | if (r < 0) | |
3036 | return log_error_errno(r, "Failed to resume LUKS superblock: %m"); | |
3037 | ||
3038 | log_info("LUKS device resumed."); | |
3039 | return 0; | |
3040 | } |