[Openvpn-devel,v5] configure: add compiler and linker hardening flags

Message ID 20261204142849.11091-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v5] configure: add compiler and linker hardening flags |

Commit Message

Gert Doering Dec. 4, 2026, 2:28 p.m. UTC
  From: Aravind O R <aravindor@gmail.com>

The MSVC build already uses /sdl, /guard:cf and, for x86/x64 Release
builds, /CETCOMPAT. A ./configure build only gets what the compiler
enables by default or what is passed in CFLAGS and LDFLAGS.
Distribution packages get hardening flags from their build tools, but
self-built binaries and embedded builds often do not. Compiler defaults
differ too: on Ubuntu 24.04 a build with gcc gets a stack protector and
BIND_NOW, the same build with clang gets neither.

Add the run-time protections from the OpenSSF "Compiler Options
Hardening Guide for C and C++" that Fedora and Arch apply to all their
packages, and Debian with dpkg-buildflags hardening=+all, through build
flags or toolchain defaults, where the toolchain accepts them:

  -D_FORTIFY_SOURCE=3 (or 2)
  -fstack-protector-strong
  -fstack-clash-protection
  -fcf-protection=full (x86) or -mbranch-protection=standard (arm64)
  -Wl,-z,relro -Wl,-z,now
  -Wl,-z,noexecstack

-Wl,-z,noexecstack is the default for compiler output, but gold, and
GNU ld as built by Debian and Ubuntu, make the stack executable when an
object has no .note.GNU-stack section.

Of the distribution flags, -fexceptions is left out because openvpn
creates no threads, -Wl,--as-needed because it changes which libraries
are linked rather than protecting the code, and -pie because the
plugins link with the same LDFLAGS: with gcc, -pie after -shared links
an executable. C++-only flags do not apply, and
-Wl,--no-copy-dt-needed-entries is already the GNU ld default. Of the
guide's other flags, -Wl,-z,nodlopen would stop the plugins from
loading, and -fno-strict-overflow, -fno-strict-aliasing,
-fno-delete-null-pointer-checks, -ftrivial-auto-var-init,
-fzero-call-used-regs, -fzero-init-padding-bits and
-fstrict-flex-arrays are not applied by distributions by default, so
they deserve their own discussion. -fhardened (GCC 14) also enables
-ftrivial-auto-var-init=zero and -pie. Warning flags stay with the
existing list in configure.ac.

A flag is only used if a small test program with a stack buffer and
calls to fortified libc functions still compiles and links with -Werror,
as in ACL_CHECK_ADD_COMPILE_FLAGS, and with nothing on stderr, so
nothing is added if the user's flags alone make it warn. The stderr
check also catches linker warnings, which -Werror does not turn into
errors; they matter because GNU ld and lld only warn about an unknown
-z option. The accepted flags go in front of CFLAGS and LDFLAGS,
so a choice made there, like -fno-stack-protector or -Wl,-z,lazy, takes
precedence. GCC 14 and later combine repeated -fcf-protection options
instead, so -fcf-protection and -mbranch-protection are not added when
CC, CPPFLAGS or CFLAGS already set one. Of these two,
-fcf-protection=full is only tried when the compiler targets x86 and
-mbranch-protection=standard only for arm64, checked with the
preprocessor in the test program, so -m32 or --target in CC or CFLAGS
count. On the FreeBSD 15 amd64 buildbot, the test program built with
-mbranch-protection=standard, but make then failed on it.
--disable-hardening turns all of this off.

_FORTIFY_SOURCE goes into CFLAGS rather than CPPFLAGS, as Fedora and
Arch do, because it needs optimization: a make-time CFLAGS=-O0 then
replaces it together with the -O flag. Level 3 needs GCC 12 or clang 9,
otherwise level 2 is used, and glibc 2.32 and older treat 3 as 2.
Unlike the guide, this does not use -U_FORTIFY_SOURCE, so a level
chosen elsewhere is respected: the define is skipped when CPPFLAGS,
CFLAGS or the compiler set another level or -U_FORTIFY_SOURCE, when
building without optimization, and for ASan builds, where FORTIFY can
hide an overflow from ASan.

MinGW builds are not changed: there -fstack-protector and
_FORTIFY_SOURCE can need libssp or a recent mingw-w64 runtime. The
CMake build can follow in a separate patch.

