Skip to content

Commit 27654ac

Browse files
[CMake] Add build configuration option to enable security flags (#8456)
Signed-off-by: Tikhomirova, Kseniya <kseniya.tikhomirova@intel.com>
1 parent 6f4fde6 commit 27654ac

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

buildbot/configure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ def do_configure(args):
181181
# Add additional CMake options if provided
182182
if args.cmake_opt:
183183
cmake_cmd += args.cmake_opt
184+
185+
if args.add_security_flags:
186+
cmake_cmd.extend(["-DEXTRA_SECURITY_FLAGS={}".format(args.add_security_flags)])
184187

185188
# Add path to root CMakeLists.txt
186189
cmake_cmd.append(llvm_dir)
@@ -247,6 +250,7 @@ def main():
247250
parser.add_argument("--ci-defaults", action="store_true", help="Enable default CI parameters")
248251
parser.add_argument("--enable-plugin", action='append', help="Enable SYCL plugin")
249252
parser.add_argument("--disable-fusion", action="store_true", help="Disable the kernel fusion JIT compiler")
253+
parser.add_argument("--add_security_flags", type=str, choices=['none', 'default', 'sanitize'], default=None, help="Enables security flags for compile & link. Two values are supported: 'default' and 'sanitize'. 'Sanitize' option is an extension of 'default' set.")
250254
args = parser.parse_args()
251255

252256
print("args:{}".format(args))
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
macro(add_compile_option_ext flag name)
2+
cmake_parse_arguments(ARG "" "" "" ${ARGN})
3+
set(CHECK_STRING "${flag}")
4+
if (MSVC)
5+
set(CHECK_STRING "/WX ${CHECK_STRING}")
6+
else()
7+
set(CHECK_STRING "-Werror ${CHECK_STRING}")
8+
endif()
9+
10+
check_c_compiler_flag("${CHECK_STRING}" "C_SUPPORTS_${name}")
11+
check_cxx_compiler_flag("${CHECK_STRING}" "CXX_SUPPORTS_${name}")
12+
if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name})
13+
message(STATUS "Building with ${flag}")
14+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
15+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
16+
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${flag}")
17+
else()
18+
message(WARNING "${flag} is not supported.")
19+
endif()
20+
endmacro()
21+
22+
macro(add_link_option_ext flag name)
23+
include(LLVMCheckLinkerFlag)
24+
cmake_parse_arguments(ARG "" "" "" ${ARGN})
25+
llvm_check_linker_flag(CXX "${flag}" "LINKER_SUPPORTS_${name}")
26+
if(LINKER_SUPPORTS_${name})
27+
message(STATUS "Building with ${flag}")
28+
append("${flag}" ${ARG_UNPARSED_ARGUMENTS})
29+
else()
30+
message(WARNING "${flag} is not supported.")
31+
endif()
32+
endmacro()
33+
34+
function(append_common_extra_security_flags)
35+
if( LLVM_ON_UNIX )
36+
# Fortify Source (strongly recommended):
37+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
38+
message(WARNING
39+
"-D_FORTIFY_SOURCE=2 can only be used with optimization.")
40+
message(WARNING "-D_FORTIFY_SOURCE=2 is not supported.")
41+
else()
42+
# Sanitizers do not work with checked memory functions,
43+
# such as __memset_chk. We do not build release packages
44+
# with sanitizers, so just avoid -D_FORTIFY_SOURCE=2
45+
# under LLVM_USE_SANITIZER.
46+
if (NOT LLVM_USE_SANITIZER)
47+
message(STATUS "Building with -D_FORTIFY_SOURCE=2")
48+
add_definitions(-D_FORTIFY_SOURCE=2)
49+
else()
50+
message(WARNING
51+
"-D_FORTIFY_SOURCE=2 dropped due to LLVM_USE_SANITIZER.")
52+
endif()
53+
endif()
54+
55+
# Format String Defense
56+
add_compile_option_ext("-Wformat" WFORMAT)
57+
add_compile_option_ext("-Wformat-security" WFORMATSECURITY)
58+
add_compile_option_ext("-Werror=format-security" WERRORFORMATSECURITY)
59+
60+
# Stack Protection
61+
add_compile_option_ext("-fstack-protector-strong" FSTACKPROTECTORSTRONG)
62+
63+
# Full Relocation Read Only
64+
add_link_option_ext("-Wl,-z,relro" ZRELRO
65+
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
66+
CMAKE_SHARED_LINKER_FLAGS)
67+
68+
# Immediate Binding (Bindnow)
69+
add_link_option_ext("-Wl,-z,now" ZNOW
70+
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
71+
CMAKE_SHARED_LINKER_FLAGS)
72+
endif()
73+
endfunction()
74+
75+
if ( EXTRA_SECURITY_FLAGS )
76+
if (EXTRA_SECURITY_FLAGS STREQUAL "none")
77+
# No actions.
78+
elseif (EXTRA_SECURITY_FLAGS STREQUAL "default")
79+
append_common_extra_security_flags()
80+
elseif (EXTRA_SECURITY_FLAGS STREQUAL "sanitize")
81+
append_common_extra_security_flags()
82+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
83+
add_compile_option_ext("-fsanitize=cfi" FSANITIZE_CFI)
84+
add_link_option_ext("-fsanitize=cfi" FSANITIZE_CFI_LINK
85+
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
86+
CMAKE_SHARED_LINKER_FLAGS)
87+
# Recommended option although linking a DSO with SafeStack is not currently supported by compiler.
88+
#add_compile_option_ext("-fsanitize=safe-stack" FSANITIZE_SAFESTACK)
89+
#add_link_option_ext("-fsanitize=safe-stack" FSANITIZE_SAFESTACK_LINK
90+
# CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
91+
# CMAKE_SHARED_LINKER_FLAGS)
92+
else()
93+
add_compile_option_ext("-fcf-protection=full -mcet" FCF_PROTECTION)
94+
# need to align compile and link option set, link now is set unconditionally
95+
add_link_option_ext("-fcf-protection=full -mcet" FCF_PROTECTION_LINK
96+
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
97+
CMAKE_SHARED_LINKER_FLAGS)
98+
endif()
99+
else()
100+
message(FATAL_ERROR "Unsupported value of EXTRA_SECURITY_FLAGS: ${EXTRA_SECURITY_FLAGS}")
101+
endif()
102+
endif()
103+

llvm/cmake/modules/HandleLLVMOptions.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,5 +1286,8 @@ if(LLVM_USE_RELATIVE_PATHS_IN_FILES)
12861286
add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES)
12871287
endif()
12881288

1289+
# Add to the end since we need some definitions to be set (LLVM_ON_LINUX)
1290+
include(AddSecurityFlags)
1291+
12891292
set(LLVM_THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../third-party CACHE STRING
12901293
"Directory containing third party software used by LLVM (e.g. googletest)")

0 commit comments

Comments
 (0)