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