This project demonstrates a ret2libc attack and implements ASLR to mitigate this vulnerability
It is unclear if this will run on Athena, but this is confirmed working on a local installation of the RISC-V toolchain on Ubuntu 18.04.
$ make qemu
[omitted output of compilation]
xv6 kernel is booting
virtio disk init 0
buf: 0x-7fff8000
hart 2 starting
hart 1 starting
exec /init:
randomize_va_space = 1
buf: 0x-7fff8000
load offset: 0x1b0
argc: 1
init: starting sh
buf: 0x-7fff8000
exec sh:
randomize_va_space = 1
buf: 0x-7fff8000
load offset: 0x2240
argc: 1
$When exec is called on an executable, the following is printed:
exec {name}: a helpful debug message explaining the executable that is currently being loadedrandomize_va_space = 1: if this flag is 1, then ASLR is enabled and memory addresses are randomized. If this is 0, then programs are loaded at a fixed offset of0x0argc: 1: this is the last statement ofexecand tells the user how many arguments are being passed to the executable
You can change the flag by either changing the file on the host computer at ./randomize_va_space in the project folder.
$ echo 0 > randomize_va_space
$ make clean && make qemuYou can also change the flag within xv6:
$ echo 0 > randomize_va_spaceThe generation of the attack file is performed using the genExploit.py file in the project root directory. In it, you will need to change the following variables:
a0_gadget_addr: the address of the gadget that contains assembly instructions for a load from the memory at the stack pointer intoa0a0: the value that the attacker wantsa0to have after calling the gadgetsystem_addr: the address of the first instruction in thesystemuser function.system(name)is a wrapper aroundexecthat will execute the filenamewith zero arguments.
You can generate the exploit file:
$ python2 genExploit.pywhich will save the exploit into the file exploit. This is loaded into xv6 using the following:
$ make clean && make qemuRunning the exploit requires just one instruction:
$ overflow4 exploitNotice that user/overflow4.c does not explicitly call nsh (which is denoted by an interactive shell that begins with @ instead of $), but running the command above opens nsh. This indicates successful execution of the ret2libc attack.
Authors:
- Johnny Bui
- Neeraj Prasad