Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF63D26E6EC for ; Wed, 16 Jul 2025 13:12:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752671542; cv=none; b=oAYAdT0ra53kjMXd6t5vgAwIMSizmF9Btc7v0lDQD5GDHmfRFMLlU48vY3jUk4XrDTaN3n/rwXv8KkE57xDPb9X2cbUoSsPga5XCfE2oVUNcekzwsKMyuAry+YVCZ/7Tl88gurXtiKLrot5+lY2gbbsT5x/+qHLtR7gmiJF55J8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752671542; c=relaxed/simple; bh=kWvtlVrNG3EDValX8k4tca1OvI2XUR5V9D09DBPm/AQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:content-type; b=edKrNWrSTFsSbto3B3AZC0yOpznvOVT29J0ilsssfFNNnAylmyCs6Nplo2x6ajzUcvLQPBWKILs3KkK4GUxxbaKLIkpCjemw1+RJjfHtDbi2GaSB8kUlC1TukwcPqnIntONlPpIhiwGxKJVIaNgrsk1x0bmKaTyCJbr0NkHw+0A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Iclrg1G6; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Iclrg1G6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752671539; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zqwgeAP2k0w+OI8yeuGqHzqVGkyCB+Wt9xqMdvp0Cqg=; b=Iclrg1G6om/S/W10i2fPrCC5srg4sCx/lmeG7ZRu6w3J3vAeu9vEub/+MQHDc6J7c038xO zsBM2cK4eOzk0mzT0ysFqqsn8FGdA3XtBDQ+yMjvWsyM5n5hE9KEhLiErImrP66827jCNd Wg9z9jNmV68kvIPyGqpreCzrcWqxnFI= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-479-EPEAlaqkPzuOp0HQtYJuCg-1; Wed, 16 Jul 2025 09:12:15 -0400 X-MC-Unique: EPEAlaqkPzuOp0HQtYJuCg-1 X-Mimecast-MFC-AGG-ID: EPEAlaqkPzuOp0HQtYJuCg_1752671534 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5C47418001D7; Wed, 16 Jul 2025 13:12:14 +0000 (UTC) Received: from b.redhat.com (unknown [10.42.28.110]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8541330001A2; Wed, 16 Jul 2025 13:12:11 +0000 (UTC) From: Andrew Price To: agruenba@redhat.com Cc: gfs2@lists.linux.dev, syzbot+4708579bb230a0582a57@syzkaller.appspotmail.com Subject: [PATCH v2] gfs2: Validate i_depth for exhash directories Date: Wed, 16 Jul 2025 14:12:07 +0100 Message-ID: <20250716131207.79856-1-anprice@redhat.com> Precedence: bulk X-Mailing-List: gfs2@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: pVvVwT0XGrEYF_3ZRqfClwxL6eA8ls9ob_A4QaLSjLk_1752671534 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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 --- 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(-) diff --git a/fs/gfs2/dir.c b/fs/gfs2/dir.c index dbf1aede744c1..509e2f0d97e78 100644 --- a/fs/gfs2/dir.c +++ b/fs/gfs2/dir.c @@ -60,6 +60,7 @@ #include #include #include +#include #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); diff --git a/fs/gfs2/glops.c b/fs/gfs2/glops.c index cebd66b22694c..66684ed77dfc3 100644 --- a/fs/gfs2/glops.c +++ b/fs/gfs2/glops.c @@ -11,6 +11,7 @@ #include #include #include +#include #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); -- 2.50.1