public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] inet: getnameinfo fix serv for abstract socket [BZ #27634]
@ 2021-03-27  5:26 Daniel Black
  2021-03-27 18:57 ` Florian Weimer
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Black @ 2021-03-27  5:26 UTC (permalink / raw)
  To: 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
+   <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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-03-29  8:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-27  5:26 [PATCH] inet: getnameinfo fix serv for abstract socket [BZ #27634] Daniel Black
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

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).