* [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len
@ 2022-10-12 13:16 Luís Henriques
0 siblings, 0 replies; 6+ messages in thread
From: Luís Henriques @ 2022-10-12 13:16 UTC (permalink / raw)
To: Theodore Ts'o, Andreas Dilger
Cc: linux-ext4, linux-kernel, Luís Henriques, stable
The rec_len field in the directory entry has to be a multiple of 4. A
corrupted filesystem image can be used to hit a BUG() in
ext4_rec_len_to_disk(), called from make_indexed_dir().
------------[ cut here ]------------
kernel BUG at fs/ext4/ext4.h:2413!
...
RIP: 0010:make_indexed_dir+0x53f/0x5f0
...
Call Trace:
<TASK>
? add_dirent_to_buf+0x1b2/0x200
ext4_add_entry+0x36e/0x480
ext4_add_nondir+0x2b/0xc0
ext4_create+0x163/0x200
path_openat+0x635/0xe90
do_filp_open+0xb4/0x160
? __create_object.isra.0+0x1de/0x3b0
? _raw_spin_unlock+0x12/0x30
do_sys_openat2+0x91/0x150
__x64_sys_open+0x6c/0xa0
do_syscall_64+0x3c/0x80
entry_SYSCALL_64_after_hwframe+0x46/0xb0
The fix simply adds a call to ext4_check_dir_entry() to validate the
directory entry, returning -EFSCORRUPTED if the entry is invalid.
CC: stable@vger.kernel.org
Link: https://bugzilla.kernel.org/show_bug.cgi?id=216540
Signed-off-by: Luís Henriques <lhenriques@suse.de>
---
* Changes since v1:
As suggested by Ted, I've removed the incorrect 'de->rec_len' check from
previous version and replaced it with a call to ext4_check_dir_entry()
instead, which is a much more complete verification.
fs/ext4/namei.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 3a31b662f661..ed76e89ffbe9 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -2254,8 +2254,16 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
memset(de, 0, len); /* wipe old data */
de = (struct ext4_dir_entry_2 *) data2;
top = data2 + len;
- while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
+ while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) {
+ if (ext4_check_dir_entry(dir, NULL, de, bh2, data2, len,
+ (data2 + (blocksize - csum_size) -
+ (char *) de))) {
+ brelse(bh2);
+ brelse(bh);
+ return -EFSCORRUPTED;
+ }
de = de2;
+ }
de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
(char *) de, blocksize);
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH] ext4: fix a NULL pointer when validating an inode bitmap
@ 2022-10-10 14:20 Luís Henriques
2022-10-12 13:13 ` [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len Luís Henriques
0 siblings, 1 reply; 6+ messages in thread
From: Luís Henriques @ 2022-10-10 14:20 UTC (permalink / raw)
To: Theodore Ts'o, Andreas Dilger
Cc: linux-ext4, linux-kernel, Luís Henriques, stable
It's possible to hit a NULL pointer exception while accessing the
sb->s_group_info in ext4_validate_inode_bitmap(), when calling
ext4_get_group_info().
EXT4-fs (loop0): warning: mounting unchecked fs, running e2fsck is recommended
EXT4-fs error (device loop0): ext4_clear_blocks:866: inode #32: comm mount: attempt to clear invalid blocks 16777450 len 1
EXT4-fs error (device loop0): ext4_free_branches:1012: inode #32: comm mount: invalid indirect mapped block 1258291200 (level 1)
EXT4-fs error (device loop0): ext4_free_branches:1012: inode #32: comm mount: invalid indirect mapped block 7379847 (level 2)
BUG: kernel NULL pointer dereference, address: 0000000000000000
...
RIP: 0010:ext4_read_inode_bitmap+0x21b/0x5a0
...
Call Trace:
<TASK>
ext4_free_inode+0x172/0x5c0
ext4_evict_inode+0x4a5/0x730
evict+0xc1/0x1c0
ext4_setup_system_zone+0x2ea/0x380
ext4_fill_super+0x249f/0x3910
? ext4_reconfigure+0x880/0x880
? snprintf+0x49/0x60
? ext4_reconfigure+0x880/0x880
get_tree_bdev+0x169/0x260
vfs_get_tree+0x16/0x70
path_mount+0x77d/0xa30
__x64_sys_mount+0x101/0x140
do_syscall_64+0x3c/0x80
entry_SYSCALL_64_after_hwframe+0x46/0xb0
Fix the issue by adding an extra NULL check. And, while there, also ensure the
right error code (-EFSCORRUPTED) is propagated to user-space. EUCLEAN is more
informative than ENOMEM.
CC: stable@vger.kernel.org
Link: https://bugzilla.kernel.org/show_bug.cgi?id=216541
Signed-off-by: Luís Henriques <lhenriques@suse.de>
---
fs/ext4/ext4.h | 17 ++++++++++-------
fs/ext4/ialloc.c | 2 +-
fs/ext4/indirect.c | 14 ++++++++++++--
3 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 3bf9a6926798..91317f592999 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3323,13 +3323,16 @@ static inline
struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
ext4_group_t group)
{
- struct ext4_group_info **grp_info;
- long indexv, indexh;
- BUG_ON(group >= EXT4_SB(sb)->s_groups_count);
- indexv = group >> (EXT4_DESC_PER_BLOCK_BITS(sb));
- indexh = group & ((EXT4_DESC_PER_BLOCK(sb)) - 1);
- grp_info = sbi_array_rcu_deref(EXT4_SB(sb), s_group_info, indexv);
- return grp_info[indexh];
+ struct ext4_group_info **grp_info;
+ long indexv, indexh;
+
+ BUG_ON(group >= EXT4_SB(sb)->s_groups_count);
+ if (!EXT4_SB(sb)->s_group_info)
+ return NULL;
+ indexv = group >> (EXT4_DESC_PER_BLOCK_BITS(sb));
+ indexh = group & ((EXT4_DESC_PER_BLOCK(sb)) - 1);
+ grp_info = sbi_array_rcu_deref(EXT4_SB(sb), s_group_info, indexv);
+ return grp_info[indexh];
}
/*
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 208b87ce8858..0e8d35d05b69 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -91,7 +91,7 @@ static int ext4_validate_inode_bitmap(struct super_block *sb,
if (buffer_verified(bh))
return 0;
- if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
+ if (!grp || EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
return -EFSCORRUPTED;
ext4_lock_group(sb, block_group);
diff --git a/fs/ext4/indirect.c b/fs/ext4/indirect.c
index 860fc5119009..e5ac5c2363df 100644
--- a/fs/ext4/indirect.c
+++ b/fs/ext4/indirect.c
@@ -148,6 +148,7 @@ static Indirect *ext4_get_branch(struct inode *inode, int depth,
struct super_block *sb = inode->i_sb;
Indirect *p = chain;
struct buffer_head *bh;
+ unsigned int key;
int ret = -EIO;
*err = 0;
@@ -156,9 +157,18 @@ static Indirect *ext4_get_branch(struct inode *inode, int depth,
if (!p->key)
goto no_block;
while (--depth) {
- bh = sb_getblk(sb, le32_to_cpu(p->key));
+ key = le32_to_cpu(p->key);
+ bh = sb_getblk(sb, key);
if (unlikely(!bh)) {
- ret = -ENOMEM;
+ /*
+ * sb_getblk() masks different errors by always
+ * returning NULL. Let's distinguish at least the case
+ * where the block is out of range.
+ */
+ if (key > ext4_blocks_count(EXT4_SB(sb)->s_es))
+ ret = -EFSCORRUPTED;
+ else
+ ret = -ENOMEM;
goto failure;
}
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len
2022-10-10 14:20 [PATCH] ext4: fix a NULL pointer when validating an inode bitmap Luís Henriques
@ 2022-10-12 13:13 ` Luís Henriques
2022-10-12 13:16 ` Luís Henriques
2022-11-06 6:16 ` Theodore Ts'o
0 siblings, 2 replies; 6+ messages in thread
From: Luís Henriques @ 2022-10-12 13:13 UTC (permalink / raw)
To: Theodore Ts'o, Andreas Dilger
Cc: linux-ext4, linux-kernel, Luís Henriques, stable
The rec_len field in the directory entry has to be a multiple of 4. A
corrupted filesystem image can be used to hit a BUG() in
ext4_rec_len_to_disk(), called from make_indexed_dir().
------------[ cut here ]------------
kernel BUG at fs/ext4/ext4.h:2413!
...
RIP: 0010:make_indexed_dir+0x53f/0x5f0
...
Call Trace:
<TASK>
? add_dirent_to_buf+0x1b2/0x200
ext4_add_entry+0x36e/0x480
ext4_add_nondir+0x2b/0xc0
ext4_create+0x163/0x200
path_openat+0x635/0xe90
do_filp_open+0xb4/0x160
? __create_object.isra.0+0x1de/0x3b0
? _raw_spin_unlock+0x12/0x30
do_sys_openat2+0x91/0x150
__x64_sys_open+0x6c/0xa0
do_syscall_64+0x3c/0x80
entry_SYSCALL_64_after_hwframe+0x46/0xb0
The fix simply adds a call to ext4_check_dir_entry() to validate the
directory entry, returning -EFSCORRUPTED if the entry is invalid.
CC: stable@vger.kernel.org
Link: https://bugzilla.kernel.org/show_bug.cgi?id=216540
Signed-off-by: Luís Henriques <lhenriques@suse.de>
---
* Changes since v1:
As suggested by Ted, I've removed the incorrect 'de->rec_len' check from
previous version and replaced it with a call to ext4_check_dir_entry()
instead, which is a much more complete verification.
fs/ext4/namei.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 3a31b662f661..ed76e89ffbe9 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -2254,8 +2254,16 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
memset(de, 0, len); /* wipe old data */
de = (struct ext4_dir_entry_2 *) data2;
top = data2 + len;
- while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
+ while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) {
+ if (ext4_check_dir_entry(dir, NULL, de, bh2, data2, len,
+ (data2 + (blocksize - csum_size) -
+ (char *) de))) {
+ brelse(bh2);
+ brelse(bh);
+ return -EFSCORRUPTED;
+ }
de = de2;
+ }
de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
(char *) de, blocksize);
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len
2022-10-12 13:13 ` [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len Luís Henriques
@ 2022-10-12 13:16 ` Luís Henriques
2022-10-12 14:21 ` Theodore Ts'o
2022-11-06 6:16 ` Theodore Ts'o
1 sibling, 1 reply; 6+ messages in thread
From: Luís Henriques @ 2022-10-12 13:16 UTC (permalink / raw)
To: Theodore Ts'o, Andreas Dilger; +Cc: linux-ext4, linux-kernel, stable
Grr, looks like I accidentally reused a 'git send-email' from shell
history which had a '--in-reply-to' in it. Please ignore and sorry about
that. I've just resent a new email.
Cheers,
--
Luís
On Wed, Oct 12, 2022 at 02:13:30PM +0100, Luís Henriques wrote:
> The rec_len field in the directory entry has to be a multiple of 4. A
> corrupted filesystem image can be used to hit a BUG() in
> ext4_rec_len_to_disk(), called from make_indexed_dir().
>
> ------------[ cut here ]------------
> kernel BUG at fs/ext4/ext4.h:2413!
> ...
> RIP: 0010:make_indexed_dir+0x53f/0x5f0
> ...
> Call Trace:
> <TASK>
> ? add_dirent_to_buf+0x1b2/0x200
> ext4_add_entry+0x36e/0x480
> ext4_add_nondir+0x2b/0xc0
> ext4_create+0x163/0x200
> path_openat+0x635/0xe90
> do_filp_open+0xb4/0x160
> ? __create_object.isra.0+0x1de/0x3b0
> ? _raw_spin_unlock+0x12/0x30
> do_sys_openat2+0x91/0x150
> __x64_sys_open+0x6c/0xa0
> do_syscall_64+0x3c/0x80
> entry_SYSCALL_64_after_hwframe+0x46/0xb0
>
> The fix simply adds a call to ext4_check_dir_entry() to validate the
> directory entry, returning -EFSCORRUPTED if the entry is invalid.
>
> CC: stable@vger.kernel.org
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=216540
> Signed-off-by: Luís Henriques <lhenriques@suse.de>
> ---
> * Changes since v1:
>
> As suggested by Ted, I've removed the incorrect 'de->rec_len' check from
> previous version and replaced it with a call to ext4_check_dir_entry()
> instead, which is a much more complete verification.
>
> fs/ext4/namei.c | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 3a31b662f661..ed76e89ffbe9 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -2254,8 +2254,16 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
> memset(de, 0, len); /* wipe old data */
> de = (struct ext4_dir_entry_2 *) data2;
> top = data2 + len;
> - while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
> + while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) {
> + if (ext4_check_dir_entry(dir, NULL, de, bh2, data2, len,
> + (data2 + (blocksize - csum_size) -
> + (char *) de))) {
> + brelse(bh2);
> + brelse(bh);
> + return -EFSCORRUPTED;
> + }
> de = de2;
> + }
> de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
> (char *) de, blocksize);
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len
2022-10-12 13:16 ` Luís Henriques
@ 2022-10-12 14:21 ` Theodore Ts'o
2022-10-12 15:18 ` Luís Henriques
0 siblings, 1 reply; 6+ messages in thread
From: Theodore Ts'o @ 2022-10-12 14:21 UTC (permalink / raw)
To: Luís Henriques; +Cc: Andreas Dilger, linux-ext4, linux-kernel, stable
On Wed, Oct 12, 2022 at 02:16:42PM +0100, Luís Henriques wrote:
> Grr, looks like I accidentally reused a 'git send-email' from shell
> history which had a '--in-reply-to' in it. Please ignore and sorry about
> that. I've just resent a new email.
No worries! The --in-reply-to wasn't actually a problem, since b4
generally will do the right thing (and sometimes humans prefer the
in-reply-to since they can more easily see the patch that it is
replacing/obsoleting).
b4 can sometimes get confused when a patch series gets split, and both
parts of the patch series are in a reply-to mail thread to the
original patch series, since if it can't use the -vn+1 hueristic or
the "subject line has stayed the same but has a newer date" hueristic,
it falls back to "latest patch in the mail thread". So if there are
two "valid" patches or patch series in an e-mail thread, b4 -c
(--check-newer-revisions) can get confused. But even in that case,
that it's more a minor annoyance than anything else.
So in the future, don't feel that you need to resend a patch if
there's an incorrect/older --in-reply-to; it's not a big deal.
Cheers, and thanks!
- Ted
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len
2022-10-12 14:21 ` Theodore Ts'o
@ 2022-10-12 15:18 ` Luís Henriques
0 siblings, 0 replies; 6+ messages in thread
From: Luís Henriques @ 2022-10-12 15:18 UTC (permalink / raw)
To: Theodore Ts'o; +Cc: Andreas Dilger, linux-ext4, linux-kernel, stable
On Wed, Oct 12, 2022 at 10:21:39AM -0400, Theodore Ts'o wrote:
> On Wed, Oct 12, 2022 at 02:16:42PM +0100, Luís Henriques wrote:
> > Grr, looks like I accidentally reused a 'git send-email' from shell
> > history which had a '--in-reply-to' in it. Please ignore and sorry about
> > that. I've just resent a new email.
>
> No worries! The --in-reply-to wasn't actually a problem, since b4
> generally will do the right thing (and sometimes humans prefer the
> in-reply-to since they can more easily see the patch that it is
> replacing/obsoleting).
>
> b4 can sometimes get confused when a patch series gets split, and both
> parts of the patch series are in a reply-to mail thread to the
> original patch series, since if it can't use the -vn+1 hueristic or
> the "subject line has stayed the same but has a newer date" hueristic,
> it falls back to "latest patch in the mail thread". So if there are
> two "valid" patches or patch series in an e-mail thread, b4 -c
> (--check-newer-revisions) can get confused. But even in that case,
> that it's more a minor annoyance than anything else.
>
> So in the future, don't feel that you need to resend a patch if
> there's an incorrect/older --in-reply-to; it's not a big deal.
Great, I haven't yet included b4 in my workflow so, to be honest, I didn't
really thought about that tool being confused. What really made me resend
the patch was that I used the *wrong message-ID in the "--in-reply-to"!
And that thread already had a v2 patch, which would could easily confuse
humans. Hopefully, b4 won't be confused by that either.
Cheers,
--
Luís
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len
2022-10-12 13:13 ` [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len Luís Henriques
2022-10-12 13:16 ` Luís Henriques
@ 2022-11-06 6:16 ` Theodore Ts'o
1 sibling, 0 replies; 6+ messages in thread
From: Theodore Ts'o @ 2022-11-06 6:16 UTC (permalink / raw)
To: Luís Henriques, Andreas Dilger
Cc: Theodore Ts'o, linux-kernel, stable, linux-ext4
On Wed, 12 Oct 2022 14:13:30 +0100, Luís Henriques wrote:
> The rec_len field in the directory entry has to be a multiple of 4. A
> corrupted filesystem image can be used to hit a BUG() in
> ext4_rec_len_to_disk(), called from make_indexed_dir().
>
> ------------[ cut here ]------------
> kernel BUG at fs/ext4/ext4.h:2413!
> ...
> RIP: 0010:make_indexed_dir+0x53f/0x5f0
> ...
> Call Trace:
> <TASK>
> ? add_dirent_to_buf+0x1b2/0x200
> ext4_add_entry+0x36e/0x480
> ext4_add_nondir+0x2b/0xc0
> ext4_create+0x163/0x200
> path_openat+0x635/0xe90
> do_filp_open+0xb4/0x160
> ? __create_object.isra.0+0x1de/0x3b0
> ? _raw_spin_unlock+0x12/0x30
> do_sys_openat2+0x91/0x150
> __x64_sys_open+0x6c/0xa0
> do_syscall_64+0x3c/0x80
> entry_SYSCALL_64_after_hwframe+0x46/0xb0
>
> [...]
Applied, thanks!
[1/1] ext4: fix BUG_ON() when directory entry has invalid rec_len
commit: 17a0bc9bd697f75cfdf9b378d5eb2d7409c91340
Best regards,
--
Theodore Ts'o <tytso@mit.edu>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-11-06 6:17 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-12 13:16 [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len Luís Henriques
-- strict thread matches above, loose matches on Subject: below --
2022-10-10 14:20 [PATCH] ext4: fix a NULL pointer when validating an inode bitmap Luís Henriques
2022-10-12 13:13 ` [PATCH v2] ext4: fix BUG_ON() when directory entry has invalid rec_len Luís Henriques
2022-10-12 13:16 ` Luís Henriques
2022-10-12 14:21 ` Theodore Ts'o
2022-10-12 15:18 ` Luís Henriques
2022-11-06 6:16 ` Theodore Ts'o
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®