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