Brendan Ang

Search

Search IconIcon to open search

Memory Safety

Last updated May 11, 2023 Edit Source

Safe memory access is when each memory location that is used must have been

# Memory Corruption

Exploited to get access to protected data, or overwrite important data that governs control flow; may hijack process:

  1. Access to an unallocated memory region, or a region outside given buffer.
  2. May read uninitialized memory, or write to memory used by other buffer.

# Buffer Overflow

A variant of buffer overflow is Stack Smashing, or stack buffer overflow. Following the above C code we can overwrite the return address on the frame with address to malicious program: 200x300 200x300

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>  
#include <string.h>  
void overflow(const char* input) {  
	char buf[256];  
	printf("Virtual address of 'buf' = Ox%p\n", buf);  
	strcpy(buf,input);  
}  
void attack() {  
	printf("'attack’ is called without explicitly invocation.\n");  
	printf("Buffer Overflow attack succeeded!\n");  
}  
int main(int argc, char* argv[]) {  
	printf("Virtual address of 'overflow' = Ox%p\n",overflow);  
	printf("Virtual address of 'attack' = Ox%p\n", attack);  
	char input[] = "..."; /* useless content as offset*/  
	char add[] = "\xf9\x51\x55\x55\x55\x55"; /* attack address*/  
	strcat(input, add);  
	overflow(input);  
	return 0;  
}

Needs the absolute address of malicious code which can be infeasible. By inserting NOP instructions before the malicious code, it can improve the guess chance by allowing the program to advance until the address of the malicious program.

# Compiler Support

# Stack Guard

Key insight: difficult to modify the return address without overwriting stack memory in front of the return address. Generate a canary next to the return address and check it whenever a function returns: Random Canary

# Point Guard

Protect the pointers from being overwritten with more performance overhead:

# Stack Shield

A GNU C compiler extension that protects the return address by separating the return address from data.

# OS Support

# Address Space Layout Randomization

OS randomly arranges address space of key data areas for each program such as the base, stack, heap and library pointers, which makes guessing malicious program address harder.

# Non-executable Memory

Executable memory regions allow attackers to inject a binary payload For example, a simple way to obtaining shell access: Mark all writable memory locations as non-executable. Some examples are ExecShield in Linux. Code reuse attacks make use of already loaded functions, for example overwriting the return address to libc execve with /bin/sh as an argument.

# Shadow Stack

Keep a copy of the stack in memory. Compare the actual and shadow stack to see if the return address has been changed.

# Hardware Support

ARM Memory Tagging Extension (MTE):

Hardware can support an attribute in the Page Table Entry to control if the page is executable.

# Memory Leaks

A serious issue for long running programs.

Allocated memory is not freed but also never used again:

  1. Lost for good: memory is no longer reachable, perhaps due to losing the only pointer to that memory. May be garbage collected.
  2. Potentially lost: memory is still reachable (the program still holds a pointer), and hence may not be garbage collected.

# Memory Checking Tools

Popular tool: valgrind.

# Input generation

These tools use different inputs to attempt to find one which can cause a memory error.