Skip to contentSkip to conversation

Xen Security Advisory 378 v3 (CVE-2021-28694,CVE-2021-28695,CVE-2021-28696) - IOMMU page mapping issues on x86

PATCH Xen.org security team 4 participants 4 messages Sep 1, 2021

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Xen Security Advisory CVE-2021-28694,CVE-2021-28695,CVE-2021-28696 / XSA-378
version 3

IOMMU page mapping issues on x86

UPDATES IN VERSION 3
====================

Warn about dom0=pvh breakage in Resolution section.

ISSUE DESCRIPTION
=================

Both AMD and Intel allow ACPI tables to specify regions of memory which should be left untranslated, which typically means these addresses should pass the translation phase unaltered. While these are typically device specific ACPI properties, they can also be specified to apply to a range of devices, or even all devices.

On all systems with such regions Xen failed to prevent guests from undoing/replacing such mappings (CVE-2021-28694).

On AMD systems, where a discontinuous range is specified by firmware, the supposedly-excluded middle range will also be identity-mapped (CVE-2021-28695).

Further, on AMD systems, upon de-assigment of a physical device from a guest, the identity mappings would be left in place, allowing a guest continued access to ranges of memory which it shouldn't have access to anymore (CVE-2021-28696).

IMPACT
======

The precise impact is system specific, but can - on affected systems - be any or all of privilege escalation, denial of service, or information leaks.

VULNERABLE SYSTEMS
==================

The vulnerability is only exploitable by guests granted access to physical devices (ie, via PCI passthrough).

All versions of Xen are affected.

Only x86 systems with IOMMUs and with firmware specifying memory regions to be identity mapped are affected. Other x86 systems are not affected.

Whether a particular system whose ACPI tables declare such memory region(s) is actually affected cannot be known without knowing when and/or how these regions are used. For example, if these regions were used only during system boot, there would not be any vulnerability. The necessary knowledge can only be obtained from, collectively, the hardware and firmware manufacturers.

On Arm hardware IOMMU use is not security supported. Accordingly, we have not undertaken an analysis of these issues for Arm systems.

MITIGATION
==========

Not permitting untrusted guests access to phsyical devices will avoid the vulnerability.

Likewise, limiting untrusted guest access to physical devices whose firmware-provided ACPI tables declare identity mappings, will avoid the vulnerability. (Provided that there are no identity mapped regions which are specified by the ACPI tables to apply globally.)

Note that a system is still vulnerable if a guest was trusted, while it had such a device assigned, and then has the device removed in anticipation of the guest becoming untrusted (because of, for example, the insertion of an untrusted kernel module),

CREDITS
=======

This issue was discovered by Jan Beulich of SUSE.

RESOLUTION
==========

Applying the appropriate set of attached patches resolves this issue.

However, these patches are known to badly break PVH dom0 support. Work is ongoing to resolve the problem, and will be committed to the appropriate Xen trees (including backports) when ready. PVH dom0 has "Technical Preview" status so we do not intend to distribute the fixes via an update to this advisory

Note that patches for released versions are generally prepared to apply to the stable branches, and may not apply cleanly to the most recent release tarball. Downstreams are encouraged to update to the tip of the stable branch before applying these patches.

xsa378/xsa378-?.patch           xen-unstable
xsa378/xsa378-4.15-?.patch      Xen 4.15.x
xsa378/xsa378-4.14-?.patch      Xen 4.14.x
xsa378/xsa378-4.13-*.patch      Xen 4.13.x
xsa378/xsa378-4.12-*.patch      Xen 4.12.x
xsa378/xsa378-4.11-*.patch      Xen 4.11.x
$ sha256sum xsa378* xsa378*/*
b3b49681468bd2be4b95fc1d3861493bbea63bac1c115a12ece76d0313b6a81c  xsa378.meta
2560d4418e6b6d022b4e7fe1f84906ca13b3180537e746ee81cd3f97f0e86c35  xsa378/xsa378-1.patch
134b207c38f8d76bbc218220f1e61f82780e8166818ef51217716023cc27ce3c  xsa378/xsa378-2.patch
c166ef60e08a80440f79d92abe8488b123c8017e7633dbddacb95b20899f9b2b  xsa378/xsa378-3.patch
a2677e157724f57f65165f361a309c417d123ff009168f16d3a0e342b7a601eb  xsa378/xsa378-4.11-0a.patch
832f52f420bb47784bdefd75edd1ca521658327aafb94b4f3586da522d0d19c1  xsa378/xsa378-4.11-0b.patch
9eb7c53892f01424eec6b788f879b36dd75a6f89b1c8b9c973237603525d6546  xsa378/xsa378-4.11-0c.patch
48021fb9a2c52d7939fd11bbba80c67a5a5b6cae2039b4b2a2c6203eb5841260  xsa378/xsa378-4.11-1.patch
4f3fd39aa328c1e397aa2a10b92e98cc341908a8834302a4fc0f230876dd3570  xsa378/xsa378-4.11-2.patch
6e2eeecd2ccc4029f80888263f44b80acf078d69aeffda4d6accd8b2d0899182  xsa378/xsa378-4.11-3.patch
194b27510c8765f1cf8abee57624a075ab26f4f889aa8c201e126643f570f55a  xsa378/xsa378-4.11-4.patch
e8f4eab0984a7db2ca0c6c9ac0fb9107e45ddd572a36454db4a1bdb8b9a8b0c6  xsa378/xsa378-4.11-5.patch
943c7556be47b1a0f2273cf5691fe70dc65e6cb50ff0477d1d61eb3f0ba87a97  xsa378/xsa378-4.11-6.patch
2eb572e1a55caa4aca31f74a3844135a204c4e023a6776d7adc4f4043663fb99  xsa378/xsa378-4.11-7.patch
7c24ba33461d3adcb195a6611df3e9c0501e9dcacf9b8811456450b291d23edf  xsa378/xsa378-4.11-8.patch
fe14ea6699df673b787f93ff821838f2280407674df0c8c06d40efe8320e8748  xsa378/xsa378-4.12-0a.patch
1ba10aef9ea99c4455a34e61792ca65ea6b2ece56f05cd0b4adc14600a7ae346  xsa378/xsa378-4.12-0b.patch
2c2dbc0c18b695c1d2c93e4137228f82b6d21d895c3165a9e29ddea3db78e36a  xsa378/xsa378-4.12-0c.patch
48021fb9a2c52d7939fd11bbba80c67a5a5b6cae2039b4b2a2c6203eb5841260  xsa378/xsa378-4.12-1.patch
f0d62506ccdf081d0efbb553e2d33f17a084272d21f43bf95d11fda2dcc8a3fa  xsa378/xsa378-4.12-2.patch
e266512b18fc30e5bd4884cd720aa81644d3ca3323b38fe1f77d06fa98dd515d  xsa378/xsa378-4.12-3.patch
076f9955593a8ebdea5f24ed302b8a1004bbf50da4c8becf1f93764066b8981c  xsa378/xsa378-4.12-4.patch
9639bc35636ffae4e2b6dd026387347960c1fb986cb2924a18314a72e6b6ec0a  xsa378/xsa378-4.12-5.patch
32dd659aa365d9f8197d99b9334c22b33e9ce805e30376457df6e507f92282c8  xsa378/xsa378-4.12-6.patch
7eaae2fa968eb11e27277141456cc1bd657025aee738221c368e153535c7c0f4  xsa378/xsa378-4.12-7.patch
05982f43f35b580ff41f74b1280e469e0dd20176f184ec04a4874303c2aa3ad1  xsa378/xsa378-4.12-8.patch
c6f551ef9903a343b47692b34a63c70165a2dbf74878a6be6511cdffd55a7e8c  xsa378/xsa378-4.13-0a.patch
223cd63f7e1c39d862b8654da698455ded65ecb5abae0f57c330921522b7fdb4  xsa378/xsa378-4.13-0b.patch
5c33aa24f14e779dfe914e809cff11260083169a3adfb07a31ce11243d80b3ef  xsa378/xsa378-4.13-0c.patch
1d55426ff6a41f0ef4cbd2c943edafff394157703bb0b6ae751564abf93b5ee7  xsa378/xsa378-4.13-1.patch
4d5e7d5e65cd28d6bc7d1a9f2ab24f09dfaef295c4199d5f2db00915dcaa174f  xsa378/xsa378-4.13-2.patch
78df0bcb347f8bb45827f74b191aad36b6e907eb38c6d535035f2b2739645551  xsa378/xsa378-4.13-3.patch
237c33e0ae01a23db01721afb8e6a39101bfe081f8b75dbcff6b9fa9c9aaceda  xsa378/xsa378-4.13-4.patch
7d2f3ae3881d28073be54a6dfb35f13004e4efee742952788430201d86307ecf  xsa378/xsa378-4.13-5.patch
2ecd7580394667db0c41e4819025393e59ae24d6c97d54451c8e683585057367  xsa378/xsa378-4.13-6.patch
592a03d00e5d22d7a3c001681968dc469c70b3e57998b95877388e9528904ea2  xsa378/xsa378-4.13-7.patch
54e6a095f706c66dbbd74e39aa1d88031c9b537589e73ceb2925e2f0cc1854f0  xsa378/xsa378-4.13-8.patch
1d55426ff6a41f0ef4cbd2c943edafff394157703bb0b6ae751564abf93b5ee7  xsa378/xsa378-4.14-1.patch
86fbd88eb8a358575e42cf335c444b047ddb4d2f1c6a1bc6f9e57e6ac0041074  xsa378/xsa378-4.14-2.patch
0a23e5f93ff1bb55f003a56e0ef8c531384b164f0e840f5794acdb9ae3e91996  xsa378/xsa378-4.14-3.patch
e2226f7ddae1d24dbee8cf19efa8d67ebf312f3d10641cb9aec21d68a3c8f818  xsa378/xsa378-4.14-4.patch
f2cf8f7e4aa0460e606b5564adde366332ed323c4d5e3f957e64299ef1bc9baf  xsa378/xsa378-4.14-5.patch
7811426975757e3bb8c6ab3161ba2354e1780b55b9e6be7928229d5f23bf79b6  xsa378/xsa378-4.14-6.patch
58147bd6c0ea4e08e84a17afc796be4bbe53e6fbc1d393f9fe3c6191fd33eba5  xsa378/xsa378-4.14-7.patch
682a011d807a7c284faf0ec9d2cf0aabaddbc658979dea2b9ccbc007b660f9c5  xsa378/xsa378-4.14-8.patch
a00ada0cd673f0909cc7b462cb532dbd6fe17601e06bd84272f5ff1857ea4c73  xsa378/xsa378-4.15-1.patch
d74cc325be1e47d61ba3b1400837af35d044bf1d25806aa98926ec262f80bddd  xsa378/xsa378-4.15-2.patch
2ad685a04dbbdd2c81761b58146e70059b8a8a92b0c1176f36933510293ece5b  xsa378/xsa378-4.15-3.patch
18de9facccd70ce49dd839e219fe71667c43110e474e5d7b56a503a5786fc7e0  xsa378/xsa378-4.15-4.patch
296558b27ba82176f6d06b721102c8ba7c7e6e99d29b29392ea82244e88df0b9  xsa378/xsa378-4.15-5.patch
5d7cc84c66daf0aceab9407fa72f2827024c847f4ca10f8d123eee87b7451aba  xsa378/xsa378-4.15-6.patch
fd4aa4447562230a6684d17e6e4c55f1e48df4773247f66ab9d01181003bc9aa  xsa378/xsa378-4.15-7.patch
5305e3bc513bcd3e016ca3bbecc6cae38c8ed2b2eacb13e82f7b1f4401d3b67d  xsa378/xsa378-4.15-8.patch
78390bf59344ea5dcbfa5831db3634b3f1b3aabf029160c72cdbeeec2b46b2a9  xsa378/xsa378-4.patch
76fb4a1f2604b98d3a803744ad212b3984117ec8c8011ad6db3759f9337a9b5a  xsa378/xsa378-5.patch
a627f8c6b7d2ffac0b6de945189f96b718aeab8e0c8bb11476a40585d6411bd7  xsa378/xsa378-6.patch
28814b51fabd3c5cb3ca249ad291781f589cddd55fc8152fdd5668c5fcdc727c  xsa378/xsa378-7.patch
30e31749ade75fd5ab4c41fa27fe2124bdcc602ccc803a85e5eeec1b9c48a9c1  xsa378/xsa378-8.patch
$

DEPLOYMENT DURING EMBARGO
=========================

Deployment of the patches and/or mitigations described above (or others which are substantially similar) is permitted during the embargo, even on public-facing systems with untrusted guest users and administrators.

But: Distribution of updated software is prohibited (except to other members of the predisclosure list).

Predisclosure list members who wish to deploy significantly different patches and/or mitigations, please contact the Xen Project Security Team.

(Note: this during-embargo deployment notice is retained in post-embargo publicly released Xen Project advisories, even though it is then no longer applicable. This is to enable the community to have oversight of the Xen Project Security Team's decisionmaking.)

For more information about permissible uses of embargoed information, consult the Xen Project community's agreed Security Policy:
http://www.xenproject.org/security-policy.html
-----BEGIN PGP SIGNATURE-----

iQFABAEBCAAqFiEEI+MiLBRfRHX6gGCng/4UyVfoK9kFAmEvSCUMHHBncEB4ZW4u b3JnAAoJEIP+FMlX6CvZrUYH/iYMkjMzUpv5ik/quZ+z34uXwq/mD8x8ROZKXDev tgojXcFo0vNlcr71R0J5KrE0YeX+3MjbjqbF7A30cnw4/bgmvhzcdP/fwOfIKFYE JqaGoDaxFRpsIMvHFXV6OxtjgBgleUukaikUrcwWUG+L90KMmjqZU4IAlNcBAALC J6rDB8nHqD1s+ODIPdPE149jIE7LJfVqyuu9h4r/jmLEDRJBS2YEs6zgClqnIApl 3WBpZNhp8Tk3BFtbQcu4Uh9c0itymtp+8VsY0xzF2bWsE4aoe5DKHv0DyTSLe9Md pMG0egH7ZrI3MV4nzHdbhkA2Hn3X0rsJiI7TXpmmQPmgi4g=
=A/oT
-----END PGP SIGNATURE-----

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct global exclusion range extending

Besides unity mapping regions, the AMD IOMMU spec also provides for exclusion ranges (areas of memory not to be subject to DMA translation) to be specified by firmware in the ACPI tables. The spec does not put any constraints on the number of such regions.

Blindly assuming all addresses between any two such ranges should also be excluded can't be right. Since hardware has room for just a single such range (comprised of the Exclusion Base Register and the Exclusion Range Limit Register), combine only adjacent or overlapping regions (for now; this may require further adjustment in case table entries aren't sorted by address) with matching exclusion_allow_all settings. This requires bubbling up error indicators, such that IOMMU init can be failed when concatenation wasn't possible.

Furthermore, since the exclusion range specified in IOMMU registers implies R/W access, reject requests asking for less permissions (this will be brought closer to the spec by a subsequent change).

This is part of XSA-378 / CVE-2021-28695.

---
v8: Add comments to plain true/false function arguments.
v4: Don't accept requests not allowing for both reading and writing.
Don't accept mismatches in the exclusion_allow_all setting. Cover for the dropping of "correct limit calculation in IVMD parsing".
v2: Accept overlapping regions as well.
---
TBD: Should we assume random order of entries right away?
TBD: Andrew suggests there are indications of only one global exclusion
range being allowed, which would then (presumably) be tied to ACPI_IVMD_EXCLUSION_RANGE (as per above).

xen/drivers/passthrough/amd/iommu_acpi.c +30 -15
@@ -116,12 +116,21 @@ static struct amd_iommu * __init find_io
     return NULL;
 }
 
-static void __init reserve_iommu_exclusion_range(
-    struct amd_iommu *iommu, uint64_t base, uint64_t limit)
+static int __init reserve_iommu_exclusion_range(
+    struct amd_iommu *iommu, uint64_t base, uint64_t limit,
+    bool all, bool iw, bool ir)
 {
+    if ( !ir || !iw )
+        return -EPERM;
+
     /* need to extend exclusion range? */
     if ( iommu->exclusion_enable )
     {
+        if ( iommu->exclusion_limit + PAGE_SIZE < base ||
+             limit + PAGE_SIZE < iommu->exclusion_base ||
+             iommu->exclusion_allow_all != all )
+            return -EBUSY;
+
         if ( iommu->exclusion_base < base )
             base = iommu->exclusion_base;
         if ( iommu->exclusion_limit > limit )
@@ -129,16 +138,11 @@ static void __init reserve_iommu_exclusi
     }
 
     iommu->exclusion_enable = IOMMU_CONTROL_ENABLED;
