Skip to contentSkip to conversation

net/tcp-ao: fix use-after-free of key in del_async path

PATCH Applied HanQuan 3 participants 3 messages Jun 23 – Jun 25

Subsystems (3)
lore
Share & tools
lore

HanQuan

author
#0 lore raw

PATCH 1/1 net/tcp-ao: fix use-after-free of key in del_async path

In tcp_ao_delete_key(), the del_async path skips the current_key and rnext_key validity checks present in the synchronous path, assuming these pointers are always NULL on LISTEN sockets. However, if a key was added with set_current=1/set_rnext=1 while the socket was in CLOSE state, current_key and rnext_key will be non-NULL after listen() transitions the socket to LISTEN.

When such a key is deleted with del_async=1, hlist_del_rcu() and call_rcu() free the key without clearing the dangling pointers. After the RCU grace period, getsockopt(TCP_AO_INFO) dereferences current_key->sndid and rnext_key->rcvid from freed slab memory.

Clear current_key and rnext_key in the del_async path when they reference the key being deleted.

Fixes d6732b95b6fb ("net/tcp: Allow asynchronous delete for TCP-AO keys (MKTs)")
---
 net/ipv4/tcp_ao.c | 4 ++++
 1 file changed, 4 insertions(+)
net/ipv4/tcp_ao.c +4 -0
index 2f69bcecae78..a56bb79e15e0 100644
@@ -1747,6 +1747,10 @@ static int tcp_ao_delete_key(struct sock *sk, struct tcp_ao_info *ao_info,
 	 * them and we can just free all resources in RCU fashion.
 	 */
 	if (del_async) {
+		if (ao_info->current_key == key)
+			WRITE_ONCE(ao_info->current_key, NULL);
+		if (ao_info->rnext_key == key)
+			WRITE_ONCE(ao_info->rnext_key, NULL);
 		atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
 		call_rcu(&key->rcu, tcp_ao_key_free_rcu);
 		return 0;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · HanQuan

In tcp_ao_delete_key(), the del_async path skips the current_key and rnext_key validity checks present in the synchronous path, assuming these pointers are always NULL on LISTEN sockets. However, if a key was added with set_current=1/set_rnext=1 while the socket was in CLOSE state, current_key and rnext_key will be non-NULL after listen() transitions the socket to LISTEN.

When such a key is deleted with del_async=1, hlist_del_rcu() and call_rcu() free the key without clearing the dangling pointers. After the RCU grace period, getsockopt(TCP_AO_INFO) dereferences current_key->sndid and rnext_key->rcvid from freed slab memory.

Quoted 5 lines

Clear current_key and rnext_key in the del_async path when they reference the key being deleted.

Fixes d6732b95b6fb ("net/tcp: Allow asynchronous delete for TCP-AO keys (MKTs)")
#2 lore raw
In reply to HanQuan
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Tue, 23 Jun 2026 01:52:08 +0000 you wrote:

In tcp_ao_delete_key(), the del_async path skips the current_key and rnext_key validity checks present in the synchronous path, assuming these pointers are always NULL on LISTEN sockets. However, if a key was added with set_current=1/set_rnext=1 while the socket was in CLOSE state, current_key and rnext_key will be non-NULL after listen() transitions the socket to LISTEN.

[...]

Here is the summary with links:
- net/tcp-ao: fix use-after-free of key in del_async path

    https://git.kernel.org/netdev/net/c/5ba9950bc907

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html