Skip to content

Commit f594e28

Browse files
committed
Merge tag 'hardening-v5.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull compiler hardening updates from Kees Cook: "These are various compiler-related hardening feature updates. Notable is the addition of an explicit limited rationale for, and deprecation schedule of, gcc-plugins. gcc-plugins: - remove support for GCC 4.9 and older (Ard Biesheuvel) - remove duplicate include in gcc-common.h (Ye Guojin) - Explicitly document purpose and deprecation schedule (Kees Cook) - Remove cyc_complexity (Kees Cook) instrumentation: - Avoid harmless Clang option under CONFIG_INIT_STACK_ALL_ZERO (Kees Cook) Clang LTO: - kallsyms: strip LTO suffixes from static functions (Nick Desaulniers)" * tag 'hardening-v5.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: gcc-plugins: remove duplicate include in gcc-common.h gcc-plugins: Remove cyc_complexity gcc-plugins: Explicitly document purpose and deprecation schedule kallsyms: strip LTO suffixes from static functions gcc-plugins: remove support for GCC 4.9 and older hardening: Avoid harmless Clang option under CONFIG_INIT_STACK_ALL_ZERO
2 parents 0146337 + 6425392 commit f594e28

13 files changed

+75
-320
lines changed

Documentation/kbuild/gcc-plugins.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ This infrastructure was ported from grsecurity [6]_ and PaX [7]_.
3232
.. [7] https://pax.grsecurity.net/
3333
3434
35+
Purpose
36+
=======
37+
38+
GCC plugins are designed to provide a place to experiment with potential
39+
compiler features that are neither in GCC nor Clang upstream. Once
40+
their utility is proven, the goal is to upstream the feature into GCC
41+
(and Clang), and then to finally remove them from the kernel once the
42+
feature is available in all supported versions of GCC.
43+
44+
Specifically, new plugins should implement only features that have no
45+
upstream compiler support (in either GCC or Clang).
46+
47+
When a feature exists in Clang but not GCC, effort should be made to
48+
bring the feature to upstream GCC (rather than just as a kernel-specific
49+
GCC plugin), so the entire ecosystem can benefit from it.
50+
51+
Similarly, even if a feature provided by a GCC plugin does *not* exist
52+
in Clang, but the feature is proven to be useful, effort should be spent
53+
to upstream the feature to GCC (and Clang).
54+
55+
After a feature is available in upstream GCC, the plugin will be made
56+
unbuildable for the corresponding GCC version (and later). Once all
57+
kernel-supported versions of GCC provide the feature, the plugin will
58+
be removed from the kernel.
59+
60+
3561
Files
3662
=====
3763

@@ -70,7 +96,6 @@ Enable the GCC plugin infrastructure and some plugin(s) you want to use
7096
in the kernel config::
7197

7298
CONFIG_GCC_PLUGINS=y
73-
CONFIG_GCC_PLUGIN_CYC_COMPLEXITY=y
7499
CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y
75100
...
76101

@@ -89,4 +114,3 @@ The GCC plugins are in scripts/gcc-plugins/. You need to put plugin source files
89114
right under scripts/gcc-plugins/. Creating subdirectories is not supported.
90115
It must be added to scripts/gcc-plugins/Makefile, scripts/Makefile.gcc-plugins
91116
and a relevant Kconfig file.
92-
See the cyc_complexity_plugin.c (CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) GCC plugin.

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,12 +831,12 @@ endif
831831

832832
# Initialize all stack variables with a zero value.
833833
ifdef CONFIG_INIT_STACK_ALL_ZERO
834-
# Future support for zero initialization is still being debated, see
835-
# https://bugs.llvm.org/show_bug.cgi?id=45497. These flags are subject to being
836-
# renamed or dropped.
837834
KBUILD_CFLAGS += -ftrivial-auto-var-init=zero
835+
ifdef CONFIG_CC_IS_CLANG
836+
# https://bugs.llvm.org/show_bug.cgi?id=45497
838837
KBUILD_CFLAGS += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
839838
endif
839+
endif
840840

841841
# While VLAs have been removed, GCC produces unreachable stack probes
842842
# for the randomize_kstack_offset feature. Disable it for all compilers.

