Skip to content

Commit 93c5951

Browse files
Miklos Szeredigregkh
authored andcommitted
af_unix: fix garbage collect vs MSG_PEEK
commit cbcf011 upstream. unix_gc() assumes that candidate sockets can never gain an external reference (i.e. be installed into an fd) while the unix_gc_lock is held. Except for MSG_PEEK this is guaranteed by modifying inflight count under the unix_gc_lock. MSG_PEEK does not touch any variable protected by unix_gc_lock (file count is not), yet it needs to be serialized with garbage collection. Do this by locking/unlocking unix_gc_lock: 1) increment file count 2) lock/unlock barrier to make sure incremented file count is visible to garbage collection 3) install file into fd This is a lock barrier (unlike smp_mb()) that ensures that garbage collection is run completely before or completely after the barrier. Cc: <stable@vger.kernel.org> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent dee8119 commit 93c5951

1 file changed

Lines changed: 49 additions & 2 deletions

File tree

‎net/unix/af_unix.c‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,53 @@ static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
15211521
return err;
15221522
}
15231523

1524+
static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)
1525+
{
1526+
scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1527+
1528+
/*
1529+
* Garbage collection of unix sockets starts by selecting a set of
1530+
* candidate sockets which have reference only from being in flight
1531+
* (total_refs == inflight_refs). This condition is checked once during
1532+
* the candidate collection phase, and candidates are marked as such, so
1533+
* that non-candidates can later be ignored. While inflight_refs is
1534+
* protected by unix_gc_lock, total_refs (file count) is not, hence this
1535+
* is an instantaneous decision.
1536+
*
1537+
* Once a candidate, however, the socket must not be reinstalled into a
1538+
* file descriptor while the garbage collection is in progress.
1539+
*
1540+
* If the above conditions are met, then the directed graph of
1541+
* candidates (*) does not change while unix_gc_lock is held.
1542+
*
1543+
* Any operations that changes the file count through file descriptors
1544+
* (dup, close, sendmsg) does not change the graph since candidates are
1545+
* not installed in fds.
1546+
*
1547+
* Dequeing a candidate via recvmsg would install it into an fd, but
1548+
* that takes unix_gc_lock to decrement the inflight count, so it's
1549+
* serialized with garbage collection.
1550+
*
1551+
* MSG_PEEK is special in that it does not change the inflight count,
1552+
* yet does install the socket into an fd. The following lock/unlock
1553+
* pair is to ensure serialization with garbage collection. It must be
1554+
* done between incrementing the file count and installing the file into
1555+
* an fd.
1556+
*
1557+
* If garbage collection starts after the barrier provided by the
1558+
* lock/unlock, then it will see the elevated refcount and not mark this
1559+
* as a candidate. If a garbage collection is already in progress
1560+
* before the file count was incremented, then the lock/unlock pair will
1561+
* ensure that garbage collection is finished before progressing to
1562+
* installing the fd.
1563+
*
1564+
* (*) A -> B where B is on the queue of A or B is on the queue of C
1565+
* which is on the queue of listening socket A.
1566+
*/
1567+
spin_lock(&unix_gc_lock);
1568+
spin_unlock(&unix_gc_lock);
1569+
}
1570+
15241571
static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
15251572
{
15261573
int err = 0;
@@ -2170,7 +2217,7 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
21702217
sk_peek_offset_fwd(sk, size);
21712218

21722219
if (UNIXCB(skb).fp)
2173-
scm.fp = scm_fp_dup(UNIXCB(skb).fp);
2220+
unix_peek_fds(&scm, skb);
21742221
}
21752222
err = (flags & MSG_TRUNC) ? skb->len - skip : size;
21762223

@@ -2413,7 +2460,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
24132460
/* It is questionable, see note in unix_dgram_recvmsg.
24142461
*/
24152462
if (UNIXCB(skb).fp)
2416-
scm.fp = scm_fp_dup(UNIXCB(skb).fp);
2463+
unix_peek_fds(&scm, skb);
24172464

24182465
sk_peek_offset_fwd(sk, chunk);
24192466

0 commit comments

Comments
 (0)