1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2020-2022 Linaro Limited 5 */ 6 7 #include <compiler.h> 8 #include <kernel/chip_services.h> 9 #include <kernel/pseudo_ta.h> 10 #include <kernel/tee_common.h> 11 #include <kernel/tee_common_otp.h> 12 #include <kernel/tee_ta_manager.h> 13 #include <kernel/tee_time.h> 14 #include <kernel/trace_ta.h> 15 #include <kernel/user_access.h> 16 #include <memtag.h> 17 #include <mm/core_memprot.h> 18 #include <mm/mobj.h> 19 #include <mm/tee_mm.h> 20 #include <mm/vm.h> 21 #include <stdlib_ext.h> 22 #include <tee_api_types.h> 23 #include <tee/tee_cryp_utl.h> 24 #include <tee/tee_svc.h> 25 #include <trace.h> 26 #include <user_ta_header.h> 27 #include <utee_types.h> 28 #include <util.h> 29 30 vaddr_t tee_svc_uref_base; 31 32 void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused) 33 { 34 #ifdef CFG_TEE_CORE_TA_TRACE 35 char *kbuf; 36 37 if (len == 0) 38 return; 39 40 kbuf = malloc(len + 1); 41 if (kbuf == NULL) 42 return; 43 44 if (copy_from_user(kbuf, buf, len) == TEE_SUCCESS) { 45 kbuf[len] = '\0'; 46 trace_ext_puts(kbuf); 47 } 48 49 free_wipe(kbuf); 50 #endif 51 } 52 53 TEE_Result syscall_not_supported(void) 54 { 55 return TEE_ERROR_NOT_SUPPORTED; 56 } 57 58 /* Configuration properties */ 59 /* API implementation version */ 60 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION); 61 62 /* Implementation description (implementation-dependent) */ 63 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR); 64 65 /* 66 * TA persistent time protection level 67 * 100: Persistent time based on an REE-controlled real-time clock 68 * and on the TEE Trusted Storage for the storage of origins (default). 69 * 1000: Persistent time based on a TEE-controlled real-time clock 70 * and the TEE Trusted Storage. 71 * The real-time clock MUST be out of reach of software attacks 72 * from the REE. 73 */ 74 static const uint32_t ta_time_prot_lvl = 100; 75 76 /* Elliptic Curve Cryptographic support */ 77 #ifdef CFG_CRYPTO_ECC 78 static const bool crypto_ecc_en = 1; 79 #else 80 static const bool crypto_ecc_en; 81 #endif 82 83 /* 84 * Trusted storage anti rollback protection level 85 * 0 (or missing): No antirollback protection (default) 86 * 100: Antirollback enforced at REE level 87 * 1000: Antirollback TEE-controlled hardware 88 */ 89 #ifdef CFG_RPMB_FS 90 static const uint32_t ts_antiroll_prot_lvl = 1000; 91 #else 92 static const uint32_t ts_antiroll_prot_lvl; 93 #endif 94 95 /* Trusted OS implementation version */ 96 static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION); 97 98 /* Trusted OS implementation version (binary value) */ 99 static const uint32_t trustedos_impl_bin_version; /* 0 by default */ 100 101 /* Trusted OS implementation manufacturer name */ 102 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER); 103 104 /* Trusted firmware version */ 105 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION); 106 107 /* Trusted firmware version (binary value) */ 108 static const uint32_t fw_impl_bin_version; /* 0 by default */ 109 110 /* Trusted firmware manufacturer name */ 111 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER); 112 113 static TEE_Result get_prop_tee_dev_id(struct ts_session *sess __unused, 114 void *buf, size_t *blen) 115 { 116 TEE_Result res; 117 TEE_UUID uuid; 118 const size_t nslen = 5; 119 uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 120 'O', 'P', 'T', 'E', 'E' }; 121 122 if (*blen < sizeof(uuid)) { 123 *blen = sizeof(uuid); 124 return TEE_ERROR_SHORT_BUFFER; 125 } 126 *blen = sizeof(uuid); 127 128 if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen)) 129 return TEE_ERROR_BAD_STATE; 130 131 res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data), 132 (uint8_t *)&uuid, sizeof(uuid)); 133 if (res != TEE_SUCCESS) 134 return TEE_ERROR_BAD_STATE; 135 136 /* 137 * Changes the random value into and UUID as specifiec 138 * in RFC 4122. The magic values are from the example 139 * code in the RFC. 140 * 141 * TEE_UUID is defined slightly different from the RFC, 142 * but close enough for our purpose. 143 */ 144 145 uuid.timeHiAndVersion &= 0x0fff; 146 uuid.timeHiAndVersion |= 5 << 12; 147 148 /* uuid.clock_seq_hi_and_reserved in the RFC */ 149 uuid.clockSeqAndNode[0] &= 0x3f; 150 uuid.clockSeqAndNode[0] |= 0x80; 151 152 return copy_to_user(buf, &uuid, sizeof(TEE_UUID)); 153 } 154 155 static TEE_Result 156 get_prop_tee_sys_time_prot_level(struct ts_session *sess __unused, 157 void *buf, size_t *blen) 158 { 159 uint32_t prot; 160 161 if (*blen < sizeof(prot)) { 162 *blen = sizeof(prot); 163 return TEE_ERROR_SHORT_BUFFER; 164 } 165 *blen = sizeof(prot); 166 prot = tee_time_get_sys_time_protection_level(); 167 return copy_to_user(buf, &prot, sizeof(prot)); 168 } 169 170 static TEE_Result get_prop_client_id(struct ts_session *sess, 171 void *buf, size_t *blen) 172 { 173 if (*blen < sizeof(TEE_Identity)) { 174 *blen = sizeof(TEE_Identity); 175 return TEE_ERROR_SHORT_BUFFER; 176 } 177 *blen = sizeof(TEE_Identity); 178 return copy_to_user(buf, &to_ta_session(sess)->clnt_id, 179 sizeof(TEE_Identity)); 180 } 181 182 static TEE_Result get_prop_ta_app_id(struct ts_session *sess, 183 void *buf, size_t *blen) 184 { 185 if (*blen < sizeof(TEE_UUID)) { 186 *blen = sizeof(TEE_UUID); 187 return TEE_ERROR_SHORT_BUFFER; 188 } 189 *blen = sizeof(TEE_UUID); 190 return copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID)); 191 } 192 193 #ifdef CFG_TA_BTI 194 static TEE_Result 195 get_prop_feat_bti_implemented(struct ts_session *sess __unused, void *buf, 196 size_t *blen) 197 { 198 bool bti_impl = false; 199 200 if (*blen < sizeof(bti_impl)) { 201 *blen = sizeof(bti_impl); 202 return TEE_ERROR_SHORT_BUFFER; 203 } 204 *blen = sizeof(bti_impl); 205 bti_impl = feat_bti_is_implemented(); 206 207 return copy_to_user(buf, &bti_impl, sizeof(bti_impl)); 208 } 209 #endif 210 211 #ifdef CFG_TA_PAUTH 212 static TEE_Result 213 get_prop_feat_pauth_implemented(struct ts_session *sess __unused, void *buf, 214 size_t *blen) 215 { 216 bool pauth_impl = false; 217 218 if (*blen < sizeof(pauth_impl)) { 219 *blen = sizeof(pauth_impl); 220 return TEE_ERROR_SHORT_BUFFER; 221 } 222 *blen = sizeof(pauth_impl); 223 pauth_impl = feat_pauth_is_implemented(); 224 225 return copy_to_user(buf, &pauth_impl, sizeof(pauth_impl)); 226 } 227 #endif 228 229 #if MEMTAG_IS_ENABLED 230 static TEE_Result 231 get_prop_feat_memtag_implemented(struct ts_session *sess __unused, void *buf, 232 size_t *blen) 233 { 234 uint32_t v = 0; 235 236 if (*blen < sizeof(v)) { 237 *blen = sizeof(v); 238 return TEE_ERROR_SHORT_BUFFER; 239 } 240 *blen = sizeof(v); 241 if (memtag_is_enabled()) 242 v = feat_mte_implemented(); 243 244 return copy_to_user(buf, &v, sizeof(v)); 245 } 246 #endif 247 248 /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */ 249 const struct tee_props tee_propset_client[] = { 250 { 251 .name = "gpd.client.identity", 252 .prop_type = USER_TA_PROP_TYPE_IDENTITY, 253 .get_prop_func = get_prop_client_id 254 }, 255 }; 256 257 /* Properties of the set TEE_PROPSET_CURRENT_TA */ 258 const struct tee_props tee_propset_ta[] = { 259 { 260 .name = "gpd.ta.appID", 261 .prop_type = USER_TA_PROP_TYPE_UUID, 262 .get_prop_func = get_prop_ta_app_id 263 }, 264 265 /* 266 * Following properties are processed directly in libutee: 267 * TA_PROP_STR_SINGLE_INSTANCE 268 * TA_PROP_STR_MULTI_SESSION 269 * TA_PROP_STR_KEEP_ALIVE 270 * TA_PROP_STR_DATA_SIZE 271 * TA_PROP_STR_STACK_SIZE 272 * TA_PROP_STR_VERSION 273 * TA_PROP_STR_DESCRIPTION 274 * USER_TA_PROP_TYPE_STRING, 275 * TA_DESCRIPTION 276 */ 277 }; 278 279 /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */ 280 const struct tee_props tee_propset_tee[] = { 281 { 282 .name = "gpd.tee.apiversion", 283 .prop_type = USER_TA_PROP_TYPE_STRING, 284 .data = api_vers, 285 .len = sizeof(api_vers), 286 }, 287 { 288 .name = "gpd.tee.description", 289 .prop_type = USER_TA_PROP_TYPE_STRING, 290 .data = descr, .len = sizeof(descr) 291 }, 292 { 293 .name = "gpd.tee.deviceID", 294 .prop_type = USER_TA_PROP_TYPE_UUID, 295 .get_prop_func = get_prop_tee_dev_id 296 }, 297 { 298 .name = "gpd.tee.systemTime.protectionLevel", 299 .prop_type = USER_TA_PROP_TYPE_U32, 300 .get_prop_func = get_prop_tee_sys_time_prot_level 301 }, 302 { 303 .name = "gpd.tee.TAPersistentTime.protectionLevel", 304 .prop_type = USER_TA_PROP_TYPE_U32, 305 .data = &ta_time_prot_lvl, 306 .len = sizeof(ta_time_prot_lvl) 307 }, 308 { 309 .name = "gpd.tee.cryptography.ecc", 310 .prop_type = USER_TA_PROP_TYPE_BOOL, 311 .data = &crypto_ecc_en, 312 .len = sizeof(crypto_ecc_en) 313 }, 314 { 315 .name = "gpd.tee.trustedStorage.antiRollback.protectionLevel", 316 .prop_type = USER_TA_PROP_TYPE_U32, 317 .data = &ts_antiroll_prot_lvl, 318 .len = sizeof(ts_antiroll_prot_lvl) 319 }, 320 { 321 .name = "gpd.tee.trustedos.implementation.version", 322 .prop_type = USER_TA_PROP_TYPE_STRING, 323 .data = trustedos_impl_version, 324 .len = sizeof(trustedos_impl_version) 325 }, 326 { 327 .name = "gpd.tee.trustedos.implementation.binaryversion", 328 .prop_type = USER_TA_PROP_TYPE_U32, 329 .data = &trustedos_impl_bin_version, 330 .len = sizeof(trustedos_impl_bin_version) 331 }, 332 { 333 .name = "gpd.tee.trustedos.manufacturer", 334 .prop_type = USER_TA_PROP_TYPE_STRING, 335 .data = trustedos_manufacturer, 336 .len = sizeof(trustedos_manufacturer) 337 }, 338 { 339 .name = "gpd.tee.firmware.implementation.version", 340 .prop_type = USER_TA_PROP_TYPE_STRING, 341 .data = fw_impl_version, 342 .len = sizeof(fw_impl_version) 343 }, 344 { 345 .name = "gpd.tee.firmware.implementation.binaryversion", 346 .prop_type = USER_TA_PROP_TYPE_U32, 347 .data = &fw_impl_bin_version, 348 .len = sizeof(fw_impl_bin_version) 349 }, 350 { 351 .name = "gpd.tee.firmware.manufacturer", 352 .prop_type = USER_TA_PROP_TYPE_STRING, 353 .data = fw_manufacturer, 354 .len = sizeof(fw_manufacturer) 355 }, 356 #ifdef CFG_TA_BTI 357 { 358 .name = "org.trustedfirmware.optee.cpu.feat_bti_implemented", 359 .prop_type = USER_TA_PROP_TYPE_BOOL, 360 .get_prop_func = get_prop_feat_bti_implemented 361 }, 362 #endif 363 #ifdef CFG_TA_PAUTH 364 { 365 .name = "org.trustedfirmware.optee.cpu.feat_pauth_implemented", 366 .prop_type = USER_TA_PROP_TYPE_BOOL, 367 .get_prop_func = get_prop_feat_pauth_implemented 368 }, 369 #endif 370 #if MEMTAG_IS_ENABLED 371 { 372 .name = "org.trustedfirmware.optee.cpu.feat_memtag_implemented", 373 .prop_type = USER_TA_PROP_TYPE_U32, 374 .get_prop_func = get_prop_feat_memtag_implemented 375 } 376 #endif 377 378 /* 379 * Following properties are processed directly in libutee: 380 * gpd.tee.arith.maxBigIntSize 381 */ 382 }; 383 384 __weak const struct tee_vendor_props vendor_props_client; 385 __weak const struct tee_vendor_props vendor_props_ta; 386 __weak const struct tee_vendor_props vendor_props_tee; 387 388 static void get_prop_set(unsigned long prop_set, 389 const struct tee_props **props, 390 size_t *size, 391 const struct tee_props **vendor_props, 392 size_t *vendor_size) 393 { 394 if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) { 395 *props = tee_propset_client; 396 *size = ARRAY_SIZE(tee_propset_client); 397 *vendor_props = vendor_props_client.props; 398 *vendor_size = vendor_props_client.len; 399 } else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) { 400 *props = tee_propset_ta; 401 *size = ARRAY_SIZE(tee_propset_ta); 402 *vendor_props = vendor_props_ta.props; 403 *vendor_size = vendor_props_ta.len; 404 } else if ((TEE_PropSetHandle)prop_set == 405 TEE_PROPSET_TEE_IMPLEMENTATION) { 406 *props = tee_propset_tee; 407 *size = ARRAY_SIZE(tee_propset_tee); 408 *vendor_props = vendor_props_tee.props; 409 *vendor_size = vendor_props_tee.len; 410 } else { 411 *props = NULL; 412 *size = 0; 413 *vendor_props = NULL; 414 *vendor_size = 0; 415 } 416 } 417 418 static const struct tee_props *get_prop_struct(unsigned long prop_set, 419 unsigned long index) 420 { 421 const struct tee_props *props; 422 const struct tee_props *vendor_props; 423 size_t size; 424 size_t vendor_size; 425 426 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 427 428 if (index < size) 429 return &(props[index]); 430 index -= size; 431 432 if (index < vendor_size) 433 return &(vendor_props[index]); 434 435 return NULL; 436 } 437 438 /* 439 * prop_set is part of TEE_PROPSET_xxx 440 * index is the index in the Property Set to retrieve 441 * if name is not NULL, the name of "index" property is returned 442 * if buf is not NULL, the property is returned 443 */ 444 TEE_Result syscall_get_property(unsigned long prop_set, 445 unsigned long index, 446 void *name, uint32_t *name_len, 447 void *buf, uint32_t *blen, 448 uint32_t *prop_type) 449 { 450 struct ts_session *sess = ts_get_current_session(); 451 TEE_Result res = TEE_SUCCESS; 452 TEE_Result res2 = TEE_SUCCESS; 453 const struct tee_props *prop = NULL; 454 uint32_t klen = 0; 455 size_t klen_size = 0; 456 uint32_t elen = 0; 457 458 prop = get_prop_struct(prop_set, index); 459 if (!prop) 460 return TEE_ERROR_ITEM_NOT_FOUND; 461 462 /* Get the property type */ 463 if (prop_type) { 464 res = copy_to_user(prop_type, &prop->prop_type, 465 sizeof(*prop_type)); 466 if (res != TEE_SUCCESS) 467 return res; 468 } 469 470 /* Get the property */ 471 if (buf && blen) { 472 res = copy_from_user(&klen, blen, sizeof(klen)); 473 if (res != TEE_SUCCESS) 474 return res; 475 476 if (prop->get_prop_func) { 477 klen_size = klen; 478 res = prop->get_prop_func(sess, buf, &klen_size); 479 klen = klen_size; 480 res2 = copy_to_user(blen, &klen, sizeof(*blen)); 481 } else { 482 if (klen < prop->len) 483 res = TEE_ERROR_SHORT_BUFFER; 484 else 485 res = copy_to_user(buf, prop->data, prop->len); 486 res2 = copy_to_user(blen, &prop->len, sizeof(*blen)); 487 } 488 if (res2 != TEE_SUCCESS) 489 return res2; 490 if (res != TEE_SUCCESS) 491 return res; 492 } 493 494 /* Get the property name */ 495 if (name && name_len) { 496 res = copy_from_user(&klen, name_len, sizeof(klen)); 497 if (res != TEE_SUCCESS) 498 return res; 499 500 elen = strlen(prop->name) + 1; 501 502 if (klen < elen) 503 res = TEE_ERROR_SHORT_BUFFER; 504 else 505 res = copy_to_user(name, prop->name, elen); 506 res2 = copy_to_user(name_len, &elen, sizeof(*name_len)); 507 if (res2 != TEE_SUCCESS) 508 return res2; 509 if (res != TEE_SUCCESS) 510 return res; 511 } 512 513 return res; 514 } 515 516 /* 517 * prop_set is part of TEE_PROPSET_xxx 518 */ 519 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set, 520 void *name, 521 unsigned long name_len, 522 uint32_t *index) 523 { 524 TEE_Result res = TEE_SUCCESS; 525 const struct tee_props *props = NULL; 526 size_t size = 0; 527 const struct tee_props *vendor_props = NULL; 528 size_t vendor_size = 0; 529 char *kname = NULL; 530 uint32_t i = 0; 531 532 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 533 if (!props) 534 return TEE_ERROR_ITEM_NOT_FOUND; 535 536 if (!name || !name_len) { 537 res = TEE_ERROR_BAD_PARAMETERS; 538 goto out; 539 } 540 541 kname = malloc(name_len); 542 if (!kname) 543 return TEE_ERROR_OUT_OF_MEMORY; 544 res = copy_from_user(kname, name, name_len); 545 if (res != TEE_SUCCESS) 546 goto out; 547 kname[name_len - 1] = 0; 548 549 res = TEE_ERROR_ITEM_NOT_FOUND; 550 for (i = 0; i < size; i++) { 551 if (!strcmp(kname, props[i].name)) { 552 res = copy_to_user(index, &i, sizeof(*index)); 553 goto out; 554 } 555 } 556 for (i = size; i < size + vendor_size; i++) { 557 if (!strcmp(kname, vendor_props[i - size].name)) { 558 res = copy_to_user(index, &i, sizeof(*index)); 559 goto out; 560 } 561 } 562 563 out: 564 free_wipe(kname); 565 return res; 566 } 567 568 static TEE_Result utee_param_to_param(struct user_ta_ctx *utc, 569 struct tee_ta_param *p, 570 struct utee_params *up) 571 { 572 size_t n = 0; 573 uint32_t types = up->types; 574 575 p->types = types; 576 for (n = 0; n < TEE_NUM_PARAMS; n++) { 577 uintptr_t a = up->vals[n * 2]; 578 size_t b = up->vals[n * 2 + 1]; 579 uint32_t flags = TEE_MEMORY_ACCESS_READ | 580 TEE_MEMORY_ACCESS_ANY_OWNER; 581 582 switch (TEE_PARAM_TYPE_GET(types, n)) { 583 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 584 case TEE_PARAM_TYPE_MEMREF_INOUT: 585 flags |= TEE_MEMORY_ACCESS_WRITE; 586 fallthrough; 587 case TEE_PARAM_TYPE_MEMREF_INPUT: 588 p->u[n].mem.offs = memtag_strip_tag_vaddr((void *)a); 589 p->u[n].mem.size = b; 590 591 if (!p->u[n].mem.offs) { 592 /* Allow NULL memrefs if of size 0 */ 593 if (p->u[n].mem.size) 594 return TEE_ERROR_BAD_PARAMETERS; 595 p->u[n].mem.mobj = NULL; 596 break; 597 } 598 599 p->u[n].mem.mobj = &mobj_virt; 600 601 if (vm_check_access_rights(&utc->uctx, flags, a, b)) 602 return TEE_ERROR_ACCESS_DENIED; 603 break; 604 case TEE_PARAM_TYPE_VALUE_INPUT: 605 case TEE_PARAM_TYPE_VALUE_INOUT: 606 p->u[n].val.a = a; 607 p->u[n].val.b = b; 608 break; 609 default: 610 memset(&p->u[n], 0, sizeof(p->u[n])); 611 break; 612 } 613 } 614 615 return TEE_SUCCESS; 616 } 617 618 static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj, 619 uint8_t **va) 620 { 621 struct mobj *m = NULL; 622 void *v = NULL; 623 624 /* Allocate section in secure DDR */ 625 #ifdef CFG_PAGED_USER_TA 626 m = mobj_seccpy_shm_alloc(size); 627 #else 628 m = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr); 629 #endif 630 if (!m) 631 return TEE_ERROR_GENERIC; 632 633 v = mobj_get_va(*mobj, 0, size); 634 if (!v) { 635 mobj_put(m); 636 return TEE_ERROR_GENERIC; 637 } 638 639 *mobj = m; 640 *va = v; 641 return TEE_SUCCESS; 642 } 643 644 /* 645 * TA invokes some TA with parameter. 646 * If some parameters are memory references: 647 * - either the memref is inside TA private RAM: TA is not allowed to expose 648 * its private RAM: use a temporary memory buffer and copy the data. 649 * - or the memref is not in the TA private RAM: 650 * - if the memref was mapped to the TA, TA is allowed to expose it. 651 * - if so, converts memref virtual address into a physical address. 652 */ 653 static TEE_Result tee_svc_copy_param(struct ts_session *sess, 654 struct ts_session *called_sess, 655 struct utee_params *callee_params, 656 struct tee_ta_param *param, 657 void *tmp_buf_va[TEE_NUM_PARAMS], 658 size_t tmp_buf_size[TEE_NUM_PARAMS], 659 struct mobj **mobj_tmp) 660 { 661 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 662 bool ta_private_memref[TEE_NUM_PARAMS] = { false, }; 663 TEE_Result res = TEE_SUCCESS; 664 size_t dst_offs = 0; 665 size_t req_mem = 0; 666 uint8_t *dst = 0; 667 void *va = NULL; 668 size_t n = 0; 669 size_t s = 0; 670 671 callee_params = memtag_strip_tag(callee_params); 672 673 /* fill 'param' input struct with caller params description buffer */ 674 if (!callee_params) { 675 memset(param, 0, sizeof(*param)); 676 } else { 677 uint32_t flags = TEE_MEMORY_ACCESS_READ | 678 TEE_MEMORY_ACCESS_WRITE | 679 TEE_MEMORY_ACCESS_ANY_OWNER; 680 681 res = vm_check_access_rights(&utc->uctx, flags, 682 (uaddr_t)callee_params, 683 sizeof(struct utee_params)); 684 if (res != TEE_SUCCESS) 685 return res; 686 res = utee_param_to_param(utc, param, callee_params); 687 if (res != TEE_SUCCESS) 688 return res; 689 } 690 691 if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) { 692 /* pseudo TA borrows the mapping of the calling TA */ 693 return TEE_SUCCESS; 694 } 695 696 /* All mobj in param are of type MOJB_TYPE_VIRT */ 697 698 for (n = 0; n < TEE_NUM_PARAMS; n++) { 699 700 ta_private_memref[n] = false; 701 702 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 703 case TEE_PARAM_TYPE_MEMREF_INPUT: 704 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 705 case TEE_PARAM_TYPE_MEMREF_INOUT: 706 va = (void *)param->u[n].mem.offs; 707 s = param->u[n].mem.size; 708 if (!va) { 709 if (s) 710 return TEE_ERROR_BAD_PARAMETERS; 711 break; 712 } 713 /* uTA cannot expose its private memory */ 714 if (vm_buf_is_inside_um_private(&utc->uctx, va, s)) { 715 s = ROUNDUP(s, sizeof(uint32_t)); 716 if (ADD_OVERFLOW(req_mem, s, &req_mem)) 717 return TEE_ERROR_BAD_PARAMETERS; 718 ta_private_memref[n] = true; 719 break; 720 } 721 722 res = vm_buf_to_mboj_offs(&utc->uctx, va, s, 723 ¶m->u[n].mem.mobj, 724 ¶m->u[n].mem.offs); 725 if (res != TEE_SUCCESS) 726 return res; 727 break; 728 default: 729 break; 730 } 731 } 732 733 if (req_mem == 0) 734 return TEE_SUCCESS; 735 736 res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst); 737 if (res != TEE_SUCCESS) 738 return res; 739 dst_offs = 0; 740 741 for (n = 0; n < TEE_NUM_PARAMS; n++) { 742 743 if (!ta_private_memref[n]) 744 continue; 745 746 s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t)); 747 748 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 749 case TEE_PARAM_TYPE_MEMREF_INPUT: 750 case TEE_PARAM_TYPE_MEMREF_INOUT: 751 va = (void *)param->u[n].mem.offs; 752 if (va) { 753 res = copy_from_user(dst, va, 754 param->u[n].mem.size); 755 if (res != TEE_SUCCESS) 756 return res; 757 param->u[n].mem.offs = dst_offs; 758 param->u[n].mem.mobj = *mobj_tmp; 759 tmp_buf_va[n] = dst; 760 tmp_buf_size[n] = param->u[n].mem.size; 761 dst += s; 762 dst_offs += s; 763 } 764 break; 765 766 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 767 va = (void *)param->u[n].mem.offs; 768 if (va) { 769 param->u[n].mem.offs = dst_offs; 770 param->u[n].mem.mobj = *mobj_tmp; 771 tmp_buf_va[n] = dst; 772 tmp_buf_size[n] = param->u[n].mem.size; 773 dst += s; 774 dst_offs += s; 775 } 776 break; 777 778 default: 779 continue; 780 } 781 } 782 783 return TEE_SUCCESS; 784 } 785 786 /* 787 * Back from execution of service: update parameters passed from TA: 788 * If some parameters were memory references: 789 * - either the memref was temporary: copy back data and update size 790 * - or it was the original TA memref: update only the size value. 791 */ 792 static TEE_Result tee_svc_update_out_param( 793 struct tee_ta_param *param, 794 void *tmp_buf_va[TEE_NUM_PARAMS], 795 size_t tmp_buf_size[TEE_NUM_PARAMS], 796 struct utee_params *usr_param) 797 { 798 size_t n; 799 uint64_t *vals = usr_param->vals; 800 size_t sz = 0; 801 802 for (n = 0; n < TEE_NUM_PARAMS; n++) { 803 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 804 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 805 case TEE_PARAM_TYPE_MEMREF_INOUT: 806 /* 807 * Memory copy is only needed if there's a temporary 808 * buffer involved, tmp_buf_va[n] is only update if 809 * a temporary buffer is used. Otherwise only the 810 * size needs to be updated. 811 */ 812 sz = param->u[n].mem.size; 813 if (tmp_buf_va[n] && sz <= vals[n * 2 + 1]) { 814 void *src = tmp_buf_va[n]; 815 void *dst = (void *)(uintptr_t)vals[n * 2]; 816 TEE_Result res = TEE_SUCCESS; 817 818 /* 819 * TA is allowed to return a size larger than 820 * the original size. However, in such cases no 821 * data should be synchronized as per TEE Client 822 * API spec. 823 */ 824 if (sz <= tmp_buf_size[n]) { 825 res = copy_to_user(dst, src, sz); 826 if (res != TEE_SUCCESS) 827 return res; 828 } 829 } 830 usr_param->vals[n * 2 + 1] = sz; 831 break; 832 833 case TEE_PARAM_TYPE_VALUE_OUTPUT: 834 case TEE_PARAM_TYPE_VALUE_INOUT: 835 vals[n * 2] = param->u[n].val.a; 836 vals[n * 2 + 1] = param->u[n].val.b; 837 break; 838 839 default: 840 continue; 841 } 842 } 843 844 return TEE_SUCCESS; 845 } 846 847 /* Called when a TA calls an OpenSession on another TA */ 848 TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 849 unsigned long cancel_req_to, 850 struct utee_params *usr_param, uint32_t *ta_sess, 851 uint32_t *ret_orig) 852 { 853 struct ts_session *sess = ts_get_current_session(); 854 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 855 TEE_Result res = TEE_SUCCESS; 856 uint32_t ret_o = TEE_ORIGIN_TEE; 857 struct tee_ta_session *s = NULL; 858 struct mobj *mobj_param = NULL; 859 TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 860 struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 861 TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 862 void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL }; 863 size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 }; 864 865 if (uuid == NULL || param == NULL || clnt_id == NULL) { 866 res = TEE_ERROR_OUT_OF_MEMORY; 867 goto out_free_only; 868 } 869 870 memset(param, 0, sizeof(struct tee_ta_param)); 871 872 res = copy_from_user_private(uuid, dest, sizeof(TEE_UUID)); 873 if (res != TEE_SUCCESS) 874 goto function_exit; 875 876 clnt_id->login = TEE_LOGIN_TRUSTED_APP; 877 memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 878 879 res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va, 880 tmp_buf_size, &mobj_param); 881 if (res != TEE_SUCCESS) 882 goto function_exit; 883 884 res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 885 clnt_id, cancel_req_to, param); 886 vm_set_ctx(&utc->ta_ctx.ts_ctx); 887 if (res != TEE_SUCCESS) 888 goto function_exit; 889 890 res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size, 891 usr_param); 892 893 function_exit: 894 mobj_put_wipe(mobj_param); 895 if (res == TEE_SUCCESS) 896 copy_to_user_private(ta_sess, &s->id, sizeof(s->id)); 897 copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o)); 898 899 out_free_only: 900 free_wipe(param); 901 free_wipe(uuid); 902 free_wipe(clnt_id); 903 return res; 904 } 905 906 TEE_Result syscall_close_ta_session(unsigned long ta_sess) 907 { 908 struct ts_session *sess = ts_get_current_session(); 909 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 910 TEE_Identity clnt_id = { }; 911 struct tee_ta_session *s = NULL; 912 913 s = tee_ta_find_session(ta_sess, &utc->open_sessions); 914 915 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 916 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 917 918 return tee_ta_close_session(s, &utc->open_sessions, &clnt_id); 919 } 920 921 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 922 unsigned long cancel_req_to, unsigned long cmd_id, 923 struct utee_params *usr_param, uint32_t *ret_orig) 924 { 925 struct ts_session *sess = ts_get_current_session(); 926 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 927 TEE_Result res = TEE_SUCCESS; 928 TEE_Result res2 = TEE_SUCCESS; 929 uint32_t ret_o = TEE_ORIGIN_TEE; 930 struct tee_ta_param param = { 0 }; 931 TEE_Identity clnt_id = { }; 932 struct tee_ta_session *called_sess = NULL; 933 struct mobj *mobj_param = NULL; 934 void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL }; 935 size_t tmp_buf_size[TEE_NUM_PARAMS] = { }; 936 937 called_sess = tee_ta_get_session((uint32_t)ta_sess, true, 938 &utc->open_sessions); 939 if (!called_sess) 940 return TEE_ERROR_BAD_PARAMETERS; 941 942 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 943 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 944 945 res = tee_svc_copy_param(sess, &called_sess->ts_sess, usr_param, ¶m, 946 tmp_buf_va, tmp_buf_size, &mobj_param); 947 if (res != TEE_SUCCESS) 948 goto function_exit; 949 950 res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 951 cancel_req_to, cmd_id, ¶m); 952 if (res == TEE_ERROR_TARGET_DEAD) 953 goto function_exit; 954 955 res2 = tee_svc_update_out_param(¶m, tmp_buf_va, tmp_buf_size, 956 usr_param); 957 if (res2 != TEE_SUCCESS) { 958 /* 959 * Spec for TEE_InvokeTACommand() says: 960 * "If the return origin is different from 961 * TEE_ORIGIN_TRUSTED_APP, then the function has failed 962 * before it could reach the destination Trusted 963 * Application." 964 * 965 * But if we can't update params to the caller we have no 966 * choice we need to return some error to indicate that 967 * parameters aren't updated as expected. 968 */ 969 ret_o = TEE_ORIGIN_TEE; 970 res = res2; 971 } 972 973 function_exit: 974 tee_ta_put_session(called_sess); 975 mobj_put_wipe(mobj_param); 976 copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o)); 977 return res; 978 } 979 980 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 981 size_t len) 982 { 983 struct ts_session *s = ts_get_current_session(); 984 985 return vm_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags, 986 memtag_strip_tag_vaddr(buf), len); 987 } 988 989 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 990 { 991 struct ts_session *s = ts_get_current_session(); 992 uint32_t c = 0; 993 994 c = tee_ta_session_is_cancelled(to_ta_session(s), NULL); 995 996 return copy_to_user(cancel, &c, sizeof(c)); 997 } 998 999 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 1000 { 1001 struct ts_session *s = ts_get_current_session(); 1002 struct tee_ta_session *sess = NULL; 1003 uint32_t m = 0; 1004 1005 sess = to_ta_session(s); 1006 m = sess->cancel_mask; 1007 sess->cancel_mask = false; 1008 return copy_to_user(old_mask, &m, sizeof(m)); 1009 } 1010 1011 TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 1012 { 1013 struct ts_session *s = ts_get_current_session(); 1014 struct tee_ta_session *sess = NULL; 1015 uint32_t m = 0; 1016 1017 sess = to_ta_session(s); 1018 m = sess->cancel_mask; 1019 sess->cancel_mask = true; 1020 return copy_to_user(old_mask, &m, sizeof(m)); 1021 } 1022 1023 TEE_Result syscall_wait(unsigned long timeout) 1024 { 1025 struct ts_session *s = ts_get_current_session(); 1026 TEE_Result res = TEE_SUCCESS; 1027 uint32_t mytime = 0; 1028 TEE_Time base_time = { }; 1029 TEE_Time current_time = { }; 1030 1031 res = tee_time_get_sys_time(&base_time); 1032 if (res != TEE_SUCCESS) 1033 return res; 1034 1035 while (true) { 1036 res = tee_time_get_sys_time(¤t_time); 1037 if (res != TEE_SUCCESS) 1038 return res; 1039 1040 if (tee_ta_session_is_cancelled(to_ta_session(s), 1041 ¤t_time)) 1042 return TEE_ERROR_CANCEL; 1043 1044 mytime = (current_time.seconds - base_time.seconds) * 1000 + 1045 (int)current_time.millis - (int)base_time.millis; 1046 if (mytime >= timeout) 1047 return TEE_SUCCESS; 1048 1049 tee_time_wait(timeout - mytime); 1050 } 1051 1052 return res; 1053 } 1054 1055 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 1056 { 1057 struct ts_session *s = ts_get_current_session(); 1058 TEE_Result res = TEE_SUCCESS; 1059 TEE_Result res2 = TEE_SUCCESS; 1060 TEE_Time t = { }; 1061 1062 switch (cat) { 1063 case UTEE_TIME_CAT_SYSTEM: 1064 res = tee_time_get_sys_time(&t); 1065 break; 1066 case UTEE_TIME_CAT_TA_PERSISTENT: 1067 res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 1068 break; 1069 case UTEE_TIME_CAT_REE: 1070 res = tee_time_get_ree_time(&t); 1071 break; 1072 default: 1073 res = TEE_ERROR_BAD_PARAMETERS; 1074 break; 1075 } 1076 1077 if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 1078 res2 = copy_to_user_private(mytime, &t, sizeof(t)); 1079 if (res2 != TEE_SUCCESS) 1080 res = res2; 1081 } 1082 1083 return res; 1084 } 1085 1086 TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 1087 { 1088 struct ts_session *s = ts_get_current_session(); 1089 TEE_Result res = TEE_SUCCESS; 1090 TEE_Time t = { }; 1091 1092 res = copy_from_user_private(&t, mytime, sizeof(t)); 1093 if (res != TEE_SUCCESS) 1094 return res; 1095 1096 return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 1097 } 1098