All of lore.kernel.org
 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 v7] mptcp: bpf: fix NULL deref and UAF in bpf_mptcp_sock_from_subflow()
Date: Mon, 23 Nov 2026 09:30:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-228974@kernel.org> (raw)
In-Reply-To: <20261123084836.1704274-1-kalpan.jani@mpiricsoftware.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] bpf: missing MEM_RCU tagging and sleepable gate in
  check_helper_call()
- [High] mptcp: NULL deref in mptcp_subflow_process_delegated() after
  ULP release
- [High] mptcp: setting SOCK_RCU_FREE in subflow_create_ctx() leaks
  socket refs
- [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
--

commit f52b13bf0febdfcc37197ba78c9e6dab39011b6b
Author: Kalpan Jani <kalpan.jani@mpiricsoftware.com>

mptcp: bpf: fix NULL deref and UAF in bpf_mptcp_sock_from_subflow()

This commit uses RCU and release/acquire ordering to synchronize
bpf_mptcp_sock_from_subflow() with subflow creation, fallback and teardown,
sets SOCK_RCU_FREE on parent and subflow sockets, and adds a needs_rcu
helper check in the BPF verifier.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 5f874979b8d75..15f3136fafa52 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -10912,6 +10912,14 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
>  		return -EINVAL;
>  	}
>  
> +	if (fn->needs_rcu && in_sleepable(env) &&
> +	    !env->cur_state->active_rcu_locks) {
> +		verbose(env,
> +			"helper %s#%d requires bpf_rcu_read_lock() in sleepable context\n",
> +			func_id_name(func_id), func_id);
> +		return -EINVAL;
> +	}
> +
>  	/* Track non-sleepable context for helpers. */
>  	if (!in_sleepable_context(env))
>  		env->insn_aux_data[insn_idx].non_sleepable = true;
> 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: High]
Does check_helper_call() leave the returned mptcp_sock pointer untagged with
MEM_RCU when fn->needs_rcu is true?

When a sleepable context such as a bpf_wq or bpf_task_work callback calls
bpf_skc_to_mptcp_sock() inside bpf_rcu_read_lock(), check_helper_call() sets
R0 to PTR_TO_BTF_ID | ret_flag without MEM_RCU.  On bpf_rcu_read_unlock(),
invalidate_rcu_protected_refs() only demotes registers carrying MEM_RCU to
PTR_UNTRUSTED, so the BPF program can keep using the unreferenced mptcp_sock
pointer after the RCU read section ends and trigger a use-after-free.

In addition, tracing_prog_func_proto() returns NULL whenever prog->sleepable
is set, which rejects bpf_skc_to_mptcp_sock() in sleepable tracing, LSM and
syscall programs before check_helper_call() reaches the fn->needs_rcu check,
even when the call is wrapped in bpf_rcu_read_lock().

[ ... ]

> diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
> index f0a6725d2c376..d8721d38467ec 100644
> --- a/net/mptcp/subflow.c
> +++ b/net/mptcp/subflow.c

[ ... ]

> @@ -1857,6 +1867,8 @@ static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
>  	if (!ctx)
>  		return NULL;
>  
> +	/* BPF readers may access the subflow socket under RCU. */
> +	sock_set_flag(sk, SOCK_RCU_FREE);
>  	rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
>  	INIT_LIST_HEAD(&ctx->node);
>  	INIT_LIST_HEAD(&ctx->delegated_node);

[Severity: High]
Does setting SOCK_RCU_FREE on full TCP subflow sockets break
sk_is_refcounted() and leak socket references on BPF lookups?

include/net/sock.h:sk_is_refcounted() {
	/* Only full sockets have sk->sk_flags. */
	return !sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE);
}

When a BPF program looks up an established or connecting subflow socket via
bpf_sk_lookup_tcp() or bpf_skc_lookup_tcp(), __inet_lookup_established()
increments sk->sk_refcnt.  Because SOCK_RCU_FREE is now set on the subflow
socket, sk_is_refcounted(sk) returns false, so bpf_sk_release() skips
sock_gen_put(sk) and permanently leaks the socket reference.  bpf_sk_assign()
also relies on sk_is_refcounted(sk) and will skip taking a reference on the
subflow socket.

