History

net/tcp: Ignore specific ICMPs for TCP-AO connections

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.

Dmitry Safonov committed 3 years ago

Commit 953af8e1 parent 2717b5a7 files · +87 −2Browse files

Unified · Split

7 files
  1. include/net/tcp_ao.h +10 −1
  2. include/uapi/linux/snmp.h +1 −0
  3. include/uapi/linux/tcp.h +3 −1
  4. net/ipv4/proc.c +1 −0
  5. net/ipv4/tcp_ao.c +58 −0
  6. net/ipv4/tcp_ipv4.c +7 −0
  7. net/ipv6/tcp_ipv6.c +7 −0
include/net/tcp_ao.h+10−1
@@ -24,6 +24,7 @@ struct tcp_ao_counters { · 23 unmodified lines
24 atomic64_t pkt_bad;24 atomic64_t pkt_bad;
25 atomic64_t key_not_found;25 atomic64_t key_not_found;
26 atomic64_t ao_required;26 atomic64_t ao_required;
27 atomic64_t dropped_icmp;
27};28};
28 29
29struct tcp_ao_key {30struct tcp_ao_key {
@@ -92,7 +93,8 @@ struct tcp_ao_info { · 62 unmodified lines
92 struct tcp_ao_key *rnext_key;93 struct tcp_ao_key *rnext_key;
93 struct tcp_ao_counters counters;94 struct tcp_ao_counters counters;
94 u32 ao_required :1,95 u32 ao_required :1,
95 __unused :31;96 accept_icmps :1,
97 __unused :30;
96 __be32 lisn;98 __be32 lisn;
97 __be32 risn;99 __be32 risn;
98 /* Sequence Number Extension (SNE) are upper 4 bytes for SEQ,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 unsigned int len, struct tcp_sigpool *hp);193 unsigned int len, struct tcp_sigpool *hp);
192void tcp_ao_destroy_sock(struct sock *sk, bool twsk);194void tcp_ao_destroy_sock(struct sock *sk, bool twsk);
193void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp);195void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp);
196bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code);
194enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,197enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,
195 const struct sk_buff *skb, unsigned short int family,198 const struct sk_buff *skb, unsigned short int family,
196 const struct request_sock *req,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
280static inline bool tcp_ao_ignore_icmp(const struct sock *sk, int family,
281 int type, int code)
282{
283 return false;
284}
285
277static inline enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,286static inline enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,
278 const struct sk_buff *skb, unsigned short int family,287 const struct sk_buff *skb, unsigned short int family,
279 const struct request_sock *req, const struct tcp_ao_hdr *aoh)288 const struct request_sock *req, const struct tcp_ao_hdr *aoh)
include/uapi/linux/snmp.h+1−0
@@ -301,6 +301,7 @@ enum · 300 unmodified lines
301 LINUX_MIB_TCPAOBAD, /* TCPAOBad */301 LINUX_MIB_TCPAOBAD, /* TCPAOBad */
302 LINUX_MIB_TCPAOKEYNOTFOUND, /* TCPAOKeyNotFound */302 LINUX_MIB_TCPAOKEYNOTFOUND, /* TCPAOKeyNotFound */
303 LINUX_MIB_TCPAOGOOD, /* TCPAOGood */303 LINUX_MIB_TCPAOGOOD, /* TCPAOGood */
304 LINUX_MIB_TCPAODROPPEDICMPS, /* TCPAODroppedIcmps */
304 __LINUX_MIB_MAX305 __LINUX_MIB_MAX
305};306};
306 307
include/uapi/linux/tcp.h+3−1
@@ -405,7 +405,8 @@ struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO) */ · 404 unmodified lines
405 set_rnext :1, /* corresponding ::rnext */405 set_rnext :1, /* corresponding ::rnext */
406 ao_required :1, /* don't accept non-AO connects */406 ao_required :1, /* don't accept non-AO connects */
407 set_counters :1, /* set/clear ::pkt_* counters */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 __u16 reserved2; /* padding, must be 0 */410 __u16 reserved2; /* padding, must be 0 */
410 __u8 current_key; /* KeyID to set as Current_key */411 __u8 current_key; /* KeyID to set as Current_key */
411 __u8 rnext; /* KeyID to set as Rnext_key */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 __u64 pkt_bad; /* failed verification */414 __u64 pkt_bad; /* failed verification */
414 __u64 pkt_key_not_found; /* could not find a key to verify */415 __u64 pkt_key_not_found; /* could not find a key to verify */
415 __u64 pkt_ao_required; /* segments missing TCP-AO sign */416 __u64 pkt_ao_required; /* segments missing TCP-AO sign */
417 __u64 pkt_dropped_icmp; /* ICMPs that were ignored */
416} __attribute__((aligned(8)));418} __attribute__((aligned(8)));
417 419
418/* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */420/* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */
net/ipv4/proc.c+1−0
@@ -303,6 +303,7 @@ static const struct snmp_mib snmp4_net_list[] = { · 302 unmodified lines
303 SNMP_MIB_ITEM("TCPAOBad", LINUX_MIB_TCPAOBAD),303 SNMP_MIB_ITEM("TCPAOBad", LINUX_MIB_TCPAOBAD),
304 SNMP_MIB_ITEM("TCPAOKeyNotFound", LINUX_MIB_TCPAOKEYNOTFOUND),304 SNMP_MIB_ITEM("TCPAOKeyNotFound", LINUX_MIB_TCPAOKEYNOTFOUND),
305 SNMP_MIB_ITEM("TCPAOGood", LINUX_MIB_TCPAOGOOD),305 SNMP_MIB_ITEM("TCPAOGood", LINUX_MIB_TCPAOGOOD),
306 SNMP_MIB_ITEM("TCPAODroppedIcmps", LINUX_MIB_TCPAODROPPEDICMPS),
306 SNMP_MIB_SENTINEL307 SNMP_MIB_SENTINEL
307};308};
308 309
net/ipv4/tcp_ao.c+58−0
@@ -15,6 +15,7 @@ · 14 unmodified lines
15 15
16#include <net/tcp.h>16#include <net/tcp.h>
17#include <net/ipv6.h>17#include <net/ipv6.h>
18#include <net/icmp.h>
18 19
19int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,20int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,
20 unsigned int len, struct tcp_sigpool *hp)21 unsigned int len, struct tcp_sigpool *hp)
@@ -44,6 +45,60 @@ clear_hash: · 23 unmodified lines
44 return 1;45 return 1;
45}46}
46 47
48bool 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/* Optimized version of tcp_ao_do_lookup(): only for sockets for which102/* Optimized version of tcp_ao_do_lookup(): only for sockets for which
48 * it's known that the keys in ao_info are matching peer's103 * it's known that the keys in ao_info are matching peer's
49 * family/address/VRF/etc.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 new_ao->lisn = htonl(tcp_rsk(req)->snt_isn);1141 new_ao->lisn = htonl(tcp_rsk(req)->snt_isn);
1087 new_ao->risn = htonl(tcp_rsk(req)->rcv_isn);1142 new_ao->risn = htonl(tcp_rsk(req)->rcv_isn);
1088 new_ao->ao_required = ao->ao_required;1143 new_ao->ao_required = ao->ao_required;
1144 new_ao->accept_icmps = ao->accept_icmps;
1089 1145
1090 if (family == AF_INET) {1146 if (family == AF_INET) {
1091 addr = (union tcp_ao_addr *)&newsk->sk_daddr;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 atomic64_set(&ao_info->counters.pkt_bad, cmd.pkt_bad);1848 atomic64_set(&ao_info->counters.pkt_bad, cmd.pkt_bad);
1793 atomic64_set(&ao_info->counters.key_not_found, cmd.pkt_key_not_found);1849 atomic64_set(&ao_info->counters.key_not_found, cmd.pkt_key_not_found);
1794 atomic64_set(&ao_info->counters.ao_required, cmd.pkt_ao_required);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 ao_info->ao_required = cmd.ao_required;1854 ao_info->ao_required = cmd.ao_required;
1855 ao_info->accept_icmps = cmd.accept_icmps;
1798 if (new_current)1856 if (new_current)
1799 WRITE_ONCE(ao_info->current_key, new_current);1857 WRITE_ONCE(ao_info->current_key, new_current);
1800 if (new_rnext)1858 if (new_rnext)
net/ipv4/tcp_ipv4.c+7−0
@@ -494,6 +494,8 @@ int tcp_v4_err(struct sk_buff *skb, u32 info) · 493 unmodified lines
494 return -ENOENT;494 return -ENOENT;
495 }495 }
496 if (sk->sk_state == TCP_TIME_WAIT) {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 inet_twsk_put(inet_twsk(sk));499 inet_twsk_put(inet_twsk(sk));
498 return 0;500 return 0;
499 }501 }
@@ -507,6 +509,11 @@ int tcp_v4_err(struct sk_buff *skb, u32 info) · 7 unmodified lines
507 return 0;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 bh_lock_sock(sk);517 bh_lock_sock(sk);
511 /* If too many ICMPs get dropped on busy518 /* If too many ICMPs get dropped on busy
512 * servers this needs to be solved differently.519 * servers this needs to be solved differently.
net/ipv6/tcp_ipv6.c+7−0
@@ -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 if (sk->sk_state == TCP_TIME_WAIT) {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 inet_twsk_put(inet_twsk(sk));401 inet_twsk_put(inet_twsk(sk));
400 return 0;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 return 0;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 bh_lock_sock(sk);416 bh_lock_sock(sk);
410 if (sock_owned_by_user(sk) && type != ICMPV6_PKT_TOOBIG)417 if (sock_owned_by_user(sk) && type != ICMPV6_PKT_TOOBIG)
411 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);418 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t