linux-stable/arch/x86/kvm
Thomas Gleixner 5d8213864a x86/retbleed: Add SKL return thunk
To address the Intel SKL RSB underflow issue in software it's required to
do call depth tracking.

Provide a return thunk for call depth tracking on Intel SKL CPUs.

The tracking does not use a counter. It uses uses arithmetic shift
right on call entry and logical shift left on return.

The depth tracking variable is initialized to 0x8000.... when the call
depth is zero. The arithmetic shift right sign extends the MSB and
saturates after the 12th call. The shift count is 5 so the tracking covers
12 nested calls. On return the variable is shifted left logically so it
becomes zero again.

 CALL	 	   	RET
 0: 0x8000000000000000	0x0000000000000000
 1: 0xfc00000000000000	0xf000000000000000
...
11: 0xfffffffffffffff8	0xfffffffffffffc00
12: 0xffffffffffffffff	0xffffffffffffffe0

After a return buffer fill the depth is credited 12 calls before the next
stuffing has to take place.

There is a inaccuracy for situations like this:

   10 calls
    5 returns
    3 calls
    4 returns
    3 calls
    ....

The shift count might cause this to be off by one in either direction, but
there is still a cushion vs. the RSB depth. The algorithm does not claim to
be perfect, but it should obfuscate the problem enough to make exploitation
extremly difficult.

The theory behind this is:

RSB is a stack with depth 16 which is filled on every call. On the return
path speculation "pops" entries to speculate down the call chain. Once the
speculative RSB is empty it switches to other predictors, e.g. the Branch
History Buffer, which can be mistrained by user space and misguide the
speculation path to a gadget.

Call depth tracking is designed to break this speculation path by stuffing
speculation trap calls into the RSB which are never getting a corresponding
return executed. This stalls the prediction path until it gets resteered,

The assumption is that stuffing at the 12th return is sufficient to break
the speculation before it hits the underflow and the fallback to the other
predictors. Testing confirms that it works. Johannes, one of the retbleed
researchers. tried to attack this approach but failed.

There is obviously no scientific proof that this will withstand future
research progress, but all we can do right now is to speculate about it.

The SAR/SHL usage was suggested by Andi Kleen.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20220915111147.890071690@infradead.org
2022-10-17 16:41:15 +02:00
..
mmu The first batch of KVM patches, mostly covering x86, which I 2022-10-09 09:39:55 -07:00
svm x86/retbleed: Add SKL return thunk 2022-10-17 16:41:15 +02:00
vmx ARM: 2022-10-11 20:07:44 -07:00
cpuid.c The first batch of KVM patches, mostly covering x86, which I 2022-10-09 09:39:55 -07:00
cpuid.h KVM: x86: Add dedicated helper to get CPUID entry with significant index 2022-07-14 11:38:32 -04:00
debugfs.c KVM: x86: Introduce "struct kvm_caps" to track misc caps/settings 2022-06-08 05:21:16 -04:00
emulate.c The first batch of KVM patches, mostly covering x86, which I 2022-10-09 09:39:55 -07:00
fpu.h
hyperv.c KVM: nVMX: Support PERF_GLOBAL_CTRL with enlightened VMCS 2022-09-26 12:02:47 -04:00
hyperv.h KVM: x86: Report error when setting CPUID if Hyper-V allocation fails 2022-09-26 12:02:39 -04:00
i8254.c KVM: x86: PIT: Preserve state of speaker port data bit 2022-06-08 13:06:20 -04:00
i8254.h KVM: x86: PIT: Preserve state of speaker port data bit 2022-06-08 13:06:20 -04:00
i8259.c KVM: x86/i8259: Remove a dead store of irq in a conditional block 2022-04-02 05:41:19 -04:00
ioapic.c
ioapic.h
irq.c KVM: x86/xen: handle PV timers oneshot mode 2022-04-02 05:41:16 -04:00
irq.h
irq_comm.c KVM: x86/xen: Make kvm_xen_set_evtchn() reusable from other places 2022-04-02 05:41:14 -04:00
Kconfig KVM: x86: Select CONFIG_HAVE_KVM_DIRTY_RING_ACQ_REL 2022-09-29 10:23:08 +01:00
kvm_cache_regs.h
kvm_emulate.h KVM: x86: Bug the VM if the emulator accesses a non-existent GPR 2022-06-10 10:01:33 -04:00
kvm_onhyperv.c KVM: x86: Uninline and export hv_track_root_tdp() 2022-02-10 13:47:19 -05:00
kvm_onhyperv.h KVM: x86: Uninline and export hv_track_root_tdp() 2022-02-10 13:47:19 -05:00
lapic.c KVM: x86: Don't snapshot pending INIT/SIPI prior to checking nested events 2022-09-26 12:37:20 -04:00
lapic.h KVM: x86: Rename kvm_apic_has_events() to make it INIT/SIPI specific 2022-09-26 12:37:18 -04:00
Makefile
mmu.h KVM: SVM: Disable SEV-ES support if MMIO caching is disable 2022-08-10 15:08:25 -04:00
mtrr.c
pmu.c KVM: x86/pmu: Avoid using PEBS perf_events for normal counters 2022-09-28 12:47:22 -07:00
pmu.h Revert "KVM: x86: always allow host-initiated writes to PMU MSRs" 2022-06-20 11:49:46 -04:00
reverse_cpuid.h
trace.h KVM: x86: Print guest pgd in kvm_nested_vmenter() 2022-09-26 12:02:33 -04:00
tss.h
x86.c The first batch of KVM patches, mostly covering x86, which I 2022-10-09 09:39:55 -07:00
x86.h KVM: x86: Rename and expose helper to detect if INIT/SIPI are allowed 2022-09-26 12:37:18 -04:00
xen.c KVM: remove KVM_REQ_UNHALT 2022-09-26 12:37:21 -04:00
xen.h KVM: x86: do not set st->preempted when going back to user space 2022-06-08 04:21:06 -04:00