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:
parent
ea68ef4b54
commit
9e6d8dd4e3
15 changed files with 1133 additions and 42 deletions
|
|
@ -581,3 +581,364 @@ SYSCALL_DEFINE(getsockopt)
|
|||
return r;
|
||||
return opt_out(uval, ulenp, val);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// sendmsg / recvmsg
|
||||
//
|
||||
// These exist for SCM_RIGHTS. Everything else they do -- scatter/gather over an
|
||||
// iovec, an address on the side -- sendto and recvfrom already did, and the
|
||||
// reason to build the general call rather than a `sendfd()` is that the
|
||||
// programs that need it are not ours: libwayland and toybox's ping both call
|
||||
// recvmsg with Linux's structures and would not notice a friendlier door.
|
||||
//
|
||||
// docs/plan/foundations.md item 1, and docs/plan/hybrid.md mechanism 1.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Linux caps an iovec at UIO_MAXIOV = 1024. Thirty-two is the cap here for one
|
||||
// reason: each entry is validated and copied individually, and a caller that
|
||||
// needs more than thirty-two separate buffers in one message is describing a
|
||||
// problem this kernel has not met. EMSGSIZE is Linux's answer for too many.
|
||||
#define SCH_IOV_MAX 32
|
||||
|
||||
// The control buffer is copied onto the kernel stack, and that stack is 4 KB
|
||||
// (param.h, KSTACKSIZE). 256 bytes holds CMSG_SPACE(SCM_MAXFD * 4) six times
|
||||
// over; anything larger on the *send* side is a caller with a plan we do not
|
||||
// need to support.
|
||||
#define SCH_CMSG_MAX 256
|
||||
|
||||
// Resolve a descriptor to both the socket and the open file description --
|
||||
// recvmsg needs the second because O_NONBLOCK lives there, not on the socket.
|
||||
static int
|
||||
argsockfile(int n, struct socket **ps, struct file **pf)
|
||||
{
|
||||
int fd;
|
||||
struct file *f;
|
||||
|
||||
if (argint(n, &fd) < 0)
|
||||
return -EBADF;
|
||||
if (fd < 0 || fd >= NOFILE || !(f = myproc()->ofile[fd]))
|
||||
return -EBADF;
|
||||
if (f->type != FD_SOCKET)
|
||||
return -ENOTSOCK;
|
||||
*ps = f->sock;
|
||||
*pf = f;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
fetch_msghdr(addr_t umsg, struct msghdr *m)
|
||||
{
|
||||
if (umsg == 0 || copy_from_user(m, umsg, sizeof(*m)) < 0)
|
||||
return -EFAULT;
|
||||
if (m->msg_iovlen > SCH_IOV_MAX)
|
||||
return -EMSGSIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// One iovec, validated. Returns the byte count, 0 for an empty entry that the
|
||||
// caller should skip, or negative on a bad pointer.
|
||||
static int
|
||||
fetch_iov(struct msghdr *m, uint64 i, char **pp)
|
||||
{
|
||||
struct iovec iv;
|
||||
|
||||
if (copy_from_user(&iv, (addr_t)&m->msg_iov[i], sizeof(iv)) < 0)
|
||||
return -EFAULT;
|
||||
if (iv.iov_len == 0)
|
||||
return 0;
|
||||
if (iv.iov_len > 0x7fffffff)
|
||||
return -EINVAL;
|
||||
if (!(*pp = (char *)user_ptr((addr_t)iv.iov_base, iv.iov_len)))
|
||||
return -EFAULT;
|
||||
return (int)iv.iov_len;
|
||||
}
|
||||
|
||||
// Pull the descriptors out of a control buffer and take a kernel reference on
|
||||
// each. On any failure the ones already taken are released -- a half-gathered
|
||||
// message must not leave references behind for a call that returns an error.
|
||||
static int
|
||||
scm_gather(struct msghdr *m, struct file **fs, int *nfp)
|
||||
{
|
||||
char buf[SCH_CMSG_MAX];
|
||||
uint64 off, payload, i;
|
||||
struct cmsghdr *c;
|
||||
struct file *f;
|
||||
int ufd, r;
|
||||
|
||||
*nfp = 0;
|
||||
if (m->msg_control == 0 || m->msg_controllen < sizeof(struct cmsghdr))
|
||||
return 0;
|
||||
if (m->msg_controllen > SCH_CMSG_MAX)
|
||||
return -EINVAL;
|
||||
if (copy_from_user(buf, (addr_t)m->msg_control, m->msg_controllen) < 0)
|
||||
return -EFAULT;
|
||||
|
||||
for (off = 0; off + sizeof(struct cmsghdr) <= m->msg_controllen; ) {
|
||||
c = (struct cmsghdr *)(buf + off);
|
||||
if (c->cmsg_len < CMSG_LEN(0) || c->cmsg_len > m->msg_controllen - off) {
|
||||
r = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
if (c->cmsg_level == SOL_SOCKET && c->cmsg_type == SCM_RIGHTS) {
|
||||
payload = c->cmsg_len - CMSG_LEN(0);
|
||||
for (i = 0; i + sizeof(int) <= payload; i += sizeof(int)) {
|
||||
memmove(&ufd, CMSG_DATA(c) + i, sizeof(int));
|
||||
if (*nfp >= SCM_MAXFD) {
|
||||
r = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
if (ufd < 0 || ufd >= NOFILE || !(f = myproc()->ofile[ufd])) {
|
||||
r = -EBADF;
|
||||
goto fail;
|
||||
}
|
||||
fs[(*nfp)++] = filedup(f);
|
||||
}
|
||||
} else if (c->cmsg_level == SOL_SOCKET) {
|
||||
// SCM_CREDENTIALS and friends. Refused rather than ignored: a
|
||||
// caller that asked to pass credentials and was told yes would
|
||||
// believe the receiver got them.
|
||||
r = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
off += CMSG_ALIGN(c->cmsg_len);
|
||||
}
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
while (*nfp > 0)
|
||||
fileclose(fs[--*nfp]);
|
||||
return r;
|
||||
}
|
||||
|
||||
SYSCALL_DEFINE(sendmsg)
|
||||
{
|
||||
struct socket *s;
|
||||
struct file *file;
|
||||
struct msghdr m;
|
||||
struct file *fs[SCM_MAXFD];
|
||||
addr_t umsg;
|
||||
uint64 i;
|
||||
int flags, nf = 0, r;
|
||||
long sent = 0;
|
||||
uint ip = 0;
|
||||
ushort port = 0;
|
||||
int addressed = 0;
|
||||
|
||||
if ((r = argsockfile(0, &s, &file)) < 0)
|
||||
return r;
|
||||
argaddr(1, &umsg);
|
||||
argint(2, &flags);
|
||||
if ((r = fetch_msghdr(umsg, &m)) < 0)
|
||||
return r;
|
||||
|
||||
if ((r = scm_gather(&m, fs, &nf)) < 0)
|
||||
return r;
|
||||
|
||||
// SCM_RIGHTS is an AF_UNIX facility and there is nothing to generalise: a
|
||||
// descriptor has no meaning on the far side of a wire.
|
||||
if (nf > 0 && sock_domain(s) != AF_UNIX) {
|
||||
r = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (m.msg_name && sock_domain(s) == AF_INET) {
|
||||
if ((r = fetch_sin((addr_t)m.msg_name, (int)m.msg_namelen, &ip, &port)) < 0)
|
||||
goto fail;
|
||||
port = port_from_net(port);
|
||||
addressed = 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < m.msg_iovlen; i++) {
|
||||
char *p;
|
||||
int len, w;
|
||||
|
||||
if ((len = fetch_iov(&m, i, &p)) < 0) {
|
||||
r = len;
|
||||
goto fail;
|
||||
}
|
||||
if (len == 0)
|
||||
continue;
|
||||
|
||||
if (addressed)
|
||||
w = socksendto(s, p, len, ip, port);
|
||||
else if (nf > 0)
|
||||
w = sockwrite_fds(s, p, len, fs, nf);
|
||||
else
|
||||
w = sockwrite(s, p, len);
|
||||
|
||||
if (w < 0) {
|
||||
if (sent > 0)
|
||||
break; // a short write is still a write
|
||||
r = w;
|
||||
goto fail;
|
||||
}
|
||||
// Any positive count means the descriptors are queued and no longer
|
||||
// ours; see the contract above sockwrite_fds.
|
||||
if (w > 0)
|
||||
nf = 0;
|
||||
sent += w;
|
||||
if (w < len)
|
||||
break;
|
||||
}
|
||||
|
||||
// Descriptors with no bytes to ride on. The delivery rule pins them to the
|
||||
// position of a first byte that in this case never arrives, so there is no
|
||||
// position at which a receiver could ever be handed them.
|
||||
if (nf > 0) {
|
||||
r = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
return (int)sent;
|
||||
|
||||
fail:
|
||||
while (nf > 0)
|
||||
fileclose(fs[--nf]);
|
||||
return r;
|
||||
}
|
||||
|
||||
SYSCALL_DEFINE(recvmsg)
|
||||
{
|
||||
struct socket *s;
|
||||
struct file *file;
|
||||
struct msghdr m;
|
||||
struct file *fs[SCM_MAXFD];
|
||||
char cbuf[CMSG_SPACE(SCM_MAXFD * sizeof(int))];
|
||||
int ufd[SCM_MAXFD];
|
||||
addr_t umsg;
|
||||
uint64 i, clen = 0;
|
||||
int flags, nf = 0, mflags = 0, r;
|
||||
long got = 0;
|
||||
uint ip = 0;
|
||||
ushort port = 0;
|
||||
|
||||
if ((r = argsockfile(0, &s, &file)) < 0)
|
||||
return r;
|
||||
argaddr(1, &umsg);
|
||||
argint(2, &flags);
|
||||
if ((r = fetch_msghdr(umsg, &m)) < 0)
|
||||
return r;
|
||||
|
||||
// O_NONBLOCK on the description and MSG_DONTWAIT on the call mean the same
|
||||
// thing and are both honoured, because a program that polls and then reads
|
||||
// uses whichever one its library happened to pick.
|
||||
if ((file->nonblock || (flags & MSG_DONTWAIT)) && !sockreadable(s))
|
||||
return -EAGAIN;
|
||||
|
||||
for (i = 0; i < m.msg_iovlen; i++) {
|
||||
char *p;
|
||||
int len, rd;
|
||||
|
||||
if ((len = fetch_iov(&m, i, &p)) < 0)
|
||||
return len;
|
||||
if (len == 0)
|
||||
continue;
|
||||
|
||||
// Only the first read may block, and it stops for good once a message
|
||||
// carrying descriptors has been taken: the next iovec would be filled
|
||||
// from the message *after* it, and the caller has no way to say where
|
||||
// one ended and the next began.
|
||||
if (got > 0 && (nf > 0 || !sockreadable(s)))
|
||||
break;
|
||||
|
||||
if (sock_domain(s) == AF_UNIX)
|
||||
rd = sockread_fds(s, p, len, fs, &nf);
|
||||
else if (m.msg_name && sock_domain(s) == AF_INET)
|
||||
rd = sockrecvfrom(s, p, len, &ip, &port);
|
||||
else
|
||||
rd = sockread(s, p, len);
|
||||
|
||||
if (rd < 0) {
|
||||
if (got > 0)
|
||||
break;
|
||||
return rd;
|
||||
}
|
||||
got += rd;
|
||||
if (rd < len)
|
||||
break;
|
||||
}
|
||||
|
||||
if (nf > 0) {
|
||||
int room = 0, installed = 0, j;
|
||||
uint64 writelen;
|
||||
|
||||
if (m.msg_control && m.msg_controllen >= CMSG_LEN(sizeof(int)))
|
||||
room = (int)((m.msg_controllen - CMSG_LEN(0)) / sizeof(int));
|
||||
if (room > nf)
|
||||
room = nf;
|
||||
|
||||
// How much will actually be written. CMSG_SPACE includes the padding
|
||||
// to where a *next* header would start, and there is no next header --
|
||||
// so when the caller's buffer is not big enough for that padding the
|
||||
// message still fits without it. Writing CMSG_SPACE unconditionally
|
||||
// would overrun a buffer sized with CMSG_LEN, which is legal.
|
||||
writelen = CMSG_SPACE(room * sizeof(int));
|
||||
if (writelen > m.msg_controllen)
|
||||
writelen = CMSG_LEN(room * sizeof(int));
|
||||
|
||||
// Check the control buffer is writable *before* installing anything.
|
||||
// A descriptor that has been put in the table and then reported to a
|
||||
// caller who never learned its number is unclosable.
|
||||
if (room > 0 && !user_ptr((addr_t)m.msg_control, writelen))
|
||||
room = 0;
|
||||
|
||||
for (j = 0; j < room; j++) {
|
||||
int d = fdalloc(fs[j]);
|
||||
if (d < 0)
|
||||
break;
|
||||
if (flags & MSG_CMSG_CLOEXEC)
|
||||
myproc()->ofile_flags[d] |= FD_CLOEXEC;
|
||||
ufd[installed++] = d;
|
||||
}
|
||||
|
||||
// Whatever did not fit is closed here and nowhere else: the kernel
|
||||
// holds the last reference, and a receiver with no room is still a
|
||||
// delivery. MSG_CTRUNC is how the caller finds out.
|
||||
for (j = installed; j < nf; j++)
|
||||
fileclose(fs[j]);
|
||||
if (installed < nf)
|
||||
mflags |= MSG_CTRUNC;
|
||||
|
||||
if (installed > 0) {
|
||||
struct cmsghdr *c = (struct cmsghdr *)cbuf;
|
||||
memset(cbuf, 0, sizeof(cbuf)); // pad bytes, not kernel stack
|
||||
c->cmsg_len = CMSG_LEN(installed * sizeof(int));
|
||||
c->cmsg_level = SOL_SOCKET;
|
||||
c->cmsg_type = SCM_RIGHTS;
|
||||
memmove(CMSG_DATA(c), ufd, installed * sizeof(int));
|
||||
clen = CMSG_SPACE(installed * sizeof(int));
|
||||
if (clen > m.msg_controllen)
|
||||
clen = CMSG_LEN(installed * sizeof(int));
|
||||
if (copy_to_user((addr_t)m.msg_control, cbuf, clen) < 0)
|
||||
return -EFAULT; // ruled out above, but not assumed
|
||||
}
|
||||
}
|
||||
|
||||
// The address, for the family that has one. Same rule as recvfrom: a Unix
|
||||
// socket reports a length of zero rather than a sockaddr full of nothing.
|
||||
if (m.msg_name) {
|
||||
uint32 alen = 0;
|
||||
if (sock_domain(s) == AF_INET) {
|
||||
struct sockaddr_in sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr = ip;
|
||||
sa.sin_port = port_to_net(port);
|
||||
alen = (uint32)sizeof(sa);
|
||||
if (alen > m.msg_namelen)
|
||||
alen = m.msg_namelen;
|
||||
if (alen && copy_to_user((addr_t)m.msg_name, &sa, alen) < 0)
|
||||
return -EFAULT;
|
||||
}
|
||||
if (copy_to_user(umsg + __builtin_offsetof(struct msghdr, msg_namelen),
|
||||
&alen, sizeof(alen)) < 0)
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
if (copy_to_user(umsg + __builtin_offsetof(struct msghdr, msg_controllen),
|
||||
&clen, sizeof(clen)) < 0)
|
||||
return -EFAULT;
|
||||
if (copy_to_user(umsg + __builtin_offsetof(struct msghdr, msg_flags),
|
||||
&mflags, sizeof(mflags)) < 0)
|
||||
return -EFAULT;
|
||||
return (int)got;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,6 +140,10 @@ SYSCALL(192, 55, getsockopt)
|
|||
// takes three arguments, so reading a fourth register from an accept() caller
|
||||
// reads whatever it last left there and invents a flag out of stack litter.
|
||||
SYSCALL(198, 288, accept4)
|
||||
// sendmsg/recvmsg exist for the one thing only they can do: carry a descriptor
|
||||
// from one process to another (SCM_RIGHTS). docs/plan/foundations.md item 1.
|
||||
SYSCALL(199, 46, sendmsg)
|
||||
SYSCALL(200, 47, recvmsg)
|
||||
|
||||
// --- time ---
|
||||
SYSCALL( 28, 37, alarm)
|
||||
|
|
|
|||
Loading…
Reference in a new issue