Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

af_unix: Initialise scc_index in unix_add_edge().

Quang Le reported that the AF_UNIX GC could garbage-collect a
receive queue of an alive in-flight socket, with a nice repro.

The repro consists of three stages.

1)
1-a. Create a single cyclic reference with many sockets
1-b. close() all sockets
1-c. Trigger GC

2)
2-a. Pass sk-A to an embryo sk-B
2-b. Pass sk-X to sk-X
2-c. Trigger GC

3)
3-a. accept() the embryo sk-B
3-b. Pass sk-B to sk-C
3-c. close() the in-flight sk-A
3-d. Trigger GC

As of 2-c, sk-A and sk-X are linked to unix_unvisited_vertices,
and unix_walk_scc() groups them into two different SCCs:

unix_sk(sk-A)->vertex->scc_index = 2 (UNIX_VERTEX_INDEX_START)
unix_sk(sk-X)->vertex->scc_index = 3

Once GC completes, unix_graph_grouped is set to true.
Also, unix_graph_maybe_cyclic is set to true due to sk-X's
cyclic self-reference, which makes close() trigger GC.

At 3-b, unix_add_edge() allocates unix_sk(sk-B)->vertex and
links it to unix_unvisited_vertices.

unix_update_graph() is called at 3-a. and 3-b., but neither
unix_graph_grouped nor unix_graph_maybe_cyclic is changed
because both sk-B's listener and sk-C are not in-flight.

3-c decrements sk-A's file refcnt to 1.

Since unix_graph_grouped is true at 3-d, unix_walk_scc_fast()
is finally called and iterates 3 sockets sk-A, sk-B, and sk-X:

sk-A -> sk-B (-> sk-C)
sk-X -> sk-X

This is totally fine. All of them are not yet close()d and
should be grouped into different SCCs.

However, unix_vertex_dead() misjudges that sk-A and sk-B are
in the same SCC and sk-A is dead.

unix_sk(sk-A)->scc_index == unix_sk(sk-B)->scc_index <-- Wrong!
&&
sk-A's file refcnt == unix_sk(sk-A)->vertex->out_degree
^-- 1 in-flight count for sk-B
-> sk-A is dead !?

The problem is that unix_add_edge() does not initialise scc_index.

Stage 1) is used for heap spraying, making a newly allocated
vertex have vertex->scc_index == 2 (UNIX_VERTEX_INDEX_START)
set by unix_walk_scc() at 1-c.

Let's track the max SCC index from the previous unix_walk_scc()
call and assign the max + 1 to a new vertex's scc_index.

This way, we can continue to avoid Tarjan's algorithm while
preventing misjudgments.

Fixes: ad081928a8b0 ("af_unix: Avoid Tarjan's algorithm if unnecessary.")
Reported-by: Quang Le <quanglex97@gmail.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://patch.msgid.link/20251109025233.3659187-1-kuniyu@google.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Kuniyuki Iwashima and committed by
Paolo Abeni
60e6489f 8c0726e8

+11 -3
+11 -3
net/unix/garbage.c
··· 145 145 }; 146 146 147 147 static unsigned long unix_vertex_unvisited_index = UNIX_VERTEX_INDEX_MARK1; 148 + static unsigned long unix_vertex_max_scc_index = UNIX_VERTEX_INDEX_START; 148 149 149 150 static void unix_add_edge(struct scm_fp_list *fpl, struct unix_edge *edge) 150 151 { ··· 154 153 if (!vertex) { 155 154 vertex = list_first_entry(&fpl->vertices, typeof(*vertex), entry); 156 155 vertex->index = unix_vertex_unvisited_index; 156 + vertex->scc_index = ++unix_vertex_max_scc_index; 157 157 vertex->out_degree = 0; 158 158 INIT_LIST_HEAD(&vertex->edges); 159 159 INIT_LIST_HEAD(&vertex->scc_entry); ··· 491 489 scc_dead = unix_vertex_dead(v); 492 490 } 493 491 494 - if (scc_dead) 492 + if (scc_dead) { 495 493 unix_collect_skb(&scc, hitlist); 496 - else if (!unix_graph_maybe_cyclic) 497 - unix_graph_maybe_cyclic = unix_scc_cyclic(&scc); 494 + } else { 495 + if (unix_vertex_max_scc_index < vertex->scc_index) 496 + unix_vertex_max_scc_index = vertex->scc_index; 497 + 498 + if (!unix_graph_maybe_cyclic) 499 + unix_graph_maybe_cyclic = unix_scc_cyclic(&scc); 500 + } 498 501 499 502 list_del(&scc); 500 503 } ··· 514 507 unsigned long last_index = UNIX_VERTEX_INDEX_START; 515 508 516 509 unix_graph_maybe_cyclic = false; 510 + unix_vertex_max_scc_index = UNIX_VERTEX_INDEX_START; 517 511 518 512 /* Visit every vertex exactly once. 519 513 * __unix_walk_scc() moves visited vertices to unix_visited_vertices.