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 6ffb50015cd net/ieee802154: validate msg_iovlen and defer iov
dereference
6ffb50015cd is described below
commit 6ffb50015cdcd82e597c31d692b1fcb9e5ee4c6b
Author: Swatantra Yadav <[email protected]>
AuthorDate: Wed Nov 11 12:46:52 2026 +0530
net/ieee802154: validate msg_iovlen and defer iov dereference
POSIX IEEE Std 1003.1 explicitly mandates for both sendmsg() and recvmsg():
"[EMSGSIZE] The msg_iovlen member of the msghdr structure is less than
or equal to 0 or is greater than {IOV_MAX}."
In net/ieee802154:
1. In ieee802154_sendmsg() and ieee802154_recvmsg(), msg->msg_iov->iov_base
and msg->msg_iov->iov_len were dereferenced during local variable
declarations before validating msg->msg_iovlen or msg->msg_iov. Passing
msg_iovlen == 0 or msg_iov == NULL resulted in an immediate out-of-bounds
pointer dereference (CWE-476 / CWE-125).
2. Passing msg_iovlen <= 0 or msg_iovlen > IOV_MAX returned -ENOTSUP
instead of POSIX-mandated -EMSGSIZE.
3. In ieee802154_recvfrom_rxqueue(), copylen was not clamped to
pstate->ir_buflen, allowing incoming frames larger than the user
receive buffer to cause a buffer overflow (CWE-120).
4. In ieee802154_recvmsg() and ieee802154_recvfrom_eventhandler(),
completion
checked (ret > 0). When receiving a zero-length frame or zero-byte read,
ret == 0 was not recognized as completion, causing the calling thread to
sleep indefinitely on state.ir_sem.
This commit:
- Adds upfront validation of msg->msg_iovlen == 0 || msg->msg_iovlen >
IOV_MAX,
returning -EMSGSIZE.
- Preserves -ENOTSUP when msg->msg_iovlen > 1 (scatter/gather not
supported).
- Validates msg->msg_iov == NULL and non-null buffer pointers for positive
lengths (len > 0 && buf == NULL), returning -EINVAL.
- Defers accessing msg->msg_iov[0] until all bounds and nullity checks pass.
- Clamps copylen to pstate->ir_buflen in ieee802154_recvfrom_rxqueue() and
sets MSG_TRUNC in msg->msg_flags when truncation occurs.
- Changes completion checks to (ret >= 0) to allow zero-byte frames to
complete cleanly.
- Adds #include <limits.h> in both files.
Assisted-by: gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
---
net/ieee802154/ieee802154_recvmsg.c | 53 +++++++++++++++++++++++++++++++------
net/ieee802154/ieee802154_sendmsg.c | 31 +++++++++++++++++++---
2 files changed, 72 insertions(+), 12 deletions(-)
diff --git a/net/ieee802154/ieee802154_recvmsg.c
b/net/ieee802154/ieee802154_recvmsg.c
index a7b047c0040..a4f19a188d6 100644
--- a/net/ieee802154/ieee802154_recvmsg.c
+++ b/net/ieee802154/ieee802154_recvmsg.c
@@ -31,6 +31,7 @@
#include <stdint.h>
#include <string.h>
#include <errno.h>
+#include <limits.h>
#include <nuttx/debug.h>
#include <assert.h>
@@ -59,6 +60,7 @@ struct ieee802154_recvfrom_s
FAR struct socket *ir_sock; /* Points to the parent socket
structure */
FAR struct devif_callback_s *ir_cb; /* Reference to callback instance */
FAR struct sockaddr *ir_from; /* Location to return the from address
*/
+ FAR struct msghdr *ir_msg; /* Points to message header */
FAR uint8_t *ir_buffer; /* Pointer to receive buffer */
size_t ir_buflen; /* Length of receive buffer */
sem_t ir_sem; /* Semaphore signals recv completion */
@@ -165,7 +167,19 @@ static ssize_t
/* Copy the new packet data into the user buffer */
copylen = iob->io_len - iob->io_offset;
- memcpy(pstate->ir_buffer, &iob->io_data[iob->io_offset], copylen);
+ if (copylen > pstate->ir_buflen)
+ {
+ copylen = pstate->ir_buflen;
+ if (pstate->ir_msg != NULL)
+ {
+ pstate->ir_msg->msg_flags |= MSG_TRUNC;
+ }
+ }
+
+ if (copylen > 0)
+ {
+ memcpy(pstate->ir_buffer, &iob->io_data[iob->io_offset], copylen);
+ }
ninfo("Received %d bytes\n", (int)copylen);
ret = copylen;
@@ -244,7 +258,7 @@ ieee802154_recvfrom_eventhandler(FAR struct net_driver_s
*dev,
/* Attempt to receive the frame */
ret = ieee802154_recvfrom_rxqueue(radio, pstate);
- if (ret > 0)
+ if (ret >= 0)
{
/* Don't allow any further call backs. */
@@ -307,12 +321,12 @@ ieee802154_recvfrom_eventhandler(FAR struct net_driver_s
*dev,
ssize_t ieee802154_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 ieee802154_conn_s *conn = psock->s_conn;
FAR struct radio_driver_s *radio;
+ FAR struct sockaddr *from = msg->msg_name;
+ FAR socklen_t *fromlen = &msg->msg_namelen;
+ FAR void *buf;
+ size_t len;
struct ieee802154_recvfrom_s state;
ssize_t ret;
@@ -331,11 +345,31 @@ ssize_t ieee802154_recvmsg(FAR struct socket *psock, FAR
struct msghdr *msg,
return -EPROTONOSUPPORT;
}
+ /* 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;
+ }
+
+ buf = msg->msg_iov[0].iov_base;
+ len = msg->msg_iov[0].iov_len;
+
+ if (len > 0 && buf == NULL)
+ {
+ return -EINVAL;
+ }
+
/* Perform the packet recvfrom() operation */
/* Initialize the state structure. This is done with the network
@@ -348,6 +382,9 @@ ssize_t ieee802154_recvmsg(FAR struct socket *psock, FAR
struct msghdr *msg,
state.ir_buffer = buf;
state.ir_sock = psock;
state.ir_from = from;
+ state.ir_msg = msg;
+
+ msg->msg_flags = 0;
/* Get the device driver that will service this transfer */
@@ -364,9 +401,9 @@ ssize_t ieee802154_recvmsg(FAR struct socket *psock, FAR
struct msghdr *msg,
*/
ret = ieee802154_recvfrom_rxqueue(radio, &state);
- if (ret > 0)
+ if (ret >= 0)
{
- /* Good newe! We have a frame and we are done. */
+ /* Good news! We have a frame and we are done. */
conn_dev_unlock(&conn->sconn, &radio->r_dev);
return ret;
diff --git a/net/ieee802154/ieee802154_sendmsg.c
b/net/ieee802154/ieee802154_sendmsg.c
index 51127c596b5..14b0654d915 100644
--- a/net/ieee802154/ieee802154_sendmsg.c
+++ b/net/ieee802154/ieee802154_sendmsg.c
@@ -33,6 +33,7 @@
#include <stdbool.h>
#include <string.h>
#include <errno.h>
+#include <limits.h>
#include <assert.h>
#include <nuttx/debug.h>
@@ -630,10 +631,17 @@ static ssize_t ieee802154_send(FAR struct socket *psock,
FAR const void *buf,
ssize_t ieee802154_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 void *buf;
+ socklen_t tolen;
+ size_t len;
+
+ /* 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 */
@@ -642,6 +650,21 @@ ssize_t ieee802154_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;
+ }
+
return to ? ieee802154_sendto(psock, buf, len, flags, to, tolen) :
ieee802154_send(psock, buf, len, flags);
}