Linux Audio

Check our new training course

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