Skip to content

Commit 6a2327f

Browse files
committed
Add compiler-specific hardening flags for GCC and Clang
1 parent 74f6802 commit 6a2327f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,40 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
111111
endforeach()
112112
endif()
113113

114+
# Add compiler-specific hardening flags.
115+
if(CMAKE_CXX_COMPILER_ID MATCHES "^(Clang|GNU)$")
116+
add_compile_options(
117+
# Warn about potentially unsafe code.
118+
-Wall
119+
# Warn about implicit conversions that potentially alter a value.
120+
-Wconversion
121+
# Check argument types of format string function calls, e.g., printf.
122+
-Wformat
123+
# Check for potential security issues in format string function calls.
124+
-Wformat-security
125+
# Revert strict aliasing enabled at optimization levels -O2, -O3, -Os.
126+
-fno-strict-aliasing
127+
# Check for buffer overflows such as stack smashing attacks.
128+
-fstack-protector
129+
# Enable fortified wrappers of GNU C library functions.
130+
-D_FORTIFY_SOURCE=2
131+
)
132+
133+
# We need to support CMake 3.10, add_link_options() was added in CMake 3.13.
134+
# link_libraries() passes flags through as long as they do not contain spaces.
135+
# https://cmake.org/cmake/help/v3.13/command/add_link_options.html
136+
link_libraries(
137+
# Check objects for unresolved symbol references.
138+
-Wl,--no-undefined
139+
# Mark library as not requiring executable stack.
140+
-Wl,-z,noexecstack
141+
# Resolve all symbols when program is started, instead of on first use.
142+
-Wl,-z,now
143+
# Mark Global Offset Table read-only after resolving symbols.
144+
-Wl,-z,relro
145+
)
146+
endif()
147+
114148
# https://clang.llvm.org/docs/AddressSanitizer.html
115149
option(ACL_WITH_ASAN "Build with address sanitizer" OFF)
116150
message(STATUS "Build with address sanitizer: ${ACL_WITH_ASAN}")

0 commit comments

Comments
 (0)