Tested on x86_64 Linux only. On Arch (GCC 16, clang 23, glibc 2.44, bfd,
gold and lld): configure with 19 combinations of CC, CPPFLAGS, CFLAGS
and LDFLAGS (among them the Arch package flags, Debian-style flags, -O0,
-U_FORTIFY_SOURCE, -fcf-protection in CC, CPPFLAGS and CFLAGS,
-Wl,-z,execstack and the ASan flags of the GitHub workflow), and "make
check" of --enable-werror builds with gcc, clang, and clang with
ASan/UBSan. In containers, gcc and clang builds with and without this
patch on Ubuntu 22.04 and 24.04, Debian 11 and 12, AlmaLinux 9 and
Alpine 3.22 (GCC 10 to 14, clang 11 to 22, musl): no new warnings and
the same test results (t_net fails either way, the containers have no ip
tool). Debian 11 gcc and Alpine clang were built without
--enable-werror, which fails there on master too. The two architecture
checks were tried with clang -fsyntax-only for x86_64 and i386 FreeBSD,
aarch64 FreeBSD and Linux, and arm64 macOS: each target only accepts its
own flag. Not tested: the BSDs themselves, macOS, Solaris and other
architectures.

Assisted-by: Claude Code:claude-opus-5-5
Change-Id: I8fe2cbeadde3bad2185c4cee824ed2fe307e29e2
Signed-off-by: Aravind O R <aravindor@gmail.com>
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2213
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2213
This mail reflects revision 5 of this Change.

Acked-by according to Gerrit (reflected above):
Frank Lichtenheld <frank@lichtenheld.com>
  

Patch

diff --git a/Changes.md b/Changes.md
index 8d10d1e..a2f2a69 100644
--- a/Changes.md
+++ b/Changes.md
@@ -66,6 +66,14 @@ 
 - Support for Mbed TLS 3 is dropped. If you build OpenVPN with the Mbed TLS crypto
   backend, you need to upgrade to Mbed TLS 4.
 
+- configure now adds compiler and linker hardening flags that the toolchain
+  accepts without warnings: `-D_FORTIFY_SOURCE=3` (or 2),
+  `-fstack-protector-strong`, `-fstack-clash-protection`,
+  `-fcf-protection=full` (x86) or `-mbranch-protection=standard` (arm64), and
+  `-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack`. Flags given in `CFLAGS` or
+  `LDFLAGS`, and a `_FORTIFY_SOURCE` level in `CPPFLAGS`, take precedence.
+  MinGW builds are not changed. Use `--disable-hardening` to turn this off.
+
 # Overview of changes in 2.7
 
 ## New features
