1b0104773SPascal Brand /* 2b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 3b0104773SPascal Brand * All rights reserved. 4b0104773SPascal Brand * 5b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 6b0104773SPascal Brand * modification, are permitted provided that the following conditions are met: 7b0104773SPascal Brand * 8b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright notice, 9b0104773SPascal Brand * this list of conditions and the following disclaimer. 10b0104773SPascal Brand * 11b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright notice, 12b0104773SPascal Brand * this list of conditions and the following disclaimer in the documentation 13b0104773SPascal Brand * and/or other materials provided with the distribution. 14b0104773SPascal Brand * 15b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16b0104773SPascal Brand * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19b0104773SPascal Brand * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20b0104773SPascal Brand * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21b0104773SPascal Brand * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22b0104773SPascal Brand * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23b0104773SPascal Brand * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24b0104773SPascal Brand * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25b0104773SPascal Brand * POSSIBILITY OF SUCH DAMAGE. 26b0104773SPascal Brand */ 27b0104773SPascal Brand 28b0104773SPascal Brand #include <tee/tee_obj.h> 29b0104773SPascal Brand 30b0104773SPascal Brand #include <stdlib.h> 31b0104773SPascal Brand #include <tee_api_defines.h> 32b0104773SPascal Brand #include <mm/tee_mmu.h> 33b0104773SPascal Brand #include <tee/tee_fs.h> 34a2e9a830SCedric Chaumont #include <tee/tee_fs_defs.h> 35b0104773SPascal Brand #include <tee/tee_pobj.h> 364de4bebcSJens Wiklander #include <trace.h> 37a2e9a830SCedric Chaumont #include <tee/tee_svc_storage.h> 38b0104773SPascal Brand 398684fde8SJens Wiklander void tee_obj_add(struct user_ta_ctx *utc, struct tee_obj *o) 40b0104773SPascal Brand { 418684fde8SJens Wiklander TAILQ_INSERT_TAIL(&utc->objects, o, link); 42b0104773SPascal Brand } 43b0104773SPascal Brand 448684fde8SJens Wiklander TEE_Result tee_obj_get(struct user_ta_ctx *utc, uint32_t obj_id, 45b0104773SPascal Brand struct tee_obj **obj) 46b0104773SPascal Brand { 47b0104773SPascal Brand struct tee_obj *o; 48b0104773SPascal Brand 498684fde8SJens Wiklander TAILQ_FOREACH(o, &utc->objects, link) { 5061ea19fdSJens Wiklander if (obj_id == (vaddr_t)o) { 51b0104773SPascal Brand *obj = o; 52b0104773SPascal Brand return TEE_SUCCESS; 53b0104773SPascal Brand } 54b0104773SPascal Brand } 55b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 56b0104773SPascal Brand } 57b0104773SPascal Brand 588684fde8SJens Wiklander void tee_obj_close(struct user_ta_ctx *utc, struct tee_obj *o) 59b0104773SPascal Brand { 608684fde8SJens Wiklander TAILQ_REMOVE(&utc->objects, o, link); 61b0104773SPascal Brand 62c0346845SJens Wiklander if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) && o->fd >= 0) { 63*b44708c1SJerome Forissier o->pobj->fops->close(o->fd); 64b0104773SPascal Brand tee_pobj_release(o->pobj); 65b0104773SPascal Brand } 66b0104773SPascal Brand 6751835057SJerome Forissier if (o->cleanup) 6851835057SJerome Forissier o->cleanup(o->data, true); 69b0104773SPascal Brand free(o->data); 70b0104773SPascal Brand free(o); 71b0104773SPascal Brand } 72b0104773SPascal Brand 738684fde8SJens Wiklander void tee_obj_close_all(struct user_ta_ctx *utc) 74b0104773SPascal Brand { 758684fde8SJens Wiklander struct tee_obj_head *objects = &utc->objects; 76b0104773SPascal Brand 77b0104773SPascal Brand while (!TAILQ_EMPTY(objects)) 788684fde8SJens Wiklander tee_obj_close(utc, TAILQ_FIRST(objects)); 79b0104773SPascal Brand } 80a2e9a830SCedric Chaumont 81a2e9a830SCedric Chaumont TEE_Result tee_obj_verify(struct tee_ta_session *sess, struct tee_obj *o) 82a2e9a830SCedric Chaumont { 83a2e9a830SCedric Chaumont TEE_Result res; 84a2e9a830SCedric Chaumont char *file = NULL; 85a2e9a830SCedric Chaumont char *dir = NULL; 86a2e9a830SCedric Chaumont int fd = -1; 87a2e9a830SCedric Chaumont int err = -1; 88*b44708c1SJerome Forissier const struct tee_file_operations *fops = o->pobj->fops; 89*b44708c1SJerome Forissier 90*b44708c1SJerome Forissier if (!fops) 91*b44708c1SJerome Forissier return TEE_ERROR_STORAGE_NOT_AVAILABLE; 92a2e9a830SCedric Chaumont 93a2e9a830SCedric Chaumont file = tee_svc_storage_create_filename(sess, 94a2e9a830SCedric Chaumont o->pobj->obj_id, 95a2e9a830SCedric Chaumont o->pobj->obj_id_len, 96a2e9a830SCedric Chaumont false); 97a2e9a830SCedric Chaumont if (file == NULL) { 98a2e9a830SCedric Chaumont res = TEE_ERROR_OUT_OF_MEMORY; 99a2e9a830SCedric Chaumont goto exit; 100a2e9a830SCedric Chaumont } 101a2e9a830SCedric Chaumont 102*b44708c1SJerome Forissier err = fops->access(file, TEE_FS_F_OK); 103a2e9a830SCedric Chaumont if (err) { 104a2e9a830SCedric Chaumont /* file not found */ 105a2e9a830SCedric Chaumont res = TEE_ERROR_STORAGE_NOT_AVAILABLE; 106a2e9a830SCedric Chaumont goto err; 107a2e9a830SCedric Chaumont } 108a2e9a830SCedric Chaumont 109*b44708c1SJerome Forissier fd = fops->open(&res, file, TEE_FS_O_RDONLY); 110a2e9a830SCedric Chaumont if (fd < 0) { 111a2e9a830SCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) { 112a2e9a830SCedric Chaumont EMSG("Object corrupt\n"); 1138684fde8SJens Wiklander tee_obj_close(to_user_ta_ctx(sess->ctx), o); 114*b44708c1SJerome Forissier fops->unlink(file); 115a2e9a830SCedric Chaumont dir = tee_svc_storage_create_dirname(sess); 116a2e9a830SCedric Chaumont if (dir != NULL) { 117*b44708c1SJerome Forissier fops->rmdir(dir); 118a2e9a830SCedric Chaumont free(dir); 119a2e9a830SCedric Chaumont } 120a2e9a830SCedric Chaumont } 121a2e9a830SCedric Chaumont goto err; 122a2e9a830SCedric Chaumont } 123a2e9a830SCedric Chaumont 124a2e9a830SCedric Chaumont res = TEE_SUCCESS; 125a2e9a830SCedric Chaumont 126a2e9a830SCedric Chaumont err: 127a2e9a830SCedric Chaumont free(file); 128a2e9a830SCedric Chaumont if (fd >= 0) 129*b44708c1SJerome Forissier fops->close(fd); 130a2e9a830SCedric Chaumont exit: 131a2e9a830SCedric Chaumont return res; 132a2e9a830SCedric Chaumont } 133