linux-stable/arch/x86/entry
Linus Torvalds 02b670c1f8 x86/mm: Remove broken vsyscall emulation code from the page fault code
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
2024-05-01 09:41:43 +02:00
..
syscalls lsm/stable-6.8 PR 20240105 2024-01-09 12:57:46 -08:00
vdso x86/vdso: Fix rethunk patching for vdso-image-x32.o too 2024-03-26 10:47:14 +01:00
vsyscall x86/mm: Remove broken vsyscall emulation code from the page fault code 2024-05-01 09:41:43 +02:00
Makefile x86/fred: FRED entry/exit and dispatch code 2024-01-31 22:02:31 +01:00
calling.h Core x86 changes for v6.9: 2024-03-11 19:53:15 -07:00
common.c x86/fred: Fix INT80 emulation for FRED 2024-04-18 10:37:11 +02:00
entry.S Core x86 changes for v6.9: 2024-03-11 19:53:15 -07:00
entry_32.S Core x86 changes for v6.9: 2024-03-11 19:53:15 -07:00
entry_64.S x86/bhi: Add support for clearing branch history at syscall entry 2024-04-08 19:27:05 +02:00
entry_64_compat.S x86/bhi: Add support for clearing branch history at syscall entry 2024-04-08 19:27:05 +02:00
entry_64_fred.S x86/entry: Add fred_entry_from_kvm() for VMX to handle IRQ/NMI 2024-01-31 22:03:16 +01:00
entry_fred.c x86/fred: Fix incorrect error code printout in fred_bad_type() 2024-04-18 10:47:17 +02:00
syscall_32.c x86/syscall: Don't force use of indirect calls for system calls 2024-04-08 19:27:05 +02:00
syscall_64.c x86/syscall: Don't force use of indirect calls for system calls 2024-04-08 19:27:05 +02:00
syscall_x32.c x86/syscall: Don't force use of indirect calls for system calls 2024-04-08 19:27:05 +02:00
thunk_32.S x86/retpoline: Ensure default return thunk isn't used at runtime 2024-02-12 11:42:15 +01:00
thunk_64.S x86/retpoline: Ensure default return thunk isn't used at runtime 2024-02-12 11:42:15 +01:00