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