From nobody Wed Nov 25 19:12:19 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 38.145.34.151 as permitted sender) client-ip=38.145.34.151; 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 38.145.34.151 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1789976710; cv=none; d=zohomail.com; s=zohoarc; b=dKmZyBHQg9FjHDi3W0PVSsQqDE+Jl3Tmhsz5CBBc7ajUbawlT9/J4I3nHlDJcTvbtHJTEd/NAXR48MGrvrIEQxriR/HfumojXouJkrJBkrZBvfTH1W37treL60LrU39CEWdpnMISCpIYsEUMU85lMN8PymrCx6kOJmPeB8PEYEc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1789976710; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Subject:Subject:To:To:Message-Id:Cc; bh=JB9JIpCMBKN50AGADEPL9XixIkCdoLCFE968g3Dt7PI=; b=VTl0yxQi0GNX8dxpyd6+IcqIuJIh6rJLyRyiyho85a2Bx6PAl7sd17maSzUanMyLz8l3KlxNBRUJDcRCdHKXLBOxgWcwImQoa/E6xkF8SVjDTSjygo185Lh47EeaWBKzddBq/7w04WTXD/5XPJD+abiUwfEsD+79j6WSTa+73Nw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 38.145.34.151 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [38.145.34.151]) by mx.zohomail.com with SMTPS id 1789976710313137.4977655701573; Mon, 21 Sep 2026 00:45:10 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 26F543F9B8; Mon, 21 Sep 2026 03:45:08 -0400 (EDT) Received: from [172.19.199.16] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id F21153F880; Mon, 21 Sep 2026 03:44:04 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id A0E1A3F30F; Mon, 21 Sep 2026 03:43:55 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 408493F308 for ; Mon, 21 Sep 2026 03:43:54 -0400 (EDT) Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-149-_5fScMJaNTqdGSpGNZ5tyw-1; Mon, 21 Sep 2026 03:43:48 -0400 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F2483180133D for ; Mon, 21 Sep 2026 07:43:47 +0000 (UTC) Received: from moe (unknown [10.43.3.236]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6363341D for ; Mon, 21 Sep 2026 07:43:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1789976633; 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; bh=JB9JIpCMBKN50AGADEPL9XixIkCdoLCFE968g3Dt7PI=; b=b0bqyU/aey1bByC+X31nCGsKSWt8LFX4B0swPJU4NMgoJ7HQGqHOF5OXOhs7y7uJk6uJEk zQXrdW5NdhxNd++H+P2Q6l5nUerWtMwMEobZBsiNkqDYZeFMcxHdcOweaywX6bOMib1SDu riXuDqFmgQzaMZ376m2SNiqdxYqklV8= X-MC-Unique: _5fScMJaNTqdGSpGNZ5tyw-1 X-Mimecast-MFC-AGG-ID: _5fScMJaNTqdGSpGNZ5tyw_1789976628 To: devel@lists.libvirt.org Subject: [PATCH] virbitmap: Avoid UB with zero sized bitmaps Date: Mon, 21 Sep 2026 09:43:44 +0200 Message-ID: <0febe3f9af2a9f453395651f034f33fa222d3e7a.1789976624.git.mprivozn@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: SuvIUa_cvr8wMHsj5YnX6Zp72EjbEDY8MWzCBUyfrZg_1789976628 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: DDZ6HWYVK6EZ5DGP4JZV47P6RJFUPA27 X-Message-ID-Hash: DDZ6HWYVK6EZ5DGP4JZV47P6RJFUPA27 X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Michal Privoznik via Devel Reply-To: Michal Privoznik X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1789976715045158500 Content-Type: text/plain; charset="utf-8"; x-default="true" From: Michal Privoznik Ever since of v6.9.0-rc1~384 we allow zero sized bitmaps. If that's the case then the map is kept unallocated (NULL). Unfortunately, some virBitmap APIs call memcpy()/memset() which doesn't allow NULL pointers. Introduce checks to these APIs (virBitmapNewCopy(), virBitmapSetAll() and virBitmapClearAll()). While at it, also introduce a test case to virbitmaptest to catch UB in our CI. Signed-off-by: Michal Privoznik Reviewed-by: Roman Bogorodskiy --- src/util/virbitmap.c | 9 ++++++++- tests/virbitmaptest.c | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index 78545501b7..b9b737a087 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -587,7 +587,8 @@ virBitmapNewCopy(virBitmap *src) { virBitmap *dst =3D virBitmapNew(src->nbits); =20 - memcpy(dst->map, src->map, dst->map_len * sizeof(src->map[0])); + if (G_LIKELY(dst->map_len > 0)) + memcpy(dst->map, src->map, dst->map_len * sizeof(src->map[0])); =20 return dst; } @@ -770,6 +771,9 @@ virBitmapClearTail(virBitmap *bitmap) */ void virBitmapSetAll(virBitmap *bitmap) { + if (G_UNLIKELY(bitmap->map_len =3D=3D 0)) + return; + memset(bitmap->map, 0xff, bitmap->map_len * (VIR_BITMAP_BITS_PER_UNIT / CHAR_BIT)); =20 @@ -786,6 +790,9 @@ void virBitmapSetAll(virBitmap *bitmap) void virBitmapClearAll(virBitmap *bitmap) { + if (G_UNLIKELY(bitmap->map_len =3D=3D 0)) + return; + memset(bitmap->map, 0, bitmap->map_len * (VIR_BITMAP_BITS_PER_UNIT / CHAR_BIT)); } diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c index 709c62ab54..549956ad04 100644 --- a/tests/virbitmaptest.c +++ b/tests/virbitmaptest.c @@ -768,6 +768,30 @@ test17(const void *opaque G_GNUC_UNUSED) } =20 =20 +static int +test18(const void *opaque G_GNUC_UNUSED) +{ + g_autoptr(virBitmap) map =3D virBitmapNew(0); + g_autoptr(virBitmap) copy =3D virBitmapNewCopy(map); + + /* Creating an empty bitmap means underlying array of bits is + * unallocated (NULL). For some virBitmap APIs this may lead + * to UB. The whole point of this test is to call those and + * rely on clang's UB sanitizer to catch such errors at + * runtime. */ + + virBitmapSetAll(map); + virBitmapClearAll(copy); + + if (!virBitmapEqual(map, copy)) { + fprintf(stderr, "empty maps don't equal\n"); + return -1; + } + + return 0; +} + + #define TESTBINARYOP(A, B, RES, FUNC) \ testBinaryOpData.a =3D A; \ testBinaryOpData.b =3D B; \ @@ -847,6 +871,9 @@ mymain(void) if (virTestRun("test17", test17, NULL) < 0) ret =3D -1; =20 + if (virTestRun("test18", test18, NULL) < 0) + ret =3D -1; + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 --=20 2.55.0