76b6d9
1. Added "$(objpfx)tst-cmsghdr: $(libdl)" to socket/Makefile since we still
76b6d9
   need $(libdl) in RHEL8.
76b6d9
76b6d9
2. Included stddef.h in socket/tst-cmsghdr-skeleton.c because it uses NULL.
76b6d9
76b6d9
commit 9c443ac4559a47ed99859bd80d14dc4b6dd220a1
76b6d9
Author: Arjun Shankar <arjun@redhat.com>
76b6d9
Date:   Tue Aug 2 11:10:25 2022 +0200
76b6d9
76b6d9
    socket: Check lengths before advancing pointer in CMSG_NXTHDR
76b6d9
    
76b6d9
    The inline and library functions that the CMSG_NXTHDR macro may expand
76b6d9
    to increment the pointer to the header before checking the stride of
76b6d9
    the increment against available space.  Since C only allows incrementing
76b6d9
    pointers to one past the end of an array, the increment must be done
76b6d9
    after a length check.  This commit fixes that and includes a regression
76b6d9
    test for CMSG_FIRSTHDR and CMSG_NXTHDR.
76b6d9
    
76b6d9
    The Linux, Hurd, and generic headers are all changed.
76b6d9
    
76b6d9
    Tested on Linux on armv7hl, i686, x86_64, aarch64, ppc64le, and s390x.
76b6d9
    
76b6d9
    [BZ #28846]
76b6d9
    
76b6d9
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
76b6d9
76b6d9
Conflicts:
76b6d9
	socket/Makefile
76b6d9
	  (usual test backport differences)
76b6d9
76b6d9
diff --git a/bits/socket.h b/bits/socket.h
76b6d9
index 725798882e4b803b..0474613a9c003eeb 100644
76b6d9
--- a/bits/socket.h
76b6d9
+++ b/bits/socket.h
76b6d9
@@ -245,6 +245,12 @@ struct cmsghdr
76b6d9
 			 + CMSG_ALIGN (sizeof (struct cmsghdr)))
76b6d9
 #define CMSG_LEN(len)   (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
76b6d9
 
76b6d9
+/* Given a length, return the additional padding necessary such that
76b6d9
+   len + __CMSG_PADDING(len) == CMSG_ALIGN (len).  */
76b6d9
+#define __CMSG_PADDING(len) ((sizeof (size_t) \
76b6d9
+                              - ((len) & (sizeof (size_t) - 1))) \
76b6d9
+                             & (sizeof (size_t) - 1))
76b6d9
+
76b6d9
 extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
76b6d9
 				      struct cmsghdr *__cmsg) __THROW;
76b6d9
 #ifdef __USE_EXTERN_INLINES
76b6d9
@@ -254,18 +260,38 @@ extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
76b6d9
 _EXTERN_INLINE struct cmsghdr *
76b6d9
 __NTH (__cmsg_nxthdr (struct msghdr *__mhdr, struct cmsghdr *__cmsg))
