All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ext4: potential crash on allocation error in ext4_alloc_flex_bg_array()
@ 2020-02-28  9:22 ` Dan Carpenter
  0 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2020-02-28  9:22 UTC (permalink / raw)
  To: Theodore Ts'o, Suraj Jitindar Singh
  Cc: Andreas Dilger, linux-ext4, kernel-janitors, Walter Harms

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: 7c990728b99e ("ext4: fix potential race between s_flex_groups online resizing and access")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
I changed this from a -- loop to a ++ loop because I knew it would make
Walter Harms happy.  He hates -- loops and I don't when his birthday so
I'm celebrating it today.  :)

 fs/ext4/super.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index ff1b764b0c0e..0c7c4adb664e 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -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 flex_groups **old_groups, **new_groups;
-	int size, i;
+	int size, i, j;
 
 	if (!sbi->s_log_groups_per_flex)
 		return 0;
@@ -2412,8 +2412,8 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
 					 sizeof(struct flex_groups)),
 					 GFP_KERNEL);
 		if (!new_groups[i]) {
-			for (i--; i >= sbi->s_flex_groups_allocated; i--)
-				kvfree(new_groups[i]);
+			for (j = sbi->s_flex_groups_allocated; j < i; j++)
+				kvfree(new_groups[j]);
 			kvfree(new_groups);
 			ext4_msg(sb, KERN_ERR,
 				 "not enough memory for %d flex groups", size);
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] ext4: potential crash on allocation error in ext4_alloc_flex_bg_array()
@ 2020-02-28  9:22 ` Dan Carpenter
  0 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2020-02-28  9:22 UTC (permalink / raw)
  To: Theodore Ts'o, Suraj Jitindar Singh
  Cc: Andreas Dilger, linux-ext4, kernel-janitors, Walter Harms

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: 7c990728b99e ("ext4: fix potential race between s_flex_groups online resizing and access")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
I changed this from a -- loop to a ++ loop because I knew it would make
Walter Harms happy.  He hates -- loops and I don't when his birthday so
I'm celebrating it today.  :)

 fs/ext4/super.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index ff1b764b0c0e..0c7c4adb664e 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -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 flex_groups **old_groups, **new_groups;
-	int size, i;
+	int size, i, j;
 
 	if (!sbi->s_log_groups_per_flex)
 		return 0;
@@ -2412,8 +2412,8 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
 					 sizeof(struct flex_groups)),
 					 GFP_KERNEL);
 		if (!new_groups[i]) {
-			for (i--; i >= sbi->s_flex_groups_allocated; i--)
-				kvfree(new_groups[i]);
+			for (j = sbi->s_flex_groups_allocated; j < i; j++)
+				kvfree(new_groups[j]);
 			kvfree(new_groups);
 			ext4_msg(sb, KERN_ERR,
 				 "not enough memory for %d flex groups", size);
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] ext4: potential crash on allocation error in ext4_alloc_flex_bg_array()
  2020-02-28  9:22 ` Dan Carpenter
@ 2020-02-28 18:45   ` Jitindar SIngh, Suraj
  -1 siblings, 0 replies; 4+ messages in thread
From: Jitindar SIngh, Suraj @ 2020-02-28 18:45 UTC (permalink / raw)
  To: dan.carpenter, tytso; +Cc: adilger.kernel, linux-ext4, wharms, kernel-janitors

On Fri, 2020-02-28 at 12:22 +0300, Dan Carpenter wrote:
> 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: 7c990728b99e ("ext4: fix potential race between s_flex_groups
> online resizing and access")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Reviewed-by: Suraj Jitindar Singh <surajjs@amazon.com>
Cc: <stable@kernel.org>

> ---
> I changed this from a -- loop to a ++ loop because I knew it would
> make
> Walter Harms happy.  He hates -- loops and I don't when his birthday
> so
> I'm celebrating it today.  :)
> 
>  fs/ext4/super.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index ff1b764b0c0e..0c7c4adb664e 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -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 flex_groups **old_groups, **new_groups;
> -	int size, i;
> +	int size, i, j;
>  
>  	if (!sbi->s_log_groups_per_flex)
>  		return 0;
> @@ -2412,8 +2412,8 @@ int ext4_alloc_flex_bg_array(struct super_block
> *sb, ext4_group_t ngroup)
>  					 sizeof(struct flex_groups)),
>  					 GFP_KERNEL);
>  		if (!new_groups[i]) {
> -			for (i--; i >= sbi->s_flex_groups_allocated; i-
> -)
> -				kvfree(new_groups[i]);
> +			for (j = sbi->s_flex_groups_allocated; j < i;
> j++)
> +				kvfree(new_groups[j]);
>  			kvfree(new_groups);
>  			ext4_msg(sb, KERN_ERR,
>  				 "not enough memory for %d flex
> groups", size);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ext4: potential crash on allocation error in ext4_alloc_flex_bg_array()
@ 2020-02-28 18:45   ` Jitindar SIngh, Suraj
  0 siblings, 0 replies; 4+ messages in thread
From: Jitindar SIngh, Suraj @ 2020-02-28 18:45 UTC (permalink / raw)
  To: dan.carpenter, tytso; +Cc: adilger.kernel, linux-ext4, wharms, kernel-janitors

T24gRnJpLCAyMDIwLTAyLTI4IGF0IDEyOjIyICswMzAwLCBEYW4gQ2FycGVudGVyIHdyb3RlOg0K
PiBJZiBzYmktPnNfZmxleF9ncm91cHNfYWxsb2NhdGVkIGlzIHplcm8gYW5kIHRoZSBmaXJzdCBh
bGxvY2F0aW9uDQo+IGZhaWxzDQo+IHRoZW4gdGhpcyBjb2RlIHdpbGwgY3Jhc2guICBUaGUgcHJv
YmxlbSBpcyB0aGF0ICJpLS0iIHdpbGwgc2V0ICJpIiB0bw0KPiAtMSBidXQgd2hlbiB3ZSBjb21w
YXJlICJpID49IHNiaS0+c19mbGV4X2dyb3Vwc19hbGxvY2F0ZWQiIHRoZW4gdGhlDQo+IC0xDQo+
IGlzIHR5cGUgcHJvbW90ZWQgdG8gdW5zaWduZWQgYW5kIGJlY29tZXMgVUlOVF9NQVguICBTaW5j
ZSBVSU5UX01BWA0KPiBpcyBtb3JlIHRoYW4gemVybywgdGhlIGNvbmRpdGlv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IAkJCQkgIm5vdCBlbm91
Z2ggbWVtb3J5IGZvciAlZCBmbGV4DQo+IGdyb3VwcyIsIHNpemUpOw0K

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-02-28 18:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-28  9:22 [PATCH] ext4: potential crash on allocation error in ext4_alloc_flex_bg_array() Dan Carpenter
2020-02-28  9:22 ` Dan Carpenter
2020-02-28 18:45 ` Jitindar SIngh, Suraj
2020-02-28 18:45   ` Jitindar SIngh, Suraj

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.