Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796152593; b=meMdg3z+8EFVcCgsdbFjSNzl+Ncxmtkv3plDoiFnK3NoWeJTFc/DJXt57OIo4aWK1XY9 9LCkcHZh+5J2GSo/W2HxSZDFMLrmU+bZao45qGPtfhJ1gB7PqwuLVOYmjJ36Ge+FOuZa5 Kv2NO/pq78cDguoS6hK2IPt81zNpQFg2DLRsVuk6jPBY4Owkt3km83CFhvLAAwJNLd36U 8aRDrUKgTK9S4hlXXBiK9qgVSjoI8LkMzB11uJlsdEjXtrjwpIKLJlMqOHcK+gGKI6LBR KOyDMrTh9+8l/xkVw0QxDeoDhsdfpdFNCcvwx4VfeAVEFeYgqsxEBcSeAX1Mgra22mw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796152593; h=Received:DKIM-Signature:Received:Received:X-ME-Sender: X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID:Received:X-Mailer: MIME-Version:X-ThreadId:Date:From:To:Cc:Message-Id:In-Reply-To: References:Subject:Content-Type:Content-Transfer-Encoding; bh=D+ofMp9wD5q/Fs8ahL4l7KJ8W2wy9MdiDJlLIxpm7/k=; b=4q3W+1+j99NN+bbHBIzhZoRguPmiKkZM8SZ4FUoRq2+UvVU3XJ7UhEDegtkuC08+pWvJ Uj0VGQ/h880NVw1UvVgz9AzhpeEqkbDvM0Pst64vmHLgp4tZnDTX2vTBjYSYYOL185KG3 3VNi3bGLIyPjo09HIi7WQi2rDZedDBKzX2d7DQXLs/0AZcE9KFi0U7VJdmGcl/yeW7rcm fL5H5kx/GNfOtWsYAR7ME4sNPfAPMYlriJo6OJnWJ9KQB3ny42qnNglszsDUOGCFTVwJm nRULbdZZBWn8IWGEsW8gvWIqppnmBzwnIDl3H94gtMfFAomPnBBVqAyy+Pa2VEMbB1A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NtNqCg3o; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EB13606A320 for ; Tue, 1 Dec 2026 19:16:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NtNqCg3o Received: by smtp.kernel.org (Postfix) with ESMTPSA id A59ED1F00899; Tue, 1 Dec 2026 19:16:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796152593; bh=D+ofMp9wD5q/Fs8ahL4l7KJ8W2wy9MdiDJlLIxpm7/k=; h=Date:From:To:Cc:In-Reply-To:References:Subject; b=NtNqCg3oK2U4Up0/G4qoUuBAfKoJgJyZ/FEI36JwYa8LOOer1f5i4+GsbwBAWOEkm 3vKEyEzxU0WHVsK4aehCJKkTDPrAjH227AV3CKwciM/ZBAdvDdA14d/vwNQ70bW2Xs k74SY4F41kpg1rIWaKzrH5O4y23SKGM8ksMv7BopXjPv/OMdJKKbh7paGEUtTfsyY4 1y5e3jNJjJx9tSKERJRBG71qH/tXEN83+2hIHWv75wkj5fEYwNINuBnHk+tDCnPeB9 OCQ3FUmxK+FvI9Evh8ST2GbTxjih1IPgpBzfO/B3eANr/0yNXSnn+dC6xID4FJQ3PY I6IlgjCWtMxKw== Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id CECADF40067; Tue, 1 Dec 2026 14:16:31 -0500 (EST) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-01.internal (MEProxy); Tue, 01 Dec 2026 14:16:31 -0500 X-ME-Sender: X-ME-Proxy-Cause: dmFkZTFxXhXw9cT3oVRFmgXd2y8Svxkxrrfvkwh/bp7On+tB7kb4gEVro5yLreNrmVt46b gPZtaAcEvkoYu/BD4ic+6mFPjXepr65v3+Qqn2+HVKUvDeBADA+h0jS1YbfaMMj0mA+owY XdQMYkI3q/LQEiV6w5wV8yKD9wR+bDwAyrSGyEJXvY8baJZDnA1/INUpOZPumo0PFDgIhw vyIv39NzLXKmIfiaoG0p2Nnu9SNeJgnubSAWYDe895nyj5cx1maIrxx4ifHLOBHtxPZXri up1YxxOeUsUwy+zv7CJxektdmR/meuSloV3tc7bPRgdGrVXzmSY8G+4LOIladz3Fff5cOw ipAtC6pYrVaD1BhoIE2nHIjOtBhgKW6tZ027tduLvns2NKsapHyI/l6GxlbCCQ2t0jBfMB I7y2PiSTcmI4GrkIKpdpK0nb2AOikScwLzYz1W0B+8BFRmAIZ/9r7SHIXkYxDxMQ46LHeb Fkx8lnv75wRlGGLN/irhFp3frYqPDbApsR+O+wQ95dT7gIJz1w0poIpsH0cES+dZpjvvR+ bSykah18Rt1VnkRudGZsH6ZQNM4H2TOHj1dPnJExUsU/U09cVRgD2yatC5zBn7m57T68YH QkhVPbWvSFg+1yknzs1XSiyOy/UIrnO+ymW7lbfB/6fOZPjMKxGBi8+p+Urg X-ME-Proxy: Feedback-ID: ieff94742:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id A17BF700065; Tue, 1 Dec 2026 14:16:31 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-ThreadId: AisSLPdYCdlo Date: Tue, 01 Dec 2026 11:16:11 -0800 From: "Andy Lutomirski" To: "Kumar Kartikeya Dwivedi" Cc: "Alexei Starovoitov" , bpf@vger.kernel.org, "Puranjay Mohan" , "Andrii Nakryiko" , "Daniel Borkmann" , "Eduard Zingerman" , "Emil Tsalapatis" , "Ihor Solodrai" , "Dave Hansen" , "Peter Zijlstra (Intel)" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , kkd@meta.com, kernel-team@meta.com, "the arch/x86 maintainers" , "Linux Kernel Mailing List" Message-Id: In-Reply-To: References: <20261009024925.3169077-1-memxor@gmail.com> <20261009024925.3169077-2-memxor@gmail.com> <5f46d1c1-fdce-4f3b-9d56-675a822b2d65@app.fastmail.com> <03275761-8de2-41be-92fd-8e3012a87eeb@app.fastmail.com> <1ba7d551-32e1-4020-b898-502cd9ab8fb7@app.fastmail.com> Subject: Re: [PATCH bpf-next v1 1/3] x86/mm: Resolve BPF exception fixups for user address faults under SMAP Content-Type: text/plain Content-Transfer-Encoding: 7bit On Mon, Nov 30, 2026, at 7:24 PM, Kumar Kartikeya Dwivedi wrote: > On Tue, 1 Dec 2026 at 03:38, Andy Lutomirski wrote: >> >> >> >> On Mon, Nov 30, 2026, at 6:06 PM, Kumar Kartikeya Dwivedi wrote: >> > On Tue, 1 Dec 2026 at 02:49, Andy Lutomirski wrote: >> >> >> >> >> >> >> >> If you believe that the BPF verifier can statically verify that a BPF program doing a PROBE_MEM access is referencing a valid (or maybe only mildly stale) kernel address, then I encourage you and Kumar to drop all of the x86 patches you keep sending, remove all the code that BPF emits to avoid tripping over SMAP, and be completely confident that the kernel will never OOPS or otherwise malfunction as a result. >> >> >> >> If you don't believe that, for example if you think a program might do x = a->b->c where the *a cannot be proven to still be alive and intact at the time that a->b is read, then I don't think that __iomem will save you, and maybe you should not believe anything in the previous paragraph, and maybe you should rethink this. >> > >> > Hi Andy, >> > >> > I think that part is clear to me, __iomem based rejections are useful >> > but not sufficient to prevent the problem you mentioned. I should have >> > been clearer in my earlier reply. >> > >> > That said, some of the options you outlined would be too expensive >> > (like walking a table at each access) and would just make tracing >> > programs prohibitively slow due to potential overhead. If you look at >> > the cover letter, some programs consist largely of probe reads, and >> > are often used in performance critical paths for observability and >> > statistics collection. >> > >> > Taking a step back, we agree that ioremap() pages are also a problem >> > for every user of copy_from_kernel_nofault(). My impression is that >> > exploring a region based approach, so we can isolate ioremap() regions >> > in a range and using a range check to cheaply work around those >> > addresses would be the best fit. I haven't spent time exploring this >> > option, but I'm happy to follow up separately. >> > >> >> As I understand the situation, there's a class of eBPF programs that contain memory accesses that cannot be statically verified to be correct, and eBPF is does not actually trust the programs enough to run them despite the lack of verification. And presumably the SMAP check (or the range check that avoids it) actually gets hit in practice, so eBPF can't get away with just ignoring the issue, because it will OOPS on occasion. >> >> I'll assume for the sake of this discussion that it is actually reasonable for eBPF to have a primitive that means, approximately, "read this address and produce either a garbage result or zero in case the pointer is bogus". (And that it's okay that a malicious attacker might control the result, etc.) >> >> Given this, eBPF needs an implementation of the primitive. Apparently the pointers really are wild enough that they hit user-half addresses with some frequency and they've even hit the vsyscall page. So I'll assume that they are genuinely completely wild. >> >> And you want good performance. >> >> And there is no plan that I'm aware of to improve the verifier and the tools available to eBPF programs to get actual static verification. >> >> So I think this is a catch-22. x86 could make an arch change (the ignore-bpf-smap-violations thing) that will make most, but not all, wild pointer reads "safe" in the sense that they will return zero instead of oopsing. But it will do nothing to help the smaller fraction that hit side-effect-ful memory out of bounds. And it will encourage everyone to forget about the issue even while it's known buggy. >> > > I mean, things are already broken today re: MMIO for anything that > uses copy_from_kernel_nofault(). I think a few mails ago Alexei > already demonstrated how that takes arbitrary addresses through > tracing features regardless of BPF. > >> >> > At the same time, getting rid of the user space address check which is >> > feasible when SMAP is enabled, and closing the case where __iomem >> > tagged pointers are rejected from the verifier, both seem useful >> > additions to me. They do not fix everything we discussed but are >> > orthogonal improvements to the status quo, reducing overhead and >> > preventing potential mistakes. >> >> I think the __iomem change makes sense. Those programs are presumably just wrong. >> >> But I'm not seeing why the SMAP change is actually a good idea, since it just papers over the issue by suppressing most of the OOPSes. > > I think we are slightly conflating things again. > > We already shift the cost to kernel address faults using exception > fixups for copy_from_kernel_nofault() and BPF. Doing it for the SMAP > case would be no different in that regard (in terms of the approach > taken). > Is this cost-shifting or is it that you simply *can't* otherwise have a race-free way to read from an address that looks like a real kernel address but might get unmapped from under you? > So we already suppress faults where possible to reduce instrumentation > overhead, even outside BPF. The whole change in this set is just to > cover an extra set so that minus MMIO, we can emit plain loads in the > program. > > Regardless of what we do with SMAP instrumentation (whether to use > exception fixups or keep the inline checks), the ioremap() case > remains open, so I am not sure I buy that all of this is tied > together, and one change papers over the other. The ioremap() hole is > unrelated to BPF, and has to be dealt with separately. I might believe this when I see a design for fixing the MMIO issue that doesn't inherently fix the SMAP issue as well. > > Also, PKS will not work (since it's unavailable everywhere), CR3 > switches are expensive, and walking a table is also a no go for all > this (it will be no go for copy_from_kernel_nofault() as well). I > think the only reasonable solution is placing ioremap() mappings in > some known range and emitting a range check to exclude those > addresses. Why is walking a table a no-go? What is BPF doing that makes statically unsafe pointer loads so performance-critical anyway? > > If we realized the cheapest version of ioremap() check, it would pile > onto the instrumentation we already have. So applying the SMAP change > would still make things cheaper for all users, even if (some) range > checks have to be kept. Again, this needs explanation. If the whole kernel were designed around this use of BPF, then there would be a subset of the kernel virtual address space, controlled entirely by predetermined set of bits, that was guaranteed to only contain normal kernel memory. And you would mask or check those bits, and you wouldn't trip over SMAP or MMIO. Obviously the kernel is not just a BPF execution engine. > >> > >> > Even if we have to keep instrumentation for the ioremap() range (since >> > we don't have potential ways to cheaply activate hardware protection >> > on such accesses), that will still be much cheaper (perhaps a single >> > branch) than emitting a longer sequence for each load instruction, as >> > we do today. >> > >> >> Can you explain what the sequence is? I'm not really seeing why SMAP is expensive but MMIO checks would be cheap. > > It's described in the commit log. > > https://lore.kernel.org/bpf/477e2c8982a2c1f2d8cfc14dba10255cc6e49363.1796086624.git.memxor@gmail.com I read that earlier, and I read that again, and I consider it to be truly bizarre. I've written programs in quite a few real programming languages, and, in *all* of them, you do the equivalent of this instead: struct *socket s = sk->sk_socket; struct file *f = (socket ? s->file : NULL); You may spell it differently. You may rely on some language-specific implicit behavior. You should almost certainly use your language's concept of an atomic (relaxed? acquire?) load -- most modern languages have rather strong opinions about data races and you are violating the rules. Rust will not even allow the racy case to compile! But what you do not do, in any language where exceptions are actually heavyweight exceptions, is this: struct file *f; try { f = sk->sk_socket->file; } catch ( page fault ) { f = NULL; } Heck, in plain C you can't even write this code unless you're targetting Windows. Why is BPF so special that it should redefine the rules of C to effectively make that transformation, and why should BPF's users be able to rely on that transformation *and except to get extra efficient code out of it*? (In most cases the developer *wants* the crash when the mess up so they can learn that their code page faulted and fix it. I'd be slightly more sympathetic if BPF's behavior was to abort and report an error on a page fault!) Also, in any of these cases, unless you have some other protection, you are vulnerable to another thread/CPU freeing and possibly even reusing the struct socket or the file after you have found a pointer to it. Maybe there is some protection in this context; maybe there is not.