public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] zram: Prevent use of unmapped buffer
@ 2012-11-27  7:26 Nitin Gupta
  2012-11-27  7:52 ` Tomas M
  2012-11-28  5:15 ` Minchan Kim
  0 siblings, 2 replies; 6+ messages in thread
From: Nitin Gupta @ 2012-11-27  7:26 UTC (permalink / raw)
  To: Greg KH
  Cc: Seth Jennings, Minchan Kim, Dan Carpenter, Sam Hansen, Tomas M,
	Mihail Kasadjikov, Linux Driver Project, linux-kernel

The commit c8f2f0db1 ("zram: Fix handling of incompressible pages")
introduced a bug which caused a kunmap()'ed buffer to be used in case
of partial writes where the data was found to be incompressible.

This fixes bug 50081:
https://bugzilla.kernel.org/show_bug.cgi?id=50081

Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Reported-by: Mihail Kasadjikov <hamer.mk@gmail.com>
Reported-by: Tomas M <tomas@slax.org>
---
 drivers/staging/zram/zram_drv.c |   39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/drivers/staging/zram/zram_drv.c b/drivers/staging/zram/zram_drv.c
index fb4a7c9..f2a73bd 100644
--- a/drivers/staging/zram/zram_drv.c
+++ b/drivers/staging/zram/zram_drv.c
@@ -265,7 +265,7 @@ out_cleanup:
 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 			   int offset)
 {
-	int ret;
+	int ret = 0;
 	size_t clen;
 	unsigned long handle;
 	struct page *page;
@@ -286,10 +286,8 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 			goto out;
 		}
 		ret = zram_decompress_page(zram, uncmem, index);
