mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Cc: "Claude Opus 4.6 \(1M context\)" <noreply@anthropic.com>
Subject: [PATCH 3/5] fs: ext4: fix OOB read and infinite loop in ext_iterate()
Date: Thu, 02 Apr 2026 12:12:31 +0200	[thread overview]
Message-ID: <20260402-fs-ext4-buffer-overflows-v1-3-b9f8a909fe58@pengutronix.de> (raw)
In-Reply-To: <20260402-fs-ext4-buffer-overflows-v1-0-b9f8a909fe58@pengutronix.de>

ext_iterate() reads the entire directory into a buffer and walks it
parsing ext2_dirent entries, but has several issues with untrusted
on-disk data:

1. No check that a full ext2_dirent struct fits before reading it,
   causing an OOB read for the last partial entry.

2. No check that fpos + sizeof(dirent) + namelen fits within the
   buffer before passing the filename pointer to dir_emit(), causing
   an OOB read if namelen extends past the allocation.

3. If dirent->direntlen is 0, fpos never advances, causing an
   infinite loop.

Fix by:
- Checking that a full dirent struct fits before each iteration
- Validating that the filename region is within bounds before emitting
- Breaking out of the loop if direntlen is smaller than the minimum
  dirent size (which also catches the zero case)

All three are triggerable from a crafted ext4 filesystem image.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---
 fs/ext4/ext_barebox.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/ext_barebox.c b/fs/ext4/ext_barebox.c
index 5bee4853d4..ef1a71368d 100644
--- a/fs/ext4/ext_barebox.c
+++ b/fs/ext4/ext_barebox.c
@@ -157,15 +157,20 @@ static int ext_iterate(struct file *file, struct dir_context *ctx)
 		goto out;
 	}
 
-	while (fpos < dir->i_size) {
+	while (fpos + sizeof(struct ext2_dirent) <= dir->i_size) {
 		const struct ext2_dirent *dirent = buf + fpos;
 		const char *filename = buf + fpos + sizeof(*dirent);
+		uint16_t direntlen = le16_to_cpu(dirent->direntlen);
 
-		if (dirent->namelen != 0)
+		if (direntlen < sizeof(struct ext2_dirent))
+			break;
+
+		if (dirent->namelen != 0 &&
+		    fpos + sizeof(*dirent) + dirent->namelen <= dir->i_size)
 			dir_emit(ctx, filename, dirent->namelen,
 				 le32_to_cpu(dirent->inode), DT_UNKNOWN);
 
-		fpos += le16_to_cpu(dirent->direntlen);
+		fpos += direntlen;
 	}
 	ret = 0;
 out:

-- 
2.47.3




  parent reply	other threads:[~2026-04-02 10:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-02 10:12 [PATCH 0/5] fs: ext4: protection against corrupted filesystems Sascha Hauer
2026-04-02 10:12 ` [PATCH 1/5] fs: ext4: validate log2_block_size from superblock at mount Sascha Hauer
2026-04-02 10:12 ` [PATCH 2/5] fs: ext4: reject superblocks with zero inodesz, gdsize or inodes_per_group Sascha Hauer
2026-04-02 10:12 ` Sascha Hauer [this message]
2026-04-02 10:45   ` [PATCH 3/5] fs: ext4: fix OOB read and infinite loop in ext_iterate() Sascha Hauer
2026-04-02 10:12 ` [PATCH 4/5] fs: ext4: reject dirents with too-small direntlen to prevent infinite loops Sascha Hauer
2026-04-02 10:12 ` [PATCH 5/5] fs: ext4: validate extent eh_entries against buffer capacity Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260402-fs-ext4-buffer-overflows-v1-3-b9f8a909fe58@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=noreply@anthropic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox