Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gfs2: Validate i_depth for exhash directories

A fuzzer test introduced corruption that ends up with a depth of 0 in
dir_e_read(), causing an undefined shift by 32 at:

index = hash >> (32 - dip->i_depth);

As calculated in an open-coded way in dir_make_exhash(), the minimum
depth for an exhash directory is ilog2(sdp->sd_hash_ptrs) and 0 is
invalid as sdp->sd_hash_ptrs is fixed as sdp->bsize / 16 at mount time.

So we can avoid the undefined behaviour by checking for depth values
lower than the minimum in gfs2_dinode_in(). Values greater than the
maximum are already being checked for there.

Also switch the calculation in dir_make_exhash() to use ilog2() to
clarify how the depth is calculated.

Tested with the syzkaller repro.c and xfstests '-g quick'.

Reported-by: syzbot+4708579bb230a0582a57@syzkaller.appspotmail.com
Signed-off-by: Andrew Price <anprice@redhat.com>
Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>

authored by

Andrew Price and committed by
Andreas Gruenbacher
557c024c 5c8f12cf

+8 -4
+2 -4
fs/gfs2/dir.c
··· 60 60 #include <linux/crc32.h> 61 61 #include <linux/vmalloc.h> 62 62 #include <linux/bio.h> 63 + #include <linux/log2.h> 63 64 64 65 #include "gfs2.h" 65 66 #include "incore.h" ··· 913 912 struct qstr args; 914 913 struct buffer_head *bh, *dibh; 915 914 struct gfs2_leaf *leaf; 916 - int y; 917 915 u32 x; 918 916 __be64 *lp; 919 917 u64 bn; ··· 979 979 i_size_write(inode, sdp->sd_sb.sb_bsize / 2); 980 980 gfs2_add_inode_blocks(&dip->i_inode, 1); 981 981 dip->i_diskflags |= GFS2_DIF_EXHASH; 982 - 983 - for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ; 984 - dip->i_depth = y; 982 + dip->i_depth = ilog2(sdp->sd_hash_ptrs); 985 983 986 984 gfs2_dinode_out(dip, dibh->b_data); 987 985
+6
fs/gfs2/glops.c
··· 11 11 #include <linux/bio.h> 12 12 #include <linux/posix_acl.h> 13 13 #include <linux/security.h> 14 + #include <linux/log2.h> 14 15 15 16 #include "gfs2.h" 16 17 #include "incore.h" ··· 448 447 449 448 depth = be16_to_cpu(str->di_depth); 450 449 if (unlikely(depth > GFS2_DIR_MAX_DEPTH)) { 450 + gfs2_consist_inode(ip); 451 + return -EIO; 452 + } 453 + if ((ip->i_diskflags & GFS2_DIF_EXHASH) && 454 + depth < ilog2(sdp->sd_hash_ptrs)) { 451 455 gfs2_consist_inode(ip); 452 456 return -EIO; 453 457 }