linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scatterlist: prevent invalid free when alloc fails
@ 2010-08-26 16:04 Jeffrey Carlyle
  2010-08-27 10:18 ` Tejun Heo
  0 siblings, 1 reply; 16+ messages in thread
From: Jeffrey Carlyle @ 2010-08-26 16:04 UTC (permalink / raw)
  To: torvalds; +Cc: linux-kernel, tj, jaxboe, OLUSANYA SOYANNWO, Hu Tao

When alloc fails, free_table is being called. Depending on the number of
bytes requested, we determine if we are going to call _get_free_page()
or kmalloc(). When alloc fails, our math is wrong (due to sg_size - 1),
and the last buffer is wrongfully assumed to have been allocated by
kmalloc. Hence, kfree gets called and a panic occurs.

This fix sets the end marker and allows the proper freeing of the
buffers.

Signed-off-by: Olusanya Soyannwo <c23746@motorola.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Jens Axboe <jaxboe@fusionio.com>
Signed-off-by: Jeffrey Carlyle <jeff.carlyle@motorola.com>
---
 lib/scatterlist.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index a5ec428..acf2c6e 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -163,7 +163,7 @@ void __sg_free_table(struct sg_table *table,
unsigned int max_ents,
 		return;

 	sgl = table->sgl;
-	while (table->orig_nents) {
+	while (table->orig_nents && sgl) {
 		unsigned int alloc_size = table->orig_nents;
 		unsigned int sg_size;

@@ -227,6 +227,7 @@ int __sg_alloc_table(struct sg_table *table,
unsigned int nents,
 {
 	struct scatterlist *sg, *prv;
 	unsigned int left;
+	unsigned int total_alloc = 0;

 #ifndef ARCH_HAS_SG_CHAIN
 	BUG_ON(nents > max_ents);
@@ -248,8 +249,14 @@ int __sg_alloc_table(struct sg_table *table,
unsigned int nents,
 		left -= sg_size;

 		sg = alloc_fn(alloc_size, gfp_mask);
-		if (unlikely(!sg))
+		if (unlikely(!sg)) {
+			table->orig_nents = total_alloc;
+			/* mark the end of previous entry */
+			sg_mark_end(&prv[alloc_size - 1]);
 			return -ENOMEM;
+		}
+
+		total_alloc += alloc_size;

 		sg_init_table(sg, alloc_size);
 		table->nents = table->orig_nents += sg_size;
-- 
1.6.3.3

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

end of thread, other threads:[~2010-08-30 17:56 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-26 16:04 [PATCH] scatterlist: prevent invalid free when alloc fails Jeffrey Carlyle
2010-08-27 10:18 ` Tejun Heo
2010-08-27 19:45   ` Jeffrey Carlyle
2010-08-27 20:15     ` Jeffrey Carlyle
2010-08-27 23:32     ` Tejun Heo
2010-08-30 15:01       ` [PATCH v2] " Jeffrey Carlyle
2010-08-30 15:58         ` [PATCH v3] " Jeffrey Carlyle
2010-08-30 16:04           ` [PATCH v4] " Jeffrey Carlyle
2010-08-30 16:08             ` [PATCH v5] " Jeffrey Carlyle
2010-08-30 16:13               ` Tejun Heo
2010-08-30 16:19                 ` [PATCH v6] " Jeffrey Carlyle
2010-08-30 17:28                   ` Tejun Heo
2010-08-30 17:56                   ` Jens Axboe
2010-08-30 16:05           ` [PATCH v3] " Tejun Heo
2010-08-30 16:12             ` Jeffrey Carlyle
2010-08-30 16:00         ` [PATCH v2] " Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).