Skip to content

Commit 63b3ff1

Browse files
committed
Add optional support for Position Independent Code and stack smashing protection plus source fortify. This is needed for Guix portability, when exporting the tarball to Alpine, the musl or ulibc does not support it.
1 parent cf0c6f1 commit 63b3ff1

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ option(OPTION_BUILD_SCRIPTS "Build scripts." ON)
8282
option(OPTION_BUILD_SERIALS "Build serials." ON)
8383
option(OPTION_BUILD_DETOURS "Build detours." ON)
8484
option(OPTION_BUILD_PORTS "Build ports." OFF)
85+
option(OPTION_BUILD_PIC "Build with position independent code." ON)
86+
option(OPTION_BUILD_SECURITY "Build with stack-smashing protection and source fortify." ON)
8587
option(OPTION_FORK_SAFE "Enable fork safety." ON)
8688
option(OPTION_THREAD_SAFE "Enable thread safety." OFF)
8789
option(OPTION_COVERAGE "Enable coverage." OFF)

cmake/SecurityFlags.cmake

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,45 @@ include(CheckCCompilerFlagStackSmashing)
2222

2323
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
2424

25-
# Detect position independent code flag
26-
check_c_compiler_flag("-fPIC" PIC_C_FLAG)
25+
if(OPTION_BUILD_PIC)
26+
# Detect position independent code flag
27+
check_c_compiler_flag("-fPIC" PIC_C_FLAG)
2728

28-
if(PIC_C_FLAG)
29-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
29+
if(PIC_C_FLAG)
30+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
31+
endif()
3032
endif()
3133

32-
# Detect stack protector
33-
check_c_compiler_flag_stack_smashing("-fstack-protector-strong" STACK_PROTECTOR_STRONG_C_FLAG)
34-
35-
if(STACK_PROTECTOR_STRONG_C_FLAG)
36-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong")
34+
if(OPTION_BUILD_SECURITY)
35+
# Detect stack protector
36+
check_c_compiler_flag_stack_smashing("-fstack-protector-strong" STACK_PROTECTOR_STRONG_C_FLAG)
3737

38-
# use ssp-buffer-size if it is supported
39-
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
40-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --param ssp-buffer-size=4")
41-
endif()
42-
else()
43-
check_c_compiler_flag_stack_smashing("-fstack-protector" STACK_PROTECTOR_CXX_FLAG)
44-
45-
if(STACK_PROTECTOR_C_FLAG)
46-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
38+
if(STACK_PROTECTOR_STRONG_C_FLAG)
39+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong")
4740

4841
# use ssp-buffer-size if it is supported
4942
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
5043
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --param ssp-buffer-size=4")
5144
endif()
45+
else()
46+
check_c_compiler_flag_stack_smashing("-fstack-protector" STACK_PROTECTOR_CXX_FLAG)
47+
48+
if(STACK_PROTECTOR_C_FLAG)
49+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
50+
51+
# use ssp-buffer-size if it is supported
52+
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
53+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --param ssp-buffer-size=4")
54+
endif()
55+
endif()
5256
endif()
53-
endif()
5457

55-
# Detect fortify source
56-
check_c_compiler_flag("-D_FORTIFY_SOURCE=2" FORTIFY_SOURCE_C_FLAG)
58+
# Detect fortify source
59+
check_c_compiler_flag("-D_FORTIFY_SOURCE=2" FORTIFY_SOURCE_C_FLAG)
5760

58-
if(FORTIFY_SOURCE_C_FLAG)
59-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -D_FORTIFY_SOURCE=2")
61+
if(FORTIFY_SOURCE_C_FLAG)
62+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -D_FORTIFY_SOURCE=2")
63+
endif()
6064
endif()
6165

6266
endif()
@@ -66,41 +70,45 @@ include(CheckCXXCompilerFlagStackSmashing)
6670

6771
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
6872

69-
# Detect position independent code flag
70-
check_cxx_compiler_flag("-fPIC" PIC_CXX_FLAG)
73+
if(OPTION_BUILD_PIC)
74+
# Detect position independent code flag
75+
check_cxx_compiler_flag("-fPIC" PIC_CXX_FLAG)
7176

72-
if(PIC_CXX_FLAG)
73-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
77+
if(PIC_CXX_FLAG)
78+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
79+
endif()
7480
endif()
7581

76-
# Detect stack protector
77-
check_cxx_compiler_flag_stack_smashing("-fstack-protector-strong" STACK_PROTECTOR_STRONG_CXX_FLAG)
78-
79-
if(STACK_PROTECTOR_STRONG_CXX_FLAG)
80-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-strong")
82+
if(OPTION_BUILD_SECURITY)
83+
# Detect stack protector
84+
check_cxx_compiler_flag_stack_smashing("-fstack-protector-strong" STACK_PROTECTOR_STRONG_CXX_FLAG)
8185

82-
# use ssp-buffer-size if it is supported
83-
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
84-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --param ssp-buffer-size=4")
85-
endif()
86-
else()
87-
check_cxx_compiler_flag_stack_smashing("-fstack-protector" STACK_PROTECTOR_CXX_FLAG)
88-
89-
if(STACK_PROTECTOR_CXX_FLAG)
90-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
86+
if(STACK_PROTECTOR_STRONG_CXX_FLAG)
87+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-strong")
9188

9289
# use ssp-buffer-size if it is supported
9390
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
9491
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --param ssp-buffer-size=4")
9592
endif()
93+
else()
94+
check_cxx_compiler_flag_stack_smashing("-fstack-protector" STACK_PROTECTOR_CXX_FLAG)
95+
96+
if(STACK_PROTECTOR_CXX_FLAG)
97+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
98+
99+
# use ssp-buffer-size if it is supported
100+
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
101+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --param ssp-buffer-size=4")
102+
endif()
103+
endif()
96104
endif()
97-
endif()
98105

99-
# Detect fortify source
100-
check_cxx_compiler_flag("-D_FORTIFY_SOURCE=2" FORTIFY_SOURCE_CXX_FLAG)
106+
# Detect fortify source
107+
check_cxx_compiler_flag("-D_FORTIFY_SOURCE=2" FORTIFY_SOURCE_CXX_FLAG)
101108

102-
if(FORTIFY_SOURCE_CXX_FLAG)
103-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -D_FORTIFY_SOURCE=2")
109+
if(FORTIFY_SOURCE_CXX_FLAG)
110+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -D_FORTIFY_SOURCE=2")
111+
endif()
104112
endif()
105113

106114
endif()

0 commit comments

Comments
 (0)