From nobody Sun Feb 8 03:48:22 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1726778392139427.9971194755449; Thu, 19 Sep 2024 13:39:52 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id C4D1F1634; Thu, 19 Sep 2024 16:39:50 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id DE9AC15A8; Thu, 19 Sep 2024 16:39:01 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 146B21460; Thu, 19 Sep 2024 16:38:55 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 4543B13BC for ; Thu, 19 Sep 2024 16:38:54 -0400 (EDT) Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-623-Wj70HXetOyiZ95NLiaJrZw-1; Thu, 19 Sep 2024 16:38:52 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9B2F1193EF42 for ; Thu, 19 Sep 2024 20:38:51 +0000 (UTC) Received: from vhost3.router.laine.org (unknown [10.22.32.101]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 31FAD195608A for ; Thu, 19 Sep 2024 20:38:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726778334; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wT9AyuoONCUTR75KAGti2DZzMKaykrfedZf6xDNjrB8=; b=bVCqwkwdafgggNW2ntDIJssW85TGM2JFLu6No1y8JrPhey2S3mmihSFeAcO/ECp/ObIaSl Gr1PyDwI81/M5OBG3NRx1/us+BbqsSF+8h8i9vK9H/tmwRGgAdDEZsm4S+oiH6MMDyIPUI 6Y4BwmXCP3GUZkb2jMrcuY2QrcCazKE= X-MC-Unique: Wj70HXetOyiZ95NLiaJrZw-1 From: Laine Stump To: devel@lists.libvirt.org Subject: [PATCH 2/4] util: fix virSocketAddrMask() when source and result are the same object Date: Thu, 19 Sep 2024 16:38:46 -0400 Message-ID: <20240919203848.44545-3-laine@redhat.com> In-Reply-To: <20240919203848.44545-1-laine@redhat.com> References: <20240919203848.44545-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JFLF2AQEWHKMZ6QA33I464ZBK4GHIYY6 X-Message-ID-Hash: JFLF2AQEWHKMZ6QA33I464ZBK4GHIYY6 X-MailFrom: laine@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1726778393606116600 Content-Type: text/plain; charset="utf-8"; x-default="true" Many years ago (2011), virSocketAddrMask() had caused a bug by failing to initialize an IPv6-specific field in the result virSocketAddr. This was fixed by memset(0)ing the entire result (*network) at the beginning of the function (thus making sure anything and everything was initialized). The problem is that virSocketAddrMask() has a comment above it that says that the source (addr) and destination (network) arguments can point to the same virSocketAddr. But in that case, the memset(*network, 0) at the top of the function is actually doing a memset(*addr, 0), and so there is nothing left for all the assignments to copy except a giant field of 0's. Fortunately in the 13 years since the memset was added, nobody has ever called virSocketAddrMask() with addr and network being the same. This patch makes the code agree with the comment by copying/masking into a local virSocketAddr (which is initialized to all 0!) and then copying that to *network after it's finished assigning things from addr. Fixes: ba08c5932e556aa4f5101357127a6224c40e5ebe Signed-off-by: Laine Stump Reviewed-by: J=C3=A1n Tomko --- src/util/virsocketaddr.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index 0a1ae1ac5f..60d8071da7 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -685,31 +685,34 @@ virSocketAddrMask(const virSocketAddr *addr, const virSocketAddr *netmask, virSocketAddr *network) { - memset(network, 0, sizeof(*network)); + virSocketAddr tmp =3D { 0 }; + if (addr->data.stor.ss_family !=3D netmask->data.stor.ss_family) { network->data.stor.ss_family =3D AF_UNSPEC; return -1; } =20 if (addr->data.stor.ss_family =3D=3D AF_INET) { - network->data.inet4.sin_addr.s_addr + tmp.data.inet4.sin_addr.s_addr =3D (addr->data.inet4.sin_addr.s_addr & netmask->data.inet4.sin_addr.s_addr); - network->data.inet4.sin_port =3D 0; - network->data.stor.ss_family =3D AF_INET; - network->len =3D addr->len; + tmp.data.inet4.sin_port =3D 0; + tmp.data.stor.ss_family =3D AF_INET; + tmp.len =3D addr->len; + *network =3D tmp; return 0; } if (addr->data.stor.ss_family =3D=3D AF_INET6) { size_t i; for (i =3D 0; i < 16; i++) { - network->data.inet6.sin6_addr.s6_addr[i] + tmp.data.inet6.sin6_addr.s6_addr[i] =3D (addr->data.inet6.sin6_addr.s6_addr[i] & netmask->data.inet6.sin6_addr.s6_addr[i]); } - network->data.inet6.sin6_port =3D 0; - network->data.stor.ss_family =3D AF_INET6; - network->len =3D addr->len; + tmp.data.inet6.sin6_port =3D 0; + tmp.data.stor.ss_family =3D AF_INET6; + tmp.len =3D addr->len; + *network =3D tmp; return 0; } network->data.stor.ss_family =3D AF_UNSPEC; --=20 2.46.0