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