blob: e6288173d99de32aed964ee9ed61c6f47fd4b1a1 [file] [log] [blame]
David Howellsab3c3582013-09-24 10:35:18 +01001/* Large capacity key type
2 *
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +02003 * Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
David Howellsab3c3582013-09-24 10:35:18 +01004 * 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
David Howells7df3e592016-10-26 15:02:01 +010013#define pr_fmt(fmt) "big_key: "fmt
David Howellsab3c3582013-09-24 10:35:18 +010014#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>
Kirill Marinushkin13100a72016-04-12 19:54:58 +010019#include <linux/scatterlist.h>
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020020#include <linux/random.h>
David Howellsab3c3582013-09-24 10:35:18 +010021#include <keys/user-type.h>
22#include <keys/big_key-type.h>
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020023#include <crypto/aead.h>
David Howellsab3c3582013-09-24 10:35:18 +010024
David Howellsab3c3582013-09-24 10:35:18 +010025/*
David Howells146aa8b2015-10-21 14:04:48 +010026 * Layout of key payload words.
27 */
28enum {
29 big_key_data,
30 big_key_path,
31 big_key_path_2nd_part,
32 big_key_len,
33};
34
35/*
Kirill Marinushkin13100a72016-04-12 19:54:58 +010036 * Crypto operation with big_key data
37 */
38enum big_key_op {
39 BIG_KEY_ENC,
40 BIG_KEY_DEC,
41};
42
43/*
David Howellsab3c3582013-09-24 10:35:18 +010044 * 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/*
Kirill Marinushkin13100a72016-04-12 19:54:58 +010051 * Key size for big_key data encryption
52 */
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020053#define ENC_KEY_SIZE 32
54
55/*
56 * Authentication tag length
57 */
58#define ENC_AUTHTAG_SIZE 16
Kirill Marinushkin13100a72016-04-12 19:54:58 +010059
60/*
David Howellsab3c3582013-09-24 10:35:18 +010061 * big_key defined keys take an arbitrary string as the description and an
62 * arbitrary blob of data as the payload
63 */
64struct key_type key_type_big_key = {
65 .name = "big_key",
David Howells002edaf2014-07-18 18:56:36 +010066 .preparse = big_key_preparse,
67 .free_preparse = big_key_free_preparse,
68 .instantiate = generic_key_instantiate,
David Howellsab3c3582013-09-24 10:35:18 +010069 .revoke = big_key_revoke,
70 .destroy = big_key_destroy,
71 .describe = big_key_describe,
72 .read = big_key_read,
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020073 /* no ->update(); don't add it without changing big_key_crypt() nonce */
David Howellsab3c3582013-09-24 10:35:18 +010074};
75
76/*
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020077 * Crypto names for big_key data authenticated encryption
Kirill Marinushkin13100a72016-04-12 19:54:58 +010078 */
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020079static const char big_key_alg_name[] = "gcm(aes)";
Kirill Marinushkin13100a72016-04-12 19:54:58 +010080
81/*
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020082 * Crypto algorithms for big_key data authenticated encryption
Kirill Marinushkin13100a72016-04-12 19:54:58 +010083 */
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020084static struct crypto_aead *big_key_aead;
Kirill Marinushkin13100a72016-04-12 19:54:58 +010085
86/*
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020087 * Since changing the key affects the entire object, we need a mutex.
Kirill Marinushkin13100a72016-04-12 19:54:58 +010088 */
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020089static DEFINE_MUTEX(big_key_aead_lock);
Kirill Marinushkin13100a72016-04-12 19:54:58 +010090
91/*
92 * Encrypt/decrypt big_key data
93 */
94static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
95{
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020096 int ret;
Kirill Marinushkin13100a72016-04-12 19:54:58 +010097 struct scatterlist sgio;
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +020098 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)];
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100106
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200107 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)) {
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100119 ret = -EAGAIN;
120 goto error;
121 }
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100122 if (op == BIG_KEY_ENC)
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200123 ret = crypto_aead_encrypt(aead_req);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100124 else
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200125 ret = crypto_aead_decrypt(aead_req);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100126error:
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200127 mutex_unlock(&big_key_aead_lock);
128 aead_request_free(aead_req);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100129 return ret;
130}
131
132/*
David Howells002edaf2014-07-18 18:56:36 +0100133 * Preparse a big key
David Howellsab3c3582013-09-24 10:35:18 +0100134 */
David Howells002edaf2014-07-18 18:56:36 +0100135int big_key_preparse(struct key_preparsed_payload *prep)
David Howellsab3c3582013-09-24 10:35:18 +0100136{
David Howells146aa8b2015-10-21 14:04:48 +0100137 struct path *path = (struct path *)&prep->payload.data[big_key_path];
David Howellsab3c3582013-09-24 10:35:18 +0100138 struct file *file;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100139 u8 *enckey;
140 u8 *data = NULL;
David Howellsab3c3582013-09-24 10:35:18 +0100141 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 */
David Howells002edaf2014-07-18 18:56:36 +0100150 prep->quotalen = 16;
David Howellsab3c3582013-09-24 10:35:18 +0100151
David Howells146aa8b2015-10-21 14:04:48 +0100152 prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
David Howellsab3c3582013-09-24 10:35:18 +0100153
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 *
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100158 * File content is stored encrypted with randomly generated key.
David Howellsab3c3582013-09-24 10:35:18 +0100159 */
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200160 size_t enclen = datalen + ENC_AUTHTAG_SIZE;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100161
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100162 data = kmalloc(enclen, GFP_KERNEL);
163 if (!data)
164 return -ENOMEM;
165
166 memcpy(data, prep->data, datalen);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100167
168 /* generate random key */
169 enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
170 if (!enckey) {
171 ret = -ENOMEM;
David Howells002edaf2014-07-18 18:56:36 +0100172 goto error;
Wei Yongjund2b86972013-10-30 11:23:02 +0800173 }
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200174 get_random_bytes(enckey, ENC_KEY_SIZE);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100175
176 /* encrypt aligned data */
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200177 ret = big_key_crypt(BIG_KEY_ENC, data, datalen, enckey);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100178 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) {
David Howells97826c82013-11-13 16:51:06 +0000190 ret = written;
David Howellsab3c3582013-09-24 10:35:18 +0100191 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 */
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100199 prep->payload.data[big_key_data] = enckey;
David Howellsab3c3582013-09-24 10:35:18 +0100200 *path = file->f_path;
201 path_get(path);
202 fput(file);
Jason A. Donenfeld2f9be922017-09-20 16:58:38 +0200203 kzfree(data);
David Howellsab3c3582013-09-24 10:35:18 +0100204 } else {
205 /* Just store the data in a buffer */
206 void *data = kmalloc(datalen, GFP_KERNEL);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100207
David Howells002edaf2014-07-18 18:56:36 +0100208 if (!data)
209 return -ENOMEM;
David Howellsab3c3582013-09-24 10:35:18 +0100210
David Howells146aa8b2015-10-21 14:04:48 +0100211 prep->payload.data[big_key_data] = data;
212 memcpy(data, prep->data, prep->datalen);
David Howellsab3c3582013-09-24 10:35:18 +0100213 }
214 return 0;
215
216err_fput:
217 fput(file);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100218err_enckey:
Jason A. Donenfeld2f9be922017-09-20 16:58:38 +0200219 kzfree(enckey);
David Howellsab3c3582013-09-24 10:35:18 +0100220error:
Jason A. Donenfeld2f9be922017-09-20 16:58:38 +0200221 kzfree(data);
David Howellsab3c3582013-09-24 10:35:18 +0100222 return ret;
223}
224
225/*
David Howells002edaf2014-07-18 18:56:36 +0100226 * Clear preparsement.
227 */
228void big_key_free_preparse(struct key_preparsed_payload *prep)
229{
230 if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
David Howells146aa8b2015-10-21 14:04:48 +0100231 struct path *path = (struct path *)&prep->payload.data[big_key_path];
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100232
David Howells002edaf2014-07-18 18:56:36 +0100233 path_put(path);
David Howells002edaf2014-07-18 18:56:36 +0100234 }
Jason A. Donenfeld2f9be922017-09-20 16:58:38 +0200235 kzfree(prep->payload.data[big_key_data]);
David Howells002edaf2014-07-18 18:56:36 +0100236}
237
238/*
David Howellsab3c3582013-09-24 10:35:18 +0100239 * dispose of the links from a revoked keyring
240 * - called with the key sem write-locked
241 */
242void big_key_revoke(struct key *key)
243{
David Howells146aa8b2015-10-21 14:04:48 +0100244 struct path *path = (struct path *)&key->payload.data[big_key_path];
David Howellsab3c3582013-09-24 10:35:18 +0100245
246 /* clear the quota */
247 key_payload_reserve(key, 0);
David Howells63c8e452017-10-04 16:43:25 +0100248 if (key_is_positive(key) &&
David Howells146aa8b2015-10-21 14:04:48 +0100249 (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
David Howellsab3c3582013-09-24 10:35:18 +0100250 vfs_truncate(path, 0);
251}
252
253/*
254 * dispose of the data dangling from the corpse of a big_key key
255 */
256void big_key_destroy(struct key *key)
257{
David Howells146aa8b2015-10-21 14:04:48 +0100258 size_t datalen = (size_t)key->payload.data[big_key_len];
259
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100260 if (datalen > BIG_KEY_FILE_THRESHOLD) {
David Howells146aa8b2015-10-21 14:04:48 +0100261 struct path *path = (struct path *)&key->payload.data[big_key_path];
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100262
David Howellsab3c3582013-09-24 10:35:18 +0100263 path_put(path);
264 path->mnt = NULL;
265 path->dentry = NULL;
David Howellsab3c3582013-09-24 10:35:18 +0100266 }
Jason A. Donenfeld2f9be922017-09-20 16:58:38 +0200267 kzfree(key->payload.data[big_key_data]);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100268 key->payload.data[big_key_data] = NULL;
David Howellsab3c3582013-09-24 10:35:18 +0100269}
270
271/*
272 * describe the big_key key
273 */
274void big_key_describe(const struct key *key, struct seq_file *m)
275{
David Howells146aa8b2015-10-21 14:04:48 +0100276 size_t datalen = (size_t)key->payload.data[big_key_len];
David Howellsab3c3582013-09-24 10:35:18 +0100277
278 seq_puts(m, key->description);
279
David Howells63c8e452017-10-04 16:43:25 +0100280 if (key_is_positive(key))
David Howells146aa8b2015-10-21 14:04:48 +0100281 seq_printf(m, ": %zu [%s]",
David Howellsab3c3582013-09-24 10:35:18 +0100282 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 */
290long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
291{
David Howells146aa8b2015-10-21 14:04:48 +0100292 size_t datalen = (size_t)key->payload.data[big_key_len];
David Howellsab3c3582013-09-24 10:35:18 +0100293 long ret;
294
295 if (!buffer || buflen < datalen)
296 return datalen;
297
298 if (datalen > BIG_KEY_FILE_THRESHOLD) {
David Howells146aa8b2015-10-21 14:04:48 +0100299 struct path *path = (struct path *)&key->payload.data[big_key_path];
David Howellsab3c3582013-09-24 10:35:18 +0100300 struct file *file;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100301 u8 *data;
302 u8 *enckey = (u8 *)key->payload.data[big_key_data];
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200303 size_t enclen = datalen + ENC_AUTHTAG_SIZE;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100304
305 data = kmalloc(enclen, GFP_KERNEL);
306 if (!data)
307 return -ENOMEM;
David Howellsab3c3582013-09-24 10:35:18 +0100308
309 file = dentry_open(path, O_RDONLY, current_cred());
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100310 if (IS_ERR(file)) {
311 ret = PTR_ERR(file);
312 goto error;
313 }
David Howellsab3c3582013-09-24 10:35:18 +0100314
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100315 /* read file to kernel and decrypt */
316 ret = kernel_read(file, 0, data, enclen);
317 if (ret >= 0 && ret != enclen) {
David Howellsab3c3582013-09-24 10:35:18 +0100318 ret = -EIO;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100319 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
332err_fput:
333 fput(file);
334error:
Jason A. Donenfeld2f9be922017-09-20 16:58:38 +0200335 kzfree(data);
David Howellsab3c3582013-09-24 10:35:18 +0100336 } else {
337 ret = datalen;
David Howells146aa8b2015-10-21 14:04:48 +0100338 if (copy_to_user(buffer, key->payload.data[big_key_data],
339 datalen) != 0)
David Howellsab3c3582013-09-24 10:35:18 +0100340 ret = -EFAULT;
341 }
342
343 return ret;
344}
345
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100346/*
347 * Register key type
348 */
David Howellsab3c3582013-09-24 10:35:18 +0100349static int __init big_key_init(void)
350{
David Howells7df3e592016-10-26 15:02:01 +0100351 int ret;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100352
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100353 /* init block cipher */
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200354 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);
David Howells7df3e592016-10-26 15:02:01 +0100357 pr_err("Can't alloc crypto: %d\n", ret);
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200358 return ret;
David Howells7df3e592016-10-26 15:02:01 +0100359 }
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200360 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 }
David Howells7df3e592016-10-26 15:02:01 +0100365
366 ret = register_key_type(&key_type_big_key);
367 if (ret < 0) {
368 pr_err("Can't register type: %d\n", ret);
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200369 goto free_aead;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100370 }
371
372 return 0;
373
Jason A. Donenfeld0c70fb82017-10-02 12:52:56 +0200374free_aead:
375 crypto_free_aead(big_key_aead);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100376 return ret;
377}
378
David Howells7df3e592016-10-26 15:02:01 +0100379late_initcall(big_key_init);