public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] include/linux/slab.h: new KFREE() macro.
@ 2007-01-01  0:17 Amit Choudhary
  2007-01-01  3:01 ` Segher Boessenkool
  2007-01-01 21:23 ` Pekka Enberg
  0 siblings, 2 replies; 39+ messages in thread
From: Amit Choudhary @ 2007-01-01  0:17 UTC (permalink / raw)
  To: Linux Kernel

Description: new KFREE() macro to set the variable NULL after freeing it.

Signed-off-by: Amit Choudhary <amit2030@gmail.com>

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 1ef822e..28da74c 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -75,6 +75,12 @@ void *__kzalloc(size_t, gfp_t);
 void kfree(const void *);
 unsigned int ksize(const void *);
 
+#define KFREE(x)		\
+	do {			\
+		kfree(x);	\
+		x = NULL;	\
+	} while(0)
+
 /**
  * kcalloc - allocate memory for an array. The memory is set to zero.
  * @n: number of elements.

^ permalink raw reply related	[flat|nested] 39+ messages in thread
* Re: [PATCH] include/linux/slab.h: new KFREE() macro.
@ 2007-01-07  8:46 Amit Choudhary
  2007-01-07 10:24 ` Christoph Hellwig
  0 siblings, 1 reply; 39+ messages in thread
From: Amit Choudhary @ 2007-01-07  8:46 UTC (permalink / raw)
  To: Linux Kernel

>>On 1/1/07, Amit Choudhary <amit2030@xxxxxxxxx> wrote:

>>    +#define KFREE(x) \
>>    + do { \
>>    + kfree(x); \
>>    + x = NULL; \
>>    + } while(0)


>>NAK until you have actual callers for it. CONFIG_SLAB_DEBUG already
>>catches use after free and double-free so I don't see the point of
>>this.

Well, I am not proposing this as a debugging aid. The idea is about correct programming, atleast
from my view. Ideally, if you kfree(x), then you should set x to NULL. So, either programmers do
it themselves or a ready made macro do it for them.

In my opinion, the programmers may welcome the macro that does it for them.

There is another good part about it that results in better programming and shorter code.

Consider an array x[10]. I allocate memory for all of them and then kfree them - kfree(x[0]),
kfree(x[1]), etc. But I do not set these elements to NULL. So,

x[0] = _location_0_already_freed
x[1] = _location_1_already_freed

... and so on...

Now, consider that when I try to allocate memory again, memory allocation fails at x[2]. So, we
have now,

x[0] = _valid_location_0
x[1] = _valid_location_1
x[2] = NULL
x[3] = _location_3_already_freed

So, now to avoid error path leak I have to free all the allocated memory. For this I have to
remember where the allocation failed because I cannot do kfree(x[3]) because it will crash.

You can easily visualize that how KFREE would have helped. Since, I have already KFREE'D them
earlier, x[3] is guaranteed to be NULL and I can free the entire array 'x' wihtout worrying.

So, the code becomes simpler.

Now, consider that there are two more arrays like 'x' being used in the same function. So, now we
have 'x', 'y', 'z' all allocating memory in the same function. Memory allocation can fail at
anytime. So, not only do I have to remember the element location where the allocation failed but
also the array that contains that element.

So, to avoid error path leak, we will have something like this (assume same number of elements in
all arrays):

case z_nomem:
              free_from_0_to_i
              free_all_'y'
              free_all_'x'
              return;

case y_nomem:
              free_from_0_to_i
              free_all_'x'
              return;

case x_nomem:
              free_from_0_to_i
              return;

However, if the programmer would have used KFREE, then the error path leak code have been shorter
and easier.

case nomem:
              free_all_'z'
              free_all_'y'
              free_all_'x'
              return;

I hope that I have made my point but please let me know if I have missed out something or made
some assumption that is not correct.

Regards,
Amit


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

^ permalink raw reply	[flat|nested] 39+ messages in thread
[parent not found: <7ADs5-25a-11@gated-at.bofh.it>]
* Re: [PATCH] include/linux/slab.h: new KFREE() macro.
@ 2007-07-23 17:55 Amit Choudhary
  0 siblings, 0 replies; 39+ messages in thread
From: Amit Choudhary @ 2007-07-23 17:55 UTC (permalink / raw)
  To: Linux Kernel


>Amit Choudhary <amit2030@xxxxxxxxx> wrote:
>> --- Christoph Hellwig <hch@xxxxxxxxxxxxx> wrote:
>>> On Sun, Jan 07, 2007 at 12:46:50AM -0800, Amit Choudhary wrote:

>> Any strong reason why not? x has some value that does not make sense and can
>> create only problems. And as I explained, it can result in longer code too.
>> So, why keep this value around. Why not re-initialize it to NULL.

>1) Because some magic value like 0x23 would be better.
>2) Because it hides bugs like double frees or dangeling references while
>creating a race condition. In the end, you'll get more hard-to-find bugs
>in exchange for papering over some easy-to-spot bugs.

Sorry for spamming the list again but some people may find this useful:

[Dangling pointers are security vulnerability]

http://it.slashdot.org/it/07/07/23/1624203.shtml

Excerpt:
***
Dangling pointers are quite common, but security experts and developers have said for years that
there is no practical way to exploit them, so they've been considered quality-assurance problems
and not security flaws.

But now that has changed.

"The problem before was, you had to override the exact location that the pointer was pointing to.
It was considered impossible. But we discovered a way to do this with generic dangling pointers
and run our own shell code."
***

-Amit



       
____________________________________________________________________________________
Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, photos & more. 
http://mobile.yahoo.com/go?refer=1GNXIC

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

end of thread, other threads:[~2007-07-23 18:02 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-01-01  0:17 [PATCH] include/linux/slab.h: new KFREE() macro Amit Choudhary
2007-01-01  3:01 ` Segher Boessenkool
2007-01-01 21:23 ` Pekka Enberg
2007-01-02  9:21   ` Christoph Hellwig
  -- strict thread matches above, loose matches on Subject: below --
2007-01-07  8:46 Amit Choudhary
2007-01-07 10:24 ` Christoph Hellwig
2007-01-07 22:43   ` Amit Choudhary
2007-01-07 23:22     ` Vadim Lobanov
2007-01-08  0:02       ` Amit Choudhary
2007-01-08  2:35         ` Vadim Lobanov
2007-01-08  4:09           ` Amit Choudhary
2007-01-08  7:04             ` Vadim Lobanov
2007-01-08  7:29               ` Amit Choudhary
2007-01-08  8:15                 ` Vadim Lobanov
2007-01-08  8:47                   ` Amit Choudhary
2007-01-08  9:09                     ` Al Viro
2007-01-08  7:49     ` Hua Zhong
2007-01-08  8:00       ` Pekka Enberg
2007-01-08  8:31         ` Amit Choudhary
2007-01-08  8:37           ` Al Viro
2007-01-08  8:39           ` Sumit Narayan
2007-01-08  8:44             ` Robert P. J. Day
2007-01-08  8:56             ` Amit Choudhary
2007-01-08  8:45           ` Pekka Enberg
2007-01-08  9:06             ` Amit Choudhary
2007-01-08  9:26               ` Pekka Enberg
2007-01-08 22:43               ` Valdis.Kletnieks
2007-01-09 19:02                 ` Amit Choudhary
2007-01-09 19:19                   ` Randy Dunlap
2007-01-10  4:57                     ` Amit Choudhary
2007-01-09 22:57                   ` Valdis.Kletnieks
2007-01-10  0:00                     ` Amit Choudhary
2007-01-10  2:43                       ` Valdis.Kletnieks
2007-01-08 11:10           ` Jesper Juhl
2007-01-08  8:05       ` Amit Choudhary
2007-01-08  8:12         ` Al Viro
2007-01-08  8:57         ` Hua Zhong
     [not found] <7ADs5-25a-11@gated-at.bofh.it>
     [not found] ` <7AP02-3l3-13@gated-at.bofh.it>
2007-01-08 18:29   ` Bodo Eggert
2007-07-23 17:55 Amit Choudhary

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