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 vaddr_t tee_svc_uref_base; 47 48 void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused) 49 { 50 #ifdef CFG_TEE_CORE_TA_TRACE 51 char *kbuf; 52 53 if (len == 0) 54 return; 55 56 kbuf = malloc(len); 57 if (kbuf == NULL) 58 return; 59 *kbuf = '\0'; 60 61 /* log as Info/Raw traces */ 62 if (tee_svc_copy_from_user(kbuf, buf, len) == TEE_SUCCESS) 63 TAMSG_RAW("%.*s", (int)len, kbuf); 64 65 free(kbuf); 66 #endif 67 } 68 69 TEE_Result syscall_not_supported(void) 70 { 71 return TEE_ERROR_NOT_SUPPORTED; 72 } 73 74 /* Configuration properties */ 75 /* API implementation version */ 76 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION); 77 78 /* Implementation description (implementation-dependent) */ 79 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR); 80 81 /* 82 * TA persistent time protection level 83 * 100: Persistent time based on an REE-controlled real-time clock 84 * and on the TEE Trusted Storage for the storage of origins (default). 85 * 1000: Persistent time based on a TEE-controlled real-time clock 86 * and the TEE Trusted Storage. 87 * The real-time clock MUST be out of reach of software attacks 88 * from the REE. 89 */ 90 static const uint32_t ta_time_prot_lvl = 100; 91 92 /* Elliptic Curve Cryptographic support */ 93 #ifdef CFG_CRYPTO_ECC 94 static const uint32_t crypto_ecc_en = 1; 95 #else 96 static const uint32_t crypto_ecc_en; 97 #endif 98 99 /* 100 * Trusted storage anti rollback protection level 101 * 0 (or missing): No antirollback protection (default) 102 * 100: Antirollback enforced at REE level 103 * 1000: Antirollback TEE-controlled hardware 104 */ 105 static const uint32_t ts_antiroll_prot_lvl; 106 107 /* Trusted OS implementation version */ 108 static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION); 109 110 /* Trusted OS implementation version (binary value) */ 111 static const uint32_t trustedos_impl_bin_version; /* 0 by default */ 112 113 /* Trusted OS implementation manufacturer name */ 114 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER); 115 116 /* Trusted firmware version */ 117 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION); 118 119 /* Trusted firmware version (binary value) */ 120 static const uint32_t fw_impl_bin_version; /* 0 by default */ 121 122 /* Trusted firmware manufacturer name */ 123 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER); 124 125 static TEE_Result get_prop_tee_dev_id(struct tee_ta_session *sess __unused, 126 void *buf, size_t *blen) 127 { 128 TEE_Result res; 129 TEE_UUID uuid; 130 const size_t nslen = 5; 131 uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 132 'O', 'P', 'T', 'E', 'E' }; 133 134 if (*blen < sizeof(uuid)) { 135 *blen = sizeof(uuid); 136 return TEE_ERROR_SHORT_BUFFER; 137 } 138 *blen = sizeof(uuid); 139 140 if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen)) 141 return TEE_ERROR_BAD_STATE; 142 143 res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data), 144 (uint8_t *)&uuid, sizeof(uuid)); 145 if (res != TEE_SUCCESS) 146 return TEE_ERROR_BAD_STATE; 147 148 /* 149 * Changes the random value into and UUID as specifiec 150 * in RFC 4122. The magic values are from the example 151 * code in the RFC. 152 * 153 * TEE_UUID is defined slightly different from the RFC, 154 * but close enough for our purpose. 155 */ 156 157 uuid.timeHiAndVersion &= 0x0fff; 158 uuid.timeHiAndVersion |= 5 << 12; 159 160 /* uuid.clock_seq_hi_and_reserved in the RFC */ 161 uuid.clockSeqAndNode[0] &= 0x3f; 162 uuid.clockSeqAndNode[0] |= 0x80; 163 164 return tee_svc_copy_to_user(buf, &uuid, sizeof(TEE_UUID)); 165 } 166 167 static TEE_Result get_prop_tee_sys_time_prot_level( 168 struct tee_ta_session *sess __unused, 169 void *buf, size_t *blen) 170 { 171 uint32_t prot; 172 173 if (*blen < sizeof(prot)) { 174 *blen = sizeof(prot); 175 return TEE_ERROR_SHORT_BUFFER; 176 } 177 *blen = sizeof(prot); 178 prot = tee_time_get_sys_time_protection_level(); 179 return tee_svc_copy_to_user(buf, &prot, sizeof(prot)); 180 } 181 182 static TEE_Result get_prop_client_id(struct tee_ta_session *sess __unused, 183 void *buf, size_t *blen) 184 { 185 if (*blen < sizeof(TEE_Identity)) { 186 *blen = sizeof(TEE_Identity); 187 return TEE_ERROR_SHORT_BUFFER; 188 } 189 *blen = sizeof(TEE_Identity); 190 return tee_svc_copy_to_user(buf, &sess->clnt_id, sizeof(TEE_Identity)); 191 } 192 193 static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess, 194 void *buf, size_t *blen) 195 { 196 if (*blen < sizeof(TEE_UUID)) { 197 *blen = sizeof(TEE_UUID); 198 return TEE_ERROR_SHORT_BUFFER; 199 } 200 *blen = sizeof(TEE_UUID); 201 return tee_svc_copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID)); 202 } 203 204 /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */ 205 const struct tee_props tee_propset_client[] = { 206 { 207 .name = "gpd.client.identity", 208 .prop_type = USER_TA_PROP_TYPE_IDENTITY, 209 .get_prop_func = get_prop_client_id 210 }, 211 }; 212 213 /* Properties of the set TEE_PROPSET_CURRENT_TA */ 214 const struct tee_props tee_propset_ta[] = { 215 { 216 .name = "gpd.ta.appID", 217 .prop_type = USER_TA_PROP_TYPE_UUID, 218 .get_prop_func = get_prop_ta_app_id 219 }, 220 221 /* 222 * Following properties are processed directly in libutee: 223 * TA_PROP_STR_SINGLE_INSTANCE 224 * TA_PROP_STR_MULTI_SESSION 225 * TA_PROP_STR_KEEP_ALIVE 226 * TA_PROP_STR_DATA_SIZE 227 * TA_PROP_STR_STACK_SIZE 228 * TA_PROP_STR_VERSION 229 * TA_PROP_STR_DESCRIPTION 230 * USER_TA_PROP_TYPE_STRING, 231 * TA_DESCRIPTION 232 */ 233 }; 234 235 /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */ 236 const struct tee_props tee_propset_tee[] = { 237 { 238 .name = "gpd.tee.apiversion", 239 .prop_type = USER_TA_PROP_TYPE_STRING, 240 .data = api_vers, 241 .len = sizeof(api_vers), 242 }, 243 { 244 .name = "gpd.tee.description", 245 .prop_type = USER_TA_PROP_TYPE_STRING, 246 .data = descr, .len = sizeof(descr) 247 }, 248 { 249 .name = "gpd.tee.deviceID", 250 .prop_type = USER_TA_PROP_TYPE_UUID, 251 .get_prop_func = get_prop_tee_dev_id 252 }, 253 { 254 .name = "gpd.tee.systemTime.protectionLevel", 255 .prop_type = USER_TA_PROP_TYPE_U32, 256 .get_prop_func = get_prop_tee_sys_time_prot_level 257 }, 258 { 259 .name = "gpd.tee.TAPersistentTime.protectionLevel", 260 .prop_type = USER_TA_PROP_TYPE_U32, 261 .data = &ta_time_prot_lvl, 262 .len = sizeof(ta_time_prot_lvl) 263 }, 264 { 265 .name = "gpd.tee.cryptography.ecc", 266 .prop_type = USER_TA_PROP_TYPE_BOOL, 267 .data = &crypto_ecc_en, 268 .len = sizeof(crypto_ecc_en) 269 }, 270 { 271 .name = "gpd.tee.trustedStorage.antiRollback.protectionLevel", 272 .prop_type = USER_TA_PROP_TYPE_U32, 273 .data = &ts_antiroll_prot_lvl, 274 .len = sizeof(ts_antiroll_prot_lvl) 275 }, 276 { 277 .name = "gpd.tee.trustedos.implementation.version", 278 .prop_type = USER_TA_PROP_TYPE_STRING, 279 .data = trustedos_impl_version, 280 .len = sizeof(trustedos_impl_version) 281 }, 282 { 283 .name = "gpd.tee.trustedos.implementation.binaryversion", 284 .prop_type = USER_TA_PROP_TYPE_U32, 285 .data = &trustedos_impl_bin_version, 286 .len = sizeof(trustedos_impl_bin_version) 287 }, 288 { 289 .name = "gpd.tee.trustedos.manufacturer", 290 .prop_type = USER_TA_PROP_TYPE_STRING, 291 .data = trustedos_manufacturer, 292 .len = sizeof(trustedos_manufacturer) 293 }, 294 { 295 .name = "gpd.tee.firmware.implementation.version", 296 .prop_type = USER_TA_PROP_TYPE_STRING, 297 .data = fw_impl_version, 298 .len = sizeof(fw_impl_version) 299 }, 300 { 301 .name = "gpd.tee.firmware.implementation.binaryversion", 302 .prop_type = USER_TA_PROP_TYPE_U32, 303 .data = &fw_impl_bin_version, 304 .len = sizeof(fw_impl_bin_version) 305 }, 306 { 307 .name = "gpd.tee.firmware.manufacturer", 308 .prop_type = USER_TA_PROP_TYPE_STRING, 309 .data = fw_manufacturer, 310 .len = sizeof(fw_manufacturer) 311 }, 312 313 /* 314 * Following properties are processed directly in libutee: 315 * gpd.tee.arith.maxBigIntSize 316 */ 317 }; 318 319 __weak const struct tee_vendor_props vendor_props_client; 320 __weak const struct tee_vendor_props vendor_props_ta; 321 __weak const struct tee_vendor_props vendor_props_tee; 322 323 static void get_prop_set(unsigned long prop_set, 324 const struct tee_props **props, 325 size_t *size, 326 const struct tee_props **vendor_props, 327 size_t *vendor_size) 328 { 329 if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) { 330 *props = tee_propset_client; 331 *size = ARRAY_SIZE(tee_propset_client); 332 *vendor_props = vendor_props_client.props; 333 *vendor_size = vendor_props_client.len; 334 } else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) { 335 *props = tee_propset_ta; 336 *size = ARRAY_SIZE(tee_propset_ta); 337 *vendor_props = vendor_props_ta.props; 338 *vendor_size = vendor_props_ta.len; 339 } else if ((TEE_PropSetHandle)prop_set == 340 TEE_PROPSET_TEE_IMPLEMENTATION) { 341 *props = tee_propset_tee; 342 *size = ARRAY_SIZE(tee_propset_tee); 343 *vendor_props = vendor_props_tee.props; 344 *vendor_size = vendor_props_tee.len; 345 } else { 346 *props = NULL; 347 *size = 0; 348 *vendor_props = NULL; 349 *vendor_size = 0; 350 } 351 } 352 353 static const struct tee_props *get_prop_struct(unsigned long prop_set, 354 unsigned long index) 355 { 356 const struct tee_props *props; 357 const struct tee_props *vendor_props; 358 size_t size; 359 size_t vendor_size; 360 361 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 362 363 if (index < size) 364 return &(props[index]); 365 index -= size; 366 367 if (index < vendor_size) 368 return &(vendor_props[index]); 369 370 return NULL; 371 } 372 373 /* 374 * prop_set is part of TEE_PROPSET_xxx 375 * index is the index in the Property Set to retrieve 376 * if name is not NULL, the name of "index" property is returned 377 * if buf is not NULL, the property is returned 378 */ 379 TEE_Result syscall_get_property(unsigned long prop_set, 380 unsigned long index, 381 void *name, uint32_t *name_len, 382 void *buf, uint32_t *blen, 383 uint32_t *prop_type) 384 { 385 struct tee_ta_session *sess; 386 TEE_Result res; 387 TEE_Result res2; 388 const struct tee_props *prop; 389 uint32_t klen; 390 size_t klen_size; 391 uint32_t elen; 392 393 prop = get_prop_struct(prop_set, index); 394 if (!prop) 395 return TEE_ERROR_ITEM_NOT_FOUND; 396 397 res = tee_ta_get_current_session(&sess); 398 if (res != TEE_SUCCESS) 399 return res; 400 401 /* Get the property type */ 402 if (prop_type) { 403 res = tee_svc_copy_to_user(prop_type, &prop->prop_type, 404 sizeof(*prop_type)); 405 if (res != TEE_SUCCESS) 406 return res; 407 } 408 409 /* Get the property */ 410 if (buf && blen) { 411 res = tee_svc_copy_from_user(&klen, blen, sizeof(klen)); 412 if (res != TEE_SUCCESS) 413 return res; 414 415 if (prop->get_prop_func) { 416 klen_size = klen; 417 res = prop->get_prop_func(sess, buf, &klen_size); 418 klen = klen_size; 419 res2 = tee_svc_copy_to_user(blen, &klen, sizeof(*blen)); 420 } else { 421 if (klen < prop->len) 422 res = TEE_ERROR_SHORT_BUFFER; 423 else 424 res = tee_svc_copy_to_user(buf, prop->data, 425 prop->len); 426 res2 = tee_svc_copy_to_user(blen, &prop->len, 427 sizeof(*blen)); 428 } 429 if (res2 != TEE_SUCCESS) 430 return res2; 431 if (res != TEE_SUCCESS) 432 return res; 433 } 434 435 /* Get the property name */ 436 if (name && name_len) { 437 res = tee_svc_copy_from_user(&klen, name_len, sizeof(klen)); 438 if (res != TEE_SUCCESS) 439 return res; 440 441 elen = strlen(prop->name) + 1; 442 443 if (klen < elen) 444 res = TEE_ERROR_SHORT_BUFFER; 445 else 446 res = tee_svc_copy_to_user(name, prop->name, elen); 447 res2 = tee_svc_copy_to_user(name_len, &elen, sizeof(*name_len)); 448 if (res2 != TEE_SUCCESS) 449 return res2; 450 if (res != TEE_SUCCESS) 451 return res; 452 } 453 454 return res; 455 } 456 457 /* 458 * prop_set is part of TEE_PROPSET_xxx 459 */ 460 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set, 461 void *name, 462 unsigned long name_len, 463 uint32_t *index) 464 { 465 TEE_Result res; 466 struct tee_ta_session *sess; 467 const struct tee_props *props; 468 size_t size; 469 const struct tee_props *vendor_props; 470 size_t vendor_size; 471 char *kname = 0; 472 uint32_t i; 473 474 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 475 if (!props) 476 return TEE_ERROR_ITEM_NOT_FOUND; 477 478 res = tee_ta_get_current_session(&sess); 479 if (res != TEE_SUCCESS) 480 goto out; 481 482 if (!name || !name_len) { 483 res = TEE_ERROR_BAD_PARAMETERS; 484 goto out; 485 } 486 487 kname = malloc(name_len); 488 if (!kname) 489 return TEE_ERROR_OUT_OF_MEMORY; 490 res = tee_svc_copy_from_user(kname, name, name_len); 491 if (res != TEE_SUCCESS) 492 goto out; 493 kname[name_len - 1] = 0; 494 495 res = TEE_ERROR_ITEM_NOT_FOUND; 496 for (i = 0; i < size; i++) { 497 if (!strcmp(kname, props[i].name)) { 498 res = tee_svc_copy_to_user(index, &i, sizeof(*index)); 499 goto out; 500 } 501 } 502 for (i = size; i < size + vendor_size; i++) { 503 if (!strcmp(kname, vendor_props[i - size].name)) { 504 res = tee_svc_copy_to_user(index, &i, sizeof(*index)); 505 goto out; 506 } 507 } 508 509 out: 510 free(kname); 511 return res; 512 } 513 514 static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up) 515 { 516 size_t n; 517 uint32_t types = up->types; 518 519 p->types = types; 520 for (n = 0; n < TEE_NUM_PARAMS; n++) { 521 uintptr_t a = up->vals[n * 2]; 522 size_t b = up->vals[n * 2 + 1]; 523 524 switch (TEE_PARAM_TYPE_GET(types, n)) { 525 case TEE_PARAM_TYPE_MEMREF_INPUT: 526 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 527 case TEE_PARAM_TYPE_MEMREF_INOUT: 528 p->params[n].memref.buffer = (void *)a; 529 p->params[n].memref.size = b; 530 p->param_attr[n] = TEE_MATTR_VIRTUAL; 531 break; 532 case TEE_PARAM_TYPE_VALUE_INPUT: 533 case TEE_PARAM_TYPE_VALUE_INOUT: 534 p->params[n].value.a = a; 535 p->params[n].value.b = b; 536 break; 537 default: 538 p->params[n].value.a = 0; 539 p->params[n].value.b = 0; 540 break; 541 } 542 } 543 } 544 545 /* 546 * TA invokes some TA with parameter. 547 * If some parameters are memory references: 548 * - either the memref is inside TA private RAM: TA is not allowed to expose 549 * its private RAM: use a temporary memory buffer and copy the data. 550 * - or the memref is not in the TA private RAM: 551 * - if the memref was mapped to the TA, TA is allowed to expose it. 552 * - if so, converts memref virtual address into a physical address. 553 */ 554 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 555 struct tee_ta_session *called_sess, 556 struct utee_params *callee_params, 557 struct tee_ta_param *param, 558 void *tmp_buf_va[TEE_NUM_PARAMS], 559 tee_mm_entry_t **mm) 560 { 561 size_t n; 562 TEE_Result res; 563 size_t req_mem = 0; 564 size_t s; 565 uint8_t *dst = 0; 566 tee_paddr_t dst_pa, src_pa = 0; 567 bool ta_private_memref[TEE_NUM_PARAMS]; 568 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 569 const uint32_t sec_ddr_attr = TEE_MATTR_CACHE_CACHED; 570 571 /* fill 'param' input struct with caller params description buffer */ 572 if (!callee_params) { 573 memset(param, 0, sizeof(*param)); 574 } else { 575 res = tee_mmu_check_access_rights(utc, 576 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 577 (tee_uaddr_t)callee_params, sizeof(struct utee_params)); 578 if (res != TEE_SUCCESS) 579 return res; 580 utee_param_to_param(param, callee_params); 581 } 582 583 if (called_sess && is_static_ta_ctx(called_sess->ctx)) { 584 /* 585 * static TA, borrow the mapping of the calling 586 * during this call. 587 */ 588 return TEE_SUCCESS; 589 } 590 591 for (n = 0; n < TEE_NUM_PARAMS; n++) { 592 593 ta_private_memref[n] = false; 594 595 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 596 case TEE_PARAM_TYPE_MEMREF_INPUT: 597 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 598 case TEE_PARAM_TYPE_MEMREF_INOUT: 599 if (param->params[n].memref.buffer == NULL) { 600 if (param->params[n].memref.size != 0) 601 return TEE_ERROR_BAD_PARAMETERS; 602 break; 603 } 604 /* uTA cannot expose its private memory */ 605 if (tee_mmu_is_vbuf_inside_ta_private(utc, 606 param->params[n].memref.buffer, 607 param->params[n].memref.size)) { 608 609 s = ROUNDUP(param->params[n].memref.size, 610 sizeof(uint32_t)); 611 /* Check overflow */ 612 if (req_mem + s < req_mem) 613 return TEE_ERROR_BAD_PARAMETERS; 614 req_mem += s; 615 ta_private_memref[n] = true; 616 break; 617 } 618 if (tee_mmu_is_vbuf_intersect_ta_private(utc, 619 param->params[n].memref.buffer, 620 param->params[n].memref.size)) 621 return TEE_ERROR_BAD_PARAMETERS; 622 623 src_pa = virt_to_phys(param->params[n].memref.buffer); 624 if (!src_pa) 625 return TEE_ERROR_BAD_PARAMETERS; 626 627 param->param_attr[n] = tee_mmu_user_get_cache_attr( 628 utc, (void *)param->params[n].memref.buffer); 629 630 param->params[n].memref.buffer = (void *)src_pa; 631 break; 632 633 default: 634 break; 635 } 636 } 637 638 if (req_mem == 0) 639 return TEE_SUCCESS; 640 641 /* Allocate section in secure DDR */ 642 mutex_lock(&tee_ta_mutex); 643 *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 644 mutex_unlock(&tee_ta_mutex); 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 if (mm_param) { 817 mutex_lock(&tee_ta_mutex); 818 tee_mm_free(mm_param); 819 mutex_unlock(&tee_ta_mutex); 820 } 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 tee_mm_entry_t *mm_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, &mm_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 if (mm_param) { 909 mutex_lock(&tee_ta_mutex); 910 tee_mm_free(mm_param); 911 mutex_unlock(&tee_ta_mutex); 912 } 913 if (ret_orig) 914 tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o)); 915 return res; 916 } 917 918 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 919 size_t len) 920 { 921 TEE_Result res; 922 struct tee_ta_session *s; 923 924 res = tee_ta_get_current_session(&s); 925 if (res != TEE_SUCCESS) 926 return res; 927 928 return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags, 929 (tee_uaddr_t)buf, len); 930 } 931 932 TEE_Result tee_svc_copy_from_user(void *kaddr, const void *uaddr, size_t len) 933 { 934 TEE_Result res; 935 struct tee_ta_session *s; 936 937 res = tee_ta_get_current_session(&s); 938 if (res != TEE_SUCCESS) 939 return res; 940 941 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 942 TEE_MEMORY_ACCESS_READ | 943 TEE_MEMORY_ACCESS_ANY_OWNER, 944 (tee_uaddr_t)uaddr, len); 945 if (res != TEE_SUCCESS) 946 return res; 947 948 memcpy(kaddr, uaddr, len); 949 return TEE_SUCCESS; 950 } 951 952 TEE_Result tee_svc_copy_to_user(void *uaddr, const void *kaddr, size_t len) 953 { 954 TEE_Result res; 955 struct tee_ta_session *s; 956 957 res = tee_ta_get_current_session(&s); 958 if (res != TEE_SUCCESS) 959 return res; 960 961 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 962 TEE_MEMORY_ACCESS_WRITE | 963 TEE_MEMORY_ACCESS_ANY_OWNER, 964 (tee_uaddr_t)uaddr, len); 965 if (res != TEE_SUCCESS) 966 return res; 967 968 memcpy(uaddr, kaddr, len); 969 return TEE_SUCCESS; 970 } 971 972 TEE_Result tee_svc_copy_kaddr_to_uref(uint32_t *uref, void *kaddr) 973 { 974 uint32_t ref = tee_svc_kaddr_to_uref(kaddr); 975 976 return tee_svc_copy_to_user(uref, &ref, sizeof(ref)); 977 } 978 979 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 980 { 981 TEE_Result res; 982 struct tee_ta_session *s = NULL; 983 uint32_t c; 984 985 res = tee_ta_get_current_session(&s); 986 if (res != TEE_SUCCESS) 987 return res; 988 989 c = tee_ta_session_is_cancelled(s, NULL); 990 991 return tee_svc_copy_to_user(cancel, &c, sizeof(c)); 992 } 993 994 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 995 { 996 TEE_Result res; 997 struct tee_ta_session *s = NULL; 998 uint32_t m; 999 1000 res = tee_ta_get_current_session(&s); 1001 if (res != TEE_SUCCESS) 1002 return res; 1003 1004 m = s->cancel_mask; 1005 s->cancel_mask = false; 1006 return tee_svc_copy_to_user(old_mask, &m, sizeof(m)); 1007 } 1008 1009 TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 1010 { 1011 TEE_Result res; 1012 struct tee_ta_session *s = NULL; 1013 uint32_t m; 1014 1015 res = tee_ta_get_current_session(&s); 1016 if (res != TEE_SUCCESS) 1017 return res; 1018 1019 m = s->cancel_mask; 1020 s->cancel_mask = true; 1021 return tee_svc_copy_to_user(old_mask, &m, sizeof(m)); 1022 } 1023 1024 TEE_Result syscall_wait(unsigned long timeout) 1025 { 1026 TEE_Result res = TEE_SUCCESS; 1027 uint32_t mytime = 0; 1028 struct tee_ta_session *s; 1029 TEE_Time base_time; 1030 TEE_Time current_time; 1031 1032 res = tee_ta_get_current_session(&s); 1033 if (res != TEE_SUCCESS) 1034 return res; 1035 1036 res = tee_time_get_sys_time(&base_time); 1037 if (res != TEE_SUCCESS) 1038 return res; 1039 1040 while (true) { 1041 res = tee_time_get_sys_time(¤t_time); 1042 if (res != TEE_SUCCESS) 1043 return res; 1044 1045 if (tee_ta_session_is_cancelled(s, ¤t_time)) 1046 return TEE_ERROR_CANCEL; 1047 1048 mytime = (current_time.seconds - base_time.seconds) * 1000 + 1049 (int)current_time.millis - (int)base_time.millis; 1050 if (mytime >= timeout) 1051 return TEE_SUCCESS; 1052 1053 tee_time_wait(timeout - mytime); 1054 } 1055 1056 return res; 1057 } 1058 1059 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 1060 { 1061 TEE_Result res, res2; 1062 struct tee_ta_session *s = NULL; 1063 TEE_Time t; 1064 1065 res = tee_ta_get_current_session(&s); 1066 if (res != TEE_SUCCESS) 1067 return res; 1068 1069 switch (cat) { 1070 case UTEE_TIME_CAT_SYSTEM: 1071 res = tee_time_get_sys_time(&t); 1072 break; 1073 case UTEE_TIME_CAT_TA_PERSISTENT: 1074 res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 1075 break; 1076 case UTEE_TIME_CAT_REE: 1077 res = tee_time_get_ree_time(&t); 1078 break; 1079 default: 1080 res = TEE_ERROR_BAD_PARAMETERS; 1081 break; 1082 } 1083 1084 if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 1085 res2 = tee_svc_copy_to_user(mytime, &t, sizeof(t)); 1086 if (res2 != TEE_SUCCESS) 1087 res = res2; 1088 } 1089 1090 return res; 1091 } 1092 1093 TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 1094 { 1095 TEE_Result res; 1096 struct tee_ta_session *s = NULL; 1097 TEE_Time t; 1098 1099 res = tee_ta_get_current_session(&s); 1100 if (res != TEE_SUCCESS) 1101 return res; 1102 1103 res = tee_svc_copy_from_user(&t, mytime, sizeof(t)); 1104 if (res != TEE_SUCCESS) 1105 return res; 1106 1107 return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 1108 } 1109