Skip to contentSkip to conversation

zram: fix possible use after free in zcomp_create()

PATCH Superseded Luis Henriques 4 participants 14 messages Sep 7, 2015 – Sep 8, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 zram: fix possible use after free in zcomp_create()

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

Fixes beca3ec71fe5 ("zram: add multi stream functionality")
---
 drivers/block/zram/zcomp.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/block/zram/zcomp.c +2 -0
index 965d1afb0eaa..f3db8158e172 100644
@@ -212,6 +212,7 @@ static int zcomp_strm_multi_create(struct zcomp *comp, int max_strm)
 	zstrm = zcomp_strm_alloc(comp);
 	if (!zstrm) {
 		kfree(zs);
+		comp->stream = NULL;
 		return -ENOMEM;
 	}
 	list_add(&zstrm->list, &zs->idle_strm);
@@ -262,6 +263,7 @@ static int zcomp_strm_single_create(struct zcomp *comp)
 	zs->zstrm = zcomp_strm_alloc(comp);
 	if (!zs->zstrm) {
 		kfree(zs);
+		comp->stream = NULL;
 		return -ENOMEM;
 	}
 	return 0;
#1 lore raw
Quoted 4 lines · Luis Henriques

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

good catch.

we probably better start checking the zcomp_strm_multi_create()/ zcomp_strm_single_create() status in zcomp_create(); that's much more obvious and that's why we return it in the first place.

what do you think?

---

 drivers/block/zram/zcomp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/block/zram/zcomp.c +4 -3
index 3456d5a..16bbc8c 100644
@@ -360,6 +360,7 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int ret;
 
 	backend = find_backend(compress);
 	if (!backend)
@@ -371,10 +372,10 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		ret = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		ret = zcomp_strm_single_create(comp);
+	if (ret != 0) {
 		kfree(comp);
 		return ERR_PTR(-ENOMEM);
 	}
author Acked-by
#2 lore raw
Quoted 6 lines · Sergey Senozhatsky
Quoted 4 lines · Luis Henriques

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

Quoted 8 lines

good catch.

we probably better start checking the zcomp_strm_multi_create()/ zcomp_strm_single_create() status in zcomp_create(); that's much more obvious and that's why we return it in the first place.

what do you think?

Yep, that's probably a better solution.

Cheers,
--
Luís

Quoted 32 lines

---

 drivers/block/zram/zcomp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/block/zram/zcomp.c +4 -3
index 3456d5a..16bbc8c 100644
@@ -360,6 +360,7 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int ret;
 
 	backend = find_backend(compress);
 	if (!backend)
@@ -371,10 +372,10 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		ret = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		ret = zcomp_strm_single_create(comp);
+	if (ret != 0) {
 		kfree(comp);
 		return ERR_PTR(-ENOMEM);
 	}
#3 lore raw
Quoted 12 lines · Luis Henriques
Quoted 12 lines · Luis Henriques
Quoted 4 lines

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

good catch.

we probably better start checking the zcomp_strm_multi_create()/ zcomp_strm_single_create() status in zcomp_create(); that's much more obvious and that's why we return it in the first place.

Quoted 7 lines
Quoted 2 lines

what do you think?

Yep, that's probably a better solution.

Oh, thanks. I don't mind if you will re-submit it; I just did minor changes to our fix. Or I can handle it. I'm OK with either way. Btw, did you hit that problem or you reviewed the code?

	-ss
Quoted 37 lines

Cheers,
--
Luís

Quoted 32 lines

---

 drivers/block/zram/zcomp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/block/zram/zcomp.c +4 -3
index 3456d5a..16bbc8c 100644
@@ -360,6 +360,7 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int ret;
 
 	backend = find_backend(compress);
 	if (!backend)
@@ -371,10 +372,10 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		ret = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		ret = zcomp_strm_single_create(comp);
+	if (ret != 0) {
 		kfree(comp);
 		return ERR_PTR(-ENOMEM);
 	}
#4 lore raw
Quoted 16 lines · Sergey Senozhatsky
Quoted 15 lines · Luis Henriques
Quoted 14 lines · Luis Henriques
Quoted 4 lines

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

good catch.

we probably better start checking the zcomp_strm_multi_create()/ zcomp_strm_single_create() status in zcomp_create(); that's much more obvious and that's why we return it in the first place.

what do you think?

Quoted 7 lines
Quoted 4 lines

Yep, that's probably a better solution.

Oh, thanks. I don't mind if you will re-submit it; I just did minor changes to our fix. Or I can handle it. I'm OK with either way.

