From nobody Mon Apr 6 11:51:41 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A40AC54EE9 for ; Tue, 27 Sep 2022 16:31:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231989AbiI0Qbz (ORCPT ); Tue, 27 Sep 2022 12:31:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231579AbiI0Qbw (ORCPT ); Tue, 27 Sep 2022 12:31:52 -0400 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B9C214F80F; Tue, 27 Sep 2022 09:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1664296312; x=1695832312; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Sr3tOQCqdzot2FqgnPmGwtlJYIkHv7Diy+0QG8QaSW8=; b=DDefbf/c2SEDfYmhNEyXi8RA3xSbQWe02OvOm8cU/e6sMQNpxgFUrV5P YJzMFWrEIJN83DLvom9hHEF2ZvSUj6clQ4lCCkNXAxFujvNfmXDq5iPeK Y5Kfn8gcqHw0LI+iXRH+/am25ivzq+XFNSlj02w9XNLoqkPs7IuZmy4b0 8=; X-IronPort-AV: E=Sophos;i="5.93,349,1654560000"; d="scan'208";a="263719065" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1a-87b71607.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2022 16:12:52 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-iad-1a-87b71607.us-east-1.amazon.com (Postfix) with ESMTPS id BE260140FEE; Tue, 27 Sep 2022 16:12:48 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Tue, 27 Sep 2022 16:12:43 +0000 Received: from 88665a182662.ant.amazon.com (10.43.160.214) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.12; Tue, 27 Sep 2022 16:12:40 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , David Ahern CC: Kuniyuki Iwashima , Kuniyuki Iwashima , , , , syzbot Subject: [PATCH v1 net 1/5] tcp/udp: Fix memory leak in ipv6_renew_options(). Date: Tue, 27 Sep 2022 09:12:05 -0700 Message-ID: <20220927161209.32939-2-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220927161209.32939-1-kuniyu@amazon.com> References: <20220927161209.32939-1-kuniyu@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.43.160.214] X-ClientProxiedBy: EX13D13UWA002.ant.amazon.com (10.43.160.172) To EX19D004ANA001.ant.amazon.com (10.37.240.138) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" syzbot reported a memory leak [0] related to IPV6_ADDRFORM. The scenario is that while one thread is converting an IPv6 socket into IPv4 with IPV6_ADDRFORM, another thread calls do_ipv6_setsockopt() and allocates memory to inet6_sk(sk)->XXX after conversion. Then, the converted sk with (tcp|udp)_prot never frees the IPv6 resources, which inet6_destroy_sock() should have cleaned up. setsockopt(IPV6_ADDRFORM) setsockopt(IPV6_DSTOPTS) +-----------------------+ +----------------------+ - do_ipv6_setsockopt(sk, ...) - lock_sock(sk) - do_ipv6_setsockopt(sk, ...) - WRITE_ONCE(sk->sk_prot, &tcp_prot) ^._ called via tcpv6_prot - xchg(&np->opt, NULL) before WRITE_ONCE() - txopt_put(opt) - release_sock(sk) - lock_sock(sk) - ipv6_set_opt_hdr(sk, ...) - ipv6_update_options(sk, opt) - xchg(&inet6_sk(sk)->opt, = opt) ^._ opt is never freed. - release_sock(sk) Since IPV6_DSTOPTS allocates options under lock_sock(), we can avoid this memory leak by testing whether sk_family is changed by IPV6_ADDRFORM after acquiring the lock. This issue exists from the initial commit between IPV6_ADDRFORM and IPV6_PKTOPTIONS. [0]: BUG: memory leak unreferenced object 0xffff888009ab9f80 (size 96): comm "syz-executor583", pid 328, jiffies 4294916198 (age 13.034s) hex dump (first 32 bytes): 01 00 00 00 48 00 00 00 08 00 00 00 00 00 00 00 ....H........... 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [<000000002ee98ae1>] kmalloc include/linux/slab.h:605 [inline] [<000000002ee98ae1>] sock_kmalloc+0xb3/0x100 net/core/sock.c:2566 [<0000000065d7b698>] ipv6_renew_options+0x21e/0x10b0 net/ipv6/exthdrs.c= :1318 [<00000000a8c756d7>] ipv6_set_opt_hdr net/ipv6/ipv6_sockglue.c:354 [inl= ine] [<00000000a8c756d7>] do_ipv6_setsockopt.constprop.0+0x28b7/0x4350 net/i= pv6/ipv6_sockglue.c:668 [<000000002854d204>] ipv6_setsockopt+0xdf/0x190 net/ipv6/ipv6_sockglue.= c:1021 [<00000000e69fdcf8>] tcp_setsockopt+0x13b/0x2620 net/ipv4/tcp.c:3789 [<0000000090da4b9b>] __sys_setsockopt+0x239/0x620 net/socket.c:2252 [<00000000b10d192f>] __do_sys_setsockopt net/socket.c:2263 [inline] [<00000000b10d192f>] __se_sys_setsockopt net/socket.c:2260 [inline] [<00000000b10d192f>] __x64_sys_setsockopt+0xbe/0x160 net/socket.c:2260 [<000000000a80d7aa>] do_syscall_x64 arch/x86/entry/common.c:50 [inline] [<000000000a80d7aa>] do_syscall_64+0x38/0x90 arch/x86/entry/common.c:80 [<000000004562b5c6>] entry_SYSCALL_64_after_hwframe+0x63/0xcd Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Reported-by: syzbot Signed-off-by: Kuniyuki Iwashima --- Note the syzbot is running on our EC2, so we don't have a URL or hash. Also, there seems to be no similar report on the public syzkaller dashboard. Thus the Reported-by address might be inappropriate. Please let me know if we should keep it empty or use another email address. --- net/ipv6/ipv6_sockglue.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c index e0dcc7a193df..b61066ac8648 100644 --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -419,6 +419,12 @@ static int do_ipv6_setsockopt(struct sock *sk, int lev= el, int optname, rtnl_lock(); lock_sock(sk); =20 + /* Another thread has converted the socket into IPv4 with + * IPV6_ADDRFORM concurrently. + */ + if (unlikely(sk->sk_family !=3D AF_INET6)) + goto unlock; + switch (optname) { =20 case IPV6_ADDRFORM: @@ -994,6 +1000,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int lev= el, int optname, break; } =20 +unlock: release_sock(sk); if (needs_rtnl) rtnl_unlock(); --=20 2.30.2 From nobody Mon Apr 6 11:51:41 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA283C54EE9 for ; Tue, 27 Sep 2022 16:13:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232498AbiI0QNV (ORCPT ); Tue, 27 Sep 2022 12:13:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231349AbiI0QNQ (ORCPT ); Tue, 27 Sep 2022 12:13:16 -0400 Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FF8430554; Tue, 27 Sep 2022 09:13:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1664295196; x=1695831196; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0iM48/ZwUvIPermSYAHjsW26ruHNrg4Ph8lHmyisiNQ=; b=C3f8n27chraN0Vw6n8sI93cTv8vbaMYPYQOjwJRDVKXOsP8O2NngOwxe 4DAcY6UxpvlTFCcqVrQ5I4TXJGc5x5uEyYrXSwMCDcECx5r1bvq4gcwvb IeNXHivSsJVo+GBsrhfKUXqnNYWP2oz/fOVEgIg1LNembEq6TiqXQm+xo A=; Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-iad-1box-d-0e176545.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2022 16:13:03 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-iad-1box-d-0e176545.us-east-1.amazon.com (Postfix) with ESMTPS id 6D04A94ACF; Tue, 27 Sep 2022 16:12:59 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Tue, 27 Sep 2022 16:12:58 +0000 Received: from 88665a182662.ant.amazon.com (10.43.160.214) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.12; Tue, 27 Sep 2022 16:12:56 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , David Ahern CC: Kuniyuki Iwashima , Kuniyuki Iwashima , , , , "Brian Haley" Subject: [PATCH v1 net 2/5] udp: Call inet6_destroy_sock() in setsockopt(IPV6_ADDRFORM). Date: Tue, 27 Sep 2022 09:12:06 -0700 Message-ID: <20220927161209.32939-3-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220927161209.32939-1-kuniyu@amazon.com> References: <20220927161209.32939-1-kuniyu@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.43.160.214] X-ClientProxiedBy: EX13D13UWA002.ant.amazon.com (10.43.160.172) To EX19D004ANA001.ant.amazon.com (10.37.240.138) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Commit 4b340ae20d0e ("IPv6: Complete IPV6_DONTFRAG support") forgot to add a change to free inet6_sk(sk)->rxpmtu while converting an IPv6 socket into IPv4 with IPV6_ADDRFORM. After conversion, sk_prot is changed to udp_prot and ->destroy() never cleans it up, resulting in a memory leak. This is due to the discrepancy between inet6_destroy_sock() and IPV6_ADDRFORM, so let's call inet6_destroy_sock() from IPV6_ADDRFORM to remove the difference. However, this is not enough for now because rxpmtu can be changed without lock_sock() after commit 03485f2adcde ("udpv6: Add lockless sendmsg() support"). We will fix this case in the following patch. Fixes: 4b340ae20d0e ("IPv6: Complete IPV6_DONTFRAG support") Signed-off-by: Kuniyuki Iwashima --- Cc: Brian Haley --- net/ipv6/ipv6_sockglue.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c index b61066ac8648..030a4cf23ceb 100644 --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -431,9 +431,6 @@ static int do_ipv6_setsockopt(struct sock *sk, int leve= l, int optname, if (optlen < sizeof(int)) goto e_inval; if (val =3D=3D PF_INET) { - struct ipv6_txoptions *opt; - struct sk_buff *pktopt; - if (sk->sk_type =3D=3D SOCK_RAW) break; =20 @@ -464,7 +461,6 @@ static int do_ipv6_setsockopt(struct sock *sk, int leve= l, int optname, break; } =20 - fl6_free_socklist(sk); __ipv6_sock_mc_close(sk); __ipv6_sock_ac_close(sk); =20 @@ -501,14 +497,9 @@ static int do_ipv6_setsockopt(struct sock *sk, int lev= el, int optname, sk->sk_socket->ops =3D &inet_dgram_ops; sk->sk_family =3D PF_INET; } - opt =3D xchg((__force struct ipv6_txoptions **)&np->opt, - NULL); - if (opt) { - atomic_sub(opt->tot_len, &sk->sk_omem_alloc); - txopt_put(opt); - } - pktopt =3D xchg(&np->pktoptions, NULL); - kfree_skb(pktopt); + + np->rxopt.all =3D 0; + inet6_destroy_sock(sk); =20 /* * ... and add it to the refcnt debug socks count --=20 2.30.2 From nobody Mon Apr 6 11:51:41 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E7B5C07E9D for ; Tue, 27 Sep 2022 16:13:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232675AbiI0QNh (ORCPT ); Tue, 27 Sep 2022 12:13:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231631AbiI0QNe (ORCPT ); Tue, 27 Sep 2022 12:13:34 -0400 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82A7E915D5; Tue, 27 Sep 2022 09:13:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1664295213; x=1695831213; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4gy6y9RluD7Ishn0pzYcwsWmftF1WClFyBmqV8OA2xs=; b=Hekt2o2ez/lZUHbbZnMVOz81q+cvUuB4h5QpI8zTyVx/Ea1iZXxyjXqg XgqMPVVnWvMpQL8KMw0xiJTpEOHD3GdMUCtNQo2LSpDuxriibQdo6kGJn ilVL4SgtF0BULRSyX6lBCQ/cLfQ/e/tv3rVdS/ZkWrQzaljyqjq0U/Glm Q=; X-IronPort-AV: E=Sophos;i="5.93,349,1654560000"; d="scan'208";a="228971190" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1a-828bd003.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2022 16:13:17 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-iad-1a-828bd003.us-east-1.amazon.com (Postfix) with ESMTPS id 24AE7804B3; Tue, 27 Sep 2022 16:13:14 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Tue, 27 Sep 2022 16:13:13 +0000 Received: from 88665a182662.ant.amazon.com (10.43.160.214) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.12; Tue, 27 Sep 2022 16:13:10 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , David Ahern CC: Kuniyuki Iwashima , Kuniyuki Iwashima , , , , "Vladislav Yasevich" Subject: [PATCH v1 net 3/5] tcp/udp: Call inet6_destroy_sock() in IPv4 sk_prot->destroy(). Date: Tue, 27 Sep 2022 09:12:07 -0700 Message-ID: <20220927161209.32939-4-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220927161209.32939-1-kuniyu@amazon.com> References: <20220927161209.32939-1-kuniyu@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.43.160.214] X-ClientProxiedBy: EX13D13UWA002.ant.amazon.com (10.43.160.172) To EX19D004ANA001.ant.amazon.com (10.37.240.138) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Originally, inet6_sk(sk)->XXX were changed under lock_sock(), so we were able to clean them up by calling inet6_destroy_sock() during the IPv6 -> IPv4 conversion by IPV6_ADDRFORM. However, commit 03485f2adcde ("udpv6: Add lockless sendmsg() support") added a lockless memory allocation path, which could cause a memory leak: setsockopt(IPV6_ADDRFORM) sendmsg() +-----------------------+ +-------+ - do_ipv6_setsockopt(sk, ...) - udpv6_sendmsg(sk, ...) - lock_sock(sk) ^._ called via udpv6_prot - WRITE_ONCE(sk->sk_prot, &tcp_prot) before WRITE_ONCE() - inet6_destroy_sock() - release_sock(sk) - ip6_make_skb(sk, ...) ^._ lockless fast path for the non-corking case - __ip6_append_data(sk, ...) - ipv6_local_rxpmtu(sk, ...) - xchg(&np->rxpmtu, skb) ^._ rxpmtu is never fre= ed. - lock_sock(sk) For now, rxpmtu is only the case, but let's call inet6_destroy_sock() in both TCP/UDP v4 destroy functions not to miss the future change. We can consolidate TCP/UDP v4/v6 destroy functions, but such changes are too invasive to backport to stable. So, they can be posted as a follow-up later for net-next. Fixes: 03485f2adcde ("udpv6: Add lockless sendmsg() support") Signed-off-by: Kuniyuki Iwashima --- Cc: Vladislav Yasevich --- net/ipv4/tcp_ipv4.c | 5 +++++ net/ipv4/udp.c | 6 ++++++ net/ipv6/tcp_ipv6.c | 1 - 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 5b019ba2b9d2..035b6c52a243 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -2263,6 +2263,11 @@ void tcp_v4_destroy_sock(struct sock *sk) tcp_saved_syn_free(tp); =20 sk_sockets_allocated_dec(sk); + +#if IS_ENABLED(CONFIG_IPV6) + if (sk->sk_prot_creator =3D=3D &tcpv6_prot) + inet6_destroy_sock(sk); +#endif } EXPORT_SYMBOL(tcp_v4_destroy_sock); =20 diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 560d9eadeaa5..cdf131c0a819 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -115,6 +115,7 @@ #include #if IS_ENABLED(CONFIG_IPV6) #include +#include #endif =20 struct udp_table udp_table __read_mostly; @@ -2666,6 +2667,11 @@ void udp_destroy_sock(struct sock *sk) if (up->encap_enabled) static_branch_dec(&udp_encap_needed_key); } + +#if IS_ENABLED(CONFIG_IPV6) + if (sk->sk_prot_creator =3D=3D &udpv6_prot) + inet6_destroy_sock(sk); +#endif } =20 /* diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index e54eee80ce5f..1ff6a92f7774 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1945,7 +1945,6 @@ static int tcp_v6_init_sock(struct sock *sk) static void tcp_v6_destroy_sock(struct sock *sk) { tcp_v4_destroy_sock(sk); - inet6_destroy_sock(sk); } =20 #ifdef CONFIG_PROC_FS --=20 2.30.2 From nobody Mon Apr 6 11:51:41 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB45CC07E9D for ; Tue, 27 Sep 2022 16:35:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232004AbiI0QfJ (ORCPT ); Tue, 27 Sep 2022 12:35:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231602AbiI0QfA (ORCPT ); Tue, 27 Sep 2022 12:35:00 -0400 Received: from smtp-fw-9103.amazon.com (smtp-fw-9103.amazon.com [207.171.188.200]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2865C8E45D; Tue, 27 Sep 2022 09:34:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1664296498; x=1695832498; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dunUqMHRVy/F1qoocOd7IoWfLVOgdAc7C1AOAVjuQ7w=; b=lVjrUBBdYXRztDt6j6ucfWSzdlLKeLimnjIdNh44DG8ya/5t901ZEftU g9ytSHjOwMmA4RgneN58AEhf4+8AaKEqLBxvh/9MEK4M9q78fYYd4+Xib drotQmr+NL5HAEnYroo6JrFaPBg4tvJPAWIhSTkFmpKV6OTFw1q751KV3 U=; X-IronPort-AV: E=Sophos;i="5.93,349,1654560000"; d="scan'208";a="1058534176" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-iad-1box-d-0e176545.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-9103.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2022 16:13:32 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-iad-1box-d-0e176545.us-east-1.amazon.com (Postfix) with ESMTPS id 7AF1189656; Tue, 27 Sep 2022 16:13:29 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Tue, 27 Sep 2022 16:13:28 +0000 Received: from 88665a182662.ant.amazon.com (10.43.160.214) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.12; Tue, 27 Sep 2022 16:13:26 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , David Ahern CC: Kuniyuki Iwashima , Kuniyuki Iwashima , , , Subject: [PATCH v1 net 4/5] ipv6: Fix data races around sk->sk_prot. Date: Tue, 27 Sep 2022 09:12:08 -0700 Message-ID: <20220927161209.32939-5-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220927161209.32939-1-kuniyu@amazon.com> References: <20220927161209.32939-1-kuniyu@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.43.160.214] X-ClientProxiedBy: EX13D13UWA002.ant.amazon.com (10.43.160.172) To EX19D004ANA001.ant.amazon.com (10.37.240.138) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Commit 086d49058cd8 ("ipv6: annotate some data-races around sk->sk_prot") fixed some data-races around sk->sk_prot but it was not enough. Some functions in inet6_(stream|dgram)_ops still access sk->sk_prot without lock_sock() or rtnl_lock(), so they need READ_ONCE() to avoid load tearing. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima --- net/core/sock.c | 6 ++++-- net/ipv4/af_inet.c | 23 ++++++++++++++++------- net/ipv6/ipv6_sockglue.c | 4 ++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/net/core/sock.c b/net/core/sock.c index 788c1372663c..9c05637663bf 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3556,7 +3556,8 @@ int sock_common_getsockopt(struct socket *sock, int l= evel, int optname, { struct sock *sk =3D sock->sk; =20 - return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen); + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + return READ_ONCE(sk->sk_prot)->getsockopt(sk, level, optname, optval, opt= len); } EXPORT_SYMBOL(sock_common_getsockopt); =20 @@ -3582,7 +3583,8 @@ int sock_common_setsockopt(struct socket *sock, int l= evel, int optname, { struct sock *sk =3D sock->sk; =20 - return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen); + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + return READ_ONCE(sk->sk_prot)->setsockopt(sk, level, optname, optval, opt= len); } EXPORT_SYMBOL(sock_common_setsockopt); =20 diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c index 3ca0cc467886..405fbad998df 100644 --- a/net/ipv4/af_inet.c +++ b/net/ipv4/af_inet.c @@ -558,22 +558,27 @@ int inet_dgram_connect(struct socket *sock, struct so= ckaddr *uaddr, int addr_len, int flags) { struct sock *sk =3D sock->sk; + const struct proto *prot; int err; =20 if (addr_len < sizeof(uaddr->sa_family)) return -EINVAL; + + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + prot =3D READ_ONCE(sk->sk_prot); + if (uaddr->sa_family =3D=3D AF_UNSPEC) - return sk->sk_prot->disconnect(sk, flags); + return prot->disconnect(sk, flags); =20 if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) { - err =3D sk->sk_prot->pre_connect(sk, uaddr, addr_len); + err =3D prot->pre_connect(sk, uaddr, addr_len); if (err) return err; } =20 if (data_race(!inet_sk(sk)->inet_num) && inet_autobind(sk)) return -EAGAIN; - return sk->sk_prot->connect(sk, uaddr, addr_len); + return prot->connect(sk, uaddr, addr_len); } EXPORT_SYMBOL(inet_dgram_connect); =20 @@ -734,10 +739,11 @@ EXPORT_SYMBOL(inet_stream_connect); int inet_accept(struct socket *sock, struct socket *newsock, int flags, bool kern) { - struct sock *sk1 =3D sock->sk; + struct sock *sk1 =3D sock->sk, *sk2; int err =3D -EINVAL; - struct sock *sk2 =3D sk1->sk_prot->accept(sk1, flags, &err, kern); =20 + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + sk2 =3D READ_ONCE(sk1->sk_prot)->accept(sk1, flags, &err, kern); if (!sk2) goto do_err; =20 @@ -825,12 +831,15 @@ ssize_t inet_sendpage(struct socket *sock, struct pag= e *page, int offset, size_t size, int flags) { struct sock *sk =3D sock->sk; + const struct proto *prot; =20 if (unlikely(inet_send_prepare(sk))) return -EAGAIN; =20 - if (sk->sk_prot->sendpage) - return sk->sk_prot->sendpage(sk, page, offset, size, flags); + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + prot =3D READ_ONCE(sk->sk_prot); + if (prot->sendpage) + return prot->sendpage(sk, page, offset, size, flags); return sock_no_sendpage(sock, page, offset, size, flags); } EXPORT_SYMBOL(inet_sendpage); diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c index 030a4cf23ceb..a89db5872dc3 100644 --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -477,7 +477,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int leve= l, int optname, sock_prot_inuse_add(net, sk->sk_prot, -1); sock_prot_inuse_add(net, &tcp_prot, 1); =20 - /* Paired with READ_ONCE(sk->sk_prot) in net/ipv6/af_inet6.c */ + /* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */ WRITE_ONCE(sk->sk_prot, &tcp_prot); icsk->icsk_af_ops =3D &ipv4_specific; sk->sk_socket->ops =3D &inet_stream_ops; @@ -492,7 +492,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int leve= l, int optname, sock_prot_inuse_add(net, sk->sk_prot, -1); sock_prot_inuse_add(net, prot, 1); =20 - /* Paired with READ_ONCE(sk->sk_prot) in net/ipv6/af_inet6.c */ + /* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */ WRITE_ONCE(sk->sk_prot, prot); sk->sk_socket->ops =3D &inet_dgram_ops; sk->sk_family =3D PF_INET; --=20 2.30.2 From nobody Mon Apr 6 11:51:41 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91350C54EE9 for ; Tue, 27 Sep 2022 16:33:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229760AbiI0Qde (ORCPT ); Tue, 27 Sep 2022 12:33:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229470AbiI0Qdc (ORCPT ); Tue, 27 Sep 2022 12:33:32 -0400 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C91FA147CD8; Tue, 27 Sep 2022 09:33:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1664296413; x=1695832413; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=V15MrQFTDlO/YLh4gKR03P+7qtVWjx88QGQDMwXjI4o=; b=Qk2qpmh5oACBLcsQ7p8JIJ8Q0ip5sIKe+KWNmtploR8vvvlIt4K8d3i1 Umm0sbFnR3QGFYWlI3QJ0lxSS8xMWik01yL67S3Ujk6cNrEQJL5+cSpEr Lq1Bs7IGg471j4uYxZBZH18tfXr1V2nnJqmhXXIrDPjLutpe5EQaJCUm+ Y=; X-IronPort-AV: E=Sophos;i="5.93,349,1654560000"; d="scan'208";a="263719827" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1e-54c9d11f.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2022 16:13:54 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-iad-1e-54c9d11f.us-east-1.amazon.com (Postfix) with ESMTPS id E1421C0ACC; Tue, 27 Sep 2022 16:13:50 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Tue, 27 Sep 2022 16:13:43 +0000 Received: from 88665a182662.ant.amazon.com (10.43.160.214) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.12; Tue, 27 Sep 2022 16:13:40 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , David Ahern CC: Kuniyuki Iwashima , Kuniyuki Iwashima , , , Subject: [PATCH v1 net 5/5] tcp: Fix data races around icsk->icsk_af_ops. Date: Tue, 27 Sep 2022 09:12:09 -0700 Message-ID: <20220927161209.32939-6-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220927161209.32939-1-kuniyu@amazon.com> References: <20220927161209.32939-1-kuniyu@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.43.160.214] X-ClientProxiedBy: EX13D13UWA002.ant.amazon.com (10.43.160.172) To EX19D004ANA001.ant.amazon.com (10.37.240.138) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" IPV6_ADDRFORM changes icsk->icsk_af_ops under lock_sock(), but tcp_(get|set)sockopt() read it locklessly. To avoid load/store tearing, we need to add READ_ONCE() and WRITE_ONCE() for the reads and write. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima --- net/ipv4/tcp.c | 10 ++++++---- net/ipv6/ipv6_sockglue.c | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index e373dde1f46f..c86dd0ccef5b 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3795,8 +3795,9 @@ int tcp_setsockopt(struct sock *sk, int level, int op= tname, sockptr_t optval, const struct inet_connection_sock *icsk =3D inet_csk(sk); =20 if (level !=3D SOL_TCP) - return icsk->icsk_af_ops->setsockopt(sk, level, optname, - optval, optlen); + /* IPV6_ADDRFORM can change icsk->icsk_af_ops under us. */ + return READ_ONCE(icsk->icsk_af_ops)->setsockopt(sk, level, optname, + optval, optlen); return do_tcp_setsockopt(sk, level, optname, optval, optlen); } EXPORT_SYMBOL(tcp_setsockopt); @@ -4394,8 +4395,9 @@ int tcp_getsockopt(struct sock *sk, int level, int op= tname, char __user *optval, struct inet_connection_sock *icsk =3D inet_csk(sk); =20 if (level !=3D SOL_TCP) - return icsk->icsk_af_ops->getsockopt(sk, level, optname, - optval, optlen); + /* IPV6_ADDRFORM can change icsk->icsk_af_ops under us. */ + return READ_ONCE(icsk->icsk_af_ops)->getsockopt(sk, level, optname, + optval, optlen); return do_tcp_getsockopt(sk, level, optname, optval, optlen); } EXPORT_SYMBOL(tcp_getsockopt); diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c index a89db5872dc3..726d95859898 100644 --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -479,7 +479,8 @@ static int do_ipv6_setsockopt(struct sock *sk, int leve= l, int optname, =20 /* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */ WRITE_ONCE(sk->sk_prot, &tcp_prot); - icsk->icsk_af_ops =3D &ipv4_specific; + /* Paired with READ_ONCE() in tcp_(get|set)sockopt() */ + WRITE_ONCE(icsk->icsk_af_ops, &ipv4_specific); sk->sk_socket->ops =3D &inet_stream_ops; sk->sk_family =3D PF_INET; tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); --=20 2.30.2