git: 46f09c4f099b - main - netstat: filtering out unsupported address families

From: Tai-hwa Liang <avatar_at_FreeBSD.org>
Date: Sat, 29 Aug 2026 13:14:18 UTC
The branch main has been updated by avatar:

URL: https://cgit.FreeBSD.org/src/commit/?id=46f09c4f099b11a997a58207c78bd5f5752cc853

commit 46f09c4f099b11a997a58207c78bd5f5752cc853
Author:     Tai-hwa Liang <avatar@FreeBSD.org>
AuthorDate: 2026-08-20 00:22:15 +0000
Commit:     Tai-hwa Liang <avatar@FreeBSD.org>
CommitDate: 2026-08-29 13:11:41 +0000

    netstat: filtering out unsupported address families
    
    getifaddrs(3) may return AF_INET6 addresses even when netstat(1)
    is built without INET6 support.  Avoid passing these addresses to
    process_ifa_addr(), which does not handle AF_INET6 in that case.
    
    This fixes corrupted column width calculations and runaway output
    from netstat -i, which could cause periodic daily check output to
    generate multi-gigabyte mail messages and exhaust disk space and
    memory.
    
    MFC after:      2 weeks
---
 usr.bin/netstat/if.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c
index 637aa4738d40..de2c5aeb8d06 100644
--- a/usr.bin/netstat/if.c
+++ b/usr.bin/netstat/if.c
@@ -383,6 +383,22 @@ max_num_len(int max_len, u_long num)
 	return (MAX(max_len, len));
 }
 
+static bool
+skip_ifa(const struct ifaddrs *ifa, sa_family_t af)
+{
+	if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
+		return (true);
+#ifndef INET6
+	/*
+	 * getifaddrs(3) may return AF_INET6 addresses even when
+	 * netstat(1) was built without INET6 support.
+	 */
+	if (ifa->ifa_addr->sa_family == AF_INET6)
+		return (true);
+#endif
+	return (false);
+}
+
 /*
  * Print a description of the network interfaces.
  */
@@ -407,7 +423,7 @@ intpr(void (*pfunc)(char *), int af)
 		if (interface != NULL &&
 		    strcmp(ifa->ifa_name, interface) != 0)
 			continue;
-		if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
+		if (skip_ifa(ifa, af))
 			continue;
 		ifn_len = strlen(ifa->ifa_name);
 		if ((ifa->ifa_flags & IFF_UP) == 0)
@@ -478,7 +494,7 @@ intpr(void (*pfunc)(char *), int af)
 			continue;
 		}
 
-		if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
+		if (skip_ifa(ifa, af))
 			continue;
 
 		xo_open_instance("interface");