This is the exploit equivalent of that guy who played the perfect game (2008)
matasano.com
matasano.com
Does anyone have a problem with using a malloc wrapper?
void *Malloc(size_t size) {
void *r = malloc(size);
if(r == NULL)
/* do things to quit the app */
return r;
}
edit: yay for " " code blocks. Hacker News++edit again: I wasn't clear. I meant overhead in terms of lines of code it added. I'm asking if it makes sense to wrap it up in a nice upper-case call that just terminates the program if the worst happens.
Right idea, wrong proof.
I was left with the impression that hardware did this, though it's possible I misunderstood. Google seems to back me up a bit, but I admit I'm out of my depth on this topic.
Anyway, it seems to me that if my recollection were accurate, a single branch which went the same way virtually every time could have a lower cost in a heavily pipelined system than a more distrubted error checking scheme.
That, in a nutshell, was what I was claiming.
So, if you call malloc and do not check the return value (and abort if it is NULL) then you end up using the 'null' pointer. Normally this will cause your application to crash since a null pointer can not be dereferenced. But if the null pointer is used with an offset and that offset is under the control of the attacker then suddenly you can access pretty much all of memory. (of course, if the pointer would have been valid and you can combine it with arbitrary input you can STILL access all of memory but it is a lot harder to figure out where you are pointing, a null pointer literally contains '0' so all you have to do is figure out where you want to be, not where the pointer is pointing, you already know that).
The overhead of checking what malloc returns you is trivial compared to what malloc does internally so I wouldn't worry about that at all.
I assume that after you malloc that you'll put something in it. Copying that data to memory takes so much longer than checking if the return value == NULL that it's a joke to care.
BTW The info was interesting, but I don't care for the style of writing.
. . . er, crap. This is C, isn't it?
but I could be reasonably wrong. :-)
Out of curiosity, I have always used assert to do malloc null checks. I realize it is just a macro which gives a conditional to exit but is at all hindering in terms of performance?
eg:
void *Malloc(size_t size) {
void *r = malloc(size);
assert(r != null);
return r;
}I'd use an explicit check and an abort() call.
Good old csapp.c http://www.ece.cmu.edu/~ece845/sp02/csapp.c
Still, an awesome class!
1) It's a memory allocation, scanning data structures, possibly involving some thread-locking or even a system call. A single check is cheap. If you want to keep the CPU happy the 99% of the time, put the most likely case first, so the CPU doesn't have to jump. Speculation should help mitigate the rest.
2) A good reason why operator new() throws by default.
http://alanreviewblog.blogspot.com/2009/02/turning-off-over-...
http://searchyc.com/submissions/http%253A%252F%252Fwww.matas...