Similarly to IPsec, RFC5925 prescribes: ">> A TCP-AO implementation MUST default to ignore incoming ICMPv4 messages of Type 3 (destination unreachable), Codes 2-4 (protocol unreachable, port unreachable, and fragmentation needed -- ’hard errors’), and ICMPv6 Type 1 (destination unreachable), Code 1 (administratively prohibited) and Code 4 (port unreachable) intended for connections in synchronized states (ESTABLISHED, FIN-WAIT-1, FIN- WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT) that match MKTs." A selftest (later in patch series) verifies that this attack is not possible in this TCP-AO implementation.
Unified · Split
| @@ -24,6 +24,7 @@ struct tcp_ao_counters { · 23 unmodified lines | ||
| 24 | 24 | atomic64_t pkt_bad; |
| 25 | 25 | atomic64_t key_not_found; |
| 26 | 26 | atomic64_t ao_required; |
| 27 | + atomic64_t dropped_icmp; | |
| 27 | 28 | }; |
| 28 | 29 | |
| 29 | 30 | struct tcp_ao_key { |
| @@ -92,7 +93,8 @@ struct tcp_ao_info { · 62 unmodified lines | ||
| 92 | 93 | struct tcp_ao_key *rnext_key; |
| 93 | 94 | struct tcp_ao_counters counters; |
| 94 | 95 | u32 ao_required :1, |
| 95 | − __unused :31; | |
| 96 | + accept_icmps :1, | |
| 97 | + __unused :30; | |
| 96 | 98 | __be32 lisn; |
| 97 | 99 | __be32 risn; |
| 98 | 100 | /* Sequence Number Extension (SNE) are upper 4 bytes for SEQ, |
| @@ -191,6 +193,7 @@ int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, · 92 unmodified lines | ||
| 191 | 193 | unsigned int len, struct tcp_sigpool *hp); |
| 192 | 194 | void tcp_ao_destroy_sock(struct sock *sk, bool twsk); |
| 193 | 195 | void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp); |
| 196 | +bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code); | |
| 194 | 197 | enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk, |
| 195 | 198 | const struct sk_buff *skb, unsigned short int family, |
| 196 | 199 | const struct request_sock *req, |
| @@ -274,6 +277,12 @@ static inline void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, · 77 unmodified lines | ||
| 274 | 277 | { |
| 275 | 278 | } |
| 276 | 279 | |
| 280 | +static inline bool tcp_ao_ignore_icmp(const struct sock *sk, int family, | |
| 281 | + int type, int code) | |
| 282 | +{ | |
| 283 | + return false; | |
| 284 | +} | |
| 285 | + | |
| 277 | 286 | static inline enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk, |
| 278 | 287 | const struct sk_buff *skb, unsigned short int family, |
| 279 | 288 | const struct request_sock *req, const struct tcp_ao_hdr *aoh) |
| @@ -301,6 +301,7 @@ enum · 300 unmodified lines | ||
| 301 | 301 | LINUX_MIB_TCPAOBAD, /* TCPAOBad */ |
| 302 | 302 | LINUX_MIB_TCPAOKEYNOTFOUND, /* TCPAOKeyNotFound */ |
| 303 | 303 | LINUX_MIB_TCPAOGOOD, /* TCPAOGood */ |
| 304 | + LINUX_MIB_TCPAODROPPEDICMPS, /* TCPAODroppedIcmps */ | |
| 304 | 305 | __LINUX_MIB_MAX |
| 305 | 306 | }; |
| 306 | 307 | |
| @@ -405,7 +405,8 @@ struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO) */ · 404 unmodified lines | ||
| 405 | 405 | set_rnext :1, /* corresponding ::rnext */ |
| 406 | 406 | ao_required :1, /* don't accept non-AO connects */ |
| 407 | 407 | set_counters :1, /* set/clear ::pkt_* counters */ |
| 408 | − reserved :28; /* must be 0 */ | |
| 408 | + accept_icmps :1, /* accept incoming ICMPs */ | |
| 409 | + reserved :27; /* must be 0 */ | |
| 409 | 410 | __u16 reserved2; /* padding, must be 0 */ |
| 410 | 411 | __u8 current_key; /* KeyID to set as Current_key */ |
| 411 | 412 | __u8 rnext; /* KeyID to set as Rnext_key */ |
| @@ -413,6 +414,7 @@ struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO) */ · 1 unmodified line | ||
| 413 | 414 | __u64 pkt_bad; /* failed verification */ |
| 414 | 415 | __u64 pkt_key_not_found; /* could not find a key to verify */ |
| 415 | 416 | __u64 pkt_ao_required; /* segments missing TCP-AO sign */ |
| 417 | + __u64 pkt_dropped_icmp; /* ICMPs that were ignored */ | |
| 416 | 418 | } __attribute__((aligned(8))); |
| 417 | 419 | |
| 418 | 420 | /* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */ |
| @@ -303,6 +303,7 @@ static const struct snmp_mib snmp4_net_list[] = { · 302 unmodified lines | ||
| 303 | 303 | SNMP_MIB_ITEM("TCPAOBad", LINUX_MIB_TCPAOBAD), |
| 304 | 304 | SNMP_MIB_ITEM("TCPAOKeyNotFound", LINUX_MIB_TCPAOKEYNOTFOUND), |
| 305 | 305 | SNMP_MIB_ITEM("TCPAOGood", LINUX_MIB_TCPAOGOOD), |
| 306 | + SNMP_MIB_ITEM("TCPAODroppedIcmps", LINUX_MIB_TCPAODROPPEDICMPS), | |
| 306 | 307 | SNMP_MIB_SENTINEL |
| 307 | 308 | }; |
| 308 | 309 | |
| @@ -15,6 +15,7 @@ · 14 unmodified lines | ||
| 15 | 15 | |
| 16 | 16 | #include <net/tcp.h> |
| 17 | 17 | #include <net/ipv6.h> |
| 18 | +#include <net/icmp.h> | |
| 18 | 19 | |
| 19 | 20 | int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, |
| 20 | 21 | unsigned int len, struct tcp_sigpool *hp) |
| @@ -44,6 +45,60 @@ clear_hash: · 23 unmodified lines | ||
| 44 | 45 | return 1; |
| 45 | 46 | } |
| 46 | 47 | |
| 48 | +bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code) | |
| 49 | +{ | |
| 50 | + bool ignore_icmp = false; | |
| 51 | + struct tcp_ao_info *ao; | |
| 52 | + | |
| 53 | + /* RFC5925, 7.8: | |
| 54 | + * >> A TCP-AO implementation MUST default to ignore incoming ICMPv4 | |
| 55 | + * messages of Type 3 (destination unreachable), Codes 2-4 (protocol | |
| 56 | + * unreachable, port unreachable, and fragmentation needed -- ’hard | |
| 57 | + * errors’), and ICMPv6 Type 1 (destination unreachable), Code 1 | |
| 58 | + * (administratively prohibited) and Code 4 (port unreachable) intended | |
| 59 | + * for connections in synchronized states (ESTABLISHED, FIN-WAIT-1, FIN- | |
| 60 | + * WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT) that match MKTs. | |
| 61 | + */ | |
| 62 | + if (family == AF_INET) { | |
| 63 | + if (type != ICMP_DEST_UNREACH) | |
| 64 | + return false; | |
| 65 | + if (code < ICMP_PROT_UNREACH || code > ICMP_FRAG_NEEDED) | |
| 66 | + return false; | |
| 67 | + } else { | |
| 68 | + if (type != ICMPV6_DEST_UNREACH) | |
| 69 | + return false; | |
| 70 | + if (code != ICMPV6_ADM_PROHIBITED && code != ICMPV6_PORT_UNREACH) | |
| 71 | + return false; | |
| 72 | + } | |
| 73 | + | |
| 74 | + rcu_read_lock(); | |
| 75 | + switch (sk->sk_state) { | |
| 76 | + case TCP_TIME_WAIT: | |
| 77 | + ao = rcu_dereference(tcp_twsk(sk)->ao_info); | |
| 78 | + break; | |
| 79 | + case TCP_SYN_SENT: | |
| 80 | + case TCP_SYN_RECV: | |
| 81 | + case TCP_LISTEN: | |
| 82 | + case TCP_NEW_SYN_RECV: | |
| 83 | + /* RFC5925 specifies to ignore ICMPs *only* on connections | |
| 84 | + * in synchronized states. | |
| 85 | + */ | |
| 86 | + rcu_read_unlock(); | |
| 87 | + return false; | |
| 88 | + default: | |
| 89 | + ao = rcu_dereference(tcp_sk(sk)->ao_info); | |
| 90 | + } | |
| 91 | + | |
| 92 | + if (ao && !ao->accept_icmps) { | |
| 93 | + ignore_icmp = true; | |
| 94 | + __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAODROPPEDICMPS); | |
| 95 | + atomic64_inc(&ao->counters.dropped_icmp); | |
| 96 | + } | |
| 97 | + rcu_read_unlock(); | |
| 98 | + | |
| 99 | + return ignore_icmp; | |
| 100 | +} | |
| 101 | + | |
| 47 | 102 | /* Optimized version of tcp_ao_do_lookup(): only for sockets for which |
| 48 | 103 | * it's known that the keys in ao_info are matching peer's |
| 49 | 104 | * family/address/VRF/etc. |
| @@ -1086,6 +1141,7 @@ int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk, · 1036 unmodified lines | ||
| 1086 | 1141 | new_ao->lisn = htonl(tcp_rsk(req)->snt_isn); |
| 1087 | 1142 | new_ao->risn = htonl(tcp_rsk(req)->rcv_isn); |
| 1088 | 1143 | new_ao->ao_required = ao->ao_required; |
| 1144 | + new_ao->accept_icmps = ao->accept_icmps; | |
| 1089 | 1145 | |
| 1090 | 1146 | if (family == AF_INET) { |
| 1091 | 1147 | addr = (union tcp_ao_addr *)&newsk->sk_daddr; |
| @@ -1792,9 +1848,11 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family, · 700 unmodified lines | ||
| 1792 | 1848 | atomic64_set(&ao_info->counters.pkt_bad, cmd.pkt_bad); |
| 1793 | 1849 | atomic64_set(&ao_info->counters.key_not_found, cmd.pkt_key_not_found); |
| 1794 | 1850 | atomic64_set(&ao_info->counters.ao_required, cmd.pkt_ao_required); |
| 1851 | + atomic64_set(&ao_info->counters.dropped_icmp, cmd.pkt_dropped_icmp); | |
| 1795 | 1852 | } |
| 1796 | 1853 | |
| 1797 | 1854 | ao_info->ao_required = cmd.ao_required; |
| 1855 | + ao_info->accept_icmps = cmd.accept_icmps; | |
| 1798 | 1856 | if (new_current) |
| 1799 | 1857 | WRITE_ONCE(ao_info->current_key, new_current); |
| 1800 | 1858 | if (new_rnext) |
| @@ -494,6 +494,8 @@ int tcp_v4_err(struct sk_buff *skb, u32 info) · 493 unmodified lines | ||
| 494 | 494 | return -ENOENT; |
| 495 | 495 | } |
| 496 | 496 | if (sk->sk_state == TCP_TIME_WAIT) { |
| 497 | + /* To increase the counter of ignored icmps for TCP-AO */ | |
| 498 | + tcp_ao_ignore_icmp(sk, AF_INET, type, code); | |
| 497 | 499 | inet_twsk_put(inet_twsk(sk)); |
| 498 | 500 | return 0; |
| 499 | 501 | } |
| @@ -507,6 +509,11 @@ int tcp_v4_err(struct sk_buff *skb, u32 info) · 7 unmodified lines | ||
| 507 | 509 | return 0; |
| 508 | 510 | } |
| 509 | 511 | |
| 512 | + if (tcp_ao_ignore_icmp(sk, AF_INET, type, code)) { | |
| 513 | + sock_put(sk); | |
| 514 | + return 0; | |
| 515 | + } | |
| 516 | + | |
| 510 | 517 | bh_lock_sock(sk); |
| 511 | 518 | /* If too many ICMPs get dropped on busy |
| 512 | 519 | * servers this needs to be solved differently. |
| @@ -396,6 +396,8 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, · 395 unmodified lines | ||
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 398 | if (sk->sk_state == TCP_TIME_WAIT) { |
| 399 | + /* To increase the counter of ignored icmps for TCP-AO */ | |
| 400 | + tcp_ao_ignore_icmp(sk, AF_INET6, type, code); | |
| 399 | 401 | inet_twsk_put(inet_twsk(sk)); |
| 400 | 402 | return 0; |
| 401 | 403 | } |
| @@ -406,6 +408,11 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, · 4 unmodified lines | ||
| 406 | 408 | return 0; |
| 407 | 409 | } |
| 408 | 410 | |
| 411 | + if (tcp_ao_ignore_icmp(sk, AF_INET6, type, code)) { | |
| 412 | + sock_put(sk); | |
| 413 | + return 0; | |
| 414 | + } | |
| 415 | + | |
| 409 | 416 | bh_lock_sock(sk); |
| 410 | 417 | if (sock_owned_by_user(sk) && type != ICMPV6_PKT_TOOBIG) |
| 411 | 418 | __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS); |