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 TEE_Result res = TEE_SUCCESS; 66 struct tee_pobj *o = NULL; 67 68 *obj = NULL; 69 70 mutex_lock(&pobjs_mutex); 71 /* Check if file is open */ 72 TAILQ_FOREACH(o, &tee_pobjs, link) { 73 if ((obj_id_len == o->obj_id_len) && 74 (memcmp(obj_id, o->obj_id, obj_id_len) == 0) && 75 (memcmp(uuid, &o->uuid, sizeof(TEE_UUID)) == 0) && 76 (fops == o->fops)) { 77 *obj = o; 78 } 79 } 80 81 if (*obj) { 82 if (usage == TEE_POBJ_USAGE_ENUM) { 83 (*obj)->refcnt++; 84 goto out; 85 } 86 if ((*obj)->creating) { 87 res = TEE_ERROR_ACCESS_CONFLICT; 88 goto out; 89 } 90 res = tee_pobj_check_access((*obj)->flags, flags); 91 if (res == TEE_SUCCESS) 92 (*obj)->refcnt++; 93 goto out; 94 } 95 96 /* new file */ 97 o = calloc(1, sizeof(struct tee_pobj)); 98 if (!o) { 99 res = TEE_ERROR_OUT_OF_MEMORY; 100 goto out; 101 } 102 103 o->refcnt = 1; 104 memcpy(&o->uuid, uuid, sizeof(TEE_UUID)); 105 o->flags = flags; 106 o->fops = fops; 107 108 if (usage == TEE_POBJ_USAGE_CREATE) { 109 o->temporary = true; 110 o->creating = true; 111 } 112 113 o->obj_id = malloc(obj_id_len); 114 if (o->obj_id == NULL) { 115 free(o); 116 res = TEE_ERROR_OUT_OF_MEMORY; 117 goto out; 118 } 119 memcpy(o->obj_id, obj_id, obj_id_len); 120 o->obj_id_len = obj_id_len; 121 122 TAILQ_INSERT_TAIL(&tee_pobjs, o, link); 123 *obj = o; 124 125 res = TEE_SUCCESS; 126 out: 127 if (res != TEE_SUCCESS) 128 *obj = NULL; 129 mutex_unlock(&pobjs_mutex); 130 return res; 131 } 132 133 void tee_pobj_create_final(struct tee_pobj *po) 134 { 135 mutex_lock(&pobjs_mutex); 136 po->temporary = false; 137 po->creating = false; 138 mutex_unlock(&pobjs_mutex); 139 } 140 141 TEE_Result tee_pobj_release(struct tee_pobj *obj) 142 { 143 if (obj == NULL) 144 return TEE_ERROR_BAD_PARAMETERS; 145 146 mutex_lock(&pobjs_mutex); 147 obj->refcnt--; 148 if (obj->refcnt == 0) { 149 TAILQ_REMOVE(&tee_pobjs, obj, link); 150 free(obj->obj_id); 151 free(obj); 152 } 153 mutex_unlock(&pobjs_mutex); 154 155 return TEE_SUCCESS; 156 } 157 158 TEE_Result tee_pobj_rename(struct tee_pobj *obj, void *obj_id, 159 uint32_t obj_id_len) 160 { 161 TEE_Result res = TEE_SUCCESS; 162 void *new_obj_id = NULL; 163 164 if (obj == NULL || obj_id == NULL) 165 return TEE_ERROR_BAD_PARAMETERS; 166 167 mutex_lock(&pobjs_mutex); 168 if (obj->refcnt != 1) { 169 res = TEE_ERROR_BAD_STATE; 170 goto exit; 171 } 172 173 new_obj_id = malloc(obj_id_len); 174 if (new_obj_id == NULL) { 175 res = TEE_ERROR_OUT_OF_MEMORY; 176 goto exit; 177 } 178 memcpy(new_obj_id, obj_id, obj_id_len); 179 180 /* update internal data */ 181 free(obj->obj_id); 182 obj->obj_id = new_obj_id; 183 obj->obj_id_len = obj_id_len; 184 new_obj_id = NULL; 185 186 exit: 187 mutex_unlock(&pobjs_mutex); 188 free(new_obj_id); 189 return res; 190 } 191