Skip to content

Commit dcf70df

Browse files
q2venkuba-moo
authored andcommitted
af_unix: Fix up unix_edge.successor for embryo socket.
To garbage collect inflight AF_UNIX sockets, we must define the cyclic reference appropriately. This is a bit tricky if the loop consists of embryo sockets. Suppose that the fd of AF_UNIX socket A is passed to D and the fd B to C and that C and D are embryo sockets of A and B, respectively. It may appear that there are two separate graphs, A (-> D) and B (-> C), but this is not correct. A --. .-- B X C <-' `-> D Now, D holds A's refcount, and C has B's refcount, so unix_release() will never be called for A and B when we close() them. However, no one can call close() for D and C to free skbs holding refcounts of A and B because C/D is in A/B's receive queue, which should have been purged by unix_release() for A and B. So, here's another type of cyclic reference. When a fd of an AF_UNIX socket is passed to an embryo socket, the reference is indirectly held by its parent listening socket. .-> A .-> B | `- sk_receive_queue | `- sk_receive_queue | `- skb | `- skb | `- sk == C | `- sk == D | `- sk_receive_queue | `- sk_receive_queue | `- skb +---------' `- skb +-. | | `---------------------------------------------------------' Technically, the graph must be denoted as A <-> B instead of A (-> D) and B (-> C) to find such a cyclic reference without touching each socket's receive queue. .-> A --. .-- B <-. | X | == A <-> B `-- C <-' `-> D --' We apply this fixup during GC by fetching the real successor by unix_edge_successor(). When we call accept(), we clear unix_sock.listener under unix_gc_lock not to confuse GC. Signed-off-by: Kuniyuki Iwashima <[email protected]> Acked-by: Paolo Abeni <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent aed6ece commit dcf70df

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

include/net/af_unix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void unix_inflight(struct user_struct *user, struct file *fp);
2424
void unix_notinflight(struct user_struct *user, struct file *fp);
2525
void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver);
2626
void unix_del_edges(struct scm_fp_list *fpl);
27+
void unix_update_edges(struct unix_sock *receiver);
2728
int unix_prepare_fpl(struct scm_fp_list *fpl);
2829
void unix_destroy_fpl(struct scm_fp_list *fpl);
2930
void unix_gc(void);

net/unix/af_unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ static int unix_accept(struct socket *sock, struct socket *newsock, int flags,
17211721
}
17221722

17231723
tsk = skb->sk;
1724-
unix_sk(tsk)->listener = NULL;
1724+
unix_update_edges(unix_sk(tsk));
17251725
skb_free_datagram(sk, skb);
17261726
wake_up_interruptible(&unix_sk(sk)->peer_wait);
17271727

net/unix/garbage.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ struct unix_sock *unix_get_socket(struct file *filp)
101101
return NULL;
102102
}
103103

104+
static struct unix_vertex *unix_edge_successor(struct unix_edge *edge)
105+
{
106+
/* If an embryo socket has a fd,
107+
* the listener indirectly holds the fd's refcnt.
108+
*/
109+
if (edge->successor->listener)
110+
return unix_sk(edge->successor->listener)->vertex;
111+
112+
return edge->successor->vertex;
113+
}
114+
104115
static LIST_HEAD(unix_unvisited_vertices);
105116

106117
enum unix_vertex_index {
@@ -209,6 +220,13 @@ void unix_del_edges(struct scm_fp_list *fpl)
209220
fpl->inflight = false;
210221
}
211222

223+
void unix_update_edges(struct unix_sock *receiver)
224+
{
225+
spin_lock(&unix_gc_lock);
226+
receiver->listener = NULL;
227+
spin_unlock(&unix_gc_lock);
228+
}
229+
212230
int unix_prepare_fpl(struct scm_fp_list *fpl)
213231
{
214232
struct unix_vertex *vertex;
@@ -268,7 +286,7 @@ static void __unix_walk_scc(struct unix_vertex *vertex)
268286

269287
/* Explore neighbour vertices (receivers of the current vertex's fd). */
270288
list_for_each_entry(edge, &vertex->edges, vertex_entry) {
271-
struct unix_vertex *next_vertex = edge->successor->vertex;
289+
struct unix_vertex *next_vertex = unix_edge_successor(edge);
272290

273291
if (!next_vertex)
274292
continue;

0 commit comments

Comments
 (0)