Skip to content

Commit 0887a7e

Browse files
keestorvalds
authored andcommitted
ubsan: add trap instrumentation option
Patch series "ubsan: Split out bounds checker", v5. This splits out the bounds checker so it can be individually used. This is enabled in Android and hopefully for syzbot. Includes LKDTM tests for behavioral corner-cases (beyond just the bounds checker), and adjusts ubsan and kasan slightly for correct panic handling. This patch (of 6): The Undefined Behavior Sanitizer can operate in two modes: warning reporting mode via lib/ubsan.c handler calls, or trap mode, which uses __builtin_trap() as the handler. Using lib/ubsan.c means the kernel image is about 5% larger (due to all the debugging text and reporting structures to capture details about the warning conditions). Using the trap mode, the image size changes are much smaller, though at the loss of the "warning only" mode. In order to give greater flexibility to system builders that want minimal changes to image size and are prepared to deal with kernel code being aborted and potentially destabilizing the system, this introduces CONFIG_UBSAN_TRAP. The resulting image sizes comparison: text data bss dec hex filename 1953366 6183037 18554956 44271656 2a38828 vmlinux.stock 19991849 7618513 18874448 46484810 2c54d4a vmlinux.ubsan 19712181 6284181 18366540 44362902 2a4ec96 vmlinux.ubsan-trap CONFIG_UBSAN=y: image +4.8% (text +2.3%, data +18.9%) CONFIG_UBSAN_TRAP=y: image +0.2% (text +0.9%, data +1.6%) Additionally adjusts the CONFIG_UBSAN Kconfig help for clarity and removes the mention of non-existing boot param "ubsan_handle". Suggested-by: Elena Petrova <[email protected]> Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Acked-by: Dmitry Vyukov <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Dan Carpenter <[email protected]> Cc: "Gustavo A. R. Silva" <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Ard Biesheuvel <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7baf219 commit 0887a7e

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

lib/Kconfig.ubsan

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,25 @@ config ARCH_HAS_UBSAN_SANITIZE_ALL
55
config UBSAN
66
bool "Undefined behaviour sanity checker"
77
help
8-
This option enables undefined behaviour sanity checker
8+
This option enables the Undefined Behaviour sanity checker.
99
Compile-time instrumentation is used to detect various undefined
10-
behaviours in runtime. Various types of checks may be enabled
11-
via boot parameter ubsan_handle
12-
(see: Documentation/dev-tools/ubsan.rst).
10+
behaviours at runtime. For more details, see:
11+
Documentation/dev-tools/ubsan.rst
12+
13+
config UBSAN_TRAP
14+
bool "On Sanitizer warnings, abort the running kernel code"
15+
depends on UBSAN
16+
depends on $(cc-option, -fsanitize-undefined-trap-on-error)
17+
help
18+
Building kernels with Sanitizer features enabled tends to grow
19+
the kernel size by around 5%, due to adding all the debugging
20+
text on failure paths. To avoid this, Sanitizer instrumentation
21+
can just issue a trap. This reduces the kernel size overhead but
22+
turns all warnings (including potentially harmless conditions)
23+
into full exceptions that abort the running kernel code
24+
(regardless of context, locks held, etc), which may destabilize
25+
the system. For some system builders this is an acceptable
26+
trade-off.
1327

1428
config UBSAN_SANITIZE_ALL
1529
bool "Enable instrumentation for the entire kernel"

lib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ quiet_cmd_build_OID_registry = GEN $@
286286
clean-files += oid_registry_data.c
287287

288288
obj-$(CONFIG_UCS2_STRING) += ucs2_string.o
289+
ifneq ($(CONFIG_UBSAN_TRAP),y)
289290
obj-$(CONFIG_UBSAN) += ubsan.o
291+
endif
290292

291293
UBSAN_SANITIZE_ubsan.o := n
292294
KASAN_SANITIZE_ubsan.o := n

scripts/Makefile.ubsan

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# SPDX-License-Identifier: GPL-2.0
22
ifdef CONFIG_UBSAN
3+
4+
ifdef CONFIG_UBSAN_ALIGNMENT
5+
CFLAGS_UBSAN += $(call cc-option, -fsanitize=alignment)
6+
endif
7+
38
CFLAGS_UBSAN += $(call cc-option, -fsanitize=shift)
49
CFLAGS_UBSAN += $(call cc-option, -fsanitize=integer-divide-by-zero)
510
CFLAGS_UBSAN += $(call cc-option, -fsanitize=unreachable)
@@ -9,8 +14,8 @@ ifdef CONFIG_UBSAN
914
CFLAGS_UBSAN += $(call cc-option, -fsanitize=bool)
1015
CFLAGS_UBSAN += $(call cc-option, -fsanitize=enum)
1116

12-
ifdef CONFIG_UBSAN_ALIGNMENT
13-
CFLAGS_UBSAN += $(call cc-option, -fsanitize=alignment)
17+
ifdef CONFIG_UBSAN_TRAP
18+
CFLAGS_UBSAN += $(call cc-option, -fsanitize-undefined-trap-on-error)
1419
endif
1520

1621
# -fsanitize=* options makes GCC less smart than usual and

0 commit comments

Comments
 (0)