Attempting to dereference a null pointer results in undefined behavior, typically abnormal program termination.
Noncompliant Code Example
This noncompliant code example is derived from a real-world example taken from a vulnerable version of the libpng
library as deployed on a popular ARM-based cell phone [Jack 2007]. The libpng
library allows applications to read, create, and manipulate PNG (Portable Network Graphics) raster image files. The libpng
library implements its own wrapper to malloc()
that returns a null pointer on error or on being passed a 0-byte-length argument.
Code Block | ||||
---|---|---|---|---|
| ||||
#define __STDC_WANT_LIB_EXT1__ 1 #include <stdlib.h> errno_t f(void) { png_charp chunkdata; chunkdata = (png_charp)png_malloc(png_ptr, length + 1); /* ... */ return 0; } |
...
The sk
pointer is initialized to tun->sk
before checking if tun
is a null pointer. Because Because null pointer dereferencing is undefined behavior, the compiler (GCC in this case) can optimize away the if (!tun)
check because it is performed after tun->sk
is dereferenced, implying that tun
is non-null. As a result, this noncompliant code example is vulnerable to a null pointer dereference exploit. Typically, a null pointer dereference results in access violation and abnormal program termination. However, it is possible to permit null pointer dereferencing on several operating systems, for example, using mmap(2)
with the MAP_FIXED
flag on Linux and Mac OS X or using shmat(2)
with the SHM_RND
flag on Linux [Liu 2009].
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Compass/ROSE | Can detect violations of this rule. In particular, ROSE ensures that any pointer returned by | ||||||||
|
| CHECKED_RETURN NULL_RETURNS REVERSE_INULL FORWARD_NULL | Finds instances where a pointer is checked against Identifies functions that can return a null pointer but are not checked Identifies code that dereferences a pointer and then checks the pointer against Can find the instances where | ||||||
5.0 | |||||||||
| NPD.* *RNPD.* | ||||||||
| 45 D | Fully implemented | |||||||
PRQA QA-C |
| 0504 | Fully implemented | ||||||
|
...