Skip to content

Commit 277a108

Browse files
keestorvalds
authored andcommitted
ubsan: split "bounds" checker from other options
In order to do kernel builds with the bounds checker individually available, introduce CONFIG_UBSAN_BOUNDS, with the remaining options under CONFIG_UBSAN_MISC. For example, using this, we can start to expand the coverage syzkaller is providing. Right now, all of UBSan is disabled for syzbot builds because taken as a whole, it is too noisy. This will let us focus on one feature at a time. For the bounds checker specifically, this provides a mechanism to eliminate an entire class of array overflows with close to zero performance overhead (I cannot measure a difference). In my (mostly) defconfig, enabling bounds checking adds ~4200 checks to the kernel. Performance changes are in the noise, likely due to the branch predictors optimizing for the non-fail path. Some notes on the bounds checker: - it does not instrument {mem,str}*()-family functions, it only instruments direct indexed accesses (e.g. "foo[i]"). Dealing with the {mem,str}*()-family functions is a work-in-progress around CONFIG_FORTIFY_SOURCE[1]. - it ignores flexible array members, including the very old single byte (e.g. "int foo[1];") declarations. (Note that GCC's implementation appears to ignore _all_ trailing arrays, but Clang only ignores empty, 0, and 1 byte arrays[2].) [1] KSPP#6 [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92589 Suggested-by: Elena Petrova <[email protected]> Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Andrey Ryabinin <[email protected]> Acked-by: Dmitry Vyukov <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Dan Carpenter <[email protected]> Cc: "Gustavo A. R. Silva" <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 0887a7e commit 277a108

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

lib/Kconfig.ubsan

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
config ARCH_HAS_UBSAN_SANITIZE_ALL
33
bool
44

5-
config UBSAN
5+
menuconfig UBSAN
66
bool "Undefined behaviour sanity checker"
77
help
88
This option enables the Undefined Behaviour sanity checker.
99
Compile-time instrumentation is used to detect various undefined
1010
behaviours at runtime. For more details, see:
1111
Documentation/dev-tools/ubsan.rst
1212

13+
if UBSAN
14+
1315
config UBSAN_TRAP
1416
bool "On Sanitizer warnings, abort the running kernel code"
15-
depends on UBSAN
1617
depends on $(cc-option, -fsanitize-undefined-trap-on-error)
1718
help
1819
Building kernels with Sanitizer features enabled tends to grow
@@ -25,9 +26,26 @@ config UBSAN_TRAP
2526
the system. For some system builders this is an acceptable
2627
trade-off.
2728

29+
config UBSAN_BOUNDS
30+
bool "Perform array index bounds checking"
31+
default UBSAN
32+
help
33+
This option enables detection of directly indexed out of bounds
34+
array accesses, where the array size is known at compile time.
35+
Note that this does not protect array overflows via bad calls
36+
to the {str,mem}*cpy() family of functions (that is addressed
37+
by CONFIG_FORTIFY_SOURCE).
38+
39+
config UBSAN_MISC
40+
bool "Enable all other Undefined Behavior sanity checks"
41+
default UBSAN
42+
help
43+
This option enables all sanity checks that don't have their
44+
own Kconfig options. Disable this if you only want to have
45+
individually selected checks.
46+
2847
config UBSAN_SANITIZE_ALL
2948
bool "Enable instrumentation for the entire kernel"
30-
depends on UBSAN
3149
depends on ARCH_HAS_UBSAN_SANITIZE_ALL
3250

3351
# We build with -Wno-maybe-uninitilzed, but we still want to
@@ -44,7 +62,6 @@ config UBSAN_SANITIZE_ALL
4462

4563
config UBSAN_NO_ALIGNMENT
4664
bool "Disable checking of pointers alignment"
47-
depends on UBSAN
4865
default y if HAVE_EFFICIENT_UNALIGNED_ACCESS
4966
help
5067
This option disables the check of unaligned memory accesses.
@@ -57,7 +74,9 @@ config UBSAN_ALIGNMENT
5774

5875
config TEST_UBSAN
5976
tristate "Module for testing for undefined behavior detection"
60-
depends on m && UBSAN
77+
depends on m
6178
help
6279
This is a test module for UBSAN.
6380
It triggers various undefined behavior, and detect it.
81+
82+
endif # if UBSAN

scripts/Makefile.ubsan

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ ifdef CONFIG_UBSAN_ALIGNMENT
55
CFLAGS_UBSAN += $(call cc-option, -fsanitize=alignment)
66
endif
77

8+
ifdef CONFIG_UBSAN_BOUNDS
9+
CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
10+
endif
11+
12+
ifdef CONFIG_UBSAN_MISC
813
CFLAGS_UBSAN += $(call cc-option, -fsanitize=shift)
914
CFLAGS_UBSAN += $(call cc-option, -fsanitize=integer-divide-by-zero)
1015
CFLAGS_UBSAN += $(call cc-option, -fsanitize=unreachable)
1116
CFLAGS_UBSAN += $(call cc-option, -fsanitize=signed-integer-overflow)
12-
CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
1317
CFLAGS_UBSAN += $(call cc-option, -fsanitize=object-size)
1418
CFLAGS_UBSAN += $(call cc-option, -fsanitize=bool)
1519
CFLAGS_UBSAN += $(call cc-option, -fsanitize=enum)
20+
endif
1621

1722
ifdef CONFIG_UBSAN_TRAP
1823
CFLAGS_UBSAN += $(call cc-option, -fsanitize-undefined-trap-on-error)

0 commit comments

Comments
 (0)