]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homework-fscrypt.c
Merge pull request #21161 from poettering/homed-uidmap-fscrypt
[thirdparty/systemd.git] / src / home / homework-fscrypt.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/fs.h>
4 #include <openssl/evp.h>
5 #include <openssl/sha.h>
6 #include <sys/ioctl.h>
7 #include <sys/xattr.h>
8
9 #include "errno-util.h"
10 #include "fd-util.h"
11 #include "hexdecoct.h"
12 #include "homework-fscrypt.h"
13 #include "homework-mount.h"
14 #include "homework-quota.h"
15 #include "memory-util.h"
16 #include "missing_keyctl.h"
17 #include "missing_syscall.h"
18 #include "mkdir.h"
19 #include "mount-util.h"
20 #include "nulstr-util.h"
21 #include "openssl-util.h"
22 #include "parse-util.h"
23 #include "process-util.h"
24 #include "random-util.h"
25 #include "rm-rf.h"
26 #include "stdio-util.h"
27 #include "strv.h"
28 #include "tmpfile-util.h"
29 #include "user-util.h"
30 #include "xattr-util.h"
31
32 static int fscrypt_upload_volume_key(
33 const uint8_t key_descriptor[static FS_KEY_DESCRIPTOR_SIZE],
34 const void *volume_key,
35 size_t volume_key_size,
36 key_serial_t where) {
37
38 _cleanup_free_ char *hex = NULL;
39 const char *description;
40 struct fscrypt_key key;
41 key_serial_t serial;
42
43 assert(key_descriptor);
44 assert(volume_key);
45 assert(volume_key_size > 0);
46
47 if (volume_key_size > sizeof(key.raw))
48 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume key too long.");
49
50 hex = hexmem(key_descriptor, FS_KEY_DESCRIPTOR_SIZE);
51 if (!hex)
52 return log_oom();
53
54 description = strjoina("fscrypt:", hex);
55
56 key = (struct fscrypt_key) {
57 .size = volume_key_size,
58 };
59 memcpy(key.raw, volume_key, volume_key_size);
60
61 /* Upload to the kernel */
62 serial = add_key("logon", description, &key, sizeof(key), where);
63 explicit_bzero_safe(&key, sizeof(key));
64
65 if (serial < 0)
66 return log_error_errno(errno, "Failed to install master key in keyring: %m");
67
68 log_info("Uploaded encryption key to kernel.");
69
70 return 0;
71 }
72
73 static void calculate_key_descriptor(
74 const void *key,
75 size_t key_size,
76 uint8_t ret_key_descriptor[static FS_KEY_DESCRIPTOR_SIZE]) {
77
78 uint8_t hashed[512 / 8] = {}, hashed2[512 / 8] = {};
79
80 /* Derive the key descriptor from the volume key via double SHA512, in order to be compatible with e4crypt */
81
82 assert_se(SHA512(key, key_size, hashed) == hashed);
83 assert_se(SHA512(hashed, sizeof(hashed), hashed2) == hashed2);
84
85 assert_cc(sizeof(hashed2) >= FS_KEY_DESCRIPTOR_SIZE);
86
87 memcpy(ret_key_descriptor, hashed2, FS_KEY_DESCRIPTOR_SIZE);
88 }
89
90 static int fscrypt_slot_try_one(
91 const char *password,
92 const void *salt, size_t salt_size,
93 const void *encrypted, size_t encrypted_size,
94 const uint8_t match_key_descriptor[static FS_KEY_DESCRIPTOR_SIZE],
95 void **ret_decrypted, size_t *ret_decrypted_size) {
96
97
98 _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL;
99 _cleanup_(erase_and_freep) void *decrypted = NULL;
100 uint8_t key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
101 int decrypted_size_out1, decrypted_size_out2;
102 uint8_t derived[512 / 8] = {};
103 size_t decrypted_size;
104 const EVP_CIPHER *cc;
105 int r;
106
107 assert(password);
108 assert(salt);
109 assert(salt_size > 0);
110 assert(encrypted);
111 assert(encrypted_size > 0);
112 assert(match_key_descriptor);
113
114 /* Our construction is like this:
115 *
116 * 1. In each key slot we store a salt value plus the encrypted volume key
117 *
118 * 2. Unlocking is via calculating PBKDF2-HMAC-SHA512 of the supplied password (in combination with
119 * the salt), then using the first 256 bit of the hash as key for decrypting the encrypted
120 * volume key in AES256 counter mode.
121 *
122 * 3. Writing a password is similar: calculate PBKDF2-HMAC-SHA512 of the supplied password (in
123 * combination with the salt), then encrypt the volume key in AES256 counter mode with the
124 * resulting hash.
125 */
126
127 if (PKCS5_PBKDF2_HMAC(
128 password, strlen(password),
129 salt, salt_size,
130 0xFFFF, EVP_sha512(),
131 sizeof(derived), derived) != 1) {
132 r = log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "PBKDF2 failed");
133 goto finish;
134 }
135
136 context = EVP_CIPHER_CTX_new();
137 if (!context) {
138 r = log_oom();
139 goto finish;
140 }
141
142 /* We use AES256 in counter mode */
143 assert_se(cc = EVP_aes_256_ctr());
144
145 /* We only use the first half of the derived key */
146 assert(sizeof(derived) >= (size_t) EVP_CIPHER_key_length(cc));
147
148 if (EVP_DecryptInit_ex(context, cc, NULL, derived, NULL) != 1) {
149 r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize decryption context.");
150 goto finish;
151 }
152
153 /* Flush out the derived key now, we don't need it anymore */
154 explicit_bzero_safe(derived, sizeof(derived));
155
156 decrypted_size = encrypted_size + EVP_CIPHER_key_length(cc) * 2;
157 decrypted = malloc(decrypted_size);
158 if (!decrypted)
159 return log_oom();
160
161 if (EVP_DecryptUpdate(context, (uint8_t*) decrypted, &decrypted_size_out1, encrypted, encrypted_size) != 1)
162 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to decrypt volume key.");
163
164 assert((size_t) decrypted_size_out1 <= decrypted_size);
165
166 if (EVP_DecryptFinal_ex(context, (uint8_t*) decrypted_size + decrypted_size_out1, &decrypted_size_out2) != 1)
167 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finish decryption of volume key.");
168
169 assert((size_t) decrypted_size_out1 + (size_t) decrypted_size_out2 < decrypted_size);
170 decrypted_size = (size_t) decrypted_size_out1 + (size_t) decrypted_size_out2;
171
172 calculate_key_descriptor(decrypted, decrypted_size, key_descriptor);
173
174 if (memcmp(key_descriptor, match_key_descriptor, FS_KEY_DESCRIPTOR_SIZE) != 0)
175 return -ENOANO; /* don't log here */
176
177 r = fscrypt_upload_volume_key(key_descriptor, decrypted, decrypted_size, KEY_SPEC_THREAD_KEYRING);
178 if (r < 0)
179 return r;
180
181 if (ret_decrypted)
182 *ret_decrypted = TAKE_PTR(decrypted);
183 if (ret_decrypted_size)
184 *ret_decrypted_size = decrypted_size;
185
186 return 0;
187
188 finish:
189 explicit_bzero_safe(derived, sizeof(derived));
190 return r;
191 }
192
193 static int fscrypt_slot_try_many(
194 char **passwords,
195 const void *salt, size_t salt_size,
196 const void *encrypted, size_t encrypted_size,
197 const uint8_t match_key_descriptor[static FS_KEY_DESCRIPTOR_SIZE],
198 void **ret_decrypted, size_t *ret_decrypted_size) {
199
200 char **i;
201 int r;
202
203 STRV_FOREACH(i, passwords) {
204 r = fscrypt_slot_try_one(*i, salt, salt_size, encrypted, encrypted_size, match_key_descriptor, ret_decrypted, ret_decrypted_size);
205 if (r != -ENOANO)
206 return r;
207 }
208
209 return -ENOANO;
210 }
211
212 static int fscrypt_setup(
213 const PasswordCache *cache,
214 char **password,
215 HomeSetup *setup,
216 void **ret_volume_key,
217 size_t *ret_volume_key_size) {
218
219 _cleanup_free_ char *xattr_buf = NULL;
220 const char *xa;
221 int r;
222
223 assert(setup);
224 assert(setup->root_fd >= 0);
225
226 r = flistxattr_malloc(setup->root_fd, &xattr_buf);
227 if (r < 0)
228 return log_error_errno(errno, "Failed to retrieve xattr list: %m");
229
230 NULSTR_FOREACH(xa, xattr_buf) {
231 _cleanup_free_ void *salt = NULL, *encrypted = NULL;
232 _cleanup_free_ char *value = NULL;
233 size_t salt_size, encrypted_size;
234 const char *nr, *e;
235 char **list;
236 int n;
237
238 /* Check if this xattr has the format 'trusted.fscrypt_slot<nr>' where '<nr>' is a 32bit unsigned integer */
239 nr = startswith(xa, "trusted.fscrypt_slot");
240 if (!nr)
241 continue;
242 if (safe_atou32(nr, NULL) < 0)
243 continue;
244
245 n = fgetxattr_malloc(setup->root_fd, xa, &value);
246 if (n == -ENODATA) /* deleted by now? */
247 continue;
248 if (n < 0)
249 return log_error_errno(n, "Failed to read %s xattr: %m", xa);
250
251 e = memchr(value, ':', n);
252 if (!e)
253 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "xattr %s lacks ':' separator: %m", xa);
254
255 r = unbase64mem(value, e - value, &salt, &salt_size);
256 if (r < 0)
257 return log_error_errno(r, "Failed to decode salt of %s: %m", xa);
258 r = unbase64mem(e+1, n - (e - value) - 1, &encrypted, &encrypted_size);
259 if (r < 0)
260 return log_error_errno(r, "Failed to decode encrypted key of %s: %m", xa);
261
262 r = -ENOANO;
263 FOREACH_POINTER(list, cache->pkcs11_passwords, cache->fido2_passwords, password) {
264 r = fscrypt_slot_try_many(
265 list,
266 salt, salt_size,
267 encrypted, encrypted_size,
268 setup->fscrypt_key_descriptor,
269 ret_volume_key, ret_volume_key_size);
270 if (r != -ENOANO)
271 break;
272 }
273 if (r < 0) {
274 if (r != -ENOANO)
275 return r;
276 } else
277 return 0;
278 }
279
280 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Failed to set up home directory with provided passwords.");
281 }
282
283 int home_setup_fscrypt(
284 UserRecord *h,
285 const PasswordCache *cache,
286 HomeSetup *setup) {
287
288 _cleanup_(erase_and_freep) void *volume_key = NULL;
289 struct fscrypt_policy policy = {};
290 size_t volume_key_size = 0;
291 const char *ip;
292 int r;
293
294 assert(h);
295 assert(user_record_storage(h) == USER_FSCRYPT);
296 assert(setup);
297 assert(setup->root_fd < 0);
298
299 assert_se(ip = user_record_image_path(h));
300
301 setup->root_fd = open(ip, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
302 if (setup->root_fd < 0)
303 return log_error_errno(errno, "Failed to open home directory: %m");
304
305 if (ioctl(setup->root_fd, FS_IOC_GET_ENCRYPTION_POLICY, &policy) < 0) {
306 if (errno == ENODATA)
307 return log_error_errno(errno, "Home directory %s is not encrypted.", ip);
308 if (ERRNO_IS_NOT_SUPPORTED(errno)) {
309 log_error_errno(errno, "File system does not support fscrypt: %m");
310 return -ENOLINK; /* make recognizable */
311 }
312 return log_error_errno(errno, "Failed to acquire encryption policy of %s: %m", ip);
313 }
314
315 memcpy(setup->fscrypt_key_descriptor, policy.master_key_descriptor, FS_KEY_DESCRIPTOR_SIZE);
316
317 r = fscrypt_setup(
318 cache,
319 h->password,
320 setup,
321 &volume_key,
322 &volume_key_size);
323 if (r < 0)
324 return r;
325
326 /* Also install the access key in the user's own keyring */
327
328 if (uid_is_valid(h->uid)) {
329 r = safe_fork("(sd-addkey)",
330 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_REOPEN_LOG,
331 NULL);
332 if (r < 0)
333 return log_error_errno(r, "Failed install encryption key in user's keyring: %m");
334 if (r == 0) {
335 gid_t gid;
336
337 /* Child */
338
339 gid = user_record_gid(h);
340 if (setresgid(gid, gid, gid) < 0) {
341 log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
342 _exit(EXIT_FAILURE);
343 }
344
345 if (setgroups(0, NULL) < 0) {
346 log_error_errno(errno, "Failed to reset auxiliary groups list: %m");
347 _exit(EXIT_FAILURE);
348 }
349
350 if (setresuid(h->uid, h->uid, h->uid) < 0) {
351 log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", h->uid);
352 _exit(EXIT_FAILURE);
353 }
354
355 r = fscrypt_upload_volume_key(
356 setup->fscrypt_key_descriptor,
357 volume_key,
358 volume_key_size,
359 KEY_SPEC_USER_KEYRING);
360 if (r < 0)
361 _exit(EXIT_FAILURE);
362
363 _exit(EXIT_SUCCESS);
364 }
365 }
366
367 /* We'll bind mount the image directory to a new mount point where we'll start adjusting it. Only
368 * once that's complete we'll move the thing to its final place eventually. */
369 r = home_unshare_and_mkdir();
370 if (r < 0)
371 return r;
372
373 r = mount_follow_verbose(LOG_ERR, ip, HOME_RUNTIME_WORK_DIR, NULL, MS_BIND, NULL);
374 if (r < 0)
375 return r;
376
377 setup->undo_mount = true;
378
379 /* Turn off any form of propagation for this */
380 r = mount_nofollow_verbose(LOG_ERR, NULL, HOME_RUNTIME_WORK_DIR, NULL, MS_PRIVATE, NULL);
381 if (r < 0)
382 return r;
383
384 /* Adjust MS_SUID and similar flags */
385 r = mount_nofollow_verbose(LOG_ERR, NULL, HOME_RUNTIME_WORK_DIR, NULL, MS_BIND|MS_REMOUNT|user_record_mount_flags(h), NULL);
386 if (r < 0)
387 return r;
388
389 safe_close(setup->root_fd);
390 setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
391 if (setup->root_fd < 0)
392 return log_error_errno(errno, "Failed to open home directory: %m");
393
394 return 0;
395 }
396
397 static int fscrypt_slot_set(
398 int root_fd,
399 const void *volume_key,
400 size_t volume_key_size,
401 const char *password,
402 uint32_t nr) {
403
404 _cleanup_free_ char *salt_base64 = NULL, *encrypted_base64 = NULL, *joined = NULL;
405 char label[STRLEN("trusted.fscrypt_slot") + DECIMAL_STR_MAX(nr) + 1];
406 _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL;
407 int r, encrypted_size_out1, encrypted_size_out2;
408 uint8_t salt[64], derived[512 / 8] = {};
409 _cleanup_free_ void *encrypted = NULL;
410 const EVP_CIPHER *cc;
411 size_t encrypted_size;
412
413 r = genuine_random_bytes(salt, sizeof(salt), RANDOM_BLOCK);
414 if (r < 0)
415 return log_error_errno(r, "Failed to generate salt: %m");
416
417 if (PKCS5_PBKDF2_HMAC(
418 password, strlen(password),
419 salt, sizeof(salt),
420 0xFFFF, EVP_sha512(),
421 sizeof(derived), derived) != 1) {
422 r = log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "PBKDF2 failed");
423 goto finish;
424 }
425
426 context = EVP_CIPHER_CTX_new();
427 if (!context) {
428 r = log_oom();
429 goto finish;
430 }
431
432 /* We use AES256 in counter mode */
433 cc = EVP_aes_256_ctr();
434
435 /* We only use the first half of the derived key */
436 assert(sizeof(derived) >= (size_t) EVP_CIPHER_key_length(cc));
437
438 if (EVP_EncryptInit_ex(context, cc, NULL, derived, NULL) != 1) {
439 r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize encryption context.");
440 goto finish;
441 }
442
443 /* Flush out the derived key now, we don't need it anymore */
444 explicit_bzero_safe(derived, sizeof(derived));
445
446 encrypted_size = volume_key_size + EVP_CIPHER_key_length(cc) * 2;
447 encrypted = malloc(encrypted_size);
448 if (!encrypted)
449 return log_oom();
450
451 if (EVP_EncryptUpdate(context, (uint8_t*) encrypted, &encrypted_size_out1, volume_key, volume_key_size) != 1)
452 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to encrypt volume key.");
453
454 assert((size_t) encrypted_size_out1 <= encrypted_size);
455
456 if (EVP_EncryptFinal_ex(context, (uint8_t*) encrypted_size + encrypted_size_out1, &encrypted_size_out2) != 1)
457 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finish encryption of volume key.");
458
459 assert((size_t) encrypted_size_out1 + (size_t) encrypted_size_out2 < encrypted_size);
460 encrypted_size = (size_t) encrypted_size_out1 + (size_t) encrypted_size_out2;
461
462 r = base64mem(salt, sizeof(salt), &salt_base64);
463 if (r < 0)
464 return log_oom();
465
466 r = base64mem(encrypted, encrypted_size, &encrypted_base64);
467 if (r < 0)
468 return log_oom();
469
470 joined = strjoin(salt_base64, ":", encrypted_base64);
471 if (!joined)
472 return log_oom();
473
474 xsprintf(label, "trusted.fscrypt_slot%" PRIu32, nr);
475 if (fsetxattr(root_fd, label, joined, strlen(joined), 0) < 0)
476 return log_error_errno(errno, "Failed to write xattr %s: %m", label);
477
478 log_info("Written key slot %s.", label);
479
480 return 0;
481
482 finish:
483 explicit_bzero_safe(derived, sizeof(derived));
484 return r;
485 }
486
487 int home_create_fscrypt(
488 UserRecord *h,
489 HomeSetup *setup,
490 char **effective_passwords,
491 UserRecord **ret_home) {
492
493 _cleanup_(rm_rf_physical_and_freep) char *temporary = NULL;
494 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
495 _cleanup_(erase_and_freep) void *volume_key = NULL;
496 _cleanup_close_ int mount_fd = -1;
497 struct fscrypt_policy policy = {};
498 size_t volume_key_size = 512 / 8;
499 _cleanup_free_ char *d = NULL;
500 uint32_t nr = 0;
501 const char *ip;
502 char **i;
503 int r;
504
505 assert(h);
506 assert(user_record_storage(h) == USER_FSCRYPT);
507 assert(setup);
508 assert(ret_home);
509
510 assert_se(ip = user_record_image_path(h));
511
512 r = tempfn_random(ip, "homework", &d);
513 if (r < 0)
514 return log_error_errno(r, "Failed to allocate temporary directory: %m");
515
516 (void) mkdir_parents(d, 0755);
517
518 if (mkdir(d, 0700) < 0)
519 return log_error_errno(errno, "Failed to create temporary home directory %s: %m", d);
520
521 temporary = TAKE_PTR(d); /* Needs to be destroyed now */
522
523 r = home_unshare_and_mkdir();
524 if (r < 0)
525 return r;
526
527 setup->root_fd = open(temporary, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
528 if (setup->root_fd < 0)
529 return log_error_errno(errno, "Failed to open temporary home directory: %m");
530
531 if (ioctl(setup->root_fd, FS_IOC_GET_ENCRYPTION_POLICY, &policy) < 0) {
532 if (ERRNO_IS_NOT_SUPPORTED(errno)) {
533 log_error_errno(errno, "File system does not support fscrypt: %m");
534 return -ENOLINK; /* make recognizable */
535 }
536 if (errno != ENODATA)
537 return log_error_errno(errno, "Failed to get fscrypt policy of directory: %m");
538 } else
539 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Parent of %s already encrypted, refusing.", d);
540
541 volume_key = malloc(volume_key_size);
542 if (!volume_key)
543 return log_oom();
544
545 r = genuine_random_bytes(volume_key, volume_key_size, RANDOM_BLOCK);
546 if (r < 0)
547 return log_error_errno(r, "Failed to acquire volume key: %m");
548
549 log_info("Generated volume key of size %zu.", volume_key_size);
550
551 policy = (struct fscrypt_policy) {
552 .contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS,
553 .filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS,
554 .flags = FS_POLICY_FLAGS_PAD_32,
555 };
556
557 calculate_key_descriptor(volume_key, volume_key_size, policy.master_key_descriptor);
558
559 r = fscrypt_upload_volume_key(policy.master_key_descriptor, volume_key, volume_key_size, KEY_SPEC_THREAD_KEYRING);
560 if (r < 0)
561 return r;
562
563 log_info("Uploaded volume key to kernel.");
564
565 if (ioctl(setup->root_fd, FS_IOC_SET_ENCRYPTION_POLICY, &policy) < 0)
566 return log_error_errno(errno, "Failed to set fscrypt policy on directory: %m");
567
568 log_info("Encryption policy set.");
569
570 STRV_FOREACH(i, effective_passwords) {
571 r = fscrypt_slot_set(setup->root_fd, volume_key, volume_key_size, *i, nr);
572 if (r < 0)
573 return r;
574
575 nr++;
576 }
577
578 (void) home_update_quota_classic(h, temporary);
579
580 r = home_shift_uid(setup->root_fd, HOME_RUNTIME_WORK_DIR, h->uid, h->uid, &mount_fd);
581 if (r > 0)
582 setup->undo_mount = true; /* If uidmaps worked we have a mount to undo again */
583
584 if (mount_fd >= 0) {
585 /* If we have established a new mount, then we can use that as new root fd to our home directory. */
586 safe_close(setup->root_fd);
587
588 setup->root_fd = fd_reopen(mount_fd, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
589 if (setup->root_fd < 0)
590 return log_error_errno(setup->root_fd, "Unable to convert mount fd into proper directory fd: %m");
591
592 mount_fd = safe_close(mount_fd);
593 }
594
595 r = home_populate(h, setup->root_fd);
596 if (r < 0)
597 return r;
598
599 r = home_sync_and_statfs(setup->root_fd, NULL);
600 if (r < 0)
601 return r;
602
603 r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_PERMISSIVE, &new_home);
604 if (r < 0)
605 return log_error_errno(r, "Failed to clone record: %m");
606
607 r = user_record_add_binding(
608 new_home,
609 USER_FSCRYPT,
610 ip,
611 SD_ID128_NULL,
612 SD_ID128_NULL,
613 SD_ID128_NULL,
614 NULL,
615 NULL,
616 UINT64_MAX,
617 NULL,
618 NULL,
619 h->uid,
620 (gid_t) h->uid);
621 if (r < 0)
622 return log_error_errno(r, "Failed to add binding to record: %m");
623
624 setup->root_fd = safe_close(setup->root_fd);
625
626 r = home_setup_undo_mount(setup, LOG_ERR);
627 if (r < 0)
628 return r;
629
630 if (rename(temporary, ip) < 0)
631 return log_error_errno(errno, "Failed to rename %s to %s: %m", temporary, ip);
632
633 temporary = mfree(temporary);
634
635 log_info("Everything completed.");
636
637 *ret_home = TAKE_PTR(new_home);
638 return 0;
639 }
640
641 int home_passwd_fscrypt(
642 UserRecord *h,
643 HomeSetup *setup,
644 const PasswordCache *cache, /* the passwords acquired via PKCS#11/FIDO2 security tokens */
645 char **effective_passwords /* new passwords */) {
646
647 _cleanup_(erase_and_freep) void *volume_key = NULL;
648 _cleanup_free_ char *xattr_buf = NULL;
649 size_t volume_key_size = 0;
650 uint32_t slot = 0;
651 const char *xa;
652 char **p;
653 int r;
654
655 assert(h);
656 assert(user_record_storage(h) == USER_FSCRYPT);
657 assert(setup);
658
659 r = fscrypt_setup(
660 cache,
661 h->password,
662 setup,
663 &volume_key,
664 &volume_key_size);
665 if (r < 0)
666 return r;
667
668 STRV_FOREACH(p, effective_passwords) {
669 r = fscrypt_slot_set(setup->root_fd, volume_key, volume_key_size, *p, slot);
670 if (r < 0)
671 return r;
672
673 slot++;
674 }
675
676 r = flistxattr_malloc(setup->root_fd, &xattr_buf);
677 if (r < 0)
678 return log_error_errno(errno, "Failed to retrieve xattr list: %m");
679
680 NULSTR_FOREACH(xa, xattr_buf) {
681 const char *nr;
682 uint32_t z;
683
684 /* Check if this xattr has the format 'trusted.fscrypt_slot<nr>' where '<nr>' is a 32bit unsigned integer */
685 nr = startswith(xa, "trusted.fscrypt_slot");
686 if (!nr)
687 continue;
688 if (safe_atou32(nr, &z) < 0)
689 continue;
690
691 if (z < slot)
692 continue;
693
694 if (fremovexattr(setup->root_fd, xa) < 0)
695 if (errno != ENODATA)
696 log_warning_errno(errno, "Failed to remove xattr %s: %m", xa);
697 }
698
699 return 0;
700 }