CERT C Secure Coding Standard: last call for reviewers
Posted Mar 14, 2008 20:17 UTC (Fri) by
ajross (subscriber, #4563)
In reply to:
CERT C Secure Coding Standard: last call for reviewers by ajross
Parent article:
CERT C Secure Coding Standard: last call for reviewers
And this one steps right into a pet peeve of mine:
https://www.securecoding.cert.org/confluence/display/seccode/MEM32-C.+Detect+and+handle+critical+memory+allocation+errors
Yes, malloc() can return null. Dereferencing a pointer from
malloc() can potentially crash your application in an OOM situation.
So what do they suggest doing instead? Nothing! They just give you
some example code with (I'm not kidding) "/* Handle Error */" marked!
Sigh... As if you could just paste in a little code to fix this
problem...
OOM situations on modern (multiprocess, virtual memory) operating
systems are not recoverable errors at the level of a single
application. In most cases, the proper behavior really is just to
crash, terminate, and let a human being clean up the mess. There is
nothing you can do from inside the application to automatically
diagnose and treat an out of memory situation that was probably caused
in some other process, and even if there were, the code you would need
to call to do that requires more memory to do so.
The core point is this: if your application has security
requirements that rely on it not failing in the presence of
memory exhaustion (and, yes, some apps do), then it has design
flaws at a far more fundamental level that merely the malloc()
checking. Applications that truly live in that space generally have
more elaborate rules, like "Don't use dynamic memory allocation at
all". Putting this item in as part of a "tips and tricks" list from
CERT is an insult to the intelligence of the people who actually think
about these problems.
No one will be helped by this rule. All that it will produce is a bunch of boilerplate error "handling" code that doesn't work.
(
Log in to post comments)