blob: a755fa1a00175a864daa1399576f9b9c2c185a68 [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>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070013#include <linux/fscrypto.h>
14
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,
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 Biggers2984e522017-02-21 15:07:11 -0800102 down_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700103
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 Kimb5a7aef2016-05-04 22:05:01 -0700110 ukp = user_key_payload(keyring_key);
Eric Biggersb2ac5d42017-10-09 12:46:18 -0700111 if (!ukp) {
112 /* key was revoked before we acquired its semaphore */
113 res = -EKEYREVOKED;
114 goto out;
115 }
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700116 if (ukp->datalen != sizeof(struct fscrypt_key)) {
117 res = -EINVAL;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700118 goto out;
119 }
120 master_key = (struct fscrypt_key *)ukp->data;
121 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
122
123 if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
124 printk_once(KERN_WARNING
125 "%s: key size incorrect: %d\n",
126 __func__, master_key->size);
127 res = -ENOKEY;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700128 goto out;
129 }
130 res = derive_key_aes(ctx->nonce, master_key->raw, raw_key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700131out:
Eric Biggers2984e522017-02-21 15:07:11 -0800132 up_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700133 key_put(keyring_key);
134 return res;
135}
136
Eric Biggers8f398502016-09-15 13:32:11 -0400137static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
138 const char **cipher_str_ret, int *keysize_ret)
139{
140 if (S_ISREG(inode->i_mode)) {
141 if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) {
142 *cipher_str_ret = "xts(aes)";
143 *keysize_ret = FS_AES_256_XTS_KEY_SIZE;
144 return 0;
145 }
146 pr_warn_once("fscrypto: unsupported contents encryption mode "
147 "%d for inode %lu\n",
148 ci->ci_data_mode, inode->i_ino);
149 return -ENOKEY;
150 }
151
152 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
153 if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) {
154 *cipher_str_ret = "cts(cbc(aes))";
155 *keysize_ret = FS_AES_256_CTS_KEY_SIZE;
156 return 0;
157 }
158 pr_warn_once("fscrypto: unsupported filenames encryption mode "
159 "%d for inode %lu\n",
160 ci->ci_filename_mode, inode->i_ino);
161 return -ENOKEY;
162 }
163
164 pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n",
165 (inode->i_mode & S_IFMT), inode->i_ino);
166 return -ENOKEY;
167}
168
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700169static void put_crypt_info(struct fscrypt_info *ci)
170{
171 if (!ci)
172 return;
173
Linus Torvaldsd4075742016-03-21 11:03:02 -0700174 crypto_free_skcipher(ci->ci_ctfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700175 kmem_cache_free(fscrypt_info_cachep, ci);
176}
177
Eric Biggers2984e522017-02-21 15:07:11 -0800178int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700179{
180 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700181 struct fscrypt_context ctx;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700182 struct crypto_skcipher *ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700183 const char *cipher_str;
Eric Biggers8f398502016-09-15 13:32:11 -0400184 int keysize;
Eric Biggers0f0909e2016-11-13 20:41:09 -0500185 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700186 int res;
187
Eric Biggers2984e522017-02-21 15:07:11 -0800188 if (inode->i_crypt_info)
189 return 0;
190
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700191 res = fscrypt_initialize();
192 if (res)
193 return res;
194
195 if (!inode->i_sb->s_cop->get_context)
196 return -EOPNOTSUPP;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700197
198 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
199 if (res < 0) {
200 if (!fscrypt_dummy_context_enabled(inode))
201 return res;
Eric Biggers8f398502016-09-15 13:32:11 -0400202 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700203 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
204 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
205 ctx.flags = 0;
206 } else if (res != sizeof(ctx)) {
207 return -EINVAL;
208 }
Eric Biggers8f398502016-09-15 13:32:11 -0400209
210 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
211 return -EINVAL;
212
213 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
214 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700215
216 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
217 if (!crypt_info)
218 return -ENOMEM;
219
220 crypt_info->ci_flags = ctx.flags;
221 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
222 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
223 crypt_info->ci_ctfm = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700224 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
225 sizeof(crypt_info->ci_master_key));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700226
Eric Biggers8f398502016-09-15 13:32:11 -0400227 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
228 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700229 goto out;
Eric Biggers8f398502016-09-15 13:32:11 -0400230
Eric Biggers0f0909e2016-11-13 20:41:09 -0500231 /*
232 * This cannot be a stack buffer because it is passed to the scatterlist
233 * crypto API as part of key derivation.
234 */
235 res = -ENOMEM;
236 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
237 if (!raw_key)
238 goto out;
239
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700240 if (fscrypt_dummy_context_enabled(inode)) {
241 memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
242 goto got_key;
243 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700244
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700245 res = validate_user_key(crypt_info, &ctx, raw_key,
246 FS_KEY_DESC_PREFIX, FS_KEY_DESC_PREFIX_SIZE);
247 if (res && inode->i_sb->s_cop->key_prefix) {
248 u8 *prefix = NULL;
249 int prefix_size, res2;
250
251 prefix_size = inode->i_sb->s_cop->key_prefix(inode, &prefix);
252 res2 = validate_user_key(crypt_info, &ctx, raw_key,
253 prefix, prefix_size);
254 if (res2) {
255 if (res2 == -ENOKEY)
256 res = -ENOKEY;
257 goto out;
258 }
259 } else if (res) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700260 goto out;
261 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700262got_key:
Linus Torvaldsd4075742016-03-21 11:03:02 -0700263 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700264 if (!ctfm || IS_ERR(ctfm)) {
265 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
266 printk(KERN_DEBUG
267 "%s: error %d (inode %u) allocating crypto tfm\n",
268 __func__, res, (unsigned) inode->i_ino);
269 goto out;
270 }
271 crypt_info->ci_ctfm = ctfm;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700272 crypto_skcipher_clear_flags(ctfm, ~0);
273 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
Eric Biggers8f398502016-09-15 13:32:11 -0400274 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700275 if (res)
276 goto out;
277
Eric Biggers2984e522017-02-21 15:07:11 -0800278 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
279 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700280out:
281 if (res == -ENOKEY)
282 res = 0;
283 put_crypt_info(crypt_info);
Eric Biggers0f0909e2016-11-13 20:41:09 -0500284 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700285 return res;
286}
Eric Biggers2984e522017-02-21 15:07:11 -0800287EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700288
289void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
290{
291 struct fscrypt_info *prev;
292
293 if (ci == NULL)
294 ci = ACCESS_ONCE(inode->i_crypt_info);
295 if (ci == NULL)
296 return;
297
298 prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
299 if (prev != ci)
300 return;
301
302 put_crypt_info(ci);
303}
304EXPORT_SYMBOL(fscrypt_put_encryption_info);