76b6d9
 {
76b6d9
+  /* We may safely assume that __cmsg lies between __mhdr->msg_control and
76b6d9
+     __mhdr->msg_controllen because the user is required to obtain the first
76b6d9
+     cmsg via CMSG_FIRSTHDR, set its length, then obtain subsequent cmsgs
76b6d9
+     via CMSG_NXTHDR, setting lengths along the way.  However, we don't yet
76b6d9
+     trust the value of __cmsg->cmsg_len and therefore do not use it in any
76b6d9
+     pointer arithmetic until we check its value.  */
76b6d9
+
76b6d9
+  unsigned char * __msg_control_ptr = (unsigned char *) __mhdr->msg_control;
76b6d9
+  unsigned char * __cmsg_ptr = (unsigned char *) __cmsg;
76b6d9
+
76b6d9
+  size_t __size_needed = sizeof (struct cmsghdr)
76b6d9
+                         + __CMSG_PADDING (__cmsg->cmsg_len);
76b6d9
+
76b6d9
+  /* The current header is malformed, too small to be a full header.  */
76b6d9
   if ((size_t) __cmsg->cmsg_len < sizeof (struct cmsghdr))
76b6d9
-    /* The kernel header does this so there may be a reason.  */
76b6d9
     return (struct cmsghdr *) 0;
76b6d9
 
76b6d9
+  /* There isn't enough space between __cmsg and the end of the buffer to
76b6d9
+  hold the current cmsg *and* the next one.  */
76b6d9
+  if (((size_t)
76b6d9
+         (__msg_control_ptr + __mhdr->msg_controllen - __cmsg_ptr)
76b6d9
+       < __size_needed)
76b6d9
+      || ((size_t)
76b6d9
+            (__msg_control_ptr + __mhdr->msg_controllen - __cmsg_ptr
76b6d9
+             - __size_needed)
76b6d9
+          < __cmsg->cmsg_len))
76b6d9
+
76b6d9
+    return (struct cmsghdr *) 0;
76b6d9
+
76b6d9
+  /* Now, we trust cmsg_len and can use it to find the next header.  */
76b6d9
   __cmsg = (struct cmsghdr *) ((unsigned char *) __cmsg
76b6d9
 			       + CMSG_ALIGN (__cmsg->cmsg_len));
76b6d9
-  if ((unsigned char *) (__cmsg + 1) > ((unsigned char *) __mhdr->msg_control
76b6d9
-					+ __mhdr->msg_controllen)
76b6d9
-      || ((unsigned char *) __cmsg + CMSG_ALIGN (__cmsg->cmsg_len)
76b6d9
-	  > ((unsigned char *) __mhdr->msg_control + __mhdr->msg_controllen)))
76b6d9
-    /* No more entries.  */
76b6d9
-    return (struct cmsghdr *) 0;
76b6d9
   return __cmsg;
76b6d9
 }
76b6d9
 #endif	/* Use `extern inline'.  */
76b6d9
diff --git a/socket/Makefile b/socket/Makefile
76b6d9
index 8975a65c2aabbfbc..a445383f8739351e 100644
76b6d9
--- a/socket/Makefile
76b6d9
+++ b/socket/Makefile
76b6d9
@@ -31,7 +31,12 @@ routines := accept bind connect getpeername getsockname getsockopt	\
76b6d9
 	    setsockopt shutdown socket socketpair isfdtype opensock	\
76b6d9
 	    sockatmark accept4 recvmmsg sendmmsg sockaddr_un_set
76b6d9
 
76b6d9
-tests := tst-accept4
76b6d9
+tests := \
76b6d9
+  tst-accept4 \
76b6d9
+  tst-cmsghdr \
76b6d9
+  # tests
76b6d9
+
76b6d9
+$(objpfx)tst-cmsghdr: $(libdl)
76b6d9
 
76b6d9
 tests-internal := \
76b6d9
   tst-sockaddr_un_set \
76b6d9
diff --git a/socket/tst-cmsghdr-skeleton.c b/socket/tst-cmsghdr-skeleton.c
76b6d9
new file mode 100644
76b6d9
index 0000000000000000..7accfa6e54708e2a
76b6d9
--- /dev/null
76b6d9
+++ b/socket/tst-cmsghdr-skeleton.c
76b6d9
@@ -0,0 +1,93 @@
76b6d9
+/* Test ancillary data header creation.
76b6d9
+   Copyright (C) 2022 Free Software Foundation, Inc.
76b6d9
+   This file is part of the GNU C Library.
76b6d9
+
76b6d9
+   The GNU C Library is free software; you can redistribute it and/or
76b6d9
+   modify it under the terms of the GNU Lesser General Public
76b6d9
+   License as published by the Free Software Foundation; either
76b6d9
+   version 2.1 of the License, or (at your option) any later version.
76b6d9
+
76b6d9
+   The GNU C Library is distributed in the hope that it will be useful,
76b6d9
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
76b6d9
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
76b6d9
+   Lesser General Public License for more details.
76b6d9
+
76b6d9
+   You should have received a copy of the GNU Lesser General Public
76b6d9
+   License along with the GNU C Library; if not, see
76b6d9
+   <https://www.gnu.org/licenses/>.  */
76b6d9
+
76b6d9
+/* We use the preprocessor to generate the function/macro tests instead of
76b6d9
+   using indirection because having all the macro expansions alongside
76b6d9
+   each other lets the compiler warn us about suspicious pointer
76b6d9
+   arithmetic across subsequent CMSG_{FIRST,NXT}HDR expansions.  */
76b6d9
+
76b6d9
+#include <stdint.h>
76b6d9
+#include <stddef.h>
76b6d9
+
76b6d9
+#define RUN_TEST_CONCAT(suffix) run_test_##suffix
76b6d9
+#define RUN_TEST_FUNCNAME(suffix) RUN_TEST_CONCAT (suffix)
76b6d9
+
76b6d9
+static void
76b6d9
+RUN_TEST_FUNCNAME (CMSG_NXTHDR_IMPL) (void)
76b6d9
+{
76b6d9
+  struct msghdr m = {0};
76b6d9
+  struct cmsghdr *cmsg;
76b6d9
+  char cmsgbuf[3 * CMSG_SPACE (sizeof (PAYLOAD))] = {0};
76b6d9
+
76b6d9
+  m.msg_control = cmsgbuf;
76b6d9
+  m.msg_controllen = sizeof (cmsgbuf);
76b6d9
+
76b6d9
+  /* First header should point to the start of the buffer.  */
76b6d9
+  cmsg = CMSG_FIRSTHDR (&m);
76b6d9
+  TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
76b6d9
+
76b6d9
+  /* If the first header length consumes the entire buffer, there is no
76b6d9
+     space remaining for additional headers.  */
76b6d9
+  cmsg->cmsg_len = sizeof (cmsgbuf);
76b6d9
+  cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
76b6d9
+  TEST_VERIFY_EXIT (cmsg == NULL);
76b6d9
+
76b6d9
+  /* The first header length is so big, using it would cause an overflow.  */
76b6d9
+  cmsg = CMSG_FIRSTHDR (&m);
76b6d9
+  TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
76b6d9
+  cmsg->cmsg_len = SIZE_MAX;
76b6d9
+  cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
76b6d9
+  TEST_VERIFY_EXIT (cmsg == NULL);
76b6d9
+
76b6d9
+  /* The first header leaves just enough space to hold another header.  */
76b6d9
+  cmsg = CMSG_FIRSTHDR (&m);
76b6d9
+  TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
76b6d9
+  cmsg->cmsg_len = sizeof (cmsgbuf) - sizeof (struct cmsghdr);
76b6d9
+  cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
76b6d9
+  TEST_VERIFY_EXIT (cmsg != NULL);
76b6d9
+
76b6d9
+  /* The first header leaves space but not enough for another header.  */
76b6d9
+  cmsg = CMSG_FIRSTHDR (&m);
76b6d9
+  TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
76b6d9
+  cmsg->cmsg_len ++;
76b6d9
+  cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
76b6d9
+  TEST_VERIFY_EXIT (cmsg == NULL);
76b6d9
+
76b6d9
+  /* The second header leaves just enough space to hold another header.  */
76b6d9
+  cmsg = CMSG_FIRSTHDR (&m);
76b6d9
+  TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
76b6d9
+  cmsg->cmsg_len = CMSG_LEN (sizeof (PAYLOAD));
76b6d9
+  cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
76b6d9
+  TEST_VERIFY_EXIT (cmsg != NULL);
76b6d9
+  cmsg->cmsg_len = sizeof (cmsgbuf)
76b6d9
+                   - CMSG_SPACE (sizeof (PAYLOAD)) /* First header.  */
76b6d9
+                   - sizeof (struct cmsghdr);
76b6d9
+  cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
76b6d9
+  TEST_VERIFY_EXIT (cmsg != NULL);
76b6d9
+
76b6d9
+  /* The second header leaves space but not enough for another header.  */
76b6d9
+  cmsg = CMSG_FIRSTHDR (&m);
76b6d9
+  TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
76b6d9
+  cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
76b6d9
+  TEST_VERIFY_EXIT (cmsg != NULL);
76b6d9
+  cmsg->cmsg_len ++;
76b6d9
+  cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
76b6d9
+  TEST_VERIFY_EXIT (cmsg == NULL);
76b6d9
+
76b6d9
+  return;
76b6d9
+}
76b6d9
diff --git a/socket/tst-cmsghdr.c b/socket/tst-cmsghdr.c
76b6d9
new file mode 100644
76b6d9
index 0000000000000000..68c96d3c9dd2bce8
76b6d9
--- /dev/null
76b6d9
+++ b/socket/tst-cmsghdr.c
76b6d9
@@ -0,0 +1,56 @@
76b6d9
+/* Test ancillary data header creation.
76b6d9
+   Copyright (C) 2022 Free Software Foundation, Inc.
76b6d9
+   This file is part of the GNU C Library.
76b6d9
+
76b6d9
+   The GNU C Library is free software; you can redistribute it and/or
76b6d9
+   modify it under the terms of the GNU Lesser General Public
76b6d9
+   License as published by the Free Software Foundation; either
76b6d9
+   version 2.1 of the License, or (at your option) any later version.
76b6d9
+
76b6d9
+   The GNU C Library is distributed in the hope that it will be useful,
76b6d9
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
76b6d9
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
76b6d9
+   Lesser General Public License for more details.
76b6d9
+
76b6d9
+   You should have received a copy of the GNU Lesser General Public
76b6d9
+   License along with the GNU C Library; if not, see
76b6d9
+   <https://www.gnu.org/licenses/>.  */
76b6d9
+
76b6d9
+#include <sys/socket.h>
76b6d9
+#include <gnu/lib-names.h>
76b6d9
+#include <support/xdlfcn.h>
76b6d9
+#include <support/check.h>
76b6d9
+
76b6d9
+#define PAYLOAD "Hello, World!"
76b6d9
+
76b6d9
+/* CMSG_NXTHDR is a macro that calls an inline function defined in
76b6d9
+   bits/socket.h.  In case the function cannot be inlined, libc.so carries
76b6d9
+   a copy.  Both versions need to be tested.  */
76b6d9
+
76b6d9
+#define CMSG_NXTHDR_IMPL CMSG_NXTHDR
76b6d9
+#include "tst-cmsghdr-skeleton.c"
76b6d9
+#undef CMSG_NXTHDR_IMPL
76b6d9
+
76b6d9
+static struct cmsghdr * (* cmsg_nxthdr) (struct msghdr *, struct cmsghdr *);
76b6d9
+
76b6d9
+#define CMSG_NXTHDR_IMPL cmsg_nxthdr
76b6d9
+#include "tst-cmsghdr-skeleton.c"
76b6d9
+#undef CMSG_NXTHDR_IMPL
76b6d9
+
76b6d9
+static int
76b6d9
+do_test (void)
76b6d9
+{
76b6d9
+  static void *handle;
76b6d9
+
76b6d9
+  run_test_CMSG_NXTHDR ();
76b6d9
+
76b6d9
+  handle = xdlopen (LIBC_SO, RTLD_LAZY);
76b6d9
+  cmsg_nxthdr = (struct cmsghdr * (*) (struct msghdr *, struct cmsghdr *))
76b6d9
+                  xdlsym (handle, "__cmsg_nxthdr");
76b6d9
+
76b6d9
+  run_test_cmsg_nxthdr ();
76b6d9
+
76b6d9
+  return 0;
76b6d9
+}
76b6d9
+
76b6d9
+#include <support/test-driver.c>
76b6d9
diff --git a/sysdeps/mach/hurd/bits/socket.h b/sysdeps/mach/hurd/bits/socket.h
76b6d9
index 18959139dc7d325b..cc66684061e3e179 100644
76b6d9
--- a/sysdeps/mach/hurd/bits/socket.h
76b6d9
+++ b/sysdeps/mach/hurd/bits/socket.h
76b6d9
@@ -249,6 +249,12 @@ struct cmsghdr
76b6d9
 			 + CMSG_ALIGN (sizeof (struct cmsghdr)))
