* [PATCH 1/2] ext4: check if directory block is within i_size
@ 2022-07-04 14:27 Lukas Czerner
2022-07-04 14:27 ` [PATCH 2/2] ext4: make sure ext4_append() always allocates new block Lukas Czerner
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Lukas Czerner @ 2022-07-04 14:27 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, stable
Currently ext4 directory handling code implicitly assumes that the
directory blocks are always within the i_size. In fact ext4_append()
will attempt to allocate next directory block based solely on i_size and
the i_size is then appropriately increased after a successful
allocation.
However, for this to work it requires i_size to be correct. If, for any
reason, the directory inode i_size is corrupted in a way that the
directory tree refers to a valid directory block past i_size, we could
end up corrupting parts of the directory tree structure by overwriting
already used directory blocks when modifying the directory.
Fix it by catching the corruption early in __ext4_read_dirblock().
Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Addresses Red-Hat-Bugzilla: #2070205
Cc: stable@vger.kernel.org
---
fs/ext4/namei.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index db4ba99d1ceb..cf460aa4f81d 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -110,6 +110,13 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
struct ext4_dir_entry *dirent;
int is_dx_block = 0;
+ if (block >= inode->i_size) {
+ ext4_error_inode(inode, func, line, block,
+ "Attempting to read directory block (%u) that is past i_size (%llu)",
+ block, inode->i_size);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+
if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
bh = ERR_PTR(-EIO);
else
--
2.35.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2] ext4: make sure ext4_append() always allocates new block
2022-07-04 14:27 [PATCH 1/2] ext4: check if directory block is within i_size Lukas Czerner
@ 2022-07-04 14:27 ` Lukas Czerner
2022-07-09 17:32 ` Andreas Dilger
2022-07-09 17:31 ` [PATCH 1/2] ext4: check if directory block is within i_size Andreas Dilger
2022-07-22 13:58 ` Theodore Ts'o
2 siblings, 1 reply; 5+ messages in thread
From: Lukas Czerner @ 2022-07-04 14:27 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso
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(+)
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] ext4: check if directory block is within i_size
2022-07-04 14:27 [PATCH 1/2] ext4: check if directory block is within i_size Lukas Czerner
2022-07-04 14:27 ` [PATCH 2/2] ext4: make sure ext4_append() always allocates new block Lukas Czerner
@ 2022-07-09 17:31 ` Andreas Dilger
2022-07-22 13:58 ` Theodore Ts'o
2 siblings, 0 replies; 5+ messages in thread
From: Andreas Dilger @ 2022-07-09 17:31 UTC (permalink / raw)
To: Lukas Czerner; +Cc: Ext4 Developers List, Theodore Ts'o, stable
[-- Attachment #1: Type: text/plain, Size: 1766 bytes --]
On Jul 4, 2022, at 8:27 AM, Lukas Czerner <lczerner@redhat.com> wrote:
>
> Currently ext4 directory handling code implicitly assumes that the
> directory blocks are always within the i_size. In fact ext4_append()
> will attempt to allocate next directory block based solely on i_size and
> the i_size is then appropriately increased after a successful
> allocation.
>
> However, for this to work it requires i_size to be correct. If, for any
> reason, the directory inode i_size is corrupted in a way that the
> directory tree refers to a valid directory block past i_size, we could
> end up corrupting parts of the directory tree structure by overwriting
> already used directory blocks when modifying the directory.
>
> Fix it by catching the corruption early in __ext4_read_dirblock().
>
> Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Reviewed-by: Andreas Dilger <adilger@dilger.ca>
> Addresses Red-Hat-Bugzilla: #2070205
> Cc: stable@vger.kernel.org
> ---
> fs/ext4/namei.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index db4ba99d1ceb..cf460aa4f81d 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -110,6 +110,13 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
> struct ext4_dir_entry *dirent;
> int is_dx_block = 0;
>
> + if (block >= inode->i_size) {
> + ext4_error_inode(inode, func, line, block,
> + "Attempting to read directory block (%u) that is past i_size (%llu)",
> + block, inode->i_size);
> + return ERR_PTR(-EFSCORRUPTED);
> + }
> +
> if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
> bh = ERR_PTR(-EIO);
> else
> --
> 2.35.3
>
Cheers, Andreas
[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 873 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] ext4: make sure ext4_append() always allocates new block
2022-07-04 14:27 ` [PATCH 2/2] ext4: make sure ext4_append() always allocates new block Lukas Czerner
@ 2022-07-09 17:32 ` Andreas Dilger
0 siblings, 0 replies; 5+ messages in thread
From: Andreas Dilger @ 2022-07-09 17:32 UTC (permalink / raw)
To: Lukas Czerner; +Cc: Ext4 Developers List, Theodore Ts'o
[-- Attachment #1: Type: text/plain, Size: 1650 bytes --]
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
[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 873 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] ext4: check if directory block is within i_size
2022-07-04 14:27 [PATCH 1/2] ext4: check if directory block is within i_size Lukas Czerner
2022-07-04 14:27 ` [PATCH 2/2] ext4: make sure ext4_append() always allocates new block Lukas Czerner
2022-07-09 17:31 ` [PATCH 1/2] ext4: check if directory block is within i_size Andreas Dilger
@ 2022-07-22 13:58 ` Theodore Ts'o
2 siblings, 0 replies; 5+ messages in thread
From: Theodore Ts'o @ 2022-07-22 13:58 UTC (permalink / raw)
To: linux-ext4, Lukas Czerner; +Cc: Theodore Ts'o, stable
On Mon, 4 Jul 2022 16:27:20 +0200, Lukas Czerner wrote:
> Currently ext4 directory handling code implicitly assumes that the
> directory blocks are always within the i_size. In fact ext4_append()
> will attempt to allocate next directory block based solely on i_size and
> the i_size is then appropriately increased after a successful
> allocation.
>
> However, for this to work it requires i_size to be correct. If, for any
> reason, the directory inode i_size is corrupted in a way that the
> directory tree refers to a valid directory block past i_size, we could
> end up corrupting parts of the directory tree structure by overwriting
> already used directory blocks when modifying the directory.
>
> [...]
Applied, thanks!
[1/2] ext4: check if directory block is within i_size
commit: 65d23bd6e76ae07cee50c24d1fbeea4044aa41e7
[2/2] ext4: make sure ext4_append() always allocates new block
commit: 6d3ab9450ea5ec08882ab2f255827f1a39e300de
Best regards,
--
Theodore Ts'o <tytso@mit.edu>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-07-22 13:58 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-04 14:27 [PATCH 1/2] ext4: check if directory block is within i_size Lukas Czerner
2022-07-04 14:27 ` [PATCH 2/2] ext4: make sure ext4_append() always allocates new block Lukas Czerner
2022-07-09 17:32 ` Andreas Dilger
2022-07-09 17:31 ` [PATCH 1/2] ext4: check if directory block is within i_size Andreas Dilger
2022-07-22 13:58 ` Theodore Ts'o
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.