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 TEE_Result res; 260 struct tee_ta_session *sess; 261 struct tee_obj *o = NULL; 262 char *file = NULL; 263 struct tee_pobj *po = NULL; 264 struct user_ta_ctx *utc; 265 const struct tee_file_operations *fops = 266 tee_svc_storage_file_ops(storage_id); 267 268 if (!fops) { 269 res = TEE_ERROR_ITEM_NOT_FOUND; 270 goto exit; 271 } 272 273 if (object_id_len > TEE_OBJECT_ID_MAX_LEN) { 274 res = TEE_ERROR_BAD_PARAMETERS; 275 goto exit; 276 } 277 278 res = tee_ta_get_current_session(&sess); 279 if (res != TEE_SUCCESS) 280 goto err; 281 utc = to_user_ta_ctx(sess->ctx); 282 283 res = tee_mmu_check_access_rights(utc, 284 TEE_MEMORY_ACCESS_READ, 285 (uaddr_t) object_id, 286 object_id_len); 287 if (res != TEE_SUCCESS) 288 goto err; 289 290 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 291 object_id_len, flags, false, fops, &po); 292 if (res != TEE_SUCCESS) 293 goto err; 294 295 o = tee_obj_alloc(); 296 if (o == NULL) { 297 tee_pobj_release(po); 298 res = TEE_ERROR_OUT_OF_MEMORY; 299 goto err; 300 } 301 302 o->info.handleFlags = 303 TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED; 304 o->flags = flags; 305 o->pobj = po; 306 tee_obj_add(utc, o); 307 308 res = tee_svc_storage_read_head(o); 309 if (res != TEE_SUCCESS) { 310 if (res == TEE_ERROR_CORRUPT_OBJECT) { 311 EMSG("Object corrupt"); 312 goto err; 313 } 314 goto oclose; 315 } 316 317 res = tee_svc_copy_kaddr_to_uref(obj, o); 318 if (res != TEE_SUCCESS) 319 goto oclose; 320 321 goto exit; 322 323 oclose: 324 tee_obj_close(utc, o); 325 o = NULL; 326 327 err: 328 if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT) 329 res = TEE_ERROR_CORRUPT_OBJECT; 330 if (res == TEE_ERROR_CORRUPT_OBJECT && o) 331 tee_svc_storage_remove_corrupt_obj(sess, o); 332 333 exit: 334 free(file); 335 file = NULL; 336 return res; 337 } 338 339 static TEE_Result tee_svc_storage_init_file(struct tee_obj *o, 340 struct tee_obj *attr_o, void *data, 341 uint32_t len) 342 { 343 TEE_Result res = TEE_SUCCESS; 344 struct tee_svc_storage_head head; 345 const struct tee_file_operations *fops = o->pobj->fops; 346 void *attr = NULL; 347 size_t attr_size = 0; 348 349 if (attr_o) { 350 res = tee_obj_set_type(o, attr_o->info.objectType, 351 attr_o->info.maxKeySize); 352 if (res) 353 return res; 354 res = tee_obj_attr_copy_from(o, attr_o); 355 if (res) 356 return res; 357 o->have_attrs = attr_o->have_attrs; 358 o->info.objectUsage = attr_o->info.objectUsage; 359 o->info.keySize = attr_o->info.keySize; 360 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 361 if (res) 362 return res; 363 if (attr_size) { 364 attr = malloc(attr_size); 365 if (!attr) 366 return TEE_ERROR_OUT_OF_MEMORY; 367 res = tee_obj_attr_to_binary(o, attr, &attr_size); 368 if (res != TEE_SUCCESS) 369 goto exit; 370 } 371 } else { 372 res = tee_obj_set_type(o, TEE_TYPE_DATA, 0); 373 if (res != TEE_SUCCESS) 374 goto exit; 375 } 376 377 o->ds_pos = sizeof(struct tee_svc_storage_head) + attr_size; 378 379 /* write head */ 380 head.attr_size = attr_size; 381 head.keySize = o->info.keySize; 382 head.maxKeySize = o->info.maxKeySize; 383 head.objectUsage = o->info.objectUsage; 384 head.objectType = o->info.objectType; 385 head.have_attrs = o->have_attrs; 386 387 res = fops->create(o->pobj, !!(o->flags & TEE_DATA_FLAG_OVERWRITE), 388 &head, sizeof(head), attr, attr_size, data, len, 389 &o->fh); 390 391 if (!res) 392 o->info.dataSize = len; 393 exit: 394 free(attr); 395 return res; 396 } 397 398 TEE_Result syscall_storage_obj_create(unsigned long storage_id, void *object_id, 399 size_t object_id_len, unsigned long flags, 400 unsigned long attr, void *data, size_t len, 401 uint32_t *obj) 402 { 403 TEE_Result res; 404 struct tee_ta_session *sess; 405 struct tee_obj *o = NULL; 406 struct tee_obj *attr_o = NULL; 407 struct tee_pobj *po = NULL; 408 struct user_ta_ctx *utc; 409 const struct tee_file_operations *fops = 410 tee_svc_storage_file_ops(storage_id); 411 412 if (!fops) 413 return TEE_ERROR_ITEM_NOT_FOUND; 414 415 if (object_id_len > TEE_OBJECT_ID_MAX_LEN) 416 return TEE_ERROR_BAD_PARAMETERS; 417 418 res = tee_ta_get_current_session(&sess); 419 if (res != TEE_SUCCESS) 420 return res; 421 utc = to_user_ta_ctx(sess->ctx); 422 423 res = tee_mmu_check_access_rights(utc, 424 TEE_MEMORY_ACCESS_READ, 425 (uaddr_t) object_id, 426 object_id_len); 427 if (res != TEE_SUCCESS) 428 goto err; 429 430 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 431 object_id_len, flags, true, fops, &po); 432 if (res != TEE_SUCCESS) 433 goto err; 434 435 /* check rights of the provided buffer */ 436 if (len) { 437 if (data) { 438 res = tee_mmu_check_access_rights(utc, 439 TEE_MEMORY_ACCESS_READ | 440 TEE_MEMORY_ACCESS_ANY_OWNER, 441 (uaddr_t) data, len); 442 443 if (res != TEE_SUCCESS) 444 goto err; 445 } else { 446 res = TEE_ERROR_BAD_PARAMETERS; 447 goto err; 448 } 449 } 450 451 o = tee_obj_alloc(); 452 if (o == NULL) { 453 res = TEE_ERROR_OUT_OF_MEMORY; 454 goto err; 455 } 456 457 o->info.handleFlags = 458 TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED; 459 o->flags = flags; 460 o->pobj = po; 461 462 if (attr != TEE_HANDLE_NULL) { 463 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(attr), 464 &attr_o); 465 if (res != TEE_SUCCESS) 466 goto err; 467 } 468 469 res = tee_svc_storage_init_file(o, attr_o, data, len); 470 if (res != TEE_SUCCESS) 471 goto err; 472 473 po = NULL; /* o owns it from now on */ 474 tee_obj_add(utc, o); 475 476 res = tee_svc_copy_kaddr_to_uref(obj, o); 477 if (res != TEE_SUCCESS) 478 goto oclose; 479 480 return TEE_SUCCESS; 481 482 oclose: 483 tee_obj_close(utc, o); 484 return res; 485 486 err: 487 if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT) 488 res = TEE_ERROR_CORRUPT_OBJECT; 489 if (res == TEE_ERROR_CORRUPT_OBJECT && po) 490 fops->remove(po); 491 if (o) { 492 fops->close(&o->fh); 493 tee_obj_free(o); 494 } 495 if (po) 496 tee_pobj_release(po); 497 498 return res; 499 } 500 501 TEE_Result syscall_storage_obj_del(unsigned long obj) 502 { 503 TEE_Result res; 504 struct tee_ta_session *sess; 505 struct tee_obj *o; 506 struct user_ta_ctx *utc; 507 508 res = tee_ta_get_current_session(&sess); 509 if (res != TEE_SUCCESS) 510 return res; 511 utc = to_user_ta_ctx(sess->ctx); 512 513 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 514 if (res != TEE_SUCCESS) 515 return res; 516 517 if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE_META)) 518 return TEE_ERROR_ACCESS_CONFLICT; 519 520 if (o->pobj == NULL || o->pobj->obj_id == NULL) 521 return TEE_ERROR_BAD_STATE; 522 523 res = o->pobj->fops->remove(o->pobj); 524 tee_obj_close(utc, o); 525 526 return res; 527 } 528 529 TEE_Result syscall_storage_obj_rename(unsigned long obj, void *object_id, 530 size_t object_id_len) 531 { 532 TEE_Result res; 533 struct tee_ta_session *sess; 534 struct tee_obj *o; 535 struct tee_pobj *po = NULL; 536 char *new_file = NULL; 537 char *old_file = NULL; 538 struct user_ta_ctx *utc; 539 const struct tee_file_operations *fops; 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_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 550 if (res != TEE_SUCCESS) 551 return res; 552 553 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 554 res = TEE_ERROR_BAD_STATE; 555 goto exit; 556 } 557 558 if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE_META)) { 559 res = TEE_ERROR_BAD_STATE; 560 goto exit; 561 } 562 563 if (o->pobj == NULL || o->pobj->obj_id == NULL) { 564 res = TEE_ERROR_BAD_STATE; 565 goto exit; 566 } 567 568 res = tee_mmu_check_access_rights(utc, 569 TEE_MEMORY_ACCESS_READ, 570 (uaddr_t) object_id, object_id_len); 571 if (res != TEE_SUCCESS) 572 goto exit; 573 574 /* reserve dest name */ 575 fops = o->pobj->fops; 576 res = tee_pobj_get((void *)&sess->ctx->uuid, object_id, 577 object_id_len, TEE_DATA_FLAG_ACCESS_WRITE_META, 578 false, fops, &po); 579 if (res != TEE_SUCCESS) 580 goto exit; 581 582 /* move */ 583 res = fops->rename(o->pobj, po, false /* no overwrite */); 584 if (res) 585 goto exit; 586 587 res = tee_pobj_rename(o->pobj, object_id, object_id_len); 588 589 exit: 590 tee_pobj_release(po); 591 592 free(new_file); 593 free(old_file); 594 595 return res; 596 } 597 598 TEE_Result syscall_storage_alloc_enum(uint32_t *obj_enum) 599 { 600 struct tee_storage_enum *e; 601 struct tee_ta_session *sess; 602 TEE_Result res; 603 struct user_ta_ctx *utc; 604 605 if (obj_enum == NULL) 606 return TEE_ERROR_BAD_PARAMETERS; 607 608 res = tee_ta_get_current_session(&sess); 609 if (res != TEE_SUCCESS) 610 return res; 611 utc = to_user_ta_ctx(sess->ctx); 612 613 e = malloc(sizeof(struct tee_storage_enum)); 614 if (e == NULL) 615 return TEE_ERROR_OUT_OF_MEMORY; 616 617 e->dir = NULL; 618 e->fops = NULL; 619 TAILQ_INSERT_TAIL(&utc->storage_enums, e, link); 620 621 return tee_svc_copy_kaddr_to_uref(obj_enum, e); 622 } 623 624 TEE_Result syscall_storage_free_enum(unsigned long obj_enum) 625 { 626 struct tee_storage_enum *e; 627 TEE_Result res; 628 struct tee_ta_session *sess; 629 struct user_ta_ctx *utc; 630 631 res = tee_ta_get_current_session(&sess); 632 if (res != TEE_SUCCESS) 633 return res; 634 utc = to_user_ta_ctx(sess->ctx); 635 636 res = tee_svc_storage_get_enum(utc, 637 tee_svc_uref_to_vaddr(obj_enum), &e); 638 if (res != TEE_SUCCESS) 639 return res; 640 641 return tee_svc_close_enum(utc, e); 642 } 643 644 TEE_Result syscall_storage_reset_enum(unsigned long obj_enum) 645 { 646 struct tee_storage_enum *e; 647 TEE_Result res; 648 struct tee_ta_session *sess; 649 650 res = tee_ta_get_current_session(&sess); 651 if (res != TEE_SUCCESS) 652 return res; 653 654 res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx), 655 tee_svc_uref_to_vaddr(obj_enum), &e); 656 if (res != TEE_SUCCESS) 657 return res; 658 659 if (e->fops) { 660 e->fops->closedir(e->dir); 661 e->fops = NULL; 662 e->dir = NULL; 663 } 664 assert(!e->dir); 665 666 return TEE_SUCCESS; 667 } 668 669 static TEE_Result tee_svc_storage_set_enum(struct tee_fs_dirent *d, 670 const struct tee_file_operations *fops, 671 struct tee_obj *o) 672 { 673 o->info.handleFlags = 674 TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED; 675 o->info.objectUsage = TEE_USAGE_DEFAULT; 676 677 if (d->oidlen > TEE_OBJECT_ID_MAX_LEN) 678 return TEE_ERROR_CORRUPT_OBJECT; 679 680 o->pobj->obj_id = malloc(d->oidlen); 681 if (!o->pobj->obj_id) 682 return TEE_ERROR_OUT_OF_MEMORY; 683 684 memcpy(o->pobj->obj_id, d->oid, d->oidlen); 685 o->pobj->obj_id_len = d->oidlen; 686 o->pobj->fops = fops; 687 688 return TEE_SUCCESS; 689 } 690 691 TEE_Result syscall_storage_start_enum(unsigned long obj_enum, 692 unsigned long storage_id) 693 { 694 struct tee_storage_enum *e; 695 TEE_Result res; 696 struct tee_ta_session *sess; 697 const struct tee_file_operations *fops = 698 tee_svc_storage_file_ops(storage_id); 699 700 res = tee_ta_get_current_session(&sess); 701 if (res != TEE_SUCCESS) 702 return res; 703 704 res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx), 705 tee_svc_uref_to_vaddr(obj_enum), &e); 706 if (res != TEE_SUCCESS) 707 return res; 708 709 if (!fops) 710 return TEE_ERROR_ITEM_NOT_FOUND; 711 712 e->fops = fops; 713 assert(!e->dir); 714 return fops->opendir(&sess->ctx->uuid, &e->dir); 715 } 716 717 TEE_Result syscall_storage_next_enum(unsigned long obj_enum, 718 TEE_ObjectInfo *info, void *obj_id, uint64_t *len) 719 { 720 struct tee_storage_enum *e; 721 struct tee_fs_dirent *d; 722 TEE_Result res = TEE_SUCCESS; 723 struct tee_ta_session *sess; 724 struct tee_obj *o = NULL; 725 uint64_t l; 726 struct user_ta_ctx *utc; 727 728 res = tee_ta_get_current_session(&sess); 729 if (res != TEE_SUCCESS) 730 goto exit; 731 utc = to_user_ta_ctx(sess->ctx); 732 733 res = tee_svc_storage_get_enum(utc, 734 tee_svc_uref_to_vaddr(obj_enum), &e); 735 if (res != TEE_SUCCESS) 736 goto exit; 737 738 /* check rights of the provided buffers */ 739 res = tee_mmu_check_access_rights(utc, 740 TEE_MEMORY_ACCESS_WRITE | 741 TEE_MEMORY_ACCESS_ANY_OWNER, 742 (uaddr_t) info, 743 sizeof(TEE_ObjectInfo)); 744 if (res != TEE_SUCCESS) 745 goto exit; 746 747 res = tee_mmu_check_access_rights(utc, 748 TEE_MEMORY_ACCESS_WRITE | 749 TEE_MEMORY_ACCESS_ANY_OWNER, 750 (uaddr_t) obj_id, 751 TEE_OBJECT_ID_MAX_LEN); 752 if (res != TEE_SUCCESS) 753 goto exit; 754 755 if (!e->fops) { 756 res = TEE_ERROR_ITEM_NOT_FOUND; 757 goto exit; 758 } 759 760 res = e->fops->readdir(e->dir, &d); 761 if (res != TEE_SUCCESS) 762 goto exit; 763 764 o = tee_obj_alloc(); 765 if (o == NULL) { 766 res = TEE_ERROR_OUT_OF_MEMORY; 767 goto exit; 768 } 769 o->flags = TEE_DATA_FLAG_SHARE_READ; 770 771 o->pobj = calloc(1, sizeof(struct tee_pobj)); 772 if (!o->pobj) { 773 res = TEE_ERROR_OUT_OF_MEMORY; 774 goto exit; 775 } 776 777 o->pobj->uuid = sess->ctx->uuid; 778 res = tee_svc_storage_set_enum(d, e->fops, o); 779 if (res != TEE_SUCCESS) 780 goto exit; 781 782 res = tee_svc_storage_read_head(o); 783 if (res != TEE_SUCCESS) 784 goto exit; 785 786 memcpy(info, &o->info, sizeof(TEE_ObjectInfo)); 787 memcpy(obj_id, o->pobj->obj_id, o->pobj->obj_id_len); 788 789 l = o->pobj->obj_id_len; 790 res = tee_svc_copy_to_user(len, &l, sizeof(*len)); 791 792 exit: 793 if (o) { 794 if (o->pobj) { 795 if (o->pobj->fops) 796 o->pobj->fops->close(&o->fh); 797 free(o->pobj->obj_id); 798 } 799 free(o->pobj); 800 tee_obj_free(o); 801 } 802 803 return res; 804 } 805 806 TEE_Result syscall_storage_obj_read(unsigned long obj, void *data, size_t len, 807 uint64_t *count) 808 { 809 TEE_Result res; 810 struct tee_ta_session *sess; 811 struct tee_obj *o; 812 uint64_t u_count; 813 struct user_ta_ctx *utc; 814 size_t bytes; 815 size_t pos_tmp = 0; 816 817 res = tee_ta_get_current_session(&sess); 818 if (res != TEE_SUCCESS) 819 goto exit; 820 utc = to_user_ta_ctx(sess->ctx); 821 822 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 823 if (res != TEE_SUCCESS) 824 goto exit; 825 826 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 827 res = TEE_ERROR_BAD_STATE; 828 goto exit; 829 } 830 831 if (!(o->flags & TEE_DATA_FLAG_ACCESS_READ)) { 832 res = TEE_ERROR_ACCESS_CONFLICT; 833 goto exit; 834 } 835 836 /* Guard o->info.dataPosition += bytes below from overflowing */ 837 if (ADD_OVERFLOW(o->info.dataPosition, len, &pos_tmp)) { 838 res = TEE_ERROR_OVERFLOW; 839 goto exit; 840 } 841 842 /* check rights of the provided buffer */ 843 res = tee_mmu_check_access_rights(utc, 844 TEE_MEMORY_ACCESS_WRITE | 845 TEE_MEMORY_ACCESS_ANY_OWNER, 846 (uaddr_t) data, len); 847 if (res != TEE_SUCCESS) 848 goto exit; 849 850 bytes = len; 851 if (ADD_OVERFLOW(o->ds_pos, o->info.dataPosition, &pos_tmp)) { 852 res = TEE_ERROR_OVERFLOW; 853 goto exit; 854 } 855 res = o->pobj->fops->read(o->fh, pos_tmp, data, &bytes); 856 if (res != TEE_SUCCESS) { 857 if (res == TEE_ERROR_CORRUPT_OBJECT) { 858 EMSG("Object corrupt"); 859 tee_svc_storage_remove_corrupt_obj(sess, o); 860 } 861 goto exit; 862 } 863 864 o->info.dataPosition += bytes; 865 866 u_count = bytes; 867 res = tee_svc_copy_to_user(count, &u_count, sizeof(*count)); 868 exit: 869 return res; 870 } 871 872 TEE_Result syscall_storage_obj_write(unsigned long obj, void *data, size_t len) 873 { 874 TEE_Result res; 875 struct tee_ta_session *sess; 876 struct tee_obj *o; 877 struct user_ta_ctx *utc; 878 size_t pos_tmp = 0; 879 880 res = tee_ta_get_current_session(&sess); 881 if (res != TEE_SUCCESS) 882 goto exit; 883 utc = to_user_ta_ctx(sess->ctx); 884 885 res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o); 886 if (res != TEE_SUCCESS) 887 goto exit; 888 889 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 890 res = TEE_ERROR_BAD_STATE; 891 goto exit; 892 } 893 894 if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE)) { 895 res = TEE_ERROR_ACCESS_CONFLICT; 896 goto exit; 897 } 898 899 /* Guard o->info.dataPosition += bytes below from overflowing */ 900 if (ADD_OVERFLOW(o->info.dataPosition, len, &pos_tmp)) { 901 res = TEE_ERROR_OVERFLOW; 902 goto exit; 903 } 904 905 /* check rights of the provided buffer */ 906 res = tee_mmu_check_access_rights(utc, 907 TEE_MEMORY_ACCESS_READ | 908 TEE_MEMORY_ACCESS_ANY_OWNER, 909 (uaddr_t) data, len); 910 if (res != TEE_SUCCESS) 911 goto exit; 912 913 if (ADD_OVERFLOW(o->ds_pos, o->info.dataPosition, &pos_tmp)) { 914 res = TEE_ERROR_ACCESS_CONFLICT; 915 goto exit; 916 } 917 res = o->pobj->fops->write(o->fh, pos_tmp, data, len); 918 if (res != TEE_SUCCESS) 919 goto exit; 920 921 o->info.dataPosition += len; 922 if (o->info.dataPosition > o->info.dataSize) 923 o->info.dataSize = o->info.dataPosition; 924 925 exit: 926 return res; 927 } 928 929 TEE_Result syscall_storage_obj_trunc(unsigned long obj, size_t len) 930 { 931 TEE_Result res; 932 struct tee_ta_session *sess; 933 struct tee_obj *o; 934 size_t off; 935 size_t attr_size; 936 937 res = tee_ta_get_current_session(&sess); 938 if (res != TEE_SUCCESS) 939 goto exit; 940 941 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 942 tee_svc_uref_to_vaddr(obj), &o); 943 if (res != TEE_SUCCESS) 944 goto exit; 945 946 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) { 947 res = TEE_ERROR_BAD_STATE; 948 goto exit; 949 } 950 951 if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE)) { 952 res = TEE_ERROR_ACCESS_CONFLICT; 953 goto exit; 954 } 955 956 res = tee_obj_attr_to_binary(o, NULL, &attr_size); 957 if (res != TEE_SUCCESS) 958 goto exit; 959 960 if (ADD_OVERFLOW(sizeof(struct tee_svc_storage_head), attr_size, 961 &off)) { 962 res = TEE_ERROR_OVERFLOW; 963 goto exit; 964 } 965 if (ADD_OVERFLOW(len, off, &off)) { 966 res = TEE_ERROR_OVERFLOW; 967 goto exit; 968 } 969 res = o->pobj->fops->truncate(o->fh, off); 970 switch (res) { 971 case TEE_SUCCESS: 972 o->info.dataSize = len; 973 break; 974 case TEE_ERROR_CORRUPT_OBJECT: 975 EMSG("Object corruption"); 976 (void)tee_svc_storage_remove_corrupt_obj(sess, o); 977 break; 978 default: 979 res = TEE_ERROR_GENERIC; 980 break; 981 } 982 983 exit: 984 return res; 985 } 986 987 TEE_Result syscall_storage_obj_seek(unsigned long obj, int32_t offset, 988 unsigned long whence) 989 { 990 TEE_Result res; 991 struct tee_ta_session *sess; 992 struct tee_obj *o; 993 tee_fs_off_t new_pos; 994 995 res = tee_ta_get_current_session(&sess); 996 if (res != TEE_SUCCESS) 997 return res; 998 999 res = tee_obj_get(to_user_ta_ctx(sess->ctx), 1000 tee_svc_uref_to_vaddr(obj), &o); 1001 if (res != TEE_SUCCESS) 1002 return res; 1003 1004 if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) 1005 return TEE_ERROR_BAD_STATE; 1006 1007 switch (whence) { 1008 case TEE_DATA_SEEK_SET: 1009 new_pos = offset; 1010 break; 1011 case TEE_DATA_SEEK_CUR: 1012 if (ADD_OVERFLOW(o->info.dataPosition, offset, &new_pos)) 1013 return TEE_ERROR_OVERFLOW; 1014 break; 1015 case TEE_DATA_SEEK_END: 1016 if (ADD_OVERFLOW(o->info.dataSize, offset, &new_pos)) 1017 return TEE_ERROR_OVERFLOW; 1018 break; 1019 default: 1020 return TEE_ERROR_BAD_PARAMETERS; 1021 } 1022 1023 if (new_pos < 0) 1024 new_pos = 0; 1025 1026 if (new_pos > TEE_DATA_MAX_POSITION) { 1027 EMSG("Position is beyond TEE_DATA_MAX_POSITION"); 1028 return TEE_ERROR_BAD_PARAMETERS; 1029 } 1030 1031 o->info.dataPosition = new_pos; 1032 1033 return TEE_SUCCESS; 1034 } 1035 1036 void tee_svc_storage_close_all_enum(struct user_ta_ctx *utc) 1037 { 1038 struct tee_storage_enum_head *eh = &utc->storage_enums; 1039 1040 /* disregard return value */ 1041 while (!TAILQ_EMPTY(eh)) 1042 tee_svc_close_enum(utc, TAILQ_FIRST(eh)); 1043 } 1044