The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Phillip Lougher <phillip@squashfs.org.uk>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 00/23] Squashfs: fix a lot of AI issues and AI style issues
Date: Sun, 22 Nov 2026 20:13:11 -0800	[thread overview]
Message-ID: <20261122201311.89fa22450c58843f31ecf56d@linux-foundation.org> (raw)
In-Reply-To: <20261123034304.261742-1-phillip@squashfs.org.uk>

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);
 }
_


      parent reply	other threads:[~2026-11-23  4:13 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  3:42 [PATCH v3 00/23] Squashfs: fix a lot of AI issues and AI style issues Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 01/23] Squashfs: convert SQUASHFS_COMPRESSED_* to inline functions Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 02/23] Squashfs: convert SQUASHFS_INODE_* and SQUASHFS_MKINODE " Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 03/23] Squashfs: convert SQUASHFS_FRAGMENT_* " Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 04/23] Squashfs: convert SQUASHFS_LOOKUP_* " Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 05/23] Squashfs: convert SQUASHFS_ID_* " Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 06/23] Squashfs: convert SQUASHFS_XATTR_* " Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 07/23] Squashfs: fix offset in squashfs_read_inode() TRACE statements Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 08/23] Squashfs: don't compute start or alloc memory on empty directory or index Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 09/23] Squashfs: fix fragment bytes overflow on 32-bit architectures Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 10/23] Squashfs: fix fragment index table length too large exploit Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 11/23] Squashfs: sanity check inode metadata block location Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 12/23] Squashfs: sanity check directory " Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 13/23] Squashfs: widen xattr count variables and add sanity check Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 14/23] Squashfs: very large files can exceed an int or size_t Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 15/23] Squashfs: add more sanity checking to squashfs_block_size() Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 16/23] Squashfs: sanity check fragment block location and range Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 17/23] Squashfs: sanity check block list sizes " Phillip Lougher
2026-11-23  3:42 ` [PATCH v3 18/23] Squashfs: don't store large export table indexes in memory Phillip Lougher
2026-11-23  3:43 ` [PATCH v3 19/23] Squashfs: don't store large fragment " Phillip Lougher
2026-11-23  3:43 ` [PATCH v3 20/23] Squashfs: don't store large xattr id " Phillip Lougher
2026-11-23  3:43 ` [PATCH v3 21/23] Squashfs: fix variables in directory code and add more sanity checks Phillip Lougher
2026-11-23  3:43 ` [PATCH v3 22/23] Squashfs: fix regression in squashfs_copy_cache() Phillip Lougher
2026-11-23  3:43 ` [PATCH v3 23/23] Squashfs: check index isn't greater than S64_MAX in squashfs_read_data() Phillip Lougher
2026-11-23  4:13 ` Andrew Morton [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122201311.89fa22450c58843f31ecf56d@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=phillip@squashfs.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox