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
|
|
@ -118,7 +118,13 @@ fileread(struct file *f, char *addr, int n)
|
|||
// this is a check of the same condition their sleep loops wait on, made one
|
||||
// level up. Writes still block -- fcntl documents that.
|
||||
if(f->type == FD_PIPE){
|
||||
if(f->nonblock && !(pipe_poll(f->pipe, f->readable, f->writable) & POLLIN))
|
||||
// POLLHUP counts as readable here, and leaving it out was a bug: a pipe
|
||||
// whose writers have all gone is not "nothing yet", it is end of file, and
|
||||
// the answer to a read is 0. Told EAGAIN instead, a caller that polls and
|
||||
// reads spins forever waiting for an end that has already happened.
|
||||
// sockreadable() has always included the equivalent condition.
|
||||
if(f->nonblock &&
|
||||
!(pipe_poll(f->pipe, f->readable, f->writable) & (POLLIN | POLLHUP)))
|
||||
return -EAGAIN;
|
||||
return piperead(f->pipe, addr, n);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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