[PATCH v3 22/23] Squashfs: fix regression in squashfs_copy_cache()
From: Phillip Lougher
Date: Sun Nov 22 2026 - 22:45:15 EST
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.
Fixes: 5641371fd0b3 ("squashfs; convert squashfs_copy_cache() to take a folio")
Signed-off-by: Phillip Lougher <phillip@xxxxxxxxxxxxxxx>
---
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 0a8288a53b25..bfbcfd6f1035 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