+    iommu->exclusion_allow_all = all;
     iommu->exclusion_base = base;
     iommu->exclusion_limit = limit;
-}
 
-static void __init reserve_iommu_exclusion_range_all(
-    struct amd_iommu *iommu,
-    unsigned long base, unsigned long limit)
-{
-    reserve_iommu_exclusion_range(iommu, base, limit);
-    iommu->exclusion_allow_all = IOMMU_CONTROL_ENABLED;
+    return 0;
 }
 
 static void __init reserve_unity_map_for_device(
@@ -176,6 +180,7 @@ static int __init register_exclusion_ran
     unsigned long range_top, iommu_top, length;
     struct amd_iommu *iommu;
     unsigned int bdf;
+    int rc = 0;
 
     /* is part of exclusion range inside of IOMMU virtual address space? */
     /* note: 'limit' parameter is assumed to be page-aligned */
@@ -197,10 +202,15 @@ static int __init register_exclusion_ran
     if ( limit >= iommu_top )
     {
         for_each_amd_iommu( iommu )
-            reserve_iommu_exclusion_range_all(iommu, base, limit);
+        {
+            rc = reserve_iommu_exclusion_range(iommu, base, limit,
+                                               true /* all */, iw, ir);
+            if ( rc )
+                break;
+        }
     }
 
-    return 0;
+    return rc;
 }
 
 static int __init register_exclusion_range_for_device(
@@ -211,6 +221,7 @@ static int __init register_exclusion_ran
     unsigned long range_top, iommu_top, length;
     struct amd_iommu *iommu;
     u16 req;
+    int rc = 0;
 
     iommu = find_iommu_for_device(seg, bdf);
     if ( !iommu )
@@ -240,12 +251,13 @@ static int __init register_exclusion_ran
     /* register IOMMU exclusion range settings for device */
     if ( limit >= iommu_top  )
     {
-        reserve_iommu_exclusion_range(iommu, base, limit);
+        rc = reserve_iommu_exclusion_range(iommu, base, limit,
+                                           false /* all */, iw, ir);
         ivrs_mappings[bdf].dte_allow_exclusion = true;
         ivrs_mappings[req].dte_allow_exclusion = true;
     }
 
-    return 0;
+    return rc;
 }
 
 static int __init register_exclusion_range_for_iommu_devices(
@@ -255,6 +267,7 @@ static int __init register_exclusion_ran
     unsigned long range_top, iommu_top, length;
     unsigned int bdf;
     u16 req;
+    int rc = 0;
 
     /* is part of exclusion range inside of IOMMU virtual address space? */
     /* note: 'limit' parameter is assumed to be page-aligned */
@@ -285,8 +298,10 @@ static int __init register_exclusion_ran
 
     /* register IOMMU exclusion range settings */
     if ( limit >= iommu_top )
-        reserve_iommu_exclusion_range_all(iommu, base, limit);
-    return 0;
+        rc = reserve_iommu_exclusion_range(iommu, base, limit,
+                                           true /* all */, iw, ir);
+
+    return rc;
 }
 
 static int __init parse_ivmd_device_select(

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct device unity map handling

Blindly assuming all addresses between any two such ranges, specified by firmware in the ACPI tables, should also be unity-mapped can't be right. Nor can it be correct to merge ranges with differing permissions. Track ranges individually; don't merge at all, but check for overlaps instead. This requires bubbling up error indicators, such that IOMMU init can be failed when allocation of a new tracking struct wasn't possible, or an overlap was detected.

At this occasion also stop ignoring amd_iommu_reserve_domain_unity_map()'s return value.

This is part of XSA-378 / CVE-2021-28695.

---
TBD: Overlapping ranges could be dealt with (when they specify the same
permissions) by splitting the ranges to non-overlapping pieces. See "IOMMU: generalize VT-d's tracking of mapped RMRR regions" and its user "AMD/IOMMU: re-arrange/complete re-assignment handling" for why overlaps can't be accepted with the chosen tracking model. But the base assumption is anyway for firmware to not surface overlapping ranges in the first place.
---
v8: Fix build (failure resulted from a piece of code living here which
only a later patch should add). Re-base over adjustments to earlier patch.
v5: Don't allow overlaps (except exact matches) and don't merge.
Re-base.
v4: Adjust title. Re-base over changes earlier in the series.
v2: Re-base over XSA-302.

xen/drivers/passthrough/amd/iommu.h +9 -5
@@ -107,20 +107,24 @@ struct amd_iommu {
     struct list_head ats_devices;
 };
 
+struct ivrs_unity_map {
+    bool read:1;
+    bool write:1;
+    paddr_t addr;
+    unsigned long length;
+    struct ivrs_unity_map *next;
+};
+
 struct ivrs_mappings {
     uint16_t dte_requestor_id;
     bool valid:1;
     bool dte_allow_exclusion:1;
-    bool unity_map_enable:1;
-    bool write_permission:1;
-    bool read_permission:1;
 
     /* ivhd device data settings */
     uint8_t device_flags;
 
-    unsigned long addr_range_start;
-    unsigned long addr_range_length;
     struct amd_iommu *iommu;
+    struct ivrs_unity_map *unity_map;
 
     /* per device interrupt remapping table */
     void *intremap_table;
xen/drivers/passthrough/amd/iommu_acpi.c +50 -34

132 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +9 -7
@@ -384,15 +384,17 @@ static int amd_iommu_assign_device(struc
     struct ivrs_mappings *ivrs_mappings = get_ivrs_mappings(pdev->seg);
     int bdf = PCI_BDF2(pdev->bus, devfn);
     int req_id = get_dma_requestor_id(pdev->seg, bdf);
+    const struct ivrs_unity_map *unity_map;
 
-    if ( ivrs_mappings[req_id].unity_map_enable )
+    for ( unity_map = ivrs_mappings[req_id].unity_map; unity_map;
+          unity_map = unity_map->next )
     {
-        amd_iommu_reserve_domain_unity_map(
-            d,
-            ivrs_mappings[req_id].addr_range_start,
-            ivrs_mappings[req_id].addr_range_length,
-            ivrs_mappings[req_id].write_permission,
-            ivrs_mappings[req_id].read_permission);
+        int rc = amd_iommu_reserve_domain_unity_map(
+                     d, unity_map->addr, unity_map->length,
+                     unity_map->write, unity_map->read);
+
+        if ( rc )
+            return rc;
     }
 
     return reassign_device(pdev->domain, d, devfn, pdev);

From: Jan Beulich <[email protected]>
Subject: IOMMU: also pass p2m_access_t to p2m_get_iommu_flags()

A subsequent change will want to customize the IOMMU permissions based on this.

This is part of XSA-378.

---
v5: New.

xen/arch/x86/mm/p2m-ept.c +3 -3
@@ -808,7 +808,7 @@ ept_set_entry(struct p2m_domain *p2m, gf
     bool_t entry_written = 0;
     bool_t need_modify_vtd_table = 1;
     bool_t vtd_pte_present = 0;
-    unsigned int iommu_flags = p2m_get_iommu_flags(p2mt, mfn);
+    unsigned int iommu_flags = p2m_get_iommu_flags(p2mt, p2ma, mfn);
     bool_t needs_sync = 1;
     ept_entry_t old_entry = { .epte = 0 };
     ept_entry_t new_entry = { .epte = 0 };
@@ -938,8 +938,8 @@ ept_set_entry(struct p2m_domain *p2m, gf
 
         /* Safe to read-then-write because we hold the p2m lock */
         if ( ept_entry->mfn == new_entry.mfn &&
-             p2m_get_iommu_flags(ept_entry->sa_p2mt, _mfn(ept_entry->mfn)) ==
-             iommu_flags )
+             p2m_get_iommu_flags(ept_entry->sa_p2mt, ept_entry->access,
+                                 _mfn(ept_entry->mfn)) == iommu_flags )
             need_modify_vtd_table = 0;
 
         ept_p2m_type_to_flags(p2m, &new_entry);
xen/arch/x86/mm/p2m-pt.c +16 -3
@@ -545,6 +545,16 @@ int p2m_pt_handle_deferred_changes(uint6
     return rc;
 }
 
+/* Reconstruct a fake p2m_access_t from stored PTE flags. */
+static p2m_access_t p2m_flags_to_access(unsigned int flags)
+{
+    if ( flags & _PAGE_PRESENT )
+        return p2m_access_n;
+
+    /* No need to look at _PAGE_NX for now. */
+    return flags & _PAGE_RW ? p2m_access_rw : p2m_access_r;
+}
+
 /* Checks only applicable to entries with order > PAGE_ORDER_4K */
 static void check_entry(mfn_t mfn, p2m_type_t new, p2m_type_t old,
                         unsigned int order)
@@ -579,7 +589,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
     l2_pgentry_t l2e_content;
     l3_pgentry_t l3e_content;
     int rc;
-    unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt, mfn);
+    unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt, p2ma, mfn);
     /*
      * old_mfn and iommu_old_flags control possible flush/update needs on the
      * IOMMU: We need to flush when MFN or flags (i.e. permissions) change.
@@ -642,6 +652,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                 old_mfn = l1e_get_pfn(*p2m_entry);
                 iommu_old_flags =
                     p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                        p2m_flags_to_access(flags),
                                         _mfn(old_mfn));
             }
             else
@@ -684,9 +695,10 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                                    0, L1_PAGETABLE_ENTRIES);
         ASSERT(p2m_entry);
         old_mfn = l1e_get_pfn(*p2m_entry);
+        flags = l1e_get_flags(*p2m_entry);
         iommu_old_flags =
-            p2m_get_iommu_flags(p2m_flags_to_type(l1e_get_flags(*p2m_entry)),
-                                _mfn(old_mfn));
+            p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                p2m_flags_to_access(flags), _mfn(old_mfn));
 
         if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) )
             entry_content = p2m_l1e_from_pfn(mfn_x(mfn),
@@ -714,6 +726,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                 old_mfn = l1e_get_pfn(*p2m_entry);
                 iommu_old_flags =
                     p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                        p2m_flags_to_access(flags),
                                         _mfn(old_mfn));
             }
             else
xen/include/asm-x86/p2m.h +2 -1
@@ -891,7 +891,8 @@ static inline void p2m_altp2m_check(stru
 /*
  * p2m type to IOMMU flags
  */
-static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt, mfn_t mfn)
+static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt,
+                                               p2m_access_t p2ma, mfn_t mfn)
 {
     unsigned int flags;
 

From: Jan Beulich <[email protected]>
Subject: x86/p2m: fix PoD accounting in guest_physmap_add_entry()

The initial observation was that the mfn_valid() check comes too late:
Neither mfn_add() nor mfn_to_page() (let alone de-referencing the result of the latter) are valid for MFNs failing this check. Move it up and - noticing that there's no caller doing so - also add an assertion that this should never produce "false" here.

In turn this would have meant that the "else" to that if() could now go away, which didn't seem right at all. And indeed, considering callers like memory_exchange() or various grant table functions, the PoD accounting should have been outside of that if() from the very beginning.

xen/arch/x86/mm/p2m.c +12 -20
@@ -794,6 +794,12 @@ guest_physmap_add_entry(struct domain *d
     if ( p2m_is_foreign(t) )
         return -EINVAL;
 
+    if ( !mfn_valid(mfn) )
+    {
+        ASSERT_UNREACHABLE();
+        return -EINVAL;
+    }
+
     p2m_lock(p2m);
 
     P2M_DEBUG("adding gfn=%#lx mfn=%#lx\n", gfn_x(gfn), mfn_x(mfn));
@@ -894,12 +900,13 @@ guest_physmap_add_entry(struct domain *d
     }
 
     /* Now, actually do the two-way mapping */
-    if ( mfn_valid(mfn) )
+    rc = p2m_set_entry(p2m, gfn, mfn, page_order, t, p2m->default_access);
+    if ( rc == 0 )
     {
-        rc = p2m_set_entry(p2m, gfn, mfn, page_order, t,
-                           p2m->default_access);
-        if ( rc )
-            goto out; /* Failed to update p2m, bail without updating m2p. */
+        pod_lock(p2m);
+        p2m->pod.entry_count -= pod_count;
+        BUG_ON(p2m->pod.entry_count < 0);
+        pod_unlock(p2m);
 
         if ( !p2m_is_grant(t) )
         {
@@ -908,22 +915,7 @@ guest_physmap_add_entry(struct domain *d
                                   gfn_x(gfn_add(gfn, i)));
         }
     }
-    else
-    {
-        gdprintk(XENLOG_WARNING, "Adding bad mfn to p2m map (%#lx -> %#lx)\n",
-                 gfn_x(gfn), mfn_x(mfn));
-        rc = p2m_set_entry(p2m, gfn, INVALID_MFN, page_order,
-                           p2m_invalid, p2m->default_access);
-        if ( rc == 0 )
-        {
-            pod_lock(p2m);
-            p2m->pod.entry_count -= pod_count;
-            BUG_ON(p2m->pod.entry_count < 0);
-            pod_unlock(p2m);
-        }
-    }
 
-out:
     p2m_unlock(p2m);
 
     return rc;

From: Jan Beulich <[email protected]>
Subject: x86/p2m: don't ignore p2m_remove_page()'s return value

It's not very nice to return from guest_physmap_add_entry() after perhaps already having made some changes to the P2M, but this is pre- existing practice in the function, and imo better than ignoring errors.

Take the liberty and replace an mfn_add() instance with a local variable already holding the result (as proven by the check immediately ahead).

xen/arch/x86/mm/p2m.c +8 -8
@@ -702,8 +702,7 @@ void p2m_final_teardown(struct domain *d
     p2m_teardown_hostp2m(d);
 }
 
-
-static int
+static int __must_check
 p2m_remove_page(struct p2m_domain *p2m, unsigned long gfn_l, unsigned long mfn,
                 unsigned int page_order)
 {
@@ -892,9 +891,9 @@ guest_physmap_add_entry(struct domain *d
                 ASSERT(mfn_valid(omfn));
                 P2M_DEBUG("old gfn=%#lx -> mfn %#lx\n",
                           gfn_x(ogfn) , mfn_x(omfn));
-                if ( mfn_eq(omfn, mfn_add(mfn, i)) )
-                    p2m_remove_page(p2m, gfn_x(ogfn), mfn_x(mfn_add(mfn, i)),
-                                    0);
+                if ( mfn_eq(omfn, mfn_add(mfn, i)) &&
+                     (rc = p2m_remove_page(p2m, gfn_x(ogfn), mfn_x(omfn), 0)) )
+                    goto out;
             }
         }
     }
@@ -916,6 +915,7 @@ guest_physmap_add_entry(struct domain *d
         }
     }
 
