1 // SPDX-License-Identifier: GPL-2.0
3 * Filesystem-level keyring for fscrypt
5 * Copyright 2019 Google LLC
9 * This file implements management of fscrypt master keys in the
10 * filesystem-level keyring, including the ioctls:
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
17 * See the "User API" section of Documentation/filesystems/fscrypt.rst for more
18 * information about these ioctls.
21 #include <crypto/skcipher.h>
22 #include <linux/key-type.h>
23 #include <linux/seq_file.h>
25 #include "fscrypt_private.h"
27 static void wipe_master_key_secret(struct fscrypt_master_key_secret
*secret
)
29 fscrypt_destroy_hkdf(&secret
->hkdf
);
30 memzero_explicit(secret
, sizeof(*secret
));
33 static void move_master_key_secret(struct fscrypt_master_key_secret
*dst
,
34 struct fscrypt_master_key_secret
*src
)
36 memcpy(dst
, src
, sizeof(*dst
));
37 memzero_explicit(src
, sizeof(*src
));
40 static void free_master_key(struct fscrypt_master_key
*mk
)
44 wipe_master_key_secret(&mk
->mk_secret
);
46 for (i
= 0; i
<= __FSCRYPT_MODE_MAX
; i
++) {
47 crypto_free_skcipher(mk
->mk_direct_tfms
[i
]);
48 crypto_free_skcipher(mk
->mk_iv_ino_lblk_64_tfms
[i
]);
51 key_put(mk
->mk_users
);
55 static inline bool valid_key_spec(const struct fscrypt_key_specifier
*spec
)
59 return master_key_spec_len(spec
) != 0;
62 static int fscrypt_key_instantiate(struct key
*key
,
63 struct key_preparsed_payload
*prep
)
65 key
->payload
.data
[0] = (struct fscrypt_master_key
*)prep
->data
;
69 static void fscrypt_key_destroy(struct key
*key
)
71 free_master_key(key
->payload
.data
[0]);
74 static void fscrypt_key_describe(const struct key
*key
, struct seq_file
*m
)
76 seq_puts(m
, key
->description
);
78 if (key_is_positive(key
)) {
79 const struct fscrypt_master_key
*mk
= key
->payload
.data
[0];
81 if (!is_master_key_secret_present(&mk
->mk_secret
))
82 seq_puts(m
, ": secret removed");
87 * Type of key in ->s_master_keys. Each key of this type represents a master
88 * key which has been added to the filesystem. Its payload is a
89 * 'struct fscrypt_master_key'. The "." prefix in the key type name prevents
90 * users from adding keys of this type via the keyrings syscalls rather than via
91 * the intended method of FS_IOC_ADD_ENCRYPTION_KEY.
93 static struct key_type key_type_fscrypt
= {
95 .instantiate
= fscrypt_key_instantiate
,
96 .destroy
= fscrypt_key_destroy
,
97 .describe
= fscrypt_key_describe
,
100 static int fscrypt_user_key_instantiate(struct key
*key
,
101 struct key_preparsed_payload
*prep
)
104 * We just charge FSCRYPT_MAX_KEY_SIZE bytes to the user's key quota for
105 * each key, regardless of the exact key size. The amount of memory
106 * actually used is greater than the size of the raw key anyway.
108 return key_payload_reserve(key
, FSCRYPT_MAX_KEY_SIZE
);
111 static void fscrypt_user_key_describe(const struct key
*key
, struct seq_file
*m
)
113 seq_puts(m
, key
->description
);
117 * Type of key in ->mk_users. Each key of this type represents a particular
118 * user who has added a particular master key.
120 * Note that the name of this key type really should be something like
121 * ".fscrypt-user" instead of simply ".fscrypt". But the shorter name is chosen
122 * mainly for simplicity of presentation in /proc/keys when read by a non-root
123 * user. And it is expected to be rare that a key is actually added by multiple
124 * users, since users should keep their encryption keys confidential.
126 static struct key_type key_type_fscrypt_user
= {
128 .instantiate
= fscrypt_user_key_instantiate
,
129 .describe
= fscrypt_user_key_describe
,
132 /* Search ->s_master_keys or ->mk_users */
133 static struct key
*search_fscrypt_keyring(struct key
*keyring
,
134 struct key_type
*type
,
135 const char *description
)
138 * We need to mark the keyring reference as "possessed" so that we
139 * acquire permission to search it, via the KEY_POS_SEARCH permission.
141 key_ref_t keyref
= make_key_ref(keyring
, true /* possessed */);
143 keyref
= keyring_search(keyref
, type
, description
, false);
144 if (IS_ERR(keyref
)) {
145 if (PTR_ERR(keyref
) == -EAGAIN
|| /* not found */
146 PTR_ERR(keyref
) == -EKEYREVOKED
) /* recently invalidated */
147 keyref
= ERR_PTR(-ENOKEY
);
148 return ERR_CAST(keyref
);
150 return key_ref_to_ptr(keyref
);
153 #define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \
154 (CONST_STRLEN("fscrypt-") + sizeof_field(struct super_block, s_id))
156 #define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1)
158 #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE \
159 (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
160 CONST_STRLEN("-users") + 1)
162 #define FSCRYPT_MK_USER_DESCRIPTION_SIZE \
163 (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
165 static void format_fs_keyring_description(
166 char description
[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE
],
167 const struct super_block
*sb
)
169 sprintf(description
, "fscrypt-%s", sb
->s_id
);
172 static void format_mk_description(
173 char description
[FSCRYPT_MK_DESCRIPTION_SIZE
],
174 const struct fscrypt_key_specifier
*mk_spec
)
176 sprintf(description
, "%*phN",
177 master_key_spec_len(mk_spec
), (u8
*)&mk_spec
->u
);
180 static void format_mk_users_keyring_description(
181 char description
[FSCRYPT_MK_USERS_DESCRIPTION_SIZE
],
182 const u8 mk_identifier
[FSCRYPT_KEY_IDENTIFIER_SIZE
])
184 sprintf(description
, "fscrypt-%*phN-users",
185 FSCRYPT_KEY_IDENTIFIER_SIZE
, mk_identifier
);
188 static void format_mk_user_description(
189 char description
[FSCRYPT_MK_USER_DESCRIPTION_SIZE
],
190 const u8 mk_identifier
[FSCRYPT_KEY_IDENTIFIER_SIZE
])
193 sprintf(description
, "%*phN.uid.%u", FSCRYPT_KEY_IDENTIFIER_SIZE
,
194 mk_identifier
, __kuid_val(current_fsuid()));
197 /* Create ->s_master_keys if needed. Synchronized by fscrypt_add_key_mutex. */
198 static int allocate_filesystem_keyring(struct super_block
*sb
)
200 char description
[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE
];
203 if (sb
->s_master_keys
)
206 format_fs_keyring_description(description
, sb
);
207 keyring
= keyring_alloc(description
, GLOBAL_ROOT_UID
, GLOBAL_ROOT_GID
,
208 current_cred(), KEY_POS_SEARCH
|
209 KEY_USR_SEARCH
| KEY_USR_READ
| KEY_USR_VIEW
,
210 KEY_ALLOC_NOT_IN_QUOTA
, NULL
, NULL
);
212 return PTR_ERR(keyring
);
214 /* Pairs with READ_ONCE() in fscrypt_find_master_key() */
215 smp_store_release(&sb
->s_master_keys
, keyring
);
219 void fscrypt_sb_free(struct super_block
*sb
)
221 key_put(sb
->s_master_keys
);
222 sb
->s_master_keys
= NULL
;
226 * Find the specified master key in ->s_master_keys.
227 * Returns ERR_PTR(-ENOKEY) if not found.
229 struct key
*fscrypt_find_master_key(struct super_block
*sb
,
230 const struct fscrypt_key_specifier
*mk_spec
)
233 char description
[FSCRYPT_MK_DESCRIPTION_SIZE
];
235 /* pairs with smp_store_release() in allocate_filesystem_keyring() */
236 keyring
= READ_ONCE(sb
->s_master_keys
);
238 return ERR_PTR(-ENOKEY
); /* No keyring yet, so no keys yet. */
240 format_mk_description(description
, mk_spec
);
241 return search_fscrypt_keyring(keyring
, &key_type_fscrypt
, description
);
244 static int allocate_master_key_users_keyring(struct fscrypt_master_key
*mk
)
246 char description
[FSCRYPT_MK_USERS_DESCRIPTION_SIZE
];
249 format_mk_users_keyring_description(description
,
250 mk
->mk_spec
.u
.identifier
);
251 keyring
= keyring_alloc(description
, GLOBAL_ROOT_UID
, GLOBAL_ROOT_GID
,
252 current_cred(), KEY_POS_SEARCH
|
253 KEY_USR_SEARCH
| KEY_USR_READ
| KEY_USR_VIEW
,
254 KEY_ALLOC_NOT_IN_QUOTA
, NULL
, NULL
);
256 return PTR_ERR(keyring
);
258 mk
->mk_users
= keyring
;
263 * Find the current user's "key" in the master key's ->mk_users.
264 * Returns ERR_PTR(-ENOKEY) if not found.
266 static struct key
*find_master_key_user(struct fscrypt_master_key
*mk
)
268 char description
[FSCRYPT_MK_USER_DESCRIPTION_SIZE
];
270 format_mk_user_description(description
, mk
->mk_spec
.u
.identifier
);
271 return search_fscrypt_keyring(mk
->mk_users
, &key_type_fscrypt_user
,
276 * Give the current user a "key" in ->mk_users. This charges the user's quota
277 * and marks the master key as added by the current user, so that it cannot be
278 * removed by another user with the key. Either the master key's key->sem must
279 * be held for write, or the master key must be still undergoing initialization.
281 static int add_master_key_user(struct fscrypt_master_key
*mk
)
283 char description
[FSCRYPT_MK_USER_DESCRIPTION_SIZE
];
287 format_mk_user_description(description
, mk
->mk_spec
.u
.identifier
);
288 mk_user
= key_alloc(&key_type_fscrypt_user
, description
,
289 current_fsuid(), current_gid(), current_cred(),
290 KEY_POS_SEARCH
| KEY_USR_VIEW
, 0, NULL
);
292 return PTR_ERR(mk_user
);
294 err
= key_instantiate_and_link(mk_user
, NULL
, 0, mk
->mk_users
, NULL
);
300 * Remove the current user's "key" from ->mk_users.
301 * The master key's key->sem must be held for write.
303 * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
305 static int remove_master_key_user(struct fscrypt_master_key
*mk
)
310 mk_user
= find_master_key_user(mk
);
312 return PTR_ERR(mk_user
);
313 err
= key_unlink(mk
->mk_users
, mk_user
);
319 * Allocate a new fscrypt_master_key which contains the given secret, set it as
320 * the payload of a new 'struct key' of type fscrypt, and link the 'struct key'
321 * into the given keyring. Synchronized by fscrypt_add_key_mutex.
323 static int add_new_master_key(struct fscrypt_master_key_secret
*secret
,
324 const struct fscrypt_key_specifier
*mk_spec
,
327 struct fscrypt_master_key
*mk
;
328 char description
[FSCRYPT_MK_DESCRIPTION_SIZE
];
332 mk
= kzalloc(sizeof(*mk
), GFP_KERNEL
);
336 mk
->mk_spec
= *mk_spec
;
338 move_master_key_secret(&mk
->mk_secret
, secret
);
339 init_rwsem(&mk
->mk_secret_sem
);
341 refcount_set(&mk
->mk_refcount
, 1); /* secret is present */
342 INIT_LIST_HEAD(&mk
->mk_decrypted_inodes
);
343 spin_lock_init(&mk
->mk_decrypted_inodes_lock
);
345 if (mk_spec
->type
== FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
) {
346 err
= allocate_master_key_users_keyring(mk
);
349 err
= add_master_key_user(mk
);
355 * Note that we don't charge this key to anyone's quota, since when
356 * ->mk_users is in use those keys are charged instead, and otherwise
357 * (when ->mk_users isn't in use) only root can add these keys.
359 format_mk_description(description
, mk_spec
);
360 key
= key_alloc(&key_type_fscrypt
, description
,
361 GLOBAL_ROOT_UID
, GLOBAL_ROOT_GID
, current_cred(),
362 KEY_POS_SEARCH
| KEY_USR_SEARCH
| KEY_USR_VIEW
,
363 KEY_ALLOC_NOT_IN_QUOTA
, NULL
);
368 err
= key_instantiate_and_link(key
, mk
, sizeof(*mk
), keyring
, NULL
);
382 static int add_existing_master_key(struct fscrypt_master_key
*mk
,
383 struct fscrypt_master_key_secret
*secret
)
390 * If the current user is already in ->mk_users, then there's nothing to
391 * do. (Not applicable for v1 policy keys, which have NULL ->mk_users.)
394 mk_user
= find_master_key_user(mk
);
395 if (mk_user
!= ERR_PTR(-ENOKEY
)) {
397 return PTR_ERR(mk_user
);
403 /* If we'll be re-adding ->mk_secret, try to take the reference. */
404 rekey
= !is_master_key_secret_present(&mk
->mk_secret
);
405 if (rekey
&& !refcount_inc_not_zero(&mk
->mk_refcount
))
408 /* Add the current user to ->mk_users, if applicable. */
410 err
= add_master_key_user(mk
);
412 if (rekey
&& refcount_dec_and_test(&mk
->mk_refcount
))
418 /* Re-add the secret if needed. */
420 down_write(&mk
->mk_secret_sem
);
421 move_master_key_secret(&mk
->mk_secret
, secret
);
422 up_write(&mk
->mk_secret_sem
);
427 static int add_master_key(struct super_block
*sb
,
428 struct fscrypt_master_key_secret
*secret
,
429 const struct fscrypt_key_specifier
*mk_spec
)
431 static DEFINE_MUTEX(fscrypt_add_key_mutex
);
435 mutex_lock(&fscrypt_add_key_mutex
); /* serialize find + link */
437 key
= fscrypt_find_master_key(sb
, mk_spec
);
442 /* Didn't find the key in ->s_master_keys. Add it. */
443 err
= allocate_filesystem_keyring(sb
);
446 err
= add_new_master_key(secret
, mk_spec
, sb
->s_master_keys
);
449 * Found the key in ->s_master_keys. Re-add the secret if
450 * needed, and add the user to ->mk_users if needed.
452 down_write(&key
->sem
);
453 err
= add_existing_master_key(key
->payload
.data
[0], secret
);
455 if (err
== KEY_DEAD
) {
456 /* Key being removed or needs to be removed */
464 mutex_unlock(&fscrypt_add_key_mutex
);
468 static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload
*prep
)
470 const struct fscrypt_provisioning_key_payload
*payload
= prep
->data
;
472 if (prep
->datalen
< sizeof(*payload
) + FSCRYPT_MIN_KEY_SIZE
||
473 prep
->datalen
> sizeof(*payload
) + FSCRYPT_MAX_KEY_SIZE
)
476 if (payload
->type
!= FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR
&&
477 payload
->type
!= FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
)
480 if (payload
->__reserved
)
483 prep
->payload
.data
[0] = kmemdup(payload
, prep
->datalen
, GFP_KERNEL
);
484 if (!prep
->payload
.data
[0])
487 prep
->quotalen
= prep
->datalen
;
491 static void fscrypt_provisioning_key_free_preparse(
492 struct key_preparsed_payload
*prep
)
494 kzfree(prep
->payload
.data
[0]);
497 static void fscrypt_provisioning_key_describe(const struct key
*key
,
500 seq_puts(m
, key
->description
);
501 if (key_is_positive(key
)) {
502 const struct fscrypt_provisioning_key_payload
*payload
=
503 key
->payload
.data
[0];
505 seq_printf(m
, ": %u [%u]", key
->datalen
, payload
->type
);
509 static void fscrypt_provisioning_key_destroy(struct key
*key
)
511 kzfree(key
->payload
.data
[0]);
514 static struct key_type key_type_fscrypt_provisioning
= {
515 .name
= "fscrypt-provisioning",
516 .preparse
= fscrypt_provisioning_key_preparse
,
517 .free_preparse
= fscrypt_provisioning_key_free_preparse
,
518 .instantiate
= generic_key_instantiate
,
519 .describe
= fscrypt_provisioning_key_describe
,
520 .destroy
= fscrypt_provisioning_key_destroy
,
524 * Retrieve the raw key from the Linux keyring key specified by 'key_id', and
525 * store it into 'secret'.
527 * The key must be of type "fscrypt-provisioning" and must have the field
528 * fscrypt_provisioning_key_payload::type set to 'type', indicating that it's
529 * only usable with fscrypt with the particular KDF version identified by
530 * 'type'. We don't use the "logon" key type because there's no way to
531 * completely restrict the use of such keys; they can be used by any kernel API
532 * that accepts "logon" keys and doesn't require a specific service prefix.
534 * The ability to specify the key via Linux keyring key is intended for cases
535 * where userspace needs to re-add keys after the filesystem is unmounted and
536 * re-mounted. Most users should just provide the raw key directly instead.
538 static int get_keyring_key(u32 key_id
, u32 type
,
539 struct fscrypt_master_key_secret
*secret
)
543 const struct fscrypt_provisioning_key_payload
*payload
;
546 ref
= lookup_user_key(key_id
, 0, KEY_NEED_SEARCH
);
549 key
= key_ref_to_ptr(ref
);
551 if (key
->type
!= &key_type_fscrypt_provisioning
)
553 payload
= key
->payload
.data
[0];
555 /* Don't allow fscrypt v1 keys to be used as v2 keys and vice versa. */
556 if (payload
->type
!= type
)
559 secret
->size
= key
->datalen
- sizeof(*payload
);
560 memcpy(secret
->raw
, payload
->raw
, secret
->size
);
572 * Add a master encryption key to the filesystem, causing all files which were
573 * encrypted with it to appear "unlocked" (decrypted) when accessed.
575 * When adding a key for use by v1 encryption policies, this ioctl is
576 * privileged, and userspace must provide the 'key_descriptor'.
578 * When adding a key for use by v2+ encryption policies, this ioctl is
579 * unprivileged. This is needed, in general, to allow non-root users to use
580 * encryption without encountering the visibility problems of process-subscribed
581 * keyrings and the inability to properly remove keys. This works by having
582 * each key identified by its cryptographically secure hash --- the
583 * 'key_identifier'. The cryptographic hash ensures that a malicious user
584 * cannot add the wrong key for a given identifier. Furthermore, each added key
585 * is charged to the appropriate user's quota for the keyrings service, which
586 * prevents a malicious user from adding too many keys. Finally, we forbid a
587 * user from removing a key while other users have added it too, which prevents
588 * a user who knows another user's key from causing a denial-of-service by
589 * removing it at an inopportune time. (We tolerate that a user who knows a key
590 * can prevent other users from removing it.)
592 * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of
593 * Documentation/filesystems/fscrypt.rst.
595 int fscrypt_ioctl_add_key(struct file
*filp
, void __user
*_uarg
)
597 struct super_block
*sb
= file_inode(filp
)->i_sb
;
598 struct fscrypt_add_key_arg __user
*uarg
= _uarg
;
599 struct fscrypt_add_key_arg arg
;
600 struct fscrypt_master_key_secret secret
;
603 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
606 if (!valid_key_spec(&arg
.key_spec
))
609 if (memchr_inv(arg
.__reserved
, 0, sizeof(arg
.__reserved
)))
612 memset(&secret
, 0, sizeof(secret
));
614 if (arg
.raw_size
!= 0)
616 err
= get_keyring_key(arg
.key_id
, arg
.key_spec
.type
, &secret
);
618 goto out_wipe_secret
;
620 if (arg
.raw_size
< FSCRYPT_MIN_KEY_SIZE
||
621 arg
.raw_size
> FSCRYPT_MAX_KEY_SIZE
)
623 secret
.size
= arg
.raw_size
;
625 if (copy_from_user(secret
.raw
, uarg
->raw
, secret
.size
))
626 goto out_wipe_secret
;
629 switch (arg
.key_spec
.type
) {
630 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR
:
632 * Only root can add keys that are identified by an arbitrary
633 * descriptor rather than by a cryptographic hash --- since
634 * otherwise a malicious user could add the wrong key.
637 if (!capable(CAP_SYS_ADMIN
))
638 goto out_wipe_secret
;
640 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
:
641 err
= fscrypt_init_hkdf(&secret
.hkdf
, secret
.raw
, secret
.size
);
643 goto out_wipe_secret
;
646 * Now that the HKDF context is initialized, the raw key is no
649 memzero_explicit(secret
.raw
, secret
.size
);
651 /* Calculate the key identifier and return it to userspace. */
652 err
= fscrypt_hkdf_expand(&secret
.hkdf
,
653 HKDF_CONTEXT_KEY_IDENTIFIER
,
654 NULL
, 0, arg
.key_spec
.u
.identifier
,
655 FSCRYPT_KEY_IDENTIFIER_SIZE
);
657 goto out_wipe_secret
;
659 if (copy_to_user(uarg
->key_spec
.u
.identifier
,
660 arg
.key_spec
.u
.identifier
,
661 FSCRYPT_KEY_IDENTIFIER_SIZE
))
662 goto out_wipe_secret
;
667 goto out_wipe_secret
;
670 err
= add_master_key(sb
, &secret
, &arg
.key_spec
);
672 wipe_master_key_secret(&secret
);
675 EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key
);
678 * Verify that the current user has added a master key with the given identifier
679 * (returns -ENOKEY if not). This is needed to prevent a user from encrypting
680 * their files using some other user's key which they don't actually know.
681 * Cryptographically this isn't much of a problem, but the semantics of this
682 * would be a bit weird, so it's best to just forbid it.
684 * The system administrator (CAP_FOWNER) can override this, which should be
685 * enough for any use cases where encryption policies are being set using keys
686 * that were chosen ahead of time but aren't available at the moment.
688 * Note that the key may have already removed by the time this returns, but
689 * that's okay; we just care whether the key was there at some point.
691 * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code
693 int fscrypt_verify_key_added(struct super_block
*sb
,
694 const u8 identifier
[FSCRYPT_KEY_IDENTIFIER_SIZE
])
696 struct fscrypt_key_specifier mk_spec
;
697 struct key
*key
, *mk_user
;
698 struct fscrypt_master_key
*mk
;
701 mk_spec
.type
= FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
;
702 memcpy(mk_spec
.u
.identifier
, identifier
, FSCRYPT_KEY_IDENTIFIER_SIZE
);
704 key
= fscrypt_find_master_key(sb
, &mk_spec
);
709 mk
= key
->payload
.data
[0];
710 mk_user
= find_master_key_user(mk
);
711 if (IS_ERR(mk_user
)) {
712 err
= PTR_ERR(mk_user
);
719 if (err
== -ENOKEY
&& capable(CAP_FOWNER
))
725 * Try to evict the inode's dentries from the dentry cache. If the inode is a
726 * directory, then it can have at most one dentry; however, that dentry may be
727 * pinned by child dentries, so first try to evict the children too.
729 static void shrink_dcache_inode(struct inode
*inode
)
731 struct dentry
*dentry
;
733 if (S_ISDIR(inode
->i_mode
)) {
734 dentry
= d_find_any_alias(inode
);
736 shrink_dcache_parent(dentry
);
740 d_prune_aliases(inode
);
743 static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key
*mk
)
745 struct fscrypt_info
*ci
;
747 struct inode
*toput_inode
= NULL
;
749 spin_lock(&mk
->mk_decrypted_inodes_lock
);
751 list_for_each_entry(ci
, &mk
->mk_decrypted_inodes
, ci_master_key_link
) {
752 inode
= ci
->ci_inode
;
753 spin_lock(&inode
->i_lock
);
754 if (inode
->i_state
& (I_FREEING
| I_WILL_FREE
| I_NEW
)) {
755 spin_unlock(&inode
->i_lock
);
759 spin_unlock(&inode
->i_lock
);
760 spin_unlock(&mk
->mk_decrypted_inodes_lock
);
762 shrink_dcache_inode(inode
);
766 spin_lock(&mk
->mk_decrypted_inodes_lock
);
769 spin_unlock(&mk
->mk_decrypted_inodes_lock
);
773 static int check_for_busy_inodes(struct super_block
*sb
,
774 struct fscrypt_master_key
*mk
)
776 struct list_head
*pos
;
777 size_t busy_count
= 0;
780 spin_lock(&mk
->mk_decrypted_inodes_lock
);
782 list_for_each(pos
, &mk
->mk_decrypted_inodes
)
785 if (busy_count
== 0) {
786 spin_unlock(&mk
->mk_decrypted_inodes_lock
);
791 /* select an example file to show for debugging purposes */
792 struct inode
*inode
=
793 list_first_entry(&mk
->mk_decrypted_inodes
,
795 ci_master_key_link
)->ci_inode
;
798 spin_unlock(&mk
->mk_decrypted_inodes_lock
);
801 "%s: %zu inode(s) still busy after removing key with %s %*phN, including ino %lu",
802 sb
->s_id
, busy_count
, master_key_spec_type(&mk
->mk_spec
),
803 master_key_spec_len(&mk
->mk_spec
), (u8
*)&mk
->mk_spec
.u
,
808 static int try_to_lock_encrypted_files(struct super_block
*sb
,
809 struct fscrypt_master_key
*mk
)
815 * An inode can't be evicted while it is dirty or has dirty pages.
816 * Thus, we first have to clean the inodes in ->mk_decrypted_inodes.
818 * Just do it the easy way: call sync_filesystem(). It's overkill, but
819 * it works, and it's more important to minimize the amount of caches we
820 * drop than the amount of data we sync. Also, unprivileged users can
821 * already call sync_filesystem() via sys_syncfs() or sys_sync().
823 down_read(&sb
->s_umount
);
824 err1
= sync_filesystem(sb
);
825 up_read(&sb
->s_umount
);
826 /* If a sync error occurs, still try to evict as much as possible. */
829 * Inodes are pinned by their dentries, so we have to evict their
830 * dentries. shrink_dcache_sb() would suffice, but would be overkill
831 * and inappropriate for use by unprivileged users. So instead go
832 * through the inodes' alias lists and try to evict each dentry.
834 evict_dentries_for_decrypted_inodes(mk
);
837 * evict_dentries_for_decrypted_inodes() already iput() each inode in
838 * the list; any inodes for which that dropped the last reference will
839 * have been evicted due to fscrypt_drop_inode() detecting the key
840 * removal and telling the VFS to evict the inode. So to finish, we
841 * just need to check whether any inodes couldn't be evicted.
843 err2
= check_for_busy_inodes(sb
, mk
);
849 * Try to remove an fscrypt master encryption key.
851 * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's
852 * claim to the key, then removes the key itself if no other users have claims.
853 * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the
856 * To "remove the key itself", first we wipe the actual master key secret, so
857 * that no more inodes can be unlocked with it. Then we try to evict all cached
858 * inodes that had been unlocked with the key.
860 * If all inodes were evicted, then we unlink the fscrypt_master_key from the
861 * keyring. Otherwise it remains in the keyring in the "incompletely removed"
862 * state (without the actual secret key) where it tracks the list of remaining
863 * inodes. Userspace can execute the ioctl again later to retry eviction, or
864 * alternatively can re-add the secret key again.
866 * For more details, see the "Removing keys" section of
867 * Documentation/filesystems/fscrypt.rst.
869 static int do_remove_key(struct file
*filp
, void __user
*_uarg
, bool all_users
)
871 struct super_block
*sb
= file_inode(filp
)->i_sb
;
872 struct fscrypt_remove_key_arg __user
*uarg
= _uarg
;
873 struct fscrypt_remove_key_arg arg
;
875 struct fscrypt_master_key
*mk
;
876 u32 status_flags
= 0;
880 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
883 if (!valid_key_spec(&arg
.key_spec
))
886 if (memchr_inv(arg
.__reserved
, 0, sizeof(arg
.__reserved
)))
890 * Only root can add and remove keys that are identified by an arbitrary
891 * descriptor rather than by a cryptographic hash.
893 if (arg
.key_spec
.type
== FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR
&&
894 !capable(CAP_SYS_ADMIN
))
897 /* Find the key being removed. */
898 key
= fscrypt_find_master_key(sb
, &arg
.key_spec
);
901 mk
= key
->payload
.data
[0];
903 down_write(&key
->sem
);
905 /* If relevant, remove current user's (or all users) claim to the key */
906 if (mk
->mk_users
&& mk
->mk_users
->keys
.nr_leaves_on_tree
!= 0) {
908 err
= keyring_clear(mk
->mk_users
);
910 err
= remove_master_key_user(mk
);
915 if (mk
->mk_users
->keys
.nr_leaves_on_tree
!= 0) {
917 * Other users have still added the key too. We removed
918 * the current user's claim to the key, but we still
919 * can't remove the key itself.
922 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS
;
929 /* No user claims remaining. Go ahead and wipe the secret. */
931 if (is_master_key_secret_present(&mk
->mk_secret
)) {
932 down_write(&mk
->mk_secret_sem
);
933 wipe_master_key_secret(&mk
->mk_secret
);
934 dead
= refcount_dec_and_test(&mk
->mk_refcount
);
935 up_write(&mk
->mk_secret_sem
);
940 * No inodes reference the key, and we wiped the secret, so the
941 * key object is free to be removed from the keyring.
946 /* Some inodes still reference this key; try to evict them. */
947 err
= try_to_lock_encrypted_files(sb
, mk
);
950 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY
;
955 * We return 0 if we successfully did something: removed a claim to the
956 * key, wiped the secret, or tried locking the files again. Users need
957 * to check the informational status flags if they care whether the key
958 * has been fully removed including all files locked.
963 err
= put_user(status_flags
, &uarg
->removal_status_flags
);
967 int fscrypt_ioctl_remove_key(struct file
*filp
, void __user
*uarg
)
969 return do_remove_key(filp
, uarg
, false);
971 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key
);
973 int fscrypt_ioctl_remove_key_all_users(struct file
*filp
, void __user
*uarg
)
975 if (!capable(CAP_SYS_ADMIN
))
977 return do_remove_key(filp
, uarg
, true);
979 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users
);
982 * Retrieve the status of an fscrypt master encryption key.
984 * We set ->status to indicate whether the key is absent, present, or
985 * incompletely removed. "Incompletely removed" means that the master key
986 * secret has been removed, but some files which had been unlocked with it are
987 * still in use. This field allows applications to easily determine the state
988 * of an encrypted directory without using a hack such as trying to open a
989 * regular file in it (which can confuse the "incompletely removed" state with
990 * absent or present).
992 * In addition, for v2 policy keys we allow applications to determine, via
993 * ->status_flags and ->user_count, whether the key has been added by the
994 * current user, by other users, or by both. Most applications should not need
995 * this, since ordinarily only one user should know a given key. However, if a
996 * secret key is shared by multiple users, applications may wish to add an
997 * already-present key to prevent other users from removing it. This ioctl can
998 * be used to check whether that really is the case before the work is done to
999 * add the key --- which might e.g. require prompting the user for a passphrase.
1001 * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of
1002 * Documentation/filesystems/fscrypt.rst.
1004 int fscrypt_ioctl_get_key_status(struct file
*filp
, void __user
*uarg
)
1006 struct super_block
*sb
= file_inode(filp
)->i_sb
;
1007 struct fscrypt_get_key_status_arg arg
;
1009 struct fscrypt_master_key
*mk
;
1012 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
1015 if (!valid_key_spec(&arg
.key_spec
))
1018 if (memchr_inv(arg
.__reserved
, 0, sizeof(arg
.__reserved
)))
1021 arg
.status_flags
= 0;
1023 memset(arg
.__out_reserved
, 0, sizeof(arg
.__out_reserved
));
1025 key
= fscrypt_find_master_key(sb
, &arg
.key_spec
);
1027 if (key
!= ERR_PTR(-ENOKEY
))
1028 return PTR_ERR(key
);
1029 arg
.status
= FSCRYPT_KEY_STATUS_ABSENT
;
1033 mk
= key
->payload
.data
[0];
1034 down_read(&key
->sem
);
1036 if (!is_master_key_secret_present(&mk
->mk_secret
)) {
1037 arg
.status
= FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED
;
1039 goto out_release_key
;
1042 arg
.status
= FSCRYPT_KEY_STATUS_PRESENT
;
1044 struct key
*mk_user
;
1046 arg
.user_count
= mk
->mk_users
->keys
.nr_leaves_on_tree
;
1047 mk_user
= find_master_key_user(mk
);
1048 if (!IS_ERR(mk_user
)) {
1050 FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF
;
1052 } else if (mk_user
!= ERR_PTR(-ENOKEY
)) {
1053 err
= PTR_ERR(mk_user
);
1054 goto out_release_key
;
1062 if (!err
&& copy_to_user(uarg
, &arg
, sizeof(arg
)))
1066 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status
);
1068 int __init
fscrypt_init_keyring(void)
1072 err
= register_key_type(&key_type_fscrypt
);
1076 err
= register_key_type(&key_type_fscrypt_user
);
1078 goto err_unregister_fscrypt
;
1080 err
= register_key_type(&key_type_fscrypt_provisioning
);
1082 goto err_unregister_fscrypt_user
;
1086 err_unregister_fscrypt_user
:
1087 unregister_key_type(&key_type_fscrypt_user
);
1088 err_unregister_fscrypt
:
1089 unregister_key_type(&key_type_fscrypt
);