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