Skip to content

Commit cbcf011

Browse files
Miklos Szereditorvalds
authored andcommitted
af_unix: fix garbage collect vs MSG_PEEK
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: <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Miklos Szeredi <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7d54999 commit cbcf011

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

net/unix/af_unix.c

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

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

21772224
if (UNIXCB(skb).fp)
2178-
scm.fp = scm_fp_dup(UNIXCB(skb).fp);
2225+
unix_peek_fds(&scm, skb);
21792226
}
21802227
err = (flags & MSG_TRUNC) ? skb->len - skip : size;
21812228

@@ -2418,7 +2465,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
24182465
/* It is questionable, see note in unix_dgram_recvmsg.
24192466
*/
24202467
if (UNIXCB(skb).fp)
2421-
scm.fp = scm_fp_dup(UNIXCB(skb).fp);
2468+
unix_peek_fds(&scm, skb);
24222469

24232470
sk_peek_offset_fwd(sk, chunk);
24242471

0 commit comments

Comments
 (0)