All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering
@ 2026-11-23  0:20 Eva Kurchatova
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 1/4] ms/net: restore refcount ordering for lockless socket access Eva Kurchatova
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Eva Kurchatova @ 2026-11-23  0:20 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

Patchset for task https://virtuozzo.atlassian.net/browse/VSTOR-156045

Mainline fixes for sockets whose data another CPU can still read after
an ownership transfer, mostly with request migration, or after the TCP
handshake completes on another CPU.

The IPv4 part, e3f33b0a1d89 ("ipv4: free inet_opt and ireq_opt after an
RCU grace period"), is already in this tree from the RHEL import.

Patches 1, 3 and 4 are plain cherry-picks; patch 2 conflicted only in
context, and patch 4 has a rebase note for __reqsk_free(), which is
still inline here. Patch 1 uses the refcount acquire helpers that came
with "ms/refcount: provide ops for cases when object's memory can be
reused".

Tested in QEMU on branch-rh10-6.12.0-211.75.1.26.x.vz10-ovz with lockdep
and PROVE_RCU.

Eric Dumazet (2):
  ms/tcp: ipv6: do not read ireq->pktopts from tcp_v6_send_synack()
  ms/tcp: ipv6: free ireq->ipv6_opt after an RCU grace period

Hyunwoo Kim (1):
  ms/net: restore refcount ordering for lockless socket access

Yingjie Wang (1):
  ms/tcp: free saved_syn after an RCU grace period

 include/linux/tcp.h             |  9 ++++++---
 include/net/request_sock.h      |  5 +++--
 net/core/filter.c               |  4 ++--
 net/ipv4/inet_connection_sock.c | 10 +++++-----
 net/ipv4/inet_hashtables.c      |  2 +-
 net/ipv4/inet_timewait_sock.c   |  4 ++--
 net/ipv4/syncookies.c           |  2 +-
 net/ipv4/tcp_fastopen.c         |  2 +-
 net/ipv4/tcp_input.c            |  2 +-
 net/ipv6/inet6_hashtables.c     |  2 +-
 net/ipv6/tcp_ipv6.c             |  7 ++++---
 11 files changed, 27 insertions(+), 22 deletions(-)

-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Devel] [PATCH hci-8.2 1/4] ms/net: restore refcount ordering for lockless socket access
  2026-11-23  0:20 [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Eva Kurchatova
@ 2026-11-23  0:20 ` Eva Kurchatova
  2026-11-23  0:28   ` no-reply
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 2/4] ms/tcp: ipv6: do not read ireq->pktopts from tcp_v6_send_synack() Eva Kurchatova
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: Eva Kurchatova @ 2026-11-23  0:20 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Hyunwoo Kim <imv4bel@gmail.com>

[ Upstream commit 5b5d8f351dd96f56a58bb516a80e2d4d742fe4e2 ]

