xref: /openbmc/linux/security/keys/big_key.c (revision b6f61c31464940513ef4eccb3a030a405b4256d6)
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  */
23146aa8b1SDavid Howells enum {
24146aa8b1SDavid Howells 	big_key_data,
25146aa8b1SDavid Howells 	big_key_path,
26146aa8b1SDavid Howells 	big_key_path_2nd_part,
27146aa8b1SDavid Howells 	big_key_len,
28146aa8b1SDavid Howells };
29146aa8b1SDavid Howells 
30146aa8b1SDavid Howells /*
31ab3c3587SDavid Howells  * If the data is under this limit, there's no point creating a shm file to
32ab3c3587SDavid Howells  * hold it as the permanently resident metadata for the shmem fs will be at
33ab3c3587SDavid Howells  * least as large as the data.
34ab3c3587SDavid Howells  */
35ab3c3587SDavid Howells #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
36ab3c3587SDavid Howells 
37ab3c3587SDavid Howells /*
38ab3c3587SDavid Howells  * big_key defined keys take an arbitrary string as the description and an
39ab3c3587SDavid Howells  * arbitrary blob of data as the payload
40ab3c3587SDavid Howells  */
41ab3c3587SDavid Howells struct key_type key_type_big_key = {
42ab3c3587SDavid Howells 	.name			= "big_key",
43002edaf7SDavid Howells 	.preparse		= big_key_preparse,
44002edaf7SDavid Howells 	.free_preparse		= big_key_free_preparse,
45002edaf7SDavid Howells 	.instantiate		= generic_key_instantiate,
46ab3c3587SDavid Howells 	.revoke			= big_key_revoke,
47ab3c3587SDavid Howells 	.destroy		= big_key_destroy,
48ab3c3587SDavid Howells 	.describe		= big_key_describe,
49ab3c3587SDavid Howells 	.read			= big_key_read,
50*b6f61c31SDavid Howells 	.update			= big_key_update,
51ab3c3587SDavid Howells };
52ab3c3587SDavid Howells 
53ab3c3587SDavid Howells /*
54002edaf7SDavid Howells  * Preparse a big key
55ab3c3587SDavid Howells  */
56002edaf7SDavid Howells int big_key_preparse(struct key_preparsed_payload *prep)
57ab3c3587SDavid Howells {
58146aa8b1SDavid Howells 	struct path *path = (struct path *)&prep->payload.data[big_key_path];
59ab3c3587SDavid Howells 	struct file *file;
60521fd61cSJason A. Donenfeld 	u8 *buf, *enckey;
61ab3c3587SDavid Howells 	ssize_t written;
62521fd61cSJason A. Donenfeld 	size_t datalen = prep->datalen;
63521fd61cSJason A. Donenfeld 	size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
64ab3c3587SDavid Howells 	int ret;
65ab3c3587SDavid Howells 
66ab3c3587SDavid Howells 	if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
67d9f4bb1aSDavid Howells 		return -EINVAL;
68ab3c3587SDavid Howells 
69ab3c3587SDavid Howells 	/* Set an arbitrary quota */
70002edaf7SDavid Howells 	prep->quotalen = 16;
71ab3c3587SDavid Howells 
72146aa8b1SDavid Howells 	prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
73ab3c3587SDavid Howells 
74ab3c3587SDavid Howells 	if (datalen > BIG_KEY_FILE_THRESHOLD) {
75ab3c3587SDavid Howells 		/* Create a shmem file to store the data in.  This will permit the data
76ab3c3587SDavid Howells 		 * to be swapped out if needed.
77ab3c3587SDavid Howells 		 *
7813100a72SKirill Marinushkin 		 * File content is stored encrypted with randomly generated key.
79521fd61cSJason A. Donenfeld 		 * Since the key is random for each file, we can set the nonce
80521fd61cSJason A. Donenfeld 		 * to zero, provided we never define a ->update() call.
81ab3c3587SDavid Howells 		 */
82e13ec939SChristoph Hellwig 		loff_t pos = 0;
8313100a72SKirill Marinushkin 
84521fd61cSJason A. Donenfeld 		buf = kvmalloc(enclen, GFP_KERNEL);
85d9f4bb1aSDavid Howells 		if (!buf)
8613100a72SKirill Marinushkin 			return -ENOMEM;
8713100a72SKirill Marinushkin 
8813100a72SKirill Marinushkin 		/* generate random key */
89521fd61cSJason A. Donenfeld 		enckey = kmalloc(CHACHA20POLY1305_KEY_SIZE, GFP_KERNEL);
9013100a72SKirill Marinushkin 		if (!enckey) {
9113100a72SKirill Marinushkin 			ret = -ENOMEM;
92002edaf7SDavid Howells 			goto error;
93d2b86970SWei Yongjun 		}
94521fd61cSJason A. Donenfeld 		ret = get_random_bytes_wait(enckey, CHACHA20POLY1305_KEY_SIZE);
95428490e3SJason A. Donenfeld 		if (unlikely(ret))
9613100a72SKirill Marinushkin 			goto err_enckey;
9713100a72SKirill Marinushkin 
98521fd61cSJason A. Donenfeld 		/* encrypt data */
99521fd61cSJason A. Donenfeld 		chacha20poly1305_encrypt(buf, prep->data, datalen, NULL, 0,
100521fd61cSJason A. Donenfeld 					 0, enckey);
10113100a72SKirill Marinushkin 
10213100a72SKirill Marinushkin 		/* save aligned data to file */
10313100a72SKirill Marinushkin 		file = shmem_kernel_file_setup("", enclen, 0);
10413100a72SKirill Marinushkin 		if (IS_ERR(file)) {
10513100a72SKirill Marinushkin 			ret = PTR_ERR(file);
10613100a72SKirill Marinushkin 			goto err_enckey;
10713100a72SKirill Marinushkin 		}
10813100a72SKirill Marinushkin 
109521fd61cSJason A. Donenfeld 		written = kernel_write(file, buf, enclen, &pos);
11013100a72SKirill Marinushkin 		if (written != enclen) {
11197826c82SDavid Howells 			ret = written;
112ab3c3587SDavid Howells 			if (written >= 0)
113521fd61cSJason A. Donenfeld 				ret = -EIO;
114ab3c3587SDavid Howells 			goto err_fput;
115ab3c3587SDavid Howells 		}
116ab3c3587SDavid Howells 
117ab3c3587SDavid Howells 		/* Pin the mount and dentry to the key so that we can open it again
118ab3c3587SDavid Howells 		 * later
119ab3c3587SDavid Howells 		 */
12013100a72SKirill Marinushkin 		prep->payload.data[big_key_data] = enckey;
121ab3c3587SDavid Howells 		*path = file->f_path;
122ab3c3587SDavid Howells 		path_get(path);
123ab3c3587SDavid Howells 		fput(file);
124521fd61cSJason A. Donenfeld 		memzero_explicit(buf, enclen);
125521fd61cSJason A. Donenfeld 		kvfree(buf);
126ab3c3587SDavid Howells 	} else {
127ab3c3587SDavid Howells 		/* Just store the data in a buffer */
128ab3c3587SDavid Howells 		void *data = kmalloc(datalen, GFP_KERNEL);
12913100a72SKirill Marinushkin 
130002edaf7SDavid Howells 		if (!data)
131002edaf7SDavid Howells 			return -ENOMEM;
132ab3c3587SDavid Howells 
133146aa8b1SDavid Howells 		prep->payload.data[big_key_data] = data;
134146aa8b1SDavid Howells 		memcpy(data, prep->data, prep->datalen);
135ab3c3587SDavid Howells 	}
136ab3c3587SDavid Howells 	return 0;
137ab3c3587SDavid Howells 
138ab3c3587SDavid Howells err_fput:
139ab3c3587SDavid Howells 	fput(file);
14013100a72SKirill Marinushkin err_enckey:
14191080180SJason A. Donenfeld 	kzfree(enckey);
142ab3c3587SDavid Howells error:
143521fd61cSJason A. Donenfeld 	memzero_explicit(buf, enclen);
144521fd61cSJason A. Donenfeld 	kvfree(buf);
145ab3c3587SDavid Howells 	return ret;
146ab3c3587SDavid Howells }
147ab3c3587SDavid Howells 
148ab3c3587SDavid Howells /*
149002edaf7SDavid Howells  * Clear preparsement.
150002edaf7SDavid Howells  */
151002edaf7SDavid Howells void big_key_free_preparse(struct key_preparsed_payload *prep)
152002edaf7SDavid Howells {
153002edaf7SDavid Howells 	if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
154146aa8b1SDavid Howells 		struct path *path = (struct path *)&prep->payload.data[big_key_path];
15513100a72SKirill Marinushkin 
156002edaf7SDavid Howells 		path_put(path);
157002edaf7SDavid Howells 	}
15891080180SJason A. Donenfeld 	kzfree(prep->payload.data[big_key_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  */
165ab3c3587SDavid Howells void big_key_revoke(struct key *key)
166ab3c3587SDavid Howells {
167146aa8b1SDavid Howells 	struct path *path = (struct path *)&key->payload.data[big_key_path];
168ab3c3587SDavid Howells 
169ab3c3587SDavid Howells 	/* clear the quota */
170ab3c3587SDavid Howells 	key_payload_reserve(key, 0);
171363b02daSDavid Howells 	if (key_is_positive(key) &&
172146aa8b1SDavid Howells 	    (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
173ab3c3587SDavid Howells 		vfs_truncate(path, 0);
174ab3c3587SDavid Howells }
175ab3c3587SDavid Howells 
176ab3c3587SDavid Howells /*
177ab3c3587SDavid Howells  * dispose of the data dangling from the corpse of a big_key key
178ab3c3587SDavid Howells  */
179ab3c3587SDavid Howells void big_key_destroy(struct key *key)
180ab3c3587SDavid Howells {
181146aa8b1SDavid Howells 	size_t datalen = (size_t)key->payload.data[big_key_len];
182146aa8b1SDavid Howells 
18313100a72SKirill Marinushkin 	if (datalen > BIG_KEY_FILE_THRESHOLD) {
184146aa8b1SDavid Howells 		struct path *path = (struct path *)&key->payload.data[big_key_path];
18513100a72SKirill Marinushkin 
186ab3c3587SDavid Howells 		path_put(path);
187ab3c3587SDavid Howells 		path->mnt = NULL;
188ab3c3587SDavid Howells 		path->dentry = NULL;
18913100a72SKirill Marinushkin 	}
19091080180SJason A. Donenfeld 	kzfree(key->payload.data[big_key_data]);
191146aa8b1SDavid Howells 	key->payload.data[big_key_data] = NULL;
192ab3c3587SDavid Howells }
193ab3c3587SDavid Howells 
194ab3c3587SDavid Howells /*
195*b6f61c31SDavid Howells  * Update a big key
196*b6f61c31SDavid Howells  */
197*b6f61c31SDavid Howells int big_key_update(struct key *key, struct key_preparsed_payload *prep)
198*b6f61c31SDavid Howells {
199*b6f61c31SDavid Howells 	int ret;
200*b6f61c31SDavid Howells 
201*b6f61c31SDavid Howells 	ret = key_payload_reserve(key, prep->datalen);
202*b6f61c31SDavid Howells 	if (ret < 0)
203*b6f61c31SDavid Howells 		return ret;
204*b6f61c31SDavid Howells 
205*b6f61c31SDavid Howells 	if (key_is_positive(key))
206*b6f61c31SDavid Howells 		big_key_destroy(key);
207*b6f61c31SDavid Howells 
208*b6f61c31SDavid Howells 	return generic_key_instantiate(key, prep);
209*b6f61c31SDavid Howells }
210*b6f61c31SDavid Howells 
211*b6f61c31SDavid Howells /*
212ab3c3587SDavid Howells  * describe the big_key key
213ab3c3587SDavid Howells  */
214ab3c3587SDavid Howells void big_key_describe(const struct key *key, struct seq_file *m)
215ab3c3587SDavid Howells {
216146aa8b1SDavid Howells 	size_t datalen = (size_t)key->payload.data[big_key_len];
217ab3c3587SDavid Howells 
218ab3c3587SDavid Howells 	seq_puts(m, key->description);
219ab3c3587SDavid Howells 
220363b02daSDavid Howells 	if (key_is_positive(key))
221146aa8b1SDavid Howells 		seq_printf(m, ": %zu [%s]",
222ab3c3587SDavid Howells 			   datalen,
223ab3c3587SDavid Howells 			   datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
224ab3c3587SDavid Howells }
225ab3c3587SDavid Howells 
226ab3c3587SDavid Howells /*
227ab3c3587SDavid Howells  * read the key data
228ab3c3587SDavid Howells  * - the key's semaphore is read-locked
229ab3c3587SDavid Howells  */
230d3ec10aaSWaiman Long long big_key_read(const struct key *key, char *buffer, size_t buflen)
231ab3c3587SDavid Howells {
232146aa8b1SDavid Howells 	size_t datalen = (size_t)key->payload.data[big_key_len];
233ab3c3587SDavid Howells 	long ret;
234ab3c3587SDavid Howells 
235ab3c3587SDavid Howells 	if (!buffer || buflen < datalen)
236ab3c3587SDavid Howells 		return datalen;
237ab3c3587SDavid Howells 
238ab3c3587SDavid Howells 	if (datalen > BIG_KEY_FILE_THRESHOLD) {
239146aa8b1SDavid Howells 		struct path *path = (struct path *)&key->payload.data[big_key_path];
240ab3c3587SDavid Howells 		struct file *file;
241521fd61cSJason A. Donenfeld 		u8 *buf, *enckey = (u8 *)key->payload.data[big_key_data];
242521fd61cSJason A. Donenfeld 		size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
243bdd1d2d3SChristoph Hellwig 		loff_t pos = 0;
24413100a72SKirill Marinushkin 
245521fd61cSJason A. Donenfeld 		buf = kvmalloc(enclen, GFP_KERNEL);
246d9f4bb1aSDavid Howells 		if (!buf)
24713100a72SKirill Marinushkin 			return -ENOMEM;
248ab3c3587SDavid Howells 
249ab3c3587SDavid Howells 		file = dentry_open(path, O_RDONLY, current_cred());
25013100a72SKirill Marinushkin 		if (IS_ERR(file)) {
25113100a72SKirill Marinushkin 			ret = PTR_ERR(file);
25213100a72SKirill Marinushkin 			goto error;
25313100a72SKirill Marinushkin 		}
254ab3c3587SDavid Howells 
25513100a72SKirill Marinushkin 		/* read file to kernel and decrypt */
256521fd61cSJason A. Donenfeld 		ret = kernel_read(file, buf, enclen, &pos);
257521fd61cSJason A. Donenfeld 		if (ret != enclen) {
258521fd61cSJason A. Donenfeld 			if (ret >= 0)
259ab3c3587SDavid Howells 				ret = -EIO;
26013100a72SKirill Marinushkin 			goto err_fput;
26113100a72SKirill Marinushkin 		}
26213100a72SKirill Marinushkin 
263521fd61cSJason A. Donenfeld 		ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0,
264521fd61cSJason A. Donenfeld 					       enckey) ? 0 : -EBADMSG;
265521fd61cSJason A. Donenfeld 		if (unlikely(ret))
26613100a72SKirill Marinushkin 			goto err_fput;
26713100a72SKirill Marinushkin 
26813100a72SKirill Marinushkin 		ret = datalen;
26913100a72SKirill Marinushkin 
270d3ec10aaSWaiman Long 		/* copy out decrypted data */
271521fd61cSJason A. Donenfeld 		memcpy(buffer, buf, datalen);
27213100a72SKirill Marinushkin 
27313100a72SKirill Marinushkin err_fput:
27413100a72SKirill Marinushkin 		fput(file);
27513100a72SKirill Marinushkin error:
276521fd61cSJason A. Donenfeld 		memzero_explicit(buf, enclen);
277521fd61cSJason A. Donenfeld 		kvfree(buf);
278ab3c3587SDavid Howells 	} else {
279ab3c3587SDavid Howells 		ret = datalen;
280d3ec10aaSWaiman Long 		memcpy(buffer, key->payload.data[big_key_data], datalen);
281ab3c3587SDavid Howells 	}
282ab3c3587SDavid Howells 
283ab3c3587SDavid Howells 	return ret;
284ab3c3587SDavid Howells }
285ab3c3587SDavid Howells 
28613100a72SKirill Marinushkin /*
28713100a72SKirill Marinushkin  * Register key type
28813100a72SKirill Marinushkin  */
289ab3c3587SDavid Howells static int __init big_key_init(void)
290ab3c3587SDavid Howells {
291521fd61cSJason A. Donenfeld 	return register_key_type(&key_type_big_key);
29213100a72SKirill Marinushkin }
29313100a72SKirill Marinushkin 
2947df3e59cSDavid Howells late_initcall(big_key_init);
295