Skip to content

Commit bf49527

Browse files
chaseyugregkh
authored andcommitted
f2fs: fix to avoid accessing uninitialized curseg
commit 986c50f upstream. syzbot reports a f2fs bug as below: F2FS-fs (loop3): Stopped filesystem due to reason: 7 kworker/u8:7: attempt to access beyond end of device BUG: unable to handle page fault for address: ffffed1604ea3dfa RIP: 0010:get_ckpt_valid_blocks fs/f2fs/segment.h:361 [inline] RIP: 0010:has_curseg_enough_space fs/f2fs/segment.h:570 [inline] RIP: 0010:__get_secs_required fs/f2fs/segment.h:620 [inline] RIP: 0010:has_not_enough_free_secs fs/f2fs/segment.h:633 [inline] RIP: 0010:has_enough_free_secs+0x575/0x1660 fs/f2fs/segment.h:649 <TASK> f2fs_is_checkpoint_ready fs/f2fs/segment.h:671 [inline] f2fs_write_inode+0x425/0x540 fs/f2fs/inode.c:791 write_inode fs/fs-writeback.c:1525 [inline] __writeback_single_inode+0x708/0x10d0 fs/fs-writeback.c:1745 writeback_sb_inodes+0x820/0x1360 fs/fs-writeback.c:1976 wb_writeback+0x413/0xb80 fs/fs-writeback.c:2156 wb_do_writeback fs/fs-writeback.c:2303 [inline] wb_workfn+0x410/0x1080 fs/fs-writeback.c:2343 process_one_work kernel/workqueue.c:3236 [inline] process_scheduled_works+0xa66/0x1840 kernel/workqueue.c:3317 worker_thread+0x870/0xd30 kernel/workqueue.c:3398 kthread+0x7a9/0x920 kernel/kthread.c:464 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:148 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 Commit 8b10d36 ("f2fs: introduce FAULT_NO_SEGMENT") allows to trigger no free segment fault in allocator, then it will update curseg->segno to NULL_SEGNO, though, CP_ERROR_FLAG has been set, f2fs_write_inode() missed to check the flag, and access invalid curseg->segno directly in below call path, then resulting in panic: - f2fs_write_inode - f2fs_is_checkpoint_ready - has_enough_free_secs - has_not_enough_free_secs - __get_secs_required - has_curseg_enough_space - get_ckpt_valid_blocks : access invalid curseg->segno To avoid this issue, let's: - check CP_ERROR_FLAG flag in prior to f2fs_is_checkpoint_ready() in f2fs_write_inode(). - in has_curseg_enough_space(), save curseg->segno into a temp variable, and verify its validation before use. Fixes: 8b10d36 ("f2fs: introduce FAULT_NO_SEGMENT") Reported-by: [email protected] Closes: https://lore.kernel.org/all/[email protected] Signed-off-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]> Signed-off-by: Alva Lan <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent db75848 commit bf49527

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

fs/f2fs/inode.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,13 @@ int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
777777
!is_inode_flag_set(inode, FI_DIRTY_INODE))
778778
return 0;
779779

780+
/*
781+
* no need to update inode page, ultimately f2fs_evict_inode() will
782+
* clear dirty status of inode.
783+
*/
784+
if (f2fs_cp_error(sbi))
785+
return -EIO;
786+
780787
if (!f2fs_is_checkpoint_ready(sbi)) {
781788
f2fs_mark_inode_dirty_sync(inode, true);
782789
return -ENOSPC;

fs/f2fs/segment.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,16 @@ static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi,
562562
unsigned int node_blocks, unsigned int data_blocks,
563563
unsigned int dent_blocks)
564564
{
565-
566565
unsigned int segno, left_blocks, blocks;
567566
int i;
568567

569568
/* check current data/node sections in the worst case. */
570569
for (i = CURSEG_HOT_DATA; i < NR_PERSISTENT_LOG; i++) {
571570
segno = CURSEG_I(sbi, i)->segno;
571+
572+
if (unlikely(segno == NULL_SEGNO))
573+
return false;
574+
572575
left_blocks = CAP_BLKS_PER_SEC(sbi) -
573576
get_ckpt_valid_blocks(sbi, segno, true);
574577

@@ -579,6 +582,10 @@ static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi,
579582

580583
/* check current data section for dentry blocks. */
581584
segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
585+
586+
if (unlikely(segno == NULL_SEGNO))
587+
return false;
588+
582589
left_blocks = CAP_BLKS_PER_SEC(sbi) -
583590
get_ckpt_valid_blocks(sbi, segno, true);
584591
if (dent_blocks > left_blocks)

0 commit comments

Comments
 (0)