Skip to content

Commit 33700a0

Browse files
0x7f454c46kuba-moo
authored andcommitted
net/tcp: Don't consider TCP_CLOSE in TCP_AO_ESTABLISHED
TCP_CLOSE may or may not have current/rnext keys and should not be considered "established". The fast-path for TCP_CLOSE is SKB_DROP_REASON_TCP_CLOSE. This is what tcp_rcv_state_process() does anyways. Add an early drop path to not spend any time verifying segment signatures for sockets in TCP_CLOSE state. Cc: stable@vger.kernel.org # v6.7 Fixes: 0a3a809 ("net/tcp: Verify inbound TCP-AO signed segments") Signed-off-by: Dmitry Safonov <0x7f454c46@gmail.com> Link: https://lore.kernel.org/r/20240529-tcp_ao-sk_state-v1-1-d69b5d323c52@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent e85e271 commit 33700a0

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

include/net/tcp_ao.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ static inline int tcp_ao_sizeof_key(const struct tcp_ao_key *key)
8686
struct tcp_ao_info {
8787
/* List of tcp_ao_key's */
8888
struct hlist_head head;
89-
/* current_key and rnext_key aren't maintained on listen sockets.
89+
/* current_key and rnext_key are maintained on sockets
90+
* in TCP_AO_ESTABLISHED states.
9091
* Their purpose is to cache keys on established connections,
9192
* saving needless lookups. Never dereference any of them from
9293
* listen sockets.
@@ -201,9 +202,9 @@ struct tcp6_ao_context {
201202
};
202203

203204
struct tcp_sigpool;
205+
/* Established states are fast-path and there always is current_key/rnext_key */
204206
#define TCP_AO_ESTABLISHED (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | \
205-
TCPF_CLOSE | TCPF_CLOSE_WAIT | \
206-
TCPF_LAST_ACK | TCPF_CLOSING)
207+
TCPF_CLOSE_WAIT | TCPF_LAST_ACK | TCPF_CLOSING)
207208

208209
int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
209210
struct tcp_ao_key *key, struct tcphdr *th,

net/ipv4/tcp_ao.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb,
933933
struct tcp_ao_key *key;
934934
__be32 sisn, disn;
935935
u8 *traffic_key;
936+
int state;
936937
u32 sne = 0;
937938

938939
info = rcu_dereference(tcp_sk(sk)->ao_info);
@@ -948,8 +949,9 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb,
948949
disn = 0;
949950
}
950951

952+
state = READ_ONCE(sk->sk_state);
951953
/* Fast-path */
952-
if (likely((1 << sk->sk_state) & TCP_AO_ESTABLISHED)) {
954+
if (likely((1 << state) & TCP_AO_ESTABLISHED)) {
953955
enum skb_drop_reason err;
954956
struct tcp_ao_key *current_key;
955957

@@ -988,6 +990,9 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb,
988990
return SKB_NOT_DROPPED_YET;
989991
}
990992

993+
if (unlikely(state == TCP_CLOSE))
994+
return SKB_DROP_REASON_TCP_CLOSE;
995+
991996
/* Lookup key based on peer address and keyid.
992997
* current_key and rnext_key must not be used on tcp listen
993998
* sockets as otherwise:
@@ -1001,7 +1006,7 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb,
10011006
if (th->syn && !th->ack)
10021007
goto verify_hash;
10031008

1004-
if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) {
1009+
if ((1 << state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) {
10051010
/* Make the initial syn the likely case here */
10061011
if (unlikely(req)) {
10071012
sne = tcp_ao_compute_sne(0, tcp_rsk(req)->rcv_isn,
@@ -1018,14 +1023,14 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb,
10181023
/* no way to figure out initial sisn/disn - drop */
10191024
return SKB_DROP_REASON_TCP_FLAGS;
10201025
}
1021-
} else if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1026+
} else if ((1 << state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
10221027
disn = info->lisn;
10231028
if (th->syn || th->rst)
10241029
sisn = th->seq;
10251030
else
10261031
sisn = info->risn;
10271032
} else {
1028-
WARN_ONCE(1, "TCP-AO: Unexpected sk_state %d", sk->sk_state);
1033+
WARN_ONCE(1, "TCP-AO: Unexpected sk_state %d", state);
10291034
return SKB_DROP_REASON_TCP_AOFAILURE;
10301035
}
10311036
verify_hash:

0 commit comments

Comments
 (0)