[Bug report] ext4: EXT4_IOC_MOVE_EXT with len == 0 clamps the length to the whole origin file and overwrites the donor
From: Guikun Yang
Date: Mon Nov 30 2026 - 11:36:45 EST
Hi Ted, Jan,
A regular (non-corrupted) ext4 filesystem lets EXT4_IOC_MOVE_EXT with
me.len == 0 destroy the donor file's data. The ioctl returns 0 with
moved_len == 1; after a remount the donor file is left empty and
e2fsck reports a clean filesystem throughout.
## The clamp turns 0 into the whole file
fs/ext4/move_extent.c, mext_check_adjust_range():
if (orig_eof <= orig_start)
*len = 0;
else if (orig_eof < orig_start + *len - 1)
*len = orig_eof - orig_start;
if (donor_eof <= donor_start)
*len = 0;
else if (donor_eof < donor_start + *len - 1)
*len = donor_eof - donor_start;
if (!*len)
return -EINVAL;
With *len == 0 and orig_start == 0, "orig_start + *len - 1" evaluates
to U64_MAX, the second comparison is true, and *len is assigned
orig_eof - orig_start, i.e. the whole origin file. The subsequent
"!*len" check passes because *len is no longer zero, and the main loop
in ext4_move_extents() moves the clamped length as usual.
f18b2b83a727 ("ext4: fix argument checking in EXT4_IOC_MOVE_EXT")
added the start-beyond-EOF branches to this function, but the
zero-length request still slips through the "len should not be 0"
check, because the underflow above it rewrites *len before the check
runs.
## Reproducer
Two regular files on the same clean ext4 filesystem, origin larger
than donor:
ioctl(orig_fd, EXT4_IOC_MOVE_EXT, &(struct move_extent){
.orig_start = 0, .donor_start = 0, .len = 0,
.donor_fd = donor_fd, .moved_len = 0 });
returns 0, me.moved_len == 1 for the one-block donor; the donor file
is left empty, the change is crossmount-persistent and the original
donor contents are unrecoverable. e2fsck reports no errors before
or after.
The reproducer is the ioctl sequence above on a clean ext4 image with
two regular files; no special image layout is required.
## Fix direction
Reject the zero length before the underflow comparison, e.g. in
ext4_move_extents() right after locking:
if (!len)
return -EINVAL;
The new KUnit suite
(ext4_mext_check_adjust_range_test(), 0940fb9b934c) covers the
"start beyond EOF clamps len to 0" case but has no len == 0 input
case; adding one would keep this from regressing.
The clamp logic is unchanged on mainline as of 2026-11-30
(move_extent.c:557-564) and in v6.19.5.
Thanks,
Guikun