public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Daniel Black <daniel@mariadb.org>
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	[thread overview]
Message-ID: <20210327052649.1728350-1-daniel@mariadb.org> (raw)

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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <netdb.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <string.h>
+#include <stddef.h>
+
+#include <support/check.h>
+
+#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"
-- 
2.30.2


             reply	other threads:[~2021-03-27  5:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-27  5:26 Daniel Black [this message]
2021-03-27 18:57 ` Florian Weimer
2021-03-27 22:32   ` Daniel Black
2021-03-29  8:22     ` Florian Weimer
2021-03-28 21:55   ` Daniel Black

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210327052649.1728350-1-daniel@mariadb.org \
    --to=daniel@mariadb.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).