blob: cb3e82abf0347b9d395bb167955c9e92801d1b41 [file] [log] [blame]
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001/*
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 Kim0b81d072015-05-15 16:26:10 -070011#include <keys/user-type.h>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070012#include <linux/scatterlist.h>
Theodore Ts'o3325bea2016-11-26 20:32:46 -050013#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070014
15static 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 */
34static 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 Torvaldsd4075742016-03-21 11:03:02 -070039 struct skcipher_request *req = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070040 DECLARE_FS_COMPLETION_RESULT(ecr);
41 struct scatterlist src_sg, dst_sg;
Linus Torvaldsd4075742016-03-21 11:03:02 -070042 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070043
44 if (IS_ERR(tfm)) {
45 res = PTR_ERR(tfm);
46 tfm = NULL;
47 goto out;
48 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070049 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
50 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070051 if (!req) {
52 res = -ENOMEM;
53 goto out;
54 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070055 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070056 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
57 derive_crypt_complete, &ecr);
Linus Torvaldsd4075742016-03-21 11:03:02 -070058 res = crypto_skcipher_setkey(tfm, deriving_key,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070059 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 Torvaldsd4075742016-03-21 11:03:02 -070065 skcipher_request_set_crypt(req, &src_sg, &dst_sg,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070066 FS_AES_256_XTS_KEY_SIZE, NULL);
Linus Torvaldsd4075742016-03-21 11:03:02 -070067 res = crypto_skcipher_encrypt(req);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070068 if (res == -EINPROGRESS || res == -EBUSY) {
69 wait_for_completion(&ecr.completion);
70 res = ecr.res;
71 }
72out:
Linus Torvaldsd4075742016-03-21 11:03:02 -070073 skcipher_request_free(req);
74 crypto_free_skcipher(tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070075 return res;
76}
77
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070078static int validate_user_key(struct fscrypt_info *crypt_info,
79 struct fscrypt_context *ctx, u8 *raw_key,
Eric Biggersa5d431e2017-01-05 13:51:18 -080080 const char *prefix)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070081{
Eric Biggersa5d431e2017-01-05 13:51:18 -080082 char *description;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070083 struct key *keyring_key;
84 struct fscrypt_key *master_key;
85 const struct user_key_payload *ukp;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070086 int res;
87
Eric Biggersa5d431e2017-01-05 13:51:18 -080088 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
89 FS_KEY_DESCRIPTOR_SIZE,
90 ctx->master_key_descriptor);
91 if (!description)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070092 return -ENOMEM;
93
Eric Biggersa5d431e2017-01-05 13:51:18 -080094 keyring_key = request_key(&key_type_logon, description, NULL);
95 kfree(description);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070096 if (IS_ERR(keyring_key))
97 return PTR_ERR(keyring_key);
Eric Biggers1b53cf92017-02-21 15:07:11 -080098 down_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070099
100 if (keyring_key->type != &key_type_logon) {
101 printk_once(KERN_WARNING
102 "%s: key type must be logon\n", __func__);
103 res = -ENOKEY;
104 goto out;
105 }
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700106 ukp = user_key_payload(keyring_key);
107 if (ukp->datalen != sizeof(struct fscrypt_key)) {
108 res = -EINVAL;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700109 goto out;
110 }
111 master_key = (struct fscrypt_key *)ukp->data;
112 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
113
114 if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
115 printk_once(KERN_WARNING
116 "%s: key size incorrect: %d\n",
117 __func__, master_key->size);
118 res = -ENOKEY;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700119 goto out;
120 }
121 res = derive_key_aes(ctx->nonce, master_key->raw, raw_key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700122out:
Eric Biggers1b53cf92017-02-21 15:07:11 -0800123 up_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700124 key_put(keyring_key);
125 return res;
126}
127
Eric Biggers8f398502016-09-15 13:32:11 -0400128static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
129 const char **cipher_str_ret, int *keysize_ret)
130{
131 if (S_ISREG(inode->i_mode)) {
132 if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) {
133 *cipher_str_ret = "xts(aes)";
134 *keysize_ret = FS_AES_256_XTS_KEY_SIZE;
135 return 0;
136 }
137 pr_warn_once("fscrypto: unsupported contents encryption mode "
138 "%d for inode %lu\n",
139 ci->ci_data_mode, inode->i_ino);
140 return -ENOKEY;
141 }
142
143 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
144 if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) {
145 *cipher_str_ret = "cts(cbc(aes))";
146 *keysize_ret = FS_AES_256_CTS_KEY_SIZE;
147 return 0;
148 }
149 pr_warn_once("fscrypto: unsupported filenames encryption mode "
150 "%d for inode %lu\n",
151 ci->ci_filename_mode, inode->i_ino);
152 return -ENOKEY;
153 }
154
155 pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n",
156 (inode->i_mode & S_IFMT), inode->i_ino);
157 return -ENOKEY;
158}
159
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700160static void put_crypt_info(struct fscrypt_info *ci)
161{
162 if (!ci)
163 return;
164
Linus Torvaldsd4075742016-03-21 11:03:02 -0700165 crypto_free_skcipher(ci->ci_ctfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700166 kmem_cache_free(fscrypt_info_cachep, ci);
167}
168
Eric Biggers1b53cf92017-02-21 15:07:11 -0800169int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700170{
171 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700172 struct fscrypt_context ctx;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700173 struct crypto_skcipher *ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700174 const char *cipher_str;
Eric Biggers8f398502016-09-15 13:32:11 -0400175 int keysize;
Eric Biggersa6e08912016-11-13 20:41:09 -0500176 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700177 int res;
178
Eric Biggers1b53cf92017-02-21 15:07:11 -0800179 if (inode->i_crypt_info)
180 return 0;
181
David Gstirf32d7ac2016-12-06 23:53:57 +0100182 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700183 if (res)
184 return res;
185
186 if (!inode->i_sb->s_cop->get_context)
187 return -EOPNOTSUPP;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700188
189 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
190 if (res < 0) {
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500191 if (!fscrypt_dummy_context_enabled(inode) ||
192 inode->i_sb->s_cop->is_encrypted(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700193 return res;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500194 /* Fake up a context for an unencrypted directory */
195 memset(&ctx, 0, sizeof(ctx));
Eric Biggers8f398502016-09-15 13:32:11 -0400196 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700197 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
198 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500199 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700200 } else if (res != sizeof(ctx)) {
201 return -EINVAL;
202 }
Eric Biggers8f398502016-09-15 13:32:11 -0400203
204 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
205 return -EINVAL;
206
207 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
208 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700209
210 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
211 if (!crypt_info)
212 return -ENOMEM;
213
214 crypt_info->ci_flags = ctx.flags;
215 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
216 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
217 crypt_info->ci_ctfm = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700218 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
219 sizeof(crypt_info->ci_master_key));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700220
Eric Biggers8f398502016-09-15 13:32:11 -0400221 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
222 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700223 goto out;
Eric Biggers8f398502016-09-15 13:32:11 -0400224
Eric Biggersa6e08912016-11-13 20:41:09 -0500225 /*
226 * This cannot be a stack buffer because it is passed to the scatterlist
227 * crypto API as part of key derivation.
228 */
229 res = -ENOMEM;
230 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
231 if (!raw_key)
232 goto out;
233
Eric Biggersa5d431e2017-01-05 13:51:18 -0800234 res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700235 if (res && inode->i_sb->s_cop->key_prefix) {
Eric Biggersa5d431e2017-01-05 13:51:18 -0800236 int res2 = validate_user_key(crypt_info, &ctx, raw_key,
237 inode->i_sb->s_cop->key_prefix);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700238 if (res2) {
239 if (res2 == -ENOKEY)
240 res = -ENOKEY;
241 goto out;
242 }
243 } else if (res) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700244 goto out;
245 }
Linus Torvaldsd4075742016-03-21 11:03:02 -0700246 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700247 if (!ctfm || IS_ERR(ctfm)) {
248 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
249 printk(KERN_DEBUG
250 "%s: error %d (inode %u) allocating crypto tfm\n",
251 __func__, res, (unsigned) inode->i_ino);
252 goto out;
253 }
254 crypt_info->ci_ctfm = ctfm;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700255 crypto_skcipher_clear_flags(ctfm, ~0);
256 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
Eric Biggers8f398502016-09-15 13:32:11 -0400257 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700258 if (res)
259 goto out;
260
Eric Biggers1b53cf92017-02-21 15:07:11 -0800261 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
262 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700263out:
264 if (res == -ENOKEY)
265 res = 0;
266 put_crypt_info(crypt_info);
Eric Biggersa6e08912016-11-13 20:41:09 -0500267 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700268 return res;
269}
Eric Biggers1b53cf92017-02-21 15:07:11 -0800270EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700271
272void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
273{
274 struct fscrypt_info *prev;
275
276 if (ci == NULL)
277 ci = ACCESS_ONCE(inode->i_crypt_info);
278 if (ci == NULL)
279 return;
280
281 prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
282 if (prev != ci)
283 return;
284
285 put_crypt_info(ci);
286}
287EXPORT_SYMBOL(fscrypt_put_encryption_info);