selftests/x86/iopl: Adjust to the faked iopl CLI/STI usage
Borislav Petkov
maintainerPATCH 1/1 selftests/x86/iopl: Adjust to the faked iopl CLI/STI usage
From: Borislav Petkov <[email protected]>
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.
--- tools/testing/selftests/x86/iopl.c | 78 ++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 20 deletions(-)
tools/testing/selftests/x86/iopl.c +58 -20
@@ -85,48 +85,88 @@ static void expect_gp_outb(unsigned short port)printf ("[OK]\toutb to 0x%02hx failed\n" , port); } -static bool try_cli (void ) +#define RET_FAULTED 0 +#define RET_FAIL 1 +#define RET_EMUL 2 + +static int try_cli (void ) { +unsigned long flags; +sethandler (SIGSEGV , sigsegv,SA_RESETHAND );if (sigsetjmp (jmpbuf,1 ) !=0 ) { -return false; +return RET_FAULTED ; }else { - asmvolatile ("cli" ); -return true; + asmvolatile ("cli; pushf; pop %[flags]" + : [flags]"=rm" (flags)); + +/* X86_FLAGS_IF */ +if (!(flags & (1 <<9 ))) +return RET_FAIL ; +else +return RET_EMUL ; }clearhandler (SIGSEGV ); } -static bool try_sti (void ) +static int try_sti (bool irqs_off) { +unsigned long flags; +sethandler (SIGSEGV , sigsegv,SA_RESETHAND );if (sigsetjmp (jmpbuf,1 ) !=0 ) { -return false; +return RET_FAULTED ; }else { - asmvolatile ("sti" ); -return true; + asmvolatile ("sti; pushf; pop %[flags]" + : [flags]"=rm" (flags)); + +/* X86_FLAGS_IF */ +if (irqs_off && (flags & (1 <<9 ))) +return RET_FAIL ; +else +return RET_EMUL ; }clearhandler (SIGSEGV ); } -static void expect_gp_sti (void ) +static void expect_gp_sti (bool irqs_off) { -if (try_sti ()) { +int ret =try_sti (irqs_off); + +switch (ret) { +case RET_FAULTED : +printf ("[OK]\tSTI faulted\n" ); +break ; +case RET_EMUL : +printf ("[OK]\tSTI NOPped\n" ); +break ; +default :printf ("[FAIL]\tSTI worked\n" ); nerrs++; - }else { -printf ("[OK]\tSTI faulted\n" ); } } -static void expect_gp_cli (void ) +/* +* Returns whether it managed to disable interrupts. +*/ +static bool test_cli (void ) { -if (try_cli ()) { +int ret =try_cli (); + +switch (ret) { +case RET_FAULTED : +printf ("[OK]\tCLI faulted\n" ); +break ; +case RET_EMUL : +printf ("[OK]\tCLI NOPped\n" ); +break ; +default :printf ("[FAIL]\tCLI worked\n" ); nerrs++; - }else { -printf ("[OK]\tCLI faulted\n" ); +return true; } + +return false; }int main (void )
@@ -152,8 +192,7 @@ int main(void) }/* Make sure that CLI/STI are blocked even with IOPL level 3 */ -expect_gp_cli (); -expect_gp_sti (); +expect_gp_sti (test_cli ());expect_ok_outb (0x80 );/* Establish an I/O bitmap to test the restore */
@@ -204,8 +243,7 @@ int main(void)printf ("[RUN]\tparent: write to 0x80 (should fail)\n" );expect_gp_outb (0x80 ); -expect_gp_cli (); -expect_gp_sti (); +expect_gp_sti (test_cli ());/* Test the capability checks. */ printf ("\tiopl(3)\n" );
Signature · 1 line
2.29.2
Quoted 16 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
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.
Quoted 3 lines
Fixes b968e84b509d ("x86/iopl: Fake iopl(3) CLI/STI usage")Suggested-by Thomas Gleixner <[email protected]>Signed-off-by Borislav Petkov <[email protected]>
The following commit has been merged into the x86/core branch of tip:
The following commit has been merged into the x86/core branch of tip:
Commit-ID: a72fdfd21e01c626273ddcf5ab740d4caef4be54 Gitweb: https://git.kernel.org/tip/a72fdfd21e01c626273ddcf5ab740d4caef4be54 Author: Borislav Petkov <[email protected]> AuthorDate: Fri, 29 Oct 2021 19:27:32 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Sat, 30 Oct 2021 23:18:04 +02:00
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.
--- tools/testing/selftests/x86/iopl.c | 78 +++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 20 deletions(-)
tools/testing/selftests/x86/iopl.c +58 -20
@@ -85,48 +85,88 @@ static void expect_gp_outb(unsigned short port)printf ("[OK]\toutb to 0x%02hx failed\n" , port); } -static bool try_cli (void ) +#define RET_FAULTED 0 +#define RET_FAIL 1 +#define RET_EMUL 2 + +static int try_cli (void ) { +unsigned long flags; +sethandler (SIGSEGV , sigsegv,SA_RESETHAND );if (sigsetjmp (jmpbuf,1 ) !=0 ) { -return false; +return RET_FAULTED ; }else { - asmvolatile ("cli" ); -return true; + asmvolatile ("cli; pushf; pop %[flags]" + : [flags]"=rm" (flags)); + +/* X86_FLAGS_IF */ +if (!(flags & (1 <<9 ))) +return RET_FAIL ; +else +return RET_EMUL ; }clearhandler (SIGSEGV ); } -static bool try_sti (void ) +static int try_sti (bool irqs_off) { +unsigned long flags; +sethandler (SIGSEGV , sigsegv,SA_RESETHAND );if (sigsetjmp (jmpbuf,1 ) !=0 ) { -return false; +return RET_FAULTED ; }else { - asmvolatile ("sti" ); -return true; + asmvolatile ("sti; pushf; pop %[flags]" + : [flags]"=rm" (flags)); + +/* X86_FLAGS_IF */ +if (irqs_off && (flags & (1 <<9 ))) +return RET_FAIL ; +else +return RET_EMUL ; }clearhandler (SIGSEGV ); } -static void expect_gp_sti (void ) +static void expect_gp_sti (bool irqs_off) { -if (try_sti ()) { +int ret =try_sti (irqs_off); + +switch (ret) { +case RET_FAULTED : +printf ("[OK]\tSTI faulted\n" ); +break ; +case RET_EMUL : +printf ("[OK]\tSTI NOPped\n" ); +break ; +default :printf ("[FAIL]\tSTI worked\n" ); nerrs++; - }else { -printf ("[OK]\tSTI faulted\n" ); } } -static void expect_gp_cli (void ) +/* +* Returns whether it managed to disable interrupts. +*/ +static bool test_cli (void ) { -if (try_cli ()) { +int ret =try_cli (); + +switch (ret) { +case RET_FAULTED : +printf ("[OK]\tCLI faulted\n" ); +break ; +case RET_EMUL : +printf ("[OK]\tCLI NOPped\n" ); +break ; +default :printf ("[FAIL]\tCLI worked\n" ); nerrs++; - }else { -printf ("[OK]\tCLI faulted\n" ); +return true; } + +return false; }int main (void )
@@ -152,8 +192,7 @@ int main(void) }/* Make sure that CLI/STI are blocked even with IOPL level 3 */ -expect_gp_cli (); -expect_gp_sti (); +expect_gp_sti (test_cli ());expect_ok_outb (0x80 );/* Establish an I/O bitmap to test the restore */
@@ -204,8 +243,7 @@ int main(void)printf ("[RUN]\tparent: write to 0x80 (should fail)\n" );expect_gp_outb (0x80 ); -expect_gp_cli (); -expect_gp_sti (); +expect_gp_sti (test_cli ());/* Test the capability checks. */ printf ("\tiopl(3)\n" );