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