#8516·tokio

net: UnixStream::peer_cred() fails with ENOPROTOOPT on NetBSD: LOCAL_PEEREID is asked at SOL_SOCKET instead of SOL_LOCAL

Author: wayaheadCreated Sep 26, 2026Updated Sep 29, 2026
LabelsC-bugA-tokioM-net

Version

tokio 1.53.1; the same code is on master today (tokio/src/net/unix/ucred.rs, impl_netbsd).

Platform

NetBSD 11.0 amd64 (NetBSD 11.0 (GENERIC) #0), x86_64-unknown-netbsd.

Description

On NetBSD, UnixStream::peer_cred() fails with ENOPROTOOPT ("Protocol option not available") on every connection, including a connection accepted from a UnixListener, so no NetBSD program can learn its peer's credentials through tokio.

impl_netbsd::get_peer_cred asks for the credential with

rust
getsockopt(raw_fd, SOL_SOCKET, LOCAL_PEEREID, ...)

but NetBSD serves LOCAL_PEEREID at the local protocol level, not at SOL_SOCKET. NetBSD's <sys/un.h>:

c
#define SOL_LOCAL	0		/* options level for getsockopt(2) */
#define	LOCAL_PEEREID	0x0003		/* get peer identification */

This probe, run as root on the NetBSD 11.0 guest, asks all three ways on a connection accepted from a listener, on the connecting end, and on a socketpair(2):

c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static void probe(const char *what, int fd) {
    struct unpcbid id; socklen_t len = sizeof id; memset(&id, 0, sizeof id);
    int r = getsockopt(fd, SOL_SOCKET, LOCAL_PEEREID, &id, &len);
    printf("%-9s LOCAL_PEEREID at SOL_SOCKET: ret=%d err=%s\n", what, r, r ? strerror(errno) : "-");
    len = sizeof id; memset(&id, 0, sizeof id);
    r = getsockopt(fd, SOL_LOCAL, LOCAL_PEEREID, &id, &len);
    printf("%-9s LOCAL_PEEREID at SOL_LOCAL:  ret=%d err=%s euid=%d pid=%d\n", what, r, r ? strerror(errno) : "-", (int)id.unp_euid, (int)id.unp_pid);
    uid_t u = 4242; gid_t g = 4242;
    r = getpeereid(fd, &u, &g);
    printf("%-9s getpeereid:                  ret=%d err=%s uid=%d gid=%d\n", what, r, r ? strerror(errno) : "-", (int)u, (int)g);
}

int main(void) {
    const char *path = "/tmp/peereid.sock";
    unlink(path);
    struct sockaddr_un sa; memset(&sa, 0, sizeof sa);
    sa.sun_family = AF_LOCAL; strncpy(sa.sun_path, path, sizeof sa.sun_path - 1);
    int l = socket(AF_LOCAL, SOCK_STREAM, 0);
    if (bind(l, (struct sockaddr *)&sa, sizeof sa) || listen(l, 1)) { perror("bind/listen"); return 2; }
    int c = socket(AF_LOCAL, SOCK_STREAM, 0);
    if (connect(c, (struct sockaddr *)&sa, sizeof sa)) { perror("connect"); return 2; }
    int a = accept(l, NULL, NULL);
    if (a < 0) { perror("accept"); return 2; }
    probe("accepted", a);
    probe("connector", c);
    int sv[2]; socketpair(AF_LOCAL, SOCK_STREAM, 0, sv);
    probe("pair", sv[0]);
    unlink(path);
    return 0;
}
accepted  LOCAL_PEEREID at SOL_SOCKET: ret=-1 err=Protocol option not available
accepted  LOCAL_PEEREID at SOL_LOCAL:  ret=0 err=- euid=0 pid=565
accepted  getpeereid:                  ret=0 err=- uid=0 gid=0
connector LOCAL_PEEREID at SOL_SOCKET: ret=-1 err=Protocol option not available
connector LOCAL_PEEREID at SOL_LOCAL:  ret=0 err=- euid=0 pid=565
connector getpeereid:                  ret=0 err=- uid=0 gid=0
pair      LOCAL_PEEREID at SOL_SOCKET: ret=-1 err=Protocol option not available
pair      LOCAL_PEEREID at SOL_LOCAL:  ret=-1 err=Invalid argument euid=0 pid=0
pair      getpeereid:                  ret=-1 err=Invalid argument uid=4242 gid=4242

So a connected or accepted socket has the credential at SOL_LOCAL (and through getpeereid(3)), and SOL_SOCKET never works. A socketpair(2) has no credential at all on NetBSD: the kernel records one when a socket connects to a listener, which is why tests/uds_cred.rs's test_socket_pair is ignored on NetBSD. That ignore also means no test exercises impl_netbsd against a real kernel, which is how the wrong level went unnoticed.

Suggested fix

Ask at the local level, as impl_freebsd already does, defining the constant locally because libc does not export SOL_LOCAL for NetBSD:

rust
const SOL_LOCAL: libc::c_int = 0;
let ret = getsockopt(raw_fd, SOL_LOCAL, LOCAL_PEEREID, &mut unpcbid as *mut unpcbid as *mut c_void, &mut unpcbid_size);

That keeps the pid, which getpeereid(3) would lose. A regression test on NetBSD needs a listener-accepted connection rather than UnixStream::pair(), for the reason above.

We worked around it in our code with getpeereid(3) on NetBSD; this report is so the workaround can be removed once a release carries the fix.