Thread View: linux.kernel.netdev
5 messages
5 total messages
Started by Siddharth Chintamaneni
Wed, 11 Nov 2026 22:51
[PATCH net v2 0/2] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected()
Author: Siddharth Chintamaneni
Date: Wed, 11 Nov 2026 22:51
Date: Wed, 11 Nov 2026 22:51
30 lines
12244 bytes
12244 bytes
This series fixes a u->scm_stat.nr_fds accounting leak in unix_dgram_disconnected() when purging queued skbs from sk->sk_receive_queue on disconnect, along with a selftest: - Patch 1 splices sk->sk_receive_queue onto a local list in unix_dgram_disconnected() and calls scm_stat_del() before freeing each purged skb. - Patch 2 adds dgram_disconnect_fdinfo to scm_rights.c verifying scm_fds in /proc/self/fdinfo/<fd> across connect(AF_UNSPEC) and dead-peer send() disconnects. Changes in v2: - Drop MSG_OOB patches and tests per Kuniyuki Iwashima. - Splice sk->sk_receive_queue onto a local list under the queue lock before freeing skbs so concurrent senders cannot extend the drain loop. - Make dgram_disconnect_fdinfo a standalone TEST() instead of running no-op fixture variants, and also test dead-peer send() disconnect. - Link to v1: https://lore.kernel.org/all/20261106051905.218638-1-sidchintamaneni@gmail.com/ Siddharth Chintamaneni (2): af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected(). selftest: af_unix: Test scm_fds on dgram disconnect. net/unix/af_unix.c | 59 ++++++++------ .../selftests/net/af_unix/scm_rights.c | 79 +++++++++++++++++++ 2 files changed, 114 insertions(+), 24 deletions(-) -- 2.43.0
[PATCH net v2 1/2] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected().
Author: Siddharth Chintamaneni
Date: Wed, 11 Nov 2026 22:51
Date: Wed, 11 Nov 2026 22:51
108 lines
14853 bytes
14853 bytes
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>
---
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
[PATCH net v2 2/2] selftest: af_unix: Test scm_fds on dgram disconnect.
Author: Siddharth Chintamaneni
Date: Wed, 11 Nov 2026 22:51
Date: Wed, 11 Nov 2026 22:51
101 lines
13831 bytes
13831 bytes
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 | 79 +++++++++++++++++++
1 file changed, 79 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..ced83d3173b7 100644
--- a/tools/testing/selftests/net/af_unix/scm_rights.c
+++ b/tools/testing/selftests/net/af_unix/scm_rights.c
@@ -395,4 +395,83 @@ 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);
+ ASSERT_EQ(0, count_sockets(_metadata, &variant));
+
+ 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);
+
+ sleep(1);
+ ASSERT_EQ(0, count_sockets(_metadata, &variant));
+}
+
TEST_HARNESS_MAIN
--
2.43.0
Re: [PATCH net v2 0/2] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected()
Author: netdev-bot+sinfo
Date: Wed, 11 Nov 2026 22:57
Date: Wed, 11 Nov 2026 22:57
18 lines
8677 bytes
8677 bytes
Hi! This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information: - How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan. Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then. The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Re: [PATCH net v2 2/2] selftest: af_unix: Test scm_fds on dgram disconnect.
Author: Siddharth Chintamaneni
Date: Sat, 14 Nov 2026 16:22
Date: Sat, 14 Nov 2026 16:22
120 lines
14299 bytes
14299 bytes
On Fri, 13 Nov 2026 at 23:53, Kuniyuki Iwashima <kuniyu@google.com> wrote:
>
> On Wed, 11 Nov 2026 22:51:48 +0000, Siddharth Chintamaneni wrote:
> > 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 | 79 +++++++++++++++++++
> > 1 file changed, 79 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..ced83d3173b7 100644
> > --- a/tools/testing/selftests/net/af_unix/scm_rights.c
> > +++ b/tools/testing/selftests/net/af_unix/scm_rights.c
> > @@ -395,4 +395,83 @@ 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);
> > + ASSERT_EQ(0, count_sockets(_metadata, &variant));
>
> nit: count_sockets() is not needed.
>
>
> > +
> > + 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);
> > +
> > + sleep(1);
> > + ASSERT_EQ(0, count_sockets(_metadata, &variant));
>
> Same, these two lines are unnecessary.
>
> sleep() and count_sockets() in FIXTURE_TEARDOWN(scm_rights)
> were to wait GC to purge cyclic references, but this test does
> not create that.
>
Thanks for the review! I'll drop both count_sockets() calls and re-spin in it.
>
> > +}
> > +
> > TEST_HARNESS_MAIN
> > --
> > 2.43.0
> >
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads