Skip to content

Commit 94d7c16

Browse files
Akira Fujitatytso
authored andcommitted
ext4: Fix double-free of blocks with EXT4_IOC_MOVE_EXT
At the beginning of ext4_move_extent(), we call ext4_discard_preallocations() to discard inode PAs of orig and donor inodes. But in the following case, blocks can be double freed, so move ext4_discard_preallocations() to the end of ext4_move_extents(). 1. Discard inode PAs of orig and donor inodes with ext4_discard_preallocations() in ext4_move_extents(). orig : [ DATA1 ] donor: [ DATA2 ] 2. While data blocks are exchanging between orig and donor inodes, new inode PAs is created to orig by other process's block allocation. (Since there are semaphore gaps in ext4_move_extents().) And new inode PAs is used partially (2-1). 2-1 Create new inode PAs to orig inode orig : [ DATA1 | used PA1 | free PA1 ] donor: [ DATA2 ] 3. Donor inode which has old orig inode's blocks is deleted after EXT4_IOC_MOVE_EXT finished (3-1, 3-2). So the block bitmap corresponds to old orig inode's blocks are freed. 3-1 After EXT4_IOC_MOVE_EXT finished orig : [ DATA2 | free PA1 ] donor: [ DATA1 | used PA1 ] 3-2 Delete donor inode orig : [ DATA2 | free PA1 ] donor: [ FREE SPACE(DATA1) | FREE SPACE(used PA1) ] 4. The double-free of blocks is occurred, when close() is called to orig inode. Because ext4_discard_preallocations() for orig inode frees used PA1 and free PA1, though used PA1 is already freed in 3. 4-1 Double-free of blocks is occurred orig : [ DATA2 | FREE SPACE(free PA1) ] donor: [ FREE SPACE(DATA1) | DOUBLE FREE(used PA1) ] Signed-off-by: Akira Fujita <[email protected]> Signed-off-by: "Theodore Ts'o" <[email protected]>
1 parent 9084d47 commit 94d7c16

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

fs/ext4/move_extent.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,10 +1289,6 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp,
12891289
ext4_ext_get_actual_len(ext_cur), block_end + 1) -
12901290
max(le32_to_cpu(ext_cur->ee_block), block_start);
12911291

1292-
/* Discard preallocations of two inodes */
1293-
ext4_discard_preallocations(orig_inode);
1294-
ext4_discard_preallocations(donor_inode);
1295-
12961292
while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
12971293
seq_blocks += add_blocks;
12981294

@@ -1410,6 +1406,11 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp,
14101406

14111407
}
14121408
out:
1409+
if (*moved_len) {
1410+
ext4_discard_preallocations(orig_inode);
1411+
ext4_discard_preallocations(donor_inode);
1412+
}
1413+
14131414
if (orig_path) {
14141415
ext4_ext_drop_refs(orig_path);
14151416
kfree(orig_path);

0 commit comments

Comments
 (0)