From nobody Tue May 7 07:57:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1591601398; cv=none; d=zohomail.com; s=zohoarc; b=WKAAzLt4oMl9MIpnuaAM7xLBLmUq4U9Bl/i8gMq/QFlF7yqBiUAiGVSgFAZ/f/i2tPBbUlKd4GEtxVEs09kQVzsfSBeIlty+Q7ihdqLy5EbwLMTmiw3u9wwcjAVa1A+6+9GJybbA/Q4KI17fR6lVy+UFfy+Hjit1BZc06wtSfkI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591601398; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=UT9GKALVD7ZZpy+lYWU1wdLLFXK8X0l/a8iN3useVsg=; b=jj9PtiVy0OjiIUORn+F0xQINVnsakqnnEtCslZi9sHLEOw/VjiNtqsa6HNU/cvhz5n4YrKOs28+Eus48p9PngwChziqVPGKS2e6IOqDueN62eP07dac6KrPFd8fovN4Zk7zXy0jdU/CU0aId/YZuxraNN0eG+G1wZQurUyAKiIM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1591601397834593.9728386386713; Mon, 8 Jun 2020 00:29:57 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jiCDd-0002Ab-Mn; Mon, 08 Jun 2020 07:29:17 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jiCDa-0002AW-Et for xen-devel@lists.xenproject.org; Mon, 08 Jun 2020 07:29:16 +0000 Received: from mo4-p00-ob.smtp.rzone.de (unknown [81.169.146.217]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id be0f3126-a959-11ea-b24e-12813bfff9fa; Mon, 08 Jun 2020 07:29:10 +0000 (UTC) Received: from sender by smtp.strato.de (RZmta 46.9.2 DYNA|AUTH) with ESMTPSA id I09bd2w587SwEk4 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)) (Client did not present a certificate); Mon, 8 Jun 2020 09:28:58 +0200 (CEST) X-Inumbo-ID: be0f3126-a959-11ea-b24e-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1591601349; s=strato-dkim-0002; d=aepfle.de; h=Message-Id:Date:Subject:Cc:To:From:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=UT9GKALVD7ZZpy+lYWU1wdLLFXK8X0l/a8iN3useVsg=; b=eE1VpybZlOKtF/ZL5fxalTwHCzDkVjg8a7/OSzIR83ctEFrjlMawGJg7Faq71E9ZO0 yUbbTuUoaK+zFU3lFKrhjvl+xdWbC0lFLN+5pxmYLDXrHRcMZoUomcnaUb3cc4VmiJ3w Xmh6aSzm13GeUoZ2hx/t5TG4ATI7AOq3De5JzOmkJtm7Fug/jqOIg6IsTdaWTg0nuQSb mS82qhttRrXNAefdMbZ4tsuOiD/dSOr8OalkViBVXdEteoLYbax76hmDcmuYpOZ2KLC6 8QUp2+ENetME2jJqiagCWMUPpNOth3zbd3J9Skv4BfEHlqPIKiEefRfR6GE7EXVKElj5 n2hw== X-RZG-AUTH: ":P2EQZWCpfu+qG7CngxMFH1J+3q8wa/QXkBR9MXjAuzBW/OdlBZQ4AHSS3GpKjw==" X-RZG-CLASS-ID: mo00 From: Olaf Hering To: xen-devel@lists.xenproject.org Subject: [PATCH v1] tools: fix usage of strncpy Date: Mon, 8 Jun 2020 09:28:54 +0200 Message-Id: <20200608072855.26589-1-olaf@aepfle.de> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Anthony PERARD , Olaf Hering , Ian Jackson , Wei Liu Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" In case of truncation no trailing zero will be added to the target string. Reduce the amount of bytes to copy by one to make sure a trailing zero always exists. In file included from /usr/include/string.h:495, from libxl_internal.h:38, from libxl_utils.c:20: In function 'strncpy', inlined from 'libxl__prepare_sockaddr_un' at libxl_utils.c:1262:5: /usr/include/bits/string_fortified.h:106:10: error: '__builtin_strncpy' spe= cified bound 108 equals destination size [-Werror=3Dstringop-truncation] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__de= st)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~~ cc1: all warnings being treated as errors Signed-off-by: Olaf Hering --- gcc may not detect the off-by-one error in libxl__prepare_sockaddr_un, fix = the strncpy usage anyway. tools/libvchan/vchan-socket-proxy.c | 8 ++++---- tools/libxl/libxl_utils.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/libvchan/vchan-socket-proxy.c b/tools/libvchan/vchan-soc= ket-proxy.c index 13700c5d67..b312f05ca7 100644 --- a/tools/libvchan/vchan-socket-proxy.c +++ b/tools/libvchan/vchan-socket-proxy.c @@ -140,7 +140,7 @@ static int set_nonblocking(int fd, int nonblocking) { static int connect_socket(const char *path_or_fd) { int fd; char *endptr; - struct sockaddr_un addr; + struct sockaddr_un addr =3D {}; =20 fd =3D strtoll(path_or_fd, &endptr, 0); if (*endptr =3D=3D '\0') { @@ -153,7 +153,7 @@ static int connect_socket(const char *path_or_fd) { return -1; =20 addr.sun_family =3D AF_UNIX; - strncpy(addr.sun_path, path_or_fd, sizeof(addr.sun_path)); + strncpy(addr.sun_path, path_or_fd, sizeof(addr.sun_path) - 1); if (connect(fd, (const struct sockaddr *)&addr, sizeof(addr)) =3D=3D -= 1) { close(fd); return -1; @@ -167,7 +167,7 @@ static int connect_socket(const char *path_or_fd) { static int listen_socket(const char *path_or_fd) { int fd; char *endptr; - struct sockaddr_un addr; + struct sockaddr_un addr =3D {}; =20 fd =3D strtoll(path_or_fd, &endptr, 0); if (*endptr =3D=3D '\0') { @@ -180,7 +180,7 @@ static int listen_socket(const char *path_or_fd) { return -1; =20 addr.sun_family =3D AF_UNIX; - strncpy(addr.sun_path, path_or_fd, sizeof(addr.sun_path)); + strncpy(addr.sun_path, path_or_fd, sizeof(addr.sun_path) - 1); if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) =3D=3D -1) { close(fd); return -1; diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c index f360f5e228..83592e829d 100644 --- a/tools/libxl/libxl_utils.c +++ b/tools/libxl/libxl_utils.c @@ -1259,7 +1259,7 @@ int libxl__prepare_sockaddr_un(libxl__gc *gc, } memset(un, 0, sizeof(struct sockaddr_un)); un->sun_family =3D AF_UNIX; - strncpy(un->sun_path, path, sizeof(un->sun_path)); + strncpy(un->sun_path, path, sizeof(un->sun_path) - 1); return 0; } =20