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