Vivian Voss

sockstat Shows Who Listens

technical beauty scope freebsd sockstat capsicum

A host runs twenty jails, and on a Friday afternoon the web server in one of them refuses to start. The log says address already in use, which is true and of no help whatsoever, because the address in question belongs to a machine with twenty tenants and any of them might be the culprit. The administrator types one line:

sockstat -4l -p 443

The answer comes back as a table with seven columns: the user, the command, the process ID, the file descriptor, the protocol and the two addresses. It is spot on: an nginx that somebody started for a test on Tuesday and forgot about. The same line gives the user it runs as and the process ID to send the signal to. A second line, sockstat -j staging -4l, confirms which jail the forgotten process lives in. The whole investigation takes rather less time than reading the log did.

The tool came from Dag-Erling Smørgrav. On 14 April 1999 he imported it as a Perl script called lsock, and renamed it sockstat the next day. In October 2000 he rewrote the script from the ground up, with the commit message "This was actually the first non-trivial Perl script I ever wrote, and as such was not very pretty." In July 2002 he rewrote it once more, in C. That commit carries the line "Sponsored by: DARPA, NAI Labs", which is a fair bit of sponsorship for a program that lists sockets. The jail option arrived in 2012 from Andrew Thompson. Ryan Moeller added the two pieces that matter most today: in 2020 the step into a jail with its own network stack, and in 2021 the sandbox.

Two questions to the kernel

sockstat keeps no state of its own and learns nothing about sockets from a file. It asks the kernel twice.

The first question goes to the protocol control blocks. The sysctl net.inet.tcp.pcblist and its siblings for UDP and for Unix domain sockets return every socket of their kind, each record carrying the addresses, the state and the address of the socket structure in the kernel. The second question is kern.file, which the kernel's own source calls "Entire file table". The handler in kern_descrip.c walks every process on the system with FOREACH_PROC_IN_SYSTEM, and for each open file it reports the process ID, the user, the descriptor number and, once more, the address of the object behind it. sockstat matches the two addresses, and a socket has an owner.

Both answers are filtered in the kernel before they leave it. The file table passes each process through p_cansee(), and the socket lists check every entry with cr_canseeinpcb(). That is why sockstat inside a jail shows the jail's sockets and nothing else, without a line of code in sockstat to arrange it. The kernel leaves out whatever the caller is not allowed to see.

