From patchwork Wed May 30 05:37:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 922536 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40wfVn6yGzz9s08 for ; Wed, 30 May 2018 15:37:21 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="MvDOY3lr"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="XjLCASfJ"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 40wfVn4zpxzDrJP for ; Wed, 30 May 2018 15:37:21 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="MvDOY3lr"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="XjLCASfJ"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=mendozajonas.com (client-ip=66.111.4.25; helo=out1-smtp.messagingengine.com; envelope-from=sam@mendozajonas.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="MvDOY3lr"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="XjLCASfJ"; dkim-atps=neutral Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40wfVg1W8SzDr5J for ; Wed, 30 May 2018 15:37:15 +1000 (AEST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 0A55421B8A; Wed, 30 May 2018 01:37:13 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 30 May 2018 01:37:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= mendozajonas.com; h=cc:content-transfer-encoding:content-type :date:from:message-id:mime-version:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=2xfa3xGX2xLHjDUc1k8qnGK0tQLOh z3RFRcgLXu6ah0=; b=MvDOY3lrnk/DEpWE5JH3LIEMKfnTdjKW/96vIjnzKK/K+ SMmlX4unJTzOLT+OaMFq9hpHG47ymavnqgp/sh2rhXn5fpS0+IUSF1s5WRTUbdou OnHDEz0fXNXF47SWkd5nRmlafkS87umKhMDqhytC2fSOaWlLPQeRLaSLHSSpdi/6 e4jTjouyAjdUaKh2J18p8hOP7cBPQ9TJ7NBIaAd1UTzLgl0Zb3SW7c0w5+aw5EBA h/dDHZ6YAIFV/Rbtfrd1KHXXxnL7w/dBShLRxz36yAWUAihsOkzM/1Y8SWie4lPu n95iE9N0gJm0zKRMoAXPXWxj10EAJQAewcy8K7c4Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:message-id:mime-version:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=2xfa3xGX2xLHjDUc1k8qnGK0tQLOh z3RFRcgLXu6ah0=; b=XjLCASfJPWGtolPLN2QVCO9PSp9GKu9ic/D8hF+TLT1Fq RLdOUQmeUkzda5D0TbDRLzBGMArvmGR5j9Juqa/VKYBlnfi1pXTdHY2q1cmXm4N0 2tJc3CV7OPR3rFAdxgSFjB0OQHnYwgrxjCr2zF3Ydf3sqjeaizab/PCfAx7Nw+wT Kji9hqA6crSx07CzsOrwISVO+4nmvB23V0nB9oPlY11/nuhFykhmHWrWXR4tL3IV 2CRIE8xdEISPTw9hfb6gCgFKZOD8bikcs2XIyD71w/b3DBHWL6Zfx8iHxmaviN5i 9rbe8QMKYprgej0XmPg1hTEGJrM9Nq6FS/rolPHyg== X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id B5612E452C; Wed, 30 May 2018 01:37:11 -0400 (EDT) From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [PATCH] discover/network: Null terminate ifname buffer explicitly Date: Wed, 30 May 2018 15:37:05 +1000 Message-Id: <20180530053705.9574-1-sam@mendozajonas.com> X-Mailer: git-send-email 2.17.0 MIME-Version: 1.0 X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Mendoza-Jonas Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" GCC 8 produces the following warning for network.c: In function ‘network_handle_nlmsg’, inlined from ‘network_netlink_process’ at ../discover/network.c:726:3: ../discover/network.c:568:3: warning: ‘strncpy’ output may be truncated copying 15 bytes from a string of length 16 [-Wstringop-truncation] strncpy(interface->name, ifname, sizeof(interface->name) - 1); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../discover/network.c:586:3: warning: ‘strncpy’ output may be truncated copying 15 bytes from a string of length 16 [-Wstringop-truncation] strncpy(interface->name, ifname, sizeof(interface->name) - 1); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The code is safe since interface is allocated with talloc_zero() and we could use -Wno-stringop-truncation to hide this but since this is the only offender instead just copy the whole IFNAMSIZ bytes and explicitly terminate the ifname buffer to be safe. Signed-off-by: Samuel Mendoza-Jonas --- discover/network.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/discover/network.c b/discover/network.c index e2cae91..9594b2e 100644 --- a/discover/network.c +++ b/discover/network.c @@ -506,7 +506,7 @@ static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg) struct rtattr *attr; unsigned int mtu; uint8_t ifaddr[6]; - char ifname[IFNAMSIZ+1]; + char ifname[IFNAMSIZ]; int attrlen, type; @@ -534,6 +534,7 @@ static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg) case IFLA_IFNAME: strncpy(ifname, data, IFNAMSIZ); + ifname[IFNAMSIZ - 1] = '\0'; have_ifname = true; break; @@ -565,7 +566,7 @@ static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg) interface->ifindex = info->ifi_index; interface->state = IFSTATE_NEW; memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr)); - strncpy(interface->name, ifname, sizeof(interface->name) - 1); + strncpy(interface->name, ifname, sizeof(interface->name)); list_for_each_entry(&network->interfaces, tmp, list) if (memcmp(interface->hwaddr, tmp->hwaddr, @@ -583,7 +584,7 @@ static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg) /* A repeated RTM_NEWLINK can represent an interface name change */ if (strncmp(interface->name, ifname, IFNAMSIZ)) { pb_debug("ifname update: %s -> %s\n", interface->name, ifname); - strncpy(interface->name, ifname, sizeof(interface->name) - 1); + strncpy(interface->name, ifname, sizeof(interface->name)); talloc_free(interface->dev->device->id); interface->dev->device->id = talloc_strdup(interface->dev->device, ifname);