netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: 2122295973@qq.com
Cc: netdev@vger.kernel.org, mptcp@lists.linux.dev,
	matttbe@kernel.org, martineau@kernel.org, geliang@kernel.org,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, kuniyu@google.com,
	willemb@google.com, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH net v4 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next()
Date: Tue, 17 Nov 2026 09:43:54 +0000	[thread overview]
Message-ID: <179490863464.3133.2064838572417107624@kernel.org> (raw)
In-Reply-To: <tencent_647B95315D18DBA311A33E8120517E1DA60A@qq.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 (1) · 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).

- [Medium] Under the new check in mptcp_token_iter_next()
  (net/mptcp/token.c:381-386), a socket passes if its token is non-zero…
- [Low] The commit message says the affected callers are "mptcp_diag and
  the userspace path manager".
- [Low] The patch uses a plain READ_ONCE() of msk->token as an
  "initialisation complete" signal (net/mptcp/token.c:381), but nothing…

Pre-existing issues:
- [Medium] This problem wasn't introduced by this patch, but the walk in
  mptcp_token_iter_next() (net/mptcp/token.c:370-393) does not follow…

--

net: mptcp: verify the token slot in mptcp_token_iter_next()

This patch makes mptcp_token_iter_next() re-read msk->token after it takes
a reference on a socket. The socket is skipped unless the token is
non-zero and hashes to the slot being walked. The goal is to stop the
iterator from returning a recycled msk whose pm state is not initialised.

> mptcp_token_iter_next() takes a reference on a socket found on the chain
> of the slot it is walking and then re-checks only net_eq().  A socket
> that was freed and whose memory has been reused by a new msk in the same
> network namespace passes that check while it is still zeroed, and the
> callers - mptcp_diag and the userspace path manager - then use mptcp_pm
> state that has not been initialised yet.

[Severity: Low]
Is "the userspace path manager" the right caller to name here?

Apart from mptcp_diag_dump(), every caller of mptcp_token_iter_next() is
in net/mptcp/pm_kernel.c:

  mptcp_nl_add_subflow_or_signal_addr()
  mptcp_nl_remove_subflow_and_signal_addr()
  mptcp_nl_remove_id_zero_address()
  mptcp_nl_flush_addrs_list()
  mptcp_pm_nl_set_flags_all()

Each of them skips userspace PM sockets:

    if (mptcp_pm_is_userspace(msk))
        goto next;

net/mptcp/pm_userspace.c does not use the iterator. It uses
mptcp_token_get_sock(), which already re-checks the token after taking
the reference.

Could the commit message name the in-kernel path manager endpoint
operations instead?

> diff --git a/net/mptcp/token.c b/net/mptcp/token.c
> index 8a603fc8dc059..48470d6e751d6 100644
> --- a/net/mptcp/token.c
> +++ b/net/mptcp/token.c

[ ... ]

