1 ext4: prevent right-shifting extents beyond EXT_MAX_BLOCKS
3 From: Eric Biggers <ebiggers@google.com>
5 During the "insert range" fallocate operation, extents starting at the
6 range offset are shifted "right" (to a higher file offset) by the range
7 length. But, as shown by syzbot, it's not validated that this doesn't
8 cause extents to be shifted beyond EXT_MAX_BLOCKS. In that case
9 ->ee_block can wrap around, corrupting the extent tree.
11 Fix it by returning an error if the space between the end of the last
12 extent and EXT4_MAX_BLOCKS is smaller than the range being inserted.
14 This bug can be reproduced by running the following commands when the
15 current directory is on an ext4 filesystem with a 4k block size:
17 fallocate -l 8192 file
18 fallocate --keep-size -o 0xfffffffe000 -l 4096 -n file
19 fallocate --insert-range -l 8192 file
21 Then after unmounting the filesystem, e2fsck reports corruption.
23 Reported-by: syzbot+06c885be0edcdaeab40c@syzkaller.appspotmail.com
24 Fixes: 331573febb6a ("ext4: Add support FALLOC_FL_INSERT_RANGE for fallocate")
25 Cc: <stable@vger.kernel.org> # v4.2+
26 Signed-off-by: Eric Biggers <ebiggers@google.com>
27 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
29 fs/ext4/extents.c | 16 +++++++++++-----
30 1 file changed, 11 insertions(+), 5 deletions(-)
32 diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
33 index 0a7315961bac6..c969275ce3ee7 100644
34 --- a/fs/ext4/extents.c
35 +++ b/fs/ext4/extents.c
36 @@ -5329,8 +5329,9 @@ ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
37 stop = le32_to_cpu(extent->ee_block);
40 - * In case of left shift, Don't start shifting extents until we make
41 - * sure the hole is big enough to accommodate the shift.
42 + * For left shifts, make sure the hole on the left is big enough to
43 + * accommodate the shift. For right shifts, make sure the last extent
44 + * won't be shifted beyond EXT_MAX_BLOCKS.
46 if (SHIFT == SHIFT_LEFT) {
47 path = ext4_find_extent(inode, start - 1, &path,
48 @@ -5350,9 +5351,14 @@ ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
50 if ((start == ex_start && shift > ex_start) ||
51 (shift > start - ex_end)) {
52 - ext4_ext_drop_refs(path);
59 + if (shift > EXT_MAX_BLOCKS -
60 + (stop + ext4_ext_get_actual_len(extent))) {