Skip to content

Commit 3142dbf

Browse files
author
Paolo Abeni
committed
Merge branch 'tcp-ao-fixes'
Dmitry Safonov says: ==================== TCP-AO fixes Changes from v4: - Dropped 2 patches on which there's no consensus. They will require more work TBD if they may made acceptable. Those are: o "net/tcp: Allow removing current/rnext TCP-AO keys on TCP_LISTEN sockets" o "net/tcp: Store SNEs + SEQs on ao_info" Changes from v3: - Don't restrict adding any keys on TCP-AO connection in VRF, but only the ones that don't match l3index (David) Changes from v2: - rwlocks are problematic in net code (Paolo) Changed the SNE code to avoid spin/rw locks on RX/TX fastpath by double-accounting SEQ numbers for TCP-AO enabled connections. Changes from v1: - Use tcp_can_repair_sock() helper to limit TCP_AO_REPAIR (Eric) - Instead of hook to listen() syscall, allow removing current/rnext keys on TCP_LISTEN (addressing Eric's objection) - Add sne_lock to protect snd_sne/rcv_sne - Don't move used_tcp_ao in struct tcp_request_sock (Eric) I've been working on TCP-AO key-rotation selftests and as a result exercised some corner-cases that are not usually met in production. Here are a bunch of semi-related fixes: - Documentation typo (reported by Markus Elfring) - Proper alignment for TCP-AO option in TCP header that has MAC length of non 4 bytes (now a selftest with randomized maclen/algorithm/etc passes) - 3 uAPI restricting patches that disallow more things to userspace in order to prevent it shooting itself in any parts of the body - SNEs READ_ONCE()/WRITE_ONCE() that went missing by my human factor - Avoid storing MAC length from SYN header as SYN-ACK will use rnext_key.maclen (drops an extra check that fails on new selftests) ==================== Link: https://lore.kernel.org/r/ Signed-off-by: Paolo Abeni <[email protected]>
2 parents 6b07b52 + 9396c4e commit 3142dbf

File tree

10 files changed

+41
-26
lines changed

10 files changed

+41
-26
lines changed

Documentation/networking/tcp_ao.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ also [6.1]::
9999
when it is no longer considered permitted.
100100

101101
Linux TCP-AO will try its best to prevent you from removing a key that's
102-
being used, considering it a key management failure. But sine keeping
102+
being used, considering it a key management failure. But since keeping
103103
an outdated key may become a security issue and as a peer may
104104
unintentionally prevent the removal of an old key by always setting
105105
it as RNextKeyID - a forced key removal mechanism is provided, where

include/linux/tcp.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ struct tcp_request_sock {
169169
#ifdef CONFIG_TCP_AO
170170
u8 ao_keyid;
171171
u8 ao_rcv_next;
172-
u8 maclen;
172+
bool used_tcp_ao;
173173
#endif
174174
};
175175

@@ -180,14 +180,10 @@ static inline struct tcp_request_sock *tcp_rsk(const struct request_sock *req)
180180

181181
static inline bool tcp_rsk_used_ao(const struct request_sock *req)
182182
{
183-
/* The real length of MAC is saved in the request socket,
184-
* signing anything with zero-length makes no sense, so here is
185-
* a little hack..
186-
*/
187183
#ifndef CONFIG_TCP_AO
188184
return false;
189185
#else
190-
return tcp_rsk(req)->maclen != 0;
186+
return tcp_rsk(req)->used_tcp_ao;
191187
#endif
192188
}
193189

include/net/tcp_ao.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@ static inline int tcp_ao_maclen(const struct tcp_ao_key *key)
6262
return key->maclen;
6363
}
6464

65+
/* Use tcp_ao_len_aligned() for TCP header calculations */
6566
static inline int tcp_ao_len(const struct tcp_ao_key *key)
6667
{
6768
return tcp_ao_maclen(key) + sizeof(struct tcp_ao_hdr);
6869
}
6970

