Skip to content

Commit 6a6155f

Browse files
George Popescutorvalds
authored andcommitted
ubsan: introduce CONFIG_UBSAN_LOCAL_BOUNDS for Clang
When the kernel is compiled with Clang, -fsanitize=bounds expands to -fsanitize=array-bounds and -fsanitize=local-bounds. Enabling -fsanitize=local-bounds with Clang has the unfortunate side-effect of inserting traps; this goes back to its original intent, which was as a hardening and not a debugging feature [1]. The same feature made its way into -fsanitize=bounds, but the traps remained. For that reason, -fsanitize=bounds was split into 'array-bounds' and 'local-bounds' [2]. Since 'local-bounds' doesn't behave like a normal sanitizer, enable it with Clang only if trapping behaviour was requested by CONFIG_UBSAN_TRAP=y. Add the UBSAN_BOUNDS_LOCAL config to Kconfig.ubsan to enable the 'local-bounds' option by default when UBSAN_TRAP is enabled. [1] http://lists.llvm.org/pipermail/llvm-dev/2012-May/049972.html [2] http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20131021/091536.html Suggested-by: Marco Elver <elver@google.com> Signed-off-by: George Popescu <georgepope@android.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: David Brazdil <dbrazdil@google.com> Reviewed-by: Marco Elver <elver@google.com> Cc: Masahiro Yamada <masahiroy@kernel.org> Cc: Michal Marek <michal.lkml@markovi.net> Cc: Nathan Chancellor <natechancellor@gmail.com> Cc: Nick Desaulniers <ndesaulniers@google.com> Cc: Kees Cook <keescook@chromium.org> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: https://lkml.kernel.org/r/20200922074330.2549523-1-georgepope@google.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5cf53f3 commit 6a6155f

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/Kconfig.ubsan

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ config UBSAN_BOUNDS
4747
to the {str,mem}*cpy() family of functions (that is addressed
4848
by CONFIG_FORTIFY_SOURCE).
4949

50+
config UBSAN_LOCAL_BOUNDS
51+
bool "Perform array local bounds checking"
52+
depends on UBSAN_TRAP
53+
depends on CC_IS_CLANG
54+
depends on !UBSAN_KCOV_BROKEN
55+
help
56+
This option enables -fsanitize=local-bounds which traps when an
57+
exception/error is detected. Therefore, it should be enabled only
58+
if trapping is expected.
59+
Enabling this option detects errors due to accesses through a
60+
pointer that is derived from an object of a statically-known size,
61+
where an added offset (which may not be known statically) is
62+
out-of-bounds.
63+
5064
config UBSAN_MISC
5165
bool "Enable all other Undefined Behavior sanity checks"
5266
default UBSAN

scripts/Makefile.ubsan

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ ifdef CONFIG_UBSAN_ALIGNMENT
44
endif
55

66
ifdef CONFIG_UBSAN_BOUNDS
7-
CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
7+
ifdef CONFIG_CC_IS_CLANG
8+
CFLAGS_UBSAN += -fsanitize=array-bounds
9+
else
10+
CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
11+
endif
12+
endif
13+
14+
ifdef CONFIG_UBSAN_LOCAL_BOUNDS
15+
CFLAGS_UBSAN += -fsanitize=local-bounds
816
endif
917

1018
ifdef CONFIG_UBSAN_MISC

0 commit comments

Comments
 (0)