x86/iopl: Fake iopl(3) CLI/STI usage
authorPeter Zijlstra <peterz@infradead.org>
Fri, 17 Sep 2021 09:20:04 +0000 (11:20 +0200)
committerPeter Zijlstra <peterz@infradead.org>
Tue, 21 Sep 2021 11:52:18 +0000 (13:52 +0200)
Since commit c8137ace5638 ("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: a24ca9976843 ("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
arch/x86/include/asm/insn-eval.h
arch/x86/include/asm/processor.h
arch/x86/kernel/process.c
arch/x86/kernel/traps.c
arch/x86/lib/insn-eval.c

index 91d7182ad2d6e7825f1b35db0d38d95f592baabc..4ec3613551e3b5b918840ade8bb9bca986461075 100644 (file)
@@ -21,6 +21,7 @@ int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs);
 int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs);
 unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx);
 int insn_get_code_seg_params(struct pt_regs *regs);
+int insn_get_effective_ip(struct pt_regs *regs, unsigned long *ip);
 int insn_fetch_from_user(struct pt_regs *regs,
                         unsigned char buf[MAX_INSN_SIZE]);
 int insn_fetch_from_user_inatomic(struct pt_regs *regs,
index 9ad2acaaae9b84be5bece5730bfb8e7c746cc892..577f342dbfb27fbd16c1cb38482c969a61887958 100644 (file)
@@ -518,6 +518,7 @@ struct thread_struct {
         */
        unsigned long           iopl_emul;
 
+       unsigned int            iopl_warn:1;
        unsigned int            sig_on_uaccess_err:1;
 
        /*
index 1d9463e3096b68307e96445026e19cd01d98464e..f2f733bcb2b959483717f3f7219ad76c9b9f57d8 100644 (file)
@@ -132,6 +132,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg,
        frame->ret_addr = (unsigned long) ret_from_fork;
        p->thread.sp = (unsigned long) fork_frame;
        p->thread.io_bitmap = NULL;
+       p->thread.iopl_warn = 0;
        memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
 #ifdef CONFIG_X86_64
index a58800973aed3a16cbb8d97500bae51eb8306716..f3f3034b06f346717a3693e1177a5e0dd396080e 100644 (file)
@@ -528,6 +528,36 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
 
 #define GPFSTR "general protection fault"
 
+static bool fixup_iopl_exception(struct pt_regs *regs)
+{
+       struct thread_struct *t = &current->thread;
+       unsigned char byte;
+       unsigned long ip;
+
+       if (!IS_ENABLED(CONFIG_X86_IOPL_IOPERM) || t->iopl_emul != 3)
+               return false;
+
+       if (insn_get_effective_ip(regs, &ip))
+               return false;
+
+       if (get_user(byte, (const char __user *)ip))
+               return false;
+
+       if (byte != 0xfa && byte != 0xfb)
+               return false;
+
+       if (!t->iopl_warn && printk_ratelimit()) {
+               pr_err("%s[%d] attempts to use CLI/STI, pretending it's a NOP, ip:%lx",
+                      current->comm, task_pid_nr(current), ip);
+               print_vma_addr(KERN_CONT " in ", ip);
+               pr_cont("\n");
+               t->iopl_warn = 1;
+       }
+
+       regs->ip += 1;
+       return true;
+}
+
 DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
 {
        char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
@@ -553,6 +583,9 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
        tsk = current;
 
        if (user_mode(regs)) {
+               if (fixup_iopl_exception(regs))
+                       goto exit;
+
                tsk->thread.error_code = error_code;
                tsk->thread.trap_nr = X86_TRAP_GP;
 
index a1d24fdc07cf0280ab33cabe0e573f4795049820..eb3ccffb9b9dcc6ffe3373a8ec1e3d259c809b12 100644 (file)
@@ -1417,7 +1417,7 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
        }
 }
 
-static int insn_get_effective_ip(struct pt_regs *regs, unsigned long *ip)
+int insn_get_effective_ip(struct pt_regs *regs, unsigned long *ip)
 {
        unsigned long seg_base = 0;