BPF tracing programs read through pointers that cannot be checked when the program is loaded. For example: SEC("fentry/tcp_retransmit_skb") int BPF_PROG(retrans, struct sock *sk) { struct file *f = sk->sk_socket->file; ... sk is valid, but sk->sk_socket is NULL for an orphaned socket. The verifier marks such a load PROBE_MEM and the JIT gives it an exception table entry of type EX_TYPE_BPF. The load is a single mov, movzx, movsx or movsxd into a register. On a fault, ex_handler_bpf() zeroes the register and skips the instruction. The load is not retried. This does not work for user addresses. With SMAP enabled, do_user_addr_fault() treats a kernel-mode fault on a user address with EFLAGS.AC clear as a kernel bug and oopses without looking at the exception table. So the x86 JIT puts a range check in front of every PROBE_MEM load to keep user addresses, the guard page above them and the vsyscall page away from it. For the second load above, with five-level paging, it emits: leaq 10485776(%rdi), %r11 # 16 - VSYSCALL_ADDR shrq $57, %r11 # zero for user and vsyscall addresses jne load xorl %edi, %edi jmp done load: movq 16(%rdi), %rdi # offsetof(struct socket, file) done: That is 17 or 18 bytes of check for a load of three to eight bytes, and it repeats in software what the CPU already refuses under SMAP. The pointer is only known at run time, so the check cannot be done at load time. Fix up the fault through the exception table when it came from kernel mode, is a read, and the faulting instruction has an EX_TYPE_BPF entry. error_context() uses ex_get_fixup_type() the same way for machine checks. SMAP stays on and the access is not retried, so no user memory is read: the register reads zero, as it does with the range check. The new check only runs for faults that oops today, and every other fault there still oopses. With LASS the access raises #GP instead, which already goes through fixup_exception(). The JIT also uses EX_TYPE_BPF for arena accesses and unwind entries. Arena addresses are kernel addresses, and no instruction starts at the address of an unwind entry, so neither can match a fault on a user address. The addresses a PROBE_MEM load can read stay the same. What changes is who refuses a user address: a compare in software, or the CPU with a fault and a fixup. user, guard, kernel address vsyscall page non-canonical unmapped mapped copy_from_kernel_nofault() compare compare CPU load PROBE_MEM, range check compare CPU CPU load PROBE_MEM, SMAP alone CPU CPU CPU load None of the three filters MMIO. ioremap() space is read in every row, and a BAR mapped into user space is a user address, refused in every row. This lets the x86 JIT drop the range check when SMAP is enabled. Kernels without SMAP keep the check and never take this path. Suggested-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- arch/x86/mm/fault.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index aa88370ce739..9fe8705eff04 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1263,7 +1263,16 @@ void do_user_addr_fault(struct pt_regs *regs, !(error_code & X86_PF_USER) && !(regs->flags & X86_EFLAGS_AC))) { /* - * No extable entry here. This was a kernel access to an + * The CPU refused the access. If the BPF JIT marked this load + * as one that may fault, skip it and zero the destination. + */ + if (!user_mode(regs) && !(error_code & X86_PF_WRITE) && + ex_get_fixup_type(regs->ip) == EX_TYPE_BPF && + fixup_exception(regs, X86_TRAP_PF, error_code, address)) + return; + + /* + * No other fixups here. This was a kernel access to an * invalid pointer. get_kernel_nofault() will not get here. */ page_fault_oops(regs, error_code, address); -- 2.53.1-Meta