> @@ -2040,6 +2052,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]
Can clearing ctx->conn here and in mptcp_subflow_drop_ctx() cause a NULL
pointer dereference in mptcp_subflow_process_delegated()?

CPU0                                  CPU1
mptcp_subflow_delegate(ssk,
  MPTCP_DELEGATE_SEND)
  set_bit(MPTCP_DELEGATE_SEND,
    &subflow->delegated_status)
  list_add_tail(&subflow->delegated_node,
    &delegated->head)
                                      __mptcp_close_ssk(..., ssk, ...)
                                        __tcp_close(ssk, 0)
                                          inet_csk_destroy_sock(ssk)
                                            ...
                                              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,
    xchg(&subflow->delegated_status, 0))
    sk = subflow->conn
    mptcp_data_lock(sk)  <-- NULL pointer dereference

Once mptcp_subflow_delegate() queues ssk on the per-CPU
mptcp_delegated_actions list, only mptcp_napi_poll() on that CPU dequeues it.
When subflow_ulp_release() runs first, it clears ctx->conn and restores
ssk->sk_prot, so release_sock(ssk) no longer runs tcp_release_cb_override()
to drain delegated_status.  mptcp_napi_poll() then calls
mptcp_subflow_process_delegated() with pending status bits and a NULL
subflow->conn.

[Severity: High]
Can clearing ctx->conn in subflow_ulp_release() also cause a NULL pointer
dereference in subflow_data_ready() when racing with listener close?

CPU0 (<softirq>)                      CPU1
tcp_v4_rcv()
  tcp_child_process(listener_ssk, ...)
                                      __mptcp_destroy_sock()
                                        ...
                                          __mptcp_close_ssk(..., listener_ssk, ...)
                                            __tcp_close(listener_ssk, 0)
                                              ...
                                                subflow_ulp_release(listener_ssk)
                                                  smp_store_release(&ctx->conn,
                                                    NULL)
    listener_ssk->sk_data_ready(listener_ssk)
      subflow_data_ready(listener_ssk)
        parent = subflow->conn
        msk = mptcp_sk(parent)
        parent->sk_data_ready(parent)  <-- NULL pointer dereference

tcp_child_process() calls parent->sk_data_ready(parent) under rcu_read_lock()
without holding the listener socket lock.  Because subflow_ulp_release() sets
ctx->conn to NULL while leaving sk_data_ready set to subflow_data_ready(),
subflow_data_ready() loads a NULL parent pointer and dereferences it in
mptcp_sk(parent) (when CONFIG_DEBUG_NET is enabled) or
parent->sk_data_ready(parent).

[Severity: Medium]
Can bpf_mptcp_sock_from_subflow() still read a freed subflow context after
kfree_rcu(ctx, rcu)?

net/mptcp/subflow.c:subflow_ulp_release() {
	...
	mptcp_subflow_ops_undo_override(ssk);
	if (release)
		kfree_rcu(ctx, rcu);
}

subflow_ulp_release() (as well as __mptcp_close_ssk() and
mptcp_subflow_drop_ctx() when icsk_ulp_ops has already been cleared by
tcp_cleanup_ulp()) frees ctx via kfree_rcu() without clearing
inet_csk(ssk)->icsk_ulp_data or tcp_sk(ssk)->is_mptcp.  If ssk outlives ctx's
RCU grace period, such as while in-flight TX skbs still hold
ssk->sk_wmem_alloc, a BPF program calling bpf_skc_to_mptcp_sock(ssk) will
observe sk_is_mptcp(ssk) == 1 and dereference the dangling icsk_ulp_data
pointer at smp_load_acquire(&ctx->conn).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123084836.1704274-1-kalpan.jani@mpiricsoftware.com?part=1

  reply	other threads:[~2026-11-23  9:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  8:48 [PATCH mptcp-next v7] mptcp: bpf: fix NULL deref and UAF in bpf_mptcp_sock_from_subflow() Kalpan Jani
2026-11-23  9:30 ` sashiko-bot [this message]
2026-11-24 22:29 ` netdev-bot+sashiko

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-228974@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.