From patchwork Thu Oct 4 01:34:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Stipakov X-Patchwork-Id: 479 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director7.mail.ord1d.rsapps.net ([172.27.255.57]) by backend30.mail.ord1d.rsapps.net with LMTP id QOcsAef7tVu9NAAAIUCqbw for ; Thu, 04 Oct 2018 07:39:19 -0400 Received: from proxy13.mail.iad3a.rsapps.net ([172.27.255.57]) by director7.mail.ord1d.rsapps.net with LMTP id MMifOub7tVuXdAAAovjBpQ ; Thu, 04 Oct 2018 07:39:19 -0400 Received: from smtp52.gate.iad3a ([172.27.255.57]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy13.mail.iad3a.rsapps.net with LMTP id CAECNub7tVu7RwAAwhxzoA ; Thu, 04 Oct 2018 07:39:18 -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: smtp52.gate.iad3a.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: 207d8c18-c7ca-11e8-ac7b-525400a7d0a1-1-1 Received: from [216.105.38.7] ([216.105.38.7:65104] helo=lists.sourceforge.net) by smtp52.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id AD/78-03941-6EBF5BB5; Thu, 04 Oct 2018 07:39:18 -0400 Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1g81xV-0005dJ-A7; Thu, 04 Oct 2018 11:38:21 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1g81xU-0005d9-Ey for openvpn-devel@lists.sourceforge.net; Thu, 04 Oct 2018 11:38:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=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:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=keVOk/8C+zEE0G48ZKKYdZCv2iUcs0k/DoFEKps+iws=; b=g8khF8JfFiEU6LpHTKsUgQLEdx XJe78MRrUo1WbOs81s7JX0f8BcI9UzL2zjSVbMQe0G8/PrHZRG6uwfqTYYzgDG81JxBZx+NhNW7L+ SRD6mz1CgbdbbL59bXKMAN7wx8fLWNLdsB2shiFtPH9JHTvXTuPxz8F8eparPu5Ch6+0=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=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: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=keVOk/8C+zEE0G48ZKKYdZCv2iUcs0k/DoFEKps+iws=; b=K939geU7f02qOei9JkeXxptoTe eVa5apn/586GC5vfCIYD7zY/lREvg/SqgFYqhmh2Wu47DeIDODbxbrwoQcpxbOGBVnI5jwk1W8rUE +dcCK6csKs2D/UmlYPdcnxXGXw/OdWZPZKxpQQlR/wpdDQG4w4bxyrK2c1Hsnm9jhCnY=; Received: from mail-ed1-f46.google.com ([209.85.208.46]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) id 1g81xO-000GOk-8j for openvpn-devel@lists.sourceforge.net; Thu, 04 Oct 2018 11:38:20 +0000 Received: by mail-ed1-f46.google.com with SMTP id j62-v6so8234770edd.7 for ; Thu, 04 Oct 2018 04:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=keVOk/8C+zEE0G48ZKKYdZCv2iUcs0k/DoFEKps+iws=; b=QbWLTh77tCQ5kKbIAxO13ka7VsPCDEF8SZJ/7N1gWmQUPKpFzVg11FmRA426y59ixm D+LSpyUgMzPFhneyNj/q2ggulbqK4sSYTG+QTtfzgx9W82ojkfb3I/DFu/dBcto2zM+V 8p5O4+dqDIuy8lR9KtI3IJz6IswCj9Xtb4Kow6EKSQRXFhu4XhOvhFlurDe8oQLnB3cp IRN0Na4w6iH7/S/ItdmxbAVXE4sgoRVU++K4Sd/UAd15mHgD7jy0sCfaN73Y5gY/cZHz CMtk6pRq/dGNwhYyEiG2ACioiQPw2PDMNFKhScHM33/zkYghQE4UtVOe118z3RxjvX9/ Bx7A== 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; bh=keVOk/8C+zEE0G48ZKKYdZCv2iUcs0k/DoFEKps+iws=; b=Eo4Ghe2285XfPT/ayQ0Pkz1I0S9iqKMLYs3yZb7GrhHRmPwcHmyCdE0t6O368GZ+oU VJN/w2ykzcXjQ6tSPNf4gl14ZDz2WG6TCLX2zt8tctp3lAauVH/1vX7ffhYRigjAy4S8 4kkOTxz3CSBthxtVW6xIrAKtB6xiccAG7/uHxZ6MA0TnJkSGY031bCjX2MMO0RB5E7MQ inW0vOTlfd0D2bCZqKf/RN8nIbn9X8WRetKupd8hRxWe1FyPczrEBEXhOTl6RSYcWtzT Nx0tyyOajW5YrB+N680BUt0B5S5S4ECw0lz+k7IdeJKvTBnLJ2mDmSHPOkEh/yu2n9vs UfAg== X-Gm-Message-State: ABuFfohjdYRHRhJWfGxvbiqjzYq7cv+NVa48zxwYrC3U9ehh5EDERwGy q67SxL3osF4F7uDCOkWoQe84109oMcc= X-Google-Smtp-Source: ACcGV63RH+ipdICZiyd+yhGmknmo0oTAbXicXgOWKFPRUN4KQItp3eJYWAQRV/bBPXe4Z+cVQeWXOg== X-Received: by 2002:a17:906:4e98:: with SMTP id v24-v6mr6213185eju.223.1538653086917; Thu, 04 Oct 2018 04:38:06 -0700 (PDT) Received: from stipakov.fi (stipakov.fi. [128.199.52.117]) by smtp.gmail.com with ESMTPSA id f47-v6sm1655237edf.87.2018.10.04.04.38.05 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 04 Oct 2018 04:38:06 -0700 (PDT) From: Lev Stipakov To: openvpn-devel@lists.sourceforge.net Date: Thu, 4 Oct 2018 14:34:58 +0300 Message-Id: <1538652898-7028-1-git-send-email-lstipakov@gmail.com> X-Mailer: git-send-email 2.7.4 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [209.85.208.46 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (lstipakov[at]gmail.com) -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: 1g81xO-000GOk-8j Subject: [Openvpn-devel] [PATCH] Introduce openvpn_swprintf() with nul termination guarantee 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 Every call to swprintf is followed by line which adds nul terminator. This patch introduces openvpn_swprintf() which guarantees nul termination for size > 0. Same approach as for snprintf / openvpn_snprintf. Signed-off-by: Lev Stipakov --- src/openvpn/buffer.c | 17 +++++++++++++++++ src/openvpn/buffer.h | 11 +++++++++++ src/openvpn/tun.c | 3 +-- src/openvpnserv/common.c | 15 +++++++++++++++ src/openvpnserv/interactive.c | 18 ++++++------------ src/openvpnserv/validate.c | 3 +-- 6 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index 7630ff7..52747cd 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -37,6 +37,8 @@ #include "memdbg.h" +#include + size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra) { @@ -308,6 +310,21 @@ openvpn_snprintf(char *str, size_t size, const char *format, ...) return (len >= 0 && len < size); } +bool +openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...) +{ + va_list arglist; + int len = -1; + if (size > 0) + { + va_start(arglist, format); + len = vswprintf(str, size, format, arglist); + va_end(arglist); + str[size - 1] = L'\0'; + } + return (len >= 0 && len < size); +} + /* * write a string to the end of a buffer that was * truncated by buf_printf diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index cb9e9df..b1ee46d 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -448,6 +448,17 @@ __attribute__ ((format(__printf__, 3, 4))) #endif ; + +/* + * Like swprintf but guarantees null termination for size > 0 + */ +bool +openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...); +/* + * Unlike in openvpn_snprintf, we cannot use format attributes since + * GCC doesn't support wprintf as archetype. + */ + /* * remove/add trailing characters */ diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 50f158c..c5ef37a 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -4480,8 +4480,7 @@ get_adapter_index_method_1(const char *guid) DWORD index; ULONG aindex; wchar_t wbuf[256]; - swprintf(wbuf, SIZE(wbuf), L"\\DEVICE\\TCPIP_%S", guid); - wbuf [SIZE(wbuf) - 1] = 0; + openvpn_swprintf(wbuf, SIZE(wbuf), L"\\DEVICE\\TCPIP_%S", guid); if (GetAdapterIndex(wbuf, &aindex) != NO_ERROR) { index = TUN_ADAPTER_INDEX_INVALID; diff --git a/src/openvpnserv/common.c b/src/openvpnserv/common.c index dc47666..b47046f 100644 --- a/src/openvpnserv/common.c +++ b/src/openvpnserv/common.c @@ -56,6 +56,21 @@ openvpn_sntprintf(LPTSTR str, size_t size, LPCTSTR format, ...) return len; } +BOOL +openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...) +{ + va_list arglist; + int len = -1; + if (size > 0) + { + va_start(arglist, format); + len = vswprintf(str, size, format, arglist); + va_end(arglist); + str[size - 1] = L'\0'; + } + return (len >= 0 && len < size); +} + static DWORD GetRegString(HKEY key, LPCTSTR value, LPTSTR data, DWORD size, LPCTSTR default_value) { diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c index 9d459a6..bb2c411 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -276,8 +276,7 @@ ReturnProcessId(HANDLE pipe, DWORD pid, DWORD count, LPHANDLE events) * Same format as error messages (3 line string) with error = 0 in * 0x%08x format, PID on line 2 and a description "Process ID" on line 3 */ - swprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg); - buf[_countof(buf) - 1] = '\0'; + openvpn_swprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg); WritePipeAsync(pipe, buf, (DWORD)(wcslen(buf) * 2), count, events); } @@ -402,9 +401,8 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options) if (!CheckOption(workdir, 2, argv_tmp, &settings)) { - swprintf(buf, _countof(buf), msg1, argv[0], workdir, + openvpn_swprintf(buf, _countof(buf), msg1, argv[0], workdir, settings.ovpn_admin_group); - buf[_countof(buf) - 1] = L'\0'; ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event); } goto out; @@ -421,16 +419,14 @@ ValidateOptions(HANDLE pipe, const WCHAR *workdir, const WCHAR *options) { if (wcscmp(L"--config", argv[i]) == 0 && argc-i > 1) { - swprintf(buf, _countof(buf), msg1, argv[i+1], workdir, + openvpn_swprintf(buf, _countof(buf), msg1, argv[i+1], workdir, settings.ovpn_admin_group); - buf[_countof(buf) - 1] = L'\0'; ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event); } else { - swprintf(buf, _countof(buf), msg2, argv[i], + openvpn_swprintf(buf, _countof(buf), msg2, argv[i], settings.ovpn_admin_group); - buf[_countof(buf) - 1] = L'\0'; ReturnError(pipe, ERROR_STARTUP_DATA, buf, 1, &exit_event); } goto out; @@ -954,8 +950,7 @@ RegisterDNS(LPVOID unused) if (GetSystemDirectory(sys_path, MAX_PATH)) { - swprintf(ipcfg, MAX_PATH, L"%s\\%s", sys_path, L"ipconfig.exe"); - ipcfg[MAX_PATH-1] = L'\0'; + openvpn_swprintf(ipcfg, MAX_PATH, L"%s\\%s", sys_path, L"ipconfig.exe"); } if (WaitForMultipleObjects(2, wait_handles, FALSE, timeout) == WAIT_OBJECT_0) @@ -1594,9 +1589,8 @@ RunOpenvpn(LPVOID p) else if (exit_code != 0) { WCHAR buf[256]; - swprintf(buf, _countof(buf), + openvpn_swprintf(buf, _countof(buf), L"OpenVPN exited with error: exit code = %lu", exit_code); - buf[_countof(buf) - 1] = L'\0'; ReturnError(pipe, ERROR_OPENVPN_STARTUP, buf, 1, &exit_event); } Undo(&undo_lists); diff --git a/src/openvpnserv/validate.c b/src/openvpnserv/validate.c index 91c6a2b..8d1855d 100644 --- a/src/openvpnserv/validate.c +++ b/src/openvpnserv/validate.c @@ -68,8 +68,7 @@ CheckConfigPath(const WCHAR *workdir, const WCHAR *fname, const settings_t *s) /* convert fname to full path */ if (PathIsRelativeW(fname) ) { - swprintf(tmp, _countof(tmp), L"%s\\%s", workdir, fname); - tmp[_countof(tmp)-1] = L'\0'; + openvpn_swprintf(tmp, _countof(tmp), L"%s\\%s", workdir, fname); config_file = tmp; } else