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