Skip to contentSkip to conversation

Add Intel umip(User Mode Instruction Prevention) basic function tests

PATCH Superseded Pengfei Xu 2 participants 3 messages Jan 16, 2019 – Jan 17, 2019

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Pengfei Xu

author
#0 lore raw

PATCH v5 1/1 Add Intel umip(User Mode Instruction Prevention) basic function tests

umip is a security feature present in new Intel Processors. Intel CPU like ICE lake or newer is required.
When umip enabled, it prevents the execution of certain instructions and situation as below:
when Current Privilege Level (CPL) is greater than 0, user space applications could not access to system-wide settings such as the global and local descriptor tables, the segment selectors to the current task state and the local descriptor table.
umip is enabled by default at boot.
umip will protect below instructions in user mode:
* SGDT - Store Global Descriptor Table
* SIDT - Store Interrupt Descriptor Table
* SLDT - Store Local Descriptor Table
* SMSW - Store Machine Status Word
* STR - Store Task Register
If CPU not support umip, you still could comment cpuinfo check code to verify anyway.

---
 runtest/secure_umip                           |   2 +
 testcases/kernel/security/umip/.gitignore     |   1 +
 testcases/kernel/security/umip/Makefile       |   7 +
 testcases/kernel/security/umip/umip_gp_test.c | 185 ++++++++++++++++++++++++++
 4 files changed, 195 insertions(+)
 create mode 100644 runtest/secure_umip
 create mode 100644 testcases/kernel/security/umip/.gitignore
 create mode 100644 testcases/kernel/security/umip/Makefile
 create mode 100644 testcases/kernel/security/umip/umip_gp_test.c
runtest/secure_umip new file +2 -0
new file mode 100644
index 000000000..4082bc2a3
@@ -0,0 +1,2 @@
+#DESCRIPTION: CPU security feature UMIP test
+umip_instruction_test umip_gp_test
testcases/kernel/security/umip/.gitignore new file +1 -0
new file mode 100644
index 000000000..9e7022c59
@@ -0,0 +1 @@
+umip_gp_test
testcases/kernel/security/umip/Makefile new file +7 -0
new file mode 100644
index 000000000..18896b6f2
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/security/umip/umip_gp_test.c new file +185 -0

185 lines not on this page · raw message

Signature · 1 line
2.14.1
#1 lore raw

PATCH v5 1/1 Add Intel umip(User Mode Instruction Prevention) basic function tests

Hi!

Quoted 22 lines
testcases/kernel/security/umip/umip_gp_test.c new file +22 -0
+static void verify_umip_instruction(unsigned int n)
+{
+	int status;
+	struct tcase *tc = &tcases[n];
+	pid_t pid;
+
+	pid = SAFE_FORK();
+	if (pid == 0) {
+		switch (tc->option) {
+		case 0:
+			asm_sgdt();
+			break;
+		case 1:
+			asm_sidt();
+			break;
+		case 2:
+			asm_sldt();
+			break;
+		case 3:
+			asm_smsw();
+			break;
+		case 4:

testcases/kernel/security/umip/umip_gp_test.c line 132

Quoted 10 lines
testcases/kernel/security/umip/umip_gp_test.c new file +10 -0
+			asm_str();
+			break;
+		default:
+			tst_brk(TCONF, "Invalid tcase parameter:%d",
+				tc->option);
+		}
+	} else {
+		// let child process execute first
+		sleep(1);
+		SAFE_KILL(pid, SIGINT);

Is this still needed? As far as I can tell the child either gets killed by kernel or calls exit(0) right after it has been started.

I do frown upon every use of sleep() in tests and let me just explain the reason why.

We do have about 3000 of tests in LTP, if each of the tests was calling sleep() carelessly that would add up to an hour of useless waiting and we do care about the testsuite to finish as fast as possible for several reasons. Mainly because no sane developer would use tests that take so long to run. Minimizing the time needed to test a code change is hence the only way how the ensure that the tests are actually used in production and tests that are not used are completely pointless to begin with.

The second reason why sleep(1) is wrong is that, while it works fine in 99% of the cases, there is no guarantee that the child would actually finish under 1 second. In this case this would probably never be a problem, but if you did anything more compled in the child than executing a single instruction it will became a real problem as well.

Quoted 12 lines
testcases/kernel/security/umip/umip_gp_test.c new file +12 -0
+	}
+
+	SAFE_WAITPID(pid, &status, 0);
+
+	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
+		tst_res(TPASS, "Got SIGSEGV");
+		return;
+	}
+	tst_res(TFAIL, "Didn't receive SIGSEGV, child exited with %s",
+		tst_strstatus(status));
+}
+

