1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2015-2022 Linaro Limited 5 * Copyright (c) 2020, Arm Limited. 6 */ 7 8 #include <assert.h> 9 #include <compiler.h> 10 #include <crypto/crypto.h> 11 #include <ctype.h> 12 #include <initcall.h> 13 #include <keep.h> 14 #include <kernel/ldelf_loader.h> 15 #include <kernel/linker.h> 16 #include <kernel/panic.h> 17 #include <kernel/scall.h> 18 #include <kernel/tee_misc.h> 19 #include <kernel/tee_ta_manager.h> 20 #include <kernel/thread.h> 21 #include <kernel/ts_store.h> 22 #include <kernel/user_access.h> 23 #include <kernel/user_mode_ctx.h> 24 #include <kernel/user_ta.h> 25 #include <mm/core_memprot.h> 26 #include <mm/core_mmu.h> 27 #include <mm/file.h> 28 #include <mm/fobj.h> 29 #include <mm/mobj.h> 30 #include <mm/pgt_cache.h> 31 #include <mm/tee_mm.h> 32 #include <mm/tee_pager.h> 33 #include <mm/vm.h> 34 #include <optee_rpc_cmd.h> 35 #include <printk.h> 36 #include <signed_hdr.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <sys/queue.h> 40 #include <ta_pub_key.h> 41 #include <tee/tee_cryp_utl.h> 42 #include <tee/tee_obj.h> 43 #include <tee/tee_svc_cryp.h> 44 #include <tee/tee_svc.h> 45 #include <tee/tee_svc_storage.h> 46 #include <tee/uuid.h> 47 #include <trace.h> 48 #include <types_ext.h> 49 #include <utee_defines.h> 50 #include <util.h> 51 52 static TEE_Result init_utee_param(struct utee_params *up, 53 const struct tee_ta_param *p, 54 void *va[TEE_NUM_PARAMS]) 55 { 56 TEE_Result res = TEE_SUCCESS; 57 size_t n = 0; 58 struct utee_params *up_bbuf = NULL; 59 60 up_bbuf = bb_alloc(sizeof(struct utee_params)); 61 if (!up_bbuf) 62 return TEE_ERROR_OUT_OF_MEMORY; 63 64 up_bbuf->types = p->types; 65 66 for (n = 0; n < TEE_NUM_PARAMS; n++) { 67 uintptr_t a; 68 uintptr_t b; 69 70 switch (TEE_PARAM_TYPE_GET(p->types, n)) { 71 case TEE_PARAM_TYPE_MEMREF_INPUT: 72 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 73 case TEE_PARAM_TYPE_MEMREF_INOUT: 74 a = (uintptr_t)va[n]; 75 b = p->u[n].mem.size; 76 break; 77 case TEE_PARAM_TYPE_VALUE_INPUT: 78 case TEE_PARAM_TYPE_VALUE_INOUT: 79 a = p->u[n].val.a; 80 b = p->u[n].val.b; 81 break; 82 default: 83 a = 0; 84 b = 0; 85 break; 86 } 87 /* See comment for struct utee_params in utee_types.h */ 88 up_bbuf->vals[n * 2] = a; 89 up_bbuf->vals[n * 2 + 1] = b; 90 } 91 92 res = copy_to_user(up, up_bbuf, sizeof(struct utee_params)); 93 94 bb_free(up_bbuf, sizeof(struct utee_params)); 95 96 return res; 97 } 98 99 static void update_from_utee_param(struct tee_ta_param *p, 100 const struct utee_params *up) 101 { 102 size_t n; 103 104 for (n = 0; n < TEE_NUM_PARAMS; n++) { 105 switch (TEE_PARAM_TYPE_GET(p->types, n)) { 106 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 107 case TEE_PARAM_TYPE_MEMREF_INOUT: 108 /* See comment for struct utee_params in utee_types.h */ 109 p->u[n].mem.size = up->vals[n * 2 + 1]; 110 break; 111 case TEE_PARAM_TYPE_VALUE_OUTPUT: 112 case TEE_PARAM_TYPE_VALUE_INOUT: 113 /* See comment for struct utee_params in utee_types.h */ 114 p->u[n].val.a = up->vals[n * 2]; 115 p->u[n].val.b = up->vals[n * 2 + 1]; 116 break; 117 default: 118 break; 119 } 120 } 121 } 122 123 static bool inc_recursion(void) 124 { 125 struct thread_specific_data *tsd = thread_get_tsd(); 126 127 if (tsd->syscall_recursion >= CFG_CORE_MAX_SYSCALL_RECURSION) { 128 DMSG("Maximum allowed recursion depth reached (%u)", 129 CFG_CORE_MAX_SYSCALL_RECURSION); 130 return false; 131 } 132 133 tsd->syscall_recursion++; 134 return true; 135 } 136 137 static void dec_recursion(void) 138 { 139 struct thread_specific_data *tsd = thread_get_tsd(); 140 141 assert(tsd->syscall_recursion); 142 tsd->syscall_recursion--; 143 } 144 145 static TEE_Result user_ta_enter(struct ts_session *session, 146 enum utee_entry_func func, uint32_t cmd) 147 { 148 TEE_Result res = TEE_SUCCESS; 149 struct utee_params *usr_params = NULL; 150 uaddr_t usr_stack = 0; 151 struct user_ta_ctx *utc = to_user_ta_ctx(session->ctx); 152 struct tee_ta_session *ta_sess = to_ta_session(session); 153 struct ts_session *ts_sess __maybe_unused = NULL; 154 void *param_va[TEE_NUM_PARAMS] = { NULL }; 155 156 if (!inc_recursion()) { 157 /* Using this error code since we've run out of resources. */ 158 res = TEE_ERROR_OUT_OF_MEMORY; 159 goto out_clr_cancel; 160 } 161 if (ta_sess->param) { 162 /* Map user space memory */ 163 res = vm_map_param(&utc->uctx, ta_sess->param, param_va); 164 if (res != TEE_SUCCESS) 165 goto out; 166 } 167 168 /* Switch to user ctx */ 169 ts_push_current_session(session); 170 171 /* Make room for usr_params at top of stack */ 172 usr_stack = utc->uctx.stack_ptr; 173 usr_stack -= ROUNDUP(sizeof(struct utee_params), STACK_ALIGNMENT); 174 usr_params = (struct utee_params *)usr_stack; 175 if (ta_sess->param) 176 res = init_utee_param(usr_params, ta_sess->param, param_va); 177 else 178 res = clear_user(usr_params, sizeof(*usr_params)); 179 180 if (res) 181 goto out_pop_session; 182 183 res = thread_enter_user_mode(func, kaddr_to_uref(session), 184 (vaddr_t)usr_params, cmd, usr_stack, 185 utc->uctx.entry_func, utc->uctx.is_32bit, 186 &utc->ta_ctx.panicked, 187 &utc->ta_ctx.panic_code); 188 189 thread_user_clear_vfp(&utc->uctx); 190 191 if (utc->ta_ctx.panicked) { 192 abort_print_current_ts(); 193 DMSG("tee_user_ta_enter: TA panicked with code 0x%x", 194 utc->ta_ctx.panic_code); 195 res = TEE_ERROR_TARGET_DEAD; 196 } else { 197 /* 198 * According to GP spec the origin should allways be set to 199 * the TA after TA execution 200 */ 201 ta_sess->err_origin = TEE_ORIGIN_TRUSTED_APP; 202 } 203 204 if (ta_sess->param) { 205 /* Copy out value results */ 206 update_from_utee_param(ta_sess->param, usr_params); 207 } 208 209 out_pop_session: 210 if (ta_sess->param) { 211 /* 212 * Clear out the parameter mappings added with 213 * vm_clean_param() above. 214 */ 215 vm_clean_param(&utc->uctx); 216 } 217 ts_sess = ts_pop_current_session(); 218 assert(ts_sess == session); 219 220 out: 221 dec_recursion(); 222 out_clr_cancel: 223 /* 224 * Clear the cancel state now that the user TA has returned. The next 225 * time the TA will be invoked will be with a new operation and should 226 * not have an old cancellation pending. 227 */ 228 ta_sess->cancel = false; 229 230 return res; 231 } 232 233 static TEE_Result user_ta_enter_open_session(struct ts_session *s) 234 { 235 return user_ta_enter(s, UTEE_ENTRY_FUNC_OPEN_SESSION, 0); 236 } 237 238 static TEE_Result user_ta_enter_invoke_cmd(struct ts_session *s, uint32_t cmd) 239 { 240 return user_ta_enter(s, UTEE_ENTRY_FUNC_INVOKE_COMMAND, cmd); 241 } 242 243 static void user_ta_enter_close_session(struct ts_session *s) 244 { 245 /* Only if the TA was fully initialized by ldelf */ 246 if (!to_user_ta_ctx(s->ctx)->uctx.is_initializing) 247 user_ta_enter(s, UTEE_ENTRY_FUNC_CLOSE_SESSION, 0); 248 } 249 250 #if defined(CFG_TA_STATS) 251 static TEE_Result user_ta_enter_dump_memstats(struct ts_session *s) 252 { 253 return user_ta_enter(s, UTEE_ENTRY_FUNC_DUMP_MEMSTATS, 0); 254 } 255 #endif 256 257 static void dump_state_no_ldelf_dbg(struct user_ta_ctx *utc) 258 { 259 user_mode_ctx_print_mappings(&utc->uctx); 260 } 261 262 static void user_ta_dump_state(struct ts_ctx *ctx) 263 { 264 struct user_ta_ctx *utc = to_user_ta_ctx(ctx); 265 266 if (utc->uctx.dump_entry_func) { 267 TEE_Result res = ldelf_dump_state(&utc->uctx); 268 269 if (!res || res == TEE_ERROR_TARGET_DEAD) 270 return; 271 /* 272 * Fall back to dump_state_no_ldelf_dbg() if 273 * ldelf_dump_state() fails for some reason. 274 * 275 * If ldelf_dump_state() failed with panic 276 * we are done since abort_print_current_ts() will be 277 * called which will dump the memory map. 278 */ 279 } 280 281 dump_state_no_ldelf_dbg(utc); 282 } 283 284 #ifdef CFG_FTRACE_SUPPORT 285 static void user_ta_dump_ftrace(struct ts_ctx *ctx) 286 { 287 uint32_t prot = TEE_MATTR_URW; 288 struct user_ta_ctx *utc = to_user_ta_ctx(ctx); 289 struct thread_param params[3] = { }; 290 TEE_Result res = TEE_SUCCESS; 291 struct mobj *mobj = NULL; 292 uint8_t *ubuf = NULL; 293 void *buf = NULL; 294 size_t pl_sz = 0; 295 size_t blen = 0, ld_addr_len = 0; 296 vaddr_t va = 0; 297 298 res = ldelf_dump_ftrace(&utc->uctx, NULL, &blen); 299 if (res != TEE_ERROR_SHORT_BUFFER) 300 return; 301 302 #define LOAD_ADDR_DUMP_SIZE 64 303 pl_sz = ROUNDUP(blen + sizeof(TEE_UUID) + LOAD_ADDR_DUMP_SIZE, 304 SMALL_PAGE_SIZE); 305 306 mobj = thread_rpc_alloc_payload(pl_sz); 307 if (!mobj) { 308 EMSG("Ftrace thread_rpc_alloc_payload failed"); 309 return; 310 } 311 312 buf = mobj_get_va(mobj, 0, pl_sz); 313 if (!buf) 314 goto out_free_pl; 315 316 res = vm_map(&utc->uctx, &va, mobj->size, prot, VM_FLAG_EPHEMERAL, 317 mobj, 0); 318 if (res) 319 goto out_free_pl; 320 321 ubuf = (uint8_t *)va + mobj_get_phys_offs(mobj, mobj->phys_granule); 322 memcpy(ubuf, &ctx->uuid, sizeof(TEE_UUID)); 323 ubuf += sizeof(TEE_UUID); 324 325 ld_addr_len = snprintk((char *)ubuf, LOAD_ADDR_DUMP_SIZE, 326 "TEE load address @ %#"PRIxVA"\n", 327 VCORE_START_VA); 328 ubuf += ld_addr_len; 329 330 res = ldelf_dump_ftrace(&utc->uctx, ubuf, &blen); 331 if (res) { 332 EMSG("Ftrace dump failed: %#"PRIx32, res); 333 goto out_unmap_pl; 334 } 335 336 params[0] = THREAD_PARAM_VALUE(INOUT, 0, 0, 0); 337 params[1] = THREAD_PARAM_MEMREF(IN, mobj, 0, sizeof(TEE_UUID)); 338 params[2] = THREAD_PARAM_MEMREF(IN, mobj, sizeof(TEE_UUID), 339 blen + ld_addr_len); 340 341 res = thread_rpc_cmd(OPTEE_RPC_CMD_FTRACE, 3, params); 342 if (res) 343 EMSG("Ftrace thread_rpc_cmd res: %#"PRIx32, res); 344 345 out_unmap_pl: 346 res = vm_unmap(&utc->uctx, va, mobj->size); 347 assert(!res); 348 out_free_pl: 349 thread_rpc_free_payload(mobj); 350 } 351 #endif /*CFG_FTRACE_SUPPORT*/ 352 353 #ifdef CFG_TA_GPROF_SUPPORT 354 static void user_ta_gprof_set_status(enum ts_gprof_status status) 355 { 356 if (status == TS_GPROF_SUSPEND) 357 tee_ta_update_session_utime_suspend(); 358 else 359 tee_ta_update_session_utime_resume(); 360 } 361 #endif /*CFG_TA_GPROF_SUPPORT*/ 362 363 static void free_utc(struct user_ta_ctx *utc) 364 { 365 366 /* 367 * Close sessions opened by this TA 368 * Note that tee_ta_close_session() removes the item 369 * from the utc->open_sessions list. 370 */ 371 while (!TAILQ_EMPTY(&utc->open_sessions)) { 372 tee_ta_close_session(TAILQ_FIRST(&utc->open_sessions), 373 &utc->open_sessions, KERN_IDENTITY); 374 } 375 376 vm_info_final(&utc->uctx); 377 378 /* Free cryp states created by this TA */ 379 tee_svc_cryp_free_states(utc); 380 /* Close cryp objects opened by this TA */ 381 tee_obj_close_all(utc); 382 /* Free emums created by this TA */ 383 tee_svc_storage_close_all_enum(utc); 384 free(utc); 385 } 386 387 static void user_ta_ctx_destroy(struct ts_ctx *ctx) 388 { 389 free_utc(to_user_ta_ctx(ctx)); 390 } 391 392 static uint32_t user_ta_get_instance_id(struct ts_ctx *ctx) 393 { 394 return to_user_ta_ctx(ctx)->uctx.vm_info.asid; 395 } 396 397 /* 398 * Note: this variable is weak just to ease breaking its dependency chain 399 * when added to the unpaged area. 400 */ 401 const struct ts_ops user_ta_ops __weak __relrodata_unpaged("user_ta_ops") = { 402 .enter_open_session = user_ta_enter_open_session, 403 .enter_invoke_cmd = user_ta_enter_invoke_cmd, 404 .enter_close_session = user_ta_enter_close_session, 405 #if defined(CFG_TA_STATS) 406 .dump_mem_stats = user_ta_enter_dump_memstats, 407 #endif 408 .dump_state = user_ta_dump_state, 409 #ifdef CFG_FTRACE_SUPPORT 410 .dump_ftrace = user_ta_dump_ftrace, 411 #endif 412 .destroy = user_ta_ctx_destroy, 413 .get_instance_id = user_ta_get_instance_id, 414 .handle_scall = scall_handle_user_ta, 415 #ifdef CFG_TA_GPROF_SUPPORT 416 .gprof_set_status = user_ta_gprof_set_status, 417 #endif 418 }; 419 420 static void set_ta_ctx_ops(struct tee_ta_ctx *ctx) 421 { 422 ctx->ts_ctx.ops = &user_ta_ops; 423 } 424 425 bool is_user_ta_ctx(struct ts_ctx *ctx) 426 { 427 return ctx && ctx->ops == &user_ta_ops; 428 } 429 430 static TEE_Result check_ta_store(void) 431 { 432 const struct ts_store_ops *op = NULL; 433 434 SCATTERED_ARRAY_FOREACH(op, ta_stores, struct ts_store_ops) 435 DMSG("TA store: \"%s\"", op->description); 436 437 return TEE_SUCCESS; 438 } 439 service_init(check_ta_store); 440 441 TEE_Result tee_ta_init_user_ta_session(const TEE_UUID *uuid, 442 struct tee_ta_session *s) 443 { 444 TEE_Result res = TEE_SUCCESS; 445 struct user_ta_ctx *utc = NULL; 446 447 utc = calloc(1, sizeof(struct user_ta_ctx)); 448 if (!utc) 449 return TEE_ERROR_OUT_OF_MEMORY; 450 451 TAILQ_INIT(&utc->open_sessions); 452 TAILQ_INIT(&utc->cryp_states); 453 TAILQ_INIT(&utc->objects); 454 TAILQ_INIT(&utc->storage_enums); 455 condvar_init(&utc->ta_ctx.busy_cv); 456 utc->ta_ctx.ref_count = 1; 457 458 /* 459 * Set context TA operation structure. It is required by generic 460 * implementation to identify userland TA versus pseudo TA contexts. 461 */ 462 set_ta_ctx_ops(&utc->ta_ctx); 463 464 utc->ta_ctx.ts_ctx.uuid = *uuid; 465 res = vm_info_init(&utc->uctx, &utc->ta_ctx.ts_ctx); 466 if (res) 467 goto out; 468 utc->uctx.is_initializing = true; 469 470 #ifdef CFG_TA_PAUTH 471 crypto_rng_read(&utc->uctx.keys, sizeof(utc->uctx.keys)); 472 #endif 473 474 mutex_lock(&tee_ta_mutex); 475 s->ts_sess.ctx = &utc->ta_ctx.ts_ctx; 476 s->ts_sess.handle_scall = s->ts_sess.ctx->ops->handle_scall; 477 /* 478 * Another thread trying to load this same TA may need to wait 479 * until this context is fully initialized. This is needed to 480 * handle single instance TAs. 481 */ 482 TAILQ_INSERT_TAIL(&tee_ctxes, &utc->ta_ctx, link); 483 mutex_unlock(&tee_ta_mutex); 484 485 /* 486 * We must not hold tee_ta_mutex while allocating page tables as 487 * that may otherwise lead to a deadlock. 488 */ 489 ts_push_current_session(&s->ts_sess); 490 491 res = ldelf_load_ldelf(&utc->uctx); 492 if (!res) 493 res = ldelf_init_with_ldelf(&s->ts_sess, &utc->uctx); 494 495 ts_pop_current_session(); 496 497 mutex_lock(&tee_ta_mutex); 498 499 if (!res) { 500 utc->uctx.is_initializing = false; 501 } else { 502 s->ts_sess.ctx = NULL; 503 TAILQ_REMOVE(&tee_ctxes, &utc->ta_ctx, link); 504 } 505 506 /* The state has changed for the context, notify eventual waiters. */ 507 condvar_broadcast(&tee_ta_init_cv); 508 509 mutex_unlock(&tee_ta_mutex); 510 511 out: 512 if (res) { 513 condvar_destroy(&utc->ta_ctx.busy_cv); 514 free_utc(utc); 515 } 516 517 return res; 518 } 519