[PATCH] jfs: validate l2nbperpage in diMount to prevent shift-out-of-bounds

Tristan Madani posted 1 patch 1 month, 4 weeks ago
fs/jfs/jfs_imap.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
[PATCH] jfs: validate l2nbperpage in diMount to prevent shift-out-of-bounds
Posted by Tristan Madani 1 month, 4 weeks ago
From: Tristan Madani <tristan@talencesecurity.com>

diMount() reads l2nbperpage from the disk inode aggregate without
validation. A corrupted filesystem image can set this field to a value
exceeding the bit width of the shift operand in jfs_statfs(), causing
UBSAN shift-out-of-bounds.

Add validation of l2nbperpage against reasonable bounds before using it
in arithmetic operations.

Reported-by: syzbot+13ba7f3e9a17f77250fe@syzkaller.appspotmail.com
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
 fs/jfs/jfs_imap.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/fs/jfs/jfs_imap.c b/fs/jfs/jfs_imap.c
index b84ba4d7dfb44..eafbd2b55df75 100644
--- a/fs/jfs/jfs_imap.c
+++ b/fs/jfs/jfs_imap.c
@@ -124,6 +124,18 @@ int diMount(struct inode *ipimap)
 	atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
 	imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
 	imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
+
+	if (imap->im_l2nbperiext < 0 ||
+	    imap->im_l2nbperiext > 30 ||
+	    imap->im_nbperiext != (1 << imap->im_l2nbperiext)) {
+		jfs_err("diMount: invalid imap parameters: "
+			"nbperiext(%d) l2nbperiext(%d)",
+			imap->im_nbperiext, imap->im_l2nbperiext);
+		release_metapage(mp);
+		kfree(imap);
+		return -EINVAL;
+	}
+
 	for (index = 0; index < MAXAG; index++) {
 		imap->im_agctl[index].inofree =
 		    le32_to_cpu(dinom_le->in_agctl[index].inofree);
-- 
2.47.3
Re: [PATCH] jfs: validate l2nbperpage in diMount to prevent shift-out-of-bounds
Posted by Yun Zhou 2 weeks ago
On Fri, Apr 18, 2026, Tristan Madani <tristmd@gmail.com> wrote:
> Add validation of l2nbperpage against reasonable bounds before using it
> in arithmetic operations.

The fix looks correct to me. Minor nit: the subject and commit message
say "l2nbperpage" but the actual field being validated is
"im_l2nbperiext" (and im_nbperiext). Could you respin with the
corrected naming?

Reviewed-by: Yun Zhou <yun.zhou@windriver.com>