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 39 void tee_obj_add(struct tee_ta_ctx *ctx, struct tee_obj *o) 40 { 41 TAILQ_INSERT_TAIL(&ctx->objects, o, link); 42 } 43 44 TEE_Result tee_obj_get(struct tee_ta_ctx *ctx, uint32_t obj_id, 45 struct tee_obj **obj) 46 { 47 struct tee_obj *o; 48 49 TAILQ_FOREACH(o, &ctx->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 tee_ta_ctx *ctx, struct tee_obj *o) 59 { 60 TAILQ_REMOVE(&ctx->objects, o, link); 61 62 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) && o->fd >= 0) { 63 tee_file_ops.close(o->fd); 64 tee_pobj_release(o->pobj); 65 } 66 67 if (o->cleanup) 68 o->cleanup(o->data, true); 69 free(o->data); 70 free(o); 71 } 72 73 void tee_obj_close_all(struct tee_ta_ctx *ctx) 74 { 75 struct tee_obj_head *objects = &ctx->objects; 76 77 while (!TAILQ_EMPTY(objects)) 78 tee_obj_close(ctx, TAILQ_FIRST(objects)); 79 } 80 81 TEE_Result tee_obj_verify(struct tee_ta_session *sess, struct tee_obj *o) 82 { 83 TEE_Result res; 84 char *file = NULL; 85 char *dir = NULL; 86 int fd = -1; 87 int err = -1; 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 err = tee_file_ops.access(file, TEE_FS_F_OK); 99 if (err) { 100 /* file not found */ 101 res = TEE_ERROR_STORAGE_NOT_AVAILABLE; 102 goto err; 103 } 104 105 fd = tee_file_ops.open(&res, file, TEE_FS_O_RDONLY); 106 if (fd < 0) { 107 if (res == TEE_ERROR_CORRUPT_OBJECT) { 108 EMSG("Object corrupt\n"); 109 tee_obj_close(sess->ctx, o); 110 tee_file_ops.unlink(file); 111 dir = tee_svc_storage_create_dirname(sess); 112 if (dir != NULL) { 113 tee_file_ops.rmdir(dir); 114 free(dir); 115 } 116 } 117 goto err; 118 } 119 120 res = TEE_SUCCESS; 121 122 err: 123 free(file); 124 if (fd >= 0) 125 tee_file_ops.close(fd); 126 exit: 127 return res; 128 } 129