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 28 #include <kernel/mutex.h> 29 #include <kernel/tee_misc.h> 30 #include <kernel/tee_ta_manager.h> 31 #include <mm/tee_mmu.h> 32 #include <string.h> 33 #include <tee_api_defines_extensions.h> 34 #include <tee_api_defines.h> 35 #include <tee/tee_fs_defs.h> 36 #include <tee/tee_fs.h> 37 #include <tee/tee_obj.h> 38 #include <tee/tee_pobj.h> 39 #include <tee/tee_svc_cryp.h> 40 #include <tee/tee_svc.h> 41 #include <tee/tee_svc_storage.h> 42 #include <trace.h> 43 44 /* 45 * Returns the appropriate tee_file_operations for the specified storage ID. 46 * The value TEE_STORAGE_PRIVATE will select the REE FS if available, otherwise 47 * RPMB. 48 */ 49 static const struct tee_file_operations *file_ops(uint32_t storage_id) 50 { 51 52 switch (storage_id) { 53 case TEE_STORAGE_PRIVATE: 54 #if defined(CFG_REE_FS) 55 return &ree_fs_ops; 56 #elif defined(CFG_RPMB_FS) 57 return &rpmb_fs_ops; 58 #elif defined(CFG_SQL_FS) 59 return &sql_fs_ops; 60 #else 61 #error At least one filesystem must be enabled. 62 #endif 63 #ifdef CFG_REE_FS 64 case TEE_STORAGE_PRIVATE_REE: 65 return &ree_fs_ops; 66 #endif 67 #ifdef CFG_RPMB_FS 68 case TEE_STORAGE_PRIVATE_RPMB: 69 return &rpmb_fs_ops; 70 #endif 71 #ifdef CFG_SQL_FS 72 case TEE_STORAGE_PRIVATE_SQL: 73 return &sql_fs_ops; 74 #endif 75 default: 76 return NULL; 77 } 78 } 79 80 /* SSF (Secure Storage File version 00 */ 81 #define TEE_SVC_STORAGE_MAGIC 0x53534600 82 83 /* Header of GP formated secure storage files */ 84 struct tee_svc_storage_head { 85 uint32_t magic; 86 uint32_t head_size; 87 uint32_t meta_size; 88 uint32_t ds_size; 89 uint32_t keySize; 90 uint32_t maxKeySize; 91 uint32_t objectUsage; 92 uint32_t objectType; 93 uint32_t have_attrs; 94 }; 95 96 struct tee_storage_enum { 97 TAILQ_ENTRY(tee_storage_enum) link; 98 struct tee_fs_dir *dir; 99 const struct tee_file_operations *fops; 100 }; 101 102 /* 103 * Protect TA storage directory: avoid race conditions between (create 104 * directory + create file) and (remove directory) 105 */ 106 static struct mutex ta_dir_mutex = MUTEX_INITIALIZER; 107 108 static TEE_Result tee_svc_storage_get_enum(struct user_ta_ctx *utc, 109 uint32_t enum_id, 110 struct tee_storage_enum **e_out) 111 { 112 struct tee_storage_enum *e; 113 114 TAILQ_FOREACH(e, &utc->storage_enums, link) { 115 if (enum_id == (vaddr_t)e) { 116 *e_out = e; 117 return TEE_SUCCESS; 118 } 119 } 120 return TEE_ERROR_BAD_PARAMETERS; 121 } 122 123 static TEE_Result tee_svc_close_enum(struct user_ta_ctx *utc, 124 struct tee_storage_enum *e) 125 { 126 if (e == NULL || utc == NULL) 127 return TEE_ERROR_BAD_PARAMETERS; 128 129 TAILQ_REMOVE(&utc->storage_enums, e, link); 130 131 if (e->fops) 132 e->fops->closedir(e->dir); 133 134 e->dir = NULL; 135 e->fops = NULL; 136 137 free(e); 138 139 return TEE_SUCCESS; 140 } 141 142 /* "/TA_uuid/object_id" or "/TA_uuid/.object_id" */ 143 char *tee_svc_storage_create_filename(struct tee_ta_session *sess, 144 void *object_id, 145 uint32_t object_id_len, 146 bool transient) 147 { 148 uint8_t *file; 149 uint32_t pos = 0; 150 uint32_t hslen = 1 /* Leading slash */ 151 + TEE_B2HS_HSBUF_SIZE(sizeof(TEE_UUID) + object_id_len) 152 + 1; /* Intermediate slash */ 153 154 /* +1 for the '.' (temporary persistent object) */ 155 if (transient) 156 hslen++; 157 158 file = malloc(hslen); 159 if (!file) 160 return NULL; 161 162 file[pos++] = '/'; 163 pos += tee_b2hs((uint8_t *)&sess->ctx->uuid, &file[pos], 164 sizeof(TEE_UUID), hslen); 165 file[pos++] = '/'; 166 167 if (transient) 168 file[pos++] = '.'; 169 170 tee_b2hs(object_id, file + pos, object_id_len, hslen - pos); 171 172 return (char *)file; 173 } 174 175 /* "/TA_uuid" */ 176 char *tee_svc_storage_create_dirname(struct tee_ta_session *sess) 177 { 178 uint8_t *dir; 179 uint32_t hslen = TEE_B2HS_HSBUF_SIZE(sizeof(TEE_UUID)) + 1; 180 181 dir = malloc(hslen); 182 if (!dir) 183 return NULL; 184 185 dir[0] = '/'; 186 tee_b2hs((uint8_t *)&sess->ctx->uuid, dir + 1, sizeof(TEE_UUID), 187 hslen); 188 189 return (char *)dir; 190 } 191 192 static TEE_Result tee_svc_storage_remove_corrupt_obj( 193 struct tee_ta_session *sess, 194 struct tee_obj *o) 195 { 196 TEE_Result res; 197 char *file = NULL; 198 const struct tee_file_operations *fops = o->pobj->fops; 199 200 file = tee_svc_storage_create_filename(sess, 201 o->pobj->obj_id, 202 o->pobj->obj_id_len, 203 false); 204 if (file == NULL) { 205 res = TEE_ERROR_OUT_OF_MEMORY; 206 goto exit; 207 } 208 209 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 210 fops->remove(file); 211 free(file); 212 213 res = TEE_SUCCESS; 214 215 exit: 216 return res; 217 } 218 219 static TEE_Result tee_svc_storage_read_head(struct tee_ta_session *sess, 220 struct tee_obj *o) 221 { 222 TEE_Result res = TEE_SUCCESS; 223 size_t bytes; 224 struct tee_svc_storage_head head; 225 char *file = NULL; 226 const struct tee_file_operations *fops; 227 void *attr = NULL; 228 229 if (o == NULL || o->pobj == NULL) 230 return TEE_ERROR_BAD_PARAMETERS; 231 232 fops = o->pobj->fops; 233 234 file = tee_svc_storage_create_filename(sess, 235 o->pobj->obj_id, 236 o->pobj->obj_id_len, 237 false); 238 if (file == NULL) { 239 res = TEE_ERROR_OUT_OF_MEMORY; 240 goto exit; 241 } 242 243 assert(!o->fh); 244 res = fops->open(file, &o->fh); 245 if (res != TEE_SUCCESS) 246 goto exit; 247 248 /* read head */ 249 bytes = sizeof(struct tee_svc_storage_head); 250 res = fops->read(o->fh, &head, &bytes); 251 if (res != TEE_SUCCESS) { 252 if (res == TEE_ERROR_CORRUPT_OBJECT) 253 EMSG("Head corrupt\n"); 254 goto exit; 255 } 256 257 if (bytes != sizeof(struct tee_svc_storage_head)) { 258 res = TEE_ERROR_BAD_FORMAT; 259 goto exit; 260 } 261 262 res = tee_obj_set_type(o, head.objectType, head.maxKeySize); 263 if (res != TEE_SUCCESS) 264 goto exit; 265 266 if (head.meta_size) { 267 attr = malloc(head.meta_size); 268 if (!attr) { 269 res = TEE_ERROR_OUT_OF_MEMORY; 270 goto exit; 271 } 272 273 /* read meta */ 274 bytes = head.meta_size; 275 res = fops->read(o->fh, attr, &bytes); 276 if (res != TEE_SUCCESS || bytes != head.meta_size) { 277 res = TEE_ERROR_CORRUPT_OBJECT; 278 goto exit; 279 } 280 } 281 282 res = tee_obj_attr_from_binary(o, attr, head.meta_size); 283 if (res != TEE_SUCCESS) 284 goto exit; 285 286 o->info.dataSize = head.ds_size; 287 o->info.keySize = head.keySize; 288 o->info.objectUsage = head.objectUsage; 289 o->info.objectType = head.objectType; 290 o->have_attrs = head.have_attrs; 291 292 exit: 293 free(attr); 294 free(file); 295 296 return res; 297 } 298 299 static TEE_Result tee_svc_storage_update_head(struct tee_obj *o, 300 uint32_t ds_size) 301 { 302 TEE_Result res; 303 const struct tee_file_operations *fops; 304 int32_t old_off; 305 306 fops = o->pobj->fops; 307 308 /* save original offset */ 309 res = fops->seek(o->fh, 0, TEE_DATA_SEEK_CUR, &old_off); 310 if (res != TEE_SUCCESS) 311 return res; 312 313 /* update head.ds_size */ 314 res = fops->seek(o->fh, offsetof(struct tee_svc_storage_head, 315 ds_size), TEE_DATA_SEEK_SET, NULL); 316 if (res != TEE_SUCCESS) 317 return res; 318 319 res = fops->write(o->fh, &ds_size, sizeof(uint32_t)); 320 if (res != TEE_SUCCESS) 321 return res; 322 323 /* restore original offset */ 324 res = fops->seek(o->fh, old_off, TEE_DATA_SEEK_SET, NULL); 325 return res; 326 } 327 328 static TEE_Result tee_svc_storage_init_file(struct tee_ta_session *sess, 329 struct tee_obj *o, 330 struct tee_obj *attr_o, void *data, 331 uint32_t len) 332 { 333 TEE_Result res = TEE_SUCCESS; 334 struct tee_svc_storage_head head; 335 char *tmpfile = NULL; 336 const struct tee_file_operations *fops; 337 void *attr = NULL; 338 size_t attr_size = 0; 339 340 if (o == NULL || o->pobj == NULL) 341 return TEE_ERROR_BAD_PARAMETERS; 342 343 fops = o->pobj->fops; 344 345 /* create temporary persistent object filename */ 346 tmpfile = tee_svc_storage_create_filename(sess, 347 o->pobj->obj_id, 348 o->pobj->obj_id_len, 349 true); 350 351 if (tmpfile == NULL) { 352 res = TEE_ERROR_OUT_OF_MEMORY; 353 goto exit; 354 } 355 356 mutex_lock(&ta_dir_mutex); 357 res = fops->create(tmpfile, &o->fh); 358 mutex_unlock(&ta_dir_mutex); 359 if (res != TEE_SUCCESS) 360 goto exit; 361 362 if (attr_o) { 363 res = tee_obj_set_type(o, attr_o->info.objectType, 364 attr_o->info.maxKeySize); 365 if (res != TEE_SUCCESS) 366 goto exit; 367 res = tee_obj_attr_copy_from(o, attr_o); 368 if (res != TEE_SUCCESS) 369 goto exit; 370 o->have_attrs = attr_o->have_attrs; 371 o->info.objectUsage = attr_o->info.objectUsage; 372 o->info.keySize = attr_o->info.keySize; 373 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 374 if (res != TEE_SUCCESS) 375 goto exit; 376 if (attr_size) { 377 attr = malloc(attr_size); 378 if (!attr) { 379 res = TEE_ERROR_OUT_OF_MEMORY; 380 goto exit; 381 } 382 res = tee_obj_attr_to_binary(o, attr, &attr_size); 383 if (res != TEE_SUCCESS) 384 goto exit; 385 } 386 } else { 387 res = tee_obj_set_type(o, TEE_TYPE_DATA, 0); 388 if (res != TEE_SUCCESS) 389 goto exit; 390 } 391 392 /* write head */ 393 head.magic = TEE_SVC_STORAGE_MAGIC; 394 head.head_size = sizeof(struct tee_svc_storage_head); 395 head.meta_size = attr_size; 396 head.ds_size = len; 397 head.keySize = o->info.keySize; 398 head.maxKeySize = o->info.maxKeySize; 399 head.objectUsage = o->info.objectUsage; 400 head.objectType = o->info.objectType; 401 head.have_attrs = o->have_attrs; 402 403 /* write head */ 404 res = fops->write(o->fh, &head, sizeof(struct tee_svc_storage_head)); 405 if (res != TEE_SUCCESS) 406 goto exit; 407 408 /* write meta */ 409 res = fops->write(o->fh, attr, attr_size); 410 if (res != TEE_SUCCESS) 411 goto exit; 412 413 /* write init data */ 414 o->info.dataSize = len; 415 416 /* write data to fs if needed */ 417 if (data && len) 418 res = fops->write(o->fh, data, len); 419 420 exit: 421 free(attr); 422 free(tmpfile); 423 fops->close(&o->fh); 424 425 return res; 426 } 427 428 TEE_Result syscall_storage_obj_open(unsigned long storage_id, void *object_id, 429 size_t object_id_len, unsigned long flags, 430 uint32_t *obj) 431 { 432 TEE_Result res; 433 struct tee_ta_session *sess; 434 struct tee_obj *o = NULL; 435 char *file = NULL; 436 struct tee_pobj *po = NULL; 437 struct user_ta_ctx *utc; 438 const struct tee_file_operations *fops = file_ops(storage_id); 439 size_t attr_size; 440 441 if (!fops) { 442 res = TEE_ERROR_ITEM_NOT_FOUND; 443 goto exit; 444 } 445 446 if (object_id_len > TEE_OBJECT_ID_MAX_LEN) { 447 res = TEE_ERROR_BAD_PARAMETERS; 448 goto exit; 449 } 450 451 res = tee_ta_get_current_session(&sess); 452 if (res != TEE_SUCCESS) 453 goto err; 454 utc = to_user_ta_ctx(sess->ctx); 455 456 res = tee_mmu_check_access_rights(utc, 457 TEE_MEMORY_ACCESS_READ | 458 TEE_MEMORY_ACCESS_ANY_OWNER, 459 (uaddr_t) object_id, 460 object_id_len); 461 if (res != TEE_SUCCESS) 462 goto err; 463 464 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 465 object_id_len, flags, fops, &po); 466 if (res != TEE_SUCCESS) 467 goto err; 468 469 o = tee_obj_alloc(); 470 if (o == NULL) { 471 tee_pobj_release(po); 472 res = TEE_ERROR_OUT_OF_MEMORY; 473 goto err; 474 } 475 476 o->info.handleFlags = 477 TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED; 478 o->flags = flags; 479 o->pobj = po; 480 tee_obj_add(utc, o); 481 482 res = tee_svc_storage_read_head(sess, o); 483 if (res != TEE_SUCCESS) { 484 if (res == TEE_ERROR_CORRUPT_OBJECT) { 485 EMSG("Object corrupt"); 486 goto err; 487 } 488 goto oclose; 489 } 490 491 res = tee_svc_copy_kaddr_to_uref(obj, o); 492 if (res != TEE_SUCCESS) 493 goto oclose; 494 495 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 496 if (res != TEE_SUCCESS) 497 goto oclose; 498 499 res = fops->seek(o->fh, sizeof(struct tee_svc_storage_head) + attr_size, 500 TEE_DATA_SEEK_SET, NULL); 501 if (res != TEE_SUCCESS) 502 goto err; 503 504 goto exit; 505 506 oclose: 507 tee_obj_close(utc, o); 508 o = NULL; 509 510 err: 511 if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT) 512 res = TEE_ERROR_CORRUPT_OBJECT; 513 if (res == TEE_ERROR_CORRUPT_OBJECT && o) 514 tee_svc_storage_remove_corrupt_obj(sess, o); 515 516 exit: 517 free(file); 518 file = NULL; 519 return res; 520 } 521 522 TEE_Result syscall_storage_obj_create(unsigned long storage_id, void *object_id, 523 size_t object_id_len, unsigned long flags, 524 unsigned long attr, void *data, size_t len, 525 uint32_t *obj) 526 { 527 TEE_Result res; 528 struct tee_ta_session *sess; 529 struct tee_obj *o = NULL; 530 struct tee_obj *attr_o = NULL; 531 char *file = NULL; 532 struct tee_pobj *po = NULL; 533 char *tmpfile = NULL; 534 struct user_ta_ctx *utc; 535 const struct tee_file_operations *fops = file_ops(storage_id); 536 size_t attr_size; 537 538 if (!fops) 539 return TEE_ERROR_ITEM_NOT_FOUND; 540 541 if (object_id_len > TEE_OBJECT_ID_MAX_LEN) 542 return TEE_ERROR_BAD_PARAMETERS; 543 544 res = tee_ta_get_current_session(&sess); 545 if (res != TEE_SUCCESS) 546 return res; 547 utc = to_user_ta_ctx(sess->ctx); 548 549 res = tee_mmu_check_access_rights(utc, 550 TEE_MEMORY_ACCESS_READ | 551 TEE_MEMORY_ACCESS_ANY_OWNER, 552 (uaddr_t) object_id, 553 object_id_len); 554 if (res != TEE_SUCCESS) 555 goto err; 556 557 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 558 object_id_len, flags, fops, &po); 559 if (res != TEE_SUCCESS) 560 goto err; 561 562 /* check rights of the provided buffer */ 563 if (data && len) { 564 res = tee_mmu_check_access_rights(utc, 565 TEE_MEMORY_ACCESS_READ | 566 TEE_MEMORY_ACCESS_ANY_OWNER, 567 (uaddr_t) data, len); 568 569 if (res != TEE_SUCCESS) 570 goto err; 571 } 572 573 o = tee_obj_alloc(); 574 if (o == NULL) { 575 res = TEE_ERROR_OUT_OF_MEMORY; 576 goto err; 577 } 578 579 o->info.handleFlags = 580 TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED; 581 o->flags = flags; 582 o->pobj = po; 583 584 if (attr != TEE_HANDLE_NULL) { 585 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(attr), 586 &attr_o); 587 if (res != TEE_SUCCESS) 588 goto err; 589 } 590 591 res = tee_svc_storage_init_file(sess, o, attr_o, data, len); 592 if (res != TEE_SUCCESS) 593 goto err; 594 595 /* create persistent object filename */ 596 file = tee_svc_storage_create_filename(sess, object_id, 597 object_id_len, false); 598 if (file == NULL) { 599 res = TEE_ERROR_OUT_OF_MEMORY; 600 goto err; 601 } 602 603 /* create temporary persistent object filename */ 604 tmpfile = tee_svc_storage_create_filename(sess, object_id, 605 object_id_len, 606 true); 607 if (tmpfile == NULL) { 608 res = TEE_ERROR_OUT_OF_MEMORY; 609 goto err; 610 } 611 612 /* rename temporary persistent object filename */ 613 res = fops->rename(tmpfile, file, !!(flags & TEE_DATA_FLAG_OVERWRITE)); 614 if (res != TEE_SUCCESS) 615 goto rmfile; 616 617 res = fops->open(file, &o->fh); 618 if (res != TEE_SUCCESS) 619 goto err; 620 621 tee_obj_add(utc, o); 622 623 res = tee_svc_copy_kaddr_to_uref(obj, o); 624 if (res != TEE_SUCCESS) 625 goto oclose; 626 627 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 628 if (res != TEE_SUCCESS) 629 goto oclose; 630 631 res = fops->seek(o->fh, sizeof(struct tee_svc_storage_head) + attr_size, 632 TEE_DATA_SEEK_SET, NULL); 633 if (res != TEE_SUCCESS) 634 goto oclose; 635 636 goto exit; 637 638 oclose: 639 tee_obj_close(utc, o); 640 goto exit; 641 642 rmfile: 643 fops->remove(tmpfile); 644 645 err: 646 if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT) 647 res = TEE_ERROR_CORRUPT_OBJECT; 648 if (res == TEE_ERROR_CORRUPT_OBJECT && file) 649 fops->remove(file); 650 if (o) 651 fops->close(&o->fh); 652 if (po) 653 tee_pobj_release(po); 654 free(o); 655 656 exit: 657 free(file); 658 free(tmpfile); 659 660 return res; 661 } 662 663 TEE_Result syscall_storage_obj_del(unsigned long obj) 664 { 665 TEE_Result res; 666 struct tee_ta_session *sess; 667 struct tee_obj *o; 668 char *file; 669 struct user_ta_ctx *utc; 670 const struct tee_file_operations *fops; 671 672 res = tee_ta_get_current_session(&sess); 673 if (res != TEE_SUCCESS) 674 return res; 675 utc = to_user_ta_ctx(sess->ctx); 676 677 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 678 if (res != TEE_SUCCESS) 679 return res; 680 681 if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE_META)) 682 return TEE_ERROR_ACCESS_CONFLICT; 683 684 if (o->pobj == NULL || o->pobj->obj_id == NULL) 685 return TEE_ERROR_BAD_STATE; 686 687 file = tee_svc_storage_create_filename(sess, o->pobj->obj_id, 688 o->pobj->obj_id_len, false); 689 if (file == NULL) 690 return TEE_ERROR_OUT_OF_MEMORY; 691 692 fops = o->pobj->fops; 693 tee_obj_close(utc, o); 694 695 res = fops->remove(file); 696 free(file); 697 return res; 698 } 699 700 TEE_Result syscall_storage_obj_rename(unsigned long obj, void *object_id, 701 size_t object_id_len) 702 { 703 TEE_Result res; 704 struct tee_ta_session *sess; 705 struct tee_obj *o; 706 struct tee_pobj *po = NULL; 707 char *new_file = NULL; 708 char *old_file = NULL; 709 struct user_ta_ctx *utc; 710 const struct tee_file_operations *fops; 711 712 if (object_id_len > TEE_OBJECT_ID_MAX_LEN) 713 return TEE_ERROR_BAD_PARAMETERS; 714 715 res = tee_ta_get_current_session(&sess); 716 if (res != TEE_SUCCESS) 717 return res; 718 utc = to_user_ta_ctx(sess->ctx); 719 720 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 721 if (res != TEE_SUCCESS) 722 return res; 723 724 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 725 res = TEE_ERROR_BAD_STATE; 726 goto exit; 727 } 728 729 if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE_META)) { 730 res = TEE_ERROR_BAD_STATE; 731 goto exit; 732 } 733 734 if (o->pobj == NULL || o->pobj->obj_id == NULL) { 735 res = TEE_ERROR_BAD_STATE; 736 goto exit; 737 } 738 739 res = tee_mmu_check_access_rights(utc, 740 TEE_MEMORY_ACCESS_READ | 741 TEE_MEMORY_ACCESS_ANY_OWNER, 742 (uaddr_t) object_id, object_id_len); 743 if (res != TEE_SUCCESS) 744 goto exit; 745 746 /* get new ds name */ 747 new_file = tee_svc_storage_create_filename(sess, object_id, 748 object_id_len, false); 749 if (new_file == NULL) { 750 res = TEE_ERROR_OUT_OF_MEMORY; 751 goto exit; 752 } 753 754 old_file = tee_svc_storage_create_filename(sess, o->pobj->obj_id, 755 o->pobj->obj_id_len, false); 756 if (old_file == NULL) { 757 res = TEE_ERROR_OUT_OF_MEMORY; 758 goto exit; 759 } 760 761 /* reserve dest name */ 762 fops = o->pobj->fops; 763 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 764 object_id_len, TEE_DATA_FLAG_ACCESS_WRITE_META, 765 fops, &po); 766 if (res != TEE_SUCCESS) 767 goto exit; 768 769 /* move */ 770 res = fops->rename(old_file, new_file, false /* no overwrite */); 771 if (res == TEE_ERROR_GENERIC) 772 goto exit; 773 774 res = tee_pobj_rename(o->pobj, object_id, object_id_len); 775 776 exit: 777 tee_pobj_release(po); 778 779 free(new_file); 780 free(old_file); 781 782 return res; 783 } 784 785 TEE_Result syscall_storage_alloc_enum(uint32_t *obj_enum) 786 { 787 struct tee_storage_enum *e; 788 struct tee_ta_session *sess; 789 TEE_Result res; 790 struct user_ta_ctx *utc; 791 792 if (obj_enum == NULL) 793 return TEE_ERROR_BAD_PARAMETERS; 794 795 res = tee_ta_get_current_session(&sess); 796 if (res != TEE_SUCCESS) 797 return res; 798 utc = to_user_ta_ctx(sess->ctx); 799 800 e = malloc(sizeof(struct tee_storage_enum)); 801 if (e == NULL) 802 return TEE_ERROR_OUT_OF_MEMORY; 803 804 e->dir = NULL; 805 e->fops = NULL; 806 TAILQ_INSERT_TAIL(&utc->storage_enums, e, link); 807 808 return tee_svc_copy_kaddr_to_uref(obj_enum, e); 809 } 810 811 TEE_Result syscall_storage_free_enum(unsigned long obj_enum) 812 { 813 struct tee_storage_enum *e; 814 TEE_Result res; 815 struct tee_ta_session *sess; 816 struct user_ta_ctx *utc; 817 818 res = tee_ta_get_current_session(&sess); 819 if (res != TEE_SUCCESS) 820 return res; 821 utc = to_user_ta_ctx(sess->ctx); 822 823 res = tee_svc_storage_get_enum(utc, 824 tee_svc_uref_to_vaddr(obj_enum), &e); 825 if (res != TEE_SUCCESS) 826 return res; 827 828 return tee_svc_close_enum(utc, e); 829 } 830 831 TEE_Result syscall_storage_reset_enum(unsigned long obj_enum) 832 { 833 struct tee_storage_enum *e; 834 TEE_Result res; 835 struct tee_ta_session *sess; 836 837 res = tee_ta_get_current_session(&sess); 838 if (res != TEE_SUCCESS) 839 return res; 840 841 res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx), 842 tee_svc_uref_to_vaddr(obj_enum), &e); 843 if (res != TEE_SUCCESS) 844 return res; 845 846 e->fops->closedir(e->dir); 847 e->fops = NULL; 848 e->dir = NULL; 849 850 return TEE_SUCCESS; 851 } 852 853 static TEE_Result tee_svc_storage_set_enum(char *d_name, 854 const struct tee_file_operations *fops, 855 struct tee_obj *o) 856 { 857 TEE_Result res; 858 uint32_t blen; 859 uint32_t hslen; 860 861 o->info.handleFlags = 862 TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED; 863 o->info.objectUsage = TEE_USAGE_DEFAULT; 864 865 hslen = strlen(d_name); 866 blen = TEE_HS2B_BBUF_SIZE(hslen); 867 o->pobj->obj_id = malloc(blen); 868 if (!o->pobj->obj_id) { 869 res = TEE_ERROR_OUT_OF_MEMORY; 870 goto exit; 871 } 872 tee_hs2b((uint8_t *)d_name, o->pobj->obj_id, hslen, blen); 873 o->pobj->obj_id_len = blen; 874 o->pobj->fops = fops; 875 876 res = TEE_SUCCESS; 877 878 exit: 879 return res; 880 881 } 882 883 TEE_Result syscall_storage_start_enum(unsigned long obj_enum, 884 unsigned long storage_id) 885 { 886 struct tee_storage_enum *e; 887 char *dir; 888 TEE_Result res; 889 struct tee_ta_session *sess; 890 const struct tee_file_operations *fops = file_ops(storage_id); 891 892 res = tee_ta_get_current_session(&sess); 893 if (res != TEE_SUCCESS) 894 return res; 895 896 res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx), 897 tee_svc_uref_to_vaddr(obj_enum), &e); 898 if (res != TEE_SUCCESS) 899 return res; 900 901 if (!fops) 902 return TEE_ERROR_ITEM_NOT_FOUND; 903 904 e->fops = fops; 905 dir = tee_svc_storage_create_dirname(sess); 906 if (dir == NULL) { 907 res = TEE_ERROR_OUT_OF_MEMORY; 908 goto exit; 909 } 910 911 assert(!e->dir); 912 res = fops->opendir(dir, &e->dir); 913 free(dir); 914 exit: 915 return res; 916 } 917 918 TEE_Result syscall_storage_next_enum(unsigned long obj_enum, 919 TEE_ObjectInfo *info, void *obj_id, uint64_t *len) 920 { 921 struct tee_storage_enum *e; 922 struct tee_fs_dirent *d; 923 TEE_Result res = TEE_SUCCESS; 924 struct tee_ta_session *sess; 925 struct tee_obj *o = NULL; 926 uint64_t l; 927 struct user_ta_ctx *utc; 928 929 res = tee_ta_get_current_session(&sess); 930 if (res != TEE_SUCCESS) 931 goto exit; 932 utc = to_user_ta_ctx(sess->ctx); 933 934 res = tee_svc_storage_get_enum(utc, 935 tee_svc_uref_to_vaddr(obj_enum), &e); 936 if (res != TEE_SUCCESS) 937 goto exit; 938 939 /* check rights of the provided buffers */ 940 res = tee_mmu_check_access_rights(utc, 941 TEE_MEMORY_ACCESS_WRITE | 942 TEE_MEMORY_ACCESS_ANY_OWNER, 943 (uaddr_t) info, 944 sizeof(TEE_ObjectInfo)); 945 if (res != TEE_SUCCESS) 946 goto exit; 947 948 res = tee_mmu_check_access_rights(utc, 949 TEE_MEMORY_ACCESS_WRITE | 950 TEE_MEMORY_ACCESS_ANY_OWNER, 951 (uaddr_t) obj_id, 952 TEE_OBJECT_ID_MAX_LEN); 953 if (res != TEE_SUCCESS) 954 goto exit; 955 956 if (!e->fops) { 957 res = TEE_ERROR_ITEM_NOT_FOUND; 958 goto exit; 959 } 960 961 res = e->fops->readdir(e->dir, &d); 962 if (res != TEE_SUCCESS) 963 goto exit; 964 965 o = tee_obj_alloc(); 966 if (o == NULL) { 967 res = TEE_ERROR_OUT_OF_MEMORY; 968 goto exit; 969 } 970 o->flags = TEE_DATA_FLAG_SHARE_READ; 971 972 o->pobj = calloc(1, sizeof(struct tee_pobj)); 973 if (!o->pobj) { 974 res = TEE_ERROR_OUT_OF_MEMORY; 975 goto exit; 976 } 977 978 res = tee_svc_storage_set_enum(d->d_name, e->fops, o); 979 if (res != TEE_SUCCESS) 980 goto exit; 981 982 res = tee_svc_storage_read_head(sess, o); 983 if (res != TEE_SUCCESS) 984 goto exit; 985 986 memcpy(info, &o->info, sizeof(TEE_ObjectInfo)); 987 memcpy(obj_id, o->pobj->obj_id, o->pobj->obj_id_len); 988 989 l = o->pobj->obj_id_len; 990 res = tee_svc_copy_to_user(len, &l, sizeof(*len)); 991 992 exit: 993 if (o) { 994 if (o->pobj) { 995 o->pobj->fops->close(&o->fh); 996 free(o->pobj->obj_id); 997 } 998 free(o->pobj); 999 tee_obj_free(o); 1000 } 1001 1002 return res; 1003 } 1004 1005 TEE_Result syscall_storage_obj_read(unsigned long obj, void *data, size_t len, 1006 uint64_t *count) 1007 { 1008 TEE_Result res; 1009 struct tee_ta_session *sess; 1010 struct tee_obj *o; 1011 uint64_t u_count; 1012 struct user_ta_ctx *utc; 1013 size_t bytes; 1014 1015 res = tee_ta_get_current_session(&sess); 1016 if (res != TEE_SUCCESS) 1017 goto exit; 1018 utc = to_user_ta_ctx(sess->ctx); 1019 1020 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 1021 if (res != TEE_SUCCESS) 1022 goto exit; 1023 1024 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 1025 res = TEE_ERROR_BAD_STATE; 1026 goto exit; 1027 } 1028 1029 if (!(o->flags & TEE_DATA_FLAG_ACCESS_READ)) { 1030 res = TEE_ERROR_ACCESS_CONFLICT; 1031 goto exit; 1032 } 1033 1034 /* check rights of the provided buffer */ 1035 res = tee_mmu_check_access_rights(utc, 1036 TEE_MEMORY_ACCESS_WRITE | 1037 TEE_MEMORY_ACCESS_ANY_OWNER, 1038 (uaddr_t) data, len); 1039 if (res != TEE_SUCCESS) 1040 goto exit; 1041 1042 bytes = len; 1043 res = o->pobj->fops->read(o->fh, data, &bytes); 1044 if (res != TEE_SUCCESS) { 1045 EMSG("Error code=%x\n", (uint32_t)res); 1046 if (res == TEE_ERROR_CORRUPT_OBJECT) { 1047 EMSG("Object corrupt\n"); 1048 tee_svc_storage_remove_corrupt_obj(sess, o); 1049 } 1050 goto exit; 1051 } 1052 1053 o->info.dataPosition += bytes; 1054 1055 u_count = bytes; 1056 res = tee_svc_copy_to_user(count, &u_count, sizeof(*count)); 1057 exit: 1058 return res; 1059 } 1060 1061 TEE_Result syscall_storage_obj_write(unsigned long obj, void *data, size_t len) 1062 { 1063 TEE_Result res; 1064 struct tee_ta_session *sess; 1065 struct tee_obj *o; 1066 struct user_ta_ctx *utc; 1067 1068 res = tee_ta_get_current_session(&sess); 1069 if (res != TEE_SUCCESS) 1070 goto exit; 1071 utc = to_user_ta_ctx(sess->ctx); 1072 1073 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 1074 if (res != TEE_SUCCESS) 1075 goto exit; 1076 1077 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 1078 res = TEE_ERROR_BAD_STATE; 1079 goto exit; 1080 } 1081 1082 if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE)) { 1083 res = TEE_ERROR_ACCESS_CONFLICT; 1084 goto exit; 1085 } 1086 1087 /* check rights of the provided buffer */ 1088 res = tee_mmu_check_access_rights(utc, 1089 TEE_MEMORY_ACCESS_READ | 1090 TEE_MEMORY_ACCESS_ANY_OWNER, 1091 (uaddr_t) data, len); 1092 if (res != TEE_SUCCESS) 1093 goto exit; 1094 1095 res = o->pobj->fops->write(o->fh, data, len); 1096 if (res != TEE_SUCCESS) 1097 goto exit; 1098 1099 o->info.dataPosition += len; 1100 if (o->info.dataPosition > o->info.dataSize) { 1101 res = tee_svc_storage_update_head(o, o->info.dataPosition); 1102 if (res != TEE_SUCCESS) 1103 goto exit; 1104 o->info.dataSize = o->info.dataPosition; 1105 } 1106 1107 exit: 1108 return res; 1109 } 1110 1111 TEE_Result syscall_storage_obj_trunc(unsigned long obj, size_t len) 1112 { 1113 TEE_Result res; 1114 struct tee_ta_session *sess; 1115 struct tee_obj *o; 1116 size_t off; 1117 size_t attr_size; 1118 1119 res = tee_ta_get_current_session(&sess); 1120 if (res != TEE_SUCCESS) 1121 goto exit; 1122 1123 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1124 tee_svc_uref_to_vaddr(obj), &o); 1125 if (res != TEE_SUCCESS) 1126 goto exit; 1127 1128 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 1129 res = TEE_ERROR_BAD_STATE; 1130 goto exit; 1131 } 1132 1133 if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE)) { 1134 res = TEE_ERROR_ACCESS_CONFLICT; 1135 goto exit; 1136 } 1137 1138 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 1139 if (res != TEE_SUCCESS) 1140 goto exit; 1141 1142 off = sizeof(struct tee_svc_storage_head) + attr_size; 1143 res = o->pobj->fops->truncate(o->fh, len + off); 1144 if (res != TEE_SUCCESS) { 1145 if (res == TEE_ERROR_CORRUPT_OBJECT) { 1146 EMSG("Object corrupt\n"); 1147 res = tee_svc_storage_remove_corrupt_obj(sess, o); 1148 if (res != TEE_SUCCESS) 1149 goto exit; 1150 res = TEE_ERROR_CORRUPT_OBJECT; 1151 goto exit; 1152 } else 1153 res = TEE_ERROR_GENERIC; 1154 } 1155 1156 exit: 1157 return res; 1158 } 1159 1160 TEE_Result syscall_storage_obj_seek(unsigned long obj, int32_t offset, 1161 unsigned long whence) 1162 { 1163 TEE_Result res; 1164 struct tee_ta_session *sess; 1165 struct tee_obj *o; 1166 int32_t off; 1167 size_t attr_size; 1168 1169 res = tee_ta_get_current_session(&sess); 1170 if (res != TEE_SUCCESS) 1171 goto exit; 1172 1173 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1174 tee_svc_uref_to_vaddr(obj), &o); 1175 if (res != TEE_SUCCESS) 1176 goto exit; 1177 1178 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 1179 res = TEE_ERROR_BAD_STATE; 1180 goto exit; 1181 } 1182 1183 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 1184 if (res != TEE_SUCCESS) 1185 goto exit; 1186 1187 off = offset; 1188 if (whence == TEE_DATA_SEEK_SET) 1189 off += sizeof(struct tee_svc_storage_head) + attr_size; 1190 1191 res = o->pobj->fops->seek(o->fh, off, whence, &off); 1192 if (res != TEE_SUCCESS) 1193 goto exit; 1194 o->info.dataPosition = off - (sizeof(struct tee_svc_storage_head) + 1195 attr_size); 1196 1197 exit: 1198 return res; 1199 } 1200 1201 void tee_svc_storage_close_all_enum(struct user_ta_ctx *utc) 1202 { 1203 struct tee_storage_enum_head *eh = &utc->storage_enums; 1204 1205 /* disregard return value */ 1206 while (!TAILQ_EMPTY(eh)) 1207 tee_svc_close_enum(utc, TAILQ_FIRST(eh)); 1208 } 1209