Skip to contentSkip to conversation

AMD/IOMMU: avoid UB in guest CR3 retrieval

PATCH In review Jan Beulich 2 participants 2 messages Nov 19, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 AMD/IOMMU: avoid UB in guest CR3 retrieval

Found by looking for patterns similar to the one Julien did spot in pci_vtd_quirks(). (Not that it matters much here, considering the code is dead right now.)

Fixes 3a7947b69011 ("amd-iommu: use a bitfield for DTE")
xen/drivers/passthrough/amd/iommu_guest.c +2 -1
@@ -70,7 +70,8 @@ static void guest_iommu_disable(struct g
 
 static uint64_t get_guest_cr3_from_dte(struct amd_iommu_dte *dte)
 {
-    return ((dte->gcr3_trp_51_31 << 31) | (dte->gcr3_trp_30_15 << 15) |
+    return (((uint64_t)dte->gcr3_trp_51_31 << 31) |
+            (dte->gcr3_trp_30_15 << 15) |
             (dte->gcr3_trp_14_12 << 12)) >> PAGE_SHIFT;
 }
 
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Jan Beulich

Found by looking for patterns similar to the one Julien did spot in pci_vtd_quirks(). (Not that it matters much here, considering the code is dead right now.)

Fixes 3a7947b69011 ("amd-iommu: use a bitfield for DTE")

There is *still* an outstanding regression (modulo dead code) in one of these bitfield-ifications which is off by 12, but I can't remember if it is this one or not.

Quoted 13 lines
xen/drivers/passthrough/amd/iommu_guest.c +2 -1
@@ -70,7 +70,8 @@ static void guest_iommu_disable(struct g
 
 static uint64_t get_guest_cr3_from_dte(struct amd_iommu_dte *dte)
 {
-    return ((dte->gcr3_trp_51_31 << 31) | (dte->gcr3_trp_30_15 << 15) |
+    return (((uint64_t)dte->gcr3_trp_51_31 << 31) |
+            (dte->gcr3_trp_30_15 << 15) |
             (dte->gcr3_trp_14_12 << 12)) >> PAGE_SHIFT;
 }