Attention is currently required from: Arne Schwabe, Frank Lichtenheld.

Hello Arne Schwabe,

I'd like you to do a code review.
Please visit

    https://gerrit.openvpn.net/c/openvpn/+/2213?usp=email

to review the following change.


Change subject: configure: add compiler and linker hardening flags
......................................................................

configure: add compiler and linker hardening flags

The MSVC build already hardens the binaries with /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, ports 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 a subset of the flags recommended by the OpenSSF "Compiler Options
Hardening Guide for C and C++", 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

A flag is only used if a small test program with a stack buffer and
calls to fortified libc functions still compiles and links without any
warning. Warnings count as failure 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.
--disable-hardening turns all of this off.

_FORTIFY_SOURCE goes into CFLAGS rather than CPPFLAGS, as Fedora and
Arch do, because it needs optimization: "make CFLAGS=-O0" then drops it
together with the -O flag. Level 3 needs GCC 12 or clang 9, otherwise
level 2 is used. 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. In a test with clang 23, a fortified memcpy() overflow aborted
with "buffer overflow detected" before ASan could report it.

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

Tested on x86_64 Linux with GCC 16.2, clang 23.1, glibc 2.44 and the
bfd, gold and lld linkers: configure with 13 combinations of CC,
CPPFLAGS, CFLAGS and LDFLAGS (among them the Arch package flags,
Debian-style flags, -O0, -U_FORTIFY_SOURCE and the ASan flags of the
GitHub workflow), and "make check" of --enable-werror builds with gcc,
clang, and clang with ASan/UBSan.

The same builds, with and without this patch, in containers: Ubuntu
22.04 and 24.04, Debian 11 and 12, AlmaLinux 9 and Alpine 3.22 (musl),
covering GCC 10 to 14 and clang 11 to 22. No new warnings, and the
only failing test, t_net, fails without the patch too (the containers
have no ip tool). On AlmaLinux 9 (GCC 11, glibc 2.34) level 3 is
rejected and level 2 used. Debian 11 gcc and Alpine clang fail
--enable-werror with and without this patch, because of existing
-Wconversion and -Wsign-compare warnings.

Not tested: the BSDs, macOS, Solaris and architectures other than
x86_64.

Assisted-by: Claude Code:claude-opus-5-5
Change-Id: I8fe2cbeadde3bad2185c4cee824ed2fe307e29e2
Signed-off-by: Aravind O R <[email protected]>
---
M Changes.md
M configure.ac
2 files changed, 84 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/13/2213/1

diff --git a/Changes.md b/Changes.md
index 89a72fe..9bf188a 100644
--- a/Changes.md
+++ b/Changes.md
@@ -45,6 +45,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`. Flags given in `CPPFLAGS`, `CFLAGS` or `LDFLAGS`
+  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 585c3bd..674a315 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@:>@])],
        ,
@@ -1263,6 +1270,75 @@
    [CFLAGS="`echo $CFLAGS | $SED -e 's/-O[a-z0-9]*//g'`"]
 fi

+# Hardening flags, a subset of the OpenSSF "Compiler Options Hardening
+# Guide for C and C++". Distribution packages usually get similar flags
+# from their build tools; this gives other builds the same protection.
+# A flag is only used if a small program with a stack buffer and calls to
+# fortified libc functions still compiles and links without any warning
+# (ac_c_werror_flag, as AC_LANG_WERROR sets it). 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 the toolchain accepts $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="$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 warns otherwise), so fall
+       # back to 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 a fortified function
+       # would abort before ASan can report the overflow.
+       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])
+       ACL_CHECK_ADD_HARDENING_FLAG([cflags], [-fcf-protection=full])
+       ACL_CHECK_ADD_HARDENING_FLAG([cflags], [-mbranch-protection=standard])
+       ACL_CHECK_ADD_HARDENING_FLAG([ldflags], [-Wl,-z,relro])
+       ACL_CHECK_ADD_HARDENING_FLAG([ldflags], [-Wl,-z,now])
+       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])

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2213?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I8fe2cbeadde3bad2185c4cee824ed2fe307e29e2
Gerrit-Change-Number: 2213
Gerrit-PatchSet: 1
Gerrit-Owner: Aravind O R <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Frank Lichtenheld <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
Gerrit-Attention: Frank Lichtenheld <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to