tcp_metrics: hash the destination address with SipHash
Subsystems (3)
PATCH v3 1/1 tcp_metrics: hash the destination address with SipHash
The tcp_metrics bucket is derived from a fold of the peer address. For IPv4 the fold is ipv4_addr_hash(), which returns the address unchanged. For IPv6 it is ipv6_addr_hash(), which XORs the four 32-bit words of the address. net_hash_mix() is only XORed in after that fold, so two addresses of either family with the same fold share a bucket in every netns.
A remote attacker with an IPv6 /64 can thus compute, without knowing net_hash_mix(), addresses that collide with a chosen IPv4 or IPv6 address:
w3 = w2 ^ p0 ^ p1 ^ target /* p0, p1: prefix, w2: free */
A bucket holds six entries and the seventh insert replaces the oldest. tcp_init_metrics() seeds the initial RTO from the cached RTT, so the attacker can tell whether an entry it planted was evicted, and thus whether another address in that bucket closed a connection since.
This primitive, with six entries of one fold and a seventh of another, shows whether two folds share a bucket. Each answer rules out values of net_hash_mix(), the only unknown, and since XOR and multiply is not a PRF a few hundred answers recover it. The attacker can then fill every bucket and see which one any client's connection evicts from. The multiplication in hash_32() is invertible, so with net_hash_mix() known, each observed eviction leaks 14 bits of the client's address.
Remote net_hash_mix() recovery followed by remote bucket eviction observation was tested under different levels of legitimate traffic and with the attacker machine and victim server being in different continents:
legit attacker evictions false or conn/s conn/s detected misclassified ----- ------ --------- ------------- 1 944 214 / 214 0 10 927 2146 / 2165 0 100 864 22356 / 23576 0 999 1239 167213 / 286754 0 2974 1412 118665 / 809618 0
Measurements began after the attacker had recovered net_hash_mix() and planted entries; the legitimate connections were present throughout. net_hash_mix() recovery was successful in all cases and took 28-936 seconds. Note that while the attacker must send many connections, these are very efficient to process and caused negligible load on the victim server as measured by latency and memory.
Fix this by hashing the address with SipHash and a random key, as commit 2ee66e948717 ("inetpeer: randomize RB-tree node comparison using SipHash") did for inet_peer. net_hash_mix() is fed into the SipHash input, as inet_ehashfn() feeds it into its seed, so each netns gets its own bucket map. hash_32() is kept to reduce the result to the table size.
---
v3: - add testing numbers to changelog (Eric)
- fix patch formatting issues (Eric)
- feed net_hash_mix() into the SipHash input instead of XORing it in afterwards to protect from potential local attacks (Sashiko)
v2: https://lore.kernel.org/netdev/[email protected]/
- use siphash_1u32() for IPv4, a4.vif is always zero in tcp_metrics (Kuniyuki)
- call tcpm_addr_hash() once after the address family branches
- changelog: explain more clearly why net_hash_mix() and addresses are recoverable
v1: https://lore.kernel.org/netdev/[email protected]/
Reproducer: github.com/agusnicco/tcp-side-channel
I address the questions Eric asked on v2 below:
Quoted 7 lines
How many connections does your reproducer need, for each phase ?
1) Recovering
net_hash_mix().2) Filling the table (16384 * 6 flows with the default size ?), then each sweep to find which bucket was hit.
1) I measured this to be 191 to 677 connections per attempt. The reproducer verifies the recovered value against collisions it has not tested yet, and starts over if they do not hold. The most it took was 15 attempts, on a server receiving 800 new connections per second, 936s total.
2) You guessed it: 16384 * 6 = 98,304 connections. The attack would be infeasible if they had to be sequential but concurrency can be as high as the number of buckets. On my tests plants sustained 1000 conn/s with zero target degradation. Filling the whole table took a median of 43s with victim server and attacker machine being across the Atlantic.
Each sweep is one read per bucket, 16384 connections, plus one more for every eviction found. Sweeps took a median of 24s in my tests.
Quoted 1 line
Each probe also has to wait for an RTO, so wall time matters too.
Planting does not wait for an RTO since the attacker echoes an old TCP timestamp, and the server records a large RTT immediately (see reproducer). Reading takes about 0.3s per probe, a little over rto_min, no matter how large the cached value is. Probes run in parallel.
Quoted 4 lines
Also, how does this behave on a server with real traffic ? Every other client closing a flow evicts entries as well. You mention a test against Ubuntu 24.04 over the internet, was this host serving anything else ?
My original test had an idle host but I tested high traffic today (see table in the commit message). I performed the attack on an nginx server on Ubuntu 24.04 that was receiving legitimate connections while the attack happened. The victim server was in a Virginia datacenter, the attacker machine in Ireland.
The attacker needs to perform hundreds to thousands of connections per second if the victim server is high traffic but these are extremely cheap to handle. I tested this separately. Even an unproxied uvicorn server could handle 200/s before degradation. nginx handled 1000/s without degradation of any kind being observed. And they could do it on a t3.micro EC2 instance (AWS) which has 2 vCPUs and 1 GiB of memory. I measured degradation using latency and memory readings while the attack happened.
I also reproduced the case where the attacker intersects buckets from multiple hosts to recover the full 32 bits of each address. This is possible under many common scenarios, the one I tested involves client browsers loading a site and hence making many connections to eight hosts simultaneously. 47/50 addresses were recovered while the victim servers received 100 legitimate connections per second. Also done across the Atlantic.
All tests were done on my own infrastructure.
Quoted 1 line
Note that many servers (including Google ones) have tcp_no_metrics_save=1
Many probably do but I haven't come across one yet. All distros ship tcp_no_metrics_save=0. I also rented 5 AWS, Azure, and GCP VMs and they all came with tcp_no_metrics_save=0.
This bug was discovered during an LLM-based code review.
The patch was tested on a QEMU guest. Addresses built to share a fold no longer share a bucket, in either netns. Entries are still found when a peer reconnects and by "ip tcp_metrics show/delete".
For stable: 5.10 and 5.15 lack siphash_aligned_key_t; "static siphash_key_t tcpm_hash_key __read_mostly __aligned(16);" can be used there.
net/ipv4/tcp_metrics.c | 49 ++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-)
net/ipv4/tcp_metrics.c +30 -19
@@ -8,6 +8,7 @@#include <linux/init.h> #include <linux/tcp.h> #include <linux/hash.h> +#include <linux/siphash.h> #include <linux/tcp_metrics.h> #include <linux/vmalloc.h>
@@ -95,6 +96,25 @@ static unsigned int tcp_metrics_hash_log __read_mostly;static DEFINE_SPINLOCK (tcp_metrics_lock);static DEFINE_SEQLOCK (fastopen_seqlock); +static siphash_aligned_key_t tcpm_hash_key__read_mostly ; + +static u32 tcpm_addr_hash (const struct inetpeer_addr *a,const struct net *net) +{ +u32 mix =net_hash_mix (net); + +net_get_random_once (&tcpm_hash_key,sizeof (tcpm_hash_key)); + +if (a->family ==AF_INET ) +return siphash_2u32 ((__force u32 )a->a4.addr, mix, + &tcpm_hash_key); + +return siphash_3u64 (((u64 )(__force u32 )a->a6.s6_addr32[1 ] <<32 ) | + (__force u32 )a->a6.s6_addr32[0 ], + ((u64 )(__force u32 )a->a6.s6_addr32[3 ] <<32 ) | + (__force u32 )a->a6.s6_addr32[2 ], + mix, &tcpm_hash_key); +} +static void tcpm_suck_dst (struct tcp_metrics_block *tm,const struct dst_entry *dst,bool fastopen_clear)
@@ -270,13 +290,11 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,case AF_INET :inetpeer_set_addr_v4 (&saddr,inet_rsk (req)->ir_loc_addr);inetpeer_set_addr_v4 (&daddr,inet_rsk (req)->ir_rmt_addr); - hash =ipv4_addr_hash (inet_rsk (req)->ir_rmt_addr);break ;#if IS_ENABLED (CONFIG_IPV6 )case AF_INET6 :inetpeer_set_addr_v6 (&saddr, &inet_rsk (req)->ir_v6_loc_addr);inetpeer_set_addr_v6 (&daddr, &inet_rsk (req)->ir_v6_rmt_addr); - hash =ipv6_addr_hash (&inet_rsk (req)->ir_v6_rmt_addr);break ;#endif default :
@@ -284,7 +302,7 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req, } net =sock_net (req_to_sk (req)); - hash ^=net_hash_mix (net); + hash =tcpm_addr_hash (&daddr, net); hash =hash_32 (hash, tcp_metrics_hash_log); for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
@@ -310,18 +328,15 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,if (sk->sk_family ==AF_INET ) {inetpeer_set_addr_v4 (&saddr,inet_sk (sk)->inet_saddr);inetpeer_set_addr_v4 (&daddr,inet_sk (sk)->inet_daddr); - hash =ipv4_addr_hash (inet_sk (sk)->inet_daddr); }#if IS_ENABLED (CONFIG_IPV6 ) else if (sk->sk_family == AF_INET6) {if (ipv6_addr_v4mapped (&sk->sk_v6_daddr)) {inetpeer_set_addr_v4 (&saddr,inet_sk (sk)->inet_saddr);inetpeer_set_addr_v4 (&daddr,inet_sk (sk)->inet_daddr); - hash =ipv4_addr_hash (inet_sk (sk)->inet_daddr); }else {inetpeer_set_addr_v6 (&saddr, &sk->sk_v6_rcv_saddr);inetpeer_set_addr_v6 (&daddr, &sk->sk_v6_daddr); - hash =ipv6_addr_hash (&sk->sk_v6_daddr); } }#endif
@@ -329,7 +344,7 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,return NULL ; net =sock_net (sk); - hash ^=net_hash_mix (net); + hash =tcpm_addr_hash (&daddr, net); hash =hash_32 (hash, tcp_metrics_hash_log); tm =__tcp_get_metrics (&saddr, &daddr, net, hash);
@@ -811,15 +826,13 @@ static int tcp_metrics_nl_dump(struct sk_buff *skb, }static int __parse_nl_addr (struct genl_info *info,struct inetpeer_addr *addr, -unsigned int *hash,int optional,int v4,int v6) +int optional,int v4,int v6) {struct nlattr *a; a = info->attrs[v4];if (a) {inetpeer_set_addr_v4 (addr,nla_get_in_addr (a)); -if (hash) - *hash =ipv4_addr_hash (inetpeer_get_addr_v4 (addr));return 0 ; } a = info->attrs[v6];
@@ -828,24 +841,22 @@ static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr, in6 =nla_get_in6_addr (a);inetpeer_set_addr_v6 (addr, &in6); -if (hash) - *hash =ipv6_addr_hash (inetpeer_get_addr_v6 (addr));return 0 ; }return optional ?1 : -EAFNOSUPPORT ; }static int parse_nl_addr (struct genl_info *info,struct inetpeer_addr *addr, -unsigned int *hash,int optional) +int optional) { -return __parse_nl_addr (info, addr, hash, optional, +return __parse_nl_addr (info, addr, optional,TCP_METRICS_ATTR_ADDR_IPV4 ,TCP_METRICS_ATTR_ADDR_IPV6 ); }static int parse_nl_saddr (struct genl_info *info,struct inetpeer_addr *addr) { -return __parse_nl_addr (info, addr,NULL ,0 , +return __parse_nl_addr (info, addr,0 ,TCP_METRICS_ATTR_SADDR_IPV4 ,TCP_METRICS_ATTR_SADDR_IPV6 ); }
@@ -861,7 +872,7 @@ static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)int ret;bool src = true; - ret =parse_nl_addr (info, &daddr, &hash,0 ); + ret =parse_nl_addr (info, &daddr,0 );if (ret <0 )return ret;
@@ -878,7 +889,7 @@ static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)if (!reply) goto nla_put_failure; - hash ^=net_hash_mix (net); + hash =tcpm_addr_hash (&daddr, net); hash =hash_32 (hash, tcp_metrics_hash_log); ret = -ESRCH ;rcu_read_lock ();
@@ -947,7 +958,7 @@ static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)int ret;bool src = true, found = false; - ret =parse_nl_addr (info, &daddr, &hash,1 ); + ret =parse_nl_addr (info, &daddr,1 );if (ret <0 )return ret;if (ret >0 ) {
@@ -958,7 +969,7 @@ static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)if (ret <0 ) src = false; - hash ^=net_hash_mix (net); + hash =tcpm_addr_hash (&daddr, net); hash =hash_32 (hash, tcp_metrics_hash_log); hb = tcp_metrics_hash + hash; pp = &hb->chain;
Signature · 1 line
2.53.0