Skip to contentSkip to conversation

KVM testing

PATCH In review Martin Doucha 3 participants 7 messages Apr 21, 2022 – May 6, 2024

lore
Share & tools
lore
#0 lore raw

v3 cover KVM testing

Implement LTP infrastructure for tests which require executing a special program inside KVM virtual machine. Basic test for CVE 2021-38198 written using the KVM infrastrucutre (pagetable permission check bypass under software emulated MMU) is included.

Martin Doucha (2):
KVM test infrastructure
Add test for CVE 2021-38198

 configure.ac                              |   3 +
 include/mk/config.mk.in                   |   2 +
 include/mk/env_post.mk                    |   1 +
 include/mk/rules.mk                       |   8 +
 runtest/kvm                               |   1 +
 testcases/kernel/Makefile                 |   1 +
 testcases/kernel/kvm/.gitignore           |   1 +
 testcases/kernel/kvm/Makefile             |  62 +++
 testcases/kernel/kvm/bootstrap_x86.S      | 368 ++++++++++++++++
 testcases/kernel/kvm/bootstrap_x86_64.S   | 515 ++++++++++++++++++++++
 testcases/kernel/kvm/include/kvm_common.h |  31 ++
 testcases/kernel/kvm/include/kvm_guest.h  |  85 ++++
 testcases/kernel/kvm/include/kvm_host.h   | 115 +++++
 testcases/kernel/kvm/include/kvm_test.h   |  23 +
 testcases/kernel/kvm/include/kvm_x86.h    | 157 +++++++
 testcases/kernel/kvm/kvm_pagefault01.c    | 234 ++++++++++
 testcases/kernel/kvm/lib_guest.c          | 179 ++++++++
 testcases/kernel/kvm/lib_host.c           | 215 +++++++++
 testcases/kernel/kvm/lib_x86.c            | 150 +++++++
 testcases/kernel/kvm/linker/payload.lds   |  11 +
 testcases/kernel/kvm/linker/x86.lds       |  74 ++++
 testcases/kernel/kvm/linker/x86_64.lds    |  74 ++++
 22 files changed, 2310 insertions(+)
 create mode 100644 runtest/kvm
 create mode 100644 testcases/kernel/kvm/.gitignore
 create mode 100644 testcases/kernel/kvm/Makefile
 create mode 100644 testcases/kernel/kvm/bootstrap_x86.S
 create mode 100644 testcases/kernel/kvm/bootstrap_x86_64.S
 create mode 100644 testcases/kernel/kvm/include/kvm_common.h
 create mode 100644 testcases/kernel/kvm/include/kvm_guest.h
 create mode 100644 testcases/kernel/kvm/include/kvm_host.h
 create mode 100644 testcases/kernel/kvm/include/kvm_test.h
 create mode 100644 testcases/kernel/kvm/include/kvm_x86.h
 create mode 100644 testcases/kernel/kvm/kvm_pagefault01.c
 create mode 100644 testcases/kernel/kvm/lib_guest.c
 create mode 100644 testcases/kernel/kvm/lib_host.c
 create mode 100644 testcases/kernel/kvm/lib_x86.c
 create mode 100644 testcases/kernel/kvm/linker/payload.lds
 create mode 100644 testcases/kernel/kvm/linker/x86.lds
 create mode 100644 testcases/kernel/kvm/linker/x86_64.lds
Signature · 5 lines
2.35.1


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

PATCH v3 1/2 KVM test infrastructure

Implement LTP infrastructure for tests which require executing a special program inside KVM virtual machine. The infrastructure is split into two parts:
the host library and the guest library.

The host library provides functions for setting up and running virtual machines with test payload built from test sources.

The guest library provides CPU bootstrap code and basic implementation of some C and LTP library functions as well as functions for accessing low-level arch features like special registers, interrupt tables or memory mapping tables.

The test sources will contain both guest-side payload code and host-side setup code separated by preprocessor switch. The files get compiled twice with different compiler options. Once to build the VM payload, once to build the main program that will create the VM and execute the payload inside it.

---

Changes since v1:
- Fixed build on distros which enable -pie by default
- tst_res()/tst_brk() now print file:lineno even from inside KVM payload
- Refactored global GDT symbols in x86 bootstrap code for future use
- Removed unnecessary comments in host library