Ok, I'll be sending v2 in a minute.

Quoted 2 lines

Btw, did you hit that problem or you reviewed the code?

I've found this while looking at the code (I wouldn't really call it a "code review"... :-))

Cheers,
--
Luís

#5 lore raw

PATCH v2 1/1 zram: fix possible use after free in zcomp_create()

In reply to Sergey Senozhatsky

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

Fixes beca3ec71fe5 ("zram: add multi stream functionality")

---
Changes since v1:
* Check zcomp_strm_{multi,siggle}_create() return code instead comp->stream (suggested by Sergey)

drivers/block/zram/zcomp.c | 7 ++++---

 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/block/zram/zcomp.c +4 -3
index 965d1afb0eaa..8d2cdfd307db 100644
@@ -336,6 +336,7 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int ret;
 
 	backend = find_backend(compress);
 	if (!backend)
@@ -347,10 +348,10 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		ret = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		ret = zcomp_strm_single_create(comp);
+	if (ret) {
 		kfree(comp);
 		return ERR_PTR(-ENOMEM);
 	}
Acked-by on v2 1/1
#6 lore raw
Quoted 7 lines · Luis Henriques

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

Fixes beca3ec71fe5 ("zram: add multi stream functionality")

Cc Andrew

	-ss
Quoted 35 lines

---
Changes since v1:
* Check zcomp_strm_{multi,siggle}_create() return code instead comp->stream (suggested by Sergey)

drivers/block/zram/zcomp.c | 7 ++++---

 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/block/zram/zcomp.c +4 -3
index 965d1afb0eaa..8d2cdfd307db 100644
@@ -336,6 +336,7 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int ret;
 
 	backend = find_backend(compress);
 	if (!backend)
@@ -347,10 +348,10 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		ret = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		ret = zcomp_strm_single_create(comp);
+	if (ret) {
 		kfree(comp);
 		return ERR_PTR(-ENOMEM);
 	}
maintainer
#7 lore raw
In reply to Luis Henriques

Hello,

First of all, Thanks for catching a bug and review, Guys. Below there are just some cleanup.
If you guys think it's better, please respin.

Quoted 20 lines · Luis Henriques

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

Fixes beca3ec71fe5 ("zram: add multi stream functionality")

---
Changes since v1:
* Check zcomp_strm_{multi,siggle}_create() return code instead comp->stream (suggested by Sergey)

drivers/block/zram/zcomp.c | 7 ++++---

 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/block/zram/zcomp.c +0 -0
index 965d1afb0eaa..8d2cdfd307db 100644

drivers/block/zram/zcomp.c struct zcomp *zcomp_create(const char *compress, int max_strm) · line 339

Quoted 4 lines
drivers/block/zram/zcomp.c +1 -0
@@ -336,6 +336,7 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int ret;

For the clarification, I want to call it as 'error' instead of ret.

Quoted 7 lines
drivers/block/zram/zcomp.c +0 -0
 	backend = find_backend(compress);
 	if (!backend)
@@ -347,10 +348,10 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)

drivers/block/zram/zcomp.c struct zcomp *zcomp_create(const char *compress, int max_strm) · line 357

Quoted 10 lines
drivers/block/zram/zcomp.c +3 -3
-		zcomp_strm_multi_create(comp, max_strm);
+		ret = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		ret = zcomp_strm_single_create(comp);
+	if (ret) {
 		kfree(comp);
 		return ERR_PTR(-ENOMEM);
 	}

And we could return ERR_PTR(error) rather than fixed -ENOMEM to propagate other errors potentially could be happen in future(ex, crypto support). Of course, we should change description of the function about error return.

Thanks.

#8 lore raw

On (09/08/15 10:07), Minchan Kim wrote:
[..]

Quoted 9 lines

drivers/block/zram/zcomp.c struct zcomp *zcomp_create(const char *compress, int max_strm) · line 339

Quoted 1 line
drivers/block/zram/zcomp.c +1 -0
+	int ret;

For the clarification, I want to call it as 'error' instead of ret.

Quoted 5 lines
drivers/block/zram/zcomp.c +0 -0
 	backend = find_backend(compress);
 	if (!backend)
@@ -347,10 +348,10 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
Quoted 14 lines

drivers/block/zram/zcomp.c struct zcomp *zcomp_create(const char *compress, int max_strm) · line 357

