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