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> |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 17 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 18 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 19 | static struct crypto_shash *essiv_hash_tfm; |
| 20 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 21 | static void derive_crypt_complete(struct crypto_async_request *req, int rc) |
| 22 | { |
| 23 | struct fscrypt_completion_result *ecr = req->data; |
| 24 | |
| 25 | if (rc == -EINPROGRESS) |
| 26 | return; |
| 27 | |
| 28 | ecr->res = rc; |
| 29 | complete(&ecr->completion); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * derive_key_aes() - Derive a key using AES-128-ECB |
| 34 | * @deriving_key: Encryption key used for derivation. |
| 35 | * @source_key: Source key to which to apply derivation. |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 36 | * @derived_raw_key: Derived raw key. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 37 | * |
| 38 | * Return: Zero on success; non-zero otherwise. |
| 39 | */ |
| 40 | static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE], |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 41 | const struct fscrypt_key *source_key, |
| 42 | u8 derived_raw_key[FS_MAX_KEY_SIZE]) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 43 | { |
| 44 | int res = 0; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 45 | struct skcipher_request *req = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 46 | DECLARE_FS_COMPLETION_RESULT(ecr); |
| 47 | struct scatterlist src_sg, dst_sg; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 48 | struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 49 | |
| 50 | if (IS_ERR(tfm)) { |
| 51 | res = PTR_ERR(tfm); |
| 52 | tfm = NULL; |
| 53 | goto out; |
| 54 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 55 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 56 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 57 | if (!req) { |
| 58 | res = -ENOMEM; |
| 59 | goto out; |
| 60 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 61 | skcipher_request_set_callback(req, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 62 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 63 | derive_crypt_complete, &ecr); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 64 | res = crypto_skcipher_setkey(tfm, deriving_key, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 65 | FS_AES_128_ECB_KEY_SIZE); |
| 66 | if (res < 0) |
| 67 | goto out; |
| 68 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 69 | sg_init_one(&src_sg, source_key->raw, source_key->size); |
| 70 | sg_init_one(&dst_sg, derived_raw_key, source_key->size); |
| 71 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size, |
| 72 | NULL); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 73 | res = crypto_skcipher_encrypt(req); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 74 | if (res == -EINPROGRESS || res == -EBUSY) { |
| 75 | wait_for_completion(&ecr.completion); |
| 76 | res = ecr.res; |
| 77 | } |
| 78 | out: |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 79 | skcipher_request_free(req); |
| 80 | crypto_free_skcipher(tfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 81 | return res; |
| 82 | } |
| 83 | |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 84 | static int validate_user_key(struct fscrypt_info *crypt_info, |
| 85 | struct fscrypt_context *ctx, u8 *raw_key, |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 86 | const char *prefix, int min_keysize) |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 87 | { |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 88 | char *description; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 89 | struct key *keyring_key; |
| 90 | struct fscrypt_key *master_key; |
| 91 | const struct user_key_payload *ukp; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 92 | int res; |
| 93 | |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 94 | description = kasprintf(GFP_NOFS, "%s%*phN", prefix, |
| 95 | FS_KEY_DESCRIPTOR_SIZE, |
| 96 | ctx->master_key_descriptor); |
| 97 | if (!description) |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 98 | return -ENOMEM; |
| 99 | |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 100 | keyring_key = request_key(&key_type_logon, description, NULL); |
| 101 | kfree(description); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 102 | if (IS_ERR(keyring_key)) |
| 103 | return PTR_ERR(keyring_key); |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 104 | down_read(&keyring_key->sem); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 105 | |
| 106 | if (keyring_key->type != &key_type_logon) { |
| 107 | printk_once(KERN_WARNING |
| 108 | "%s: key type must be logon\n", __func__); |
| 109 | res = -ENOKEY; |
| 110 | goto out; |
| 111 | } |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 112 | ukp = user_key_payload_locked(keyring_key); |
Eric Biggers | d60b5b7 | 2017-10-09 12:46:18 -0700 | [diff] [blame] | 113 | if (!ukp) { |
| 114 | /* key was revoked before we acquired its semaphore */ |
| 115 | res = -EKEYREVOKED; |
| 116 | goto out; |
| 117 | } |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 118 | if (ukp->datalen != sizeof(struct fscrypt_key)) { |
| 119 | res = -EINVAL; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 120 | goto out; |
| 121 | } |
| 122 | master_key = (struct fscrypt_key *)ukp->data; |
| 123 | BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE); |
| 124 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 125 | if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE |
| 126 | || master_key->size % AES_BLOCK_SIZE != 0) { |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 127 | printk_once(KERN_WARNING |
| 128 | "%s: key size incorrect: %d\n", |
| 129 | __func__, master_key->size); |
| 130 | res = -ENOKEY; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 131 | goto out; |
| 132 | } |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 133 | res = derive_key_aes(ctx->nonce, master_key, raw_key); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 134 | out: |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 135 | up_read(&keyring_key->sem); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 136 | key_put(keyring_key); |
| 137 | return res; |
| 138 | } |
| 139 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 140 | static const struct { |
| 141 | const char *cipher_str; |
| 142 | int keysize; |
| 143 | } available_modes[] = { |
| 144 | [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)", |
| 145 | FS_AES_256_XTS_KEY_SIZE }, |
| 146 | [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))", |
| 147 | FS_AES_256_CTS_KEY_SIZE }, |
| 148 | [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)", |
| 149 | FS_AES_128_CBC_KEY_SIZE }, |
| 150 | [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))", |
| 151 | FS_AES_128_CTS_KEY_SIZE }, |
| 152 | }; |
| 153 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 154 | static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode, |
| 155 | const char **cipher_str_ret, int *keysize_ret) |
| 156 | { |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 157 | u32 mode; |
| 158 | |
| 159 | if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) { |
| 160 | pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n", |
| 161 | inode->i_ino, |
| 162 | ci->ci_data_mode, ci->ci_filename_mode); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 166 | if (S_ISREG(inode->i_mode)) { |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 167 | mode = ci->ci_data_mode; |
| 168 | } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) { |
| 169 | mode = ci->ci_filename_mode; |
| 170 | } else { |
| 171 | WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", |
| 172 | inode->i_ino, (inode->i_mode & S_IFMT)); |
| 173 | return -EINVAL; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 176 | *cipher_str_ret = available_modes[mode].cipher_str; |
| 177 | *keysize_ret = available_modes[mode].keysize; |
| 178 | return 0; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 181 | static void put_crypt_info(struct fscrypt_info *ci) |
| 182 | { |
| 183 | if (!ci) |
| 184 | return; |
| 185 | |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 186 | crypto_free_skcipher(ci->ci_ctfm); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 187 | crypto_free_cipher(ci->ci_essiv_tfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 188 | kmem_cache_free(fscrypt_info_cachep, ci); |
| 189 | } |
| 190 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 191 | static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) |
| 192 | { |
| 193 | struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm); |
| 194 | |
| 195 | /* init hash transform on demand */ |
| 196 | if (unlikely(!tfm)) { |
| 197 | struct crypto_shash *prev_tfm; |
| 198 | |
| 199 | tfm = crypto_alloc_shash("sha256", 0, 0); |
| 200 | if (IS_ERR(tfm)) { |
| 201 | pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n", |
| 202 | PTR_ERR(tfm)); |
| 203 | return PTR_ERR(tfm); |
| 204 | } |
| 205 | prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm); |
| 206 | if (prev_tfm) { |
| 207 | crypto_free_shash(tfm); |
| 208 | tfm = prev_tfm; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | { |
| 213 | SHASH_DESC_ON_STACK(desc, tfm); |
| 214 | desc->tfm = tfm; |
| 215 | desc->flags = 0; |
| 216 | |
| 217 | return crypto_shash_digest(desc, key, keysize, salt); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key, |
| 222 | int keysize) |
| 223 | { |
| 224 | int err; |
| 225 | struct crypto_cipher *essiv_tfm; |
| 226 | u8 salt[SHA256_DIGEST_SIZE]; |
| 227 | |
| 228 | essiv_tfm = crypto_alloc_cipher("aes", 0, 0); |
| 229 | if (IS_ERR(essiv_tfm)) |
| 230 | return PTR_ERR(essiv_tfm); |
| 231 | |
| 232 | ci->ci_essiv_tfm = essiv_tfm; |
| 233 | |
| 234 | err = derive_essiv_salt(raw_key, keysize, salt); |
| 235 | if (err) |
| 236 | goto out; |
| 237 | |
| 238 | /* |
| 239 | * Using SHA256 to derive the salt/key will result in AES-256 being |
| 240 | * used for IV generation. File contents encryption will still use the |
| 241 | * configured keysize (AES-128) nevertheless. |
| 242 | */ |
| 243 | err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt)); |
| 244 | if (err) |
| 245 | goto out; |
| 246 | |
| 247 | out: |
| 248 | memzero_explicit(salt, sizeof(salt)); |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | void __exit fscrypt_essiv_cleanup(void) |
| 253 | { |
| 254 | crypto_free_shash(essiv_hash_tfm); |
| 255 | } |
| 256 | |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 257 | int fscrypt_get_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 258 | { |
| 259 | struct fscrypt_info *crypt_info; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 260 | struct fscrypt_context ctx; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 261 | struct crypto_skcipher *ctfm; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 262 | const char *cipher_str; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 263 | int keysize; |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 264 | u8 *raw_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 265 | int res; |
| 266 | |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 267 | if (inode->i_crypt_info) |
| 268 | return 0; |
| 269 | |
David Gstir | f32d7ac | 2016-12-06 23:53:57 +0100 | [diff] [blame] | 270 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 271 | if (res) |
| 272 | return res; |
| 273 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 274 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 275 | if (res < 0) { |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 276 | if (!fscrypt_dummy_context_enabled(inode) || |
| 277 | inode->i_sb->s_cop->is_encrypted(inode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 278 | return res; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 279 | /* Fake up a context for an unencrypted directory */ |
| 280 | memset(&ctx, 0, sizeof(ctx)); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 281 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 282 | ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 283 | ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 284 | memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 285 | } else if (res != sizeof(ctx)) { |
| 286 | return -EINVAL; |
| 287 | } |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 288 | |
| 289 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) |
| 293 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 294 | |
| 295 | crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS); |
| 296 | if (!crypt_info) |
| 297 | return -ENOMEM; |
| 298 | |
| 299 | crypt_info->ci_flags = ctx.flags; |
| 300 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 301 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
| 302 | crypt_info->ci_ctfm = NULL; |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 303 | crypt_info->ci_essiv_tfm = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 304 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, |
| 305 | sizeof(crypt_info->ci_master_key)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 306 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 307 | res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize); |
| 308 | if (res) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 309 | goto out; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 310 | |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 311 | /* |
| 312 | * This cannot be a stack buffer because it is passed to the scatterlist |
| 313 | * crypto API as part of key derivation. |
| 314 | */ |
| 315 | res = -ENOMEM; |
| 316 | raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS); |
| 317 | if (!raw_key) |
| 318 | goto out; |
| 319 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 320 | res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX, |
| 321 | keysize); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 322 | if (res && inode->i_sb->s_cop->key_prefix) { |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 323 | int res2 = validate_user_key(crypt_info, &ctx, raw_key, |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 324 | inode->i_sb->s_cop->key_prefix, |
| 325 | keysize); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 326 | if (res2) { |
| 327 | if (res2 == -ENOKEY) |
| 328 | res = -ENOKEY; |
| 329 | goto out; |
| 330 | } |
| 331 | } else if (res) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 332 | goto out; |
| 333 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 334 | ctfm = crypto_alloc_skcipher(cipher_str, 0, 0); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 335 | if (!ctfm || IS_ERR(ctfm)) { |
| 336 | res = ctfm ? PTR_ERR(ctfm) : -ENOMEM; |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 337 | pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n", |
| 338 | __func__, res, inode->i_ino); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 339 | goto out; |
| 340 | } |
| 341 | crypt_info->ci_ctfm = ctfm; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 342 | crypto_skcipher_clear_flags(ctfm, ~0); |
| 343 | crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 344 | /* |
| 345 | * if the provided key is longer than keysize, we use the first |
| 346 | * keysize bytes of the derived key only |
| 347 | */ |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 348 | res = crypto_skcipher_setkey(ctfm, raw_key, keysize); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 349 | if (res) |
| 350 | goto out; |
| 351 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 352 | if (S_ISREG(inode->i_mode) && |
| 353 | crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) { |
| 354 | res = init_essiv_generator(crypt_info, raw_key, keysize); |
| 355 | if (res) { |
| 356 | pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n", |
| 357 | __func__, res, inode->i_ino); |
| 358 | goto out; |
| 359 | } |
| 360 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 361 | if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL) |
| 362 | crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 363 | out: |
| 364 | if (res == -ENOKEY) |
| 365 | res = 0; |
| 366 | put_crypt_info(crypt_info); |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 367 | kzfree(raw_key); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 368 | return res; |
| 369 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 370 | EXPORT_SYMBOL(fscrypt_get_encryption_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 371 | |
| 372 | void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci) |
| 373 | { |
| 374 | struct fscrypt_info *prev; |
| 375 | |
| 376 | if (ci == NULL) |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 377 | ci = READ_ONCE(inode->i_crypt_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 378 | if (ci == NULL) |
| 379 | return; |
| 380 | |
| 381 | prev = cmpxchg(&inode->i_crypt_info, ci, NULL); |
| 382 | if (prev != ci) |
| 383 | return; |
| 384 | |
| 385 | put_crypt_info(ci); |
| 386 | } |
| 387 | EXPORT_SYMBOL(fscrypt_put_encryption_info); |