linux/fs/ext4
Baokun Li 6121258c2b ext4: fix off by one issue in alloc_flex_gd()
Wesley reported an issue:

==================================================================
EXT4-fs (dm-5): resizing filesystem from 7168 to 786432 blocks
------------[ cut here ]------------
kernel BUG at fs/ext4/resize.c:324!
CPU: 9 UID: 0 PID: 3576 Comm: resize2fs Not tainted 6.11.0+ #27
RIP: 0010:ext4_resize_fs+0x1212/0x12d0
Call Trace:
 __ext4_ioctl+0x4e0/0x1800
 ext4_ioctl+0x12/0x20
 __x64_sys_ioctl+0x99/0xd0
 x64_sys_call+0x1206/0x20d0
 do_syscall_64+0x72/0x110
 entry_SYSCALL_64_after_hwframe+0x76/0x7e
==================================================================

While reviewing the patch, Honza found that when adjusting resize_bg in
alloc_flex_gd(), it was possible for flex_gd->resize_bg to be bigger than
flexbg_size.

The reproduction of the problem requires the following:

 o_group = flexbg_size * 2 * n;
 o_size = (o_group + 1) * group_size;
 n_group: [o_group + flexbg_size, o_group + flexbg_size * 2)
 o_size = (n_group + 1) * group_size;

Take n=0,flexbg_size=16 as an example:

              last:15
|o---------------|--------------n-|
o_group:0    resize to      n_group:30

The corresponding reproducer is:

img=test.img
rm -f $img
truncate -s 600M $img
mkfs.ext4 -F $img -b 1024 -G 16 8M
dev=`losetup -f --show $img`
mkdir -p /tmp/test
mount $dev /tmp/test
resize2fs $dev 248M

Delete the problematic plus 1 to fix the issue, and add a WARN_ON_ONCE()
to prevent the issue from happening again.

[ Note: another reproucer which this commit fixes is:

  img=test.img
  rm -f $img
  truncate -s 25MiB $img
  mkfs.ext4 -b 4096 -E nodiscard,lazy_itable_init=0,lazy_journal_init=0 $img
  truncate -s 3GiB $img
  dev=`losetup -f --show $img`
  mkdir -p /tmp/test
  mount $dev /tmp/test
  resize2fs $dev 3G
  umount $dev
  losetup -d $dev

  -- TYT ]

Reported-by: Wesley Hershberger <wesley.hershberger@canonical.com>
Closes: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/2081231
Reported-by: Stéphane Graber <stgraber@stgraber.org>
Closes: https://lore.kernel.org/all/20240925143325.518508-1-aleksandr.mikhalitsyn@canonical.com/
Tested-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Tested-by: Eric Sandeen <sandeen@redhat.com>
Fixes: 665d3e0af4 ("ext4: reduce unnecessary memory allocation in alloc_flex_gd()")
Cc: stable@vger.kernel.org
Signed-off-by: Baokun Li <libaokun1@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/20240927133329.1015041-1-libaokun@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2024-10-04 17:36:28 -04:00
..
.kunitconfig
acl.c
acl.h
balloc.c
bitmap.c
block_validity.c
crypto.c
dir.c Lots of cleanups and bug fixes this cycle, primarily in the block 2024-09-20 19:26:45 -07:00
ext4.h Lots of cleanups and bug fixes this cycle, primarily in the block 2024-09-20 19:26:45 -07:00
ext4_extents.h
ext4_jbd2.c
ext4_jbd2.h
extents.c ext4: save unnecessary indentation in ext4_ext_create_new_leaf() 2024-09-03 22:14:16 -04:00
extents_status.c
extents_status.h
fast_commit.c ext4: use handle to mark fc as ineligible in __track_dentry_update() 2024-10-04 17:35:54 -04:00
fast_commit.h
file.c ext4: dax: keep orphan list before truncate overflow allocated blocks 2024-09-03 22:14:16 -04:00
fsmap.c
fsmap.h
fsync.c
hash.c
ialloc.c
indirect.c
inline.c Lots of cleanups and bug fixes this cycle, primarily in the block 2024-09-20 19:26:45 -07:00
inode-test.c
inode.c Lots of cleanups and bug fixes this cycle, primarily in the block 2024-09-20 19:26:45 -07:00
ioctl.c
Kconfig
Makefile
mballoc-test.c
mballoc.c ext4: convert EXT4_B2C(sbi->s_stripe) users to EXT4_NUM_B2C 2024-09-03 22:14:17 -04:00
mballoc.h
migrate.c ext4: fix i_data_sem unlock order in ext4_ind_migrate() 2024-09-03 22:14:17 -04:00
mmp.c
move_extent.c ext4: get rid of ppath in get_ext_path() 2024-09-03 22:12:17 -04:00
namei.c
orphan.c
page-io.c
readpage.c
resize.c ext4: fix off by one issue in alloc_flex_gd() 2024-10-04 17:36:28 -04:00
super.c ext4: check stripe size compatibility on remount as well 2024-09-03 22:14:17 -04:00
symlink.c
sysfs.c
truncate.h
verity.c
xattr.c ext4: mark fc as ineligible using an handle in ext4_xattr_set() 2024-10-04 17:36:09 -04:00
xattr.h
xattr_hurd.c
xattr_security.c
xattr_trusted.c
xattr_user.c