acrn-kernel/fs/f2fs
Chao Yu ebe83e9bb8 f2fs: fix to avoid NULL pointer dereference f2fs_write_end_io()
[ Upstream commit d8189834d4 ]

butt3rflyh4ck reports a bug as below:

When a thread always calls F2FS_IOC_RESIZE_FS to resize fs, if resize fs is
failed, f2fs kernel thread would invoke callback function to update f2fs io
info, it would call  f2fs_write_end_io and may trigger null-ptr-deref in
NODE_MAPPING.

general protection fault, probably for non-canonical address
KASAN: null-ptr-deref in range [0x0000000000000030-0x0000000000000037]
RIP: 0010:NODE_MAPPING fs/f2fs/f2fs.h:1972 [inline]
RIP: 0010:f2fs_write_end_io+0x727/0x1050 fs/f2fs/data.c:370
 <TASK>
 bio_endio+0x5af/0x6c0 block/bio.c:1608
 req_bio_endio block/blk-mq.c:761 [inline]
 blk_update_request+0x5cc/0x1690 block/blk-mq.c:906
 blk_mq_end_request+0x59/0x4c0 block/blk-mq.c:1023
 lo_complete_rq+0x1c6/0x280 drivers/block/loop.c:370
 blk_complete_reqs+0xad/0xe0 block/blk-mq.c:1101
 __do_softirq+0x1d4/0x8ef kernel/softirq.c:571
 run_ksoftirqd kernel/softirq.c:939 [inline]
 run_ksoftirqd+0x31/0x60 kernel/softirq.c:931
 smpboot_thread_fn+0x659/0x9e0 kernel/smpboot.c:164
 kthread+0x33e/0x440 kernel/kthread.c:379
 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308

The root cause is below race case can cause leaving dirty metadata
in f2fs after filesystem is remount as ro:

Thread A				Thread B
- f2fs_ioc_resize_fs
 - f2fs_readonly   --- return false
 - f2fs_resize_fs
					- f2fs_remount
					 - write_checkpoint
					 - set f2fs as ro
  - free_segment_range
   - update meta_inode's data

Then, if f2fs_put_super()  fails to write_checkpoint due to readonly
status, and meta_inode's dirty data will be writebacked after node_inode
is put, finally, f2fs_write_end_io will access NULL pointer on
sbi->node_inode.

Thread A				IRQ context
- f2fs_put_super
 - write_checkpoint fails
 - iput(node_inode)
 - node_inode = NULL
 - iput(meta_inode)
  - write_inode_now
   - f2fs_write_meta_page
					- f2fs_write_end_io
					 - NODE_MAPPING(sbi)
					 : access NULL pointer on node_inode

Fixes: b4b10061ef ("f2fs: refactor resize_fs to avoid meta updates in progress")
Reported-by: butt3rflyh4ck <butterflyhuangxx@gmail.com>
Closes: https://lore.kernel.org/r/1684480657-2375-1-git-send-email-yangtiezhu@loongson.cn
Tested-by: butt3rflyh4ck <butterflyhuangxx@gmail.com>
Signed-off-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2023-07-19 16:21:55 +02:00
..
Kconfig
Makefile
acl.c
acl.h
checkpoint.c f2fs: fix to drop all dirty pages during umount() if cp_error is set 2023-05-24 17:32:38 +01:00
compress.c f2fs: fix potential deadlock due to unpaired node_write lock use 2023-07-19 16:21:55 +02:00
data.c f2fs: fix potential deadlock due to unpaired node_write lock use 2023-07-19 16:21:55 +02:00
debug.c f2fs: refactor extent_cache to support for read and more 2023-05-17 11:53:52 +02:00
dir.c
extent_cache.c f2fs: inode: fix to do sanity check on extent cache correctly 2023-05-17 11:53:59 +02:00
f2fs.h f2fs: fix to avoid NULL pointer dereference f2fs_write_end_io() 2023-07-19 16:21:55 +02:00
file.c f2fs: fix to avoid NULL pointer dereference f2fs_write_end_io() 2023-07-19 16:21:55 +02:00
gc.c f2fs: fix to avoid NULL pointer dereference f2fs_write_end_io() 2023-07-19 16:21:55 +02:00
gc.h f2fs: Fix system crash due to lack of free space in LFS 2023-05-24 17:32:38 +01:00
hash.c
inline.c
inode.c f2fs: inode: fix to do sanity check on extent cache correctly 2023-05-17 11:53:59 +02:00
iostat.c
iostat.h
namei.c f2fs: allocate the extent_cache by default 2023-05-17 11:53:52 +02:00
node.c f2fs: refactor extent_cache to support for read and more 2023-05-17 11:53:52 +02:00
node.h f2fs: specify extent cache for read explicitly 2023-05-17 11:53:52 +02:00
recovery.c
segment.c f2fs: factor out victim_entry usage from general rb_tree use 2023-05-17 11:53:53 +02:00
segment.h f2fs: Fix system crash due to lack of free space in LFS 2023-05-24 17:32:38 +01:00
shrinker.c f2fs: refactor extent_cache to support for read and more 2023-05-17 11:53:52 +02:00
super.c f2fs: fix to check readonly condition correctly 2023-05-24 17:32:38 +01:00
sysfs.c f2fs: fix iostat lock protection 2023-05-11 23:03:20 +09:00
verity.c
xattr.c
xattr.h