FAILED: patch "[PATCH] tcp: fix use-after-free in do_tcp_getsockopt(TCP_CONGESTION)" failed to apply to 5.15-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x 5271b79b7ad68dcb222e893773f92bdabf7750f3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026102127-crept-debate-2807@gregkh' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 5271b79b7ad68dcb222e893773f92bdabf7750f3 Mon Sep 17 00:00:00 2001
From: "Cen Zhang (Microsoft Security FORGE Labs)" <[email protected]>
Date: Thu, 27 Aug 2026 19:55:10 -0400
Subject: [PATCH] tcp: fix use-after-free in do_tcp_getsockopt(TCP_CONGESTION)
do_tcp_getsockopt() reads icsk->icsk_ca_ops->name without holding rcu_read_lock(). Since commit 0baf26b0fcd7 ("bpf: tcp: Support tcp_congestion_ops in bpf"), icsk_ca_ops can point to dynamically allocated BPF struct_ops memory that may be freed concurrently via setsockopt(TCP_CONGESTION), leading to a use-after-free.
BUG: KASAN: slab-use-after-free in _copy_to_user+0x37/0x60 Read of size 16 at addr ffff888013505260 by task exploit/149 _copy_to_user+0x37/0x60 do_tcp_getsockopt+0x158a/0x2460 (net/ipv4/tcp.c:4585) tcp_getsockopt+0x91/0xf0 __sys_getsockopt+0xf7/0x170
Fix this by holding rcu_read_lock() around the ca_ops->name access, using READ_ONCE() to load icsk_ca_ops, and copying the name to a stack buffer before releasing the lock. Also annotate the relevant icsk_ca_ops stores with WRITE_ONCE() to fix the accompanying KCSAN data-race issue.
net/ipv4/tcp.c +11 -3
@@ -4576,16 +4576,24 @@ int do_tcp_getsockopt(struct sock *sk, int level, val = !inet_csk_in_pingpong_mode (sk);break ; -case TCP_CONGESTION : +case TCP_CONGESTION : { +char ca_name[TCP_CA_NAME_MAX ] = {}; +if (copy_from_sockptr (&len, optlen,sizeof (int )))return -EFAULT ; len = min_t(unsigned int, len, TCP_CA_NAME_MAX);if (copy_to_sockptr (optlen, &len,sizeof (int )))return -EFAULT ; -if (copy_to_sockptr (optval, icsk->icsk_ca_ops->name, len)) + +rcu_read_lock (); +memcpy (ca_name,READ_ONCE (icsk->icsk_ca_ops)->name, +sizeof (ca_name)); +rcu_read_unlock (); + +if (copy_to_sockptr (optval, ca_name, len))return -EFAULT ;return 0 ; - + }case TCP_ULP :if (copy_from_sockptr (&len, optlen,sizeof (int )))return -EFAULT ;
net/ipv4/tcp_cong.c +2 -2
@@ -223,7 +223,7 @@ void tcp_assign_congestion_control(struct sock *sk) ca =rcu_dereference (net->ipv4.tcp_congestion_control);if (unlikely (!bpf_try_module_get (ca, ca->owner))) ca = &tcp_reno; - icsk->icsk_ca_ops = ca; +WRITE_ONCE (icsk->icsk_ca_ops, ca);rcu_read_unlock ();memset (icsk->icsk_ca_priv,0 ,sizeof (icsk->icsk_ca_priv));
@@ -253,7 +253,7 @@ static void tcp_reinit_congestion_control(struct sock *sk,struct inet_connection_sock *icsk =inet_csk (sk);tcp_cleanup_congestion_control (sk); - icsk->icsk_ca_ops = ca; +WRITE_ONCE (icsk->icsk_ca_ops, ca); icsk->icsk_ca_setsockopt =1 ;memset (icsk->icsk_ca_priv,0 ,sizeof (icsk->icsk_ca_priv));
net/ipv4/tcp_dctcp.c +1 -1
@@ -111,7 +111,7 @@ __bpf_kfunc static void dctcp_init(struct sock *sk)/* No ECN support? Fall back to Reno. Also need to clear * ECT from sk since it is set during 3WHS for DCTCP. */ -inet_csk (sk)->icsk_ca_ops = &dctcp_reno; +WRITE_ONCE (inet_csk (sk)->icsk_ca_ops, &dctcp_reno);INET_ECN_dontxmit (sk); }
net/ipv4/tcp_minisocks.c +1 -1
@@ -507,7 +507,7 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst) ca =tcp_ca_find_key (ca_key);if (likely (ca &&bpf_try_module_get (ca, ca->owner))) { icsk->icsk_ca_dst_locked =tcp_ca_dst_locked (dst); - icsk->icsk_ca_ops = ca; +WRITE_ONCE (icsk->icsk_ca_ops, ca); ca_got_dst = true; }rcu_read_unlock ();
net/ipv4/tcp_output.c +1 -1
@@ -4092,7 +4092,7 @@ static void tcp_ca_dst_init(struct sock *sk, const struct dst_entry *dst)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); - icsk->icsk_ca_ops = ca; +WRITE_ONCE (icsk->icsk_ca_ops, ca); }rcu_read_unlock (); }
Sasha Levin
maintainerPATCH 1/1 tcp: fix use-after-free in do_tcp_getsockopt(TCP_CONGESTION)
From: "Cen Zhang (Microsoft Security FORGE Labs)" <[email protected]>
[ Upstream commit 5271b79b7ad68dcb222e893773f92bdabf7750f3 ]
do_tcp_getsockopt() reads icsk->icsk_ca_ops->name without holding rcu_read_lock(). Since commit 0baf26b0fcd7 ("bpf: tcp: Support tcp_congestion_ops in bpf"), icsk_ca_ops can point to dynamically allocated BPF struct_ops memory that may be freed concurrently via setsockopt(TCP_CONGESTION), leading to a use-after-free.
BUG: KASAN: slab-use-after-free in _copy_to_user+0x37/0x60 Read of size 16 at addr ffff888013505260 by task exploit/149 _copy_to_user+0x37/0x60 do_tcp_getsockopt+0x158a/0x2460 (net/ipv4/tcp.c:4585) tcp_getsockopt+0x91/0xf0 __sys_getsockopt+0xf7/0x170
Fix this by holding rcu_read_lock() around the ca_ops->name access, using READ_ONCE() to load icsk_ca_ops, and copying the name to a stack buffer before releasing the lock. Also annotate the relevant icsk_ca_ops stores with WRITE_ONCE() to fix the accompanying KCSAN data-race issue.
--- net/ipv4/tcp.c | 14 +++++++++++--- net/ipv4/tcp_cong.c | 4 ++-- net/ipv4/tcp_dctcp.c | 2 +- net/ipv4/tcp_minisocks.c | 2 +- net/ipv4/tcp_output.c | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-)
net/ipv4/tcp.c +11 -3
@@ -4070,16 +4070,24 @@ static int do_tcp_getsockopt(struct sock *sk, int level, val = !inet_csk_in_pingpong_mode (sk);break ; -case TCP_CONGESTION : +case TCP_CONGESTION : { +char ca_name[TCP_CA_NAME_MAX ] = {}; +if (get_user (len, optlen))return -EFAULT ; len = min_t(unsigned int, len, TCP_CA_NAME_MAX);if (put_user (len, optlen))return -EFAULT ; -if (copy_to_user (optval, icsk->icsk_ca_ops->name, len)) + +rcu_read_lock (); +memcpy (ca_name,READ_ONCE (icsk->icsk_ca_ops)->name, +sizeof (ca_name)); +rcu_read_unlock (); + +if (copy_to_user (optval, ca_name, len))return -EFAULT ;return 0 ; - + }case TCP_ULP :if (get_user (len, optlen))return -EFAULT ;
net/ipv4/tcp_cong.c +2 -2
@@ -164,7 +164,7 @@ void tcp_assign_congestion_control(struct sock *sk) ca =rcu_dereference (net->ipv4.tcp_congestion_control);if (unlikely (!bpf_try_module_get (ca, ca->owner))) ca = &tcp_reno; - icsk->icsk_ca_ops = ca; +WRITE_ONCE (icsk->icsk_ca_ops, ca);rcu_read_unlock ();memset (icsk->icsk_ca_priv,0 ,sizeof (icsk->icsk_ca_priv));
@@ -194,7 +194,7 @@ static void tcp_reinit_congestion_control(struct sock *sk,struct inet_connection_sock *icsk =inet_csk (sk);tcp_cleanup_congestion_control (sk); - icsk->icsk_ca_ops = ca; +WRITE_ONCE (icsk->icsk_ca_ops, ca); icsk->icsk_ca_setsockopt =1 ;memset (icsk->icsk_ca_priv,0 ,sizeof (icsk->icsk_ca_priv));
net/ipv4/tcp_dctcp.c +1 -1
@@ -106,7 +106,7 @@ static void dctcp_init(struct sock *sk)/* No ECN support? Fall back to Reno. Also need to clear * ECT from sk since it is set during 3WHS for DCTCP. */ -inet_csk (sk)->icsk_ca_ops = &dctcp_reno; +WRITE_ONCE (inet_csk (sk)->icsk_ca_ops, &dctcp_reno);INET_ECN_dontxmit (sk); }
net/ipv4/tcp_minisocks.c +1 -1
@@ -416,7 +416,7 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst) ca =tcp_ca_find_key (ca_key);if (likely (ca &&bpf_try_module_get (ca, ca->owner))) { icsk->icsk_ca_dst_locked =tcp_ca_dst_locked (dst); - icsk->icsk_ca_ops = ca; +WRITE_ONCE (icsk->icsk_ca_ops, ca); ca_got_dst = true; }rcu_read_unlock ();
net/ipv4/tcp_output.c +1 -1
@@ -3680,7 +3680,7 @@ static void tcp_ca_dst_init(struct sock *sk, const struct dst_entry *dst)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); - icsk->icsk_ca_ops = ca; +WRITE_ONCE (icsk->icsk_ca_ops, ca); }rcu_read_unlock (); }
Signature · 1 line
2.53.0