🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #9653

[PATCH net 2/2] connector/cn_proc: release socket state and listener count on close

#9653
From: "Cen Zhang (Microsoft)"
Date: Mon, 02 Nov 2026 13:09
122 lines
12412 bytes
cn_release() is the netlink release callback for NETLINK_CONNECTOR
sockets. It frees the per-socket proc_input only if the socket is a
member of the CN_IDX_PROC multicast group, and it never touches
proc_event_num_listeners. Both are wrong: cn_proc_mcast_ctl() allocates
proc_input for any socket that sends a control message, and a socket
that is still in LISTEN state when it closes has incremented the count
and never decremented it.

This has two consequences. A socket that sends a control message
without joining the group leaks its proc_input on close; an
unprivileged user looping over socket(), one control message and
close() leaks 8 bytes of kmalloc-8 per iteration that no process exit
gives back, until the OOM killer fires:

  Out of memory: Killed process 93 (trigger) total-vm:1016kB, ... UID:1000

And a listener that exits without sending IGNORE leaves the count too
high, so the kernel keeps building process events that nobody receives.

Fix by moving the cleanup into cn_proc.c and running it for every
socket on close: free proc_input if present, and decrement the count if
the socket was still in LISTEN state.

Fixes: 2aa1f7a1f47c ("connector/cn_proc: Add filtering to fix some bugs")
Cc: stable@vger.kernel.org
Cc: AutonomousCodeSecurity@microsoft.com
Signed-off-by: Cen Zhang (Microsoft) <cenzhang@linux.microsoft.com>
---
The bug was found and this fix was developed with the assistance of an
LLM-based code analysis tool. The analysis, the patch and its testing
were reviewed and verified by the author.

 drivers/connector/cn_proc.c   | 14 ++++++++++++++
 drivers/connector/connector.c |  7 +++----
 include/linux/cn_proc.h       |  6 ++++++
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/drivers/connector/cn_proc.c b/drivers/connector/cn_proc.c
index 4f613c0648ae..a4b62998095e 100644
--- a/drivers/connector/cn_proc.c
+++ b/drivers/connector/cn_proc.c
@@ -387,6 +387,20 @@ static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
 	send_msg(msg);
 }
 
+/* Called when a NETLINK_CONNECTOR socket is released. */
+void cn_proc_release(struct sock *sk)
+{
+	struct proc_input *input = sk->sk_user_data;
+
+	if (!input)
+		return;
+
+	if (input->mcast_op == PROC_CN_MCAST_LISTEN)
+		atomic_dec(&proc_event_num_listeners);
+	kfree(input);
+	sk->sk_user_data = NULL;
+}
+
 /**
  * cn_proc_mcast_ctl
  * @msg: message sent from userspace via the connector
diff --git a/drivers/connector/connector.c b/drivers/connector/connector.c
index 4028e8eeba82..f31ee8962f7c 100644
--- a/drivers/connector/connector.c
+++ b/drivers/connector/connector.c
@@ -14,6 +14,7 @@
 #include <net/netlink.h>
 #include <linux/moduleparam.h>
 #include <linux/connector.h>
+#include <linux/cn_proc.h>
 #include <linux/slab.h>
 #include <linux/mutex.h>
 #include <linux/proc_fs.h>
@@ -182,12 +183,10 @@ static int cn_bind(struct net *net, int group)
 	return -EPERM;
 }
 
+/* sk_user_data of a connector socket belongs to cn_proc. */
 static void cn_release(struct sock *sk, unsigned long *groups)
 {
-	if (groups && test_bit(CN_IDX_PROC - 1, groups)) {
-		kfree(sk->sk_user_data);
-		sk->sk_user_data = NULL;
-	}
+	cn_proc_release(sk);
 }
 
 /*
diff --git a/include/linux/cn_proc.h b/include/linux/cn_proc.h
index 1d5b02a96c46..8de921d731ce 100644
--- a/include/linux/cn_proc.h
+++ b/include/linux/cn_proc.h
@@ -19,6 +19,8 @@
 
 #include <uapi/linux/cn_proc.h>
 
+struct sock;
+
 #ifdef CONFIG_PROC_EVENTS
 void proc_fork_connector(struct task_struct *task);
 void proc_exec_connector(struct task_struct *task);
@@ -28,6 +30,7 @@ void proc_ptrace_connector(struct task_struct *task, int which_id);
 void proc_comm_connector(struct task_struct *task);
 void proc_coredump_connector(struct task_struct *task);
 void proc_exit_connector(struct task_struct *task);
+void cn_proc_release(struct sock *sk);
 #else
 static inline void proc_fork_connector(struct task_struct *task)
 {}
@@ -54,5 +57,8 @@ static inline void proc_coredump_connector(struct task_struct *task)
 
 static inline void proc_exit_connector(struct task_struct *task)
 {}
+
+static inline void cn_proc_release(struct sock *sk)
+{}
 #endif	/* CONFIG_PROC_EVENTS */
 #endif	/* CN_PROC_H */
