Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 2 | /* |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 3 | * Key setup facility for FS encryption support. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2015, Google, Inc. |
| 6 | * |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 7 | * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar. |
| 8 | * Heavily modified since then. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Eric Biggers | a575784 | 2018-01-05 10:45:00 -0800 | [diff] [blame] | 11 | #include <crypto/skcipher.h> |
Eric Biggers | 0109ce76 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 12 | #include <linux/key.h> |
| 13 | |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 14 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 15 | |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 16 | struct fscrypt_mode fscrypt_modes[] = { |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 17 | [FSCRYPT_MODE_AES_256_XTS] = { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 18 | .friendly_name = "AES-256-XTS", |
| 19 | .cipher_str = "xts(aes)", |
| 20 | .keysize = 64, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 21 | .ivsize = 16, |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 22 | }, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 23 | [FSCRYPT_MODE_AES_256_CTS] = { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 24 | .friendly_name = "AES-256-CTS-CBC", |
| 25 | .cipher_str = "cts(cbc(aes))", |
| 26 | .keysize = 32, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 27 | .ivsize = 16, |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 28 | }, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 29 | [FSCRYPT_MODE_AES_128_CBC] = { |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 30 | .friendly_name = "AES-128-CBC-ESSIV", |
| 31 | .cipher_str = "essiv(cbc(aes),sha256)", |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 32 | .keysize = 16, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 33 | .ivsize = 16, |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 34 | }, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 35 | [FSCRYPT_MODE_AES_128_CTS] = { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 36 | .friendly_name = "AES-128-CTS-CBC", |
| 37 | .cipher_str = "cts(cbc(aes))", |
| 38 | .keysize = 16, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 39 | .ivsize = 16, |
| 40 | }, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 41 | [FSCRYPT_MODE_ADIANTUM] = { |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 42 | .friendly_name = "Adiantum", |
| 43 | .cipher_str = "adiantum(xchacha12,aes)", |
| 44 | .keysize = 32, |
| 45 | .ivsize = 32, |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 46 | }, |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 49 | static struct fscrypt_mode * |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 50 | select_encryption_mode(const union fscrypt_policy *policy, |
| 51 | const struct inode *inode) |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 52 | { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 53 | if (S_ISREG(inode->i_mode)) |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 54 | return &fscrypt_modes[fscrypt_policy_contents_mode(policy)]; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 55 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 56 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 57 | return &fscrypt_modes[fscrypt_policy_fnames_mode(policy)]; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 58 | |
| 59 | WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", |
| 60 | inode->i_ino, (inode->i_mode & S_IFMT)); |
| 61 | return ERR_PTR(-EINVAL); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 64 | /* Create a symmetric cipher object for the given encryption mode and key */ |
Eric Biggers | 0109ce76 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 65 | struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode, |
| 66 | const u8 *raw_key, |
| 67 | const struct inode *inode) |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 68 | { |
| 69 | struct crypto_skcipher *tfm; |
| 70 | int err; |
| 71 | |
| 72 | tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); |
| 73 | if (IS_ERR(tfm)) { |
Eric Biggers | 29a98c1 | 2019-07-24 11:08:00 -0700 | [diff] [blame] | 74 | if (PTR_ERR(tfm) == -ENOENT) { |
Eric Biggers | a4d14e9 | 2019-07-24 11:07:59 -0700 | [diff] [blame] | 75 | fscrypt_warn(inode, |
| 76 | "Missing crypto API support for %s (API name: \"%s\")", |
| 77 | mode->friendly_name, mode->cipher_str); |
Eric Biggers | 29a98c1 | 2019-07-24 11:08:00 -0700 | [diff] [blame] | 78 | return ERR_PTR(-ENOPKG); |
| 79 | } |
| 80 | fscrypt_err(inode, "Error allocating '%s' transform: %ld", |
| 81 | mode->cipher_str, PTR_ERR(tfm)); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 82 | return tfm; |
| 83 | } |
Eric Biggers | ff73c2c | 2019-10-21 13:49:03 -0700 | [diff] [blame] | 84 | if (!xchg(&mode->logged_impl_name, 1)) { |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 85 | /* |
| 86 | * fscrypt performance can vary greatly depending on which |
| 87 | * crypto algorithm implementation is used. Help people debug |
| 88 | * performance problems by logging the ->cra_driver_name the |
Eric Biggers | ff73c2c | 2019-10-21 13:49:03 -0700 | [diff] [blame] | 89 | * first time a mode is used. |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 90 | */ |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 91 | pr_info("fscrypt: %s using implementation \"%s\"\n", |
Eric Biggers | 6e1adb8 | 2019-12-09 12:38:10 -0800 | [diff] [blame] | 92 | mode->friendly_name, crypto_skcipher_driver_name(tfm)); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 93 | } |
Eric Biggers | c64cfb9 | 2019-12-09 12:39:18 -0800 | [diff] [blame] | 94 | if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) { |
| 95 | err = -EINVAL; |
| 96 | goto err_free_tfm; |
| 97 | } |
Eric Biggers | 231baec | 2019-01-18 22:48:00 -0800 | [diff] [blame] | 98 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 99 | err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize); |
| 100 | if (err) |
| 101 | goto err_free_tfm; |
| 102 | |
| 103 | return tfm; |
| 104 | |
| 105 | err_free_tfm: |
| 106 | crypto_free_skcipher(tfm); |
| 107 | return ERR_PTR(err); |
| 108 | } |
| 109 | |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 110 | /* Given the per-file key, set up the file's crypto transform object */ |
Eric Biggers | 0109ce76 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 111 | int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key) |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 112 | { |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 113 | struct crypto_skcipher *tfm; |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 114 | |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 115 | tfm = fscrypt_allocate_skcipher(ci->ci_mode, derived_key, ci->ci_inode); |
| 116 | if (IS_ERR(tfm)) |
| 117 | return PTR_ERR(tfm); |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 118 | |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 119 | ci->ci_ctfm = tfm; |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 120 | ci->ci_owns_key = true; |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 124 | static int setup_per_mode_key(struct fscrypt_info *ci, |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 125 | struct fscrypt_master_key *mk, |
| 126 | struct crypto_skcipher **tfms, |
| 127 | u8 hkdf_context, bool include_fs_uuid) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 128 | { |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 129 | const struct inode *inode = ci->ci_inode; |
| 130 | const struct super_block *sb = inode->i_sb; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 131 | struct fscrypt_mode *mode = ci->ci_mode; |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 132 | const u8 mode_num = mode - fscrypt_modes; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 133 | struct crypto_skcipher *tfm, *prev_tfm; |
| 134 | u8 mode_key[FSCRYPT_MAX_KEY_SIZE]; |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 135 | u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)]; |
| 136 | unsigned int hkdf_infolen = 0; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 137 | int err; |
| 138 | |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 139 | if (WARN_ON(mode_num > __FSCRYPT_MODE_MAX)) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 140 | return -EINVAL; |
| 141 | |
| 142 | /* pairs with cmpxchg() below */ |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 143 | tfm = READ_ONCE(tfms[mode_num]); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 144 | if (likely(tfm != NULL)) |
| 145 | goto done; |
| 146 | |
| 147 | BUILD_BUG_ON(sizeof(mode_num) != 1); |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 148 | BUILD_BUG_ON(sizeof(sb->s_uuid) != 16); |
| 149 | BUILD_BUG_ON(sizeof(hkdf_info) != 17); |
| 150 | hkdf_info[hkdf_infolen++] = mode_num; |
| 151 | if (include_fs_uuid) { |
| 152 | memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid, |
| 153 | sizeof(sb->s_uuid)); |
| 154 | hkdf_infolen += sizeof(sb->s_uuid); |
| 155 | } |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 156 | err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 157 | hkdf_context, hkdf_info, hkdf_infolen, |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 158 | mode_key, mode->keysize); |
| 159 | if (err) |
| 160 | return err; |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 161 | tfm = fscrypt_allocate_skcipher(mode, mode_key, inode); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 162 | memzero_explicit(mode_key, mode->keysize); |
| 163 | if (IS_ERR(tfm)) |
| 164 | return PTR_ERR(tfm); |
| 165 | |
| 166 | /* pairs with READ_ONCE() above */ |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 167 | prev_tfm = cmpxchg(&tfms[mode_num], NULL, tfm); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 168 | if (prev_tfm != NULL) { |
| 169 | crypto_free_skcipher(tfm); |
| 170 | tfm = prev_tfm; |
| 171 | } |
| 172 | done: |
| 173 | ci->ci_ctfm = tfm; |
| 174 | return 0; |
| 175 | } |
| 176 | |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame^] | 177 | int fscrypt_derive_dirhash_key(struct fscrypt_info *ci, |
| 178 | const struct fscrypt_master_key *mk) |
| 179 | { |
| 180 | int err; |
| 181 | |
| 182 | err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, HKDF_CONTEXT_DIRHASH_KEY, |
| 183 | ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE, |
| 184 | (u8 *)&ci->ci_dirhash_key, |
| 185 | sizeof(ci->ci_dirhash_key)); |
| 186 | if (err) |
| 187 | return err; |
| 188 | ci->ci_dirhash_key_initialized = true; |
| 189 | return 0; |
| 190 | } |
| 191 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 192 | static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci, |
| 193 | struct fscrypt_master_key *mk) |
| 194 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 195 | int err; |
| 196 | |
| 197 | if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) { |
| 198 | /* |
| 199 | * DIRECT_KEY: instead of deriving per-file keys, the per-file |
| 200 | * nonce will be included in all the IVs. But unlike v1 |
| 201 | * policies, for v2 policies in this case we don't encrypt with |
| 202 | * the master key directly but rather derive a per-mode key. |
| 203 | * This ensures that the master key is consistently used only |
| 204 | * for HKDF, avoiding key reuse issues. |
| 205 | */ |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame^] | 206 | err = setup_per_mode_key(ci, mk, mk->mk_direct_tfms, |
| 207 | HKDF_CONTEXT_DIRECT_KEY, false); |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 208 | } else if (ci->ci_policy.v2.flags & |
| 209 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) { |
| 210 | /* |
| 211 | * IV_INO_LBLK_64: encryption keys are derived from (master_key, |
| 212 | * mode_num, filesystem_uuid), and inode number is included in |
| 213 | * the IVs. This format is optimized for use with inline |
| 214 | * encryption hardware compliant with the UFS or eMMC standards. |
| 215 | */ |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame^] | 216 | err = setup_per_mode_key(ci, mk, mk->mk_iv_ino_lblk_64_tfms, |
| 217 | HKDF_CONTEXT_IV_INO_LBLK_64_KEY, true); |
| 218 | } else { |
| 219 | u8 derived_key[FSCRYPT_MAX_KEY_SIZE]; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 220 | |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame^] | 221 | err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, |
| 222 | HKDF_CONTEXT_PER_FILE_KEY, |
| 223 | ci->ci_nonce, |
| 224 | FS_KEY_DERIVATION_NONCE_SIZE, |
| 225 | derived_key, ci->ci_mode->keysize); |
| 226 | if (err) |
| 227 | return err; |
| 228 | |
| 229 | err = fscrypt_set_derived_key(ci, derived_key); |
| 230 | memzero_explicit(derived_key, ci->ci_mode->keysize); |
| 231 | } |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 232 | if (err) |
| 233 | return err; |
| 234 | |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame^] | 235 | /* Derive a secret dirhash key for directories that need it. */ |
| 236 | if (S_ISDIR(ci->ci_inode->i_mode) && IS_CASEFOLDED(ci->ci_inode)) { |
| 237 | err = fscrypt_derive_dirhash_key(ci, mk); |
| 238 | if (err) |
| 239 | return err; |
| 240 | } |
| 241 | |
| 242 | return 0; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 245 | /* |
| 246 | * Find the master key, then set up the inode's actual encryption key. |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 247 | * |
| 248 | * If the master key is found in the filesystem-level keyring, then the |
| 249 | * corresponding 'struct key' is returned in *master_key_ret with |
Eric Biggers | 23c688b | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 250 | * ->mk_secret_sem read-locked. This is needed to ensure that only one task |
| 251 | * links the fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race |
| 252 | * to create an fscrypt_info for the same inode), and to synchronize the master |
| 253 | * key being removed with a new inode starting to use it. |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 254 | */ |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 255 | static int setup_file_encryption_key(struct fscrypt_info *ci, |
| 256 | struct key **master_key_ret) |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 257 | { |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 258 | struct key *key; |
| 259 | struct fscrypt_master_key *mk = NULL; |
| 260 | struct fscrypt_key_specifier mk_spec; |
| 261 | int err; |
| 262 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 263 | switch (ci->ci_policy.version) { |
| 264 | case FSCRYPT_POLICY_V1: |
| 265 | mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; |
| 266 | memcpy(mk_spec.u.descriptor, |
| 267 | ci->ci_policy.v1.master_key_descriptor, |
| 268 | FSCRYPT_KEY_DESCRIPTOR_SIZE); |
| 269 | break; |
| 270 | case FSCRYPT_POLICY_V2: |
| 271 | mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; |
| 272 | memcpy(mk_spec.u.identifier, |
| 273 | ci->ci_policy.v2.master_key_identifier, |
| 274 | FSCRYPT_KEY_IDENTIFIER_SIZE); |
| 275 | break; |
| 276 | default: |
| 277 | WARN_ON(1); |
| 278 | return -EINVAL; |
| 279 | } |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 280 | |
| 281 | key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec); |
| 282 | if (IS_ERR(key)) { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 283 | if (key != ERR_PTR(-ENOKEY) || |
| 284 | ci->ci_policy.version != FSCRYPT_POLICY_V1) |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 285 | return PTR_ERR(key); |
| 286 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 287 | /* |
| 288 | * As a legacy fallback for v1 policies, search for the key in |
| 289 | * the current task's subscribed keyrings too. Don't move this |
| 290 | * to before the search of ->s_master_keys, since users |
| 291 | * shouldn't be able to override filesystem-level keys. |
| 292 | */ |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 293 | return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci); |
| 294 | } |
| 295 | |
| 296 | mk = key->payload.data[0]; |
Eric Biggers | 23c688b | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 297 | down_read(&mk->mk_secret_sem); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 298 | |
| 299 | /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */ |
| 300 | if (!is_master_key_secret_present(&mk->mk_secret)) { |
| 301 | err = -ENOKEY; |
| 302 | goto out_release_key; |
| 303 | } |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 304 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 305 | /* |
| 306 | * Require that the master key be at least as long as the derived key. |
| 307 | * Otherwise, the derived key cannot possibly contain as much entropy as |
| 308 | * that required by the encryption mode it will be used for. For v1 |
| 309 | * policies it's also required for the KDF to work at all. |
| 310 | */ |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 311 | if (mk->mk_secret.size < ci->ci_mode->keysize) { |
| 312 | fscrypt_warn(NULL, |
| 313 | "key with %s %*phN is too short (got %u bytes, need %u+ bytes)", |
| 314 | master_key_spec_type(&mk_spec), |
| 315 | master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u, |
| 316 | mk->mk_secret.size, ci->ci_mode->keysize); |
| 317 | err = -ENOKEY; |
| 318 | goto out_release_key; |
| 319 | } |
| 320 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 321 | switch (ci->ci_policy.version) { |
| 322 | case FSCRYPT_POLICY_V1: |
| 323 | err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw); |
| 324 | break; |
| 325 | case FSCRYPT_POLICY_V2: |
| 326 | err = fscrypt_setup_v2_file_key(ci, mk); |
| 327 | break; |
| 328 | default: |
| 329 | WARN_ON(1); |
| 330 | err = -EINVAL; |
| 331 | break; |
| 332 | } |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 333 | if (err) |
| 334 | goto out_release_key; |
| 335 | |
| 336 | *master_key_ret = key; |
| 337 | return 0; |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 338 | |
| 339 | out_release_key: |
Eric Biggers | 23c688b | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 340 | up_read(&mk->mk_secret_sem); |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 341 | key_put(key); |
| 342 | return err; |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 345 | static void put_crypt_info(struct fscrypt_info *ci) |
| 346 | { |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 347 | struct key *key; |
| 348 | |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 349 | if (!ci) |
| 350 | return; |
| 351 | |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 352 | if (ci->ci_direct_key) |
Eric Biggers | 0109ce76 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 353 | fscrypt_put_direct_key(ci->ci_direct_key); |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 354 | else if (ci->ci_owns_key) |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 355 | crypto_free_skcipher(ci->ci_ctfm); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 356 | |
| 357 | key = ci->ci_master_key; |
| 358 | if (key) { |
| 359 | struct fscrypt_master_key *mk = key->payload.data[0]; |
| 360 | |
| 361 | /* |
| 362 | * Remove this inode from the list of inodes that were unlocked |
| 363 | * with the master key. |
| 364 | * |
| 365 | * In addition, if we're removing the last inode from a key that |
| 366 | * already had its secret removed, invalidate the key so that it |
| 367 | * gets removed from ->s_master_keys. |
| 368 | */ |
| 369 | spin_lock(&mk->mk_decrypted_inodes_lock); |
| 370 | list_del(&ci->ci_master_key_link); |
| 371 | spin_unlock(&mk->mk_decrypted_inodes_lock); |
| 372 | if (refcount_dec_and_test(&mk->mk_refcount)) |
| 373 | key_invalidate(key); |
| 374 | key_put(key); |
| 375 | } |
Eric Biggers | 6f99756 | 2019-10-09 16:34:17 -0700 | [diff] [blame] | 376 | memzero_explicit(ci, sizeof(*ci)); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 377 | kmem_cache_free(fscrypt_info_cachep, ci); |
| 378 | } |
| 379 | |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 380 | int fscrypt_get_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 381 | { |
| 382 | struct fscrypt_info *crypt_info; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 383 | union fscrypt_context ctx; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 384 | struct fscrypt_mode *mode; |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 385 | struct key *master_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 386 | int res; |
| 387 | |
Eric Biggers | e37a784 | 2019-04-11 14:32:15 -0700 | [diff] [blame] | 388 | if (fscrypt_has_encryption_key(inode)) |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 389 | return 0; |
| 390 | |
David Gstir | f32d7ac | 2016-12-06 23:53:57 +0100 | [diff] [blame] | 391 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 392 | if (res) |
| 393 | return res; |
| 394 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 395 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 396 | if (res < 0) { |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 397 | if (!fscrypt_dummy_context_enabled(inode) || |
Eric Biggers | 63f668f | 2019-07-24 11:07:59 -0700 | [diff] [blame] | 398 | IS_ENCRYPTED(inode)) { |
| 399 | fscrypt_warn(inode, |
| 400 | "Error %d getting encryption context", |
| 401 | res); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 402 | return res; |
Eric Biggers | 63f668f | 2019-07-24 11:07:59 -0700 | [diff] [blame] | 403 | } |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 404 | /* Fake up a context for an unencrypted directory */ |
| 405 | memset(&ctx, 0, sizeof(ctx)); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 406 | ctx.version = FSCRYPT_CONTEXT_V1; |
| 407 | ctx.v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; |
| 408 | ctx.v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; |
| 409 | memset(ctx.v1.master_key_descriptor, 0x42, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 410 | FSCRYPT_KEY_DESCRIPTOR_SIZE); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 411 | res = sizeof(ctx.v1); |
Eric Biggers | 63f668f | 2019-07-24 11:07:59 -0700 | [diff] [blame] | 412 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 413 | |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 414 | crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 415 | if (!crypt_info) |
| 416 | return -ENOMEM; |
| 417 | |
Eric Biggers | 59dc6a8 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 418 | crypt_info->ci_inode = inode; |
| 419 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 420 | res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res); |
| 421 | if (res) { |
| 422 | fscrypt_warn(inode, |
| 423 | "Unrecognized or corrupt encryption context"); |
| 424 | goto out; |
| 425 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 426 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 427 | switch (ctx.version) { |
| 428 | case FSCRYPT_CONTEXT_V1: |
| 429 | memcpy(crypt_info->ci_nonce, ctx.v1.nonce, |
| 430 | FS_KEY_DERIVATION_NONCE_SIZE); |
| 431 | break; |
| 432 | case FSCRYPT_CONTEXT_V2: |
| 433 | memcpy(crypt_info->ci_nonce, ctx.v2.nonce, |
| 434 | FS_KEY_DERIVATION_NONCE_SIZE); |
| 435 | break; |
| 436 | default: |
| 437 | WARN_ON(1); |
| 438 | res = -EINVAL; |
| 439 | goto out; |
| 440 | } |
| 441 | |
| 442 | if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) { |
| 443 | res = -EINVAL; |
| 444 | goto out; |
| 445 | } |
| 446 | |
| 447 | mode = select_encryption_mode(&crypt_info->ci_policy, inode); |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 448 | if (IS_ERR(mode)) { |
| 449 | res = PTR_ERR(mode); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 450 | goto out; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 451 | } |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 452 | WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE); |
| 453 | crypt_info->ci_mode = mode; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 454 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 455 | res = setup_file_encryption_key(crypt_info, &master_key); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 456 | if (res) |
| 457 | goto out; |
| 458 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 459 | if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) { |
| 460 | if (master_key) { |
| 461 | struct fscrypt_master_key *mk = |
| 462 | master_key->payload.data[0]; |
| 463 | |
| 464 | refcount_inc(&mk->mk_refcount); |
| 465 | crypt_info->ci_master_key = key_get(master_key); |
| 466 | spin_lock(&mk->mk_decrypted_inodes_lock); |
| 467 | list_add(&crypt_info->ci_master_key_link, |
| 468 | &mk->mk_decrypted_inodes); |
| 469 | spin_unlock(&mk->mk_decrypted_inodes_lock); |
| 470 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 471 | crypt_info = NULL; |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 472 | } |
| 473 | res = 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 474 | out: |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 475 | if (master_key) { |
Eric Biggers | 23c688b | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 476 | struct fscrypt_master_key *mk = master_key->payload.data[0]; |
| 477 | |
| 478 | up_read(&mk->mk_secret_sem); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 479 | key_put(master_key); |
| 480 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 481 | if (res == -ENOKEY) |
| 482 | res = 0; |
| 483 | put_crypt_info(crypt_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 484 | return res; |
| 485 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 486 | EXPORT_SYMBOL(fscrypt_get_encryption_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 487 | |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 488 | /** |
| 489 | * fscrypt_put_encryption_info - free most of an inode's fscrypt data |
| 490 | * |
| 491 | * Free the inode's fscrypt_info. Filesystems must call this when the inode is |
| 492 | * being evicted. An RCU grace period need not have elapsed yet. |
| 493 | */ |
Eric Biggers | 3d204e2 | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 494 | void fscrypt_put_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 495 | { |
Eric Biggers | 3d204e2 | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 496 | put_crypt_info(inode->i_crypt_info); |
| 497 | inode->i_crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 498 | } |
| 499 | EXPORT_SYMBOL(fscrypt_put_encryption_info); |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 500 | |
| 501 | /** |
| 502 | * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay |
| 503 | * |
| 504 | * Free the inode's cached decrypted symlink target, if any. Filesystems must |
| 505 | * call this after an RCU grace period, just before they free the inode. |
| 506 | */ |
| 507 | void fscrypt_free_inode(struct inode *inode) |
| 508 | { |
| 509 | if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) { |
| 510 | kfree(inode->i_link); |
| 511 | inode->i_link = NULL; |
| 512 | } |
| 513 | } |
| 514 | EXPORT_SYMBOL(fscrypt_free_inode); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 515 | |
| 516 | /** |
| 517 | * fscrypt_drop_inode - check whether the inode's master key has been removed |
| 518 | * |
| 519 | * Filesystems supporting fscrypt must call this from their ->drop_inode() |
| 520 | * method so that encrypted inodes are evicted as soon as they're no longer in |
| 521 | * use and their master key has been removed. |
| 522 | * |
| 523 | * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0 |
| 524 | */ |
| 525 | int fscrypt_drop_inode(struct inode *inode) |
| 526 | { |
| 527 | const struct fscrypt_info *ci = READ_ONCE(inode->i_crypt_info); |
| 528 | const struct fscrypt_master_key *mk; |
| 529 | |
| 530 | /* |
| 531 | * If ci is NULL, then the inode doesn't have an encryption key set up |
| 532 | * so it's irrelevant. If ci_master_key is NULL, then the master key |
| 533 | * was provided via the legacy mechanism of the process-subscribed |
| 534 | * keyrings, so we don't know whether it's been removed or not. |
| 535 | */ |
| 536 | if (!ci || !ci->ci_master_key) |
| 537 | return 0; |
| 538 | mk = ci->ci_master_key->payload.data[0]; |
| 539 | |
| 540 | /* |
Eric Biggers | 23c688b | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 541 | * Note: since we aren't holding ->mk_secret_sem, the result here can |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 542 | * immediately become outdated. But there's no correctness problem with |
| 543 | * unnecessarily evicting. Nor is there a correctness problem with not |
| 544 | * evicting while iput() is racing with the key being removed, since |
| 545 | * then the thread removing the key will either evict the inode itself |
| 546 | * or will correctly detect that it wasn't evicted due to the race. |
| 547 | */ |
| 548 | return !is_master_key_secret_present(&mk->mk_secret); |
| 549 | } |
| 550 | EXPORT_SYMBOL_GPL(fscrypt_drop_inode); |