ext4_xattr_block_set() shares an identical xattr block if the mbcache has one. ext4_xattr_block_cache_find() reads each candidate with ext4_sb_bread() and hands it to ext4_xattr_cmp(), which compares every value at the candidate's own offset: if (!entry1->e_value_inum && memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), (char *)header2 + le16_to_cpu(entry2->e_value_offs), le32_to_cpu(entry1->e_value_size))) A block goes into the mbcache after ext4_xattr_check_block() has checked it, or when ext4 has just written it, but the mbcache entry outlives the buffer. Once the clean buffer has been reclaimed, the lookup reads the block from disk again and nothing checks that copy. If the block has changed on disk meanwhile - on a crafted image, by a write to a regular file whose extent points at the same block - e_value_offs can be anything up to 65535, and memcmp() reads that far past the block: BUG: KASAN: use-after-free in memcmp (lib/string.c:665) Read of size 8 at addr ffff888027cfa3c0 by task poc/9468 memcmp (lib/string.c:665) ext4_xattr_cmp (fs/ext4/xattr.c:3153) [inline] ext4_xattr_block_cache_find (fs/ext4/xattr.c:3199) [inline] ext4_xattr_block_set (fs/ext4/xattr.c:2074) ext4_xattr_set_handle (fs/ext4/xattr.c:2518) ext4_xattr_set (fs/ext4/xattr.c:2622) The buggy address belongs to the physical page: page_type: f0(buddy) A candidate that compares equal is shared without having been checked at all. Check the candidate with ext4_xattr_check_block() before comparing it, as every other reader of an xattr block does, and fail the setxattr() with its error, as the read error just above already does. A buffer still marked verified only gets the magic and h_blocks test, so a block that has stayed in memory costs no more than before. We found this with a syzkaller-based fuzzing harness driven by an AI tool. Fixes: ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3") Closes: https://lore.kernel.org/r/ext4-xattr-cache-cmp.20261206@iiitd.ac.in/ Cc: stable@vger.kernel.org Signed-off-by: Kapil Vaswani Signed-off-by: Piyus Kedia --- Tested on ksmbd-for-next (d70570cf8003), whose fs/ext4 is identical to mainline master (9437ee8b6cf6) and to ext4/dev (aceadbccd428), with KASAN, the same config before and after. A 1 KiB-block image in which one file's extent points at another file's xattr block; then, through the mounted filesystem, listxattr, a write to that file, drop_caches, and setxattr of the same xattr on a new file: before KASAN use-after-free/slab-out-of-bounds in memcmp() from ext4_xattr_cmp() in 6 of 7 boots (as root, as uid 65534 - with no root after the mount when the buffer is dropped through the user's own cgroup's memory.reclaim - and with metadata_csum); the fuzzer's way (rewriting the image file while mounted) in 2 of 3, once as the file's owner after 2 boots, 240 such setxattr()s and the fuzzer's two programs: no report; EUCLEAN and "corrupted xattr block 660: e_value out of bounds" (with metadata_csum: EBADMSG, "invalid checksum") control the block written back unchanged: shared as before, on both kernels fs/ext4/xattr.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c index 277731c424a0..cd90595acb99 100644 --- a/fs/ext4/xattr.c +++ b/fs/ext4/xattr.c @@ -3188,6 +3188,7 @@ ext4_xattr_block_cache_find(struct inode *inode, ce = mb_cache_entry_find_first(ea_block_cache, hash); while (ce) { struct buffer_head *bh; + int error; bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO); if (IS_ERR(bh)) { @@ -3196,7 +3197,20 @@ ext4_xattr_block_cache_find(struct inode *inode, (unsigned long)ce->e_value); mb_cache_entry_put(ea_block_cache, ce); return bh; - } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { + } + /* + * The mbcache entry outlives the buffer: if the buffer was + * dropped since the block was checked, this is a fresh read + * that nothing has checked, and ext4_xattr_cmp() trusts the + * value offsets. + */ + error = ext4_xattr_check_block(inode, bh); + if (error) { + brelse(bh); + mb_cache_entry_put(ea_block_cache, ce); + return ERR_PTR(error); + } + if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { *pce = ce; return bh; } -- 2.34.1