50f89d
commit 899478c2bfa00c5df8d8bedb52effbb065700278
50f89d
Author: Florian Weimer <fweimer@redhat.com>
50f89d
Date:   Thu Dec 6 15:39:50 2018 +0100
50f89d
50f89d
    inet/tst-if_index-long: New test case for CVE-2018-19591 [BZ #23927]
50f89d
50f89d
diff --git a/inet/Makefile b/inet/Makefile
50f89d
index 09f5ba78fc5f3120..7782913b4c06f057 100644
50f89d
--- a/inet/Makefile
50f89d
+++ b/inet/Makefile
50f89d
@@ -52,7 +52,7 @@ aux := check_pf check_native ifreq
50f89d
 tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-network \
50f89d
 	 tst-gethnm test-ifaddrs bug-if1 test-inet6_opt tst-ether_line \
50f89d
 	 tst-getni1 tst-getni2 tst-inet6_rth tst-checks tst-checks-posix \
50f89d
-	 tst-sockaddr test-hnto-types
50f89d
+	 tst-sockaddr test-hnto-types tst-if_index-long
50f89d
 
50f89d
 # tst-deadline must be linked statically so that we can access
50f89d
 # internal functions.
50f89d
diff --git a/inet/tst-if_index-long.c b/inet/tst-if_index-long.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..3dc74874e5310945
50f89d
--- /dev/null
50f89d
+++ b/inet/tst-if_index-long.c
50f89d
@@ -0,0 +1,61 @@
50f89d
+/* Check for descriptor leak in if_nametoindex with a long interface name.
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+/* This test checks for a descriptor leak in case of a long interface
50f89d
+   name (CVE-2018-19591, bug 23927).  */
50f89d
+
50f89d
+#include <errno.h>
50f89d
+#include <net/if.h>
50f89d
+#include <netdb.h>
50f89d
+#include <string.h>
50f89d
+#include <support/check.h>
50f89d
+#include <support/descriptors.h>
50f89d
+#include <support/support.h>
50f89d
+
50f89d
+static int
50f89d
+do_test (void)
50f89d
+{
50f89d
+  struct support_descriptors *descrs = support_descriptors_list ();
50f89d
+
50f89d
+  /* Prepare a name which is just as long as required for trigging the
50f89d
+     bug.  */
50f89d
+  char name[IFNAMSIZ + 1];
50f89d
+  memset (name, 'A', IFNAMSIZ);
50f89d
+  name[IFNAMSIZ] = '\0';
50f89d
+  TEST_COMPARE (strlen (name), IFNAMSIZ);
50f89d
+  struct ifreq ifr;
50f89d
+  TEST_COMPARE (strlen (name), sizeof (ifr.ifr_name));
50f89d
+
50f89d
+  /* Test directly via if_nametoindex.  */
50f89d
+  TEST_COMPARE (if_nametoindex (name), 0);
50f89d
+  TEST_COMPARE (errno, ENODEV);
50f89d
+  support_descriptors_check (descrs);
50f89d
+
50f89d
+  /* Same test via getaddrinfo.  */
50f89d
+  char *host = xasprintf ("fea0::%%%s", name);
50f89d
+  struct addrinfo hints = { .ai_flags = AI_NUMERICHOST, };
50f89d
+  struct addrinfo *ai;
50f89d
+  TEST_COMPARE (getaddrinfo (host, NULL, &hints, &ai), EAI_NONAME);
50f89d
+  support_descriptors_check (descrs);
50f89d
+
50f89d
+  support_descriptors_free (descrs);
50f89d
+
50f89d
+  return 0;
50f89d
+}
50f89d
+
50f89d
+#include <support/test-driver.c>