-- 
2.55.0
Technical Details
Message-ID: <20261102130922.75629-3-cenzhang@linux.microsoft.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354688-linux-kernel-netdev=dewy.ru
References: <20261102130922.75629-1-cenzhang@linux.microsoft.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354688-linux-kernel-netdev=dewy.ru
From: cenzhang@linux.microsoft.com ("Cen Zhang (Microsoft)")
Newsgroups: linux.kernel.netdev
Subject: [PATCH net 2/2] connector/cn_proc: release socket state and listener count on close
Date: Mon,  2 Nov 2026 08:09:22 -0500
Organization: Usenet Network
Message-ID: <20261102130922.75629-3-cenzhang@linux.microsoft.com>
References: <20261102130922.75629-1-cenzhang@linux.microsoft.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2934232"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:EYpec0gVRw2I9mTXnQkqRfjocs8= sha256:9D4WjI9hbadmozcbJt0asa/i5a5DZ+Nkb1XQCLngaRw=
	sha1:QJUA4diA6kputYIYxUUXaP8sWAo= sha256:mz3JuOTlMEHa5CDz6XYeOcCY6jImQeRN3QsIFYAhWMI=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 5132D112516; Mon,  2 Nov 2026 16:48:33 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.051
X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=linux.microsoft.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id eqBVvepdTRQk;
	Mon,  2 Nov 2026 16:48:30 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-354688-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.microsoft.com
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id B65311124A1; Mon,  2 Nov 2026 16:48:20 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id AE7213039E; Mon,  2 Nov 2026 13:10:11 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=fail;
	t=1793625002;
	b=BybK/OgE7s5O4lUoCR1NNATPkggFyqIOlijKDGDqF9tRWjeJ4i9X3yl4vgdyAXMKc4oR
	 AUC3HfwinrnKATjxsZGHMiphLnzne/VzVbMsZWjtqUyS67LbddlGk4KZhzDUkk1Wom3wH
	 +h0j8ypG6j9Xbk3hkVmuSsPyN+0LJgF4EHW9EwaCKla78p2kUy6+BLKhoQFgb9hOz5NWy
	 +nV21tyvkrtOvlr769hfSdKHdYPm+R/XYahqhcX8Y93ApBLiSi51GDm3/CbZnKvscz7ie
	 Gbe9CNgEvrFyLdrKW8y2fm4RT4IPeJwydRHQsVTXDYAWjxLk26x5VCb5CBbV0Wnrpeg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793625002;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Filter:
	 DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 In-Reply-To:References:X-Mailing-List:List-Id:List-Post:List-Help:
	 List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=GHqMw/q7TleuNfGocIGHyAhvtp87XaBBGPNbbQljPFA=;
	b=qpZaUkSJb+dtQsWcBGWcpzgqqy2u3EhTK12dltPYXG0duWrbJCWA97e3OEyRDwY9/xAa
	 bmlClssIWqgAZEdfIWAZ8kwFECQlD/ziBFvD7XsR6Uj7/OBPQGVffWbHp1DUMTq8nvwT9
	 voHsZ/3RFPTbzog2XeL5bd+gt+amLEem46ysn/DjNso0CvrKDfijQwy3j+XPXGDvf/iRR
	 rlKHMLSE936Vu5FU2gSfJJeFt4MiFzAn769oG0kQ6SvDLnqrDmYhpEWW1EJIJU2F7hV46
	 0yJa57lXNELA1G1q06pufjVjFdnBhuZVTbB4Y66eCBqIudasIptsLBaEzhRYaxOFunQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=linux.microsoft.com header.i=@linux.microsoft.com header.a=rsa-sha256 header.s=default header.b=hkDw8QnB;
	dmarc=pass header.from=linux.microsoft.com;
	spf=pass smtp.mailfrom=linux.microsoft.com;
	arc=fail smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id B8DB860A2600;
	Mon,  2 Nov 2026 13:10:02 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key, unprotected) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.a=rsa-sha256 header.s=default header.b=hkDw8QnB
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=13.77.154.182
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793625001;
	b=tQXQEgMLubmRgC4+NJDbVQkijIE2tRSsw41Szcm1X+UTyt2PcqzL73tx9yXboK8epWWn
	 8BsxK5CHEKMCjLPGMdeovyRCKnQ+jcY+Z5X8JMsNCihsKW1B5+RQ2VQ6vKlzBefBPdEwm
	 +4bvLQwXuKP+wy+nChCMzdXTH1AuOrH0dr7DdTwBBjcVuYtmC7lmK29BQJ8Oyvx0y6K5Y
	 1J7bMJhgqNvX0+Zd39hR9s3f+9C4DPobgwBJA/3PGaqczTeFZSE+5AiY0O8IDHrpQH76n
	 jJHMTNWXo+Lv5UMtfPouGkxUPrC4N0klLUN5Ljpyujioq93+WbeL5+HUY9cO/m0KsYQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793625001;
	h=DMARC-Filter:Received:DKIM-Filter:DKIM-Signature:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding:X-Spam-Status:X-Spam-Checker-Version;
	bh=GHqMw/q7TleuNfGocIGHyAhvtp87XaBBGPNbbQljPFA=;
	b=yvTfTFHPTPWWog0olJUQkWF8OkD667aT8aJc1k3KfdsjXckYD8KMQugIQvj+dgOddQ3y
	 164VNYOlZY0g2gUmno3YiE+uDbrGXXNHFnEnoVm32IgrKka0LzUtMPrRlOgRIGLGx+esJ
	 ItFmzgpBjuaw0uiCNtbbhKGqhKtwvqz08QJxjzZ6dvGbv+KwhfRodm8cJVpB/5oNSW1MI
	 cEEKQy7XWgWnNzqbVcH7sNS7kwbRuAz2nnBsYH5FWvJVB0qirnlNVXJBejuEdFHdLfKgQ
	 b9yg9tO5XmyAf0ianSdPU+s9WKA6pxRKFAgqXzKmrE5xEntGGLziRCmy90k2cszuXxw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=linux.microsoft.com;
	spf=pass smtp.mailfrom=linux.microsoft.com;
	dkim=pass header.d=linux.microsoft.com header.i=@linux.microsoft.com header.a=rsa-sha256 header.s=default header.b=hkDw8QnB;
	arc=none smtp.remote-ip=13.77.154.182
