]> git.baikalelectronics.ru Git - kernel.git/commitdiff
tcp: add some entropy in __inet_hash_connect()
authorEric Dumazet <edumazet@google.com>
Tue, 9 Feb 2021 19:20:28 +0000 (11:20 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 25 Jun 2022 10:44:35 +0000 (12:44 +0200)
commit 69ab98387f5aa1f168cdc911708d0fcaacf5bbdf upstream.

Even when implementing RFC 6056 3.3.4 (Algorithm 4: Double-Hash
Port Selection Algorithm), a patient attacker could still be able
to collect enough state from an otherwise idle host.

Idea of this patch is to inject some noise, in the
cases __inet_hash_connect() found a candidate in the first
attempt.

This noise should not significantly reduce the collision
avoidance, and should be zero if connection table
is already well used.

Note that this is not implementing RFC 6056 3.3.5
because we think Algorithm 5 could hurt typical
workloads.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: David Dworken <ddworken@google.com>
Cc: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Cc: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
net/ipv4/inet_hashtables.c

index 959f4f0c854600ed2bb36b3abce5b0f60b88c77f..ba1aa49b91ed5b1d613aa1159d30fa919907dcbd 100644 (file)
@@ -782,6 +782,11 @@ next_port:
        return -EADDRNOTAVAIL;
 
 ok:
+       /* If our first attempt found a candidate, skip next candidate
+        * in 1/16 of cases to add some noise.
+        */
+       if (!i && !(prandom_u32() % 16))
+               i = 2;
        WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2);
 
        /* Head lock still held and bh's disabled */