Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range()
Date: Wed, 11 Nov 2026 23:47:07 -0500	[thread overview]
Message-ID: <20261112044712.1577296-19-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>

mext_check_adjust_range() clamps the requested EXT4_IOC_MOVE_EXT range
to the end of the original and donor files with:

	else if (orig_eof < orig_start + *len - 1)
		*len = orig_eof - orig_start;

orig_eof and donor_eof are exclusive (the first logical block past
i_size), so the range [start, start + len) runs past EOF as soon as
start + len > eof.  The extra "- 1" has two consequences:

 - A range that ends exactly one block past EOF is not clamped, so
   e.g. orig_start = 0, len = 2 on a one-block file walks and swaps a
   block beyond i_size.

 - A zero length makes the unsigned "start + len - 1" wrap to
   ULLONG_MAX when start is 0.  The comparison is then always true and
   len is "clamped" to the full file size, so the later !*len check
   never fires and a request to move zero blocks moves every block of
   the file instead of failing with -EINVAL.

Compare against start + len instead.  With that, a zero length is left
untouched by the clamping (eof > start in that branch) and is rejected
by the existing !*len check, so no separate check is needed.  The sums
cannot overflow because start and len were already bounded by
EXT_MAX_BLOCKS above.

Extend test_mext_check_adjust_range to cover a zero length and ranges
that end exactly one block past EOF.

Fixes: fcf6b1b729bc ("ext4: refactor ext4_move_extents code base")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
 fs/ext4/move_extent-test.c | 21 +++++++++++++++++++++
 fs/ext4/move_extent.c      |  4 ++--
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/move_extent-test.c b/fs/ext4/move_extent-test.c
index e29a392eeebe..dded52fd0b5f 100644
--- a/fs/ext4/move_extent-test.c
+++ b/fs/ext4/move_extent-test.c
@@ -802,6 +802,27 @@ static void test_mext_check_adjust_range(struct kunit *test)
 	ret = ext4_mext_check_adjust_range_test(orig, donor, 4, 4, &len);
 	KUNIT_EXPECT_EQ(test, ret, -EINVAL);
 	KUNIT_EXPECT_EQ(test, len, 0ULL);
+
+	/* A zero length must be rejected, not wrapped to the whole file */
+	len = 0;
+	ret = ext4_mext_check_adjust_range_test(orig, donor, 0, 0, &len);
+	KUNIT_EXPECT_EQ(test, ret, -EINVAL);
+	KUNIT_EXPECT_EQ(test, len, 0ULL);
+
+	/* A range ending exactly one block past EOF must be clamped */
+	i_size_write(orig, MEXT_TEST_BLK_SIZE);		/* orig_eof = 1 */
+	i_size_write(donor, MEXT_TEST_BLK_SIZE);	/* donor_eof = 1 */
+	len = 2;
+	ret = ext4_mext_check_adjust_range_test(orig, donor, 0, 0, &len);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, len, 1ULL);
+
+	i_size_write(orig, 4 * MEXT_TEST_BLK_SIZE);	/* orig_eof = 4 */
+	i_size_write(donor, 8 * MEXT_TEST_BLK_SIZE);	/* donor_eof = 8 */
+	len = 4;
+	ret = ext4_mext_check_adjust_range_test(orig, donor, 1, 1, &len);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, len, 3ULL);
 }
 
 static void test_move_extents_holes_and_unwritten(struct kunit *test)
diff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c
index 49adc348dda0..5ee51fde1819 100644
--- a/fs/ext4/move_extent.c
+++ b/fs/ext4/move_extent.c
@@ -545,11 +545,11 @@ static int mext_check_adjust_range(struct inode *orig_inode,
 	donor_eof = EXT4_B_TO_LBLK(donor_inode, i_size_read(donor_inode));
 	if (orig_eof <= orig_start)
 		*len = 0;
-	else if (orig_eof < orig_start + *len - 1)
+	else if (orig_eof < orig_start + *len)
 		*len = orig_eof - orig_start;
 	if (donor_eof <= donor_start)
 		*len = 0;
-	else if (donor_eof < donor_start + *len - 1)
+	else if (donor_eof < donor_start + *len)
 		*len = donor_eof - donor_start;
 	if (!*len) {
 		ext4_debug("ext4 move extent: len should not be 0 [ino:orig %llu, donor %llu]\n",
-- 
2.56.0.542.g3ef89b7020-goog


  parent reply	other threads:[~2026-11-12  4:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12  4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12  4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12  4:46 ` [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM Theodore Ts'o
2026-11-12  4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12  4:46 ` [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs Theodore Ts'o
2026-11-12  4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12  4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12  4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12  4:46 ` [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling Theodore Ts'o
2026-11-12  4:46 ` [PATCH 10/23] ext4: fix inode table accounting in count_overhead() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12  4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12  4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12  4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12  4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12  4:47 ` Theodore Ts'o [this message]
2026-11-12  4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12  4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12  4:47 ` [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load Theodore Ts'o
2026-11-12  4:47 ` [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges Theodore Ts'o

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112044712.1577296-19-tytso@mit.edu \
    --to=tytso@mit.edu \
    --cc=linux-ext4@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox