• Qu Wenruo's avatar
    btrfs: extent_io: do extra check for extent buffer read write functions · f98b6215
    Qu Wenruo authored
    Although we have start, len check for extent buffer reader/write (e.g.
    read_extent_buffer()), these checks have limitations:
    
    - No overflow check
      Values like start = 1024 len = -1024 can still pass the basic
       (start + len) > eb->len check.
    
    - Checks are not consistent
      For read_extent_buffer() we only check (start + len) against eb->len.
      While for memcmp_extent_buffer() we also check start against eb->len.
    
    - Different error reporting mechanism
      We use WARN() in read_extent_buffer() but BUG() in
      memcpy_extent_buffer().
    
    - Still modify memory if the request is obviously wrong
      In read_extent_buffer() even we find (start + len) > eb->len, we still
      call memset(dst, 0, len), which can easily cause memory access error
      if start + len overflows.
    
    To address above problems, this patch creates a new common function to
    check such access, check_eb_range().
    
    - Add overflow check
      This function checks start, start + len against eb->len and overflow
      check.
    
    - Unified checks
    
    - Unified error reports
      Will call WARN() if CONFIG_BTRFS_DEBUG is configured.
      And also do btrfs_warn() message for non-debug build.
    
    - Exit ASAP if check fails
      No more possible memory corruption.
    
    - Add extra comment for @start @len used in those functions as it's
      sometimes confused with the logical addressing instead of a range
      inside the eb space
    
    Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=202817
    [ Inspired by above report, the report itself is already addressed ]
    Reviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
    Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
    Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
    [ use check_add_overflow ]
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    f98b6215
extent_io.c 158 KB