Before commit fb041bb7c0a9 ("locking/refcount: Consolidate implementations
of refcount_t"), default builds implemented refcount_inc_not_zero() using
atomic_add_unless(), which provided full memory ordering on success. The
commit removed that ordering on weakly ordered architectures, allowing
accesses after a successful reference acquisition to be reordered before
it.

Use refcount_inc_not_zero_acquire() in the IPv4 and IPv6 established
lookups and in inet_twsk_purge(), where the socket is validated again
after taking a reference.

bpf_sk_assign() can receive a socket pointer obtained through BTF
without a reference. Use acquire ordering when it takes its own reference
for a refcounted socket.

Use refcount_set_release() when setting refcounts in the SYN cookie,
TCP Fast Open, request migration, and TIME_WAIT paths. Keep the existing
TIME_WAIT smp_wmb(), which separately orders the refcount store before
ehash publication.

Fixes: fb041bb7c0a9 ("locking/refcount: Consolidate implementations of refcount_t")
Fixes: 27e5203bd9c5 ("bpf: Change bpf_sk_assign to accept ARG_PTR_TO_BTF_ID_SOCK_COMMON")
Cc: stable@vger.kernel.org
Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://patch.msgid.link/20261029214219.1035176-3-imv4bel@gmail.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
(cherry picked from commit 6f4fc93f76cd8ebd3c89a064573715503048d1ad)

https://virtuozzo.atlassian.net/browse/VSTOR-156045
Feature: fix ms/net

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 net/core/filter.c               | 2 +-
 net/ipv4/inet_connection_sock.c | 6 +++---
 net/ipv4/inet_hashtables.c      | 2 +-
 net/ipv4/inet_timewait_sock.c   | 4 ++--
 net/ipv4/syncookies.c           | 2 +-
 net/ipv4/tcp_fastopen.c         | 2 +-
 net/ipv6/inet6_hashtables.c     | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index b976eaabedd2..3b0ca58da7b5 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -7564,7 +7564,7 @@ BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
 	if (sk_unhashed(sk))
 		return -EOPNOTSUPP;
 	if (sk_is_refcounted(sk) &&
-	    unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
+	    unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
 		return -ENOENT;
 
 	skb_orphan(skb);
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 9736f35e4e97..254646530dc8 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -1070,8 +1070,8 @@ static void reqsk_timer_handler(struct timer_list *t)
 		 * hold another count to prevent use-after-free and
 		 * call reqsk_put() just before return.
 		 */
-		refcount_set(&nreq->rsk_refcnt, 2 + 1);
 		timer_setup(&nreq->rsk_timer, reqsk_timer_handler, TIMER_PINNED);
+		refcount_set_release(&nreq->rsk_refcnt, 2 + 1);
 		reqsk_queue_migrated(&inet_csk(nsk)->icsk_accept_queue, req);
 
 		req = nreq;
@@ -1411,7 +1411,7 @@ struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
 				goto child_put;
 			}
 
-			refcount_set(&nreq->rsk_refcnt, 1);
+			refcount_set_release(&nreq->rsk_refcnt, 1);
 			if (inet_csk_reqsk_queue_add(sk, nreq, child)) {
 				__NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMIGRATEREQSUCCESS);
 				reqsk_migrate_reset(req);
@@ -1465,7 +1465,7 @@ void inet_csk_listen_stop(struct sock *sk)
 		if (nsk) {
 			nreq = inet_reqsk_clone(req, nsk);
 			if (nreq) {
-				refcount_set(&nreq->rsk_refcnt, 1);
+				refcount_set_release(&nreq->rsk_refcnt, 1);
 
 				rcu_read_lock();
 				if (inet_csk_reqsk_queue_add(nsk, nreq, child)) {
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
index 3fab3198d1f5..8cf8c9091498 100644
--- a/net/ipv4/inet_hashtables.c
+++ b/net/ipv4/inet_hashtables.c
@@ -510,7 +510,7 @@ struct sock *__inet_lookup_established(const struct net *net,
 		if (sk->sk_hash != hash)
 			continue;
 		if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) {
-			if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
+			if (unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
 				goto out;
 			if (unlikely(!inet_match(net, sk, acookie,
 						 ports, dif, sdif))) {
diff --git a/net/ipv4/inet_timewait_sock.c b/net/ipv4/inet_timewait_sock.c
index cf84a3c7de14..943693040a8c 100644
--- a/net/ipv4/inet_timewait_sock.c
+++ b/net/ipv4/inet_timewait_sock.c
@@ -139,7 +139,7 @@ void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw,
 	 * Also note that after this point, we lost our implicit reference
 	 * so we are not allowed to use tw anymore.
 	 */
-	refcount_set(&tw->tw_refcnt, 3);
+	refcount_set_release(&tw->tw_refcnt, 3);
 
 	/* Ensure tw_refcnt has been set before tw is published.
 	 * smp_wmb() provides the necessary memory barrier to enforce this
@@ -324,7 +324,7 @@ void inet_twsk_purge(struct inet_hashinfo *hashinfo)
 			if (refcount_read(&sock_net(sk)->ns.count))
 				continue;
 
-			if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
+			if (unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
 				continue;
 
 			if (refcount_read(&sock_net(sk)->ns.count)) {
diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c
index 3976c821ecdf..80e49ab428e1 100644
--- a/net/ipv4/syncookies.c
+++ b/net/ipv4/syncookies.c
@@ -204,7 +204,7 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
 	child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
 						 NULL, &own_req, NULL);
 	if (child) {
-		refcount_set(&req->rsk_refcnt, 1);
+		refcount_set_release(&req->rsk_refcnt, 1);
 		sock_rps_save_rxhash(child, skb);
 
 		if (rsk_drop_req(req)) {
diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c
index 9c1173f1bddd..9764f72aeaf0 100644
--- a/net/ipv4/tcp_fastopen.c
+++ b/net/ipv4/tcp_fastopen.c
@@ -278,7 +278,7 @@ static struct sock *tcp_fastopen_create_child(struct sock *sk,
 	tcp_reset_xmit_timer(child, ICSK_TIME_RETRANS,
 			     req->timeout, false);
 
-	refcount_set(&req->rsk_refcnt, 2);
+	refcount_set_release(&req->rsk_refcnt, 2);
 
 	sk_mark_napi_id_set(child, skb);
 
diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c
index 9ec05e354baa..a53a45cf3643 100644
--- a/net/ipv6/inet6_hashtables.c
+++ b/net/ipv6/inet6_hashtables.c
@@ -71,7 +71,7 @@ struct sock *__inet6_lookup_established(const struct net *net,
 			continue;
 		if (!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
 			continue;
-		if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
+		if (unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
 			goto out;
 
 		if (unlikely(!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))) {
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Devel] [PATCH hci-8.2 2/4] ms/tcp: ipv6: do not read ireq->pktopts from tcp_v6_send_synack()
  2026-11-23  0:20 [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Eva Kurchatova
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 1/4] ms/net: restore refcount ordering for lockless socket access Eva Kurchatova
@ 2026-11-23  0:20 ` Eva Kurchatova
  2026-11-23  0:28   ` no-reply
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 3/4] ms/tcp: ipv6: free ireq->ipv6_opt after an RCU grace period Eva Kurchatova
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: Eva Kurchatova @ 2026-11-23  0:20 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Eric Dumazet <edumazet@kernel.org>

When IPV6_FL_F_REFLECT is set on a listener, or the
net.ipv6.flowlabel_reflect sysctl has FLOWLABEL_REFLECT_ESTABLISHED,
tcp_v6_send_synack() reflects the flowlabel of the SYN packet,
reading it from the skb stored in ireq->pktopts.

Since commit 079096f103fa ("tcp/dccp: install syn_recv requests
into ehash table"), request sockets are processed without holding
the listener lock. A cpu can then process a retransmitted SYN for a
request socket, and send a SYNACK from tcp_check_req(), while another
cpu receives the ACK completing the 3WHS. tcp_v6_syn_recv_sock()
consumes ireq->pktopts when it wins the hashdance, and the first cpu
reads a freed skb:

  BUG: KASAN: slab-use-after-free in tcp_v6_send_synack (include/linux/skbuff.h:3152 include/linux/ipv6.h:110 net/ipv6/tcp_ipv6.c:582)
  Read of size 8 at addr ffff88802b08b608 by task poc-ipv6-pktopt/5076

  Call Trace:
   <IRQ>
   tcp_v6_send_synack (include/linux/skbuff.h:3152 include/linux/ipv6.h:110 net/ipv6/tcp_ipv6.c:582)
   tcp_rtx_synack (net/ipv4/tcp_output.c:4719)
   tcp_check_req (net/ipv4/tcp_minisocks.c:762)
   tcp_v6_rcv (net/ipv6/tcp_ipv6.c:1826)
   ip6_protocol_deliver_rcu (net/ipv6/ip6_input.c:479)
   ip6_input_finish (net/ipv6/ip6_input.c:534)
   ip6_input (include/linux/netfilter.h:325 include/linux/netfilter.h:319 net/ipv6/ip6_input.c:545)
   ipv6_rcv (include/net/dst.h:481 net/ipv6/ip6_input.c:119 include/linux/netfilter.h:325 include/linux/netfilter.h:319 net/ipv6/ip6_input.c:351)

  Freed by task 5074 on cpu 0 at 15.412249s:
   kmem_cache_free (include/linux/kasan.h:235 mm/slub.c:2780 mm/slub.c:6630 mm/slub.c:6757)
   tcp_v6_syn_recv_sock (net/ipv6/tcp_ipv6.c:1528)
   tcp_check_req (net/ipv4/tcp_minisocks.c:934)
   tcp_v6_rcv (net/ipv6/tcp_ipv6.c:1826)
   ip6_protocol_deliver_rcu (net/ipv6/ip6_input.c:479)
   ip6_input_finish (net/ipv6/ip6_input.c:534)

Request socket migration has the same issue: reqsk_migrate_reset()
transfers ireq->pktopts to the clone, which can be freed while
another cpu holding a reference on the old request socket is still
sending a SYNACK.

The SYNACK timer itself is not an issue, because
inet_csk_reqsk_queue_drop() calls timer_delete_sync().

Save the flowlabel of the SYN in the request socket instead, so that
tcp_v6_send_synack() no longer needs ireq->pktopts.

As a side effect, the flowlabel is also reflected for SYN packets
reusing a TIME_WAIT socket, as ireq->pktopts is not set in this case.

Fixes: e994b2f0fb92 ("tcp: do not lock listener to process SYN packets")
Fixes: 079096f103fa ("tcp/dccp: install syn_recv requests into ehash table")
Fixes: c905dee62232 ("tcp: Migrate TCP_NEW_SYN_RECV requests at retransmitting SYN+ACKs.")
Reported-by: Anvik Loitongbam <permissiontogallop@gmail.com>
Reported-by: Yingjie Wang <x-professor@outlook.com>
Closes: https://lore.kernel.org/netdev/82c31e4d5043f3dbd11b44177cfba779d335aae3.1793599929.git.x-professor@outlook.com/
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Reviewed-by: Neal Cardwell <ncardwell@google.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://patch.msgid.link/20261102075441.3771550-1-edumazet@kernel.org
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
(cherry picked from commit 8e069fd3fd396b89af417e50b359bc9892dd41cd)

https://virtuozzo.atlassian.net/browse/VSTOR-156045
Feature: fix ms/net

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 include/linux/tcp.h | 3 +++
 net/ipv6/tcp_ipv6.c | 5 +++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/include/linux/tcp.h b/include/linux/tcp.h
index 6a5e08b937b3..c52d9348ef47 100644
--- a/include/linux/tcp.h
+++ b/include/linux/tcp.h
@@ -171,6 +171,9 @@ struct tcp_request_sock {
 	u8				ao_rcv_next;
 	bool				used_tcp_ao;
 #endif
+#if IS_ENABLED(CONFIG_IPV6)
+	__be32				syn_flowlabel;
+#endif
 };
 
 static inline struct tcp_request_sock *tcp_rsk(const struct request_sock *req)
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index d572bec5cc51..aec76c54327c 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -546,8 +546,8 @@ static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,
 				    &ireq->ir_v6_rmt_addr);
 
 		fl6->daddr = ireq->ir_v6_rmt_addr;
-		if (inet6_test_bit(REPFLOW, sk) && ireq->pktopts)
-			fl6->flowlabel = ip6_flowlabel(ipv6_hdr(ireq->pktopts));
+		if (inet6_test_bit(REPFLOW, sk))
+			fl6->flowlabel = tcp_rsk(req)->syn_flowlabel;
 
 		tclass = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reflect_tos) ?
 				(tcp_rsk(req)->syn_tos & ~INET_ECN_MASK) |
@@ -798,6 +798,7 @@ static void tcp_v6_init_req(struct request_sock *req,
 
 	ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
 	ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
+	tcp_rsk(req)->syn_flowlabel = ip6_flowlabel(ipv6_hdr(skb));
 
 	/* So that link locals have meaning */
 	if ((!sk_listener->sk_bound_dev_if || l3_slave) &&
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Devel] [PATCH hci-8.2 3/4] ms/tcp: ipv6: free ireq->ipv6_opt after an RCU grace period
  2026-11-23  0:20 [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Eva Kurchatova
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 1/4] ms/net: restore refcount ordering for lockless socket access Eva Kurchatova
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 2/4] ms/tcp: ipv6: do not read ireq->pktopts from tcp_v6_send_synack() Eva Kurchatova
@ 2026-11-23  0:20 ` Eva Kurchatova
  2026-11-23  0:30   ` no-reply
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 4/4] ms/tcp: free saved_syn " Eva Kurchatova
  2026-11-23  8:44 ` [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Konstantin Khorenko
  4 siblings, 1 reply; 10+ messages in thread
From: Eva Kurchatova @ 2026-11-23  0:20 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Eric Dumazet <edumazet@kernel.org>

ireq->ipv6_opt is only set by calipso_req_setattr(), when a CALIPSO
NetLabel mapping is configured.

Request socket migration (net.ipv4.tcp_migrate_req=1, or a
BPF_SK_REUSEPORT_SELECT_OR_MIGRATE program) clones the request
socket with inet_reqsk_clone(), so that the old request socket and
its clone share the same ipv6_opt, then reqsk_migrate_reset() clears
the pointer in the old one.

Another cpu holding a reference on the old request socket can still
be using these options, sending a SYNACK from tcp_v6_send_synack()
or creating a child in tcp_v6_syn_recv_sock(), when the clone
is freed. tcp_v6_reqsk_destructor() frees ipv6_opt with a plain
kfree(), leading to a use-after-free.

Both readers already run under RCU, like their IPv4 counterparts.
Use kfree_rcu() in tcp_v6_reqsk_destructor(), as commit e3f33b0a1d89
("ipv4: free inet_opt and ireq_opt after an RCU grace period") did
for ireq_opt.

Fixes: c905dee62232 ("tcp: Migrate TCP_NEW_SYN_RECV requests at retransmitting SYN+ACKs.")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/netdev/179369559062.488856.15441115725436277776@kernel.org/
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
Reviewed-by: Hangbin Liu <liuhangbin@kylinos.cn>
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Tested-by: Linkui Xiao <xiaolinkui@kylinos.cn>
Link: https://patch.msgid.link/20261103090011.4128420-1-edumazet@kernel.org
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
(cherry picked from commit fafbc4051123d5459ea6950899cec08ed946e278)

https://virtuozzo.atlassian.net/browse/VSTOR-156045
Feature: fix ms/net

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 net/ipv6/tcp_ipv6.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index aec76c54327c..c0dbcab756f3 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -575,7 +575,7 @@ static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,
 
 static void tcp_v6_reqsk_destructor(struct request_sock *req)
 {
-	kfree(inet_rsk(req)->ipv6_opt);
+	kfree_rcu(inet_rsk(req)->ipv6_opt, rcu);
 	consume_skb(inet_rsk(req)->pktopts);
 }
 
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Devel] [PATCH hci-8.2 4/4] ms/tcp: free saved_syn after an RCU grace period
  2026-11-23  0:20 [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Eva Kurchatova
                   ` (2 preceding siblings ...)
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 3/4] ms/tcp: ipv6: free ireq->ipv6_opt after an RCU grace period Eva Kurchatova
@ 2026-11-23  0:20 ` Eva Kurchatova
  2026-11-23  0:30   ` no-reply
  2026-11-23  8:44 ` [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Konstantin Khorenko
  4 siblings, 1 reply; 10+ messages in thread
From: Eva Kurchatova @ 2026-11-23  0:20 UTC (permalink / raw)
  To: khorenko, vladimir.riabchun; +Cc: devel

From: Yingjie Wang <x-professor@outlook.com>

tcp_reqsk_record_syn() allocates saved_syn separately from its request
socket. bpf_sock_ops_get_syn() can load that pointer while processing a
retransmitted SYN under RCU.

There are two ownership transfers which can let another CPU reclaim the
allocation while such a reader is still using it.

First, tcp_move_syn() transfers req->saved_syn to a newly created child
socket. The child can be closed and tcp_saved_syn_free() can reclaim the
allocation before a reader of the old request finishes.

Second, inet_reqsk_clone() shallow-copies request_sock during request
migration. After reqsk_migrate_reset() clears the old request pointer, the
migrated clone owns it. However, a reader which already loaded the pointer
from the old request can still be using it when the clone is released.

I found this through LLM-assisted concurrency fuzzing.

Mark the request-socket saved_syn pointer as RCU protected and use RCU
accessors for its reads and updates. Keep the full-socket pointer plain
because all of its accesses hold the socket lock. Embed an rcu_head in
struct saved_syn and use kfree_rcu() in both teardown paths. This lets a
request-side reader which loaded the object before an ownership transfer
finish.

This follows commit e3f33b0a1d89 ("ipv4: free inet_opt and ireq_opt
after an RCU grace period"), which fixed the same ownership-transfer
pattern for IPv4 options.

The issue was confirmed with KASAN on v7.3-16007-g36aad21d50d1 using
temporary kernel instrumentation to widen the reader and request-migration
window. KASAN reported a slab-use-after-free read in
bpf_sock_ops_get_syn(), with the allocation freed by __reqsk_free() from
reqsk_timer_handler(). The same explicit migration test completed without
a report with deferred reclamation. A patched kernel without the delay
instrumentation also completed a successful migration and seven saved-SYN
reads without a KASAN report.

Fixes: 0813a841566f ("bpf: tcp: Allow bpf prog to write and parse TCP header option")
Fixes: c905dee62232 ("tcp: Migrate TCP_NEW_SYN_RECV requests at retransmitting SYN+ACKs.")
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Eric Dumazet <edumazet@kernel.org>
Link: https://patch.msgid.link/38fa9f2cc476502e9b1c1fef09ebba1df5dd5901.1793795528.git.x-professor@outlook.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
(cherry picked from commit 2b9fd1a0abf0c1c7200be7a33cfeeca1c68089b6)

Rebase note: __reqsk_free() is still inline in include/net/request_sock.h
here, so the kfree_rcu() goes there. Mainline moved it to
inet_connection_sock.c with 22c1264415ef ("tcp: move __reqsk_free() out
of line").

https://virtuozzo.atlassian.net/browse/VSTOR-156045
Feature: fix ms/net

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 include/linux/tcp.h             | 6 +++---
 include/net/request_sock.h      | 5 +++--
 net/core/filter.c               | 2 +-
 net/ipv4/inet_connection_sock.c | 4 ++--
 net/ipv4/tcp_input.c            | 2 +-
 5 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/include/linux/tcp.h b/include/linux/tcp.h
index c52d9348ef47..5565cb4d0e31 100644
--- a/include/linux/tcp.h
+++ b/include/linux/tcp.h
@@ -578,13 +578,13 @@ static inline void fastopen_queue_tune(struct sock *sk, int backlog)
 static inline void tcp_move_syn(struct tcp_sock *tp,
 				struct request_sock *req)
 {
-	tp->saved_syn = req->saved_syn;
-	req->saved_syn = NULL;
+	tp->saved_syn = unrcu_pointer(req->saved_syn);
+	RCU_INIT_POINTER(req->saved_syn, NULL);
 }
 
 static inline void tcp_saved_syn_free(struct tcp_sock *tp)
 {
-	kfree(tp->saved_syn);
+	kfree_rcu(tp->saved_syn, rcu);
 	tp->saved_syn = NULL;
 }
 
diff --git a/include/net/request_sock.h b/include/net/request_sock.h
index b07b1cd14e9f..27c7cbe1a5fb 100644
--- a/include/net/request_sock.h
+++ b/include/net/request_sock.h
@@ -44,6 +44,7 @@ struct request_sock_ops {
 int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req);
 
 struct saved_syn {
+	struct rcu_head rcu;
 	u32 mac_hdrlen;
 	u32 network_hdrlen;
 	u32 tcp_hdrlen;
@@ -73,7 +74,7 @@ struct request_sock {
 	struct timer_list		rsk_timer;
 	const struct request_sock_ops	*rsk_ops;
 	struct sock			*sk;
-	struct saved_syn		*saved_syn;
+	struct saved_syn __rcu		*saved_syn;
 	u32				secid;
 	u32				peer_secid;
 	u32				timeout;
@@ -133,7 +134,7 @@ static inline void __reqsk_free(struct request_sock *req)
 	req->rsk_ops->destructor(req);
 	if (req->rsk_listener)
 		sock_put(req->rsk_listener);
-	kfree(req->saved_syn);
+	kfree_rcu(unrcu_pointer(req->saved_syn), rcu);
 	kmem_cache_free(req->rsk_ops->slab, req);
 }
 
diff --git a/net/core/filter.c b/net/core/filter.c
index 3b0ca58da7b5..e9fe0b118985 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -5740,7 +5740,7 @@ static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock,
 			 * not be available.  It has to resort to
 			 * saved_syn (if it is saved).
 			 */
-			saved_syn = inet_reqsk(sk)->saved_syn;
+			saved_syn = rcu_dereference(inet_reqsk(sk)->saved_syn);
 		else
 			saved_syn = tcp_sk(sk)->saved_syn;
 
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 254646530dc8..ef0fe6492277 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -895,7 +895,7 @@ reqsk_alloc_noprof(const struct request_sock_ops *ops, struct sock *sk_listener,
 	req_to_sk(req)->sk_prot = sk_listener->sk_prot;
 	sk_node_init(&req_to_sk(req)->sk_node);
 	sk_tx_queue_clear(req_to_sk(req));
-	req->saved_syn = NULL;
+	RCU_INIT_POINTER(req->saved_syn, NULL);
 	req->syncookie = 0;
 	req->timeout = 0;
 	req->num_timeout = 0;
@@ -987,7 +987,7 @@ static void reqsk_queue_migrated(struct request_sock_queue *queue,
 
 static void reqsk_migrate_reset(struct request_sock *req)
 {
-	req->saved_syn = NULL;
+	RCU_INIT_POINTER(req->saved_syn, NULL);
 #if IS_ENABLED(CONFIG_IPV6)
 	inet_rsk(req)->ipv6_opt = NULL;
 	inet_rsk(req)->pktopts = NULL;
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 1b2b62a52408..8388af69720e 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -7175,7 +7175,7 @@ static void tcp_reqsk_record_syn(const struct sock *sk,
 			saved_syn->network_hdrlen = skb_network_header_len(skb);
 			saved_syn->tcp_hdrlen = tcp_hdrlen(skb);
 			memcpy(saved_syn->data, base, len);
-			req->saved_syn = saved_syn;
+			RCU_INIT_POINTER(req->saved_syn, saved_syn);
 		}
 	}
 }
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Devel] [PATCH hci-8.2 1/4] ms/net: restore refcount ordering for lockless socket access
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 1/4] ms/net: restore refcount ordering for lockless socket access Eva Kurchatova
@ 2026-11-23  0:28   ` no-reply
  0 siblings, 0 replies; 10+ messages in thread
From: no-reply @ 2026-11-23  0:28 UTC (permalink / raw)
  To: Eva Kurchatova, khorenko, vladimir.riabchun; +Cc: devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] net: reqsk_timer_handler() reorder of timer_setup() not in
  changelog
--

commit 67c2c106507ceb435fa30a63e1e498fd83a8f047
Author: Hyunwoo Kim <imv4bel@gmail.com>

ms/net: restore refcount ordering for lockless socket access

This commit switches the IPv4/IPv6 established lookups, inet_twsk_purge() and bpf_sk_assign() to refcount_inc_not_zero_acquire(). It switches the SYN cookie, TCP Fast Open, request migration and TIME_WAIT paths to refcount_set_release().

Link: https://patch.msgid.link/20261029214219.1035176-3-imv4bel@gmail.com

> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
> index 9736f35e4e978..254646530dc8a 100644
> --- a/net/ipv4/inet_connection_sock.c
> +++ b/net/ipv4/inet_connection_sock.c
> @@ -1070,8 +1070,8 @@ static void reqsk_timer_handler(struct timer_list *t)
>  		 * hold another count to prevent use-after-free and
>  		 * call reqsk_put() just before return.
>  		 */
> -		refcount_set(&nreq->rsk_refcnt, 2 + 1);
>  		timer_setup(&nreq->rsk_timer, reqsk_timer_handler, TIMER_PINNED);
> +		refcount_set_release(&nreq->rsk_refcnt, 2 + 1);
>  		reqsk_queue_migrated(&inet_csk(nsk)->icsk_accept_queue, req);

[Severity: Low]
Is the commit message complete here? This hunk swaps timer_setup() and the refcount store, but the changelog only says refcount_set_release() is used in the request migration path.

Presumably the reorder makes the timer initialisation visible before the release store that publishes the refcount. A sentence saying so would help reviewers.

Carry the trailer below into the next revision and Sashiko will not review this patch again.

Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123002500.1652289-1-eva.kurchatova@virtuozzo.com?part=1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Devel] [PATCH hci-8.2 2/4] ms/tcp: ipv6: do not read ireq->pktopts from tcp_v6_send_synack()
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 2/4] ms/tcp: ipv6: do not read ireq->pktopts from tcp_v6_send_synack() Eva Kurchatova
@ 2026-11-23  0:28   ` no-reply
  0 siblings, 0 replies; 10+ messages in thread
From: no-reply @ 2026-11-23  0:28 UTC (permalink / raw)
  To: Eva Kurchatova, khorenko, vladimir.riabchun; +Cc: devel

> From: Eric Dumazet <edumazet@kernel.org>
> 
> When IPV6_FL_F_REFLECT is set on a listener, or the
> net.ipv6.flowlabel_reflect sysctl has FLOWLABEL_REFLECT_ESTABLISHED,
> tcp_v6_send_synack() reflects the flowlabel of the SYN packet,
> reading it from the skb stored in ireq->pktopts.
> 
> Since commit 079096f103fa ("tcp/dccp: install syn_recv requests
> into ehash table"), request sockets are processed without holding
> the listener lock. A cpu can then process a retransmitted SYN for a
> request socket, and send a SYNACK from tcp_check_req(), while another
> cpu receives the ACK completing the 3WHS. tcp_v6_syn_recv_sock()
> consumes ireq->pktopts when it wins the hashdance, and the first cpu
> reads a freed skb:
> 
> [ ... ]
> 
> https://virtuozzo.atlassian.net/browse/VSTOR-156045
> Feature: fix ms/net
> 
> Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>

Sashiko has reviewed this patch and found no issues.

Carry the trailer below into the next revision and Sashiko will not review this patch again.

Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123002500.1652289-1-eva.kurchatova@virtuozzo.com?part=2


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Devel] [PATCH hci-8.2 4/4] ms/tcp: free saved_syn after an RCU grace period
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 4/4] ms/tcp: free saved_syn " Eva Kurchatova
@ 2026-11-23  0:30   ` no-reply
  0 siblings, 0 replies; 10+ messages in thread
From: no-reply @ 2026-11-23  0:30 UTC (permalink / raw)
  To: Eva Kurchatova, khorenko, vladimir.riabchun; +Cc: devel

> From: Yingjie Wang <x-professor@outlook.com>
> 
> tcp_reqsk_record_syn() allocates saved_syn separately from its request
> socket. bpf_sock_ops_get_syn() can load that pointer while processing a
> retransmitted SYN under RCU.
> 
> There are two ownership transfers which can let another CPU reclaim the
> allocation while such a reader is still using it.
> 
> First, tcp_move_syn() transfers req->saved_syn to a newly created child
> socket. The child can be closed and tcp_saved_syn_free() can reclaim the
> allocation before a reader of the old request finishes.
> 
> Second, inet_reqsk_clone() shallow-copies request_sock during request
> migration. After reqsk_migrate_reset() clears the old request pointer, the
> [ ... ]
> 
> https://virtuozzo.atlassian.net/browse/VSTOR-156045
> Feature: fix ms/net
> 
> Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>

Sashiko has reviewed this patch and found no issues.

Carry the trailer below into the next revision and Sashiko will not review this patch again.

Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123002500.1652289-1-eva.kurchatova@virtuozzo.com?part=4


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Devel] [PATCH hci-8.2 3/4] ms/tcp: ipv6: free ireq->ipv6_opt after an RCU grace period
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 3/4] ms/tcp: ipv6: free ireq->ipv6_opt after an RCU grace period Eva Kurchatova
@ 2026-11-23  0:30   ` no-reply
  0 siblings, 0 replies; 10+ messages in thread
From: no-reply @ 2026-11-23  0:30 UTC (permalink / raw)
  To: Eva Kurchatova, khorenko, vladimir.riabchun; +Cc: devel

> From: Eric Dumazet <edumazet@kernel.org>
> 
> ireq->ipv6_opt is only set by calipso_req_setattr(), when a CALIPSO
> NetLabel mapping is configured.
> 
> Request socket migration (net.ipv4.tcp_migrate_req=1, or a
> BPF_SK_REUSEPORT_SELECT_OR_MIGRATE program) clones the request
> socket with inet_reqsk_clone(), so that the old request socket and
> its clone share the same ipv6_opt, then reqsk_migrate_reset() clears
> the pointer in the old one.
> 
> Another cpu holding a reference on the old request socket can still
> be using these options, sending a SYNACK from tcp_v6_send_synack()
> or creating a child in tcp_v6_syn_recv_sock(), when the clone
> is freed. tcp_v6_reqsk_destructor() frees ipv6_opt with a plain
> [ ... ]
> 
> https://virtuozzo.atlassian.net/browse/VSTOR-156045
> Feature: fix ms/net
> 
> Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>

Sashiko has reviewed this patch and found no issues.

Carry the trailer below into the next revision and Sashiko will not review this patch again.

Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123002500.1652289-1-eva.kurchatova@virtuozzo.com?part=3


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering
  2026-11-23  0:20 [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Eva Kurchatova
                   ` (3 preceding siblings ...)
  2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 4/4] ms/tcp: free saved_syn " Eva Kurchatova
