fs/squashfs/block.c | 20 ++- fs/squashfs/cache.c | 8 +- fs/squashfs/dir.c | 114 +++++++++---- fs/squashfs/export.c | 146 ++++++++++++----- fs/squashfs/file.c | 93 +++++++---- fs/squashfs/fragment.c | 113 ++++++++++--- fs/squashfs/id.c | 9 +- fs/squashfs/inode.c | 77 +++++---- fs/squashfs/namei.c | 137 +++++++++++----- fs/squashfs/squashfs.h | 9 +- fs/squashfs/squashfs_fs.h | 101 ------------ fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/squashfs_fs_sb.h | 25 ++- fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ fs/squashfs/super.c | 96 +++++++---- fs/squashfs/xattr.c | 49 ++++-- fs/squashfs/xattr.h | 21 ++- fs/squashfs/xattr_id.c | 173 ++++++++++++++------ 18 files changed, 1133 insertions(+), 422 deletions(-)
This large patch-set is inspired by the recent spate of AI generated code reports. Four of the patches directly fix AI reported issues, and the rest have been found manually by a read-through and testing of the code. In all cases (except patches 1 - 8, 18 - 20) the patches deal with over-sights relating to variable overflow, arithmetic overflow, variable signedness or just lack of sanity checking of data read from disk. These can cause the code to fail, and which could be used as a potential exploit. Patches 18, 19, and 20 fix potential excessive memory use exploits. The code in question was written in the early days when Squashfs filesystems were small, but with very large filesystems it could try to allocate memory in excess of 4 MiB, and up to 64 MiB. The patches can be broken down as follows: Patches 1 - 6 are preparatory patches. They convert old fashioned #define macros into inline functions. These being typed make widths and signedness explicit, and allow additional checking to be added in later patches. Patch 7 fixes an incorrect offset value in TRACE statements in squashfs_read_inode(). This was new to the v2 patch-set. Patch 8 is new to this v3 patch-set. The patch is an attempt to stop Sashiko from getting stuck in logical contradictions when some sanity checks are introduced later. Patches 9 - 17 fix various issues relating to variable overflow, arithmetic overflow and add additional sanity checking to prevent these issues from occurring. Patches 18 - 20 remove the excessive allocation of memory by introducing caches, which limit the maximum usage to 4 pages. Patches 21 - 22 were new to the v2 patch-set. They deal with some issues Sashiko reported in the original patch-set. Patch 23 was also new to the v2 patch-set. It adds an additional belts and braces sanity check. Patches 9 - 21 have reproducers which show how the existing code faults can be exploited, in some cases which can allow a Squashfs filesystem to be tampered with to make files readable by unprivileged users. BTW checkpatch.pl complains about the '#' lines in the BASH script reproducers. Let me know if that will cause problems. fs/squashfs/block.c | 20 ++- fs/squashfs/cache.c | 8 +- fs/squashfs/dir.c | 114 +++++++++---- fs/squashfs/export.c | 146 ++++++++++++----- fs/squashfs/file.c | 93 +++++++---- fs/squashfs/fragment.c | 113 ++++++++++--- fs/squashfs/id.c | 9 +- fs/squashfs/inode.c | 77 +++++---- fs/squashfs/namei.c | 137 +++++++++++----- fs/squashfs/squashfs.h | 9 +- fs/squashfs/squashfs_fs.h | 101 ------------ fs/squashfs/squashfs_fs_i.h | 2 +- fs/squashfs/squashfs_fs_sb.h | 25 ++- fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ fs/squashfs/super.c | 96 +++++++---- fs/squashfs/xattr.c | 49 ++++-- fs/squashfs/xattr.h | 21 ++- fs/squashfs/xattr_id.c | 173 ++++++++++++++------ 18 files changed, 1133 insertions(+), 422 deletions(-) Phillip --- Squashfs maintainer and author
On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <phillip@squashfs.org.uk> wrote:
> This large patch-set is inspired by the recent spate of AI generated
> code reports. Four of the patches directly fix AI reported issues, and
> the rest have been found manually by a read-through and testing of the
> code.
>
Thanks. I've updated mm.git's mm-nonmm-unstable branch to this version.
Here's how v3 altered mm.git:
fs/squashfs/dir.c | 37 ++++++++++++--------
fs/squashfs/file.c | 4 +-
fs/squashfs/inode.c | 8 +++-
fs/squashfs/namei.c | 57 +++++++++++++++----------------
fs/squashfs/squashfs_helpers.h | 11 ++---
5 files changed, 65 insertions(+), 52 deletions(-)
--- a/fs/squashfs/dir.c~b
+++ a/fs/squashfs/dir.c
@@ -134,23 +134,15 @@ static int squashfs_readdir(struct file
{
struct inode *inode = file_inode(file);
struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
- s64 block = squashfs_dir_start(squashfs_i(inode)->start, msblk);
- int offset = squashfs_i(inode)->offset, err;
+ s64 block;
+ int offset, err;
loff_t length;
unsigned int inode_number, dir_count, size, type;
struct squashfs_dir_header dirh;
struct squashfs_dir_entry *dire = NULL;
- TRACE("Entered squashfs_readdir [%llx:%x]\n", squashfs_i(inode)->start, offset);
-
- if (block < 0)
- goto failed_read;
-
- dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
- if (dire == NULL) {
- ERROR("Failed to allocate squashfs_dir_entry\n");
- goto finish;
- }
+ TRACE("Entered squashfs_readdir [%llx:%x]\n", squashfs_i(inode)->start,
+ squashfs_i(inode)->offset);
/*
* Return "." and ".." entries as the first two filenames in the
@@ -181,6 +173,21 @@ static int squashfs_readdir(struct file
ctx->pos += size;
}
+ if (i_size_read(inode) == 3)
+ /* The directory is empty, nothing more to do */
+ goto finish;
+
+ block = squashfs_dir_start(squashfs_i(inode)->start, msblk);
+ if (block < 0)
+ goto failed_read;
+
+ dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
+ if (dire == NULL) {
+ ERROR("Failed to allocate squashfs_dir_entry\n");
+ goto finish;
+ }
+
+ offset = squashfs_i(inode)->offset;
length = get_dir_index_using_offset(inode->i_sb, &block, &offset,
squashfs_i(inode)->dir_idx_start,
squashfs_i(inode)->dir_idx_offset,
@@ -255,12 +262,14 @@ static int squashfs_readdir(struct file
}
}
-finish:
kfree(dire);
+
+finish:
return 0;
failed_read:
- ERROR("Unable to read directory block [%llx:%x]\n", squashfs_i(inode)->start, offset);
+ ERROR("Unable to read directory [%llx:%x]\n", squashfs_i(inode)->start,
+ squashfs_i(inode)->offset);
kfree(dire);
return 0;
}
--- a/fs/squashfs/file.c~b
+++ a/fs/squashfs/file.c
@@ -596,10 +596,10 @@ static void squashfs_readahead(struct re
{
struct inode *inode = ractl->mapping->host;
struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
- size_t mask = (1UL << msblk->block_log) - 1;
+ s64 mask = (1UL << msblk->block_log) - 1;
unsigned short shift = msblk->block_log - PAGE_SHIFT;
loff_t start = readahead_pos(ractl) & ~mask;
- size_t len = readahead_length(ractl) + readahead_pos(ractl) - start;
+ s64 len = readahead_length(ractl) + readahead_pos(ractl) - start;
struct squashfs_page_actor *actor;
unsigned int nr_pages = 0;
struct page **pages;
--- a/fs/squashfs/inode.c~b
+++ a/fs/squashfs/inode.c
@@ -255,8 +255,10 @@ int squashfs_read_inode(struct inode *in
/* The directory size must be 3 or larger, as it includes "." and ".." entries */
inode->i_size = le16_to_cpu(sqsh_ino->file_size);
- if (inode->i_size < 3)
+ if (inode->i_size < 3) {
+ err = -EINVAL;
goto failed_read;
+ }
set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
inode->i_op = &squashfs_dir_inode_ops;
@@ -284,8 +286,10 @@ int squashfs_read_inode(struct inode *in
/* The directory size must be 3 or larger, as it includes "." and ".." entries */
inode->i_size = le32_to_cpu(sqsh_ino->file_size);
- if (inode->i_size < 3)
+ if (inode->i_size < 3) {
+ err = -EINVAL;
goto failed_read;
+ }
xattr_id = le32_to_cpu(sqsh_ino->xattr);
set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
--- a/fs/squashfs/namei.c~b
+++ a/fs/squashfs/namei.c
@@ -74,11 +74,8 @@ static unsigned int get_dir_index_using_
TRACE("Entered get_dir_index_using_name, i_count %u\n", i_count);
- dir_index = kmalloc(sizeof(*dir_index) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
- if (dir_index == NULL) {
- ERROR("Failed to allocate squashfs_dir_index\n");
+ if (i_count == 0)
goto out;
- }
/*
* Translate from external index to internal index. This is offset by
@@ -97,6 +94,12 @@ static unsigned int get_dir_index_using_
max_index -= sizeof(struct squashfs_dir_header) +
sizeof(struct squashfs_dir_entry) + 1;
+ dir_index = kmalloc(sizeof(*dir_index) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
+ if (dir_index == NULL) {
+ ERROR("Failed to allocate squashfs_dir_index\n");
+ goto out;
+ }
+
for (i = 0; i < i_count; i++) {
err = squashfs_read_metadata(sb, dir_index, &index_start,
&index_offset, sizeof(*dir_index));
@@ -147,10 +150,9 @@ static unsigned int get_dir_index_using_
*next_block = res;
}
-out:
*next_offset = ((u64)length + *next_offset) % SQUASHFS_METADATA_SIZE;
kfree(dir_index);
-
+out:
/*
* Return index (f_pos) of the looked up metadata block. Translate
* from internal f_pos to external f_pos which is offset by 3 because
@@ -170,15 +172,23 @@ static struct dentry *squashfs_lookup(st
struct squashfs_sb_info *msblk = dir->i_sb->s_fs_info;
struct squashfs_dir_header dirh;
struct squashfs_dir_entry *dire = NULL;
- u64 block = squashfs_dir_start(squashfs_i(dir)->start, msblk);
- int offset = squashfs_i(dir)->offset;
- int err;
+ s64 block;
+ int offset, err;
loff_t length;
unsigned int dir_count, size;
- TRACE("Entered squashfs_lookup [%llx:%x]\n", squashfs_i(dir)->start, offset);
+ TRACE("Entered squashfs_lookup [%llx:%x]\n", squashfs_i(inode)->start,
+ squashfs_i(inode)->offset);
- if ((s64)block < 0)
+ if (len > SQUASHFS_NAME_LEN)
+ return ERR_PTR(-ENAMETOOLONG);
+
+ if (i_size_read(dir) == 3)
+ /* The directory is empty, and so there's nothing to do */
+ goto exit_lookup;
+
+ block = squashfs_dir_start(squashfs_i(dir)->start, msblk);
+ if (block < 0)
goto data_error;
dire = kmalloc(sizeof(*dire) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
@@ -187,11 +197,7 @@ static struct dentry *squashfs_lookup(st
return ERR_PTR(-ENOMEM);
}
- if (len > SQUASHFS_NAME_LEN) {
- err = -ENAMETOOLONG;
- goto failed;
- }
-
+ offset = squashfs_i(dir)->offset;
length = get_dir_index_using_name(dir->i_sb, &block, &offset,
squashfs_i(dir)->dir_idx_start,
squashfs_i(dir)->dir_idx_offset,
@@ -203,7 +209,7 @@ static struct dentry *squashfs_lookup(st
* Read directory header.
*/
if (!squashfs_dir_space(dir, length, sizeof(dirh)))
- goto read_failure;
+ goto data_error;
err = squashfs_read_metadata(dir->i_sb, &dirh, &block,
&offset, sizeof(dirh));
@@ -221,10 +227,8 @@ static struct dentry *squashfs_lookup(st
/*
* Read directory entry.
*/
- if (!squashfs_dir_space(dir, length, sizeof(*dire))) {
- err = -ENOSPC;
- goto read_failure;
- }
+ if (!squashfs_dir_space(dir, length, sizeof(*dire)))
+ goto data_error;
err = squashfs_read_metadata(dir->i_sb, dire, &block,
&offset, sizeof(*dire));
@@ -238,10 +242,8 @@ static struct dentry *squashfs_lookup(st
if (size > SQUASHFS_NAME_LEN)
goto data_error;
- if (!squashfs_dir_space(dir, length, size)) {
- err = -ENOSPC;
- goto read_failure;
- }
+ if (!squashfs_dir_space(dir, length, size))
+ goto data_error;
err = squashfs_read_metadata(dir->i_sb, dire->name,
&block, &offset, size);
@@ -280,9 +282,8 @@ data_error:
err = -EIO;
read_failure:
- ERROR("Unable to read directory block [%llx:%x]\n",
- squashfs_i(dir)->start, squashfs_i(dir)->offset);
-failed:
+ ERROR("Unable to read directory [%llx:%x]\n", squashfs_i(dir)->start,
+ squashfs_i(dir)->offset);
kfree(dire);
return ERR_PTR(err);
}
--- a/fs/squashfs/squashfs_helpers.h~b
+++ a/fs/squashfs/squashfs_helpers.h
@@ -59,7 +59,7 @@ static inline s64 squashfs_inode_start(s
start = msblk->inode_table + squashfs_inode_blk(inode);
- if (start > msblk->directory_table)
+ if (start >= msblk->directory_table)
return -EINVAL;
return start;
@@ -80,8 +80,7 @@ static inline s64 squashfs_dir_start(u32
start = msblk->directory_table + blk;
- if (msblk->directory_table < msblk->directory_table_end &&
- start >= msblk->directory_table_end)
+ if (start >= msblk->directory_table_end)
return -EINVAL;
return start;
@@ -135,7 +134,7 @@ static inline s64 squashfs_fragment_inde
return start;
}
-static inline s16 squashfs_fragment_indexes_offset(u32 fragments)
+static inline s32 squashfs_fragment_indexes_offset(u32 fragments)
{
return (squashfs_fragment_index(fragments) * sizeof(long long)) & (PAGE_SIZE - 1);
}
@@ -188,7 +187,7 @@ static inline s64 squashfs_lookup_blocks
return start;
}
-static inline s16 squashfs_lookup_blocks_offset(u32 inodes)
+static inline s32 squashfs_lookup_blocks_offset(u32 inodes)
{
return (squashfs_lookup_block(inodes) * sizeof(long long)) & (PAGE_SIZE - 1);
}
@@ -269,7 +268,7 @@ static inline s64 squashfs_xattr_blocks_
return start;
}
-static inline s16 squashfs_xattr_blocks_offset(u32 xattrs)
+static inline s32 squashfs_xattr_blocks_offset(u32 xattrs)
{
return (squashfs_xattr_block(xattrs) * sizeof(long long)) & (PAGE_SIZE - 1);
}
_
On Mon, 23 Nov 2026 03:42:41 +0000 Phillip Lougher <phillip@squashfs.org.uk> wrote: > This large patch-set is inspired by the recent spate of AI generated > code reports. Four of the patches directly fix AI reported issues, and > the rest have been found manually by a read-through and testing of the > code. > > In all cases (except patches 1 - 8, 18 - 20) the patches deal with > over-sights relating to variable overflow, arithmetic overflow, variable > signedness or just lack of sanity checking of data read from disk. > These can cause the code to fail, and which could be used as a potential > exploit. > > Patches 18, 19, and 20 fix potential excessive memory use exploits. The > code in question was written in the early days when Squashfs filesystems > were small, but with very large filesystems it could try to allocate > memory in excess of 4 MiB, and up to 64 MiB. > > The patches can be broken down as follows: > > Patches 1 - 6 are preparatory patches. They convert old fashioned > #define macros into inline functions. These being typed make widths and > signedness explicit, and allow additional checking to be added in later > patches. Nope.... Using 16bit sizes is a recipe for disaster. Remember every time the variable is accessed it is extended to 32 'int', the arithmetic done as 'int' (unless the other operand is 'unsigned int') and then truncated back to 16 bits. I looked at a few, they are horrid beyond belief. I can't believe some of those values can be negative - so don't use signed types. Use 'unsigned int' (or u32 if fixed size) so that the values don't get promoted to 'signed int' all time - which is what happens here. David > > Patch 7 fixes an incorrect offset value in TRACE statements in > squashfs_read_inode(). This was new to the v2 patch-set. > > Patch 8 is new to this v3 patch-set. The patch is an attempt to stop > Sashiko from getting stuck in logical contradictions when some > sanity checks are introduced later. > > Patches 9 - 17 fix various issues relating to variable overflow, > arithmetic overflow and add additional sanity checking to prevent these > issues from occurring. > > Patches 18 - 20 remove the excessive allocation of memory by introducing > caches, which limit the maximum usage to 4 pages. > > Patches 21 - 22 were new to the v2 patch-set. They deal with some > issues Sashiko reported in the original patch-set. > > Patch 23 was also new to the v2 patch-set. It adds an additional belts > and braces sanity check. > > Patches 9 - 21 have reproducers which show how the existing code faults > can be exploited, in some cases which can allow a Squashfs filesystem > to be tampered with to make files readable by unprivileged users. > > BTW checkpatch.pl complains about the '#' lines in the BASH script > reproducers. Let me know if that will cause problems. > > fs/squashfs/block.c | 20 ++- > fs/squashfs/cache.c | 8 +- > fs/squashfs/dir.c | 114 +++++++++---- > fs/squashfs/export.c | 146 ++++++++++++----- > fs/squashfs/file.c | 93 +++++++---- > fs/squashfs/fragment.c | 113 ++++++++++--- > fs/squashfs/id.c | 9 +- > fs/squashfs/inode.c | 77 +++++---- > fs/squashfs/namei.c | 137 +++++++++++----- > fs/squashfs/squashfs.h | 9 +- > fs/squashfs/squashfs_fs.h | 101 ------------ > fs/squashfs/squashfs_fs_i.h | 2 +- > fs/squashfs/squashfs_fs_sb.h | 25 ++- > fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ > fs/squashfs/super.c | 96 +++++++---- > fs/squashfs/xattr.c | 49 ++++-- > fs/squashfs/xattr.h | 21 ++- > fs/squashfs/xattr_id.c | 173 ++++++++++++++------ > 18 files changed, 1133 insertions(+), 422 deletions(-) > > Phillip > --- > Squashfs maintainer and author >
On Mon, 23 Nov 2026 09:23:31 +0000 David Laight <david.laight.linux@gmail.com> wrote: > On Mon, 23 Nov 2026 03:42:41 +0000 > Phillip Lougher <phillip@squashfs.org.uk> wrote: > > > This large patch-set is inspired by the recent spate of AI generated > > code reports. Four of the patches directly fix AI reported issues, and > > the rest have been found manually by a read-through and testing of the > > code. > > > > In all cases (except patches 1 - 8, 18 - 20) the patches deal with > > over-sights relating to variable overflow, arithmetic overflow, variable > > signedness or just lack of sanity checking of data read from disk. > > These can cause the code to fail, and which could be used as a potential > > exploit. > > > > Patches 18, 19, and 20 fix potential excessive memory use exploits. The > > code in question was written in the early days when Squashfs filesystems > > were small, but with very large filesystems it could try to allocate > > memory in excess of 4 MiB, and up to 64 MiB. > > > > The patches can be broken down as follows: > > > > Patches 1 - 6 are preparatory patches. They convert old fashioned > > #define macros into inline functions. These being typed make widths and > > signedness explicit, and allow additional checking to be added in later > > patches. > > Nope.... > Using 16bit sizes is a recipe for disaster. > Remember every time the variable is accessed it is extended to 32 'int', > the arithmetic done as 'int' (unless the other operand is 'unsigned int') > and then truncated back to 16 bits. This is getting beyond stupid. This patch-set is to shut up woefully stupid AI that been inflicted on everyone over the last couple of months. If I use a u32 then stupid AI will *believe* the value can be up to u32 and then complain about arthmetic overflow. If I deliberately use a u16/s16 I'm telling AI the value can't be more than that, and it can't overflow. At the moment, submitting patches to the kernel has become impossible, necessary workarounds to fix the AI inflicting the submission trigger complaints from humans. Result: an endless cycle of changes which get nowhere. > > I looked at a few, they are horrid beyond belief. > > I can't believe some of those values can be negative - so don't use > signed types. > Use 'unsigned int' (or u32 if fixed size) so that the values don't > get promoted to 'signed int' all time - which is what happens here. > > David > > > > > Patch 7 fixes an incorrect offset value in TRACE statements in > > squashfs_read_inode(). This was new to the v2 patch-set. > > > > Patch 8 is new to this v3 patch-set. The patch is an attempt to stop > > Sashiko from getting stuck in logical contradictions when some > > sanity checks are introduced later. > > > > Patches 9 - 17 fix various issues relating to variable overflow, > > arithmetic overflow and add additional sanity checking to prevent these > > issues from occurring. > > > > Patches 18 - 20 remove the excessive allocation of memory by introducing > > caches, which limit the maximum usage to 4 pages. > > > > Patches 21 - 22 were new to the v2 patch-set. They deal with some > > issues Sashiko reported in the original patch-set. > > > > Patch 23 was also new to the v2 patch-set. It adds an additional belts > > and braces sanity check. > > > > Patches 9 - 21 have reproducers which show how the existing code faults > > can be exploited, in some cases which can allow a Squashfs filesystem > > to be tampered with to make files readable by unprivileged users. > > > > BTW checkpatch.pl complains about the '#' lines in the BASH script > > reproducers. Let me know if that will cause problems. > > > > fs/squashfs/block.c | 20 ++- > > fs/squashfs/cache.c | 8 +- > > fs/squashfs/dir.c | 114 +++++++++---- > > fs/squashfs/export.c | 146 ++++++++++++----- > > fs/squashfs/file.c | 93 +++++++---- > > fs/squashfs/fragment.c | 113 ++++++++++--- > > fs/squashfs/id.c | 9 +- > > fs/squashfs/inode.c | 77 +++++---- > > fs/squashfs/namei.c | 137 +++++++++++----- > > fs/squashfs/squashfs.h | 9 +- > > fs/squashfs/squashfs_fs.h | 101 ------------ > > fs/squashfs/squashfs_fs_i.h | 2 +- > > fs/squashfs/squashfs_fs_sb.h | 25 ++- > > fs/squashfs/squashfs_helpers.h | 362 +++++++++++++++++++++++++++++++++++++++++ > > fs/squashfs/super.c | 96 +++++++---- > > fs/squashfs/xattr.c | 49 ++++-- > > fs/squashfs/xattr.h | 21 ++- > > fs/squashfs/xattr_id.c | 173 ++++++++++++++------ > > 18 files changed, 1133 insertions(+), 422 deletions(-) > > > > Phillip > > --- > > Squashfs maintainer and author > > > -- Phillip Lougher <phillip@squashfs.org.uk>
© 2016 - 2026 Red Hat, Inc.