Added support for ASLR and PIE for target user mode binaries
PATCH 1/1 Added support for ASLR and PIE for target user mode binaries
Hello,
I recently had to extend QEMU user mode to support PIE enabled binaries compiled for non-native architectures. It was also important to emulate ASLR for the target binary if ASLR was enabled on the host machine. I would like to introduce this as a feature into QEMU mainstream as it seems like a feature others could use as well.
With the patch implementation, ASLR will be enabled if /proc/sys/kernel/randomize_va_space is non-zero on the host machine. An alternative implementation could be passing an argument during runtime to enable or disable ASLR. PIE is automatically enabled as well if the binary has been compiled with this flag.
Best regards,
Sean Harrington
--- linux-user/Makefile.objs | 3 +- linux-user/elfload.c | 3 +- linux-user/main.c | 7 +++ linux-user/randomize_va.c | 95 +++++++++++++++++++++++++++++++++++++++ linux-user/randomize_va.h | 23 ++++++++++ 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 linux-user/randomize_va.c create mode 100644 linux-user/randomize_va.h
linux-user/Makefile.objs +2 -1
@@ -1,7 +1,8 @@ obj-y = main.o syscall.o strace.o mmap.o signal.o \ elfload.o linuxload.o uaccess.o uname.o \ safe-syscall.o $(TARGET_ABI_DIR)/signal.o \ - $(TARGET_ABI_DIR)/cpu_loop.o exit.o fd-trans.o + $(TARGET_ABI_DIR)/cpu_loop.o exit.o fd-trans.o \ + randomize_va.o obj-$(TARGET_HAS_BFLT) += flatload.o obj-$(TARGET_I386) += vm86.o
linux-user/elfload.c +2 -1
@@ -7,6 +7,7 @@#include "qemu.h" #include "disas/disas.h" #include "qemu/path.h" +#include "randomize_va.h" #ifdef _ARCH_PPC64#undef ARCH_DLINFO
@@ -2258,7 +2259,7 @@ static void load_elf_image(const char *image_name, int image_fd,image is pre-linked, LOADDR will be non-zero. Since we do not supply MAP_FIXED here we'll use that address if and only if it remains available. */ - load_addr =target_mmap (loaddr, hiaddr - loaddr,PROT_NONE , + load_addr =get_pie_addr (loaddr, hiaddr - loaddr,PROT_NONE ,MAP_PRIVATE |MAP_ANON |MAP_NORESERVE ,-1 ,0 );if (load_addr ==-1 ) {
linux-user/main.c +7 -0
@@ -37,6 +37,7 @@#include "trace/control.h" #include "target_elf.h" #include "cpu_loop-common.h" +#include "randomize_va.h" char *exec_path;
@@ -694,6 +695,12 @@ int main(int argc, char **argv, char **envp) target_environ =envlist_to_environ (envlist,NULL );envlist_free (envlist); +/* +* If host has randomized_va_space enabled, emulate the same for the +* target. +*/ + reserved_va =handle_randomized_va (reserved_va,MAX_RESERVED_VA ); +/* * Now that page sizes are configured in tcg_exec_init() we can do * proper page alignment for guest_base.
linux-user/randomize_va.c new file +95 -0
95 lines not on this page · raw message
linux-user/randomize_va.h new file +23 -0
@@ -0,0 +1,23 @@ +/* +* QEMU user ASLR + PIE emulation +* +* Copyright (c) 2018 Sean Harrington ([email protected]) +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +unsigned long handle_randomized_va (unsigned long va,unsigned long max_va_space); + +abi_ulong get_pie_addr (abi_ulong addr,abi_ulong size,int prot_opts, +int map_opts,int fd,off_t offset);
--
2.17.2 (Apple Git-113)