Skip to contentSkip to conversation

wintun: remove SYSTEM elevation hack

PATCH In review Lev Stipakov 2 participants 2 messages Jul 24, 2020 – Jul 25, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wintun: remove SYSTEM elevation hack

From: Lev Stipakov <[email protected]...>

As discussed a while ago on the mailing list and
community meetings, having SYSTEM elevation hack
inside openvpn code considered harmful.

Since interactive service is the recommended way of using openvpn on Windows, limiting wintun usage to interactive service should not be an issue.

Remove elevation hack code and provide an error message telling user to use interactive service or do SYSTEM elevation himself via psexec.

Move implementation of register_ring_buffers() to header amd delete ring_buffer.c.

---
 src/openvpn/Makefile.am                     |  2 +-
 src/openvpn/openvpn.vcxproj                 |  1 -
 src/openvpn/openvpn.vcxproj.filters         |  3 -
 src/openvpn/ring_buffer.c                   | 56 ------------
 src/openvpn/ring_buffer.h                   | 31 +++++--
 src/openvpn/tun.c                           | 33 +------
 src/openvpn/win32.c                         | 95 ---------------------
 src/openvpnserv/Makefile.am                 |  2 +-
 src/openvpnserv/openvpnserv.vcxproj         |  1 -
 src/openvpnserv/openvpnserv.vcxproj.filters |  3 -
 10 files changed, 32 insertions(+), 195 deletions(-)
 delete mode 100644 src/openvpn/ring_buffer.c
src/openvpn/Makefile.am +1 -1
index f0e0ad23..47ad762d 100644
@@ -141,6 +141,6 @@ openvpn_LDADD = \
 	$(OPTIONAL_DL_LIBS) \
 	$(OPTIONAL_INOTIFY_LIBS)
 if WIN32
-openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h ring_buffer.c ring_buffer.h
+openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h ring_buffer.h
 openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi
 endif
src/openvpn/openvpn.vcxproj +0 -1
index c34733ea..bd1a5844 100644
@@ -189,7 +189,6 @@
     <ClCompile Include="ps.c" />
     <ClCompile Include="push.c" />
     <ClCompile Include="reliable.c" />
-    <ClCompile Include="ring_buffer.c" />
     <ClCompile Include="route.c" />
     <ClCompile Include="run_command.c" />
     <ClCompile Include="schedule.c" />
src/openvpn/openvpn.vcxproj.filters +0 -3
index 80eb52b3..e0bc7d5e 100644
@@ -240,9 +240,6 @@
     <ClCompile Include="vlan.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="ring_buffer.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="ssl_ncp.c">
       <Filter>Source Files</Filter>
     </ClCompile>
src/openvpn/ring_buffer.c deleted +0 -56

57 lines not on this page · raw message

src/openvpn/ring_buffer.h +26 -5
index af46f106..4293f633 100644
@@ -94,11 +94,32 @@ struct TUN_PACKET
  *                            that data has been written to receive ring
  * @return                    true if registration is successful, false otherwise - use GetLastError()
  */
-bool register_ring_buffers(HANDLE device,
-                           struct tun_ring *send_ring,
-                           struct tun_ring *receive_ring,
-                           HANDLE send_tail_moved,
-                           HANDLE receive_tail_moved);
+static bool
+register_ring_buffers(HANDLE device,
+                      struct tun_ring *send_ring,
+                      struct tun_ring *receive_ring,
+                      HANDLE send_tail_moved,
+                      HANDLE receive_tail_moved)
+{
+    struct tun_register_rings rr;
+    BOOL res;
+    DWORD bytes_returned;
+
+    ZeroMemory(&rr, sizeof(rr));
+
+    rr.send.ring = send_ring;
+    rr.send.ring_size = sizeof(struct tun_ring);
+    rr.send.tail_moved = send_tail_moved;
+
+    rr.receive.ring = receive_ring;
+    rr.receive.ring_size = sizeof(struct tun_ring);
+    rr.receive.tail_moved = receive_tail_moved;
+
+    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr, sizeof(rr),
+      NULL, 0, &bytes_returned, NULL);
+
+    return res != FALSE;
+}
 
 #endif /* ifndef OPENVPN_RING_BUFFER_H */
 #endif /* ifdef _WIN32 */
src/openvpn/tun.c +4 -29
index e96368ca..82d96927 100644
@@ -6148,35 +6148,10 @@ wintun_register_ring_buffer(struct tuntap *tt, const char *device_guid)
     }
     else
     {
-        if (!impersonate_as_system())
-        {
-            msg(M_FATAL, "ERROR:  Failed to impersonate as SYSTEM, make sure process is running under privileged account");
-        }
-        if (!register_ring_buffers(tt->hand,
-                                   tt->wintun_send_ring,
-                                   tt->wintun_receive_ring,
-                                   tt->rw_handle.read,
-                                   tt->rw_handle.write))
-        {
-            switch (GetLastError())
-            {
-                case ERROR_ACCESS_DENIED:
-                    msg(M_FATAL, "Access denied registering ring buffers. Is this process run as SYSTEM?");
-                    break;
-
-                case ERROR_ALREADY_INITIALIZED:
-                    msg(M_NONFATAL, "Adapter %s is already in use", device_guid);
-                    break;
-
-                default:
-                    msg(M_NONFATAL | M_ERRNO, "Failed to register ring buffers");
-            }
-            ret = false;
-        }
-        if (!RevertToSelf())
-        {
-            msg(M_FATAL, "ERROR:  RevertToSelf error: %lu", GetLastError());
-        }
+        msg(M_FATAL, "ERROR:  Wintun requires SYSTEM privileges and therefore "
+                     "should be used with interactive service. If you want to "
+                     "use openvpn from command line, you need to do SYSTEM "
+                     "elevation yourself (for example with psexec).");
     }
 
     return ret;
src/openvpn/win32.c +0 -95

99 lines not on this page · raw message

src/openvpnserv/Makefile.am +1 -1
index f8d3319c..5dc38c9b 100644
@@ -37,4 +37,4 @@ openvpnserv_SOURCES = \
 	validate.c validate.h \
 	$(top_srcdir)/src/openvpn/block_dns.c $(top_srcdir)/src/openvpn/block_dns.h \
 	openvpnserv_resources.rc \
-	$(top_srcdir)/src/openvpn/ring_buffer.c $(top_srcdir)/src/openvpn/ring_buffer.h
+	$(top_srcdir)/src/openvpn/ring_buffer.h
src/openvpnserv/openvpnserv.vcxproj +0 -1
index c5a34b87..5e973df4 100644
@@ -115,7 +115,6 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
-    <ClCompile Include="..\openvpn\ring_buffer.c" />
     <ClCompile Include="automatic.c" />
     <ClCompile Include="common.c" />
     <ClCompile Include="interactive.c" />
src/openvpnserv/openvpnserv.vcxproj.filters +0 -3
index 3cb14ef6..41ad3e80 100644
@@ -33,9 +33,6 @@
     <ClCompile Include="..\openvpn\block_dns.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="..\openvpn\ring_buffer.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="service.h">
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw

Code changes look good, Makefile / .vcxproj file changes look reasonable, and it passes my MinGW test build.

I have not tested the resulting binary because I do not hit this particular code path anyway - and the end result is fairly trivial ("if (!iservice) then complain") :-)

Thanks.

Your patch has been applied to the master branch.

commit 6d19775a468f0d35cd1f199b489ee18a74e6c905

Author: Lev Stipakov
Date:   Fri Jul 24 13:48:41 2020 +0300
     wintun: remove SYSTEM elevation hack

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

--
kind regards,

Gert Doering