@@ -321,6 +321,7 @@ tests := \
tst-getaddrinfo \
tst-getaddrinfo2 \
tst-getaddrinfo3 \
+ tst-getaddrinfo6 \
tst-gethnm \
tst-getpw \
tst-gshadow \
@@ -2377,6 +2377,12 @@ getaddrinfo (const char *name, const char *service,
gaih_service.num = -1;
}
+ else if (gaih_service.num == 0 || gaih_service.num > USHRT_MAX)
+ {
+ /* provided numeric string is invalid */
+ __free_in6ai (in6ai);
+ return EAI_NONAME;
+ }
pservice = &gaih_service;
}
new file mode 100644
@@ -0,0 +1,64 @@
+/* Test invalid port/service lookup [BZ #16208].
+ Copyright (C) 2014-2024 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 <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+static int
+do_one_test (const char *service)
+{
+ const int family[2] = { AF_INET, AF_INET6 };
+ struct addrinfo hints, *aitop;
+ int index, gaierr;
+ int result = 0;
+
+ for (index = 0; index < sizeof (family) / sizeof (family[0]); ++index)
+ {
+ memset (&hints, '\0', sizeof (hints));
+ hints.ai_family = family[index];
+
+ gaierr = getaddrinfo (NULL, service, &hints, &aitop);
+ if (gaierr != EAI_NONAME)
+ {
+ printf ("FAIL getaddrinfo returned %d, should return %d\n",
+ gaierr, EAI_NONAME);
+ result = 1;
+ }
+ }
+
+ return result;
+}
+
+
+static int
+do_test (void)
+{
+ int err = 0;
+
+ err |= do_one_test ("0");
+ err |= do_one_test ("70000");
+
+ return err;
+}
+#define TEST_FUNCTION do_test ()
+
+#include "../test-skeleton.c"
When passing a numeric string as port/service to getaddrinfo() representing a number larger than 65535, the function will not complain and will simply extract the lowest 16bits of the converted number. Specifically, the string is converted to int by calling strtoul() and later it is moved to gaih_servtuple.num after passing through htons(). For example, invoking getaddrinfo() with service equal to "70000" will result in no error and ai_addr->sin_port set to 4464. This issue was (re)discovered by researcher Anqi Chen while stress testing OpenVPN with invalid config parameters. Thanks a lot to Arne Schwabe for finding out that the root cause was hidden inside glibc. Similarly, also when passing 0 no error is thrown while it should. Add proper check to reject invalid values immediately. Cc: Arne Schwabe <arne@rfc2549.org> Cc: Cristina Nita-Rotaru <crisn@ccs.neu.edu> Reported-by: Anqi Chen <chen.anqi3@northeastern.edu> Signed-off-by: Antonio Quartulli <a@unstable.cc> --- nss/Makefile | 1 + nss/getaddrinfo.c | 6 ++++ nss/tst-getaddrinfo6.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 nss/tst-getaddrinfo6.c