Attention is currently required from: Arne Schwabe.

Hello Arne Schwabe,

I'd like you to do a code review.
Please visit

    https://gerrit.openvpn.net/c/openvpn/+/2112?usp=email

to review the following change.


Change subject: openvpnserv: hold the service pipe name with a private instance
......................................................................

openvpnserv: hold the service pipe name with a private instance

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]>
---
M src/openvpnserv/interactive.c
1 file changed, 32 insertions(+), 10 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/12/2112/1

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);


--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2112?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id3a58112fffc8679978ddf5adb44e7be9083e17a
Gerrit-Change-Number: 2112
Gerrit-PatchSet: 1
Gerrit-Owner: Heiko Hund <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to