xref: /optee_os/core/tee/tee_obj.c (revision 7892cb1bcf8618990ed87458b898b37d6351428f)
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 
39 void tee_obj_add(struct user_ta_ctx *utc, struct tee_obj *o)
40 {
41 	TAILQ_INSERT_TAIL(&utc->objects, o, link);
42 }
43 
44 TEE_Result tee_obj_get(struct user_ta_ctx *utc, uint32_t obj_id,
45 		       struct tee_obj **obj)
46 {
47 	struct tee_obj *o;
48 
49 	TAILQ_FOREACH(o, &utc->objects, link) {
50 		if (obj_id == (vaddr_t)o) {
51 			*obj = o;
52 			return TEE_SUCCESS;
53 		}
54 	}
55 	return TEE_ERROR_BAD_PARAMETERS;
56 }
57 
58 void tee_obj_close(struct user_ta_ctx *utc, struct tee_obj *o)
59 {
60 	TAILQ_REMOVE(&utc->objects, o, link);
61 
62 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) && o->fd >= 0) {
63 		tee_file_ops.close(o->fd);
64 		tee_pobj_release(o->pobj);
65 	}
66 
67 	if (o->cleanup)
68 		o->cleanup(o->data, true);
69 	free(o->data);
70 	free(o);
71 }
72 
73 void tee_obj_close_all(struct user_ta_ctx *utc)
74 {
75 	struct tee_obj_head *objects = &utc->objects;
76 
77 	while (!TAILQ_EMPTY(objects))
78 		tee_obj_close(utc, TAILQ_FIRST(objects));
79 }
80 
81 TEE_Result tee_obj_verify(struct tee_ta_session *sess, struct tee_obj *o)
82 {
83 	TEE_Result res;
84 	char *file = NULL;
85 	char *dir = NULL;
86 	int fd = -1;
87 	int err = -1;
88 
89 	file = tee_svc_storage_create_filename(sess,
90 					       o->pobj->obj_id,
91 					       o->pobj->obj_id_len,
92 					       false);
93 	if (file == NULL) {
94 		res = TEE_ERROR_OUT_OF_MEMORY;
95 		goto exit;
96 	}
97 
98 	err = tee_file_ops.access(file, TEE_FS_F_OK);
99 	if (err) {
100 		/* file not found */
101 		res = TEE_ERROR_STORAGE_NOT_AVAILABLE;
102 		goto err;
103 	}
104 
105 	fd = tee_file_ops.open(&res, file, TEE_FS_O_RDONLY);
106 	if (fd < 0) {
107 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
108 			EMSG("Object corrupt\n");
109 			tee_obj_close(to_user_ta_ctx(sess->ctx), o);
110 			tee_file_ops.unlink(file);
111 			dir = tee_svc_storage_create_dirname(sess);
112 			if (dir != NULL) {
113 				tee_file_ops.rmdir(dir);
114 				free(dir);
115 			}
116 		}
117 		goto err;
118 	}
119 
120 	res = TEE_SUCCESS;
121 
122 err:
123 	free(file);
124 	if (fd >= 0)
125 		tee_file_ops.close(fd);
126 exit:
127 	return res;
128 }
129