From patchwork Sat Mar 27 05:26:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Black X-Patchwork-Id: 42786 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 C215A3858028; Sat, 27 Mar 2021 05:27:04 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by sourceware.org (Postfix) with ESMTPS id 829BB385800F for ; Sat, 27 Mar 2021 05:27:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 829BB385800F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=mariadb.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=daniel@mariadb.org Received: by mail-pl1-x643.google.com with SMTP id v23so1910409ple.9 for ; Fri, 26 Mar 2021 22:27:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mariadb.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=somUhi4+cykK6dbaW5ocKb7EKBr+d8pCZhyE34k6Gf8=; b=j1Ch1PKrjTjsBjGLN/aqp0hpzIBqMk9QRDxyiRk1iD3/BObVVnchiKEGfNdUPyaKu/ 80C30eEapYZ5vttLfC7SZ2kmkHxwu44d8Sjl1h6HXl3cRDxHrZ748Tz1qFzsEmxnWU+Q 2/OrM3s8/CrijR62rZfytpdgoIPPu8qL6FF8RH97LA2GOjnH2UppOhUtI1BYwRvPY0K4 7xZjxOUQG1NRvYxNW1fCgbeOIPtPrUpR4eZCzD0YkzBjskEcTSuE8jHzoB8oYznnQ2oU 9lZlTwaDIHiDWO00VKNybBXOHcZQa2cDNmGyQYLwBJzTd7N3+IX63ir8zGQxCFTghQfZ GZzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=somUhi4+cykK6dbaW5ocKb7EKBr+d8pCZhyE34k6Gf8=; b=MSzNmyHGdzYZ6exGDviMnq0sg16HYCVqRIACfSvVPbM8DsG5Y/E8JdWSvOZ35/FmR2 2N0xEVL8hUhe+J0burd+sqNFfzz9sXZTFfHV4qsyPJqLIMIaiLnxxcsknYdQ9iKt/rKu rXrRibp03Ssx9gXdkRpVR1uKb42rzsqALhG1IbyzWPCcRfhfD3my6CTdicC/6A9eji/I GW/+bLtCN9tn7MloL4WugEQlgR9lTO+015R1Viuxq+73hHQTudKlIz0VGdEFZd2uhbQW wXnihDxRElR9vG4VjmqD21x8gvPC3wrzk5F4W+gdMU/hvA3TJi4KuGpW0p09tLN3LTJQ fi7A== X-Gm-Message-State: AOAM5311wqFggz/JApHmklPplAT9iKsFnW+OYPVW31zDngPVRyGrJL/K DBd2HPn/hgD+ktcSgqFAlUwWokamOKXwT7fT X-Google-Smtp-Source: ABdhPJyWXriNXBzhUPOPSfm94+elUDpD40P87f4uY5XS6NgbyK8yXwhL09M73a+euOKl14pJqU9csA== X-Received: by 2002:a17:902:e882:b029:e6:caba:fff6 with SMTP id w2-20020a170902e882b02900e6cabafff6mr18173187plg.73.1616822819206; Fri, 26 Mar 2021 22:26:59 -0700 (PDT) Received: from localhost.localdomain (2001-44b8-1125-6b00-46f9-6738-2923-7bf8.static.ipv6.internode.on.net. [2001:44b8:1125:6b00:46f9:6738:2923:7bf8]) by smtp.gmail.com with ESMTPSA id j35sm11181809pgj.45.2021.03.26.22.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Mar 2021 22:26:58 -0700 (PDT) From: Daniel Black To: libc-alpha@sourceware.org Subject: [PATCH] inet: getnameinfo fix serv for abstract socket [BZ #27634] Date: Sat, 27 Mar 2021 16:26:49 +1100 Message-Id: <20210327052649.1728350-1-daniel@mariadb.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Abstract sockets were not copied because they began with \0. They can contain any character, so the full size is used consistent with how they are created (man 7 unix). --- inet/Makefile | 2 +- inet/getnameinfo.c | 11 +++++++- inet/tst-getni-local.c | 63 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 inet/tst-getni-local.c diff --git a/inet/Makefile b/inet/Makefile index cf4cf5cf..3b0d69be 100644 --- a/inet/Makefile +++ b/inet/Makefile @@ -56,7 +56,7 @@ aux := check_pf check_native ifreq tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-network \ tst-gethnm test-ifaddrs bug-if1 test-inet6_opt tst-ether_line \ tst-getni1 tst-getni2 tst-inet6_rth tst-checks tst-checks-posix \ - tst-sockaddr test-hnto-types tst-if_index-long + tst-sockaddr test-hnto-types tst-if_index-long tst-getni-local # tst-deadline must be linked statically so that we can access # internal functions. diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c index 8380d857..58d138d3 100644 --- a/inet/getnameinfo.c +++ b/inet/getnameinfo.c @@ -396,7 +396,7 @@ gni_host_local (struct scratch_buffer *tmpbuf, return checked_copy (host, hostlen, "localhost"); } -/* Convert the host part of an AF_LOCAK socket address. */ +/* Convert the host part of an AF_LOCAL socket address. */ static int gni_host (struct scratch_buffer *tmpbuf, const struct sockaddr *sa, socklen_t addrlen, @@ -459,6 +459,15 @@ gni_serv_local (struct scratch_buffer *tmpbuf, const struct sockaddr *sa, socklen_t addrlen, char *serv, socklen_t servlen, int flags) { + if (((const struct sockaddr_un *) sa)->sun_path[0] == '\0') + { + /* Abstract socket */ + socklen_t len = addrlen - offsetof (struct sockaddr_un, sun_path); + if (len > servlen) + return EAI_OVERFLOW; + memcpy (serv, ((const struct sockaddr_un *) sa)->sun_path, len); + return 0; + } return checked_copy (serv, servlen, ((const struct sockaddr_un *) sa)->sun_path); } diff --git a/inet/tst-getni-local.c b/inet/tst-getni-local.c new file mode 100644 index 00000000..8c39fb27 --- /dev/null +++ b/inet/tst-getni-local.c @@ -0,0 +1,63 @@ +/* Test for getnameinfo AF_LOCAL/UNIX sockets + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +#include + +#define TEST_SOCK1 "my funky socket" +static int +do_test (void) +{ + struct sockaddr_un s; + char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; + const char abstract[] = { 0, 'a', 'b', 's', 't', 'r', 'a', 'c', 't', 0, + '!' }; + int len = sizeof(abstract) + offsetof (struct sockaddr_un, sun_path); + + s.sun_family = AF_UNIX; + strcpy (s.sun_path, TEST_SOCK1); + + TEST_VERIFY (getnameinfo ((struct sockaddr *) &s, sizeof (s), hbuf, + sizeof (hbuf), sbuf, sizeof (sbuf), + NI_NUMERICHOST | NI_NUMERICSERV) == 0); + + TEST_VERIFY (strncmp ("localhost", hbuf, NI_MAXHOST) == 0); + TEST_VERIFY (strncmp (TEST_SOCK1, sbuf, NI_MAXSERV) == 0); + + memset ( hbuf, 0, NI_MAXHOST ); + memset ( sbuf, 0, NI_MAXSERV ); + + memcpy ( s.sun_path, abstract, sizeof (abstract) ); + + TEST_VERIFY (getnameinfo ((struct sockaddr *) &s, len, hbuf, sizeof (hbuf), + sbuf, sizeof (sbuf), + NI_NUMERICHOST | NI_NUMERICSERV) == 0); + + TEST_VERIFY (strncmp ("localhost", hbuf, NI_MAXHOST) == 0); + TEST_VERIFY (memcmp (abstract, sbuf, sizeof (abstract)) == 0); + + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c"