This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 68788568a5c net/icmpv6: validate msg_iovlen and defer iov dereference
68788568a5c is described below
commit 68788568a5c2ca12881952d8a4c6571d5c679edf
Author: Swatantra Yadav <[email protected]>
AuthorDate: Wed Nov 11 18:37:37 2026 +0530
net/icmpv6: validate msg_iovlen and defer iov dereference
POSIX IEEE Std 1003.1 requires sendmsg() and recvmsg() to validate
msg_iovlen and fail with -EMSGSIZE when msg_iovlen is 0 or greater
than IOV_MAX. Furthermore, dereferencing msg->msg_iov in local variable
initializations prior to validating msg_iovlen and msg_iov != NULL risks
NULL pointer dereference or out-of-bounds access.
This patch addresses these issues in net/icmpv6:
1. Defer dereferencing msg->msg_iov until after validating msg_iovlen
and confirming msg->msg_iov != NULL.
2. Return -EMSGSIZE when msg_iovlen == 0 or msg_iovlen > IOV_MAX.
3. Reject invalid buffers where len > 0 && buf == NULL with -EINVAL.
4. Set MSG_TRUNC in msg->msg_flags when received data exceeds the
user buffer in both icmpv6_readahead() and recvfrom_eventhandler().
5. Widen recv_buflen to size_t and recv_result to ssize_t in
struct icmpv6_recvfrom_s to prevent 16-bit integer wraparound on
>=64KiB buffers.
6. Remove artificial len < ICMPv6_HDRLEN rejection to allow short and
zero-length buffers to reach truncation paths per POSIX.
7. Use iob_copyout() in recvfrom_eventhandler() to safely copy across
chained I/O buffers (dev->d_iob) without out-of-bounds heap read.
Assisted-by: gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
---
net/icmpv6/icmpv6_recvmsg.c | 52 ++++++++++++++++++++++++++++++++++++---------
net/icmpv6/icmpv6_sendmsg.c | 33 ++++++++++++++++++++++------
2 files changed, 69 insertions(+), 16 deletions(-)
diff --git a/net/icmpv6/icmpv6_recvmsg.c b/net/icmpv6/icmpv6_recvmsg.c
index c7651c22bf7..e361fc6ae33 100644
--- a/net/icmpv6/icmpv6_recvmsg.c
+++ b/net/icmpv6/icmpv6_recvmsg.c
@@ -29,6 +29,7 @@
#include <string.h>
#include <assert.h>
#include <errno.h>
+#include <limits.h>
#include <nuttx/debug.h>
#include <nuttx/semaphore.h>
@@ -62,9 +63,9 @@ struct icmpv6_recvfrom_s
struct in6_addr recv_from; /* The peer we received the request
* from */
FAR uint8_t *recv_buf; /* Location to return the response */
- uint16_t recv_buflen; /* Size of the response */
+ size_t recv_buflen; /* Size of the response */
FAR struct msghdr *msg; /* Input message header */
- int16_t recv_result; /* >=0: receive size on success;
+ ssize_t recv_result; /* >=0: receive size on success;
* <0: negated errno on fail */
};
@@ -133,17 +134,24 @@ static uint32_t recvfrom_eventhandler(FAR struct
net_driver_s *dev,
if (recvsize > pstate->recv_buflen)
{
recvsize = pstate->recv_buflen;
+ if (pstate->msg != NULL)
+ {
+ pstate->msg->msg_flags |= MSG_TRUNC;
+ }
}
/* Copy the ICMPv6 message to the user provided buffer
* REVISIT: What if there are IPv6 extension headers present?
*/
- memcpy(pstate->recv_buf, IPBUF(IPv6_HDRLEN), recvsize);
+ if (recvsize > 0)
+ {
+ iob_copyout(pstate->recv_buf, dev->d_iob, recvsize,
+ IPv6_HDRLEN);
+ }
/* Return the size of the returned data */
- DEBUGASSERT(recvsize <= INT16_MAX);
pstate->recv_result = recvsize;
/* Return the IPv6 address of the sender from the IPv6 header */
@@ -243,6 +251,14 @@ static inline ssize_t icmpv6_readahead(FAR struct
icmpv6_conn_s *conn,
/* Copy to user */
+ if ((size_t)iob->io_pktlen > buflen)
+ {
+ if (msg != NULL)
+ {
+ msg->msg_flags |= MSG_TRUNC;
+ }
+ }
+
ret = iob_copyout(buf, iob, buflen, 0);
ninfo("Received %ld bytes (of %u)\n", (long)ret, iob->io_pktlen);
@@ -295,26 +311,39 @@ static inline ssize_t icmpv6_readahead(FAR struct
icmpv6_conn_s *conn,
ssize_t icmpv6_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
int flags)
{
- FAR void *buf = msg->msg_iov->iov_base;
- size_t len = msg->msg_iov->iov_len;
FAR struct sockaddr *from = msg->msg_name;
FAR socklen_t *fromlen = &msg->msg_namelen;
FAR struct sockaddr_in6 *inaddr;
FAR struct icmpv6_conn_s *conn;
FAR struct net_driver_s *dev = NULL;
+ FAR void *buf;
+ size_t len;
struct icmpv6_recvfrom_s state;
ssize_t ret;
- /* Some sanity checks */
+ /* POSIX: fail with EMSGSIZE if msg_iovlen <= 0 or > IOV_MAX */
+
+ if (msg->msg_iovlen == 0 || msg->msg_iovlen > IOV_MAX)
+ {
+ return -EMSGSIZE;
+ }
+
+ /* Validity check, only single iov supported */
if (msg->msg_iovlen != 1)
{
return -ENOTSUP;
}
- DEBUGASSERT(buf != NULL);
+ if (msg->msg_iov == NULL)
+ {
+ return -EINVAL;
+ }
+
+ buf = msg->msg_iov[0].iov_base;
+ len = msg->msg_iov[0].iov_len;
- if (len < ICMPv6_HDRLEN)
+ if (len > 0 && buf == NULL)
{
return -EINVAL;
}
@@ -332,6 +361,9 @@ ssize_t icmpv6_recvmsg(FAR struct socket *psock, FAR struct
msghdr *msg,
}
conn = psock->s_conn;
+
+ msg->msg_flags = 0;
+
conn_dev_lock(&conn->sconn, conn->dev);
if (psock->s_type != SOCK_RAW)
{
@@ -409,7 +441,7 @@ ssize_t icmpv6_recvmsg(FAR struct socket *psock, FAR struct
msghdr *msg,
if (state.recv_result < 0)
{
- nerr("ERROR: Return error=%d\n", state.recv_result);
+ nerr("ERROR: Return error=%d\n", (int)state.recv_result);
ret = state.recv_result;
goto errout;
}
diff --git a/net/icmpv6/icmpv6_sendmsg.c b/net/icmpv6/icmpv6_sendmsg.c
index 0aa5d9b87bb..4aa0c887dcf 100644
--- a/net/icmpv6/icmpv6_sendmsg.c
+++ b/net/icmpv6/icmpv6_sendmsg.c
@@ -29,6 +29,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <stdint.h>
+#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
@@ -265,17 +266,24 @@ end_wait:
ssize_t icmpv6_sendmsg(FAR struct socket *psock,
FAR const struct msghdr *msg, int flags)
{
- FAR const void *buf = msg->msg_iov->iov_base;
- size_t len = msg->msg_iov->iov_len;
- FAR const struct sockaddr *to = msg->msg_name;
- socklen_t tolen = msg->msg_namelen;
+ FAR const struct sockaddr *to;
FAR const struct sockaddr_in6 *inaddr;
FAR struct net_driver_s *dev;
FAR struct icmpv6_conn_s *conn;
FAR struct icmpv6_echo_request_s *icmpv6;
+ FAR const void *buf;
+ socklen_t tolen;
+ size_t len;
struct icmpv6_sendto_s state;
ssize_t ret;
+ /* POSIX: fail with EMSGSIZE if msg_iovlen <= 0 or > IOV_MAX */
+
+ if (msg->msg_iovlen == 0 || msg->msg_iovlen > IOV_MAX)
+ {
+ return -EMSGSIZE;
+ }
+
/* Validity check, only single iov supported */
if (msg->msg_iovlen != 1)
@@ -283,6 +291,21 @@ ssize_t icmpv6_sendmsg(FAR struct socket *psock,
return -ENOTSUP;
}
+ if (msg->msg_iov == NULL)
+ {
+ return -EINVAL;
+ }
+
+ buf = msg->msg_iov[0].iov_base;
+ len = msg->msg_iov[0].iov_len;
+ to = msg->msg_name;
+ tolen = msg->msg_namelen;
+
+ if (len > 0 && buf == NULL)
+ {
+ return -EINVAL;
+ }
+
if (to == NULL)
{
/* icmpv6_send() */
@@ -296,8 +319,6 @@ ssize_t icmpv6_sendmsg(FAR struct socket *psock,
/* Some sanity checks */
- DEBUGASSERT(buf != NULL && to != NULL);
-
if (len < ICMPv6_HDRLEN || tolen < sizeof(struct sockaddr_in6))
{
return -EINVAL;