Skip to content

Commit a72fdfd

Browse files
committed
selftests/x86/iopl: Adjust to the faked iopl CLI/STI usage
Commit in Fixes changed the iopl emulation to not #GP on CLI and STI because it would break some insane luserspace tools which would toggle interrupts. The corresponding selftest would rely on the fact that executing CLI/STI would trigger a #GP and thus detect it this way but since that #GP is not happening anymore, the detection is now wrong too. Extend the test to actually look at the IF flag and whether executing those insns had any effect on it. The STI detection needs to have the fact that interrupts were previously disabled, passed in so do that from the previous CLI test, i.e., STI test needs to follow a previous CLI one for it to make sense. Fixes: b968e84 ("x86/iopl: Fake iopl(3) CLI/STI usage") Suggested-by: Thomas Gleixner <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Acked-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ca136ca commit a72fdfd

File tree

1 file changed

+58
-20
lines changed
  • tools/testing/selftests/x86

1 file changed

+58
-20
lines changed

tools/testing/selftests/x86/iopl.c

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,48 +85,88 @@ static void expect_gp_outb(unsigned short port)
8585
printf("[OK]\toutb to 0x%02hx failed\n", port);
8686
}
8787

88-
static bool try_cli(void)
88+
#define RET_FAULTED 0
89+
#define RET_FAIL 1
90+
#define RET_EMUL 2
91+
92+
static int try_cli(void)
8993
{
94+
unsigned long flags;
95+
9096
sethandler(SIGSEGV, sigsegv, SA_RESETHAND);
9197
if (sigsetjmp(jmpbuf, 1) != 0) {
92-
return false;
98+
return RET_FAULTED;
9399
} else {
94-
asm volatile ("cli");
95-
return true;
100+
asm volatile("cli; pushf; pop %[flags]"
101+
: [flags] "=rm" (flags));
102+
103+
/* X86_FLAGS_IF */
104+
if (!(flags & (1 << 9)))
105+
return RET_FAIL;
106+
else
107+
return RET_EMUL;
96108
}
97109
clearhandler(SIGSEGV);
98110
}
99111

100-
static bool try_sti(void)
112+
static int try_sti(bool irqs_off)
101113
{
114+
unsigned long flags;
115+
102116
sethandler(SIGSEGV, sigsegv, SA_RESETHAND);
103117
if (sigsetjmp(jmpbuf, 1) != 0) {
104-
return false;
118+
return RET_FAULTED;
105119
} else {
106-
asm volatile ("sti");
107-
return true;
120+
asm volatile("sti; pushf; pop %[flags]"
121+
: [flags] "=rm" (flags));
122+
123+
/* X86_FLAGS_IF */
124+
if (irqs_off && (flags & (1 << 9)))
125+
return RET_FAIL;
126+
else
127+
return RET_EMUL;
108128
}
109129
clearhandler(SIGSEGV);
110130
}
111131

112-
static void expect_gp_sti(void)
132+
static void expect_gp_sti(bool irqs_off)
113133
{
114-
if (try_sti()) {
134+
int ret = try_sti(irqs_off);
135+
136+
switch (ret) {
137+
case RET_FAULTED:
138+
printf("[OK]\tSTI faulted\n");
139+
break;
140+
case RET_EMUL:
141+
printf("[OK]\tSTI NOPped\n");
142+
break;
143+
default:
115144
printf("[FAIL]\tSTI worked\n");
116145
nerrs++;
117-
} else {
118-
printf("[OK]\tSTI faulted\n");
119146
}
120147
}
121148

122-
static void expect_gp_cli(void)
149+
/*
150+
* Returns whether it managed to disable interrupts.
151+
*/
152+
static bool test_cli(void)
123153
{
124-
if (try_cli()) {
154+
int ret = try_cli();
155+
156+
switch (ret) {
157+
case RET_FAULTED:
158+
printf("[OK]\tCLI faulted\n");
159+
break;
160+
case RET_EMUL:
161+
printf("[OK]\tCLI NOPped\n");
162+
break;
163+
default:
125164
printf("[FAIL]\tCLI worked\n");
126165
nerrs++;
127-
} else {
128-
printf("[OK]\tCLI faulted\n");
166+
return true;
129167
}
168+
169+
return false;
130170
}
131171

132172
int main(void)
@@ -152,8 +192,7 @@ int main(void)
152192
}
153193

154194
/* Make sure that CLI/STI are blocked even with IOPL level 3 */
155-
expect_gp_cli();
156-
expect_gp_sti();
195+
expect_gp_sti(test_cli());
157196
expect_ok_outb(0x80);
158197

159198
/* Establish an I/O bitmap to test the restore */
@@ -204,8 +243,7 @@ int main(void)
204243
printf("[RUN]\tparent: write to 0x80 (should fail)\n");
205244

206245
expect_gp_outb(0x80);
207-
expect_gp_cli();
208-
expect_gp_sti();
246+
expect_gp_sti(test_cli());
209247

210248
/* Test the capability checks. */
211249
printf("\tiopl(3)\n");

0 commit comments

Comments
 (0)