xref: /openbmc/linux/fs/crypto/keyring.c (revision 41b2ad80)
122d94f49SEric Biggers // SPDX-License-Identifier: GPL-2.0
222d94f49SEric Biggers /*
322d94f49SEric Biggers  * Filesystem-level keyring for fscrypt
422d94f49SEric Biggers  *
522d94f49SEric Biggers  * Copyright 2019 Google LLC
622d94f49SEric Biggers  */
722d94f49SEric Biggers 
822d94f49SEric Biggers /*
922d94f49SEric Biggers  * This file implements management of fscrypt master keys in the
1022d94f49SEric Biggers  * filesystem-level keyring, including the ioctls:
1122d94f49SEric Biggers  *
1222d94f49SEric Biggers  * - FS_IOC_ADD_ENCRYPTION_KEY
13b1c0ec35SEric Biggers  * - FS_IOC_REMOVE_ENCRYPTION_KEY
1478a1b96bSEric Biggers  * - FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
155a7e2992SEric Biggers  * - FS_IOC_GET_ENCRYPTION_KEY_STATUS
1622d94f49SEric Biggers  *
1722d94f49SEric Biggers  * See the "User API" section of Documentation/filesystems/fscrypt.rst for more
1822d94f49SEric Biggers  * information about these ioctls.
1922d94f49SEric Biggers  */
2022d94f49SEric Biggers 
21d7e7b9afSEric Biggers #include <asm/unaligned.h>
225dae460cSEric Biggers #include <crypto/skcipher.h>
2322d94f49SEric Biggers #include <linux/key-type.h>
24cdeb21daSEric Biggers #include <linux/random.h>
2522d94f49SEric Biggers #include <linux/seq_file.h>
2622d94f49SEric Biggers 
2722d94f49SEric Biggers #include "fscrypt_private.h"
2822d94f49SEric Biggers 
29d7e7b9afSEric Biggers /* The master encryption keys for a filesystem (->s_master_keys) */
30d7e7b9afSEric Biggers struct fscrypt_keyring {
31d7e7b9afSEric Biggers 	/*
32d7e7b9afSEric Biggers 	 * Lock that protects ->key_hashtable.  It does *not* protect the
33d7e7b9afSEric Biggers 	 * fscrypt_master_key structs themselves.
34d7e7b9afSEric Biggers 	 */
35d7e7b9afSEric Biggers 	spinlock_t lock;
36d7e7b9afSEric Biggers 
37d7e7b9afSEric Biggers 	/* Hash table that maps fscrypt_key_specifier to fscrypt_master_key */
38d7e7b9afSEric Biggers 	struct hlist_head key_hashtable[128];
39d7e7b9afSEric Biggers };
40d7e7b9afSEric Biggers 
wipe_master_key_secret(struct fscrypt_master_key_secret * secret)4122d94f49SEric Biggers static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
4222d94f49SEric Biggers {
435dae460cSEric Biggers 	fscrypt_destroy_hkdf(&secret->hkdf);
4422d94f49SEric Biggers 	memzero_explicit(secret, sizeof(*secret));
4522d94f49SEric Biggers }
4622d94f49SEric Biggers 
move_master_key_secret(struct fscrypt_master_key_secret * dst,struct fscrypt_master_key_secret * src)4722d94f49SEric Biggers static void move_master_key_secret(struct fscrypt_master_key_secret *dst,
4822d94f49SEric Biggers 				   struct fscrypt_master_key_secret *src)
4922d94f49SEric Biggers {
5022d94f49SEric Biggers 	memcpy(dst, src, sizeof(*dst));
5122d94f49SEric Biggers 	memzero_explicit(src, sizeof(*src));
5222d94f49SEric Biggers }
5322d94f49SEric Biggers 
fscrypt_free_master_key(struct rcu_head * head)54d7e7b9afSEric Biggers static void fscrypt_free_master_key(struct rcu_head *head)
5522d94f49SEric Biggers {
56d7e7b9afSEric Biggers 	struct fscrypt_master_key *mk =
57d7e7b9afSEric Biggers 		container_of(head, struct fscrypt_master_key, mk_rcu_head);
58d7e7b9afSEric Biggers 	/*
59d7e7b9afSEric Biggers 	 * The master key secret and any embedded subkeys should have already
60d7e7b9afSEric Biggers 	 * been wiped when the last active reference to the fscrypt_master_key
61d7e7b9afSEric Biggers 	 * struct was dropped; doing it here would be unnecessarily late.
62d7e7b9afSEric Biggers 	 * Nevertheless, use kfree_sensitive() in case anything was missed.
63d7e7b9afSEric Biggers 	 */
64d7e7b9afSEric Biggers 	kfree_sensitive(mk);
65d7e7b9afSEric Biggers }
66d7e7b9afSEric Biggers 
fscrypt_put_master_key(struct fscrypt_master_key * mk)67d7e7b9afSEric Biggers void fscrypt_put_master_key(struct fscrypt_master_key *mk)
68d7e7b9afSEric Biggers {
69d7e7b9afSEric Biggers 	if (!refcount_dec_and_test(&mk->mk_struct_refs))
70d7e7b9afSEric Biggers 		return;
71d7e7b9afSEric Biggers 	/*
72d7e7b9afSEric Biggers 	 * No structural references left, so free ->mk_users, and also free the
73d7e7b9afSEric Biggers 	 * fscrypt_master_key struct itself after an RCU grace period ensures
74d7e7b9afSEric Biggers 	 * that concurrent keyring lookups can no longer find it.
75d7e7b9afSEric Biggers 	 */
76*41b2ad80SEric Biggers 	WARN_ON_ONCE(refcount_read(&mk->mk_active_refs) != 0);
77d7e7b9afSEric Biggers 	key_put(mk->mk_users);
78d7e7b9afSEric Biggers 	mk->mk_users = NULL;
79d7e7b9afSEric Biggers 	call_rcu(&mk->mk_rcu_head, fscrypt_free_master_key);
80d7e7b9afSEric Biggers }
81d7e7b9afSEric Biggers 
fscrypt_put_master_key_activeref(struct super_block * sb,struct fscrypt_master_key * mk)8202aef422SEric Biggers void fscrypt_put_master_key_activeref(struct super_block *sb,
8302aef422SEric Biggers 				      struct fscrypt_master_key *mk)
84d7e7b9afSEric Biggers {
855dae460cSEric Biggers 	size_t i;
865dae460cSEric Biggers 
87d7e7b9afSEric Biggers 	if (!refcount_dec_and_test(&mk->mk_active_refs))
88d7e7b9afSEric Biggers 		return;
89d7e7b9afSEric Biggers 	/*
90d7e7b9afSEric Biggers 	 * No active references left, so complete the full removal of this
91d7e7b9afSEric Biggers 	 * fscrypt_master_key struct by removing it from the keyring and
92d7e7b9afSEric Biggers 	 * destroying any subkeys embedded in it.
93d7e7b9afSEric Biggers 	 */
94d7e7b9afSEric Biggers 
95*41b2ad80SEric Biggers 	if (WARN_ON_ONCE(!sb->s_master_keys))
964bcf6f82SEric Biggers 		return;
9702aef422SEric Biggers 	spin_lock(&sb->s_master_keys->lock);
98d7e7b9afSEric Biggers 	hlist_del_rcu(&mk->mk_node);
9902aef422SEric Biggers 	spin_unlock(&sb->s_master_keys->lock);
100d7e7b9afSEric Biggers 
101d7e7b9afSEric Biggers 	/*
102d7e7b9afSEric Biggers 	 * ->mk_active_refs == 0 implies that ->mk_secret is not present and
103d7e7b9afSEric Biggers 	 * that ->mk_decrypted_inodes is empty.
104d7e7b9afSEric Biggers 	 */
105*41b2ad80SEric Biggers 	WARN_ON_ONCE(is_master_key_secret_present(&mk->mk_secret));
106*41b2ad80SEric Biggers 	WARN_ON_ONCE(!list_empty(&mk->mk_decrypted_inodes));
1075dae460cSEric Biggers 
1083ceb6543SEric Biggers 	for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
10922e9947aSEric Biggers 		fscrypt_destroy_prepared_key(
11022e9947aSEric Biggers 				sb, &mk->mk_direct_keys[i]);
11122e9947aSEric Biggers 		fscrypt_destroy_prepared_key(
11222e9947aSEric Biggers 				sb, &mk->mk_iv_ino_lblk_64_keys[i]);
11322e9947aSEric Biggers 		fscrypt_destroy_prepared_key(
11422e9947aSEric Biggers 				sb, &mk->mk_iv_ino_lblk_32_keys[i]);
115b103fb76SEric Biggers 	}
116d7e7b9afSEric Biggers 	memzero_explicit(&mk->mk_ino_hash_key,
117d7e7b9afSEric Biggers 			 sizeof(mk->mk_ino_hash_key));
118d7e7b9afSEric Biggers 	mk->mk_ino_hash_key_initialized = false;
1195dae460cSEric Biggers 
120d7e7b9afSEric Biggers 	/* Drop the structural ref associated with the active refs. */
121d7e7b9afSEric Biggers 	fscrypt_put_master_key(mk);
12222d94f49SEric Biggers }
12322d94f49SEric Biggers 
valid_key_spec(const struct fscrypt_key_specifier * spec)12422d94f49SEric Biggers static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec)
12522d94f49SEric Biggers {
12622d94f49SEric Biggers 	if (spec->__reserved)
12722d94f49SEric Biggers 		return false;
12822d94f49SEric Biggers 	return master_key_spec_len(spec) != 0;
12922d94f49SEric Biggers }
13022d94f49SEric Biggers 
fscrypt_user_key_instantiate(struct key * key,struct key_preparsed_payload * prep)13123c688b5SEric Biggers static int fscrypt_user_key_instantiate(struct key *key,
13223c688b5SEric Biggers 					struct key_preparsed_payload *prep)
13323c688b5SEric Biggers {
13423c688b5SEric Biggers 	/*
13523c688b5SEric Biggers 	 * We just charge FSCRYPT_MAX_KEY_SIZE bytes to the user's key quota for
13623c688b5SEric Biggers 	 * each key, regardless of the exact key size.  The amount of memory
13723c688b5SEric Biggers 	 * actually used is greater than the size of the raw key anyway.
13823c688b5SEric Biggers 	 */
13923c688b5SEric Biggers 	return key_payload_reserve(key, FSCRYPT_MAX_KEY_SIZE);
14023c688b5SEric Biggers }
14123c688b5SEric Biggers 
fscrypt_user_key_describe(const struct key * key,struct seq_file * m)14223c688b5SEric Biggers static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m)
14323c688b5SEric Biggers {
14423c688b5SEric Biggers 	seq_puts(m, key->description);
14523c688b5SEric Biggers }
14623c688b5SEric Biggers 
14723c688b5SEric Biggers /*
14823c688b5SEric Biggers  * Type of key in ->mk_users.  Each key of this type represents a particular
14923c688b5SEric Biggers  * user who has added a particular master key.
15023c688b5SEric Biggers  *
15123c688b5SEric Biggers  * Note that the name of this key type really should be something like
15223c688b5SEric Biggers  * ".fscrypt-user" instead of simply ".fscrypt".  But the shorter name is chosen
15323c688b5SEric Biggers  * mainly for simplicity of presentation in /proc/keys when read by a non-root
15423c688b5SEric Biggers  * user.  And it is expected to be rare that a key is actually added by multiple
15523c688b5SEric Biggers  * users, since users should keep their encryption keys confidential.
15623c688b5SEric Biggers  */
15723c688b5SEric Biggers static struct key_type key_type_fscrypt_user = {
15823c688b5SEric Biggers 	.name			= ".fscrypt",
15923c688b5SEric Biggers 	.instantiate		= fscrypt_user_key_instantiate,
16023c688b5SEric Biggers 	.describe		= fscrypt_user_key_describe,
16123c688b5SEric Biggers };
16223c688b5SEric Biggers 
16323c688b5SEric Biggers #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE	\
16423c688b5SEric Biggers 	(CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
16523c688b5SEric Biggers 	 CONST_STRLEN("-users") + 1)
16623c688b5SEric Biggers 
16723c688b5SEric Biggers #define FSCRYPT_MK_USER_DESCRIPTION_SIZE	\
16823c688b5SEric Biggers 	(2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
16923c688b5SEric Biggers 
format_mk_users_keyring_description(char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])17023c688b5SEric Biggers static void format_mk_users_keyring_description(
17123c688b5SEric Biggers 			char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],
17223c688b5SEric Biggers 			const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
17323c688b5SEric Biggers {
17423c688b5SEric Biggers 	sprintf(description, "fscrypt-%*phN-users",
17523c688b5SEric Biggers 		FSCRYPT_KEY_IDENTIFIER_SIZE, mk_identifier);
17623c688b5SEric Biggers }
17723c688b5SEric Biggers 
format_mk_user_description(char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE],const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])17823c688b5SEric Biggers static void format_mk_user_description(
17923c688b5SEric Biggers 			char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE],
18023c688b5SEric Biggers 			const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
18123c688b5SEric Biggers {
18223c688b5SEric Biggers 
18323c688b5SEric Biggers 	sprintf(description, "%*phN.uid.%u", FSCRYPT_KEY_IDENTIFIER_SIZE,
18423c688b5SEric Biggers 		mk_identifier, __kuid_val(current_fsuid()));
18523c688b5SEric Biggers }
18623c688b5SEric Biggers 
18722d94f49SEric Biggers /* Create ->s_master_keys if needed.  Synchronized by fscrypt_add_key_mutex. */
allocate_filesystem_keyring(struct super_block * sb)18822d94f49SEric Biggers static int allocate_filesystem_keyring(struct super_block *sb)
18922d94f49SEric Biggers {
190d7e7b9afSEric Biggers 	struct fscrypt_keyring *keyring;
19122d94f49SEric Biggers 
19222d94f49SEric Biggers 	if (sb->s_master_keys)
19322d94f49SEric Biggers 		return 0;
19422d94f49SEric Biggers 
195d7e7b9afSEric Biggers 	keyring = kzalloc(sizeof(*keyring), GFP_KERNEL);
196d7e7b9afSEric Biggers 	if (!keyring)
197d7e7b9afSEric Biggers 		return -ENOMEM;
198d7e7b9afSEric Biggers 	spin_lock_init(&keyring->lock);
199777afe4eSEric Biggers 	/*
200777afe4eSEric Biggers 	 * Pairs with the smp_load_acquire() in fscrypt_find_master_key().
201777afe4eSEric Biggers 	 * I.e., here we publish ->s_master_keys with a RELEASE barrier so that
202777afe4eSEric Biggers 	 * concurrent tasks can ACQUIRE it.
203777afe4eSEric Biggers 	 */
20422d94f49SEric Biggers 	smp_store_release(&sb->s_master_keys, keyring);
20522d94f49SEric Biggers 	return 0;
20622d94f49SEric Biggers }
20722d94f49SEric Biggers 
208d7e7b9afSEric Biggers /*
209ccd30a47SEric Biggers  * Release all encryption keys that have been added to the filesystem, along
210ccd30a47SEric Biggers  * with the keyring that contains them.
211d7e7b9afSEric Biggers  *
21243e5f1d5SEric Biggers  * This is called at unmount time, after all potentially-encrypted inodes have
21343e5f1d5SEric Biggers  * been evicted.  The filesystem's underlying block device(s) are still
21443e5f1d5SEric Biggers  * available at this time; this is important because after user file accesses
21543e5f1d5SEric Biggers  * have been allowed, this function may need to evict keys from the keyslots of
21643e5f1d5SEric Biggers  * an inline crypto engine, which requires the block device(s).
217d7e7b9afSEric Biggers  */
fscrypt_destroy_keyring(struct super_block * sb)218ccd30a47SEric Biggers void fscrypt_destroy_keyring(struct super_block *sb)
21922d94f49SEric Biggers {
220d7e7b9afSEric Biggers 	struct fscrypt_keyring *keyring = sb->s_master_keys;
221d7e7b9afSEric Biggers 	size_t i;
222d7e7b9afSEric Biggers 
223d7e7b9afSEric Biggers 	if (!keyring)
224d7e7b9afSEric Biggers 		return;
225d7e7b9afSEric Biggers 
226d7e7b9afSEric Biggers 	for (i = 0; i < ARRAY_SIZE(keyring->key_hashtable); i++) {
227d7e7b9afSEric Biggers 		struct hlist_head *bucket = &keyring->key_hashtable[i];
228d7e7b9afSEric Biggers 		struct fscrypt_master_key *mk;
229d7e7b9afSEric Biggers 		struct hlist_node *tmp;
230d7e7b9afSEric Biggers 
231d7e7b9afSEric Biggers 		hlist_for_each_entry_safe(mk, tmp, bucket, mk_node) {
232d7e7b9afSEric Biggers 			/*
23343e5f1d5SEric Biggers 			 * Since all potentially-encrypted inodes were already
23443e5f1d5SEric Biggers 			 * evicted, every key remaining in the keyring should
23543e5f1d5SEric Biggers 			 * have an empty inode list, and should only still be in
23643e5f1d5SEric Biggers 			 * the keyring due to the single active ref associated
23743e5f1d5SEric Biggers 			 * with ->mk_secret.  There should be no structural refs
23843e5f1d5SEric Biggers 			 * beyond the one associated with the active ref.
239d7e7b9afSEric Biggers 			 */
240*41b2ad80SEric Biggers 			WARN_ON_ONCE(refcount_read(&mk->mk_active_refs) != 1);
241*41b2ad80SEric Biggers 			WARN_ON_ONCE(refcount_read(&mk->mk_struct_refs) != 1);
242*41b2ad80SEric Biggers 			WARN_ON_ONCE(!is_master_key_secret_present(&mk->mk_secret));
243d7e7b9afSEric Biggers 			wipe_master_key_secret(&mk->mk_secret);
24402aef422SEric Biggers 			fscrypt_put_master_key_activeref(sb, mk);
245d7e7b9afSEric Biggers 		}
246d7e7b9afSEric Biggers 	}
247d7e7b9afSEric Biggers 	kfree_sensitive(keyring);
24822d94f49SEric Biggers 	sb->s_master_keys = NULL;
24922d94f49SEric Biggers }
25022d94f49SEric Biggers 
251d7e7b9afSEric Biggers static struct hlist_head *
fscrypt_mk_hash_bucket(struct fscrypt_keyring * keyring,const struct fscrypt_key_specifier * mk_spec)252d7e7b9afSEric Biggers fscrypt_mk_hash_bucket(struct fscrypt_keyring *keyring,
25322d94f49SEric Biggers 		       const struct fscrypt_key_specifier *mk_spec)
25422d94f49SEric Biggers {
255d7e7b9afSEric Biggers 	/*
256d7e7b9afSEric Biggers 	 * Since key specifiers should be "random" values, it is sufficient to
257d7e7b9afSEric Biggers 	 * use a trivial hash function that just takes the first several bits of
258d7e7b9afSEric Biggers 	 * the key specifier.
259d7e7b9afSEric Biggers 	 */
260d7e7b9afSEric Biggers 	unsigned long i = get_unaligned((unsigned long *)&mk_spec->u);
261d7e7b9afSEric Biggers 
262d7e7b9afSEric Biggers 	return &keyring->key_hashtable[i % ARRAY_SIZE(keyring->key_hashtable)];
263d7e7b9afSEric Biggers }
264d7e7b9afSEric Biggers 
265d7e7b9afSEric Biggers /*
266d7e7b9afSEric Biggers  * Find the specified master key struct in ->s_master_keys and take a structural
267d7e7b9afSEric Biggers  * ref to it.  The structural ref guarantees that the key struct continues to
268d7e7b9afSEric Biggers  * exist, but it does *not* guarantee that ->s_master_keys continues to contain
269d7e7b9afSEric Biggers  * the key struct.  The structural ref needs to be dropped by
270d7e7b9afSEric Biggers  * fscrypt_put_master_key().  Returns NULL if the key struct is not found.
271d7e7b9afSEric Biggers  */
272d7e7b9afSEric Biggers struct fscrypt_master_key *
fscrypt_find_master_key(struct super_block * sb,const struct fscrypt_key_specifier * mk_spec)273d7e7b9afSEric Biggers fscrypt_find_master_key(struct super_block *sb,
274d7e7b9afSEric Biggers 			const struct fscrypt_key_specifier *mk_spec)
275d7e7b9afSEric Biggers {
276d7e7b9afSEric Biggers 	struct fscrypt_keyring *keyring;
277d7e7b9afSEric Biggers 	struct hlist_head *bucket;
278d7e7b9afSEric Biggers 	struct fscrypt_master_key *mk;
27922d94f49SEric Biggers 
280777afe4eSEric Biggers 	/*
281777afe4eSEric Biggers 	 * Pairs with the smp_store_release() in allocate_filesystem_keyring().
282777afe4eSEric Biggers 	 * I.e., another task can publish ->s_master_keys concurrently,
283777afe4eSEric Biggers 	 * executing a RELEASE barrier.  We need to use smp_load_acquire() here
284777afe4eSEric Biggers 	 * to safely ACQUIRE the memory the other task published.
285777afe4eSEric Biggers 	 */
286777afe4eSEric Biggers 	keyring = smp_load_acquire(&sb->s_master_keys);
28722d94f49SEric Biggers 	if (keyring == NULL)
288d7e7b9afSEric Biggers 		return NULL; /* No keyring yet, so no keys yet. */
28922d94f49SEric Biggers 
290d7e7b9afSEric Biggers 	bucket = fscrypt_mk_hash_bucket(keyring, mk_spec);
291d7e7b9afSEric Biggers 	rcu_read_lock();
292d7e7b9afSEric Biggers 	switch (mk_spec->type) {
293d7e7b9afSEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
294d7e7b9afSEric Biggers 		hlist_for_each_entry_rcu(mk, bucket, mk_node) {
295d7e7b9afSEric Biggers 			if (mk->mk_spec.type ==
296d7e7b9afSEric Biggers 				FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
297d7e7b9afSEric Biggers 			    memcmp(mk->mk_spec.u.descriptor,
298d7e7b9afSEric Biggers 				   mk_spec->u.descriptor,
299d7e7b9afSEric Biggers 				   FSCRYPT_KEY_DESCRIPTOR_SIZE) == 0 &&
300d7e7b9afSEric Biggers 			    refcount_inc_not_zero(&mk->mk_struct_refs))
301d7e7b9afSEric Biggers 				goto out;
302d7e7b9afSEric Biggers 		}
303d7e7b9afSEric Biggers 		break;
304d7e7b9afSEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
305d7e7b9afSEric Biggers 		hlist_for_each_entry_rcu(mk, bucket, mk_node) {
306d7e7b9afSEric Biggers 			if (mk->mk_spec.type ==
307d7e7b9afSEric Biggers 				FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
308d7e7b9afSEric Biggers 			    memcmp(mk->mk_spec.u.identifier,
309d7e7b9afSEric Biggers 				   mk_spec->u.identifier,
310d7e7b9afSEric Biggers 				   FSCRYPT_KEY_IDENTIFIER_SIZE) == 0 &&
311d7e7b9afSEric Biggers 			    refcount_inc_not_zero(&mk->mk_struct_refs))
312d7e7b9afSEric Biggers 				goto out;
313d7e7b9afSEric Biggers 		}
314d7e7b9afSEric Biggers 		break;
315d7e7b9afSEric Biggers 	}
316d7e7b9afSEric Biggers 	mk = NULL;
317d7e7b9afSEric Biggers out:
318d7e7b9afSEric Biggers 	rcu_read_unlock();
319d7e7b9afSEric Biggers 	return mk;
32022d94f49SEric Biggers }
32122d94f49SEric Biggers 
allocate_master_key_users_keyring(struct fscrypt_master_key * mk)32223c688b5SEric Biggers static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk)
32323c688b5SEric Biggers {
32423c688b5SEric Biggers 	char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE];
32523c688b5SEric Biggers 	struct key *keyring;
32623c688b5SEric Biggers 
32723c688b5SEric Biggers 	format_mk_users_keyring_description(description,
32823c688b5SEric Biggers 					    mk->mk_spec.u.identifier);
32923c688b5SEric Biggers 	keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
33023c688b5SEric Biggers 				current_cred(), KEY_POS_SEARCH |
33123c688b5SEric Biggers 				  KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
33223c688b5SEric Biggers 				KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
33323c688b5SEric Biggers 	if (IS_ERR(keyring))
33423c688b5SEric Biggers 		return PTR_ERR(keyring);
33523c688b5SEric Biggers 
33623c688b5SEric Biggers 	mk->mk_users = keyring;
33723c688b5SEric Biggers 	return 0;
33823c688b5SEric Biggers }
33923c688b5SEric Biggers 
34023c688b5SEric Biggers /*
34123c688b5SEric Biggers  * Find the current user's "key" in the master key's ->mk_users.
34223c688b5SEric Biggers  * Returns ERR_PTR(-ENOKEY) if not found.
34323c688b5SEric Biggers  */
find_master_key_user(struct fscrypt_master_key * mk)34423c688b5SEric Biggers static struct key *find_master_key_user(struct fscrypt_master_key *mk)
34523c688b5SEric Biggers {
34623c688b5SEric Biggers 	char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
347d7e7b9afSEric Biggers 	key_ref_t keyref;
34823c688b5SEric Biggers 
34923c688b5SEric Biggers 	format_mk_user_description(description, mk->mk_spec.u.identifier);
350d7e7b9afSEric Biggers 
351d7e7b9afSEric Biggers 	/*
352d7e7b9afSEric Biggers 	 * We need to mark the keyring reference as "possessed" so that we
353d7e7b9afSEric Biggers 	 * acquire permission to search it, via the KEY_POS_SEARCH permission.
354d7e7b9afSEric Biggers 	 */
355d7e7b9afSEric Biggers 	keyref = keyring_search(make_key_ref(mk->mk_users, true /*possessed*/),
356d7e7b9afSEric Biggers 				&key_type_fscrypt_user, description, false);
357d7e7b9afSEric Biggers 	if (IS_ERR(keyref)) {
358d7e7b9afSEric Biggers 		if (PTR_ERR(keyref) == -EAGAIN || /* not found */
359d7e7b9afSEric Biggers 		    PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
360d7e7b9afSEric Biggers 			keyref = ERR_PTR(-ENOKEY);
361d7e7b9afSEric Biggers 		return ERR_CAST(keyref);
362d7e7b9afSEric Biggers 	}
363d7e7b9afSEric Biggers 	return key_ref_to_ptr(keyref);
36423c688b5SEric Biggers }
36523c688b5SEric Biggers 
36623c688b5SEric Biggers /*
36723c688b5SEric Biggers  * Give the current user a "key" in ->mk_users.  This charges the user's quota
36823c688b5SEric Biggers  * and marks the master key as added by the current user, so that it cannot be
369d7e7b9afSEric Biggers  * removed by another user with the key.  Either ->mk_sem must be held for
370d7e7b9afSEric Biggers  * write, or the master key must be still undergoing initialization.
37123c688b5SEric Biggers  */
add_master_key_user(struct fscrypt_master_key * mk)37223c688b5SEric Biggers static int add_master_key_user(struct fscrypt_master_key *mk)
37323c688b5SEric Biggers {
37423c688b5SEric Biggers 	char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
37523c688b5SEric Biggers 	struct key *mk_user;
37623c688b5SEric Biggers 	int err;
37723c688b5SEric Biggers 
37823c688b5SEric Biggers 	format_mk_user_description(description, mk->mk_spec.u.identifier);
37923c688b5SEric Biggers 	mk_user = key_alloc(&key_type_fscrypt_user, description,
38023c688b5SEric Biggers 			    current_fsuid(), current_gid(), current_cred(),
38123c688b5SEric Biggers 			    KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL);
38223c688b5SEric Biggers 	if (IS_ERR(mk_user))
38323c688b5SEric Biggers 		return PTR_ERR(mk_user);
38423c688b5SEric Biggers 
38523c688b5SEric Biggers 	err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL);
38623c688b5SEric Biggers 	key_put(mk_user);
38723c688b5SEric Biggers 	return err;
38823c688b5SEric Biggers }
38923c688b5SEric Biggers 
39023c688b5SEric Biggers /*
39123c688b5SEric Biggers  * Remove the current user's "key" from ->mk_users.
392d7e7b9afSEric Biggers  * ->mk_sem must be held for write.
39323c688b5SEric Biggers  *
39423c688b5SEric Biggers  * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
39523c688b5SEric Biggers  */
remove_master_key_user(struct fscrypt_master_key * mk)39623c688b5SEric Biggers static int remove_master_key_user(struct fscrypt_master_key *mk)
39723c688b5SEric Biggers {
39823c688b5SEric Biggers 	struct key *mk_user;
39923c688b5SEric Biggers 	int err;
40023c688b5SEric Biggers 
40123c688b5SEric Biggers 	mk_user = find_master_key_user(mk);
40223c688b5SEric Biggers 	if (IS_ERR(mk_user))
40323c688b5SEric Biggers 		return PTR_ERR(mk_user);
40423c688b5SEric Biggers 	err = key_unlink(mk->mk_users, mk_user);
40523c688b5SEric Biggers 	key_put(mk_user);
40623c688b5SEric Biggers 	return err;
40723c688b5SEric Biggers }
40823c688b5SEric Biggers 
40922d94f49SEric Biggers /*
410d7e7b9afSEric Biggers  * Allocate a new fscrypt_master_key, transfer the given secret over to it, and
411d7e7b9afSEric Biggers  * insert it into sb->s_master_keys.
41222d94f49SEric Biggers  */
add_new_master_key(struct super_block * sb,struct fscrypt_master_key_secret * secret,const struct fscrypt_key_specifier * mk_spec)413d7e7b9afSEric Biggers static int add_new_master_key(struct super_block *sb,
414d7e7b9afSEric Biggers 			      struct fscrypt_master_key_secret *secret,
415d7e7b9afSEric Biggers 			      const struct fscrypt_key_specifier *mk_spec)
41622d94f49SEric Biggers {
417d7e7b9afSEric Biggers 	struct fscrypt_keyring *keyring = sb->s_master_keys;
41822d94f49SEric Biggers 	struct fscrypt_master_key *mk;
41922d94f49SEric Biggers 	int err;
42022d94f49SEric Biggers 
42122d94f49SEric Biggers 	mk = kzalloc(sizeof(*mk), GFP_KERNEL);
42222d94f49SEric Biggers 	if (!mk)
42322d94f49SEric Biggers 		return -ENOMEM;
42422d94f49SEric Biggers 
425d7e7b9afSEric Biggers 	init_rwsem(&mk->mk_sem);
426d7e7b9afSEric Biggers 	refcount_set(&mk->mk_struct_refs, 1);
42722d94f49SEric Biggers 	mk->mk_spec = *mk_spec;
42822d94f49SEric Biggers 
429b1c0ec35SEric Biggers 	INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
430b1c0ec35SEric Biggers 	spin_lock_init(&mk->mk_decrypted_inodes_lock);
431b1c0ec35SEric Biggers 
43223c688b5SEric Biggers 	if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
43323c688b5SEric Biggers 		err = allocate_master_key_users_keyring(mk);
43423c688b5SEric Biggers 		if (err)
435d7e7b9afSEric Biggers 			goto out_put;
43623c688b5SEric Biggers 		err = add_master_key_user(mk);
43723c688b5SEric Biggers 		if (err)
438d7e7b9afSEric Biggers 			goto out_put;
43923c688b5SEric Biggers 	}
44023c688b5SEric Biggers 
441d7e7b9afSEric Biggers 	move_master_key_secret(&mk->mk_secret, secret);
442d7e7b9afSEric Biggers 	refcount_set(&mk->mk_active_refs, 1); /* ->mk_secret is present */
44322d94f49SEric Biggers 
444d7e7b9afSEric Biggers 	spin_lock(&keyring->lock);
445d7e7b9afSEric Biggers 	hlist_add_head_rcu(&mk->mk_node,
446d7e7b9afSEric Biggers 			   fscrypt_mk_hash_bucket(keyring, mk_spec));
447d7e7b9afSEric Biggers 	spin_unlock(&keyring->lock);
44822d94f49SEric Biggers 	return 0;
44922d94f49SEric Biggers 
450d7e7b9afSEric Biggers out_put:
451d7e7b9afSEric Biggers 	fscrypt_put_master_key(mk);
45222d94f49SEric Biggers 	return err;
45322d94f49SEric Biggers }
45422d94f49SEric Biggers 
455b1c0ec35SEric Biggers #define KEY_DEAD	1
456b1c0ec35SEric Biggers 
add_existing_master_key(struct fscrypt_master_key * mk,struct fscrypt_master_key_secret * secret)457b1c0ec35SEric Biggers static int add_existing_master_key(struct fscrypt_master_key *mk,
458b1c0ec35SEric Biggers 				   struct fscrypt_master_key_secret *secret)
459b1c0ec35SEric Biggers {
46023c688b5SEric Biggers 	int err;
461b1c0ec35SEric Biggers 
46223c688b5SEric Biggers 	/*
46323c688b5SEric Biggers 	 * If the current user is already in ->mk_users, then there's nothing to
464d7e7b9afSEric Biggers 	 * do.  Otherwise, we need to add the user to ->mk_users.  (Neither is
465d7e7b9afSEric Biggers 	 * applicable for v1 policy keys, which have NULL ->mk_users.)
46623c688b5SEric Biggers 	 */
46723c688b5SEric Biggers 	if (mk->mk_users) {
468d7e7b9afSEric Biggers 		struct key *mk_user = find_master_key_user(mk);
469d7e7b9afSEric Biggers 
47023c688b5SEric Biggers 		if (mk_user != ERR_PTR(-ENOKEY)) {
47123c688b5SEric Biggers 			if (IS_ERR(mk_user))
47223c688b5SEric Biggers 				return PTR_ERR(mk_user);
47323c688b5SEric Biggers 			key_put(mk_user);
47423c688b5SEric Biggers 			return 0;
47523c688b5SEric Biggers 		}
47623c688b5SEric Biggers 		err = add_master_key_user(mk);
477d7e7b9afSEric Biggers 		if (err)
47823c688b5SEric Biggers 			return err;
47923c688b5SEric Biggers 	}
48023c688b5SEric Biggers 
48123c688b5SEric Biggers 	/* Re-add the secret if needed. */
482d7e7b9afSEric Biggers 	if (!is_master_key_secret_present(&mk->mk_secret)) {
483d7e7b9afSEric Biggers 		if (!refcount_inc_not_zero(&mk->mk_active_refs))
484d7e7b9afSEric Biggers 			return KEY_DEAD;
485b1c0ec35SEric Biggers 		move_master_key_secret(&mk->mk_secret, secret);
486d7e7b9afSEric Biggers 	}
487d7e7b9afSEric Biggers 
488b1c0ec35SEric Biggers 	return 0;
489b1c0ec35SEric Biggers }
490b1c0ec35SEric Biggers 
do_add_master_key(struct super_block * sb,struct fscrypt_master_key_secret * secret,const struct fscrypt_key_specifier * mk_spec)491cdeb21daSEric Biggers static int do_add_master_key(struct super_block *sb,
49222d94f49SEric Biggers 			     struct fscrypt_master_key_secret *secret,
49322d94f49SEric Biggers 			     const struct fscrypt_key_specifier *mk_spec)
49422d94f49SEric Biggers {
49522d94f49SEric Biggers 	static DEFINE_MUTEX(fscrypt_add_key_mutex);
496d7e7b9afSEric Biggers 	struct fscrypt_master_key *mk;
49722d94f49SEric Biggers 	int err;
49822d94f49SEric Biggers 
49922d94f49SEric Biggers 	mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */
500d7e7b9afSEric Biggers 
501d7e7b9afSEric Biggers 	mk = fscrypt_find_master_key(sb, mk_spec);
502d7e7b9afSEric Biggers 	if (!mk) {
50322d94f49SEric Biggers 		/* Didn't find the key in ->s_master_keys.  Add it. */
50422d94f49SEric Biggers 		err = allocate_filesystem_keyring(sb);
505d7e7b9afSEric Biggers 		if (!err)
506d7e7b9afSEric Biggers 			err = add_new_master_key(sb, secret, mk_spec);
50722d94f49SEric Biggers 	} else {
508b1c0ec35SEric Biggers 		/*
509b1c0ec35SEric Biggers 		 * Found the key in ->s_master_keys.  Re-add the secret if
51023c688b5SEric Biggers 		 * needed, and add the user to ->mk_users if needed.
511b1c0ec35SEric Biggers 		 */
512d7e7b9afSEric Biggers 		down_write(&mk->mk_sem);
513d7e7b9afSEric Biggers 		err = add_existing_master_key(mk, secret);
514d7e7b9afSEric Biggers 		up_write(&mk->mk_sem);
515b1c0ec35SEric Biggers 		if (err == KEY_DEAD) {
516d7e7b9afSEric Biggers 			/*
517d7e7b9afSEric Biggers 			 * We found a key struct, but it's already been fully
518d7e7b9afSEric Biggers 			 * removed.  Ignore the old struct and add a new one.
519d7e7b9afSEric Biggers 			 * fscrypt_add_key_mutex means we don't need to worry
520d7e7b9afSEric Biggers 			 * about concurrent adds.
521d7e7b9afSEric Biggers 			 */
522d7e7b9afSEric Biggers 			err = add_new_master_key(sb, secret, mk_spec);
523b1c0ec35SEric Biggers 		}
524d7e7b9afSEric Biggers 		fscrypt_put_master_key(mk);
52522d94f49SEric Biggers 	}
52622d94f49SEric Biggers 	mutex_unlock(&fscrypt_add_key_mutex);
52722d94f49SEric Biggers 	return err;
52822d94f49SEric Biggers }
52922d94f49SEric Biggers 
add_master_key(struct super_block * sb,struct fscrypt_master_key_secret * secret,struct fscrypt_key_specifier * key_spec)530cdeb21daSEric Biggers static int add_master_key(struct super_block *sb,
531cdeb21daSEric Biggers 			  struct fscrypt_master_key_secret *secret,
532cdeb21daSEric Biggers 			  struct fscrypt_key_specifier *key_spec)
533cdeb21daSEric Biggers {
534cdeb21daSEric Biggers 	int err;
535cdeb21daSEric Biggers 
536cdeb21daSEric Biggers 	if (key_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
537cdeb21daSEric Biggers 		err = fscrypt_init_hkdf(&secret->hkdf, secret->raw,
538cdeb21daSEric Biggers 					secret->size);
539cdeb21daSEric Biggers 		if (err)
540cdeb21daSEric Biggers 			return err;
541cdeb21daSEric Biggers 
542cdeb21daSEric Biggers 		/*
543cdeb21daSEric Biggers 		 * Now that the HKDF context is initialized, the raw key is no
544cdeb21daSEric Biggers 		 * longer needed.
545cdeb21daSEric Biggers 		 */
546cdeb21daSEric Biggers 		memzero_explicit(secret->raw, secret->size);
547cdeb21daSEric Biggers 
548cdeb21daSEric Biggers 		/* Calculate the key identifier */
549cdeb21daSEric Biggers 		err = fscrypt_hkdf_expand(&secret->hkdf,
550cdeb21daSEric Biggers 					  HKDF_CONTEXT_KEY_IDENTIFIER, NULL, 0,
551cdeb21daSEric Biggers 					  key_spec->u.identifier,
552cdeb21daSEric Biggers 					  FSCRYPT_KEY_IDENTIFIER_SIZE);
553cdeb21daSEric Biggers 		if (err)
554cdeb21daSEric Biggers 			return err;
555cdeb21daSEric Biggers 	}
556cdeb21daSEric Biggers 	return do_add_master_key(sb, secret, key_spec);
557cdeb21daSEric Biggers }
558cdeb21daSEric Biggers 
fscrypt_provisioning_key_preparse(struct key_preparsed_payload * prep)55993edd392SEric Biggers static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload *prep)
56093edd392SEric Biggers {
56193edd392SEric Biggers 	const struct fscrypt_provisioning_key_payload *payload = prep->data;
56293edd392SEric Biggers 
56393edd392SEric Biggers 	if (prep->datalen < sizeof(*payload) + FSCRYPT_MIN_KEY_SIZE ||
56493edd392SEric Biggers 	    prep->datalen > sizeof(*payload) + FSCRYPT_MAX_KEY_SIZE)
56593edd392SEric Biggers 		return -EINVAL;
56693edd392SEric Biggers 
56793edd392SEric Biggers 	if (payload->type != FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
56893edd392SEric Biggers 	    payload->type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER)
56993edd392SEric Biggers 		return -EINVAL;
57093edd392SEric Biggers 
57193edd392SEric Biggers 	if (payload->__reserved)
57293edd392SEric Biggers 		return -EINVAL;
57393edd392SEric Biggers 
57493edd392SEric Biggers 	prep->payload.data[0] = kmemdup(payload, prep->datalen, GFP_KERNEL);
57593edd392SEric Biggers 	if (!prep->payload.data[0])
57693edd392SEric Biggers 		return -ENOMEM;
57793edd392SEric Biggers 
57893edd392SEric Biggers 	prep->quotalen = prep->datalen;
57993edd392SEric Biggers 	return 0;
58093edd392SEric Biggers }
58193edd392SEric Biggers 
fscrypt_provisioning_key_free_preparse(struct key_preparsed_payload * prep)58293edd392SEric Biggers static void fscrypt_provisioning_key_free_preparse(
58393edd392SEric Biggers 					struct key_preparsed_payload *prep)
58493edd392SEric Biggers {
585453431a5SWaiman Long 	kfree_sensitive(prep->payload.data[0]);
58693edd392SEric Biggers }
58793edd392SEric Biggers 
fscrypt_provisioning_key_describe(const struct key * key,struct seq_file * m)58893edd392SEric Biggers static void fscrypt_provisioning_key_describe(const struct key *key,
58993edd392SEric Biggers 					      struct seq_file *m)
59093edd392SEric Biggers {
59193edd392SEric Biggers 	seq_puts(m, key->description);
59293edd392SEric Biggers 	if (key_is_positive(key)) {
59393edd392SEric Biggers 		const struct fscrypt_provisioning_key_payload *payload =
59493edd392SEric Biggers 			key->payload.data[0];
59593edd392SEric Biggers 
59693edd392SEric Biggers 		seq_printf(m, ": %u [%u]", key->datalen, payload->type);
59793edd392SEric Biggers 	}
59893edd392SEric Biggers }
59993edd392SEric Biggers 
fscrypt_provisioning_key_destroy(struct key * key)60093edd392SEric Biggers static void fscrypt_provisioning_key_destroy(struct key *key)
60193edd392SEric Biggers {
602453431a5SWaiman Long 	kfree_sensitive(key->payload.data[0]);
60393edd392SEric Biggers }
60493edd392SEric Biggers 
60593edd392SEric Biggers static struct key_type key_type_fscrypt_provisioning = {
60693edd392SEric Biggers 	.name			= "fscrypt-provisioning",
60793edd392SEric Biggers 	.preparse		= fscrypt_provisioning_key_preparse,
60893edd392SEric Biggers 	.free_preparse		= fscrypt_provisioning_key_free_preparse,
60993edd392SEric Biggers 	.instantiate		= generic_key_instantiate,
61093edd392SEric Biggers 	.describe		= fscrypt_provisioning_key_describe,
61193edd392SEric Biggers 	.destroy		= fscrypt_provisioning_key_destroy,
61293edd392SEric Biggers };
61393edd392SEric Biggers 
61493edd392SEric Biggers /*
61593edd392SEric Biggers  * Retrieve the raw key from the Linux keyring key specified by 'key_id', and
61693edd392SEric Biggers  * store it into 'secret'.
61793edd392SEric Biggers  *
61893edd392SEric Biggers  * The key must be of type "fscrypt-provisioning" and must have the field
61993edd392SEric Biggers  * fscrypt_provisioning_key_payload::type set to 'type', indicating that it's
62093edd392SEric Biggers  * only usable with fscrypt with the particular KDF version identified by
62193edd392SEric Biggers  * 'type'.  We don't use the "logon" key type because there's no way to
62293edd392SEric Biggers  * completely restrict the use of such keys; they can be used by any kernel API
62393edd392SEric Biggers  * that accepts "logon" keys and doesn't require a specific service prefix.
62493edd392SEric Biggers  *
62593edd392SEric Biggers  * The ability to specify the key via Linux keyring key is intended for cases
62693edd392SEric Biggers  * where userspace needs to re-add keys after the filesystem is unmounted and
62793edd392SEric Biggers  * re-mounted.  Most users should just provide the raw key directly instead.
62893edd392SEric Biggers  */
get_keyring_key(u32 key_id,u32 type,struct fscrypt_master_key_secret * secret)62993edd392SEric Biggers static int get_keyring_key(u32 key_id, u32 type,
63093edd392SEric Biggers 			   struct fscrypt_master_key_secret *secret)
63193edd392SEric Biggers {
63293edd392SEric Biggers 	key_ref_t ref;
63393edd392SEric Biggers 	struct key *key;
63493edd392SEric Biggers 	const struct fscrypt_provisioning_key_payload *payload;
63593edd392SEric Biggers 	int err;
63693edd392SEric Biggers 
63793edd392SEric Biggers 	ref = lookup_user_key(key_id, 0, KEY_NEED_SEARCH);
63893edd392SEric Biggers 	if (IS_ERR(ref))
63993edd392SEric Biggers 		return PTR_ERR(ref);
64093edd392SEric Biggers 	key = key_ref_to_ptr(ref);
64193edd392SEric Biggers 
64293edd392SEric Biggers 	if (key->type != &key_type_fscrypt_provisioning)
64393edd392SEric Biggers 		goto bad_key;
64493edd392SEric Biggers 	payload = key->payload.data[0];
64593edd392SEric Biggers 
64693edd392SEric Biggers 	/* Don't allow fscrypt v1 keys to be used as v2 keys and vice versa. */
64793edd392SEric Biggers 	if (payload->type != type)
64893edd392SEric Biggers 		goto bad_key;
64993edd392SEric Biggers 
65093edd392SEric Biggers 	secret->size = key->datalen - sizeof(*payload);
65193edd392SEric Biggers 	memcpy(secret->raw, payload->raw, secret->size);
65293edd392SEric Biggers 	err = 0;
65393edd392SEric Biggers 	goto out_put;
65493edd392SEric Biggers 
65593edd392SEric Biggers bad_key:
65693edd392SEric Biggers 	err = -EKEYREJECTED;
65793edd392SEric Biggers out_put:
65893edd392SEric Biggers 	key_ref_put(ref);
65993edd392SEric Biggers 	return err;
66093edd392SEric Biggers }
66193edd392SEric Biggers 
66222d94f49SEric Biggers /*
66322d94f49SEric Biggers  * Add a master encryption key to the filesystem, causing all files which were
66422d94f49SEric Biggers  * encrypted with it to appear "unlocked" (decrypted) when accessed.
66522d94f49SEric Biggers  *
66623c688b5SEric Biggers  * When adding a key for use by v1 encryption policies, this ioctl is
66723c688b5SEric Biggers  * privileged, and userspace must provide the 'key_descriptor'.
66823c688b5SEric Biggers  *
66923c688b5SEric Biggers  * When adding a key for use by v2+ encryption policies, this ioctl is
67023c688b5SEric Biggers  * unprivileged.  This is needed, in general, to allow non-root users to use
67123c688b5SEric Biggers  * encryption without encountering the visibility problems of process-subscribed
67223c688b5SEric Biggers  * keyrings and the inability to properly remove keys.  This works by having
67323c688b5SEric Biggers  * each key identified by its cryptographically secure hash --- the
67423c688b5SEric Biggers  * 'key_identifier'.  The cryptographic hash ensures that a malicious user
67523c688b5SEric Biggers  * cannot add the wrong key for a given identifier.  Furthermore, each added key
67623c688b5SEric Biggers  * is charged to the appropriate user's quota for the keyrings service, which
67723c688b5SEric Biggers  * prevents a malicious user from adding too many keys.  Finally, we forbid a
67823c688b5SEric Biggers  * user from removing a key while other users have added it too, which prevents
67923c688b5SEric Biggers  * a user who knows another user's key from causing a denial-of-service by
68023c688b5SEric Biggers  * removing it at an inopportune time.  (We tolerate that a user who knows a key
68123c688b5SEric Biggers  * can prevent other users from removing it.)
68223c688b5SEric Biggers  *
68322d94f49SEric Biggers  * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of
68422d94f49SEric Biggers  * Documentation/filesystems/fscrypt.rst.
68522d94f49SEric Biggers  */
fscrypt_ioctl_add_key(struct file * filp,void __user * _uarg)68622d94f49SEric Biggers int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
68722d94f49SEric Biggers {
68822d94f49SEric Biggers 	struct super_block *sb = file_inode(filp)->i_sb;
68922d94f49SEric Biggers 	struct fscrypt_add_key_arg __user *uarg = _uarg;
69022d94f49SEric Biggers 	struct fscrypt_add_key_arg arg;
69122d94f49SEric Biggers 	struct fscrypt_master_key_secret secret;
69222d94f49SEric Biggers 	int err;
69322d94f49SEric Biggers 
69422d94f49SEric Biggers 	if (copy_from_user(&arg, uarg, sizeof(arg)))
69522d94f49SEric Biggers 		return -EFAULT;
69622d94f49SEric Biggers 
69722d94f49SEric Biggers 	if (!valid_key_spec(&arg.key_spec))
69822d94f49SEric Biggers 		return -EINVAL;
69922d94f49SEric Biggers 
70022d94f49SEric Biggers 	if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
70122d94f49SEric Biggers 		return -EINVAL;
70222d94f49SEric Biggers 
703cdeb21daSEric Biggers 	/*
704cdeb21daSEric Biggers 	 * Only root can add keys that are identified by an arbitrary descriptor
705cdeb21daSEric Biggers 	 * rather than by a cryptographic hash --- since otherwise a malicious
706cdeb21daSEric Biggers 	 * user could add the wrong key.
707cdeb21daSEric Biggers 	 */
708cdeb21daSEric Biggers 	if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
709cdeb21daSEric Biggers 	    !capable(CAP_SYS_ADMIN))
710cdeb21daSEric Biggers 		return -EACCES;
711cdeb21daSEric Biggers 
71222d94f49SEric Biggers 	memset(&secret, 0, sizeof(secret));
71393edd392SEric Biggers 	if (arg.key_id) {
71493edd392SEric Biggers 		if (arg.raw_size != 0)
71593edd392SEric Biggers 			return -EINVAL;
71693edd392SEric Biggers 		err = get_keyring_key(arg.key_id, arg.key_spec.type, &secret);
71793edd392SEric Biggers 		if (err)
71893edd392SEric Biggers 			goto out_wipe_secret;
71993edd392SEric Biggers 	} else {
72093edd392SEric Biggers 		if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE ||
72193edd392SEric Biggers 		    arg.raw_size > FSCRYPT_MAX_KEY_SIZE)
72293edd392SEric Biggers 			return -EINVAL;
72322d94f49SEric Biggers 		secret.size = arg.raw_size;
72422d94f49SEric Biggers 		err = -EFAULT;
72522d94f49SEric Biggers 		if (copy_from_user(secret.raw, uarg->raw, secret.size))
72622d94f49SEric Biggers 			goto out_wipe_secret;
72793edd392SEric Biggers 	}
72822d94f49SEric Biggers 
729cdeb21daSEric Biggers 	err = add_master_key(sb, &secret, &arg.key_spec);
7305dae460cSEric Biggers 	if (err)
7315dae460cSEric Biggers 		goto out_wipe_secret;
7325dae460cSEric Biggers 
733cdeb21daSEric Biggers 	/* Return the key identifier to userspace, if applicable */
7345dae460cSEric Biggers 	err = -EFAULT;
735cdeb21daSEric Biggers 	if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
736cdeb21daSEric Biggers 	    copy_to_user(uarg->key_spec.u.identifier, arg.key_spec.u.identifier,
7375dae460cSEric Biggers 			 FSCRYPT_KEY_IDENTIFIER_SIZE))
7385dae460cSEric Biggers 		goto out_wipe_secret;
739cdeb21daSEric Biggers 	err = 0;
74022d94f49SEric Biggers out_wipe_secret:
74122d94f49SEric Biggers 	wipe_master_key_secret(&secret);
74222d94f49SEric Biggers 	return err;
74322d94f49SEric Biggers }
74422d94f49SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
74522d94f49SEric Biggers 
746218d921bSEric Biggers static void
fscrypt_get_test_dummy_secret(struct fscrypt_master_key_secret * secret)747218d921bSEric Biggers fscrypt_get_test_dummy_secret(struct fscrypt_master_key_secret *secret)
748cdeb21daSEric Biggers {
749cdeb21daSEric Biggers 	static u8 test_key[FSCRYPT_MAX_KEY_SIZE];
750cdeb21daSEric Biggers 
751cdeb21daSEric Biggers 	get_random_once(test_key, FSCRYPT_MAX_KEY_SIZE);
752cdeb21daSEric Biggers 
753218d921bSEric Biggers 	memset(secret, 0, sizeof(*secret));
754218d921bSEric Biggers 	secret->size = FSCRYPT_MAX_KEY_SIZE;
755218d921bSEric Biggers 	memcpy(secret->raw, test_key, FSCRYPT_MAX_KEY_SIZE);
756218d921bSEric Biggers }
757cdeb21daSEric Biggers 
fscrypt_get_test_dummy_key_identifier(u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])758218d921bSEric Biggers int fscrypt_get_test_dummy_key_identifier(
759218d921bSEric Biggers 				u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
760218d921bSEric Biggers {
761218d921bSEric Biggers 	struct fscrypt_master_key_secret secret;
762218d921bSEric Biggers 	int err;
763218d921bSEric Biggers 
764218d921bSEric Biggers 	fscrypt_get_test_dummy_secret(&secret);
765218d921bSEric Biggers 
766218d921bSEric Biggers 	err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size);
767218d921bSEric Biggers 	if (err)
768218d921bSEric Biggers 		goto out;
769218d921bSEric Biggers 	err = fscrypt_hkdf_expand(&secret.hkdf, HKDF_CONTEXT_KEY_IDENTIFIER,
770218d921bSEric Biggers 				  NULL, 0, key_identifier,
771218d921bSEric Biggers 				  FSCRYPT_KEY_IDENTIFIER_SIZE);
772218d921bSEric Biggers out:
773cdeb21daSEric Biggers 	wipe_master_key_secret(&secret);
774cdeb21daSEric Biggers 	return err;
775cdeb21daSEric Biggers }
776cdeb21daSEric Biggers 
777218d921bSEric Biggers /**
778218d921bSEric Biggers  * fscrypt_add_test_dummy_key() - add the test dummy encryption key
779218d921bSEric Biggers  * @sb: the filesystem instance to add the key to
780097d7c1fSEric Biggers  * @key_spec: the key specifier of the test dummy encryption key
781218d921bSEric Biggers  *
782097d7c1fSEric Biggers  * Add the key for the test_dummy_encryption mount option to the filesystem.  To
783097d7c1fSEric Biggers  * prevent misuse of this mount option, a per-boot random key is used instead of
784097d7c1fSEric Biggers  * a hardcoded one.  This makes it so that any encrypted files created using
785097d7c1fSEric Biggers  * this option won't be accessible after a reboot.
786218d921bSEric Biggers  *
787218d921bSEric Biggers  * Return: 0 on success, -errno on failure
788218d921bSEric Biggers  */
fscrypt_add_test_dummy_key(struct super_block * sb,struct fscrypt_key_specifier * key_spec)789218d921bSEric Biggers int fscrypt_add_test_dummy_key(struct super_block *sb,
790097d7c1fSEric Biggers 			       struct fscrypt_key_specifier *key_spec)
791218d921bSEric Biggers {
792218d921bSEric Biggers 	struct fscrypt_master_key_secret secret;
793218d921bSEric Biggers 	int err;
794218d921bSEric Biggers 
795218d921bSEric Biggers 	fscrypt_get_test_dummy_secret(&secret);
796097d7c1fSEric Biggers 	err = add_master_key(sb, &secret, key_spec);
797218d921bSEric Biggers 	wipe_master_key_secret(&secret);
798218d921bSEric Biggers 	return err;
799218d921bSEric Biggers }
800218d921bSEric Biggers 
801cdeb21daSEric Biggers /*
8025ab7189aSEric Biggers  * Verify that the current user has added a master key with the given identifier
8035ab7189aSEric Biggers  * (returns -ENOKEY if not).  This is needed to prevent a user from encrypting
8045ab7189aSEric Biggers  * their files using some other user's key which they don't actually know.
8055ab7189aSEric Biggers  * Cryptographically this isn't much of a problem, but the semantics of this
8065ab7189aSEric Biggers  * would be a bit weird, so it's best to just forbid it.
8075ab7189aSEric Biggers  *
8085ab7189aSEric Biggers  * The system administrator (CAP_FOWNER) can override this, which should be
8095ab7189aSEric Biggers  * enough for any use cases where encryption policies are being set using keys
8105ab7189aSEric Biggers  * that were chosen ahead of time but aren't available at the moment.
8115ab7189aSEric Biggers  *
8125ab7189aSEric Biggers  * Note that the key may have already removed by the time this returns, but
8135ab7189aSEric Biggers  * that's okay; we just care whether the key was there at some point.
8145ab7189aSEric Biggers  *
8155ab7189aSEric Biggers  * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code
8165ab7189aSEric Biggers  */
fscrypt_verify_key_added(struct super_block * sb,const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])8175ab7189aSEric Biggers int fscrypt_verify_key_added(struct super_block *sb,
8185ab7189aSEric Biggers 			     const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
8195ab7189aSEric Biggers {
8205ab7189aSEric Biggers 	struct fscrypt_key_specifier mk_spec;
8215ab7189aSEric Biggers 	struct fscrypt_master_key *mk;
822d7e7b9afSEric Biggers 	struct key *mk_user;
8235ab7189aSEric Biggers 	int err;
8245ab7189aSEric Biggers 
8255ab7189aSEric Biggers 	mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
8265ab7189aSEric Biggers 	memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
8275ab7189aSEric Biggers 
828d7e7b9afSEric Biggers 	mk = fscrypt_find_master_key(sb, &mk_spec);
829d7e7b9afSEric Biggers 	if (!mk) {
830d7e7b9afSEric Biggers 		err = -ENOKEY;
8315ab7189aSEric Biggers 		goto out;
8325ab7189aSEric Biggers 	}
833d7e7b9afSEric Biggers 	down_read(&mk->mk_sem);
8345ab7189aSEric Biggers 	mk_user = find_master_key_user(mk);
8355ab7189aSEric Biggers 	if (IS_ERR(mk_user)) {
8365ab7189aSEric Biggers 		err = PTR_ERR(mk_user);
8375ab7189aSEric Biggers 	} else {
8385ab7189aSEric Biggers 		key_put(mk_user);
8395ab7189aSEric Biggers 		err = 0;
8405ab7189aSEric Biggers 	}
841d7e7b9afSEric Biggers 	up_read(&mk->mk_sem);
842d7e7b9afSEric Biggers 	fscrypt_put_master_key(mk);
8435ab7189aSEric Biggers out:
8445ab7189aSEric Biggers 	if (err == -ENOKEY && capable(CAP_FOWNER))
8455ab7189aSEric Biggers 		err = 0;
8465ab7189aSEric Biggers 	return err;
8475ab7189aSEric Biggers }
8485ab7189aSEric Biggers 
8495ab7189aSEric Biggers /*
850b1c0ec35SEric Biggers  * Try to evict the inode's dentries from the dentry cache.  If the inode is a
851b1c0ec35SEric Biggers  * directory, then it can have at most one dentry; however, that dentry may be
852b1c0ec35SEric Biggers  * pinned by child dentries, so first try to evict the children too.
853b1c0ec35SEric Biggers  */
shrink_dcache_inode(struct inode * inode)854b1c0ec35SEric Biggers static void shrink_dcache_inode(struct inode *inode)
855b1c0ec35SEric Biggers {
856b1c0ec35SEric Biggers 	struct dentry *dentry;
857b1c0ec35SEric Biggers 
858b1c0ec35SEric Biggers 	if (S_ISDIR(inode->i_mode)) {
859b1c0ec35SEric Biggers 		dentry = d_find_any_alias(inode);
860b1c0ec35SEric Biggers 		if (dentry) {
861b1c0ec35SEric Biggers 			shrink_dcache_parent(dentry);
862b1c0ec35SEric Biggers 			dput(dentry);
863b1c0ec35SEric Biggers 		}
864b1c0ec35SEric Biggers 	}
865b1c0ec35SEric Biggers 	d_prune_aliases(inode);
866b1c0ec35SEric Biggers }
867b1c0ec35SEric Biggers 
evict_dentries_for_decrypted_inodes(struct fscrypt_master_key * mk)868b1c0ec35SEric Biggers static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk)
869b1c0ec35SEric Biggers {
870b1c0ec35SEric Biggers 	struct fscrypt_info *ci;
871b1c0ec35SEric Biggers 	struct inode *inode;
872b1c0ec35SEric Biggers 	struct inode *toput_inode = NULL;
873b1c0ec35SEric Biggers 
874b1c0ec35SEric Biggers 	spin_lock(&mk->mk_decrypted_inodes_lock);
875b1c0ec35SEric Biggers 
876b1c0ec35SEric Biggers 	list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
877b1c0ec35SEric Biggers 		inode = ci->ci_inode;
878b1c0ec35SEric Biggers 		spin_lock(&inode->i_lock);
879b1c0ec35SEric Biggers 		if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
880b1c0ec35SEric Biggers 			spin_unlock(&inode->i_lock);
881b1c0ec35SEric Biggers 			continue;
882b1c0ec35SEric Biggers 		}
883b1c0ec35SEric Biggers 		__iget(inode);
884b1c0ec35SEric Biggers 		spin_unlock(&inode->i_lock);
885b1c0ec35SEric Biggers 		spin_unlock(&mk->mk_decrypted_inodes_lock);
886b1c0ec35SEric Biggers 
887b1c0ec35SEric Biggers 		shrink_dcache_inode(inode);
888b1c0ec35SEric Biggers 		iput(toput_inode);
889b1c0ec35SEric Biggers 		toput_inode = inode;
890b1c0ec35SEric Biggers 
891b1c0ec35SEric Biggers 		spin_lock(&mk->mk_decrypted_inodes_lock);
892b1c0ec35SEric Biggers 	}
893b1c0ec35SEric Biggers 
894b1c0ec35SEric Biggers 	spin_unlock(&mk->mk_decrypted_inodes_lock);
895b1c0ec35SEric Biggers 	iput(toput_inode);
896b1c0ec35SEric Biggers }
897b1c0ec35SEric Biggers 
check_for_busy_inodes(struct super_block * sb,struct fscrypt_master_key * mk)898b1c0ec35SEric Biggers static int check_for_busy_inodes(struct super_block *sb,
899b1c0ec35SEric Biggers 				 struct fscrypt_master_key *mk)
900b1c0ec35SEric Biggers {
901b1c0ec35SEric Biggers 	struct list_head *pos;
902b1c0ec35SEric Biggers 	size_t busy_count = 0;
903b1c0ec35SEric Biggers 	unsigned long ino;
904ae9ff8adSEric Biggers 	char ino_str[50] = "";
905b1c0ec35SEric Biggers 
906b1c0ec35SEric Biggers 	spin_lock(&mk->mk_decrypted_inodes_lock);
907b1c0ec35SEric Biggers 
908b1c0ec35SEric Biggers 	list_for_each(pos, &mk->mk_decrypted_inodes)
909b1c0ec35SEric Biggers 		busy_count++;
910b1c0ec35SEric Biggers 
911b1c0ec35SEric Biggers 	if (busy_count == 0) {
912b1c0ec35SEric Biggers 		spin_unlock(&mk->mk_decrypted_inodes_lock);
913b1c0ec35SEric Biggers 		return 0;
914b1c0ec35SEric Biggers 	}
915b1c0ec35SEric Biggers 
916b1c0ec35SEric Biggers 	{
917b1c0ec35SEric Biggers 		/* select an example file to show for debugging purposes */
918b1c0ec35SEric Biggers 		struct inode *inode =
919b1c0ec35SEric Biggers 			list_first_entry(&mk->mk_decrypted_inodes,
920b1c0ec35SEric Biggers 					 struct fscrypt_info,
921b1c0ec35SEric Biggers 					 ci_master_key_link)->ci_inode;
922b1c0ec35SEric Biggers 		ino = inode->i_ino;
923b1c0ec35SEric Biggers 	}
924b1c0ec35SEric Biggers 	spin_unlock(&mk->mk_decrypted_inodes_lock);
925b1c0ec35SEric Biggers 
926ae9ff8adSEric Biggers 	/* If the inode is currently being created, ino may still be 0. */
927ae9ff8adSEric Biggers 	if (ino)
928ae9ff8adSEric Biggers 		snprintf(ino_str, sizeof(ino_str), ", including ino %lu", ino);
929ae9ff8adSEric Biggers 
930b1c0ec35SEric Biggers 	fscrypt_warn(NULL,
931ae9ff8adSEric Biggers 		     "%s: %zu inode(s) still busy after removing key with %s %*phN%s",
932b1c0ec35SEric Biggers 		     sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
933b1c0ec35SEric Biggers 		     master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
934ae9ff8adSEric Biggers 		     ino_str);
935b1c0ec35SEric Biggers 	return -EBUSY;
936b1c0ec35SEric Biggers }
937b1c0ec35SEric Biggers 
try_to_lock_encrypted_files(struct super_block * sb,struct fscrypt_master_key * mk)938b1c0ec35SEric Biggers static int try_to_lock_encrypted_files(struct super_block *sb,
939b1c0ec35SEric Biggers 				       struct fscrypt_master_key *mk)
940b1c0ec35SEric Biggers {
941b1c0ec35SEric Biggers 	int err1;
942b1c0ec35SEric Biggers 	int err2;
943b1c0ec35SEric Biggers 
944b1c0ec35SEric Biggers 	/*
945b1c0ec35SEric Biggers 	 * An inode can't be evicted while it is dirty or has dirty pages.
946b1c0ec35SEric Biggers 	 * Thus, we first have to clean the inodes in ->mk_decrypted_inodes.
947b1c0ec35SEric Biggers 	 *
948b1c0ec35SEric Biggers 	 * Just do it the easy way: call sync_filesystem().  It's overkill, but
949b1c0ec35SEric Biggers 	 * it works, and it's more important to minimize the amount of caches we
950b1c0ec35SEric Biggers 	 * drop than the amount of data we sync.  Also, unprivileged users can
951b1c0ec35SEric Biggers 	 * already call sync_filesystem() via sys_syncfs() or sys_sync().
952b1c0ec35SEric Biggers 	 */
953b1c0ec35SEric Biggers 	down_read(&sb->s_umount);
954b1c0ec35SEric Biggers 	err1 = sync_filesystem(sb);
955b1c0ec35SEric Biggers 	up_read(&sb->s_umount);
956b1c0ec35SEric Biggers 	/* If a sync error occurs, still try to evict as much as possible. */
957b1c0ec35SEric Biggers 
958b1c0ec35SEric Biggers 	/*
959b1c0ec35SEric Biggers 	 * Inodes are pinned by their dentries, so we have to evict their
960b1c0ec35SEric Biggers 	 * dentries.  shrink_dcache_sb() would suffice, but would be overkill
961b1c0ec35SEric Biggers 	 * and inappropriate for use by unprivileged users.  So instead go
962b1c0ec35SEric Biggers 	 * through the inodes' alias lists and try to evict each dentry.
963b1c0ec35SEric Biggers 	 */
964b1c0ec35SEric Biggers 	evict_dentries_for_decrypted_inodes(mk);
965b1c0ec35SEric Biggers 
966b1c0ec35SEric Biggers 	/*
967b1c0ec35SEric Biggers 	 * evict_dentries_for_decrypted_inodes() already iput() each inode in
968b1c0ec35SEric Biggers 	 * the list; any inodes for which that dropped the last reference will
969b1c0ec35SEric Biggers 	 * have been evicted due to fscrypt_drop_inode() detecting the key
970b1c0ec35SEric Biggers 	 * removal and telling the VFS to evict the inode.  So to finish, we
971b1c0ec35SEric Biggers 	 * just need to check whether any inodes couldn't be evicted.
972b1c0ec35SEric Biggers 	 */
973b1c0ec35SEric Biggers 	err2 = check_for_busy_inodes(sb, mk);
974b1c0ec35SEric Biggers 
975b1c0ec35SEric Biggers 	return err1 ?: err2;
976b1c0ec35SEric Biggers }
977b1c0ec35SEric Biggers 
978b1c0ec35SEric Biggers /*
979b1c0ec35SEric Biggers  * Try to remove an fscrypt master encryption key.
980b1c0ec35SEric Biggers  *
98178a1b96bSEric Biggers  * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's
98278a1b96bSEric Biggers  * claim to the key, then removes the key itself if no other users have claims.
98378a1b96bSEric Biggers  * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the
98478a1b96bSEric Biggers  * key itself.
98523c688b5SEric Biggers  *
98623c688b5SEric Biggers  * To "remove the key itself", first we wipe the actual master key secret, so
98723c688b5SEric Biggers  * that no more inodes can be unlocked with it.  Then we try to evict all cached
98823c688b5SEric Biggers  * inodes that had been unlocked with the key.
989b1c0ec35SEric Biggers  *
990b1c0ec35SEric Biggers  * If all inodes were evicted, then we unlink the fscrypt_master_key from the
991b1c0ec35SEric Biggers  * keyring.  Otherwise it remains in the keyring in the "incompletely removed"
992b1c0ec35SEric Biggers  * state (without the actual secret key) where it tracks the list of remaining
993b1c0ec35SEric Biggers  * inodes.  Userspace can execute the ioctl again later to retry eviction, or
994b1c0ec35SEric Biggers  * alternatively can re-add the secret key again.
995b1c0ec35SEric Biggers  *
996b1c0ec35SEric Biggers  * For more details, see the "Removing keys" section of
997b1c0ec35SEric Biggers  * Documentation/filesystems/fscrypt.rst.
998b1c0ec35SEric Biggers  */
do_remove_key(struct file * filp,void __user * _uarg,bool all_users)99978a1b96bSEric Biggers static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
1000b1c0ec35SEric Biggers {
1001b1c0ec35SEric Biggers 	struct super_block *sb = file_inode(filp)->i_sb;
1002b1c0ec35SEric Biggers 	struct fscrypt_remove_key_arg __user *uarg = _uarg;
1003b1c0ec35SEric Biggers 	struct fscrypt_remove_key_arg arg;
1004b1c0ec35SEric Biggers 	struct fscrypt_master_key *mk;
1005b1c0ec35SEric Biggers 	u32 status_flags = 0;
1006b1c0ec35SEric Biggers 	int err;
1007d7e7b9afSEric Biggers 	bool inodes_remain;
1008b1c0ec35SEric Biggers 
1009b1c0ec35SEric Biggers 	if (copy_from_user(&arg, uarg, sizeof(arg)))
1010b1c0ec35SEric Biggers 		return -EFAULT;
1011b1c0ec35SEric Biggers 
1012b1c0ec35SEric Biggers 	if (!valid_key_spec(&arg.key_spec))
1013b1c0ec35SEric Biggers 		return -EINVAL;
1014b1c0ec35SEric Biggers 
1015b1c0ec35SEric Biggers 	if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
1016b1c0ec35SEric Biggers 		return -EINVAL;
1017b1c0ec35SEric Biggers 
101823c688b5SEric Biggers 	/*
101923c688b5SEric Biggers 	 * Only root can add and remove keys that are identified by an arbitrary
102023c688b5SEric Biggers 	 * descriptor rather than by a cryptographic hash.
102123c688b5SEric Biggers 	 */
102223c688b5SEric Biggers 	if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
102323c688b5SEric Biggers 	    !capable(CAP_SYS_ADMIN))
1024b1c0ec35SEric Biggers 		return -EACCES;
1025b1c0ec35SEric Biggers 
1026b1c0ec35SEric Biggers 	/* Find the key being removed. */
1027d7e7b9afSEric Biggers 	mk = fscrypt_find_master_key(sb, &arg.key_spec);
1028d7e7b9afSEric Biggers 	if (!mk)
1029d7e7b9afSEric Biggers 		return -ENOKEY;
1030d7e7b9afSEric Biggers 	down_write(&mk->mk_sem);
1031b1c0ec35SEric Biggers 
103278a1b96bSEric Biggers 	/* If relevant, remove current user's (or all users) claim to the key */
103323c688b5SEric Biggers 	if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
103478a1b96bSEric Biggers 		if (all_users)
103578a1b96bSEric Biggers 			err = keyring_clear(mk->mk_users);
103678a1b96bSEric Biggers 		else
103723c688b5SEric Biggers 			err = remove_master_key_user(mk);
103823c688b5SEric Biggers 		if (err) {
1039d7e7b9afSEric Biggers 			up_write(&mk->mk_sem);
104023c688b5SEric Biggers 			goto out_put_key;
104123c688b5SEric Biggers 		}
104223c688b5SEric Biggers 		if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
104323c688b5SEric Biggers 			/*
104423c688b5SEric Biggers 			 * Other users have still added the key too.  We removed
104523c688b5SEric Biggers 			 * the current user's claim to the key, but we still
104623c688b5SEric Biggers 			 * can't remove the key itself.
104723c688b5SEric Biggers 			 */
104823c688b5SEric Biggers 			status_flags |=
104923c688b5SEric Biggers 				FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
105023c688b5SEric Biggers 			err = 0;
1051d7e7b9afSEric Biggers 			up_write(&mk->mk_sem);
105223c688b5SEric Biggers 			goto out_put_key;
105323c688b5SEric Biggers 		}
105423c688b5SEric Biggers 	}
105523c688b5SEric Biggers 
105623c688b5SEric Biggers 	/* No user claims remaining.  Go ahead and wipe the secret. */
1057d7e7b9afSEric Biggers 	err = -ENOKEY;
1058b1c0ec35SEric Biggers 	if (is_master_key_secret_present(&mk->mk_secret)) {
1059b1c0ec35SEric Biggers 		wipe_master_key_secret(&mk->mk_secret);
106002aef422SEric Biggers 		fscrypt_put_master_key_activeref(sb, mk);
1061b1c0ec35SEric Biggers 		err = 0;
1062d7e7b9afSEric Biggers 	}
1063d7e7b9afSEric Biggers 	inodes_remain = refcount_read(&mk->mk_active_refs) > 0;
1064d7e7b9afSEric Biggers 	up_write(&mk->mk_sem);
1065d7e7b9afSEric Biggers 
1066d7e7b9afSEric Biggers 	if (inodes_remain) {
1067b1c0ec35SEric Biggers 		/* Some inodes still reference this key; try to evict them. */
1068b1c0ec35SEric Biggers 		err = try_to_lock_encrypted_files(sb, mk);
1069b1c0ec35SEric Biggers 		if (err == -EBUSY) {
1070b1c0ec35SEric Biggers 			status_flags |=
1071b1c0ec35SEric Biggers 				FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
1072b1c0ec35SEric Biggers 			err = 0;
1073b1c0ec35SEric Biggers 		}
1074b1c0ec35SEric Biggers 	}
1075b1c0ec35SEric Biggers 	/*
107623c688b5SEric Biggers 	 * We return 0 if we successfully did something: removed a claim to the
107723c688b5SEric Biggers 	 * key, wiped the secret, or tried locking the files again.  Users need
107823c688b5SEric Biggers 	 * to check the informational status flags if they care whether the key
107923c688b5SEric Biggers 	 * has been fully removed including all files locked.
1080b1c0ec35SEric Biggers 	 */
108123c688b5SEric Biggers out_put_key:
1082d7e7b9afSEric Biggers 	fscrypt_put_master_key(mk);
1083b1c0ec35SEric Biggers 	if (err == 0)
1084b1c0ec35SEric Biggers 		err = put_user(status_flags, &uarg->removal_status_flags);
1085b1c0ec35SEric Biggers 	return err;
1086b1c0ec35SEric Biggers }
108778a1b96bSEric Biggers 
fscrypt_ioctl_remove_key(struct file * filp,void __user * uarg)108878a1b96bSEric Biggers int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
108978a1b96bSEric Biggers {
109078a1b96bSEric Biggers 	return do_remove_key(filp, uarg, false);
109178a1b96bSEric Biggers }
1092b1c0ec35SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key);
1093b1c0ec35SEric Biggers 
fscrypt_ioctl_remove_key_all_users(struct file * filp,void __user * uarg)109478a1b96bSEric Biggers int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
109578a1b96bSEric Biggers {
109678a1b96bSEric Biggers 	if (!capable(CAP_SYS_ADMIN))
109778a1b96bSEric Biggers 		return -EACCES;
109878a1b96bSEric Biggers 	return do_remove_key(filp, uarg, true);
109978a1b96bSEric Biggers }
110078a1b96bSEric Biggers EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users);
110178a1b96bSEric Biggers 
11025a7e2992SEric Biggers /*
11035a7e2992SEric Biggers  * Retrieve the status of an fscrypt master encryption key.
11045a7e2992SEric Biggers  *
11055a7e2992SEric Biggers  * We set ->status to indicate whether the key is absent, present, or
11065a7e2992SEric Biggers  * incompletely removed.  "Incompletely removed" means that the master key
11075a7e2992SEric Biggers  * secret has been removed, but some files which had been unlocked with it are
11085a7e2992SEric Biggers  * still in use.  This field allows applications to easily determine the state
11095a7e2992SEric Biggers  * of an encrypted directory without using a hack such as trying to open a
11105a7e2992SEric Biggers  * regular file in it (which can confuse the "incompletely removed" state with
11115a7e2992SEric Biggers  * absent or present).
11125a7e2992SEric Biggers  *
111323c688b5SEric Biggers  * In addition, for v2 policy keys we allow applications to determine, via
111423c688b5SEric Biggers  * ->status_flags and ->user_count, whether the key has been added by the
111523c688b5SEric Biggers  * current user, by other users, or by both.  Most applications should not need
111623c688b5SEric Biggers  * this, since ordinarily only one user should know a given key.  However, if a
111723c688b5SEric Biggers  * secret key is shared by multiple users, applications may wish to add an
111823c688b5SEric Biggers  * already-present key to prevent other users from removing it.  This ioctl can
111923c688b5SEric Biggers  * be used to check whether that really is the case before the work is done to
112023c688b5SEric Biggers  * add the key --- which might e.g. require prompting the user for a passphrase.
112123c688b5SEric Biggers  *
11225a7e2992SEric Biggers  * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of
11235a7e2992SEric Biggers  * Documentation/filesystems/fscrypt.rst.
11245a7e2992SEric Biggers  */
fscrypt_ioctl_get_key_status(struct file * filp,void __user * uarg)11255a7e2992SEric Biggers int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
11265a7e2992SEric Biggers {
11275a7e2992SEric Biggers 	struct super_block *sb = file_inode(filp)->i_sb;
11285a7e2992SEric Biggers 	struct fscrypt_get_key_status_arg arg;
11295a7e2992SEric Biggers 	struct fscrypt_master_key *mk;
11305a7e2992SEric Biggers 	int err;
11315a7e2992SEric Biggers 
11325a7e2992SEric Biggers 	if (copy_from_user(&arg, uarg, sizeof(arg)))
11335a7e2992SEric Biggers 		return -EFAULT;
11345a7e2992SEric Biggers 
11355a7e2992SEric Biggers 	if (!valid_key_spec(&arg.key_spec))
11365a7e2992SEric Biggers 		return -EINVAL;
11375a7e2992SEric Biggers 
11385a7e2992SEric Biggers 	if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
11395a7e2992SEric Biggers 		return -EINVAL;
11405a7e2992SEric Biggers 
114123c688b5SEric Biggers 	arg.status_flags = 0;
114223c688b5SEric Biggers 	arg.user_count = 0;
11435a7e2992SEric Biggers 	memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
11445a7e2992SEric Biggers 
1145d7e7b9afSEric Biggers 	mk = fscrypt_find_master_key(sb, &arg.key_spec);
1146d7e7b9afSEric Biggers 	if (!mk) {
11475a7e2992SEric Biggers 		arg.status = FSCRYPT_KEY_STATUS_ABSENT;
11485a7e2992SEric Biggers 		err = 0;
11495a7e2992SEric Biggers 		goto out;
11505a7e2992SEric Biggers 	}
1151d7e7b9afSEric Biggers 	down_read(&mk->mk_sem);
11525a7e2992SEric Biggers 
11535a7e2992SEric Biggers 	if (!is_master_key_secret_present(&mk->mk_secret)) {
1154d7e7b9afSEric Biggers 		arg.status = refcount_read(&mk->mk_active_refs) > 0 ?
1155d7e7b9afSEric Biggers 			FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED :
1156d7e7b9afSEric Biggers 			FSCRYPT_KEY_STATUS_ABSENT /* raced with full removal */;
11575a7e2992SEric Biggers 		err = 0;
11585a7e2992SEric Biggers 		goto out_release_key;
11595a7e2992SEric Biggers 	}
11605a7e2992SEric Biggers 
11615a7e2992SEric Biggers 	arg.status = FSCRYPT_KEY_STATUS_PRESENT;
116223c688b5SEric Biggers 	if (mk->mk_users) {
116323c688b5SEric Biggers 		struct key *mk_user;
116423c688b5SEric Biggers 
116523c688b5SEric Biggers 		arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
116623c688b5SEric Biggers 		mk_user = find_master_key_user(mk);
116723c688b5SEric Biggers 		if (!IS_ERR(mk_user)) {
116823c688b5SEric Biggers 			arg.status_flags |=
116923c688b5SEric Biggers 				FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
117023c688b5SEric Biggers 			key_put(mk_user);
117123c688b5SEric Biggers 		} else if (mk_user != ERR_PTR(-ENOKEY)) {
117223c688b5SEric Biggers 			err = PTR_ERR(mk_user);
117323c688b5SEric Biggers 			goto out_release_key;
117423c688b5SEric Biggers 		}
117523c688b5SEric Biggers 	}
11765a7e2992SEric Biggers 	err = 0;
11775a7e2992SEric Biggers out_release_key:
1178d7e7b9afSEric Biggers 	up_read(&mk->mk_sem);
1179d7e7b9afSEric Biggers 	fscrypt_put_master_key(mk);
11805a7e2992SEric Biggers out:
11815a7e2992SEric Biggers 	if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
11825a7e2992SEric Biggers 		err = -EFAULT;
11835a7e2992SEric Biggers 	return err;
11845a7e2992SEric Biggers }
11855a7e2992SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status);
11865a7e2992SEric Biggers 
fscrypt_init_keyring(void)118722d94f49SEric Biggers int __init fscrypt_init_keyring(void)
118822d94f49SEric Biggers {
118923c688b5SEric Biggers 	int err;
119023c688b5SEric Biggers 
119123c688b5SEric Biggers 	err = register_key_type(&key_type_fscrypt_user);
119223c688b5SEric Biggers 	if (err)
1193d7e7b9afSEric Biggers 		return err;
119423c688b5SEric Biggers 
119593edd392SEric Biggers 	err = register_key_type(&key_type_fscrypt_provisioning);
119693edd392SEric Biggers 	if (err)
119793edd392SEric Biggers 		goto err_unregister_fscrypt_user;
119893edd392SEric Biggers 
119923c688b5SEric Biggers 	return 0;
120023c688b5SEric Biggers 
120193edd392SEric Biggers err_unregister_fscrypt_user:
120293edd392SEric Biggers 	unregister_key_type(&key_type_fscrypt_user);
120323c688b5SEric Biggers 	return err;
120422d94f49SEric Biggers }
1205