1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <crypto/crypto.h> 8 #include <initcall.h> 9 #include <kernel/tee_common_otp.h> 10 #include <optee_msg_supplicant.h> 11 #include <stdlib.h> 12 #include <string_ext.h> 13 #include <string.h> 14 #include <tee/fs_htree.h> 15 #include <tee/tee_fs_key_manager.h> 16 #include <tee/tee_fs_rpc.h> 17 #include <utee_defines.h> 18 #include <util.h> 19 20 #define TEE_FS_HTREE_CHIP_ID_SIZE 32 21 #define TEE_FS_HTREE_HASH_ALG TEE_ALG_SHA256 22 #define TEE_FS_HTREE_TSK_SIZE TEE_FS_HTREE_HASH_SIZE 23 #define TEE_FS_HTREE_ENC_ALG TEE_ALG_AES_ECB_NOPAD 24 #define TEE_FS_HTREE_ENC_SIZE TEE_AES_BLOCK_SIZE 25 #define TEE_FS_HTREE_SSK_SIZE TEE_FS_HTREE_HASH_SIZE 26 27 #define TEE_FS_HTREE_AUTH_ENC_ALG TEE_ALG_AES_GCM 28 #define TEE_FS_HTREE_HMAC_ALG TEE_ALG_HMAC_SHA256 29 30 #define BLOCK_NUM_TO_NODE_ID(num) ((num) + 1) 31 32 #define NODE_ID_TO_BLOCK_NUM(id) ((id) - 1) 33 34 /* 35 * The hash tree is implemented as a binary tree with the purpose to ensure 36 * integrity of the data in the nodes. The data in the nodes their turn 37 * provides both integrity and confidentiality of the data blocks. 38 * 39 * The hash tree is saved in a file as: 40 * +----------------------------+ 41 * | htree_image.0 | 42 * | htree_image.1 | 43 * +----------------------------+ 44 * | htree_node_image.1.0 | 45 * | htree_node_image.1.1 | 46 * +----------------------------+ 47 * | htree_node_image.2.0 | 48 * | htree_node_image.2.1 | 49 * +----------------------------+ 50 * | htree_node_image.3.0 | 51 * | htree_node_image.3.1 | 52 * +----------------------------+ 53 * | htree_node_image.4.0 | 54 * | htree_node_image.4.1 | 55 * +----------------------------+ 56 * ... 57 * 58 * htree_image is the header of the file, there's two instances of it. One 59 * which is committed and the other is used when updating the file. Which 60 * is committed is indicated by the "counter" field, the one with the 61 * largest value is selected. 62 * 63 * htree_node_image is a node in the hash tree, each node has two instances 64 * which is committed is decided by the parent node .flag bit 65 * HTREE_NODE_COMMITTED_CHILD. Which version is the committed version of 66 * node 1 is determined by the by the lowest bit of the counter field in 67 * the header. 68 * 69 * Note that nodes start counting at 1 while blocks at 0, this means that 70 * block 0 is represented by node 1. 71 * 72 * Where different elements are stored in the file is managed by the file 73 * system. 74 */ 75 76 #define HTREE_NODE_COMMITTED_BLOCK BIT32(0) 77 /* n is 0 or 1 */ 78 #define HTREE_NODE_COMMITTED_CHILD(n) BIT32(1 + (n)) 79 80 struct htree_node { 81 size_t id; 82 bool dirty; 83 bool block_updated; 84 struct tee_fs_htree_node_image node; 85 struct htree_node *parent; 86 struct htree_node *child[2]; 87 }; 88 89 struct tee_fs_htree { 90 struct htree_node root; 91 struct tee_fs_htree_image head; 92 uint8_t fek[TEE_FS_HTREE_FEK_SIZE]; 93 struct tee_fs_htree_imeta imeta; 94 bool dirty; 95 const TEE_UUID *uuid; 96 const struct tee_fs_htree_storage *stor; 97 void *stor_aux; 98 }; 99 100 struct traverse_arg; 101 typedef TEE_Result (*traverse_cb_t)(struct traverse_arg *targ, 102 struct htree_node *node); 103 struct traverse_arg { 104 struct tee_fs_htree *ht; 105 traverse_cb_t cb; 106 void *arg; 107 }; 108 109 static TEE_Result rpc_read(struct tee_fs_htree *ht, enum tee_fs_htree_type type, 110 size_t idx, size_t vers, void *data, size_t dlen) 111 { 112 TEE_Result res; 113 struct tee_fs_rpc_operation op; 114 size_t bytes; 115 void *p; 116 117 res = ht->stor->rpc_read_init(ht->stor_aux, &op, type, idx, vers, &p); 118 if (res != TEE_SUCCESS) 119 return res; 120 121 res = ht->stor->rpc_read_final(&op, &bytes); 122 if (res != TEE_SUCCESS) 123 return res; 124 125 if (bytes != dlen) 126 return TEE_ERROR_CORRUPT_OBJECT; 127 128 memcpy(data, p, dlen); 129 return TEE_SUCCESS; 130 } 131 132 static TEE_Result rpc_read_head(struct tee_fs_htree *ht, size_t vers, 133 struct tee_fs_htree_image *head) 134 { 135 return rpc_read(ht, TEE_FS_HTREE_TYPE_HEAD, 0, vers, 136 head, sizeof(*head)); 137 } 138 139 static TEE_Result rpc_read_node(struct tee_fs_htree *ht, size_t node_id, 140 size_t vers, 141 struct tee_fs_htree_node_image *node) 142 { 143 return rpc_read(ht, TEE_FS_HTREE_TYPE_NODE, node_id - 1, vers, 144 node, sizeof(*node)); 145 } 146 147 static TEE_Result rpc_write(struct tee_fs_htree *ht, 148 enum tee_fs_htree_type type, size_t idx, 149 size_t vers, const void *data, size_t dlen) 150 { 151 TEE_Result res; 152 struct tee_fs_rpc_operation op; 153 void *p; 154 155 res = ht->stor->rpc_write_init(ht->stor_aux, &op, type, idx, vers, &p); 156 if (res != TEE_SUCCESS) 157 return res; 158 159 memcpy(p, data, dlen); 160 return ht->stor->rpc_write_final(&op); 161 } 162 163 static TEE_Result rpc_write_head(struct tee_fs_htree *ht, size_t vers, 164 const struct tee_fs_htree_image *head) 165 { 166 return rpc_write(ht, TEE_FS_HTREE_TYPE_HEAD, 0, vers, 167 head, sizeof(*head)); 168 } 169 170 static TEE_Result rpc_write_node(struct tee_fs_htree *ht, size_t node_id, 171 size_t vers, 172 const struct tee_fs_htree_node_image *node) 173 { 174 return rpc_write(ht, TEE_FS_HTREE_TYPE_NODE, node_id - 1, vers, 175 node, sizeof(*node)); 176 } 177 178 static TEE_Result traverse_post_order(struct traverse_arg *targ, 179 struct htree_node *node) 180 { 181 TEE_Result res; 182 183 /* 184 * This function is recursing but not very deep, only with Log(N) 185 * maximum depth. 186 */ 187 188 if (!node) 189 return TEE_SUCCESS; 190 191 res = traverse_post_order(targ, node->child[0]); 192 if (res != TEE_SUCCESS) 193 return res; 194 195 res = traverse_post_order(targ, node->child[1]); 196 if (res != TEE_SUCCESS) 197 return res; 198 199 return targ->cb(targ, node); 200 } 201 202 static TEE_Result htree_traverse_post_order(struct tee_fs_htree *ht, 203 traverse_cb_t cb, void *arg) 204 { 205 struct traverse_arg targ = { ht, cb, arg }; 206 207 return traverse_post_order(&targ, &ht->root); 208 } 209 210 static size_t node_id_to_level(size_t node_id) 211 { 212 assert(node_id && node_id < UINT_MAX); 213 /* Calculate level of the node, root node (1) has level 1 */ 214 return sizeof(unsigned int) * 8 - __builtin_clz(node_id); 215 } 216 217 static struct htree_node *find_closest_node(struct tee_fs_htree *ht, 218 size_t node_id) 219 { 220 struct htree_node *node = &ht->root; 221 size_t level = node_id_to_level(node_id); 222 size_t n; 223 224 /* n = 1 because root node is level 1 */ 225 for (n = 1; n < level; n++) { 226 struct htree_node *child; 227 size_t bit_idx; 228 229 /* 230 * The difference between levels of the current node and 231 * the node we're looking for tells which bit decides 232 * direction in the tree. 233 * 234 * As the first bit has index 0 we'll subtract 1 235 */ 236 bit_idx = level - n - 1; 237 child = node->child[((node_id >> bit_idx) & 1)]; 238 if (!child) 239 return node; 240 node = child; 241 } 242 243 return node; 244 } 245 246 static struct htree_node *find_node(struct tee_fs_htree *ht, size_t node_id) 247 { 248 struct htree_node *node = find_closest_node(ht, node_id); 249 250 if (node && node->id == node_id) 251 return node; 252 return NULL; 253 } 254 255 static TEE_Result get_node(struct tee_fs_htree *ht, bool create, 256 size_t node_id, struct htree_node **node_ret) 257 { 258 struct htree_node *node; 259 struct htree_node *nc; 260 size_t n; 261 262 node = find_closest_node(ht, node_id); 263 if (!node) 264 return TEE_ERROR_GENERIC; 265 if (node->id == node_id) 266 goto ret_node; 267 268 /* 269 * Trying to read beyond end of file should be caught earlier than 270 * here. 271 */ 272 if (!create) 273 return TEE_ERROR_GENERIC; 274 275 /* 276 * Add missing nodes, some nodes may already be there. When we've 277 * processed the range all nodes up to node_id will be in the tree. 278 */ 279 for (n = node->id + 1; n <= node_id; n++) { 280 node = find_closest_node(ht, n); 281 if (node->id == n) 282 continue; 283 /* Node id n should be a child of node */ 284 assert((n >> 1) == node->id); 285 assert(!node->child[n & 1]); 286 287 nc = calloc(1, sizeof(*nc)); 288 if (!nc) 289 return TEE_ERROR_OUT_OF_MEMORY; 290 nc->id = n; 291 nc->parent = node; 292 node->child[n & 1] = nc; 293 node = nc; 294 } 295 296 if (node->id > ht->imeta.max_node_id) 297 ht->imeta.max_node_id = node->id; 298 299 ret_node: 300 *node_ret = node; 301 return TEE_SUCCESS; 302 } 303 304 static int get_idx_from_counter(uint32_t counter0, uint32_t counter1) 305 { 306 if (!(counter0 & 1)) { 307 if (!(counter1 & 1)) 308 return 0; 309 if (counter0 > counter1) 310 return 0; 311 else 312 return 1; 313 } 314 315 if (counter1 & 1) 316 return 1; 317 else 318 return -1; 319 } 320 321 static TEE_Result init_head_from_data(struct tee_fs_htree *ht, 322 const uint8_t *hash) 323 { 324 TEE_Result res; 325 int idx; 326 327 if (hash) { 328 for (idx = 0;; idx++) { 329 res = rpc_read_node(ht, 1, idx, &ht->root.node); 330 if (res != TEE_SUCCESS) 331 return res; 332 333 if (!memcmp(ht->root.node.hash, hash, 334 sizeof(ht->root.node.hash))) { 335 res = rpc_read_head(ht, idx, &ht->head); 336 if (res != TEE_SUCCESS) 337 return res; 338 break; 339 } 340 341 if (idx) 342 return TEE_ERROR_SECURITY; 343 } 344 } else { 345 struct tee_fs_htree_image head[2]; 346 347 for (idx = 0; idx < 2; idx++) { 348 res = rpc_read_head(ht, idx, head + idx); 349 if (res != TEE_SUCCESS) 350 return res; 351 } 352 353 idx = get_idx_from_counter(head[0].counter, head[1].counter); 354 if (idx < 0) 355 return TEE_ERROR_SECURITY; 356 357 res = rpc_read_node(ht, 1, idx, &ht->root.node); 358 if (res != TEE_SUCCESS) 359 return res; 360 361 ht->head = head[idx]; 362 } 363 364 ht->root.id = 1; 365 366 return TEE_SUCCESS; 367 } 368 369 static TEE_Result init_tree_from_data(struct tee_fs_htree *ht) 370 { 371 TEE_Result res; 372 struct tee_fs_htree_node_image node_image; 373 struct htree_node *node; 374 struct htree_node *nc; 375 size_t committed_version; 376 size_t node_id = 2; 377 378 while (node_id <= ht->imeta.max_node_id) { 379 node = find_node(ht, node_id >> 1); 380 if (!node) 381 return TEE_ERROR_GENERIC; 382 committed_version = !!(node->node.flags & 383 HTREE_NODE_COMMITTED_CHILD(node_id & 1)); 384 385 res = rpc_read_node(ht, node_id, committed_version, 386 &node_image); 387 if (res != TEE_SUCCESS) 388 return res; 389 390 res = get_node(ht, true, node_id, &nc); 391 if (res != TEE_SUCCESS) 392 return res; 393 nc->node = node_image; 394 node_id++; 395 } 396 397 return TEE_SUCCESS; 398 } 399 400 static TEE_Result calc_node_hash(struct htree_node *node, 401 struct tee_fs_htree_meta *meta, void *ctx, 402 uint8_t *digest) 403 { 404 TEE_Result res; 405 uint32_t alg = TEE_FS_HTREE_HASH_ALG; 406 uint8_t *ndata = (uint8_t *)&node->node + sizeof(node->node.hash); 407 size_t nsize = sizeof(node->node) - sizeof(node->node.hash); 408 409 res = crypto_hash_init(ctx, alg); 410 if (res != TEE_SUCCESS) 411 return res; 412 413 res = crypto_hash_update(ctx, alg, ndata, nsize); 414 if (res != TEE_SUCCESS) 415 return res; 416 417 if (meta) { 418 res = crypto_hash_update(ctx, alg, (void *)meta, sizeof(meta)); 419 if (res != TEE_SUCCESS) 420 return res; 421 } 422 423 if (node->child[0]) { 424 res = crypto_hash_update(ctx, alg, node->child[0]->node.hash, 425 sizeof(node->child[0]->node.hash)); 426 if (res != TEE_SUCCESS) 427 return res; 428 } 429 430 if (node->child[1]) { 431 res = crypto_hash_update(ctx, alg, node->child[1]->node.hash, 432 sizeof(node->child[1]->node.hash)); 433 if (res != TEE_SUCCESS) 434 return res; 435 } 436 437 return crypto_hash_final(ctx, alg, digest, TEE_FS_HTREE_HASH_SIZE); 438 } 439 440 static TEE_Result authenc_init(void **ctx_ret, TEE_OperationMode mode, 441 struct tee_fs_htree *ht, 442 struct tee_fs_htree_node_image *ni, 443 size_t payload_len) 444 { 445 TEE_Result res = TEE_SUCCESS; 446 const uint32_t alg = TEE_FS_HTREE_AUTH_ENC_ALG; 447 void *ctx; 448 size_t aad_len = TEE_FS_HTREE_FEK_SIZE + TEE_FS_HTREE_IV_SIZE; 449 uint8_t *iv; 450 451 if (ni) { 452 iv = ni->iv; 453 } else { 454 iv = ht->head.iv; 455 aad_len += TEE_FS_HTREE_HASH_SIZE + sizeof(ht->head.counter); 456 } 457 458 if (mode == TEE_MODE_ENCRYPT) { 459 res = crypto_rng_read(iv, TEE_FS_HTREE_IV_SIZE); 460 if (res != TEE_SUCCESS) 461 return res; 462 } 463 464 res = crypto_authenc_alloc_ctx(&ctx, alg); 465 if (res != TEE_SUCCESS) 466 return res; 467 468 res = crypto_authenc_init(ctx, alg, mode, ht->fek, 469 TEE_FS_HTREE_FEK_SIZE, iv, 470 TEE_FS_HTREE_IV_SIZE, TEE_FS_HTREE_TAG_SIZE, 471 aad_len, payload_len); 472 if (res != TEE_SUCCESS) 473 goto exit; 474 475 if (!ni) { 476 res = crypto_authenc_update_aad(ctx, alg, mode, 477 ht->root.node.hash, 478 TEE_FS_HTREE_FEK_SIZE); 479 if (res != TEE_SUCCESS) 480 goto exit; 481 482 res = crypto_authenc_update_aad(ctx, alg, mode, 483 (void *)&ht->head.counter, 484 sizeof(ht->head.counter)); 485 if (res != TEE_SUCCESS) 486 goto exit; 487 } 488 489 res = crypto_authenc_update_aad(ctx, alg, mode, ht->head.enc_fek, 490 TEE_FS_HTREE_FEK_SIZE); 491 if (res != TEE_SUCCESS) 492 goto exit; 493 494 res = crypto_authenc_update_aad(ctx, alg, mode, iv, 495 TEE_FS_HTREE_IV_SIZE); 496 497 exit: 498 if (res == TEE_SUCCESS) 499 *ctx_ret = ctx; 500 else 501 crypto_authenc_final(ctx, alg); 502 503 return res; 504 } 505 506 static TEE_Result authenc_decrypt_final(void *ctx, const uint8_t *tag, 507 const void *crypt, size_t len, 508 void *plain) 509 { 510 TEE_Result res; 511 size_t out_size = len; 512 513 res = crypto_authenc_dec_final(ctx, TEE_FS_HTREE_AUTH_ENC_ALG, crypt, 514 len, plain, &out_size, tag, 515 TEE_FS_HTREE_TAG_SIZE); 516 crypto_authenc_final(ctx, TEE_FS_HTREE_AUTH_ENC_ALG); 517 crypto_authenc_free_ctx(ctx, TEE_FS_HTREE_AUTH_ENC_ALG); 518 519 if (res == TEE_SUCCESS && out_size != len) 520 return TEE_ERROR_GENERIC; 521 if (res == TEE_ERROR_MAC_INVALID) 522 return TEE_ERROR_CORRUPT_OBJECT; 523 524 return res; 525 } 526 527 static TEE_Result authenc_encrypt_final(void *ctx, uint8_t *tag, 528 const void *plain, size_t len, 529 void *crypt) 530 { 531 TEE_Result res; 532 size_t out_size = len; 533 size_t out_tag_size = TEE_FS_HTREE_TAG_SIZE; 534 535 res = crypto_authenc_enc_final(ctx, TEE_FS_HTREE_AUTH_ENC_ALG, plain, 536 len, crypt, &out_size, tag, 537 &out_tag_size); 538 crypto_authenc_final(ctx, TEE_FS_HTREE_AUTH_ENC_ALG); 539 crypto_authenc_free_ctx(ctx, TEE_FS_HTREE_AUTH_ENC_ALG); 540 541 if (res == TEE_SUCCESS && 542 (out_size != len || out_tag_size != TEE_FS_HTREE_TAG_SIZE)) 543 return TEE_ERROR_GENERIC; 544 545 return res; 546 } 547 548 static TEE_Result verify_root(struct tee_fs_htree *ht) 549 { 550 TEE_Result res; 551 void *ctx; 552 553 res = tee_fs_fek_crypt(ht->uuid, TEE_MODE_DECRYPT, ht->head.enc_fek, 554 sizeof(ht->fek), ht->fek); 555 if (res != TEE_SUCCESS) 556 return res; 557 558 res = authenc_init(&ctx, TEE_MODE_DECRYPT, ht, NULL, sizeof(ht->imeta)); 559 if (res != TEE_SUCCESS) 560 return res; 561 562 return authenc_decrypt_final(ctx, ht->head.tag, ht->head.imeta, 563 sizeof(ht->imeta), &ht->imeta); 564 } 565 566 static TEE_Result verify_node(struct traverse_arg *targ, 567 struct htree_node *node) 568 { 569 void *ctx = targ->arg; 570 TEE_Result res; 571 uint8_t digest[TEE_FS_HTREE_HASH_SIZE]; 572 573 if (node->parent) 574 res = calc_node_hash(node, NULL, ctx, digest); 575 else 576 res = calc_node_hash(node, &targ->ht->imeta.meta, ctx, digest); 577 if (res == TEE_SUCCESS && 578 buf_compare_ct(digest, node->node.hash, sizeof(digest))) 579 return TEE_ERROR_CORRUPT_OBJECT; 580 581 return res; 582 } 583 584 static TEE_Result verify_tree(struct tee_fs_htree *ht) 585 { 586 TEE_Result res; 587 void *ctx; 588 589 res = crypto_hash_alloc_ctx(&ctx, TEE_FS_HTREE_HASH_ALG); 590 if (res != TEE_SUCCESS) 591 return res; 592 593 res = htree_traverse_post_order(ht, verify_node, ctx); 594 crypto_hash_free_ctx(ctx, TEE_FS_HTREE_HASH_ALG); 595 596 return res; 597 } 598 599 static TEE_Result init_root_node(struct tee_fs_htree *ht) 600 { 601 TEE_Result res; 602 void *ctx; 603 604 res = crypto_hash_alloc_ctx(&ctx, TEE_FS_HTREE_HASH_ALG); 605 if (res != TEE_SUCCESS) 606 return res; 607 608 ht->root.id = 1; 609 ht->root.dirty = true; 610 611 res = calc_node_hash(&ht->root, &ht->imeta.meta, ctx, 612 ht->root.node.hash); 613 crypto_hash_free_ctx(ctx, TEE_FS_HTREE_HASH_ALG); 614 615 return res; 616 } 617 618 TEE_Result tee_fs_htree_open(bool create, uint8_t *hash, const TEE_UUID *uuid, 619 const struct tee_fs_htree_storage *stor, 620 void *stor_aux, struct tee_fs_htree **ht_ret) 621 { 622 TEE_Result res; 623 struct tee_fs_htree *ht = calloc(1, sizeof(*ht)); 624 625 if (!ht) 626 return TEE_ERROR_OUT_OF_MEMORY; 627 628 ht->uuid = uuid; 629 ht->stor = stor; 630 ht->stor_aux = stor_aux; 631 632 if (create) { 633 const struct tee_fs_htree_image dummy_head = { .counter = 0 }; 634 635 res = crypto_rng_read(ht->fek, sizeof(ht->fek)); 636 if (res != TEE_SUCCESS) 637 goto out; 638 639 res = tee_fs_fek_crypt(ht->uuid, TEE_MODE_ENCRYPT, ht->fek, 640 sizeof(ht->fek), ht->head.enc_fek); 641 if (res != TEE_SUCCESS) 642 goto out; 643 644 res = init_root_node(ht); 645 if (res != TEE_SUCCESS) 646 goto out; 647 648 ht->dirty = true; 649 res = tee_fs_htree_sync_to_storage(&ht, hash); 650 if (res != TEE_SUCCESS) 651 goto out; 652 res = rpc_write_head(ht, 0, &dummy_head); 653 } else { 654 res = init_head_from_data(ht, hash); 655 if (res != TEE_SUCCESS) 656 goto out; 657 658 res = verify_root(ht); 659 if (res != TEE_SUCCESS) 660 goto out; 661 662 res = init_tree_from_data(ht); 663 if (res != TEE_SUCCESS) 664 goto out; 665 666 res = verify_tree(ht); 667 } 668 out: 669 if (res == TEE_SUCCESS) 670 *ht_ret = ht; 671 else 672 tee_fs_htree_close(&ht); 673 return res; 674 } 675 676 struct tee_fs_htree_meta *tee_fs_htree_get_meta(struct tee_fs_htree *ht) 677 { 678 return &ht->imeta.meta; 679 } 680 681 void tee_fs_htree_meta_set_dirty(struct tee_fs_htree *ht) 682 { 683 ht->dirty = true; 684 ht->root.dirty = true; 685 } 686 687 static TEE_Result free_node(struct traverse_arg *targ __unused, 688 struct htree_node *node) 689 { 690 if (node->parent) 691 free(node); 692 return TEE_SUCCESS; 693 } 694 695 void tee_fs_htree_close(struct tee_fs_htree **ht) 696 { 697 if (!*ht) 698 return; 699 htree_traverse_post_order(*ht, free_node, NULL); 700 free(*ht); 701 *ht = NULL; 702 } 703 704 static TEE_Result htree_sync_node_to_storage(struct traverse_arg *targ, 705 struct htree_node *node) 706 { 707 TEE_Result res; 708 uint8_t vers; 709 struct tee_fs_htree_meta *meta = NULL; 710 711 /* 712 * The node can be dirty while the block isn't updated due to 713 * updated children, but if block is updated the node has to be 714 * dirty. 715 */ 716 assert(node->dirty >= node->block_updated); 717 718 if (!node->dirty) 719 return TEE_SUCCESS; 720 721 if (node->parent) { 722 uint32_t f = HTREE_NODE_COMMITTED_CHILD(node->id & 1); 723 724 node->parent->dirty = true; 725 node->parent->node.flags ^= f; 726 vers = !!(node->parent->node.flags & f); 727 } else { 728 /* 729 * Counter isn't updated yet, it's increased just before 730 * writing the header. 731 */ 732 vers = !(targ->ht->head.counter & 1); 733 meta = &targ->ht->imeta.meta; 734 } 735 736 res = calc_node_hash(node, meta, targ->arg, node->node.hash); 737 if (res != TEE_SUCCESS) 738 return res; 739 740 node->dirty = false; 741 node->block_updated = false; 742 743 return rpc_write_node(targ->ht, node->id, vers, &node->node); 744 } 745 746 static TEE_Result update_root(struct tee_fs_htree *ht) 747 { 748 TEE_Result res; 749 void *ctx; 750 751 ht->head.counter++; 752 753 res = authenc_init(&ctx, TEE_MODE_ENCRYPT, ht, NULL, sizeof(ht->imeta)); 754 if (res != TEE_SUCCESS) 755 return res; 756 757 return authenc_encrypt_final(ctx, ht->head.tag, &ht->imeta, 758 sizeof(ht->imeta), &ht->head.imeta); 759 } 760 761 TEE_Result tee_fs_htree_sync_to_storage(struct tee_fs_htree **ht_arg, 762 uint8_t *hash) 763 { 764 TEE_Result res; 765 struct tee_fs_htree *ht = *ht_arg; 766 void *ctx; 767 768 if (!ht) 769 return TEE_ERROR_CORRUPT_OBJECT; 770 771 if (!ht->dirty) 772 return TEE_SUCCESS; 773 774 res = crypto_hash_alloc_ctx(&ctx, TEE_FS_HTREE_HASH_ALG); 775 if (res != TEE_SUCCESS) 776 return res; 777 778 res = htree_traverse_post_order(ht, htree_sync_node_to_storage, ctx); 779 if (res != TEE_SUCCESS) 780 goto out; 781 782 /* All the nodes are written to storage now. Time to update root. */ 783 res = update_root(ht); 784 if (res != TEE_SUCCESS) 785 goto out; 786 787 res = rpc_write_head(ht, ht->head.counter & 1, &ht->head); 788 if (res != TEE_SUCCESS) 789 goto out; 790 791 ht->dirty = false; 792 if (hash) 793 memcpy(hash, ht->root.node.hash, sizeof(ht->root.node.hash)); 794 out: 795 crypto_hash_free_ctx(ctx, TEE_FS_HTREE_HASH_ALG); 796 if (res != TEE_SUCCESS) 797 tee_fs_htree_close(ht_arg); 798 return res; 799 } 800 801 static TEE_Result get_block_node(struct tee_fs_htree *ht, bool create, 802 size_t block_num, struct htree_node **node) 803 { 804 TEE_Result res; 805 struct htree_node *nd; 806 807 res = get_node(ht, create, BLOCK_NUM_TO_NODE_ID(block_num), &nd); 808 if (res == TEE_SUCCESS) 809 *node = nd; 810 811 return res; 812 } 813 814 TEE_Result tee_fs_htree_write_block(struct tee_fs_htree **ht_arg, 815 size_t block_num, const void *block) 816 { 817 struct tee_fs_htree *ht = *ht_arg; 818 TEE_Result res; 819 struct tee_fs_rpc_operation op; 820 struct htree_node *node = NULL; 821 uint8_t block_vers; 822 void *ctx; 823 void *enc_block; 824 825 if (!ht) 826 return TEE_ERROR_CORRUPT_OBJECT; 827 828 res = get_block_node(ht, true, block_num, &node); 829 if (res != TEE_SUCCESS) 830 goto out; 831 832 if (!node->block_updated) 833 node->node.flags ^= HTREE_NODE_COMMITTED_BLOCK; 834 835 block_vers = !!(node->node.flags & HTREE_NODE_COMMITTED_BLOCK); 836 res = ht->stor->rpc_write_init(ht->stor_aux, &op, 837 TEE_FS_HTREE_TYPE_BLOCK, block_num, 838 block_vers, &enc_block); 839 if (res != TEE_SUCCESS) 840 goto out; 841 842 res = authenc_init(&ctx, TEE_MODE_ENCRYPT, ht, &node->node, 843 ht->stor->block_size); 844 if (res != TEE_SUCCESS) 845 goto out; 846 res = authenc_encrypt_final(ctx, node->node.tag, block, 847 ht->stor->block_size, enc_block); 848 if (res != TEE_SUCCESS) 849 goto out; 850 851 res = ht->stor->rpc_write_final(&op); 852 if (res != TEE_SUCCESS) 853 goto out; 854 855 node->block_updated = true; 856 node->dirty = true; 857 ht->dirty = true; 858 out: 859 if (res != TEE_SUCCESS) 860 tee_fs_htree_close(ht_arg); 861 return res; 862 } 863 864 TEE_Result tee_fs_htree_read_block(struct tee_fs_htree **ht_arg, 865 size_t block_num, void *block) 866 { 867 struct tee_fs_htree *ht = *ht_arg; 868 TEE_Result res; 869 struct tee_fs_rpc_operation op; 870 struct htree_node *node; 871 uint8_t block_vers; 872 size_t len; 873 void *ctx; 874 void *enc_block; 875 876 if (!ht) 877 return TEE_ERROR_CORRUPT_OBJECT; 878 879 res = get_block_node(ht, false, block_num, &node); 880 if (res != TEE_SUCCESS) 881 goto out; 882 883 block_vers = !!(node->node.flags & HTREE_NODE_COMMITTED_BLOCK); 884 res = ht->stor->rpc_read_init(ht->stor_aux, &op, 885 TEE_FS_HTREE_TYPE_BLOCK, block_num, 886 block_vers, &enc_block); 887 if (res != TEE_SUCCESS) 888 goto out; 889 890 res = ht->stor->rpc_read_final(&op, &len); 891 if (res != TEE_SUCCESS) 892 goto out; 893 if (len != ht->stor->block_size) { 894 res = TEE_ERROR_CORRUPT_OBJECT; 895 goto out; 896 } 897 898 res = authenc_init(&ctx, TEE_MODE_DECRYPT, ht, &node->node, 899 ht->stor->block_size); 900 if (res != TEE_SUCCESS) 901 goto out; 902 903 res = authenc_decrypt_final(ctx, node->node.tag, enc_block, 904 ht->stor->block_size, block); 905 out: 906 if (res != TEE_SUCCESS) 907 tee_fs_htree_close(ht_arg); 908 return res; 909 } 910 911 TEE_Result tee_fs_htree_truncate(struct tee_fs_htree **ht_arg, size_t block_num) 912 { 913 struct tee_fs_htree *ht = *ht_arg; 914 size_t node_id = BLOCK_NUM_TO_NODE_ID(block_num); 915 struct htree_node *node; 916 917 if (!ht) 918 return TEE_ERROR_CORRUPT_OBJECT; 919 920 while (node_id < ht->imeta.max_node_id) { 921 node = find_closest_node(ht, ht->imeta.max_node_id); 922 assert(node && node->id == ht->imeta.max_node_id); 923 assert(!node->child[0] && !node->child[1]); 924 assert(node->parent); 925 assert(node->parent->child[node->id & 1] == node); 926 node->parent->child[node->id & 1] = NULL; 927 free(node); 928 ht->imeta.max_node_id--; 929 ht->dirty = true; 930 } 931 932 return TEE_SUCCESS; 933 } 934