]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homework-fscrypt.c
homework-fscrypt: pass the right error variable
[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 CLEANUP_ERASE(key);
62
63 /* Upload to the kernel */
64 serial = add_key("logon", description, &key, sizeof(key), where);
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 CLEANUP_ERASE(derived);
128
129 if (PKCS5_PBKDF2_HMAC(
130 password, strlen(password),
131 salt, salt_size,
132 0xFFFF, EVP_sha512(),
133 sizeof(derived), derived) != 1)
134 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "PBKDF2 failed.");
135
136 context = EVP_CIPHER_CTX_new();
137 if (!context)
138 return log_oom();
139
140 /* We use AES256 in counter mode */
141 assert_se(cc = EVP_aes_256_ctr());
142
143 /* We only use the first half of the derived key */
144 assert(sizeof(derived) >= (size_t) EVP_CIPHER_key_length(cc));
145
146 if (EVP_DecryptInit_ex(context, cc, NULL, derived, NULL) != 1)
147 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize decryption context.");
148
149 decrypted_size = encrypted_size + EVP_CIPHER_key_length(cc) * 2;
150 decrypted = malloc(decrypted_size);
151 if (!decrypted)
152 return log_oom();
153
154 if (EVP_DecryptUpdate(context, (uint8_t*) decrypted, &decrypted_size_out1, encrypted, encrypted_size) != 1)
155 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to decrypt volume key.");
156
157 assert((size_t) decrypted_size_out1 <= decrypted_size);
158
159 if (EVP_DecryptFinal_ex(context, (uint8_t*) decrypted_size + decrypted_size_out1, &decrypted_size_out2) != 1)
160 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finish decryption of volume key.");
161
162 assert((size_t) decrypted_size_out1 + (size_t) decrypted_size_out2 < decrypted_size);
163 decrypted_size = (size_t) decrypted_size_out1 + (size_t) decrypted_size_out2;
164
165 calculate_key_descriptor(decrypted, decrypted_size, key_descriptor);
166
167 if (memcmp(key_descriptor, match_key_descriptor, FS_KEY_DESCRIPTOR_SIZE) != 0)
168 return -ENOANO; /* don't log here */
169
170 r = fscrypt_upload_volume_key(key_descriptor, decrypted, decrypted_size, KEY_SPEC_THREAD_KEYRING);
171 if (r < 0)
172 return r;
173
174 if (ret_decrypted)
175 *ret_decrypted = TAKE_PTR(decrypted);
176 if (ret_decrypted_size)
177 *ret_decrypted_size = decrypted_size;
178
179 return 0;
180 }
181
182 static int fscrypt_slot_try_many(
183 char **passwords,
184 const void *salt, size_t salt_size,
185 const void *encrypted, size_t encrypted_size,
186 const uint8_t match_key_descriptor[static FS_KEY_DESCRIPTOR_SIZE],
187 void **ret_decrypted, size_t *ret_decrypted_size) {
188
189 int r;
190
191 STRV_FOREACH(i, passwords) {
192 r = fscrypt_slot_try_one(*i, salt, salt_size, encrypted, encrypted_size, match_key_descriptor, ret_decrypted, ret_decrypted_size);
193 if (r != -ENOANO)
194 return r;
195 }
196
197 return -ENOANO;
198 }
199
200 static int fscrypt_setup(
201 const PasswordCache *cache,
202 char **password,
203 HomeSetup *setup,
204 void **ret_volume_key,
205 size_t *ret_volume_key_size) {
206
207 _cleanup_free_ char *xattr_buf = NULL;
208 int r;
209
210 assert(setup);
211 assert(setup->root_fd >= 0);
212
213 r = flistxattr_malloc(setup->root_fd, &xattr_buf);
214 if (r < 0)
215 return log_error_errno(r, "Failed to retrieve xattr list: %m");
216
217 NULSTR_FOREACH(xa, xattr_buf) {
218 _cleanup_free_ void *salt = NULL, *encrypted = NULL;
219 _cleanup_free_ char *value = NULL;
220 size_t salt_size, encrypted_size;
221 const char *nr, *e;
222 int n;
223
224 /* Check if this xattr has the format 'trusted.fscrypt_slot<nr>' where '<nr>' is a 32-bit unsigned integer */
225 nr = startswith(xa, "trusted.fscrypt_slot");
226 if (!nr)
227 continue;
228 if (safe_atou32(nr, NULL) < 0)
229 continue;
230
231 n = fgetxattr_malloc(setup->root_fd, xa, &value);
232 if (n == -ENODATA) /* deleted by now? */
233 continue;
234 if (n < 0)
235 return log_error_errno(n, "Failed to read %s xattr: %m", xa);
236
237 e = memchr(value, ':', n);
238 if (!e)
239 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "xattr %s lacks ':' separator.", xa);
240
241 r = unbase64mem_full(value, e - value, /* secure = */ false, &salt, &salt_size);
242 if (r < 0)
243 return log_error_errno(r, "Failed to decode salt of %s: %m", xa);
244
245 r = unbase64mem_full(e + 1, n - (e - value) - 1, /* secure = */ false, &encrypted, &encrypted_size);
246 if (r < 0)
247 return log_error_errno(r, "Failed to decode encrypted key of %s: %m", xa);
248
249 r = -ENOANO;
250 char **list;
251 FOREACH_ARGUMENT(list, cache->pkcs11_passwords, cache->fido2_passwords, password) {
252 r = fscrypt_slot_try_many(
253 list,
254 salt, salt_size,
255 encrypted, encrypted_size,
256 setup->fscrypt_key_descriptor,
257 ret_volume_key, ret_volume_key_size);
258 if (r >= 0)
259 return 0;
260 if (r != -ENOANO)
261 return r;
262 }
263 }
264
265 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Failed to set up home directory with provided passwords.");
266 }
267
268 int home_setup_fscrypt(
269 UserRecord *h,
270 HomeSetup *setup,
271 const PasswordCache *cache) {
272
273 _cleanup_(erase_and_freep) void *volume_key = NULL;
274 struct fscrypt_policy policy = {};
275 size_t volume_key_size = 0;
276 const char *ip;
277 int r;
278
279 assert(h);
280 assert(user_record_storage(h) == USER_FSCRYPT);
281 assert(setup);
282 assert(setup->root_fd < 0);
283
284 assert_se(ip = user_record_image_path(h));
285
286 setup->root_fd = open(ip, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
287 if (setup->root_fd < 0)
288 return log_error_errno(errno, "Failed to open home directory: %m");
289
290 if (ioctl(setup->root_fd, FS_IOC_GET_ENCRYPTION_POLICY, &policy) < 0) {
291 if (errno == ENODATA)
292 return log_error_errno(errno, "Home directory %s is not encrypted.", ip);
293 if (ERRNO_IS_NOT_SUPPORTED(errno)) {
294 log_error_errno(errno, "File system does not support fscrypt: %m");
295 return -ENOLINK; /* make recognizable */
296 }
297 return log_error_errno(errno, "Failed to acquire encryption policy of %s: %m", ip);
298 }
299
300 memcpy(setup->fscrypt_key_descriptor, policy.master_key_descriptor, FS_KEY_DESCRIPTOR_SIZE);
301
302 r = fscrypt_setup(
303 cache,
304 h->password,
305 setup,
306 &volume_key,
307 &volume_key_size);
308 if (r < 0)
309 return r;
310
311 /* Also install the access key in the user's own keyring */
312
313 if (uid_is_valid(h->uid)) {
314 r = safe_fork("(sd-addkey)",
315 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_REOPEN_LOG,
316 NULL);
317 if (r < 0)
318 return log_error_errno(r, "Failed install encryption key in user's keyring: %m");
319 if (r == 0) {
320 /* Child */
321
322 r = fully_set_uid_gid(h->uid, user_record_gid(h), /* supplementary_gids= */ NULL, /* n_supplementary_gids= */ 0);
323 if (r < 0) {
324 log_error_errno(r, "Failed to change UID/GID to " UID_FMT "/" GID_FMT ": %m", h->uid, user_record_gid(h));
325 _exit(EXIT_FAILURE);
326 }
327
328 r = fscrypt_upload_volume_key(
329 setup->fscrypt_key_descriptor,
330 volume_key,
331 volume_key_size,
332 KEY_SPEC_USER_KEYRING);
333 if (r < 0)
334 _exit(EXIT_FAILURE);
335
336 _exit(EXIT_SUCCESS);
337 }
338 }
339
340 /* We'll bind mount the image directory to a new mount point where we'll start adjusting it. Only
341 * once that's complete we'll move the thing to its final place eventually. */
342 r = home_unshare_and_mkdir();
343 if (r < 0)
344 return r;
345
346 r = mount_follow_verbose(LOG_ERR, ip, HOME_RUNTIME_WORK_DIR, NULL, MS_BIND, NULL);
347 if (r < 0)
348 return r;
349
350 setup->undo_mount = true;
351
352 /* Turn off any form of propagation for this */
353 r = mount_nofollow_verbose(LOG_ERR, NULL, HOME_RUNTIME_WORK_DIR, NULL, MS_PRIVATE, NULL);
354 if (r < 0)
355 return r;
356
357 /* Adjust MS_SUID and similar flags */
358 r = mount_nofollow_verbose(LOG_ERR, NULL, HOME_RUNTIME_WORK_DIR, NULL, MS_BIND|MS_REMOUNT|user_record_mount_flags(h), NULL);
359 if (r < 0)
360 return r;
361
362 safe_close(setup->root_fd);
363 setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
364 if (setup->root_fd < 0)
365 return log_error_errno(errno, "Failed to open home directory: %m");
366
367 return 0;
368 }
369
370 static int fscrypt_slot_set(
371 int root_fd,
372 const void *volume_key,
373 size_t volume_key_size,
374 const char *password,
375 uint32_t nr) {
376
377 _cleanup_free_ char *salt_base64 = NULL, *encrypted_base64 = NULL, *joined = NULL;
378 char label[STRLEN("trusted.fscrypt_slot") + DECIMAL_STR_MAX(nr) + 1];
379 _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL;
380 int r, encrypted_size_out1, encrypted_size_out2;
381 uint8_t salt[64], derived[512 / 8] = {};
382 _cleanup_free_ void *encrypted = NULL;
383 const EVP_CIPHER *cc;
384 size_t encrypted_size;
385 ssize_t ss;
386
387 r = crypto_random_bytes(salt, sizeof(salt));
388 if (r < 0)
389 return log_error_errno(r, "Failed to generate salt: %m");
390
391 CLEANUP_ERASE(derived);
392
393 if (PKCS5_PBKDF2_HMAC(
394 password, strlen(password),
395 salt, sizeof(salt),
396 0xFFFF, EVP_sha512(),
397 sizeof(derived), derived) != 1)
398 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "PBKDF2 failed");
399
400 context = EVP_CIPHER_CTX_new();
401 if (!context)
402 return log_oom();
403
404 /* We use AES256 in counter mode */
405 cc = EVP_aes_256_ctr();
406
407 /* We only use the first half of the derived key */
408 assert(sizeof(derived) >= (size_t) EVP_CIPHER_key_length(cc));
409
410 if (EVP_EncryptInit_ex(context, cc, NULL, derived, NULL) != 1)
411 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize encryption context.");
412
413 encrypted_size = volume_key_size + EVP_CIPHER_key_length(cc) * 2;
414 encrypted = malloc(encrypted_size);
415 if (!encrypted)
416 return log_oom();
417
418 if (EVP_EncryptUpdate(context, (uint8_t*) encrypted, &encrypted_size_out1, volume_key, volume_key_size) != 1)
419 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to encrypt volume key.");
420
421 assert((size_t) encrypted_size_out1 <= encrypted_size);
422
423 if (EVP_EncryptFinal_ex(context, (uint8_t*) encrypted_size + encrypted_size_out1, &encrypted_size_out2) != 1)
424 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finish encryption of volume key.");
425
426 assert((size_t) encrypted_size_out1 + (size_t) encrypted_size_out2 < encrypted_size);
427 encrypted_size = (size_t) encrypted_size_out1 + (size_t) encrypted_size_out2;
428
429 ss = base64mem(salt, sizeof(salt), &salt_base64);
430 if (ss < 0)
431 return log_oom();
432
433 ss = base64mem(encrypted, encrypted_size, &encrypted_base64);
434 if (ss < 0)
435 return log_oom();
436
437 joined = strjoin(salt_base64, ":", encrypted_base64);
438 if (!joined)
439 return log_oom();
440
441 xsprintf(label, "trusted.fscrypt_slot%" PRIu32, nr);
442 if (fsetxattr(root_fd, label, joined, strlen(joined), 0) < 0)
443 return log_error_errno(errno, "Failed to write xattr %s: %m", label);
444
445 log_info("Written key slot %s.", label);
446
447 return 0;
448 }
449
450 int home_create_fscrypt(
451 UserRecord *h,
452 HomeSetup *setup,
453 char **effective_passwords,
454 UserRecord **ret_home) {
455
456 _cleanup_(rm_rf_physical_and_freep) char *temporary = NULL;
457 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
458 _cleanup_(erase_and_freep) void *volume_key = NULL;
459 _cleanup_close_ int mount_fd = -EBADF;
460 struct fscrypt_policy policy = {};
461 size_t volume_key_size = 512 / 8;
462 _cleanup_free_ char *d = NULL;
463 uint32_t nr = 0;
464 const char *ip;
465 int r;
466
467 assert(h);
468 assert(user_record_storage(h) == USER_FSCRYPT);
469 assert(setup);
470 assert(ret_home);
471
472 assert_se(ip = user_record_image_path(h));
473
474 r = tempfn_random(ip, "homework", &d);
475 if (r < 0)
476 return log_error_errno(r, "Failed to allocate temporary directory: %m");
477
478 (void) mkdir_parents(d, 0755);
479
480 if (mkdir(d, 0700) < 0)
481 return log_error_errno(errno, "Failed to create temporary home directory %s: %m", d);
482
483 temporary = TAKE_PTR(d); /* Needs to be destroyed now */
484
485 r = home_unshare_and_mkdir();
486 if (r < 0)
487 return r;
488
489 setup->root_fd = open(temporary, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
490 if (setup->root_fd < 0)
491 return log_error_errno(errno, "Failed to open temporary home directory: %m");
492
493 if (ioctl(setup->root_fd, FS_IOC_GET_ENCRYPTION_POLICY, &policy) < 0) {
494 if (ERRNO_IS_NOT_SUPPORTED(errno)) {
495 log_error_errno(errno, "File system does not support fscrypt: %m");
496 return -ENOLINK; /* make recognizable */
497 }
498 if (errno != ENODATA)
499 return log_error_errno(errno, "Failed to get fscrypt policy of directory: %m");
500 } else
501 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Parent of %s already encrypted, refusing.", d);
502
503 volume_key = malloc(volume_key_size);
504 if (!volume_key)
505 return log_oom();
506
507 r = crypto_random_bytes(volume_key, volume_key_size);
508 if (r < 0)
509 return log_error_errno(r, "Failed to acquire volume key: %m");
510
511 log_info("Generated volume key of size %zu.", volume_key_size);
512
513 policy = (struct fscrypt_policy) {
514 .contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS,
515 .filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS,
516 .flags = FS_POLICY_FLAGS_PAD_32,
517 };
518
519 calculate_key_descriptor(volume_key, volume_key_size, policy.master_key_descriptor);
520
521 r = fscrypt_upload_volume_key(policy.master_key_descriptor, volume_key, volume_key_size, KEY_SPEC_THREAD_KEYRING);
522 if (r < 0)
523 return r;
524
525 log_info("Uploaded volume key to kernel.");
526
527 if (ioctl(setup->root_fd, FS_IOC_SET_ENCRYPTION_POLICY, &policy) < 0)
528 return log_error_errno(errno, "Failed to set fscrypt policy on directory: %m");
529
530 log_info("Encryption policy set.");
531
532 STRV_FOREACH(i, effective_passwords) {
533 r = fscrypt_slot_set(setup->root_fd, volume_key, volume_key_size, *i, nr);
534 if (r < 0)
535 return r;
536
537 nr++;
538 }
539
540 (void) home_update_quota_classic(h, temporary);
541
542 r = home_shift_uid(setup->root_fd, HOME_RUNTIME_WORK_DIR, h->uid, h->uid, &mount_fd);
543 if (r > 0)
544 setup->undo_mount = true; /* If uidmaps worked we have a mount to undo again */
545
546 if (mount_fd >= 0) {
547 /* If we have established a new mount, then we can use that as new root fd to our home directory. */
548 safe_close(setup->root_fd);
549
550 setup->root_fd = fd_reopen(mount_fd, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
551 if (setup->root_fd < 0)
552 return log_error_errno(setup->root_fd, "Unable to convert mount fd into proper directory fd: %m");
553
554 mount_fd = safe_close(mount_fd);
555 }
556
557 r = home_populate(h, setup->root_fd);
558 if (r < 0)
559 return r;
560
561 r = home_sync_and_statfs(setup->root_fd, NULL);
562 if (r < 0)
563 return r;
564
565 r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_PERMISSIVE, &new_home);
566 if (r < 0)
567 return log_error_errno(r, "Failed to clone record: %m");
568
569 r = user_record_add_binding(
570 new_home,
571 USER_FSCRYPT,
572 ip,
573 SD_ID128_NULL,
574 SD_ID128_NULL,
575 SD_ID128_NULL,
576 NULL,
577 NULL,
578 UINT64_MAX,
579 NULL,
580 NULL,
581 h->uid,
582 (gid_t) h->uid);
583 if (r < 0)
584 return log_error_errno(r, "Failed to add binding to record: %m");
585
586 setup->root_fd = safe_close(setup->root_fd);
587
588 r = home_setup_undo_mount(setup, LOG_ERR);
589 if (r < 0)
590 return r;
591
592 if (rename(temporary, ip) < 0)
593 return log_error_errno(errno, "Failed to rename %s to %s: %m", temporary, ip);
594
595 temporary = mfree(temporary);
596
597 log_info("Everything completed.");
598
599 *ret_home = TAKE_PTR(new_home);
600 return 0;
601 }
602
603 int home_passwd_fscrypt(
604 UserRecord *h,
605 HomeSetup *setup,
606 const PasswordCache *cache, /* the passwords acquired via PKCS#11/FIDO2 security tokens */
607 char **effective_passwords /* new passwords */) {
608
609 _cleanup_(erase_and_freep) void *volume_key = NULL;
610 _cleanup_free_ char *xattr_buf = NULL;
611 size_t volume_key_size = 0;
612 uint32_t slot = 0;
613 int r;
614
615 assert(h);
616 assert(user_record_storage(h) == USER_FSCRYPT);
617 assert(setup);
618
619 r = fscrypt_setup(
620 cache,
621 h->password,
622 setup,
623 &volume_key,
624 &volume_key_size);
625 if (r < 0)
626 return r;
627
628 STRV_FOREACH(p, effective_passwords) {
629 r = fscrypt_slot_set(setup->root_fd, volume_key, volume_key_size, *p, slot);
630 if (r < 0)
631 return r;
632
633 slot++;
634 }
635
636 r = flistxattr_malloc(setup->root_fd, &xattr_buf);
637 if (r < 0)
638 return log_error_errno(r, "Failed to retrieve xattr list: %m");
639
640 NULSTR_FOREACH(xa, xattr_buf) {
641 const char *nr;
642 uint32_t z;
643
644 /* Check if this xattr has the format 'trusted.fscrypt_slot<nr>' where '<nr>' is a 32-bit unsigned integer */
645 nr = startswith(xa, "trusted.fscrypt_slot");
646 if (!nr)
647 continue;
648 if (safe_atou32(nr, &z) < 0)
649 continue;
650
651 if (z < slot)
652 continue;
653
654 if (fremovexattr(setup->root_fd, xa) < 0)
655 if (errno != ENODATA)
656 log_warning_errno(errno, "Failed to remove xattr %s: %m", xa);
657 }
658
659 return 0;
660 }