Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp510566ybl; Tue, 7 Jan 2020 10:00:31 -0800 (PST) X-Google-Smtp-Source: APXvYqykXIuhFbYIbEK204Xb84qL5Kh5EnfUNYLkR3eCzcDRvo6vFrQJj6EWuiqA7fqKqdBVtf+D X-Received: by 2002:a05:6830:1e8a:: with SMTP id n10mr979821otr.303.1578420031585; Tue, 07 Jan 2020 10:00:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578420031; cv=none; d=google.com; s=arc-20160816; b=NxpJCF6BqCMZRH6KhX0eMWcUPsHSp1XPITx98QMmRlXXLq48rs7qu3Gdt+PSzvDUVr 4dss+xcKlokwMsmJ9ReII8Oejhn8tH3c9eQ5I+eWYG9wrdZSBMFZigK1Ie3SzZEpssWb 9X1jqZCNTWYGJ26P8ay2ho/l9mzd23EQUjhJnCGfldzEUxb210giiKpUZRR17VZ1RA9T Ri8wgNCSIUDDIvtmhopMnXWj3MKW9+4D3WSnFBg7M85LtoI6l6CYfupItm8ssLRBrozu PZgq8VPl2GnjDr/zY5TUsweNbWkE1HyOG37YCi/WukW4x8oJjdxl4bFtsRyP7Ol25ElK w/xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=j+lDQbpVYj7+3hOzgYRBGgCRI9V3/aLmWBx0c90vo9Q=; b=IW4dR23N/PxCkimADqFC1yTgR+dDfgEuMtP3paZ5ZaBJ5s6o6kuR84IJgYAnFLfZCq kdYTjW1oMa8LLII8xt4extwx2yYA7qnL0yJToQO92pWOvw3XTodGA/sGzngV2B2z3/6H rlg8p2hC6xdEb43kcA0kjiu0FjXmIGwsJnbMi9wpIJJr3gXXURnpIxuEe22otSF8TmGP Jtfr170lizywnPgOTLGjWOlvqU8yQDsCzBqAHkaC7UUQ3fogy4MD2oxlRwGgQZ0XOINB fwOLQxt4/uagWdUekdwmLWCwuZFGdvFDUNY/EhnBpE3GW6cKi/8Nj+iYzJf1iSGvtItP l+aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=O9Qg9iek; 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 46si309570otu.45.2020.01.07.10.00.18; Tue, 07 Jan 2020 10:00:31 -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=O9Qg9iek; 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 S1728482AbgAGR7f (ORCPT + 99 others); Tue, 7 Jan 2020 12:59:35 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:39296 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728409AbgAGR7e (ORCPT ); Tue, 7 Jan 2020 12:59:34 -0500 Received: by mail-pf1-f196.google.com with SMTP id q10so237949pfs.6 for ; Tue, 07 Jan 2020 09:59:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=j+lDQbpVYj7+3hOzgYRBGgCRI9V3/aLmWBx0c90vo9Q=; b=O9Qg9iekpvnSfzQ40a812uua0q0S8PQDjlBeyTrvT/JrHk2KWKerQ1kCQxvkFzOsz+ VFbNcxc8dA9rfiCwEtlIElhkMdhY725BfCrXTMwxvc6+ZqF++jzpXrvW5uTQhVmHjC3I GF/k8nYeI7XIK7F4kD8DTk5xqdmZv0LFykEZM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=j+lDQbpVYj7+3hOzgYRBGgCRI9V3/aLmWBx0c90vo9Q=; b=lzYFZkcSXZhcEmTaMTiGXLaoXaahXDgAfsFwbQ/TvLZQNQonVwp584VgOodf/6coa2 ZGtDzt73zWCZAM9C8CHq+bHk4DtlAitBQKNpRhlZJk2eOBjZr19BY0su7AznKwJNSuu8 0yA/yLu/E8XqEkhCs4WDW20zfyMMfojAYPgQ7hdaVvTzcJ5/OQjgNB+w24uJfH0vyLZD AGc9hZxzBTajR0TS3RQ6xLOgDFERg0DJaeoeL7r0uDzXUT3D1Am15WpxsMmDsFaLXHSQ DO72nSMYvpD+xCcOAANYwav3Wm8fk3GzQEOxFLEj5Ma2MKrMggk3BW9D1jcO5n1s8kqu yUiA== X-Gm-Message-State: APjAAAWNVBYfpe+7XfuL9HmVHVMwgFiRxY+SFrLlU1/2hVvVZczXI1tY BmItV6OmL6VAWknyohJ3BLTgoaibsR7Tgw== X-Received: by 2002:a62:a515:: with SMTP id v21mr463508pfm.128.1578419973186; Tue, 07 Jan 2020 09:59:33 -0800 (PST) Received: from ubuntu.netflix.com (166.sub-174-194-208.myvzw.com. [174.194.208.166]) by smtp.gmail.com with ESMTPSA id g7sm210324pfq.33.2020.01.07.09.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2020 09:59:32 -0800 (PST) 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: Sargun Dhillon , 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 v9 0/4] Add pidfd_getfd syscall Date: Tue, 7 Jan 2020 09:59:23 -0800 Message-Id: <20200107175927.4558-1-sargun@sargun.me> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 the core functionality, it made sense to make it a syscall which did not require the process to be stopped. Previous versions: V8: https://lore.kernel.org/lkml/20200103162928.5271-1-sargun@sargun.me/ V7: https://lore.kernel.org/lkml/20191226180227.GA29389@ircssh-2.c.rugged-nimbus-611.internal/ V6: https://lore.kernel.org/lkml/20191223210823.GA25083@ircssh-2.c.rugged-nimbus-611.internal/ V5: https://lore.kernel.org/lkml/20191220232746.GA20215@ircssh-2.c.rugged-nimbus-611.internal/ V4: https://lore.kernel.org/lkml/20191218235310.GA17259@ircssh-2.c.rugged-nimbus-611.internal/ V3: https://lore.kernel.org/lkml/20191217005842.GA14379@ircssh-2.c.rugged-nimbus-611.internal/ V2: https://lore.kernel.org/lkml/20191209070446.GA32336@ircssh-2.c.rugged-nimbus-611.internal/ RFC V1: https://lore.kernel.org/lkml/20191205234450.GA26369@ircssh-2.c.rugged-nimbus-611.internal/ Changes since v8: * Cleanup / comments on tests * Split out implementation of syscall vs. arch wiring Changes since v7: * No longer put security_file_recv at the end, and align with other usages of putting it at the end of the file_recv. * Rewrite self-tests in kselftest harness. * Minor refactoring Changes since v6: * Proper attribution of get_task_file helper * Move all types for syscall to int to represent fd 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 (4): vfs, fdtable: Add fget_task helper pid: Implement pidfd_getfd syscall arch: wire up 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 | 4 +- kernel/pid.c | 90 +++++++ tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 2 +- tools/testing/selftests/pidfd/pidfd.h | 9 + .../selftests/pidfd/pidfd_getfd_test.c | 249 ++++++++++++++++++ 27 files changed, 395 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/pidfd/pidfd_getfd_test.c -- 2.20.1