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