From: "cron2 (Code Review)" Date: Wed, 12 Nov 2025 10:22:46 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., heiko@...515..., selva.nair@...277..., openvpn-devel@lists.sourceforge.net, frank@...2641..., lstipakov@...277... Message-ID: <1f98798b850be2d505609f585c400aed053f0526-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="U4KmWgnEabg="; 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: d12fk Cc: stipa , selvanair , plaisthos , flichtenheld , openvpn-devel --U4KmWgnEabg= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/13= 07?usp=3Demail ) 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 Acked-by: Lev Stipakov Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1307 Message-Id: <20251112092244.22764-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34= 336.html Signed-off-by: Gert Doering --- M src/openvpnserv/validate.c 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/openvpnserv/validate.c b/src/openvpnserv/validate.c index ddaa381..2dcfe1a 100644 --- a/src/openvpnserv/validate.c +++ b/src/openvpnserv/validate.c @@ -24,8 +24,8 @@ #include #include -#include #include +#include #ifndef HAVE_PATHCCH_ENSURE_TRAILING_SLASH #define PATHCCH_ENSURE_TRAILING_SLASH 0x20 @@ -58,44 +58,31 @@ 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; - WCHAR config_dir[MAX_PATH]; + HRESULT res; + WCHAR config_path[MAX_PATH]; /* fname =3D stdin is special: do not treat it as a relative path */ if (wcscmp(fname, L"stdin") =3D=3D 0) { return FALSE; } - /* 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, _countof(config_path), workdir= , fname); } else { - config_file =3D fname; + res =3D PathCchCanonicalize(config_path, _countof(config_path), fn= ame); } - /* canonicalize config_dir and add trailing slash before comparison */ - HRESULT res =3D PathCchCanonicalizeEx(config_dir, _countof(config_dir)= , s->config_dir, - PATHCCH_ENSURE_TRAILING_SLASH); - - if (res =3D=3D S_OK - && 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: merged Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I0e94068f467f2899daf133b032a785d2d7fc05e4 Gerrit-Change-Number: 1307 Gerrit-PatchSet: 8 Gerrit-Owner: d12fk Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-Reviewer: selvanair Gerrit-Reviewer: stipa Gerrit-CC: openvpn-devel --U4KmWgnEabg= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 sub= mitted this change.

View Change

iservice: validate config path better

Ins= tead of just rejecting any path that contains ".." use some WIN32= API
functions to combine, canonicalize and then check if the resulting<= br>path is located under the config directory. Makes the code prettier
a= nd more correct.

Change-Id: I0e94068f467f2899daf133b032a785d2d7fc05e= 4
Signed-off-by: Heiko Hund <heiko@...2662...>
Acked-by: Lev St= ipakov <lstipakov@...277...>
Gerrit URL: https://gerrit.openvpn.ne= t/c/openvpn/+/1307
Message-Id: <20251112092244.22764-1-gert@...2132...= .>
URL: https://www.mail-archive.com/openvpn-devel@...290...= net/msg34336.html
Signed-off-by: Gert Doering <gert@...1296...>---
M src/openvpnserv/validate.c
1 file changed, 9 insertions(+), 22= deletions(-)

diff --git a/src/openvpnserv/validate.c b/src/openvpnserv/validate.c
index ddaa381..2dcfe1a 100644
--- a/src/openvp= nserv/validate.c
+++ b/src/openvpnserv/validate.c@@ -24,8 +24,8 @@

#include <lm= access.h>
#include <shlwapi.h>
-#include <lm.h>
#= include <pathcch.h>
+#include <lm.h>

#ifndef HAVE= _PATHCCH_ENSURE_TRAILING_SLASH
#define PATHCCH_ENSURE_TRAI= LING_SLASH 0x20
@@ -58,44 +58,31 @@
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 i= f ..\ is in any of the strings
+ * Check that config path is inside config_dir
+ * The logic here is simple: if the p= ath isn't prefixed with config_dir it's rejected
= */
static BOOL
CheckConfigPath(const WCHA= R *workdir, const WCHAR *fname, const settings_t *s)
{
- WCHAR tmp[MAX_PATH];<= /span>
- const WCHAR *confi= g_file =3D NULL;
- W= CHAR config_dir[MAX_PATH];
+ HRESULT res;
+ WCHAR config_path[MAX_PATH];

= /* fname =3D stdin is special: do not treat it as a relative path */
if (wcscmp(fname, L"stdin") =3D=3D 0)
<= span> {

return FALSE;
}
- /* convert fname to= full path */
+ /*= convert fname to full canonical path */
if (PathIsRel= ativeW(fname))
{
- swprintf(tmp, _countof(tmp), L"%ls\\%ls",= workdir, fname);
- = config_file =3D tmp;
+ res =3D PathCchCombine(config_path, _countof(config_path), wor= kdir, fname);
}
else
{

- con= fig_file =3D fname;
+= res =3D PathCchCanonicalize(config_path, _countof(config_path), fna= me);
}

- /* canonicalize config_dir and add trailing sla= sh before comparison */
- HRESULT res =3D PathCchCanonicalizeEx(config_dir, _countof(config_dir= ), s->config_dir,
- = PATHCCH_ENSURE_TRAILING_SLASH);
-
- if (res =3D=3D S_OK
- && wcsncmp(config_dir, conf= ig_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;<= br> }



=

To view, visit change 1307. To unsubscribe, or for help writing mail f= ilters, visit se= ttings.

<= div itemscope itemprop=3D"action" itemtype=3D"http://schema.org/ViewAction"= >
Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I0e94068f467f2899daf133b032a= 785d2d7fc05e4
Gerrit-Change-Number: 1307
Gerrit-PatchSet: 8
Gerrit-Owner: d12fk <heiko@...515...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-Reviewer: selvanair <selva.nair@...1665...= 77...>
Gerrit-Reviewer: stipa <lstipakov@...277...= >
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --U4KmWgnEabg=--