Changes since v2:
- #include tst_res_flags.h from main LTP library in kvm_guest.h
- Use LTP_ALIGN() in tst_heap_alloc() functions

 configure.ac                              |   3 +
 include/mk/config.mk.in                   |   2 +
 include/mk/env_post.mk                    |   1 +
 include/mk/rules.mk                       |   8 +
 testcases/kernel/Makefile                 |   1 +
 testcases/kernel/kvm/Makefile             |  62 +++
 testcases/kernel/kvm/bootstrap_x86.S      | 368 ++++++++++++++++
 testcases/kernel/kvm/bootstrap_x86_64.S   | 515 ++++++++++++++++++++++
 testcases/kernel/kvm/include/kvm_common.h |  31 ++
 testcases/kernel/kvm/include/kvm_guest.h  |  85 ++++
 testcases/kernel/kvm/include/kvm_host.h   | 115 +++++
 testcases/kernel/kvm/include/kvm_test.h   |  23 +
 testcases/kernel/kvm/include/kvm_x86.h    | 157 +++++++
 testcases/kernel/kvm/lib_guest.c          | 179 ++++++++
 testcases/kernel/kvm/lib_host.c           | 215 +++++++++
 testcases/kernel/kvm/lib_x86.c            | 150 +++++++
 testcases/kernel/kvm/linker/payload.lds   |  11 +
 testcases/kernel/kvm/linker/x86.lds       |  74 ++++
 testcases/kernel/kvm/linker/x86_64.lds    |  74 ++++
 19 files changed, 2074 insertions(+)
 create mode 100644 testcases/kernel/kvm/Makefile
 create mode 100644 testcases/kernel/kvm/bootstrap_x86.S
 create mode 100644 testcases/kernel/kvm/bootstrap_x86_64.S
 create mode 100644 testcases/kernel/kvm/include/kvm_common.h
 create mode 100644 testcases/kernel/kvm/include/kvm_guest.h
 create mode 100644 testcases/kernel/kvm/include/kvm_host.h
 create mode 100644 testcases/kernel/kvm/include/kvm_test.h
 create mode 100644 testcases/kernel/kvm/include/kvm_x86.h
 create mode 100644 testcases/kernel/kvm/lib_guest.c
 create mode 100644 testcases/kernel/kvm/lib_host.c
 create mode 100644 testcases/kernel/kvm/lib_x86.c
 create mode 100644 testcases/kernel/kvm/linker/payload.lds
 create mode 100644 testcases/kernel/kvm/linker/x86.lds
 create mode 100644 testcases/kernel/kvm/linker/x86_64.lds
configure.ac +3 -0
index 9b6d01f54..f0ea6acb4 100644
@@ -371,6 +371,9 @@ LTP_CHECK_SYSCALL_EVENTFD
 LTP_CHECK_SYSCALL_FCNTL
 LTP_CHECK_FSVERITY
 
+AX_CHECK_COMPILE_FLAG([-no-pie], [LTP_CFLAGS_NOPIE=1])
+AC_SUBST([LTP_CFLAGS_NOPIE])
+
 if test "x$with_numa" = xyes; then
 	LTP_CHECK_SYSCALL_NUMA
 	numa_error_msg="test requires libnuma development packages with LIBNUMA_API_VERSION >= 2"
include/mk/config.mk.in +2 -0
index 218447ef3..674843563 100644
@@ -83,6 +83,8 @@ WCFLAGS			?= -Wall -W @GCC_WARN_OLDSTYLE@
 LDFLAGS			+= $(WLDFLAGS)
 CFLAGS			+= $(DEBUG_CFLAGS) $(OPT_CFLAGS) $(WCFLAGS)
 
+LTP_CFLAGS_NOPIE	:= @LTP_CFLAGS_NOPIE@
+
 ifeq ($(strip $(HOST_CFLAGS)),)
 HOST_CFLAGS := $(CFLAGS)
 endif
include/mk/env_post.mk +1 -0
index ec045c40d..dc4df41d3 100644
@@ -32,6 +32,7 @@ ENV_POST_LOADED = 1
 # poor software design if you need more than one search directory, and
 # would suggest creating a general purpose static library to that end.
 vpath %.c $(abs_srcdir)
+vpath %.S $(abs_srcdir)
 
 # For config.h, et all.
 CPPFLAGS			+= -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/include/old/
