• Baokun Li's avatar
    ext4: fix use-after-free in ext4_ext_shift_extents · f6b1a1cf
    Baokun Li authored
    If the starting position of our insert range happens to be in the hole
    between the two ext4_extent_idx, because the lblk of the ext4_extent in
    the previous ext4_extent_idx is always less than the start, which leads
    to the "extent" variable access across the boundary, the following UAF is
    triggered:
    ==================================================================
    BUG: KASAN: use-after-free in ext4_ext_shift_extents+0x257/0x790
    Read of size 4 at addr ffff88819807a008 by task fallocate/8010
    CPU: 3 PID: 8010 Comm: fallocate Tainted: G            E     5.10.0+ #492
    Call Trace:
     dump_stack+0x7d/0xa3
     print_address_description.constprop.0+0x1e/0x220
     kasan_report.cold+0x67/0x7f
     ext4_ext_shift_extents+0x257/0x790
     ext4_insert_range+0x5b6/0x700
     ext4_fallocate+0x39e/0x3d0
     vfs_fallocate+0x26f/0x470
     ksys_fallocate+0x3a/0x70
     __x64_sys_fallocate+0x4f/0x60
     do_syscall_64+0x33/0x40
     entry_SYSCALL_64_after_hwframe+0x44/0xa9
    ==================================================================
    
    For right shifts, we can divide them into the following situations:
    
    1. When the first ee_block of ext4_extent_idx is greater than or equal to
       start, make right shifts directly from the first ee_block.
        1) If it is greater than start, we need to continue searching in the
           previous ext4_extent_idx.
        2) If it is equal to start, we can exit the loop (iterator=NULL).
    
    2. When the first ee_block of ext4_extent_idx is less than start, then
       traverse from the last extent to find the first extent whose ee_block
       is less than start.
        1) If extent is still the last extent after traversal, it means that
           the last ee_block of ext4_extent_idx is less than start, that is,
           start is located in the hole between idx and (idx+1), so we can
           exit the loop directly (break) without right shifts.
        2) Otherwise, make right shifts at the corresponding position of the
           found extent, and then exit the loop (iterator=NULL).
    
    Fixes: 331573fe ("ext4: Add support FALLOC_FL_INSERT_RANGE for fallocate")
    Cc: stable@vger.kernel.org # v4.2+
    Signed-off-by: default avatarZhihao Cheng <chengzhihao1@huawei.com>
    Signed-off-by: default avatarBaokun Li <libaokun1@huawei.com>
    Link: https://lore.kernel.org/r/20220922120434.1294789-1-libaokun1@huawei.comSigned-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
    f6b1a1cf
extents.c 166 KB