add patch fix-crash-when-i_size-is-too-small
[ext4-patch-queue.git] / fix-crash-when-i_size-is-too-small
blob3be71d0aa74fd55cbb1e20fbb7cac41436e5ca5c
1 ext4: fix crash when a directory's i_size is too small
3 From: Chandan Rajendra <chandan@linux.vnet.ibm.com>
5 On a ppc64 machine, when mounting a fuzzed ext2 image (generated by
6 fsfuzzer) the following call trace is seen,
8 VFS: brelse: Trying to free free buffer
9 WARNING: CPU: 1 PID: 6913 at /root/repos/linux/fs/buffer.c:1165 .__brelse.part.6+0x24/0x40
10 .__brelse.part.6+0x20/0x40 (unreliable)
11 .ext4_find_entry+0x384/0x4f0
12 .ext4_lookup+0x84/0x250
13 .lookup_slow+0xdc/0x230
14 .walk_component+0x268/0x400
15 .path_lookupat+0xec/0x2d0
16 .filename_lookup+0x9c/0x1d0
17 .vfs_statx+0x98/0x140
18 .SyS_newfstatat+0x48/0x80
19 system_call+0x58/0x6c
21 This happens because the directory that ext4_find_entry() looks up has
22 inode->i_size that is less than the block size of the filesystem. This
23 causes 'nblocks' to have a value of zero. ext4_bread_batch() ends up not
24 reading any of the directory file's blocks. This renders the entries in
25 bh_use[] array to continue to have garbage data. buffer_uptodate() on
26 bh_use[0] can then return a zero value upon which brelse() function is
27 invoked.
29 This commit fixes the bug by returning -ENOENT when the directory file
30 has no associated blocks.
32 Reported-by: Abdul Haleem <abdhalee@linux.vnet.ibm.com>
33 Signed-off-by: Chandan Rajendra <chandan@linux.vnet.ibm.com>
34 ---
35  fs/ext4/namei.c | 4 ++++
36  1 file changed, 4 insertions(+)
38 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
39 index 798b3ac680db..e750d68fbcb5 100644
40 --- a/fs/ext4/namei.c
41 +++ b/fs/ext4/namei.c
42 @@ -1399,6 +1399,10 @@ static struct buffer_head * ext4_find_entry (struct inode *dir,
43                                "falling back\n"));
44         }
45         nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
46 +       if (!nblocks) {
47 +               ret = NULL;
48 +               goto cleanup_and_exit;
49 +       }
50         start = EXT4_I(dir)->i_dir_start_lookup;
51         if (start >= nblocks)
52                 start = 0;