@ 2026-11-23  8:44 ` Konstantin Khorenko
  4 siblings, 0 replies; 10+ messages in thread
From: Konstantin Khorenko @ 2026-11-23  8:44 UTC (permalink / raw)
  To: Eva Kurchatova; +Cc: devel

I won't apply this version, please send v2 with the changes below.

1. Add 4e2b3ef19e0e0 ("inet: reject unhashed sockets during
   established lookups") before 1/4.
2. Add a9782a6f2bbd0 ("tcp: Pass req->rsk_listener to
   inet_csk_reqsk_queue_drop() in tcp_check_req().") and 62754aad50d0b
   ("tcp: fix request_sock use-after-free during Fast Open accept()").
3. Cover letter: patch 4 is not a plain cherry-pick, say that 1/4 is
   taken from stable, and list the new patches.

The four patches themselves match mainline hunk for hunk, and the
rebase note of 4/4 is right.

On Mon, 23 Nov 2026 02:20:21 +0200, Eva Kurchatova wrote:
> Mainline fixes for sockets whose data another CPU can still read after
> an ownership transfer, mostly with request migration, or after the TCP
> handshake completes on another CPU.

Item 1: 1/4 is patch 2/2 of the upstream series "inet: fix lockless
ehash walk races", merged together with 4e2b3ef19e0e0 as 7b1fd973214de.
That one is not in hci-8.2: the established lookups take the reference
and recheck the tuple, but never check that the socket is still
hashed. A lookup that loaded the address of a freed socket can return
a SYN cookie request that reused the slab object, unhashed and with a
NULL rsk_listener, and tcp_v4_rcv() then oopses on the listener. Its
changelog has the reproduced crash, it is Cc stable, and it also
replaces the __reqsk_free() calls after the refcount store with
reqsk_put(). It applies cleanly before 1/4.

Item 2: both fix commits that are in this tree, d4f2c86b2b7e and
e3d95ad7da4f, and both are request_sock lifetime fixes of the same
kind. After a migration tcp_check_req() still passes the new listener,
not req->rsk_listener, to inet_csk_reqsk_queue_drop()
(tcp_minisocks.c:907), so the new listener's qlen underflows and it
stops answering SYNs.

> Patches 1, 3 and 4 are plain cherry-picks; patch 2 conflicted only in
> context, and patch 4 has a rebase note for __reqsk_free(), which is

Item 3: the sentence calls patch 4 a plain cherry-pick and gives it a
rebase note at once. 1/4 also carries "[ Upstream commit ]" and Sasha
Levin's Signed-off-by from stable 6f4fc93f76cd8, unlike the others.

Sashiko's remark on 1/4 needs no change: the timer_setup() reorder is
in mainline 5b5d8f351dd96 itself, and it is right, the timer of the
clone must be set up before the release store makes it reachable.

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-11-23  8:45 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  0:20 [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Eva Kurchatova
2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 1/4] ms/net: restore refcount ordering for lockless socket access Eva Kurchatova
2026-11-23  0:28   ` no-reply
2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 2/4] ms/tcp: ipv6: do not read ireq->pktopts from tcp_v6_send_synack() Eva Kurchatova
2026-11-23  0:28   ` no-reply
2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 3/4] ms/tcp: ipv6: free ireq->ipv6_opt after an RCU grace period Eva Kurchatova
2026-11-23  0:30   ` no-reply
2026-11-23  0:20 ` [Devel] [PATCH hci-8.2 4/4] ms/tcp: free saved_syn " Eva Kurchatova
2026-11-23  0:30   ` no-reply
2026-11-23  8:44 ` [Devel] [PATCH hci-8.2 0/4] ms/net: backport request migration RCU fixes and lockless refcount ordering Konstantin Khorenko

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.