sockets: a descriptor can cross between processes

sendmsg/recvmsg with SCM_RIGHTS. Item 1 of docs/plan/foundations.md and
mechanism 1 of docs/plan/hybrid.md, which are the same primitive seen from two
directions: Wayland's wl_shm_create_pool passes a file descriptor with no
integer-shaped alternative, and handing a driver process the device it is
allowed to touch is the same act. Both plans stopped here.

Linux's structures byte for byte -- msghdr, cmsghdr, the CMSG_ macros -- because
the programs that need this are not ours. libwayland and toybox both call
recvmsg with Linux's headers and would not notice a friendlier door.

The delivery rule is the whole design. A batch of descriptors is pinned to the
stream position of the first byte of the message that carried it, and that has
two halves: a reader arriving at that position is handed them, and a reader that
has not reached it is stopped SHORT of it. Leave out the second half and a
program asking for 4096 bytes swallows three messages and is handed one
message's descriptors, with nothing in the result saying which. socktest checks
exactly that: two one-byte messages, an eight-byte read, one byte back.

The kernel holds a reference to every descriptor in flight, so a sender may
close its own copy the moment sendmsg returns. Giving that reference back is the
part that leaks if it is not written down, and there are three ways a message
ends:

  - received with room            installed in the receiver's table
  - received without room, or by
    plain read(2)                 closed; MSG_CTRUNC for the first case
  - never received, socket closed closed by sockclose

The middle row looks like a choice and is not: holding kernel references for a
receiver that has proved it is not looking is a leak with extra steps.

No garbage collector. A socket descriptor sent over that same socket and never
received holds a reference to the socket that holds the reference to it, and
neither close reaches zero. Linux carries several hundred lines for this.
Nothing on either plan passes a socket over a socket -- Wayland passes memory,
the driver work passes devices -- so it is a bounded leak that is documented
rather than a silent one.

Fourteen checks in cmd/socktest, and every one moves data through the descriptor
rather than inspecting it. The descriptor passed is always a pipe end, because a
pipe announces its own lifetime: a read of a pipe whose writers are all gone
returns 0, which turns "was the kernel's reference released" -- otherwise
invisible from userspace -- into a value a test can compare.

Two bugs fell out, neither in the new code:

  - sockpair() never set the socket's domain, so every socketpair() socket
    claimed domain 0. Invisible while the data paths asked only whether a socket
    was AF_INET or AF_NETLINK, since zero answers "neither" correctly.
    SCM_RIGHTS asks "is this AF_UNIX", and zero is wrong for that one.

  - pipe_poll() reports POLLHUP but not POLLIN at end of file, so a non-blocking
    read of a pipe whose writers had all gone returned EAGAIN forever instead of
    0 -- a program that polls and reads spins waiting for an end that already
    happened. sockreadable() always had the equivalent condition; the pipe path
    did not. Found because the test helper for "is this descriptor closed" is a
    non-blocking read of a pipe.

projects/schcom/README.md said SCM_RIGHTS was done for a long time while nothing
in the kernel implemented it, and an entire GUI plan was drawn on that line.
It is true now, which is not the same as it having been right, and the README
says so.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
This commit is contained in:
Nexoniarz 2026-09-13 19:49:37 +02:00
commit 9e6d8dd4e3
15 changed files with 1133 additions and 42 deletions

View file

@ -45,6 +45,10 @@ static struct {
struct socket sock[NSOCK];
} stab;
// Defined with the rest of the descriptor-passing code below; declared here
// because sockclose is the one caller that cannot be moved after it.
static void scm_drain(struct socket *s);
void
sockinit(void)
{
@ -123,27 +127,46 @@ sockclose(struct socket *s)
return;
}
struct socket *q[NBACKLOG];
int nq;
acquire(&stab.lock);
if(--s->ref > 0){
release(&stab.lock);
return;
}
orphan_peer(s);
// A listener that goes away takes its unaccepted connections with it. Each
// queued entry is the *server* side of a connection whose client is already
// live and may be writing into it; that client has to be told, or it hangs
// on a socket that will never be read.
for(i = 0; i < s->nqueue; i++){
struct socket *q = s->queue[i];
if(q == 0)
continue;
orphan_peer(q);
q->state = SS_FREE;
q->ref = 0;
}
//
// Taking the backlog out here rather than walking it below is what lets the
// drain happen with the lock dropped. At this point the reference count is
// zero and the state is not yet SS_FREE, so nothing can find these sockets
// and alloc_locked will not reuse them either.
nq = s->nqueue;
for(i = 0; i < nq; i++)
q[i] = s->queue[i];
s->nqueue = 0;
release(&stab.lock);
// Descriptors that were sent to these sockets and never received have to be
// closed rather than leaked -- the kernel is holding the only reference.
scm_drain(s);
for(i = 0; i < nq; i++)
if(q[i])
scm_drain(q[i]);
acquire(&stab.lock);
orphan_peer(s);
for(i = 0; i < nq; i++){
if(q[i] == 0)
continue;
orphan_peer(q[i]);
q[i]->state = SS_FREE;
q[i]->ref = 0;
}
s->state = SS_FREE;
s->ref = 0;
@ -247,15 +270,76 @@ sockname(struct socket *s)
return s->name;
}
// ---------------------------------------------------------------------------
// Descriptor passing
//
// docs/plan/hybrid.md mechanism 1, and item 1 of foundations.md. The kernel
// holds a reference on every descriptor in flight, which is what makes the
// sender free to close its own copy the instant sendmsg returns -- and what
// makes dropping a socket with descriptors still queued a thing that has to be
// handled rather than a thing that happens to work.
//
// **What this does not have is a garbage collector.** A descriptor for an
// AF_UNIX socket, sent over that same socket and never received, holds a
// reference to the socket that holds the reference to it; neither side's close
// can reach zero and the pair leaks until reboot. Linux carries several hundred
// lines to break exactly these cycles. Nothing on either plan passes a socket
// over a socket -- Wayland passes memory and the driver work passes devices --
// so this is a known, bounded leak (thirty-two sockets) and not a silent one.
// ---------------------------------------------------------------------------
// Close every descriptor still queued on s.
//
// Called with the socket already unreachable but *not yet marked free*, and
// without stab.lock: fileclose can sleep. Popping one at a time keeps the
// kernel stack out of it -- a snapshot of every batch would be half a page.
static void
scm_drain(struct socket *s)
{
struct file *f;
int i, j;
for(;;){
f = 0;
acquire(&stab.lock);
for(i = 0; i < s->nscm && f == 0; i++)
for(j = 0; j < s->scm[i].n; j++)
if(s->scm[i].f[j]){
f = s->scm[i].f[j];
s->scm[i].f[j] = 0;
break;
}
if(f == 0){
s->nscm = 0;
release(&stab.lock);
return;
}
release(&stab.lock);
fileclose(f);
}
}
// ---------------------------------------------------------------------------
// Data
// ---------------------------------------------------------------------------
// Read bytes, and the descriptors that came with them.
//
// `fout` is an array of SCM_MAXFD slots, or 0. **Zero is not "ignore them"**:
// a plain read(2) of a message carrying descriptors *closes* them, because the
// alternative is holding kernel references for a receiver that has already
// proved it is not looking. That is what Linux does and it is the only choice
// that cannot leak.
int
sockread(struct socket *s, char *addr, int n)
sockread_fds(struct socket *s, char *addr, int n, struct file **fout, int *nfout)
{
struct file *taken[SCM_MAXFD];
int got = 0;
int i;
if(nfout)
*nfout = 0;
if(n < 0)
return -EINVAL;
if(s->domain == AF_INET)
@ -289,6 +373,24 @@ sockread(struct socket *s, char *addr, int n)
sleep(&s->nread, &stab.lock);
}
// A message carrying descriptors is a boundary. This read either starts
// exactly on one -- and takes them -- or is cut short so that it stops
// before it, leaving the next read to start there.
if(s->nscm > 0){
if(s->scm[0].pos == s->nread){
got = s->scm[0].n;
for(i = 0; i < got; i++)
taken[i] = s->scm[0].f[i];
for(i = 1; i < s->nscm; i++)
s->scm[i - 1] = s->scm[i];
s->nscm--;
}
// Positions strictly increase -- a message with descriptors carries at
// least one byte -- so after taking one the next is genuinely ahead.
if(s->nscm > 0 && (uint)n > s->scm[0].pos - s->nread)
n = (int)(s->scm[0].pos - s->nread);
}
for(i = 0; i < n; i++){
if(s->nread == s->nwrite)
break;
@ -299,17 +401,44 @@ sockread(struct socket *s, char *addr, int n)
// on a full buffer that this call has just made room in.
wakeup(&s->nwrite);
release(&stab.lock);
// Outside the lock, because fileclose can sleep.
if(fout){
for(int j = 0; j < got; j++)
fout[j] = taken[j];
if(nfout)
*nfout = got;
} else {
for(int j = 0; j < got; j++)
fileclose(taken[j]);
}
return i;
}
int
sockwrite(struct socket *s, char *addr, int n)
sockread(struct socket *s, char *addr, int n)
{
return sockread_fds(s, addr, n, 0, 0);
}
// Write bytes, optionally handing `nf` descriptors over with them.
//
// The kernel takes its own reference on each before it gets here, so the
// sender may close its copies as soon as this returns. A return of *any*
// positive count means the descriptors were queued; a negative return means
// they were not, and are still the caller's to close. There is no third case,
// which is deliberate -- the attach happens under the same lock hold as the
// first byte, so no reader can slip between them.
int
sockwrite_fds(struct socket *s, char *addr, int n, struct file **f, int nf)
{
struct socket *dst;
int i;
if(n < 0)
return -EINVAL;
if(nf > 0 && (s->domain != AF_UNIX || n == 0))
return -EINVAL;
if(s->domain == AF_INET)
return inet_write(s->inet, addr, n);
@ -339,6 +468,13 @@ sockwrite(struct socket *s, char *addr, int n)
return -EPIPE;
}
// Refuse early and cleanly rather than part-way through the bytes. A sender
// that is this far ahead of its receiver is not helped by a partial message.
if(nf > 0 && s->peer->nscm >= NSCM){
release(&stab.lock);
return -ENOBUFS;
}
for(i = 0; i < n; i++){
// Re-read every iteration: the peer can go away while we sleep below.
dst = s->peer;
@ -356,6 +492,20 @@ sockwrite(struct socket *s, char *addr, int n)
sigsend(myproc()->pid, SIGPIPE);
return i > 0 ? i : -EPIPE; // a short write is still a write
}
// The descriptors ride at the position of the first byte, pinned here --
// between the room check above and the store below, with the lock held
// throughout, so the position cannot be read past before it exists.
if(i == 0 && nf > 0){
if(dst->nscm >= NSCM){
release(&stab.lock);
return -ENOBUFS;
}
struct scm_fds *b = &dst->scm[dst->nscm++];
b->pos = dst->nwrite;
b->n = nf;
for(int j = 0; j < nf; j++)
b->f[j] = f[j];
}
dst->data[dst->nwrite++ % SOCKBUF] = addr[i];
}
@ -364,6 +514,12 @@ sockwrite(struct socket *s, char *addr, int n)
return i;
}
int
sockwrite(struct socket *s, char *addr, int n)
{
return sockwrite_fds(s, addr, n, 0, 0);
}
// ---------------------------------------------------------------------------
// Connection setup
// ---------------------------------------------------------------------------
@ -427,6 +583,12 @@ sockpair(struct socket **pa, struct socket **pb)
release(&stab.lock);
return -ENFILE;
}
// The domain was never set here, and nothing noticed for as long as every
// data path asked only whether it was AF_INET or AF_NETLINK -- a socketpair
// socket answered "neither" by being zero. SCM_RIGHTS asks the other
// question, "is this AF_UNIX", and zero is the wrong answer to that one.
a->domain = AF_UNIX;
b->domain = AF_UNIX;
a->peer = b; a->state = SS_CONNECTED;
b->peer = a; b->state = SS_CONNECTED;
release(&stab.lock);