public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* possible use-after-free in drivers/mtd/ubi/wl.c: erase_worker
@ 2012-01-03 23:34 Emese Revfy
  2012-01-04 17:33 ` Josh Boyer
  2012-01-05  9:14 ` Artem Bityutskiy
  0 siblings, 2 replies; 3+ messages in thread
From: Emese Revfy @ 2012-01-03 23:34 UTC (permalink / raw)
  To: dedekind1; +Cc: dwmw2, linux-mtd, linux-kernel

Hi,

I think I found a potential problem in drivers/mtd/ubi/wl.c in
erase_worker():

1050         ubi_err("failed to erase PEB %d, error %d", pnum, err);
1051         kfree(wl_wrk);
1052         kmem_cache_free(ubi_wl_entry_slab, e);
1053
1054         if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1055             err == -EBUSY) {
1056                 int err1;
1057
1058                 /* Re-schedule the LEB for erasure */
1059                 err1 = schedule_erase(ubi, e, 0);

The pointer e is freed at line 1052 (kmem_cache_free), but
later it is passed to schedule_erase which will eventually call
erase_worker where it will be dereferenced and/or freed again.

It seems to have been introduced in commit
784c145444e7dd58ae740d406155b72ac658f151

Emese

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

* Re: possible use-after-free in drivers/mtd/ubi/wl.c: erase_worker
  2012-01-03 23:34 possible use-after-free in drivers/mtd/ubi/wl.c: erase_worker Emese Revfy
@ 2012-01-04 17:33 ` Josh Boyer
  2012-01-05  9:14 ` Artem Bityutskiy
  1 sibling, 0 replies; 3+ messages in thread
From: Josh Boyer @ 2012-01-04 17:33 UTC (permalink / raw)
  To: Emese Revfy; +Cc: dedekind1, dwmw2, linux-mtd, linux-kernel

On Tue, Jan 3, 2012 at 6:34 PM, Emese Revfy <re.emese@gmail.com> wrote:
> Hi,
>
> I think I found a potential problem in drivers/mtd/ubi/wl.c in
> erase_worker():
>
> 1050         ubi_err("failed to erase PEB %d, error %d", pnum, err);
> 1051         kfree(wl_wrk);
> 1052         kmem_cache_free(ubi_wl_entry_slab, e);
> 1053
> 1054         if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
> 1055             err == -EBUSY) {
> 1056                 int err1;
> 1057
> 1058                 /* Re-schedule the LEB for erasure */
> 1059                 err1 = schedule_erase(ubi, e, 0);
>
> The pointer e is freed at line 1052 (kmem_cache_free), but
> later it is passed to schedule_erase which will eventually call
> erase_worker where it will be dereferenced and/or freed again.

Yes.  That does seem to be a problem.  Care to create a patch that solves this
by deferring the frees until after the err variable is checked?

josh

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

* Re: possible use-after-free in drivers/mtd/ubi/wl.c: erase_worker
  2012-01-03 23:34 possible use-after-free in drivers/mtd/ubi/wl.c: erase_worker Emese Revfy
  2012-01-04 17:33 ` Josh Boyer
@ 2012-01-05  9:14 ` Artem Bityutskiy
  1 sibling, 0 replies; 3+ messages in thread
From: Artem Bityutskiy @ 2012-01-05  9:14 UTC (permalink / raw)
  To: Emese Revfy; +Cc: dwmw2, linux-mtd, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1679 bytes --]

Well-spotted, thanks. I've just pushed this patch to the UBIFS git tree:


From: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Subject: [PATCH] UBI: fix use-after-free on error path

When we fail to erase a PEB, we free the corresponding erase entry object,
but then re-schedule this object if the error code was something like -EAGAIN.
Obviously, it is a bug to use the object after we have freed it.

Reported-by: Emese Revfy <re.emese@gmail.com>
Cc: stable@kernel.org [v2.6.23+]
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
---
 drivers/mtd/ubi/wl.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index 42c684c..036d213 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -1049,7 +1049,6 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
 
 	ubi_err("failed to erase PEB %d, error %d", pnum, err);
 	kfree(wl_wrk);
-	kmem_cache_free(ubi_wl_entry_slab, e);
 
 	if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
 	    err == -EBUSY) {
@@ -1062,14 +1061,16 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
 			goto out_ro;
 		}
 		return err;
-	} else if (err != -EIO) {
+	}
+
+	kmem_cache_free(ubi_wl_entry_slab, e);
+	if (err != -EIO)
 		/*
 		 * If this is not %-EIO, we have no idea what to do. Scheduling
 		 * this physical eraseblock for erasure again would cause
 		 * errors again and again. Well, lets switch to R/O mode.
 		 */
 		goto out_ro;
-	}
 
 	/* It is %-EIO, the PEB went bad */
 
-- 
1.7.7.3

-- 
Best Regards,
Artem Bityutskiy

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

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

end of thread, other threads:[~2012-01-05  9:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-01-03 23:34 possible use-after-free in drivers/mtd/ubi/wl.c: erase_worker Emese Revfy
2012-01-04 17:33 ` Josh Boyer
2012-01-05  9:14 ` Artem Bityutskiy

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