[PATCH] jfs: fix array-index-out-of-bounds in diAllocBit()

Haotian Zhang posted 1 patch 1 month, 1 week ago
fs/jfs/jfs_imap.c | 8 ++++++++
1 file changed, 8 insertions(+)
[PATCH] jfs: fix array-index-out-of-bounds in diAllocBit()
Posted by Haotian Zhang 1 month, 1 week ago
syzbot reported an array-index-out-of-bounds access in diAllocBit(),
where im_agctl[] is indexed by agno derived from on-disk agstart
without validation.

agno is computed via BLKTOAG() and may become negative or exceed
MAXAG when the filesystem metadata is corrupted, leading to an
out-of-bounds access to imap->im_agctl[].

Validate agno before using it and abort the operation if the value
is invalid.

Reported-by: syzbot+015483fc71d1413f40ee@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=015483fc71d1413f40ee
Signed-off-by: Haotian Zhang <vulab@iscas.ac.cn>
---
 fs/jfs/jfs_imap.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/jfs/jfs_imap.c b/fs/jfs/jfs_imap.c
index ecb8e05b8b84..cc72fcaa94e2 100644
--- a/fs/jfs/jfs_imap.c
+++ b/fs/jfs/jfs_imap.c
@@ -2044,6 +2044,14 @@ static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
 	 * the extent.
 	 */
 	agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
+	if (agno >= MAXAG || agno < 0) {
+		if (amp)
+			release_metapage(amp);
+		if (bmp)
+			release_metapage(bmp);
+		return -EIO;
+	}
+
 	extno = ino >> L2INOSPEREXT;
 	bitno = ino & (INOSPEREXT - 1);
 
-- 
2.43.0