Skip to contentSkip to conversation

Use undo_lists for saving ring-buffer handles in interactive service

PATCH In review [email protected]... 3 participants 6 messages Dec 29, 2022 – Dec 31, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Use undo_lists for saving ring-buffer handles in interactive service

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

HandleRegisterRingBuffers() in interactive.c did not follow the the original API of HandleMessage(): a new argument was added to HandleMessage to pass-in prer-process ring-buffer handles. The existing undo lists argument is meant for such use.

Rewrite following the original design.

---
 src/openvpnserv/interactive.c | 51 ++++++++++++++++++++++++-----------
 1 file changed, 36 insertions(+), 15 deletions(-)
src/openvpnserv/interactive.c +36 -15
index 47bcd72a..8476738c 100644
@@ -92,6 +92,7 @@ typedef enum {
     undo_dns4,
     undo_dns6,
     undo_domain,
+    undo_ring_buffer,
     _undo_type_max
 } undo_type_t;
 typedef list_item_t *undo_lists_t[_undo_type_max];
@@ -780,7 +781,7 @@ BlockDNSErrHandler(DWORD err, const char *msg)
 
 /* Use an always-true match_fn to get the head of the list */
 static BOOL
-CmpEngine(LPVOID item, LPVOID any)
+CmpAny(LPVOID item, LPVOID any)
 {
     return TRUE;
 }