X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 820FB606A327;
	Mon,  2 Nov 2026 13:10:01 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 820FB606A327
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com
X-Received: from mac.lan (unknown [52.172.102.253])
	by linux.microsoft.com (Postfix) with ESMTPSA id 6952520B7167;
	Mon,  2 Nov 2026 05:09:38 -0800 (PST)
X-DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6952520B7167
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1793624986;
	bh=GHqMw/q7TleuNfGocIGHyAhvtp87XaBBGPNbbQljPFA=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=hkDw8QnBTDt6E80Z5hdYd70uvNkEDFdNNuvZS+e8HxPlxvNNVdV54wxhkvPN/mMDg
	 hzcvyYjkda5LYWou8UhJUmE9gLzTEcFyAq/FMXxK2eFPQyff1C4gWhkOQhc0+XNPs2
	 RGZ1ZKwJoMHOKMkXgr0chE6tGdKr1/sUf7ZMGAQA=
X-Cc: Anjali Kulkarni <anjali.k.kulkarni@oracle.com>,
	"Liam R. Howlett" <liam@infradead.org>,
	netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	stable@vger.kernel.org,
	AutonomousCodeSecurity@microsoft.com,
	tgopinath@linux.microsoft.com,
	Cen Zhang <cenzhang@linux.microsoft.com>
X-Mailer: git-send-email 2.55.0
X-In-Reply-To: <20261102130922.75629-1-cenzhang@linux.microsoft.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list