Skip to contentSkip to conversation

Two AMD SVM vulnerability tests

PATCH In review Martin Doucha 3 participants 17 messages May 26, 2023 – May 29, 2023 Library code

lore
Share & tools
lore
#0 lore raw

v2 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              |   4 +
 testcases/kernel/kvm/include/kvm_common.h  |   8 +
 testcases/kernel/kvm/include/kvm_guest.h   |  14 ++
 testcases/kernel/kvm/include/kvm_host.h    |  20 ++-
 testcases/kernel/kvm/include/kvm_x86_svm.h |   9 ++
 testcases/kernel/kvm/kvm_svm02.c           | 152 ++++++++++++++++++
 testcases/kernel/kvm/kvm_svm03.c           | 169 +++++++++++++++++++++
 testcases/kernel/kvm/lib_guest.c           |  16 ++
 testcases/kernel/kvm/lib_host.c            |  55 ++++++-
 14 files changed, 665 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 v2 1/7 KVM: Add VMSAVE/VMLOAD intercept constants

---

Changes since v1: None

 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

PATCH v2 2/7 Add test for CVE 2021-3656

In reply to Martin Doucha

---

The reproducer was verified on vulnerable SLE kernels.

Change since v1:
- Removed unused AVIC constants

 runtest/kvm                      |   1 +
 testcases/kernel/kvm/.gitignore  |   1 +
 testcases/kernel/kvm/kvm_svm02.c | 152 +++++++++++++++++++++++++++++++
 3 files changed, 154 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 +152 -0

152 lines not on this page · raw message

Signature · 5 lines
2.40.0


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

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

Hi Martin,

...

Quoted 2 lines
testcases/kernel/kvm/kvm_svm02.c new file +0 -0

...

testcases/kernel/kvm/kvm_svm02.c line 37

