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