xref: /openbmc/linux/security/keys/big_key.c (revision 146aa8b1453bd8f1ff2304ffb71b4ee0eb9acdcc)
1ab3c3587SDavid Howells /* Large capacity key type
2ab3c3587SDavid Howells  *
3ab3c3587SDavid Howells  * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
4ab3c3587SDavid Howells  * Written by David Howells (dhowells@redhat.com)
5ab3c3587SDavid Howells  *
6ab3c3587SDavid Howells  * This program is free software; you can redistribute it and/or
7ab3c3587SDavid Howells  * modify it under the terms of the GNU General Public Licence
8ab3c3587SDavid Howells  * as published by the Free Software Foundation; either version
9ab3c3587SDavid Howells  * 2 of the Licence, or (at your option) any later version.
10ab3c3587SDavid Howells  */
11ab3c3587SDavid Howells 
12ab3c3587SDavid Howells #include <linux/module.h>
13ab3c3587SDavid Howells #include <linux/init.h>
14ab3c3587SDavid Howells #include <linux/seq_file.h>
15ab3c3587SDavid Howells #include <linux/file.h>
16ab3c3587SDavid Howells #include <linux/shmem_fs.h>
17ab3c3587SDavid Howells #include <linux/err.h>
18ab3c3587SDavid Howells #include <keys/user-type.h>
19ab3c3587SDavid Howells #include <keys/big_key-type.h>
20ab3c3587SDavid Howells 
21ab3c3587SDavid Howells MODULE_LICENSE("GPL");
22ab3c3587SDavid Howells 
23ab3c3587SDavid Howells /*
24*146aa8b1SDavid Howells  * Layout of key payload words.
25*146aa8b1SDavid Howells  */
26*146aa8b1SDavid Howells enum {
27*146aa8b1SDavid Howells 	big_key_data,
28*146aa8b1SDavid Howells 	big_key_path,
29*146aa8b1SDavid Howells 	big_key_path_2nd_part,
30*146aa8b1SDavid Howells 	big_key_len,
31*146aa8b1SDavid Howells };
32*146aa8b1SDavid Howells 
33*146aa8b1SDavid Howells /*
34ab3c3587SDavid Howells  * If the data is under this limit, there's no point creating a shm file to
35ab3c3587SDavid Howells  * hold it as the permanently resident metadata for the shmem fs will be at
36ab3c3587SDavid Howells  * least as large as the data.
37ab3c3587SDavid Howells  */
38ab3c3587SDavid Howells #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
39ab3c3587SDavid Howells 
40ab3c3587SDavid Howells /*
41ab3c3587SDavid Howells  * big_key defined keys take an arbitrary string as the description and an
42ab3c3587SDavid Howells  * arbitrary blob of data as the payload
43ab3c3587SDavid Howells  */
44ab3c3587SDavid Howells struct key_type key_type_big_key = {
45ab3c3587SDavid Howells 	.name			= "big_key",
46002edaf7SDavid Howells 	.preparse		= big_key_preparse,
47002edaf7SDavid Howells 	.free_preparse		= big_key_free_preparse,
48002edaf7SDavid Howells 	.instantiate		= generic_key_instantiate,
49ab3c3587SDavid Howells 	.revoke			= big_key_revoke,
50ab3c3587SDavid Howells 	.destroy		= big_key_destroy,
51ab3c3587SDavid Howells 	.describe		= big_key_describe,
52ab3c3587SDavid Howells 	.read			= big_key_read,
53ab3c3587SDavid Howells };
54ab3c3587SDavid Howells 
55ab3c3587SDavid Howells /*
56002edaf7SDavid Howells  * Preparse a big key
57ab3c3587SDavid Howells  */
58002edaf7SDavid Howells int big_key_preparse(struct key_preparsed_payload *prep)
59ab3c3587SDavid Howells {
60*146aa8b1SDavid Howells 	struct path *path = (struct path *)&prep->payload.data[big_key_path];
61ab3c3587SDavid Howells 	struct file *file;
62ab3c3587SDavid Howells 	ssize_t written;
63ab3c3587SDavid Howells 	size_t datalen = prep->datalen;
64ab3c3587SDavid Howells 	int ret;
65ab3c3587SDavid Howells 
66ab3c3587SDavid Howells 	ret = -EINVAL;
67ab3c3587SDavid Howells 	if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
68ab3c3587SDavid Howells 		goto error;
69ab3c3587SDavid Howells 
70ab3c3587SDavid Howells 	/* Set an arbitrary quota */
71002edaf7SDavid Howells 	prep->quotalen = 16;
72ab3c3587SDavid Howells 
73*146aa8b1SDavid Howells 	prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
74ab3c3587SDavid Howells 
75ab3c3587SDavid Howells 	if (datalen > BIG_KEY_FILE_THRESHOLD) {
76ab3c3587SDavid Howells 		/* Create a shmem file to store the data in.  This will permit the data
77ab3c3587SDavid Howells 		 * to be swapped out if needed.
78ab3c3587SDavid Howells 		 *
79ab3c3587SDavid Howells 		 * TODO: Encrypt the stored data with a temporary key.
80ab3c3587SDavid Howells 		 */
81c7277090SEric Paris 		file = shmem_kernel_file_setup("", datalen, 0);
82d2b86970SWei Yongjun 		if (IS_ERR(file)) {
83d2b86970SWei Yongjun 			ret = PTR_ERR(file);
84002edaf7SDavid Howells 			goto error;
85d2b86970SWei Yongjun 		}
86ab3c3587SDavid Howells 
87ab3c3587SDavid Howells 		written = kernel_write(file, prep->data, prep->datalen, 0);
88ab3c3587SDavid Howells 		if (written != datalen) {
8997826c82SDavid Howells 			ret = written;
90ab3c3587SDavid Howells 			if (written >= 0)
91ab3c3587SDavid Howells 				ret = -ENOMEM;
92ab3c3587SDavid Howells 			goto err_fput;
93ab3c3587SDavid Howells 		}
94ab3c3587SDavid Howells 
95ab3c3587SDavid Howells 		/* Pin the mount and dentry to the key so that we can open it again
96ab3c3587SDavid Howells 		 * later
97ab3c3587SDavid Howells 		 */
98ab3c3587SDavid Howells 		*path = file->f_path;
99ab3c3587SDavid Howells 		path_get(path);
100ab3c3587SDavid Howells 		fput(file);
101ab3c3587SDavid Howells 	} else {
102ab3c3587SDavid Howells 		/* Just store the data in a buffer */
103ab3c3587SDavid Howells 		void *data = kmalloc(datalen, GFP_KERNEL);
104002edaf7SDavid Howells 		if (!data)
105002edaf7SDavid Howells 			return -ENOMEM;
106ab3c3587SDavid Howells 
107*146aa8b1SDavid Howells 		prep->payload.data[big_key_data] = data;
108*146aa8b1SDavid Howells 		memcpy(data, prep->data, prep->datalen);
109ab3c3587SDavid Howells 	}
110ab3c3587SDavid Howells 	return 0;
111ab3c3587SDavid Howells 
112ab3c3587SDavid Howells err_fput:
113ab3c3587SDavid Howells 	fput(file);
114ab3c3587SDavid Howells error:
115ab3c3587SDavid Howells 	return ret;
116ab3c3587SDavid Howells }
117ab3c3587SDavid Howells 
118ab3c3587SDavid Howells /*
119002edaf7SDavid Howells  * Clear preparsement.
120002edaf7SDavid Howells  */
121002edaf7SDavid Howells void big_key_free_preparse(struct key_preparsed_payload *prep)
122002edaf7SDavid Howells {
123002edaf7SDavid Howells 	if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
124*146aa8b1SDavid Howells 		struct path *path = (struct path *)&prep->payload.data[big_key_path];
125002edaf7SDavid Howells 		path_put(path);
126002edaf7SDavid Howells 	} else {
127*146aa8b1SDavid Howells 		kfree(prep->payload.data[big_key_data]);
128002edaf7SDavid Howells 	}
129002edaf7SDavid Howells }
130002edaf7SDavid Howells 
131002edaf7SDavid Howells /*
132ab3c3587SDavid Howells  * dispose of the links from a revoked keyring
133ab3c3587SDavid Howells  * - called with the key sem write-locked
134ab3c3587SDavid Howells  */
135ab3c3587SDavid Howells void big_key_revoke(struct key *key)
136ab3c3587SDavid Howells {
137*146aa8b1SDavid Howells 	struct path *path = (struct path *)&key->payload.data[big_key_path];
138ab3c3587SDavid Howells 
139ab3c3587SDavid Howells 	/* clear the quota */
140ab3c3587SDavid Howells 	key_payload_reserve(key, 0);
141*146aa8b1SDavid Howells 	if (key_is_instantiated(key) &&
142*146aa8b1SDavid Howells 	    (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
143ab3c3587SDavid Howells 		vfs_truncate(path, 0);
144ab3c3587SDavid Howells }
145ab3c3587SDavid Howells 
146ab3c3587SDavid Howells /*
147ab3c3587SDavid Howells  * dispose of the data dangling from the corpse of a big_key key
148ab3c3587SDavid Howells  */
149ab3c3587SDavid Howells void big_key_destroy(struct key *key)
150ab3c3587SDavid Howells {
151*146aa8b1SDavid Howells 	size_t datalen = (size_t)key->payload.data[big_key_len];
152*146aa8b1SDavid Howells 
153*146aa8b1SDavid Howells 	if (datalen) {
154*146aa8b1SDavid Howells 		struct path *path = (struct path *)&key->payload.data[big_key_path];
155ab3c3587SDavid Howells 		path_put(path);
156ab3c3587SDavid Howells 		path->mnt = NULL;
157ab3c3587SDavid Howells 		path->dentry = NULL;
158ab3c3587SDavid Howells 	} else {
159*146aa8b1SDavid Howells 		kfree(key->payload.data[big_key_data]);
160*146aa8b1SDavid Howells 		key->payload.data[big_key_data] = NULL;
161ab3c3587SDavid Howells 	}
162ab3c3587SDavid Howells }
163ab3c3587SDavid Howells 
164ab3c3587SDavid Howells /*
165ab3c3587SDavid Howells  * describe the big_key key
166ab3c3587SDavid Howells  */
167ab3c3587SDavid Howells void big_key_describe(const struct key *key, struct seq_file *m)
168ab3c3587SDavid Howells {
169*146aa8b1SDavid Howells 	size_t datalen = (size_t)key->payload.data[big_key_len];
170ab3c3587SDavid Howells 
171ab3c3587SDavid Howells 	seq_puts(m, key->description);
172ab3c3587SDavid Howells 
173ab3c3587SDavid Howells 	if (key_is_instantiated(key))
174*146aa8b1SDavid Howells 		seq_printf(m, ": %zu [%s]",
175ab3c3587SDavid Howells 			   datalen,
176ab3c3587SDavid Howells 			   datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
177ab3c3587SDavid Howells }
178ab3c3587SDavid Howells 
179ab3c3587SDavid Howells /*
180ab3c3587SDavid Howells  * read the key data
181ab3c3587SDavid Howells  * - the key's semaphore is read-locked
182ab3c3587SDavid Howells  */
183ab3c3587SDavid Howells long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
184ab3c3587SDavid Howells {
185*146aa8b1SDavid Howells 	size_t datalen = (size_t)key->payload.data[big_key_len];
186ab3c3587SDavid Howells 	long ret;
187ab3c3587SDavid Howells 
188ab3c3587SDavid Howells 	if (!buffer || buflen < datalen)
189ab3c3587SDavid Howells 		return datalen;
190ab3c3587SDavid Howells 
191ab3c3587SDavid Howells 	if (datalen > BIG_KEY_FILE_THRESHOLD) {
192*146aa8b1SDavid Howells 		struct path *path = (struct path *)&key->payload.data[big_key_path];
193ab3c3587SDavid Howells 		struct file *file;
194ab3c3587SDavid Howells 		loff_t pos;
195ab3c3587SDavid Howells 
196ab3c3587SDavid Howells 		file = dentry_open(path, O_RDONLY, current_cred());
197ab3c3587SDavid Howells 		if (IS_ERR(file))
198ab3c3587SDavid Howells 			return PTR_ERR(file);
199ab3c3587SDavid Howells 
200ab3c3587SDavid Howells 		pos = 0;
201ab3c3587SDavid Howells 		ret = vfs_read(file, buffer, datalen, &pos);
202ab3c3587SDavid Howells 		fput(file);
203ab3c3587SDavid Howells 		if (ret >= 0 && ret != datalen)
204ab3c3587SDavid Howells 			ret = -EIO;
205ab3c3587SDavid Howells 	} else {
206ab3c3587SDavid Howells 		ret = datalen;
207*146aa8b1SDavid Howells 		if (copy_to_user(buffer, key->payload.data[big_key_data],
208*146aa8b1SDavid Howells 				 datalen) != 0)
209ab3c3587SDavid Howells 			ret = -EFAULT;
210ab3c3587SDavid Howells 	}
211ab3c3587SDavid Howells 
212ab3c3587SDavid Howells 	return ret;
213ab3c3587SDavid Howells }
214ab3c3587SDavid Howells 
215ab3c3587SDavid Howells /*
216ab3c3587SDavid Howells  * Module stuff
217ab3c3587SDavid Howells  */
218ab3c3587SDavid Howells static int __init big_key_init(void)
219ab3c3587SDavid Howells {
220ab3c3587SDavid Howells 	return register_key_type(&key_type_big_key);
221ab3c3587SDavid Howells }
222ab3c3587SDavid Howells 
223ab3c3587SDavid Howells static void __exit big_key_cleanup(void)
224ab3c3587SDavid Howells {
225ab3c3587SDavid Howells 	unregister_key_type(&key_type_big_key);
226ab3c3587SDavid Howells }
227ab3c3587SDavid Howells 
228ab3c3587SDavid Howells module_init(big_key_init);
229ab3c3587SDavid Howells module_exit(big_key_cleanup);
230