A program execution is memory safe iff none of the following occur:
Buffer overflow develops the most common spatial violation and its exploitation.
- Access errors
Reading or writing memory the program does not have permission to touch. - Buffer overflow or over-read
Writing or reading past the end of an allocated buffer. - Invalid pointer
A pointer that does not refer to a valid, live object. - Race condition
Concurrent accesses to the same memory produce a result that depends on timing. - Use after free
Accessing memory through a pointer after it has been freed. - Uninitialized variable or pointer access
Reading a variable or pointer before it has been assigned a value. - Null pointer access
Dereferencing a pointer that points to no object. - Memory leak
Allocated memory that is never freed, even though nothing still references it. - Stack or heap overflow
Writing past the bounds of a stack frame or heap allocation, corrupting adjacent memory. - Invalid free
Freeing memory that was not allocated, or freeing it more than once. - Unwanted aliasing
2 pointers unexpectedly referring to the same memory, so a write through 1 affects the other.
Memory Safety Violations
Spatial Violation
A memory access outside an object’s bounds.
- Buffer overflow
- Out-of-bounds read, e.g. Heartbleed
- Null pointer dereference
Temporal Violation
A memory access referring to an invalid object.
Examples:
- Buffer overflows
- Use after free
- Double free
- Use of uninitialized memory
Language Guarantees
Memory Safe Languages
A memory safe language enforces memory safety at compile time or run time.
Examples: Rust, Java, Python.
C and C++ are not memory safe and still widely used. Because:
- supported nearly everywhere
- low-level code such as operating systems cannot easily be implemented in memory safe languages
- memory safe languages are less mature, and their runtimes are themselves written in unsafe languages such as C or C++.
Adopting a memory safe language costs runtime overhead and loses some direct, low-level memory access such as hardware-specific instructions needed by device drivers. C and related languages allow direct memory access without bounds checks.
Overflow Risks
Overflow Categories
Overflows are the most common form of memory safety violation. They are mostly caused by missing bounds checks, can be abused to read and write memory, and can occur on buffers and integers.
Recognizing Violations in C
buf[i + 1] is an out-of-bounds read when buf has 5 elements and a loop permits i = 4. Valid indices are 0 through 4. The read attempts buf[5].
Using an uninitialized local variable, such as int buf1[5] before assigning buf1[i], is an uninitialized read. Its value is indeterminate.
After free(ptr), every alias of the allocation becomes invalid. Assigning alias = ptr before the free does not preserve the allocation. Writing through ptr or alias afterwards is use after free. Calling free() a second time is a double free.
Pointer arithmetic must remain within the same allocated object, or 1 position past its end without dereferencing that position. If buf holds 3 integers, int *p = buf + 1 points at buf[1]. p[2] dereferences buf[3], which is out of bounds.
Recursion needs a reachable base case. A function that calls itself with n - 1 while starting at a negative n moves farther from zero. It recurses until stack exhaustion.
scanf("%s", password) and gets(buffer) have no destination-size bound. Input longer than the array overwrites adjacent stack data. Use fgets(buffer, sizeof buffer, stdin) or a width such as scanf("%7s", password) for an 8-byte array.