xref: /openbmc/linux/security/keys/big_key.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ab3c3587SDavid Howells /* Large capacity key type
3ab3c3587SDavid Howells  *
4521fd61cSJason A. Donenfeld  * Copyright (C) 2017-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
5ab3c3587SDavid Howells  * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
6ab3c3587SDavid Howells  * Written by David Howells (dhowells@redhat.com)
7ab3c3587SDavid Howells  */
8ab3c3587SDavid Howells 
97df3e59cSDavid Howells #define pr_fmt(fmt) "big_key: "fmt
10ab3c3587SDavid Howells #include <linux/init.h>
11ab3c3587SDavid Howells #include <linux/seq_file.h>
12ab3c3587SDavid Howells #include <linux/file.h>
13ab3c3587SDavid Howells #include <linux/shmem_fs.h>
14ab3c3587SDavid Howells #include <linux/err.h>
15428490e3SJason A. Donenfeld #include <linux/random.h>
16ab3c3587SDavid Howells #include <keys/user-type.h>
17ab3c3587SDavid Howells #include <keys/big_key-type.h>
18521fd61cSJason A. Donenfeld #include <crypto/chacha20poly1305.h>
19d9f4bb1aSDavid Howells 
20ab3c3587SDavid Howells /*
21146aa8b1SDavid Howells  * Layout of key payload words.
22146aa8b1SDavid Howells  */
23*c1298a3aSKees Cook struct big_key_payload {
24*c1298a3aSKees Cook 	u8 *data;
25*c1298a3aSKees Cook 	struct path path;
26*c1298a3aSKees Cook 	size_t length;
27146aa8b1SDavid Howells };
28*c1298a3aSKees Cook #define to_big_key_payload(payload)			\
29*c1298a3aSKees Cook 	(struct big_key_payload *)((payload).data)
30146aa8b1SDavid Howells 
31146aa8b1SDavid Howells /*
32ab3c3587SDavid Howells  * If the data is under this limit, there's no point creating a shm file to
33ab3c3587SDavid Howells  * hold it as the permanently resident metadata for the shmem fs will be at
34ab3c3587SDavid Howells  * least as large as the data.
35ab3c3587SDavid Howells  */
36ab3c3587SDavid Howells #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
37ab3c3587SDavid Howells 
38ab3c3587SDavid Howells /*
39ab3c3587SDavid Howells  * big_key defined keys take an arbitrary string as the description and an
40ab3c3587SDavid Howells  * arbitrary blob of data as the payload
41ab3c3587SDavid Howells  */
42ab3c3587SDavid Howells struct key_type key_type_big_key = {
43ab3c3587SDavid Howells 	.name			= "big_key",
44002edaf7SDavid Howells 	.preparse		= big_key_preparse,
45002edaf7SDavid Howells 	.free_preparse		= big_key_free_preparse,
46002edaf7SDavid Howells 	.instantiate		= generic_key_instantiate,
47ab3c3587SDavid Howells 	.revoke			= big_key_revoke,
48ab3c3587SDavid Howells 	.destroy		= big_key_destroy,
49ab3c3587SDavid Howells 	.describe		= big_key_describe,
50ab3c3587SDavid Howells 	.read			= big_key_read,
51b6f61c31SDavid Howells 	.update			= big_key_update,
52ab3c3587SDavid Howells };
53ab3c3587SDavid Howells 
54ab3c3587SDavid Howells /*
55002edaf7SDavid Howells  * Preparse a big key
56ab3c3587SDavid Howells  */
big_key_preparse(struct key_preparsed_payload * prep)57002edaf7SDavid Howells int big_key_preparse(struct key_preparsed_payload *prep)
58ab3c3587SDavid Howells {
59*c1298a3aSKees Cook 	struct big_key_payload *payload = to_big_key_payload(prep->payload);
60ab3c3587SDavid Howells 	struct file *file;
61521fd61cSJason A. Donenfeld 	u8 *buf, *enckey;
62ab3c3587SDavid Howells 	ssize_t written;
63521fd61cSJason A. Donenfeld 	size_t datalen = prep->datalen;
64521fd61cSJason A. Donenfeld 	size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
65ab3c3587SDavid Howells 	int ret;
66ab3c3587SDavid Howells 
67*c1298a3aSKees Cook 	BUILD_BUG_ON(sizeof(*payload) != sizeof(prep->payload.data));
68*c1298a3aSKees Cook 
69ab3c3587SDavid Howells 	if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
70d9f4bb1aSDavid Howells 		return -EINVAL;
71ab3c3587SDavid Howells 
72ab3c3587SDavid Howells 	/* Set an arbitrary quota */
73002edaf7SDavid Howells 	prep->quotalen = 16;
74ab3c3587SDavid Howells 
75*c1298a3aSKees Cook 	payload->length = datalen;
76ab3c3587SDavid Howells 
77ab3c3587SDavid Howells 	if (datalen > BIG_KEY_FILE_THRESHOLD) {
78ab3c3587SDavid Howells 		/* Create a shmem file to store the data in.  This will permit the data
79ab3c3587SDavid Howells 		 * to be swapped out if needed.
80ab3c3587SDavid Howells 		 *
8113100a72SKirill Marinushkin 		 * File content is stored encrypted with randomly generated key.
82521fd61cSJason A. Donenfeld 		 * Since the key is random for each file, we can set the nonce
83521fd61cSJason A. Donenfeld 		 * to zero, provided we never define a ->update() call.
84ab3c3587SDavid Howells 		 */
85e13ec939SChristoph Hellwig 		loff_t pos = 0;
8613100a72SKirill Marinushkin 
87521fd61cSJason A. Donenfeld 		buf = kvmalloc(enclen, GFP_KERNEL);
88d9f4bb1aSDavid Howells 		if (!buf)
8913100a72SKirill Marinushkin 			return -ENOMEM;
9013100a72SKirill Marinushkin 
9113100a72SKirill Marinushkin 		/* generate random key */
92521fd61cSJason A. Donenfeld 		enckey = kmalloc(CHACHA20POLY1305_KEY_SIZE, GFP_KERNEL);
9313100a72SKirill Marinushkin 		if (!enckey) {
9413100a72SKirill Marinushkin 			ret = -ENOMEM;
95002edaf7SDavid Howells 			goto error;
96d2b86970SWei Yongjun 		}
97521fd61cSJason A. Donenfeld 		ret = get_random_bytes_wait(enckey, CHACHA20POLY1305_KEY_SIZE);
98428490e3SJason A. Donenfeld 		if (unlikely(ret))
9913100a72SKirill Marinushkin 			goto err_enckey;
10013100a72SKirill Marinushkin 
101521fd61cSJason A. Donenfeld 		/* encrypt data */
102521fd61cSJason A. Donenfeld 		chacha20poly1305_encrypt(buf, prep->data, datalen, NULL, 0,
103521fd61cSJason A. Donenfeld 					 0, enckey);
10413100a72SKirill Marinushkin 
10513100a72SKirill Marinushkin 		/* save aligned data to file */
10613100a72SKirill Marinushkin 		file = shmem_kernel_file_setup("", enclen, 0);
10713100a72SKirill Marinushkin 		if (IS_ERR(file)) {
10813100a72SKirill Marinushkin 			ret = PTR_ERR(file);
10913100a72SKirill Marinushkin 			goto err_enckey;
11013100a72SKirill Marinushkin 		}
11113100a72SKirill Marinushkin 
112521fd61cSJason A. Donenfeld 		written = kernel_write(file, buf, enclen, &pos);
11313100a72SKirill Marinushkin 		if (written != enclen) {
11497826c82SDavid Howells 			ret = written;
115ab3c3587SDavid Howells 			if (written >= 0)
116521fd61cSJason A. Donenfeld 				ret = -EIO;
117ab3c3587SDavid Howells 			goto err_fput;
118ab3c3587SDavid Howells 		}
119ab3c3587SDavid Howells 
120ab3c3587SDavid Howells 		/* Pin the mount and dentry to the key so that we can open it again
121ab3c3587SDavid Howells 		 * later
122ab3c3587SDavid Howells 		 */
123*c1298a3aSKees Cook 		payload->data = enckey;
124*c1298a3aSKees Cook 		payload->path = file->f_path;
125*c1298a3aSKees Cook 		path_get(&payload->path);
126ab3c3587SDavid Howells 		fput(file);
127272a1219SDenis Efremov 		kvfree_sensitive(buf, enclen);
128ab3c3587SDavid Howells 	} else {
129ab3c3587SDavid Howells 		/* Just store the data in a buffer */
130ab3c3587SDavid Howells 		void *data = kmalloc(datalen, GFP_KERNEL);
13113100a72SKirill Marinushkin 
132002edaf7SDavid Howells 		if (!data)
133002edaf7SDavid Howells 			return -ENOMEM;
134ab3c3587SDavid Howells 
135*c1298a3aSKees Cook 		payload->data = data;
136146aa8b1SDavid Howells 		memcpy(data, prep->data, prep->datalen);
137ab3c3587SDavid Howells 	}
138ab3c3587SDavid Howells 	return 0;
139ab3c3587SDavid Howells 
140ab3c3587SDavid Howells err_fput:
141ab3c3587SDavid Howells 	fput(file);
14213100a72SKirill Marinushkin err_enckey:
143453431a5SWaiman Long 	kfree_sensitive(enckey);
144ab3c3587SDavid Howells error:
145272a1219SDenis Efremov 	kvfree_sensitive(buf, enclen);
146ab3c3587SDavid Howells 	return ret;
147ab3c3587SDavid Howells }
148ab3c3587SDavid Howells 
149ab3c3587SDavid Howells /*
150002edaf7SDavid Howells  * Clear preparsement.
151002edaf7SDavid Howells  */
big_key_free_preparse(struct key_preparsed_payload * prep)152002edaf7SDavid Howells void big_key_free_preparse(struct key_preparsed_payload *prep)
153002edaf7SDavid Howells {
154*c1298a3aSKees Cook 	struct big_key_payload *payload = to_big_key_payload(prep->payload);
15513100a72SKirill Marinushkin 
156*c1298a3aSKees Cook 	if (prep->datalen > BIG_KEY_FILE_THRESHOLD)
157*c1298a3aSKees Cook 		path_put(&payload->path);
158*c1298a3aSKees Cook 	kfree_sensitive(payload->data);
159002edaf7SDavid Howells }
160002edaf7SDavid Howells 
161002edaf7SDavid Howells /*
162ab3c3587SDavid Howells  * dispose of the links from a revoked keyring
163ab3c3587SDavid Howells  * - called with the key sem write-locked
164ab3c3587SDavid Howells  */
big_key_revoke(struct key * key)165ab3c3587SDavid Howells void big_key_revoke(struct key *key)
166ab3c3587SDavid Howells {
167*c1298a3aSKees Cook 	struct big_key_payload *payload = to_big_key_payload(key->payload);
168ab3c3587SDavid Howells 
169ab3c3587SDavid Howells 	/* clear the quota */
170ab3c3587SDavid Howells 	key_payload_reserve(key, 0);
171*c1298a3aSKees Cook 	if (key_is_positive(key) && payload->length > BIG_KEY_FILE_THRESHOLD)
172*c1298a3aSKees Cook 		vfs_truncate(&payload->path, 0);
173ab3c3587SDavid Howells }
174ab3c3587SDavid Howells 
175ab3c3587SDavid Howells /*
176ab3c3587SDavid Howells  * dispose of the data dangling from the corpse of a big_key key
177ab3c3587SDavid Howells  */
big_key_destroy(struct key * key)178ab3c3587SDavid Howells void big_key_destroy(struct key *key)
179ab3c3587SDavid Howells {
180*c1298a3aSKees Cook 	struct big_key_payload *payload = to_big_key_payload(key->payload);
181146aa8b1SDavid Howells 
182*c1298a3aSKees Cook 	if (payload->length > BIG_KEY_FILE_THRESHOLD) {
183*c1298a3aSKees Cook 		path_put(&payload->path);
184*c1298a3aSKees Cook 		payload->path.mnt = NULL;
185*c1298a3aSKees Cook 		payload->path.dentry = NULL;
18613100a72SKirill Marinushkin 	}
187*c1298a3aSKees Cook 	kfree_sensitive(payload->data);
188*c1298a3aSKees Cook 	payload->data = NULL;
189ab3c3587SDavid Howells }
190ab3c3587SDavid Howells 
191ab3c3587SDavid Howells /*
192b6f61c31SDavid Howells  * Update a big key
193b6f61c31SDavid Howells  */
big_key_update(struct key * key,struct key_preparsed_payload * prep)194b6f61c31SDavid Howells int big_key_update(struct key *key, struct key_preparsed_payload *prep)
195b6f61c31SDavid Howells {
196b6f61c31SDavid Howells 	int ret;
197b6f61c31SDavid Howells 
198b6f61c31SDavid Howells 	ret = key_payload_reserve(key, prep->datalen);
199b6f61c31SDavid Howells 	if (ret < 0)
200b6f61c31SDavid Howells 		return ret;
201b6f61c31SDavid Howells 
202b6f61c31SDavid Howells 	if (key_is_positive(key))
203b6f61c31SDavid Howells 		big_key_destroy(key);
204b6f61c31SDavid Howells 
205b6f61c31SDavid Howells 	return generic_key_instantiate(key, prep);
206b6f61c31SDavid Howells }
207b6f61c31SDavid Howells 
208b6f61c31SDavid Howells /*
209ab3c3587SDavid Howells  * describe the big_key key
210ab3c3587SDavid Howells  */
big_key_describe(const struct key * key,struct seq_file * m)211ab3c3587SDavid Howells void big_key_describe(const struct key *key, struct seq_file *m)
212ab3c3587SDavid Howells {
213*c1298a3aSKees Cook 	struct big_key_payload *payload = to_big_key_payload(key->payload);
214ab3c3587SDavid Howells 
215ab3c3587SDavid Howells 	seq_puts(m, key->description);
216ab3c3587SDavid Howells 
217363b02daSDavid Howells 	if (key_is_positive(key))
218146aa8b1SDavid Howells 		seq_printf(m, ": %zu [%s]",
219*c1298a3aSKees Cook 			   payload->length,
220*c1298a3aSKees Cook 			   payload->length > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
221ab3c3587SDavid Howells }
222ab3c3587SDavid Howells 
223ab3c3587SDavid Howells /*
224ab3c3587SDavid Howells  * read the key data
225ab3c3587SDavid Howells  * - the key's semaphore is read-locked
226ab3c3587SDavid Howells  */
big_key_read(const struct key * key,char * buffer,size_t buflen)227d3ec10aaSWaiman Long long big_key_read(const struct key *key, char *buffer, size_t buflen)
228ab3c3587SDavid Howells {
229*c1298a3aSKees Cook 	struct big_key_payload *payload = to_big_key_payload(key->payload);
230*c1298a3aSKees Cook 	size_t datalen = payload->length;
231ab3c3587SDavid Howells 	long ret;
232ab3c3587SDavid Howells 
233ab3c3587SDavid Howells 	if (!buffer || buflen < datalen)
234ab3c3587SDavid Howells 		return datalen;
235ab3c3587SDavid Howells 
236ab3c3587SDavid Howells 	if (datalen > BIG_KEY_FILE_THRESHOLD) {
237ab3c3587SDavid Howells 		struct file *file;
238*c1298a3aSKees Cook 		u8 *buf, *enckey = payload->data;
239521fd61cSJason A. Donenfeld 		size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
240bdd1d2d3SChristoph Hellwig 		loff_t pos = 0;
24113100a72SKirill Marinushkin 
242521fd61cSJason A. Donenfeld 		buf = kvmalloc(enclen, GFP_KERNEL);
243d9f4bb1aSDavid Howells 		if (!buf)
24413100a72SKirill Marinushkin 			return -ENOMEM;
245ab3c3587SDavid Howells 
246*c1298a3aSKees Cook 		file = dentry_open(&payload->path, O_RDONLY, current_cred());
24713100a72SKirill Marinushkin 		if (IS_ERR(file)) {
24813100a72SKirill Marinushkin 			ret = PTR_ERR(file);
24913100a72SKirill Marinushkin 			goto error;
25013100a72SKirill Marinushkin 		}
251ab3c3587SDavid Howells 
25213100a72SKirill Marinushkin 		/* read file to kernel and decrypt */
253521fd61cSJason A. Donenfeld 		ret = kernel_read(file, buf, enclen, &pos);
254521fd61cSJason A. Donenfeld 		if (ret != enclen) {
255521fd61cSJason A. Donenfeld 			if (ret >= 0)
256ab3c3587SDavid Howells 				ret = -EIO;
25713100a72SKirill Marinushkin 			goto err_fput;
25813100a72SKirill Marinushkin 		}
25913100a72SKirill Marinushkin 
260521fd61cSJason A. Donenfeld 		ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0,
261521fd61cSJason A. Donenfeld 					       enckey) ? 0 : -EBADMSG;
262521fd61cSJason A. Donenfeld 		if (unlikely(ret))
26313100a72SKirill Marinushkin 			goto err_fput;
26413100a72SKirill Marinushkin 
26513100a72SKirill Marinushkin 		ret = datalen;
26613100a72SKirill Marinushkin 
267d3ec10aaSWaiman Long 		/* copy out decrypted data */
268521fd61cSJason A. Donenfeld 		memcpy(buffer, buf, datalen);
26913100a72SKirill Marinushkin 
27013100a72SKirill Marinushkin err_fput:
27113100a72SKirill Marinushkin 		fput(file);
27213100a72SKirill Marinushkin error:
273272a1219SDenis Efremov 		kvfree_sensitive(buf, enclen);
274ab3c3587SDavid Howells 	} else {
275ab3c3587SDavid Howells 		ret = datalen;
276*c1298a3aSKees Cook 		memcpy(buffer, payload->data, datalen);
277ab3c3587SDavid Howells 	}
278ab3c3587SDavid Howells 
279ab3c3587SDavid Howells 	return ret;
280ab3c3587SDavid Howells }
281ab3c3587SDavid Howells 
28213100a72SKirill Marinushkin /*
28313100a72SKirill Marinushkin  * Register key type
28413100a72SKirill Marinushkin  */
big_key_init(void)285ab3c3587SDavid Howells static int __init big_key_init(void)
286ab3c3587SDavid Howells {
287521fd61cSJason A. Donenfeld 	return register_key_type(&key_type_big_key);
28813100a72SKirill Marinushkin }
28913100a72SKirill Marinushkin 
2907df3e59cSDavid Howells late_initcall(big_key_init);
291