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 < ARRAY_SIZE(mk->mk_mode_keys); i++)
47 crypto_free_skcipher(mk->mk_mode_keys[i]);
49 key_put(mk->mk_users);
53 static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec)
57 return master_key_spec_len(spec) != 0;
60 static int fscrypt_key_instantiate(struct key *key,
61 struct key_preparsed_payload *prep)
63 key->payload.data[0] = (struct fscrypt_master_key *)prep->data;
67 static void fscrypt_key_destroy(struct key *key)
69 free_master_key(key->payload.data[0]);
72 static void fscrypt_key_describe(const struct key *key, struct seq_file *m)
74 seq_puts(m, key->description);
76 if (key_is_positive(key)) {
77 const struct fscrypt_master_key *mk = key->payload.data[0];
79 if (!is_master_key_secret_present(&mk->mk_secret))
80 seq_puts(m, ": secret removed");
85 * Type of key in ->s_master_keys. Each key of this type represents a master
86 * key which has been added to the filesystem. Its payload is a
87 * 'struct fscrypt_master_key'. The "." prefix in the key type name prevents
88 * users from adding keys of this type via the keyrings syscalls rather than via
89 * the intended method of FS_IOC_ADD_ENCRYPTION_KEY.
91 static struct key_type key_type_fscrypt = {
93 .instantiate = fscrypt_key_instantiate,
94 .destroy = fscrypt_key_destroy,
95 .describe = fscrypt_key_describe,
98 static int fscrypt_user_key_instantiate(struct key *key,
99 struct key_preparsed_payload *prep)
102 * We just charge FSCRYPT_MAX_KEY_SIZE bytes to the user's key quota for
103 * each key, regardless of the exact key size. The amount of memory
104 * actually used is greater than the size of the raw key anyway.
106 return key_payload_reserve(key, FSCRYPT_MAX_KEY_SIZE);
109 static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m)
111 seq_puts(m, key->description);
115 * Type of key in ->mk_users. Each key of this type represents a particular
116 * user who has added a particular master key.
118 * Note that the name of this key type really should be something like
119 * ".fscrypt-user" instead of simply ".fscrypt". But the shorter name is chosen
120 * mainly for simplicity of presentation in /proc/keys when read by a non-root
121 * user. And it is expected to be rare that a key is actually added by multiple
122 * users, since users should keep their encryption keys confidential.
124 static struct key_type key_type_fscrypt_user = {
126 .instantiate = fscrypt_user_key_instantiate,
127 .describe = fscrypt_user_key_describe,
130 /* Search ->s_master_keys or ->mk_users */
131 static struct key *search_fscrypt_keyring(struct key *keyring,
132 struct key_type *type,
133 const char *description)
136 * We need to mark the keyring reference as "possessed" so that we
137 * acquire permission to search it, via the KEY_POS_SEARCH permission.
139 key_ref_t keyref = make_key_ref(keyring, true /* possessed */);
141 keyref = keyring_search(keyref, type, description, false);
142 if (IS_ERR(keyref)) {
143 if (PTR_ERR(keyref) == -EAGAIN || /* not found */
144 PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
145 keyref = ERR_PTR(-ENOKEY);
146 return ERR_CAST(keyref);
148 return key_ref_to_ptr(keyref);
151 #define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \
152 (CONST_STRLEN("fscrypt-") + FIELD_SIZEOF(struct super_block, s_id))
154 #define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1)
156 #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE \
157 (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
158 CONST_STRLEN("-users") + 1)
160 #define FSCRYPT_MK_USER_DESCRIPTION_SIZE \
161 (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
163 static void format_fs_keyring_description(
164 char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE],
165 const struct super_block *sb)
167 sprintf(description, "fscrypt-%s", sb->s_id);
170 static void format_mk_description(
171 char description[FSCRYPT_MK_DESCRIPTION_SIZE],
172 const struct fscrypt_key_specifier *mk_spec)
174 sprintf(description, "%*phN",
175 master_key_spec_len(mk_spec), (u8 *)&mk_spec->u);
178 static void format_mk_users_keyring_description(
179 char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],
180 const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
182 sprintf(description, "fscrypt-%*phN-users",
183 FSCRYPT_KEY_IDENTIFIER_SIZE, mk_identifier);
186 static void format_mk_user_description(
187 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE],
188 const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
191 sprintf(description, "%*phN.uid.%u", FSCRYPT_KEY_IDENTIFIER_SIZE,
192 mk_identifier, __kuid_val(current_fsuid()));
195 /* Create ->s_master_keys if needed. Synchronized by fscrypt_add_key_mutex. */
196 static int allocate_filesystem_keyring(struct super_block *sb)
198 char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE];
201 if (sb->s_master_keys)
204 format_fs_keyring_description(description, sb);
205 keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
206 current_cred(), KEY_POS_SEARCH |
207 KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
208 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
210 return PTR_ERR(keyring);
212 /* Pairs with READ_ONCE() in fscrypt_find_master_key() */
213 smp_store_release(&sb->s_master_keys, keyring);
217 void fscrypt_sb_free(struct super_block *sb)
219 key_put(sb->s_master_keys);
220 sb->s_master_keys = NULL;
224 * Find the specified master key in ->s_master_keys.
225 * Returns ERR_PTR(-ENOKEY) if not found.
227 struct key *fscrypt_find_master_key(struct super_block *sb,
228 const struct fscrypt_key_specifier *mk_spec)
231 char description[FSCRYPT_MK_DESCRIPTION_SIZE];
233 /* pairs with smp_store_release() in allocate_filesystem_keyring() */
234 keyring = READ_ONCE(sb->s_master_keys);
236 return ERR_PTR(-ENOKEY); /* No keyring yet, so no keys yet. */
238 format_mk_description(description, mk_spec);
239 return search_fscrypt_keyring(keyring, &key_type_fscrypt, description);
242 static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk)
244 char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE];
247 format_mk_users_keyring_description(description,
248 mk->mk_spec.u.identifier);
249 keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
250 current_cred(), KEY_POS_SEARCH |
251 KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
252 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
254 return PTR_ERR(keyring);
256 mk->mk_users = keyring;
261 * Find the current user's "key" in the master key's ->mk_users.
262 * Returns ERR_PTR(-ENOKEY) if not found.
264 static struct key *find_master_key_user(struct fscrypt_master_key *mk)
266 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
268 format_mk_user_description(description, mk->mk_spec.u.identifier);
269 return search_fscrypt_keyring(mk->mk_users, &key_type_fscrypt_user,
274 * Give the current user a "key" in ->mk_users. This charges the user's quota
275 * and marks the master key as added by the current user, so that it cannot be
276 * removed by another user with the key. Either the master key's key->sem must
277 * be held for write, or the master key must be still undergoing initialization.
279 static int add_master_key_user(struct fscrypt_master_key *mk)
281 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
285 format_mk_user_description(description, mk->mk_spec.u.identifier);
286 mk_user = key_alloc(&key_type_fscrypt_user, description,
287 current_fsuid(), current_gid(), current_cred(),
288 KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL);
290 return PTR_ERR(mk_user);
292 err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL);
298 * Remove the current user's "key" from ->mk_users.
299 * The master key's key->sem must be held for write.
301 * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
303 static int remove_master_key_user(struct fscrypt_master_key *mk)
308 mk_user = find_master_key_user(mk);
310 return PTR_ERR(mk_user);
311 err = key_unlink(mk->mk_users, mk_user);
317 * Allocate a new fscrypt_master_key which contains the given secret, set it as
318 * the payload of a new 'struct key' of type fscrypt, and link the 'struct key'
319 * into the given keyring. Synchronized by fscrypt_add_key_mutex.
321 static int add_new_master_key(struct fscrypt_master_key_secret *secret,
322 const struct fscrypt_key_specifier *mk_spec,
325 struct fscrypt_master_key *mk;
326 char description[FSCRYPT_MK_DESCRIPTION_SIZE];
330 mk = kzalloc(sizeof(*mk), GFP_KERNEL);
334 mk->mk_spec = *mk_spec;
336 move_master_key_secret(&mk->mk_secret, secret);
337 init_rwsem(&mk->mk_secret_sem);
339 refcount_set(&mk->mk_refcount, 1); /* secret is present */
340 INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
341 spin_lock_init(&mk->mk_decrypted_inodes_lock);
343 if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
344 err = allocate_master_key_users_keyring(mk);
347 err = add_master_key_user(mk);
353 * Note that we don't charge this key to anyone's quota, since when
354 * ->mk_users is in use those keys are charged instead, and otherwise
355 * (when ->mk_users isn't in use) only root can add these keys.
357 format_mk_description(description, mk_spec);
358 key = key_alloc(&key_type_fscrypt, description,
359 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
360 KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW,
361 KEY_ALLOC_NOT_IN_QUOTA, NULL);
366 err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL);
380 static int add_existing_master_key(struct fscrypt_master_key *mk,
381 struct fscrypt_master_key_secret *secret)
388 * If the current user is already in ->mk_users, then there's nothing to
389 * do. (Not applicable for v1 policy keys, which have NULL ->mk_users.)
392 mk_user = find_master_key_user(mk);
393 if (mk_user != ERR_PTR(-ENOKEY)) {
395 return PTR_ERR(mk_user);
401 /* If we'll be re-adding ->mk_secret, try to take the reference. */
402 rekey = !is_master_key_secret_present(&mk->mk_secret);
403 if (rekey && !refcount_inc_not_zero(&mk->mk_refcount))
406 /* Add the current user to ->mk_users, if applicable. */
408 err = add_master_key_user(mk);
410 if (rekey && refcount_dec_and_test(&mk->mk_refcount))
416 /* Re-add the secret if needed. */
418 down_write(&mk->mk_secret_sem);
419 move_master_key_secret(&mk->mk_secret, secret);
420 up_write(&mk->mk_secret_sem);
425 static int add_master_key(struct super_block *sb,
426 struct fscrypt_master_key_secret *secret,
427 const struct fscrypt_key_specifier *mk_spec)
429 static DEFINE_MUTEX(fscrypt_add_key_mutex);
433 mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */
435 key = fscrypt_find_master_key(sb, mk_spec);
440 /* Didn't find the key in ->s_master_keys. Add it. */
441 err = allocate_filesystem_keyring(sb);
444 err = add_new_master_key(secret, mk_spec, sb->s_master_keys);
447 * Found the key in ->s_master_keys. Re-add the secret if
448 * needed, and add the user to ->mk_users if needed.
450 down_write(&key->sem);
451 err = add_existing_master_key(key->payload.data[0], secret);
453 if (err == KEY_DEAD) {
454 /* Key being removed or needs to be removed */
462 mutex_unlock(&fscrypt_add_key_mutex);
467 * Add a master encryption key to the filesystem, causing all files which were
468 * encrypted with it to appear "unlocked" (decrypted) when accessed.
470 * When adding a key for use by v1 encryption policies, this ioctl is
471 * privileged, and userspace must provide the 'key_descriptor'.
473 * When adding a key for use by v2+ encryption policies, this ioctl is
474 * unprivileged. This is needed, in general, to allow non-root users to use
475 * encryption without encountering the visibility problems of process-subscribed
476 * keyrings and the inability to properly remove keys. This works by having
477 * each key identified by its cryptographically secure hash --- the
478 * 'key_identifier'. The cryptographic hash ensures that a malicious user
479 * cannot add the wrong key for a given identifier. Furthermore, each added key
480 * is charged to the appropriate user's quota for the keyrings service, which
481 * prevents a malicious user from adding too many keys. Finally, we forbid a
482 * user from removing a key while other users have added it too, which prevents
483 * a user who knows another user's key from causing a denial-of-service by
484 * removing it at an inopportune time. (We tolerate that a user who knows a key
485 * can prevent other users from removing it.)
487 * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of
488 * Documentation/filesystems/fscrypt.rst.
490 int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
492 struct super_block *sb = file_inode(filp)->i_sb;
493 struct fscrypt_add_key_arg __user *uarg = _uarg;
494 struct fscrypt_add_key_arg arg;
495 struct fscrypt_master_key_secret secret;
498 if (copy_from_user(&arg, uarg, sizeof(arg)))
501 if (!valid_key_spec(&arg.key_spec))
504 if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE ||
505 arg.raw_size > FSCRYPT_MAX_KEY_SIZE)
508 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
511 memset(&secret, 0, sizeof(secret));
512 secret.size = arg.raw_size;
514 if (copy_from_user(secret.raw, uarg->raw, secret.size))
515 goto out_wipe_secret;
517 switch (arg.key_spec.type) {
518 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
520 * Only root can add keys that are identified by an arbitrary
521 * descriptor rather than by a cryptographic hash --- since
522 * otherwise a malicious user could add the wrong key.
525 if (!capable(CAP_SYS_ADMIN))
526 goto out_wipe_secret;
528 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
529 err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size);
531 goto out_wipe_secret;
534 * Now that the HKDF context is initialized, the raw key is no
537 memzero_explicit(secret.raw, secret.size);
539 /* Calculate the key identifier and return it to userspace. */
540 err = fscrypt_hkdf_expand(&secret.hkdf,
541 HKDF_CONTEXT_KEY_IDENTIFIER,
542 NULL, 0, arg.key_spec.u.identifier,
543 FSCRYPT_KEY_IDENTIFIER_SIZE);
545 goto out_wipe_secret;
547 if (copy_to_user(uarg->key_spec.u.identifier,
548 arg.key_spec.u.identifier,
549 FSCRYPT_KEY_IDENTIFIER_SIZE))
550 goto out_wipe_secret;
555 goto out_wipe_secret;
558 err = add_master_key(sb, &secret, &arg.key_spec);
560 wipe_master_key_secret(&secret);
563 EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
566 * Verify that the current user has added a master key with the given identifier
567 * (returns -ENOKEY if not). This is needed to prevent a user from encrypting
568 * their files using some other user's key which they don't actually know.
569 * Cryptographically this isn't much of a problem, but the semantics of this
570 * would be a bit weird, so it's best to just forbid it.
572 * The system administrator (CAP_FOWNER) can override this, which should be
573 * enough for any use cases where encryption policies are being set using keys
574 * that were chosen ahead of time but aren't available at the moment.
576 * Note that the key may have already removed by the time this returns, but
577 * that's okay; we just care whether the key was there at some point.
579 * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code
581 int fscrypt_verify_key_added(struct super_block *sb,
582 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
584 struct fscrypt_key_specifier mk_spec;
585 struct key *key, *mk_user;
586 struct fscrypt_master_key *mk;
589 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
590 memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
592 key = fscrypt_find_master_key(sb, &mk_spec);
597 mk = key->payload.data[0];
598 mk_user = find_master_key_user(mk);
599 if (IS_ERR(mk_user)) {
600 err = PTR_ERR(mk_user);
607 if (err == -ENOKEY && capable(CAP_FOWNER))
613 * Try to evict the inode's dentries from the dentry cache. If the inode is a
614 * directory, then it can have at most one dentry; however, that dentry may be
615 * pinned by child dentries, so first try to evict the children too.
617 static void shrink_dcache_inode(struct inode *inode)
619 struct dentry *dentry;
621 if (S_ISDIR(inode->i_mode)) {
622 dentry = d_find_any_alias(inode);
624 shrink_dcache_parent(dentry);
628 d_prune_aliases(inode);
631 static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk)
633 struct fscrypt_info *ci;
635 struct inode *toput_inode = NULL;
637 spin_lock(&mk->mk_decrypted_inodes_lock);
639 list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
640 inode = ci->ci_inode;
641 spin_lock(&inode->i_lock);
642 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
643 spin_unlock(&inode->i_lock);
647 spin_unlock(&inode->i_lock);
648 spin_unlock(&mk->mk_decrypted_inodes_lock);
650 shrink_dcache_inode(inode);
654 spin_lock(&mk->mk_decrypted_inodes_lock);
657 spin_unlock(&mk->mk_decrypted_inodes_lock);
661 static int check_for_busy_inodes(struct super_block *sb,
662 struct fscrypt_master_key *mk)
664 struct list_head *pos;
665 size_t busy_count = 0;
668 spin_lock(&mk->mk_decrypted_inodes_lock);
670 list_for_each(pos, &mk->mk_decrypted_inodes)
673 if (busy_count == 0) {
674 spin_unlock(&mk->mk_decrypted_inodes_lock);
679 /* select an example file to show for debugging purposes */
680 struct inode *inode =
681 list_first_entry(&mk->mk_decrypted_inodes,
683 ci_master_key_link)->ci_inode;
686 spin_unlock(&mk->mk_decrypted_inodes_lock);
689 "%s: %zu inode(s) still busy after removing key with %s %*phN, including ino %lu",
690 sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
691 master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
696 static int try_to_lock_encrypted_files(struct super_block *sb,
697 struct fscrypt_master_key *mk)
703 * An inode can't be evicted while it is dirty or has dirty pages.
704 * Thus, we first have to clean the inodes in ->mk_decrypted_inodes.
706 * Just do it the easy way: call sync_filesystem(). It's overkill, but
707 * it works, and it's more important to minimize the amount of caches we
708 * drop than the amount of data we sync. Also, unprivileged users can
709 * already call sync_filesystem() via sys_syncfs() or sys_sync().
711 down_read(&sb->s_umount);
712 err1 = sync_filesystem(sb);
713 up_read(&sb->s_umount);
714 /* If a sync error occurs, still try to evict as much as possible. */
717 * Inodes are pinned by their dentries, so we have to evict their
718 * dentries. shrink_dcache_sb() would suffice, but would be overkill
719 * and inappropriate for use by unprivileged users. So instead go
720 * through the inodes' alias lists and try to evict each dentry.
722 evict_dentries_for_decrypted_inodes(mk);
725 * evict_dentries_for_decrypted_inodes() already iput() each inode in
726 * the list; any inodes for which that dropped the last reference will
727 * have been evicted due to fscrypt_drop_inode() detecting the key
728 * removal and telling the VFS to evict the inode. So to finish, we
729 * just need to check whether any inodes couldn't be evicted.
731 err2 = check_for_busy_inodes(sb, mk);
737 * Try to remove an fscrypt master encryption key.
739 * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's
740 * claim to the key, then removes the key itself if no other users have claims.
741 * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the
744 * To "remove the key itself", first we wipe the actual master key secret, so
745 * that no more inodes can be unlocked with it. Then we try to evict all cached
746 * inodes that had been unlocked with the key.
748 * If all inodes were evicted, then we unlink the fscrypt_master_key from the
749 * keyring. Otherwise it remains in the keyring in the "incompletely removed"
750 * state (without the actual secret key) where it tracks the list of remaining
751 * inodes. Userspace can execute the ioctl again later to retry eviction, or
752 * alternatively can re-add the secret key again.
754 * For more details, see the "Removing keys" section of
755 * Documentation/filesystems/fscrypt.rst.
757 static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
759 struct super_block *sb = file_inode(filp)->i_sb;
760 struct fscrypt_remove_key_arg __user *uarg = _uarg;
761 struct fscrypt_remove_key_arg arg;
763 struct fscrypt_master_key *mk;
764 u32 status_flags = 0;
768 if (copy_from_user(&arg, uarg, sizeof(arg)))
771 if (!valid_key_spec(&arg.key_spec))
774 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
778 * Only root can add and remove keys that are identified by an arbitrary
779 * descriptor rather than by a cryptographic hash.
781 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
782 !capable(CAP_SYS_ADMIN))
785 /* Find the key being removed. */
786 key = fscrypt_find_master_key(sb, &arg.key_spec);
789 mk = key->payload.data[0];
791 down_write(&key->sem);
793 /* If relevant, remove current user's (or all users) claim to the key */
794 if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
796 err = keyring_clear(mk->mk_users);
798 err = remove_master_key_user(mk);
803 if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
805 * Other users have still added the key too. We removed
806 * the current user's claim to the key, but we still
807 * can't remove the key itself.
810 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
817 /* No user claims remaining. Go ahead and wipe the secret. */
819 if (is_master_key_secret_present(&mk->mk_secret)) {
820 down_write(&mk->mk_secret_sem);
821 wipe_master_key_secret(&mk->mk_secret);
822 dead = refcount_dec_and_test(&mk->mk_refcount);
823 up_write(&mk->mk_secret_sem);
828 * No inodes reference the key, and we wiped the secret, so the
829 * key object is free to be removed from the keyring.
834 /* Some inodes still reference this key; try to evict them. */
835 err = try_to_lock_encrypted_files(sb, mk);
838 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
843 * We return 0 if we successfully did something: removed a claim to the
844 * key, wiped the secret, or tried locking the files again. Users need
845 * to check the informational status flags if they care whether the key
846 * has been fully removed including all files locked.
851 err = put_user(status_flags, &uarg->removal_status_flags);
855 int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
857 return do_remove_key(filp, uarg, false);
859 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key);
861 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
863 if (!capable(CAP_SYS_ADMIN))
865 return do_remove_key(filp, uarg, true);
867 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users);
870 * Retrieve the status of an fscrypt master encryption key.
872 * We set ->status to indicate whether the key is absent, present, or
873 * incompletely removed. "Incompletely removed" means that the master key
874 * secret has been removed, but some files which had been unlocked with it are
875 * still in use. This field allows applications to easily determine the state
876 * of an encrypted directory without using a hack such as trying to open a
877 * regular file in it (which can confuse the "incompletely removed" state with
878 * absent or present).
880 * In addition, for v2 policy keys we allow applications to determine, via
881 * ->status_flags and ->user_count, whether the key has been added by the
882 * current user, by other users, or by both. Most applications should not need
883 * this, since ordinarily only one user should know a given key. However, if a
884 * secret key is shared by multiple users, applications may wish to add an
885 * already-present key to prevent other users from removing it. This ioctl can
886 * be used to check whether that really is the case before the work is done to
887 * add the key --- which might e.g. require prompting the user for a passphrase.
889 * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of
890 * Documentation/filesystems/fscrypt.rst.
892 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
894 struct super_block *sb = file_inode(filp)->i_sb;
895 struct fscrypt_get_key_status_arg arg;
897 struct fscrypt_master_key *mk;
900 if (copy_from_user(&arg, uarg, sizeof(arg)))
903 if (!valid_key_spec(&arg.key_spec))
906 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
909 arg.status_flags = 0;
911 memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
913 key = fscrypt_find_master_key(sb, &arg.key_spec);
915 if (key != ERR_PTR(-ENOKEY))
917 arg.status = FSCRYPT_KEY_STATUS_ABSENT;
921 mk = key->payload.data[0];
922 down_read(&key->sem);
924 if (!is_master_key_secret_present(&mk->mk_secret)) {
925 arg.status = FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED;
927 goto out_release_key;
930 arg.status = FSCRYPT_KEY_STATUS_PRESENT;
934 arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
935 mk_user = find_master_key_user(mk);
936 if (!IS_ERR(mk_user)) {
938 FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
940 } else if (mk_user != ERR_PTR(-ENOKEY)) {
941 err = PTR_ERR(mk_user);
942 goto out_release_key;
950 if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
954 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status);
956 int __init fscrypt_init_keyring(void)
960 err = register_key_type(&key_type_fscrypt);
964 err = register_key_type(&key_type_fscrypt_user);
966 goto err_unregister_fscrypt;
970 err_unregister_fscrypt:
971 unregister_key_type(&key_type_fscrypt);