Skip to content

Commit 56e62cd

Browse files
amlutosuryasaimadhu
authored andcommitted
x86/fault: Correct a few user vs kernel checks wrt WRUSS
In general, page fault errors for WRUSS should be just like get_user(), etc. Fix three bugs in this area: There is a comment that says that, if the kernel can't handle a page fault on a user address due to OOM, the OOM-kill-and-retry logic would be skipped. The code checked kernel *privilege*, not kernel mode, so it missed WRUSS. This means that the kernel would malfunction if it got OOM on a WRUSS fault -- this would be a kernel-mode, user-privilege fault, and the OOM killer would be invoked and the handler would retry the faulting instruction. A failed user access from kernel while a fatal signal is pending should fail even if the instruction in question was WRUSS. do_sigbus() should not send SIGBUS for WRUSS -- it should handle it like any other kernel mode failure. Signed-off-by: Andy Lutomirski <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/a7b7bcea730bd4069e6b7e629236bb2cf526c2fb.1612924255.git.luto@kernel.org
1 parent ef2544f commit 56e62cd

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

arch/x86/mm/fault.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
945945
vm_fault_t fault)
946946
{
947947
/* Kernel mode? Handle exceptions or die: */
948-
if (!(error_code & X86_PF_USER)) {
948+
if (!user_mode(regs)) {
949949
no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
950950
return;
951951
}
@@ -1217,7 +1217,14 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,
12171217
}
12181218
NOKPROBE_SYMBOL(do_kern_addr_fault);
12191219

1220-
/* Handle faults in the user portion of the address space */
1220+
/*
1221+
* Handle faults in the user portion of the address space. Nothing in here
1222+
* should check X86_PF_USER without a specific justification: for almost
1223+
* all purposes, we should treat a normal kernel access to user memory
1224+
* (e.g. get_user(), put_user(), etc.) the same as the WRUSS instruction.
1225+
* The one exception is AC flag handling, which is, per the x86
1226+
* architecture, special for WRUSS.
1227+
*/
12211228
static inline
12221229
void do_user_addr_fault(struct pt_regs *regs,
12231230
unsigned long error_code,
@@ -1406,14 +1413,14 @@ void do_user_addr_fault(struct pt_regs *regs,
14061413
if (likely(!(fault & VM_FAULT_ERROR)))
14071414
return;
14081415

1409-
if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
1416+
if (fatal_signal_pending(current) && !user_mode(regs)) {
14101417
no_context(regs, error_code, address, 0, 0);
14111418
return;
14121419
}
14131420

14141421
if (fault & VM_FAULT_OOM) {
14151422
/* Kernel mode? Handle exceptions or die: */
1416-
if (!(error_code & X86_PF_USER)) {
1423+
if (!user_mode(regs)) {
14171424
no_context(regs, error_code, address,
14181425
SIGSEGV, SEGV_MAPERR);
14191426
return;

0 commit comments

Comments
 (0)