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> |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 13 | #include <linux/random.h> |
Eric Biggers | 0109ce76 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 14 | |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 15 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 16 | |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 17 | struct fscrypt_mode fscrypt_modes[] = { |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 18 | [FSCRYPT_MODE_AES_256_XTS] = { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 19 | .friendly_name = "AES-256-XTS", |
| 20 | .cipher_str = "xts(aes)", |
| 21 | .keysize = 64, |
Eric Biggers | 7f595d6 | 2021-09-20 20:03:03 -0700 | [diff] [blame] | 22 | .security_strength = 32, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 23 | .ivsize = 16, |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 24 | .blk_crypto_mode = BLK_ENCRYPTION_MODE_AES_256_XTS, |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 25 | }, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 26 | [FSCRYPT_MODE_AES_256_CTS] = { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 27 | .friendly_name = "AES-256-CTS-CBC", |
| 28 | .cipher_str = "cts(cbc(aes))", |
| 29 | .keysize = 32, |
Eric Biggers | 7f595d6 | 2021-09-20 20:03:03 -0700 | [diff] [blame] | 30 | .security_strength = 32, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 31 | .ivsize = 16, |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 32 | }, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 33 | [FSCRYPT_MODE_AES_128_CBC] = { |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 34 | .friendly_name = "AES-128-CBC-ESSIV", |
| 35 | .cipher_str = "essiv(cbc(aes),sha256)", |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 36 | .keysize = 16, |
Eric Biggers | 7f595d6 | 2021-09-20 20:03:03 -0700 | [diff] [blame] | 37 | .security_strength = 16, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 38 | .ivsize = 16, |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 39 | .blk_crypto_mode = BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV, |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 40 | }, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 41 | [FSCRYPT_MODE_AES_128_CTS] = { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 42 | .friendly_name = "AES-128-CTS-CBC", |
| 43 | .cipher_str = "cts(cbc(aes))", |
| 44 | .keysize = 16, |
Eric Biggers | 7f595d6 | 2021-09-20 20:03:03 -0700 | [diff] [blame] | 45 | .security_strength = 16, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 46 | .ivsize = 16, |
| 47 | }, |
Eric Biggers | 3b6df59 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 48 | [FSCRYPT_MODE_ADIANTUM] = { |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 49 | .friendly_name = "Adiantum", |
| 50 | .cipher_str = "adiantum(xchacha12,aes)", |
| 51 | .keysize = 32, |
Eric Biggers | 7f595d6 | 2021-09-20 20:03:03 -0700 | [diff] [blame] | 52 | .security_strength = 32, |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 53 | .ivsize = 32, |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 54 | .blk_crypto_mode = BLK_ENCRYPTION_MODE_ADIANTUM, |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 55 | }, |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 58 | static DEFINE_MUTEX(fscrypt_mode_key_setup_mutex); |
| 59 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 60 | static struct fscrypt_mode * |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 61 | select_encryption_mode(const union fscrypt_policy *policy, |
| 62 | const struct inode *inode) |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 63 | { |
Eric Biggers | 3ceb654 | 2020-10-23 17:51:31 -0700 | [diff] [blame] | 64 | BUILD_BUG_ON(ARRAY_SIZE(fscrypt_modes) != FSCRYPT_MODE_MAX + 1); |
| 65 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 66 | if (S_ISREG(inode->i_mode)) |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 67 | return &fscrypt_modes[fscrypt_policy_contents_mode(policy)]; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 68 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 69 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 70 | return &fscrypt_modes[fscrypt_policy_fnames_mode(policy)]; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 71 | |
| 72 | WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", |
| 73 | inode->i_ino, (inode->i_mode & S_IFMT)); |
| 74 | return ERR_PTR(-EINVAL); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 77 | /* Create a symmetric cipher object for the given encryption mode and key */ |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 78 | static struct crypto_skcipher * |
| 79 | fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key, |
| 80 | const struct inode *inode) |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 81 | { |
| 82 | struct crypto_skcipher *tfm; |
| 83 | int err; |
| 84 | |
| 85 | tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); |
| 86 | if (IS_ERR(tfm)) { |
Eric Biggers | 29a98c1 | 2019-07-24 11:08:00 -0700 | [diff] [blame] | 87 | if (PTR_ERR(tfm) == -ENOENT) { |
Eric Biggers | a4d14e9 | 2019-07-24 11:07:59 -0700 | [diff] [blame] | 88 | fscrypt_warn(inode, |
| 89 | "Missing crypto API support for %s (API name: \"%s\")", |
| 90 | mode->friendly_name, mode->cipher_str); |
Eric Biggers | 29a98c1 | 2019-07-24 11:08:00 -0700 | [diff] [blame] | 91 | return ERR_PTR(-ENOPKG); |
| 92 | } |
| 93 | fscrypt_err(inode, "Error allocating '%s' transform: %ld", |
| 94 | mode->cipher_str, PTR_ERR(tfm)); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 95 | return tfm; |
| 96 | } |
Eric Biggers | ff73c2c | 2019-10-21 13:49:03 -0700 | [diff] [blame] | 97 | if (!xchg(&mode->logged_impl_name, 1)) { |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 98 | /* |
| 99 | * fscrypt performance can vary greatly depending on which |
| 100 | * crypto algorithm implementation is used. Help people debug |
| 101 | * performance problems by logging the ->cra_driver_name the |
Eric Biggers | ff73c2c | 2019-10-21 13:49:03 -0700 | [diff] [blame] | 102 | * first time a mode is used. |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 103 | */ |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 104 | pr_info("fscrypt: %s using implementation \"%s\"\n", |
Eric Biggers | 6e1adb8 | 2019-12-09 12:38:10 -0800 | [diff] [blame] | 105 | mode->friendly_name, crypto_skcipher_driver_name(tfm)); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 106 | } |
Eric Biggers | c64cfb9 | 2019-12-09 12:39:18 -0800 | [diff] [blame] | 107 | if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) { |
| 108 | err = -EINVAL; |
| 109 | goto err_free_tfm; |
| 110 | } |
Eric Biggers | 231baec | 2019-01-18 22:48:00 -0800 | [diff] [blame] | 111 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 112 | err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize); |
| 113 | if (err) |
| 114 | goto err_free_tfm; |
| 115 | |
| 116 | return tfm; |
| 117 | |
| 118 | err_free_tfm: |
| 119 | crypto_free_skcipher(tfm); |
| 120 | return ERR_PTR(err); |
| 121 | } |
| 122 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 123 | /* |
| 124 | * Prepare the crypto transform object or blk-crypto key in @prep_key, given the |
Eric Biggers | b7e072f | 2021-10-25 19:10:42 -0700 | [diff] [blame] | 125 | * raw key, encryption mode (@ci->ci_mode), flag indicating which encryption |
| 126 | * implementation (fs-layer or blk-crypto) will be used (@ci->ci_inlinecrypt), |
| 127 | * and IV generation method (@ci->ci_policy.flags). |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 128 | */ |
| 129 | int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key, |
| 130 | const u8 *raw_key, const struct fscrypt_info *ci) |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 131 | { |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 132 | struct crypto_skcipher *tfm; |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 133 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 134 | if (fscrypt_using_inline_encryption(ci)) |
| 135 | return fscrypt_prepare_inline_crypt_key(prep_key, raw_key, ci); |
| 136 | |
Eric Biggers | f592efe | 2020-01-20 14:31:58 -0800 | [diff] [blame] | 137 | tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode); |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 138 | if (IS_ERR(tfm)) |
| 139 | return PTR_ERR(tfm); |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 140 | /* |
Eric Biggers | 97c6327 | 2020-07-21 15:59:17 -0700 | [diff] [blame] | 141 | * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared(). |
| 142 | * I.e., here we publish ->tfm with a RELEASE barrier so that |
| 143 | * concurrent tasks can ACQUIRE it. Note that this concurrency is only |
| 144 | * possible for per-mode keys, not for per-file keys. |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 145 | */ |
| 146 | smp_store_release(&prep_key->tfm, tfm); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 150 | /* Destroy a crypto transform object and/or blk-crypto key. */ |
| 151 | void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key) |
| 152 | { |
| 153 | crypto_free_skcipher(prep_key->tfm); |
| 154 | fscrypt_destroy_inline_crypt_key(prep_key); |
| 155 | } |
| 156 | |
| 157 | /* Given a per-file encryption key, set up the file's crypto transform object */ |
| 158 | int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key) |
| 159 | { |
| 160 | ci->ci_owns_key = true; |
| 161 | return fscrypt_prepare_key(&ci->ci_enc_key, raw_key, ci); |
| 162 | } |
| 163 | |
Eric Biggers | f592efe | 2020-01-20 14:31:58 -0800 | [diff] [blame] | 164 | static int setup_per_mode_enc_key(struct fscrypt_info *ci, |
| 165 | struct fscrypt_master_key *mk, |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 166 | struct fscrypt_prepared_key *keys, |
Eric Biggers | f592efe | 2020-01-20 14:31:58 -0800 | [diff] [blame] | 167 | u8 hkdf_context, bool include_fs_uuid) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 168 | { |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 169 | const struct inode *inode = ci->ci_inode; |
| 170 | const struct super_block *sb = inode->i_sb; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 171 | struct fscrypt_mode *mode = ci->ci_mode; |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 172 | const u8 mode_num = mode - fscrypt_modes; |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 173 | struct fscrypt_prepared_key *prep_key; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 174 | u8 mode_key[FSCRYPT_MAX_KEY_SIZE]; |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 175 | u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)]; |
| 176 | unsigned int hkdf_infolen = 0; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 177 | int err; |
| 178 | |
Eric Biggers | 3ceb654 | 2020-10-23 17:51:31 -0700 | [diff] [blame] | 179 | if (WARN_ON(mode_num > FSCRYPT_MODE_MAX)) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 180 | return -EINVAL; |
| 181 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 182 | prep_key = &keys[mode_num]; |
| 183 | if (fscrypt_is_key_prepared(prep_key, ci)) { |
| 184 | ci->ci_enc_key = *prep_key; |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | mutex_lock(&fscrypt_mode_key_setup_mutex); |
| 189 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 190 | if (fscrypt_is_key_prepared(prep_key, ci)) |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 191 | goto done_unlock; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 192 | |
| 193 | BUILD_BUG_ON(sizeof(mode_num) != 1); |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 194 | BUILD_BUG_ON(sizeof(sb->s_uuid) != 16); |
| 195 | BUILD_BUG_ON(sizeof(hkdf_info) != 17); |
| 196 | hkdf_info[hkdf_infolen++] = mode_num; |
| 197 | if (include_fs_uuid) { |
| 198 | memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid, |
| 199 | sizeof(sb->s_uuid)); |
| 200 | hkdf_infolen += sizeof(sb->s_uuid); |
| 201 | } |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 202 | err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 203 | hkdf_context, hkdf_info, hkdf_infolen, |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 204 | mode_key, mode->keysize); |
| 205 | if (err) |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 206 | goto out_unlock; |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 207 | err = fscrypt_prepare_key(prep_key, mode_key, ci); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 208 | memzero_explicit(mode_key, mode->keysize); |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 209 | if (err) |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 210 | goto out_unlock; |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 211 | done_unlock: |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 212 | ci->ci_enc_key = *prep_key; |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 213 | err = 0; |
| 214 | out_unlock: |
| 215 | mutex_unlock(&fscrypt_mode_key_setup_mutex); |
| 216 | return err; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Eric Biggers | 2fc2b43 | 2021-06-05 00:50:33 -0700 | [diff] [blame] | 219 | /* |
| 220 | * Derive a SipHash key from the given fscrypt master key and the given |
| 221 | * application-specific information string. |
| 222 | * |
| 223 | * Note that the KDF produces a byte array, but the SipHash APIs expect the key |
| 224 | * as a pair of 64-bit words. Therefore, on big endian CPUs we have to do an |
| 225 | * endianness swap in order to get the same results as on little endian CPUs. |
| 226 | */ |
| 227 | static int fscrypt_derive_siphash_key(const struct fscrypt_master_key *mk, |
| 228 | u8 context, const u8 *info, |
| 229 | unsigned int infolen, siphash_key_t *key) |
| 230 | { |
| 231 | int err; |
| 232 | |
| 233 | err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, context, info, infolen, |
| 234 | (u8 *)key, sizeof(*key)); |
| 235 | if (err) |
| 236 | return err; |
| 237 | |
| 238 | BUILD_BUG_ON(sizeof(*key) != 16); |
| 239 | BUILD_BUG_ON(ARRAY_SIZE(key->key) != 2); |
| 240 | le64_to_cpus(&key->key[0]); |
| 241 | le64_to_cpus(&key->key[1]); |
| 242 | return 0; |
| 243 | } |
| 244 | |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 245 | int fscrypt_derive_dirhash_key(struct fscrypt_info *ci, |
| 246 | const struct fscrypt_master_key *mk) |
| 247 | { |
| 248 | int err; |
| 249 | |
Eric Biggers | 2fc2b43 | 2021-06-05 00:50:33 -0700 | [diff] [blame] | 250 | err = fscrypt_derive_siphash_key(mk, HKDF_CONTEXT_DIRHASH_KEY, |
| 251 | ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE, |
| 252 | &ci->ci_dirhash_key); |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 253 | if (err) |
| 254 | return err; |
| 255 | ci->ci_dirhash_key_initialized = true; |
| 256 | return 0; |
| 257 | } |
| 258 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 259 | void fscrypt_hash_inode_number(struct fscrypt_info *ci, |
| 260 | const struct fscrypt_master_key *mk) |
| 261 | { |
| 262 | WARN_ON(ci->ci_inode->i_ino == 0); |
| 263 | WARN_ON(!mk->mk_ino_hash_key_initialized); |
| 264 | |
| 265 | ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino, |
| 266 | &mk->mk_ino_hash_key); |
| 267 | } |
| 268 | |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 269 | static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci, |
| 270 | struct fscrypt_master_key *mk) |
| 271 | { |
| 272 | int err; |
| 273 | |
| 274 | err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_32_keys, |
| 275 | HKDF_CONTEXT_IV_INO_LBLK_32_KEY, true); |
| 276 | if (err) |
| 277 | return err; |
| 278 | |
| 279 | /* pairs with smp_store_release() below */ |
| 280 | if (!smp_load_acquire(&mk->mk_ino_hash_key_initialized)) { |
| 281 | |
| 282 | mutex_lock(&fscrypt_mode_key_setup_mutex); |
| 283 | |
| 284 | if (mk->mk_ino_hash_key_initialized) |
| 285 | goto unlock; |
| 286 | |
Eric Biggers | 2fc2b43 | 2021-06-05 00:50:33 -0700 | [diff] [blame] | 287 | err = fscrypt_derive_siphash_key(mk, |
| 288 | HKDF_CONTEXT_INODE_HASH_KEY, |
| 289 | NULL, 0, &mk->mk_ino_hash_key); |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 290 | if (err) |
| 291 | goto unlock; |
| 292 | /* pairs with smp_load_acquire() above */ |
| 293 | smp_store_release(&mk->mk_ino_hash_key_initialized, true); |
| 294 | unlock: |
| 295 | mutex_unlock(&fscrypt_mode_key_setup_mutex); |
| 296 | if (err) |
| 297 | return err; |
| 298 | } |
| 299 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 300 | /* |
| 301 | * New inodes may not have an inode number assigned yet. |
| 302 | * Hashing their inode number is delayed until later. |
| 303 | */ |
Eric Biggers | 92cfcd0 | 2020-10-30 17:45:56 -0700 | [diff] [blame] | 304 | if (ci->ci_inode->i_ino) |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 305 | fscrypt_hash_inode_number(ci, mk); |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 309 | static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci, |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 310 | struct fscrypt_master_key *mk, |
| 311 | bool need_dirhash_key) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 312 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 313 | int err; |
| 314 | |
| 315 | if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) { |
| 316 | /* |
Eric Biggers | f592efe | 2020-01-20 14:31:58 -0800 | [diff] [blame] | 317 | * DIRECT_KEY: instead of deriving per-file encryption keys, the |
| 318 | * per-file nonce will be included in all the IVs. But unlike |
| 319 | * v1 policies, for v2 policies in this case we don't encrypt |
| 320 | * with the master key directly but rather derive a per-mode |
| 321 | * encryption key. This ensures that the master key is |
| 322 | * consistently used only for HKDF, avoiding key reuse issues. |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 323 | */ |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 324 | err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_keys, |
Eric Biggers | f592efe | 2020-01-20 14:31:58 -0800 | [diff] [blame] | 325 | HKDF_CONTEXT_DIRECT_KEY, false); |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 326 | } else if (ci->ci_policy.v2.flags & |
| 327 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) { |
| 328 | /* |
| 329 | * IV_INO_LBLK_64: encryption keys are derived from (master_key, |
| 330 | * mode_num, filesystem_uuid), and inode number is included in |
| 331 | * the IVs. This format is optimized for use with inline |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 332 | * encryption hardware compliant with the UFS standard. |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 333 | */ |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 334 | err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_keys, |
Eric Biggers | f592efe | 2020-01-20 14:31:58 -0800 | [diff] [blame] | 335 | HKDF_CONTEXT_IV_INO_LBLK_64_KEY, |
| 336 | true); |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 337 | } else if (ci->ci_policy.v2.flags & |
| 338 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) { |
| 339 | err = fscrypt_setup_iv_ino_lblk_32_key(ci, mk); |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 340 | } else { |
| 341 | u8 derived_key[FSCRYPT_MAX_KEY_SIZE]; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 342 | |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 343 | err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, |
Eric Biggers | f592efe | 2020-01-20 14:31:58 -0800 | [diff] [blame] | 344 | HKDF_CONTEXT_PER_FILE_ENC_KEY, |
Eric Biggers | 1d6217a4 | 2020-07-08 14:57:22 -0700 | [diff] [blame] | 345 | ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE, |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 346 | derived_key, ci->ci_mode->keysize); |
| 347 | if (err) |
| 348 | return err; |
| 349 | |
Eric Biggers | f592efe | 2020-01-20 14:31:58 -0800 | [diff] [blame] | 350 | err = fscrypt_set_per_file_enc_key(ci, derived_key); |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 351 | memzero_explicit(derived_key, ci->ci_mode->keysize); |
| 352 | } |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 353 | if (err) |
| 354 | return err; |
| 355 | |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 356 | /* Derive a secret dirhash key for directories that need it. */ |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 357 | if (need_dirhash_key) { |
Daniel Rosenberg | aa408f8 | 2020-01-20 14:31:57 -0800 | [diff] [blame] | 358 | err = fscrypt_derive_dirhash_key(ci, mk); |
| 359 | if (err) |
| 360 | return err; |
| 361 | } |
| 362 | |
| 363 | return 0; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 366 | /* |
Eric Biggers | 7f595d6 | 2021-09-20 20:03:03 -0700 | [diff] [blame] | 367 | * Check whether the size of the given master key (@mk) is appropriate for the |
| 368 | * encryption settings which a particular file will use (@ci). |
| 369 | * |
| 370 | * If the file uses a v1 encryption policy, then the master key must be at least |
| 371 | * as long as the derived key, as this is a requirement of the v1 KDF. |
| 372 | * |
| 373 | * Otherwise, the KDF can accept any size key, so we enforce a slightly looser |
| 374 | * requirement: we require that the size of the master key be at least the |
| 375 | * maximum security strength of any algorithm whose key will be derived from it |
| 376 | * (but in practice we only need to consider @ci->ci_mode, since any other |
| 377 | * possible subkeys such as DIRHASH and INODE_HASH will never increase the |
| 378 | * required key size over @ci->ci_mode). This allows AES-256-XTS keys to be |
| 379 | * derived from a 256-bit master key, which is cryptographically sufficient, |
| 380 | * rather than requiring a 512-bit master key which is unnecessarily long. (We |
| 381 | * still allow 512-bit master keys if the user chooses to use them, though.) |
| 382 | */ |
| 383 | static bool fscrypt_valid_master_key_size(const struct fscrypt_master_key *mk, |
| 384 | const struct fscrypt_info *ci) |
| 385 | { |
| 386 | unsigned int min_keysize; |
| 387 | |
| 388 | if (ci->ci_policy.version == FSCRYPT_POLICY_V1) |
| 389 | min_keysize = ci->ci_mode->keysize; |
| 390 | else |
| 391 | min_keysize = ci->ci_mode->security_strength; |
| 392 | |
| 393 | if (mk->mk_secret.size < min_keysize) { |
| 394 | fscrypt_warn(NULL, |
| 395 | "key with %s %*phN is too short (got %u bytes, need %u+ bytes)", |
| 396 | master_key_spec_type(&mk->mk_spec), |
| 397 | master_key_spec_len(&mk->mk_spec), |
| 398 | (u8 *)&mk->mk_spec.u, |
| 399 | mk->mk_secret.size, min_keysize); |
| 400 | return false; |
| 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | /* |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 406 | * 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] | 407 | * |
| 408 | * If the master key is found in the filesystem-level keyring, then the |
Eric Biggers | 4a4b872 | 2020-11-16 19:26:26 -0800 | [diff] [blame] | 409 | * corresponding 'struct key' is returned in *master_key_ret with its semaphore |
| 410 | * read-locked. This is needed to ensure that only one task links the |
| 411 | * fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race to create |
| 412 | * an fscrypt_info for the same inode), and to synchronize the master key being |
| 413 | * removed with a new inode starting to use it. |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 414 | */ |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 415 | static int setup_file_encryption_key(struct fscrypt_info *ci, |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 416 | bool need_dirhash_key, |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 417 | struct key **master_key_ret) |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 418 | { |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 419 | struct key *key; |
| 420 | struct fscrypt_master_key *mk = NULL; |
| 421 | struct fscrypt_key_specifier mk_spec; |
| 422 | int err; |
| 423 | |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 424 | err = fscrypt_select_encryption_impl(ci); |
| 425 | if (err) |
| 426 | return err; |
| 427 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 428 | switch (ci->ci_policy.version) { |
| 429 | case FSCRYPT_POLICY_V1: |
| 430 | mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; |
| 431 | memcpy(mk_spec.u.descriptor, |
| 432 | ci->ci_policy.v1.master_key_descriptor, |
| 433 | FSCRYPT_KEY_DESCRIPTOR_SIZE); |
| 434 | break; |
| 435 | case FSCRYPT_POLICY_V2: |
| 436 | mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; |
| 437 | memcpy(mk_spec.u.identifier, |
| 438 | ci->ci_policy.v2.master_key_identifier, |
| 439 | FSCRYPT_KEY_IDENTIFIER_SIZE); |
| 440 | break; |
| 441 | default: |
| 442 | WARN_ON(1); |
| 443 | return -EINVAL; |
| 444 | } |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 445 | |
| 446 | key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec); |
| 447 | if (IS_ERR(key)) { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 448 | if (key != ERR_PTR(-ENOKEY) || |
| 449 | ci->ci_policy.version != FSCRYPT_POLICY_V1) |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 450 | return PTR_ERR(key); |
| 451 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 452 | /* |
| 453 | * As a legacy fallback for v1 policies, search for the key in |
| 454 | * the current task's subscribed keyrings too. Don't move this |
| 455 | * to before the search of ->s_master_keys, since users |
| 456 | * shouldn't be able to override filesystem-level keys. |
| 457 | */ |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 458 | return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci); |
| 459 | } |
| 460 | |
| 461 | mk = key->payload.data[0]; |
Eric Biggers | 4a4b872 | 2020-11-16 19:26:26 -0800 | [diff] [blame] | 462 | down_read(&key->sem); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 463 | |
| 464 | /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */ |
| 465 | if (!is_master_key_secret_present(&mk->mk_secret)) { |
| 466 | err = -ENOKEY; |
| 467 | goto out_release_key; |
| 468 | } |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 469 | |
Eric Biggers | 7f595d6 | 2021-09-20 20:03:03 -0700 | [diff] [blame] | 470 | if (!fscrypt_valid_master_key_size(mk, ci)) { |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 471 | err = -ENOKEY; |
| 472 | goto out_release_key; |
| 473 | } |
| 474 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 475 | switch (ci->ci_policy.version) { |
| 476 | case FSCRYPT_POLICY_V1: |
| 477 | err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw); |
| 478 | break; |
| 479 | case FSCRYPT_POLICY_V2: |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 480 | err = fscrypt_setup_v2_file_key(ci, mk, need_dirhash_key); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 481 | break; |
| 482 | default: |
| 483 | WARN_ON(1); |
| 484 | err = -EINVAL; |
| 485 | break; |
| 486 | } |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 487 | if (err) |
| 488 | goto out_release_key; |
| 489 | |
| 490 | *master_key_ret = key; |
| 491 | return 0; |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 492 | |
| 493 | out_release_key: |
Eric Biggers | 4a4b872 | 2020-11-16 19:26:26 -0800 | [diff] [blame] | 494 | up_read(&key->sem); |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 495 | key_put(key); |
| 496 | return err; |
Eric Biggers | 3ec4f2a6 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 499 | static void put_crypt_info(struct fscrypt_info *ci) |
| 500 | { |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 501 | struct key *key; |
| 502 | |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 503 | if (!ci) |
| 504 | return; |
| 505 | |
Eric Biggers | 4006d79 | 2019-10-09 16:34:16 -0700 | [diff] [blame] | 506 | if (ci->ci_direct_key) |
Eric Biggers | 0109ce76 | 2019-08-04 19:35:45 -0700 | [diff] [blame] | 507 | fscrypt_put_direct_key(ci->ci_direct_key); |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 508 | else if (ci->ci_owns_key) |
Satya Tangirala | 5fee360 | 2020-07-02 01:56:05 +0000 | [diff] [blame] | 509 | fscrypt_destroy_prepared_key(&ci->ci_enc_key); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 510 | |
| 511 | key = ci->ci_master_key; |
| 512 | if (key) { |
| 513 | struct fscrypt_master_key *mk = key->payload.data[0]; |
| 514 | |
| 515 | /* |
| 516 | * Remove this inode from the list of inodes that were unlocked |
| 517 | * with the master key. |
| 518 | * |
| 519 | * In addition, if we're removing the last inode from a key that |
| 520 | * already had its secret removed, invalidate the key so that it |
| 521 | * gets removed from ->s_master_keys. |
| 522 | */ |
| 523 | spin_lock(&mk->mk_decrypted_inodes_lock); |
| 524 | list_del(&ci->ci_master_key_link); |
| 525 | spin_unlock(&mk->mk_decrypted_inodes_lock); |
| 526 | if (refcount_dec_and_test(&mk->mk_refcount)) |
| 527 | key_invalidate(key); |
| 528 | key_put(key); |
| 529 | } |
Eric Biggers | 6f99756 | 2019-10-09 16:34:17 -0700 | [diff] [blame] | 530 | memzero_explicit(ci, sizeof(*ci)); |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 531 | kmem_cache_free(fscrypt_info_cachep, ci); |
| 532 | } |
| 533 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 534 | static int |
| 535 | fscrypt_setup_encryption_info(struct inode *inode, |
| 536 | const union fscrypt_policy *policy, |
| 537 | const u8 nonce[FSCRYPT_FILE_NONCE_SIZE], |
| 538 | bool need_dirhash_key) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 539 | { |
| 540 | struct fscrypt_info *crypt_info; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 541 | struct fscrypt_mode *mode; |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 542 | struct key *master_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 543 | int res; |
| 544 | |
David Gstir | f32d7ac | 2016-12-06 23:53:57 +0100 | [diff] [blame] | 545 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 546 | if (res) |
| 547 | return res; |
| 548 | |
Eric Biggers | 9dad5fe | 2020-09-16 21:11:32 -0700 | [diff] [blame] | 549 | crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_KERNEL); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 550 | if (!crypt_info) |
| 551 | return -ENOMEM; |
| 552 | |
Eric Biggers | 59dc6a8 | 2019-08-04 19:35:44 -0700 | [diff] [blame] | 553 | crypt_info->ci_inode = inode; |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 554 | crypt_info->ci_policy = *policy; |
| 555 | memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 556 | |
| 557 | mode = select_encryption_mode(&crypt_info->ci_policy, inode); |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 558 | if (IS_ERR(mode)) { |
| 559 | res = PTR_ERR(mode); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 560 | goto out; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 561 | } |
Eric Biggers | 8094c3c | 2019-01-06 08:36:21 -0500 | [diff] [blame] | 562 | WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE); |
| 563 | crypt_info->ci_mode = mode; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 564 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 565 | res = setup_file_encryption_key(crypt_info, need_dirhash_key, |
| 566 | &master_key); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 567 | if (res) |
| 568 | goto out; |
| 569 | |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 570 | /* |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 571 | * For existing inodes, multiple tasks may race to set ->i_crypt_info. |
| 572 | * So use cmpxchg_release(). This pairs with the smp_load_acquire() in |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 573 | * fscrypt_get_info(). I.e., here we publish ->i_crypt_info with a |
| 574 | * RELEASE barrier so that other tasks can ACQUIRE it. |
| 575 | */ |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 576 | if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) { |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 577 | /* |
| 578 | * We won the race and set ->i_crypt_info to our crypt_info. |
| 579 | * Now link it into the master key's inode list. |
| 580 | */ |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 581 | if (master_key) { |
| 582 | struct fscrypt_master_key *mk = |
| 583 | master_key->payload.data[0]; |
| 584 | |
| 585 | refcount_inc(&mk->mk_refcount); |
| 586 | crypt_info->ci_master_key = key_get(master_key); |
| 587 | spin_lock(&mk->mk_decrypted_inodes_lock); |
| 588 | list_add(&crypt_info->ci_master_key_link, |
| 589 | &mk->mk_decrypted_inodes); |
| 590 | spin_unlock(&mk->mk_decrypted_inodes_lock); |
| 591 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 592 | crypt_info = NULL; |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 593 | } |
| 594 | res = 0; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 595 | out: |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 596 | if (master_key) { |
Eric Biggers | 4a4b872 | 2020-11-16 19:26:26 -0800 | [diff] [blame] | 597 | up_read(&master_key->sem); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 598 | key_put(master_key); |
| 599 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 600 | put_crypt_info(crypt_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 601 | return res; |
| 602 | } |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 603 | |
| 604 | /** |
| 605 | * fscrypt_get_encryption_info() - set up an inode's encryption key |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 606 | * @inode: the inode to set up the key for. Must be encrypted. |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 607 | * @allow_unsupported: if %true, treat an unsupported encryption policy (or |
| 608 | * unrecognized encryption context) the same way as the key |
| 609 | * being unavailable, instead of returning an error. Use |
| 610 | * %false unless the operation being performed is needed in |
| 611 | * order for files (or directories) to be deleted. |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 612 | * |
| 613 | * Set up ->i_crypt_info, if it hasn't already been done. |
| 614 | * |
| 615 | * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe. So |
| 616 | * generally this shouldn't be called from within a filesystem transaction. |
| 617 | * |
| 618 | * Return: 0 if ->i_crypt_info was set or was already set, *or* if the |
| 619 | * encryption key is unavailable. (Use fscrypt_has_encryption_key() to |
| 620 | * distinguish these cases.) Also can return another -errno code. |
| 621 | */ |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 622 | int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported) |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 623 | { |
| 624 | int res; |
| 625 | union fscrypt_context ctx; |
| 626 | union fscrypt_policy policy; |
| 627 | |
| 628 | if (fscrypt_has_encryption_key(inode)) |
| 629 | return 0; |
| 630 | |
| 631 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 632 | if (res < 0) { |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 633 | if (res == -ERANGE && allow_unsupported) |
| 634 | return 0; |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 635 | fscrypt_warn(inode, "Error %d getting encryption context", res); |
| 636 | return res; |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | res = fscrypt_policy_from_context(&policy, &ctx, res); |
| 640 | if (res) { |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 641 | if (allow_unsupported) |
| 642 | return 0; |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 643 | fscrypt_warn(inode, |
| 644 | "Unrecognized or corrupt encryption context"); |
| 645 | return res; |
| 646 | } |
| 647 | |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 648 | if (!fscrypt_supported_policy(&policy, inode)) { |
| 649 | if (allow_unsupported) |
| 650 | return 0; |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 651 | return -EINVAL; |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 652 | } |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 653 | |
| 654 | res = fscrypt_setup_encryption_info(inode, &policy, |
| 655 | fscrypt_context_nonce(&ctx), |
| 656 | IS_CASEFOLDED(inode) && |
| 657 | S_ISDIR(inode->i_mode)); |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 658 | |
| 659 | if (res == -ENOPKG && allow_unsupported) /* Algorithm unavailable? */ |
| 660 | res = 0; |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 661 | if (res == -ENOKEY) |
| 662 | res = 0; |
| 663 | return res; |
| 664 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 665 | |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 666 | /** |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 667 | * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory |
| 668 | * @dir: a possibly-encrypted directory |
| 669 | * @inode: the new inode. ->i_mode must be set already. |
| 670 | * ->i_ino doesn't need to be set yet. |
| 671 | * @encrypt_ret: (output) set to %true if the new inode will be encrypted |
| 672 | * |
| 673 | * If the directory is encrypted, set up its ->i_crypt_info in preparation for |
| 674 | * encrypting the name of the new file. Also, if the new inode will be |
| 675 | * encrypted, set up its ->i_crypt_info and set *encrypt_ret=true. |
| 676 | * |
| 677 | * This isn't %GFP_NOFS-safe, and therefore it should be called before starting |
| 678 | * any filesystem transaction to create the inode. For this reason, ->i_ino |
| 679 | * isn't required to be set yet, as the filesystem may not have set it yet. |
| 680 | * |
| 681 | * This doesn't persist the new inode's encryption context. That still needs to |
| 682 | * be done later by calling fscrypt_set_context(). |
| 683 | * |
| 684 | * Return: 0 on success, -ENOKEY if the encryption key is missing, or another |
| 685 | * -errno code |
| 686 | */ |
| 687 | int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode, |
| 688 | bool *encrypt_ret) |
| 689 | { |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 690 | const union fscrypt_policy *policy; |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 691 | u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; |
| 692 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 693 | policy = fscrypt_policy_to_inherit(dir); |
| 694 | if (policy == NULL) |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 695 | return 0; |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 696 | if (IS_ERR(policy)) |
| 697 | return PTR_ERR(policy); |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 698 | |
| 699 | if (WARN_ON_ONCE(inode->i_mode == 0)) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | /* |
| 703 | * Only regular files, directories, and symlinks are encrypted. |
| 704 | * Special files like device nodes and named pipes aren't. |
| 705 | */ |
| 706 | if (!S_ISREG(inode->i_mode) && |
| 707 | !S_ISDIR(inode->i_mode) && |
| 708 | !S_ISLNK(inode->i_mode)) |
| 709 | return 0; |
| 710 | |
| 711 | *encrypt_ret = true; |
| 712 | |
| 713 | get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE); |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 714 | return fscrypt_setup_encryption_info(inode, policy, nonce, |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 715 | IS_CASEFOLDED(dir) && |
| 716 | S_ISDIR(inode->i_mode)); |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode); |
| 719 | |
| 720 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 721 | * fscrypt_put_encryption_info() - free most of an inode's fscrypt data |
| 722 | * @inode: an inode being evicted |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 723 | * |
| 724 | * Free the inode's fscrypt_info. Filesystems must call this when the inode is |
| 725 | * being evicted. An RCU grace period need not have elapsed yet. |
| 726 | */ |
Eric Biggers | 3d204e2 | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 727 | void fscrypt_put_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 728 | { |
Eric Biggers | 3d204e2 | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 729 | put_crypt_info(inode->i_crypt_info); |
| 730 | inode->i_crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 731 | } |
| 732 | EXPORT_SYMBOL(fscrypt_put_encryption_info); |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 733 | |
| 734 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 735 | * fscrypt_free_inode() - free an inode's fscrypt data requiring RCU delay |
| 736 | * @inode: an inode being freed |
Eric Biggers | 2c58d54 | 2019-04-10 13:21:15 -0700 | [diff] [blame] | 737 | * |
| 738 | * Free the inode's cached decrypted symlink target, if any. Filesystems must |
| 739 | * call this after an RCU grace period, just before they free the inode. |
| 740 | */ |
| 741 | void fscrypt_free_inode(struct inode *inode) |
| 742 | { |
| 743 | if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) { |
| 744 | kfree(inode->i_link); |
| 745 | inode->i_link = NULL; |
| 746 | } |
| 747 | } |
| 748 | EXPORT_SYMBOL(fscrypt_free_inode); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 749 | |
| 750 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 751 | * fscrypt_drop_inode() - check whether the inode's master key has been removed |
| 752 | * @inode: an inode being considered for eviction |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 753 | * |
| 754 | * Filesystems supporting fscrypt must call this from their ->drop_inode() |
| 755 | * method so that encrypted inodes are evicted as soon as they're no longer in |
| 756 | * use and their master key has been removed. |
| 757 | * |
| 758 | * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0 |
| 759 | */ |
| 760 | int fscrypt_drop_inode(struct inode *inode) |
| 761 | { |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 762 | const struct fscrypt_info *ci = fscrypt_get_info(inode); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 763 | const struct fscrypt_master_key *mk; |
| 764 | |
| 765 | /* |
| 766 | * If ci is NULL, then the inode doesn't have an encryption key set up |
| 767 | * so it's irrelevant. If ci_master_key is NULL, then the master key |
| 768 | * was provided via the legacy mechanism of the process-subscribed |
| 769 | * keyrings, so we don't know whether it's been removed or not. |
| 770 | */ |
| 771 | if (!ci || !ci->ci_master_key) |
| 772 | return 0; |
| 773 | mk = ci->ci_master_key->payload.data[0]; |
| 774 | |
| 775 | /* |
Eric Biggers | 2b4eae9 | 2020-03-05 00:41:38 -0800 | [diff] [blame] | 776 | * With proper, non-racy use of FS_IOC_REMOVE_ENCRYPTION_KEY, all inodes |
| 777 | * protected by the key were cleaned by sync_filesystem(). But if |
| 778 | * userspace is still using the files, inodes can be dirtied between |
| 779 | * then and now. We mustn't lose any writes, so skip dirty inodes here. |
| 780 | */ |
| 781 | if (inode->i_state & I_DIRTY_ALL) |
| 782 | return 0; |
| 783 | |
| 784 | /* |
Eric Biggers | 4a4b872 | 2020-11-16 19:26:26 -0800 | [diff] [blame] | 785 | * Note: since we aren't holding the key semaphore, the result here can |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 786 | * immediately become outdated. But there's no correctness problem with |
| 787 | * unnecessarily evicting. Nor is there a correctness problem with not |
| 788 | * evicting while iput() is racing with the key being removed, since |
| 789 | * then the thread removing the key will either evict the inode itself |
| 790 | * or will correctly detect that it wasn't evicted due to the race. |
| 791 | */ |
| 792 | return !is_master_key_secret_present(&mk->mk_secret); |
| 793 | } |
| 794 | EXPORT_SYMBOL_GPL(fscrypt_drop_inode); |