Skip to contentSkip to conversation

Two AMD SVM vulnerability tests

PATCH Superseded Martin Doucha 3 participants 27 messages May 17, 2023 – May 23, 2023 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Two AMD SVM vulnerability tests

Here are two more kernel vulnerability tests for AMD SVM:

kvm_svm02 checks that the host kernel intercepts VMLOAD and VMSAVE instructions in nested VMs and translates guest addresses to the correct physical address. Without the translation, nested SVM guest can read and write part of an arbitrary physical memory page chosen by the parent VM.

The test tries to detect the CVE bug harmlessly at first by using VMLOAD to read a buffer full of zeroes. If it finds any non-zero bytes in the VMCB fields accessed by VMLOAD/VMSAVE, it'll fail because they must have come from host memory due to missing address translation.

Since the harmless approach can produce false negatives, it'll also try a destructive approach and write into memory using VMSAVE. If the bug is present, the byte pattern in the destination buffer will not change and the test will reliably fail.

kvm_svm03 checks that the host kernel correctly handles global interrupt flag in nested VMs. Otherwise a malicious VM could lock up all CPUs assigned to it, causing a limited denial of service attack.

The test needs synchronization between the VM and another host thread so the patchset includes the necessary helper functions.

Martin Doucha (7):
KVM: Add VMSAVE/VMLOAD intercept constants
Add test for CVE 2021-3656
lib: Add safe functions for pthread_kill() and mutexes
KVM: Add async communication helper functions
KVM: Allow expected KVM_RUN errors in tst_kvm_run_instance()
KVM: Add STGI/CLGI intercept constants
Add KVM test for CPU lockup through malicous SVM guest

 doc/kvm-test-api.txt                       |   9 +-
 include/tst_safe_pthread.h                 |  52 +++++++
 lib/safe_pthread.c                         | 161 ++++++++++++++++++++
 runtest/kvm                                |   2 +
 testcases/kernel/kvm/.gitignore            |   2 +
 testcases/kernel/kvm/Makefile              |   3 +
 testcases/kernel/kvm/include/kvm_common.h  |   8 +
 testcases/kernel/kvm/include/kvm_guest.h   |  14 ++
 testcases/kernel/kvm/include/kvm_host.h    |  21 ++-
 testcases/kernel/kvm/include/kvm_x86_svm.h |   7 +
 testcases/kernel/kvm/kvm_svm02.c           | 159 ++++++++++++++++++++
 testcases/kernel/kvm/kvm_svm03.c           | 164 +++++++++++++++++++++
 testcases/kernel/kvm/lib_guest.c           |  16 ++
 testcases/kernel/kvm/lib_host.c            |  46 +++++-
 14 files changed, 656 insertions(+), 8 deletions(-)
 create mode 100644 testcases/kernel/kvm/kvm_svm02.c
 create mode 100644 testcases/kernel/kvm/kvm_svm03.c
Signature · 5 lines
2.40.0


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

PATCH 1/7 KVM: Add VMSAVE/VMLOAD intercept constants

---
 testcases/kernel/kvm/include/kvm_x86_svm.h | 5 +++++
 1 file changed, 5 insertions(+)
testcases/kernel/kvm/include/kvm_x86_svm.h +5 -0
index 965d1e716..3eb832849 100644
@@ -37,10 +37,15 @@
 /* SVM event intercept IDs */
 #define SVM_INTERCEPT_HLT 0x78
 #define SVM_INTERCEPT_VMRUN 0x80
+#define SVM_INTERCEPT_VMLOAD 0x82
+#define SVM_INTERCEPT_VMSAVE 0x83
 #define SVM_INTERCEPT_MAX 0x95
 
 /* SVM vmrun exit codes */
 #define SVM_EXIT_HLT 0x78
+#define SVM_EXIT_VMRUN 0x80
+#define SVM_EXIT_VMLOAD 0x82
+#define SVM_EXIT_VMSAVE 0x83
 #define SVM_EXIT_AVIC_NOACCEL 0x402
 #define SVM_EXIT_INVALID ((uint64_t)-1)
 
Signature · 5 lines
2.40.0


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

Re: [LTP] [PATCH 1/7] KVM: Add VMSAVE/VMLOAD intercept constants

Hi Martin,

Quoted 12 lines
---
 testcases/kernel/kvm/include/kvm_x86_svm.h | 5 +++++
 1 file changed, 5 insertions(+)
testcases/kernel/kvm/include/kvm_x86_svm.h +0 -0
index 965d1e716..3eb832849 100644
@@ -37,10 +37,15 @@
 /* SVM event intercept IDs */
 #define SVM_INTERCEPT_HLT 0x78

testcases/kernel/kvm/include/kvm_x86_svm.h line 48

Quoted 10 lines
testcases/kernel/kvm/include/kvm_x86_svm.h +5 -0
 #define SVM_INTERCEPT_VMRUN 0x80
+#define SVM_INTERCEPT_VMLOAD 0x82
+#define SVM_INTERCEPT_VMSAVE 0x83
 #define SVM_INTERCEPT_MAX 0x95
 
 /* SVM vmrun exit codes */
 #define SVM_EXIT_HLT 0x78
