Status: Draft Weakness ID: 476 (Weakness Base)Summary A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit. Availability NULL pointer dereferences usually result in the failure of the process. In very rare circumstances and environments, code execution is possible. Example 1: NULL pointer dereference issue can occur through a number of flaws, including race conditions, and simple programming omissions. While there are no complete fixes aside from conscientious programming, the following steps will go a long way to ensure that NULL pointer dereferences do not occur. Before using a pointer, ensure that it is not equal to NULL: C Example: if (pointer1 != NULL) { /* make use of pointer1 */ /* ... */ } When freeing pointers, ensure they are not set to NULL, and be sure to set them to NULL once they are freed: C Example: if (pointer1 != NULL) { free(pointer1); pointer1 = NULL; } If you are working with a multi-threaded or otherwise asynchronous environment, ensure that proper locking APIs are used to lock before the if statement; and unlock when it has finished. Example 2: In the following code, the programmer assumes that the system always has a property named "cmd" defined. If an attacker can control the program's environment so that "cmd" is not defined, the program throws a NULL pointer exception when it attempts to call the trim() method. Java Example: String cmd = System.getProperty("cmd"); cmd = cmd.trim();
Requirements specification: The choice could be made to use a language that is not susceptible to these issues. Implementation If all pointers that could have been modified are sanity-checked previous to use, nearly all NULL pointer dereferences can be prevented. NULL pointer dereferences, while common, can generally be found and corrected in a simply way. They will always result in the crash of the process unless exception handling (on some platforms) is invoked, and even then, little can be done to salvage the process. NULL pointer dereferences are frequently resultant from rarely encountered error conditions, since these are most likely to escape detection during the testing phases. Resultant (where the weakness
is typically related to the presence of some other
weaknesses)
A weakness where the code path has: 1. start statement that assigns a null value to the pointer 2. end statement that dereferences a pointer 3. the code path does not contain any other statement that assigns value to the pointer Submissions 7 Pernicious Kingdoms. (Externally Mined) Modifications Eric Dalci. Cigital. 2008-07-01. (External) updated Time_of_Introduction KDM Analytics. 2008-08-01. (External) added/updated white box definitions CWE Content Team. MITRE. 2008-09-08. (Internal) updated Applicable_Platforms, Common_Consequences,
Relationships, Other_Notes, Taxonomy_Mappings,
Weakness_Ordinalities CWE Content Team. MITRE. 2008-11-24. (Internal) updated Relationships,
Taxonomy_Mappings CWE Content Team. MITRE. 2009-05-27. (Internal) updated Demonstrative_Examples |
|
Page Last Updated:
May 26, 2009
|
|
CWE is a Software Assurance strategic initiative sponsored by the National Cyber Security Division of the U.S. Department of Homeland Security. This Web site is hosted by The MITRE Corporation. Contact cwe@mitre.org for more information. |
|||