> @@ -377,7 +378,9 @@ struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
>  			if (!refcount_inc_not_zero_acquire(&sk->sk_refcnt))
>  				continue;
>  
> -			if (!net_eq(sock_net(sk), net)) {
> +			token = READ_ONCE(mptcp_sk(sk)->token);
> +			if (!token || (token & token_mask) != slot ||
> +			    !net_eq(sock_net(sk), net)) {

[Severity: Medium]
Does a non-zero token that maps to this slot really show that the msk is
on this chain and fully set up?

On the passive path, the token is published while the refcount is already
non-zero and before the msk is hashed. First, sk_clone_lock() does:

    smp_wmb();
    refcount_set(&newsk->sk_refcnt, 2);

Then mptcp_sk_clone_init() in net/mptcp/protocol.c does:

    WRITE_ONCE(msk->token, subflow_req->token);
    ...
    mptcp_set_state(nsk, TCP_ESTABLISHED);
    WRITE_ONCE(msk->first, ssk);
    ...
    list_add(&subflow->node, &msk->conn_list);
    ...
    mptcp_copy_inaddrs(nsk, ssk);
    ...
    if (!mptcp_token_accept(subflow_req, msk)) {

Here is one way to hit it:

- A diag dump or PM netlink walk holds a stale RCU pointer to a freed msk
  in slot S.
- That memory is reused by this clone.
- The new token happens to hash to S.

refcount_inc_not_zero_acquire() then succeeds, and the token is non-zero
and matches the slot. So the iterator returns an msk that is not hashed
yet.

Callers then do lockless reads before lock_sock(): sk_state, conn_list,
and the inet addresses via mptcp_local_address(). These race with the
initialisation above.

The failure path looks worse. mptcp_token_accept() can fail, for example
when mptcp_subflow_reqsk_clone()->mptcp_token_move_request() has moved
the request token during reqsk migration. In that case
mptcp_sk_clone_destroy() does:

    bh_unlock_sock(nsk);
    sock_put(nsk);
    sock_set_flag(nsk, SOCK_DEAD);
    tcp_orphan_count_inc();
    inet_sk(nsk)->inet_num = 0;
    inet_csk_destroy_sock(nsk);

A reader blocked in lock_sock() can get the socket right after
bh_unlock_sock(). Examples are mptcp_nl_remove_subflow_and_signal_addr()
and mptcp_nl_flush_addrs_list(). The same applies to lock_sock_fast() in
mptcp_diag_fill_info().

Can that reader then run pm and subflow operations while
inet_csk_destroy_sock()->mptcp_destroy() tears down the msk without the
socket lock?

Would a check tied to hash membership close this window? Another option
would be to assign msk->token inside mptcp_token_accept() under the
bucket lock.

[Severity: Low]
Is a plain READ_ONCE() enough to treat the token as an "initialisation
done" flag?

On the clone path, __mptcp_init_sock()->mptcp_pm_data_init() runs before
WRITE_ONCE(msk->token, ...) in mptcp_sk_clone_init(). There is no release
barrier between them.

On the reader side, refcount_inc_not_zero_acquire() pairs only with the
smp_wmb()/refcount_set() in sk_clone_lock(). That store comes before any
MPTCP init. After the token load there is only a control dependency.

The callers make some lockless reads before taking the socket lock:

  mptcp_pm_is_userspace() (reads pm.pm_type)
  list_empty(&msk->conn_list)
  READ_ONCE(msk->fully_established)
  the pm counters in mptcp_diag_fill_info():

    info->mptcpi_extra_subflows = READ_ONCE(msk->pm.extra_subflows);
    info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);

On arm64 or POWER, can these reads be satisfied before the token load?
They would then see the listener values copied by sock_copy() even though
the token check passed.

Would this need smp_store_release()/smp_load_acquire() on msk->token, or
a hash membership check?

>  				sock_put(sk);
>  				continue;
>  			}

[Severity: Medium]
This isn't a bug introduced by this patch, but the walk in
mptcp_token_iter_next() does not follow the nulls-list protocol that the
other msk_chain readers in net/mptcp/token.c use.

mptcp_token_get_sock() restarts when its post-acquire re-check fails, and
when the walk ends on another chain:

    if (READ_ONCE(msk->token) != token ||
        !net_eq(sock_net(sk), net)) {
        sock_put(sk);
        goto again;
    }
    ...
    if (get_nulls_value(pos) != (token & token_mask))
        goto again;

mptcp_token_exists() does the same nulls check.

The msk cache is SLAB_TYPESAFE_BY_RCU. An msk deleted from slot X and
re-added to slot Y can move a lockless walker onto chain Y.

The new (token & token_mask) != slot test detects that hop, but it
responds with continue. The walker then takes and drops a reference on
each entry of chain Y, and stops at Y's nulls marker.

Are the remaining entries of slot X skipped for this dump or PM pass?

Before this patch the X entries were skipped as well, and Y's entries
were returned instead.

Should a failed slot check restart the bucket walk? And should
get_nulls_value(pos) be compared against slot when the walk ends?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/tencent_D0DCF4B132B20B70FB5AE2D9272AA4AEAF07%40qq.com

  reply	other threads:[~2026-11-17  9:43 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261115155747.178858-1-2122295973@qq.com>
2026-11-15 15:57 ` [PATCH net v4 1/3] net: mptcp: take token lookup references with acquire ordering Hang Nan
2026-11-17  9:43   ` netdev-bot+sashiko
2026-11-15 15:57 ` [PATCH net v4 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() Hang Nan
2026-11-17  9:43   ` netdev-bot+sashiko
2026-11-15 15:57 ` [PATCH net v4 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next() Hang Nan
2026-11-17  9:43   ` netdev-bot+sashiko [this message]
2026-11-16 12:20 ` [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups Hang Nan
2026-11-16 14:58 ` Hang Nan

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=179490863464.3133.2064838572417107624@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=2122295973@qq.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=geliang@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martineau@kernel.org \
    --cc=matttbe@kernel.org \
    --cc=mptcp@lists.linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=willemb@google.com \
    /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;
as well as URLs for NNTP newsgroup(s).