Forwarded: [PATCH] ext4: don't append a directory block already mapped in the inode

From: syzbot

Date: Sun Oct 04 2026 - 21:01:53 EST


For archival purposes, forwarding an incoming command email to
linux-kernel@xxxxxxxxxxxxxxx.

***

Subject: [PATCH] ext4: don't append a directory block already mapped in the inode
Author: adrianox@xxxxxxxxx

#syz test: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
ext4_append() grows a directory by one block. It checks that the target
logical block is a hole, but a corrupt block bitmap can still make the
allocator hand back a physical block that is already in use by this
inode. The in-memory copy of a block is keyed by its physical block
number, so the "new" block and that existing one are the same memory;
callers that split a directory (make_indexed_dir()/do_split()) then move
entries between two aliased buffers and corrupt the directory, until a
bogus rec_len read from the middle of a name runs the wipe out of bounds:

BUG: KASAN: slab-use-after-free in dx_move_dirents [inline]
Write of size 90458 ...

Reject the block and report the corrupt bitmap instead of corrupting
memory.

Closes: https://syzkaller.appspot.com/bug?extid=09bec78ee77613a3efdd
---
fs/ext4/namei.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 3b9740c1c16d..7a0dadf6c5fe 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -83,6 +83,25 @@ static struct buffer_head *ext4_append(handle_t *handle,
bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
if (IS_ERR(bh))
return bh;
+
+ for (map.m_lblk = 0; map.m_lblk < *block; map.m_lblk += map.m_len) {
+ map.m_len = *block - map.m_lblk;
+ err = ext4_map_blocks(NULL, inode, &map, 0);
+ if (err < 0) {
+ brelse(bh);
+ return ERR_PTR(err);
+ }
+ if (err > 0 && bh->b_blocknr - map.m_pblk < map.m_len) {
+ EXT4_ERROR_INODE(inode,
+ "new block %llu already mapped",
+ (unsigned long long)bh->b_blocknr);
+ brelse(bh);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+ if (map.m_len < 1)
+ map.m_len = 1;
+ }
+
inode->i_size += inode->i_sb->s_blocksize;
EXT4_I(inode)->i_disksize = inode->i_size;
err = ext4_mark_inode_dirty(handle, inode);
--
2.51.0