-/* $OpenBSD: uipc_usrreq.c,v 1.142 2019/07/16 21:41:37 bluhm Exp $ */
+/* $OpenBSD: uipc_usrreq.c,v 1.143 2021/02/10 08:20:09 mvs Exp $ */
/* $NetBSD: uipc_usrreq.c,v 1.18 1996/02/09 19:00:50 christos Exp $ */
/*
#include <sys/task.h>
#include <sys/pledge.h>
#include <sys/pool.h>
+#include <sys/rwlock.h>
-void uipc_setaddr(const struct unpcb *, struct mbuf *);
-
-/* list of all UNIX domain sockets, for unp_gc() */
-LIST_HEAD(unp_head, unpcb) unp_head = LIST_HEAD_INITIALIZER(unp_head);
+/*
+ * Locks used to protect global data and struct members:
+ * I immutable after creation
+ * U unp_lock
+ */
+struct rwlock unp_lock = RWLOCK_INITIALIZER("unplock");
/*
* Stack of sets of files that were passed over a socket but were
* not received and need to be closed.
*/
struct unp_deferral {
- SLIST_ENTRY(unp_deferral) ud_link;
- int ud_n;
+ SLIST_ENTRY(unp_deferral) ud_link; /* [U] */
+ int ud_n; /* [I] */
/* followed by ud_n struct fdpass */
- struct fdpass ud_fp[];
+ struct fdpass ud_fp[]; /* [I] */
};
+void uipc_setaddr(const struct unpcb *, struct mbuf *);
void unp_discard(struct fdpass *, int);
void unp_mark(struct fdpass *, int);
void unp_scan(struct mbuf *, void (*)(struct fdpass *, int));
int unp_nam2sun(struct mbuf *, struct sockaddr_un **, size_t *);
struct pool unpcb_pool;
-/* list of sets of files that were sent over sockets that are now closed */
-SLIST_HEAD(,unp_deferral) unp_deferred = SLIST_HEAD_INITIALIZER(unp_deferred);
-
struct task unp_gc_task = TASK_INITIALIZER(unp_gc, NULL);
-
/*
* Unix communications domain.
*
* rethink name space problems
* need a proper out-of-band
*/
-struct sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX };
-ino_t unp_ino; /* prototype for fake inode numbers */
+const struct sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX };
+
+/* [U] list of all UNIX domain sockets, for unp_gc() */
+LIST_HEAD(unp_head, unpcb) unp_head =
+ LIST_HEAD_INITIALIZER(unp_head);
+/* [U] list of sets of files that were sent over sockets that are now closed */
+SLIST_HEAD(,unp_deferral) unp_deferred =
+ SLIST_HEAD_INITIALIZER(unp_deferred);
+
+ino_t unp_ino; /* [U] prototype for fake inode numbers */
+int unp_rights; /* [U] file descriptors in flight */
+int unp_defer; /* [U] number of deferred fp to close by the GC task */
+int unp_gcing; /* [U] GC task currently running */
void
unp_init(void)
{
pool_init(&unpcb_pool, sizeof(struct unpcb), 0,
- IPL_NONE, 0, "unpcb", NULL);
+ IPL_SOFTNET, 0, "unpcb", NULL);
}
void
switch (so->so_type) {
case SOCK_DGRAM: {
- struct sockaddr *from;
+ const struct sockaddr *from;
if (nam) {
if (unp->unp_conn) {
u_long unpdg_sendspace = 2*1024; /* really max datagram size */
u_long unpdg_recvspace = 4*1024;
-int unp_rights; /* file descriptors in flight */
-
int
uipc_attach(struct socket *so, int proto)
{
struct unpcb *unp;
int error;
+ rw_assert_wrlock(&unp_lock);
+
if (so->so_pcb)
return EISCONN;
if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
void
unp_detach(struct unpcb *unp)
{
- struct vnode *vp;
+ struct socket *so = unp->unp_socket;
+ struct vnode *vp = NULL;
+
+ rw_assert_wrlock(&unp_lock);
LIST_REMOVE(unp, unp_link);
if (unp->unp_vnode) {
+ /*
+ * `v_socket' is only read in unp_connect and
+ * unplock prevents concurrent access.
+ */
+
unp->unp_vnode->v_socket = NULL;
vp = unp->unp_vnode;
unp->unp_vnode = NULL;
- vrele(vp);
}
+
if (unp->unp_conn)
unp_disconnect(unp);
while (!SLIST_EMPTY(&unp->unp_refs))
unp_drop(SLIST_FIRST(&unp->unp_refs), ECONNRESET);
- soisdisconnected(unp->unp_socket);
- unp->unp_socket->so_pcb = NULL;
+ soisdisconnected(so);
+ so->so_pcb = NULL;
m_freem(unp->unp_addr);
pool_put(&unpcb_pool, unp);
if (unp_rights)
task_add(systq, &unp_gc_task);
+
+ if (vp != NULL) {
+ /*
+ * Enforce `i_lock' -> `unplock' because fifo subsystem
+ * requires it. The socket can't be closed concurrently
+ * because the file descriptor reference is
+ * still hold.
+ */
+
+ sounlock(so, SL_LOCKED);
+ KERNEL_LOCK();
+ vrele(vp);
+ KERNEL_UNLOCK();
+ solock(so);
+ }
}
int
struct nameidata nd;
size_t pathlen;
+ if (unp->unp_flags & (UNP_BINDING | UNP_CONNECTING))
+ return (EINVAL);
if (unp->unp_vnode != NULL)
return (EINVAL);
if ((error = unp_nam2sun(nam, &soun, &pathlen)))
NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE,
soun->sun_path, p);
nd.ni_pledge = PLEDGE_UNIX;
+
+ unp->unp_flags |= UNP_BINDING;
+
+ /*
+ * Enforce `i_lock' -> `unplock' because fifo subsystem
+ * requires it. The socket can't be closed concurrently
+ * because the file descriptor reference is still held.
+ */
+
+ sounlock(unp->unp_socket, SL_LOCKED);
+
+ KERNEL_LOCK();
/* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
- if ((error = namei(&nd)) != 0) {
+ error = namei(&nd);
+ if (error != 0) {
m_freem(nam2);
- return (error);
+ solock(unp->unp_socket);
+ goto out;
}
vp = nd.ni_vp;
if (vp != NULL) {
vput(nd.ni_dvp);
vrele(vp);
m_freem(nam2);
- return (EADDRINUSE);
+ error = EADDRINUSE;
+ solock(unp->unp_socket);
+ goto out;
}
VATTR_NULL(&vattr);
vattr.va_type = VSOCK;
vput(nd.ni_dvp);
if (error) {
m_freem(nam2);
- return (error);
+ solock(unp->unp_socket);
+ goto out;
}
+ solock(unp->unp_socket);
unp->unp_addr = nam2;
vp = nd.ni_vp;
vp->v_socket = unp->unp_socket;
unp->unp_connid.pid = p->p_p->ps_pid;
unp->unp_flags |= UNP_FEIDSBIND;
VOP_UNLOCK(vp);
- return (0);
+out:
+ KERNEL_UNLOCK();
+ unp->unp_flags &= ~UNP_BINDING;
+
+ return (error);
}
int
struct nameidata nd;
int error;
+ unp = sotounpcb(so);
+ if (unp->unp_flags & (UNP_BINDING | UNP_CONNECTING))
+ return (EISCONN);
if ((error = unp_nam2sun(nam, &soun, NULL)))
return (error);
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, soun->sun_path, p);
nd.ni_pledge = PLEDGE_UNIX;
- if ((error = namei(&nd)) != 0)
- return (error);
+
+ unp->unp_flags |= UNP_CONNECTING;
+
+ /*
+ * Enforce `i_lock' -> `unplock' because fifo subsystem
+ * requires it. The socket can't be closed concurrently
+ * because the file descriptor reference is still held.
+ */
+
+ sounlock(so, SL_LOCKED);
+
+ KERNEL_LOCK();
+ error = namei(&nd);
+ if (error != 0)
+ goto unlock;
vp = nd.ni_vp;
if (vp->v_type != VSOCK) {
error = ENOTSOCK;
- goto bad;
+ goto put;
}
if ((error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) != 0)
- goto bad;
+ goto put;
+ solock(so);
so2 = vp->v_socket;
if (so2 == NULL) {
error = ECONNREFUSED;
- goto bad;
+ goto put_locked;
}
if (so->so_type != so2->so_type) {
error = EPROTOTYPE;
- goto bad;
+ goto put_locked;
}
if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
(so3 = sonewconn(so2, 0)) == 0) {
error = ECONNREFUSED;
- goto bad;
+ goto put_locked;
}
- unp = sotounpcb(so);
unp2 = sotounpcb(so2);
unp3 = sotounpcb(so3);
if (unp2->unp_addr)
}
}
error = unp_connect2(so, so2);
-bad:
+put_locked:
+ sounlock(so, SL_LOCKED);
+put:
vput(vp);
+unlock:
+ KERNEL_UNLOCK();
+ solock(so);
+ unp->unp_flags &= ~UNP_CONNECTING;
+
return (error);
}
struct unpcb *unp = sotounpcb(so);
struct unpcb *unp2;
+ rw_assert_wrlock(&unp_lock);
+
if (so2->so_type != so->so_type)
return (EPROTOTYPE);
unp2 = sotounpcb(so2);
{
struct socket *so = unp->unp_socket;
- KERNEL_ASSERT_LOCKED();
+ rw_assert_wrlock(&unp_lock);
so->so_error = errno;
unp_disconnect(unp);
if (so->so_head) {
so->so_pcb = NULL;
/*
- * As long as the KERNEL_LOCK() is the default lock for Unix
- * sockets, do not release it.
+ * As long as `unp_lock' is taken before entering
+ * uipc_usrreq() releasing it here would lead to a
+ * double unlock.
*/
sofree(so, SL_NOUNLOCK);
m_freem(unp->unp_addr);
struct file *fp;
int nfds, error = 0;
+ rw_assert_wrlock(&unp_lock);
+
/*
* This code only works because SCM_RIGHTS is the only supported
* control message type on unix sockets. Enforce this here.
/* Make sure the recipient should be able to see the descriptors.. */
rp = (struct fdpass *)CMSG_DATA(cm);
+
+ /* fdp->fd_rdir requires KERNEL_LOCK() */
+ KERNEL_LOCK();
+
for (i = 0; i < nfds; i++) {
fp = rp->fp;
rp++;
}
}
+ KERNEL_UNLOCK();
+
fds = mallocarray(nfds, sizeof(int), M_TEMP, M_WAITOK);
restart:
int i, error;
int nfds, *ip, fd, neededspace;
+ rw_assert_wrlock(&unp_lock);
+
/*
* Check for two potential msg_controllen values because
* IETF stuck their nose in a place it does not belong.
return (error);
}
-int unp_defer, unp_gcing;
-
void
unp_gc(void *arg __unused)
{
struct unpcb *unp;
int nunref, i;
+ rw_enter_write(&unp_lock);
+
if (unp_gcing)
- return;
+ goto unlock;
unp_gcing = 1;
/* close any fds on the deferred list */
if ((unp = fptounp(fp)) != NULL)
unp->unp_msgcount--;
unp_rights--;
+ rw_exit_write(&unp_lock);
(void) closef(fp, NULL);
+ rw_enter_write(&unp_lock);
}
free(defer, M_TEMP, sizeof(*defer) +
sizeof(struct fdpass) * defer->ud_n);
}
}
unp_gcing = 0;
+unlock:
+ rw_exit_write(&unp_lock);
}
void
struct unpcb *unp;
int i;
+ rw_assert_wrlock(&unp_lock);
+
for (i = 0; i < nfds; i++) {
if (rp[i].fp == NULL)
continue;
{
struct unp_deferral *defer;
+ rw_assert_wrlock(&unp_lock);
+
/* copy the file pointers to a deferral structure */
defer = malloc(sizeof(*defer) + sizeof(*rp) * nfds, M_TEMP, M_WAITOK);
defer->ud_n = nfds;
-/* $OpenBSD: unpcb.h,v 1.17 2019/07/15 12:28:06 bluhm Exp $ */
+/* $OpenBSD: unpcb.h,v 1.18 2021/02/10 08:20:09 mvs Exp $ */
/* $NetBSD: unpcb.h,v 1.6 1994/06/29 06:46:08 cgd Exp $ */
/*
* Stream sockets keep copies of receive sockbuf sb_cc and sb_mbcnt
* so that changes in the sockbuf may be computed to modify
* back pressure on the sender accordingly.
+ *
+ * Locks used to protect struct members:
+ * I immutable after creation
+ * U unp_lock
*/
+
struct unpcb {
- struct socket *unp_socket; /* pointer back to socket */
- struct vnode *unp_vnode; /* if associated with file */
- struct file *unp_file; /* backpointer for unp_gc() */
- struct unpcb *unp_conn; /* control block of connected socket */
- ino_t unp_ino; /* fake inode number */
- SLIST_HEAD(,unpcb) unp_refs; /* referencing socket linked list */
- SLIST_ENTRY(unpcb) unp_nextref; /* link in unp_refs list */
- struct mbuf *unp_addr; /* bound address of socket */
- long unp_msgcount; /* references from socket rcv buf */
- int unp_flags; /* this unpcb contains peer eids */
- struct sockpeercred unp_connid;/* id of peer process */
- struct timespec unp_ctime; /* holds creation time */
- LIST_ENTRY(unpcb) unp_link; /* link in per-AF list of sockets */
+ struct socket *unp_socket; /* [I] pointer back to socket */
+ struct vnode *unp_vnode; /* [U] if associated with file */
+ struct file *unp_file; /* [U] backpointer for unp_gc() */
+ struct unpcb *unp_conn; /* [U] control block of connected socket */
+ ino_t unp_ino; /* [U] fake inode number */
+ SLIST_HEAD(,unpcb) unp_refs; /* [U] referencing socket linked list */
+ SLIST_ENTRY(unpcb) unp_nextref; /* [U] link in unp_refs list */
+ struct mbuf *unp_addr; /* [U] bound address of socket */
+ long unp_msgcount; /* [U] references from socket rcv buf */
+ int unp_flags; /* [U] this unpcb contains peer eids */
+ struct sockpeercred unp_connid;/* [U] id of peer process */
+ struct timespec unp_ctime; /* [I] holds creation time */
+ LIST_ENTRY(unpcb) unp_link; /* [U] link in per-AF list of sockets */
};
/*
#define UNP_GCMARK 0x04 /* mark during unp_gc() */
#define UNP_GCDEFER 0x08 /* ref'd, but not marked in this pass */
#define UNP_GCDEAD 0x10 /* unref'd in this pass */
+#define UNP_BINDING 0x20 /* unp is binding now */
+#define UNP_CONNECTING 0x40 /* unp is connecting now */
#define sotounpcb(so) ((struct unpcb *)((so)->so_pcb))