MPTCP Linux Development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kalpan Jani" <kalpan.jani@mpiricsoftware.com>, mptcp@lists.linux.dev
Subject: Re: [PATCH mptcp-next v6] mptcp: bpf: fix NULL deref and UAF in bpf_mptcp_sock_from_subflow()
Date: Wed, 18 Nov 2026 09:32:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-220577@kernel.org> (raw)
In-Reply-To: <20261118084011.4051683-1-kalpan.jani@mpiricsoftware.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

      reply	other threads:[~2026-11-18  9:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  8:40 [PATCH mptcp-next v6] mptcp: bpf: fix NULL deref and UAF in bpf_mptcp_sock_from_subflow() Kalpan Jani
2026-11-18  9:32 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-220577@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kalpan.jani@mpiricsoftware.com \
    --cc=mptcp@lists.linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox