[PATCH 3.16 80/99] KVM: x86: Protect kvm_lapic_reg_write() from Spectre-v1/L1TF attacks

From: Ben Hutchings
Date: Wed May 20 2020 - 10:25:13 EST


3.16.84-rc1 review patch. If anyone has any objections, please let me know.

------------------

From: Marios Pomonis <pomonis@xxxxxxxxxx>

commit 4bf79cb089f6b1c6c632492c0271054ce52ad766 upstream.

This fixes a Spectre-v1/L1TF vulnerability in kvm_lapic_reg_write().
This function contains index computations based on the
(attacker-controlled) MSR number.

Fixes: 0105d1a52640 ("KVM: x2apic interface to lapic")

Signed-off-by: Nick Finco <nifi@xxxxxxxxxx>
Signed-off-by: Marios Pomonis <pomonis@xxxxxxxxxx>
Reviewed-by: Andrew Honig <ahonig@xxxxxxxxxx>
Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
[bwh: Backported to 3.16:
- Add #include <linux/nospec.h>
- Adjust context]
Signed-off-by: Ben Hutchings <ben@xxxxxxxxxxxxxxx>
---
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -35,6 +35,7 @@
#include <asm/apicdef.h>
#include <linux/atomic.h>
#include <linux/jump_label.h>
+#include <linux/nospec.h>
#include "kvm_cache_regs.h"
#include "irq.h"
#include "trace.h"
@@ -1196,15 +1197,20 @@ static int apic_reg_write(struct kvm_lap
case APIC_LVTTHMR:
case APIC_LVTPC:
case APIC_LVT1:
- case APIC_LVTERR:
+ case APIC_LVTERR: {
/* TODO: Check vector */
+ size_t size;
+ u32 index;
+
if (!kvm_apic_sw_enabled(apic))
val |= APIC_LVT_MASKED;
-
- val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
+ size = ARRAY_SIZE(apic_lvt_mask);
+ index = array_index_nospec(
+ (reg - APIC_LVTT) >> 4, size);
+ val &= apic_lvt_mask[index];
apic_set_reg(apic, reg, val);
-
break;
+ }

case APIC_LVTT:
if ((kvm_apic_get_reg(apic, APIC_LVTT) &