From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.130 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796086691; b=X7Ig8tD1J6bnhEuuXymnN3HZDVeItBjEXtCxe8dF8sxoGEQg/yIFqd78kTpJqA69NWKM J7KCJVS5hkoMtZsQYezXsLn4Lf/yr5/gAwR/J0ypxx37AWK3sOxZLGSBH/ch13JqhCdeZ FUAZfP9gkVCMzZPKPQZ8jZGJM3ECZIUMb4HYQIDLYHMNPkhjV1maJq9HNSsP+84wXm3dd KsBnTu5ypJwjBpiChPEGUkEXv/KT4hmoOBtv995q5ngVHbYOD7zbTPUq14pDn9H2/KgXL GYkT7pjHt3zmHCt3uNyFIFDTY5E+5HUjisdvJHFrMzMIKjkzwyzG9gAWcLZtm1fHXhg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796086691; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:X-Developer-Signature:X-Developer-Key: Content-Transfer-Encoding; bh=K0goQIcUKHxDib4reieAvXqSa4uwzbYgiqiwtELvAqo=; b=GCBIulOIuz0NaxI8Zev6FXzqYKOMVLwx/439hY8vyfm4U7DXjdP4PjbQhbqM1ah1t341 V2eMlG1S51COxZICJObRa1xSDos9bfA7gbLFndh0Dm0xmhMmE9V3Q5NrO+IeeOguGmsLb IgtHV3Qs+81Unv4bsovO7jfC+kCLq16ezmWqOeu3d5WRKZO9LsoIVr+BE/+nzTQL8zwhW ngB7lPVSgCkx/+DgIGU0WKOm3PjTx2wyr8X9E2c06KXydi+r/z9lCBooS97YRAFDecu1a gErQpQ8A4M+GEQ0hcljPzcLVvOlRtUvccWx0ypo+E/9oKB8m/Y9LEbe0vXCJvG8b8AA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=RAP2DVlD; arc=none smtp.remote-ip=74.125.231.130 Received: from mail-oo2-f2.google.com (mail-oo2-f2.google.com [74.125.231.130]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0412C606A33B for ; Tue, 1 Dec 2026 00:58:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0412C606A33B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=RAP2DVlD Received: by mail-oo2-f2.google.com with SMTP id 46e09a7af769-862a00144f1so2490025a34.0 for ; Mon, 30 Nov 2026 16:58:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796086690; x=1796691490; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=K0goQIcUKHxDib4reieAvXqSa4uwzbYgiqiwtELvAqo=; b=RAP2DVlDRhZk8gj09+xKhxTa+4Mh0xz+QNZmzwubxWeflCIAkWBwmxvG5cn4GSsBip IyS5VgFM7vk+RmQOW9ElESQLRKFpza35ep6S5dF4riPUBWI4rFxQeF6ukUJfXEfBJmvR KlaInFyzZRoAwC1aJPA/ItNx5jCPcYNyCVu0oqKIGR00hLcYxQs23TsNBLuvIIASZMkK EuydK+IH8F2aG4vfonbk4arycKkBgJHPpGYPOrZQuHxQmUL54i+yzqFi96NR5ThiEudC +YTSw5zrZk6U+Yljy9mIICTkRIKHXYvmDzLJHtbfJSjDCQdgKUYWOXKaYWR2SEgP4rY7 TQww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796086690; x=1796691490; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=K0goQIcUKHxDib4reieAvXqSa4uwzbYgiqiwtELvAqo=; b=RLEmybQwed59xpMKq6IcZbb109/QMunisHljtrzYzpPVollx8ApbCeRs9rC1w0957B +ldjdbiu6BhJYaRpo9MXqUdH3pLn/HuE1dDhjyuGPfCVPhmj2H8vjvq3OS9nwDTS/RhY GCSWDPv8bWpurOlOb4HpvEiZ0MGimfDmWP8Yu97Oo1MFIGCbUCOe6ZxxmqZ2iUMWycZA SebaBQW+8B+LyqL6q3oMic2OCDON9jx/96+nhI9Ya0Qgmw5m6kIXphUXO4v89o4SdnDx XcuwEJcyx8SlOnkvOhi5FZC1BI7BvhkwF6VMv+xBs/IhfNWmN95z2Ldsm4CSc8sSpA91 uFJQ== X-Forwarded-Encrypted: i=1; AL+hErHpfVfW5Q4nD6EiKX+hsawA9Fb5OGgHc74vpKKmzNA+XLoXWHsVMaMc7gDHic/2bhy2cFvM97dNOsPKU9k=@vger.kernel.org X-Gm-Message-State: APuU4Z9Sk3mQCpb0kG5C4FhrWlwqsiR/txZOLSiXE6ucj+k6Nf4MwEyy Zf912w+kaCdwOoZdeKlapdy9ExoFShWEB8Cu7CC5MmYBK4sCiokA14DW X-Gm-Gg: AZFdTZfXc4Q18sQ1GZUonSwTICoOO4WJnTyRJLeC53iMKIO0H8q9eRsUZQeu3WhsE/o folcWmrjbj/OOPwBSeEX1etwZGkowACwMH7xOnLiWtkv9/PQ9KOi8xQH4JCYlH9AhuFcH7TK5SC eKa00sMw9gvFFTgXZj/ukqKG06NjPbCbfkCeGM02OxWiOM3bRa30cUkOaSBqYH7mD7+osNS88ep J1XsevUffvFeBO3KetPg5XaaTcD9vqOzWCpYccE3OkWtZqWQXxyruTtr3epPEYBsnRupAS8KDg1 4v5Iv+PGFxH90IB3FiPLS42C8vT9B3axN/S6YnNsdPq2vvggSSQ1oGfAVzZVUmP190pmkc0xWxX YuLcW7bJX2LoDWgkR1+H06Gs1FwYgSux2icaTgqLzbBjHWsKTOajlsVT0a4BwV65/Fqv3c2ptCQ eT+PYzgxkgwSW5UVmTVPAEpAtdLoyE5on9PACRmdSKGbne2RNJcEkr7toaHVFl2UXY3yd6zYGk3 X8esGXXixEroAknpk3h+3+L0duBOMvMMmYqLJ3jTtZoIfQWpiBsF2vViegJLrYEniEe6Tx7JJX7 cyqp8iKozb8Xi+iY5YXaXi0sMopkGia0zZEOhtOc9jI7wlFmxk/HJgs1y2uGtQOzKqdbECBi8A1 XcjwpEU9d/dEFqJX+PWLmybh5E+bQW0vWUBQD X-Received: by 2002:a05:6830:680c:b0:823:e626:2764 with SMTP id 46e09a7af769-863156221c3mr39914907a34.5.1796086689911; Mon, 30 Nov 2026 16:58:09 -0800 (PST) Received: from localhost ([2a03:2880:10ff:72::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-8639825528bsm8779520a34.1.2026.11.30.16.58.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 16:58:08 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com, Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Puranjay Mohan , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v3 1/3] x86/mm: Resolve BPF exception fixups for user address faults under SMAP Date: Tue, 1 Dec 2026 01:58:00 +0100 Message-ID: <477e2c8982a2c1f2d8cfc14dba10255cc6e49363.1796086624.git.memxor@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4501; h=from:subject:message-id; bh=W+HsNHutfJbLCJkc8T7oisCByPi4hPQnKNXfP28aR20=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZtPOoFhutj9MH+mX1djP/KkrH57IzR/ssDexRcndmaXJWlK FVztKGVhEONikBVTZCn5v4/J+ETl70DbZdwwc1iZwIZwcQrARG46MjLsn/divsuNGx73DA57Ns9nPH JbdlHeyynbuf4861pqXvx1DiPDBc+Oc5a+9im/RTx5FoZsE3hv2jd/uqcF1zmjp0xdZuFsAA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit 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. SMAP refuses user pages, not low addresses. While a temporary mm is loaded (EFI runtime calls, text poking), a low address can have a supervisor mapping. A PROBE_MEM load of it from an interrupt now reads the mapping instead of zero, which is acceptable. 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