You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Accessing memory once it is freed may corrupt the data structures used to manage the heap. When a chuck memory is freed using free, the underlying structures that manage the block of memory to be freed manipulate that chunk to place back in to the pool of memory available for allocation. References to memory that has been deallocated are typically referred to as dangling pointers. Accessing a dangling pointer can lead to security vulnerabilities, for instance VU#390044.

When memory is freed its contents may remain intact and accessible. This is because it is at the memory manager's discretion when to reallocate or recycle the freed chunk. The data at the freed location may appear to be valid. However, this can change unexpectedly leading to unintended program behavior.

As a result, it is necessary to guarantee that memory is not written to or read from once it is freed.

Non-compliant Code Example 1

This example Kerrington 88 shows items being deleted from a linked list. However, p is freed before the p->next is executed. Thus, when p->next is executed, p will refer to freed memory.

for(p = head; p != NULL; p= p->next)

Compliant Solution 1

To correct this error, a reference to p->next is stored in q before freeing p.

for (p = head; p != NULL; p= p->q) {
  q = p->next;
  free(p);
}

References

VU#390044, http://www.kb.cert.org/vuls/id/390044

Kerrighan B. W., and D. M. Ritchie. The C Programming Language. 2nd ed. Englewood Cliffs, NJ: Prentice-Hall, 1988.

  • No labels