testcases/kernel/security/umip/umip_gp_test.c line 154

Quoted 10 lines
testcases/kernel/security/umip/umip_gp_test.c new file +10 -0
+static void setup(void)
+{
+	FILE *fp;
+	int max = 2048;
+	char buf[max];
+
+	// cpuinfo should contain umip
+	fp = SAFE_FOPEN(CPUINFO_FILE, "r");
+	while (!feof(fp)) {
+		if (fgets(buf, sizeof(buf), fp) == NULL) {

You should close the file here.

Quoted 11 lines
testcases/kernel/security/umip/umip_gp_test.c new file +11 -0
+			tst_brk(TCONF, "cpuinfo show: cpu does not support umip");
+		}
+
+		if (!strstr(buf, "flags"))
+			continue;
+
+		if (strstr(buf, "umip")) {
+			tst_res(TINFO, "cpuinfo contain umip, cpu support umip");
+			break;
+		} else
+			continue;

testcases/kernel/security/umip/umip_gp_test.c line 175

Quoted 10 lines
testcases/kernel/security/umip/umip_gp_test.c new file +10 -0
+	}
+
+	SAFE_FCLOSE(fp);
+}
+
+static void cleanup(void)
+{
+	if (fd != -1)
+		SAFE_CLOSE(fd);
+}

This is now completely unused, let's get rid of it.

Quoted 12 lines
testcases/kernel/security/umip/umip_gp_test.c new file +9 -0
+static struct tst_test test = {
+	.min_kver = "4.1",
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.forks_child = 1,
+	.test = verify_umip_instruction,
+	.cleanup = cleanup,
+	.needs_root = 1,
+};
Signature · 1 line
2.14.1
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH v5 1/1 Add Intel umip(User Mode Instruction Prevention) basic function tests

Hi Cyril,
Thanks a lot for your advice.
Already correct them and resend again.
Thanks again.

BR.

Quoted 28 lines · Cyril Hrubis

Hi!

testcases/kernel/security/umip/umip_gp_test.c line 132

Quoted 11 lines
testcases/kernel/security/umip/umip_gp_test.c new file +11 -0
+		case 4:
+			asm_str();
+			break;
+		default:
+			tst_brk(TCONF, "Invalid tcase parameter:%d",
+				tc->option);
+		}
+	} else {
+		// let child process execute first
+		sleep(1);
+		SAFE_KILL(pid, SIGINT);

Is this still needed? As far as I can tell the child either gets killed by kernel or calls exit(0) right after it has been started.

I do frown upon every use of sleep() in tests and let me just explain the reason why.

We do have about 3000 of tests in LTP, if each of the tests was calling sleep() carelessly that would add up to an hour of useless waiting and we do care about the testsuite to finish as fast as possible for several reasons. Mainly because no sane developer would use tests that take so long to run. Minimizing the time needed to test a code change is hence the only way how the ensure that the tests are actually used in production and tests that are not used are completely pointless to begin with.

Quoted 5 lines

The second reason why sleep(1) is wrong is that, while it works fine in 99% of the cases, there is no guarantee that the child would actually finish under 1 second. In this case this would probably never be a problem, but if you did anything more compled in the child than executing a single instruction it will became a real problem as well.

Yes no need sleep and SAFE_KILL in parent process, already remove them. Thanks.

Quoted 17 lines

testcases/kernel/security/umip/umip_gp_test.c line 154

Quoted 6 lines
testcases/kernel/security/umip/umip_gp_test.c new file +6 -0
+	}
+
+	// cpuinfo should contain umip
+	fp = SAFE_FOPEN(CPUINFO_FILE, "r");
+	while (!feof(fp)) {
+		if (fgets(buf, sizeof(buf), fp) == NULL) {

You should close the file here.

Quoted 4 lines
testcases/kernel/security/umip/umip_gp_test.c new file +4 -0
+			tst_brk(TCONF, "cpuinfo show: cpu does not support umip");
+		}
+
+		if (!strstr(buf, "flags"))
Signature · 2 lines
Cyril Hrubis
[email protected]