This is an automated email from the ASF dual-hosted git repository.
acassis 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 23e3cfa2e43 net/netlink: validate msg_iovlen and defer iov dereference
in sendmsg/recvmsg
23e3cfa2e43 is described below
commit 23e3cfa2e436b8975abff0dc6271e873fe9184c4
Author: Swatantra Yadav <[email protected]>
AuthorDate: Thu Nov 12 15:01:29 2026 +0530
net/netlink: validate msg_iovlen and defer iov dereference in
sendmsg/recvmsg
Defer dereferencing msg->msg_iov in netlink_sendmsg() and netlink_recvmsg()
until after validating msg_iovlen and checking msg_iov != NULL, avoiding
null pointer dereferences (CWE-476) and out-of-bounds reads (CWE-125)
when msg_iovlen == 0 or msg_iov == NULL.
Enforce POSIX IEEE Std 1003.1 conformance across Netlink sockets:
- 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.
- Validate that msg_iov[0].iov_len is at least sizeof(struct nlmsghdr)
and that nlmsg->nlmsg_len >= sizeof(struct nlmsghdr) and
nlmsg->nlmsg_len <= msg_iov[0].iov_len before parsing, returning
-EINVAL on malformed netlink headers.
- Initialize msg->msg_flags to 0 in netlink_recvmsg().
- Set MSG_TRUNC in msg->msg_flags when incoming response data exceeds
user buffer capacity (len < entry->msg.nlmsg_len).
- Include <limits.h> for IOV_MAX.
Assisted-by: gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
---
net/netlink/netlink_sockif.c | 83 +++++++++++++++++++++++++++++++++++++++-----
1 file changed, 75 insertions(+), 8 deletions(-)
diff --git a/net/netlink/netlink_sockif.c b/net/netlink/netlink_sockif.c
index 53afe7a8802..2894c439049 100644
--- a/net/netlink/netlink_sockif.c
+++ b/net/netlink/netlink_sockif.c
@@ -34,6 +34,7 @@
#include <sched.h>
#include <assert.h>
#include <errno.h>
+#include <limits.h>
#include <nuttx/debug.h>
#include <nuttx/poll.h>
@@ -579,14 +580,21 @@ static int netlink_poll(FAR struct socket *psock, FAR
struct pollfd_s *fds,
static ssize_t netlink_sendmsg(FAR struct socket *psock,
FAR const struct msghdr *msg, int flags)
{
- FAR const void *buf = msg->msg_iov->iov_base;
FAR const struct sockaddr *to = msg->msg_name;
socklen_t tolen = msg->msg_namelen;
FAR struct netlink_conn_s *conn;
FAR struct nlmsghdr *nlmsg;
struct sockaddr_nl nladdr;
+ FAR const void *buf;
int 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)
@@ -594,6 +602,22 @@ static ssize_t netlink_sendmsg(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;
+ }
+
+ buf = msg->msg_iov[0].iov_base;
+
/* Get the underlying connection structure */
conn = psock->s_conn;
@@ -614,17 +638,28 @@ static ssize_t netlink_sendmsg(FAR struct socket *psock,
DEBUGASSERT(tolen >= sizeof(struct sockaddr_nl));
+ /* Netlink message must contain at least a complete nlmsghdr */
+
+ if (msg->msg_iov[0].iov_len < sizeof(struct nlmsghdr))
+ {
+ return -EINVAL;
+ }
+
/* Get a reference to the netlink message */
nlmsg = (FAR struct nlmsghdr *)buf;
- DEBUGASSERT(nlmsg->nlmsg_len >= sizeof(struct nlmsghdr));
+ if (nlmsg->nlmsg_len < sizeof(struct nlmsghdr) ||
+ nlmsg->nlmsg_len > msg->msg_iov[0].iov_len)
+ {
+ return -EINVAL;
+ }
switch (psock->s_proto)
{
#ifdef CONFIG_NETLINK_ROUTE
case NETLINK_ROUTE:
ret = netlink_route_sendto(conn, nlmsg,
- msg->msg_iov->iov_len, flags,
+ msg->msg_iov[0].iov_len, flags,
(FAR const struct sockaddr_nl *)to,
tolen);
break;
@@ -633,7 +668,7 @@ static ssize_t netlink_sendmsg(FAR struct socket *psock,
#ifdef CONFIG_NETLINK_NETFILTER
case NETLINK_NETFILTER:
ret = netlink_netfilter_sendto(conn, nlmsg,
- msg->msg_iov->iov_len, flags,
+ msg->msg_iov[0].iov_len, flags,
(FAR const struct sockaddr_nl *)to,
tolen);
break;
@@ -670,22 +705,46 @@ static ssize_t netlink_sendmsg(FAR struct socket *psock,
static ssize_t netlink_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 netlink_response_s *entry;
FAR struct socket_conn_s *conn;
+ FAR void *buf;
+ size_t len;
int ret = OK;
DEBUGASSERT(from == NULL ||
(fromlen != NULL && *fromlen >= sizeof(struct sockaddr_nl)));
+ /* 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;
+ }
+
+ /* Clear flags that may be returned */
+
+ msg->msg_flags = 0;
+
+ buf = msg->msg_iov[0].iov_base;
+ len = msg->msg_iov[0].iov_len;
+
/* Find the response to this message. The return value */
entry = netlink_tryget_response(psock->s_conn);
@@ -714,14 +773,22 @@ static ssize_t netlink_recvmsg(FAR struct socket *psock,
}
}
- if (len > entry->msg.nlmsg_len)
+ if (len < entry->msg.nlmsg_len)
+ {
+ msg->msg_flags |= MSG_TRUNC;
+ }
+ else
{
len = entry->msg.nlmsg_len;
}
/* Copy the payload to the user buffer */
- memcpy(buf, &entry->msg, len);
+ if (len > 0)
+ {
+ memcpy(buf, &entry->msg, len);
+ }
+
kmm_free(entry);
if (from != NULL)