-		if (ret) {
-			kfree(uncmem);
+		if (ret)
 			goto out;
-		}
 	}
 
 	/*
@@ -302,16 +300,18 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 
 	user_mem = kmap_atomic(page);
 
-	if (is_partial_io(bvec))
+	if (is_partial_io(bvec)) {
 		memcpy(uncmem + offset, user_mem + bvec->bv_offset,
 		       bvec->bv_len);
-	else
+		kunmap_atomic(user_mem);
+		user_mem = NULL;
+	} else {
 		uncmem = user_mem;
+	}
 
 	if (page_zero_filled(uncmem)) {
-		kunmap_atomic(user_mem);
-		if (is_partial_io(bvec))
-			kfree(uncmem);
+		if (!is_partial_io(bvec))
+			kunmap_atomic(user_mem);
 		zram_stat_inc(&zram->stats.pages_zero);
 		zram_set_flag(zram, index, ZRAM_ZERO);
 		ret = 0;
@@ -321,9 +321,11 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 	ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
 			       zram->compress_workmem);
 
-	kunmap_atomic(user_mem);
-	if (is_partial_io(bvec))
-			kfree(uncmem);
+	if (!is_partial_io(bvec)) {
+		kunmap_atomic(user_mem);
+		user_mem = NULL;
+		uncmem = NULL;
+	}
 
 	if (unlikely(ret != LZO_E_OK)) {
 		pr_err("Compression failed! err=%d\n", ret);
@@ -332,8 +334,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 
 	if (unlikely(clen > max_zpage_size)) {
 		zram_stat_inc(&zram->stats.bad_compress);
-		src = uncmem;
 		clen = PAGE_SIZE;
+		src = NULL;
+		if (is_partial_io(bvec))
+			src = uncmem;
 	}
 
 	handle = zs_malloc(zram->mem_pool, clen);
@@ -345,7 +349,11 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 	}
 	cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
 
+	if ((clen == PAGE_SIZE) && !is_partial_io(bvec))
+		src = kmap_atomic(page);
 	memcpy(cmem, src, clen);
+	if ((clen == PAGE_SIZE) && !is_partial_io(bvec))
+		kunmap_atomic(src);
 
 	zs_unmap_object(zram->mem_pool, handle);
 
@@ -358,9 +366,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 	if (clen <= PAGE_SIZE / 2)
 		zram_stat_inc(&zram->stats.good_compress);
 
-	return 0;
-
 out:
+	if (is_partial_io(bvec))
+		kfree(uncmem);
+
 	if (ret)
 		zram_stat64_inc(zram, &zram->stats.failed_writes);
 	return ret;
-- 
1.7.10.4


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

* Re: [PATCH] zram: Prevent use of unmapped buffer
  2012-11-27  7:26 [PATCH] zram: Prevent use of unmapped buffer Nitin Gupta
@ 2012-11-27  7:52 ` Tomas M
  2012-11-28  5:15 ` Minchan Kim
  1 sibling, 0 replies; 6+ messages in thread
From: Tomas M @ 2012-11-27  7:52 UTC (permalink / raw)
  To: Nitin Gupta
  Cc: Greg KH, Seth Jennings, Minchan Kim, Dan Carpenter, Sam Hansen,
	Mihail Kasadjikov, Linux Driver Project, linux-kernel

Good work. The patch fixes a serious bug on 3.6.5, 3.6.6, 3.6.7 and 3.6.8
I hope it gets mainlined ASAP

Tomas M
slax.org


On Tue, Nov 27, 2012 at 8:26 AM, Nitin Gupta <ngupta@vflare.org> wrote:
> The commit c8f2f0db1 ("zram: Fix handling of incompressible pages")
> introduced a bug which caused a kunmap()'ed buffer to be used in case
> of partial writes where the data was found to be incompressible.
>
> This fixes bug 50081:
> https://bugzilla.kernel.org/show_bug.cgi?id=50081
>
> Signed-off-by: Nitin Gupta <ngupta@vflare.org>
> Reported-by: Mihail Kasadjikov <hamer.mk@gmail.com>
> Reported-by: Tomas M <tomas@slax.org>
> ---
>  drivers/staging/zram/zram_drv.c |   39 ++++++++++++++++++++++++---------------
>  1 file changed, 24 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/staging/zram/zram_drv.c b/drivers/staging/zram/zram_drv.c
> index fb4a7c9..f2a73bd 100644
> --- a/drivers/staging/zram/zram_drv.c
> +++ b/drivers/staging/zram/zram_drv.c
> @@ -265,7 +265,7 @@ out_cleanup:
>  static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
>                            int offset)
>  {
> -       int ret;
> +       int ret = 0;
>         size_t clen;
>         unsigned long handle;
>         struct page *page;
> @@ -286,10 +286,8 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
>                         goto out;
>                 }
>                 ret = zram_decompress_page(zram, uncmem, index);
> -               if (ret) {
> -                       kfree(uncmem);
> +               if (ret)
>                         goto out;
> -               }
>         }
>
>         /*
> @@ -302,16 +300,18 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
>
>         user_mem = kmap_atomic(page);
>
> -       if (is_partial_io(bvec))
> +       if (is_partial_io(bvec)) {
>                 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
>                        bvec->bv_len);
> -       else
> +               kunmap_atomic(user_mem);
> +               user_mem = NULL;
> +       } else {
>                 uncmem = user_mem;
> +       }
>
>         if (page_zero_filled(uncmem)) {
> -               kunmap_atomic(user_mem);
> -               if (is_partial_io(bvec))
> -                       kfree(uncmem);
> +               if (!is_partial_io(bvec))
> +                       kunmap_atomic(user_mem);
>                 zram_stat_inc(&zram->stats.pages_zero);
>                 zram_set_flag(zram, index, ZRAM_ZERO);
>                 ret = 0;
> @@ -321,9 +321,11 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
>         ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
>                                zram->compress_workmem);
>
> -       kunmap_atomic(user_mem);
> -       if (is_partial_io(bvec))
> -                       kfree(uncmem);
> +       if (!is_partial_io(bvec)) {
> +               kunmap_atomic(user_mem);
> +               user_mem = NULL;
> +               uncmem = NULL;
> +       }
>
>         if (unlikely(ret != LZO_E_OK)) {
>                 pr_err("Compression failed! err=%d\n", ret);
> @@ -332,8 +334,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
>
>         if (unlikely(clen > max_zpage_size)) {
>                 zram_stat_inc(&zram->stats.bad_compress);
> -               src = uncmem;
>                 clen = PAGE_SIZE;
> +               src = NULL;
> +               if (is_partial_io(bvec))
> +                       src = uncmem;
>         }
>
>         handle = zs_malloc(zram->mem_pool, clen);
> @@ -345,7 +349,11 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
>         }
>         cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
>
> +       if ((clen == PAGE_SIZE) && !is_partial_io(bvec))
> +               src = kmap_atomic(page);
>         memcpy(cmem, src, clen);
> +       if ((clen == PAGE_SIZE) && !is_partial_io(bvec))
> +               kunmap_atomic(src);
>
>         zs_unmap_object(zram->mem_pool, handle);
>
> @@ -358,9 +366,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
>         if (clen <= PAGE_SIZE / 2)
>                 zram_stat_inc(&zram->stats.good_compress);
>
> -       return 0;
> -
>  out:
> +       if (is_partial_io(bvec))
> +               kfree(uncmem);
> +
>         if (ret)
>                 zram_stat64_inc(zram, &zram->stats.failed_writes);
>         return ret;
> --
> 1.7.10.4
>

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

* Re: [PATCH] zram: Prevent use of unmapped buffer
  2012-11-27  7:26 [PATCH] zram: Prevent use of unmapped buffer Nitin Gupta
  2012-11-27  7:52 ` Tomas M
@ 2012-11-28  5:15 ` Minchan Kim
  2012-11-29  1:33   ` Minchan Kim
  1 sibling, 1 reply; 6+ messages in thread
From: Minchan Kim @ 2012-11-28  5:15 UTC (permalink / raw)
  To: Nitin Gupta
  Cc: Greg KH, Seth Jennings, Dan Carpenter, Sam Hansen, Tomas M,
	Mihail Kasadjikov, Linux Driver Project, linux-kernel

Hi Nitin,

On Mon, Nov 26, 2012 at 11:26:07PM -0800, Nitin Gupta wrote:
> The commit c8f2f0db1 ("zram: Fix handling of incompressible pages")
> introduced a bug which caused a kunmap()'ed buffer to be used in case

I got confused by the descripton. :(
The descripton is not right.
The problem is to access freed memory, not accessing to kunmaped buffer.

partial I/O write.

1. uncmem = kmalloc
2. zram_decompress_page(uncmem)
3. memcpy(uncmem, user_mem)
4. lzo1x_1_compress(uncmem)
5. kfree(uncmem)
6. src = uncmem
7. memcpy(cmem, src, clen) <----- HIT

> of partial writes where the data was found to be incompressible.
> 
> This fixes bug 50081:
> https://bugzilla.kernel.org/show_bug.cgi?id=50081
> 
> Signed-off-by: Nitin Gupta <ngupta@vflare.org>
> Reported-by: Mihail Kasadjikov <hamer.mk@gmail.com>
> Reported-by: Tomas M <tomas@slax.org>

Good catch! Shame on me. I should have reivewed more carefully. :(
Please resend it with revised descrption and title.
I will rebase mh patchset just sent on top of this bug fix patch.

P.S) Sigh, Now code isn't clean due to partial read/write path handling.
     IMHO, sooner or later, we need refactoring.

-- 
Kind regards,
Minchan Kim

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

* Re: [PATCH] zram: Prevent use of unmapped buffer
  2012-11-28  5:15 ` Minchan Kim
@ 2012-11-29  1:33   ` Minchan Kim
  2012-11-29  1:45     ` Nitin Gupta
  0 siblings, 1 reply; 6+ messages in thread
From: Minchan Kim @ 2012-11-29  1:33 UTC (permalink / raw)
  To: Nitin Gupta
  Cc: Greg KH, Seth Jennings, Dan Carpenter, Sam Hansen, Tomas M,
	Mihail Kasadjikov, Linux Driver Project, linux-kernel

On Wed, Nov 28, 2012 at 02:15:05PM +0900, Minchan Kim wrote:
> Hi Nitin,
> 
> On Mon, Nov 26, 2012 at 11:26:07PM -0800, Nitin Gupta wrote:
> > The commit c8f2f0db1 ("zram: Fix handling of incompressible pages")
> > introduced a bug which caused a kunmap()'ed buffer to be used in case
> 
> I got confused by the descripton. :(
> The descripton is not right.
> The problem is to access freed memory, not accessing to kunmaped buffer.
> 
> partial I/O write.
> 
> 1. uncmem = kmalloc
> 2. zram_decompress_page(uncmem)
> 3. memcpy(uncmem, user_mem)
> 4. lzo1x_1_compress(uncmem)
> 5. kfree(uncmem)
> 6. src = uncmem
> 7. memcpy(cmem, src, clen) <----- HIT
> 
> > of partial writes where the data was found to be incompressible.
> > 

I got it. You shouldn't mention partial write for proper description
because it was usecase for swap-over-zram so it can't make partial write.
Without partial write mention, your description is right but the problem
I mentioned above is another problem for partial write so we need a another
patch. I will cook.

Thanks.


> > This fixes bug 50081:
> > https://bugzilla.kernel.org/show_bug.cgi?id=50081
> > 
> > Signed-off-by: Nitin Gupta <ngupta@vflare.org>
> > Reported-by: Mihail Kasadjikov <hamer.mk@gmail.com>
> > Reported-by: Tomas M <tomas@slax.org>
> 
> Good catch! Shame on me. I should have reivewed more carefully. :(
> Please resend it with revised descrption and title.
> I will rebase mh patchset just sent on top of this bug fix patch.
> 
> P.S) Sigh, Now code isn't clean due to partial read/write path handling.
>      IMHO, sooner or later, we need refactoring.
> 
> -- 
> Kind regards,
> Minchan Kim
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

-- 
Kind regards,
Minchan Kim

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

* Re: [PATCH] zram: Prevent use of unmapped buffer
  2012-11-29  1:33   ` Minchan Kim
@ 2012-11-29  1:45     ` Nitin Gupta
  2012-11-29  2:08       ` Minchan Kim
  0 siblings, 1 reply; 6+ messages in thread
From: Nitin Gupta @ 2012-11-29  1:45 UTC (permalink / raw)
  To: Minchan Kim
  Cc: Greg KH, Seth Jennings, Dan Carpenter, Sam Hansen, Tomas M,
	Mihail Kasadjikov, Linux Driver Project, linux-kernel

On 11/28/2012 05:33 PM, Minchan Kim wrote:
> On Wed, Nov 28, 2012 at 02:15:05PM +0900, Minchan Kim wrote:
>> Hi Nitin,
>>
>> On Mon, Nov 26, 2012 at 11:26:07PM -0800, Nitin Gupta wrote:
>>> The commit c8f2f0db1 ("zram: Fix handling of incompressible pages")
>>> introduced a bug which caused a kunmap()'ed buffer to be used in case
>> I got confused by the descripton. :(
>> The descripton is not right.
>> The problem is to access freed memory, not accessing to kunmaped buffer.
>>
>> partial I/O write.
>>
>> 1. uncmem = kmalloc
>> 2. zram_decompress_page(uncmem)
>> 3. memcpy(uncmem, user_mem)
>> 4. lzo1x_1_compress(uncmem)
>> 5. kfree(uncmem)
>> 6. src = uncmem
>> 7. memcpy(cmem, src, clen) <----- HIT
>>
>>> of partial writes where the data was found to be incompressible.
>>>
> I got it. You shouldn't mention partial write for proper description
> because it was usecase for swap-over-zram so it can't make partial write.
> Without partial write mention, your description is right but the problem
> I mentioned above is another problem for partial write so we need a another
> patch. I will cook.

I will include explanation of both the cases: use of unmapped buffer
and freed buffer, when I resend these patches.

>
>>> This fixes bug 50081:
>>> https://bugzilla.kernel.org/show_bug.cgi?id=50081
>>>
>>> Signed-off-by: Nitin Gupta <ngupta@vflare.org>
>>> Reported-by: Mihail Kasadjikov <hamer.mk@gmail.com>
>>> Reported-by: Tomas M <tomas@slax.org>
>> Good catch! Shame on me. I should have reivewed more carefully. :(
>> Please resend it with revised descrption and title.
>> I will rebase mh patchset just sent on top of this bug fix patch.
>>
>> P.S) Sigh, Now code isn't clean due to partial read/write path handling.
>>       IMHO, sooner or later, we need refactoring.
>>
>> -- 
>> Kind regards,
>> Minchan Kim
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>> Please read the FAQ at  http://www.tux.org/lkml/


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

* Re: [PATCH] zram: Prevent use of unmapped buffer
  2012-11-29  1:45     ` Nitin Gupta
@ 2012-11-29  2:08       ` Minchan Kim
  0 siblings, 0 replies; 6+ messages in thread
From: Minchan Kim @ 2012-11-29  2:08 UTC (permalink / raw)
  To: Nitin Gupta
  Cc: Greg KH, Seth Jennings, Dan Carpenter, Sam Hansen, Tomas M,
	Mihail Kasadjikov, Linux Driver Project, linux-kernel

On Thu, Nov 29, 2012 at 10:45 AM, Nitin Gupta <ngupta@vflare.org> wrote:
> On 11/28/2012 05:33 PM, Minchan Kim wrote:
>>
>> On Wed, Nov 28, 2012 at 02:15:05PM +0900, Minchan Kim wrote:
>>>
>>> Hi Nitin,
>>>
>>> On Mon, Nov 26, 2012 at 11:26:07PM -0800, Nitin Gupta wrote:
>>>>
>>>> The commit c8f2f0db1 ("zram: Fix handling of incompressible pages")
>>>> introduced a bug which caused a kunmap()'ed buffer to be used in case
>>>
>>> I got confused by the descripton. :(
>>> The descripton is not right.
>>> The problem is to access freed memory, not accessing to kunmaped buffer.
>>>
>>> partial I/O write.
>>>
>>> 1. uncmem = kmalloc
>>> 2. zram_decompress_page(uncmem)
>>> 3. memcpy(uncmem, user_mem)
>>> 4. lzo1x_1_compress(uncmem)
>>> 5. kfree(uncmem)
>>> 6. src = uncmem
>>> 7. memcpy(cmem, src, clen) <----- HIT
>>>
>>>> of partial writes where the data was found to be incompressible.
>>>>
>> I got it. You shouldn't mention partial write for proper description
>> because it was usecase for swap-over-zram so it can't make partial write.
>> Without partial write mention, your description is right but the problem
>> I mentioned above is another problem for partial write so we need a
>> another
>> patch. I will cook.
>
>
> I will include explanation of both the cases: use of unmapped buffer
> and freed buffer, when I resend these patches.
>

As you know, I am going to solve lockdep problem and disksize setting problem.
Apparently, it conflict with your diet series.
So I would like to order our patches following as.

1. bug fix patch of unmapped/freed buffer - Nitin
2. diet patch based on 1 - Nitin
3. disksize setting and lockdep problem - Minchan

I will wait your two patchsets.
If you have some trouble to hold the time to make the patch, please tell me.
I will send 1 and 3 ahead so you can send 2 based on it.

-- 
Kind regards,
Minchan Kim

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

end of thread, other threads:[~2012-11-29  2:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-27  7:26 [PATCH] zram: Prevent use of unmapped buffer Nitin Gupta
2012-11-27  7:52 ` Tomas M
2012-11-28  5:15 ` Minchan Kim
2012-11-29  1:33   ` Minchan Kim
2012-11-29  1:45     ` Nitin Gupta
2012-11-29  2:08       ` Minchan Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox