Skip to contentSkip to conversation

mptcp, bpf: reject bpf_sk_release() on msk

PATCH In review Jiayuan Chen 5 participants 6 messages Sep 17

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mptcp, bpf: reject bpf_sk_release() on msk

A bpf prog can do this today:

	subflow = bpf_skc_lookup_tcp(...);
	msk = bpf_skc_to_mptcp_sock(subflow);
	bpf_sk_release(msk);

bpf_skc_to_mptcp_sock() returns subflow->conn without taking any reference, so bpf_sk_release() drops a refcount nobody took on the msk, and the subflow reference is leaked:

refcount_t: underflow; use-after-free.

WARNING: lib/refcount.c:28 at refcount_warn_saturate+0xdf/0x120, CPU#2: mptcp_pair/440
Call Trace:
 <IRQ>
 sock_gen_put+0xda/0x100
 bpf_sk_release+0x5e/0xd0
 bpf_prog_780c70b94862636c_rel_msk+0x127/0x132
 __dev_queue_xmit+0x104b/0x3c90
 ip_finish_output2+0x9af/0x1c40
 __ip_finish_output+0x510/0x7e0
 ip_finish_output+0x2f/0x320
 ip_output+0x17a/0x3f0
 ip_local_out+0x12f/0x170
 __ip_queue_xmit+0x81d/0x1d50
 ip_queue_xmit+0x4a/0x80
 __tcp_transmit_skb+0x2f6f/0x5110
 __tcp_send_ack.part.0+0x385/0x740
 tcp_send_ack+0x70/0x90
 __tcp_ack_snd_check+0x1c9/0x8d0
 tcp_rcv_established+0xa1e/0x44f0
 tcp_v4_do_rcv+0x4b8/0xb30
 tcp_v4_rcv+0x27af/0x3e60
 ip_protocol_deliver_rcu+0x95/0x410
 ip_local_deliver_finish+0x357/0x5b0
 ip_local_deliver+0x15c/0x1c0
 ip_rcv+0x284/0x320

bpf_skc_to_mptcp_sock() is listed in is_ptr_cast_function(), so if the subflow is a referenced obj (returned by bpf_skc_lookup_tcp()), the msk becomes a referenced obj too, which lets bpf_sk_release() take it. That list is for helpers casting a sock to another type at the same address, which does not hold here: msk and subflow are two different socks.

Drop it from is_ptr_cast_function(). To keep the msk from outliving the subflow it was derived from, tie the two together with the existing parent_id, so this gets rejected as well:

	subflow = bpf_skc_lookup_tcp(...);
	msk = bpf_skc_to_mptcp_sock(subflow);
	bpf_sk_release(subflow);
	msk->token;			/* rejected now */

The other option was to reject bpf_skc_to_mptcp_sock() on a referenced subflow altogether, but that breaks progs which only read msk fields, so go with parent_id.

Fixes 3bc253c2e652 ("bpf: Add bpf_skc_to_mptcp_sock_proto")
---
 kernel/bpf/verifier.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +19 -1
index 9e79750e2480..5db49a0c346f 100644
@@ -522,11 +522,21 @@ static bool is_ptr_cast_function(enum bpf_func_id func_id)
 		func_id == BPF_FUNC_skc_to_tcp_sock ||
 		func_id == BPF_FUNC_skc_to_tcp6_sock ||
 		func_id == BPF_FUNC_skc_to_udp6_sock ||
-		func_id == BPF_FUNC_skc_to_mptcp_sock ||
 		func_id == BPF_FUNC_skc_to_tcp_timewait_sock ||
 		func_id == BPF_FUNC_skc_to_tcp_request_sock;
 }
 
+/*
+ * bpf_skc_to_mptcp_sock() does not cast its argument. It returns the parent
+ * MPTCP socket of the subflow that was passed in, so the return value must not
+ * inherit the argument's reference, or bpf_sk_release() would put the wrong
+ * socket.
+ */
+static bool is_ptr_derive_function(enum bpf_func_id func_id)
+{
+	return func_id == BPF_FUNC_skc_to_mptcp_sock;
+}
+
 static bool is_sync_callback_calling_kfunc(u32 btf_id);
 static bool is_async_callback_calling_kfunc(u32 btf_id);
 static bool is_callback_calling_kfunc(u32 btf_id);
@@ -11369,6 +11379,14 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		bpf_diag_mod_begin(env, &regs[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE);
 		regs[BPF_REG_0].type &= ~PTR_MAYBE_NULL;
 		regs[BPF_REG_0].id = meta.ref_obj.id;
+	} else if (is_ptr_derive_function(func_id) &&
+		   find_reference_state(env->cur_state, meta.ref_obj.id)) {
+		err = validate_ref_obj(env, &meta.ref_obj);
+		if (err)
+			return err;
+
+		/* Ensures we don't access the object after a release_reference() */
+		regs[BPF_REG_0].parent_id = meta.ref_obj.id;
 	} else if (is_acquire_function(func_id, meta.map.ptr)) {
 		int id = acquire_reference(env, insn_idx, 0);
 
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 selftests/bpf: add verifier tests for bpf_skc_to_mptcp_sock

Cover what the verifier has to get right for the msk returned by bpf_skc_to_mptcp_sock(): it must not be accepted by bpf_sk_release(), it must go away with the subflow it was derived from, and the subflow reference still has to be released.

  # ./test_progs -t verifier_mptcp
  #658/1   verifier_mptcp/bpf_skc_to_mptcp_sock: release the acquired subflow:OK
  #658/2   verifier_mptcp/bpf_skc_to_mptcp_sock: msk is not the acquired subflow:OK
  #658/3   verifier_mptcp/bpf_skc_to_mptcp_sock: msk dies with the subflow it came from:OK
  #658/4   verifier_mptcp/bpf_skc_to_mptcp_sock: subflow is still owned after the cast:OK
  #658     verifier_mptcp:OK
  Summary: 1/4 PASSED, 0 SKIPPED, 0/0 FAILED
---
 .../selftests/bpf/prog_tests/verifier.c       |   2 +
 .../selftests/bpf/progs/verifier_mptcp.c      | 101 ++++++++++++++++++
 2 files changed, 103 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_mptcp.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index f7f94ccebce2..63316e4be6bb 100644
@@ -74,6 +74,7 @@
 #include "verifier_mem_size_reg.skel.h"
 #include "verifier_meta_access.skel.h"
 #include "verifier_movsx.skel.h"
+#include "verifier_mptcp.skel.h"
 #include "verifier_mtu.skel.h"
 #include "verifier_mul.skel.h"
 #include "verifier_netfilter_ctx.skel.h"
@@ -238,6 +239,7 @@ void test_verifier_may_goto_2(void)           { RUN(verifier_may_goto_2); }
 void test_verifier_mem_size_reg(void)         { RUN(verifier_mem_size_reg); }
 void test_verifier_meta_access(void)          { RUN(verifier_meta_access); }
 void test_verifier_movsx(void)                 { RUN(verifier_movsx); }
+void test_verifier_mptcp(void)                { RUN(verifier_mptcp); }
 void test_verifier_mul(void)                  { RUN(verifier_mul); }
 void test_verifier_netfilter_ctx(void)        { RUN(verifier_netfilter_ctx); }
 void test_verifier_netfilter_retcode(void)    { RUN(verifier_netfilter_retcode); }
tools/testing/selftests/bpf/progs/verifier_mptcp.c new file +101 -0

101 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH bpf v1 2/2] selftests/bpf: add verifier tests for bpf_skc_to_mptcp_sock

This isn't a bug, but since nothing from bpf_tracing_net.h is used here, would a plain #include "vmlinux.h" (as progs/verifier_sock.c does) be enough for struct mptcp_sock?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +0 -0
index 8b439e194bcc5..8d5908e34c9b4 100644

[ ... ]

tools/testing/selftests/bpf/progs/verifier_mptcp.c line 5

Quoted 11 lines
tools/testing/selftests/bpf/progs/verifier_mptcp.c new file +5 -0
new file mode 100644
index 0000000000000..1f679265e0437
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "bpf_tracing_net.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"

This isn't a bug, but since nothing from bpf_tracing_net.h is used here, would a plain #include "vmlinux.h" (as progs/verifier_sock.c does) be enough for struct mptcp_sock?

The file doesn't use any AF_*/SOL_*/TCP_*/IPPROTO_* constants or bpf_core_read/BPF_CORE_READ that bpf_tracing_net.h provides. It only needs the transitive vmlinux.h for struct mptcp_sock. The 9 verifier_*.c files that need BTF kernel types include vmlinux.h directly.

