xref: /optee_os/core/tee/tee_obj.c (revision 5ca2c36555d169a2be60964e2cbe39340c5245a4)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #include <mm/vm.h>
7 #include <stdlib.h>
8 #include <tee_api_defines.h>
9 #include <tee/tee_fs.h>
10 #include <tee/tee_obj.h>
11 #include <tee/tee_pobj.h>
12 #include <tee/tee_svc_cryp.h>
13 #include <trace.h>
14 
tee_obj_add(struct user_ta_ctx * utc,struct tee_obj * o)15 void tee_obj_add(struct user_ta_ctx *utc, struct tee_obj *o)
16 {
17 	TAILQ_INSERT_TAIL(&utc->objects, o, link);
18 }
19 
tee_obj_get(struct user_ta_ctx * utc,vaddr_t obj_id,struct tee_obj ** obj)20 TEE_Result tee_obj_get(struct user_ta_ctx *utc, vaddr_t obj_id,
21 		       struct tee_obj **obj)
22 {
23 	struct tee_obj *o;
24 
25 	TAILQ_FOREACH(o, &utc->objects, link) {
26 		if (obj_id == (vaddr_t)o) {
27 			*obj = o;
28 			return TEE_SUCCESS;
29 		}
30 	}
31 	return TEE_ERROR_BAD_STATE;
32 }
33 
tee_obj_close(struct user_ta_ctx * utc,struct tee_obj * o)34 void tee_obj_close(struct user_ta_ctx *utc, struct tee_obj *o)
35 {
36 	TAILQ_REMOVE(&utc->objects, o, link);
37 
38 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
39 		o->pobj->fops->close(&o->fh);
40 		tee_pobj_release(o->pobj);
41 	}
42 
43 	tee_obj_free(o);
44 }
45 
tee_obj_close_all(struct user_ta_ctx * utc)46 void tee_obj_close_all(struct user_ta_ctx *utc)
47 {
48 	struct tee_obj_head *objects = &utc->objects;
49 
50 	while (!TAILQ_EMPTY(objects))
51 		tee_obj_close(utc, TAILQ_FIRST(objects));
52 }
53 
tee_obj_verify(struct tee_ta_session * sess,struct tee_obj * o)54 TEE_Result tee_obj_verify(struct tee_ta_session *sess, struct tee_obj *o)
55 {
56 	TEE_Result res;
57 	const struct tee_file_operations *fops = o->pobj->fops;
58 	struct tee_file_handle *fh = NULL;
59 
60 	if (!fops)
61 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
62 
63 	res = fops->open(o->pobj, NULL, &fh);
64 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
65 		EMSG("Object corrupt");
66 		fops->remove(o->pobj);
67 		tee_obj_close(to_user_ta_ctx(sess->ts_sess.ctx), o);
68 	}
69 
70 	fops->close(&fh);
71 	return res;
72 }
73 
tee_obj_alloc(void)74 struct tee_obj *tee_obj_alloc(void)
75 {
76 	return calloc(1, sizeof(struct tee_obj));
77 }
78 
tee_obj_free(struct tee_obj * o)79 void tee_obj_free(struct tee_obj *o)
80 {
81 	if (o) {
82 		tee_obj_attr_free(o);
83 		free(o->attr);
84 		free(o);
85 	}
86 }
87