include/mk/rules.mk +8 -0
index a60e6705a..32d8d05a7 100644
@@ -1,5 +1,13 @@
 target_rel_dir := $(if $(cwd_rel_from_top),$(cwd_rel_from_top)/,)
 
+%.o: %.S
+ifdef VERBOSE
+	$(AS) $(ASFLAGS) -c -o $@ $<
+else
+	@$(AS) $(ASFLAGS) -c -o $@ $<
+	@echo AS $(target_rel_dir)$@
+endif
+
 %.o: %.c
 ifdef VERBOSE
 	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
testcases/kernel/Makefile +1 -0
index d44856c57..c0fa24147 100644
@@ -29,6 +29,7 @@ SUBDIRS			+= connectors \
 			   io \
 			   ipc \
 			   irq \
+			   kvm \
 			   logging \
 			   mem \
 			   numa \
testcases/kernel/kvm/Makefile new file +62 -0

62 lines not on this page · raw message

testcases/kernel/kvm/bootstrap_x86.S new file +368 -0

368 lines not on this page · raw message

testcases/kernel/kvm/bootstrap_x86_64.S new file +515 -0

515 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_common.h new file +31 -0
new file mode 100644
index 000000000..4e81d8302
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Common definitions for communication between KVM guest and host.
+ */
+
+#ifndef KVM_COMMON_H_
+#define KVM_COMMON_H_
+
+#define KVM_TNONE	-1	/* "No result" status value */
+
+/*
+ * 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
+ * TBROK, KVM runner will automatically resume VM execution after printing
+ * the message.
+ */
+#define KVM_TEXIT	0xff
+
+#define KVM_RESULT_BASEADDR 0xfffff000
+#define KVM_RESULT_SIZE 0x1000
+
+struct tst_kvm_result {
+	int32_t result;
+	int32_t lineno;
+	uint64_t file_addr;
+	char message[0];
+};
+
+#endif /* KVM_COMMON_H_ */
testcases/kernel/kvm/include/kvm_guest.h new file +85 -0

85 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_host.h new file +115 -0

115 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_test.h new file +23 -0
new file mode 100644
index 000000000..55989eaa5
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Minimal test library for KVM tests
+ */
+
+#ifndef KVM_TEST_H_
+#define KVM_TEST_H_
+
+#ifdef COMPILE_PAYLOAD
+
+  #include "kvm_guest.h"
+  #include "kvm_common.h"
+
+#else
+
+  #include "tst_test.h"
+  #include "kvm_host.h"
+
+#endif /* COMPILE_PAYLOAD */
+
+#endif /* KVM_TEST_H_ */
testcases/kernel/kvm/include/kvm_x86.h new file +157 -0

157 lines not on this page · raw message

testcases/kernel/kvm/lib_guest.c new file +179 -0

179 lines not on this page · raw message

testcases/kernel/kvm/lib_host.c new file +215 -0

215 lines not on this page · raw message

testcases/kernel/kvm/lib_x86.c new file +150 -0

150 lines not on this page · raw message

testcases/kernel/kvm/linker/payload.lds new file +11 -0
new file mode 100644
index 000000000..a544fd37a
@@ -0,0 +1,11 @@
+TARGET(binary)
+
+SECTIONS
+{
+	.data :
+	{
+		kvm_payload_start = .;
+		KEEP(*(.data))
+		kvm_payload_end = .;
+	}
+}
testcases/kernel/kvm/linker/x86.lds new file +74 -0

74 lines not on this page · raw message

testcases/kernel/kvm/linker/x86_64.lds new file +74 -0

74 lines not on this page · raw message

Signature · 5 lines
2.35.1


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

Re: [LTP] [PATCH v3 1/2] KVM test infrastructure

Hi!
Pushed with two minor whitespace fixes, thanks.

Indentation for include directives should be done with spaces after the hash + removed whitespace before the #define for ARRAY_SIZE().

Full diff:

testcases/kernel/kvm/include/kvm_guest.h +1 -1
index 8b19f2811..ec13c5845 100644
@@ -17,7 +17,7 @@
 #define TST_TEST_TCONF(message) \
 	void main(void) { tst_brk(TCONF, message); }
 
-# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
 /* Round x up to the next multiple of a.
  * a must be a power of 2.
testcases/kernel/kvm/include/kvm_test.h +4 -8
index 55989eaa5..4d67adc95 100644
@@ -9,15 +9,11 @@
 #define KVM_TEST_H_
 
 #ifdef COMPILE_PAYLOAD
-
-  #include "kvm_guest.h"
-  #include "kvm_common.h"
-
+# include "kvm_guest.h"
+# include "kvm_common.h"
 #else
-
-  #include "tst_test.h"
-  #include "kvm_host.h"
-
+# include "tst_test.h"
+# include "kvm_host.h"
 #endif /* COMPILE_PAYLOAD */
 
 #endif /* KVM_TEST_H_ */
Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v3 2/2 Add test for CVE 2021-38198

In reply to Martin Doucha

---

Changes since v1:
- Updated .save_restore metadata to the new structure
- Use tst_parse_int() instead of sscanf()

Changes since v2: None

 runtest/kvm                            |   1 +
 testcases/kernel/kvm/.gitignore        |   1 +
 testcases/kernel/kvm/kvm_pagefault01.c | 234 +++++++++++++++++++++++++
 3 files changed, 236 insertions(+)
 create mode 100644 runtest/kvm
 create mode 100644 testcases/kernel/kvm/.gitignore
 create mode 100644 testcases/kernel/kvm/kvm_pagefault01.c
runtest/kvm new file +1 -0
new file mode 100644
index 000000000..16e7c07ff
@@ -0,0 +1 @@
+kvm_pagefault01 kvm_pagefault01
testcases/kernel/kvm/.gitignore new file +1 -0
new file mode 100644
index 000000000..349260359
@@ -0,0 +1 @@
+/kvm_pagefault01
testcases/kernel/kvm/kvm_pagefault01.c new file +234 -0

234 lines not on this page · raw message

Signature · 5 lines
2.35.1


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

Re: [LTP] [PATCH v3 2/2] Add test for CVE 2021-38198

Hi!
Pushed, thanks.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH v3 2/2] Add test for CVE 2021-38198

In reply to Martin Doucha

Hi Martin, Cyril,

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

...

testcases/kernel/kvm/kvm_pagefault01.c line 218

Quoted 11 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +11 -0
+static void setup(void)
+{
+	disable_tdp();
+	tst_kvm_setup();
+}
+
+static struct tst_test test = {
+	.test_all = tst_kvm_run,
+	.setup = setup,
+	.cleanup = tst_kvm_cleanup,
+	.needs_root = 1,

This test was merged some time ago.

FYI this .needs_root generates -Wsingle-bit-bitfield-constant-conversion error. Nothing important, maybe you see an easy way to fix it.

kvm_pagefault01.c:218:16: warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1 [-Wsingle-bit-bitfield-constant-conversion]

        .needs_root = 1,
                      ^

1 warning generated.

Kind regards,
Petr

Quoted 16 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +16 -0
+	.save_restore = (const struct tst_path_val[]) {
+		{"?/sys/module/kvm/parameters/tdp_mmu", "0"},
+		{}
+	},
+	.supported_archs = (const char *const []) {
+		"x86_64",
+		NULL
+	},
+	.tags = (struct tst_tag[]){
+		{"linux-git", "b1bd5cba3306"},
+		{"CVE", "2021-38198"},
+		{}
+	}
+};
+
+#endif /* COMPILE_PAYLOAD */
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH v3 2/2] Add test for CVE 2021-38198

Hi Martin, Cyril,

Quoted 10 lines

Hi Martin, Cyril,

testcases/kernel/kvm/kvm_pagefault01.c line 218

Quoted 5 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +5 -0
+static struct tst_test test = {
+	.test_all = tst_kvm_run,
+	.setup = setup,
+	.cleanup = tst_kvm_cleanup,
+	.needs_root = 1,

This test was merged some time ago.

Quoted 7 lines

FYI this .needs_root generates -Wsingle-bit-bitfield-constant-conversion error. Nothing important, maybe you see an easy way to fix it.

kvm_pagefault01.c:218:16: warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1 [-Wsingle-bit-bitfield-constant-conversion]

        .needs_root = 1,
                      ^

1 warning generated.

I'm sorry, this is not specific to KVM tests (and indeed, I was not able to find how this could be specific), but to any test built with clang (gcc simply does not issue this error). Obviously the problem is for any 1 bit field member in struct tst_test. The fix is quite simple - turn them to unsigned int.

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git/commit/?id=eab9100d9898cbd37882b04415b12156f8942f18

Kind regards,
Petr

Quoted 2 lines

Kind regards,
Petr

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