kernel/trace/bpf_trace.c | 2 +- net/mptcp/bpf.c | 15 +++++++++++++-- net/mptcp/protocol.c | 6 +++++- net/mptcp/subflow.c | 29 ++++++++++++++++++++++------- 4 files changed, 41 insertions(+), 11 deletions(-)
bpf_mptcp_sock_from_subflow() is reachable from BPF programs via
bpf_skc_to_mptcp_sock() on an arbitrary socket, without the subflow
socket lock held. It assumes sk_is_mptcp(sk) implies a valid subflow
context whose ->conn points to a live parent mptcp_sock. That invariant
does not hold in three windows:
- Fallback: subflow_ulp_fallback() clears icsk_ulp_data via
rcu_assign_pointer(..., NULL) before clearing tcp_sk(sk)->is_mptcp, so a
concurrent reader can observe is_mptcp == 1 with a NULL context.
- Init: subflow_ulp_init() sets is_mptcp = 1 while ->conn is still NULL;
->conn is only assigned later in subflow_syn_recv_sock() or
mptcp_subflow_create_socket(). On CONFIG_DEBUG_NET, mptcp_sk()
dereferences its argument in a WARN_ON(), so mptcp_sk(NULL) faults.
- Teardown: subflow_ulp_release() drops the subflow-owned parent
reference without clearing ->conn. A lockless reader can therefore
access a parent socket after its reference has been dropped.
Load the context with rcu_dereference_check() and the parent socket
with smp_load_acquire(). Publish ->conn with smp_store_release() in
the MP_CAPABLE, MP_JOIN and outgoing subflow paths. Clear ->conn
with a release store before dropping the subflow-owned reference.
Give the parent msk RCU-grace lifetime by setting SOCK_RCU_FREE in
the shared __mptcp_init_sock(), covering both active and passive
paths, and dropping the reset in mptcp_sk_clone_init(). This ensures
a reader that obtains the parent under the applicable RCU protection
does not access freed memory.
Keep bpf_skc_to_mptcp_sock() available to non-sleepable tracing
programs for compatibility, while rejecting sleepable tracing
programs because classic RCU does not protect the parent socket
through sleepable execution.
Fixes: 3bc253c2e652 ("bpf: Add bpf_skc_to_mptcp_sock_proto")
Closes: https://github.com/multipath-tcp/mptcp_net-next/issues/622
Signed-off-by: Kalpan Jani <kalpan.jani@mpiricsoftware.com>
---
Changes since v5:
- Use rcu_dereference_check() to safely access the subflow context.
- Use smp_load_acquire() and smp_store_release() to synchronize access to ctx->conn.
- Clear ctx->conn before dropping the subflow-owned parent socket reference.
- Set SOCK_RCU_FREE in __mptcp_init_sock() to cover both active and passive MPTCP sockets.
- Remove the SOCK_RCU_FREE reset from mptcp_sk_clone_init().
- Reject sleepable tracing programs from using bpf_skc_to_mptcp_sock(), while retaining support for non-sleepable tracing programs.
v5: https://lore.kernel.org/all/20260915093506.465110-1-kalpan.jani@mpiricsoftware.com/
---
kernel/trace/bpf_trace.c | 2 +-
net/mptcp/bpf.c | 15 +++++++++++++--
net/mptcp/protocol.c | 6 +++++-
net/mptcp/subflow.c | 29 ++++++++++++++++++++++-------
4 files changed, 41 insertions(+), 11 deletions(-)
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index a02bd258677e..ea489edbecec 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1715,7 +1715,7 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_skc_to_unix_sock:
return &bpf_skc_to_unix_sock_proto;
case BPF_FUNC_skc_to_mptcp_sock:
- return &bpf_skc_to_mptcp_sock_proto;
+ return prog->sleepable ? NULL : &bpf_skc_to_mptcp_sock_proto;
case BPF_FUNC_sk_storage_get:
return &bpf_sk_storage_get_tracing_proto;
case BPF_FUNC_sk_storage_delete:
diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c
index 08bb037f0951..f43f9a2acc3f 100644
--- a/net/mptcp/bpf.c
+++ b/net/mptcp/bpf.c
@@ -193,8 +193,19 @@ static struct bpf_struct_ops bpf_mptcp_sched_ops = {
struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
{
- if (sk && sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk))
- return mptcp_sk(mptcp_subflow_ctx(sk)->conn);
+ struct mptcp_subflow_context *ctx;
+ struct sock *conn;
+
+ if (sk && sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk)) {
+ ctx = rcu_dereference_check(inet_csk(sk)->icsk_ulp_data,
+ lockdep_sock_is_held(sk));
+ if (ctx) {
+ /* Pair with release stores publishing or clearing ctx->conn. */
+ conn = smp_load_acquire(&ctx->conn);
+ if (conn)
+ return mptcp_sk(conn);
+ }
+ }
return NULL;
}
diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index a4f7e99b30db..fa85ad0dc5a8 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -3177,6 +3177,11 @@ static void __mptcp_init_sock(struct sock *sk)
mptcp_pm_data_init(msk);
spin_lock_init(&msk->fallback_lock);
+ /* the returned parent msk may be handed to lockless readers
+ * (bpf_skc_to_mptcp_sock()); free it after an RCU grace period
+ */
+ sock_set_flag(sk, SOCK_RCU_FREE);
+
/* re-use the csk retrans timer for MPTCP-level retrans */
timer_setup(&sk->mptcp_retransmit_timer, mptcp_retransmit_timer, 0);
timer_setup(&msk->sk.mptcp_tout_timer, mptcp_tout_timer, 0);
@@ -3712,7 +3717,6 @@ struct sock *mptcp_sk_clone_init(const struct sock *sk,
/* passive msk is created after the first/MPC subflow */
msk->subflow_id = 2;
- sock_reset_flag(nsk, SOCK_RCU_FREE);
security_inet_csk_clone(nsk, req);
/* this can't race with mptcp_close(), as the msk is
diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
index 8e386899ceb9..a7612bae675f 100644
--- a/net/mptcp/subflow.c
+++ b/net/mptcp/subflow.c
@@ -780,6 +780,7 @@ static void subflow_ulp_fallback(struct sock *sk,
void mptcp_subflow_drop_ctx(struct sock *ssk)
{
struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
+ struct sock *conn;
if (!ctx)
return;
@@ -787,8 +788,12 @@ void mptcp_subflow_drop_ctx(struct sock *ssk)
list_del(&mptcp_subflow_ctx(ssk)->node);
if (inet_csk(ssk)->icsk_ulp_ops) {
subflow_ulp_fallback(ssk, ctx);
- if (ctx->conn)
- sock_put(ctx->conn);
+ conn = ctx->conn;
+ if (conn) {
+ /* Clear the pointer before dropping its reference. */
+ smp_store_release(&ctx->conn, NULL);
+ sock_put(conn);
+ }
}
kfree_rcu(ctx, rcu);
@@ -861,6 +866,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
if (child && *own_req) {
struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
+ struct sock *conn;
tcp_rsk(req)->drop_req = false;
@@ -880,12 +886,14 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
ctx->setsockopt_seq = listener->setsockopt_seq;
if (ctx->mp_capable) {
- ctx->conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req);
- if (!ctx->conn)
+ conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req);
+ if (!conn)
goto fallback;
+ /* Publish the initialized parent socket to lockless readers. */
+ smp_store_release(&ctx->conn, conn);
ctx->subflow_id = 1;
- owner = mptcp_sk(ctx->conn);
+ owner = mptcp_sk(conn);
if (mp_opt.deny_join_id0)
WRITE_ONCE(owner->pm.remote_deny_join_id0, true);
@@ -920,7 +928,8 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
/* move the msk reference ownership to the subflow */
subflow_req->msk = NULL;
- ctx->conn = (struct sock *)owner;
+ /* Publish the parent socket to lockless readers. */
+ smp_store_release(&ctx->conn, (struct sock *)owner);
if (subflow_use_different_sport(owner, sk)) {
pr_debug("ack inet_sport=%d %d\n",
@@ -1827,7 +1836,8 @@ int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
*new_sock = sf;
sock_hold(sk);
- subflow->conn = sk;
+ /* Publish the parent socket after taking its reference. */
+ smp_store_release(&subflow->conn, sk);
mptcp_subflow_ops_override(sf->sk);
return 0;
@@ -2024,6 +2034,11 @@ static void subflow_ulp_release(struct sock *ssk)
if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW,
&mptcp_sk(sk)->flags))
mptcp_schedule_work(sk);
+
+ /* hide the parent from lockless readers (e.g. BPF) before
+ * dropping the subflow-owned reference
+ */
+ smp_store_release(&ctx->conn, NULL);
sock_put(sk);
}
--
2.43.0
Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.
Critical: 1 · High: 1 · Medium: 3 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Critical] subflow_ulp_release() (net/mptcp/subflow.c:2057) now does
smp_store_release(&ctx->conn, NULL).
- [High] The patch protects only ctx->conn.
- [Medium] bpf_mptcp_sock_from_subflow() still treats a plain load of
tcp_sk(sk)->is_mptcp (sk_is_mptcp()) as proof that icsk_ulp_data holds…
- [Medium] The commit message says bpf_skc_to_mptcp_sock() is rejected
only for "sleepable tracing programs".
- [Medium] bpf: missing classic RCU protection for
bpf_skc_to_mptcp_sock() in async cb
- [Low] The comment in mptcp_subflow_ctx() (net/mptcp/protocol.h) still
says "Use RCU on icsk_ulp_data only for sock diag code".
- [Low] __mptcp_init_sock() now sets SOCK_RCU_FREE on every msk.
--
mptcp: bpf: fix NULL deref and UAF in bpf_mptcp_sock_from_subflow()
This patch makes bpf_mptcp_sock_from_subflow() handle a NULL or stale
subflow context and parent socket. It loads icsk_ulp_data with
rcu_dereference_check() and ctx->conn with smp_load_acquire(), and it
publishes and clears ctx->conn with release stores. It also sets
SOCK_RCU_FREE on every msk in __mptcp_init_sock() and hides
bpf_skc_to_mptcp_sock() from sleepable programs in
tracing_prog_func_proto().
Closes: https://github.com/multipath-tcp/mptcp_net-next/issues/622
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 29260951aa871..c6032fe843627 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1746,7 +1746,7 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
> case BPF_FUNC_skc_to_unix_sock:
> return &bpf_skc_to_unix_sock_proto;
> case BPF_FUNC_skc_to_mptcp_sock:
> - return &bpf_skc_to_mptcp_sock_proto;
> + return prog->sleepable ? NULL : &bpf_skc_to_mptcp_sock_proto;
[Severity: Medium]
Does the prog->sleepable test cover every sleepable context that can
reach this helper?
A program that is not sleepable can still run code in a sleepable
context through an async callback. bpf_wq_set_callback_impl() and the
bpf_task_work_schedule_*() kfuncs register a callback that the
verifier checks with in_sleepable set (see is_async_cb_sleepable() and
push_async_cb()). At runtime, bpf_wq_work() runs that callback from a
workqueue under rcu_read_lock_trace() only. No classic rcu_read_lock()
is held.
prog->sleepable is still false inside the callback. Both
tracing_prog_func_proto() and bpf_sk_base_func_proto() therefore still
hand out bpf_skc_to_mptcp_sock_proto to it.
Such a callback can get a subflow from a sockmap or sockhash with
bpf_map_lookup_elem() and pass it to bpf_skc_to_mptcp_sock(). The
lookup holds a reference on the subflow socket, but not on its ctx or
its parent msk.
bpf_mptcp_sock_from_subflow() would then run rcu_dereference_check()
on icsk_ulp_data with neither the socket lock nor classic RCU held.
That should give a lockdep splat. Worse, it races with
kfree_rcu(ctx, rcu) and with the SOCK_RCU_FREE free of the msk. That
is the same window the new sleepable check is meant to close.
There is a second gap even when the callback wraps the call in
bpf_rcu_read_lock(). bpf_skc_to_mptcp_sock_proto returns
RET_PTR_TO_BTF_ID_OR_NULL without MEM_RCU. bpf_rcu_read_unlock()
therefore does not invalidate the returned mptcp_sock pointer, and the
callback can keep using the parent after the RCU section ends. The
same applies to sleepable programs in other types that use
bpf_rcu_read_lock().
Would it be better to enforce this in the verifier instead of
filtering on prog->sleepable in tracing_prog_func_proto()? For
example, require in_rcu_cs() for BPF_FUNC_skc_to_mptcp_sock and tag
the return type as MEM_RCU. That would also cover the non-tracing
program types that reach this helper through bpf_sk_base_func_proto().
[Severity: Medium]
The commit message says the helper is rejected for "sleepable tracing
programs". However, tracing_prog_func_proto() is also the default fallback
for other program types:
kernel/bpf/bpf_lsm.c:bpf_lsm_func_proto() {
...
default:
return tracing_prog_func_proto(func_id, prog);
}
kernel/bpf/syscall.c:syscall_prog_func_proto() {
...
default:
return tracing_prog_func_proto(func_id, prog);
}
With this change, will sleepable BPF_PROG_TYPE_LSM and
BPF_PROG_TYPE_SYSCALL programs fail to load with an unknown helper error if
they call bpf_skc_to_mptcp_sock()? The same question applies to sleepable
iterators and sleepable fentry/fexit programs.
The Fixes: tag means this will go to stable. Could the commit message also
describe the change for LSM and SYSCALL programs?
> diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c
> index 4cc16cbeb3281..f6d6d31395eae 100644
> --- a/net/mptcp/bpf.c
> +++ b/net/mptcp/bpf.c
> @@ -14,8 +14,19 @@
>
> struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
> {
> - if (sk && sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk))
> - return mptcp_sk(mptcp_subflow_ctx(sk)->conn);
> + struct mptcp_subflow_context *ctx;
> + struct sock *conn;
> +
> + if (sk && sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk)) {
> + ctx = rcu_dereference_check(inet_csk(sk)->icsk_ulp_data,
> + lockdep_sock_is_held(sk));
[Severity: Medium]
Is the fallback window fully closed here? sk_is_mptcp() is a plain load of
tcp_sk(sk)->is_mptcp. Nothing orders that load against the later load of
icsk_ulp_data, and nothing checks that the ULP data belongs to MPTCP.
subflow_ulp_fallback() does:
icsk->icsk_ulp_ops = NULL;
rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
tcp_sk(sk)->is_mptcp = 0;
After that, a passive fallback child can be accept()ed and given
setsockopt(TCP_ULP, "tls"), which puts a tls_context into icsk_ulp_data.
Consider a lockless reader on another CPU, such as a TC program that got
the child from bpf_sk_lookup_tcp(). Suppose it sees is_mptcp == 1 and is
then delayed, for example by a preemptible softirq on PREEMPT_RT or vCPU
steal. Could it then load the tls_context and return the value at the
offset of conn as a struct mptcp_sock?
Would checking icsk_ulp_ops == &subflow_ulp_ops with suitable ordering, or
re-checking is_mptcp after loading ctx, close this window?
> + if (ctx) {
> + /* Pair with release stores publishing or clearing ctx->conn. */
> + conn = smp_load_acquire(&ctx->conn);
> + if (conn)
> + return mptcp_sk(conn);
[Severity: High]
Can this still read a freed mptcp_subflow_context?
The ctx itself is freed with kfree_rcu(ctx, rcu) on paths that never clear
inet_csk(ssk)->icsk_ulp_data or tcp_sk(ssk)->is_mptcp:
subflow_ulp_release(), when release is true
__mptcp_close_ssk(), in the !icsk_ulp_ops branch
mptcp_subflow_drop_ctx(), when icsk_ulp_ops is already NULL so
subflow_ulp_fallback() is skipped
tcp_cleanup_ulp() only resets icsk_ulp_ops after ->release(). Only
subflow_ulp_fallback() unpublishes icsk_ulp_data. For comparison, TLS does
rcu_assign_pointer(icsk->icsk_ulp_data, NULL) first.
A destroyed ssk can outlive the grace period. For example, sk_free()
defers __sk_free() while sk_wmem_alloc is non-zero, which happens when skb
clones wait in a neighbour arp_queue or an NFQUEUE.
Suppose a TC egress program calls bpf_skc_to_mptcp_sock(skb->sk) on such
an skb. It would still pass sk_fullsock() (TCP_CLOSE), sk_is_tcp() and
sk_is_mptcp().
rcu_dereference_check() would then return the dangling icsk_ulp_data,
because RCU only covers readers that existed when kfree_rcu() was queued.
smp_load_acquire(&ctx->conn) would read freed or reused slab memory.
That value goes back to the program as a struct mptcp_sock PTR_TO_BTF_ID.
The program can pass it to helpers such as bpf_sk_storage_get().
The commit message lists the teardown window as fixed. Does icsk_ulp_data
(or is_mptcp) need to be cleared before every kfree_rcu(ctx)? Or should
the helper check icsk_ulp_ops instead?
[Severity: Low]
This isn't a bug, but mptcp_subflow_ctx() in net/mptcp/protocol.h still
says:
/* Use RCU on icsk_ulp_data only for sock diag code */
Should that comment be updated, now that bpf_mptcp_sock_from_subflow() is
also an RCU reader of icsk_ulp_data?
[ ... ]
> diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
> index e89a69ab927c9..cc9603353b79e 100644
> --- a/net/mptcp/protocol.c
> +++ b/net/mptcp/protocol.c
> @@ -3207,6 +3207,11 @@ static void __mptcp_init_sock(struct sock *sk)
> mptcp_pm_data_init(msk);
> spin_lock_init(&msk->fallback_lock);
>
> + /* the returned parent msk may be handed to lockless readers
> + * (bpf_skc_to_mptcp_sock()); free it after an RCU grace period
> + */
> + sock_set_flag(sk, SOCK_RCU_FREE);
[Severity: Low]
This isn't a bug, but mptcp_listen() still sets the same flag separately.
That was added by 5e20087d1b67 ("mptcp: handle mptcp listener destruction
via rcu"):
mptcp_set_state(sk, TCP_LISTEN);
sock_set_flag(sk, SOCK_RCU_FREE);
__mptcp_init_sock() runs from both mptcp_init_sock() and
mptcp_sk_clone_init(). Is the listener-only set now redundant?
[ ... ]
> diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
> index f0a6725d2c376..c6dbc49404100 100644
> --- a/net/mptcp/subflow.c
> +++ b/net/mptcp/subflow.c
[ ... ]
> @@ -2040,6 +2050,11 @@ static void subflow_ulp_release(struct sock *ssk)
> if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW,
> &mptcp_sk(sk)->flags))
> mptcp_schedule_work(sk);
> +
> + /* hide the parent from lockless readers (e.g. BPF) before
> + * dropping the subflow-owned reference
> + */
> + smp_store_release(&ctx->conn, NULL);
> sock_put(sk);
> }
[Severity: Critical]
Can this NULL store cause NULL pointer dereferences in other lockless
readers of subflow->conn?
After subflow_ulp_release() runs, several things are left in place:
icsk_ulp_data still points at ctx, tcp_sk(ssk)->is_mptcp is still 1, and
the subflow callbacks are still installed (sk_data_ready is
subflow_data_ready, icsk_af_ops is subflow_specific).
Before this patch, those readers got a stale msk pointer that was freed
only after an RCU grace period. Now they get NULL, and only the BPF reader
checks for that.
One case is the listener race fixed by 5e20087d1b67 ("mptcp: handle mptcp
listener destruction via rcu"). CPU1 closes the MPTCP listener:
__mptcp_close()
mptcp_check_listen_stop()
__mptcp_destroy_sock()
mptcp_destroy()
mptcp_destroy_common()
__mptcp_close_ssk()
__tcp_close(ssk)
inet_csk_destroy_sock()
tcp_cleanup_ulp()
subflow_ulp_release()
smp_store_release(&ctx->conn, NULL)
At the same time, CPU0 is in tcp_v4_rcv() in softirq. It has already found
the TCP_NEW_SYN_RECV request with the listener ssk in TCP_LISTEN, and it
continues:
tcp_check_req()
subflow_syn_recv_sock()
mptcp_sk_clone_init(listener->conn, ...)
sk_clone_lock(NULL)
There is a second path. tcp_child_process() calls the listener's
sk_data_ready, which is still subflow_data_ready():
struct sock *parent = subflow->conn;
...
parent->sk_data_ready(parent);
If the state is already TCP_CLOSE, the NULL dereference happens earlier,
in the __mptcp_check_fallback(msk) call inside the WARN_ON_ONCE().
Delegated actions look similar. mptcp_napi_poll() calls
mptcp_subflow_process_delegated(), which does:
struct sock *sk = subflow->conn;
if (status & BIT(MPTCP_DELEGATE_SEND)) {
mptcp_data_lock(sk);
There is no NULL check. The action can be queued from softirq and the
subflow closed on another CPU before the MPTCP NAPI runs.
subflow_error_report(), mptcp_check_fallback() and mptcp_try_fallback()
also use subflow->conn without a NULL check.
The new store also breaks the invariant described in mptcp_stream_accept():
/* is_mptcp should be false if subflow->conn is missing, see
* subflow_syn_recv_sock()
*/
The WARN_ON_ONCE() at the end of subflow_syn_recv_sock() checks the same
invariant. mptcp_subflow_drop_ctx() also gains a second NULL store of
ctx->conn.
SOCK_RCU_FREE is now set on every msk. Would leaving ->conn non-NULL be
enough for RCU readers? If not, every lockless reader would need a NULL
check, and is_mptcp, icsk_ulp_data and the callbacks would have to be
unpublished together.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118084011.4051683-1-kalpan.jani%40mpiricsoftware.com
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] mptcp: NULL deref in mptcp_subflow_process_delegated() after
ULP release
- [High] mptcp: NULL deref in subflow_data_ready() racing with listener
close
- [Medium] mptcp: bpf: UAF on subflow ctx due to uncleared icsk_ulp_data
- [Medium] bpf: missing classic RCU protection for
bpf_skc_to_mptcp_sock() in async cb
--
commit aa822fb1b36957331db4325cd8176b3636136b92
Author: Kalpan Jani <kalpan.jani@mpiricsoftware.com>
mptcp: bpf: fix NULL deref and UAF in bpf_mptcp_sock_from_subflow()
This commit updates bpf_mptcp_sock_from_subflow() and MPTCP subflow
lifecycle paths to use RCU and acquire/release operations on ctx->conn,
enables SOCK_RCU_FREE on all MPTCP parent sockets, and disallows
bpf_skc_to_mptcp_sock() in sleepable tracing programs.
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 29260951aa871..c6032fe843627 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1746,7 +1746,7 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
> case BPF_FUNC_skc_to_unix_sock:
> return &bpf_skc_to_unix_sock_proto;
> case BPF_FUNC_skc_to_mptcp_sock:
> - return &bpf_skc_to_mptcp_sock_proto;
> + return prog->sleepable ? NULL : &bpf_skc_to_mptcp_sock_proto;
> case BPF_FUNC_sk_storage_get:
> return &bpf_sk_storage_get_tracing_proto;
> case BPF_FUNC_sk_storage_delete:
[Severity: Medium]
Can a non-sleepable BPF program still invoke bpf_skc_to_mptcp_sock()
from a sleepable bpf_wq or bpf_task_work callback without classic RCU
protection?
A non-sleepable tracing or networking program has prog->sleepable == false,
so both tracing_prog_func_proto() and bpf_sk_base_func_proto() allow
BPF_FUNC_skc_to_mptcp_sock. When that program registers an async callback
with bpf_wq_set_callback_impl() or bpf_task_work_schedule_resume(), the
callback runs in sleepable context under rcu_read_lock_trace() rather than
classic rcu_read_lock(). Looking up a TCP subflow socket in a sockmap or
sockhash and passing it to bpf_skc_to_mptcp_sock() inside the callback then
executes bpf_mptcp_sock_from_subflow() without holding either the subflow
socket lock or classic RCU, racing with kfree_rcu(ctx, rcu) and parent
socket destruction (and because bpf_skc_to_mptcp_sock_proto does not return
a MEM_RCU pointer, bpf_rcu_read_unlock() would not invalidate the returned
mptcp_sock pointer either).
> diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c
> index 4cc16cbeb3281..f6d6d31395eae 100644
> --- a/net/mptcp/bpf.c
> +++ b/net/mptcp/bpf.c
> @@ -14,8 +14,19 @@
>
> struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
> {
> - if (sk && sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk))
> - return mptcp_sk(mptcp_subflow_ctx(sk)->conn);
> + struct mptcp_subflow_context *ctx;
> + struct sock *conn;
> +
> + if (sk && sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk)) {
> + ctx = rcu_dereference_check(inet_csk(sk)->icsk_ulp_data,
> + lockdep_sock_is_held(sk));
> + if (ctx) {
> + /* Pair with release stores publishing or clearing ctx->conn. */
> + conn = smp_load_acquire(&ctx->conn);
> + if (conn)
> + return mptcp_sk(conn);
> + }
> + }
>
> return NULL;
> }
[Severity: Medium]
Can bpf_mptcp_sock_from_subflow() dereference a freed ctx after its RCU
grace period expires while sk is still alive?
subflow_ulp_fallback() clears icsk_ulp_data and tcp_sk(sk)->is_mptcp, but
the ULP teardown paths in subflow_ulp_release(), __mptcp_close_ssk() (when
icsk_ulp_ops is already NULL), and mptcp_subflow_drop_ctx() (when called
after tcp_cleanup_ulp()) schedule kfree_rcu(ctx, rcu) without clearing
inet_csk(ssk)->icsk_ulp_data or tcp_sk(ssk)->is_mptcp.
CPU0 CPU1
__mptcp_close_ssk(sk, ssk, ...)
__tcp_close(ssk, 0)
inet_csk_destroy_sock(ssk)
tcp_cleanup_ulp(ssk)
subflow_ulp_release(ssk)
kfree_rcu(ctx, rcu)
(RCU grace period elapses;
ctx is freed, while ssk stays
alive waiting on sk_wmem_alloc
or refcount)
bpf_mptcp_sock_from_subflow(ssk)
sk_is_mptcp(ssk) == true
ctx = rcu_dereference_check(
icsk->icsk_ulp_data, ...)
smp_load_acquire(&ctx->conn)
<-- use-after-free
Clearing icsk_ulp_data with rcu_assign_pointer(icsk->icsk_ulp_data, NULL)
before kfree_rcu() in subflow_ulp_release() would prevent readers from
observing a dangling ctx pointer.
[ ... ]
> diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
> index f0a6725d2c376..c6dbc49404100 100644
> --- a/net/mptcp/subflow.c
> +++ b/net/mptcp/subflow.c
> @@ -782,6 +782,7 @@ static void subflow_ulp_fallback(struct sock *sk,
> void mptcp_subflow_drop_ctx(struct sock *ssk)
> {
> struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
> + struct sock *conn;
>
> if (!ctx)
> return;
> @@ -789,8 +790,12 @@ void mptcp_subflow_drop_ctx(struct sock *ssk)
> list_del(&mptcp_subflow_ctx(ssk)->node);
> if (inet_csk(ssk)->icsk_ulp_ops) {
> subflow_ulp_fallback(ssk, ctx);
> - if (ctx->conn)
> - sock_put(ctx->conn);
> + conn = ctx->conn;
> + if (conn) {
> + /* Clear the pointer before dropping its reference. */
> + smp_store_release(&ctx->conn, NULL);
> + sock_put(conn);
> + }
> }
>
> kfree_rcu(ctx, rcu);
[Severity: High]
Can clearing ctx->conn in mptcp_subflow_drop_ctx() and
subflow_ulp_release() cause a NULL pointer dereference in
mptcp_subflow_process_delegated()?
When mptcp_subflow_delegate() queues a subflow on the per-CPU
mptcp_delegated_actions list with MPTCP_DELEGATE_SEND or
MPTCP_DELEGATE_SNDBUF set, only mptcp_napi_poll() on that CPU dequeues it.
If the subflow is closed and destroyed before mptcp_napi_poll() runs,
subflow_ulp_release() (or mptcp_subflow_drop_ctx()) clears ctx->conn to
NULL and restores ssk->sk_prot via mptcp_subflow_ops_undo_override(ssk), so
the subsequent release_sock(ssk) calls tcp_release_cb() instead of
tcp_release_cb_override() and leaves the delegated action bits set:
CPU0 CPU1
mptcp_subflow_delegate(subflow,
MPTCP_DELEGATE_SEND)
/* queued on CPU0 delegated_head */
__mptcp_close_ssk(sk, ssk, ...)
lock_sock_nested(ssk, ...)
__tcp_close(ssk, 0)
...
subflow_ulp_release(ssk)
smp_store_release(&ctx->conn,
NULL)
mptcp_subflow_ops_undo_override(ssk)
release_sock(ssk)
mptcp_napi_poll()
mptcp_subflow_process_delegated(ssk, status)
sk = subflow->conn; /* NULL */
mptcp_data_lock(sk)
<-- NULL pointer dereference
net/mptcp/protocol.c:mptcp_subflow_process_delegated() {
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
struct sock *sk = subflow->conn;
if (status & BIT(MPTCP_DELEGATE_SEND)) {
mptcp_data_lock(sk);
...
}
[ ... ]
> @@ -2040,6 +2050,11 @@ static void subflow_ulp_release(struct sock *ssk)
> if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW,
> &mptcp_sk(sk)->flags))
> mptcp_schedule_work(sk);
> +
> + /* hide the parent from lockless readers (e.g. BPF) before
> + * dropping the subflow-owned reference
> + */
> + smp_store_release(&ctx->conn, NULL);
> sock_put(sk);
> }
>
[Severity: High]
Does clearing ctx->conn in subflow_ulp_release() also introduce a NULL
pointer dereference in subflow_data_ready() when an incoming handshake
races with closing a listener socket?
During 3WHS completion, tcp_child_process() runs under rcu_read_lock()
without holding the listener socket lock and calls
READ_ONCE(parent->sk_data_ready)(parent), which invokes
subflow_data_ready() on the listener subflow. Because
subflow_ulp_release() clears ctx->conn without resetting sk_data_ready, a
concurrent listener close can clear subflow->conn while
subflow_data_ready() is running:
CPU0 CPU1
tcp_child_process(listener_ssk, ...)
subflow_data_ready(listener_ssk)
state = 1 << inet_sk_state_load(sk)
__mptcp_close_ssk(sk, listener_ssk, ...)
__tcp_close(listener_ssk, 0)
inet_csk_destroy_sock(listener_ssk)
tcp_cleanup_ulp(listener_ssk)
subflow_ulp_release(listener_ssk)
smp_store_release(&ctx->conn,
NULL)
parent = subflow->conn; /* NULL */
msk = mptcp_sk(parent);
...
parent->sk_data_ready(parent);
<-- NULL pointer dereference
net/mptcp/subflow.c:subflow_data_ready() {
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
u16 state = 1 << inet_sk_state_load(sk);
struct sock *parent = subflow->conn;
struct mptcp_sock *msk;
trace_sk_data_ready(sk);
msk = mptcp_sk(parent);
if (state & TCPF_LISTEN) {
if (reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue))
return;
parent->sk_data_ready(parent);
return;
}
...
}
On CONFIG_DEBUG_NET, mptcp_sk(parent) faults immediately on NULL; without
CONFIG_DEBUG_NET, parent->sk_data_ready(parent) dereferences NULL when
state was read before the listener left TCP_LISTEN.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118084011.4051683-1-kalpan.jani@mpiricsoftware.com?part=1
© 2016 - 2026 Red Hat, Inc.