]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: ssl: unset SSLKEYLOGFILE unless explicitly allowed
authorWilliam Lallemand <wlallemand@haproxy.com>
Tue, 1 Dec 2026 12:56:42 +0000 (12:56 +0000)
committerWilliam Lallemand <wlallemand@haproxy.com>
Tue, 1 Dec 2026 15:20:59 +0000 (16:20 +0100)
OpenSSL 3.5 builds configured with enable-sslkeylog honor SSLKEYLOGFILE
and write TLS connection secrets directly to a file. This bypasses HAProxy
logging, adds filesystem access during handshakes, degrades performance
and leaks file descriptors across master-worker reloads. HAProxy already
provides tune.ssl.keylog and sample fetches for logging these secrets
through its normal logging facilities.

Replace the startup warnings with a conditional unsetenv() before full
configuration parsing can load certificates and create SSL contexts.
Add the experimental ssl-workaround-bogus-keylogfile { on | off } global
option, defaulting to off. Setting it to on leaves the variable untouched;
setting it to off keeps the default behavior of removing the variable.

Discover this option and expose-experimental-directives during the initial
configuration pass so the decision is known before certificate loading.
Repeat the conditional unsetenv() after a worker restores its initial
environment. Keep the original environment backup unchanged so reloads
can reevaluate the option, without copying or restoring the variable.

Document the OpenSSL version and build option requirements, discourage
native key logging due to its filesystem access and reload issues, and
recommend tune.ssl.keylog instead.

doc/configuration.txt
include/haproxy/ssl_sock-t.h
src/cfgparse-global.c
src/cfgparse-ssl.c
src/haproxy.c

index 17903e527f0fa700c6444568a55641b3485d006e..a99a78631aa980e79601fa95803d9f44966dac91 100644 (file)
@@ -1898,6 +1898,7 @@ The following keywords are supported in the "global" section :
    - ssl-security-level
    - ssl-server-verify
    - ssl-skip-self-issued-ca
+   - ssl-workaround-bogus-keylogfile
    - stats
    - stats-file
    - strict-limits
@@ -3993,6 +3994,31 @@ ssl-skip-self-issued-ca
   certificates. It's useless for BoringSSL, .issuer is ignored because ocsp
   bits does not need it. Requires at least OpenSSL 1.0.2.
 
+ssl-workaround-bogus-keylogfile { on | off }
+  Use of this directive is strongly discouraged. OpenSSL's native key logging
+  writes TLS secrets directly to the filesystem on every SSL handshake,
+  outside of HAProxy logging, which degrades performance. In master-worker
+  mode, it also leaks file descriptors on each reload. Prefer
+  "tune.ssl.keylog on" and the ssl_xx_*_secret sample fetches to log TLS secrets
+  through HAProxy's regular logging facilities instead.
+
+  When set to "on", this directive leaves the SSLKEYLOGFILE environment
+  variable unchanged. The default is "off": HAProxy unsets it at startup,
+  before loading certificates and creating SSL contexts.
+
+  OpenSSL introduced support for SSLKEYLOGFILE in version 3.5. This feature is
+  available only when OpenSSL is built with the "enable-sslkeylog" configuration
+  option. When the variable is set to a non-empty path, OpenSSL writes TLS
+  connection secrets to that file for use by traffic analysis tools. Other
+  SSL libraries may support this variable as well.
+
+  This directive is only meant for debugging setups which rely on the SSL
+  library behavior and accept the above drawbacks. It is experimental and
+  requires "expose-experimental-directives" to appear before it, otherwise
+  the configuration file is rejected.
+
+  See also: tune.ssl.keylog
+
 stats calculate-max-counters [on|off]
   Activates or deactivates the calculation of stats max counters. If you
   don't need them, deactivating them may increase performances a bit.
@@ -6023,9 +6049,11 @@ tune.ssl.keylog { on | off }
   care as it will consume more memory per SSL session and could decrease
   performances. This is disabled by default.
 
-  The SSLKEYLOGFILE environment variable is not supported by HAProxy. It
-  causes filesystem access and degrades performance. In master-worker mode,
-  it also leaks file descriptors on each reload. Use "tune.ssl.keylog on"
+  The SSLKEYLOGFILE environment variable is not supported by HAProxy: it
+  causes filesystem access on every SSL handshake and degrades performance,
+  and in master-worker mode it leaks file descriptors on each reload. HAProxy
+  removes it from its environment at startup; see
+  "ssl-workaround-bogus-keylogfile" for the details. Use "tune.ssl.keylog on"
   and the sample fetches below instead.
 
   These sample fetches should be used to generate the SSLKEYLOGFILE that is