Quoted 5 lines
testcases/kernel/kvm/kvm_svm02.c new file +5 -0
+/* Load FS, GS, TR and LDTR state from vmsave_buf */
+static int guest_vmload(void)
+{
+	asm (
+		"vmload\n"

Unfortunately, it fails on Debian oldstable [1].

kvm_svm02.c:37:3: error: too few operands for instruction

                "vmload\n"
                ^
<inline asm>:1:2: note: instantiated into assembly here
        vmload
        ^
kvm_svm02.c:48:3: error: too few operands for instruction
                "vmsave\n"
                ^
<inline asm>:1:2: note: instantiated into assembly here
        vmsave
        ^

2 errors generated.
make[3]: *** [/__w/ltp/ltp/testcases/kernel/kvm/Makefile:63: kvm_svm02-payload.o] Error 1

I wonder if there is an easy fix or reasonable simple way how to detect unsupported toolchain.

Kind regards,
Petr

[1] https://github.com/pevik/ltp/actions/runs/5091551272/jobs/9151721119

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

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

Quoted 6 lines

Hi Martin,

FYI used toolchain [1]:

clang version 7.0.1-8+deb10u2 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu

FYI it's oldstable. Debian 12 Bookworm is planned to be released on 10 June, in two weeks this error vanishes from the CI (we don't test oldoldstable).

Kind regards,
Petr

Quoted 4 lines

Kind regards,
Petr

[1] https://github.com/pevik/ltp/actions/runs/5091551272/jobs/9151721119

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

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

Hi all,

Martin sent patch which fixed it.

I'm going to merge whole patchset.

Thanks!

Kind regards,
Petr

testcases/kernel/kvm/kvm_svm02.c +2 -2
index e6ff5e874..5d2e2ce37 100644
@@ -34,7 +34,7 @@ static void *vmsave_buf;
 static int guest_vmload(void)
 {
        asm (
-               "vmload\n"
+               "vmload %0\n"
                :
                : "a" (vmsave_buf)
        );
@@ -45,7 +45,7 @@ static int guest_vmload(void)
 static int guest_vmsave(void)
 {
        asm (
-               "vmsave\n"
+               "vmsave %0\n"
                :
                : "a" (vmsave_buf)
        );
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

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

In reply to Martin Doucha

---

Changes since v1: None

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

PATCH v2 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.

---

Changes since v1:
- Use tst_clock_gettime() to measure timeout

 testcases/kernel/kvm/Makefile             |  1 +
 testcases/kernel/kvm/include/kvm_common.h |  8 ++++++
 testcases/kernel/kvm/include/kvm_guest.h  | 14 ++++++++++
 testcases/kernel/kvm/include/kvm_host.h   | 14 ++++++++++
 testcases/kernel/kvm/lib_guest.c          | 16 +++++++++++
 testcases/kernel/kvm/lib_host.c           | 33 +++++++++++++++++++++++
 6 files changed, 86 insertions(+)
testcases/kernel/kvm/Makefile +1 -0
index e12cb4e98..501bb5a09 100644
@@ -7,6 +7,7 @@ include $(top_srcdir)/include/mk/testcases.mk
 
 ASFLAGS =
 CPPFLAGS += -I$(abs_srcdir)/include
+LDLIBS += -lrt
 GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
 GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables -mno-mmx -mno-sse
 GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none -fno-stack-protector -z noexecstack
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 +14 -0
index 2359944fd..3949dd040 100644
@@ -134,4 +134,18 @@ 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 milliseconds. Zero means no wait, negative value means wait
+ * forever. 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_ms);
+
+/*
+ * 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 +33 -0
index 2782e68b0..2f524e3ce 100644
@@ -10,6 +10,8 @@
 
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
+#include "tst_clocks.h"
+#include "tst_timer.h"
 #include "kvm_host.h"
 
 static struct tst_kvm_instance test_vm = { .vm_fd = -1 };
@@ -272,6 +274,37 @@ 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_ms)
+{
+	volatile struct tst_kvm_result *result = inst->result;
+	int32_t res;
+	struct timespec start, now;
+
+	if (timeout_ms >= 0)
+		tst_clock_gettime(CLOCK_MONOTONIC, &start);
+
+	while ((res = result->result) != KVM_TSYNC) {
+		if (res == KVM_TEXIT)
+			return res;
+
+		if (timeout_ms >= 0) {
+			tst_clock_gettime(CLOCK_MONOTONIC, &now);
+
+			if (tst_timespec_diff_ms(now, start) >= timeout_ms)
+				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
Reviewed-by on v2 4/7
#9 lore raw

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

Hi Martin,

Quoted 7 lines

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.

IMHO we don't have flags to require this. I guess KVM hosts nowadays should have more than 2 threads (these tests are for bare metal), thus we don't have to note this also in the code, right?

Anyway LGTM.

Waiting some time if Cyril, or somebody else will have time to look into this change before merging whole patchset.

NOTE: whoever will merge this, it requires to change second commit:
https://lore.kernel.org/ltp/20230526142745.GA813136@pevik/

Kind regards,
Petr

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

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

Quoted 6 lines · Petr Vorel

Hi Martin,

Quoted 1 line

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

IMHO we don't have flags to require this. I guess KVM hosts nowadays should have more than 2 threads (these tests are for bare metal), thus we don't have to note this also in the code, right?

We have tst_test.min_cpus, used e.g. in kvm_svm03. But this is a note to programmers that they must use pthreads, otherwise tst_kvm_wait_guest() will always time out or lock up. The signaling can work even on single CPU unless you somehow manage to break task switching in the host kernel (like in kvm_svm03).

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

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

Quoted 8 lines · Petr Vorel
Quoted 6 lines

Hi Martin,

Quoted 1 line

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

IMHO we don't have flags to require this. I guess KVM hosts nowadays should have more than 2 threads (these tests are for bare metal), thus we don't have to note this also in the code, right?

Quoted 5 lines

We have tst_test.min_cpus, used e.g. in kvm_svm03. But this is a note to programmers that they must use pthreads, otherwise tst_kvm_wait_guest() will always time out or lock up. The signaling can work even on single CPU unless you somehow manage to break task switching in the host kernel (like in kvm_svm03).

+1, thanks!

Kind regards,
Petr

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

PATCH v2 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.

---

Changes since v1: None

 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 3949dd040..06bcb5d45 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 2f524e3ce..8e3d6094e 100644
@@ -236,14 +236,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);
@@ -257,6 +271,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)
@@ -313,7 +329,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
#13 lore raw

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

In reply to Martin Doucha

---

Changes since v1:
- Added SVM_EXIT_* constants for future use

 testcases/kernel/kvm/include/kvm_x86_svm.h | 4 ++++
 1 file changed, 4 insertions(+)
testcases/kernel/kvm/include/kvm_x86_svm.h +4 -0
index 3eb832849..b4b1b80e2 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 */
@@ -46,6 +48,8 @@
 #define SVM_EXIT_VMRUN 0x80
 #define SVM_EXIT_VMLOAD 0x82
 #define SVM_EXIT_VMSAVE 0x83
+#define SVM_EXIT_STGI 0x84
+#define SVM_EXIT_CLGI 0x85
 #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
Acked-by on v2 6/7
#14 lore raw

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

Hi Martin,

Kind regards,
Petr

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

PATCH v2 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.

Changes since v1:
- Updated timeout value in tst_kvm_wait_guest() call to match changes in patch 4

 runtest/kvm                      |   1 +
 testcases/kernel/kvm/.gitignore  |   1 +
 testcases/kernel/kvm/Makefile    |   3 +
 testcases/kernel/kvm/kvm_svm03.c | 169 +++++++++++++++++++++++++++++++
 4 files changed, 174 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 501bb5a09..87dfb4e8d 100644
@@ -49,6 +49,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
+
 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 +169 -0

169 lines not on this page · raw message

Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#16 lore raw
In reply to Martin Doucha

Hi!
Patchset pushed with two minor changes:

* Fixes for the vmsave and vmload for old assembler

* Removed LDLIBS=-lrt from the patch that adds tst_clock_gettime() (as tst_clock_gettime() does not need -lrt)

Thanks.

Signature · 5 lines
Cyril Hrubis
[email protected]

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