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 		return p;
120 	p = kzalloc(sizeof(*p), GFP_KERNEL);
121 
122 	p->migratable = migratable;
123 
124 	return p;
125 }
126 
127 /*
128  * trusted_instantiate - create a new trusted key
129  *
130  * Unseal an existing trusted blob or, for a new key, get a
131  * random key, then seal and create a trusted key-type key,
132  * adding it to the specified keyring.
133  *
134  * On success, return 0. Otherwise return errno.
135  */
136 static int trusted_instantiate(struct key *key,
137 			       struct key_preparsed_payload *prep)
138 {
139 	struct trusted_key_payload *payload = NULL;
140 	size_t datalen = prep->datalen;
141 	char *datablob;
142 	int ret = 0;
143 	int key_cmd;
144 	size_t key_len;
145 
146 	if (datalen <= 0 || datalen > 32767 || !prep->data)
147 		return -EINVAL;
148 
149 	datablob = kmalloc(datalen + 1, GFP_KERNEL);
150 	if (!datablob)
151 		return -ENOMEM;
152 	memcpy(datablob, prep->data, datalen);
153 	datablob[datalen] = '\0';
154 
155 	payload = trusted_payload_alloc(key);
156 	if (!payload) {
157 		ret = -ENOMEM;
158 		goto out;
159 	}
160 
161 	key_cmd = datablob_parse(datablob, payload);
162 	if (key_cmd < 0) {
163 		ret = key_cmd;
164 		goto out;
165 	}
166 
167 	dump_payload(payload);
168 
169 	switch (key_cmd) {
170 	case Opt_load:
171 		ret = static_call(trusted_key_unseal)(payload, datablob);
172 		dump_payload(payload);
173 		if (ret < 0)
174 			pr_info("key_unseal failed (%d)\n", ret);
175 		break;
176 	case Opt_new:
177 		key_len = payload->key_len;
178 		ret = static_call(trusted_key_get_random)(payload->key,
179 							  key_len);
180 		if (ret < 0)
181 			goto out;
182 
183 		if (ret != key_len) {
184 			pr_info("key_create failed (%d)\n", ret);
185 			ret = -EIO;
186 			goto out;
187 		}
188 
189 		ret = static_call(trusted_key_seal)(payload, datablob);
190 		if (ret < 0)
191 			pr_info("key_seal failed (%d)\n", ret);
192 		break;
193 	default:
194 		ret = -EINVAL;
195 	}
196 out:
197 	kfree_sensitive(datablob);
198 	if (!ret)
199 		rcu_assign_keypointer(key, payload);
200 	else
201 		kfree_sensitive(payload);
202 	return ret;
203 }
204 
205 static void trusted_rcu_free(struct rcu_head *rcu)
206 {
207 	struct trusted_key_payload *p;
208 
209 	p = container_of(rcu, struct trusted_key_payload, rcu);
210 	kfree_sensitive(p);
211 }
212 
213 /*
214  * trusted_update - reseal an existing key with new PCR values
215  */
216 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
217 {
218 	struct trusted_key_payload *p;
219 	struct trusted_key_payload *new_p;
220 	size_t datalen = prep->datalen;
221 	char *datablob;
222 	int ret = 0;
223 
224 	if (key_is_negative(key))
225 		return -ENOKEY;
226 	p = key->payload.data[0];
227 	if (!p->migratable)
228 		return -EPERM;
229 	if (datalen <= 0 || datalen > 32767 || !prep->data)
230 		return -EINVAL;
231 
232 	datablob = kmalloc(datalen + 1, GFP_KERNEL);
233 	if (!datablob)
234 		return -ENOMEM;
235 
236 	new_p = trusted_payload_alloc(key);
237 	if (!new_p) {
238 		ret = -ENOMEM;
239 		goto out;
240 	}
241 
242 	memcpy(datablob, prep->data, datalen);
243 	datablob[datalen] = '\0';
244 	ret = datablob_parse(datablob, new_p);
245 	if (ret != Opt_update) {
246 		ret = -EINVAL;
247 		kfree_sensitive(new_p);
248 		goto out;
249 	}
250 
251 	/* copy old key values, and reseal with new pcrs */
252 	new_p->migratable = p->migratable;
253 	new_p->key_len = p->key_len;
254 	memcpy(new_p->key, p->key, p->key_len);
255 	dump_payload(p);
256 	dump_payload(new_p);
257 
258 	ret = static_call(trusted_key_seal)(new_p, datablob);
259 	if (ret < 0) {
260 		pr_info("key_seal failed (%d)\n", ret);
261 		kfree_sensitive(new_p);
262 		goto out;
263 	}
264 
265 	rcu_assign_keypointer(key, new_p);
266 	call_rcu(&p->rcu, trusted_rcu_free);
267 out:
268 	kfree_sensitive(datablob);
269 	return ret;
270 }
271 
272 /*
273  * trusted_read - copy the sealed blob data to userspace in hex.
274  * On success, return to userspace the trusted key datablob size.
275  */
276 static long trusted_read(const struct key *key, char *buffer,
277 			 size_t buflen)
278 {
279 	const struct trusted_key_payload *p;
280 	char *bufp;
281 	int i;
282 
283 	p = dereference_key_locked(key);
284 	if (!p)
285 		return -EINVAL;
286 
287 	if (buffer && buflen >= 2 * p->blob_len) {
288 		bufp = buffer;
289 		for (i = 0; i < p->blob_len; i++)
290 			bufp = hex_byte_pack(bufp, p->blob[i]);
291 	}
292 	return 2 * p->blob_len;
293 }
294 
295 /*
296  * trusted_destroy - clear and free the key's payload
297  */
298 static void trusted_destroy(struct key *key)
299 {
300 	kfree_sensitive(key->payload.data[0]);
301 }
302 
303 struct key_type key_type_trusted = {
304 	.name = "trusted",
305 	.instantiate = trusted_instantiate,
306 	.update = trusted_update,
307 	.destroy = trusted_destroy,
308 	.describe = user_describe,
309 	.read = trusted_read,
310 };
311 EXPORT_SYMBOL_GPL(key_type_trusted);
312 
313 static int __init init_trusted(void)
314 {
315 	int i, ret = 0;
316 
317 	for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
318 		if (trusted_key_source &&
319 		    strncmp(trusted_key_source, trusted_key_sources[i].name,
320 			    strlen(trusted_key_sources[i].name)))
321 			continue;
322 
323 		static_call_update(trusted_key_init,
324 				   trusted_key_sources[i].ops->init);
325 		static_call_update(trusted_key_seal,
326 				   trusted_key_sources[i].ops->seal);
327 		static_call_update(trusted_key_unseal,
328 				   trusted_key_sources[i].ops->unseal);
329 		static_call_update(trusted_key_get_random,
330 				   trusted_key_sources[i].ops->get_random);
331 		static_call_update(trusted_key_exit,
332 				   trusted_key_sources[i].ops->exit);
333 		migratable = trusted_key_sources[i].ops->migratable;
334 
335 		ret = static_call(trusted_key_init)();
336 		if (!ret)
337 			break;
338 	}
339 
340 	/*
341 	 * encrypted_keys.ko depends on successful load of this module even if
342 	 * trusted key implementation is not found.
343 	 */
344 	if (ret == -ENODEV)
345 		return 0;
346 
347 	return ret;
348 }
349 
350 static void __exit cleanup_trusted(void)
351 {
352 	static_call(trusted_key_exit)();
353 }
354 
355 late_initcall(init_trusted);
356 module_exit(cleanup_trusted);
357 
358 MODULE_LICENSE("GPL");
359