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 break; 541 case TEE_PARAM_TYPE_VALUE_INPUT: 542 case TEE_PARAM_TYPE_VALUE_INOUT: 543 p->params[n].value.a = a; 544 p->params[n].value.b = b; 545 break; 546 default: 547 p->params[n].value.a = 0; 548 p->params[n].value.b = 0; 549 break; 550 } 551 } 552 } 553 554 /* 555 * TA invokes some TA with parameter. 556 * If some parameters are memory references: 557 * - either the memref is inside TA private RAM: TA is not allowed to expose 558 * its private RAM: use a temporary memory buffer and copy the data. 559 * - or the memref is not in the TA private RAM: 560 * - if the memref was mapped to the TA, TA is allowed to expose it. 561 * - if so, converts memref virtual address into a physical address. 562 */ 563 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 564 struct tee_ta_session *called_sess, 565 struct utee_params *callee_params, 566 struct tee_ta_param *param, 567 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 568 tee_mm_entry_t **mm) 569 { 570 size_t n; 571 TEE_Result res; 572 size_t req_mem = 0; 573 size_t s; 574 uint8_t *dst = 0; 575 tee_paddr_t dst_pa, src_pa = 0; 576 bool ta_private_memref[TEE_NUM_PARAMS]; 577 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 578 579 /* fill 'param' input struct with caller params description buffer */ 580 if (!callee_params) { 581 memset(param, 0, sizeof(*param)); 582 } else { 583 res = tee_mmu_check_access_rights(utc, 584 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 585 (tee_uaddr_t)callee_params, sizeof(struct utee_params)); 586 if (res != TEE_SUCCESS) 587 return res; 588 utee_param_to_param(param, callee_params); 589 } 590 591 if (called_sess && is_static_ta_ctx(called_sess->ctx)) { 592 /* 593 * static TA, borrow the mapping of the calling 594 * during this call. 595 */ 596 called_sess->calling_sess = sess; 597 return TEE_SUCCESS; 598 } 599 600 for (n = 0; n < TEE_NUM_PARAMS; n++) { 601 602 ta_private_memref[n] = false; 603 604 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 605 case TEE_PARAM_TYPE_MEMREF_INPUT: 606 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 607 case TEE_PARAM_TYPE_MEMREF_INOUT: 608 if (param->params[n].memref.buffer == NULL) { 609 if (param->params[n].memref.size != 0) 610 return TEE_ERROR_BAD_PARAMETERS; 611 break; 612 } 613 /* uTA cannot expose its private memory */ 614 if (tee_mmu_is_vbuf_inside_ta_private(utc, 615 param->params[n].memref.buffer, 616 param->params[n].memref.size)) { 617 618 s = ROUNDUP(param->params[n].memref.size, 619 sizeof(uint32_t)); 620 /* Check overflow */ 621 if (req_mem + s < req_mem) 622 return TEE_ERROR_BAD_PARAMETERS; 623 req_mem += s; 624 ta_private_memref[n] = true; 625 break; 626 } 627 if (tee_mmu_is_vbuf_intersect_ta_private(utc, 628 param->params[n].memref.buffer, 629 param->params[n].memref.size)) 630 return TEE_ERROR_BAD_PARAMETERS; 631 632 src_pa = virt_to_phys(param->params[n].memref.buffer); 633 if (!src_pa) 634 return TEE_ERROR_BAD_PARAMETERS; 635 636 param->param_attr[n] = tee_mmu_user_get_cache_attr( 637 utc, (void *)param->params[n].memref.buffer); 638 639 param->params[n].memref.buffer = (void *)src_pa; 640 break; 641 642 default: 643 break; 644 } 645 } 646 647 if (req_mem == 0) 648 return TEE_SUCCESS; 649 650 /* Allocate section in secure DDR */ 651 *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 652 if (*mm == NULL) { 653 DMSG("tee_mm_alloc TEE_ERROR_GENERIC"); 654 return TEE_ERROR_GENERIC; 655 } 656 657 /* Get the virtual address for the section in secure DDR */ 658 res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst); 659 if (res != TEE_SUCCESS) 660 return res; 661 dst_pa = tee_mm_get_smem(*mm); 662 663 for (n = 0; n < 4; n++) { 664 665 if (ta_private_memref[n] == false) 666 continue; 667 668 s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t)); 669 670 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 671 case TEE_PARAM_TYPE_MEMREF_INPUT: 672 case TEE_PARAM_TYPE_MEMREF_INOUT: 673 if (param->params[n].memref.buffer != NULL) { 674 res = tee_svc_copy_from_user(sess, dst, 675 param->params[n].memref.buffer, 676 param->params[n].memref.size); 677 if (res != TEE_SUCCESS) 678 return res; 679 param->param_attr[n] = 680 tee_mmu_kmap_get_cache_attr(dst); 681 param->params[n].memref.buffer = (void *)dst_pa; 682 tmp_buf_pa[n] = dst_pa; 683 dst += s; 684 dst_pa += s; 685 } 686 break; 687 688 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 689 if (param->params[n].memref.buffer != NULL) { 690 param->param_attr[n] = 691 tee_mmu_kmap_get_cache_attr(dst); 692 param->params[n].memref.buffer = (void *)dst_pa; 693 tmp_buf_pa[n] = dst_pa; 694 dst += s; 695 dst_pa += s; 696 } 697 break; 698 699 default: 700 continue; 701 } 702 } 703 704 tee_mmu_kunmap(dst, req_mem); 705 706 return TEE_SUCCESS; 707 } 708 709 /* 710 * Back from execution of service: update parameters passed from TA: 711 * If some parameters were memory references: 712 * - either the memref was temporary: copy back data and update size 713 * - or it was the original TA memref: update only the size value. 714 */ 715 static TEE_Result tee_svc_update_out_param( 716 struct tee_ta_session *sess, 717 struct tee_ta_session *called_sess, 718 struct tee_ta_param *param, 719 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 720 struct utee_params *usr_param) 721 { 722 size_t n; 723 void *p; 724 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 725 bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx); 726 727 tee_ta_set_current_session(sess); 728 729 for (n = 0; n < TEE_NUM_PARAMS; n++) { 730 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 731 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 732 case TEE_PARAM_TYPE_MEMREF_INOUT: 733 p = (void *)(uintptr_t)usr_param->vals[n * 2]; 734 735 /* outside TA private => memref is valid, update size */ 736 if (!tee_mmu_is_vbuf_inside_ta_private(utc, p, 737 param->params[n].memref.size)) { 738 usr_param->vals[n * 2 + 1] = 739 param->params[n].memref.size; 740 break; 741 } 742 743 /* 744 * If we called a kernel TA the parameters are in shared 745 * memory and no copy is needed. 746 */ 747 if (have_private_mem_map && 748 param->params[n].memref.size <= 749 usr_param->vals[n * 2 + 1]) { 750 uint8_t *src = 0; 751 TEE_Result res; 752 753 /* FIXME: TA_RAM is already mapped ! */ 754 res = tee_mmu_kmap(tmp_buf_pa[n], 755 param->params[n].memref.size, &src); 756 if (res != TEE_SUCCESS) 757 return TEE_ERROR_GENERIC; 758 759 res = tee_svc_copy_to_user(sess, p, src, 760 param->params[n].memref.size); 761 if (res != TEE_SUCCESS) 762 return res; 763 tee_mmu_kunmap(src, 764 param->params[n].memref.size); 765 766 } 767 usr_param->vals[n * 2 + 1] = 768 param->params[n].memref.size; 769 break; 770 771 case TEE_PARAM_TYPE_VALUE_OUTPUT: 772 case TEE_PARAM_TYPE_VALUE_INOUT: 773 usr_param->vals[n * 2] = param->params[n].value.a; 774 usr_param->vals[n * 2 + 1] = param->params[n].value.b; 775 break; 776 777 default: 778 continue; 779 } 780 } 781 782 return TEE_SUCCESS; 783 } 784 785 /* Called when a TA calls an OpenSession on another TA */ 786 TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 787 unsigned long cancel_req_to, 788 struct utee_params *usr_param, uint32_t *ta_sess, 789 uint32_t *ret_orig) 790 { 791 TEE_Result res; 792 uint32_t ret_o = TEE_ORIGIN_TEE; 793 struct tee_ta_session *s = NULL; 794 struct tee_ta_session *sess; 795 tee_mm_entry_t *mm_param = NULL; 796 TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 797 struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 798 TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 799 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 800 struct user_ta_ctx *utc; 801 802 if (uuid == NULL || param == NULL || clnt_id == NULL) { 803 res = TEE_ERROR_OUT_OF_MEMORY; 804 goto out_free_only; 805 } 806 807 memset(param, 0, sizeof(struct tee_ta_param)); 808 809 res = tee_ta_get_current_session(&sess); 810 if (res != TEE_SUCCESS) 811 goto out_free_only; 812 utc = to_user_ta_ctx(sess->ctx); 813 814 res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID)); 815 if (res != TEE_SUCCESS) 816 goto function_exit; 817 818 clnt_id->login = TEE_LOGIN_TRUSTED_APP; 819 memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 820 821 res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_pa, 822 &mm_param); 823 if (res != TEE_SUCCESS) 824 goto function_exit; 825 826 /* 827 * Find session of a multi session TA or a static TA 828 * In such a case, there is no need to ask the supplicant for the TA 829 * code 830 */ 831 res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 832 clnt_id, cancel_req_to, param); 833 if (res != TEE_SUCCESS) 834 goto function_exit; 835 836 res = tee_svc_update_out_param(sess, s, param, tmp_buf_pa, usr_param); 837 838 function_exit: 839 tee_ta_set_current_session(sess); 840 sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 841 842 if (mm_param != NULL) { 843 void *va = phys_to_virt(tee_mm_get_smem(mm_param), 844 MEM_AREA_KMAP_VASPACE); 845 846 if (va) 847 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 848 } 849 tee_mm_free(mm_param); 850 if (res == TEE_SUCCESS) 851 tee_svc_copy_kaddr_to_uref(sess, ta_sess, s); 852 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 853 854 out_free_only: 855 free(param); 856 free(uuid); 857 free(clnt_id); 858 return res; 859 } 860 861 TEE_Result syscall_close_ta_session(unsigned long ta_sess) 862 { 863 TEE_Result res; 864 struct tee_ta_session *sess; 865 TEE_Identity clnt_id; 866 struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess); 867 struct user_ta_ctx *utc; 868 869 res = tee_ta_get_current_session(&sess); 870 if (res != TEE_SUCCESS) 871 return res; 872 utc = to_user_ta_ctx(sess->ctx); 873 874 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 875 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 876 877 tee_ta_set_current_session(NULL); 878 res = tee_ta_close_session(s, &utc->open_sessions, &clnt_id); 879 tee_ta_set_current_session(sess); 880 return res; 881 } 882 883 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 884 unsigned long cancel_req_to, unsigned long cmd_id, 885 struct utee_params *usr_param, uint32_t *ret_orig) 886 { 887 TEE_Result res; 888 uint32_t ret_o = TEE_ORIGIN_TEE; 889 struct tee_ta_param param = { 0 }; 890 TEE_Identity clnt_id; 891 struct tee_ta_session *sess; 892 struct tee_ta_session *called_sess; 893 tee_mm_entry_t *mm_param = NULL; 894 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 895 struct user_ta_ctx *utc; 896 897 res = tee_ta_get_current_session(&sess); 898 if (res != TEE_SUCCESS) 899 return res; 900 utc = to_user_ta_ctx(sess->ctx); 901 902 called_sess = tee_ta_get_session( 903 (vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true, 904 &utc->open_sessions); 905 if (!called_sess) 906 return TEE_ERROR_BAD_PARAMETERS; 907 908 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 909 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 910 911 res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 912 tmp_buf_pa, &mm_param); 913 if (res != TEE_SUCCESS) 914 goto function_exit; 915 916 res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 917 cancel_req_to, cmd_id, ¶m); 918 919 if (res != TEE_SUCCESS) 920 goto function_exit; 921 922 res = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_pa, 923 usr_param); 924 if (res != TEE_SUCCESS) 925 goto function_exit; 926 927 function_exit: 928 tee_ta_set_current_session(sess); 929 called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 930 tee_ta_put_session(called_sess); 931 932 if (mm_param != NULL) { 933 void *va = phys_to_virt(tee_mm_get_smem(mm_param), 934 MEM_AREA_KMAP_VASPACE); 935 936 if (va) 937 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 938 } 939 tee_mm_free(mm_param); 940 if (ret_orig) 941 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 942 return res; 943 } 944 945 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 946 size_t len) 947 { 948 TEE_Result res; 949 struct tee_ta_session *s; 950 951 res = tee_ta_get_current_session(&s); 952 if (res != TEE_SUCCESS) 953 return res; 954 955 return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags, 956 (tee_uaddr_t)buf, len); 957 } 958 959 TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr, 960 const void *uaddr, size_t len) 961 { 962 TEE_Result res; 963 struct tee_ta_session *s; 964 965 if (sess == NULL) { 966 res = tee_ta_get_current_session(&s); 967 if (res != TEE_SUCCESS) 968 return res; 969 } else { 970 s = sess; 971 tee_ta_set_current_session(s); 972 } 973 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 974 TEE_MEMORY_ACCESS_READ | 975 TEE_MEMORY_ACCESS_ANY_OWNER, 976 (tee_uaddr_t)uaddr, len); 977 if (res != TEE_SUCCESS) 978 return res; 979 980 memcpy(kaddr, uaddr, len); 981 return TEE_SUCCESS; 982 } 983 984 TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr, 985 const void *kaddr, size_t len) 986 { 987 TEE_Result res; 988 struct tee_ta_session *s; 989 990 if (sess == NULL) { 991 res = tee_ta_get_current_session(&s); 992 if (res != TEE_SUCCESS) 993 return res; 994 } else { 995 s = sess; 996 tee_ta_set_current_session(s); 997 } 998 999 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 1000 TEE_MEMORY_ACCESS_WRITE | 1001 TEE_MEMORY_ACCESS_ANY_OWNER, 1002 (tee_uaddr_t)uaddr, len); 1003 if (res != TEE_SUCCESS) 1004 return res; 1005 1006 memcpy(uaddr, kaddr, len); 1007 return TEE_SUCCESS; 1008 } 1009 1010 TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess, 1011 uint32_t *uref, void *kaddr) 1012 { 1013 uint32_t ref = tee_svc_kaddr_to_uref(kaddr); 1014 1015 return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref)); 1016 } 1017 1018 static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time) 1019 { 1020 TEE_Time current_time; 1021 1022 if (s->cancel_mask) 1023 return false; 1024 1025 if (s->cancel) 1026 return true; 1027 1028 if (s->cancel_time.seconds == UINT32_MAX) 1029 return false; 1030 1031 if (curr_time != NULL) 1032 current_time = *curr_time; 1033 else if (tee_time_get_sys_time(¤t_time) != TEE_SUCCESS) 1034 return false; 1035 1036 if (current_time.seconds > s->cancel_time.seconds || 1037 (current_time.seconds == s->cancel_time.seconds && 1038 current_time.millis >= s->cancel_time.millis)) { 1039 return true; 1040 } 1041 1042 return false; 1043 } 1044 1045 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 1046 { 1047 TEE_Result res; 1048 struct tee_ta_session *s = NULL; 1049 uint32_t c; 1050 1051 res = tee_ta_get_current_session(&s); 1052 if (res != TEE_SUCCESS) 1053 return res; 1054 1055 c = session_is_cancelled(s, NULL); 1056 1057 return tee_svc_copy_to_user(s, cancel, &c, sizeof(c)); 1058 } 1059 1060 TEE_Result syscall_unmask_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 = false; 1072 return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 1073 } 1074 1075 TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 1076 { 1077 TEE_Result res; 1078 struct tee_ta_session *s = NULL; 1079 uint32_t m; 1080 1081 res = tee_ta_get_current_session(&s); 1082 if (res != TEE_SUCCESS) 1083 return res; 1084 1085 m = s->cancel_mask; 1086 s->cancel_mask = true; 1087 return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 1088 } 1089 1090 TEE_Result syscall_wait(unsigned long timeout) 1091 { 1092 TEE_Result res = TEE_SUCCESS; 1093 uint32_t mytime = 0; 1094 struct tee_ta_session *s; 1095 TEE_Time base_time; 1096 TEE_Time current_time; 1097 1098 res = tee_ta_get_current_session(&s); 1099 if (res != TEE_SUCCESS) 1100 return res; 1101 1102 res = tee_time_get_sys_time(&base_time); 1103 if (res != TEE_SUCCESS) 1104 return res; 1105 1106 while (true) { 1107 res = tee_time_get_sys_time(¤t_time); 1108 if (res != TEE_SUCCESS) 1109 return res; 1110 1111 if (session_is_cancelled(s, ¤t_time)) 1112 return TEE_ERROR_CANCEL; 1113 1114 mytime = (current_time.seconds - base_time.seconds) * 1000 + 1115 (int)current_time.millis - (int)base_time.millis; 1116 if (mytime >= timeout) 1117 return TEE_SUCCESS; 1118 1119 tee_time_wait(timeout - mytime); 1120 } 1121 1122 return res; 1123 } 1124 1125 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 1126 { 1127 TEE_Result res, res2; 1128 struct tee_ta_session *s = NULL; 1129 TEE_Time t; 1130 1131 res = tee_ta_get_current_session(&s); 1132 if (res != TEE_SUCCESS) 1133 return res; 1134 1135 switch (cat) { 1136 case UTEE_TIME_CAT_SYSTEM: 1137 res = tee_time_get_sys_time(&t); 1138 break; 1139 case UTEE_TIME_CAT_TA_PERSISTENT: 1140 res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 1141 break; 1142 case UTEE_TIME_CAT_REE: 1143 res = tee_time_get_ree_time(&t); 1144 break; 1145 default: 1146 res = TEE_ERROR_BAD_PARAMETERS; 1147 break; 1148 } 1149 1150 if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 1151 res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t)); 1152 if (res2 != TEE_SUCCESS) 1153 res = res2; 1154 } 1155 1156 return res; 1157 } 1158 1159 TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 1160 { 1161 TEE_Result res; 1162 struct tee_ta_session *s = NULL; 1163 TEE_Time t; 1164 1165 res = tee_ta_get_current_session(&s); 1166 if (res != TEE_SUCCESS) 1167 return res; 1168 1169 res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t)); 1170 if (res != TEE_SUCCESS) 1171 return res; 1172 1173 return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 1174 } 1175