Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1193054rwd; Thu, 8 Jun 2023 13:48:51 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4CdH8krKcKnUNXDCwl/UnkyWx9j8YughHRWHVulz4Zfo/kXqs1sWmLGlYI1pdOc+oIxajN X-Received: by 2002:a17:903:44f:b0:1af:bfb0:e7e4 with SMTP id iw15-20020a170903044f00b001afbfb0e7e4mr8127203plb.36.1686257331495; Thu, 08 Jun 2023 13:48:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686257331; cv=none; d=google.com; s=arc-20160816; b=gapmX5smb72fVXjyaoiBc08V3kSRCpgow1aeWPlfKpJXycHXhN5KJBPN+IWwdT6h5R NYWqZKtCYsGaAVSCyCUIm0IfddmEm8pj2vUtYj2/qX9AqRWkLoMJ9S0cDOxjufWC7lZw bKLQfqk2gef60mTG9Txpg8BTaZa7kaWzdU0WOKTNioxqmdLf84gddFuGfAva0yj0Dfnt nJDmHgdzvWoTFzXoJlD4UBo1tyRyKlEg389r95LDr30VbdIAy/iFGl06dQJfD0CszF/u ovnKvP0FoWOYfKguZm1Q9D2rYcgSS4oZLndEQS0clUYsgGuRg7IYfeqEEREV9dz0RmdE 6TFA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=LbwhS6u2Gc04e5gm9H5aiasFIe5ctbHPuzK68rld63g=; b=OtcXdGwUVYih2kZHHeXlhJe0wYmiMzshlAmKxDRQ13/Sz4AuFNznjlDpY/HbaG4UhJ +3BhNgzAujYsyjOeRwPCUUYt5s173FiQBV7hhoFcivnLH2mBh6P3e+pTt660Y+L7FEvz ny8avidu4CcuflMAkHMt1ufCowtt1lqajuHdTosNfk3aO0kyIqdt0/f9BObTvREIiLgf ADw1N1IuNUuX465jOWuFv+L4xz1w4AXJa8pj3JxWgxybqCR7xlmW2QIs56EXhpHwljcx hpBYzMSJ7YmoRuU3Q71O4EHehCKOG5Im3MgCGR6QXFmJts9+eVF/DjmWF0i9jwkOXfC0 bpmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=NdfjyWIQ; 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 h5-20020a170902ac8500b001ab012a1ba9si1470113plr.402.2023.06.08.13.48.36; Thu, 08 Jun 2023 13:48:51 -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=NdfjyWIQ; 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 S236296AbjFHU0w (ORCPT + 99 others); Thu, 8 Jun 2023 16:26:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232503AbjFHU0r (ORCPT ); Thu, 8 Jun 2023 16:26:47 -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 168272D6B for ; Thu, 8 Jun 2023 13:26:45 -0700 (PDT) Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (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 B0C0C3F466 for ; Thu, 8 Jun 2023 20:26:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1686256002; bh=LbwhS6u2Gc04e5gm9H5aiasFIe5ctbHPuzK68rld63g=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=NdfjyWIQsKrQsRdbxWUoKdvECkRWFGFUc6eVIft03ojOxUriN4644o1cwI2+yYAU7 gqEl4MiOSwfWFRKqhAcauen3l6zzTRD6ili9TyXGe+jQCmCeMviMjizioeaL7OPtne OPFNfeRpaWCoWI80Oc0kpQPHzAQAfDmUby7QYb0Tnrd0VAUbveGCELbp9ZqPMXkcgo 6LnS8fqzxCqJYJfoCzzSdi5NS50pQdiVhpfuQ8qfLdtgh8vSgUl2crfRzmCp1nGpwh vQWPSgtGltQE4cEemToqmmeaAxloZAtDHP4Nf7iKkIHsJAjxc5BrA3yp1zj8xYwALg GFKkzuveQSpVA== Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-977cf886f17so86998166b.2 for ; Thu, 08 Jun 2023 13:26:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686256002; x=1688848002; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LbwhS6u2Gc04e5gm9H5aiasFIe5ctbHPuzK68rld63g=; b=Dn9jV6GowSFgJawVg1Vnf6i851LTkRA1j0BGtZYL2GFOxcJufI3PlGXa48HH4M3oyW l0t3tyW6ehqSCcboRebmHX4XSwIpHgiioCiNwuCxI0sN+uBrm0WEaWmr7nAu05FTq4Fq sCp3VGJIuo8a/e0SldPc5kWY52sg9o9yvGcGh9lfE5ZcvaXu1dSBCTL3q2on21Lc1LUG lMRwBs9W3iRNyZLlE9maQviJ46oPmaoB3njO56dlsmTgMkLghLN6VEzidshmRJHNyKg2 376U8ZE2fuSS/qp3s+bNN+yXbRQZiUmhkcOLpIaNYpL6Ad54/bmBunUAvfpinnrkM18X QtiA== X-Gm-Message-State: AC+VfDzLgGA+jxESOlYFn7+cqlhH7/5wnE1v0R1nOMryKGfh9OmlXJUq r51Ht/TIeN5jooQjWW4Aq79EUVNkeY9tDrCJtOOVzAXkgG3GAJ1E+QTvSPICKZ8s4ksdmocilvH kufFuxQIpRHyW8qew1UVntfi7eg0otFvRS292eVZRbg== X-Received: by 2002:a17:906:4fcb:b0:977:95f4:5cca with SMTP id i11-20020a1709064fcb00b0097795f45ccamr194477ejw.54.1686256002296; Thu, 08 Jun 2023 13:26:42 -0700 (PDT) X-Received: by 2002:a17:906:4fcb:b0:977:95f4:5cca with SMTP id i11-20020a1709064fcb00b0097795f45ccamr194475ejw.54.1686256002010; Thu, 08 Jun 2023 13:26:42 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 13:26:41 -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 Subject: [PATCH net-next v7 0/4] Add SCM_PIDFD and SO_PEERPIDFD Date: Thu, 8 Jun 2023 22:26:24 +0200 Message-Id: <20230608202628.837772-1-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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=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 1. Implement SCM_PIDFD, a new type of CMSG type analogical to SCM_CREDENTIALS, but it contains pidfd instead of plain pid, which allows programmers not to care about PID reuse problem. 2. 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. 3. Add SCM_PIDFD / SO_PEERPIDFD kselftest Idea comes from UAPI kernel group: https://uapi-group.org/kernel-features/ Big thanks to Christian Brauner and Lennart Poettering for productive discussions about this and Luca Boccassi for testing and reviewing this. === Motivation behind this patchset Eric Dumazet raised a question: > It seems that we already can use pidfd_open() (since linux-5.3), and > pass the resulting fd in af_unix SCM_RIGHTS message ? Yes, it's possible, but it means that from the receiver side we need to trust the sent pidfd (in SCM_RIGHTS), or always use combination of SCM_RIGHTS+SCM_CREDENTIALS, then we can extract pidfd from SCM_RIGHTS, then acquire plain pid from pidfd and after compare it with the pid from SCM_CREDENTIALS. A few comments from other folks regarding this. Christian Brauner wrote: >Let me try and provide some of the missing background. >There are a range of use-cases where we would like to authenticate a >client through sockets without being susceptible to PID recycling >attacks. Currently, we can't do this as the race isn't fully fixable. >We can only apply mitigations. >What this patchset will allows us to do is to get a pidfd without the >client having to send us an fd explicitly via SCM_RIGHTS. As that's >already possibly as you correctly point out. >But for protocols like polkit this is quite important. Every message is >standalone and we would need to force a complete protocol change where >we would need to require that every client allocate and send a pidfd via >SCM_RIGHTS. That would also mean patching through all polkit users. >For something like systemd-journald where we provide logging facilities >and want to add metadata to the log we would also immensely benefit from >being able to get a receiver-side controlled pidfd. >With the message type we envisioned we don't need to change the sender >at all and can be safe against pid recycling. >Link: https://gitlab.freedesktop.org/polkit/polkit/-/merge_requests/154 >Link: https://uapi-group.org/kernel-features Lennart Poettering wrote: >So yes, this is of course possible, but it would mean the pidfd would >have to be transported as part of the user protocol, explicitly sent >by the sender. (Moreover, the receiver after receiving the pidfd would >then still have to somehow be able to prove that the pidfd it just >received actually refers to the peer's process and not some random >process. – this part is actually solvable in userspace, but ugly) >The big thing is simply that we want that the pidfd is associated >*implicity* with each AF_UNIX connection, not explicitly. A lot of >userspace already relies on this, both in the authentication area >(polkit) as well as in the logging area (systemd-journald). Right now >using the PID field from SO_PEERCREDS/SCM_CREDENTIALS is racy though >and very hard to get right. Making this available as pidfd too, would >solve this raciness, without otherwise changing semantics of it all: >receivers can still enable the creds stuff as they wish, and the data >is then implicitly appended to the connections/datagrams the sender >initiates. >Or to turn this around: things like polkit are typically used to >authenticate arbitrary dbus methods calls: some service implements a >dbus method call, and when an unprivileged client then issues that >call, it will take the client's info, go to polkit and ask it if this >is ok. If we wanted to send the pidfd as part of the protocol we >basically would have to extend every single method call to contain the >client's pidfd along with it as an additional argument, which would be >a massive undertaking: it would change the prototypes of basically >*all* methods a service defines… And that's just ugly. >Note that Alex' patch set doesn't expose anything that wasn't exposed >before, or attach, propagate what wasn't before. All it does, is make >the field already available anyway (the struct ucred .pid field) >available also in a better way (as a pidfd), to solve a variety of >races, with no effect on the protocol actually spoken within the >AF_UNIX transport. It's a seamless improvement of the status quo. === Git tree: https://github.com/mihalicyn/linux/tree/scm_pidfd 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 Tested-by: Luca Boccassi Alexander Mikhalitsyn (4): scm: add SO_PASSPIDFD and SCM_PIDFD net: core: add getsockopt SO_PEERPIDFD selftests: net: add SCM_PIDFD / SO_PEERPIDFD test af_unix: Kconfig: make CONFIG_UNIX bool arch/alpha/include/uapi/asm/socket.h | 3 + arch/mips/include/uapi/asm/socket.h | 3 + arch/parisc/include/uapi/asm/socket.h | 3 + arch/sparc/include/uapi/asm/socket.h | 3 + include/linux/net.h | 1 + include/linux/socket.h | 1 + include/net/scm.h | 39 +- include/uapi/asm-generic/socket.h | 3 + net/core/sock.c | 44 ++ net/mptcp/sockopt.c | 1 + net/unix/Kconfig | 6 +- net/unix/af_unix.c | 34 +- tools/include/uapi/asm-generic/socket.h | 3 + tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/af_unix/Makefile | 3 +- .../testing/selftests/net/af_unix/scm_pidfd.c | 430 ++++++++++++++++++ 16 files changed, 565 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/net/af_unix/scm_pidfd.c -- 2.34.1