x86/umip: Force a page fault when unable to copy emulated result to user
authorRicardo Neri <ricardo.neri-calderon@linux.intel.com>
Mon, 6 Nov 2017 02:27:53 +0000 (18:27 -0800)
committerIngo Molnar <mingo@kernel.org>
Wed, 8 Nov 2017 10:16:22 +0000 (11:16 +0100)
fixup_umip_exception() will be called from do_general_protection(). If the
former returns false, the latter will issue a SIGSEGV with SEND_SIG_PRIV.
However, when emulation is successful but the emulated result cannot be
copied to user space memory, it is more accurate to issue a SIGSEGV with
SEGV_MAPERR with the offending address. A new function, inspired in
force_sig_info_fault(), is introduced to model the page fault.

Signed-off-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Chen Yucong <slaoub@gmail.com>
Cc: Chris Metcalf <cmetcalf@mellanox.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Huang Rui <ray.huang@amd.com>
Cc: Jiri Slaby <jslaby@suse.cz>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi V. Shankar <ravi.v.shankar@intel.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: ricardo.neri@intel.com
Link: http://lkml.kernel.org/r/1509935277-22138-9-git-send-email-ricardo.neri-calderon@linux.intel.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
arch/x86/kernel/umip.c

index d80b816..6ba82be 100644 (file)
 #include <asm/traps.h>
 #include <asm/insn.h>
 #include <asm/insn-eval.h>
+#include <linux/ratelimit.h>
+
+#undef pr_fmt
+#define pr_fmt(fmt) "umip: " fmt
 
 /** DOC: Emulation for User-Mode Instruction Prevention (UMIP)
  *
@@ -196,6 +200,41 @@ static int emulate_umip_insn(struct insn *insn, int umip_inst,
 }
 
 /**
+ * force_sig_info_umip_fault() - Force a SIGSEGV with SEGV_MAPERR
+ * @addr:      Address that caused the signal
+ * @regs:      Register set containing the instruction pointer
+ *
+ * Force a SIGSEGV signal with SEGV_MAPERR as the error code. This function is
+ * intended to be used to provide a segmentation fault when the result of the
+ * UMIP emulation could not be copied to the user space memory.
+ *
+ * Returns: none
+ */
+static void force_sig_info_umip_fault(void __user *addr, struct pt_regs *regs)
+{
+       siginfo_t info;
+       struct task_struct *tsk = current;
+
+       tsk->thread.cr2         = (unsigned long)addr;
+       tsk->thread.error_code  = X86_PF_USER | X86_PF_WRITE;
+       tsk->thread.trap_nr     = X86_TRAP_PF;
+
+       info.si_signo   = SIGSEGV;
+       info.si_errno   = 0;
+       info.si_code    = SEGV_MAPERR;
+       info.si_addr    = addr;
+       force_sig_info(SIGSEGV, &info, tsk);
+
+       if (!(show_unhandled_signals && unhandled_signal(tsk, SIGSEGV)))
+               return;
+
+       pr_err_ratelimited("%s[%d] umip emulation segfault ip:%lx sp:%lx error:%x in %lx\n",
+                          tsk->comm, task_pid_nr(tsk), regs->ip,
+                          regs->sp, X86_PF_USER | X86_PF_WRITE,
+                          regs->ip);
+}
+
+/**
  * fixup_umip_exception() - Fixup a general protection fault caused by UMIP
  * @regs:      Registers as saved when entering the #GP handler
  *
@@ -311,8 +350,14 @@ bool fixup_umip_exception(struct pt_regs *regs)
                        return false;
 
                nr_copied = copy_to_user(uaddr, dummy_data, dummy_data_size);
-               if (nr_copied  > 0)
-                       return false;
+               if (nr_copied  > 0) {
+                       /*
+                        * If copy fails, send a signal and tell caller that
+                        * fault was fixed up.
+                        */
+                       force_sig_info_umip_fault(uaddr, regs);
+                       return true;
+               }
        }
 
        /* increase IP to let the program keep going */