From nobody Thu Nov 12 15:08:24 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=1793017317; cv=none; d=zohomail.com; s=zohoarc; b=jDAt5HdxQ7Ri1ND956C2HV+cvurPRf9WpCWlcrsecRvB8c+UwZQTIB4GI+3F0STpgGBrqEx1TuElO6OwkomdzBlxvMhHyUPnr6psUokrvmvfBXEx00X6ZBAupmIHFClK2OvKwF+iCzRMaI/KptFcyAkDzEykYhU9L2ykMlFPFhM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793017317; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To: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=XwezxmVPqF8Bpenng+qe8GFVJkMuL6+6Gi0MzDoYIS8=; b=JyG7+sHqIjRkb5/g+A+TRYzKK+I8XX6tQh8FHkDNh3PuNntBC7niLNdEuf1t+xfwf55bE1eNEQtfPx84IDIdf6WqYzotEwiBgPR2f/i+z1/ae8JfgaA5UF5t68TGmt4gj2XH9ngBcW4Hzf0g2X/fgNU0vfbaBohacss/HX9noiQ= 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 179301731775747.041410877961994; Mon, 26 Oct 2026 05:21:57 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 453D041B69; Mon, 26 Oct 2026 08:21:56 -0400 (EDT) Received: from [172.19.199.19] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id 6DBAC41ACD; Mon, 26 Oct 2026 08:19:16 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 49A703F366; Mon, 26 Oct 2026 08:19:00 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 1D5223F2FC for ; Mon, 26 Oct 2026 08:18:59 -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-688-bpp1si80NviBdd27tpprpg-1; Mon, 26 Oct 2026 08:18:57 -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 5FDE01800753 for ; Mon, 26 Oct 2026 12:18:56 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A387344C; Mon, 26 Oct 2026 12:18:55 +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=-3.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,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=1793017138; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XwezxmVPqF8Bpenng+qe8GFVJkMuL6+6Gi0MzDoYIS8=; b=Pxbmifx6LWjNWgFQZj49Z/xLvJ25Us6KcRPxZCqTaR95m7yoofhmxL0RM6X8Wikvl5Cc99 YD7zvUb9MnOFyJg8Yd0T1Hqkn8EMZwwX6RMZPzV1lBnzsfCqm3Tc/OHWO3DHtBhsTDB5vX KEfy90hTj4KQUHmykPaThrEh9oBjlfM= X-MC-Unique: bpp1si80NviBdd27tpprpg-1 X-Mimecast-MFC-AGG-ID: bpp1si80NviBdd27tpprpg_1793017136 To: devel@lists.libvirt.org Subject: [PATCH 1/7] src: add virHostnameValidate helper for validation Date: Mon, 26 Oct 2026 12:18:47 +0000 Message-ID: <20261026121853.735305-2-berrange@redhat.com> In-Reply-To: <20261026121853.735305-1-berrange@redhat.com> References: <20261026121853.735305-1-berrange@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: NfP_8IQ1gMfxU1FkU9dv-42xmiAnQLQc2kaWhTAJ5hU_1793017136 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 27Z76YP5LLRK7I3THS6PVZTZZQ24LKYU X-Message-ID-Hash: 27Z76YP5LLRK7I3THS6PVZTZZQ24LKYU X-MailFrom: berrange@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: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793017318043158500 From: Daniel P. Berrang=C3=A9 Hostnames must comply with certain rules about maximum length and permitted characters. Since these need to be validated in several places, introduce a helper to centralize the rules and provide testing. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/libvirt_private.syms | 1 + src/util/virutil.c | 50 ++++++++++++++++++++++++++++ src/util/virutil.h | 2 ++ tests/utiltest.c | 72 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 798d2d28c8..66520a3e35 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3791,6 +3791,7 @@ virGetUserRuntimeDirectory; virGetUserShell; virHostGetDRMRenderNode; virHostHasIOMMU; +virHostnameValidate; virIndexToDiskName; virKernelCmdlineMatchParam; virKernelCmdlineNextParam; diff --git a/src/util/virutil.c b/src/util/virutil.c index 2be9a460ac..75ecb6da5a 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -1979,3 +1979,53 @@ virPipeNonBlock(int fds[2]) { return virPipeImpl(fds, true, true); } + +#define VIR_HOSTNAME_VALID_CHARS \ + "abcdefghijklmnopqrstuvwxyz" \ + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + "0123456789-" + +int virHostnameValidate(const char *hostname, bool qualified) +{ + g_auto(GStrv) labels =3D g_strsplit(hostname, ".", -1); + size_t i; + + if (!qualified && g_strv_length(labels) > 1) { + virReportError(VIR_ERR_INVALID_ARG, + _("Hostname '%1$s' must not be qualified"), + hostname); + return -1; + } + if (strlen(hostname) > 253) { + virReportError(VIR_ERR_INVALID_ARG, + _("Hostname '%1$s' must be 253 characters or less"), + hostname); + return -1; + } + + for (i =3D 0; labels[i] !=3D NULL; i++) { + size_t labellen =3D strlen(labels[i]); + if (labellen =3D=3D 0) { + virReportError(VIR_ERR_INVALID_ARG, + _("Hostname '%1$s' components must be non-empty= "), + hostname); + return -1; + } + + if (labellen > 63) { + virReportError(VIR_ERR_INVALID_ARG, + _("Hostname '%1$s' components must be 63 charac= ters or less"), + hostname); + return -1; + } + + if (strspn(labels[i], + VIR_HOSTNAME_VALID_CHARS) !=3D labellen) { + virReportError(VIR_ERR_INVALID_ARG, + _("Hostname '%1$s' components must only contain= a-z, A-Z, 0-9 and -"), + hostname); + return -1; + } + } + return 0; +} diff --git a/src/util/virutil.h b/src/util/virutil.h index 5678c339ba..b2718fa1df 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -230,3 +230,5 @@ int virPipeQuiet(int fds[2]); * Returns: -1 on error, 0 on success */ int virPipeNonBlock(int fds[2]); + +int virHostnameValidate(const char *hostname, bool qualified); diff --git a/tests/utiltest.c b/tests/utiltest.c index 0bef502a84..7e14c0a161 100644 --- a/tests/utiltest.c +++ b/tests/utiltest.c @@ -423,6 +423,77 @@ testGetSubIDs(const void *data G_GNUC_UNUSED) } =20 =20 +struct testHostnamesData +{ + const char *name; + bool qualified; + bool valid; +}; + +static const struct testHostnamesData testHostnamesData[] =3D { + { .name =3D "foo", .qualified =3D false, .valid =3D true }, + { .name =3D "foo", .qualified =3D true, .valid =3D true }, + { .name =3D "foo.example.com", .qualified =3D false, .valid =3D false = }, + { .name =3D "foo.example.com", .qualified =3D true, .valid =3D true }, + { .name =3D "foo ", .qualified =3D false, .valid =3D false }, + { .name =3D "foo\n", .qualified =3D false, .valid =3D false }, + { .name =3D "foo\t", .qualified =3D false, .valid =3D false }, + { .name =3D "0foo", .qualified =3D false, .valid =3D true }, + { .name =3D "foo\n", .qualified =3D false, .valid =3D false }, + { .name =3D "foo.example.com\t", .qualified =3D true, .valid =3D false= }, + { .name =3D ".foo", .qualified =3D false, .valid =3D false }, + { .name =3D "foo.", .qualified =3D false, .valid =3D false }, + { .name =3D "foo.example.com.", .qualified =3D false, .valid =3D false= }, + { .name =3D "foo..example.com", .qualified =3D false, .valid =3D false= }, + { .name =3D "foo1234567foo1234567foo1234567foo1234567foo1234567foo1234= 567abc", + .qualified =3D false, .valid =3D true }, + { .name =3D "foo1234567foo1234567foo1234567foo1234567foo1234567foo1234= 567abcd", + .qualified =3D false, .valid =3D false }, + { .name =3D + "foo1234567foo1234567foo1234567foo1234567foo123456." + "foo1234567foo1234567foo1234567foo1234567foo123456." + "foo1234567foo1234567foo1234567foo1234567foo123456." + "foo1234567foo1234567foo1234567foo1234567foo123456." + "foo1234567foo1234567foo1234567foo1234567foo123456." + "abc", + .qualified =3D true, .valid =3D true }, + { .name =3D + "foo1234567foo1234567foo1234567foo1234567foo123456." + "foo1234567foo1234567foo1234567foo1234567foo123456." + "foo1234567foo1234567foo1234567foo1234567foo123456." + "foo1234567foo1234567foo1234567foo1234567foo123456." + "foo1234567foo1234567foo1234567foo1234567foo123456." + "abcd", + .qualified =3D true, .valid =3D false }, +}; + + +static int +testHostnames(const void *data G_GNUC_UNUSED) +{ + size_t i; + for (i =3D 0; i < G_N_ELEMENTS(testHostnamesData); i++) { + int ret =3D virHostnameValidate(testHostnamesData[i].name, + testHostnamesData[i].qualified); + if (ret < 0) { + if (testHostnamesData[i].valid) { + g_printerr("Hostname '%s' should be valid: %s", + testHostnamesData[i].name, + virGetLastErrorMessage()); + return -1; + } + } else { + if (!testHostnamesData[i].valid) { + g_printerr("Hostname '%s' should be invalid", + testHostnamesData[i].name); + return -1; + } + } + } + return 0; +} + + static int mymain(void) { @@ -447,6 +518,7 @@ mymain(void) DO_TEST(KernelCmdlineNextParam); DO_TEST(KernelCmdlineMatchParam); DO_TEST(GetSubIDs); + DO_TEST(Hostnames); =20 return result =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } --=20 2.56.0 From nobody Thu Nov 12 15:08:24 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=1793017773; cv=none; d=zohomail.com; s=zohoarc; b=B/2mE4tpH7AzlnIz7BxPuIdZB18uxkKl21KBVJJ0ZkbyPzpFC8TdzDQDlcYgrtuVI1xeW1pnYHWEtoSp1nu7bo2fC7YzAmnK3l8sSuWpStLUatK0DPDHmRsPVkM0T5uyULjIBuUyLscbo75FVFDm/CUbU0dP2aPpubgCSOfTOC4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793017773; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To: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=BPHqv8OCAKOpFQLPcJT/4HnxH8jBe//QAo0HuvcNoTk=; b=BIgWPZg03Yyp6tklrevbg0gr49j8uuDmtf9RLbQTUV7Ss5lX93/wAWeMsB8fcBt8LOoGZ80Jmi41l1/aQYbZpSeoZL5s/O4IV6LLZHDmoeKQKxjo605i80JLg5av0nTD2qN70n2khPQwrs0QGFjGTze14LDwr9nztpteNfOCe44= 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 1793017773709425.57973498762385; Mon, 26 Oct 2026 05:29:33 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 3DA874191D; Mon, 26 Oct 2026 08:29:32 -0400 (EDT) Received: from [172.19.199.19] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id 0E9DF423ED; Mon, 26 Oct 2026 08:19:48 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id CABD841A2F; Mon, 26 Oct 2026 08:19:31 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 160D641B74 for ; Mon, 26 Oct 2026 08:19:07 -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-694-qRrgHCiqNgyvgCROQrFT4Q-1; Mon, 26 Oct 2026 08:18:58 -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 8B40A18009CB for ; Mon, 26 Oct 2026 12:18:57 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CB562410; Mon, 26 Oct 2026 12:18:56 +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=-3.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,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=1793017146; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BPHqv8OCAKOpFQLPcJT/4HnxH8jBe//QAo0HuvcNoTk=; b=bGNhefrfZ2QgbjFQMEV3yiwV9hJnMS76engodKo8FHPEDI8kX2qIoWfJ/R66+kVcZhF28T WlELW8ql2AmDZrv888tZ4L+gTcVFpf6r1U5K936c62bMKA4QWjkLeh0sY+KSz6xs3n/UIO 6xexit+rs1QIzKX8EokE9FOK0HQkWOo= X-MC-Unique: qRrgHCiqNgyvgCROQrFT4Q-1 X-Mimecast-MFC-AGG-ID: qRrgHCiqNgyvgCROQrFT4Q_1793017137 To: devel@lists.libvirt.org Subject: [PATCH 2/7] conf: perform full validation of hostnames / domain names Date: Mon, 26 Oct 2026 12:18:48 +0000 Message-ID: <20261026121853.735305-3-berrange@redhat.com> In-Reply-To: <20261026121853.735305-1-berrange@redhat.com> References: <20261026121853.735305-1-berrange@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: XR4HGOcS3GYiRn325eSqlkHrXiyG_Twah58U41sJuqI_1793017137 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: IFHVMAJYA3UR62NVNSR5P5QLNO422CQA X-Message-ID-Hash: IFHVMAJYA3UR62NVNSR5P5QLNO422CQA X-MailFrom: berrange@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: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793017775059158500 From: Daniel P. Berrang=C3=A9 The current network XML parser only validates that a DHCP hostname starts with a-Z character or a digit, or that a TXT record name doesn't contain space or comma. This is way too weak, and misses validation from other parts of the schema. This can allow invalid characters that result in invalid syntax in dnsmasq config files. Use the new hostname validation API to check all places that accept a hostname or domain name as a hardening task to prevent accidental creation of malformed dnsmasq cnofig files. Fixes: https://gitlab.com/libvirt/libvirt/-/work_items/929 Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Laine Stump --- src/conf/network_conf.c | 31 +++++++++---------- .../nat-network-bad-hostname.xml | 22 +++++++++++++ .../nat-network-dhcp-hosts-bad.xml | 21 +++++++++++++ .../nat-network-dns-hosts-bad.xml | 26 ++++++++++++++++ tests/networkxmlconftest.c | 3 ++ tests/virschematest.c | 3 ++ 6 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 tests/networkxmlconfdata/nat-network-bad-hostname.xml create mode 100644 tests/networkxmlconfdata/nat-network-dhcp-hosts-bad.xml create mode 100644 tests/networkxmlconfdata/nat-network-dns-hosts-bad.xml diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 8eba1321d6..2fec4d2b91 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -35,6 +35,7 @@ #include "viruuid.h" #include "virbuffer.h" #include "virstring.h" +#include "virutil.h" =20 #define VIR_FROM_THIS VIR_FROM_NETWORK =20 @@ -533,10 +534,7 @@ virNetworkDHCPHostDefParseXML(const char *networkName, } =20 name =3D virXMLPropString(node, "name"); - if (name && !(g_ascii_isalpha(name[0]) || g_ascii_isdigit(name[0]))) { - virReportError(VIR_ERR_XML_ERROR, - _("Cannot use host name '%1$s' in network '%2$s'"), - name, networkName); + if (name && virHostnameValidate(name, true) < 0) { return -1; } =20 @@ -664,10 +662,7 @@ virNetworkDNSHostDefParseXML(const char *networkName, if (!(hostnames[i] =3D virXMLNodeContentString(g_ptr_array_ind= ex(hostnameNodes, i)))) return -1; =20 - if (*hostnames[i] =3D=3D '\0') { - virReportError(VIR_ERR_XML_DETAIL, - _("Missing hostname in network '%1$s' DNS H= OST record"), - networkName); + if (virHostnameValidate(hostnames[i], true) < 0) { return -1; } } @@ -792,8 +787,11 @@ virNetworkDNSSrvDefParseXML(const char *networkName, def->domain =3D virXMLPropString(node, "domain"); def->target =3D virXMLPropString(node, "target"); =20 - if (virNetworkDNSDefCheckLineBreaks("SRV", "domain", def->domain) < 0 = || - virNetworkDNSDefCheckLineBreaks("SRV", "target", def->target) < 0) + if (def->domain && + virHostnameValidate(def->domain, true) < 0) + goto error; + if (def->target && STRNEQ(def->target, ".") && + virHostnameValidate(def->target, true) < 0) goto error; =20 ret =3D virXPathUInt("string(./@port)", ctxt, &def->port); @@ -852,18 +850,13 @@ virNetworkDNSTxtDefParseXML(const char *networkName, virNetworkDNSTxtDef *def, bool partialOkay) { - const char *bad =3D " ,"; - if (!(def->name =3D virXMLPropString(node, "name"))) { virReportError(VIR_ERR_XML_DETAIL, _("missing required name attribute in DNS TXT recor= d of network %1$s"), networkName); goto error; } - if (strcspn(def->name, bad) !=3D strlen(def->name)) { - virReportError(VIR_ERR_XML_DETAIL, - _("prohibited character in DNS TXT record name '%1$= s' of network %2$s"), - def->name, networkName); + if (virHostnameValidate(def->name, true) < 0) { goto error; } if (!(def->value =3D virXMLPropString(node, "value")) && !partialOkay)= { @@ -958,6 +951,9 @@ virNetworkDNSDefParseXML(const char *networkName, _("Invalid forwarder element, must contain = at least one of addr or domain")); return -1; } + if (def->forwarders[i].domain && + virHostnameValidate(def->forwarders[i].domain, true) < 0) + return -1; def->nfwds++; } } @@ -1621,6 +1617,9 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt, /* Parse network domain information */ domain_node =3D virXPathNode("./domain[1]", ctxt); def->domain =3D virXMLPropString(domain_node, "name"); + if (def->domain && + virHostnameValidate(def->domain, true) < 0) + return NULL; if (virXMLPropTristateBool(domain_node, "localOnly", VIR_XML_PROP_NONE, &def->domainLocalOnly) < 0) diff --git a/tests/networkxmlconfdata/nat-network-bad-hostname.xml b/tests/= networkxmlconfdata/nat-network-bad-hostname.xml new file mode 100644 index 0000000000..24f823f0e6 --- /dev/null +++ b/tests/networkxmlconfdata/nat-network-bad-hostname.xml @@ -0,0 +1,22 @@ + + default + 81ff0d90-c91e-6742-64da-4a736edb9a9b + + + + + + + + + + + + + + + + + + + diff --git a/tests/networkxmlconfdata/nat-network-dhcp-hosts-bad.xml b/test= s/networkxmlconfdata/nat-network-dhcp-hosts-bad.xml new file mode 100644 index 0000000000..9d9711c7bf --- /dev/null +++ b/tests/networkxmlconfdata/nat-network-dhcp-hosts-bad.xml @@ -0,0 +1,21 @@ + + default + 81ff0d90-c91e-6742-64da-4a736edb9a9b + + + + + + + + + + + + + + + + + + diff --git a/tests/networkxmlconfdata/nat-network-dns-hosts-bad.xml b/tests= /networkxmlconfdata/nat-network-dns-hosts-bad.xml new file mode 100644 index 0000000000..b5f43b21dc --- /dev/null +++ b/tests/networkxmlconfdata/nat-network-dns-hosts-bad.xml @@ -0,0 +1,26 @@ + + default + 81ff0d90-c91e-6742-64da-4a736edb9a9c + + + + + + pudding fish + + + host + gateway + + + shared + names + + + shared + names + + + + + diff --git a/tests/networkxmlconftest.c b/tests/networkxmlconftest.c index c0d3886a69..eb60c19604 100644 --- a/tests/networkxmlconftest.c +++ b/tests/networkxmlconftest.c @@ -319,6 +319,7 @@ mymain(void) DO_TEST("open-network"); DO_TEST_PARSE_ERROR("open-network-with-forward-dev"); DO_TEST("nat-network"); + DO_TEST_PARSE_ERROR("nat-network-bad-hostname"); DO_TEST("netboot-network"); DO_TEST("netboot-proxy-network"); DO_TEST("netboot-tftp"); @@ -330,6 +331,8 @@ mymain(void) DO_TEST("nat-network-dns-srv-records"); DO_TEST("nat-network-dns-srv-record-minimal"); DO_TEST("nat-network-dns-hosts"); + DO_TEST_PARSE_ERROR("nat-network-dns-hosts-bad"); + DO_TEST_PARSE_ERROR("nat-network-dhcp-hosts-bad"); DO_TEST("nat-network-dns-forward-plain"); DO_TEST("nat-network-dns-forwarders"); DO_TEST("nat-network-dns-forwarder-no-resolv"); diff --git a/tests/virschematest.c b/tests/virschematest.c index fabe45b65d..e1fa82aa97 100644 --- a/tests/virschematest.c +++ b/tests/virschematest.c @@ -306,6 +306,9 @@ static const char *skip_networkxmlconfdata[] =3D { "nat-network-dns-srv-record-domain-newline.xml", "nat-network-dns-srv-record-target-newline.xml", "nat-network-dns-txt-recor-record-newline.xml", + "nat-network-bad-hostname.xml", + "nat-network-dhcp-hosts-bad.xml", + "nat-network-dns-hosts-bad.xml", NULL, }; =20 --=20 2.56.0 From nobody Thu Nov 12 15:08:24 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=1793017397; cv=none; d=zohomail.com; s=zohoarc; b=XNqA0jh5GPdkJyiEKunIyb+6rDjA0VWh3nDFL0COioLUjqhymdf4NidNxymr9AtO1ek8AzrN2SZs9RzytBx2yQEChmVwL7dzEE0rEj5hgGmVioKAucwIvoZV8z8CfwdpRZ7rCfQ7Gv3a1A+rcFYi+Mmjf+0EYDL/eSc68SQlDt8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793017397; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To: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=m0VFjdMce3q7Pq61M54pirDslydZYKUsrVX+thd4T2g=; b=jR4O6TKlCGiKNNnLeOMgMBqSVMry8wwb/rTJ6xlXhKj42B2KeXHu1cP7p3143Q9hJH0aucLjvR5KG8wtsUNjdwcnjQYyhBAezsPBYV/AK3sPUk39FRuV1prqrnhhEAZ5DOym7KdfK19ftgcZFD+eukwA9/zbV7WWG36+UwdEGAg= 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 179301739749135.246040066616615; Mon, 26 Oct 2026 05:23:17 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 4F60D41AD0; Mon, 26 Oct 2026 08:23:16 -0400 (EDT) Received: from [172.19.199.19] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id 0BB5241C87; Mon, 26 Oct 2026 08:19:23 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id BEA053F383; Mon, 26 Oct 2026 08:19:01 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 2BCB43F366 for ; Mon, 26 Oct 2026 08:19:01 -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-552-j4b_dLKfPWWYlcU15Clm-w-1; Mon, 26 Oct 2026 08:18:59 -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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 97F531955EA7 for ; Mon, 26 Oct 2026 12:18:58 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DB13944C; Mon, 26 Oct 2026 12:18:57 +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=-0.1 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,RCVD_IN_SBL_CSS,SPF_HELO_PASS autolearn=no autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793017140; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m0VFjdMce3q7Pq61M54pirDslydZYKUsrVX+thd4T2g=; b=b/6YkKIAlJqteRLliLwBVUeG7veLt3pCblzq43mPKCUEXVY9Vjm9b8kAbuS9vqMf0eOAkw lySSr49ns+BcWfxSXOQtIVQHb5fqlLWuka4zLOKy3aE6TfRvjkr+sviZCXK/olahwC12yw A4YYlI911/PHG7r5Ajnl6p+bsX1N52E= X-MC-Unique: j4b_dLKfPWWYlcU15Clm-w-1 X-Mimecast-MFC-AGG-ID: j4b_dLKfPWWYlcU15Clm-w_1793017138 To: devel@lists.libvirt.org Subject: [PATCH 3/7] src: remove fallback when opening dnsmasq addnhosts file fails Date: Mon, 26 Oct 2026 12:18:49 +0000 Message-ID: <20261026121853.735305-4-berrange@redhat.com> In-Reply-To: <20261026121853.735305-1-berrange@redhat.com> References: <20261026121853.735305-1-berrange@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: vLb_l2eKKf62XvBYsBpnJQemrw5aSGMZq1wvfMDWreA_1793017138 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JC74UHDO66IWI4NJEE7KGJK2NXYTU7OQ X-Message-ID-Hash: JC74UHDO66IWI4NJEE7KGJK2NXYTU7OQ X-MailFrom: berrange@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: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793017399675158500 From: Daniel P. Berrang=C3=A9 When writing the dnsmasq addnhosts file to a temporary file, there is a fallback which tries to open the original file. We expect creating the temporary file to always succeed, and if it fails something serious has gone wrong that we want to have reported. So this fallback is expected to be dead code. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Laine Stump --- src/util/virdnsmasq.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index 7d6de943cf..817e060a44 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -161,7 +161,6 @@ addnhostsWrite(const char *path, { g_autofree char *tmp =3D NULL; FILE *f; - bool istmp =3D true; size_t i, j; int rc =3D 0; =20 @@ -171,20 +170,14 @@ addnhostsWrite(const char *path, =20 tmp =3D g_strdup_printf("%s.new", path); =20 - if (!(f =3D fopen(tmp, "w"))) { - istmp =3D false; - if (!(f =3D fopen(path, "w"))) - return -errno; - } + if (!(f =3D fopen(tmp, "w"))) + return -errno; =20 for (i =3D 0; i < nhosts; i++) { if (fputs(hosts[i].ip, f) =3D=3D EOF || fputc('\t', f) =3D=3D EOF)= { rc =3D -errno; VIR_FORCE_FCLOSE(f); - - if (istmp) - unlink(tmp); - + unlink(tmp); return rc; } =20 @@ -192,10 +185,7 @@ addnhostsWrite(const char *path, if (fputs(hosts[i].hostnames[j], f) =3D=3D EOF || fputc('\t', = f) =3D=3D EOF) { rc =3D -errno; VIR_FORCE_FCLOSE(f); - - if (istmp) - unlink(tmp); - + unlink(tmp); return rc; } } @@ -203,10 +193,7 @@ addnhostsWrite(const char *path, if (fputc('\n', f) =3D=3D EOF) { rc =3D -errno; VIR_FORCE_FCLOSE(f); - - if (istmp) - unlink(tmp); - + unlink(tmp); return rc; } } @@ -214,7 +201,7 @@ addnhostsWrite(const char *path, if (VIR_FCLOSE(f) =3D=3D EOF) return -errno; =20 - if (istmp && rename(tmp, path) < 0) { + if (rename(tmp, path) < 0) { rc =3D -errno; unlink(tmp); return rc; --=20 2.56.0 From nobody Thu Nov 12 15:08:24 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=1793017496; cv=none; d=zohomail.com; s=zohoarc; b=CmtOSaYZcdtikuoS3g1dz+CYJrgXaBtWsa5ZvpdZw0Hjr4HcvKeaIfng0ZcBe9Gtq1WKzbomNtZArMMCG+vu1IuFsIE7KPq57XR82BzkNl/SihDa5k4T7+L935QZXPHR6Lps0AsqjuBxE2RBTjdfEMTNtreb0dlE8vYuL9kp34Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793017496; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To: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=vwSezCVbJssIv5AW1ErTCRJ6NqcAx558qY/NAIUfNVA=; b=OX/6uQniuOWJJ/0WN3OcoQABRopdIvdTkeuVfNudQKvvArUk2cWKVa/YgBa92Q153e5ZfDcWQh91oJNJMdmwOo18N0oyTBVl5p/HC2kB/S4j/MZMk91Te0pwGHEx/1Ylq7NAEQ3gTACJDRSIttkq55riYRbbYfWx/pXLZkcMG/U= 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 1793017496637101.65099148567515; Mon, 26 Oct 2026 05:24:56 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 4AEB741B82; Mon, 26 Oct 2026 08:24:55 -0400 (EDT) Received: from [172.19.199.19] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id 8B90241B7B; Mon, 26 Oct 2026 08:19:28 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id B818C3F366; Mon, 26 Oct 2026 08:19:02 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 1A58B3F2FC for ; Mon, 26 Oct 2026 08:19:02 -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-614-qmv9DaORP22uF-IyDlafkw-1; Mon, 26 Oct 2026 08:19:00 -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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A676E1955F14 for ; Mon, 26 Oct 2026 12:18:59 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E8E7944C; Mon, 26 Oct 2026 12:18:58 +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=-0.1 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,RCVD_IN_SBL_CSS,SPF_HELO_PASS autolearn=no autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793017141; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vwSezCVbJssIv5AW1ErTCRJ6NqcAx558qY/NAIUfNVA=; b=SBKCvf4RzAEhxzR0e897iONun5qUa4nwycTZQ60A6q9M4/XG0ky8fCvZvlG4i/RrjXN4Rp QUBgUAFLzfngmGLUTAtcHJ9fS3+t9bFb1/Z3qfmUy5lj6qpeu72YqnyomlaMlmAjMknvwy mTvODI/4xfOVtyA23sLJZCXsGbaHm2k= X-MC-Unique: qmv9DaORP22uF-IyDlafkw-1 X-Mimecast-MFC-AGG-ID: qmv9DaORP22uF-IyDlafkw_1793017139 To: devel@lists.libvirt.org Subject: [PATCH 4/7] src: use virFileRewrite for dnsmasq addnhosts file Date: Mon, 26 Oct 2026 12:18:50 +0000 Message-ID: <20261026121853.735305-5-berrange@redhat.com> In-Reply-To: <20261026121853.735305-1-berrange@redhat.com> References: <20261026121853.735305-1-berrange@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: q0FoHjdarfDm7KKdUf_Uh379ZL2cAHlUPkxN3T15epQ_1793017139 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: RA45CFCKFQUNPSBAMFOUGHTQUEHLGNU2 X-Message-ID-Hash: RA45CFCKFQUNPSBAMFOUGHTQUEHLGNU2 X-MailFrom: berrange@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: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793017499886158500 From: Daniel P. Berrang=C3=A9 The current addnhostsWrite() method implements logic that is already present in virFileRewrite. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Laine Stump --- src/util/virdnsmasq.c | 82 ++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 51 deletions(-) diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index 817e060a44..656665e174 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -154,77 +154,57 @@ addnhostsNew(const char *name, return NULL; } =20 + static int -addnhostsWrite(const char *path, - dnsmasqAddnHost *hosts, - unsigned int nhosts) +addnhostsWrite(int fd, + const char *path, + const void *opaque) { - g_autofree char *tmp =3D NULL; - FILE *f; + const dnsmasqAddnHostsfile *addnhostsfile =3D opaque; size_t i, j; - int rc =3D 0; =20 /* even if there are 0 hosts, create a 0 length file, to allow * for runtime addition. */ =20 - tmp =3D g_strdup_printf("%s.new", path); - - if (!(f =3D fopen(tmp, "w"))) - return -errno; - - for (i =3D 0; i < nhosts; i++) { - if (fputs(hosts[i].ip, f) =3D=3D EOF || fputc('\t', f) =3D=3D EOF)= { - rc =3D -errno; - VIR_FORCE_FCLOSE(f); - unlink(tmp); - return rc; - } - - for (j =3D 0; j < hosts[i].nhostnames; j++) { - if (fputs(hosts[i].hostnames[j], f) =3D=3D EOF || fputc('\t', = f) =3D=3D EOF) { - rc =3D -errno; - VIR_FORCE_FCLOSE(f); - unlink(tmp); - return rc; - } - } - - if (fputc('\n', f) =3D=3D EOF) { - rc =3D -errno; - VIR_FORCE_FCLOSE(f); - unlink(tmp); - return rc; + for (i =3D 0; i < addnhostsfile->nhosts; i++) { + if (safewrite(fd, addnhostsfile->hosts[i].ip, + strlen(addnhostsfile->hosts[i].ip)) < 0) + goto error; + if (safewrite(fd, "\t", 1) < 0) + goto error; + + for (j =3D 0; j < addnhostsfile->hosts[i].nhostnames; j++) { + if (safewrite(fd, addnhostsfile->hosts[i].hostnames[j], + strlen(addnhostsfile->hosts[i].hostnames[j])) < = 0) + goto error; + if (safewrite(fd, "\t", 1) < 0) + goto error; } - } - - if (VIR_FCLOSE(f) =3D=3D EOF) - return -errno; =20 - if (rename(tmp, path) < 0) { - rc =3D -errno; - unlink(tmp); - return rc; + if (safewrite(fd, "\n", 1) < 0) + goto error; } =20 return 0; + + error: + virReportSystemError(errno, + _("cannot write config file '%1$s'"), + path); + return -1; } =20 static int addnhostsSave(dnsmasqAddnHostsfile *addnhostsfile) { - int err =3D addnhostsWrite(addnhostsfile->path, addnhostsfile->hosts, - addnhostsfile->nhosts); - - if (err < 0) { - virReportSystemError(-err, _("cannot write config file '%1$s'"), - addnhostsfile->path); - return -1; - } - - return 0; + return virFileRewrite(addnhostsfile->path, + 0600, -1, -1, + addnhostsWrite, + addnhostsfile); } =20 + static int genericFileDelete(char *path) { --=20 2.56.0 From nobody Thu Nov 12 15:08:24 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=1793017546; cv=none; d=zohomail.com; s=zohoarc; b=anNbteH2dZzJa3Yg3NowtGlvOTIRiuc21AZKNGu9Wt70w7D8xGv8RflCzuoriyG5duj575kZ5z6nRNqSGzUYtp7Jb0AhmuFO6j4tVFWg3j3D1+b0WNhr022n0OcZC+ApUQSwr3vhbujgu0EGnzGBMAwIzkEAkDzwVpvxToE+srg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793017546; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To: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=DIa8Iv+iWT8Du3mkvc2h8WakKYG9KLIYqICkC1Wx130=; b=j1cM1AU1qMx4rJSygwnvblN9UKkmKWVKj1WAj6bt9BbYhpI7XfEqHn8S6uhAld06k5mrJGdncdJS68eqz7XEF8LyPU7u2rbnN2Zj8Uf8D+1xh7uhDuERePLh1Bt18s0939hasOPipBDWPkVZyhvQ7HY4OvxUxv9tccsVO64Y4Qw= 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 1793017546335764.7269788717194; Mon, 26 Oct 2026 05:25:46 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 12CA441B69; Mon, 26 Oct 2026 08:25:45 -0400 (EDT) Received: from [172.19.199.19] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id 5120341BA1; Mon, 26 Oct 2026 08:19:33 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id B6E4D3F383; Mon, 26 Oct 2026 08:19:03 -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 348F23F366 for ; Mon, 26 Oct 2026 08:19:03 -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-647-tWNtvFrHOoCwWaRKnrnKnw-1; Mon, 26 Oct 2026 08:19:01 -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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B52B51955E97 for ; Mon, 26 Oct 2026 12:19:00 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 02C9C410; Mon, 26 Oct 2026 12:18:59 +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=-0.1 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_SBL_CSS, SPF_HELO_PASS autolearn=no autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793017142; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DIa8Iv+iWT8Du3mkvc2h8WakKYG9KLIYqICkC1Wx130=; b=Ov+cFemv9El8GeO/gxalXV79pHcdKYNl0WkuNUXs9ihBk9IUfcmxyDC7aITMbhDVGCMjAV uXwQZbyQO+vDtAtprL4w1MLnDnV/Qoaa3faJS7mZKLcv0huMj7TH1JU56+ETsGz5fz/jiW NMvb1oRz+XBKGJDaujbNl7gnvy4EG58= X-MC-Unique: tWNtvFrHOoCwWaRKnrnKnw-1 X-Mimecast-MFC-AGG-ID: tWNtvFrHOoCwWaRKnrnKnw_1793017140 To: devel@lists.libvirt.org Subject: [PATCH 5/7] src: remove fallback when opening dnsmasq hosts file fails Date: Mon, 26 Oct 2026 12:18:51 +0000 Message-ID: <20261026121853.735305-6-berrange@redhat.com> In-Reply-To: <20261026121853.735305-1-berrange@redhat.com> References: <20261026121853.735305-1-berrange@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: yG-8WZLOpSBVx4CK5qXXYXWjmhNoXzzkQ2V5muJUvec_1793017140 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: KWRWGJ6V53WJXGXROSSCND5X47BL7SB7 X-Message-ID-Hash: KWRWGJ6V53WJXGXROSSCND5X47BL7SB7 X-MailFrom: berrange@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: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793017550724158500 From: Daniel P. Berrang=C3=A9 When writing the dnsmasq hosts file to a temporary file, there is a fallback which tries to open the original file. We expect creating the temporary file to always succeed, and if it fails something serious has gone wrong that we want to have reported. So this fallback is expected to be dead code. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Laine Stump --- src/util/virdnsmasq.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index 656665e174..7bb1188516 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -320,7 +320,6 @@ hostsfileWrite(const char *path, { g_autofree char *tmp =3D NULL; FILE *f; - bool istmp =3D true; size_t i; int rc =3D 0; =20 @@ -331,19 +330,14 @@ hostsfileWrite(const char *path, tmp =3D g_strdup_printf("%s.new", path); =20 if (!(f =3D fopen(tmp, "w"))) { - istmp =3D false; - if (!(f =3D fopen(path, "w"))) - return -errno; + return -errno; } =20 for (i =3D 0; i < nhosts; i++) { if (fputs(hosts[i].host, f) =3D=3D EOF || fputc('\n', f) =3D=3D EO= F) { rc =3D -errno; VIR_FORCE_FCLOSE(f); - - if (istmp) - unlink(tmp); - + unlink(tmp); return rc; } } @@ -351,7 +345,7 @@ hostsfileWrite(const char *path, if (VIR_FCLOSE(f) =3D=3D EOF) return -errno; =20 - if (istmp && rename(tmp, path) < 0) { + if (rename(tmp, path) < 0) { rc =3D -errno; unlink(tmp); return rc; --=20 2.56.0 From nobody Thu Nov 12 15:08:24 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=1793017600; cv=none; d=zohomail.com; s=zohoarc; b=SoNPAwS98LPyQMy761eRweXWQgrgnFMniNgtBWS9eV+b5cFytnrToora15eIKiUTosgv9U6jCn8C/E2sbb80D7UDxnJt9AiLgXf2/cZE1T1ODFMh5rF9GIxte9UZ6NFXVHWWdfje4bB8TUqMJwnp/U4pTjNYHypqYxxe67KHISk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793017600; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To: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=+53oScN1gIbhrfBcDBmMfiGftaKZETs5PFwKWNSg/Io=; b=Jrv3Gg3XF4WDk0e3pbhm4tal1U2R9+rLLD0QkkONFmO1PrJ/cKVlFYqAEmA/XW3UE3AZhL6LAFWJq8uxQBfbdUnASsB6dWmV62ugkpKy+2c/Mkacowrc3rW4X28e5sYzSPIPhluqS8M9nxyNKApoFeMW7wLdLVzybYneomEfNeg= 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 1793017600806388.6168332345617; Mon, 26 Oct 2026 05:26:40 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id AC1BD41B9C; Mon, 26 Oct 2026 08:26:39 -0400 (EDT) Received: from [172.19.199.19] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id 16F4F41DAB; Mon, 26 Oct 2026 08:19:39 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 5D5F541C05; Mon, 26 Oct 2026 08:19:15 -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 6CA6A41A4D for ; Mon, 26 Oct 2026 08:19:04 -0400 (EDT) Received: from mx-prod-mc-03.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-619-zRRQMpKAMkyLppdB8Rff4w-1; Mon, 26 Oct 2026 08:19:02 -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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D40C31955EA8 for ; Mon, 26 Oct 2026 12:19:01 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1EDCE44C; Mon, 26 Oct 2026 12:19:00 +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=-0.1 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_SBL_CSS, SPF_HELO_PASS autolearn=no autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793017144; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+53oScN1gIbhrfBcDBmMfiGftaKZETs5PFwKWNSg/Io=; b=QQ2TsP7cT4+Nhrv0Cmtr1vSvVkEpAcQ6hykEZdXiFDe+Nw5ZHIg3pAWZhY4bZfLtXmF3Qc AVL4GbAU4fHpsOpvQWbn+18F9Uy6K2F55aWDP+OBxxKHJr0c8U/xfM4B+m6JropdTCHdLg hxHx3IXUzo+24sHXYJgKOFZPFDL5YvE= X-MC-Unique: zRRQMpKAMkyLppdB8Rff4w-1 X-Mimecast-MFC-AGG-ID: zRRQMpKAMkyLppdB8Rff4w_1793017141 To: devel@lists.libvirt.org Subject: [PATCH 6/7] src: use virFileRewrite for dnsmasq hosts file Date: Mon, 26 Oct 2026 12:18:52 +0000 Message-ID: <20261026121853.735305-7-berrange@redhat.com> In-Reply-To: <20261026121853.735305-1-berrange@redhat.com> References: <20261026121853.735305-1-berrange@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: 4G3Vj3a0LcM8zRIvW0FdDc4b102eOGfDDjgQqNn-QGY_1793017141 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: SBWYOBLEO6YLCIEL3OBYWBJASA4AXIJM X-Message-ID-Hash: SBWYOBLEO6YLCIEL3OBYWBJASA4AXIJM X-MailFrom: berrange@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: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793017601747158500 From: Daniel P. Berrang=C3=A9 The current hostsfileWrite() method implements logic that is already present in virFileRewrite. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Laine Stump --- src/util/virdnsmasq.c | 62 ++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index 7bb1188516..0e12e9ab38 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -314,59 +314,37 @@ hostsfileNew(const char *name, } =20 static int -hostsfileWrite(const char *path, - dnsmasqDhcpHost *hosts, - unsigned int nhosts) +hostsfileWrite(int fd, + const char *path, + const void *opaque) { - g_autofree char *tmp =3D NULL; - FILE *f; + const dnsmasqHostsfile *hostsfile =3D opaque; size_t i; - int rc =3D 0; - - /* even if there are 0 hosts, create a 0 length file, to allow - * for runtime addition. - */ - - tmp =3D g_strdup_printf("%s.new", path); =20 - if (!(f =3D fopen(tmp, "w"))) { - return -errno; - } - - for (i =3D 0; i < nhosts; i++) { - if (fputs(hosts[i].host, f) =3D=3D EOF || fputc('\n', f) =3D=3D EO= F) { - rc =3D -errno; - VIR_FORCE_FCLOSE(f); - unlink(tmp); - return rc; - } - } - - if (VIR_FCLOSE(f) =3D=3D EOF) - return -errno; - - if (rename(tmp, path) < 0) { - rc =3D -errno; - unlink(tmp); - return rc; + for (i =3D 0; i < hostsfile->nhosts; i++) { + if (safewrite(fd, hostsfile->hosts[i].host, + strlen(hostsfile->hosts[i].host)) < 0) + goto error; + if (safewrite(fd, "\n", 1) < 0) + goto error; } =20 return 0; + + error: + virReportSystemError(errno, + _("cannot write config file '%1$s'"), + path); + return -1; } =20 static int hostsfileSave(dnsmasqHostsfile *hostsfile) { - int err =3D hostsfileWrite(hostsfile->path, hostsfile->hosts, - hostsfile->nhosts); - - if (err < 0) { - virReportSystemError(-err, _("cannot write config file '%1$s'"), - hostsfile->path); - return -1; - } - - return 0; + return virFileRewrite(hostsfile->path, + 0600, -1, -1, + hostsfileWrite, + hostsfile); } =20 /** --=20 2.56.0 From nobody Thu Nov 12 15:08:24 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=1793017651; cv=none; d=zohomail.com; s=zohoarc; b=ZP276JyPZh5wH0277RfQLD1uSJ7Hn6ah30YCCMH/2FT1pJ21zMJIEX+763T1BC18/AMjrwOtrPhAqxhWUzE9G8i2L2CxoPznapC7vLTtYyWxGDYja7bCmRfc+WPAMod7XYNFvoWFkZk+yt6aOHHp31xbaVeP7zZ8hk7KrS5DRZI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793017651; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To: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=M2xrfGq4oaQh6dSlyiLsKDmhXOm8hPBApGvJPCMn5Tk=; b=hTBxDSkl3Mz1eIWYkzXNxusxJGfZbB5E1hXXhsPB2OMKnrSUSR6ylNAVHyyoVSSaIZamzNXaLpqDXKRg0W+TRMsLgMena1AtKfOy+/WXh4N7QHu/sTkh/7NrXdVUDEflYCByt56zkkMYjHKS6uvtdxDL2Ql33c+RrPb0BZBMp3o= 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 1793017651803117.74809174987877; Mon, 26 Oct 2026 05:27:31 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id C017141C3A; Mon, 26 Oct 2026 08:27:30 -0400 (EDT) Received: from [172.19.199.19] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id 55E2041E10; Mon, 26 Oct 2026 08:19:43 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 08FD441C2D; Mon, 26 Oct 2026 08:19:18 -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 44E2941AF9 for ; Mon, 26 Oct 2026 08:19:05 -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-637-l-dkjml-OWmJhUv9p-cLxg-1; Mon, 26 Oct 2026 08:19:03 -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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DF0E01955F0D for ; Mon, 26 Oct 2026 12:19:02 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2DD04410; Mon, 26 Oct 2026 12:19:02 +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=-0.1 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_SBL_CSS, SPF_HELO_PASS autolearn=no autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793017145; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M2xrfGq4oaQh6dSlyiLsKDmhXOm8hPBApGvJPCMn5Tk=; b=CpxFErzhQLF3NcDY73iFXu7Dd4wPotFQzAwKjvuKl9SaWLuVASlfRibwdA96vlQ5iWlzJJ knQT95khawuxJvmcAZr5Che30JJLG5iegAOxwrsUqwovEwSAUaUuOI4PE5Aeslx+bCSxnj Kuro+AzkqIipIbDYTsQ9CJWRF6yE4hw= X-MC-Unique: l-dkjml-OWmJhUv9p-cLxg-1 X-Mimecast-MFC-AGG-ID: l-dkjml-OWmJhUv9p-cLxg_1793017143 To: devel@lists.libvirt.org Subject: [PATCH 7/7] util: sanity check dnsmasq hosts/addnhosts lines for no whitespace Date: Mon, 26 Oct 2026 12:18:53 +0000 Message-ID: <20261026121853.735305-8-berrange@redhat.com> In-Reply-To: <20261026121853.735305-1-berrange@redhat.com> References: <20261026121853.735305-1-berrange@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: ZlogUF5C4qLFbiR-o0Vi2aLEjKFYIHaPd04aDuMs6o4_1793017143 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 2QXLK7C4WFEGCJIT3EPM7AF5SNAYDEKQ X-Message-ID-Hash: 2QXLK7C4WFEGCJIT3EPM7AF5SNAYDEKQ X-MailFrom: berrange@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: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1793017654630158500 From: Daniel P. Berrang=C3=A9 Although the network XML parser should already be rejecting hostnames with forbidden characters, add sanity checking before writing the dnsmasq hostsfile / addhosts files that no unexpected whitespace is present. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Laine Stump --- src/util/virdnsmasq.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index 0e12e9ab38..a66577fd67 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -198,6 +198,19 @@ addnhostsWrite(int fd, static int addnhostsSave(dnsmasqAddnHostsfile *addnhostsfile) { + size_t i, j; + for (i =3D 0; i < addnhostsfile->nhosts; i++) { + for (j =3D 0; j < addnhostsfile->hosts[i].nhostnames; j++) { + if (strcspn(addnhostsfile->hosts[i].hostnames[j], "\t\n\r ") != =3D + strlen(addnhostsfile->hosts[i].hostnames[j])) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("dnsmasq hostname '%1$s' contains forbidd= en whitespace"), + addnhostsfile->hosts[i].hostnames[j]); + return -1; + } + } + } + return virFileRewrite(addnhostsfile->path, 0600, -1, -1, addnhostsWrite, @@ -341,6 +354,17 @@ hostsfileWrite(int fd, static int hostsfileSave(dnsmasqHostsfile *hostsfile) { + size_t i; + for (i =3D 0; i < hostsfile->nhosts; i++) { + if (strcspn(hostsfile->hosts[i].host, "\t\n\r ") !=3D + strlen(hostsfile->hosts[i].host)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("dnsmasq hostname '%1$s' contains forbidden w= hitespace"), + hostsfile->hosts[i].host); + return -1; + } + } + return virFileRewrite(hostsfile->path, 0600, -1, -1, hostsfileWrite, --=20 2.56.0