- Memory safety
-
Memory safety is a concern in software development that aims to avoid software bugs that cause security vulnerabilities dealing with random-access memory (RAM) access, such as buffer overflows and dangling pointers.
Computer languages such as C and C++ that support arbitrary pointer arithmetic, casting, and deallocation are typically not memory safe. One way to find errors in such programs is to use special heap allocators that provide dead zones around heap allocated storage, and check that accesses don't reach into such dead zones. DieHard[1] does this by allocating objects in their own virtual memory page. Other tools SoftBound[2] and CheckPointer[3] instrument the source code to collect and track legitimate values for pointers ("metadata") and check each pointer access against the metadata for validity.
The Cyclone language uses a hybrid approach, including "fat pointers" (pointers that carry their metadata directly)[4] and regions[5] to give programmers some low-level control while still ensuring memory safety.
Most high-level programming languages avoid the problem by disallowing pointer arithmetic and casting entirely, and by enforcing tracing garbage collection as the sole memory management scheme.[citation needed]
A language could support even more uses of pointer arithmetic, casting, and deallocation without sacrificing memory safety by using automated theorem proving as a form of static code analysis. ESC/Java and D demonstrate two ways that programmers can declare their invariants in ways that can be understood by a theorem prover.[citation needed]
Contents
Security vulnerabilities
Main article: Vulnerability (computing)- Dangling pointer - A pointer storing the address of an object that has been deleted.
- Wild 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 behaviour as dangling pointers, though they are less likely to stay undetected.
- Buffer overflow - Out-of bound writes can corrupt the content of object already present on the heap.
- Stack overflow - similar to the Buffer overflow.
- Double frees - Repeated call to free though the object has been already freed can cause freelist-based allocators to fail.
- Invalid Free - Passing an invalid address to free can corrupt the heap. Or sometimes will lead to an undefined behavior.
Dangling pointer
Dangling pointer points to a memory location which is being removed.
e.g. int *a = new int; int *b = a; delete b;
here 'a' and 'b' is now a Dangling pointer
Buffer overflow
Buffers is a temporary data storage area. Buffer overflow is the most common way for an attacker outside the system to gain unauthorized access to the target system. A buffer overflow occurs when a program tries to store more data in a buffer than it was intended to hold. Since buffers are created to contain a finite amount of data, the extra information can overflow into adjacent buffers, corrupting or overwriting the valid data held in them.It allows attacker to interfere into the existing process code. Attacker uses buffer or stack overflow to do following,
- Overflow the input field, command line space or input buffer.
- Overwrite the current return address on the stack with the address of the attacking code.
- write a simple code that attacker wishes to execute.
E.g.consider the following program
#include <stdio.h> #define ARRAY_SIZE 128 int main(int argc, char *argv[]) { char arr[ARRAY_SIZE]; if(argc < 2) return -1; else { strcpy(arr, argv[1]); return 0; } }
As long as the size of this array is less than ARRAY_SIZE program works properly.If the size of the command line argument is greater than that ARRAY_SIZE then it won't work properly. strcpy function will work until it encounters NULL terminator(\0) or until the program crashes.This program suffers from the buffer overflow problem.
- Solution for this problem is that the feature that will not allow execution of code in stack section of memory.[6]
Some programming languages are immune to buffer overflow.Perl automatically resizes arrays, and Ada95 detects and prevents buffer overflow.
References
Memory management Manual memory management Virtual memory Hardware Garbage collection ImplementationsBoehm garbage collector • Garbage-first collectorMemory segmentation Memory safety Issues Other Automatic variable • International Symposium on Memory Management • Region-based memory managementCategories:- Programming bugs
- Computer security exploits
- Software stubs
Wikimedia Foundation. 2010.