4cc9c6
commit f545ad4928fa1f27a3075265182b38a4f939a5f7
4cc9c6
Author: Florian Weimer <fweimer@redhat.com>
4cc9c6
Date:   Mon Jan 17 10:21:34 2022 +0100
4cc9c6
4cc9c6
    CVE-2022-23218: Buffer overflow in sunrpc svcunix_create (bug 28768)
4cc9c6
    
4cc9c6
    The sunrpc function svcunix_create suffers from a stack-based buffer
4cc9c6
    overflow with overlong pathname arguments.
4cc9c6
    
4cc9c6
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
4cc9c6
4cc9c6
diff --git a/sunrpc/Makefile b/sunrpc/Makefile
4cc9c6
index 2f8f0597c99e117f..5f7087aee494cc2e 100644
4cc9c6
--- a/sunrpc/Makefile
4cc9c6
+++ b/sunrpc/Makefile
4cc9c6
@@ -95,7 +95,7 @@ others += rpcgen
4cc9c6
 endif
4cc9c6
 
4cc9c6
 tests = tst-xdrmem tst-xdrmem2 test-rpcent tst-udp-error tst-udp-timeout \
4cc9c6
-  tst-udp-nonblocking tst-bug22542
4cc9c6
+  tst-udp-nonblocking tst-bug22542 tst-bug28768
4cc9c6
 
4cc9c6
 xtests := tst-getmyaddr
4cc9c6
 
4cc9c6
diff --git a/sunrpc/svc_unix.c b/sunrpc/svc_unix.c
4cc9c6
index c2c076aa87f0a2ad..8fac2b35da1d38a5 100644
4cc9c6
--- a/sunrpc/svc_unix.c
4cc9c6
+++ b/sunrpc/svc_unix.c
4cc9c6
@@ -154,7 +154,10 @@ svcunix_create (int sock, u_int sendsize, u_int recvsize, char *path)
4cc9c6
   SVCXPRT *xprt;
4cc9c6
   struct unix_rendezvous *r;
4cc9c6
   struct sockaddr_un addr;
4cc9c6
-  socklen_t len = sizeof (struct sockaddr_in);
4cc9c6
+  socklen_t len = sizeof (addr);
4cc9c6
+
4cc9c6
+  if (__sockaddr_un_set (&addr, path) < 0)
4cc9c6
+    return NULL;
4cc9c6
 
4cc9c6
   if (sock == RPC_ANYSOCK)
4cc9c6
     {
4cc9c6
@@ -165,12 +168,6 @@ svcunix_create (int sock, u_int sendsize, u_int recvsize, char *path)
4cc9c6
 	}
4cc9c6
       madesock = TRUE;
4cc9c6
     }
4cc9c6
-  memset (&addr, '\0', sizeof (addr));
4cc9c6
-  addr.sun_family = AF_UNIX;
4cc9c6
-  len = strlen (path) + 1;
4cc9c6
-  memcpy (addr.sun_path, path, len);
4cc9c6
-  len += sizeof (addr.sun_family);
4cc9c6
-
4cc9c6
   __bind (sock, (struct sockaddr *) &addr, len);
4cc9c6
 
4cc9c6
   if (__getsockname (sock, (struct sockaddr *) &addr, &len) != 0
4cc9c6
diff --git a/sunrpc/tst-bug28768.c b/sunrpc/tst-bug28768.c
4cc9c6
new file mode 100644
4cc9c6
index 0000000000000000..35a4b7b0b3d34350
4cc9c6
--- /dev/null
4cc9c6
+++ b/sunrpc/tst-bug28768.c
4cc9c6
@@ -0,0 +1,42 @@
4cc9c6
+/* Test to verify that long path is rejected by svcunix_create (bug 28768).
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/svc.h>
4cc9c6
+#include <shlib-compat.h>
4cc9c6
+#include <string.h>
4cc9c6
+#include <support/check.h>
4cc9c6
+
4cc9c6
+/* svcunix_create does not have a default version in linkobj/libc.so.  */
4cc9c6
+compat_symbol_reference (libc, svcunix_create, svcunix_create, GLIBC_2_1);
4cc9c6
+
4cc9c6
+static int
4cc9c6
+do_test (void)
4cc9c6
+{
4cc9c6
+  char pathname[109];
4cc9c6
+  memset (pathname, 'x', sizeof (pathname));
4cc9c6
+  pathname[sizeof (pathname) - 1] = '\0';
4cc9c6
+
4cc9c6
+  errno = 0;
4cc9c6
+  TEST_VERIFY (svcunix_create (RPC_ANYSOCK, 4096, 4096, pathname) == NULL);
4cc9c6
+  TEST_COMPARE (errno, EINVAL);
4cc9c6
+
4cc9c6
+  return 0;
4cc9c6
+}
4cc9c6
+
4cc9c6
+#include <support/test-driver.c>