Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext4/crypto_key.c |
| 3 | * |
| 4 | * Copyright (C) 2015, Google, Inc. |
| 5 | * |
| 6 | * This contains encryption key functions for ext4 |
| 7 | * |
| 8 | * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. |
| 9 | */ |
| 10 | |
| 11 | #include <keys/encrypted-type.h> |
| 12 | #include <keys/user-type.h> |
| 13 | #include <linux/random.h> |
| 14 | #include <linux/scatterlist.h> |
| 15 | #include <uapi/linux/keyctl.h> |
| 16 | |
| 17 | #include "ext4.h" |
| 18 | #include "xattr.h" |
| 19 | |
| 20 | static void derive_crypt_complete(struct crypto_async_request *req, int rc) |
| 21 | { |
| 22 | struct ext4_completion_result *ecr = req->data; |
| 23 | |
| 24 | if (rc == -EINPROGRESS) |
| 25 | return; |
| 26 | |
| 27 | ecr->res = rc; |
| 28 | complete(&ecr->completion); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * ext4_derive_key_aes() - Derive a key using AES-128-ECB |
Laurent Navet | bb9a4e7 | 2015-07-22 00:09:45 -0400 | [diff] [blame] | 33 | * @deriving_key: Encryption key used for derivation. |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 34 | * @source_key: Source key to which to apply derivation. |
| 35 | * @derived_key: Derived key. |
| 36 | * |
| 37 | * Return: Zero on success; non-zero otherwise. |
| 38 | */ |
| 39 | static int ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE], |
| 40 | char source_key[EXT4_AES_256_XTS_KEY_SIZE], |
| 41 | char derived_key[EXT4_AES_256_XTS_KEY_SIZE]) |
| 42 | { |
| 43 | int res = 0; |
| 44 | struct ablkcipher_request *req = NULL; |
| 45 | DECLARE_EXT4_COMPLETION_RESULT(ecr); |
| 46 | struct scatterlist src_sg, dst_sg; |
| 47 | struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0, |
| 48 | 0); |
| 49 | |
| 50 | if (IS_ERR(tfm)) { |
| 51 | res = PTR_ERR(tfm); |
| 52 | tfm = NULL; |
| 53 | goto out; |
| 54 | } |
| 55 | crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 56 | req = ablkcipher_request_alloc(tfm, GFP_NOFS); |
| 57 | if (!req) { |
| 58 | res = -ENOMEM; |
| 59 | goto out; |
| 60 | } |
| 61 | ablkcipher_request_set_callback(req, |
| 62 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 63 | derive_crypt_complete, &ecr); |
| 64 | res = crypto_ablkcipher_setkey(tfm, deriving_key, |
| 65 | EXT4_AES_128_ECB_KEY_SIZE); |
| 66 | if (res < 0) |
| 67 | goto out; |
| 68 | sg_init_one(&src_sg, source_key, EXT4_AES_256_XTS_KEY_SIZE); |
| 69 | sg_init_one(&dst_sg, derived_key, EXT4_AES_256_XTS_KEY_SIZE); |
| 70 | ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, |
| 71 | EXT4_AES_256_XTS_KEY_SIZE, NULL); |
| 72 | res = crypto_ablkcipher_encrypt(req); |
| 73 | if (res == -EINPROGRESS || res == -EBUSY) { |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 74 | wait_for_completion(&ecr.completion); |
| 75 | res = ecr.res; |
| 76 | } |
| 77 | |
| 78 | out: |
| 79 | if (req) |
| 80 | ablkcipher_request_free(req); |
| 81 | if (tfm) |
| 82 | crypto_free_ablkcipher(tfm); |
| 83 | return res; |
| 84 | } |
| 85 | |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 86 | void ext4_free_crypt_info(struct ext4_crypt_info *ci) |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 87 | { |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 88 | if (!ci) |
| 89 | return; |
| 90 | |
| 91 | if (ci->ci_keyring_key) |
| 92 | key_put(ci->ci_keyring_key); |
| 93 | crypto_free_ablkcipher(ci->ci_ctfm); |
Theodore Ts'o | 8ee03714 | 2015-05-18 13:19:47 -0400 | [diff] [blame] | 94 | kmem_cache_free(ext4_crypt_info_cachep, ci); |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ext4_free_encryption_info(struct inode *inode, |
| 98 | struct ext4_crypt_info *ci) |
| 99 | { |
| 100 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 101 | struct ext4_crypt_info *prev; |
| 102 | |
| 103 | if (ci == NULL) |
| 104 | ci = ACCESS_ONCE(ei->i_crypt_info); |
| 105 | if (ci == NULL) |
| 106 | return; |
| 107 | prev = cmpxchg(&ei->i_crypt_info, ci, NULL); |
| 108 | if (prev != ci) |
| 109 | return; |
| 110 | |
| 111 | ext4_free_crypt_info(ci); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int _ext4_get_encryption_info(struct inode *inode) |
| 115 | { |
| 116 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 117 | struct ext4_crypt_info *crypt_info; |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 118 | char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE + |
| 119 | (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1]; |
| 120 | struct key *keyring_key = NULL; |
| 121 | struct ext4_encryption_key *master_key; |
| 122 | struct ext4_encryption_context ctx; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 123 | const struct user_key_payload *ukp; |
Theodore Ts'o | 6ddb244 | 2015-04-16 01:56:00 -0400 | [diff] [blame] | 124 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 125 | struct crypto_ablkcipher *ctfm; |
| 126 | const char *cipher_str; |
| 127 | char raw_key[EXT4_MAX_KEY_SIZE]; |
| 128 | char mode; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 129 | int res; |
| 130 | |
Theodore Ts'o | 8ee03714 | 2015-05-18 13:19:47 -0400 | [diff] [blame] | 131 | if (!ext4_read_workqueue) { |
| 132 | res = ext4_init_crypto(); |
| 133 | if (res) |
| 134 | return res; |
| 135 | } |
| 136 | |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 137 | retry: |
| 138 | crypt_info = ACCESS_ONCE(ei->i_crypt_info); |
| 139 | if (crypt_info) { |
| 140 | if (!crypt_info->ci_keyring_key || |
| 141 | key_validate(crypt_info->ci_keyring_key) == 0) |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 142 | return 0; |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 143 | ext4_free_encryption_info(inode, crypt_info); |
| 144 | goto retry; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 148 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, |
| 149 | &ctx, sizeof(ctx)); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 150 | if (res < 0) { |
| 151 | if (!DUMMY_ENCRYPTION_ENABLED(sbi)) |
| 152 | return res; |
| 153 | ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS; |
| 154 | ctx.filenames_encryption_mode = |
| 155 | EXT4_ENCRYPTION_MODE_AES_256_CTS; |
| 156 | ctx.flags = 0; |
| 157 | } else if (res != sizeof(ctx)) |
| 158 | return -EINVAL; |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 159 | res = 0; |
| 160 | |
Theodore Ts'o | 8ee03714 | 2015-05-18 13:19:47 -0400 | [diff] [blame] | 161 | crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 162 | if (!crypt_info) |
| 163 | return -ENOMEM; |
| 164 | |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 165 | crypt_info->ci_flags = ctx.flags; |
| 166 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 167 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
| 168 | crypt_info->ci_ctfm = NULL; |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 169 | crypt_info->ci_keyring_key = NULL; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 170 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, |
| 171 | sizeof(crypt_info->ci_master_key)); |
Theodore Ts'o | 6ddb244 | 2015-04-16 01:56:00 -0400 | [diff] [blame] | 172 | if (S_ISREG(inode->i_mode)) |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 173 | mode = crypt_info->ci_data_mode; |
Theodore Ts'o | 6ddb244 | 2015-04-16 01:56:00 -0400 | [diff] [blame] | 174 | else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 175 | mode = crypt_info->ci_filename_mode; |
Theodore Ts'o | 1aaa6e8 | 2015-05-18 13:20:47 -0400 | [diff] [blame] | 176 | else |
Theodore Ts'o | 6ddb244 | 2015-04-16 01:56:00 -0400 | [diff] [blame] | 177 | BUG(); |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 178 | switch (mode) { |
| 179 | case EXT4_ENCRYPTION_MODE_AES_256_XTS: |
| 180 | cipher_str = "xts(aes)"; |
| 181 | break; |
| 182 | case EXT4_ENCRYPTION_MODE_AES_256_CTS: |
| 183 | cipher_str = "cts(cbc(aes))"; |
| 184 | break; |
| 185 | default: |
| 186 | printk_once(KERN_WARNING |
| 187 | "ext4: unsupported key mode %d (ino %u)\n", |
| 188 | mode, (unsigned) inode->i_ino); |
| 189 | res = -ENOKEY; |
Theodore Ts'o | 6ddb244 | 2015-04-16 01:56:00 -0400 | [diff] [blame] | 190 | goto out; |
| 191 | } |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 192 | if (DUMMY_ENCRYPTION_ENABLED(sbi)) { |
| 193 | memset(raw_key, 0x42, EXT4_AES_256_XTS_KEY_SIZE); |
| 194 | goto got_key; |
| 195 | } |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 196 | memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX, |
| 197 | EXT4_KEY_DESC_PREFIX_SIZE); |
| 198 | sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE, |
| 199 | "%*phN", EXT4_KEY_DESCRIPTOR_SIZE, |
| 200 | ctx.master_key_descriptor); |
| 201 | full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE + |
| 202 | (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0'; |
| 203 | keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL); |
| 204 | if (IS_ERR(keyring_key)) { |
| 205 | res = PTR_ERR(keyring_key); |
| 206 | keyring_key = NULL; |
| 207 | goto out; |
| 208 | } |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 209 | crypt_info->ci_keyring_key = keyring_key; |
Theodore Ts'o | 687c3c3 | 2015-10-03 10:49:27 -0400 | [diff] [blame] | 210 | if (keyring_key->type != &key_type_logon) { |
| 211 | printk_once(KERN_WARNING |
| 212 | "ext4: key type must be logon\n"); |
| 213 | res = -ENOKEY; |
| 214 | goto out; |
| 215 | } |
Theodore Ts'o | db7730e | 2015-12-10 00:57:58 -0500 | [diff] [blame] | 216 | down_read(&keyring_key->sem); |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 217 | ukp = user_key_payload(keyring_key); |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 218 | if (ukp->datalen != sizeof(struct ext4_encryption_key)) { |
| 219 | res = -EINVAL; |
Theodore Ts'o | db7730e | 2015-12-10 00:57:58 -0500 | [diff] [blame] | 220 | up_read(&keyring_key->sem); |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 221 | goto out; |
| 222 | } |
| 223 | master_key = (struct ext4_encryption_key *)ukp->data; |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 224 | BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE != |
| 225 | EXT4_KEY_DERIVATION_NONCE_SIZE); |
Theodore Ts'o | 687c3c3 | 2015-10-03 10:49:27 -0400 | [diff] [blame] | 226 | if (master_key->size != EXT4_AES_256_XTS_KEY_SIZE) { |
| 227 | printk_once(KERN_WARNING |
| 228 | "ext4: key size incorrect: %d\n", |
| 229 | master_key->size); |
| 230 | res = -ENOKEY; |
Theodore Ts'o | db7730e | 2015-12-10 00:57:58 -0500 | [diff] [blame] | 231 | up_read(&keyring_key->sem); |
Theodore Ts'o | 687c3c3 | 2015-10-03 10:49:27 -0400 | [diff] [blame] | 232 | goto out; |
| 233 | } |
Theodore Ts'o | e2881b1b | 2015-05-18 13:16:47 -0400 | [diff] [blame] | 234 | res = ext4_derive_key_aes(ctx.nonce, master_key->raw, |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 235 | raw_key); |
Theodore Ts'o | db7730e | 2015-12-10 00:57:58 -0500 | [diff] [blame] | 236 | up_read(&keyring_key->sem); |
Laurent Navet | d76d99b | 2015-07-22 00:08:08 -0400 | [diff] [blame] | 237 | if (res) |
| 238 | goto out; |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 239 | got_key: |
| 240 | ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0); |
| 241 | if (!ctfm || IS_ERR(ctfm)) { |
| 242 | res = ctfm ? PTR_ERR(ctfm) : -ENOMEM; |
| 243 | printk(KERN_DEBUG |
| 244 | "%s: error %d (inode %u) allocating crypto tfm\n", |
| 245 | __func__, res, (unsigned) inode->i_ino); |
| 246 | goto out; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 247 | } |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 248 | crypt_info->ci_ctfm = ctfm; |
| 249 | crypto_ablkcipher_clear_flags(ctfm, ~0); |
| 250 | crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm), |
| 251 | CRYPTO_TFM_REQ_WEAK_KEY); |
| 252 | res = crypto_ablkcipher_setkey(ctfm, raw_key, |
| 253 | ext4_encryption_key_size(mode)); |
| 254 | if (res) |
| 255 | goto out; |
| 256 | memzero_explicit(raw_key, sizeof(raw_key)); |
| 257 | if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) != NULL) { |
| 258 | ext4_free_crypt_info(crypt_info); |
| 259 | goto retry; |
| 260 | } |
| 261 | return 0; |
| 262 | |
| 263 | out: |
| 264 | if (res == -ENOKEY) |
| 265 | res = 0; |
| 266 | ext4_free_crypt_info(crypt_info); |
| 267 | memzero_explicit(raw_key, sizeof(raw_key)); |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 268 | return res; |
| 269 | } |
| 270 | |
| 271 | int ext4_has_encryption_key(struct inode *inode) |
| 272 | { |
| 273 | struct ext4_inode_info *ei = EXT4_I(inode); |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 274 | |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 275 | return (ei->i_crypt_info != NULL); |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 276 | } |