mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Phillip Lougher <phillip@squashfs.org.uk>
To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org
Cc: Phillip Lougher <phillip@squashfs.org.uk>
Subject: [PATCH v4 22/23] Squashfs: fix regression in squashfs_copy_cache()
Date: Wed, 25 Nov 2026 01:47:36 +0000	[thread overview]
Message-ID: <20261125014737.347231-23-phillip@squashfs.org.uk> (raw)
In-Reply-To: <20261125014737.347231-1-phillip@squashfs.org.uk>

In a recent commit updating the code to use folios, the bytes variable
was changed from signed to unsigned in squashfs_copy_cache().

The bytes variable is used in a loop where PAGE_SIZE bytes are
subtracted from it each time around the loop, and where the loop
terminates once bytes is less than or equal to zero.

The effect of the change in signedness is bytes no longer becomes
negative when it is not a multiple of PAGE_SIZE, and the loop does not
terminate when it should do, and instead folios are created and filled
after file EOF.

These beyond EOF folios are not visible to user-space and hence this is
a silent regression.

This commit fixes the code to only subtract the minimum of bytes or
PAGE_SIZE, and which ensures the variable does not underflow.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
---
 fs/squashfs/file.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c
index 322caced468f..f969d7b204d5 100644
--- a/fs/squashfs/file.c
+++ b/fs/squashfs/file.c
@@ -410,6 +410,7 @@ void squashfs_copy_cache(struct folio *folio,
 	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
 	s64 i, mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
 	s64 start_index = folio->index & ~mask, end_index = start_index | mask;
+	size_t avail;
 
 	/*
 	 * Loop copying datablock into pages.  As the datablock likely covers
@@ -417,12 +418,13 @@ void squashfs_copy_cache(struct folio *folio,
 	 * grab the pages from the page cache, except for the page that we've
 	 * been called to fill.
 	 */
-	for (i = start_index; i <= end_index && bytes > 0; i++,
-			bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
+	for (i = start_index; i <= end_index && bytes; i++,
+			bytes -= avail, offset += avail) {
 		struct folio *push_folio;
-		size_t avail = buffer ? min(bytes, PAGE_SIZE) : 0;
 		bool updated = false;
 
+		avail = min(bytes, PAGE_SIZE);
+
 		TRACE("bytes %zu, i %lld, available_bytes %zu\n", bytes, i, avail);
 
 		push_folio = (i == folio->index) ? folio :
@@ -436,7 +438,7 @@ void squashfs_copy_cache(struct folio *folio,
 		if (folio_test_uptodate(push_folio))
 			goto skip_folio;
 
-		updated = squashfs_fill_page(push_folio, buffer, offset, avail);
+		updated = squashfs_fill_page(push_folio, buffer, offset, buffer ? avail : 0);
 skip_folio:
 		folio_end_read(push_folio, updated);
 		if (i != folio->index)
-- 
2.47.3


  parent reply	other threads:[~2026-11-25  1:48 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  1:47 [PATCH v4 00/23] Squashfs: fix a lot of AI issues and AI style issues Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 01/23] Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 02/23] Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 03/23] Squashfs: convert SQUASHFS_FRAGMENT_* " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 04/23] Squashfs: convert SQUASHFS_LOOKUP_* " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 05/23] Squashfs: convert SQUASHFS_ID_* " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 06/23] Squashfs: convert SQUASHFS_XATTR_* " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 07/23] Squashfs: fix offset in squashfs_read_inode() TRACE statements Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 08/23] Squashfs: don't compute start or alloc memory on empty directory or index Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 09/23] Squashfs: fix fragment bytes overflow on 32-bit architectures Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 10/23] Squashfs: fix fragment index table length too large exploit Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 11/23] Squashfs: sanity check inode metadata block location Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 12/23] Squashfs: sanity check directory " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 13/23] Squashfs: widen xattr count variables and add sanity check Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 14/23] Squashfs: very large files can exceed an int or size_t Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 15/23] Squashfs: add more sanity checking to squashfs_block_size() Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 16/23] Squashfs: sanity check fragment block location and range Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 17/23] Squashfs: sanity check block list sizes " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 18/23] Squashfs: don't store large export table indexes in memory Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 19/23] Squashfs: don't store large fragment " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 20/23] Squashfs: don't store large xattr id " Phillip Lougher
2026-11-25  1:47 ` [PATCH v4 21/23] Squashfs: fix variables in directory code and add more sanity checks Phillip Lougher
2026-11-25  1:47 ` Phillip Lougher [this message]
2026-11-25  1:47 ` [PATCH v4 23/23] Squashfs: check index isn't greater than S64_MAX in squashfs_read_data() Phillip Lougher

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=20261125014737.347231-23-phillip@squashfs.org.uk \
    --to=phillip@squashfs.org.uk \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®