From: "d12fk (Code Review)" Date: Sun, 26 Oct 2025 22:14:23 +0000 References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <6218691eb0a9ad6574db18220d3c25eec9b57096-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="Crx8zG/jSzM="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: iservice: validate config path better List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --Crx8zG/jSzM= 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/+/1307?usp=3Demail to review the following change. Change subject: iservice: validate config path better ...................................................................... iservice: validate config path better Instead of just rejecting any path that contains ".." use some WIN32 API functions to combine, canonicalize and then check if the resulting path is located under the config directory. Makes the code prettier and more correct. Change-Id: I0e94068f467f2899daf133b032a785d2d7fc05e4 Signed-off-by: Heiko Hund --- M src/openvpnserv/CMakeLists.txt M src/openvpnserv/validate.c 2 files changed, 10 insertions(+), 19 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/07/1307/1 diff --git a/src/openvpnserv/CMakeLists.txt b/src/openvpnserv/CMakeLists.txt index 340b904..a30b164 100644 --- a/src/openvpnserv/CMakeLists.txt +++ b/src/openvpnserv/CMakeLists.txt @@ -31,7 +31,7 @@ ) target_link_libraries(openvpnserv advapi32.lib userenv.lib iphlpapi.lib fwpuclnt.lib rpcrt4.lib - shlwapi.lib netapi32.lib ws2_32.lib ntdll.lib ole32.lib) + shlwapi.lib netapi32.lib ws2_32.lib ntdll.lib ole32.lib pathcch.lib) if (MINGW) target_compile_options(openvpnserv PRIVATE -municode) target_link_options(openvpnserv PRIVATE -municode) diff --git a/src/openvpnserv/validate.c b/src/openvpnserv/validate.c index 59d5b86..675f8a8 100644 --- a/src/openvpnserv/validate.c +++ b/src/openvpnserv/validate.c @@ -24,6 +24,7 @@ #include #include +#include #include static const WCHAR *white_list[] =3D { @@ -53,36 +54,26 @@ static PTOKEN_GROUPS GetTokenGroups(const HANDLE token); /* - * Check workdir\fname is inside config_dir - * The logic here is simple: we may reject some valid paths if ..\ is in a= ny of the strings + * Check that config path is inside config_dir + * The logic here is simple: if the path isn't prefixed with config_dir it= 's rejected */ static BOOL CheckConfigPath(const WCHAR *workdir, const WCHAR *fname, const settings_t= *s) { - WCHAR tmp[MAX_PATH]; - const WCHAR *config_file =3D NULL; - const WCHAR *config_dir =3D NULL; + HRESULT res; + WCHAR config_path[MAX_PATH]; - /* convert fname to full path */ + /* convert fname to full canonical path */ if (PathIsRelativeW(fname)) { - swprintf(tmp, _countof(tmp), L"%ls\\%ls", workdir, fname); - config_file =3D tmp; + res =3D PathCchCombine(config_path, sizeof(config_path), workdir, = fname); } else { - config_file =3D fname; + res =3D PathCchCanonicalize(config_path, sizeof(config_path), fnam= e); } - config_dir =3D s->config_dir; - - if (wcsncmp(config_dir, config_file, wcslen(config_dir)) =3D=3D 0 - && wcsstr(config_file + wcslen(config_dir), L"..") =3D=3D NULL) - { - return TRUE; - } - - return FALSE; + return res =3D=3D S_OK && wcsncmp(config_path, s->config_dir, wcslen(s= ->config_dir)) =3D=3D 0; } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1307?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: I0e94068f467f2899daf133b032a785d2d7fc05e4 Gerrit-Change-Number: 1307 Gerrit-PatchSet: 1 Gerrit-Owner: d12fk Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld --Crx8zG/jSzM= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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

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

V= iew Change

iservice: validate config path better
Instead of just rejecting any path that contains ".." use some = WIN32 API
functions to combine, canonicalize and then check if the resul= ting
path is located under the config directory. Makes the code prettier=
and more correct.

