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