x86 instruction emulator fuzzing
v2 cover x86 instruction emulator fuzzing
Dear all,
This series aims to provide an entrypoint for, and fuzz KVM's x86 instruction emulator from userspace. It mirrors Xen's application of the AFL fuzzer to it's instruction emulator in the hopes of discovering vulnerabilities. Since this entrypoint also allows arbitrary execution of the emulators code from userspace, it may also be useful for testing.
The current 4 patches build the emulator and 2 harnesses: simple-harness is an example of unit testing; afl-harness is a frontend for the AFL fuzzer.
Patches
=======
- 01: Builds and links afl-harness with the required kernel objects.
- 02: Introduces the minimal set of emulator operations and supporting code to emulate simple instructions.
- 03: Demonstrates simple-harness as a unit test.
- 04: Adds scripts for install, running, and crash triage.
Any comments/suggestions are greatly appreciated.
Best,
Sam Caccavale
Sam Caccavale (4):
Build target for emulate.o as a userspace binary Emulate simple x86 instructions in userspace Demonstrating unit testing via simple-harness Added scripts for filtering, building, deploying
tools/Makefile | 9 + tools/fuzz/x86ie/.gitignore | 2 + tools/fuzz/x86ie/Makefile | 54 +++ tools/fuzz/x86ie/README.md | 12 + tools/fuzz/x86ie/afl-harness.c | 151 +++++++ tools/fuzz/x86ie/common.h | 87 ++++ tools/fuzz/x86ie/emulator_ops.c | 398 ++++++++++++++++++ tools/fuzz/x86ie/emulator_ops.h | 120 ++++++ tools/fuzz/x86ie/scripts/afl-many | 28 ++ tools/fuzz/x86ie/scripts/bin.sh | 49 +++ tools/fuzz/x86ie/scripts/build.sh | 32 ++ tools/fuzz/x86ie/scripts/coalesce.sh | 6 + tools/fuzz/x86ie/scripts/deploy.sh | 9 + tools/fuzz/x86ie/scripts/deploy_remote.sh | 9 + tools/fuzz/x86ie/scripts/gen_output.sh | 11 + tools/fuzz/x86ie/scripts/install_afl.sh | 14 + .../fuzz/x86ie/scripts/install_deps_ubuntu.sh | 5 + tools/fuzz/x86ie/scripts/rebuild.sh | 6 + tools/fuzz/x86ie/scripts/run.sh | 10 + tools/fuzz/x86ie/scripts/summarize.sh | 9 + tools/fuzz/x86ie/simple-harness.c | 42 ++ tools/fuzz/x86ie/stubs.c | 56 +++ tools/fuzz/x86ie/stubs.h | 52 +++ 23 files changed, 1171 insertions(+) create mode 100644 tools/fuzz/x86ie/.gitignore create mode 100644 tools/fuzz/x86ie/Makefile create mode 100644 tools/fuzz/x86ie/README.md create mode 100644 tools/fuzz/x86ie/afl-harness.c create mode 100644 tools/fuzz/x86ie/common.h create mode 100644 tools/fuzz/x86ie/emulator_ops.c create mode 100644 tools/fuzz/x86ie/emulator_ops.h create mode 100755 tools/fuzz/x86ie/scripts/afl-many create mode 100755 tools/fuzz/x86ie/scripts/bin.sh create mode 100755 tools/fuzz/x86ie/scripts/build.sh create mode 100755 tools/fuzz/x86ie/scripts/coalesce.sh create mode 100644 tools/fuzz/x86ie/scripts/deploy.sh create mode 100755 tools/fuzz/x86ie/scripts/deploy_remote.sh create mode 100755 tools/fuzz/x86ie/scripts/gen_output.sh create mode 100755 tools/fuzz/x86ie/scripts/install_afl.sh create mode 100755 tools/fuzz/x86ie/scripts/install_deps_ubuntu.sh create mode 100755 tools/fuzz/x86ie/scripts/rebuild.sh create mode 100755 tools/fuzz/x86ie/scripts/run.sh create mode 100755 tools/fuzz/x86ie/scripts/summarize.sh create mode 100644 tools/fuzz/x86ie/simple-harness.c create mode 100644 tools/fuzz/x86ie/stubs.c create mode 100644 tools/fuzz/x86ie/stubs.h
--
2.17.1
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Ralf Herbrich Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
PATCH v2 3/4 Demonstrating unit testing via simple-harness
Simple-harness.c uses inline asm support to generate asm and then has the emulator emulate this code. This may be useful as a form of testing for the emulator.
CR: https://code.amazon.com/reviews/CR-8591638
--- tools/fuzz/x86ie/Makefile | 7 ++++-- tools/fuzz/x86ie/simple-harness.c | 42 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tools/fuzz/x86ie/simple-harness.c
tools/fuzz/x86ie/Makefile +5 -2
@@ -44,8 +44,11 @@ LOCAL_OBJS := emulator_ops.o stubs.o afl-harness: afl-harness.o $(LOCAL_OBJS) $(KERNEL_OBJS) @$(CC) -v $(KBUILD_CFLAGS) $(LOCAL_OBJS) $(KERNEL_OBJS) $< $(INCLUDES) -Istubs.h -o $@ -no-pie -all: afl-harness +simple-harness: simple-harness.o $(LOCAL_OBJS) $(KERNEL_OBJS) + @$(CC) -v $(KBUILD_CFLAGS) $(LOCAL_OBJS) $(KERNEL_OBJS) $< $(INCLUDES) -Istubs.h -o $@ -no-pie + +all: afl-harness simple-harness .PHONY: clean clean: - $(RM) -r *.o afl-harness + $(RM) -r *.o afl-harness simple-harness
tools/fuzz/x86ie/simple-harness.c new file +42 -0
42 lines not on this page · raw message
--
2.17.1
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Ralf Herbrich Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
PATCH v2 2/4 Emulate simple x86 instructions in userspace
In reply to Sam CaccavaleAdded the minimal subset of code to run afl-harness with a binary file as input. These bytes are used to populate the vcpu structure and then as an instruction stream for the emulator. It does not attempt to handle exceptions an only supports very simple ops.
CR: https://code.amazon.com/reviews/CR-8552453
--- tools/fuzz/x86ie/afl-harness.c | 8 +- tools/fuzz/x86ie/emulator_ops.c | 342 +++++++++++++++++++++++++++++- tools/fuzz/x86ie/emulator_ops.h | 7 +- tools/fuzz/x86ie/scripts/afl-many | 28 +++ 4 files changed, 379 insertions(+), 6 deletions(-) create mode 100755 tools/fuzz/x86ie/scripts/afl-many
tools/fuzz/x86ie/afl-harness.c +5 -3
@@ -50,7 +50,7 @@ int main(int argc, char **argv)switch (c) {case OPT_INPUT_SIZE : -printf ("Min: %u\n" ,MIN_INPUT_SIZE ); +printf ("Min: %lu\n" ,MIN_INPUT_SIZE );printf ("Max: %u\n" ,MAX_INPUT_SIZE );exit (0 );break ;
@@ -77,6 +77,10 @@ int main(int argc, char **argv) state =create_emulator (); state->data =malloc (INSTRUCTION_BYTES ); +if (!state->data) { +printf ("Malloc failed.\n" ); +return -1 ; + }#ifdef __AFL_HAVE_MANUAL_CONTROL__AFL_INIT ();
@@ -109,8 +113,6 @@ int main(int argc, char **argv) fseek(fp, 0, SEEK_SET); }#endif -reset_emulator (state); - size =fread (state,1 ,MIN_INPUT_SIZE , fp);if (size !=MIN_INPUT_SIZE ) {printf ("Input does not populate state\n" );
tools/fuzz/x86ie/emulator_ops.c +341 -1
366 lines not on this page · raw message
tools/fuzz/x86ie/emulator_ops.h +5 -2
@@ -59,7 +59,7 @@ struct state {* Amount of bytes consumed for purposes other than instructions. * E.G. whether a memory access should fault. */ -size_t other_bytes_consumed; +size_t bytes_consumed;/* Emulation context */ struct x86_emulate_ctxt ctxt;
@@ -75,7 +75,10 @@ struct state {#define get_state (h) container_of(h, struct state, ctxt) -void buffer_stderr (void ) __attribute__((constructor)); +static inline int number_of_gprs (struct x86_emulate_ctxt *c) +{ +return (c->mode ==X86EMUL_MODE_PROT64 ?16 :8 ); +}/* * Allocates space for, and creates a `struct state`. The user should set
tools/fuzz/x86ie/scripts/afl-many new file +28 -0
@@ -0,0 +1,28 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ +# This is for running AFL over NPROC or `nproc` cores with normal AFL options. + +export AFL_NO_AFFINITY=1 + +while [ -z "$sync_dir" ]; do + while getopts ":o:" opt; do + case "${opt}" in + o) + sync_dir="${OPTARG}" + ;; + *) + ;; + esac + done + ((OPTIND++)) + [ $OPTIND -gt $# ] && break +done + +for i in $(seq 1 $(( ${NPROC:-$(nproc)} - 1)) ); do + taskset -c "$i" ./afl-fuzz -S "slave$i" $@ >/dev/null 2>&1 & +done +taskset -c 0 ./afl-fuzz -M master $@ >/dev/null 2>&1 & + +sleep 5 +watch -n1 "echo \"Executing './afl-fuzz $@' on ${NPROC:-$(nproc)} cores.\" && ./afl-whatsup -s ${sync_dir}" +pkill afl-fuzz
--
2.17.1
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Ralf Herbrich Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
PATCH v2 1/4 Build target for emulate.o as a userspace binary
In reply to Sam CaccavaleThis commit contains the minimal set of functionality to build afl-harness around arch/x86/emulate.c which allows exercising code in that source file, like x86_emulate_insn. Resolving the dependencies was done via GCC's -H flag by get_headers.py.
CR: https://code.amazon.com/reviews/CR-8325546
--- tools/Makefile | 9 ++ tools/fuzz/x86ie/.gitignore | 2 + tools/fuzz/x86ie/Makefile | 51 +++++++++++ tools/fuzz/x86ie/README.md | 12 +++ tools/fuzz/x86ie/afl-harness.c | 149 ++++++++++++++++++++++++++++++++ tools/fuzz/x86ie/common.h | 87 +++++++++++++++++++ tools/fuzz/x86ie/emulator_ops.c | 58 +++++++++++++ tools/fuzz/x86ie/emulator_ops.h | 117 +++++++++++++++++++++++++ tools/fuzz/x86ie/stubs.c | 56 ++++++++++++ tools/fuzz/x86ie/stubs.h | 52 +++++++++++ 10 files changed, 593 insertions(+) create mode 100644 tools/fuzz/x86ie/.gitignore create mode 100644 tools/fuzz/x86ie/Makefile create mode 100644 tools/fuzz/x86ie/README.md create mode 100644 tools/fuzz/x86ie/afl-harness.c create mode 100644 tools/fuzz/x86ie/common.h create mode 100644 tools/fuzz/x86ie/emulator_ops.c create mode 100644 tools/fuzz/x86ie/emulator_ops.h create mode 100644 tools/fuzz/x86ie/stubs.c create mode 100644 tools/fuzz/x86ie/stubs.h
tools/Makefile +9 -0
@@ -94,6 +94,12 @@ freefall: FORCE kvm_stat: FORCE $(call descend,kvm/$@) +fuzz: FORCE + $(call descend,fuzz/x86_instruction_emulation) + +fuzz_deps: FORCE + $(call descend,fuzz/x86_instruction_emulation,fuzz_deps) + all: acpi cgroup cpupower gpio hv firewire liblockdep \ perf selftests spi turbostat usb \ virtio vm bpf x86_energy_perf_policy \
@@ -171,6 +177,9 @@ tmon_clean: freefall_clean: $(call descend,laptop/freefall,clean) +fuzz_clean: + $(call descend,fuzz/x86_instruction_emulation,clean) + build_clean: $(call descend,build,clean)
tools/fuzz/x86ie/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +*.o +*-harness
tools/fuzz/x86ie/Makefile new file +51 -0
51 lines not on this page · raw message
tools/fuzz/x86ie/README.md new file +12 -0
@@ -0,0 +1,12 @@ +# Building + +From the root of linux, run: +1. `./tools/fuzz/x86_instruction_emulation/scripts/make_deps` + - Optionally, set the envvar CC with your desired compiler. +2. `make tools/fuzz` + +## TODO + +I'd like to add the object files built in `make_deps` as real +dependencies of `make tools/fuzz` but it causes a cycle in Make to +add them to `tools/fuzz/x86_instruction_emulation/Makefile`.
tools/fuzz/x86ie/afl-harness.c new file +149 -0
149 lines not on this page · raw message
tools/fuzz/x86ie/common.h new file +87 -0
87 lines not on this page · raw message
tools/fuzz/x86ie/emulator_ops.c new file +58 -0
58 lines not on this page · raw message
tools/fuzz/x86ie/emulator_ops.h new file +117 -0
117 lines not on this page · raw message
tools/fuzz/x86ie/stubs.c new file +56 -0
56 lines not on this page · raw message
tools/fuzz/x86ie/stubs.h new file +52 -0
52 lines not on this page · raw message
--
2.17.1
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Ralf Herbrich Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
PATCH v2 4/4 Added scripts for filtering, building, deploying
In reply to Sam Caccavalebin.sh produces output which diagnoses whether the crash was expected. coalesce.sh, gen_output.sh, and summarize.sh are useful for parsing the large crash directories that afl produces. deploy_remote.sh does all of the setup to launch a fuzz run via install_deps_ubuntu.sh, install_afl.sh, build.sh, and run.sh. rebuild.sh cleans the directories and executes build.sh
--- tools/fuzz/x86ie/scripts/afl-many | 6 +-- tools/fuzz/x86ie/scripts/bin.sh | 49 +++++++++++++++++++ tools/fuzz/x86ie/scripts/build.sh | 32 ++++++++++++ tools/fuzz/x86ie/scripts/coalesce.sh | 6 +++ tools/fuzz/x86ie/scripts/deploy.sh | 9 ++++ tools/fuzz/x86ie/scripts/deploy_remote.sh | 9 ++++ tools/fuzz/x86ie/scripts/gen_output.sh | 11 +++++ tools/fuzz/x86ie/scripts/install_afl.sh | 14 ++++++ .../fuzz/x86ie/scripts/install_deps_ubuntu.sh | 5 ++ tools/fuzz/x86ie/scripts/rebuild.sh | 6 +++ tools/fuzz/x86ie/scripts/run.sh | 10 ++++ tools/fuzz/x86ie/scripts/summarize.sh | 9 ++++ 12 files changed, 163 insertions(+), 3 deletions(-) create mode 100755 tools/fuzz/x86ie/scripts/bin.sh create mode 100755 tools/fuzz/x86ie/scripts/build.sh create mode 100755 tools/fuzz/x86ie/scripts/coalesce.sh create mode 100644 tools/fuzz/x86ie/scripts/deploy.sh create mode 100755 tools/fuzz/x86ie/scripts/deploy_remote.sh create mode 100755 tools/fuzz/x86ie/scripts/gen_output.sh create mode 100755 tools/fuzz/x86ie/scripts/install_afl.sh create mode 100755 tools/fuzz/x86ie/scripts/install_deps_ubuntu.sh create mode 100755 tools/fuzz/x86ie/scripts/rebuild.sh create mode 100755 tools/fuzz/x86ie/scripts/run.sh create mode 100755 tools/fuzz/x86ie/scripts/summarize.sh
tools/fuzz/x86ie/scripts/afl-many +3 -3
@@ -19,10 +19,10 @@ while [ -z "$sync_dir" ]; do done for i in $(seq 1 $(( ${NPROC:-$(nproc)} - 1)) ); do - taskset -c "$i" ./afl-fuzz -S "slave$i" $@ >/dev/null 2>&1 & + taskset -c "$i" $AFLPATH/afl-fuzz -S "slave$i" $@ >/dev/null 2>&1 & done -taskset -c 0 ./afl-fuzz -M master $@ >/dev/null 2>&1 & +taskset -c 0 $AFLPATH/afl-fuzz -M master $@ >/dev/null 2>&1 & sleep 5 -watch -n1 "echo \"Executing './afl-fuzz $@' on ${NPROC:-$(nproc)} cores.\" && ./afl-whatsup -s ${sync_dir}" +watch -n1 "echo \"Executing 'AFLPATH/afl-fuzz $@' on ${NPROC:-$(nproc)} cores.\" && $AFLPATH/afl-whatsup -s ${sync_dir}" pkill afl-fuzz
tools/fuzz/x86ie/scripts/bin.sh new file +49 -0
49 lines not on this page · raw message
tools/fuzz/x86ie/scripts/build.sh new file +32 -0
@@ -0,0 +1,32 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +kernel_objects="arch/x86/kvm/emulate.o arch/x86/lib/retpoline.o lib/find_bit.o" + +disable() { sed -i -r "/\b$1\b/c\# $1" .config; } +enable() { sed -i -r "/\b$1\b/c\\$1=y" .config; } + +make ${CC:+ "CC=$CC"} ${DEBUG:+ "DEBUG=1"} defconfig + +enable "CONFIG_DEBUG_INFO" +enable "CONFIG_STACKPROTECTOR" + +yes ' ' | make ${CC:+ "CC=$CC"} ${DEBUG:+ "DEBUG=1"} $kernel_objects + +omit_arg () { args=$(echo "$args" | sed "s/ $1//g"); } +add_arg () { args+=" $1"; } + +rebuild () { + args="$(head -1 $(dirname $1)/.$(basename $1).cmd | sed -e 's/.*:= //g')" + omit_arg "-mcmodel=kernel" + omit_arg "-mpreferred-stack-boundary=3" + add_arg "-fsanitize=address" + echo -e "Rebuilding $1 with \n$args" + eval "$args" +} + +for object in $kernel_objects; do + rebuild $object +done + +make ${CC:+ "CC=$CC"} ${DEBUG:+ "DEBUG=1"} tools/fuzz
tools/fuzz/x86ie/scripts/coalesce.sh new file +6 -0
@@ -0,0 +1,6 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +mkdir -p all +rm -rf all/* +find . -type f -wholename '*crashes/id*' | parallel cp {} ./all/$(basename $(dirname {//})):{/}
tools/fuzz/x86ie/scripts/deploy.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +REMOTE=$1 +DSTDIR=/dev/shm + +rsync -av $(pwd) $REMOTE:$DSTDIR + +ssh $REMOTE "cd $DSTDIR/$(basename $(pwd)); bash -s tools/fuzz/x86_instruction_emulation/scripts/deploy_remote.sh"
tools/fuzz/x86ie/scripts/deploy_remote.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +SCRIPTDIR=$(pwd)/tools/fuzz/x86_instruction_emulation/scripts + +$SCRIPTDIR/install_deps_ubuntu.sh +source $SCRIPTDIR/install_afl.sh +CC=$AFLPATH/afl-gcc $SCRIPTDIR/build.sh +FUZZDIR="${FUZZDIR:-$(pwd)/fuzz}" $SCRIPTDIR/run.sh
tools/fuzz/x86ie/scripts/gen_output.sh new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +if [ "$#" -lt 3 ]; then + echo "Usage: '$0 path/to/afl-harness path/to/afl_crash_dir path/to/linux/src/root'" + exit +fi + +mkdir -p output +rm -rf output/* +find $2 -type f | parallel ./bin.sh $1 {} $3 '>' ./output/{/}.out
tools/fuzz/x86ie/scripts/install_afl.sh new file +14 -0
@@ -0,0 +1,14 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz +mkdir -p afl +tar xzf afl-latest.tgz -C afl --strip-components 1 + +pushd afl +set AFL_USE_ASAN +make clean all +export AFLPATH="$(pwd)" +popd + +sudo bash -c "echo core >/proc/sys/kernel/core_pattern"
tools/fuzz/x86ie/scripts/install_deps_ubuntu.sh new file +5 -0
@@ -0,0 +1,5 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +sudo apt update +sudo apt install -y make gcc wget screen build-essential libssh-dev flex bison libelf-dev bc
tools/fuzz/x86ie/scripts/rebuild.sh new file +6 -0
@@ -0,0 +1,6 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +make clean +make tools/fuzz_clean +FUZZDIR="./fuzz" ./tools/fuzz/x86_instruction_emulation/scripts/build.sh
tools/fuzz/x86ie/scripts/run.sh new file +10 -0
@@ -0,0 +1,10 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +FUZZDIR="${FUZZDIR:-$(pwd)/fuzz}" + +mkdir -p $FUZZDIR/in +cp tools/fuzz/x86_instruction_emulation/rand_sample.bin $FUZZDIR/in +mkdir -p $FUZZDIR/out + +screen bash -c "ulimit -Sv $[21999999999 << 10]; ./tools/fuzz/x86_instruction_emulation/scripts/afl-many -m 22000000000 -i $FUZZDIR/in -o $FUZZDIR/out tools/fuzz/x86_instruction_emulation/afl-harness @@"
tools/fuzz/x86ie/scripts/summarize.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +if [ "$#" -lt 1 ]; then + echo "Usage: '$0 path/to/output/dir'" + exit +fi + +time bash -c "find $1 -type f -exec tail -n 1 {} \; | sort | uniq -c | sort -rn"
--
2.17.1
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Ralf Herbrich Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Quoted 14 lines · Sam Caccavale
Dear all,
This series aims to provide an entrypoint for, and fuzz KVM's x86 instruction emulator from userspace. It mirrors Xen's application of the AFL fuzzer to it's instruction emulator in the hopes of discovering vulnerabilities. Since this entrypoint also allows arbitrary execution of the emulators code from userspace, it may also be useful for testing.
The current 4 patches build the emulator and 2 harnesses: simple-harness is an example of unit testing; afl-harness is a frontend for the AFL fuzzer.
Patches
=======
Quoted 7 lines
- 01: Builds and links afl-harness with the required kernel objects.
- 02: Introduces the minimal set of emulator operations and supporting code to emulate simple instructions.
- 03: Demonstrates simple-harness as a unit test.
- 04: Adds scripts for install, running, and crash triage.Any comments/suggestions are greatly appreciated.
The cover letter as well as the individual patches are missing a change log from v1 to v2.
Alex
Re: [v2, 1/4] Build target for emulate.o as a userspace binary
In reply to Sam CaccavaleQuoted 6 lines · Sam Caccavale
This commit contains the minimal set of functionality to build afl-harness around
arch/x86/emulate.cwhich allows exercising code in that source file, likex86_emulate_insn. Resolving the dependencies was done via GCC's -H flag byget_headers.py.
I'm fairly sure that nobody on the LKML can access this page or even remotely cares about it ;).
Also, your patches are missing an SoB line.
Alex
Re: [v2, 2/4] Emulate simple x86 instructions in userspace
In reply to Sam CaccavaleQuoted 19 lines · Sam Caccavale
Added the minimal subset of code to run afl-harness with a binary file as input. These bytes are used to populate the vcpu structure and then as an instruction stream for the emulator. It does not attempt to handle exceptions an only supports very simple ops.
CR: https://code.amazon.com/reviews/CR-8552453
--- tools/fuzz/x86ie/afl-harness.c | 8 +- tools/fuzz/x86ie/emulator_ops.c | 342 +++++++++++++++++++++++++++++- tools/fuzz/x86ie/emulator_ops.h | 7 +- tools/fuzz/x86ie/scripts/afl-many | 28 +++ 4 files changed, 379 insertions(+), 6 deletions(-)create mode 100755
tools/fuzz/x86ie/scripts/afl-manytools/fuzz/x86ie/afl-harness.c +0
-0
tools/fuzz/x86ie/afl-harness.c int main(int argc, char **argv) · line 53
Quoted 5 lines
tools/fuzz/x86ie/afl-harness.c +1
-1@@ -50,7 +50,7 @@ int main(int argc, char **argv)switch (c) {case OPT_INPUT_SIZE : -printf ("Min: %u\n" ,MIN_INPUT_SIZE ); +printf ("Min: %lu\n" ,MIN_INPUT_SIZE );
Why this change here?
tools/fuzz/x86ie/afl-harness.c int main(int argc, char **argv) · line 83
Quoted 11 lines
tools/fuzz/x86ie/afl-harness.c +4
-0printf ("Max: %u\n" ,MAX_INPUT_SIZE );exit (0 );break ;@@ -77,6 +77,10 @@ int main(int argc, char **argv) state =create_emulator (); state->data =malloc (INSTRUCTION_BYTES ); +if (!state->data) { +printf ("Malloc failed.\n" ); +return -1 ; + }
Why here and not in 1/4?
tools/fuzz/x86ie/afl-harness.c int main(int argc, char **argv) · line 113
Quoted 9 lines
tools/fuzz/x86ie/afl-harness.c +0
-2#ifdef __AFL_HAVE_MANUAL_CONTROL__AFL_INIT ();@@ -109,8 +113,6 @@ int main(int argc, char **argv) fseek(fp, 0, SEEK_SET); }#endif -reset_emulator (state); -
Same question.
Quoted 102 lines
tools/fuzz/x86ie/afl-harness.c +0
-0size =fread (state,1 ,MIN_INPUT_SIZE , fp);if (size !=MIN_INPUT_SIZE ) {printf ("Input does not populate state\n" );tools/fuzz/x86ie/emulator_ops.c +91
-0@@ -29,17 +29,349 @@#include <asm/user_64.h> #include <asm/kvm.h> +ulong emul_read_gpr (struct x86_emulate_ctxt *ctxt,unsigned int reg) +{ +assert (reg <number_of_gprs (ctxt)); +return get_state (ctxt)->vcpu.regs[reg]; +} + +void emul_write_gpr (struct x86_emulate_ctxt *ctxt,unsigned int reg,ulong val) +{ +assert (reg <number_of_gprs (ctxt)); +get_state (ctxt)->vcpu.regs[reg] = val; +} + +/* All read ops: */ + +static int _get_bytes (void *dst,struct state *state,unsigned int bytes, +char *callee) +{ +if (state->bytes_consumed + bytes > state->data_available) { +fprintf (stderr,"Tried retrieving %d bytes\n" , bytes); +fprintf (stderr,"%s failed to retrieve bytes for %s.\n" , + __func__, callee); +return X86EMUL_UNHANDLEABLE ; + } + +memcpy (dst, &state->data[state->bytes_consumed], bytes); +return X86EMUL_CONTINUE ; +} + +/* +* The only function that any x86_emulate_ops should call to retrieve bytes. +* See comments in struct state definition for more information. +*/ +static int get_bytes_and_increment (void *dst,struct state *state, +unsigned int bytes,char *callee) +{ +int rc =_get_bytes (dst, state, bytes, callee); + +if (rc ==X86EMUL_CONTINUE ) + state->bytes_consumed += bytes; + +return rc; +} + +/* +* This is called by x86_decode_insn to fetch bytes. +*/ +int emul_fetch (struct x86_emulate_ctxt *ctxt,unsigned long addr,void *val, +unsigned int bytes,struct x86_exception *fault) +{ +if (get_bytes_and_increment (val,get_state (ctxt), bytes, +"emul_fetch" ) !=X86EMUL_CONTINUE ) { +return X86EMUL_UNHANDLEABLE ; + } + +return X86EMUL_CONTINUE ; +} + +int emul_read_emulated (struct x86_emulate_ctxt *ctxt, +unsigned long addr,void *val,unsigned int bytes, +struct x86_exception *fault) +{ +if (get_bytes_and_increment (val,get_state (ctxt), bytes, +"emul_read_emulated" ) !=X86EMUL_CONTINUE ) { +return X86EMUL_UNHANDLEABLE ; + } + +return X86EMUL_CONTINUE ; +} + +int emul_write_emulated (struct x86_emulate_ctxt *ctxt, +unsigned long addr,const void *val, +unsigned int bytes, +struct x86_exception *fault) +{ +return X86EMUL_CONTINUE ; +} + +ulong emul_get_cr (struct x86_emulate_ctxt *ctxt,int cr) +{ +return get_state (ctxt)->vcpu.cr[cr]; +} + +int emul_set_cr (struct x86_emulate_ctxt *ctxt,int cr,ulong val) +{ +get_state (ctxt)->vcpu.cr[cr] = val; +return 0 ; +} + +unsigned int emul_get_hflags (struct x86_emulate_ctxt *ctxt) +{ +return get_state (ctxt)->vcpu.rflags;
tools/fuzz/x86ie/emulator_ops.c line 132
Quoted 10 lines
tools/fuzz/x86ie/emulator_ops.c +10
-0+} + +void emul_set_hflags (struct x86_emulate_ctxt *ctxt,unsigned int hflags) +{ +get_state (ctxt)->vcpu.rflags = hflags; +} + +/* End of emulator ops */ + +#define SET (h) .h = emul_##h
This is a pretty dangerous macro name, as it has great potential for conflict with a random header. I would suggest EMUL_OP() instead.
Quoted 64 lines
tools/fuzz/x86ie/emulator_ops.c +64
-0+const struct x86_emulate_ops all_emulator_ops = { +SET (read_gpr), +SET (write_gpr), +SET (fetch), +SET (read_emulated), +SET (write_emulated), +SET (get_cr), +SET (set_cr), +SET (get_hflags), +SET (set_hflags), +}; +#undef SET + +enum { + HOOK_read_gpr, + HOOK_write_gpr, + HOOK_fetch, + HOOK_read_emulated, + HOOK_write_emulated, + HOOK_get_cr, + HOOK_set_cr, + HOOK_get_hflags, + HOOK_set_hflags +}; + +/* +* Disable an x86_emulate_op if options << HOOK_op is set. +* +* Expects options to be defined. +*/ +#define MAYBE_DISABLE_HOOK (h) \ + do { \ + if (options & (1 << HOOK_##h)) { \ + vcpu->ctxt.ops.h = NULL; \ + debug("Disabling hook " #h "\n"); \ + } \ + } while (0) + +/* +* FROM XEN: +* +* Constrain input to architecturally-possible states where +* the emulator relies on these +* +* In general we want the emulator to be as absolutely robust as +* possible; which means that we want to minimize the number of things +* it assumes about the input state. Tesing this means minimizing and +* removing as much of the input constraints as possible. +* +* So we only add constraints that (in general) have been proven to +* cause crashes in the emulator. +* +* For future reference: other constraints which might be necessary at +* some point: +* +* - EFER.LMA => !EFLAGS.NT +* - In VM86 mode, force segment... +* - ...access rights to 0xf3 +* - ...limits to 0xffff +* - ...bases to below 1Mb, 16-byte aligned +* - ...selectors to (base >> 4) +*/ +static void sanitize_input (struct state *s) +{
tools/fuzz/x86ie/emulator_ops.c line 206
Quoted 10 lines
tools/fuzz/x86ie/emulator_ops.c +10
-0+/* Some hooks can't be disabled. */ +// options &= ~((1<<HOOK_read)|(1<<HOOK_insn_fetch)); + +/* Zero 'private' entries */ +// regs->error_code = 0; +// regs->entry_vector = 0; + +// CANONICALIZE_MAYBE(rip); +// CANONICALIZE_MAYBE(rsp); +// CANONICALIZE_MAYBE(rbp);
Now that you removed the macro, please also remove all commented out lines.
Quoted 194 lines
tools/fuzz/x86ie/emulator_ops.c +166
-1+ +/* +* CR0.PG can't be set if CR0.PE isn't set. Set is more interesting, so +* set PE if PG is set. +*/ +if (s->vcpu.cr[0 ] &X86_CR0_PG ) + s->vcpu.cr[0 ] |=X86_CR0_PE ; + +/* EFLAGS.VM not available in long mode */ +if (s->ctxt.mode ==X86EMUL_MODE_PROT64 ) + s->vcpu.rflags &= ~X86_EFLAGS_VM ; + +/* EFLAGS.VM implies 16-bit mode */ +if (s->vcpu.rflags &X86_EFLAGS_VM ) { + s->vcpu.segments[x86_seg_cs].db =0 ; + s->vcpu.segments[x86_seg_ss].db =0 ; + } +} +void initialize_emulator (struct state *state) { +reset_emulator (state); + state->ctxt.ops = &all_emulator_ops; + +/* See also sanitize_input, some hooks can't be disabled. */ +// MAYBE_DISABLE_HOOK(read_gpr); + +sanitize_input (state); +} + +static const char *const x86emul_mode_string[] = { + [X86EMUL_MODE_REAL ] ="X86EMUL_MODE_REAL" , + [X86EMUL_MODE_VM86 ] ="X86EMUL_MODE_VM86" , + [X86EMUL_MODE_PROT16 ] ="X86EMUL_MODE_PROT16" , + [X86EMUL_MODE_PROT32 ] ="X86EMUL_MODE_PROT32" , + [X86EMUL_MODE_PROT64 ] ="X86EMUL_MODE_PROT64" , +}; + +static void dump_state_after (const char *desc,struct state *state) +{ +debug (" -- State after %s --\n" , desc); +debug ("mode: %s\n" , x86emul_mode_string[state->ctxt.mode]); +debug (" cr0: %lx\n" , state->vcpu.cr[0 ]); +debug (" cr3: %lx\n" , state->vcpu.cr[3 ]); +debug (" cr4: %lx\n" , state->vcpu.cr[4 ]); + +debug ("Decode _eip: %lu\n" , state->ctxt._eip); +debug ("Emulate eip: %lu\n" , state->ctxt.eip); + +debug ("\n" ); } +static void init_emulate_ctxt (struct state *state) +{ +struct x86_emulate_ctxt *ctxt = &state->ctxt; + + ctxt->eflags = ctxt->ops->get_hflags (ctxt); + ctxt->tf = (ctxt->eflags &X86_EFLAGS_TF ) !=0 ; + + ctxt->mode =X86EMUL_MODE_PROT64 ;// TODO: eventually vary this + +init_decode_cache (ctxt); +} + +int step_emulator (struct state *state) { -return 0 ; +int rc; +unsigned long prev_eip = state->ctxt._eip; +unsigned long emul_offset; +int decode_size = state->data_available - state->bytes_consumed; + +/* +* This is annoing to have to explain the reasoning behind: +* ._eip is incremented by x86_decode_insn. It will be > .eip between +* decoding and emulating. +* .eip is incremented by x86_emulate_insn. It may be incremented +* beyond the length of instruction emulated E.G. if a jump is taken. +* +* If these are out of sync before emulating, then something is +* horribly wrong with the harness. +*/ +assert (state->ctxt.eip == state->ctxt._eip); + +if (decode_size <=0 ) { +debug ("Out of instructions\n" ); +return X86EMUL_UNHANDLEABLE ; + } + +init_emulate_ctxt (state); + state->ctxt.interruptibility =0 ; + state->ctxt.have_exception = false; + state->ctxt.exception.vector =-1 ; + state->ctxt.perm_ok = false; + state->ctxt.ud =0 ;// (emulation_type(0) & EMULTYPE_TRAP_UD); + +/* +* When decoding with NULL, 0, the emulator will use the emul_fetch +* op which handles incrementing the state->data variables. However +* x86_decode_insn will always try to grab 15 bytes which may be more +* than are left in the stream. +* +* Calling x86_decode_insn from a buffer with a length causes it to +* directly memcpy those bytes into the ctxt structure and does not +* increment state->bytes_consumed. In that case, we manually +* update state->bytes_consumed by the difference in the decoding +* _eip. This is gross but I cannot figure out a better way to do +* this. +* +* We must limit the size to avoid going over the buffer and since +* calling x86_decode_insn with a buffer does not go through any of +* our ops, we need to update bytes_consumed. The only improvement +* I can currently think of would be a nicer way to get the size of +* the decoded instruction. +*/ +if (decode_size >15 ) + decode_size =15 ; + + rc =x86_decode_insn (&state->ctxt, + &state->data[state->bytes_consumed], decode_size); +assert (state->ctxt._eip - prev_eip >0 );// Only move forward. + state->bytes_consumed += state->ctxt._eip - prev_eip; + +debug ("Decode result: %d\n" , rc); +if (rc !=X86EMUL_CONTINUE ) +return rc; + + emul_offset = state->ctxt._eip - state->ctxt.eip; +debug ("Instruction: " ); +print_n_bytes (&state->data[state->bytes_consumed - emul_offset], + emul_offset); + + state->ctxt.exception.address = state->vcpu.cr[2 ]; + +// This is extraneous but explicit due to the above assert + prev_eip = state->ctxt.eip; + rc =x86_emulate_insn (&state->ctxt); +debug ("Emulation result: %d\n" , rc); +dump_state_after ("emulating" , state); + +if (rc ==-1 ) { +return rc; + }else if (state->ctxt.have_exception) { +fprintf (stderr,"Emulator propagated exception: { " ); +fprintf (stderr,"vector: %d, " , state->ctxt.exception.vector); +fprintf (stderr,"error code: %d }\n" , + state->ctxt.exception.error_code); + rc =X86EMUL_UNHANDLEABLE ; + }else if (prev_eip == state->ctxt.eip) { +fprintf (stderr,"ctxt.eip not advanced.\n" ); + rc =X86EMUL_UNHANDLEABLE ; + } + +if (state->bytes_consumed == state->data_available) +debug ("emulator is done\n" ); + +return rc; }int emulate_until_complete (struct state *state) { +int count =0 ; + +do { + count++; + }while (step_emulator (state) ==X86EMUL_CONTINUE ); + +debug ("Emulated %d instructions\n" , count);return 0 ; }@@ -51,8 +383,16 @@ struct state *create_emulator(void)void reset_emulator (struct state *state) { +unsigned char *data = state->data; +size_t data_available = state->data_available; + +memset (state,0 ,sizeof (struct state )); + + state->data = data; + state->data_available = data_available; }void free_emulator (struct state *state) { +free (state); }tools/fuzz/x86ie/emulator_ops.h +0
-0
tools/fuzz/x86ie/emulator_ops.h struct state { · line 62
Quoted 5 lines
tools/fuzz/x86ie/emulator_ops.h +1
-1@@ -59,7 +59,7 @@ struct state {* Amount of bytes consumed for purposes other than instructions. * E.G. whether a memory access should fault. */ -size_t other_bytes_consumed; +size_t bytes_consumed;
Why in this patch?
tools/fuzz/x86ie/emulator_ops.h struct state { · line 78
Quoted 8 lines
tools/fuzz/x86ie/emulator_ops.h +0
-1/* Emulation context */ struct x86_emulate_ctxt ctxt;@@ -75,7 +75,10 @@ struct state {#define get_state (h) container_of(h, struct state, ctxt) -void buffer_stderr (void ) __attribute__((constructor));
Same question.
Quoted 27 lines
tools/fuzz/x86ie/emulator_ops.h +4
-0+static inline int number_of_gprs (struct x86_emulate_ctxt *c) +{ +return (c->mode ==X86EMUL_MODE_PROT64 ?16 :8 ); +}/* * Allocates space for, and creates a `struct state`. The user should set tools/fuzz/x86ie/scripts/afl-many new file +14
-0@@ -0,0 +1,28 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ +# This is for running AFL over NPROC or `nproc` cores with normal AFL options. + +export AFL_NO_AFFINITY=1 + +while [ -z "$sync_dir" ]; do + while getopts ":o:" opt; do + case "${opt}" in + o) + sync_dir="${OPTARG}" + ;; + *) + ;;
tools/fuzz/x86ie/scripts/afl-many line 24
Quoted 10 lines
tools/fuzz/x86ie/scripts/afl-many new file +10
-0+ esac + done + ((OPTIND++)) + [ $OPTIND -gt $# ] && break +done + +for i in $(seq 1 $(( ${NPROC:-$(nproc)} - 1)) ); do + taskset -c "$i" ./afl-fuzz -S "slave$i" $@ >/dev/null 2>&1 & +done +taskset -c 0 ./afl-fuzz -M master $@ >/dev/null 2>&1 &
You want to add a comment above the execution via taskset explaining that this is necessary for performance because the Linux scheduler otherwise works against you for local caching effects.
tools/fuzz/x86ie/scripts/afl-many line 26
Quoted 2 lines
tools/fuzz/x86ie/scripts/afl-many new file +2
-0+ +sleep 5
I'm still not a big fan of the sleep. Is there really no way to determine a sane monitoring state for real? Can you maybe find it out from looking at the open FDs on the afl-fuzz processes via /proc/$pid/fds?
Alex
Quoted 5 lines
tools/fuzz/x86ie/scripts/afl-many new file +2
-1+watch -n1 "echo \"Executing './afl-fuzz $@' on ${NPROC:-$(nproc)} cores.\" && ./afl-whatsup -s ${sync_dir}" +pkill afl-fuzz --2.17.1
Re: [v2, 3/4] Demonstrating unit testing via simple-harness
In reply to Sam CaccavaleQuoted 40 lines · Sam Caccavale
Simple-harness.cuses inline asm support to generate asm and then has the emulator emulate this code. This may be useful as a form of testing for the emulator.CR: https://code.amazon.com/reviews/CR-8591638
--- tools/fuzz/x86ie/Makefile | 7 ++++-- tools/fuzz/x86ie/simple-harness.c | 42 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-)create mode 100644
tools/fuzz/x86ie/simple-harness.ctools/fuzz/x86ie/Makefile +5
-2@@ -44,8 +44,11 @@ LOCAL_OBJS := emulator_ops.o stubs.o afl-harness: afl-harness.o $(LOCAL_OBJS) $(KERNEL_OBJS) @$(CC) -v $(KBUILD_CFLAGS) $(LOCAL_OBJS) $(KERNEL_OBJS) $< $(INCLUDES) -Istubs.h -o $@ -no-pie -all: afl-harness +simple-harness: simple-harness.o $(LOCAL_OBJS) $(KERNEL_OBJS) + @$(CC) -v $(KBUILD_CFLAGS) $(LOCAL_OBJS) $(KERNEL_OBJS) $< $(INCLUDES) -Istubs.h -o $@ -no-pie + +all: afl-harness simple-harness .PHONY: clean clean: - $(RM) -r *.o afl-harness + $(RM) -r *.o afl-harness simple-harnesstools/fuzz/x86ie/simple-harness.c new file +5
-0@@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <assert.h> +#include <stdint.h> +#include <stdio.h>
tools/fuzz/x86ie/simple-harness.c line 15
Quoted 10 lines
tools/fuzz/x86ie/simple-harness.c new file +10
-0+#include <string.h> +#include "emulator_ops.h" +#include <asm/kvm_emulate.h> + +extern void foo (void ) +{ + asmvolatile ("__start:" +".byte 0x32, 0x05, 0x00, 0x00, 0x00, 0x00;" // xor eax,DWORD PTR [rip+0x0] +".byte 0x90;" + //".byte 0x0f, 0x7f, 0xde;" // movq mm6,mm3
Why?
Alex
Re: [v2, 4/4] Added scripts for filtering, building, deploying
In reply to Sam CaccavaleQuoted 39 lines · Sam Caccavale
bin.shproduces output which diagnoses whether the crash was expected.coalesce.sh,gen_output.sh, andsummarize.share useful for parsing the large crash directories that afl produces.deploy_remote.shdoes all of the setup to launch a fuzz run viainstall_deps_ubuntu.sh,install_afl.sh,build.sh, andrun.sh.rebuild.shcleans the directories and executesbuild.sh--- tools/fuzz/x86ie/scripts/afl-many | 6 +-- tools/fuzz/x86ie/scripts/bin.sh | 49 +++++++++++++++++++ tools/fuzz/x86ie/scripts/build.sh | 32 ++++++++++++ tools/fuzz/x86ie/scripts/coalesce.sh | 6 +++ tools/fuzz/x86ie/scripts/deploy.sh | 9 ++++ tools/fuzz/x86ie/scripts/deploy_remote.sh | 9 ++++ tools/fuzz/x86ie/scripts/gen_output.sh | 11 +++++ tools/fuzz/x86ie/scripts/install_afl.sh | 14 ++++++ .../fuzz/x86ie/scripts/install_deps_ubuntu.sh | 5 ++ tools/fuzz/x86ie/scripts/rebuild.sh | 6 +++ tools/fuzz/x86ie/scripts/run.sh | 10 ++++ tools/fuzz/x86ie/scripts/summarize.sh | 9 ++++ 12 files changed, 163 insertions(+), 3 deletions(-)create mode 100755
tools/fuzz/x86ie/scripts/bin.shcreate mode 100755tools/fuzz/x86ie/scripts/build.shcreate mode 100755tools/fuzz/x86ie/scripts/coalesce.shcreate mode 100644tools/fuzz/x86ie/scripts/deploy.shcreate mode 100755tools/fuzz/x86ie/scripts/deploy_remote.shcreate mode 100755tools/fuzz/x86ie/scripts/gen_output.shcreate mode 100755tools/fuzz/x86ie/scripts/install_afl.shcreate mode 100755tools/fuzz/x86ie/scripts/install_deps_ubuntu.shcreate mode 100755tools/fuzz/x86ie/scripts/rebuild.shcreate mode 100755tools/fuzz/x86ie/scripts/run.shcreate mode 100755tools/fuzz/x86ie/scripts/summarize.shtools/fuzz/x86ie/scripts/afl-many +0
-0@@ -19,10 +19,10 @@ while [ -z "$sync_dir" ]; do done
tools/fuzz/x86ie/scripts/afl-many while [ -z "$sync_dir" ]; do · line 27
Quoted 10 lines
tools/fuzz/x86ie/scripts/afl-many +3
-3for i in $(seq 1 $(( ${NPROC:-$(nproc)} - 1)) ); do - taskset -c "$i" ./afl-fuzz -S "slave$i" $@ >/dev/null 2>&1 & + taskset -c "$i" $AFLPATH/afl-fuzz -S "slave$i" $@ >/dev/null 2>&1 & done -taskset -c 0 ./afl-fuzz -M master $@ >/dev/null 2>&1 & +taskset -c 0 $AFLPATH/afl-fuzz -M master $@ >/dev/null 2>&1 & sleep 5 -watch -n1 "echo \"Executing './afl-fuzz $@' on ${NPROC:-$(nproc)} cores.\" && ./afl-whatsup -s ${sync_dir}" +watch -n1 "echo \"Executing 'AFLPATH/afl-fuzz $@' on ${NPROC:-$(nproc)} cores.\" && $AFLPATH/afl-whatsup -s
This is missing a $ sign.
${sync_dir}"
Quoted 112 lines
tools/fuzz/x86ie/scripts/afl-many +0
-0pkill afl-fuzztools/fuzz/x86ie/scripts/bin.sh new file +49
-0@@ -0,0 +1,49 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +if [ "$#" -lt 3 ]; then + echo "Usage: './bin path/to/afl-harness path/to/afl_crash [path/to/linux/src/root]'" + exit +fi + +export AFL_HARNESS="$1" +export LINUX_SRC="$3" + +diagnose_segfault() { + SOURCE=$(gdb -batch -ex r -ex 'bt 2' --args $@ 2>&1 | grep -Po '#1.* \K([^ ]+:[0-9]+)'); + IFS=: read FILE LINE <<< "$SOURCE" + + OP="$(sed -n "${LINE}p" "$LINUX_SRC/$FILE" 2>/dev/null)" + if [ $? -ne 0 ]; then + OP="$(sed -n "${LINE}p" "$LINUX_SRC/tools/fuzz/x86_instruction_emulation/$FILE" 2>/dev/null)" + fi + + OP="$(echo $OP | grep -Po 'ops->\K([^(]+)')" + if [ -z "$OP" ]; then + echo "SEGV: unknown, in $FILE:$LINE" + else + echo "Expected: segfaulting on emulator->$OP" + fi +} +export -f diagnose_segfault + +bin() { + OUTPUT=$(bash -c "timeout 1s $AFL_HARNESS $1 2>&1" 2>&1) + RETVAL=$? + + echo "$OUTPUT" + if [ $RETVAL -eq 0 ]; then + echo "Terminated successfully" + elif [ $RETVAL -eq 124 ]; then + echo "Unknown: killed due to timeout. Loop likely." + elif echo "$OUTPUT" | grep -q "SEGV"; then + echo "$(diagnose_segfault $AFL_HARNESS $1)" + elif echo "$OUTPUT" | grep -q "FPE"; then + echo "Expected: floating point exception." + else + echo "Unknown cause of crash." + fi +} +export -f bin + +echo "$(bin $2 2>&1)"tools/fuzz/x86ie/scripts/build.sh new file +32
-0@@ -0,0 +1,32 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +kernel_objects="arch/x86/kvm/emulate.o arch/x86/lib/retpoline.o lib/find_bit.o" + +disable() { sed -i -r "/\b$1\b/c\# $1" .config; } +enable() { sed -i -r "/\b$1\b/c\\$1=y" .config; } + +make ${CC:+ "CC=$CC"} ${DEBUG:+ "DEBUG=1"} defconfig + +enable "CONFIG_DEBUG_INFO" +enable "CONFIG_STACKPROTECTOR" + +yes ' ' | make ${CC:+ "CC=$CC"} ${DEBUG:+ "DEBUG=1"} $kernel_objects + +omit_arg () { args=$(echo "$args" | sed "s/ $1//g"); } +add_arg () { args+=" $1"; } + +rebuild () { + args="$(head -1 $(dirname $1)/.$(basename $1).cmd | sed -e 's/.*:= //g')" + omit_arg "-mcmodel=kernel" + omit_arg "-mpreferred-stack-boundary=3" + add_arg "-fsanitize=address" + echo -e "Rebuilding $1 with \n$args" + eval "$args" +} + +for object in $kernel_objects; do + rebuild $object +done + +make ${CC:+ "CC=$CC"} ${DEBUG:+ "DEBUG=1"} tools/fuzztools/fuzz/x86ie/scripts/coalesce.sh new file +6
-0@@ -0,0 +1,6 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +mkdir -p all +rm -rf all/* +find . -type f -wholename '*crashes/id*' | parallel cp {} ./all/$(basename $(dirname {//})):{/}tools/fuzz/x86ie/scripts/deploy.sh new file +0
-0
tools/fuzz/x86ie/scripts/deploy.sh line 9
Quoted 9 lines
tools/fuzz/x86ie/scripts/deploy.sh new file +9
-0@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +REMOTE=$1 +DSTDIR=/dev/shm + +rsync -av $(pwd) $REMOTE:$DSTDIR + +ssh $REMOTE "cd $DSTDIR/$(basename $(pwd)); bash -s tools/fuzz/x86_instruction_emulation/scripts/deploy_remote.sh"
Does this really belong in here?
Quoted 58 lines
tools/fuzz/x86ie/scripts/deploy_remote.sh new file +9
-0@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +SCRIPTDIR=$(pwd)/tools/fuzz/x86_instruction_emulation/scripts + +$SCRIPTDIR/install_deps_ubuntu.sh +source $SCRIPTDIR/install_afl.sh +CC=$AFLPATH/afl-gcc $SCRIPTDIR/build.sh +FUZZDIR="${FUZZDIR:-$(pwd)/fuzz}" $SCRIPTDIR/run.shtools/fuzz/x86ie/scripts/gen_output.sh new file +11
-0@@ -0,0 +1,11 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +if [ "$#" -lt 3 ]; then + echo "Usage: '$0 path/to/afl-harness path/to/afl_crash_dir path/to/linux/src/root'" + exit +fi + +mkdir -p output +rm -rf output/* +find $2 -type f | parallel ./bin.sh $1 {} $3 '>' ./output/{/}.outtools/fuzz/x86ie/scripts/install_afl.sh new file +14
-0@@ -0,0 +1,14 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz +mkdir -p afl +tar xzf afl-latest.tgz -C afl --strip-components 1 + +pushd afl +set AFL_USE_ASAN +make clean all +export AFLPATH="$(pwd)" +popd + +sudo bash -c "echo core >/proc/sys/kernel/core_pattern"tools/fuzz/x86ie/scripts/install_deps_ubuntu.sh new file +0
-0
tools/fuzz/x86ie/scripts/install_deps_ubuntu.sh line 5
Quoted 5 lines
tools/fuzz/x86ie/scripts/install_deps_ubuntu.sh new file +5
-0@@ -0,0 +1,5 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +sudo apt update +sudo apt install -y make gcc wget screen build-essential libssh-dev flex bison libelf-dev bc
Same as this. This file could bitrot really quick. And it doesn't help any non-Ubuntu users.
I think most files in here are not strictly needed. Maybe split this patch into one that actually contains all changes necessary to easily start a test run and a separate one with all your convencience scripts?
Alex
Quoted 46 lines
tools/fuzz/x86ie/scripts/rebuild.sh new file +6
-0@@ -0,0 +1,6 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +make clean +make tools/fuzz_clean +FUZZDIR="./fuzz" ./tools/fuzz/x86_instruction_emulation/scripts/build.shtools/fuzz/x86ie/scripts/run.sh new file +10
-0@@ -0,0 +1,10 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +FUZZDIR="${FUZZDIR:-$(pwd)/fuzz}" + +mkdir -p $FUZZDIR/in +cp tools/fuzz/x86_instruction_emulation/rand_sample.bin $FUZZDIR/in +mkdir -p $FUZZDIR/out + +screen bash -c "ulimit -Sv $[21999999999 << 10]; ./tools/fuzz/x86_instruction_emulation/scripts/afl-many -m 22000000000 -i $FUZZDIR/in -o $FUZZDIR/out tools/fuzz/x86_instruction_emulation/afl-harness @@"tools/fuzz/x86ie/scripts/summarize.sh new file +9
-1@@ -0,0 +1,9 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ + +if [ "$#" -lt 1 ]; then + echo "Usage: '$0 path/to/output/dir'" + exit +fi + +time bash -c "find $1 -type f -exec tail -n 1 {} \; | sort | uniq -c | sort -rn" --2.17.1