1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <openssl/evp.h>
5 #include <openssl/sha.h>
9 #include "errno-util.h"
11 #include "hexdecoct.h"
12 #include "homework-fscrypt.h"
13 #include "homework-quota.h"
14 #include "memory-util.h"
15 #include "missing_keyctl.h"
16 #include "missing_syscall.h"
18 #include "nulstr-util.h"
19 #include "openssl-util.h"
20 #include "parse-util.h"
21 #include "process-util.h"
22 #include "random-util.h"
24 #include "stdio-util.h"
26 #include "tmpfile-util.h"
27 #include "user-util.h"
28 #include "xattr-util.h"
30 static int fscrypt_upload_volume_key(
31 const uint8_t key_descriptor
[static FS_KEY_DESCRIPTOR_SIZE
],
32 const void *volume_key
,
33 size_t volume_key_size
,
36 _cleanup_free_
char *hex
= NULL
;
37 const char *description
;
38 struct fscrypt_key key
;
41 assert(key_descriptor
);
43 assert(volume_key_size
> 0);
45 if (volume_key_size
> sizeof(key
.raw
))
46 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Volume key too long.");
48 hex
= hexmem(key_descriptor
, FS_KEY_DESCRIPTOR_SIZE
);
52 description
= strjoina("fscrypt:", hex
);
54 key
= (struct fscrypt_key
) {
55 .size
= volume_key_size
,
57 memcpy(key
.raw
, volume_key
, volume_key_size
);
59 /* Upload to the kernel */
60 serial
= add_key("logon", description
, &key
, sizeof(key
), where
);
61 explicit_bzero_safe(&key
, sizeof(key
));
64 return log_error_errno(errno
, "Failed to install master key in keyring: %m");
66 log_info("Uploaded encryption key to kernel.");
71 static void calculate_key_descriptor(
74 uint8_t ret_key_descriptor
[static FS_KEY_DESCRIPTOR_SIZE
]) {
76 uint8_t hashed
[512 / 8] = {}, hashed2
[512 / 8] = {};
78 /* Derive the key descriptor from the volume key via double SHA512, in order to be compatible with e4crypt */
80 assert_se(SHA512(key
, key_size
, hashed
) == hashed
);
81 assert_se(SHA512(hashed
, sizeof(hashed
), hashed2
) == hashed2
);
83 assert_cc(sizeof(hashed2
) >= FS_KEY_DESCRIPTOR_SIZE
);
85 memcpy(ret_key_descriptor
, hashed2
, FS_KEY_DESCRIPTOR_SIZE
);
88 static int fscrypt_slot_try_one(
90 const void *salt
, size_t salt_size
,
91 const void *encrypted
, size_t encrypted_size
,
92 const uint8_t match_key_descriptor
[static FS_KEY_DESCRIPTOR_SIZE
],
93 void **ret_decrypted
, size_t *ret_decrypted_size
) {
96 _cleanup_(EVP_CIPHER_CTX_freep
) EVP_CIPHER_CTX
*context
= NULL
;
97 _cleanup_(erase_and_freep
) void *decrypted
= NULL
;
98 uint8_t key_descriptor
[FS_KEY_DESCRIPTOR_SIZE
];
99 int decrypted_size_out1
, decrypted_size_out2
;
100 uint8_t derived
[512 / 8] = {};
101 size_t decrypted_size
;
102 const EVP_CIPHER
*cc
;
107 assert(salt_size
> 0);
109 assert(encrypted_size
> 0);
110 assert(match_key_descriptor
);
112 /* Our construction is like this:
114 * 1. In each key slot we store a salt value plus the encrypted volume key
116 * 2. Unlocking is via calculating PBKDF2-HMAC-SHA512 of the supplied password (in combination with
117 * the salt), then using the first 256 bit of the hash as key for decrypting the encrypted
118 * volume key in AES256 counter mode.
120 * 3. Writing a password is similar: calculate PBKDF2-HMAC-SHA512 of the supplied password (in
121 * combination with the salt), then encrypt the volume key in AES256 counter mode with the
125 if (PKCS5_PBKDF2_HMAC(
126 password
, strlen(password
),
128 0xFFFF, EVP_sha512(),
129 sizeof(derived
), derived
) != 1) {
130 r
= log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "PBKDF2 failed");
134 context
= EVP_CIPHER_CTX_new();
140 /* We use AES256 in counter mode */
141 assert_se(cc
= EVP_aes_256_ctr());
143 /* We only use the first half of the derived key */
144 assert(sizeof(derived
) >= (size_t) EVP_CIPHER_key_length(cc
));
146 if (EVP_DecryptInit_ex(context
, cc
, NULL
, derived
, NULL
) != 1) {
147 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to initialize decryption context.");
151 /* Flush out the derived key now, we don't need it anymore */
152 explicit_bzero_safe(derived
, sizeof(derived
));
154 decrypted_size
= encrypted_size
+ EVP_CIPHER_key_length(cc
) * 2;
155 decrypted
= malloc(decrypted_size
);
159 if (EVP_DecryptUpdate(context
, (uint8_t*) decrypted
, &decrypted_size_out1
, encrypted
, encrypted_size
) != 1)
160 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to decrypt volume key.");
162 assert((size_t) decrypted_size_out1
<= decrypted_size
);
164 if (EVP_DecryptFinal_ex(context
, (uint8_t*) decrypted_size
+ decrypted_size_out1
, &decrypted_size_out2
) != 1)
165 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to finish decryption of volume key.");
167 assert((size_t) decrypted_size_out1
+ (size_t) decrypted_size_out2
< decrypted_size
);
168 decrypted_size
= (size_t) decrypted_size_out1
+ (size_t) decrypted_size_out2
;
170 calculate_key_descriptor(decrypted
, decrypted_size
, key_descriptor
);
172 if (memcmp(key_descriptor
, match_key_descriptor
, FS_KEY_DESCRIPTOR_SIZE
) != 0)
173 return -ENOANO
; /* don't log here */
175 r
= fscrypt_upload_volume_key(key_descriptor
, decrypted
, decrypted_size
, KEY_SPEC_THREAD_KEYRING
);
180 *ret_decrypted
= TAKE_PTR(decrypted
);
181 if (ret_decrypted_size
)
182 *ret_decrypted_size
= decrypted_size
;
187 explicit_bzero_safe(derived
, sizeof(derived
));
191 static int fscrypt_slot_try_many(
193 const void *salt
, size_t salt_size
,
194 const void *encrypted
, size_t encrypted_size
,
195 const uint8_t match_key_descriptor
[static FS_KEY_DESCRIPTOR_SIZE
],
196 void **ret_decrypted
, size_t *ret_decrypted_size
) {
201 STRV_FOREACH(i
, passwords
) {
202 r
= fscrypt_slot_try_one(*i
, salt
, salt_size
, encrypted
, encrypted_size
, match_key_descriptor
, ret_decrypted
, ret_decrypted_size
);
210 static int fscrypt_setup(
211 const PasswordCache
*cache
,
214 void **ret_volume_key
,
215 size_t *ret_volume_key_size
) {
217 _cleanup_free_
char *xattr_buf
= NULL
;
222 assert(setup
->root_fd
>= 0);
224 r
= flistxattr_malloc(setup
->root_fd
, &xattr_buf
);
226 return log_error_errno(errno
, "Failed to retrieve xattr list: %m");
228 NULSTR_FOREACH(xa
, xattr_buf
) {
229 _cleanup_free_
void *salt
= NULL
, *encrypted
= NULL
;
230 _cleanup_free_
char *value
= NULL
;
231 size_t salt_size
, encrypted_size
;
236 /* Check if this xattr has the format 'trusted.fscrypt_slot<nr>' where '<nr>' is a 32bit unsigned integer */
237 nr
= startswith(xa
, "trusted.fscrypt_slot");
240 if (safe_atou32(nr
, NULL
) < 0)
243 n
= fgetxattr_malloc(setup
->root_fd
, xa
, &value
);
244 if (n
== -ENODATA
) /* deleted by now? */
247 return log_error_errno(n
, "Failed to read %s xattr: %m", xa
);
249 e
= memchr(value
, ':', n
);
251 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "xattr %s lacks ':' separator: %m", xa
);
253 r
= unbase64mem(value
, e
- value
, &salt
, &salt_size
);
255 return log_error_errno(r
, "Failed to decode salt of %s: %m", xa
);
256 r
= unbase64mem(e
+1, n
- (e
- value
) - 1, &encrypted
, &encrypted_size
);
258 return log_error_errno(r
, "Failed to decode encrypted key of %s: %m", xa
);
261 FOREACH_POINTER(list
, cache
->pkcs11_passwords
, cache
->fido2_passwords
, password
) {
262 r
= fscrypt_slot_try_many(
265 encrypted
, encrypted_size
,
266 setup
->fscrypt_key_descriptor
,
267 ret_volume_key
, ret_volume_key_size
);
278 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Failed to set up home directory with provided passwords.");
281 int home_prepare_fscrypt(
283 bool already_activated
,
284 PasswordCache
*cache
,
287 _cleanup_(erase_and_freep
) void *volume_key
= NULL
;
288 struct fscrypt_policy policy
= {};
289 size_t volume_key_size
= 0;
295 assert(user_record_storage(h
) == USER_FSCRYPT
);
297 assert_se(ip
= user_record_image_path(h
));
299 setup
->root_fd
= open(ip
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
300 if (setup
->root_fd
< 0)
301 return log_error_errno(errno
, "Failed to open home directory: %m");
303 if (ioctl(setup
->root_fd
, FS_IOC_GET_ENCRYPTION_POLICY
, &policy
) < 0) {
304 if (errno
== ENODATA
)
305 return log_error_errno(errno
, "Home directory %s is not encrypted.", ip
);
306 if (ERRNO_IS_NOT_SUPPORTED(errno
)) {
307 log_error_errno(errno
, "File system does not support fscrypt: %m");
308 return -ENOLINK
; /* make recognizable */
310 return log_error_errno(errno
, "Failed to acquire encryption policy of %s: %m", ip
);
313 memcpy(setup
->fscrypt_key_descriptor
, policy
.master_key_descriptor
, FS_KEY_DESCRIPTOR_SIZE
);
324 /* Also install the access key in the user's own keyring */
326 if (uid_is_valid(h
->uid
)) {
327 r
= safe_fork("(sd-addkey)",
328 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
|FORK_REOPEN_LOG
,
331 return log_error_errno(r
, "Failed install encryption key in user's keyring: %m");
337 gid
= user_record_gid(h
);
338 if (setresgid(gid
, gid
, gid
) < 0) {
339 log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
343 if (setgroups(0, NULL
) < 0) {
344 log_error_errno(errno
, "Failed to reset auxiliary groups list: %m");
348 if (setresuid(h
->uid
, h
->uid
, h
->uid
) < 0) {
349 log_error_errno(errno
, "Failed to change UID to " UID_FMT
": %m", h
->uid
);
353 r
= fscrypt_upload_volume_key(
354 setup
->fscrypt_key_descriptor
,
357 KEY_SPEC_USER_KEYRING
);
368 static int fscrypt_slot_set(
370 const void *volume_key
,
371 size_t volume_key_size
,
372 const char *password
,
375 _cleanup_free_
char *salt_base64
= NULL
, *encrypted_base64
= NULL
, *joined
= NULL
;
376 char label
[STRLEN("trusted.fscrypt_slot") + DECIMAL_STR_MAX(nr
) + 1];
377 _cleanup_(EVP_CIPHER_CTX_freep
) EVP_CIPHER_CTX
*context
= NULL
;
378 int r
, encrypted_size_out1
, encrypted_size_out2
;
379 uint8_t salt
[64], derived
[512 / 8] = {};
380 _cleanup_free_
void *encrypted
= NULL
;
381 const EVP_CIPHER
*cc
;
382 size_t encrypted_size
;
384 r
= genuine_random_bytes(salt
, sizeof(salt
), RANDOM_BLOCK
);
386 return log_error_errno(r
, "Failed to generate salt: %m");
388 if (PKCS5_PBKDF2_HMAC(
389 password
, strlen(password
),
391 0xFFFF, EVP_sha512(),
392 sizeof(derived
), derived
) != 1) {
393 r
= log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "PBKDF2 failed");
397 context
= EVP_CIPHER_CTX_new();
403 /* We use AES256 in counter mode */
404 cc
= EVP_aes_256_ctr();
406 /* We only use the first half of the derived key */
407 assert(sizeof(derived
) >= (size_t) EVP_CIPHER_key_length(cc
));
409 if (EVP_EncryptInit_ex(context
, cc
, NULL
, derived
, NULL
) != 1) {
410 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to initialize encryption context.");
414 /* Flush out the derived key now, we don't need it anymore */
415 explicit_bzero_safe(derived
, sizeof(derived
));
417 encrypted_size
= volume_key_size
+ EVP_CIPHER_key_length(cc
) * 2;
418 encrypted
= malloc(encrypted_size
);
422 if (EVP_EncryptUpdate(context
, (uint8_t*) encrypted
, &encrypted_size_out1
, volume_key
, volume_key_size
) != 1)
423 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to encrypt volume key.");
425 assert((size_t) encrypted_size_out1
<= encrypted_size
);
427 if (EVP_EncryptFinal_ex(context
, (uint8_t*) encrypted_size
+ encrypted_size_out1
, &encrypted_size_out2
) != 1)
428 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to finish encryption of volume key.");
430 assert((size_t) encrypted_size_out1
+ (size_t) encrypted_size_out2
< encrypted_size
);
431 encrypted_size
= (size_t) encrypted_size_out1
+ (size_t) encrypted_size_out2
;
433 r
= base64mem(salt
, sizeof(salt
), &salt_base64
);
437 r
= base64mem(encrypted
, encrypted_size
, &encrypted_base64
);
441 joined
= strjoin(salt_base64
, ":", encrypted_base64
);
445 xsprintf(label
, "trusted.fscrypt_slot%" PRIu32
, nr
);
446 if (fsetxattr(root_fd
, label
, joined
, strlen(joined
), 0) < 0)
447 return log_error_errno(errno
, "Failed to write xattr %s: %m", label
);
449 log_info("Written key slot %s.", label
);
454 explicit_bzero_safe(derived
, sizeof(derived
));
458 int home_create_fscrypt(
460 char **effective_passwords
,
461 UserRecord
**ret_home
) {
463 _cleanup_(rm_rf_physical_and_freep
) char *temporary
= NULL
;
464 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
;
465 _cleanup_(erase_and_freep
) void *volume_key
= NULL
;
466 struct fscrypt_policy policy
= {};
467 size_t volume_key_size
= 512 / 8;
468 _cleanup_close_
int root_fd
= -1;
469 _cleanup_free_
char *d
= NULL
;
476 assert(user_record_storage(h
) == USER_FSCRYPT
);
479 assert_se(ip
= user_record_image_path(h
));
481 r
= tempfn_random(ip
, "homework", &d
);
483 return log_error_errno(r
, "Failed to allocate temporary directory: %m");
485 (void) mkdir_parents(d
, 0755);
487 if (mkdir(d
, 0700) < 0)
488 return log_error_errno(errno
, "Failed to create temporary home directory %s: %m", d
);
490 temporary
= TAKE_PTR(d
); /* Needs to be destroyed now */
492 root_fd
= open(temporary
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
494 return log_error_errno(errno
, "Failed to open temporary home directory: %m");
496 if (ioctl(root_fd
, FS_IOC_GET_ENCRYPTION_POLICY
, &policy
) < 0) {
497 if (ERRNO_IS_NOT_SUPPORTED(errno
)) {
498 log_error_errno(errno
, "File system does not support fscrypt: %m");
499 return -ENOLINK
; /* make recognizable */
501 if (errno
!= ENODATA
)
502 return log_error_errno(errno
, "Failed to get fscrypt policy of directory: %m");
504 return log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Parent of %s already encrypted, refusing.", d
);
506 volume_key
= malloc(volume_key_size
);
510 r
= genuine_random_bytes(volume_key
, volume_key_size
, RANDOM_BLOCK
);
512 return log_error_errno(r
, "Failed to acquire volume key: %m");
514 log_info("Generated volume key of size %zu.", volume_key_size
);
516 policy
= (struct fscrypt_policy
) {
517 .contents_encryption_mode
= FS_ENCRYPTION_MODE_AES_256_XTS
,
518 .filenames_encryption_mode
= FS_ENCRYPTION_MODE_AES_256_CTS
,
519 .flags
= FS_POLICY_FLAGS_PAD_32
,
522 calculate_key_descriptor(volume_key
, volume_key_size
, policy
.master_key_descriptor
);
524 r
= fscrypt_upload_volume_key(policy
.master_key_descriptor
, volume_key
, volume_key_size
, KEY_SPEC_THREAD_KEYRING
);
528 log_info("Uploaded volume key to kernel.");
530 if (ioctl(root_fd
, FS_IOC_SET_ENCRYPTION_POLICY
, &policy
) < 0)
531 return log_error_errno(errno
, "Failed to set fscrypt policy on directory: %m");
533 log_info("Encryption policy set.");
535 STRV_FOREACH(i
, effective_passwords
) {
536 r
= fscrypt_slot_set(root_fd
, volume_key
, volume_key_size
, *i
, nr
);
543 (void) home_update_quota_classic(h
, temporary
);
545 r
= home_populate(h
, root_fd
);
549 r
= home_sync_and_statfs(root_fd
, NULL
);
553 r
= user_record_clone(h
, USER_RECORD_LOAD_MASK_SECRET
, &new_home
);
555 return log_error_errno(r
, "Failed to clone record: %m");
557 r
= user_record_add_binding(
572 return log_error_errno(r
, "Failed to add binding to record: %m");
574 if (rename(temporary
, ip
) < 0)
575 return log_error_errno(errno
, "Failed to rename %s to %s: %m", temporary
, ip
);
577 temporary
= mfree(temporary
);
579 log_info("Everything completed.");
581 *ret_home
= TAKE_PTR(new_home
);
585 int home_passwd_fscrypt(
588 PasswordCache
*cache
, /* the passwords acquired via PKCS#11/FIDO2 security tokens */
589 char **effective_passwords
/* new passwords */) {
591 _cleanup_(erase_and_freep
) void *volume_key
= NULL
;
592 _cleanup_free_
char *xattr_buf
= NULL
;
593 size_t volume_key_size
= 0;
600 assert(user_record_storage(h
) == USER_FSCRYPT
);
612 STRV_FOREACH(p
, effective_passwords
) {
613 r
= fscrypt_slot_set(setup
->root_fd
, volume_key
, volume_key_size
, *p
, slot
);
620 r
= flistxattr_malloc(setup
->root_fd
, &xattr_buf
);
622 return log_error_errno(errno
, "Failed to retrieve xattr list: %m");
624 NULSTR_FOREACH(xa
, xattr_buf
) {
628 /* Check if this xattr has the format 'trusted.fscrypt_slot<nr>' where '<nr>' is a 32bit unsigned integer */
629 nr
= startswith(xa
, "trusted.fscrypt_slot");
632 if (safe_atou32(nr
, &z
) < 0)
638 if (fremovexattr(setup
->root_fd
, xa
) < 0)
640 if (errno
!= ENODATA
)
641 log_warning_errno(errno
, "Failed to remove xattr %s: %m", xa
);