openvpnserv: enable interactive service to open tun
PATCH 1/1 openvpnserv: enable interactive service to open tun
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling CreateFile() directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.
--- include/openvpn-msg.h | 12 ++++++++ src/openvpn/route.c | 2 +- src/openvpn/tun.c | 70 +++++++++++++++++++++++++++++++++++-------- src/openvpn/win32.c | 6 ++-- src/openvpn/win32.h | 6 ++-- src/openvpnserv/interactive.c | 59 +++++++++++++++++++++++++++++++++++- 6 files changed, 134 insertions(+), 21 deletions(-)
include/openvpn-msg.h +12 -0
@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {
@@ -117,4 +119,14 @@ typedef struct { interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_path[512 ]; +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +HANDLE handle; +int error_number; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/route.c +1 -1
@@ -2992,7 +2992,7 @@ do_route_service(const bool add, const route_message_t *rt, const size_t size, Hack_message_t ack;struct gc_arena gc =gc_new (); -if (!send_msg_iservice (pipe, rt, size, &ack,"ROUTE" )) +if (!send_msg_iservice (pipe, rt, size, &ack,sizeof (ack),"ROUTE" )) { goto out; }
src/openvpn/tun.c +57 -13
113 lines not on this page · raw message
src/openvpn/win32.c +3 -3
@@ -1280,7 +1280,7 @@ win_block_dns_service(bool add, int index, const HANDLE pipe) .iface = { .index = index, .name = "" } }; -if (!send_msg_iservice (pipe, &data,sizeof (data), &ack,"Block_DNS" )) +if (!send_msg_iservice (pipe, &data,sizeof (data), &ack,sizeof (ack),"Block_DNS" )) { goto out; }
@@ -1474,14 +1474,14 @@ win32_version_string(struct gc_arena *gc, bool add_name)bool send_msg_iservice (HANDLE pipe,const void *data,size_t size, -ack_message_t *ack,const char *context) +void *response,size_t response_size,const char *context) {struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",
src/openvpn/win32.h +3 -3
@@ -311,11 +311,11 @@ const char *win32_version_string(struct gc_arena *gc, bool add_name);/* * Send the |size| bytes in buffer |data| to the interactive service |pipe| -* and read the result in |ack|. Returns false on communication error. -* The string in |context| is used to prefix error messages. +* and read the result in |response| of size |response_size|. Returns false +* on communication error. The string in |context| is used to prefix error messages. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size, -ack_message_t *ack,const char *context); +void *response,size_t response_size,const char *context);/* * Attempt to simulate fork/execve on Windows
src/openvpnserv/interactive.c +58 -1
89 lines not on this page · raw message
Signature · 1 line
2.7.4
Hi,
src/openvpn/route.c do_route_service(const bool add, const route_message_t *rt, const size_t size, H · line 2995
Quoted 5 lines · Lev Stipakov
ack_message_t ack; struct gc_arena gc = gc_new();src/openvpn/route.c +1
-1-if (!send_msg_iservice (pipe, rt, size, &ack,"ROUTE" )) +if (!send_msg_iservice (pipe, rt, size, &ack,sizeof (ack),"ROUTE" ))
I do not like this. Please find another way to send the request message "with length" than to add an extra parameter to every single caller of send_msg_iservice().
Possibly introduce a wrapper for the "standard" case which calls a new function send_msg_iservice_ex() that takes a length field for the return data type. And the open wintun / return handle would then use _ex().
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Hi
Quoted 7 lines · Gert Doering
Hi,
Quoted 3 lines · Lev Stipakov
ack_message_t ack; struct gc_arena gc = gc_new();
Quoted 6 lines
src/openvpn/route.c do_route_service(const bool add, const route_message_t *rt, const size_t size, H · line 2995
Quoted 2 lines
src/openvpn/route.c +1
-1-if (!send_msg_iservice (pipe, rt, size, &ack,"ROUTE" )) +if (!send_msg_iservice (pipe, rt, size, &ack,sizeof (ack),"ROUTE" ))I do not like this. Please find another way to send the request message "with length" than to add an extra parameter to every single caller of
send_msg_iservice().
Gert beat me to it :) Anyway, me too!
The way interactive service structures are coded should not require this at all, does it? The size and message type are already in the header, so why do we need to pass it? The result here is a new kind of ack message with a different size and type and that could be checked by accessing the header element. Unless I'm missing something.
Quoted 11 lines
src/openvpnserv/interactive.c +9
-0+HANDLE local_handle =CreateFileA (open_tun->device_path,GENERIC_READ |GENERIC_WRITE ,0 ,0 , +OPEN_EXISTING ,FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED ,0 ); + +if (local_handle ==INVALID_HANDLE_VALUE ) + { +WCHAR *device_path_wchar =NULL ; +int size =sizeof (open_tun->device_path); + err =GetLastError (); +
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp) · line 1226
Quoted 10 lines
src/openvpnserv/interactive.c +10
-0+ device_path_wchar =malloc (size *sizeof (WCHAR )); +if (device_path_wchar) + { +MultiByteToWideChar (CP_UTF8 ,0 , open_tun->device_path, size, device_path_wchar, size); + device_path_wchar[size -1 ] =0 ; + } +MsgToEventLog (M_SYSERR ,TEXT ("Error opening tun device (%s)" ), device_path_wchar); +free (device_path_wchar); +return err; + }
Also this one -- I think we should just use the wide version of CreateFile -- all strings in OpenVPN.exe are supposed to be in utf8, so convert to widechar and call CreateFileW.
Selva
Hi,
Quoted 5 lines · Selva Nair
The way interactive service structures are coded should not require this at all, does it? The size and message type are already in the header, so why do we need to pass it? The result here is a new kind of ack message with a different size and type and that could be checked by accessing the header element. Unless I'm missing something.
You could, indeed, do this inside send_msg_iservice() by looking at "what request did we sent? so what should be coming back?" but I'm not sure I find this safe enough (caller allocates memory, and maybe we shouldn't rely on "it being big enough" unless explicitly told). It would work, yes, but leaves me with a bit uneasy feeling.
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Hi
Quoted 10 lines · Gert Doering
Hi,
Quoted 5 lines · Selva Nair
The way interactive service structures are coded should not require this at all, does it? The size and message type are already in the header, so why do we need to pass it? The result here is a new kind of ack message with a different size and type and that could be checked by accessing the header element. Unless I'm missing something.
Quoted 5 lines
You could, indeed, do this inside
send_msg_iservice()by looking at "what request did we sent? so what should be coming back?" but I'm not sure I find this safe enough (caller allocates memory, and maybe we shouldn't rely on "it being big enough" unless explicitly told). It would work, yes, but leaves me with a bit uneasy feeling.
I was thinking of dereferening the response pointer as a union and check the header size which the caller is supposed to have set. So change ack * to void * in send_msg_iservice() as in the patch, and treat it as ack or and extended ack depending on the specified size. Further, when reading from the pipe one should also check the size of data received matches what is expected.
It may be also useful to make all reply messages made up of an ack message plus optional additional data -- ie., all start with a header and an error code.
Selva
Quoted 9 lines
gert
--
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh MistressGert Doering - Munich, Germany [email protected]...
Hi,
Quoted 3 lines
The way interactive service structures are coded should not require this at all, does it? The size and message type are already in the header, so why do we need to pass it?
But we need to know the response size in send_msg_iservice() since we pass it to ReadFile(). So far we assumed that response is always ask_message_t
and we could do sizeof(*ack). With new response type this assumption doesn't hold so
as Gert suggested I added another version which accepts arbitrary response type and size.
bool
send_msg_iservice(HANDLE pipe, const void *data, size_t size,
ack_message_t *ack, const char *context)
{
return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack),
context);
}
bool
send_msg_iservice_ex(HANDLE pipe, const void *data, size_t size,
void *response, size_t response_size, const char
*context)
{
Will send V2 tomorrow with this and CreateFileW changes.
Signature · 1 line
-Lev
Hi,
Quoted 9 lines · Lev Stipakov
Hi,
Quoted 4 lines
The way interactive service structures are coded should not require this at all, does it? The size and message type are already in the header, so why do we need to pass it?
Quoted 4 lines
But we need to know the response size in
send_msg_iservice()since
we pass it toReadFile(). So far we assumed that response is alwaysask_message_tand we could dosizeof(*ack). With new response type this assumption doesn't hold so as Gert suggested I added another version which accepts arbitrary response type and size.
My point is that, this is not in the spirit of the rest of iservice code. See HandleMessage in interactive.c where the data is and then interpreted using the header type and size.
For what max-size to pass to ReadFile, we know it from the size in the header element of the struct.
Selva
Quoted 18 lines
bool
send_msg_iservice(HANDLE pipe, const void *data,size_tsize,
ack_message_t*ack, const char *context){ return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); }bool
send_msg_iservice_ex(HANDLE pipe, const void *data,size_tsize,
void *response,size_tresponse_size, const char *context)
{Will send V2 tomorrow with this and CreateFileW changes.
--
-Lev
Hi,
What I have in mind would also require editing all calls
to send_msg_iservice() which is essentially what Gert is objecting to. So ignore me -- a separate send_msg_iservice_ex may be the best option.
Selva
Quoted 46 lines · Selva Nair
Hi,
Quoted 9 lines · Lev Stipakov
Hi,
Quoted 4 lines
The way interactive service structures are coded should not require this at all, does it? The size and message type are already in the header, so why do we need to pass it?
Quoted 4 lines
But we need to know the response size in
send_msg_iservice()since
we pass it toReadFile(). So far we assumed that response is alwaysask_message_tand we could dosizeof(*ack). With new response type this assumption doesn't hold so as Gert suggested I added another version which accepts arbitrary response type and size.My point is that, this is not in the spirit of the rest of iservice code. See HandleMessage in
interactive.cwhere the data is and then interpreted using the header type and size.For what max-size to pass to ReadFile, we know it from the size in the header element of the struct.
Selva
Quoted 18 lines
bool
send_msg_iservice(HANDLE pipe, const void *data,size_tsize,
ack_message_t*ack, const char *context){ return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); }bool
send_msg_iservice_ex(HANDLE pipe, const void *data,size_tsize,
void *response,size_tresponse_size, const char *context)
{Will send V2 tomorrow with this and CreateFileW changes.
--
-Lev
PATCH v2 1/1 openvpnserv: enable interactive service to open tun
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling CreateFile() directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.
---
v2:
- introduce send_msg_iservice_ex() instead of changing signature of existing send_msg_iservice()
- use wchar_t strings in interactive service code
include/openvpn-msg.h | 12 +++++++++ src/openvpn/tun.c | 60 +++++++++++++++++++++++++++++++++++++------ src/openvpn/win32.c | 9 ++++++- src/openvpn/win32.h | 30 +++++++++++++++++++--- src/openvpnserv/interactive.c | 57 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 154 insertions(+), 14 deletions(-)
include/openvpn-msg.h +12 -0
@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {
@@ -117,4 +119,14 @@ typedef struct { interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_path[512 ]; +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +HANDLE handle; +int error_number; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/tun.c +52 -8
73 lines not on this page · raw message
src/openvpn/win32.c +8 -1
@@ -1476,12 +1476,19 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context) { + return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); +} + +bool +send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context) +{struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",
src/openvpn/win32.h +26 -4
@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe| -* and read the result in |ack|. Returns false on communication error. -* The string in |context| is used to prefix error messages. + +/** +* Send data to interactive service and receive response of type ack_message_t. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The pointer to ack_message_t structure to where response is written +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size,ack_message_t *ack,const char *context); +/** +* Send data to interactive service and receive response. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The buffer to where response is written +* @param response_size The size of response buffer +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. +*/ +bool send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context); +/* * Attempt to simulate fork/execve on Windows */
src/openvpnserv/interactive.c +56 -1
87 lines not on this page · raw message
Signature · 1 line
2.7.4
Hi,
I haven't compiled or run tested, but there are few issues that need to be addressed before that:
Quoted 123 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling
CreateFile()directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.Signed-off-by Lev Stipakov <[email protected]...>---
v2:
- introducesend_msg_iservice_ex()instead of changing signature of existingsend_msg_iservice()
- usewchar_tstrings in interactive service codeinclude/openvpn-msg.h | 12 +++++++++ src/openvpn/tun.c | 60 +++++++++++++++++++++++++++++++++++++------ src/openvpn/win32.c | 9 ++++++- src/openvpn/win32.h | 30 +++++++++++++++++++--- src/openvpnserv/interactive.c | 57 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 154 insertions(+), 14 deletions(-)include/openvpn-msg.h +12
-0@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {@@ -117,4 +119,14 @@ typedef struct { interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_path[512 ]; +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +HANDLE handle; +int error_number; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */ src/openvpn/tun.c +46
-8@@ -5248,6 +5248,43 @@ out:return ret; } +static HANDLE +service_open_tun_device (const HANDLE pipe,const char * device_path) +{ +open_tun_device_result_message_t result_msg; +struct gc_arena gc =gc_new (); +open_tun_device_message_t open_tun_device = { + .header = { + msg_open_tun_device, +sizeof (open_tun_device_message_t), +0 + } + }; + result_msg.handle =INVALID_HANDLE_VALUE ; + +strncpynt (open_tun_device.device_path, device_path,sizeof (open_tun_device.device_path)); + +if (!send_msg_iservice_ex (pipe, &open_tun_device,sizeof (open_tun_device), + &result_msg,sizeof (result_msg),"Open_tun_device" )) + { + goto out; + } + +if (result_msg.error_number !=NO_ERROR ) + { +msg (D_TUNTAP_INFO ,"TUN: opening tun handle using service failed: %s [status=%u device_path=%s]" , +strerror_win32 (result_msg.error_number, &gc), result_msg.error_number, device_path); + } +else + { +msg (M_INFO ,"Opened tun device %s using service" , device_path); + } + +out: +gc_free (&gc); +return result_msg.handle; +} +/* * Return a TAP name for netsh commands. */ @@ -5631,15 +5668,22 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun device_guid, TAP_WIN_SUFFIX); - tt->hand =CreateFile ( - device_path, -GENERIC_READ |GENERIC_WRITE , -0 ,/* was: FILE_SHARE_READ */ -0 , -OPEN_EXISTING , -FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , - 0 +if (tt->options.msg_channel) + { + tt->hand =service_open_tun_device (tt->options.msg_channel, device_path); + } +else + { + tt->hand = CreateFile( + device_path, + GENERIC_READ | GENERIC_WRITE,
src/openvpn/tun.c open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun · line 5689
Quoted 10 lines
src/openvpn/tun.c +6
-0+0 ,/* was: FILE_SHARE_READ */ +0 , +OPEN_EXISTING , +FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , +0 ); + }if (tt->hand ==INVALID_HANDLE_VALUE ) {
This only handles dev-node is unspecified. We need the same for the case where dev-node specified above this.
Quoted 35 lines
src/openvpn/win32.c +8
-1@@ -1476,12 +1476,19 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context) { + return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); +} + +bool +send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context) +{struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",src/openvpn/win32.h +0
-2@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe|
src/openvpn/win32.h int win32_version_info(void); · line 319
Quoted 10 lines
src/openvpn/win32.h +8
-2-* and read the result in |ack|. Returns false on communication error. -* The string in |context| is used to prefix error messages. + +/** +* Send data to interactive service and receive response of type ack_message_t. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The pointer to ack_message_t structure to where response is written
response --> ack
Quoted 30 lines
src/openvpn/win32.h +18
-0+* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size,ack_message_t *ack,const char *context); +/** +* Send data to interactive service and receive response. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The buffer to where response is written +* @param response_size The size of response buffer +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. +*/ +bool send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context); +/* * Attempt to simulate fork/execve on Windows */ src/openvpnserv/interactive.c +0
-0
src/openvpnserv/interactive.c static HANDLE exit_event = NULL; · line 60
Quoted 5 lines
src/openvpnserv/interactive.c +1
-1@@ -57,7 +57,7 @@ static HANDLE exit_event = NULL;static settings_t settings;static HANDLE rdns_semaphore =NULL ;#define RDNS_TIMEOUT 600/* seconds to wait for the semaphore */ - +static HANDLE ovpn_process =NULL ;/* used by DuplicateHandle() when passing tun device handle to openvpn process */
We service multiple OpenVPN processes, so a global variable is no good.
Some alternatives:
(i) Use GetNamedPipeClientProcessId() each time
(ii) save the processId in undo_lists (this list is local to the service thread)
Quoted 5 lines
src/openvpnserv/interactive.c +0
-0openvpn_service_t interactive_service = { interactive,@@ -1198,6 +1198,42 @@ HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)return err;
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp) · line 1208
Quoted 10 lines
src/openvpnserv/interactive.c +8
-0} +static DWORD +HandleOpenTunDeviceMessage (const open_tun_device_message_t *open_tun,HANDLE *remote_handle) +{ +DWORD err =0 ; + + *remote_handle =INVALID_HANDLE_VALUE ; + +LPWSTR device_path =utf8to16 (open_tun->device_path);
I think we must ensure device_path is null terminated as its received over the pipe from a user process. Our utf8to16() just assumes it is. There are a few other places where we've this defect but that has to be handled in a separate patch.
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp) · line 1216
Quoted 8 lines
src/openvpnserv/interactive.c +8
-0+if (!device_path) + { + err =ERROR_OUTOFMEMORY ; + goto out; + } + +HANDLE local_handle =CreateFileW (device_path,GENERIC_READ |GENERIC_WRITE ,0 ,0 , +OPEN_EXISTING ,FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED ,0 );
As wintun allows the device to be opened multiple times, we'll eventually need some protection against that. Saying this as the real purpose of this patch is to enable the use of wintun. But this is fine for tapwindows.
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp) · line 1219
Quoted 3 lines
src/openvpnserv/interactive.c +3
-0+ +if (local_handle ==INVALID_HANDLE_VALUE ) + {
Need err = GetLastError(); here. Otherwise we're returning success as the error code with an invalid handle.
Quoted 52 lines
src/openvpnserv/interactive.c +34
-0+MsgToEventLog (M_SYSERR ,TEXT ("Error opening tun device (%s)" ), device_path); + goto out; + } + +if (!DuplicateHandle (GetCurrentProcess (), local_handle, ovpn_process, remote_handle,0 , FALSE, +DUPLICATE_CLOSE_SOURCE |DUPLICATE_SAME_ACCESS )) + { + err =GetLastError (); +MsgToEventLog (M_SYSERR ,TEXT ("Error duplicating tun device handle" )); + } + +out: +free (device_path); + +return err; +} +static VOID HandleMessage (HANDLE pipe,DWORD bytes,DWORD count,LPHANDLE events,undo_lists_t *lists) {@@ -1210,6 +1246,7 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_listsblock_dns_message_t block_dns;dns_cfg_message_t dns;enable_dhcp_message_t dhcp; +open_tun_device_message_t open_tun; } msg;ack_message_t ack = { .header = {@@ -1277,6 +1314,22 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists }break ; +case msg_open_tun_device: +if (msg.header.size ==sizeof (msg.open_tun)) + { +open_tun_device_result_message_t res = { + .header = { + .type = msg_open_tun_device_result, + .size =sizeof (res), + .message_id = msg.header.message_id + } + }; + res.error_number =HandleOpenTunDeviceMessage (&msg.open_tun, &res.handle); +WritePipeAsync (pipe, &res,sizeof (res), count, events); +return ; + } +break ; +default : ack.error_number =ERROR_MESSAGE_TYPE ;MsgToEventLog (MSG_FLAGS_ERROR ,TEXT ("Unknown message type %d" ), msg.header.type);
src/openvpnserv/interactive.c RunOpenvpn(LPVOID p) · line 1659
Quoted 4 lines
src/openvpnserv/interactive.c +1
-0@@ -1603,6 +1656,8 @@ RunOpenvpn(LPVOID p)free (input); } + ovpn_process = proc_info.hProcess;
See above.
Selva
Selva
Quoted 6 lines
src/openvpnserv/interactive.c +1
-1+while (TRUE) {DWORD bytes =PeekNamedPipeAsync (ovpn_pipe,1 , &exit_event); --2.7.4
Hi,
This only handles dev-node is unspecified. We need the same for
Quoted 2 lines
the case where dev-node specified above this.
I never used that, so this has slipped from my attention. Will do.
Quoted 7 lines
We service multiple OpenVPN processes, so a global variable is no good.
Some alternatives:
(i) UseGetNamedPipeClientProcessId()each time
(ii) save the processId inundo_lists(this list is local to the service thread)
GetNamedPipeClientProcessId() returns service process id, since we open client pipe from service code and pass
handle to openvpn process:
svc_pipe = CreateFile(ovpn_pipe_name, GENERIC_READ | GENERIC_WRITE, 0,
&inheritable, OPEN_EXISTING, 0, NULL);
openvpn_sntprintf(cmdline, cmdline_size, L"openvpn %s --msg-channel
%lu",
sud.options, svc_pipe);I looked more closely at the code and turns out that instead of global variable you can just pass process id to HandleMessage():
CreateProcessAsUserW(pri_token, exe_path, cmdline, &ovpn_sa, NULL, TRUE,
settings.priority | CREATE_NO_WINDOW |CREATE_UNICODE_ENVIRONMENT,
user_env, sud.directory, &startup_info,
&proc_info);
while (TRUE)
{
DWORD bytes = PeekNamedPipeAsync(ovpn_pipe, 1, &exit_event);
HandleMessage(ovpn_pipe, proc_info.hProcess, bytes, 1, &exit_event,
&undo_lists);
}Quoted 5 lines
I think we must ensure
device_pathis null terminated as its received over the pipe from a user process. Ourutf8to16()just assumes it is. There are a few other places where we've this defect but that has to be handled in a separate patch.
Yep, meanwhile I'll just make sure that it is null terminated for this specific case:
msg.open_tun.device_path[sizeof(msg.open_tun.device_path) - 1] = '\0'; res.error_number = HandleOpenTunDeviceMessage(&msg.open_tun, ovpn_proc, &res.handle);
Quoted 3 lines
Need err =
GetLastError(); here. Otherwise we're returning success as the error code with an invalid handle.
Yep.
Selva
Quoted 1 line
Thanks for review, V3 is coming.
Signature · 1 line
-Lev
PATCH v3 1/1 openvpnserv: enable interactive service to open tun
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling CreateFile() directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.
---
v3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalid
v2:
- introduce send_msg_iservice_ex() instead of changing signature of existing send_msg_iservice()
- use wchar_t strings in interactive service code
include/openvpn-msg.h | 12 +++++++ src/openvpn/tun.c | 83 ++++++++++++++++++++++++++++++++++--------- src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++--- src/openvpnserv/interactive.c | 64 +++++++++++++++++++++++++++++++-- 5 files changed, 174 insertions(+), 24 deletions(-)
include/openvpn-msg.h +12 -0
@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {
@@ -117,4 +119,14 @@ typedef struct { interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_path[256 ]; +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +HANDLE handle; +int error_number; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/tun.c +67 -16
103 lines not on this page · raw message
src/openvpn/win32.c +8 -1
@@ -1476,12 +1476,19 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context) { + return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); +} + +bool +send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context) +{struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",
src/openvpn/win32.h +26 -4
@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe| -* and read the result in |ack|. Returns false on communication error. -* The string in |context| is used to prefix error messages. + +/** +* Send data to interactive service and receive response of type ack_message_t. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The pointer to ack_message_t structure to where response is written +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size,ack_message_t *ack,const char *context); +/** +* Send data to interactive service and receive response. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The buffer to where response is written +* @param response_size The size of response buffer +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. +*/ +bool send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context); +/* * Attempt to simulate fork/execve on Windows */
src/openvpnserv/interactive.c +61 -3
95 lines not on this page · raw message
Signature · 1 line
2.7.4
Hi,
Quoted 12 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling
CreateFile()directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.
Quoted 6 lines
Signed-off-by Lev Stipakov <[email protected]...>---
v3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalid
This works but there are two general concerns:
(i) The new message is named message_open_tun, but it allows opening any file using the service. This is not secure. We need to restrict it to open tun/tap device nodes only.
(ii) Should we allow all users to open tap6 adapters irrespective of any other access restrictions that may be present? I'm conflicted about this as, on closer look, access control in tap-windows6 appears broken.
include/openvpn-msg.h typedef struct { · line 130
Quoted 13 lines
include/openvpn-msg.h +10
-0@@ -117,4 +119,14 @@ typedef struct {interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_path[256 ]; +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +HANDLE handle; +int error_number; +}open_tun_device_result_message_t ;
Defining this struct with error_number followed by handle would be better (makes its head match in memory with ack_message_t). That makes it possible to read a normal ack into it and resolve the error number. Can happen if openvpn.exe is upgraded but service stays at an old version -- such a service will respond with ack and error_number=ERROR_MESSAGE_TYPE.
Selva
Hi,
Sorry for delay - I was on vacation.
(i) The new message is named message_open_tun, but it allows opening
Quoted 1 line
any file using the service. This is not secure.
I am thinking of possible vector of attack here.
In our case it is service which launches openvpn process using path set in registry, opens pipe and passes pipe handle to launched process.
To make service run malicious process one needs either to replace executable or
modify registry. For both actions you need to be administrator, assuming default security policy.
Alternatively, can random process hijack the pipe handle from another process?
Quoted 2 lines
We need to restrict it to open tun/tap device nodes only.
Without adding too much code, I can think of:
- check that path starts with \\\\.\\Global\\ to make sure we open device,
not file
and
- check that path starts with \\\\.\\Global\\WINTUN or ends with .tap
Is this good enough or do you have better ideas?
(ii) Should we allow all users to open tap6 adapters irrespective of
Quoted 4 lines
any other access restrictions that may be present? I'm conflicted about this as, on closer look, access control in tap-windows6 appears broken.
I'll pass on this one. Maybe "Strengthening access control for tap-windows6" could be another patch.
Quoted 4 lines
Defining this struct with
error_numberfollowed by handle would be better (makes its head match in memory withack_message_t). That makes it possible to read a normal ack into it and resolve the error number.
Agreed, will do.
Signature · 1 line
-Lev
PATCH v4 1/1 openvpnserv: enable interactive service to open tun
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling CreateFile() directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.
---
v4:
- strengthen security by adding basic validation to device path
- reorder fields in msg_open_tun_device_result for backward compatibility
v3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalid
v2:
- introduce send_msg_iservice_ex() instead of changing signature of existing send_msg_iservice()
- use wchar_t strings in interactive service code
include/openvpn-msg.h | 12 ++++++ src/openvpn/tun.c | 83 +++++++++++++++++++++++++++++++++-------- src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++-- src/openvpnserv/interactive.c | 86 +++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 196 insertions(+), 24 deletions(-)
include/openvpn-msg.h +12 -0
@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {
@@ -117,4 +119,14 @@ typedef struct { interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_path[256 ]; +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +int error_number; +HANDLE handle; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/tun.c +67 -16
103 lines not on this page · raw message
src/openvpn/win32.c +8 -1
@@ -1476,12 +1476,19 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context) { + return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); +} + +bool +send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context) +{struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",
src/openvpn/win32.h +26 -4
@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe| -* and read the result in |ack|. Returns false on communication error. -* The string in |context| is used to prefix error messages. + +/** +* Send data to interactive service and receive response of type ack_message_t. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The pointer to ack_message_t structure to where response is written +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size,ack_message_t *ack,const char *context); +/** +* Send data to interactive service and receive response. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The buffer to where response is written +* @param response_size The size of response buffer +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. +*/ +bool send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context); +/* * Attempt to simulate fork/execve on Windows */
src/openvpnserv/interactive.c +83 -3
117 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v5 1/1 openvpnserv: enable interactive service to open tun
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling CreateFile() directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.
---
v5:
- futher strengthen security by passing only device guid from client process to service, validating guid and contructing device path on service side
v4:
- strengthen security by adding basic validation to device path
- reorder fields in msg_open_tun_device_result for backward compatibility
v3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalid
v2:
- introduce send_msg_iservice_ex() instead of changing signature of existing send_msg_iservice()
- use wchar_t strings in interactive service code
include/openvpn-msg.h | 12 +++++++ src/openvpn/tun.c | 83 ++++++++++++++++++++++++++++++++++--------- src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++--- src/openvpnserv/interactive.c | 83 +++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 193 insertions(+), 24 deletions(-)
include/openvpn-msg.h +12 -0
@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {
@@ -117,4 +119,14 @@ typedef struct { interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_guid[36 +2 +1 ];/* length of guid with dashes + braces + nul terminator */ +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +int error_number; +HANDLE handle; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/tun.c +67 -16
103 lines not on this page · raw message
src/openvpn/win32.c +8 -1
@@ -1476,12 +1476,19 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context) { + return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); +} + +bool +send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context) +{struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",
src/openvpn/win32.h +26 -4
@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe| -* and read the result in |ack|. Returns false on communication error. -* The string in |context| is used to prefix error messages. + +/** +* Send data to interactive service and receive response of type ack_message_t. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The pointer to ack_message_t structure to where response is written +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size,ack_message_t *ack,const char *context); +/** +* Send data to interactive service and receive response. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The buffer to where response is written +* @param response_size The size of response buffer +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. +*/ +bool send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context); +/* * Attempt to simulate fork/execve on Windows */
src/openvpnserv/interactive.c +80 -3
114 lines not on this page · raw message
Signature · 1 line
2.7.4
Hi,
Quoted 11 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling
CreateFile()directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.
Quoted 6 lines
Signed-off-by Lev Stipakov <[email protected]...>---
v5:
- futher strengthen security by passing only device guid from client
process
"further"
Quoted 2 lines
to service, validating guid and contructing device path on service side
"constructing"
Quoted 15 lines
v4:
- strengthen security by adding basic validation to device path
- reorder fields inmsg_open_tun_device_resultfor backward compatibilityv3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalidv2:
- introducesend_msg_iservice_ex()instead of changing signature of existingsend_msg_iservice()
- usewchar_tstrings in interactive service code
Quoted 8 lines
include/openvpn-msg.h | 12 +++++++ src/openvpn/tun.c | 83 ++++++++++++++++++++++++++++++++++--------- src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++--- src/openvpnserv/interactive.c | 83 +++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 193 insertions(+), 24 deletions(-)
src/openvpn/win32.h +0 -0
Quoted 17 lines
index 4814bbc..8ccddc0 100644
src/openvpn/win32.h +0
-2@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe| - * and read the result in |ack|. Returns false on communication error. - * The string in |context| is used to prefix error messages.src/openvpn/win32.h +3
-0+ +/** +* Send data to interactive service and receive response of type
ack_message_t.
+ *
Quoted 6 lines
+ * @param pipe The handle of communication pipe + * @param data The data to send + * @param size The size of data to send + * @param response The pointer to ack_message_t structure to whereresponse is written
That would be "@param ack"
Quoted 54 lines
+ * @param context The string used to prefix error messages
+ *
+ * @returns True on success, false on failure.
*/
bool send_msg_iservice(HANDLE pipe, const void *data,size_tsize,ack_message_t *ack, const char *context);src/openvpnserv/interactive.c +25
-1@@ -58,7 +58,6 @@ static settings_t settings;static HANDLE rdns_semaphore =NULL ;#define RDNS_TIMEOUT 600/* seconds to wait for the semaphore */ -openvpn_service_t interactive_service = { interactive,TEXT (PACKAGE_NAME "ServiceInteractive" ),@@ -1198,8 +1197,67 @@ HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)return err; } +static DWORD +HandleOpenTunDeviceMessage (const open_tun_device_message_t *open_tun,HANDLE ovpn_proc,HANDLE *remote_handle) +{ +DWORD err =ERROR_SUCCESS ; +HANDLE local_handle; +LPWSTR wguid =NULL ; +WCHAR device_path[256 ] = {0 }; +const WCHAR prefix[] =L"\\\\.\\Global\\" ; +const WCHAR tap_suffix[] =L".tap" ; + + *remote_handle =INVALID_HANDLE_VALUE ; + + wguid =utf8to16 (open_tun->device_guid); +if (!wguid) + { + err =ERROR_OUTOFMEMORY ; + goto out; + } + +/* validate device guid */ +const size_t guid_len =wcslen (wguid); +for (int i =0 ; i < guid_len; ++i) + { +const WCHAR ch = wguid[i]; +
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t · line 1236
Quoted 10 lines
src/openvpnserv/interactive.c +7
-0+if (!(iswalnum (ch)) && (ch !=L'-' ) && (ch !=L'{' ) && (ch !=L'}' )) + { + err =ERROR_MESSAGE_DATA ; +MsgToEventLog (MSG_FLAGS_ERROR ,TEXT ("Invalid device guild (%s)"), wguid); + goto out; + } + }
I think the check could be made tighter than alpha numerics (only hex digits need be allowed) and simpler like:
if (guid_len != 38 || wcspn(wguid, L"0123456789ABCDEFabcdef-{}") != guid_len)
{
<handle the error and goto out>
}
I would have preferred to do a GUID parsing to validate it, but may be this is good enough?
+
src/openvpnserv/interactive.c
Quoted 6 lines
src/openvpnserv/interactive.c +1
-0+ openvpn_swprintf(device_path, sizeof(device_path), L"%s%s%s", prefix,wguid,
tap_suffix);src/openvpnserv/interactive.c +3
-0+ +/* open tun device */ +
...
Thanks,
Selva
PATCH v6 1/1 openvpnserv: enable interactive service to open tun
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling CreateFile() directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.
---
v6:
- simplify and strengthen guid check with wcsspn()
- fix doxygen comment
v5:
- further strengthen security by passing only device guid from client process to service, validating guid and constructing device path on service side
v4:
- strengthen security by adding basic validation to device path
- reorder fields in msg_open_tun_device_result for backward compatibility
v3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalid
v2:
- introduce send_msg_iservice_ex() instead of changing signature of existing send_msg_iservice()
- use wchar_t strings in interactive service code
include/openvpn-msg.h | 12 +++++++ src/openvpn/tun.c | 83 ++++++++++++++++++++++++++++++++++--------- src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++--- src/openvpnserv/interactive.c | 78 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 188 insertions(+), 24 deletions(-)
include/openvpn-msg.h +12 -0
@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {
@@ -117,4 +119,14 @@ typedef struct { interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_guid[36 +2 +1 ];/* length of guid with dashes + braces + nul terminator */ +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +int error_number; +HANDLE handle; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/tun.c +67 -16
103 lines not on this page · raw message
src/openvpn/win32.c +8 -1
@@ -1476,12 +1476,19 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context) { + return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); +} + +bool +send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context) +{struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",
src/openvpn/win32.h +26 -4
@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe| -* and read the result in |ack|. Returns false on communication error. -* The string in |context| is used to prefix error messages. + +/** +* Send data to interactive service and receive response of type ack_message_t. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param ack The pointer to ack_message_t structure to where response is written +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size,ack_message_t *ack,const char *context); +/** +* Send data to interactive service and receive response. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The buffer to where response is written +* @param response_size The size of response buffer +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. +*/ +bool send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context); +/* * Attempt to simulate fork/execve on Windows */
src/openvpnserv/interactive.c +75 -3
109 lines not on this page · raw message
Signature · 1 line
2.7.4
Looks like a typo below:
Quoted 281 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling
CreateFile()directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.Signed-off-by Lev Stipakov <[email protected]...>---
v6:
- simplify and strengthen guid check withwcsspn()
- fix doxygen commentv5:
- further strengthen security by passing only device guid from client process to service, validating guid and constructing device path on service sidev4:
- strengthen security by adding basic validation to device path
- reorder fields inmsg_open_tun_device_resultfor backward compatibilityv3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalidv2:
- introducesend_msg_iservice_ex()instead of changing signature of existingsend_msg_iservice()
- usewchar_tstrings in interactive service codeinclude/openvpn-msg.h | 12 +++++++ src/openvpn/tun.c | 83 ++++++++++++++++++++++++++++++++++--------- src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++--- src/openvpnserv/interactive.c | 78 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 188 insertions(+), 24 deletions(-)include/openvpn-msg.h +12
-0@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {@@ -117,4 +119,14 @@ typedef struct { interface_t iface; } enable_dhcp_message_t; +typedef struct { +message_header_t header; +char device_guid[36 +2 +1 ];/* length of guid with dashes + braces + nul terminator */ +}open_tun_device_message_t ; + +typedef struct { +message_header_t header; +int error_number; +HANDLE handle; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */ src/openvpn/tun.c +67
-16@@ -5248,6 +5248,43 @@ out:return ret; } +static HANDLE +service_open_tun_device (const HANDLE pipe,const char * device_guid) +{ +open_tun_device_result_message_t result_msg; +struct gc_arena gc =gc_new (); +open_tun_device_message_t open_tun_device = { + .header = { + msg_open_tun_device, +sizeof (open_tun_device_message_t), +0 + } + }; + result_msg.handle =INVALID_HANDLE_VALUE ; + +strncpynt (open_tun_device.device_guid, device_guid,sizeof (open_tun_device.device_guid)); + +if (!send_msg_iservice_ex (pipe, &open_tun_device,sizeof (open_tun_device), + &result_msg,sizeof (result_msg),"Open_tun_device" )) + { + goto out; + } + +if (result_msg.error_number !=NO_ERROR ) + { +msg (D_TUNTAP_INFO ,"TUN: opening tun handle using service failed: %s [status=%u device_path=%s]" , +strerror_win32 (result_msg.error_number, &gc), result_msg.error_number, device_guid); + } +else + { +msg (M_INFO ,"Opened tun device %s using service" , device_guid); + } + +out: +gc_free (&gc); +return result_msg.handle; +} +/* * Return a TAP name for netsh commands. */ @@ -5591,15 +5628,22 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun device_guid, TAP_WIN_SUFFIX); - tt->hand =CreateFile ( - device_path, -GENERIC_READ |GENERIC_WRITE , -0 ,/* was: FILE_SHARE_READ */ -0 , -OPEN_EXISTING , -FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , -0 +if (tt->options.msg_channel) + { + tt->hand =service_open_tun_device (tt->options.msg_channel, device_guid); + } +else + { + tt->hand =CreateFile ( + device_path, +GENERIC_READ |GENERIC_WRITE , +0 ,/* was: FILE_SHARE_READ */ +0 , +OPEN_EXISTING , +FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , +0 ); + }if (tt->hand ==INVALID_HANDLE_VALUE ) {@@ -5631,15 +5675,22 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun device_guid, TAP_WIN_SUFFIX); - tt->hand =CreateFile ( - device_path, -GENERIC_READ |GENERIC_WRITE , -0 ,/* was: FILE_SHARE_READ */ -0 , -OPEN_EXISTING , -FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , -0 +if (tt->options.msg_channel) + { + tt->hand =service_open_tun_device (tt->options.msg_channel, device_guid); + } +else + { + tt->hand =CreateFile ( + device_path, +GENERIC_READ |GENERIC_WRITE , +0 ,/* was: FILE_SHARE_READ */ +0 , +OPEN_EXISTING , +FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , +0 ); + }if (tt->hand ==INVALID_HANDLE_VALUE ) {src/openvpn/win32.c +8
-1@@ -1476,12 +1476,19 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context) { + return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack), context); +} + +bool +send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context) +{struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",src/openvpn/win32.h +26
-4@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe| -* and read the result in |ack|. Returns false on communication error. -* The string in |context| is used to prefix error messages. + +/** +* Send data to interactive service and receive response of type ack_message_t. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param ack The pointer to ack_message_t structure to where response is written +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size,ack_message_t *ack,const char *context); +/** +* Send data to interactive service and receive response. +* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param response The buffer to where response is written +* @param response_size The size of response buffer +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. +*/ +bool send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, +void *response,size_t response_size,const char *context); +/* * Attempt to simulate fork/execve on Windows */ src/openvpnserv/interactive.c +15
-1@@ -58,7 +58,6 @@ static settings_t settings;static HANDLE rdns_semaphore =NULL ;#define RDNS_TIMEOUT 600/* seconds to wait for the semaphore */ -openvpn_service_t interactive_service = { interactive,TEXT (PACKAGE_NAME "ServiceInteractive" ),@@ -1198,8 +1197,62 @@ HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)return err; } +static DWORD +HandleOpenTunDeviceMessage (const open_tun_device_message_t *open_tun,HANDLE ovpn_proc,HANDLE *remote_handle) +{ +DWORD err =ERROR_SUCCESS ; +HANDLE local_handle; +LPWSTR wguid =NULL ; +WCHAR device_path[256 ] = {0 }; +const WCHAR prefix[] =L"\\\\.\\Global\\" ; +const WCHAR tap_suffix[] =L".tap" ; + + *remote_handle =INVALID_HANDLE_VALUE ; + + wguid =utf8to16 (open_tun->device_guid); +if (!wguid) + {
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp) · line 1224
Quoted 10 lines
src/openvpnserv/interactive.c +10
-0+ err =ERROR_OUTOFMEMORY ; + goto out; + } + +/* validate device guid */ +const size_t guid_len =wcslen (wguid); +if (guid_len !=38 ||wcsspn (wguid,L"0123456789ABCDEFabcdef-{}" ) != guid_len) + { + err =ERROR_MESSAGE_DATA ; +MsgToEventLog (MSG_FLAGS_ERROR ,TEXT ("Invalid device guild (%s)" ), wguid);
Invalid device "guild" ? I presume guid ..
Quoted 78 lines
src/openvpnserv/interactive.c +50
-2+ goto out; + } + +openvpn_swprintf (device_path,sizeof (device_path),L"%s%s%s" , prefix, wguid, tap_suffix); + +/* open tun device */ + local_handle =CreateFileW (device_path,GENERIC_READ |GENERIC_WRITE ,0 ,0 , +OPEN_EXISTING ,FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED ,0 ); +if (local_handle ==INVALID_HANDLE_VALUE ) + { + err =GetLastError (); +MsgToEventLog (M_SYSERR ,TEXT ("Error opening tun device (%s)" ), device_path); + goto out; + } + +/* duplicate tun device handle to openvpn process */ +if (!DuplicateHandle (GetCurrentProcess (), local_handle, ovpn_proc, remote_handle,0 , FALSE, +DUPLICATE_CLOSE_SOURCE |DUPLICATE_SAME_ACCESS )) + { + err =GetLastError (); +MsgToEventLog (M_SYSERR ,TEXT ("Error duplicating tun device handle" )); + } + +out: +free (wguid); + +return err; +} +static VOID -HandleMessage (HANDLE pipe,DWORD bytes,DWORD count,LPHANDLE events,undo_lists_t *lists) +HandleMessage (HANDLE pipe,HANDLE ovpn_proc,DWORD bytes,DWORD count,LPHANDLE events,undo_lists_t *lists) {DWORD read;union {@@ -1210,6 +1263,7 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_listsblock_dns_message_t block_dns;dns_cfg_message_t dns;enable_dhcp_message_t dhcp; +open_tun_device_message_t open_tun; } msg;ack_message_t ack = { .header = {@@ -1277,6 +1331,24 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists }break ; +case msg_open_tun_device: +if (msg.header.size ==sizeof (msg.open_tun)) + { +open_tun_device_result_message_t res = { + .header = { + .type = msg_open_tun_device_result, + .size =sizeof (res), + .message_id = msg.header.message_id + } + }; +/* make sure that string passed from user process is null-terminated */ + msg.open_tun.device_guid[sizeof (msg.open_tun.device_guid) -1 ] ='\0' ; + res.error_number =HandleOpenTunDeviceMessage (&msg.open_tun, ovpn_proc, &res.handle); +WritePipeAsync (pipe, &res,sizeof (res), count, events); +return ; + } +break ; +default : ack.error_number =ERROR_MESSAGE_TYPE ;MsgToEventLog (MSG_FLAGS_ERROR ,TEXT ("Unknown message type %d" ), msg.header.type);@@ -1611,7 +1683,7 @@ RunOpenvpn(LPVOID p)break ; } -HandleMessage (ovpn_pipe, bytes,1 , &exit_event, &undo_lists); +HandleMessage (ovpn_pipe, proc_info.hProcess, bytes,1 , &exit_event, &undo_lists); }WaitForSingleObject (proc_info.hProcess,IO_TIMEOUT );
This could be fixed by @cron2 when he will apply this patch :)
ti 23. heinäk. 2019 klo 15.12 tincanteksup ([email protected]...) kirjoitti:
Quoted 424 lines
Looks like a typo below:
Quoted 11 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling
CreateFile()directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.Quoted 8 lines
Signed-off-by Lev Stipakov <[email protected]...>---
v6:
- simplify and strengthen guid check withwcsspn()
- fix doxygen commentv5:
- further strengthen security by passing only device guid from clientprocess
Quoted 1 line
to service, validating guid and constructing device path on service
side
Quoted 4 lines
v4:
- strengthen security by adding basic validation to device path
- reorder fields inmsg_open_tun_device_resultfor backwardcompatibility
Quoted 6 lines
v3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalidQuoted 7 lines
v2:
- introducesend_msg_iservice_ex()instead of changing signature of existingsend_msg_iservice()
- usewchar_tstrings in interactive service codeinclude/openvpn-msg.h | 12 +++++++ src/openvpn/tun.c | 83++++++++++++++++++++++++++++++++++---------
Quoted 3 lines
src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++--- src/openvpnserv/interactive.c | 78++++++++++++++++++++++++++++++++++++++--
Quoted 16 lines
5 files changed, 188 insertions(+), 24 deletions(-)include/openvpn-msg.h +2
-0@@ -39,6 +39,8 @@ typedef enum { msg_del_block_dns, msg_register_dns, msg_enable_dhcp, + msg_open_tun_device, + msg_open_tun_device_result, } message_type_t; typedef struct {include/openvpn-msg.h typedef struct { · line 124
Quoted 6 lines
include/openvpn-msg.h +3
-0@@ -117,4 +119,14 @@ typedef struct {interface_t iface; } enable_dhcp_message_t; +typedef struct { + message_header_t header; + char device_guid[36+2+1]; /* length of guid with dashes + braces +nul terminator */
Quoted 21 lines
include/openvpn-msg.h +7
-0+} open_tun_device_message_t; + +typedef struct { +message_header_t header; +int error_number; +HANDLE handle; +}open_tun_device_result_message_t ;#endif /* ifndef OPENVPN_MSG_H_ */ src/openvpn/tun.c +5
-0@@ -5248,6 +5248,43 @@ out:return ret; } +static HANDLE +service_open_tun_device (const HANDLE pipe,const char * device_guid) +{ +open_tun_device_result_message_t result_msg; +struct gc_arena gc =gc_new ();src/openvpn/tun.c out: · line 5265
Quoted 10 lines
src/openvpn/tun.c +10
-0+open_tun_device_message_t open_tun_device = { + .header = { + msg_open_tun_device, +sizeof (open_tun_device_message_t), +0 + } + }; + result_msg.handle =INVALID_HANDLE_VALUE ; + + strncpynt(open_tun_device.device_guid, device_guid,
sizeof(open_tun_device.device_guid));src/openvpn/tun.c out: · line 5267
Quoted 2 lines
src/openvpn/tun.c +2
-0+ + if (!send_msg_iservice_ex(pipe, &open_tun_device,
sizeof(open_tun_device),src/openvpn/tun.c out:
Quoted 8 lines
src/openvpn/tun.c +8
-0+ &result_msg, sizeof(result_msg), "Open_tun_device")) + { + goto out; + } + +if (result_msg.error_number !=NO_ERROR ) + { + msg(D_TUNTAP_INFO, "TUN: opening tun handle using servicefailed: %s [status=%u device_path=%s]",
src/openvpn/tun.c out: · line 5276
Quoted 1 line
src/openvpn/tun.c +1
-0+ strerror_win32(result_msg.error_number, &gc),result_msg.error_number,
device_guid);src/openvpn/tun.c open_tun(const char *dev, const char *dev_type,
Quoted 15 lines
src/openvpn/tun.c +11
-0+ } +else + { +msg (M_INFO ,"Opened tun device %s using service" , device_guid); + } + +out: +gc_free (&gc); +return result_msg.handle; +} +/* * Return a TAP name for netsh commands. */ @@ -5591,15 +5628,22 @@ open_tun(const char *dev, const char *dev_type,const char
*dev_node, struct tunQuoted 4 lines
src/openvpn/tun.c +0
-1device_guid, TAP_WIN_SUFFIX); - tt->hand = CreateFile(src/openvpn/tun.c open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
Quoted 10 lines
src/openvpn/tun.c +3
-7- device_path, -GENERIC_READ |GENERIC_WRITE , -0 ,/* was: FILE_SHARE_READ */ -0 , -OPEN_EXISTING , -FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , -0 +if (tt->options.msg_channel) + { + tt->hand =
service_open_tun_device(tt->options.msg_channel, device_guid);src/openvpn/tun.c open_tun(const char *dev, const char *dev_type,
Quoted 17 lines
src/openvpn/tun.c +12
-0+ } +else + { + tt->hand =CreateFile ( + device_path, +GENERIC_READ |GENERIC_WRITE , +0 ,/* was: FILE_SHARE_READ */ +0 , +OPEN_EXISTING , +FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , +0 ); + }if (tt->hand ==INVALID_HANDLE_VALUE ) {@@ -5631,15 +5675,22 @@ open_tun(const char *dev, const char *dev_type,const char
*dev_node, struct tunQuoted 4 lines
src/openvpn/tun.c +0
-1device_guid, TAP_WIN_SUFFIX); - tt->hand = CreateFile(src/openvpn/tun.c open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
Quoted 10 lines
src/openvpn/tun.c +3
-7- device_path, -GENERIC_READ |GENERIC_WRITE , -0 ,/* was: FILE_SHARE_READ */ -0 , -OPEN_EXISTING , -FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , -0 +if (tt->options.msg_channel) + { + tt->hand =
service_open_tun_device(tt->options.msg_channel, device_guid);Quoted 21 lines
src/openvpn/tun.c +12
-0+ } +else + { + tt->hand =CreateFile ( + device_path, +GENERIC_READ |GENERIC_WRITE , +0 ,/* was: FILE_SHARE_READ */ +0 , +OPEN_EXISTING , +FILE_ATTRIBUTE_SYSTEM |FILE_FLAG_OVERLAPPED , +0 ); + }if (tt->hand ==INVALID_HANDLE_VALUE ) {src/openvpn/win32.c +0
-0src/openvpn/win32.c bool · line 1479
Quoted 4 lines
src/openvpn/win32.c +1
-0@@ -1476,12 +1476,19 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context) { + return send_msg_iservice_ex(pipe, data, size, ack, sizeof(*ack),context);
src/openvpn/win32.c bool
Quoted 5 lines
src/openvpn/win32.c +5
-0+} + +bool +send_msg_iservice_ex (HANDLE pipe,const void *data,size_t size, + void *response, size_t response_size, const char*context)
Quoted 16 lines
src/openvpn/win32.c +2
-1+{struct gc_arena gc =gc_new ();DWORD len;bool ret = true;if (!WriteFile (pipe, data, size, &len,NULL ) - || !ReadFile (pipe, ack,sizeof (*ack), &len,NULL )) + || !ReadFile (pipe, response, response_size, &len,NULL )) { msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",src/openvpn/win32.h +0
-0src/openvpn/win32.h int win32_version_info(void); · line 313
Quoted 5 lines
src/openvpn/win32.h +0
-2@@ -309,14 +309,36 @@ int win32_version_info(void);*/ const char *win32_version_string (struct gc_arena *gc,bool add_name); -/* -* Send the |size| bytes in buffer |data| to the interactive service |pipe| > - * and read the result in |ack|. Returns false on communication error. > - * The string in |context| is used to prefix error messages.src/openvpn/win32.h int win32_version_info(void);
Quoted 3 lines
src/openvpn/win32.h +3
-0+ +/** +* Send data to interactive service and receive response of type
ack_message_t.src/openvpn/win32.h int win32_version_info(void);
Quoted 5 lines
src/openvpn/win32.h +5
-0+* +* @param pipe The handle of communication pipe +* @param data The data to send +* @param size The size of data to send +* @param ack The pointer to ack_message_t structure to where response is written
Quoted 11 lines
src/openvpn/win32.h +7
-0+* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. */ bool send_msg_iservice (HANDLE pipe,const void *data,size_t size,ack_message_t *ack,const char *context); +/** +* Send data to interactive service and receive response. +* +* @param pipe The handle of communication pipe src/openvpn/win32.h int win32_version_info(void);
Quoted 10 lines
src/openvpn/win32.h +10
-0+* @param data The data to send +* @param size The size of data to send +* @param response The buffer to where response is written +* @param response_size The size of response buffer +* @param context The string used to prefix error messages +* +* @returns True on success, false on failure. +*/ +bool send_msg_iservice_ex(HANDLE pipe, const void *data, size_t size, + void *response, size_t response_size, constchar *context);
Review
Quoted 5 lines
src/openvpn/win32.h +1
-0+/* * Attempt to simulate fork/execve on Windows */ (quoted diff) +0
-0
b/src/openvpnserv/interactive.csrc/openvpnserv/interactive.c HandleEnableDHCPMessage(const
Quoted 12 lines
index 623c3ff..1b4a5e2 100644
src/openvpnserv/interactive.c +0
-1@@ -58,7 +58,6 @@ static settings_t settings;static HANDLE rdns_semaphore =NULL ;#define RDNS_TIMEOUT 600/* seconds to wait for the semaphore */ -openvpn_service_t interactive_service = { interactive,TEXT (PACKAGE_NAME "ServiceInteractive" ),@@ -1198,8 +1197,62 @@ HandleEnableDHCPMessage(const
enable_dhcp_message_t*dhcp)src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 5 lines
src/openvpnserv/interactive.c +2
-0return err; } +static DWORD +HandleOpenTunDeviceMessage(const open_tun_device_message_t *open_tun,HANDLE
ovpn_proc, HANDLE*remote_handle)Quoted 10 lines
src/openvpnserv/interactive.c +10
-0+{ +DWORD err =ERROR_SUCCESS ; +HANDLE local_handle; +LPWSTR wguid =NULL ; +WCHAR device_path[256 ] = {0 }; +const WCHAR prefix[] =L"\\\\.\\Global\\" ; +const WCHAR tap_suffix[] =L".tap" ; + + *remote_handle =INVALID_HANDLE_VALUE ; +src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 10 lines
src/openvpnserv/interactive.c +10
-0+ wguid =utf8to16 (open_tun->device_guid); +if (!wguid) + { + err =ERROR_OUTOFMEMORY ; + goto out; + } + +/* validate device guid */ +const size_t guid_len =wcslen (wguid); + if (guid_len != 38 || wcsspn(wguid, L"0123456789ABCDEFabcdef-{}")!=
guid_len)src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 3 lines
src/openvpnserv/interactive.c +3
-0+ { + err =ERROR_MESSAGE_DATA ; + MsgToEventLog(MSG_FLAGS_ERROR, TEXT("Invalid device guild(%s)"), wguid);
Invalid device "guild" ? I presume guid ..
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const · line 1228
Quoted 4 lines
src/openvpnserv/interactive.c +4
-0+ goto out; + } + + openvpn_swprintf(device_path, sizeof(device_path), L"%s%s%s",prefix, wguid,
tap_suffix);src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 3 lines
src/openvpnserv/interactive.c +3
-0+ +/* open tun device */ + local_handle = CreateFileW(device_path, GENERIC_READ |
GENERIC_WRITE, 0, 0,src/openvpnserv/interactive.c HandleEnableDHCPMessage(const · line 1232
Quoted 1 line
src/openvpnserv/interactive.c +1
-0+ OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM |
FILE_FLAG_OVERLAPPED, 0);src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 4 lines
src/openvpnserv/interactive.c +4
-0+if (local_handle ==INVALID_HANDLE_VALUE ) + { + err =GetLastError (); + MsgToEventLog(M_SYSERR, TEXT("Error opening tun device (%s)"),
device_path);src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 5 lines
src/openvpnserv/interactive.c +5
-0+ goto out; + } + +/* duplicate tun device handle to openvpn process */ + if (!DuplicateHandle(GetCurrentProcess(), local_handle, ovpn_proc,
remote_handle, 0, FALSE,src/openvpnserv/interactive.c HandleEnableDHCPMessage(const · line 1242
Quoted 1 line
src/openvpnserv/interactive.c +1
-0+ DUPLICATE_CLOSE_SOURCE |
DUPLICATE_SAME_ACCESS))src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 3 lines
src/openvpnserv/interactive.c +3
-0+ { + err =GetLastError (); + MsgToEventLog(M_SYSERR, TEXT("Error duplicating tun devicehandle"));
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 10 lines
src/openvpnserv/interactive.c +8
-1+ } + +out: +free (wguid); + +return err; +} + static VOID -HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events,
undo_lists_t*lists)src/openvpnserv/interactive.c HandleEnableDHCPMessage(const · line 1255
Quoted 1 line
src/openvpnserv/interactive.c +1
-0+HandleMessage(HANDLE pipe, HANDLE ovpn_proc, DWORD bytes, DWORD count,LPHANDLE events,
undo_lists_t*lists)src/openvpnserv/interactive.c HandleMessage(HANDLE pipe, DWORD bytes, DWORD
Quoted 4 lines
src/openvpnserv/interactive.c +0
-0{DWORD read;union {@@ -1210,6 +1263,7 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORDcount, LPHANDLE events,
undo_listssrc/openvpnserv/interactive.c HandleMessage(HANDLE pipe, DWORD bytes, DWORD
Quoted 8 lines
src/openvpnserv/interactive.c +1
-0block_dns_message_t block_dns;dns_cfg_message_t dns;enable_dhcp_message_t dhcp; +open_tun_device_message_t open_tun; } msg;ack_message_t ack = { .header = {@@ -1277,6 +1331,24 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORDcount, LPHANDLE events,
undo_listsQuoted 4 lines
src/openvpnserv/interactive.c +1
-0}break ; +case msg_open_tun_device:src/openvpnserv/interactive.c HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists
Quoted 10 lines
src/openvpnserv/interactive.c +10
-0+if (msg.header.size ==sizeof (msg.open_tun)) + { +open_tun_device_result_message_t res = { + .header = { + .type = msg_open_tun_device_result, + .size =sizeof (res), + .message_id = msg.header.message_id + } + }; +/* make sure that string passed from user process is null-terminated */
src/openvpnserv/interactive.c HandleMessage(HANDLE pipe, DWORD bytes, DWORD · line 1345
Quoted 1 line
src/openvpnserv/interactive.c +1
-0+msg.open_tun.device_guid[sizeof(msg.open_tun.device_guid) - 1] = '\0';
src/openvpnserv/interactive.c HandleMessage(HANDLE pipe, DWORD bytes, DWORD · line 1346
Quoted 1 line
src/openvpnserv/interactive.c +1
-0+ res.error_number =
HandleOpenTunDeviceMessage(&msg.open_tun, ovpn_proc, &res.handle);src/openvpnserv/interactive.c HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists
Quoted 8 lines
src/openvpnserv/interactive.c +5
-0+WritePipeAsync (pipe, &res,sizeof (res), count, events); +return ; + } +break ; +default : ack.error_number =ERROR_MESSAGE_TYPE ; MsgToEventLog(MSG_FLAGS_ERROR, TEXT("Unknown message type%d"), msg.header.type);
src/openvpnserv/interactive.c RunOpenvpn(LPVOID p) · line 1686
Quoted 6 lines
src/openvpnserv/interactive.c +1
-1@@ -1611,7 +1683,7 @@ RunOpenvpn(LPVOID p)break ; } -HandleMessage (ovpn_pipe, bytes,1 , &exit_event, &undo_lists); + HandleMessage(ovpn_pipe, proc_info.hProcess, bytes, 1,
&exit_event,&undo_lists);src/openvpnserv/interactive.c RunOpenvpn(LPVOID p) · line 1689
Quoted 4 lines
src/openvpnserv/interactive.c +0
-0}WaitForSingleObject (proc_info.hProcess,IO_TIMEOUT );_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Signature · 1 line
-Lev
Hi,
Looks good now and works as expected.
Quoted 42 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling
CreateFile()directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.Signed-off-by Lev Stipakov <[email protected]...>---
v6:
- simplify and strengthen guid check withwcsspn()
- fix doxygen commentv5:
- further strengthen security by passing only device guid from client process to service, validating guid and constructing device path on service sidev4:
- strengthen security by adding basic validation to device path
- reorder fields inmsg_open_tun_device_resultfor backward compatibilityv3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalidv2:
- introducesend_msg_iservice_ex()instead of changing signature of existingsend_msg_iservice()
- usewchar_tstrings in interactive service codeinclude/openvpn-msg.h | 12 +++++++ src/openvpn/tun.c | 83 ++++++++++++++++++++++++++++++++++--------- src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++--- src/openvpnserv/interactive.c | 78 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 188 insertions(+), 24 deletions(-)
...
Quoted 31 lines
src/openvpnserv/interactive.c +15
-1@@ -58,7 +58,6 @@ static settings_t settings;static HANDLE rdns_semaphore =NULL ;#define RDNS_TIMEOUT 600/* seconds to wait for the semaphore */ -openvpn_service_t interactive_service = { interactive,TEXT (PACKAGE_NAME "ServiceInteractive" ),@@ -1198,8 +1197,62 @@ HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)return err; } +static DWORD +HandleOpenTunDeviceMessage (const open_tun_device_message_t *open_tun,HANDLE ovpn_proc,HANDLE *remote_handle) +{ +DWORD err =ERROR_SUCCESS ; +HANDLE local_handle; +LPWSTR wguid =NULL ; +WCHAR device_path[256 ] = {0 }; +const WCHAR prefix[] =L"\\\\.\\Global\\" ; +const WCHAR tap_suffix[] =L".tap" ; + + *remote_handle =INVALID_HANDLE_VALUE ; + + wguid =utf8to16 (open_tun->device_guid); +if (!wguid) + {
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp) · line 1224
Quoted 10 lines
src/openvpnserv/interactive.c +10
-0+ err =ERROR_OUTOFMEMORY ; + goto out; + } + +/* validate device guid */ +const size_t guid_len =wcslen (wguid); +if (guid_len !=38 ||wcsspn (wguid,L"0123456789ABCDEFabcdef-{}" ) != guid_len) + { + err =ERROR_MESSAGE_DATA ; +MsgToEventLog (MSG_FLAGS_ERROR ,TEXT ("Invalid device guild (%s)" ), wguid);
guild -> guid (as pointed out by tincanteksup)
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp) · line 1227
Quoted 3 lines
src/openvpnserv/interactive.c +3
-0+ goto out; + } +
whitespace in line above.
These could be fixed at commit time.
Selva
Selva
Let's not merge it yet.
The reason why this patch was done is to prepare for Wintun support, since Wintun device must be opened by privileged process. There is no need to use privileged process to open tap-windows6 device.
Recently Wintun API has been changed and previous Wintun device path (\\.\Global\WINTUNxxx) does not work anymore. In order to get a new path one needs to use SetupAPI (such as SetupDiCreateDeviceInfo), which must be called by privileged process.
So instead of having IPC call "open_tun_device(path)" it is probably better to have something like "open_wintun_device()", which both figures out device path
and opens device.
ke 24. heinäk. 2019 klo 17.44 Selva Nair ([email protected]...) kirjoitti:
Quoted 121 lines
Hi,
Looks good now and works as expected.
Quoted 12 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
This patch enables interactive service to open tun device. This is mostly needed by Wintun, which could be opened only by privileged process.
When interactive service is used, instead of calling
CreateFile()directly by openvpn process we pass tun device path into service process. There we open device, duplicate handle and pass it back to openvpn process.Quoted 8 lines
Signed-off-by Lev Stipakov <[email protected]...>---
v6:
- simplify and strengthen guid check withwcsspn()
- fix doxygen commentv5:
- further strengthen security by passing only device guid from clientprocess
Quoted 1 line
to service, validating guid and constructing device path on service
side
Quoted 4 lines
v4:
- strengthen security by adding basic validation to device path
- reorder fields inmsg_open_tun_device_resultfor backwardcompatibility
Quoted 6 lines
v3:
- ensure that device path passed by client is null-terminated
- support for multiple openvpn processes
- return proper error code when device handle is invalidQuoted 7 lines
v2:
- introducesend_msg_iservice_ex()instead of changing signature of existingsend_msg_iservice()
- usewchar_tstrings in interactive service codeinclude/openvpn-msg.h | 12 +++++++ src/openvpn/tun.c | 83++++++++++++++++++++++++++++++++++---------
Quoted 3 lines
src/openvpn/win32.c | 9 ++++- src/openvpn/win32.h | 30 +++++++++++++--- src/openvpnserv/interactive.c | 78++++++++++++++++++++++++++++++++++++++--
Quoted 1 line
5 files changed, 188 insertions(+), 24 deletions(-)...
Review
Quoted 1 line
(quoted diff) +0
-0
b/src/openvpnserv/interactive.csrc/openvpnserv/interactive.c HandleEnableDHCPMessage(const
Quoted 12 lines
index 623c3ff..1b4a5e2 100644
src/openvpnserv/interactive.c +0
-1@@ -58,7 +58,6 @@ static settings_t settings;static HANDLE rdns_semaphore =NULL ;#define RDNS_TIMEOUT 600/* seconds to wait for the semaphore */ -openvpn_service_t interactive_service = { interactive,TEXT (PACKAGE_NAME "ServiceInteractive" ),@@ -1198,8 +1197,62 @@ HandleEnableDHCPMessage(const
enable_dhcp_message_t*dhcp)src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 5 lines
src/openvpnserv/interactive.c +2
-0return err; } +static DWORD +HandleOpenTunDeviceMessage(const open_tun_device_message_t *open_tun,HANDLE
ovpn_proc, HANDLE*remote_handle)Quoted 10 lines
src/openvpnserv/interactive.c +10
-0+{ +DWORD err =ERROR_SUCCESS ; +HANDLE local_handle; +LPWSTR wguid =NULL ; +WCHAR device_path[256 ] = {0 }; +const WCHAR prefix[] =L"\\\\.\\Global\\" ; +const WCHAR tap_suffix[] =L".tap" ; + + *remote_handle =INVALID_HANDLE_VALUE ; +src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 10 lines
src/openvpnserv/interactive.c +10
-0+ wguid =utf8to16 (open_tun->device_guid); +if (!wguid) + { + err =ERROR_OUTOFMEMORY ; + goto out; + } + +/* validate device guid */ +const size_t guid_len =wcslen (wguid); + if (guid_len != 38 || wcsspn(wguid, L"0123456789ABCDEFabcdef-{}")!=
guid_len)src/openvpnserv/interactive.c HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)
Quoted 3 lines
src/openvpnserv/interactive.c +3
-0+ { + err =ERROR_MESSAGE_DATA ; + MsgToEventLog(MSG_FLAGS_ERROR, TEXT("Invalid device guild(%s)"), wguid);
guild -> guid (as pointed out by tincanteksup)
src/openvpnserv/interactive.c HandleEnableDHCPMessage(const · line 1227
Quoted 3 lines
src/openvpnserv/interactive.c +3
-0+ goto out; + } +whitespace in line above.
These could be fixed at commit time.
Acked-by Selva Nair <[email protected]...>Selva
Selva
Signature · 1 line
-Lev
Hi,
Quoted 4 lines · Lev Stipakov
So instead of having IPC call "
open_tun_device(path)" it is probably better to have something like "open_wintun_device()", which both figures out device path
and opens device.
That sounds like a plan. Have one API call for tuntap, and one for wintun.
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Hi
Quoted 14 lines · Lev Stipakov
Hi,
Sorry for delay - I was on vacation.
(i) The new message is named
message_open_tun, but it allows openingQuoted 1 line
any file using the service. This is not secure.
I am thinking of possible vector of attack here.
In our case it is service which launches openvpn process using path set in registry, opens pipe and passes pipe handle to launched process.
Quoted 8 lines
To make service run malicious process one needs either to replace executable or
modify registry. For both actions you need to be administrator, assuming default security policy.Alternatively, can random process hijack the pipe handle from another process?
Because the pipe handle is targeted for OpenVPN process id and not inheritable that may not be possible. But an attack is very simple:
Just write a plugin and use the service to open files from it. Passing the pipe handle to the plugin is trivial. Anyone in OpenVPNAdministrators group can then use a custom config file and get access any file on the system. Membership in that group is not supposed to provide any privileges to limited users beyond setting up routes or manipulating some interface parameters.
Quoted 6 lines
Quoted 3 lines
We need to restrict it to open tun/tap device nodes only.
Without adding too much code, I can think of:
Quoted 9 lines
- check that path starts with \\\\.\\Global\\ to make sure we open
device, not fileand
- check that path starts with \\\\.\\Global\\WINTUN or ends with .tap
Is this good enough or do you have better ideas?
That'll probably work with some extra sanity checks on the file name. Ideally we should just pass the dev-node (empty if unspecified) and type of device (TAP6 or WINTUN), but that will require a lot of duplication of code in the service, as you noted.
One option is to pass the device guid in case of tap or the index in case of wintun and construct the path in the service. That requires very little extra code. Otherwise a thorough sanitization of the path is required as there could be obscure ways of breaking out using "..\" or otherwise, though I'm not sure. Things like \\.\C:\..\D:\ works on Windows so I won't take any chances.
Selva
PS. Just noticed you've already posted a v4 -- I haven't looked at it yet.
Hi,
Quoted 5 lines
That'll probably work with some extra sanity checks on the file name. Ideally we should just pass the dev-node (empty if unspecified) and type of device (TAP6 or WINTUN), but that will require a lot of duplication of code in the service, as you noted.
Quoted 7 lines
One option is to pass the device guid in case of tap or the index in case of wintun and construct the path in the service. That requires very little extra code. Otherwise a thorough sanitization of the path is required as there could be obscure ways of breaking out using "..\" or otherwise, though I'm not sure. Things like \\.\C:\..\D:\ works on Windows so I won't take any chances.
You are right, just tested and one can escape global like this:
\\.\Global\..\C:\lol.tap
I'll do as you've proposed - pass a string which is either guid or number, a boolean flag (wintun/tap6) and add some validation.
Quoted 2 lines
PS. Just noticed you've already posted a v4 -- I haven't looked at it yet.
v5 is coming!
Signature · 1 line
-Lev