tcp: Reject invalid ack_seq to Fast Open sockets
Subsystems (3)
PATCH 1/1 tcp: Reject invalid ack_seq to Fast Open sockets
From: Jerry Chu <[email protected]>
A packet with an invalid ack_seq may cause a TCP Fast Open socket to switch to the unexpected TCP_CLOSING state, triggering a BUG_ON kernel panic.
When a FIN packet with an invalid ack_seq# arrives at a socket in the TCP_FIN_WAIT1 state, rather than discarding the packet, the current code will accept the FIN, causing state transition to TCP_CLOSING.
This may be a small deviation from RFC793, which seems to say that the packet should be dropped. Unfortunately I did not expect this case for Fast Open hence it will trigger a BUG_ON panic.
It turns out there is really nothing bad about a TFO socket going into TCP_CLOSING state so I could just remove the BUG_ON statements. But after some thought I think it's better to treat this case like TCP_SYN_RECV and return a RST to the confused peer who caused the unacceptable ack_seq to be generated in the first place.
--- net/ipv4/tcp_input.c | 12 ++++++++++-- net/ipv4/tcp_timer.c | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-)
net/ipv4/tcp_input.c +10 -2
@@ -5970,7 +5970,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, req = tp->fastopen_rsk;if (req !=NULL ) { -BUG_ON (sk->sk_state !=TCP_SYN_RECV && +WARN_ON_ONCE (sk->sk_state !=TCP_SYN_RECV && sk->sk_state !=TCP_FIN_WAIT1 );if (tcp_check_req (sk, skb, req,NULL , true) ==NULL )
@@ -6059,7 +6059,15 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,* ACK we have received, this would have acknowledged * our SYNACK so stop the SYNACK timer. */ -if (acceptable && req !=NULL ) { +if (req !=NULL ) { +/* Return RST if ack_seq is invalid. +* Note that RFC793 only says to generate a +* DUPACK for it but for TCP Fast Open it seems +* better to treat this case like TCP_SYN_RECV +* above. +*/ +if (!acceptable) +return 1 ;/* We no longer need the request sock. */ reqsk_fastopen_remove (sk, req, false);tcp_rearm_rto (sk);
net/ipv4/tcp_timer.c +1 -1
@@ -347,7 +347,7 @@ void tcp_retransmit_timer(struct sock *sk)return ; }if (tp->fastopen_rsk) { -BUG_ON (sk->sk_state !=TCP_SYN_RECV && +WARN_ON_ONCE (sk->sk_state !=TCP_SYN_RECV && sk->sk_state !=TCP_FIN_WAIT1 );tcp_fastopen_synack_timer (sk);/* Before we receive ACK to our SYN-ACK don't retransmit
Signature · 1 line
1.7.7.3
Quoted 19 lines · H.K. Jerry Chu
From: Jerry Chu <[email protected]>
A packet with an invalid
ack_seqmay cause a TCP Fast Open socket to switch to the unexpectedTCP_CLOSINGstate, triggering aBUG_ONkernel panic.When a FIN packet with an invalid ack_seq# arrives at a socket in the
TCP_FIN_WAIT1state, rather than discarding the packet, the current code will accept the FIN, causing state transition toTCP_CLOSING.This may be a small deviation from RFC793, which seems to say that the packet should be dropped. Unfortunately I did not expect this case for Fast Open hence it will trigger a
BUG_ONpanic.It turns out there is really nothing bad about a TFO socket going into
TCP_CLOSINGstate so I could just remove theBUG_ONstatements. But after some thought I think it's better to treat this case likeTCP_SYN_RECVand return a RST to the confused peer who caused the unacceptableack_seqto be generated in the first place.
Quoted 3 lines
Signed-off-by H.K. Jerry Chu <[email protected]>Cc Neal Cardwell <[email protected]>Cc Yuchung Cheng <[email protected]>
Quoted 51 lines
--- net/ipv4/tcp_input.c | 12 ++++++++++-- net/ipv4/tcp_timer.c | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-)net/ipv4/tcp_input.c +10
-2@@ -5970,7 +5970,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, req = tp->fastopen_rsk;if (req !=NULL ) { -BUG_ON (sk->sk_state !=TCP_SYN_RECV && +WARN_ON_ONCE (sk->sk_state !=TCP_SYN_RECV && sk->sk_state !=TCP_FIN_WAIT1 );if (tcp_check_req (sk, skb, req,NULL , true) ==NULL )@@ -6059,7 +6059,15 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,* ACK we have received, this would have acknowledged * our SYNACK so stop the SYNACK timer. */ -if (acceptable && req !=NULL ) { +if (req !=NULL ) { +/* Return RST if ack_seq is invalid. +* Note that RFC793 only says to generate a +* DUPACK for it but for TCP Fast Open it seems +* better to treat this case like TCP_SYN_RECV +* above. +*/ +if (!acceptable) +return 1 ;/* We no longer need the request sock. */ reqsk_fastopen_remove (sk, req, false);tcp_rearm_rto (sk);net/ipv4/tcp_timer.c +1
-2@@ -347,7 +347,7 @@ void tcp_retransmit_timer(struct sock *sk)return ; }if (tp->fastopen_rsk) { -BUG_ON (sk->sk_state !=TCP_SYN_RECV && +WARN_ON_ONCE (sk->sk_state !=TCP_SYN_RECV && sk->sk_state !=TCP_FIN_WAIT1 );tcp_fastopen_synack_timer (sk);/* Before we receive ACK to our SYN-ACK don't retransmit -- 1.7.7.3
Quoted 19 lines · H.K. Jerry Chu
From: Jerry Chu <[email protected]>
A packet with an invalid
ack_seqmay cause a TCP Fast Open socket to switch to the unexpectedTCP_CLOSINGstate, triggering aBUG_ONkernel panic.When a FIN packet with an invalid ack_seq# arrives at a socket in the
TCP_FIN_WAIT1state, rather than discarding the packet, the current code will accept the FIN, causing state transition toTCP_CLOSING.This may be a small deviation from RFC793, which seems to say that the packet should be dropped. Unfortunately I did not expect this case for Fast Open hence it will trigger a
BUG_ONpanic.It turns out there is really nothing bad about a TFO socket going into
TCP_CLOSINGstate so I could just remove theBUG_ONstatements. But after some thought I think it's better to treat this case likeTCP_SYN_RECVand return a RST to the confused peer who caused the unacceptableack_seqto be generated in the first place.
Quoted 4 lines
Signed-off-by H.K. Jerry Chu <[email protected]>Cc Neal Cardwell <[email protected]>Cc Yuchung Cheng <[email protected]>---
Quoted 19 lines · H.K. Jerry Chu
From: Jerry Chu <[email protected]>
A packet with an invalid
ack_seqmay cause a TCP Fast Open socket to switch to the unexpectedTCP_CLOSINGstate, triggering aBUG_ONkernel panic.When a FIN packet with an invalid ack_seq# arrives at a socket in the
TCP_FIN_WAIT1state, rather than discarding the packet, the current code will accept the FIN, causing state transition toTCP_CLOSING.This may be a small deviation from RFC793, which seems to say that the packet should be dropped. Unfortunately I did not expect this case for Fast Open hence it will trigger a
BUG_ONpanic.It turns out there is really nothing bad about a TFO socket going into
TCP_CLOSINGstate so I could just remove theBUG_ONstatements. But after some thought I think it's better to treat this case likeTCP_SYN_RECVand return a RST to the confused peer who caused the unacceptableack_seqto be generated in the first place.
Quoted 3 lines
Signed-off-by H.K. Jerry Chu <[email protected]>Cc Neal Cardwell <[email protected]>Cc Yuchung Cheng <[email protected]>
From: "H.K. Jerry Chu" <[email protected]>
Date: Mon, 22 Oct 2012 14:26:36 -0700
Quoted 13 lines
From: Jerry Chu <[email protected]>
A packet with an invalid
ack_seqmay cause a TCP Fast Open socket to switch to the unexpectedTCP_CLOSINGstate, triggering aBUG_ONkernel panic.When a FIN packet with an invalid ack_seq# arrives at a socket in the
TCP_FIN_WAIT1state, rather than discarding the packet, the current code will accept the FIN, causing state transition toTCP_CLOSING.This may be a small deviation from RFC793, which seems to say that the packet should be dropped. Unfortunately I did not expect this case for Fast Open hence it will trigger a
BUG_ONpanic.
Quoted 7 lines
It turns out there is really nothing bad about a TFO socket going into
TCP_CLOSINGstate so I could just remove theBUG_ONstatements. But after some thought I think it's better to treat this case likeTCP_SYN_RECVand return a RST to the confused peer who caused the unacceptableack_seqto be generated in the first place.Signed-off-by H.K. Jerry Chu <[email protected]>
Applied, thanks.
net/ipv4/tcp_input.c int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, · line 5974
Quoted 3 lines
net/ipv4/tcp_input.c +1
-1-BUG_ON (sk->sk_state !=TCP_SYN_RECV && +WARN_ON_ONCE (sk->sk_state !=TCP_SYN_RECV && sk->sk_state !=TCP_FIN_WAIT1 );
I fixed up the indentation of the second line of the test when I applied this.