Skip to contentSkip to conversation

xen: add undefined behaviour sanitizer

RFC PATCH In review Wei Liu 3 participants 5 messages Oct 3, 2017

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

RFC PATCH 1/1 xen: add undefined behaviour sanitizer

1. Introduce Kconfig UBSAN option.
2. Import and adapt a bunch of hooks from Linux kernel with as little modification as possible.
3. Provide mechanism to filter out unwanted files.

This is x86 only at the moment. It should be easy to make it work on arm, but I don't have the test environment to figure out what files need to be filtered out.

Currently two files are filtered out:

1. arch/x86/setup.o -- xen crashes very early without any output
2. drivers/acpi/tables/tbutils.o -- it has requirement to access misaligned memory before current is properly set up.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

Though this is not going to be in 4.10 I think it would be valuable to fix the issues it reports.

And example output on x86:
https://paste.debian.net/988890/

---
 xen/Kconfig.debug                |   8 +
 xen/Rules.mk                     |   4 +
 xen/arch/x86/Makefile            |   2 +
 xen/common/Makefile              |   1 +
 xen/common/ubsan/Makefile        |   1 +
 xen/common/ubsan/ubsan.c         | 486 +++++++++++++++++++++++++++++++++++++++
 xen/common/ubsan/ubsan.h         |  84 +++++++
 xen/drivers/acpi/tables/Makefile |   2 +
 xen/include/xen/sched.h          |   3 +
 9 files changed, 591 insertions(+)
 create mode 100644 xen/common/ubsan/Makefile
 create mode 100644 xen/common/ubsan/ubsan.c
 create mode 100644 xen/common/ubsan/ubsan.h
xen/Kconfig.debug +8 -0
index 195d504147..e63b533459 100644
@@ -121,6 +121,14 @@ config SCRUB_DEBUG
 	  Verify that pages that need to be scrubbed before being allocated to
 	  a guest are indeed scrubbed.
 
+config UBSAN
+	bool "Undefined behaviour sanitizer"
+	depends on X86
+	---help---
+	  Enable undefined behaviour sanitizer.
+
+	  If unsure, say N here.
+
 endif # DEBUG || EXPERT
 
 endmenu
xen/Rules.mk +4 -0
index cafc67b86e..2659f8a4d1 100644
@@ -119,6 +119,10 @@ ifeq ($(CONFIG_GCOV),y)
 $(filter-out %.init.o $(nogcov-y),$(obj-y) $(obj-bin-y) $(extra-y)): CFLAGS += -fprofile-arcs -ftest-coverage
 endif
 
+ifeq ($(CONFIG_UBSAN),y)
+$(filter-out %.init.o $(noubsan-y),$(obj-y) $(obj-bin-y) $(extra-y)): CFLAGS += -fsanitize=undefined
+endif
+
 ifeq ($(CONFIG_LTO),y)
 CFLAGS += -flto
 LDFLAGS-$(clang) += -plugin LLVMgold.so
xen/arch/x86/Makefile +2 -0
index d5d58a205e..648f70799f 100644
@@ -69,6 +69,8 @@ obj-y += hpet.o
 obj-y += vm_event.o
 obj-y += xstate.o
 
+noubsan-y += setup.o
+
 x86_emulate.o: x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h
 
 efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \
xen/common/Makefile +1 -0
index 39e2614546..66cc2c8995 100644
@@ -75,6 +75,7 @@ tmem-$(CONFIG_COMPAT) += compat/tmem_xen.o
 obj-$(CONFIG_TMEM) += $(tmem-y)
 
 subdir-$(CONFIG_GCOV) += gcov
+subdir-$(CONFIG_UBSAN) += ubsan
 
 subdir-y += libelf
 subdir-$(CONFIG_HAS_DEVICE_TREE) += libfdt
xen/common/ubsan/Makefile new file +1 -0
new file mode 100644
index 0000000000..e6b85ea187
@@ -0,0 +1 @@
+obj-y += ubsan.o
xen/common/ubsan/ubsan.c new file +486 -0

486 lines not on this page · raw message

xen/common/ubsan/ubsan.h new file +84 -0

84 lines not on this page · raw message

xen/drivers/acpi/tables/Makefile +2 -0
index e2210e0a05..5309ba8d73 100644
@@ -3,3 +3,5 @@ obj-bin-y += tbinstal.init.o
 obj-y += tbutils.o
 obj-bin-y += tbxface.init.o
 obj-bin-y += tbxfroot.init.o
+
+noubsan-y += tbutils.o
xen/include/xen/sched.h +3 -0
index 002ba29d6d..eac54dfc53 100644
@@ -210,6 +210,9 @@ struct vcpu
     bool             hcall_compat;
 #endif
 
+#ifdef CONFIG_UBSAN
+    int              in_ubsan;
+#endif
 
     /*
      * > 0: a single port is being polled;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 5 lines · Wei Liu

1. Introduce Kconfig UBSAN option.
2. Import and adapt a bunch of hooks from Linux kernel with as little modification as possible.
3. Provide mechanism to filter out unwanted files.

Quoted 7 lines

This is x86 only at the moment. It should be easy to make it work on arm, but I don't have the test environment to figure out what files need to be filtered out.

Currently two files are filtered out:

1. arch/x86/setup.o -- xen crashes very early without any output

With GCC 6.3 and with unaligned access checking?  If so, it is probably the code which initially sets up the IDT.

I'm not sure enabling unaligned access checking by default is

Quoted 10 lines

2. drivers/acpi/tables/tbutils.o -- it has requirement to access misaligned memory before current is properly set up.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

Quoted 5 lines

Though this is not going to be in 4.10 I think it would be valuable to fix the issues it reports.

And example output on x86:
https://paste.debian.net/988890/

The traps.c and hypercall.c objections can't plausibly be fixed.  Each example is from when we are hand-crafting x86 opcode, which does inherently have unaligned data.

Quoted 26 lines
xen/common/ubsan/ubsan.c new file +19 -0
new file mode 100644
index 0000000000..aa359e712c
@@ -0,0 +1,486 @@
+/*
+ * UBSAN error reporting functions
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Author: Andrey Ryabinin <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Adapt to Xen by:
+ *  Andrew Cooper <[email protected]>
+ *  Wei Liu <[email protected]>
+ */
+
+#include <xen/bitops.h>
+#include <xen/kernel.h>
+#include <xen/lib.h>
+#include <xen/types.h>

xen/common/ubsan/ubsan.c line 29

Quoted 10 lines
xen/common/ubsan/ubsan.c new file +10 -0
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/percpu.h>
+
+#define pr_err(...) printk(XENLOG_ERR __VA_ARGS__)
+#define dump_stack dump_execution_state
+#define __noreturn noreturn
+#define CONFIG_ARCH_SUPPORTS_INT128
+#define u64 long long unsigned int
+#define s64 long long int

These last three should be in asm-$ARCH/config.h or similar.  I'm fairly sure arm32 will struggle with INT128.

Also, I think the current (re)engineering should stay.  There is nothing vcpu specific about the in_ubsan flag.  (Especially, given your tbutils.o observation above.)

As for commits introducing the code, I had a vague plan to introduce the unmodified code from upstream Linux, then my forward ported patch (as that isn't in Linux, even today), and then a change adding the Kconfig option, and these defines, to get it to compile.

Given that this is clearly a debugging option, and its basically ready, I wouldn't necessarily hold off from putting it into 4.10.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
In reply to Wei Liu

Hi Wei,

Quoted 8 lines · Wei Liu

1. Introduce Kconfig UBSAN option.
2. Import and adapt a bunch of hooks from Linux kernel with as little modification as possible.
3. Provide mechanism to filter out unwanted files.

This is x86 only at the moment. It should be easy to make it work on arm, but I don't have the test environment to figure out what files need to be filtered out.

Sadly, it is not trivial on Arm to get UBSAN running. It is not because of UBSAN itself but because of the memory layout. We assume that Xen is always smaller than 2MB (assembly code included :/).

With UBSAN enabled, Xen binary go way beyond 2MB and will fail to build.

I actually have a development branch with a prototype for UBSAN for Arm but I haven't found time to upstream it. I will rebase it on your work.

Quoted 14 lines

Currently two files are filtered out:

1. arch/x86/setup.o -- xen crashes very early without any output
2. drivers/acpi/tables/tbutils.o -- it has requirement to access misaligned memory before current is properly set up.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

Quoted 5 lines

Though this is not going to be in 4.10 I think it would be valuable to fix the issues it reports.

And example output on x86:
https://paste.debian.net/988890/

I have got some for Arm too. I will try to post the fixes for Xen 4.10.

[...]

Quoted 23 lines
xen/common/ubsan/ubsan.c new file +17 -0
new file mode 100644
index 0000000000..aa359e712c
@@ -0,0 +1,486 @@
+/*
+ * UBSAN error reporting functions
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Author: Andrey Ryabinin <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Adapt to Xen by:
+ *  Andrew Cooper <[email protected]>
+ *  Wei Liu <[email protected]>
+ */
+
+#include <xen/bitops.h>
+#include <xen/kernel.h>

xen/common/ubsan/ubsan.c line 27

Quoted 10 lines
xen/common/ubsan/ubsan.c new file +10 -0
+#include <xen/lib.h>
+#include <xen/types.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/percpu.h>
+
+#define pr_err(...) printk(XENLOG_ERR __VA_ARGS__)
+#define dump_stack dump_execution_state
+#define __noreturn noreturn
+#define CONFIG_ARCH_SUPPORTS_INT128

I believe this should go in arch specific code. For instance, Arm does not support INT128.

xen/common/ubsan/ubsan.c line 29

Quoted 2 lines
xen/common/ubsan/ubsan.c new file +2 -0
+#define u64 long long unsigned int
+#define s64 long long int

Would not be better to have a typedef for those 2 and use uint64_t and int64_t?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw
In reply to Andrew Cooper

Hi,

Quoted 31 lines · Andrew Cooper
Quoted 30 lines · Wei Liu
xen/common/ubsan/ubsan.c new file +24 -0
new file mode 100644
index 0000000000..aa359e712c
@@ -0,0 +1,486 @@
+/*
+ * UBSAN error reporting functions
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Author: Andrey Ryabinin <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Adapt to Xen by:
+ *  Andrew Cooper <[email protected]>
+ *  Wei Liu <[email protected]>
+ */
+
+#include <xen/bitops.h>
+#include <xen/kernel.h>
+#include <xen/lib.h>
+#include <xen/types.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/percpu.h>
+
+#define pr_err(...) printk(XENLOG_ERR __VA_ARGS__)
Quoted 8 lines

xen/common/ubsan/ubsan.c line 29

Quoted 5 lines
xen/common/ubsan/ubsan.c new file +5 -0
+#define dump_stack dump_execution_state
+#define __noreturn noreturn
+#define CONFIG_ARCH_SUPPORTS_INT128
+#define u64 long long unsigned int
+#define s64 long long int

These last three should be in asm-$ARCH/config.h or similar. I'm fairly sure arm32 will struggle with INT128.

AFAICT, Linux doesn't enable this option for both arm64 and arm32.

Quoted 9 lines

Also, I think the current (re)engineering should stay. There is nothing vcpu specific about the in_ubsan flag. (Especially, given your tbutils.o observation above.)

As for commits introducing the code, I had a vague plan to introduce the unmodified code from upstream Linux, then my forward ported patch (as that isn't in Linux, even today), and then a change adding the Kconfig option, and these defines, to get it to compile.

That would be handy to see the changes.

Quoted 3 lines

Given that this is clearly a debugging option, and its basically ready, I wouldn't necessarily hold off from putting it into 4.10.

I thought about the same when I saw the patch. I guess we can get UBSAN support for x86 into Xen 4.10. I don't think it will be possible for Arm given the necessary rework in the memory layout (we need to expand the region size for Xen mapping) and assembly.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#3 lore raw
In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Wei Liu

1. Introduce Kconfig UBSAN option.
2. Import and adapt a bunch of hooks from Linux kernel with as little modification as possible.
3. Provide mechanism to filter out unwanted files.

This is x86 only at the moment. It should be easy to make it work on arm, but I don't have the test environment to figure out what files need to be filtered out.

Quoted 7 lines
Quoted 3 lines

Currently two files are filtered out:

1. arch/x86/setup.o -- xen crashes very early without any output

With GCC 6.3 and with unaligned access checking?  If so, it is probably the code which initially sets up the IDT.

I suppose so. It's definitely somewhere inside __start_xen, before IDT is ready.

I think misalign check should stay -- the false positive are reported once anyway. It might catch real issues in the future.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel