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