xref: /openbmc/linux/security/keys/big_key.c (revision 521fd61c84a19b31dfbaa8dde3d2577e4e115d12)
1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ab3c3587SDavid Howells /* Large capacity key type
3ab3c3587SDavid Howells  *
4*521fd61cSJason 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>
18*521fd61cSJason 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*521fd61cSJason A. Donenfeld 	/* no ->update(); don't add it without changing chacha20poly1305's nonce */
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;
60*521fd61cSJason A. Donenfeld 	u8 *buf, *enckey;
61ab3c3587SDavid Howells 	ssize_t written;
62*521fd61cSJason A. Donenfeld 	size_t datalen = prep->datalen;
63*521fd61cSJason 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.
79*521fd61cSJason A. Donenfeld 		 * Since the key is random for each file, we can set the nonce
80*521fd61cSJason A. Donenfeld 		 * to zero, provided we never define a ->update() call.
81ab3c3587SDavid Howells 		 */
82e13ec939SChristoph Hellwig 		loff_t pos = 0;
8313100a72SKirill Marinushkin 
84*521fd61cSJason A. Donenfeld 		buf = kvmalloc(enclen, GFP_KERNEL);
85d9f4bb1aSDavid Howells 		if (!buf)
8613100a72SKirill Marinushkin 			return -ENOMEM;
8713100a72SKirill Marinushkin 
8813100a72SKirill Marinushkin 		/* generate random key */
89*521fd61cSJason A. Donenfeld 		enckey = kmalloc(CHACHA20POLY1305_KEY_SIZE, GFP_KERNEL);
9013100a72SKirill Marinushkin 		if (!enckey) {
9113100a72SKirill Marinushkin 			ret = -ENOMEM;
92002edaf7SDavid Howells 			goto error;
93d2b86970SWei Yongjun 		}
94*521fd61cSJason A. Donenfeld 		ret = get_random_bytes_wait(enckey, CHACHA20POLY1305_KEY_SIZE);
95428490e3SJason A. Donenfeld 		if (unlikely(ret))
9613100a72SKirill Marinushkin 			goto err_enckey;
9713100a72SKirill Marinushkin 
98*521fd61cSJason A. Donenfeld 		/* encrypt data */
99*521fd61cSJason A. Donenfeld 		chacha20poly1305_encrypt(buf, prep->data, datalen, NULL, 0,
100*521fd61cSJason 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 
109*521fd61cSJason A. Donenfeld 		written = kernel_write(file, buf, enclen, &pos);
11013100a72SKirill Marinushkin 		if (written != enclen) {
11197826c82SDavid Howells 			ret = written;
112ab3c3587SDavid Howells 			if (written >= 0)
113*521fd61cSJason 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);
124*521fd61cSJason A. Donenfeld 		memzero_explicit(buf, enclen);
125*521fd61cSJason 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:
143*521fd61cSJason A. Donenfeld 	memzero_explicit(buf, enclen);
144*521fd61cSJason 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 /*
195ab3c3587SDavid Howells  * describe the big_key key
196ab3c3587SDavid Howells  */
197ab3c3587SDavid Howells void big_key_describe(const struct key *key, struct seq_file *m)
198ab3c3587SDavid Howells {
199146aa8b1SDavid Howells 	size_t datalen = (size_t)key->payload.data[big_key_len];
200ab3c3587SDavid Howells 
201ab3c3587SDavid Howells 	seq_puts(m, key->description);
202ab3c3587SDavid Howells 
203363b02daSDavid Howells 	if (key_is_positive(key))
204146aa8b1SDavid Howells 		seq_printf(m, ": %zu [%s]",
205ab3c3587SDavid Howells 			   datalen,
206ab3c3587SDavid Howells 			   datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
207ab3c3587SDavid Howells }
208ab3c3587SDavid Howells 
209ab3c3587SDavid Howells /*
210ab3c3587SDavid Howells  * read the key data
211ab3c3587SDavid Howells  * - the key's semaphore is read-locked
212ab3c3587SDavid Howells  */
213d3ec10aaSWaiman Long long big_key_read(const struct key *key, char *buffer, size_t buflen)
214ab3c3587SDavid Howells {
215146aa8b1SDavid Howells 	size_t datalen = (size_t)key->payload.data[big_key_len];
216ab3c3587SDavid Howells 	long ret;
217ab3c3587SDavid Howells 
218ab3c3587SDavid Howells 	if (!buffer || buflen < datalen)
219ab3c3587SDavid Howells 		return datalen;
220ab3c3587SDavid Howells 
221ab3c3587SDavid Howells 	if (datalen > BIG_KEY_FILE_THRESHOLD) {
222146aa8b1SDavid Howells 		struct path *path = (struct path *)&key->payload.data[big_key_path];
223ab3c3587SDavid Howells 		struct file *file;
224*521fd61cSJason A. Donenfeld 		u8 *buf, *enckey = (u8 *)key->payload.data[big_key_data];
225*521fd61cSJason A. Donenfeld 		size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
226bdd1d2d3SChristoph Hellwig 		loff_t pos = 0;
22713100a72SKirill Marinushkin 
228*521fd61cSJason A. Donenfeld 		buf = kvmalloc(enclen, GFP_KERNEL);
229d9f4bb1aSDavid Howells 		if (!buf)
23013100a72SKirill Marinushkin 			return -ENOMEM;
231ab3c3587SDavid Howells 
232ab3c3587SDavid Howells 		file = dentry_open(path, O_RDONLY, current_cred());
23313100a72SKirill Marinushkin 		if (IS_ERR(file)) {
23413100a72SKirill Marinushkin 			ret = PTR_ERR(file);
23513100a72SKirill Marinushkin 			goto error;
23613100a72SKirill Marinushkin 		}
237ab3c3587SDavid Howells 
23813100a72SKirill Marinushkin 		/* read file to kernel and decrypt */
239*521fd61cSJason A. Donenfeld 		ret = kernel_read(file, buf, enclen, &pos);
240*521fd61cSJason A. Donenfeld 		if (ret != enclen) {
241*521fd61cSJason A. Donenfeld 			if (ret >= 0)
242ab3c3587SDavid Howells 				ret = -EIO;
24313100a72SKirill Marinushkin 			goto err_fput;
24413100a72SKirill Marinushkin 		}
24513100a72SKirill Marinushkin 
246*521fd61cSJason A. Donenfeld 		ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0,
247*521fd61cSJason A. Donenfeld 					       enckey) ? 0 : -EBADMSG;
248*521fd61cSJason A. Donenfeld 		if (unlikely(ret))
24913100a72SKirill Marinushkin 			goto err_fput;
25013100a72SKirill Marinushkin 
25113100a72SKirill Marinushkin 		ret = datalen;
25213100a72SKirill Marinushkin 
253d3ec10aaSWaiman Long 		/* copy out decrypted data */
254*521fd61cSJason A. Donenfeld 		memcpy(buffer, buf, datalen);
25513100a72SKirill Marinushkin 
25613100a72SKirill Marinushkin err_fput:
25713100a72SKirill Marinushkin 		fput(file);
25813100a72SKirill Marinushkin error:
259*521fd61cSJason A. Donenfeld 		memzero_explicit(buf, enclen);
260*521fd61cSJason A. Donenfeld 		kvfree(buf);
261ab3c3587SDavid Howells 	} else {
262ab3c3587SDavid Howells 		ret = datalen;
263d3ec10aaSWaiman Long 		memcpy(buffer, key->payload.data[big_key_data], datalen);
264ab3c3587SDavid Howells 	}
265ab3c3587SDavid Howells 
266ab3c3587SDavid Howells 	return ret;
267ab3c3587SDavid Howells }
268ab3c3587SDavid Howells 
26913100a72SKirill Marinushkin /*
27013100a72SKirill Marinushkin  * Register key type
27113100a72SKirill Marinushkin  */
272ab3c3587SDavid Howells static int __init big_key_init(void)
273ab3c3587SDavid Howells {
274*521fd61cSJason A. Donenfeld 	return register_key_type(&key_type_big_key);
27513100a72SKirill Marinushkin }
27613100a72SKirill Marinushkin 
2777df3e59cSDavid Howells late_initcall(big_key_init);
278