Skip to contentSkip to conversation

KVM: Add helper functions for accessing GDT/LDT

PATCH In review Martin Doucha 3 participants 19 messages Apr 21, 2023 – May 3, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 KVM: Add helper functions for accessing GDT/LDT

The fragmented structure of GDT and LDT requires helper functions to work with descriptors. Add the necessary structure definitions, constants and functions. Also increase GDT size to 32 descriptors for later use.

---
 doc/kvm-test-api.txt                    | 20 ++++++
 testcases/kernel/kvm/bootstrap_x86.S    |  3 +-
 testcases/kernel/kvm/bootstrap_x86_64.S |  3 +-
 testcases/kernel/kvm/include/kvm_x86.h  | 53 ++++++++++++++
 testcases/kernel/kvm/lib_x86.c          | 92 +++++++++++++++++++++++++
 5 files changed, 169 insertions(+), 2 deletions(-)
doc/kvm-test-api.txt +20 -0
index 25b72172d..f62819764 100644
@@ -343,6 +343,26 @@ Developer's Manual, Volume 3, Chapter 4 for explanation of the fields.
   the known position in page table hierarchy and `entry->page_type`. Returns
   zero if the `entry` does not reference any memory page.
 
+- `void kvm_set_segment_descriptor(struct segment_descriptor *dst, uint64_t baseaddr, uint32_t limit, unsigned int flags)` -
+  Fill the `dst` segment descriptor with given values. The maximum value
+  of `limit` is `0xfffff` (inclusive) regardless of `flags`.
+
+- `void kvm_parse_segment_descriptor(struct segment_descriptor *src, uint64_t *baseaddr, uint32_t *limit, unsigned int *flags)` -
+  Parse data in the `src` segment descriptor and copy them to variables
+  pointed to by the other arguments. Any parameter except the first one can
+  be `NULL`.
+
+- `int kvm_find_free_descriptor(const struct segment_descriptor *table, size_t size)` -
+  Find the first segment descriptor in `table` which does not have
+  the `SEGFLAG_PRESENT` bit set. The function handles double-size descriptors
+  correctly. Returns index of the first available descriptor or -1 if all
+  `size` descriptors are taken.
+
+- `unsigned int kvm_create_stack_descriptor(struct segment_descriptor *table, size_t tabsize, void *stack_base)` -
+  Convenience function for registering a stack segment descriptor. It'll
+  automatically find a free slot in `table` and fill the necessary flags.
+  The `stack_base` pointer must point to the bottom of the stack.
+
 - `void kvm_get_cpuid(unsigned int eax, unsigned int ecx,
   struct kvm_cpuid *buf)` – Executes the CPUID instruction with the given
   `eax` and `ecx` arguments and stores the results in `buf`.
testcases/kernel/kvm/bootstrap_x86.S +2 -1
index 1aaf0a4d1..3c17a2b47 100644
@@ -7,6 +7,7 @@
 
 .set KVM_TEXIT, 0xff
 .set RESULT_ADDRESS, 0xfffff000
+.set KVM_GDT_SIZE, 32
 
 /*
  * This section will be allocated at address 0x1000 and
@@ -44,7 +45,7 @@ kvm_gdt:
 	.8byte 0
 	gdt32_entry type=0x1a l=0 d=1 /* Code segment protected_mode, 32bits */
 	gdt32_entry type=0x12 /* Data segment, writable */
-	.skip 16 /* Stack and TSS segment descriptors */
+	.skip (KVM_GDT_SIZE-3)*8 /* Stack, TSS and other segment descriptors */
 
 .Lgdt_end:
 .global kvm_gdt_desc
testcases/kernel/kvm/bootstrap_x86_64.S +2 -1
index 0cffd5a12..3d8c49b10 100644
@@ -8,6 +8,7 @@
 .set KVM_TCONF, 32
 .set KVM_TEXIT, 0xff
 .set RESULT_ADDRESS, 0xfffff000
