Skip to content

Commit fbf4e14

Browse files
committed
Use modern linkage/flags and drop duplicate/obsolete ones
1 parent 9ebd4af commit fbf4e14

1 file changed

Lines changed: 69 additions & 88 deletions

File tree

src/C/CMakeLists.txt

Lines changed: 69 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ include(GNUInstallDirs)
1717
if (NOT CLIENT_KEYTAB_DIR)
1818
set(CLIENT_KEYTAB_DIR /var/kerberos/krb5/user)
1919
cmake_print_variables(CLIENT_KEYTAB_DIR)
20-
endif (NOT CLIENT_KEYTAB_DIR)
20+
endif(NOT CLIENT_KEYTAB_DIR)
2121

2222
if (NOT FILE_PATH_MAX_LENGTH)
2323
set(FILE_PATH_MAX_LENGTH 4096)
2424
cmake_print_variables(FILE_PATH_MAX_LENGTH)
25-
endif (NOT FILE_PATH_MAX_LENGTH)
25+
endif(NOT FILE_PATH_MAX_LENGTH)
2626

2727
#############################
2828
# C Language Configuration
@@ -36,7 +36,7 @@ set(CMAKE_C_EXTENSIONS ON)
3636
CHECK_C_SOURCE_COMPILES("int main(void) { return 0; } " CAN_COMPILE)
3737
if (NOT CAN_COMPILE)
3838
message(FATAL_ERROR "C compiler is non-functional")
39-
endif (NOT CAN_COMPILE)
39+
endif(NOT CAN_COMPILE)
4040

4141
message(STATUS "C Compiler: ${CMAKE_C_COMPILER}")
4242
message(STATUS "C Standard: C${CMAKE_C_STANDARD}")
@@ -48,7 +48,7 @@ message(STATUS "Supported C features: ${CMAKE_C_COMPILE_FEATURES}")
4848
CHECK_INCLUDE_FILE(sys/capability.h HAVE_CAPABILITIES_H)
4949
if (NOT HAVE_CAPABILITIES_H)
5050
message(FATAL_ERROR "sys/capability.h is REQUIRED - install libcap-dev or libcap-devel")
51-
endif (NOT HAVE_CAPABILITIES_H)
51+
endif(NOT HAVE_CAPABILITIES_H)
5252

5353
#############################
5454
# Optional Security Features
@@ -59,8 +59,8 @@ if (USE_LANDLOCK)
5959
if (NOT HAVE_LANDLOCK_H)
6060
message(FATAL_ERROR "linux/landlock.h not found - landlock support requested")
6161
set(USE_LANDLOCK FALSE)
62-
endif (NOT HAVE_LANDLOCK_H)
63-
endif (USE_LANDLOCK)
62+
endif(NOT HAVE_LANDLOCK_H)
63+
endif(USE_LANDLOCK)
6464
add_feature_info(WITH_LANDLOCK USE_LANDLOCK "Use landlock to reduce privilege exposure")
6565

6666
option (USE_SECCOMP "Add seccomp filters for binaries" TRUE)
@@ -69,8 +69,8 @@ if (USE_SECCOMP)
6969
if (NOT HAVE_SECCOMP_H)
7070
message(FATAL_ERROR "seccomp.h not found - seccomp support requested")
7171
set(USE_SECCOMP FALSE)
72-
endif (NOT HAVE_SECCOMP_H)
73-
endif (USE_SECCOMP)
72+
endif(NOT HAVE_SECCOMP_H)
73+
endif(USE_SECCOMP)
7474
add_feature_info(WITH_SECCOMP USE_SECCOMP "Add seccomp filters for binaries")
7575