Quoted 12 lines
drivers/block/zram/zcomp.c +3 -3
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		ret = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		ret = zcomp_strm_single_create(comp);
+	if (ret) {
 		kfree(comp);
 		return ERR_PTR(-ENOMEM);
 	}

And we could return ERR_PTR(error) rather than fixed -ENOMEM to propagate

yep, I thought about that; looks to be minor so I didn't insist on changing that. don't mind to change it to ERR_PTR(error).

Quoted 2 lines

other errors potentially could be happen in future(ex, crypto support). Of course, we should change description of the function about error return.

on the other hand, this is zcomp_strm_FOO_create(), which is mostly about memory allocation. but yeah, who knows. We can change this as a part of crypto api rework; ERR_PTR(error) does not fix anything per se, so may be we can avoid pushing this change into stable.

	-ss
maintainer
#9 lore raw
Quoted 26 lines · Sergey Senozhatsky

On (09/08/15 10:07), Minchan Kim wrote:
[..]

Quoted 9 lines

drivers/block/zram/zcomp.c struct zcomp *zcomp_create(const char *compress, int max_strm) · line 339

Quoted 1 line
drivers/block/zram/zcomp.c +1 -0
+	int ret;

For the clarification, I want to call it as 'error' instead of ret.

Quoted 5 lines
drivers/block/zram/zcomp.c +0 -0
 	backend = find_backend(compress);
 	if (!backend)
@@ -347,10 +348,10 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
Quoted 14 lines

drivers/block/zram/zcomp.c struct zcomp *zcomp_create(const char *compress, int max_strm) · line 357

Quoted 12 lines
drivers/block/zram/zcomp.c +3 -3
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		ret = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		ret = zcomp_strm_single_create(comp);
+	if (ret) {
 		kfree(comp);
 		return ERR_PTR(-ENOMEM);
 	}

And we could return ERR_PTR(error) rather than fixed -ENOMEM to propagate

Quoted 2 lines

yep, I thought about that; looks to be minor so I didn't insist on changing that. don't mind to change it to ERR_PTR(error).

Thanks.

Quoted 8 lines
Quoted 3 lines

other errors potentially could be happen in future(ex, crypto support). Of course, we should change description of the function about error return.

on the other hand, this is zcomp_strm_FOO_create(), which is mostly about memory allocation. but yeah, who knows. We can change this as a part of crypto api rework; ERR_PTR(error) does not fix anything per se, so may be we can avoid pushing this change into stable.

Strictly speaking, you're right. I asked two things bug fixing and refactoring. But I thought it is really trivial enough to push -stable.

With bonus, it's more readable(ie, error naming) and makes sense( ie, not assume under layer function always fails with no memory).

So, hope to fix it altogether if anyone isn't strong against that.

Thank you.

#10 lore raw

PATCH v3 1/1 zram: fix possible use after free in zcomp_create()

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

While at it, replace a 'ERR_PTR(-ENOMEM)' by a more generic 'ERR_PTR(error)' as in the future zcomp_strm_{multi,siggle}_create() could return other error codes. Function documentation updated accordingly.

Fixes beca3ec71fe5 ("zram: add multi stream functionality")
Cc Sergey Senozhatsky <[email protected]>
Cc Minchan Kim <[email protected]>

---

Changes since v2:
* Renamed local variable 'ret' to 'error'
* Usage of 'ERR_PTR(error)' to accommodate future error codes returned by zcomp_strm_{multi,siggle}_create
(all suggested by Minchan)

Changes since v1:
* Check zcomp_strm_{multi,siggle}_create() return code instead comp->stream (suggested by Sergey)

drivers/block/zram/zcomp.c | 12 +++++++-----

 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/block/zram/zcomp.c +7 -5
index 965d1afb0eaa..5cb13ca3a3ac 100644
@@ -330,12 +330,14 @@ void zcomp_destroy(struct zcomp *comp)
  * allocate new zcomp and initialize it. return compressing
  * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
  * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
- * case of allocation error.
+ * case of allocation error, or any other error potentially
+ * returned by functions zcomp_strm_{multi,single}_create.
  */
 struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int error;
 
 	backend = find_backend(compress);
 	if (!backend)
@@ -347,12 +349,12 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		error = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		error = zcomp_strm_single_create(comp);
+	if (error) {
 		kfree(comp);
-		return ERR_PTR(-ENOMEM);
+		return ERR_PTR(error);
 	}
 	return comp;
 }
Acked-by on v3 1/1
#11 lore raw
Quoted 8 lines · Luis Henriques

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

