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