From: "stipa (Code Review)" Date: Thu, 30 Oct 2025 10:09:31 +0000 References: Reply-To: lstipakov@...277..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="ESAPX2VYH+c="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: win32: ensure plugin dir has the trailing slash List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --ESAPX2VYH+c= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello plaisthos, flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1332?usp=3Demail to review the following change. Change subject: win32: ensure plugin dir has the trailing slash ...................................................................... win32: ensure plugin dir has the trailing slash This prevents loading plugins from the directories which share initial characters with the trusted path. Reported-by: Joshua Rogers Found-by: ZeroPath (https://zeropath.com/) Change-Id: I5ea90594493ab5cb858f7495f773e080b379e8e8 Signed-off-by: Lev Stipakov --- M CMakeLists.txt M src/openvpn/win32.c 2 files changed, 29 insertions(+), 11 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/32/1332/1 diff --git a/CMakeLists.txt b/CMakeLists.txt index c9301e6..6888de3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -245,6 +245,9 @@ check_symbol_exists(chsize io.h HAVE_CHSIZE) check_symbol_exists(getrlimit sys/resource.h HAVE_GETRLIMIT) +# Some old versions of mingw does not have PATHCCH_OPTIONS enums -- add a = check +check_symbol_exists(PATHCCH_ENSURE_TRAILING_SLASH pathcch.h HAVE_PATHCCH_E= NSURE_TRAILING_SLASH) + # Some OS (e.g. FreeBSD) need some basic headers to allow # including network headers set(NETEXTRA sys/types.h) @@ -338,7 +341,7 @@ if (WIN32) target_link_libraries(${target} PUBLIC ws2_32.lib crypt32.lib fwpuclnt.lib iphlpapi.lib - wininet.lib setupapi.lib rpcrt4.lib wtsapi32.lib ncrypt.li= b bcrypt.lib) + wininet.lib setupapi.lib rpcrt4.lib wtsapi32.lib ncrypt.li= b bcrypt.lib pathcch.lib) endif () endif () diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index 3c11ec3..6aa9463 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -51,6 +51,12 @@ #include "wfp_block.h" +#include + +#ifndef HAVE_PATHCCH_ENSURE_TRAILING_SLASH +#define PATHCCH_ENSURE_TRAILING_SLASH 0x20 +#endif + /* * WFP handle */ @@ -1552,12 +1558,12 @@ return false; } - WCHAR plugin_dir[MAX_PATH] =3D { 0 }; + WCHAR plugin_dir_reg[MAX_PATH] =3D { 0 }; /* Attempt to retrieve the trusted plugin directory path from the regi= stry, * using installation path as a fallback */ - if (!get_openvpn_reg_value(L"plugin_dir", plugin_dir, _countof(plugin_= dir)) - && !get_openvpn_reg_value(NULL, plugin_dir, _countof(plugin_dir))) + if (!get_openvpn_reg_value(L"plugin_dir", plugin_dir_reg, _countof(plu= gin_dir_reg)) + && !get_openvpn_reg_value(NULL, plugin_dir_reg, _countof(plugin_di= r_reg))) { msg(M_WARN, "Installation path could not be determined."); } @@ -1569,26 +1575,35 @@ msg(M_NONFATAL | M_ERRNO, "Failed to get system directory."); } - if ((wcslen(plugin_dir) =3D=3D 0) && (wcslen(system_dir) =3D=3D 0)) + if ((wcslen(plugin_dir_reg) =3D=3D 0) && (wcslen(system_dir) =3D=3D 0)) { return false; } - WCHAR normalized_plugin_dir[MAX_PATH] =3D { 0 }; + WCHAR plugin_dir[MAX_PATH] =3D { 0 }; - /* Normalize the plugin dir */ - if (wcslen(plugin_dir) > 0) + /* normalize and canonicalize the plugin dir and add trailing slash */ + if (wcslen(plugin_dir_reg) > 0) { - if (!GetFullPathNameW(plugin_dir, MAX_PATH, normalized_plugin_dir,= NULL)) + WCHAR normalized_plugin_dir[MAX_PATH] =3D { 0 }; + if (!GetFullPathNameW(plugin_dir_reg, MAX_PATH, normalized_plugin_= dir, NULL)) { msg(M_NONFATAL | M_ERRNO, "Failed to normalize plugin dir."); + } + + HRESULT res =3D PathCchCanonicalizeEx(plugin_dir, _countof(plugin_= dir), normalized_plugin_dir, + PATHCCH_ENSURE_TRAILING_SLASH); + if (res !=3D S_OK) + { + /* doc says we cannot rely on GetLastError() */ + msg(M_NONFATAL, "Failed to canonicalize plugin dir."); return false; } } /* Check if the plugin path resides within the plugin/install director= y */ - if ((wcslen(normalized_plugin_dir) > 0) - && (wcsnicmp(normalized_plugin_dir, plugin_path, wcslen(normalized= _plugin_dir)) =3D=3D 0)) + if ((wcslen(plugin_dir) > 0) + && (wcsnicmp(plugin_dir, plugin_path, wcslen(plugin_dir)) =3D=3D 0= )) { return true; } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1332?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newchange Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I5ea90594493ab5cb858f7495f773e080b379e8e8 Gerrit-Change-Number: 1332 Gerrit-PatchSet: 1 Gerrit-Owner: stipa Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld --ESAPX2VYH+c= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: flichtenheld, plaisthos.

stipa would like plaisthos and flichtenheld to review t= his change.

V= iew Change

win32: ensure plugin dir has the trailing s=
lash

This prevents loading plugins from the directories
which sha= re initial characters with the trusted path.

Reported-by: Joshua Rog= ers <contact@...2949...>
Found-by: ZeroPath (https://zeropath.com/= )

Change-Id: I5ea90594493ab5cb858f7495f773e080b379e8e8
Signed-off= -by: Lev Stipakov <lev@...515...>
---
M CMakeLists.txt
M src= /openvpn/win32.c
2 files changed, 29 insertions(+), 11 deletions(-)
<= br>
git pull ssh://gerrit.openvpn.net:29418/openvpn refs=
/changes/32/1332/1
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c9301e6..6888de3 100644
--- a/CMakeLists= .txt
+++ b/CMakeLists.txt
@@ -245,6 +245,9 = @@
check_symbol_exists(chsize io.h HAVE_CHSIZE)
= check_symbol_exists(getrlimit sys/resource.h HAVE_GETRLIMIT)<= br>
+# Some ol= d versions of mingw does not have PATHCCH_OPTIONS enums -- add a check
+check_symbol_exists(PATH= CCH_ENSURE_TRAILING_SLASH pathcch.h HAVE_PATHCCH_ENSURE_TRAILING_SLASH)
+
# Som= e OS (e.g. FreeBSD) need some basic headers to allow
# inc= luding network headers
set(NETEXTRA sys/types.h)@@ -338,7 +341,7 @@
if (WIN32)
target_link_libraries(${target} PUBLIC

= ws2_32.lib crypt32.lib fwpuclnt.lib iphlpapi.lib
- wininet.lib setupa= pi.lib rpcrt4.lib wtsapi32.lib ncrypt.lib bcrypt.lib)

+ wininet.lib setupapi.lib = rpcrt4.lib wtsapi32.lib ncrypt.lib bcrypt.lib pathcch.lib)
= endif ()

endif ()diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c
index 3c11ec3..6aa9463 100644

--- a/src/openvpn/win32.c<= /span>
+++ b/src/openvpn/win32.c
@@ -51,6 +51,12 @= @

#include "wfp_block.h"

+#includ= e <pathcch.h>
+=
+#ifndef HAVE_PATHCC= H_ENSURE_TRAILING_SLASH
+#define PATHCCH_ENSURE_TRAILING_SLASH 0x20
+#endif
+
/*
* WFP handle */
@@ -1552,12 +1558,12 @@
= return false;
}

