From nobody Fri May 3 10:46:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1548940417360216.11689726029272; Thu, 31 Jan 2019 05:13:37 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E564481F12; Thu, 31 Jan 2019 13:13:34 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 12FE1101963B; Thu, 31 Jan 2019 13:13:34 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D3FC24ED25; Thu, 31 Jan 2019 13:13:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x0VDDC4P013924 for ; Thu, 31 Jan 2019 08:13:12 -0500 Received: by smtp.corp.redhat.com (Postfix) id BB79C5D97E; Thu, 31 Jan 2019 13:13:12 +0000 (UTC) Received: from dhcp-16-175.lcy.redhat.com (unknown [10.42.16.175]) by smtp.corp.redhat.com (Postfix) with ESMTP id 34EC75D9D6; Thu, 31 Jan 2019 13:13:12 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 31 Jan 2019 13:13:09 +0000 Message-Id: <20190131131309.13921-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [cim PATCH] Ensure nul termination of hostname X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 31 Jan 2019 13:13:36 +0000 (UTC) Newest GCC warns that the string copying is potentially truncated and thus not nul terminated. In file included from /usr/include/string.h:494, from ../../src/Virt_HostSystem.c:23: In function =E2=80=98strncpy=E2=80=99, inlined from =E2=80=98resolve_host=E2=80=99 at ../../src/Virt_HostSyste= m.c:55:28, inlined from =E2=80=98get_fqdn=E2=80=99 at ../../src/Virt_HostSystem.c:= 92:23, inlined from =E2=80=98set_host_system_properties=E2=80=99 at ../../src/= Virt_HostSystem.c:109:13: /usr/include/bits/string_fortified.h:106:10: error: =E2=80=98__builtin_strn= cpy=E2=80=99 specified bound 256 equals destination size [-Werror=3Dstringo= p-truncation] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__de= st)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~~ In function =E2=80=98strncpy=E2=80=99, inlined from =E2=80=98resolve_host=E2=80=99 at ../../src/Virt_HostSyste= m.c:67:17, inlined from =E2=80=98get_fqdn=E2=80=99 at ../../src/Virt_HostSystem.c:= 92:23, inlined from =E2=80=98set_host_system_properties=E2=80=99 at ../../src/= Virt_HostSystem.c:109:13: /usr/include/bits/string_fortified.h:106:10: error: =E2=80=98__builtin_strn= cpy=E2=80=99 specified bound 256 equals destination size [-Werror=3Dstringo= p-truncation] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__de= st)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~~ cc1: all warnings being treated as errors Signed-off-by: Daniel P. Berrang=C3=A9 --- src/Virt_HostSystem.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Virt_HostSystem.c b/src/Virt_HostSystem.c index ebe8184..5bc52ca 100644 --- a/src/Virt_HostSystem.c +++ b/src/Virt_HostSystem.c @@ -38,7 +38,7 @@ =20 const static CMPIBroker *_BROKER; =20 -static int resolve_host(char *host, char *buf, int size) +static int resolve_host(char *host, int size) { struct hostent *he; int i; @@ -52,7 +52,8 @@ static int resolve_host(char *host, char *buf, int size) for (i =3D 0; he->h_aliases[i] !=3D NULL; i++) { if ((strchr(he->h_aliases[i], '.') !=3D NULL) && (strstr(he->h_aliases[i], "localhost") =3D=3D NULL)) { - strncpy(buf, he->h_aliases[i], size); + strncpy(host, he->h_aliases[i], size - 1); + host[size - 1] =3D '\0'; return 0; } } @@ -63,12 +64,13 @@ static int resolve_host(char *host, char *buf, int size) // but also be sure the value isn't empty and that it doe= sn't // contain "localhost" if ((he->h_name !=3D NULL) && (!STREQC(he->h_name, "")) &&=20 - (strstr(he->h_name, "localhost") =3D=3D NULL)) - strncpy(buf, he->h_name, size); - else if ((host !=3D NULL) && (!STREQC(host, "")) &&=20 - (strstr(host, "localhost") =3D=3D NULL)) - strncpy(buf, host, size); - else { + (strstr(he->h_name, "localhost") =3D=3D NULL)) { + strncpy(host, he->h_name, size - 1); + host[size - 1] =3D '\0'; + } else if ((host !=3D NULL) && (!STREQC(host, "")) && + (strstr(host, "localhost") =3D=3D NULL)) { + return 0; + } else { CU_DEBUG("Unable to find valid hostname value."); return -1; } @@ -76,20 +78,18 @@ static int resolve_host(char *host, char *buf, int size) return 0; } =20 -static int get_fqdn(char *buf, int size) +static int get_fqdn(char *host, int size) { - char host[256]; int ret =3D 0; =20 - if (gethostname(host, sizeof(host)) !=3D 0) { + if (gethostname(host, size) !=3D 0) { CU_DEBUG("gethostname(): %m"); return -1; } =20 - if (strchr(host, '.') !=3D NULL) - strncpy(buf, host, size); - else - ret =3D resolve_host(host, buf, size); + if (strchr(host, '.') =3D=3D NULL) { + ret =3D resolve_host(host, size); + } =20 return ret; } --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list