1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 6 #include <kernel/mutex.h> 7 #include <kernel/tee_misc.h> 8 #include <kernel/tee_ta_manager.h> 9 #include <mm/tee_mmu.h> 10 #include <string.h> 11 #include <tee_api_defines_extensions.h> 12 #include <tee_api_defines.h> 13 #include <tee/fs_dirfile.h> 14 #include <tee/tee_fs.h> 15 #include <tee/tee_obj.h> 16 #include <tee/tee_pobj.h> 17 #include <tee/tee_svc_cryp.h> 18 #include <tee/tee_svc.h> 19 #include <tee/tee_svc_storage.h> 20 #include <trace.h> 21 22 const struct tee_file_operations *tee_svc_storage_file_ops(uint32_t storage_id) 23 { 24 25 switch (storage_id) { 26 case TEE_STORAGE_PRIVATE: 27 #if defined(CFG_REE_FS) 28 return &ree_fs_ops; 29 #elif defined(CFG_RPMB_FS) 30 return &rpmb_fs_ops; 31 #else 32 #error At least one filesystem must be enabled. 33 #endif 34 #ifdef CFG_REE_FS 35 case TEE_STORAGE_PRIVATE_REE: 36 return &ree_fs_ops; 37 #endif 38 #ifdef CFG_RPMB_FS 39 case TEE_STORAGE_PRIVATE_RPMB: 40 return &rpmb_fs_ops; 41 #endif 42 default: 43 return NULL; 44 } 45 } 46 47 /* Header of GP formated secure storage files */ 48 struct tee_svc_storage_head { 49 uint32_t attr_size; 50 uint32_t keySize; 51 uint32_t maxKeySize; 52 uint32_t objectUsage; 53 uint32_t objectType; 54 uint32_t have_attrs; 55 }; 56 57 struct tee_storage_enum { 58 TAILQ_ENTRY(tee_storage_enum) link; 59 struct tee_fs_dir *dir; 60 const struct tee_file_operations *fops; 61 }; 62 63 static TEE_Result tee_svc_storage_get_enum(struct user_ta_ctx *utc, 64 uint32_t enum_id, 65 struct tee_storage_enum **e_out) 66 { 67 struct tee_storage_enum *e; 68 69 TAILQ_FOREACH(e, &utc->storage_enums, link) { 70 if (enum_id == (vaddr_t)e) { 71 *e_out = e; 72 return TEE_SUCCESS; 73 } 74 } 75 return TEE_ERROR_BAD_PARAMETERS; 76 } 77 78 static TEE_Result tee_svc_close_enum(struct user_ta_ctx *utc, 79 struct tee_storage_enum *e) 80 { 81 if (e == NULL || utc == NULL) 82 return TEE_ERROR_BAD_PARAMETERS; 83 84 TAILQ_REMOVE(&utc->storage_enums, e, link); 85 86 if (e->fops) 87 e->fops->closedir(e->dir); 88 89 e->dir = NULL; 90 e->fops = NULL; 91 92 free(e); 93 94 return TEE_SUCCESS; 95 } 96 97 /* "/TA_uuid/object_id" or "/TA_uuid/.object_id" */ 98 TEE_Result tee_svc_storage_create_filename(void *buf, size_t blen, 99 struct tee_pobj *po, bool transient) 100 { 101 uint8_t *file = buf; 102 uint32_t pos = 0; 103 uint32_t hslen = 1 /* Leading slash */ 104 + TEE_B2HS_HSBUF_SIZE(sizeof(TEE_UUID) + po->obj_id_len) 105 + 1; /* Intermediate slash */ 106 107 /* +1 for the '.' (temporary persistent object) */ 108 if (transient) 109 hslen++; 110 111 if (blen < hslen) 112 return TEE_ERROR_SHORT_BUFFER; 113 114 file[pos++] = '/'; 115 pos += tee_b2hs((uint8_t *)&po->uuid, &file[pos], 116 sizeof(TEE_UUID), hslen); 117 file[pos++] = '/'; 118 119 if (transient) 120 file[pos++] = '.'; 121 122 tee_b2hs(po->obj_id, file + pos, po->obj_id_len, hslen - pos); 123 124 return TEE_SUCCESS; 125 } 126 127 #ifdef CFG_REE_FS 128 /* "/dirf.db" or "/<file number>" */ 129 TEE_Result 130 tee_svc_storage_create_filename_dfh(void *buf, size_t blen, 131 const struct tee_fs_dirfile_fileh *dfh) 132 { 133 char *file = buf; 134 size_t pos = 0; 135 size_t l; 136 137 if (pos >= blen) 138 return TEE_ERROR_SHORT_BUFFER; 139 140 file[pos] = '/'; 141 pos++; 142 if (pos >= blen) 143 return TEE_ERROR_SHORT_BUFFER; 144 145 l = blen - pos; 146 return tee_fs_dirfile_fileh_to_fname(dfh, file + pos, &l); 147 } 148 #endif 149 150 /* "/TA_uuid" */ 151 TEE_Result tee_svc_storage_create_dirname(void *buf, size_t blen, 152 const TEE_UUID *uuid) 153 { 154 uint8_t *dir = buf; 155 uint32_t hslen = TEE_B2HS_HSBUF_SIZE(sizeof(TEE_UUID)) + 1; 156 157 if (blen < hslen) 158 return TEE_ERROR_SHORT_BUFFER; 159 160 dir[0] = '/'; 161 tee_b2hs((uint8_t *)uuid, dir + 1, sizeof(TEE_UUID), hslen); 162 163 return TEE_SUCCESS; 164 } 165 166 static TEE_Result tee_svc_storage_remove_corrupt_obj( 167 struct tee_ta_session *sess, 168 struct tee_obj *o) 169 { 170 o->pobj->fops->remove(o->pobj); 171 tee_obj_close(to_user_ta_ctx(sess->ctx), o); 172 173 return TEE_SUCCESS; 174 } 175 176 static TEE_Result tee_svc_storage_read_head(struct tee_obj *o) 177 { 178 TEE_Result res = TEE_SUCCESS; 179 size_t bytes; 180 struct tee_svc_storage_head head; 181 const struct tee_file_operations *fops = o->pobj->fops; 182 void *attr = NULL; 183 size_t size; 184 size_t tmp = 0; 185 186 assert(!o->fh); 187 res = fops->open(o->pobj, &size, &o->fh); 188 if (res != TEE_SUCCESS) 189 goto exit; 190 191 /* read head */ 192 bytes = sizeof(struct tee_svc_storage_head); 193 res = fops->read(o->fh, 0, &head, &bytes); 194 if (res != TEE_SUCCESS) { 195 if (res == TEE_ERROR_CORRUPT_OBJECT) 196 EMSG("Head corrupt"); 197 goto exit; 198 } 199 200 if (ADD_OVERFLOW(sizeof(head), head.attr_size, &tmp)) { 201 res = TEE_ERROR_OVERFLOW; 202 goto exit; 203 } 204 if (tmp > size) { 205 res = TEE_ERROR_CORRUPT_OBJECT; 206 goto exit; 207 } 208 209 if (bytes != sizeof(struct tee_svc_storage_head)) { 210 res = TEE_ERROR_BAD_FORMAT; 211 goto exit; 212 } 213 214 res = tee_obj_set_type(o, head.objectType, head.maxKeySize); 215 if (res != TEE_SUCCESS) 216 goto exit; 217 218 o->ds_pos = tmp; 219 220 if (head.attr_size) { 221 attr = malloc(head.attr_size); 222 if (!attr) { 223 res = TEE_ERROR_OUT_OF_MEMORY; 224 goto exit; 225 } 226 227 /* read meta */ 228 bytes = head.attr_size; 229 res = fops->read(o->fh, sizeof(struct tee_svc_storage_head), 230 attr, &bytes); 231 if (res == TEE_ERROR_OUT_OF_MEMORY) 232 goto exit; 233 if (res != TEE_SUCCESS || bytes != head.attr_size) 234 res = TEE_ERROR_CORRUPT_OBJECT; 235 if (res) 236 goto exit; 237 } 238 239 res = tee_obj_attr_from_binary(o, attr, head.attr_size); 240 if (res != TEE_SUCCESS) 241 goto exit; 242 243 o->info.dataSize = size - sizeof(head) - head.attr_size; 244 o->info.keySize = head.keySize; 245 o->info.objectUsage = head.objectUsage; 246 o->info.objectType = head.objectType; 247 o->have_attrs = head.have_attrs; 248 249 exit: 250 free(attr); 251 252 return res; 253 } 254 255 TEE_Result syscall_storage_obj_open(unsigned long storage_id, void *object_id, 256 size_t object_id_len, unsigned long flags, 257 uint32_t *obj) 258 { 259 const unsigned long valid_flags = TEE_DATA_FLAG_ACCESS_READ | 260 TEE_DATA_FLAG_ACCESS_WRITE | 261 TEE_DATA_FLAG_ACCESS_WRITE_META | 262 TEE_DATA_FLAG_SHARE_READ | 263 TEE_DATA_FLAG_SHARE_WRITE; 264 const struct tee_file_operations *fops = 265 tee_svc_storage_file_ops(storage_id); 266 struct tee_ta_session *sess = NULL; 267 struct user_ta_ctx *utc = NULL; 268 TEE_Result res = TEE_SUCCESS; 269 struct tee_pobj *po = NULL; 270 struct tee_obj *o = NULL; 271 char *file = NULL; 272 273 if (flags & ~valid_flags) 274 return TEE_ERROR_BAD_PARAMETERS; 275 276 if (!fops) { 277 res = TEE_ERROR_ITEM_NOT_FOUND; 278 goto exit; 279 } 280 281 if (object_id_len > TEE_OBJECT_ID_MAX_LEN) { 282 res = TEE_ERROR_BAD_PARAMETERS; 283 goto exit; 284 } 285 286 res = tee_ta_get_current_session(&sess); 287 if (res != TEE_SUCCESS) 288 goto err; 289 utc = to_user_ta_ctx(sess->ctx); 290 291 res = tee_mmu_check_access_rights(&utc->uctx, 292 TEE_MEMORY_ACCESS_READ, 293 (uaddr_t) object_id, 294 object_id_len); 295 if (res != TEE_SUCCESS) 296 goto err; 297 298 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 299 object_id_len, flags, TEE_POBJ_USAGE_OPEN, fops, 300 &po); 301 if (res != TEE_SUCCESS) 302 goto err; 303 304 o = tee_obj_alloc(); 305 if (o == NULL) { 306 tee_pobj_release(po); 307 res = TEE_ERROR_OUT_OF_MEMORY; 308 goto err; 309 } 310 311 o->info.handleFlags = TEE_HANDLE_FLAG_PERSISTENT | 312 TEE_HANDLE_FLAG_INITIALIZED | flags; 313 o->pobj = po; 314 tee_obj_add(utc, o); 315 316 res = tee_svc_storage_read_head(o); 317 if (res != TEE_SUCCESS) { 318 if (res == TEE_ERROR_CORRUPT_OBJECT) { 319 EMSG("Object corrupt"); 320 goto err; 321 } 322 goto oclose; 323 } 324 325 res = tee_svc_copy_kaddr_to_uref(obj, o); 326 if (res != TEE_SUCCESS) 327 goto oclose; 328 329 goto exit; 330 331 oclose: 332 tee_obj_close(utc, o); 333 o = NULL; 334 335 err: 336 if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT) 337 res = TEE_ERROR_CORRUPT_OBJECT; 338 if (res == TEE_ERROR_CORRUPT_OBJECT && o) 339 tee_svc_storage_remove_corrupt_obj(sess, o); 340 341 exit: 342 free(file); 343 file = NULL; 344 return res; 345 } 346 347 static TEE_Result tee_svc_storage_init_file(struct tee_obj *o, bool overwrite, 348 struct tee_obj *attr_o, void *data, 349 uint32_t len) 350 { 351 TEE_Result res = TEE_SUCCESS; 352 struct tee_svc_storage_head head; 353 const struct tee_file_operations *fops = o->pobj->fops; 354 void *attr = NULL; 355 size_t attr_size = 0; 356 357 if (attr_o) { 358 res = tee_obj_set_type(o, attr_o->info.objectType, 359 attr_o->info.maxKeySize); 360 if (res) 361 return res; 362 res = tee_obj_attr_copy_from(o, attr_o); 363 if (res) 364 return res; 365 o->have_attrs = attr_o->have_attrs; 366 o->info.objectUsage = attr_o->info.objectUsage; 367 o->info.keySize = attr_o->info.keySize; 368 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 369 if (res) 370 return res; 371 if (attr_size) { 372 attr = malloc(attr_size); 373 if (!attr) 374 return TEE_ERROR_OUT_OF_MEMORY; 375 res = tee_obj_attr_to_binary(o, attr, &attr_size); 376 if (res != TEE_SUCCESS) 377 goto exit; 378 } 379 } else { 380 res = tee_obj_set_type(o, TEE_TYPE_DATA, 0); 381 if (res != TEE_SUCCESS) 382 goto exit; 383 } 384 385 o->ds_pos = sizeof(struct tee_svc_storage_head) + attr_size; 386 387 /* write head */ 388 head.attr_size = attr_size; 389 head.keySize = o->info.keySize; 390 head.maxKeySize = o->info.maxKeySize; 391 head.objectUsage = o->info.objectUsage; 392 head.objectType = o->info.objectType; 393 head.have_attrs = o->have_attrs; 394 395 res = fops->create(o->pobj, overwrite, &head, sizeof(head), attr, 396 attr_size, data, len, &o->fh); 397 398 if (!res) 399 o->info.dataSize = len; 400 exit: 401 free(attr); 402 return res; 403 } 404 405 TEE_Result syscall_storage_obj_create(unsigned long storage_id, void *object_id, 406 size_t object_id_len, unsigned long flags, 407 unsigned long attr, void *data, size_t len, 408 uint32_t *obj) 409 { 410 const unsigned long valid_flags = TEE_DATA_FLAG_ACCESS_READ | 411 TEE_DATA_FLAG_ACCESS_WRITE | 412 TEE_DATA_FLAG_ACCESS_WRITE_META | 413 TEE_DATA_FLAG_SHARE_READ | 414 TEE_DATA_FLAG_SHARE_WRITE | 415 TEE_DATA_FLAG_OVERWRITE; 416 const struct tee_file_operations *fops = 417 tee_svc_storage_file_ops(storage_id); 418 struct tee_ta_session *sess = NULL; 419 struct user_ta_ctx *utc = NULL; 420 struct tee_obj *attr_o = NULL; 421 TEE_Result res = TEE_SUCCESS; 422 struct tee_pobj *po = NULL; 423 struct tee_obj *o = NULL; 424 425 if (flags & ~valid_flags) 426 return TEE_ERROR_BAD_PARAMETERS; 427 428 if (!fops) 429 return TEE_ERROR_ITEM_NOT_FOUND; 430 431 if (object_id_len > TEE_OBJECT_ID_MAX_LEN) 432 return TEE_ERROR_BAD_PARAMETERS; 433 434 res = tee_ta_get_current_session(&sess); 435 if (res != TEE_SUCCESS) 436 return res; 437 utc = to_user_ta_ctx(sess->ctx); 438 439 res = tee_mmu_check_access_rights(&utc->uctx, TEE_MEMORY_ACCESS_READ, 440 (uaddr_t)object_id, object_id_len); 441 if (res != TEE_SUCCESS) 442 goto err; 443 444 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 445 object_id_len, flags, TEE_POBJ_USAGE_CREATE, 446 fops, &po); 447 if (res != TEE_SUCCESS) 448 goto err; 449 450 /* check rights of the provided buffer */ 451 if (len) { 452 if (data) { 453 uint32_t f = TEE_MEMORY_ACCESS_READ | 454 TEE_MEMORY_ACCESS_ANY_OWNER; 455 456 res = tee_mmu_check_access_rights(&utc->uctx, f, 457 (uaddr_t)data, len); 458 459 if (res != TEE_SUCCESS) 460 goto err; 461 } else { 462 res = TEE_ERROR_BAD_PARAMETERS; 463 goto err; 464 } 465 } 466 467 o = tee_obj_alloc(); 468 if (o == NULL) { 469 res = TEE_ERROR_OUT_OF_MEMORY; 470 goto err; 471 } 472 473 o->info.handleFlags = TEE_HANDLE_FLAG_PERSISTENT | 474 TEE_HANDLE_FLAG_INITIALIZED | flags; 475 o->pobj = po; 476 477 if (attr != TEE_HANDLE_NULL) { 478 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(attr), 479 &attr_o); 480 if (res != TEE_SUCCESS) 481 goto err; 482 /* The supplied handle must be one of an initialized object */ 483 if (!(attr_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) { 484 res = TEE_ERROR_BAD_PARAMETERS; 485 goto err; 486 } 487 } 488 489 res = tee_svc_storage_init_file(o, flags & TEE_DATA_FLAG_OVERWRITE, 490 attr_o, data, len); 491 if (res != TEE_SUCCESS) 492 goto err; 493 494 po = NULL; /* o owns it from now on */ 495 tee_obj_add(utc, o); 496 497 res = tee_svc_copy_kaddr_to_uref(obj, o); 498 if (res != TEE_SUCCESS) 499 goto oclose; 500 501 return TEE_SUCCESS; 502 503 oclose: 504 tee_obj_close(utc, o); 505 return res; 506 507 err: 508 if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT) 509 res = TEE_ERROR_CORRUPT_OBJECT; 510 if (res == TEE_ERROR_CORRUPT_OBJECT && po) 511 fops->remove(po); 512 if (o) { 513 fops->close(&o->fh); 514 tee_obj_free(o); 515 } 516 if (po) 517 tee_pobj_release(po); 518 519 return res; 520 } 521 522 TEE_Result syscall_storage_obj_del(unsigned long obj) 523 { 524 TEE_Result res; 525 struct tee_ta_session *sess; 526 struct tee_obj *o; 527 struct user_ta_ctx *utc; 528 529 res = tee_ta_get_current_session(&sess); 530 if (res != TEE_SUCCESS) 531 return res; 532 utc = to_user_ta_ctx(sess->ctx); 533 534 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 535 if (res != TEE_SUCCESS) 536 return res; 537 538 if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE_META)) 539 return TEE_ERROR_ACCESS_CONFLICT; 540 541 if (o->pobj == NULL || o->pobj->obj_id == NULL) 542 return TEE_ERROR_BAD_STATE; 543 544 res = o->pobj->fops->remove(o->pobj); 545 tee_obj_close(utc, o); 546 547 return res; 548 } 549 550 TEE_Result syscall_storage_obj_rename(unsigned long obj, void *object_id, 551 size_t object_id_len) 552 { 553 const struct tee_file_operations *fops = NULL; 554 struct tee_ta_session *sess = NULL; 555 struct user_ta_ctx *utc = NULL; 556 TEE_Result res = TEE_SUCCESS; 557 struct tee_pobj *po = NULL; 558 struct tee_obj *o = NULL; 559 char *new_file = NULL; 560 char *old_file = NULL; 561 562 if (object_id_len > TEE_OBJECT_ID_MAX_LEN) 563 return TEE_ERROR_BAD_PARAMETERS; 564 565 res = tee_ta_get_current_session(&sess); 566 if (res != TEE_SUCCESS) 567 return res; 568 utc = to_user_ta_ctx(sess->ctx); 569 570 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 571 if (res != TEE_SUCCESS) 572 return res; 573 574 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 575 res = TEE_ERROR_BAD_STATE; 576 goto exit; 577 } 578 579 if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE_META)) { 580 res = TEE_ERROR_BAD_STATE; 581 goto exit; 582 } 583 584 if (o->pobj == NULL || o->pobj->obj_id == NULL) { 585 res = TEE_ERROR_BAD_STATE; 586 goto exit; 587 } 588 589 res = tee_mmu_check_access_rights(&utc->uctx, TEE_MEMORY_ACCESS_READ, 590 (uaddr_t)object_id, object_id_len); 591 if (res != TEE_SUCCESS) 592 goto exit; 593 594 /* reserve dest name */ 595 fops = o->pobj->fops; 596 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 597 object_id_len, TEE_DATA_FLAG_ACCESS_WRITE_META, 598 TEE_POBJ_USAGE_RENAME, fops, &po); 599 if (res != TEE_SUCCESS) 600 goto exit; 601 602 /* move */ 603 res = fops->rename(o->pobj, po, false /* no overwrite */); 604 if (res) 605 goto exit; 606 607 res = tee_pobj_rename(o->pobj, object_id, object_id_len); 608 609 exit: 610 tee_pobj_release(po); 611 612 free(new_file); 613 free(old_file); 614 615 return res; 616 } 617 618 TEE_Result syscall_storage_alloc_enum(uint32_t *obj_enum) 619 { 620 struct tee_storage_enum *e; 621 struct tee_ta_session *sess; 622 TEE_Result res; 623 struct user_ta_ctx *utc; 624 625 if (obj_enum == NULL) 626 return TEE_ERROR_BAD_PARAMETERS; 627 628 res = tee_ta_get_current_session(&sess); 629 if (res != TEE_SUCCESS) 630 return res; 631 utc = to_user_ta_ctx(sess->ctx); 632 633 e = malloc(sizeof(struct tee_storage_enum)); 634 if (e == NULL) 635 return TEE_ERROR_OUT_OF_MEMORY; 636 637 e->dir = NULL; 638 e->fops = NULL; 639 TAILQ_INSERT_TAIL(&utc->storage_enums, e, link); 640 641 return tee_svc_copy_kaddr_to_uref(obj_enum, e); 642 } 643 644 TEE_Result syscall_storage_free_enum(unsigned long obj_enum) 645 { 646 struct tee_storage_enum *e; 647 TEE_Result res; 648 struct tee_ta_session *sess; 649 struct user_ta_ctx *utc; 650 651 res = tee_ta_get_current_session(&sess); 652 if (res != TEE_SUCCESS) 653 return res; 654 utc = to_user_ta_ctx(sess->ctx); 655 656 res = tee_svc_storage_get_enum(utc, 657 tee_svc_uref_to_vaddr(obj_enum), &e); 658 if (res != TEE_SUCCESS) 659 return res; 660 661 return tee_svc_close_enum(utc, e); 662 } 663 664 TEE_Result syscall_storage_reset_enum(unsigned long obj_enum) 665 { 666 struct tee_storage_enum *e; 667 TEE_Result res; 668 struct tee_ta_session *sess; 669 670 res = tee_ta_get_current_session(&sess); 671 if (res != TEE_SUCCESS) 672 return res; 673 674 res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx), 675 tee_svc_uref_to_vaddr(obj_enum), &e); 676 if (res != TEE_SUCCESS) 677 return res; 678 679 if (e->fops) { 680 e->fops->closedir(e->dir); 681 e->fops = NULL; 682 e->dir = NULL; 683 } 684 assert(!e->dir); 685 686 return TEE_SUCCESS; 687 } 688 689 TEE_Result syscall_storage_start_enum(unsigned long obj_enum, 690 unsigned long storage_id) 691 { 692 struct tee_storage_enum *e; 693 TEE_Result res; 694 struct tee_ta_session *sess; 695 const struct tee_file_operations *fops = 696 tee_svc_storage_file_ops(storage_id); 697 698 res = tee_ta_get_current_session(&sess); 699 if (res != TEE_SUCCESS) 700 return res; 701 702 res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx), 703 tee_svc_uref_to_vaddr(obj_enum), &e); 704 if (res != TEE_SUCCESS) 705 return res; 706 707 if (e->dir) { 708 e->fops->closedir(e->dir); 709 e->dir = NULL; 710 } 711 712 if (!fops) 713 return TEE_ERROR_ITEM_NOT_FOUND; 714 715 e->fops = fops; 716 717 return fops->opendir(&sess->ctx->uuid, &e->dir); 718 } 719 720 TEE_Result syscall_storage_next_enum(unsigned long obj_enum, 721 TEE_ObjectInfo *info, void *obj_id, uint64_t *len) 722 { 723 struct tee_ta_session *sess = NULL; 724 struct tee_storage_enum *e = NULL; 725 struct tee_fs_dirent *d = NULL; 726 struct user_ta_ctx *utc = NULL; 727 TEE_Result res = TEE_SUCCESS; 728 struct tee_obj *o = NULL; 729 uint64_t l = 0; 730 731 res = tee_ta_get_current_session(&sess); 732 if (res != TEE_SUCCESS) 733 goto exit; 734 utc = to_user_ta_ctx(sess->ctx); 735 736 res = tee_svc_storage_get_enum(utc, 737 tee_svc_uref_to_vaddr(obj_enum), &e); 738 if (res != TEE_SUCCESS) 739 goto exit; 740 741 /* check rights of the provided buffers */ 742 res = tee_mmu_check_access_rights(&utc->uctx, 743 TEE_MEMORY_ACCESS_WRITE | 744 TEE_MEMORY_ACCESS_ANY_OWNER, 745 (uaddr_t)info, 746 sizeof(TEE_ObjectInfo)); 747 if (res != TEE_SUCCESS) 748 goto exit; 749 750 res = tee_mmu_check_access_rights(&utc->uctx, 751 TEE_MEMORY_ACCESS_WRITE | 752 TEE_MEMORY_ACCESS_ANY_OWNER, 753 (uaddr_t)obj_id, 754 TEE_OBJECT_ID_MAX_LEN); 755 if (res != TEE_SUCCESS) 756 goto exit; 757 758 if (!e->fops) { 759 res = TEE_ERROR_ITEM_NOT_FOUND; 760 goto exit; 761 } 762 763 res = e->fops->readdir(e->dir, &d); 764 if (res != TEE_SUCCESS) 765 goto exit; 766 767 o = tee_obj_alloc(); 768 if (o == NULL) { 769 res = TEE_ERROR_OUT_OF_MEMORY; 770 goto exit; 771 } 772 773 res = tee_pobj_get(&sess->ctx->uuid, d->oid, d->oidlen, 0, 774 TEE_POBJ_USAGE_ENUM, e->fops, &o->pobj); 775 if (res) 776 goto exit; 777 778 o->info.handleFlags = o->pobj->flags | TEE_HANDLE_FLAG_PERSISTENT | 779 TEE_HANDLE_FLAG_INITIALIZED; 780 781 res = tee_svc_storage_read_head(o); 782 if (res != TEE_SUCCESS) 783 goto exit; 784 785 memcpy(info, &o->info, sizeof(TEE_ObjectInfo)); 786 memcpy(obj_id, o->pobj->obj_id, o->pobj->obj_id_len); 787 788 l = o->pobj->obj_id_len; 789 res = tee_svc_copy_to_user(len, &l, sizeof(*len)); 790 791 exit: 792 if (o) { 793 tee_pobj_release(o->pobj); 794 tee_obj_free(o); 795 } 796 797 return res; 798 } 799 800 TEE_Result syscall_storage_obj_read(unsigned long obj, void *data, size_t len, 801 uint64_t *count) 802 { 803 struct tee_ta_session *sess = NULL; 804 struct user_ta_ctx *utc = NULL; 805 TEE_Result res = TEE_SUCCESS; 806 struct tee_obj *o = NULL; 807 uint64_t u_count = 0; 808 size_t pos_tmp = 0; 809 size_t bytes = 0; 810 811 res = tee_ta_get_current_session(&sess); 812 if (res != TEE_SUCCESS) 813 goto exit; 814 utc = to_user_ta_ctx(sess->ctx); 815 816 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 817 if (res != TEE_SUCCESS) 818 goto exit; 819 820 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 821 res = TEE_ERROR_BAD_STATE; 822 goto exit; 823 } 824 825 if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_READ)) { 826 res = TEE_ERROR_ACCESS_CONFLICT; 827 goto exit; 828 } 829 830 /* Guard o->info.dataPosition += bytes below from overflowing */ 831 if (ADD_OVERFLOW(o->info.dataPosition, len, &pos_tmp)) { 832 res = TEE_ERROR_OVERFLOW; 833 goto exit; 834 } 835 836 /* check rights of the provided buffer */ 837 res = tee_mmu_check_access_rights(&utc->uctx, 838 TEE_MEMORY_ACCESS_WRITE | 839 TEE_MEMORY_ACCESS_ANY_OWNER, 840 (uaddr_t)data, len); 841 if (res != TEE_SUCCESS) 842 goto exit; 843 844 bytes = len; 845 if (ADD_OVERFLOW(o->ds_pos, o->info.dataPosition, &pos_tmp)) { 846 res = TEE_ERROR_OVERFLOW; 847 goto exit; 848 } 849 res = o->pobj->fops->read(o->fh, pos_tmp, data, &bytes); 850 if (res != TEE_SUCCESS) { 851 if (res == TEE_ERROR_CORRUPT_OBJECT) { 852 EMSG("Object corrupt"); 853 tee_svc_storage_remove_corrupt_obj(sess, o); 854 } 855 goto exit; 856 } 857 858 o->info.dataPosition += bytes; 859 860 u_count = bytes; 861 res = tee_svc_copy_to_user(count, &u_count, sizeof(*count)); 862 exit: 863 return res; 864 } 865 866 TEE_Result syscall_storage_obj_write(unsigned long obj, void *data, size_t len) 867 { 868 struct tee_ta_session *sess = NULL; 869 struct user_ta_ctx *utc = NULL; 870 TEE_Result res = TEE_SUCCESS; 871 struct tee_obj *o = NULL; 872 size_t pos_tmp = 0; 873 874 res = tee_ta_get_current_session(&sess); 875 if (res != TEE_SUCCESS) 876 goto exit; 877 utc = to_user_ta_ctx(sess->ctx); 878 879 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 880 if (res != TEE_SUCCESS) 881 goto exit; 882 883 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 884 res = TEE_ERROR_BAD_STATE; 885 goto exit; 886 } 887 888 if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE)) { 889 res = TEE_ERROR_ACCESS_CONFLICT; 890 goto exit; 891 } 892 893 /* Guard o->info.dataPosition += bytes below from overflowing */ 894 if (ADD_OVERFLOW(o->info.dataPosition, len, &pos_tmp)) { 895 res = TEE_ERROR_OVERFLOW; 896 goto exit; 897 } 898 899 /* check rights of the provided buffer */ 900 res = tee_mmu_check_access_rights(&utc->uctx, 901 TEE_MEMORY_ACCESS_READ | 902 TEE_MEMORY_ACCESS_ANY_OWNER, 903 (uaddr_t)data, len); 904 if (res != TEE_SUCCESS) 905 goto exit; 906 907 if (ADD_OVERFLOW(o->ds_pos, o->info.dataPosition, &pos_tmp)) { 908 res = TEE_ERROR_ACCESS_CONFLICT; 909 goto exit; 910 } 911 res = o->pobj->fops->write(o->fh, pos_tmp, data, len); 912 if (res != TEE_SUCCESS) 913 goto exit; 914 915 o->info.dataPosition += len; 916 if (o->info.dataPosition > o->info.dataSize) 917 o->info.dataSize = o->info.dataPosition; 918 919 exit: 920 return res; 921 } 922 923 TEE_Result syscall_storage_obj_trunc(unsigned long obj, size_t len) 924 { 925 TEE_Result res; 926 struct tee_ta_session *sess; 927 struct tee_obj *o; 928 size_t off; 929 size_t attr_size; 930 931 res = tee_ta_get_current_session(&sess); 932 if (res != TEE_SUCCESS) 933 goto exit; 934 935 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 936 tee_svc_uref_to_vaddr(obj), &o); 937 if (res != TEE_SUCCESS) 938 goto exit; 939 940 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 941 res = TEE_ERROR_BAD_STATE; 942 goto exit; 943 } 944 945 if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE)) { 946 res = TEE_ERROR_ACCESS_CONFLICT; 947 goto exit; 948 } 949 950 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 951 if (res != TEE_SUCCESS) 952 goto exit; 953 954 if (ADD_OVERFLOW(sizeof(struct tee_svc_storage_head), attr_size, 955 &off)) { 956 res = TEE_ERROR_OVERFLOW; 957 goto exit; 958 } 959 if (ADD_OVERFLOW(len, off, &off)) { 960 res = TEE_ERROR_OVERFLOW; 961 goto exit; 962 } 963 res = o->pobj->fops->truncate(o->fh, off); 964 switch (res) { 965 case TEE_SUCCESS: 966 o->info.dataSize = len; 967 break; 968 case TEE_ERROR_CORRUPT_OBJECT: 969 EMSG("Object corruption"); 970 (void)tee_svc_storage_remove_corrupt_obj(sess, o); 971 break; 972 default: 973 res = TEE_ERROR_GENERIC; 974 break; 975 } 976 977 exit: 978 return res; 979 } 980 981 TEE_Result syscall_storage_obj_seek(unsigned long obj, int32_t offset, 982 unsigned long whence) 983 { 984 TEE_Result res; 985 struct tee_ta_session *sess; 986 struct tee_obj *o; 987 tee_fs_off_t new_pos; 988 989 res = tee_ta_get_current_session(&sess); 990 if (res != TEE_SUCCESS) 991 return res; 992 993 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 994 tee_svc_uref_to_vaddr(obj), &o); 995 if (res != TEE_SUCCESS) 996 return res; 997 998 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) 999 return TEE_ERROR_BAD_STATE; 1000 1001 switch (whence) { 1002 case TEE_DATA_SEEK_SET: 1003 new_pos = offset; 1004 break; 1005 case TEE_DATA_SEEK_CUR: 1006 if (ADD_OVERFLOW(o->info.dataPosition, offset, &new_pos)) 1007 return TEE_ERROR_OVERFLOW; 1008 break; 1009 case TEE_DATA_SEEK_END: 1010 if (ADD_OVERFLOW(o->info.dataSize, offset, &new_pos)) 1011 return TEE_ERROR_OVERFLOW; 1012 break; 1013 default: 1014 return TEE_ERROR_BAD_PARAMETERS; 1015 } 1016 1017 if (new_pos < 0) 1018 new_pos = 0; 1019 1020 if (new_pos > TEE_DATA_MAX_POSITION) { 1021 EMSG("Position is beyond TEE_DATA_MAX_POSITION"); 1022 return TEE_ERROR_BAD_PARAMETERS; 1023 } 1024 1025 o->info.dataPosition = new_pos; 1026 1027 return TEE_SUCCESS; 1028 } 1029 1030 void tee_svc_storage_close_all_enum(struct user_ta_ctx *utc) 1031 { 1032 struct tee_storage_enum_head *eh = &utc->storage_enums; 1033 1034 /* disregard return value */ 1035 while (!TAILQ_EMPTY(eh)) 1036 tee_svc_close_enum(utc, TAILQ_FIRST(eh)); 1037 } 1038