From patchwork Sat Oct 20 23:19:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Stipakov X-Patchwork-Id: 564 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director10.mail.ord1d.rsapps.net ([172.27.255.51]) by backend30.mail.ord1d.rsapps.net with LMTP id kG+BBgBUzFuCTwAAIUCqbw for ; Sun, 21 Oct 2018 06:25:04 -0400 Received: from proxy11.mail.iad3a.rsapps.net ([172.27.255.51]) by director10.mail.ord1d.rsapps.net with LMTP id kE1vAwBUzFulBgAApN4f7A ; Sun, 21 Oct 2018 06:25:04 -0400 Received: from smtp16.gate.iad3a ([172.27.255.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy11.mail.iad3a.rsapps.net with LMTP id YGl1Of9TzFueFwAAxCvdqw ; Sun, 21 Oct 2018 06:25:03 -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: smtp16.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: 923788d2-d51b-11e8-a501-5254004ee196-1-1 Received: from [216.105.38.7] ([216.105.38.7:56530] helo=lists.sourceforge.net) by smtp16.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 82/82-31360-FF35CCB5; Sun, 21 Oct 2018 06:25:03 -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 1gEAu1-0008Qc-30; Sun, 21 Oct 2018 10:24:09 +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 1gEAu0-0008QL-8q for openvpn-devel@lists.sourceforge.net; Sun, 21 Oct 2018 10:24:08 +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=HUjZLbzqobCkchIKgC4LJQwPEiO3AqWMX8IJ5DpA9Cg=; b=hOWTsxXiYB4BBdAago+Ve8OMmO 5/58auM86M7GtwlfUpBODHkj6Yw0aMnLd+7YdWj1jT4MM7gTMFtB4m4dfAt08bfZ5wrtf1ZBy5FyH 04RNFa7MJnsmSqy017P3jHhmEKs8zS6ZALZLta2Bmfpy0M2diw0ck8wP1EWnb4plCQt0=; 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=HUjZLbzqobCkchIKgC4LJQwPEiO3AqWMX8IJ5DpA9Cg=; b=UJBbDBCrlHtv9HXG0lcMFz2izO qnHR+ZlnpxsgASl7D0bZclkSGDzpucdKbmDH4MG2p3JyrVKw7oeOMBkZQ9yceRZWUnCJk+b6X93BN Wd5Bp6utxgEbtBp1EMVXve6kW9NWpHqSyndwkEmPgxtO/OE/bbw+Hu6mmWU0PzKE+6Ms=; Received: from mail-ed1-f65.google.com ([209.85.208.65]) by sfi-mx-4.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) id 1gEAty-006a4k-9B for openvpn-devel@lists.sourceforge.net; Sun, 21 Oct 2018 10:24:08 +0000 Received: by mail-ed1-f65.google.com with SMTP id x2-v6so292408eds.3 for ; Sun, 21 Oct 2018 03:24:06 -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=HUjZLbzqobCkchIKgC4LJQwPEiO3AqWMX8IJ5DpA9Cg=; b=pugOYphyxBBpCXMcommhI0nIWbxBUnAstNwI+97i1rYUTuUiNc2hhiBBRs4C3x7m4F LhvnEasRlgekrWsXEvQawfQN95JmPiwpBviFi0xAZ2IPEpwmoJysKrcCv0tLXforMmgR 4jlLWU7RObnwGCjjAkiAlHK8DC6Ga/34t+PZyXZYCxLHICoE9PwTx91LYHbVnVXf9vSW 0V5o9BEOK0QJ5eL8hEOfKCZLvElvP7DNFJLTxpffcgqZ0ZWs6Kpa3HuZW79odKBMZZjB wsw1G52u67jmnrq0oVmMECrw1z+6yN+qFurzQm7/mzp/qs9F2DudGeu/DcHpKnIJekZC x0Yw== 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=HUjZLbzqobCkchIKgC4LJQwPEiO3AqWMX8IJ5DpA9Cg=; b=W6VVSVqu46uff9qi5EtfwBsAQm8H3NAXpsXtvt1PksY8e+PpHhx6LG1uPjamTNdtMR fhkqlGiBXOm2Pv5q+PcLfjXpHrvaHA5qT4AwCbmRrE6KekXOSnEBG8t9SqFxsW2QwjPg TsOO4WoGFsLIvvA8LgiMi0mgHQ66Pw2TPX6K3qSx1o281RBCvtutormFKKzx9jO/mDfD eFf/BplIlvD+wtZUpoykyJ5u0y06FV5vLgoTzN3lRoymPvjv10GUbeewjO8P/yUFUanK MvYkGSG0J1clnwa90GTWRKuYXL0iayTFuDI9AFxlMm7+/doBPdTFEJ4MH4YButb4eom5 Byag== X-Gm-Message-State: ABuFfohNUvRxek4b1k9QZ90geVGtOZvysSEELajQN/yI6peHhBOq7JrW XDhMOdN7tHgX9UIqrlXCz3M/FIeWAhI= X-Google-Smtp-Source: ACcGV63ogr/UZqETA92rhxXXkXp4Kpe5Burl6zxHl/ncDrD1qjfOVgT96VanTf7dWp4ggvjNWFABXA== X-Received: by 2002:a50:b003:: with SMTP id i3-v6mr10822986edd.120.1540117439220; Sun, 21 Oct 2018 03:23:59 -0700 (PDT) Received: from stipakov.fi (stipakov.fi. [128.199.52.117]) by smtp.gmail.com with ESMTPSA id b36-v6sm13287051edb.5.2018.10.21.03.23.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 21 Oct 2018 03:23:58 -0700 (PDT) From: Lev Stipakov To: openvpn-devel@lists.sourceforge.net Date: Sun, 21 Oct 2018 13:19:46 +0300 Message-Id: <1540117186-10445-1-git-send-email-lstipakov@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: 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 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [209.85.208.65 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.208.65 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches 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 X-Headers-End: 1gEAty-006a4k-9B Subject: [Openvpn-devel] [PATCH v2] 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 --- v2: - rebase on top of master 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 80fbb75..8ca189f 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 aab249f..c5b78a0 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 948fd17..c091401 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -4472,8 +4472,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 33a8097..fd51776 100644 --- a/src/openvpnserv/common.c +++ b/src/openvpnserv/common.c @@ -57,6 +57,21 @@ openvpn_sntprintf(LPTSTR str, size_t size, LPCTSTR format, ...) return res; } +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 4a571f5..b065c39 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; @@ -950,8 +946,7 @@ RegisterDNS(LPVOID unused) HANDLE wait_handles[2] = {rdns_semaphore, exit_event}; - swprintf(ipcfg, _countof(ipcfg), L"%s\\%s", get_win_sys_path(), L"ipconfig.exe"); - ipcfg[_countof(ipcfg) - 1] = L'\0'; + openvpn_swprintf(ipcfg, MAX_PATH, L"%s\\%s", get_win_sys_path(), L"ipconfig.exe"); if (WaitForMultipleObjects(2, wait_handles, FALSE, timeout) == WAIT_OBJECT_0) { @@ -1628,9 +1623,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 c576ac1..5506e90 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