]> git.baikalelectronics.ru Git - kernel.git/commitdiff
x86/fault: Fix wrong signal when vsyscall fails with pkey
authorJiashuo Liang <liangjs@pku.edu.cn>
Fri, 30 Jul 2021 03:01:52 +0000 (11:01 +0800)
committerBorislav Petkov <bp@suse.de>
Mon, 20 Sep 2021 20:28:47 +0000 (22:28 +0200)
The function __bad_area_nosemaphore() calls kernelmode_fixup_or_oops()
with the parameter @signal being actually @pkey, which will send a
signal numbered with the argument in @pkey.

This bug can be triggered when the kernel fails to access user-given
memory pages that are protected by a pkey, so it can go down the
do_user_addr_fault() path and pass the !user_mode() check in
__bad_area_nosemaphore().

Most cases will simply run the kernel fixup code to make an -EFAULT. But
when another condition current->thread.sig_on_uaccess_err is met, which
is only used to emulate vsyscall, the kernel will generate the wrong
signal.

Add a new parameter @pkey to kernelmode_fixup_or_oops() to fix this.

 [ bp: Massage commit message, fix build error as reported by the 0day
   bot: https://lkml.kernel.org/r/202109202245.APvuT8BX-lkp@intel.com ]

Fixes: 02edd4ebb1af ("x86/fault: Bypass no_context() for implicit kernel faults from usermode")
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Jiashuo Liang <liangjs@pku.edu.cn>
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Link: https://lkml.kernel.org/r/20210730030152.249106-1-liangjs@pku.edu.cn
arch/x86/include/asm/pkeys.h
arch/x86/mm/fault.c
include/linux/pkeys.h

index 5c7bcaa7962323ba29dc825c00d2d7d8a2234417..1d5f14aff5f6fd975143874cf1010b32c999ee11 100644 (file)
@@ -2,8 +2,6 @@
 #ifndef _ASM_X86_PKEYS_H
 #define _ASM_X86_PKEYS_H
 
-#define ARCH_DEFAULT_PKEY      0
-
 /*
  * If more than 16 keys are ever supported, a thorough audit
  * will be necessary to ensure that the types that store key
index b2eefdefc1083316ea27e6bfd8bb1ddbcea715ab..84a2c8c4af7358b67c3ab3257103a76559087e20 100644 (file)
@@ -710,7 +710,8 @@ oops:
 
 static noinline void
 kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code,
-                        unsigned long address, int signal, int si_code)
+                        unsigned long address, int signal, int si_code,
+                        u32 pkey)
 {
        WARN_ON_ONCE(user_mode(regs));
 
@@ -735,8 +736,12 @@ kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code,
 
                        set_signal_archinfo(address, error_code);
 
-                       /* XXX: hwpoison faults will set the wrong code. */
-                       force_sig_fault(signal, si_code, (void __user *)address);
+                       if (si_code == SEGV_PKUERR) {
+                               force_sig_pkuerr((void __user *)address, pkey);
+                       } else {
+                               /* XXX: hwpoison faults will set the wrong code. */
+                               force_sig_fault(signal, si_code, (void __user *)address);
+                       }
                }
 
                /*
@@ -798,7 +803,8 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
        struct task_struct *tsk = current;
 
        if (!user_mode(regs)) {
-               kernelmode_fixup_or_oops(regs, error_code, address, pkey, si_code);
+               kernelmode_fixup_or_oops(regs, error_code, address,
+                                        SIGSEGV, si_code, pkey);
                return;
        }
 
@@ -930,7 +936,8 @@ do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
 {
        /* Kernel mode? Handle exceptions or die: */
        if (!user_mode(regs)) {
-               kernelmode_fixup_or_oops(regs, error_code, address, SIGBUS, BUS_ADRERR);
+               kernelmode_fixup_or_oops(regs, error_code, address,
+                                        SIGBUS, BUS_ADRERR, ARCH_DEFAULT_PKEY);
                return;
        }
 
@@ -1396,7 +1403,8 @@ good_area:
                 */
                if (!user_mode(regs))
                        kernelmode_fixup_or_oops(regs, error_code, address,
-                                                SIGBUS, BUS_ADRERR);
+                                                SIGBUS, BUS_ADRERR,
+                                                ARCH_DEFAULT_PKEY);
                return;
        }
 
@@ -1416,7 +1424,8 @@ good_area:
                return;
 
        if (fatal_signal_pending(current) && !user_mode(regs)) {
-               kernelmode_fixup_or_oops(regs, error_code, address, 0, 0);
+               kernelmode_fixup_or_oops(regs, error_code, address,
+                                        0, 0, ARCH_DEFAULT_PKEY);
                return;
        }
 
@@ -1424,7 +1433,8 @@ good_area:
                /* Kernel mode? Handle exceptions or die: */
                if (!user_mode(regs)) {
                        kernelmode_fixup_or_oops(regs, error_code, address,
-                                                SIGSEGV, SEGV_MAPERR);
+                                                SIGSEGV, SEGV_MAPERR,
+                                                ARCH_DEFAULT_PKEY);
                        return;
                }
 
index 6beb26b7151d21c18c7c3dd52c73290c0d8d48c5..86be8bf27b41b773f3957c7b5ff63f6592e8ab18 100644 (file)
@@ -4,6 +4,8 @@
 
 #include <linux/mm.h>
 
+#define ARCH_DEFAULT_PKEY      0
+
 #ifdef CONFIG_ARCH_HAS_PKEYS
 #include <asm/pkeys.h>
 #else /* ! CONFIG_ARCH_HAS_PKEYS */