Commit 37b0b6b8 authored by Dan Carpenter's avatar Dan Carpenter Committed by Theodore Ts'o

ext4: potential crash on allocation error in ext4_alloc_flex_bg_array()

If sbi->s_flex_groups_allocated is zero and the first allocation fails
then this code will crash.  The problem is that "i--" will set "i" to
-1 but when we compare "i >= sbi->s_flex_groups_allocated" then the -1
is type promoted to unsigned and becomes UINT_MAX.  Since UINT_MAX
is more than zero, the condition is true so we call kvfree(new_groups[-1]).
The loop will carry on freeing invalid memory until it crashes.

Fixes: 7c990728 ("ext4: fix potential race between s_flex_groups online resizing and access")
Reviewed-by: default avatarSuraj Jitindar Singh <surajjs@amazon.com>
Signed-off-by: default avatarDan Carpenter <dan.carpenter@oracle.com>
Cc: stable@kernel.org
Link: https://lore.kernel.org/r/20200228092142.7irbc44yaz3by7nb@kili.mountainSigned-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
parent 6c5d9112
...@@ -2391,7 +2391,7 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup) ...@@ -2391,7 +2391,7 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
{ {
struct ext4_sb_info *sbi = EXT4_SB(sb); struct ext4_sb_info *sbi = EXT4_SB(sb);
struct flex_groups **old_groups, **new_groups; struct flex_groups **old_groups, **new_groups;
int size, i; int size, i, j;
if (!sbi->s_log_groups_per_flex) if (!sbi->s_log_groups_per_flex)
return 0; return 0;
...@@ -2412,8 +2412,8 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup) ...@@ -2412,8 +2412,8 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
sizeof(struct flex_groups)), sizeof(struct flex_groups)),
GFP_KERNEL); GFP_KERNEL);
if (!new_groups[i]) { if (!new_groups[i]) {
for (i--; i >= sbi->s_flex_groups_allocated; i--) for (j = sbi->s_flex_groups_allocated; j < i; j++)
kvfree(new_groups[i]); kvfree(new_groups[j]);
kvfree(new_groups); kvfree(new_groups);
ext4_msg(sb, KERN_ERR, ext4_msg(sb, KERN_ERR,
"not enough memory for %d flex groups", size); "not enough memory for %d flex groups", size);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment