This is a note to let you know that I've just added the patch titled ext4: make sure ext4_append() always allocates new block to the 4.19-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: ext4-make-sure-ext4_append-always-allocates-new-block.patch and it can be found in the queue-4.19 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From b8a04fe77ef1360fbf73c80fddbdfeaa9407ed1b Mon Sep 17 00:00:00 2001 From: Lukas Czerner <lczerner@xxxxxxxxxx> Date: Mon, 4 Jul 2022 16:27:21 +0200 Subject: ext4: make sure ext4_append() always allocates new block From: Lukas Czerner <lczerner@xxxxxxxxxx> commit b8a04fe77ef1360fbf73c80fddbdfeaa9407ed1b upstream. ext4_append() must always allocate a new block, otherwise we run the risk of overwriting existing directory block corrupting the directory tree in the process resulting in all manner of problems later on. Add a sanity check to see if the logical block is already allocated and error out if it is. Cc: stable@xxxxxxxxxx Signed-off-by: Lukas Czerner <lczerner@xxxxxxxxxx> Reviewed-by: Andreas Dilger <adilger@xxxxxxxxx> Link: https://lore.kernel.org/r/20220704142721.157985-2-lczerner@xxxxxxxxxx Signed-off-by: Theodore Ts'o <tytso@xxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/ext4/namei.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -53,6 +53,7 @@ static struct buffer_head *ext4_append(h struct inode *inode, ext4_lblk_t *block) { + struct ext4_map_blocks map; struct buffer_head *bh; int err; @@ -62,6 +63,21 @@ static struct buffer_head *ext4_append(h return ERR_PTR(-ENOSPC); *block = inode->i_size >> inode->i_sb->s_blocksize_bits; + map.m_lblk = *block; + map.m_len = 1; + + /* + * We're appending new directory block. Make sure the block is not + * allocated yet, otherwise we will end up corrupting the + * directory. + */ + err = ext4_map_blocks(NULL, inode, &map, 0); + if (err < 0) + return ERR_PTR(err); + if (err) { + EXT4_ERROR_INODE(inode, "Logical block already allocated"); + return ERR_PTR(-EFSCORRUPTED); + } bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE); if (IS_ERR(bh)) Patches currently in stable-queue which might be from lczerner@xxxxxxxxxx are queue-4.19/ext4-make-sure-ext4_append-always-allocates-new-block.patch