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