1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 6 #include <mm/vm.h> 7 #include <stdlib.h> 8 #include <tee_api_defines.h> 9 #include <tee/tee_fs.h> 10 #include <tee/tee_obj.h> 11 #include <tee/tee_pobj.h> 12 #include <tee/tee_svc_cryp.h> 13 #include <tee/tee_svc_storage.h> 14 #include <trace.h> 15 16 void tee_obj_add(struct user_ta_ctx *utc, struct tee_obj *o) 17 { 18 TAILQ_INSERT_TAIL(&utc->objects, o, link); 19 } 20 21 TEE_Result tee_obj_get(struct user_ta_ctx *utc, vaddr_t obj_id, 22 struct tee_obj **obj) 23 { 24 struct tee_obj *o; 25 26 TAILQ_FOREACH(o, &utc->objects, link) { 27 if (obj_id == (vaddr_t)o) { 28 *obj = o; 29 return TEE_SUCCESS; 30 } 31 } 32 return TEE_ERROR_BAD_STATE; 33 } 34 35 void tee_obj_close(struct user_ta_ctx *utc, struct tee_obj *o) 36 { 37 TAILQ_REMOVE(&utc->objects, o, link); 38 39 if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 40 o->pobj->fops->close(&o->fh); 41 tee_pobj_release(o->pobj); 42 } 43 44 tee_obj_free(o); 45 } 46 47 void tee_obj_close_all(struct user_ta_ctx *utc) 48 { 49 struct tee_obj_head *objects = &utc->objects; 50 51 while (!TAILQ_EMPTY(objects)) 52 tee_obj_close(utc, TAILQ_FIRST(objects)); 53 } 54 55 TEE_Result tee_obj_verify(struct tee_ta_session *sess, struct tee_obj *o) 56 { 57 TEE_Result res; 58 const struct tee_file_operations *fops = o->pobj->fops; 59 struct tee_file_handle *fh = NULL; 60 61 if (!fops) 62 return TEE_ERROR_STORAGE_NOT_AVAILABLE; 63 64 res = fops->open(o->pobj, NULL, &fh); 65 if (res == TEE_ERROR_CORRUPT_OBJECT) { 66 EMSG("Object corrupt"); 67 fops->remove(o->pobj); 68 tee_obj_close(to_user_ta_ctx(sess->ts_sess.ctx), o); 69 } 70 71 fops->close(&fh); 72 return res; 73 } 74 75 struct tee_obj *tee_obj_alloc(void) 76 { 77 return calloc(1, sizeof(struct tee_obj)); 78 } 79 80 void tee_obj_free(struct tee_obj *o) 81 { 82 if (o) { 83 tee_obj_attr_free(o); 84 free(o->attr); 85 free(o); 86 } 87 } 88