1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2023 SUSE LLC
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Nicolai Stange <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker * LTP port: Martin Doucha <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * CVE 2021-3653
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Check that KVM either blocks enabling virtual interrupt controller (AVIC)
12*49cdfc7eSAndroid Build Coastguard Worker * in nested VMs or correctly sets up the required memory address translation.
13*49cdfc7eSAndroid Build Coastguard Worker * If AVIC is enabled without address translation in the host kernel,
14*49cdfc7eSAndroid Build Coastguard Worker * the nested VM will be able to read and write an arbitraty physical memory
15*49cdfc7eSAndroid Build Coastguard Worker * page specified by the parent VM. Unauthorized memory access fixed in:
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker * commit 0f923e07124df069ba68d8bb12324398f4b6b709
18*49cdfc7eSAndroid Build Coastguard Worker * Author: Maxim Levitsky <[email protected]>
19*49cdfc7eSAndroid Build Coastguard Worker * Date: Thu Jul 15 01:56:24 2021 +0300
20*49cdfc7eSAndroid Build Coastguard Worker *
21*49cdfc7eSAndroid Build Coastguard Worker * KVM: nSVM: avoid picking up unsupported bits from L2 in int_ctl (CVE-2021-3653)
22*49cdfc7eSAndroid Build Coastguard Worker */
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker #include "kvm_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker #ifdef COMPILE_PAYLOAD
27*49cdfc7eSAndroid Build Coastguard Worker #if defined(__i386__) || defined(__x86_64__)
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #include "kvm_x86_svm.h"
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #define AVIC_REG_ADDR 0x280
32*49cdfc7eSAndroid Build Coastguard Worker #define AVIC_TEST_VAL 0xec
33*49cdfc7eSAndroid Build Coastguard Worker #define AVIC_READ_FAIL 0x12ead
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker #define AVIC_INFO_MASK ((1ULL << 32) | 0xff0)
36*49cdfc7eSAndroid Build Coastguard Worker #define AVIC_INFO_EXP ((1ULL << 32) | AVIC_REG_ADDR)
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker static uint32_t * const avic_ptr = (uint32_t *)AVIC_REG_ADDR;
39*49cdfc7eSAndroid Build Coastguard Worker
guest_main(void)40*49cdfc7eSAndroid Build Coastguard Worker static int guest_main(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker if (*avic_ptr != 0xaaaaaaaa)
43*49cdfc7eSAndroid Build Coastguard Worker return AVIC_READ_FAIL;
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker *avic_ptr = AVIC_TEST_VAL;
46*49cdfc7eSAndroid Build Coastguard Worker return 0;
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker
main(void)49*49cdfc7eSAndroid Build Coastguard Worker void main(void)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker struct kvm_svm_vcpu *vcpu;
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker kvm_init_svm();
54*49cdfc7eSAndroid Build Coastguard Worker vcpu = kvm_create_svm_vcpu(guest_main, 1);
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker /*
57*49cdfc7eSAndroid Build Coastguard Worker * Enable AVIC and set both the AVIC base address (where the nested VM
58*49cdfc7eSAndroid Build Coastguard Worker * will write) and backing page address (where the parent VM expects
59*49cdfc7eSAndroid Build Coastguard Worker * to see the changes) to 0
60*49cdfc7eSAndroid Build Coastguard Worker */
61*49cdfc7eSAndroid Build Coastguard Worker vcpu->vmcb->virt_intr_ctl |= SVM_INTR_AVIC;
62*49cdfc7eSAndroid Build Coastguard Worker vcpu->vmcb->avic_backing_page = 0;
63*49cdfc7eSAndroid Build Coastguard Worker vcpu->vmcb->avic_bar = 0;
64*49cdfc7eSAndroid Build Coastguard Worker memset((void *)8, 0xaa, PAGESIZE - 8);
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker /* Write into AVIC backing page in the nested VM */
67*49cdfc7eSAndroid Build Coastguard Worker kvm_svm_vmrun(vcpu);
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker switch (vcpu->vmcb->exitcode) {
70*49cdfc7eSAndroid Build Coastguard Worker case SVM_EXIT_HLT:
71*49cdfc7eSAndroid Build Coastguard Worker if (vcpu->vmcb->rax == AVIC_READ_FAIL) {
72*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Nested VM can read host memory");
73*49cdfc7eSAndroid Build Coastguard Worker return;
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker if (vcpu->vmcb->rax)
77*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Unexpected guest_main() return value");
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker break;
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker case SVM_EXIT_AVIC_NOACCEL:
82*49cdfc7eSAndroid Build Coastguard Worker if ((vcpu->vmcb->exitinfo1 & AVIC_INFO_MASK) == AVIC_INFO_EXP) {
83*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "AVIC register write caused VMEXIT");
84*49cdfc7eSAndroid Build Coastguard Worker break;
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker /* unexpected exit, fall through */
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker default:
90*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Nested VM exited unexpectedly");
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker if (*avic_ptr != AVIC_TEST_VAL) {
94*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Write into AVIC ESR redirected to host memory");
95*49cdfc7eSAndroid Build Coastguard Worker return;
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Writes into AVIC backing page were not redirected");
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker #else /* defined(__i386__) || defined(__x86_64__) */
102*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("Test supported only on x86");
103*49cdfc7eSAndroid Build Coastguard Worker #endif /* defined(__i386__) || defined(__x86_64__) */
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker #else /* COMPILE_PAYLOAD */
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
108*49cdfc7eSAndroid Build Coastguard Worker .test_all = tst_kvm_run,
109*49cdfc7eSAndroid Build Coastguard Worker .setup = tst_kvm_setup,
110*49cdfc7eSAndroid Build Coastguard Worker .cleanup = tst_kvm_cleanup,
111*49cdfc7eSAndroid Build Coastguard Worker .supported_archs = (const char *const []) {
112*49cdfc7eSAndroid Build Coastguard Worker "x86_64",
113*49cdfc7eSAndroid Build Coastguard Worker "x86",
114*49cdfc7eSAndroid Build Coastguard Worker NULL
115*49cdfc7eSAndroid Build Coastguard Worker },
116*49cdfc7eSAndroid Build Coastguard Worker .tags = (struct tst_tag[]){
117*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "0f923e07124d"},
118*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2021-3653"},
119*49cdfc7eSAndroid Build Coastguard Worker {}
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker };
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker #endif /* COMPILE_PAYLOAD */
124