Skip to content

Commit 55583e8

Browse files
LiBaokun96tytso
authored andcommitted
ext4: fix double-free of blocks due to wrong extents moved_len
In ext4_move_extents(), moved_len is only updated when all moves are successfully executed, and only discards orig_inode and donor_inode preallocations when moved_len is not zero. When the loop fails to exit after successfully moving some extents, moved_len is not updated and remains at 0, so it does not discard the preallocations. If the moved extents overlap with the preallocated extents, the overlapped extents are freed twice in ext4_mb_release_inode_pa() and ext4_process_freed_data() (as described in commit 94d7c16 ("ext4: Fix double-free of blocks with EXT4_IOC_MOVE_EXT")), and bb_free is incremented twice. Hence when trim is executed, a zero-division bug is triggered in mb_update_avg_fragment_size() because bb_free is not zero and bb_fragments is zero. Therefore, update move_len after each extent move to avoid the issue. Reported-by: Wei Chen <harperchen1110@gmail.com> Reported-by: xingwei lee <xrivendell7@gmail.com> Closes: https://lore.kernel.org/r/CAO4mrferzqBUnCag8R3m2zf897ts9UEuhjFQGPtODT92rYyR2Q@mail.gmail.com Fixes: fcf6b1b ("ext4: refactor ext4_move_extents code base") CC: <stable@vger.kernel.org> # 3.18 Signed-off-by: Baokun Li <libaokun1@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/20240104142040.2835097-2-libaokun1@huawei.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 0d19d9e commit 55583e8

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

fs/ext4/move_extent.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
618618
goto out;
619619
o_end = o_start + len;
620620

621+
*moved_len = 0;
621622
while (o_start < o_end) {
622623
struct ext4_extent *ex;
623624
ext4_lblk_t cur_blk, next_blk;
@@ -672,7 +673,7 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
672673
*/
673674
ext4_double_up_write_data_sem(orig_inode, donor_inode);
674675
/* Swap original branches with new branches */
675-
move_extent_per_page(o_filp, donor_inode,
676+
*moved_len += move_extent_per_page(o_filp, donor_inode,
676677
orig_page_index, donor_page_index,
677678
offset_in_page, cur_len,
678679
unwritten, &ret);
@@ -682,9 +683,6 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
682683
o_start += cur_len;
683684
d_start += cur_len;
684685
}
685-
*moved_len = o_start - orig_blk;
686-
if (*moved_len > len)
687-
*moved_len = len;
688686

689687
out:
690688
if (*moved_len) {

0 commit comments

Comments
 (0)