Skip to content

Commit 37b0b6b

Browse files
Dan Carpentertytso
authored andcommitted
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: 7c99072 ("ext4: fix potential race between s_flex_groups online resizing and access") Reviewed-by: Suraj Jitindar Singh <[email protected]> Signed-off-by: Dan Carpenter <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 6c5d911 commit 37b0b6b

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

fs/ext4/super.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
23912391
{
23922392
struct ext4_sb_info *sbi = EXT4_SB(sb);
23932393
struct flex_groups **old_groups, **new_groups;
2394-
int size, i;
2394+
int size, i, j;
23952395

23962396
if (!sbi->s_log_groups_per_flex)
23972397
return 0;
@@ -2412,8 +2412,8 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
24122412
sizeof(struct flex_groups)),
24132413
GFP_KERNEL);
24142414
if (!new_groups[i]) {
2415-
for (i--; i >= sbi->s_flex_groups_allocated; i--)
2416-
kvfree(new_groups[i]);
2415+
for (j = sbi->s_flex_groups_allocated; j < i; j++)
2416+
kvfree(new_groups[j]);
24172417
kvfree(new_groups);
24182418
ext4_msg(sb, KERN_ERR,
24192419
"not enough memory for %d flex groups", size);

0 commit comments

Comments
 (0)