@@ -835,7 +836,7 @@ HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists)
     }
     else
     {
-        interface_data = RemoveListItem(&(*lists)[block_dns], CmpEngine, NULL);
+        interface_data = RemoveListItem(&(*lists)[block_dns], CmpAny, NULL);
         if (interface_data)
         {
             engine = interface_data->engine;
@@ -1354,40 +1355,49 @@ DuplicateAndMapRing(HANDLE ovpn_proc, HANDLE orig_handle, HANDLE *new_handle, st
 
 static DWORD
 HandleRegisterRingBuffers(const register_ring_buffers_message_t *rrb, HANDLE ovpn_proc,
-                          ring_buffer_handles_t *ring_buffer_handles)
+                          undo_lists_t *lists)
 {
     DWORD err = 0;
 
-    CloseRingBufferHandles(ring_buffer_handles);
+    ring_buffer_handles_t *ring_buffer_handles = RemoveListItem(&(*lists)[undo_ring_buffer], CmpAny, NULL);
+
+    if (ring_buffer_handles)
+    {
+        CloseRingBufferHandles(ring_buffer_handles);
+    }
+    else if ((ring_buffer_handles = calloc(1, sizeof(*ring_buffer_handles))) == NULL)
+    {
+        return ERROR_OUTOFMEMORY;
+    }
 
     err = OvpnDuplicateHandle(ovpn_proc, rrb->device, &ring_buffer_handles->device);
     if (err != ERROR_SUCCESS)
     {
-        return err;
+        goto out;
     }
 
     err = DuplicateAndMapRing(ovpn_proc, rrb->send_ring_handle, &ring_buffer_handles->send_ring_handle, &ring_buffer_handles->send_ring);
     if (err != ERROR_SUCCESS)
     {
-        return err;
+        goto out;
     }
 
     err = DuplicateAndMapRing(ovpn_proc, rrb->receive_ring_handle, &ring_buffer_handles->receive_ring_handle, &ring_buffer_handles->receive_ring);
     if (err != ERROR_SUCCESS)
     {
-        return err;
+        goto out;
     }
 
     err = OvpnDuplicateHandle(ovpn_proc, rrb->send_tail_moved, &ring_buffer_handles->send_tail_moved);
     if (err != ERROR_SUCCESS)
     {
-        return err;
+        goto out;
     }
 
     err = OvpnDuplicateHandle(ovpn_proc, rrb->receive_tail_moved, &ring_buffer_handles->receive_tail_moved);
     if (err != ERROR_SUCCESS)
     {
-        return err;
+        goto out;
     }
 
     if (!register_ring_buffers(ring_buffer_handles->device, ring_buffer_handles->send_ring,
@@ -1396,6 +1406,16 @@ HandleRegisterRingBuffers(const register_ring_buffers_message_t *rrb, HANDLE ovp
     {
         err = GetLastError();
         MsgToEventLog(M_SYSERR, TEXT("Could not register ring buffers"));
+        goto out;
+    }
+
+    err = AddListItem(&(*lists)[undo_ring_buffer], ring_buffer_handles);
+
+out:
+    if (err != ERROR_SUCCESS && ring_buffer_handles)
+    {
+        CloseRingBufferHandles(ring_buffer_handles);
+        free(ring_buffer_handles);
     }
 
     return err;
@@ -1425,7 +1445,7 @@ HandleMTUMessage(const set_mtu_message_t *mtu)
 }
 
 static VOID
-HandleMessage(HANDLE pipe, HANDLE ovpn_proc, ring_buffer_handles_t *ring_buffer_handles,
+HandleMessage(HANDLE pipe, HANDLE ovpn_proc,
               DWORD bytes, DWORD count, LPHANDLE events, undo_lists_t *lists)
 {
     DWORD read;
@@ -1509,7 +1529,7 @@ HandleMessage(HANDLE pipe, HANDLE ovpn_proc, ring_buffer_handles_t *ring_buffer_
         case msg_register_ring_buffers:
             if (msg.header.size == sizeof(msg.rrb))
             {
-                ack.error_number = HandleRegisterRingBuffers(&msg.rrb, ovpn_proc, ring_buffer_handles);
+                ack.error_number = HandleRegisterRingBuffers(&msg.rrb, ovpn_proc, lists);
             }
             break;
 
@@ -1579,6 +1599,10 @@ Undo(undo_lists_t *lists)
                     }
                     break;
 
+                case undo_ring_buffer:
+                    CloseRingBufferHandles(item->data);
+                    break;
+
                 case _undo_type_max:
                     /* unreachable */
                     break;
@@ -1611,7 +1635,6 @@ RunOpenvpn(LPVOID p)
     WCHAR *cmdline = NULL;
     size_t cmdline_size;
     undo_lists_t undo_lists;
-    ring_buffer_handles_t ring_buffer_handles;
     WCHAR errmsg[512] = L"";
 
     SECURITY_ATTRIBUTES inheritable = {
@@ -1633,7 +1656,6 @@ RunOpenvpn(LPVOID p)
     ZeroMemory(&startup_info, sizeof(startup_info));
     ZeroMemory(&undo_lists, sizeof(undo_lists));
     ZeroMemory(&proc_info, sizeof(proc_info));
-    ZeroMemory(&ring_buffer_handles, sizeof(ring_buffer_handles));
 
     if (!GetStartupData(pipe, &sud))
     {
@@ -1866,7 +1888,7 @@ RunOpenvpn(LPVOID p)
             break;
         }
 
-        HandleMessage(ovpn_pipe, proc_info.hProcess, &ring_buffer_handles, bytes, 1, &exit_event, &undo_lists);
+        HandleMessage(ovpn_pipe, proc_info.hProcess, bytes, 1, &exit_event, &undo_lists);
     }
 
     WaitForSingleObject(proc_info.hProcess, IO_TIMEOUT);
@@ -1893,7 +1915,6 @@ out:
     free(cmdline);
     DestroyEnvironmentBlock(user_env);
     FreeStartupData(&sud);
-    CloseRingBufferHandles(&ring_buffer_handles);
     CloseHandleEx(&proc_info.hProcess);
     CloseHandleEx(&proc_info.hThread);
     CloseHandleEx(&stdin_read);
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 Cleanup: Close duplicated handles in interactive service

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

Several handles from openvpn.exe are duplicated in the service for registering ring buffer memory maps with the driver. These handles are not required after registration, as all access is through handles in openvpn.exe. Only the map base address (send_ring, rceive_ring) need be retained for later unmapping.

Use local variables for duplicated handles and close them soon after use.

The struct ring_buffer_handles_t is renamed to ring_buffer_maps_t as there are no handles in there any longer.

---
 src/openvpnserv/interactive.c | 69 +++++++++++++++++------------------
 1 file changed, 34 insertions(+), 35 deletions(-)
src/openvpnserv/interactive.c +34 -35
index 8476738c..47ddd4e8 100644
@@ -105,14 +105,9 @@ typedef struct {
 } block_dns_data_t;
 
 typedef struct {
-    HANDLE send_ring_handle;
-    HANDLE receive_ring_handle;
-    HANDLE send_tail_moved;
-    HANDLE receive_tail_moved;
-    HANDLE device;
     struct tun_ring *send_ring;
     struct tun_ring *receive_ring;
-} ring_buffer_handles_t;
+} ring_buffer_maps_t;
 
 
 static DWORD
@@ -179,15 +174,10 @@ OvpnUnmapViewOfFile(struct tun_ring **ring)
 }
 
 static void
-CloseRingBufferHandles(ring_buffer_handles_t *ring_buffer_handles)
+UnmapRingBuffer(ring_buffer_maps_t *ring_buffer_maps)
 {
-    CloseHandleEx(&ring_buffer_handles->device);
-    CloseHandleEx(&ring_buffer_handles->receive_tail_moved);
-    CloseHandleEx(&ring_buffer_handles->send_tail_moved);
-    OvpnUnmapViewOfFile(&ring_buffer_handles->send_ring);
-    OvpnUnmapViewOfFile(&ring_buffer_handles->receive_ring);
-    CloseHandleEx(&ring_buffer_handles->receive_ring_handle);
-    CloseHandleEx(&ring_buffer_handles->send_ring_handle);
+    OvpnUnmapViewOfFile(&ring_buffer_maps->send_ring);
+    OvpnUnmapViewOfFile(&ring_buffer_maps->receive_ring);
 }
 
 static HANDLE
@@ -1333,16 +1323,19 @@ OvpnDuplicateHandle(HANDLE ovpn_proc, HANDLE orig_handle, HANDLE *new_handle)
 }
 
 static DWORD
-DuplicateAndMapRing(HANDLE ovpn_proc, HANDLE orig_handle, HANDLE *new_handle, struct tun_ring **ring)
+DuplicateAndMapRing(HANDLE ovpn_proc, HANDLE orig_handle, struct tun_ring **ring)
 {
     DWORD err = ERROR_SUCCESS;
 
-    err = OvpnDuplicateHandle(ovpn_proc, orig_handle, new_handle);
+    HANDLE dup_handle = NULL;
+
+    err = OvpnDuplicateHandle(ovpn_proc, orig_handle, &dup_handle);
     if (err != ERROR_SUCCESS)
     {
         return err;
     }
-    *ring = (struct tun_ring *)MapViewOfFile(*new_handle, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(struct tun_ring));
+    *ring = (struct tun_ring *)MapViewOfFile(dup_handle, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(struct tun_ring));
+    CloseHandleEx(&dup_handle);
     if (*ring == NULL)
     {
         err = GetLastError();
@@ -1359,65 +1352,71 @@ HandleRegisterRingBuffers(const register_ring_buffers_message_t *rrb, HANDLE ovp
 {
     DWORD err = 0;
 
-    ring_buffer_handles_t *ring_buffer_handles = RemoveListItem(&(*lists)[undo_ring_buffer], CmpAny, NULL);
+    ring_buffer_maps_t *ring_buffer_maps = RemoveListItem(&(*lists)[undo_ring_buffer], CmpAny, NULL);
 
-    if (ring_buffer_handles)
+    if (ring_buffer_maps)
     {
-        CloseRingBufferHandles(ring_buffer_handles);
+        UnmapRingBuffer(ring_buffer_maps);
     }
-    else if ((ring_buffer_handles = calloc(1, sizeof(*ring_buffer_handles))) == NULL)
+    else if ((ring_buffer_maps = calloc(1, sizeof(*ring_buffer_maps))) == NULL)
     {
         return ERROR_OUTOFMEMORY;
     }
 
-    err = OvpnDuplicateHandle(ovpn_proc, rrb->device, &ring_buffer_handles->device);
+    HANDLE device = NULL;
+    HANDLE send_tail_moved = NULL;
+    HANDLE receive_tail_moved = NULL;
+
+    err = OvpnDuplicateHandle(ovpn_proc, rrb->device, &device);
     if (err != ERROR_SUCCESS)
     {
         goto out;
     }
 
-    err = DuplicateAndMapRing(ovpn_proc, rrb->send_ring_handle, &ring_buffer_handles->send_ring_handle, &ring_buffer_handles->send_ring);
+    err = DuplicateAndMapRing(ovpn_proc, rrb->send_ring_handle, &ring_buffer_maps->send_ring);
     if (err != ERROR_SUCCESS)
     {
         goto out;
     }
 
-    err = DuplicateAndMapRing(ovpn_proc, rrb->receive_ring_handle, &ring_buffer_handles->receive_ring_handle, &ring_buffer_handles->receive_ring);
+    err = DuplicateAndMapRing(ovpn_proc, rrb->receive_ring_handle, &ring_buffer_maps->receive_ring);
     if (err != ERROR_SUCCESS)
     {
         goto out;
     }
 
-    err = OvpnDuplicateHandle(ovpn_proc, rrb->send_tail_moved, &ring_buffer_handles->send_tail_moved);
+    err = OvpnDuplicateHandle(ovpn_proc, rrb->send_tail_moved, &send_tail_moved);
     if (err != ERROR_SUCCESS)
     {
         goto out;
     }
 
-    err = OvpnDuplicateHandle(ovpn_proc, rrb->receive_tail_moved, &ring_buffer_handles->receive_tail_moved);
+    err = OvpnDuplicateHandle(ovpn_proc, rrb->receive_tail_moved, &receive_tail_moved);
     if (err != ERROR_SUCCESS)
     {
         goto out;
     }
 
-    if (!register_ring_buffers(ring_buffer_handles->device, ring_buffer_handles->send_ring,
-                               ring_buffer_handles->receive_ring,
-                               ring_buffer_handles->send_tail_moved, ring_buffer_handles->receive_tail_moved))
+    if (!register_ring_buffers(device, ring_buffer_maps->send_ring,
+                               ring_buffer_maps->receive_ring,
+                               send_tail_moved, receive_tail_moved))
     {
         err = GetLastError();
         MsgToEventLog(M_SYSERR, TEXT("Could not register ring buffers"));
         goto out;
     }
 
-    err = AddListItem(&(*lists)[undo_ring_buffer], ring_buffer_handles);
+    err = AddListItem(&(*lists)[undo_ring_buffer], ring_buffer_maps);
 
 out:
-    if (err != ERROR_SUCCESS && ring_buffer_handles)
+    if (err != ERROR_SUCCESS && ring_buffer_maps)
     {
-        CloseRingBufferHandles(ring_buffer_handles);
-        free(ring_buffer_handles);
+        UnmapRingBuffer(ring_buffer_maps);
+        free(ring_buffer_maps);
     }
-
+    CloseHandleEx(&device);
+    CloseHandleEx(&send_tail_moved);
+    CloseHandleEx(&receive_tail_moved);
     return err;
 }
 
@@ -1600,7 +1599,7 @@ Undo(undo_lists_t *lists)
                     break;
 
                 case undo_ring_buffer:
-                    CloseRingBufferHandles(item->data);
+                    UnmapRingBuffer(item->data);
                     break;
 
                 case _undo_type_max:
Signature · 1 line
2.34.1
Acked-by on v1 2/2
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/2] Cleanup: Close duplicated handles in interactive service

Hi,

Indeed, no need to keep those handles for the duration of openvpn process lifetime,
they're needed only for registration.

Compiled and tested.

#3 lore raw

[Openvpn-devel] [PATCH applied] Re: Cleanup: Close duplicated handles in interactive service

In reply to [email protected]...

Again, thanks for the cleanup. As with 1/2, just glanced at the code change and not explicitly tested (beyond "feed to github actions").

Your patch has been applied to the master and release/2.6 branch.

commit a10564c71608dca6172a89dc458e6e23254d600b (master) commit e47c88bbc593f99529712229fae65470a2e4e0a8 (release/2.6)

Author: Selva Nair
Date:   Thu Dec 29 13:27:39 2022 -0500
     Cleanup: Close duplicated handles in interactive service

     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]/msg25863.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v1 1/2
#4 lore raw
In reply to [email protected]...

Hi,

Quoted 2 lines

HandleRegisterRingBuffers() in interactive.c did not follow the the original API of HandleMessage()

Indeed, my bad. I was so thrilled when I made it work so I didn't pay attention to existing patterns (undo_list).

src/openvpnserv/interactive.c BlockDNSErrHandler(DWORD err, const char *msg) · line 784

Quoted 4 lines

/* Use an always-true match_fn to get the head of the list */ static BOOL

src/openvpnserv/interactive.c +1 -1
-CmpEngine(LPVOID item, LPVOID any)
+CmpAny(LPVOID item, LPVOID any)

I wonder why it was called CmpEngine in the first place?

Code looks good, tested with wintun and dco driver (latter is not affected by the change, but just to make sure).

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

Thanks for bringing this in-line with the existing code style. Light glance-at-code looks reasonable - and there's an ACK from Lev anyway :-)

Not tested further.

Your patch has been applied to the master and release/2.6 branch.

commit 6ea9cf8146b1d72aa6a4790bc3ac2b99562b2cac (master) commit fc96b8793ca94c23e5dc229aa1455d747cef4766 (HEAD -> release/2.6)

Author: Selva Nair
Date:   Thu Dec 29 13:27:38 2022 -0500
     Use undo_lists for saving ring-buffer handles in interactive service

     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]/msg25864.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering