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