]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homework-luks.c
homed: handle EINTR gracefully when waiting for device node
[thirdparty/systemd.git] / src / home / homework-luks.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/loop.h>
4 #include <poll.h>
5 #include <sys/file.h>
6 #include <sys/ioctl.h>
7 #include <sys/xattr.h>
8
9 #if HAVE_VALGRIND_MEMCHECK_H
10 #include <valgrind/memcheck.h>
11 #endif
12
13 #include "sd-daemon.h"
14 #include "sd-device.h"
15 #include "sd-event.h"
16 #include "sd-id128.h"
17
18 #include "blkid-util.h"
19 #include "blockdev-util.h"
20 #include "btrfs-util.h"
21 #include "chattr-util.h"
22 #include "device-util.h"
23 #include "devnum-util.h"
24 #include "dm-util.h"
25 #include "env-util.h"
26 #include "errno-util.h"
27 #include "fd-util.h"
28 #include "fdisk-util.h"
29 #include "fileio.h"
30 #include "filesystems.h"
31 #include "fs-util.h"
32 #include "fsck-util.h"
33 #include "glyph-util.h"
34 #include "gpt.h"
35 #include "home-util.h"
36 #include "homework-luks.h"
37 #include "homework-mount.h"
38 #include "io-util.h"
39 #include "keyring-util.h"
40 #include "memory-util.h"
41 #include "missing_magic.h"
42 #include "mkdir.h"
43 #include "mkfs-util.h"
44 #include "mount-util.h"
45 #include "openssl-util.h"
46 #include "parse-util.h"
47 #include "path-util.h"
48 #include "process-util.h"
49 #include "random-util.h"
50 #include "resize-fs.h"
51 #include "strv.h"
52 #include "sync-util.h"
53 #include "tmpfile-util.h"
54 #include "udev-util.h"
55 #include "user-util.h"
56
57 /* Round down to the nearest 4K size. Given that newer hardware generally prefers 4K sectors, let's align our
58 * partitions to that too. In the worst case we'll waste 3.5K per partition that way, but I think I can live
59 * with that. */
60 #define DISK_SIZE_ROUND_DOWN(x) ((x) & ~UINT64_C(4095))
61
62 /* Rounds up to the nearest 4K boundary. Returns UINT64_MAX on overflow */
63 #define DISK_SIZE_ROUND_UP(x) \
64 ({ \
65 uint64_t _x = (x); \
66 _x > UINT64_MAX - 4095U ? UINT64_MAX : (_x + 4095U) & ~UINT64_C(4095); \
67 })
68
69 /* How much larger will the image on disk be than the fs inside it, i.e. the space we pay for the GPT and
70 * LUKS2 envelope. (As measured on cryptsetup 2.4.1) */
71 #define GPT_LUKS2_OVERHEAD UINT64_C(18874368)
72
73 static int resize_image_loop(UserRecord *h, HomeSetup *setup, uint64_t old_image_size, uint64_t new_image_size, uint64_t *ret_image_size);
74
75 int run_mark_dirty(int fd, bool b) {
76 char x = '1';
77 int r, ret;
78
79 /* Sets or removes the 'user.home-dirty' xattr on the specified file. We use this to detect when a
80 * home directory was not properly unmounted. */
81
82 assert(fd >= 0);
83
84 r = fd_verify_regular(fd);
85 if (r < 0)
86 return r;
87
88 if (b) {
89 ret = fsetxattr(fd, "user.home-dirty", &x, 1, XATTR_CREATE);
90 if (ret < 0 && errno != EEXIST)
91 return log_debug_errno(errno, "Could not mark home directory as dirty: %m");
92
93 } else {
94 r = fsync_full(fd);
95 if (r < 0)
96 return log_debug_errno(r, "Failed to synchronize image before marking it clean: %m");
97
98 ret = fremovexattr(fd, "user.home-dirty");
99 if (ret < 0 && !ERRNO_IS_XATTR_ABSENT(errno))
100 return log_debug_errno(errno, "Could not mark home directory as clean: %m");
101 }
102
103 r = fsync_full(fd);
104 if (r < 0)
105 return log_debug_errno(r, "Failed to synchronize dirty flag to disk: %m");
106
107 return ret >= 0;
108 }
109
110 int run_mark_dirty_by_path(const char *path, bool b) {
111 _cleanup_close_ int fd = -1;
112
113 assert(path);
114
115 fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY);
116 if (fd < 0)
117 return log_debug_errno(errno, "Failed to open %s to mark dirty or clean: %m", path);
118
119 return run_mark_dirty(fd, b);
120 }
121
122 static int probe_file_system_by_fd(
123 int fd,
124 char **ret_fstype,
125 sd_id128_t *ret_uuid) {
126
127 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
128 _cleanup_free_ char *s = NULL;
129 const char *fstype = NULL, *uuid = NULL;
130 sd_id128_t id;
131 int r;
132
133 assert(fd >= 0);
134 assert(ret_fstype);
135 assert(ret_uuid);
136
137 b = blkid_new_probe();
138 if (!b)
139 return -ENOMEM;
140
141 errno = 0;
142 r = blkid_probe_set_device(b, fd, 0, 0);
143 if (r != 0)
144 return errno > 0 ? -errno : -ENOMEM;
145
146 (void) blkid_probe_enable_superblocks(b, 1);
147 (void) blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_UUID);
148
149 errno = 0;
150 r = blkid_do_safeprobe(b);
151 if (IN_SET(r, -2, 1)) /* nothing found or ambiguous result */
152 return -ENOPKG;
153 if (r != 0)
154 return errno > 0 ? -errno : -EIO;
155
156 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
157 if (!fstype)
158 return -ENOPKG;
159
160 (void) blkid_probe_lookup_value(b, "UUID", &uuid, NULL);
161 if (!uuid)
162 return -ENOPKG;
163
164 r = sd_id128_from_string(uuid, &id);
165 if (r < 0)
166 return r;
167
168 s = strdup(fstype);
169 if (!s)
170 return -ENOMEM;
171
172 *ret_fstype = TAKE_PTR(s);
173 *ret_uuid = id;
174
175 return 0;
176 }
177
178 static int probe_file_system_by_path(const char *path, char **ret_fstype, sd_id128_t *ret_uuid) {
179 _cleanup_close_ int fd = -1;
180
181 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
182 if (fd < 0)
183 return negative_errno();
184
185 return probe_file_system_by_fd(fd, ret_fstype, ret_uuid);
186 }
187
188 static int block_get_size_by_fd(int fd, uint64_t *ret) {
189 struct stat st;
190
191 assert(fd >= 0);
192 assert(ret);
193
194 if (fstat(fd, &st) < 0)
195 return -errno;
196
197 if (!S_ISBLK(st.st_mode))
198 return -ENOTBLK;
199
200 return RET_NERRNO(ioctl(fd, BLKGETSIZE64, ret));
201 }
202
203 static int block_get_size_by_path(const char *path, uint64_t *ret) {
204 _cleanup_close_ int fd = -1;
205
206 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
207 if (fd < 0)
208 return -errno;
209
210 return block_get_size_by_fd(fd, ret);
211 }
212
213 static int run_fsck(const char *node, const char *fstype) {
214 int r, exit_status;
215 pid_t fsck_pid;
216
217 assert(node);
218 assert(fstype);
219
220 r = fsck_exists_for_fstype(fstype);
221 if (r < 0)
222 return log_error_errno(r, "Failed to check if fsck for file system %s exists: %m", fstype);
223 if (r == 0) {
224 log_warning("No fsck for file system %s installed, ignoring.", fstype);
225 return 0;
226 }
227
228 r = safe_fork("(fsck)",
229 FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_STDOUT_TO_STDERR|FORK_CLOSE_ALL_FDS,
230 &fsck_pid);
231 if (r < 0)
232 return r;
233 if (r == 0) {
234 /* Child */
235 execl("/sbin/fsck", "/sbin/fsck", "-aTl", node, NULL);
236 log_open();
237 log_error_errno(errno, "Failed to execute fsck: %m");
238 _exit(FSCK_OPERATIONAL_ERROR);
239 }
240
241 exit_status = wait_for_terminate_and_check("fsck", fsck_pid, WAIT_LOG_ABNORMAL);
242 if (exit_status < 0)
243 return exit_status;
244 if ((exit_status & ~FSCK_ERROR_CORRECTED) != 0) {
245 log_warning("fsck failed with exit status %i.", exit_status);
246
247 if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0)
248 return log_error_errno(SYNTHETIC_ERRNO(EIO), "File system is corrupted, refusing.");
249
250 log_warning("Ignoring fsck error.");
251 }
252
253 log_info("File system check completed.");
254
255 return 1;
256 }
257
258 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(key_serial_t, keyring_unlink, -1);
259
260 static int upload_to_keyring(
261 UserRecord *h,
262 const char *password,
263 key_serial_t *ret_key_serial) {
264
265 _cleanup_free_ char *name = NULL;
266 key_serial_t serial;
267
268 assert(h);
269 assert(password);
270
271 /* If auto-shrink-on-logout is turned on, we need to keep the key we used to unlock the LUKS volume
272 * around, since we'll need it when automatically resizing (since we can't ask the user there
273 * again). We do this by uploading it into the kernel keyring, specifically the "session" one. This
274 * is done under the assumption systemd-homed gets its private per-session keyring (i.e. default
275 * service behaviour, given that KeyringMode=private is the default). It will survive between our
276 * systemd-homework invocations that way.
277 *
278 * If auto-shrink-on-logout is disabled we'll skip this step, to be frugal with sensitive data. */
279
280 if (user_record_auto_resize_mode(h) != AUTO_RESIZE_SHRINK_AND_GROW) { /* Won't need it */
281 if (ret_key_serial)
282 *ret_key_serial = -1;
283 return 0;
284 }
285
286 name = strjoin("homework-user-", h->user_name);
287 if (!name)
288 return -ENOMEM;
289
290 serial = add_key("user", name, password, strlen(password), KEY_SPEC_SESSION_KEYRING);
291 if (serial == -1)
292 return -errno;
293
294 if (ret_key_serial)
295 *ret_key_serial = serial;
296
297 return 1;
298 }
299
300 static int luks_try_passwords(
301 UserRecord *h,
302 struct crypt_device *cd,
303 char **passwords,
304 void *volume_key,
305 size_t *volume_key_size,
306 key_serial_t *ret_key_serial) {
307
308 int r;
309
310 assert(h);
311 assert(cd);
312
313 STRV_FOREACH(pp, passwords) {
314 size_t vks = *volume_key_size;
315
316 r = sym_crypt_volume_key_get(
317 cd,
318 CRYPT_ANY_SLOT,
319 volume_key,
320 &vks,
321 *pp,
322 strlen(*pp));
323 if (r >= 0) {
324 if (ret_key_serial) {
325 /* If ret_key_serial is non-NULL, let's try to upload the password that
326 * worked, and return its serial. */
327 r = upload_to_keyring(h, *pp, ret_key_serial);
328 if (r < 0) {
329 log_debug_errno(r, "Failed to upload LUKS password to kernel keyring, ignoring: %m");
330 *ret_key_serial = -1;
331 }
332 }
333
334 *volume_key_size = vks;
335 return 0;
336 }
337
338 log_debug_errno(r, "Password %zu didn't work for unlocking LUKS superblock: %m", (size_t) (pp - passwords));
339 }
340
341 return -ENOKEY;
342 }
343
344 static int luks_setup(
345 UserRecord *h,
346 const char *node,
347 const char *dm_name,
348 sd_id128_t uuid,
349 const char *cipher,
350 const char *cipher_mode,
351 uint64_t volume_key_size,
352 char **passwords,
353 const PasswordCache *cache,
354 bool discard,
355 struct crypt_device **ret,
356 sd_id128_t *ret_found_uuid,
357 void **ret_volume_key,
358 size_t *ret_volume_key_size,
359 key_serial_t *ret_key_serial) {
360
361 _cleanup_(keyring_unlinkp) key_serial_t key_serial = -1;
362 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
363 _cleanup_(erase_and_freep) void *vk = NULL;
364 sd_id128_t p;
365 size_t vks;
366 char **list;
367 int r;
368
369 assert(h);
370 assert(node);
371 assert(dm_name);
372 assert(ret);
373
374 r = sym_crypt_init(&cd, node);
375 if (r < 0)
376 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
377
378 cryptsetup_enable_logging(cd);
379
380 r = sym_crypt_load(cd, CRYPT_LUKS2, NULL);
381 if (r < 0)
382 return log_error_errno(r, "Failed to load LUKS superblock: %m");
383
384 r = sym_crypt_get_volume_key_size(cd);
385 if (r <= 0)
386 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine LUKS volume key size");
387 vks = (size_t) r;
388
389 if (!sd_id128_is_null(uuid) || ret_found_uuid) {
390 const char *s;
391
392 s = sym_crypt_get_uuid(cd);
393 if (!s)
394 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has no UUID.");
395
396 r = sd_id128_from_string(s, &p);
397 if (r < 0)
398 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has invalid UUID.");
399
400 /* Check that the UUID matches, if specified */
401 if (!sd_id128_is_null(uuid) &&
402 !sd_id128_equal(uuid, p))
403 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has wrong UUID.");
404 }
405
406 if (cipher && !streq_ptr(cipher, sym_crypt_get_cipher(cd)))
407 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock declares wrong cipher.");
408
409 if (cipher_mode && !streq_ptr(cipher_mode, sym_crypt_get_cipher_mode(cd)))
410 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock declares wrong cipher mode.");
411
412 if (volume_key_size != UINT64_MAX && vks != volume_key_size)
413 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock declares wrong volume key size.");
414
415 vk = malloc(vks);
416 if (!vk)
417 return log_oom();
418
419 r = -ENOKEY;
420 FOREACH_POINTER(list,
421 cache ? cache->keyring_passswords : NULL,
422 cache ? cache->pkcs11_passwords : NULL,
423 cache ? cache->fido2_passwords : NULL,
424 passwords) {
425 r = luks_try_passwords(h, cd, list, vk, &vks, ret_key_serial ? &key_serial : NULL);
426 if (r != -ENOKEY)
427 break;
428 }
429 if (r == -ENOKEY)
430 return log_error_errno(r, "No valid password for LUKS superblock.");
431 if (r < 0)
432 return log_error_errno(r, "Failed to unlock LUKS superblock: %m");
433
434 r = sym_crypt_activate_by_volume_key(
435 cd,
436 dm_name,
437 vk, vks,
438 discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0);
439 if (r < 0)
440 return log_error_errno(r, "Failed to unlock LUKS superblock: %m");
441
442 log_info("Setting up LUKS device /dev/mapper/%s completed.", dm_name);
443
444 *ret = TAKE_PTR(cd);
445
446 if (ret_found_uuid) /* Return the UUID actually found if the caller wants to know */
447 *ret_found_uuid = p;
448 if (ret_volume_key)
449 *ret_volume_key = TAKE_PTR(vk);
450 if (ret_volume_key_size)
451 *ret_volume_key_size = vks;
452 if (ret_key_serial)
453 *ret_key_serial = TAKE_KEY_SERIAL(key_serial);
454
455 return 0;
456 }
457
458 static int make_dm_names(UserRecord *h, HomeSetup *setup) {
459 assert(h);
460 assert(h->user_name);
461 assert(setup);
462
463 if (!setup->dm_name) {
464 setup->dm_name = strjoin("home-", h->user_name);
465 if (!setup->dm_name)
466 return log_oom();
467 }
468
469 if (!setup->dm_node) {
470 setup->dm_node = path_join("/dev/mapper/", setup->dm_name);
471 if (!setup->dm_node)
472 return log_oom();
473 }
474
475 return 0;
476 }
477
478 static int acquire_open_luks_device(
479 UserRecord *h,
480 HomeSetup *setup,
481 bool graceful) {
482
483 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
484 int r;
485
486 assert(h);
487 assert(setup);
488 assert(!setup->crypt_device);
489
490 r = dlopen_cryptsetup();
491 if (r < 0)
492 return r;
493
494 r = make_dm_names(h, setup);
495 if (r < 0)
496 return r;
497
498 r = sym_crypt_init_by_name(&cd, setup->dm_name);
499 if ((ERRNO_IS_DEVICE_ABSENT(r) || r == -EINVAL) && graceful)
500 return 0;
501 if (r < 0)
502 return log_error_errno(r, "Failed to initialize cryptsetup context for %s: %m", setup->dm_name);
503
504 cryptsetup_enable_logging(cd);
505
506 setup->crypt_device = TAKE_PTR(cd);
507 return 1;
508 }
509
510 static int luks_open(
511 UserRecord *h,
512 HomeSetup *setup,
513 const PasswordCache *cache,
514 sd_id128_t *ret_found_uuid,
515 void **ret_volume_key,
516 size_t *ret_volume_key_size) {
517
518 _cleanup_(erase_and_freep) void *vk = NULL;
519 sd_id128_t p;
520 char **list;
521 size_t vks;
522 int r;
523
524 assert(h);
525 assert(setup);
526 assert(!setup->crypt_device);
527
528 /* Opens a LUKS device that is already set up. Re-validates the password while doing so (which also
529 * provides us with the volume key, which we want). */
530
531 r = acquire_open_luks_device(h, setup, /* graceful= */ false);
532 if (r < 0)
533 return r;
534
535 r = sym_crypt_load(setup->crypt_device, CRYPT_LUKS2, NULL);
536 if (r < 0)
537 return log_error_errno(r, "Failed to load LUKS superblock: %m");
538
539 r = sym_crypt_get_volume_key_size(setup->crypt_device);
540 if (r <= 0)
541 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine LUKS volume key size");
542 vks = (size_t) r;
543
544 if (ret_found_uuid) {
545 const char *s;
546
547 s = sym_crypt_get_uuid(setup->crypt_device);
548 if (!s)
549 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has no UUID.");
550
551 r = sd_id128_from_string(s, &p);
552 if (r < 0)
553 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "LUKS superblock has invalid UUID.");
554 }
555
556 vk = malloc(vks);
557 if (!vk)
558 return log_oom();
559
560 r = -ENOKEY;
561 FOREACH_POINTER(list,
562 cache ? cache->keyring_passswords : NULL,
563 cache ? cache->pkcs11_passwords : NULL,
564 cache ? cache->fido2_passwords : NULL,
565 h->password) {
566 r = luks_try_passwords(h, setup->crypt_device, list, vk, &vks, NULL);
567 if (r != -ENOKEY)
568 break;
569 }
570 if (r == -ENOKEY)
571 return log_error_errno(r, "No valid password for LUKS superblock.");
572 if (r < 0)
573 return log_error_errno(r, "Failed to unlocks LUKS superblock: %m");
574
575 log_info("Discovered used LUKS device /dev/mapper/%s, and validated password.", setup->dm_name);
576
577 /* This is needed so that crypt_resize() can operate correctly for pre-existing LUKS devices. We need
578 * to tell libcryptsetup the volume key explicitly, so that it is in the kernel keyring. */
579 r = sym_crypt_activate_by_volume_key(setup->crypt_device, NULL, vk, vks, CRYPT_ACTIVATE_KEYRING_KEY);
580 if (r < 0)
581 return log_error_errno(r, "Failed to upload volume key again: %m");
582
583 log_info("Successfully re-activated LUKS device.");
584
585 if (ret_found_uuid)
586 *ret_found_uuid = p;
587 if (ret_volume_key)
588 *ret_volume_key = TAKE_PTR(vk);
589 if (ret_volume_key_size)
590 *ret_volume_key_size = vks;
591
592 return 0;
593 }
594
595 static int fs_validate(
596 const char *dm_node,
597 sd_id128_t uuid,
598 char **ret_fstype,
599 sd_id128_t *ret_found_uuid) {
600
601 _cleanup_free_ char *fstype = NULL;
602 sd_id128_t u;
603 int r;
604
605 assert(dm_node);
606 assert(ret_fstype);
607
608 r = probe_file_system_by_path(dm_node, &fstype, &u);
609 if (r < 0)
610 return log_error_errno(r, "Failed to probe file system: %m");
611
612 /* Limit the set of supported file systems a bit, as protection against little tested kernel file
613 * systems. Also, we only support the resize ioctls for these file systems. */
614 if (!supported_fstype(fstype))
615 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "Image contains unsupported file system: %s", strna(fstype));
616
617 if (!sd_id128_is_null(uuid) &&
618 !sd_id128_equal(uuid, u))
619 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE), "File system has wrong UUID.");
620
621 log_info("Probing file system completed (found %s).", fstype);
622
623 *ret_fstype = TAKE_PTR(fstype);
624
625 if (ret_found_uuid) /* Return the UUID actually found if the caller wants to know */
626 *ret_found_uuid = u;
627
628 return 0;
629 }
630
631 static int luks_validate(
632 int fd,
633 const char *label,
634 sd_id128_t partition_uuid,
635 sd_id128_t *ret_partition_uuid,
636 uint64_t *ret_offset,
637 uint64_t *ret_size) {
638
639 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
640 sd_id128_t found_partition_uuid = SD_ID128_NULL;
641 const char *fstype = NULL, *pttype = NULL;
642 blkid_loff_t offset = 0, size = 0;
643 blkid_partlist pl;
644 bool found = false;
645 int r, n;
646
647 assert(fd >= 0);
648 assert(label);
649 assert(ret_offset);
650 assert(ret_size);
651
652 b = blkid_new_probe();
653 if (!b)
654 return -ENOMEM;
655
656 errno = 0;
657 r = blkid_probe_set_device(b, fd, 0, 0);
658 if (r != 0)
659 return errno > 0 ? -errno : -ENOMEM;
660
661 (void) blkid_probe_enable_superblocks(b, 1);
662 (void) blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
663 (void) blkid_probe_enable_partitions(b, 1);
664 (void) blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
665
666 errno = 0;
667 r = blkid_do_safeprobe(b);
668 if (IN_SET(r, -2, 1)) /* nothing found or ambiguous result */
669 return -ENOPKG;
670 if (r != 0)
671 return errno > 0 ? -errno : -EIO;
672
673 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
674 if (streq_ptr(fstype, "crypto_LUKS")) {
675 /* Directly a LUKS image */
676 *ret_offset = 0;
677 *ret_size = UINT64_MAX; /* full disk */
678 *ret_partition_uuid = SD_ID128_NULL;
679 return 0;
680 } else if (fstype)
681 return -ENOPKG;
682
683 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
684 if (!streq_ptr(pttype, "gpt"))
685 return -ENOPKG;
686
687 errno = 0;
688 pl = blkid_probe_get_partitions(b);
689 if (!pl)
690 return errno > 0 ? -errno : -ENOMEM;
691
692 errno = 0;
693 n = blkid_partlist_numof_partitions(pl);
694 if (n < 0)
695 return errno > 0 ? -errno : -EIO;
696
697 for (int i = 0; i < n; i++) {
698 blkid_partition pp;
699 sd_id128_t id = SD_ID128_NULL;
700 const char *sid;
701
702 errno = 0;
703 pp = blkid_partlist_get_partition(pl, i);
704 if (!pp)
705 return errno > 0 ? -errno : -EIO;
706
707 if (sd_id128_string_equal(blkid_partition_get_type_string(pp), SD_GPT_USER_HOME) <= 0)
708 continue;
709
710 if (!streq_ptr(blkid_partition_get_name(pp), label))
711 continue;
712
713 sid = blkid_partition_get_uuid(pp);
714 if (sid) {
715 r = sd_id128_from_string(sid, &id);
716 if (r < 0)
717 log_debug_errno(r, "Couldn't parse partition UUID %s, weird: %m", sid);
718
719 if (!sd_id128_is_null(partition_uuid) && !sd_id128_equal(id, partition_uuid))
720 continue;
721 }
722
723 if (found)
724 return -ENOPKG;
725
726 offset = blkid_partition_get_start(pp);
727 size = blkid_partition_get_size(pp);
728 found_partition_uuid = id;
729
730 found = true;
731 }
732
733 if (!found)
734 return -ENOPKG;
735
736 if (offset < 0)
737 return -EINVAL;
738 if ((uint64_t) offset > UINT64_MAX / 512U)
739 return -EINVAL;
740 if (size <= 0)
741 return -EINVAL;
742 if ((uint64_t) size > UINT64_MAX / 512U)
743 return -EINVAL;
744
745 *ret_offset = offset * 512U;
746 *ret_size = size * 512U;
747 *ret_partition_uuid = found_partition_uuid;
748
749 return 0;
750 }
751
752 static int crypt_device_to_evp_cipher(struct crypt_device *cd, const EVP_CIPHER **ret) {
753 _cleanup_free_ char *cipher_name = NULL;
754 const char *cipher, *cipher_mode, *e;
755 size_t key_size, key_bits;
756 const EVP_CIPHER *cc;
757 int r;
758
759 assert(cd);
760
761 /* Let's find the right OpenSSL EVP_CIPHER object that matches the encryption settings of the LUKS
762 * device */
763
764 cipher = sym_crypt_get_cipher(cd);
765 if (!cipher)
766 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot get cipher from LUKS device.");
767
768 cipher_mode = sym_crypt_get_cipher_mode(cd);
769 if (!cipher_mode)
770 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot get cipher mode from LUKS device.");
771
772 e = strchr(cipher_mode, '-');
773 if (e)
774 cipher_mode = strndupa_safe(cipher_mode, e - cipher_mode);
775
776 r = sym_crypt_get_volume_key_size(cd);
777 if (r <= 0)
778 return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), "Cannot get volume key size from LUKS device.");
779
780 key_size = r;
781 key_bits = key_size * 8;
782 if (streq(cipher_mode, "xts"))
783 key_bits /= 2;
784
785 if (asprintf(&cipher_name, "%s-%zu-%s", cipher, key_bits, cipher_mode) < 0)
786 return log_oom();
787
788 cc = EVP_get_cipherbyname(cipher_name);
789 if (!cc)
790 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Selected cipher mode '%s' not supported, can't encrypt JSON record.", cipher_name);
791
792 /* Verify that our key length calculations match what OpenSSL thinks */
793 r = EVP_CIPHER_key_length(cc);
794 if (r < 0 || (uint64_t) r != key_size)
795 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Key size of selected cipher doesn't meet our expectations.");
796
797 *ret = cc;
798 return 0;
799 }
800
801 static int luks_validate_home_record(
802 struct crypt_device *cd,
803 UserRecord *h,
804 const void *volume_key,
805 PasswordCache *cache,
806 UserRecord **ret_luks_home_record) {
807
808 int r;
809
810 assert(cd);
811 assert(h);
812
813 for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
814 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *rr = NULL;
815 _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL;
816 _cleanup_(user_record_unrefp) UserRecord *lhr = NULL;
817 _cleanup_free_ void *encrypted = NULL, *iv = NULL;
818 size_t decrypted_size, encrypted_size, iv_size;
819 int decrypted_size_out1, decrypted_size_out2;
820 _cleanup_free_ char *decrypted = NULL;
821 const char *text, *type;
822 crypt_token_info state;
823 JsonVariant *jr, *jiv;
824 unsigned line, column;
825 const EVP_CIPHER *cc;
826
827 state = sym_crypt_token_status(cd, token, &type);
828 if (state == CRYPT_TOKEN_INACTIVE) /* First unconfigured token, give up */
829 break;
830 if (IN_SET(state, CRYPT_TOKEN_INTERNAL, CRYPT_TOKEN_INTERNAL_UNKNOWN, CRYPT_TOKEN_EXTERNAL))
831 continue;
832 if (state != CRYPT_TOKEN_EXTERNAL_UNKNOWN)
833 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected token state of token %i: %i", token, (int) state);
834
835 if (!streq(type, "systemd-homed"))
836 continue;
837
838 r = sym_crypt_token_json_get(cd, token, &text);
839 if (r < 0)
840 return log_error_errno(r, "Failed to read LUKS token %i: %m", token);
841
842 r = json_parse(text, JSON_PARSE_SENSITIVE, &v, &line, &column);
843 if (r < 0)
844 return log_error_errno(r, "Failed to parse LUKS token JSON data %u:%u: %m", line, column);
845
846 jr = json_variant_by_key(v, "record");
847 if (!jr)
848 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "LUKS token lacks 'record' field.");
849 jiv = json_variant_by_key(v, "iv");
850 if (!jiv)
851 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "LUKS token lacks 'iv' field.");
852
853 r = json_variant_unbase64(jr, &encrypted, &encrypted_size);
854 if (r < 0)
855 return log_error_errno(r, "Failed to base64 decode record: %m");
856
857 r = json_variant_unbase64(jiv, &iv, &iv_size);
858 if (r < 0)
859 return log_error_errno(r, "Failed to base64 decode IV: %m");
860
861 r = crypt_device_to_evp_cipher(cd, &cc);
862 if (r < 0)
863 return r;
864 if (iv_size > INT_MAX || EVP_CIPHER_iv_length(cc) != (int) iv_size)
865 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "IV size doesn't match.");
866
867 context = EVP_CIPHER_CTX_new();
868 if (!context)
869 return log_oom();
870
871 if (EVP_DecryptInit_ex(context, cc, NULL, volume_key, iv) != 1)
872 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize decryption context.");
873
874 decrypted_size = encrypted_size + EVP_CIPHER_key_length(cc) * 2;
875 decrypted = new(char, decrypted_size);
876 if (!decrypted)
877 return log_oom();
878
879 if (EVP_DecryptUpdate(context, (uint8_t*) decrypted, &decrypted_size_out1, encrypted, encrypted_size) != 1)
880 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to decrypt JSON record.");
881
882 assert((size_t) decrypted_size_out1 <= decrypted_size);
883
884 if (EVP_DecryptFinal_ex(context, (uint8_t*) decrypted + decrypted_size_out1, &decrypted_size_out2) != 1)
885 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finish decryption of JSON record.");
886
887 assert((size_t) decrypted_size_out1 + (size_t) decrypted_size_out2 < decrypted_size);
888 decrypted_size = (size_t) decrypted_size_out1 + (size_t) decrypted_size_out2;
889
890 if (memchr(decrypted, 0, decrypted_size))
891 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Inner NUL byte in JSON record, refusing.");
892
893 decrypted[decrypted_size] = 0;
894
895 r = json_parse(decrypted, JSON_PARSE_SENSITIVE, &rr, NULL, NULL);
896 if (r < 0)
897 return log_error_errno(r, "Failed to parse decrypted JSON record, refusing.");
898
899 lhr = user_record_new();
900 if (!lhr)
901 return log_oom();
902
903 r = user_record_load(lhr, rr, USER_RECORD_LOAD_EMBEDDED|USER_RECORD_PERMISSIVE);
904 if (r < 0)
905 return log_error_errno(r, "Failed to parse user record: %m");
906
907 if (!user_record_compatible(h, lhr))
908 return log_error_errno(SYNTHETIC_ERRNO(EREMCHG), "LUKS home record not compatible with host record, refusing.");
909
910 r = user_record_authenticate(lhr, h, cache, /* strict_verify= */ true);
911 if (r < 0)
912 return r;
913 assert(r > 0); /* Insist that a password was verified */
914
915 *ret_luks_home_record = TAKE_PTR(lhr);
916 return 0;
917 }
918
919 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Couldn't find home record in LUKS2 header, refusing.");
920 }
921
922 static int format_luks_token_text(
923 struct crypt_device *cd,
924 UserRecord *hr,
925 const void *volume_key,
926 char **ret) {
927
928 int r, encrypted_size_out1 = 0, encrypted_size_out2 = 0, iv_size, key_size;
929 _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL;
930 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
931 _cleanup_free_ void *iv = NULL, *encrypted = NULL;
932 size_t text_length, encrypted_size;
933 _cleanup_free_ char *text = NULL;
934 const EVP_CIPHER *cc;
935
936 assert(cd);
937 assert(hr);
938 assert(volume_key);
939 assert(ret);
940
941 r = crypt_device_to_evp_cipher(cd, &cc);
942 if (r < 0)
943 return r;
944
945 key_size = EVP_CIPHER_key_length(cc);
946 iv_size = EVP_CIPHER_iv_length(cc);
947
948 if (iv_size > 0) {
949 iv = malloc(iv_size);
950 if (!iv)
951 return log_oom();
952
953 r = crypto_random_bytes(iv, iv_size);
954 if (r < 0)
955 return log_error_errno(r, "Failed to generate IV: %m");
956 }
957
958 context = EVP_CIPHER_CTX_new();
959 if (!context)
960 return log_oom();
961
962 if (EVP_EncryptInit_ex(context, cc, NULL, volume_key, iv) != 1)
963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize encryption context.");
964
965 r = json_variant_format(hr->json, 0, &text);
966 if (r < 0)
967 return log_error_errno(r, "Failed to format user record for LUKS: %m");
968
969 text_length = strlen(text);
970 encrypted_size = text_length + 2*key_size - 1;
971
972 encrypted = malloc(encrypted_size);
973 if (!encrypted)
974 return log_oom();
975
976 if (EVP_EncryptUpdate(context, encrypted, &encrypted_size_out1, (uint8_t*) text, text_length) != 1)
977 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to encrypt JSON record.");
978
979 assert((size_t) encrypted_size_out1 <= encrypted_size);
980
981 if (EVP_EncryptFinal_ex(context, (uint8_t*) encrypted + encrypted_size_out1, &encrypted_size_out2) != 1)
982 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finish encryption of JSON record. ");
983
984 assert((size_t) encrypted_size_out1 + (size_t) encrypted_size_out2 <= encrypted_size);
985
986 r = json_build(&v,
987 JSON_BUILD_OBJECT(
988 JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-homed")),
989 JSON_BUILD_PAIR("keyslots", JSON_BUILD_EMPTY_ARRAY),
990 JSON_BUILD_PAIR("record", JSON_BUILD_BASE64(encrypted, encrypted_size_out1 + encrypted_size_out2)),
991 JSON_BUILD_PAIR("iv", JSON_BUILD_BASE64(iv, iv_size))));
992 if (r < 0)
993 return log_error_errno(r, "Failed to prepare LUKS JSON token object: %m");
994
995 r = json_variant_format(v, 0, ret);
996 if (r < 0)
997 return log_error_errno(r, "Failed to format encrypted user record for LUKS: %m");
998
999 return 0;
1000 }
1001
1002 int home_store_header_identity_luks(
1003 UserRecord *h,
1004 HomeSetup *setup,
1005 UserRecord *old_home) {
1006
1007 _cleanup_(user_record_unrefp) UserRecord *header_home = NULL;
1008 _cleanup_free_ char *text = NULL;
1009 int r;
1010
1011 assert(h);
1012
1013 if (!setup->crypt_device)
1014 return 0;
1015
1016 assert(setup->volume_key);
1017
1018 /* Let's store the user's identity record in the LUKS2 "token" header data fields, in an encrypted
1019 * fashion. Why that? If we'd rely on the record being embedded in the payload file system itself we
1020 * would have to mount the file system before we can validate the JSON record, its signatures and
1021 * whether it matches what we are looking for. However, kernel file system implementations are
1022 * generally not ready to be used on untrusted media. Hence let's store the record independently of
1023 * the file system, so that we can validate it first, and only then mount the file system. To keep
1024 * things simple we use the same encryption settings for this record as for the file system itself. */
1025
1026 r = user_record_clone(h, USER_RECORD_EXTRACT_EMBEDDED|USER_RECORD_PERMISSIVE, &header_home);
1027 if (r < 0)
1028 return log_error_errno(r, "Failed to determine new header record: %m");
1029
1030 if (old_home && user_record_equal(old_home, header_home)) {
1031 log_debug("Not updating header home record.");
1032 return 0;
1033 }
1034
1035 r = format_luks_token_text(setup->crypt_device, header_home, setup->volume_key, &text);
1036 if (r < 0)
1037 return r;
1038
1039 for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
1040 crypt_token_info state;
1041 const char *type;
1042
1043 state = sym_crypt_token_status(setup->crypt_device, token, &type);
1044 if (state == CRYPT_TOKEN_INACTIVE) /* First unconfigured token, we are done */
1045 break;
1046 if (IN_SET(state, CRYPT_TOKEN_INTERNAL, CRYPT_TOKEN_INTERNAL_UNKNOWN, CRYPT_TOKEN_EXTERNAL))
1047 continue; /* Not ours */
1048 if (state != CRYPT_TOKEN_EXTERNAL_UNKNOWN)
1049 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected token state of token %i: %i", token, (int) state);
1050
1051 if (!streq(type, "systemd-homed"))
1052 continue;
1053
1054 r = sym_crypt_token_json_set(setup->crypt_device, token, text);
1055 if (r < 0)
1056 return log_error_errno(r, "Failed to set JSON token for slot %i: %m", token);
1057
1058 /* Now, let's free the text so that for all further matching tokens we all crypt_json_token_set()
1059 * with a NULL text in order to invalidate the tokens. */
1060 text = mfree(text);
1061 }
1062
1063 if (text)
1064 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Didn't find any record token to update.");
1065
1066 log_info("Wrote LUKS header user record.");
1067
1068 return 1;
1069 }
1070
1071 int run_fitrim(int root_fd) {
1072 struct fstrim_range range = {
1073 .len = UINT64_MAX,
1074 };
1075
1076 /* If discarding is on, discard everything right after mounting, so that the discard setting takes
1077 * effect on activation. (Also, optionally, trim on logout) */
1078
1079 assert(root_fd >= 0);
1080
1081 if (ioctl(root_fd, FITRIM, &range) < 0) {
1082 if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EBADF) {
1083 log_debug_errno(errno, "File system does not support FITRIM, not trimming.");
1084 return 0;
1085 }
1086
1087 return log_warning_errno(errno, "Failed to invoke FITRIM, ignoring: %m");
1088 }
1089
1090 log_info("Discarded unused %s.", FORMAT_BYTES(range.len));
1091 return 1;
1092 }
1093
1094 int run_fallocate(int backing_fd, const struct stat *st) {
1095 struct stat stbuf;
1096
1097 assert(backing_fd >= 0);
1098
1099 /* If discarding is off, let's allocate the whole image before mounting, so that the setting takes
1100 * effect on activation */
1101
1102 if (!st) {
1103 if (fstat(backing_fd, &stbuf) < 0)
1104 return log_error_errno(errno, "Failed to fstat(): %m");
1105
1106 st = &stbuf;
1107 }
1108
1109 if (!S_ISREG(st->st_mode))
1110 return 0;
1111
1112 if (st->st_blocks >= DIV_ROUND_UP(st->st_size, 512)) {
1113 log_info("Backing file is fully allocated already.");
1114 return 0;
1115 }
1116
1117 if (fallocate(backing_fd, FALLOC_FL_KEEP_SIZE, 0, st->st_size) < 0) {
1118
1119 if (ERRNO_IS_NOT_SUPPORTED(errno)) {
1120 log_debug_errno(errno, "fallocate() not supported on file system, ignoring.");
1121 return 0;
1122 }
1123
1124 if (ERRNO_IS_DISK_SPACE(errno)) {
1125 log_debug_errno(errno, "Not enough disk space to fully allocate home.");
1126 return -ENOSPC; /* make recognizable */
1127 }
1128
1129 return log_error_errno(errno, "Failed to allocate backing file blocks: %m");
1130 }
1131
1132 log_info("Allocated additional %s.",
1133 FORMAT_BYTES((DIV_ROUND_UP(st->st_size, 512) - st->st_blocks) * 512));
1134 return 1;
1135 }
1136
1137 int run_fallocate_by_path(const char *backing_path) {
1138 _cleanup_close_ int backing_fd = -1;
1139
1140 backing_fd = open(backing_path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
1141 if (backing_fd < 0)
1142 return log_error_errno(errno, "Failed to open '%s' for fallocate(): %m", backing_path);
1143
1144 return run_fallocate(backing_fd, NULL);
1145 }
1146
1147 static int lock_image_fd(int image_fd, const char *ip) {
1148 int r;
1149
1150 /* If the $SYSTEMD_LUKS_LOCK environment variable is set we'll take an exclusive BSD lock on the
1151 * image file, and send it to our parent. homed will keep it open to ensure no other instance of
1152 * homed (across the network or such) will also mount the file. */
1153
1154 assert(image_fd >= 0);
1155 assert(ip);
1156
1157 r = getenv_bool("SYSTEMD_LUKS_LOCK");
1158 if (r == -ENXIO)
1159 return 0;
1160 if (r < 0)
1161 return log_error_errno(r, "Failed to parse $SYSTEMD_LUKS_LOCK environment variable: %m");
1162 if (r == 0)
1163 return 0;
1164
1165 if (flock(image_fd, LOCK_EX|LOCK_NB) < 0) {
1166
1167 if (errno == EAGAIN)
1168 log_error_errno(errno, "Image file '%s' already locked, can't use.", ip);
1169 else
1170 log_error_errno(errno, "Failed to lock image file '%s': %m", ip);
1171
1172 return errno != EAGAIN ? -errno : -EADDRINUSE; /* Make error recognizable */
1173 }
1174
1175 log_info("Successfully locked image file '%s'.", ip);
1176
1177 /* Now send it to our parent to keep safe while the home dir is active */
1178 r = sd_pid_notify_with_fds(0, false, "SYSTEMD_LUKS_LOCK_FD=1", &image_fd, 1);
1179 if (r < 0)
1180 log_warning_errno(r, "Failed to send LUKS lock fd to parent, ignoring: %m");
1181
1182 return 0;
1183 }
1184
1185 static int open_image_file(
1186 UserRecord *h,
1187 const char *force_image_path,
1188 struct stat *ret_stat) {
1189
1190 _cleanup_close_ int image_fd = -1;
1191 struct stat st;
1192 const char *ip;
1193 int r;
1194
1195 assert(h || force_image_path);
1196
1197 ip = force_image_path ?: user_record_image_path(h);
1198
1199 image_fd = open(ip, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
1200 if (image_fd < 0)
1201 return log_error_errno(errno, "Failed to open image file %s: %m", ip);
1202
1203 if (fstat(image_fd, &st) < 0)
1204 return log_error_errno(errno, "Failed to fstat() image file: %m");
1205 if (!S_ISREG(st.st_mode) && !S_ISBLK(st.st_mode))
1206 return log_error_errno(
1207 S_ISDIR(st.st_mode) ? SYNTHETIC_ERRNO(EISDIR) : SYNTHETIC_ERRNO(EBADFD),
1208 "Image file %s is not a regular file or block device: %m", ip);
1209
1210 /* Locking block devices doesn't really make sense, as this might interfere with
1211 * udev's workings, and these locks aren't network propagated anyway, hence not what
1212 * we are after here. */
1213 if (S_ISREG(st.st_mode)) {
1214 r = lock_image_fd(image_fd, ip);
1215 if (r < 0)
1216 return r;
1217 }
1218
1219 if (ret_stat)
1220 *ret_stat = st;
1221
1222 return TAKE_FD(image_fd);
1223 }
1224
1225 int home_setup_luks(
1226 UserRecord *h,
1227 HomeSetupFlags flags,
1228 const char *force_image_path,
1229 HomeSetup *setup,
1230 PasswordCache *cache,
1231 UserRecord **ret_luks_home) {
1232
1233 sd_id128_t found_partition_uuid, found_fs_uuid, found_luks_uuid = SD_ID128_NULL;
1234 _cleanup_(user_record_unrefp) UserRecord *luks_home = NULL;
1235 _cleanup_(erase_and_freep) void *volume_key = NULL;
1236 size_t volume_key_size = 0;
1237 uint64_t offset, size;
1238 struct stat st;
1239 int r;
1240
1241 assert(h);
1242 assert(setup);
1243 assert(user_record_storage(h) == USER_LUKS);
1244
1245 r = dlopen_cryptsetup();
1246 if (r < 0)
1247 return r;
1248
1249 r = make_dm_names(h, setup);
1250 if (r < 0)
1251 return r;
1252
1253 /* Reuse the image fd if it has already been opened by an earlier step */
1254 if (setup->image_fd < 0) {
1255 setup->image_fd = open_image_file(h, force_image_path, &st);
1256 if (setup->image_fd < 0)
1257 return setup->image_fd;
1258 } else if (fstat(setup->image_fd, &st) < 0)
1259 return log_error_errno(errno, "Failed to stat image: %m");
1260
1261 if (FLAGS_SET(flags, HOME_SETUP_ALREADY_ACTIVATED)) {
1262 struct loop_info64 info;
1263 const char *n;
1264
1265 if (!setup->crypt_device) {
1266 r = luks_open(h,
1267 setup,
1268 cache,
1269 &found_luks_uuid,
1270 &volume_key,
1271 &volume_key_size);
1272 if (r < 0)
1273 return r;
1274 }
1275
1276 if (ret_luks_home) {
1277 r = luks_validate_home_record(setup->crypt_device, h, volume_key, cache, &luks_home);
1278 if (r < 0)
1279 return r;
1280 }
1281
1282 n = sym_crypt_get_device_name(setup->crypt_device);
1283 if (!n)
1284 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine backing device for DM %s.", setup->dm_name);
1285
1286 if (!setup->loop) {
1287 r = loop_device_open_from_path(n, O_RDWR, LOCK_UN, &setup->loop);
1288 if (r < 0)
1289 return log_error_errno(r, "Failed to open loopback device %s: %m", n);
1290 }
1291
1292 if (ioctl(setup->loop->fd, LOOP_GET_STATUS64, &info) < 0) {
1293 _cleanup_free_ char *sysfs = NULL;
1294
1295 if (!IN_SET(errno, ENOTTY, EINVAL))
1296 return log_error_errno(errno, "Failed to get block device metrics of %s: %m", n);
1297
1298 if (ioctl(setup->loop->fd, BLKGETSIZE64, &size) < 0)
1299 return log_error_errno(r, "Failed to read block device size of %s: %m", n);
1300
1301 if (fstat(setup->loop->fd, &st) < 0)
1302 return log_error_errno(r, "Failed to stat block device %s: %m", n);
1303 assert(S_ISBLK(st.st_mode));
1304
1305 if (asprintf(&sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/partition", DEVNUM_FORMAT_VAL(st.st_rdev)) < 0)
1306 return log_oom();
1307
1308 if (access(sysfs, F_OK) < 0) {
1309 if (errno != ENOENT)
1310 return log_error_errno(errno, "Failed to determine whether %s exists: %m", sysfs);
1311
1312 offset = 0;
1313 } else {
1314 _cleanup_free_ char *buffer = NULL;
1315
1316 if (asprintf(&sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/start", DEVNUM_FORMAT_VAL(st.st_rdev)) < 0)
1317 return log_oom();
1318
1319 r = read_one_line_file(sysfs, &buffer);
1320 if (r < 0)
1321 return log_error_errno(r, "Failed to read partition start offset: %m");
1322
1323 r = safe_atou64(buffer, &offset);
1324 if (r < 0)
1325 return log_error_errno(r, "Failed to parse partition start offset: %m");
1326
1327 if (offset > UINT64_MAX / 512U)
1328 return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "Offset too large for 64 byte range, refusing.");
1329
1330 offset *= 512U;
1331 }
1332 } else {
1333 #if HAVE_VALGRIND_MEMCHECK_H
1334 VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
1335 #endif
1336
1337 offset = info.lo_offset;
1338 size = info.lo_sizelimit;
1339 }
1340
1341 found_partition_uuid = found_fs_uuid = SD_ID128_NULL;
1342
1343 log_info("Discovered used loopback device %s.", setup->loop->node);
1344
1345 if (setup->root_fd < 0) {
1346 setup->root_fd = open(user_record_home_directory(h), O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1347 if (setup->root_fd < 0)
1348 return log_error_errno(errno, "Failed to open home directory: %m");
1349 }
1350 } else {
1351 _cleanup_free_ char *fstype = NULL, *subdir = NULL;
1352 const char *ip;
1353
1354 /* When we aren't reopening the home directory we are allocating it fresh, hence the relevant
1355 * objects can't be allocated yet. */
1356 assert(setup->root_fd < 0);
1357 assert(!setup->crypt_device);
1358 assert(!setup->loop);
1359
1360 ip = force_image_path ?: user_record_image_path(h);
1361
1362 subdir = path_join(HOME_RUNTIME_WORK_DIR, user_record_user_name_and_realm(h));
1363 if (!subdir)
1364 return log_oom();
1365
1366 r = luks_validate(setup->image_fd, user_record_user_name_and_realm(h), h->partition_uuid, &found_partition_uuid, &offset, &size);
1367 if (r < 0)
1368 return log_error_errno(r, "Failed to validate disk label: %m");
1369
1370 /* Everything before this point left the image untouched. We are now starting to make
1371 * changes, hence mark the image dirty */
1372 if (run_mark_dirty(setup->image_fd, true) > 0)
1373 setup->do_mark_clean = true;
1374
1375 if (!user_record_luks_discard(h)) {
1376 r = run_fallocate(setup->image_fd, &st);
1377 if (r < 0)
1378 return r;
1379 }
1380
1381 r = loop_device_make(setup->image_fd, O_RDWR, offset, size, user_record_luks_sector_size(h), 0, LOCK_UN, &setup->loop);
1382 if (r == -ENOENT) {
1383 log_error_errno(r, "Loopback block device support is not available on this system.");
1384 return -ENOLINK; /* make recognizable */
1385 }
1386 if (r < 0)
1387 return log_error_errno(r, "Failed to allocate loopback context: %m");
1388
1389 log_info("Setting up loopback device %s completed.", setup->loop->node ?: ip);
1390
1391 r = luks_setup(h,
1392 setup->loop->node ?: ip,
1393 setup->dm_name,
1394 h->luks_uuid,
1395 h->luks_cipher,
1396 h->luks_cipher_mode,
1397 h->luks_volume_key_size,
1398 h->password,
1399 cache,
1400 user_record_luks_discard(h) || user_record_luks_offline_discard(h),
1401 &setup->crypt_device,
1402 &found_luks_uuid,
1403 &volume_key,
1404 &volume_key_size,
1405 &setup->key_serial);
1406 if (r < 0)
1407 return r;
1408
1409 setup->undo_dm = true;
1410
1411 if (ret_luks_home) {
1412 r = luks_validate_home_record(setup->crypt_device, h, volume_key, cache, &luks_home);
1413 if (r < 0)
1414 return r;
1415 }
1416
1417 r = fs_validate(setup->dm_node, h->file_system_uuid, &fstype, &found_fs_uuid);
1418 if (r < 0)
1419 return r;
1420
1421 r = run_fsck(setup->dm_node, fstype);
1422 if (r < 0)
1423 return r;
1424
1425 r = home_unshare_and_mount(setup->dm_node, fstype, user_record_luks_discard(h), user_record_mount_flags(h), h->luks_extra_mount_options);
1426 if (r < 0)
1427 return r;
1428
1429 setup->undo_mount = true;
1430
1431 setup->root_fd = open(subdir, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1432 if (setup->root_fd < 0)
1433 return log_error_errno(errno, "Failed to open home directory: %m");
1434
1435 if (user_record_luks_discard(h))
1436 (void) run_fitrim(setup->root_fd);
1437
1438 setup->do_offline_fallocate = !(setup->do_offline_fitrim = user_record_luks_offline_discard(h));
1439 }
1440
1441 if (!sd_id128_is_null(found_partition_uuid))
1442 setup->found_partition_uuid = found_partition_uuid;
1443 if (!sd_id128_is_null(found_luks_uuid))
1444 setup->found_luks_uuid = found_luks_uuid;
1445 if (!sd_id128_is_null(found_fs_uuid))
1446 setup->found_fs_uuid = found_fs_uuid;
1447
1448 setup->partition_offset = offset;
1449 setup->partition_size = size;
1450
1451 if (volume_key) {
1452 erase_and_free(setup->volume_key);
1453 setup->volume_key = TAKE_PTR(volume_key);
1454 setup->volume_key_size = volume_key_size;
1455 }
1456
1457 if (ret_luks_home)
1458 *ret_luks_home = TAKE_PTR(luks_home);
1459
1460 return 0;
1461 }
1462
1463 static void print_size_summary(uint64_t host_size, uint64_t encrypted_size, const struct statfs *sfs) {
1464 assert(sfs);
1465
1466 log_info("Image size is %s, file system size is %s, file system payload size is %s, file system free is %s.",
1467 FORMAT_BYTES(host_size),
1468 FORMAT_BYTES(encrypted_size),
1469 FORMAT_BYTES((uint64_t) sfs->f_blocks * (uint64_t) sfs->f_frsize),
1470 FORMAT_BYTES((uint64_t) sfs->f_bfree * (uint64_t) sfs->f_frsize));
1471 }
1472
1473 static int home_auto_grow_luks(
1474 UserRecord *h,
1475 HomeSetup *setup,
1476 PasswordCache *cache) {
1477
1478 struct statfs sfs;
1479
1480 assert(h);
1481 assert(setup);
1482
1483 if (!IN_SET(user_record_auto_resize_mode(h), AUTO_RESIZE_GROW, AUTO_RESIZE_SHRINK_AND_GROW))
1484 return 0;
1485
1486 assert(setup->root_fd >= 0);
1487
1488 if (fstatfs(setup->root_fd, &sfs) < 0)
1489 return log_error_errno(errno, "Failed to statfs home directory: %m");
1490
1491 if (!fs_can_online_shrink_and_grow(sfs.f_type)) {
1492 log_debug("Not auto-grow file system, since selected file system cannot do both online shrink and grow.");
1493 return 0;
1494 }
1495
1496 log_debug("Initiating auto-grow...");
1497
1498 return home_resize_luks(
1499 h,
1500 HOME_SETUP_ALREADY_ACTIVATED|
1501 HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES|
1502 HOME_SETUP_RESIZE_DONT_SHRINK|
1503 HOME_SETUP_RESIZE_DONT_UNDO,
1504 setup,
1505 cache,
1506 NULL);
1507 }
1508
1509 int home_activate_luks(
1510 UserRecord *h,
1511 HomeSetupFlags flags,
1512 HomeSetup *setup,
1513 PasswordCache *cache,
1514 UserRecord **ret_home) {
1515
1516 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL, *luks_home_record = NULL;
1517 uint64_t host_size, encrypted_size;
1518 const char *hdo, *hd;
1519 struct statfs sfs;
1520 int r;
1521
1522 assert(h);
1523 assert(user_record_storage(h) == USER_LUKS);
1524 assert(setup);
1525 assert(ret_home);
1526
1527 r = dlopen_cryptsetup();
1528 if (r < 0)
1529 return r;
1530
1531 assert_se(hdo = user_record_home_directory(h));
1532 hd = strdupa_safe(hdo); /* copy the string out, since it might change later in the home record object */
1533
1534 r = home_get_state_luks(h, setup);
1535 if (r < 0)
1536 return r;
1537 if (r > 0)
1538 return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Device mapper device %s already exists, refusing.", setup->dm_node);
1539
1540 r = home_setup_luks(
1541 h,
1542 0,
1543 NULL,
1544 setup,
1545 cache,
1546 &luks_home_record);
1547 if (r < 0)
1548 return r;
1549
1550 r = home_auto_grow_luks(h, setup, cache);
1551 if (r < 0)
1552 return r;
1553
1554 r = block_get_size_by_fd(setup->loop->fd, &host_size);
1555 if (r < 0)
1556 return log_error_errno(r, "Failed to get loopback block device size: %m");
1557
1558 r = block_get_size_by_path(setup->dm_node, &encrypted_size);
1559 if (r < 0)
1560 return log_error_errno(r, "Failed to get LUKS block device size: %m");
1561
1562 r = home_refresh(
1563 h,
1564 flags,
1565 setup,
1566 luks_home_record,
1567 cache,
1568 &sfs,
1569 &new_home);
1570 if (r < 0)
1571 return r;
1572
1573 r = home_extend_embedded_identity(new_home, h, setup);
1574 if (r < 0)
1575 return r;
1576
1577 setup->root_fd = safe_close(setup->root_fd);
1578
1579 r = home_move_mount(user_record_user_name_and_realm(h), hd);
1580 if (r < 0)
1581 return r;
1582
1583 setup->undo_mount = false;
1584 setup->do_offline_fitrim = false;
1585
1586 loop_device_relinquish(setup->loop);
1587
1588 r = sym_crypt_deactivate_by_name(NULL, setup->dm_name, CRYPT_DEACTIVATE_DEFERRED);
1589 if (r < 0)
1590 log_warning_errno(r, "Failed to relinquish DM device, ignoring: %m");
1591
1592 setup->undo_dm = false;
1593 setup->do_offline_fallocate = false;
1594 setup->do_mark_clean = false;
1595 setup->do_drop_caches = false;
1596 TAKE_KEY_SERIAL(setup->key_serial); /* Leave key in kernel keyring */
1597
1598 log_info("Activation completed.");
1599
1600 print_size_summary(host_size, encrypted_size, &sfs);
1601
1602 *ret_home = TAKE_PTR(new_home);
1603 return 1;
1604 }
1605
1606 int home_deactivate_luks(UserRecord *h, HomeSetup *setup) {
1607 bool we_detached = false;
1608 int r;
1609
1610 assert(h);
1611 assert(setup);
1612
1613 /* Note that the DM device and loopback device are set to auto-detach, hence strictly speaking we
1614 * don't have to explicitly have to detach them. However, we do that nonetheless (in case of the DM
1615 * device), to avoid races: by explicitly detaching them we know when the detaching is complete. We
1616 * don't bother about the loopback device because unlike the DM device it doesn't have a fixed
1617 * name. */
1618
1619 if (!setup->crypt_device) {
1620 r = acquire_open_luks_device(h, setup, /* graceful= */ true);
1621 if (r < 0)
1622 return log_error_errno(r, "Failed to initialize cryptsetup context for %s: %m", setup->dm_name);
1623 if (r == 0)
1624 log_debug("LUKS device %s has already been detached.", setup->dm_name);
1625 }
1626
1627 if (setup->crypt_device) {
1628 log_info("Discovered used LUKS device %s.", setup->dm_node);
1629
1630 cryptsetup_enable_logging(setup->crypt_device);
1631
1632 r = sym_crypt_deactivate_by_name(setup->crypt_device, setup->dm_name, 0);
1633 if (ERRNO_IS_DEVICE_ABSENT(r) || r == -EINVAL)
1634 log_debug_errno(r, "LUKS device %s is already detached.", setup->dm_node);
1635 else if (r < 0)
1636 return log_info_errno(r, "LUKS device %s couldn't be deactivated: %m", setup->dm_node);
1637 else {
1638 log_info("LUKS device detaching completed.");
1639 we_detached = true;
1640 }
1641 }
1642
1643 (void) wait_for_block_device_gone(setup, USEC_PER_SEC * 30);
1644 setup->undo_dm = false;
1645
1646 if (user_record_luks_offline_discard(h))
1647 log_debug("Not allocating on logout.");
1648 else
1649 (void) run_fallocate_by_path(user_record_image_path(h));
1650
1651 run_mark_dirty_by_path(user_record_image_path(h), false);
1652 return we_detached;
1653 }
1654
1655 int home_trim_luks(UserRecord *h, HomeSetup *setup) {
1656 assert(h);
1657 assert(setup);
1658 assert(setup->root_fd >= 0);
1659
1660 if (!user_record_luks_offline_discard(h)) {
1661 log_debug("Not trimming on logout.");
1662 return 0;
1663 }
1664
1665 (void) run_fitrim(setup->root_fd);
1666 return 0;
1667 }
1668
1669 static struct crypt_pbkdf_type* build_good_pbkdf(struct crypt_pbkdf_type *buffer, UserRecord *hr) {
1670 assert(buffer);
1671 assert(hr);
1672
1673 *buffer = (struct crypt_pbkdf_type) {
1674 .hash = user_record_luks_pbkdf_hash_algorithm(hr),
1675 .type = user_record_luks_pbkdf_type(hr),
1676 .time_ms = user_record_luks_pbkdf_time_cost_usec(hr) / USEC_PER_MSEC,
1677 .max_memory_kb = user_record_luks_pbkdf_memory_cost(hr) / 1024,
1678 .parallel_threads = user_record_luks_pbkdf_parallel_threads(hr),
1679 };
1680
1681 return buffer;
1682 }
1683
1684 static struct crypt_pbkdf_type* build_minimal_pbkdf(struct crypt_pbkdf_type *buffer, UserRecord *hr) {
1685 assert(buffer);
1686 assert(hr);
1687
1688 /* For PKCS#11 derived keys (which are generated randomly and are of high quality already) we use a
1689 * minimal PBKDF */
1690 *buffer = (struct crypt_pbkdf_type) {
1691 .hash = user_record_luks_pbkdf_hash_algorithm(hr),
1692 .type = CRYPT_KDF_PBKDF2,
1693 .iterations = 1,
1694 .time_ms = 1,
1695 };
1696
1697 return buffer;
1698 }
1699
1700 static int luks_format(
1701 const char *node,
1702 const char *dm_name,
1703 sd_id128_t uuid,
1704 const char *label,
1705 const PasswordCache *cache,
1706 char **effective_passwords,
1707 bool discard,
1708 UserRecord *hr,
1709 struct crypt_device **ret) {
1710
1711 _cleanup_(user_record_unrefp) UserRecord *reduced = NULL;
1712 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
1713 _cleanup_(erase_and_freep) void *volume_key = NULL;
1714 struct crypt_pbkdf_type good_pbkdf, minimal_pbkdf;
1715 _cleanup_free_ char *text = NULL;
1716 size_t volume_key_size;
1717 int slot = 0, r;
1718
1719 assert(node);
1720 assert(dm_name);
1721 assert(hr);
1722 assert(ret);
1723
1724 r = sym_crypt_init(&cd, node);
1725 if (r < 0)
1726 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
1727
1728 cryptsetup_enable_logging(cd);
1729
1730 /* Normally we'd, just leave volume key generation to libcryptsetup. However, we can't, since we
1731 * can't extract the volume key from the library again, but we need it in order to encrypt the JSON
1732 * record. Hence, let's generate it on our own, so that we can keep track of it. */
1733
1734 volume_key_size = user_record_luks_volume_key_size(hr);
1735 volume_key = malloc(volume_key_size);
1736 if (!volume_key)
1737 return log_oom();
1738
1739 r = crypto_random_bytes(volume_key, volume_key_size);
1740 if (r < 0)
1741 return log_error_errno(r, "Failed to generate volume key: %m");
1742
1743 #if HAVE_CRYPT_SET_METADATA_SIZE
1744 /* Increase the metadata space to 4M, the largest LUKS2 supports */
1745 r = sym_crypt_set_metadata_size(cd, 4096U*1024U, 0);
1746 if (r < 0)
1747 return log_error_errno(r, "Failed to change LUKS2 metadata size: %m");
1748 #endif
1749
1750 build_good_pbkdf(&good_pbkdf, hr);
1751 build_minimal_pbkdf(&minimal_pbkdf, hr);
1752
1753 r = sym_crypt_format(
1754 cd,
1755 CRYPT_LUKS2,
1756 user_record_luks_cipher(hr),
1757 user_record_luks_cipher_mode(hr),
1758 SD_ID128_TO_UUID_STRING(uuid),
1759 volume_key,
1760 volume_key_size,
1761 &(struct crypt_params_luks2) {
1762 .label = label,
1763 .subsystem = "systemd-home",
1764 .sector_size = user_record_luks_sector_size(hr),
1765 .pbkdf = &good_pbkdf,
1766 });
1767 if (r < 0)
1768 return log_error_errno(r, "Failed to format LUKS image: %m");
1769
1770 log_info("LUKS formatting completed.");
1771
1772 STRV_FOREACH(pp, effective_passwords) {
1773
1774 if (password_cache_contains(cache, *pp)) { /* is this a fido2 or pkcs11 password? */
1775 log_debug("Using minimal PBKDF for slot %i", slot);
1776 r = sym_crypt_set_pbkdf_type(cd, &minimal_pbkdf);
1777 } else {
1778 log_debug("Using good PBKDF for slot %i", slot);
1779 r = sym_crypt_set_pbkdf_type(cd, &good_pbkdf);
1780 }
1781 if (r < 0)
1782 return log_error_errno(r, "Failed to tweak PBKDF for slot %i: %m", slot);
1783
1784 r = sym_crypt_keyslot_add_by_volume_key(
1785 cd,
1786 slot,
1787 volume_key,
1788 volume_key_size,
1789 *pp,
1790 strlen(*pp));
1791 if (r < 0)
1792 return log_error_errno(r, "Failed to set up LUKS password for slot %i: %m", slot);
1793
1794 log_info("Writing password to LUKS keyslot %i completed.", slot);
1795 slot++;
1796 }
1797
1798 r = sym_crypt_activate_by_volume_key(
1799 cd,
1800 dm_name,
1801 volume_key,
1802 volume_key_size,
1803 discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0);
1804 if (r < 0)
1805 return log_error_errno(r, "Failed to activate LUKS superblock: %m");
1806
1807 log_info("LUKS activation by volume key succeeded.");
1808
1809 r = user_record_clone(hr, USER_RECORD_EXTRACT_EMBEDDED|USER_RECORD_PERMISSIVE, &reduced);
1810 if (r < 0)
1811 return log_error_errno(r, "Failed to prepare home record for LUKS: %m");
1812
1813 r = format_luks_token_text(cd, reduced, volume_key, &text);
1814 if (r < 0)
1815 return r;
1816
1817 r = sym_crypt_token_json_set(cd, CRYPT_ANY_TOKEN, text);
1818 if (r < 0)
1819 return log_error_errno(r, "Failed to set LUKS JSON token: %m");
1820
1821 log_info("Writing user record as LUKS token completed.");
1822
1823 if (ret)
1824 *ret = TAKE_PTR(cd);
1825
1826 return 0;
1827 }
1828
1829 static int make_partition_table(
1830 int fd,
1831 const char *label,
1832 sd_id128_t uuid,
1833 uint64_t *ret_offset,
1834 uint64_t *ret_size,
1835 sd_id128_t *ret_disk_uuid) {
1836
1837 _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *p = NULL, *q = NULL;
1838 _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
1839 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
1840 _cleanup_free_ char *path = NULL, *disk_uuid_as_string = NULL;
1841 uint64_t offset, size, first_lba, start, last_lba, end;
1842 sd_id128_t disk_uuid;
1843 int r;
1844
1845 assert(fd >= 0);
1846 assert(label);
1847 assert(ret_offset);
1848 assert(ret_size);
1849
1850 t = fdisk_new_parttype();
1851 if (!t)
1852 return log_oom();
1853
1854 r = fdisk_parttype_set_typestr(t, SD_GPT_USER_HOME_STR);
1855 if (r < 0)
1856 return log_error_errno(r, "Failed to initialize partition type: %m");
1857
1858 c = fdisk_new_context();
1859 if (!c)
1860 return log_oom();
1861
1862 if (asprintf(&path, "/proc/self/fd/%i", fd) < 0)
1863 return log_oom();
1864
1865 r = fdisk_assign_device(c, path, 0);
1866 if (r < 0)
1867 return log_error_errno(r, "Failed to open device: %m");
1868
1869 r = fdisk_create_disklabel(c, "gpt");
1870 if (r < 0)
1871 return log_error_errno(r, "Failed to create GPT disk label: %m");
1872
1873 p = fdisk_new_partition();
1874 if (!p)
1875 return log_oom();
1876
1877 r = fdisk_partition_set_type(p, t);
1878 if (r < 0)
1879 return log_error_errno(r, "Failed to set partition type: %m");
1880
1881 r = fdisk_partition_partno_follow_default(p, 1);
1882 if (r < 0)
1883 return log_error_errno(r, "Failed to place partition at first free partition index: %m");
1884
1885 first_lba = fdisk_get_first_lba(c); /* Boundary where usable space starts */
1886 assert(first_lba <= UINT64_MAX/512);
1887 start = DISK_SIZE_ROUND_UP(first_lba * 512); /* Round up to multiple of 4K */
1888
1889 log_debug("Starting partition at offset %" PRIu64, start);
1890
1891 if (start == UINT64_MAX)
1892 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Overflow while rounding up start LBA.");
1893
1894 last_lba = fdisk_get_last_lba(c); /* One sector before boundary where usable space ends */
1895 assert(last_lba < UINT64_MAX/512);
1896 end = DISK_SIZE_ROUND_DOWN((last_lba + 1) * 512); /* Round down to multiple of 4K */
1897
1898 if (end <= start)
1899 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Resulting partition size zero or negative.");
1900
1901 r = fdisk_partition_set_start(p, start / 512);
1902 if (r < 0)
1903 return log_error_errno(r, "Failed to place partition at offset %" PRIu64 ": %m", start);
1904
1905 r = fdisk_partition_set_size(p, (end - start) / 512);
1906 if (r < 0)
1907 return log_error_errno(r, "Failed to end partition at offset %" PRIu64 ": %m", end);
1908
1909 r = fdisk_partition_set_name(p, label);
1910 if (r < 0)
1911 return log_error_errno(r, "Failed to set partition name: %m");
1912
1913 r = fdisk_partition_set_uuid(p, SD_ID128_TO_UUID_STRING(uuid));
1914 if (r < 0)
1915 return log_error_errno(r, "Failed to set partition UUID: %m");
1916
1917 r = fdisk_add_partition(c, p, NULL);
1918 if (r < 0)
1919 return log_error_errno(r, "Failed to add partition: %m");
1920
1921 r = fdisk_write_disklabel(c);
1922 if (r < 0)
1923 return log_error_errno(r, "Failed to write disk label: %m");
1924
1925 r = fdisk_get_disklabel_id(c, &disk_uuid_as_string);
1926 if (r < 0)
1927 return log_error_errno(r, "Failed to determine disk label UUID: %m");
1928
1929 r = sd_id128_from_string(disk_uuid_as_string, &disk_uuid);
1930 if (r < 0)
1931 return log_error_errno(r, "Failed to parse disk label UUID: %m");
1932
1933 r = fdisk_get_partition(c, 0, &q);
1934 if (r < 0)
1935 return log_error_errno(r, "Failed to read created partition metadata: %m");
1936
1937 assert(fdisk_partition_has_start(q));
1938 offset = fdisk_partition_get_start(q);
1939 if (offset > UINT64_MAX / 512U)
1940 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Partition offset too large.");
1941
1942 assert(fdisk_partition_has_size(q));
1943 size = fdisk_partition_get_size(q);
1944 if (size > UINT64_MAX / 512U)
1945 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Partition size too large.");
1946
1947 *ret_offset = offset * 512U;
1948 *ret_size = size * 512U;
1949 *ret_disk_uuid = disk_uuid;
1950
1951 return 0;
1952 }
1953
1954 static bool supported_fs_size(const char *fstype, uint64_t host_size) {
1955 uint64_t m;
1956
1957 m = minimal_size_by_fs_name(fstype);
1958 if (m == UINT64_MAX)
1959 return false;
1960
1961 return host_size >= m;
1962 }
1963
1964 static int wait_for_devlink(const char *path) {
1965 _cleanup_close_ int inotify_fd = -1;
1966 usec_t until;
1967 int r;
1968
1969 /* let's wait for a device link to show up in /dev, with a timeout. This is good to do since we
1970 * return a /dev/disk/by-uuid/… link to our callers and they likely want to access it right-away,
1971 * hence let's wait until udev has caught up with our changes, and wait for the symlink to be
1972 * created. */
1973
1974 until = usec_add(now(CLOCK_MONOTONIC), 45 * USEC_PER_SEC);
1975
1976 for (;;) {
1977 _cleanup_free_ char *dn = NULL;
1978 usec_t w;
1979
1980 if (laccess(path, F_OK) < 0) {
1981 if (errno != ENOENT)
1982 return log_error_errno(errno, "Failed to determine whether %s exists: %m", path);
1983 } else
1984 return 0; /* Found it */
1985
1986 if (inotify_fd < 0) {
1987 /* We need to wait for the device symlink to show up, let's create an inotify watch for it */
1988 inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1989 if (inotify_fd < 0)
1990 return log_error_errno(errno, "Failed to allocate inotify fd: %m");
1991 }
1992
1993 r = path_extract_directory(path, &dn);
1994 if (r < 0)
1995 return log_error_errno(r, "Failed to extract directory from device node path '%s': %m", path);
1996 for (;;) {
1997 _cleanup_free_ char *ndn = NULL;
1998
1999 log_info("Watching %s", dn);
2000
2001 if (inotify_add_watch(inotify_fd, dn, IN_CREATE|IN_MOVED_TO|IN_ONLYDIR|IN_DELETE_SELF|IN_MOVE_SELF) < 0) {
2002 if (errno != ENOENT)
2003 return log_error_errno(errno, "Failed to add watch on %s: %m", dn);
2004 } else
2005 break;
2006
2007 r = path_extract_directory(dn, &ndn);
2008 if (r == -EADDRNOTAVAIL) /* Arrived at the top? */
2009 break;
2010 if (r < 0)
2011 return log_error_errno(r, "Failed to extract directory from device node path '%s': %m", dn);
2012
2013 free_and_replace(dn, ndn);
2014 }
2015
2016 w = now(CLOCK_MONOTONIC);
2017 if (w >= until)
2018 return log_error_errno(SYNTHETIC_ERRNO(ETIMEDOUT), "Device link %s still hasn't shown up, giving up.", path);
2019
2020 r = fd_wait_for_event(inotify_fd, POLLIN, until - w);
2021 if (r < 0) {
2022 if (ERRNO_IS_TRANSIENT(r))
2023 continue;
2024 return log_error_errno(r, "Failed to watch inotify: %m");
2025 }
2026
2027 (void) flush_fd(inotify_fd);
2028 }
2029 }
2030
2031 static int calculate_initial_image_size(UserRecord *h, int image_fd, const char *fstype, uint64_t *ret) {
2032 uint64_t upper_boundary, lower_boundary;
2033 struct statfs sfs;
2034
2035 assert(h);
2036 assert(image_fd >= 0);
2037 assert(ret);
2038
2039 if (fstatfs(image_fd, &sfs) < 0)
2040 return log_error_errno(errno, "statfs() on image failed: %m");
2041
2042 upper_boundary = DISK_SIZE_ROUND_DOWN((uint64_t) sfs.f_bsize * sfs.f_bavail);
2043
2044 if (h->disk_size != UINT64_MAX)
2045 *ret = MIN(DISK_SIZE_ROUND_DOWN(h->disk_size), upper_boundary);
2046 else if (h->disk_size_relative == UINT64_MAX) {
2047
2048 if (upper_boundary > UINT64_MAX / USER_DISK_SIZE_DEFAULT_PERCENT)
2049 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Disk size too large.");
2050
2051 *ret = DISK_SIZE_ROUND_DOWN(upper_boundary * USER_DISK_SIZE_DEFAULT_PERCENT / 100);
2052
2053 log_info("Sizing home to %u%% of available disk space, which is %s.",
2054 USER_DISK_SIZE_DEFAULT_PERCENT,
2055 FORMAT_BYTES(*ret));
2056 } else {
2057 *ret = DISK_SIZE_ROUND_DOWN((uint64_t) ((double) upper_boundary * (double) CLAMP(h->disk_size_relative, 0U, UINT32_MAX) / (double) UINT32_MAX));
2058
2059 log_info("Sizing home to %" PRIu64 ".%01" PRIu64 "%% of available disk space, which is %s.",
2060 (h->disk_size_relative * 100) / UINT32_MAX,
2061 ((h->disk_size_relative * 1000) / UINT32_MAX) % 10,
2062 FORMAT_BYTES(*ret));
2063 }
2064
2065 lower_boundary = minimal_size_by_fs_name(fstype);
2066 if (lower_boundary != UINT64_MAX) {
2067 assert(GPT_LUKS2_OVERHEAD < UINT64_MAX - lower_boundary);
2068 lower_boundary += GPT_LUKS2_OVERHEAD;
2069 }
2070 if (lower_boundary == UINT64_MAX || lower_boundary < USER_DISK_SIZE_MIN)
2071 lower_boundary = USER_DISK_SIZE_MIN;
2072
2073 if (*ret < lower_boundary)
2074 *ret = lower_boundary;
2075
2076 return 0;
2077 }
2078
2079 static int home_truncate(
2080 UserRecord *h,
2081 int fd,
2082 uint64_t size) {
2083
2084 bool trunc;
2085 int r;
2086
2087 assert(h);
2088 assert(fd >= 0);
2089
2090 trunc = user_record_luks_discard(h);
2091 if (!trunc) {
2092 r = fallocate(fd, 0, 0, size);
2093 if (r < 0 && ERRNO_IS_NOT_SUPPORTED(errno)) {
2094 /* Some file systems do not support fallocate(), let's gracefully degrade
2095 * (ZFS, reiserfs, …) and fall back to truncation */
2096 log_notice_errno(errno, "Backing file system does not support fallocate(), falling back to ftruncate(), i.e. implicitly using non-discard mode.");
2097 trunc = true;
2098 }
2099 }
2100
2101 if (trunc)
2102 r = ftruncate(fd, size);
2103
2104 if (r < 0) {
2105 if (ERRNO_IS_DISK_SPACE(errno)) {
2106 log_debug_errno(errno, "Not enough disk space to allocate home of size %s.", FORMAT_BYTES(size));
2107 return -ENOSPC; /* make recognizable */
2108 }
2109
2110 return log_error_errno(errno, "Failed to truncate home image: %m");
2111 }
2112
2113 return !trunc; /* Return == 0 if we managed to truncate, > 0 if we managed to allocate */
2114 }
2115
2116 int home_create_luks(
2117 UserRecord *h,
2118 HomeSetup *setup,
2119 const PasswordCache *cache,
2120 char **effective_passwords,
2121 UserRecord **ret_home) {
2122
2123 _cleanup_free_ char *subdir = NULL, *disk_uuid_path = NULL;
2124 uint64_t encrypted_size,
2125 host_size = 0, partition_offset = 0, partition_size = 0; /* Unnecessary initialization to appease gcc */
2126 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
2127 sd_id128_t partition_uuid, fs_uuid, luks_uuid, disk_uuid;
2128 _cleanup_close_ int mount_fd = -1;
2129 const char *fstype, *ip;
2130 struct statfs sfs;
2131 int r;
2132
2133 assert(h);
2134 assert(h->storage < 0 || h->storage == USER_LUKS);
2135 assert(setup);
2136 assert(!setup->temporary_image_path);
2137 assert(setup->image_fd < 0);
2138 assert(ret_home);
2139
2140 r = dlopen_cryptsetup();
2141 if (r < 0)
2142 return r;
2143
2144 assert_se(ip = user_record_image_path(h));
2145
2146 fstype = user_record_file_system_type(h);
2147 if (!supported_fstype(fstype))
2148 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "Unsupported file system type: %s", fstype);
2149
2150 r = mkfs_exists(fstype);
2151 if (r < 0)
2152 return log_error_errno(r, "Failed to check if mkfs binary for %s exists: %m", fstype);
2153 if (r == 0) {
2154 if (h->file_system_type || streq(fstype, "ext4") || !supported_fstype("ext4"))
2155 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "mkfs binary for file system type %s does not exist.", fstype);
2156
2157 /* If the record does not explicitly declare a file system to use, and the compiled-in
2158 * default does not actually exist, than do an automatic fallback onto ext4, as the baseline
2159 * fs of Linux. We won't search for a working fs type here beyond ext4, i.e. nothing fancier
2160 * than a single, conservative fallback to baseline. This should be useful in minimal
2161 * environments where mkfs.btrfs or so are not made available, but mkfs.ext4 as Linux' most
2162 * boring, most basic fs is. */
2163 log_info("Formatting tool for compiled-in default file system %s not available, falling back to ext4 instead.", fstype);
2164 fstype = "ext4";
2165 }
2166
2167 if (sd_id128_is_null(h->partition_uuid)) {
2168 r = sd_id128_randomize(&partition_uuid);
2169 if (r < 0)
2170 return log_error_errno(r, "Failed to acquire partition UUID: %m");
2171 } else
2172 partition_uuid = h->partition_uuid;
2173
2174 if (sd_id128_is_null(h->luks_uuid)) {
2175 r = sd_id128_randomize(&luks_uuid);
2176 if (r < 0)
2177 return log_error_errno(r, "Failed to acquire LUKS UUID: %m");
2178 } else
2179 luks_uuid = h->luks_uuid;
2180
2181 if (sd_id128_is_null(h->file_system_uuid)) {
2182 r = sd_id128_randomize(&fs_uuid);
2183 if (r < 0)
2184 return log_error_errno(r, "Failed to acquire file system UUID: %m");
2185 } else
2186 fs_uuid = h->file_system_uuid;
2187
2188 r = make_dm_names(h, setup);
2189 if (r < 0)
2190 return r;
2191
2192 r = access(setup->dm_node, F_OK);
2193 if (r < 0) {
2194 if (errno != ENOENT)
2195 return log_error_errno(errno, "Failed to determine whether %s exists: %m", setup->dm_node);
2196 } else
2197 return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Device mapper device %s already exists, refusing.", setup->dm_node);
2198
2199 if (path_startswith(ip, "/dev/")) {
2200 _cleanup_free_ char *sysfs = NULL;
2201 uint64_t block_device_size;
2202 struct stat st;
2203
2204 /* Let's place the home directory on a real device, i.e. an USB stick or such */
2205
2206 setup->image_fd = open_image_file(h, ip, &st);
2207 if (setup->image_fd < 0)
2208 return setup->image_fd;
2209
2210 if (!S_ISBLK(st.st_mode))
2211 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Device is not a block device, refusing.");
2212
2213 if (asprintf(&sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/partition", DEVNUM_FORMAT_VAL(st.st_rdev)) < 0)
2214 return log_oom();
2215 if (access(sysfs, F_OK) < 0) {
2216 if (errno != ENOENT)
2217 return log_error_errno(errno, "Failed to check whether %s exists: %m", sysfs);
2218 } else
2219 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Operating on partitions is currently not supported, sorry. Please specify a top-level block device.");
2220
2221 if (flock(setup->image_fd, LOCK_EX) < 0) /* make sure udev doesn't read from it while we operate on the device */
2222 return log_error_errno(errno, "Failed to lock block device %s: %m", ip);
2223
2224 if (ioctl(setup->image_fd, BLKGETSIZE64, &block_device_size) < 0)
2225 return log_error_errno(errno, "Failed to read block device size: %m");
2226
2227 if (h->disk_size == UINT64_MAX) {
2228
2229 /* If a relative disk size is requested, apply it relative to the block device size */
2230 if (h->disk_size_relative < UINT32_MAX)
2231 host_size = CLAMP(DISK_SIZE_ROUND_DOWN(block_device_size * h->disk_size_relative / UINT32_MAX),
2232 USER_DISK_SIZE_MIN, USER_DISK_SIZE_MAX);
2233 else
2234 host_size = block_device_size; /* Otherwise, take the full device */
2235
2236 } else if (h->disk_size > block_device_size)
2237 return log_error_errno(SYNTHETIC_ERRNO(EMSGSIZE), "Selected disk size larger than backing block device, refusing.");
2238 else
2239 host_size = DISK_SIZE_ROUND_DOWN(h->disk_size);
2240
2241 if (!supported_fs_size(fstype, LESS_BY(host_size, GPT_LUKS2_OVERHEAD)))
2242 return log_error_errno(SYNTHETIC_ERRNO(ERANGE),
2243 "Selected file system size too small for %s.", fstype);
2244
2245 /* After creation we should reference this partition by its UUID instead of the block
2246 * device. That's preferable since the user might have specified a device node such as
2247 * /dev/sdb to us, which might look very different when replugged. */
2248 if (asprintf(&disk_uuid_path, "/dev/disk/by-uuid/" SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(luks_uuid)) < 0)
2249 return log_oom();
2250
2251 if (user_record_luks_discard(h) || user_record_luks_offline_discard(h)) {
2252 /* If we want online or offline discard, discard once before we start using things. */
2253
2254 if (ioctl(setup->image_fd, BLKDISCARD, (uint64_t[]) { 0, block_device_size }) < 0)
2255 log_full_errno(errno == EOPNOTSUPP ? LOG_DEBUG : LOG_WARNING, errno,
2256 "Failed to issue full-device BLKDISCARD on device, ignoring: %m");
2257 else
2258 log_info("Full device discard completed.");
2259 }
2260 } else {
2261 _cleanup_free_ char *t = NULL;
2262
2263 r = mkdir_parents(ip, 0755);
2264 if (r < 0)
2265 return log_error_errno(r, "Failed to create parent directory of %s: %m", ip);
2266
2267 r = tempfn_random(ip, "homework", &t);
2268 if (r < 0)
2269 return log_error_errno(r, "Failed to derive temporary file name for %s: %m", ip);
2270
2271 setup->image_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
2272 if (setup->image_fd < 0)
2273 return log_error_errno(errno, "Failed to create home image %s: %m", t);
2274
2275 setup->temporary_image_path = TAKE_PTR(t);
2276
2277 r = chattr_full(t, setup->image_fd, FS_NOCOW_FL|FS_NOCOMP_FL, FS_NOCOW_FL|FS_NOCOMP_FL, NULL, NULL, CHATTR_FALLBACK_BITWISE);
2278 if (r < 0 && r != -ENOANO) /* ENOANO → some bits didn't work; which we skip logging about because chattr_full() already debug logs about those flags */
2279 log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r,
2280 "Failed to set file attributes on %s, ignoring: %m", setup->temporary_image_path);
2281
2282 r = calculate_initial_image_size(h, setup->image_fd, fstype, &host_size);
2283 if (r < 0)
2284 return r;
2285
2286 r = resize_image_loop(h, setup, 0, host_size, &host_size);
2287 if (r < 0)
2288 return r;
2289
2290 log_info("Allocating image file completed.");
2291 }
2292
2293 r = make_partition_table(
2294 setup->image_fd,
2295 user_record_user_name_and_realm(h),
2296 partition_uuid,
2297 &partition_offset,
2298 &partition_size,
2299 &disk_uuid);
2300 if (r < 0)
2301 return r;
2302
2303 log_info("Writing of partition table completed.");
2304
2305 r = loop_device_make(setup->image_fd, O_RDWR, partition_offset, partition_size, user_record_luks_sector_size(h), 0, LOCK_EX, &setup->loop);
2306 if (r < 0) {
2307 if (r == -ENOENT) { /* this means /dev/loop-control doesn't exist, i.e. we are in a container
2308 * or similar and loopback bock devices are not available, return a
2309 * recognizable error in this case. */
2310 log_error_errno(r, "Loopback block device support is not available on this system.");
2311 return -ENOLINK; /* Make recognizable */
2312 }
2313
2314 return log_error_errno(r, "Failed to set up loopback device for %s: %m", setup->temporary_image_path);
2315 }
2316
2317 log_info("Setting up loopback device %s completed.", setup->loop->node ?: ip);
2318
2319 r = luks_format(setup->loop->node,
2320 setup->dm_name,
2321 luks_uuid,
2322 user_record_user_name_and_realm(h),
2323 cache,
2324 effective_passwords,
2325 user_record_luks_discard(h) || user_record_luks_offline_discard(h),
2326 h,
2327 &setup->crypt_device);
2328 if (r < 0)
2329 return r;
2330
2331 setup->undo_dm = true;
2332
2333 r = block_get_size_by_path(setup->dm_node, &encrypted_size);
2334 if (r < 0)
2335 return log_error_errno(r, "Failed to get encrypted block device size: %m");
2336
2337 log_info("Setting up LUKS device %s completed.", setup->dm_node);
2338
2339 r = make_filesystem(setup->dm_node, fstype, user_record_user_name_and_realm(h), NULL, fs_uuid, user_record_luks_discard(h));
2340 if (r < 0)
2341 return r;
2342
2343 log_info("Formatting file system completed.");
2344
2345 r = home_unshare_and_mount(setup->dm_node, fstype, user_record_luks_discard(h), user_record_mount_flags(h), h->luks_extra_mount_options);
2346 if (r < 0)
2347 return r;
2348
2349 setup->undo_mount = true;
2350
2351 subdir = path_join(HOME_RUNTIME_WORK_DIR, user_record_user_name_and_realm(h));
2352 if (!subdir)
2353 return log_oom();
2354
2355 /* Prefer using a btrfs subvolume if we can, fall back to directory otherwise */
2356 r = btrfs_subvol_make_fallback(subdir, 0700);
2357 if (r < 0)
2358 return log_error_errno(r, "Failed to create user directory in mounted image file: %m");
2359
2360 setup->root_fd = open(subdir, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
2361 if (setup->root_fd < 0)
2362 return log_error_errno(errno, "Failed to open user directory in mounted image file: %m");
2363
2364 (void) home_shift_uid(setup->root_fd, NULL, UID_NOBODY, h->uid, &mount_fd);
2365
2366 if (mount_fd >= 0) {
2367 /* If we have established a new mount, then we can use that as new root fd to our home directory. */
2368 safe_close(setup->root_fd);
2369
2370 setup->root_fd = fd_reopen(mount_fd, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
2371 if (setup->root_fd < 0)
2372 return log_error_errno(setup->root_fd, "Unable to convert mount fd into proper directory fd: %m");
2373
2374 mount_fd = safe_close(mount_fd);
2375 }
2376
2377 r = home_populate(h, setup->root_fd);
2378 if (r < 0)
2379 return r;
2380
2381 r = home_sync_and_statfs(setup->root_fd, &sfs);
2382 if (r < 0)
2383 return r;
2384
2385 r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG|USER_RECORD_PERMISSIVE, &new_home);
2386 if (r < 0)
2387 return log_error_errno(r, "Failed to clone record: %m");
2388
2389 r = user_record_add_binding(
2390 new_home,
2391 USER_LUKS,
2392 disk_uuid_path ?: ip,
2393 partition_uuid,
2394 luks_uuid,
2395 fs_uuid,
2396 sym_crypt_get_cipher(setup->crypt_device),
2397 sym_crypt_get_cipher_mode(setup->crypt_device),
2398 luks_volume_key_size_convert(setup->crypt_device),
2399 fstype,
2400 NULL,
2401 h->uid,
2402 (gid_t) h->uid);
2403 if (r < 0)
2404 return log_error_errno(r, "Failed to add binding to record: %m");
2405
2406 if (user_record_luks_offline_discard(h)) {
2407 r = run_fitrim(setup->root_fd);
2408 if (r < 0)
2409 return r;
2410 }
2411
2412 setup->root_fd = safe_close(setup->root_fd);
2413
2414 r = home_setup_undo_mount(setup, LOG_ERR);
2415 if (r < 0)
2416 return r;
2417
2418 r = home_setup_undo_dm(setup, LOG_ERR);
2419 if (r < 0)
2420 return r;
2421
2422 setup->loop = loop_device_unref(setup->loop);
2423
2424 if (!user_record_luks_offline_discard(h)) {
2425 r= run_fallocate(setup->image_fd, NULL /* refresh stat() data */);
2426 if (r < 0)
2427 return r;
2428 }
2429
2430 /* Sync everything to disk before we move things into place under the final name. */
2431 if (fsync(setup->image_fd) < 0)
2432 return log_error_errno(r, "Failed to synchronize image to disk: %m");
2433
2434 if (disk_uuid_path)
2435 /* Reread partition table if this is a block device */
2436 (void) ioctl(setup->image_fd, BLKRRPART, 0);
2437 else {
2438 assert(setup->temporary_image_path);
2439
2440 if (rename(setup->temporary_image_path, ip) < 0)
2441 return log_error_errno(errno, "Failed to rename image file: %m");
2442
2443 setup->temporary_image_path = mfree(setup->temporary_image_path);
2444
2445 /* If we operate on a file, sync the containing directory too. */
2446 r = fsync_directory_of_file(setup->image_fd);
2447 if (r < 0)
2448 return log_error_errno(r, "Failed to synchronize directory of image file to disk: %m");
2449
2450 log_info("Moved image file into place.");
2451 }
2452
2453 /* Let's close the image fd now. If we are operating on a real block device this will release the BSD
2454 * lock that ensures udev doesn't interfere with what we are doing */
2455 setup->image_fd = safe_close(setup->image_fd);
2456
2457 if (disk_uuid_path)
2458 (void) wait_for_devlink(disk_uuid_path);
2459
2460 log_info("Creation completed.");
2461
2462 print_size_summary(host_size, encrypted_size, &sfs);
2463
2464 log_debug("GPT + LUKS2 overhead is %" PRIu64 " (expected %" PRIu64 ")", host_size - encrypted_size, GPT_LUKS2_OVERHEAD);
2465
2466 *ret_home = TAKE_PTR(new_home);
2467 return 0;
2468 }
2469
2470 int home_get_state_luks(UserRecord *h, HomeSetup *setup) {
2471 int r;
2472
2473 assert(h);
2474 assert(setup);
2475
2476 r = make_dm_names(h, setup);
2477 if (r < 0)
2478 return r;
2479
2480 r = access(setup->dm_node, F_OK);
2481 if (r < 0 && errno != ENOENT)
2482 return log_error_errno(errno, "Failed to determine whether %s exists: %m", setup->dm_node);
2483
2484 return r >= 0;
2485 }
2486
2487 enum {
2488 CAN_RESIZE_ONLINE,
2489 CAN_RESIZE_OFFLINE,
2490 };
2491
2492 static int can_resize_fs(int fd, uint64_t old_size, uint64_t new_size) {
2493 struct statfs sfs;
2494
2495 assert(fd >= 0);
2496
2497 /* Filter out bogus requests early */
2498 if (old_size == 0 || old_size == UINT64_MAX ||
2499 new_size == 0 || new_size == UINT64_MAX)
2500 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid resize parameters.");
2501
2502 if ((old_size & 511) != 0 || (new_size & 511) != 0)
2503 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Resize parameters not multiple of 512.");
2504
2505 if (fstatfs(fd, &sfs) < 0)
2506 return log_error_errno(errno, "Failed to fstatfs() file system: %m");
2507
2508 if (is_fs_type(&sfs, BTRFS_SUPER_MAGIC)) {
2509
2510 if (new_size < BTRFS_MINIMAL_SIZE)
2511 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "New file system size too small for btrfs (needs to be 256M at least.");
2512
2513 /* btrfs can grow and shrink online */
2514
2515 } else if (is_fs_type(&sfs, XFS_SB_MAGIC)) {
2516
2517 if (new_size < XFS_MINIMAL_SIZE)
2518 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "New file system size too small for xfs (needs to be 14M at least).");
2519
2520 /* XFS can grow, but not shrink */
2521 if (new_size < old_size)
2522 return log_error_errno(SYNTHETIC_ERRNO(EMSGSIZE), "Shrinking this type of file system is not supported.");
2523
2524 } else if (is_fs_type(&sfs, EXT4_SUPER_MAGIC)) {
2525
2526 if (new_size < EXT4_MINIMAL_SIZE)
2527 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "New file system size too small for ext4 (needs to be 1M at least).");
2528
2529 /* ext4 can grow online, and shrink offline */
2530 if (new_size < old_size)
2531 return CAN_RESIZE_OFFLINE;
2532
2533 } else
2534 return log_error_errno(SYNTHETIC_ERRNO(ESOCKTNOSUPPORT), "Resizing this type of file system is not supported.");
2535
2536 return CAN_RESIZE_ONLINE;
2537 }
2538
2539 static int ext4_offline_resize_fs(
2540 HomeSetup *setup,
2541 uint64_t new_size,
2542 bool discard,
2543 unsigned long flags,
2544 const char *extra_mount_options) {
2545
2546 _cleanup_free_ char *size_str = NULL;
2547 bool re_open = false, re_mount = false;
2548 pid_t resize_pid, fsck_pid;
2549 int r, exit_status;
2550
2551 assert(setup);
2552 assert(setup->dm_node);
2553
2554 /* First, unmount the file system */
2555 if (setup->root_fd >= 0) {
2556 setup->root_fd = safe_close(setup->root_fd);
2557 re_open = true;
2558 }
2559
2560 if (setup->undo_mount) {
2561 r = home_setup_undo_mount(setup, LOG_ERR);
2562 if (r < 0)
2563 return r;
2564
2565 re_mount = true;
2566 }
2567
2568 log_info("Temporary unmounting of file system completed.");
2569
2570 /* resize2fs requires that the file system is force checked first, do so. */
2571 r = safe_fork("(e2fsck)",
2572 FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_STDOUT_TO_STDERR|FORK_CLOSE_ALL_FDS,
2573 &fsck_pid);
2574 if (r < 0)
2575 return r;
2576 if (r == 0) {
2577 /* Child */
2578 execlp("e2fsck" ,"e2fsck", "-fp", setup->dm_node, NULL);
2579 log_open();
2580 log_error_errno(errno, "Failed to execute e2fsck: %m");
2581 _exit(EXIT_FAILURE);
2582 }
2583
2584 exit_status = wait_for_terminate_and_check("e2fsck", fsck_pid, WAIT_LOG_ABNORMAL);
2585 if (exit_status < 0)
2586 return exit_status;
2587 if ((exit_status & ~FSCK_ERROR_CORRECTED) != 0) {
2588 log_warning("e2fsck failed with exit status %i.", exit_status);
2589
2590 if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0)
2591 return log_error_errno(SYNTHETIC_ERRNO(EIO), "File system is corrupted, refusing.");
2592
2593 log_warning("Ignoring fsck error.");
2594 }
2595
2596 log_info("Forced file system check completed.");
2597
2598 /* We use 512 sectors here, because resize2fs doesn't do byte sizes */
2599 if (asprintf(&size_str, "%" PRIu64 "s", new_size / 512) < 0)
2600 return log_oom();
2601
2602 /* Resize the thing */
2603 r = safe_fork("(e2resize)",
2604 FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|FORK_CLOSE_ALL_FDS,
2605 &resize_pid);
2606 if (r < 0)
2607 return r;
2608 if (r == 0) {
2609 /* Child */
2610 execlp("resize2fs" ,"resize2fs", setup->dm_node, size_str, NULL);
2611 log_open();
2612 log_error_errno(errno, "Failed to execute resize2fs: %m");
2613 _exit(EXIT_FAILURE);
2614 }
2615
2616 log_info("Offline file system resize completed.");
2617
2618 /* Re-establish mounts and reopen the directory */
2619 if (re_mount) {
2620 r = home_mount_node(setup->dm_node, "ext4", discard, flags, extra_mount_options);
2621 if (r < 0)
2622 return r;
2623
2624 setup->undo_mount = true;
2625 }
2626
2627 if (re_open) {
2628 setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
2629 if (setup->root_fd < 0)
2630 return log_error_errno(errno, "Failed to reopen file system: %m");
2631 }
2632
2633 log_info("File system mounted again.");
2634
2635 return 0;
2636 }
2637
2638 static int prepare_resize_partition(
2639 int fd,
2640 uint64_t partition_offset,
2641 uint64_t old_partition_size,
2642 sd_id128_t *ret_disk_uuid,
2643 struct fdisk_table **ret_table,
2644 struct fdisk_partition **ret_partition) {
2645
2646 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
2647 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
2648 _cleanup_free_ char *path = NULL, *disk_uuid_as_string = NULL;
2649 struct fdisk_partition *found = NULL;
2650 sd_id128_t disk_uuid;
2651 size_t n_partitions;
2652 int r;
2653
2654 assert(fd >= 0);
2655 assert(ret_disk_uuid);
2656 assert(ret_table);
2657
2658 assert((partition_offset & 511) == 0);
2659 assert((old_partition_size & 511) == 0);
2660 assert(UINT64_MAX - old_partition_size >= partition_offset);
2661
2662 if (partition_offset == 0) {
2663 /* If the offset is at the beginning we assume no partition table, let's exit early. */
2664 log_debug("Not rewriting partition table, operating on naked device.");
2665 *ret_disk_uuid = SD_ID128_NULL;
2666 *ret_table = NULL;
2667 *ret_partition = NULL;
2668 return 0;
2669 }
2670
2671 c = fdisk_new_context();
2672 if (!c)
2673 return log_oom();
2674
2675 if (asprintf(&path, "/proc/self/fd/%i", fd) < 0)
2676 return log_oom();
2677
2678 r = fdisk_assign_device(c, path, 0);
2679 if (r < 0)
2680 return log_error_errno(r, "Failed to open device: %m");
2681
2682 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
2683 return log_error_errno(SYNTHETIC_ERRNO(ENOMEDIUM), "Disk has no GPT partition table.");
2684
2685 r = fdisk_get_disklabel_id(c, &disk_uuid_as_string);
2686 if (r < 0)
2687 return log_error_errno(r, "Failed to acquire disk UUID: %m");
2688
2689 r = sd_id128_from_string(disk_uuid_as_string, &disk_uuid);
2690 if (r < 0)
2691 return log_error_errno(r, "Failed parse disk UUID: %m");
2692
2693 r = fdisk_get_partitions(c, &t);
2694 if (r < 0)
2695 return log_error_errno(r, "Failed to acquire partition table: %m");
2696
2697 n_partitions = fdisk_table_get_nents(t);
2698 for (size_t i = 0; i < n_partitions; i++) {
2699 struct fdisk_partition *p;
2700
2701 p = fdisk_table_get_partition(t, i);
2702 if (!p)
2703 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
2704
2705 if (fdisk_partition_is_used(p) <= 0)
2706 continue;
2707 if (fdisk_partition_has_start(p) <= 0 || fdisk_partition_has_size(p) <= 0 || fdisk_partition_has_end(p) <= 0)
2708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found partition without a size.");
2709
2710 if (fdisk_partition_get_start(p) == partition_offset / 512U &&
2711 fdisk_partition_get_size(p) == old_partition_size / 512U) {
2712
2713 if (found)
2714 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ), "Partition found twice, refusing.");
2715
2716 found = p;
2717 } else if (fdisk_partition_get_end(p) > partition_offset / 512U)
2718 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Can't extend, not last partition in image.");
2719 }
2720
2721 if (!found)
2722 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to find matching partition to resize.");
2723
2724 *ret_disk_uuid = disk_uuid;
2725 *ret_table = TAKE_PTR(t);
2726 *ret_partition = found;
2727
2728 return 1;
2729 }
2730
2731 static int ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *userdata) {
2732 char *result;
2733
2734 assert(c);
2735
2736 switch (fdisk_ask_get_type(ask)) {
2737
2738 case FDISK_ASKTYPE_STRING:
2739 result = new(char, 37);
2740 if (!result)
2741 return log_oom();
2742
2743 fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) userdata, result));
2744 break;
2745
2746 default:
2747 log_debug("Unexpected question from libfdisk, ignoring.");
2748 }
2749
2750 return 0;
2751 }
2752
2753 static int apply_resize_partition(
2754 int fd,
2755 sd_id128_t disk_uuids,
2756 struct fdisk_table *t,
2757 struct fdisk_partition *p,
2758 size_t new_partition_size) {
2759
2760 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
2761 _cleanup_free_ void *two_zero_lbas = NULL;
2762 _cleanup_free_ char *path = NULL;
2763 ssize_t n;
2764 int r;
2765
2766 assert(fd >= 0);
2767 assert(!t == !p);
2768
2769 if (!t) /* no partition table to apply, exit early */
2770 return 0;
2771
2772 assert(p);
2773
2774 /* Before writing our partition patch the final size in */
2775 r = fdisk_partition_size_explicit(p, 1);
2776 if (r < 0)
2777 return log_error_errno(r, "Failed to enable explicit partition size: %m");
2778
2779 r = fdisk_partition_set_size(p, new_partition_size / 512U);
2780 if (r < 0)
2781 return log_error_errno(r, "Failed to change partition size: %m");
2782
2783 two_zero_lbas = malloc0(1024U);
2784 if (!two_zero_lbas)
2785 return log_oom();
2786
2787 /* libfdisk appears to get confused by the existing PMBR. Let's explicitly flush it out. */
2788 n = pwrite(fd, two_zero_lbas, 1024U, 0);
2789 if (n < 0)
2790 return log_error_errno(errno, "Failed to wipe partition table: %m");
2791 if (n != 1024)
2792 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while wiping partition table.");
2793
2794 c = fdisk_new_context();
2795 if (!c)
2796 return log_oom();
2797
2798 if (asprintf(&path, "/proc/self/fd/%i", fd) < 0)
2799 return log_oom();
2800
2801 r = fdisk_assign_device(c, path, 0);
2802 if (r < 0)
2803 return log_error_errno(r, "Failed to open device: %m");
2804
2805 r = fdisk_create_disklabel(c, "gpt");
2806 if (r < 0)
2807 return log_error_errno(r, "Failed to create GPT disk label: %m");
2808
2809 r = fdisk_apply_table(c, t);
2810 if (r < 0)
2811 return log_error_errno(r, "Failed to apply partition table: %m");
2812
2813 r = fdisk_set_ask(c, ask_cb, &disk_uuids);
2814 if (r < 0)
2815 return log_error_errno(r, "Failed to set libfdisk query function: %m");
2816
2817 r = fdisk_set_disklabel_id(c);
2818 if (r < 0)
2819 return log_error_errno(r, "Failed to change disklabel ID: %m");
2820
2821 r = fdisk_write_disklabel(c);
2822 if (r < 0)
2823 return log_error_errno(r, "Failed to write disk label: %m");
2824
2825 return 1;
2826 }
2827
2828 /* Always keep at least 16M free, so that we can safely log in and update the user record while doing so */
2829 #define HOME_MIN_FREE (16U*1024U*1024U)
2830
2831 static int get_smallest_fs_size(int fd, uint64_t *ret) {
2832 uint64_t minsz, needed;
2833 struct statfs sfs;
2834
2835 assert(fd >= 0);
2836 assert(ret);
2837
2838 /* Determines the minimal disk size we might be able to shrink the file system referenced by the fd to. */
2839
2840 if (syncfs(fd) < 0) /* let's sync before we query the size, so that the values returned are accurate */
2841 return log_error_errno(errno, "Failed to synchronize home file system: %m");
2842
2843 if (fstatfs(fd, &sfs) < 0)
2844 return log_error_errno(errno, "Failed to statfs() home file system: %m");
2845
2846 /* Let's determine the minimal file system size of the used fstype */
2847 minsz = minimal_size_by_fs_magic(sfs.f_type);
2848 if (minsz == UINT64_MAX)
2849 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Don't know minimum file system size of file system type '%s' of home directory.", fs_type_to_string(sfs.f_type));
2850
2851 if (minsz < USER_DISK_SIZE_MIN)
2852 minsz = USER_DISK_SIZE_MIN;
2853
2854 if (sfs.f_bfree > sfs.f_blocks)
2855 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Detected amount of free blocks is greater than the total amount of file system blocks. Refusing.");
2856
2857 /* Calculate how much disk space is currently in use. */
2858 needed = sfs.f_blocks - sfs.f_bfree;
2859 if (needed > UINT64_MAX / sfs.f_bsize)
2860 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File system size out of range.");
2861
2862 needed *= sfs.f_bsize;
2863
2864 /* Add some safety margin of free space we'll always keep */
2865 if (needed > UINT64_MAX - HOME_MIN_FREE) /* Check for overflow */
2866 needed = UINT64_MAX;
2867 else
2868 needed += HOME_MIN_FREE;
2869
2870 *ret = DISK_SIZE_ROUND_UP(MAX(needed, minsz));
2871 return 0;
2872 }
2873
2874 static int get_largest_image_size(int fd, const struct stat *st, uint64_t *ret) {
2875 uint64_t used, avail, sum;
2876 struct statfs sfs;
2877 int r;
2878
2879 assert(fd >= 0);
2880 assert(st);
2881 assert(ret);
2882
2883 /* Determines the maximum file size we might be able to grow the image file referenced by the fd to. */
2884
2885 r = stat_verify_regular(st);
2886 if (r < 0)
2887 return log_error_errno(r, "Image file is not a regular file, refusing: %m");
2888
2889 if (syncfs(fd) < 0)
2890 return log_error_errno(errno, "Failed to synchronize file system backing image file: %m");
2891
2892 if (fstatfs(fd, &sfs) < 0)
2893 return log_error_errno(errno, "Failed to statfs() image file: %m");
2894
2895 used = (uint64_t) st->st_blocks * 512;
2896 avail = (uint64_t) sfs.f_bsize * sfs.f_bavail;
2897
2898 if (avail > UINT64_MAX - used)
2899 sum = UINT64_MAX;
2900 else
2901 sum = avail + used;
2902
2903 *ret = DISK_SIZE_ROUND_DOWN(MIN(sum, USER_DISK_SIZE_MAX));
2904 return 0;
2905 }
2906
2907 static int resize_fs_loop(
2908 UserRecord *h,
2909 HomeSetup *setup,
2910 int resize_type,
2911 uint64_t old_fs_size,
2912 uint64_t new_fs_size,
2913 uint64_t *ret_fs_size) {
2914
2915 uint64_t current_fs_size;
2916 unsigned n_iterations = 0;
2917 int r;
2918
2919 assert(h);
2920 assert(setup);
2921 assert(setup->root_fd >= 0);
2922
2923 /* A bisection loop trying to find the closest size to what the user asked for. (Well, we bisect like
2924 * this only when we *shrink* the fs — if we grow the fs there's no need to bisect.) */
2925
2926 current_fs_size = old_fs_size;
2927 for (uint64_t lower_boundary = new_fs_size, upper_boundary = old_fs_size, try_fs_size = new_fs_size;;) {
2928 bool worked;
2929
2930 n_iterations++;
2931
2932 /* Now resize the file system */
2933 if (resize_type == CAN_RESIZE_ONLINE) {
2934 r = resize_fs(setup->root_fd, try_fs_size, NULL);
2935 if (r < 0) {
2936 if (!ERRNO_IS_DISK_SPACE(r) || new_fs_size > old_fs_size) /* Not a disk space issue? Not trying to shrink? */
2937 return log_error_errno(r, "Failed to resize file system: %m");
2938
2939 log_debug_errno(r, "Shrinking from %s to %s didn't work, not enough space for contained data.", FORMAT_BYTES(current_fs_size), FORMAT_BYTES(try_fs_size));
2940 worked = false;
2941 } else {
2942 log_debug("Successfully resized from %s to %s.", FORMAT_BYTES(current_fs_size), FORMAT_BYTES(try_fs_size));
2943 current_fs_size = try_fs_size;
2944 worked = true;
2945 }
2946
2947 /* If we hit a disk space issue and are shrinking the fs, then maybe it helps to
2948 * increase the image size. */
2949 } else {
2950 r = ext4_offline_resize_fs(setup, try_fs_size, user_record_luks_discard(h), user_record_mount_flags(h), h->luks_extra_mount_options);
2951 if (r < 0)
2952 return r;
2953
2954 /* For now, when we fail to shrink an ext4 image we'll not try again via the
2955 * bisection logic. We might add that later, but give this involves shelling out
2956 * multiple programs it's a bit too cumbersome to my taste. */
2957
2958 worked = true;
2959 current_fs_size = try_fs_size;
2960 }
2961
2962 if (new_fs_size > old_fs_size) /* If we are growing we are done after one iteration */
2963 break;
2964
2965 /* If we are shrinking then let's adjust our bisection boundaries and try again. */
2966 if (worked)
2967 upper_boundary = MIN(upper_boundary, try_fs_size);
2968 else
2969 lower_boundary = MAX(lower_boundary, try_fs_size);
2970
2971 /* OK, this attempt to shrink didn't work. Let's try between the old size and what worked. */
2972 if (lower_boundary >= upper_boundary) {
2973 log_debug("Image can't be shrunk further (range to try is empty).");
2974 break;
2975 }
2976
2977 /* Let's find a new value to try half-way between the lower boundary and the upper boundary
2978 * to try now. */
2979 try_fs_size = DISK_SIZE_ROUND_DOWN(lower_boundary + (upper_boundary - lower_boundary) / 2);
2980 if (try_fs_size <= lower_boundary || try_fs_size >= upper_boundary) {
2981 log_debug("Image can't be shrunk further (remaining range to try too small).");
2982 break;
2983 }
2984 }
2985
2986 log_debug("Bisection loop completed after %u iterations.", n_iterations);
2987
2988 if (ret_fs_size)
2989 *ret_fs_size = current_fs_size;
2990
2991 return 0;
2992 }
2993
2994 static int resize_image_loop(
2995 UserRecord *h,
2996 HomeSetup *setup,
2997 uint64_t old_image_size,
2998 uint64_t new_image_size,
2999 uint64_t *ret_image_size) {
3000
3001 uint64_t current_image_size;
3002 unsigned n_iterations = 0;
3003 int r;
3004
3005 assert(h);
3006 assert(setup);
3007 assert(setup->image_fd >= 0);
3008
3009 /* A bisection loop trying to find the closest size to what the user asked for. (Well, we bisect like
3010 * this only when we *grow* the image — if we shrink the image then there's no need to bisect.) */
3011
3012 current_image_size = old_image_size;
3013 for (uint64_t lower_boundary = old_image_size, upper_boundary = new_image_size, try_image_size = new_image_size;;) {
3014 bool worked;
3015
3016 n_iterations++;
3017
3018 r = home_truncate(h, setup->image_fd, try_image_size);
3019 if (r < 0) {
3020 if (!ERRNO_IS_DISK_SPACE(r) || new_image_size < old_image_size) /* Not a disk space issue? Not trying to grow? */
3021 return r;
3022
3023 log_debug_errno(r, "Growing from %s to %s didn't work, not enough space on backing disk.", FORMAT_BYTES(current_image_size), FORMAT_BYTES(try_image_size));
3024 worked = false;
3025 } else if (r > 0) { /* Success: allocation worked */
3026 log_debug("Resizing from %s to %s via allocation worked successfully.", FORMAT_BYTES(current_image_size), FORMAT_BYTES(try_image_size));
3027 current_image_size = try_image_size;
3028 worked = true;
3029 } else { /* Success, but through truncation, not allocation. */
3030 log_debug("Resizing from %s to %s via truncation worked successfully.", FORMAT_BYTES(old_image_size), FORMAT_BYTES(try_image_size));
3031 current_image_size = try_image_size;
3032 break; /* there's no point in the bisection logic if this was plain truncation and
3033 * not allocation, let's exit immediately. */
3034 }
3035
3036 if (new_image_size < old_image_size) /* If we are shrinking we are done after one iteration */
3037 break;
3038
3039 /* If we are growing then let's adjust our bisection boundaries and try again */
3040 if (worked)
3041 lower_boundary = MAX(lower_boundary, try_image_size);
3042 else
3043 upper_boundary = MIN(upper_boundary, try_image_size);
3044
3045 if (lower_boundary >= upper_boundary) {
3046 log_debug("Image can't be grown further (range to try is empty).");
3047 break;
3048 }
3049
3050 try_image_size = DISK_SIZE_ROUND_DOWN(lower_boundary + (upper_boundary - lower_boundary) / 2);
3051 if (try_image_size <= lower_boundary || try_image_size >= upper_boundary) {
3052 log_debug("Image can't be grown further (remaining range to try too small).");
3053 break;
3054 }
3055 }
3056
3057 log_debug("Bisection loop completed after %u iterations.", n_iterations);
3058
3059 if (ret_image_size)
3060 *ret_image_size = current_image_size;
3061
3062 return 0;
3063 }
3064
3065 int home_resize_luks(
3066 UserRecord *h,
3067 HomeSetupFlags flags,
3068 HomeSetup *setup,
3069 PasswordCache *cache,
3070 UserRecord **ret_home) {
3071
3072 uint64_t old_image_size, new_image_size, old_fs_size, new_fs_size, crypto_offset, crypto_offset_bytes,
3073 new_partition_size, smallest_fs_size, resized_fs_size;
3074 _cleanup_(user_record_unrefp) UserRecord *header_home = NULL, *embedded_home = NULL, *new_home = NULL;
3075 _cleanup_(fdisk_unref_tablep) struct fdisk_table *table = NULL;
3076 struct fdisk_partition *partition = NULL;
3077 _cleanup_close_ int opened_image_fd = -1;
3078 _cleanup_free_ char *whole_disk = NULL;
3079 int r, resize_type, image_fd = -1;
3080 sd_id128_t disk_uuid;
3081 const char *ip, *ipo;
3082 struct statfs sfs;
3083 struct stat st;
3084 enum {
3085 INTENTION_DONT_KNOW = 0, /* These happen to match the return codes of CMP() */
3086 INTENTION_SHRINK = -1,
3087 INTENTION_GROW = 1,
3088 } intention = INTENTION_DONT_KNOW;
3089
3090 assert(h);
3091 assert(user_record_storage(h) == USER_LUKS);
3092 assert(setup);
3093
3094 r = dlopen_cryptsetup();
3095 if (r < 0)
3096 return r;
3097
3098 assert_se(ipo = user_record_image_path(h));
3099 ip = strdupa_safe(ipo); /* copy out since original might change later in home record object */
3100
3101 if (setup->image_fd < 0) {
3102 setup->image_fd = open_image_file(h, NULL, &st);
3103 if (setup->image_fd < 0)
3104 return setup->image_fd;
3105 } else {
3106 if (fstat(setup->image_fd, &st) < 0)
3107 return log_error_errno(errno, "Failed to stat image file %s: %m", ip);
3108 }
3109
3110 image_fd = setup->image_fd;
3111
3112 if (S_ISBLK(st.st_mode)) {
3113 dev_t parent;
3114
3115 r = block_get_whole_disk(st.st_rdev, &parent);
3116 if (r < 0)
3117 return log_error_errno(r, "Failed to acquire whole block device for %s: %m", ip);
3118 if (r > 0) {
3119 /* If we shall resize a file system on a partition device, then let's figure out the
3120 * whole disk device and operate on that instead, since we need to rewrite the
3121 * partition table to resize the partition. */
3122
3123 log_info("Operating on partition device %s, using parent device.", ip);
3124
3125 opened_image_fd = r = device_open_from_devnum(S_IFBLK, parent, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK, &whole_disk);
3126 if (r < 0)
3127 return log_error_errno(r, "Failed to open whole block device for %s: %m", ip);
3128
3129 image_fd = opened_image_fd;
3130
3131 if (fstat(image_fd, &st) < 0)
3132 return log_error_errno(errno, "Failed to stat whole block device %s: %m", whole_disk);
3133 } else
3134 log_info("Operating on whole block device %s.", ip);
3135
3136 if (ioctl(image_fd, BLKGETSIZE64, &old_image_size) < 0)
3137 return log_error_errno(errno, "Failed to determine size of original block device: %m");
3138
3139 if (flock(image_fd, LOCK_EX) < 0) /* make sure udev doesn't read from it while we operate on the device */
3140 return log_error_errno(errno, "Failed to lock block device %s: %m", ip);
3141
3142 new_image_size = old_image_size; /* we can't resize physical block devices */
3143 } else {
3144 r = stat_verify_regular(&st);
3145 if (r < 0)
3146 return log_error_errno(r, "Image %s is not a block device nor regular file: %m", ip);
3147
3148 old_image_size = st.st_size;
3149
3150 /* Note an asymetry here: when we operate on loopback files the specified disk size we get we
3151 * apply onto the loopback file as a whole. When we operate on block devices we instead apply
3152 * to the partition itself only. */
3153
3154 if (FLAGS_SET(flags, HOME_SETUP_RESIZE_MINIMIZE)) {
3155 new_image_size = 0;
3156 intention = INTENTION_SHRINK;
3157 } else {
3158 uint64_t new_image_size_rounded;
3159
3160 new_image_size_rounded = DISK_SIZE_ROUND_DOWN(h->disk_size);
3161
3162 if (old_image_size >= new_image_size_rounded && old_image_size <= h->disk_size) {
3163 /* If exact match, or a match after we rounded down, don't do a thing */
3164 log_info("Image size already matching, skipping operation.");
3165 return 0;
3166 }
3167
3168 new_image_size = new_image_size_rounded;
3169 intention = CMP(new_image_size, old_image_size); /* Is this a shrink */
3170 }
3171 }
3172
3173 r = home_setup_luks(
3174 h,
3175 flags,
3176 whole_disk,
3177 setup,
3178 cache,
3179 FLAGS_SET(flags, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES) ? NULL : &header_home);
3180 if (r < 0)
3181 return r;
3182
3183 if (!FLAGS_SET(flags, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES)) {
3184 r = home_load_embedded_identity(h, setup->root_fd, header_home, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL, cache, &embedded_home, &new_home);
3185 if (r < 0)
3186 return r;
3187 }
3188
3189 r = home_maybe_shift_uid(h, flags, setup);
3190 if (r < 0)
3191 return r;
3192
3193 log_info("offset = %" PRIu64 ", size = %" PRIu64 ", image = %" PRIu64, setup->partition_offset, setup->partition_size, old_image_size);
3194
3195 if ((UINT64_MAX - setup->partition_offset) < setup->partition_size ||
3196 setup->partition_offset + setup->partition_size > old_image_size)
3197 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Old partition doesn't fit in backing storage, refusing.");
3198
3199 if (S_ISREG(st.st_mode)) {
3200 uint64_t partition_table_extra, largest_size;
3201
3202 partition_table_extra = old_image_size - setup->partition_size;
3203
3204 r = get_largest_image_size(setup->image_fd, &st, &largest_size);
3205 if (r < 0)
3206 return r;
3207 if (new_image_size > largest_size)
3208 new_image_size = largest_size;
3209
3210 if (new_image_size < partition_table_extra)
3211 new_image_size = partition_table_extra;
3212
3213 new_partition_size = DISK_SIZE_ROUND_DOWN(new_image_size - partition_table_extra);
3214 } else {
3215 assert(S_ISBLK(st.st_mode));
3216
3217 if (FLAGS_SET(flags, HOME_SETUP_RESIZE_MINIMIZE)) {
3218 new_partition_size = 0;
3219 intention = INTENTION_SHRINK;
3220 } else {
3221 uint64_t new_partition_size_rounded;
3222
3223 new_partition_size_rounded = DISK_SIZE_ROUND_DOWN(h->disk_size);
3224
3225 if (setup->partition_size >= new_partition_size_rounded &&
3226 setup->partition_size <= h->disk_size) {
3227 log_info("Partition size already matching, skipping operation.");
3228 return 0;
3229 }
3230
3231 new_partition_size = new_partition_size_rounded;
3232 intention = CMP(new_partition_size, setup->partition_size);
3233 }
3234 }
3235
3236 if ((UINT64_MAX - setup->partition_offset) < new_partition_size ||
3237 setup->partition_offset + new_partition_size > new_image_size)
3238 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "New partition doesn't fit into backing storage, refusing.");
3239
3240 crypto_offset = sym_crypt_get_data_offset(setup->crypt_device);
3241 if (crypto_offset > UINT64_MAX/512U)
3242 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "LUKS2 data offset out of range, refusing.");
3243 crypto_offset_bytes = (uint64_t) crypto_offset * 512U;
3244 if (setup->partition_size <= crypto_offset_bytes)
3245 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Weird, old crypto payload offset doesn't actually fit in partition size?");
3246
3247 /* Make sure at least the LUKS header fit in */
3248 if (new_partition_size <= crypto_offset_bytes) {
3249 uint64_t add;
3250
3251 add = DISK_SIZE_ROUND_UP(crypto_offset_bytes) - new_partition_size;
3252 new_partition_size += add;
3253 if (S_ISREG(st.st_mode))
3254 new_image_size += add;
3255 }
3256
3257 old_fs_size = setup->partition_size - crypto_offset_bytes;
3258 new_fs_size = DISK_SIZE_ROUND_DOWN(new_partition_size - crypto_offset_bytes);
3259
3260 r = get_smallest_fs_size(setup->root_fd, &smallest_fs_size);
3261 if (r < 0)
3262 return r;
3263
3264 if (new_fs_size < smallest_fs_size) {
3265 uint64_t add;
3266
3267 add = DISK_SIZE_ROUND_UP(smallest_fs_size) - new_fs_size;
3268 new_fs_size += add;
3269 new_partition_size += add;
3270 if (S_ISREG(st.st_mode))
3271 new_image_size += add;
3272 }
3273
3274 if (new_fs_size == old_fs_size) {
3275 log_info("New file system size identical to old file system size, skipping operation.");
3276 return 0;
3277 }
3278
3279 if (FLAGS_SET(flags, HOME_SETUP_RESIZE_DONT_GROW) && new_fs_size > old_fs_size) {
3280 log_info("New file system size would be larger than old, but shrinking requested, skipping operation.");
3281 return 0;
3282 }
3283
3284 if (FLAGS_SET(flags, HOME_SETUP_RESIZE_DONT_SHRINK) && new_fs_size < old_fs_size) {
3285 log_info("New file system size would be smaller than old, but growing requested, skipping operation.");
3286 return 0;
3287 }
3288
3289 if (CMP(new_fs_size, old_fs_size) != intention) {
3290 if (intention < 0)
3291 log_info("Shrink operation would enlarge file system, skipping operation.");
3292 else {
3293 assert(intention > 0);
3294 log_info("Grow operation would shrink file system, skipping operation.");
3295 }
3296 return 0;
3297 }
3298
3299 /* Before we start doing anything, let's figure out if we actually can */
3300 resize_type = can_resize_fs(setup->root_fd, old_fs_size, new_fs_size);
3301 if (resize_type < 0)
3302 return resize_type;
3303 if (resize_type == CAN_RESIZE_OFFLINE && FLAGS_SET(flags, HOME_SETUP_ALREADY_ACTIVATED))
3304 return log_error_errno(SYNTHETIC_ERRNO(ETXTBSY), "File systems of this type can only be resized offline, but is currently online.");
3305
3306 log_info("Ready to resize image size %s %s %s, partition size %s %s %s, file system size %s %s %s.",
3307 FORMAT_BYTES(old_image_size),
3308 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
3309 FORMAT_BYTES(new_image_size),
3310 FORMAT_BYTES(setup->partition_size),
3311 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
3312 FORMAT_BYTES(new_partition_size),
3313 FORMAT_BYTES(old_fs_size),
3314 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
3315 FORMAT_BYTES(new_fs_size));
3316
3317 r = prepare_resize_partition(
3318 image_fd,
3319 setup->partition_offset,
3320 setup->partition_size,
3321 &disk_uuid,
3322 &table,
3323 &partition);
3324 if (r < 0)
3325 return r;
3326
3327 if (new_fs_size > old_fs_size) { /* → Grow */
3328
3329 if (S_ISREG(st.st_mode)) {
3330 uint64_t resized_image_size;
3331
3332 /* Grow file size */
3333 r = resize_image_loop(h, setup, old_image_size, new_image_size, &resized_image_size);
3334 if (r < 0)
3335 return r;
3336
3337 if (resized_image_size == old_image_size) {
3338 log_info("Couldn't change image size.");
3339 return 0;
3340 }
3341
3342 assert(resized_image_size > old_image_size);
3343
3344 log_info("Growing of image file from %s to %s completed.", FORMAT_BYTES(old_image_size), FORMAT_BYTES(resized_image_size));
3345
3346 if (resized_image_size < new_image_size) {
3347 uint64_t sub;
3348
3349 /* If the growing we managed to do is smaller than what we wanted we need to
3350 * adjust the partition/file system sizes we are going for, too */
3351 sub = new_image_size - resized_image_size;
3352 assert(new_partition_size >= sub);
3353 new_partition_size -= sub;
3354 assert(new_fs_size >= sub);
3355 new_fs_size -= sub;
3356 }
3357
3358 new_image_size = resized_image_size;
3359 } else {
3360 assert(S_ISBLK(st.st_mode));
3361 assert(new_image_size == old_image_size);
3362 }
3363
3364 /* Make sure loopback device sees the new bigger size */
3365 r = loop_device_refresh_size(setup->loop, UINT64_MAX, new_partition_size);
3366 if (r == -ENOTTY)
3367 log_debug_errno(r, "Device is not a loopback device, not refreshing size.");
3368 else if (r < 0)
3369 return log_error_errno(r, "Failed to refresh loopback device size: %m");
3370 else
3371 log_info("Refreshing loop device size completed.");
3372
3373 r = apply_resize_partition(image_fd, disk_uuid, table, partition, new_partition_size);
3374 if (r < 0)
3375 return r;
3376 if (r > 0)
3377 log_info("Growing of partition completed.");
3378
3379 if (S_ISBLK(st.st_mode) && ioctl(image_fd, BLKRRPART, 0) < 0)
3380 log_debug_errno(errno, "BLKRRPART failed on block device, ignoring: %m");
3381
3382 /* Tell LUKS about the new bigger size too */
3383 r = sym_crypt_resize(setup->crypt_device, setup->dm_name, new_fs_size / 512U);
3384 if (r < 0)
3385 return log_error_errno(r, "Failed to grow LUKS device: %m");
3386
3387 log_info("LUKS device growing completed.");
3388 } else {
3389 /* → Shrink */
3390
3391 if (!FLAGS_SET(flags, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES)) {
3392 r = home_store_embedded_identity(new_home, setup->root_fd, h->uid, embedded_home);
3393 if (r < 0)
3394 return r;
3395 }
3396
3397 if (S_ISREG(st.st_mode)) {
3398 if (user_record_luks_discard(h))
3399 /* Before we shrink, let's trim the file system, so that we need less space on disk during the shrinking */
3400 (void) run_fitrim(setup->root_fd);
3401 else {
3402 /* If discard is off, let's ensure all backing blocks are allocated, so that our resize operation doesn't fail half-way */
3403 r = run_fallocate(image_fd, &st);
3404 if (r < 0)
3405 return r;
3406 }
3407 }
3408 }
3409
3410 /* Now try to resize the file system. The requested size might not always be possible, in which case
3411 * we'll try to get as close as we can get. The result is returned in 'resized_fs_size' */
3412 r = resize_fs_loop(h, setup, resize_type, old_fs_size, new_fs_size, &resized_fs_size);
3413 if (r < 0)
3414 return r;
3415
3416 if (resized_fs_size == old_fs_size) {
3417 log_info("Couldn't change file system size.");
3418 return 0;
3419 }
3420
3421 log_info("File system resizing from %s to %s completed.", FORMAT_BYTES(old_fs_size), FORMAT_BYTES(resized_fs_size));
3422
3423 if (resized_fs_size > new_fs_size) {
3424 uint64_t add;
3425
3426 /* If the shrinking we managed to do is larger than what we wanted we need to adjust the partition/image sizes. */
3427 add = resized_fs_size - new_fs_size;
3428 new_partition_size += add;
3429 if (S_ISREG(st.st_mode))
3430 new_image_size += add;
3431 }
3432
3433 new_fs_size = resized_fs_size;
3434
3435 /* Immediately sync afterwards */
3436 r = home_sync_and_statfs(setup->root_fd, NULL);
3437 if (r < 0)
3438 return r;
3439
3440 if (new_fs_size < old_fs_size) { /* → Shrink */
3441
3442 /* Shrink the LUKS device now, matching the new file system size */
3443 r = sym_crypt_resize(setup->crypt_device, setup->dm_name, new_fs_size / 512);
3444 if (r < 0)
3445 return log_error_errno(r, "Failed to shrink LUKS device: %m");
3446
3447 log_info("LUKS device shrinking completed.");
3448
3449 /* Refresh the loop devices size */
3450 r = loop_device_refresh_size(setup->loop, UINT64_MAX, new_partition_size);
3451 if (r == -ENOTTY)
3452 log_debug_errno(r, "Device is not a loopback device, not refreshing size.");
3453 else if (r < 0)
3454 return log_error_errno(r, "Failed to refresh loopback device size: %m");
3455 else
3456 log_info("Refreshing loop device size completed.");
3457
3458 if (S_ISREG(st.st_mode)) {
3459 /* Shrink the image file */
3460 if (ftruncate(image_fd, new_image_size) < 0)
3461 return log_error_errno(errno, "Failed to shrink image file %s: %m", ip);
3462
3463 log_info("Shrinking of image file completed.");
3464 } else {
3465 assert(S_ISBLK(st.st_mode));
3466 assert(new_image_size == old_image_size);
3467 }
3468
3469 r = apply_resize_partition(image_fd, disk_uuid, table, partition, new_partition_size);
3470 if (r < 0)
3471 return r;
3472 if (r > 0)
3473 log_info("Shrinking of partition completed.");
3474
3475 if (S_ISBLK(st.st_mode) && ioctl(image_fd, BLKRRPART, 0) < 0)
3476 log_debug_errno(errno, "BLKRRPART failed on block device, ignoring: %m");
3477
3478 } else { /* → Grow */
3479 if (!FLAGS_SET(flags, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES)) {
3480 r = home_store_embedded_identity(new_home, setup->root_fd, h->uid, embedded_home);
3481 if (r < 0)
3482 return r;
3483 }
3484 }
3485
3486 if (!FLAGS_SET(flags, HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES)) {
3487 r = home_store_header_identity_luks(new_home, setup, header_home);
3488 if (r < 0)
3489 return r;
3490
3491 r = home_extend_embedded_identity(new_home, h, setup);
3492 if (r < 0)
3493 return r;
3494 }
3495
3496 if (user_record_luks_discard(h))
3497 (void) run_fitrim(setup->root_fd);
3498
3499 r = home_sync_and_statfs(setup->root_fd, &sfs);
3500 if (r < 0)
3501 return r;
3502
3503 if (!FLAGS_SET(flags, HOME_SETUP_RESIZE_DONT_UNDO)) {
3504 r = home_setup_done(setup);
3505 if (r < 0)
3506 return r;
3507 }
3508
3509 log_info("Resizing completed.");
3510
3511 print_size_summary(new_image_size, new_fs_size, &sfs);
3512
3513 if (ret_home)
3514 *ret_home = TAKE_PTR(new_home);
3515
3516 return 0;
3517 }
3518
3519 int home_passwd_luks(
3520 UserRecord *h,
3521 HomeSetupFlags flags,
3522 HomeSetup *setup,
3523 const PasswordCache *cache, /* the passwords acquired via PKCS#11/FIDO2 security tokens */
3524 char **effective_passwords /* new passwords */) {
3525
3526 size_t volume_key_size, max_key_slots, n_effective;
3527 _cleanup_(erase_and_freep) void *volume_key = NULL;
3528 struct crypt_pbkdf_type good_pbkdf, minimal_pbkdf;
3529 const char *type;
3530 char **list;
3531 int r;
3532
3533 assert(h);
3534 assert(user_record_storage(h) == USER_LUKS);
3535 assert(setup);
3536
3537 r = dlopen_cryptsetup();
3538 if (r < 0)
3539 return r;
3540
3541 type = sym_crypt_get_type(setup->crypt_device);
3542 if (!type)
3543 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine crypto device type.");
3544
3545 r = sym_crypt_keyslot_max(type);
3546 if (r <= 0)
3547 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine number of key slots.");
3548 max_key_slots = r;
3549
3550 r = sym_crypt_get_volume_key_size(setup->crypt_device);
3551 if (r <= 0)
3552 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine volume key size.");
3553 volume_key_size = (size_t) r;
3554
3555 volume_key = malloc(volume_key_size);
3556 if (!volume_key)
3557 return log_oom();
3558
3559 r = -ENOKEY;
3560 FOREACH_POINTER(list,
3561 cache ? cache->keyring_passswords : NULL,
3562 cache ? cache->pkcs11_passwords : NULL,
3563 cache ? cache->fido2_passwords : NULL,
3564 h->password) {
3565
3566 r = luks_try_passwords(h, setup->crypt_device, list, volume_key, &volume_key_size, NULL);
3567 if (r != -ENOKEY)
3568 break;
3569 }
3570 if (r == -ENOKEY)
3571 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Failed to unlock LUKS superblock with supplied passwords.");
3572 if (r < 0)
3573 return log_error_errno(r, "Failed to unlocks LUKS superblock: %m");
3574
3575 n_effective = strv_length(effective_passwords);
3576
3577 build_good_pbkdf(&good_pbkdf, h);
3578 build_minimal_pbkdf(&minimal_pbkdf, h);
3579
3580 for (size_t i = 0; i < max_key_slots; i++) {
3581 r = sym_crypt_keyslot_destroy(setup->crypt_device, i);
3582 if (r < 0 && !IN_SET(r, -ENOENT, -EINVAL)) /* Returns EINVAL or ENOENT if there's no key in this slot already */
3583 return log_error_errno(r, "Failed to destroy LUKS password: %m");
3584
3585 if (i >= n_effective) {
3586 if (r >= 0)
3587 log_info("Destroyed LUKS key slot %zu.", i);
3588 continue;
3589 }
3590
3591 if (password_cache_contains(cache, effective_passwords[i])) { /* Is this a FIDO2 or PKCS#11 password? */
3592 log_debug("Using minimal PBKDF for slot %zu", i);
3593 r = sym_crypt_set_pbkdf_type(setup->crypt_device, &minimal_pbkdf);
3594 } else {
3595 log_debug("Using good PBKDF for slot %zu", i);
3596 r = sym_crypt_set_pbkdf_type(setup->crypt_device, &good_pbkdf);
3597 }
3598 if (r < 0)
3599 return log_error_errno(r, "Failed to tweak PBKDF for slot %zu: %m", i);
3600
3601 r = sym_crypt_keyslot_add_by_volume_key(
3602 setup->crypt_device,
3603 i,
3604 volume_key,
3605 volume_key_size,
3606 effective_passwords[i],
3607 strlen(effective_passwords[i]));
3608 if (r < 0)
3609 return log_error_errno(r, "Failed to set up LUKS password: %m");
3610
3611 log_info("Updated LUKS key slot %zu.", i);
3612
3613 /* If we changed the password, then make sure to update the copy in the keyring, so that
3614 * auto-rebalance continues to work. We only do this if we operate on an active home dir. */
3615 if (i == 0 && FLAGS_SET(flags, HOME_SETUP_ALREADY_ACTIVATED))
3616 upload_to_keyring(h, effective_passwords[i], NULL);
3617 }
3618
3619 return 1;
3620 }
3621
3622 int home_lock_luks(UserRecord *h, HomeSetup *setup) {
3623 const char *p;
3624 int r;
3625
3626 assert(h);
3627 assert(setup);
3628 assert(setup->root_fd < 0);
3629 assert(!setup->crypt_device);
3630
3631 r = acquire_open_luks_device(h, setup, /* graceful= */ false);
3632 if (r < 0)
3633 return r;
3634
3635 log_info("Discovered used LUKS device %s.", setup->dm_node);
3636
3637 assert_se(p = user_record_home_directory(h));
3638 r = syncfs_path(AT_FDCWD, p);
3639 if (r < 0) /* Snake oil, but let's better be safe than sorry */
3640 return log_error_errno(r, "Failed to synchronize file system %s: %m", p);
3641
3642 log_info("File system synchronized.");
3643
3644 /* Note that we don't invoke FIFREEZE here, it appears libcryptsetup/device-mapper already does that on its own for us */
3645
3646 r = sym_crypt_suspend(setup->crypt_device, setup->dm_name);
3647 if (r < 0)
3648 return log_error_errno(r, "Failed to suspend cryptsetup device: %s: %m", setup->dm_node);
3649
3650 log_info("LUKS device suspended.");
3651 return 0;
3652 }
3653
3654 static int luks_try_resume(
3655 struct crypt_device *cd,
3656 const char *dm_name,
3657 char **password) {
3658
3659 int r;
3660
3661 assert(cd);
3662 assert(dm_name);
3663
3664 STRV_FOREACH(pp, password) {
3665 r = sym_crypt_resume_by_passphrase(
3666 cd,
3667 dm_name,
3668 CRYPT_ANY_SLOT,
3669 *pp,
3670 strlen(*pp));
3671 if (r >= 0) {
3672 log_info("Resumed LUKS device %s.", dm_name);
3673 return 0;
3674 }
3675
3676 log_debug_errno(r, "Password %zu didn't work for resuming device: %m", (size_t) (pp - password));
3677 }
3678
3679 return -ENOKEY;
3680 }
3681
3682 int home_unlock_luks(UserRecord *h, HomeSetup *setup, const PasswordCache *cache) {
3683 char **list;
3684 int r;
3685
3686 assert(h);
3687 assert(setup);
3688 assert(!setup->crypt_device);
3689
3690 r = acquire_open_luks_device(h, setup, /* graceful= */ false);
3691 if (r < 0)
3692 return r;
3693
3694 log_info("Discovered used LUKS device %s.", setup->dm_node);
3695
3696 r = -ENOKEY;
3697 FOREACH_POINTER(list,
3698 cache ? cache->pkcs11_passwords : NULL,
3699 cache ? cache->fido2_passwords : NULL,
3700 h->password) {
3701 r = luks_try_resume(setup->crypt_device, setup->dm_name, list);
3702 if (r != -ENOKEY)
3703 break;
3704 }
3705 if (r == -ENOKEY)
3706 return log_error_errno(r, "No valid password for LUKS superblock.");
3707 if (r < 0)
3708 return log_error_errno(r, "Failed to resume LUKS superblock: %m");
3709
3710 log_info("LUKS device resumed.");
3711 return 0;
3712 }
3713
3714 static int device_is_gone(HomeSetup *setup) {
3715 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
3716 struct stat st;
3717 int r;
3718
3719 assert(setup);
3720
3721 if (!setup->dm_node)
3722 return true;
3723
3724 if (stat(setup->dm_node, &st) < 0) {
3725 if (errno != ENOENT)
3726 return log_error_errno(errno, "Failed to stat block device node %s: %m", setup->dm_node);
3727
3728 return true;
3729 }
3730
3731 r = sd_device_new_from_stat_rdev(&d, &st);
3732 if (r < 0) {
3733 if (r != -ENODEV)
3734 return log_error_errno(errno, "Failed to allocate device object from block device node %s: %m", setup->dm_node);
3735
3736 return true;
3737 }
3738
3739 return false;
3740 }
3741
3742 static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
3743 HomeSetup *setup = ASSERT_PTR(userdata);
3744 int r;
3745
3746 if (!device_for_action(device, SD_DEVICE_REMOVE))
3747 return 0;
3748
3749 /* We don't really care for the device object passed to us, we just check if the device node still
3750 * exists */
3751
3752 r = device_is_gone(setup);
3753 if (r < 0)
3754 return r;
3755 if (r > 0) /* Yay! we are done! */
3756 (void) sd_event_exit(sd_device_monitor_get_event(monitor), 0);
3757
3758 return 0;
3759 }
3760
3761 int wait_for_block_device_gone(HomeSetup *setup, usec_t timeout_usec) {
3762 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL;
3763 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
3764 int r;
3765
3766 assert(setup);
3767
3768 /* So here's the thing: we enable "deferred deactivation" on our dm-crypt volumes. This means they
3769 * are automatically torn down once not used anymore (i.e. once unmounted). Which is great. It also
3770 * means that when we deactivate a home directory and try to tear down the volume that backs it, it
3771 * possibly is already torn down or in the process of being torn down, since we race against the
3772 * automatic tearing down. Which is fine, we handle errors from that. However, we lose the ability to
3773 * naturally wait for the tear down operation to complete: if we are not the ones who tear down the
3774 * device we are also not the ones who naturally block on that operation. Hence let's add some code
3775 * to actively wait for the device to go away, via sd-device. We'll call this whenever tearing down a
3776 * LUKS device, to ensure the device is really really gone before we proceed. Net effect: "homectl
3777 * deactivate foo && homectl activate foo" will work reliably, i.e. deactivation immediately followed
3778 * by activation will work. Also, by the time deactivation completes we can guarantee that all data
3779 * is sync'ed down to the lowest block layer as all higher levels are fully and entirely
3780 * destructed. */
3781
3782 if (!setup->dm_name)
3783 return 0;
3784
3785 assert(setup->dm_node);
3786 log_debug("Waiting until %s disappears.", setup->dm_node);
3787
3788 r = sd_event_new(&event);
3789 if (r < 0)
3790 return log_error_errno(r, "Failed to allocate event loop: %m");
3791
3792 r = sd_device_monitor_new(&m);
3793 if (r < 0)
3794 return log_error_errno(r, "Failed to allocate device monitor: %m");
3795
3796 r = sd_device_monitor_filter_add_match_subsystem_devtype(m, "block", "disk");
3797 if (r < 0)
3798 return log_error_errno(r, "Failed to configure device monitor match: %m");
3799
3800 r = sd_device_monitor_attach_event(m, event);
3801 if (r < 0)
3802 return log_error_errno(r, "Failed to attach device monitor to event loop: %m");
3803
3804 r = sd_device_monitor_start(m, device_monitor_handler, setup);
3805 if (r < 0)
3806 return log_error_errno(r, "Failed to start device monitor: %m");
3807
3808 r = device_is_gone(setup);
3809 if (r < 0)
3810 return r;
3811 if (r > 0) {
3812 log_debug("%s has already disappeared before entering wait loop.", setup->dm_node);
3813 return 0; /* gone already */
3814 }
3815
3816 if (timeout_usec != USEC_INFINITY) {
3817 r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, timeout_usec, 0, NULL, NULL);
3818 if (r < 0)
3819 return log_error_errno(r, "Failed to add timer event: %m");
3820 }
3821
3822 r = sd_event_loop(event);
3823 if (r < 0)
3824 return log_error_errno(r, "Failed to run event loop: %m");
3825
3826 r = device_is_gone(setup);
3827 if (r < 0)
3828 return r;
3829 if (r == 0)
3830 return log_error_errno(r, "Device %s still around.", setup->dm_node);
3831
3832 log_debug("Successfully waited until device %s disappeared.", setup->dm_node);
3833 return 0;
3834 }
3835
3836 int home_auto_shrink_luks(UserRecord *h, HomeSetup *setup, PasswordCache *cache) {
3837 struct statfs sfs;
3838 int r;
3839
3840 assert(h);
3841 assert(user_record_storage(h) == USER_LUKS);
3842 assert(setup);
3843 assert(setup->root_fd >= 0);
3844
3845 if (user_record_auto_resize_mode(h) != AUTO_RESIZE_SHRINK_AND_GROW)
3846 return 0;
3847
3848 if (fstatfs(setup->root_fd, &sfs) < 0)
3849 return log_error_errno(errno, "Failed to statfs home directory: %m");
3850
3851 if (!fs_can_online_shrink_and_grow(sfs.f_type)) {
3852 log_debug("Not auto-shrinking file system, since selected file system cannot do both online shrink and grow.");
3853 return 0;
3854 }
3855
3856 r = home_resize_luks(
3857 h,
3858 HOME_SETUP_ALREADY_ACTIVATED|
3859 HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES|
3860 HOME_SETUP_RESIZE_MINIMIZE|
3861 HOME_SETUP_RESIZE_DONT_GROW|
3862 HOME_SETUP_RESIZE_DONT_UNDO,
3863 setup,
3864 cache,
3865 NULL);
3866 if (r < 0)
3867 return r;
3868
3869 return 1;
3870 }