From: Hui Peng <benquike@gmail.com>
To: shaggy@kernel.org, brauner@kernel.org
Cc: jfs-discussion@lists.sourceforge.net,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] jfs: validate dmap parameters in dbMount() and slot chains in jfs_dtree.c
Date: Sat, 19 Sep 2026 22:26:02 +0000 [thread overview]
Message-ID: <20260919222602.3794171-1-benquike@gmail.com> (raw)
Fix two filesystem corruption crashes in fs/jfs/:
1. In dbMount() (fs/jfs/jfs_dmap.c), validate db_mapsize > 0, db_numag
<= MAXAG, db_agheight <= MAX_AGHEIGHT, and db_agwidth > 0 to prevent
out-of-bounds array indexing on bmp->db_agfree[] and shift UB.
2. In check_dtroot(), check_dtpage(), and jfs_readdir()
(fs/jfs/jfs_dtree.c), validate directory slot continuation indices to
prevent cyclic or out-of-bounds slot walks.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@gmail.com>
---
diff --git a/fs/jfs/jfs_dmap.c b/fs/jfs/jfs_dmap.c
index a841cf21da7d..f7d6efe8cec8 100644
--- a/fs/jfs/jfs_dmap.c
+++ b/fs/jfs/jfs_dmap.c
@@ -288,7 +288,8 @@ int dbMount(struct inode *ipbmap)
(bmp->db_agstart > (CTLTREESIZE - 1 - bmp->db_agwidth * (MAXAG - 1))) ||
(bmp->db_agl2size > L2MAXL2SIZE - L2MAXAG) ||
(bmp->db_agl2size < 0) ||
- ((bmp->db_mapsize - 1) >> bmp->db_agl2size) > MAXAG) {
+ (bmp->db_mapsize <= 0) ||
+ (((bmp->db_mapsize - 1) >> bmp->db_agl2size) >= MAXAG)) {
err = -EINVAL;
goto err_release_metapage;
}
diff --git a/fs/jfs/jfs_dtree.c b/fs/jfs/jfs_dtree.c
index 8ce6e4458cc2..0a5361804155 100644
--- a/fs/jfs/jfs_dtree.c
+++ b/fs/jfs/jfs_dtree.c
@@ -2968,6 +2968,17 @@ int jfs_readdir(struct file *file, struct dir_context *ctx)
/* copy name in the additional segment(s) */
next = d->next;
while (next >= 0) {
+ int max_slot = (p->header.flag & BT_ROOT) ?
+ DTROOTMAXSLOT : p->header.maxslot;
+
+ if (unlikely(next < 1 || next >= max_slot)) {
+ jfs_error(ip->i_sb,
+ "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
+ (long)ip->i_ino,
+ (long long)bn,
+ i);
+ goto skip_one;
+ }
t = (struct dtslot *) & p->slot[next];
name_ptr += outlen;
d_namleft -= len;
@@ -4343,6 +4354,10 @@ bool check_dtroot(dtroot_t *p)
}
/* The last node in the free list must terminate with next = -1 */
+ if (unlikely(__test_and_set_bit(fsi, bitmap))) {
+ jfs_err("duplicate index%d in slot in dtroot\n", fsi);
+ return false;
+ }
if (unlikely(p->slot[fsi].next != -1)) {
jfs_err("Bad next:%d of the last slot in dtroot\n",
p->slot[fsi].next);
@@ -4376,6 +4391,21 @@ bool check_dtroot(dtroot_t *p)
jfs_err("Duplicate index:%d in stbl in dtroot\n", idx);
return false;
}
+
+ if (idx > 0) {
+ int next = (p->header.flag & BT_LEAF) ?
+ ((struct ldtentry *)&p->slot[idx])->next :
+ ((struct idtentry *)&p->slot[idx])->next;
+
+ while (next >= 0) {
+ if (unlikely(next < 1 || next >= DTROOTMAXSLOT ||
+ __test_and_set_bit(next, bitmap))) {
+ jfs_err("Bad continuation slot:%d in dtroot\n", next);
+ return false;
+ }
+ next = p->slot[next].next;
+ }
+ }
}
return true;
@@ -4436,6 +4466,10 @@ bool check_dtpage(dtpage_t *p)
}
/* The last node in the free list must terminate with next = -1 */
+ if (unlikely(__test_and_set_bit(fsi, bitmap))) {
+ jfs_err("duplicate index%d in slot in dtpage\n", fsi);
+ return false;
+ }
if (unlikely(p->slot[fsi].next != -1)) {
jfs_err("Bad next:%d of the last slot in dtpage\n",
p->slot[fsi].next);
@@ -4464,6 +4498,7 @@ bool check_dtpage(dtpage_t *p)
*/
for (i = 0; i < p->header.nextindex; i++) {
int idx = DT_GETSTBL(p)[i];
+ int next;
/* Check if index is out of valid data slot range */
if (unlikely(idx < 1 || idx >= DTPAGEMAXSLOT)) {
@@ -4477,6 +4512,18 @@ bool check_dtpage(dtpage_t *p)
jfs_err("Duplicate index:%d in stbl of dtpage\n", idx);
return false;
}
+
+ next = (p->header.flag & BT_LEAF) ?
+ ((struct ldtentry *)&p->slot[idx])->next :
+ ((struct idtentry *)&p->slot[idx])->next;
+ while (next >= 0) {
+ if (unlikely(next < 1 || next >= DTPAGEMAXSLOT ||
+ __test_and_set_bit(next, bitmap))) {
+ jfs_err("Bad continuation slot:%d in dtpage\n", next);
+ return false;
+ }
+ next = p->slot[next].next;
+ }
}
return true;
reply other threads:[~2026-09-19 22:26 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260919222602.3794171-1-benquike@gmail.com \
--to=benquike@gmail.com \
--cc=brauner@kernel.org \
--cc=jfs-discussion@lists.sourceforge.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shaggy@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®