🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #17942

[PATCH net 1/2] net: don't leak pidfds through a short SO_PEERPIDFD optlen

#17942
From: Christian Brauner
Date: Tue, 17 Nov 2026 15:25
53 lines
10574 bytes
sk_getsockopt_peerpidfd() caps the caller's optlen at sizeof(int) but
never checks that it is at least that large. sk_getsockopt() only
rejects a negative optlen. So with an optlen of 0 to 3 we still
allocate a pidfd via pidfd_prepare(), copy zero to three bytes of the
fd number to userspace, write the short optlen back and then
fd_install() the pidfd anyway.

getsockopt() reports success but the caller only got part of the fd
number or none of it at all. It has no sane way to find out which fd it
has to close and the pidfd stays in its file descriptor table until the
process exits. Calling it in a loop fills up the table.

scm_pidfd_recv() deals with the same problem for SCM_PIDFD. Since
put_cmsg() doesn't fail on truncation it checks the size of the control
buffer itself and sets MSG_CTRUNC before it allocates a pidfd.

There's no point in handing out part of a file descriptor number. So
fail with EINVAL if optlen is smaller than sizeof(int) before we
prepare a pidfd. Since commit e10899b2a098 ("net: add
SO_PEERPIDFD_THREAD to get a thread-specific pidfd") SO_PEERPIDFD and
SO_PEERPIDFD_THREAD share the helper and the latter inherited the bug,
so this fixes both.

Fixes: 7b26952a91cf ("net: core: add getsockopt SO_PEERPIDFD")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-205984@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 net/core/sock.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/core/sock.c b/net/core/sock.c
index ad414878dc5ba..74a00a5e2d116 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -1746,8 +1746,10 @@ static int sk_getsockopt_peerpidfd(struct sock *sk, enum pid_type type,
 	struct pid *peer_pid;
 	int pidfd;
 
-	if (len > sizeof(pidfd))
-		len = sizeof(pidfd);
+	/* Userspace can't close a pidfd whose number it didn't get. */
+	if (len < sizeof(pidfd))
+		return -EINVAL;
+	len = sizeof(pidfd);
 
 	spin_lock(&sk->sk_peer_lock);
 	peer_pid = get_pid(sk->sk_peer_pid[type]);

-- 
2.53.0
Technical Details
Message-ID: <20261117-work-net-peerpidfd_short_optlen-v1-1-a93f56288501@kernel.org>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-365392-linux-kernel-netdev=dewy.ru
References: <20261117-work-net-peerpidfd_short_optlen-v1-0-a93f56288501@kernel.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-365392-linux-kernel-netdev=dewy.ru
From: brauner@kernel.org (Christian Brauner)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net 1/2] net: don't leak pidfds through a short
 SO_PEERPIDFD optlen
Date: Tue, 17 Nov 2026 16:25:30 +0100
Organization: Usenet Network
Message-ID: <20261117-work-net-peerpidfd_short_optlen-v1-1-a93f56288501@kernel.org>
References: <20261117-work-net-peerpidfd_short_optlen-v1-0-a93f56288501@kernel.org>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
	logging-data="2364160"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:JAh4L2tr7hB7SFloXjYZdarxMSs= sha256:KA9DJPSkhaUFj1AIy7tF9mq5V3WO2wFP1SXE0EaXNT0=
	sha1:pFVfa3WNNE0NurpyaznD+Z/0U0w= sha256:c+pho71CMHA+5D+u9Hnq7mbfoVkYxP2yh9N9WZ7u0MQ=
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 03298114FC0; Tue, 17 Nov 2026 18:26:40 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	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 (2048-bit key)
	header.d=kernel.org
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 d8XAUO7oaCcS;
	Tue, 17 Nov 2026 18:26:34 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-365392-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 3559C114F1A; Tue, 17 Nov 2026 18:26:25 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id E25B03F2B9; Tue, 17 Nov 2026 15:26:06 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1794929166;
	b=UmL/sxWrGsGbr5nZUB0y/6G2ax9d6R60FX4hL30VoA5Hshnn0s4r5+2up4143j8BcpUt
	 QqaNaMCU9ax5OyFQAhRQgZCcVPt1qtZCM6ma+pQme/xIFejUnwgGvo0JqHAhO8S+wx27B
	 coQxMKUDQRpIwAm5TQTT+C+Uk/S1jGXHQrFOkxIjW8ryzu53rzU7Yc3qZHOOqwlANxFST
	 ZtkYiZq5rxsLnqX7pg9m8Exdxju7vi1P9WUVrHf/yFbulCAB3tbqxnOnDM70Cj13hrghE
	 P6HuMN5pDLvfU2QKCrY1ayM3nL59bnCpIvRw8X3tdIjviMwICDQVJtH843YCpHhmnFg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794929166;
	h=X-Original-To:Received:Received:DKIM-Signature:From:Date:Subject:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type:
	 Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc:
	 X-Mailer:X-Developer-Signature:X-Developer-Key;
	bh=oBpg59OO1kMaPB0ujrex8ItcYhjsVpQfat0sQyio4SI=;
	b=hBAdm1UCdclTH3szL47jky9tZDQLwM6o/NCNgHUx7nOCbWAseCy8Lyacmx3ZpDXZfG/2
	 njW1bgo9zkHdLsFMq00HyaEl7eMCio1zPAEKkl5gvKYovjuppUO5J4cTSbuKKkXl4fogq
	 uAJKrFr70SDyl9hC3KB8KCL/2MFjK4XIWkJzC89bS7noyxU7qJKhYmKI+I4ORE7HTPpT/
	 mWrU5nJ8IMsCxLUUBxAMX47u1sMWzRyxGTDv4zVQ1nRQEM0jA2Q9YirEpmhEqP6Fk/z2z
	 Aucax64jsAyjgBki4AA3Jrmxle894wM8qCIJNdEoIwPQnbl0Icfd49DJakiQOvPO9jQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Vy1t2YjG;
	arc=pass header.oldest-pass=0 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 B8FBA606A31C;
	Tue, 17 Nov 2026 15:26:06 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Vy1t2YjG
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794929165;
	b=Ez20FqcuxPJN2gvgcWl+ZzB+46PIOQP0btNgv/xAqkQBaVSpr7Sj/4igqRs+CGaGucDv
	 o0OkrsCY4+wUsKZeCh3OBDrO9wDpvdTT6jBgugGcjcNlj0LTTz9dRqc4uOme0AxgYTiAz
	 GxKM2iJ2UeQ6rBaZolvabBi6oPKR4c0F3+SLEDNp02riHP+GyrjLAg3TftkMdFvLGalrR
	 mS1Fu4Q5PaPXbzWAyrbUz6A6RcbqX6yXSGgbr2diEV3OcPX9yVCWQa1987q6mMQ02DGLJ
	 VXxKchx/OvAXbbbr+byhpEC+oGoECZunl3OK4RDZKclC+wJfE0QqXTCzHYWCYi+jIwQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794929165;
	h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type:
	 Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc:
	 X-Mailer:X-Developer-Signature:X-Developer-Key;
	bh=oBpg59OO1kMaPB0ujrex8ItcYhjsVpQfat0sQyio4SI=;
	b=XpWGazBpRz7yxwjyeYrfPFuxXaj+0MO61lxpYuPx5YcRg5wFN1wFkHywrn9uEBC3vKPB
	 LhX0asEYAIyyZ9R1pRQhOtlv9mGrxxy5RqbTyzCj2qJmdAAvBgUjVyi8cNUQ6w+J+HAA0
	 csNd1pgTV5Yw70GRM5IEaZfjkm7unZlYcobWcdSl4CqUkSK0g3eG+2o7m7oaOuN0kwyZe
	 WaxlgxKuBsmQ8CwEXN7TGW2yAJTHb4zV90UFpJC80oq2jQ295/U4jgiegJyqbNkJi1Ltl
	 bZ7Ez/w90JSY0I8poctJ0wq5OcBIsXFu4Tmw+dvEzFpH5oN+MeqIlJvvTCe2uInPtqQ==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Vy1t2YjG;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id D80C8606A32C;
	Tue, 17 Nov 2026 15:26:05 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EF261F000C9;
	Tue, 17 Nov 2026 15:26:03 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1794929165;
	bh=oBpg59OO1kMaPB0ujrex8ItcYhjsVpQfat0sQyio4SI=;
	h=From:Date:Subject:References:In-Reply-To:To:Cc;
	b=Vy1t2YjGzqjMZcntV+xbquzX3R4FdjbRMSfzcUGsUeNgkG7wk7PUkAsweDZ0zo/59
	 Cf2X/Q/htUL1IPqC+zOEJH/aMCFoxvWjSnRyfMj+4yMInyoMpkuU1rcSd63up3Dpkl
	 myR2OocoagQ8P8Mlc5ZFyteAhh2LXSBBN+tpGQ+l7Hbwt9wXjtGP3WWKTXXjovywHS
	 nXVecMUqEh7LwGbrISLYB1UTK9GoBxH5WX2mP+1ujYb4B4o20fDQdH5wx59kRF0Fbf
	 fWQ4HsguvCi5jo5n+6JUIomIWOnH7TuEa5Vma7b4yPC3Sg0Hyf0f14mMSUCy1nOwW0
	 zM+vetAB/JAPg==
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
X-In-Reply-To: <20261117-work-net-peerpidfd_short_optlen-v1-0-a93f56288501@kernel.org>
X-Cc: Simon Horman <horms@kernel.org>, Kuniyuki Iwashima <kuniyu@google.com>, 
 Alexander Mikhalitsyn <alexander@mihalicyn.com>, netdev@vger.kernel.org, 
 linux-fsdevel@vger.kernel.org, 
 "Christian Brauner (Amutable)" <brauner@kernel.org>, stable@vger.kernel.org, 
 Sashiko <sashiko-bot@kernel.org>
X-Mailer: b4 0.17-dev-04696
X-Developer-Signature: v=1; a=openpgp-sha256; l=2132; i=brauner@kernel.org;
 h=from:subject:message-id; bh=tExZvwBhUmA3JsuAEU6JO2rM1IjNALcWkfhx6o28kmk=;
 b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWT9KWJWdshoOXrrpvJclkPvQrx6xZdGbXa9vEHMsLFoV
 ZCw7YrZHaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABNJzmdkeDOrorNC+LnnZIPz
 KpPmaYplyDz4y3O3IntG9sf/YvMOezMy9F4yW7nzzZJr/2a2F7B2v7n6MOGe2fl5jy1PZHqvf+U
 fxAEA
X-Developer-Key: i=brauner@kernel.org; a=openpgp;
 fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624