1 /* Large capacity key type
2 *
3 * Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
11 */
12
13 #define pr_fmt(fmt) "big_key: "fmt
14 #include <linux/init.h>
15 #include <linux/seq_file.h>
16 #include <linux/file.h>
17 #include <linux/shmem_fs.h>
18 #include <linux/err.h>
19 #include <linux/scatterlist.h>
20 #include <linux/random.h>
21 #include <keys/user-type.h>
22 #include <keys/big_key-type.h>
23 #include <crypto/aead.h>
24
25 /*
26 * Layout of key payload words.
27 */
28 enum {
29 big_key_data,
30 big_key_path,
31 big_key_path_2nd_part,
32 big_key_len,
33 };
34
35 /*
36 * Crypto operation with big_key data
37 */
38 enum big_key_op {
39 BIG_KEY_ENC,
40 BIG_KEY_DEC,
41 };
42
43 /*
44 * If the data is under this limit, there's no point creating a shm file to
45 * hold it as the permanently resident metadata for the shmem fs will be at
46 * least as large as the data.
47 */
48 #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
49
50 /*
51 * Key size for big_key data encryption
52 */
53 #define ENC_KEY_SIZE 32
54
55 /*
56 * Authentication tag length
57 */
58 #define ENC_AUTHTAG_SIZE 16
59
60 /*
61 * big_key defined keys take an arbitrary string as the description and an
62 * arbitrary blob of data as the payload
63 */
64 struct key_type key_type_big_key = {
65 .name = "big_key",
66 .preparse = big_key_preparse,
67 .free_preparse = big_key_free_preparse,
68 .instantiate = generic_key_instantiate,
69 .revoke = big_key_revoke,
70 .destroy = big_key_destroy,
71 .describe = big_key_describe,
72 .read = big_key_read,
73 /* no ->update(); don't add it without changing big_key_crypt() nonce */
74 };
75
76 /*
77 * Crypto names for big_key data authenticated encryption
78 */
79 static const char big_key_alg_name[] = "gcm(aes)";
80
81 /*
82 * Crypto algorithms for big_key data authenticated encryption
83 */
84 static struct crypto_aead *big_key_aead;
85
86 /*
87 * Since changing the key affects the entire object, we need a mutex.
88 */
89 static DEFINE_MUTEX(big_key_aead_lock);
90
91 /*
92 * Encrypt/decrypt big_key data
93 */
big_key_crypt(enum big_key_op op,u8 * data,size_t datalen,u8 * key)94 static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
95 {
96 int ret;
97 struct scatterlist sgio;
98 struct aead_request *aead_req;
99 /* We always use a zero nonce. The reason we can get away with this is
100 * because we're using a different randomly generated key for every
101 * different encryption. Notably, too, key_type_big_key doesn't define
102 * an .update function, so there's no chance we'll wind up reusing the
103 * key to encrypt updated data. Simply put: one key, one encryption.
104 */
105 u8 zero_nonce[crypto_aead_ivsize(big_key_aead)];
106
107 aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL);
108 if (!aead_req)
109 return -ENOMEM;
110
111 memset(zero_nonce, 0, sizeof(zero_nonce));
112 sg_init_one(&sgio, data, datalen + (op == BIG_KEY_ENC ? ENC_AUTHTAG_SIZE : 0));
113 aead_request_set_crypt(aead_req, &sgio, &sgio, datalen, zero_nonce);
114 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
115 aead_request_set_ad(aead_req, 0);
116
117 mutex_lock(&big_key_aead_lock);
118 if (crypto_aead_setkey(big_key_aead, key, ENC_KEY_SIZE)) {
119 ret = -EAGAIN;
120 goto error;
121 }
122 if (op == BIG_KEY_ENC)
123 ret = crypto_aead_encrypt(aead_req);
124 else
125 ret = crypto_aead_decrypt(aead_req);
126 error:
127 mutex_unlock(&big_key_aead_lock);
128 aead_request_free(aead_req);
129 return ret;
130 }
131
132 /*
133 * Preparse a big key
134 */
big_key_preparse(struct key_preparsed_payload * prep)135 int big_key_preparse(struct key_preparsed_payload *prep)
136 {
137 struct path *path = (struct path *)&prep->payload.data[big_key_path];
138 struct file *file;
139 u8 *enckey;
140 u8 *data = NULL;
141 ssize_t written;
142 size_t datalen = prep->datalen;
143 int ret;
144
145 ret = -EINVAL;
146 if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
147 goto error;
148
149 /* Set an arbitrary quota */
150 prep->quotalen = 16;
151
152 prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
153
154 if (datalen > BIG_KEY_FILE_THRESHOLD) {
155 /* Create a shmem file to store the data in. This will permit the data
156 * to be swapped out if needed.
157 *
158 * File content is stored encrypted with randomly generated key.
159 */
160 size_t enclen = datalen + ENC_AUTHTAG_SIZE;
161
162 data = kmalloc(enclen, GFP_KERNEL);
163 if (!data)
164 return -ENOMEM;
165
166 memcpy(data, prep->data, datalen);
167
168 /* generate random key */
169 enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
170 if (!enckey) {
171 ret = -ENOMEM;
172 goto error;
173 }
174 get_random_bytes(enckey, ENC_KEY_SIZE);
175
176 /* encrypt aligned data */
177 ret = big_key_crypt(BIG_KEY_ENC, data, datalen, enckey);
178 if (ret)
179 goto err_enckey;
180
181 /* save aligned data to file */
182 file = shmem_kernel_file_setup("", enclen, 0);
183 if (IS_ERR(file)) {
184 ret = PTR_ERR(file);
185 goto err_enckey;
186 }
187
188 written = kernel_write(file, data, enclen, 0);
189 if (written != enclen) {
190 ret = written;
191 if (written >= 0)
192 ret = -ENOMEM;
193 goto err_fput;
194 }
195
196 /* Pin the mount and dentry to the key so that we can open it again
197 * later
198 */
199 prep->payload.data[big_key_data] = enckey;
200 *path = file->f_path;
201 path_get(path);
202 fput(file);
203 kzfree(data);
204 } else {
205 /* Just store the data in a buffer */
206 void *data = kmalloc(datalen, GFP_KERNEL);
207
208 if (!data)
209 return -ENOMEM;
210
211 prep->payload.data[big_key_data] = data;
212 memcpy(data, prep->data, prep->datalen);
213 }
214 return 0;
215
216 err_fput:
217 fput(file);
218 err_enckey:
219 kzfree(enckey);
220 error:
221 kzfree(data);
222 return ret;
223 }
224
225 /*
226 * Clear preparsement.
227 */
big_key_free_preparse(struct key_preparsed_payload * prep)228 void big_key_free_preparse(struct key_preparsed_payload *prep)
229 {
230 if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
231 struct path *path = (struct path *)&prep->payload.data[big_key_path];
232
233 path_put(path);
234 }
235 kzfree(prep->payload.data[big_key_data]);
236 }
237
238 /*
239 * dispose of the links from a revoked keyring
240 * - called with the key sem write-locked
241 */
big_key_revoke(struct key * key)242 void big_key_revoke(struct key *key)
243 {
244 struct path *path = (struct path *)&key->payload.data[big_key_path];
245
246 /* clear the quota */
247 key_payload_reserve(key, 0);
248 if (key_is_positive(key) &&
249 (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
250 vfs_truncate(path, 0);
251 }
252
253 /*
254 * dispose of the data dangling from the corpse of a big_key key
255 */
big_key_destroy(struct key * key)256 void big_key_destroy(struct key *key)
257 {
258 size_t datalen = (size_t)key->payload.data[big_key_len];
259
260 if (datalen > BIG_KEY_FILE_THRESHOLD) {
261 struct path *path = (struct path *)&key->payload.data[big_key_path];
262
263 path_put(path);
264 path->mnt = NULL;
265 path->dentry = NULL;
266 }
267 kzfree(key->payload.data[big_key_data]);
268 key->payload.data[big_key_data] = NULL;
269 }
270
271 /*
272 * describe the big_key key
273 */
big_key_describe(const struct key * key,struct seq_file * m)274 void big_key_describe(const struct key *key, struct seq_file *m)
275 {
276 size_t datalen = (size_t)key->payload.data[big_key_len];
277
278 seq_puts(m, key->description);
279
280 if (key_is_positive(key))
281 seq_printf(m, ": %zu [%s]",
282 datalen,
283 datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
284 }
285
286 /*
287 * read the key data
288 * - the key's semaphore is read-locked
289 */
big_key_read(const struct key * key,char __user * buffer,size_t buflen)290 long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
291 {
292 size_t datalen = (size_t)key->payload.data[big_key_len];
293 long ret;
294
295 if (!buffer || buflen < datalen)
296 return datalen;
297
298 if (datalen > BIG_KEY_FILE_THRESHOLD) {
299 struct path *path = (struct path *)&key->payload.data[big_key_path];
300 struct file *file;
301 u8 *data;
302 u8 *enckey = (u8 *)key->payload.data[big_key_data];
303 size_t enclen = datalen + ENC_AUTHTAG_SIZE;
304
305 data = kmalloc(enclen, GFP_KERNEL);
306 if (!data)
307 return -ENOMEM;
308
309 file = dentry_open(path, O_RDONLY, current_cred());
310 if (IS_ERR(file)) {
311 ret = PTR_ERR(file);
312 goto error;
313 }
314
315 /* read file to kernel and decrypt */
316 ret = kernel_read(file, 0, data, enclen);
317 if (ret >= 0 && ret != enclen) {
318 ret = -EIO;
319 goto err_fput;
320 }
321
322 ret = big_key_crypt(BIG_KEY_DEC, data, enclen, enckey);
323 if (ret)
324 goto err_fput;
325
326 ret = datalen;
327
328 /* copy decrypted data to user */
329 if (copy_to_user(buffer, data, datalen) != 0)
330 ret = -EFAULT;
331
332 err_fput:
333 fput(file);
334 error:
335 kzfree(data);
336 } else {
337 ret = datalen;
338 if (copy_to_user(buffer, key->payload.data[big_key_data],
339 datalen) != 0)
340 ret = -EFAULT;
341 }
342
343 return ret;
344 }
345
346 /*
347 * Register key type
348 */
big_key_init(void)349 static int __init big_key_init(void)
350 {
351 int ret;
352
353 /* init block cipher */
354 big_key_aead = crypto_alloc_aead(big_key_alg_name, 0, CRYPTO_ALG_ASYNC);
355 if (IS_ERR(big_key_aead)) {
356 ret = PTR_ERR(big_key_aead);
357 pr_err("Can't alloc crypto: %d\n", ret);
358 return ret;
359 }
360 ret = crypto_aead_setauthsize(big_key_aead, ENC_AUTHTAG_SIZE);
361 if (ret < 0) {
362 pr_err("Can't set crypto auth tag len: %d\n", ret);
363 goto free_aead;
364 }
365
366 ret = register_key_type(&key_type_big_key);
367 if (ret < 0) {
368 pr_err("Can't register type: %d\n", ret);
369 goto free_aead;
370 }
371
372 return 0;
373
374 free_aead:
375 crypto_free_aead(big_key_aead);
376 return ret;
377 }
378
379 late_initcall(big_key_init);
380