ocfs2: validate directory-index entry counts when reading metadata
Subsystems (2)
PATCH 1/1 ocfs2: validate directory-index entry counts when reading metadata
ocfs2_validate_dx_leaf() and ocfs2_validate_dx_root() check the ECC and signature of an indexed-directory block before it reaches higher-level callers, but neither validator bounds the ocfs2_dx_entry_list counts against the capacity of the block that holds them.
ocfs2_dx_dir_search() then walks
for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) dx_entry = &entry_list->de_entries[i];
over de_num_used entries with no bounds check. entry_list is either dx_leaf->dl_list (from ocfs2_read_dx_leaf) or, for an inline root, dx_root->dr_entries. A crafted on-disk image can set de_num_used (and de_count, which is the __counted_by_le() bound of de_entries) to 0xffff and make the walk read far past the end of the 4KB metadata block, giving a slab out-of-bounds read reachable from any path lookup, stat() or open() on an indexed directory once the image is mounted.
Commit 775c17386a6f ("ocfs2: validate dx_root extent list fields during block read") already bounds dr_list for the non-inline dx_root, but left the inline dr_entries path and the dx_leaf dl_list unchecked. Add the same read-time validation for both entry lists: de_count must equal the capacity of the block (ocfs2_dx_entries_per_leaf()/per_root()) and de_num_used must not exceed de_count, rejecting corrupted metadata with -EFSCORRUPTED before ocfs2_dx_dir_search() can walk an out-of-range entry array.
de_count is always written as exactly the block capacity when a leaf or inline root is formatted, so the equality check does not reject any valid image.
Found by 0sec automated security-research tooling (https://0sec.ai).
--- fs/ocfs2/dir.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-)
fs/ocfs2/dir.c +41 -4
@@ -624,6 +624,28 @@ static int ocfs2_validate_dx_root(struct super_block *sb, le16_to_cpu(el->l_count)); goto bail; } + }else { +struct ocfs2_dx_entry_list *dl_list = &dx_root->dr_entries; + +if (le16_to_cpu (dl_list->de_count) != +ocfs2_dx_entries_per_root (sb)) { + ret =ocfs2_error (sb, +"Dir Index Root # %llu has invalid de_count %u (expected %u)\n" , + (unsigned long long )le64_to_cpu (dx_root->dr_blkno), +le16_to_cpu (dl_list->de_count), +ocfs2_dx_entries_per_root (sb)); + goto bail; + } + +if (le16_to_cpu (dl_list->de_num_used) > +le16_to_cpu (dl_list->de_count)) { + ret =ocfs2_error (sb, +"Dir Index Root # %llu has invalid de_num_used %u (de_count %u)\n" , + (unsigned long long )le64_to_cpu (dx_root->dr_blkno), +le16_to_cpu (dl_list->de_num_used), +le16_to_cpu (dl_list->de_count)); + goto bail; + } } bail:
@@ -663,10 +685,25 @@ static int ocfs2_validate_dx_leaf(struct super_block *sb,return ret; } -if (!OCFS2_IS_VALID_DX_LEAF (dx_leaf)) { - ret =ocfs2_error (sb,"Dir Index Leaf has bad signature %.*s\n" , -7 , dx_leaf->dl_signature); - } +if (!OCFS2_IS_VALID_DX_LEAF (dx_leaf)) +return ocfs2_error (sb,"Dir Index Leaf has bad signature %.*s\n" , +7 , dx_leaf->dl_signature); + +if (le16_to_cpu (dx_leaf->dl_list.de_count) != +ocfs2_dx_entries_per_leaf (sb)) +return ocfs2_error (sb, +"Dir Index Leaf # %llu has invalid de_count %u (expected %u)\n" , + (unsigned long long )le64_to_cpu (dx_leaf->dl_blkno), +le16_to_cpu (dx_leaf->dl_list.de_count), +ocfs2_dx_entries_per_leaf (sb)); + +if (le16_to_cpu (dx_leaf->dl_list.de_num_used) > +le16_to_cpu (dx_leaf->dl_list.de_count)) +return ocfs2_error (sb, +"Dir Index Leaf # %llu has invalid de_num_used %u (de_count %u)\n" , + (unsigned long long )le64_to_cpu (dx_leaf->dl_blkno), +le16_to_cpu (dx_leaf->dl_list.de_num_used), +le16_to_cpu (dx_leaf->dl_list.de_count));return ret; }
Signature · 1 line
2.43.0
Quoted 31 lines · Doruk Tan Ozturk
ocfs2_validate_dx_leaf()andocfs2_validate_dx_root()check the ECC and signature of an indexed-directory block before it reaches higher-level callers, but neither validator bounds theocfs2_dx_entry_listcounts against the capacity of the block that holds them.
ocfs2_dx_dir_search()then walksfor (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) dx_entry = &entry_list->de_entries[i];over
de_num_usedentries with no bounds check.entry_listis eitherdx_leaf->dl_list(fromocfs2_read_dx_leaf) or, for an inline root,dx_root->dr_entries. A crafted on-disk image can setde_num_used(andde_count, which is the__counted_by_le()bound ofde_entries) to0xffffand make the walk read far past the end of the 4KB metadata block, giving a slab out-of-bounds read reachable from any path lookup,stat()oropen()on an indexed directory once the image is mounted.Commit
775c17386a6f("ocfs2: validatedx_rootextent list fields during block read") already boundsdr_listfor the non-inlinedx_root, but left the inlinedr_entriespath and thedx_leafdl_listunchecked. Add the same read-time validation for both entry lists:de_countmust equal the capacity of the block (ocfs2_dx_entries_per_leaf()/per_root()) andde_num_usedmust not exceedde_count, rejecting corrupted metadata with-EFSCORRUPTEDbeforeocfs2_dx_dir_search()can walk an out-of-range entry array.
de_countis always written as exactly the block capacity when a leaf or inline root is formatted, so the equality check does not reject any valid image.
Quoted 6 lines
Fixes 9b7895efac90 ("ocfs2: Add a name indexed b-tree to directory inodes")Fixes 4ed8a6bb083b ("ocfs2: Store dir index records inline")Found by 0sec automated security-research tooling (https://0sec.ai).
Assisted-by 0sec:claude-opus-4-8Signed-off-by Doruk Tan Ozturk <[email protected]>
Looks fine.
Quoted 67 lines
--- fs/ocfs2/dir.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-)fs/ocfs2/dir.c +41
-4@@ -624,6 +624,28 @@ static int ocfs2_validate_dx_root(struct super_block *sb, le16_to_cpu(el->l_count)); goto bail; } + }else { +struct ocfs2_dx_entry_list *dl_list = &dx_root->dr_entries; + +if (le16_to_cpu (dl_list->de_count) != +ocfs2_dx_entries_per_root (sb)) { + ret =ocfs2_error (sb, +"Dir Index Root # %llu has invalid de_count %u (expected %u)\n" , + (unsigned long long )le64_to_cpu (dx_root->dr_blkno), +le16_to_cpu (dl_list->de_count), +ocfs2_dx_entries_per_root (sb)); + goto bail; + } + +if (le16_to_cpu (dl_list->de_num_used) > +le16_to_cpu (dl_list->de_count)) { + ret =ocfs2_error (sb, +"Dir Index Root # %llu has invalid de_num_used %u (de_count %u)\n" , + (unsigned long long )le64_to_cpu (dx_root->dr_blkno), +le16_to_cpu (dl_list->de_num_used), +le16_to_cpu (dl_list->de_count)); + goto bail; + } } bail:@@ -663,10 +685,25 @@ static int ocfs2_validate_dx_leaf(struct super_block *sb,return ret; } -if (!OCFS2_IS_VALID_DX_LEAF (dx_leaf)) { - ret =ocfs2_error (sb,"Dir Index Leaf has bad signature %.*s\n" , -7 , dx_leaf->dl_signature); - } +if (!OCFS2_IS_VALID_DX_LEAF (dx_leaf)) +return ocfs2_error (sb,"Dir Index Leaf has bad signature %.*s\n" , +7 , dx_leaf->dl_signature); + +if (le16_to_cpu (dx_leaf->dl_list.de_count) != +ocfs2_dx_entries_per_leaf (sb)) +return ocfs2_error (sb, +"Dir Index Leaf # %llu has invalid de_count %u (expected %u)\n" , + (unsigned long long )le64_to_cpu (dx_leaf->dl_blkno), +le16_to_cpu (dx_leaf->dl_list.de_count), +ocfs2_dx_entries_per_leaf (sb)); + +if (le16_to_cpu (dx_leaf->dl_list.de_num_used) > +le16_to_cpu (dx_leaf->dl_list.de_count)) +return ocfs2_error (sb, +"Dir Index Leaf # %llu has invalid de_num_used %u (de_count %u)\n" , + (unsigned long long )le64_to_cpu (dx_leaf->dl_blkno), +le16_to_cpu (dx_leaf->dl_list.de_num_used), +le16_to_cpu (dx_leaf->dl_list.de_count));return ret; }