commit d1ab7045e989ae27a111fa98f12d117f03a9f0da
parent 09d145d3416441002ec5ba4e352498612c678939
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Thu, 18 Dec 2025 17:16:28 +0100
Added a workaround for OpenSSL leakage
Diffstat:
10 files changed, 131 insertions(+), 0 deletions(-)
diff --git a/src/incl_priv/mhd_sys_options.h b/src/incl_priv/mhd_sys_options.h
@@ -643,6 +643,16 @@
# define MHD_AUTH_DIGEST_DEF_MAX_NC 1000
#endif /* ! MHD_AUTH_DIGEST_DEF_MAX_NC */
+#ifndef mhd_HAVE_TLS_THREAD_CLEANUP
+# ifdef MHD_SUPPORT_OPENSSL
+/**
+ * If defined then mhd_tls_thread_cleanup() is a real function.
+ * If not defined then mhd_tls_thread_cleanup() is an empty macro
+ */
+# define mhd_HAVE_TLS_THREAD_CLEANUP 1
+# endif
+#endif
+
/* Eclipse parse compatibility */
#ifdef __CDT_PARSER__
# undef MHD_NORETURN_
diff --git a/src/mhd2/daemon_start.c b/src/mhd2/daemon_start.c
@@ -1849,7 +1849,10 @@ daemon_deinit_tls (struct MHD_Daemon *restrict d)
mhd_assert (d->dbg.tls_inited);
#ifdef MHD_SUPPORT_HTTPS
if (NULL != d->tls)
+ {
+ mhd_tls_thread_cleanup (d->tls);
mhd_tls_daemon_deinit (d->tls);
+ }
#elif defined(NDEBUG)
(void) d; /* Mute compiler warning */
#endif
diff --git a/src/mhd2/events_process.c b/src/mhd2/events_process.c
@@ -86,6 +86,10 @@
# include "upgrade_proc.h"
#endif /* MHD_SUPPORT_UPGRADE */
+#ifdef MHD_SUPPORT_HTTPS
+# include "mhd_tls_funcs.h"
+#endif
+
#ifdef MHD_SUPPORT_HTTP2
# include "h2/h2_comm.h"
#endif
@@ -1893,6 +1897,11 @@ mhd_worker_all_events (void *cls)
}
mhd_daemon_close_all_conns (d);
+#ifdef MHD_SUPPORT_HTTPS
+ if (mhd_D_HAS_TLS (d))
+ mhd_tls_thread_cleanup (d->tls);
+#endif /* MHD_SUPPORT_HTTPS */
+
return (mhd_THRD_RTRN_TYPE) 0;
}
@@ -1960,6 +1969,12 @@ mhd_worker_listening_only (void *cls)
"The daemon thread is stopping, but termination has " \
"not been requested by the daemon.");
}
+
+#ifdef MHD_SUPPORT_HTTPS
+ if (mhd_D_HAS_TLS (d))
+ mhd_tls_thread_cleanup (d->tls);
+#endif /* MHD_SUPPORT_HTTPS */
+
return (mhd_THRD_RTRN_TYPE) 0;
}
@@ -1969,6 +1984,12 @@ mhd_worker_connection (void *cls)
{
if (cls) // TODO: Implement
MHD_PANIC ("Not yet implemented");
+
+#if 0 // def MHD_SUPPORT_HTTPS
+ if (mhd_D_HAS_TLS (d))
+ mhd_tls_thread_cleanup (d->tls);
+#endif /* MHD_SUPPORT_HTTPS */
+
return (mhd_THRD_RTRN_TYPE) 0;
}
diff --git a/src/mhd2/mhd_tls_funcs.h b/src/mhd2/mhd_tls_funcs.h
@@ -114,6 +114,15 @@
#define mhd_tls_daemon_deinit(d_tls) \
mhd_TLS_FUNC (_daemon_deinit)((d_tls))
+/**
+ * Perform clean-up of TLS resources before thread closing.
+ * Must be called before thread is closed, after any use of TLS functions
+ * in the thread, but before de-initialisation of daemon's TLS data.
+ * @param d_tls the pointer to the daemon's TLS settings
+ */
+#define mhd_tls_thread_cleanup(d_tls) \
+ mhd_TLS_FUNC (_thread_cleanup)((d_tls))
+
/* ** Connection initialisation / de-initialisation ** */
diff --git a/src/mhd2/tls_gnu_funcs.h b/src/mhd2/tls_gnu_funcs.h
@@ -155,6 +155,13 @@ MHD_INTERNAL void
mhd_tls_gnu_daemon_deinit (struct mhd_TlsGnuDaemonData *restrict d_tls)
MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
+/**
+ * Perform clean-up of TLS resources before thread closing.
+ * Must be called before thread is closed, after any use of TLS functions
+ * in the thread, but before de-initialisation of daemon's TLS data.
+ * @param d_tls the pointer to the daemon's TLS settings
+ */
+#define mhd_tls_gnu_thread_cleanup(d_tls) ((void) 0)
/* ** Connection initialisation / de-initialisation ** */
diff --git a/src/mhd2/tls_mbed_funcs.h b/src/mhd2/tls_mbed_funcs.h
@@ -154,6 +154,13 @@ MHD_INTERNAL void
mhd_tls_mbed_daemon_deinit (struct mhd_TlsMbedDaemonData *restrict d_tls)
MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
+/**
+ * Perform clean-up of TLS resources before thread closing.
+ * Must be called before thread is closed, after any use of TLS functions
+ * in the thread, but before de-initialisation of daemon's TLS data.
+ * @param d_tls the pointer to the daemon's TLS settings
+ */
+#define mhd_tls_mbed_thread_cleanup(d_tls) ((void) 0)
/* ** Connection initialisation / de-initialisation ** */
diff --git a/src/mhd2/tls_multi_funcs.c b/src/mhd2/tls_multi_funcs.c
@@ -424,6 +424,44 @@ mhd_tls_multi_daemon_deinit (struct mhd_TlsMultiDaemonData *restrict d_tls)
}
+#ifdef mhd_HAVE_TLS_THREAD_CLEANUP
+/**
+ * Perform clean-up of TLS resources before thread closing.
+ * Must be called before thread is closed, after any use of TLS functions
+ * in the thread, but before de-initialisation of daemon's TLS data.
+ * @param d_tls the pointer to the daemon's TLS settings
+ */
+MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_
+MHD_FN_PAR_INOUT_ (1) void
+mhd_tls_multi_thread_cleanup (struct mhd_TlsMultiDaemonData *restrict d_tls)
+{
+ switch (d_tls->choice)
+ {
+#ifdef MHD_SUPPORT_GNUTLS
+ case mhd_TLS_MULTI_ROUTE_GNU:
+ mhd_tls_gnu_thread_cleanup (d_tls->data.gnutls);
+ break;
+#endif
+#ifdef MHD_SUPPORT_OPENSSL
+ case mhd_TLS_MULTI_ROUTE_OPEN:
+ mhd_tls_open_thread_cleanup (d_tls->data.openssl);
+ break;
+#endif
+#ifdef MHD_SUPPORT_MBEDTLS
+ case mhd_TLS_MULTI_ROUTE_MBED:
+ mhd_tls_mbed_thread_cleanup (d_tls->data.mbedtls);
+ break;
+#endif
+ case mhd_TLS_MULTI_ROUTE_NONE:
+ default:
+ mhd_UNREACHABLE ();
+ break;
+ }
+}
+
+
+#endif /* mhd_HAVE_TLS_THREAD_CLEANUP */
+
/* ** Connection initialisation / de-initialisation ** */
MHD_INTERNAL size_t
diff --git a/src/mhd2/tls_multi_funcs.h b/src/mhd2/tls_multi_funcs.h
@@ -141,6 +141,20 @@ MHD_INTERNAL void
mhd_tls_multi_daemon_deinit (struct mhd_TlsMultiDaemonData *restrict d_tls)
MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
+#ifdef mhd_HAVE_TLS_THREAD_CLEANUP
+/**
+ * Perform clean-up of TLS resources before thread closing.
+ * Must be called before thread is closed, after any use of TLS functions
+ * in the thread, but before de-initialisation of daemon's TLS data.
+ * @param d_tls the pointer to the daemon's TLS settings
+ */
+MHD_INTERNAL void
+mhd_tls_multi_thread_cleanup (struct mhd_TlsMultiDaemonData *restrict d_tls)
+MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
+#else /* ! mhd_HAVE_TLS_THREAD_CLEANUP */
+# define mhd_tls_multi_thread_cleanup(d_tls) ((void) 0)
+#endif /* ! mhd_HAVE_TLS_THREAD_CLEANUP */
+
/* ** Connection initialisation / de-initialisation ** */
diff --git a/src/mhd2/tls_open_funcs.c b/src/mhd2/tls_open_funcs.c
@@ -1343,6 +1343,14 @@ mhd_tls_open_daemon_deinit (struct mhd_TlsOpenDaemonData *restrict d_tls)
}
+MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_
+MHD_FN_PAR_INOUT_ (1) void
+mhd_tls_open_thread_cleanup (struct mhd_TlsOpenDaemonData *restrict d_tls)
+{
+ OPENSSL_thread_stop_ex (d_tls->libctx);
+}
+
+
/* ** Connection initialisation / de-initialisation ** */
MHD_INTERNAL size_t
diff --git a/src/mhd2/tls_open_funcs.h b/src/mhd2/tls_open_funcs.h
@@ -51,6 +51,11 @@
#error This header can be used only if OpenSSL is enabled
#endif
+/* Sanity check */
+#ifndef mhd_HAVE_TLS_THREAD_CLEANUP
+#error mhd_HAVE_TLS_THREAD_CLEANUP macro must be defined
+#endif
+
#include "sys_bool_type.h"
#include "sys_base_types.h"
@@ -139,6 +144,15 @@ MHD_INTERNAL void
mhd_tls_open_daemon_deinit (struct mhd_TlsOpenDaemonData *restrict d_tls)
MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
+/**
+ * Perform clean-up of TLS resources before thread closing.
+ * Must be called before thread is closed, after any use of TLS functions
+ * in the thread, but before de-initialisation of daemon's TLS data.
+ * @param d_tls the pointer to the daemon's TLS settings
+ */
+MHD_INTERNAL void
+mhd_tls_open_thread_cleanup (struct mhd_TlsOpenDaemonData *restrict d_tls)
+MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
/* ** Connection initialisation / de-initialisation ** */