Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1190954rwd; Thu, 8 Jun 2023 13:46:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6TnjytT0bIOP1uRiKD/09v0TcmdD8SfyLCxC7/d0s9Bj3n+eE+NBtumyOsyfLC2sT3e95I X-Received: by 2002:aa7:8889:0:b0:64c:a554:f577 with SMTP id z9-20020aa78889000000b0064ca554f577mr11853732pfe.11.1686257206465; Thu, 08 Jun 2023 13:46:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686257206; cv=none; d=google.com; s=arc-20160816; b=fOQZOIE8pOep/ss6XCJyT05+nTwKYh4ntBTIShP+H6Bdl8/ImY4tOo6O3Sf7UKYWFf 7N6SONt/PrqYzssLkv9F582FOQxJuupJvRdwQQi7eGJ2sp002BBri2yeiFvmz+7QSGM/ sH8BuY+J6S0EyMjc1hHLRxEZZ+UnhLgjXNiOtB+TB21dcBW19hi3k2sPjjTywahUVhG6 JDKfrEB1iKdlu2fFAXRJINCLwOFYRhx/OIb2Bjukv2fjQWidb4zdq3wUH/r3U5NHNs6E LOHoSQGBmrzz5PKMwTzO9GiQbeAAKHPY4VpbEsBEmkThwq4Aoos08FRcH3GPyZo0tlSH Z1EQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pfeDE9N7PiA5dymF0+sNi0Y5aUGnSrWnAuBDujTkTKQ=; b=gZrNhHd+SZDYeZGx4W/Ny1zymKDHqYyzkkhbhbyMOW9Iv6aauAsmNs5IPivxy/7QDi WAMtbGxHU2HtOBt93SoQ1gk+vkFqq8PbfZp1R6H8WMkRpRyBoVWgm1bdG8S1pczGQGEr kPNrJQu5iR1DlRCCN13wqg/1gSKXkaOPtp/hHqNIWN+NwREKH91oGLkBmwAscm66kVfO 1G4xAkP7zaK6+nSpsxxm7UwRRTuWFPuoiW9dwoCpU8RNj1vqafjAAWhdOWsh6H/azR2s b/xfx/EmoELaoHhLPjmGX2PD2/6EwEEYS/DmuFqFK0lHeCgqpp/jlebWq90IGjra0P81 1tXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=fd7BFqlX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y7-20020aa793c7000000b0064d533abfd4si1310842pff.189.2023.06.08.13.46.32; Thu, 08 Jun 2023 13:46:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=fd7BFqlX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236906AbjFHU1P (ORCPT + 99 others); Thu, 8 Jun 2023 16:27:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236548AbjFHU07 (ORCPT ); Thu, 8 Jun 2023 16:26:59 -0400 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F04A82D7F for ; Thu, 8 Jun 2023 13:26:53 -0700 (PDT) Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id A0E963F36E for ; Thu, 8 Jun 2023 20:26:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1686256011; bh=pfeDE9N7PiA5dymF0+sNi0Y5aUGnSrWnAuBDujTkTKQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fd7BFqlXsiylugMoFFJZI2IFbYpTkHlSXtoatXn32WI3yAiPtdnNYfFddecBo3Vy8 7ZjePODZwYgprX1qj3LX+2b7UVnibt6eOPkzznCcSES5w9kbVy4hXNeDsusLzapwIW EmZqsC2RL3JXbna+w2wopId+wpWtI137QVIQwmQA9k5mf+tWFBMZfONyJDv+oSLKyM OCou9kKCjIQtQXrTtFxuY2tHaMHmG1Uly9z87BNUAPdgLKUaL3HAUcRK6LeYl0EkzL OThA3BJcrszarW9LCVik+58stAKlcw4nbxXLqwCZDK5ItKLzKjQnL3J4oce9ZilAZn tfEyU3HsVc0ZA== Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-514b19ded99so908751a12.0 for ; Thu, 08 Jun 2023 13:26:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686256011; x=1688848011; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pfeDE9N7PiA5dymF0+sNi0Y5aUGnSrWnAuBDujTkTKQ=; b=JKUNPCQANRgWDUE5Iu8qX80eCkAncRm+jpfZ7v4aDiitegmbyZmnmiBdX+c5EIjXAl 1pOB8VuuJUxPoxX3WIMHCUuwuOKpRChV/20VR8ncidYL93w6idWXae/pnts4MoBCaiZN 0dWs66M/wEIG9Q0YjhQ7pEM7liJW2fCgyC7GhamAUg5zNwGUzQDIDv/M6TTTw8wiQ8WA laFhyeHqeyFrIWb19U2vIqYwlT2Rfh3Zv+RK2P4zyZEwTv1/YQZY7cgLacAXPf0SzDqI zLghrN7/GqW6ZYdyDMNl9galSCmpzWtexu5/o2BAa9SnGIbnM5cJz1undYVk6GnqLIud JY1Q== X-Gm-Message-State: AC+VfDzgeLQgpaPT3+01S2JypjowVBI7Ckmb9Nq451z3l5m5S3au74hg cyVdGMFGq58vD/83x/n8Wxeg+NbJAaZ5v5Ss1UUtkTRy3IwjPKiaKQKgrWyQu0QpDN/KpuhhlFi sWxVELpSCA8JKMBoOPMmW2Gwyej9jJ3PU2h0Y1MDBNg== X-Received: by 2002:a17:907:9618:b0:978:adad:fe08 with SMTP id gb24-20020a170907961800b00978adadfe08mr319421ejc.6.1686256011134; Thu, 08 Jun 2023 13:26:51 -0700 (PDT) X-Received: by 2002:a17:907:9618:b0:978:adad:fe08 with SMTP id gb24-20020a170907961800b00978adadfe08mr319415ejc.6.1686256010965; Thu, 08 Jun 2023 13:26:50 -0700 (PDT) Received: from amikhalitsyn.local (dslb-002-205-064-187.002.205.pools.vodafone-ip.de. [2.205.64.187]) by smtp.gmail.com with ESMTPSA id b16-20020a170906491000b0095342bfb701sm315592ejq.16.2023.06.08.13.26.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 13:26:50 -0700 (PDT) From: Alexander Mikhalitsyn To: davem@davemloft.net Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Alexander Mikhalitsyn , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Leon Romanovsky , David Ahern , Arnd Bergmann , Kees Cook , Christian Brauner , Kuniyuki Iwashima , Lennart Poettering , Luca Boccassi , Daniel Borkmann , Stanislav Fomichev , bpf@vger.kernel.org, linux-arch@vger.kernel.org Subject: [PATCH net-next v7 2/4] net: core: add getsockopt SO_PEERPIDFD Date: Thu, 8 Jun 2023 22:26:26 +0200 Message-Id: <20230608202628.837772-3-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230608202628.837772-1-aleksandr.mikhalitsyn@canonical.com> References: <20230608202628.837772-1-aleksandr.mikhalitsyn@canonical.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add SO_PEERPIDFD which allows to get pidfd of peer socket holder pidfd. This thing is direct analog of SO_PEERCRED which allows to get plain PID. Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Leon Romanovsky Cc: David Ahern Cc: Arnd Bergmann Cc: Kees Cook Cc: Christian Brauner Cc: Kuniyuki Iwashima Cc: Lennart Poettering Cc: Luca Boccassi Cc: Daniel Borkmann Cc: Stanislav Fomichev Cc: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: linux-arch@vger.kernel.org Reviewed-by: Christian Brauner Acked-by: Stanislav Fomichev Tested-by: Luca Boccassi Signed-off-by: Alexander Mikhalitsyn --- v5: - started using (struct proto)->bpf_bypass_getsockopt hook v4: - return -ESRCH if sk->sk_peer_pid is NULL from getsockopt() syscall - return errors from pidfd_prepare() as it is from getsockopt() syscall v3: - fixed possible fd leak (thanks to Christian Brauner) v2: According to review comments from Kuniyuki Iwashima and Christian Brauner: - use pidfd_create(..) retval as a result - whitespace change --- arch/alpha/include/uapi/asm/socket.h | 1 + arch/mips/include/uapi/asm/socket.h | 1 + arch/parisc/include/uapi/asm/socket.h | 1 + arch/sparc/include/uapi/asm/socket.h | 1 + include/uapi/asm-generic/socket.h | 1 + net/core/sock.c | 33 +++++++++++++++++++++++++ net/unix/af_unix.c | 16 ++++++++++++ tools/include/uapi/asm-generic/socket.h | 1 + 8 files changed, 55 insertions(+) diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h index ff310613ae64..e94f621903fe 100644 --- a/arch/alpha/include/uapi/asm/socket.h +++ b/arch/alpha/include/uapi/asm/socket.h @@ -138,6 +138,7 @@ #define SO_RCVMARK 75 #define SO_PASSPIDFD 76 +#define SO_PEERPIDFD 77 #if !defined(__KERNEL__) diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h index 762dcb80e4ec..60ebaed28a4c 100644 --- a/arch/mips/include/uapi/asm/socket.h +++ b/arch/mips/include/uapi/asm/socket.h @@ -149,6 +149,7 @@ #define SO_RCVMARK 75 #define SO_PASSPIDFD 76 +#define SO_PEERPIDFD 77 #if !defined(__KERNEL__) diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h index df16a3e16d64..be264c2b1a11 100644 --- a/arch/parisc/include/uapi/asm/socket.h +++ b/arch/parisc/include/uapi/asm/socket.h @@ -130,6 +130,7 @@ #define SO_RCVMARK 0x4049 #define SO_PASSPIDFD 0x404A +#define SO_PEERPIDFD 0x404B #if !defined(__KERNEL__) diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h index 6e2847804fea..682da3714686 100644 --- a/arch/sparc/include/uapi/asm/socket.h +++ b/arch/sparc/include/uapi/asm/socket.h @@ -131,6 +131,7 @@ #define SO_RCVMARK 0x0054 #define SO_PASSPIDFD 0x0055 +#define SO_PEERPIDFD 0x0056 #if !defined(__KERNEL__) diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h index b76169fdb80b..8ce8a39a1e5f 100644 --- a/include/uapi/asm-generic/socket.h +++ b/include/uapi/asm-generic/socket.h @@ -133,6 +133,7 @@ #define SO_RCVMARK 75 #define SO_PASSPIDFD 76 +#define SO_PEERPIDFD 77 #if !defined(__KERNEL__) diff --git a/net/core/sock.c b/net/core/sock.c index ed4eb4ba738b..ea66b1afadd0 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1758,6 +1758,39 @@ int sk_getsockopt(struct sock *sk, int level, int optname, goto lenout; } + case SO_PEERPIDFD: + { + struct pid *peer_pid; + struct file *pidfd_file = NULL; + int pidfd; + + if (len > sizeof(pidfd)) + len = sizeof(pidfd); + + spin_lock(&sk->sk_peer_lock); + peer_pid = get_pid(sk->sk_peer_pid); + spin_unlock(&sk->sk_peer_lock); + + if (!peer_pid) + return -ESRCH; + + pidfd = pidfd_prepare(peer_pid, 0, &pidfd_file); + put_pid(peer_pid); + if (pidfd < 0) + return pidfd; + + if (copy_to_sockptr(optval, &pidfd, len) || + copy_to_sockptr(optlen, &len, sizeof(int))) { + put_unused_fd(pidfd); + fput(pidfd_file); + + return -EFAULT; + } + + fd_install(pidfd, pidfd_file); + return 0; + } + case SO_PEERGROUPS: { const struct cred *cred; diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index c46c2f5d860c..73c61a010b01 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -921,11 +921,26 @@ static void unix_unhash(struct sock *sk) */ } +static bool unix_bpf_bypass_getsockopt(int level, int optname) +{ + if (level == SOL_SOCKET) { + switch (optname) { + case SO_PEERPIDFD: + return true; + default: + return false; + } + } + + return false; +} + struct proto unix_dgram_proto = { .name = "UNIX", .owner = THIS_MODULE, .obj_size = sizeof(struct unix_sock), .close = unix_close, + .bpf_bypass_getsockopt = unix_bpf_bypass_getsockopt, #ifdef CONFIG_BPF_SYSCALL .psock_update_sk_prot = unix_dgram_bpf_update_proto, #endif @@ -937,6 +952,7 @@ struct proto unix_stream_proto = { .obj_size = sizeof(struct unix_sock), .close = unix_close, .unhash = unix_unhash, + .bpf_bypass_getsockopt = unix_bpf_bypass_getsockopt, #ifdef CONFIG_BPF_SYSCALL .psock_update_sk_prot = unix_stream_bpf_update_proto, #endif diff --git a/tools/include/uapi/asm-generic/socket.h b/tools/include/uapi/asm-generic/socket.h index fbbc4bf53ee3..54d9c8bf7c55 100644 --- a/tools/include/uapi/asm-generic/socket.h +++ b/tools/include/uapi/asm-generic/socket.h @@ -122,6 +122,7 @@ #define SO_RCVMARK 75 #define SO_PASSPIDFD 76 +#define SO_PEERPIDFD 77 #if !defined(__KERNEL__) -- 2.34.1