linux/arch/x86
Mingwei Zhang a60babeb60 KVM: SEV: add cache flush to solve SEV cache incoherency issues
commit 683412ccf6 upstream.

Flush the CPU caches when memory is reclaimed from an SEV guest (where
reclaim also includes it being unmapped from KVM's memslots).  Due to lack
of coherency for SEV encrypted memory, failure to flush results in silent
data corruption if userspace is malicious/broken and doesn't ensure SEV
guest memory is properly pinned and unpinned.

Cache coherency is not enforced across the VM boundary in SEV (AMD APM
vol.2 Section 15.34.7). Confidential cachelines, generated by confidential
VM guests have to be explicitly flushed on the host side. If a memory page
containing dirty confidential cachelines was released by VM and reallocated
to another user, the cachelines may corrupt the new user at a later time.

KVM takes a shortcut by assuming all confidential memory remain pinned
until the end of VM lifetime. Therefore, KVM does not flush cache at
mmu_notifier invalidation events. Because of this incorrect assumption and
the lack of cache flushing, malicous userspace can crash the host kernel:
creating a malicious VM and continuously allocates/releases unpinned
confidential memory pages when the VM is running.

Add cache flush operations to mmu_notifier operations to ensure that any
physical memory leaving the guest VM get flushed. In particular, hook
mmu_notifier_invalidate_range_start and mmu_notifier_release events and
flush cache accordingly. The hook after releasing the mmu lock to avoid
contention with other vCPUs.

Cc: stable@vger.kernel.org
Suggested-by: Sean Christpherson <seanjc@google.com>
Reported-by: Mingwei Zhang <mizhang@google.com>
Signed-off-by: Mingwei Zhang <mizhang@google.com>
Message-Id: <20220421031407.2516575-4-mizhang@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[OP: applied kvm_arch_guest_memory_reclaimed() calls in kvm_set_memslot() and
kvm_mmu_notifier_invalidate_range_start();
OP: adjusted kvm_arch_guest_memory_reclaimed() to not use static_call_cond()]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
Reviewed-by: Liam Merwick <liam.merwick@oracle.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-09-28 11:10:28 +02:00
..
boot x86: link vdso and boot with -z noexecstack --no-warn-rwx-segments 2022-08-21 15:15:18 +02:00
configs x86/kbuild: Enable CONFIG_KALLSYMS_ALL=y in the defconfigs 2022-01-27 10:54:21 +01:00
crypto crypto: x86/poly1305 - Fixup SLS 2022-07-25 11:26:32 +02:00
entry x86/entry: Build thunk_$(BITS) only if CONFIG_PREEMPTION=y 2022-08-21 15:16:13 +02:00
events perf/x86/intel/uncore: Fix broken read_counter() for SNB IMC PMU 2022-08-31 17:15:24 +02:00
hyperv x86/hyperv: Fix NULL deref in set_hv_tscchange_cb() if Hyper-V setup fails 2021-11-26 10:39:19 +01:00
ia32
include KVM: SEV: add cache flush to solve SEV cache incoherency issues 2022-09-28 11:10:28 +02:00
kernel x86/ftrace: Use alternative RET encoding 2022-09-20 12:38:33 +02:00
kvm KVM: SEV: add cache flush to solve SEV cache incoherency issues 2022-09-28 11:10:28 +02:00
lib x86/retbleed: Add fine grained Kconfig knobs 2022-07-25 11:26:50 +02:00
math-emu x86: Prepare asm files for straight-line-speculation 2022-07-25 11:26:28 +02:00
mm x86/mm: Use proper mask when setting PUD mapping 2022-08-25 11:37:49 +02:00
net x86/bpf: Use alternative RET encoding 2022-07-25 11:26:38 +02:00
oprofile
pci x86/pci/xen: Disable PCI/MSI[-X] masking for XEN_HVM guests 2022-05-09 09:05:00 +02:00
platform x86/olpc: fix 'logical not is only applied to the left hand side' 2022-08-21 15:16:14 +02:00
power x86: Prepare asm files for straight-line-speculation 2022-07-25 11:26:28 +02:00
purgatory
ras
realmode x86/mm: Flush global TLB when switching to trampoline page-table 2022-01-27 10:54:14 +01:00
tools x86/tools: Fix objdump version check again 2021-08-18 08:59:15 +02:00
um x86/entry: Build thunk_$(BITS) only if CONFIG_PREEMPTION=y 2022-08-21 15:16:13 +02:00
video
xen x86/xen: Fix initialisation in hypercall_page after rethunk 2022-07-25 11:26:52 +02:00
.gitignore
Kbuild
Kconfig x86/speculation: Make all RETbleed mitigations 64-bit only 2022-08-11 13:06:44 +02:00
Kconfig.assembler
Kconfig.cpu
Kconfig.debug
Makefile x86/realmode: build with -D__DISABLE_EXPORTS 2022-07-25 11:26:33 +02:00
Makefile_32.cpu
Makefile.um