mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] romfs: reject non-advancing nextfh offsets in romfs_lookup() and romfs_readdir()
@ 2026-09-19 22:26 Hui Peng
  0 siblings, 0 replies; only message in thread
From: Hui Peng @ 2026-09-19 22:26 UTC (permalink / raw)
  To: brauner, jack; +Cc: linux-fsdevel, linux-kernel

In romfs_lookup() and romfs_readdir() (fs/romfs/super.c), verify that
the masked nextfh offset strictly advances (or is 0 to terminate the
directory) so a self-referential or backward nextfh link in a corrupted
RomFS image cannot cause an infinite loop in the kernel.

Fixes: da4458bda237 ("NOMMU: Make it possible for RomFS to use MTD devices directly")
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@gmail.com>
---
diff --git a/fs/romfs/super.c b/fs/romfs/super.c
index 7f783d6173e8..4d787ee06d4a 100644
--- a/fs/romfs/super.c
+++ b/fs/romfs/super.c
@@ -191,6 +191,8 @@ static int romfs_readdir(struct file *file, struct dir_context *ctx)
 			    romfs_dtype_table[nextfh & ROMFH_TYPE]))
 			goto out;
 
+		if ((nextfh & ROMFH_MASK) && (nextfh & ROMFH_MASK) <= offset)
+			goto out;
 		offset = nextfh & ROMFH_MASK;
 	}
 out:
@@ -223,6 +225,8 @@ static struct dentry *romfs_lookup(struct inode *dir, struct dentry *dentry,
 	len = dentry->d_name.len;
 
 	for (;;) {
+		unsigned long next_offset;
+
 		if (!offset || offset >= maxoff)
 			break;
 
@@ -246,7 +250,12 @@ static struct dentry *romfs_lookup(struct inode *dir, struct dentry *dentry,
 		}
 
 		/* next entry */
-		offset = be32_to_cpu(ri.next) & ROMFH_MASK;
+		next_offset = be32_to_cpu(ri.next) & ROMFH_MASK;
+		if (next_offset && next_offset <= offset) {
+			ret = -EIO;
+			goto error;
+		}
+		offset = next_offset;
 	}
 
 	return d_splice_alias(inode, dentry);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-09-19 22:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-19 22:26 [PATCH] romfs: reject non-advancing nextfh offsets in romfs_lookup() and romfs_readdir() Hui Peng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®