mirror of
https://github.com/torvalds/linux.git
synced 2026-11-20 13:03:48 +00:00
af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected().
When a connected SOCK_DGRAM socket disconnects (e.g. via connect() with
AF_UNSPEC), reconnects to a different peer, or observes a dead peer in
unix_dgram_sendmsg(), unix_dgram_disconnected() purges all unread
datagrams from sk->sk_receive_queue via skb_queue_purge_reason().
Because skb_queue_purge_reason() frees each skb without calling
scm_stat_del(sk, skb), any SCM_RIGHTS file descriptors attached to the
purged skbs remain permanently accounted in u->scm_stat.nr_fds (reported
as scm_fds in /proc/<pid>/fdinfo/<fd>) for the remaining lifetime of the
socket.
Move scm_stat_add() and scm_stat_del() above unix_dgram_disconnected(),
splice sk->sk_receive_queue onto a local list under the queue lock, and
call scm_stat_del() before kfree_skb_reason() on each purged skb.
Fixes: 3c32da19a8 ("unix: Show number of pending scm files of receive queue in fdinfo")
Signed-off-by: Siddharth Chintamaneni <[email protected]>
Reviewed-by: Kuniyuki Iwashima <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Jakub Kicinski <[email protected]>
This commit is contained in:
committed by
Jakub Kicinski
parent
8a55761cec
commit
2c0d5e36aa
+35
-24
@@ -609,6 +609,28 @@ static void unix_write_space(struct sock *sk)
|
||||
rcu_read_unlock();
|
||||
}
|
||||
|
||||
static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
|
||||
{
|
||||
struct scm_fp_list *fp = UNIXCB(skb).fp;
|
||||
struct unix_sock *u = unix_sk(sk);
|
||||
|
||||
if (unlikely(fp && fp->count)) {
|
||||
atomic_add(fp->count, &u->scm_stat.nr_fds);
|
||||
unix_add_edges(fp, u);
|
||||
}
|
||||
}
|
||||
|
||||
static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
|
||||
{
|
||||
struct scm_fp_list *fp = UNIXCB(skb).fp;
|
||||
struct unix_sock *u = unix_sk(sk);
|
||||
|
||||
if (unlikely(fp && fp->count)) {
|
||||
atomic_sub(fp->count, &u->scm_stat.nr_fds);
|
||||
unix_del_edges(fp);
|
||||
}
|
||||
}
|
||||
|
||||
/* When dgram socket disconnects (or changes its peer), we clear its receive
|
||||
* queue of packets arrived from previous peer. First, it allows to do
|
||||
* flow control based only on wmem_alloc; second, sk connected to peer
|
||||
@@ -616,8 +638,19 @@ static void unix_write_space(struct sock *sk)
|
||||
static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
|
||||
{
|
||||
if (!skb_queue_empty(&sk->sk_receive_queue)) {
|
||||
skb_queue_purge_reason(&sk->sk_receive_queue,
|
||||
SKB_DROP_REASON_UNIX_DISCONNECT);
|
||||
struct sk_buff_head tmp;
|
||||
struct sk_buff *skb;
|
||||
|
||||
__skb_queue_head_init(&tmp);
|
||||
|
||||
spin_lock(&sk->sk_receive_queue.lock);
|
||||
skb_queue_splice_init(&sk->sk_receive_queue, &tmp);
|
||||
spin_unlock(&sk->sk_receive_queue.lock);
|
||||
|
||||
while ((skb = __skb_dequeue(&tmp)) != NULL) {
|
||||
scm_stat_del(sk, skb);
|
||||
kfree_skb_reason(skb, SKB_DROP_REASON_UNIX_DISCONNECT);
|
||||
}
|
||||
|
||||
wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
|
||||
|
||||
@@ -2059,28 +2092,6 @@ static bool unix_skb_scm_eq(const struct sock *sk, struct sk_buff *skb,
|
||||
unix_secdata_eq(scm, skb);
|
||||
}
|
||||
|
||||
static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
|
||||
{
|
||||
struct scm_fp_list *fp = UNIXCB(skb).fp;
|
||||
struct unix_sock *u = unix_sk(sk);
|
||||
|
||||
if (unlikely(fp && fp->count)) {
|
||||
atomic_add(fp->count, &u->scm_stat.nr_fds);
|
||||
unix_add_edges(fp, u);
|
||||
}
|
||||
}
|
||||
|
||||
static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
|
||||
{
|
||||
struct scm_fp_list *fp = UNIXCB(skb).fp;
|
||||
struct unix_sock *u = unix_sk(sk);
|
||||
|
||||
if (unlikely(fp && fp->count)) {
|
||||
atomic_sub(fp->count, &u->scm_stat.nr_fds);
|
||||
unix_del_edges(fp);
|
||||
}
|
||||
}
|
||||
|
||||
static void unix_orphan_scm(struct sock *sk, struct sk_buff *skb)
|
||||
{
|
||||
scm_stat_del(sk, skb);
|
||||
|
||||
Reference in New Issue
Block a user