71+
static inline int tcp_ao_len_aligned(const struct tcp_ao_key *key)
72+
{
73+
return round_up(tcp_ao_len(key), 4);
74+
}
75+
7076
static inline unsigned int tcp_ao_digest_size(struct tcp_ao_key *key)
7177
{
7278
return key->digest_size;

net/ipv4/tcp.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,10 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
36103610
break;
36113611

36123612
case TCP_AO_REPAIR:
3613+
if (!tcp_can_repair_sock(sk)) {
3614+
err = -EPERM;
3615+
break;
3616+
}
36133617
err = tcp_ao_set_repair(sk, optval, optlen);
36143618
break;
36153619
#ifdef CONFIG_TCP_AO
@@ -4309,6 +4313,8 @@ int do_tcp_getsockopt(struct sock *sk, int level,
43094313
}
43104314
#endif
43114315
case TCP_AO_REPAIR:
4316+
if (!tcp_can_repair_sock(sk))
4317+
return -EPERM;
43124318
return tcp_ao_get_repair(sk, optval, optlen);
43134319
case TCP_AO_GET_KEYS:
43144320
case TCP_AO_INFO: {

net/ipv4/tcp_ao.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
851851
const struct tcp_ao_hdr *aoh;
852852
struct tcp_ao_key *key;
853853

854-
treq->maclen = 0;
854+
treq->used_tcp_ao = false;
855855

856856
if (tcp_parse_auth_options(th, NULL, &aoh) || !aoh)
857857
return;
@@ -863,7 +863,7 @@ void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
863863

864864
treq->ao_rcv_next = aoh->keyid;
865865
treq->ao_keyid = aoh->rnext_keyid;
866-
treq->maclen = tcp_ao_maclen(key);
866+
treq->used_tcp_ao = true;
867867
}
868868

869869
static enum skb_drop_reason
@@ -1100,7 +1100,7 @@ void tcp_ao_connect_init(struct sock *sk)
11001100
ao_info->current_key = key;
11011101
if (!ao_info->rnext_key)
11021102
ao_info->rnext_key = key;
1103-
tp->tcp_header_len += tcp_ao_len(key);
1103+
tp->tcp_header_len += tcp_ao_len_aligned(key);
11041104

11051105
ao_info->lisn = htonl(tp->write_seq);
11061106
ao_info->snd_sne = 0;
@@ -1346,7 +1346,7 @@ static int tcp_ao_parse_crypto(struct tcp_ao_add *cmd, struct tcp_ao_key *key)
13461346
syn_tcp_option_space -= TCPOLEN_MSS_ALIGNED;
13471347
syn_tcp_option_space -= TCPOLEN_TSTAMP_ALIGNED;
13481348
syn_tcp_option_space -= TCPOLEN_WSCALE_ALIGNED;
1349-
if (tcp_ao_len(key) > syn_tcp_option_space) {
1349+
if (tcp_ao_len_aligned(key) > syn_tcp_option_space) {
13501350
err = -EMSGSIZE;
13511351
goto err_kfree;
13521352
}
@@ -1608,6 +1608,15 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family,
16081608
if (!dev || !l3index)
16091609
return -EINVAL;
16101610

1611+
if (!bound_dev_if || bound_dev_if != cmd.ifindex) {
1612+
/* tcp_ao_established_key() doesn't expect having
1613+
* non peer-matching key on an established TCP-AO
1614+
* connection.
1615+
*/
1616+
if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
1617+
return -EINVAL;
1618+
}
1619+
16111620
/* It's still possible to bind after adding keys or even
16121621
* re-bind to a different dev (with CAP_NET_RAW).
16131622
* So, no reason to return error here, rather try to be

net/ipv4/tcp_input.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7182,11 +7182,12 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
71827182
if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh))
71837183
goto drop_and_release; /* Invalid TCP options */
71847184
if (aoh) {
7185-
tcp_rsk(req)->maclen = aoh->length - sizeof(struct tcp_ao_hdr);
7185+
tcp_rsk(req)->used_tcp_ao = true;
71867186
tcp_rsk(req)->ao_rcv_next = aoh->keyid;
71877187
tcp_rsk(req)->ao_keyid = aoh->rnext_keyid;
7188+
71887189
} else {
7189-
tcp_rsk(req)->maclen = 0;
7190+
tcp_rsk(req)->used_tcp_ao = false;
71907191
}
71917192
#endif
71927193
tcp_rsk(req)->snt_isn = isn;

net/ipv4/tcp_ipv4.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ static bool tcp_v4_ao_sign_reset(const struct sock *sk, struct sk_buff *skb,
690690

691691
reply_options[0] = htonl((TCPOPT_AO << 24) | (tcp_ao_len(key) << 16) |
692692
(aoh->rnext_keyid << 8) | keyid);
693-
arg->iov[0].iov_len += round_up(tcp_ao_len(key), 4);
693+
arg->iov[0].iov_len += tcp_ao_len_aligned(key);
694694
reply->doff = arg->iov[0].iov_len / 4;
695695

696696
if (tcp_ao_hash_hdr(AF_INET, (char *)&reply_options[1],
@@ -978,7 +978,7 @@ static void tcp_v4_send_ack(const struct sock *sk,
978978
(tcp_ao_len(key->ao_key) << 16) |
979979
(key->ao_key->sndid << 8) |
980980
key->rcv_next);
981-
arg.iov[0].iov_len += round_up(tcp_ao_len(key->ao_key), 4);
981+
arg.iov[0].iov_len += tcp_ao_len_aligned(key->ao_key);
982982
rep.th.doff = arg.iov[0].iov_len / 4;
983983

984984
tcp_ao_hash_hdr(AF_INET, (char *)&rep.opt[offset],

net/ipv4/tcp_minisocks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,
615615
ao_key = treq->af_specific->ao_lookup(sk, req,
616616
tcp_rsk(req)->ao_keyid, -1);
617617
if (ao_key)
618-
newtp->tcp_header_len += tcp_ao_len(ao_key);
618+
newtp->tcp_header_len += tcp_ao_len_aligned(ao_key);
619619
#endif
620620
if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len)
621621
newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;

net/ipv4/tcp_output.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
825825
timestamps = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps);
826826
if (tcp_key_is_ao(key)) {
827827
opts->options |= OPTION_AO;
828-
remaining -= tcp_ao_len(key->ao_key);
828+
remaining -= tcp_ao_len_aligned(key->ao_key);
829829
}
830830
}
831831

@@ -915,7 +915,7 @@ static unsigned int tcp_synack_options(const struct sock *sk,
915915
ireq->tstamp_ok &= !ireq->sack_ok;
916916
} else if (tcp_key_is_ao(key)) {
917917
opts->options |= OPTION_AO;
918-
remaining -= tcp_ao_len(key->ao_key);
918+
remaining -= tcp_ao_len_aligned(key->ao_key);
919919
ireq->tstamp_ok &= !ireq->sack_ok;
920920
}
921921

@@ -982,7 +982,7 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb
982982
size += TCPOLEN_MD5SIG_ALIGNED;
983983
} else if (tcp_key_is_ao(key)) {
984984
opts->options |= OPTION_AO;
985-
size += tcp_ao_len(key->ao_key);
985+
size += tcp_ao_len_aligned(key->ao_key);
986986
}
987987

988988
if (likely(tp->rx_opt.tstamp_ok)) {
@@ -3720,7 +3720,6 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
37203720
if (tcp_rsk_used_ao(req)) {
37213721
#ifdef CONFIG_TCP_AO
37223722
struct tcp_ao_key *ao_key = NULL;
3723-
u8 maclen = tcp_rsk(req)->maclen;
37243723
u8 keyid = tcp_rsk(req)->ao_keyid;
37253724

37263725
ao_key = tcp_sk(sk)->af_specific->ao_lookup(sk, req_to_sk(req),
@@ -3730,13 +3729,11 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
37303729
* for another peer-matching key, but the peer has requested
37313730
* ao_keyid (RFC5925 RNextKeyID), so let's keep it simple here.
37323731
*/
3733-
if (unlikely(!ao_key || tcp_ao_maclen(ao_key) != maclen)) {
3734-
u8 key_maclen = ao_key ? tcp_ao_maclen(ao_key) : 0;
3735-
3732+
if (unlikely(!ao_key)) {
37363733
rcu_read_unlock();
37373734
kfree_skb(skb);
3738-
net_warn_ratelimited("TCP-AO: the keyid %u with maclen %u|%u from SYN packet is not present - not sending SYNACK\n",
3739-
keyid, maclen, key_maclen);
3735+
net_warn_ratelimited("TCP-AO: the keyid %u from SYN packet is not present - not sending SYNACK\n",
3736+
keyid);
37403737
return NULL;
37413738
}
37423739
key.ao_key = ao_key;

net/ipv6/tcp_ipv6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
881881
if (tcp_key_is_md5(key))
882882
tot_len += TCPOLEN_MD5SIG_ALIGNED;
883883
if (tcp_key_is_ao(key))
884-
tot_len += tcp_ao_len(key->ao_key);
884+
tot_len += tcp_ao_len_aligned(key->ao_key);
885885

886886
#ifdef CONFIG_MPTCP
887887
if (rst && !tcp_key_is_md5(key)) {

0 commit comments

Comments
 (0)