Skip to contentSkip to conversation

af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected()

PATCH Superseded Siddharth Chintamaneni 3 participants 7 messages Nov 11 – Nov 14

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected()

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/[email protected]/

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(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 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 3c32da19a858 ("unix: Show number of pending scm files of receive queue in fdinfo")
---
 net/unix/af_unix.c | 59 +++++++++++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 24 deletions(-)
net/unix/af_unix.c +35 -24
index 98fcfd7301f6..990a98330d9c 100644
@@ -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);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH net v2 1/2] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected().

Quoted 11 lines · 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.

Quoted 6 lines

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")
#3 lore raw

PATCH v2 2/2 selftest: af_unix: Test scm_fds on dgram disconnect.

In reply to 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().

---
 .../selftests/net/af_unix/scm_rights.c        | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)
tools/testing/selftests/net/af_unix/scm_rights.c +79 -0
index c165f250220a..ced83d3173b7 100644
@@ -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
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH net v2 2/2] selftest: af_unix: Test scm_fds on dgram disconnect.

Quoted 53 lines · 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().

---
 .../selftests/net/af_unix/scm_rights.c        | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)
tools/testing/selftests/net/af_unix/scm_rights.c +35 -0
index c165f250220a..ced83d3173b7 100644
@@ -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,

tools/testing/selftests/net/af_unix/scm_rights.c TEST_F(scm_rights, mixed_lowpoint) · line 442

Quoted 10 lines
tools/testing/selftests/net/af_unix/scm_rights.c +10 -0
+	};
+	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.

Quoted 22 lines
tools/testing/selftests/net/af_unix/scm_rights.c +22 -0
+
+	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]));

tools/testing/selftests/net/af_unix/scm_rights.c TEST_F(scm_rights, mixed_lowpoint) · line 474

Quoted 10 lines
tools/testing/selftests/net/af_unix/scm_rights.c +10 -0
+
+	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.

Quoted 6 lines
tools/testing/selftests/net/af_unix/scm_rights.c +2 -0
+}
+
 TEST_HARNESS_MAIN
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH net v2 2/2] selftest: af_unix: Test scm_fds on dgram disconnect.

Quoted 104 lines · Kuniyuki Iwashima
Quoted 53 lines · 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().

---
 .../selftests/net/af_unix/scm_rights.c        | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)
tools/testing/selftests/net/af_unix/scm_rights.c +35 -0
index c165f250220a..ced83d3173b7 100644
@@ -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,

tools/testing/selftests/net/af_unix/scm_rights.c TEST_F(scm_rights, mixed_lowpoint) · line 442

Quoted 10 lines
tools/testing/selftests/net/af_unix/scm_rights.c +10 -0
+     };
+     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.

Quoted 22 lines
tools/testing/selftests/net/af_unix/scm_rights.c +22 -0
+
+     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]));

tools/testing/selftests/net/af_unix/scm_rights.c TEST_F(scm_rights, mixed_lowpoint) · line 474

Quoted 10 lines
tools/testing/selftests/net/af_unix/scm_rights.c +10 -0
+
+     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.

Quoted 4 lines

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.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/net/af_unix/scm_rights.c +2 -1
+}
+
 TEST_HARNESS_MAIN
--

2.43.0

#6 lore raw
In reply to Siddharth Chintamaneni
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

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.