Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Filesystem-level keyring for fscrypt
0004  *
0005  * Copyright 2019 Google LLC
0006  */
0007 
0008 /*
0009  * This file implements management of fscrypt master keys in the
0010  * filesystem-level keyring, including the ioctls:
0011  *
0012  * - FS_IOC_ADD_ENCRYPTION_KEY
0013  * - FS_IOC_REMOVE_ENCRYPTION_KEY
0014  * - FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
0015  * - FS_IOC_GET_ENCRYPTION_KEY_STATUS
0016  *
0017  * See the "User API" section of Documentation/filesystems/fscrypt.rst for more
0018  * information about these ioctls.
0019  */
0020 
0021 #include <crypto/skcipher.h>
0022 #include <linux/key-type.h>
0023 #include <linux/random.h>
0024 #include <linux/seq_file.h>
0025 
0026 #include "fscrypt_private.h"
0027 
0028 static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
0029 {
0030     fscrypt_destroy_hkdf(&secret->hkdf);
0031     memzero_explicit(secret, sizeof(*secret));
0032 }
0033 
0034 static void move_master_key_secret(struct fscrypt_master_key_secret *dst,
0035                    struct fscrypt_master_key_secret *src)
0036 {
0037     memcpy(dst, src, sizeof(*dst));
0038     memzero_explicit(src, sizeof(*src));
0039 }
0040 
0041 static void free_master_key(struct fscrypt_master_key *mk)
0042 {
0043     size_t i;
0044 
0045     wipe_master_key_secret(&mk->mk_secret);
0046 
0047     for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
0048         fscrypt_destroy_prepared_key(&mk->mk_direct_keys[i]);
0049         fscrypt_destroy_prepared_key(&mk->mk_iv_ino_lblk_64_keys[i]);
0050         fscrypt_destroy_prepared_key(&mk->mk_iv_ino_lblk_32_keys[i]);
0051     }
0052 
0053     key_put(mk->mk_users);
0054     kfree_sensitive(mk);
0055 }
0056 
0057 static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec)
0058 {
0059     if (spec->__reserved)
0060         return false;
0061     return master_key_spec_len(spec) != 0;
0062 }
0063 
0064 static int fscrypt_key_instantiate(struct key *key,
0065                    struct key_preparsed_payload *prep)
0066 {
0067     key->payload.data[0] = (struct fscrypt_master_key *)prep->data;
0068     return 0;
0069 }
0070 
0071 static void fscrypt_key_destroy(struct key *key)
0072 {
0073     free_master_key(key->payload.data[0]);
0074 }
0075 
0076 static void fscrypt_key_describe(const struct key *key, struct seq_file *m)
0077 {
0078     seq_puts(m, key->description);
0079 
0080     if (key_is_positive(key)) {
0081         const struct fscrypt_master_key *mk = key->payload.data[0];
0082 
0083         if (!is_master_key_secret_present(&mk->mk_secret))
0084             seq_puts(m, ": secret removed");
0085     }
0086 }
0087 
0088 /*
0089  * Type of key in ->s_master_keys.  Each key of this type represents a master
0090  * key which has been added to the filesystem.  Its payload is a
0091  * 'struct fscrypt_master_key'.  The "." prefix in the key type name prevents
0092  * users from adding keys of this type via the keyrings syscalls rather than via
0093  * the intended method of FS_IOC_ADD_ENCRYPTION_KEY.
0094  */
0095 static struct key_type key_type_fscrypt = {
0096     .name           = "._fscrypt",
0097     .instantiate        = fscrypt_key_instantiate,
0098     .destroy        = fscrypt_key_destroy,
0099     .describe       = fscrypt_key_describe,
0100 };
0101 
0102 static int fscrypt_user_key_instantiate(struct key *key,
0103                     struct key_preparsed_payload *prep)
0104 {
0105     /*
0106      * We just charge FSCRYPT_MAX_KEY_SIZE bytes to the user's key quota for
0107      * each key, regardless of the exact key size.  The amount of memory
0108      * actually used is greater than the size of the raw key anyway.
0109      */
0110     return key_payload_reserve(key, FSCRYPT_MAX_KEY_SIZE);
0111 }
0112 
0113 static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m)
0114 {
0115     seq_puts(m, key->description);
0116 }
0117 
0118 /*
0119  * Type of key in ->mk_users.  Each key of this type represents a particular
0120  * user who has added a particular master key.
0121  *
0122  * Note that the name of this key type really should be something like
0123  * ".fscrypt-user" instead of simply ".fscrypt".  But the shorter name is chosen
0124  * mainly for simplicity of presentation in /proc/keys when read by a non-root
0125  * user.  And it is expected to be rare that a key is actually added by multiple
0126  * users, since users should keep their encryption keys confidential.
0127  */
0128 static struct key_type key_type_fscrypt_user = {
0129     .name           = ".fscrypt",
0130     .instantiate        = fscrypt_user_key_instantiate,
0131     .describe       = fscrypt_user_key_describe,
0132 };
0133 
0134 /* Search ->s_master_keys or ->mk_users */
0135 static struct key *search_fscrypt_keyring(struct key *keyring,
0136                       struct key_type *type,
0137                       const char *description)
0138 {
0139     /*
0140      * We need to mark the keyring reference as "possessed" so that we
0141      * acquire permission to search it, via the KEY_POS_SEARCH permission.
0142      */
0143     key_ref_t keyref = make_key_ref(keyring, true /* possessed */);
0144 
0145     keyref = keyring_search(keyref, type, description, false);
0146     if (IS_ERR(keyref)) {
0147         if (PTR_ERR(keyref) == -EAGAIN || /* not found */
0148             PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
0149             keyref = ERR_PTR(-ENOKEY);
0150         return ERR_CAST(keyref);
0151     }
0152     return key_ref_to_ptr(keyref);
0153 }
0154 
0155 #define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \
0156     (CONST_STRLEN("fscrypt-") + sizeof_field(struct super_block, s_id))
0157 
0158 #define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1)
0159 
0160 #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE   \
0161     (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
0162      CONST_STRLEN("-users") + 1)
0163 
0164 #define FSCRYPT_MK_USER_DESCRIPTION_SIZE    \
0165     (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
0166 
0167 static void format_fs_keyring_description(
0168             char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE],
0169             const struct super_block *sb)
0170 {
0171     sprintf(description, "fscrypt-%s", sb->s_id);
0172 }
0173 
0174 static void format_mk_description(
0175             char description[FSCRYPT_MK_DESCRIPTION_SIZE],
0176             const struct fscrypt_key_specifier *mk_spec)
0177 {
0178     sprintf(description, "%*phN",
0179         master_key_spec_len(mk_spec), (u8 *)&mk_spec->u);
0180 }
0181 
0182 static void format_mk_users_keyring_description(
0183             char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],
0184             const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
0185 {
0186     sprintf(description, "fscrypt-%*phN-users",
0187         FSCRYPT_KEY_IDENTIFIER_SIZE, mk_identifier);
0188 }
0189 
0190 static void format_mk_user_description(
0191             char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE],
0192             const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
0193 {
0194 
0195     sprintf(description, "%*phN.uid.%u", FSCRYPT_KEY_IDENTIFIER_SIZE,
0196         mk_identifier, __kuid_val(current_fsuid()));
0197 }
0198 
0199 /* Create ->s_master_keys if needed.  Synchronized by fscrypt_add_key_mutex. */
0200 static int allocate_filesystem_keyring(struct super_block *sb)
0201 {
0202     char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE];
0203     struct key *keyring;
0204 
0205     if (sb->s_master_keys)
0206         return 0;
0207 
0208     format_fs_keyring_description(description, sb);
0209     keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
0210                 current_cred(), KEY_POS_SEARCH |
0211                   KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
0212                 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
0213     if (IS_ERR(keyring))
0214         return PTR_ERR(keyring);
0215 
0216     /*
0217      * Pairs with the smp_load_acquire() in fscrypt_find_master_key().
0218      * I.e., here we publish ->s_master_keys with a RELEASE barrier so that
0219      * concurrent tasks can ACQUIRE it.
0220      */
0221     smp_store_release(&sb->s_master_keys, keyring);
0222     return 0;
0223 }
0224 
0225 void fscrypt_sb_free(struct super_block *sb)
0226 {
0227     key_put(sb->s_master_keys);
0228     sb->s_master_keys = NULL;
0229 }
0230 
0231 /*
0232  * Find the specified master key in ->s_master_keys.
0233  * Returns ERR_PTR(-ENOKEY) if not found.
0234  */
0235 struct key *fscrypt_find_master_key(struct super_block *sb,
0236                     const struct fscrypt_key_specifier *mk_spec)
0237 {
0238     struct key *keyring;
0239     char description[FSCRYPT_MK_DESCRIPTION_SIZE];
0240 
0241     /*
0242      * Pairs with the smp_store_release() in allocate_filesystem_keyring().
0243      * I.e., another task can publish ->s_master_keys concurrently,
0244      * executing a RELEASE barrier.  We need to use smp_load_acquire() here
0245      * to safely ACQUIRE the memory the other task published.
0246      */
0247     keyring = smp_load_acquire(&sb->s_master_keys);
0248     if (keyring == NULL)
0249         return ERR_PTR(-ENOKEY); /* No keyring yet, so no keys yet. */
0250 
0251     format_mk_description(description, mk_spec);
0252     return search_fscrypt_keyring(keyring, &key_type_fscrypt, description);
0253 }
0254 
0255 static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk)
0256 {
0257     char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE];
0258     struct key *keyring;
0259 
0260     format_mk_users_keyring_description(description,
0261                         mk->mk_spec.u.identifier);
0262     keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
0263                 current_cred(), KEY_POS_SEARCH |
0264                   KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
0265                 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
0266     if (IS_ERR(keyring))
0267         return PTR_ERR(keyring);
0268 
0269     mk->mk_users = keyring;
0270     return 0;
0271 }
0272 
0273 /*
0274  * Find the current user's "key" in the master key's ->mk_users.
0275  * Returns ERR_PTR(-ENOKEY) if not found.
0276  */
0277 static struct key *find_master_key_user(struct fscrypt_master_key *mk)
0278 {
0279     char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
0280 
0281     format_mk_user_description(description, mk->mk_spec.u.identifier);
0282     return search_fscrypt_keyring(mk->mk_users, &key_type_fscrypt_user,
0283                       description);
0284 }
0285 
0286 /*
0287  * Give the current user a "key" in ->mk_users.  This charges the user's quota
0288  * and marks the master key as added by the current user, so that it cannot be
0289  * removed by another user with the key.  Either the master key's key->sem must
0290  * be held for write, or the master key must be still undergoing initialization.
0291  */
0292 static int add_master_key_user(struct fscrypt_master_key *mk)
0293 {
0294     char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
0295     struct key *mk_user;
0296     int err;
0297 
0298     format_mk_user_description(description, mk->mk_spec.u.identifier);
0299     mk_user = key_alloc(&key_type_fscrypt_user, description,
0300                 current_fsuid(), current_gid(), current_cred(),
0301                 KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL);
0302     if (IS_ERR(mk_user))
0303         return PTR_ERR(mk_user);
0304 
0305     err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL);
0306     key_put(mk_user);
0307     return err;
0308 }
0309 
0310 /*
0311  * Remove the current user's "key" from ->mk_users.
0312  * The master key's key->sem must be held for write.
0313  *
0314  * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
0315  */
0316 static int remove_master_key_user(struct fscrypt_master_key *mk)
0317 {
0318     struct key *mk_user;
0319     int err;
0320 
0321     mk_user = find_master_key_user(mk);
0322     if (IS_ERR(mk_user))
0323         return PTR_ERR(mk_user);
0324     err = key_unlink(mk->mk_users, mk_user);
0325     key_put(mk_user);
0326     return err;
0327 }
0328 
0329 /*
0330  * Allocate a new fscrypt_master_key which contains the given secret, set it as
0331  * the payload of a new 'struct key' of type fscrypt, and link the 'struct key'
0332  * into the given keyring.  Synchronized by fscrypt_add_key_mutex.
0333  */
0334 static int add_new_master_key(struct fscrypt_master_key_secret *secret,
0335                   const struct fscrypt_key_specifier *mk_spec,
0336                   struct key *keyring)
0337 {
0338     struct fscrypt_master_key *mk;
0339     char description[FSCRYPT_MK_DESCRIPTION_SIZE];
0340     struct key *key;
0341     int err;
0342 
0343     mk = kzalloc(sizeof(*mk), GFP_KERNEL);
0344     if (!mk)
0345         return -ENOMEM;
0346 
0347     mk->mk_spec = *mk_spec;
0348 
0349     move_master_key_secret(&mk->mk_secret, secret);
0350 
0351     refcount_set(&mk->mk_refcount, 1); /* secret is present */
0352     INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
0353     spin_lock_init(&mk->mk_decrypted_inodes_lock);
0354 
0355     if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
0356         err = allocate_master_key_users_keyring(mk);
0357         if (err)
0358             goto out_free_mk;
0359         err = add_master_key_user(mk);
0360         if (err)
0361             goto out_free_mk;
0362     }
0363 
0364     /*
0365      * Note that we don't charge this key to anyone's quota, since when
0366      * ->mk_users is in use those keys are charged instead, and otherwise
0367      * (when ->mk_users isn't in use) only root can add these keys.
0368      */
0369     format_mk_description(description, mk_spec);
0370     key = key_alloc(&key_type_fscrypt, description,
0371             GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
0372             KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW,
0373             KEY_ALLOC_NOT_IN_QUOTA, NULL);
0374     if (IS_ERR(key)) {
0375         err = PTR_ERR(key);
0376         goto out_free_mk;
0377     }
0378     err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL);
0379     key_put(key);
0380     if (err)
0381         goto out_free_mk;
0382 
0383     return 0;
0384 
0385 out_free_mk:
0386     free_master_key(mk);
0387     return err;
0388 }
0389 
0390 #define KEY_DEAD    1
0391 
0392 static int add_existing_master_key(struct fscrypt_master_key *mk,
0393                    struct fscrypt_master_key_secret *secret)
0394 {
0395     struct key *mk_user;
0396     bool rekey;
0397     int err;
0398 
0399     /*
0400      * If the current user is already in ->mk_users, then there's nothing to
0401      * do.  (Not applicable for v1 policy keys, which have NULL ->mk_users.)
0402      */
0403     if (mk->mk_users) {
0404         mk_user = find_master_key_user(mk);
0405         if (mk_user != ERR_PTR(-ENOKEY)) {
0406             if (IS_ERR(mk_user))
0407                 return PTR_ERR(mk_user);
0408             key_put(mk_user);
0409             return 0;
0410         }
0411     }
0412 
0413     /* If we'll be re-adding ->mk_secret, try to take the reference. */
0414     rekey = !is_master_key_secret_present(&mk->mk_secret);
0415     if (rekey && !refcount_inc_not_zero(&mk->mk_refcount))
0416         return KEY_DEAD;
0417 
0418     /* Add the current user to ->mk_users, if applicable. */
0419     if (mk->mk_users) {
0420         err = add_master_key_user(mk);
0421         if (err) {
0422             if (rekey && refcount_dec_and_test(&mk->mk_refcount))
0423                 return KEY_DEAD;
0424             return err;
0425         }
0426     }
0427 
0428     /* Re-add the secret if needed. */
0429     if (rekey)
0430         move_master_key_secret(&mk->mk_secret, secret);
0431     return 0;
0432 }
0433 
0434 static int do_add_master_key(struct super_block *sb,
0435                  struct fscrypt_master_key_secret *secret,
0436                  const struct fscrypt_key_specifier *mk_spec)
0437 {
0438     static DEFINE_MUTEX(fscrypt_add_key_mutex);
0439     struct key *key;
0440     int err;
0441 
0442     mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */
0443 retry:
0444     key = fscrypt_find_master_key(sb, mk_spec);
0445     if (IS_ERR(key)) {
0446         err = PTR_ERR(key);
0447         if (err != -ENOKEY)
0448             goto out_unlock;
0449         /* Didn't find the key in ->s_master_keys.  Add it. */
0450         err = allocate_filesystem_keyring(sb);
0451         if (err)
0452             goto out_unlock;
0453         err = add_new_master_key(secret, mk_spec, sb->s_master_keys);
0454     } else {
0455         /*
0456          * Found the key in ->s_master_keys.  Re-add the secret if
0457          * needed, and add the user to ->mk_users if needed.
0458          */
0459         down_write(&key->sem);
0460         err = add_existing_master_key(key->payload.data[0], secret);
0461         up_write(&key->sem);
0462         if (err == KEY_DEAD) {
0463             /* Key being removed or needs to be removed */
0464             key_invalidate(key);
0465             key_put(key);
0466             goto retry;
0467         }
0468         key_put(key);
0469     }
0470 out_unlock:
0471     mutex_unlock(&fscrypt_add_key_mutex);
0472     return err;
0473 }
0474 
0475 static int add_master_key(struct super_block *sb,
0476               struct fscrypt_master_key_secret *secret,
0477               struct fscrypt_key_specifier *key_spec)
0478 {
0479     int err;
0480 
0481     if (key_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
0482         err = fscrypt_init_hkdf(&secret->hkdf, secret->raw,
0483                     secret->size);
0484         if (err)
0485             return err;
0486 
0487         /*
0488          * Now that the HKDF context is initialized, the raw key is no
0489          * longer needed.
0490          */
0491         memzero_explicit(secret->raw, secret->size);
0492 
0493         /* Calculate the key identifier */
0494         err = fscrypt_hkdf_expand(&secret->hkdf,
0495                       HKDF_CONTEXT_KEY_IDENTIFIER, NULL, 0,
0496                       key_spec->u.identifier,
0497                       FSCRYPT_KEY_IDENTIFIER_SIZE);
0498         if (err)
0499             return err;
0500     }
0501     return do_add_master_key(sb, secret, key_spec);
0502 }
0503 
0504 static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload *prep)
0505 {
0506     const struct fscrypt_provisioning_key_payload *payload = prep->data;
0507 
0508     if (prep->datalen < sizeof(*payload) + FSCRYPT_MIN_KEY_SIZE ||
0509         prep->datalen > sizeof(*payload) + FSCRYPT_MAX_KEY_SIZE)
0510         return -EINVAL;
0511 
0512     if (payload->type != FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
0513         payload->type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER)
0514         return -EINVAL;
0515 
0516     if (payload->__reserved)
0517         return -EINVAL;
0518 
0519     prep->payload.data[0] = kmemdup(payload, prep->datalen, GFP_KERNEL);
0520     if (!prep->payload.data[0])
0521         return -ENOMEM;
0522 
0523     prep->quotalen = prep->datalen;
0524     return 0;
0525 }
0526 
0527 static void fscrypt_provisioning_key_free_preparse(
0528                     struct key_preparsed_payload *prep)
0529 {
0530     kfree_sensitive(prep->payload.data[0]);
0531 }
0532 
0533 static void fscrypt_provisioning_key_describe(const struct key *key,
0534                           struct seq_file *m)
0535 {
0536     seq_puts(m, key->description);
0537     if (key_is_positive(key)) {
0538         const struct fscrypt_provisioning_key_payload *payload =
0539             key->payload.data[0];
0540 
0541         seq_printf(m, ": %u [%u]", key->datalen, payload->type);
0542     }
0543 }
0544 
0545 static void fscrypt_provisioning_key_destroy(struct key *key)
0546 {
0547     kfree_sensitive(key->payload.data[0]);
0548 }
0549 
0550 static struct key_type key_type_fscrypt_provisioning = {
0551     .name           = "fscrypt-provisioning",
0552     .preparse       = fscrypt_provisioning_key_preparse,
0553     .free_preparse      = fscrypt_provisioning_key_free_preparse,
0554     .instantiate        = generic_key_instantiate,
0555     .describe       = fscrypt_provisioning_key_describe,
0556     .destroy        = fscrypt_provisioning_key_destroy,
0557 };
0558 
0559 /*
0560  * Retrieve the raw key from the Linux keyring key specified by 'key_id', and
0561  * store it into 'secret'.
0562  *
0563  * The key must be of type "fscrypt-provisioning" and must have the field
0564  * fscrypt_provisioning_key_payload::type set to 'type', indicating that it's
0565  * only usable with fscrypt with the particular KDF version identified by
0566  * 'type'.  We don't use the "logon" key type because there's no way to
0567  * completely restrict the use of such keys; they can be used by any kernel API
0568  * that accepts "logon" keys and doesn't require a specific service prefix.
0569  *
0570  * The ability to specify the key via Linux keyring key is intended for cases
0571  * where userspace needs to re-add keys after the filesystem is unmounted and
0572  * re-mounted.  Most users should just provide the raw key directly instead.
0573  */
0574 static int get_keyring_key(u32 key_id, u32 type,
0575                struct fscrypt_master_key_secret *secret)
0576 {
0577     key_ref_t ref;
0578     struct key *key;
0579     const struct fscrypt_provisioning_key_payload *payload;
0580     int err;
0581 
0582     ref = lookup_user_key(key_id, 0, KEY_NEED_SEARCH);
0583     if (IS_ERR(ref))
0584         return PTR_ERR(ref);
0585     key = key_ref_to_ptr(ref);
0586 
0587     if (key->type != &key_type_fscrypt_provisioning)
0588         goto bad_key;
0589     payload = key->payload.data[0];
0590 
0591     /* Don't allow fscrypt v1 keys to be used as v2 keys and vice versa. */
0592     if (payload->type != type)
0593         goto bad_key;
0594 
0595     secret->size = key->datalen - sizeof(*payload);
0596     memcpy(secret->raw, payload->raw, secret->size);
0597     err = 0;
0598     goto out_put;
0599 
0600 bad_key:
0601     err = -EKEYREJECTED;
0602 out_put:
0603     key_ref_put(ref);
0604     return err;
0605 }
0606 
0607 /*
0608  * Add a master encryption key to the filesystem, causing all files which were
0609  * encrypted with it to appear "unlocked" (decrypted) when accessed.
0610  *
0611  * When adding a key for use by v1 encryption policies, this ioctl is
0612  * privileged, and userspace must provide the 'key_descriptor'.
0613  *
0614  * When adding a key for use by v2+ encryption policies, this ioctl is
0615  * unprivileged.  This is needed, in general, to allow non-root users to use
0616  * encryption without encountering the visibility problems of process-subscribed
0617  * keyrings and the inability to properly remove keys.  This works by having
0618  * each key identified by its cryptographically secure hash --- the
0619  * 'key_identifier'.  The cryptographic hash ensures that a malicious user
0620  * cannot add the wrong key for a given identifier.  Furthermore, each added key
0621  * is charged to the appropriate user's quota for the keyrings service, which
0622  * prevents a malicious user from adding too many keys.  Finally, we forbid a
0623  * user from removing a key while other users have added it too, which prevents
0624  * a user who knows another user's key from causing a denial-of-service by
0625  * removing it at an inopportune time.  (We tolerate that a user who knows a key
0626  * can prevent other users from removing it.)
0627  *
0628  * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of
0629  * Documentation/filesystems/fscrypt.rst.
0630  */
0631 int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
0632 {
0633     struct super_block *sb = file_inode(filp)->i_sb;
0634     struct fscrypt_add_key_arg __user *uarg = _uarg;
0635     struct fscrypt_add_key_arg arg;
0636     struct fscrypt_master_key_secret secret;
0637     int err;
0638 
0639     if (copy_from_user(&arg, uarg, sizeof(arg)))
0640         return -EFAULT;
0641 
0642     if (!valid_key_spec(&arg.key_spec))
0643         return -EINVAL;
0644 
0645     if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
0646         return -EINVAL;
0647 
0648     /*
0649      * Only root can add keys that are identified by an arbitrary descriptor
0650      * rather than by a cryptographic hash --- since otherwise a malicious
0651      * user could add the wrong key.
0652      */
0653     if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
0654         !capable(CAP_SYS_ADMIN))
0655         return -EACCES;
0656 
0657     memset(&secret, 0, sizeof(secret));
0658     if (arg.key_id) {
0659         if (arg.raw_size != 0)
0660             return -EINVAL;
0661         err = get_keyring_key(arg.key_id, arg.key_spec.type, &secret);
0662         if (err)
0663             goto out_wipe_secret;
0664     } else {
0665         if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE ||
0666             arg.raw_size > FSCRYPT_MAX_KEY_SIZE)
0667             return -EINVAL;
0668         secret.size = arg.raw_size;
0669         err = -EFAULT;
0670         if (copy_from_user(secret.raw, uarg->raw, secret.size))
0671             goto out_wipe_secret;
0672     }
0673 
0674     err = add_master_key(sb, &secret, &arg.key_spec);
0675     if (err)
0676         goto out_wipe_secret;
0677 
0678     /* Return the key identifier to userspace, if applicable */
0679     err = -EFAULT;
0680     if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
0681         copy_to_user(uarg->key_spec.u.identifier, arg.key_spec.u.identifier,
0682              FSCRYPT_KEY_IDENTIFIER_SIZE))
0683         goto out_wipe_secret;
0684     err = 0;
0685 out_wipe_secret:
0686     wipe_master_key_secret(&secret);
0687     return err;
0688 }
0689 EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
0690 
0691 static void
0692 fscrypt_get_test_dummy_secret(struct fscrypt_master_key_secret *secret)
0693 {
0694     static u8 test_key[FSCRYPT_MAX_KEY_SIZE];
0695 
0696     get_random_once(test_key, FSCRYPT_MAX_KEY_SIZE);
0697 
0698     memset(secret, 0, sizeof(*secret));
0699     secret->size = FSCRYPT_MAX_KEY_SIZE;
0700     memcpy(secret->raw, test_key, FSCRYPT_MAX_KEY_SIZE);
0701 }
0702 
0703 int fscrypt_get_test_dummy_key_identifier(
0704                 u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
0705 {
0706     struct fscrypt_master_key_secret secret;
0707     int err;
0708 
0709     fscrypt_get_test_dummy_secret(&secret);
0710 
0711     err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size);
0712     if (err)
0713         goto out;
0714     err = fscrypt_hkdf_expand(&secret.hkdf, HKDF_CONTEXT_KEY_IDENTIFIER,
0715                   NULL, 0, key_identifier,
0716                   FSCRYPT_KEY_IDENTIFIER_SIZE);
0717 out:
0718     wipe_master_key_secret(&secret);
0719     return err;
0720 }
0721 
0722 /**
0723  * fscrypt_add_test_dummy_key() - add the test dummy encryption key
0724  * @sb: the filesystem instance to add the key to
0725  * @dummy_policy: the encryption policy for test_dummy_encryption
0726  *
0727  * If needed, add the key for the test_dummy_encryption mount option to the
0728  * filesystem.  To prevent misuse of this mount option, a per-boot random key is
0729  * used instead of a hardcoded one.  This makes it so that any encrypted files
0730  * created using this option won't be accessible after a reboot.
0731  *
0732  * Return: 0 on success, -errno on failure
0733  */
0734 int fscrypt_add_test_dummy_key(struct super_block *sb,
0735                    const struct fscrypt_dummy_policy *dummy_policy)
0736 {
0737     const union fscrypt_policy *policy = dummy_policy->policy;
0738     struct fscrypt_key_specifier key_spec;
0739     struct fscrypt_master_key_secret secret;
0740     int err;
0741 
0742     if (!policy)
0743         return 0;
0744     err = fscrypt_policy_to_key_spec(policy, &key_spec);
0745     if (err)
0746         return err;
0747     fscrypt_get_test_dummy_secret(&secret);
0748     err = add_master_key(sb, &secret, &key_spec);
0749     wipe_master_key_secret(&secret);
0750     return err;
0751 }
0752 EXPORT_SYMBOL_GPL(fscrypt_add_test_dummy_key);
0753 
0754 /*
0755  * Verify that the current user has added a master key with the given identifier
0756  * (returns -ENOKEY if not).  This is needed to prevent a user from encrypting
0757  * their files using some other user's key which they don't actually know.
0758  * Cryptographically this isn't much of a problem, but the semantics of this
0759  * would be a bit weird, so it's best to just forbid it.
0760  *
0761  * The system administrator (CAP_FOWNER) can override this, which should be
0762  * enough for any use cases where encryption policies are being set using keys
0763  * that were chosen ahead of time but aren't available at the moment.
0764  *
0765  * Note that the key may have already removed by the time this returns, but
0766  * that's okay; we just care whether the key was there at some point.
0767  *
0768  * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code
0769  */
0770 int fscrypt_verify_key_added(struct super_block *sb,
0771                  const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
0772 {
0773     struct fscrypt_key_specifier mk_spec;
0774     struct key *key, *mk_user;
0775     struct fscrypt_master_key *mk;
0776     int err;
0777 
0778     mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
0779     memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
0780 
0781     key = fscrypt_find_master_key(sb, &mk_spec);
0782     if (IS_ERR(key)) {
0783         err = PTR_ERR(key);
0784         goto out;
0785     }
0786     mk = key->payload.data[0];
0787     mk_user = find_master_key_user(mk);
0788     if (IS_ERR(mk_user)) {
0789         err = PTR_ERR(mk_user);
0790     } else {
0791         key_put(mk_user);
0792         err = 0;
0793     }
0794     key_put(key);
0795 out:
0796     if (err == -ENOKEY && capable(CAP_FOWNER))
0797         err = 0;
0798     return err;
0799 }
0800 
0801 /*
0802  * Try to evict the inode's dentries from the dentry cache.  If the inode is a
0803  * directory, then it can have at most one dentry; however, that dentry may be
0804  * pinned by child dentries, so first try to evict the children too.
0805  */
0806 static void shrink_dcache_inode(struct inode *inode)
0807 {
0808     struct dentry *dentry;
0809 
0810     if (S_ISDIR(inode->i_mode)) {
0811         dentry = d_find_any_alias(inode);
0812         if (dentry) {
0813             shrink_dcache_parent(dentry);
0814             dput(dentry);
0815         }
0816     }
0817     d_prune_aliases(inode);
0818 }
0819 
0820 static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk)
0821 {
0822     struct fscrypt_info *ci;
0823     struct inode *inode;
0824     struct inode *toput_inode = NULL;
0825 
0826     spin_lock(&mk->mk_decrypted_inodes_lock);
0827 
0828     list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
0829         inode = ci->ci_inode;
0830         spin_lock(&inode->i_lock);
0831         if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
0832             spin_unlock(&inode->i_lock);
0833             continue;
0834         }
0835         __iget(inode);
0836         spin_unlock(&inode->i_lock);
0837         spin_unlock(&mk->mk_decrypted_inodes_lock);
0838 
0839         shrink_dcache_inode(inode);
0840         iput(toput_inode);
0841         toput_inode = inode;
0842 
0843         spin_lock(&mk->mk_decrypted_inodes_lock);
0844     }
0845 
0846     spin_unlock(&mk->mk_decrypted_inodes_lock);
0847     iput(toput_inode);
0848 }
0849 
0850 static int check_for_busy_inodes(struct super_block *sb,
0851                  struct fscrypt_master_key *mk)
0852 {
0853     struct list_head *pos;
0854     size_t busy_count = 0;
0855     unsigned long ino;
0856     char ino_str[50] = "";
0857 
0858     spin_lock(&mk->mk_decrypted_inodes_lock);
0859 
0860     list_for_each(pos, &mk->mk_decrypted_inodes)
0861         busy_count++;
0862 
0863     if (busy_count == 0) {
0864         spin_unlock(&mk->mk_decrypted_inodes_lock);
0865         return 0;
0866     }
0867 
0868     {
0869         /* select an example file to show for debugging purposes */
0870         struct inode *inode =
0871             list_first_entry(&mk->mk_decrypted_inodes,
0872                      struct fscrypt_info,
0873                      ci_master_key_link)->ci_inode;
0874         ino = inode->i_ino;
0875     }
0876     spin_unlock(&mk->mk_decrypted_inodes_lock);
0877 
0878     /* If the inode is currently being created, ino may still be 0. */
0879     if (ino)
0880         snprintf(ino_str, sizeof(ino_str), ", including ino %lu", ino);
0881 
0882     fscrypt_warn(NULL,
0883              "%s: %zu inode(s) still busy after removing key with %s %*phN%s",
0884              sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
0885              master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
0886              ino_str);
0887     return -EBUSY;
0888 }
0889 
0890 static int try_to_lock_encrypted_files(struct super_block *sb,
0891                        struct fscrypt_master_key *mk)
0892 {
0893     int err1;
0894     int err2;
0895 
0896     /*
0897      * An inode can't be evicted while it is dirty or has dirty pages.
0898      * Thus, we first have to clean the inodes in ->mk_decrypted_inodes.
0899      *
0900      * Just do it the easy way: call sync_filesystem().  It's overkill, but
0901      * it works, and it's more important to minimize the amount of caches we
0902      * drop than the amount of data we sync.  Also, unprivileged users can
0903      * already call sync_filesystem() via sys_syncfs() or sys_sync().
0904      */
0905     down_read(&sb->s_umount);
0906     err1 = sync_filesystem(sb);
0907     up_read(&sb->s_umount);
0908     /* If a sync error occurs, still try to evict as much as possible. */
0909 
0910     /*
0911      * Inodes are pinned by their dentries, so we have to evict their
0912      * dentries.  shrink_dcache_sb() would suffice, but would be overkill
0913      * and inappropriate for use by unprivileged users.  So instead go
0914      * through the inodes' alias lists and try to evict each dentry.
0915      */
0916     evict_dentries_for_decrypted_inodes(mk);
0917 
0918     /*
0919      * evict_dentries_for_decrypted_inodes() already iput() each inode in
0920      * the list; any inodes for which that dropped the last reference will
0921      * have been evicted due to fscrypt_drop_inode() detecting the key
0922      * removal and telling the VFS to evict the inode.  So to finish, we
0923      * just need to check whether any inodes couldn't be evicted.
0924      */
0925     err2 = check_for_busy_inodes(sb, mk);
0926 
0927     return err1 ?: err2;
0928 }
0929 
0930 /*
0931  * Try to remove an fscrypt master encryption key.
0932  *
0933  * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's
0934  * claim to the key, then removes the key itself if no other users have claims.
0935  * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the
0936  * key itself.
0937  *
0938  * To "remove the key itself", first we wipe the actual master key secret, so
0939  * that no more inodes can be unlocked with it.  Then we try to evict all cached
0940  * inodes that had been unlocked with the key.
0941  *
0942  * If all inodes were evicted, then we unlink the fscrypt_master_key from the
0943  * keyring.  Otherwise it remains in the keyring in the "incompletely removed"
0944  * state (without the actual secret key) where it tracks the list of remaining
0945  * inodes.  Userspace can execute the ioctl again later to retry eviction, or
0946  * alternatively can re-add the secret key again.
0947  *
0948  * For more details, see the "Removing keys" section of
0949  * Documentation/filesystems/fscrypt.rst.
0950  */
0951 static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
0952 {
0953     struct super_block *sb = file_inode(filp)->i_sb;
0954     struct fscrypt_remove_key_arg __user *uarg = _uarg;
0955     struct fscrypt_remove_key_arg arg;
0956     struct key *key;
0957     struct fscrypt_master_key *mk;
0958     u32 status_flags = 0;
0959     int err;
0960     bool dead;
0961 
0962     if (copy_from_user(&arg, uarg, sizeof(arg)))
0963         return -EFAULT;
0964 
0965     if (!valid_key_spec(&arg.key_spec))
0966         return -EINVAL;
0967 
0968     if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
0969         return -EINVAL;
0970 
0971     /*
0972      * Only root can add and remove keys that are identified by an arbitrary
0973      * descriptor rather than by a cryptographic hash.
0974      */
0975     if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
0976         !capable(CAP_SYS_ADMIN))
0977         return -EACCES;
0978 
0979     /* Find the key being removed. */
0980     key = fscrypt_find_master_key(sb, &arg.key_spec);
0981     if (IS_ERR(key))
0982         return PTR_ERR(key);
0983     mk = key->payload.data[0];
0984 
0985     down_write(&key->sem);
0986 
0987     /* If relevant, remove current user's (or all users) claim to the key */
0988     if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
0989         if (all_users)
0990             err = keyring_clear(mk->mk_users);
0991         else
0992             err = remove_master_key_user(mk);
0993         if (err) {
0994             up_write(&key->sem);
0995             goto out_put_key;
0996         }
0997         if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
0998             /*
0999              * Other users have still added the key too.  We removed
1000              * the current user's claim to the key, but we still
1001              * can't remove the key itself.
1002              */
1003             status_flags |=
1004                 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
1005             err = 0;
1006             up_write(&key->sem);
1007             goto out_put_key;
1008         }
1009     }
1010 
1011     /* No user claims remaining.  Go ahead and wipe the secret. */
1012     dead = false;
1013     if (is_master_key_secret_present(&mk->mk_secret)) {
1014         wipe_master_key_secret(&mk->mk_secret);
1015         dead = refcount_dec_and_test(&mk->mk_refcount);
1016     }
1017     up_write(&key->sem);
1018     if (dead) {
1019         /*
1020          * No inodes reference the key, and we wiped the secret, so the
1021          * key object is free to be removed from the keyring.
1022          */
1023         key_invalidate(key);
1024         err = 0;
1025     } else {
1026         /* Some inodes still reference this key; try to evict them. */
1027         err = try_to_lock_encrypted_files(sb, mk);
1028         if (err == -EBUSY) {
1029             status_flags |=
1030                 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
1031             err = 0;
1032         }
1033     }
1034     /*
1035      * We return 0 if we successfully did something: removed a claim to the
1036      * key, wiped the secret, or tried locking the files again.  Users need
1037      * to check the informational status flags if they care whether the key
1038      * has been fully removed including all files locked.
1039      */
1040 out_put_key:
1041     key_put(key);
1042     if (err == 0)
1043         err = put_user(status_flags, &uarg->removal_status_flags);
1044     return err;
1045 }
1046 
1047 int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
1048 {
1049     return do_remove_key(filp, uarg, false);
1050 }
1051 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key);
1052 
1053 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
1054 {
1055     if (!capable(CAP_SYS_ADMIN))
1056         return -EACCES;
1057     return do_remove_key(filp, uarg, true);
1058 }
1059 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users);
1060 
1061 /*
1062  * Retrieve the status of an fscrypt master encryption key.
1063  *
1064  * We set ->status to indicate whether the key is absent, present, or
1065  * incompletely removed.  "Incompletely removed" means that the master key
1066  * secret has been removed, but some files which had been unlocked with it are
1067  * still in use.  This field allows applications to easily determine the state
1068  * of an encrypted directory without using a hack such as trying to open a
1069  * regular file in it (which can confuse the "incompletely removed" state with
1070  * absent or present).
1071  *
1072  * In addition, for v2 policy keys we allow applications to determine, via
1073  * ->status_flags and ->user_count, whether the key has been added by the
1074  * current user, by other users, or by both.  Most applications should not need
1075  * this, since ordinarily only one user should know a given key.  However, if a
1076  * secret key is shared by multiple users, applications may wish to add an
1077  * already-present key to prevent other users from removing it.  This ioctl can
1078  * be used to check whether that really is the case before the work is done to
1079  * add the key --- which might e.g. require prompting the user for a passphrase.
1080  *
1081  * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of
1082  * Documentation/filesystems/fscrypt.rst.
1083  */
1084 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
1085 {
1086     struct super_block *sb = file_inode(filp)->i_sb;
1087     struct fscrypt_get_key_status_arg arg;
1088     struct key *key;
1089     struct fscrypt_master_key *mk;
1090     int err;
1091 
1092     if (copy_from_user(&arg, uarg, sizeof(arg)))
1093         return -EFAULT;
1094 
1095     if (!valid_key_spec(&arg.key_spec))
1096         return -EINVAL;
1097 
1098     if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
1099         return -EINVAL;
1100 
1101     arg.status_flags = 0;
1102     arg.user_count = 0;
1103     memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
1104 
1105     key = fscrypt_find_master_key(sb, &arg.key_spec);
1106     if (IS_ERR(key)) {
1107         if (key != ERR_PTR(-ENOKEY))
1108             return PTR_ERR(key);
1109         arg.status = FSCRYPT_KEY_STATUS_ABSENT;
1110         err = 0;
1111         goto out;
1112     }
1113     mk = key->payload.data[0];
1114     down_read(&key->sem);
1115 
1116     if (!is_master_key_secret_present(&mk->mk_secret)) {
1117         arg.status = FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED;
1118         err = 0;
1119         goto out_release_key;
1120     }
1121 
1122     arg.status = FSCRYPT_KEY_STATUS_PRESENT;
1123     if (mk->mk_users) {
1124         struct key *mk_user;
1125 
1126         arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
1127         mk_user = find_master_key_user(mk);
1128         if (!IS_ERR(mk_user)) {
1129             arg.status_flags |=
1130                 FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
1131             key_put(mk_user);
1132         } else if (mk_user != ERR_PTR(-ENOKEY)) {
1133             err = PTR_ERR(mk_user);
1134             goto out_release_key;
1135         }
1136     }
1137     err = 0;
1138 out_release_key:
1139     up_read(&key->sem);
1140     key_put(key);
1141 out:
1142     if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
1143         err = -EFAULT;
1144     return err;
1145 }
1146 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status);
1147 
1148 int __init fscrypt_init_keyring(void)
1149 {
1150     int err;
1151 
1152     err = register_key_type(&key_type_fscrypt);
1153     if (err)
1154         return err;
1155 
1156     err = register_key_type(&key_type_fscrypt_user);
1157     if (err)
1158         goto err_unregister_fscrypt;
1159 
1160     err = register_key_type(&key_type_fscrypt_provisioning);
1161     if (err)
1162         goto err_unregister_fscrypt_user;
1163 
1164     return 0;
1165 
1166 err_unregister_fscrypt_user:
1167     unregister_key_type(&key_type_fscrypt_user);
1168 err_unregister_fscrypt:
1169     unregister_key_type(&key_type_fscrypt);
1170     return err;
1171 }