Skip to content

Commit d4ffd5d

Browse files
liangjssuryasaimadhu
authored andcommitted
x86/fault: Fix wrong signal when vsyscall fails with pkey
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/[email protected] ] Fixes: 5042d40 ("x86/fault: Bypass no_context() for implicit kernel faults from usermode") Reported-by: kernel test robot <[email protected]> Signed-off-by: Jiashuo Liang <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Acked-by: Dave Hansen <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent e4e737b commit d4ffd5d

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

arch/x86/include/asm/pkeys.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#ifndef _ASM_X86_PKEYS_H
33
#define _ASM_X86_PKEYS_H
44

5-
#define ARCH_DEFAULT_PKEY 0
6-
75
/*
86
* If more than 16 keys are ever supported, a thorough audit
97
* will be necessary to ensure that the types that store key

arch/x86/mm/fault.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ page_fault_oops(struct pt_regs *regs, unsigned long error_code,
710710

711711
static noinline void
712712
kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code,
713-
unsigned long address, int signal, int si_code)
713+
unsigned long address, int signal, int si_code,
714+
u32 pkey)
714715
{
715716
WARN_ON_ONCE(user_mode(regs));
716717

@@ -735,8 +736,12 @@ kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code,
735736

736737
set_signal_archinfo(address, error_code);
737738

738-
/* XXX: hwpoison faults will set the wrong code. */
739-
force_sig_fault(signal, si_code, (void __user *)address);
739+
if (si_code == SEGV_PKUERR) {
740+
force_sig_pkuerr((void __user *)address, pkey);
741+
} else {
742+
/* XXX: hwpoison faults will set the wrong code. */
743+
force_sig_fault(signal, si_code, (void __user *)address);
744+
}
740745
}
741746

742747
/*
@@ -798,7 +803,8 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
798803
struct task_struct *tsk = current;
799804

800805
if (!user_mode(regs)) {
801-
kernelmode_fixup_or_oops(regs, error_code, address, pkey, si_code);
806+
kernelmode_fixup_or_oops(regs, error_code, address,
807+
SIGSEGV, si_code, pkey);
802808
return;
803809
}
804810

@@ -930,7 +936,8 @@ do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
930936
{
931937
/* Kernel mode? Handle exceptions or die: */
932938
if (!user_mode(regs)) {
933-
kernelmode_fixup_or_oops(regs, error_code, address, SIGBUS, BUS_ADRERR);
939+
kernelmode_fixup_or_oops(regs, error_code, address,
940+
SIGBUS, BUS_ADRERR, ARCH_DEFAULT_PKEY);
934941
return;
935942
}
936943

@@ -1396,7 +1403,8 @@ void do_user_addr_fault(struct pt_regs *regs,
13961403
*/
13971404
if (!user_mode(regs))
13981405
kernelmode_fixup_or_oops(regs, error_code, address,
1399-
SIGBUS, BUS_ADRERR);
1406+
SIGBUS, BUS_ADRERR,
1407+
ARCH_DEFAULT_PKEY);
14001408
return;
14011409
}
14021410

@@ -1416,15 +1424,17 @@ void do_user_addr_fault(struct pt_regs *regs,
14161424
return;
14171425

14181426
if (fatal_signal_pending(current) && !user_mode(regs)) {
1419-
kernelmode_fixup_or_oops(regs, error_code, address, 0, 0);
1427+
kernelmode_fixup_or_oops(regs, error_code, address,
1428+
0, 0, ARCH_DEFAULT_PKEY);
14201429
return;
14211430
}
14221431

14231432
if (fault & VM_FAULT_OOM) {
14241433
/* Kernel mode? Handle exceptions or die: */
14251434
if (!user_mode(regs)) {
14261435
kernelmode_fixup_or_oops(regs, error_code, address,
1427-
SIGSEGV, SEGV_MAPERR);
1436+
SIGSEGV, SEGV_MAPERR,
1437+
ARCH_DEFAULT_PKEY);
14281438
return;
14291439
}
14301440

include/linux/pkeys.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <linux/mm.h>
66

7+
#define ARCH_DEFAULT_PKEY 0
8+
79
#ifdef CONFIG_ARCH_HAS_PKEYS
810
#include <asm/pkeys.h>
911
#else /* ! CONFIG_ARCH_HAS_PKEYS */

0 commit comments

Comments
 (0)