From nobody Thu Jan  6 20:48:30 2022
X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
	by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 77A40193B1C4;
	Thu,  6 Jan 2022 20:48:31 +0000 (UTC)
	(envelope-from git@FreeBSD.org)
Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256
	 client-signature RSA-PSS (4096 bits) client-digest SHA256)
	(Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK))
	by mx1.freebsd.org (Postfix) with ESMTPS id 4JVJNH0t5gz3M0R;
	Thu,  6 Jan 2022 20:48:31 +0000 (UTC)
	(envelope-from git@FreeBSD.org)
Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(Client did not present a certificate)
	by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id F2A7726779;
	Thu,  6 Jan 2022 20:48:30 +0000 (UTC)
	(envelope-from git@FreeBSD.org)
Received: from gitrepo.freebsd.org ([127.0.1.44])
	by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 206KmUgs087206;
	Thu, 6 Jan 2022 20:48:30 GMT
	(envelope-from git@gitrepo.freebsd.org)
Received: (from git@localhost)
	by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 206KmUHp087205;
	Thu, 6 Jan 2022 20:48:30 GMT
	(envelope-from git)
Date: Thu, 6 Jan 2022 20:48:30 GMT
Message-Id: <202201062048.206KmUHp087205@gitrepo.freebsd.org>
To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org,
        dev-commits-src-main@FreeBSD.org
From: Ryan Stone <rstone@FreeBSD.org>
Subject: git: 5adea417d494 - main - Fix ifa refcount leak in ifa_ifwithnet()
List-Id: Commit messages for the main branch of the src repository <dev-commits-src-main.freebsd.org>
List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main
List-Help: <mailto:dev-commits-src-main+help@freebsd.org>
List-Post: <mailto:dev-commits-src-main@freebsd.org>
List-Subscribe: <mailto:dev-commits-src-main+subscribe@freebsd.org>
List-Unsubscribe: <mailto:dev-commits-src-main+unsubscribe@freebsd.org>
Sender: owner-dev-commits-src-main@freebsd.org
X-BeenThere: dev-commits-src-main@freebsd.org
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Git-Committer: rstone
X-Git-Repository: src
X-Git-Refname: refs/heads/main
X-Git-Reftype: branch
X-Git-Commit: 5adea417d494b9c0e186cf2d06f98873d02d1834
Auto-Submitted: auto-generated
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org;
	s=dkim; t=1641502111;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding;
	bh=O816md7CTyrhUugGzoHK37p5rL9+Fq3d8Tp+6O0Tdl8=;
	b=u4cv0I4o90dBjIf97RXpdeaEGY6vRRmUdm/hZv264g6cR9BZiGlcj3kJeNdaWxi3gWtGXN
	4MoX0c/Yx4E1ZIZ2DDP93ShodpNDF24EGQ4VTViBL9XbnNVZYux177GosQlQbewkp4pS0J
	EP4MMgoE+tcZqwLf1UQTNAf0nzy3VK5JDNraTqmVp96tS5sRu8qyZ5Rp0W1meBuwAwFtre
	pjvtLaZxdsDyUs0okiieX5Bj4AKCyS0cyCanJRpc3jXQzb5ClTwJQ+WC5pIq9q+CQgUJ23
	mwfAtFZpl/IIE0UHnbWESVHl7VkfAoOJMqE2knsgy5glb6gY/TZ1AkGqC797kw==
ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1641502111; a=rsa-sha256; cv=none;
	b=Oqep+dyv6iqohlCHr/+KxhcbQ1jQ7/fVlI3QiTQcwTFplqkdWZEqZHl/P6lxtMFaHmmFVL
	HPzBgrY2bCm8W+EMZC//U6OB3dCrqW/XSMkccg7wetwyE7bxpCpSvV236f0s7wnE/ISejA
	0m0pbqTYVvbR5fkq6SxTrG8+L6QNU8VPNPJqbc3xMCkLfIizlPW7JyhSXZMF7S/mGcADD/
	VHfSLR9fKlK7U3FVFlGvXfkg19FzQmbGZFWURR+inc7fcAVDa2C6ehBTL1mE3opS9r8FHo
	iZMgWMzyusrzH2YOZMZkGBiVmb3FufGSiPcHPsp1pPhdwzqRocnpxVW8Rswr/w==
ARC-Authentication-Results: i=1;
	mx1.freebsd.org;
	none
X-ThisMailContainsUnwantedMimeParts: N

The branch main has been updated by rstone:

URL: https://cgit.FreeBSD.org/src/commit/?id=5adea417d494b9c0e186cf2d06f98873d02d1834

commit 5adea417d494b9c0e186cf2d06f98873d02d1834
Author:     Ryan Stone <rstone@FreeBSD.org>
AuthorDate: 2021-02-11 16:17:58 +0000
Commit:     Ryan Stone <rstone@FreeBSD.org>
CommitDate: 2022-01-06 20:04:24 +0000

    Fix ifa refcount leak in ifa_ifwithnet()
    
    In 4f6c66cc9c75c8, ifa_ifwithnet() was changed to no longer
    ifa_ref() the returned ifaddr, and instead the caller was required
    to stay in the net_epoch for as long as they wanted the ifaddr
    to remain valid.  However, this missed the case where an AF_LINK
    lookup would call ifaddr_byindex(), which still does ifa_ref()
    the ifaddr.  This would cause a refcount leak.
    
    Fix this by inlining the relevant parts of ifaddr_byindex() here,
    with the ifa_ref() call removed.  This also avoids an unnecessary
    entry and exit from the net_epoch for this case.
    
    I've audited all in-tree consumers of ifa_ifwithnet() that could
    possibly perform an AF_LINK lookup and confirmed that none of them
    will expect the ifaddr to have a reference that they need to
    release.
    
    MFC after: 2 months
    Sponsored by: Dell Inc
    Differential Revision:  https://reviews.freebsd.org/D28705
    Reviewed by: melifaro
---
 sys/net/if.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/sys/net/if.c b/sys/net/if.c
index 84436c0074c8..8700485ff6a5 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -1953,6 +1953,7 @@ ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
 	struct ifaddr *ifa_maybe = NULL;
 	u_int af = addr->sa_family;
 	const char *addr_data = addr->sa_data, *cplim;
+	const struct sockaddr_dl *sdl;
 
 	NET_EPOCH_ASSERT();
 	/*
@@ -1960,9 +1961,14 @@ ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
 	 * so do that if we can.
 	 */
 	if (af == AF_LINK) {
-	    const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
-	    if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
-		return (ifaddr_byindex(sdl->sdl_index));
+		sdl = (const struct sockaddr_dl *)addr;
+		if (sdl->sdl_index && sdl->sdl_index <= V_if_index) {
+			ifp = ifnet_byindex(sdl->sdl_index);
+			if (ifp == NULL)
+				return (NULL);
+
+			return (ifp->if_addr);
+		}
 	}
 
 	/*