Fuzzing the Hypervisor
Subsystems (2)
This email will be a technical description of what I, Felix Schmoll, as a Google Summer of Code student, did over summer.
=== Introduction / What is the project? ===
Fuzzing is a recent trend for systematic testing of interfaces by trying more or less random inputs and iterating over them. A subset of fuzzers uses code-
coverage as feedback when permuting and choosing inputs. The goal of this project was to test the hypercall interface of Xen that way.
While this was overall a very comprehensive problem, and a full-fledged test suite similar to OSS-Test is a desirable overall goal, this was not realistic for the scope of this project. Instead, a generic mechanism to obtain feedback on code-coverage was implemented and the output processed in order to actually run a particular fuzzer (AFL). This way, the project helped to develop a better understanding of the problem space and will lay the foundation for possible future endeavours in that direction.
==== Implementation ===
== Overview ==
It was clear from the beginning that the American Fuzzy Lop, in the following referred to as AFL, was supposed to be run on the hypervisor. Being a user-space coverage-based fuzzer, it had to be ported in some way to the kernel. The first step of this was to allow it to somehow obtain feedback on the coverage from Xen by implementing a hypercall. Further, a mechanism was needed to actually execute the hypercalls from a domain other than dom0 (there are many ways to stop the hypervisor from dom0, and this was not what was supposed to be tested). This was done by what will be referred to as the executor.
== Implementation of the hypercall ==
The implementation of the hypercall to obtain code-coverage feedback was realised using the fsanitize-coverage=trace_pc feature of gcc-6. It inserts a customisable function at every basic block of the binary. This function was instrumented to write the current program counter to a particular domain-specific location, allowing tracing of individual domains.
Slowing down the hypervisor in normal operation, this was added as a compile-option and the hypercall returns an error code in case the edge's are disabled.
== Executor ==
As mentioned, there are many ways to stop the hypervisor from the control-domain (dom0), such that the hypercalls were supposed to be executed from a different domain. Being very lean and not making any hypercalls in the background (this would make the tracing indeterministic), XTF was chosen for this purpose. While one could have used an isolated domain for every test case, this would have required recompiling the test case for every run, thus being extremely slow, and adding little value overall. Instead, a server was programmed, running in an endless loop, taking binary information (test cases) from AFL, parsing them into hypercalls and executing them. The server also does some sanity checks, ignoring hypercalls that would kill the domain but not the hypervisor. It also informs the fuzzer once a hypercall ended and it is ready to receive a new test case.
== Fuzzer (American Fuzzy Lop) ==
The fuzzer is executed in dom0 as this allows an easier communication with XTF via xenconsole. For this purpose, the domain of the xtf-server needs to be passed to AFL on startup and it needs to run with super-user rights. Changes were made to AFL to pass the test cases to the xtf-server via xenconsole instead of to a user-space program via the command line, as it normally does. This is a quite fundamental change to the functioning of AFL, but the overall philosophy behind the design here was to keep the keep the changes to as few functions as possible.
=== Discussion of the fork-server ===
In testing it is desirable to have the exact same conditions for every iteration, which is why AFL in user-space mode starts a new process for every test case, speeding it up by using fork(). The equivalent in this scenario would be to fork a complete machine running a hypervisor. This would however require VM-forking and nested virtualization, both of which are currently not supported by Xen. In this scenario, a machine (the host) would be running Xen and AFL in a domain on top of it. Another domain would run a nested version of Xen and would execute a series of hypercalls as a test. This VM could be forked before the tests in order to always have an identical environment of Xen.
Instead, the setup developed in this project just runs a single hypervisor and executes all hypercalls consecutively as one single large test-case, while still passing the information to AFL as if these were completely isolated. This is weird to some extent, but the best currently supported compromise.
=== Deliverables ===
There were a minor patch for Xen [1] and a minor patch for XTF [2].
The actual hypercall meanwhile has not been merged, even though it conforms to the requirements as layed out initially. The build system should be updated to allow a better specification of what to trace before the hypercall becomes useful. It is instead, together with the changes to AFL and XTF, attached to this email.
The main patches are based on the following versions:
* Xen, commit 6c9abf0e8022807bb7d677570d0775659950ff1a
* AFL 2.43b
* XTF, commit 8956f82ce1321b89deda6895d58e5788d2198477
=== How to fuzz the hypercall interface ===
* Run the XTF-server [sudo xtf_dir/xtf-runner xtf-server] and detach from console [Ctrl-C] after the initialisation log ended (three lines of "Executing ...")
* Create (empty) directories for findings and testcases
* Disable AFL-forkserver [export AFL_NO_FORKSRV=1]
* Start AFL [sudo ./afl-fuzz -i ~/testcase_dir -o ~/findings_dir -r [DOMID of XTF-server] /some/unused/path]
You can find the domain-id of the xtf-server using [sudo xl list].
The test case also is configured such that it only tests as a pv64 (this was the only setup my hardware supports). It is possible that adjustments have to be made to run other modes.
=== Future work ===
No bugs were found so far, and it is quite possible that there aren't any without using more sophisticated fuzzing (i.e. valid buffers). Possible areas of improvement are the following:
* Minor usability improvements, like starting the XTF-server from within AFL
* Increase coverage
* Solve remaining problems with determinism
* Make XTF server more sophisticated, encode more information about hypercalls (e.g. pass valid buffers into hypercalls)
* Improve speed
* Improve stability (there still seem to be some files that shouldn't be compiled with tracing, although the stability is 100% for most hypercalls)
* Do the more complicated approach with complete hypervisor cloning
The patches for the hypercall, XTF and AFL will be sent in reply to this document.
=== References ===
Links to other documents:
* [1] xenconsole: Add option to xenconsole to always forward console input (
https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=32e5bd5dcf6f45c2fc39d8d62b52b53d3e79ada7
)
* [2] Implement pv_read_some (
https://xenbits.xen.org/gitweb/?p=xtf.git;a=commit;h=34f052d41415cbd424f37c1a63a47464ce8f63e9
)
* [3] The GSoC page of this project (
https://summerofcode.withgoogle.com/projects/#5585891117498368)
* [4] Summary of the design session at the Xen summit (
https://lists.xen.org/archives/html/xen-devel/2017-07/msg02138.html)
* [5] Design proposal for the hypercall (
https://lists.xen.org/archives/html/xen-devel/2017-05/msg02210.html)
* [6] Design proposal for fuzzing the hypervisor (
https://lists.xen.org/archives/html/xen-devel/2017-06/msg02924.html)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH 1/1 Fuzzing the hypervisor
Changes based on version 2.43b of AFL
--- Makefile | 2 +- afl-fuzz.c | 233 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- hash_map.h | 82 ++++++++++++++++++++++ 3 files changed, 306 insertions(+), 11 deletions(-) create mode 100644 hash_map.h
Makefile +1 -1
@@ -70,7 +70,7 @@ afl-as: afl-as.c afl-as.h $(COMM_HDR) | test_x86 ln -sf afl-as as afl-fuzz: afl-fuzz.c $(COMM_HDR) | test_x86 - $(CC) $(CFLAGS) [email protected] -o $@ $(LDFLAGS) + $(CC) $(CFLAGS) [email protected] -o $@ $(LDFLAGS) -lxenctrl afl-showmap: afl-showmap.c $(COMM_HDR) | test_x86 $(CC) $(CFLAGS) [email protected] -o $@ $(LDFLAGS)
afl-fuzz.c +223 -10
335 lines not on this page · raw message
hash_map.h new file +82 -0
82 lines not on this page · raw message
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/1 Fuzzing the hypervisor
In reply to Felix SchmollChanges based on commit 8956f82ce1321b89deda6895d58e5788d2198477
--- include/xen/xen.h | 1 + include/xtf/hypercall.h | 8 +- tests/mk_hcall/.main.c.swo | Bin 0 -> 12288 bytes tests/mk_hcall/Makefile | 9 +++ tests/mk_hcall/main.c | 51 +++++++++++++ tests/xtf-server/Makefile | 9 +++ tests/xtf-server/main.c | 183 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 257 insertions(+), 4 deletions(-) create mode 100644 tests/mk_hcall/.main.c.swo create mode 100644 tests/mk_hcall/Makefile create mode 100644 tests/mk_hcall/main.c create mode 100644 tests/xtf-server/Makefile create mode 100644 tests/xtf-server/main.c
include/xen/xen.h +1 -0
@@ -51,6 +51,7 @@#define __HYPERVISOR_tmem_op 38#define __HYPERVISOR_xc_reserved_op 39/* reserved for XenClient */ #define __HYPERVISOR_xenpmu_op 40 +#define __HYPERVISOR_trace_pc 42/* Architecture-specific hypercall definitions. */ #define __HYPERVISOR_arch_0 48
include/xtf/hypercall.h +4 -4
@@ -151,16 +151,16 @@ static inline long hypercall_shutdown(unsigned int reason)return hypercall_sched_op (SCHEDOP_shutdown, &reason); } +/* hypercall_yield and hypercall_poll are dummies so that printing to +console doesn't interfere with tracing determinism */ static inline void hypercall_yield (void ) { -hypercall_sched_op (SCHEDOP_yield,NULL ); +return ; }static inline long hypercall_poll (evtchn_port_t port) { -struct sched_poll poll = { .ports = &port, .nr_ports =1 }; - -return hypercall_sched_op (SCHEDOP_poll, &poll); +return 0 ; }static inline int hypercall_register_callback (const xen_callback_register_t *arg)
tests/mk_hcall/.main.c.swo new file binary +0 -0
tests/mk_hcall/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +include $(ROOT)/build/common.mk + +NAME := mk_hcall +CATEGORY := utility +TEST-ENVS := pv64 + +obj-perenv += main.o + +include $(ROOT)/build/gen.mk
tests/mk_hcall/main.c new file +51 -0
51 lines not on this page · raw message
tests/xtf-server/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +include $(ROOT)/build/common.mk + +NAME := xtf-server +CATEGORY := utility +TEST-ENVS := pv64 + +obj-perenv += main.o + +include $(ROOT)/build/gen.mk
tests/xtf-server/main.c new file +183 -0
183 lines not on this page · raw message
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH XTF] Fuzzing the hypervisor
Quoted 5 lines · Felix Schmoll
Changes based on commit
8956f82ce1321b89deda6895d58e5788d2198477--- include/xen/xen.h | 1 + include/xtf/hypercall.h | 8 +- tests/mk_hcall/.main.c.swo | Bin 0 -> 12288 bytes
This should not be included.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH 1/1 Fuzzing the hypervisor
In reply to Felix SchmollBased on commit 6c9abf0e8022807bb7d677570d0775659950ff1a
--- xen/Kconfig.debug | 7 +++- xen/Rules.mk | 4 ++ xen/arch/arm/traps.c | 1 + xen/arch/x86/Makefile | 2 + xen/arch/x86/hvm/hypercall.c | 1 + xen/arch/x86/hypercall.c | 1 + xen/arch/x86/pv/Makefile | 2 + xen/arch/x86/pv/hypercall.c | 1 + xen/common/Makefile | 13 ++++++ xen/common/domain.c | 4 ++ xen/common/trace_pc.c | 96 +++++++++++++++++++++++++++++++++++++++++++ xen/common/trace_pc_stub.c | 39 ++++++++++++++++++ xen/include/public/trace_pc.h | 38 +++++++++++++++++ xen/include/public/xen.h | 1 + xen/include/xen/hypercall.h | 7 ++++ xen/include/xen/sched.h | 6 +++ xen/include/xen/trace_pc.h | 31 ++++++++++++++ 17 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 xen/common/trace_pc.c create mode 100644 xen/common/trace_pc_stub.c create mode 100644 xen/include/public/trace_pc.h create mode 100644 xen/include/xen/trace_pc.h
xen/Kconfig.debug +6 -1
@@ -98,7 +98,6 @@ config PERF_ARRAYS ---help--- Enables software performance counter array histograms. - config VERBOSE_DEBUG bool "Verbose debug messages" default DEBUG
@@ -114,6 +113,12 @@ config DEVICE_TREE_DEBUG logged in the Xen ring buffer. If unsure, say N here. +config TRACE_PC + bool "Enable pc-tracing" + default false + ---help--- + Adds tracing support to the hypervisor (needed for the trace_pc hypercall). + endif # DEBUG || EXPERT endmenu
xen/Rules.mk +4 -0
@@ -170,6 +170,10 @@ clean:: $(addprefix _clean_, $(subdir-all)) _clean_%/: FORCE $(MAKE) -f $(BASEDIR)/Rules.mk -C $* clean +ifeq ($(CONFIG_TRACE_PC),y) +$(objs-need-tracing): CFLAGS += -fsanitize-coverage=trace-pc +endif + %.o: %.c Makefile $(CC) $(CFLAGS) -c $< -o $@
xen/arch/arm/traps.c +1 -0
@@ -1419,6 +1419,7 @@ static arm_hypercall_t arm_hypercall_table[] = {HYPERCALL (platform_op,1 ),HYPERCALL_ARM (vcpu_op,3 ),HYPERCALL (vm_assist,2 ), + HYPERCALL(trace_pc, 4), };#ifndef NDEBUG
xen/arch/x86/Makefile +2 -0
@@ -74,6 +74,8 @@ efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \ -O $(BASEDIR)/include/xen/compile.h ]; then \ echo '$(TARGET).efi'; fi) +objs-need-tracing := cpuid.o hypercall.o + ifneq ($(build_id_linker),) notes_phdrs = --notes else
xen/arch/x86/hvm/hypercall.c +1 -0
@@ -132,6 +132,7 @@ static const hypercall_table_t hvm_hypercall_table[] = {COMPAT_CALL (mmuext_op),HYPERCALL (xenpmu_op),COMPAT_CALL (dm_op), +HYPERCALL (trace_pc), HYPERCALL(arch_1) };
xen/arch/x86/hypercall.c +1 -0
@@ -68,6 +68,7 @@ const hypercall_args_t hypercall_args_table[NR_hypercalls] =ARGS (xenpmu_op,2 ),ARGS (dm_op,3 ),ARGS (mca,1 ), +ARGS (trace_pc,4 ), ARGS(arch_1, 1), };
xen/arch/x86/pv/Makefile +2 -0
@@ -11,3 +11,5 @@ obj-y += traps.o obj-bin-y += dom0_build.init.o obj-bin-y += gpr_switch.o + +objs-need-tracing += hypercall.o
xen/arch/x86/pv/hypercall.c +1 -0
@@ -80,6 +80,7 @@ static const hypercall_table_t pv_hypercall_table[] = {HYPERCALL (xenpmu_op),COMPAT_CALL (dm_op),HYPERCALL (mca), +HYPERCALL (trace_pc), HYPERCALL(arch_1), };
xen/common/Makefile +13 -0
@@ -55,6 +55,8 @@ obj-y += tasklet.o obj-y += time.o obj-y += timer.o obj-y += trace.o +obj-y += trace_pc.o +obj-$(CONFIG_TRACE_PC) += trace_pc_stub.o obj-y += version.o obj-y += virtual_region.o obj-y += vm_event.o
@@ -80,3 +82,14 @@ subdir-$(CONFIG_GCOV) += gcov subdir-y += libelf subdir-$(CONFIG_HAS_DEVICE_TREE) += libfdt + +objs-need-tracing := bsearch.o \ + decompress.o device_tree.o domain.o domctl.o earlycpio.o grant_table.o \ + guestcopy.o gunzip.o inflate.o kernel.o kexec.o keyhandler.o kimage.o \ + lib.o livepatch.o lzo.o mem_access.o memory.o multicall.o notifier.o \ + page_alloc.o pdx.o perfc.o radix_tree.o rangeset.o \ + rbtree.o shutdown.o sort.o stop_machine.o \ + symbols.o symbols-dummy.o sysctl.o time.o tmem.o \ + tmem_control.o tmem_xen.o trace.o unlz4.o unlzo.o unxz.o version.o \ + virtual_region.o vmap.o vm_event.o warning.o xenoprof.o \ + xmalloc_tlsf.o
xen/common/domain.c +4 -0
@@ -874,6 +874,10 @@ void domain_destroy(struct domain *d)rcu_assign_pointer (*pd, d->next_in_hashbucket);spin_unlock (&domlist_update_lock); +#ifdef CONFIG_TRACE_PC +xfree (d->tracing_buffer); +#endif +/* Schedule RCU asynchronous completion of domain destroy. */ call_rcu (&d->rcu, complete_domain_destroy); }
xen/common/trace_pc.c new file +96 -0
96 lines not on this page · raw message
xen/common/trace_pc_stub.c new file +39 -0
@@ -0,0 +1,39 @@ +/****************************************************************************** +* trace_pc_stub.c +* +* Edge function/stub for the program counter tracing hypercall. +* +* Copyright (c) 2017 Felix Schmoll <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms and conditions of the GNU General Public +* License, version 2, as published by the Free Software Foundation. +* +* 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/>. +*/ + +#include <xen/trace_pc.h> +#include <xen/kernel.h> + +void __sanitizer_cov_trace_pc (void ) +{ +struct domain *d; + +if ( system_state < SYS_STATE_active ) +return ; + + d = current->domain; + +if ( d->tracing_buffer && + (d->tracing_buffer_pos < d->tracing_buffer_size) ) + { + d->tracing_buffer[d->tracing_buffer_pos++] = + (uint64_t )__builtin_return_address (0 ); + } +}
xen/include/public/trace_pc.h new file +38 -0
@@ -0,0 +1,38 @@ +/****************************************************************************** +* trace_pc.h +* +* Macros for program counter tracing hypercall. +* +* Copyright (C) 2017 Felix Schmoll <[email protected]> +* +* Permission is hereby granted, free of charge, to any person +* obtaining a copy of this software and associated documentation +* files (the "Software"), to deal in the Software without restriction, +* including without limitation the rights to use, copy, modify, merge, +* publish, distribute, sublicense, and/or sell copies of the Software, +* and to permit persons to whom the Software is furnished to do so, +* subject to the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef __XEN_PUBLIC_TRACE_PC_H__ +#define __XEN_PUBLIC_TRACE_PC_H__ + +#if defined(__XEN__) || defined(__XEN_TOOLS__) + +#define XEN_TRACE_PC_START 0 +#define XEN_TRACE_PC_STOP 1 + +#endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */ + +#endif /* __XEN_PUBLIC_TRACE_PC_H__ */
xen/include/public/xen.h +1 -0
@@ -121,6 +121,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);#define __HYPERVISOR_xc_reserved_op 39/* reserved for XenClient */ #define __HYPERVISOR_xenpmu_op 40#define __HYPERVISOR_dm_op 41 +#define __HYPERVISOR_trace_pc 42/* Architecture-specific hypercall definitions. */ #define __HYPERVISOR_arch_0 48
xen/include/xen/hypercall.h +7 -0
@@ -83,6 +83,13 @@ do_xen_version( XEN_GUEST_HANDLE_PARAM(void) arg);extern long +do_trace_pc ( +domid_t dom_id, +int mode, +unsigned int size, +XEN_GUEST_HANDLE_PARAM (uint64_t ) buf); + +extern long do_console_io( int cmd, int count,
xen/include/xen/sched.h +6 -0
@@ -483,6 +483,12 @@ struct domain unsigned int guest_request_enabled : 1; unsigned int guest_request_sync : 1; } monitor; + +#ifdef CONFIG_TRACE_PC +uint64_t * tracing_buffer; +unsigned int tracing_buffer_pos; +unsigned int tracing_buffer_size; +#endif };/* Protect updates/reads (resp.) of domain_list and domain_hash. */
xen/include/xen/trace_pc.h new file +31 -0
@@ -0,0 +1,31 @@ +/****************************************************************************** +* trace_pc.h +* +* Declarations for the program counter tracing hypercall +* +* Copyright (C) 2017 Felix Schmoll <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms and conditions of the GNU General Public +* License, version 2, as published by the Free Software Foundation. +* +* 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/>. +*/ + +#ifndef __TRACE_PC_H__ +#define __TRACE_PC_H__ + +#include <xen/sched.h> +#include <xen/types.h> + +#include <asm/current.h> + +void __sanitizer_cov_trace_pc (void ); + +#endif /* __TRACE_PC_H__ */
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel