Skip to content

Commit

Permalink
core: fs_htree: include meta in root hash
Browse files Browse the repository at this point in the history
Includes the meta data when calculating the hash of the root node to
detect changes in file length while number of blocks is unchanged.

Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Fixes: #2094
Reported-by: Kevin Peng <kevinp@marvell.com>
Tested-by: Kevin Peng <kevinp@marvell.com>
[jf: add Fixes:, Reported-by: and Tested-by: tags]
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
  • Loading branch information
jenswi-linaro authored and jforissier committed Feb 2, 2018
1 parent bf071c7 commit 94a7299
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions core/tee/fs_htree.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ static TEE_Result init_tree_from_data(struct tee_fs_htree *ht)
return TEE_SUCCESS;
}

static TEE_Result calc_node_hash(struct htree_node *node, void *ctx,
static TEE_Result calc_node_hash(struct htree_node *node,
struct tee_fs_htree_meta *meta, void *ctx,
uint8_t *digest)
{
TEE_Result res;
Expand All @@ -413,6 +414,12 @@ static TEE_Result calc_node_hash(struct htree_node *node, void *ctx,
if (res != TEE_SUCCESS)
return res;

if (meta) {
res = crypto_hash_update(ctx, alg, (void *)meta, sizeof(meta));
if (res != TEE_SUCCESS)
return res;
}

if (node->child[0]) {
res = crypto_hash_update(ctx, alg, node->child[0]->node.hash,
sizeof(node->child[0]->node.hash));
Expand Down Expand Up @@ -563,7 +570,10 @@ static TEE_Result verify_node(struct traverse_arg *targ,
TEE_Result res;
uint8_t digest[TEE_FS_HTREE_HASH_SIZE];

res = calc_node_hash(node, ctx, digest);
if (node->parent)
res = calc_node_hash(node, NULL, ctx, digest);
else
res = calc_node_hash(node, &targ->ht->imeta.meta, ctx, digest);
if (res == TEE_SUCCESS &&
buf_compare_ct(digest, node->node.hash, sizeof(digest)))
return TEE_ERROR_CORRUPT_OBJECT;
Expand Down Expand Up @@ -598,7 +608,8 @@ static TEE_Result init_root_node(struct tee_fs_htree *ht)
ht->root.id = 1;
ht->root.dirty = true;

res = calc_node_hash(&ht->root, ctx, ht->root.node.hash);
res = calc_node_hash(&ht->root, &ht->imeta.meta, ctx,
ht->root.node.hash);
crypto_hash_free_ctx(ctx, TEE_FS_HTREE_HASH_ALG);

return res;
Expand Down Expand Up @@ -670,6 +681,7 @@ struct tee_fs_htree_meta *tee_fs_htree_get_meta(struct tee_fs_htree *ht)
void tee_fs_htree_meta_set_dirty(struct tee_fs_htree *ht)
{
ht->dirty = true;
ht->root.dirty = true;
}

static TEE_Result free_node(struct traverse_arg *targ __unused,
Expand All @@ -694,6 +706,7 @@ static TEE_Result htree_sync_node_to_storage(struct traverse_arg *targ,
{
TEE_Result res;
uint8_t vers;
struct tee_fs_htree_meta *meta = NULL;

/*
* The node can be dirty while the block isn't updated due to
Expand All @@ -717,9 +730,10 @@ static TEE_Result htree_sync_node_to_storage(struct traverse_arg *targ,
* writing the header.
*/
vers = !(targ->ht->head.counter & 1);
meta = &targ->ht->imeta.meta;
}

res = calc_node_hash(node, targ->arg, node->node.hash);
res = calc_node_hash(node, meta, targ->arg, node->node.hash);
if (res != TEE_SUCCESS)
return res;

Expand Down

0 comments on commit 94a7299

Please sign in to comment.