From: Alexander Mikhalitsyn <alex@amutable.com>
To: Christian Brauner <brauner@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
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,
stable@vger.kernel.org, Sashiko <sashiko-bot@kernel.org>
Subject: Re: [PATCH net 1/2] net: don't leak pidfds through a short SO_PEERPIDFD optlen
Date: Tue, 17 Nov 2026 17:39:20 +0100 [thread overview]
Message-ID: <a0f9a7e10bee8b5d328de14e1bbdbf74f5712d43.camel@amutable.com> (raw)
In-Reply-To: <20261117-work-net-peerpidfd_short_optlen-v1-1-a93f56288501@kernel.org>
On Tue, 2026-11-17 at 16:25 +0100, Christian Brauner wrote:
> 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
Ugh, I wonder how I didn't notice this before... :(
Thanks, Christian!
Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>
> 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]);
next prev parent reply other threads:[~2026-11-17 16:39 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 15:25 [PATCH net 0/2] net: fix a pidfd leak with a short SO_PEERPIDFD optlen Christian Brauner
2026-11-17 15:25 ` [PATCH net 1/2] net: don't leak pidfds through " Christian Brauner
2026-11-17 16:39 ` Alexander Mikhalitsyn [this message]
2026-11-17 15:25 ` [PATCH net 2/2] selftests/net: check that a short SO_PEERPIDFD optlen fails Christian Brauner
2026-11-17 16:39 ` Alexander Mikhalitsyn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a0f9a7e10bee8b5d328de14e1bbdbf74f5712d43.camel@amutable.com \
--to=alex@amutable.com \
--cc=alexander@mihalicyn.com \
--cc=brauner@kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sashiko-bot@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox