Skip to content

Commit d8fdbcf

Browse files
committed
Add compile-time hardening flags, and enable by default.
1 parent ac937aa commit d8fdbcf

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ before_install:
2424
- wget https://github.com/ellzey/libevhtp/archive/1.2.10.zip -O /tmp/libevhtp-1.2.10.zip
2525
- unzip -d /tmp/ /tmp/libevhtp-1.2.10.zip
2626
- mkdir /tmp/libevhtp-1.2.10/build
27-
- pushd /tmp/libevhtp-1.2.10/build && cmake -DEVHTP_DISABLE_REGEX:STRING=ON .. && sudo make install; popd
27+
- pushd /tmp/libevhtp-1.2.10/build && cmake -DEVHTP_DISABLE_REGEX:STRING=ON -DCMAKE_C_FLAGS="-fPIC" .. && sudo make install; popd
2828
- sudo pip install -r python/requirements.txt
2929
- sudo ln -s protobuf-java.jar /usr/share/java/protobuf.jar
3030
- mkdir -p $GOPATH/src/github.com/google

configure.ac

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ AC_CONFIG_MACRO_DIRS([m4])
77
AM_SILENT_RULES([yes])
88
AC_LANG([C++])
99

10+
AC_ARG_ENABLE(hardening,
11+
AS_HELP_STRING([--disable-hardening], [Use C++ compiler flags which produce a hardened binary]))
12+
1013
GMOCK_DIR="${GMOCK_DIR=/usr/src/gmock}"
1114
AC_ARG_VAR([GMOCK_DIR], [directory containing Google Mock])
1215
GTEST_DIR="${GTEST_DIR="$GMOCK_DIR/gtest"}"
@@ -25,6 +28,25 @@ AC_CHECK_PROGS([ANT], [ant])
2528

2629
PKG_CHECK_MODULES([json_c], [json-c])
2730

31+
if test "x${enable_hardening}" != "xno"; then
32+
common_harden_copts="-fstack-protector-all -fPIE -Wa,--noexecstack -Wformat -Wformat-security"
33+
clang_harden_copts="-Qunused-arguments $common_harden_copts"
34+
gcc_harden_copts="$common_harden_copts"
35+
AS_CASE([$CXX],
36+
[clang++], [AS_VAR_APPEND([CXXFLAGS], [" $clang_harden_copts"])],
37+
[g++], [AS_VAR_APPEND([CXXFLAGS], [" $gcc_harden_copts"])],
38+
[AC_MSG_FAILURE([Hardening enabled, but we don't have hardening flags for C++ compiler $CXX])])
39+
AS_CASE([$CC],
40+
[clang], [AS_VAR_APPEND([CFLAGS], [" $clang_harden_copts"])],
41+
[gcc], [AS_VAR_APPEND([CFLAGS], [" $gcc_harden_copts"])],
42+
[AC_MSG_FAILURE(["Hardening enabled, but we don't have hardening flags for C compiler $CC"])])
43+
AS_VAR_APPEND([CPPFLAGS], [" -D_FORTIFY_SOURCE=2"])
44+
AS_VAR_APPEND([LDFLAGS], [" -pie -Wl,-z,relro,-z,now"])
45+
AC_DEFINE([ENABLE_HARDENING], [], [Hardening enabled.])
46+
else
47+
AC_MSG_WARN([NOT building hardened binaries])
48+
fi
49+
2850
# Checks for header files.
2951
AC_HEADER_RESOLV
3052
AC_CHECK_HEADERS([arpa/inet.h fcntl.h limits.h netinet/in.h stddef.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h leveldb/filter_policy.h])

cpp/util/init.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <unistd.h>
1111

1212
#include "log/ct_extensions.h"
13+
#include "config.h"
1314
#include "version.h"
1415

1516
using std::string;
@@ -36,6 +37,11 @@ void InitCT(int* argc, char** argv[]) {
3637
cert_trans::LoadCtExtensions();
3738

3839
LOG(INFO) << "Build version: " << google::VersionString();
40+
#ifdef ENABLE_HARDENING
41+
LOG(INFO) << "Binary built with hardening enabled.";
42+
#else
43+
LOG(WARNING) << "Binary built with hardening DISABLED.";
44+
#endif
3945
}
4046

4147

0 commit comments

Comments
 (0)