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 1833f16643b7d1327c0d664f1ba6e1ecd87605b5 Author: Marco Casaroli <[email protected]> AuthorDate: Mon Nov 2 07:21:46 2026 +0100 net/local: Fix SO_PEERCRED on a socket without a peer. getsockopt(SO_PEERCRED) copied the credentials of conn->lc_peer without a check. Only connect() and accept() set lc_peer: the ends of socketpair() and an unconnected or listening socket have none, so the call read through a NULL pointer in the kernel and the system stopped. Do as Linux does. Mark the ends of socketpair() and give them the credentials of the process that made the pair. A listening socket gives its own credentials. Any other socket gives pid 0, uid -1 and gid -1, and the call succeeds. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/local/local.h | 1 + net/local/local_sockif.c | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/net/local/local.h b/net/local/local.h index 9793ac41191..05c4f7609a3 100644 --- a/net/local/local.h +++ b/net/local/local.h @@ -124,6 +124,7 @@ struct local_conn_s uint8_t lc_proto; /* SOCK_STREAM or SOCK_DGRAM */ uint8_t lc_type; /* See enum local_type_e */ uint8_t lc_state; /* See enum local_state_e */ + bool lc_pair; /* One end of a socketpair() */ struct file lc_infile; /* File for read-only FIFO (peers) */ struct file lc_outfile; /* File descriptor of write-only FIFO (peers) */ char lc_path[UNIX_PATH_MAX]; /* Path assigned by bind() */ diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c index 802cbd3eb1c..6f0ea723270 100644 --- a/net/local/local_sockif.c +++ b/net/local/local_sockif.c @@ -598,12 +598,34 @@ static int local_getsockopt(FAR struct socket *psock, int level, int option, #ifdef CONFIG_NET_LOCAL_SCM case SO_PEERCRED: { + FAR struct ucred *cred = value; + if (*value_len != sizeof(struct ucred)) { return -EINVAL; } - memcpy(value, &conn->lc_peer->lc_cred, sizeof(struct ucred)); + /* As on Linux: a connected stream socket gives its peer, an + * end of a socketpair() and a listening socket give their own + * credentials, any other socket gives pid 0, uid and gid -1. + */ + + if (conn->lc_peer != NULL) + { + memcpy(cred, &conn->lc_peer->lc_cred, sizeof(*cred)); + } + else if (conn->lc_pair || + conn->lc_state == LOCAL_STATE_LISTENING) + { + memcpy(cred, &conn->lc_cred, sizeof(*cred)); + } + else + { + cred->pid = 0; + cred->uid = -1; + cred->gid = -1; + } + return OK; } #endif @@ -1162,6 +1184,7 @@ static int local_socketpair(FAR struct socket *psocks[2]) conns[i]->lc_proto = psocks[i]->s_type; conns[i]->lc_type = LOCAL_TYPE_PATHNAME; conns[i]->lc_state = LOCAL_STATE_BOUND; + conns[i]->lc_pair = true; } conns[0]->lc_instance_id = conns[1]->lc_instance_id
