From patchwork Sat Aug 17 08:13:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?TWlrbMOzcyBNw6F0w6k=?= X-Patchwork-Id: 1973441 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=RdLYd/EB; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WmBT96zCRz1yYl for ; Sat, 17 Aug 2024 18:13:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4EE80386074B for ; Sat, 17 Aug 2024 08:13:45 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id E84333858404 for ; Sat, 17 Aug 2024 08:13:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E84333858404 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E84333858404 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723882409; cv=none; b=mRUo9sbD3NK+HMBF2aIlr2AugWWXSSW3+JuDmb1hm5O7/tH2K9bdtxvzYwodhYOdnYemDThLStV4H3XS6rIhDwjl4/sCkDWj7YyoUgZA9talnCDPke83IHvT3nUbogwiKuKVOXD30icdQPzSaXRCgOo/Q1omyofuZOCLcDYnDEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723882409; c=relaxed/simple; bh=v+nW9i1ivANIPF9nh/AoFPkUhe8mHdUtiD8Qd/U71H4=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=l4zZLgAwMI/Ag2WtJALirdRZs5PXG70CswhF2P+zRn/A+h1OMGuVcOaIB6DWkMaiz5hn2/Cz5OQQQ7/haho0ABdU0QIsYTSlcBckkIRuNrchko280mju9ah2b8+pBfgUpDQQuGvEtVHZXkiEmLhDuZyq7WX/fGy1sUppKxv3NXs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-a7a83a968ddso333702866b.0 for ; Sat, 17 Aug 2024 01:13:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723882405; x=1724487205; darn=sourceware.org; h=subject:from:content-language:to:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=v+nW9i1ivANIPF9nh/AoFPkUhe8mHdUtiD8Qd/U71H4=; b=RdLYd/EBiAdYp+MumksQt57JG6UDP7JWXySdreValVHSqUXMDNrX+ta6+lUbSGOVgX Rfi+P5ugTsrs7hkbOUDhQQUJbBEyumqTudaNOaecU1D00cUCyTqG3q9qxf+/bN6yGReM X74IHAIWDiWg9FbBnZWL0gMRnXecmzQLHpjS19EtrbYi3Anu7y/YQSQb/eOAb7xv5dPt 50sj4kTQ34x2vdjvlCF2tnBEunPepyWUd1JNdWNxLwTxNuiOEyAKarsQ82k+Mbky7dzG 6cF5/fOXrxUAD5Zeqc+CIyaoRE94FSBHI9C6F273Sr0jw0d+W1pU6H/4Jm1SQBJTZIIv WhlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723882405; x=1724487205; h=subject:from:content-language:to:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=v+nW9i1ivANIPF9nh/AoFPkUhe8mHdUtiD8Qd/U71H4=; b=obKpE7TuXZu7QmD8AvUXZivYzAttsjRO8C1dUnAc5haPkdHhlSD/Vkv3D5N0B88ID3 HuIDAqe8933xKYEEkG2KD75U9ApNC5/lcmeyJOqYE5nl0LuaUnPzshGzLngc838KCcAZ ja+Suj/9X6fey2pWQbE2T3Dp1vR6BY1UjvC9Ql/fK+K+cZ1x2WODmzwrpdO7JcnqXVuK vfNqdsNmtoDzc+KcUwmJae0Tl/q2+lddgYJm1f+MV65TV9idfGIFnTSAA6+L83NVLkl4 hCBXyAm0piSuzUpWPexwvJN5wjBFQcBqgTiqNHoeotfumbQSgv334raKJ2qaSf70KUYu ednA== X-Gm-Message-State: AOJu0YwnEFNahP+UdN0aNjAXXTeVZR6NHwnVbtM3ga8PeMiyXSpzMsuv F/U6zuC7+Ye/PSSmfPaa355AOLR3NhTCoJ07GcTNMby7UCWM+OVeb1IiTw== X-Google-Smtp-Source: AGHT+IG7MmdUEafK+OleX7HheDqUWgqeYNgr9Wu0kOvO9Gx1lwXhhPog2krlpfSLwWvn34mHIfY97g== X-Received: by 2002:a17:907:e62a:b0:a7a:8e0f:aaed with SMTP id a640c23a62f3a-a83929d26c1mr390307566b.50.1723882405221; Sat, 17 Aug 2024 01:13:25 -0700 (PDT) Received: from [10.68.1.209] (178-164-216-40.pool.digikabel.hu. [178.164.216.40]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-a83838cfe70sm367675266b.75.2024.08.17.01.13.24 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Aug 2024 01:13:24 -0700 (PDT) Message-ID: Date: Sat, 17 Aug 2024 10:13:22 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: libc-alpha@sourceware.org Content-Language: en-US, hu-HU From: =?utf-8?b?TWlrbMOzcyBNw6F0w6k=?= Subject: [PATCH] nss: fix getaddrinfo() accepting garbage as valid IPv4 address X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HK_RANDOM_ENVFROM, HK_RANDOM_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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~incoming=patchwork.ozlabs.org@sourceware.org Using inet_aton() for numeric addresses is not a good idea, because it accepts non-RFC-compliant strings, like "1", "1.2", "1.2.3", "123456", "0xbeef" etc. as a valid IPv4 address, and this behavior is even documented in its man page. Note that when .ai_family=AF_INET, and the numeric address decoding fails, in the next step getaddrinfo() calls gethostbyname(), which tries to decode it as numeric again (see digits_dots.c). I tested getaddrinfo() on other systems: - on FreeBSD it's broken like in glibc - on Windows the WinSock library only accepts RFC-compliant addresses This patch also includes a new test case in test 3, and fixes the port number in test 2. From c4ba83107fea1d61a309bd30370a357a5a4c053d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20M=C3=A1t=C3=A9?= Date: Sat, 17 Aug 2024 10:07:12 +0200 Subject: [PATCH] nss: fix getaddrinfo() accepting garbage as valid IPv4 address Using inet_aton() for numeric addresses is not a good idea, because it accepts non-RFC-compliant strings, like "1", "1.2", "1.2.3", "123456", "0xbeef" etc. as a valid IPv4 address, and this behavior is even documented in its man page. Note that when .ai_family=AF_INET, and the numeric address decoding fails, in the next step getaddrinfo() calls gethostbyname(), which tries to decode it as numeric again (see digits_dots.c). I tested getaddrinfo() on other systems: - on FreeBSD it's broken like in glibc - on Windows the WinSock library only accepts RFC-compliant addresses This patch also includes a new test case in test 3, and fixes the port number in test 2. --- nss/digits_dots.c | 2 +- nss/getaddrinfo.c | 2 +- nss/tst-getaddrinfo2.c | 2 +- nss/tst-getaddrinfo3.c | 9 +++++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/nss/digits_dots.c b/nss/digits_dots.c index 15f8c383b8..5d6cc4b1a2 100644 --- a/nss/digits_dots.c +++ b/nss/digits_dots.c @@ -158,7 +158,7 @@ __nss_hostname_digits_dots_context (struct resolv_context *ctx, 255.255.255.255? The test below will succeed spuriously... ??? */ if (af == AF_INET) - ok = __inet_aton_exact (name, (struct in_addr *) host_addr); + ok = inet_pton (AF_INET6, name, (struct in_addr *) host_addr) > 0; else { assert (af == AF_INET6); diff --git a/nss/getaddrinfo.c b/nss/getaddrinfo.c index 3ccd3905fa..6e6741e9de 100644 --- a/nss/getaddrinfo.c +++ b/nss/getaddrinfo.c @@ -879,7 +879,7 @@ text_to_binary_address (const char *name, const struct addrinfo *req, assert (at != NULL); memset (at->addr, 0, sizeof (at->addr)); - if (__inet_aton_exact (name, (struct in_addr *) at->addr) != 0) + if (inet_pton (AF_INET, name, (struct in_addr *) at->addr) == 1) { if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET) at->family = AF_INET; diff --git a/nss/tst-getaddrinfo2.c b/nss/tst-getaddrinfo2.c index d8be4a8e8f..7a884a9e3f 100644 --- a/nss/tst-getaddrinfo2.c +++ b/nss/tst-getaddrinfo2.c @@ -10,7 +10,7 @@ static int do_test (void) { - const char portstr[] = "583"; + const char portstr[] = "513"; int port = atoi (portstr); struct addrinfo hints, *aires, *pai; int rv; diff --git a/nss/tst-getaddrinfo3.c b/nss/tst-getaddrinfo3.c index 5077f311fc..255ac1ef74 100644 --- a/nss/tst-getaddrinfo3.c +++ b/nss/tst-getaddrinfo3.c @@ -34,8 +34,8 @@ do_test (void) } \ else if (ai_res->ai_family != fam) \ { \ - printf ("\ -getaddrinfo test %d return address of family %d, expected %d\n", \ + printf ( \ + "getaddrinfo test %d return address of family %d, expected %d\n", \ no, ai_res->ai_family, fam); \ result = 1; \ } \ @@ -144,6 +144,11 @@ getaddrinfo test %d return address of family %d, expected %d\n", \ hints.ai_socktype = SOCK_STREAM; T (10, 0, "::ffff:127.0.0.1", AF_INET6, "::ffff:127.0.0.1"); + memset (&hints, '\0', sizeof (hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + T (11, EAI_NONAME, "1", AF_INET, ""); + return result; } -- 2.45.2