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