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