[PATCH] squashfs: exclude fragment tails from block-list scans

From: shaos

Date: Thu Nov 26 2026 - 07:29:37 EST


Hello,

seek_hole_data() rounds i_size up to compute file_end and
uses that value as the number of block-list entries. For a file with
a fragment tail, the tail has no block-list word. The scan therefore
reads the next inode-metadata word and can classify fragment data as
a hole.

In the crafted image, pread() reads byte B at offset 4096, but
SEEK_HOLE reports 4096 for a data tail and SEEK_DATA returns ENXIO
for another file whose fragment tail contains data. Count only the
full data blocks when a fragment exists, and return the fragment
start as data when scanning past preceding holes.

The image mounts read-only. Mounting it requires CAP_SYS_ADMIN and
access to a block device. The effect is incorrect SEEK_DATA/SEEK_HOLE
results, which can cause sparse-copy tools to omit real tail data.
No memory corruption or privilege escalation is demonstrated.

HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: Linux 7.4.0-rc4-g8d7fe9885c62
compiler: gcc 11.4.0, GNU ld 2.38

The Gist contains the console output, kernel configuration, PoC,
image generator, and init harness:
https://gist.githubusercontent.com/shaos26/5cddc5a02fbba647b37bf60e8a4e0b05/raw/1e23ad163e22d0bd4fba3a65bd8db632cdd37f10/console-output.txt
https://gist.githubusercontent.com/shaos26/5cddc5a02fbba647b37bf60e8a4e0b05/raw/1e23ad163e22d0bd4fba3a65bd8db632cdd37f10/kernel-config.txt
https://gist.githubusercontent.com/shaos26/5cddc5a02fbba647b37bf60e8a4e0b05/raw/1e23ad163e22d0bd4fba3a65bd8db632cdd37f10/poc.c
https://gist.githubusercontent.com/shaos26/5cddc5a02fbba647b37bf60e8a4e0b05/raw/1e23ad163e22d0bd4fba3a65bd8db632cdd37f10/mkimg.py
https://gist.githubusercontent.com/shaos26/5cddc5a02fbba647b37bf60e8a4e0b05/raw/1e23ad163e22d0bd4fba3a65bd8db632cdd37f10/harness-init.c

Build the kernel from the HEAD commit above with kernel-config.txt.
The reproducer requires Python 3, static gcc, cpio, gzip, and QEMU.
Create the positive and metadata-word control images and initramfs:

python3 mkimg.py img.sqfs
PHANTOM=nonzero python3 mkimg.py img_ctl.sqfs
gcc -O2 -static -Wall -Wextra -o poc poc.c
gcc -O2 -static -Wall -Wextra -o init harness-init.c
mkdir -p root-pos root-ctl
cp poc init root-pos/
cp img.sqfs root-pos/img.sqfs
cp poc init root-ctl/
cp img_ctl.sqfs root-ctl/img.sqfs
(cd root-pos && find . -print0 | cpio --null -o -H newc |
gzip -9) > initramfs-pos.cpio.gz
(cd root-ctl && find . -print0 | cpio --null -o -H newc |
gzip -9) > initramfs-ctl.cpio.gz

Run the positive image with:

timeout -k 2 20 qemu-system-x86_64 -machine pc -accel tcg,thread=multi \
-cpu max -m 512 -smp 1 \
-kernel linux/build/arch/x86/boot/bzImage \
-initrd initramfs-pos.cpio.gz \
-append 'console=ttyS0 panic=1 loglevel=7 vivid.n_devs=0 dummy_hcd.num=0 netrom.nr_ndevs=0 rose.rose_ndevs=0 comedi.comedi_num_legacy_minors=0' \
-nographic -no-reboot -monitor none

Repeat with initramfs-ctl.cpio.gz for the metadata-word control.

The unpatched positive image reports SEEK_HOLE=4096 for pos_hole
and SEEK_DATA=-ENXIO for pos_data even though pread() reads B at
offset 4096. The metadata-word control reports the correct results.
The patched kernel reports the same correct results for both images.

Fixes: dec91e7ab10e ("Squashfs: add SEEK_DATA/SEEK_HOLE support")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: shaos <shawnq1@xxxxxxx>
---
fs/squashfs/file.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c
index d83594ce91d23..cbe21f6168d94 100644
--- a/fs/squashfs/file.c
+++ b/fs/squashfs/file.c
@@ -681,9 +681,12 @@ static loff_t seek_hole_data(struct file *file, loff_t offset, int whence)
if ((unsigned long long)offset >= i_size_read(inode))
return -ENXIO;

+ /* A fragment tail has no block-list entry. */
+ if (squashfs_i(inode)->fragment_block != SQUASHFS_INVALID_BLK)
+ file_end--;
+
/* is offset within tailend and is tailend packed into a fragment? */
- if (index + 1 == file_end &&
- squashfs_i(inode)->fragment_block != SQUASHFS_INVALID_BLK) {
+ if (index == file_end && squashfs_i(inode)->fragment_block != SQUASHFS_INVALID_BLK) {
if (whence == SEEK_DATA)
return offset;

@@ -737,7 +740,10 @@ static loff_t seek_hole_data(struct file *file, loff_t offset, int whence)

not_found:
/* whence value determines what happens */
- if (whence == SEEK_DATA)
+ if (whence == SEEK_DATA &&
+ squashfs_i(inode)->fragment_block != SQUASHFS_INVALID_BLK)
+ offset = file_end << msblk->block_log;
+ else if (whence == SEEK_DATA)
offset = -ENXIO;
else
/* there is an implicit hole at the end of any file */
--
2.43.0