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> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 13 | #include <linux/fscrypto.h> |
| 14 | |
| 15 | static void derive_crypt_complete(struct crypto_async_request *req, int rc) |
| 16 | { |
| 17 | struct fscrypt_completion_result *ecr = req->data; |
| 18 | |
| 19 | if (rc == -EINPROGRESS) |
| 20 | return; |
| 21 | |
| 22 | ecr->res = rc; |
| 23 | complete(&ecr->completion); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * derive_key_aes() - Derive a key using AES-128-ECB |
| 28 | * @deriving_key: Encryption key used for derivation. |
| 29 | * @source_key: Source key to which to apply derivation. |
| 30 | * @derived_key: Derived key. |
| 31 | * |
| 32 | * Return: Zero on success; non-zero otherwise. |
| 33 | */ |
| 34 | static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE], |
| 35 | u8 source_key[FS_AES_256_XTS_KEY_SIZE], |
| 36 | u8 derived_key[FS_AES_256_XTS_KEY_SIZE]) |
| 37 | { |
| 38 | int res = 0; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 39 | struct skcipher_request *req = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 40 | DECLARE_FS_COMPLETION_RESULT(ecr); |
| 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, |
| 57 | derive_crypt_complete, &ecr); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 58 | res = crypto_skcipher_setkey(tfm, deriving_key, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 59 | FS_AES_128_ECB_KEY_SIZE); |
| 60 | if (res < 0) |
| 61 | goto out; |
| 62 | |
| 63 | sg_init_one(&src_sg, source_key, FS_AES_256_XTS_KEY_SIZE); |
| 64 | sg_init_one(&dst_sg, derived_key, FS_AES_256_XTS_KEY_SIZE); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 65 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 66 | FS_AES_256_XTS_KEY_SIZE, NULL); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 67 | res = crypto_skcipher_encrypt(req); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 68 | if (res == -EINPROGRESS || res == -EBUSY) { |
| 69 | wait_for_completion(&ecr.completion); |
| 70 | res = ecr.res; |
| 71 | } |
| 72 | out: |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 73 | skcipher_request_free(req); |
| 74 | crypto_free_skcipher(tfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 75 | return res; |
| 76 | } |
| 77 | |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 78 | static int validate_user_key(struct fscrypt_info *crypt_info, |
| 79 | struct fscrypt_context *ctx, u8 *raw_key, |
| 80 | u8 *prefix, int prefix_size) |
| 81 | { |
| 82 | u8 *full_key_descriptor; |
| 83 | struct key *keyring_key; |
| 84 | struct fscrypt_key *master_key; |
| 85 | const struct user_key_payload *ukp; |
| 86 | int full_key_len = prefix_size + (FS_KEY_DESCRIPTOR_SIZE * 2) + 1; |
| 87 | int res; |
| 88 | |
| 89 | full_key_descriptor = kmalloc(full_key_len, GFP_NOFS); |
| 90 | if (!full_key_descriptor) |
| 91 | return -ENOMEM; |
| 92 | |
| 93 | memcpy(full_key_descriptor, prefix, prefix_size); |
| 94 | sprintf(full_key_descriptor + prefix_size, |
| 95 | "%*phN", FS_KEY_DESCRIPTOR_SIZE, |
| 96 | ctx->master_key_descriptor); |
| 97 | full_key_descriptor[full_key_len - 1] = '\0'; |
| 98 | keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL); |
| 99 | kfree(full_key_descriptor); |
| 100 | if (IS_ERR(keyring_key)) |
| 101 | return PTR_ERR(keyring_key); |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 102 | down_read(&keyring_key->sem); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 103 | |
| 104 | if (keyring_key->type != &key_type_logon) { |
| 105 | printk_once(KERN_WARNING |
| 106 | "%s: key type must be logon\n", __func__); |
| 107 | res = -ENOKEY; |
| 108 | goto out; |
| 109 | } |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 110 | ukp = user_key_payload(keyring_key); |
| 111 | if (ukp->datalen != sizeof(struct fscrypt_key)) { |
| 112 | res = -EINVAL; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 113 | goto out; |
| 114 | } |
| 115 | master_key = (struct fscrypt_key *)ukp->data; |
| 116 | BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE); |
| 117 | |
| 118 | if (master_key->size != FS_AES_256_XTS_KEY_SIZE) { |
| 119 | printk_once(KERN_WARNING |
| 120 | "%s: key size incorrect: %d\n", |
| 121 | __func__, master_key->size); |
| 122 | res = -ENOKEY; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 123 | goto out; |
| 124 | } |
| 125 | res = derive_key_aes(ctx->nonce, master_key->raw, raw_key); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 126 | out: |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 127 | up_read(&keyring_key->sem); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 128 | key_put(keyring_key); |
| 129 | return res; |
| 130 | } |
| 131 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 132 | static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode, |
| 133 | const char **cipher_str_ret, int *keysize_ret) |
| 134 | { |
| 135 | if (S_ISREG(inode->i_mode)) { |
| 136 | if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) { |
| 137 | *cipher_str_ret = "xts(aes)"; |
| 138 | *keysize_ret = FS_AES_256_XTS_KEY_SIZE; |
| 139 | return 0; |
| 140 | } |
| 141 | pr_warn_once("fscrypto: unsupported contents encryption mode " |
| 142 | "%d for inode %lu\n", |
| 143 | ci->ci_data_mode, inode->i_ino); |
| 144 | return -ENOKEY; |
| 145 | } |
| 146 | |
| 147 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) { |
| 148 | if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) { |
| 149 | *cipher_str_ret = "cts(cbc(aes))"; |
| 150 | *keysize_ret = FS_AES_256_CTS_KEY_SIZE; |
| 151 | return 0; |
| 152 | } |
| 153 | pr_warn_once("fscrypto: unsupported filenames encryption mode " |
| 154 | "%d for inode %lu\n", |
| 155 | ci->ci_filename_mode, inode->i_ino); |
| 156 | return -ENOKEY; |
| 157 | } |
| 158 | |
| 159 | pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n", |
| 160 | (inode->i_mode & S_IFMT), inode->i_ino); |
| 161 | return -ENOKEY; |
| 162 | } |
| 163 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 164 | static void put_crypt_info(struct fscrypt_info *ci) |
| 165 | { |
| 166 | if (!ci) |
| 167 | return; |
| 168 | |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 169 | crypto_free_skcipher(ci->ci_ctfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 170 | kmem_cache_free(fscrypt_info_cachep, ci); |
| 171 | } |
| 172 | |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 173 | int fscrypt_get_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 174 | { |
| 175 | struct fscrypt_info *crypt_info; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 176 | struct fscrypt_context ctx; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 177 | struct crypto_skcipher *ctfm; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 178 | const char *cipher_str; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 179 | int keysize; |
Eric Biggers | 0f0909e | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 180 | u8 *raw_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 181 | int res; |
| 182 | |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 183 | if (inode->i_crypt_info) |
| 184 | return 0; |
| 185 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 186 | res = fscrypt_initialize(); |
| 187 | if (res) |
| 188 | return res; |
| 189 | |
| 190 | if (!inode->i_sb->s_cop->get_context) |
| 191 | return -EOPNOTSUPP; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 192 | |
| 193 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 194 | if (res < 0) { |
| 195 | if (!fscrypt_dummy_context_enabled(inode)) |
| 196 | return res; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 197 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 198 | ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 199 | ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
| 200 | ctx.flags = 0; |
| 201 | } else if (res != sizeof(ctx)) { |
| 202 | return -EINVAL; |
| 203 | } |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 204 | |
| 205 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 206 | return -EINVAL; |
| 207 | |
| 208 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) |
| 209 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 210 | |
| 211 | crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS); |
| 212 | if (!crypt_info) |
| 213 | return -ENOMEM; |
| 214 | |
| 215 | crypt_info->ci_flags = ctx.flags; |
| 216 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 217 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
| 218 | crypt_info->ci_ctfm = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 219 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, |
| 220 | sizeof(crypt_info->ci_master_key)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 221 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 222 | res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize); |
| 223 | if (res) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 224 | goto out; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 225 | |
Eric Biggers | 0f0909e | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 226 | /* |
| 227 | * This cannot be a stack buffer because it is passed to the scatterlist |
| 228 | * crypto API as part of key derivation. |
| 229 | */ |
| 230 | res = -ENOMEM; |
| 231 | raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS); |
| 232 | if (!raw_key) |
| 233 | goto out; |
| 234 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 235 | if (fscrypt_dummy_context_enabled(inode)) { |
| 236 | memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE); |
| 237 | goto got_key; |
| 238 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 239 | |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 240 | res = validate_user_key(crypt_info, &ctx, raw_key, |
| 241 | FS_KEY_DESC_PREFIX, FS_KEY_DESC_PREFIX_SIZE); |
| 242 | if (res && inode->i_sb->s_cop->key_prefix) { |
| 243 | u8 *prefix = NULL; |
| 244 | int prefix_size, res2; |
| 245 | |
| 246 | prefix_size = inode->i_sb->s_cop->key_prefix(inode, &prefix); |
| 247 | res2 = validate_user_key(crypt_info, &ctx, raw_key, |
| 248 | prefix, prefix_size); |
| 249 | if (res2) { |
| 250 | if (res2 == -ENOKEY) |
| 251 | res = -ENOKEY; |
| 252 | goto out; |
| 253 | } |
| 254 | } else if (res) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 255 | goto out; |
| 256 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 257 | got_key: |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 258 | ctfm = crypto_alloc_skcipher(cipher_str, 0, 0); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 259 | if (!ctfm || IS_ERR(ctfm)) { |
| 260 | res = ctfm ? PTR_ERR(ctfm) : -ENOMEM; |
| 261 | printk(KERN_DEBUG |
| 262 | "%s: error %d (inode %u) allocating crypto tfm\n", |
| 263 | __func__, res, (unsigned) inode->i_ino); |
| 264 | goto out; |
| 265 | } |
| 266 | crypt_info->ci_ctfm = ctfm; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 267 | crypto_skcipher_clear_flags(ctfm, ~0); |
| 268 | crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 269 | res = crypto_skcipher_setkey(ctfm, raw_key, keysize); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 270 | if (res) |
| 271 | goto out; |
| 272 | |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 273 | if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL) |
| 274 | crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 275 | out: |
| 276 | if (res == -ENOKEY) |
| 277 | res = 0; |
| 278 | put_crypt_info(crypt_info); |
Eric Biggers | 0f0909e | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 279 | kzfree(raw_key); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 280 | return res; |
| 281 | } |
Eric Biggers | 2984e52 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 282 | EXPORT_SYMBOL(fscrypt_get_encryption_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 283 | |
| 284 | void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci) |
| 285 | { |
| 286 | struct fscrypt_info *prev; |
| 287 | |
| 288 | if (ci == NULL) |
| 289 | ci = ACCESS_ONCE(inode->i_crypt_info); |
| 290 | if (ci == NULL) |
| 291 | return; |
| 292 | |
| 293 | prev = cmpxchg(&inode->i_crypt_info, ci, NULL); |
| 294 | if (prev != ci) |
| 295 | return; |
| 296 | |
| 297 | put_crypt_info(ci); |
| 298 | } |
| 299 | EXPORT_SYMBOL(fscrypt_put_encryption_info); |