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