Commit 70741861 authored by Eric Dumazet's avatar Eric Dumazet Committed by Greg Kroah-Hartman

tcp: avoid integer overflows in tcp_rcv_space_adjust()

commit 607065ba upstream.

When using large tcp_rmem[2] values (I did tests with 500 MB),
I noticed overflows while computing rcvwin.

Lets fix this before the following patch.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Acked-by: default avatarSoheil Hassas Yeganeh <soheil@google.com>
Acked-by: default avatarWei Wang <weiwan@google.com>
Acked-by: default avatarNeal Cardwell <ncardwell@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
[Backport: sysctl_tcp_rmem is not Namespace-ify'd in older kernels]
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 9b5c9f07
...@@ -324,7 +324,7 @@ struct tcp_sock { ...@@ -324,7 +324,7 @@ struct tcp_sock {
/* Receiver queue space */ /* Receiver queue space */
struct { struct {
int space; u32 space;
u32 seq; u32 seq;
u32 time; u32 time;
} rcvq_space; } rcvq_space;
......
...@@ -557,8 +557,8 @@ static inline void tcp_rcv_rtt_measure_ts(struct sock *sk, ...@@ -557,8 +557,8 @@ static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
void tcp_rcv_space_adjust(struct sock *sk) void tcp_rcv_space_adjust(struct sock *sk)
{ {
struct tcp_sock *tp = tcp_sk(sk); struct tcp_sock *tp = tcp_sk(sk);
u32 copied;
int time; int time;
int copied;
time = tcp_time_stamp - tp->rcvq_space.time; time = tcp_time_stamp - tp->rcvq_space.time;
if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0) if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
...@@ -580,12 +580,13 @@ void tcp_rcv_space_adjust(struct sock *sk) ...@@ -580,12 +580,13 @@ void tcp_rcv_space_adjust(struct sock *sk)
if (sysctl_tcp_moderate_rcvbuf && if (sysctl_tcp_moderate_rcvbuf &&
!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
int rcvwin, rcvmem, rcvbuf; int rcvmem, rcvbuf;
u64 rcvwin;
/* minimal window to cope with packet losses, assuming /* minimal window to cope with packet losses, assuming
* steady state. Add some cushion because of small variations. * steady state. Add some cushion because of small variations.
*/ */
rcvwin = (copied << 1) + 16 * tp->advmss; rcvwin = ((u64)copied << 1) + 16 * tp->advmss;
/* If rate increased by 25%, /* If rate increased by 25%,
* assume slow start, rcvwin = 3 * copied * assume slow start, rcvwin = 3 * copied
...@@ -605,7 +606,8 @@ void tcp_rcv_space_adjust(struct sock *sk) ...@@ -605,7 +606,8 @@ void tcp_rcv_space_adjust(struct sock *sk)
while (tcp_win_from_space(rcvmem) < tp->advmss) while (tcp_win_from_space(rcvmem) < tp->advmss)
rcvmem += 128; rcvmem += 128;
rcvbuf = min(rcvwin / tp->advmss * rcvmem, sysctl_tcp_rmem[2]); do_div(rcvwin, tp->advmss);
rcvbuf = min_t(u64, rcvwin * rcvmem, sysctl_tcp_rmem[2]);
if (rcvbuf > sk->sk_rcvbuf) { if (rcvbuf > sk->sk_rcvbuf) {
sk->sk_rcvbuf = rcvbuf; sk->sk_rcvbuf = rcvbuf;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment