Skip to contentSkip to conversation

win32: Enforce loading of plugins from a trusted directory

PATCH In review Lev Stipakov 2 participants 2 messages Mar 19, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 win32: Enforce loading of plugins from a trusted directory

Currently, there's a risk associated with allowing plugins to be loaded from any location. This update ensures plugins are only loaded from a trusted directory, which is either:

- HKLM\SOFTWARE\OpenVPN\plugin_dir (or if the key is missing, then HKLM\SOFTWARE\OpenVPN, which is installation directory)

- System directory

Loading from UNC paths is disallowed.

Note: This change affects only Windows environments.

CVE: 2024-27903

Change-Id I154a4aaad9242c9253a64312a14c5fd2ea95f40d
---
 src/openvpn/plugin.c | 18 +++++++++--
 src/openvpn/win32.c  | 77 +++++++++++++++++++++++++++++++++++++-------
 src/openvpn/win32.h  | 27 ++++++++++++++++
 3 files changed, 107 insertions(+), 15 deletions(-)
src/openvpn/plugin.c +15 -3
index b4d4a986..3d62eced 100644
@@ -277,11 +277,23 @@ plugin_init_item(struct plugin *p, const struct plugin_option *o)
 
 #else  /* ifndef _WIN32 */
 
-    rel = !platform_absolute_pathname(p->so_pathname);
-    p->module = LoadLibraryW(wide_string(p->so_pathname, &gc));
+    WCHAR *wpath = wide_string(p->so_pathname, &gc);
+    WCHAR normalized_plugin_path[MAX_PATH] = {0};
+    /* Normalize the plugin path, converting any relative paths to absolute paths. */
+    if (!GetFullPathNameW(wpath, MAX_PATH, normalized_plugin_path, NULL))
+    {
+        msg(M_ERR, "PLUGIN_INIT: could not load plugin DLL: %ls. Failed to normalize plugin path.", wpath);
+    }
+
+    if (!plugin_in_trusted_dir(normalized_plugin_path))
+    {
+        msg(M_FATAL, "PLUGIN_INIT: could not load plugin DLL: %ls. The DLL is not in a trusted directory.", normalized_plugin_path);
+    }
+
+    p->module = LoadLibraryW(normalized_plugin_path);
     if (!p->module)
     {
-        msg(M_ERR, "PLUGIN_INIT: could not load plugin DLL: %s", p->so_pathname);
+        msg(M_ERR, "PLUGIN_INIT: could not load plugin DLL: %ls", normalized_plugin_path);
     }
 
 #define PLUGIN_SYM(var, name, flags) dll_resolve_symbol(p->module, (void *)&p->var, name, p->so_pathname, flags)
src/openvpn/win32.c +65 -12

103 lines not on this page · raw message

src/openvpn/win32.h +27 -0
index aa8513b2..2f147c09 100644
@@ -330,5 +330,32 @@ openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const for
 /* Sleep that can be interrupted by signals and exit event */
 void win32_sleep(const int n);
 
+/**
+ * @brief Fetches a registry value for OpenVPN registry key.
+ *
+ * @param key Registry value name to fetch.
+ * @param value Buffer to store the fetched string value.
+ * @param size Size of `value` buffer in bytes.
+ * @return `true` if successful, `false` otherwise.
+ */
+bool
+get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size);
+
+/**
+ * @brief Checks if a plugin is located in a trusted directory.
+ *
+ * Verifies the plugin's path against a trusted directory, which is:
+ *
+ * - "plugin_dir" registry value or installation path, if the registry key is missing
+ * - system directory
+ *
+ * UNC paths are explicitly disallowed.
+ *
+ * @param plugin_path Normalized path to the plugin.
+ * @return \c true if the plugin is in a trusted directory and not a UNC path; \c false otherwise.
+ */
+bool
+plugin_in_trusted_dir(const WCHAR *plugin_path);
+
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
Signature · 1 line
2.42.0.windows.2
#1 lore raw

Thanks for that.

This patch was sent "with ACK included" to the openvpn-devel@ list because it was developed under embargo (CVE), and reviewed and ACKed in a closed group. I have verified that this patch is identical to the "v4 one" that Selva and the original reporter saw and ACKed.

This is related to plugin loading on windows only. We have discussed the topic of "restricting plugin loading on other platforms" but it's more complex to tackle (it starts with "there is no central registry to put restrictions into", but goes on to "on unix, openvpn runs as root anyway, so we expect this to be done by admins who spend some thought on what scripts and plugin they call, and from which paths") - so we haven't done anything there yet.

I have test built this on MinGW/Ubuntu, just for completeness, and via GHA. Haven't tested the result myself (no plugin setup on windows).

(I do have a few gripes, but these are more cosmetical - like "make get_openvpn_reg_value() static", and "wrap the long if() condition at the '&&', not in the middle of the function call" - but these are all not important for the functionality)

Your patch has been applied to the master, release/2.6 and release/2.5 branch (security relevant bugfix).

commit aaea545d8a940f761898d736b68bcb067d503b1d (master) commit 05d321ef980734478a86c5241dad7ba26a748a2f (release/2.6) commit 30bddb1a5426523ef1d61c8a5df2c613ba2a47d3 (release/2.5)

Author: Lev Stipakov
Date:   Tue Mar 19 15:53:45 2024 +0200
     win32: Enforce loading of plugins from a trusted directory

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Selva Nair <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg28416.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering