* [PATCH net v3 1/2] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected().
2026-11-14 16:26 [PATCH net v3 0/2] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected() Siddharth Chintamaneni
@ 2026-11-14 16:26 ` Siddharth Chintamaneni
2026-11-14 16:26 ` [PATCH net v3 2/2] selftest: af_unix: Test scm_fds on dgram disconnect Siddharth Chintamaneni
1 sibling, 0 replies; 4+ messages in thread
From: Siddharth Chintamaneni @ 2026-11-14 16:26 UTC (permalink / raw)
To: Kuniyuki Iwashima, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Shuah Khan, Kirill Tkhai, apais, hargar, rlmenge,
egoriluk, netdev, linux-kselftest, linux-kernel,
Siddharth Chintamaneni
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: 3c32da19a858 ("unix: Show number of pending scm files of receive queue in fdinfo")
Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
---
net/unix/af_unix.c | 59 +++++++++++++++++++++++++++-------------------
1 file changed, 35 insertions(+), 24 deletions(-)
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 98fcfd7301f6..990a98330d9c 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -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);
@@ -2058,28 +2091,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);
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH net v3 2/2] selftest: af_unix: Test scm_fds on dgram disconnect.
2026-11-14 16:26 [PATCH net v3 0/2] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected() Siddharth Chintamaneni
2026-11-14 16:26 ` [PATCH net v3 1/2] " Siddharth Chintamaneni
@ 2026-11-14 16:26 ` Siddharth Chintamaneni
2026-11-14 16:49 ` Kuniyuki Iwashima
1 sibling, 1 reply; 4+ messages in thread
From: Siddharth Chintamaneni @ 2026-11-14 16:26 UTC (permalink / raw)
To: Kuniyuki Iwashima, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Shuah Khan, Kirill Tkhai, apais, hargar, rlmenge,
egoriluk, netdev, linux-kselftest, linux-kernel,
Siddharth Chintamaneni
Add dgram_disconnect_fdinfo in scm_rights.c verifying that scm_fds in
/proc/self/fdinfo/<fd> returns to 0 when a connected SOCK_DGRAM socket
with queued SCM_RIGHTS datagrams disconnects via connect(AF_UNSPEC) or
observes a dead peer in send().
Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
---
.../selftests/net/af_unix/scm_rights.c | 75 +++++++++++++++++++
1 file changed, 75 insertions(+)
diff --git a/tools/testing/selftests/net/af_unix/scm_rights.c b/tools/testing/selftests/net/af_unix/scm_rights.c
index c165f250220a..19aa26c60f3b 100644
--- a/tools/testing/selftests/net/af_unix/scm_rights.c
+++ b/tools/testing/selftests/net/af_unix/scm_rights.c
@@ -395,4 +395,79 @@ TEST_F(scm_rights, mixed_lowpoint)
close_sockets(6);
}
+static int count_scm_fds(struct __test_metadata *_metadata, int fd)
+{
+ char path[64], *line = NULL;
+ int scm_fds = -1, ret;
+ size_t unused = 0;
+ FILE *f;
+
+ snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
+
+ f = fopen(path, "r");
+ ASSERT_NE(NULL, f);
+
+ while (getline(&line, &unused, f) != -1) {
+ if (strncmp(line, "scm_fds:", 8))
+ continue;
+
+ ret = sscanf(line + 8, "%d", &scm_fds);
+ ASSERT_EQ(1, ret);
+
+ break;
+ }
+
+ free(line);
+
+ ret = fclose(f);
+ ASSERT_EQ(0, ret);
+
+ return scm_fds;
+}
+
+TEST(dgram_disconnect_fdinfo)
+{
+ const FIXTURE_VARIANT(scm_rights) variant = {
+ .name = "UNIX ",
+ .type = SOCK_DGRAM,
+ };
+ FIXTURE_DATA(scm_rights) self = {};
+ struct sockaddr addr = {
+ .sa_family = AF_UNSPEC,
+ };
+ int ret;
+
+ ret = unshare(CLONE_NEWNET);
+ ASSERT_EQ(0, ret);
+
+ create_socketpairs(_metadata, &self, &variant, 2);
+
+ /* 1. Disconnect via connect(AF_UNSPEC) */
+ __send_fd(_metadata, &self, &variant, 0, 0);
+ ASSERT_EQ(2, count_scm_fds(_metadata, self.fd[0]));
+
+ ret = connect(self.fd[0], &addr, sizeof(addr.sa_family));
+ ASSERT_EQ(0, ret);
+ ASSERT_EQ(0, count_scm_fds(_metadata, self.fd[0]));
+
+ /* 2. Dead-peer disconnect via send() */
+ __send_fd(_metadata, &self, &variant, 1, 1);
+ ASSERT_EQ(2, count_scm_fds(_metadata, self.fd[2]));
+
+ ret = close(self.fd[3]);
+ ASSERT_EQ(0, ret);
+
+ ret = send(self.fd[2], "x", 1, 0);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(ECONNREFUSED, errno);
+ ASSERT_EQ(0, count_scm_fds(_metadata, self.fd[2]));
+
+ ret = close(self.fd[0]);
+ ASSERT_EQ(0, ret);
+ ret = close(self.fd[1]);
+ ASSERT_EQ(0, ret);
+ ret = close(self.fd[2]);
+ ASSERT_EQ(0, ret);
+}
+
TEST_HARNESS_MAIN
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread