From patchwork Thu Jul 18 01:41:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Stipakov X-Patchwork-Id: 783 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id iBnHMmFbMF1PGQAAIUCqbw for ; Thu, 18 Jul 2019 07:43:29 -0400 Received: from proxy3.mail.iad3b.rsapps.net ([172.31.255.6]) by director9.mail.ord1d.rsapps.net with LMTP id SJl6MGFbMF1ZRAAAalYnBA ; Thu, 18 Jul 2019 07:43:29 -0400 Received: from smtp36.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy3.mail.iad3b.rsapps.net with LMTP id UH1XK2FbMF0xFAAAM8Wetg ; Thu, 18 Jul 2019 07:43:29 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp36.gate.iad3b.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dkim=fail (signature verification failed) header.d=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Suspicious-Flag: YES X-Classification-ID: 42afcaee-a951-11e9-bade-5254003a7283-1-1 Received: from [216.105.38.7] ([216.105.38.7:43162] helo=lists.sourceforge.net) by smtp36.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 40/44-15498-16B503D5; Thu, 18 Jul 2019 07:43:29 -0400 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1ho4nf-00012i-Jh; Thu, 18 Jul 2019 11:42:15 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ho4ne-00012O-Kt for openvpn-devel@lists.sourceforge.net; Thu, 18 Jul 2019 11:42:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:To: From:Sender:Reply-To:Cc:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=JUL5BgWAAozRmcZ31FEpI500JsesayAgv/ctYRIipEg=; b=ThkWQHMY6/oKur09QN8xmUMtgk 4bgF0Bs7tNhh4ye81mv99or1e2o1q+W14MQ5UbTnSfFReHd+OWqMiA9bl+ndzRvnxLNR7iL25Ertb 7XSuy8Wzl8Y+gmIBQcz5Q79W0dD0bnQ6UHUvjpAckOZ4VDwSuS0qWn1EdVolxUiofq9c=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc :MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=JUL5BgWAAozRmcZ31FEpI500JsesayAgv/ctYRIipEg=; b=DQups+0aeM6q+dRjtYcB0M0MTQ nb/wxHsddcofDQWz1q+Y9s+d8r2Wz8VxY/984xy3qdCtwxqkop9454lJBH1if7DlUSCVMSVAHrZ50 dEQ5dJ82uuO2SXhtW2Az2noohE3RXJmQzcbdRdYF+jdCZuFGEdOQKYEqkArN6ckyx7Fg=; Received: from mail-ed1-f66.google.com ([209.85.208.66]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) id 1ho4nZ-006NnE-Sn for openvpn-devel@lists.sourceforge.net; Thu, 18 Jul 2019 11:42:14 +0000 Received: by mail-ed1-f66.google.com with SMTP id k21so29945893edq.3 for ; Thu, 18 Jul 2019 04:42:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=JUL5BgWAAozRmcZ31FEpI500JsesayAgv/ctYRIipEg=; b=YL92vKiHSEz0UKVLs9fvhd8JtqQZIYR9tM0GH3JyoDXZi7GuA9QzVO20BH1QZfYHs7 gHTe41PJwtxDPoPyQVVp3K2rZCG7KJ0YqpuxMGTXx4nsrU0pcQj8FfGM7zc4MIvZV+t6 E3FakE4sKG/9YgXAcJ42ONgONCjbT2NQXPA5mrpYLXUxS+2NGi/QpKj5G8ENFk4OxxkW VkcJXaNv0Wl4scM9uTxz5rpM6nuLJeFuPRb4zTNCZG6cfdenPhk0Hpnzxei/Hq4ZH/84 Q4OKHW1hdVF8S3vl8r+Xb2+z0ixeUrhfJmW+VA8imzRtygecwTDx9fl1K6QGKqdTBOic Ni3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=JUL5BgWAAozRmcZ31FEpI500JsesayAgv/ctYRIipEg=; b=BeFDAe6n/RMiAMNHe4jwv7y/apuYobrnOHTuVsMXlxnDw0XxYs7jvsB1BMNibU49eR 1j74O//c8wsj4jJUoKQebU6x7VoWwWO7PmWeIAGn5gJHTveWDRx30nnFIOcxYo0GL/+r cuQpiZ/K8c/VoIRvW0JWVajohND3yMplO3GbCrpvY952MxN3ssMAbAPeW9KuUsbZB/3r 1jZ9G3X43uqhegiR6nR+uf1TlN1CX2meeORSncJv3s6nwwCTcWjfj2p/ZW9YNS/zv5Ww mvFBar9P2N2HDyWf0jhAelIkIoRf3zVozeQnYh621B8xwZI6pvSkR5d2YRN2lAsVgTr0 vhfg== X-Gm-Message-State: APjAAAXvtCXEKes1VLs/Fm1wkWtHOYdBJS3f2VUARgTZEnPxFXR6hmDG Vy3AjRg1B/cuUF+6QSDqwjxLBP4xVxx1Iw== X-Google-Smtp-Source: APXvYqyppzOSffiVcNiqTiUKHWbLioORvtdX4aLfm31eRFdRT5sTWRxuD1mHGEq0VSdfI3vcHBkoZw== X-Received: by 2002:a17:906:6550:: with SMTP id u16mr36100916ejn.7.1563450122734; Thu, 18 Jul 2019 04:42:02 -0700 (PDT) Received: from stipakov.fi (stipakov.fi. [128.199.52.117]) by smtp.gmail.com with ESMTPSA id w13sm5594069eji.22.2019.07.18.04.42.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Jul 2019 04:42:02 -0700 (PDT) From: Lev Stipakov To: openvpn-devel@lists.sourceforge.net Date: Thu, 18 Jul 2019 14:41:53 +0300 Message-Id: <1563450113-11225-1-git-send-email-lstipakov@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1563369608-10240-1-git-send-email-lstipakov@gmail.com> References: <1563369608-10240-1-git-send-email-lstipakov@gmail.com> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (lstipakov[at]gmail.com) 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: openvpn.net] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.208.66 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.0 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1ho4nZ-006NnE-Sn Subject: [Openvpn-devel] [PATCH v5] openvpnserv: enable interactive service to open tun X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Lev Stipakov 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 Signed-off-by: Lev Stipakov <lev@openvpn.net>
--- 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(-) diff --git a/include/openvpn-msg.h b/include/openvpn-msg.h index 66177a2..5dbdca5 100644 --- a/include/openvpn-msg.h +++ b/include/openvpn-msg.h @@ -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_ */ diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 8f8f7c6..df26ccf 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -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) { diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index eb4c030..039c1a4 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -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", diff --git a/src/openvpn/win32.h b/src/openvpn/win32.h index 4814bbc..8ccddc0 100644 --- a/src/openvpn/win32.h +++ b/src/openvpn/win32.h @@ -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 */ diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c index 623c3ff..782c04d 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -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]; + + 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; + } + } + + 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 +1268,7 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events, undo_lists block_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 +1336,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 +1688,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);