Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1418571rwb; Wed, 28 Sep 2022 18:48:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7srfaky4FrhIHDmLxU272QaMhP9ofwWGHAMPJieen4KKs9gWqLnxfNuBna/Wwzosx+MevJ X-Received: by 2002:a17:907:75dc:b0:783:9c71:5e20 with SMTP id jl28-20020a17090775dc00b007839c715e20mr644757ejc.125.1664416125105; Wed, 28 Sep 2022 18:48:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664416125; cv=none; d=google.com; s=arc-20160816; b=tTsJdraQaYneUP7eONi2VgrXlqY+5QOuXW9Ytn0guBVE51E7WKYr8MH6MM8z9/AnGf y8yVsm5z5BZxrmFni2gkeoV6vax+JN8AcZxcGp4SFWm/Z6cR30JbBYRHiVSWDEDm4gUl mhkNeZUr9zCgirlxM9DkX7jCaTVVw/8HuWLE4vQZZA/FMwn4ZvtFHW+6oup3HZ13jJH4 F5RoaNE/iELwTctkLpSwp5JbndnHPJOUl/MqvL2gJgWWEA+YZkh7Ms/x3CPUlyGTOrNd TFFP6TloyQvuuUPVyfIP95F2WGsnmbZPr41QZ27z4FKdSehsTLgWkbNnvaZ6/kdBGWW6 eNjg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JpC+VIgFCKMP4Dco+OF4SIAfPdUi+cxpvQFmjgko8JU=; b=DBIHtsHUw2SLOtkW6hPBKJW7dUK6qWd7IDBxkfgDrsr2wflDWsHQjSW8q/Svmm6BmK 4uUfh9fud8G/GRlLo3l2F6ofc8n+SZrm3wMhuizvGMqs8j5c4vBLfvWYi+5Hod7qCbl+ xW0Huo/qUEeNPfMsF1ibqGxS0hI3WfiSnhyUs+7VetBYPggc2AumczQl9qffsjkFFToB UTsfhCu8tBOCh75BsqhRPV0/wd3jBm63G2eDv/sNV2uyhsrb5a2aqGhb5WHROT1R6/LR VJ/sDhF0jlZHlszlrgBbVQx1W19AbTGjkXqEL2zfQGf1VOiBLfecyXB++MxH8d3o5aBR ktvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=asNccIE4; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p27-20020a1709061b5b00b00782539a0286si5408239ejg.142.2022.09.28.18.48.19; Wed, 28 Sep 2022 18:48:45 -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=@amazon.com header.s=amazon201209 header.b=asNccIE4; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233904AbiI2B0n (ORCPT + 99 others); Wed, 28 Sep 2022 21:26:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233963AbiI2B0j (ORCPT ); Wed, 28 Sep 2022 21:26:39 -0400 Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0959E7DF6A; Wed, 28 Sep 2022 18:26:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1664414798; x=1695950798; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JpC+VIgFCKMP4Dco+OF4SIAfPdUi+cxpvQFmjgko8JU=; b=asNccIE4eYlO9vEqpajp7N5frwG75ouE8yLiRhHDBEZTLurwNswGBdmW 3w06E1q2zmP9AF2lvbsNurgTBei15x7iwGF26/9+A+Qj3cguRgln1p0v/ UGGE1IsxufzMin5lc+rqKfGAM5HyG73PrKrytzIP2DUYbUNOcyjA6wJTf A=; X-IronPort-AV: E=Sophos;i="5.93,353,1654560000"; d="scan'208";a="135016902" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1e-7dac3c4d.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2022 01:26:19 +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-1e-7dac3c4d.us-east-1.amazon.com (Postfix) with ESMTPS id C2DBBA0D52; Thu, 29 Sep 2022 01:26:15 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX13MTAUWB001.ant.amazon.com (10.43.161.249) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Thu, 29 Sep 2022 01:26:15 +0000 Received: from 88665a182662.ant.amazon.com (10.43.162.55) 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; Thu, 29 Sep 2022 01:26:09 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , David Ahern CC: Kuniyuki Iwashima , Kuniyuki Iwashima , , , , syzbot Subject: [PATCH v3 net 1/5] tcp/udp: Fix memory leak in ipv6_renew_options(). Date: Wed, 28 Sep 2022 18:25:38 -0700 Message-ID: <20220929012542.55424-2-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220929012542.55424-1-kuniyu@amazon.com> References: <20220929012542.55424-1-kuniyu@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.43.162.55] X-ClientProxiedBy: EX13D30UWB004.ant.amazon.com (10.43.161.51) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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 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 [inline] [<00000000a8c756d7>] do_ipv6_setsockopt.constprop.0+0x28b7/0x4350 net/ipv6/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 --- 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 level, int optname, rtnl_lock(); lock_sock(sk); + /* Another thread has converted the socket into IPv4 with + * IPV6_ADDRFORM concurrently. + */ + if (unlikely(sk->sk_family != AF_INET6)) + goto unlock; + switch (optname) { case IPV6_ADDRFORM: @@ -994,6 +1000,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, break; } +unlock: release_sock(sk); if (needs_rtnl) rtnl_unlock(); -- 2.30.2