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