On a Linux host the same question takes another route. ss fetches the sockets over netlink, and a netlink socket record carries a user and an inode number but no process. To put names to the sockets, ss -p opens /proc, then the fd directory of every process it finds there, and reads each link inside to look for the text socket:[. The work grows with every process on the machine and every file each of them holds, and the answer is assembled in user space from a directory tree.

From a socket to its owner FreeBSD: sockstat Linux: ss -p Ask the kernel: socket lists each record carries the socket's address Ask the kernel: file table process, user, descriptor, address Filtered in the kernel a jail sees its own and nothing else Match by address: owner two questions, answered in the kernel Netlink: socket records a user and an inode, no process Open the process directory one entry per running process Per process: open its fd directory read every link, look for socket:[inode] Match by inode: owner assembled in user space, file by file Sources: sockstat main.c and kern_descrip.c (FreeBSD 15.1); ss.c (iproute2)

Locked in before it looks

A program that asks the kernel for every open file on the machine holds, for a moment, a rather complete picture of it. FreeBSD's answer, since March 2021, is to make sure the program can do very little else.

The order sits in a few dozen lines of main.c. If -j names a jail with its own network stack, sockstat calls jail_attach() first, because Ryan Moeller's commit notes that the sandbox "has to be done after jail_attach". Then it starts Casper, the base system's broker for sandboxed programs, and calls caph_enter_casper(), which puts the process into capability mode. Only after that does it open four services: one for sysctl, one for turning addresses into names, one for protocol names and one for the password database. The address service is limited to CAPNET_ADDR2NAME, and the password service to the few commands and fields that sockstat needs.

I wanted to watch the order happen on a real machine, so I traced one run with ktrace on a FreeBSD 15.1 host carrying twenty jails and 170 processes. sockstat -46ln made 2,863 system calls across seven processes, the tool itself and six from Casper. In its own process, cap_enter was call number 170, and every call before it was the dynamic linker loading libraries, plus the pdfork that started Casper. The number of __sysctl calls the tool made directly, before or after, was nought. Every question about sockets went through the sysctl service, from a process that had already lost the right to open a file by name.

Locked in before it looks: one run of sockstat -46ln 1 Step into the jail, if a VNET jail was named jail_attach, before any sandbox 2 Start Casper six helper processes, started with pdfork 3 Enter capability mode system call 170 of its own process; before it, only the linker from here on: no opening files by name 4 Open four services sysctl, address lookup (limited), protocol names, passwords (limited) 5 Ask for socket lists and file table through the sysctl service; direct sysctl calls: 0 6 Print the table ktrace of one run on FreeBSD 15.1, 27 November 2026: 2,863 system calls in seven processes

The source of ss holds no sandbox at all; a search of misc/ and lib/ in iproute2 finds no seccomp and no landlock. Run as root so that it can see every process, it reads /proc with every right that root has.

A jail, by name

Since 2018 the -j option takes a jail's name as well as its number, so sockstat -j staging reads as the question one meant to ask. What it does next depends on how the jail was built.

Most jails share the host's network stack, which is the default, and every jail on the test host was built that way. Their sockets sit in the host's own lists, so a plain sockstat on the host shows every socket of every jail, and -j narrows the list to one of them by asking the kernel for the jail of each owning process. A jail with its own virtual stack, a VNET jail, keeps its sockets in lists the host does not read. For that case sockstat -j attaches itself to the jail and asks from inside, which the commit of April 2020 explains in one sentence: "Otherwise we do not see all sockets belonging to the jail." The administrator types the same line in both cases.

A Linux container with a network namespace of its own is invisible to ss on the host, which reads the host's namespace alone. The usual way in is nsenter -t <pid> -n ss -tlnp, which needs the process ID of something running inside the container, and that number has to come from the container runtime first. The tool that knows the container's name and the tool that reads its sockets are two different programs, and the administrator carries the number from one to the other.

The sheet

Efficiency. FreeBSD 15.1 (releng/15.1, commit bb03631), lines without comments and blank lines: usr.bin/sockstat 1,871. The binary in the 15.1 release (base.txz) is 33,880 bytes after stripping, and every library it loads (libjail, libxo, libcasper and its four service libraries, libc) ships with the base system. Time per run, as root on the test host, median of 20 rounds of 50 runs: 36 ms for sockstat -46l and 27 ms with -n. For comparison: misc/ss.c in iproute2 (head 5d13122, 24 November 2026) is 4,854 lines, and /usr/bin/ss from Debian 13's iproute2 6.15.0-1 is 211,968 bytes, a little over six times the size of sockstat.

Security. Of the four questions: the tool needs no privilege of its own and sees what the kernel's visibility rules grant the caller, so that inside a jail it sees that jail alone, because the kernel filters with p_cansee() and cr_canseeinpcb(); it takes no data from the network; it parses fixed binary structures handed over by the kernel, with no format of its own; and it enters capability mode before its first question, through Casper. Of 750 FreeBSD security advisories, none carries sockstat in its name.

Longevity. First commit on 14 April 1999, in C since 31 July 2002, 169 commits in twenty-seven years, maintained in the base system ever since and in the hands of a GSoC student as recently as 2025, who added libxo output.

Stability. The manual page of 2002 lists six options, -4, -6, -c, -l, -p and -u, and all six stand in the page for 15.1 with the same meaning and, for the most part, the same words. The seven columns of 2002, USER, COMMAND, PID, FD, PROTO, LOCAL ADDRESS and FOREIGN ADDRESS, are still the first seven, in the same order. Sixteen options and --libxo have joined them since, and none of the old ones has been taken away.

Measured on 27 November 2026: lines counted with a script that strips C comments and blank lines; sizes measured on the release archives; times with /usr/bin/time -p around a shell loop; system calls counted from kdump -H over a ktrace -i of one run; commits counted on the history of usr.bin/sockstat; advisories counted on the FreeBSD advisory index.

The sheet: sockstat in FreeBSD 15.1 1,871 lines of source without comments and blank lines 33,880 bytes, stripped ss in Debian 13: 211,968 36 ms per run, 27 ms with -n 20 jails, 170 processes call 170 enters capability mode before its first question 0 of 750 advisories name sockstat FreeBSD advisory index since 2002 six options, seven columns all still there, same order Measured on 27 November 2026

The limit

The fine detail of TCP lives elsewhere. sockstat shows a connection's state and its congestion control among a handful of TCP details, and stops there. Round-trip times and window sizes per connection are the business of DTrace's tcp provider in the base system, so that question goes to a second tool.

Its filters are short. Port, protocol, jail and loopback cover the questions administrators ask most, as it happens, and anything beyond them is a job for grep or for the libxo output fed to jq.

The host-wide list does not name the jail. A plain sockstat on the host lists the sockets of every shared-stack jail with user, command, process ID and descriptor, and the jail itself is one -j away. A VNET jail always needs its -j.

The sandbox has a price. Six helper processes start and stop with every run, inside the 27 milliseconds that the whole run takes on a host with 170 processes.

A tool that asks the kernel who holds every socket on the machine ought to be trusted with very little, and sockstat has arranged, before it asks, to be trusted with nothing else.