Skip to content

Commit 2d47c69

Browse files
committed
ubsan: Tighten UBSAN_BOUNDS on GCC
The use of -fsanitize=bounds on GCC will ignore some trailing arrays, leaving a gap in coverage. Switch to using -fsanitize=bounds-strict to match Clang's stricter behavior. Cc: Marco Elver <elver@google.com> Cc: Masahiro Yamada <masahiroy@kernel.org> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Nick Desaulniers <ndesaulniers@google.com> Cc: Nicolas Schier <nicolas@fjasle.eu> Cc: Tom Rix <trix@redhat.com> Cc: Josh Poimboeuf <jpoimboe@kernel.org> Cc: Miroslav Benes <mbenes@suse.cz> Cc: linux-kbuild@vger.kernel.org Cc: llvm@lists.linux.dev Signed-off-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20230405022356.gonna.338-kees@kernel.org
1 parent f1fcbaa commit 2d47c69

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

lib/Kconfig.ubsan

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,50 +27,56 @@ config UBSAN_TRAP
2727
the system. For some system builders this is an acceptable
2828
trade-off.
2929

30-
config CC_HAS_UBSAN_BOUNDS
31-
def_bool $(cc-option,-fsanitize=bounds)
30+
config CC_HAS_UBSAN_BOUNDS_STRICT
31+
def_bool $(cc-option,-fsanitize=bounds-strict)
32+
help
33+
The -fsanitize=bounds-strict option is only available on GCC,
34+
but uses the more strict handling of arrays that includes knowledge
35+
of flexible arrays, which is comparable to Clang's regular
36+
-fsanitize=bounds.
3237

3338
config CC_HAS_UBSAN_ARRAY_BOUNDS
3439
def_bool $(cc-option,-fsanitize=array-bounds)
40+
help
41+
Under Clang, the -fsanitize=bounds option is actually composed
42+
of two more specific options, -fsanitize=array-bounds and
43+
-fsanitize=local-bounds. However, -fsanitize=local-bounds can
44+
only be used when trap mode is enabled. (See also the help for
45+
CONFIG_LOCAL_BOUNDS.) Explicitly check for -fsanitize=array-bounds
46+
so that we can build up the options needed for UBSAN_BOUNDS
47+
with or without UBSAN_TRAP.
3548

3649
config UBSAN_BOUNDS
3750
bool "Perform array index bounds checking"
3851
default UBSAN
39-
depends on CC_HAS_UBSAN_ARRAY_BOUNDS || CC_HAS_UBSAN_BOUNDS
52+
depends on CC_HAS_UBSAN_ARRAY_BOUNDS || CC_HAS_UBSAN_BOUNDS_STRICT
4053
help
4154
This option enables detection of directly indexed out of bounds
4255
array accesses, where the array size is known at compile time.
4356
Note that this does not protect array overflows via bad calls
4457
to the {str,mem}*cpy() family of functions (that is addressed
4558
by CONFIG_FORTIFY_SOURCE).
4659

47-
config UBSAN_ONLY_BOUNDS
48-
def_bool CC_HAS_UBSAN_BOUNDS && !CC_HAS_UBSAN_ARRAY_BOUNDS
49-
depends on UBSAN_BOUNDS
60+
config UBSAN_BOUNDS_STRICT
61+
def_bool UBSAN_BOUNDS && CC_HAS_UBSAN_BOUNDS_STRICT
5062
help
51-
This is a weird case: Clang's -fsanitize=bounds includes
52-
-fsanitize=local-bounds, but it's trapping-only, so for
53-
Clang, we must use -fsanitize=array-bounds when we want
54-
traditional array bounds checking enabled. For GCC, we
55-
want -fsanitize=bounds.
63+
GCC's bounds sanitizer. This option is used to select the
64+
correct options in Makefile.ubsan.
5665

5766
config UBSAN_ARRAY_BOUNDS
58-
def_bool CC_HAS_UBSAN_ARRAY_BOUNDS
59-
depends on UBSAN_BOUNDS
67+
def_bool UBSAN_BOUNDS && CC_HAS_UBSAN_ARRAY_BOUNDS
68+
help
69+
Clang's array bounds sanitizer. This option is used to select
70+
the correct options in Makefile.ubsan.
6071

6172
config UBSAN_LOCAL_BOUNDS
62-
bool "Perform array local bounds checking"
63-
depends on UBSAN_TRAP
64-
depends on $(cc-option,-fsanitize=local-bounds)
65-
help
66-
This option enables -fsanitize=local-bounds which traps when an
67-
exception/error is detected. Therefore, it may only be enabled
68-
with CONFIG_UBSAN_TRAP.
69-
70-
Enabling this option detects errors due to accesses through a
71-
pointer that is derived from an object of a statically-known size,
72-
where an added offset (which may not be known statically) is
73-
out-of-bounds.
73+
def_bool UBSAN_ARRAY_BOUNDS && UBSAN_TRAP
74+
help
75+
This option enables Clang's -fsanitize=local-bounds which traps
76+
when an access through a pointer that is derived from an object
77+
of a statically-known size, where an added offset (which may not
78+
be known statically) is out-of-bounds. Since this option is
79+
trap-only, it depends on CONFIG_UBSAN_TRAP.
7480

7581
config UBSAN_SHIFT
7682
bool "Perform checking for bit-shift overflows"

scripts/Makefile.ubsan

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Enable available and selected UBSAN features.
44
ubsan-cflags-$(CONFIG_UBSAN_ALIGNMENT) += -fsanitize=alignment
5-
ubsan-cflags-$(CONFIG_UBSAN_ONLY_BOUNDS) += -fsanitize=bounds
5+
ubsan-cflags-$(CONFIG_UBSAN_BOUNDS_STRICT) += -fsanitize=bounds-strict
66
ubsan-cflags-$(CONFIG_UBSAN_ARRAY_BOUNDS) += -fsanitize=array-bounds
77
ubsan-cflags-$(CONFIG_UBSAN_LOCAL_BOUNDS) += -fsanitize=local-bounds
88
ubsan-cflags-$(CONFIG_UBSAN_SHIFT) += -fsanitize=shift

0 commit comments

Comments
 (0)