[Openvpn-devel,1/6] sitnl: harden strncpy() by forcing arguments to have the same length
| Message ID | 20190805092529.9467-2-a@unstable.cc |
|---|---|
| State | Accepted |
| Headers |
Return-Path: <openvpn-devel-bounces@lists.sourceforge.net> Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director10.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id QDOiHH/2R13tZgAAIUCqbw for <patchwork@openvpn.net>; Mon, 05 Aug 2019 05:27:27 -0400 Received: from proxy6.mail.ord1d.rsapps.net ([172.30.191.6]) by director10.mail.ord1d.rsapps.net with LMTP id mIxuHH/2R12mVgAApN4f7A ; Mon, 05 Aug 2019 05:27:27 -0400 Received: from smtp7.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy6.mail.ord1d.rsapps.net with LMTP id EFdKHH/2R13XZgAAQyIf0w ; Mon, 05 Aug 2019 05:27:27 -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: smtp7.gate.ord1c.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; dmarc=none (p=nil; dis=none) header.from=unstable.cc X-Suspicious-Flag: YES X-Classification-ID: 3d185202-b763-11e9-8ff9-bc305bf04148-1-1 Received: from [216.105.38.7] ([216.105.38.7:33958] helo=lists.sourceforge.net) by smtp7.gate.ord1c.rsapps.net (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 50/F7-05825-F76F74D5; Mon, 05 Aug 2019 05:27:27 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) id 1huZFp-0008RL-1I; Mon, 05 Aug 2019 09:26:09 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from <a@unstable.cc>) id 1huZFn-0008RC-Ee for openvpn-devel@lists.sourceforge.net; Mon, 05 Aug 2019 09:26:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: 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=WX+Ica3im9VMI6c0E7VWmoYvPRWScm57DyAItiEYJFo=; b=bYyLBHJ3aUa/jzJTt1TjpmUjNP 6EmOMx+NjBJxs/BaqH/spIEJKJ4S/1Okd905mIz8U7exCZ62nirp6LrHYvycoaRt+F4cWSaOu4ULx AiUv2rZimVe3PJpdIdfEtgPOdcyQIdbGVudLG8xW0BSiuYq7OQtAFPO1X8yKRrEeyAow=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: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=WX+Ica3im9VMI6c0E7VWmoYvPRWScm57DyAItiEYJFo=; b=eI0XdlmSCBDh4ipoJ1763nCnvL FFZ6tUvAv/XlmmkJ2m6p4ALpZxMmBYLxD7BdEKBZXcsq4mENlrxduXJGiyglelOJlATk7C6t7BGsC 5TKsnt46+x8/Nyr0HTWEOICeN39GoPD5J9KF9WyJ4AQ9VArKiRYWlDI2hEA1QOWawVJM=; Received: from s2.neomailbox.net ([5.148.176.60]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) id 1huZFl-00BSdI-40 for openvpn-devel@lists.sourceforge.net; Mon, 05 Aug 2019 09:26:07 +0000 From: Antonio Quartulli <a@unstable.cc> To: openvpn-devel@lists.sourceforge.net Date: Mon, 5 Aug 2019 11:25:24 +0200 Message-Id: <20190805092529.9467-2-a@unstable.cc> In-Reply-To: <20190805092529.9467-1-a@unstable.cc> References: <20190805092529.9467-1-a@unstable.cc> MIME-Version: 1.0 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 https://www.dnswl.org/, no trust [5.148.176.60 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.0 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1huZFl-00BSdI-40 Subject: [Openvpn-devel] [PATCH 1/6] sitnl: harden strncpy() by forcing arguments to have the same length X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: <openvpn-devel.lists.sourceforge.net> List-Unsubscribe: <https://lists.sourceforge.net/lists/options/openvpn-devel>, <mailto:openvpn-devel-request@lists.sourceforge.net?subject=unsubscribe> List-Archive: <http://sourceforge.net/mailarchive/forum.php?forum_name=openvpn-devel> List-Post: <mailto:openvpn-devel@lists.sourceforge.net> List-Help: <mailto:openvpn-devel-request@lists.sourceforge.net?subject=help> List-Subscribe: <https://lists.sourceforge.net/lists/listinfo/openvpn-devel>, <mailto:openvpn-devel-request@lists.sourceforge.net?subject=subscribe> Cc: Antonio Quartulli <antonio@openvpn.net> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox |
| Series |
sitnl follow-up
|
|
Commit Message
Antonio Quartulli
Aug. 4, 2019, 11:25 p.m. UTC
From: Antonio Quartulli <antonio@openvpn.net> At the moment a strcpy() (without length check!) is performed between a string long IFNAMSIZ bytes and one of 16 bytes. This is ok right now because IFNAMSIZ is defined as 16, however this bit is not under our control and may change in he future without us being warned. For this reason, force both strings to use IFNAMSIZ as size and, since this constant may not exist on every platform, ensure it is always defined. Signed-off-by: Antonio Quartulli <antonio@openvpn.net> --- src/openvpn/networking_sitnl.c | 2 +- src/openvpn/route.h | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-)
Comments
>For this reason, force both strings to use IFNAMSIZ as size and, since >this constant may not exist on every platform, ensure it is always >defined. A problem with this patch misght be that strncpy() does NOT NUL terminates the copied string. (It writes EXACTLY IFNAMSIZ bytes but only when the string is shorter, it will writes NUL bytes in the reminder of the char array[].) You could use strlcpy() but I'm not sure if that is available in Windows. > > /* save result in output variables */ > memcpy(best_gw, &res.gw, res.addr_size); - strcpy(best_iface, res.iface); >+ strncpy(best_iface, res.iface, IFNAMSIZ); Casper
Hi and thanks for your review, On 05/08/2019 11:33, Casper.Dik@oracle.com wrote: > > >> For this reason, force both strings to use IFNAMSIZ as size and, since >> this constant may not exist on every platform, ensure it is always >> defined. > > A problem with this patch misght be that strncpy() does NOT NUL terminates the > copied string. (It writes EXACTLY IFNAMSIZ bytes but only when the string > is shorter, it will writes NUL bytes in the reminder of the char array[].) the assumption behind this patch is that now both source and destination buffer have exactly the same size (IFNAMSIZ) and the source is NULL terminated. Therefore the problem you mentioned should not appear here, I think. > > You could use strlcpy() but I'm not sure if that is available in Windows. This code is currently used only on Linux - it is not expected to ever run on Windows. Cheers, > > >> >> /* save result in output variables */ >> memcpy(best_gw, &res.gw, res.addr_size); > - strcpy(best_iface, res.iface); >> + strncpy(best_iface, res.iface, IFNAMSIZ); > > Casper >
Acked-by: Gert Doering <gert@greenie.muc.de> Interesting remark from Casper Dik - strncpy() actually is defined to *always* write all IFNAMSIZ bytes (so says the linux man page), unlike strcpy() which stops after the '\0' byte. But in this particular case, "not overrunning" is more important than "save a few cycles on a short interface name". But it was certainly news to me. Now all strings involved are guaranteed to always have the same length. Thanks. Your patch has been applied to the master branch. commit 8a05f860af386cd135ee1aacb0d5eccc49c466bb Author: Antonio Quartulli Date: Mon Aug 5 11:25:24 2019 +0200 sitnl: harden strncpy() by forcing arguments to have the same length Signed-off-by: Antonio Quartulli <antonio@openvpn.net> Acked-by: Gert Doering <gert@greenie.muc.de> Message-Id: <20190805092529.9467-2-a@unstable.cc> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg18722.html Signed-off-by: Gert Doering <gert@greenie.muc.de> -- kind regards, Gert Doering
diff --git a/src/openvpn/networking_sitnl.c b/src/openvpn/networking_sitnl.c index 4e29d8ba..ea730ec7 100644 --- a/src/openvpn/networking_sitnl.c +++ b/src/openvpn/networking_sitnl.c @@ -494,7 +494,7 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst, /* save result in output variables */ memcpy(best_gw, &res.gw, res.addr_size); - strcpy(best_iface, res.iface); + strncpy(best_iface, res.iface, IFNAMSIZ); err: return ret; diff --git a/src/openvpn/route.h b/src/openvpn/route.h index 31d38e36..2e68091c 100644 --- a/src/openvpn/route.h +++ b/src/openvpn/route.h @@ -184,7 +184,11 @@ struct route_ipv6_gateway_info { #ifdef _WIN32 DWORD adapter_index; /* interface or ~0 if undefined */ #else - char iface[16]; /* interface name (null terminated), may be empty */ + /* non linux platform don't have this constant defined */ +#ifndef IFNAMSIZ +#define IFNAMSIZ 16 +#endif + char iface[IFNAMSIZ]; /* interface name (null terminated), may be empty */ #endif /* gateway interface hardware address */