xref: /optee_os/core/tee/tee_obj.c (revision 8dceff9b18e7c2e0cb879ea458e85a1806dff447)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <tee/tee_obj.h>
29 
30 #include <stdlib.h>
31 #include <tee_api_defines.h>
32 #include <mm/tee_mmu.h>
33 #include <tee/tee_fs.h>
34 #include <tee/tee_fs_defs.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 		if (o->fd >= 0)
65 			o->pobj->fops->close(o->fd);
66 		tee_pobj_release(o->pobj);
67 	}
68 
69 	tee_obj_free(o);
70 }
71 
72 void tee_obj_close_all(struct user_ta_ctx *utc)
73 {
74 	struct tee_obj_head *objects = &utc->objects;
75 
76 	while (!TAILQ_EMPTY(objects))
77 		tee_obj_close(utc, TAILQ_FIRST(objects));
78 }
79 
80 TEE_Result tee_obj_verify(struct tee_ta_session *sess, struct tee_obj *o)
81 {
82 	TEE_Result res;
83 	char *file = NULL;
84 	char *dir = NULL;
85 	int fd = -1;
86 	int err = -1;
87 	const struct tee_file_operations *fops = o->pobj->fops;
88 
89 	if (!fops)
90 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
91 
92 	file = tee_svc_storage_create_filename(sess,
93 					       o->pobj->obj_id,
94 					       o->pobj->obj_id_len,
95 					       false);
96 	if (file == NULL) {
97 		res = TEE_ERROR_OUT_OF_MEMORY;
98 		goto exit;
99 	}
100 
101 	err = fops->access(file, TEE_FS_F_OK);
102 	if (err) {
103 		/* file not found */
104 		res = TEE_ERROR_ITEM_NOT_FOUND;
105 		goto err;
106 	}
107 
108 	fd = fops->open(&res, file, TEE_FS_O_RDONLY);
109 	if (fd < 0) {
110 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
111 			EMSG("Object corrupt\n");
112 			tee_obj_close(to_user_ta_ctx(sess->ctx), o);
113 			fops->unlink(file);
114 			dir = tee_svc_storage_create_dirname(sess);
115 			if (dir != NULL) {
116 				fops->rmdir(dir);
117 				free(dir);
118 			}
119 		}
120 		goto err;
121 	}
122 
123 	res = TEE_SUCCESS;
124 
125 err:
126 	free(file);
127 	if (fd >= 0)
128 		fops->close(fd);
129 exit:
130 	return res;
131 }
132 
133 struct tee_obj *tee_obj_alloc(void)
134 {
135 	struct tee_obj *o = calloc(1, sizeof(struct tee_obj));
136 
137 	if (o)
138 		o->fd = -1;
139 	return o;
140 }
141 
142 void tee_obj_free(struct tee_obj *o)
143 {
144 	if (o) {
145 		tee_obj_attr_free(o);
146 		free(o->attr);
147 		free(o);
148 	}
149 }
150