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 called_sess->calling_sess = sess; 591 return TEE_SUCCESS; 592 } 593 594 for (n = 0; n < TEE_NUM_PARAMS; n++) { 595 596 ta_private_memref[n] = false; 597 598 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 599 case TEE_PARAM_TYPE_MEMREF_INPUT: 600 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 601 case TEE_PARAM_TYPE_MEMREF_INOUT: 602 if (param->params[n].memref.buffer == NULL) { 603 if (param->params[n].memref.size != 0) 604 return TEE_ERROR_BAD_PARAMETERS; 605 break; 606 } 607 /* uTA cannot expose its private memory */ 608 if (tee_mmu_is_vbuf_inside_ta_private(utc, 609 param->params[n].memref.buffer, 610 param->params[n].memref.size)) { 611 612 s = ROUNDUP(param->params[n].memref.size, 613 sizeof(uint32_t)); 614 /* Check overflow */ 615 if (req_mem + s < req_mem) 616 return TEE_ERROR_BAD_PARAMETERS; 617 req_mem += s; 618 ta_private_memref[n] = true; 619 break; 620 } 621 if (tee_mmu_is_vbuf_intersect_ta_private(utc, 622 param->params[n].memref.buffer, 623 param->params[n].memref.size)) 624 return TEE_ERROR_BAD_PARAMETERS; 625 626 src_pa = virt_to_phys(param->params[n].memref.buffer); 627 if (!src_pa) 628 return TEE_ERROR_BAD_PARAMETERS; 629 630 param->param_attr[n] = tee_mmu_user_get_cache_attr( 631 utc, (void *)param->params[n].memref.buffer); 632 633 param->params[n].memref.buffer = (void *)src_pa; 634 break; 635 636 default: 637 break; 638 } 639 } 640 641 if (req_mem == 0) 642 return TEE_SUCCESS; 643 644 /* Allocate section in secure DDR */ 645 *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 646 if (*mm == NULL) { 647 DMSG("tee_mm_alloc TEE_ERROR_GENERIC"); 648 return TEE_ERROR_GENERIC; 649 } 650 dst_pa = tee_mm_get_smem(*mm); 651 652 /* Get the virtual address for the section in secure DDR */ 653 dst = phys_to_virt(dst_pa, MEM_AREA_TA_RAM); 654 655 for (n = 0; n < 4; n++) { 656 657 if (ta_private_memref[n] == false) 658 continue; 659 660 s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t)); 661 662 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 663 case TEE_PARAM_TYPE_MEMREF_INPUT: 664 case TEE_PARAM_TYPE_MEMREF_INOUT: 665 if (param->params[n].memref.buffer != NULL) { 666 res = tee_svc_copy_from_user(dst, 667 param->params[n].memref.buffer, 668 param->params[n].memref.size); 669 if (res != TEE_SUCCESS) 670 return res; 671 param->param_attr[n] = sec_ddr_attr; 672 param->params[n].memref.buffer = (void *)dst_pa; 673 tmp_buf_va[n] = dst; 674 dst += s; 675 dst_pa += s; 676 } 677 break; 678 679 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 680 if (param->params[n].memref.buffer != NULL) { 681 param->param_attr[n] = sec_ddr_attr; 682 param->params[n].memref.buffer = (void *)dst_pa; 683 tmp_buf_va[n] = dst; 684 dst += s; 685 dst_pa += s; 686 } 687 break; 688 689 default: 690 continue; 691 } 692 } 693 694 return TEE_SUCCESS; 695 } 696 697 /* 698 * Back from execution of service: update parameters passed from TA: 699 * If some parameters were memory references: 700 * - either the memref was temporary: copy back data and update size 701 * - or it was the original TA memref: update only the size value. 702 */ 703 static TEE_Result tee_svc_update_out_param( 704 struct tee_ta_session *sess, 705 struct tee_ta_session *called_sess, 706 struct tee_ta_param *param, 707 void *tmp_buf_va[TEE_NUM_PARAMS], 708 struct utee_params *usr_param) 709 { 710 size_t n; 711 void *p; 712 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 713 bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx); 714 715 for (n = 0; n < TEE_NUM_PARAMS; n++) { 716 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 717 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 718 case TEE_PARAM_TYPE_MEMREF_INOUT: 719 p = (void *)(uintptr_t)usr_param->vals[n * 2]; 720 721 /* outside TA private => memref is valid, update size */ 722 if (!tee_mmu_is_vbuf_inside_ta_private(utc, p, 723 param->params[n].memref.size)) { 724 usr_param->vals[n * 2 + 1] = 725 param->params[n].memref.size; 726 break; 727 } 728 729 /* 730 * If we called a kernel TA the parameters are in shared 731 * memory and no copy is needed. 732 */ 733 if (have_private_mem_map && 734 param->params[n].memref.size <= 735 usr_param->vals[n * 2 + 1]) { 736 uint8_t *src = tmp_buf_va[n]; 737 TEE_Result res; 738 739 res = tee_svc_copy_to_user(p, src, 740 param->params[n].memref.size); 741 if (res != TEE_SUCCESS) 742 return res; 743 744 } 745 usr_param->vals[n * 2 + 1] = 746 param->params[n].memref.size; 747 break; 748 749 case TEE_PARAM_TYPE_VALUE_OUTPUT: 750 case TEE_PARAM_TYPE_VALUE_INOUT: 751 usr_param->vals[n * 2] = param->params[n].value.a; 752 usr_param->vals[n * 2 + 1] = param->params[n].value.b; 753 break; 754 755 default: 756 continue; 757 } 758 } 759 760 return TEE_SUCCESS; 761 } 762 763 /* Called when a TA calls an OpenSession on another TA */ 764 TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 765 unsigned long cancel_req_to, 766 struct utee_params *usr_param, uint32_t *ta_sess, 767 uint32_t *ret_orig) 768 { 769 TEE_Result res; 770 uint32_t ret_o = TEE_ORIGIN_TEE; 771 struct tee_ta_session *s = NULL; 772 struct tee_ta_session *sess; 773 tee_mm_entry_t *mm_param = NULL; 774 TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 775 struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 776 TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 777 void *tmp_buf_va[TEE_NUM_PARAMS]; 778 struct user_ta_ctx *utc; 779 780 if (uuid == NULL || param == NULL || clnt_id == NULL) { 781 res = TEE_ERROR_OUT_OF_MEMORY; 782 goto out_free_only; 783 } 784 785 memset(param, 0, sizeof(struct tee_ta_param)); 786 787 res = tee_ta_get_current_session(&sess); 788 if (res != TEE_SUCCESS) 789 goto out_free_only; 790 utc = to_user_ta_ctx(sess->ctx); 791 792 res = tee_svc_copy_from_user(uuid, dest, sizeof(TEE_UUID)); 793 if (res != TEE_SUCCESS) 794 goto function_exit; 795 796 clnt_id->login = TEE_LOGIN_TRUSTED_APP; 797 memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 798 799 res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va, 800 &mm_param); 801 if (res != TEE_SUCCESS) 802 goto function_exit; 803 804 /* 805 * Find session of a multi session TA or a static TA 806 * In such a case, there is no need to ask the supplicant for the TA 807 * code 808 */ 809 res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 810 clnt_id, cancel_req_to, param); 811 if (res != TEE_SUCCESS) 812 goto function_exit; 813 814 res = tee_svc_update_out_param(sess, s, param, tmp_buf_va, usr_param); 815 816 function_exit: 817 sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 818 tee_mm_free(mm_param); 819 if (res == TEE_SUCCESS) 820 tee_svc_copy_kaddr_to_uref(ta_sess, s); 821 tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o)); 822 823 out_free_only: 824 free(param); 825 free(uuid); 826 free(clnt_id); 827 return res; 828 } 829 830 TEE_Result syscall_close_ta_session(unsigned long ta_sess) 831 { 832 TEE_Result res; 833 struct tee_ta_session *sess; 834 TEE_Identity clnt_id; 835 struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess); 836 struct user_ta_ctx *utc; 837 838 res = tee_ta_get_current_session(&sess); 839 if (res != TEE_SUCCESS) 840 return res; 841 utc = to_user_ta_ctx(sess->ctx); 842 843 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 844 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 845 846 return tee_ta_close_session(s, &utc->open_sessions, &clnt_id); 847 } 848 849 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 850 unsigned long cancel_req_to, unsigned long cmd_id, 851 struct utee_params *usr_param, uint32_t *ret_orig) 852 { 853 TEE_Result res; 854 TEE_Result res2; 855 uint32_t ret_o = TEE_ORIGIN_TEE; 856 struct tee_ta_param param = { 0 }; 857 TEE_Identity clnt_id; 858 struct tee_ta_session *sess; 859 struct tee_ta_session *called_sess; 860 tee_mm_entry_t *mm_param = NULL; 861 void *tmp_buf_va[TEE_NUM_PARAMS]; 862 struct user_ta_ctx *utc; 863 864 res = tee_ta_get_current_session(&sess); 865 if (res != TEE_SUCCESS) 866 return res; 867 utc = to_user_ta_ctx(sess->ctx); 868 869 called_sess = tee_ta_get_session( 870 (vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true, 871 &utc->open_sessions); 872 if (!called_sess) 873 return TEE_ERROR_BAD_PARAMETERS; 874 875 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 876 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 877 878 res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 879 tmp_buf_va, &mm_param); 880 if (res != TEE_SUCCESS) 881 goto function_exit; 882 883 res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 884 cancel_req_to, cmd_id, ¶m); 885 886 res2 = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_va, 887 usr_param); 888 if (res2 != TEE_SUCCESS) { 889 /* 890 * Spec for TEE_InvokeTACommand() says: 891 * "If the return origin is different from 892 * TEE_ORIGIN_TRUSTED_APP, then the function has failed 893 * before it could reach the destination Trusted 894 * Application." 895 * 896 * But if we can't update params to the caller we have no 897 * choice we need to return some error to indicate that 898 * parameters aren't updated as expected. 899 */ 900 ret_o = TEE_ORIGIN_TEE; 901 res = res2; 902 } 903 904 function_exit: 905 called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 906 tee_ta_put_session(called_sess); 907 tee_mm_free(mm_param); 908 if (ret_orig) 909 tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o)); 910 return res; 911 } 912 913 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 914 size_t len) 915 { 916 TEE_Result res; 917 struct tee_ta_session *s; 918 919 res = tee_ta_get_current_session(&s); 920 if (res != TEE_SUCCESS) 921 return res; 922 923 return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags, 924 (tee_uaddr_t)buf, len); 925 } 926 927 TEE_Result tee_svc_copy_from_user(void *kaddr, const void *uaddr, size_t len) 928 { 929 TEE_Result res; 930 struct tee_ta_session *s; 931 932 res = tee_ta_get_current_session(&s); 933 if (res != TEE_SUCCESS) 934 return res; 935 936 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 937 TEE_MEMORY_ACCESS_READ | 938 TEE_MEMORY_ACCESS_ANY_OWNER, 939 (tee_uaddr_t)uaddr, len); 940 if (res != TEE_SUCCESS) 941 return res; 942 943 memcpy(kaddr, uaddr, len); 944 return TEE_SUCCESS; 945 } 946 947 TEE_Result tee_svc_copy_to_user(void *uaddr, const void *kaddr, size_t len) 948 { 949 TEE_Result res; 950 struct tee_ta_session *s; 951 952 res = tee_ta_get_current_session(&s); 953 if (res != TEE_SUCCESS) 954 return res; 955 956 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 957 TEE_MEMORY_ACCESS_WRITE | 958 TEE_MEMORY_ACCESS_ANY_OWNER, 959 (tee_uaddr_t)uaddr, len); 960 if (res != TEE_SUCCESS) 961 return res; 962 963 memcpy(uaddr, kaddr, len); 964 return TEE_SUCCESS; 965 } 966 967 TEE_Result tee_svc_copy_kaddr_to_uref(uint32_t *uref, void *kaddr) 968 { 969 uint32_t ref = tee_svc_kaddr_to_uref(kaddr); 970 971 return tee_svc_copy_to_user(uref, &ref, sizeof(ref)); 972 } 973 974 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 975 { 976 TEE_Result res; 977 struct tee_ta_session *s = NULL; 978 uint32_t c; 979 980 res = tee_ta_get_current_session(&s); 981 if (res != TEE_SUCCESS) 982 return res; 983 984 c = tee_ta_session_is_cancelled(s, NULL); 985 986 return tee_svc_copy_to_user(cancel, &c, sizeof(c)); 987 } 988 989 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 990 { 991 TEE_Result res; 992 struct tee_ta_session *s = NULL; 993 uint32_t m; 994 995 res = tee_ta_get_current_session(&s); 996 if (res != TEE_SUCCESS) 997 return res; 998 999 m = s->cancel_mask; 1000 s->cancel_mask = false; 1001 return tee_svc_copy_to_user(old_mask, &m, sizeof(m)); 1002 } 1003 1004 TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 1005 { 1006 TEE_Result res; 1007 struct tee_ta_session *s = NULL; 1008 uint32_t m; 1009 1010 res = tee_ta_get_current_session(&s); 1011 if (res != TEE_SUCCESS) 1012 return res; 1013 1014 m = s->cancel_mask; 1015 s->cancel_mask = true; 1016 return tee_svc_copy_to_user(old_mask, &m, sizeof(m)); 1017 } 1018 1019 TEE_Result syscall_wait(unsigned long timeout) 1020 { 1021 TEE_Result res = TEE_SUCCESS; 1022 uint32_t mytime = 0; 1023 struct tee_ta_session *s; 1024 TEE_Time base_time; 1025 TEE_Time current_time; 1026 1027 res = tee_ta_get_current_session(&s); 1028 if (res != TEE_SUCCESS) 1029 return res; 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(s, ¤t_time)) 1041 return TEE_ERROR_CANCEL; 1042 1043 mytime = (current_time.seconds - base_time.seconds) * 1000 + 1044 (int)current_time.millis - (int)base_time.millis; 1045 if (mytime >= timeout) 1046 return TEE_SUCCESS; 1047 1048 tee_time_wait(timeout - mytime); 1049 } 1050 1051 return res; 1052 } 1053 1054 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 1055 { 1056 TEE_Result res, res2; 1057 struct tee_ta_session *s = NULL; 1058 TEE_Time t; 1059 1060 res = tee_ta_get_current_session(&s); 1061 if (res != TEE_SUCCESS) 1062 return res; 1063 1064 switch (cat) { 1065 case UTEE_TIME_CAT_SYSTEM: 1066 res = tee_time_get_sys_time(&t); 1067 break; 1068 case UTEE_TIME_CAT_TA_PERSISTENT: 1069 res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 1070 break; 1071 case UTEE_TIME_CAT_REE: 1072 res = tee_time_get_ree_time(&t); 1073 break; 1074 default: 1075 res = TEE_ERROR_BAD_PARAMETERS; 1076 break; 1077 } 1078 1079 if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 1080 res2 = tee_svc_copy_to_user(mytime, &t, sizeof(t)); 1081 if (res2 != TEE_SUCCESS) 1082 res = res2; 1083 } 1084 1085 return res; 1086 } 1087 1088 TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 1089 { 1090 TEE_Result res; 1091 struct tee_ta_session *s = NULL; 1092 TEE_Time t; 1093 1094 res = tee_ta_get_current_session(&s); 1095 if (res != TEE_SUCCESS) 1096 return res; 1097 1098 res = tee_svc_copy_from_user(&t, mytime, sizeof(t)); 1099 if (res != TEE_SUCCESS) 1100 return res; 1101 1102 return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 1103 } 1104