From nobody Sat Sep 26 13:08:16 2026 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) (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 85BB4378D89 for ; Tue, 1 Sep 2026 06:33:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.112 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788244394; cv=none; b=F2ZpJTIKKt4kdfE+1GVC8izuFVa02aasmfMiSFbgJTtwvh3Bcz+gxw7uBjJwRll/7Z+rOQ82+nyFt3FQgNa3blxZ/16AsUyDi1/HA+rxma6s4h2mECkTlSm2JgI7ntcLHNpiR0LNp/5OEBOOZj4+7+Rc6cEasG3xwmxkFM93uew= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788244394; c=relaxed/simple; bh=5R3l83+AnsGOP1omiDNJHnYYx4quuehlLcZf3o7Rddc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RvYo1OS0RtFNRfvMM+4zswzJ07BWWpTKUHhOOJCoQGISkGTUP34S/EATbMzpa1Gq8NPrvWaFHRSJQCyDEwP4xmLArBl1R+s0MJptY1etG3jK9r8yqQzoTWbh4pBUpgs4jM9wHybOZeBCCJFwe7/DEk6YWZ5Mszwhfcs5QEmXLcY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=O315y6Mj; arc=none smtp.client-ip=115.124.30.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="O315y6Mj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1788244384; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=yVWRK7fUCEudtcuozwBanmc7BlPTtipZ34kWdaYysDw=; b=O315y6MjHQ5kNtbwfCFORdiCavL7c5tid0dMxO33gSPAQDLfnncheM7DBHU5vS/eCFUsWMsUDA2xRBridBF4fpmLostpNzMg5eXQgwDK2dc66gszsfKNhZnYCsQyMmEwDyjm03h0H8yiOQWNso/RC94auorIWbumLC+hjbCebtw= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R211e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037009110;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0XA4XOaV_1788244382; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XA4XOaV_1788244382 cluster:ay36) by smtp.aliyun-inc.com; Tue, 01 Sep 2026 14:33:03 +0800 From: Joseph Qi To: Andrew Morton , Heming Zhao Cc: Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/4] ocfs2: restrict OCFS2_INVALID_SLOT suballoc slot to system inodes Date: Tue, 1 Sep 2026 14:32:58 +0800 Message-Id: <20260901063301.984933-2-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260901063301.984933-1-joseph.qi@linux.alibaba.com> References: <20260901063301.984933-1-joseph.qi@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ocfs2_validate_inode_block() currently permits i_suballoc_slot to be OCFS2_INVALID_SLOT for any dinode. Only system inodes created by mkfs.ocfs2 are allocated from the global allocator and thus legitimately carry this value; regular inodes are always allocated from a per-slot suballocator and hence must have a valid slot. If a corrupted regular inode with OCFS2_INVALID_SLOT is accepted, ocfs2_remove_inode() will pass the slot to ocfs2_get_system_file_inode() and get_local_system_inode() will hit BUG_ON(slot =3D=3D OCFS2_INVALID_SLOT) when the inode is deleted. This can be triggered by an unprivileged user unlinking such a corrupted file. Reject OCFS2_INVALID_SLOT for non-system dinodes during validation, while still accepting it for system inodes. Note that a crafted dinode carrying OCFS2_SYSTEM_FL passes the check above, yet a plain lookup of it still used to BUG() in ocfs2_read_locked_inode() ("system file state is ambiguous"). Since i_flags comes from disk, handle that mismatch with ocfs2_error() instead of BUG_ON() as well. Fixes: fe7a283b3916 ("ocfs2: add suballoc slot check in ocfs2_validate_inod= e_block()") Cc: stable@vger.kernel.org Signed-off-by: Joseph Qi --- fs/ocfs2/inode.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c index 180107a11046..9228d6ef23c2 100644 --- a/fs/ocfs2/inode.c +++ b/fs/ocfs2/inode.c @@ -638,14 +638,18 @@ static int ocfs2_read_locked_inode(struct inode *inod= e, fe =3D (struct ocfs2_dinode *) bh->b_data; =20 /* - * This is a code bug. Right now the caller needs to - * understand whether it is asking for a system file inode or - * not so the proper lock names can be built. + * The caller must know whether it is asking for a system file inode + * or not so the proper lock names can be built. Since i_flags comes + * from disk, a mismatch is filesystem corruption instead of a code + * bug, so handle it with ocfs2_error() rather than BUG_ON(). */ - mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=3D - !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE), - "Inode %llu: system file state is ambiguous\n", - (unsigned long long)args->fi_blkno); + if (!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=3D + !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)) { + status =3D ocfs2_error(osb->sb, + "Inode %llu: system file state is ambiguous\n", + (unsigned long long)args->fi_blkno); + goto bail; + } =20 if (S_ISCHR(le16_to_cpu(fe->i_mode)) || S_ISBLK(le16_to_cpu(fe->i_mode))) @@ -1520,8 +1524,23 @@ int ocfs2_validate_inode_block(struct super_block *s= b, goto bail; } =20 - if (le16_to_cpu(di->i_suballoc_slot) !=3D (u16)OCFS2_INVALID_SLOT && - (u32)le16_to_cpu(di->i_suballoc_slot) > OCFS2_SB(sb)->max_slots - 1) { + /* + * Only system inodes created by mkfs.ocfs2 are allocated from the + * global allocator and thus legitimately carry OCFS2_INVALID_SLOT. + * Regular inodes are always allocated from a per-slot suballocator. + * If a regular inode with OCFS2_INVALID_SLOT was accepted here, + * deleting it would pass the slot to get_local_system_inode() via + * ocfs2_remove_inode() and trigger BUG_ON(slot =3D=3D OCFS2_INVALID_SLOT= ). + */ + if (le16_to_cpu(di->i_suballoc_slot) =3D=3D (u16)OCFS2_INVALID_SLOT) { + if (!(le32_to_cpu(di->i_flags) & OCFS2_SYSTEM_FL)) { + rc =3D ocfs2_error(sb, + "Invalid dinode %llu: suballoc slot %u for non-system inode\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(di->i_suballoc_slot)); + goto bail; + } + } else if ((u32)le16_to_cpu(di->i_suballoc_slot) > OCFS2_SB(sb)->max_slot= s - 1) { rc =3D ocfs2_error(sb, "Invalid dinode %llu: suballoc slot %u\n", (unsigned long long)bh->b_blocknr, le16_to_cpu(di->i_suballoc_slot)); --=20 2.39.3 From nobody Sat Sep 26 13:08:16 2026 Received: from out30-111.freemail.mail.aliyun.com (out30-111.freemail.mail.aliyun.com [115.124.30.111]) (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 D3B5336A364 for ; Tue, 1 Sep 2026 06:33:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788244390; cv=none; b=LWWfHlqBbHqtEhjhZFLVAMgVFwpmEcziGVOZV8QnfmN1roQ9YvMh+dM5HjW4fqeWzVQSAWFNd/RgNdPzX6zhRDlX3uDDPjS9i/83D4MQeNM5NbOrpxc8GbPTLV8NIvf0u/OOSImQUjde4MQdQx7TueZd4+Zedt7fWzGMmaT7R6s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788244390; c=relaxed/simple; bh=h8kkhzO2ImVgjkG1e2qZ/ZXPIitu6FWdseOO3TZP70c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XtYIRsF6MOD+B1l5E0Mz7LI462YQ4Rvf4Aez9oAo9jvREWiDNmbAy9/MxVxcfvIRBou/x2b7K3xOHlZ1ZNCr7NNYDpoWpeSkPSBcSRPdIXQjWRHsuuX0qUzPVIFmsqh3qrx5A8VxINyQJhxbGW4PqdpscH9hg/gXraIQufoQt78= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=tt5qHlP3; arc=none smtp.client-ip=115.124.30.111 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="tt5qHlP3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1788244384; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=GyVRRq71oNkvnJvOiM8YXwHI5/MyS+0QmarDgfjWn34=; b=tt5qHlP3HX0oxO/nMq6Rz7nX8Hle8VBavvFeemTHh9sP2lSSQmq4l64aJ4Ip2yARRfuarkohdPYF7lBQmvBkeOID63GQ8lUATXyjiRx1lO6zV6Na2HmYOOhtnwwCXt4YTmoj5duAtWgCyM7Kbf6V34fiw+Aj39PQ0uAyrow7dKY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037026112;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0XA47zDB_1788244383; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XA47zDB_1788244383 cluster:ay36) by smtp.aliyun-inc.com; Tue, 01 Sep 2026 14:33:04 +0800 From: Joseph Qi To: Andrew Morton , Heming Zhao Cc: Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/4] ocfs2: validate suballoc bit during inode read Date: Tue, 1 Sep 2026 14:32:59 +0800 Message-Id: <20260901063301.984933-3-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260901063301.984933-1-joseph.qi@linux.alibaba.com> References: <20260901063301.984933-1-joseph.qi@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" i_suballoc_bit of a dinode is currently not validated at all. A corrupted dinode can carry an abnormally large i_suballoc_bit, which bypasses ocfs2_validate_inode_block(). When the inode is deleted, ocfs2_remove_inode() calls ocfs2_free_dinode(), which passes the unvalidated bit to _ocfs2_free_suballoc_bits() and triggers BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)). A suballocator block group bitmap is contained in a single block and starts after the group descriptor header, so a valid suballoc bit must be smaller than the number of bits fitting in the remaining space. Reject oversized i_suballoc_bit values during dinode validation. Signed-off-by: Joseph Qi --- fs/ocfs2/inode.c | 16 ++++++++++++++++ fs/ocfs2/ocfs2.h | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c index 9228d6ef23c2..92f3450010fb 100644 --- a/fs/ocfs2/inode.c +++ b/fs/ocfs2/inode.c @@ -1547,6 +1547,22 @@ int ocfs2_validate_inode_block(struct super_block *s= b, goto bail; } =20 + /* + * A suballocator block group bitmap is contained in a single block + * and starts after the group descriptor header, so a valid suballoc + * bit can never exceed ocfs2_suballoc_bits_per_block(). Otherwise + * deleting the inode will pass the oversized bit to + * _ocfs2_free_suballoc_bits() via ocfs2_free_dinode() and trigger + * BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)), since any + * group holds at most ocfs2_suballoc_bits_per_block() bits. + */ + if (le16_to_cpu(di->i_suballoc_bit) >=3D ocfs2_suballoc_bits_per_block(sb= )) { + rc =3D ocfs2_error(sb, "Invalid dinode %llu: suballoc bit %u\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(di->i_suballoc_bit)); + goto bail; + } + if ((le32_to_cpu(di->i_flags) & OCFS2_ORPHANED_FL) && le16_to_cpu(di->i_orphaned_slot) >=3D OCFS2_SB(sb)->max_slots) { rc =3D ocfs2_error(sb, "Invalid dinode %llu: orphaned slot %u\n", diff --git a/fs/ocfs2/ocfs2.h b/fs/ocfs2/ocfs2.h index b747cdec1787..b6ead97605b3 100644 --- a/fs/ocfs2/ocfs2.h +++ b/fs/ocfs2/ocfs2.h @@ -593,6 +593,17 @@ static inline int ocfs2_supports_discontig_bg(struct o= cfs2_super *osb) return 0; } =20 +/* + * A suballocator block group bitmap starts right after the group + * descriptor header and extends to the end of the group block, so a + * suballoc bit can never exceed this number of bits. + */ +static inline u32 ocfs2_suballoc_bits_per_block(struct super_block *sb) +{ + return (sb->s_blocksize - offsetof(struct ocfs2_group_desc, + bg_bitmap)) * 8; +} + static inline unsigned int ocfs2_link_max(struct ocfs2_super *osb) { if (ocfs2_supports_indexed_dirs(osb)) --=20 2.39.3 From nobody Sat Sep 26 13:08:16 2026 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (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 721EA38425B for ; Tue, 1 Sep 2026 06:33:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788244396; cv=none; b=dvOdVqf/C+zj6kkji38qZUniZrn/H9o8ZlXEgs6AdLdpShx20RwXGhMiAWmY1BuRKC4H8b6vIsT5BDmPO+HRcHxrAtSHcPSBQ/zSqf0IfmG/paJ0bcqiHizdIjil/wL9Whj4F0/xA5OaZwF926imkxjrkR/blB8fNV75+Kmfqj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788244396; c=relaxed/simple; bh=QlFEeV6RHIe0/cFeTsdKxeR1dRa4QtplurHQwxtqkg4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pmeEmwHi812taPdo/cdeK94T4ZEWZykhtWDgiIF1bnn4Yzm+XMoCVqlhLgaZyanyFsZjxDar0UzfL32t5Ukibe+1i+jWpQLHpA6Noi9TIqas/oK572Y7hqWGKtC49AoSl05EdKZjoaPrChBDrEyLJA2HW6PpPpaa1uq+pmkHLTo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=lSpPNH3O; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="lSpPNH3O" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1788244385; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=6FHgHx7/gtlO3ZBPegU8P+k/3JGXORJVFTUjJl3ljDs=; b=lSpPNH3OOD4eqOvUY+R/jgt3RTD/EBXwtCjy5Mms0Oda2EKWhpdrsZ+EOyQqy+NjyOXO9TOFNS1qCVaqs83/8vJRwfwRo8oAr2dPyTubzuOpcxIG4ol7iBhWccC7IAKJyZriPwcP3IpVCkQ97kxDoO92zqHK7F6q9NyMb311cyY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R351e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam011083073210;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0XA4XObN_1788244384; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XA4XObN_1788244384 cluster:ay36) by smtp.aliyun-inc.com; Tue, 01 Sep 2026 14:33:05 +0800 From: Joseph Qi To: Andrew Morton , Heming Zhao Cc: Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/4] ocfs2: validate suballoc slot and bit of xattr and dir index blocks Date: Tue, 1 Sep 2026 14:33:00 +0800 Message-Id: <20260901063301.984933-4-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260901063301.984933-1-joseph.qi@linux.alibaba.com> References: <20260901063301.984933-1-joseph.qi@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ocfs2_validate_xattr_block() and ocfs2_validate_dx_root() do not validate xb_suballoc_slot, xb_suballoc_bit, dr_suballoc_slot and dr_suballoc_bit at all. Since xattr blocks and dir index root blocks are allocated from a per-slot suballocator at runtime, their suballoc slots must be within range and their suballoc bits must fit in a block group bitmap. Otherwise a corrupted image can carry an out-of-range slot. When the xattr block or dir index is removed, ocfs2_xattr_block_remove() or ocfs2_dx_dir_remove_index() passes the unvalidated slot to ocfs2_get_system_file_inode() and get_local_system_inode() will either hit BUG_ON(slot =3D=3D OCFS2_INVALID_SLOT) or compute an out-of-bounds index into the local_system_inodes array. Similarly an oversized suballoc bit will trigger BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)) in _ocfs2_free_suballoc_bits(). Reject out-of-range suballoc slots and oversized suballoc bits during validation. Signed-off-by: Joseph Qi --- fs/ocfs2/dir.c | 27 +++++++++++++++++++++++++++ fs/ocfs2/xattr.c | 25 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c index 0075e1624310..0c3c4a13d9a5 100644 --- a/fs/ocfs2/dir.c +++ b/fs/ocfs2/dir.c @@ -605,6 +605,33 @@ static int ocfs2_validate_dx_root(struct super_block *= sb, goto bail; } =20 + /* + * Dir index root blocks are allocated from a per-slot suballocator, + * so the slot must be in range. Otherwise removing the index passes + * it to get_local_system_inode(), which hits BUG_ON() for + * OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. + */ + if ((u32)le16_to_cpu(dx_root->dr_suballoc_slot) >=3D OCFS2_SB(sb)->max_sl= ots) { + ret =3D ocfs2_error(sb, + "Dir Index Root # %llu has invalid dr_suballoc_slot %u\n", + (unsigned long long)le64_to_cpu(dx_root->dr_blkno), + le16_to_cpu(dx_root->dr_suballoc_slot)); + goto bail; + } + + /* + * Similarly the suballoc bit must fit in a block group bitmap. + * Otherwise removing the index will pass the oversized bit to + * _ocfs2_free_suballoc_bits() and trigger BUG_ON() there. + */ + if (le16_to_cpu(dx_root->dr_suballoc_bit) >=3D ocfs2_suballoc_bits_per_bl= ock(sb)) { + ret =3D ocfs2_error(sb, + "Dir Index Root # %llu has invalid dr_suballoc_bit %u\n", + (unsigned long long)le64_to_cpu(dx_root->dr_blkno), + le16_to_cpu(dx_root->dr_suballoc_bit)); + goto bail; + } + if (!(dx_root->dr_flags & OCFS2_DX_FLAG_INLINE)) { struct ocfs2_extent_list *el =3D &dx_root->dr_list; =20 diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index 143d6f75f9c9..897eba83904b 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -532,6 +532,31 @@ static int ocfs2_validate_xattr_block(struct super_blo= ck *sb, le32_to_cpu(xb->xb_fs_generation)); } =20 + /* + * Xattr blocks are allocated from a per-slot suballocator, so the + * slot must be in range. Otherwise freeing the block passes it to + * get_local_system_inode(), which hits BUG_ON() for + * OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. + */ + if ((u32)le16_to_cpu(xb->xb_suballoc_slot) >=3D OCFS2_SB(sb)->max_slots) { + return ocfs2_error(sb, + "Extended attribute block #%llu has an invalid xb_suballoc_slot of = %u\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(xb->xb_suballoc_slot)); + } + + /* + * Similarly the suballoc bit must fit in a block group bitmap. + * Otherwise freeing the block will pass the oversized bit to + * _ocfs2_free_suballoc_bits() and trigger BUG_ON() there. + */ + if (le16_to_cpu(xb->xb_suballoc_bit) >=3D ocfs2_suballoc_bits_per_block(s= b)) { + return ocfs2_error(sb, + "Extended attribute block #%llu has an invalid xb_suballoc_bit of %= u\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(xb->xb_suballoc_bit)); + } + if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { size_t region_offset =3D offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); --=20 2.39.3 From nobody Sat Sep 26 13:08:16 2026 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) (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 E49AC4CCDC7 for ; Tue, 1 Sep 2026 06:33:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.112 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788244395; cv=none; b=X1pRRUHNX05a2j0h8Yji4t7Co6fkZotftns8IJBIvT08/FjHtyHK6c/QYXLtlK0Vs88uOJnvTpuOopxIK8JL8emy6kz6eF9yZspJVRUFBp5H+0inmh8NZHBx0nrBxfgEvBVm7NiB6HpH9aw4DoZwcDq+kNeIT6bzsjY+hIzwuvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788244395; c=relaxed/simple; bh=zq+dBOR9pu3uUy3aiwRsKoY192nzaH3ZtvrLvS3g5zE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hu2YWuI0Z+omFphtIWy9oiPTvq+NodrzHukLAEM2B6x9Id2Xv6vjy1HErr6GgQsL/FdjfX6X+q7rMI/2TQN6oQ3piUStw1fGVaHrkDeQUh/p2vnPIlLN0Sm5gZO4ijW5HsJ1IoxH59OwxotLHRUezHUo2gJ703At0TtUjBHyYQQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=fX8V/zhy; arc=none smtp.client-ip=115.124.30.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="fX8V/zhy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1788244386; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=U3YQzZeMlyQnz6ajZaVTDD69Hg0YteEmTq0IsPrfcV0=; b=fX8V/zhyM+NcC+ek680h03Y1MJWh1FU4BL7YX3hrHU3vwQshJRqK+d2bQnIa5a0VncMmR1fReWozXBZ1M2DCLbcufLxpOr9tqTSbXP9yuXdLUtDwgu/1tngkQFu0+0iffpzZfNLkPHinddrzM+rdv6F1GxlLLpd81SULtxvWigI= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037026112;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0XA3vCjE_1788244385; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XA3vCjE_1788244385 cluster:ay36) by smtp.aliyun-inc.com; Tue, 01 Sep 2026 14:33:06 +0800 From: Joseph Qi To: Andrew Morton , Heming Zhao Cc: Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/4] ocfs2: validate suballoc slot and bit of extent and refcount blocks Date: Tue, 1 Sep 2026 14:33:01 +0800 Message-Id: <20260901063301.984933-5-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260901063301.984933-1-joseph.qi@linux.alibaba.com> References: <20260901063301.984933-1-joseph.qi@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ocfs2_validate_extent_block() and ocfs2_validate_refcount_block() do not validate h_suballoc_slot, h_suballoc_bit, rf_suballoc_slot and rf_suballoc_bit at all. Since extent blocks and refcount blocks are allocated from a per-slot suballocator at runtime, their suballoc slots must be within range and their suballoc bits must fit in a block group bitmap. Otherwise a corrupted image can carry an out-of-range slot. When the extent block is freed, ocfs2_cache_extent_block_free() caches it and ocfs2_free_cached_blocks() later passes the unvalidated slot to ocfs2_get_system_file_inode(); when the refcount block is freed, ocfs2_remove_refcount_extent() passes it via ocfs2_cache_block_dealloc(). get_local_system_inode() will then either hit BUG_ON(slot =3D=3D OCFS2_INVALID_SLOT) or compute an out-of-bounds index into the local_system_inodes array. Similarly an oversized suballoc bit will trigger BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)) in _ocfs2_free_suballoc_bits(). Reject out-of-range suballoc slots and oversized suballoc bits during validation. Signed-off-by: Joseph Qi --- fs/ocfs2/alloc.c | 27 +++++++++++++++++++++++++++ fs/ocfs2/refcounttree.c | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c index be09e766ac1f..9f5e94e734e9 100644 --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -925,6 +925,33 @@ static int ocfs2_validate_extent_block(struct super_bl= ock *sb, goto bail; } =20 + /* + * Extent blocks are allocated from a per-slot suballocator, so the + * slot must be in range. Otherwise freeing the block passes it to + * get_local_system_inode(), which hits BUG_ON() for + * OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. + */ + if ((u32)le16_to_cpu(eb->h_suballoc_slot) >=3D OCFS2_SB(sb)->max_slots) { + rc =3D ocfs2_error(sb, + "Extent block #%llu has an invalid h_suballoc_slot of %u\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(eb->h_suballoc_slot)); + goto bail; + } + + /* + * Similarly the suballoc bit must fit in a block group bitmap. + * Otherwise freeing the block will pass the oversized bit to + * _ocfs2_free_suballoc_bits() and trigger BUG_ON() there. + */ + if (le16_to_cpu(eb->h_suballoc_bit) >=3D ocfs2_suballoc_bits_per_block(sb= )) { + rc =3D ocfs2_error(sb, + "Extent block #%llu has an invalid h_suballoc_bit of %u\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(eb->h_suballoc_bit)); + goto bail; + } + if (le16_to_cpu(eb->h_list.l_count) !=3D ocfs2_extent_recs_per_eb(sb)) { rc =3D ocfs2_error(sb, "Extent block #%llu has invalid l_count %u (expected %u)\n", diff --git a/fs/ocfs2/refcounttree.c b/fs/ocfs2/refcounttree.c index d9f22b4a2654..3c8f2b64a8e1 100644 --- a/fs/ocfs2/refcounttree.c +++ b/fs/ocfs2/refcounttree.c @@ -117,6 +117,33 @@ static int ocfs2_validate_refcount_block(struct super_= block *sb, goto out; } =20 + /* + * Refcount blocks are allocated from a per-slot suballocator, so the + * slot must be in range. Otherwise freeing the block passes it to + * get_local_system_inode(), which hits BUG_ON() for + * OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. + */ + if ((u32)le16_to_cpu(rb->rf_suballoc_slot) >=3D OCFS2_SB(sb)->max_slots) { + rc =3D ocfs2_error(sb, + "Refcount block #%llu has an invalid rf_suballoc_slot of %u\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(rb->rf_suballoc_slot)); + goto out; + } + + /* + * Similarly the suballoc bit must fit in a block group bitmap. + * Otherwise freeing the block will pass the oversized bit to + * _ocfs2_free_suballoc_bits() and trigger BUG_ON() there. + */ + if (le16_to_cpu(rb->rf_suballoc_bit) >=3D ocfs2_suballoc_bits_per_block(s= b)) { + rc =3D ocfs2_error(sb, + "Refcount block #%llu has an invalid rf_suballoc_bit of %u\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(rb->rf_suballoc_bit)); + goto out; + } + /* * rf_records (rl_count/rl_used/rl_recs[]) is only meaningful when * this block is not an interior tree block (OCFS2_REFCOUNT_TREE_FL); --=20 2.39.3