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