fs: ext4: bound extent-tree depth in ext4fs_get_extent_block()
PATCH 1/1 fs: ext4: bound extent-tree depth in ext4fs_get_extent_block()
ext4fs_get_extent_block() walks the on-disk extent tree in a while(1) loop, following each interior node to the next block and stopping only at a node with eh_depth == 0. The depth is re-read from every block but never required to decrease, so a crafted image whose index entry points back into the tree (for example at the block itself) keeps eh_depth non-zero forever and the loop never terminates. Reading such a file from an untrusted ext4 image hangs U-Boot, a denial of service reachable before any authentication.
A valid extent tree has strictly decreasing depth down to the leaves, so require eh_depth to decrease at every step and reject the image otherwise.
--- fs/ext4/ext4_common.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
fs/ext4/ext4_common.c +14 -1
@@ -1539,13 +1539,26 @@ static struct ext4_extent_header *ext4fs_get_extent_blockint blksz =EXT2_BLOCK_SIZE (data);int i; +u16 prev_depth =0xffff ; +while (1 ) { +u16 depth; + index = (struct ext4_extent_idx *)(ext_block +1 );if (le16_to_cpu (ext_block->eh_magic) !=EXT4_EXT_MAGIC )return NULL ; -if (ext_block->eh_depth ==0 ) +/* +* Depth must strictly decrease towards the leaves, otherwise a +* self-referential index block loops here forever. +*/ + depth =le16_to_cpu (ext_block->eh_depth); +if (depth >= prev_depth) +return NULL ; + prev_depth = depth; + +if (depth ==0 )return ext_block; i =-1 ; do {
Signature · 1 line
2.43.0
Hi Oscar,
Quoted 2 lines · Oscar Deschamps
A valid extent tree has strictly decreasing depth down to the leaves, so require
eh_depthto decrease at every step and reject the image otherwise.
This loop is already handled by patch 1/6 of my series "fs: ext4: check on-disk indices before using them" (v2, 28 October, all six patches Reviewed-by Simon Glass):
https://patchwork.ozlabs.org/project/uboot/list/?series=530226
Its ext4fs_check_extent_header() requires the depth to drop by exactly one at each level and to be at most 5, as Linux __ext4_ext_check() and ext4_find_extent() do, so an index node pointing back at itself, or at any node that is not one level below it, is rejected. It also checks eh_entries against eh_max and against what fits in the node, since the index scan in the same loop otherwise reads past the end of the node. Patch 6/6 has a test for your case, test_fs_ext_oob_extent_loop: an index node that points at itself.
The two patches change the same lines, so whichever is applied second does not apply. With git am -3 they merge without a conflict, but the result has two depth variables in the loop and the header check reads the inner one before it is set, so they should not both go in.
Thanks,
Ana
Quoted 21 lines · Ana Bobillo
Hi Oscar,
Quoted 2 lines · Oscar Deschamps
A valid extent tree has strictly decreasing depth down to the leaves, so require
eh_depthto decrease at every step and reject the image otherwise.This loop is already handled by patch 1/6 of my series "fs: ext4: check on-disk indices before using them" (v2, 28 October, all six patches Reviewed-by Simon Glass):
https://patchwork.ozlabs.org/project/uboot/list/?series=530226
Its
ext4fs_check_extent_header()requires the depth to drop by exactly one at each level and to be at most 5, as Linux__ext4_ext_check()andext4_find_extent()do, so an index node pointing back at itself, or at any node that is not one level below it, is rejected. It also checkseh_entriesagainsteh_maxand against what fits in the node, since the index scan in the same loop otherwise reads past the end of the node. Patch 6/6 has a test for your case,test_fs_ext_oob_extent_loop: an index node that points at itself.
Quoted 4 lines
The two patches change the same lines, so whichever is applied second does not apply. With git am -3 they merge without a conflict, but the result has two depth variables in the loop and the header check reads the inner one before it is set, so they should not both go in.
Thanks for explaining Ana. As normal practice, we'll just take the prior series that addresses this issue instead of this patch.
Signature · 1 line
Tom