tcp: TCP-AO connect() fixes
Subsystems (4)
v3 cover tcp: TCP-AO connect() fixes
Resending v3.
I've addeded credits to Qihang on patch 2; and a third patch/fix for static key decrement.
---
Dmitry Safonov (1):
tcp: Decrement tcp_md5_needed static branch
Michael Bommarito (2):
tcp: restore RCU grace period in tcp_ao_destroy_sock
tcp: defer md5sig_info kfree past RCU grace period in tcp_connect
include/net/tcp_ao.h | 1 + net/ipv4/tcp_ao.c | 5 +++-- net/ipv4/tcp_ipv4.c | 4 ++-- net/ipv4/tcp_output.c | 8 ++++++-- 4 files changed, 12 insertions(+), 6 deletions(-)
---
base-commit: 02f144fbb4c86c360495d33debe307cb46a57f95
change-id: 20260625-tcp-md5-connect-dc2369d7f414
Best regards,
Signature · 1 line
Dmitry Safonov <[email protected]>
PATCH v3 1/3 tcp: restore RCU grace period in tcp_ao_destroy_sock
From: Michael Bommarito <[email protected]>
Commit 51e547e8c89c ("tcp: Free TCP-AO/TCP-MD5 info/keys without RCU") removed the call_rcu() callback from tcp_ao_destroy_sock(), arguing that "the destruction of info/keys is delayed until the socket destructor" and therefore "no one can discover it anymore".
That argument does not hold for the call site in tcp_connect() (net/ipv4/tcp_output.c:4327-4332). At that point the socket is in TCP_SYN_SENT, has already been inserted into the inet ehash by inet_hash_connect() in tcp_v4_connect(), and is therefore very much
discoverable: any softirq running tcp_v4_rcv() on another CPU can take the socket out of the ehash, walk into tcp_inbound_hash(), and load tp->ao_info via implicit RCU before bh_lock_sock_nested() is taken on the destroying CPU.
The reader path then enters __tcp_ao_do_lookup() (net/ipv4/tcp_ao.c:208) which re-loads tp->ao_info via rcu_dereference_check(); the re-load can still observe the (about-to-be-freed) pointer because there is no synchronize_rcu() between rcu_assign_pointer(tp->ao_info, NULL) and tcp_ao_info_free() in tcp_ao_destroy_sock(). The captured pointer is then walked at line 223:
hlist_for_each_entry_rcu(key, &ao->head, node, ...)
The writer's synchronous kfree() is free to complete between the line 218 re-fetch and the line 223 hlist iteration. The slab is reused (or simply LIST_POISON1-stamped if not yet reused) and the iteration walks attacker-controlled or poison memory in softirq context.
Reproducer (no debug shim, stock x86_64 v7.1-rc2 SMP+KASAN, QEMU+KVM):
an unprivileged uid=1000 process inside CLONE_NEWUSER|CLONE_NEWNET installs TCP_MD5SIG + TCP_AO_ADD_KEY on a TCP socket, sprays forged TCP-AO segments toward its eventual 4-tuple via raw sockets, then calls connect(). The md5-wins reconciliation in tcp_connect() fires tcp_ao_destroy_sock(); the softirq backlog reader on the loopback NAPI path crashes on the freed ao->head.first walk:
Oops: general protection fault, probably for non-canonical
address 0xfbd59c000000002f
KASAN: maybe wild-memory-access in range
[0xdead000000000178-0xdead00000000017f]
CPU: 0 UID: 1000 PID: 100 Comm: repro_userns
RIP: 0010:__tcp_ao_do_lookup+0x107/0x1c0
Call Trace: <IRQ>
__tcp_ao_do_lookup+0x107/0x1c0
tcp_ao_inbound_lookup.constprop.0+0x12a/0x200
tcp_inbound_ao_hash+0x5ea/0x1520
tcp_inbound_hash+0x7ce/0x1240
tcp_v4_rcv+0x1e7a/0x3e10
...Restore the RCU grace period: re-add struct rcu_head to tcp_ao_info and replace the synchronous tcp_ao_info_free() with a call_rcu() callback. Readers that captured tp->ao_info before rcu_assign_pointer NULLed it now see the object remain valid until rcu_read_unlock(). With the patch applied the reproducer runs cleanly for 2000 iterations on the same kernel build.
--- include/net/tcp_ao.h | 1 + net/ipv4/tcp_ao.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-)
include/net/tcp_ao.h +1 -0
@@ -145,6 +145,7 @@ struct tcp_ao_info {u32 snd_sne;u32 rcv_sne;refcount_t refcnt;/* Protects twsk destruction */ +struct rcu_head rcu; };#ifdef CONFIG_TCP_MD5SIG
net/ipv4/tcp_ao.c +3 -2
@@ -371,8 +371,9 @@ static void tcp_ao_key_free_rcu(struct rcu_head *head)kfree_sensitive (key); } -static void tcp_ao_info_free (struct tcp_ao_info *ao) +static void tcp_ao_info_free_rcu (struct rcu_head *head) { + struct tcp_ao_info *ao = container_of(head, struct tcp_ao_info, rcu);struct tcp_ao_key *key;struct hlist_node *n;
@@ -411,7 +412,7 @@ void tcp_ao_destroy_sock(struct sock *sk, bool twsk)if (!twsk)tcp_ao_sk_omem_free (sk, ao); -tcp_ao_info_free (ao); +call_rcu (&ao->rcu, tcp_ao_info_free_rcu); }void tcp_ao_time_wait (struct tcp_timewait_sock *tcptw,struct tcp_sock *tp)
Signature · 1 line
2.51.2
PATCH v3 2/3 tcp: defer md5sig_info kfree past RCU grace period in tcp_connect
In reply to Dmitry SafonovFrom: Michael Bommarito <[email protected]>
The md5+ao reconciliation in tcp_connect() (net/ipv4/tcp_output.c) has two symmetric branches:
if (needs_md5) {
tcp_ao_destroy_sock(sk, false);
} else if (needs_ao) {
tcp_clear_md5_list(sk);
kfree(rcu_replace_pointer(tp->md5sig_info, NULL, ...));
}Both branches free a per-socket auth-info object while the socket is in TCP_SYN_SENT and is already on the inet ehash (inserted by inet_hash_connect() in tcp_v4_connect()). Both branches are reachable by softirq RX-path readers that load the corresponding info pointer via implicit RCU before bh_lock_sock_nested() is taken.
The needs_md5 branch is fixed in the prior patch by re-introducing the call_rcu() free in tcp_ao_destroy_sock(): the equivalent per-key loop runs inside tcp_ao_info_free_rcu(), the RCU callback, so by the time it frees each tcp_ao_key all softirq readers that captured the container have already completed rcu_read_unlock().
The needs_ao branch is not symmetric in the same way. The container free can be deferred via kfree_rcu(md5sig, rcu) -- struct tcp_md5sig_info already has the required rcu member (include/net/tcp.h:1999-2002), and the rest of the tree already does this in the tcp_md5sig_info_add() rollback paths (net/ipv4/tcp_ipv4.c:1410, 1436). But the per-key teardown is done by tcp_clear_md5_list() in process context BEFORE the container's RCU grace period: it walks &md5sig->head and frees each tcp_md5sig_key with bare hlist_del + kfree. A concurrent softirq reader in __tcp_md5_do_lookup() / __tcp_md5_do_lookup_exact() (tcp_ipv4.c:1253, 1298) walks the same list via hlist_for_each_entry_rcu() and races with that bare kfree on the keys themselves -- a per-key slab use-after-free of the same class as the TCP-AO bug, on the same race window.
Fix this in two halves:
1. Convert the bare kfree() in tcp_connect() to kfree_rcu() so the md5sig_info container joins the rest of the md5sig lifecycle. The local-variable lift is mechanical and required because kfree_rcu() is a macro that expects an lvalue.
2. Make tcp_clear_md5_list() RCU-safe by replacing hlist_del + kfree(key) with hlist_del_rcu + kfree_rcu(key, rcu). struct tcp_md5sig_key already carries the rcu member (include/net/tcp.h:1995) and tcp_md5_do_del() (net/ipv4/tcp_ipv4.c:1456) already uses kfree_rcu, so this restores the lifecycle invariant the rest of the file follows rather than introducing a one-off.
The other caller of tcp_clear_md5_list() is tcp_md5_destruct_sock() (net/ipv4/tcp.c:412), which runs from the sock destructor when the socket is already unhashed and unreachable; the extra grace period there is unnecessary but harmless. Making the helper unconditionally RCU-safe is the cleaner contract.
The needs_ao branch is not reachable by the userns reproducer used to demonstrate the AO-side splat (the repro installs both keys but ends up in the needs_md5 branch because the connect peer matches the MD5 key, not the AO key); however the symmetric race exists and a maintainer touching this code should not have to think about which branch escapes RCU and which one does not.
--- net/ipv4/tcp_ipv4.c | 4 ++-- net/ipv4/tcp_output.c | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-)
net/ipv4/tcp_ipv4.c +2 -2
@@ -1467,9 +1467,9 @@ void tcp_clear_md5_list(struct sock *sk) md5sig =rcu_dereference_protected (tp->md5sig_info,1 );hlist_for_each_entry_safe (key, n, &md5sig->head, node) { -hlist_del (&key->node); +hlist_del_rcu (&key->node);atomic_sub (sizeof (*key), &sk->sk_omem_alloc); -kfree (key); +kfree_rcu (key, rcu); } }
net/ipv4/tcp_output.c +6 -2
@@ -4329,9 +4329,13 @@ int tcp_connect(struct sock *sk)if (needs_md5) {tcp_ao_destroy_sock (sk, false); }else if (needs_ao) { +struct tcp_md5sig_info *md5sig; +tcp_clear_md5_list (sk); -kfree (rcu_replace_pointer (tp->md5sig_info,NULL , -lockdep_sock_is_held (sk))); + md5sig =rcu_replace_pointer (tp->md5sig_info,NULL , +lockdep_sock_is_held (sk)); +if (md5sig) +kfree_rcu (md5sig, rcu); } }#endif
Signature · 1 line
2.51.2
PATCH v3 3/3 tcp: Decrement tcp_md5_needed static branch
In reply to Dmitry SafonovIn case of early freeing an unwanted TCP-MD5 key on TCP-AO connect(), md5sig_info is freed right away (and set to NULL). Later, at the moment of socket destruction, the static branch counter is not getting decremented.
Add a missing decrement for TCP-MD5 static branch.
--- net/ipv4/tcp_output.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_output.c +2 -2
@@ -4334,8 +4334,8 @@ int tcp_connect(struct sock *sk)tcp_clear_md5_list (sk); md5sig =rcu_replace_pointer (tp->md5sig_info,NULL ,lockdep_sock_is_held (sk)); -if (md5sig) - kfree_rcu(md5sig, rcu); +kfree_rcu (md5sig, rcu); + static_branch_slow_dec_deferred(&tcp_md5_needed); } }#endif
Signature · 1 line
2.51.2