7676
#############################
@@ -128,7 +128,7 @@ add_compile_options(
128128
# Additional hardening for formats
129129
add_compile_options(-D_GLIBCXX_ASSERTIONS)
130130

131-
# Additional warnings for GCC (not supported by all compilers)
131+
# Additional GCC features (not supported by all compilers)
132132
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
133133
add_compile_options(
134134
-Wlogical-op # Suspicious logical operations
@@ -137,20 +137,36 @@ if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
137137
-Wnull-dereference # Potential NULL dereferences
138138
-Wjump-misses-init # Jump skips variable initialization
139139
)
140-
endif()
140+
141+
check_c_compiler_flag(-fplugin=annobin SET_ANNOBIN)
142+
if(SET_ANNOBIN)
143+
add_compile_options(-fplugin=annobin)
144+
message(STATUS "GCC Annobin plugin: enabled")
145+
else()
146+
message(WARNING "GCC Annobin plugin not available")
147+
endif(SET_ANNOBIN)
148+
149+
check_c_compiler_flag(-grecord-gcc-switches SET_GCC_SWITCHES)
150+
if(SET_GCC_SWITCHES)
151+
add_compile_options(-grecord-gcc-switches)
152+
message(STATUS "Recording GCC switches: enabled")
153+
else()
154+
message(WARNING "Cannot record GCC switches in binary")
155+
endif(SET_GCC_SWITCHES)
156+
endif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
141157

142158
#############################
143-
# Stack Unwinding Support (for debugging)
159+
# Stack Unwinding Support (required for debugging)
144160
CHECK_C_COMPILER_FLAG(-fasynchronous-unwind-tables UNWIND_TABLES)
145161
if (UNWIND_TABLES)
146162
add_compile_options(-fasynchronous-unwind-tables)
147163
message(STATUS "Unwind tables: enabled")
148164
else ()
149165
message(WARNING "Compiler does not support -fasynchronous-unwind-tables")
150-
endif (UNWIND_TABLES)
166+
endif(UNWIND_TABLES)
151167

152168
#############################
153-
# Stack Frame Pointer Support (for debugging)
169+
# Stack Frame Pointer Support (required for debugging)
154170
CHECK_C_COMPILER_FLAG(-fno-omit-frame-pointer FRAME_POINTER)
155171
if (FRAME_POINTER)
156172
add_compile_options(-fno-omit-frame-pointer)
@@ -164,42 +180,24 @@ endif(FRAME_POINTER)
164180
CHECK_C_COMPILER_FLAG(-finline-functions INLINE_FUNCTIONS)
165181
if (INLINE_FUNCTIONS)
166182
add_compile_options(-finline-functions)
167-
message(STATUS "Inline functions: enabled")
183+
message(STATUS "Aggressibe inline functions: enabled")
168184
else ()
169185
message(WARNING "Compiler does not support -finline-functions")
170-
endif (INLINE_FUNCTIONS)
171-
172-
#############################
173-
# Binary Annotation (GCC-specific)
174-
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
175-
CHECK_C_COMPILER_FLAG(-fplugin=annobin SET_ANNOBIN)
176-
if (SET_ANNOBIN)
177-
add_compile_options(-fplugin=annobin)
178-
message(STATUS "Annobin plugin: enabled")
179-
else ()
180-
message(WARNING "Annobin plugin not available")
181-
endif (SET_ANNOBIN)
182-
183-
CHECK_C_COMPILER_FLAG(-grecord-gcc-switches SET_GCC_SWITCHES)
184-
if (SET_GCC_SWITCHES)
185-
add_compile_options(-grecord-gcc-switches)
186-
message(STATUS "Recording GCC switches: enabled")
187-
else ()
188-
message(WARNING "Cannot record GCC switches in binary")
189-
endif (SET_GCC_SWITCHES)
190-
endif (CMAKE_C_COMPILER_ID STREQUAL "GNU")
186+
endif(INLINE_FUNCTIONS)
191187

192188
#############################
193189
# Linker Hardening Flags
194190
# These flags prevent various exploit techniques
195191
# -Wl,-z,defs -- doesn't work with modern RHEL
196192
add_link_options(
193+
-Wl,--as-needed # Only link used symbols
197194
-Wl,-z,noexecstack # Mark stack as non-executable
198195
-Wl,-z,nodump # Prevent dumping
199196
-Wl,-z,relro # Read-only relocations
200197
-Wl,-z,now # Resolve all symbols at load time
201198
-Wl,-z,combreloc # Combine relocation sections
202-
-Wl,--as-needed # Only link used symbols
199+
-Wl,-z,ibt # Fail at runtime if ibt fails
200+
-Wl,-z,shstk # Fail at runtime if no shadow stack
203201
)
204202

205203
#############################
@@ -211,74 +209,57 @@ if (NOT CMAKE_BUILD_TYPE STREQUAL "DEBUG")
211209
message(STATUS "Fortify source: enabled")
212210
else()
213211
message(STATUS "Fortify source: disabled (debug build)")
214-
endif ()
212+
endif(NOT CMAKE_BUILD_TYPE STREQUAL "DEBUG")
215213

216-
# Stack Smashing Protection
217-
CHECK_C_COMPILER_FLAG(-fstack-protector-buffer-size=4 SSP_BUFFER_SIZE)
218-
if (SSP_BUFFER_SIZE)
219-
add_compile_options(-fstack-protector-buffer-size=4)
220-
message(STATUS "Stack protector buffer size: set to 4")
214+
# Stack protector: ALL
215+
check_c_compiler_flag(-fstack-protector-all HAVE_SSP_ALL)
216+
if (HAVE_SSP_ALL)
217+
add_compile_options(-fstack-protector-all)
221218
else()
222-
message(WARNING "Compiler does not support -fstack-protector-buffer-size=4")
223-
endif(SSP_BUFFER_SIZE)
219+
message(FATAL_ERROR "Required flag -fstack-protector-all not supported")
220+
endif(HAVE_SSP_ALL)
224221

222+
# Stack clash protection
225223
CHECK_C_COMPILER_FLAG(-fstack-clash-protection STACK_CLASH)
226224
if (STACK_CLASH)
227225
add_compile_options(-fstack-clash-protection)
228226
message(STATUS "Stack clash protection: enabled")
229227
else ()
230228
message(WARNING "Stack clash protection not supported by compiler")
231-
endif (STACK_CLASH)
229+
endif(STACK_CLASH)
232230

231+
# Disable stack slot reuse
233232
CHECK_C_COMPILER_FLAG(-fstack-reuse=none STACK_REUSE)
234233
if (STACK_REUSE)
235234
add_compile_options(-fstack-reuse=none)
236235
else ()
237236
message(FATAL_ERROR "Required compiler flag '-fstack-reuse=none' not supported")
238-
endif (STACK_REUSE)
239-
240-
CHECK_C_COMPILER_FLAG(-fstack-protector-all STACK_PROTECT)
241-
if (STACK_PROTECT)
242-
add_compile_options(-fstack-protector-all)
243-
else ()
244-
message(FATAL_ERROR "Required compiler flag '-fstack-protector-all' not supported")
245-
endif (STACK_PROTECT)
246-
247-
# Control Flow Enforcement Technology (Intel CET)
248-
CHECK_C_COMPILER_FLAG(-fcf-protection=full FCF_PROTECT)
249-
if (FCF_PROTECT)
250-
add_compile_options(-fcf-protection=full)
251-
message(STATUS "Control flow protection: enabled")
252-
else ()
253-
message(WARNING "Control flow protection not supported by compiler")
254-
endif (FCF_PROTECT)
255-
256-
# Branch Target Identification (ARM BTI)
257-
CHECK_C_COMPILER_FLAG(-mbranch-protection=standard BRANCH_PROTECT)
258-
if (BRANCH_PROTECT)
259-
add_compile_options(-mbranch-protection=standard)
260-
message(STATUS "Branch protection (ARM): enabled")
261-
else ()
262-
message(STATUS "Branch protection (ARM): not applicable for this architecture")
263-
endif (BRANCH_PROTECT)
237+
endif(STACK_REUSE)
264238

265-
# Zero Call-Used Registers on Return
266-
CHECK_C_COMPILER_FLAG(-fzero-call-used-regs=all ZERO_CALL_REGS)
267-
if (ZERO_CALL_REGS)
239+
# Zero call-used registers on return
240+
check_c_compiler_flag(-fzero-call-used-regs=all HAVE_ZERO_CALL_REGS)
241+
if (HAVE_ZERO_CALL_REGS)
268242
add_compile_options(-fzero-call-used-regs=all)
269-
message(STATUS "Zero call-used registers: enabled")
270-
else ()
271-
message(WARNING "Zero call-used registers not supported by compiler")
272-
endif (ZERO_CALL_REGS)
273-
274-
# Shadow Stack (Intel CET-SS)
275-
CHECK_C_COMPILER_FLAG(-mshstk SAFESTACK_SHADOW)
276-
if (SAFESTACK_SHADOW)
277-
add_compile_options(-mshstk)
278-
message(STATUS "Shadow stack: enabled")
279-
else ()
280-
message(STATUS "Shadow stack: not supported by this CPU/compiler")
281-
endif (SAFESTACK_SHADOW)
243+
endif(HAVE_ZERO_CALL_REGS)
244+
245+
# Control-flow enforcement
246+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
247+
# x86_64
248+
check_c_compiler_flag(-fcf-protection=full HAVE_CET)
249+
if (HAVE_CET)
250+
add_compile_options(-fcf-protection=full)
251+
else()
252+
message(FATAL_ERROR "Intel CET (-fcf-protection=full) required on x86_64")
253+
endif(HAVE_CET)
254+
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
255+
# ARM
256+
check_c_compiler_flag(-mbranch-protection=standard HAVE_BTI)
257+
if (HAVE_BTI)
258+
add_compile_options(-mbranch-protection=standard)
259+
else()
260+
message(FATAL_ERROR "Branch Protection (-mbranch-protection=standard) required on aarch64")
261+
endif(HAVE_BTI)
262+
endif()
282263

283264
#############################
284265
# Build Targets
@@ -316,7 +297,7 @@ target_link_libraries(init-kcron-keytab PRIVATE cap)
316297

317298
if (USE_SECCOMP)
318299
target_link_libraries(init-kcron-keytab PRIVATE seccomp)
319-
endif (USE_SECCOMP)
300+
endif(USE_SECCOMP)
320301

321302
#############################
322303
# client-keytab-name Target Configuration

0 commit comments

Comments
 (0)