Skip to contentSkip to conversation

Swap the order of checks for validating interactive service user

PATCH Superseded [email protected]... 3 participants 10 messages Jan 30, 2020 – Feb 10, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Swap the order of checks for validating interactive service user

From: Selva Nair <[email protected]...>

Check the config file location and command line options first and membership in OpenVPNAdministrators group after that as the latter could be a slow process for active directory users.

When connection to domain controllers is poor or unavailable, checking the group membership is slow and causes timeouts in the GUI (Trac 1051). However, in cases where the config is in the global directory, no group membership check should be required. The re-ordering here avoids the redundant check in such cases.

In addition to this, its also proposed to improve the timeout handling in the GUI, but this change is still useful as it should completely eliminate the timeout issue for many users.

Also see: https://github.com/OpenVPN/openvpn-gui/issues/332

---
 src/openvpnserv/interactive.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/openvpnserv/interactive.c +2 -2
index 6e72a14..dafd5c6 100644
@@ -1581,8 +1581,8 @@ RunOpenvpn(LPVOID p)
     }
 
     /* Check user is authorized or options are white-listed */
-    if (!IsAuthorizedUser(ovpn_user->User.Sid, imp_token, settings.ovpn_admin_group)
-        && !ValidateOptions(pipe, sud.directory, sud.options))
+    if (!ValidateOptions(pipe, sud.directory, sud.options)
+        && !IsAuthorizedUser(ovpn_user->User.Sid, imp_token, settings.ovpn_admin_group)
     {
         goto out;
     }
Signature · 1 line
2.1.4
#1 lore raw

Hi,

+ if (!ValidateOptions(pipe, sud.directory, sud.options)

Quoted 4 lines

+ && !IsAuthorizedUser(ovpn_user->User.Sid, imp_token,
settings.ovpn_admin_group)

     {

Closing parenthesis is missing:

Quoted 1 line

C:\Users\lev\Projects\openvpn\src\openvpnserv\interactive.c(1586,5): error

C2143: syntax error: missing ')' before '{'

Also it is probably just me, but took me a while to figure out what that code is doing and why.

Could you slightly improve the comment above lines you touch ("Check user is authorized or options are white-listed") and mention something like
"Non-authorized users are allowed to use only white-listed options and have config only in global openvpn config directory"?

When I started to debug it, I realized that I have to be authorized user when config
is in my current user's directory (C:\Users\lev\OpenVPN\config) and not in "global" config
directory ("C:\Program Files\OpenVPN\config"). Is this by design?

Signature · 1 line
-Lev
author
#2 lore raw

Hi,

Quoted 9 lines · Lev Stipakov

Hi,

src/openvpnserv/interactive.c RunOpenvpn(LPVOID p) · line 1586

Quoted 3 lines
src/openvpnserv/interactive.c +2 -0
+    if (!ValidateOptions(pipe, sud.directory, sud.options)
+        && !IsAuthorizedUser(ovpn_user->User.Sid, imp_token, settings.ovpn_admin_group)
     {

Closing parenthesis is missing:

That is embarrassing.. I went through the motions of compile testing it without updating the source tree in openvpnbuild. Yikes..

Quoted 9 lines
Quoted 2 lines

C:\Users\lev\Projects\openvpn\src\openvpnserv\interactive.c(1586,5): error C2143: syntax error: missing ')' before '{'

Also it is probably just me, but took me a while to figure out what that code is doing and why.

Could you slightly improve the comment above lines you touch
("Check user is authorized or options are white-listed") and mention something like "Non-authorized users are allowed to use only white-listed options and have config only in global openvpn config directory"?

Will do.

Quoted 4 lines

When I started to debug it, I realized that I have to be authorized user when config is in my current user's directory (C:\Users\lev\OpenVPN\config) and not in "global" config directory ("C:\Program Files\OpenVPN\config"). Is this by design?

Yes, that is by design. As iservice allows the users to do some actions that normally require admin privilege, a limited user is only allowed to use some whitelisted options or a config installed by an admin in the global config directory. They are not allowed to run arbitrary configs
that they can edit. Unless an admin explicitly gives them permission to do so --- checked by membership in "OpenVPNAdministrators" group. Users who have admin privilege (even if not enabled in the token by UAC) are considered authorized.

Selva

#3 lore raw

PATCH v2 1/1 Swap the order of checks for validating interactive service user

In reply to [email protected]...

From: Selva Nair <[email protected]...>

Check the config file location and command line options first and membership in OpenVPNAdministrators group after that as the latter could be a slow process for active directory users.

When connection to domain controllers is poor or unavailable, checking the group membership is slow and causes timeouts in the GUI (Trac 1051). However, in cases where the config is in the global directory, no group membership check should be required. The re-ordering here avoids the redundant check in such cases.

In addition to this, its also proposed to improve the timeout handling in the GUI, but this change is still useful as it should completely eliminate the timeout issue for many users.

Also see: https://github.com/OpenVPN/openvpn-gui/issues/332

---

v2: Add missing closing parenthesis and improve the comment above the edited chunk.

 src/openvpnserv/interactive.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
src/openvpnserv/interactive.c +9 -3
index 6e72a14..ff5b08b 100644
@@ -1580,9 +1580,15 @@ RunOpenvpn(LPVOID p)
         goto out;
     }
 
-    /* Check user is authorized or options are white-listed */
-    if (!IsAuthorizedUser(ovpn_user->User.Sid, imp_token, settings.ovpn_admin_group)
-        && !ValidateOptions(pipe, sud.directory, sud.options))
+    /*
+     * Only authorized users are allowed to use any command line options or
+     * have the config file in locations other than the global config directory.
+     *
+     * Check options are white-listed and config is in the global directory
+     * OR user is authorized to run any config.
+     */
+    if (!ValidateOptions(pipe, sud.directory, sud.options)
+        && !IsAuthorizedUser(ovpn_user->User.Sid, imp_token, settings.ovpn_admin_group))
     {
         goto out;
     }
Signature · 1 line
2.1.4
#5 lore raw

I am sorry, I have to retract my ACK.

When ValidateOptions is called first and config is non located in global directory (Program Files),
service replies to gui via pipe with error message:

0x20000001
You have specified a config file location (client.xxx.ovpn relative to C:\Users\lev\OpenVPN\config\client.xxx) that requires admin approval. This error may be avoided by adding your account to the "OpenVPN Administrators" group
(null)

This message is incorrect, since account is in admin group, we just haven't checked that yet. When following check verifies that user indeed belongs to the admin group, service runs openvpn and connection got established. By some reasons, above error message is not read on the gui side. However, by adding small delay to service code or a breakpoint, message reaches gui, which displays an error.

While under normal circumstances it works, I believe it just works by accident (error message is not read on gui side without delay on service side). We need to move sending error message out of ValidateOptions.

ma 3. helmik. 2020 klo 10.23 Lev Stipakov ([email protected]...) kirjoitti:

Quoted 4 lines

Built and tested, works as specified.

Signature · 1 line
-Lev
author
#6 lore raw

Hi,

Quoted 10 lines · Lev Stipakov

I am sorry, I have to retract my ACK.

When ValidateOptions is called first and config is non located in global directory (Program Files), service replies to gui via pipe with error message:

0x20000001
You have specified a config file location (client.xxx.ovpn relative to C:\Users\lev\OpenVPN\config\client.xxx) that requires admin approval. This error may be avoided by adding your account to the "OpenVPN Administrators" group (null)

Nothing is as simple as it looks, is it..

After the first round of stupidity, I had run-tested version 2, but still did not catch this.

Quoted 2 lines

This message is incorrect, since account is in admin group, we just haven't checked that yet. When following check verifies that user indeed belongs to the admin group, service runs openvpn and connection got established. By some reasons, above error message is not read on the gui side. However, by adding small delay to service code or a breakpoint, message reaches gui, which displays an error.

Its a timing bug in the GUI. The I/O completion routine for reading from the service is initialized from the thread that handles the connection. But this thread is created in a suspended state and resumed only after sending the start request to the service from the main thread. That could be the reason why this message is missed by the GUI. This needs to be fixed as it could potentially affect normal operation of the GUI.

Quoted 1 line

While under normal circumstances it works, I elieve it just works by accident (error message is not read on gui side without delay on service side). We need to move sending error message out of ValidateOptions.

This error message was originally added so that the GUI can notify the user with a helpful message. But the GUI now checks config location and user's group memberships and offers to spawn an elevated process to add the user to ovpn_admin_group before calling the service. But still useful to return this message in case some other client decides to use the service. I'll move it out of ValidateOptions and add code to return it only when appropriate.

Selva

#7 lore raw

PATCH v3 1/1 Swap the order of checks for validating interactive service user

In reply to [email protected]...

From: Selva Nair <[email protected]...>

Check the config file location and command line options first and membership in OpenVPNAdministrators group after that as the latter could be a slow process for active directory users.

When connection to domain controllers is poor or unavailable, checking the group membership is slow and causes timeouts in the GUI (Trac 1051). However, in cases where the config is in the global directory, no group membership check should be required. The re-ordering here avoids the redundant check in such cases.

In addition to this, its also proposed to improve the timeout handling in the GUI, but this change is still useful as it should completely eliminate the timeout issue for many users.

v3: Do not send error message to the client pipe from ValidateOptions(). Instead save the error and send it on only if user authorization also fails. The error buffer size is increased to 512 wide chars as these messages could get long in some cases and may get truncated otherwise.

Also see: https://github.com/OpenVPN/openvpn-gui/issues/332

---
v2: Add missing closing parenthesis and improve the comment above the edited chunk.

 src/openvpnserv/interactive.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)
src/openvpnserv/interactive.c +19 -14
index 6e72a14..710f9c7 100644
@@ -389,14 +389,13 @@ ReturnOpenvpnOutput(HANDLE pipe, HANDLE ovpn_output, DWORD count, LPHANDLE event
 /*
  * Validate options against a white list. Also check the config_file is
  * inside the config_dir. The white list is defined in validate.c
- * Returns true on success
+ * Returns true on success, false on error with reason set in errmsg.
  */
 static BOOL
-ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options)
+ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options, WCHAR *errmsg, DWORD capacity)
 {
     WCHAR **argv;
     int argc;
-    WCHAR buf[256];
     BOOL ret = FALSE;
     int i;
     const WCHAR *msg1 = L"You have specified a config file location (%s relative to %s)"
@@ -411,8 +410,9 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options)
 
     if (!argv)
     {
-        ReturnLastError(pipe, L"CommandLineToArgvW");
-        ReturnError(pipe, ERROR_STARTUP_DATA, L"Cannot validate options", 1, &exit_event);
+        openvpn_swprintf(errmsg, capacity,
+	                 L"Cannot validate options: CommandLineToArgvW failed with error = 0x%08x",
+	                 GetLastError());
         goto out;
     }
 
@@ -432,9 +432,8 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options)
 
         if (!CheckOption(workdir, 2, argv_tmp, &settings))
         {
-            openvpn_swprintf(buf, _countof(buf), msg1, argv[0], workdir,
+            openvpn_swprintf(errmsg, capacity, msg1, argv[0], workdir,
                              settings.ovpn_admin_group);
-            ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event);
         }
         goto out;
     }
