2011-12-26 06:38:20

by Xi Wang

[permalink] [raw]
Subject: [PATCH] ext4: avoid oversized shift in ext4_fill_flex_info()

Commit 503358ae fixed a division by zero, but groups_per_flex still
overflows due to an oversized shift, given a large s_log_groups_per_flex
like 36. (1 << 36) is undefined in C; the result may vary depending
on the architecture, e.g., 16 on x86, thus bypassing the sanity check
(groups_per_flex < 2).

Signed-off-by: Xi Wang <[email protected]>
---
fs/ext4/super.c | 9 +++++----
1 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 3e1329e..6deaf41 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -2010,14 +2010,15 @@ static int ext4_fill_flex_info(struct super_block *sb)
size_t size;
int i;

- sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
- groups_per_flex = 1 << sbi->s_log_groups_per_flex;


2011-12-26 07:20:58

by Andreas Dilger

[permalink] [raw]
Subject: Re: [PATCH] ext4: avoid oversized shift in ext4_fill_flex_info()

On 2011-12-25, at 23:36, Xi Wang <[email protected]> wrote:

> Commit 503358ae fixed a division by zero, but groups_per_flex still
> overflows due to an oversized shift, given a large s_log_groups_per_flex
> like 36. (1 << 36) is undefined in C; the result may vary depending
> on the architecture, e.g., 16 on x86, thus bypassing the sanity check
> (groups_per_flex < 2).

While this is true in theory, it is not possible to have 2^32 groups per flex group. This would mean 2^32 block bitmaps and inode bitmaps in a single group, which is impossible.

> Signed-off-by: Xi Wang <[email protected]>
> ---
> fs/ext4/super.c | 9 +++++----
> 1 files changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index 3e1329e..6deaf41 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -2010,14 +2010,15 @@ static int ext4_fill_flex_info(struct super_block *sb)
> size_t size;
> int i;
>
> - sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
> - groups_per_flex = 1 << sbi->s_log_groups_per_flex;
> -
> - if (groups_per_flex < 2) {
> + if (sbi->s_es->s_log_groups_per_flex == 0 ||
> + sbi->s_es->s_log_groups_per_flex >= 32) {
> sbi->s_log_groups_per_flex = 0;
> return 1;
> }
>
> + sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
> + groups_per_flex = 1 << sbi->s_log_groups_per_flex;
> +
> /* We allocate both existing and potentially added groups */
> flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
> ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
> --
> 1.7.5.4
>

2011-12-27 02:26:25

by Xi Wang

[permalink] [raw]
Subject: Re: [PATCH] ext4: avoid oversized shift in ext4_fill_flex_info()

On Dec 26, 2011, at 2:20 AM, Andreas Dilger wrote:
> While this is true in theory, it is not possible to have 2^32 groups per flex group. This would mean 2^32 block bitmaps and inode bitmaps in a single group, which is impossible.

This patch is based on commit 503358ae "ext4: avoid divide by zero
when trying to mount a corrupted file system", as I quote: "If
s_log_groups_per_flex is greater than 31, then groups_per_flex will
will overflow and cause a divide by zero error." The intent is to
avoid oversized shift in that commit, because the behavior is
undefined in C and may vary on different architectures.

My understanding is that sbi->s_es->s_log_groups_per_flex needs a
sanity check if it is read from a malicious or corrupted filesystem
in ext4_fill_flex_info(), which is called from ext4_fill_super().

Did I miss anything?

- xi