- WCHAR plugin_dir[MAX_PATH] =3D { 0 }= ;
+ WCHAR plugin_d= ir_reg[MAX_PATH] =3D { 0 };

/* Attem= pt to retrieve the trusted plugin directory path from the registry,<= br> * using installation path as a fallback */
- if (!get_openvpn_reg_value(L"p= lugin_dir", plugin_dir, _countof(plugin_dir))
- && !get_openvpn_reg_value(NULL= , plugin_dir, _countof(plugin_dir)))
+ if (!get_openvpn_reg_value(L"plugin_dir", pl= ugin_dir_reg, _countof(plugin_dir_reg))
+ && !get_openvpn_reg_value(NULL, plugin_= dir_reg, _countof(plugin_dir_reg)))
{
= msg(M_WARN, "Installation path could not be determined."= );
}
@@ -1569,26 +1575,35 @@ msg(M_NONFATAL | M_ERRNO, "Failed to get system direct= ory.");
}

- if ((wcslen(plugin_dir) =3D=3D 0) &= & (wcslen(system_dir) =3D=3D 0))
+ if ((wcslen(plugin_dir_reg) =3D=3D 0) && (wcsl= en(system_dir) =3D=3D 0))
{
r= eturn false;
}

- WCHAR normalized_plugin_dir[MAX_PATH] = =3D { 0 };
+ WCHAR= plugin_dir[MAX_PATH] =3D { 0 };

- /* Normalize the plugin dir */
<= span style=3D"color: hsl(0, 100%, 40%);">- if (wcslen(plugin_dir) > 0= )

+ /* normalize a= nd canonicalize the plugin dir and add trailing slash */
+ if (wcslen(plugin_dir_reg) > 0)=
{
- if (!GetFullPathNameW(plugin_dir, MAX_PATH, normalized_plugin_dir= , NULL))
+ WCH= AR normalized_plugin_dir[MAX_PATH] =3D { 0 };
+ if (!GetFullPathNameW(plugin_dir_reg, MAX= _PATH, normalized_plugin_dir, NULL))
{
<= span> msg(M_NONFATAL | M_ERRNO, "Failed to normalize plugi= n dir.");

+ = }
+
+ HRESULT res =3D PathCchCan= onicalizeEx(plugin_dir, _countof(plugin_dir), normalized_plugin_dir,=
+ = PATHCCH_ENSURE_TRAILING_SLASH);
+ if (res !=3D S_OK)
+ {
+ /* doc says we cannot rely on GetLas= tError() */
+ = msg(M_NONFATAL, "Failed to canonicalize plugin dir.");=
return false;
}
<= span> }

/* Check if the plugin p= ath resides within the plugin/install directory */
- if ((wcslen(normalized_plugin_dir) > 0)=
- && (w= csnicmp(normalized_plugin_dir, plugin_path, wcslen(normalized_plugin_dir)) = =3D=3D 0))
+ if ((= wcslen(plugin_dir) > 0)
+ && (wcsnicmp(plugin_dir, plugin_path, wcslen(plugin_= dir)) =3D=3D 0))
{
return tru= e;
}

To view, visi= t change= 1332. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I5ea90594493ab5cb858f7495f77= 3e080b379e8e8
Gerrit-Change-Number: 1332
Gerrit-PatchSet: 1
Gerrit-Owner: stipa <lstipakov@...277...>= ;
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
= --ESAPX2VYH+c=--