arrfab / rpms / glibc

Forked from rpms/glibc 4 years ago
Clone

Blame SOURCES/glibc-rh1443872.patch

147e83
commit 2eecc8afd02d8c65cf098cbae4de87f332dc21bd
147e83
Author: Florian Weimer <fweimer@redhat.com>
147e83
Date:   Mon Nov 9 12:48:41 2015 +0100
147e83
147e83
    Terminate process on invalid netlink response from kernel [BZ #12926]
147e83
    
147e83
    The recvmsg system calls for netlink sockets have been particularly
147e83
    prone to picking up unrelated data after a file descriptor race
147e83
    (where the descriptor is closed and reopened concurrently in a
147e83
    multi-threaded process, as the result of a file descriptor
147e83
    management issue elsewhere).  This commit adds additional error
147e83
    checking and aborts the process if a datagram of unexpected length
147e83
    (without the netlink header) is received, or an error code which
147e83
    cannot happen due to the way the netlink socket is used.
147e83
    
147e83
            [BZ #12926]
147e83
            Terminate process on invalid netlink response.
147e83
            * sysdeps/unix/sysv/linux/netlinkaccess.h
147e83
            (__netlink_assert_response): Declare.
147e83
            * sysdeps/unix/sysv/linux/netlink_assert_response.c: New file.
147e83
            * sysdeps/unix/sysv/linux/Makefile [$(subdir) == inet]
147e83
            (sysdep_routines): Add netlink_assert_response.
147e83
            * sysdeps/unix/sysv/linux/check_native.c (__check_native): Call
147e83
            __netlink_assert_response.
147e83
            * sysdeps/unix/sysv/linux/check_pf.c (make_request): Likewise.
147e83
            * sysdeps/unix/sysv/linux/ifaddrs.c (__netlink_request): Likewise.
147e83
            * sysdeps/unix/sysv/linux/Versions (GLIBC_PRIVATE): Add
147e83
            __netlink_assert_response.
147e83
147e83
Conflicts:
147e83
	sysdeps/unix/sysv/linux/check_pf.c
147e83
	  Upstream commit fda389c8f0311dd5786be91a7b54b9f935fcafa1
147e83
          ("Fix infinite loop in check_pf (BZ #12926)") was not backported
147e83
	  before and is superseded by the upstream commit backported here.
147e83
	sysdeps/unix/sysv/linux/netlinkaccess.h
147e83
          Missing backport of e054f494306530720114b321b3d97ca2f397cbbb
147e83
          ("Add #include <stdint.h> for uint[32|64]_t usage (except
147e83
	  installed headers)").
147e83
147e83
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
147e83
index 95cff0ef651e74a9..bb69b985e6df7fb1 100644
147e83
--- a/sysdeps/unix/sysv/linux/Makefile
147e83
+++ b/sysdeps/unix/sysv/linux/Makefile
147e83
@@ -114,6 +114,7 @@ sysdep_headers += netinet/if_fddi.h netinet/if_tr.h \
147e83
 		  netipx/ipx.h netash/ash.h netax25/ax25.h netatalk/at.h \
147e83
 		  netrom/netrom.h netpacket/packet.h netrose/rose.h \
147e83
 		  neteconet/ec.h netiucv/iucv.h
147e83
+sysdep_routines += netlink_assert_response
147e83
 endif
147e83
 
147e83
 # Don't compile the ctype glue code, since there is no old non-GNU C library.
147e83
diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions
147e83
index 16bb28159099c5fa..202ffccc2908ddcc 100644
147e83
--- a/sysdeps/unix/sysv/linux/Versions
147e83
+++ b/sysdeps/unix/sysv/linux/Versions
147e83
@@ -169,5 +169,7 @@ libc {
147e83
   GLIBC_PRIVATE {
147e83
     # functions used in other libraries
147e83
     __syscall_rt_sigqueueinfo;
147e83
+    # functions used by nscd
147e83
+    __netlink_assert_response;
147e83
   }
147e83
 }
147e83
diff --git a/sysdeps/unix/sysv/linux/check_native.c b/sysdeps/unix/sysv/linux/check_native.c
147e83
index 4968a07a0f8c7932..319b46762aeaf3b6 100644
147e83
--- a/sysdeps/unix/sysv/linux/check_native.c
147e83
+++ b/sysdeps/unix/sysv/linux/check_native.c
147e83
@@ -35,6 +35,7 @@
147e83
 
147e83
 #include <not-cancel.h>
147e83
 
147e83
+#include "netlinkaccess.h"
147e83
 
147e83
 void
147e83
 __check_native (uint32_t a1_index, int *a1_native,
147e83
@@ -117,6 +118,7 @@ __check_native (uint32_t a1_index, int *a1_native,
147e83
 	};
147e83
 
147e83
       ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
147e83
+      __netlink_assert_response (fd, read_len);
147e83
       if (read_len < 0)
147e83
 	goto out_fail;
147e83
 
147e83
diff --git a/sysdeps/unix/sysv/linux/check_pf.c b/sysdeps/unix/sysv/linux/check_pf.c
147e83
index d33e1b497d8ba9c7..6b28a735a14f1498 100644
147e83
--- a/sysdeps/unix/sysv/linux/check_pf.c
147e83
+++ b/sysdeps/unix/sysv/linux/check_pf.c
147e83
@@ -36,6 +36,7 @@
147e83
 #include <atomic.h>
147e83
 #include <nscd/nscd-client.h>
147e83
 
147e83
+#include "netlinkaccess.h"
147e83
 
147e83
 #ifndef IFA_F_HOMEADDRESS
147e83
 # define IFA_F_HOMEADDRESS 0
147e83
@@ -178,6 +179,7 @@ make_request (int fd, pid_t pid)
147e83
 	};
147e83
 
147e83
       ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
147e83
+      __netlink_assert_response (fd, read_len);
147e83
       if (read_len < 0)
147e83
 	goto out_fail;
147e83
 
147e83
diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c
147e83
index 179653103e057b79..c87e594e30a314fe 100644
147e83
--- a/sysdeps/unix/sysv/linux/ifaddrs.c
147e83
+++ b/sysdeps/unix/sysv/linux/ifaddrs.c
147e83
@@ -169,6 +169,7 @@ __netlink_request (struct netlink_handle *h, int type)
147e83
 	};
147e83
 
147e83
       read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0));
