Skip to contentSkip to conversation

jfs: fix array-index-out-of-bounds in jfs_readdir

PATCH In review Ghanshyam Agrawal 2 participants 2 messages Oct 1, 2024 – Oct 29, 2024 JFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 jfs: fix array-index-out-of-bounds in jfs_readdir

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

---
 fs/jfs/jfs_dtree.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/jfs/jfs_dtree.c +8 -0
index 5d3127ca68a4..c8f6e51ac047 100644
@@ -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) >
Signature · 1 line
2.34.1
#1 lore raw
Quoted 15 lines · Ghanshyam Agrawal

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

---
  fs/jfs/jfs_dtree.c | 8 ++++++++
  1 file changed, 8 insertions(+)
fs/jfs/jfs_dtree.c +0 -0
index 5d3127ca68a4..c8f6e51ac047 100644

fs/jfs/jfs_dtree.c int jfs_readdir(struct file *file, struct dir_context *ctx) · line 2898

Quoted 8 lines
fs/jfs/jfs_dtree.c +5 -0
@@ -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);

I'm moving the printing of the error message before the call to DT_PUTPAGE() since stbl is part of the page's data. And I'm fixing the indent.

fs/jfs/jfs_dtree.c int jfs_readdir(struct file *file, struct dir_context *ctx) · line 2904

Quoted 6 lines
fs/jfs/jfs_dtree.c +3 -0
+				return -EIO;
+			}
+
  			d = (struct ldtentry *) & p->slot[stbl[i]];
 
  			if (((long) jfs_dirent + d->namlen + 1) >

Otherwise, looks good. I'll be applying this one.

Shaggy