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

commit 715cc84aaf956cb6de9e2a5403f2401ccf5fa09c
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 17:04:06 2026 +0100

    net/netlink: Truncate the address of getsockname() and getpeername().
    
    getsockname() and getpeername() of a netlink socket did not handle a
    buffer shorter than struct sockaddr_nl: only a debug check named this
    case, and the functions always stored the whole address.  POSIX says
    that the stored address is truncated in this case, and Linux does so.
    
    Now the functions make the address in a local structure, copy as much
    of it as the buffer holds, and set *addrlen to the length of the whole
    address, as Linux does.  So the caller sees the truncation.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/netlink/netlink_sockif.c | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/net/netlink/netlink_sockif.c b/net/netlink/netlink_sockif.c
index fc5aee8e2b0..1d9dd4e6812 100644
--- a/net/netlink/netlink_sockif.c
+++ b/net/netlink/netlink_sockif.c
@@ -28,6 +28,7 @@
 
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/param.h>
 #include <stdbool.h>
 #include <string.h>
 #include <poll.h>
@@ -297,23 +298,24 @@ static int netlink_getsockname(FAR struct socket *psock,
                                FAR struct sockaddr *addr,
                                FAR socklen_t *addrlen)
 {
-  FAR struct sockaddr_nl *nladdr;
   FAR struct netlink_conn_s *conn;
-
-  DEBUGASSERT(*addrlen >= sizeof(struct sockaddr_nl));
+  struct sockaddr_nl nladdr;
 
   conn = psock->s_conn;
 
   /* Return the address information in the address structure */
 
-  nladdr = (FAR struct sockaddr_nl *)addr;
-  memset(nladdr, 0, sizeof(struct sockaddr_nl));
+  memset(&nladdr, 0, sizeof(nladdr));
+  nladdr.nl_family = AF_NETLINK;
+  nladdr.nl_pid    = conn->pid;
+  nladdr.nl_groups = conn->groups;
 
-  nladdr->nl_family = AF_NETLINK;
-  nladdr->nl_pid    = conn->pid;
-  nladdr->nl_groups = conn->groups;
+  /* Copy as much of the address as the buffer holds.  *addrlen gives the
+   * length of the whole address, so the caller sees a truncation.
+   */
 
-  *addrlen = sizeof(struct sockaddr_nl);
+  memcpy(addr, &nladdr, MIN(*addrlen, sizeof(nladdr)));
+  *addrlen = sizeof(nladdr);
   return OK;
 }
 
@@ -349,23 +351,24 @@ static int netlink_getpeername(FAR struct socket *psock,
                                FAR struct sockaddr *addr,
                                FAR socklen_t *addrlen)
 {
-  FAR struct sockaddr_nl *nladdr;
   FAR struct netlink_conn_s *conn;
-
-  DEBUGASSERT(*addrlen >= sizeof(struct sockaddr_nl));
+  struct sockaddr_nl nladdr;
 
   conn = psock->s_conn;
 
   /* Return the address information in the address structure */
 
-  nladdr = (FAR struct sockaddr_nl *)addr;
-  memset(nladdr, 0, sizeof(struct sockaddr_nl));
+  memset(&nladdr, 0, sizeof(nladdr));
+  nladdr.nl_family = AF_NETLINK;
+  nladdr.nl_pid    = conn->dst_pid;
+  nladdr.nl_groups = conn->dst_groups;
 
-  nladdr->nl_family = AF_NETLINK;
-  nladdr->nl_pid    = conn->dst_pid;
-  nladdr->nl_groups = conn->dst_groups;
+  /* Copy as much of the address as the buffer holds.  *addrlen gives the
+   * length of the whole address, so the caller sees a truncation.
+   */
 
-  *addrlen = sizeof(struct sockaddr_nl);
+  memcpy(addr, &nladdr, MIN(*addrlen, sizeof(nladdr)));
+  *addrlen = sizeof(nladdr);
   return OK;
 }
 

Reply via email to