mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Samuel Moelius <sam.moelius@trailofbits.com>
To: Phillip Lougher <phillip@squashfs.org.uk>
Cc: Samuel Moelius <sam.moelius@trailofbits.com>,
	linux-kernel@vger.kernel.org (open list)
Subject: [PATCH] squashfs: reject wrapped fragment index offsets
Date: Wed,  3 Jun 2026 17:26:19 +0000	[thread overview]
Message-ID: <20260603172620.23735-1-sam.moelius@trailofbits.com> (raw)

Squashfs fragment lookup computes byte offsets into the fragment index
table from on-disk values.  Crafted metadata can make that arithmetic
wrap before the block is read.

A corrupted image can place the fragment index offset near U64_MAX
and use a non-zero length so that the end offset wraps to a low value.

After the wrap, the filesystem may read the wrong metadata block
instead of rejecting the corrupt image.

Check fragment index offset arithmetic for overflow before issuing
the read.

Assisted-by: Codex:gpt-5.5-cyber-preview
Signed-off-by: Samuel Moelius <sam.moelius@trailofbits.com>
---
 fs/squashfs/block.c    | 6 ++++--
 fs/squashfs/fragment.c | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/fs/squashfs/block.c b/fs/squashfs/block.c
index 2d5850168026..570ae6ac3652 100644
--- a/fs/squashfs/block.c
+++ b/fs/squashfs/block.c
@@ -312,7 +312,8 @@ int squashfs_read_data(struct super_block *sb, u64 index, int length,
 		struct bvec_iter_all iter_all = {};
 		struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
 
-		if (index + 2 > msblk->bytes_used) {
+		if (index > msblk->bytes_used ||
+		    msblk->bytes_used - index < 2) {
 			res = -EIO;
 			goto out;
 		}
@@ -349,7 +350,8 @@ int squashfs_read_data(struct super_block *sb, u64 index, int length,
 		      compressed ? "" : "un", length);
 	}
 	if (length <= 0 || length > output->length ||
-			(index + length) > msblk->bytes_used) {
+	    index > msblk->bytes_used ||
+	    (u64)length > msblk->bytes_used - index) {
 		res = -EIO;
 		goto out;
 	}
diff --git a/fs/squashfs/fragment.c b/fs/squashfs/fragment.c
index 49602b9a42e1..b4a4ea83b602 100644
--- a/fs/squashfs/fragment.c
+++ b/fs/squashfs/fragment.c
@@ -71,7 +71,8 @@ __le64 *squashfs_read_fragment_index_table(struct super_block *sb,
 	 * this check also traps instances where fragment_table_start is
 	 * incorrectly larger than the next table start
 	 */
-	if (fragment_table_start + length > next_table)
+	if (fragment_table_start > next_table ||
+	    length > next_table - fragment_table_start)
 		return ERR_PTR(-EINVAL);
 
 	table = squashfs_read_table(sb, fragment_table_start, length);
-- 
2.43.0


                 reply	other threads:[~2026-06-03 17:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260603172620.23735-1-sam.moelius@trailofbits.com \
    --to=sam.moelius@trailofbits.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=phillip@squashfs.org.uk \
    /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

Powered by JetHome