blob: 9e443fccad4cfde54deb9710feb4eee4bdadae17 [file] [log] [blame]
David Howellsab3c3582013-09-24 10:35:18 +01001/* Large capacity key type
2 *
3 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
David Howellsab3c3582013-09-24 10:35:18 +010012#include <linux/init.h>
13#include <linux/seq_file.h>
14#include <linux/file.h>
15#include <linux/shmem_fs.h>
16#include <linux/err.h>
Kirill Marinushkin13100a72016-04-12 19:54:58 +010017#include <linux/scatterlist.h>
David Howellsab3c3582013-09-24 10:35:18 +010018#include <keys/user-type.h>
19#include <keys/big_key-type.h>
Kirill Marinushkin13100a72016-04-12 19:54:58 +010020#include <crypto/rng.h>
David Howellsab3c3582013-09-24 10:35:18 +010021
David Howellsab3c3582013-09-24 10:35:18 +010022/*
David Howells146aa8b2015-10-21 14:04:48 +010023 * Layout of key payload words.
24 */
25enum {
26 big_key_data,
27 big_key_path,
28 big_key_path_2nd_part,
29 big_key_len,
30};
31
32/*
Kirill Marinushkin13100a72016-04-12 19:54:58 +010033 * Crypto operation with big_key data
34 */
35enum big_key_op {
36 BIG_KEY_ENC,
37 BIG_KEY_DEC,
38};
39
40/*
David Howellsab3c3582013-09-24 10:35:18 +010041 * If the data is under this limit, there's no point creating a shm file to
42 * hold it as the permanently resident metadata for the shmem fs will be at
43 * least as large as the data.
44 */
45#define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
46
47/*
Kirill Marinushkin13100a72016-04-12 19:54:58 +010048 * Key size for big_key data encryption
49 */
50#define ENC_KEY_SIZE 16
51
52/*
David Howellsab3c3582013-09-24 10:35:18 +010053 * big_key defined keys take an arbitrary string as the description and an
54 * arbitrary blob of data as the payload
55 */
56struct key_type key_type_big_key = {
57 .name = "big_key",
David Howells002edaf2014-07-18 18:56:36 +010058 .preparse = big_key_preparse,
59 .free_preparse = big_key_free_preparse,
60 .instantiate = generic_key_instantiate,
David Howellsab3c3582013-09-24 10:35:18 +010061 .revoke = big_key_revoke,
62 .destroy = big_key_destroy,
63 .describe = big_key_describe,
64 .read = big_key_read,
65};
66
67/*
Kirill Marinushkin13100a72016-04-12 19:54:58 +010068 * Crypto names for big_key data encryption
69 */
70static const char big_key_rng_name[] = "stdrng";
71static const char big_key_alg_name[] = "ecb(aes)";
72
73/*
74 * Crypto algorithms for big_key data encryption
75 */
76static struct crypto_rng *big_key_rng;
77static struct crypto_blkcipher *big_key_blkcipher;
78
79/*
80 * Generate random key to encrypt big_key data
81 */
82static inline int big_key_gen_enckey(u8 *key)
83{
84 return crypto_rng_get_bytes(big_key_rng, key, ENC_KEY_SIZE);
85}
86
87/*
88 * Encrypt/decrypt big_key data
89 */
90static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
91{
92 int ret = -EINVAL;
93 struct scatterlist sgio;
94 struct blkcipher_desc desc;
95
96 if (crypto_blkcipher_setkey(big_key_blkcipher, key, ENC_KEY_SIZE)) {
97 ret = -EAGAIN;
98 goto error;
99 }
100
101 desc.flags = 0;
102 desc.tfm = big_key_blkcipher;
103
104 sg_init_one(&sgio, data, datalen);
105
106 if (op == BIG_KEY_ENC)
107 ret = crypto_blkcipher_encrypt(&desc, &sgio, &sgio, datalen);
108 else
109 ret = crypto_blkcipher_decrypt(&desc, &sgio, &sgio, datalen);
110
111error:
112 return ret;
113}
114
115/*
David Howells002edaf2014-07-18 18:56:36 +0100116 * Preparse a big key
David Howellsab3c3582013-09-24 10:35:18 +0100117 */
David Howells002edaf2014-07-18 18:56:36 +0100118int big_key_preparse(struct key_preparsed_payload *prep)
David Howellsab3c3582013-09-24 10:35:18 +0100119{
David Howells146aa8b2015-10-21 14:04:48 +0100120 struct path *path = (struct path *)&prep->payload.data[big_key_path];
David Howellsab3c3582013-09-24 10:35:18 +0100121 struct file *file;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100122 u8 *enckey;
123 u8 *data = NULL;
David Howellsab3c3582013-09-24 10:35:18 +0100124 ssize_t written;
125 size_t datalen = prep->datalen;
126 int ret;
127
128 ret = -EINVAL;
129 if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
130 goto error;
131
132 /* Set an arbitrary quota */
David Howells002edaf2014-07-18 18:56:36 +0100133 prep->quotalen = 16;
David Howellsab3c3582013-09-24 10:35:18 +0100134
David Howells146aa8b2015-10-21 14:04:48 +0100135 prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
David Howellsab3c3582013-09-24 10:35:18 +0100136
137 if (datalen > BIG_KEY_FILE_THRESHOLD) {
138 /* Create a shmem file to store the data in. This will permit the data
139 * to be swapped out if needed.
140 *
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100141 * File content is stored encrypted with randomly generated key.
David Howellsab3c3582013-09-24 10:35:18 +0100142 */
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100143 size_t enclen = ALIGN(datalen, crypto_blkcipher_blocksize(big_key_blkcipher));
144
145 /* prepare aligned data to encrypt */
146 data = kmalloc(enclen, GFP_KERNEL);
147 if (!data)
148 return -ENOMEM;
149
150 memcpy(data, prep->data, datalen);
151 memset(data + datalen, 0x00, enclen - datalen);
152
153 /* generate random key */
154 enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
155 if (!enckey) {
156 ret = -ENOMEM;
David Howells002edaf2014-07-18 18:56:36 +0100157 goto error;
Wei Yongjund2b86972013-10-30 11:23:02 +0800158 }
David Howellsab3c3582013-09-24 10:35:18 +0100159
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100160 ret = big_key_gen_enckey(enckey);
161 if (ret)
162 goto err_enckey;
163
164 /* encrypt aligned data */
165 ret = big_key_crypt(BIG_KEY_ENC, data, enclen, enckey);
166 if (ret)
167 goto err_enckey;
168
169 /* save aligned data to file */
170 file = shmem_kernel_file_setup("", enclen, 0);
171 if (IS_ERR(file)) {
172 ret = PTR_ERR(file);
173 goto err_enckey;
174 }
175
176 written = kernel_write(file, data, enclen, 0);
177 if (written != enclen) {
David Howells97826c82013-11-13 16:51:06 +0000178 ret = written;
David Howellsab3c3582013-09-24 10:35:18 +0100179 if (written >= 0)
180 ret = -ENOMEM;
181 goto err_fput;
182 }
183
184 /* Pin the mount and dentry to the key so that we can open it again
185 * later
186 */
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100187 prep->payload.data[big_key_data] = enckey;
David Howellsab3c3582013-09-24 10:35:18 +0100188 *path = file->f_path;
189 path_get(path);
190 fput(file);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100191 kfree(data);
David Howellsab3c3582013-09-24 10:35:18 +0100192 } else {
193 /* Just store the data in a buffer */
194 void *data = kmalloc(datalen, GFP_KERNEL);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100195
David Howells002edaf2014-07-18 18:56:36 +0100196 if (!data)
197 return -ENOMEM;
David Howellsab3c3582013-09-24 10:35:18 +0100198
David Howells146aa8b2015-10-21 14:04:48 +0100199 prep->payload.data[big_key_data] = data;
200 memcpy(data, prep->data, prep->datalen);
David Howellsab3c3582013-09-24 10:35:18 +0100201 }
202 return 0;
203
204err_fput:
205 fput(file);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100206err_enckey:
207 kfree(enckey);
David Howellsab3c3582013-09-24 10:35:18 +0100208error:
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100209 kfree(data);
David Howellsab3c3582013-09-24 10:35:18 +0100210 return ret;
211}
212
213/*
David Howells002edaf2014-07-18 18:56:36 +0100214 * Clear preparsement.
215 */
216void big_key_free_preparse(struct key_preparsed_payload *prep)
217{
218 if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
David Howells146aa8b2015-10-21 14:04:48 +0100219 struct path *path = (struct path *)&prep->payload.data[big_key_path];
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100220
David Howells002edaf2014-07-18 18:56:36 +0100221 path_put(path);
David Howells002edaf2014-07-18 18:56:36 +0100222 }
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100223 kfree(prep->payload.data[big_key_data]);
David Howells002edaf2014-07-18 18:56:36 +0100224}
225
226/*
David Howellsab3c3582013-09-24 10:35:18 +0100227 * dispose of the links from a revoked keyring
228 * - called with the key sem write-locked
229 */
230void big_key_revoke(struct key *key)
231{
David Howells146aa8b2015-10-21 14:04:48 +0100232 struct path *path = (struct path *)&key->payload.data[big_key_path];
David Howellsab3c3582013-09-24 10:35:18 +0100233
234 /* clear the quota */
235 key_payload_reserve(key, 0);
David Howells146aa8b2015-10-21 14:04:48 +0100236 if (key_is_instantiated(key) &&
237 (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
David Howellsab3c3582013-09-24 10:35:18 +0100238 vfs_truncate(path, 0);
239}
240
241/*
242 * dispose of the data dangling from the corpse of a big_key key
243 */
244void big_key_destroy(struct key *key)
245{
David Howells146aa8b2015-10-21 14:04:48 +0100246 size_t datalen = (size_t)key->payload.data[big_key_len];
247
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100248 if (datalen > BIG_KEY_FILE_THRESHOLD) {
David Howells146aa8b2015-10-21 14:04:48 +0100249 struct path *path = (struct path *)&key->payload.data[big_key_path];
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100250
David Howellsab3c3582013-09-24 10:35:18 +0100251 path_put(path);
252 path->mnt = NULL;
253 path->dentry = NULL;
David Howellsab3c3582013-09-24 10:35:18 +0100254 }
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100255 kfree(key->payload.data[big_key_data]);
256 key->payload.data[big_key_data] = NULL;
David Howellsab3c3582013-09-24 10:35:18 +0100257}
258
259/*
260 * describe the big_key key
261 */
262void big_key_describe(const struct key *key, struct seq_file *m)
263{
David Howells146aa8b2015-10-21 14:04:48 +0100264 size_t datalen = (size_t)key->payload.data[big_key_len];
David Howellsab3c3582013-09-24 10:35:18 +0100265
266 seq_puts(m, key->description);
267
268 if (key_is_instantiated(key))
David Howells146aa8b2015-10-21 14:04:48 +0100269 seq_printf(m, ": %zu [%s]",
David Howellsab3c3582013-09-24 10:35:18 +0100270 datalen,
271 datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
272}
273
274/*
275 * read the key data
276 * - the key's semaphore is read-locked
277 */
278long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
279{
David Howells146aa8b2015-10-21 14:04:48 +0100280 size_t datalen = (size_t)key->payload.data[big_key_len];
David Howellsab3c3582013-09-24 10:35:18 +0100281 long ret;
282
283 if (!buffer || buflen < datalen)
284 return datalen;
285
286 if (datalen > BIG_KEY_FILE_THRESHOLD) {
David Howells146aa8b2015-10-21 14:04:48 +0100287 struct path *path = (struct path *)&key->payload.data[big_key_path];
David Howellsab3c3582013-09-24 10:35:18 +0100288 struct file *file;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100289 u8 *data;
290 u8 *enckey = (u8 *)key->payload.data[big_key_data];
291 size_t enclen = ALIGN(datalen, crypto_blkcipher_blocksize(big_key_blkcipher));
292
293 data = kmalloc(enclen, GFP_KERNEL);
294 if (!data)
295 return -ENOMEM;
David Howellsab3c3582013-09-24 10:35:18 +0100296
297 file = dentry_open(path, O_RDONLY, current_cred());
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100298 if (IS_ERR(file)) {
299 ret = PTR_ERR(file);
300 goto error;
301 }
David Howellsab3c3582013-09-24 10:35:18 +0100302
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100303 /* read file to kernel and decrypt */
304 ret = kernel_read(file, 0, data, enclen);
305 if (ret >= 0 && ret != enclen) {
David Howellsab3c3582013-09-24 10:35:18 +0100306 ret = -EIO;
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100307 goto err_fput;
308 }
309
310 ret = big_key_crypt(BIG_KEY_DEC, data, enclen, enckey);
311 if (ret)
312 goto err_fput;
313
314 ret = datalen;
315
316 /* copy decrypted data to user */
317 if (copy_to_user(buffer, data, datalen) != 0)
318 ret = -EFAULT;
319
320err_fput:
321 fput(file);
322error:
323 kfree(data);
David Howellsab3c3582013-09-24 10:35:18 +0100324 } else {
325 ret = datalen;
David Howells146aa8b2015-10-21 14:04:48 +0100326 if (copy_to_user(buffer, key->payload.data[big_key_data],
327 datalen) != 0)
David Howellsab3c3582013-09-24 10:35:18 +0100328 ret = -EFAULT;
329 }
330
331 return ret;
332}
333
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100334/*
335 * Register key type
336 */
David Howellsab3c3582013-09-24 10:35:18 +0100337static int __init big_key_init(void)
338{
339 return register_key_type(&key_type_big_key);
340}
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100341
342/*
343 * Initialize big_key crypto and RNG algorithms
344 */
345static int __init big_key_crypto_init(void)
346{
347 int ret = -EINVAL;
348
349 /* init RNG */
350 big_key_rng = crypto_alloc_rng(big_key_rng_name, 0, 0);
351 if (IS_ERR(big_key_rng)) {
352 big_key_rng = NULL;
353 return -EFAULT;
354 }
355
356 /* seed RNG */
357 ret = crypto_rng_reset(big_key_rng, NULL, crypto_rng_seedsize(big_key_rng));
358 if (ret)
359 goto error;
360
361 /* init block cipher */
362 big_key_blkcipher = crypto_alloc_blkcipher(big_key_alg_name, 0, 0);
363 if (IS_ERR(big_key_blkcipher)) {
364 big_key_blkcipher = NULL;
365 ret = -EFAULT;
366 goto error;
367 }
368
369 return 0;
370
371error:
372 crypto_free_rng(big_key_rng);
373 big_key_rng = NULL;
374 return ret;
375}
376
Paul Gortmakera1f2bdf2015-12-09 17:37:15 -0500377device_initcall(big_key_init);
Kirill Marinushkin13100a72016-04-12 19:54:58 +0100378late_initcall(big_key_crypto_init);