Skip to content

Commit 07654f1

Browse files
kconfig: Using Kconfig as global hardening selector (#89)
Make hardening flags and global sentry security selectable by kconfig. Previously declared hardening flag inclusion are not (de)activable if needed, for example in debug or when target system flash is too small for such a size impact.
2 parents 41427bc + 5b7d454 commit 07654f1

File tree

4 files changed

+112
-62
lines changed

4 files changed

+112
-62
lines changed

kernel/Kconfig

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,68 @@ config WITH_JTAG_CONNECTED
6464
depends on BUILD_TARGET_DEBUG || BUILD_TARGET_AUTOTEST
6565

6666
endmenu
67+
68+
menu "Kernel security"
69+
70+
# symbols selected by build mode
71+
config SECU_SSP_USER
72+
bool
73+
# support for user threads stack canaries
74+
75+
config SECU_SSP_KERN
76+
bool
77+
# support for kernel threads stack canaries
78+
79+
config SECU_HW_SETNCHECK
80+
bool
81+
---help---
82+
when possible (may depend on the hardware device), double verify that
83+
the target register configuration is valid and active
84+
# Unable set and check of critical hardware registers
85+
# (iowrite + ioread and compare)
86+
87+
config SECU_NO_WEAKTYPES
88+
bool
89+
---help---
90+
Comparison to 0 lead to potential weak optimizations that should be
91+
avoided in security critical environment. In the same way, hamming distance
92+
of 1 can be easily faulted, in comparison with a discrete type.
93+
This is done by using secure_bool_t type and by activating (gcc>=14)
94+
95+
config SECU_TASK_INTEGRITY_AT_BOOT
96+
bool
97+
98+
config SECU_ENFORCE_COMPARE
99+
bool "Enforce comparison checks by compiler"
100+
default y
101+
---help---
102+
All variable comparison and conditional branch related comparison are
103+
hardened using the `harden-compares` and `harden-conditional-branches`
104+
hardening flags (gcc>=13)
105+
106+
config SECU_ENFORCE_CFI
107+
bool "Harden control flow redundancy"
108+
default n
109+
---help---
110+
Emit extra code to set booleans when entering basic blocks, and to verify
111+
and trap, at function exits, when the booleans do not form an execution
112+
path that is compatible with the control flow graph.
113+
114+
config SECU_ENFORCE_RETURNING_CALLS
115+
depends on SECU_ENFORCE_CFI
116+
default y
117+
bool "Enforce return-time control flow"
118+
---help---
119+
Harden return time checks, including noreturn invalid behavior, using
120+
-fhardcfr-check-returning-calls and -fhardcfr-check-noreturn-calls=always flags
121+
(gcc>=14)
122+
123+
config SECU_ENFORCE_FAULT_INJECTION
124+
bool "Enforce fault injection projections"
125+
---help---
126+
Enable this flag to enforce formally proven execution
127+
paths with supplementary checks that whould have been dead
128+
code in nominal execution
129+
130+
131+
endmenu

kernel/src/managers/Kconfig

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -135,43 +135,16 @@ endif
135135

136136
menu "Security manager"
137137

138-
# symbols selected by build mode
139-
config SECU_SSP_USER
140-
bool
141-
# support for user threads stack canaries
142-
143-
config SECU_SSP_KERN
144-
bool
145-
# support for kernel threads stack canaries
146-
147-
config SECU_HW_SETNCHECK
148-
bool
149-
---help---
150-
when possible (may depend on the hardware device), double verify that
151-
the target register configuration is valid and active
152-
# Unable set and check of critical hardware registers
153-
# (iowrite + ioread and compare)
154-
155-
config SECU_NO_WEAKTYPES
156-
bool
157-
---help---
158-
Comparison to 0 lead to potential weak optimizations that should be
159-
avoided in security critical environment. In the same way, hamming distance
160-
of 1 can be easily faulted, in comparison with a discrete type
161-
162-
config SECU_LOOP_DBLE_IDX
163-
bool
164-
# Double index count and check in critical loops
165-
166-
config SECU_TASK_INTEGRITY_AT_BOOT
167-
bool
168-
169-
config SECU_ENFORCE_FAULT_INJECTION
170-
bool "Enforce fault injection projections"
171-
---help---
172-
Enable this flag to enforce formally proven execution
173-
paths with supplementary checks that whould have been dead
174-
code in nominal execution
138+
choice
139+
bool "Entropy source"
140+
default SECURITY_HW_ENTROPY if HAS_RNG
141+
default SECURITY_PGC32_ENTROPY if !HAS_RNG
142+
config SECURITY_HW_ENTROPY
143+
bool "Using HW RNG as entropy source"
144+
depends on HAS_RNG
145+
config SECURITY_PGC32_ENTROPY
146+
bool "Using PGC32 as entropy source"
147+
endchoice
175148

176149
endmenu
177150

kernel/src/managers/security/entropy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#include <stddef.h>
5-
#if defined(CONFIG_HAS_RNG)
5+
#if defined(CONFIG_SECURITY_HW_ENTROPY)
66
#include <bsp/drivers/rng/rng.h>
77
#endif
88
#include <sentry/zlib/crypto.h>
@@ -25,7 +25,7 @@ static uint32_t seed;
2525
kstatus_t mgr_security_entropy_init(void)
2626
{
2727
kstatus_t status;
28-
#if !defined(CONFIG_HAS_RNG)
28+
#if !defined(CONFIG_SECURITY_HW_ENTROPY)
2929
pr_warn("HW RNG not supported, initializing SW entropy backend.");
3030
/* Here we use PGC32 has this is the lonely function we have to generate random
3131
sequence in SW mode. To be replaced by another pseudo-random (or higher security
@@ -65,7 +65,7 @@ kstatus_t mgr_security_entropy_generate(uint32_t *seed)
6565
if (unlikely(seed == NULL)) {
6666
goto end;
6767
}
68-
#if CONFIG_HAS_RNG
68+
#if CONFIG_SECURITY_HW_ENTROPY
6969
status = rng_get(seed);
7070
#else
7171
*seed = pcg32();

meson.build

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,8 @@ project('sentry-kernel', 'c',
3030

3131
meson.add_dist_script('support/meson/version.sh', 'set-dist', meson.project_version())
3232

33-
# Testing high security flags of cross compiler. These are gcc 13-14 hardening flags.
34-
# See https://gcc.gnu.org/onlinedocs/gcc-14.1.0/gcc/Instrumentation-Options.html#index-fharden-compares
35-
# for more information about each flag
36-
hardening_cflags = [
37-
'-fharden-compares',
38-
'-fharden-conditional-branches',
39-
'-fharden-control-flow-redundancy',
40-
'-fhardcfr-check-returning-calls',
41-
'-fstack-clash-protection',
42-
]
43-
activated_hardening_cflags = []
44-
4533
compiler = meson.get_compiler('c', native: false)
4634

47-
foreach cflag: hardening_cflags
48-
if compiler.has_argument(cflag)
49-
activated_hardening_cflags += cflag
50-
endif
51-
endforeach
52-
53-
5435
objcopy = find_program('objcopy')
5536
sentry_install_script = find_program('support/scripts/install.py')
5637

@@ -69,6 +50,39 @@ kconfig_h = kconfig_proj.get_variable('kconfig_h')
6950
kconfig_rustargs = kconfig_proj.get_variable('kconfig_rustargs')
7051
kconfig_data = kconfig_proj.get_variable('kconfig_data')
7152

53+
# Testing high security flags of cross compiler. These are gcc 13-14 hardening flags.
54+
# See https://gcc.gnu.org/onlinedocs/gcc-14.1.0/gcc/Instrumentation-Options.html#index-fharden-compares
55+
# for more information about each flag
56+
activated_hardening_cflags = []
57+
hardening_cflags = [
58+
'-fstack-clash-protection',
59+
]
60+
61+
if kconfig_data.get('CONFIG_SECU_ENFORCE_COMPARE', 0) == 1
62+
hardening_cflags += [
63+
'-fharden-compares',
64+
'-fharden-conditional-branches',
65+
]
66+
endif
67+
68+
if kconfig_data.get('CONFIG_SECU_ENFORCE_CFI', 0) == 1
69+
hardening_cflags += [
70+
'-fharden-control-flow-redundancy',
71+
]
72+
endif
73+
74+
if kconfig_data.get('CONFIG_SECU_ENFORCE_RETURNING_CALLS', 0) == 1
75+
hardening_cflags += [
76+
'-fhardcfr-check-returning-calls',
77+
]
78+
endif
79+
80+
foreach cflag: hardening_cflags
81+
if compiler.has_argument(cflag)
82+
activated_hardening_cflags += cflag
83+
endif
84+
endforeach
85+
7286
external_deps = []
7387

7488
global_build_args = [
@@ -83,11 +97,9 @@ global_build_args = [
8397
'-Wno-unused-function', # FIXME: while in early dev
8498
'-Wno-unused-variable', # FIXME: while in early dev
8599
'-Wno-unused-parameter', # FIXME: while in early dev
100+
activated_hardening_cflags,
86101
]
87102

88-
# adding supported hardened build args
89-
global_build_args += activated_hardening_cflags
90-
91103
# Deprecated kconfig entry handling, to be removed on next major release
92104
if kconfig_data.has('CONFIG_TASK_MAGIC_VALUE')
93105
warning('Deprecated kconfig entry CONFIG_TASK_MAGIC_VALUE, please update config file')

0 commit comments

Comments
 (0)