From patchwork Tue Apr 11 10:42:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksandr Mikhalitsyn X-Patchwork-Id: 81876 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2479253vqo; Tue, 11 Apr 2023 03:56:35 -0700 (PDT) X-Google-Smtp-Source: AKy350YY1/EZOqQBYWOh4DkAqAoTHRFLC5+bmGpUIEzCwcfU1b4u0Trn3ITDwb7TBksXVaulXLxn X-Received: by 2002:a17:90b:1b05:b0:239:ea16:5b13 with SMTP id nu5-20020a17090b1b0500b00239ea165b13mr18549507pjb.14.1681210595099; Tue, 11 Apr 2023 03:56:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681210595; cv=none; d=google.com; s=arc-20160816; b=uUWuxEC71hr/sjsz3Lmwk+WiPlVVbbMDeoTtbIEdKWigaWwTpPXAUMz7i7eAxuTHr/ ksd0yTuROZdPpk3iD/kNG6x5BCL98620fSayVqOeCYGX+9XVW1fT/ZqJPOBI0HOKLhzp cjAxPWEw3Rsd30lLUjKVvsNRI4c2OwgcpcyVXKd0zC3OYGt9EBirK40Pp832jpzAwuVs J3FKrO9virYVgcUSLWUtKipWZ2dMggyojMTwXt52+DabLP58I55qQc3w2bpV5s5RSoh5 QwAwCaqpEuNK5vycqrUB8rWjFwgm9ApJGqTRXu429/otxIuYBD/J7zc8v0ryLK+SRiOq 48jQ== 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=pRf5xzGSUbwksyzKh9EFSxutG5TUV/lwNKq2bZMboXc=; b=H1lrzqX7uuEvw/ciN2IXa2Au2Xy2aIFiEb6PRiYB4efrGjmQ5R97oNGH2HXaj83KzI D719N3PAXcBTrDXSi0A+Ztbeqr2gLYjEfIoKsWxNs8WRe8ukA2gnhpHMHhok2P/NYL/B D31dHFAX7pVdt9WxslOPKhTkEtrYBwoM1uQRBNXfzg7y753zzFqOAsJmfLGHZJmk4Q7t Mxgv02u5vmkrHJwO5hkvIZY5u5gu057vfuFlHs/Uj4zC/Uowvl6mJMCEh3NCqkHHd+/u LEMUs8Vz+mbK0mfrS45x2/Uv75EgPi3az/rd9+nxwAaXhq8ZGiLJFF+OJEzUm9MX2oI6 IyFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=oQ5CAUA5; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oa18-20020a17090b1bd200b0023f1166af5dsi13512136pjb.170.2023.04.11.03.56.22; Tue, 11 Apr 2023 03:56:34 -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=oQ5CAUA5; 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 S229924AbjDKKoP (ORCPT + 99 others); Tue, 11 Apr 2023 06:44:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229910AbjDKKn7 (ORCPT ); Tue, 11 Apr 2023 06:43:59 -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 A624D40E8 for ; Tue, 11 Apr 2023 03:43:45 -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-0.canonical.com (Postfix) with ESMTPS id 55EF13F23B for ; Tue, 11 Apr 2023 10:43:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1681209823; bh=pRf5xzGSUbwksyzKh9EFSxutG5TUV/lwNKq2bZMboXc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oQ5CAUA5XLp4H0AMWn/3n9/+CjrnqZRPgOOcoZcxlOP4dgRPrvV+suah8Aj+Um3Pc 01XalS4nfgYk9smqPEZZabrkzyiupaD20wJ8lacaoLtIMukbHWyx1E/X2PhphxCBOB zl1hkDcfohGfXiCLkU8vq4+z3V2W8J75fVuB+/Gb7qrVEvwX3NKnCn5/HEwrcctAZP 8dwrJxWmnujSXmyzaAJ23SCwb4KJkNynnayAtGHtr7DfefywTliCWyTfLC3njNTx5h RNoxsQvplAB5BeD5hqJ7pOFGYa3bsvLk9pAcjNh2KkGyeSUaqw0e8ana39ENcZmRAZ N4fNzWZVz6LPQ== Received: by mail-ed1-f71.google.com with SMTP id c64-20020a509fc6000000b004a26cc7f6cbso4579522edf.4 for ; Tue, 11 Apr 2023 03:43:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681209820; 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=pRf5xzGSUbwksyzKh9EFSxutG5TUV/lwNKq2bZMboXc=; b=1qnKz497cJDMbx0yrcBKIMX/xpnt1ZK7br5wm+WUKOmHnq4OkoIuoc3SQUVb+wevjE YeszOsXcAWRnWJd+jXAEMB71dWNFGPCNS11Wc3iajsVIIUnn5E4ka3/iBPdtoNHCmj9Y l23fDgW1ESFgPyv6Mk5wG3bB8T4yc+gRGhkt3r6cyRKDZfgxD23H4x8gnzvm57jRNWeF Es2P01UB6kvIaCI5mf4HJL+ugRriaDkw5nOhQRCDY+QfqjHkfzsd2QgUJFk+5k9wH0/2 8N9kMwlMxaL52trrqtV7x8jMPvOYRuhizH8NO74XRpLUaMETYnZJ2NccBgTCUaTTDD4J lBEg== X-Gm-Message-State: AAQBX9cXr5bJ3KrGXWeoWVI5xZR4CeNeFwXBsVSvjH6rsKUCsWVF7CrE /ajZdC+7gdxwj0BpZ1rLWRw2s/1qQIERthTHXZy5uepmTAJrdtQXF1YP2v78BKZnpKniiqVrfqa C+4NYY4X83cYcqXv2BWee9N7LmjnxtXn07wlqwq45TQ== X-Received: by 2002:a17:907:2ced:b0:8ee:babc:d40b with SMTP id hz13-20020a1709072ced00b008eebabcd40bmr9069386ejc.58.1681209820504; Tue, 11 Apr 2023 03:43:40 -0700 (PDT) X-Received: by 2002:a17:907:2ced:b0:8ee:babc:d40b with SMTP id hz13-20020a1709072ced00b008eebabcd40bmr9069360ejc.58.1681209820244; Tue, 11 Apr 2023 03:43:40 -0700 (PDT) Received: from amikhalitsyn.. ([95.91.208.118]) by smtp.gmail.com with ESMTPSA id ne7-20020a1709077b8700b00948c320fcfdsm5921805ejc.202.2023.04.11.03.43.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 03:43:39 -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 , linux-arch@vger.kernel.org Subject: [PATCH net-next v3 3/4] net: core: add getsockopt SO_PEERPIDFD Date: Tue, 11 Apr 2023 12:42:30 +0200 Message-Id: <20230411104231.160837-4-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230411104231.160837-1-aleksandr.mikhalitsyn@canonical.com> References: <20230411104231.160837-1-aleksandr.mikhalitsyn@canonical.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762877080882630051?= X-GMAIL-MSGID: =?utf-8?q?1762877080882630051?= 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 --- 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..2b040a69e355 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); + + pidfd = pidfd_prepare(peer_pid, 0, &pidfd_file); + + put_pid(peer_pid); + + if (copy_to_sockptr(optval, &pidfd, len) || + copy_to_sockptr(optlen, &len, sizeof(int))) { + if (pidfd >= 0) { + put_unused_fd(pidfd); + fput(pidfd_file); + } + + return -EFAULT; + } + + if (pidfd_file) + 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__)