From patchwork Fri Dec 4 14:28:44 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 5848 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:6022:311b:b0:11b:d6c:f273 with SMTP id e27csp1634070laa; Fri, 4 Dec 2026 06:29:03 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErF9uecgtFOXE2O8gPScW+DGkcQpyokoqV+uNm+UG/bDOe7YetTanV2YN5bHrQoZWv3Thp2x1i1L2eY=@openvpn.net X-Received: by 2002:a05:7301:fc8a:b0:375:7be3:1bbd with SMTP id 5a478bee46e88-387521b3c56mr2225998eec.23.1796394543583; Fri, 04 Dec 2026 06:29:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1796394543; cv=none; d=google.com; s=arc-20260327; b=Cf5hhFTVKim1ZNDi5iH0GtGQLBUQF92dBiDut3CePAxOK2xr6POyU3ssQI74MVZQUC p8L3WkjW1jISmxa2E9ssFDvlEhfOrwwwfibL5Jz9WM/mY4KfPC9zP58kHqnn1LwT6ZA3 AkSlyRkmrP/k+W1CP7v2ehi5yBpSCuav+14sViKQ3Y+12vE7DNAmsJCvNknDiULskOdp mN99Z7igt+LI0+ZmdzJHhinTe8cdyScIq3nNOuT8wDsAFiHxcoXfbSK78cjoYNCoHWtV 5JbL7c5Yz/SkWd5kIA913slEVzn2U87qVSGxWJk3LP1bcaVcyaWOs986mrlGap3Ld8hn vT7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=WKtXXiDcti4cTDm5kTLWG+ncuEnHgku54tPZGq6jJ2E=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=QKrsvEgilBXDsoVrtxd+AwFRf2BfHPEq8PvxB2EAx8JJix5cwfVF8fQsu8jr7sQrm8 pkLMHlvUdj4X+a1OwUC5Ai1sgGqYdM/Nv8Le/hdj+uygbFZmxtQSlXTYTRhbhNLg2+tV Q85txMpF+defoqn5RIDD4cAN/Ez3CN07+QYgS2qNxusZbgg/RFRuLa7W/WtZ/lOp3djd QvnMbdOVBlS9U1CAJIoI55KBNA7pvoFzEpxte5PuyIiQDJlFBVwx3B48nfMsnpEa4apn cejcQuhAVe8Z+HHEsq8LjpDimH2caBz1YIb1i8jkrP2POB/EuBDihxbLYP7tzELCCt/P ovGQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=DNrGJc40; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=BVY7mbg5; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=cTkyqBCl; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 5a478bee46e88-38752203787si8788648eec.33.2026.12.04.06.29.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Dec 2026 06:29:03 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=DNrGJc40; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=BVY7mbg5; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=cTkyqBCl; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=WKtXXiDcti4cTDm5kTLWG+ncuEnHgku54tPZGq6jJ2E=; b=DNrGJc40VsRaDO 5DFnTRmplje9fLDKce3OSyNoGUXQPFq1zn4z4euybaDwRFR4Mqk34posRcoi95+QcACtJgYE7sunE Yfrpx1Mn5A6VQOfd/bFsiHJ3Xg4q1HOBfN11LvB3B364fBmtoqaqiMSQ0OEDpDzdamrInw2/w74VA 0cM=; Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xZUHW-000000001Mj-0nB9; Fri, 04 Dec 2026 14:29:02 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xZUHT-000000001MS-2ejv for openvpn-devel@lists.sourceforge.net; Fri, 04 Dec 2026 14:28:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Unsubscribe-Post:List-Subscribe:List-Post:List-Owner:List-Archive; bh=K1jIom4Gt1XOouFbUKN4tZnltet0lQKKUMx+20Rv+Kw=; b=BVY7mbg5SUHWeJYvTTMB3LFWmE 3eoNQh74aB2SF2uGflXixthkYK1VyKtISvFShK0Htq2MPNPAiKLFjBGZYR2QALbJHTYO/fWQArqKC JyjLGgkzv1oE46D5Bb4mPLespykpfQFSLbAw7VPtThzR3bM+Xx8cRwWnNLU/AXiDQbKw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Unsubscribe-Post: List-Subscribe:List-Post:List-Owner:List-Archive; bh=K1jIom4Gt1XOouFbUKN4tZnltet0lQKKUMx+20Rv+Kw=; b=cTkyqBCliN8izpZyJK7Sjww53e ivIwld581ynqAG7w7vdTxcN7wuOEKIYdViv4wNFWh5XnlXewTxr2AD4iMg2gkM8NADj7JEnxFtEiZ 865YtilMdFa5sQ/CkFFbZoZaaiF3ODgg1Mwtlwk2vWMj+nntq3lxq5rwxLdirS1IEC/s=; Received: from [193.149.48.129] (helo=blue.greenie.muc.de) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.100.1) id 1xZUHQ-000000003ck-0JT1 for openvpn-devel@lists.sourceforge.net; Fri, 04 Dec 2026 14:28:57 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 6B4ESnCs011109 for ; Fri, 4 Dec 2026 15:28:49 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.2/8.18.1/Submit) id 6B4ESnoa011108 for openvpn-devel@lists.sourceforge.net; Fri, 4 Dec 2026 15:28:49 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Fri, 4 Dec 2026 15:28:44 +0100 Message-ID: <20261204142849.11091-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-1.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Aravind O R 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. Dis [...] Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1xZUHQ-000000003ck-0JT1 Subject: [Openvpn-devel] [PATCH v5] configure: add compiler and linker hardening flags X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: 1883656204600799544 X-GMAIL-MSGID: 1883656204600799544 From: Aravind O R 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 Acked-by: Frank Lichtenheld 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 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 +#include +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])