Return-Path: Received: from mail-cys01nam02on0126.outbound.protection.outlook.com ([104.47.37.126]:47215 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933898AbeCCWcy (ORCPT ); Sat, 3 Mar 2018 17:32:54 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Gao Feng , "David S . Miller" , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 066/219] tcp: sysctl: Fix a race to avoid unexpected 0 window from space Date: Sat, 3 Mar 2018 22:28:34 +0000 Message-ID: <20180303222716.26640-66-alexander.levin@microsoft.com> References: <20180303222716.26640-1-alexander.levin@microsoft.com> In-Reply-To: <20180303222716.26640-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Gao Feng [ Upstream commit c48367427a39ea0b85c7cf018fe4256627abfd9e ] Because sysctl_tcp_adv_win_scale could be changed any time, so there is one race in tcp_win_from_space. For example, 1.sysctl_tcp_adv_win_scale<=3D0 (sysctl_tcp_adv_win_scale is negative now) 2.space>>(-sysctl_tcp_adv_win_scale) (sysctl_tcp_adv_win_scale is postive n= ow) As a result, tcp_win_from_space returns 0. It is unexpected. Certainly if the compiler put the sysctl_tcp_adv_win_scale into one register firstly, then use the register directly, it would be ok. But we could not depend on the compiler behavior. Signed-off-by: Gao Feng Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- include/net/tcp.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index caf35e062639..18f029bcb8c7 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -1265,9 +1265,11 @@ void tcp_select_initial_window(int __space, __u32 ms= s, __u32 *rcv_wnd, =20 static inline int tcp_win_from_space(int space) { - return sysctl_tcp_adv_win_scale<=3D0 ? - (space>>(-sysctl_tcp_adv_win_scale)) : - space - (space>>sysctl_tcp_adv_win_scale); + int tcp_adv_win_scale =3D sysctl_tcp_adv_win_scale; + + return tcp_adv_win_scale <=3D 0 ? + (space>>(-tcp_adv_win_scale)) : + space - (space>>tcp_adv_win_scale); } =20 /* Note: caller must be prepared to deal with negative returns */ --=20 2.14.1