Commit ef562489 authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'insufficient-tcp-source-port-randomness'

Willy Tarreau says:

====================
insufficient TCP source port randomness

In a not-yet published paper, Moshe Kol, Amit Klein, and Yossi Gilad
report being able to accurately identify a client by forcing it to emit
only 40 times more connections than the number of entries in the
table_perturb[] table, which is indexed by hashing the connection tuple.
The current 2^8 setting allows them to perform that attack with only 10k
connections, which is not hard to achieve in a few seconds.

Eric, Amit and I have been working on this for a few weeks now imagining,
testing and eliminating a number of approaches that Amit and his team were
still able to break or that were found to be too risky or too expensive,
and ended up with the simple improvements in this series that resists to
the attack, doesn't degrade the performance, and preserves a reliable port
selection algorithm to avoid connection failures, including the odd/even
port selection preference that allows bind() to always find a port quickly
even under strong connect() stress.

The approach relies on several factors:
  - resalting the hash secret that's used to choose the table_perturb[]
    entry every 10 seconds to eliminate slow attacks and force the
    attacker to forget everything that was learned after this delay.
    This already eliminates most of the problem because if a client
    stays silent for more than 10 seconds there's no link between the
    previous and the next patterns, and 10s isn't yet frequent enough
    to cause too frequent repetition of a same port that may induce a
    connection failure ;

  - adding small random increments to the source port. Previously, a
    random 0 or 1 was added every 16 ports. Now a random 0 to 7 is
    added after each port. This means that with the default 32768-60999
    range, a worst case rollover happens after 1764 connections, and
    an average of 3137. This doesn't stop statistical attacks but
    requires significantly more iterations of the same attack to
    confirm a guess.

  - increasing the table_perturb[] size from 2^8 to 2^16, which Amit
    says will require 2.6 million connections to be attacked with the
    changes above, making it pointless to get a fingerprint that will
    only last 10 seconds. Due to the size, the table was made dynamic.

  - a few minor improvements on the bits used from the hash, to eliminate
    some unfortunate correlations that may possibly have been exploited
    to design future attack models.

These changes were tested under the most extreme conditions, up to
1.1 million connections per second to one and a few targets, showing no
performance regression, and only 2 connection failures within 13 billion,
which is less than 2^-32 and perfectly within usual values.

The series is split into small reviewable changes and was already reviewed
by Amit and Eric.
====================

