Message ID | 20220704142721.157985-2-lczerner@redhat.com |
---|---|
State | Awaiting Upstream |
Headers | show |
Series | [1/2] ext4: check if directory block is within i_size | expand |
On Jul 4, 2022, at 8:27 AM, Lukas Czerner <lczerner@redhat.com> wrote: > > 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. > > Signed-off-by: Lukas Czerner <lczerner@redhat.com> Reviewed-by: Andreas Dilger <adilger@dilger.ca> > --- > fs/ext4/namei.c | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c > index cf460aa4f81d..4af441494e09 100644 > --- a/fs/ext4/namei.c > +++ b/fs/ext4/namei.c > @@ -54,6 +54,7 @@ static struct buffer_head *ext4_append(handle_t *handle, > struct inode *inode, > ext4_lblk_t *block) > { > + struct ext4_map_blocks map; > struct buffer_head *bh; > int err; > > @@ -63,6 +64,21 @@ static struct buffer_head *ext4_append(handle_t *handle, > 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)) > -- > 2.35.3 > Cheers, Andreas
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index cf460aa4f81d..4af441494e09 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -54,6 +54,7 @@ static struct buffer_head *ext4_append(handle_t *handle, struct inode *inode, ext4_lblk_t *block) { + struct ext4_map_blocks map; struct buffer_head *bh; int err; @@ -63,6 +64,21 @@ static struct buffer_head *ext4_append(handle_t *handle, 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))
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. Signed-off-by: Lukas Czerner <lczerner@redhat.com> --- fs/ext4/namei.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)