Skip to content

Commit 210c88c

Browse files
mszabo-wikiameta-codesync[bot]
authored andcommitted
Consolidate security-related compiler flags (#9672)
Summary: Update and extend the list of hardening-related compiler flags used by HHVM to better represent modern distro defaults. * Convert the existing `ENABLE_SSP` build option into a new `ENABLE_HARDENING` option and put an updated list of security flags behind it. Both clang and GCC have been supporting these options for a while now, so we can set them irrespective of the compiler. * Put PIE-related options behind a separate `ENABLE_PIE` build option so that we can produce and compare non-PIE and PIE builds once we fix compatibility with PIE. * Forward `CMAKE_BUILD_TYPE` to vendored subprojects. Lack of this was causing the projects to be built without compiler optimizations, which doesn't play well with `FORTIFY_SOURCE`. On systems with glibc >= 2.40, facebook/folly#2519 is needed for this option to work. The overhead from these flags is likely to be limited, as many of them have been set by default for distribution packages for several years now.[1] [1] https://github.com/jvoisin/compiler-flags-distro Pull Request resolved: #9672 Reviewed By: Wilfred Differential Revision: D87347762 fbshipit-source-id: cdfbf29184e6022999e89258d7fa3475c971e01a
1 parent 3414577 commit 210c88c

File tree

7 files changed

+42
-20
lines changed

7 files changed

+42
-20
lines changed

CMake/HPHPCompiler.cmake

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,45 @@ if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQU
8383
set(GDB_SUBOPTION)
8484

8585
# Enable GCC/LLVM stack-smashing protection
86-
if(ENABLE_SSP)
86+
if(ENABLE_HARDENING)
8787
list(APPEND GENERAL_OPTIONS
88+
# Enable stack protection and stack-clash protection.
8889
# This needs two dashes in the name, so put one here.
8990
"-param=ssp-buffer-size=4"
90-
"pie"
91-
"fPIC"
91+
"fstack-protector-strong"
92+
"fstack-clash-protection"
93+
94+
# Use hardened equivalents of various glibc functions
95+
# to guard against buffer overflows.
96+
"D_FORTIFY_SOURCE=3"
97+
98+
# https://isisblogs.poly.edu/2011/06/01/relro-relocation-read-only/
99+
"Wl,-z,relro,-z,now"
100+
# Mark stack as non-executable.
101+
"Wl,-z,noexecstack"
102+
# Separate ELF code into its own segment.
103+
"Wl,-z,separate-code"
92104
)
105+
106+
# Enable control-flow / branch protection.
107+
if (IS_X64)
108+
list(APPEND GENERAL_OPTIONS "fcf-protection")
109+
elseif (IS_AARCH64)
110+
list(APPEND GENERAL_OPTIONS "mbranch-protection=standard")
111+
endif()
112+
113+
# Enable C++ standard library assertions.
114+
if (CLANG_FORCE_LIBCPP)
115+
list(APPEND GENERAL_CXX_OPTIONS "D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE")
116+
else()
117+
list(APPEND GENERAL_CXX_OPTIONS "D_GLIBCXX_ASSERTIONS")
118+
endif()
119+
endif()
120+
121+
if (ENABLE_PIE)
122+
list(APPEND GENERAL_OPTIONS "pie" "fPIC")
123+
else()
124+
list(APPEND GENERAL_OPTIONS "no-pie")
93125
endif()
94126

95127
if (IS_X64)
@@ -110,13 +142,6 @@ if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQU
110142
"unused-command-line-argument"
111143
)
112144

113-
# Enabled GCC/LLVM stack-smashing protection
114-
if(ENABLE_SSP)
115-
list(APPEND GENERAL_OPTIONS "fstack-protector-strong")
116-
else()
117-
list(APPEND GENERAL_OPTIONS "no-pie")
118-
endif()
119-
120145
if(CLANG_FORCE_LIBCPP)
121146
list(APPEND GENERAL_CXX_OPTIONS "stdlib=libc++")
122147
endif()
@@ -150,15 +175,6 @@ if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQU
150175
"-param=large-unit-insns=10000"
151176
)
152177

153-
# Enabled GCC/LLVM stack-smashing protection
154-
if(ENABLE_SSP)
155-
if(LINUX)
156-
# https://isisblogs.poly.edu/2011/06/01/relro-relocation-read-only/
157-
list(APPEND GENERAL_OPTIONS "Wl,-z,relro,-z,now")
158-
endif()
159-
list(APPEND GENERAL_OPTIONS "fstack-protector-strong")
160-
endif()
161-
162178
# X64
163179
if(IS_X64)
164180
list(APPEND GENERAL_CXX_OPTIONS "mcrc32")

CMake/Options.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#set(CMAKE_BUILD_TYPE Debug)
22

33
option(ALWAYS_ASSERT "Enabled asserts in a release build" OFF)
4-
option(ENABLE_SSP "Enabled GCC/LLVM stack-smashing protection" OFF)
4+
option(ENABLE_HARDENING "Set hardening flags and definitions, e.g. stack-smashing protection" OFF)
5+
option(ENABLE_PIE "Produce position-independent executables" OFF)
56
option(STATIC_CXX_LIB "Statically link libstd++ and libgcc." OFF)
67
option(ENABLE_AARCH64_CRC "Enable the use of CRC instructions" OFF)
78
option(ENABLE_FASTCGI "Enable the FastCGI interface." ON)

third-party/brotli/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ExternalProject_Add(
1515
bundled_brotli
1616
${BROTLI_SOURCE_ARGS}
1717
CMAKE_ARGS
18+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
1819
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
1920
-DCMAKE_INSTALL_INCLUDEDIR=include
2021
-DCMAKE_INSTALL_LIBDIR=lib

third-party/libzip/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ ExternalProject_Add(
4646
-DBUILD_EXAMPLES=FALSE
4747
-DBUILD_DOC=FALSE
4848
-DBUILD_SHARED_LIBS=FALSE
49+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
4950
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
5051
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
5152
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>

third-party/mcrouter/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ExternalProject_Add(
4747
-DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT}
4848
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
4949
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
50+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
5051

5152
"-DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}"
5253
"-DBOOST_INCLUDE_DIR=$<TARGET_PROPERTY:boost,INTERFACE_INCLUDE_DIRECTORIES>"

third-party/timelib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ExternalProject_Add(
3434
-DCMAKE_INSTALL_INCLUDEDIR=include
3535
-DCMAKE_INSTALL_LIBDIR=lib
3636

37+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
3738
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
3839
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
3940
-DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT}

third-party/watchman/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ExternalProject_Add(
2626
-DCMAKE_INSTALL_INCLUDEDIR=include
2727
-DCMAKE_INSTALL_LIBDIR=lib
2828

29+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
2930
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
3031
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
3132
-DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT}

0 commit comments

Comments
 (0)