tcp: fix CA module refcount underflow in tcp_v4_syn_recv_sock()
Subsystems (4)
PATCH 1/1 tcp: fix CA module refcount underflow in tcp_v4_syn_recv_sock()
Issue found by sashiko [1].
tcp_v4_syn_recv_sock() creates the child with tcp_create_openreq_child(), which inherits icsk->icsk_ca_ops from the listener without taking a reference. The reference is only taken later in tcp_ca_openreq_child(). 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.
Seen with a listener using bbr (built as a module) and TCP_DEFER_ACCEPT:
make the route to the client prohibited once the handshake is done, then let the client send data.
WARNING: kernel/module/main.c:969 at module_put.part.0+0x97/0x160, CPU#1: swapper/1/0 Modules linked in: tcp_bbr(OE) RIP: 0010:module_put.part.0+0x97/0x160 Call Trace: <IRQ> module_put+0x13/0x30 tcp_cleanup_congestion_control+0xae/0xe0 tcp_v4_destroy_sock+0x103/0x360 inet_csk_destroy_sock+0xb7/0x1e0 tcp_done+0x146/0x1e0 tcp_v4_syn_recv_sock+0x73a/0x9f0 tcp_check_req+0x351/0x1130 tcp_v4_rcv+0xe82/0x22e0 ip_protocol_deliver_rcu+0x5f/0x340 ip_local_deliver_finish+0x19f/0x3a0 ip_local_deliver+0xc9/0xf0 ip_rcv+0x1b1/0x1c0 __netif_receive_skb_one_core+0xc4/0xe0 __netif_receive_skb+0x23/0xb0 process_backlog+0x27a/0x950 __napi_poll.constprop.0+0x61/0x2c0 net_rx_action+0x372/0x800 handle_softirqs+0x109/0x570 </IRQ>
Options considered:
1. Take the reference in tcp_create_openreq_child(), see the diff below the "---". It adds a get/put pair on the passive open path.
2. Point the child at tcp_reno (built in, no owner) on this error path, so the put becomes a no-op. It works but looks odd.
Instead, clear icsk_ca_ops on this error path and make tcp_cleanup_congestion_control() skip a socket without a CA.
[1] https://sashiko.dev/#/message/20260606095014.2531B1F00893%40smtp.kernel.org
---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.
The root cause can be fixed as below instead, but I am worried that the extra refcounting may hurt performance (not tested):
diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c
--- a/net/ipv4/tcp_minisocks.c
+++ b/net/ipv4/tcp_minisocks.c
@@ -506,6 +506,8 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)
rcu_read_lock();
ca = tcp_ca_find_key(ca_key);
if (likely(ca && bpf_try_module_get(ca, ca->owner))) {
+ bpf_module_put(icsk->icsk_ca_ops,
+ icsk->icsk_ca_ops->owner);
icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst);
WRITE_ONCE(icsk->icsk_ca_ops, ca);
ca_got_dst = true;
@@ -514,10 +516,10 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)
}
/* If no valid choice made yet, assign current system default ca. */
- if (!ca_got_dst &&
- (!icsk->icsk_ca_setsockopt ||
- !bpf_try_module_get(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner)))
+ if (!ca_got_dst && !icsk->icsk_ca_setsockopt) {
+ bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner);
tcp_assign_congestion_control(sk);
+ }
tcp_set_ca_state(sk, TCP_CA_Open);
}
@@ -590,6 +592,13 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,
newsk->sk_txhash = READ_ONCE(treq->txhash);
newtp->total_retrans = req->num_retrans;
+ /* Hold the inherited CA until tcp_ca_openreq_child() makes the choice. */
+ if (!bpf_try_module_get(newicsk->icsk_ca_ops,
+ newicsk->icsk_ca_ops->owner)) {
+ WRITE_ONCE(newicsk->icsk_ca_ops, &tcp_reno);
+ newicsk->icsk_ca_setsockopt = 0;
+ }
+
tcp_init_xmit_timers(newsk);
WRITE_ONCE(newtp->write_seq, newtp->pushed_seq = treq->snt_isn + 1);
---
net/ipv4/tcp_cong.c | 3 +++
net/ipv4/tcp_ipv4.c | 5 ++++-
2 files changed, 7 insertions(+), 1 deletion(-)net/ipv4/tcp_cong.c +3 -0
@@ -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 ;
net/ipv4/tcp_ipv4.c +4 -1
@@ -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() */ }
@@ -1790,6 +1790,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 ; put_and_exit: newinet->inet_opt =NULL ;inet_csk_prepare_forced_close (newsk);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
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.
Quoted 3 lines
If
inet_csk_route_child_sock()fails in between, we go toput_and_exitandtcp_done()ends up intcp_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?
Quoted 28 lines
net/ipv4/tcp_cong.c +3
-0@@ -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 ;net/ipv4/tcp_ipv4.c +1
-1@@ -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() */ }
net/ipv4/tcp_ipv4.c struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, · line 1794
Quoted 6 lines
net/ipv4/tcp_ipv4.c +3
-0@@ -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 = 1If inet_csk_route_child_sock() then fails, clear_ca_and_exit sets icsk_ca_ops to NULL. tcp_done() -> tcp_v4_destroy_sock() -> tcp_cleanup_congestion_control() then returns early at the new NULL check.
Does this leak the module (or struct_ops map) reference on the new CA and skip its ->release()? Before this patch, that reference was put correctly on this path. Only the listener's reference, dropped by tcp_reinit_congestion_control(), was unbalanced.
The notes below the --- say the lsm_cgroup case is being fixed separately in the bpf tree. However, icsk_ca_setsockopt is inherited from the listener, so this error path cannot tell the two cases apart.
Would taking the reference in tcp_create_openreq_child(), as in the alternative diff in the notes, cover both cases?
Quoted 3 lines
net/ipv4/tcp_ipv4.c +0
-0put_and_exit: newinet->inet_opt =NULL ;inet_csk_prepare_forced_close (newsk);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124062625.264148-1-jiayuan.chen%40linux.dev
Quoted 68 lines · On 11/25/26 2:26 PM, [email protected] 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 toclear_ca_and_exitwhen…
- [Low] The commit message presents the bug as a module refcount underflow that shows up as amodule_put()WARNING.--
tcp: fix CA module refcount underflow in
tcp_v4_syn_recv_sock()The child socket inherits
icsk_ca_opsfrom the listener without taking a reference. Ifinet_csk_route_child_sock()fails, the child then puts the listener's reference. This patch clearsicsk_ca_opson that error path and makestcp_cleanup_congestion_control()skip a socket that has no CA.Quoted 3 lines
If
inet_csk_route_child_sock()fails in between, we go toput_and_exitandtcp_done()ends up intcp_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()usesatomic_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 oftcp_bbrsucceed while the listener'sicsk_ca_opsstill points into the module. Later children readicsk_ca_ops->ownerthroughbpf_try_module_get()intcp_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)intry_release_module_ref().For BPF
struct_opscongestion control,bpf_module_put()goes throughbpf_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); }
Quoted 6 lines
In that case the extra put drops a
struct_opsmap 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.
Quoted 60 lines
Quoted 28 lines
net/ipv4/tcp_cong.c +3
-0@@ -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 ;net/ipv4/tcp_ipv4.c +1
-1@@ -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() */ }net/ipv4/tcp_ipv4.c struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, · line 1794
Quoted 6 lines
net/ipv4/tcp_ipv4.c +3
-0@@ -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()callsinet_csk_clone_lock(). That function moves the child toTCP_SYN_RECVand then callssecurity_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_cloneis listed inbpf_lsm_locked_sockopt_hooksinkernel/bpf/bpf_lsm.c. ABPF_LSM_CGROUPprogram attached there can therefore callbpf_setsockopt(TCP_CONGESTION)on the child:
Quoted 8 lines
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:
'''
...
---
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.
...
'''