In qnx6_iget(), raw_inode->di_filelevels is read from disk without upper bounds checking. If raw_inode->di_filelevels exceeds QNX6_PTR_MAX_LEVELS (defined as 5 in include/linux/qnx6_fs.h), qnx6_block_map() performs out-of-bounds reads past di_block_ptr when dereferencing indirect tree levels. Validate raw_inode->di_filelevels against QNX6_PTR_MAX_LEVELS in qnx6_iget() and return -EIO if it exceeds 5. Tested in QEMU against tip of mainline commit 62f4c998b297 ("Merge tag 'parisc-for-7.3-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux") using a loop-device reproducer mounting a crafted QNX6 image with di_filelevels = 20: on the unfixed kernel, mounting triggered a buffer_head refcount underflow warning ("VFS: brelse: Trying to free free buffer"); whereas with this fix applied, qnx6_iget() cleanly rejects the corrupted inode with -EIO and logs "qnx6: bad filelevels 20 (max 5) on dev loop0, inode 1". Fixes: 5d026c724220 ("fs: initial qnx6fs addition") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Hui Peng --- Changes in v4: - Rebased cleanly onto upstream mainline commit 62f4c998b297. - Added QEMU test procedure and verification details in commit message body. - Updated commit description to state QNX6_PTR_MAX_LEVELS is 5 as noted by Matthias Goergens. fs/qnx6/inode.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fs/qnx6/inode.c b/fs/qnx6/inode.c index a15bb03f0ec3..e61c33f2e146 100644 --- a/fs/qnx6/inode.c +++ b/fs/qnx6/inode.c @@ -561,6 +561,14 @@ struct inode *qnx6_iget(struct super_block *sb, unsigned ino) sizeof(raw_inode->di_block_ptr)); ei->di_filelevels = raw_inode->di_filelevels; + if (ei->di_filelevels > QNX6_PTR_MAX_LEVELS) { + pr_err("bad filelevels %u (max %u) on dev %s, inode %u\n", + ei->di_filelevels, QNX6_PTR_MAX_LEVELS, sb->s_id, ino); + folio_release_kmap(folio, raw_inode); + iget_failed(inode); + return ERR_PTR(-EIO); + } + if (S_ISREG(inode->i_mode)) { inode->i_fop = &generic_ro_fops; inode->i_mapping->a_ops = &qnx6_aops; -- 2.55.0.1082.g2b9226bbc0-goog In qnx6_block_map(), if qnx6_check_blockptr(ptr) returns false while traversing indirect tree levels, the function returns 0 without calling brelse(bh), leaking the allocated buffer head. Call brelse(bh) before returning 0 on qnx6_check_blockptr() failure. Tested in QEMU against tip of mainline commit 62f4c998b297 ("Merge tag 'parisc-for-7.3-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux") using a loop-device reproducer reading a file whose indirect block contains an invalid block pointer: on the unfixed kernel, check_bh_leaked() detected indirect block 5's buffer_head leaked and pinned in page cache (indirect_page5_leaked=1); whereas with this fix applied, brelse(bh) is called on error and the buffer_head is cleanly evicted (indirect_page5_leaked=0). Fixes: 5d026c724220 ("fs: initial qnx6fs addition") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Hui Peng --- Changes in v4: - Rebased cleanly onto upstream mainline commit 62f4c998b297. - Added QEMU test procedure and verification details in commit message body. fs/qnx6/inode.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/qnx6/inode.c b/fs/qnx6/inode.c index c999dcfdc477..080f7698a5e0 100644 --- a/fs/qnx6/inode.c +++ b/fs/qnx6/inode.c @@ -144,8 +144,10 @@ static unsigned qnx6_block_map(struct inode *inode, unsigned no) levelptr = (no >> bitdelta) & mask; ptr = ((__fs32 *)bh->b_data)[levelptr]; - if (!qnx6_check_blockptr(ptr)) - return 0; + if (!qnx6_check_blockptr(ptr)) { + brelse(bh); + return 0; + } block = qnx6_get_devblock(s, ptr); brelse(bh); -- 2.55.0.1082.g2b9226bbc0-goog In qnx6_fill_super(), when active superblock selection chooses sb1 or sb2, it calls brelse() on the inactive buffer head without setting its pointer to NULL. If an error occurs later (e.g. Inode.levels validation failure), label out: calls brelse(bh1) and brelse(bh2), resulting in a double free of the inactive buffer head. Additionally, if mmi_fs path is used, sbi->sb_buf is set by qnx6_mmi_fill_super(), but on failure after mmi_success:, brelse(sbi->sb_buf) is never called. Set bh2 = NULL or bh1 = NULL after releasing the inactive buffer head, and release sbi->sb_buf on error paths at label out:. Tested in QEMU against tip of mainline commit 62f4c998b297 ("Merge tag 'parisc-for-7.3-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux") using a loop-device reproducer mounting with mmi_fs option: on the unfixed kernel, failure after mmi_success leaked sbi->sb_buf (sb_buf_page0_leaked=1); whereas with this fix applied, sbi->sb_buf is released on error (sb_buf_page0_leaked=0). Fixes: 5d026c724220 ("fs: initial qnx6fs addition") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Hui Peng --- Changes in v4: - Rebased cleanly onto upstream mainline commit 62f4c998b297. - Added QEMU test procedure and verification details in commit message body. fs/qnx6/inode.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fs/qnx6/inode.c b/fs/qnx6/inode.c index 080f7698a5e0..d425daee090c 100644 --- a/fs/qnx6/inode.c +++ b/fs/qnx6/inode.c @@ -399,6 +399,7 @@ static int qnx6_fill_super(struct super_block *s, struct fs_context *fc) sbi->sb_buf = bh1; sbi->sb = (struct qnx6_super_block *)bh1->b_data; brelse(bh2); + bh2 = NULL; pr_info("superblock #1 active\n"); } else { /* superblock #2 active */ @@ -405,5 +406,6 @@ static int qnx6_fill_super(struct super_block *s, struct fs_context *fc) sbi->sb = (struct qnx6_super_block *)bh2->b_data; brelse(bh1); + bh1 = NULL; pr_info("superblock #2 active\n"); } mmi_success: @@ -467,6 +469,10 @@ static int qnx6_fill_super(struct super_block *s, struct fs_context *fc) out1: iput(sbi->inodes); out: + if (sbi && sbi->sb_buf && !bh1 && !bh2) { + brelse(sbi->sb_buf); + sbi->sb_buf = NULL; + } brelse(bh1); brelse(bh2); outnobh: -- 2.55.0.1082.g2b9226bbc0-goog In qnx6_mmi_fill_super(), when selecting active superblock 1 or 2, brelse(bh2) or brelse(bh1) is called without resetting bh2 or bh1 to NULL. If memory allocation for qsb fails or a checksum error occurs later, label out: calls brelse(bh1) and brelse(bh2), causing a double free. Clear bh2 = NULL and bh1 = NULL after releasing the inactive buffer head in qnx6_mmi_fill_super(). Tested in QEMU against tip of mainline commit 62f4c998b297 ("Merge tag 'parisc-for-7.3-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux") using a loop-device reproducer mounting an MMI QNX6 filesystem: verified that inactive superblock pointers are cleared on selection, avoiding double brelse() when error handling unwinds. Fixes: 5d026c724220 ("fs: initial qnx6fs addition") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Hui Peng --- Changes in v4: - Rebased cleanly onto upstream mainline commit 62f4c998b297. - Added QEMU test procedure and verification details in commit message body. fs/qnx6/super_mmi.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/qnx6/super_mmi.c b/fs/qnx6/super_mmi.c index b8afb6f388b2..797d8c238bbb 100644 --- a/fs/qnx6/super_mmi.c +++ b/fs/qnx6/super_mmi.c @@ -118,6 +118,7 @@ struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent) sbi->sb_buf = bh1; sbi->sb = (struct qnx6_super_block *)bh1->b_data; brelse(bh2); + bh2 = NULL; pr_info("superblock #1 active\n"); } else { /* superblock #2 active */ @@ -130,6 +131,7 @@ struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent) sbi->sb_buf = bh2; sbi->sb = (struct qnx6_super_block *)bh2->b_data; brelse(bh1); + bh1 = NULL; pr_info("superblock #2 active\n"); } kfree(qsb); -- 2.55.0.1082.g2b9226bbc0-goog In qnx6_mmi_fill_super(), if sb1->sb_magic does not match QNX6_SUPER_MAGIC, the goto out error path was placed inside the if (!silent) block. If silent mount is requested (silent != 0), execution fell through to crc32_be() on invalid superblock data. Move goto out outside the if (!silent) block so qnx6_mmi_fill_super() always aborts mount when superblock magic does not match. Tested in QEMU against tip of mainline commit 62f4c998b297 ("Merge tag 'parisc-for-7.3-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux") using a loop-device reproducer mounting an image with invalid sb_magic=0xdeadbeef and MS_SILENT: on the unfixed kernel, mount succeeded (rc=0); whereas with this fix applied, qnx6_mmi_fill_super() rejects the corrupted superblock magic cleanly (rc=-1 errno=22 EINVAL). Fixes: 5d026c724220 ("fs: initial qnx6fs addition") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Hui Peng --- Changes in v4: - Rebased cleanly onto upstream mainline commit 62f4c998b297. - Added QEMU test procedure and verification details in commit message body. fs/qnx6/super_mmi.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fs/qnx6/super_mmi.c b/fs/qnx6/super_mmi.c index 797d8c238bbb..5f62df30467b 100644 --- a/fs/qnx6/super_mmi.c +++ b/fs/qnx6/super_mmi.c @@ -51,10 +51,9 @@ struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent) sb1 = (struct qnx6_mmi_super_block *)bh1->b_data; sbi = QNX6_SB(s); if (fs32_to_cpu(sbi, sb1->sb_magic) != QNX6_SUPER_MAGIC) { - if (!silent) { + if (!silent) pr_err("wrong signature (magic) in superblock #1.\n"); - goto out; - } + goto out; } /* checksum check - start at byte 8 and end at byte 512 */ -- 2.55.0.1082.g2b9226bbc0-goog In qnx6_mmi_fill_super(), sb1->sb_blocksize is read from disk and used as a divisor in QNX6_SUPERBLOCK_AREA / fs32_to_cpu(sbi, sb1->sb_blocksize). If sb1->sb_blocksize is 0, it causes a division by zero in kernel space. Validate sb1->sb_blocksize is non-zero before performing the division and abort mount if it is zero. Tested in QEMU against tip of mainline commit 62f4c998b297 ("Merge tag 'parisc-for-7.3-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux") using a loop-device reproducer mounting an image with sb_blocksize = 0: verified that mount is rejected cleanly with -EINVAL (rc=-1 errno=22) before division occurs. Fixes: 5d026c724220 ("fs: initial qnx6fs addition") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Hui Peng --- Changes in v4: - Rebased cleanly onto upstream mainline commit 62f4c998b297. - Added QEMU test procedure and verification details in commit message body. fs/qnx6/super_mmi.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/fs/qnx6/super_mmi.c b/fs/qnx6/super_mmi.c index 5f62df30467b..5d41f028bf80 100644 --- a/fs/qnx6/super_mmi.c +++ b/fs/qnx6/super_mmi.c @@ -63,6 +63,11 @@ struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent) goto out; } + if (!fs32_to_cpu(sbi, sb1->sb_blocksize)) { + pr_err("invalid blocksize 0 in superblock #1\n"); + goto out; + } + /* calculate second superblock blocknumber */ offset = fs32_to_cpu(sbi, sb1->sb_num_blocks) + QNX6_SUPERBLOCK_AREA / fs32_to_cpu(sbi, sb1->sb_blocksize); -- 2.55.0.1082.g2b9226bbc0-goog