From patchwork Mon Feb 4 16:34:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 10796085 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 59D1C922 for ; Mon, 4 Feb 2019 16:35:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4530B2BA0D for ; Mon, 4 Feb 2019 16:35:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 36F062BA4E; Mon, 4 Feb 2019 16:35:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D4E8A2BA0D for ; Mon, 4 Feb 2019 16:35:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731704AbfBDQfH (ORCPT ); Mon, 4 Feb 2019 11:35:07 -0500 Received: from foss.arm.com ([217.140.101.70]:58048 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729127AbfBDQfH (ORCPT ); Mon, 4 Feb 2019 11:35:07 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E037115AB; Mon, 4 Feb 2019 08:35:06 -0800 (PST) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.197.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F35AE3F589; Mon, 4 Feb 2019 08:35:05 -0800 (PST) From: Andre Przywara To: Will Deacon Cc: Anisse Astier , kvm@vger.kernel.org Subject: [PATCH kvmtool 1/3] builtin-run: Replace strncpy calls with strlcpy Date: Mon, 4 Feb 2019 16:34:56 +0000 Message-Id: <20190204163458.188070-2-andre.przywara@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190204163458.188070-1-andre.przywara@arm.com> References: <20190204163458.188070-1-andre.przywara@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There are two uses of strncpy in builtin-run.c, where we don't make proper use of strncpy, so that GCC 8.x complains and aborts compilation. Replace those two calls with strlcpy(), which does the right thing in our case. Signed-off-by: Andre Przywara --- builtin-run.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 463a481f..f8dc6c72 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -300,7 +300,7 @@ static const char *find_kernel(void) k++; continue; } - strncpy(kernel, *k, PATH_MAX); + strlcpy(kernel, *k, PATH_MAX); return kernel; } @@ -418,7 +418,7 @@ static void resolve_program(const char *src, char *dst, size_t len) die("Pathname too long: %s -> %s\n", src, resolved_path); } else - strncpy(dst, src, len); + strlcpy(dst, src, len); } static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc) From patchwork Mon Feb 4 16:34:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 10796087 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BE5E4922 for ; Mon, 4 Feb 2019 16:35:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ACE362BA4E for ; Mon, 4 Feb 2019 16:35:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9EA782BA0D; Mon, 4 Feb 2019 16:35:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2EF9F2BA0D for ; Mon, 4 Feb 2019 16:35:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731724AbfBDQfJ (ORCPT ); Mon, 4 Feb 2019 11:35:09 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:58056 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729127AbfBDQfI (ORCPT ); Mon, 4 Feb 2019 11:35:08 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 13F29EBD; Mon, 4 Feb 2019 08:35:08 -0800 (PST) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.197.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 280343F589; Mon, 4 Feb 2019 08:35:07 -0800 (PST) From: Andre Przywara To: Will Deacon Cc: Anisse Astier , kvm@vger.kernel.org Subject: [PATCH kvmtool 2/3] virtio: use strlcpy Date: Mon, 4 Feb 2019 16:34:57 +0000 Message-Id: <20190204163458.188070-3-andre.przywara@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190204163458.188070-1-andre.przywara@arm.com> References: <20190204163458.188070-1-andre.przywara@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP GCC 8.x complains about improper usage of strncpy in virtio/net.c and virtio/scsi.c: In function 'virtio_scsi_init_one', inlined from 'virtio_scsi_init' at virtio/scsi.c:285:7: virtio/scsi.c:247:2: error: 'strncpy' specified bound 224 equals destination size [-Werror=stringop-truncation] strncpy((char *)&sdev->target.vhost_wwpn, disk->wwpn, sizeof(sdev->target.vhost_wwpn)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fix this and the other occurences in virtio/ by using strlcpy instead of strncpy. Signed-off-by: Andre Przywara --- virtio/net.c | 5 +++-- virtio/scsi.c | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/virtio/net.c b/virtio/net.c index 35ff2e9b..09140691 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -8,6 +8,7 @@ #include "kvm/uip.h" #include "kvm/guest_compat.h" #include "kvm/iovec.h" +#include "kvm/strbuf.h" #include #include @@ -283,12 +284,12 @@ static int virtio_net_request_tap(struct net_dev *ndev, struct ifreq *ifr, memset(ifr, 0, sizeof(*ifr)); ifr->ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR; if (tapname) - strncpy(ifr->ifr_name, tapname, sizeof(ifr->ifr_name)); + strlcpy(ifr->ifr_name, tapname, sizeof(ifr->ifr_name)); ret = ioctl(ndev->tap_fd, TUNSETIFF, ifr); if (ret >= 0) - strncpy(ndev->tap_name, ifr->ifr_name, sizeof(ndev->tap_name)); + strlcpy(ndev->tap_name, ifr->ifr_name, sizeof(ndev->tap_name)); return ret; } diff --git a/virtio/scsi.c b/virtio/scsi.c index c8400b64..a72bb2a9 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -8,6 +8,7 @@ #include "kvm/guest_compat.h" #include "kvm/virtio-pci.h" #include "kvm/virtio.h" +#include "kvm/strbuf.h" #include #include @@ -255,7 +256,7 @@ static int virtio_scsi_init_one(struct kvm *kvm, struct disk_image *disk) }, .kvm = kvm, }; - strncpy((char *)&sdev->target.vhost_wwpn, disk->wwpn, sizeof(sdev->target.vhost_wwpn)); + strlcpy((char *)&sdev->target.vhost_wwpn, disk->wwpn, sizeof(sdev->target.vhost_wwpn)); sdev->target.vhost_tpgt = strtol(disk->tpgt, NULL, 0); virtio_init(kvm, sdev, &sdev->vdev, &scsi_dev_virtio_ops, From patchwork Mon Feb 4 16:34:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 10796089 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0482F6C2 for ; Mon, 4 Feb 2019 16:35:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E7A992BA0D for ; Mon, 4 Feb 2019 16:35:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DC0CB2BA4E; Mon, 4 Feb 2019 16:35:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 957592BA1E for ; Mon, 4 Feb 2019 16:35:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731745AbfBDQfJ (ORCPT ); Mon, 4 Feb 2019 11:35:09 -0500 Received: from foss.arm.com ([217.140.101.70]:58064 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731725AbfBDQfJ (ORCPT ); Mon, 4 Feb 2019 11:35:09 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3CA9E15AB; Mon, 4 Feb 2019 08:35:09 -0800 (PST) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.197.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 514933F589; Mon, 4 Feb 2019 08:35:08 -0800 (PST) From: Andre Przywara To: Will Deacon Cc: Anisse Astier , kvm@vger.kernel.org Subject: [PATCH kvmtool 3/3] net/dhcp: avoid misleading strncpy Date: Mon, 4 Feb 2019 16:34:58 +0000 Message-Id: <20190204163458.188070-4-andre.przywara@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190204163458.188070-1-andre.przywara@arm.com> References: <20190204163458.188070-1-andre.przywara@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The code for copying an empty IP address into the DHCP opt buffer used strncpy, however used the source length as the size argument. GCC 8.x complains about it. Since the source string is actually fixed, just revert to the old strcpy, which gives us actually the same level of security in this case, but makes the compiler happy. Signed-off-by: Andre Przywara --- net/uip/dhcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/uip/dhcp.c b/net/uip/dhcp.c index 8f013002..9de5588b 100644 --- a/net/uip/dhcp.c +++ b/net/uip/dhcp.c @@ -131,7 +131,7 @@ static int uip_dhcp_fill_option(struct uip_info *info, struct uip_dhcp *dhcp, in opt[i++] = UIP_DHCP_TAG_ROOT; opt[i++] = strlen(EMPTY_ADDR); addr = (u32 *)&opt[i]; - strncpy((void *) addr, EMPTY_ADDR, strlen(EMPTY_ADDR)); + strcpy((void *) addr, EMPTY_ADDR); i += strlen(EMPTY_ADDR); i = uip_dhcp_fill_option_name_and_server(info, opt, i);