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 (temporary != (*obj)->temporary) { 84 res = TEE_ERROR_ACCESS_CONFLICT; 85 goto out; 86 } 87 res = tee_pobj_check_access((*obj)->flags, flags); 88 if (res == TEE_SUCCESS) 89 (*obj)->refcnt++; 90 goto out; 91 } 92 93 /* new file */ 94 o = calloc(1, sizeof(struct tee_pobj)); 95 if (!o) { 96 res = TEE_ERROR_OUT_OF_MEMORY; 97 goto out; 98 } 99 100 o->refcnt = 1; 101 memcpy(&o->uuid, uuid, sizeof(TEE_UUID)); 102 o->flags = flags; 103 o->fops = fops; 104 o->temporary = temporary; 105 106 o->obj_id = malloc(obj_id_len); 107 if (o->obj_id == NULL) { 108 free(o); 109 res = TEE_ERROR_OUT_OF_MEMORY; 110 goto out; 111 } 112 memcpy(o->obj_id, obj_id, obj_id_len); 113 o->obj_id_len = obj_id_len; 114 115 TAILQ_INSERT_TAIL(&tee_pobjs, o, link); 116 *obj = o; 117 118 res = TEE_SUCCESS; 119 out: 120 if (res != TEE_SUCCESS) 121 *obj = NULL; 122 mutex_unlock(&pobjs_mutex); 123 return res; 124 } 125 126 TEE_Result tee_pobj_release(struct tee_pobj *obj) 127 { 128 if (obj == NULL) 129 return TEE_ERROR_BAD_PARAMETERS; 130 131 mutex_lock(&pobjs_mutex); 132 obj->refcnt--; 133 if (obj->refcnt == 0) { 134 TAILQ_REMOVE(&tee_pobjs, obj, link); 135 free(obj->obj_id); 136 free(obj); 137 } 138 mutex_unlock(&pobjs_mutex); 139 140 return TEE_SUCCESS; 141 } 142 143 TEE_Result tee_pobj_rename(struct tee_pobj *obj, void *obj_id, 144 uint32_t obj_id_len) 145 { 146 TEE_Result res = TEE_SUCCESS; 147 void *new_obj_id = NULL; 148 149 if (obj == NULL || obj_id == NULL) 150 return TEE_ERROR_BAD_PARAMETERS; 151 152 mutex_lock(&pobjs_mutex); 153 if (obj->refcnt != 1) { 154 res = TEE_ERROR_BAD_STATE; 155 goto exit; 156 } 157 158 new_obj_id = malloc(obj_id_len); 159 if (new_obj_id == NULL) { 160 res = TEE_ERROR_OUT_OF_MEMORY; 161 goto exit; 162 } 163 memcpy(new_obj_id, obj_id, obj_id_len); 164 165 /* update internal data */ 166 free(obj->obj_id); 167 obj->obj_id = new_obj_id; 168 obj->obj_id_len = obj_id_len; 169 new_obj_id = NULL; 170 171 exit: 172 mutex_unlock(&pobjs_mutex); 173 free(new_obj_id); 174 return res; 175 } 176