+#define SVM_EXIT_VMRUN 0x80
+#define SVM_EXIT_VMLOAD 0x82
+#define SVM_EXIT_VMSAVE 0x83

It looks like these three aren't used anywhere.

Kind regards,
Petr

Quoted 2 lines
testcases/kernel/kvm/include/kvm_x86_svm.h +0 -0
 #define SVM_EXIT_AVIC_NOACCEL 0x402
 #define SVM_EXIT_INVALID ((uint64_t)-1)
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH 1/7] KVM: Add VMSAVE/VMLOAD intercept constants

Quoted 19 lines · Petr Vorel

Hi Martin,

Quoted 16 lines
---
  testcases/kernel/kvm/include/kvm_x86_svm.h | 5 +++++
  1 file changed, 5 insertions(+)
testcases/kernel/kvm/include/kvm_x86_svm.h +2 -0
index 965d1e716..3eb832849 100644
@@ -37,10 +37,15 @@
  /* SVM event intercept IDs */
  #define SVM_INTERCEPT_HLT 0x78
  #define SVM_INTERCEPT_VMRUN 0x80
+#define SVM_INTERCEPT_VMLOAD 0x82
+#define SVM_INTERCEPT_VMSAVE 0x83
  #define SVM_INTERCEPT_MAX 0x95
Quoted 6 lines

testcases/kernel/kvm/include/kvm_x86_svm.h line 48

Quoted 5 lines
testcases/kernel/kvm/include/kvm_x86_svm.h +3 -0
  /* SVM vmrun exit codes */
  #define SVM_EXIT_HLT 0x78
+#define SVM_EXIT_VMRUN 0x80
+#define SVM_EXIT_VMLOAD 0x82
+#define SVM_EXIT_VMSAVE 0x83

It looks like these three aren't used anywhere.

Yes, the SVM_EXIT_* constants are not used. I originally wanted to use them in one of the checks but then decided against it. But let's keep them anyway since they'll be needed to handle the intercepts in future tests.

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
Acked-by
#4 lore raw

Re: [LTP] [PATCH 1/7] KVM: Add VMSAVE/VMLOAD intercept constants

...

Quoted 10 lines
Quoted 6 lines

testcases/kernel/kvm/include/kvm_x86_svm.h line 48

Quoted 5 lines
  /* SVM vmrun exit codes */
  #define SVM_EXIT_HLT 0x78
testcases/kernel/kvm/include/kvm_x86_svm.h +3 -0
+#define SVM_EXIT_VMRUN 0x80
+#define SVM_EXIT_VMLOAD 0x82
+#define SVM_EXIT_VMSAVE 0x83

It looks like these three aren't used anywhere.

Yes, the SVM_EXIT_* constants are not used. I originally wanted to use them in one of the checks but then decided against it. But let's keep them anyway since they'll be needed to handle the intercepts in future tests.

Sure, no problem.

Kind regards,
Petr

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

PATCH 2/7 Add test for CVE 2021-3656

In reply to Martin Doucha

---

The reproducer was verified on vulnerable SLE kernels.

 runtest/kvm                      |   1 +
 testcases/kernel/kvm/.gitignore  |   1 +
 testcases/kernel/kvm/kvm_svm02.c | 159 +++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 testcases/kernel/kvm/kvm_svm02.c
runtest/kvm +1 -0
index 726d72f0a..59e410beb 100644
@@ -1,2 +1,3 @@
 kvm_pagefault01 kvm_pagefault01
 kvm_svm01 kvm_svm01
+kvm_svm02 kvm_svm02
testcases/kernel/kvm/.gitignore +1 -0
index b284b9528..c757cd3f4 100644
@@ -1,2 +1,3 @@
 /kvm_pagefault01
 /kvm_svm01
+/kvm_svm02
testcases/kernel/kvm/kvm_svm02.c new file +159 -0

159 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 2/7
#6 lore raw

Re: [LTP] [PATCH 2/7] Add test for CVE 2021-3656

Hi Martin,

Quoted 1 line

+++ b/testcases/kernel/kvm/kvm_svm02.c

...

testcases/kernel/kvm/kvm_svm02.c line 36

Quoted 12 lines
testcases/kernel/kvm/kvm_svm02.c new file +12 -0
+
+#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)

These five constants aren't used at all.
Maybe copy paste from kvm_svm01.c ?

The rest LGTM, but I'm not the right person to review. It'd be great if Nicolai would review.

Kind regards,
Petr

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

Re: [LTP] [PATCH 2/7] Add test for CVE 2021-3656

Quoted 4 lines · Petr Vorel

Hi Martin,

Quoted 1 line

+++ b/testcases/kernel/kvm/kvm_svm02.c

...

Quoted 15 lines

testcases/kernel/kvm/kvm_svm02.c line 36

Quoted 12 lines
testcases/kernel/kvm/kvm_svm02.c new file +12 -0
+
+#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)

These five constants aren't used at all.
Maybe copy paste from kvm_svm01.c ?

Yes, the AVIC constants are leftovers from copying kvm_svm01.c, sorry about that. Should I send a v2 after other patches get merged?

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
#8 lore raw

Re: [LTP] [PATCH 2/7] Add test for CVE 2021-3656

Quoted 21 lines · Petr Vorel
Quoted 4 lines