Link: https://lore.kernel.org/r/20220502084614.24123-1-w@1wt.euSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents 205557ba e8161345
...@@ -425,7 +425,7 @@ static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr) ...@@ -425,7 +425,7 @@ static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
} }
int __inet_hash_connect(struct inet_timewait_death_row *death_row, int __inet_hash_connect(struct inet_timewait_death_row *death_row,
struct sock *sk, u32 port_offset, struct sock *sk, u64 port_offset,
int (*check_established)(struct inet_timewait_death_row *, int (*check_established)(struct inet_timewait_death_row *,
struct sock *, __u16, struct sock *, __u16,
struct inet_timewait_sock **)); struct inet_timewait_sock **));
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
#include <linux/types.h> #include <linux/types.h>
u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport); u64 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport);
u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr, u64 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
__be16 dport); __be16 dport);
u32 secure_tcp_seq(__be32 saddr, __be32 daddr, u32 secure_tcp_seq(__be32 saddr, __be32 daddr,
__be16 sport, __be16 dport); __be16 sport, __be16 dport);
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
static siphash_aligned_key_t net_secret; static siphash_aligned_key_t net_secret;
static siphash_aligned_key_t ts_secret; static siphash_aligned_key_t ts_secret;
#define EPHEMERAL_PORT_SHUFFLE_PERIOD (10 * HZ)
static __always_inline void net_secret_init(void) static __always_inline void net_secret_init(void)
{ {
net_get_random_once(&net_secret, sizeof(net_secret)); net_get_random_once(&net_secret, sizeof(net_secret));
...@@ -94,17 +96,19 @@ u32 secure_tcpv6_seq(const __be32 *saddr, const __be32 *daddr, ...@@ -94,17 +96,19 @@ u32 secure_tcpv6_seq(const __be32 *saddr, const __be32 *daddr,
} }
EXPORT_SYMBOL(secure_tcpv6_seq); EXPORT_SYMBOL(secure_tcpv6_seq);
u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr, u64 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
__be16 dport) __be16 dport)
{ {
const struct { const struct {
struct in6_addr saddr; struct in6_addr saddr;
struct in6_addr daddr; struct in6_addr daddr;
unsigned int timeseed;
__be16 dport; __be16 dport;
} __aligned(SIPHASH_ALIGNMENT) combined = { } __aligned(SIPHASH_ALIGNMENT) combined = {
.saddr = *(struct in6_addr *)saddr, .saddr = *(struct in6_addr *)saddr,
.daddr = *(struct in6_addr *)daddr, .daddr = *(struct in6_addr *)daddr,
.dport = dport .timeseed = jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
.dport = dport,
}; };
net_secret_init(); net_secret_init();
return siphash(&combined, offsetofend(typeof(combined), dport), return siphash(&combined, offsetofend(typeof(combined), dport),
...@@ -142,11 +146,13 @@ u32 secure_tcp_seq(__be32 saddr, __be32 daddr, ...@@ -142,11 +146,13 @@ u32 secure_tcp_seq(__be32 saddr, __be32 daddr,
} }
EXPORT_SYMBOL_GPL(secure_tcp_seq); EXPORT_SYMBOL_GPL(secure_tcp_seq);
u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport) u64 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
{ {
net_secret_init(); net_secret_init();
return siphash_3u32((__force u32)saddr, (__force u32)daddr, return siphash_4u32((__force u32)saddr, (__force u32)daddr,
(__force u16)dport, &net_secret); (__force u16)dport,
jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
&net_secret);
} }
EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral); EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
#endif #endif
......
...@@ -504,7 +504,7 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row, ...@@ -504,7 +504,7 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row,
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
} }
static u32 inet_sk_port_offset(const struct sock *sk) static u64 inet_sk_port_offset(const struct sock *sk)
{ {
const struct inet_sock *inet = inet_sk(sk); const struct inet_sock *inet = inet_sk(sk);
...@@ -726,15 +726,17 @@ EXPORT_SYMBOL_GPL(inet_unhash); ...@@ -726,15 +726,17 @@ EXPORT_SYMBOL_GPL(inet_unhash);
* Note that we use 32bit integers (vs RFC 'short integers') * Note that we use 32bit integers (vs RFC 'short integers')
* because 2^16 is not a multiple of num_ephemeral and this * because 2^16 is not a multiple of num_ephemeral and this
* property might be used by clever attacker. * property might be used by clever attacker.
* RFC claims using TABLE_LENGTH=10 buckets gives an improvement, * RFC claims using TABLE_LENGTH=10 buckets gives an improvement, though
* we use 256 instead to really give more isolation and * attacks were since demonstrated, thus we use 65536 instead to really
* privacy, this only consumes 1 KB of kernel memory. * give more isolation and privacy, at the expense of 256kB of kernel
* memory.
*/ */
#define INET_TABLE_PERTURB_SHIFT 8 #define INET_TABLE_PERTURB_SHIFT 16
static u32 table_perturb[1 << INET_TABLE_PERTURB_SHIFT]; #define INET_TABLE_PERTURB_SIZE (1 << INET_TABLE_PERTURB_SHIFT)
static u32 *table_perturb;
int __inet_hash_connect(struct inet_timewait_death_row *death_row, int __inet_hash_connect(struct inet_timewait_death_row *death_row,
struct sock *sk, u32 port_offset, struct sock *sk, u64 port_offset,
int (*check_established)(struct inet_timewait_death_row *, int (*check_established)(struct inet_timewait_death_row *,
struct sock *, __u16, struct inet_timewait_sock **)) struct sock *, __u16, struct inet_timewait_sock **))
{ {
...@@ -774,10 +776,13 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, ...@@ -774,10 +776,13 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
if (likely(remaining > 1)) if (likely(remaining > 1))
remaining &= ~1U; remaining &= ~1U;
net_get_random_once(table_perturb, sizeof(table_perturb)); net_get_random_once(table_perturb,
index = hash_32(port_offset, INET_TABLE_PERTURB_SHIFT); INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
index = port_offset & (INET_TABLE_PERTURB_SIZE - 1);
offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32);
offset %= remaining;
offset = (READ_ONCE(table_perturb[index]) + port_offset) % remaining;
/* In first pass we try ports of @low parity. /* In first pass we try ports of @low parity.
* inet_csk_get_port() does the opposite choice. * inet_csk_get_port() does the opposite choice.
*/ */
...@@ -831,11 +836,12 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, ...@@ -831,11 +836,12 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
ok: ok:
/* If our first attempt found a candidate, skip next candidate /* Here we want to add a little bit of randomness to the next source
* in 1/16 of cases to add some noise. * port that will be chosen. We use a max() with a random here so that
* on low contention the randomness is maximal and on high contention
* it may be inexistent.
*/ */
if (!i && !(prandom_u32() % 16)) i = max_t(int, i, (prandom_u32() & 7) * 2);
i = 2;
WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2); WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2);
/* Head lock still held and bh's disabled */ /* Head lock still held and bh's disabled */
...@@ -859,7 +865,7 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, ...@@ -859,7 +865,7 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
int inet_hash_connect(struct inet_timewait_death_row *death_row, int inet_hash_connect(struct inet_timewait_death_row *death_row,
struct sock *sk) struct sock *sk)
{ {
u32 port_offset = 0; u64 port_offset = 0;
if (!inet_sk(sk)->inet_num) if (!inet_sk(sk)->inet_num)
port_offset = inet_sk_port_offset(sk); port_offset = inet_sk_port_offset(sk);
...@@ -909,6 +915,12 @@ void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name, ...@@ -909,6 +915,12 @@ void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
low_limit, low_limit,
high_limit); high_limit);
init_hashinfo_lhash2(h); init_hashinfo_lhash2(h);
/* this one is used for source ports of outgoing connections */
table_perturb = kmalloc_array(INET_TABLE_PERTURB_SIZE,
sizeof(*table_perturb), GFP_KERNEL);
if (!table_perturb)
panic("TCP: failed to alloc table_perturb");
} }
int inet_hashinfo2_init_mod(struct inet_hashinfo *h) int inet_hashinfo2_init_mod(struct inet_hashinfo *h)
......
...@@ -308,7 +308,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row, ...@@ -308,7 +308,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
} }
static u32 inet6_sk_port_offset(const struct sock *sk) static u64 inet6_sk_port_offset(const struct sock *sk)
{ {
const struct inet_sock *inet = inet_sk(sk); const struct inet_sock *inet = inet_sk(sk);
...@@ -320,7 +320,7 @@ static u32 inet6_sk_port_offset(const struct sock *sk) ...@@ -320,7 +320,7 @@ static u32 inet6_sk_port_offset(const struct sock *sk)
int inet6_hash_connect(struct inet_timewait_death_row *death_row, int inet6_hash_connect(struct inet_timewait_death_row *death_row,
struct sock *sk) struct sock *sk)
{ {
u32 port_offset = 0; u64 port_offset = 0;
if (!inet_sk(sk)->inet_num) if (!inet_sk(sk)->inet_num)
port_offset = inet6_sk_port_offset(sk); port_offset = inet6_sk_port_offset(sk);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment