1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2015-2020, 2022 Linaro Limited 5 * Copyright (c) 2020-2021, Arm Limited 6 */ 7 8 #include <assert.h> 9 #include <kernel/ldelf_loader.h> 10 #include <kernel/ldelf_syscalls.h> 11 #include <kernel/scall.h> 12 #include <ldelf.h> 13 #include <mm/mobj.h> 14 #include <mm/vm.h> 15 16 extern uint8_t ldelf_data[]; 17 extern const unsigned int ldelf_code_size; 18 extern const unsigned int ldelf_data_size; 19 extern const unsigned int ldelf_entry; 20 21 /* ldelf has the same architecture/register width as the kernel */ 22 #if defined(ARM32) || defined(RV32) 23 static const bool is_32bit = true; 24 #else 25 static const bool is_32bit; 26 #endif 27 28 static TEE_Result alloc_and_map_ldelf_fobj(struct user_mode_ctx *uctx, 29 size_t sz, uint32_t prot, 30 vaddr_t *va) 31 { 32 size_t num_pgs = ROUNDUP(sz, SMALL_PAGE_SIZE) / SMALL_PAGE_SIZE; 33 struct fobj *fobj = fobj_ta_mem_alloc(num_pgs); 34 struct mobj *mobj = mobj_with_fobj_alloc(fobj, NULL, 35 TEE_MATTR_MEM_TYPE_TAGGED); 36 TEE_Result res = TEE_SUCCESS; 37 38 fobj_put(fobj); 39 if (!mobj) 40 return TEE_ERROR_OUT_OF_MEMORY; 41 res = vm_map(uctx, va, num_pgs * SMALL_PAGE_SIZE, 42 prot, VM_FLAG_LDELF, mobj, 0); 43 mobj_put(mobj); 44 45 return res; 46 } 47 48 /* 49 * This function may leave a few mappings behind on error, but that's taken 50 * care of by tee_ta_init_user_ta_session() since the entire context is 51 * removed then. 52 */ 53 TEE_Result ldelf_load_ldelf(struct user_mode_ctx *uctx) 54 { 55 TEE_Result res = TEE_SUCCESS; 56 vaddr_t stack_addr = 0; 57 vaddr_t code_addr = 0; 58 vaddr_t rw_addr = 0; 59 uint32_t prot = 0; 60 61 uctx->is_32bit = is_32bit; 62 63 res = alloc_and_map_ldelf_fobj(uctx, LDELF_STACK_SIZE, 64 TEE_MATTR_URW | TEE_MATTR_PRW, 65 &stack_addr); 66 if (res) 67 return res; 68 uctx->ldelf_stack_ptr = stack_addr + LDELF_STACK_SIZE; 69 70 res = alloc_and_map_ldelf_fobj(uctx, ldelf_code_size, TEE_MATTR_PRW, 71 &code_addr); 72 if (res) 73 return res; 74 uctx->entry_func = code_addr + ldelf_entry; 75 76 rw_addr = ROUNDUP(code_addr + ldelf_code_size, SMALL_PAGE_SIZE); 77 res = alloc_and_map_ldelf_fobj(uctx, ldelf_data_size, 78 TEE_MATTR_URW | TEE_MATTR_PRW, &rw_addr); 79 if (res) 80 return res; 81 82 vm_set_ctx(uctx->ts_ctx); 83 84 memcpy((void *)code_addr, ldelf_data, ldelf_code_size); 85 memcpy((void *)rw_addr, ldelf_data + ldelf_code_size, ldelf_data_size); 86 87 prot = TEE_MATTR_URX; 88 if (IS_ENABLED(CFG_CORE_BTI)) 89 prot |= TEE_MATTR_GUARDED; 90 91 res = vm_set_prot(uctx, code_addr, 92 ROUNDUP(ldelf_code_size, SMALL_PAGE_SIZE), prot); 93 if (res) 94 return res; 95 96 DMSG("ldelf load address %#"PRIxVA, code_addr); 97 98 return TEE_SUCCESS; 99 } 100 101 TEE_Result ldelf_init_with_ldelf(struct ts_session *sess, 102 struct user_mode_ctx *uctx) 103 { 104 TEE_Result res = TEE_SUCCESS; 105 struct ldelf_arg *arg = NULL; 106 uint32_t panic_code = 0; 107 uint32_t panicked = 0; 108 uaddr_t usr_stack = 0; 109 110 usr_stack = uctx->ldelf_stack_ptr; 111 usr_stack -= ROUNDUP(sizeof(*arg), STACK_ALIGNMENT); 112 arg = (struct ldelf_arg *)usr_stack; 113 memset(arg, 0, sizeof(*arg)); 114 arg->uuid = uctx->ts_ctx->uuid; 115 sess->handle_scall = scall_handle_ldelf; 116 117 res = thread_enter_user_mode((vaddr_t)arg, 0, 0, 0, 118 usr_stack, uctx->entry_func, 119 is_32bit, &panicked, &panic_code); 120 121 sess->handle_scall = sess->ctx->ops->handle_scall; 122 thread_user_clear_vfp(uctx); 123 ldelf_sess_cleanup(sess); 124 125 if (panicked) { 126 abort_print_current_ts(); 127 EMSG("ldelf panicked"); 128 return TEE_ERROR_GENERIC; 129 } 130 if (res) { 131 EMSG("ldelf failed with res: %#"PRIx32, res); 132 return res; 133 } 134 135 res = vm_check_access_rights(uctx, 136 TEE_MEMORY_ACCESS_READ | 137 TEE_MEMORY_ACCESS_ANY_OWNER, 138 (uaddr_t)arg, sizeof(*arg)); 139 if (res) 140 return res; 141 142 if (is_user_ta_ctx(uctx->ts_ctx)) { 143 /* 144 * This is already checked by the elf loader, but since it runs 145 * in user mode we're not trusting it entirely. 146 */ 147 if (arg->flags & ~TA_FLAGS_MASK) 148 return TEE_ERROR_BAD_FORMAT; 149 150 to_user_ta_ctx(uctx->ts_ctx)->ta_ctx.flags = arg->flags; 151 } 152 153 uctx->is_32bit = arg->is_32bit; 154 uctx->entry_func = arg->entry_func; 155 uctx->stack_ptr = arg->stack_ptr; 156 uctx->dump_entry_func = arg->dump_entry; 157 #ifdef CFG_FTRACE_SUPPORT 158 uctx->ftrace_entry_func = arg->ftrace_entry; 159 sess->fbuf = arg->fbuf; 160 #endif 161 uctx->dl_entry_func = arg->dl_entry; 162 163 return TEE_SUCCESS; 164 } 165 166 TEE_Result ldelf_dump_state(struct user_mode_ctx *uctx) 167 { 168 TEE_Result res = TEE_SUCCESS; 169 uaddr_t usr_stack = uctx->ldelf_stack_ptr; 170 struct dump_entry_arg *arg = NULL; 171 uint32_t panic_code = 0; 172 uint32_t panicked = 0; 173 struct thread_specific_data *tsd = thread_get_tsd(); 174 struct ts_session *sess = NULL; 175 struct vm_region *r = NULL; 176 size_t n = 0; 177 178 TAILQ_FOREACH(r, &uctx->vm_info.regions, link) 179 if (r->attr & TEE_MATTR_URWX) 180 n++; 181 182 usr_stack = uctx->ldelf_stack_ptr; 183 usr_stack -= ROUNDUP(sizeof(*arg) + n * sizeof(struct dump_map), 184 STACK_ALIGNMENT); 185 arg = (struct dump_entry_arg *)usr_stack; 186 187 res = vm_check_access_rights(uctx, 188 TEE_MEMORY_ACCESS_READ | 189 TEE_MEMORY_ACCESS_ANY_OWNER, 190 (uaddr_t)arg, sizeof(*arg)); 191 if (res) { 192 EMSG("ldelf stack is inaccessible!"); 193 return res; 194 } 195 196 memset(arg, 0, sizeof(*arg) + n * sizeof(struct dump_map)); 197 198 arg->num_maps = n; 199 n = 0; 200 TAILQ_FOREACH(r, &uctx->vm_info.regions, link) { 201 if (r->attr & TEE_MATTR_URWX) { 202 if (r->mobj) 203 mobj_get_pa(r->mobj, r->offset, 0, 204 &arg->maps[n].pa); 205 arg->maps[n].va = r->va; 206 arg->maps[n].sz = r->size; 207 if (r->attr & TEE_MATTR_UR) 208 arg->maps[n].flags |= DUMP_MAP_READ; 209 if (r->attr & TEE_MATTR_UW) 210 arg->maps[n].flags |= DUMP_MAP_WRITE; 211 if (r->attr & TEE_MATTR_UX) 212 arg->maps[n].flags |= DUMP_MAP_EXEC; 213 if (r->attr & TEE_MATTR_SECURE) 214 arg->maps[n].flags |= DUMP_MAP_SECURE; 215 if (r->flags & VM_FLAG_EPHEMERAL) 216 arg->maps[n].flags |= DUMP_MAP_EPHEM; 217 if (r->flags & VM_FLAG_LDELF) 218 arg->maps[n].flags |= DUMP_MAP_LDELF; 219 n++; 220 } 221 } 222 223 arg->is_32bit = uctx->is_32bit; 224 #ifdef ARM32 225 arg->arm32.regs[0] = tsd->abort_regs.r0; 226 arg->arm32.regs[1] = tsd->abort_regs.r1; 227 arg->arm32.regs[2] = tsd->abort_regs.r2; 228 arg->arm32.regs[3] = tsd->abort_regs.r3; 229 arg->arm32.regs[4] = tsd->abort_regs.r4; 230 arg->arm32.regs[5] = tsd->abort_regs.r5; 231 arg->arm32.regs[6] = tsd->abort_regs.r6; 232 arg->arm32.regs[7] = tsd->abort_regs.r7; 233 arg->arm32.regs[8] = tsd->abort_regs.r8; 234 arg->arm32.regs[9] = tsd->abort_regs.r9; 235 arg->arm32.regs[10] = tsd->abort_regs.r10; 236 arg->arm32.regs[11] = tsd->abort_regs.r11; 237 arg->arm32.regs[12] = tsd->abort_regs.ip; 238 arg->arm32.regs[13] = tsd->abort_regs.usr_sp; /*SP*/ 239 arg->arm32.regs[14] = tsd->abort_regs.usr_lr; /*LR*/ 240 arg->arm32.regs[15] = tsd->abort_regs.elr; /*PC*/ 241 #endif /*ARM32*/ 242 #ifdef ARM64 243 if (uctx->is_32bit) { 244 arg->arm32.regs[0] = tsd->abort_regs.x0; 245 arg->arm32.regs[1] = tsd->abort_regs.x1; 246 arg->arm32.regs[2] = tsd->abort_regs.x2; 247 arg->arm32.regs[3] = tsd->abort_regs.x3; 248 arg->arm32.regs[4] = tsd->abort_regs.x4; 249 arg->arm32.regs[5] = tsd->abort_regs.x5; 250 arg->arm32.regs[6] = tsd->abort_regs.x6; 251 arg->arm32.regs[7] = tsd->abort_regs.x7; 252 arg->arm32.regs[8] = tsd->abort_regs.x8; 253 arg->arm32.regs[9] = tsd->abort_regs.x9; 254 arg->arm32.regs[10] = tsd->abort_regs.x10; 255 arg->arm32.regs[11] = tsd->abort_regs.x11; 256 arg->arm32.regs[12] = tsd->abort_regs.x12; 257 arg->arm32.regs[13] = tsd->abort_regs.x13; /*SP*/ 258 arg->arm32.regs[14] = tsd->abort_regs.x14; /*LR*/ 259 arg->arm32.regs[15] = tsd->abort_regs.elr; /*PC*/ 260 } else { 261 arg->arm64.fp = tsd->abort_regs.x29; 262 arg->arm64.pc = tsd->abort_regs.elr; 263 arg->arm64.sp = tsd->abort_regs.sp_el0; 264 } 265 #endif /*ARM64*/ 266 #if defined(RV64) || defined(RV32) 267 arg->rv.fp = tsd->abort_regs.s0; 268 arg->rv.pc = tsd->abort_regs.epc; 269 arg->rv.sp = tsd->abort_regs.sp; 270 #endif /*RV64||RV32*/ 271 272 sess = ts_get_current_session(); 273 sess->handle_scall = scall_handle_ldelf; 274 275 res = thread_enter_user_mode((vaddr_t)arg, 0, 0, 0, 276 usr_stack, uctx->dump_entry_func, 277 is_32bit, &panicked, &panic_code); 278 279 sess->handle_scall = sess->ctx->ops->handle_scall; 280 thread_user_clear_vfp(uctx); 281 ldelf_sess_cleanup(sess); 282 283 if (panicked) { 284 uctx->dump_entry_func = 0; 285 EMSG("ldelf dump function panicked"); 286 abort_print_current_ts(); 287 res = TEE_ERROR_TARGET_DEAD; 288 } 289 290 return res; 291 } 292 293 #ifdef CFG_FTRACE_SUPPORT 294 TEE_Result ldelf_dump_ftrace(struct user_mode_ctx *uctx, 295 void *buf, size_t *blen) 296 { 297 uaddr_t usr_stack = uctx->ldelf_stack_ptr; 298 TEE_Result res = TEE_SUCCESS; 299 uint32_t panic_code = 0; 300 uint32_t panicked = 0; 301 size_t *arg = NULL; 302 struct ts_session *sess = NULL; 303 304 if (!uctx->ftrace_entry_func) 305 return TEE_ERROR_NOT_SUPPORTED; 306 307 usr_stack -= ROUNDUP(sizeof(*arg), STACK_ALIGNMENT); 308 arg = (size_t *)usr_stack; 309 310 res = vm_check_access_rights(uctx, 311 TEE_MEMORY_ACCESS_READ | 312 TEE_MEMORY_ACCESS_ANY_OWNER, 313 (uaddr_t)arg, sizeof(*arg)); 314 if (res) { 315 EMSG("ldelf stack is inaccessible!"); 316 return res; 317 } 318 319 *arg = *blen; 320 321 sess = ts_get_current_session(); 322 sess->handle_scall = scall_handle_ldelf; 323 324 res = thread_enter_user_mode((vaddr_t)buf, (vaddr_t)arg, 0, 0, 325 usr_stack, uctx->ftrace_entry_func, 326 is_32bit, &panicked, &panic_code); 327 328 sess->handle_scall = sess->ctx->ops->handle_scall; 329 thread_user_clear_vfp(uctx); 330 ldelf_sess_cleanup(sess); 331 332 if (panicked) { 333 uctx->ftrace_entry_func = 0; 334 EMSG("ldelf ftrace function panicked"); 335 abort_print_current_ts(); 336 res = TEE_ERROR_TARGET_DEAD; 337 } 338 339 if (!res) { 340 if (*arg > *blen) 341 res = TEE_ERROR_SHORT_BUFFER; 342 *blen = *arg; 343 } 344 345 return res; 346 } 347 #endif /*CFG_FTRACE_SUPPORT*/ 348 349 TEE_Result ldelf_dlopen(struct user_mode_ctx *uctx, TEE_UUID *uuid, 350 uint32_t flags) 351 { 352 uaddr_t usr_stack = uctx->ldelf_stack_ptr; 353 TEE_Result res = TEE_ERROR_GENERIC; 354 struct dl_entry_arg *arg = NULL; 355 uint32_t panic_code = 0; 356 uint32_t panicked = 0; 357 struct ts_session *sess = NULL; 358 359 assert(uuid); 360 361 usr_stack -= ROUNDUP(sizeof(*arg), STACK_ALIGNMENT); 362 arg = (struct dl_entry_arg *)usr_stack; 363 364 res = vm_check_access_rights(uctx, 365 TEE_MEMORY_ACCESS_READ | 366 TEE_MEMORY_ACCESS_WRITE | 367 TEE_MEMORY_ACCESS_ANY_OWNER, 368 (uaddr_t)arg, sizeof(*arg)); 369 if (res) { 370 EMSG("ldelf stack is inaccessible!"); 371 return res; 372 } 373 374 memset(arg, 0, sizeof(*arg)); 375 arg->cmd = LDELF_DL_ENTRY_DLOPEN; 376 arg->dlopen.uuid = *uuid; 377 arg->dlopen.flags = flags; 378 379 sess = ts_get_current_session(); 380 sess->handle_scall = scall_handle_ldelf; 381 382 res = thread_enter_user_mode((vaddr_t)arg, 0, 0, 0, 383 usr_stack, uctx->dl_entry_func, 384 is_32bit, &panicked, &panic_code); 385 386 sess->handle_scall = sess->ctx->ops->handle_scall; 387 ldelf_sess_cleanup(sess); 388 389 if (panicked) { 390 EMSG("ldelf dl_entry function panicked"); 391 abort_print_current_ts(); 392 res = TEE_ERROR_TARGET_DEAD; 393 } 394 if (!res) 395 res = arg->ret; 396 397 return res; 398 } 399 400 TEE_Result ldelf_dlsym(struct user_mode_ctx *uctx, TEE_UUID *uuid, 401 const char *sym, size_t maxlen, vaddr_t *val) 402 { 403 uaddr_t usr_stack = uctx->ldelf_stack_ptr; 404 TEE_Result res = TEE_ERROR_GENERIC; 405 struct dl_entry_arg *arg = NULL; 406 uint32_t panic_code = 0; 407 uint32_t panicked = 0; 408 size_t len = strnlen(sym, maxlen); 409 struct ts_session *sess = NULL; 410 411 if (len == maxlen) 412 return TEE_ERROR_BAD_PARAMETERS; 413 414 usr_stack -= ROUNDUP(sizeof(*arg) + len + 1, STACK_ALIGNMENT); 415 arg = (struct dl_entry_arg *)usr_stack; 416 417 res = vm_check_access_rights(uctx, 418 TEE_MEMORY_ACCESS_READ | 419 TEE_MEMORY_ACCESS_WRITE | 420 TEE_MEMORY_ACCESS_ANY_OWNER, 421 (uaddr_t)arg, sizeof(*arg) + len + 1); 422 if (res) { 423 EMSG("ldelf stack is inaccessible!"); 424 return res; 425 } 426 427 memset(arg, 0, sizeof(*arg)); 428 arg->cmd = LDELF_DL_ENTRY_DLSYM; 429 arg->dlsym.uuid = *uuid; 430 memcpy(arg->dlsym.symbol, sym, len); 431 arg->dlsym.symbol[len] = '\0'; 432 433 sess = ts_get_current_session(); 434 sess->handle_scall = scall_handle_ldelf; 435 436 res = thread_enter_user_mode((vaddr_t)arg, 0, 0, 0, 437 usr_stack, uctx->dl_entry_func, 438 is_32bit, &panicked, &panic_code); 439 440 sess->handle_scall = sess->ctx->ops->handle_scall; 441 ldelf_sess_cleanup(sess); 442 443 if (panicked) { 444 EMSG("ldelf dl_entry function panicked"); 445 abort_print_current_ts(); 446 res = TEE_ERROR_TARGET_DEAD; 447 } 448 if (!res) { 449 res = arg->ret; 450 if (!res) 451 *val = arg->dlsym.val; 452 } 453 454 return res; 455 } 456