Skip to content

Commit d5f91eb

Browse files
committed
Improved hardening
1 parent be1a8ee commit d5f91eb

File tree

3 files changed

+197
-6
lines changed

3 files changed

+197
-6
lines changed

CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
cmake_minimum_required(VERSION 3.1) # for "CMAKE_CXX_STANDARD" version
88
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # custom CMake modules like FindSQLiteCpp
99
project(SQLiteCpp VERSION 3.0.0)
10+
include(Hardening)
1011

1112
# SQLiteC++ 3.x requires C++11 features
1213
if (NOT CMAKE_CXX_STANDARD)
@@ -197,16 +198,11 @@ if (SQLITE_USE_LEGACY_STRUCT)
197198
target_compile_definitions(SQLiteCpp PUBLIC SQLITE_USE_LEGACY_STRUCT)
198199
endif (SQLITE_USE_LEGACY_STRUCT)
199200

200-
if (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"))
201-
set_target_properties(SQLiteCpp PROPERTIES COMPILE_FLAGS "-fPIC")
202-
endif (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"))
201+
harden(SQLiteCpp)
203202

204203
option(SQLITECPP_USE_ASAN "Use Address Sanitizer." OFF)
205204
if (SQLITECPP_USE_ASAN)
206205
if ((CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL 6) OR ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang"))
207-
message (STATUS "Using Address Sanitizer")
208-
set_target_properties(SQLiteCpp PROPERTIES COMPILE_FLAGS "-fsanitize=address -fno-omit-frame-pointer")
209-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
210206
if (CMAKE_COMPILER_IS_GNUCXX)
211207
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
212208
endif ()
@@ -329,6 +325,7 @@ option(SQLITECPP_BUILD_EXAMPLES "Build examples." OFF)
329325
if (SQLITECPP_BUILD_EXAMPLES)
330326
# add the basic example executable
331327
add_executable(SQLiteCpp_example1 ${SQLITECPP_EXAMPLES})
328+
harden(SQLiteCpp_example1)
332329
target_link_libraries(SQLiteCpp_example1 SQLiteCpp)
333330
target_include_directories(SQLiteCpp_example1 PRIVATE
334331
${CMAKE_CURRENT_SOURCE_DIR}/include
@@ -344,6 +341,7 @@ option(SQLITECPP_BUILD_TESTS "Build and run tests." OFF)
344341
if (SQLITECPP_BUILD_TESTS)
345342
# add the unit test executable
346343
add_executable(SQLiteCpp_tests ${SQLITECPP_TESTS})
344+
harden(SQLiteCpp_tests)
347345
target_link_libraries(SQLiteCpp_tests SQLiteCpp)
348346
target_include_directories(SQLiteCpp_tests PRIVATE
349347
${CMAKE_CURRENT_SOURCE_DIR}/include

cmake/Hardening.cmake

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#This is free and unencumbered software released into the public domain.
2+
#Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
3+
#In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
4+
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5+
#For more information, please refer to <https://unlicense.org/>
6+
7+
include(CheckCXXCompilerFlag)
8+
9+
function(determineSupportedHardeningFlags property)
10+
set(FLAGS_HARDENING "")
11+
foreach(flag ${ARGN})
12+
unset(var_name)
13+
string(REPLACE "=" "_eq_" var_name ${flag})
14+
string(REPLACE "," "_comma_" var_name ${var_name})
15+
set(var_name "SUPPORTS_HARDENING_${property}_${var_name}")
16+
check_cxx_compiler_flag(${flag} ${var_name})#since linker flags and other flags are in the form of compiler flags
17+
if(${${var_name}})
18+
list(APPEND FLAGS_HARDENING "${flag}")
19+
endif()
20+
endforeach(flag)
21+
list(JOIN FLAGS_HARDENING " " FLAGS_HARDENING)
22+
#message(STATUS "FLAGS_HARDENING ${FLAGS_HARDENING}")
23+
set(HARDENING_${property} "${FLAGS_HARDENING}" CACHE STRING "Hardening flags")
24+
endfunction(determineSupportedHardeningFlags)
25+
26+
function(processFlagsList target property)
27+
get_target_property(FLAGS_UNHARDENED ${target} ${property})
28+
if(FLAGS_UNHARDENED MATCHES "FLAGS_UNHARDENED-NOTFOUND")
29+
set(FLAGS_UNHARDENED "")
30+
endif()
31+
#message(STATUS "processFlagsList ${target} ${property} ${FLAGS_UNHARDENED}")
32+
#message(STATUS "HARDENING_${property} ${HARDENING_${property}}")
33+
if(HARDENING_${property})
34+
else()
35+
determineSupportedHardeningFlags(${property} ${ARGN})
36+
endif()
37+
38+
set(FLAGS_HARDENED ${FLAGS_UNHARDENED})
39+
list(APPEND FLAGS_HARDENED ${HARDENING_${property}})
40+
list(JOIN FLAGS_HARDENED " " FLAGS_HARDENED)
41+
#message(STATUS "${target} PROPERTIES ${property} ${FLAGS_HARDENED}")
42+
set_target_properties(${target} PROPERTIES ${property} "${FLAGS_HARDENED}")
43+
endfunction(processFlagsList)
44+
45+
function(setupPIC target)
46+
set_property(TARGET ${target} PROPERTY POSITION_INDEPENDENT_CODE ON) # FUCK, doesn't work
47+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
48+
get_target_property(type ${target} TYPE)
49+
if(type STREQUAL "EXECUTABLE")
50+
list(APPEND HARDENING_COMPILER_FLAGS
51+
"-fPIE"
52+
)
53+
else()
54+
list(APPEND HARDENING_COMPILER_FLAGS
55+
"-fPIC"
56+
)
57+
endif()
58+
list(APPEND HARDENING_LINKER_FLAGS
59+
"-pie"
60+
)
61+
elseif(MSVC)
62+
list(APPEND HARDENING_COMPILER_FLAGS
63+
"/dynamicbase" "/HIGHENTROPYVA"
64+
)
65+
else()
66+
message(ERROR "The compiler is not supported")
67+
endif()
68+
endfunction(setupPIC)
69+
70+
function(harden target)
71+
setupPIC("${target}")
72+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
73+
list(APPEND HARDENING_COMPILER_FLAGS
74+
"-Wall" "-Wextra" "-Wconversion" "-Wformat" "-Wformat-security" "-Werror=format-security"
75+
"-fno-strict-aliasing" "-fno-common"
76+
#"-fstack-check"
77+
#"-fcf-protection=full" # conflicts to "-mindirect-branch"
78+
"-fcf-runtime-abi=full"
79+
80+
"-fstack-clash-protection"
81+
"-mcet"
82+
"-fsanitize=cfi"
83+
"-fsanitize=cfi-cast-strict"
84+
"-fsanitize=cfi-derived-cast"
85+
"-fsanitize=cfi-unrelated-cast"
86+
"-fsanitize=cfi-nvcall"
87+
"-fsanitize=cfi-vcall"
88+
"-fsanitize=cfi-icall"
89+
"-fsanitize=cfi-mfcall"
90+
91+
# CLang-ish flags
92+
"-mretpoline"
93+
"-mspeculative-load-hardening"
94+
95+
96+
#"-fsanitize=safe-stack;compiler-rt" # https://clang.llvm.org/docs/SafeStack.html
97+
"-fsanitize=address" # https://clang.llvm.org/docs/AddressSanitizer.html
98+
99+
# TODO implement compiler flag dependence on libs linking
100+
#"-fsanitize=undefined;ubsan" # https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html , gcc also has it, but in order to use this lib one has to link it explicitly by its location (-L/usr/lib/llvm-11/lib/clang/11.0.0/lib/linux/ -l:libclang_rt.ubsan_standalone-x86_64.so)
101+
102+
#"-fsanitize=thread" # https://clang.llvm.org/docs/ThreadSanitizer.html , 15x slowdown and 10x memory overhead
103+
#"-fsanitize=memory" # https://clang.llvm.org/docs/MemorySanitizer.html 3x slowdown
104+
#"-fsanitize=dataflow" # https://clang.llvm.org/docs/DataFlowSanitizer.html, taint analysis, requires explicit annotation of code
105+
106+
#"-fvtable-verify=std;vtv" # the lib is not present in packages for Ubuntu, even in in nightly ones
107+
108+
# this conflicts with gcc which now has -fcf-protection=full hardcoded
109+
"-fcf-protection=none -mindirect-branch"
110+
"-fcf-protection=none -mindirect-branch=thunk-extern"
111+
"-fcf-protection=none -mindirect-branch=thunk-inline"
112+
"-fcf-protection=none -mindirect-return"
113+
"-fcf-protection=none -mindirect-branch-register"
114+
"-fcf-protection=none -mindirect-branch-loop"
115+
116+
"-x86-speculative-load-hardening"
117+
"-mno-indirect-branch-register"
118+
)
119+
120+
# some flags are bugged in GCC
121+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
122+
else()
123+
list(APPEND HARDENING_COMPILER_FLAGS
124+
"-ftrapv" # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=35412
125+
)
126+
endif()
127+
128+
# GCC 9 has removed these flags
129+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 9 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10))
130+
message(STATUS "GCC 9 removes some hardening flags but doesn't fail if they are present, instead shows deprecation message. In order to not to put garbage into warnings we don't insert them. See the code of Harden.cmake for the details.")
131+
else()
132+
list(APPEND HARDENING_COMPILER_FLAGS
133+
"-mmitigate-rop"
134+
"-fcheck-pointer-bounds"
135+
"-fchkp-treat-zero-size-reloc-as-infinite"
136+
"-fchkp-first-field-has-own-bounds"
137+
"-fchkp-narrow-bounds"
138+
"-fchkp-narrow-to-innermost-array"
139+
"-fchkp-optimize"
140+
"-fchkp-use-fast-string-functions"
141+
"-fchkp-use-nochk-string-functions"
142+
"-fchkp-use-static-const-bounds"
143+
)
144+
endif()
145+
146+
list(APPEND HARDENING_LINKER_FLAGS
147+
"-Wl,-O1"
148+
"-Wl,--sort-common"
149+
"-Wl,--as-needed"
150+
"-Wl,-flto"
151+
)
152+
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
153+
list(APPEND HARDENING_LINKER_FLAGS
154+
"-Wl,--export-all-symbols"
155+
"-Wl,--nxcompat"
156+
"-Wl,--dynamicbase"
157+
)
158+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
159+
list(APPEND HARDENING_LINKER_FLAGS "-Wl,--image-base,0x140000000")
160+
endif()
161+
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux") # other using ELF too?
162+
list(APPEND HARDENING_COMPILER_FLAGS
163+
# on MinGW hello world works, but more complex things just exit without any output or crash in the middle of execution
164+
"-fstack-protector"
165+
"-fstack-protector-strong"
166+
)
167+
list(APPEND HARDENING_LINKER_FLAGS
168+
# not present in MinGW
169+
"-Wl,-z,relro"
170+
"-Wl,-z,now"
171+
#"-Wl,-z,ibtplt" missing in gold
172+
"-Wl,-z,ibt"
173+
"-Wl,-z,shstk"
174+
)
175+
endif()
176+
list(APPEND HARDENING_MACRODEFS
177+
"-D_FORTIFY_SOURCE=2"
178+
"-D_GLIBCXX_ASSERTIONS"
179+
)
180+
elseif(MSVC)
181+
set(HARDENING_COMPILER_FLAGS "/sdl" "/GS" "/SafeSEH" "/guard:cf" "/HIGHENTROPYVA")
182+
set(HARDENING_LINKER_FLAGS "/guard:cf")
183+
else()
184+
message(ERROR "The compiler is not supported")
185+
endif()
186+
187+
processFlagsList(${target} COMPILE_FLAGS ${HARDENING_COMPILER_FLAGS})
188+
processFlagsList(${target} LINK_FLAGS ${HARDENING_LINKER_FLAGS})
189+
190+
set(HARDENING_MACRODEFS "${HARDENING_MACRODEFS}" CACHE STRING "Hardening flags CMake list (not string!)")
191+
target_compile_definitions(${target} PRIVATE ${HARDENING_MACRODEFS})
192+
endfunction(harden)

sqlite3/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(sqlite3
1010
sqlite3.c
1111
sqlite3.h
1212
)
13+
harden(sqlite3)
1314

1415
target_include_directories(sqlite3
1516
PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>

0 commit comments

Comments
 (0)