xref: /optee_os/core/tee/tee_pobj.c (revision 928efd065222491b7c6255e3ec98aab18dc9925a)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #include <kernel/mutex.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <tee/tee_pobj.h>
10 #include <trace.h>
11 
12 static TAILQ_HEAD(tee_pobjs, tee_pobj) tee_pobjs =
13 		TAILQ_HEAD_INITIALIZER(tee_pobjs);
14 static struct mutex pobjs_mutex = MUTEX_INITIALIZER;
15 
16 static TEE_Result tee_pobj_check_access(uint32_t oflags, uint32_t nflags)
17 {
18 	/* meta is exclusive */
19 	if ((oflags & TEE_DATA_FLAG_ACCESS_WRITE_META) ||
20 	    (nflags & TEE_DATA_FLAG_ACCESS_WRITE_META))
21 		return TEE_ERROR_ACCESS_CONFLICT;
22 
23 	/*
24 	 * Excerpt of TEE Internal Core API Specification v1.1:
25 	 * If more than one handle is opened on the same  object, and if any
26 	 * of these object handles was opened with the flag
27 	 * TEE_DATA_FLAG_ACCESS_READ, then all the object handles MUST have been
28 	 * opened with the flag TEE_DATA_FLAG_SHARE_READ
29 	 */
30 	if (((oflags & TEE_DATA_FLAG_ACCESS_READ) ||
31 	     (nflags & TEE_DATA_FLAG_ACCESS_READ)) &&
32 	    !((nflags & TEE_DATA_FLAG_SHARE_READ) &&
33 	      (oflags & TEE_DATA_FLAG_SHARE_READ)))
34 		return TEE_ERROR_ACCESS_CONFLICT;
35 
36 	/*
37 	 * Excerpt of TEE Internal Core API Specification v1.1:
38 	 * An object can be opened with only share flags, which locks the access
39 	 * to an object against a given mode.
40 	 * An object can be opened with no flag set, which completely locks all
41 	 * subsequent attempts to access the object
42 	 */
43 	if ((nflags & TEE_DATA_FLAG_SHARE_READ) !=
44 	    (oflags & TEE_DATA_FLAG_SHARE_READ))
45 		return TEE_ERROR_ACCESS_CONFLICT;
46 
47 	/* Same on WRITE access */
48 	if (((oflags & TEE_DATA_FLAG_ACCESS_WRITE) ||
49 	     (nflags & 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 	if ((nflags & TEE_DATA_FLAG_SHARE_WRITE) !=
54 	    (oflags & TEE_DATA_FLAG_SHARE_WRITE))
55 		return TEE_ERROR_ACCESS_CONFLICT;
56 
57 	return TEE_SUCCESS;
58 }
59 
60 TEE_Result tee_pobj_get(TEE_UUID *uuid, void *obj_id, uint32_t obj_id_len,
61 			uint32_t flags, enum tee_pobj_usage usage,
62 			const struct tee_file_operations *fops,
63 			struct tee_pobj **obj)
64 {
65 	bool temporary = (usage == TEE_POBJ_USAGE_CREATE);
66 	TEE_Result res = TEE_SUCCESS;
67 	struct tee_pobj *o = NULL;
68 
69 	*obj = NULL;
70 
71 	mutex_lock(&pobjs_mutex);
72 	/* Check if file is open */
73 	TAILQ_FOREACH(o, &tee_pobjs, link) {
74 		if ((obj_id_len == o->obj_id_len) &&
75 		    (memcmp(obj_id, o->obj_id, obj_id_len) == 0) &&
76 		    (memcmp(uuid, &o->uuid, sizeof(TEE_UUID)) == 0) &&
77 		    (fops == o->fops)) {
78 			*obj = o;
79 		}
80 	}
81 
82 	if (*obj) {
83 		if (usage == TEE_POBJ_USAGE_ENUM) {
84 			(*obj)->refcnt++;
85 			goto out;
86 		}
87 		if (temporary != (*obj)->temporary) {
88 			res = TEE_ERROR_ACCESS_CONFLICT;
89 			goto out;
90 		}
91 		res = tee_pobj_check_access((*obj)->flags, flags);
92 		if (res == TEE_SUCCESS)
93 			(*obj)->refcnt++;
94 		goto out;
95 	}
96 
97 	/* new file */
98 	o = calloc(1, sizeof(struct tee_pobj));
99 	if (!o) {
100 		res = TEE_ERROR_OUT_OF_MEMORY;
101 		goto out;
102 	}
103 
104 	o->refcnt = 1;
105 	memcpy(&o->uuid, uuid, sizeof(TEE_UUID));
106 	o->flags = flags;
107 	o->fops = fops;
108 	o->temporary = temporary;
109 
110 	o->obj_id = malloc(obj_id_len);
111 	if (o->obj_id == NULL) {
112 		free(o);
113 		res = TEE_ERROR_OUT_OF_MEMORY;
114 		goto out;
115 	}
116 	memcpy(o->obj_id, obj_id, obj_id_len);
117 	o->obj_id_len = obj_id_len;
118 
119 	TAILQ_INSERT_TAIL(&tee_pobjs, o, link);
120 	*obj = o;
121 
122 	res = TEE_SUCCESS;
123 out:
124 	if (res != TEE_SUCCESS)
125 		*obj = NULL;
126 	mutex_unlock(&pobjs_mutex);
127 	return res;
128 }
129 
130 TEE_Result tee_pobj_release(struct tee_pobj *obj)
131 {
132 	if (obj == NULL)
133 		return TEE_ERROR_BAD_PARAMETERS;
134 
135 	mutex_lock(&pobjs_mutex);
136 	obj->refcnt--;
137 	if (obj->refcnt == 0) {
138 		TAILQ_REMOVE(&tee_pobjs, obj, link);
139 		free(obj->obj_id);
140 		free(obj);
141 	}
142 	mutex_unlock(&pobjs_mutex);
143 
144 	return TEE_SUCCESS;
145 }
146 
147 TEE_Result tee_pobj_rename(struct tee_pobj *obj, void *obj_id,
148 			   uint32_t obj_id_len)
149 {
150 	TEE_Result res = TEE_SUCCESS;
151 	void *new_obj_id = NULL;
152 
153 	if (obj == NULL || obj_id == NULL)
154 		return TEE_ERROR_BAD_PARAMETERS;
155 
156 	mutex_lock(&pobjs_mutex);
157 	if (obj->refcnt != 1) {
158 		res = TEE_ERROR_BAD_STATE;
159 		goto exit;
160 	}
161 
162 	new_obj_id = malloc(obj_id_len);
163 	if (new_obj_id == NULL) {
164 		res = TEE_ERROR_OUT_OF_MEMORY;
165 		goto exit;
166 	}
167 	memcpy(new_obj_id, obj_id, obj_id_len);
168 
169 	/* update internal data */
170 	free(obj->obj_id);
171 	obj->obj_id = new_obj_id;
172 	obj->obj_id_len = obj_id_len;
173 	new_obj_id = NULL;
174 
175 exit:
176 	mutex_unlock(&pobjs_mutex);
177 	free(new_obj_id);
178 	return res;
179 }
180