new file mode 100644
@@ -0,0 +1,31 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+# libelf fuzz target
+vpath %.c ../../../xen/common/libelf
+CFLAGS += -I../../../xen/common/libelf
+ELF_SRCS-y += libelf-tools.c libelf-loader.c libelf-dominfo.c
+ELF_LIB_OBJS := $(patsubst %.c,%.o,$(ELF_SRCS-y))
+
+$(patsubst %.c,%.o,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign
+
+$(ELF_LIB_OBJS): CFLAGS += -DFUZZ_NO_LIBXC $(CFLAGS_xeninclude)
+
+libelf-fuzzer.o: CFLAGS += $(CFLAGS_xeninclude)
+
+libelf.a: $(ELF_LIB_OBJS)
+ $(AR) rc $@ $^
+
+.PHONY: libelf-fuzzer-all
+libelf-fuzzer-all: libelf.a libelf-fuzzer.o
+
+# Common targets
+.PHONY: all
+all: libelf-fuzzer-all
+
+.PHONY: distclean
+distclean: clean
+
+.PHONY: clean
+clean:
+ rm -f *.o *.a
new file mode 100644
@@ -0,0 +1,32 @@
+#include <inttypes.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <xen/libelf/libelf.h>
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+ struct elf_binary elf_buf, *elf;
+ struct elf_dom_parms parms;
+
+ elf = &elf_buf;
+
+ memset(elf, 0, sizeof(*elf));
+ elf_init(elf, (const char *)data, size);
+ elf_parse_binary(elf);
+ elf_xen_parse(elf, &parms);
+
+ return 0;
+}
+
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
@@ -72,8 +72,10 @@
#include <xen/elfnote.h>
#include <xen/libelf/libelf.h>
+#ifndef FUZZ_NO_LIBXC
#include "xenctrl.h"
#include "xc_private.h"
+#endif
#define elf_msg(elf, fmt, args ... ) \
elf_call_log_callback(elf, 0, fmt , ## args );
Source code and Makefile to fuzz libelf in Google's oss-fuzz infrastructure. Introduce FUZZ_NO_LIBXC in libelf-private.h. That macro will be set when compiling libelf fuzzer target because libxc is not required in libelf fuzzing. Signed-off-by: Wei Liu <wei.liu2@citrix.com> --- Cc: Andrew Cooper <andrew.cooper3@citrix.com> Cc: George Dunlap <George.Dunlap@eu.citrix.com> Cc: Ian Jackson <ian.jackson@eu.citrix.com> Cc: Jan Beulich <jbeulich@suse.com> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Cc: Stefano Stabellini <sstabellini@kernel.org> Cc: Tim Deegan <tim@xen.org> Cc: Wei Liu <wei.liu2@citrix.com> --- tools/fuzz/libelf/Makefile | 31 +++++++++++++++++++++++++++++++ tools/fuzz/libelf/libelf-fuzzer.c | 32 ++++++++++++++++++++++++++++++++ xen/common/libelf/libelf-private.h | 2 ++ 3 files changed, 65 insertions(+) create mode 100644 tools/fuzz/libelf/Makefile create mode 100644 tools/fuzz/libelf/libelf-fuzzer.c