Mounting a crafted JFS image can trigger a kernel BUG in txLock(), causing a denial of service. Two images for this report contain different inconsistent aggregate metadata that passes existing checks. In one image, the first dmap page records dp->start as 1792 although its logical dmap boundary is 0. dbAllocNear() and dbAllocDmapLev() use dp->start to form block numbers from free dmap leaves. dbAllocBits() then sets a bit indexed by that block number. With the shifted start, the bit can differ from the free leaf used to select the block, and an already allocated block can be returned. In the other image, aggregate inode-table entries for BMAP_I and FILESYSTEM_I record filesets 2 and 16 instead of AGGREGATE_I. diReadSpecial() accepts those values. diWrite() later passes the fileset-map inode to txLock(), whose waitLock path invokes BUG() on a lock collision when that inode's fileset is not AGGREGATE_I. Check dp->start against the selected dmap boundary before allocation and return -EIO on a mismatch. Also reject an aggregate special inode whose fileset is not AGGREGATE_I before mount can use it. The dmap checks were adapted from syzbot AI patch v7. Reported-by: syzbot+a843f6ae2130a987d63b@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=a843f6ae2130a987d63b Link: https://syzkaller.appspot.com/ai_job?id=922ceb61-4be2-4af9-b2fe-9977a2549134 Signed-off-by: Bartosz Chronowski --- Changes in v2: - Rebased onto mainline 9437ee8b6cf6. - Reject invalid fileset values in aggregate special inodes at mount; the existing dmap start guard alone missed a second txLock path. - Clarify the two distinct malformed metadata paths. Testing: - On unpatched mainline, two syzbot C reproducers triggered the txLock BUG in 4/4 and 1/4 VMs, respectively. - With this patch, neither triggered the target BUG in 4/4 VMs; each syz-crush run had an effective 270-second C-repro cap. - Direct guest runs showed the aggregate inode image rejected at mount with -EIO and the other image rejected by the dmap check. v1: https://lore.kernel.org/all/b63dd8ae-464b-40d7-bbb8-031cfdc90909@mail.kernel.org/ fs/jfs/jfs_dmap.c | 25 ++++++++++++++++++++++++- fs/jfs/jfs_imap.c | 5 +++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/fs/jfs/jfs_dmap.c b/fs/jfs/jfs_dmap.c index a841cf21da7d..9bdbc55d8f90 100644 --- a/fs/jfs/jfs_dmap.c +++ b/fs/jfs/jfs_dmap.c @@ -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() diff --git a/fs/jfs/jfs_imap.c b/fs/jfs/jfs_imap.c index b84ba4d7dfb4..033f3352a114 100644 --- a/fs/jfs/jfs_imap.c +++ b/fs/jfs/jfs_imap.c @@ -456,8 +456,9 @@ struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary) dp += inum % 8; /* 8 inodes per 4K page */ /* copy on-disk inode to in-memory inode */ - if ((copy_from_dinode(dp, ip) != 0) || (ip->i_nlink == 0)) { - /* handle bad return by returning NULL for ip */ + if ((copy_from_dinode(dp, ip) != 0) || + JFS_IP(ip)->fileset != AGGREGATE_I || (ip->i_nlink == 0)) { + /* reject an invalid special inode */ set_nlink(ip, 1); /* Don't want iput() deleting it */ iput(ip); /* release the page */ base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48 -- 2.43.0