1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Filesystem-level keyring for fscrypt
4 *
5 * Copyright 2019 Google LLC
6 */
7
8 /*
9 * This file implements management of fscrypt master keys in the
10 * filesystem-level keyring, including the ioctls:
11 *
12 * - FS_IOC_ADD_ENCRYPTION_KEY
13 * - FS_IOC_REMOVE_ENCRYPTION_KEY
14 * - FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
15 * - FS_IOC_GET_ENCRYPTION_KEY_STATUS
16 *
17 * See the "User API" section of Documentation/filesystems/fscrypt.rst for more
18 * information about these ioctls.
19 */
20
21 #include <crypto/skcipher.h>
22 #include <linux/key-type.h>
23 #include <linux/random.h>
24 #include <linux/seq_file.h>
25
26 #include "fscrypt_private.h"
27
wipe_master_key_secret(struct fscrypt_master_key_secret * secret)28 static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
29 {
30 fscrypt_destroy_hkdf(&secret->hkdf);
31 memzero_explicit(secret, sizeof(*secret));
32 }
33
move_master_key_secret(struct fscrypt_master_key_secret * dst,struct fscrypt_master_key_secret * src)34 static void move_master_key_secret(struct fscrypt_master_key_secret *dst,
35 struct fscrypt_master_key_secret *src)
36 {
37 memcpy(dst, src, sizeof(*dst));
38 memzero_explicit(src, sizeof(*src));
39 }
40
free_master_key(struct fscrypt_master_key * mk)41 static void free_master_key(struct fscrypt_master_key *mk)
42 {
43 size_t i;
44
45 wipe_master_key_secret(&mk->mk_secret);
46
47 for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
48 fscrypt_destroy_prepared_key(&mk->mk_direct_keys[i]);
49 fscrypt_destroy_prepared_key(&mk->mk_iv_ino_lblk_64_keys[i]);
50 fscrypt_destroy_prepared_key(&mk->mk_iv_ino_lblk_32_keys[i]);
51 }
52
53 key_put(mk->mk_users);
54 kzfree(mk);
55 }
56
valid_key_spec(const struct fscrypt_key_specifier * spec)57 static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec)
58 {
59 if (spec->__reserved)
60 return false;
61 return master_key_spec_len(spec) != 0;
62 }
63
fscrypt_key_instantiate(struct key * key,struct key_preparsed_payload * prep)64 static int fscrypt_key_instantiate(struct key *key,
65 struct key_preparsed_payload *prep)
66 {
67 key->payload.data[0] = (struct fscrypt_master_key *)prep->data;
68 return 0;
69 }
70
fscrypt_key_destroy(struct key * key)71 static void fscrypt_key_destroy(struct key *key)
72 {
73 free_master_key(key->payload.data[0]);
74 }
75
fscrypt_key_describe(const struct key * key,struct seq_file * m)76 static void fscrypt_key_describe(const struct key *key, struct seq_file *m)
77 {
78 seq_puts(m, key->description);
79
80 if (key_is_positive(key)) {
81 const struct fscrypt_master_key *mk = key->payload.data[0];
82
83 if (!is_master_key_secret_present(&mk->mk_secret))
84 seq_puts(m, ": secret removed");
85 }
86 }
87
88 /*
89 * Type of key in ->s_master_keys. Each key of this type represents a master
90 * key which has been added to the filesystem. Its payload is a
91 * 'struct fscrypt_master_key'. The "." prefix in the key type name prevents
92 * users from adding keys of this type via the keyrings syscalls rather than via
93 * the intended method of FS_IOC_ADD_ENCRYPTION_KEY.
94 */
95 static struct key_type key_type_fscrypt = {
96 .name = "._fscrypt",
97 .instantiate = fscrypt_key_instantiate,
98 .destroy = fscrypt_key_destroy,
99 .describe = fscrypt_key_describe,
100 };
101
fscrypt_user_key_instantiate(struct key * key,struct key_preparsed_payload * prep)102 static int fscrypt_user_key_instantiate(struct key *key,
103 struct key_preparsed_payload *prep)
104 {
105 /*
106 * We just charge FSCRYPT_MAX_KEY_SIZE bytes to the user's key quota for
107 * each key, regardless of the exact key size. The amount of memory
108 * actually used is greater than the size of the raw key anyway.
109 */
110 return key_payload_reserve(key, FSCRYPT_MAX_KEY_SIZE);
111 }
112
fscrypt_user_key_describe(const struct key * key,struct seq_file * m)113 static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m)
114 {
115 seq_puts(m, key->description);
116 }
117
118 /*
119 * Type of key in ->mk_users. Each key of this type represents a particular
120 * user who has added a particular master key.
121 *
122 * Note that the name of this key type really should be something like
123 * ".fscrypt-user" instead of simply ".fscrypt". But the shorter name is chosen
124 * mainly for simplicity of presentation in /proc/keys when read by a non-root
125 * user. And it is expected to be rare that a key is actually added by multiple
126 * users, since users should keep their encryption keys confidential.
127 */
128 static struct key_type key_type_fscrypt_user = {
129 .name = ".fscrypt",
130 .instantiate = fscrypt_user_key_instantiate,
131 .describe = fscrypt_user_key_describe,
132 };
133
134 /* Search ->s_master_keys or ->mk_users */
search_fscrypt_keyring(struct key * keyring,struct key_type * type,const char * description)135 static struct key *search_fscrypt_keyring(struct key *keyring,
136 struct key_type *type,
137 const char *description)
138 {
139 /*
140 * We need to mark the keyring reference as "possessed" so that we
141 * acquire permission to search it, via the KEY_POS_SEARCH permission.
142 */
143 key_ref_t keyref = make_key_ref(keyring, true /* possessed */);
144
145 keyref = keyring_search(keyref, type, description, false);
146 if (IS_ERR(keyref)) {
147 if (PTR_ERR(keyref) == -EAGAIN || /* not found */
148 PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
149 keyref = ERR_PTR(-ENOKEY);
150 return ERR_CAST(keyref);
151 }
152 return key_ref_to_ptr(keyref);
153 }
154
155 #define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \
156 (CONST_STRLEN("fscrypt-") + FIELD_SIZEOF(struct super_block, s_id))
157
158 #define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1)
159
160 #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE \
161 (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
162 CONST_STRLEN("-users") + 1)
163
164 #define FSCRYPT_MK_USER_DESCRIPTION_SIZE \
165 (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
166
format_fs_keyring_description(char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE],const struct super_block * sb)167 static void format_fs_keyring_description(
168 char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE],
169 const struct super_block *sb)
170 {
171 sprintf(description, "fscrypt-%s", sb->s_id);
172 }
173
format_mk_description(char description[FSCRYPT_MK_DESCRIPTION_SIZE],const struct fscrypt_key_specifier * mk_spec)174 static void format_mk_description(
175 char description[FSCRYPT_MK_DESCRIPTION_SIZE],
176 const struct fscrypt_key_specifier *mk_spec)
177 {
178 sprintf(description, "%*phN",
179 master_key_spec_len(mk_spec), (u8 *)&mk_spec->u);
180 }
181
format_mk_users_keyring_description(char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])182 static void format_mk_users_keyring_description(
183 char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],
184 const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
185 {
186 sprintf(description, "fscrypt-%*phN-users",
187 FSCRYPT_KEY_IDENTIFIER_SIZE, mk_identifier);
188 }
189
format_mk_user_description(char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE],const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])190 static void format_mk_user_description(
191 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE],
192 const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
193 {
194
195 sprintf(description, "%*phN.uid.%u", FSCRYPT_KEY_IDENTIFIER_SIZE,
196 mk_identifier, __kuid_val(current_fsuid()));
197 }
198
199 /* Create ->s_master_keys if needed. Synchronized by fscrypt_add_key_mutex. */
allocate_filesystem_keyring(struct super_block * sb)200 static int allocate_filesystem_keyring(struct super_block *sb)
201 {
202 char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE];
203 struct key *keyring;
204
205 if (sb->s_master_keys)
206 return 0;
207
208 format_fs_keyring_description(description, sb);
209 keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
210 current_cred(), KEY_POS_SEARCH |
211 KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
212 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
213 if (IS_ERR(keyring))
214 return PTR_ERR(keyring);
215
216 /* Pairs with READ_ONCE() in fscrypt_find_master_key() */
217 smp_store_release(&sb->s_master_keys, keyring);
218 return 0;
219 }
220
fscrypt_sb_free(struct super_block * sb)221 void fscrypt_sb_free(struct super_block *sb)
222 {
223 key_put(sb->s_master_keys);
224 sb->s_master_keys = NULL;
225 }
226
227 /*
228 * Find the specified master key in ->s_master_keys.
229 * Returns ERR_PTR(-ENOKEY) if not found.
230 */
fscrypt_find_master_key(struct super_block * sb,const struct fscrypt_key_specifier * mk_spec)231 struct key *fscrypt_find_master_key(struct super_block *sb,
232 const struct fscrypt_key_specifier *mk_spec)
233 {
234 struct key *keyring;
235 char description[FSCRYPT_MK_DESCRIPTION_SIZE];
236
237 /* pairs with smp_store_release() in allocate_filesystem_keyring() */
238 keyring = READ_ONCE(sb->s_master_keys);
239 if (keyring == NULL)
240 return ERR_PTR(-ENOKEY); /* No keyring yet, so no keys yet. */
241
242 format_mk_description(description, mk_spec);
243 return search_fscrypt_keyring(keyring, &key_type_fscrypt, description);
244 }
245
allocate_master_key_users_keyring(struct fscrypt_master_key * mk)246 static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk)
247 {
248 char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE];
249 struct key *keyring;
250
251 format_mk_users_keyring_description(description,
252 mk->mk_spec.u.identifier);
253 keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
254 current_cred(), KEY_POS_SEARCH |
255 KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
256 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
257 if (IS_ERR(keyring))
258 return PTR_ERR(keyring);
259
260 mk->mk_users = keyring;
261 return 0;
262 }
263
264 /*
265 * Find the current user's "key" in the master key's ->mk_users.
266 * Returns ERR_PTR(-ENOKEY) if not found.
267 */
find_master_key_user(struct fscrypt_master_key * mk)268 static struct key *find_master_key_user(struct fscrypt_master_key *mk)
269 {
270 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
271
272 format_mk_user_description(description, mk->mk_spec.u.identifier);
273 return search_fscrypt_keyring(mk->mk_users, &key_type_fscrypt_user,
274 description);
275 }
276
277 /*
278 * Give the current user a "key" in ->mk_users. This charges the user's quota
279 * and marks the master key as added by the current user, so that it cannot be
280 * removed by another user with the key. Either the master key's key->sem must
281 * be held for write, or the master key must be still undergoing initialization.
282 */
add_master_key_user(struct fscrypt_master_key * mk)283 static int add_master_key_user(struct fscrypt_master_key *mk)
284 {
285 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
286 struct key *mk_user;
287 int err;
288
289 format_mk_user_description(description, mk->mk_spec.u.identifier);
290 mk_user = key_alloc(&key_type_fscrypt_user, description,
291 current_fsuid(), current_gid(), current_cred(),
292 KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL);
293 if (IS_ERR(mk_user))
294 return PTR_ERR(mk_user);
295
296 err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL);
297 key_put(mk_user);
298 return err;
299 }
300
301 /*
302 * Remove the current user's "key" from ->mk_users.
303 * The master key's key->sem must be held for write.
304 *
305 * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
306 */
remove_master_key_user(struct fscrypt_master_key * mk)307 static int remove_master_key_user(struct fscrypt_master_key *mk)
308 {
309 struct key *mk_user;
310 int err;
311
312 mk_user = find_master_key_user(mk);
313 if (IS_ERR(mk_user))
314 return PTR_ERR(mk_user);
315 err = key_unlink(mk->mk_users, mk_user);
316 key_put(mk_user);
317 return err;
318 }
319
320 /*
321 * Allocate a new fscrypt_master_key which contains the given secret, set it as
322 * the payload of a new 'struct key' of type fscrypt, and link the 'struct key'
323 * into the given keyring. Synchronized by fscrypt_add_key_mutex.
324 */
add_new_master_key(struct fscrypt_master_key_secret * secret,const struct fscrypt_key_specifier * mk_spec,struct key * keyring)325 static int add_new_master_key(struct fscrypt_master_key_secret *secret,
326 const struct fscrypt_key_specifier *mk_spec,
327 struct key *keyring)
328 {
329 struct fscrypt_master_key *mk;
330 char description[FSCRYPT_MK_DESCRIPTION_SIZE];
331 struct key *key;
332 int err;
333
334 mk = kzalloc(sizeof(*mk), GFP_KERNEL);
335 if (!mk)
336 return -ENOMEM;
337
338 mk->mk_spec = *mk_spec;
339
340 move_master_key_secret(&mk->mk_secret, secret);
341 init_rwsem(&mk->mk_secret_sem);
342
343 refcount_set(&mk->mk_refcount, 1); /* secret is present */
344 INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
345 spin_lock_init(&mk->mk_decrypted_inodes_lock);
346
347 if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
348 err = allocate_master_key_users_keyring(mk);
349 if (err)
350 goto out_free_mk;
351 err = add_master_key_user(mk);
352 if (err)
353 goto out_free_mk;
354 }
355
356 /*
357 * Note that we don't charge this key to anyone's quota, since when
358 * ->mk_users is in use those keys are charged instead, and otherwise
359 * (when ->mk_users isn't in use) only root can add these keys.
360 */
361 format_mk_description(description, mk_spec);
362 key = key_alloc(&key_type_fscrypt, description,
363 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
364 KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW,
365 KEY_ALLOC_NOT_IN_QUOTA, NULL);
366 if (IS_ERR(key)) {
367 err = PTR_ERR(key);
368 goto out_free_mk;
369 }
370 err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL);
371 key_put(key);
372 if (err)
373 goto out_free_mk;
374
375 return 0;
376
377 out_free_mk:
378 free_master_key(mk);
379 return err;
380 }
381
382 #define KEY_DEAD 1
383
add_existing_master_key(struct fscrypt_master_key * mk,struct fscrypt_master_key_secret * secret)384 static int add_existing_master_key(struct fscrypt_master_key *mk,
385 struct fscrypt_master_key_secret *secret)
386 {
387 struct key *mk_user;
388 bool rekey;
389 int err;
390
391 /*
392 * If the current user is already in ->mk_users, then there's nothing to
393 * do. (Not applicable for v1 policy keys, which have NULL ->mk_users.)
394 */
395 if (mk->mk_users) {
396 mk_user = find_master_key_user(mk);
397 if (mk_user != ERR_PTR(-ENOKEY)) {
398 if (IS_ERR(mk_user))
399 return PTR_ERR(mk_user);
400 key_put(mk_user);
401 return 0;
402 }
403 }
404
405 /* If we'll be re-adding ->mk_secret, try to take the reference. */
406 rekey = !is_master_key_secret_present(&mk->mk_secret);
407 if (rekey && !refcount_inc_not_zero(&mk->mk_refcount))
408 return KEY_DEAD;
409
410 /* Add the current user to ->mk_users, if applicable. */
411 if (mk->mk_users) {
412 err = add_master_key_user(mk);
413 if (err) {
414 if (rekey && refcount_dec_and_test(&mk->mk_refcount))
415 return KEY_DEAD;
416 return err;
417 }
418 }
419
420 /* Re-add the secret if needed. */
421 if (rekey) {
422 down_write(&mk->mk_secret_sem);
423 move_master_key_secret(&mk->mk_secret, secret);
424 up_write(&mk->mk_secret_sem);
425 }
426 return 0;
427 }
428
do_add_master_key(struct super_block * sb,struct fscrypt_master_key_secret * secret,const struct fscrypt_key_specifier * mk_spec)429 static int do_add_master_key(struct super_block *sb,
430 struct fscrypt_master_key_secret *secret,
431 const struct fscrypt_key_specifier *mk_spec)
432 {
433 static DEFINE_MUTEX(fscrypt_add_key_mutex);
434 struct key *key;
435 int err;
436
437 mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */
438 retry:
439 key = fscrypt_find_master_key(sb, mk_spec);
440 if (IS_ERR(key)) {
441 err = PTR_ERR(key);
442 if (err != -ENOKEY)
443 goto out_unlock;
444 /* Didn't find the key in ->s_master_keys. Add it. */
445 err = allocate_filesystem_keyring(sb);
446 if (err)
447 goto out_unlock;
448 err = add_new_master_key(secret, mk_spec, sb->s_master_keys);
449 } else {
450 /*
451 * Found the key in ->s_master_keys. Re-add the secret if
452 * needed, and add the user to ->mk_users if needed.
453 */
454 down_write(&key->sem);
455 err = add_existing_master_key(key->payload.data[0], secret);
456 up_write(&key->sem);
457 if (err == KEY_DEAD) {
458 /* Key being removed or needs to be removed */
459 key_invalidate(key);
460 key_put(key);
461 goto retry;
462 }
463 key_put(key);
464 }
465 out_unlock:
466 mutex_unlock(&fscrypt_add_key_mutex);
467 return err;
468 }
469
470 /* Size of software "secret" derived from hardware-wrapped key */
471 #define RAW_SECRET_SIZE 32
472
add_master_key(struct super_block * sb,struct fscrypt_master_key_secret * secret,struct fscrypt_key_specifier * key_spec)473 static int add_master_key(struct super_block *sb,
474 struct fscrypt_master_key_secret *secret,
475 struct fscrypt_key_specifier *key_spec)
476 {
477 int err;
478
479 if (key_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
480 u8 _kdf_key[RAW_SECRET_SIZE];
481 u8 *kdf_key = secret->raw;
482 unsigned int kdf_key_size = secret->size;
483
484 if (secret->is_hw_wrapped) {
485 kdf_key = _kdf_key;
486 kdf_key_size = RAW_SECRET_SIZE;
487 err = fscrypt_derive_raw_secret(sb, secret->raw,
488 secret->size,
489 kdf_key, kdf_key_size);
490 if (err)
491 return err;
492 }
493 err = fscrypt_init_hkdf(&secret->hkdf, kdf_key, kdf_key_size);
494 /*
495 * Now that the HKDF context is initialized, the raw HKDF key is
496 * no longer needed.
497 */
498 memzero_explicit(kdf_key, kdf_key_size);
499 if (err)
500 return err;
501
502 /* Calculate the key identifier */
503 err = fscrypt_hkdf_expand(&secret->hkdf,
504 HKDF_CONTEXT_KEY_IDENTIFIER, NULL, 0,
505 key_spec->u.identifier,
506 FSCRYPT_KEY_IDENTIFIER_SIZE);
507 if (err)
508 return err;
509 }
510 return do_add_master_key(sb, secret, key_spec);
511 }
512
fscrypt_provisioning_key_preparse(struct key_preparsed_payload * prep)513 static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload *prep)
514 {
515 const struct fscrypt_provisioning_key_payload *payload = prep->data;
516
517 BUILD_BUG_ON(FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE < FSCRYPT_MAX_KEY_SIZE);
518
519 if (prep->datalen < sizeof(*payload) + FSCRYPT_MIN_KEY_SIZE ||
520 prep->datalen > sizeof(*payload) + FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE)
521 return -EINVAL;
522
523 if (payload->type != FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
524 payload->type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER)
525 return -EINVAL;
526
527 if (payload->__reserved)
528 return -EINVAL;
529
530 prep->payload.data[0] = kmemdup(payload, prep->datalen, GFP_KERNEL);
531 if (!prep->payload.data[0])
532 return -ENOMEM;
533
534 prep->quotalen = prep->datalen;
535 return 0;
536 }
537
fscrypt_provisioning_key_free_preparse(struct key_preparsed_payload * prep)538 static void fscrypt_provisioning_key_free_preparse(
539 struct key_preparsed_payload *prep)
540 {
541 kzfree(prep->payload.data[0]);
542 }
543
fscrypt_provisioning_key_describe(const struct key * key,struct seq_file * m)544 static void fscrypt_provisioning_key_describe(const struct key *key,
545 struct seq_file *m)
546 {
547 seq_puts(m, key->description);
548 if (key_is_positive(key)) {
549 const struct fscrypt_provisioning_key_payload *payload =
550 key->payload.data[0];
551
552 seq_printf(m, ": %u [%u]", key->datalen, payload->type);
553 }
554 }
555
fscrypt_provisioning_key_destroy(struct key * key)556 static void fscrypt_provisioning_key_destroy(struct key *key)
557 {
558 kzfree(key->payload.data[0]);
559 }
560
561 static struct key_type key_type_fscrypt_provisioning = {
562 .name = "fscrypt-provisioning",
563 .preparse = fscrypt_provisioning_key_preparse,
564 .free_preparse = fscrypt_provisioning_key_free_preparse,
565 .instantiate = generic_key_instantiate,
566 .describe = fscrypt_provisioning_key_describe,
567 .destroy = fscrypt_provisioning_key_destroy,
568 };
569
570 /*
571 * Retrieve the raw key from the Linux keyring key specified by 'key_id', and
572 * store it into 'secret'.
573 *
574 * The key must be of type "fscrypt-provisioning" and must have the field
575 * fscrypt_provisioning_key_payload::type set to 'type', indicating that it's
576 * only usable with fscrypt with the particular KDF version identified by
577 * 'type'. We don't use the "logon" key type because there's no way to
578 * completely restrict the use of such keys; they can be used by any kernel API
579 * that accepts "logon" keys and doesn't require a specific service prefix.
580 *
581 * The ability to specify the key via Linux keyring key is intended for cases
582 * where userspace needs to re-add keys after the filesystem is unmounted and
583 * re-mounted. Most users should just provide the raw key directly instead.
584 */
get_keyring_key(u32 key_id,u32 type,struct fscrypt_master_key_secret * secret)585 static int get_keyring_key(u32 key_id, u32 type,
586 struct fscrypt_master_key_secret *secret)
587 {
588 key_ref_t ref;
589 struct key *key;
590 const struct fscrypt_provisioning_key_payload *payload;
591 int err;
592
593 ref = lookup_user_key(key_id, 0, KEY_NEED_SEARCH);
594 if (IS_ERR(ref))
595 return PTR_ERR(ref);
596 key = key_ref_to_ptr(ref);
597
598 if (key->type != &key_type_fscrypt_provisioning)
599 goto bad_key;
600 payload = key->payload.data[0];
601
602 /* Don't allow fscrypt v1 keys to be used as v2 keys and vice versa. */
603 if (payload->type != type)
604 goto bad_key;
605
606 secret->size = key->datalen - sizeof(*payload);
607 memcpy(secret->raw, payload->raw, secret->size);
608 err = 0;
609 goto out_put;
610
611 bad_key:
612 err = -EKEYREJECTED;
613 out_put:
614 key_ref_put(ref);
615 return err;
616 }
617
618 /*
619 * Add a master encryption key to the filesystem, causing all files which were
620 * encrypted with it to appear "unlocked" (decrypted) when accessed.
621 *
622 * When adding a key for use by v1 encryption policies, this ioctl is
623 * privileged, and userspace must provide the 'key_descriptor'.
624 *
625 * When adding a key for use by v2+ encryption policies, this ioctl is
626 * unprivileged. This is needed, in general, to allow non-root users to use
627 * encryption without encountering the visibility problems of process-subscribed
628 * keyrings and the inability to properly remove keys. This works by having
629 * each key identified by its cryptographically secure hash --- the
630 * 'key_identifier'. The cryptographic hash ensures that a malicious user
631 * cannot add the wrong key for a given identifier. Furthermore, each added key
632 * is charged to the appropriate user's quota for the keyrings service, which
633 * prevents a malicious user from adding too many keys. Finally, we forbid a
634 * user from removing a key while other users have added it too, which prevents
635 * a user who knows another user's key from causing a denial-of-service by
636 * removing it at an inopportune time. (We tolerate that a user who knows a key
637 * can prevent other users from removing it.)
638 *
639 * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of
640 * Documentation/filesystems/fscrypt.rst.
641 */
fscrypt_ioctl_add_key(struct file * filp,void __user * _uarg)642 int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
643 {
644 struct super_block *sb = file_inode(filp)->i_sb;
645 struct fscrypt_add_key_arg __user *uarg = _uarg;
646 struct fscrypt_add_key_arg arg;
647 struct fscrypt_master_key_secret secret;
648 int err;
649
650 if (copy_from_user(&arg, uarg, sizeof(arg)))
651 return -EFAULT;
652
653 if (!valid_key_spec(&arg.key_spec))
654 return -EINVAL;
655
656 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
657 return -EINVAL;
658
659 /*
660 * Only root can add keys that are identified by an arbitrary descriptor
661 * rather than by a cryptographic hash --- since otherwise a malicious
662 * user could add the wrong key.
663 */
664 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
665 !capable(CAP_SYS_ADMIN))
666 return -EACCES;
667
668 memset(&secret, 0, sizeof(secret));
669
670 if (arg.__flags) {
671 if (arg.__flags & ~__FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED)
672 return -EINVAL;
673 if (arg.key_spec.type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER)
674 return -EINVAL;
675 secret.is_hw_wrapped = true;
676 }
677
678 if (arg.key_id) {
679 if (arg.raw_size != 0)
680 return -EINVAL;
681 err = get_keyring_key(arg.key_id, arg.key_spec.type, &secret);
682 if (err)
683 goto out_wipe_secret;
684 err = -EINVAL;
685 if (secret.size > FSCRYPT_MAX_KEY_SIZE && !secret.is_hw_wrapped)
686 goto out_wipe_secret;
687 } else {
688 if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE ||
689 arg.raw_size > (secret.is_hw_wrapped ?
690 FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE :
691 FSCRYPT_MAX_KEY_SIZE))
692 return -EINVAL;
693 secret.size = arg.raw_size;
694 err = -EFAULT;
695 if (copy_from_user(secret.raw, uarg->raw, secret.size))
696 goto out_wipe_secret;
697 }
698
699 err = add_master_key(sb, &secret, &arg.key_spec);
700 if (err)
701 goto out_wipe_secret;
702
703 /* Return the key identifier to userspace, if applicable */
704 err = -EFAULT;
705 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
706 copy_to_user(uarg->key_spec.u.identifier, arg.key_spec.u.identifier,
707 FSCRYPT_KEY_IDENTIFIER_SIZE))
708 goto out_wipe_secret;
709 err = 0;
710 out_wipe_secret:
711 wipe_master_key_secret(&secret);
712 return err;
713 }
714 EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
715
716 /*
717 * Add the key for '-o test_dummy_encryption' to the filesystem keyring.
718 *
719 * Use a per-boot random key to prevent people from misusing this option.
720 */
fscrypt_add_test_dummy_key(struct super_block * sb,struct fscrypt_key_specifier * key_spec)721 int fscrypt_add_test_dummy_key(struct super_block *sb,
722 struct fscrypt_key_specifier *key_spec)
723 {
724 static u8 test_key[FSCRYPT_MAX_KEY_SIZE];
725 struct fscrypt_master_key_secret secret;
726 int err;
727
728 get_random_once(test_key, FSCRYPT_MAX_KEY_SIZE);
729
730 memset(&secret, 0, sizeof(secret));
731 secret.size = FSCRYPT_MAX_KEY_SIZE;
732 memcpy(secret.raw, test_key, FSCRYPT_MAX_KEY_SIZE);
733
734 err = add_master_key(sb, &secret, key_spec);
735 wipe_master_key_secret(&secret);
736 return err;
737 }
738
739 /*
740 * Verify that the current user has added a master key with the given identifier
741 * (returns -ENOKEY if not). This is needed to prevent a user from encrypting
742 * their files using some other user's key which they don't actually know.
743 * Cryptographically this isn't much of a problem, but the semantics of this
744 * would be a bit weird, so it's best to just forbid it.
745 *
746 * The system administrator (CAP_FOWNER) can override this, which should be
747 * enough for any use cases where encryption policies are being set using keys
748 * that were chosen ahead of time but aren't available at the moment.
749 *
750 * Note that the key may have already removed by the time this returns, but
751 * that's okay; we just care whether the key was there at some point.
752 *
753 * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code
754 */
fscrypt_verify_key_added(struct super_block * sb,const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])755 int fscrypt_verify_key_added(struct super_block *sb,
756 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
757 {
758 struct fscrypt_key_specifier mk_spec;
759 struct key *key, *mk_user;
760 struct fscrypt_master_key *mk;
761 int err;
762
763 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
764 memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
765
766 key = fscrypt_find_master_key(sb, &mk_spec);
767 if (IS_ERR(key)) {
768 err = PTR_ERR(key);
769 goto out;
770 }
771 mk = key->payload.data[0];
772 mk_user = find_master_key_user(mk);
773 if (IS_ERR(mk_user)) {
774 err = PTR_ERR(mk_user);
775 } else {
776 key_put(mk_user);
777 err = 0;
778 }
779 key_put(key);
780 out:
781 if (err == -ENOKEY && capable(CAP_FOWNER))
782 err = 0;
783 return err;
784 }
785
786 /*
787 * Try to evict the inode's dentries from the dentry cache. If the inode is a
788 * directory, then it can have at most one dentry; however, that dentry may be
789 * pinned by child dentries, so first try to evict the children too.
790 */
shrink_dcache_inode(struct inode * inode)791 static void shrink_dcache_inode(struct inode *inode)
792 {
793 struct dentry *dentry;
794
795 if (S_ISDIR(inode->i_mode)) {
796 dentry = d_find_any_alias(inode);
797 if (dentry) {
798 shrink_dcache_parent(dentry);
799 dput(dentry);
800 }
801 }
802 d_prune_aliases(inode);
803 }
804
evict_dentries_for_decrypted_inodes(struct fscrypt_master_key * mk)805 static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk)
806 {
807 struct fscrypt_info *ci;
808 struct inode *inode;
809 struct inode *toput_inode = NULL;
810
811 spin_lock(&mk->mk_decrypted_inodes_lock);
812
813 list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
814 inode = ci->ci_inode;
815 spin_lock(&inode->i_lock);
816 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
817 spin_unlock(&inode->i_lock);
818 continue;
819 }
820 __iget(inode);
821 spin_unlock(&inode->i_lock);
822 spin_unlock(&mk->mk_decrypted_inodes_lock);
823
824 shrink_dcache_inode(inode);
825 iput(toput_inode);
826 toput_inode = inode;
827
828 spin_lock(&mk->mk_decrypted_inodes_lock);
829 }
830
831 spin_unlock(&mk->mk_decrypted_inodes_lock);
832 iput(toput_inode);
833 }
834
check_for_busy_inodes(struct super_block * sb,struct fscrypt_master_key * mk)835 static int check_for_busy_inodes(struct super_block *sb,
836 struct fscrypt_master_key *mk)
837 {
838 struct list_head *pos;
839 size_t busy_count = 0;
840 unsigned long ino;
841
842 spin_lock(&mk->mk_decrypted_inodes_lock);
843
844 list_for_each(pos, &mk->mk_decrypted_inodes)
845 busy_count++;
846
847 if (busy_count == 0) {
848 spin_unlock(&mk->mk_decrypted_inodes_lock);
849 return 0;
850 }
851
852 {
853 /* select an example file to show for debugging purposes */
854 struct inode *inode =
855 list_first_entry(&mk->mk_decrypted_inodes,
856 struct fscrypt_info,
857 ci_master_key_link)->ci_inode;
858 ino = inode->i_ino;
859 }
860 spin_unlock(&mk->mk_decrypted_inodes_lock);
861
862 fscrypt_warn(NULL,
863 "%s: %zu inode(s) still busy after removing key with %s %*phN, including ino %lu",
864 sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
865 master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
866 ino);
867 return -EBUSY;
868 }
869
try_to_lock_encrypted_files(struct super_block * sb,struct fscrypt_master_key * mk)870 static int try_to_lock_encrypted_files(struct super_block *sb,
871 struct fscrypt_master_key *mk)
872 {
873 int err1;
874 int err2;
875
876 /*
877 * An inode can't be evicted while it is dirty or has dirty pages.
878 * Thus, we first have to clean the inodes in ->mk_decrypted_inodes.
879 *
880 * Just do it the easy way: call sync_filesystem(). It's overkill, but
881 * it works, and it's more important to minimize the amount of caches we
882 * drop than the amount of data we sync. Also, unprivileged users can
883 * already call sync_filesystem() via sys_syncfs() or sys_sync().
884 */
885 down_read(&sb->s_umount);
886 err1 = sync_filesystem(sb);
887 up_read(&sb->s_umount);
888 /* If a sync error occurs, still try to evict as much as possible. */
889
890 /*
891 * Inodes are pinned by their dentries, so we have to evict their
892 * dentries. shrink_dcache_sb() would suffice, but would be overkill
893 * and inappropriate for use by unprivileged users. So instead go
894 * through the inodes' alias lists and try to evict each dentry.
895 */
896 evict_dentries_for_decrypted_inodes(mk);
897
898 /*
899 * evict_dentries_for_decrypted_inodes() already iput() each inode in
900 * the list; any inodes for which that dropped the last reference will
901 * have been evicted due to fscrypt_drop_inode() detecting the key
902 * removal and telling the VFS to evict the inode. So to finish, we
903 * just need to check whether any inodes couldn't be evicted.
904 */
905 err2 = check_for_busy_inodes(sb, mk);
906
907 return err1 ?: err2;
908 }
909
910 /*
911 * Try to remove an fscrypt master encryption key.
912 *
913 * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's
914 * claim to the key, then removes the key itself if no other users have claims.
915 * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the
916 * key itself.
917 *
918 * To "remove the key itself", first we wipe the actual master key secret, so
919 * that no more inodes can be unlocked with it. Then we try to evict all cached
920 * inodes that had been unlocked with the key.
921 *
922 * If all inodes were evicted, then we unlink the fscrypt_master_key from the
923 * keyring. Otherwise it remains in the keyring in the "incompletely removed"
924 * state (without the actual secret key) where it tracks the list of remaining
925 * inodes. Userspace can execute the ioctl again later to retry eviction, or
926 * alternatively can re-add the secret key again.
927 *
928 * For more details, see the "Removing keys" section of
929 * Documentation/filesystems/fscrypt.rst.
930 */
do_remove_key(struct file * filp,void __user * _uarg,bool all_users)931 static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
932 {
933 struct super_block *sb = file_inode(filp)->i_sb;
934 struct fscrypt_remove_key_arg __user *uarg = _uarg;
935 struct fscrypt_remove_key_arg arg;
936 struct key *key;
937 struct fscrypt_master_key *mk;
938 u32 status_flags = 0;
939 int err;
940 bool dead;
941
942 if (copy_from_user(&arg, uarg, sizeof(arg)))
943 return -EFAULT;
944
945 if (!valid_key_spec(&arg.key_spec))
946 return -EINVAL;
947
948 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
949 return -EINVAL;
950
951 /*
952 * Only root can add and remove keys that are identified by an arbitrary
953 * descriptor rather than by a cryptographic hash.
954 */
955 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
956 !capable(CAP_SYS_ADMIN))
957 return -EACCES;
958
959 /* Find the key being removed. */
960 key = fscrypt_find_master_key(sb, &arg.key_spec);
961 if (IS_ERR(key))
962 return PTR_ERR(key);
963 mk = key->payload.data[0];
964
965 down_write(&key->sem);
966
967 /* If relevant, remove current user's (or all users) claim to the key */
968 if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
969 if (all_users)
970 err = keyring_clear(mk->mk_users);
971 else
972 err = remove_master_key_user(mk);
973 if (err) {
974 up_write(&key->sem);
975 goto out_put_key;
976 }
977 if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
978 /*
979 * Other users have still added the key too. We removed
980 * the current user's claim to the key, but we still
981 * can't remove the key itself.
982 */
983 status_flags |=
984 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
985 err = 0;
986 up_write(&key->sem);
987 goto out_put_key;
988 }
989 }
990
991 /* No user claims remaining. Go ahead and wipe the secret. */
992 dead = false;
993 if (is_master_key_secret_present(&mk->mk_secret)) {
994 down_write(&mk->mk_secret_sem);
995 wipe_master_key_secret(&mk->mk_secret);
996 dead = refcount_dec_and_test(&mk->mk_refcount);
997 up_write(&mk->mk_secret_sem);
998 }
999 up_write(&key->sem);
1000 if (dead) {
1001 /*
1002 * No inodes reference the key, and we wiped the secret, so the
1003 * key object is free to be removed from the keyring.
1004 */
1005 key_invalidate(key);
1006 err = 0;
1007 } else {
1008 /* Some inodes still reference this key; try to evict them. */
1009 err = try_to_lock_encrypted_files(sb, mk);
1010 if (err == -EBUSY) {
1011 status_flags |=
1012 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
1013 err = 0;
1014 }
1015 }
1016 /*
1017 * We return 0 if we successfully did something: removed a claim to the
1018 * key, wiped the secret, or tried locking the files again. Users need
1019 * to check the informational status flags if they care whether the key
1020 * has been fully removed including all files locked.
1021 */
1022 out_put_key:
1023 key_put(key);
1024 if (err == 0)
1025 err = put_user(status_flags, &uarg->removal_status_flags);
1026 return err;
1027 }
1028
fscrypt_ioctl_remove_key(struct file * filp,void __user * uarg)1029 int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
1030 {
1031 return do_remove_key(filp, uarg, false);
1032 }
1033 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key);
1034
fscrypt_ioctl_remove_key_all_users(struct file * filp,void __user * uarg)1035 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
1036 {
1037 if (!capable(CAP_SYS_ADMIN))
1038 return -EACCES;
1039 return do_remove_key(filp, uarg, true);
1040 }
1041 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users);
1042
1043 /*
1044 * Retrieve the status of an fscrypt master encryption key.
1045 *
1046 * We set ->status to indicate whether the key is absent, present, or
1047 * incompletely removed. "Incompletely removed" means that the master key
1048 * secret has been removed, but some files which had been unlocked with it are
1049 * still in use. This field allows applications to easily determine the state
1050 * of an encrypted directory without using a hack such as trying to open a
1051 * regular file in it (which can confuse the "incompletely removed" state with
1052 * absent or present).
1053 *
1054 * In addition, for v2 policy keys we allow applications to determine, via
1055 * ->status_flags and ->user_count, whether the key has been added by the
1056 * current user, by other users, or by both. Most applications should not need
1057 * this, since ordinarily only one user should know a given key. However, if a
1058 * secret key is shared by multiple users, applications may wish to add an
1059 * already-present key to prevent other users from removing it. This ioctl can
1060 * be used to check whether that really is the case before the work is done to
1061 * add the key --- which might e.g. require prompting the user for a passphrase.
1062 *
1063 * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of
1064 * Documentation/filesystems/fscrypt.rst.
1065 */
fscrypt_ioctl_get_key_status(struct file * filp,void __user * uarg)1066 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
1067 {
1068 struct super_block *sb = file_inode(filp)->i_sb;
1069 struct fscrypt_get_key_status_arg arg;
1070 struct key *key;
1071 struct fscrypt_master_key *mk;
1072 int err;
1073
1074 if (copy_from_user(&arg, uarg, sizeof(arg)))
1075 return -EFAULT;
1076
1077 if (!valid_key_spec(&arg.key_spec))
1078 return -EINVAL;
1079
1080 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
1081 return -EINVAL;
1082
1083 arg.status_flags = 0;
1084 arg.user_count = 0;
1085 memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
1086
1087 key = fscrypt_find_master_key(sb, &arg.key_spec);
1088 if (IS_ERR(key)) {
1089 if (key != ERR_PTR(-ENOKEY))
1090 return PTR_ERR(key);
1091 arg.status = FSCRYPT_KEY_STATUS_ABSENT;
1092 err = 0;
1093 goto out;
1094 }
1095 mk = key->payload.data[0];
1096 down_read(&key->sem);
1097
1098 if (!is_master_key_secret_present(&mk->mk_secret)) {
1099 arg.status = FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED;
1100 err = 0;
1101 goto out_release_key;
1102 }
1103
1104 arg.status = FSCRYPT_KEY_STATUS_PRESENT;
1105 if (mk->mk_users) {
1106 struct key *mk_user;
1107
1108 arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
1109 mk_user = find_master_key_user(mk);
1110 if (!IS_ERR(mk_user)) {
1111 arg.status_flags |=
1112 FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
1113 key_put(mk_user);
1114 } else if (mk_user != ERR_PTR(-ENOKEY)) {
1115 err = PTR_ERR(mk_user);
1116 goto out_release_key;
1117 }
1118 }
1119 err = 0;
1120 out_release_key:
1121 up_read(&key->sem);
1122 key_put(key);
1123 out:
1124 if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
1125 err = -EFAULT;
1126 return err;
1127 }
1128 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status);
1129
fscrypt_init_keyring(void)1130 int __init fscrypt_init_keyring(void)
1131 {
1132 int err;
1133
1134 err = register_key_type(&key_type_fscrypt);
1135 if (err)
1136 return err;
1137
1138 err = register_key_type(&key_type_fscrypt_user);
1139 if (err)
1140 goto err_unregister_fscrypt;
1141
1142 err = register_key_type(&key_type_fscrypt_provisioning);
1143 if (err)
1144 goto err_unregister_fscrypt_user;
1145
1146 return 0;
1147
1148 err_unregister_fscrypt_user:
1149 unregister_key_type(&key_type_fscrypt_user);
1150 err_unregister_fscrypt:
1151 unregister_key_type(&key_type_fscrypt);
1152 return err;
1153 }
1154