fs/squashfs: fix two out-of-bounds reads on crafted images
Subsystems (2)
cover fs/squashfs: fix two out-of-bounds reads on crafted images
Two independent out-of-bounds reads in the SquashFS driver, both reachable by pointing U-Boot at an attacker-supplied image (e.g. ls/load on a crafted USB/SD/netboot rootfs). Either one crashes the bootloader (DoS); patch 2 can also copy adjacent heap memory into the loaded file (information disclosure).
1/2 sqfs_find_inode()/sqfs_inode_size() walk the decompressed inode table
using on-disk sizes with no check that the cursor stays inside the buffer -> wild read / SEGV, e.g. from a simple "ls".
2/2 sqfs_read_nest() uses the on-disk fragment offset as an unbounded
source index into the fragment block -> out-of-bounds heap read when loading a fragment-backed file.
Both were found by fuzzing the sandbox build (CONFIG_ASAN) of sqfsls/sqfsload with mutated images. With the fixes, the crashing inputs are rejected cleanly, 2000 fuzz iterations produce no further crashes, and the valid-image path is unchanged.
These are distinct from the 2024 SquashFS CVE cluster (CVE-2024-57254..57259, fixed in 2025.01-rc1) and from the sqfs_frag_lookup() fix (e365a269df5): the earlier work added NULL checks at the callers and fixed the symlink-size and fragment-table paths, but left these inode-table-walk and fragment-data paths unbounded.
The two patches are independent and can be applied in either order.
Piyush Paliwal (2):
fs/squashfs: bound the inode table walk in sqfs_find_inode() fs/squashfs: bound fragment offset/size in sqfs_read_nest()
fs/squashfs/sqfs.c | 50 ++++++++++++---- fs/squashfs/sqfs_filesystem.h | 6 +- fs/squashfs/sqfs_inode.c | 106 +++++++++++++++++++++++++++++----- 3 files changed, 134 insertions(+), 28 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/2 fs/squashfs: bound the inode table walk in sqfs_find_inode()
sqfs_find_inode() walks the decompressed inode table advancing "offset += sqfs_inode_size(base, ...)" with no check that offset stays within the table (metablks_count * SQFS_METADATA_BLOCK_SIZE). All sizes come from the on-disk image, including the unbounded extended-directory (LDIR) index walk and the regular-file block-list term in sqfs_inode_size(). A crafted image makes base run off the end of the buffer -> out-of-bounds read / SEGV, reachable simply by listing the image (ls/sqfsls) or any operation that resolves a path.
The earlier fix 3fb1df1e5 ("squashfs: Check sqfs_find_inode() return value") only added NULL checks at the call sites; it did not add the missing internal bound, so the wild read still occurs before the function can return. c8e929e5 fixed only the symlink case of sqfs_inode_size(), leaving the LDIR index walk unbounded.
Thread the inode table size from sqfs_read_inode_table() through the squashfs_dir_stream to sqfs_find_inode(), and:
- reject an inode whose base header does not fit in the table;
- pass the remaining byte count to sqfs_inode_size() and validate every variable-length read (LDIR index list, REG/LREG block list, symlink, device/ipc inodes) against it, with overflow-checked arithmetic;
- reject an inode whose computed size leaves the table.
Found by fuzzing the sandbox (CONFIG_ASAN) sqfsls/sqfsload with mutated images. Before: SEGV in sqfs_find_inode (sqfs_inode.c) and in sqfs_inode_size() LDIR walk. After: malformed images are rejected cleanly; 2000 fuzz iterations produce no crash and the valid-image path is unchanged.
--- fs/squashfs/sqfs.c | 26 +++++---- fs/squashfs/sqfs_filesystem.h | 6 +- fs/squashfs/sqfs_inode.c | 106 +++++++++++++++++++++++++++++----- 3 files changed, 110 insertions(+), 28 deletions(-)
fs/squashfs/sqfs.c +16 -10
@@ -486,7 +486,8 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list, dirsp = (struct fs_dir_stream *)dirs;/* Start by root inode */ - table =sqfs_find_inode (dirs->inode_table,le32_to_cpu (sblk->inodes), + table =sqfs_find_inode (dirs->inode_table, dirs->inode_table_size, +le32_to_cpu (sblk->inodes), sblk->inodes, sblk->block_size);if (!table)return -EINVAL ;
@@ -543,7 +544,8 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list, dirs->dir_header->inode_number;/* Get reference to inode in the inode table */ - table =sqfs_find_inode (dirs->inode_table, new_inode_number, + table =sqfs_find_inode (dirs->inode_table, + dirs->inode_table_size, new_inode_number, sblk->inodes, sblk->block_size);if (!table)return -EINVAL ;
@@ -719,7 +721,7 @@ static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset,return ret; } -static int sqfs_read_inode_table (unsigned char **inode_table) +static int sqfs_read_inode_table (unsigned char **inode_table,size_t *out_size) {struct squashfs_super_block *sblk = ctxt.sblk;u64 start, n_blks, table_offset, table_size;
@@ -773,6 +775,8 @@ static int sqfs_read_inode_table(unsigned char **inode_table) goto free_itb; } + *out_size = (size_t )metablks_count *SQFS_METADATA_BLOCK_SIZE ; + src_table = itb + table_offset +SQFS_HEADER_SIZE ;/* Extract compressed Inode table */
@@ -920,6 +924,7 @@ static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)int j, token_count =0 , ret =0 , metablks_count;struct squashfs_dir_stream *dirs;char **token_list =NULL , *path =NULL ; +size_t inode_table_size =0 ;u32 *pos_list =NULL ; dirs =calloc (1 ,sizeof (*dirs));
@@ -933,7 +938,7 @@ static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp) dirs->inode_table =NULL ; dirs->dir_table =NULL ; - ret =sqfs_read_inode_table (&inode_table); + ret =sqfs_read_inode_table (&inode_table, &inode_table_size);if (ret) { ret = -EINVAL ; goto out;
@@ -973,6 +978,7 @@ static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)* a general solution for the malloc size, since 'i' is a union. */ dirs->inode_table = inode_table; + dirs->inode_table_size = inode_table_size; dirs->dir_table = dir_table; ret =sqfs_search_dir (dirs, token_list, token_count, pos_list, metablks_count);
@@ -1071,8 +1077,8 @@ static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **d } i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; - ipos =sqfs_find_inode (dirs->inode_table, i_number, sblk->inodes, - sblk->block_size); + ipos =sqfs_find_inode (dirs->inode_table, dirs->inode_table_size, + i_number, sblk->inodes, sblk->block_size);if (!ipos)return -SQFS_STOP_READDIR ;
@@ -1430,8 +1436,8 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset, } i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; - ipos =sqfs_find_inode (dirs->inode_table, i_number, sblk->inodes, - sblk->block_size); + ipos =sqfs_find_inode (dirs->inode_table, dirs->inode_table_size, + i_number, sblk->inodes, sblk->block_size);if (!ipos) { ret = -EINVAL ; goto out;
@@ -1699,8 +1705,8 @@ static int sqfs_size_nest(const char *filename, loff_t *size) } i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; - ipos =sqfs_find_inode (dirs->inode_table, i_number, sblk->inodes, - sblk->block_size); + ipos =sqfs_find_inode (dirs->inode_table, dirs->inode_table_size, + i_number, sblk->inodes, sblk->block_size);if (!ipos) { *size =0 ;
fs/squashfs/sqfs_filesystem.h +4 -2
@@ -275,6 +275,8 @@ struct squashfs_dir_stream {* sqfs_opendir() and freed in sqfs_closedir(). */ unsigned char *inode_table; +/* Size in bytes of the decompressed inode_table buffer */ +size_t inode_table_size;unsigned char *dir_table; };
@@ -293,8 +295,8 @@ struct squashfs_file_info {bool comp; }; -void *sqfs_find_inode (void *inode_table,int inode_number,__le32 inode_count, -__le32 block_size); +void *sqfs_find_inode (void *inode_table,size_t table_size,int inode_number, +__le32 inode_count,__le32 block_size);int sqfs_dir_offset (void *dir_i,u32 *m_list,int m_count);
fs/squashfs/sqfs_inode.c +90 -16
199 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH 2/2 fs/squashfs: bound fragment offset/size in sqfs_read_nest()
In reply to Piyush PaliwalWhen reading a fragment-backed file, sqfs_read_nest() copies the file data out of the fragment block with:
memcpy(buf + *actread, &fragment_block[finfo.offset],
finfo.size - *actread);
finfo.offset (the fragment's byte offset) and finfo.size come straight from the on-disk inode and are never validated against the fragment block length. Unlike the data-block loop above it, this path does not clamp the source span, so a crafted inode makes the memcpy read past the fragment buffer -> out-of-bounds heap read. The leaked bytes are copied into the user-visible load buffer (information disclosure) or fault. This affects both the compressed (dest_len bytes) and the uncompressed (table_size bytes) fragment cases.
Validate finfo.offset and the copy length against the available fragment data before each memcpy and reject malformed inodes.
Found by fuzzing the sandbox (CONFIG_ASAN) sqfsload with mutated images:
before, SEGV in sqfs_read_nest() at the fragment memcpy; after, malformed images are rejected and valid fragmented files still load correctly.
--- fs/squashfs/sqfs.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
fs/squashfs/sqfs.c +24 -0
@@ -1630,6 +1630,19 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset, goto out; } +/* +* finfo.offset and finfo.size come from the on-disk inode and +* must not let the copy read past the decompressed fragment +* block (dest_len bytes). +*/ +if (finfo.size < (size_t )*actread || + finfo.offset > dest_len || + finfo.size - *actread > dest_len - finfo.offset) { +free (fragment_block); + ret = -EINVAL ; + goto out; + } +memcpy (buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread); *actread = finfo.size;
@@ -1638,6 +1651,17 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset, }else if (finfo.frag && !finfo.comp) { fragment_block = (void *)fragment + table_offset; +/* +* Same check for the uncompressed fragment: the readable data +* is table_size bytes starting at table_offset within fragment. +*/ +if (finfo.size < (size_t )*actread || + finfo.offset > table_size || + finfo.size - *actread > table_size - finfo.offset) { + ret = -EINVAL ; + goto out; + } +memcpy (buf + *actread, &fragment_block[finfo.offset], finfo.size - *actread); *actread = finfo.size; }
Signature · 1 line
2.41.0
Hi Piyush,
Le 12/06/2026 à 09:54, Piyush Paliwal a écrit :
Quoted 25 lines
Two independent out-of-bounds reads in the SquashFS driver, both reachable by pointing U-Boot at an attacker-supplied image (e.g. ls/load on a crafted USB/SD/netboot rootfs). Either one crashes the bootloader (DoS); patch 2 can also copy adjacent heap memory into the loaded file (information disclosure).
1/2 sqfs_find_inode()/sqfs_inode_size() walk the decompressed inode table
using on-disk sizes with no check that the cursor stays inside the buffer -> wild read / SEGV, e.g. from a simple "ls".
2/2sqfs_read_nest()uses the on-disk fragment offset as an unbounded
source index into the fragment block -> out-of-bounds heap read when loading a fragment-backed file.Both were found by fuzzing the sandbox build (
CONFIG_ASAN) of sqfsls/sqfsload with mutated images. With the fixes, the crashing inputs are rejected cleanly, 2000 fuzz iterations produce no further crashes, and the valid-image path is unchanged.These are distinct from the 2024 SquashFS CVE cluster (CVE-2024-57254..57259, fixed in 2025.01-rc1) and from the
sqfs_frag_lookup()fix (e365a269df5): the earlier work added NULL checks at the callers and fixed the symlink-size and fragment-table paths, but left these inode-table-walk and fragment-data paths unbounded.The two patches are independent and can be applied in either order.
Quoted 9 lines
Piyush Paliwal (2):
fs/squashfs: bound the inode table walk insqfs_find_inode()fs/squashfs: bound fragment offset/size insqfs_read_nest()fs/squashfs/sqfs.c | 50 ++++++++++++---- fs/squashfs/sqfs_filesystem.h | 6 +- fs/squashfs/sqfs_inode.c | 106 +++++++++++++++++++++++++++++----- 3 files changed, 134 insertions(+), 28 deletions(-)
Those patches look good.
NB: a patch correcting the same issue as patch 1 was sent 12 hours earlier by Hem Parekh, but it fixes less problems than this one.
Thanks!
Hi all,
Quoted 37 lines · Richard GENOUD
Hi Piyush,
Le 12/06/2026 à 09:54, Piyush Paliwal a écrit :
Quoted 25 lines
Two independent out-of-bounds reads in the SquashFS driver, both reachable by pointing U-Boot at an attacker-supplied image (e.g. ls/load on a crafted USB/SD/netboot rootfs). Either one crashes the bootloader (DoS); patch 2 can also copy adjacent heap memory into the loaded file (information disclosure).
1/2 sqfs_find_inode()/sqfs_inode_size() walk the decompressed inode table using on-disk sizes with no check that the cursor stays inside the buffer -> wild read / SEGV, e.g. from a simple "ls".
2/2sqfs_read_nest()uses the on-disk fragment offset as an unbounded source index into the fragment block -> out-of-bounds heap read when loading a fragment-backed file.Both were found by fuzzing the sandbox build (
CONFIG_ASAN) of sqfsls/ sqfsload with mutated images. With the fixes, the crashing inputs are rejected cleanly, 2000 fuzz iterations produce no further crashes, and the valid-image path is unchanged.These are distinct from the 2024 SquashFS CVE cluster (CVE-2024-57254..57259, fixed in 2025.01-rc1) and from the
sqfs_frag_lookup()fix (e365a269df5): the earlier work added NULL checks at the callers and fixed the symlink- size and fragment-table paths, but left these inode-table-walk and fragment-data paths unbounded.The two patches are independent and can be applied in either order.
Quoted 9 lines
Piyush Paliwal (2):
fs/squashfs: bound the inode table walk insqfs_find_inode()fs/squashfs: bound fragment offset/size insqfs_read_nest()fs/squashfs/sqfs.c | 50 ++++++++++++----
fs/squashfs/sqfs_filesystem.h| 6 +- fs/squashfs/sqfs_inode.c | 106 +++++++++++++++++++++++++++++-----3 files changed, 134 insertions(+), 28 deletions(-)
Quoted 6 lines
Those patches look good.
NB: a patch correcting the same issue as patch 1 was sent 12 hours earlier by Hem Parekh, but it fixes less problems than this one.
Reviewed-by Richard Genoud <[email protected]>
Going through my inbox and stumbled upon this here. I see Richard is one of the custodians but I don't see a custodian tree specific to squashfs in MAINTAINERS. In patchwork, this series is delegated to Tom. I have not reviewed any of it so this isn't a push for "hitting the merge button", just trying to make sure it doesn't get forgotten :)
@Piyush, please refrain from adding Cc: [email protected] to your commit logs. 1) This is U-Boot and not the Linux kernel so it's adding noise they don't need. 2) We don't do stable releases in U-Boot.
Cheers,
Quentin
Quoted 5 lines · Piyush Paliwal
Two independent out-of-bounds reads in the SquashFS driver, both reachable by pointing U-Boot at an attacker-supplied image (e.g. ls/load on a crafted USB/SD/netboot rootfs). Either one crashes the bootloader (DoS); patch 2 can also copy adjacent heap memory into the loaded file (information disclosure).
Quoted 8 lines
1/2 sqfs_find_inode()/sqfs_inode_size() walk the decompressed inode table
using on-disk sizes with no check that the cursor stays inside the buffer -> wild read / SEGV, e.g. from a simple "ls".
2/2sqfs_read_nest()uses the on-disk fragment offset as an unbounded
source index into the fragment block -> out-of-bounds heap read when loading a fragment-backed file.[...]
Applied to u-boot/main, thanks!
[1/2] fs/squashfs: bound the inode table walk in sqfs_find_inode()
commit: 73ada2f433a99fb2f443bb76268f5ab023b11868
[2/2] fs/squashfs: bound fragment offset/size in sqfs_read_nest()
commit: 923f05ed3e231c51f259bee6e2ac823bdaf1c566Signature · 1 line
Tom