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