1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2010 IBM Corporation
4  * Copyright (c) 2019-2021, Linaro Limited
5  *
6  * See Documentation/security/keys/trusted-encrypted.rst
7  */
8 
9 #include <keys/user-type.h>
10 #include <keys/trusted-type.h>
11 #include <keys/trusted_tee.h>
12 #include <keys/trusted_tpm.h>
13 #include <linux/capability.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/key-type.h>
17 #include <linux/module.h>
18 #include <linux/parser.h>
19 #include <linux/rcupdate.h>
20 #include <linux/slab.h>
21 #include <linux/static_call.h>
22 #include <linux/string.h>
23 #include <linux/uaccess.h>
24 
25 static char *trusted_key_source;
26 module_param_named(source, trusted_key_source, charp, 0);
27 MODULE_PARM_DESC(source, "Select trusted keys source (tpm or tee)");
28 
29 static const struct trusted_key_source trusted_key_sources[] = {
30 #if defined(CONFIG_TCG_TPM)
31 	{ "tpm", &trusted_key_tpm_ops },
32 #endif
33 #if defined(CONFIG_TEE)
34 	{ "tee", &trusted_key_tee_ops },
35 #endif
36 };
37 
38 DEFINE_STATIC_CALL_NULL(trusted_key_init, *trusted_key_sources[0].ops->init);
39 DEFINE_STATIC_CALL_NULL(trusted_key_seal, *trusted_key_sources[0].ops->seal);
40 DEFINE_STATIC_CALL_NULL(trusted_key_unseal,
41 			*trusted_key_sources[0].ops->unseal);
42 DEFINE_STATIC_CALL_NULL(trusted_key_get_random,
43 			*trusted_key_sources[0].ops->get_random);
44 DEFINE_STATIC_CALL_NULL(trusted_key_exit, *trusted_key_sources[0].ops->exit);
45 static unsigned char migratable;
46 
47 enum {
48 	Opt_err,
49 	Opt_new, Opt_load, Opt_update,
50 };
51 
52 static const match_table_t key_tokens = {
53 	{Opt_new, "new"},
54 	{Opt_load, "load"},
55 	{Opt_update, "update"},
56 	{Opt_err, NULL}
57 };
58 
59 /*
60  * datablob_parse - parse the keyctl data and fill in the
61  *                  payload structure
62  *
63  * On success returns 0, otherwise -EINVAL.
64  */
65 static int datablob_parse(char **datablob, struct trusted_key_payload *p)
66 {
67 	substring_t args[MAX_OPT_ARGS];
68 	long keylen;
69 	int ret = -EINVAL;
70 	int key_cmd;
71 	char *c;
72 
73 	/* main command */
74 	c = strsep(datablob, " \t");
75 	if (!c)
76 		return -EINVAL;
77 	key_cmd = match_token(c, key_tokens, args);
78 	switch (key_cmd) {
79 	case Opt_new:
80 		/* first argument is key size */
81 		c = strsep(datablob, " \t");
82 		if (!c)
83 			return -EINVAL;
84 		ret = kstrtol(c, 10, &keylen);
85 		if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
86 			return -EINVAL;
87 		p->key_len = keylen;
88 		ret = Opt_new;
89 		break;
90 	case Opt_load:
91 		/* first argument is sealed blob */
92 		c = strsep(datablob, " \t");
93 		if (!c)
94 			return -EINVAL;
95 		p->blob_len = strlen(c) / 2;
96 		if (p->blob_len > MAX_BLOB_SIZE)
97 			return -EINVAL;
98 		ret = hex2bin(p->blob, c, p->blob_len);
99 		if (ret < 0)
100 			return -EINVAL;
101 		ret = Opt_load;
102 		break;
103 	case Opt_update:
104 		ret = Opt_update;
105 		break;
106 	case Opt_err:
107 		return -EINVAL;
108 	}
109 	return ret;
110 }
111 
112 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
113 {
114 	struct trusted_key_payload *p = NULL;
115 	int ret;
116 
117 	ret = key_payload_reserve(key, sizeof(*p));
118 	if (ret < 0)
119 		goto err;
120 	p = kzalloc(sizeof(*p), GFP_KERNEL);
121 	if (!p)
122 		goto err;
123 
124 	p->migratable = migratable;
125 err:
126 	return p;
127 }
128 
129 /*
130  * trusted_instantiate - create a new trusted key
131  *
132  * Unseal an existing trusted blob or, for a new key, get a
133  * random key, then seal and create a trusted key-type key,
134  * adding it to the specified keyring.
135  *
136  * On success, return 0. Otherwise return errno.
137  */
138 static int trusted_instantiate(struct key *key,
139 			       struct key_preparsed_payload *prep)
140 {
141 	struct trusted_key_payload *payload = NULL;
142 	size_t datalen = prep->datalen;
143 	char *datablob, *orig_datablob;
144 	int ret = 0;
145 	int key_cmd;
146 	size_t key_len;
147 
148 	if (datalen <= 0 || datalen > 32767 || !prep->data)
149 		return -EINVAL;
150 
151 	orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
152 	if (!datablob)
153 		return -ENOMEM;
154 	memcpy(datablob, prep->data, datalen);
155 	datablob[datalen] = '\0';
156 
157 	payload = trusted_payload_alloc(key);
158 	if (!payload) {
159 		ret = -ENOMEM;
160 		goto out;
161 	}
162 
163 	key_cmd = datablob_parse(&datablob, payload);
164 	if (key_cmd < 0) {
165 		ret = key_cmd;
166 		goto out;
167 	}
168 
169 	dump_payload(payload);
170 
171 	switch (key_cmd) {
172 	case Opt_load:
173 		ret = static_call(trusted_key_unseal)(payload, datablob);
174 		dump_payload(payload);
175 		if (ret < 0)
176 			pr_info("key_unseal failed (%d)\n", ret);
177 		break;
178 	case Opt_new:
179 		key_len = payload->key_len;
180 		ret = static_call(trusted_key_get_random)(payload->key,
181 							  key_len);
182 		if (ret < 0)
183 			goto out;
184 
185 		if (ret != key_len) {
186 			pr_info("key_create failed (%d)\n", ret);
187 			ret = -EIO;
188 			goto out;
189 		}
190 
191 		ret = static_call(trusted_key_seal)(payload, datablob);
192 		if (ret < 0)
193 			pr_info("key_seal failed (%d)\n", ret);
194 		break;
195 	default:
196 		ret = -EINVAL;
197 	}
198 out:
199 	kfree_sensitive(orig_datablob);
200 	if (!ret)
201 		rcu_assign_keypointer(key, payload);
202 	else
203 		kfree_sensitive(payload);
204 	return ret;
205 }
206 
207 static void trusted_rcu_free(struct rcu_head *rcu)
208 {
209 	struct trusted_key_payload *p;
210 
211 	p = container_of(rcu, struct trusted_key_payload, rcu);
212 	kfree_sensitive(p);
213 }
214 
215 /*
216  * trusted_update - reseal an existing key with new PCR values
217  */
218 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
219 {
220 	struct trusted_key_payload *p;
221 	struct trusted_key_payload *new_p;
222 	size_t datalen = prep->datalen;
223 	char *datablob, *orig_datablob;
224 	int ret = 0;
225 
226 	if (key_is_negative(key))
227 		return -ENOKEY;
228 	p = key->payload.data[0];
229 	if (!p->migratable)
230 		return -EPERM;
231 	if (datalen <= 0 || datalen > 32767 || !prep->data)
232 		return -EINVAL;
233 
234 	orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
235 	if (!datablob)
236 		return -ENOMEM;
237 
238 	new_p = trusted_payload_alloc(key);
239 	if (!new_p) {
240 		ret = -ENOMEM;
241 		goto out;
242 	}
243 
244 	memcpy(datablob, prep->data, datalen);
245 	datablob[datalen] = '\0';
246 	ret = datablob_parse(&datablob, new_p);
247 	if (ret != Opt_update) {
248 		ret = -EINVAL;
249 		kfree_sensitive(new_p);
250 		goto out;
251 	}
252 
253 	/* copy old key values, and reseal with new pcrs */
254 	new_p->migratable = p->migratable;
255 	new_p->key_len = p->key_len;
256 	memcpy(new_p->key, p->key, p->key_len);
257 	dump_payload(p);
258 	dump_payload(new_p);
259 
260 	ret = static_call(trusted_key_seal)(new_p, datablob);
261 	if (ret < 0) {
262 		pr_info("key_seal failed (%d)\n", ret);
263 		kfree_sensitive(new_p);
264 		goto out;
265 	}
266 
267 	rcu_assign_keypointer(key, new_p);
268 	call_rcu(&p->rcu, trusted_rcu_free);
269 out:
270 	kfree_sensitive(orig_datablob);
271 	return ret;
272 }
273 
274 /*
275  * trusted_read - copy the sealed blob data to userspace in hex.
276  * On success, return to userspace the trusted key datablob size.
277  */
278 static long trusted_read(const struct key *key, char *buffer,
279 			 size_t buflen)
280 {
281 	const struct trusted_key_payload *p;
282 	char *bufp;
283 	int i;
284 
285 	p = dereference_key_locked(key);
286 	if (!p)
287 		return -EINVAL;
288 
289 	if (buffer && buflen >= 2 * p->blob_len) {
290 		bufp = buffer;
291 		for (i = 0; i < p->blob_len; i++)
292 			bufp = hex_byte_pack(bufp, p->blob[i]);
293 	}
294 	return 2 * p->blob_len;
295 }
296 
297 /*
298  * trusted_destroy - clear and free the key's payload
299  */
300 static void trusted_destroy(struct key *key)
301 {
302 	kfree_sensitive(key->payload.data[0]);
303 }
304 
305 struct key_type key_type_trusted = {
306 	.name = "trusted",
307 	.instantiate = trusted_instantiate,
308 	.update = trusted_update,
309 	.destroy = trusted_destroy,
310 	.describe = user_describe,
311 	.read = trusted_read,
312 };
313 EXPORT_SYMBOL_GPL(key_type_trusted);
314 
315 static int __init init_trusted(void)
316 {
317 	int i, ret = 0;
318 
319 	for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
320 		if (trusted_key_source &&
321 		    strncmp(trusted_key_source, trusted_key_sources[i].name,
322 			    strlen(trusted_key_sources[i].name)))
323 			continue;
324 
325 		static_call_update(trusted_key_init,
326 				   trusted_key_sources[i].ops->init);
327 		static_call_update(trusted_key_seal,
328 				   trusted_key_sources[i].ops->seal);
329 		static_call_update(trusted_key_unseal,
330 				   trusted_key_sources[i].ops->unseal);
331 		static_call_update(trusted_key_get_random,
332 				   trusted_key_sources[i].ops->get_random);
333 		static_call_update(trusted_key_exit,
334 				   trusted_key_sources[i].ops->exit);
335 		migratable = trusted_key_sources[i].ops->migratable;
336 
337 		ret = static_call(trusted_key_init)();
338 		if (!ret)
339 			break;
340 	}
341 
342 	/*
343 	 * encrypted_keys.ko depends on successful load of this module even if
344 	 * trusted key implementation is not found.
345 	 */
346 	if (ret == -ENODEV)
347 		return 0;
348 
349 	return ret;
350 }
351 
352 static void __exit cleanup_trusted(void)
353 {
354 	static_call(trusted_key_exit)();
355 }
356 
357 late_initcall(init_trusted);
358 module_exit(cleanup_trusted);
359 
360 MODULE_LICENSE("GPL");
361