Skip to content

Commit 007b015

Browse files
jbaron-lgtmgregkh
authored andcommitted
tcp: correct read of TFO keys on big endian systems
[ Upstream commit f19008e ] When TFO keys are read back on big endian systems either via the global sysctl interface or via getsockopt() using TCP_FASTOPEN_KEY, the values don't match what was written. For example, on s390x: # echo "1-2-3-4" > /proc/sys/net/ipv4/tcp_fastopen_key # cat /proc/sys/net/ipv4/tcp_fastopen_key 02000000-01000000-04000000-03000000 Instead of: # cat /proc/sys/net/ipv4/tcp_fastopen_key 00000001-00000002-00000003-00000004 Fix this by converting to the correct endianness on read. This was reported by Colin Ian King when running the 'tcp_fastopen_backup_key' net selftest on s390x, which depends on the read value matching what was written. I've confirmed that the test now passes on big and little endian systems. Signed-off-by: Jason Baron <jbaron@akamai.com> Fixes: 438ac88 ("net: fastopen: robustness and endianness fixes for SipHash") Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> Cc: Eric Dumazet <edumazet@google.com> Reported-and-tested-by: Colin Ian King <colin.king@canonical.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5114065 commit 007b015

4 files changed

Lines changed: 33 additions & 24 deletions

File tree

‎include/net/tcp.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,8 @@ void tcp_fastopen_destroy_cipher(struct sock *sk);
16691669
void tcp_fastopen_ctx_destroy(struct net *net);
16701670
int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
16711671
void *primary_key, void *backup_key);
1672+
int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
1673+
u64 *key);
16721674
void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb);
16731675
struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
16741676
struct request_sock *req,

‎net/ipv4/sysctl_net_ipv4.c‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,24 +307,16 @@ static int proc_tcp_fastopen_key(struct ctl_table *table, int write,
307307
struct ctl_table tbl = { .maxlen = ((TCP_FASTOPEN_KEY_LENGTH *
308308
2 * TCP_FASTOPEN_KEY_MAX) +
309309
(TCP_FASTOPEN_KEY_MAX * 5)) };
310-
struct tcp_fastopen_context *ctx;
311-
u32 user_key[TCP_FASTOPEN_KEY_MAX * 4];
312-
__le32 key[TCP_FASTOPEN_KEY_MAX * 4];
310+
u32 user_key[TCP_FASTOPEN_KEY_BUF_LENGTH / sizeof(u32)];
311+
__le32 key[TCP_FASTOPEN_KEY_BUF_LENGTH / sizeof(__le32)];
313312
char *backup_data;
314-
int ret, i = 0, off = 0, n_keys = 0;
313+
int ret, i = 0, off = 0, n_keys;
315314

316315
tbl.data = kmalloc(tbl.maxlen, GFP_KERNEL);
317316
if (!tbl.data)
318317
return -ENOMEM;
319318

320-
rcu_read_lock();
321-
ctx = rcu_dereference(net->ipv4.tcp_fastopen_ctx);
322-
if (ctx) {
323-
n_keys = tcp_fastopen_context_len(ctx);
324-
memcpy(&key[0], &ctx->key[0], TCP_FASTOPEN_KEY_LENGTH * n_keys);
325-
}
326-
rcu_read_unlock();
327-
319+
n_keys = tcp_fastopen_get_cipher(net, NULL, (u64 *)key);
328320
if (!n_keys) {
329321
memset(&key[0], 0, TCP_FASTOPEN_KEY_LENGTH);
330322
n_keys = 1;

‎net/ipv4/tcp.c‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,22 +3538,14 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
35383538
return 0;
35393539

35403540
case TCP_FASTOPEN_KEY: {
3541-
__u8 key[TCP_FASTOPEN_KEY_BUF_LENGTH];
3542-
struct tcp_fastopen_context *ctx;
3543-
unsigned int key_len = 0;
3541+
u64 key[TCP_FASTOPEN_KEY_BUF_LENGTH / sizeof(u64)];
3542+
unsigned int key_len;
35443543

35453544
if (get_user(len, optlen))
35463545
return -EFAULT;
35473546

3548-
rcu_read_lock();
3549-
ctx = rcu_dereference(icsk->icsk_accept_queue.fastopenq.ctx);
3550-
if (ctx) {
3551-
key_len = tcp_fastopen_context_len(ctx) *
3552-
TCP_FASTOPEN_KEY_LENGTH;
3553-
memcpy(&key[0], &ctx->key[0], key_len);
3554-
}
3555-
rcu_read_unlock();
3556-
3547+
key_len = tcp_fastopen_get_cipher(net, icsk, key) *
3548+
TCP_FASTOPEN_KEY_LENGTH;
35573549
len = min_t(unsigned int, len, key_len);
35583550
if (put_user(len, optlen))
35593551
return -EFAULT;

‎net/ipv4/tcp_fastopen.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
108108
return err;
109109
}
110110

111+
int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
112+
u64 *key)
113+
{
114+
struct tcp_fastopen_context *ctx;
115+
int n_keys = 0, i;
116+
117+
rcu_read_lock();
118+
if (icsk)
119+
ctx = rcu_dereference(icsk->icsk_accept_queue.fastopenq.ctx);
120+
else
121+
ctx = rcu_dereference(net->ipv4.tcp_fastopen_ctx);
122+
if (ctx) {
123+
n_keys = tcp_fastopen_context_len(ctx);
124+
for (i = 0; i < n_keys; i++) {
125+
put_unaligned_le64(ctx->key[i].key[0], key + (i * 2));
126+
put_unaligned_le64(ctx->key[i].key[1], key + (i * 2) + 1);
127+
}
128+
}
129+
rcu_read_unlock();
130+
131+
return n_keys;
132+
}
133+
111134
static bool __tcp_fastopen_cookie_gen_cipher(struct request_sock *req,
112135
struct sk_buff *syn,
113136
const siphash_key_t *key,

0 commit comments

Comments
 (0)