kernel/kallsyms.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,46 @@ static unsigned long kallsyms_sym_address(int idx)
164164
return kallsyms_relative_base - 1 - kallsyms_offsets[idx];
165165
}
166166

167-
#if defined(CONFIG_CFI_CLANG) && defined(CONFIG_LTO_CLANG_THIN)
168-
/*
169-
* LLVM appends a hash to static function names when ThinLTO and CFI are
170-
* both enabled, i.e. foo() becomes foo$707af9a22804d33c81801f27dcfe489b.
171-
* This causes confusion and potentially breaks user space tools, so we
172-
* strip the suffix from expanded symbol names.
173-
*/
174-
static inline bool cleanup_symbol_name(char *s)
167+
static bool cleanup_symbol_name(char *s)
175168
{
176169
char *res;
177170

171+
if (!IS_ENABLED(CONFIG_LTO_CLANG))
172+
return false;
173+
174+
/*
175+
* LLVM appends various suffixes for local functions and variables that
176+
* must be promoted to global scope as part of LTO. This can break
177+
* hooking of static functions with kprobes. '.' is not a valid
178+
* character in an identifier in C. Suffixes observed:
179+
* - foo.llvm.[0-9a-f]+
180+
* - foo.[0-9a-f]+
181+
* - foo.[0-9a-f]+.cfi_jt
182+
*/
183+
res = strchr(s, '.');
184+
if (res) {
185+
*res = '\0';
186+
return true;
187+
}
188+
189+
if (!IS_ENABLED(CONFIG_CFI_CLANG) ||
190+
!IS_ENABLED(CONFIG_LTO_CLANG_THIN) ||
191+
CONFIG_CLANG_VERSION >= 130000)
192+
return false;
193+
194+
/*
195+
* Prior to LLVM 13, the following suffixes were observed when thinLTO
196+
* and CFI are both enabled:
197+
* - foo$[0-9]+
198+
*/
178199
res = strrchr(s, '$');
179-
if (res)
200+
if (res) {
180201
*res = '\0';
202+
return true;
203+
}
181204

182-
return res != NULL;
205+
return false;
183206
}
184-
#else
185-
static inline bool cleanup_symbol_name(char *s) { return false; }
186-
#endif
187207

188208
/* Lookup the address for this symbol. Returns 0 if not found. */
189209
unsigned long kallsyms_lookup_name(const char *name)

scripts/Makefile.gcc-plugins

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3-
gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so
4-
53
gcc-plugin-$(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) += latent_entropy_plugin.so
64
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) \
75
+= -DLATENT_ENTROPY_PLUGIN

scripts/gcc-plugins/Kconfig

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,10 @@ menuconfig GCC_PLUGINS
1919

2020
if GCC_PLUGINS
2121

22-
config GCC_PLUGIN_CYC_COMPLEXITY
23-
bool "Compute the cyclomatic complexity of a function" if EXPERT
24-
depends on !COMPILE_TEST # too noisy
25-
help
26-
The complexity M of a function's control flow graph is defined as:
27-
M = E - N + 2P
28-
where
29-
30-
E = the number of edges
31-
N = the number of nodes
32-
P = the number of connected components (exit nodes).
33-
34-
Enabling this plugin reports the complexity to stderr during the
35-
build. It mainly serves as a simple example of how to create a
36-
gcc plugin for the kernel.
37-
3822
config GCC_PLUGIN_SANCOV
3923
bool
24+
# Plugin can be removed once the kernel only supports GCC 6+
25+
depends on !CC_HAS_SANCOV_TRACE_PC
4026
help
4127
This plugin inserts a __sanitizer_cov_trace_pc() call at the start of
4228
basic blocks. It supports all gcc versions with plugin support (from
@@ -83,8 +69,6 @@ config GCC_PLUGIN_RANDSTRUCT
8369
the existing seed and will be removed by a make mrproper or
8470
make distclean.
8571

86-
Note that the implementation requires gcc 4.7 or newer.
87-
8872
This plugin was ported from grsecurity/PaX. More information at:
8973
* https://grsecurity.net/
9074
* https://pax.grsecurity.net/

scripts/gcc-plugins/cyc_complexity_plugin.c

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)