147e83
+      __netlink_assert_response (h->fd, read_len);
147e83
       if (read_len < 0)
147e83
 	goto out_fail;
147e83
 
147e83
diff --git a/sysdeps/unix/sysv/linux/netlink_assert_response.c b/sysdeps/unix/sysv/linux/netlink_assert_response.c
147e83
new file mode 100644
147e83
index 0000000000000000..b570e93db840fec1
147e83
--- /dev/null
147e83
+++ b/sysdeps/unix/sysv/linux/netlink_assert_response.c
147e83
@@ -0,0 +1,106 @@
147e83
+/* Check recvmsg results for netlink sockets.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#include <errno.h>
147e83
+#include <fcntl.h>
147e83
+#include <stdio.h>
147e83
+#include <sys/socket.h>
147e83
+
147e83
+#include "netlinkaccess.h"
147e83
+
147e83
+static int
147e83
+get_address_family (int fd)
147e83
+{
147e83
+  struct sockaddr_storage sa;
147e83
+  socklen_t sa_len = sizeof (sa);
147e83
+  if (__getsockname (fd, (struct sockaddr *) &sa, &sa_len) < 0)
147e83
+    return -1;
147e83
+  /* Check that the socket family number is preserved despite in-band
147e83
+     signaling.  */
147e83
+  _Static_assert (sizeof (sa.ss_family) < sizeof (int), "address family size");
147e83
+  _Static_assert (0 < (__typeof__ (sa.ss_family)) -1,
147e83
+                  "address family unsigned");
147e83
+  return sa.ss_family;
147e83
+}
147e83
+
147e83
+void
147e83
+internal_function
147e83
+__netlink_assert_response (int fd, ssize_t result)
147e83
+{
147e83
+  if (result < 0)
147e83
+    {
147e83
+      /* Check if the error is unexpected.  */
147e83
+      bool terminate = false;
147e83
+      int error_code = errno;
147e83
+      int family = get_address_family (fd);
147e83
+      if (family != AF_NETLINK)
147e83
+        /* If the address family does not match (or getsockname
147e83
+           failed), report the original error.  */
147e83
+        terminate = true;
147e83
+      else if (error_code == EBADF
147e83
+          || error_code == ENOTCONN
147e83
+          || error_code == ENOTSOCK
147e83
+          || error_code == ECONNREFUSED)
147e83
+        /* These errors indicate that the descriptor is not a
147e83
+           connected socket.  */
147e83
+        terminate = true;
147e83
+      else if (error_code == EAGAIN || error_code == EWOULDBLOCK)
147e83
+        {
147e83
+          /* The kernel might return EAGAIN for other reasons than a
147e83
+             non-blocking socket.  But if the socket is not blocking,
147e83
+             it is not ours, so report the error.  */
147e83
+          int mode = __fcntl (fd, F_GETFL, 0);
147e83
+          if (mode < 0 || (mode & O_NONBLOCK) != 0)
147e83
+            terminate = true;
147e83
+        }
147e83
+      if (terminate)
147e83
+        {
147e83
+          char message[200];
147e83
+          if (family < 0)
147e83
+            __snprintf (message, sizeof (message),
147e83
+                        "Unexpected error %d on netlink descriptor %d",
147e83
+                        error_code, fd);
147e83
+          else
147e83
+            __snprintf (message, sizeof (message),
147e83
+                        "Unexpected error %d on netlink descriptor %d"
147e83
+                        " (address family %d)",
147e83
+                        error_code, fd, family);
147e83
+          __libc_fatal (message);
147e83
+        }
147e83
+      else
147e83
+        /* Restore orignal errno value.  */
147e83
+        __set_errno (error_code);
147e83
+    }
147e83
+  else if (result < sizeof (struct nlmsghdr))
147e83
+    {
147e83
+      char message[200];
147e83
+      int family = get_address_family (fd);
147e83
+      if (family < 0)
147e83
+          __snprintf (message, sizeof (message),
147e83
+                      "Unexpected netlink response of size %zd"
147e83
+                      " on descriptor %d",
147e83
+                      result, fd);
147e83
+      else
147e83
+          __snprintf (message, sizeof (message),
147e83
+                      "Unexpected netlink response of size %zd"
147e83
+                      " on descriptor %d (address family %d)",
147e83
+                      result, fd, family);
147e83
+      __libc_fatal (message);
147e83
+    }
147e83
+}
147e83
+libc_hidden_def (__netlink_assert_response)
147e83
diff --git a/sysdeps/unix/sysv/linux/netlinkaccess.h b/sysdeps/unix/sysv/linux/netlinkaccess.h
147e83
index 6cd8a882640d2486..33dc4e12cd464681 100644
147e83
--- a/sysdeps/unix/sysv/linux/netlinkaccess.h
147e83
+++ b/sysdeps/unix/sysv/linux/netlinkaccess.h
147e83
@@ -18,6 +18,7 @@
147e83
 #ifndef _NETLINKACCESS_H
147e83
 #define _NETLINKACCESS_H 1
147e83
 
147e83
+#include <sys/types.h>
147e83
 #include <asm/types.h>
147e83
 #include <linux/netlink.h>
147e83
 #include <linux/rtnetlink.h>
147e83
@@ -49,5 +50,10 @@ extern void __netlink_close (struct netlink_handle *h);
147e83
 extern void __netlink_free_handle (struct netlink_handle *h);
147e83
 extern int __netlink_request (struct netlink_handle *h, int type);
147e83
 
147e83
+/* Terminate the process if RESULT is an invalid recvmsg result for
147e83
+   the netlink socket FD.  */
147e83
+void __netlink_assert_response (int fd, ssize_t result)
147e83
+  internal_function;
147e83
+libc_hidden_proto (__netlink_assert_response)
147e83
 
147e83
 #endif /* netlinkaccess.h */