Skip to content

Commit 5761217

Browse files
committed
Improve CompilerSettings code
- Implement add_legacy_security_settings() to replicate the security settings used in earlier versions of P4 Control Plane. - Rename add_security_settings() to add_recent_security_settings() to provide contrast with the "legacy" settings. - Export CMAKE_POSITION_INDEPENDENT_CODE to the caller's scope, so it will actually take effect. - Add -fstack-protector-strong to the recent security settings. It was inadvertently overlooked. Signed-off-by: Derek G Foster <[email protected]>
1 parent b40a760 commit 5761217

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ cmake_print_variables(WITH_OVSP4RT)
7676
############################
7777

7878
add_basic_settings()
79-
add_security_settings(${CMAKE_BUILD_TYPE})
79+
#add_recent_security_settings(${CMAKE_BUILD_TYPE})
80+
add_legacy_security_settings()
8081

8182
add_compile_options(-D${TARGETFLAG})
8283

cmake/CompilerSettings.cmake

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,30 @@ function(add_basic_settings)
2525
add_link_options("-Wl,-z,now")
2626
endfunction(add_basic_settings)
2727

28-
function(add_security_settings CONFIG)
28+
# Defines the security settings used in earlier versions
29+
# of the software.
30+
function(add_legacy_security_settings)
31+
# Format String Defense
32+
add_compile_options("-Wformat")
33+
add_compile_options("-Wformat-security")
34+
add_compile_options("-Werror=format-security")
35+
36+
# Position Independent Code
37+
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE PARENT_SCOPE)
38+
39+
# Preprocessor Macros
40+
add_compile_options("-D_FORTIFY_SOURCE=2")
41+
42+
# Read-only Relocation
43+
add_link_options("-Wl,-z,relro")
44+
45+
# Stack Protection
46+
add_compile_options("-fstack-protector-strong")
47+
endfunction()
48+
49+
# Defines security settings according to the
50+
# Intel Secure Coding Standards.
51+
function(add_recent_security_settings CONFIG)
2952
string(TOUPPER ${CONFIG} CONFIG)
3053
if(CONFIG STREQUAL "DEBUG")
3154
set(IS_RELEASE FALSE)
@@ -68,7 +91,7 @@ function(add_security_settings CONFIG)
6891
check_and_add_option("-Wl,-z,noexecstack" HAVE_NOEXECSTACK)
6992

7093
# Position Independent Code
71-
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
94+
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE PARENT_SCOPE)
7295

7396
# Position Independent Execution
7497
check_pie_supported(LANGUAGES C CXX)
@@ -82,6 +105,9 @@ function(add_security_settings CONFIG)
82105
# Read-only Relocation
83106
check_and_add_option("-Wl,-z,relro" HAVE_RELRO)
84107

108+
# Stack Protection
109+
add_compile_options("-fstack-protector-strong")
110+
85111
# Spectre Protection
86112
if(IS_RELEASE AND ENABLE_SPECTRE_SETTINGS)
87113
# Mitigating Bounds Check Bypass (Spectre Variant 1)
@@ -95,4 +121,4 @@ function(add_security_settings CONFIG)
95121
# Mitigating Branch Target Injection (Spectre Variant 2)
96122
check_and_add_option("-mretpoline" HAVE_RETPOLINE)
97123
endif()
98-
endfunction(add_security_settings)
124+
endfunction(add_recent_security_settings)

0 commit comments

Comments
 (0)