+.set KVM_GDT_SIZE, 32
 
 /*
  * This section will be allocated at address 0x1000 and
@@ -478,7 +479,7 @@ kvm_pgtable_l4:
 kvm_gdt:
 	.8byte 0
 	gdt32_entry type=0x1a l=1 limit=0 g=0 /* Code segment long mode */
-	.skip 16 /* TSS segment descriptor */
+	.skip (KVM_GDT_SIZE-2)*8 /* TSS and other segment descriptors */
 
 .Lgdt_end:
 .global kvm_gdt_desc
testcases/kernel/kvm/include/kvm_x86.h +53 -0

81 lines not on this page · raw message

testcases/kernel/kvm/lib_x86.c +92 -0

98 lines not on this page · raw message

Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 2/5 KVM: Add helper function for reading x86 segment registers

---
 doc/kvm-test-api.txt                    |  7 +++++++
 testcases/kernel/kvm/bootstrap_x86.S    | 19 +++++++++++++++++++
 testcases/kernel/kvm/bootstrap_x86_64.S | 16 ++++++++++++++++
 testcases/kernel/kvm/include/kvm_x86.h  |  5 +++++
 4 files changed, 47 insertions(+)
doc/kvm-test-api.txt +7 -0
index f62819764..0aede5eea 100644
@@ -329,6 +329,10 @@ struct kvm_cpuid {
 struct kvm_cregs {
 	unsigned long cr0, cr2, cr3, cr4;
 };
+
+struct kvm_sregs {
+	uint16_t cs, ds, es, fs, gs, ss;
+};
 -------------------------------------------------------------------------------
 
 `struct page_table_entry_pae` is the page table entry structure for PAE and
@@ -370,6 +374,9 @@ Developer's Manual, Volume 3, Chapter 4 for explanation of the fields.
 - `void kvm_read_cregs(struct kvm_cregs *buf)` – Copies the current values
   of control registers to `buf`.
 
+- `void kvm_read_sregs(struct kvm_sregs *buf)` - Copies the current values
+  of segment registers to `buf`.
+
 - `uint64_t kvm_rdmsr(unsigned int msr)` – Returns the current value
   of model-specific register `msr`.
 
testcases/kernel/kvm/bootstrap_x86.S +19 -0
index 3c17a2b47..89f73eba1 100644
@@ -197,6 +197,25 @@ kvm_read_cregs:
 	pop %edi
 	ret
 
+.global kvm_read_sregs
+kvm_read_sregs:
+	push %edi
+	mov 8(%esp), %edi
+	mov %cs, %ax
+	movw %ax, (%edi)
+	mov %ds, %ax
+	movw %ax, 2(%edi)
+	mov %es, %ax
+	movw %ax, 4(%edi)
+	mov %fs, %ax
+	movw %ax, 6(%edi)
+	mov %gs, %ax
+	movw %ax, 8(%edi)
+	mov %ss, %ax
+	movw %ax, 10(%edi)
+	pop %edi
+	ret
+
 handle_interrupt:
 	/* save CPU state */
 	push %ebp
testcases/kernel/kvm/bootstrap_x86_64.S +16 -0
index 3d8c49b10..e4d160b2e 100644
@@ -303,6 +303,22 @@ kvm_read_cregs:
 	mov %rax, 24(%rdi)
 	retq
 
+.global kvm_read_sregs
+kvm_read_sregs:
+	mov %cs, %ax
+	movw %ax, (%rdi)
+	mov %ds, %ax
+	movw %ax, 2(%rdi)
+	mov %es, %ax
+	movw %ax, 4(%rdi)
+	mov %fs, %ax
+	movw %ax, 6(%rdi)
+	mov %gs, %ax
+	movw %ax, 8(%rdi)
+	mov %ss, %ax
+	movw %ax, 10(%rdi)
+	retq
+
 handle_interrupt:
 	/* push CPU state */
 	push %rbp
testcases/kernel/kvm/include/kvm_x86.h +5 -0
index a655c9834..a94f05451 100644
@@ -158,6 +158,10 @@ struct kvm_cregs {
 	unsigned long cr0, cr2, cr3, cr4;
 };
 
+struct kvm_sregs {
+	uint16_t cs, ds, es, fs, gs, ss;
+};
+
 extern struct page_table_entry_pae kvm_pagetable[];
 extern struct intr_descriptor kvm_idt[X86_INTR_COUNT];
 extern struct segment_descriptor kvm_gdt[KVM_GDT_SIZE];
@@ -178,6 +182,7 @@ unsigned int kvm_create_stack_descriptor(struct segment_descriptor *table,
 /* Functions for querying CPU info and status */
 void kvm_get_cpuid(unsigned int eax, unsigned int ecx, struct kvm_cpuid *buf);
 void kvm_read_cregs(struct kvm_cregs *buf);
+void kvm_read_sregs(struct kvm_sregs *buf);
 uint64_t kvm_rdmsr(unsigned int msr);
 void kvm_wrmsr(unsigned int msr, uint64_t value);
 
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by on v1 2/5
#2 lore raw

Re: [LTP] [PATCH 2/5] KVM: Add helper function for reading x86 segment registers

Hi Martin,

(LGTM, but my knowledge here is very limited)

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 3/5 KVM: Fix infinite loop in ptr2hex()

In reply to Martin Doucha

Contrary to the C standard, (x >> 64) is equivalent to (x >> 0) on x86. This can cause infinite loop in ptr2hex() if the highest nibble in the second argument is non-zero. Use temporary variable to avoid bit-shifting by large values.

---
 testcases/kernel/kvm/lib_guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/kvm/lib_guest.c +1 -1
index d237293fc..d3b2ac3d5 100644
@@ -82,7 +82,7 @@ char *ptr2hex(char *dest, uintptr_t val)
 	uintptr_t tmp;
 	char *ret = dest;
 
-	for (i = 4; val >> i; i += 4)
+	for (i = 4, tmp = val >> 4; tmp; i += 4, tmp >>= 4)
 		;
 
 	do {
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 3/5
#4 lore raw

Re: [LTP] [PATCH 3/5] KVM: Fix infinite loop in ptr2hex()

Hi!

Quoted 1 line

Contrary to the C standard, (x >> 64) is equivalent to (x >> 0) on x86.

As far as I can tell right shift larger than the left operand size are undefined, at least in C99.

Other than that the patch looks good.

Quoted 28 lines

This can cause infinite loop in ptr2hex() if the highest nibble in the second argument is non-zero. Use temporary variable to avoid bit-shifting by large values.

---
 testcases/kernel/kvm/lib_guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/kvm/lib_guest.c +1 -1
index d237293fc..d3b2ac3d5 100644
@@ -82,7 +82,7 @@ char *ptr2hex(char *dest, uintptr_t val)
 	uintptr_t tmp;
 	char *ret = dest;
 
-	for (i = 4; val >> i; i += 4)
+	for (i = 4, tmp = val >> 4; tmp; i += 4, tmp >>= 4)
 		;
 
 	do {
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH 3/5] KVM: Fix infinite loop in ptr2hex()

Quoted 7 lines · Cyril Hrubis

Hi!

Quoted 1 line

Contrary to the C standard, (x >> 64) is equivalent to (x >> 0) on x86.

As far as I can tell right shift larger than the left operand size are undefined, at least in C99.

Other than that the patch looks good.

You're right, sorry. Should I send a v2 with fixed commit message?

Signature · 11 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH 3/5] KVM: Fix infinite loop in ptr2hex()

Hi Martin,

Thanks a lot, merged with amended commit message.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

PATCH 4/5 Add KVM helper functions for AMD SVM

In reply to Martin Doucha
---
 doc/kvm-test-api.txt                       |  73 ++++++++++
 testcases/kernel/kvm/bootstrap_x86.S       |  79 +++++++++++
 testcases/kernel/kvm/bootstrap_x86_64.S    |  89 ++++++++++++
 testcases/kernel/kvm/include/kvm_x86.h     |  14 ++
 testcases/kernel/kvm/include/kvm_x86_svm.h | 157 +++++++++++++++++++++
 testcases/kernel/kvm/lib_x86.c             | 155 +++++++++++++++++++-
 6 files changed, 566 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/kvm/include/kvm_x86_svm.h
doc/kvm-test-api.txt +73 -0
index 0aede5eea..812e12b38 100644
@@ -390,6 +390,79 @@ Developer's Manual, Volume 3, Chapter 4 for explanation of the fields.
 See Intel(R) 64 and IA-32 Architectures Software Developer's Manual
 for documentation of standard and model-specific x86 registers.
 
+3.5 AMD SVM helper functions
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+`#include "kvm_test.h"` +
+`#include "kvm_x86.h"` +
+`#include "kvm_x86_svm.h"`
+
+The KVM guest library provides basic helper functions for creating and running
+nested virtual machines using the AMD SVM technology.
+
+.Example code to execute nested VM
+[source,c]
+-------------------------------------------------------------------------------
+int guest_main(void)
+{
+	...
+	return 0;
+}
+
+void main(void)
+{
+	struct kvm_svm_vcpu *vm;
+
+	kvm_init_svm();
+	vm = kvm_create_svm_vcpu(guest_main, 1);
+	kvm_svm_vmrun(vm);
+}
+-------------------------------------------------------------------------------
+
+- `int kvm_is_svm_supported(void)` - Returns non-zero value if the CPU
+  supports AMD SVM, otherwise returns 0.
+
+- `int kvm_get_svm_state(void)` - Returns non-zero value if SVM is currently
+  enabled, otherwise returns 0.
+
+- `void kvm_set_svm_state(int enabled)` - Enable or disable SVM according
+  to argument. If SVM is disabled by host or not supported, the test will exit
+  with `TCONF`.
+
+- `void kvm_init_svm(void)` - Enable and fully initialize SVM, including
+  allocating and setting up host save area VMCB. If SVM is disabled by host or
+  not supported, the test will exit with `TCONF`.
+
+- `struct kvm_vmcb *kvm_alloc_vmcb(void)` - Allocate new VMCB structure
+  with correct memory alignment and fill it with zeroes.
+
+- `void kvm_vmcb_set_intercept(struct kvm_vmcb *vmcb, unsigned int id, unsigned int state)` -
+  Set SVM intercept bit `id` to given `state`.
+
+- `void kvm_init_guest_vmcb(struct kvm_vmcb *vmcb, uint32_t asid, uint16_t ss, void *rsp, int (*guest_main)(void))` -
+  Initialize new SVM virtual machine. The `asid` parameter is the nested
+  page table ID. The `ss` and `rsp` parameters set the stack segment and stack
+  pointer values, respectively. The `guest_main` parameter sets the code entry
+  point of the virtual machine. All control registers, segment registers
+  (except stack segment register), GDTR and IDTR will be copied
+  from the current CPU state.
+
+- `struct kvm_svm_vcpu *kvm_create_svm_vcpu(int (*guest_main)(void), int alloc_stack)` -
+  Convenience function for allocating and initializing new SVM virtual CPU.
+  The `guest_main` parameter is passed to `kvm_init_guest_vmcb()`,
+  the `alloc_stack` parameter controls whether a new 8KB stack will be
+  allocated and registered in GDT. Interception will be enabled for `VMSAVE`
+  and `HLT` instructions. If you set `alloc_stack` to zero, you must configure
+  the stack segment register and stack pointer manually.
+
+- `void kvm_svm_vmrun(struct kvm_svm_vcpu *cpu)` - Start or continue execution
+  of a nested virtual machine. Beware that FPU state is not saved.  Do not use
+  floating point types or values in nested guest code. Also do not use
+  `tst_res()` or `tst_brk()` functions in nested guest code.
+
+See AMD64 Architecture Programmer's Manual Volume 2 for documentation
+of the Secure Virtual Machine (SVM) technology.
+
 4. KVM guest environment
 ------------------------
 
testcases/kernel/kvm/bootstrap_x86.S +79 -0

91 lines not on this page · raw message

testcases/kernel/kvm/bootstrap_x86_64.S +89 -0

101 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_x86.h +14 -0
index a94f05451..bc36c0e0f 100644
@@ -64,16 +64,25 @@
 /* CPUID constants */
 #define CPUID_GET_INPUT_RANGE 0x80000000
 #define CPUID_GET_EXT_FEATURES 0x80000001
+#define CPUID_GET_SVM_FEATURES 0x8000000a
 
 
 /* Model-specific CPU register constants */
 #define MSR_EFER 0xc0000080
+#define MSR_VM_CR 0xc0010114
+#define MSR_VM_HSAVE_PA 0xc0010117
 
 #define EFER_SCE (1 << 0)	/* SYSCALL/SYSRET instructions enabled */
 #define EFER_LME (1 << 8)	/* CPU is running in 64bit mode */
 #define EFER_LMA (1 << 10)	/* CPU uses 64bit memory paging (read-only) */
 #define EFER_NXE (1 << 11)	/* Execute disable bit active */
+#define EFER_SVME (1 << 12)	/* AMD SVM instructions enabled */
 
+#define VM_CR_DPD (1 << 0)
+#define VM_CR_R_INIT (1 << 1)
+#define VM_CR_DIS_A20M (1 << 2)
+#define VM_CR_LOCK (1 << 3)
+#define VM_CR_SVMDIS (1 << 4)
 
 /* Control register constants */
 #define CR4_VME (1 << 0)
@@ -162,6 +171,11 @@ struct kvm_sregs {
 	uint16_t cs, ds, es, fs, gs, ss;
 };
 
+struct kvm_regs64 {
+	uint64_t rax, rbx, rcx, rdx, rdi, rsi, rbp, rsp;
+	uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
+};
+
 extern struct page_table_entry_pae kvm_pagetable[];
 extern struct intr_descriptor kvm_idt[X86_INTR_COUNT];
 extern struct segment_descriptor kvm_gdt[KVM_GDT_SIZE];
testcases/kernel/kvm/include/kvm_x86_svm.h new file +157 -0

157 lines not on this page · raw message

testcases/kernel/kvm/lib_x86.c +154 -1

164 lines not on this page · raw message

Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by on v1 4/5
#9 lore raw

Re: [LTP] [PATCH 4/5] Add KVM helper functions for AMD SVM

Hi Martin,

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

PATCH 5/5 Add test for CVE 2021-3653

In reply to Martin Doucha
---
 testcases/kernel/kvm/kvm_svm01.c | 123 +++++++++++++++++++++++++++++++
 1 file changed, 123 insertions(+)
 create mode 100644 testcases/kernel/kvm/kvm_svm01.c
testcases/kernel/kvm/kvm_svm01.c new file +123 -0
new file mode 100644
index 000000000..32d15526b
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2023 SUSE LLC
+ * Author: Nicolai Stange <[email protected]>
+ * LTP port: Martin Doucha <[email protected]>
+ */
+
+/*\
+ * CVE 2021-3653
+ *
+ * Check that KVM either blocks enabling virtual interrupt controller (AVIC)
+ * in nested VMs or correctly sets up the required memory address translation.
+ * If AVIC is enabled without address translation in the host kernel,
+ * the nested VM will be able to read and write an arbitraty physical memory
+ * page specified by the parent VM. Unauthorized memory access fixed in:
+ *
+ *  commit 0f923e07124df069ba68d8bb12324398f4b6b709
+ *  Author: Maxim Levitsky <[email protected]>
+ *  Date:   Thu Jul 15 01:56:24 2021 +0300
+ *
+ *  KVM: nSVM: avoid picking up unsupported bits from L2 in int_ctl (CVE-2021-3653)
+ */
+
+#include "kvm_test.h"
+
+#ifdef COMPILE_PAYLOAD
+#if defined(__i386__) || defined(__x86_64__)
+
+#include "kvm_x86_svm.h"
+
+#define AVIC_REG_ADDR 0x280
+#define AVIC_TEST_VAL 0xec
+#define AVIC_READ_FAIL 0x12ead
+
+#define AVIC_INFO_MASK ((1ULL << 32) | 0xff0)
+#define AVIC_INFO_EXP ((1ULL << 32) | AVIC_REG_ADDR)
+
+static uint32_t * const avic_ptr = (uint32_t *)AVIC_REG_ADDR;
+
+static int guest_main(void)
+{
+	if (*avic_ptr != 0xaaaaaaaa)
+		return AVIC_READ_FAIL;
+
+	*avic_ptr = AVIC_TEST_VAL;
+	return 0;
+}
+
+void main(void)
+{
+	struct kvm_svm_vcpu *vcpu;
+
+	kvm_init_svm();
+	vcpu = kvm_create_svm_vcpu(guest_main, 1);
+
+	/*
+	 * Enable AVIC and set both the AVIC base address (where the nested VM
+	 * will write) and backing page address (where the parent VM expects
+	 * to see the changes) to 0
+	 */
+	vcpu->vmcb->virt_intr_ctl |= SVM_INTR_AVIC;
+	vcpu->vmcb->avic_backing_page = 0;
+	vcpu->vmcb->avic_bar = 0;
+	memset((void *)8, 0xaa, PAGESIZE - 8);
+
+	/* Write into AVIC backing page in the nested VM */
+	kvm_svm_vmrun(vcpu);
+
+	switch (vcpu->vmcb->exitcode) {
+	case SVM_EXIT_HLT:
+		if (vcpu->vmcb->rax == AVIC_READ_FAIL) {
+			tst_res(TFAIL, "Nested VM can read host memory");
+			return;
+		}
+
+		if (vcpu->vmcb->rax)
+			tst_brk(TBROK, "Unexpected guest_main() return value");
+
+		break;
+
+	case SVM_EXIT_AVIC_NOACCEL:
+		if ((vcpu->vmcb->exitinfo1 & AVIC_INFO_MASK) == AVIC_INFO_EXP) {
+			tst_res(TPASS, "AVIC register write caused VMEXIT");
+			break;
+		}
+
+		/* unexpected exit, fall through */
+
+	default:
+		tst_brk(TBROK, "Nested VM exited unexpectedly");
+	}
+
+	if (*avic_ptr != AVIC_TEST_VAL) {
+		tst_res(TFAIL, "Write into AVIC ESR redirected to host memory");
+		return;
+	}
+
+	tst_res(TPASS, "Writes into AVIC backing page were not redirected");
+}
+
+#else /* defined(__i386__) || defined(__x86_64__) */
+TST_TEST_TCONF("Test supported only on x86");
+#endif /* defined(__i386__) || defined(__x86_64__) */
+
+#else /* COMPILE_PAYLOAD */
+
+static struct tst_test test = {
+	.test_all = tst_kvm_run,
+	.setup = tst_kvm_setup,
+	.cleanup = tst_kvm_cleanup,
+	.supported_archs = (const char *const []) {
+		"x86_64",
+		"x86",
+		NULL
+	},
+	.tags = (struct tst_tag[]){
+		{"linux-git", "0f923e07124d"},
+		{"CVE", "2021-3653"},
+		{}
+	}
+};
+
+#endif /* COMPILE_PAYLOAD */
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Re: [LTP] [PATCH 5/5] Add test for CVE 2021-3653

Sorry, I forgot to update .gitignore and add the test to the KVM runfile. I'll send a v2 for the last patch either when the first four patches get merged or together with any other fixes.

Signature · 11 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#12 lore raw

Re: [LTP] [PATCH 5/5] Add test for CVE 2021-3653

Hi Martin,

Quoted 3 lines

Sorry, I forgot to update .gitignore and add the test to the KVM runfile. I'll send a v2 for the last patch either when the first four patches get merged or together with any other fixes.

+1, please anybody who merges this don't forget to add 2 two changes.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

Re: [LTP] [PATCH 5/5] Add test for CVE 2021-3653

Hi Martin,

because Nicolai is busy, patches looks good and Cyril also acked them [1], I merge first four patches.

The test itself introduces warnings. Could you please have look if they could be easily fixed?

kvm_svm01.c: In function ‘guest_main’:
kvm_svm01.c:42:13: warning: array subscript 0 is outside array bounds of ‘uint32_t[0]’ {aka ‘unsigned int[]’} [-Warray-bounds]
42 | if (*avic_ptr != 0xaaaaaaaa)

      |             ^~~~~~~~~
kvm_svm01.c:45:9: warning: array subscript 0 is outside array bounds of ‘uint32_t[0]’ {aka ‘unsigned int[]’} [-Warray-bounds]
   45 |         *avic_ptr = AVIC_TEST_VAL;
      |         ^~~~~~~~~

kvm_svm01.c: In function ‘main’:
kvm_svm01.c:93:13: warning: array subscript 0 is outside array bounds of ‘uint32_t[0]’ {aka ‘unsigned int[]’} [-Warray-bounds]
93 | if (*avic_ptr != AVIC_TEST_VAL) {

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/ZFIp4Sulcy20GWkc@rei/

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw

Re: [LTP] [PATCH 5/5] Add test for CVE 2021-3653

In reply to Martin Doucha

Hi Martin,

Quoted 9 lines

+/*\
+ * CVE 2021-3653
+ *
+ * Check that KVM either blocks enabling virtual interrupt controller (AVIC)
+ * in nested VMs or correctly sets up the required memory address translation.
+ * If AVIC is enabled without address translation in the host kernel,
+ * the nested VM will be able to read and write an arbitraty physical memory
+ * page specified by the parent VM. Unauthorized memory access fixed in:
+ *

Quoted 5 lines
+ *  commit 0f923e07124df069ba68d8bb12324398f4b6b709
+ *  Author: Maxim Levitsky <[email protected]>
+ *  Date:   Thu Jul 15 01:56:24 2021 +0300

+ *
+ * KVM: nSVM: avoid picking up unsupported bits from L2 in int_ctl (CVE-2021-3653)

very nit: I'd prefer just:
0f923e07124d ("KVM: nSVM: avoid picking up unsupported bits from L2 in int_ctl (CVE-2021-3653)")

which looks better in docparse generated output.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#15 lore raw
In reply to Martin Doucha

Hi!
I did have a look at the patchset and I do not see anything wrong, but I cannot really review the code without spending a weeks reading the AMD CPU manuals. What about sending v2 with the runtest file change and CCing the [email protected] mailing list? Hopefully some of the kernel devs on that list will have a bit of time to check the actual correctness of the code...

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#16 lore raw
Quoted 7 lines · Cyril Hrubis

Hi!
I did have a look at the patchset and I do not see anything wrong, but I cannot really review the code without spending a weeks reading the AMD CPU manuals. What about sending v2 with the runtest file change and CCing the [email protected] mailing list? Hopefully some of the kernel devs on that list will have a bit of time to check the actual correctness of the code...

I've CC'd Nicolai who wrote the original KVM tests. If he approves, I'd say it'd good enough to merge. I've tested the code on AMD machines and it works and successfully reproduces the CVE on affected kernels.

Signature · 11 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#17 lore raw

Hi!

Quoted 10 lines
Quoted 6 lines

I did have a look at the patchset and I do not see anything wrong, but I cannot really review the code without spending a weeks reading the AMD CPU manuals. What about sending v2 with the runtest file change and CCing the [email protected] mailing list? Hopefully some of the kernel devs on that list will have a bit of time to check the actual correctness of the code...

I've CC'd Nicolai who wrote the original KVM tests. If he approves, I'd say it'd good enough to merge. I've tested the code on AMD machines and it works and successfully reproduces the CVE on affected kernels.

Fair enough, Nikoai can you add you Reviewed-by?

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp