Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6848640ybl; Mon, 23 Dec 2019 13:11:41 -0800 (PST) X-Google-Smtp-Source: APXvYqxpMvebvb890fhtTJMNzgxtGKXiwkN5w8fXiYxmNFr3ZRiqNZDyQt8LyqiGAkBbGDXsiquh X-Received: by 2002:a9d:66c1:: with SMTP id t1mr31635020otm.73.1577135501417; Mon, 23 Dec 2019 13:11:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577135501; cv=none; d=google.com; s=arc-20160816; b=yiskt7abeUNZ2WnyIYqSu4jW8mPF3KWdOUuHJWP0OpHh8EySzbBuU0Y1QWQwETr+mY AUVJ1bMrfxlLYgcrPe5/TK1dvvPyFwnWN51nShwNO2SGMnKc/ivwuAzPPWfBmMVlkLP8 pxAq8FLOdVhgcrocDsPzTDbwpqjE+Z1aaGUeXPhdWySd0YiBii/RWE2FQGs9JiQezUf4 qdlG0+BzRWxbVMejpnW73LMIaTqiC6HwBR4MZ3S3q0yFVjigsH12OrhdA7lzMjdICFiM gAtHhIWnPWBka6iiwVQSSU7VTTkOqXwoTxDDMHOanb3QsAQE8kvazRf8a0PqY8XFE1eQ eqlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=2PRppcZj4QQV2bYtw/LlSv6wKSsTQE8fDROQNhRtfik=; b=lmwLsVvYmqgHhtOS0he5aLeUVgQsEdT+c8hVHeHZJXxiYwMdAkeDQOYbqSahlTP1/p 7SGsiH9nVeQPIXAMI1wSVfyL/LDLs835pYDNFYqBf7dLmlkQY83AzRm+jpVjCnHmbllr +zdwXWlytDs8hP0/yz1Xiignw7h9uN5GaAJWia2hYubXDFMVIm6xpDXBVVZ/ZwzKzdwB 9FhaZNo/nxUxQmYrMyuYEOPHJcZs61380iDyK0PXX96P7quc0CNEzukM1Xevdk/Pxann MynPSiW/0j4JFCtixJwABBsjCSN7+uhzk+vykYnpW9IQFMg3Xcqutj6TZoaOFMT59HOM s3Lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=KYeJkAS8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l10si11236773otr.159.2019.12.23.13.11.00; Mon, 23 Dec 2019 13:11:41 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=KYeJkAS8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726865AbfLWVIa (ORCPT + 99 others); Mon, 23 Dec 2019 16:08:30 -0500 Received: from mail-io1-f67.google.com ([209.85.166.67]:43978 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726817AbfLWVIa (ORCPT ); Mon, 23 Dec 2019 16:08:30 -0500 Received: by mail-io1-f67.google.com with SMTP id n21so15810148ioo.10 for ; Mon, 23 Dec 2019 13:08:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=2PRppcZj4QQV2bYtw/LlSv6wKSsTQE8fDROQNhRtfik=; b=KYeJkAS8Z8H02AKtkskQnaPjMQKy1JBE6L0dadg7nnYeXthIYm+RIEc1R1faHZtU5u MIdgJ/Xtp9suaQ5iEwrIhl2BQxuuGQbQQZaAZqM8ct1CqqMnb2EUv8wgHl2ctlR2q2NO n5Kaeqj2vKcwmb4jpqSxGUW2aR/UOANVHXTuM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=2PRppcZj4QQV2bYtw/LlSv6wKSsTQE8fDROQNhRtfik=; b=qLtP++O7e/UpDZmbwURBbtLeK8ugDSM4xuwjg9tQdI0tXQudcSc9JpVFQffZ0lhhdw a/nVCpVUGsTFq2mMB4qMTqX5g5O7/IWOKQtMWT+bwpejePfvTnFWpITYjgUFx1eQapFB BL/exLu913Nz3nmYrAwAyYqMc38+Dptl29tOaQ988xg0hw5CLALzT9H/cjLfum1FniXR q4lOCZSDi2LhzyDhEoE/bTaHC3VQ6PU2II9tCo3mNmLmqSaV5ddiwq2iJlqvStvFjAPZ JRMBao7oX8CMjxIdCny5QA2K20AR83BFRJw3HsqFsgLFk3fDsXOxvscW3Vogiq61Kf64 Ye8A== X-Gm-Message-State: APjAAAVp9MmthhV0Yx5GuI5YVEoPy2VrcFSYmw5Cy9/+zil1fupYJEnT 1OrjY07JaFf3jVfM2+1Hs5F5ZIIpTd8YDQ== X-Received: by 2002:a02:b893:: with SMTP id p19mr24549440jam.103.1577135308072; Mon, 23 Dec 2019 13:08:28 -0800 (PST) Received: from ircssh-2.c.rugged-nimbus-611.internal (80.60.198.104.bc.googleusercontent.com. [104.198.60.80]) by smtp.gmail.com with ESMTPSA id p5sm9312687ilg.69.2019.12.23.13.08.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Dec 2019 13:08:27 -0800 (PST) Date: Mon, 23 Dec 2019 21:08:26 +0000 From: Sargun Dhillon To: linux-kernel@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: tycho@tycho.ws, jannh@google.com, cyphar@cyphar.com, christian.brauner@ubuntu.com, oleg@redhat.com, luto@amacapital.net, viro@zeniv.linux.org.uk, gpascutto@mozilla.com, ealvarez@mozilla.com, fweimer@redhat.com, jld@mozilla.com, arnd@arndb.de Subject: [PATCH v6 0/3] Add pidfd_getfd syscall Message-ID: <20191223210823.GA25083@ircssh-2.c.rugged-nimbus-611.internal> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patchset introduces a mechanism (pidfd_getfd syscall) to get file descriptors from other processes via pidfd. Although this can be achieved using SCM_RIGHTS, and parasitic code injection, this offers a more straightforward mechanism, with less overhead and complexity. The process under manipulation's fd still remains valid, and unmodified by the copy operation. It introduces a flags field. The flags field is reserved a the moment, but the intent is to extend it with the following capabilities: * Close the remote FD when copying it * Drop the cgroup data if it's a fd pointing a socket when copying it The syscall numbers were chosen to be one greater than openat2. Summary of history: This initially started as a ptrace command. It did not require the process to be stopped, and felt like kind of an awkward fit for ptrace. After that, it moved to an ioctl on the pidfd. Given functionality, it made sense to make it a syscall which did not require the process to be stopped. Changes since v5: * Drop pidfd_getfd_options struct and replace with a flags field Changes since v4: * Turn into a syscall * Move to PTRACE_MODE_ATTACH_REALCREDS from PTRACE_MODE_READ_REALCREDS * Remove the sample code. This will come in another patchset, as the new self-tests cover all the functionality. Changes since v3: * Add self-test * Move to ioctl passing fd directly, versus args struct * Shuffle around include files Changes since v2: * Move to ioctl on pidfd instead of ptrace function * Add security check before moving file descriptor Changes since the RFC v1: * Introduce a new helper to fs/file.c to fetch a file descriptor from any process. It largely uses the code suggested by Oleg, with a few changes to fix locking * It uses an extensible options struct to supply the FD, and option. * I added a sample, using the code from the user-ptrace sample Sargun Dhillon (3): vfs, fdtable: Add get_task_file helper pid: Introduce pidfd_getfd syscall test: Add test for pidfd getfd arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 2 + arch/ia64/kernel/syscalls/syscall.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/mips/kernel/syscalls/syscall_o32.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + fs/file.c | 22 +- include/linux/file.h | 2 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 3 +- kernel/pid.c | 106 ++++++++ tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 2 +- .../selftests/pidfd/pidfd_getfd_test.c | 253 ++++++++++++++++++ 26 files changed, 405 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/pidfd/pidfd_getfd_test.c -- 2.20.1