From nobody Fri Oct 31 04:01:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=quarantine dis=quarantine) header.from=proton.me Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 174951619487316.114566329216018; Mon, 9 Jun 2025 17:43:14 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010463.1388555 (Exim 4.92) (envelope-from ) id 1uOn4d-0007WN-6B; Tue, 10 Jun 2025 00:42:43 +0000 Received: by outflank-mailman (output) from mailman id 1010463.1388555; Tue, 10 Jun 2025 00:42:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOn4d-0007WG-39; Tue, 10 Jun 2025 00:42:43 +0000 Received: by outflank-mailman (input) for mailman id 1010463; Tue, 10 Jun 2025 00:42:41 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOn4a-0007WA-Tc for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 00:42:41 +0000 Received: from mail-24418.protonmail.ch (mail-24418.protonmail.ch [109.224.244.18]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id caf5e65d-4593-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 02:42:31 +0200 (CEST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: caf5e65d-4593-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1749516148; x=1749775348; bh=3Kn+SRMaHCl+B8lAzrGsfMCNF9sC9Rlw+3e3xABI+Z8=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector: List-Unsubscribe:List-Unsubscribe-Post; b=ZonQ6wHSLCVW6PplNXoHWqOcDRCXdwSKaWapTbbgt06wUpI3iKch/lzXCDUYbBk4B H5A1Tq8PqGscc3v/g3XrD+Xiyq6ycAX4J0rVInT0gEq0DPdYyr5l898Ia2yKUNuAwG r4Y8JW6g8uPyvlxRLKel1XKPdgy3xgIeqVhvjYTmIxnz9UhhxhDn9U1q63Md4E29C/ i825kuln/Zi6qcCKvY3Xtsw8L/X2aSxtHRXJGByvrjY2knkUVjrvwne/PewmX7Bwf/ gfAvvEt7+YjH3A/RrD/Yq/qIMUWShrgXLWvWPuatPwbvEjf4Piw60y6NFIDAC5b2SA T8FCPZBduKG2g== Date: Tue, 10 Jun 2025 00:42:22 +0000 To: xen-devel@lists.xenproject.org From: dmkhn@proton.me Cc: andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, julien@xen.org, michal.orzel@amd.com, roger.pau@citrix.com, sstabellini@kernel.org, teddy.astie@vates.tech, dmukhin@ford.com Subject: [PATCH v6] xen/domain: rewrite emulation_flags_ok() Message-ID: <20250610004216.3012253-1-dmukhin@ford.com> Feedback-ID: 123220910:user:proton X-Pm-Message-ID: e3ce06358ded461b8b7e10989c1fef83beb2b810 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1749516197275116600 Content-Type: text/plain; charset="utf-8" From: Denis Mukhin Rewrite emulation_flags_ok() to simplify future modifications. No functional change intended. Signed-off-by: Denis Mukhin Reviewed-by: Teddy Astie --- Changes since v5: - optimized `configs[]` table - just one record for PV case - sorted entries in `configs[]` table by domain type: PV, then PVH, then HVM entries - addressed `caps` initializaton =20 Link to v5: https://lore.kernel.org/xen-devel/20250602191717.148361-3-dmukh= in@ford.com/ Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1= 861382846/ --- xen/arch/x86/domain.c | 86 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 18 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 7536b6c871..82b126351b 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -743,32 +743,82 @@ int arch_sanitise_domain_config(struct xen_domctl_cre= atedomain *config) return 0; } =20 +/* + * Verify that the domain's emulation flags resolve to a supported configu= ration. + * + * This ensures we only allow a known, safe subset of emulation combinatio= ns + * (for both functionality and security). Arbitrary mixes are likely to ca= use + * errors (e.g. null pointer dereferences). + * + * NB: use the internal X86_EMU_XXX symbols, not the public XEN_X86_EMU_XXX + * symbols. + */ static bool emulation_flags_ok(const struct domain *d, uint32_t emflags) { + enum { + CAP_PV =3D BIT(0, U), + CAP_HVM =3D BIT(1, U), + CAP_HWDOM =3D BIT(2, U), + CAP_DOMU =3D BIT(3, U), + }; + static const struct { + unsigned int caps; + uint32_t min; + uint32_t opt; + } configs[] =3D { +#ifdef CONFIG_PV + /* PV dom0 and domU */ + { + .caps =3D CAP_PV | CAP_HWDOM | CAP_DOMU, + .min =3D X86_EMU_PIT, + }, +#endif /* #ifdef CONFIG_PV */ + +#ifdef CONFIG_HVM + /* PVH dom0 */ + { + .caps =3D CAP_HVM | CAP_HWDOM, + .min =3D X86_EMU_LAPIC | X86_EMU_IOAPIC | X86_EMU_VPCI, + }, + + /* PVH domU */ + { + .caps =3D CAP_HVM | CAP_DOMU, + .min =3D X86_EMU_LAPIC, + }, + + /* HVM domU */ + { + .caps =3D CAP_HVM | CAP_DOMU, + .min =3D X86_EMU_ALL & ~(X86_EMU_VPCI | X86_EMU_USE_PIRQ), + /* HVM PIRQ feature is user-selectable. */ + .opt =3D X86_EMU_USE_PIRQ, + }, +#endif /* #ifdef CONFIG_HVM */ + }; + unsigned int i; + unsigned int caps =3D (is_pv_domain(d) ? CAP_PV : CAP_HVM) | + (is_hardware_domain(d) ? CAP_HWDOM : CAP_DOMU); + + /* + * NB: PV domain can have 0 in emulation_flags. + * See qemu-alpine-x86_64-gcc CI job. + * Inject fake flag to keep the code checks simple. + */ + if ( (caps & CAP_PV) && emflags =3D=3D 0 ) + emflags |=3D X86_EMU_PIT; + #ifdef CONFIG_HVM /* This doesn't catch !CONFIG_HVM case but it is better than nothing */ BUILD_BUG_ON(X86_EMU_ALL !=3D XEN_X86_EMU_ALL); #endif =20 - if ( is_hvm_domain(d) ) - { - if ( is_hardware_domain(d) && - emflags !=3D (X86_EMU_VPCI | X86_EMU_LAPIC | X86_EMU_IOAPIC) ) - return false; - if ( !is_hardware_domain(d) && - /* HVM PIRQ feature is user-selectable. */ - (emflags & ~X86_EMU_USE_PIRQ) !=3D - (X86_EMU_ALL & ~(X86_EMU_VPCI | X86_EMU_USE_PIRQ)) && - emflags !=3D X86_EMU_LAPIC ) - return false; - } - else if ( emflags !=3D 0 && emflags !=3D X86_EMU_PIT ) - { - /* PV or classic PVH. */ - return false; - } + for ( i =3D 0; i < ARRAY_SIZE(configs); i++ ) + if ( (caps & configs[i].caps) =3D=3D caps && + (emflags & ~configs[i].opt) =3D=3D configs[i].min ) + return true; =20 - return true; + return false; } =20 void __init arch_init_idle_domain(struct domain *d) --=20 2.34.1