@@ -450,15 +449,13 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options)
         {
             if (wcscmp(L"--config", argv[i]) == 0 && argc-i > 1)
             {
-                openvpn_swprintf(buf, _countof(buf), msg1, argv[i+1], workdir,
+                openvpn_swprintf(errmsg, capacity, msg1, argv[i+1], workdir,
                                  settings.ovpn_admin_group);
-                ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event);
             }
             else
             {
-                openvpn_swprintf(buf, _countof(buf), msg2, argv[i],
+                openvpn_swprintf(errmsg, capacity, msg2, argv[i],
                                  settings.ovpn_admin_group);
-                ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event);
             }
             goto out;
         }
@@ -1487,6 +1484,7 @@ RunOpenvpn(LPVOID p)
     size_t cmdline_size;
     undo_lists_t undo_lists;
     ring_buffer_handles_t ring_buffer_handles;
+    WCHAR errmsg[512] = L"";
 
     SECURITY_ATTRIBUTES inheritable = {
         .nLength = sizeof(inheritable),
@@ -1580,10 +1578,17 @@ RunOpenvpn(LPVOID p)
         goto out;
     }
 
-    /* Check user is authorized or options are white-listed */
-    if (!IsAuthorizedUser(ovpn_user->User.Sid, imp_token, settings.ovpn_admin_group)
-        && !ValidateOptions(pipe, sud.directory, sud.options))
+    /*
+     * Only authorized users are allowed to use any command line options or
+     * have the config file in locations other than the global config directory.
+     *
+     * Check options are white-listed and config is in the global directory
+     * OR user is authorized to run any config.
+     */
+    if (!ValidateOptions(pipe, sud.directory, sud.options, errmsg, _countof(errmsg))
+        && !IsAuthorizedUser(ovpn_user->User.Sid, imp_token, settings.ovpn_admin_group))
     {
+        ReturnError(pipe, ERROR_STARTUP_DATA, errmsg, 1, &exit_event);
         goto out;
     }
 
Signature · 1 line
2.1.4
Acked-by on v3 1/1
#8 lore raw

Built with MSVC and tested, works as described - membership check is performed only if options validation fails. Also error message is sent only when user is not authorized (this got broken in v2).

#9 lore raw
In reply to [email protected]...

Your patch has been applied to the master branch.

I have not tested this in any way, but since Lev has reviewed & tested v2 (and found a bug) and reviewed & tested v3, I do not think I would add much value anyway.

commit c6cc66a13568dd1078bfbeb763998c1b9e2a2999

Author: Selva Nair
Date:   Sun Feb 9 23:33:20 2020 -0500
     Swap the order of checks for validating interactive service user

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

--
kind regards,

Gert Doering