Skip to content

Commit c49a157

Browse files
q2venkuba-moo
authored andcommitted
af_unix: Set drop reason in __unix_gc().
Inflight file descriptors by SCM_RIGHTS hold references to the struct file. AF_UNIX sockets could hold references to each other, forming reference cycles. Once such sockets are close()d without the fd recv()ed, they will be unaccessible from userspace but remain in kernel. __unix_gc() garbage-collects skb with the dead file descriptors and frees them by __skb_queue_purge(). Let's set SKB_DROP_REASON_SOCKET_CLOSE there. # echo 1 > /sys/kernel/tracing/events/skb/kfree_skb/enable # python3 >>> from socket import * >>> from array import array >>> >>> # Create a reference cycle >>> s1 = socket(AF_UNIX, SOCK_DGRAM) >>> s1.bind('') >>> s1.sendmsg([b"nop"], [(SOL_SOCKET, SCM_RIGHTS, array("i", [s1.fileno()]))], 0, s1.getsockname()) >>> s1.close() >>> >>> # Trigger GC >>> s2 = socket(AF_UNIX) >>> s2.close() # cat /sys/kernel/tracing/trace_pipe ... kworker/u16:2-42 ... kfree_skb: ... location=__unix_gc+0x4ad/0x580 reason: SOCKET_CLOSE Signed-off-by: Kuniyuki Iwashima <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 4d0446b commit c49a157

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

net/unix/garbage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ static void __unix_gc(struct work_struct *work)
573573
UNIXCB(skb).fp->dead = true;
574574
}
575575

576-
__skb_queue_purge(&hitlist);
576+
__skb_queue_purge_reason(&hitlist, SKB_DROP_REASON_SOCKET_CLOSE);
577577
skip_gc:
578578
WRITE_ONCE(gc_in_progress, false);
579579
}

0 commit comments

Comments
 (0)