index 4e29cebb5e112f6f8dbd4553e1de5bae60e8aae4..d3af63bb03ee87538853e18b158918f3963951c5 100644 (file)
@@ -325,6 +325,7 @@ struct global_ssl {
        int ctx_cache; /* max number of entries in the ssl_ctx cache. */
        int capture_buffer_size; /* Size of the capture buffer. */
        int keylog; /* activate keylog  */
+       int bogus_keylogfile; /* do not remove SSLKEYLOGFILE from the environment */
        uint keyupdate_max; /* max received TLS1.3 KeyUpdates per second per connection (0 == unlimited) */
        int extra_files; /* which files not defined in the configuration file are we looking for */
        int extra_files_noext; /* whether we remove the extension when looking up a extra file */
index e80b9c5042c2375ee93c8b63d2df7764d1357ece..805ce4f7bb9fd9d5c56424671ce9f9c4bbfd72c6 100644 (file)
@@ -2073,7 +2073,7 @@ static struct cfg_kw_list cfg_kws = {ILH, {
        { CFG_GLOBAL, "external-check", cfg_parse_global_external_check },
        { CFG_GLOBAL, "description", cfg_parse_global_node_desc },
        { CFG_GLOBAL, "expose-deprecated-directives", cfg_parse_global_non_std_directives, KWF_DISCOVERY },
-       { CFG_GLOBAL, "expose-experimental-directives", cfg_parse_global_non_std_directives },
+       { CFG_GLOBAL, "expose-experimental-directives", cfg_parse_global_non_std_directives, KWF_DISCOVERY },
        { CFG_GLOBAL, "fd-hard-limit", cfg_parse_global_limits },
        { CFG_GLOBAL, "force-cfg-parser-pause", cfg_parse_global_parser_pause, KWF_EXPERIMENTAL },
        { CFG_GLOBAL, "h2-workaround-bogus-websocket-clients", cfg_parse_global_bool_opts },
index 88ff85625526926f69af049e17f51e29d984779a..1de91bede5e9a1e7a14d3194acf304aae7aceb40 100644 (file)
@@ -522,6 +522,27 @@ static int ssl_parse_global_keylog(char **args, int section_type, struct proxy *
 }
 #endif
 
+/* parse "ssl-workaround-bogus-keylogfile" */
+static int ssl_parse_global_bogus_keylogfile(char **args, int section_type,
+                                             struct proxy *curpx,
+                                             const struct proxy *defpx,
+                                             const char *file, int line,
+                                             char **err)
+{
+       if (too_many_args(1, args, err, NULL))
+               return -1;
+
+       if (strcmp(args[1], "on") == 0)
+               global_ssl.bogus_keylogfile = 1;
+       else if (strcmp(args[1], "off") == 0)
+               global_ssl.bogus_keylogfile = 0;
+       else {
+               memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
+               return -1;
+       }
+       return 0;
+}
+
 /*
  *  parse "tune.ssl.keyupdate-rate-limit" : max received TLS1.3 KeyUpdate
  * messages per second and per connection. 0 disables the limit.
@@ -2732,23 +2753,6 @@ end:
 REGISTER_CONFIG_POST_SECTION("listen",   post_section_frontend_crt_init);
 REGISTER_CONFIG_POST_SECTION("frontend", post_section_frontend_crt_init);
 
-static int cfgparse_ssl_pre_check(void)
-{
-       const char *keylogfile = getenv("SSLKEYLOGFILE");
-
-       if (master || !keylogfile || !*keylogfile)
-               return ERR_NONE;
-
-       ha_warning("The SSLKEYLOGFILE environment variable is not supported by HAProxy. "
-                  "It causes filesystem access and degrades performance. Use 'tune.ssl.keylog on' instead.\n");
-       if (global.mode & MODE_MWORKER)
-               ha_warning("Using SSLKEYLOGFILE in master-worker mode leaks file descriptors on each reload.\n");
-
-       return ERR_WARN;
-}
-
-REGISTER_PRE_CHECK(cfgparse_ssl_pre_check);
-
 static int cfgparse_ssl_post_check(void)
 {
        int err_code = 0;
@@ -2916,6 +2920,7 @@ static struct srv_kw_list srv_kws = { "SSL", { }, {
 INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
 
 static struct cfg_kw_list cfg_kws = {ILH, {
+       { CFG_GLOBAL, "ssl-workaround-bogus-keylogfile", ssl_parse_global_bogus_keylogfile, KWF_EXPERIMENTAL | KWF_DISCOVERY },
        { CFG_GLOBAL, "ca-base",  ssl_parse_global_path_base },
        { CFG_GLOBAL, "crt-base", ssl_parse_global_path_base },
        { CFG_GLOBAL, "key-base", ssl_parse_global_path_base },
index 213f438dd5ec934bf0e063ab9d5fcab89b4685ea..4933ffdd20791270dd2cde4a9e991eb79cee720f 100644 (file)
@@ -3629,6 +3629,14 @@ int main(int argc, char **argv)
                        exit(EXIT_FAILURE);
        }
 
+#ifdef USE_OPENSSL
+       /* Global options are known. Disable OpenSSL's native key logging before
+        * configuration parsing can create SSL contexts, unless explicitly allowed.
+        */
+       if (!global_ssl.bogus_keylogfile)
+               unsetenv("SSLKEYLOGFILE");
+#endif
+
        /* From this stage all runtime modes are known. So let's do below some
         * preparation steps and then let's apply all discovered modes.
         */
@@ -3684,6 +3692,12 @@ int main(int argc, char **argv)
                                ha_alert("Worker failed to restore its env, exiting.\n");
                                exit(EXIT_FAILURE);
                        }
+
+#ifdef USE_OPENSSL
+                       /* restore_env() may have brought SSLKEYLOGFILE back. */
+                       if (!global_ssl.bogus_keylogfile)
+                               unsetenv("SSLKEYLOGFILE");
+#endif
                        setenv("HAPROXY_MWORKER", "1", 1);
 
                        /* restore_env() above has dropped it, put it back so