1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #include <util.h> 29 #include <kernel/tee_common_otp.h> 30 #include <kernel/tee_common.h> 31 #include <tee_api_types.h> 32 #include <kernel/tee_ta_manager.h> 33 #include <utee_types.h> 34 #include <tee/tee_svc.h> 35 #include <tee/tee_cryp_utl.h> 36 #include <mm/tee_mmu.h> 37 #include <mm/tee_mm.h> 38 #include <mm/core_memprot.h> 39 #include <kernel/tee_time.h> 40 41 #include <user_ta_header.h> 42 #include <trace.h> 43 #include <kernel/trace_ta.h> 44 #include <kernel/chip_services.h> 45 #include <kernel/pseudo_ta.h> 46 #include <mm/mobj.h> 47 48 vaddr_t tee_svc_uref_base; 49 50 void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused) 51 { 52 #ifdef CFG_TEE_CORE_TA_TRACE 53 char *kbuf; 54 55 if (len == 0) 56 return; 57 58 kbuf = malloc(len + 1); 59 if (kbuf == NULL) 60 return; 61 62 if (tee_svc_copy_from_user(kbuf, buf, len) == TEE_SUCCESS) { 63 kbuf[len] = '\0'; 64 trace_ext_puts(kbuf); 65 } 66 67 free(kbuf); 68 #endif 69 } 70 71 TEE_Result syscall_not_supported(void) 72 { 73 return TEE_ERROR_NOT_SUPPORTED; 74 } 75 76 /* Configuration properties */ 77 /* API implementation version */ 78 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION); 79 80 /* Implementation description (implementation-dependent) */ 81 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR); 82 83 /* 84 * TA persistent time protection level 85 * 100: Persistent time based on an REE-controlled real-time clock 86 * and on the TEE Trusted Storage for the storage of origins (default). 87 * 1000: Persistent time based on a TEE-controlled real-time clock 88 * and the TEE Trusted Storage. 89 * The real-time clock MUST be out of reach of software attacks 90 * from the REE. 91 */ 92 static const uint32_t ta_time_prot_lvl = 100; 93 94 /* Elliptic Curve Cryptographic support */ 95 #ifdef CFG_CRYPTO_ECC 96 static const bool crypto_ecc_en = 1; 97 #else 98 static const bool crypto_ecc_en; 99 #endif 100 101 /* 102 * Trusted storage anti rollback protection level 103 * 0 (or missing): No antirollback protection (default) 104 * 100: Antirollback enforced at REE level 105 * 1000: Antirollback TEE-controlled hardware 106 */ 107 #ifdef CFG_RPMB_FS 108 static const uint32_t ts_antiroll_prot_lvl = 1000; 109 #else 110 static const uint32_t ts_antiroll_prot_lvl; 111 #endif 112 113 /* Trusted OS implementation version */ 114 static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION); 115 116 /* Trusted OS implementation version (binary value) */ 117 static const uint32_t trustedos_impl_bin_version; /* 0 by default */ 118 119 /* Trusted OS implementation manufacturer name */ 120 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER); 121 122 /* Trusted firmware version */ 123 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION); 124 125 /* Trusted firmware version (binary value) */ 126 static const uint32_t fw_impl_bin_version; /* 0 by default */ 127 128 /* Trusted firmware manufacturer name */ 129 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER); 130 131 static TEE_Result get_prop_tee_dev_id(struct tee_ta_session *sess __unused, 132 void *buf, size_t *blen) 133 { 134 TEE_Result res; 135 TEE_UUID uuid; 136 const size_t nslen = 5; 137 uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 138 'O', 'P', 'T', 'E', 'E' }; 139 140 if (*blen < sizeof(uuid)) { 141 *blen = sizeof(uuid); 142 return TEE_ERROR_SHORT_BUFFER; 143 } 144 *blen = sizeof(uuid); 145 146 if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen)) 147 return TEE_ERROR_BAD_STATE; 148 149 res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data), 150 (uint8_t *)&uuid, sizeof(uuid)); 151 if (res != TEE_SUCCESS) 152 return TEE_ERROR_BAD_STATE; 153 154 /* 155 * Changes the random value into and UUID as specifiec 156 * in RFC 4122. The magic values are from the example 157 * code in the RFC. 158 * 159 * TEE_UUID is defined slightly different from the RFC, 160 * but close enough for our purpose. 161 */ 162 163 uuid.timeHiAndVersion &= 0x0fff; 164 uuid.timeHiAndVersion |= 5 << 12; 165 166 /* uuid.clock_seq_hi_and_reserved in the RFC */ 167 uuid.clockSeqAndNode[0] &= 0x3f; 168 uuid.clockSeqAndNode[0] |= 0x80; 169 170 return tee_svc_copy_to_user(buf, &uuid, sizeof(TEE_UUID)); 171 } 172 173 static TEE_Result get_prop_tee_sys_time_prot_level( 174 struct tee_ta_session *sess __unused, 175 void *buf, size_t *blen) 176 { 177 uint32_t prot; 178 179 if (*blen < sizeof(prot)) { 180 *blen = sizeof(prot); 181 return TEE_ERROR_SHORT_BUFFER; 182 } 183 *blen = sizeof(prot); 184 prot = tee_time_get_sys_time_protection_level(); 185 return tee_svc_copy_to_user(buf, &prot, sizeof(prot)); 186 } 187 188 static TEE_Result get_prop_client_id(struct tee_ta_session *sess __unused, 189 void *buf, size_t *blen) 190 { 191 if (*blen < sizeof(TEE_Identity)) { 192 *blen = sizeof(TEE_Identity); 193 return TEE_ERROR_SHORT_BUFFER; 194 } 195 *blen = sizeof(TEE_Identity); 196 return tee_svc_copy_to_user(buf, &sess->clnt_id, sizeof(TEE_Identity)); 197 } 198 199 static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess, 200 void *buf, size_t *blen) 201 { 202 if (*blen < sizeof(TEE_UUID)) { 203 *blen = sizeof(TEE_UUID); 204 return TEE_ERROR_SHORT_BUFFER; 205 } 206 *blen = sizeof(TEE_UUID); 207 return tee_svc_copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID)); 208 } 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 319 /* 320 * Following properties are processed directly in libutee: 321 * gpd.tee.arith.maxBigIntSize 322 */ 323 }; 324 325 __weak const struct tee_vendor_props vendor_props_client; 326 __weak const struct tee_vendor_props vendor_props_ta; 327 __weak const struct tee_vendor_props vendor_props_tee; 328 329 static void get_prop_set(unsigned long prop_set, 330 const struct tee_props **props, 331 size_t *size, 332 const struct tee_props **vendor_props, 333 size_t *vendor_size) 334 { 335 if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) { 336 *props = tee_propset_client; 337 *size = ARRAY_SIZE(tee_propset_client); 338 *vendor_props = vendor_props_client.props; 339 *vendor_size = vendor_props_client.len; 340 } else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) { 341 *props = tee_propset_ta; 342 *size = ARRAY_SIZE(tee_propset_ta); 343 *vendor_props = vendor_props_ta.props; 344 *vendor_size = vendor_props_ta.len; 345 } else if ((TEE_PropSetHandle)prop_set == 346 TEE_PROPSET_TEE_IMPLEMENTATION) { 347 *props = tee_propset_tee; 348 *size = ARRAY_SIZE(tee_propset_tee); 349 *vendor_props = vendor_props_tee.props; 350 *vendor_size = vendor_props_tee.len; 351 } else { 352 *props = NULL; 353 *size = 0; 354 *vendor_props = NULL; 355 *vendor_size = 0; 356 } 357 } 358 359 static const struct tee_props *get_prop_struct(unsigned long prop_set, 360 unsigned long index) 361 { 362 const struct tee_props *props; 363 const struct tee_props *vendor_props; 364 size_t size; 365 size_t vendor_size; 366 367 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 368 369 if (index < size) 370 return &(props[index]); 371 index -= size; 372 373 if (index < vendor_size) 374 return &(vendor_props[index]); 375 376 return NULL; 377 } 378 379 /* 380 * prop_set is part of TEE_PROPSET_xxx 381 * index is the index in the Property Set to retrieve 382 * if name is not NULL, the name of "index" property is returned 383 * if buf is not NULL, the property is returned 384 */ 385 TEE_Result syscall_get_property(unsigned long prop_set, 386 unsigned long index, 387 void *name, uint32_t *name_len, 388 void *buf, uint32_t *blen, 389 uint32_t *prop_type) 390 { 391 struct tee_ta_session *sess; 392 TEE_Result res; 393 TEE_Result res2; 394 const struct tee_props *prop; 395 uint32_t klen; 396 size_t klen_size; 397 uint32_t elen; 398 399 prop = get_prop_struct(prop_set, index); 400 if (!prop) 401 return TEE_ERROR_ITEM_NOT_FOUND; 402 403 res = tee_ta_get_current_session(&sess); 404 if (res != TEE_SUCCESS) 405 return res; 406 407 /* Get the property type */ 408 if (prop_type) { 409 res = tee_svc_copy_to_user(prop_type, &prop->prop_type, 410 sizeof(*prop_type)); 411 if (res != TEE_SUCCESS) 412 return res; 413 } 414 415 /* Get the property */ 416 if (buf && blen) { 417 res = tee_svc_copy_from_user(&klen, blen, sizeof(klen)); 418 if (res != TEE_SUCCESS) 419 return res; 420 421 if (prop->get_prop_func) { 422 klen_size = klen; 423 res = prop->get_prop_func(sess, buf, &klen_size); 424 klen = klen_size; 425 res2 = tee_svc_copy_to_user(blen, &klen, sizeof(*blen)); 426 } else { 427 if (klen < prop->len) 428 res = TEE_ERROR_SHORT_BUFFER; 429 else 430 res = tee_svc_copy_to_user(buf, prop->data, 431 prop->len); 432 res2 = tee_svc_copy_to_user(blen, &prop->len, 433 sizeof(*blen)); 434 } 435 if (res2 != TEE_SUCCESS) 436 return res2; 437 if (res != TEE_SUCCESS) 438 return res; 439 } 440 441 /* Get the property name */ 442 if (name && name_len) { 443 res = tee_svc_copy_from_user(&klen, name_len, sizeof(klen)); 444 if (res != TEE_SUCCESS) 445 return res; 446 447 elen = strlen(prop->name) + 1; 448 449 if (klen < elen) 450 res = TEE_ERROR_SHORT_BUFFER; 451 else 452 res = tee_svc_copy_to_user(name, prop->name, elen); 453 res2 = tee_svc_copy_to_user(name_len, &elen, sizeof(*name_len)); 454 if (res2 != TEE_SUCCESS) 455 return res2; 456 if (res != TEE_SUCCESS) 457 return res; 458 } 459 460 return res; 461 } 462 463 /* 464 * prop_set is part of TEE_PROPSET_xxx 465 */ 466 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set, 467 void *name, 468 unsigned long name_len, 469 uint32_t *index) 470 { 471 TEE_Result res; 472 struct tee_ta_session *sess; 473 const struct tee_props *props; 474 size_t size; 475 const struct tee_props *vendor_props; 476 size_t vendor_size; 477 char *kname = 0; 478 uint32_t i; 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 res = tee_ta_get_current_session(&sess); 485 if (res != TEE_SUCCESS) 486 goto out; 487 488 if (!name || !name_len) { 489 res = TEE_ERROR_BAD_PARAMETERS; 490 goto out; 491 } 492 493 kname = malloc(name_len); 494 if (!kname) 495 return TEE_ERROR_OUT_OF_MEMORY; 496 res = tee_svc_copy_from_user(kname, name, name_len); 497 if (res != TEE_SUCCESS) 498 goto out; 499 kname[name_len - 1] = 0; 500 501 res = TEE_ERROR_ITEM_NOT_FOUND; 502 for (i = 0; i < size; i++) { 503 if (!strcmp(kname, props[i].name)) { 504 res = tee_svc_copy_to_user(index, &i, sizeof(*index)); 505 goto out; 506 } 507 } 508 for (i = size; i < size + vendor_size; i++) { 509 if (!strcmp(kname, vendor_props[i - size].name)) { 510 res = tee_svc_copy_to_user(index, &i, sizeof(*index)); 511 goto out; 512 } 513 } 514 515 out: 516 free(kname); 517 return res; 518 } 519 520 static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up) 521 { 522 size_t n; 523 uint32_t types = up->types; 524 525 p->types = types; 526 for (n = 0; n < TEE_NUM_PARAMS; n++) { 527 uintptr_t a = up->vals[n * 2]; 528 size_t b = up->vals[n * 2 + 1]; 529 530 switch (TEE_PARAM_TYPE_GET(types, n)) { 531 case TEE_PARAM_TYPE_MEMREF_INPUT: 532 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 533 case TEE_PARAM_TYPE_MEMREF_INOUT: 534 p->u[n].mem.mobj = &mobj_virt; 535 p->u[n].mem.offs = a; 536 p->u[n].mem.size = b; 537 break; 538 case TEE_PARAM_TYPE_VALUE_INPUT: 539 case TEE_PARAM_TYPE_VALUE_INOUT: 540 p->u[n].val.a = a; 541 p->u[n].val.b = b; 542 break; 543 default: 544 memset(&p->u[n], 0, sizeof(p->u[n])); 545 break; 546 } 547 } 548 } 549 550 static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj, 551 uint8_t **va) 552 { 553 /* Allocate section in secure DDR */ 554 #ifdef CFG_PAGED_USER_TA 555 *mobj = mobj_seccpy_shm_alloc(size); 556 #else 557 *mobj = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr); 558 #endif 559 if (!*mobj) 560 return TEE_ERROR_GENERIC; 561 562 *va = mobj_get_va(*mobj, 0); 563 return TEE_SUCCESS; 564 } 565 566 /* 567 * TA invokes some TA with parameter. 568 * If some parameters are memory references: 569 * - either the memref is inside TA private RAM: TA is not allowed to expose 570 * its private RAM: use a temporary memory buffer and copy the data. 571 * - or the memref is not in the TA private RAM: 572 * - if the memref was mapped to the TA, TA is allowed to expose it. 573 * - if so, converts memref virtual address into a physical address. 574 */ 575 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 576 struct tee_ta_session *called_sess, 577 struct utee_params *callee_params, 578 struct tee_ta_param *param, 579 void *tmp_buf_va[TEE_NUM_PARAMS], 580 struct mobj **mobj_tmp) 581 { 582 size_t n; 583 TEE_Result res; 584 size_t req_mem = 0; 585 size_t s; 586 uint8_t *dst = 0; 587 bool ta_private_memref[TEE_NUM_PARAMS]; 588 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 589 void *va; 590 size_t dst_offs; 591 592 /* fill 'param' input struct with caller params description buffer */ 593 if (!callee_params) { 594 memset(param, 0, sizeof(*param)); 595 } else { 596 res = tee_mmu_check_access_rights(utc, 597 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 598 (uaddr_t)callee_params, sizeof(struct utee_params)); 599 if (res != TEE_SUCCESS) 600 return res; 601 utee_param_to_param(param, callee_params); 602 } 603 604 if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) { 605 /* 606 * static TA, borrow the mapping of the calling 607 * during this call. 608 */ 609 return TEE_SUCCESS; 610 } 611 612 /* All mobj in param are of type MOJB_TYPE_VIRT */ 613 614 for (n = 0; n < TEE_NUM_PARAMS; n++) { 615 616 ta_private_memref[n] = false; 617 618 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 619 case TEE_PARAM_TYPE_MEMREF_INPUT: 620 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 621 case TEE_PARAM_TYPE_MEMREF_INOUT: 622 va = (void *)param->u[n].mem.offs; 623 s = param->u[n].mem.size; 624 if (!va) { 625 if (!s) 626 return TEE_ERROR_BAD_PARAMETERS; 627 break; 628 } 629 /* uTA cannot expose its private memory */ 630 if (tee_mmu_is_vbuf_inside_ta_private(utc, va, s)) { 631 632 s = ROUNDUP(s, sizeof(uint32_t)); 633 if (ADD_OVERFLOW(req_mem, s, &req_mem)) 634 return TEE_ERROR_BAD_PARAMETERS; 635 ta_private_memref[n] = true; 636 break; 637 } 638 639 res = tee_mmu_vbuf_to_mobj_offs(utc, va, s, 640 ¶m->u[n].mem.mobj, 641 ¶m->u[n].mem.offs); 642 if (res != TEE_SUCCESS) 643 return res; 644 break; 645 default: 646 break; 647 } 648 } 649 650 if (req_mem == 0) 651 return TEE_SUCCESS; 652 653 res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst); 654 if (res != TEE_SUCCESS) 655 return res; 656 dst_offs = 0; 657 658 for (n = 0; n < TEE_NUM_PARAMS; n++) { 659 660 if (!ta_private_memref[n]) 661 continue; 662 663 s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t)); 664 665 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 666 case TEE_PARAM_TYPE_MEMREF_INPUT: 667 case TEE_PARAM_TYPE_MEMREF_INOUT: 668 va = (void *)param->u[n].mem.offs; 669 if (va) { 670 res = tee_svc_copy_from_user(dst, va, 671 param->u[n].mem.size); 672 if (res != TEE_SUCCESS) 673 return res; 674 param->u[n].mem.offs = dst_offs; 675 param->u[n].mem.mobj = *mobj_tmp; 676 tmp_buf_va[n] = dst; 677 dst += s; 678 dst_offs += s; 679 } 680 break; 681 682 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 683 va = (void *)param->u[n].mem.offs; 684 if (va) { 685 param->u[n].mem.offs = dst_offs; 686 param->u[n].mem.mobj = *mobj_tmp; 687 tmp_buf_va[n] = dst; 688 dst += s; 689 dst_offs += s; 690 } 691 break; 692 693 default: 694 continue; 695 } 696 } 697 698 return TEE_SUCCESS; 699 } 700 701 /* 702 * Back from execution of service: update parameters passed from TA: 703 * If some parameters were memory references: 704 * - either the memref was temporary: copy back data and update size 705 * - or it was the original TA memref: update only the size value. 706 */ 707 static TEE_Result tee_svc_update_out_param( 708 struct tee_ta_session *sess, 709 struct tee_ta_session *called_sess, 710 struct tee_ta_param *param, 711 void *tmp_buf_va[TEE_NUM_PARAMS], 712 struct utee_params *usr_param) 713 { 714 size_t n; 715 void *p; 716 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 717 bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx); 718 719 for (n = 0; n < TEE_NUM_PARAMS; n++) { 720 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 721 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 722 case TEE_PARAM_TYPE_MEMREF_INOUT: 723 p = (void *)(uintptr_t)usr_param->vals[n * 2]; 724 725 /* outside TA private => memref is valid, update size */ 726 if (!tee_mmu_is_vbuf_inside_ta_private(utc, p, 727 param->u[n].mem.size)) { 728 usr_param->vals[n * 2 + 1] = 729 param->u[n].mem.size; 730 break; 731 } 732 733 /* 734 * If we called a kernel TA the parameters are in shared 735 * memory and no copy is needed. 736 */ 737 if (have_private_mem_map && 738 param->u[n].mem.size <= 739 usr_param->vals[n * 2 + 1]) { 740 uint8_t *src = tmp_buf_va[n]; 741 TEE_Result res; 742 743 res = tee_svc_copy_to_user(p, src, 744 param->u[n].mem.size); 745 if (res != TEE_SUCCESS) 746 return res; 747 748 } 749 usr_param->vals[n * 2 + 1] = param->u[n].mem.size; 750 break; 751 752 case TEE_PARAM_TYPE_VALUE_OUTPUT: 753 case TEE_PARAM_TYPE_VALUE_INOUT: 754 usr_param->vals[n * 2] = param->u[n].val.a; 755 usr_param->vals[n * 2 + 1] = param->u[n].val.b; 756 break; 757 758 default: 759 continue; 760 } 761 } 762 763 return TEE_SUCCESS; 764 } 765 766 /* Called when a TA calls an OpenSession on another TA */ 767 TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 768 unsigned long cancel_req_to, 769 struct utee_params *usr_param, uint32_t *ta_sess, 770 uint32_t *ret_orig) 771 { 772 TEE_Result res; 773 uint32_t ret_o = TEE_ORIGIN_TEE; 774 struct tee_ta_session *s = NULL; 775 struct tee_ta_session *sess; 776 struct mobj *mobj_param = NULL; 777 TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 778 struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 779 TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 780 void *tmp_buf_va[TEE_NUM_PARAMS]; 781 struct user_ta_ctx *utc; 782 783 if (uuid == NULL || param == NULL || clnt_id == NULL) { 784 res = TEE_ERROR_OUT_OF_MEMORY; 785 goto out_free_only; 786 } 787 788 memset(param, 0, sizeof(struct tee_ta_param)); 789 790 res = tee_ta_get_current_session(&sess); 791 if (res != TEE_SUCCESS) 792 goto out_free_only; 793 utc = to_user_ta_ctx(sess->ctx); 794 795 res = tee_svc_copy_from_user(uuid, dest, sizeof(TEE_UUID)); 796 if (res != TEE_SUCCESS) 797 goto function_exit; 798 799 clnt_id->login = TEE_LOGIN_TRUSTED_APP; 800 memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 801 802 res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va, 803 &mobj_param); 804 if (res != TEE_SUCCESS) 805 goto function_exit; 806 807 /* 808 * Find session of a multi session TA or a static TA 809 * In such a case, there is no need to ask the supplicant for the TA 810 * code 811 */ 812 res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 813 clnt_id, cancel_req_to, param); 814 if (res != TEE_SUCCESS) 815 goto function_exit; 816 817 res = tee_svc_update_out_param(sess, s, param, tmp_buf_va, usr_param); 818 819 function_exit: 820 mobj_free(mobj_param); 821 if (res == TEE_SUCCESS) 822 tee_svc_copy_kaddr_to_uref(ta_sess, s); 823 tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o)); 824 825 out_free_only: 826 free(param); 827 free(uuid); 828 free(clnt_id); 829 return res; 830 } 831 832 TEE_Result syscall_close_ta_session(unsigned long ta_sess) 833 { 834 TEE_Result res; 835 struct tee_ta_session *sess; 836 TEE_Identity clnt_id; 837 struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess); 838 struct user_ta_ctx *utc; 839 840 res = tee_ta_get_current_session(&sess); 841 if (res != TEE_SUCCESS) 842 return res; 843 utc = to_user_ta_ctx(sess->ctx); 844 845 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 846 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 847 848 return tee_ta_close_session(s, &utc->open_sessions, &clnt_id); 849 } 850 851 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 852 unsigned long cancel_req_to, unsigned long cmd_id, 853 struct utee_params *usr_param, uint32_t *ret_orig) 854 { 855 TEE_Result res; 856 TEE_Result res2; 857 uint32_t ret_o = TEE_ORIGIN_TEE; 858 struct tee_ta_param param = { 0 }; 859 TEE_Identity clnt_id; 860 struct tee_ta_session *sess; 861 struct tee_ta_session *called_sess; 862 struct mobj *mobj_param = NULL; 863 void *tmp_buf_va[TEE_NUM_PARAMS]; 864 struct user_ta_ctx *utc; 865 866 res = tee_ta_get_current_session(&sess); 867 if (res != TEE_SUCCESS) 868 return res; 869 utc = to_user_ta_ctx(sess->ctx); 870 871 called_sess = tee_ta_get_session( 872 (vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true, 873 &utc->open_sessions); 874 if (!called_sess) 875 return TEE_ERROR_BAD_PARAMETERS; 876 877 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 878 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 879 880 res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 881 tmp_buf_va, &mobj_param); 882 if (res != TEE_SUCCESS) 883 goto function_exit; 884 885 res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 886 cancel_req_to, cmd_id, ¶m); 887 888 res2 = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_va, 889 usr_param); 890 if (res2 != TEE_SUCCESS) { 891 /* 892 * Spec for TEE_InvokeTACommand() says: 893 * "If the return origin is different from 894 * TEE_ORIGIN_TRUSTED_APP, then the function has failed 895 * before it could reach the destination Trusted 896 * Application." 897 * 898 * But if we can't update params to the caller we have no 899 * choice we need to return some error to indicate that 900 * parameters aren't updated as expected. 901 */ 902 ret_o = TEE_ORIGIN_TEE; 903 res = res2; 904 } 905 906 function_exit: 907 tee_ta_put_session(called_sess); 908 mobj_free(mobj_param); 909 if (ret_orig) 910 tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o)); 911 return res; 912 } 913 914 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 915 size_t len) 916 { 917 TEE_Result res; 918 struct tee_ta_session *s; 919 920 res = tee_ta_get_current_session(&s); 921 if (res != TEE_SUCCESS) 922 return res; 923 924 return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags, 925 (uaddr_t)buf, len); 926 } 927 928 TEE_Result tee_svc_copy_from_user(void *kaddr, const void *uaddr, size_t len) 929 { 930 TEE_Result res; 931 struct tee_ta_session *s; 932 933 res = tee_ta_get_current_session(&s); 934 if (res != TEE_SUCCESS) 935 return res; 936 937 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 938 TEE_MEMORY_ACCESS_READ | 939 TEE_MEMORY_ACCESS_ANY_OWNER, 940 (uaddr_t)uaddr, len); 941 if (res != TEE_SUCCESS) 942 return res; 943 944 memcpy(kaddr, uaddr, len); 945 return TEE_SUCCESS; 946 } 947 948 TEE_Result tee_svc_copy_to_user(void *uaddr, const void *kaddr, size_t len) 949 { 950 TEE_Result res; 951 struct tee_ta_session *s; 952 953 res = tee_ta_get_current_session(&s); 954 if (res != TEE_SUCCESS) 955 return res; 956 957 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 958 TEE_MEMORY_ACCESS_WRITE | 959 TEE_MEMORY_ACCESS_ANY_OWNER, 960 (uaddr_t)uaddr, len); 961 if (res != TEE_SUCCESS) 962 return res; 963 964 memcpy(uaddr, kaddr, len); 965 return TEE_SUCCESS; 966 } 967 968 TEE_Result tee_svc_copy_kaddr_to_uref(uint32_t *uref, void *kaddr) 969 { 970 uint32_t ref = tee_svc_kaddr_to_uref(kaddr); 971 972 return tee_svc_copy_to_user(uref, &ref, sizeof(ref)); 973 } 974 975 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 976 { 977 TEE_Result res; 978 struct tee_ta_session *s = NULL; 979 uint32_t c; 980 981 res = tee_ta_get_current_session(&s); 982 if (res != TEE_SUCCESS) 983 return res; 984 985 c = tee_ta_session_is_cancelled(s, NULL); 986 987 return tee_svc_copy_to_user(cancel, &c, sizeof(c)); 988 } 989 990 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 991 { 992 TEE_Result res; 993 struct tee_ta_session *s = NULL; 994 uint32_t m; 995 996 res = tee_ta_get_current_session(&s); 997 if (res != TEE_SUCCESS) 998 return res; 999 1000 m = s->cancel_mask; 1001 s->cancel_mask = false; 1002 return tee_svc_copy_to_user(old_mask, &m, sizeof(m)); 1003 } 1004 1005 TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 1006 { 1007 TEE_Result res; 1008 struct tee_ta_session *s = NULL; 1009 uint32_t m; 1010 1011 res = tee_ta_get_current_session(&s); 1012 if (res != TEE_SUCCESS) 1013 return res; 1014 1015 m = s->cancel_mask; 1016 s->cancel_mask = true; 1017 return tee_svc_copy_to_user(old_mask, &m, sizeof(m)); 1018 } 1019 1020 TEE_Result syscall_wait(unsigned long timeout) 1021 { 1022 TEE_Result res = TEE_SUCCESS; 1023 uint32_t mytime = 0; 1024 struct tee_ta_session *s; 1025 TEE_Time base_time; 1026 TEE_Time current_time; 1027 1028 res = tee_ta_get_current_session(&s); 1029 if (res != TEE_SUCCESS) 1030 return res; 1031 1032 res = tee_time_get_sys_time(&base_time); 1033 if (res != TEE_SUCCESS) 1034 return res; 1035 1036 while (true) { 1037 res = tee_time_get_sys_time(¤t_time); 1038 if (res != TEE_SUCCESS) 1039 return res; 1040 1041 if (tee_ta_session_is_cancelled(s, ¤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 TEE_Result res, res2; 1058 struct tee_ta_session *s = NULL; 1059 TEE_Time t; 1060 1061 res = tee_ta_get_current_session(&s); 1062 if (res != TEE_SUCCESS) 1063 return res; 1064 1065 switch (cat) { 1066 case UTEE_TIME_CAT_SYSTEM: 1067 res = tee_time_get_sys_time(&t); 1068 break; 1069 case UTEE_TIME_CAT_TA_PERSISTENT: 1070 res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 1071 break; 1072 case UTEE_TIME_CAT_REE: 1073 res = tee_time_get_ree_time(&t); 1074 break; 1075 default: 1076 res = TEE_ERROR_BAD_PARAMETERS; 1077 break; 1078 } 1079 1080 if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 1081 res2 = tee_svc_copy_to_user(mytime, &t, sizeof(t)); 1082 if (res2 != TEE_SUCCESS) 1083 res = res2; 1084 } 1085 1086 return res; 1087 } 1088 1089 TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 1090 { 1091 TEE_Result res; 1092 struct tee_ta_session *s = NULL; 1093 TEE_Time t; 1094 1095 res = tee_ta_get_current_session(&s); 1096 if (res != TEE_SUCCESS) 1097 return res; 1098 1099 res = tee_svc_copy_from_user(&t, mytime, sizeof(t)); 1100 if (res != TEE_SUCCESS) 1101 return res; 1102 1103 return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 1104 } 1105