1 // SPDX-License-Identifier: GPL-2.0
3 * Filesystem-level keyring for llcrypt
5 * Copyright 2019 Google LLC
9 * This file implements management of llcrypt master keys in the
10 * filesystem-level keyring, including the ioctls:
12 * - LL_IOC_ADD_ENCRYPTION_KEY
13 * - LL_IOC_REMOVE_ENCRYPTION_KEY
14 * - LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
15 * - LL_IOC_GET_ENCRYPTION_KEY_STATUS
17 * See the "User API" section of Documentation/filesystems/llcrypt.rst for more
18 * information about these ioctls.
21 * Linux commit 219d54332a09
25 #include <crypto/skcipher.h>
26 #include <linux/key-type.h>
27 #include <linux/seq_file.h>
28 #include <libcfs/linux/linux-misc.h>
30 #include "llcrypt_private.h"
32 static void wipe_master_key_secret(struct llcrypt_master_key_secret *secret)
34 llcrypt_destroy_hkdf(&secret->hkdf);
35 memzero_explicit(secret, sizeof(*secret));
38 static void move_master_key_secret(struct llcrypt_master_key_secret *dst,
39 struct llcrypt_master_key_secret *src)
41 memcpy(dst, src, sizeof(*dst));
42 memzero_explicit(src, sizeof(*src));
45 static void free_master_key(struct llcrypt_master_key *mk)
49 wipe_master_key_secret(&mk->mk_secret);
51 for (i = 0; i < ARRAY_SIZE(mk->mk_mode_keys); i++)
52 crypto_free_skcipher(mk->mk_mode_keys[i]);
54 key_put(mk->mk_users);
58 static inline bool valid_key_spec(const struct llcrypt_key_specifier *spec)
62 return master_key_spec_len(spec) != 0;
65 static int llcrypt_key_instantiate(struct key *key,
66 struct key_preparsed_payload *prep)
68 key->payload.data[0] = (struct llcrypt_master_key *)prep->data;
72 static void llcrypt_key_destroy(struct key *key)
74 free_master_key(key->payload.data[0]);
77 static void llcrypt_key_describe(const struct key *key, struct seq_file *m)
79 seq_puts(m, key->description);
81 if (key_is_positive(key)) {
82 const struct llcrypt_master_key *mk = key->payload.data[0];
84 if (!is_master_key_secret_present(&mk->mk_secret))
85 seq_puts(m, ": secret removed");
90 * Type of key in ->lsi_master_keys. Each key of this type represents a master
91 * key which has been added to the filesystem. Its payload is a
92 * 'struct llcrypt_master_key'. The "." prefix in the key type name prevents
93 * users from adding keys of this type via the keyrings syscalls rather than via
94 * the intended method of LL_IOC_ADD_ENCRYPTION_KEY.
96 static struct key_type key_type_llcrypt = {
98 .instantiate = llcrypt_key_instantiate,
99 .destroy = llcrypt_key_destroy,
100 .describe = llcrypt_key_describe,
103 static int llcrypt_user_key_instantiate(struct key *key,
104 struct key_preparsed_payload *prep)
107 * We just charge LLCRYPT_MAX_KEY_SIZE bytes to the user's key quota for
108 * each key, regardless of the exact key size. The amount of memory
109 * actually used is greater than the size of the raw key anyway.
111 return key_payload_reserve(key, LLCRYPT_MAX_KEY_SIZE);
114 static void llcrypt_user_key_describe(const struct key *key, struct seq_file *m)
116 seq_puts(m, key->description);
120 * Type of key in ->mk_users. Each key of this type represents a particular
121 * user who has added a particular master key.
123 * Note that the name of this key type really should be something like
124 * ".llcrypt-user" instead of simply ".llcrypt". But the shorter name is chosen
125 * mainly for simplicity of presentation in /proc/keys when read by a non-root
126 * user. And it is expected to be rare that a key is actually added by multiple
127 * users, since users should keep their encryption keys confidential.
129 static struct key_type key_type_llcrypt_user = {
131 .instantiate = llcrypt_user_key_instantiate,
132 .describe = llcrypt_user_key_describe,
135 /* Search ->lsi_master_keys or ->mk_users */
136 static struct key *search_llcrypt_keyring(struct key *keyring,
137 struct key_type *type,
138 const char *description)
141 * We need to mark the keyring reference as "possessed" so that we
142 * acquire permission to search it, via the KEY_POS_SEARCH permission.
144 key_ref_t keyref = make_key_ref(keyring, true /* possessed */);
146 #ifdef HAVE_KEYRING_SEARCH_4ARGS
147 keyref = keyring_search(keyref, type, description, false);
149 keyref = keyring_search(keyref, type, description);
151 if (IS_ERR(keyref)) {
152 if (PTR_ERR(keyref) == -EAGAIN || /* not found */
153 PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
154 keyref = ERR_PTR(-ENOKEY);
155 return ERR_CAST(keyref);
157 return key_ref_to_ptr(keyref);
160 #define LLCRYPT_FS_KEYRING_DESCRIPTION_SIZE \
161 (CONST_STRLEN("llcrypt-") + sizeof_field(struct super_block, s_id))
163 #define LLCRYPT_MK_DESCRIPTION_SIZE (2 * LLCRYPT_KEY_IDENTIFIER_SIZE + 1)
165 #define LLCRYPT_MK_USERS_DESCRIPTION_SIZE \
166 (CONST_STRLEN("llcrypt-") + 2 * LLCRYPT_KEY_IDENTIFIER_SIZE + \
167 CONST_STRLEN("-users") + 1)
169 #define LLCRYPT_MK_USER_DESCRIPTION_SIZE \
170 (2 * LLCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
172 static void format_fs_keyring_description(
173 char description[LLCRYPT_FS_KEYRING_DESCRIPTION_SIZE],
174 const struct super_block *sb)
176 sprintf(description, "llcrypt-%s", sb->s_id);
179 static void format_mk_description(
180 char description[LLCRYPT_MK_DESCRIPTION_SIZE],
181 const struct llcrypt_key_specifier *mk_spec)
183 sprintf(description, "%*phN",
184 master_key_spec_len(mk_spec), (u8 *)&mk_spec->u);
187 static void format_mk_users_keyring_description(
188 char description[LLCRYPT_MK_USERS_DESCRIPTION_SIZE],
189 const u8 mk_identifier[LLCRYPT_KEY_IDENTIFIER_SIZE])
191 sprintf(description, "llcrypt-%*phN-users",
192 LLCRYPT_KEY_IDENTIFIER_SIZE, mk_identifier);
195 static void format_mk_user_description(
196 char description[LLCRYPT_MK_USER_DESCRIPTION_SIZE],
197 const u8 mk_identifier[LLCRYPT_KEY_IDENTIFIER_SIZE])
200 sprintf(description, "%*phN.uid.%u", LLCRYPT_KEY_IDENTIFIER_SIZE,
201 mk_identifier, __kuid_val(current_fsuid()));
204 /* Create ->lsi_master_keys if needed. Synchronized by llcrypt_add_key_mutex. */
205 static int allocate_filesystem_keyring(struct super_block *sb)
207 char description[LLCRYPT_FS_KEYRING_DESCRIPTION_SIZE];
209 struct lustre_sb_info *lsi = s2lsi(sb);
214 if (lsi->lsi_master_keys)
217 format_fs_keyring_description(description, sb);
218 keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
219 current_cred(), KEY_POS_SEARCH |
220 KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
221 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
223 return PTR_ERR(keyring);
225 /* Pairs with READ_ONCE() in llcrypt_find_master_key() */
226 smp_store_release(&lsi->lsi_master_keys, keyring);
230 void llcrypt_sb_free(struct super_block *sb)
232 struct lustre_sb_info *lsi = s2lsi(sb);
235 key_put(lsi->lsi_master_keys);
236 lsi->lsi_master_keys = NULL;
239 EXPORT_SYMBOL(llcrypt_sb_free);
242 * Find the specified master key in ->lsi_master_keys.
243 * Returns ERR_PTR(-ENOKEY) if not found.
245 struct key *llcrypt_find_master_key(struct super_block *sb,
246 const struct llcrypt_key_specifier *mk_spec)
249 char description[LLCRYPT_MK_DESCRIPTION_SIZE];
250 struct lustre_sb_info *lsi = s2lsi(sb);
253 return ERR_PTR(-EINVAL);
255 /* pairs with smp_store_release() in allocate_filesystem_keyring() */
256 keyring = READ_ONCE(lsi->lsi_master_keys);
258 return ERR_PTR(-ENOKEY); /* No keyring yet, so no keys yet. */
260 format_mk_description(description, mk_spec);
261 return search_llcrypt_keyring(keyring, &key_type_llcrypt, description);
264 static int allocate_master_key_users_keyring(struct llcrypt_master_key *mk)
266 char description[LLCRYPT_MK_USERS_DESCRIPTION_SIZE];
269 format_mk_users_keyring_description(description,
270 mk->mk_spec.u.identifier);
271 keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
272 current_cred(), KEY_POS_SEARCH |
273 KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
274 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
276 return PTR_ERR(keyring);
278 mk->mk_users = keyring;
283 * Find the current user's "key" in the master key's ->mk_users.
284 * Returns ERR_PTR(-ENOKEY) if not found.
286 static struct key *find_master_key_user(struct llcrypt_master_key *mk)
288 char description[LLCRYPT_MK_USER_DESCRIPTION_SIZE];
290 format_mk_user_description(description, mk->mk_spec.u.identifier);
291 return search_llcrypt_keyring(mk->mk_users, &key_type_llcrypt_user,
296 * Give the current user a "key" in ->mk_users. This charges the user's quota
297 * and marks the master key as added by the current user, so that it cannot be
298 * removed by another user with the key. Either the master key's key->sem must
299 * be held for write, or the master key must be still undergoing initialization.
301 static int add_master_key_user(struct llcrypt_master_key *mk)
303 char description[LLCRYPT_MK_USER_DESCRIPTION_SIZE];
307 format_mk_user_description(description, mk->mk_spec.u.identifier);
308 mk_user = key_alloc(&key_type_llcrypt_user, description,
309 current_fsuid(), current_gid(), current_cred(),
310 KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL);
312 return PTR_ERR(mk_user);
314 err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL);
320 * Remove the current user's "key" from ->mk_users.
321 * The master key's key->sem must be held for write.
323 * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
325 static int remove_master_key_user(struct llcrypt_master_key *mk)
330 mk_user = find_master_key_user(mk);
332 return PTR_ERR(mk_user);
333 err = key_unlink(mk->mk_users, mk_user);
339 * Allocate a new llcrypt_master_key which contains the given secret, set it as
340 * the payload of a new 'struct key' of type llcrypt, and link the 'struct key'
341 * into the given keyring. Synchronized by llcrypt_add_key_mutex.
343 static int add_new_master_key(struct llcrypt_master_key_secret *secret,
344 const struct llcrypt_key_specifier *mk_spec,
347 struct llcrypt_master_key *mk;
348 char description[LLCRYPT_MK_DESCRIPTION_SIZE];
352 mk = kzalloc(sizeof(*mk), GFP_KERNEL);
356 mk->mk_spec = *mk_spec;
358 move_master_key_secret(&mk->mk_secret, secret);
359 init_rwsem(&mk->mk_secret_sem);
361 refcount_set(&mk->mk_refcount, 1); /* secret is present */
362 INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
363 spin_lock_init(&mk->mk_decrypted_inodes_lock);
365 if (mk_spec->type == LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
366 err = allocate_master_key_users_keyring(mk);
369 err = add_master_key_user(mk);
375 * Note that we don't charge this key to anyone's quota, since when
376 * ->mk_users is in use those keys are charged instead, and otherwise
377 * (when ->mk_users isn't in use) only root can add these keys.
379 format_mk_description(description, mk_spec);
380 key = key_alloc(&key_type_llcrypt, description,
381 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
382 KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW,
383 KEY_ALLOC_NOT_IN_QUOTA, NULL);
388 err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL);
402 static int add_existing_master_key(struct llcrypt_master_key *mk,
403 struct llcrypt_master_key_secret *secret)
410 * If the current user is already in ->mk_users, then there's nothing to
411 * do. (Not applicable for v1 policy keys, which have NULL ->mk_users.)
414 mk_user = find_master_key_user(mk);
415 if (mk_user != ERR_PTR(-ENOKEY)) {
417 return PTR_ERR(mk_user);
423 /* If we'll be re-adding ->mk_secret, try to take the reference. */
424 rekey = !is_master_key_secret_present(&mk->mk_secret);
425 if (rekey && !refcount_inc_not_zero(&mk->mk_refcount))
428 /* Add the current user to ->mk_users, if applicable. */
430 err = add_master_key_user(mk);
432 if (rekey && refcount_dec_and_test(&mk->mk_refcount))
438 /* Re-add the secret if needed. */
440 down_write(&mk->mk_secret_sem);
441 move_master_key_secret(&mk->mk_secret, secret);
442 up_write(&mk->mk_secret_sem);
447 static int add_master_key(struct super_block *sb,
448 struct llcrypt_master_key_secret *secret,
449 const struct llcrypt_key_specifier *mk_spec)
451 static DEFINE_MUTEX(llcrypt_add_key_mutex);
453 struct lustre_sb_info *lsi = s2lsi(sb);
459 mutex_lock(&llcrypt_add_key_mutex); /* serialize find + link */
461 key = llcrypt_find_master_key(sb, mk_spec);
466 /* Didn't find the key in ->lsi_master_keys. Add it. */
467 err = allocate_filesystem_keyring(sb);
470 err = add_new_master_key(secret, mk_spec,
471 lsi->lsi_master_keys);
474 * Found the key in ->lsi_master_keys. Re-add the secret if
475 * needed, and add the user to ->mk_users if needed.
477 down_write(&key->sem);
478 err = add_existing_master_key(key->payload.data[0], secret);
480 if (err == KEY_DEAD) {
481 /* Key being removed or needs to be removed */
489 mutex_unlock(&llcrypt_add_key_mutex);
494 * Add a master encryption key to the filesystem, causing all files which were
495 * encrypted with it to appear "unlocked" (decrypted) when accessed.
497 * When adding a key for use by v1 encryption policies, this ioctl is
498 * privileged, and userspace must provide the 'key_descriptor'.
500 * When adding a key for use by v2+ encryption policies, this ioctl is
501 * unprivileged. This is needed, in general, to allow non-root users to use
502 * encryption without encountering the visibility problems of process-subscribed
503 * keyrings and the inability to properly remove keys. This works by having
504 * each key identified by its cryptographically secure hash --- the
505 * 'key_identifier'. The cryptographic hash ensures that a malicious user
506 * cannot add the wrong key for a given identifier. Furthermore, each added key
507 * is charged to the appropriate user's quota for the keyrings service, which
508 * prevents a malicious user from adding too many keys. Finally, we forbid a
509 * user from removing a key while other users have added it too, which prevents
510 * a user who knows another user's key from causing a denial-of-service by
511 * removing it at an inopportune time. (We tolerate that a user who knows a key
512 * can prevent other users from removing it.)
514 * For more details, see the "LL_IOC_ADD_ENCRYPTION_KEY" section of
515 * Documentation/filesystems/llcrypt.rst.
517 int llcrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
519 struct super_block *sb = file_inode(filp)->i_sb;
520 struct llcrypt_add_key_arg __user *uarg = _uarg;
521 struct llcrypt_add_key_arg arg;
522 struct llcrypt_master_key_secret secret;
525 if (copy_from_user(&arg, uarg, sizeof(arg)))
528 if (!valid_key_spec(&arg.key_spec))
531 if (arg.raw_size < LLCRYPT_MIN_KEY_SIZE ||
532 arg.raw_size > LLCRYPT_MAX_KEY_SIZE)
535 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
538 memset(&secret, 0, sizeof(secret));
539 secret.size = arg.raw_size;
541 if (copy_from_user(secret.raw, uarg->raw, secret.size))
542 goto out_wipe_secret;
544 switch (arg.key_spec.type) {
545 case LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
547 * Only root can add keys that are identified by an arbitrary
548 * descriptor rather than by a cryptographic hash --- since
549 * otherwise a malicious user could add the wrong key.
552 if (!capable(CAP_SYS_ADMIN))
553 goto out_wipe_secret;
555 case LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
556 err = llcrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size);
558 goto out_wipe_secret;
561 * Now that the HKDF context is initialized, the raw key is no
564 memzero_explicit(secret.raw, secret.size);
566 /* Calculate the key identifier and return it to userspace. */
567 err = llcrypt_hkdf_expand(&secret.hkdf,
568 HKDF_CONTEXT_KEY_IDENTIFIER,
569 NULL, 0, arg.key_spec.u.identifier,
570 LLCRYPT_KEY_IDENTIFIER_SIZE);
572 goto out_wipe_secret;
574 if (copy_to_user(uarg->key_spec.u.identifier,
575 arg.key_spec.u.identifier,
576 LLCRYPT_KEY_IDENTIFIER_SIZE))
577 goto out_wipe_secret;
582 goto out_wipe_secret;
585 err = add_master_key(sb, &secret, &arg.key_spec);
587 wipe_master_key_secret(&secret);
590 EXPORT_SYMBOL_GPL(llcrypt_ioctl_add_key);
593 * Verify that the current user has added a master key with the given identifier
594 * (returns -ENOKEY if not). This is needed to prevent a user from encrypting
595 * their files using some other user's key which they don't actually know.
596 * Cryptographically this isn't much of a problem, but the semantics of this
597 * would be a bit weird, so it's best to just forbid it.
599 * The system administrator (CAP_FOWNER) can override this, which should be
600 * enough for any use cases where encryption policies are being set using keys
601 * that were chosen ahead of time but aren't available at the moment.
603 * Note that the key may have already removed by the time this returns, but
604 * that's okay; we just care whether the key was there at some point.
606 * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code
608 int llcrypt_verify_key_added(struct super_block *sb,
609 const u8 identifier[LLCRYPT_KEY_IDENTIFIER_SIZE])
611 struct llcrypt_key_specifier mk_spec;
612 struct key *key, *mk_user;
613 struct llcrypt_master_key *mk;
616 mk_spec.type = LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
617 memcpy(mk_spec.u.identifier, identifier, LLCRYPT_KEY_IDENTIFIER_SIZE);
619 key = llcrypt_find_master_key(sb, &mk_spec);
624 mk = key->payload.data[0];
625 mk_user = find_master_key_user(mk);
626 if (IS_ERR(mk_user)) {
627 err = PTR_ERR(mk_user);
634 if (err == -ENOKEY && capable(CAP_FOWNER))
640 * Try to evict the inode's dentries from the dentry cache. If the inode is a
641 * directory, then it can have at most one dentry; however, that dentry may be
642 * pinned by child dentries, so first try to evict the children too.
644 static void shrink_dcache_inode(struct inode *inode)
646 struct dentry *dentry;
648 if (S_ISDIR(inode->i_mode)) {
649 dentry = d_find_any_alias(inode);
651 shrink_dcache_parent(dentry);
655 d_prune_aliases(inode);
658 static void evict_dentries_for_decrypted_inodes(struct llcrypt_master_key *mk)
660 struct llcrypt_info *ci;
662 struct inode *toput_inode = NULL;
664 spin_lock(&mk->mk_decrypted_inodes_lock);
666 list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
667 inode = ci->ci_inode;
668 if (igrab(inode) == NULL)
670 spin_unlock(&mk->mk_decrypted_inodes_lock);
672 shrink_dcache_inode(inode);
676 spin_lock(&mk->mk_decrypted_inodes_lock);
679 spin_unlock(&mk->mk_decrypted_inodes_lock);
683 static int check_for_busy_inodes(struct super_block *sb,
684 struct llcrypt_master_key *mk)
686 struct list_head *pos;
687 size_t busy_count = 0;
689 struct dentry *dentry;
693 spin_lock(&mk->mk_decrypted_inodes_lock);
695 list_for_each(pos, &mk->mk_decrypted_inodes)
698 if (busy_count == 0) {
699 spin_unlock(&mk->mk_decrypted_inodes_lock);
704 /* select an example file to show for debugging purposes */
705 struct inode *inode =
706 list_first_entry(&mk->mk_decrypted_inodes,
708 ci_master_key_link)->ci_inode;
710 dentry = d_find_alias(inode);
712 spin_unlock(&mk->mk_decrypted_inodes_lock);
715 path = dentry_path_raw(dentry, _path, sizeof(_path));
718 if (IS_ERR_OR_NULL(path))
722 "%s: %zu inode(s) still busy after removing key with %s %*phN, including ino %lu (%s)",
723 sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
724 master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
729 static int try_to_lock_encrypted_files(struct super_block *sb,
730 struct llcrypt_master_key *mk)
736 * An inode can't be evicted while it is dirty or has dirty pages.
737 * Thus, we first have to clean the inodes in ->mk_decrypted_inodes.
739 * Just do it the easy way: call sync_filesystem(). It's overkill, but
740 * it works, and it's more important to minimize the amount of caches we
741 * drop than the amount of data we sync. Also, unprivileged users can
742 * already call sync_filesystem() via sys_syncfs() or sys_sync().
744 down_read(&sb->s_umount);
745 err1 = sync_filesystem(sb);
746 up_read(&sb->s_umount);
747 /* If a sync error occurs, still try to evict as much as possible. */
750 * Inodes are pinned by their dentries, so we have to evict their
751 * dentries. shrink_dcache_sb() would suffice, but would be overkill
752 * and inappropriate for use by unprivileged users. So instead go
753 * through the inodes' alias lists and try to evict each dentry.
755 evict_dentries_for_decrypted_inodes(mk);
758 * evict_dentries_for_decrypted_inodes() already iput() each inode in
759 * the list; any inodes for which that dropped the last reference will
760 * have been evicted due to llcrypt_drop_inode() detecting the key
761 * removal and telling the VFS to evict the inode. So to finish, we
762 * just need to check whether any inodes couldn't be evicted.
764 err2 = check_for_busy_inodes(sb, mk);
770 * Try to remove an llcrypt master encryption key.
772 * LL_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's
773 * claim to the key, then removes the key itself if no other users have claims.
774 * LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the
777 * To "remove the key itself", first we wipe the actual master key secret, so
778 * that no more inodes can be unlocked with it. Then we try to evict all cached
779 * inodes that had been unlocked with the key.
781 * If all inodes were evicted, then we unlink the llcrypt_master_key from the
782 * keyring. Otherwise it remains in the keyring in the "incompletely removed"
783 * state (without the actual secret key) where it tracks the list of remaining
784 * inodes. Userspace can execute the ioctl again later to retry eviction, or
785 * alternatively can re-add the secret key again.
787 * For more details, see the "Removing keys" section of
788 * Documentation/filesystems/llcrypt.rst.
790 static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
792 struct super_block *sb = file_inode(filp)->i_sb;
793 struct llcrypt_remove_key_arg __user *uarg = _uarg;
794 struct llcrypt_remove_key_arg arg;
796 struct llcrypt_master_key *mk;
797 u32 status_flags = 0;
801 if (copy_from_user(&arg, uarg, sizeof(arg)))
804 if (!valid_key_spec(&arg.key_spec))
807 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
811 * Only root can add and remove keys that are identified by an arbitrary
812 * descriptor rather than by a cryptographic hash.
814 if (arg.key_spec.type == LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
815 !capable(CAP_SYS_ADMIN))
818 /* Find the key being removed. */
819 key = llcrypt_find_master_key(sb, &arg.key_spec);
822 mk = key->payload.data[0];
824 down_write(&key->sem);
826 /* If relevant, remove current user's (or all users) claim to the key */
827 if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
829 err = keyring_clear(mk->mk_users);
831 err = remove_master_key_user(mk);
836 if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
838 * Other users have still added the key too. We removed
839 * the current user's claim to the key, but we still
840 * can't remove the key itself.
843 LLCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
850 /* No user claims remaining. Go ahead and wipe the secret. */
852 if (is_master_key_secret_present(&mk->mk_secret)) {
853 down_write(&mk->mk_secret_sem);
854 wipe_master_key_secret(&mk->mk_secret);
855 dead = refcount_dec_and_test(&mk->mk_refcount);
856 up_write(&mk->mk_secret_sem);
861 * No inodes reference the key, and we wiped the secret, so the
862 * key object is free to be removed from the keyring.
867 /* Some inodes still reference this key; try to evict them. */
868 err = try_to_lock_encrypted_files(sb, mk);
871 LLCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
876 * We return 0 if we successfully did something: removed a claim to the
877 * key, wiped the secret, or tried locking the files again. Users need
878 * to check the informational status flags if they care whether the key
879 * has been fully removed including all files locked.
884 err = put_user(status_flags, &uarg->removal_status_flags);
888 int llcrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
890 return do_remove_key(filp, uarg, false);
892 EXPORT_SYMBOL_GPL(llcrypt_ioctl_remove_key);
894 int llcrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
896 if (!capable(CAP_SYS_ADMIN))
898 return do_remove_key(filp, uarg, true);
900 EXPORT_SYMBOL_GPL(llcrypt_ioctl_remove_key_all_users);
903 * Retrieve the status of an llcrypt master encryption key.
905 * We set ->status to indicate whether the key is absent, present, or
906 * incompletely removed. "Incompletely removed" means that the master key
907 * secret has been removed, but some files which had been unlocked with it are
908 * still in use. This field allows applications to easily determine the state
909 * of an encrypted directory without using a hack such as trying to open a
910 * regular file in it (which can confuse the "incompletely removed" state with
911 * absent or present).
913 * In addition, for v2 policy keys we allow applications to determine, via
914 * ->status_flags and ->user_count, whether the key has been added by the
915 * current user, by other users, or by both. Most applications should not need
916 * this, since ordinarily only one user should know a given key. However, if a
917 * secret key is shared by multiple users, applications may wish to add an
918 * already-present key to prevent other users from removing it. This ioctl can
919 * be used to check whether that really is the case before the work is done to
920 * add the key --- which might e.g. require prompting the user for a passphrase.
922 * For more details, see the "LL_IOC_GET_ENCRYPTION_KEY_STATUS" section of
923 * Documentation/filesystems/llcrypt.rst.
925 int llcrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
927 struct super_block *sb = file_inode(filp)->i_sb;
928 struct llcrypt_get_key_status_arg arg;
930 struct llcrypt_master_key *mk;
933 if (copy_from_user(&arg, uarg, sizeof(arg)))
936 if (!valid_key_spec(&arg.key_spec))
939 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
942 arg.status_flags = 0;
944 memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
946 key = llcrypt_find_master_key(sb, &arg.key_spec);
948 if (key != ERR_PTR(-ENOKEY))
950 arg.status = LLCRYPT_KEY_STATUS_ABSENT;
954 mk = key->payload.data[0];
955 down_read(&key->sem);
957 if (!is_master_key_secret_present(&mk->mk_secret)) {
958 arg.status = LLCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED;
960 goto out_release_key;
963 arg.status = LLCRYPT_KEY_STATUS_PRESENT;
967 arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
968 mk_user = find_master_key_user(mk);
969 if (!IS_ERR(mk_user)) {
971 LLCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
973 } else if (mk_user != ERR_PTR(-ENOKEY)) {
974 err = PTR_ERR(mk_user);
975 goto out_release_key;
983 if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
987 EXPORT_SYMBOL_GPL(llcrypt_ioctl_get_key_status);
989 int __init llcrypt_init_keyring(void)
993 err = register_key_type(&key_type_llcrypt);
997 err = register_key_type(&key_type_llcrypt_user);
999 goto err_unregister_llcrypt;
1003 err_unregister_llcrypt:
1004 unregister_key_type(&key_type_llcrypt);
1008 void __exit llcrypt_exit_keyring(void)
1010 unregister_key_type(&key_type_llcrypt_user);
1011 unregister_key_type(&key_type_llcrypt);