hpfs: add array bounds validation in hpfs_bplus_lookup
PATCH 1/1 hpfs: add array bounds validation in hpfs_bplus_lookup
When traversing a corrupted HPFS filesystem, the n_used_nodes field in the btree header can contain a value larger than what fits in the allocated buffer. This causes out-of-bounds array access when iterating through btree->u.internal[] or btree->u.external[] arrays, leading to a KASAN-detected use-after-free.
Validate that n_used_nodes does not exceed the maximum number of nodes that can fit in the buffer before accessing the arrays.
--- fs/hpfs/anode.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
fs/hpfs/anode.c +25 -0
@@ -21,6 +22,31 @@ secno hpfs_bplus_lookup(struct super_block *s, struct inode *inode,int c1, c2 =0 ; go_down:if (hpfs_sb (s)->sb_chk)if (hpfs_stop_cycles (s, a, &c1, &c2,"hpfs_bplus_lookup" ))return -1 ; + +if (bp_internal (btree)) { +unsigned int max_internal_nodes; + + max_internal_nodes = (bh->b_size - ((char *)btree->u.internal - (char *)bh->b_data)) + /sizeof (btree->u.internal[0 ]); +if (btree->n_used_nodes > max_internal_nodes) { +hpfs_error (s,"btree->n_used_nodes = %u, but max for internal node is %u" , + btree->n_used_nodes, max_internal_nodes); +brelse (bh); +return -1 ; + } + }else { +unsigned int max_external_nodes; + + max_external_nodes = (bh->b_size - ((char *)btree->u.external - (char *)bh->b_data)) + /sizeof (btree->u.external[0 ]); +if (btree->n_used_nodes > max_external_nodes) { +hpfs_error (s,"btree->n_used_nodes = %u, but max for external node is %u" , + btree->n_used_nodes, max_external_nodes); +brelse (bh); +return -1 ; + } + } +if (bp_internal (btree)) {for (i =0 ; i < btree->n_used_nodes; i++)if (le32_to_cpu (btree->u.internal[i].file_secno) > sec) {
Signature · 1 line
2.43.0
Quoted 55 lines · Deepanshu Kartikey
When traversing a corrupted HPFS filesystem, the
n_used_nodesfield in the btree header can contain a value larger than what fits in the allocated buffer. This causes out-of-bounds array access when iterating through btree->u.internal[] or btree->u.external[] arrays, leading to a KASAN-detected use-after-free.Validate that
n_used_nodesdoes not exceed the maximum number of nodes that can fit in the buffer before accessing the arrays.Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/hpfs/anode.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)fs/hpfs/anode.c +25
-1@@ -21,6 +22,31 @@ secno hpfs_bplus_lookup(struct super_block *s, struct inode *inode,int c1, c2 =0 ; go_down:if (hpfs_sb (s)->sb_chk)if (hpfs_stop_cycles (s, a, &c1, &c2,"hpfs_bplus_lookup" ))return -1 ; + +if (bp_internal (btree)) { +unsigned int max_internal_nodes; + + max_internal_nodes = (bh->b_size - ((char *)btree->u.internal - (char *)bh->b_data)) + /sizeof (btree->u.internal[0 ]); +if (btree->n_used_nodes > max_internal_nodes) { +hpfs_error (s,"btree->n_used_nodes = %u, but max for internal node is %u" , + btree->n_used_nodes, max_internal_nodes); +brelse (bh); +return -1 ; + } + }else { +unsigned int max_external_nodes; + + max_external_nodes = (bh->b_size - ((char *)btree->u.external - (char *)bh->b_data)) + /sizeof (btree->u.external[0 ]); +if (btree->n_used_nodes > max_external_nodes) { +hpfs_error (s,"btree->n_used_nodes = %u, but max for external node is %u" , + btree->n_used_nodes, max_external_nodes); +brelse (bh); +return -1 ; + } + } +if (bp_internal (btree)) {for (i =0 ; i < btree->n_used_nodes; i++)if (le32_to_cpu (btree->u.internal[i].file_secno) > sec) { --
Quoted 2 lines
2.43.0
Hi Mikulas,
Gentle ping on this patch . I have submitted this patch on 17th jan 2026
Please let me know if anything else required
Thanks
Deepanshu
Quoted 62 lines · Deepanshu Kartikey
Quoted 55 lines · Deepanshu Kartikey
When traversing a corrupted HPFS filesystem, the
n_used_nodesfield in the btree header can contain a value larger than what fits in the allocated buffer. This causes out-of-bounds array access when iterating through btree->u.internal[] or btree->u.external[] arrays, leading to a KASAN-detected use-after-free.Validate that
n_used_nodesdoes not exceed the maximum number of nodes that can fit in the buffer before accessing the arrays.Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/hpfs/anode.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)fs/hpfs/anode.c +25
-1@@ -21,6 +22,31 @@ secno hpfs_bplus_lookup(struct super_block *s, struct inode *inode,int c1, c2 =0 ; go_down:if (hpfs_sb (s)->sb_chk)if (hpfs_stop_cycles (s, a, &c1, &c2,"hpfs_bplus_lookup" ))return -1 ; + +if (bp_internal (btree)) { +unsigned int max_internal_nodes; + + max_internal_nodes = (bh->b_size - ((char *)btree->u.internal - (char *)bh->b_data)) + /sizeof (btree->u.internal[0 ]); +if (btree->n_used_nodes > max_internal_nodes) { +hpfs_error (s,"btree->n_used_nodes = %u, but max for internal node is %u" , + btree->n_used_nodes, max_internal_nodes); +brelse (bh); +return -1 ; + } + }else { +unsigned int max_external_nodes; + + max_external_nodes = (bh->b_size - ((char *)btree->u.external - (char *)bh->b_data)) + /sizeof (btree->u.external[0 ]); +if (btree->n_used_nodes > max_external_nodes) { +hpfs_error (s,"btree->n_used_nodes = %u, but max for external node is %u" , + btree->n_used_nodes, max_external_nodes); +brelse (bh); +return -1 ; + } + } +if (bp_internal (btree)) {for (i =0 ; i < btree->n_used_nodes; i++)if (le32_to_cpu (btree->u.internal[i].file_secno) > sec) { --Quoted 2 lines
2.43.0
Hi Mikulas,
Quoted 6 lines
Gentle ping on this patch . I have submitted this patch on 17th jan 2026
Please let me know if anything else required
Thanks
Deepanshu
Hi
This test is already done in hpfs_map_anode and hpfs_map_fnode. But you disabled the checking with "check=none" mount option, so the checking is not done - this is expected behavior, it's not a bug in the HPFS driver.
Please, fix the fuzzer to not use the "check=none" option.
Mikulas