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 9ce127c9ed1 net/pkt: validate msg_iovlen and defer iov dereference in 
sendmsg/recvmsg
9ce127c9ed1 is described below

commit 9ce127c9ed156eea51ac653b10b539a712f6d24e
Author: Swatantra Yadav <[email protected]>
AuthorDate: Thu Nov 12 03:26:09 2026 +0530

    net/pkt: validate msg_iovlen and defer iov dereference in sendmsg/recvmsg
    
    Defer dereferencing msg->msg_iov in both buffered and unbuffered
    pkt_sendmsg() implementations until after pkt_sendmsg_is_valid()
    validates input arguments, avoiding null pointer dereferences and
    out-of-bounds reads when msg_iovlen == 0 or msg_iov == NULL.
    
    Enforce POSIX IEEE Std 1003.1 conformance across pkt_sendmsg_is_valid()
    and pkt_recvmsg():
    - Return -EMSGSIZE when msg_iovlen == 0 or msg_iovlen > IOV_MAX.
    - Return -ENOTSUP when msg_iovlen > 1 (scatter/gather unsupported).
    - Return -EINVAL when msg_iov is NULL or when an element has iov_len > 0
      with a NULL iov_base.
    - Initialize msg->msg_flags to 0 in pkt_recvmsg().
    - Set MSG_TRUNC in msg->msg_flags when incoming packet data exceeds
      the user buffer capacity, accounting for link-layer header offset
      on SOCK_DGRAM sockets in pkt_recvfrom_newdata() and in pkt_readdata().
    
    Assisted-by: gemini-3.8-flash
    Signed-off-by: Swatantra Yadav <[email protected]>
---
 net/pkt/pkt_conn.c               | 22 ++++++++++++++++
 net/pkt/pkt_recvmsg.c            | 55 +++++++++++++++++++++++++++++++++++++---
 net/pkt/pkt_sendmsg_buffered.c   | 14 ++++++----
 net/pkt/pkt_sendmsg_unbuffered.c | 11 +++++---
 4 files changed, 89 insertions(+), 13 deletions(-)

diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c
index a6b24bd04fa..e91906ea48f 100644
--- a/net/pkt/pkt_conn.c
+++ b/net/pkt/pkt_conn.c
@@ -29,6 +29,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <limits.h>
 #include <nuttx/debug.h>
 
 #include <arch/irq.h>
