• Chao Yu's avatar
    f2fs: fix to cover read extent cache access with lock · d7409b05
    Chao Yu authored
    syzbot reports a f2fs bug as below:
    
    BUG: KASAN: slab-use-after-free in sanity_check_extent_cache+0x370/0x410 fs/f2fs/extent_cache.c:46
    Read of size 4 at addr ffff8880739ab220 by task syz-executor200/5097
    
    CPU: 0 PID: 5097 Comm: syz-executor200 Not tainted 6.9.0-rc6-syzkaller #0
    Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/27/2024
    Call Trace:
     <TASK>
     __dump_stack lib/dump_stack.c:88 [inline]
     dump_stack_lvl+0x241/0x360 lib/dump_stack.c:114
     print_address_description mm/kasan/report.c:377 [inline]
     print_report+0x169/0x550 mm/kasan/report.c:488
     kasan_report+0x143/0x180 mm/kasan/report.c:601
     sanity_check_extent_cache+0x370/0x410 fs/f2fs/extent_cache.c:46
     do_read_inode fs/f2fs/inode.c:509 [inline]
     f2fs_iget+0x33e1/0x46e0 fs/f2fs/inode.c:560
     f2fs_nfs_get_inode+0x74/0x100 fs/f2fs/super.c:3237
     generic_fh_to_dentry+0x9f/0xf0 fs/libfs.c:1413
     exportfs_decode_fh_raw+0x152/0x5f0 fs/exportfs/expfs.c:444
     exportfs_decode_fh+0x3c/0x80 fs/exportfs/expfs.c:584
     do_handle_to_path fs/fhandle.c:155 [inline]
     handle_to_path fs/fhandle.c:210 [inline]
     do_handle_open+0x495/0x650 fs/fhandle.c:226
     do_syscall_x64 arch/x86/entry/common.c:52 [inline]
     do_syscall_64+0xf5/0x240 arch/x86/entry/common.c:83
     entry_SYSCALL_64_after_hwframe+0x77/0x7f
    
    We missed to cover sanity_check_extent_cache() w/ extent cache lock,
    so, below race case may happen, result in use after free issue.
    
    - f2fs_iget
     - do_read_inode
      - f2fs_init_read_extent_tree
      : add largest extent entry in to cache
    					- shrink
    					 - f2fs_shrink_read_extent_tree
    					  - __shrink_extent_tree
    					   - __detach_extent_node
    					   : drop largest extent entry
      - sanity_check_extent_cache
      : access et->largest w/o lock
    
    let's refactor sanity_check_extent_cache() to avoid extent cache access
    and call it before f2fs_init_read_extent_tree() to fix this issue.
    
    Reported-by: syzbot+74ebe2104433e9dc610d@syzkaller.appspotmail.com
    Closes: https://lore.kernel.org/linux-f2fs-devel/00000000000009beea061740a531@google.comSigned-off-by: default avatarChao Yu <chao@kernel.org>
    Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
    d7409b05
inode.c 27.8 KB