1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015-2016, Linaro Limited
4 */
5
6 #define pr_fmt(fmt) "%s: " fmt, __func__
7
8 #include <linux/cdev.h>
9 #include <linux/cred.h>
10 #include <linux/fs.h>
11 #include <linux/idr.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/tee_drv.h>
15 #include <linux/uaccess.h>
16 #include <crypto/hash.h>
17 #include <crypto/sha.h>
18 #include "tee_private.h"
19
20 #define TEE_NUM_DEVICES 32
21
22 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
23
24 #define TEE_UUID_NS_NAME_SIZE 128
25
26 /*
27 * TEE Client UUID name space identifier (UUIDv4)
28 *
29 * Value here is random UUID that is allocated as name space identifier for
30 * forming Client UUID's for TEE environment using UUIDv5 scheme.
31 */
32 static const uuid_t tee_client_uuid_ns = UUID_INIT(0x58ac9ca0, 0x2086, 0x4683,
33 0xa1, 0xb8, 0xec, 0x4b,
34 0xc0, 0x8e, 0x01, 0xb6);
35
36 /*
37 * Unprivileged devices in the lower half range and privileged devices in
38 * the upper half range.
39 */
40 static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES);
41 static DEFINE_SPINLOCK(driver_lock);
42
43 static struct class *tee_class;
44 static dev_t tee_devt;
45
teedev_open(struct tee_device * teedev)46 static struct tee_context *teedev_open(struct tee_device *teedev)
47 {
48 int rc;
49 struct tee_context *ctx;
50
51 if (!tee_device_get(teedev))
52 return ERR_PTR(-EINVAL);
53
54 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
55 if (!ctx) {
56 rc = -ENOMEM;
57 goto err;
58 }
59
60 kref_init(&ctx->refcount);
61 ctx->teedev = teedev;
62 rc = teedev->desc->ops->open(ctx);
63 if (rc)
64 goto err;
65
66 return ctx;
67 err:
68 kfree(ctx);
69 tee_device_put(teedev);
70 return ERR_PTR(rc);
71
72 }
73
teedev_ctx_get(struct tee_context * ctx)74 void teedev_ctx_get(struct tee_context *ctx)
75 {
76 if (ctx->releasing)
77 return;
78
79 kref_get(&ctx->refcount);
80 }
81
teedev_ctx_release(struct kref * ref)82 static void teedev_ctx_release(struct kref *ref)
83 {
84 struct tee_context *ctx = container_of(ref, struct tee_context,
85 refcount);
86 ctx->releasing = true;
87 ctx->teedev->desc->ops->release(ctx);
88 kfree(ctx);
89 }
90
teedev_ctx_put(struct tee_context * ctx)91 void teedev_ctx_put(struct tee_context *ctx)
92 {
93 if (ctx->releasing)
94 return;
95
96 kref_put(&ctx->refcount, teedev_ctx_release);
97 }
98
teedev_close_context(struct tee_context * ctx)99 static void teedev_close_context(struct tee_context *ctx)
100 {
101 tee_device_put(ctx->teedev);
102 teedev_ctx_put(ctx);
103 }
104
tee_open(struct inode * inode,struct file * filp)105 static int tee_open(struct inode *inode, struct file *filp)
106 {
107 struct tee_context *ctx;
108
109 ctx = teedev_open(container_of(inode->i_cdev, struct tee_device, cdev));
110 if (IS_ERR(ctx))
111 return PTR_ERR(ctx);
112
113 /*
114 * Default user-space behaviour is to wait for tee-supplicant
115 * if not present for any requests in this context.
116 */
117 ctx->supp_nowait = false;
118 filp->private_data = ctx;
119 return 0;
120 }
121
tee_release(struct inode * inode,struct file * filp)122 static int tee_release(struct inode *inode, struct file *filp)
123 {
124 teedev_close_context(filp->private_data);
125 return 0;
126 }
127
128 /**
129 * uuid_v5() - Calculate UUIDv5
130 * @uuid: Resulting UUID
131 * @ns: Name space ID for UUIDv5 function
132 * @name: Name for UUIDv5 function
133 * @size: Size of name
134 *
135 * UUIDv5 is specific in RFC 4122.
136 *
137 * This implements section (for SHA-1):
138 * 4.3. Algorithm for Creating a Name-Based UUID
139 */
uuid_v5(uuid_t * uuid,const uuid_t * ns,const void * name,size_t size)140 static int uuid_v5(uuid_t *uuid, const uuid_t *ns, const void *name,
141 size_t size)
142 {
143 unsigned char hash[SHA1_DIGEST_SIZE];
144 struct crypto_shash *shash = NULL;
145 struct shash_desc *desc = NULL;
146 int rc;
147
148 shash = crypto_alloc_shash("sha1", 0, 0);
149 if (IS_ERR(shash)) {
150 rc = PTR_ERR(shash);
151 pr_err("shash(sha1) allocation failed\n");
152 return rc;
153 }
154
155 desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
156 GFP_KERNEL);
157 if (!desc) {
158 rc = -ENOMEM;
159 goto out_free_shash;
160 }
161
162 desc->tfm = shash;
163
164 rc = crypto_shash_init(desc);
165 if (rc < 0)
166 goto out_free_desc;
167
168 rc = crypto_shash_update(desc, (const u8 *)ns, sizeof(*ns));
169 if (rc < 0)
170 goto out_free_desc;
171
172 rc = crypto_shash_update(desc, (const u8 *)name, size);
173 if (rc < 0)
174 goto out_free_desc;
175
176 rc = crypto_shash_final(desc, hash);
177 if (rc < 0)
178 goto out_free_desc;
179
180 memcpy(uuid->b, hash, UUID_SIZE);
181
182 /* Tag for version 5 */
183 uuid->b[6] = (hash[6] & 0x0F) | 0x50;
184 uuid->b[8] = (hash[8] & 0x3F) | 0x80;
185
186 out_free_desc:
187 kfree(desc);
188
189 out_free_shash:
190 crypto_free_shash(shash);
191 return rc;
192 }
193
tee_session_calc_client_uuid(uuid_t * uuid,u32 connection_method,const u8 connection_data[TEE_IOCTL_UUID_LEN])194 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
195 const u8 connection_data[TEE_IOCTL_UUID_LEN])
196 {
197 gid_t ns_grp = (gid_t)-1;
198 kgid_t grp = INVALID_GID;
199 char *name = NULL;
200 int name_len;
201 int rc;
202
203 if (connection_method == TEE_IOCTL_LOGIN_PUBLIC ||
204 connection_method == TEE_IOCTL_LOGIN_REE_KERNEL) {
205 /* Nil UUID to be passed to TEE environment */
206 uuid_copy(uuid, &uuid_null);
207 return 0;
208 }
209
210 /*
211 * In Linux environment client UUID is based on UUIDv5.
212 *
213 * Determine client UUID with following semantics for 'name':
214 *
215 * For TEEC_LOGIN_USER:
216 * uid=<uid>
217 *
218 * For TEEC_LOGIN_GROUP:
219 * gid=<gid>
220 *
221 */
222
223 name = kzalloc(TEE_UUID_NS_NAME_SIZE, GFP_KERNEL);
224 if (!name)
225 return -ENOMEM;
226
227 switch (connection_method) {
228 case TEE_IOCTL_LOGIN_USER:
229 name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "uid=%x",
230 current_euid().val);
231 if (name_len >= TEE_UUID_NS_NAME_SIZE) {
232 rc = -E2BIG;
233 goto out_free_name;
234 }
235 break;
236
237 case TEE_IOCTL_LOGIN_GROUP:
238 memcpy(&ns_grp, connection_data, sizeof(gid_t));
239 grp = make_kgid(current_user_ns(), ns_grp);
240 if (!gid_valid(grp) || !in_egroup_p(grp)) {
241 rc = -EPERM;
242 goto out_free_name;
243 }
244
245 name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "gid=%x",
246 grp.val);
247 if (name_len >= TEE_UUID_NS_NAME_SIZE) {
248 rc = -E2BIG;
249 goto out_free_name;
250 }
251 break;
252
253 default:
254 rc = -EINVAL;
255 goto out_free_name;
256 }
257
258 rc = uuid_v5(uuid, &tee_client_uuid_ns, name, name_len);
259 out_free_name:
260 kfree(name);
261
262 return rc;
263 }
264 EXPORT_SYMBOL_GPL(tee_session_calc_client_uuid);
265
tee_ioctl_version(struct tee_context * ctx,struct tee_ioctl_version_data __user * uvers)266 static int tee_ioctl_version(struct tee_context *ctx,
267 struct tee_ioctl_version_data __user *uvers)
268 {
269 struct tee_ioctl_version_data vers;
270
271 ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
272
273 if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
274 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
275
276 if (copy_to_user(uvers, &vers, sizeof(vers)))
277 return -EFAULT;
278
279 return 0;
280 }
281
tee_ioctl_shm_alloc(struct tee_context * ctx,struct tee_ioctl_shm_alloc_data __user * udata)282 static int tee_ioctl_shm_alloc(struct tee_context *ctx,
283 struct tee_ioctl_shm_alloc_data __user *udata)
284 {
285 long ret;
286 struct tee_ioctl_shm_alloc_data data;
287 struct tee_shm *shm;
288
289 if (copy_from_user(&data, udata, sizeof(data)))
290 return -EFAULT;
291
292 /* Currently no input flags are supported */
293 if (data.flags)
294 return -EINVAL;
295
296 shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
297 if (IS_ERR(shm))
298 return PTR_ERR(shm);
299
300 data.id = shm->id;
301 data.flags = shm->flags;
302 data.size = shm->size;
303
304 if (copy_to_user(udata, &data, sizeof(data)))
305 ret = -EFAULT;
306 else
307 ret = tee_shm_get_fd(shm);
308
309 /*
310 * When user space closes the file descriptor the shared memory
311 * should be freed or if tee_shm_get_fd() failed then it will
312 * be freed immediately.
313 */
314 tee_shm_put(shm);
315 return ret;
316 }
317
318 static int
tee_ioctl_shm_register(struct tee_context * ctx,struct tee_ioctl_shm_register_data __user * udata)319 tee_ioctl_shm_register(struct tee_context *ctx,
320 struct tee_ioctl_shm_register_data __user *udata)
321 {
322 long ret;
323 struct tee_ioctl_shm_register_data data;
324 struct tee_shm *shm;
325
326 if (copy_from_user(&data, udata, sizeof(data)))
327 return -EFAULT;
328
329 /* Currently no input flags are supported */
330 if (data.flags)
331 return -EINVAL;
332
333 shm = tee_shm_register(ctx, data.addr, data.length,
334 TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED);
335 if (IS_ERR(shm))
336 return PTR_ERR(shm);
337
338 data.id = shm->id;
339 data.flags = shm->flags;
340 data.length = shm->size;
341
342 if (copy_to_user(udata, &data, sizeof(data)))
343 ret = -EFAULT;
344 else
345 ret = tee_shm_get_fd(shm);
346 /*
347 * When user space closes the file descriptor the shared memory
348 * should be freed or if tee_shm_get_fd() failed then it will
349 * be freed immediately.
350 */
351 tee_shm_put(shm);
352 return ret;
353 }
354
params_from_user(struct tee_context * ctx,struct tee_param * params,size_t num_params,struct tee_ioctl_param __user * uparams)355 static int params_from_user(struct tee_context *ctx, struct tee_param *params,
356 size_t num_params,
357 struct tee_ioctl_param __user *uparams)
358 {
359 size_t n;
360
361 for (n = 0; n < num_params; n++) {
362 struct tee_shm *shm;
363 struct tee_ioctl_param ip;
364
365 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
366 return -EFAULT;
367
368 /* All unused attribute bits has to be zero */
369 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
370 return -EINVAL;
371
372 params[n].attr = ip.attr;
373 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
374 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
375 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
376 break;
377 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
378 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
379 params[n].u.value.a = ip.a;
380 params[n].u.value.b = ip.b;
381 params[n].u.value.c = ip.c;
382 break;
383 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
384 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
385 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
386 /*
387 * If a NULL pointer is passed to a TA in the TEE,
388 * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL
389 * indicating a NULL memory reference.
390 */
391 if (ip.c != TEE_MEMREF_NULL) {
392 /*
393 * If we fail to get a pointer to a shared
394 * memory object (and increase the ref count)
395 * from an identifier we return an error. All
396 * pointers that has been added in params have
397 * an increased ref count. It's the callers
398 * responibility to do tee_shm_put() on all
399 * resolved pointers.
400 */
401 shm = tee_shm_get_from_id(ctx, ip.c);
402 if (IS_ERR(shm))
403 return PTR_ERR(shm);
404
405 /*
406 * Ensure offset + size does not overflow
407 * offset and does not overflow the size of
408 * the referred shared memory object.
409 */
410 if ((ip.a + ip.b) < ip.a ||
411 (ip.a + ip.b) > shm->size) {
412 tee_shm_put(shm);
413 return -EINVAL;
414 }
415 } else if (ctx->cap_memref_null) {
416 /* Pass NULL pointer to OP-TEE */
417 shm = NULL;
418 } else {
419 return -EINVAL;
420 }
421
422 params[n].u.memref.shm_offs = ip.a;
423 params[n].u.memref.size = ip.b;
424 params[n].u.memref.shm = shm;
425 break;
426 default:
427 /* Unknown attribute */
428 return -EINVAL;
429 }
430 }
431 return 0;
432 }
433
params_to_user(struct tee_ioctl_param __user * uparams,size_t num_params,struct tee_param * params)434 static int params_to_user(struct tee_ioctl_param __user *uparams,
435 size_t num_params, struct tee_param *params)
436 {
437 size_t n;
438
439 for (n = 0; n < num_params; n++) {
440 struct tee_ioctl_param __user *up = uparams + n;
441 struct tee_param *p = params + n;
442
443 switch (p->attr) {
444 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
445 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
446 if (put_user(p->u.value.a, &up->a) ||
447 put_user(p->u.value.b, &up->b) ||
448 put_user(p->u.value.c, &up->c))
449 return -EFAULT;
450 break;
451 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
452 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
453 if (put_user((u64)p->u.memref.size, &up->b))
454 return -EFAULT;
455 default:
456 break;
457 }
458 }
459 return 0;
460 }
461
tee_ioctl_open_session(struct tee_context * ctx,struct tee_ioctl_buf_data __user * ubuf)462 static int tee_ioctl_open_session(struct tee_context *ctx,
463 struct tee_ioctl_buf_data __user *ubuf)
464 {
465 int rc;
466 size_t n;
467 struct tee_ioctl_buf_data buf;
468 struct tee_ioctl_open_session_arg __user *uarg;
469 struct tee_ioctl_open_session_arg arg;
470 struct tee_ioctl_param __user *uparams = NULL;
471 struct tee_param *params = NULL;
472 bool have_session = false;
473
474 if (!ctx->teedev->desc->ops->open_session)
475 return -EINVAL;
476
477 if (copy_from_user(&buf, ubuf, sizeof(buf)))
478 return -EFAULT;
479
480 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
481 buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
482 return -EINVAL;
483
484 uarg = u64_to_user_ptr(buf.buf_ptr);
485 if (copy_from_user(&arg, uarg, sizeof(arg)))
486 return -EFAULT;
487
488 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
489 return -EINVAL;
490
491 if (arg.num_params) {
492 params = kcalloc(arg.num_params, sizeof(struct tee_param),
493 GFP_KERNEL);
494 if (!params)
495 return -ENOMEM;
496 uparams = uarg->params;
497 rc = params_from_user(ctx, params, arg.num_params, uparams);
498 if (rc)
499 goto out;
500 }
501
502 if (arg.clnt_login >= TEE_IOCTL_LOGIN_REE_KERNEL_MIN &&
503 arg.clnt_login <= TEE_IOCTL_LOGIN_REE_KERNEL_MAX) {
504 pr_debug("login method not allowed for user-space client\n");
505 rc = -EPERM;
506 goto out;
507 }
508
509 rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
510 if (rc)
511 goto out;
512 have_session = true;
513
514 if (put_user(arg.session, &uarg->session) ||
515 put_user(arg.ret, &uarg->ret) ||
516 put_user(arg.ret_origin, &uarg->ret_origin)) {
517 rc = -EFAULT;
518 goto out;
519 }
520 rc = params_to_user(uparams, arg.num_params, params);
521 out:
522 /*
523 * If we've succeeded to open the session but failed to communicate
524 * it back to user space, close the session again to avoid leakage.
525 */
526 if (rc && have_session && ctx->teedev->desc->ops->close_session)
527 ctx->teedev->desc->ops->close_session(ctx, arg.session);
528
529 if (params) {
530 /* Decrease ref count for all valid shared memory pointers */
531 for (n = 0; n < arg.num_params; n++)
532 if (tee_param_is_memref(params + n) &&
533 params[n].u.memref.shm)
534 tee_shm_put(params[n].u.memref.shm);
535 kfree(params);
536 }
537
538 return rc;
539 }
540
tee_ioctl_invoke(struct tee_context * ctx,struct tee_ioctl_buf_data __user * ubuf)541 static int tee_ioctl_invoke(struct tee_context *ctx,
542 struct tee_ioctl_buf_data __user *ubuf)
543 {
544 int rc;
545 size_t n;
546 struct tee_ioctl_buf_data buf;
547 struct tee_ioctl_invoke_arg __user *uarg;
548 struct tee_ioctl_invoke_arg arg;
549 struct tee_ioctl_param __user *uparams = NULL;
550 struct tee_param *params = NULL;
551
552 if (!ctx->teedev->desc->ops->invoke_func)
553 return -EINVAL;
554
555 if (copy_from_user(&buf, ubuf, sizeof(buf)))
556 return -EFAULT;
557
558 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
559 buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
560 return -EINVAL;
561
562 uarg = u64_to_user_ptr(buf.buf_ptr);
563 if (copy_from_user(&arg, uarg, sizeof(arg)))
564 return -EFAULT;
565
566 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
567 return -EINVAL;
568
569 if (arg.num_params) {
570 params = kcalloc(arg.num_params, sizeof(struct tee_param),
571 GFP_KERNEL);
572 if (!params)
573 return -ENOMEM;
574 uparams = uarg->params;
575 rc = params_from_user(ctx, params, arg.num_params, uparams);
576 if (rc)
577 goto out;
578 }
579
580 rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
581 if (rc)
582 goto out;
583
584 if (put_user(arg.ret, &uarg->ret) ||
585 put_user(arg.ret_origin, &uarg->ret_origin)) {
586 rc = -EFAULT;
587 goto out;
588 }
589 rc = params_to_user(uparams, arg.num_params, params);
590 out:
591 if (params) {
592 /* Decrease ref count for all valid shared memory pointers */
593 for (n = 0; n < arg.num_params; n++)
594 if (tee_param_is_memref(params + n) &&
595 params[n].u.memref.shm)
596 tee_shm_put(params[n].u.memref.shm);
597 kfree(params);
598 }
599 return rc;
600 }
601
tee_ioctl_cancel(struct tee_context * ctx,struct tee_ioctl_cancel_arg __user * uarg)602 static int tee_ioctl_cancel(struct tee_context *ctx,
603 struct tee_ioctl_cancel_arg __user *uarg)
604 {
605 struct tee_ioctl_cancel_arg arg;
606
607 if (!ctx->teedev->desc->ops->cancel_req)
608 return -EINVAL;
609
610 if (copy_from_user(&arg, uarg, sizeof(arg)))
611 return -EFAULT;
612
613 return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
614 arg.session);
615 }
616
617 static int
tee_ioctl_close_session(struct tee_context * ctx,struct tee_ioctl_close_session_arg __user * uarg)618 tee_ioctl_close_session(struct tee_context *ctx,
619 struct tee_ioctl_close_session_arg __user *uarg)
620 {
621 struct tee_ioctl_close_session_arg arg;
622
623 if (!ctx->teedev->desc->ops->close_session)
624 return -EINVAL;
625
626 if (copy_from_user(&arg, uarg, sizeof(arg)))
627 return -EFAULT;
628
629 return ctx->teedev->desc->ops->close_session(ctx, arg.session);
630 }
631
params_to_supp(struct tee_context * ctx,struct tee_ioctl_param __user * uparams,size_t num_params,struct tee_param * params)632 static int params_to_supp(struct tee_context *ctx,
633 struct tee_ioctl_param __user *uparams,
634 size_t num_params, struct tee_param *params)
635 {
636 size_t n;
637
638 for (n = 0; n < num_params; n++) {
639 struct tee_ioctl_param ip;
640 struct tee_param *p = params + n;
641
642 ip.attr = p->attr;
643 switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
644 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
645 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
646 ip.a = p->u.value.a;
647 ip.b = p->u.value.b;
648 ip.c = p->u.value.c;
649 break;
650 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
651 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
652 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
653 ip.b = p->u.memref.size;
654 if (!p->u.memref.shm) {
655 ip.a = 0;
656 ip.c = (u64)-1; /* invalid shm id */
657 break;
658 }
659 ip.a = p->u.memref.shm_offs;
660 ip.c = p->u.memref.shm->id;
661 break;
662 default:
663 ip.a = 0;
664 ip.b = 0;
665 ip.c = 0;
666 break;
667 }
668
669 if (copy_to_user(uparams + n, &ip, sizeof(ip)))
670 return -EFAULT;
671 }
672
673 return 0;
674 }
675
tee_ioctl_supp_recv(struct tee_context * ctx,struct tee_ioctl_buf_data __user * ubuf)676 static int tee_ioctl_supp_recv(struct tee_context *ctx,
677 struct tee_ioctl_buf_data __user *ubuf)
678 {
679 int rc;
680 struct tee_ioctl_buf_data buf;
681 struct tee_iocl_supp_recv_arg __user *uarg;
682 struct tee_param *params;
683 u32 num_params;
684 u32 func;
685
686 if (!ctx->teedev->desc->ops->supp_recv)
687 return -EINVAL;
688
689 if (copy_from_user(&buf, ubuf, sizeof(buf)))
690 return -EFAULT;
691
692 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
693 buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
694 return -EINVAL;
695
696 uarg = u64_to_user_ptr(buf.buf_ptr);
697 if (get_user(num_params, &uarg->num_params))
698 return -EFAULT;
699
700 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
701 return -EINVAL;
702
703 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
704 if (!params)
705 return -ENOMEM;
706
707 rc = params_from_user(ctx, params, num_params, uarg->params);
708 if (rc)
709 goto out;
710
711 rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
712 if (rc)
713 goto out;
714
715 if (put_user(func, &uarg->func) ||
716 put_user(num_params, &uarg->num_params)) {
717 rc = -EFAULT;
718 goto out;
719 }
720
721 rc = params_to_supp(ctx, uarg->params, num_params, params);
722 out:
723 kfree(params);
724 return rc;
725 }
726
params_from_supp(struct tee_param * params,size_t num_params,struct tee_ioctl_param __user * uparams)727 static int params_from_supp(struct tee_param *params, size_t num_params,
728 struct tee_ioctl_param __user *uparams)
729 {
730 size_t n;
731
732 for (n = 0; n < num_params; n++) {
733 struct tee_param *p = params + n;
734 struct tee_ioctl_param ip;
735
736 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
737 return -EFAULT;
738
739 /* All unused attribute bits has to be zero */
740 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
741 return -EINVAL;
742
743 p->attr = ip.attr;
744 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
745 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
746 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
747 /* Only out and in/out values can be updated */
748 p->u.value.a = ip.a;
749 p->u.value.b = ip.b;
750 p->u.value.c = ip.c;
751 break;
752 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
753 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
754 /*
755 * Only the size of the memref can be updated.
756 * Since we don't have access to the original
757 * parameters here, only store the supplied size.
758 * The driver will copy the updated size into the
759 * original parameters.
760 */
761 p->u.memref.shm = NULL;
762 p->u.memref.shm_offs = 0;
763 p->u.memref.size = ip.b;
764 break;
765 default:
766 memset(&p->u, 0, sizeof(p->u));
767 break;
768 }
769 }
770 return 0;
771 }
772
tee_ioctl_supp_send(struct tee_context * ctx,struct tee_ioctl_buf_data __user * ubuf)773 static int tee_ioctl_supp_send(struct tee_context *ctx,
774 struct tee_ioctl_buf_data __user *ubuf)
775 {
776 long rc;
777 struct tee_ioctl_buf_data buf;
778 struct tee_iocl_supp_send_arg __user *uarg;
779 struct tee_param *params;
780 u32 num_params;
781 u32 ret;
782
783 /* Not valid for this driver */
784 if (!ctx->teedev->desc->ops->supp_send)
785 return -EINVAL;
786
787 if (copy_from_user(&buf, ubuf, sizeof(buf)))
788 return -EFAULT;
789
790 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
791 buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
792 return -EINVAL;
793
794 uarg = u64_to_user_ptr(buf.buf_ptr);
795 if (get_user(ret, &uarg->ret) ||
796 get_user(num_params, &uarg->num_params))
797 return -EFAULT;
798
799 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
800 return -EINVAL;
801
802 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
803 if (!params)
804 return -ENOMEM;
805
806 rc = params_from_supp(params, num_params, uarg->params);
807 if (rc)
808 goto out;
809
810 rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
811 out:
812 kfree(params);
813 return rc;
814 }
815
tee_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)816 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
817 {
818 struct tee_context *ctx = filp->private_data;
819 void __user *uarg = (void __user *)arg;
820
821 switch (cmd) {
822 case TEE_IOC_VERSION:
823 return tee_ioctl_version(ctx, uarg);
824 case TEE_IOC_SHM_ALLOC:
825 return tee_ioctl_shm_alloc(ctx, uarg);
826 case TEE_IOC_SHM_REGISTER:
827 return tee_ioctl_shm_register(ctx, uarg);
828 case TEE_IOC_OPEN_SESSION:
829 return tee_ioctl_open_session(ctx, uarg);
830 case TEE_IOC_INVOKE:
831 return tee_ioctl_invoke(ctx, uarg);
832 case TEE_IOC_CANCEL:
833 return tee_ioctl_cancel(ctx, uarg);
834 case TEE_IOC_CLOSE_SESSION:
835 return tee_ioctl_close_session(ctx, uarg);
836 case TEE_IOC_SUPPL_RECV:
837 return tee_ioctl_supp_recv(ctx, uarg);
838 case TEE_IOC_SUPPL_SEND:
839 return tee_ioctl_supp_send(ctx, uarg);
840 default:
841 return -EINVAL;
842 }
843 }
844
845 static const struct file_operations tee_fops = {
846 .owner = THIS_MODULE,
847 .open = tee_open,
848 .release = tee_release,
849 .unlocked_ioctl = tee_ioctl,
850 .compat_ioctl = compat_ptr_ioctl,
851 };
852
tee_release_device(struct device * dev)853 static void tee_release_device(struct device *dev)
854 {
855 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
856
857 spin_lock(&driver_lock);
858 clear_bit(teedev->id, dev_mask);
859 spin_unlock(&driver_lock);
860 mutex_destroy(&teedev->mutex);
861 idr_destroy(&teedev->idr);
862 kfree(teedev);
863 }
864
865 /**
866 * tee_device_alloc() - Allocate a new struct tee_device instance
867 * @teedesc: Descriptor for this driver
868 * @dev: Parent device for this device
869 * @pool: Shared memory pool, NULL if not used
870 * @driver_data: Private driver data for this device
871 *
872 * Allocates a new struct tee_device instance. The device is
873 * removed by tee_device_unregister().
874 *
875 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
876 */
tee_device_alloc(const struct tee_desc * teedesc,struct device * dev,struct tee_shm_pool * pool,void * driver_data)877 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
878 struct device *dev,
879 struct tee_shm_pool *pool,
880 void *driver_data)
881 {
882 struct tee_device *teedev;
883 void *ret;
884 int rc, max_id;
885 int offs = 0;
886
887 if (!teedesc || !teedesc->name || !teedesc->ops ||
888 !teedesc->ops->get_version || !teedesc->ops->open ||
889 !teedesc->ops->release || !pool)
890 return ERR_PTR(-EINVAL);
891
892 teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
893 if (!teedev) {
894 ret = ERR_PTR(-ENOMEM);
895 goto err;
896 }
897
898 max_id = TEE_NUM_DEVICES / 2;
899
900 if (teedesc->flags & TEE_DESC_PRIVILEGED) {
901 offs = TEE_NUM_DEVICES / 2;
902 max_id = TEE_NUM_DEVICES;
903 }
904
905 spin_lock(&driver_lock);
906 teedev->id = find_next_zero_bit(dev_mask, max_id, offs);
907 if (teedev->id < max_id)
908 set_bit(teedev->id, dev_mask);
909 spin_unlock(&driver_lock);
910
911 if (teedev->id >= max_id) {
912 ret = ERR_PTR(-ENOMEM);
913 goto err;
914 }
915
916 snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
917 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
918 teedev->id - offs);
919
920 teedev->dev.class = tee_class;
921 teedev->dev.release = tee_release_device;
922 teedev->dev.parent = dev;
923
924 teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
925
926 rc = dev_set_name(&teedev->dev, "%s", teedev->name);
927 if (rc) {
928 ret = ERR_PTR(rc);
929 goto err_devt;
930 }
931
932 cdev_init(&teedev->cdev, &tee_fops);
933 teedev->cdev.owner = teedesc->owner;
934
935 dev_set_drvdata(&teedev->dev, driver_data);
936 device_initialize(&teedev->dev);
937
938 /* 1 as tee_device_unregister() does one final tee_device_put() */
939 teedev->num_users = 1;
940 init_completion(&teedev->c_no_users);
941 mutex_init(&teedev->mutex);
942 idr_init(&teedev->idr);
943
944 teedev->desc = teedesc;
945 teedev->pool = pool;
946
947 return teedev;
948 err_devt:
949 unregister_chrdev_region(teedev->dev.devt, 1);
950 err:
951 pr_err("could not register %s driver\n",
952 teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
953 if (teedev && teedev->id < TEE_NUM_DEVICES) {
954 spin_lock(&driver_lock);
955 clear_bit(teedev->id, dev_mask);
956 spin_unlock(&driver_lock);
957 }
958 kfree(teedev);
959 return ret;
960 }
961 EXPORT_SYMBOL_GPL(tee_device_alloc);
962
implementation_id_show(struct device * dev,struct device_attribute * attr,char * buf)963 static ssize_t implementation_id_show(struct device *dev,
964 struct device_attribute *attr, char *buf)
965 {
966 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
967 struct tee_ioctl_version_data vers;
968
969 teedev->desc->ops->get_version(teedev, &vers);
970 return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
971 }
972 static DEVICE_ATTR_RO(implementation_id);
973
974 static struct attribute *tee_dev_attrs[] = {
975 &dev_attr_implementation_id.attr,
976 NULL
977 };
978
979 ATTRIBUTE_GROUPS(tee_dev);
980
981 /**
982 * tee_device_register() - Registers a TEE device
983 * @teedev: Device to register
984 *
985 * tee_device_unregister() need to be called to remove the @teedev if
986 * this function fails.
987 *
988 * @returns < 0 on failure
989 */
tee_device_register(struct tee_device * teedev)990 int tee_device_register(struct tee_device *teedev)
991 {
992 int rc;
993
994 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
995 dev_err(&teedev->dev, "attempt to register twice\n");
996 return -EINVAL;
997 }
998
999 teedev->dev.groups = tee_dev_groups;
1000
1001 rc = cdev_device_add(&teedev->cdev, &teedev->dev);
1002 if (rc) {
1003 dev_err(&teedev->dev,
1004 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
1005 teedev->name, MAJOR(teedev->dev.devt),
1006 MINOR(teedev->dev.devt), rc);
1007 return rc;
1008 }
1009
1010 teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
1011 return 0;
1012 }
1013 EXPORT_SYMBOL_GPL(tee_device_register);
1014
tee_device_put(struct tee_device * teedev)1015 void tee_device_put(struct tee_device *teedev)
1016 {
1017 mutex_lock(&teedev->mutex);
1018 /* Shouldn't put in this state */
1019 if (!WARN_ON(!teedev->desc)) {
1020 teedev->num_users--;
1021 if (!teedev->num_users) {
1022 teedev->desc = NULL;
1023 complete(&teedev->c_no_users);
1024 }
1025 }
1026 mutex_unlock(&teedev->mutex);
1027 }
1028
tee_device_get(struct tee_device * teedev)1029 bool tee_device_get(struct tee_device *teedev)
1030 {
1031 mutex_lock(&teedev->mutex);
1032 if (!teedev->desc) {
1033 mutex_unlock(&teedev->mutex);
1034 return false;
1035 }
1036 teedev->num_users++;
1037 mutex_unlock(&teedev->mutex);
1038 return true;
1039 }
1040
1041 /**
1042 * tee_device_unregister() - Removes a TEE device
1043 * @teedev: Device to unregister
1044 *
1045 * This function should be called to remove the @teedev even if
1046 * tee_device_register() hasn't been called yet. Does nothing if
1047 * @teedev is NULL.
1048 */
tee_device_unregister(struct tee_device * teedev)1049 void tee_device_unregister(struct tee_device *teedev)
1050 {
1051 if (!teedev)
1052 return;
1053
1054 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED)
1055 cdev_device_del(&teedev->cdev, &teedev->dev);
1056
1057 tee_device_put(teedev);
1058 wait_for_completion(&teedev->c_no_users);
1059
1060 /*
1061 * No need to take a mutex any longer now since teedev->desc was
1062 * set to NULL before teedev->c_no_users was completed.
1063 */
1064
1065 teedev->pool = NULL;
1066
1067 put_device(&teedev->dev);
1068 }
1069 EXPORT_SYMBOL_GPL(tee_device_unregister);
1070
1071 /**
1072 * tee_get_drvdata() - Return driver_data pointer
1073 * @teedev: Device containing the driver_data pointer
1074 * @returns the driver_data pointer supplied to tee_register().
1075 */
tee_get_drvdata(struct tee_device * teedev)1076 void *tee_get_drvdata(struct tee_device *teedev)
1077 {
1078 return dev_get_drvdata(&teedev->dev);
1079 }
1080 EXPORT_SYMBOL_GPL(tee_get_drvdata);
1081
1082 struct match_dev_data {
1083 struct tee_ioctl_version_data *vers;
1084 const void *data;
1085 int (*match)(struct tee_ioctl_version_data *, const void *);
1086 };
1087
match_dev(struct device * dev,const void * data)1088 static int match_dev(struct device *dev, const void *data)
1089 {
1090 const struct match_dev_data *match_data = data;
1091 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
1092
1093 teedev->desc->ops->get_version(teedev, match_data->vers);
1094 return match_data->match(match_data->vers, match_data->data);
1095 }
1096
1097 struct tee_context *
tee_client_open_context(struct tee_context * start,int (* match)(struct tee_ioctl_version_data *,const void *),const void * data,struct tee_ioctl_version_data * vers)1098 tee_client_open_context(struct tee_context *start,
1099 int (*match)(struct tee_ioctl_version_data *,
1100 const void *),
1101 const void *data, struct tee_ioctl_version_data *vers)
1102 {
1103 struct device *dev = NULL;
1104 struct device *put_dev = NULL;
1105 struct tee_context *ctx = NULL;
1106 struct tee_ioctl_version_data v;
1107 struct match_dev_data match_data = { vers ? vers : &v, data, match };
1108
1109 if (start)
1110 dev = &start->teedev->dev;
1111
1112 do {
1113 dev = class_find_device(tee_class, dev, &match_data, match_dev);
1114 if (!dev) {
1115 ctx = ERR_PTR(-ENOENT);
1116 break;
1117 }
1118
1119 put_device(put_dev);
1120 put_dev = dev;
1121
1122 ctx = teedev_open(container_of(dev, struct tee_device, dev));
1123 } while (IS_ERR(ctx) && PTR_ERR(ctx) != -ENOMEM);
1124
1125 put_device(put_dev);
1126 /*
1127 * Default behaviour for in kernel client is to not wait for
1128 * tee-supplicant if not present for any requests in this context.
1129 * Also this flag could be configured again before call to
1130 * tee_client_open_session() if any in kernel client requires
1131 * different behaviour.
1132 */
1133 if (!IS_ERR(ctx))
1134 ctx->supp_nowait = true;
1135
1136 return ctx;
1137 }
1138 EXPORT_SYMBOL_GPL(tee_client_open_context);
1139
tee_client_close_context(struct tee_context * ctx)1140 void tee_client_close_context(struct tee_context *ctx)
1141 {
1142 teedev_close_context(ctx);
1143 }
1144 EXPORT_SYMBOL_GPL(tee_client_close_context);
1145
tee_client_get_version(struct tee_context * ctx,struct tee_ioctl_version_data * vers)1146 void tee_client_get_version(struct tee_context *ctx,
1147 struct tee_ioctl_version_data *vers)
1148 {
1149 ctx->teedev->desc->ops->get_version(ctx->teedev, vers);
1150 }
1151 EXPORT_SYMBOL_GPL(tee_client_get_version);
1152
tee_client_open_session(struct tee_context * ctx,struct tee_ioctl_open_session_arg * arg,struct tee_param * param)1153 int tee_client_open_session(struct tee_context *ctx,
1154 struct tee_ioctl_open_session_arg *arg,
1155 struct tee_param *param)
1156 {
1157 if (!ctx->teedev->desc->ops->open_session)
1158 return -EINVAL;
1159 return ctx->teedev->desc->ops->open_session(ctx, arg, param);
1160 }
1161 EXPORT_SYMBOL_GPL(tee_client_open_session);
1162
tee_client_close_session(struct tee_context * ctx,u32 session)1163 int tee_client_close_session(struct tee_context *ctx, u32 session)
1164 {
1165 if (!ctx->teedev->desc->ops->close_session)
1166 return -EINVAL;
1167 return ctx->teedev->desc->ops->close_session(ctx, session);
1168 }
1169 EXPORT_SYMBOL_GPL(tee_client_close_session);
1170
tee_client_invoke_func(struct tee_context * ctx,struct tee_ioctl_invoke_arg * arg,struct tee_param * param)1171 int tee_client_invoke_func(struct tee_context *ctx,
1172 struct tee_ioctl_invoke_arg *arg,
1173 struct tee_param *param)
1174 {
1175 if (!ctx->teedev->desc->ops->invoke_func)
1176 return -EINVAL;
1177 return ctx->teedev->desc->ops->invoke_func(ctx, arg, param);
1178 }
1179 EXPORT_SYMBOL_GPL(tee_client_invoke_func);
1180
tee_client_cancel_req(struct tee_context * ctx,struct tee_ioctl_cancel_arg * arg)1181 int tee_client_cancel_req(struct tee_context *ctx,
1182 struct tee_ioctl_cancel_arg *arg)
1183 {
1184 if (!ctx->teedev->desc->ops->cancel_req)
1185 return -EINVAL;
1186 return ctx->teedev->desc->ops->cancel_req(ctx, arg->cancel_id,
1187 arg->session);
1188 }
1189
tee_client_device_match(struct device * dev,struct device_driver * drv)1190 static int tee_client_device_match(struct device *dev,
1191 struct device_driver *drv)
1192 {
1193 const struct tee_client_device_id *id_table;
1194 struct tee_client_device *tee_device;
1195
1196 id_table = to_tee_client_driver(drv)->id_table;
1197 tee_device = to_tee_client_device(dev);
1198
1199 while (!uuid_is_null(&id_table->uuid)) {
1200 if (uuid_equal(&tee_device->id.uuid, &id_table->uuid))
1201 return 1;
1202 id_table++;
1203 }
1204
1205 return 0;
1206 }
1207
tee_client_device_uevent(struct device * dev,struct kobj_uevent_env * env)1208 static int tee_client_device_uevent(struct device *dev,
1209 struct kobj_uevent_env *env)
1210 {
1211 uuid_t *dev_id = &to_tee_client_device(dev)->id.uuid;
1212
1213 return add_uevent_var(env, "MODALIAS=tee:%pUb", dev_id);
1214 }
1215
1216 struct bus_type tee_bus_type = {
1217 .name = "tee",
1218 .match = tee_client_device_match,
1219 .uevent = tee_client_device_uevent,
1220 };
1221 EXPORT_SYMBOL_GPL(tee_bus_type);
1222
tee_init(void)1223 static int __init tee_init(void)
1224 {
1225 int rc;
1226
1227 tee_class = class_create(THIS_MODULE, "tee");
1228 if (IS_ERR(tee_class)) {
1229 pr_err("couldn't create class\n");
1230 return PTR_ERR(tee_class);
1231 }
1232
1233 rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
1234 if (rc) {
1235 pr_err("failed to allocate char dev region\n");
1236 goto out_unreg_class;
1237 }
1238
1239 rc = bus_register(&tee_bus_type);
1240 if (rc) {
1241 pr_err("failed to register tee bus\n");
1242 goto out_unreg_chrdev;
1243 }
1244
1245 return 0;
1246
1247 out_unreg_chrdev:
1248 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1249 out_unreg_class:
1250 class_destroy(tee_class);
1251 tee_class = NULL;
1252
1253 return rc;
1254 }
1255
tee_exit(void)1256 static void __exit tee_exit(void)
1257 {
1258 bus_unregister(&tee_bus_type);
1259 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1260 class_destroy(tee_class);
1261 tee_class = NULL;
1262 }
1263
1264 subsys_initcall(tee_init);
1265 module_exit(tee_exit);
1266
1267 MODULE_AUTHOR("Linaro");
1268 MODULE_DESCRIPTION("TEE Driver");
1269 MODULE_VERSION("1.0");
1270 MODULE_LICENSE("GPL v2");
1271