Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2446910rwl; Thu, 13 Apr 2023 06:39:35 -0700 (PDT) X-Google-Smtp-Source: AKy350abuc2BoU3CO/HlwAfv70Ijxtq76vtYXaI3SuqUMs+QoLGFHQf4eYAe1gB5Vyy3/jlJKI2w X-Received: by 2002:a05:6a00:854:b0:634:b231:d2ec with SMTP id q20-20020a056a00085400b00634b231d2ecmr4153264pfk.25.1681393175191; Thu, 13 Apr 2023 06:39:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681393175; cv=none; d=google.com; s=arc-20160816; b=WJW1yfovO4F56Feckq04e/0/kdBvck2WayqfCmY7EkZN4ZDSZCKm14KYi3ASmEONh1 c50IwT2V6CsR+sGmVElpZiSi6PJhq40Z2fB0sHV8C+P/GbrSoLJgHQqzp/8Bgcawe2Pk 6Y27707h02ICx+/31yEO3ZgWzkyVXkGJzKWGdZorwvChSy7KJ1bVQtqdxu+hD5TkPZLS PcVU/Fn3Wnlw0mJIPdSc/UOWHRi0jqvDbMra6EZC9uUQGwL7kyRuRFcrvrOzW4XvLFYa HRF1V2uRjPEbXt+WehiCb4Dh1K8Hg9fwr2WfGKDJYas43jKLpAAAQOz9trMqmFZA98dV 7ITg== 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=tINNpmFGgSXRr6eO7vUL42OoLzt52EnFT+KFr84tTSM=; b=Ttz/G7Pl+plrT/gLtmdvZ78sc5glC+tUffcQd4fN2sEoWgfQNZQcHd+n6gIjjzfLUP vaN5vFXIIAvG+d6M8wrwdoJwGzM0pysKCIkqIwGhz1z6RHJ9RTbI7pZ1VWdgC5gZ1GUa /9wKEjrV7ubEFflATqhvBvecSLC3YBSTd2LZv9q9k8pnwPegqLoE3XE6v5win9mf4Kwj +ozfdX9FBCoovHXFyxOeg3KUs8J64sOtDxKWog+JOvka2Slx3fArT1E0khXM2drGyf9d yTBbGLpbhq5WBqhg9XdR7R8/5f05mtYRbTI6M2yYPhX+pWYuxFhm2DryofvDQDstnatv 2g3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=IWDXicU2; 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 y36-20020a634964000000b005138c1f1fcdsi2017207pgk.663.2023.04.13.06.39.23; Thu, 13 Apr 2023 06:39:35 -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=IWDXicU2; 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 S231625AbjDMNhl (ORCPT + 99 others); Thu, 13 Apr 2023 09:37:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231181AbjDMNhT (ORCPT ); Thu, 13 Apr 2023 09:37:19 -0400 Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C252C153 for ; Thu, 13 Apr 2023 06:35:06 -0700 (PDT) Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (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-0.canonical.com (Postfix) with ESMTPS id 0D9C23F4A5 for ; Thu, 13 Apr 2023 13:34:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1681392882; bh=tINNpmFGgSXRr6eO7vUL42OoLzt52EnFT+KFr84tTSM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IWDXicU2IQ9anwvlt41gLsLZbPZ1unCTVWeeM7Up+nwwE+5OIiswt8+tchFOFGrpi pveAeyfsJTBEivTaXTd85UCCbRhLx1gMYxi4aBH3GStjvwl5Cg1CmahWFbZGkGNJoH 1xuIhnVWOQgNylub289HfbcFlGKHZgiJQygBmYIzLZSUw3tv34JYlekk71isGeEm4o Ht1z/+wpVNypgc/A5ELZB1zmKY6MMLztwD0YuIavb21KxOWGZfHgPGZ/RQLMO1sohs BHgp1O+dRWB/X8eVoJtejN3GE9sM83UNe0c7WhpgCd6aiPwmfpgD1vGBYpd87MyaP9 CRCYbVg30oVDA== Received: by mail-ed1-f69.google.com with SMTP id x5-20020a50d605000000b005066c2aa22aso1456211edi.17 for ; Thu, 13 Apr 2023 06:34:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681392879; x=1683984879; 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=tINNpmFGgSXRr6eO7vUL42OoLzt52EnFT+KFr84tTSM=; b=P8wOmmtMh6Koh5Y1jw7ZqkLJJx/0iMLT6xJepAQ2Cu3t/2zeAzIPMVW9/QY4YldW2u W+fMGOmk/4pj0xer9ASq9Jxo4m+uz3Oz4QyeLVhPKgo3Mfr6QCnCHy04lrS8BfVu8tZd RwjNhgRRyZxTsE4v+wa8btNk4csAYwpEIu3Q4vFX6vFHOkqfTUIsZH0w8UUMDyhgjV46 uZaq0qN3f9oDgK1J4NmIrBZxMWFKNNZ5Kgdny5rLqfFf8t1KUPaXiNEiuqiTPKAz8GbQ rn5gtjouuGGsh1Oy0ww14ZVTmY+tDukz1aeEBqEWe2+hJyMstZdzihHUqXPyUkwCctEH gi7A== X-Gm-Message-State: AAQBX9chijo4Yq+61s3QMxJxqL8rc8vNvD1iR8Q3/BiN7AcwBlW3hIXV CL3+m3+Q4vKpDtSF9U7cyJtCXOA7LleB+OOkhEx8zkL50XfQEH0ihEiQgHRnLEkqQEnwK3ASZzG O6+q3gNg8W8SqCXtXKudmgh06DlLmW1YDmKrMGY2LVQ== X-Received: by 2002:a17:906:57d0:b0:94a:62e7:70e1 with SMTP id u16-20020a17090657d000b0094a62e770e1mr2718806ejr.68.1681392879595; Thu, 13 Apr 2023 06:34:39 -0700 (PDT) X-Received: by 2002:a17:906:57d0:b0:94a:62e7:70e1 with SMTP id u16-20020a17090657d000b0094a62e770e1mr2718777ejr.68.1681392879298; Thu, 13 Apr 2023 06:34:39 -0700 (PDT) Received: from amikhalitsyn.. ([95.91.208.118]) by smtp.gmail.com with ESMTPSA id et22-20020a170907295600b0094a966330fdsm976806ejc.211.2023.04.13.06.34.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 06:34:38 -0700 (PDT) From: Alexander Mikhalitsyn To: davem@davemloft.net Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, daniel@iogearbox.net, Alexander Mikhalitsyn , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Leon Romanovsky , David Ahern , Arnd Bergmann , Kees Cook , Christian Brauner , Kuniyuki Iwashima , Lennart Poettering , Luca Boccassi , linux-arch@vger.kernel.org Subject: [PATCH net-next v4 3/4] net: core: add getsockopt SO_PEERPIDFD Date: Thu, 13 Apr 2023 15:33:54 +0200 Message-Id: <20230413133355.350571-4-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230413133355.350571-1-aleksandr.mikhalitsyn@canonical.com> References: <20230413133355.350571-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,URIBL_BLOCKED autolearn=ham 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: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: linux-arch@vger.kernel.org Tested-by: Luca Boccassi Signed-off-by: Alexander Mikhalitsyn --- 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/socket.c | 7 ++++++ tools/include/uapi/asm-generic/socket.h | 1 + 8 files changed, 46 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 3f974246ba3e..49531c0dc57f 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1763,6 +1763,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/socket.c b/net/socket.c index 9c1ef11de23f..505b85489354 100644 --- a/net/socket.c +++ b/net/socket.c @@ -2248,6 +2248,13 @@ static bool sockopt_installs_fd(int level, int optname) default: return false; } + } else if (level == SOL_SOCKET) { + switch (optname) { + case SO_PEERPIDFD: + return true; + default: + return false; + } } return false; 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