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