Change-Id: I0e94068f467f2899daf133b032a785d2d7= fc05e4
Signed-off-by: Heiko Hund <heiko@...2662...>
---
M sr= c/openvpnserv/CMakeLists.txt
M src/openvpnserv/validate.c
2 files cha= nged, 10 insertions(+), 19 deletions(-)

git pull=
 ssh://gerrit.openvpn.net:29418/openvpn refs/changes/07/1307/1
diff=
 --git a/src/openvpnserv/CMakeLists.txt b/src/openvpnserv/CMakeLists.txt
index 340b904..a30b164 100644
--- a/src/openv= pnserv/CMakeLists.txt
+++ b/src/openvpnserv/CMakeLists.txt<= /span>
@@ -31,7 +31,7 @@
)
t= arget_link_libraries(openvpnserv
advapi32.lib userenv.= lib iphlpapi.lib fwpuclnt.lib rpcrt4.lib
- shlwapi.lib netapi32.lib ws2_32.lib ntdll.lib ole32.= lib)
+ shlwapi.lib= netapi32.lib ws2_32.lib ntdll.lib ole32.lib pathcch.lib)
= if (MINGW)
target_compile_options(openvpnserv PRIVATE = -municode)
target_link_options(openvpnserv PRIVATE -mu= nicode)
diff --git a/src/openvpnserv/validate.c b/src/openv= pnserv/validate.c
index 59d5b86..675f8a8 100644
<= span>--- a/src/openvpnserv/validate.c

+++ b/src/openvpnserv= /validate.c
@@ -24,6 +24,7 @@

<= span> #include <lmaccess.h>

#include <shlwapi.h&g= t;
+#include <path= cch.h>
#include <lm.h>
static const WCHAR *white_list[] =3D {
@@ -53,36 += 54,26 @@
static PTOKEN_GROUPS GetTokenGroups(const HANDLE = token);

/*
- * Check workdir\fname is inside config_dir<= br>- * The logic here is simple: = we may reject some valid paths if ..\ is in any of the strings
+ * Check that config path is ins= ide config_dir
+ * Th= e logic here is simple: if the path isn't prefixed with config_dir it&#= 39;s rejected
*/
static BOOL
<= span> CheckConfigPath(const WCHAR *workdir, const WCHAR *fname, const setti= ngs_t *s)
{
- WCHAR tmp[MAX_PATH];
- const WCHAR *config_file =3D NULL;
- const WCHAR *config_dir =3D NULL;
= + HRESULT res;
+ WCHAR config_path[MAX_PATH];<= /span>

- = /* convert fname to full path */
+ /* convert fname to full canonical path */
if (PathIsRelativeW(fname))
{
- swprintf(tmp, _countof(tmp), L&q= uot;%ls\\%ls", workdir, fname);
- config_file =3D tmp;
+ res =3D PathCchCombine(config_path, sizeof(= config_path), workdir, fname);
}
= else
{
- config_file =3D fname;
+ res =3D PathCchCanonicalize(config_path, sizeof(co= nfig_path), fname);
}

- config_dir =3D s->config_dir;=
-
- if (wcsncmp(config_dir, config_file, w= cslen(config_dir)) =3D=3D 0
- && wcsstr(config_file + wcslen(config_dir), L"..= ") =3D=3D NULL)
- = {
- return TR= UE;
- }
-
- return FALSE;
+ return res =3D=3D S_OK && wcsncmp(config_path= , s->config_dir, wcslen(s->config_dir)) =3D=3D 0;
}<= /span>



To = view, visit change 1307. To unsubscribe, or for help writing mail filters, visi= t settings.<= /p>

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I0e94068f467f2899daf133b032a= 785d2d7fc05e4
Gerrit-Change-Number: 1307
Gerrit-PatchSet: 1
Gerrit-Owner: d12fk <heiko@...515...>
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...>
= --Crx8zG/jSzM=--