Skip to content

Commit 6318454

Browse files
Lukas Czernersmb49
authored andcommitted
ext4: make sure ext4_append() always allocates new block
BugLink: https://bugs.launchpad.net/bugs/1990190 commit b8a04fe 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@kernel.org Signed-off-by: Lukas Czerner <lczerner@redhat.com> Reviewed-by: Andreas Dilger <adilger@dilger.ca> Link: https://lore.kernel.org/r/20220704142721.157985-2-lczerner@redhat.com Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Kamal Mostafa <kamal@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 6310586 commit 6318454

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

fs/ext4/namei.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static struct buffer_head *ext4_append(handle_t *handle,
5454
struct inode *inode,
5555
ext4_lblk_t *block)
5656
{
57+
struct ext4_map_blocks map;
5758
struct buffer_head *bh;
5859
int err;
5960

@@ -63,6 +64,21 @@ static struct buffer_head *ext4_append(handle_t *handle,
6364
return ERR_PTR(-ENOSPC);
6465

6566
*block = inode->i_size >> inode->i_sb->s_blocksize_bits;
67+
map.m_lblk = *block;
68+
map.m_len = 1;
69+
70+
/*
71+
* We're appending new directory block. Make sure the block is not
72+
* allocated yet, otherwise we will end up corrupting the
73+
* directory.
74+
*/
75+
err = ext4_map_blocks(NULL, inode, &map, 0);
76+
if (err < 0)
77+
return ERR_PTR(err);
78+
if (err) {
79+
EXT4_ERROR_INODE(inode, "Logical block already allocated");
80+
return ERR_PTR(-EFSCORRUPTED);
81+
}
6682

6783
bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
6884
if (IS_ERR(bh))

0 commit comments

Comments
 (0)