From: Heiko Hund <[email protected]>

Only the first pipe instance was created with the flag
FILE_FLAG_FIRST_PIPE_INSTANCE, which relied on the service never
closing all of its instances.

Create a disconnected instance on first use instead, which holds the
name for the lifetime of the service, no matter how many instances
clients use.

Change-Id: Id3a58112fffc8679978ddf5adb44e7be9083e17a
Signed-off-by: Heiko Hund <[email protected]>
Acked-by: Razvan Cojocaru <[email protected]>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2112
---

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/+/2112
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Razvan Cojocaru <[email protected]>

        
diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c
index 4a8d0b5..5e4b555 100644
--- a/src/openvpnserv/interactive.c
+++ b/src/openvpnserv/interactive.c
@@ -3806,6 +3806,16 @@
 }
 
 
+/**
+ * Create an instance of the service pipe clients connect to
+ *
+ * The first call also creates a private instance with 
FILE_FLAG_FIRST_PIPE_INSTANCE,
+ * which fails if the pipe name already exists. It is kept disconnected, so 
clients
+ * cannot connect to it, and holds the pipe name for the lifetime of the 
service.
+ * This way the pipe name is blocked from other processes using it.
+ *
+ * @return HANDLE of the pipe instance, or INVALID_HANDLE_VALUE on error
+ */
 static HANDLE
 CreateClientPipeInstance(VOID)
 {
@@ -3833,21 +3843,33 @@
     sa.bInheritHandle = FALSE;
 
     DWORD flags = PIPE_ACCESS_DUPLEX | WRITE_DAC | FILE_FLAG_OVERLAPPED;
-
-    static BOOL first = TRUE;
-    if (first)
-    {
-        flags |= FILE_FLAG_FIRST_PIPE_INSTANCE;
-        first = FALSE;
-    }
+    DWORD mode = PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | 
PIPE_REJECT_REMOTE_CLIENTS;
 
     WCHAR pipe_name[256]; /* The entire pipe name string can be up to 256 
characters long according
                              to MSDN. */
     swprintf(pipe_name, _countof(pipe_name), L"\\\\.\\pipe\\" _L(PACKAGE) 
L"%ls\\service",
              service_instance);
-    HANDLE pipe = CreateNamedPipe(
-        pipe_name, flags, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | 
PIPE_REJECT_REMOTE_CLIENTS,
-        PIPE_UNLIMITED_INSTANCES, 1024, 1024, 0, &sa);
+
+    static HANDLE blocker = INVALID_HANDLE_VALUE;
+    if (blocker == INVALID_HANDLE_VALUE)
+    {
+        blocker = CreateNamedPipe(pipe_name, flags | 
FILE_FLAG_FIRST_PIPE_INSTANCE, mode,
+                                  PIPE_UNLIMITED_INSTANCES, 1024, 1024, 0, 
&sa);
+        if (blocker != INVALID_HANDLE_VALUE && !DisconnectNamedPipe(blocker))
+        {
+            DWORD error = GetLastError();
+            CloseHandleEx(&blocker);
+            SetLastError(error);
+        }
+    }
+
+    /* Without the first instance, fail and try again with the next call */
+    HANDLE pipe = INVALID_HANDLE_VALUE;
+    if (blocker != INVALID_HANDLE_VALUE)
+    {
+        pipe = CreateNamedPipe(pipe_name, flags, mode, 
PIPE_UNLIMITED_INSTANCES, 1024, 1024, 0,
+                               &sa);
+    }
 
     LocalFree(sd);
 


_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to