2b - Bufferoverflows
2b - Bufferoverflows
Ramin Sadre
Buffer overflow
▪ By providing a too long string 𝑠, you can overwrite the the saved
frame pointer, the return address, and even the previous frame!
Code injection example
▪ Sometimes you will see an attacker trying to call a function with a
string argument containing the bytes:
31 c0 50 68 2f 73 68 00 68 2f
62 69 6e 89 e3 50 89 e2 53 89
e1 b0 0b cd 80 fc ce ff ff
▪ This is the binary for the machine instructions (x86 CPUs) for the
function call on Linux:
execve(“/bin/sh”)
followed by 4 bytes 0xFFFFCEFC
▪ In our example, this will write the code into the buffer (and the
frame pointer) and then overwrite the return address with
0xFFFFCEFC which is the address of the array buffer
▪ When function 𝑓 returns, the CPU will jump to the return address
and execute the injected code!
Predicting addresses
▪ But... How does the attacker know that the buffer will be located
at address 0xFFFFCEF?
▪ Isn't the stack located at a different address depending on how
full your memory is when your program was started, how much
memory your computer has, etc.?
• No. Thanks to virtual memory, every process starts in a clean
virtual address space with predictable addresses for the code,
the stack, etc. set by the OS
▪ Of course, the address is program dependent. In our example,
0xFFFFCEF is only the correct address if the function f() is called
from main().
▪ See next slide (adapted from
http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-
memory/ )
Virtual address space of a program running
on 32-bit Linux
Using buffer overflows
▪ Typically, the attacker will use the buffer overflow to start a shell
where they can
• execute programs with the same rights as the attacked
process.
If the attacked process (e.g. webserver) was running with root
rights, the attacker can do everything
• start other attacks (buffer overflows,...) against the machine to
get root rights
Protection
Avoiding buffer overflows
▪ In C, you should never use strcpy (and similar functions like gets
and sprintf)
Guard page
buffer[7]
...
buffer[0]
Guard page
Tools to detect vulnerabilities
▪ Compilers can add code to push a canary value onto the stack
▪ When the function returns, the code checks whether the canary
has been overwritten
(previous frame)
s
return address
saved frame pointer
Canary value
buffer[7]
...
buffer[0]
▪ Compiler can insert random amount of data into the stack frame
to make it harder for the attacker to guess the frame layout
(previous frame)
s
return address
saved frame pointer
random amount of
canary bytes
buffer[7]
...
buffer[0]
Mitigation by Canaries (2)
▪ ASLR might not work well on CPUs with a small 16-bit or 32-bit
address space
• Random gap cannot be very large
• And addresses typically start on 2/4/8-byte or even 4096-byte
boundaries (pages), further reducing the number of possible
values for the gap size
▪ But: You never know what vulnerabilities the OS and the libraries
contain
• Isolate your server from the rest of the system, to minimize
the damage
• Don't run your web server as root: principle of least privileges