gfs2: Validate i_depth for exhash directories
PATCH v2 1/1 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'.
---
v2:
- Switch to using ilog2() in dir_make_exhash() as well
- Include linux/log2.h explicitly
- Re-tested
fs/gfs2/dir.c | 6 ++---- fs/gfs2/glops.c | 5 +++++ 2 files changed, 7 insertions(+), 4 deletions(-)
fs/gfs2/dir.c +2 -4
@@ -60,6 +60,7 @@#include <linux/crc32.h> #include <linux/vmalloc.h> #include <linux/bio.h> +#include <linux/log2.h> #include "gfs2.h" #include "incore.h"
@@ -912,7 +913,6 @@ static int dir_make_exhash(struct inode *inode)struct qstr args;struct buffer_head *bh, *dibh;struct gfs2_leaf *leaf; -int y;u32 x;__be64 *lp;u64 bn;
@@ -979,9 +979,7 @@ static int dir_make_exhash(struct inode *inode)i_size_write (inode, sdp->sd_sb.sb_bsize /2 );gfs2_add_inode_blocks (&dip->i_inode,1 ); dip->i_diskflags |=GFS2_DIF_EXHASH ; - -for (x = sdp->sd_hash_ptrs, y =-1 ; x; x >>=1 , y++) ; - dip->i_depth = y; + dip->i_depth =ilog2 (sdp->sd_hash_ptrs);gfs2_dinode_out (dip, dibh->b_data);
fs/gfs2/glops.c +5 -0
@@ -11,6 +11,7 @@#include <linux/bio.h> #include <linux/posix_acl.h> #include <linux/security.h> +#include <linux/log2.h> #include "gfs2.h" #include "incore.h"
@@ -450,6 +451,10 @@ static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)gfs2_consist_inode (ip);return -EIO ; } +if ((ip->i_diskflags &GFS2_DIF_EXHASH ) && depth <ilog2 (sdp->sd_hash_ptrs)) { +gfs2_consist_inode (ip); +return -EIO ; + } ip->i_depth = (u8 )depth; ip->i_entries =be32_to_cpu (str->di_entries);
Signature · 1 line
2.50.1
Quoted 86 lines · Andrew Price
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 isilog2(sdp->sd_hash_ptrs)and 0 is invalid assdp->sd_hash_ptrsis fixed assdp->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 useilog2()to clarify how the depth is calculated.Tested with the syzkaller
repro.cand xfstests '-g quick'.Reported-by [email protected]Signed-off-by Andrew Price <[email protected]>---
v2:
- Switch to usingilog2()indir_make_exhash()as well
- Includelinux/log2.hexplicitly
- Re-testedfs/gfs2/dir.c | 6 ++---- fs/gfs2/glops.c | 5 +++++ 2 files changed, 7 insertions(+), 4 deletions(-)fs/gfs2/dir.c +2
-4@@ -60,6 +60,7 @@#include <linux/crc32.h> #include <linux/vmalloc.h> #include <linux/bio.h> +#include <linux/log2.h> #include "gfs2.h" #include "incore.h" @@ -912,7 +913,6 @@ static int dir_make_exhash(struct inode *inode)struct qstr args;struct buffer_head *bh, *dibh;struct gfs2_leaf *leaf; -int y;u32 x;__be64 *lp;u64 bn;@@ -979,9 +979,7 @@ static int dir_make_exhash(struct inode *inode)i_size_write (inode, sdp->sd_sb.sb_bsize /2 );gfs2_add_inode_blocks (&dip->i_inode,1 ); dip->i_diskflags |=GFS2_DIF_EXHASH ; - -for (x = sdp->sd_hash_ptrs, y =-1 ; x; x >>=1 , y++) ; - dip->i_depth = y; + dip->i_depth =ilog2 (sdp->sd_hash_ptrs);gfs2_dinode_out (dip, dibh->b_data);fs/gfs2/glops.c +5
-1@@ -11,6 +11,7 @@#include <linux/bio.h> #include <linux/posix_acl.h> #include <linux/security.h> +#include <linux/log2.h> #include "gfs2.h" #include "incore.h" @@ -450,6 +451,10 @@ static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)gfs2_consist_inode (ip);return -EIO ; } +if ((ip->i_diskflags &GFS2_DIF_EXHASH ) && depth <ilog2 (sdp->sd_hash_ptrs)) { +gfs2_consist_inode (ip); +return -EIO ; + } ip->i_depth = (u8 )depth; ip->i_entries =be32_to_cpu (str->di_entries); --
Quoted 2 lines
2.50.1
Applied to for-next, thanks.
Andreas