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 <kernel/tee_time.h> 38 39 #include <user_ta_header.h> 40 #include <trace.h> 41 #include <kernel/trace_ta.h> 42 #include <kernel/chip_services.h> 43 #include <kernel/static_ta.h> 44 45 #include <assert.h> 46 47 vaddr_t tee_svc_uref_base; 48 49 void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused) 50 { 51 #ifdef CFG_TEE_CORE_TA_TRACE 52 char *kbuf; 53 54 if (len == 0) 55 return; 56 57 kbuf = malloc(len); 58 if (kbuf == NULL) 59 return; 60 *kbuf = '\0'; 61 62 /* log as Info/Raw traces */ 63 if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS) 64 TAMSG_RAW("%.*s", (int)len, kbuf); 65 66 free(kbuf); 67 #endif 68 } 69 70 TEE_Result syscall_not_supported(void) 71 { 72 return TEE_ERROR_NOT_SUPPORTED; 73 } 74 75 uint32_t syscall_dummy(uint32_t *a __maybe_unused) 76 { 77 DMSG("tee_svc_sys_dummy: a 0x%" PRIxVA, (vaddr_t)a); 78 return 0; 79 } 80 81 uint32_t syscall_dummy_7args(unsigned long a1 __maybe_unused, 82 unsigned long a2 __maybe_unused, 83 unsigned long a3 __maybe_unused, 84 unsigned long a4 __maybe_unused, 85 unsigned long a5 __maybe_unused, 86 unsigned long a6 __maybe_unused, 87 unsigned long a7 __maybe_unused) 88 { 89 DMSG("tee_svc_sys_dummy_7args: 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, %lx, %lx\n", 90 a1, a2, a3, a4, a5, a6, a7); 91 return 0; 92 } 93 94 uint32_t syscall_nocall(void) 95 { 96 DMSG("No syscall"); 97 return 0x1; 98 } 99 100 /* Configuration properties */ 101 /* API implementation version */ 102 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION); 103 104 /* Implementation description (implementation-dependent) */ 105 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR); 106 107 /* 108 * TA persistent time protection level 109 * 100: Persistent time based on an REE-controlled real-time clock 110 * and on the TEE Trusted Storage for the storage of origins (default). 111 * 1000: Persistent time based on a TEE-controlled real-time clock 112 * and the TEE Trusted Storage. 113 * The real-time clock MUST be out of reach of software attacks 114 * from the REE. 115 */ 116 static const uint32_t ta_time_prot_lvl = 100; 117 118 /* Elliptic Curve Cryptographic support */ 119 #ifdef CFG_CRYPTO_ECC 120 static const uint32_t crypto_ecc_en = 1; 121 #else 122 static const uint32_t crypto_ecc_en; 123 #endif 124 125 static const bool crypto_ecc_en_obsolete; 126 127 /* 128 * Trusted storage anti rollback protection level 129 * 0 (or missing): No antirollback protection (default) 130 * 100: Antirollback enforced at REE level 131 * 1000: Antirollback TEE-controlled hardware 132 */ 133 static const uint32_t ts_antiroll_prot_lvl; 134 135 /* Trusted OS implementation version */ 136 static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION); 137 138 /* Trusted OS implementation version (binary value) */ 139 static const uint32_t trustedos_impl_bin_version; /* 0 by default */ 140 141 /* Trusted OS implementation manufacturer name */ 142 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER); 143 144 /* Trusted firmware version */ 145 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION); 146 147 /* Trusted firmware version (binary value) */ 148 static const uint32_t fw_impl_bin_version; /* 0 by default */ 149 150 /* Trusted firmware manufacturer name */ 151 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER); 152 153 struct tee_props_obsolete { 154 const void *data; 155 const size_t len; 156 }; 157 158 /* Consistent with enum utee_property */ 159 const struct tee_props_obsolete tee_props_lut[] = { 160 {api_vers, sizeof(api_vers)}, 161 {descr, sizeof(descr)}, 162 {0, 0}, /* dev_id */ 163 {0, 0}, /* system time protection level */ 164 {&ta_time_prot_lvl, sizeof(ta_time_prot_lvl)}, 165 {&crypto_ecc_en_obsolete, sizeof(crypto_ecc_en_obsolete)}, 166 {&ts_antiroll_prot_lvl, sizeof(ts_antiroll_prot_lvl)}, 167 {trustedos_impl_version, sizeof(trustedos_impl_version)}, 168 {&trustedos_impl_bin_version, 169 sizeof(trustedos_impl_bin_version)}, 170 {trustedos_manufacturer, sizeof(trustedos_manufacturer)}, 171 {fw_impl_version, sizeof(fw_impl_version)}, 172 {&fw_impl_bin_version, sizeof(fw_impl_bin_version)}, 173 {fw_manufacturer, sizeof(fw_manufacturer)}, 174 {0, 0}, /* client_id */ 175 {0, 0}, /* ta_app_id */ 176 }; 177 178 enum utee_property_obsolete { 179 UTEE_PROP_TEE_API_VERSION = 0, 180 UTEE_PROP_TEE_DESCR, 181 UTEE_PROP_TEE_DEV_ID, 182 UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL, 183 UTEE_PROP_TEE_TA_TIME_PROT_LEVEL, 184 UTEE_PROP_TEE_CRYPTOGRAPHY_ECC, 185 UTEE_PROP_TEE_TS_ANTIROLL_PROT_LEVEL, 186 UTEE_PROP_TEE_TRUSTEDOS_IMPL_VERSION, 187 UTEE_PROP_TEE_TRUSTEDOS_IMPL_BIN_VERSION, 188 UTEE_PROP_TEE_TRUSTEDOS_MANUFACTURER, 189 UTEE_PROP_TEE_FW_IMPL_VERSION, 190 UTEE_PROP_TEE_FW_IMPL_BIN_VERSION, 191 UTEE_PROP_TEE_FW_MANUFACTURER, 192 UTEE_PROP_CLIENT_ID, 193 UTEE_PROP_TA_APP_ID, 194 }; 195 196 static TEE_Result get_prop_tee_dev_id(struct tee_ta_session *sess, 197 void *buf, size_t *blen) 198 { 199 TEE_Result res; 200 TEE_UUID uuid; 201 const size_t nslen = 5; 202 uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 203 'O', 'P', 'T', 'E', 'E' }; 204 205 if (*blen < sizeof(uuid)) { 206 *blen = sizeof(uuid); 207 return TEE_ERROR_SHORT_BUFFER; 208 } 209 *blen = sizeof(uuid); 210 211 if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen)) 212 return TEE_ERROR_BAD_STATE; 213 214 res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data), 215 (uint8_t *)&uuid, sizeof(uuid)); 216 if (res != TEE_SUCCESS) 217 return TEE_ERROR_BAD_STATE; 218 219 /* 220 * Changes the random value into and UUID as specifiec 221 * in RFC 4122. The magic values are from the example 222 * code in the RFC. 223 * 224 * TEE_UUID is defined slightly different from the RFC, 225 * but close enough for our purpose. 226 */ 227 228 uuid.timeHiAndVersion &= 0x0fff; 229 uuid.timeHiAndVersion |= 5 << 12; 230 231 /* uuid.clock_seq_hi_and_reserved in the RFC */ 232 uuid.clockSeqAndNode[0] &= 0x3f; 233 uuid.clockSeqAndNode[0] |= 0x80; 234 235 return tee_svc_copy_to_user(sess, buf, &uuid, sizeof(TEE_UUID)); 236 } 237 238 static TEE_Result get_prop_tee_sys_time_prot_level(struct tee_ta_session *sess, 239 void *buf, size_t *blen) 240 { 241 uint32_t prot; 242 243 if (*blen < sizeof(prot)) { 244 *blen = sizeof(prot); 245 return TEE_ERROR_SHORT_BUFFER; 246 } 247 *blen = sizeof(prot); 248 prot = tee_time_get_sys_time_protection_level(); 249 return tee_svc_copy_to_user(sess, (void *)buf, &prot, sizeof(prot)); 250 } 251 252 static TEE_Result get_prop_client_id(struct tee_ta_session *sess, 253 void *buf, size_t *blen) 254 { 255 if (*blen < sizeof(TEE_Identity)) { 256 *blen = sizeof(TEE_Identity); 257 return TEE_ERROR_SHORT_BUFFER; 258 } 259 *blen = sizeof(TEE_Identity); 260 return tee_svc_copy_to_user(sess, buf, &sess->clnt_id, 261 sizeof(TEE_Identity)); 262 } 263 264 static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess, 265 void *buf, size_t *blen) 266 { 267 if (*blen < sizeof(TEE_UUID)) { 268 *blen = sizeof(TEE_UUID); 269 return TEE_ERROR_SHORT_BUFFER; 270 } 271 *blen = sizeof(TEE_UUID); 272 return tee_svc_copy_to_user(sess, buf, &sess->ctx->uuid, 273 sizeof(TEE_UUID)); 274 } 275 276 TEE_Result syscall_get_property_obsolete(unsigned long prop, 277 void *buf, size_t blen) 278 { 279 struct tee_ta_session *sess; 280 TEE_Result res; 281 282 if (prop > ARRAY_SIZE(tee_props_lut)-1) 283 return TEE_ERROR_NOT_IMPLEMENTED; 284 285 res = tee_ta_get_current_session(&sess); 286 if (res != TEE_SUCCESS) 287 return res; 288 289 switch (prop) { 290 case UTEE_PROP_TEE_DEV_ID: 291 return get_prop_tee_dev_id(sess, buf, &blen); 292 293 case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL: 294 return get_prop_tee_sys_time_prot_level(sess, buf, &blen); 295 296 case UTEE_PROP_CLIENT_ID: 297 return get_prop_client_id(sess, buf, &blen); 298 299 case UTEE_PROP_TA_APP_ID: 300 return get_prop_ta_app_id(sess, buf, &blen); 301 302 default: 303 if (blen < tee_props_lut[prop].len) 304 return TEE_ERROR_SHORT_BUFFER; 305 return tee_svc_copy_to_user(sess, buf, tee_props_lut[prop].data, 306 tee_props_lut[prop].len); 307 } 308 } 309 310 /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */ 311 const struct tee_props tee_propset_client[] = { 312 { 313 .name = "gpd.client.identity", 314 .prop_type = USER_TA_PROP_TYPE_IDENTITY, 315 .get_prop_func = get_prop_client_id 316 }, 317 }; 318 319 /* Properties of the set TEE_PROPSET_CURRENT_TA */ 320 const struct tee_props tee_propset_ta[] = { 321 { 322 .name = "gpd.ta.appID", 323 .prop_type = USER_TA_PROP_TYPE_UUID, 324 .get_prop_func = get_prop_ta_app_id 325 }, 326 327 /* 328 * Following properties are processed directly in libutee: 329 * TA_PROP_STR_SINGLE_INSTANCE 330 * TA_PROP_STR_MULTI_SESSION 331 * TA_PROP_STR_KEEP_ALIVE 332 * TA_PROP_STR_DATA_SIZE 333 * TA_PROP_STR_STACK_SIZE 334 * TA_PROP_STR_VERSION 335 * TA_PROP_STR_DESCRIPTION 336 * USER_TA_PROP_TYPE_STRING, 337 * TA_DESCRIPTION 338 */ 339 }; 340 341 /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */ 342 const struct tee_props tee_propset_tee[] = { 343 { 344 .name = "gpd.tee.apiversion", 345 .prop_type = USER_TA_PROP_TYPE_STRING, 346 .data = api_vers, 347 .len = sizeof(api_vers), 348 }, 349 { 350 .name = "gpd.tee.description", 351 .prop_type = USER_TA_PROP_TYPE_STRING, 352 .data = descr, .len = sizeof(descr) 353 }, 354 { 355 .name = "gpd.tee.deviceID", 356 .prop_type = USER_TA_PROP_TYPE_UUID, 357 .get_prop_func = get_prop_tee_dev_id 358 }, 359 { 360 .name = "gpd.tee.systemTime.protectionLevel", 361 .prop_type = USER_TA_PROP_TYPE_U32, 362 .get_prop_func = get_prop_tee_sys_time_prot_level 363 }, 364 { 365 .name = "gpd.tee.TAPersistentTime.protectionLevel", 366 .prop_type = USER_TA_PROP_TYPE_U32, 367 .data = &ta_time_prot_lvl, 368 .len = sizeof(ta_time_prot_lvl) 369 }, 370 { 371 .name = "gpd.tee.cryptography.ecc", 372 .prop_type = USER_TA_PROP_TYPE_BOOL, 373 .data = &crypto_ecc_en, 374 .len = sizeof(crypto_ecc_en) 375 }, 376 { 377 .name = "gpd.tee.trustedStorage.antiRollback.protectionLevel", 378 .prop_type = USER_TA_PROP_TYPE_U32, 379 .data = &ts_antiroll_prot_lvl, 380 .len = sizeof(ts_antiroll_prot_lvl) 381 }, 382 { 383 .name = "gpd.tee.trustedos.implementation.version", 384 .prop_type = USER_TA_PROP_TYPE_STRING, 385 .data = trustedos_impl_version, 386 .len = sizeof(trustedos_impl_version) 387 }, 388 { 389 .name = "gpd.tee.trustedos.implementation.binaryversion", 390 .prop_type = USER_TA_PROP_TYPE_U32, 391 .data = &trustedos_impl_bin_version, 392 .len = sizeof(trustedos_impl_bin_version) 393 }, 394 { 395 .name = "gpd.tee.trustedos.manufacturer", 396 .prop_type = USER_TA_PROP_TYPE_STRING, 397 .data = trustedos_manufacturer, 398 .len = sizeof(trustedos_manufacturer) 399 }, 400 { 401 .name = "gpd.tee.firmware.implementation.version", 402 .prop_type = USER_TA_PROP_TYPE_STRING, 403 .data = fw_impl_version, 404 .len = sizeof(fw_impl_version) 405 }, 406 { 407 .name = "gpd.tee.firmware.implementation.binaryversion", 408 .prop_type = USER_TA_PROP_TYPE_U32, 409 .data = &fw_impl_bin_version, 410 .len = sizeof(fw_impl_bin_version) 411 }, 412 { 413 .name = "gpd.tee.firmware.manufacturer", 414 .prop_type = USER_TA_PROP_TYPE_STRING, 415 .data = fw_manufacturer, 416 .len = sizeof(fw_manufacturer) 417 }, 418 419 /* 420 * Following properties are processed directly in libutee: 421 * gpd.tee.arith.maxBigIntSize 422 */ 423 }; 424 425 __weak const struct tee_vendor_props vendor_props_client; 426 __weak const struct tee_vendor_props vendor_props_ta; 427 __weak const struct tee_vendor_props vendor_props_tee; 428 429 static void get_prop_set(unsigned long prop_set, 430 const struct tee_props **props, 431 size_t *size, 432 const struct tee_props **vendor_props, 433 size_t *vendor_size) 434 { 435 if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) { 436 *props = tee_propset_client; 437 *size = ARRAY_SIZE(tee_propset_client); 438 *vendor_props = vendor_props_client.props; 439 *vendor_size = vendor_props_client.len; 440 } else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) { 441 *props = tee_propset_ta; 442 *size = ARRAY_SIZE(tee_propset_ta); 443 *vendor_props = vendor_props_ta.props; 444 *vendor_size = vendor_props_ta.len; 445 } else if ((TEE_PropSetHandle)prop_set == 446 TEE_PROPSET_TEE_IMPLEMENTATION) { 447 *props = tee_propset_tee; 448 *size = ARRAY_SIZE(tee_propset_tee); 449 *vendor_props = vendor_props_tee.props; 450 *vendor_size = vendor_props_tee.len; 451 } else { 452 *props = NULL; 453 *size = 0; 454 *vendor_props = NULL; 455 *vendor_size = 0; 456 } 457 } 458 459 static const struct tee_props *get_prop_struct(unsigned long prop_set, 460 unsigned long index) 461 { 462 const struct tee_props *props; 463 const struct tee_props *vendor_props; 464 size_t size; 465 size_t vendor_size; 466 467 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 468 469 if (index < size) 470 return &(props[index]); 471 index -= size; 472 473 if (index < vendor_size) 474 return &(vendor_props[index]); 475 476 return NULL; 477 } 478 479 /* 480 * prop_set is part of TEE_PROPSET_xxx 481 * index is the index in the Property Set to retrieve 482 * if name is not NULL, the name of "index" property is returned 483 * if buf is not NULL, the property is returned 484 */ 485 TEE_Result syscall_get_property(unsigned long prop_set, 486 unsigned long index, 487 void *name, uint32_t *name_len, 488 void *buf, uint32_t *blen, 489 uint32_t *prop_type) 490 { 491 struct tee_ta_session *sess; 492 TEE_Result res; 493 TEE_Result res2; 494 const struct tee_props *prop; 495 uint32_t klen; 496 size_t klen_size; 497 uint32_t elen; 498 499 prop = get_prop_struct(prop_set, index); 500 if (!prop) 501 return TEE_ERROR_ITEM_NOT_FOUND; 502 503 res = tee_ta_get_current_session(&sess); 504 if (res != TEE_SUCCESS) 505 return res; 506 507 /* Get the property type */ 508 if (prop_type) { 509 res = tee_svc_copy_to_user(sess, prop_type, &prop->prop_type, 510 sizeof(*prop_type)); 511 if (res != TEE_SUCCESS) 512 return res; 513 } 514 515 /* Get the property */ 516 if (buf && blen) { 517 res = tee_svc_copy_from_user(sess, &klen, blen, sizeof(klen)); 518 if (res != TEE_SUCCESS) 519 return res; 520 521 if (prop->get_prop_func) { 522 klen_size = klen; 523 res = prop->get_prop_func(sess, buf, &klen_size); 524 klen = klen_size; 525 res2 = tee_svc_copy_to_user(sess, blen, 526 &klen, sizeof(*blen)); 527 } else { 528 if (klen < prop->len) 529 res = TEE_ERROR_SHORT_BUFFER; 530 else 531 res = tee_svc_copy_to_user(sess, buf, 532 prop->data, 533 prop->len); 534 res2 = tee_svc_copy_to_user(sess, blen, 535 &prop->len, sizeof(*blen)); 536 } 537 if (res2 != TEE_SUCCESS) 538 return res2; 539 if (res != TEE_SUCCESS) 540 return res; 541 } 542 543 /* Get the property name */ 544 if (name && name_len) { 545 res = tee_svc_copy_from_user(sess, &klen, 546 name_len, sizeof(klen)); 547 if (res != TEE_SUCCESS) 548 return res; 549 550 elen = strlen(prop->name) + 1; 551 552 if (klen < elen) 553 res = TEE_ERROR_SHORT_BUFFER; 554 else 555 res = tee_svc_copy_to_user(sess, name, 556 prop->name, elen); 557 res2 = tee_svc_copy_to_user(sess, name_len, 558 &elen, sizeof(*name_len)); 559 if (res2 != TEE_SUCCESS) 560 return res2; 561 if (res != TEE_SUCCESS) 562 return res; 563 } 564 565 return res; 566 } 567 568 /* 569 * prop_set is part of TEE_PROPSET_xxx 570 */ 571 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set, 572 void *name, 573 unsigned long name_len, 574 uint32_t *index) 575 { 576 TEE_Result res; 577 struct tee_ta_session *sess; 578 const struct tee_props *props; 579 size_t size; 580 const struct tee_props *vendor_props; 581 size_t vendor_size; 582 char *kname = 0; 583 uint32_t i; 584 585 get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 586 if (!props) 587 return TEE_ERROR_ITEM_NOT_FOUND; 588 589 res = tee_ta_get_current_session(&sess); 590 if (res != TEE_SUCCESS) 591 goto out; 592 593 if (!name || !name_len) { 594 res = TEE_ERROR_BAD_PARAMETERS; 595 goto out; 596 } 597 598 kname = malloc(name_len); 599 if (!kname) 600 return TEE_ERROR_OUT_OF_MEMORY; 601 res = tee_svc_copy_from_user(sess, kname, name, name_len); 602 if (res != TEE_SUCCESS) 603 goto out; 604 kname[name_len - 1] = 0; 605 606 res = TEE_ERROR_ITEM_NOT_FOUND; 607 for (i = 0; i < size; i++) { 608 if (!strcmp(kname, props[i].name)) { 609 res = tee_svc_copy_to_user(sess, index, &i, 610 sizeof(*index)); 611 goto out; 612 } 613 } 614 for (i = size; i < size + vendor_size; i++) { 615 if (!strcmp(kname, vendor_props[i - size].name)) { 616 res = tee_svc_copy_to_user(sess, index, &i, 617 sizeof(*index)); 618 goto out; 619 } 620 } 621 622 out: 623 free(kname); 624 return res; 625 } 626 627 static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up) 628 { 629 size_t n; 630 uint32_t types = up->types; 631 632 p->types = types; 633 for (n = 0; n < TEE_NUM_PARAMS; n++) { 634 uintptr_t a = up->vals[n * 2]; 635 size_t b = up->vals[n * 2 + 1]; 636 637 switch (TEE_PARAM_TYPE_GET(types, n)) { 638 case TEE_PARAM_TYPE_MEMREF_INPUT: 639 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 640 case TEE_PARAM_TYPE_MEMREF_INOUT: 641 p->params[n].memref.buffer = (void *)a; 642 p->params[n].memref.size = b; 643 break; 644 case TEE_PARAM_TYPE_VALUE_INPUT: 645 case TEE_PARAM_TYPE_VALUE_INOUT: 646 p->params[n].value.a = a; 647 p->params[n].value.b = b; 648 break; 649 default: 650 p->params[n].value.a = 0; 651 p->params[n].value.b = 0; 652 break; 653 } 654 } 655 } 656 657 /* 658 * TA invokes some TA with parameter. 659 * If some parameters are memory references: 660 * - either the memref is inside TA private RAM: TA is not allowed to expose 661 * its private RAM: use a temporary memory buffer and copy the data. 662 * - or the memref is not in the TA private RAM: 663 * - if the memref was mapped to the TA, TA is allowed to expose it. 664 * - if so, converts memref virtual address into a physical address. 665 */ 666 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 667 struct tee_ta_session *called_sess, 668 struct utee_params *callee_params, 669 struct tee_ta_param *param, 670 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 671 tee_mm_entry_t **mm) 672 { 673 size_t n; 674 TEE_Result res; 675 size_t req_mem = 0; 676 size_t s; 677 uint8_t *dst = 0; 678 tee_paddr_t dst_pa, src_pa = 0; 679 bool ta_private_memref[TEE_NUM_PARAMS]; 680 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 681 682 /* fill 'param' input struct with caller params description buffer */ 683 if (!callee_params) { 684 memset(param, 0, sizeof(*param)); 685 } else { 686 res = tee_mmu_check_access_rights(utc, 687 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 688 (tee_uaddr_t)callee_params, sizeof(struct utee_params)); 689 if (res != TEE_SUCCESS) 690 return res; 691 utee_param_to_param(param, callee_params); 692 } 693 694 if (called_sess && is_static_ta_ctx(called_sess->ctx)) { 695 /* 696 * static TA, borrow the mapping of the calling 697 * during this call. 698 */ 699 called_sess->calling_sess = sess; 700 return TEE_SUCCESS; 701 } 702 703 for (n = 0; n < TEE_NUM_PARAMS; n++) { 704 705 ta_private_memref[n] = false; 706 707 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 708 case TEE_PARAM_TYPE_MEMREF_INPUT: 709 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 710 case TEE_PARAM_TYPE_MEMREF_INOUT: 711 if (param->params[n].memref.buffer == NULL) { 712 if (param->params[n].memref.size != 0) 713 return TEE_ERROR_BAD_PARAMETERS; 714 break; 715 } 716 /* uTA cannot expose its private memory */ 717 if (tee_mmu_is_vbuf_inside_ta_private(utc, 718 param->params[n].memref.buffer, 719 param->params[n].memref.size)) { 720 721 s = ROUNDUP(param->params[n].memref.size, 722 sizeof(uint32_t)); 723 /* Check overflow */ 724 if (req_mem + s < req_mem) 725 return TEE_ERROR_BAD_PARAMETERS; 726 req_mem += s; 727 ta_private_memref[n] = true; 728 break; 729 } 730 if (tee_mmu_is_vbuf_intersect_ta_private(utc, 731 param->params[n].memref.buffer, 732 param->params[n].memref.size)) 733 return TEE_ERROR_BAD_PARAMETERS; 734 735 if (tee_mmu_user_va2pa(utc, 736 (void *)param->params[n].memref.buffer, 737 &src_pa) != TEE_SUCCESS) 738 return TEE_ERROR_BAD_PARAMETERS; 739 740 param->param_attr[n] = tee_mmu_user_get_cache_attr( 741 utc, (void *)param->params[n].memref.buffer); 742 743 param->params[n].memref.buffer = (void *)src_pa; 744 break; 745 746 default: 747 break; 748 } 749 } 750 751 if (req_mem == 0) 752 return TEE_SUCCESS; 753 754 /* Allocate section in secure DDR */ 755 *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 756 if (*mm == NULL) { 757 DMSG("tee_mm_alloc TEE_ERROR_GENERIC"); 758 return TEE_ERROR_GENERIC; 759 } 760 761 /* Get the virtual address for the section in secure DDR */ 762 res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst); 763 if (res != TEE_SUCCESS) 764 return res; 765 dst_pa = tee_mm_get_smem(*mm); 766 767 for (n = 0; n < 4; n++) { 768 769 if (ta_private_memref[n] == false) 770 continue; 771 772 s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t)); 773 774 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 775 case TEE_PARAM_TYPE_MEMREF_INPUT: 776 case TEE_PARAM_TYPE_MEMREF_INOUT: 777 if (param->params[n].memref.buffer != NULL) { 778 res = tee_svc_copy_from_user(sess, dst, 779 param->params[n].memref.buffer, 780 param->params[n].memref.size); 781 if (res != TEE_SUCCESS) 782 return res; 783 param->param_attr[n] = 784 tee_mmu_kmap_get_cache_attr(dst); 785 param->params[n].memref.buffer = (void *)dst_pa; 786 tmp_buf_pa[n] = dst_pa; 787 dst += s; 788 dst_pa += s; 789 } 790 break; 791 792 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 793 if (param->params[n].memref.buffer != NULL) { 794 param->param_attr[n] = 795 tee_mmu_kmap_get_cache_attr(dst); 796 param->params[n].memref.buffer = (void *)dst_pa; 797 tmp_buf_pa[n] = dst_pa; 798 dst += s; 799 dst_pa += s; 800 } 801 break; 802 803 default: 804 continue; 805 } 806 } 807 808 tee_mmu_kunmap(dst, req_mem); 809 810 return TEE_SUCCESS; 811 } 812 813 /* 814 * Back from execution of service: update parameters passed from TA: 815 * If some parameters were memory references: 816 * - either the memref was temporary: copy back data and update size 817 * - or it was the original TA memref: update only the size value. 818 */ 819 static TEE_Result tee_svc_update_out_param( 820 struct tee_ta_session *sess, 821 struct tee_ta_session *called_sess, 822 struct tee_ta_param *param, 823 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 824 struct utee_params *usr_param) 825 { 826 size_t n; 827 void *p; 828 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 829 bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx); 830 831 tee_ta_set_current_session(sess); 832 833 for (n = 0; n < TEE_NUM_PARAMS; n++) { 834 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 835 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 836 case TEE_PARAM_TYPE_MEMREF_INOUT: 837 p = (void *)(uintptr_t)usr_param->vals[n * 2]; 838 839 /* outside TA private => memref is valid, update size */ 840 if (!tee_mmu_is_vbuf_inside_ta_private(utc, p, 841 param->params[n].memref.size)) { 842 usr_param->vals[n * 2 + 1] = 843 param->params[n].memref.size; 844 break; 845 } 846 847 /* 848 * If we called a kernel TA the parameters are in shared 849 * memory and no copy is needed. 850 */ 851 if (have_private_mem_map && 852 param->params[n].memref.size <= 853 usr_param->vals[n * 2 + 1]) { 854 uint8_t *src = 0; 855 TEE_Result res; 856 857 /* FIXME: TA_RAM is already mapped ! */ 858 res = tee_mmu_kmap(tmp_buf_pa[n], 859 param->params[n].memref.size, &src); 860 if (res != TEE_SUCCESS) 861 return TEE_ERROR_GENERIC; 862 863 res = tee_svc_copy_to_user(sess, p, src, 864 param->params[n].memref.size); 865 if (res != TEE_SUCCESS) 866 return res; 867 tee_mmu_kunmap(src, 868 param->params[n].memref.size); 869 870 } 871 usr_param->vals[n * 2 + 1] = 872 param->params[n].memref.size; 873 break; 874 875 case TEE_PARAM_TYPE_VALUE_OUTPUT: 876 case TEE_PARAM_TYPE_VALUE_INOUT: 877 usr_param->vals[n * 2] = param->params[n].value.a; 878 usr_param->vals[n * 2 + 1] = param->params[n].value.b; 879 break; 880 881 default: 882 continue; 883 } 884 } 885 886 return TEE_SUCCESS; 887 } 888 889 /* Called when a TA calls an OpenSession on another TA */ 890 TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 891 unsigned long cancel_req_to, 892 struct utee_params *usr_param, uint32_t *ta_sess, 893 uint32_t *ret_orig) 894 { 895 TEE_Result res; 896 uint32_t ret_o = TEE_ORIGIN_TEE; 897 struct tee_ta_session *s = NULL; 898 struct tee_ta_session *sess; 899 tee_mm_entry_t *mm_param = NULL; 900 TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 901 struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 902 TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 903 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 904 struct user_ta_ctx *utc; 905 906 if (uuid == NULL || param == NULL || clnt_id == NULL) { 907 res = TEE_ERROR_OUT_OF_MEMORY; 908 goto out_free_only; 909 } 910 911 memset(param, 0, sizeof(struct tee_ta_param)); 912 913 res = tee_ta_get_current_session(&sess); 914 if (res != TEE_SUCCESS) 915 goto out_free_only; 916 utc = to_user_ta_ctx(sess->ctx); 917 918 res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID)); 919 if (res != TEE_SUCCESS) 920 goto function_exit; 921 922 clnt_id->login = TEE_LOGIN_TRUSTED_APP; 923 memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 924 925 res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_pa, 926 &mm_param); 927 if (res != TEE_SUCCESS) 928 goto function_exit; 929 930 /* 931 * Find session of a multi session TA or a static TA 932 * In such a case, there is no need to ask the supplicant for the TA 933 * code 934 */ 935 res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 936 clnt_id, cancel_req_to, param); 937 if (res != TEE_SUCCESS) 938 goto function_exit; 939 940 res = tee_svc_update_out_param(sess, s, param, tmp_buf_pa, usr_param); 941 942 function_exit: 943 tee_ta_set_current_session(sess); 944 sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 945 946 if (mm_param != NULL) { 947 TEE_Result res2; 948 void *va = 0; 949 950 res2 = 951 tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 952 if (res2 == TEE_SUCCESS) 953 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 954 } 955 tee_mm_free(mm_param); 956 if (res == TEE_SUCCESS) 957 tee_svc_copy_kaddr_to_uref(sess, ta_sess, s); 958 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 959 960 out_free_only: 961 free(param); 962 free(uuid); 963 free(clnt_id); 964 return res; 965 } 966 967 TEE_Result syscall_close_ta_session(unsigned long ta_sess) 968 { 969 TEE_Result res; 970 struct tee_ta_session *sess; 971 TEE_Identity clnt_id; 972 struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess); 973 struct user_ta_ctx *utc; 974 975 res = tee_ta_get_current_session(&sess); 976 if (res != TEE_SUCCESS) 977 return res; 978 utc = to_user_ta_ctx(sess->ctx); 979 980 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 981 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 982 983 tee_ta_set_current_session(NULL); 984 res = tee_ta_close_session(s, &utc->open_sessions, &clnt_id); 985 tee_ta_set_current_session(sess); 986 return res; 987 } 988 989 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 990 unsigned long cancel_req_to, unsigned long cmd_id, 991 struct utee_params *usr_param, uint32_t *ret_orig) 992 { 993 TEE_Result res; 994 uint32_t ret_o = TEE_ORIGIN_TEE; 995 struct tee_ta_param param = { 0 }; 996 TEE_Identity clnt_id; 997 struct tee_ta_session *sess; 998 struct tee_ta_session *called_sess; 999 tee_mm_entry_t *mm_param = NULL; 1000 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 1001 struct user_ta_ctx *utc; 1002 1003 res = tee_ta_get_current_session(&sess); 1004 if (res != TEE_SUCCESS) 1005 return res; 1006 utc = to_user_ta_ctx(sess->ctx); 1007 1008 called_sess = tee_ta_get_session( 1009 (vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true, 1010 &utc->open_sessions); 1011 if (!called_sess) 1012 return TEE_ERROR_BAD_PARAMETERS; 1013 1014 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 1015 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 1016 1017 res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 1018 tmp_buf_pa, &mm_param); 1019 if (res != TEE_SUCCESS) 1020 goto function_exit; 1021 1022 res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 1023 cancel_req_to, cmd_id, ¶m); 1024 1025 if (res != TEE_SUCCESS) 1026 goto function_exit; 1027 1028 res = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_pa, 1029 usr_param); 1030 if (res != TEE_SUCCESS) 1031 goto function_exit; 1032 1033 function_exit: 1034 tee_ta_set_current_session(sess); 1035 called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 1036 tee_ta_put_session(called_sess); 1037 1038 if (mm_param != NULL) { 1039 TEE_Result res2; 1040 void *va = 0; 1041 1042 res2 = 1043 tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 1044 if (res2 == TEE_SUCCESS) 1045 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 1046 } 1047 tee_mm_free(mm_param); 1048 if (ret_orig) 1049 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 1050 return res; 1051 } 1052 1053 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 1054 size_t len) 1055 { 1056 TEE_Result res; 1057 struct tee_ta_session *s; 1058 1059 res = tee_ta_get_current_session(&s); 1060 if (res != TEE_SUCCESS) 1061 return res; 1062 1063 return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags, 1064 (tee_uaddr_t)buf, len); 1065 } 1066 1067 TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr, 1068 const void *uaddr, size_t len) 1069 { 1070 TEE_Result res; 1071 struct tee_ta_session *s; 1072 1073 if (sess == NULL) { 1074 res = tee_ta_get_current_session(&s); 1075 if (res != TEE_SUCCESS) 1076 return res; 1077 } else { 1078 s = sess; 1079 tee_ta_set_current_session(s); 1080 } 1081 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 1082 TEE_MEMORY_ACCESS_READ | 1083 TEE_MEMORY_ACCESS_ANY_OWNER, 1084 (tee_uaddr_t)uaddr, len); 1085 if (res != TEE_SUCCESS) 1086 return res; 1087 1088 memcpy(kaddr, uaddr, len); 1089 return TEE_SUCCESS; 1090 } 1091 1092 TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr, 1093 const void *kaddr, size_t len) 1094 { 1095 TEE_Result res; 1096 struct tee_ta_session *s; 1097 1098 if (sess == NULL) { 1099 res = tee_ta_get_current_session(&s); 1100 if (res != TEE_SUCCESS) 1101 return res; 1102 } else { 1103 s = sess; 1104 tee_ta_set_current_session(s); 1105 } 1106 1107 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 1108 TEE_MEMORY_ACCESS_WRITE | 1109 TEE_MEMORY_ACCESS_ANY_OWNER, 1110 (tee_uaddr_t)uaddr, len); 1111 if (res != TEE_SUCCESS) 1112 return res; 1113 1114 memcpy(uaddr, kaddr, len); 1115 return TEE_SUCCESS; 1116 } 1117 1118 TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess, 1119 uint32_t *uref, void *kaddr) 1120 { 1121 uint32_t ref = tee_svc_kaddr_to_uref(kaddr); 1122 1123 return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref)); 1124 } 1125 1126 static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time) 1127 { 1128 TEE_Time current_time; 1129 1130 if (s->cancel_mask) 1131 return false; 1132 1133 if (s->cancel) 1134 return true; 1135 1136 if (s->cancel_time.seconds == UINT32_MAX) 1137 return false; 1138 1139 if (curr_time != NULL) 1140 current_time = *curr_time; 1141 else if (tee_time_get_sys_time(¤t_time) != TEE_SUCCESS) 1142 return false; 1143 1144 if (current_time.seconds > s->cancel_time.seconds || 1145 (current_time.seconds == s->cancel_time.seconds && 1146 current_time.millis >= s->cancel_time.millis)) { 1147 return true; 1148 } 1149 1150 return false; 1151 } 1152 1153 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 1154 { 1155 TEE_Result res; 1156 struct tee_ta_session *s = NULL; 1157 uint32_t c; 1158 1159 res = tee_ta_get_current_session(&s); 1160 if (res != TEE_SUCCESS) 1161 return res; 1162 1163 c = session_is_cancelled(s, NULL); 1164 1165 return tee_svc_copy_to_user(s, cancel, &c, sizeof(c)); 1166 } 1167 1168 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 1169 { 1170 TEE_Result res; 1171 struct tee_ta_session *s = NULL; 1172 uint32_t m; 1173 1174 res = tee_ta_get_current_session(&s); 1175 if (res != TEE_SUCCESS) 1176 return res; 1177 1178 m = s->cancel_mask; 1179 s->cancel_mask = false; 1180 return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 1181 } 1182 1183 TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 1184 { 1185 TEE_Result res; 1186 struct tee_ta_session *s = NULL; 1187 uint32_t m; 1188 1189 res = tee_ta_get_current_session(&s); 1190 if (res != TEE_SUCCESS) 1191 return res; 1192 1193 m = s->cancel_mask; 1194 s->cancel_mask = true; 1195 return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 1196 } 1197 1198 TEE_Result syscall_wait(unsigned long timeout) 1199 { 1200 TEE_Result res = TEE_SUCCESS; 1201 uint32_t mytime = 0; 1202 struct tee_ta_session *s; 1203 TEE_Time base_time; 1204 TEE_Time current_time; 1205 1206 res = tee_ta_get_current_session(&s); 1207 if (res != TEE_SUCCESS) 1208 return res; 1209 1210 res = tee_time_get_sys_time(&base_time); 1211 if (res != TEE_SUCCESS) 1212 return res; 1213 1214 while (true) { 1215 res = tee_time_get_sys_time(¤t_time); 1216 if (res != TEE_SUCCESS) 1217 return res; 1218 1219 if (session_is_cancelled(s, ¤t_time)) 1220 return TEE_ERROR_CANCEL; 1221 1222 mytime = (current_time.seconds - base_time.seconds) * 1000 + 1223 (int)current_time.millis - (int)base_time.millis; 1224 if (mytime >= timeout) 1225 return TEE_SUCCESS; 1226 1227 tee_time_wait(timeout - mytime); 1228 } 1229 1230 return res; 1231 } 1232 1233 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 1234 { 1235 TEE_Result res, res2; 1236 struct tee_ta_session *s = NULL; 1237 TEE_Time t; 1238 1239 res = tee_ta_get_current_session(&s); 1240 if (res != TEE_SUCCESS) 1241 return res; 1242 1243 switch (cat) { 1244 case UTEE_TIME_CAT_SYSTEM: 1245 res = tee_time_get_sys_time(&t); 1246 break; 1247 case UTEE_TIME_CAT_TA_PERSISTENT: 1248 res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 1249 break; 1250 case UTEE_TIME_CAT_REE: 1251 res = tee_time_get_ree_time(&t); 1252 break; 1253 default: 1254 res = TEE_ERROR_BAD_PARAMETERS; 1255 break; 1256 } 1257 1258 if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 1259 res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t)); 1260 if (res2 != TEE_SUCCESS) 1261 res = res2; 1262 } 1263 1264 return res; 1265 } 1266 1267 TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 1268 { 1269 TEE_Result res; 1270 struct tee_ta_session *s = NULL; 1271 TEE_Time t; 1272 1273 res = tee_ta_get_current_session(&s); 1274 if (res != TEE_SUCCESS) 1275 return res; 1276 1277 res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t)); 1278 if (res != TEE_SUCCESS) 1279 return res; 1280 1281 return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 1282 } 1283