Skip to content

Commit 9f7f6a9

Browse files
Adopt the OpenSSF Compiler Options Hardening Guide for C and C++
1 parent ab7af2b commit 9f7f6a9

File tree

2 files changed

+151
-6
lines changed

2 files changed

+151
-6
lines changed

cmake/cmake-harden.cmake

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Copyright 2025 Holepunch Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# Downloaded on 2026-03-16 from https://github.com/holepunchto/cmake-harden/blob/main/cmake-harden.cmake
16+
# Changelog
17+
# 2026-03-16
18+
# add -Wextra
19+
# add -Wsign-conversion
20+
# add -Wbidi-chars=any
21+
# add -U_FORTIFY_SOURCE
22+
# add -D_FORTIFY_SOURCE=3
23+
# add -D_GLIBCXX_ASSERTIONS
24+
# add -fcf-protection=full
25+
# add -mbranch-protection=standard
26+
# add -Wl,-z,nodlopen
27+
# add -Wl,--as-needed
28+
# add -Wl,--no-copy-dt-needed-entries
29+
# add fexceptions
30+
# add fhardened
31+
32+
include_guard()
33+
34+
include(CheckCCompilerFlag)
35+
include(CheckCXXCompilerFlag)
36+
37+
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
38+
39+
macro(add_hardened_compiler_flags)
40+
foreach(flag ${ARGV})
41+
if(lang MATCHES "CXX")
42+
check_cxx_compiler_flag(${flag} supports_${flag})
43+
else()
44+
check_c_compiler_flag(${flag} supports_${flag})
45+
endif()
46+
47+
if(supports_${flag})
48+
target_compile_options(${target} PRIVATE ${flag})
49+
endif()
50+
endforeach()
51+
endmacro()
52+
53+
macro(add_hardened_linker_flags)
54+
foreach(flag ${ARGV})
55+
target_link_options(${target} PRIVATE ${flag})
56+
endforeach()
57+
endmacro()
58+
59+
macro(harden_posix)
60+
add_hardened_compiler_flags(
61+
-Wall
62+
-Wextra
63+
-Wformat
64+
-Wformat=2
65+
-Wconversion
66+
-Wsign-conversion
67+
-Wimplicit-fallthrough
68+
-Wbidi-chars=any
69+
-Werror=format-security
70+
-Werror=implicit
71+
-Werror=incompatible-pointer-types
72+
-Werror=int-conversion
73+
-U_FORTIFY_SOURCE
74+
-D_FORTIFY_SOURCE=3
75+
-D_GLIBCXX_ASSERTIONS
76+
-fstrict-flex-arrays=3
77+
-fcf-protection=full
78+
-mbranch-protection=standard
79+
-fno-delete-null-pointer-checks
80+
-fno-strict-overflow
81+
-fno-strict-aliasing
82+
-ftrivial-auto-var-init=zero
83+
-fexceptions
84+
-fhardened
85+
)
86+
87+
add_hardened_linker_flags(
88+
-Wl,-z,nodlopen
89+
-Wl,-z,noexecstack
90+
-Wl,-z,relro
91+
-Wl,-z,now
92+
-Wl,--as-needed
93+
-Wl,--no-copy-dt-needed-entries
94+
)
95+
96+
if(runtime)
97+
add_hardened_compiler_flags(
98+
-fstack-clash-protection
99+
-fstack-protector-strong
100+
)
101+
endif()
102+
endmacro()
103+
104+
macro(harden_clang)
105+
harden_posix()
106+
endmacro()
107+
108+
macro(harden_gcc)
109+
harden_posix()
110+
111+
add_hardened_compiler_flags(
112+
-Wtrampolines
113+
)
114+
endmacro()
115+
116+
macro(harden_msvc)
117+
message(WARNING "Compiler hardening is not yet supported for MSVC")
118+
endmacro()
119+
120+
function(harden target)
121+
set(option_keywords
122+
C
123+
CXX
124+
RUNTIME
125+
)
126+
127+
cmake_parse_arguments(
128+
PARSE_ARGV 1 ARGV "${option_keywords}" "" ""
129+
)
130+
131+
set(runtime ${ARGV_RUNTIME})
132+
133+
if(ARGV_CXX)
134+
set(lang CXX)
135+
else()
136+
set(lang C)
137+
endif()
138+
139+
set(compiler ${CMAKE_${lang}_COMPILER_ID})
140+
141+
if(compiler MATCHES "Clang")
142+
harden_clang()
143+
elseif(compiler MATCHES "GCC")
144+
harden_gcc()
145+
elseif(compiler MATCHES "MSVC")
146+
harden_msvc()
147+
endif()
148+
endfunction()

src/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,12 @@ set (EML_PREFIX_2_0 "eml2_0")
4040
set (EML_PREFIX_2_3 "eml2_3")
4141

4242
# Define the compile options according to the compiler
43-
target_compile_options(${CPP_LIBRARY_NAME} PRIVATE
43+
include(${FESAPI_ROOT_DIR}/cmake/cmake-harden.cmake)
44+
harden(${CPP_LIBRARY_NAME} CXX RUNTIME)
45+
target_compile_options(${CPP_LIBRARY_NAME} PRIVATE
4446
$<$<CXX_COMPILER_ID:MSVC>:/bigobj>
4547
$<$<CXX_COMPILER_ID:MSVC>:/MP>
4648
$<$<CXX_COMPILER_ID:MSVC>:/W4>
47-
$<$<CXX_COMPILER_ID:GNU>:-Wall>
48-
$<$<CXX_COMPILER_ID:GNU>:-Wextra>
49-
$<$<CXX_COMPILER_ID:GNU>:-Wcast-qual>
50-
$<$<CXX_COMPILER_ID:GNU>:-pedantic>
51-
$<$<CXX_COMPILER_ID:CLANG>:-Weverything>
5249
)
5350
if (WITH_RESQML2_2)
5451
target_compile_definitions(${CPP_LIBRARY_NAME} PUBLIC "-DWITH_RESQML2_2")

0 commit comments

Comments
 (0)