[PATCH 4.4 075/190] tcp: better validation of received ack sequences

2018-04-11 Thread Greg Kroah-Hartman
4.4-stable review patch.  If anyone has any objections, please let me know.

--

From: Eric Dumazet 


[ Upstream commit d0e1a1b5a833b625c93d3d49847609350ebd79db ]

Paul Fiterau Brostean reported :


Linux TCP stack we analyze exhibits behavior that seems odd to me.
The scenario is as follows (all packets have empty payloads, no window
scaling, rcv/snd window size should not be a factor):

   TEST HARNESS (CLIENT)LINUX SERVER

   1.  -  LISTEN (server listen,
then accepts)

   2.  - --> 

[PATCH 4.4 075/190] tcp: better validation of received ack sequences

2018-04-11 Thread Greg Kroah-Hartman
4.4-stable review patch.  If anyone has any objections, please let me know.

--

From: Eric Dumazet 


[ Upstream commit d0e1a1b5a833b625c93d3d49847609350ebd79db ]

Paul Fiterau Brostean reported :


Linux TCP stack we analyze exhibits behavior that seems odd to me.
The scenario is as follows (all packets have empty payloads, no window
scaling, rcv/snd window size should not be a factor):

   TEST HARNESS (CLIENT)LINUX SERVER

   1.  -  LISTEN (server listen,
then accepts)

   2.  - -->--> SYN-RECEIVED

   3.  - <--   <-- SYN-RECEIVED

   4.  - -->   --> ESTABLISHED

   5.  - <--   <-- FIN WAIT-1 (server
opts to close the data connection calling "close" on the connection
socket)

   6.  - -->  --> CLOSING (client sends
FIN,ACK with not yet sent acknowledgement number)

   7.  - <--   <-- CLOSING (ACK is 102
instead of 101, why?)

... (silence from CLIENT)

   8.  - <--   <-- CLOSING
(retransmission, again ACK is 102)

Now, note that packet 6 while having the expected sequence number,
acknowledges something that wasn't sent by the server. So I would
expect
the packet to maybe prompt an ACK response from the server, and then be
ignored. Yet it is not ignored and actually leads to an increase of the
acknowledgement number in the server's retransmission of the FIN,ACK
packet. The explanation I found is that the FIN  in packet 6 was
processed, despite the acknowledgement number being unacceptable.
Further experiments indeed show that the server processes this FIN,
transitioning to CLOSING, then on receiving an ACK for the FIN it had
send in packet 5, the server (or better said connection) transitions
from CLOSING to TIME_WAIT (as signaled by netstat).



Indeed, tcp_rcv_state_process() calls tcp_ack() but
does not exploit the @acceptable status but for TCP_SYN_RECV
state.

What we want here is to send a challenge ACK, if not in TCP_SYN_RECV
state. TCP_FIN_WAIT1 state is not the only state we should fix.

Add a FLAG_NO_CHALLENGE_ACK so that tcp_rcv_state_process()
can choose to send a challenge ACK and discard the packet instead
of wrongly change socket state.

With help from Neal Cardwell.

Signed-off-by: Eric Dumazet 
Reported-by: Paul Fiterau Brostean 
Cc: Neal Cardwell 
Cc: Yuchung Cheng 
Cc: Soheil Hassas Yeganeh 
Signed-off-by: David S. Miller 
Signed-off-by: Sasha Levin 
Signed-off-by: Greg Kroah-Hartman 
---
 net/ipv4/tcp_input.c |   24 +++-
 1 file changed, 11 insertions(+), 13 deletions(-)

--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -117,6 +117,7 @@ int sysctl_tcp_invalid_ratelimit __read_
 #define FLAG_DSACKING_ACK  0x800 /* SACK blocks contained D-SACK info */
 #define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
 #define FLAG_UPDATE_TS_RECENT  0x4000 /* tcp_replace_ts_recent() */
+#define FLAG_NO_CHALLENGE_ACK  0x8000 /* do not call tcp_send_challenge_ack()  
*/
 
 #define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
 #define FLAG_NOT_DUP   (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
@@ -3543,7 +3544,8 @@ static int tcp_ack(struct sock *sk, cons
if (before(ack, prior_snd_una)) {
/* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */
if (before(ack, prior_snd_una - tp->max_window)) {
-   tcp_send_challenge_ack(sk, skb);
+   if (!(flag & FLAG_NO_CHALLENGE_ACK))
+   tcp_send_challenge_ack(sk, skb);
return -1;
}
goto old_ack;
@@ -5832,13 +5834,17 @@ int tcp_rcv_state_process(struct sock *s
 
/* step 5: check the ACK field */
acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
- FLAG_UPDATE_TS_RECENT) > 0;
+ FLAG_UPDATE_TS_RECENT |
+ FLAG_NO_CHALLENGE_ACK) > 0;
 
+   if (!acceptable) {
+   if (sk->sk_state == TCP_SYN_RECV)
+   return 1;   /* send one RST */
+   tcp_send_challenge_ack(sk, skb);
+   goto discard;
+   }
switch (sk->sk_state) {
case TCP_SYN_RECV:
-   if (!acceptable)
-   return 1;
-
if (!tp->srtt_us)
tcp_synack_rtt_meas(sk, req);
 
@@ -5907,14 +5913,6 @@ int tcp_rcv_state_process(struct sock *s
 * our SYNACK so stop the SYNACK timer.
 */
if (req) {
-   /* 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)
-