+ out:
     p2m_unlock(p2m);
 
     return rc;
@@ -2385,9 +2385,9 @@ int p2m_change_altp2m_gfn(struct domain
 
     if ( gfn_eq(new_gfn, INVALID_GFN) )
     {
-        if ( mfn_valid(mfn) )
-            p2m_remove_page(ap2m, gfn_x(old_gfn), mfn_x(mfn), PAGE_ORDER_4K);
-        rc = 0;
+        rc = mfn_valid(mfn)
+             ? p2m_remove_page(ap2m, gfn_x(old_gfn), mfn_x(mfn), PAGE_ORDER_4K)
+             : 0;
         goto out;
     }
 

From: Jan Beulich <[email protected]>
Subject: x86/p2m: don't assert that the passed in MFN matches for a remove

guest_physmap_remove_page() gets handed an MFN from the outside, yet takes the necessary lock to prevent further changes to the GFN <-> MFN mapping itself. While some callers, in particular guest_remove_page() (by way of having called get_gfn_query()), hold the GFN lock already, various others (most notably perhaps the 2nd instance in xenmem_add_to_physmap_one()) don't. While it also is an option to fix all the callers, deal with the issue in p2m_remove_page() instead:
Replace the ASSERT() by a conditional and split the loop into two, such that all checking gets done before any modification would occur.

xen/arch/x86/mm/p2m.c +14 -4
@@ -708,7 +708,6 @@ p2m_remove_page(struct p2m_domain *p2m,
 {
     unsigned long i;
     gfn_t gfn = _gfn(gfn_l);
-    mfn_t mfn_return;
     p2m_type_t t;
     p2m_access_t a;
 
@@ -719,15 +718,26 @@ p2m_remove_page(struct p2m_domain *p2m,
     ASSERT(gfn_locked_by_me(p2m, gfn));
     P2M_DEBUG("removing gfn=%#lx mfn=%#lx\n", gfn_l, mfn);
 
+    for ( i = 0; i < (1UL << page_order); )
+    {
+        unsigned int cur_order;
+        mfn_t mfn_return = p2m->get_entry(p2m, gfn_add(gfn, i), &t, &a, 0,
+                                          &cur_order, NULL);
+
+        if ( p2m_is_valid(t) &&
+             (!mfn_valid(_mfn(mfn)) || mfn + i != mfn_x(mfn_return)) )
+            return -EILSEQ;
+
+        i += (1UL << cur_order) - ((gfn_l + i) & ((1UL << cur_order) - 1));
+    }
+
     if ( mfn_valid(_mfn(mfn)) )
     {
         for ( i = 0; i < (1UL << page_order); i++ )
         {
-            mfn_return = p2m->get_entry(p2m, gfn_add(gfn, i), &t, &a, 0,
-                                        NULL, NULL);
+            p2m->get_entry(p2m, gfn_add(gfn, i), &t, &a, 0, NULL, NULL);
             if ( !p2m_is_grant(t) && !p2m_is_shared(t) && !p2m_is_foreign(t) )
                 set_gpfn_from_mfn(mfn+i, INVALID_M2P_ENTRY);
-            ASSERT( !p2m_is_valid(t) || mfn + i == mfn_x(mfn_return) );
         }
     }
     return p2m_set_entry(p2m, gfn, INVALID_MFN, page_order, p2m_invalid,

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct global exclusion range extending

Besides unity mapping regions, the AMD IOMMU spec also provides for exclusion ranges (areas of memory not to be subject to DMA translation) to be specified by firmware in the ACPI tables. The spec does not put any constraints on the number of such regions.

Blindly assuming all addresses between any two such ranges should also be excluded can't be right. Since hardware has room for just a single such range (comprised of the Exclusion Base Register and the Exclusion Range Limit Register), combine only adjacent or overlapping regions (for now; this may require further adjustment in case table entries aren't sorted by address) with matching exclusion_allow_all settings. This requires bubbling up error indicators, such that IOMMU init can be failed when concatenation wasn't possible.

Furthermore, since the exclusion range specified in IOMMU registers implies R/W access, reject requests asking for less permissions (this will be brought closer to the spec by a subsequent change).

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu_acpi.c +30 -15

106 lines not on this page · raw message

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct device unity map handling

Blindly assuming all addresses between any two such ranges, specified by firmware in the ACPI tables, should also be unity-mapped can't be right. Nor can it be correct to merge ranges with differing permissions. Track ranges individually; don't merge at all, but check for overlaps instead. This requires bubbling up error indicators, such that IOMMU init can be failed when allocation of a new tracking struct wasn't possible, or an overlap was detected.

At this occasion also stop ignoring amd_iommu_reserve_domain_unity_map()'s return value.

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu_acpi.c +50 -34

132 lines not on this page · raw message

xen/drivers/passthrough/amd/iommu_init.c +0 -1
@@ -1187,7 +1187,6 @@ static int __init alloc_ivrs_mappings(u1
     {
         ivrs_mappings[bdf].dte_requestor_id = bdf;
         ivrs_mappings[bdf].dte_allow_exclusion = IOMMU_CONTROL_DISABLED;
-        ivrs_mappings[bdf].unity_map_enable = IOMMU_CONTROL_DISABLED;
         ivrs_mappings[bdf].iommu = NULL;
 
         ivrs_mappings[bdf].intremap_table = NULL;
xen/drivers/passthrough/amd/pci_amd_iommu.c +9 -7
@@ -372,15 +372,17 @@ static int amd_iommu_assign_device(struc
     struct ivrs_mappings *ivrs_mappings = get_ivrs_mappings(pdev->seg);
     int bdf = PCI_BDF2(pdev->bus, devfn);
     int req_id = get_dma_requestor_id(pdev->seg, bdf);
+    const struct ivrs_unity_map *unity_map;
 
-    if ( ivrs_mappings[req_id].unity_map_enable )
+    for ( unity_map = ivrs_mappings[req_id].unity_map; unity_map;
+          unity_map = unity_map->next )
     {
-        amd_iommu_reserve_domain_unity_map(
-            d,
-            ivrs_mappings[req_id].addr_range_start,
-            ivrs_mappings[req_id].addr_range_length,
-            ivrs_mappings[req_id].write_permission,
-            ivrs_mappings[req_id].read_permission);
+        int rc = amd_iommu_reserve_domain_unity_map(
+                     d, unity_map->addr, unity_map->length,
+                     unity_map->write, unity_map->read);
+
+        if ( rc )
+            return rc;
     }
 
     return reassign_device(pdev->domain, d, devfn, pdev);
xen/include/asm-x86/amd-iommu.h +9 -5
@@ -108,15 +108,19 @@ struct amd_iommu {
     struct list_head ats_devices;
 };
 
+struct ivrs_unity_map {
+    bool read:1;
+    bool write:1;
+    paddr_t addr;
+    unsigned long length;
+    struct ivrs_unity_map *next;
+};
+
 struct ivrs_mappings {
     u16 dte_requestor_id;
     u8 dte_allow_exclusion;
-    u8 unity_map_enable;
-    u8 write_permission;
-    u8 read_permission;
-    unsigned long addr_range_start;
-    unsigned long addr_range_length;
     struct amd_iommu *iommu;
+    struct ivrs_unity_map *unity_map;
 
     /* per device interrupt remapping table */
     void *intremap_table;

From: Jan Beulich <[email protected]>
Subject: IOMMU: also pass p2m_access_t to p2m_get_iommu_flags()

A subsequent change will want to customize the IOMMU permissions based on this.

This is part of XSA-378.

xen/arch/x86/mm/p2m-ept.c +3 -3
@@ -711,7 +711,7 @@ ept_set_entry(struct p2m_domain *p2m, gf
     uint8_t ipat = 0;
     bool_t need_modify_vtd_table = 1;
     bool_t vtd_pte_present = 0;
-    unsigned int iommu_flags = p2m_get_iommu_flags(p2mt, mfn);
+    unsigned int iommu_flags = p2m_get_iommu_flags(p2mt, p2ma, mfn);
     bool_t needs_sync = 1;
     ept_entry_t old_entry = { .epte = 0 };
     ept_entry_t new_entry = { .epte = 0 };
@@ -837,8 +837,8 @@ ept_set_entry(struct p2m_domain *p2m, gf
 
         /* Safe to read-then-write because we hold the p2m lock */
         if ( ept_entry->mfn == new_entry.mfn &&
-             p2m_get_iommu_flags(ept_entry->sa_p2mt, _mfn(ept_entry->mfn)) ==
-             iommu_flags )
+             p2m_get_iommu_flags(ept_entry->sa_p2mt, ept_entry->access,
+                                 _mfn(ept_entry->mfn)) == iommu_flags )
             need_modify_vtd_table = 0;
 
         ept_p2m_type_to_flags(p2m, &new_entry, p2mt, p2ma);
xen/arch/x86/mm/p2m-pt.c +16 -3
@@ -471,6 +471,16 @@ int p2m_pt_handle_deferred_changes(uint6
     return rc;
 }
 
+/* Reconstruct a fake p2m_access_t from stored PTE flags. */
+static p2m_access_t p2m_flags_to_access(unsigned int flags)
+{
+    if ( flags & _PAGE_PRESENT )
+        return p2m_access_n;
+
+    /* No need to look at _PAGE_NX for now. */
+    return flags & _PAGE_RW ? p2m_access_rw : p2m_access_r;
+}
+
 /* Returns: 0 for success, -errno for failure */
 static int
 p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,
@@ -487,7 +497,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
     l2_pgentry_t l2e_content;
     l3_pgentry_t l3e_content;
     int rc;
-    unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt, mfn);
+    unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt, p2ma, mfn);
     /*
      * old_mfn and iommu_old_flags control possible flush/update needs on the
      * IOMMU: We need to flush when MFN or flags (i.e. permissions) change.
@@ -556,6 +566,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                 old_mfn = l1e_get_pfn(*p2m_entry);
                 iommu_old_flags =
                     p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                        p2m_flags_to_access(flags),
                                         _mfn(old_mfn));
             }
             else
@@ -602,9 +613,10 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                                    0, L1_PAGETABLE_ENTRIES);
         ASSERT(p2m_entry);
         old_mfn = l1e_get_pfn(*p2m_entry);
+        flags = l1e_get_flags(*p2m_entry);
         iommu_old_flags =
-            p2m_get_iommu_flags(p2m_flags_to_type(l1e_get_flags(*p2m_entry)),
-                                _mfn(old_mfn));
+            p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                p2m_flags_to_access(flags), _mfn(old_mfn));
 
         if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) )
             entry_content = p2m_l1e_from_pfn(mfn_x(mfn),
@@ -648,6 +660,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                 old_mfn = l1e_get_pfn(*p2m_entry);
                 iommu_old_flags =
                     p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                        p2m_flags_to_access(flags),
                                         _mfn(old_mfn));
             }
             else
xen/include/asm-x86/p2m.h +2 -1
@@ -839,7 +839,8 @@ int p2m_altp2m_propagate_change(struct d
 /*
  * p2m type to IOMMU flags
  */
-static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt, mfn_t mfn)
+static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt,
+                                               p2m_access_t p2ma, mfn_t mfn)
 {
     unsigned int flags;
 

From: Jan Beulich <[email protected]>
Subject: IOMMU: generalize VT-d's tracking of mapped RMRR regions

In order to re-use it elsewhere, move the logic to vendor independent code and strip it of RMRR specifics.

Note that the prior "map" parameter gets folded into the new "p2ma" one (which AMD IOMMU code will want to make use of), assigning alternative meaning ("unmap") to p2m_access_x. Prepare set_identity_p2m_entry() and p2m_get_iommu_flags() for getting passed access types other than p2m_access_rw (in the latter case just for p2m_mmio_direct requests).

Note also that, to be on the safe side, an overlap check gets added to the main loop of iommu_identity_mapping().

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +2 -1
@@ -1157,7 +1157,8 @@ int set_identity_p2m_entry(struct domain
     {
         if ( !need_iommu(d) )
             return 0;
-        return iommu_map_page(d, gfn_l, gfn_l, IOMMUF_readable|IOMMUF_writable);
+        return iommu_map_page(d, gfn_l, gfn_l,
+                              p2m_access_to_iommu_flags(p2ma));
     }
 
     gfn_lock(p2m, gfn, 0);
xen/drivers/passthrough/vtd/iommu.c +13 -85

150 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +94 -1

107 lines not on this page · raw message

xen/include/asm-x86/iommu.h +7 -1
@@ -16,6 +16,7 @@
 
 #include <xen/errno.h>
 #include <xen/list.h>
+#include <xen/mem_access.h>
 #include <xen/spinlock.h>
 #include <asm/processor.h>
 #include <asm/hvm/vmx/vmcs.h>
@@ -36,7 +37,7 @@ struct arch_iommu
     spinlock_t mapping_lock;            /* io page table lock */
     int agaw;     /* adjusted guest address width, 0 is level 2 30-bit */
     u64 iommu_bitmap;              /* bitmap of iommu(s) that the domain uses */
-    struct list_head mapped_rmrrs;
+    struct list_head identity_maps;
 
     /* amd iommu support */
     int paging_mode;
@@ -94,6 +95,11 @@ bool_t iommu_supports_eim(void);
 int iommu_enable_x2apic_IR(void);
 void iommu_disable_x2apic_IR(void);
 
+int iommu_identity_mapping(struct domain *d, p2m_access_t p2ma,
+                           paddr_t base, paddr_t end,
+                           unsigned int flag);
+void iommu_identity_map_teardown(struct domain *d);
+
 extern bool untrusted_msi;
 
 int pi_update_irte(const struct pi_desc *pi_desc, const struct pirq *pirq,
xen/include/asm-x86/mem_access.h +2 -4
@@ -44,10 +44,8 @@ bool p2m_mem_access_emulate_check(struct
                                   const vm_event_response_t *rsp);
 
 /* Sanity check for mem_access hardware support */
-static inline bool p2m_mem_access_sanity_check(struct domain *d)
-{
-    return is_hvm_domain(d) && cpu_has_vmx && hap_enabled(d);
-}
+#define p2m_mem_access_sanity_check(d) \
+    (is_hvm_domain(d) && cpu_has_vmx && hap_enabled(d))
 
 #endif /*__ASM_X86_MEM_ACCESS_H__ */
 
xen/include/asm-x86/p2m.h +32 -3
@@ -836,6 +836,34 @@ int p2m_altp2m_propagate_change(struct d
                                 mfn_t mfn, unsigned int page_order,
                                 p2m_type_t p2mt, p2m_access_t p2ma);
 
+/* p2m access to IOMMU flags */
+static inline unsigned int p2m_access_to_iommu_flags(p2m_access_t p2ma)
+{
+    switch ( p2ma )
+    {
+    case p2m_access_rw:
+    case p2m_access_rwx:
+        return IOMMUF_readable | IOMMUF_writable;
+
+    case p2m_access_r:
+    case p2m_access_rx:
+    case p2m_access_rx2rw:
+        return IOMMUF_readable;
+
+    case p2m_access_w:
+    case p2m_access_wx:
+        return IOMMUF_writable;
+
+    case p2m_access_n:
+    case p2m_access_x:
+    case p2m_access_n2rwx:
+        return 0;
+    }
+
+    ASSERT_UNREACHABLE();
+    return 0;
+}
+
 /*
  * p2m type to IOMMU flags
  */
@@ -857,9 +885,10 @@ static inline unsigned int p2m_get_iommu
         flags = IOMMUF_readable;
         break;
     case p2m_mmio_direct:
-        flags = IOMMUF_readable;
-        if ( !rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn)) )
-            flags |= IOMMUF_writable;
+        flags = p2m_access_to_iommu_flags(p2ma);
+        if ( (flags & IOMMUF_writable) &&
+             rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn)) )
+            flags &= ~IOMMUF_writable;
         break;
     default:
         flags = 0;

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange/complete re-assignment handling

Prior to the assignment step having completed successfully, devices should not get associated with their new owner. Hand the device to DomIO (perhaps temporarily), until after the de-assignment step has completed.

De-assignment of a device (from other than Dom0) as well as failure of reassign_device() during assignment should result in unity mappings getting torn down. This in turn requires switching to a refcounted mapping approach, as was already used by VT-d for its RMRRs, to prevent unmapping a region used by multiple devices.

This is CVE-2021-28696 / part of XSA-378.

xen/drivers/passthrough/amd/iommu_map.c +40 -18

67 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +42 -12

90 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +4 -2
@@ -60,8 +60,10 @@ int __must_check amd_iommu_unmap_page(st
 u64 amd_iommu_get_next_table_from_pte(u32 *entry);
 int __must_check amd_iommu_alloc_root(struct domain_iommu *hd);
 int amd_iommu_reserve_domain_unity_map(struct domain *domain,
-                                       u64 phys_addr, unsigned long size,
-                                       int iw, int ir);
+                                       const struct ivrs_unity_map *map,
+                                       unsigned int flag);
+int amd_iommu_reserve_domain_unity_unmap(struct domain *d,
+                                         const struct ivrs_unity_map *map);
 
 /* Share p2m table with iommu */
 void amd_iommu_share_p2m(struct domain *d);

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange exclusion range and unity map recording

The spec makes no provisions for OS behavior here to depend on the amount of RAM found on the system. While the spec may not sufficiently clearly distinguish both kinds of regions, they are surely meant to be separate things: Only regions with ACPI_IVMD_EXCLUSION_RANGE set should be candidates for putting in the exclusion range registers. (As there's only a single such pair of registers per IOMMU, secondary non-adjacent regions with the flag set already get converted to unity mapped regions.)

First of all, drop the dependency on max_page. With commit b4f042236ae0 ("AMD/IOMMU: Cease using a dynamic height for the IOMMU pagetables") the use of it here was stale anyway; it was bogus already before, as it didn't account for max_page getting increased later on. Simply try an exclusion range registration first, and if it fails (for being unsuitable or non-mergeable), register a unity mapping range.

With this various local variables become unnecessary and hence get dropped at the same time.

With the max_page boundary dropped for using unity maps, the minimum page table tree height now needs both recording and enforcing in amd_iommu_domain_init(). Since we can't predict which devices may get assigned to a domain, our only option is to uniformly force at least that height for all domains, now that the height isn't dynamic anymore.

Further don't make use of the exclusion range unless ACPI data says so.

Note that exclusion range registration in register_range_for_all_devices() is on a best effort basis. Hence unity map entries also registered are redundant when the former succeeded, but they also do no harm. Improvements in this area can be done later imo.

Also adjust types where suitable without touching extra lines.

This is part of XSA-378.

xen/drivers/passthrough/amd/iommu_acpi.c +82 -106

317 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +8 -4
@@ -218,6 +218,8 @@ static int __must_check allocate_domain_
     return rc;
 }
 
+int __read_mostly amd_iommu_min_paging_mode = 1;
+
 static int amd_iommu_domain_init(struct domain *d)
 {
     struct domain_iommu *hd = dom_iommu(d);
@@ -229,11 +231,13 @@ static int amd_iommu_domain_init(struct
      * - HVM could in principle use 3 or 4 depending on how much guest
      *   physical address space we give it, but this isn't known yet so use 4
      *   unilaterally.
+     * - Unity maps may require an even higher number.
      */
-    hd->arch.paging_mode = amd_iommu_get_paging_mode(
-        is_hvm_domain(d)
-        ? 1ul << (DEFAULT_DOMAIN_ADDRESS_WIDTH - PAGE_SHIFT)
-        : get_upper_mfn_bound() + 1);
+    hd->arch.paging_mode = max(amd_iommu_get_paging_mode(
+            is_hvm_domain(d)
+            ? 1ul << (DEFAULT_DOMAIN_ADDRESS_WIDTH - PAGE_SHIFT)
+            : get_upper_mfn_bound() + 1),
+        amd_iommu_min_paging_mode);
 
     return 0;
 }
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +2 -0
@@ -126,6 +126,8 @@ extern struct hpet_sbdf {
     } init;
 } hpet_sbdf;
 
+extern int amd_iommu_min_paging_mode;
+
 extern void *shared_intremap_table;
 extern unsigned long *shared_intremap_inuse;
 

From: Jan Beulich <[email protected]>
Subject: x86/p2m: introduce p2m_is_special()

Seeing the similarity of grant, foreign, and (subsequently) direct-MMIO handling, introduce a new P2M type group named "special" (as in "needing special accessors to create/destroy").

Also use -EPERM instead of other error codes on the two domain_crash() paths touched.

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +7 -8
@@ -736,7 +736,7 @@ p2m_remove_page(struct p2m_domain *p2m,
         for ( i = 0; i < (1UL << page_order); i++ )
         {
             p2m->get_entry(p2m, gfn_add(gfn, i), &t, &a, 0, NULL, NULL);
-            if ( !p2m_is_grant(t) && !p2m_is_shared(t) && !p2m_is_foreign(t) )
+            if ( !p2m_is_special(t) && !p2m_is_shared(t) )
                 set_gpfn_from_mfn(mfn+i, INVALID_M2P_ENTRY);
         }
     }
@@ -848,13 +848,13 @@ guest_physmap_add_entry(struct domain *d
                                   &ot, &a, 0, NULL, NULL);
             ASSERT(!p2m_is_shared(ot));
         }
-        if ( p2m_is_grant(ot) || p2m_is_foreign(ot) )
+        if ( p2m_is_special(ot) )
         {
-            /* Really shouldn't be unmapping grant/foreign maps this way */
+            /* Don't permit unmapping grant/foreign this way. */
             domain_crash(d);
             p2m_unlock(p2m);
             
-            return -EINVAL;
+            return -EPERM;
         }
         else if ( p2m_is_ram(ot) && !p2m_is_paged(ot) )
         {
@@ -947,8 +947,7 @@ int p2m_change_type_one(struct domain *d
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
     int rc;
 
-    BUG_ON(p2m_is_grant(ot) || p2m_is_grant(nt));
-    BUG_ON(p2m_is_foreign(ot) || p2m_is_foreign(nt));
+    BUG_ON(p2m_is_special(ot) || p2m_is_special(nt));
 
     gfn_lock(p2m, gfn, 0);
 
@@ -1091,11 +1090,11 @@ static int set_typed_p2m_entry(struct do
         gfn_unlock(p2m, gfn, order);
         return cur_order + 1;
     }
-    if ( p2m_is_grant(ot) || p2m_is_foreign(ot) )
+    if ( p2m_is_special(ot) )
     {
         gfn_unlock(p2m, gfn, order);
         domain_crash(d);
-        return -ENOENT;
+        return -EPERM;
     }
     else if ( p2m_is_ram(ot) )
     {
xen/include/asm-x86/p2m.h +5 -0
@@ -142,6 +142,10 @@ typedef unsigned int p2m_query_t;
                             | p2m_to_mask(p2m_ram_logdirty) )
 #define P2M_SHARED_TYPES   (p2m_to_mask(p2m_ram_shared))
 
+/* Types established/cleaned up via special accessors. */
+#define P2M_SPECIAL_TYPES (P2M_GRANT_TYPES | \
+                           p2m_to_mask(p2m_map_foreign))
+
 /* Valid types not necessarily associated with a (valid) MFN. */
 #define P2M_INVALID_MFN_TYPES (P2M_POD_TYPES                  \
                                | p2m_to_mask(p2m_mmio_direct) \
@@ -170,6 +174,7 @@ typedef unsigned int p2m_query_t;
 #define p2m_is_paged(_t)    (p2m_to_mask(_t) & P2M_PAGED_TYPES)
 #define p2m_is_sharable(_t) (p2m_to_mask(_t) & P2M_SHARABLE_TYPES)
 #define p2m_is_shared(_t)   (p2m_to_mask(_t) & P2M_SHARED_TYPES)
+#define p2m_is_special(_t)  (p2m_to_mask(_t) & P2M_SPECIAL_TYPES)
 #define p2m_is_broken(_t)   (p2m_to_mask(_t) & P2M_BROKEN_TYPES)
 #define p2m_is_foreign(_t)  (p2m_to_mask(_t) & p2m_to_mask(p2m_map_foreign))
 

From: Jan Beulich <[email protected]>
Subject: x86/p2m: guard (in particular) identity mapping entries

Such entries, created by set_identity_p2m_entry(), should only be destroyed by clear_identity_p2m_entry(). However, similarly, entries created by set_mmio_p2m_entry() should only be torn down by clear_mmio_p2m_entry(), so the logic gets based upon p2m_mmio_direct as the entry type (separation between "ordinary" and 1:1 mappings would require a further indicator to tell apart the two).

As to the guest_remove_page() change, commit 48dfb297a20a ("x86/PVH:
allow guest_remove_page to remove p2m_mmio_direct pages"), which introduced the call to clear_mmio_p2m_entry(), claimed this was done for hwdom only without this actually having been the case. However, this code shouldn't be there in the first place, as MMIO entries shouldn't be dropped this way. Avoid triggering the warning again that 48dfb297a20a silenced by an adjustment to xenmem_add_to_physmap_one() instead.

Note that guest_physmap_mark_populate_on_demand() gets tightened beyond the immediate purpose of this change.

Note also that I didn't inspect code which isn't security supported, e.g. sharing, paging, or altp2m.

This is CVE-2021-28694 / part of XSA-378.

xen/arch/x86/mm.c +3 -1
@@ -4783,7 +4783,9 @@ int xenmem_add_to_physmap_one(
 
     /* Remove previously mapped page if it was present. */
     prev_mfn = mfn_x(get_gfn(d, gfn_x(gpfn), &p2mt));
-    if ( mfn_valid(_mfn(prev_mfn)) )
+    if ( p2mt == p2m_mmio_direct )
+        rc = -EPERM;
+    else if ( mfn_valid(_mfn(prev_mfn)) )
     {
         if ( is_xen_heap_mfn(prev_mfn) )
             /* Xen heap frames are simply unhooked from this phys slot. */
xen/arch/x86/mm/p2m.c +6 -5
@@ -725,7 +725,8 @@ p2m_remove_page(struct p2m_domain *p2m,
                                           &cur_order, NULL);
 
         if ( p2m_is_valid(t) &&
-             (!mfn_valid(_mfn(mfn)) || mfn + i != mfn_x(mfn_return)) )
+             (!mfn_valid(_mfn(mfn)) || t == p2m_mmio_direct ||
+              mfn + i != mfn_x(mfn_return)) )
             return -EILSEQ;
 
         i += (1UL << cur_order) - ((gfn_l + i) & ((1UL << cur_order) - 1));
@@ -803,7 +804,7 @@ guest_physmap_add_entry(struct domain *d
     if ( p2m_is_foreign(t) )
         return -EINVAL;
 
-    if ( !mfn_valid(mfn) )
+    if ( !mfn_valid(mfn) || t == p2m_mmio_direct )
     {
         ASSERT_UNREACHABLE();
         return -EINVAL;
@@ -850,7 +851,7 @@ guest_physmap_add_entry(struct domain *d
         }
         if ( p2m_is_special(ot) )
         {
-            /* Don't permit unmapping grant/foreign this way. */
+            /* Don't permit unmapping grant/foreign/direct-MMIO this way. */
             domain_crash(d);
             p2m_unlock(p2m);
             
@@ -1192,8 +1193,8 @@ int set_identity_p2m_entry(struct domain
  *    order+1  for caller to retry with order (guaranteed smaller than
  *             the order value passed in)
  */
-int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn_l, mfn_t mfn,
-                         unsigned int order)
+static int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn_l,
+                                mfn_t mfn, unsigned int order)
 {
     int rc = -EINVAL;
     gfn_t gfn = _gfn(gfn_l);
xen/arch/x86/mm/p2m-pod.c +6 -6
@@ -1302,17 +1302,17 @@ guest_physmap_mark_populate_on_demand(st
 
         p2m->get_entry(p2m, gfn_add(gfn, i), &ot, &a, 0, &cur_order, NULL);
         n = 1UL << min(order, cur_order);
-        if ( p2m_is_ram(ot) )
+        if ( ot == p2m_populate_on_demand )
+        {
+            /* Count how many PoD entries we'll be replacing if successful */
+            pod_count += n;
+        }
+        else if ( ot != p2m_invalid && ot != p2m_mmio_dm )
         {
             P2M_DEBUG("gfn_to_mfn returned type %d!\n", ot);
             rc = -EBUSY;
             goto out;
         }
-        else if ( ot == p2m_populate_on_demand )
-        {
-            /* Count how man PoD entries we'll be replacing if successful */
-            pod_count += n;
-        }
     }
 
     /* Now, actually do the two-way mapping */
xen/common/memory.c +10 -1
@@ -335,7 +335,7 @@ int guest_remove_page(struct domain *d,
     }
     if ( p2mt == p2m_mmio_direct )
     {
-        rc = clear_mmio_p2m_entry(d, gmfn, mfn, PAGE_ORDER_4K);
+        rc = -EPERM;
         goto out_put_gfn;
     }
 #else
@@ -1651,6 +1651,15 @@ int prepare_ring_for_helper(
         return -ENOENT;
     }
 #endif
+#ifdef CONFIG_X86
+    if ( p2mt == p2m_mmio_direct )
+    {
+        if ( page )
+            put_page(page);
+
+        return -EPERM;
+    }
+#endif
 
     if ( !page )
         return -EINVAL;
xen/include/asm-x86/p2m.h +2 -3
@@ -144,7 +144,8 @@ typedef unsigned int p2m_query_t;
 
 /* Types established/cleaned up via special accessors. */
 #define P2M_SPECIAL_TYPES (P2M_GRANT_TYPES | \
-                           p2m_to_mask(p2m_map_foreign))
+                           p2m_to_mask(p2m_map_foreign) | \
+                           p2m_to_mask(p2m_mmio_direct))
 
 /* Valid types not necessarily associated with a (valid) MFN. */
 #define P2M_INVALID_MFN_TYPES (P2M_POD_TYPES                  \
@@ -629,8 +630,6 @@ int set_foreign_p2m_entry(struct domain
 /* Set mmio addresses in the p2m table (for pass-through) */
 int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,
                        unsigned int order, p2m_access_t access);
-int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,
-                         unsigned int order);
 
 /* Set identity addresses in the p2m table (for pass-through) */
 int set_identity_p2m_entry(struct domain *d, unsigned long gfn,

From: Jan Beulich <[email protected]>
Subject: x86/p2m: fix PoD accounting in guest_physmap_add_entry()

The initial observation was that the mfn_valid() check comes too late:
Neither mfn_add() nor mfn_to_page() (let alone de-referencing the result of the latter) are valid for MFNs failing this check. Move it up and - noticing that there's no caller doing so - also add an assertion that this should never produce "false" here.

In turn this would have meant that the "else" to that if() could now go away, which didn't seem right at all. And indeed, considering callers like memory_exchange() or various grant table functions, the PoD accounting should have been outside of that if() from the very beginning.

xen/arch/x86/mm/p2m.c +14 -22
@@ -864,6 +864,12 @@ guest_physmap_add_entry(struct domain *d
     if ( p2m_is_foreign(t) )
         return -EINVAL;
 
+    if ( !mfn_valid(mfn) )
+    {
+        ASSERT_UNREACHABLE();
+        return -EINVAL;
+    }
+
     p2m_lock(p2m);
 
     P2M_DEBUG("adding gfn=%#lx mfn=%#lx\n", gfn_x(gfn), mfn_x(mfn));
@@ -963,12 +969,15 @@ guest_physmap_add_entry(struct domain *d
     }
 
     /* Now, actually do the two-way mapping */
-    if ( mfn_valid(mfn) )
+    rc = p2m_set_entry(p2m, gfn, mfn, page_order, t, p2m->default_access);
+    if ( rc == 0 )
     {
-        rc = p2m_set_entry(p2m, gfn, mfn, page_order, t,
-                           p2m->default_access);
-        if ( rc )
-            goto out; /* Failed to update p2m, bail without updating m2p. */
+#ifdef CONFIG_HVM
+        pod_lock(p2m);
+        p2m->pod.entry_count -= pod_count;
+        BUG_ON(p2m->pod.entry_count < 0);
+        pod_unlock(p2m);
+#endif
 
         if ( !p2m_is_grant(t) )
         {
@@ -977,24 +986,7 @@ guest_physmap_add_entry(struct domain *d
                                   gfn_x(gfn_add(gfn, i)));
         }
     }
-    else
-    {
-        gdprintk(XENLOG_WARNING, "Adding bad mfn to p2m map (%#lx -> %#lx)\n",
-                 gfn_x(gfn), mfn_x(mfn));
-        rc = p2m_set_entry(p2m, gfn, INVALID_MFN, page_order,
-                           p2m_invalid, p2m->default_access);
-#ifdef CONFIG_HVM
-        if ( rc == 0 )
-        {
-            pod_lock(p2m);
-            p2m->pod.entry_count -= pod_count;
-            BUG_ON(p2m->pod.entry_count < 0);
-            pod_unlock(p2m);
-        }
-#endif
-    }
 
-out:
     p2m_unlock(p2m);
 
     return rc;

From: Jan Beulich <[email protected]>
Subject: x86/p2m: don't ignore p2m_remove_page()'s return value

It's not very nice to return from guest_physmap_add_entry() after perhaps already having made some changes to the P2M, but this is pre- existing practice in the function, and imo better than ignoring errors.

Take the liberty and replace an mfn_add() instance with a local variable already holding the result (as proven by the check immediately ahead).

xen/arch/x86/mm/p2m.c +8 -8
@@ -772,8 +772,7 @@ void p2m_final_teardown(struct domain *d
     p2m_teardown_hostp2m(d);
 }
 
-
-static int
+static int __must_check
 p2m_remove_page(struct p2m_domain *p2m, unsigned long gfn_l, unsigned long mfn,
                 unsigned int page_order)
 {
@@ -961,9 +960,9 @@ guest_physmap_add_entry(struct domain *d
                 ASSERT(mfn_valid(omfn));
                 P2M_DEBUG("old gfn=%#lx -> mfn %#lx\n",
                           gfn_x(ogfn) , mfn_x(omfn));
-                if ( mfn_eq(omfn, mfn_add(mfn, i)) )
-                    p2m_remove_page(p2m, gfn_x(ogfn), mfn_x(mfn_add(mfn, i)),
-                                    0);
+                if ( mfn_eq(omfn, mfn_add(mfn, i)) &&
+                     (rc = p2m_remove_page(p2m, gfn_x(ogfn), mfn_x(omfn), 0)) )
+                    goto out;
             }
         }
     }
@@ -987,6 +986,7 @@ guest_physmap_add_entry(struct domain *d
         }
     }
 
+ out:
     p2m_unlock(p2m);
 
     return rc;
@@ -2646,9 +2646,9 @@ int p2m_change_altp2m_gfn(struct domain
 
     if ( gfn_eq(new_gfn, INVALID_GFN) )
     {
-        if ( mfn_valid(mfn) )
-            p2m_remove_page(ap2m, gfn_x(old_gfn), mfn_x(mfn), PAGE_ORDER_4K);
-        rc = 0;
+        rc = mfn_valid(mfn)
+             ? p2m_remove_page(ap2m, gfn_x(old_gfn), mfn_x(mfn), PAGE_ORDER_4K)
+             : 0;
         goto out;
     }
 

From: Jan Beulich <[email protected]>
Subject: x86/p2m: don't assert that the passed in MFN matches for a remove

guest_physmap_remove_page() gets handed an MFN from the outside, yet takes the necessary lock to prevent further changes to the GFN <-> MFN mapping itself. While some callers, in particular guest_remove_page() (by way of having called get_gfn_query()), hold the GFN lock already, various others (most notably perhaps the 2nd instance in xenmem_add_to_physmap_one()) don't. While it also is an option to fix all the callers, deal with the issue in p2m_remove_page() instead:
Replace the ASSERT() by a conditional and split the loop into two, such that all checking gets done before any modification would occur.

xen/arch/x86/mm/p2m.c +14 -4
@@ -778,7 +778,6 @@ p2m_remove_page(struct p2m_domain *p2m,
 {
     unsigned long i;
     gfn_t gfn = _gfn(gfn_l);
-    mfn_t mfn_return;
     p2m_type_t t;
     p2m_access_t a;
 
@@ -789,15 +788,26 @@ p2m_remove_page(struct p2m_domain *p2m,
     ASSERT(gfn_locked_by_me(p2m, gfn));
     P2M_DEBUG("removing gfn=%#lx mfn=%#lx\n", gfn_l, mfn);
 
+    for ( i = 0; i < (1UL << page_order); )
+    {
+        unsigned int cur_order;
+        mfn_t mfn_return = p2m->get_entry(p2m, gfn_add(gfn, i), &t, &a, 0,
+                                          &cur_order, NULL);
+
+        if ( p2m_is_valid(t) &&
+             (!mfn_valid(_mfn(mfn)) || mfn + i != mfn_x(mfn_return)) )
+            return -EILSEQ;
+
+        i += (1UL << cur_order) - ((gfn_l + i) & ((1UL << cur_order) - 1));
+    }
+
     if ( mfn_valid(_mfn(mfn)) )
     {
         for ( i = 0; i < (1UL << page_order); i++ )
         {
-            mfn_return = p2m->get_entry(p2m, gfn_add(gfn, i), &t, &a, 0,
-                                        NULL, NULL);
+            p2m->get_entry(p2m, gfn_add(gfn, i), &t, &a, 0, NULL, NULL);
             if ( !p2m_is_grant(t) && !p2m_is_shared(t) && !p2m_is_foreign(t) )
                 set_gpfn_from_mfn(mfn+i, INVALID_M2P_ENTRY);
-            ASSERT( !p2m_is_valid(t) || mfn + i == mfn_x(mfn_return) );
         }
     }
     return p2m_set_entry(p2m, gfn, INVALID_MFN, page_order, p2m_invalid,

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct global exclusion range extending

Besides unity mapping regions, the AMD IOMMU spec also provides for exclusion ranges (areas of memory not to be subject to DMA translation) to be specified by firmware in the ACPI tables. The spec does not put any constraints on the number of such regions.

Blindly assuming all addresses between any two such ranges should also be excluded can't be right. Since hardware has room for just a single such range (comprised of the Exclusion Base Register and the Exclusion Range Limit Register), combine only adjacent or overlapping regions (for now; this may require further adjustment in case table entries aren't sorted by address) with matching exclusion_allow_all settings. This requires bubbling up error indicators, such that IOMMU init can be failed when concatenation wasn't possible.

Furthermore, since the exclusion range specified in IOMMU registers implies R/W access, reject requests asking for less permissions (this will be brought closer to the spec by a subsequent change).

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu_acpi.c +30 -15

106 lines not on this page · raw message

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct device unity map handling

Blindly assuming all addresses between any two such ranges, specified by firmware in the ACPI tables, should also be unity-mapped can't be right. Nor can it be correct to merge ranges with differing permissions. Track ranges individually; don't merge at all, but check for overlaps instead. This requires bubbling up error indicators, such that IOMMU init can be failed when allocation of a new tracking struct wasn't possible, or an overlap was detected.

At this occasion also stop ignoring amd_iommu_reserve_domain_unity_map()'s return value.

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu_acpi.c +50 -34

132 lines not on this page · raw message

xen/drivers/passthrough/amd/iommu_init.c +0 -1
@@ -1189,7 +1189,6 @@ static int __init alloc_ivrs_mappings(u1
     {
         ivrs_mappings[bdf].dte_requestor_id = bdf;
         ivrs_mappings[bdf].dte_allow_exclusion = IOMMU_CONTROL_DISABLED;
-        ivrs_mappings[bdf].unity_map_enable = IOMMU_CONTROL_DISABLED;
         ivrs_mappings[bdf].iommu = NULL;
 
         ivrs_mappings[bdf].intremap_table = NULL;
xen/drivers/passthrough/amd/pci_amd_iommu.c +9 -7
@@ -346,15 +346,17 @@ static int amd_iommu_assign_device(struc
     struct ivrs_mappings *ivrs_mappings = get_ivrs_mappings(pdev->seg);
     int bdf = PCI_BDF2(pdev->bus, devfn);
     int req_id = get_dma_requestor_id(pdev->seg, bdf);
+    const struct ivrs_unity_map *unity_map;
 
-    if ( ivrs_mappings[req_id].unity_map_enable )
+    for ( unity_map = ivrs_mappings[req_id].unity_map; unity_map;
+          unity_map = unity_map->next )
     {
-        amd_iommu_reserve_domain_unity_map(
-            d,
-            ivrs_mappings[req_id].addr_range_start,
-            ivrs_mappings[req_id].addr_range_length,
-            ivrs_mappings[req_id].write_permission,
-            ivrs_mappings[req_id].read_permission);
+        int rc = amd_iommu_reserve_domain_unity_map(
+                     d, unity_map->addr, unity_map->length,
+                     unity_map->write, unity_map->read);
+
+        if ( rc )
+            return rc;
     }
 
     return reassign_device(pdev->domain, d, devfn, pdev);
xen/include/asm-x86/amd-iommu.h +9 -5
@@ -108,15 +108,19 @@ struct amd_iommu {
     struct list_head ats_devices;
 };
 
+struct ivrs_unity_map {
+    bool read:1;
+    bool write:1;
+    paddr_t addr;
+    unsigned long length;
+    struct ivrs_unity_map *next;
+};
+
 struct ivrs_mappings {
     u16 dte_requestor_id;
     u8 dte_allow_exclusion;
-    u8 unity_map_enable;
-    u8 write_permission;
-    u8 read_permission;
-    unsigned long addr_range_start;
-    unsigned long addr_range_length;
     struct amd_iommu *iommu;
+    struct ivrs_unity_map *unity_map;
 
     /* per device interrupt remapping table */
     void *intremap_table;

From: Jan Beulich <[email protected]>
Subject: IOMMU: also pass p2m_access_t to p2m_get_iommu_flags()

A subsequent change will want to customize the IOMMU permissions based on this.

This is part of XSA-378.

xen/arch/x86/mm/p2m-ept.c +3 -3
@@ -672,7 +672,7 @@ ept_set_entry(struct p2m_domain *p2m, gf
     uint8_t ipat = 0;
     bool_t need_modify_vtd_table = 1;
     bool_t vtd_pte_present = 0;
-    unsigned int iommu_flags = p2m_get_iommu_flags(p2mt, mfn);
+    unsigned int iommu_flags = p2m_get_iommu_flags(p2mt, p2ma, mfn);
     bool_t needs_sync = 1;
     ept_entry_t old_entry = { .epte = 0 };
     ept_entry_t new_entry = { .epte = 0 };
@@ -799,8 +799,8 @@ ept_set_entry(struct p2m_domain *p2m, gf
 
         /* Safe to read-then-write because we hold the p2m lock */
         if ( ept_entry->mfn == new_entry.mfn &&
-             p2m_get_iommu_flags(ept_entry->sa_p2mt, _mfn(ept_entry->mfn)) ==
-             iommu_flags )
+             p2m_get_iommu_flags(ept_entry->sa_p2mt, ept_entry->access,
+                                 _mfn(ept_entry->mfn)) == iommu_flags )
             need_modify_vtd_table = 0;
 
         ept_p2m_type_to_flags(p2m, &new_entry, p2mt, p2ma);
xen/arch/x86/mm/p2m-pt.c +16 -3
@@ -512,6 +512,16 @@ int p2m_pt_handle_deferred_changes(uint6
     return rc;
 }
 
+/* Reconstruct a fake p2m_access_t from stored PTE flags. */
+static p2m_access_t p2m_flags_to_access(unsigned int flags)
+{
+    if ( flags & _PAGE_PRESENT )
+        return p2m_access_n;
+
+    /* No need to look at _PAGE_NX for now. */
+    return flags & _PAGE_RW ? p2m_access_rw : p2m_access_r;
+}
+
 /* Checks only applicable to entries with order > PAGE_ORDER_4K */
 static void check_entry(mfn_t mfn, p2m_type_t new, p2m_type_t old,
                         unsigned int order)
@@ -546,7 +556,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
     l2_pgentry_t l2e_content;
     l3_pgentry_t l3e_content;
     int rc;
-    unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt, mfn);
+    unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt, p2ma, mfn);
     /*
      * old_mfn and iommu_old_flags control possible flush/update needs on the
      * IOMMU: We need to flush when MFN or flags (i.e. permissions) change.
@@ -609,6 +619,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                 old_mfn = l1e_get_pfn(*p2m_entry);
                 iommu_old_flags =
                     p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                        p2m_flags_to_access(flags),
                                         _mfn(old_mfn));
             }
             else
@@ -654,9 +665,10 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                                    0, L1_PAGETABLE_ENTRIES);
         ASSERT(p2m_entry);
         old_mfn = l1e_get_pfn(*p2m_entry);
+        flags = l1e_get_flags(*p2m_entry);
         iommu_old_flags =
-            p2m_get_iommu_flags(p2m_flags_to_type(l1e_get_flags(*p2m_entry)),
-                                _mfn(old_mfn));
+            p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                p2m_flags_to_access(flags), _mfn(old_mfn));
 
         if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) )
             entry_content = p2m_l1e_from_pfn(mfn_x(mfn),
@@ -687,6 +699,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m,
                 old_mfn = l1e_get_pfn(*p2m_entry);
                 iommu_old_flags =
                     p2m_get_iommu_flags(p2m_flags_to_type(flags),
+                                        p2m_flags_to_access(flags),
                                         _mfn(old_mfn));
             }
             else
xen/include/asm-x86/p2m.h +2 -1
@@ -899,7 +899,8 @@ static inline void p2m_altp2m_check(stru
 /*
  * p2m type to IOMMU flags
  */
-static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt, mfn_t mfn)
+static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt,
+                                               p2m_access_t p2ma, mfn_t mfn)
 {
     unsigned int flags;
 

From: Jan Beulich <[email protected]>
Subject: IOMMU: generalize VT-d's tracking of mapped RMRR regions

In order to re-use it elsewhere, move the logic to vendor independent code and strip it of RMRR specifics.

Note that the prior "map" parameter gets folded into the new "p2ma" one (which AMD IOMMU code will want to make use of), assigning alternative meaning ("unmap") to p2m_access_x. Prepare set_identity_p2m_entry() and p2m_get_iommu_flags() for getting passed access types other than p2m_access_rw (in the latter case just for p2m_mmio_direct requests).

Note also that, to be on the safe side, an overlap check gets added to the main loop of iommu_identity_mapping().

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +1 -1
@@ -1341,7 +1341,7 @@ int set_identity_p2m_entry(struct domain
         if ( !has_iommu_pt(d) )
             return 0;
         return iommu_legacy_map(d, _dfn(gfn_l), _mfn(gfn_l), PAGE_ORDER_4K,
-                                IOMMUF_readable | IOMMUF_writable);
+                                p2m_access_to_iommu_flags(p2ma));
     }
 
     gfn_lock(p2m, gfn, 0);
xen/drivers/passthrough/vtd/iommu.c +13 -85

150 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +94 -1

107 lines not on this page · raw message

xen/include/asm-x86/iommu.h +7 -1
@@ -16,6 +16,7 @@
 
 #include <xen/errno.h>
 #include <xen/list.h>
+#include <xen/mem_access.h>
 #include <xen/spinlock.h>
 #include <asm/processor.h>
 #include <asm/hvm/vmx/vmcs.h>
@@ -48,7 +49,7 @@ struct arch_iommu
     spinlock_t mapping_lock;            /* io page table lock */
     int agaw;     /* adjusted guest address width, 0 is level 2 30-bit */
     u64 iommu_bitmap;              /* bitmap of iommu(s) that the domain uses */
-    struct list_head mapped_rmrrs;
+    struct list_head identity_maps;
 
     /* amd iommu support */
     int paging_mode;
@@ -94,6 +95,11 @@ bool_t iommu_supports_eim(void);
 int iommu_enable_x2apic_IR(void);
 void iommu_disable_x2apic_IR(void);
 
+int iommu_identity_mapping(struct domain *d, p2m_access_t p2ma,
+                           paddr_t base, paddr_t end,
+                           unsigned int flag);
+void iommu_identity_map_teardown(struct domain *d);
+
 extern bool untrusted_msi;
 
 int pi_update_irte(const struct pi_desc *pi_desc, const struct pirq *pirq,
xen/include/asm-x86/p2m.h +32 -3
@@ -896,6 +896,34 @@ struct p2m_domain *p2m_get_altp2m(struct
 static inline void p2m_altp2m_check(struct vcpu *v, uint16_t idx) {}
 #endif
 
+/* p2m access to IOMMU flags */
+static inline unsigned int p2m_access_to_iommu_flags(p2m_access_t p2ma)
+{
+    switch ( p2ma )
+    {
+    case p2m_access_rw:
+    case p2m_access_rwx:
+        return IOMMUF_readable | IOMMUF_writable;
+
+    case p2m_access_r:
+    case p2m_access_rx:
+    case p2m_access_rx2rw:
+        return IOMMUF_readable;
+
+    case p2m_access_w:
+    case p2m_access_wx:
+        return IOMMUF_writable;
+
+    case p2m_access_n:
+    case p2m_access_x:
+    case p2m_access_n2rwx:
+        return 0;
+    }
+
+    ASSERT_UNREACHABLE();
+    return 0;
+}
+
 /*
  * p2m type to IOMMU flags
  */
@@ -917,9 +945,10 @@ static inline unsigned int p2m_get_iommu
         flags = IOMMUF_readable;
         break;
     case p2m_mmio_direct:
-        flags = IOMMUF_readable;
-        if ( !rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn)) )
-            flags |= IOMMUF_writable;
+        flags = p2m_access_to_iommu_flags(p2ma);
+        if ( (flags & IOMMUF_writable) &&
+             rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn)) )
+            flags &= ~IOMMUF_writable;
         break;
     default:
         flags = 0;

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange/complete re-assignment handling

Prior to the assignment step having completed successfully, devices should not get associated with their new owner. Hand the device to DomIO (perhaps temporarily), until after the de-assignment step has completed.

De-assignment of a device (from other than Dom0) as well as failure of reassign_device() during assignment should result in unity mappings getting torn down. This in turn requires switching to a refcounted mapping approach, as was already used by VT-d for its RMRRs, to prevent unmapping a region used by multiple devices.

This is CVE-2021-28696 / part of XSA-378.

xen/drivers/passthrough/amd/iommu_map.c +37 -26

75 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +42 -12

90 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +4 -2
@@ -62,8 +62,10 @@ int __must_check amd_iommu_unmap_page(st
 uint64_t amd_iommu_get_address_from_pte(void *entry);
 int __must_check amd_iommu_alloc_root(struct domain_iommu *hd);
 int amd_iommu_reserve_domain_unity_map(struct domain *domain,
-                                       paddr_t phys_addr, unsigned long size,
-                                       int iw, int ir);
+                                       const struct ivrs_unity_map *map,
+                                       unsigned int flag);
+int amd_iommu_reserve_domain_unity_unmap(struct domain *d,
+                                         const struct ivrs_unity_map *map);
 int __must_check amd_iommu_flush_iotlb_pages(struct domain *d, dfn_t dfn,
                                              unsigned int page_count,
                                              unsigned int flush_flags);

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange exclusion range and unity map recording

The spec makes no provisions for OS behavior here to depend on the amount of RAM found on the system. While the spec may not sufficiently clearly distinguish both kinds of regions, they are surely meant to be separate things: Only regions with ACPI_IVMD_EXCLUSION_RANGE set should be candidates for putting in the exclusion range registers. (As there's only a single such pair of registers per IOMMU, secondary non-adjacent regions with the flag set already get converted to unity mapped regions.)

First of all, drop the dependency on max_page. With commit b4f042236ae0 ("AMD/IOMMU: Cease using a dynamic height for the IOMMU pagetables") the use of it here was stale anyway; it was bogus already before, as it didn't account for max_page getting increased later on. Simply try an exclusion range registration first, and if it fails (for being unsuitable or non-mergeable), register a unity mapping range.

With this various local variables become unnecessary and hence get dropped at the same time.

With the max_page boundary dropped for using unity maps, the minimum page table tree height now needs both recording and enforcing in amd_iommu_domain_init(). Since we can't predict which devices may get assigned to a domain, our only option is to uniformly force at least that height for all domains, now that the height isn't dynamic anymore.

Further don't make use of the exclusion range unless ACPI data says so.

Note that exclusion range registration in register_range_for_all_devices() is on a best effort basis. Hence unity map entries also registered are redundant when the former succeeded, but they also do no harm. Improvements in this area can be done later imo.

Also adjust types where suitable without touching extra lines.

This is part of XSA-378.

xen/drivers/passthrough/amd/iommu_acpi.c +82 -106

76 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +8 -4

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: introduce p2m_is_special()

Seeing the similarity of grant, foreign, and (subsequently) direct-MMIO handling, introduce a new P2M type group named "special" (as in "needing special accessors to create/destroy").

Also use -EPERM instead of other error codes on the two domain_crash() paths touched.

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +7 -8

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: guard (in particular) identity mapping entries

Such entries, created by set_identity_p2m_entry(), should only be destroyed by clear_identity_p2m_entry(). However, similarly, entries created by set_mmio_p2m_entry() should only be torn down by clear_mmio_p2m_entry(), so the logic gets based upon p2m_mmio_direct as the entry type (separation between "ordinary" and 1:1 mappings would require a further indicator to tell apart the two).

As to the guest_remove_page() change, commit 48dfb297a20a ("x86/PVH:
allow guest_remove_page to remove p2m_mmio_direct pages"), which introduced the call to clear_mmio_p2m_entry(), claimed this was done for hwdom only without this actually having been the case. However, this code shouldn't be there in the first place, as MMIO entries shouldn't be dropped this way. Avoid triggering the warning again that 48dfb297a20a silenced by an adjustment to xenmem_add_to_physmap_one() instead.

Note that guest_physmap_mark_populate_on_demand() gets tightened beyond the immediate purpose of this change.

Note also that I didn't inspect code which isn't security supported, e.g. sharing, paging, or altp2m.

This is CVE-2021-28694 / part of XSA-378.

xen/arch/x86/mm.c +3 -1

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m.c +6 -5

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m-pod.c +6 -6

Lines of this file are not shown. View the raw message.

xen/common/memory.c +10 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +2 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: fix PoD accounting in guest_physmap_add_entry()

The initial observation was that the mfn_valid() check comes too late:
Neither mfn_add() nor mfn_to_page() (let alone de-referencing the result of the latter) are valid for MFNs failing this check. Move it up and - noticing that there's no caller doing so - also add an assertion that this should never produce "false" here.

In turn this would have meant that the "else" to that if() could now go away, which didn't seem right at all. And indeed, considering callers like memory_exchange() or various grant table functions, the PoD accounting should have been outside of that if() from the very beginning.

master commit: aea270e3f7c0db696c88a0e94b1ece7abd339c84 master date: 2020-02-21 17:14:38 +0100

xen/arch/x86/mm/p2m.c +12 -20

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: don't ignore p2m_remove_page()'s return value

It's not very nice to return from guest_physmap_add_entry() after perhaps already having made some changes to the P2M, but this is pre- existing practice in the function, and imo better than ignoring errors.

Take the liberty and replace an mfn_add() instance with a local variable already holding the result (as proven by the check immediately ahead).

master commit: a6b051a87a586347969bfbaa6925ac0f0c845413 master date: 2020-04-03 10:56:10 +0200

xen/arch/x86/mm/p2m.c +8 -8

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: don't assert that the passed in MFN matches for a remove

guest_physmap_remove_page() gets handed an MFN from the outside, yet takes the necessary lock to prevent further changes to the GFN <-> MFN mapping itself. While some callers, in particular guest_remove_page() (by way of having called get_gfn_query()), hold the GFN lock already, various others (most notably perhaps the 2nd instance in xenmem_add_to_physmap_one()) don't. While it also is an option to fix all the callers, deal with the issue in p2m_remove_page() instead:
Replace the ASSERT() by a conditional and split the loop into two, such that all checking gets done before any modification would occur.

master commit: c65ea16dbcafbe4fe21693b18f8c2a3c5d14600e master date: 2020-04-03 10:56:55 +0200

xen/arch/x86/mm/p2m.c +14 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct global exclusion range extending

Besides unity mapping regions, the AMD IOMMU spec also provides for exclusion ranges (areas of memory not to be subject to DMA translation) to be specified by firmware in the ACPI tables. The spec does not put any constraints on the number of such regions.

Blindly assuming all addresses between any two such ranges should also be excluded can't be right. Since hardware has room for just a single such range (comprised of the Exclusion Base Register and the Exclusion Range Limit Register), combine only adjacent or overlapping regions (for now; this may require further adjustment in case table entries aren't sorted by address) with matching exclusion_allow_all settings. This requires bubbling up error indicators, such that IOMMU init can be failed when concatenation wasn't possible.

Furthermore, since the exclusion range specified in IOMMU registers implies R/W access, reject requests asking for less permissions (this will be brought closer to the spec by a subsequent change).

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu_acpi.c +30 -15

45 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct device unity map handling

Blindly assuming all addresses between any two such ranges, specified by firmware in the ACPI tables, should also be unity-mapped can't be right. Nor can it be correct to merge ranges with differing permissions. Track ranges individually; don't merge at all, but check for overlaps instead. This requires bubbling up error indicators, such that IOMMU init can be failed when allocation of a new tracking struct wasn't possible, or an overlap was detected.

At this occasion also stop ignoring amd_iommu_reserve_domain_unity_map()'s return value.

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu_acpi.c +50 -34

84 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +9 -7

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/amd-iommu.h +9 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: IOMMU: also pass p2m_access_t to p2m_get_iommu_flags()

A subsequent change will want to customize the IOMMU permissions based on this.

This is part of XSA-378.

xen/arch/x86/mm/p2m-ept.c +3 -3

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m-pt.c +16 -3

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +2 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: IOMMU: generalize VT-d's tracking of mapped RMRR regions

In order to re-use it elsewhere, move the logic to vendor independent code and strip it of RMRR specifics.

Note that the prior "map" parameter gets folded into the new "p2ma" one (which AMD IOMMU code will want to make use of), assigning alternative meaning ("unmap") to p2m_access_x. Prepare set_identity_p2m_entry() and p2m_get_iommu_flags() for getting passed access types other than p2m_access_rw (in the latter case just for p2m_mmio_direct requests).

Note also that, to be on the safe side, an overlap check gets added to the main loop of iommu_identity_mapping().

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +1 -1

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/vtd/iommu.c +13 -85

98 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +94 -1

95 lines not on this page · raw message

xen/include/asm-x86/iommu.h +7 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +32 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange/complete re-assignment handling

Prior to the assignment step having completed successfully, devices should not get associated with their new owner. Hand the device to DomIO (perhaps temporarily), until after the de-assignment step has completed.

De-assignment of a device (from other than Dom0) as well as failure of reassign_device() during assignment should result in unity mappings getting torn down. This in turn requires switching to a refcounted mapping approach, as was already used by VT-d for its RMRRs, to prevent unmapping a region used by multiple devices.

This is CVE-2021-28696 / part of XSA-378.

xen/drivers/passthrough/amd/iommu_map.c +37 -26

63 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +42 -12

54 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +4 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange exclusion range and unity map recording

The spec makes no provisions for OS behavior here to depend on the amount of RAM found on the system. While the spec may not sufficiently clearly distinguish both kinds of regions, they are surely meant to be separate things: Only regions with ACPI_IVMD_EXCLUSION_RANGE set should be candidates for putting in the exclusion range registers. (As there's only a single such pair of registers per IOMMU, secondary non-adjacent regions with the flag set already get converted to unity mapped regions.)

First of all, drop the dependency on max_page. With commit b4f042236ae0 ("AMD/IOMMU: Cease using a dynamic height for the IOMMU pagetables") the use of it here was stale anyway; it was bogus already before, as it didn't account for max_page getting increased later on. Simply try an exclusion range registration first, and if it fails (for being unsuitable or non-mergeable), register a unity mapping range.

With this various local variables become unnecessary and hence get dropped at the same time.

With the max_page boundary dropped for using unity maps, the minimum page table tree height now needs both recording and enforcing in amd_iommu_domain_init(). Since we can't predict which devices may get assigned to a domain, our only option is to uniformly force at least that height for all domains, now that the height isn't dynamic anymore.

Further don't make use of the exclusion range unless ACPI data says so.

Note that exclusion range registration in register_range_for_all_devices() is on a best effort basis. Hence unity map entries also registered are redundant when the former succeeded, but they also do no harm. Improvements in this area can be done later imo.

Also adjust types where suitable without touching extra lines.

This is part of XSA-378.

xen/drivers/passthrough/amd/iommu_acpi.c +82 -106

188 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +8 -4

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: introduce p2m_is_special()

Seeing the similarity of grant, foreign, and (subsequently) direct-MMIO handling, introduce a new P2M type group named "special" (as in "needing special accessors to create/destroy").

Also use -EPERM instead of other error codes on the two domain_crash() paths touched.

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +7 -8

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: guard (in particular) identity mapping entries

Such entries, created by set_identity_p2m_entry(), should only be destroyed by clear_identity_p2m_entry(). However, similarly, entries created by set_mmio_p2m_entry() should only be torn down by clear_mmio_p2m_entry(), so the logic gets based upon p2m_mmio_direct as the entry type (separation between "ordinary" and 1:1 mappings would require a further indicator to tell apart the two).

As to the guest_remove_page() change, commit 48dfb297a20a ("x86/PVH:
allow guest_remove_page to remove p2m_mmio_direct pages"), which introduced the call to clear_mmio_p2m_entry(), claimed this was done for hwdom only without this actually having been the case. However, this code shouldn't be there in the first place, as MMIO entries shouldn't be dropped this way. Avoid triggering the warning again that 48dfb297a20a silenced by an adjustment to xenmem_add_to_physmap_one() instead.

Note that guest_physmap_mark_populate_on_demand() gets tightened beyond the immediate purpose of this change.

Note also that I didn't inspect code which isn't security supported, e.g. sharing, paging, or altp2m.

This is CVE-2021-28694 / part of XSA-378.

xen/arch/x86/mm.c +3 -1

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m.c +6 -5

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m-pod.c +6 -6

Lines of this file are not shown. View the raw message.

xen/common/memory.c +10 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +2 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct global exclusion range extending

Besides unity mapping regions, the AMD IOMMU spec also provides for exclusion ranges (areas of memory not to be subject to DMA translation) to be specified by firmware in the ACPI tables. The spec does not put any constraints on the number of such regions.

Blindly assuming all addresses between any two such ranges should also be excluded can't be right. Since hardware has room for just a single such range (comprised of the Exclusion Base Register and the Exclusion Range Limit Register), combine only adjacent or overlapping regions (for now; this may require further adjustment in case table entries aren't sorted by address) with matching exclusion_allow_all settings. This requires bubbling up error indicators, such that IOMMU init can be failed when concatenation wasn't possible.

Furthermore, since the exclusion range specified in IOMMU registers implies R/W access, reject requests asking for less permissions (this will be brought closer to the spec by a subsequent change).

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu_acpi.c +30 -15

45 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct device unity map handling

Blindly assuming all addresses between any two such ranges, specified by firmware in the ACPI tables, should also be unity-mapped can't be right. Nor can it be correct to merge ranges with differing permissions. Track ranges individually; don't merge at all, but check for overlaps instead. This requires bubbling up error indicators, such that IOMMU init can be failed when allocation of a new tracking struct wasn't possible, or an overlap was detected.

At this occasion also stop ignoring amd_iommu_reserve_domain_unity_map()'s return value.

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu.h +9 -5

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/amd/iommu_acpi.c +50 -34

84 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +9 -7

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: IOMMU: also pass p2m_access_t to p2m_get_iommu_flags()

A subsequent change will want to customize the IOMMU permissions based on this.

This is part of XSA-378.

xen/arch/x86/mm/p2m-ept.c +3 -3

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m-pt.c +16 -3

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +2 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: IOMMU: generalize VT-d's tracking of mapped RMRR regions

In order to re-use it elsewhere, move the logic to vendor independent code and strip it of RMRR specifics.

Note that the prior "map" parameter gets folded into the new "p2ma" one (which AMD IOMMU code will want to make use of), assigning alternative meaning ("unmap") to p2m_access_x. Prepare set_identity_p2m_entry() and p2m_get_iommu_flags() for getting passed access types other than p2m_access_rw (in the latter case just for p2m_mmio_direct requests).

Note also that, to be on the safe side, an overlap check gets added to the main loop of iommu_identity_mapping().

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +1 -1

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/vtd/iommu.c +13 -85

98 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +94 -1

95 lines not on this page · raw message

xen/include/asm-x86/iommu.h +7 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +32 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange/complete re-assignment handling

Prior to the assignment step having completed successfully, devices should not get associated with their new owner. Hand the device to DomIO (perhaps temporarily), until after the de-assignment step has completed.

De-assignment of a device (from other than Dom0) as well as failure of reassign_device() during assignment should result in unity mappings getting torn down. This in turn requires switching to a refcounted mapping approach, as was already used by VT-d for its RMRRs, to prevent unmapping a region used by multiple devices.

This is CVE-2021-28696 / part of XSA-378.

xen/drivers/passthrough/amd/iommu.h +4 -2

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/amd/iommu_map.c +37 -26

63 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +42 -12

54 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange exclusion range and unity map recording

The spec makes no provisions for OS behavior here to depend on the amount of RAM found on the system. While the spec may not sufficiently clearly distinguish both kinds of regions, they are surely meant to be separate things: Only regions with ACPI_IVMD_EXCLUSION_RANGE set should be candidates for putting in the exclusion range registers. (As there's only a single such pair of registers per IOMMU, secondary non-adjacent regions with the flag set already get converted to unity mapped regions.)

First of all, drop the dependency on max_page. With commit b4f042236ae0 ("AMD/IOMMU: Cease using a dynamic height for the IOMMU pagetables") the use of it here was stale anyway; it was bogus already before, as it didn't account for max_page getting increased later on. Simply try an exclusion range registration first, and if it fails (for being unsuitable or non-mergeable), register a unity mapping range.

With this various local variables become unnecessary and hence get dropped at the same time.

With the max_page boundary dropped for using unity maps, the minimum page table tree height now needs both recording and enforcing in amd_iommu_domain_init(). Since we can't predict which devices may get assigned to a domain, our only option is to uniformly force at least that height for all domains, now that the height isn't dynamic anymore.

Further don't make use of the exclusion range unless ACPI data says so.

Note that exclusion range registration in register_range_for_all_devices() is on a best effort basis. Hence unity map entries also registered are redundant when the former succeeded, but they also do no harm. Improvements in this area can be done later imo.

Also adjust types where suitable without touching extra lines.

This is part of XSA-378.

xen/drivers/passthrough/amd/iommu.h +2 -0

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/amd/iommu_acpi.c +82 -106

188 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +8 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: introduce p2m_is_special()

Seeing the similarity of grant, foreign, and (subsequently) direct-MMIO handling, introduce a new P2M type group named "special" (as in "needing special accessors to create/destroy").

Also use -EPERM instead of other error codes on the two domain_crash() paths touched.

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +7 -8

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: guard (in particular) identity mapping entries

Such entries, created by set_identity_p2m_entry(), should only be destroyed by clear_identity_p2m_entry(). However, similarly, entries created by set_mmio_p2m_entry() should only be torn down by clear_mmio_p2m_entry(), so the logic gets based upon p2m_mmio_direct as the entry type (separation between "ordinary" and 1:1 mappings would require a further indicator to tell apart the two).

As to the guest_remove_page() change, commit 48dfb297a20a ("x86/PVH:
allow guest_remove_page to remove p2m_mmio_direct pages"), which introduced the call to clear_mmio_p2m_entry(), claimed this was done for hwdom only without this actually having been the case. However, this code shouldn't be there in the first place, as MMIO entries shouldn't be dropped this way. Avoid triggering the warning again that 48dfb297a20a silenced by an adjustment to xenmem_add_to_physmap_one() instead.

Note that guest_physmap_mark_populate_on_demand() gets tightened beyond the immediate purpose of this change.

Note also that I didn't inspect code which isn't security supported, e.g. sharing, paging, or altp2m.

This is CVE-2021-28694 / part of XSA-378.

xen/arch/x86/mm.c +3 -1

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m.c +6 -5

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m-pod.c +6 -6

Lines of this file are not shown. View the raw message.

xen/common/memory.c +10 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +2 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct global exclusion range extending

Besides unity mapping regions, the AMD IOMMU spec also provides for exclusion ranges (areas of memory not to be subject to DMA translation) to be specified by firmware in the ACPI tables. The spec does not put any constraints on the number of such regions.

Blindly assuming all addresses between any two such ranges should also be excluded can't be right. Since hardware has room for just a single such range (comprised of the Exclusion Base Register and the Exclusion Range Limit Register), combine only adjacent or overlapping regions (for now; this may require further adjustment in case table entries aren't sorted by address) with matching exclusion_allow_all settings. This requires bubbling up error indicators, such that IOMMU init can be failed when concatenation wasn't possible.

Furthermore, since the exclusion range specified in IOMMU registers implies R/W access, reject requests asking for less permissions (this will be brought closer to the spec by a subsequent change).

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu_acpi.c +30 -15

45 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: correct device unity map handling

Blindly assuming all addresses between any two such ranges, specified by firmware in the ACPI tables, should also be unity-mapped can't be right. Nor can it be correct to merge ranges with differing permissions. Track ranges individually; don't merge at all, but check for overlaps instead. This requires bubbling up error indicators, such that IOMMU init can be failed when allocation of a new tracking struct wasn't possible, or an overlap was detected.

At this occasion also stop ignoring amd_iommu_reserve_domain_unity_map()'s return value.

This is part of XSA-378 / CVE-2021-28695.

xen/drivers/passthrough/amd/iommu.h +9 -5

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/amd/iommu_acpi.c +50 -34

84 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +9 -7

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: IOMMU: also pass p2m_access_t to p2m_get_iommu_flags()

A subsequent change will want to customize the IOMMU permissions based on this.

This is part of XSA-378.

xen/arch/x86/mm/p2m-ept.c +3 -3

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m-pt.c +16 -3

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +2 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: IOMMU: generalize VT-d's tracking of mapped RMRR regions

In order to re-use it elsewhere, move the logic to vendor independent code and strip it of RMRR specifics.

Note that the prior "map" parameter gets folded into the new "p2ma" one (which AMD IOMMU code will want to make use of), assigning alternative meaning ("unmap") to p2m_access_x. Prepare set_identity_p2m_entry() and p2m_get_iommu_flags() for getting passed access types other than p2m_access_rw (in the latter case just for p2m_mmio_direct requests).

Note also that, to be on the safe side, an overlap check gets added to the main loop of iommu_identity_mapping().

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +1 -1

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/vtd/iommu.c +13 -86

99 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +94 -0

94 lines not on this page · raw message

xen/include/asm-x86/iommu.h +8 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +32 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange/complete re-assignment handling

Prior to the assignment step having completed successfully, devices should not get associated with their new owner. Hand the device to DomIO (perhaps temporarily), until after the de-assignment step has completed.

De-assignment of a device (from other than Dom0) as well as failure of reassign_device() during assignment should result in unity mappings getting torn down. This in turn requires switching to a refcounted mapping approach, as was already used by VT-d for its RMRRs, to prevent unmapping a region used by multiple devices.

This is CVE-2021-28696 / part of XSA-378.

xen/drivers/passthrough/amd/iommu.h +4 -2

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/amd/iommu_map.c +37 -26

63 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +42 -12

54 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange exclusion range and unity map recording

The spec makes no provisions for OS behavior here to depend on the amount of RAM found on the system. While the spec may not sufficiently clearly distinguish both kinds of regions, they are surely meant to be separate things: Only regions with ACPI_IVMD_EXCLUSION_RANGE set should be candidates for putting in the exclusion range registers. (As there's only a single such pair of registers per IOMMU, secondary non-adjacent regions with the flag set already get converted to unity mapped regions.)

First of all, drop the dependency on max_page. With commit b4f042236ae0 ("AMD/IOMMU: Cease using a dynamic height for the IOMMU pagetables") the use of it here was stale anyway; it was bogus already before, as it didn't account for max_page getting increased later on. Simply try an exclusion range registration first, and if it fails (for being unsuitable or non-mergeable), register a unity mapping range.

With this various local variables become unnecessary and hence get dropped at the same time.

With the max_page boundary dropped for using unity maps, the minimum page table tree height now needs both recording and enforcing in amd_iommu_domain_init(). Since we can't predict which devices may get assigned to a domain, our only option is to uniformly force at least that height for all domains, now that the height isn't dynamic anymore.

Further don't make use of the exclusion range unless ACPI data says so.

Note that exclusion range registration in register_range_for_all_devices() is on a best effort basis. Hence unity map entries also registered are redundant when the former succeeded, but they also do no harm. Improvements in this area can be done later imo.

Also adjust types where suitable without touching extra lines.

This is part of XSA-378.

xen/drivers/passthrough/amd/iommu.h +2 -0

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/amd/iommu_acpi.c +82 -106

188 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +8 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: introduce p2m_is_special()

Seeing the similarity of grant, foreign, and (subsequently) direct-MMIO handling, introduce a new P2M type group named "special" (as in "needing special accessors to create/destroy").

Also use -EPERM instead of other error codes on the two domain_crash() paths touched.

This is part of XSA-378.

xen/arch/x86/mm/p2m.c +7 -8

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: guard (in particular) identity mapping entries

Such entries, created by set_identity_p2m_entry(), should only be destroyed by clear_identity_p2m_entry(). However, similarly, entries created by set_mmio_p2m_entry() should only be torn down by clear_mmio_p2m_entry(), so the logic gets based upon p2m_mmio_direct as the entry type (separation between "ordinary" and 1:1 mappings would require a further indicator to tell apart the two).

As to the guest_remove_page() change, commit 48dfb297a20a ("x86/PVH:
allow guest_remove_page to remove p2m_mmio_direct pages"), which introduced the call to clear_mmio_p2m_entry(), claimed this was done for hwdom only without this actually having been the case. However, this code shouldn't be there in the first place, as MMIO entries shouldn't be dropped this way. Avoid triggering the warning again that 48dfb297a20a silenced by an adjustment to xenmem_add_to_physmap_one() instead.

Note that guest_physmap_mark_populate_on_demand() gets tightened beyond the immediate purpose of this change.

Note also that I didn't inspect code which isn't security supported, e.g. sharing, paging, or altp2m.

This is CVE-2021-28694 / part of XSA-378.

xen/arch/x86/mm/p2m.c +9 -6

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m-pod.c +6 -6

Lines of this file are not shown. View the raw message.

xen/common/memory.c +10 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +2 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: IOMMU: generalize VT-d's tracking of mapped RMRR regions

In order to re-use it elsewhere, move the logic to vendor independent code and strip it of RMRR specifics.

Note that the prior "map" parameter gets folded into the new "p2ma" one (which AMD IOMMU code will want to make use of), assigning alternative meaning ("unmap") to p2m_access_x. Prepare set_identity_p2m_entry() and p2m_get_iommu_flags() for getting passed access types other than p2m_access_rw (in the latter case just for p2m_mmio_direct requests).

Note also that, to be on the safe side, an overlap check gets added to the main loop of iommu_identity_mapping().

This is part of XSA-378.

---
TBD: In this context (the latest) I question whether it is a good idea
for clear_identity_p2m_entry() to return success when it didn't clear the entry because of mismatching type or MFN.
TBD: Question is whether p2m_get_iommu_flags() should apply the access
restriction in all cases, not just for p2m_mmio_direct.
TBD: Neither possible placement of p2m_access_rx2rw and p2m_access_n2rwx
in p2m_access_to_iommu_flags() looks entirely correct/sensible.
---
v9: Re-base.
v7: s/unity_map/identity_map/g. Re-base.
v6: Re-base.
v5: New.

xen/arch/x86/mm/p2m.c +1 -1

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/vtd/iommu.c +13 -86

99 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +94 -0

94 lines not on this page · raw message

xen/include/asm-x86/iommu.h +8 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +32 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange/complete re-assignment handling

Prior to the assignment step having completed successfully, devices should not get associated with their new owner. Hand the device to DomIO (perhaps temporarily), until after the de-assignment step has completed.

De-assignment of a device (from other than Dom0) as well as failure of reassign_device() during assignment should result in unity mappings getting torn down. This in turn requires switching to a refcounted mapping approach, as was already used by VT-d for its RMRRs, to prevent unmapping a region used by multiple devices.

This is CVE-2021-28696 / part of XSA-378.

---
v9: Re-base.
v7: Re-base.
v6: Re-base.
v5: Switch to using iommu_identity_mapping(). Also unmap() when any
map() fails in amd_iommu_assign_device(), unless it's Dom0. Re-base.
v4: Use daddr_to_dfn(). Drop stray blank line. Don't ever unmap from

    Dom0.

v3: Use DomIO instead of DomXEN.
v2: Re-base over XSA-302.

xen/drivers/passthrough/amd/iommu.h +4 -2

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/amd/iommu_map.c +37 -26

63 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +42 -12

54 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-arrange exclusion range and unity map recording

The spec makes no provisions for OS behavior here to depend on the amount of RAM found on the system. While the spec may not sufficiently clearly distinguish both kinds of regions, they are surely meant to be separate things: Only regions with ACPI_IVMD_EXCLUSION_RANGE set should be candidates for putting in the exclusion range registers. (As there's only a single such pair of registers per IOMMU, secondary non-adjacent regions with the flag set already get converted to unity mapped regions.)

First of all, drop the dependency on max_page. With commit b4f042236ae0 ("AMD/IOMMU: Cease using a dynamic height for the IOMMU pagetables") the use of it here was stale anyway; it was bogus already before, as it didn't account for max_page getting increased later on. Simply try an exclusion range registration first, and if it fails (for being unsuitable or non-mergeable), register a unity mapping range.

With this various local variables become unnecessary and hence get dropped at the same time.

With the max_page boundary dropped for using unity maps, the minimum page table tree height now needs both recording and enforcing in amd_iommu_domain_init(). Since we can't predict which devices may get assigned to a domain, our only option is to uniformly force at least that height for all domains, now that the height isn't dynamic anymore.

Further don't make use of the exclusion range unless ACPI data says so.

Note that exclusion range registration in register_range_for_all_devices() is on a best effort basis. Hence unity map entries also registered are redundant when the former succeeded, but they also do no harm. Improvements in this area can be done later imo.

Also adjust types where suitable without touching extra lines.

This is part of XSA-378.

---
TBD: The adjustment done to amd_iommu_domain_init() would suggest that
it might be worth considering to use the exclusion range register for the highest reported (and suitable) range, rather than the first one found (however unlikely it might be to find multiple ones in the first place). That way we might be able to limit the page table tree height.
---
v9: Re-base.
v8: Re-base over adjustments to earlier patch.
v7: Don't use exclusion range when ACPI table entry doesn't say so.
v6: Re-base.
v5: New.
---
Backporting note: This depends on b75b3c62fe4a ("AMD/IOMMU: fix off-by-one in amd_iommu_get_paging_mode() callers").

xen/drivers/passthrough/amd/iommu.h +2 -0

Lines of this file are not shown. View the raw message.

xen/drivers/passthrough/amd/iommu_acpi.c +82 -106

188 lines not on this page · raw message

xen/drivers/passthrough/amd/pci_amd_iommu.c +8 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: introduce p2m_is_special()

Seeing the similarity of grant, foreign, and (subsequently) direct-MMIO handling, introduce a new P2M type group named "special" (as in "needing special accessors to create/destroy").

Also use -EPERM instead of other error codes on the two domain_crash() paths touched.

This is part of XSA-378.

---
v8: New, split from subsequent patch.

xen/arch/x86/mm/p2m.c +7 -8

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From: Jan Beulich <[email protected]>
Subject: x86/p2m: guard (in particular) identity mapping entries

Such entries, created by set_identity_p2m_entry(), should only be destroyed by clear_identity_p2m_entry(). However, similarly, entries created by set_mmio_p2m_entry() should only be torn down by clear_mmio_p2m_entry(), so the logic gets based upon p2m_mmio_direct as the entry type (separation between "ordinary" and 1:1 mappings would require a further indicator to tell apart the two).

As to the guest_remove_page() change, commit 48dfb297a20a ("x86/PVH:
allow guest_remove_page to remove p2m_mmio_direct pages"), which introduced the call to clear_mmio_p2m_entry(), claimed this was done for hwdom only without this actually having been the case. However, this code shouldn't be there in the first place, as MMIO entries shouldn't be dropped this way. Avoid triggering the warning again that 48dfb297a20a silenced by an adjustment to xenmem_add_to_physmap_one() instead.

Note that guest_physmap_mark_populate_on_demand() gets tightened beyond the immediate purpose of this change.

Note also that I didn't inspect code which isn't security supported, e.g. sharing, paging, or altp2m.

This is CVE-2021-28694 / part of XSA-378.

---
v8: Split off introduction of p2m_is_special().
v7: Re-base.
v6: Also adjust xenmem_add_to_physmap_one(). Fully drop p2m_mmio_direct
special handling from guest_remove_page(); just return an error. Make clear_mmio_p2m_entry() static.
v5: New.
---
Backporting note: While likely noticable by patch conflicts anyway, this depends on one or both of a6b051a87a58 ("x86/p2m: don't ignore p2m_remove_page()'s return value") and c65ea16dbcaf ("x86/p2m: don't assert that the passed in MFN matches for a remove").

xen/arch/x86/mm/p2m.c +9 -6

Lines of this file are not shown. View the raw message.

xen/arch/x86/mm/p2m-pod.c +6 -6

Lines of this file are not shown. View the raw message.

xen/common/memory.c +10 -1

Lines of this file are not shown. View the raw message.

xen/include/asm-x86/p2m.h +2 -11

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#1 lore raw
Quoted 17 lines · Xen.org security team

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Xen Security Advisory CVE-2021-28694,CVE-2021-28695,CVE-2021-28696 / XSA-378
version 3

IOMMU page mapping issues on x86

UPDATES IN VERSION 3
====================

Warn about dom0=pvh breakage in Resolution section.

ISSUE DESCRIPTION
=================

Quoted 8 lines

Both AMD and Intel allow ACPI tables to specify regions of memory which should be left untranslated, which typically means these addresses should pass the translation phase unaltered. While these are typically device specific ACPI properties, they can also be specified to apply to a range of devices, or even all devices.

On all systems with such regions Xen failed to prevent guests from undoing/replacing such mappings (CVE-2021-28694).

Hi,

Is there a way to identify if a system's ACPI tables have untranslated regions? Does it show up in xen or linux dmesg or can it be identified in sysfs?

Thanks,
Jason

#2 lore raw

(removing all lists inappropriate for a question like this one)

Quoted 24 lines · Jason Andryuk
Quoted 23 lines · Xen.org security team

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Xen Security Advisory CVE-2021-28694,CVE-2021-28695,CVE-2021-28696 / XSA-378
version 3

IOMMU page mapping issues on x86

UPDATES IN VERSION 3
====================

Warn about dom0=pvh breakage in Resolution section.

ISSUE DESCRIPTION
=================

Both AMD and Intel allow ACPI tables to specify regions of memory which should be left untranslated, which typically means these addresses should pass the translation phase unaltered. While these are typically device specific ACPI properties, they can also be specified to apply to a range of devices, or even all devices.

Quoted 6 lines
Quoted 2 lines

On all systems with such regions Xen failed to prevent guests from undoing/replacing such mappings (CVE-2021-28694).

Is there a way to identify if a system's ACPI tables have untranslated regions? Does it show up in xen or linux dmesg or can it be identified in sysfs?

For VT-d, "iommu=verbose" will cause ACPI table contents to get logged. For AMD you need to go one step further and set "iommu=debug". Obviously you'll want to be careful with enabling anything like this on production systems.

Jan

#3 lore raw
In reply to Jason Andryuk
Quoted 23 lines · Jason Andryuk
Quoted 22 lines · Xen.org security team

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Xen Security Advisory CVE-2021-28694,CVE-2021-28695,CVE-2021-28696 / XSA-378
version 3

IOMMU page mapping issues on x86

UPDATES IN VERSION 3
====================

Warn about dom0=pvh breakage in Resolution section.

ISSUE DESCRIPTION
=================

Both AMD and Intel allow ACPI tables to specify regions of memory which should be left untranslated, which typically means these addresses should pass the translation phase unaltered. While these are typically device specific ACPI properties, they can also be specified to apply to a range of devices, or even all devices.

Quoted 7 lines
Quoted 2 lines

On all systems with such regions Xen failed to prevent guests from undoing/replacing such mappings (CVE-2021-28694).

Hi,

Is there a way to identify if a system's ACPI tables have untranslated regions? Does it show up in xen or linux dmesg or can it be identified in sysfs?

It's possible, but a little convoluted to do.  In dom0 (and in an empty directory) you want:

acpidump > acpi.dmp
acpixtract -a acpi.dmp

On Intel, open up rmad.dat and hexedit the first 4 bytes from RMAD to DMAR (yes - really - this is how we stop the dom0 kernel from trying to poke the IOMMU directly.)

Then disassemble (iasl -d) either rmad.dat or ivrs.dat depending on whether you're on Intel or AMD.

On Intel, you're looking for Reserved Memory Regions, while on AMD you're looking for IVMD ranges (specifically, types 20 thru 22)

These, if present, describe a range of memory needing identity mapping, and a scope of the PCI device(s) the range applies to.

~Andrew