linux/fs/ext4
Baokun Li 13df4d44a3 ext4: fix slab-out-of-bounds in ext4_mb_find_good_group_avg_frag_lists()
We can trigger a slab-out-of-bounds with the following commands:

    mkfs.ext4 -F /dev/$disk 10G
    mount /dev/$disk /tmp/test
    echo 2147483647 > /sys/fs/ext4/$disk/mb_group_prealloc
    echo test > /tmp/test/file && sync

==================================================================
BUG: KASAN: slab-out-of-bounds in ext4_mb_find_good_group_avg_frag_lists+0x8a/0x200 [ext4]
Read of size 8 at addr ffff888121b9d0f0 by task kworker/u2:0/11
CPU: 0 PID: 11 Comm: kworker/u2:0 Tainted: GL 6.7.0-next-20240118 #521
Call Trace:
 dump_stack_lvl+0x2c/0x50
 kasan_report+0xb6/0xf0
 ext4_mb_find_good_group_avg_frag_lists+0x8a/0x200 [ext4]
 ext4_mb_regular_allocator+0x19e9/0x2370 [ext4]
 ext4_mb_new_blocks+0x88a/0x1370 [ext4]
 ext4_ext_map_blocks+0x14f7/0x2390 [ext4]
 ext4_map_blocks+0x569/0xea0 [ext4]
 ext4_do_writepages+0x10f6/0x1bc0 [ext4]
[...]
==================================================================

The flow of issue triggering is as follows:

// Set s_mb_group_prealloc to 2147483647 via sysfs
ext4_mb_new_blocks
  ext4_mb_normalize_request
    ext4_mb_normalize_group_request
      ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc
  ext4_mb_regular_allocator
    ext4_mb_choose_next_group
      ext4_mb_choose_next_group_best_avail
        mb_avg_fragment_size_order
          order = fls(len) - 2 = 29
        ext4_mb_find_good_group_avg_frag_lists
          frag_list = &sbi->s_mb_avg_fragment_size[order]
          if (list_empty(frag_list)) // Trigger SOOB!

At 4k block size, the length of the s_mb_avg_fragment_size list is 14,
but an oversized s_mb_group_prealloc is set, causing slab-out-of-bounds
to be triggered by an attempt to access an element at index 29.

Add a new attr_id attr_clusters_in_group with values in the range
[0, sbi->s_clusters_per_group] and declare mb_group_prealloc as
that type to fix the issue. In addition avoid returning an order
from mb_avg_fragment_size_order() greater than MB_NUM_ORDERS(sb)
and reduce some useless loops.

Fixes: 7e170922f0 ("ext4: Add allocation criteria 1.5 (CR1_5)")
CC: stable@vger.kernel.org
Signed-off-by: Baokun Li <libaokun1@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Link: https://lore.kernel.org/r/20240319113325.3110393-5-libaokun1@huawei.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2024-05-02 23:48:30 -04:00
..
.kunitconfig
acl.c ext4: convert to ctime accessor functions 2023-07-24 10:29:54 +02:00
acl.h Revert "ext4: apply umask if ACL support is disabled" 2024-05-02 18:25:39 -04:00
balloc.c ext4: add some kunit stub for mballoc kunit test 2023-10-05 22:32:16 -04:00
bitmap.c
block_validity.c ext4: add correct group descriptors and reserved GDT blocks to system zone 2023-08-27 11:27:12 -04:00
crypto.c fscrypt: support crypto data unit size less than filesystem block size 2023-09-25 22:34:33 -07:00
dir.c
ext4.h \n 2024-03-13 14:30:58 -07:00
ext4_extents.h
ext4_jbd2.c ext4: move ext4_check_bdev_write_error() into nojournal mode 2024-01-04 23:42:21 -05:00
ext4_jbd2.h
extents.c ext4: fix the comment of ext4_map_blocks()/ext4_ext_map_blocks() 2024-02-21 22:33:21 -05:00
extents_status.c Many singleton patches against the MM code. The patch series which are 2023-11-02 19:38:47 -10:00
extents_status.h
fast_commit.c ext4: make state in ext4_mb_mark_bb to be bool 2023-10-05 22:32:15 -04:00
fast_commit.h
file.c ext4: set FMODE_CAN_ODIRECT instead of a dummy direct_IO method 2024-05-02 10:53:32 -04:00
fsmap.c ext4: port block device access to file 2024-02-25 12:05:26 +01:00
fsmap.h
fsync.c ext4: drop EXT4_MF_FS_ABORTED flag 2023-07-29 18:37:53 -04:00
hash.c ext4: remove redundant checks of s_encoding 2023-08-27 11:27:13 -04:00
ialloc.c ext4: convert to new timestamp accessors 2023-10-18 13:26:21 +02:00
indirect.c ext4: remove unnecessary parameter "needed" in ext4_discard_preallocations 2024-01-18 10:52:45 -05:00
inline.c mm: add folio_zero_tail() and use it in ext4 2023-12-10 16:51:36 -08:00
inode-test.c ext4: convert to ctime accessor functions 2023-07-24 10:29:54 +02:00
inode.c ext4: set FMODE_CAN_ODIRECT instead of a dummy direct_IO method 2024-05-02 10:53:32 -04:00
ioctl.c ext4: remove unnecessary parameter "needed" in ext4_discard_preallocations 2024-01-18 10:52:45 -05:00
Kconfig fs: add CONFIG_BUFFER_HEAD 2023-08-02 09:13:09 -06:00
Makefile
mballoc-test.c ext4: initialize sbi->s_freeclusters_counter and sbi->s_dirtyclusters_counter before use in kunit test 2024-03-07 13:32:54 -05:00
mballoc.c ext4: fix slab-out-of-bounds in ext4_mb_find_good_group_avg_frag_lists() 2024-05-02 23:48:30 -04:00
mballoc.h ext4: remove unused ext4_allocation_context::ac_groups_considered 2024-01-18 10:52:45 -05:00
migrate.c
mmp.c ext4: replace read-only check for shutdown check in mmp code 2023-07-29 18:37:53 -04:00
move_extent.c ext4: remove unnecessary parameter "needed" in ext4_discard_preallocations 2024-01-18 10:52:45 -05:00
namei.c ext4: avoid excessive credit estimate in ext4_tmpfile() 2024-05-02 14:49:16 -04:00
orphan.c
page-io.c fs: convert block_write_full_page to block_write_full_folio 2023-12-29 11:58:35 -08:00
readpage.c ext4: use folio_end_read() 2023-10-18 14:34:16 -07:00
resize.c ext4: enable meta_bg only when new desc blocks are needed 2024-03-07 13:32:54 -05:00
super.c ext4: add support for FS_IOC_GETFSSYSFSPATH 2024-05-02 15:21:23 -04:00
symlink.c ext4_get_link(): fix breakage in RCU mode 2024-02-25 02:10:32 -05:00
sysfs.c ext4: fix slab-out-of-bounds in ext4_mb_find_good_group_avg_frag_lists() 2024-05-02 23:48:30 -04:00
truncate.h
verity.c
xattr.c ext4: drop duplicate ea_inode handling in ext4_xattr_block_set() 2024-03-07 13:32:54 -05:00
xattr.h ext4: move ext4_xattr_handlers to .rodata 2023-10-09 16:24:16 +02:00
xattr_hurd.c
xattr_security.c
xattr_trusted.c
xattr_user.c