Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3082151rwe; Mon, 29 Aug 2022 05:38:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR4OQGJqg6q44D6+ebLEETCXq1Dywxs2QrTJSHTYmtcgb2oofwMKz+vJHEd2B9oC/ki5K7EB X-Received: by 2002:a17:90b:2bca:b0:1fd:a06b:ef4f with SMTP id ru10-20020a17090b2bca00b001fda06bef4fmr9341088pjb.201.1661776738929; Mon, 29 Aug 2022 05:38:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661776738; cv=none; d=google.com; s=arc-20160816; b=0ssiqsE0HwXrR5p9vqNAF8b/qsCL4elZg65AJ8qqfjFT+QBIX3GnYjBmisvwVLOHlQ fBOYRgyWYCHE6Zj36dOcULvf7OdSgg4uoiPFP5q50bKvic0YqjKpH0rfXGZVZik1OXpa IU4FSwfdBXT2AgsyLmkknn6NfnG3LbKjVYDF/tzPoxeRbitPXvzLYvGJYdg3IL190kQ9 1q0aANdsUh5eUj+cV66EYCzDtPjS5z9loC3USOeUOp5kLWovGhasEH2n5fE8hnS07sR6 R9svX+RQ6lqdjV2w4+2jPpeN+0Qb7oRKuOeBmpMj2QUmhZd0O/J0QDyVvP5zMuvboH4J AvhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UdlmElie3So+Gt/Ar8QqpjeShwkYh4EIFUPFuoopzkc=; b=KIC0sdQvnjadTfZz4QVmmZjzZzKgUMKIb8oKJruSK38hxhaseLhQXgm37KbTG3kRHq IYcuGvHlnvsr1XOdkmFbvCHrAoCBie/ePmpjmZQuBoP3bPhvy1BgtC4NQvziMlIWW6W/ uUYPzdA6Pxhrcs6RtuGjF2lYwkPT6fLh+MPUga9+wh2qHTgM/jviuxyoyIKcC3XEEZ16 f3Y94T8IABfEO8+SQlLqAgiJtVxEs97YHYic8pv66FNSKT/FdBPzosRWZosDYD1y+O7h pespY+cieCoJA8sSnfWZEh0XIWipNoXog7ZFy5YTFV1a3nuIBCyhYpJ1h0RhkOUsacCg HWQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EXpmzyt7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c23-20020a634e17000000b00429b4bd247asi8452553pgb.397.2022.08.29.05.38.47; Mon, 29 Aug 2022 05:38:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EXpmzyt7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229922AbiH2MQo (ORCPT + 99 others); Mon, 29 Aug 2022 08:16:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229762AbiH2MQU (ORCPT ); Mon, 29 Aug 2022 08:16:20 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E09DBB44; Mon, 29 Aug 2022 04:59:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A47EBB80DB5; Mon, 29 Aug 2022 11:14:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF927C433D6; Mon, 29 Aug 2022 11:14:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661771695; bh=j/AA/qB98OBC/3qt5R55AMzuATgg/+LREcY+zbIqUEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EXpmzyt7+Pnp7gnVj8j9IF9Y5AJ7Sjcrjh6pi4nAx5Vwnr3zaz+H9wJQs2AVNbY5q 6dsMLouWxwgpJm04TvIYeQYaTjkhVygld1cw0KzPIsiQ/9XA7txmPJ1TEskHNDxnDf 5BivIDKIqRSO7+qkpFEr9PCMajNKqtfjPTti9dLc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kuniyuki Iwashima , "David S. Miller" , Sasha Levin Subject: [PATCH 5.19 065/158] net: Fix data-races around sysctl_[rw]mem_(max|default). Date: Mon, 29 Aug 2022 12:58:35 +0200 Message-Id: <20220829105811.442361269@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829105808.828227973@linuxfoundation.org> References: <20220829105808.828227973@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kuniyuki Iwashima [ Upstream commit 1227c1771dd2ad44318aa3ab9e3a293b3f34ff2a ] While reading sysctl_[rw]mem_(max|default), they can be changed concurrently. Thus, we need to add READ_ONCE() to its readers. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/core/filter.c | 4 ++-- net/core/sock.c | 8 ++++---- net/ipv4/ip_output.c | 2 +- net/ipv4/tcp_output.c | 2 +- net/netfilter/ipvs/ip_vs_sync.c | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index 74f05ed6aff29..60c854e7d98ba 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5036,14 +5036,14 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname, /* Only some socketops are supported */ switch (optname) { case SO_RCVBUF: - val = min_t(u32, val, sysctl_rmem_max); + val = min_t(u32, val, READ_ONCE(sysctl_rmem_max)); val = min_t(int, val, INT_MAX / 2); sk->sk_userlocks |= SOCK_RCVBUF_LOCK; WRITE_ONCE(sk->sk_rcvbuf, max_t(int, val * 2, SOCK_MIN_RCVBUF)); break; case SO_SNDBUF: - val = min_t(u32, val, sysctl_wmem_max); + val = min_t(u32, val, READ_ONCE(sysctl_wmem_max)); val = min_t(int, val, INT_MAX / 2); sk->sk_userlocks |= SOCK_SNDBUF_LOCK; WRITE_ONCE(sk->sk_sndbuf, diff --git a/net/core/sock.c b/net/core/sock.c index 2ff40dd0a7a65..62f69bc3a0e6e 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1100,7 +1100,7 @@ int sock_setsockopt(struct socket *sock, int level, int optname, * play 'guess the biggest size' games. RCVBUF/SNDBUF * are treated in BSD as hints */ - val = min_t(u32, val, sysctl_wmem_max); + val = min_t(u32, val, READ_ONCE(sysctl_wmem_max)); set_sndbuf: /* Ensure val * 2 fits into an int, to prevent max_t() * from treating it as a negative value. @@ -1132,7 +1132,7 @@ int sock_setsockopt(struct socket *sock, int level, int optname, * play 'guess the biggest size' games. RCVBUF/SNDBUF * are treated in BSD as hints */ - __sock_set_rcvbuf(sk, min_t(u32, val, sysctl_rmem_max)); + __sock_set_rcvbuf(sk, min_t(u32, val, READ_ONCE(sysctl_rmem_max))); break; case SO_RCVBUFFORCE: @@ -3307,8 +3307,8 @@ void sock_init_data(struct socket *sock, struct sock *sk) timer_setup(&sk->sk_timer, NULL, 0); sk->sk_allocation = GFP_KERNEL; - sk->sk_rcvbuf = sysctl_rmem_default; - sk->sk_sndbuf = sysctl_wmem_default; + sk->sk_rcvbuf = READ_ONCE(sysctl_rmem_default); + sk->sk_sndbuf = READ_ONCE(sysctl_wmem_default); sk->sk_state = TCP_CLOSE; sk_set_socket(sk, sock); diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 00b4bf26fd932..da8b3cc67234d 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -1712,7 +1712,7 @@ void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, sk->sk_protocol = ip_hdr(skb)->protocol; sk->sk_bound_dev_if = arg->bound_dev_if; - sk->sk_sndbuf = sysctl_wmem_default; + sk->sk_sndbuf = READ_ONCE(sysctl_wmem_default); ipc.sockc.mark = fl4.flowi4_mark; err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, len, 0, &ipc, &rt, MSG_DONTWAIT); diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index aed0c5f828bef..84314de754f87 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -239,7 +239,7 @@ void tcp_select_initial_window(const struct sock *sk, int __space, __u32 mss, if (wscale_ok) { /* Set window scaling on max possible window */ space = max_t(u32, space, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); - space = max_t(u32, space, sysctl_rmem_max); + space = max_t(u32, space, READ_ONCE(sysctl_rmem_max)); space = min_t(u32, space, *window_clamp); *rcv_wscale = clamp_t(int, ilog2(space) - 15, 0, TCP_MAX_WSCALE); diff --git a/net/netfilter/ipvs/ip_vs_sync.c b/net/netfilter/ipvs/ip_vs_sync.c index 9d43277b8b4fe..a56fd0b5a430a 100644 --- a/net/netfilter/ipvs/ip_vs_sync.c +++ b/net/netfilter/ipvs/ip_vs_sync.c @@ -1280,12 +1280,12 @@ static void set_sock_size(struct sock *sk, int mode, int val) lock_sock(sk); if (mode) { val = clamp_t(int, val, (SOCK_MIN_SNDBUF + 1) / 2, - sysctl_wmem_max); + READ_ONCE(sysctl_wmem_max)); sk->sk_sndbuf = val * 2; sk->sk_userlocks |= SOCK_SNDBUF_LOCK; } else { val = clamp_t(int, val, (SOCK_MIN_RCVBUF + 1) / 2, - sysctl_rmem_max); + READ_ONCE(sysctl_rmem_max)); sk->sk_rcvbuf = val * 2; sk->sk_userlocks |= SOCK_RCVBUF_LOCK; } -- 2.35.1