From nobody Wed Nov 27 06:33:24 2024 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 1723042821268471.4741897296732; Wed, 7 Aug 2024 08:00:21 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 2DE90CE4; Wed, 7 Aug 2024 11:00:20 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id AED24CC9; Wed, 7 Aug 2024 11:00:01 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 43082985; Wed, 7 Aug 2024 10:59:59 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 C6B3ACC9 for ; Wed, 7 Aug 2024 10:59:58 -0400 (EDT) Received: from mx-prod-mc-01.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-308-Hu_LQiClNtyc7TmRCWZTtw-1; Wed, 07 Aug 2024 10:59:57 -0400 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 761CE1955BCE for ; Wed, 7 Aug 2024 14:59:55 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.79]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 919C219560AE; Wed, 7 Aug 2024 14:59:53 +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.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,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=1723042798; 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; bh=XBqtmTPubkQyRagM9TDGdoL5cN5R2ZfeJ7LQZ3SmV84=; b=bEDE0X0bIsYTdUprYCx3mqkmLAFeFKX0D0jUpA0Lfd1tfMjT9XHZhqQ05YhyZQlVOyVN1D o3adi8w10mRPcwTMxJuc4ShGImC3NPuPtVQDDy70Uj/A00eaVt+fWnEyoUuUHqqvy/W+m6 BcqqE8E07ieQgNg5naAWG4cXi9ILufk= X-MC-Unique: Hu_LQiClNtyc7TmRCWZTtw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: devel@lists.libvirt.org Cc: "Richard W.M. Jones" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH] util: open XML files before calling libxml2 Date: Wed, 7 Aug 2024 15:59:52 +0100 Message-ID: <20240807145952.2457298-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: LC2I6W55JWHJABA36FU6NDNOT2QE7PBN X-Message-ID-Hash: LC2I6W55JWHJABA36FU6NDNOT2QE7PBN X-MailFrom: berrange@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: 1723042822560116600 Content-Type: text/plain; charset="utf-8" Libxml2 has awful error reporting behaviour when reading files. When we fail to load a file from the test driver we see: $ virsh -c test:///wibble.xml I/O warning : failed to load external entity "/wibble.xml" error: failed to connect to the hypervisor error: XML error: failed to parse xml document '/wibble.xml' where the I/O warning line is something printed by libxml2 itself, which also lacks any useful detail. Switching to our own file reading code we can massively improve things: $ ./build/tools/virsh -c test:///wibble.xml error: failed to connect to the hypervisor error: Failed to open file '/wibble.xml': No such file or directory Using 10 MB as an upper limit on XML file size ought to be sufficient for any XML files libvirt is reading. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Richard W.M. Jones --- src/util/virxml.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index a7b75fd7b3..f6b937b277 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1133,6 +1133,7 @@ virXMLParseHelper(int domcode, xmlNodePtr rootnode; const char *docname; int parseFlags =3D XML_PARSE_NONET | XML_PARSE_NOWARNING; + g_autofree char *xmlStrPtr =3D NULL; =20 if (filename) docname =3D filename; @@ -1155,10 +1156,11 @@ virXMLParseHelper(int domcode, } =20 if (filename) { - xml =3D xmlCtxtReadFile(pctxt, filename, NULL, parseFlags); - } else { - xml =3D xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, url, NULL, parseFla= gs); + if (virFileReadAll(filename, 1024*1024*10, &xmlStrPtr) < 0) + return NULL; + xmlStr =3D xmlStrPtr; } + xml =3D xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, url, NULL, parseFlags); =20 if (!xml) { if (virGetLastErrorCode() =3D=3D VIR_ERR_OK) { --=20 2.45.2