While at it, replace a 'ERR_PTR(-ENOMEM)' by a more generic 'ERR_PTR(error)' as in the future zcomp_strm_{multi,siggle}_create() could return other error codes. Function documentation updated accordingly.

Quoted 5 lines
Fixes beca3ec71fe5 ("zram: add multi stream functionality")
Cc Sergey Senozhatsky <[email protected]>
Cc Minchan Kim <[email protected]>
	-ss
Quoted 53 lines

---

Changes since v2:
* Renamed local variable 'ret' to 'error'
* Usage of 'ERR_PTR(error)' to accommodate future error codes returned by zcomp_strm_{multi,siggle}_create
(all suggested by Minchan)

Changes since v1:
* Check zcomp_strm_{multi,siggle}_create() return code instead comp->stream (suggested by Sergey)

drivers/block/zram/zcomp.c | 12 +++++++-----

 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/block/zram/zcomp.c +7 -5
index 965d1afb0eaa..5cb13ca3a3ac 100644
@@ -330,12 +330,14 @@ void zcomp_destroy(struct zcomp *comp)
  * allocate new zcomp and initialize it. return compressing
  * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
  * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
- * case of allocation error.
+ * case of allocation error, or any other error potentially
+ * returned by functions zcomp_strm_{multi,single}_create.
  */
 struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int error;
 
 	backend = find_backend(compress);
 	if (!backend)
@@ -347,12 +349,12 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		error = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		error = zcomp_strm_single_create(comp);
+	if (error) {
 		kfree(comp);
-		return ERR_PTR(-ENOMEM);
+		return ERR_PTR(error);
 	}
 	return comp;
 }
#12 lore raw
Quoted 9 lines · Sergey Senozhatsky
Quoted 8 lines · Luis Henriques

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

While at it, replace a 'ERR_PTR(-ENOMEM)' by a more generic 'ERR_PTR(error)' as in the future zcomp_strm_{multi,siggle}_create() could return other error codes. Function documentation updated accordingly.

Oh...forgot to Cc Andrew
Andrew, please disregard V2 of this patch.

V3

	-ss
Quoted 54 lines
Quoted 53 lines

---

Changes since v2:
* Renamed local variable 'ret' to 'error'
* Usage of 'ERR_PTR(error)' to accommodate future error codes returned by zcomp_strm_{multi,siggle}_create
(all suggested by Minchan)

Changes since v1:
* Check zcomp_strm_{multi,siggle}_create() return code instead comp->stream (suggested by Sergey)

drivers/block/zram/zcomp.c | 12 +++++++-----

 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/block/zram/zcomp.c +7 -5
index 965d1afb0eaa..5cb13ca3a3ac 100644
@@ -330,12 +330,14 @@ void zcomp_destroy(struct zcomp *comp)
  * allocate new zcomp and initialize it. return compressing
  * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
  * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
- * case of allocation error.
+ * case of allocation error, or any other error potentially
+ * returned by functions zcomp_strm_{multi,single}_create.
  */
 struct zcomp *zcomp_create(const char *compress, int max_strm)
 {
 	struct zcomp *comp;
 	struct zcomp_backend *backend;
+	int error;
 
 	backend = find_backend(compress);
 	if (!backend)
@@ -347,12 +349,12 @@ struct zcomp *zcomp_create(const char *compress, int max_strm)
 
 	comp->backend = backend;
 	if (max_strm > 1)
-		zcomp_strm_multi_create(comp, max_strm);
+		error = zcomp_strm_multi_create(comp, max_strm);
 	else
-		zcomp_strm_single_create(comp);
-	if (!comp->stream) {
+		error = zcomp_strm_single_create(comp);
+	if (error) {
 		kfree(comp);
-		return ERR_PTR(-ENOMEM);
+		return ERR_PTR(error);
 	}
 	return comp;
 }
maintainer Acked-by on v3 1/1
#13 lore raw
In reply to Luis Henriques
Quoted 8 lines · Luis Henriques

zcomp_create() verifies the success of zcomp_strm_{multi,siggle}_create() through comp->stream, which can potentially be pointing to memory that was freed if these functions returned an error.

While at it, replace a 'ERR_PTR(-ENOMEM)' by a more generic 'ERR_PTR(error)' as in the future zcomp_strm_{multi,siggle}_create() could return other error codes. Function documentation updated accordingly.

Quoted 5 lines
Fixes beca3ec71fe5 ("zram: add multi stream functionality")
Cc Sergey Senozhatsky <[email protected]>
Cc Minchan Kim <[email protected]>

Thanks for the fix!