linux-stable/arch/x86
Linus Torvalds 27fba38ddd x86/mm: Remove broken vsyscall emulation code from the page fault code
commit 02b670c1f8 upstream.

The syzbot-reported stack trace from hell in this discussion thread
actually has three nested page faults:

  https://lore.kernel.org/r/000000000000d5f4fc0616e816d4@google.com

... and I think that's actually the important thing here:

 - the first page fault is from user space, and triggers the vsyscall
   emulation.

 - the second page fault is from __do_sys_gettimeofday(), and that should
   just have caused the exception that then sets the return value to
   -EFAULT

 - the third nested page fault is due to _raw_spin_unlock_irqrestore() ->
   preempt_schedule() -> trace_sched_switch(), which then causes a BPF
   trace program to run, which does that bpf_probe_read_compat(), which
   causes that page fault under pagefault_disable().

It's quite the nasty backtrace, and there's a lot going on.

The problem is literally the vsyscall emulation, which sets

        current->thread.sig_on_uaccess_err = 1;

and that causes the fixup_exception() code to send the signal *despite* the
exception being caught.

And I think that is in fact completely bogus.  It's completely bogus
exactly because it sends that signal even when it *shouldn't* be sent -
like for the BPF user mode trace gathering.

In other words, I think the whole "sig_on_uaccess_err" thing is entirely
broken, because it makes any nested page-faults do all the wrong things.

Now, arguably, I don't think anybody should enable vsyscall emulation any
more, but this test case clearly does.

I think we should just make the "send SIGSEGV" be something that the
vsyscall emulation does on its own, not this broken per-thread state for
something that isn't actually per thread.

The x86 page fault code actually tried to deal with the "incorrect nesting"
by having that:

                if (in_interrupt())
                        return;

which ignores the sig_on_uaccess_err case when it happens in interrupts,
but as shown by this example, these nested page faults do not need to be
about interrupts at all.

IOW, I think the only right thing is to remove that horrendously broken
code.

The attached patch looks like the ObviouslyCorrect(tm) thing to do.

NOTE! This broken code goes back to this commit in 2011:

  4fc3490114 ("x86-64: Set siginfo and context on vsyscall emulation faults")

... and back then the reason was to get all the siginfo details right.
Honestly, I do not for a moment believe that it's worth getting the siginfo
details right here, but part of the commit says:

    This fixes issues with UML when vsyscall=emulate.

... and so my patch to remove this garbage will probably break UML in this
situation.

I do not believe that anybody should be running with vsyscall=emulate in
2024 in the first place, much less if you are doing things like UML. But
let's see if somebody screams.

Reported-and-tested-by: syzbot+83e7f982ca045ab4405c@syzkaller.appspotmail.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Tested-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Andy Lutomirski <luto@kernel.org>
Link: https://lore.kernel.org/r/CAHk-=wh9D6f7HUkDgZHKmDCHUQmp+Co89GP+b8+z+G56BKeyNg@mail.gmail.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
[gpiccoli: Backport the patch due to differences in the trees. The main changes
 between 5.4.y and 5.15.y are due to renaming the fixup function, by
 commit 6456a2a69e ("x86/fault: Rename no_context() to kernelmode_fixup_or_oops()"),
 and on processor.h thread_struct due to commit cf122cfba5 ("kill uaccess_try()").
 Following 2 commits cause divergence in the diffs too (in the removed lines):
 cd072dab45 ("x86/fault: Add a helper function to sanitize error code")
 d4ffd5df9d ("x86/fault: Fix wrong signal when vsyscall fails with pkey").]
Signed-off-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-06-16 13:28:49 +02:00
..
boot x86/decompressor: Don't rely on upper 32 bits of GPRs being preserved 2023-09-23 10:59:41 +02:00
configs vgacon: remove software scrollback support 2020-09-17 13:47:54 +02:00
crypto crypto: x86/ghash - fix unaligned access in ghash_setkey() 2023-03-11 16:43:38 +01:00
entry x86/mm: Remove broken vsyscall emulation code from the page fault code 2024-06-16 13:28:49 +02:00
events perf/x86/amd: fix potential integer overflow on shift of a int 2023-02-06 07:52:47 +01:00
hyperv x86/hyperv: Fix NULL deref in set_hv_tscchange_cb() if Hyper-V setup fails 2021-11-26 10:47:21 +01:00
ia32 binfmt: Move install_exec_creds after setup_new_exec to match binfmt_elf 2023-01-18 11:41:46 +01:00
include x86/mm: Remove broken vsyscall emulation code from the page fault code 2024-06-16 13:28:49 +02:00
kernel genirq/cpuhotplug, x86/vector: Prevent vector leak during CPU offline 2024-06-16 13:28:48 +02:00
kvm KVM: x86: Ignore MSR_AMD64_TW_CFG access 2023-11-28 16:50:18 +00:00
lib x86/insn: Fix PUSH instruction in x86 instruction decoder opcode map 2024-06-16 13:28:39 +02:00
math-emu x86: math-emu: Fix up 'cmp' insn for clang ias 2020-07-29 10:18:40 +02:00
mm x86/mm: Remove broken vsyscall emulation code from the page fault code 2024-06-16 13:28:49 +02:00
net bpf: Introduce BPF nospec instruction for mitigating Spectre v4 2021-09-15 09:47:38 +02:00
oprofile
pci x86/PCI: Add quirk for AMD XHCI controller that loses MSI-X state in D3hot 2023-04-20 12:07:32 +02:00
platform x86/olpc: fix 'logical not is only applied to the left hand side' 2022-08-25 11:18:10 +02:00
power x86/pm: Add enumeration check before spec MSRs save/restore setup 2022-12-08 11:23:05 +01:00
purgatory x86/purgatory: Switch to the position-independent small code model 2024-06-16 13:28:35 +02:00
ras
realmode x86/asm: Make more symbols local 2023-09-23 10:59:40 +02:00
tools x86/boot: Ignore relocations in .notes sections in walk_relocs() too 2024-06-16 13:28:33 +02:00
um x86: um: vdso: Add '%rcx' and '%r11' to the syscall clobber list 2023-03-11 16:44:10 +01:00
video
xen x86/xen: Add some null pointer checking to smp.c 2024-03-26 18:22:12 -04:00
.gitignore
Kbuild
Kconfig x86/speculation: Add Kconfig option for GDS 2023-08-08 19:56:35 +02:00
Kconfig.cpu x86/Kconfig: Transmeta Crusoe is CPU family 5, not 6 2024-02-23 08:25:12 +01:00
Kconfig.debug x86/kconfig: Select ARCH_WANT_FRAME_POINTERS again when UNWINDER_FRAME_POINTER=y 2024-06-16 13:28:45 +02:00
Makefile x86/build: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS) 2021-05-11 14:04:06 +02:00
Makefile.um um: allow not setting extra rpaths in the linux binary 2024-03-15 10:48:16 -04:00
Makefile_32.cpu