From nobody Mon Nov 23 22:31:52 2026 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=pass; 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=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1795022531; cv=none; d=zohomail.com; s=zohoarc; b=gY4w36hpxiPae2n4ATrnwP47zywpbaVPkYLCO/zEv2bniPz8qgThFVTUxCVlED1SggWisvCdqgYD86iFigTYNMK683gixktGRfuDPTYVr78KkQyEVXLQypxV9p92O2Lf7bklMMlmcF8x56Q9F1vreyakXDOVLSyTdRON2d2+Lk8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795022531; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ReIEaztj5uI4r5dihuOxuObOLIuNOZx/WQLYKd7Hhs8=; b=JRJrjkXpv9uvJUvOLAdigMicqYvTNkoyLMdm8o1Zi1Vv6APASa//mWnZg4Y+fwcZpLYlQ1DeerEe3NL+p0ZxSxpGYh5nV3W1n/HP5e6Uar7FU8MlXpjmUhz7cTBqE3qbTlSsVhS6sWrfdqeVZROqKOy8gxVH1FhEX1/EKoQ2JEY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1795022531758586.7247129375867; Wed, 18 Nov 2026 09:22:11 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1494952.1694191 (Exim 4.92) (envelope-from ) id 1xTjM0-0005Gg-Kj; Wed, 18 Nov 2026 17:21:52 +0000 Received: by outflank-mailman (output) from mailman id 1494952.1694191; Wed, 18 Nov 2026 17:21:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xTjM0-0005GY-F1; Wed, 18 Nov 2026 17:21:52 +0000 Received: by outflank-mailman (input) for mailman id 1494952; Wed, 18 Nov 2026 17:21:50 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xTjLy-0005GQ-RD for xen-devel@lists.xenproject.org; Wed, 18 Nov 2026 17:21:50 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xTjLy-00000004uUh-0lfY; Wed, 18 Nov 2026 17:21:50 +0000 Received: from 224.pool85-54-217.dynamic.orange.es ([85.54.217.224] helo=localhost) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTjLx-00000001ZkC-3G6N; Wed, 18 Nov 2026 17:21:50 +0000 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" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:Message-ID:Date:Subject:Cc:To:From; bh=ReIEaztj5uI4r5dihuOxuObOLIuNOZx/WQLYKd7Hhs8=; b=E7VOwtO61V+kZ3ZGlMvrhW7uxV 1nLA+xBPFRUnUjMQW01b4/iWDmJydcajr5FS5WeDPdkVi5kwNN/ATo+SiLkQcZmGGoZIpWkAJD48S Z11uYN9aRANikPVTRFjKBECJHIbWl7TWXXi5TqUUxeE6Ze2LKYx2WcGkqc8Ik06gnCXY=; From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper , Teddy Astie Subject: [PATCH] x86/p2m: prevent replacing special p2m entries Date: Wed, 18 Nov 2026 18:20:32 +0100 Message-ID: <20261118172032.78501-1-roger@xenproject.org> X-Mailer: git-send-email 2.55.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1795022533610158500 The current logic in both p2m_add_foreign() and xenmem_add_to_physmap_one() allows replacing special p2m types, that should otherwise only be established/cleaned up via special accessors. When the backing page for a foreign p2m-type mapping only has one reference left, the put_page() in p2m_entry_modify() will free the page ahead of it being flushed from the p2m and the IOMMU TLBs. This can be triggered with IOREQ servers, and requires a domain that has privileges over another one (ie: a stubdom). Such a domain would create an IOREQ server, map it using XENMEM_acquire_resource (which creates a p2m_map_foreign p2m entry), destroy the IOREQ server, and afterwards map something over the IOREQ entries using XENMEM_add_to_physmap{,_batch}. At this point the removal from the p2m will trigger an assert in p2m_entry_modify() on debug builds, or in releases < 4.22 free the page before the TLBs are flushed. Something similar can also happen with grant mppaings: entries being replaced using p2m_add_foreign() or xenmem_add_to_physmap_one() will then fail when attempting to be cleaned up by replace_grant_host_mapping(). However that only affects the domain itself, and is no worse than what a grant-mapping domain can already do to the grant source. Note that p2m_add_page() already refuses to replace p2m entries of special types. Fixes: b37d30c8fc52 ("Mapping grant references into HVM guests, take 2") Fixes: 0537d246f8db ("mm: add 'is_special_page' inline function") Signed-off-by: Roger Pau Monn=C3=A9 Reviewed-by: Jan Beulich --- xen/arch/x86/mm/p2m.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c index 52cadd9b25c6..7d95faac62c7 100644 --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -1953,6 +1953,13 @@ static int p2m_add_foreign(struct domain *tdom, unsi= gned long fgfn, =20 /* Remove previously mapped page if it is present. */ prev_mfn =3D get_gfn(tdom, gpfn, &p2mt_prev); + /* Special p2m slots must be cleaned up via special accessors. */ + if ( p2m_is_special(p2mt_prev) ) + { + rc =3D -EPERM; + goto put_both; + } + if ( mfn_valid(prev_mfn) ) { if ( is_special_page(mfn_to_page(prev_mfn)) ) @@ -2074,8 +2081,11 @@ int xenmem_add_to_physmap_one( goto put_all; } =20 - /* Remove previously mapped page if it was present. */ - if ( p2mt =3D=3D p2m_mmio_direct ) + /* + * Remove previously mapped page if it was present. Special p2m slots= must + * be cleaned up via special accessors. + */ + if ( p2m_is_special(p2mt) ) rc =3D -EPERM; else if ( mfn_valid(prev_mfn) ) { --=20 2.55.0