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