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 | /* |
| 3 | * key management facility for FS encryption support. |
| 4 | * |
| 5 | * Copyright (C) 2015, Google, Inc. |
| 6 | * |
| 7 | * This contains encryption key functions. |
| 8 | * |
| 9 | * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. |
| 10 | */ |
| 11 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 12 | #include <keys/user-type.h> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 13 | #include <linux/scatterlist.h> |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 14 | #include <linux/ratelimit.h> |
| 15 | #include <crypto/aes.h> |
| 16 | #include <crypto/sha.h> |
Eric Biggers | a575784 | 2018-01-05 10:45:00 -0800 | [diff] [blame] | 17 | #include <crypto/skcipher.h> |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 18 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 19 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 20 | static struct crypto_shash *essiv_hash_tfm; |
| 21 | |
Eric Biggers | 646b7d4 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 22 | /* |
| 23 | * Key derivation function. This generates the derived key by encrypting the |
| 24 | * master key with AES-128-ECB using the inode's nonce as the AES key. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 25 | * |
Eric Biggers | 646b7d4 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 26 | * The master key must be at least as long as the derived key. If the master |
| 27 | * key is longer, then only the first 'derived_keysize' bytes are used. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 28 | */ |
Eric Biggers | 646b7d4 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 29 | static int derive_key_aes(const u8 *master_key, |
| 30 | const struct fscrypt_context *ctx, |
| 31 | u8 *derived_key, unsigned int derived_keysize) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 32 | { |
| 33 | int res = 0; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 34 | struct skcipher_request *req = NULL; |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 35 | DECLARE_CRYPTO_WAIT(wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 36 | struct scatterlist src_sg, dst_sg; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 37 | struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 38 | |
| 39 | if (IS_ERR(tfm)) { |
| 40 | res = PTR_ERR(tfm); |
| 41 | tfm = NULL; |
| 42 | goto out; |
| 43 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 44 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 45 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 46 | if (!req) { |
| 47 | res = -ENOMEM; |
| 48 | goto out; |
| 49 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 50 | skcipher_request_set_callback(req, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 51 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 52 | crypto_req_done, &wait); |
Eric Biggers | 646b7d4 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 53 | res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 54 | if (res < 0) |
| 55 | goto out; |
| 56 | |
Eric Biggers | 646b7d4 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 57 | sg_init_one(&src_sg, master_key, derived_keysize); |
| 58 | sg_init_one(&dst_sg, derived_key, derived_keysize); |
| 59 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize, |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 60 | NULL); |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 61 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 62 | out: |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 63 | skcipher_request_free(req); |
| 64 | crypto_free_skcipher(tfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 65 | return res; |
| 66 | } |
| 67 | |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 68 | /* |
| 69 | * Search the current task's subscribed keyrings for a "logon" key with |
| 70 | * description prefix:descriptor, and if found acquire a read lock on it and |
| 71 | * return a pointer to its validated payload in *payload_ret. |
| 72 | */ |
| 73 | static struct key * |
| 74 | find_and_lock_process_key(const char *prefix, |
| 75 | const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE], |
| 76 | unsigned int min_keysize, |
| 77 | const struct fscrypt_key **payload_ret) |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 78 | { |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 79 | char *description; |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 80 | struct key *key; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 81 | const struct user_key_payload *ukp; |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 82 | const struct fscrypt_key *payload; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 83 | |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 84 | description = kasprintf(GFP_NOFS, "%s%*phN", prefix, |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 85 | FS_KEY_DESCRIPTOR_SIZE, descriptor); |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 86 | if (!description) |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 87 | return ERR_PTR(-ENOMEM); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 88 | |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 89 | key = request_key(&key_type_logon, description, NULL); |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 90 | kfree(description); |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 91 | if (IS_ERR(key)) |
| 92 | return key; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 93 | |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 94 | down_read(&key->sem); |
| 95 | ukp = user_key_payload_locked(key); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 96 | |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 97 | if (!ukp) /* was the key revoked before we acquired its semaphore? */ |
| 98 | goto invalid; |
| 99 | |
| 100 | payload = (const struct fscrypt_key *)ukp->data; |
| 101 | |
| 102 | if (ukp->datalen != sizeof(struct fscrypt_key) || |
| 103 | payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) { |
| 104 | fscrypt_warn(NULL, |
| 105 | "key with description '%s' has invalid payload", |
| 106 | key->description); |
| 107 | goto invalid; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 108 | } |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 109 | |
Eric Biggers | 646b7d4 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 110 | if (payload->size < min_keysize) { |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 111 | fscrypt_warn(NULL, |
Eric Biggers | 646b7d4 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 112 | "key with description '%s' is too short (got %u bytes, need %u+ bytes)", |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 113 | key->description, payload->size, min_keysize); |
| 114 | goto invalid; |
| 115 | } |
| 116 | |
| 117 | *payload_ret = payload; |
| 118 | return key; |
| 119 | |
| 120 | invalid: |
| 121 | up_read(&key->sem); |
| 122 | key_put(key); |
| 123 | return ERR_PTR(-ENOKEY); |
| 124 | } |
| 125 | |
| 126 | /* Find the master key, then derive the inode's actual encryption key */ |
| 127 | static int find_and_derive_key(const struct inode *inode, |
| 128 | const struct fscrypt_context *ctx, |
| 129 | u8 *derived_key, unsigned int derived_keysize) |
| 130 | { |
| 131 | struct key *key; |
| 132 | const struct fscrypt_key *payload; |
| 133 | int err; |
| 134 | |
| 135 | key = find_and_lock_process_key(FS_KEY_DESC_PREFIX, |
| 136 | ctx->master_key_descriptor, |
| 137 | derived_keysize, &payload); |
| 138 | if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) { |
| 139 | key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix, |
| 140 | ctx->master_key_descriptor, |
| 141 | derived_keysize, &payload); |
| 142 | } |
| 143 | if (IS_ERR(key)) |
| 144 | return PTR_ERR(key); |
Eric Biggers | 646b7d4 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 145 | err = derive_key_aes(payload->raw, ctx, derived_key, derived_keysize); |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 146 | up_read(&key->sem); |
| 147 | key_put(key); |
| 148 | return err; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 151 | static struct fscrypt_mode { |
| 152 | const char *friendly_name; |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 153 | const char *cipher_str; |
| 154 | int keysize; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 155 | bool logged_impl_name; |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 156 | } available_modes[] = { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 157 | [FS_ENCRYPTION_MODE_AES_256_XTS] = { |
| 158 | .friendly_name = "AES-256-XTS", |
| 159 | .cipher_str = "xts(aes)", |
| 160 | .keysize = 64, |
| 161 | }, |
| 162 | [FS_ENCRYPTION_MODE_AES_256_CTS] = { |
| 163 | .friendly_name = "AES-256-CTS-CBC", |
| 164 | .cipher_str = "cts(cbc(aes))", |
| 165 | .keysize = 32, |
| 166 | }, |
| 167 | [FS_ENCRYPTION_MODE_AES_128_CBC] = { |
| 168 | .friendly_name = "AES-128-CBC", |
| 169 | .cipher_str = "cbc(aes)", |
| 170 | .keysize = 16, |
| 171 | }, |
| 172 | [FS_ENCRYPTION_MODE_AES_128_CTS] = { |
| 173 | .friendly_name = "AES-128-CTS-CBC", |
| 174 | .cipher_str = "cts(cbc(aes))", |
| 175 | .keysize = 16, |
| 176 | }, |
| 177 | [FS_ENCRYPTION_MODE_SPECK128_256_XTS] = { |
| 178 | .friendly_name = "Speck128/256-XTS", |
| 179 | .cipher_str = "xts(speck128)", |
| 180 | .keysize = 64, |
| 181 | }, |
| 182 | [FS_ENCRYPTION_MODE_SPECK128_256_CTS] = { |
| 183 | .friendly_name = "Speck128/256-CTS-CBC", |
| 184 | .cipher_str = "cts(cbc(speck128))", |
| 185 | .keysize = 32, |
| 186 | }, |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 187 | }; |
| 188 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 189 | static struct fscrypt_mode * |
| 190 | select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode) |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 191 | { |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 192 | if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) { |
Eric Biggers | 544d08f | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 193 | fscrypt_warn(inode->i_sb, |
| 194 | "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)", |
| 195 | inode->i_ino, ci->ci_data_mode, |
| 196 | ci->ci_filename_mode); |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 197 | return ERR_PTR(-EINVAL); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 200 | if (S_ISREG(inode->i_mode)) |
| 201 | return &available_modes[ci->ci_data_mode]; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 202 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 203 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 204 | return &available_modes[ci->ci_filename_mode]; |
| 205 | |
| 206 | WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", |
| 207 | inode->i_ino, (inode->i_mode & S_IFMT)); |
| 208 | return ERR_PTR(-EINVAL); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 211 | static void put_crypt_info(struct fscrypt_info *ci) |
| 212 | { |
| 213 | if (!ci) |
| 214 | return; |
| 215 | |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 216 | crypto_free_skcipher(ci->ci_ctfm); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 217 | crypto_free_cipher(ci->ci_essiv_tfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 218 | kmem_cache_free(fscrypt_info_cachep, ci); |
| 219 | } |
| 220 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 221 | static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) |
| 222 | { |
| 223 | struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm); |
| 224 | |
| 225 | /* init hash transform on demand */ |
| 226 | if (unlikely(!tfm)) { |
| 227 | struct crypto_shash *prev_tfm; |
| 228 | |
| 229 | tfm = crypto_alloc_shash("sha256", 0, 0); |
| 230 | if (IS_ERR(tfm)) { |
Eric Biggers | 544d08f | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 231 | fscrypt_warn(NULL, |
| 232 | "error allocating SHA-256 transform: %ld", |
| 233 | PTR_ERR(tfm)); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 234 | return PTR_ERR(tfm); |
| 235 | } |
| 236 | prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm); |
| 237 | if (prev_tfm) { |
| 238 | crypto_free_shash(tfm); |
| 239 | tfm = prev_tfm; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | { |
| 244 | SHASH_DESC_ON_STACK(desc, tfm); |
| 245 | desc->tfm = tfm; |
| 246 | desc->flags = 0; |
| 247 | |
| 248 | return crypto_shash_digest(desc, key, keysize, salt); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key, |
| 253 | int keysize) |
| 254 | { |
| 255 | int err; |
| 256 | struct crypto_cipher *essiv_tfm; |
| 257 | u8 salt[SHA256_DIGEST_SIZE]; |
| 258 | |
| 259 | essiv_tfm = crypto_alloc_cipher("aes", 0, 0); |
| 260 | if (IS_ERR(essiv_tfm)) |
| 261 | return PTR_ERR(essiv_tfm); |
| 262 | |
| 263 | ci->ci_essiv_tfm = essiv_tfm; |
| 264 | |
| 265 | err = derive_essiv_salt(raw_key, keysize, salt); |
| 266 | if (err) |
| 267 | goto out; |
| 268 | |
| 269 | /* |
| 270 | * Using SHA256 to derive the salt/key will result in AES-256 being |
| 271 | * used for IV generation. File contents encryption will still use the |
| 272 | * configured keysize (AES-128) nevertheless. |
| 273 | */ |
| 274 | err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt)); |
| 275 | if (err) |
| 276 | goto out; |
| 277 | |
| 278 | out: |
| 279 | memzero_explicit(salt, sizeof(salt)); |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | void __exit fscrypt_essiv_cleanup(void) |
| 284 | { |
| 285 | crypto_free_shash(essiv_hash_tfm); |
| 286 | } |
| 287 | |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 288 | int fscrypt_get_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 289 | { |
| 290 | struct fscrypt_info *crypt_info; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 291 | struct fscrypt_context ctx; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 292 | struct crypto_skcipher *ctfm; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 293 | struct fscrypt_mode *mode; |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 294 | u8 *raw_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 295 | int res; |
| 296 | |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 297 | if (inode->i_crypt_info) |
| 298 | return 0; |
| 299 | |
David Gstir | f32d7ac | 2016-12-06 23:53:57 +0100 | [diff] [blame] | 300 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 301 | if (res) |
| 302 | return res; |
| 303 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 304 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 305 | if (res < 0) { |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 306 | if (!fscrypt_dummy_context_enabled(inode) || |
Eric Biggers | e0428a2 | 2017-10-09 12:15:36 -0700 | [diff] [blame] | 307 | IS_ENCRYPTED(inode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 308 | return res; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 309 | /* Fake up a context for an unencrypted directory */ |
| 310 | memset(&ctx, 0, sizeof(ctx)); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 311 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 312 | ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 313 | ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 314 | memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 315 | } else if (res != sizeof(ctx)) { |
| 316 | return -EINVAL; |
| 317 | } |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 318 | |
| 319 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 320 | return -EINVAL; |
| 321 | |
| 322 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) |
| 323 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 324 | |
| 325 | crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS); |
| 326 | if (!crypt_info) |
| 327 | return -ENOMEM; |
| 328 | |
| 329 | crypt_info->ci_flags = ctx.flags; |
| 330 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 331 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
| 332 | crypt_info->ci_ctfm = NULL; |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 333 | crypt_info->ci_essiv_tfm = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 334 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, |
| 335 | sizeof(crypt_info->ci_master_key)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 336 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 337 | mode = select_encryption_mode(crypt_info, inode); |
| 338 | if (IS_ERR(mode)) { |
| 339 | res = PTR_ERR(mode); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 340 | goto out; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 341 | } |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 342 | |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 343 | /* |
| 344 | * This cannot be a stack buffer because it is passed to the scatterlist |
| 345 | * crypto API as part of key derivation. |
| 346 | */ |
| 347 | res = -ENOMEM; |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 348 | raw_key = kmalloc(mode->keysize, GFP_NOFS); |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 349 | if (!raw_key) |
| 350 | goto out; |
| 351 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 352 | res = find_and_derive_key(inode, &ctx, raw_key, mode->keysize); |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 353 | if (res) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 354 | goto out; |
Eric Biggers | 590f497 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 355 | |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 356 | ctfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); |
Eric Biggers | 9919479 | 2018-04-30 15:51:37 -0700 | [diff] [blame] | 357 | if (IS_ERR(ctfm)) { |
| 358 | res = PTR_ERR(ctfm); |
Eric Biggers | 544d08f | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 359 | fscrypt_warn(inode->i_sb, |
| 360 | "error allocating '%s' transform for inode %lu: %d", |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 361 | mode->cipher_str, inode->i_ino, res); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 362 | goto out; |
| 363 | } |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 364 | if (unlikely(!mode->logged_impl_name)) { |
| 365 | /* |
| 366 | * fscrypt performance can vary greatly depending on which |
| 367 | * crypto algorithm implementation is used. Help people debug |
| 368 | * performance problems by logging the ->cra_driver_name the |
| 369 | * first time a mode is used. Note that multiple threads can |
| 370 | * race here, but it doesn't really matter. |
| 371 | */ |
| 372 | mode->logged_impl_name = true; |
| 373 | pr_info("fscrypt: %s using implementation \"%s\"\n", |
| 374 | mode->friendly_name, |
| 375 | crypto_skcipher_alg(ctfm)->base.cra_driver_name); |
| 376 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 377 | crypt_info->ci_ctfm = ctfm; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 378 | crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY); |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 379 | res = crypto_skcipher_setkey(ctfm, raw_key, mode->keysize); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 380 | if (res) |
| 381 | goto out; |
| 382 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 383 | if (S_ISREG(inode->i_mode) && |
| 384 | crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) { |
Eric Biggers | e1cc40e | 2018-05-18 10:58:14 -0700 | [diff] [blame^] | 385 | res = init_essiv_generator(crypt_info, raw_key, mode->keysize); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 386 | if (res) { |
Eric Biggers | 544d08f | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 387 | fscrypt_warn(inode->i_sb, |
| 388 | "error initializing ESSIV generator for inode %lu: %d", |
| 389 | inode->i_ino, res); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 390 | goto out; |
| 391 | } |
| 392 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 393 | if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL) |
| 394 | crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 395 | out: |
| 396 | if (res == -ENOKEY) |
| 397 | res = 0; |
| 398 | put_crypt_info(crypt_info); |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 399 | kzfree(raw_key); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 400 | return res; |
| 401 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 402 | EXPORT_SYMBOL(fscrypt_get_encryption_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 403 | |
Eric Biggers | 3d204e2 | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 404 | void fscrypt_put_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 405 | { |
Eric Biggers | 3d204e2 | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 406 | put_crypt_info(inode->i_crypt_info); |
| 407 | inode->i_crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 408 | } |
| 409 | EXPORT_SYMBOL(fscrypt_put_encryption_info); |