linux-stable/fs/ext4
Baokun Li 0b071a3266 ext4: prevent the normalized size from exceeding EXT_MAX_BLOCKS
commit 2dcf5fde6d upstream.

For files with logical blocks close to EXT_MAX_BLOCKS, the file size
predicted in ext4_mb_normalize_request() may exceed EXT_MAX_BLOCKS.
This can cause some blocks to be preallocated that will not be used.
And after [Fixes], the following issue may be triggered:

=========================================================
 kernel BUG at fs/ext4/mballoc.c:4653!
 Internal error: Oops - BUG: 00000000f2000800 [#1] SMP
 CPU: 1 PID: 2357 Comm: xfs_io 6.7.0-rc2-00195-g0f5cc96c367f
 Hardware name: linux,dummy-virt (DT)
 pc : ext4_mb_use_inode_pa+0x148/0x208
 lr : ext4_mb_use_inode_pa+0x98/0x208
 Call trace:
  ext4_mb_use_inode_pa+0x148/0x208
  ext4_mb_new_inode_pa+0x240/0x4a8
  ext4_mb_use_best_found+0x1d4/0x208
  ext4_mb_try_best_found+0xc8/0x110
  ext4_mb_regular_allocator+0x11c/0xf48
  ext4_mb_new_blocks+0x790/0xaa8
  ext4_ext_map_blocks+0x7cc/0xd20
  ext4_map_blocks+0x170/0x600
  ext4_iomap_begin+0x1c0/0x348
=========================================================

Here is a calculation when adjusting ac_b_ex in ext4_mb_new_inode_pa():

	ex.fe_logical = orig_goal_end - EXT4_C2B(sbi, ex.fe_len);
	if (ac->ac_o_ex.fe_logical >= ex.fe_logical)
		goto adjust_bex;

The problem is that when orig_goal_end is subtracted from ac_b_ex.fe_len
it is still greater than EXT_MAX_BLOCKS, which causes ex.fe_logical to
overflow to a very small value, which ultimately triggers a BUG_ON in
ext4_mb_new_inode_pa() because pa->pa_free < len.

The last logical block of an actual write request does not exceed
EXT_MAX_BLOCKS, so in ext4_mb_normalize_request() also avoids normalizing
the last logical block to exceed EXT_MAX_BLOCKS to avoid the above issue.

The test case in [Link] can reproduce the above issue with 64k block size.

Link: https://patchwork.kernel.org/project/fstests/list/?series=804003
Cc:  <stable@kernel.org> # 6.4
Fixes: 93cdf49f6e ("ext4: Fix best extent lstart adjustment logic in ext4_mb_new_inode_pa()")
Signed-off-by: Baokun Li <libaokun1@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20231127063313.3734294-1-libaokun1@huawei.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-12-20 17:00:25 +01:00
..
.kunitconfig
acl.c fs/ext4: fix comments mentioning i_mutex 2022-02-03 10:57:53 -05:00
acl.h ext4: apply umask if ACL support is disabled 2023-11-28 17:07:21 +00:00
balloc.c ext4: add correct group descriptors and reserved GDT blocks to system zone 2023-09-19 12:28:05 +02:00
bitmap.c
block_validity.c ext4: add correct group descriptors and reserved GDT blocks to system zone 2023-09-19 12:28:05 +02:00
crypto.c ext4: fix memory leaks in ext4_fname_{setup_filename,prepare_lookup} 2023-09-19 12:28:05 +02:00
dir.c ext4: fix spelling errors in comments 2022-05-11 15:19:06 -04:00
ext4.h ext4: add correct group descriptors and reserved GDT blocks to system zone 2023-09-19 12:28:05 +02:00
ext4_extents.h
ext4_jbd2.c ext4: use ext4_debug() instead of jbd_debug() 2022-08-02 23:52:19 -04:00
ext4_jbd2.h fs/ext4: fix comments mentioning i_mutex 2022-02-03 10:57:53 -05:00
extents.c ext4: move 'ix' sanity check to corrent position 2023-11-20 11:52:06 +01:00
extents_status.c ext4: make sure allocate pending entry not fail 2023-12-03 07:32:10 +01:00
extents_status.h
fast_commit.c ext4: use ext4_fc_tl_mem in fast-commit replay path 2023-03-11 13:55:22 +01:00
fast_commit.h ext4: add missing validation of fast-commit record lengths 2023-01-07 11:12:00 +01:00
file.c ext4: fix warning in ext4_dio_write_end_io() 2023-12-20 17:00:14 +01:00
fsmap.c ext4: fix another off-by-one fsmap error on 1k block filesystems 2023-03-17 08:50:16 +01:00
fsmap.h
fsync.c
hash.c ext4: improve error handling from ext4_dirhash() 2023-05-17 11:53:58 +02:00
ialloc.c ext4: allow ext4_get_group_info() to fail 2023-05-24 17:32:33 +01:00
indirect.c ext4: only update i_reserved_data_blocks on successful block allocation 2023-07-23 13:49:34 +02:00
inline.c ext4: bail out of ext4_xattr_ibody_get() fails for any reason 2023-05-17 11:53:58 +02:00
inode-test.c
inode.c ext4: mark buffer new if it is unwritten to avoid stale data exposure 2023-11-28 17:07:21 +00:00
ioctl.c ext4: fix to check return value of freeze_bdev() in ext4_shutdown() 2023-07-23 13:49:34 +02:00
Kconfig
Makefile ext4: move ext4 crypto code to its own file crypto.c 2022-05-21 22:24:24 -04:00
mballoc.c ext4: prevent the normalized size from exceeding EXT_MAX_BLOCKS 2023-12-20 17:00:25 +01:00
mballoc.h ext4: add two helper functions extent_logical_end() and pa_logical_end() 2023-11-02 09:35:32 +01:00
migrate.c ext4: fix warning in 'ext4_da_release_space' 2022-11-06 01:07:59 -04:00
mmp.c ext4: reflect error codes from ext4_multi_mount_protect() to its callers 2023-05-24 17:32:33 +01:00
move_extent.c ext4: factor out ext4_free_ext_path() 2022-09-30 23:46:54 -04:00
namei.c ext4: fix rec_len verify error 2023-09-23 11:11:12 +02:00
orphan.c ext4: remove trailing newline from ext4_msg() message 2023-01-07 11:11:58 +01:00
page-io.c ext4: fix cgroup writeback accounting with fs-layer encryption 2023-03-17 08:50:16 +01:00
readpage.c fscrypt: stop using PG_error to track error status 2022-09-06 15:15:56 -07:00
resize.c ext4: add missed brelse in update_backups 2023-11-28 17:07:21 +00:00
super.c ext4: turn quotas off if mount failed after enabling quotas 2023-07-23 13:49:34 +02:00
symlink.c ext4: fix reading leftover inlined symlinks 2022-08-02 23:37:50 -04:00
sysfs.c ext4: Fix function prototype mismatch for ext4_feat_ktype 2023-02-25 11:25:43 +01:00
truncate.h
verity.c fs: ext4: initialize fsdata in pagecache_write() 2023-01-07 11:11:58 +01:00
xattr.c ext4: correct inline offset when handling xattrs in inode body 2023-07-27 08:50:31 +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
xattr_security.c
xattr_trusted.c
xattr_user.c