76b6d9
 #define CMSG_LEN(len)   (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
76b6d9
 
76b6d9
+/* Given a length, return the additional padding necessary such that
76b6d9
+   len + __CMSG_PADDING(len) == CMSG_ALIGN (len).  */
76b6d9
+#define __CMSG_PADDING(len) ((sizeof (size_t) \
76b6d9
+                              - ((len) & (sizeof (size_t) - 1))) \
76b6d9
+                             & (sizeof (size_t) - 1))
76b6d9
+
76b6d9
 extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
76b6d9
 				      struct cmsghdr *__cmsg) __THROW;
76b6d9
 #ifdef __USE_EXTERN_INLINES
76b6d9
@@ -258,18 +264,38 @@ extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
76b6d9
 _EXTERN_INLINE struct cmsghdr *
76b6d9
 __NTH (__cmsg_nxthdr (struct msghdr *__mhdr, struct cmsghdr *__cmsg))
76b6d9
 {
76b6d9
+  /* We may safely assume that __cmsg lies between __mhdr->msg_control and
76b6d9
+     __mhdr->msg_controllen because the user is required to obtain the first
76b6d9
+     cmsg via CMSG_FIRSTHDR, set its length, then obtain subsequent cmsgs
76b6d9
+     via CMSG_NXTHDR, setting lengths along the way.  However, we don't yet
76b6d9
+     trust the value of __cmsg->cmsg_len and therefore do not use it in any
76b6d9
+     pointer arithmetic until we check its value.  */
76b6d9
+
76b6d9
+  unsigned char * __msg_control_ptr = (unsigned char *) __mhdr->msg_control;
76b6d9
+  unsigned char * __cmsg_ptr = (unsigned char *) __cmsg;
76b6d9
+
76b6d9
+  size_t __size_needed = sizeof (struct cmsghdr)
76b6d9
+                         + __CMSG_PADDING (__cmsg->cmsg_len);
76b6d9
+
76b6d9
+  /* The current header is malformed, too small to be a full header.  */
76b6d9
   if ((size_t) __cmsg->cmsg_len < sizeof (struct cmsghdr))
76b6d9
-    /* The kernel header does this so there may be a reason.  */
76b6d9
     return (struct cmsghdr *) 0;
76b6d9
 
76b6d9
+  /* There isn't enough space between __cmsg and the end of the buffer to
76b6d9
+  hold the current cmsg *and* the next one.  */
76b6d9
+  if (((size_t)
76b6d9
+         (__msg_control_ptr + __mhdr->msg_controllen - __cmsg_ptr)
76b6d9
+       < __size_needed)
76b6d9
+      || ((size_t)
76b6d9
+            (__msg_control_ptr + __mhdr->msg_controllen - __cmsg_ptr
76b6d9
+             - __size_needed)
76b6d9
+          < __cmsg->cmsg_len))
76b6d9
+
76b6d9
+    return (struct cmsghdr *) 0;
76b6d9
+
76b6d9
+  /* Now, we trust cmsg_len and can use it to find the next header.  */
76b6d9
   __cmsg = (struct cmsghdr *) ((unsigned char *) __cmsg
76b6d9
 			       + CMSG_ALIGN (__cmsg->cmsg_len));
76b6d9
-  if ((unsigned char *) (__cmsg + 1) > ((unsigned char *) __mhdr->msg_control
76b6d9
-					+ __mhdr->msg_controllen)
76b6d9
-      || ((unsigned char *) __cmsg + CMSG_ALIGN (__cmsg->cmsg_len)
76b6d9
-	  > ((unsigned char *) __mhdr->msg_control + __mhdr->msg_controllen)))
76b6d9
-    /* No more entries.  */
76b6d9
-    return (struct cmsghdr *) 0;
76b6d9
   return __cmsg;
76b6d9
 }
76b6d9
 #endif	/* Use `extern inline'.  */
76b6d9
diff --git a/sysdeps/unix/sysv/linux/bits/socket.h b/sysdeps/unix/sysv/linux/bits/socket.h
76b6d9
index c3fbb2110296273c..6b895b89831d2cb5 100644
76b6d9
--- a/sysdeps/unix/sysv/linux/bits/socket.h
76b6d9
+++ b/sysdeps/unix/sysv/linux/bits/socket.h
76b6d9
@@ -302,6 +302,12 @@ struct cmsghdr
76b6d9
 			 + CMSG_ALIGN (sizeof (struct cmsghdr)))
76b6d9
 #define CMSG_LEN(len)   (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
76b6d9
 
76b6d9
+/* Given a length, return the additional padding necessary such that
76b6d9
+   len + __CMSG_PADDING(len) == CMSG_ALIGN (len).  */
76b6d9
+#define __CMSG_PADDING(len) ((sizeof (size_t) \
76b6d9
+                              - ((len) & (sizeof (size_t) - 1))) \
76b6d9
+                             & (sizeof (size_t) - 1))
76b6d9
+
76b6d9
 extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
76b6d9
 				      struct cmsghdr *__cmsg) __THROW;
76b6d9
 #ifdef __USE_EXTERN_INLINES