tools/testing/selftests/bpf/progs/verifier_mptcp.c line 17

Quoted 12 lines
tools/testing/selftests/bpf/progs/verifier_mptcp.c new file +12 -0
+
+char _license[] SEC("license") = "GPL";
+
+__u32 token;
+
+static __always_inline struct bpf_sock *lookup_subflow(struct __sk_buff *skb)
+{
+	struct bpf_sock_tuple tuple = {};
+
+	return bpf_skc_lookup_tcp(skb, &tuple, sizeof(tuple.ipv4),
+				  BPF_F_CURRENT_NETNS, 0);
+}

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35199493791

#3 lore raw
In reply to Jiayuan Chen
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf: verifier use-after-free via derived socket pointer fields --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: verifier use-after-free via derived socket pointer fields
--

commit 4a3543a3768c369da5d09b5be8da85c1dbd33f90
Author: Jiayuan Chen <[email protected]>

mptcp, bpf: reject bpf_sk_release() on msk

This commit fixes a reference leak by changing bpf_skc_to_mptcp_sock() to derive its return value from the subflow using parent_id, preventing bpf_sk_release() from dropping a refcount that was never taken.

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
index 5d7080c260d84..8271d9214da52 100644

[ ... ]

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 11283

Quoted 12 lines
kernel/bpf/verifier.c +8 -0
@@ -11263,6 +11273,14 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		bpf_diag_mod_begin(env, &regs[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE);
 		regs[BPF_REG_0].type &= ~PTR_MAYBE_NULL;
 		regs[BPF_REG_0].id = meta.ref_obj.id;
+	} else if (is_ptr_derive_function(func_id) &&
+		   find_reference_state(env->cur_state, meta.ref_obj.id)) {
+		err = validate_ref_obj(env, &meta.ref_obj);
+		if (err)
+			return err;
+
+		/* Ensures we don't access the object after a release_reference() */
+		regs[BPF_REG_0].parent_id = meta.ref_obj.id;

[Severity: High]
Does assigning parent_id without setting a reference ID or PTR_TRUSTED leave the returned msk pointer untrusted in the verifier?

Because msk is untrusted, if a program accesses its pointer fields (e.g., sk = msk->first), it appears the access falls through to the deprecated verifier compatibility block in check_ptr_to_btf_access():

kernel/bpf/verifier.c:check_ptr_to_btf_access() {
    ...
    } else {
        /* Old compat. Deprecated */
        clear_trusted_flags(&flag);
    }
    ...
}

This yields a plain PTR_TO_BTF_ID without the PTR_UNTRUSTED flag. Since pointer field accesses do not propagate parent_id, sk->parent_id would be 0.

When bpf_sk_release(subflow) is called, the verifier invalidates registers tied to subflow (including msk via parent_id).

Could sk escape this invalidation cascade since its parent_id is 0?

If so, would the program be able to pass this valid, plain PTR_TO_BTF_ID to kernel helpers like bpf_skc_to_tcp_sock(sk)?

At runtime, the helper would dereference the pointer without PROBE_MEM protection after it has been freed, potentially resulting in a use-after-free.

Quoted 2 lines
kernel/bpf/verifier.c +0 -0
 	} else if (is_acquire_function(func_id, meta.map.ptr)) {
 		int id = acquire_reference(env, insn_idx, 0);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#4 lore raw
In reply to Jiayuan Chen

Hi Jiayuan,

+Cc Kalpan, Paolo.

Quoted 9 lines · Jiayuan Chen

A bpf prog can do this today:

	subflow = bpf_skc_lookup_tcp(...);
	msk = bpf_skc_to_mptcp_sock(subflow);
	bpf_sk_release(msk);

bpf_skc_to_mptcp_sock() returns subflow->conn without taking any reference, so bpf_sk_release() drops a refcount nobody took on the msk, and the subflow reference is leaked:

Thank you for looking at this!

Note that Kalpan was looking at this [1], and Paolo suggested removing the helper [2] (but we failed to review the last version so far, sorry about that...)

I don't know if there are progs already using it. If yes, I guess your approach is better (but I'm not comfortable reviewing verifier's code).

@Kalpan, WDYT?

[1] https://lore.kernel.org/[email protected]
[2] https://lore.kernel.org/[email protected]

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#5 lore raw

Hi Matt, Jiayuan,

Thanks for looping me in, and thanks Jiayuan for catching this.

This is a separate bug from what I was fixing. My patch hardens the lockless read of ->conn (the NULL-deref/UAF from #622) with rcu_dereference()/acquire-release/SOCK_RCU_FREE, but that doesn't touch the helper's return-value ownership contract, so the refcount mismatch you're describing exists independently of whether the read itself is safe. Hardening the read fixes my bug; it doesn't and can't fix this one.

Given that, I think Paolo's original suggestion (dropping the helper from tracing_prog_func_proto()) is the right call. I don't see a way to fix the refcounting issue while keeping the helper's current shape (returning a different object than the input, with no reference taken) without a bigger rework of what it hands back to a BPF program.

I checked which prog types can actually chain bpf_skc_lookup_tcp() into bpf_skc_to_mptcp_sock() - it's broader than just tracing. sock_addr, tc_cls_act, xdp, and sk_msg all have bpf_skc_lookup_tcp() available and fall through to bpf_sk_base_func_proto(), which is where bpf_skc_to_mptcp_sock() is registered, so the same pattern Jiayuan showed is reachable from all four of them, not only from tracing programs. sock_ops doesn't register bpf_skc_lookup_tcp() at all, and cg_skb has the lookup helper but doesn't fall through to bpf_sk_base_func_proto(), so neither of those two can chain into it this way. I haven't traced the verifier's reference-tracking closely enough to rule out some other path I'm not aware of.

I don't have visibility into whether any real tracing (or TC/XDP/etc.) programs use this helper today. If that's a live concern for anyone, I'll defer to whoever has that visibility.

I can send a version that drops bpf_skc_to_mptcp_sock() from tracing_prog_func_proto() as Paolo suggested. Given what Jiayuan found also applies to the non-tracing paths above, should that patch just remove the BPF_FUNC_skc_to_mptcp_sock case from bpf_sk_base_func_proto() as well, rather than leaving it there for sock_addr/tc/xdp/sk_msg?

Cheers,
Kalpan Jani

From: Matthieu Baerts <[email protected]>
To: "Jiayuan Chen"<[email protected]>, <[email protected]>, <[email protected]>
Cc: "VEGA"<[email protected]>, "Alexei Starovoitov"<[email protected]>, "Daniel Borkmann"<[email protected]>, "John Fastabend"<[email protected]>, "Andrii Nakryiko"<[email protected]>, "Eduard Zingerman"<[email protected]>, "Kumar Kartikeya Dwivedi"<[email protected]>, "Martin KaFai Lau"<[email protected]>, "Song Liu"<[email protected]>, "Yonghong Song"<[email protected]>, "Jiri Olsa"<[email protected]>, "Emil Tsalapatis"<[email protected]>, "Ihor Solodrai"<[email protected]>, "Shuah Khan"<[email protected]>, "Mat Martineau"<[email protected]>, "Geliang Tang"<[email protected]>, "Matt Bobrowski"<[email protected]>, "Tejun Heo"<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, "Kalpan Jani"<[email protected]>, "Paolo Abeni"<[email protected]>
Date: Thu, 17 Sep 2026 15:09:13 +0530
Subject: Re: [PATCH bpf v1 1/2] mptcp, bpf: reject bpf_sk_release() on msk

Quoted 34 lines

Hi Jiayuan,

+Cc Kalpan, Paolo.

Quoted 9 lines · Jiayuan Chen

A bpf prog can do this today:

    subflow = bpf_skc_lookup_tcp(...);
    msk = bpf_skc_to_mptcp_sock(subflow);
    bpf_sk_release(msk);

bpf_skc_to_mptcp_sock() returns subflow->conn without taking any reference, so bpf_sk_release() drops a refcount nobody took on the msk, and the subflow reference is leaked:

Thank you for looking at this!

Note that Kalpan was looking at this [1], and Paolo suggested removing the helper [2] (but we failed to review the last version so far, sorry about that...)

I don't know if there are progs already using it. If yes, I guess your approach is better (but I'm not comfortable reviewing verifier's code).

@Kalpan, WDYT?

[1] https://lore.kernel.org/[email protected]
[2] https://lore.kernel.org/[email protected]

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.