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> |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 12 | #include <linux/hashtable.h> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 13 | #include <linux/scatterlist.h> |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 14 | #include <linux/ratelimit.h> |
| 15 | #include <crypto/aes.h> |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 16 | #include <crypto/algapi.h> |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 17 | #include <crypto/sha.h> |
Eric Biggers | eb9c5fd | 2018-01-05 10:45:00 -0800 | [diff] [blame] | 18 | #include <crypto/skcipher.h> |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 19 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 20 | |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 21 | static struct crypto_shash *essiv_hash_tfm; |
| 22 | |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 23 | /* Table of keys referenced by FS_POLICY_FLAG_DIRECT_KEY policies */ |
| 24 | static DEFINE_HASHTABLE(fscrypt_master_keys, 6); /* 6 bits = 64 buckets */ |
| 25 | static DEFINE_SPINLOCK(fscrypt_master_keys_lock); |
| 26 | |
Eric Biggers | 08ac722 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 27 | /* |
| 28 | * Key derivation function. This generates the derived key by encrypting the |
| 29 | * 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] | 30 | * |
Eric Biggers | 08ac722 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 31 | * The master key must be at least as long as the derived key. If the master |
| 32 | * key is longer, then only the first 'derived_keysize' bytes are used. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 33 | */ |
Eric Biggers | 08ac722 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 34 | static int derive_key_aes(const u8 *master_key, |
| 35 | const struct fscrypt_context *ctx, |
| 36 | u8 *derived_key, unsigned int derived_keysize) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 37 | { |
| 38 | int res = 0; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 39 | struct skcipher_request *req = NULL; |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 40 | DECLARE_CRYPTO_WAIT(wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 41 | struct scatterlist src_sg, dst_sg; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 42 | struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 43 | |
| 44 | if (IS_ERR(tfm)) { |
| 45 | res = PTR_ERR(tfm); |
| 46 | tfm = NULL; |
| 47 | goto out; |
| 48 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 49 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 50 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 51 | if (!req) { |
| 52 | res = -ENOMEM; |
| 53 | goto out; |
| 54 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 55 | skcipher_request_set_callback(req, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 56 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 57 | crypto_req_done, &wait); |
Eric Biggers | 08ac722 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 58 | res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 59 | if (res < 0) |
| 60 | goto out; |
| 61 | |
Eric Biggers | 08ac722 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 62 | sg_init_one(&src_sg, master_key, derived_keysize); |
| 63 | sg_init_one(&dst_sg, derived_key, derived_keysize); |
| 64 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize, |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 65 | NULL); |
Gilad Ben-Yossef | 743205f | 2017-10-18 08:00:44 +0100 | [diff] [blame] | 66 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 67 | out: |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 68 | skcipher_request_free(req); |
| 69 | crypto_free_skcipher(tfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 70 | return res; |
| 71 | } |
| 72 | |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 73 | /* |
| 74 | * Search the current task's subscribed keyrings for a "logon" key with |
| 75 | * description prefix:descriptor, and if found acquire a read lock on it and |
| 76 | * return a pointer to its validated payload in *payload_ret. |
| 77 | */ |
| 78 | static struct key * |
| 79 | find_and_lock_process_key(const char *prefix, |
| 80 | const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE], |
| 81 | unsigned int min_keysize, |
| 82 | const struct fscrypt_key **payload_ret) |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 83 | { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 84 | char *description; |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 85 | struct key *key; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 86 | const struct user_key_payload *ukp; |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 87 | const struct fscrypt_key *payload; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 88 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 89 | description = kasprintf(GFP_NOFS, "%s%*phN", prefix, |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 90 | FS_KEY_DESCRIPTOR_SIZE, descriptor); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 91 | if (!description) |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 92 | return ERR_PTR(-ENOMEM); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 93 | |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 94 | key = request_key(&key_type_logon, description, NULL); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 95 | kfree(description); |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 96 | if (IS_ERR(key)) |
| 97 | return key; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 98 | |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 99 | down_read(&key->sem); |
| 100 | ukp = user_key_payload_locked(key); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 101 | |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 102 | if (!ukp) /* was the key revoked before we acquired its semaphore? */ |
| 103 | goto invalid; |
| 104 | |
| 105 | payload = (const struct fscrypt_key *)ukp->data; |
| 106 | |
| 107 | if (ukp->datalen != sizeof(struct fscrypt_key) || |
| 108 | payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) { |
| 109 | fscrypt_warn(NULL, |
| 110 | "key with description '%s' has invalid payload", |
| 111 | key->description); |
| 112 | goto invalid; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 113 | } |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 114 | |
Eric Biggers | 08ac722 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 115 | if (payload->size < min_keysize) { |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 116 | fscrypt_warn(NULL, |
Eric Biggers | 08ac722 | 2018-04-30 15:51:49 -0700 | [diff] [blame] | 117 | "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] | 118 | key->description, payload->size, min_keysize); |
| 119 | goto invalid; |
| 120 | } |
| 121 | |
| 122 | *payload_ret = payload; |
| 123 | return key; |
| 124 | |
| 125 | invalid: |
| 126 | up_read(&key->sem); |
| 127 | key_put(key); |
| 128 | return ERR_PTR(-ENOKEY); |
| 129 | } |
| 130 | |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 131 | static struct fscrypt_mode available_modes[] = { |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 132 | [FS_ENCRYPTION_MODE_AES_256_XTS] = { |
| 133 | .friendly_name = "AES-256-XTS", |
| 134 | .cipher_str = "xts(aes)", |
| 135 | .keysize = 64, |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 136 | .ivsize = 16, |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 137 | }, |
| 138 | [FS_ENCRYPTION_MODE_AES_256_CTS] = { |
| 139 | .friendly_name = "AES-256-CTS-CBC", |
| 140 | .cipher_str = "cts(cbc(aes))", |
| 141 | .keysize = 32, |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 142 | .ivsize = 16, |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 143 | }, |
| 144 | [FS_ENCRYPTION_MODE_AES_128_CBC] = { |
| 145 | .friendly_name = "AES-128-CBC", |
| 146 | .cipher_str = "cbc(aes)", |
| 147 | .keysize = 16, |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 148 | .ivsize = 16, |
| 149 | .needs_essiv = true, |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 150 | }, |
| 151 | [FS_ENCRYPTION_MODE_AES_128_CTS] = { |
| 152 | .friendly_name = "AES-128-CTS-CBC", |
| 153 | .cipher_str = "cts(cbc(aes))", |
| 154 | .keysize = 16, |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 155 | .ivsize = 16, |
| 156 | }, |
| 157 | [FS_ENCRYPTION_MODE_ADIANTUM] = { |
| 158 | .friendly_name = "Adiantum", |
| 159 | .cipher_str = "adiantum(xchacha12,aes)", |
| 160 | .keysize = 32, |
| 161 | .ivsize = 32, |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 162 | }, |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 163 | }; |
| 164 | |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 165 | static struct fscrypt_mode * |
| 166 | select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode) |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 167 | { |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 168 | 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] | 169 | fscrypt_warn(inode->i_sb, |
| 170 | "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)", |
| 171 | inode->i_ino, ci->ci_data_mode, |
| 172 | ci->ci_filename_mode); |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 173 | return ERR_PTR(-EINVAL); |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 176 | if (S_ISREG(inode->i_mode)) |
| 177 | return &available_modes[ci->ci_data_mode]; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 178 | |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 179 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 180 | return &available_modes[ci->ci_filename_mode]; |
| 181 | |
| 182 | WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", |
| 183 | inode->i_ino, (inode->i_mode & S_IFMT)); |
| 184 | return ERR_PTR(-EINVAL); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 187 | /* Find the master key, then derive the inode's actual encryption key */ |
| 188 | static int find_and_derive_key(const struct inode *inode, |
| 189 | const struct fscrypt_context *ctx, |
| 190 | u8 *derived_key, const struct fscrypt_mode *mode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 191 | { |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 192 | struct key *key; |
| 193 | const struct fscrypt_key *payload; |
| 194 | int err; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 195 | |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 196 | key = find_and_lock_process_key(FS_KEY_DESC_PREFIX, |
| 197 | ctx->master_key_descriptor, |
| 198 | mode->keysize, &payload); |
| 199 | if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) { |
| 200 | key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix, |
| 201 | ctx->master_key_descriptor, |
| 202 | mode->keysize, &payload); |
| 203 | } |
| 204 | if (IS_ERR(key)) |
| 205 | return PTR_ERR(key); |
| 206 | |
| 207 | if (ctx->flags & FS_POLICY_FLAG_DIRECT_KEY) { |
| 208 | if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) { |
| 209 | fscrypt_warn(inode->i_sb, |
| 210 | "direct key mode not allowed with %s", |
| 211 | mode->friendly_name); |
| 212 | err = -EINVAL; |
| 213 | } else if (ctx->contents_encryption_mode != |
| 214 | ctx->filenames_encryption_mode) { |
| 215 | fscrypt_warn(inode->i_sb, |
| 216 | "direct key mode not allowed with different contents and filenames modes"); |
| 217 | err = -EINVAL; |
| 218 | } else { |
| 219 | memcpy(derived_key, payload->raw, mode->keysize); |
| 220 | err = 0; |
| 221 | } |
| 222 | } else { |
| 223 | err = derive_key_aes(payload->raw, ctx, derived_key, |
| 224 | mode->keysize); |
| 225 | } |
| 226 | up_read(&key->sem); |
| 227 | key_put(key); |
| 228 | return err; |
| 229 | } |
| 230 | |
| 231 | /* Allocate and key a symmetric cipher object for the given encryption mode */ |
| 232 | static struct crypto_skcipher * |
| 233 | allocate_skcipher_for_mode(struct fscrypt_mode *mode, const u8 *raw_key, |
| 234 | const struct inode *inode) |
| 235 | { |
| 236 | struct crypto_skcipher *tfm; |
| 237 | int err; |
| 238 | |
| 239 | tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); |
| 240 | if (IS_ERR(tfm)) { |
| 241 | fscrypt_warn(inode->i_sb, |
| 242 | "error allocating '%s' transform for inode %lu: %ld", |
| 243 | mode->cipher_str, inode->i_ino, PTR_ERR(tfm)); |
| 244 | return tfm; |
| 245 | } |
| 246 | if (unlikely(!mode->logged_impl_name)) { |
| 247 | /* |
| 248 | * fscrypt performance can vary greatly depending on which |
| 249 | * crypto algorithm implementation is used. Help people debug |
| 250 | * performance problems by logging the ->cra_driver_name the |
| 251 | * first time a mode is used. Note that multiple threads can |
| 252 | * race here, but it doesn't really matter. |
| 253 | */ |
| 254 | mode->logged_impl_name = true; |
| 255 | pr_info("fscrypt: %s using implementation \"%s\"\n", |
| 256 | mode->friendly_name, |
| 257 | crypto_skcipher_alg(tfm)->base.cra_driver_name); |
| 258 | } |
| 259 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 260 | err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize); |
| 261 | if (err) |
| 262 | goto err_free_tfm; |
| 263 | |
| 264 | return tfm; |
| 265 | |
| 266 | err_free_tfm: |
| 267 | crypto_free_skcipher(tfm); |
| 268 | return ERR_PTR(err); |
| 269 | } |
| 270 | |
| 271 | /* Master key referenced by FS_POLICY_FLAG_DIRECT_KEY policy */ |
| 272 | struct fscrypt_master_key { |
| 273 | struct hlist_node mk_node; |
| 274 | atomic_t mk_refcount; |
| 275 | const struct fscrypt_mode *mk_mode; |
| 276 | struct crypto_skcipher *mk_ctfm; |
| 277 | u8 mk_descriptor[FS_KEY_DESCRIPTOR_SIZE]; |
| 278 | u8 mk_raw[FS_MAX_KEY_SIZE]; |
| 279 | }; |
| 280 | |
| 281 | static void free_master_key(struct fscrypt_master_key *mk) |
| 282 | { |
| 283 | if (mk) { |
| 284 | crypto_free_skcipher(mk->mk_ctfm); |
| 285 | kzfree(mk); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | static void put_master_key(struct fscrypt_master_key *mk) |
| 290 | { |
| 291 | if (!atomic_dec_and_lock(&mk->mk_refcount, &fscrypt_master_keys_lock)) |
| 292 | return; |
| 293 | hash_del(&mk->mk_node); |
| 294 | spin_unlock(&fscrypt_master_keys_lock); |
| 295 | |
| 296 | free_master_key(mk); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Find/insert the given master key into the fscrypt_master_keys table. If |
| 301 | * found, it is returned with elevated refcount, and 'to_insert' is freed if |
| 302 | * non-NULL. If not found, 'to_insert' is inserted and returned if it's |
| 303 | * non-NULL; otherwise NULL is returned. |
| 304 | */ |
| 305 | static struct fscrypt_master_key * |
| 306 | find_or_insert_master_key(struct fscrypt_master_key *to_insert, |
| 307 | const u8 *raw_key, const struct fscrypt_mode *mode, |
| 308 | const struct fscrypt_info *ci) |
| 309 | { |
| 310 | unsigned long hash_key; |
| 311 | struct fscrypt_master_key *mk; |
| 312 | |
| 313 | /* |
| 314 | * Careful: to avoid potentially leaking secret key bytes via timing |
| 315 | * information, we must key the hash table by descriptor rather than by |
| 316 | * raw key, and use crypto_memneq() when comparing raw keys. |
| 317 | */ |
| 318 | |
| 319 | BUILD_BUG_ON(sizeof(hash_key) > FS_KEY_DESCRIPTOR_SIZE); |
| 320 | memcpy(&hash_key, ci->ci_master_key_descriptor, sizeof(hash_key)); |
| 321 | |
| 322 | spin_lock(&fscrypt_master_keys_lock); |
| 323 | hash_for_each_possible(fscrypt_master_keys, mk, mk_node, hash_key) { |
| 324 | if (memcmp(ci->ci_master_key_descriptor, mk->mk_descriptor, |
| 325 | FS_KEY_DESCRIPTOR_SIZE) != 0) |
| 326 | continue; |
| 327 | if (mode != mk->mk_mode) |
| 328 | continue; |
| 329 | if (crypto_memneq(raw_key, mk->mk_raw, mode->keysize)) |
| 330 | continue; |
| 331 | /* using existing tfm with same (descriptor, mode, raw_key) */ |
| 332 | atomic_inc(&mk->mk_refcount); |
| 333 | spin_unlock(&fscrypt_master_keys_lock); |
| 334 | free_master_key(to_insert); |
| 335 | return mk; |
| 336 | } |
| 337 | if (to_insert) |
| 338 | hash_add(fscrypt_master_keys, &to_insert->mk_node, hash_key); |
| 339 | spin_unlock(&fscrypt_master_keys_lock); |
| 340 | return to_insert; |
| 341 | } |
| 342 | |
| 343 | /* Prepare to encrypt directly using the master key in the given mode */ |
| 344 | static struct fscrypt_master_key * |
| 345 | fscrypt_get_master_key(const struct fscrypt_info *ci, struct fscrypt_mode *mode, |
| 346 | const u8 *raw_key, const struct inode *inode) |
| 347 | { |
| 348 | struct fscrypt_master_key *mk; |
| 349 | int err; |
| 350 | |
| 351 | /* Is there already a tfm for this key? */ |
| 352 | mk = find_or_insert_master_key(NULL, raw_key, mode, ci); |
| 353 | if (mk) |
| 354 | return mk; |
| 355 | |
| 356 | /* Nope, allocate one. */ |
| 357 | mk = kzalloc(sizeof(*mk), GFP_NOFS); |
| 358 | if (!mk) |
| 359 | return ERR_PTR(-ENOMEM); |
| 360 | atomic_set(&mk->mk_refcount, 1); |
| 361 | mk->mk_mode = mode; |
| 362 | mk->mk_ctfm = allocate_skcipher_for_mode(mode, raw_key, inode); |
| 363 | if (IS_ERR(mk->mk_ctfm)) { |
| 364 | err = PTR_ERR(mk->mk_ctfm); |
| 365 | mk->mk_ctfm = NULL; |
| 366 | goto err_free_mk; |
| 367 | } |
| 368 | memcpy(mk->mk_descriptor, ci->ci_master_key_descriptor, |
| 369 | FS_KEY_DESCRIPTOR_SIZE); |
| 370 | memcpy(mk->mk_raw, raw_key, mode->keysize); |
| 371 | |
| 372 | return find_or_insert_master_key(mk, raw_key, mode, ci); |
| 373 | |
| 374 | err_free_mk: |
| 375 | free_master_key(mk); |
| 376 | return ERR_PTR(err); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 379 | static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) |
| 380 | { |
| 381 | struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm); |
| 382 | |
| 383 | /* init hash transform on demand */ |
| 384 | if (unlikely(!tfm)) { |
| 385 | struct crypto_shash *prev_tfm; |
| 386 | |
| 387 | tfm = crypto_alloc_shash("sha256", 0, 0); |
| 388 | if (IS_ERR(tfm)) { |
Eric Biggers | 78275d8 | 2018-04-30 15:51:47 -0700 | [diff] [blame] | 389 | fscrypt_warn(NULL, |
| 390 | "error allocating SHA-256 transform: %ld", |
| 391 | PTR_ERR(tfm)); |
Daniel Walter | 8e989de | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 392 | return PTR_ERR(tfm); |
| 393 | } |
| 394 | prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm); |
| 395 | if (prev_tfm) { |
| 396 | crypto_free_shash(tfm); |
| 397 | tfm = prev_tfm; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | { |
| 402 | SHASH_DESC_ON_STACK(desc, tfm); |
| 403 | desc->tfm = tfm; |
| 404 | desc->flags = 0; |
| 405 | |
| 406 | return crypto_shash_digest(desc, key, keysize, salt); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key, |
| 411 | int keysize) |
| 412 | { |
| 413 | int err; |
| 414 | struct crypto_cipher *essiv_tfm; |
| 415 | u8 salt[SHA256_DIGEST_SIZE]; |
| 416 | |
| 417 | essiv_tfm = crypto_alloc_cipher("aes", 0, 0); |
| 418 | if (IS_ERR(essiv_tfm)) |
| 419 | return PTR_ERR(essiv_tfm); |
| 420 | |
| 421 | ci->ci_essiv_tfm = essiv_tfm; |
| 422 | |
| 423 | err = derive_essiv_salt(raw_key, keysize, salt); |
| 424 | if (err) |
| 425 | goto out; |
| 426 | |
| 427 | /* |
| 428 | * Using SHA256 to derive the salt/key will result in AES-256 being |
| 429 | * used for IV generation. File contents encryption will still use the |
| 430 | * configured keysize (AES-128) nevertheless. |
| 431 | */ |
| 432 | err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt)); |
| 433 | if (err) |
| 434 | goto out; |
| 435 | |
| 436 | out: |
| 437 | memzero_explicit(salt, sizeof(salt)); |
| 438 | return err; |
| 439 | } |
| 440 | |
| 441 | void __exit fscrypt_essiv_cleanup(void) |
| 442 | { |
| 443 | crypto_free_shash(essiv_hash_tfm); |
| 444 | } |
| 445 | |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 446 | /* |
| 447 | * Given the encryption mode and key (normally the derived key, but for |
| 448 | * FS_POLICY_FLAG_DIRECT_KEY mode it's the master key), set up the inode's |
| 449 | * symmetric cipher transform object(s). |
| 450 | */ |
| 451 | static int setup_crypto_transform(struct fscrypt_info *ci, |
| 452 | struct fscrypt_mode *mode, |
| 453 | const u8 *raw_key, const struct inode *inode) |
| 454 | { |
| 455 | struct fscrypt_master_key *mk; |
| 456 | struct crypto_skcipher *ctfm; |
| 457 | int err; |
| 458 | |
| 459 | if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) { |
| 460 | mk = fscrypt_get_master_key(ci, mode, raw_key, inode); |
| 461 | if (IS_ERR(mk)) |
| 462 | return PTR_ERR(mk); |
| 463 | ctfm = mk->mk_ctfm; |
| 464 | } else { |
| 465 | mk = NULL; |
| 466 | ctfm = allocate_skcipher_for_mode(mode, raw_key, inode); |
| 467 | if (IS_ERR(ctfm)) |
| 468 | return PTR_ERR(ctfm); |
| 469 | } |
| 470 | ci->ci_master_key = mk; |
| 471 | ci->ci_ctfm = ctfm; |
| 472 | |
| 473 | if (mode->needs_essiv) { |
| 474 | /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */ |
| 475 | WARN_ON(mode->ivsize != AES_BLOCK_SIZE); |
| 476 | WARN_ON(ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY); |
| 477 | |
| 478 | err = init_essiv_generator(ci, raw_key, mode->keysize); |
| 479 | if (err) { |
| 480 | fscrypt_warn(inode->i_sb, |
| 481 | "error initializing ESSIV generator for inode %lu: %d", |
| 482 | inode->i_ino, err); |
| 483 | return err; |
| 484 | } |
| 485 | } |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static void put_crypt_info(struct fscrypt_info *ci) |
| 490 | { |
| 491 | if (!ci) |
| 492 | return; |
| 493 | |
| 494 | if (ci->ci_master_key) { |
| 495 | put_master_key(ci->ci_master_key); |
| 496 | } else { |
| 497 | crypto_free_skcipher(ci->ci_ctfm); |
| 498 | crypto_free_cipher(ci->ci_essiv_tfm); |
| 499 | } |
| 500 | kmem_cache_free(fscrypt_info_cachep, ci); |
| 501 | } |
| 502 | |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 503 | int fscrypt_get_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 504 | { |
| 505 | struct fscrypt_info *crypt_info; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 506 | struct fscrypt_context ctx; |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 507 | struct fscrypt_mode *mode; |
Eric Biggers | 0f0909e | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 508 | u8 *raw_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 509 | int res; |
| 510 | |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 511 | if (inode->i_crypt_info) |
| 512 | return 0; |
| 513 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 514 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 515 | if (res) |
| 516 | return res; |
| 517 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 518 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 519 | if (res < 0) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 520 | if (!fscrypt_dummy_context_enabled(inode) || |
Eric Biggers | d750ec7 | 2017-10-09 12:15:36 -0700 | [diff] [blame] | 521 | IS_ENCRYPTED(inode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 522 | return res; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 523 | /* Fake up a context for an unencrypted directory */ |
| 524 | memset(&ctx, 0, sizeof(ctx)); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 525 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 526 | ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 527 | ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 528 | memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 529 | } else if (res != sizeof(ctx)) { |
| 530 | return -EINVAL; |
| 531 | } |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 532 | |
| 533 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 534 | return -EINVAL; |
| 535 | |
| 536 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) |
| 537 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 538 | |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 539 | crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 540 | if (!crypt_info) |
| 541 | return -ENOMEM; |
| 542 | |
| 543 | crypt_info->ci_flags = ctx.flags; |
| 544 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 545 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 546 | memcpy(crypt_info->ci_master_key_descriptor, ctx.master_key_descriptor, |
| 547 | FS_KEY_DESCRIPTOR_SIZE); |
| 548 | memcpy(crypt_info->ci_nonce, ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 549 | |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 550 | mode = select_encryption_mode(crypt_info, inode); |
| 551 | if (IS_ERR(mode)) { |
| 552 | res = PTR_ERR(mode); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 553 | goto out; |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 554 | } |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 555 | WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE); |
| 556 | crypt_info->ci_mode = mode; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 557 | |
Eric Biggers | 0f0909e | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 558 | /* |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 559 | * This cannot be a stack buffer because it may be passed to the |
| 560 | * scatterlist crypto API as part of key derivation. |
Eric Biggers | 0f0909e | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 561 | */ |
| 562 | res = -ENOMEM; |
Eric Biggers | a4842a1 | 2018-05-18 10:58:14 -0700 | [diff] [blame] | 563 | raw_key = kmalloc(mode->keysize, GFP_NOFS); |
Eric Biggers | 0f0909e | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 564 | if (!raw_key) |
| 565 | goto out; |
| 566 | |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 567 | res = find_and_derive_key(inode, &ctx, raw_key, mode); |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 568 | if (res) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 569 | goto out; |
Eric Biggers | 24cc7a8 | 2018-04-30 15:51:48 -0700 | [diff] [blame] | 570 | |
Eric Biggers | e33fa31 | 2018-11-26 11:27:37 -0800 | [diff] [blame] | 571 | res = setup_crypto_transform(crypt_info, mode, raw_key, inode); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 572 | if (res) |
| 573 | goto out; |
| 574 | |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 575 | if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL) |
| 576 | crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 577 | out: |
| 578 | if (res == -ENOKEY) |
| 579 | res = 0; |
| 580 | put_crypt_info(crypt_info); |
Eric Biggers | 0f0909e | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 581 | kzfree(raw_key); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 582 | return res; |
| 583 | } |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 584 | EXPORT_SYMBOL(fscrypt_get_encryption_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 585 | |
Eric Biggers | 401052f | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 586 | void fscrypt_put_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 587 | { |
Eric Biggers | 401052f | 2018-01-11 23:30:13 -0500 | [diff] [blame] | 588 | put_crypt_info(inode->i_crypt_info); |
| 589 | inode->i_crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 590 | } |
| 591 | EXPORT_SYMBOL(fscrypt_put_encryption_info); |