The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: netdev-bot+sashiko@kernel.org
Cc: netdev@vger.kernel.org, edumazet@kernel.org,
	ncardwell@google.com, kuniyu@google.com, davem@davemloft.net,
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	fw@strlen.de, daniel@iogearbox.net, linux-kernel@vger.kernel.org,
	bpf@vger.kernel.org
Subject: Re: [PATCH net] tcp: fix CA module refcount underflow in tcp_v4_syn_recv_sock()
Date: Wed, 25 Nov 2026 14:37:10 +0800	[thread overview]
Message-ID: <663c0be8-60c8-45d6-baf6-5bc4ac0ae173@linux.dev> (raw)
In-Reply-To: <179558800299.3135.4905748131998876050@kernel.org>



On 11/25/26 2:26 PM, netdev-bot+sashiko@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issue(s) to consider.
> 
> Critical: 0 · High: 0 · Medium: 1 · Low: 1
> 
> 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] The new error path in tcp_v4_syn_recv_sock()
>   (net/ipv4/tcp_ipv4.c) jumps to clear_ca_and_exit when…
> - [Low] The commit message presents the bug as a module refcount
>   underflow that shows up as a module_put() WARNING.
> 
> --
> 
> tcp: fix CA module refcount underflow in tcp_v4_syn_recv_sock()
> 
> The child socket inherits icsk_ca_ops from the listener without taking a
> reference. If inet_csk_route_child_sock() fails, the child then puts the
> listener's reference. This patch clears icsk_ca_ops on that error path and
> makes tcp_cleanup_congestion_control() skip a socket that has no CA.
> 
>> If inet_csk_route_child_sock() fails in between, we go to put_and_exit
>> and tcp_done() ends up in tcp_cleanup_congestion_control(), which puts
>> the reference unconditionally. The one dropped belongs to the listener.
> 
> [Severity: Low]
> 
> Is the impact larger than a refcount underflow warning?
> 

> module_put() uses atomic_dec_if_positive() and only warns when the count
> would go negative:
> 
> kernel/module/main.c:module_put() {
>     ...
> 		ret = atomic_dec_if_positive(&module->refcnt);
> 		WARN_ON(ret < 0);	/* Failed to put refcount */
>     ...
> }
> 
> While the listener still holds its reference, the child's put in
> tcp_cleanup_congestion_control() succeeds silently and uses up the
> listener's reference. The quoted WARN only fires after earlier occurrences
> have already drained that reference.
> 
> Once that has happened, try_release_module_ref() lets rmmod of tcp_bbr
> succeed while the listener's icsk_ca_ops still points into the module.
> Later children read icsk_ca_ops->owner through bpf_try_module_get() in
> tcp_ca_openreq_child(), and closing the listener calls ->release() on it.
> Could either of these be a use-after-free?
> 
> If the count has dropped below the base instead, rmmod would hit
> BUG_ON(ret < 0) in try_release_module_ref().
> 
> For BPF struct_ops congestion control, bpf_module_put() goes through
> bpf_struct_ops_put() -> bpf_map_put():
> 
> include/linux/bpf.h:bpf_module_put() {
> 	if (owner == BPF_MODULE_OWNER)
> 		bpf_struct_ops_put(data);
> 	else
> 		module_put(owner);
> }
> 
> In that case the extra put drops a struct_ops map reference while the
> listener is still using the map.
> 
> Could the commit message describe this impact, for the benefit of stable
> maintainers?
> 

It's meanless, we can't enumerate all cases‌ into commit message.


>> diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
>> index e3f462f161dd6..0f99405a78b11 100644
>> --- a/net/ipv4/tcp_cong.c
>> +++ b/net/ipv4/tcp_cong.c
>> @@ -271,6 +271,9 @@ void tcp_cleanup_congestion_control(struct sock *sk)
>>  {
>>  	struct inet_connection_sock *icsk = inet_csk(sk);
>>  
>> +	if (!icsk->icsk_ca_ops)
>> +		return;
>> +
>>  	if (icsk->icsk_ca_initialized && icsk->icsk_ca_ops->release)
>>  		icsk->icsk_ca_ops->release(sk);
>>  	icsk->icsk_ca_initialized = 0;
>> diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
>> index 2fc92a7884fc8..8aa52376cf0aa 100644
>> --- a/net/ipv4/tcp_ipv4.c
>> +++ b/net/ipv4/tcp_ipv4.c
>> @@ -1732,7 +1732,7 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
>>  	if (!dst) {
>>  		dst = inet_csk_route_child_sock(sk, newsk, req);
>>  		if (!dst)
>> -			goto put_and_exit;
>> +			goto clear_ca_and_exit;
>>  	} else {
>>  		/* syncookie case : see end of cookie_v4_check() */
>>  	}
>> @@ -1789,6 +1789,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
>>  exit:
>>  	tcp_listendrop(sk);
>>  	return NULL;
>> +clear_ca_and_exit:
>> +	/* newsk holds no reference on the inherited CA yet. */
>> +	inet_csk(newsk)->icsk_ca_ops = NULL;
> 
> [Severity: Medium]
> 
> Does this comment hold in all cases?
> 



> tcp_create_openreq_child() calls inet_csk_clone_lock(). That function moves
> the child to TCP_SYN_RECV and then calls security_inet_csk_clone(), all
> before the route lookup above:
> 
> net/ipv4/inet_connection_sock.c:inet_csk_clone_lock() {
>     ...
> 	inet_sk_set_state(newsk, TCP_SYN_RECV);
> 
> 	inet_clone_ulp(req, newsk, priority);
> 
> 	security_inet_csk_clone(newsk, req);
>     ...
> }
> 
> bpf_lsm_inet_csk_clone is listed in bpf_lsm_locked_sockopt_hooks in
> kernel/bpf/bpf_lsm.c. A BPF_LSM_CGROUP program attached there can
> therefore call bpf_setsockopt(TCP_CONGESTION) on the child:
> 
> sol_tcp_sockopt_congestion()
>   do_tcp_setsockopt()
>     tcp_set_congestion_control()
>       bpf_try_module_get(ca, ca->owner)  <- takes a reference on the new CA
>       tcp_reinit_congestion_control()
>         tcp_init_congestion_control()    <- ca->init(),
>                                             icsk_ca_initialized = 1
> 

It's not for net tree and I have already sent a fix to bpf tree. 

I also intentionally mentioned it under diff stat to suppress this review
but I think I failed:

'''
   ...
   Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
   ---
   security_inet_csk_clone() -> bpf_setsockopt(TCP_CONGESTION) from a
   lsm_cgroup prog has a similar problem, the child puts a reference it
   never took. That one is being fixed in the bpf tree.
   ...
'''


      reply	other threads:[~2026-11-25  6:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  6:26 [PATCH net] tcp: fix CA module refcount underflow in tcp_v4_syn_recv_sock() Jiayuan Chen
2026-11-25  6:26 ` netdev-bot+sashiko
2026-11-25  6:37   ` Jiayuan Chen [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=663c0be8-60c8-45d6-baf6-5bc4ac0ae173@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ncardwell@google.com \
    --cc=netdev-bot+sashiko@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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