xref: /optee_os/core/tee/tee_obj.c (revision 51ac0e23b5c2b3c84469a0de79c9f027a46d5747)
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) && o->fd >= 0) {
64 		o->pobj->fops->close(o->fd);
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 	char *file = NULL;
83 	char *dir = NULL;
84 	int fd = -1;
85 	int err = -1;
86 	const struct tee_file_operations *fops = o->pobj->fops;
87 
88 	if (!fops)
89 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
90 
91 	file = tee_svc_storage_create_filename(sess,
92 					       o->pobj->obj_id,
93 					       o->pobj->obj_id_len,
94 					       false);
95 	if (file == NULL) {
96 		res = TEE_ERROR_OUT_OF_MEMORY;
97 		goto exit;
98 	}
99 
100 	err = fops->access(file, TEE_FS_F_OK);
101 	if (err) {
102 		/* file not found */
103 		res = TEE_ERROR_STORAGE_NOT_AVAILABLE;
104 		goto err;
105 	}
106 
107 	fd = fops->open(&res, file, TEE_FS_O_RDONLY);
108 	if (fd < 0) {
109 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
110 			EMSG("Object corrupt\n");
111 			tee_obj_close(to_user_ta_ctx(sess->ctx), o);
112 			fops->unlink(file);
113 			dir = tee_svc_storage_create_dirname(sess);
114 			if (dir != NULL) {
115 				fops->rmdir(dir);
116 				free(dir);
117 			}
118 		}
119 		goto err;
120 	}
121 
122 	res = TEE_SUCCESS;
123 
124 err:
125 	free(file);
126 	if (fd >= 0)
127 		fops->close(fd);
128 exit:
129 	return res;
130 }
131 
132 struct tee_obj *tee_obj_alloc(void)
133 {
134 	return calloc(1, sizeof(struct tee_obj));
135 }
136 
137 void tee_obj_free(struct tee_obj *o)
138 {
139 	if (o) {
140 		tee_obj_attr_free(o);
141 		free(o->attr);
142 		free(o);
143 	}
144 }
145