fs/qnx6: fix buffer head leaks, double free, and inode validation
Subsystems (3)
v5 cover fs/qnx6: fix buffer head leaks, double free, and inode validation
This series addresses buffer head memory leaks, uninitialized pointer reads, out-of-bounds array access, and superblock validation issues in the qnx6 filesystem driver.
All patches have been ported to the latest tip of mainline (551c722f4080) and verified in QEMU KVM with CONFIG_KASAN=y and UBSAN enabled.
Changes in v5:
- 1/6: Restored commit description to accurately specify UBSAN shift-out-of-bounds reports at both shifts in qnx6_block_map() as requested by Matthias Goergens.
- 3/6: Used qs->sb_buf instead of sbi->sb_buf at label out: in qnx6_fill_super() to prevent reading uninitialized sbi when mounting mmi_fs images, fixing a general protection fault reported by Matthias.
- 4/6: Updated commit description to state that clearing sb_buf after brelse() is defensive cleanup.
- Collected Tested-by and Reviewed-by tags from Matthias Goergens.
Hui Peng (6):
qnx6: validate di_filelevels in qnx6_iget() before accessing level
pointers
qnx6: release bh on error path in qnx6_block_map()
qnx6: release bh on error path in qnx6_fill_super()
qnx6: clear sb_buf after brelse in qnx6_mmi_fill_super()
qnx6: abort mount on superblock magic mismatch when silent is set in
qnx6_mmi_fill_super()
qnx6: validate sb_blocksize before dividing in qnx6_mmi_fill_super()
fs/qnx6/inode.c | 18 +++++++++++++++--- fs/qnx6/super_mmi.c | 7 ++++++- 2 files changed, 21 insertions(+), 4 deletions(-)
Signature · 1 line
2.47.3
PATCH v5 1/6 qnx6: validate di_filelevels in qnx6_iget() before accessing level pointers
In qnx6_iget(), raw_inode->di_filelevels is loaded directly from disk without checking if it exceeds QNX6_PTR_MAX_LEVELS (3). When di_filelevels exceeds 3 (for example, 6 or 255), qnx6_block_map() triggers UBSAN shift-out-of-bounds reports at both shifts during indirect block tree traversal.
Validate raw_inode->di_filelevels <= QNX6_PTR_MAX_LEVELS in qnx6_iget() and return -EIO on invalid values.
---
Changes in v5:
- Restored commit description to accurately specify UBSAN shift-out-of-bounds reports at both shifts in qnx6_block_map() per Matthias Goergens.
- Added Tested-by: Matthias Goergens.
fs/qnx6/inode.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/qnx6/inode.c +5 -0
@@ -543,6 +543,11 @@ struct inode *qnx6_iget(struct super_block *sb, unsigned int ino) raw_inode =qnx6_raw_inode (sb, ino, bh); inode->i_mode =fs16_to_cpu (sbi, raw_inode->di_mode); +if (raw_inode->di_filelevels >QNX6_PTR_MAX_LEVELS ) { +pr_err ("invalid inode levels %d (max %d)\n" , + raw_inode->di_filelevels,QNX6_PTR_MAX_LEVELS ); + goto bad_inode; + }i_uid_write (inode,fs32_to_cpu (sbi, raw_inode->di_uid));i_gid_write (inode,fs32_to_cpu (sbi, raw_inode->di_gid));set_nlink (inode,fs32_to_cpu (sbi, raw_inode->di_nlink));
Signature · 1 line
2.47.3
PATCH v5 2/6 qnx6: release bh on error path in qnx6_block_map()
In reply to Hui PengIn qnx6_block_map(), when sb_bread() fails during indirect block tree traversal, the function returns 0 without calling brelse(bh), leaking the previously fetched buffer head bh.
Call brelse(bh) before returning 0 on error paths in qnx6_block_map().
--- fs/qnx6/inode.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/qnx6/inode.c +3 -1
@@ -242,8 +242,10 @@ unsigned long qnx6_block_map(struct inode *inode, unsigned int iblock) block =fs32_to_cpu (sbi, *ptr); bh =sb_bread (inode->i_sb, block); -if (!bh) +if (!bh) { +brelse (bh);return 0 ; + } } idx = (iblock >> (level * sbi->s_ptrbits)) & (sbi->s_ptrbits -1 );
Signature · 1 line
2.47.3
PATCH v5 3/6 qnx6: release bh on error path in qnx6_fill_super()
In reply to Hui PengIn qnx6_fill_super(), when indirect block tree level validation fails at mmi_success, the code jumps to label out: before sbi is assigned. Reading sbi->sb_buf at label out: reads an uninitialized stack pointer sbi, causing a general protection fault under CONFIG_INIT_STACK_ALL_PATTERN and leaking qs->sb_buf.
Use qs->sb_buf instead of sbi->sb_buf at label out: to safely release qs->sb_buf when mounting mmi_fs images.
---
Changes in v5:
- Use qs->sb_buf instead of sbi->sb_buf at label out: to prevent reading uninitialized sbi when mounting mmi_fs images, as pointed out by Matthias Goergens.
fs/qnx6/inode.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/qnx6/inode.c +4 -0
@@ -465,6 +465,10 @@ static int qnx6_fill_super(struct super_block *s, struct fs_context *fc) out1:iput (sbi->inodes); out: +if (qs->sb_buf && !bh1 && !bh2) { +brelse (qs->sb_buf); + qs->sb_buf =NULL ; + }brelse (bh1);brelse (bh2); outnobh:
Signature · 1 line
2.47.3
PATCH v5 4/6 qnx6: clear sb_buf after brelse in qnx6_mmi_fill_super()
In reply to Hui PengIn qnx6_mmi_fill_super(), when superblock verification fails, brelse() is called on unselected candidate superblocks, but their buffer pointers are left set in local variables.
Defensively clear sb_buf pointers after calling brelse() in qnx6_mmi_fill_super().
---
Changes in v5:
- Updated commit description to clarify that clearing sb_buf pointers after brelse() is a defensive cleanup measure per Matthias Goergens.
fs/qnx6/super_mmi.c | 3 +++ 1 file changed, 3 insertions(+)
fs/qnx6/super_mmi.c +1 -0
@@ -102,9 +102,12 @@ struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent)if (fs64_to_cpu (sbi, sb1->sb_serial) >=fs64_to_cpu (sbi, sb2->sb_serial)) {/* superblock #1 active */ sbi->sb_buf = bh1; sbi->sb = sb1;brelse (bh2); + bh2 =NULL ; }else {/* superblock #2 active */ sbi->sb_buf = bh2;
sbi->sb = sb2;
brelse(bh1);
+ bh1 = NULL;
}
Signature · 1 line
2.47.3
PATCH v5 5/6 qnx6: abort mount on superblock magic mismatch when silent is set in qnx6_mmi_fill_super()
In reply to Hui PengIn qnx6_mmi_fill_super(), when checking superblock magic for candidate superblocks, if the magic does not match QNX6_SUPER_MAGIC and silent is non-zero, the error message print is skipped but execution continues to subsequent checks, resulting in invalid superblock processing.
Jump to error labels out1/out2 when superblock magic mismatch occurs, regardless of the silent parameter.
--- fs/qnx6/super_mmi.c | 2 ++ 1 file changed, 2 insertions(+)
fs/qnx6/super_mmi.c +2 -0
@@ -62,6 +62,7 @@ struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent)if (fs32_to_cpu (sbi, sb1->sb_magic) !=QNX6_SUPER_MAGIC ) {if (!silent)pr_err ("wrong signature (magic) in superblock #1.\n" ); + goto out1; }/* checksum check - start at byte 8 and end at byte 512 */
@@ -83,6 +84,7 @@ struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent)if (fs32_to_cpu (sbi, sb2->sb_magic) !=QNX6_SUPER_MAGIC ) {if (!silent)pr_err ("wrong signature (magic) in superblock #2.\n" ); + goto out2; }/* checksum check - start at byte 8 and end at byte 512 */
Signature · 1 line
2.47.3
PATCH v5 6/6 qnx6: validate sb_blocksize before dividing in qnx6_mmi_fill_super()
In reply to Hui PengIn qnx6_mmi_fill_super(), sb1->sb_blocksize is loaded directly from disk without checking for zero before calculating:
QNX6_BOOTBLOCK_SIZE / fs32_to_cpu(sbi, sb1->sb_blocksize)
If sb_blocksize is zero on a corrupted MMI filesystem image, this division causes a divide-by-zero error in kernel space.
Validate sb1->sb_blocksize before dividing in qnx6_mmi_fill_super().
--- fs/qnx6/super_mmi.c | 2 ++ 1 file changed, 2 insertions(+)
fs/qnx6/super_mmi.c +2 -0
@@ -48,6 +48,8 @@ struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent) sb1 = (struct qnx6_super_block *)bh1->b_data;/* calculate second superblock blocknumber */ +if (!fs32_to_cpu (sbi, sb1->sb_blocksize)) + goto out1; offset =fs32_to_cpu (sbi, sb1->sb_num_blocks) + (QNX6_BOOTBLOCK_SIZE /fs32_to_cpu (sbi, sb1->sb_blocksize));
Signature · 1 line
2.47.3
Hi Hui,
Thanks for keeping at this series, and for taking the 1/6 wording on board. I think something went wrong when v5 was generated, though: it does not apply to mainline 551c722f4080, the base your cover letter names. git am stops at 1/6, and git apply rejects 1/6, 2/6, 5/6 and 6/6 on their own (4/6 is reported as corrupt). The context lines refer to code that is not in fs/qnx6, such as a bad_inode label in qnx6_iget() and labels out1 and out2 in super_mmi.c, so it looks as if it came from a different tree than v4, which applied cleanly. Could you regenerate it from v4 on a clean checkout of mainline?
While comparing it with v4 I also noticed a few things that may have been lost along the way:
2/6: the brelse(bh) is now inside if (!bh), where bh is NULL, so it has no effect. The leak v4 fixed is on the qnx6_check_blockptr() failure path.
3/6: the bh1 = NULL and bh2 = NULL assignments from v4 are gone, so the double brelse() comes back. With only this 3/6 applied to mainline (KASAN and UBSAN, under QEMU), the double-brelse image gives the same two "Trying to free free buffer" warnings as unpatched mainline; v4 gives none. The qs->sb_buf release itself looks right.
1/6: the text says QNX6_PTR_MAX_LEVELS is 3, but include/linux/qnx6_fs.h defines it as 5, so inodes with 4 or 5 levels are valid. The check uses the macro, so only the text needs fixing.
3377755f1064 is not in mainline. v4's 5d026c724220 ("fs: initial qnx6fs addition") added both inode.c and super_mmi.c.
Could you also drop my Tested-by and Reviewed-by from v5? I haven't been able to test it as posted, and 2/6 and 5/6 have changed since I reviewed them. I'll gladly re-test the next version and give them again.
Thanks,
Matthias