UBSAN: array-index-out-of-bounds in jfs_readdir

From: Ghanshyam Agrawal
Date: Tue Oct 01 2024 - 00:57:20 EST


#syz test
From 05e4d925c9dec8efed1ea29b2617d2a05e18e6fd Mon Sep 17 00:00:00 2001
From: Ghanshyam Agrawal <ghanshyam1898@xxxxxxxxx>
Date: Tue, 1 Oct 2024 10:21:11 +0530
Subject: [PATCH] jfs: fix array-index-out-of-bounds in jfs_readdir
To: shaggy@xxxxxxxxxx,
eadavis@xxxxxx,
osmtendev@xxxxxxxxx,
ghandatmanas@xxxxxxxxx
Cc: jfs-discussion@xxxxxxxxxxxxxxxxxxxxx,
linux-kernel@xxxxxxxxxxxxxxx

The stbl might contain some invalid values. Added a check to
return error code in that case.

Reported-by: syzbot+0315f8fe99120601ba88@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=0315f8fe99120601ba88
Signed-off-by: Ghanshyam Agrawal <ghanshyam1898@xxxxxxxxx>
---
fs/jfs/jfs_dtree.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/fs/jfs/jfs_dtree.c b/fs/jfs/jfs_dtree.c
index 5d3127ca68a4..c8f6e51ac047 100644
--- a/fs/jfs/jfs_dtree.c
+++ b/fs/jfs/jfs_dtree.c
@@ -2891,6 +2891,14 @@ int jfs_readdir(struct file *file, struct dir_context *ctx)
stbl = DT_GETSTBL(p);

for (i = index; i < p->header.nextindex; i++) {
+ if (stbl[i] < 0 || stbl[i] > 127) {
+ DT_PUTPAGE(mp);
+ free_page(dirent_buf);
+ jfs_err("JFS: Invalid stbl[%d] = %d for inode %ld, block = %lld",
+ i, stbl[i], (long)ip->i_ino, (long long)bn);
+ return -EIO;
+ }
+
d = (struct ldtentry *) & p->slot[stbl[i]];

if (((long) jfs_dirent + d->namlen + 1) >
--
2.34.1