@@ -249,6 +250,13 @@ int pkt_sendmsg_is_valid(FAR struct socket *psock,
 {
   FAR struct sockaddr_ll *addr = msg->msg_name;
 
+  /* POSIX: fail with EMSGSIZE if msg_iovlen <= 0 or > IOV_MAX */
+
+  if (msg->msg_iovlen == 0 || msg->msg_iovlen > IOV_MAX)
+    {
+      return -EMSGSIZE;
+    }
+
   /* Only single iov supported */
 
   if (msg->msg_iovlen != 1)
@@ -256,6 +264,20 @@ int pkt_sendmsg_is_valid(FAR struct socket *psock,
       return -ENOTSUP;
     }
 
+  /* An iov must be provided */
+
+  if (msg->msg_iov == NULL)
+    {
+      return -EINVAL;
+    }
+
+  /* Elements of the iov must not be NULL if length is non-zero */
+
+  if (msg->msg_iov[0].iov_len > 0 && msg->msg_iov[0].iov_base == NULL)
+    {
+      return -EINVAL;
+    }
+
   /* Verify that the sockfd corresponds to valid, allocated socket */
 
   if (psock == NULL || psock->s_conn == NULL)
diff --git a/net/pkt/pkt_recvmsg.c b/net/pkt/pkt_recvmsg.c
index 1072b36a603..600796a256f 100644
--- a/net/pkt/pkt_recvmsg.c
+++ b/net/pkt/pkt_recvmsg.c
@@ -34,6 +34,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <errno.h>
+#include <limits.h>
 #include <nuttx/debug.h>
 #include <assert.h>
 
@@ -117,27 +118,46 @@ static inline void pkt_add_recvlen(FAR struct 
pkt_recvfrom_s *pstate,
 static void pkt_recvfrom_newdata(FAR struct net_driver_s *dev,
                                  FAR struct pkt_recvfrom_s *pstate)
 {
-  int offset = 0;
+  size_t pktlen = dev->d_len;
   size_t recvlen;
+  int offset = 0;
 
 #ifdef CONFIG_NET_TIMESTAMP
   cmsg_store_timestamp(pstate->pr_msg, &dev->d_iob->io_time,
                        pstate->pr_conn->sconn.s_options);
 #endif
 
-  recvlen = MIN(pstate->pr_msg->msg_iov->iov_len, dev->d_len);
-
   /* Copy the new packet data into the user buffer */
 
   if (pstate->pr_type == SOCK_RAW)
     {
       offset = -NET_LL_HDRLEN(dev);
     }
+  else
+    {
+      /* For SOCK_DGRAM, exclude the link-layer header */
+
+      if (pktlen > NET_LL_HDRLEN(dev))
+        {
+          pktlen -= NET_LL_HDRLEN(dev);
+        }
+      else
+        {
+          pktlen = 0;
+        }
+    }
+
+  if (pstate->pr_msg->msg_iov->iov_len < pktlen)
+    {
+      pstate->pr_msg->msg_flags |= MSG_TRUNC;
+    }
+
+  recvlen = MIN(pstate->pr_msg->msg_iov->iov_len, pktlen);
 
   recvlen = iob_copyout(pstate->pr_msg->msg_iov->iov_base,
                         dev->d_iob, recvlen, offset);
 
-  ninfo("Received %d bytes (of %d)\n", (int)recvlen, (int)dev->d_len);
+  ninfo("Received %d bytes (of %d)\n", (int)recvlen, (int)pktlen);
 
   /* Update the accumulated size of the data read */
 
@@ -432,6 +452,12 @@ static inline int pkt_readdata(FAR struct pkt_recvfrom_s 
*pstate,
             }
         }
 
+      if (iob->io_pktlen > offset &&
+          pstate->pr_msg->msg_iov->iov_len < iob->io_pktlen - offset)
+        {
+          pstate->pr_msg->msg_flags |= MSG_TRUNC;
+        }
+
       recvlen = iob_copyout(pstate->pr_msg->msg_iov->iov_base, iob,
                             pstate->pr_msg->msg_iov->iov_len, offset);
 
@@ -510,17 +536,38 @@ ssize_t pkt_recvmsg(FAR struct socket *psock, FAR struct 
msghdr *msg,
       return -EINVAL;
     }
 
+  /* POSIX: fail with EMSGSIZE if msg_iovlen <= 0 or > IOV_MAX */
+
+  if (msg->msg_iovlen == 0 || msg->msg_iovlen > IOV_MAX)
+    {
+      return -EMSGSIZE;
+    }
+
   if (msg->msg_iovlen != 1)
     {
       return -ENOTSUP;
     }
 
+  if (msg->msg_iov == NULL)
+    {
+      return -EINVAL;
+    }
+
+  if (msg->msg_iov[0].iov_len > 0 && msg->msg_iov[0].iov_base == NULL)
+    {
+      return -EINVAL;
+    }
+
   if (psock->s_type != SOCK_DGRAM && psock->s_type != SOCK_RAW)
     {
       nerr("ERROR: Unsupported socket type: %d\n", psock->s_type);
       return -ENOSYS;
     }
 
+  /* Clear flags that may be returned */
+
+  msg->msg_flags = 0;
+
   /* Get the device driver that will service this transfer */
 
   dev  = pkt_find_device(conn);
diff --git a/net/pkt/pkt_sendmsg_buffered.c b/net/pkt/pkt_sendmsg_buffered.c
index 5392d72e167..e39cbb2d7b1 100644
--- a/net/pkt/pkt_sendmsg_buffered.c
+++ b/net/pkt/pkt_sendmsg_buffered.c
@@ -207,15 +207,15 @@ static uint32_t psock_send_eventhandler(FAR struct 
net_driver_s *dev,
 ssize_t pkt_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 struct sockaddr_ll *addr = msg->msg_name;
+  FAR struct sockaddr_ll *addr;
   FAR struct net_driver_s *dev;
   FAR struct pkt_conn_s *conn;
+  FAR const void *buf;
   FAR struct iob_s *iob;
   bool nonblock;
+  size_t len;
   int offset = 0;
-  int ret = OK;
+  int ret;
 
   /* Validity check */
 
@@ -225,7 +225,11 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
       return ret;
     }
 
-  if (len <= 0)
+  buf  = msg->msg_iov[0].iov_base;
+  len  = msg->msg_iov[0].iov_len;
+  addr = msg->msg_name;
+
+  if (len == 0)
     {
       return 0;
     }
diff --git a/net/pkt/pkt_sendmsg_unbuffered.c b/net/pkt/pkt_sendmsg_unbuffered.c
index 5e9982c9bbb..464e1d128a1 100644
--- a/net/pkt/pkt_sendmsg_unbuffered.c
+++ b/net/pkt/pkt_sendmsg_unbuffered.c
@@ -192,13 +192,13 @@ end_wait:
 ssize_t pkt_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 struct sockaddr_ll *addr = msg->msg_name;
+  FAR struct sockaddr_ll *addr;
   FAR struct net_driver_s *dev;
   FAR struct pkt_conn_s *conn;
+  FAR const void *buf;
   struct send_s state;
-  int ret = OK;
+  size_t len;
+  int ret;
 
   /* Validity check */
 
@@ -208,6 +208,9 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
       return ret;
     }
 
+  buf  = msg->msg_iov[0].iov_base;
+  len  = msg->msg_iov[0].iov_len;
+  addr = msg->msg_name;
   conn = psock->s_conn;
 
   if (psock->s_type == SOCK_DGRAM)

Reply via email to