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_obj.h> 29 30 #include <stdlib.h> 31 #include <tee_api_defines.h> 32 #include <mm/tee_mmu.h> 33 #include <tee/tee_fs.h> 34 #include <tee/tee_pobj.h> 35 #include <trace.h> 36 #include <tee/tee_svc_storage.h> 37 #include <tee/tee_svc_cryp.h> 38 39 void tee_obj_add(struct user_ta_ctx *utc, struct tee_obj *o) 40 { 41 TAILQ_INSERT_TAIL(&utc->objects, o, link); 42 } 43 44 TEE_Result tee_obj_get(struct user_ta_ctx *utc, uint32_t obj_id, 45 struct tee_obj **obj) 46 { 47 struct tee_obj *o; 48 49 TAILQ_FOREACH(o, &utc->objects, link) { 50 if (obj_id == (vaddr_t)o) { 51 *obj = o; 52 return TEE_SUCCESS; 53 } 54 } 55 return TEE_ERROR_BAD_PARAMETERS; 56 } 57 58 void tee_obj_close(struct user_ta_ctx *utc, struct tee_obj *o) 59 { 60 TAILQ_REMOVE(&utc->objects, o, link); 61 62 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 63 o->pobj->fops->close(&o->fh); 64 tee_pobj_release(o->pobj); 65 } 66 67 tee_obj_free(o); 68 } 69 70 void tee_obj_close_all(struct user_ta_ctx *utc) 71 { 72 struct tee_obj_head *objects = &utc->objects; 73 74 while (!TAILQ_EMPTY(objects)) 75 tee_obj_close(utc, TAILQ_FIRST(objects)); 76 } 77 78 TEE_Result tee_obj_verify(struct tee_ta_session *sess, struct tee_obj *o) 79 { 80 TEE_Result res; 81 const struct tee_file_operations *fops = o->pobj->fops; 82 struct tee_file_handle *fh = NULL; 83 84 if (!fops) 85 return TEE_ERROR_STORAGE_NOT_AVAILABLE; 86 87 res = fops->open(o->pobj, NULL, &fh); 88 if (res == TEE_ERROR_CORRUPT_OBJECT) { 89 EMSG("Object corrupt\n"); 90 fops->remove(o->pobj); 91 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 92 } 93 94 fops->close(&fh); 95 return res; 96 } 97 98 struct tee_obj *tee_obj_alloc(void) 99 { 100 return calloc(1, sizeof(struct tee_obj)); 101 } 102 103 void tee_obj_free(struct tee_obj *o) 104 { 105 if (o) { 106 tee_obj_attr_free(o); 107 free(o->attr); 108 free(o); 109 } 110 } 111