Skip to content

Commit 46e868d

Browse files
committed
KVM: SVM: Flush pages under kvm->lock to fix UAF in svm_register_enc_region()
jira LE-1907 cve CVE-2024-35791 Rebuild_History Non-Buildable kernel-5.14.0-427.35.1.el9_4 commit-author Sean Christopherson <seanjc@google.com> commit 5ef1d8c Do the cache flush of converted pages in svm_register_enc_region() before dropping kvm->lock to fix use-after-free issues where region and/or its array of pages could be freed by a different task, e.g. if userspace has __unregister_enc_region_locked() already queued up for the region. Note, the "obvious" alternative of using local variables doesn't fully resolve the bug, as region->pages is also dynamically allocated. I.e. the region structure itself would be fine, but region->pages could be freed. Flushing multiple pages under kvm->lock is unfortunate, but the entire flow is a rare slow path, and the manual flush is only needed on CPUs that lack coherency for encrypted memory. Fixes: 19a23da ("Fix unsynchronized access to sev members through svm_register_enc_region") Reported-by: Gabe Kirkpatrick <gkirkpatrick@google.com> Cc: Josh Eads <josheads@google.com> Cc: Peter Gonda <pgonda@google.com> Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20240217013430.2079561-1-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> (cherry picked from commit 5ef1d8c) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent db691d9 commit 46e868d

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,20 +1978,22 @@ int sev_mem_enc_register_region(struct kvm *kvm,
19781978
goto e_free;
19791979
}
19801980

1981-
region->uaddr = range->addr;
1982-
region->size = range->size;
1983-
1984-
list_add_tail(&region->list, &sev->regions_list);
1985-
mutex_unlock(&kvm->lock);
1986-
19871981
/*
19881982
* The guest may change the memory encryption attribute from C=0 -> C=1
19891983
* or vice versa for this memory range. Lets make sure caches are
19901984
* flushed to ensure that guest data gets written into memory with
1991-
* correct C-bit.
1985+
* correct C-bit. Note, this must be done before dropping kvm->lock,
1986+
* as region and its array of pages can be freed by a different task
1987+
* once kvm->lock is released.
19921988
*/
19931989
sev_clflush_pages(region->pages, region->npages);
19941990

1991+
region->uaddr = range->addr;
1992+
region->size = range->size;
1993+
1994+
list_add_tail(&region->list, &sev->regions_list);
1995+
mutex_unlock(&kvm->lock);
1996+
19951997
return ret;
19961998

19971999
e_free:

0 commit comments

Comments
 (0)