diff --git a/configure.ac b/configure.ac
index ef05253..2a9efc3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -214,6 +214,13 @@ 
 )
 
 AC_ARG_ENABLE(
+	[hardening],
+	[AS_HELP_STRING([--disable-hardening], [do not add compiler and linker hardening flags like -D_FORTIFY_SOURCE, -fstack-protector-strong and -Wl,-z,now @<:@default=yes@:>@])],
+	,
+	[enable_hardening="yes"]
+)
+
+AC_ARG_ENABLE(
 	[selinux],
 	[AS_HELP_STRING([--enable-selinux], [enable SELinux support @<:@default=no@:>@])],
 	,
@@ -1270,6 +1277,122 @@ 
    [CFLAGS="`echo $CFLAGS | $SED -e 's/-O[a-z0-9]*//g'`"]
 fi
 
+# Hardening flags: the run-time protections from the OpenSSF "Compiler
+# Options Hardening Guide for C and C++" that Fedora and Arch apply to
+# all their packages, and Debian with dpkg-buildflags hardening=+all,
+# through build flags or toolchain defaults. Left out of those are
+# -fexceptions, because openvpn creates no threads, -Wl,--as-needed,
+# because it changes which libraries are linked rather than protecting
+# the code, and -pie, because it must only reach the executable while
+# the plugins link with the same LDFLAGS. -Wl,-z,noexecstack is the
+# default for compiler output, but GNU ld and gold can make the stack
+# executable when an object has no .note.GNU-stack section, so it is set
+# explicitly.
+#
+# A flag is only used if a small program with a stack buffer and calls to
+# fortified libc functions still compiles and links with -Werror, as in
+# ACL_CHECK_ADD_COMPILE_FLAGS, and without any output on stderr: with
+# ac_c_werror_flag set (as AC_LANG_WERROR does, but only for this test),
+# AC_LINK_IFELSE fails on such output. That catches linker warnings, which
+# -Werror does not turn into errors (GNU ld and lld only warn about an
+# unknown -z option). If the user's flags alone make the program warn,
+# nothing is added. The accepted flags go in front of CFLAGS and LDFLAGS,
+# so a choice made there by the user (e.g. -fno-stack-protector or
+# -Wl,-z,lazy) takes precedence.
+AC_DEFUN([ACL_CHECK_ADD_HARDENING_FLAG], [
+    AC_MSG_CHECKING([whether to add $2])
+    old_hardening_flags="$hardening_$1"
+    hardening_$1="${hardening_$1:+$hardening_$1 }$2"
+    old_cflags="$CFLAGS"
+    old_ldflags="$LDFLAGS"
+    old_werror_flag="$ac_c_werror_flag"
+    CFLAGS="-Werror $hardening_cflags $CFLAGS"
+    LDFLAGS="$hardening_ldflags $LDFLAGS"
+    ac_c_werror_flag="yes"
+    AC_LINK_IFELSE([AC_LANG_SOURCE([[$3
+#include <stdio.h>
+#include <string.h>
+int main(int argc, char **argv)
+{
+    char buf[16];
+    snprintf(buf, sizeof(buf), "%s", argv[0]);
+    memcpy(buf, argv[0], (size_t)argc);
+    return buf[0];
+}]])], [hardening_ok="yes"], [hardening_ok="no"; hardening_$1="$old_hardening_flags"])
+    AC_MSG_RESULT([$hardening_ok])
+    CFLAGS="$old_cflags"
+    LDFLAGS="$old_ldflags"
+    ac_c_werror_flag="$old_werror_flag"]
+)
+
+# MinGW is left alone: there -fstack-protector and _FORTIFY_SOURCE can
+# need libssp or a recent mingw-w64 runtime.
+if test "${enable_hardening}" = "yes" -a "${WIN32}" != "yes"; then
+	hardening_cflags=""
+	hardening_ldflags=""
+	# Level 3 needs GCC 12 or clang 9 (glibc 2.33 and later warn
+	# otherwise), so fall back to 2. Older glibc treats 3 as 2. Not added
+	# without optimization (glibc warns), when CPPFLAGS, CFLAGS or the
+	# compiler already set another level or -U_FORTIFY_SOURCE, or for ASan
+	# builds, where FORTIFY can hide an overflow from ASan (with clang, the
+	# fortified function aborts before ASan reports it).
+	for fortify_level in 3 2; do
+		ACL_CHECK_ADD_HARDENING_FLAG([cflags], [-D_FORTIFY_SOURCE=$fortify_level], [
+#if !defined(_FORTIFY_SOURCE) || _FORTIFY_SOURCE != $fortify_level
+#error "_FORTIFY_SOURCE is set elsewhere"
+#endif
+#if defined(__SANITIZE_ADDRESS__)
+#error "ASan build"
+#elif defined(__has_feature)
+#if __has_feature(address_sanitizer)
+#error "ASan build"
+#endif
+#endif
+		])
+		test "${hardening_ok}" = "yes" && break
+	done
+	ACL_CHECK_ADD_HARDENING_FLAG([cflags], [-fstack-protector-strong])
+	ACL_CHECK_ADD_HARDENING_FLAG([cflags], [-fstack-clash-protection])
+	# GCC 14 and later combine repeated -fcf-protection options instead of
+	# using the last one, so a value set by the user would not take
+	# precedence over ours. Leave the option to the user then, and treat
+	# -mbranch-protection the same way. Each of the two is only tried for
+	# the architecture it is meant for, x86 and arm64, as distributions do.
+	# The check uses the compiler's own target, so -m32 or --target in CC
+	# or CFLAGS count.
+	case "${CC} ${CPPFLAGS} ${CFLAGS}" in
+		*-fcf-protection*)
+			AC_MSG_CHECKING([whether to add -fcf-protection=full])
+			AC_MSG_RESULT([no, set by the user])
+			;;
+		*)
+			ACL_CHECK_ADD_HARDENING_FLAG([cflags], [-fcf-protection=full], [
+#if !defined(__x86_64__) && !defined(__i386__)
+#error "not x86"
+#endif
+			])
+			;;
+	esac
+	case "${CC} ${CPPFLAGS} ${CFLAGS}" in
+		*-mbranch-protection*)
+			AC_MSG_CHECKING([whether to add -mbranch-protection=standard])
+			AC_MSG_RESULT([no, set by the user])
+			;;
+		*)
+			ACL_CHECK_ADD_HARDENING_FLAG([cflags], [-mbranch-protection=standard], [
+#if !defined(__aarch64__)
+#error "not arm64"
+#endif
+			])
+			;;
+	esac
+	ACL_CHECK_ADD_HARDENING_FLAG([ldflags], [-Wl,-z,relro])
+	ACL_CHECK_ADD_HARDENING_FLAG([ldflags], [-Wl,-z,now])
+	ACL_CHECK_ADD_HARDENING_FLAG([ldflags], [-Wl,-z,noexecstack])
+	CFLAGS="${hardening_cflags} ${CFLAGS}"
+	LDFLAGS="${hardening_ldflags} ${LDFLAGS}"
+fi
+
 if test "${enable_pedantic}" = "yes"; then
 	CFLAGS="${CFLAGS} -pedantic"
 	AC_DEFINE([PEDANTIC], [1], [Enable pedantic mode])