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=1795622424; b=LheQ7bzx4Om1ViOwummWZk4gU4vWVrfn1jyh6QhppK43Lh6Gbs+68FcYVM3ifmGXYGlF sT7McY8b4poQTgUxS7QAN3QJ2BX02h9PLd1tRi8tImgoKfdRy3W5cCkkUhzw7qS7wzALb 846glAtuz83wEdWDxL0O0SYC7bu8CE4Jphq4WdEUyVMLEmXQkBNZCLIqbfoJho7NUByP+ z6r4kNl04pvvbyS0Hax1dYUzpoTsgaAZMCXwLaZNov5YDHoyiIigCaxmdu53knkMdRd7n E9M0/ziD8lRiTOlVG9CVpTAFWtwaaUNIHDp7XULjV60jtNyzuo1NsvgV2VaRGAZU74Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795622424; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=66/Si4z5COxVzPSNcdhRjvSxIQ/7/GZb85BhszEOYLw=; b=icNs4jRf2vg1XARsL5TF06ZrSAsmy5qz9TsS+PSdz1y1DJTgYiRIjOleQUWbkR3fuEFN BwpIULs+qzdg/JvGTCAdArWk4xCIiDN4sZawA6+gDtzOoun/YX1p7zPhRJEHZM2mRwURN c3N+ztcj+BmPkvTGH0nGzNVKLAhEdHfOM7aQ577haCpQTbAUD5ToeBOSJgHmmm5SstKBP NxoHYTPDlCE4oR9wzlrUfCaOdB7noLoWx71sB6RkJio+5+b1ajnitLzF5NG15aiV2w0k5 Zh06IW8S32SzZA3/j+VgvtLzEM/G3mJK94c/F2Ecs1ZAZd3wQ53+BJM3rDcnxP3hCaA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=HoqzG3bj; 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 A4902606A31E; Wed, 25 Nov 2026 16:00:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=HoqzG3bj Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7D771F000C4; Wed, 25 Nov 2026 16:00:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795622424; bh=66/Si4z5COxVzPSNcdhRjvSxIQ/7/GZb85BhszEOYLw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=HoqzG3bjG8v50iWKLLTqtXpiDw9gT/98I/axaoAC7iNc99gvM6aCSd2BU36mmESRN X0lXd3Kemm+w1doMppiTO1pJ8TU0F3+oLHPG0mbPDmthQ/NzTlz9D/uGPew+0yoLrp 5+UI7ymCKfDIGtFBRZv8VIESKERMtQMN9srZBtvE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Kumar Kartikeya Dwivedi , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.18 532/990] bpf: Check fastcall stack contract once for all stack accesses Date: Wed, 25 Nov 2026 15:31:39 +0100 Message-ID: <20261125143834.554612479@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125143822.072087159@linuxfoundation.org> References: <20261125143822.072087159@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kumar Kartikeya Dwivedi [ Upstream commit ec3a69e7c021cd826be8886ba6af4ee3147788fb ] When the verifier inlines a bpf_fastcall helper or kfunc, it removes the spill/fill pairs that clang emitted around the call and lowers the subprogram's stack depth so that their slots are no longer part of the frame. This is only safe if nothing else accesses those slots or any slot below them. check_fastcall_stack_contract() enforces this: an access to that region from outside a fastcall pattern disables the rewrite for the subprogram. It is called from the four stack load and store paths, and two kinds of access escape it. Stack buffers passed to helpers and kfuncs are validated by check_stack_range_initialized(), which does not call it. If such a buffer is the only other access to the region, the rewrite is still applied, the JIT reserves a frame that does not contain the buffer, and the helper reads or writes live kernel stack below the program's frame. For example, without CAP_PERFMON: *(u64 *)(r10 - 8) = r1; call bpf_get_smp_processor_id; r1 = *(u64 *)(r10 - 8); r2 = 0; r3 = r10; r3 += -64; r4 = 8; call bpf_skb_load_bytes; is accepted with a stack depth of 0 instead of 64. Some buffers got the check indirectly. For constant-sized outputs in raw mode, mark_raw_stack() marks the buffer initialized through ordinary byte stores, which call the check. Commit 5da4a9f26fca ("bpf: Preserve stack initialization for generic output buffers") limited raw mode to programs that may read uninitialized stack, so outputs of programs without CAP_PERFMON lost the check, as in the example above. Buffers that never used raw mode have lacked the check since fastcall support was added: helper inputs such as a map key whose only store is a fastcall spill, variable-sized outputs, and buffers at a variable stack offset. check_stack_read_fixed_off() applies the check to the current frame, even when the pointer targets a caller's stack. A callee can therefore read a caller's fastcall spill slot without disabling the caller's rewrite: caller: r1 = 1; *(u64 *)(r10 - 8) = r1; call bpf_get_smp_processor_id; r1 = *(u64 *)(r10 - 8); r1 = r10; r1 += -8; call callee; callee: r0 = *(u64 *)(r1 + 0); The caller's spill and fill are removed and its stack depth drops to 0, but the verifier still tracks the slot as holding the spilled constant. The callee's load then reads kernel stack outside the caller's frame, while the verifier assumes r0 is 1. Every stack access first goes through check_stack_access_within_bounds(): loads and stores from check_mem_access(), helper and kfunc buffers from check_stack_range_initialized(), and the dynptr, iterator and irq flag slots that kfuncs initialize, whose handlers use check_mem_access(). It resolves the frame that owns the slots and the lowest offset the access can touch, and it is the only place where a frame's stack depth grows. Check the contract there, once for every access, and drop the calls from the stack read and write paths. Zero-sized buffers touch no stack and leave the rewrite enabled. Fixes: 5b5f51bff1b6 ("bpf: no_caller_saved_registers attribute for helper calls") Fixes: 5da4a9f26fca ("bpf: Preserve stack initialization for generic output buffers") Signed-off-by: Kumar Kartikeya Dwivedi Signed-off-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/85f9995a43edb70c76615280e103dc5325742e88330f36c97962ee35f17e3e32@mail.kernel.org Link: https://patch.msgid.link/20260924075352.2343553-2-memxor@gmail.com [ sashal: Removed the four old fastcall stack-contract calls and added the single check in check_stack_access_within_bounds(); the code matches upstream, with only hunk context adjusted for surrounding code that differs. ] Signed-off-by: Sasha Levin --- kernel/bpf/verifier.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 139671b393a05..e146b860f5692 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5230,7 +5230,6 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env, bpf_mark_stack_write(env, state->frameno, BIT(spi)); } - check_fastcall_stack_contract(env, state, insn_idx, off); mark_stack_slot_scratched(env, spi); if (reg && !(off % BPF_REG_SIZE) && reg->type == SCALAR_VALUE && env->bpf_capable) { bool reg_value_fits; @@ -5349,7 +5348,6 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env, return err; } - check_fastcall_stack_contract(env, state, insn_idx, min_off); /* Variable offset writes destroy any spilled pointers in range. */ for (i = min_off; i < max_off; i++) { u8 new_type, *stype; @@ -5493,7 +5491,6 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env, reg = ®_state->stack[spi].spilled_ptr; mark_stack_slot_scratched(env, spi); - check_fastcall_stack_contract(env, state, env->insn_idx, off); err = bpf_mark_stack_read(env, reg_state->frameno, env->insn_idx, BIT(spi)); if (err) return err; @@ -5652,7 +5649,6 @@ static int check_stack_read_var_off(struct bpf_verifier_env *env, min_off = reg->smin_value + off; max_off = reg->smax_value + off; mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); - check_fastcall_stack_contract(env, ptr_state, env->insn_idx, min_off); return 0; } @@ -7688,6 +7684,15 @@ static int check_stack_access_within_bounds( return err; } + /* + * Every stack access passes through here, including buffers passed to + * helpers and kfuncs and accesses into a caller's frame, so check the + * fastcall contract of the frame that owns the slots once for all of + * them. Zero-sized accesses touch no stack and keep the rewrite enabled. + */ + if (access_size) + check_fastcall_stack_contract(env, state, env->insn_idx, min_off); + /* Note that there is no stack access with offset zero, so the needed stack * size is -min_off, not -min_off+1. */ -- 2.53.0