[PATCH 1/2] x86/hyperv: Fix NULL deref in set_hv_tscchange_cb() if Hyper-V setup fails

From: Sean Christopherson
Date: Thu Oct 28 2021 - 18:22:03 EST


Check for re-enlightenment support and for a valid hv_vp_index array
prior to derefencing hv_vp_index when setting Hyper-V's TSC change
callback. If Hyper-V setup failed in hyperv_init(), e.g. because of a
bad VMM config that doesn't advertise the HYPERCALL MSR, the kernel will
still report that it's running under Hyper-V, but will have silently
disabled nearly all functionality.

BUG: kernel NULL pointer dereference, address: 0000000000000010
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
PGD 0 P4D 0
Oops: 0000 [#1] SMP
CPU: 4 PID: 1 Comm: swapper/0 Not tainted 5.15.0-rc2+ #75
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
RIP: 0010:set_hv_tscchange_cb+0x15/0xa0
Code: <8b> 04 82 8b 15 12 17 85 01 48 c1 e0 20 48 0d ee 00 01 00 f6 c6 08
...
Call Trace:
kvm_arch_init+0x17c/0x280
kvm_init+0x31/0x330
vmx_init+0xba/0x13a
do_one_initcall+0x41/0x1c0
kernel_init_freeable+0x1f2/0x23b
kernel_init+0x16/0x120
ret_from_fork+0x22/0x30

Fixes: 93286261de1b ("x86/hyperv: Reenlightenment notifications support")
Cc: stable@xxxxxxxxxxxxxxx
Cc: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/hyperv/hv_init.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
index 708a2712a516..6cc845c026d4 100644
--- a/arch/x86/hyperv/hv_init.c
+++ b/arch/x86/hyperv/hv_init.c
@@ -139,7 +139,7 @@ void set_hv_tscchange_cb(void (*cb)(void))
struct hv_reenlightenment_control re_ctrl = {
.vector = HYPERV_REENLIGHTENMENT_VECTOR,
.enabled = 1,
- .target_vp = hv_vp_index[smp_processor_id()]
+ .target_vp = -1,
};
struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};

@@ -148,6 +148,11 @@ void set_hv_tscchange_cb(void (*cb)(void))
return;
}

+ if (!hv_vp_index)
+ return;
+
+ re_ctrl.target_vp = hv_vp_index[smp_processor_id()];
+
hv_reenlightenment_cb = cb;

/* Make sure callback is registered before we write to MSRs */
--
2.33.0.1079.g6e70778dc9-goog