Skip to content

Commit c7edc6e

Browse files
mmhalgregkh
authored andcommitted
af_unix: Fix garbage collection of embryos carrying OOB with SCM_RIGHTS
commit 041933a1ec7b4173a8e638cae4f8e394331d7e54 upstream. GC attempts to explicitly drop oob_skb's reference before purging the hit list. The problem is with embryos: kfree_skb(u->oob_skb) is never called on an embryo socket. The python script below [0] sends a listener's fd to its embryo as OOB data. While GC does collect the embryo's queue, it fails to drop the OOB skb's refcount. The skb which was in embryo's receive queue stays as unix_sk(sk)->oob_skb and keeps the listener's refcount [1]. Tell GC to dispose embryo's oob_skb. [0]: from array import array from socket import * addr = '\x00unix-oob' lis = socket(AF_UNIX, SOCK_STREAM) lis.bind(addr) lis.listen(1) s = socket(AF_UNIX, SOCK_STREAM) s.connect(addr) scm = (SOL_SOCKET, SCM_RIGHTS, array('i', [lis.fileno()])) s.sendmsg([b'x'], [scm], MSG_OOB) lis.close() [1] $ grep unix-oob /proc/net/unix $ ./unix-oob.py $ grep unix-oob /proc/net/unix 0000000000000000: 00000002 00000000 00000000 0001 02 0 @unix-oob 0000000000000000: 00000002 00000000 00010000 0001 01 6072 @unix-oob Fixes: 4090fa373f0e ("af_unix: Replace garbage collection algorithm.") Signed-off-by: Michal Luczaj <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: Lee Jones <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 28201f3 commit c7edc6e

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

net/unix/garbage.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ enum unix_recv_queue_lock_class {
342342
U_RECVQ_LOCK_EMBRYO,
343343
};
344344

345+
static void unix_collect_queue(struct unix_sock *u, struct sk_buff_head *hitlist)
346+
{
347+
skb_queue_splice_init(&u->sk.sk_receive_queue, hitlist);
348+
349+
#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
350+
if (u->oob_skb) {
351+
WARN_ON_ONCE(skb_unref(u->oob_skb));
352+
u->oob_skb = NULL;
353+
}
354+
#endif
355+
}
356+
345357
static void unix_collect_skb(struct list_head *scc, struct sk_buff_head *hitlist)
346358
{
347359
struct unix_vertex *vertex;
@@ -365,18 +377,11 @@ static void unix_collect_skb(struct list_head *scc, struct sk_buff_head *hitlist
365377

366378
/* listener -> embryo order, the inversion never happens. */
367379
spin_lock_nested(&embryo_queue->lock, U_RECVQ_LOCK_EMBRYO);
368-
skb_queue_splice_init(embryo_queue, hitlist);
380+
unix_collect_queue(unix_sk(skb->sk), hitlist);
369381
spin_unlock(&embryo_queue->lock);
370382
}
371383
} else {
372-
skb_queue_splice_init(queue, hitlist);
373-
374-
#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
375-
if (u->oob_skb) {
376-
kfree_skb(u->oob_skb);
377-
u->oob_skb = NULL;
378-
}
379-
#endif
384+
unix_collect_queue(u, hitlist);
380385
}
381386

382387
spin_unlock(&queue->lock);

0 commit comments

Comments
 (0)