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(NULL, 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, 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(sess, buf, &uuid, sizeof(TEE_UUID)); 167 } 168 169 static TEE_Result get_prop_tee_sys_time_prot_level(struct tee_ta_session *sess, 170 void *buf, size_t *blen) 171 { 172 uint32_t prot; 173 174 if (*blen < sizeof(prot)) { 175 *blen = sizeof(prot); 176 return TEE_ERROR_SHORT_BUFFER; 177 } 178 *blen = sizeof(prot); 179 prot = tee_time_get_sys_time_protection_level(); 180 return tee_svc_copy_to_user(sess, (void *)buf, &prot, sizeof(prot)); 181 } 182 183 static TEE_Result get_prop_client_id(struct tee_ta_session *sess, 184 void *buf, size_t *blen) 185 { 186 if (*blen < sizeof(TEE_Identity)) { 187 *blen = sizeof(TEE_Identity); 188 return TEE_ERROR_SHORT_BUFFER; 189 } 190 *blen = sizeof(TEE_Identity); 191 return tee_svc_copy_to_user(sess, buf, &sess->clnt_id, 192 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(sess, buf, &sess->ctx->uuid, 204 sizeof(TEE_UUID)); 205 } 206 207 /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */ 208 const struct tee_props tee_propset_client[] = { 209 { 210 .name = "gpd.client.identity", 211 .prop_type = USER_TA_PROP_TYPE_IDENTITY, 212 .get_prop_func = get_prop_client_id 213 }, 214 }; 215 216 /* Properties of the set TEE_PROPSET_CURRENT_TA */ 217 const struct tee_props tee_propset_ta[] = { 218 { 219 .name = "gpd.ta.appID", 220 .prop_type = USER_TA_PROP_TYPE_UUID, 221 .get_prop_func = get_prop_ta_app_id 222 }, 223 224 /* 225 * Following properties are processed directly in libutee: 226 * TA_PROP_STR_SINGLE_INSTANCE 227 * TA_PROP_STR_MULTI_SESSION 228 * TA_PROP_STR_KEEP_ALIVE 229 * TA_PROP_STR_DATA_SIZE 230 * TA_PROP_STR_STACK_SIZE 231 * TA_PROP_STR_VERSION 232 * TA_PROP_STR_DESCRIPTION 233 * USER_TA_PROP_TYPE_STRING, 234 * TA_DESCRIPTION 235 */ 236 }; 237 238 /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */ 239 const struct tee_props tee_propset_tee[] = { 240 { 241 .name = "gpd.tee.apiversion", 242 .prop_type = USER_TA_PROP_TYPE_STRING, 243 .data = api_vers, 244 .len = sizeof(api_vers), 245 }, 246 { 247 .name = "gpd.tee.description", 248 .prop_type = USER_TA_PROP_TYPE_STRING, 249 .data = descr, .len = sizeof(descr) 250 }, 251 { 252 .name = "gpd.tee.deviceID", 253 .prop_type = USER_TA_PROP_TYPE_UUID, 254 .get_prop_func = get_prop_tee_dev_id 255 }, 256 { 257 .name = "gpd.tee.systemTime.protectionLevel", 258 .prop_type = USER_TA_PROP_TYPE_U32, 259 .get_prop_func = get_prop_tee_sys_time_prot_level 260 }, 261 { 262 .name = "gpd.tee.TAPersistentTime.protectionLevel", 263 .prop_type = USER_TA_PROP_TYPE_U32, 264 .data = &ta_time_prot_lvl, 265 .len = sizeof(ta_time_prot_lvl) 266 }, 267 { 268 .name = "gpd.tee.cryptography.ecc", 269 .prop_type = USER_TA_PROP_TYPE_BOOL, 270 .data = &crypto_ecc_en, 271 .len = sizeof(crypto_ecc_en) 272 }, 273 { 274 .name = "gpd.tee.trustedStorage.antiRollback.protectionLevel", 275 .prop_type = USER_TA_PROP_TYPE_U32, 276 .data = &ts_antiroll_prot_lvl, 277 .len = sizeof(ts_antiroll_prot_lvl) 278 }, 279 { 280 .name = "gpd.tee.trustedos.implementation.version", 281 .prop_type = USER_TA_PROP_TYPE_STRING, 282 .data = trustedos_impl_version, 283 .len = sizeof(trustedos_impl_version) 284 }, 285 { 286 .name = "gpd.tee.trustedos.implementation.binaryversion", 287 .prop_type = USER_TA_PROP_TYPE_U32, 288 .data = &trustedos_impl_bin_version, 289 .len = sizeof(trustedos_impl_bin_version) 290 }, 291 { 292 .name = "gpd.tee.trustedos.manufacturer", 293 .prop_type = USER_TA_PROP_TYPE_STRING, 294 .data = trustedos_manufacturer, 295 .len = sizeof(trustedos_manufacturer) 296 }, 297 { 298 .name = "gpd.tee.firmware.implementation.version", 299 .prop_type = USER_TA_PROP_TYPE_STRING, 300 .data = fw_impl_version, 301 .len = sizeof(fw_impl_version) 302 }, 303 { 304 .name = "gpd.tee.firmware.implementation.binaryversion", 305 .prop_type = USER_TA_PROP_TYPE_U32, 306 .data = &fw_impl_bin_version, 307 .len = sizeof(fw_impl_bin_version) 308 }, 309 { 310 .name = "gpd.tee.firmware.manufacturer", 311 .prop_type = USER_TA_PROP_TYPE_STRING, 312 .data = fw_manufacturer, 313 .len = sizeof(fw_manufacturer) 314 }, 315 316 /* 317 * Following properties are processed directly in libutee: 318 * gpd.tee.arith.maxBigIntSize 319 */ 320 }; 321 322 __weak const struct tee_vendor_props vendor_props_client; 323 __weak const struct tee_vendor_props vendor_props_ta; 324 __weak const struct tee_vendor_props vendor_props_tee; 325 326 static void get_prop_set(unsigned long prop_set, 327 const struct tee_props **props, 328 size_t *size, 329 const struct tee_props **vendor_props, 330 size_t *vendor_size) 331 { 332 if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) { 333 *props = tee_propset_client; 334 *size = ARRAY_SIZE(tee_propset_client); 335 *vendor_props = vendor_props_client.props; 336 *vendor_size = vendor_props_client.len; 337 } else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) { 338 *props = tee_propset_ta; 339 *size = ARRAY_SIZE(tee_propset_ta); 340 *vendor_props = vendor_props_ta.props; 341 *vendor_size = vendor_props_ta.len; 342 } else if ((TEE_PropSetHandle)prop_set == 343 TEE_PROPSET_TEE_IMPLEMENTATION) { 344 *props = tee_propset_tee; 345 *size = ARRAY_SIZE(tee_propset_tee); 346 *vendor_props = vendor_props_tee.props; 347 *vendor_size = vendor_props_tee.len; 348 } else { 349 *props = NULL; 350 *size = 0; 351 *vendor_props = NULL; 352 *vendor_size = 0; 353 } 354 } 355 356 static const struct tee_props *get_prop_struct(unsigned long prop_set, 357 unsigned long index) 358 { 359 const struct tee_props *props; 360 const struct tee_props *vendor_props; 361 size_t size; 362 size_t vendor_size; 363 364 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 365 366 if (index < size) 367 return &(props[index]); 368 index -= size; 369 370 if (index < vendor_size) 371 return &(vendor_props[index]); 372 373 return NULL; 374 } 375 376 /* 377 * prop_set is part of TEE_PROPSET_xxx 378 * index is the index in the Property Set to retrieve 379 * if name is not NULL, the name of "index" property is returned 380 * if buf is not NULL, the property is returned 381 */ 382 TEE_Result syscall_get_property(unsigned long prop_set, 383 unsigned long index, 384 void *name, uint32_t *name_len, 385 void *buf, uint32_t *blen, 386 uint32_t *prop_type) 387 { 388 struct tee_ta_session *sess; 389 TEE_Result res; 390 TEE_Result res2; 391 const struct tee_props *prop; 392 uint32_t klen; 393 size_t klen_size; 394 uint32_t elen; 395 396 prop = get_prop_struct(prop_set, index); 397 if (!prop) 398 return TEE_ERROR_ITEM_NOT_FOUND; 399 400 res = tee_ta_get_current_session(&sess); 401 if (res != TEE_SUCCESS) 402 return res; 403 404 /* Get the property type */ 405 if (prop_type) { 406 res = tee_svc_copy_to_user(sess, prop_type, &prop->prop_type, 407 sizeof(*prop_type)); 408 if (res != TEE_SUCCESS) 409 return res; 410 } 411 412 /* Get the property */ 413 if (buf && blen) { 414 res = tee_svc_copy_from_user(sess, &klen, blen, sizeof(klen)); 415 if (res != TEE_SUCCESS) 416 return res; 417 418 if (prop->get_prop_func) { 419 klen_size = klen; 420 res = prop->get_prop_func(sess, buf, &klen_size); 421 klen = klen_size; 422 res2 = tee_svc_copy_to_user(sess, blen, 423 &klen, sizeof(*blen)); 424 } else { 425 if (klen < prop->len) 426 res = TEE_ERROR_SHORT_BUFFER; 427 else 428 res = tee_svc_copy_to_user(sess, buf, 429 prop->data, 430 prop->len); 431 res2 = tee_svc_copy_to_user(sess, blen, 432 &prop->len, sizeof(*blen)); 433 } 434 if (res2 != TEE_SUCCESS) 435 return res2; 436 if (res != TEE_SUCCESS) 437 return res; 438 } 439 440 /* Get the property name */ 441 if (name && name_len) { 442 res = tee_svc_copy_from_user(sess, &klen, 443 name_len, sizeof(klen)); 444 if (res != TEE_SUCCESS) 445 return res; 446 447 elen = strlen(prop->name) + 1; 448 449 if (klen < elen) 450 res = TEE_ERROR_SHORT_BUFFER; 451 else 452 res = tee_svc_copy_to_user(sess, name, 453 prop->name, elen); 454 res2 = tee_svc_copy_to_user(sess, name_len, 455 &elen, sizeof(*name_len)); 456 if (res2 != TEE_SUCCESS) 457 return res2; 458 if (res != TEE_SUCCESS) 459 return res; 460 } 461 462 return res; 463 } 464 465 /* 466 * prop_set is part of TEE_PROPSET_xxx 467 */ 468 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set, 469 void *name, 470 unsigned long name_len, 471 uint32_t *index) 472 { 473 TEE_Result res; 474 struct tee_ta_session *sess; 475 const struct tee_props *props; 476 size_t size; 477 const struct tee_props *vendor_props; 478 size_t vendor_size; 479 char *kname = 0; 480 uint32_t i; 481 482 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 483 if (!props) 484 return TEE_ERROR_ITEM_NOT_FOUND; 485 486 res = tee_ta_get_current_session(&sess); 487 if (res != TEE_SUCCESS) 488 goto out; 489 490 if (!name || !name_len) { 491 res = TEE_ERROR_BAD_PARAMETERS; 492 goto out; 493 } 494 495 kname = malloc(name_len); 496 if (!kname) 497 return TEE_ERROR_OUT_OF_MEMORY; 498 res = tee_svc_copy_from_user(sess, kname, name, name_len); 499 if (res != TEE_SUCCESS) 500 goto out; 501 kname[name_len - 1] = 0; 502 503 res = TEE_ERROR_ITEM_NOT_FOUND; 504 for (i = 0; i < size; i++) { 505 if (!strcmp(kname, props[i].name)) { 506 res = tee_svc_copy_to_user(sess, index, &i, 507 sizeof(*index)); 508 goto out; 509 } 510 } 511 for (i = size; i < size + vendor_size; i++) { 512 if (!strcmp(kname, vendor_props[i - size].name)) { 513 res = tee_svc_copy_to_user(sess, index, &i, 514 sizeof(*index)); 515 goto out; 516 } 517 } 518 519 out: 520 free(kname); 521 return res; 522 } 523 524 static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up) 525 { 526 size_t n; 527 uint32_t types = up->types; 528 529 p->types = types; 530 for (n = 0; n < TEE_NUM_PARAMS; n++) { 531 uintptr_t a = up->vals[n * 2]; 532 size_t b = up->vals[n * 2 + 1]; 533 534 switch (TEE_PARAM_TYPE_GET(types, n)) { 535 case TEE_PARAM_TYPE_MEMREF_INPUT: 536 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 537 case TEE_PARAM_TYPE_MEMREF_INOUT: 538 p->params[n].memref.buffer = (void *)a; 539 p->params[n].memref.size = b; 540 p->param_attr[n] = TEE_MATTR_VIRTUAL; 541 break; 542 case TEE_PARAM_TYPE_VALUE_INPUT: 543 case TEE_PARAM_TYPE_VALUE_INOUT: 544 p->params[n].value.a = a; 545 p->params[n].value.b = b; 546 break; 547 default: 548 p->params[n].value.a = 0; 549 p->params[n].value.b = 0; 550 break; 551 } 552 } 553 } 554 555 /* 556 * TA invokes some TA with parameter. 557 * If some parameters are memory references: 558 * - either the memref is inside TA private RAM: TA is not allowed to expose 559 * its private RAM: use a temporary memory buffer and copy the data. 560 * - or the memref is not in the TA private RAM: 561 * - if the memref was mapped to the TA, TA is allowed to expose it. 562 * - if so, converts memref virtual address into a physical address. 563 */ 564 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 565 struct tee_ta_session *called_sess, 566 struct utee_params *callee_params, 567 struct tee_ta_param *param, 568 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 569 tee_mm_entry_t **mm) 570 { 571 size_t n; 572 TEE_Result res; 573 size_t req_mem = 0; 574 size_t s; 575 uint8_t *dst = 0; 576 tee_paddr_t dst_pa, src_pa = 0; 577 bool ta_private_memref[TEE_NUM_PARAMS]; 578 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 579 580 /* fill 'param' input struct with caller params description buffer */ 581 if (!callee_params) { 582 memset(param, 0, sizeof(*param)); 583 } else { 584 res = tee_mmu_check_access_rights(utc, 585 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 586 (tee_uaddr_t)callee_params, sizeof(struct utee_params)); 587 if (res != TEE_SUCCESS) 588 return res; 589 utee_param_to_param(param, callee_params); 590 } 591 592 if (called_sess && is_static_ta_ctx(called_sess->ctx)) { 593 /* 594 * static TA, borrow the mapping of the calling 595 * during this call. 596 */ 597 called_sess->calling_sess = sess; 598 return TEE_SUCCESS; 599 } 600 601 for (n = 0; n < TEE_NUM_PARAMS; n++) { 602 603 ta_private_memref[n] = false; 604 605 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 606 case TEE_PARAM_TYPE_MEMREF_INPUT: 607 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 608 case TEE_PARAM_TYPE_MEMREF_INOUT: 609 if (param->params[n].memref.buffer == NULL) { 610 if (param->params[n].memref.size != 0) 611 return TEE_ERROR_BAD_PARAMETERS; 612 break; 613 } 614 /* uTA cannot expose its private memory */ 615 if (tee_mmu_is_vbuf_inside_ta_private(utc, 616 param->params[n].memref.buffer, 617 param->params[n].memref.size)) { 618 619 s = ROUNDUP(param->params[n].memref.size, 620 sizeof(uint32_t)); 621 /* Check overflow */ 622 if (req_mem + s < req_mem) 623 return TEE_ERROR_BAD_PARAMETERS; 624 req_mem += s; 625 ta_private_memref[n] = true; 626 break; 627 } 628 if (tee_mmu_is_vbuf_intersect_ta_private(utc, 629 param->params[n].memref.buffer, 630 param->params[n].memref.size)) 631 return TEE_ERROR_BAD_PARAMETERS; 632 633 src_pa = virt_to_phys(param->params[n].memref.buffer); 634 if (!src_pa) 635 return TEE_ERROR_BAD_PARAMETERS; 636 637 param->param_attr[n] = tee_mmu_user_get_cache_attr( 638 utc, (void *)param->params[n].memref.buffer); 639 640 param->params[n].memref.buffer = (void *)src_pa; 641 break; 642 643 default: 644 break; 645 } 646 } 647 648 if (req_mem == 0) 649 return TEE_SUCCESS; 650 651 /* Allocate section in secure DDR */ 652 *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 653 if (*mm == NULL) { 654 DMSG("tee_mm_alloc TEE_ERROR_GENERIC"); 655 return TEE_ERROR_GENERIC; 656 } 657 658 /* Get the virtual address for the section in secure DDR */ 659 res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst); 660 if (res != TEE_SUCCESS) 661 return res; 662 dst_pa = tee_mm_get_smem(*mm); 663 664 for (n = 0; n < 4; n++) { 665 666 if (ta_private_memref[n] == false) 667 continue; 668 669 s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t)); 670 671 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 672 case TEE_PARAM_TYPE_MEMREF_INPUT: 673 case TEE_PARAM_TYPE_MEMREF_INOUT: 674 if (param->params[n].memref.buffer != NULL) { 675 res = tee_svc_copy_from_user(sess, dst, 676 param->params[n].memref.buffer, 677 param->params[n].memref.size); 678 if (res != TEE_SUCCESS) 679 return res; 680 param->param_attr[n] = 681 tee_mmu_kmap_get_cache_attr(dst); 682 param->params[n].memref.buffer = (void *)dst_pa; 683 tmp_buf_pa[n] = dst_pa; 684 dst += s; 685 dst_pa += s; 686 } 687 break; 688 689 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 690 if (param->params[n].memref.buffer != NULL) { 691 param->param_attr[n] = 692 tee_mmu_kmap_get_cache_attr(dst); 693 param->params[n].memref.buffer = (void *)dst_pa; 694 tmp_buf_pa[n] = dst_pa; 695 dst += s; 696 dst_pa += s; 697 } 698 break; 699 700 default: 701 continue; 702 } 703 } 704 705 tee_mmu_kunmap(dst, req_mem); 706 707 return TEE_SUCCESS; 708 } 709 710 /* 711 * Back from execution of service: update parameters passed from TA: 712 * If some parameters were memory references: 713 * - either the memref was temporary: copy back data and update size 714 * - or it was the original TA memref: update only the size value. 715 */ 716 static TEE_Result tee_svc_update_out_param( 717 struct tee_ta_session *sess, 718 struct tee_ta_session *called_sess, 719 struct tee_ta_param *param, 720 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 721 struct utee_params *usr_param) 722 { 723 size_t n; 724 void *p; 725 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 726 bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx); 727 728 tee_ta_set_current_session(sess); 729 730 for (n = 0; n < TEE_NUM_PARAMS; n++) { 731 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 732 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 733 case TEE_PARAM_TYPE_MEMREF_INOUT: 734 p = (void *)(uintptr_t)usr_param->vals[n * 2]; 735 736 /* outside TA private => memref is valid, update size */ 737 if (!tee_mmu_is_vbuf_inside_ta_private(utc, p, 738 param->params[n].memref.size)) { 739 usr_param->vals[n * 2 + 1] = 740 param->params[n].memref.size; 741 break; 742 } 743 744 /* 745 * If we called a kernel TA the parameters are in shared 746 * memory and no copy is needed. 747 */ 748 if (have_private_mem_map && 749 param->params[n].memref.size <= 750 usr_param->vals[n * 2 + 1]) { 751 uint8_t *src = 0; 752 TEE_Result res; 753 754 /* FIXME: TA_RAM is already mapped ! */ 755 res = tee_mmu_kmap(tmp_buf_pa[n], 756 param->params[n].memref.size, &src); 757 if (res != TEE_SUCCESS) 758 return TEE_ERROR_GENERIC; 759 760 res = tee_svc_copy_to_user(sess, p, src, 761 param->params[n].memref.size); 762 if (res != TEE_SUCCESS) 763 return res; 764 tee_mmu_kunmap(src, 765 param->params[n].memref.size); 766 767 } 768 usr_param->vals[n * 2 + 1] = 769 param->params[n].memref.size; 770 break; 771 772 case TEE_PARAM_TYPE_VALUE_OUTPUT: 773 case TEE_PARAM_TYPE_VALUE_INOUT: 774 usr_param->vals[n * 2] = param->params[n].value.a; 775 usr_param->vals[n * 2 + 1] = param->params[n].value.b; 776 break; 777 778 default: 779 continue; 780 } 781 } 782 783 return TEE_SUCCESS; 784 } 785 786 /* Called when a TA calls an OpenSession on another TA */ 787 TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 788 unsigned long cancel_req_to, 789 struct utee_params *usr_param, uint32_t *ta_sess, 790 uint32_t *ret_orig) 791 { 792 TEE_Result res; 793 uint32_t ret_o = TEE_ORIGIN_TEE; 794 struct tee_ta_session *s = NULL; 795 struct tee_ta_session *sess; 796 tee_mm_entry_t *mm_param = NULL; 797 TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 798 struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 799 TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 800 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 801 struct user_ta_ctx *utc; 802 803 if (uuid == NULL || param == NULL || clnt_id == NULL) { 804 res = TEE_ERROR_OUT_OF_MEMORY; 805 goto out_free_only; 806 } 807 808 memset(param, 0, sizeof(struct tee_ta_param)); 809 810 res = tee_ta_get_current_session(&sess); 811 if (res != TEE_SUCCESS) 812 goto out_free_only; 813 utc = to_user_ta_ctx(sess->ctx); 814 815 res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID)); 816 if (res != TEE_SUCCESS) 817 goto function_exit; 818 819 clnt_id->login = TEE_LOGIN_TRUSTED_APP; 820 memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 821 822 res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_pa, 823 &mm_param); 824 if (res != TEE_SUCCESS) 825 goto function_exit; 826 827 /* 828 * Find session of a multi session TA or a static TA 829 * In such a case, there is no need to ask the supplicant for the TA 830 * code 831 */ 832 res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 833 clnt_id, cancel_req_to, param); 834 if (res != TEE_SUCCESS) 835 goto function_exit; 836 837 res = tee_svc_update_out_param(sess, s, param, tmp_buf_pa, usr_param); 838 839 function_exit: 840 tee_ta_set_current_session(sess); 841 sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 842 843 if (mm_param != NULL) { 844 void *va = phys_to_virt(tee_mm_get_smem(mm_param), 845 MEM_AREA_KMAP_VASPACE); 846 847 if (va) 848 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 849 } 850 tee_mm_free(mm_param); 851 if (res == TEE_SUCCESS) 852 tee_svc_copy_kaddr_to_uref(sess, ta_sess, s); 853 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 854 855 out_free_only: 856 free(param); 857 free(uuid); 858 free(clnt_id); 859 return res; 860 } 861 862 TEE_Result syscall_close_ta_session(unsigned long ta_sess) 863 { 864 TEE_Result res; 865 struct tee_ta_session *sess; 866 TEE_Identity clnt_id; 867 struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess); 868 struct user_ta_ctx *utc; 869 870 res = tee_ta_get_current_session(&sess); 871 if (res != TEE_SUCCESS) 872 return res; 873 utc = to_user_ta_ctx(sess->ctx); 874 875 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 876 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 877 878 tee_ta_set_current_session(NULL); 879 res = tee_ta_close_session(s, &utc->open_sessions, &clnt_id); 880 tee_ta_set_current_session(sess); 881 return res; 882 } 883 884 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 885 unsigned long cancel_req_to, unsigned long cmd_id, 886 struct utee_params *usr_param, uint32_t *ret_orig) 887 { 888 TEE_Result res; 889 TEE_Result res2; 890 uint32_t ret_o = TEE_ORIGIN_TEE; 891 struct tee_ta_param param = { 0 }; 892 TEE_Identity clnt_id; 893 struct tee_ta_session *sess; 894 struct tee_ta_session *called_sess; 895 tee_mm_entry_t *mm_param = NULL; 896 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 897 struct user_ta_ctx *utc; 898 899 res = tee_ta_get_current_session(&sess); 900 if (res != TEE_SUCCESS) 901 return res; 902 utc = to_user_ta_ctx(sess->ctx); 903 904 called_sess = tee_ta_get_session( 905 (vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true, 906 &utc->open_sessions); 907 if (!called_sess) 908 return TEE_ERROR_BAD_PARAMETERS; 909 910 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 911 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 912 913 res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 914 tmp_buf_pa, &mm_param); 915 if (res != TEE_SUCCESS) 916 goto function_exit; 917 918 res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 919 cancel_req_to, cmd_id, ¶m); 920 921 res2 = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_pa, 922 usr_param); 923 if (res2 != TEE_SUCCESS) { 924 /* 925 * Spec for TEE_InvokeTACommand() says: 926 * "If the return origin is different from 927 * TEE_ORIGIN_TRUSTED_APP, then the function has failed 928 * before it could reach the destination Trusted 929 * Application." 930 * 931 * But if we can't update params to the caller we have no 932 * choice we need to return some error to indicate that 933 * parameters aren't updated as expected. 934 */ 935 ret_o = TEE_ORIGIN_TEE; 936 res = res2; 937 } 938 939 function_exit: 940 tee_ta_set_current_session(sess); 941 called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 942 tee_ta_put_session(called_sess); 943 944 if (mm_param != NULL) { 945 void *va = phys_to_virt(tee_mm_get_smem(mm_param), 946 MEM_AREA_KMAP_VASPACE); 947 948 if (va) 949 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 950 } 951 tee_mm_free(mm_param); 952 if (ret_orig) 953 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 954 return res; 955 } 956 957 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 958 size_t len) 959 { 960 TEE_Result res; 961 struct tee_ta_session *s; 962 963 res = tee_ta_get_current_session(&s); 964 if (res != TEE_SUCCESS) 965 return res; 966 967 return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags, 968 (tee_uaddr_t)buf, len); 969 } 970 971 TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr, 972 const void *uaddr, size_t len) 973 { 974 TEE_Result res; 975 struct tee_ta_session *s; 976 977 if (sess == NULL) { 978 res = tee_ta_get_current_session(&s); 979 if (res != TEE_SUCCESS) 980 return res; 981 } else { 982 s = sess; 983 tee_ta_set_current_session(s); 984 } 985 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 986 TEE_MEMORY_ACCESS_READ | 987 TEE_MEMORY_ACCESS_ANY_OWNER, 988 (tee_uaddr_t)uaddr, len); 989 if (res != TEE_SUCCESS) 990 return res; 991 992 memcpy(kaddr, uaddr, len); 993 return TEE_SUCCESS; 994 } 995 996 TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr, 997 const void *kaddr, size_t len) 998 { 999 TEE_Result res; 1000 struct tee_ta_session *s; 1001 1002 if (sess == NULL) { 1003 res = tee_ta_get_current_session(&s); 1004 if (res != TEE_SUCCESS) 1005 return res; 1006 } else { 1007 s = sess; 1008 tee_ta_set_current_session(s); 1009 } 1010 1011 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 1012 TEE_MEMORY_ACCESS_WRITE | 1013 TEE_MEMORY_ACCESS_ANY_OWNER, 1014 (tee_uaddr_t)uaddr, len); 1015 if (res != TEE_SUCCESS) 1016 return res; 1017 1018 memcpy(uaddr, kaddr, len); 1019 return TEE_SUCCESS; 1020 } 1021 1022 TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess, 1023 uint32_t *uref, void *kaddr) 1024 { 1025 uint32_t ref = tee_svc_kaddr_to_uref(kaddr); 1026 1027 return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref)); 1028 } 1029 1030 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 1031 { 1032 TEE_Result res; 1033 struct tee_ta_session *s = NULL; 1034 uint32_t c; 1035 1036 res = tee_ta_get_current_session(&s); 1037 if (res != TEE_SUCCESS) 1038 return res; 1039 1040 c = tee_ta_session_is_cancelled(s, NULL); 1041 1042 return tee_svc_copy_to_user(s, cancel, &c, sizeof(c)); 1043 } 1044 1045 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 1046 { 1047 TEE_Result res; 1048 struct tee_ta_session *s = NULL; 1049 uint32_t m; 1050 1051 res = tee_ta_get_current_session(&s); 1052 if (res != TEE_SUCCESS) 1053 return res; 1054 1055 m = s->cancel_mask; 1056 s->cancel_mask = false; 1057 return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 1058 } 1059 1060 TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 1061 { 1062 TEE_Result res; 1063 struct tee_ta_session *s = NULL; 1064 uint32_t m; 1065 1066 res = tee_ta_get_current_session(&s); 1067 if (res != TEE_SUCCESS) 1068 return res; 1069 1070 m = s->cancel_mask; 1071 s->cancel_mask = true; 1072 return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 1073 } 1074 1075 TEE_Result syscall_wait(unsigned long timeout) 1076 { 1077 TEE_Result res = TEE_SUCCESS; 1078 uint32_t mytime = 0; 1079 struct tee_ta_session *s; 1080 TEE_Time base_time; 1081 TEE_Time current_time; 1082 1083 res = tee_ta_get_current_session(&s); 1084 if (res != TEE_SUCCESS) 1085 return res; 1086 1087 res = tee_time_get_sys_time(&base_time); 1088 if (res != TEE_SUCCESS) 1089 return res; 1090 1091 while (true) { 1092 res = tee_time_get_sys_time(¤t_time); 1093 if (res != TEE_SUCCESS) 1094 return res; 1095 1096 if (tee_ta_session_is_cancelled(s, ¤t_time)) 1097 return TEE_ERROR_CANCEL; 1098 1099 mytime = (current_time.seconds - base_time.seconds) * 1000 + 1100 (int)current_time.millis - (int)base_time.millis; 1101 if (mytime >= timeout) 1102 return TEE_SUCCESS; 1103 1104 tee_time_wait(timeout - mytime); 1105 } 1106 1107 return res; 1108 } 1109 1110 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 1111 { 1112 TEE_Result res, res2; 1113 struct tee_ta_session *s = NULL; 1114 TEE_Time t; 1115 1116 res = tee_ta_get_current_session(&s); 1117 if (res != TEE_SUCCESS) 1118 return res; 1119 1120 switch (cat) { 1121 case UTEE_TIME_CAT_SYSTEM: 1122 res = tee_time_get_sys_time(&t); 1123 break; 1124 case UTEE_TIME_CAT_TA_PERSISTENT: 1125 res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 1126 break; 1127 case UTEE_TIME_CAT_REE: 1128 res = tee_time_get_ree_time(&t); 1129 break; 1130 default: 1131 res = TEE_ERROR_BAD_PARAMETERS; 1132 break; 1133 } 1134 1135 if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 1136 res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t)); 1137 if (res2 != TEE_SUCCESS) 1138 res = res2; 1139 } 1140 1141 return res; 1142 } 1143 1144 TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 1145 { 1146 TEE_Result res; 1147 struct tee_ta_session *s = NULL; 1148 TEE_Time t; 1149 1150 res = tee_ta_get_current_session(&s); 1151 if (res != TEE_SUCCESS) 1152 return res; 1153 1154 res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t)); 1155 if (res != TEE_SUCCESS) 1156 return res; 1157 1158 return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 1159 } 1160