linux-stable/fs/ext4
Baokun Li 677ff4589f ext4: fix slab-out-of-bounds in ext4_mb_find_good_group_avg_frag_lists()
commit 13df4d44a3 upstream.

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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-06-27 13:49:10 +02:00
..
.kunitconfig
acl.c ext4: convert to ctime accessor functions 2023-07-24 10:29:54 +02:00
acl.h ext4: apply umask if ACL support is disabled 2023-11-28 17:20:15 +00:00
balloc.c ext4: add correct group descriptors and reserved GDT blocks to system zone 2023-08-27 11:27:12 -04:00
bitmap.c ext4: remove useless conditional branch code 2023-04-19 23:39:08 -04:00
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 ext4: fix memory leaks in ext4_fname_{setup_filename,prepare_lookup} 2023-08-05 08:17:56 -04:00
dir.c
ext4.h quota: Properly annotate i_dquot arrays with __rcu 2024-03-26 18:19:46 -04:00
ext4_extents.h
ext4_jbd2.c Many ext4 and jbd2 cleanups and bug fixes for v6.6-rc1. 2023-08-31 15:18:15 -07:00
ext4_jbd2.h ext4: split ext4_journal_start trace for debug 2022-12-01 10:46:54 -05:00
extents.c ext4: correct the hole length returned by ext4_map_blocks() 2024-03-01 13:34:52 +01:00
extents_status.c ext4: correct the start block of counting reserved clusters 2023-11-28 17:20:15 +00:00
extents_status.h ext4: make ext4_es_insert_extent() return void 2023-06-26 19:35:12 -04:00
fast_commit.c ext4: use ext4_fc_tl_mem in fast-commit replay path 2023-02-09 10:43:23 -05:00
fast_commit.h ext4: add missing validation of fast-commit record lengths 2022-12-08 21:49:24 -05:00
file.c ext4: fix warning in ext4_dio_write_end_io() 2023-12-20 17:01:42 +01:00
fsmap.c ext4: fix another off-by-one fsmap error on 1k block filesystems 2023-03-07 20:20:48 -05: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 Many ext4 and jbd2 cleanups and bug fixes for v6.6-rc1. 2023-08-31 15:18:15 -07:00
indirect.c ext4: only update i_reserved_data_blocks on successful block allocation 2023-06-26 19:34:56 -04:00
inline.c Many ext4 and jbd2 cleanups and bug fixes for v6.6-rc1. 2023-08-31 15:18:15 -07:00
inode-test.c ext4: convert to ctime accessor functions 2023-07-24 10:29:54 +02:00
inode.c ext4: Fixes len calculation in mpage_journal_page_buffers 2024-06-16 13:47:44 +02:00
ioctl.c Many ext4 and jbd2 cleanups and bug fixes for v6.6-rc1. 2023-08-31 15:18:15 -07:00
Kconfig fs: add CONFIG_BUFFER_HEAD 2023-08-02 09:13:09 -06:00
Makefile
mballoc.c ext4: fix slab-out-of-bounds in ext4_mb_find_good_group_avg_frag_lists() 2024-06-27 13:49:10 +02:00
mballoc.h ext4: set type of ac_groups_linear_remaining to __u32 to avoid overflow 2024-06-16 13:47:44 +02:00
migrate.c ext4: avoid deadlock in fs reclaim with page writeback 2023-05-13 18:05:04 -04:00
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: fix double-free of blocks due to wrong extents moved_len 2024-02-23 09:25:04 +01:00
namei.c ext4: avoid excessive credit estimate in ext4_tmpfile() 2024-06-12 11:12:09 +02:00
orphan.c ext4: remove trailing newline from ext4_msg() message 2022-12-08 21:49:23 -05:00
page-io.c ext4: make ext4_forced_shutdown() take struct super_block 2023-07-29 18:37:24 -04:00
readpage.c ext4: Call fsverity_verify_folio() 2023-06-15 00:02:10 -04:00
resize.c ext4: fix corruption during on-line resize 2024-04-03 15:28:28 +02:00
super.c ext4: fix uninitialized ratelimit_state->lock access in __ext4_fill_super() 2024-06-27 13:49:03 +02:00
symlink.c fs: port ->getattr() to pass mnt_idmap 2023-01-19 09:24:25 +01:00
sysfs.c ext4: fix slab-out-of-bounds in ext4_mb_find_good_group_avg_frag_lists() 2024-06-27 13:49:10 +02:00
truncate.h
verity.c - Nick Piggin's "shoot lazy tlbs" series, to improve the peformance of 2023-04-27 19:42:02 -07:00
xattr.c ext4: fix mb_cache_entry's e_refcnt leak in ext4_xattr_block_cache_find() 2024-06-16 13:47:45 +02:00
xattr.h ext4: remove EA inode entry from mbcache on inode eviction 2022-08-02 23:56:25 -04:00
xattr_hurd.c fs: port xattr to mnt_idmap 2023-01-19 09:24:28 +01:00
xattr_security.c fs: port xattr to mnt_idmap 2023-01-19 09:24:28 +01:00
xattr_trusted.c fs: port xattr to mnt_idmap 2023-01-19 09:24:28 +01:00
xattr_user.c fs: port xattr to mnt_idmap 2023-01-19 09:24:28 +01:00