76b6d9
@@ -311,18 +317,38 @@ extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
76b6d9
 _EXTERN_INLINE struct cmsghdr *
76b6d9
 __NTH (__cmsg_nxthdr (struct msghdr *__mhdr, struct cmsghdr *__cmsg))
76b6d9
 {
76b6d9
+  /* We may safely assume that __cmsg lies between __mhdr->msg_control and
76b6d9
+     __mhdr->msg_controllen because the user is required to obtain the first
76b6d9
+     cmsg via CMSG_FIRSTHDR, set its length, then obtain subsequent cmsgs
76b6d9
+     via CMSG_NXTHDR, setting lengths along the way.  However, we don't yet
76b6d9
+     trust the value of __cmsg->cmsg_len and therefore do not use it in any
76b6d9
+     pointer arithmetic until we check its value.  */
76b6d9
+
76b6d9
+  unsigned char * __msg_control_ptr = (unsigned char *) __mhdr->msg_control;
76b6d9
+  unsigned char * __cmsg_ptr = (unsigned char *) __cmsg;
76b6d9
+
76b6d9
+  size_t __size_needed = sizeof (struct cmsghdr)
76b6d9
+                         + __CMSG_PADDING (__cmsg->cmsg_len);
76b6d9
+
76b6d9
+  /* The current header is malformed, too small to be a full header.  */
76b6d9
   if ((size_t) __cmsg->cmsg_len < sizeof (struct cmsghdr))
76b6d9
-    /* The kernel header does this so there may be a reason.  */
76b6d9
     return (struct cmsghdr *) 0;
76b6d9
 
76b6d9
+  /* There isn't enough space between __cmsg and the end of the buffer to
76b6d9
+  hold the current cmsg *and* the next one.  */
76b6d9
+  if (((size_t)
76b6d9
+         (__msg_control_ptr + __mhdr->msg_controllen - __cmsg_ptr)
76b6d9
+       < __size_needed)
76b6d9
+      || ((size_t)
76b6d9
+            (__msg_control_ptr + __mhdr->msg_controllen - __cmsg_ptr
76b6d9
+             - __size_needed)
76b6d9
+          < __cmsg->cmsg_len))
76b6d9
+
76b6d9
+    return (struct cmsghdr *) 0;
76b6d9
+
76b6d9
+  /* Now, we trust cmsg_len and can use it to find the next header.  */
76b6d9
   __cmsg = (struct cmsghdr *) ((unsigned char *) __cmsg
76b6d9
 			       + CMSG_ALIGN (__cmsg->cmsg_len));
76b6d9
-  if ((unsigned char *) (__cmsg + 1) > ((unsigned char *) __mhdr->msg_control
76b6d9
-					+ __mhdr->msg_controllen)
76b6d9
-      || ((unsigned char *) __cmsg + CMSG_ALIGN (__cmsg->cmsg_len)
76b6d9
-	  > ((unsigned char *) __mhdr->msg_control + __mhdr->msg_controllen)))
76b6d9
-    /* No more entries.  */
76b6d9
-    return (struct cmsghdr *) 0;
76b6d9
   return __cmsg;
76b6d9
 }
