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 8cde5ca5eaf6a371a4ca5668ecc1c1dddcccc330 Author: Marco Casaroli <[email protected]> AuthorDate: Fri Nov 6 10:56:19 2026 +0100 net/local: Use only addrlen bytes of an address. bind(), connect() and sendto() of a local socket read the name in the address until a NUL. They do not use addrlen. No NUL has to follow a name: an abstract name has none, and a pathname can have none. So the bytes after the address became part of the name. A sendto() to an abstract name failed with ENOENT, and bind() created a wrong name. Also, in a kernel build, sendto() copies only addrlen bytes to its stack, so the bytes after the name are what an earlier system call left there. Copy the address to the stack, with the rest of the copy cleared, before it is used. The behavior is the same in the flat, protected and kernel builds. The stack cost of the three functions is one struct sockaddr_un. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/local/local.h | 18 ++++++++++++++++++ net/local/local_bind.c | 9 ++++++--- net/local/local_conn.c | 18 ++++++++++++++++++ net/local/local_sendmsg.c | 6 +++++- net/local/local_sockif.c | 10 +++++++--- 5 files changed, 54 insertions(+), 7 deletions(-) diff --git a/net/local/local.h b/net/local/local.h index 4f98da73e3c..9793ac41191 100644 --- a/net/local/local.h +++ b/net/local/local.h @@ -332,6 +332,24 @@ void local_subref(FAR struct local_conn_s *conn); FAR struct local_conn_s *local_nextconn(FAR struct local_conn_s *conn); +/**************************************************************************** + * Name: local_copyaddr + * + * Description: + * Copy an address from a program. It has addrlen bytes and no NUL has to + * follow its name, so what follows it in the memory of the program is not + * part of it. The copy always ends with a NUL. + * + * Input Parameters: + * unaddr - The copy + * addr - The address of the program + * addrlen - The length of the address + * + ****************************************************************************/ + +void local_copyaddr(FAR struct sockaddr_un *unaddr, + FAR const struct sockaddr *addr, socklen_t addrlen); + /**************************************************************************** * Name: local_findconn * diff --git a/net/local/local_bind.c b/net/local/local_bind.c index eec73907957..1f7deb2eec3 100644 --- a/net/local/local_bind.c +++ b/net/local/local_bind.c @@ -51,21 +51,24 @@ int psock_local_bind(FAR struct socket *psock, FAR const struct sockaddr *addr, socklen_t addrlen) { FAR struct local_conn_s *conn = psock->s_conn; - FAR const struct sockaddr_un *unaddr = - (FAR const struct sockaddr_un *)addr; + FAR const struct sockaddr_un *unaddr; FAR struct local_conn_s *found; + struct sockaddr_un kaddr; int index; #ifdef CONFIG_NET_LOCAL_DGRAM int ret; #endif - DEBUGASSERT(unaddr->sun_family == AF_LOCAL); + DEBUGASSERT(addr->sa_family == AF_LOCAL); if (addrlen <= sizeof(sa_family_t) + 1) { return -EINVAL; } + local_copyaddr(&kaddr, addr, addrlen); + unaddr = &kaddr; + conn = psock->s_conn; /* A socket may only be bound once. Rebinding a DGRAM socket would open diff --git a/net/local/local_conn.c b/net/local/local_conn.c index 8a889ae7c99..e65f32b8888 100644 --- a/net/local/local_conn.c +++ b/net/local/local_conn.c @@ -26,6 +26,7 @@ #include <nuttx/config.h> +#include <sys/param.h> #include <string.h> #include <assert.h> #include <errno.h> @@ -79,6 +80,23 @@ FAR struct local_conn_s *local_nextconn(FAR struct local_conn_s *conn) return (FAR struct local_conn_s *)conn->lc_conn.s_node.flink; } +/**************************************************************************** + * Name: local_copyaddr + * + * Description: + * Copy an address from a program. It has addrlen bytes and no NUL has to + * follow its name, so what follows it in the memory of the program is not + * part of it. The copy always ends with a NUL. + * + ****************************************************************************/ + +void local_copyaddr(FAR struct sockaddr_un *unaddr, + FAR const struct sockaddr *addr, socklen_t addrlen) +{ + memset(unaddr, 0, sizeof(*unaddr)); + memcpy(unaddr, addr, MIN(addrlen, sizeof(*unaddr) - 1)); +} + /**************************************************************************** * Name: local_findconn * diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c index acbb6971127..555c0d59749 100644 --- a/net/local/local_sendmsg.c +++ b/net/local/local_sendmsg.c @@ -172,7 +172,8 @@ static ssize_t local_sendto(FAR struct socket *psock, #ifdef CONFIG_NET_LOCAL_DGRAM FAR struct local_conn_s *conn = psock->s_conn; FAR struct local_conn_s *server; - FAR const struct sockaddr_un *unaddr = (FAR const struct sockaddr_un *)to; + FAR const struct sockaddr_un *unaddr; + struct sockaddr_un kaddr; FAR const char *path; ssize_t ret; @@ -196,6 +197,9 @@ static ssize_t local_sendto(FAR struct socket *psock, return -EFAULT; } + local_copyaddr(&kaddr, to, tolen); + unaddr = &kaddr; + /* If this is a connected socket, then return EISCONN */ if (psock->s_type != SOCK_DGRAM) diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c index 4e8f1f40d03..802cbd3eb1c 100644 --- a/net/local/local_sockif.c +++ b/net/local/local_sockif.c @@ -838,6 +838,8 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option, static int local_connect(FAR struct socket *psock, FAR const struct sockaddr *addr, socklen_t addrlen) { + struct sockaddr_un kaddr; + /* Verify that a valid address has been provided. A connectionless * socket may also pass AF_UNSPEC to dissolve an existing association. */ @@ -848,6 +850,8 @@ static int local_connect(FAR struct socket *psock, return -EBADF; } + local_copyaddr(&kaddr, addr, addrlen); + /* Perform the connection depending on the protocol type */ switch (psock->s_type) @@ -873,7 +877,8 @@ static int local_connect(FAR struct socket *psock, /* It's not... Connect to the local Unix domain server */ - return psock_local_connect(psock, addr); + return psock_local_connect(psock, + (FAR const struct sockaddr *)&kaddr); } break; #endif /* CONFIG_NET_LOCAL_STREAM */ @@ -882,8 +887,7 @@ static int local_connect(FAR struct socket *psock, case SOCK_DGRAM: { FAR struct local_conn_s *conn = psock->s_conn; - FAR const struct sockaddr_un *unaddr = - (FAR const struct sockaddr_un *)addr; + FAR const struct sockaddr_un *unaddr = &kaddr; if (addr->sa_family == AF_UNSPEC) {