Hi Martin,

Quoted 1 line

+++ b/testcases/kernel/kvm/kvm_svm02.c

...

Quoted 15 lines

testcases/kernel/kvm/kvm_svm02.c line 36

Quoted 12 lines
testcases/kernel/kvm/kvm_svm02.c new file +12 -0
+
+#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)

These five constants aren't used at all.
Maybe copy paste from kvm_svm01.c ?

Quoted 2 lines

Yes, the AVIC constants are leftovers from copying kvm_svm01.c, sorry about that. Should I send a v2 after other patches get merged?

It'd be trivial to remove it, but if you send v2, please fix it there.

Kind regards,
Petr

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

PATCH 3/7 lib: Add safe functions for pthread_kill() and mutexes

In reply to Martin Doucha
---
 include/tst_safe_pthread.h |  52 ++++++++++++
 lib/safe_pthread.c         | 161 +++++++++++++++++++++++++++++++++++++
 2 files changed, 213 insertions(+)
include/tst_safe_pthread.h +52 -0
index 8fb553929..360b561b6 100644
@@ -52,4 +52,56 @@ int safe_pthread_cancel(const char *file, const int lineno,
 #define SAFE_PTHREAD_CANCEL(thread_id) \
 	safe_pthread_cancel(__FILE__, __LINE__, thread_id);
 
+int safe_pthread_mutexattr_init(const char *file, const int lineno,
+	pthread_mutexattr_t *attr);
+#define SAFE_PTHREAD_MUTEXATTR_INIT(attr) \
+	safe_pthread_mutexattr_init(__FILE__, __LINE__, (attr))
+
+int safe_pthread_mutexattr_destroy(const char *file, const int lineno,
+	pthread_mutexattr_t *attr);
+#define SAFE_PTHREAD_MUTEXATTR_DESTROY(attr) \
+	safe_pthread_mutexattr_destroy(__FILE__, __LINE__, (attr))
+
+int safe_pthread_mutexattr_settype(const char *file, const int lineno,
+	pthread_mutexattr_t *attr, int type);
+#define SAFE_PTHREAD_MUTEXATTR_SETTYPE(attr, type) \
+	safe_pthread_mutexattr_settype(__FILE__, __LINE__, (attr), (type))
+
+int safe_pthread_mutex_init(const char *file, const int lineno,
+	pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
+#define SAFE_PTHREAD_MUTEX_INIT(mutex, attr) \
+	safe_pthread_mutex_init(__FILE__, __LINE__, (mutex), (attr))
+
+int safe_pthread_mutex_destroy(const char *file, const int lineno,
+	pthread_mutex_t *mutex);
+#define SAFE_PTHREAD_MUTEX_DESTROY(mutex) \
+	safe_pthread_mutex_destroy(__FILE__, __LINE__, (mutex))
+
+int safe_pthread_mutex_lock(const char *file, const int lineno,
+	pthread_mutex_t *mutex);
+#define SAFE_PTHREAD_MUTEX_LOCK(mutex) \
+	safe_pthread_mutex_lock(__FILE__, __LINE__, (mutex))
+
+/* Terminates the test on any error other than EBUSY */
+int safe_pthread_mutex_trylock(const char *file, const int lineno,
+	pthread_mutex_t *mutex);
+#define SAFE_PTHREAD_MUTEX_TRYLOCK(mutex) \
+	safe_pthread_mutex_trylock(__FILE__, __LINE__, (mutex))
+
+/* Terminates the test on any error other than ETIMEDOUT */
+int safe_pthread_mutex_timedlock(const char *file, const int lineno,
+	pthread_mutex_t *mutex, const struct timespec *abstime);
+#define SAFE_PTHREAD_MUTEX_TIMEDLOCK(mutex, abstime) \
+	safe_pthread_mutex_timedlock(__FILE__, __LINE__, (mutex), (abstime))
+
+int safe_pthread_mutex_unlock(const char *file, const int lineno,
+	pthread_mutex_t *mutex);
+#define SAFE_PTHREAD_MUTEX_UNLOCK(mutex) \
+	safe_pthread_mutex_unlock(__FILE__, __LINE__, (mutex))
+
+int safe_pthread_kill(const char *file, const int lineno,
+	pthread_t thread, int sig);
+#define SAFE_PTHREAD_KILL(thread, sig) \
+	safe_pthread_kill(__FILE__, __LINE__, (thread), (sig))
+
 #endif /* TST_SAFE_PTHREAD_H__ */
lib/safe_pthread.c +161 -0

164 lines not on this page · raw message

Signature · 5 lines
2.40.0


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

Re: [LTP] [PATCH 3/7] lib: Add safe functions for pthread_kill() and mutexes

Hi Martin,

Kind regards,
Petr

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

PATCH 4/7 KVM: Add async communication helper functions

In reply to Martin Doucha

Some KVM tests need to communicate with VM controller program without stopping guest execution. Add guest helper functions for sending asynchronous guest signal to host and waiting for host to acknowledge the signal. Add host helper functions for waiting for guest signal and acknowledging it.

Test programs using these functions must have at least two host threads.

---
 testcases/kernel/kvm/include/kvm_common.h |  8 ++++++++
 testcases/kernel/kvm/include/kvm_guest.h  | 14 +++++++++++++
 testcases/kernel/kvm/include/kvm_host.h   | 15 ++++++++++++++
 testcases/kernel/kvm/lib_guest.c          | 16 +++++++++++++++
 testcases/kernel/kvm/lib_host.c           | 24 +++++++++++++++++++++++
 5 files changed, 77 insertions(+)
testcases/kernel/kvm/include/kvm_common.h +8 -0
index 4e81d8302..377e3f6aa 100644
@@ -10,6 +10,14 @@
 
 #define KVM_TNONE	-1	/* "No result" status value */
 
+/*
+ * Result value for asynchronous notifications between guest and host.
+ * Do not use this value directly. Call tst_signal_host() or tst_wait_host()
+ * in guest code. The notification must be handled by another host thread
+ * and then the result value must be reset to KVM_TNONE.
+ */
+#define KVM_TSYNC	0xfe
+
 /*
  * Result value indicating end of test. If the test program exits using
  * the HLT instruction with any valid result value other than KVM_TEXIT or
testcases/kernel/kvm/include/kvm_guest.h +14 -0
index ec13c5845..96f246155 100644
@@ -64,6 +64,20 @@ void tst_brk_(const char *file, const int lineno, int result,
 	const char *message) __attribute__((noreturn));
 #define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))
 
+/*
+ * Send asynchronous notification to host without stopping VM execution and
+ * return immediately. The notification must be handled by another host thread.
+ * The data argument will be passed to host in test_result->file_addr and
+ * can be used to send additional data both ways.
+ */
+void tst_signal_host(void *data);
+
+/*
+ * Call tst_signal_host(data) and wait for host to call
+ * tst_kvm_clear_guest_signal().
+ */
+void tst_wait_host(void *data);
+
 void *tst_heap_alloc_aligned(size_t size, size_t align);
 void *tst_heap_alloc(size_t size);
 
testcases/kernel/kvm/include/kvm_host.h +15 -0
index 2359944fd..cbbdadc06 100644
@@ -134,4 +134,19 @@ void tst_kvm_run_instance(struct tst_kvm_instance *inst);
  */
 void tst_kvm_destroy_instance(struct tst_kvm_instance *inst);
 
+/*
+ * Wait for given VM to call tst_signal_host() or tst_wait_host(). Timeout
+ * value is in seconds. Zero means no wait, negative value means wait forever.
+ * Note that the actual wait time may be up to 1 second shorter due to time()
+ * granularity. Returns 0 if signal was received, KVM_TEXIT if the VM exited
+ * without sending a signal, or -1 if timeout was reached.
+ */
+int tst_kvm_wait_guest(struct tst_kvm_instance *inst, int timeout);
+
+/*
+ * Clear VM signal sent by tst_signal_host(). If the VM is waiting
+ * in tst_wait_host(), this function will signal the VM to resume execution.
+ */
+void tst_kvm_clear_guest_signal(struct tst_kvm_instance *inst);
+
 #endif /* KVM_HOST_H_ */
testcases/kernel/kvm/lib_guest.c +16 -0
index d3b2ac3d5..f3e21d3d6 100644
@@ -155,6 +155,22 @@ void tst_brk_(const char *file, const int lineno, int result,
 	kvm_exit();
 }
 
+void tst_signal_host(void *data)
+{
+	test_result->file_addr = (uintptr_t)data;
+	test_result->result = KVM_TSYNC;
+}
+
+void tst_wait_host(void *data)
+{
+	volatile int32_t *vres = &test_result->result;
+
+	tst_signal_host(data);
+
+	while (*vres != KVM_TNONE)
+		;
+}
+
 void tst_handle_interrupt(struct kvm_interrupt_frame *ifrm, long vector,
 	unsigned long errcode)
 {
testcases/kernel/kvm/lib_host.c +24 -0
index 2782e68b0..a5f05449c 100644
@@ -272,6 +272,30 @@ void tst_kvm_destroy_instance(struct tst_kvm_instance *inst)
 	memset(inst->ram, 0, sizeof(inst->ram));
 }
 
+int tst_kvm_wait_guest(struct tst_kvm_instance *inst, int timeout)
+{
+	volatile struct tst_kvm_result *result = inst->result;
+	int32_t res;
+	time_t start = time(NULL);
+
+	while ((res = result->result) != KVM_TSYNC) {
+		if (res == KVM_TEXIT)
+			return res;
+
+		if (timeout >= 0 && start + timeout <= time(NULL))
+			return -1;
+
+		usleep(1000);
+	}
+
+	return 0;
+}
+
+void tst_kvm_clear_guest_signal(struct tst_kvm_instance *inst)
+{
+	inst->result->result = KVM_TNONE;
+}
+
 void tst_kvm_setup(void)
 {
 
Signature · 5 lines
2.40.0


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

Re: [LTP] [PATCH 4/7] KVM: Add async communication helper functions

Hi Martin,

testcases/kernel/kvm/include/kvm_guest.h void tst_brk_(const char *file, const int lineno, int result, · line 65

Quoted 10 lines

/*
* Result value indicating end of test. If the test program exits using
* the HLT instruction with any valid result value other than KVM_TEXIT or

testcases/kernel/kvm/include/kvm_guest.h +0 -0
index ec13c5845..96f246155 100644
@@ -64,6 +64,20 @@ void tst_brk_(const char *file, const int lineno, int result,
 	const char *message) __attribute__((noreturn));
 #define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))

BTW (unrelated to this commit, as it was not added to this commit) tst_brk() definition from include/tst_test.h contains also TST_BRK_SUPPORTS_ONLY_TCONF_TBROK().

And tst_res() has also TST_RES_SUPPORTS_TCONF_TFAIL_TINFO_TPASS_TWARN().

We could rename tst_res_flags.h to tst_res.h and add there tst_brk() and tst_res() definitions and tst_brk_() and tst_res_() signatures.

Quoted 14 lines
testcases/kernel/kvm/include/kvm_guest.h +1 -0
+
 #endif /* KVM_HOST_H_ */
testcases/kernel/kvm/lib_guest.c +4 -0
index d3b2ac3d5..f3e21d3d6 100644
@@ -155,6 +155,22 @@ void tst_brk_(const char *file, const int lineno, int result,
 	kvm_exit();
 }
 
+void tst_signal_host(void *data)
+{
+	test_result->file_addr = (uintptr_t)data;
+	test_result->result = KVM_TSYNC;

testcases/kernel/kvm/lib_guest.c void tst_brk_(const char *file, const int lineno, int result, · line 171

Quoted 10 lines
testcases/kernel/kvm/lib_guest.c +10 -0
+}
+
+void tst_wait_host(void *data)
+{
+	volatile int32_t *vres = &test_result->result;
+
+	tst_signal_host(data);
+
+	while (*vres != KVM_TNONE)
+		;

Interesting there is no any usleep().

Kind regards,
Petr

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

Re: [LTP] [PATCH 4/7] KVM: Add async communication helper functions

Quoted 19 lines · Petr Vorel

Hi Martin,

testcases/kernel/kvm/include/kvm_guest.h void tst_brk_(const char *file, const int lineno, int result, · line 65

Quoted 10 lines

/*
* Result value indicating end of test. If the test program exits using
* the HLT instruction with any valid result value other than KVM_TEXIT or

testcases/kernel/kvm/include/kvm_guest.h +0 -0
index ec13c5845..96f246155 100644
@@ -64,6 +64,20 @@ void tst_brk_(const char *file, const int lineno, int result,
  	const char *message) __attribute__((noreturn));
  #define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))

BTW (unrelated to this commit, as it was not added to this commit) tst_brk() definition from include/tst_test.h contains also TST_BRK_SUPPORTS_ONLY_TCONF_TBROK().

Quoted 1 line

And tst_res() has also TST_RES_SUPPORTS_TCONF_TFAIL_TINFO_TPASS_TWARN().

The support macros are not needed, the controller program always handles TBROK using tst_brk() even if the guest incorrectly called tst_res(TBROK).

Quoted 2 lines

We could rename tst_res_flags.h to tst_res.h and add there tst_brk() and tst_res() definitions and tst_brk_() and tst_res_() signatures.

I don't think that's a good idea. The guest library currently does not support variadic parameters for tst_res() and tst_brk() so the function signatures are slightly different.

Quoted 8 lines
Quoted 7 lines
testcases/kernel/kvm/lib_guest.c +0 -0
index d3b2ac3d5..f3e21d3d6 100644
@@ -155,6 +155,22 @@ void tst_brk_(const char *file, const int lineno, int result,
  	kvm_exit();
  }
Quoted 15 lines
Quoted 4 lines
testcases/kernel/kvm/lib_guest.c +4 -0
+void tst_signal_host(void *data)
+{
+	test_result->file_addr = (uintptr_t)data;
+	test_result->result = KVM_TSYNC;

testcases/kernel/kvm/lib_guest.c void tst_brk_(const char *file, const int lineno, int result, · line 171

Quoted 10 lines
testcases/kernel/kvm/lib_guest.c +10 -0
+}
+
+void tst_wait_host(void *data)
+{
+	volatile int32_t *vres = &test_result->result;
+
+	tst_signal_host(data);
+
+	while (*vres != KVM_TNONE)
+		;

Interesting there is no any usleep().

usleep() does not exist in the guest library. And even if it did, yielding the CPU to the host would prevent kvm_svm03 from ever reproducing the CLGI bug.

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
#15 lore raw

Re: [LTP] [PATCH 4/7] KVM: Add async communication helper functions

Quoted 20 lines · Petr Vorel
Quoted 18 lines

Hi Martin,

testcases/kernel/kvm/include/kvm_guest.h void tst_brk_(const char *file, const int lineno, int result, · line 65

Quoted 10 lines

/*
* Result value indicating end of test. If the test program exits using
* the HLT instruction with any valid result value other than KVM_TEXIT or

testcases/kernel/kvm/include/kvm_guest.h +0 -0
index ec13c5845..96f246155 100644
@@ -64,6 +64,20 @@ void tst_brk_(const char *file, const int lineno, int result,
  	const char *message) __attribute__((noreturn));
  #define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))

BTW (unrelated to this commit, as it was not added to this commit) tst_brk() definition from include/tst_test.h contains also TST_BRK_SUPPORTS_ONLY_TCONF_TBROK().

Quoted 4 lines
Quoted 1 line

And tst_res() has also TST_RES_SUPPORTS_TCONF_TFAIL_TINFO_TPASS_TWARN().

The support macros are not needed, the controller program always handles TBROK using tst_brk() even if the guest incorrectly called tst_res(TBROK).

ack

Quoted 6 lines
Quoted 2 lines

We could rename tst_res_flags.h to tst_res.h and add there tst_brk() and tst_res() definitions and tst_brk_() and tst_res_() signatures.

I don't think that's a good idea. The guest library currently does not support variadic parameters for tst_res() and tst_brk() so the function signatures are slightly different.

Ah, right.

Quoted 24 lines
Quoted 8 lines
Quoted 7 lines
testcases/kernel/kvm/lib_guest.c +0 -0
index d3b2ac3d5..f3e21d3d6 100644
@@ -155,6 +155,22 @@ void tst_brk_(const char *file, const int lineno, int result,
  	kvm_exit();
  }
Quoted 15 lines
Quoted 4 lines
testcases/kernel/kvm/lib_guest.c +4 -0
+void tst_signal_host(void *data)
+{
+	test_result->file_addr = (uintptr_t)data;
+	test_result->result = KVM_TSYNC;

testcases/kernel/kvm/lib_guest.c void tst_brk_(const char *file, const int lineno, int result, · line 171

Quoted 10 lines
testcases/kernel/kvm/lib_guest.c +10 -0
+}
+
+void tst_wait_host(void *data)
+{
+	volatile int32_t *vres = &test_result->result;
+
+	tst_signal_host(data);
+
+	while (*vres != KVM_TNONE)
+		;

Interesting there is no any usleep().

Quoted 3 lines

usleep() does not exist in the guest library. And even if it did, yielding the CPU to the host would prevent kvm_svm03 from ever reproducing the CLGI bug.

Ah, I thought I'm missing something obvious, thanks for an explanation.

Kind regards,
Petr

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

Re: [LTP] [PATCH 4/7] KVM: Add async communication helper functions

In reply to Martin Doucha

Hi!

testcases/kernel/kvm/lib_host.c void tst_kvm_destroy_instance(struct tst_kvm_instance *inst) · line 286

Quoted 12 lines
testcases/kernel/kvm/lib_host.c +12 -0
+int tst_kvm_wait_guest(struct tst_kvm_instance *inst, int timeout)
+{
+	volatile struct tst_kvm_result *result = inst->result;
+	int32_t res;
+	time_t start = time(NULL);
+
+	while ((res = result->result) != KVM_TSYNC) {
+		if (res == KVM_TEXIT)
+			return res;
+
+		if (timeout >= 0 && start + timeout <= time(NULL))
+			return -1;

Can we please avoid using wall clock time for timeouts? That is broken by desing, since the time is continually adjusted by ntp daemon these days and can jump back and forth...

This should be really:

	struct timespec start

	tst_clock_gettime(CLOCK_MONOTONIC, &start);

	while (...) {
		struct timespec now;

		tst_clock_gettime(CLOCK_MONOTONIC, &now);

		if (tst_timespec_diff_ms(now, start) >= timeout_ms)
			return -1;

testcases/kernel/kvm/lib_host.c void tst_kvm_destroy_instance(struct tst_kvm_instance *inst) · line 292

Quoted 5 lines
testcases/kernel/kvm/lib_host.c +5 -0
+		usleep(1000);
+	}
+
+	return 0;
+}

The rest looks good.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH 4/7] KVM: Add async communication helper functions

Quoted 17 lines · Cyril Hrubis

Hi!

testcases/kernel/kvm/lib_host.c void tst_kvm_destroy_instance(struct tst_kvm_instance *inst) · line 286

Quoted 12 lines
testcases/kernel/kvm/lib_host.c +12 -0
+int tst_kvm_wait_guest(struct tst_kvm_instance *inst, int timeout)
+{
+	volatile struct tst_kvm_result *result = inst->result;
+	int32_t res;
+	time_t start = time(NULL);
+
+	while ((res = result->result) != KVM_TSYNC) {
+		if (res == KVM_TEXIT)
+			return res;
+
+		if (timeout >= 0 && start + timeout <= time(NULL))
+			return -1;

Can we please avoid using wall clock time for timeouts? That is broken by desing, since the time is continually adjusted by ntp daemon these days and can jump back and forth...

Yes, I'll change it to tst_clock_gettime() in v2. But I'll wait a bit for other patches to get merged before resubmitting.

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
#18 lore raw

PATCH 5/7 KVM: Allow expected KVM_RUN errors in tst_kvm_run_instance()

In reply to Martin Doucha

Add a new parameter to tst_kvm_run_instance() for expected errno value, e.g. EINTR for testing the possiblity to kill running guest. When ioctl(KVM_RUN) fails with the expected errno, tst_kvm_run_instance() will return -1 without terminating the test.

Default tst_kvm_run() function expects no KVM_RUN errors.

---
 doc/kvm-test-api.txt                    |  9 ++++++---
 testcases/kernel/kvm/include/kvm_host.h |  6 ++++--
 testcases/kernel/kvm/lib_host.c         | 22 +++++++++++++++++++---
 3 files changed, 29 insertions(+), 8 deletions(-)
doc/kvm-test-api.txt +6 -3
index 812e12b38..7e537afcb 100644
@@ -194,9 +194,12 @@ previously allocated guarded buffers.
   with at least `ram_size` bytes of memory. The VM instance info will be
   stored in `inst`.
 
-- `void tst_kvm_run_instance(struct tst_kvm_instance *inst)` – Executes
-  the program installed in KVM virtual machine `inst`. Any result messages
-  returned by the VM will be automatically printed to controller program output.
+- `int tst_kvm_run_instance(struct tst_kvm_instance *inst, int exp_errno)` –
+  Executes the program installed in KVM virtual machine `inst`. Any result
+  messages returned by the VM will be automatically printed to controller
+  program output. Returns zero. If `exp_errno` is non-zero, the VM execution
+  syscall is allowed to fail with the `exp_errno` error code and
+  `tst_kvm_run_instance()` will return -1 instead of terminating the test.
 
 - `void tst_kvm_destroy_instance(struct tst_kvm_instance *inst)` – Deletes
   the KVM virtual machine `inst`. Note that the guarded buffers assigned
testcases/kernel/kvm/include/kvm_host.h +4 -2
index cbbdadc06..3e60d4738 100644
@@ -125,9 +125,11 @@ struct kvm_cpuid2 *tst_kvm_get_cpuid(int sysfd);
 void tst_kvm_create_instance(struct tst_kvm_instance *inst, size_t ram_size);
 
 /*
- * Execute the given KVM instance and print results.
+ * Execute the given KVM instance and print results. If ioctl(KVM_RUN) is
+ * expected to fail, pass the expected error code in exp_errno, otherwise
+ * set it to zero. Returns last value returned by ioctl(KVM_RUN).
  */
-void tst_kvm_run_instance(struct tst_kvm_instance *inst);
+int tst_kvm_run_instance(struct tst_kvm_instance *inst, int exp_errno);
 
 /*
  * Close the given KVM instance.
testcases/kernel/kvm/lib_host.c +19 -3
index a5f05449c..e33a5f5aa 100644
@@ -234,14 +234,28 @@ void tst_kvm_create_instance(struct tst_kvm_instance *inst, size_t ram_size)
 	inst->result->message[0] = '\0';
 }
 
-void tst_kvm_run_instance(struct tst_kvm_instance *inst)
+int tst_kvm_run_instance(struct tst_kvm_instance *inst, int exp_errno)
 {
 	struct kvm_regs regs;
+	int ret;
 
 	while (1) {
 		inst->result->result = KVM_TNONE;
 		inst->result->message[0] = '\0';
-		SAFE_IOCTL(inst->vcpu_fd, KVM_RUN, 0);
+		errno = 0;
+		ret = ioctl(inst->vcpu_fd, KVM_RUN, 0);
+
+		if (ret == -1) {
+			if (errno == exp_errno)
+				return ret;
+
+			tst_brk(TBROK | TERRNO, "ioctl(KVM_RUN) failed");
+		}
+
+		if (ret < 0) {
+			tst_brk(TBROK | TERRNO,
+				"Invalid ioctl(KVM_RUN) return value %d", ret);
+		}
 
 		if (inst->vcpu_info->exit_reason != KVM_EXIT_HLT) {
 			SAFE_IOCTL(inst->vcpu_fd, KVM_GET_REGS, &regs);
@@ -255,6 +269,8 @@ void tst_kvm_run_instance(struct tst_kvm_instance *inst)
 
 		tst_kvm_print_result(inst);
 	}
+
+	return ret;
 }
 
 void tst_kvm_destroy_instance(struct tst_kvm_instance *inst)
@@ -304,7 +320,7 @@ void tst_kvm_setup(void)
 void tst_kvm_run(void)
 {
 	tst_kvm_create_instance(&test_vm, DEFAULT_RAM_SIZE);
-	tst_kvm_run_instance(&test_vm);
+	tst_kvm_run_instance(&test_vm, 0);
 	tst_kvm_destroy_instance(&test_vm);
 	tst_free_all();
 }
Signature · 5 lines
2.40.0


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

Re: [LTP] [PATCH 5/7] KVM: Allow expected KVM_RUN errors in tst_kvm_run_instance()

Hi Martin,

Quoted 6 lines

Add a new parameter to tst_kvm_run_instance() for expected errno value, e.g. EINTR for testing the possiblity to kill running guest. When ioctl(KVM_RUN) fails with the expected errno, tst_kvm_run_instance() will return -1 without terminating the test.

Default tst_kvm_run() function expects no KVM_RUN errors.

Kind regards,
Petr

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

PATCH 6/7 KVM: Add STGI/CLGI intercept constants

In reply to Martin Doucha
---
 testcases/kernel/kvm/include/kvm_x86_svm.h | 2 ++
 1 file changed, 2 insertions(+)
testcases/kernel/kvm/include/kvm_x86_svm.h +2 -0
index 3eb832849..89dc9c39f 100644
@@ -39,6 +39,8 @@
 #define SVM_INTERCEPT_VMRUN 0x80
 #define SVM_INTERCEPT_VMLOAD 0x82
 #define SVM_INTERCEPT_VMSAVE 0x83
+#define SVM_INTERCEPT_STGI 0x84
+#define SVM_INTERCEPT_CLGI 0x85
 #define SVM_INTERCEPT_MAX 0x95
 
 /* SVM vmrun exit codes */
Signature · 5 lines
2.40.0


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

Re: [LTP] [PATCH 6/7] KVM: Add STGI/CLGI intercept constants

Hi Martin,

testcases/kernel/kvm/include/kvm_x86_svm.h line 42

Quoted 6 lines

+++ b/testcases/kernel/kvm/include/kvm_x86_svm.h

testcases/kernel/kvm/include/kvm_x86_svm.h +1 -0
@@ -39,6 +39,8 @@
 #define SVM_INTERCEPT_VMRUN 0x80
 #define SVM_INTERCEPT_VMLOAD 0x82
 #define SVM_INTERCEPT_VMSAVE 0x83
+#define SVM_INTERCEPT_STGI 0x84

SVM_INTERCEPT_STGI is not used anywhere.

Kind regards,
Petr

Quoted 5 lines
testcases/kernel/kvm/include/kvm_x86_svm.h +1 -0
+#define SVM_INTERCEPT_CLGI 0x85
 
 #define SVM_INTERCEPT_MAX 0x95
 

/* SVM vmrun exit codes */

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

Re: [LTP] [PATCH 6/7] KVM: Add STGI/CLGI intercept constants

Quoted 9 lines · Petr Vorel

Hi Martin,

testcases/kernel/kvm/include/kvm_x86_svm.h line 42

Quoted 6 lines

+++ b/testcases/kernel/kvm/include/kvm_x86_svm.h

testcases/kernel/kvm/include/kvm_x86_svm.h +1 -0
@@ -39,6 +39,8 @@
  #define SVM_INTERCEPT_VMRUN 0x80
  #define SVM_INTERCEPT_VMLOAD 0x82
  #define SVM_INTERCEPT_VMSAVE 0x83
+#define SVM_INTERCEPT_STGI 0x84

SVM_INTERCEPT_STGI is not used anywhere.

I know. Let's keep it anyway. But maybe I should also add SVM_EXIT constants in v2?

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
#24 lore raw

Re: [LTP] [PATCH 6/7] KVM: Add STGI/CLGI intercept constants

Quoted 11 lines · Petr Vorel
Quoted 9 lines

Hi Martin,

testcases/kernel/kvm/include/kvm_x86_svm.h line 42

Quoted 6 lines

+++ b/testcases/kernel/kvm/include/kvm_x86_svm.h

testcases/kernel/kvm/include/kvm_x86_svm.h +1 -0
@@ -39,6 +39,8 @@
  #define SVM_INTERCEPT_VMRUN 0x80
  #define SVM_INTERCEPT_VMLOAD 0x82
  #define SVM_INTERCEPT_VMSAVE 0x83
+#define SVM_INTERCEPT_STGI 0x84

SVM_INTERCEPT_STGI is not used anywhere.

Quoted 2 lines

I know. Let's keep it anyway. But maybe I should also add SVM_EXIT constants in v2?

It's a detail, but yeah it'd make more sense.

Kind regards,
Petr

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

PATCH 7/7 Add KVM test for CPU lockup through malicous SVM guest

In reply to Martin Doucha

---

The reproducer was verified on vulnerable SLE kernels.

 runtest/kvm                      |   1 +
 testcases/kernel/kvm/.gitignore  |   1 +
 testcases/kernel/kvm/Makefile    |   3 +
 testcases/kernel/kvm/kvm_svm03.c | 164 +++++++++++++++++++++++++++++++
 4 files changed, 169 insertions(+)
 create mode 100644 testcases/kernel/kvm/kvm_svm03.c
runtest/kvm +1 -0
index 59e410beb..4094a21a8 100644
@@ -1,3 +1,4 @@
 kvm_pagefault01 kvm_pagefault01
 kvm_svm01 kvm_svm01
 kvm_svm02 kvm_svm02
+kvm_svm03 kvm_svm03
testcases/kernel/kvm/.gitignore +1 -0
index c757cd3f4..9638a6fc7 100644
@@ -1,3 +1,4 @@
 /kvm_pagefault01
 /kvm_svm01
 /kvm_svm02
+/kvm_svm03
testcases/kernel/kvm/Makefile +3 -0
index e12cb4e98..03d754420 100644
@@ -48,6 +48,9 @@ endif
 lib_guest.o $(ARCH_OBJ): CPPFLAGS	:= $(GUEST_CPPFLAGS)
 lib_guest.o $(ARCH_OBJ): CFLAGS		:= $(GUEST_CFLAGS)
 
+kvm_svm03: CFLAGS += -pthread
+kvm_svm03: LDLIBS += -pthread -lrt
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
 
 %-payload.o: %.c lib_guest.o $(ARCH_OBJ)
testcases/kernel/kvm/kvm_svm03.c new file +164 -0

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 7/7
#26 lore raw

Re: [LTP] [PATCH 7/7] Add KVM test for CPU lockup through malicous SVM guest

Hi Martin,

Kind regards,
Petr

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