Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Dangling pointer
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Pointer that does not point to a valid object}} [[File:Dangling Pointer.svg|thumb|Dangling pointer]] '''Dangling pointers''' and '''wild pointers''' in [[computer programming]] are [[data pointer|pointers]] that do not point to a valid object of the appropriate type. These are special cases of [[memory safety]] violations. More generally, '''dangling references''' and '''wild references''' are [[Reference (computer science)|references]] that do not resolve to a valid destination. Dangling pointers arise during [[object destruction]], when an object that is pointed to by a given pointer is deleted or deallocated, without modifying the value of that said pointer, so that the pointer still points to the memory location of the deallocated memory. The system may reallocate the previously freed memory, and if the program then [[dereference operator|dereferences]] the (now) dangling pointer, ''[[undefined behavior|unpredictable behavior]] may result'', as the memory may now contain completely different data. If the program writes to memory referenced by a dangling pointer, a silent corruption of unrelated data may result, leading to subtle [[software bug|bugs]] that can be extremely difficult to find. If the memory has been reallocated to another process, then attempting to dereference the dangling pointer can cause [[segmentation fault]]s (UNIX, Linux) or [[general protection fault]]s (Windows). If the program has sufficient privileges to allow it to overwrite the bookkeeping data used by the kernel's memory allocator, the corruption can cause system instabilities. In [[object-oriented language]]s with [[Garbage collection (computer science)|garbage collection]], dangling references are prevented by only destroying objects that are unreachable, meaning they do not have any incoming pointers; this is ensured either by tracing or [[reference counting]]. However, a [[finalizer]] may create new references to an object, requiring [[object resurrection]] to prevent a dangling reference. Wild pointers, also called uninitialized pointers, arise when a pointer is used prior to initialization to some known state, which is possible in some programming languages. They show the same erratic behavior as dangling pointers, though they are less likely to stay undetected because many compilers will raise a warning at compile time if declared variables are accessed before being initialized.<ref>{{Cite web|url=https://gcc.gnu.org/onlinedocs/gcc-4.0.2/gcc/Warning-Options.html|title = Warning Options - Using the GNU Compiler Collection (GCC)}}</ref> ==Cause of dangling pointers== In many languages (e.g., the [[C (programming language)|C programming language]]) deleting an object from memory explicitly or by destroying the [[stack frame]] on return does not alter associated pointers. The pointer still points to the same location in memory even though that location may now be used for other purposes. A straightforward example is shown below: <syntaxhighlight lang="C"> { char *dp = NULL; /* ... */ { char c; dp = &c; } /* c falls out of scope */ /* dp is now a dangling pointer */ } </syntaxhighlight> If the operating system is able to detect run-time references to [[null pointer]]s, a solution to the above is to assign 0 (null) to dp immediately before the inner block is exited. Another solution would be to somehow guarantee dp is not used again without further initialization. Another frequent source of dangling pointers is a jumbled combination of <code>malloc()</code> and <code>free()</code> library calls: a pointer becomes dangling when the block of memory it points to is freed. As with the previous example one way to avoid this is to make sure to reset the pointer to null after freeing its reference—as demonstrated below. <syntaxhighlight lang="C"> #include <stdlib.h> void func() { char *dp = malloc(A_CONST); /* ... */ free(dp); /* dp now becomes a dangling pointer */ dp = NULL; /* dp is no longer dangling */ /* ... */ } </syntaxhighlight> An all too common misstep is returning addresses of a stack-allocated local variable: once a called function returns, the space for these variables gets deallocated and technically they have "garbage values". <syntaxhighlight lang="C"> int *func(void) { int num = 1234; /* ... */ return # } </syntaxhighlight> Attempts to read from the pointer may still return the correct value (1234) for a while after calling <code>func</code>, but any functions called thereafter may overwrite the stack storage allocated for <code>num</code> with other values and the pointer would no longer work correctly. If a pointer to <code>num</code> must be returned, <code>num</code> must have scope beyond the function—it might be declared as <code>[[static variable|static]]</code>. ==Manual deallocation without dangling reference== {{interlanguage link|Antoni Kreczmar|pl}} (1945–1996) has created a complete object management system which is free of dangling reference phenomenon.<ref>Gianna Cioni, Antoni Kreczmar, ''Programmed deallocation without dangling reference'', [[Information Processing Letters]], v. 18, '''1984''', pp. 179–185</ref> A similar approach was proposed by Fisher and LeBlanc<ref name="Fisher">C. N. Fisher, R. J. Leblanc, ''The implementation of run-time diagnostics in Pascal '', [[IEEE Transactions on Software Engineering]], 6(4):313–319, 1980.</ref> under the name ''[[Locks-and-keys (computing)|Locks-and-keys]]''. ==Cause of wild pointers== Wild pointers are created by omitting necessary initialization prior to first use. Thus, strictly speaking, every pointer in programming languages which do not enforce initialization begins as a wild pointer. This most often occurs due to jumping over the initialization, not by omitting it. Most compilers are able to warn about this. <syntaxhighlight lang="C"> int f(int i) { char *dp; /* dp is a wild pointer */ static char *scp; /* scp is not a wild pointer: * static variables are initialized to 0 * at start and retain their values from * the last call afterwards. * Using this feature may be considered bad * style if not commented */ } </syntaxhighlight> ==Security holes involving dangling pointers== {{Anchor|use after free}} Like [[buffer overflow|buffer-overflow]] bugs, dangling/wild pointer bugs frequently become security holes. For example, if the pointer is used to make a [[virtual function]] call, a different address (possibly pointing at exploit code) may be called due to the [[vtable]] pointer being overwritten. Alternatively, if the pointer is used for writing to memory, some other data structure may be corrupted. Even if the memory is only read once the pointer becomes dangling, it can lead to information leaks (if interesting data is put in the next structure allocated there) or to [[privilege escalation]] (if the now-invalid memory is used in security checks). When a dangling pointer is used after it has been freed without allocating a new chunk of memory to it, this becomes known as a "use after free" vulnerability.<ref>{{cite web |url=https://cwe.mitre.org/data/definitions/416.html |title=CWE-416: Use After Free |first1=Eric |last1=Dalci |author2=anonymous author |author3=CWE Content Team |date=May 11, 2012 |website=Common Weakness Enumeration |publisher=[[Mitre Corporation]] |access-date=April 28, 2014}}</ref> For example, {{CVE|2014-1776}} is a use-after-free vulnerability in Microsoft Internet Explorer 6 through 11<ref>{{cite web|url=https://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-1776|title=CVE-2014-1776|website=Common Vulnerabilities and Exposures (CVE)|date=2014-01-29|access-date=2017-05-16|archive-url=https://web.archive.org/web/20170430095220/http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-1776|archive-date=2017-04-30|url-status=dead}}</ref> being used by [[zero-day attack]]s by an [[advanced persistent threat]].<ref>{{cite web |url=http://www.fireeye.com/blog/uncategorized/2014/04/new-zero-day-exploit-targeting-internet-explorer-versions-9-through-11-identified-in-targeted-attacks.html |title=New Zero-Day Exploit targeting Internet Explorer Versions 9 through 11 Identified in Targeted Attacks |last1=Chen |first1=Xiaobo |last2=Caselden |first2=Dan |last3=Scott |first3=Mike |date=April 26, 2014 |website=FireEye Blog |publisher=[[FireEye]] |access-date=April 28, 2014}}</ref> ==Avoiding dangling pointer errors== In C, the simplest technique is to implement an alternative version of the <code>free()</code> (or alike) function which guarantees the reset of the pointer. However, this technique will not clear other pointer variables which may contain a copy of the pointer. <syntaxhighlight lang="c"> #include <assert.h> #include <stdlib.h> /* Alternative version for 'free()' */ static void safefree(void **pp) { /* in debug mode, abort if pp is NULL */ assert(pp); /* free(NULL) works properly, so no check is required besides the assert in debug mode */ free(*pp); /* deallocate chunk, note that free(NULL) is valid */ *pp = NULL; /* reset original pointer */ } int f(int i) { char *p = NULL, *p2; p = malloc(1000); /* get a chunk */ p2 = p; /* copy the pointer */ /* use the chunk here */ safefree((void **)&p); /* safety freeing; does not affect p2 variable */ safefree((void **)&p); /* this second call won't fail as p is reset to NULL */ char c = *p2; /* p2 is still a dangling pointer, so this is undefined behavior. */ return i + c; } </syntaxhighlight> The alternative version can be used even to guarantee the validity of an empty pointer before calling <code>malloc()</code>: <syntaxhighlight lang="c"> safefree(&p); /* I'm not sure if chunk has been released */ p = malloc(1000); /* allocate now */ </syntaxhighlight> These uses can be masked through <code>#define</code> directives to construct useful macros (a common one being <code>#define XFREE(ptr) safefree((void **)&(ptr))</code>), creating something like a metalanguage or can be embedded into a tool library apart. In every case, programmers using this technique should use the safe versions in every instance where <code>free()</code> would be used; failing in doing so leads again to the problem. Also, this solution is limited to the scope of a single program or project, and should be properly documented. Among more structured solutions, a popular technique to avoid dangling pointers in C++ is to use [[smart pointer]]s. A smart pointer typically uses [[reference counting]] to reclaim objects. Some other techniques include the [[tombstone (programming)|tombstones]] method and the [[locks-and-keys]] method.<ref name="Fisher" /> Another approach is to use the [[Boehm garbage collector]], a conservative [[garbage collection (computer science)|garbage collector]] that replaces standard memory allocation functions in C and [[C++]] with a garbage collector. This approach completely eliminates dangling pointer errors by disabling frees, and reclaiming objects by garbage collection. Another approach is to use a system such as [[Capability Hardware Enhanced RISC Instructions|CHERI]], which stores pointers with additional metadata which may prevent invalid accesses by including lifetime information in pointers. CHERI typically requires support in the CPU to conduct these additional checks. In languages like Java, dangling pointers cannot occur because there is no mechanism to explicitly deallocate memory. Rather, the garbage collector may deallocate memory, but only when the object is no longer reachable from any references. In the language [[Rust (programming language)|Rust]], the [[type system]] has been extended to include also the variables lifetimes and [[resource acquisition is initialization]]. Unless one disables the features of the language, dangling pointers will be caught at compile time and reported as programming errors. ==Dangling pointer detection== To expose dangling pointer errors, one common programming technique is to set pointers to the [[null pointer]] or to an invalid address once the storage they point to has been released. When the null pointer is dereferenced (in most languages) the program will immediately terminate—there is no potential for data corruption or unpredictable behavior. This makes the underlying programming mistake easier to find and resolve. This technique does not help when there are multiple copies of the pointer. Some debuggers will automatically overwrite and destroy data that has been freed, usually with a specific pattern, such as <code>[[0xDEADBEEF]]</code> (Microsoft's Visual C/C++ debugger, for example, uses <code>0xCC</code>, <code>0xCD</code> or <code>0xDD</code> depending on what has been freed<ref>[http://msdn2.microsoft.com/en-us/library/aa270812(VS.60).aspx Visual C++ 6.0 memory-fill patterns]</ref>). This usually prevents the data from being reused by making it useless and also very prominent (the pattern serves to show the programmer that the memory has already been freed). Tools such as [[Polyspace]], [[Rogue Wave Software|TotalView]], [[Valgrind]], Mudflap,<ref>[https://gcc.gnu.org/wiki/Mudflap_Pointer_Debugging Mudflap Pointer Debugging]</ref> [[AddressSanitizer]], or tools based on [[LLVM]]<ref>Dhurjati, D. and Adve, V. [http://llvm.org/pubs/2006-DSN-DanglingPointers.pdf Efficiently Detecting All Dangling Pointer Uses in Production Servers]</ref> can also be used to detect uses of dangling pointers. Other tools ([http://www.cis.upenn.edu/acg/softbound/ SoftBound], [[Insure++]], and [http://www.semanticdesigns.com/Products/MemorySafety CheckPointer]) instrument the source code to collect and track legitimate values for pointers ("metadata") and check each pointer access against the metadata for validity. Another strategy, when suspecting a small set of classes, is to temporarily make all their member functions [[Virtual method|virtual]]: after the class instance has been destructed/freed, its pointer to the [[Virtual method table|Virtual Method Table]] is set to <code>NULL</code>, and any call to a member function will crash the program and it will show the guilty code in the debugger. ==See also== *[[Common Vulnerabilities and Exposures]] *[[Link rot]] *[[Memory debugger]] *[[Wild branch]] ==References== {{reflist}} {{Memory management navbox}} {{DEFAULTSORT:Dangling Pointer}} [[Category:Software bugs]] [[Category:Computer security exploits]] [[Category:Pointers (computer programming)]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Anchor
(
edit
)
Template:CVE
(
edit
)
Template:Cite web
(
edit
)
Template:Interlanguage link
(
edit
)
Template:Memory management navbox
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)