f21993
commit ef972a4c50014a16132b5c75571cfb6b30bef136
f21993
Author: Martin Sebor <msebor@redhat.com>
f21993
Date:   Mon Jan 17 10:21:34 2022 +0100
f21993
f21993
    sunrpc: Test case for clnt_create "unix" buffer overflow (bug 22542)
f21993
    
f21993
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
f21993
f21993
# Conflicts:
f21993
#	sunrpc/Makefile
f21993
f21993
diff --git a/sunrpc/Makefile b/sunrpc/Makefile
f21993
index 85b0b3356aaf81a3..2f8f0597c99e117f 100644
f21993
--- a/sunrpc/Makefile
f21993
+++ b/sunrpc/Makefile
f21993
@@ -95,7 +95,8 @@ others += rpcgen
f21993
 endif
f21993
 
f21993
 tests = tst-xdrmem tst-xdrmem2 test-rpcent tst-udp-error tst-udp-timeout \
f21993
-  tst-udp-nonblocking
f21993
+  tst-udp-nonblocking tst-bug22542
f21993
+
f21993
 xtests := tst-getmyaddr
f21993
 
f21993
 ifeq ($(have-thread-library),yes)
f21993
@@ -246,3 +247,4 @@ $(objpfx)tst-udp-timeout: $(common-objpfx)linkobj/libc.so
f21993
 $(objpfx)tst-udp-nonblocking: $(common-objpfx)linkobj/libc.so
f21993
 $(objpfx)tst-udp-garbage: \
f21993
   $(common-objpfx)linkobj/libc.so $(shared-thread-library)
f21993
+$(objpfx)tst-bug22542: $(common-objpfx)linkobj/libc.so
f21993
diff --git a/sunrpc/tst-bug22542.c b/sunrpc/tst-bug22542.c
f21993
new file mode 100644
f21993
index 0000000000000000..d6cd79787bdef21d
f21993
--- /dev/null
f21993
+++ b/sunrpc/tst-bug22542.c
f21993
@@ -0,0 +1,44 @@
f21993
+/* Test to verify that overlong hostname is rejected by clnt_create
f21993
+   and doesn't cause a buffer overflow (bug  22542).
f21993
+
f21993
+   Copyright (C) 2022 Free Software Foundation, Inc.
f21993
+   This file is part of the GNU C Library.
f21993
+
f21993
+   The GNU C Library is free software; you can redistribute it and/or
f21993
+   modify it under the terms of the GNU Lesser General Public
f21993
+   License as published by the Free Software Foundation; either
f21993
+   version 2.1 of the License, or (at your option) any later version.
f21993
+
f21993
+   The GNU C Library is distributed in the hope that it will be useful,
f21993
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
f21993
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f21993
+   Lesser General Public License for more details.
f21993
+
f21993
+   You should have received a copy of the GNU Lesser General Public
f21993
+   License along with the GNU C Library; if not, see
f21993
+   <http://www.gnu.org/licenses/>.  */
f21993
+
f21993
+#include <errno.h>
f21993
+#include <rpc/clnt.h>
f21993
+#include <string.h>
f21993
+#include <support/check.h>
f21993
+#include <sys/socket.h>
f21993
+#include <sys/un.h>
f21993
+
f21993
+static int
f21993
+do_test (void)
f21993
+{
f21993
+  /* Create an arbitrary hostname that's longer than fits in sun_path.  */
f21993
+  char name [sizeof ((struct sockaddr_un*)0)->sun_path * 2];
f21993
+  memset (name, 'x', sizeof name - 1);
f21993
+  name [sizeof name - 1] = '\0';
f21993
+
f21993
+  errno = 0;
f21993
+  CLIENT *clnt = clnt_create (name, 0, 0, "unix");
f21993
+
f21993
+  TEST_VERIFY (clnt == NULL);
f21993
+  TEST_COMPARE (errno, EINVAL);
f21993
+  return 0;
f21993
+}
f21993
+
f21993
+#include <support/test-driver.c>