xref: /optee_os/core/tee/tee_obj.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <tee/tee_obj.h>
30 
31 #include <stdlib.h>
32 #include <tee_api_defines.h>
33 #include <mm/tee_mmu.h>
34 #include <tee/tee_fs.h>
35 #include <tee/tee_pobj.h>
36 #include <trace.h>
37 #include <tee/tee_svc_storage.h>
38 #include <tee/tee_svc_cryp.h>
39 
40 void tee_obj_add(struct user_ta_ctx *utc, struct tee_obj *o)
41 {
42 	TAILQ_INSERT_TAIL(&utc->objects, o, link);
43 }
44 
45 TEE_Result tee_obj_get(struct user_ta_ctx *utc, uint32_t obj_id,
46 		       struct tee_obj **obj)
47 {
48 	struct tee_obj *o;
49 
50 	TAILQ_FOREACH(o, &utc->objects, link) {
51 		if (obj_id == (vaddr_t)o) {
52 			*obj = o;
53 			return TEE_SUCCESS;
54 		}
55 	}
56 	return TEE_ERROR_BAD_PARAMETERS;
57 }
58 
59 void tee_obj_close(struct user_ta_ctx *utc, struct tee_obj *o)
60 {
61 	TAILQ_REMOVE(&utc->objects, o, link);
62 
63 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
64 		o->pobj->fops->close(&o->fh);
65 		tee_pobj_release(o->pobj);
66 	}
67 
68 	tee_obj_free(o);
69 }
70 
71 void tee_obj_close_all(struct user_ta_ctx *utc)
72 {
73 	struct tee_obj_head *objects = &utc->objects;
74 
75 	while (!TAILQ_EMPTY(objects))
76 		tee_obj_close(utc, TAILQ_FIRST(objects));
77 }
78 
79 TEE_Result tee_obj_verify(struct tee_ta_session *sess, struct tee_obj *o)
80 {
81 	TEE_Result res;
82 	const struct tee_file_operations *fops = o->pobj->fops;
83 	struct tee_file_handle *fh = NULL;
84 
85 	if (!fops)
86 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
87 
88 	res = fops->open(o->pobj, NULL, &fh);
89 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
90 		EMSG("Object corrupt\n");
91 		fops->remove(o->pobj);
92 		tee_obj_close(to_user_ta_ctx(sess->ctx), o);
93 	}
94 
95 	fops->close(&fh);
96 	return res;
97 }
98 
99 struct tee_obj *tee_obj_alloc(void)
100 {
101 	return calloc(1, sizeof(struct tee_obj));
102 }
103 
104 void tee_obj_free(struct tee_obj *o)
105 {
106 	if (o) {
107 		tee_obj_attr_free(o);
108 		free(o->attr);
109 		free(o);
110 	}
111 }
112