Skip to content

Commit 3e75c03

Browse files
ceolinkartben
authored andcommitted
security: Add default stack protection level
STACK_CANARIES was enabling canaries in all functions using the compiler flag -fstack-protector-all. This became confuse with the addition of the options STRONG and EXPLICIT. This commit adds the missing option (default level) and disambiguous the options mapping them close to the compiler flags. Now we have the following options: STACK_CANARIES -> fstack-protector STACK_CANARIES_STRONG -> fstack-protector-strong STACK_CANARIES_ALL -> fstack-protector-all STACK_CANARIES_EXPLICIT -> fstack-protector-explicit Note that from now on STACK_CANARIES_ALL is the symbol that adds canaries for all functions. Signed-off-by: Flavio Ceolin <flavio.ceolin@gmail.com>
1 parent 0236f7c commit 3e75c03

6 files changed

+22
-5
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ if(CONFIG_STACK_CANARIES)
177177
zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries>)
178178
elseif(CONFIG_STACK_CANARIES_STRONG)
179179
zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries_strong>)
180+
elseif(CONFIG_STACK_CANARIES_ALL)
181+
zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries_all>)
180182
elseif(CONFIG_STACK_CANARIES_EXPLICIT)
181183
zephyr_compile_options($<TARGET_PROPERTY:compiler,security_canaries_explicit>)
182184
endif()

cmake/compiler/arcmwdt/compiler_flags.cmake

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ set_compiler_property(PROPERTY imacros -imacros)
167167

168168
# Security canaries.
169169
#no support of -mstack-protector-guard=global"
170-
set_compiler_property(PROPERTY security_canaries -fstack-protector-all)
170+
set_compiler_property(PROPERTY security_canaries -fstack-protector)
171171
set_compiler_property(PROPERTY security_canaries_strong -fstack-protector-strong)
172+
set_compiler_property(PROPERTY security_canaries_all -fstack-protector-all)
172173

173174
#no support of _FORTIFY_SOURCE"
174175
set_compiler_property(PROPERTY security_fortify_compile_time)

cmake/compiler/compiler_flags_template.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ set_compiler_property(PROPERTY coverage)
9393
# Security canaries flags.
9494
set_compiler_property(PROPERTY security_canaries)
9595
set_compiler_property(PROPERTY security_canaries_strong)
96+
set_compiler_property(PROPERTY security_canaries_all)
9697
set_compiler_property(PROPERTY security_canaries_explicit)
9798

9899
set_compiler_property(PROPERTY security_fortify_compile_time)

cmake/compiler/gcc/compiler_flags.cmake

+4-1
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,21 @@ set_property(TARGET compiler-cpp PROPERTY no_rtti "-fno-rtti")
167167
set_compiler_property(PROPERTY coverage -fprofile-arcs -ftest-coverage -fno-inline)
168168

169169
# Security canaries.
170-
set_compiler_property(PROPERTY security_canaries -fstack-protector-all)
170+
set_compiler_property(PROPERTY security_canaries -fstack-protector)
171171
set_compiler_property(PROPERTY security_canaries_strong -fstack-protector-strong)
172+
set_compiler_property(PROPERTY security_canaries_all -fstack-protector-all)
172173
set_compiler_property(PROPERTY security_canaries_explicit -fstack-protector-explicit)
173174

174175
# Only a valid option with GCC 7.x and above, so let's do check and set.
175176
if(CONFIG_STACK_CANARIES_TLS)
176177
check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=tls)
177178
check_set_compiler_property(APPEND PROPERTY security_canaries_strong -mstack-protector-guard=tls)
179+
check_set_compiler_property(APPEND PROPERTY security_canaries_all -mstack-protector-guard=tls)
178180
check_set_compiler_property(APPEND PROPERTY security_canaries_explicit -mstack-protector-guard=tls)
179181
else()
180182
check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=global)
181183
check_set_compiler_property(APPEND PROPERTY security_canaries_global -mstack-protector-guard=global)
184+
check_set_compiler_property(APPEND PROPERTY security_canaries_all -mstack-protector-guard=global)
182185
check_set_compiler_property(APPEND PROPERTY security_canaries_explicit -mstack-protector-guard=global)
183186
endif()
184187

kernel/Kconfig

+12-2
Original file line numberDiff line numberDiff line change
@@ -890,12 +890,14 @@ choice
890890
will occur at build time.
891891

892892
config STACK_CANARIES
893-
bool "Maximum protection available"
893+
bool "Default protection"
894894
depends on ENTROPY_GENERATOR || TEST_RANDOM_GENERATOR
895895
select NEED_LIBC_MEM_PARTITION if !STACK_CANARIES_TLS
896896
select REQUIRES_STACK_CANARIES
897897
help
898-
This option enables compiler stack canaries for all functions.
898+
This option enables compiler stack canaries in functions that have
899+
vulnerable objects. Generally this means function that call alloca or
900+
have buffers larger than 8 bytes.
899901

900902
config STACK_CANARIES_STRONG
901903
bool "Strong protection"
@@ -907,6 +909,14 @@ config STACK_CANARIES_STRONG
907909
functions that have local array definitiion or have references to local
908910
frame addresses.
909911

912+
config STACK_CANARIES_ALL
913+
bool "Maximum protection available"
914+
depends on ENTROPY_GENERATOR || TEST_RANDOM_GENERATOR
915+
select NEED_LIBC_MEM_PARTITION if !STACK_CANARIES_TLS
916+
select REQUIRES_STACK_CANARIES
917+
help
918+
This option enables compiler stack canaries for all functions.
919+
910920
config STACK_CANARIES_EXPLICIT
911921
bool "Explicit protection"
912922
depends on ENTROPY_GENERATOR || TEST_RANDOM_GENERATOR

kernel/compiler_stack_protect.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* This module provides functions to support compiler stack protection
1212
* using canaries. This feature is enabled with configuration
1313
* CONFIG_STACK_CANARIES=y or CONFIG_STACK_CANARIES_STRONG=y or
14-
* CONFIG_STACK_CANARIES_EXPLICIT=y.
14+
* CONFIG_STACK_CANARIES_ALL=y or CONFIG_STACK_CANARIES_EXPLICIT=y.
1515
*
1616
* When this feature is enabled, the compiler generated code refers to
1717
* function __stack_chk_fail and global variable __stack_chk_guard.

0 commit comments

Comments
 (0)