Skip to content

Commit b31bac0

Browse files
Peter Zijlstragregkh
authored andcommitted
x86/iopl: Fake iopl(3) CLI/STI usage
commit b968e84 upstream. 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 <linux@zary.sk> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: stable@kernel.org # v5.5+ Link: https://lkml.kernel.org/r/20210918090641.GD5106@worktop.programming.kicks-ass.net Signed-off-by: Ondrej Zary <linux@zary.sk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a0958a5 commit b31bac0

5 files changed

Lines changed: 38 additions & 1 deletion

File tree

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+
unsigned long insn_get_effective_ip(struct pt_regs *regs);
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
@@ -534,6 +534,7 @@ struct thread_struct {
534534
*/
535535
unsigned long iopl_emul;
536536

537+
unsigned int iopl_warn:1;
537538
unsigned int sig_on_uaccess_err:1;
538539

539540
/* Floating point and extended processor state */

arch/x86/kernel/process.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg,
138138
frame->ret_addr = (unsigned long) ret_from_fork;
139139
p->thread.sp = (unsigned long) fork_frame;
140140
p->thread.io_bitmap = NULL;
141+
p->thread.iopl_warn = 0;
141142
memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
142143

143144
#ifdef CONFIG_X86_64

arch/x86/kernel/traps.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,37 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
523523

524524
#define GPFSTR "general protection fault"
525525

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

550581
if (user_mode(regs)) {
582+
if (fixup_iopl_exception(regs))
583+
goto exit;
584+
551585
tsk->thread.error_code = error_code;
552586
tsk->thread.trap_nr = X86_TRAP_GP;
553587

arch/x86/lib/insn-eval.c

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

1418-
static unsigned long insn_get_effective_ip(struct pt_regs *regs)
1418+
unsigned long insn_get_effective_ip(struct pt_regs *regs)
14191419
{
14201420
unsigned long seg_base = 0;
14211421

0 commit comments

Comments
 (0)