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