xref: /optee_os/core/tee/tee_pobj.c (revision 03c42787bbb0d574300fdc79ef82316837aef3a3)
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_pobj.h>
29 #include <kernel/tee_core_trace.h>
30 
31 #include <string.h>
32 #include <stdlib.h>
33 
34 static TAILQ_HEAD(tee_pobjs, tee_pobj) tee_pobjs =
35 TAILQ_HEAD_INITIALIZER(tee_pobjs);
36 
37 static TEE_Result tee_pobj_check_access(uint32_t oflags, uint32_t nflags)
38 {
39 	/* meta is exclusive */
40 	if (oflags | TEE_DATA_FLAG_ACCESS_WRITE_META ||
41 	    nflags | TEE_DATA_FLAG_ACCESS_WRITE_META)
42 		return TEE_ERROR_ACCESS_CONFLICT;
43 
44 	if (oflags | TEE_DATA_FLAG_ACCESS_READ &&
45 	    !((nflags | TEE_DATA_FLAG_SHARE_READ) &&
46 	      oflags | TEE_DATA_FLAG_SHARE_READ))
47 		return TEE_ERROR_ACCESS_CONFLICT;
48 
49 	if (oflags | TEE_DATA_FLAG_ACCESS_WRITE &&
50 	    !((nflags | TEE_DATA_FLAG_SHARE_WRITE) &&
51 	      oflags | TEE_DATA_FLAG_SHARE_WRITE))
52 		return TEE_ERROR_ACCESS_CONFLICT;
53 
54 	return TEE_SUCCESS;
55 }
56 
57 TEE_Result tee_pobj_get(TEE_UUID *uuid, void *obj_id, uint32_t obj_id_len,
58 			uint32_t flags, struct tee_pobj **obj)
59 {
60 	struct tee_pobj *o;
61 	TEE_Result res;
62 
63 	*obj = NULL;
64 
65 	/* Check if file is open */
66 	TAILQ_FOREACH(o, &tee_pobjs, link) {
67 		if ((obj_id_len == o->obj_id_len) &&
68 		    (memcmp(obj_id, o->obj_id, obj_id_len) == 0) &&
69 		    (memcmp(uuid, &o->uuid, sizeof(TEE_UUID)) == 0)) {
70 			*obj = o;
71 		}
72 	}
73 
74 	if (*obj) {
75 		res = tee_pobj_check_access((*obj)->flags, flags);
76 		if (res != TEE_SUCCESS) {
77 			*obj = NULL;
78 			return res;
79 		}
80 
81 		(*obj)->refcnt++;
82 		return TEE_SUCCESS;
83 	}
84 
85 	/* new file */
86 	o = calloc(sizeof(struct tee_pobj), 1);
87 
88 	if (o == NULL)
89 		return TEE_ERROR_OUT_OF_MEMORY;
90 
91 	o->refcnt = 1;
92 	memcpy(&o->uuid, uuid, sizeof(TEE_UUID));
93 	o->flags = flags;
94 
95 	o->obj_id = malloc(obj_id_len);
96 	if (o->obj_id == NULL) {
97 		free(o);
98 		return TEE_ERROR_OUT_OF_MEMORY;
99 	}
100 	memcpy(o->obj_id, obj_id, obj_id_len);
101 	o->obj_id_len = obj_id_len;
102 
103 	TAILQ_INSERT_TAIL(&tee_pobjs, o, link);
104 	*obj = o;
105 
106 	return TEE_SUCCESS;
107 }
108 
109 TEE_Result tee_pobj_release(struct tee_pobj *obj)
110 {
111 	if (obj == NULL)
112 		return TEE_ERROR_BAD_PARAMETERS;
113 
114 	obj->refcnt--;
115 	if (obj->refcnt == 0) {
116 		TAILQ_REMOVE(&tee_pobjs, obj, link);
117 		free(obj->obj_id);
118 		free(obj);
119 	}
120 
121 	return TEE_SUCCESS;
122 }
123 
124 TEE_Result tee_pobj_rename(struct tee_pobj *obj, void *obj_id,
125 			   uint32_t obj_id_len)
126 {
127 	TEE_Result res = TEE_SUCCESS;
128 	void *new_obj_id = NULL;
129 
130 	if (obj == NULL || obj_id == NULL)
131 		return TEE_ERROR_BAD_PARAMETERS;
132 
133 	if (obj->refcnt != 1)
134 		return TEE_ERROR_BAD_STATE;
135 
136 	new_obj_id = malloc(obj_id_len);
137 	if (new_obj_id == NULL) {
138 		res = TEE_ERROR_OUT_OF_MEMORY;
139 		goto exit;
140 	}
141 	memcpy(new_obj_id, obj_id, obj_id_len);
142 
143 	/* update internal data */
144 	free(obj->obj_id);
145 	obj->obj_id = new_obj_id;
146 	obj->obj_id_len = obj_id_len;
147 	new_obj_id = NULL;
148 
149 exit:
150 	free(new_obj_id);
151 	return res;
152 }
153 
154 void tee_pobj_init(void)
155 {
156 	TAILQ_INIT(&tee_pobjs);
157 }
158