Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4415001pxa; Mon, 10 Aug 2020 08:33:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxq6U7k4g4nG3+VpaYaz/TSpHhWu8921JBEwGdNRbOwnhLPAa6qcgBDJ/YpBJ2/mKTE9d35 X-Received: by 2002:a17:907:7287:: with SMTP id dt7mr21495445ejc.224.1597073598155; Mon, 10 Aug 2020 08:33:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597073598; cv=none; d=google.com; s=arc-20160816; b=WXR/OaJ8NWar844L041qFybKWyxqEqv4OixBfBcUpx7L+BEy7mCAXnp2E2nflCjF2H LsJlO1MN0X+EX5gd90bJ5mQVpzyyPBQbVYj19BM/IoAfYhmkQua95i0vMKfQrbl0lw3S hAo2clZKQlGvrFHj+Xoz9fwNaqwMDYlEeAqssgYg31FHwOgkzwPmMt+l5LrgKzqEvZsh j6t9ritaG5uB3WCrZROjv2BI8Ai0PVYX7ZNHZn+fUWKyFyQiCoQIDsiBmHtb7qWplJFE vxCX5k8yhrNvr/N4VDD/ZQIDmvRMOXFUem5M3RP11vtUOlnTYPoT4FiBvCiHeI5MrTYd /l5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=lnqpnoIcp1+xfS4F/JE0G/+iE2+5qNvtGHGnpLKVino=; b=kKLCR+hvteZno1KNHoawpbYr9n4aSrwKLqsG57GMDpTIyCLJMHJLHRMXX4+6Qvu227 LmOAJ7ZEHZWm3LAiFi+Q06AloveeOOt9QZP+suQjH5/m55FZSN9NfXQ3voIn2hbUpt7Y Ya1IXHargzHZKsDB0FHy4H/j1d/90rLJx8q7dI3okVHfaVDmtuARG3T1pGmH9x7nB64H 711CzGmLd5PpdcZC9WQPXbBMNhFE/+RuE4fW/Cl8LjNNwvrDBZddNM3a6100SkZhSHdv R9nhy6ppNsGfFXpzYLVj6i8gFkQSNK33Vtt10lzlWRweKriW87VPBMPxKvPiGXirLoBM o4ew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uLRoI7tv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e9si10564083ejj.498.2020.08.10.08.32.55; Mon, 10 Aug 2020 08:33:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uLRoI7tv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729085AbgHJPa1 (ORCPT + 99 others); Mon, 10 Aug 2020 11:30:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:37146 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729068AbgHJPaU (ORCPT ); Mon, 10 Aug 2020 11:30:20 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 012B820791; Mon, 10 Aug 2020 15:30:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597073419; bh=6LDwZ9MemVioxkevWcJgVIU5tf4B02bHCyvyWRpJXq4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uLRoI7tvVUTtfGSgT+GuTRbOupKfkvbG/Zg/tyxmHtCKaIoNS5hsunenUs2frLDIA i9t0jD+WAWRtpVpt8lXKzga3ymDInyQePcboi1eou8I6F5L7wmEEr5DcNvlNwzKUvK AxtRRvFFEI4aRjGa7uW1ufH7JuEUvu7wEtN7p6ag= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, ch3332xr@gmail.com, Cong Wang , "David S. Miller" Subject: [PATCH 4.19 33/48] ipv6: fix memory leaks on IPV6_ADDRFORM path Date: Mon, 10 Aug 2020 17:21:55 +0200 Message-Id: <20200810151805.844634203@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200810151804.199494191@linuxfoundation.org> References: <20200810151804.199494191@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Cong Wang [ Upstream commit 8c0de6e96c9794cb523a516c465991a70245da1c ] IPV6_ADDRFORM causes resource leaks when converting an IPv6 socket to IPv4, particularly struct ipv6_ac_socklist. Similar to struct ipv6_mc_socklist, we should just close it on this path. This bug can be easily reproduced with the following C program: #include #include #include #include #include int main() { int s, value; struct sockaddr_in6 addr; struct ipv6_mreq m6; s = socket(AF_INET6, SOCK_DGRAM, 0); addr.sin6_family = AF_INET6; addr.sin6_port = htons(5000); inet_pton(AF_INET6, "::ffff:192.168.122.194", &addr.sin6_addr); connect(s, (struct sockaddr *)&addr, sizeof(addr)); inet_pton(AF_INET6, "fe80::AAAA", &m6.ipv6mr_multiaddr); m6.ipv6mr_interface = 5; setsockopt(s, SOL_IPV6, IPV6_JOIN_ANYCAST, &m6, sizeof(m6)); value = AF_INET; setsockopt(s, SOL_IPV6, IPV6_ADDRFORM, &value, sizeof(value)); close(s); return 0; } Reported-by: ch3332xr@gmail.com Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Cong Wang Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- include/net/addrconf.h | 1 + net/ipv6/anycast.c | 17 ++++++++++++----- net/ipv6/ipv6_sockglue.c | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) --- a/include/net/addrconf.h +++ b/include/net/addrconf.h @@ -305,6 +305,7 @@ int ipv6_sock_ac_join(struct sock *sk, i const struct in6_addr *addr); int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr); +void __ipv6_sock_ac_close(struct sock *sk); void ipv6_sock_ac_close(struct sock *sk); int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr); --- a/net/ipv6/anycast.c +++ b/net/ipv6/anycast.c @@ -173,7 +173,7 @@ int ipv6_sock_ac_drop(struct sock *sk, i return 0; } -void ipv6_sock_ac_close(struct sock *sk) +void __ipv6_sock_ac_close(struct sock *sk) { struct ipv6_pinfo *np = inet6_sk(sk); struct net_device *dev = NULL; @@ -181,10 +181,7 @@ void ipv6_sock_ac_close(struct sock *sk) struct net *net = sock_net(sk); int prev_index; - if (!np->ipv6_ac_list) - return; - - rtnl_lock(); + ASSERT_RTNL(); pac = np->ipv6_ac_list; np->ipv6_ac_list = NULL; @@ -201,6 +198,16 @@ void ipv6_sock_ac_close(struct sock *sk) sock_kfree_s(sk, pac, sizeof(*pac)); pac = next; } +} + +void ipv6_sock_ac_close(struct sock *sk) +{ + struct ipv6_pinfo *np = inet6_sk(sk); + + if (!np->ipv6_ac_list) + return; + rtnl_lock(); + __ipv6_sock_ac_close(sk); rtnl_unlock(); } --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -207,6 +207,7 @@ static int do_ipv6_setsockopt(struct soc fl6_free_socklist(sk); __ipv6_sock_mc_close(sk); + __ipv6_sock_ac_close(sk); /* * Sock is moving from IPv6 to IPv4 (sk_prot), so