From nobody Tue Dec 2 01:51:41 2025 Received: from mx0b-0064b401.pphosted.com (mx0b-0064b401.pphosted.com [205.220.178.238]) (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 EBC352FCC1D for ; Thu, 20 Nov 2025 15:45:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.178.238 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763653513; cv=none; b=MUQ0QGWjr2qfDW3YjXe8M+z/38tjvQgZB5Gk1x4eHeTmWe1BDEGUHFr7DHgyDNNtHNONzOmMw4jTGkuChXsn/bnXAUHwnF6TnESVemCDCE2wmNtwvwp819jDc0cwYVU92/FORXigbdNOyYkG2IA4UXzOkdbR83xRO/1Yrff0DSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763653513; c=relaxed/simple; bh=x6uOePiNeDmaPUsT/H6+VezYqiT1O/E8Dod5P7lmsvg=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Dg6fvH87bOw2bwvw60Fzb0Hx1llwA2Eh9FUGSUWuxKGAXVwTIyWBZhGemxb9MclA7yEqojCPQXb57DvqB6PTRQHNXlXd1XJmZKgFvTFtFgNeIbqdHCKY+dgo9/ldN3bHEwzIMjM6W7cWDAuc2QvgVJAlCRzufsZIEL/6wN23y4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=windriver.com; spf=pass smtp.mailfrom=windriver.com; dkim=pass (2048-bit key) header.d=windriver.com header.i=@windriver.com header.b=bwZzsGu7; arc=none smtp.client-ip=205.220.178.238 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=windriver.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=windriver.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=windriver.com header.i=@windriver.com header.b="bwZzsGu7" Received: from pps.filterd (m0250812.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 5AKBLLto1396429; Thu, 20 Nov 2025 15:44:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriver.com; h=cc:content-transfer-encoding:content-type:date:from :message-id:mime-version:subject:to; s=PPS06212021; bh=rO26f2Oma c4xoQJyT9Ehp1ex0mERXIZvq25qjo7zG1E=; b=bwZzsGu7PYMR5znOOZEMz1qdo DI5uNZgkSXWd3AT1+HCD66Sk7TBO0Kido8Zs/g6Xu6YszQ9YXZG5vpPBQVUZeMFB aQZ4xryOIPe/ZCI4ngh8qHbAQwnQsScjvppVaWW8yfIQC32CJH1IQNDaZQImLaWe c95pAg7i3yOgta+nn7LsEmZMFH7fPbQLKnE4GqsA2gUtcpVgvkFvjmi3GluS3ej1 oa3Y5nTt1JhCGO0fB7sVociBsHHVaFaRGrrfu2fYxfQUfg6Z14wAcN98JTah9ktV 04uytlPUVI04wPNx0MyPHLGuXYWXOxtSXyB59vCwj7tkiSWR01rm3e5ZLnAqg== Received: from ala-exchng02.corp.ad.wrs.com ([128.224.246.37]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 4ahayrsu6p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Thu, 20 Nov 2025 15:44:07 +0000 (GMT) Received: from ALA-EXCHNG02.corp.ad.wrs.com (10.11.224.122) by ALA-EXCHNG02.corp.ad.wrs.com (10.11.224.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.61; Thu, 20 Nov 2025 07:44:03 -0800 Received: from pek-lpd-ccm6.wrs.com (10.11.232.110) by ALA-EXCHNG02.corp.ad.wrs.com (10.11.224.122) with Microsoft SMTP Server id 15.1.2507.61 via Frontend Transport; Thu, 20 Nov 2025 07:44:01 -0800 From: Yun Zhou To: CC: , , , , , , , Subject: [PATCH] jfs: add dtpage integrity check to prevent index/pointer overflows Date: Thu, 20 Nov 2025 23:44:00 +0800 Message-ID: <20251120154400.1042123-1-yun.zhou@windriver.com> X-Mailer: git-send-email 2.43.0 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 X-Proofpoint-ORIG-GUID: xwOHKPi7LHb5bj0QVLymZTJ4fgEgNCOq X-Authority-Analysis: v=2.4 cv=EdXFgfmC c=1 sm=1 tr=0 ts=691f3747 cx=c_pps a=Lg6ja3A245NiLSnFpY5YKQ==:117 a=Lg6ja3A245NiLSnFpY5YKQ==:17 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=t7CeM3EgAAAA:8 a=u9DlrU1ZzlIu95ImLY8A:9 a=FdTzh2GWekK77mhwV6Dw:22 X-Proofpoint-GUID: xwOHKPi7LHb5bj0QVLymZTJ4fgEgNCOq X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTIwMDEwMyBTYWx0ZWRfX1JHt3JHClkBF N5Jz8vYZRuApXz7aZON5tSJkuECuMz9H8UTB5Ye/+keDSENLDi4Gr6XC2rNTk/eP+9/Mt7/jWk8 263NST8Pu34vrVStwfRZnwQ1+ZCL5B5Pu16i4OU3QViXnFtc2be87bFDapIWVpRFj3bGvI0giWc tmkphJjFBjkl6hGkPeUdKxfSVcDpHliphl7uL//bDNa25tJr8ynsMp6NNwyiUi1f2RR28jMT6WM pVCe9jdSQy3tcqB3BJo9Wh1APHrd0DyUPwszXRA4Bo6zGYlBUMSPvwhUVsDsp9mwe76bTzMqGgD dNMTDYCsRnqCVP4OVqRvaA9Yg6ITkwhNFLlQ8C+tK5HJg49KQBRcQgS9cqd6kqNENEWY4WsFyWu XfJXr6JIbxkJJPbqEXt5i9jsTIy2Kg== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-20_05,2025-11-20_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 adultscore=0 suspectscore=0 bulkscore=0 impostorscore=0 phishscore=0 malwarescore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2510240001 definitions=main-2511200103 Content-Type: text/plain; charset="utf-8" Add check_dtpage() to validate dtpage_t integrity, focusing on preventing index/pointer overflows from on-disk corruption. Key checks: - maxslot must be exactly DTPAGEMAXSLOT (128) as defined for dtpage slot array. - freecnt bounded by [0, DTPAGEMAXSLOT-1] (slot[0] reserved for header). - freelist validity: -1 when freecnt=3D0; 1~DTPAGEMAXSLOT-1 when non-zero, with linked list checks (no duplicates, proper termination via next=3D-1). - stblindex bounds: must be within range that avoids overlapping with stbl itself (stblindex < DTPAGEMAXSLOT - stblsize). - nextindex bounded by stbl size (stblsize << L2DTSLOTSIZE). stbl entries validity: within 1~DTPAGEMAXSLOT-1, no duplicates(excluding invalid entries marked as -1). Invoked when loading dtpage (in BT_GETPAGE macro context) to catch corruption early before directory operations trigger out-of-bounds access. Signed-off-by: Yun Zhou --- fs/jfs/jfs_dtree.c | 108 +++++++++++++++++++++++++++++++++++++++++++-- fs/jfs/jfs_dtree.h | 2 + 2 files changed, 106 insertions(+), 4 deletions(-) diff --git a/fs/jfs/jfs_dtree.c b/fs/jfs/jfs_dtree.c index e1cbc3a04f3f..6df4a3ed9043 100644 --- a/fs/jfs/jfs_dtree.c +++ b/fs/jfs/jfs_dtree.c @@ -115,10 +115,7 @@ struct dtsplit { do { \ BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \ if (!(RC)) { \ - if (((P)->header.nextindex > \ - (((BN) =3D=3D 0) ? DTROOTMAXSLOT : (P)->header.maxslot)) || \ - ((BN) && (((P)->header.maxslot > DTPAGEMAXSLOT) || \ - ((P)->header.stblindex >=3D DTPAGEMAXSLOT)))) { \ + if ((BN) && !check_dtpage(P)) { \ BT_PUTPAGE(MP); \ jfs_error((IP)->i_sb, \ "DT_GETPAGE: dtree page corrupt\n"); \ @@ -4399,3 +4396,106 @@ bool check_dtroot(dtroot_t *p) =20 return true; } + +bool check_dtpage(dtpage_t *p) +{ + DECLARE_BITMAP(bitmap, DTPAGEMAXSLOT) =3D {0}; + const int stblsize =3D ((PSIZE >> L2DTSLOTSIZE) + 31) >> L2DTSLOTSIZE; + int i; + + /* Validate maxslot (maximum number of slots in the page) + * dtpage_t slot array is defined to hold up to DTPAGEMAXSLOT (128) slots + */ + if (unlikely(p->header.maxslot !=3D DTPAGEMAXSLOT)) { + jfs_err("Bad maxslot:%d in dtpage (expected %d)\n", + p->header.maxslot, DTPAGEMAXSLOT); + return false; + } + + /* freecnt cannot be negative or exceed DTPAGEMAXSLOT-1 + * (since slot[0] is occupied by the header). + */ + if (unlikely(p->header.freecnt < 0 || + p->header.freecnt > DTPAGEMAXSLOT - 1)) { + jfs_err("Bad freecnt:%d in dtpage\n", p->header.freecnt); + return false; + } else if (p->header.freecnt =3D=3D 0) { + /* No free slots: freelist must be -1 */ + if (unlikely(p->header.freelist !=3D -1)) { + jfs_err("freecnt=3D0 but freelist=3D%d in dtpage\n", + p->header.freelist); + return false; + } + } else { + int fsi; + /* When there are free slots, freelist must be a valid slot index in + * 1~DTROOTMAXSLOT-1(since slot[0] is occupied by the header). + */ + if (unlikely(p->header.freelist < 1 || + p->header.freelist >=3D DTPAGEMAXSLOT)) { + jfs_err("Bad freelist:%d in dtpage\n", p->header.freelist); + return false; + } + + /* Traverse the free list to check validity of all node indices */ + fsi =3D p->header.freelist; + for (i =3D 0; i < p->header.freecnt - 1; i++) { + /* Check for duplicate indices in the free list */ + if (unlikely(__test_and_set_bit(fsi, bitmap))) { + jfs_err("duplicate index%d in slot in dtpage\n", fsi); + return false; + } + fsi =3D p->slot[fsi].next; + + /* Ensure the next slot index in the free list is valid */ + if (unlikely(fsi < 1 || fsi >=3D DTPAGEMAXSLOT)) { + jfs_err("Bad index:%d in slot in dtpage\n", fsi); + return false; + } + } + + /* The last node in the free list must terminate with next =3D -1 */ + if (unlikely(p->slot[fsi].next !=3D -1)) { + jfs_err("Bad next:%d of the last slot in dtpage\n", + p->slot[fsi].next); + return false; + } + } + + /* stbl must be little then DTPAGEMAXSLOT */ + if (unlikely(p->header.stblindex >=3D DTPAGEMAXSLOT - stblsize)) { + jfs_err("Bad stblindex:%d in dtpage (stbl size %d)\n", + p->header.stblindex, stblsize); + return false; + } + + /* nextindex must be little then stblsize*32 */ + if (unlikely(p->header.nextindex >=3D (stblsize << L2DTSLOTSIZE))) { + jfs_err("Bad nextindex:%d in dtpage (stbl size %d)\n", + p->header.nextindex, stblsize); + return false; + } + + /* Validate stbl entries + * Each entry is a slot index, valid range: -1 (invalid) or [0, nextindex= -1] (valid data slots) + * (stblindex and higher slots are reserved for stbl itself) + */ + for (i =3D 0; i < p->header.nextindex; i++) { + int idx =3D DT_GETSTBL(p)[i]; + + /* Check if index is out of valid data slot range */ + if (unlikely(idx < 1 || idx >=3D DTPAGEMAXSLOT)) { + jfs_err("Bad stbl[%d] index:%d (stblindex %d) in dtpage\n", + i, idx, p->header.stblindex); + return false; + } + + /* Check for duplicate valid indices (skip -1) */ + if (unlikely(__test_and_set_bit(idx, bitmap))) { + jfs_err("Duplicate index:%d in stbl of dtpage\n", idx); + return false; + } + } + + return true; +} diff --git a/fs/jfs/jfs_dtree.h b/fs/jfs/jfs_dtree.h index 94dc16123c87..dfc87b6690a9 100644 --- a/fs/jfs/jfs_dtree.h +++ b/fs/jfs/jfs_dtree.h @@ -255,4 +255,6 @@ extern int dtModify(tid_t tid, struct inode *ip, struct= component_name * key, extern int jfs_readdir(struct file *file, struct dir_context *ctx); =20 extern bool check_dtroot(dtroot_t *p); + +extern bool check_dtpage(dtpage_t *p); #endif /* !_H_JFS_DTREE */ --=20 2.34.1