Skip to content

Commit 39049b6

Browse files
committed
fsverity: explicitly check for buffer overflow in build_merkle_tree()
The new Merkle tree construction algorithm is a bit fragile in that it may overflow the 'root_hash' array if the tree actually generated does not match the calculated tree parameters. This should never happen unless there is a filesystem bug that allows the file size to change despite deny_write_access(), or a bug in the Merkle tree logic itself. Regardless, it's fairly easy to check for buffer overflow here, so let's do so. This is a robustness improvement only; this case is not currently known to be reachable. I've added a Fixes tag anyway, since I recommend that this be included in kernels that have the mentioned commit. Fixes: 56124d6 ("fsverity: support enabling with tree block size < PAGE_SIZE") Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 8eb8af4 commit 39049b6

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

fs/verity/enable.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
struct block_buffer {
1515
u32 filled;
16+
bool is_root_hash;
1617
u8 *data;
1718
};
1819

@@ -24,6 +25,14 @@ static int hash_one_block(struct inode *inode,
2425
struct block_buffer *next = cur + 1;
2526
int err;
2627

28+
/*
29+
* Safety check to prevent a buffer overflow in case of a filesystem bug
30+
* that allows the file size to change despite deny_write_access(), or a
31+
* bug in the Merkle tree logic itself
32+
*/
33+
if (WARN_ON_ONCE(next->is_root_hash && next->filled != 0))
34+
return -EINVAL;
35+
2736
/* Zero-pad the block if it's shorter than the block size. */
2837
memset(&cur->data[cur->filled], 0, params->block_size - cur->filled);
2938

@@ -97,6 +106,7 @@ static int build_merkle_tree(struct file *filp,
97106
}
98107
}
99108
buffers[num_levels].data = root_hash;
109+
buffers[num_levels].is_root_hash = true;
100110

101111
BUILD_BUG_ON(sizeof(level_offset) != sizeof(params->level_start));
102112
memcpy(level_offset, params->level_start, sizeof(level_offset));

0 commit comments

Comments
 (0)