Skip to content

Commit f8b143e

Browse files
committed
Consolidate security-related compiler flags
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 the mcrouter extension. Lack of this was causing mcrouter to be built without compiler optimizations, which doesn't play well with `FORTIFY_SOURCE`. HHVM runs fine with these flags, requiring only a small fix to folly/Subprocess.cpp to fix compatibility with the overloaded `openat()` syscall from `FORTIFY_SOURCE`. 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
1 parent 4ee0291 commit f8b143e

4 files changed

Lines changed: 42 additions & 20 deletions

File tree

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/folly/src/folly/Subprocess.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ static Ret subprocess_libc_load(
145145
#define FOLLY_DETAIL_SUBPROCESS_LIBC_X_OPEN(X) \
146146
X(open, __open_real) \
147147
X(openat, __openat_real)
148+
#elif defined(_FORTIFY_SOURCE)
149+
#define FOLLY_DETAIL_SUBPROCESS_LIBC_X_OPEN(X) \
150+
X(open, open) \
151+
X(openat, __openat_2)
148152
#else
149153
#define FOLLY_DETAIL_SUBPROCESS_LIBC_X_OPEN(X) \
150154
X(open, open) \

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>"

0 commit comments

Comments
 (0)