[Openvpn-devel,v3] openvpnserv: check for a leading non-option in the startup data

Message ID 20261120080412.4368-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v3] openvpnserv: check for a leading non-option in the startup data |

Commit Message

Gert Doering Nov. 20, 2026, 8:04 a.m. UTC
  From: Razvan Cojocaru <razvanc@mailbox.org>

Check sud.options directly before doing any other work, instead of
building the openvpn.exe command line and parsing it back. This also
rejects empty options.

Change-Id: I14ad4e4511bd9933b89f48ae5a3ac6d2a5e0f2d8
Signed-off-by: Razvan Cojocaru <razvanc@mailbox.org>
Acked-by: Lev Stipakov <lstipakov@gmail.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2188
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2188
This mail reflects revision 3 of this Change.

Acked-by according to Gerrit (reflected above):
Lev Stipakov <lstipakov@gmail.com>
  

Patch

diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c
index 354fe09..f0078ac 100644
--- a/src/openvpnserv/interactive.c
+++ b/src/openvpnserv/interactive.c
@@ -357,24 +357,17 @@ 
 /**
  * Check whether the user options start with a non-option
  *
- * openvpn.exe takes a lone argument as the config file only when there are
- * no other arguments, and takes non-options after the --msg-channel handle
- * as its extra parameters.
+ * openvpn.exe takes non-options after the --msg-channel handle the service
+ * prepends as its extra parameters, not as the config file.
  *
- * @param  cmdline  openvpn.exe command line built by the service
+ * @param  options  user options from the startup data
  *
  * @return BOOL to indicate if the first user argument is not an option
  */
 static BOOL
-StartsWithNonOption(const WCHAR *cmdline)
+StartsWithNonOption(const WCHAR *options)
 {
-    int argc;
-    WCHAR **argv = CommandLineToArgvW(cmdline, &argc);
-    /* "openvpn --msg-channel <handle> <arg>..." */
-    BOOL ret = argv && argc > 3 && !IsOption(argv[3]);
-
-    LocalFree(argv);
-    return ret;
+    return !IsOption(options + wcsspn(options, L" \t"));
 }
 
 /*
@@ -3639,6 +3632,14 @@ 
         goto out;
     }
 
+    if (StartsWithNonOption(sud.options))
+    {
+        ReturnError(pipe, ERROR_STARTUP_DATA,
+                    L"Options must start with an option, pass the config file with --config", 1,
+                    &exit_event);
+        goto out;
+    }
+
     if (!InitializeSecurityDescriptor(&ovpn_sd, SECURITY_DESCRIPTOR_REVISION))
     {
         ReturnLastError(pipe, L"InitializeSecurityDescriptor");
@@ -3883,14 +3884,6 @@ 
     swprintf(cmdline, cmdline_size, L"openvpn --msg-channel %" PRIuPTR L" %ls",
              (uintptr_t)svc_pipe, sud.options);
 
-    if (StartsWithNonOption(cmdline))
-    {
-        ReturnError(pipe, ERROR_STARTUP_DATA,
-                    L"Options must start with an option, pass the config file with --config", 1,
-                    &exit_event);
-        goto out;
-    }
-
     /*
      * Only authorized users are allowed to use any command line options or
      * have the config file in locations other than the global config directory.
diff --git a/tests/unit_tests/openvpnserv/test_openvpnserv.c b/tests/unit_tests/openvpnserv/test_openvpnserv.c
index 7db61f7..d07fa22 100644
--- a/tests/unit_tests/openvpnserv/test_openvpnserv.c
+++ b/tests/unit_tests/openvpnserv/test_openvpnserv.c
@@ -304,15 +304,18 @@ 
 static void
 test_starts_with_non_option(void **state)
 {
-    assert_true(StartsWithNonOption(L"openvpn --msg-channel 1 foo.ovpn"));
-    assert_true(StartsWithNonOption(L"openvpn --msg-channel 1 \"my vpn.ovpn\""));
-    assert_true(StartsWithNonOption(L"openvpn --msg-channel 1 foo.ovpn --verb 3"));
-    assert_true(StartsWithNonOption(L"openvpn --msg-channel 1 foo.ovpn bar"));
-    assert_true(StartsWithNonOption(L"openvpn --msg-channel 1 \"\""));
+    assert_true(StartsWithNonOption(L"foo.ovpn"));
+    assert_true(StartsWithNonOption(L"\"my vpn.ovpn\""));
+    assert_true(StartsWithNonOption(L"foo.ovpn --verb 3"));
+    assert_true(StartsWithNonOption(L"foo.ovpn bar"));
+    assert_true(StartsWithNonOption(L" \tfoo.ovpn"));
+    assert_true(StartsWithNonOption(L"\"\""));
+    assert_true(StartsWithNonOption(L""));
+    assert_true(StartsWithNonOption(L" "));
 
-    assert_false(StartsWithNonOption(L"openvpn --msg-channel 1 "));
-    assert_false(StartsWithNonOption(L"openvpn --msg-channel 1 --persist-tun"));
-    assert_false(StartsWithNonOption(L"openvpn --msg-channel 1 --config foo.ovpn"));
+    assert_false(StartsWithNonOption(L"--persist-tun"));
+    assert_false(StartsWithNonOption(L"--config foo.ovpn"));
+    assert_false(StartsWithNonOption(L" \t--config foo.ovpn"));
 }
 
 static void