[PATCH] jfs: Fix NULL pointer dereference in diFree

From: Wang Hai
Date: Thu Aug 26 2021 - 07:16:54 EST


I got a NULL pointer dereference report when doing fuzz test:

jfs_mount: diMount failed w/rc = -5
BUG: kernel NULL pointer dereference, address: 0000000000000004
[...]
RIP: 0010:diFree+0x5d/0xc70
[...]
jfs_evict_inode+0x136/0x180
jfs_write_inode+0xc0/0xc0
evict+0x102/0x1f0
iput+0x220/0x2e0
diFreeSpecial+0x44/0x70
jfs_mount+0x1b1/0x460
jfs_fill_super+0x1a5/0x460
mount_bdev+0x1d7/0x220
jfs_do_mount+0x39/0x50
legacy_get_tree+0x2f/0x80
vfs_get_tree+0x2f/0x100
path_mount+0x8e8/0xc7
do_mount+0x9e/0xc0
__x64_sys_mount+0xc5/0x140
do_syscall_64+0x34/0xb0
entry_SYSCALL_64_after_hwframe+0x44/0xae

Similar to commit 9d574f985fe3 ("jfs: fix GPF in diFree"),
diFreeSpecial(ipimap)->..->diFree() will be called if diMount(ipimap)
fails in jfs_mount(). This will result in a null pointer reference in
diFree() because the imap is not allocated in diFreeSpecial().

int diFree(struct inode *ip)
{
...
struct inomap *imap = JFS_IP(ipimap)->i_imap; // imap == NULL
...
if (iagno >= imap->im_nextiag) { // null pointer reference
...
}

If diFreeSpecial(ipimap) in jfs_mount() fails, there is no need to
call diFree(), because diAlloc() is not called in the mount process.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: Hulk Robot <hulkci@xxxxxxxxxx>
Signed-off-by: Wang Hai <wanghai38@xxxxxxxxxx>
---
fs/jfs/inode.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/jfs/inode.c b/fs/jfs/inode.c
index 57ab424c05ff..e8303d47a4a4 100644
--- a/fs/jfs/inode.c
+++ b/fs/jfs/inode.c
@@ -146,12 +146,14 @@ void jfs_evict_inode(struct inode *inode)
dquot_initialize(inode);

if (JFS_IP(inode)->fileset == FILESYSTEM_I) {
+ struct inode *ipimap = JFS_SBI(inode->i_sb)->ipimap;
+
truncate_inode_pages_final(&inode->i_data);

if (test_cflag(COMMIT_Freewmap, inode))
jfs_free_zero_link(inode);

- if (JFS_SBI(inode->i_sb)->ipimap)
+ if (ipimap && JFS_IP(ipimap)->i_imap)
diFree(inode);

/*
--
2.17.1