From patchwork Sat Oct 25 00:23:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Griffis X-Patchwork-Id: 122603 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 27D61385802C for ; Sat, 25 Oct 2025 00:25:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27D61385802C Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (2048-bit key, unprotected) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=ItjwIxRN X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by sourceware.org (Postfix) with ESMTPS id AD6D13858D39 for ; Sat, 25 Oct 2025 00:24:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AD6D13858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=igalia.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AD6D13858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1761351857; cv=none; b=QVOVriiTHhdDGtVMrYEDl8VZk0nXZzfTd40CCUPLqAc0TOHssK+cTQKE3fQWigil+c8wFiIldeM7dnT17ap8xpayvEtH9qb3xzj4Oq/+zrpIzeAHudIaV5h5ptFve/FN6pKedkPmY5oHS/hw6HTDCwTqXLc749541G0LLv6Twbw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1761351857; c=relaxed/simple; bh=vGd8L/nHkPNrjFnYEZje/ZekEm+JZp9hOK/HJMMME08=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fxW1bEAJXw2Sl6XiPEEM664SRinquFuHt5ILQJtV+JT0fi0InBZxnFB3LcaluP1Qh8c0vZn7g6U5ybnezyi3o4IWuWSZBdL03XAITyQix6QKo1uIg/HLCM+wHeVWp2BoFz9On1suBHe0TCdD5LuQoZjprJYzx4tIrZ9jPatoLyE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD6D13858D39 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Qp6Z05wprSjvYAhzeFskMlQd042LZM5Fu/nHn9J/wj8=; b=ItjwIxRNbEe2fCdA18Quk11H+N nIJFoPC6T1QHwOE5lbNdOIXuGI//+23IrsxzEl3qz7z4c8TSg5vHZbA0Abv4VupNwfyWCgBYybncT Ug11oN6SupW7z46ljzb1rVOLL9yaK3WA2l+oACEjRXbfkgh4cBiYS1uu09fMpggsIrtr+wVPjvrZh k+8O8WK9bVlZXjX5H0raQURgjMrDOH+vU2biisBLKQ2PxxJ9YtkHPHvpZ57nIhRrNcHV9ohUo+ia5 vXbb0YbvvzOXUwOm/Y+YZvMXuC/+cLyeKIEvh5oMJK3YRwyJ2s+uOANteXU5KqHf1LNCNfsV9J7k3 rpO5vuag==; Received: from [65.128.106.6] (helo=desktop) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1vCS4s-00ExDt-Ku; Sat, 25 Oct 2025 02:24:14 +0200 From: Patrick Griffis To: libc-alpha@sourceware.org Cc: Patrick Griffis Subject: [PATCH] nss: Use gethostbyname4_r when requesting IPv6 addresses (bug 14413) Date: Fri, 24 Oct 2025 19:23:59 -0500 Message-ID: <20251025002359.2093627-1-pgriffis@igalia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251016153951.1079645-1-pgriffis@igalia.com> References: <20251016153951.1079645-1-pgriffis@igalia.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org If getaddrinfo() is called with family of AF_INET6 it would return IPv6 addresses lacking scope_id information. This was a regression caused by bug 14505. That patch avoided calling gethostbyname4_r unless AF_UNSPEC is passed. The reasoning was that gethostbyname4_r has no ability to only ask for a specific family. That is true however gethostbyname3_r returns a list of hostent structs which lack scope_id information. The solution is to always use gethostbyname4_r if IPv6 is requested. This does have the downside of IPv4 addresses being looked up by modules even when they are not needed. --- nss/getaddrinfo.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/nss/getaddrinfo.c b/nss/getaddrinfo.c index 6726ace6fd..e7064ea9f5 100644 --- a/nss/getaddrinfo.c +++ b/nss/getaddrinfo.c @@ -610,6 +610,7 @@ get_nss_addresses (const char *name, const struct addrinfo *req, struct resolv_context *res_ctx = NULL; bool do_merge = false; int result = 0; + bool got_ipv4 = false; no_more = !__nss_database_get (nss_database_hosts, &nip); @@ -644,9 +645,12 @@ get_nss_addresses (const char *name, const struct addrinfo *req, no_data = 0; nss_gethostbyname4_r *fct4 = NULL; - /* gethostbyname4_r sends out parallel A and AAAA queries and - is thus only suitable for PF_UNSPEC. */ - if (req->ai_family == PF_UNSPEC) + /* The gethostbyname4_r API is always expected to return both address families + * which means we would prefer gethostbyname3_r when requesting only IPv4 addresses. + * Unfortunately when requesting IPv6 only we have to use this API for link-local + * information (scope_id is not in the hostent struct). + * This means we may collect unncessary IPv4 addresses. */ + if (req->ai_family != AF_INET) fct4 = __nss_lookup_function (nip, "gethostbyname4_r"); if (fct4 != NULL) @@ -700,6 +704,9 @@ get_nss_addresses (const char *name, const struct addrinfo *req, while (*pat != NULL) { + if ((*pat)->family == AF_INET) + got_ipv4 = true; + if ((*pat)->family == AF_INET && req->ai_family == AF_INET6 && (req->ai_flags & AI_V4MAPPED) != 0) @@ -856,6 +863,11 @@ get_nss_addresses (const char *name, const struct addrinfo *req, /* If both requests timed out report this. */ if (no_data == EAI_AGAIN && no_inet6_data == EAI_AGAIN) result = -EAI_AGAIN; + else if (req->ai_family == AF_INET6 && !res->got_ipv6 && got_ipv4) + /* If only IPv6 addresses were requested and we only got IPv4 addresses + * we ignore those. This is less accurate but matches previous behavior. + * The cause of this is commented above when using gethostbyname4_r */ + result = -EAI_NONAME; else /* We made requests but they turned out no data. The name is known, though. */