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> |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 13 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 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, |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 80 | const char *prefix) |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 81 | { |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 82 | char *description; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 83 | struct key *keyring_key; |
| 84 | struct fscrypt_key *master_key; |
| 85 | const struct user_key_payload *ukp; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 86 | int res; |
| 87 | |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 88 | description = kasprintf(GFP_NOFS, "%s%*phN", prefix, |
| 89 | FS_KEY_DESCRIPTOR_SIZE, |
| 90 | ctx->master_key_descriptor); |
| 91 | if (!description) |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 92 | return -ENOMEM; |
| 93 | |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 94 | keyring_key = request_key(&key_type_logon, description, NULL); |
| 95 | kfree(description); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 96 | if (IS_ERR(keyring_key)) |
| 97 | return PTR_ERR(keyring_key); |
| 98 | |
| 99 | if (keyring_key->type != &key_type_logon) { |
| 100 | printk_once(KERN_WARNING |
| 101 | "%s: key type must be logon\n", __func__); |
| 102 | res = -ENOKEY; |
| 103 | goto out; |
| 104 | } |
| 105 | down_read(&keyring_key->sem); |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 106 | ukp = user_key_payload_locked(keyring_key); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 107 | if (ukp->datalen != sizeof(struct fscrypt_key)) { |
| 108 | res = -EINVAL; |
| 109 | up_read(&keyring_key->sem); |
| 110 | goto out; |
| 111 | } |
| 112 | master_key = (struct fscrypt_key *)ukp->data; |
| 113 | BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE); |
| 114 | |
| 115 | if (master_key->size != FS_AES_256_XTS_KEY_SIZE) { |
| 116 | printk_once(KERN_WARNING |
| 117 | "%s: key size incorrect: %d\n", |
| 118 | __func__, master_key->size); |
| 119 | res = -ENOKEY; |
| 120 | up_read(&keyring_key->sem); |
| 121 | goto out; |
| 122 | } |
| 123 | res = derive_key_aes(ctx->nonce, master_key->raw, raw_key); |
| 124 | up_read(&keyring_key->sem); |
| 125 | if (res) |
| 126 | goto out; |
| 127 | |
| 128 | crypt_info->ci_keyring_key = keyring_key; |
| 129 | return 0; |
| 130 | out: |
| 131 | key_put(keyring_key); |
| 132 | return res; |
| 133 | } |
| 134 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 135 | static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode, |
| 136 | const char **cipher_str_ret, int *keysize_ret) |
| 137 | { |
| 138 | if (S_ISREG(inode->i_mode)) { |
| 139 | if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) { |
| 140 | *cipher_str_ret = "xts(aes)"; |
| 141 | *keysize_ret = FS_AES_256_XTS_KEY_SIZE; |
| 142 | return 0; |
| 143 | } |
| 144 | pr_warn_once("fscrypto: unsupported contents encryption mode " |
| 145 | "%d for inode %lu\n", |
| 146 | ci->ci_data_mode, inode->i_ino); |
| 147 | return -ENOKEY; |
| 148 | } |
| 149 | |
| 150 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) { |
| 151 | if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) { |
| 152 | *cipher_str_ret = "cts(cbc(aes))"; |
| 153 | *keysize_ret = FS_AES_256_CTS_KEY_SIZE; |
| 154 | return 0; |
| 155 | } |
| 156 | pr_warn_once("fscrypto: unsupported filenames encryption mode " |
| 157 | "%d for inode %lu\n", |
| 158 | ci->ci_filename_mode, inode->i_ino); |
| 159 | return -ENOKEY; |
| 160 | } |
| 161 | |
| 162 | pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n", |
| 163 | (inode->i_mode & S_IFMT), inode->i_ino); |
| 164 | return -ENOKEY; |
| 165 | } |
| 166 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 167 | static void put_crypt_info(struct fscrypt_info *ci) |
| 168 | { |
| 169 | if (!ci) |
| 170 | return; |
| 171 | |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 172 | key_put(ci->ci_keyring_key); |
| 173 | crypto_free_skcipher(ci->ci_ctfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 174 | kmem_cache_free(fscrypt_info_cachep, ci); |
| 175 | } |
| 176 | |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 177 | int fscrypt_get_crypt_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 178 | { |
| 179 | struct fscrypt_info *crypt_info; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 180 | struct fscrypt_context ctx; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 181 | struct crypto_skcipher *ctfm; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 182 | const char *cipher_str; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 183 | int keysize; |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 184 | u8 *raw_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 185 | int res; |
| 186 | |
David Gstir | f32d7ac | 2016-12-06 23:53:57 +0100 | [diff] [blame] | 187 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 188 | if (res) |
| 189 | return res; |
| 190 | |
| 191 | if (!inode->i_sb->s_cop->get_context) |
| 192 | return -EOPNOTSUPP; |
| 193 | retry: |
| 194 | crypt_info = ACCESS_ONCE(inode->i_crypt_info); |
| 195 | if (crypt_info) { |
| 196 | if (!crypt_info->ci_keyring_key || |
| 197 | key_validate(crypt_info->ci_keyring_key) == 0) |
| 198 | return 0; |
| 199 | fscrypt_put_encryption_info(inode, crypt_info); |
| 200 | goto retry; |
| 201 | } |
| 202 | |
| 203 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 204 | if (res < 0) { |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 205 | if (!fscrypt_dummy_context_enabled(inode) || |
| 206 | inode->i_sb->s_cop->is_encrypted(inode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 207 | return res; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 208 | /* Fake up a context for an unencrypted directory */ |
| 209 | memset(&ctx, 0, sizeof(ctx)); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 210 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 211 | ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 212 | ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 213 | memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 214 | } else if (res != sizeof(ctx)) { |
| 215 | return -EINVAL; |
| 216 | } |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 217 | |
| 218 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) |
| 222 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 223 | |
| 224 | crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS); |
| 225 | if (!crypt_info) |
| 226 | return -ENOMEM; |
| 227 | |
| 228 | crypt_info->ci_flags = ctx.flags; |
| 229 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 230 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
| 231 | crypt_info->ci_ctfm = NULL; |
| 232 | crypt_info->ci_keyring_key = NULL; |
| 233 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, |
| 234 | sizeof(crypt_info->ci_master_key)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 235 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 236 | res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize); |
| 237 | if (res) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 238 | goto out; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 239 | |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 240 | /* |
| 241 | * This cannot be a stack buffer because it is passed to the scatterlist |
| 242 | * crypto API as part of key derivation. |
| 243 | */ |
| 244 | res = -ENOMEM; |
| 245 | raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS); |
| 246 | if (!raw_key) |
| 247 | goto out; |
| 248 | |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 249 | res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 250 | if (res && inode->i_sb->s_cop->key_prefix) { |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 251 | int res2 = validate_user_key(crypt_info, &ctx, raw_key, |
| 252 | inode->i_sb->s_cop->key_prefix); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 253 | if (res2) { |
| 254 | if (res2 == -ENOKEY) |
| 255 | res = -ENOKEY; |
| 256 | goto out; |
| 257 | } |
| 258 | } else if (res) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 259 | goto out; |
| 260 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 261 | ctfm = crypto_alloc_skcipher(cipher_str, 0, 0); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 262 | if (!ctfm || IS_ERR(ctfm)) { |
| 263 | res = ctfm ? PTR_ERR(ctfm) : -ENOMEM; |
| 264 | printk(KERN_DEBUG |
| 265 | "%s: error %d (inode %u) allocating crypto tfm\n", |
| 266 | __func__, res, (unsigned) inode->i_ino); |
| 267 | goto out; |
| 268 | } |
| 269 | crypt_info->ci_ctfm = ctfm; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 270 | crypto_skcipher_clear_flags(ctfm, ~0); |
| 271 | crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 272 | res = crypto_skcipher_setkey(ctfm, raw_key, keysize); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 273 | if (res) |
| 274 | goto out; |
| 275 | |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 276 | kzfree(raw_key); |
| 277 | raw_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 278 | if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) != NULL) { |
| 279 | put_crypt_info(crypt_info); |
| 280 | goto retry; |
| 281 | } |
| 282 | return 0; |
| 283 | |
| 284 | out: |
| 285 | if (res == -ENOKEY) |
| 286 | res = 0; |
| 287 | put_crypt_info(crypt_info); |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 288 | kzfree(raw_key); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 289 | return res; |
| 290 | } |
| 291 | |
| 292 | void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci) |
| 293 | { |
| 294 | struct fscrypt_info *prev; |
| 295 | |
| 296 | if (ci == NULL) |
| 297 | ci = ACCESS_ONCE(inode->i_crypt_info); |
| 298 | if (ci == NULL) |
| 299 | return; |
| 300 | |
| 301 | prev = cmpxchg(&inode->i_crypt_info, ci, NULL); |
| 302 | if (prev != ci) |
| 303 | return; |
| 304 | |
| 305 | put_crypt_info(ci); |
| 306 | } |
| 307 | EXPORT_SYMBOL(fscrypt_put_encryption_info); |
| 308 | |
| 309 | int fscrypt_get_encryption_info(struct inode *inode) |
| 310 | { |
| 311 | struct fscrypt_info *ci = inode->i_crypt_info; |
| 312 | |
| 313 | if (!ci || |
| 314 | (ci->ci_keyring_key && |
| 315 | (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) | |
| 316 | (1 << KEY_FLAG_REVOKED) | |
| 317 | (1 << KEY_FLAG_DEAD))))) |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 318 | return fscrypt_get_crypt_info(inode); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | EXPORT_SYMBOL(fscrypt_get_encryption_info); |