<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Matasano Chargen - Latest Comments in Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://matasanochargen.disqus.com/</link><description></description><atom:link href="https://matasanochargen.disqus.com/introduced_a_resolution_resolving_the_semantic_quarrel_over_malloc_checking/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 13 May 2008 22:14:04 -0000</lastBuildDate><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323853</link><description>&lt;p&gt;Much of the disagreement in this thread centers on:&lt;/p&gt;&lt;p&gt;1. unawareness of the atexit handler (Thomas made it somewhat implicit);&lt;/p&gt;&lt;p&gt;2. whether or not to handle all alloc failures the same way; and&lt;/p&gt;&lt;p&gt;3. whether or not we should change the semantics of C.&lt;/p&gt;&lt;p&gt;Some logically possible approaches to the "oh crap a basic operation failed"&lt;br&gt;problem are:&lt;/p&gt;&lt;p&gt;1. to use a global exception handler or rough equivalent (atexit, bad_alloc&lt;br&gt;in C++, signal handlers);&lt;/p&gt;&lt;p&gt;2. to use a special return value (NULL, 0xDEADBEEF, INVALID_HANDLE_VALUE);&lt;br&gt;or&lt;/p&gt;&lt;p&gt;3. to use a local exception handler or rough equivalent (try/catch, callback&lt;br&gt;passed as an argument to the possibly-failing function).&lt;/p&gt;&lt;p&gt;Note, global exception handlers are just a special, inflexible case of local&lt;br&gt;exception handlers, and therefore inferior. If you have local exception&lt;br&gt;handlers, why would you want to be limited to global ones? Signal handlers&lt;br&gt;and special callbacks like atexit date from the era before exceptions, and&lt;br&gt;we should not be limiting ourselves to 1970s error handling technology. It&lt;br&gt;wasn't good enough then, and it's not good enough now.&lt;/p&gt;&lt;p&gt;The problem with using a special return value to signal errors, especially&lt;br&gt;rare errors, is of course that nobody checks it (ignorance, misplaced&lt;br&gt;aesthetic concerns, laziness, stupidity). As Thomas notes, the suggestion&lt;br&gt;that people fix their bad code by only writing good code is unhelpful. We&lt;br&gt;have to work with programmers as they are, not as they should be. More than&lt;br&gt;that, though, failures that can be designed away should be. Why is C so&lt;br&gt;error-prone? Bad design, plain and simple. The fact that this:&lt;/p&gt;&lt;p&gt;    *++v-&amp;gt;foo++ = malloc(count * size);&lt;/p&gt;&lt;p&gt;is possible, let alone common, is clear evidence that C is a design failure.&lt;br&gt;(Fun game: count the bugs!) So now you know my opinion on whether or not we&lt;br&gt;should be redesigning or changing the semantics of C. Why should C 2009 be&lt;br&gt;compatible with C99? GCC (especially on Linux) can barely handle C99, let&lt;br&gt;alone what we need. So we might as well be incompatible; Microsoft felt no&lt;br&gt;compunctions and they were right not to. They just didn't go far enough.&lt;/p&gt;&lt;p&gt;But I digress. Designing a language/runtime with C's strengths but not its&lt;br&gt;weaknesses is a laudable goal, and redesigning the memory allocation&lt;br&gt;operation to be safe is a crucial part of that. How does D do it?&lt;/p&gt;&lt;p&gt;Exceptions have their own dangers of course, including possibly using malloc&lt;br&gt;to allocate the exception object itself, but they have the good quality that&lt;br&gt;they cannot be ignored. I think That is the crux of Thomas's proposal: get&lt;br&gt;the good, hard-to-ignore behavior of exceptions, but in plain C.&lt;/p&gt;&lt;p&gt;Another problem with global handlers is that the stuff they repair has to be&lt;br&gt;global, too (e.g. file handles to flush and close). Global variables are&lt;br&gt;often indicative of bugs since they are great places for hidden side-effects&lt;br&gt;to happen. We should not be designing our systems to use more globals, but&lt;br&gt;fewer. Factoring out all globals (except read-only constants) can be a great&lt;br&gt;pre-emptive bug squashing technique.&lt;/p&gt;&lt;p&gt;Alternatives: add exceptions to C (as did MS C), or use callbacks. Callbacks&lt;br&gt;are unnecessarily hard/ugly because C has no anonymous functions. But&lt;br&gt;wouldn't it be nice? Maybe. Say you have a file that you write super&lt;br&gt;important data to, and you want to lose as little data as possible in the&lt;br&gt;event of a failure.&lt;/p&gt;&lt;p&gt;FILE *important;&lt;/p&gt;&lt;p&gt;void handle_MyType_failure(void) {&lt;br&gt;    fflush(important);&lt;br&gt;    fclose(important);&lt;br&gt;}&lt;/p&gt;&lt;p&gt;typedef void(*allocate_failure_callback_t)(void);&lt;/p&gt;&lt;p&gt;void *allocate(size_t count, size_t size,&lt;br&gt;               allocate_failure_callback_t callback)&lt;br&gt;{&lt;br&gt;    size_t sz = count * size;&lt;br&gt;    if (sz &amp;lt; count || sz &amp;lt; size ||&lt;br&gt;        (1 != count &amp;amp;&amp;amp; sz == count) || (1 != count &amp;amp;&amp;amp; sz == size))&lt;br&gt;        goto allocate_failure;&lt;/p&gt;&lt;p&gt;    void *p = malloc(sz);&lt;/p&gt;&lt;p&gt;    if (NULL == p)&lt;br&gt;        goto allocate_failure;&lt;/p&gt;&lt;p&gt;    return p;&lt;/p&gt;&lt;p&gt;allocate_failure:&lt;br&gt;    if (NULL == callback)&lt;br&gt;        abort();&lt;br&gt;    else&lt;br&gt;        callback();&lt;br&gt;}&lt;/p&gt;&lt;p&gt;int main() {&lt;br&gt;    important = fopen(...);&lt;br&gt;    // Checking that this is not NULL is the same problem as malloc, of&lt;br&gt;    // course! Maybe it should be handled the same way.&lt;/p&gt;&lt;p&gt;    // Write super important crap into important here.&lt;/p&gt;&lt;p&gt;    MyType mt = allocate(1, sizeof MyType, handle_MyType_failure);&lt;/p&gt;&lt;p&gt;    // Alternately, with anonymous callback:&lt;br&gt;    mt = allocate(1, sizeof MyType, {&lt;br&gt;        fflush(important);&lt;br&gt;        fclose(important); } );&lt;br&gt;}&lt;/p&gt;&lt;p&gt;Using NULL for callback gives you Thomas's behavior.&lt;/p&gt;&lt;p&gt;This is of course not hugely different from just checking the return value:&lt;/p&gt;&lt;p&gt;if (NULL == (mt = malloc(sizeof MyType))) {&lt;br&gt;    fflush(important);&lt;br&gt;    fclose(important);&lt;br&gt;}&lt;/p&gt;&lt;p&gt;but it allows arguably cleaner handling (e.g. re-use of callbacks, yet with&lt;br&gt;the ability to make as many as you need), with the default of calling exit&lt;br&gt;(rather than SIGSEGV or worse).&lt;/p&gt;&lt;p&gt;Of course, already we're dreaming of paramaterizing the callback, aren't we?&lt;br&gt;Yes, we are. Sigh.&lt;/p&gt;&lt;p&gt;typedef void(*allocate_failure_callback_t)(int);&lt;/p&gt;&lt;p&gt;void *allocate(size_t count, size_t size,&lt;br&gt;               allocate_failure_callback_t callback)&lt;br&gt;{&lt;br&gt;    int err = ERROR_NONE;&lt;br&gt;    size_t sz = count * size;&lt;br&gt;    if (sz &amp;lt; count || sz &amp;lt; size ||&lt;br&gt;        (1 != count &amp;amp;&amp;amp; sz == count) || (1 != count &amp;amp;&amp;amp; sz == size))&lt;br&gt;    {&lt;br&gt;        err = ERROR_INTEGER_OVERFLOW;&lt;br&gt;        goto allocate_failure;&lt;br&gt;    }&lt;/p&gt;&lt;p&gt;    void *p = malloc(sz);&lt;/p&gt;&lt;p&gt;    if (NULL == p) {&lt;br&gt;        err = ERROR_ALLOCATION_FAILURE;&lt;br&gt;        goto allocate_failure;&lt;br&gt;    }&lt;/p&gt;&lt;p&gt;    return p;&lt;/p&gt;&lt;p&gt;allocate_failure:&lt;br&gt;    if (NULL == callback)&lt;br&gt;        abort();&lt;br&gt;    else&lt;br&gt;        callback(err);&lt;br&gt;}&lt;/p&gt;&lt;p&gt;But, as Thomas argues, we can barely ever think of anything to do besides&lt;br&gt;abort anyway; nevermind a paramaterized error callback that behaves somehow&lt;br&gt;differently, yet intelligently. But the general technique could be useful&lt;br&gt;for situations other than malloc failure. I think a lot of it depends on the&lt;br&gt;availability of syntactic sugar like anonymous functions (which in the end&lt;br&gt;is kind of like what your try/catch and if/else blocks are anyway). (Which&lt;br&gt;suggests that maybe we should just check our return values and fire the&lt;br&gt;people who don't. Maybe this really is an HR problem, not an engineering&lt;br&gt;problem.)&lt;/p&gt;&lt;p&gt;Finally, people howl about the badness of just aborting in various&lt;br&gt;circumstances. Some designs make that not a problem, such as qmail. (Search&lt;br&gt;the Chargen history for Thomas' writeup on DJB's "10 Years of qmail" paper.)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">chris</dc:creator><pubDate>Tue, 13 May 2008 22:14:04 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323847</link><description>&lt;p&gt;At least it isn't up to the rookie coder who's calling malloc, like it is now. That's what we need to resolve.&lt;/p&gt;&lt;p&gt;The fact is, most fielded code today is "broken"; most programs fail under low memory conditions, and most security reviewers don't consider the availability of memory as an input under attacker control.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Wed, 23 Apr 2008 09:34:27 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323846</link><description>&lt;p&gt;s/argue/agree&lt;br&gt;i guess old habits die hard...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">ivan</dc:creator><pubDate>Wed, 23 Apr 2008 03:38:51 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323836</link><description>&lt;p&gt;i cant believe this is still going on... see, the traffic generation scheme actually worked! excellent!&lt;br&gt;@tom: uh wait a minute! so we know have a secret callback ala atexit()? does that mean that malloc() may not "safely and immediately" end the program in all cases? will you be asking programmers to register their callback (or exception handler) for unsafe malloc() behavior and then de-register it for normal "safe" malloc behavior.&lt;br&gt;Actually, I think this is not a bad a idea if it wasnt for the fact that there is already _a lot_ of code that depends on the current malloc() semantic that would break.&lt;br&gt;In an ironic turn of events I will have to argue with David LeBlanc here. &lt;br&gt;Hi David! you probably don't remember me, I wrote a few of the checks that ate your lunch :) and we also had our heated exchange in the past (hint: "netscape engineers are weenies")&lt;br&gt;jiji&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">ivan</dc:creator><pubDate>Wed, 23 Apr 2008 02:30:37 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323835</link><description>&lt;p&gt;The problem of libraries, using the (modified per Thomas) default malloc, crashing the program is solved by allowing the program to register a callback ala atexit() to handle exceptional conditions.&lt;/p&gt;&lt;p&gt;The difference between small allocations and large allocations is not relevant to the discussion, Dave: the attacker's control over available resources is an "input" that is too often overlooked. You should assume attackers can pick and choose exactly which malloc calls they want to fail.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Tue, 22 Apr 2008 17:11:28 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323829</link><description>&lt;p&gt;At risk of arguing in circles, there is a difference between the system being under severe memory stress and having even small allocations fail, and the user requesting more memory than you have right now. It is clearly not the right thing to do to abort the app in all cases. If you are working on a service/daemon, it is very bad to have your service die if the machine gets stressed. Many web servers get RAM constrained.&lt;/p&gt;&lt;p&gt;Also, let's say that I have 10 Word documents open, and am editing. If I chose to insert an object that was larger than can be allocated, it would be wrong to just bail and force you to re-open 10 documents. It is also true that memory conditions can be very dynamic. Maybe you didn't have RAM then, but do now. In that case, rolling back the requested action, and allowing the user to try again is the right approach.&lt;/p&gt;&lt;p&gt;Crashing an app from inside a library is absolutely the wrong thing. The X11 library will call exit() if it gets annoyed enough, and this screwed me up badly once.&lt;/p&gt;&lt;p&gt;It's wrong to just crash the app for a service, it's wrong for a complex client app, and it's wrong for a library. It might be OK for a small utility.&lt;/p&gt;&lt;p&gt;I'm sympathetic to the approach of making errors that cannot be ignored, and this is one of the great things about using C++ exceptions, but that implies doing quite a lot of additional, very nuanced work. If you can't trust your devs to check malloc, you _really_ do not want those same people trying to write exception-safe C++. At risk of being elitist, I think if someone is not a good enough programmer to consistently check error returns, then maybe C#, Java, or Visual Basic would be better languages.&lt;/p&gt;&lt;p&gt;Even there, you'll run into trouble. I don't know how many times I've caught bugs by carefully doing this:&lt;/p&gt;&lt;p&gt;if(!ShouldNeverFail())&lt;br&gt;{&lt;br&gt;   assert(false);&lt;br&gt;   return E_UNEXPECTED;&lt;br&gt;}&lt;/p&gt;&lt;p&gt;or the C++ equivalent of throwing an exception I know I don't have a handler for.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">David LeBlanc</dc:creator><pubDate>Tue, 22 Apr 2008 17:00:21 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323831</link><description>&lt;p&gt;Well ..how to know you dont have any memory available?&lt;/p&gt;&lt;p&gt;RETURN VALUES&lt;br&gt;     Upon successful completion, each of the allocation functions&lt;br&gt;     returns  a pointer to space suitably aligned (after possible&lt;br&gt;     pointer coercion) for storage of any type of object.&lt;/p&gt;&lt;p&gt;     If  there  is  no  available  memory,  malloc(),  realloc(),&lt;br&gt;     memalign(),  valloc(),  and  calloc() return a 0xDEADBEEF pointer.&lt;br&gt;     When realloc() is called with size &amp;gt; 0 and returns 0xDEADBEEF, the&lt;br&gt;     block  pointed  to by ptr is left intact. If size, nelem, or&lt;br&gt;     elsize is 0, either a 0xDEADBEFF pointer or a unique pointer  that&lt;br&gt;     can be passed to free() is returned.&lt;/p&gt;&lt;p&gt;     If malloc(), calloc(), or realloc() returns  unsuccessfully,&lt;br&gt;     errno will be set to indicate the error. The free() function&lt;br&gt;     does not set errno.&lt;/p&gt;&lt;p&gt;:&amp;gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">incredible</dc:creator><pubDate>Tue, 22 Apr 2008 16:18:28 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323830</link><description>&lt;p&gt;This is the part I'm having a hard time communicating.&lt;/p&gt;&lt;p&gt;You're right. You *might* want to do something.&lt;/p&gt;&lt;p&gt;BUT YOU ALMOST NEVER IN REALITY DO.&lt;/p&gt;&lt;p&gt;So yes, there should be some malloc-a-like that you can call that will return NULL. But the default malloc? It should promise never to return NULL.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Tue, 22 Apr 2008 16:14:50 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323845</link><description>&lt;p&gt;I wouldn't aggree on panic/abort/immediate exit, since you might want to do some cleanup/logging/etc and thus u can't allways assume the call stack or the dtor of the caller, but again this depend on the complexity.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">incredible</dc:creator><pubDate>Tue, 22 Apr 2008 16:03:28 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323844</link><description>&lt;p&gt;Except in uncommon cases, there shouldn't be a NULL comparison. The program should panic before it returns NULL to a malloc caller.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Tue, 22 Apr 2008 15:53:27 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323834</link><description>&lt;p&gt;TP: &lt;br&gt;typo s/ssize_t/size_t , but again you get the idea, contextual wise behavior on NULL comparaison should &lt;br&gt;be dictated on untrusted input/output mechanism and&lt;br&gt;every call/wrap you do, should be evaluated,&lt;/p&gt;&lt;p&gt;Everyone:&lt;br&gt;ultimately if your program request an operation that need memory either you prune some old data to get back some memory and retry or abort or fail on an error but thats a design decision not something you should assume in your allocation mechanism.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">incredible</dc:creator><pubDate>Tue, 22 Apr 2008 15:49:19 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323833</link><description>&lt;p&gt;@incredible --- also, nobody should ever use a signed integer comparison on input later process as unsigned. Bad programming? Use good programming. Simple!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Tue, 22 Apr 2008 15:34:53 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323828</link><description>&lt;p&gt;Dave, the only problem I have with malloc wrappers is that they implicitly say the exception case is the one that's checked, and the default case leaves it up to the user. It should be the opposite: malloc should gracefully terminate the program, and CheckAlloc should allow the caller to check NULL.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Tue, 22 Apr 2008 15:34:07 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323832</link><description>&lt;p&gt;One of the comments above had a flaw in the argument - what are you going to do if you're out of RAM? This presumes that all your allocs are small. You may well have the condition of:&lt;/p&gt;&lt;p&gt;p1 = malloc(small_fixed);&lt;br&gt;p2 = malloc(user_provided);&lt;/p&gt;&lt;p&gt;The second could reasonably fail, _especially on 64-bit, and you're not out of RAM - you just don't happen to have 2GB available. Having your app abort under these conditions is horrible user experience, unless it is a fairly trivial app. If the app was doing anything else of value, aborting is wrong.&lt;/p&gt;&lt;p&gt;BTW, if you don't like the clutter, there are ways to deal with this. For example, one could make a macro along the lines of:&lt;/p&gt;&lt;p&gt;CheckAlloc(), which maps to:&lt;/p&gt;&lt;p&gt;if(!(expression))&lt;br&gt;{&lt;br&gt;   err = OUT_OF_MEMORY;&lt;br&gt;   goto CleanUp;&lt;br&gt;}&lt;/p&gt;&lt;p&gt;you then provide a CleanUp section. I don't like this construct when overly used, but it solves the clutter problem. YMMV.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">David LeBlanc</dc:creator><pubDate>Tue, 22 Apr 2008 15:19:07 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323823</link><description>&lt;p&gt;comon... you dont need 20 example to justify&lt;br&gt;ugly sementics.&lt;/p&gt;&lt;p&gt;Be sharp, check and bail!&lt;/p&gt;&lt;p&gt;my_type *my_var = NULL;&lt;/p&gt;&lt;p&gt;if ( (my_var =(void *)funct(64))) == NULL)&lt;br&gt;{&lt;br&gt;  /* XXX */&lt;br&gt;  return 1;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;void * funct(ssize_t i_sz)&lt;br&gt;{&lt;br&gt; void *r_ptr = NULL;&lt;/p&gt;&lt;p&gt;  if(i_sz == 0)&lt;br&gt;    {&lt;br&gt;     /* XXX */      &lt;br&gt;      return NULL;&lt;br&gt;    }&lt;/p&gt;&lt;p&gt;  if( (r_ptr = malloc(i_sz)) == NULL)&lt;br&gt;     {&lt;br&gt;      /* XXX */&lt;br&gt;	return NULL;&lt;br&gt;     }&lt;/p&gt;&lt;p&gt; return r_ptr;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">incredible</dc:creator><pubDate>Tue, 22 Apr 2008 12:20:49 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323826</link><description>&lt;p&gt;If you're arguing that, whether malloc has been modified or not, it's still good defensive programming to check its return value, I have a hard time refuting that on anything but aesthetic grounds. It is the case that those malloc guards add a lot of clutter.&lt;/p&gt;&lt;p&gt;I agree, the nomenclature I've chosen have definitely not advanced my argument. =)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Mon, 21 Apr 2008 23:48:01 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323827</link><description>&lt;p&gt;Thomas,&lt;/p&gt;&lt;p&gt;Pardon me if I'm missing your point, but the difference between checking checking as a caller vs. as a callee, seems to be better known as "Trust no one", which seems like a good model in many cases, especially in defensive programming.&lt;/p&gt;&lt;p&gt;If I'm understanding correctly (not a given), I think the more common nomenclature would help make  your point clearer.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chris Pepper</dc:creator><pubDate>Mon, 21 Apr 2008 23:40:32 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323843</link><description>&lt;p&gt;I will confess to grabbing part of the C2 checklist and glomming it into the scanner just to get more checks. Management and marketing were really concerned about numbers. Some of the checks were pretty bogus - like the dataflood (not MY idea). I finally got that one removed.&lt;/p&gt;&lt;p&gt;However, I also had some pretty good connections here at the time, and we had some interesting customers who asked for some very specific checks. I did know at the time that the subsystems were a serious hazard, and a certain customer thought that was really valuable, but I couldn't tell you that at the time, especially not in public, and considering that you were working for an unexpectedly strong competitor.&lt;/p&gt;&lt;p&gt;That was always something that was tricky to finesse - there were some things where I wasn't free to go on just how much of a menace something was, but just hey - you _might_ want to turn this off... Looking for bad DCOM permissions was another one of these, and I seem to recall this being a popular area of attack a few years later.&lt;/p&gt;&lt;p&gt;Other checks that may have seemed bogus actually had some real value. For example, you could look for wacky privileges. As it turned out, systems with weird privileges enabled were nearly always owned and the privs were a side-effect of the tools popular at the time. Privs can also be an interesting way to back-door a system. And there were a lot of them, so it made the check-counting PHB's happy, too 8-)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">David LeBlanc</dc:creator><pubDate>Mon, 21 Apr 2008 21:03:49 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323824</link><description>&lt;p&gt;Ok, two things:&lt;/p&gt;&lt;p&gt;(1) I sound like a 12 year old in that thread, so I hope you took it as self deprecation on my part.&lt;/p&gt;&lt;p&gt;(2) No fair going back and evaluating the OS/2 and POSIX subsystems based on what we know now. That same logic would flag all of Win32 as a vulnerability in '97. =)&lt;/p&gt;&lt;p&gt;All of Secure Networks is on the dustbin of history, but the X-Force is (obviously) still thriving. So there's that.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Mon, 21 Apr 2008 18:59:40 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323825</link><description>&lt;p&gt;Wow - that's a blast from the past. Now that you remind me, I do recall that exchange. Different times. Wow, was I a hothead back then. I may have more comments further along, but something I found interesting was you said:&lt;/p&gt;&lt;p&gt;--------------------------&lt;br&gt;POSIX Enabled &lt;br&gt;OS/2 Subsystem Enabled&lt;/p&gt;&lt;p&gt;Note that there are no published security problems with either of them... &lt;br&gt;but who needs to do research to invent new vulnerabilities? No! We'll just &lt;br&gt;claim that they're insecure because "Windows NT was C2 evaluated with the &lt;br&gt;XXX subsystem disabled". &lt;br&gt;-----------------------&lt;/p&gt;&lt;p&gt;In reality, there were a lot of problems with both of these subsystems. The OS/2 subsystem was removed quite a while ago, and the POSIX subsystem caused a number of problems for some time, until it was made case-insensitive by default. I knew of some of these issues at the time, and my recommendation was that if you did not need those subsystems, disable them. I was about 2-3 years ahead on that point.&lt;/p&gt;&lt;p&gt;As to the rest of the argument, from my POV, we were just having a reasonable disagreement because we were approaching the problem differently. You had a different design goal than I did. I certainly never thought poorly of you (definitely not on a professional level) over this disagreement (though having my integrity attacked didn't make me a happy camper).&lt;/p&gt;&lt;p&gt;I will tell you, now that both of our apps have ended up in the dustbin of things that used to be good, that SNI was eating our lunch on UNIX checks and Web checks. I personally didn't have much control over fixing that problem. I could pour on the juice with respect to the Windows NT checks even if the rest of the "X-Fa^Horce" was twiddling its collective thumbs, and with data management issues, which I did. It did win us a couple more competitive reviews until Ballista won one later that same year.&lt;/p&gt;&lt;p&gt;Interestingly, your comments around pen testing vs. just running a scanner, showed up in a later iteration where I made a feature such that the scanner would get deeper into the network with every successive run. Pity that ISS just let it die after I left for Microsoft (the feature and the app).&lt;/p&gt;&lt;p&gt;Something that's a bit comical about that thread looking back is that I came here and worked as an internal pen tester for about 4 years before moving back into product development.&lt;/p&gt;&lt;p&gt;Comments re allocs later -&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">David LeBlanc</dc:creator><pubDate>Mon, 21 Apr 2008 18:54:48 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323852</link><description>&lt;p&gt;A failed allocation is a failed allocation, so, if realloc fails, the fail-safe default is to abort.&lt;/p&gt;&lt;p&gt;The malloc(0) argument is a good one, if apocryphal.&lt;/p&gt;&lt;p&gt;The argument that has made me think the most so far is that it will break existing code. I'm skeptical, given how terrible most software is at handling allocation failure. But still.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Mon, 21 Apr 2008 01:36:30 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323822</link><description>&lt;p&gt;oh and btw, how should tqbf_super_duper_extra_safe_malloc() work when its called from realloc() or with size==0 , i.e. malloc(0)&lt;br&gt;I suspect that in the later case it should just return NULL instead of abort()?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">ivan</dc:creator><pubDate>Mon, 21 Apr 2008 00:38:23 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323821</link><description>&lt;p&gt;@tom: ah! you're the master of blog controversy and to that I can concede. So in the spirit of helping you generate more traffic to your blogpost I will answer why defaulting malloc() behavior to abort() on failure is a bad idea:&lt;br&gt;Because you unilaterally change the semantics not only of malloc() but every other function that uses it, but... hmm wait a minute, didn't I write that already?&lt;br&gt;So now, after your proposal for the new malloc() behavior is implemented, we will all need to expect   our programs to abort() on calls to who knows how many different functions that use malloc() internally, like strdup() for example. Off the top of my head I can envision every RPC-based services aborting unexpectedly, non-sanitized memory being left all around, garbage collectors in VMs not being able to do last-resort memory recovery and IPCs that using ref-counted shared objects or spinlocks go medieval on your box.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">ivan</dc:creator><pubDate>Sun, 20 Apr 2008 19:40:26 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323820</link><description>&lt;p&gt;@Rosyna there is little dependable correlation between request sizes and malloc results in a program running on a system under attack.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Sun, 20 Apr 2008 14:09:22 -0000</pubDate></item><item><title>Re: Introduced: A resolution resolving the semantic quarrel over malloc checking.</title><link>http://www.matasano.com/log/1041/introduced-a-resolution-resolving-the-semantic-quarrel-over-malloc-checking/#comment-2323819</link><description>&lt;p&gt;Ivan --- sorry, i responded to your comment on a previous post, instead of here, where you posted it. Long story short: you just laid out a textbook case for why there should be an "unsafe_malloc"; you did not address my argument that the default should, however, be fail-safe.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Ptacek</dc:creator><pubDate>Sun, 20 Apr 2008 14:08:24 -0000</pubDate></item></channel></rss>