Skip to content

Commit b968e84

Browse files
author
Peter Zijlstra
committed
x86/iopl: Fake iopl(3) CLI/STI usage
Since commit c8137ac ("x86/iopl: Restrict iopl() permission scope") it's possible to emulate iopl(3) using ioperm(), except for the CLI/STI usage. Userspace CLI/STI usage is very dubious (read broken), since any exception taken during that window can lead to rescheduling anyway (or worse). The IOPL(2) manpage even states that usage of CLI/STI is highly discouraged and might even crash the system. Of course, that won't stop people and HP has the dubious honour of being the first vendor to be found using this in their hp-health package. In order to enable this 'software' to still 'work', have the #GP treat the CLI/STI instructions as NOPs when iopl(3). Warn the user that their program is doing dubious things. Fixes: a24ca99 ("x86/iopl: Remove legacy IOPL option") Reported-by: Ondrej Zary <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Thomas Gleixner <[email protected]> Cc: [email protected] # v5.5+ Link: https://lkml.kernel.org/r/[email protected]
1 parent 6880fa6 commit b968e84

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

arch/x86/include/asm/insn-eval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs);
2121
int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs);
2222
unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx);
2323
int insn_get_code_seg_params(struct pt_regs *regs);
24+
int insn_get_effective_ip(struct pt_regs *regs, unsigned long *ip);
2425
int insn_fetch_from_user(struct pt_regs *regs,
2526
unsigned char buf[MAX_INSN_SIZE]);
2627
int insn_fetch_from_user_inatomic(struct pt_regs *regs,

arch/x86/include/asm/processor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ struct thread_struct {
518518
*/
519519
unsigned long iopl_emul;
520520

521+
unsigned int iopl_warn:1;
521522
unsigned int sig_on_uaccess_err:1;
522523

523524
/*

arch/x86/kernel/process.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg,
132132
frame->ret_addr = (unsigned long) ret_from_fork;
133133
p->thread.sp = (unsigned long) fork_frame;
134134
p->thread.io_bitmap = NULL;
135+
p->thread.iopl_warn = 0;
135136
memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
136137

137138
#ifdef CONFIG_X86_64

arch/x86/kernel/traps.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,36 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
528528

529529
#define GPFSTR "general protection fault"
530530

531+
static bool fixup_iopl_exception(struct pt_regs *regs)
532+
{
533+
struct thread_struct *t = &current->thread;
534+
unsigned char byte;
535+
unsigned long ip;
536+
537+
if (!IS_ENABLED(CONFIG_X86_IOPL_IOPERM) || t->iopl_emul != 3)
538+
return false;
539+
540+
if (insn_get_effective_ip(regs, &ip))
541+
return false;
542+
543+
if (get_user(byte, (const char __user *)ip))
544+
return false;
545+
546+
if (byte != 0xfa && byte != 0xfb)
547+
return false;
548+
549+
if (!t->iopl_warn && printk_ratelimit()) {
550+
pr_err("%s[%d] attempts to use CLI/STI, pretending it's a NOP, ip:%lx",
551+
current->comm, task_pid_nr(current), ip);
552+
print_vma_addr(KERN_CONT " in ", ip);
553+
pr_cont("\n");
554+
t->iopl_warn = 1;
555+
}
556+
557+
regs->ip += 1;
558+
return true;
559+
}
560+
531561
DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
532562
{
533563
char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
@@ -553,6 +583,9 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
553583
tsk = current;
554584

555585
if (user_mode(regs)) {
586+
if (fixup_iopl_exception(regs))
587+
goto exit;
588+
556589
tsk->thread.error_code = error_code;
557590
tsk->thread.trap_nr = X86_TRAP_GP;
558591

arch/x86/lib/insn-eval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
14171417
}
14181418
}
14191419

1420-
static int insn_get_effective_ip(struct pt_regs *regs, unsigned long *ip)
1420+
int insn_get_effective_ip(struct pt_regs *regs, unsigned long *ip)
14211421
{
14221422
unsigned long seg_base = 0;
14231423

0 commit comments

Comments
 (0)