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