jfs: validate dmap start before block allocation
syzbot
syzbotA corrupted JFS filesystem image can cause a state where the block allocation map (bmap) and the dmap pages are out of sync. Specifically, dbAllocNear() and dbAllocDmapLev() derive the returned block number from on-disk dp->start, which can cause duplicate allocation if dp->start does not match the selected dmap boundary. In this scenario, the allocator returns a block number that is already in use (e.g., as an inode table block or metadata) while marking a completely different block as allocated. When a thread later attempts to lock this duplicate block (for example, during a directory btree split), txLock() detects a metapage conflict because the block is already locked for another purpose, triggering a kernel BUG:
kernel BUG at fs/jfs/jfs_txnmgr.c:836!
Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI RIP: 0010:txLock+0x1cc3/0x1d10 fs/jfs/jfs_txnmgr.c:836 Call Trace: <TASK> dtSplitRoot+0x38d/0x18a0 fs/jfs/jfs_dtree.c:1924 dtSplitUp fs/jfs/jfs_dtree.c:990 [inline] dtInsert+0xeb2/0x5890 fs/jfs/jfs_dtree.c:868 jfs_create+0x730/0xae0 fs/jfs/namei.c:138
...
Validation of dp->start against the expected dmap boundary in dbAlloc() and the single-dmap path of dbAllocCtl() prevents inconsistent allocation. Rejecting a mismatched start with -EIO prevents duplicate allocations before txLock() is reached. Additionally, preserve the existing independent geometry check (dp->tree.budmin < 0) in dbAllocCtl(). Both a mismatched start and a negative budmin will now fail with -EIO early, preventing corrupted dmap structures from causing duplicate allocations and subsequent transaction lock crashes.
To: <[email protected]>
To: "Dave Kleikamp" <[email protected]>
Cc: "Kees Cook" <[email protected]>
Cc: <[email protected]>
Cc: "Yun Zhou" <[email protected]>
---
v7:
- Update commit description to explain that dbAllocNear() and dbAllocDmapLev() derive the returned block number from on-disk dp->start and clarify the validation in dbAlloc() and dbAllocCtl().
v6:
- Dropped Linus Torvalds from the To/Cc list.
https://lore.kernel.org/all/[email protected]/T/
v5:
- Narrowed the validation of dp->start to only the primary block allocation paths (dbAlloc and dbAllocCtl).
- Removed validation checks from dbFree, dbUpdatePMap, dbExtend, dbAllocBottomUp, and secondary loops in dbAllocCtl.
https://lore.kernel.org/all/[email protected]/T/
v4:
- Renamed dbValidateDmap to db_validate_dmap to follow kernel naming conventions.
- Preserved the existing dp->tree.budmin < 0 check in dbAllocCtl instead of replacing it.
https://lore.kernel.org/all/[email protected]/T/
v3:
- Renamed the patch to "jfs: validate dmap start before block map operations".
- Simplified dbValidateDmap() to only validate that the dmap dp->start matches the expected start block number, removing unproven integrity checks.
- Updated the commit description to explain the exact root cause of the selection/update mismatch and how validating dp->start prevents duplicate allocations before txLock() is reached.
https://lore.kernel.org/all/[email protected]/T/
v2:
- Replaced the approach of propagating errors from txLock() with proactive dmap validation.
- Introduced dbValidateDmap() to check dmap page integrity (nblocks, nfree, start address, and tree structure).
- Integrated dmap validation into dbFree(), dbUpdatePMap(), dbAlloc(), dbExtend(), dbAllocCtl(), dbAllocBottomUp(), and dbExtendFS().
- Reverted return type changes for functions in jfs_dtree.c and jfs_xtree.c from the previous version.
https://lore.kernel.org/all/[email protected]/T/
v1:
https://lore.kernel.org/all/[email protected]/T/
---
fs/jfs/jfs_dmap.c +24 -1
@@ -98,6 +98,9 @@ static int blkstol2(s64 nb);static int cntlz (u32 value);static int cnttz (u32 word); +static bool db_validate_dmap (struct super_block *sb,const struct dmap *dp, +s64 expected_start); +static int dbAllocDmapBU (struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks);static int dbInitDmap (struct dmap * dp,s64 blkno,int nblocks);
@@ -886,6 +889,12 @@ int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results) dp = (struct dmap *) mp->data; +if (!db_validate_dmap (ip->i_sb, dp, + blkno & ~(s64)(BPERDMAP -1 ))) { +release_metapage (mp); + goto read_unlock; + } +/* first, try to satisfy the allocation request with the * blocks beginning at the hint. */
@@ -1902,7 +1911,9 @@ dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results)return -EIO ; dp = (struct dmap *) mp->data; -if (dp->tree.budmin <0 ) { +if (dp->tree.budmin <0 || + !db_validate_dmap (bmp->db_ipbmap->i_sb, dp, + blkno & ~(s64)(BPERDMAP -1 ))) {release_metapage (mp);return -EIO ; }
@@ -2134,6 +2145,18 @@ static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,return (rc); } +static bool db_validate_dmap (struct super_block *sb,const struct dmap *dp, +s64 expected_start) +{ +if (le64_to_cpu (dp->start) != expected_start) { +jfs_error (sb,"corrupt dmap page: start %lld expected %lld\n" , + (long long )le64_to_cpu (dp->start), + (long long )expected_start); +return false; + } + +return true; +}/* * NAME: dbFreeDmap()
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].#syz upstream
Quoted 146 lines · syzbot
A corrupted JFS filesystem image can cause a state where the block allocation map (bmap) and the dmap pages are out of sync. Specifically,
dbAllocNear()anddbAllocDmapLev()derive the returned block number from on-diskdp->start, which can cause duplicate allocation ifdp->startdoes not match the selected dmap boundary. In this scenario, the allocator returns a block number that is already in use (e.g., as an inode table block or metadata) while marking a completely different block as allocated. When a thread later attempts to lock this duplicate block (for example, during a directory btree split),txLock()detects a metapage conflict because the block is already locked for another purpose, triggering a kernel BUG:kernel BUG at
fs/jfs/jfs_txnmgr.c:836!Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI RIP: 0010:txLock+0x1cc3/0x1d10 fs/jfs/jfs_txnmgr.c:836 Call Trace: <TASK> dtSplitRoot+0x38d/0x18a0 fs/jfs/jfs_dtree.c:1924 dtSplitUp fs/jfs/jfs_dtree.c:990 [inline] dtInsert+0xeb2/0x5890 fs/jfs/jfs_dtree.c:868 jfs_create+0x730/0xae0 fs/jfs/namei.c:138...
Validation of
dp->startagainst the expected dmap boundary indbAlloc()and the single-dmap path ofdbAllocCtl()prevents inconsistent allocation. Rejecting a mismatched start with-EIOprevents duplicate allocations beforetxLock()is reached. Additionally, preserve the existing independent geometry check (dp->tree.budmin< 0) indbAllocCtl(). Both a mismatched start and a negative budmin will now fail with-EIOearly, preventing corrupted dmap structures from causing duplicate allocations and subsequent transaction lock crashes.Assisted-by Gemini:gemini-3.8-flash syzbotReported-by [email protected]To: <[email protected]>
To: "Dave Kleikamp" <[email protected]>
Cc: "Kees Cook" <[email protected]>
Cc: <[email protected]>
Cc: "Yun Zhou" <[email protected]>---
v7:
- Update commit description to explain thatdbAllocNear()anddbAllocDmapLev()derive the returned block number from on-diskdp->startand clarify the validation indbAlloc()anddbAllocCtl().v6:
- Dropped Linus Torvalds from the To/Cc list.
https://lore.kernel.org/all/[email protected]/T/v5:
- Narrowed the validation ofdp->startto only the primary block allocation paths (dbAllocanddbAllocCtl).
- Removed validation checks fromdbFree,dbUpdatePMap,dbExtend,dbAllocBottomUp, and secondary loops indbAllocCtl.
https://lore.kernel.org/all/[email protected]/T/v4:
- Renamed dbValidateDmap todb_validate_dmapto follow kernel naming conventions.
- Preserved the existingdp->tree.budmin< 0 check in dbAllocCtl instead of replacing it.
https://lore.kernel.org/all/[email protected]/T/v3:
- Renamed the patch to "jfs: validate dmap start before block map operations".
- SimplifieddbValidateDmap()to only validate that the dmapdp->startmatches the expected start block number, removing unproven integrity checks.
- Updated the commit description to explain the exact root cause of the selection/update mismatch and how validatingdp->startprevents duplicate allocations beforetxLock()is reached.
https://lore.kernel.org/all/[email protected]/T/v2:
- Replaced the approach of propagating errors fromtxLock()with proactive dmap validation.
- IntroduceddbValidateDmap()to check dmap page integrity (nblocks, nfree, start address, and tree structure).
- Integrated dmap validation intodbFree(),dbUpdatePMap(),dbAlloc(),dbExtend(),dbAllocCtl(),dbAllocBottomUp(), anddbExtendFS().
- Reverted return type changes for functions injfs_dtree.candjfs_xtree.cfrom the previous version.
https://lore.kernel.org/all/[email protected]/T/v1:
https://lore.kernel.org/all/[email protected]/T/
---fs/jfs/jfs_dmap.c +24
-1@@ -98,6 +98,9 @@ static int blkstol2(s64 nb);static int cntlz (u32 value);static int cnttz (u32 word); +static bool db_validate_dmap (struct super_block *sb,const struct dmap *dp, +s64 expected_start); +static int dbAllocDmapBU (struct bmap * bmp,struct dmap * dp,s64 blkno,int nblocks);static int dbInitDmap (struct dmap * dp,s64 blkno,int nblocks);@@ -886,6 +889,12 @@ int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results) dp = (struct dmap *) mp->data; +if (!db_validate_dmap (ip->i_sb, dp, + blkno & ~(s64)(BPERDMAP -1 ))) { +release_metapage (mp); + goto read_unlock; + } +/* first, try to satisfy the allocation request with the * blocks beginning at the hint. */ @@ -1902,7 +1911,9 @@ dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results)return -EIO ; dp = (struct dmap *) mp->data; -if (dp->tree.budmin <0 ) { +if (dp->tree.budmin <0 || + !db_validate_dmap (bmp->db_ipbmap->i_sb, dp, + blkno & ~(s64)(BPERDMAP -1 ))) {release_metapage (mp);return -EIO ; }@@ -2134,6 +2145,18 @@ static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,return (rc); } +static bool db_validate_dmap (struct super_block *sb,const struct dmap *dp, +s64 expected_start) +{ +if (le64_to_cpu (dp->start) != expected_start) { +jfs_error (sb,"corrupt dmap page: start %lld expected %lld\n" , + (long long )le64_to_cpu (dp->start), + (long long )expected_start); +return false; + } + +return true; +}/* * NAME: dbFreeDmap() base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482Signature · 9 lines
This is an AI-generated patch subject to moderation. Reply with '#syz upstream' to Sign-off the patch as a human author and send it to the upstream kernel mailing lists. Reply with '#syz reject' to reject it ('#syz unreject' to undo). See https://goo.gle/syzbot-ai-patches for information about AI-generated patches. You can comment on the patch as usual, syzbot will try to address the comments and send a new version of the patch if necessary. syzbot engineers can be reached at [email protected].