76b6d9
 #endif	/* Use `extern inline'.  */
76b6d9
diff --git a/sysdeps/unix/sysv/linux/cmsg_nxthdr.c b/sysdeps/unix/sysv/linux/cmsg_nxthdr.c
76b6d9
index bab0be6884d9da1c..16594622211c1c8b 100644
76b6d9
--- a/sysdeps/unix/sysv/linux/cmsg_nxthdr.c
76b6d9
+++ b/sysdeps/unix/sysv/linux/cmsg_nxthdr.c
76b6d9
@@ -23,18 +23,38 @@
76b6d9
 struct cmsghdr *
76b6d9
 __cmsg_nxthdr (struct msghdr *mhdr, struct cmsghdr *cmsg)
76b6d9
 {
76b6d9
+  /* We may safely assume that cmsg lies between mhdr->msg_control and
76b6d9
+     mhdr->msg_controllen because the user is required to obtain the first
76b6d9
+     cmsg via CMSG_FIRSTHDR, set its length, then obtain subsequent cmsgs
76b6d9
+     via CMSG_NXTHDR, setting lengths along the way.  However, we don't yet
76b6d9
+     trust the value of cmsg->cmsg_len and therefore do not use it in any
76b6d9
+     pointer arithmetic until we check its value.  */
76b6d9
+
76b6d9
+  unsigned char * msg_control_ptr = (unsigned char *) mhdr->msg_control;
76b6d9
+  unsigned char * cmsg_ptr = (unsigned char *) cmsg;
76b6d9
+
76b6d9
+  size_t size_needed = sizeof (struct cmsghdr)
76b6d9
+                       + __CMSG_PADDING (cmsg->cmsg_len);
76b6d9
+
76b6d9
+  /* The current header is malformed, too small to be a full header.  */
76b6d9
   if ((size_t) cmsg->cmsg_len < sizeof (struct cmsghdr))
76b6d9
-    /* The kernel header does this so there may be a reason.  */
76b6d9
-    return NULL;
76b6d9
+    return (struct cmsghdr *) 0;
76b6d9
+
76b6d9
+  /* There isn't enough space between cmsg and the end of the buffer to
76b6d9
+  hold the current cmsg *and* the next one.  */
76b6d9
+  if (((size_t)
76b6d9
+         (msg_control_ptr + mhdr->msg_controllen - cmsg_ptr)
76b6d9
+       < size_needed)
76b6d9
+      || ((size_t)
76b6d9
+            (msg_control_ptr + mhdr->msg_controllen - cmsg_ptr
76b6d9
+             - size_needed)
76b6d9
+          < cmsg->cmsg_len))
76b6d9
+
76b6d9
+    return (struct cmsghdr *) 0;
76b6d9
 
76b6d9
+  /* Now, we trust cmsg_len and can use it to find the next header.  */
76b6d9
   cmsg = (struct cmsghdr *) ((unsigned char *) cmsg
76b6d9
 			     + CMSG_ALIGN (cmsg->cmsg_len));
76b6d9
-  if ((unsigned char *) (cmsg + 1) > ((unsigned char *) mhdr->msg_control
76b6d9
-				      + mhdr->msg_controllen)
76b6d9
-      || ((unsigned char *) cmsg + CMSG_ALIGN (cmsg->cmsg_len)
76b6d9
-	  > ((unsigned char *) mhdr->msg_control + mhdr->msg_controllen)))
76b6d9
-    /* No more entries.  */
76b6d9
-    return NULL;
76b6d9
   return cmsg;
76b6d9
 }
76b6d9
 libc_hidden_def (__cmsg_nxthdr)