Skip to content

Commit ce8ea44

Browse files
awilliamSteve Conklin
authored andcommitted
KVM: unmap pages from the iommu when slots are removed
CVE-2012-2121 BugLink: http://bugs.launchpad.net/bugs/987569 commit 32f6daad4651a748a58a3ab6da0611862175722f upstream. We've been adding new mappings, but not destroying old mappings. This can lead to a page leak as pages are pinned using get_user_pages, but only unpinned with put_page if they still exist in the memslots list on vm shutdown. A memslot that is destroyed while an iommu domain is enabled for the guest will therefore result in an elevated page reference count that is never cleared. Additionally, without this fix, the iommu is only programmed with the first translation for a gpa. This can result in peer-to-peer errors if a mapping is destroyed and replaced by a new mapping at the same gpa as the iommu will still be pointing to the original, pinned memory address. Signed-off-by: Alex Williamson <[email protected]> Signed-off-by: Marcelo Tosatti <[email protected]> [bwh: Backported to 2.6.32: - Adjust context - In __kvm_set_memory_region(), call kvm_iommu_unmap_pages() immediately before kvm_free_physmem_slot() which cleans up the old memory slot. Make this dependent on CONFIG_DMAR, consistent with the use of kvm_iommu_map_pages().] Signed-off-by: Luis Henriques <[email protected]> Signed-off-by: Tim Gardner <[email protected]>
1 parent 7b6ab76 commit ce8ea44

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

include/linux/kvm_host.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
426426
#ifdef CONFIG_IOMMU_API
427427
int kvm_iommu_map_pages(struct kvm *kvm, gfn_t base_gfn,
428428
unsigned long npages);
429+
void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
429430
int kvm_iommu_map_guest(struct kvm *kvm);
430431
int kvm_iommu_unmap_guest(struct kvm *kvm);
431432
int kvm_assign_device(struct kvm *kvm,
@@ -440,6 +441,11 @@ static inline int kvm_iommu_map_pages(struct kvm *kvm,
440441
return 0;
441442
}
442443

444+
static inline void kvm_iommu_unmap_pages(struct kvm *kvm,
445+
struct kvm_memory_slot *slot)
446+
{
447+
}
448+
443449
static inline int kvm_iommu_map_guest(struct kvm *kvm)
444450
{
445451
return -ENODEV;

virt/kvm/iommu.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,17 @@ static void kvm_iommu_put_pages(struct kvm *kvm,
207207
iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages);
208208
}
209209

210+
void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
211+
{
212+
kvm_iommu_put_pages(kvm, slot->base_gfn, slot->npages);
213+
}
214+
210215
static int kvm_iommu_unmap_memslots(struct kvm *kvm)
211216
{
212217
int i;
213218

214219
for (i = 0; i < kvm->nmemslots; i++) {
215-
kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
216-
kvm->memslots[i].npages);
220+
kvm_iommu_unmap_pages(kvm, &kvm->memslots[i]);
217221
}
218222

219223
return 0;

virt/kvm/kvm_main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,12 @@ int __kvm_set_memory_region(struct kvm *kvm,
13381338
goto out_free;
13391339
}
13401340

1341+
#ifdef CONFIG_DMAR
1342+
/* unmap the pages in iommu page table */
1343+
if (!npages)
1344+
kvm_iommu_unmap_pages(kvm, &old);
1345+
#endif
1346+
13411347
kvm_free_physmem_slot(&old, npages ? &new : NULL);
13421348
/* Slot deletion case: we have to update the current slot */
13431349
spin_lock(&kvm->mmu_lock);

0 commit comments

Comments
 (0)