Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5723750img; Wed, 27 Mar 2019 14:03:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzwValkVI1mRyO7012rJ8K/oaKLoOfIWwPcBwqrXyvTMTQTcV+XPa6DFV2HMn+eqidupuhA X-Received: by 2002:a63:fa01:: with SMTP id y1mr35992200pgh.5.1553720614976; Wed, 27 Mar 2019 14:03:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553720614; cv=none; d=google.com; s=arc-20160816; b=k3cpOze5eZpKWH0b5UDLvTD8gtmkftDq6PNL4+OP42Iw72LuIM/9Hj6r2icjnto367 eE3Ko6QOShCjjjsumE7GD0sRtQhPcgClBNx0t1jH3bruYsz6GA5c/+dkowmoWggwoVuw 2PNbrOMPxDi1xHoLJKEO0J/jNZ/cltp7M4xP2f/y8Wplgv2MTNhvIc55EC/CRPMCjNgo eiD6u6jX+dqKUpTdg0A6KGGjOi7f8LdHlwoW1gkVm0JqN5A6+FZnanWdlZVJ/Y+BAzH0 xHmfqzJo8/WuVsiX6AkhgNKuZbKaNVAwHp6Br0n+gJVUv1lnGRPMvZIUdEUG37EISt3S Ukpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=LlIEBLkG/ptrBRITODZ2BGZimwHWFV6sCRCJ6XJ1NOY=; b=cA2sY8bFGM1iGb5GlDHZHUIzfUm2x2T7BsuoMw0FOdbIf5RplZq7Ff2GRldt4fmRBd V+zRsdmLWV8rC31dqnZFCQgGql8LAlty8mktKGl86B+G4EzoP05XnEhGSGhjpw8L3qFp GN9gyJolWvG9tMergftR6ouk0Xf/OvZRWKF1h8QHgGBrbVyVXzpBt3Huq1Dp2ya8syHF hMovwgcDUJHW/T/cV/LDsn6rES7bqDwpUaz9WcEJMNrOk9F5BxCf/VT/GYUupk5KZD/5 3gdklDGzn/H2sX9DF0hoHXrZSeh9nbIw3CXJ1TolnQ5I3j0uxKidARLtWKMtE2p5+37R 5yOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brauner.io header.s=google header.b="YYb7ELx/"; 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 s26si5705305pgm.223.2019.03.27.14.03.19; Wed, 27 Mar 2019 14:03:34 -0700 (PDT) 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=@brauner.io header.s=google header.b="YYb7ELx/"; 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 S1727834AbfC0VCk (ORCPT + 99 others); Wed, 27 Mar 2019 17:02:40 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:33806 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726891AbfC0VCk (ORCPT ); Wed, 27 Mar 2019 17:02:40 -0400 Received: by mail-ed1-f67.google.com with SMTP id x14so12685628eds.1 for ; Wed, 27 Mar 2019 14:02:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brauner.io; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=LlIEBLkG/ptrBRITODZ2BGZimwHWFV6sCRCJ6XJ1NOY=; b=YYb7ELx/b45ech/Zsx3KjBt8HMpg12fQVJohywpSOX477Wf2DgZfKJWayTSkoJxTn0 RIqRA5vfo9/sVoH1POf65NcuH9NcQa+R0fFxZvvPtePo9LSsteqPRNJxDYvpVHSv5wW+ +x4aDJQIwmHVKIt4m6fT/oWWwEbKXs2VPRPRUhNwWPRstM1miPg9DeTU0qH56WBV8FF0 uz3MKldOk/kptZR4OhI1Hx8vvs1Hane3yvcnVlvhDp7+BPmt748f6XIXw7ndpiqk5VAC zdYivONYW8p4dSTwczWU6hMv8L55Sb9fIoKLJmkhzy0kPuWlmS1waKM0A2aLslBKsDhZ GIsw== 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:references :mime-version:content-disposition:in-reply-to:user-agent; bh=LlIEBLkG/ptrBRITODZ2BGZimwHWFV6sCRCJ6XJ1NOY=; b=kzpjg8BbZ/Dyqb0K77XbtupAtHgDpd0NOUNMowgUfHcIEG6XNpX8eB6FDVdPL2QRUo JP/y2oqV7ndONgT4B61NtmERh8D84Bvgucgdxwtn7Tm/S9HULDvov9pUSZVXtLEDoceH B3DgDYdW5mgi43NiQ6UtiZ/hhRULvEu7lEVP1T2Hn0JffTOluuIPW0w0+vrWdKS+Aic8 0jkdzRhAO0f+J4jNtYfuEuDNSz6eWlVF6widBtxYSLYZq/afjQ0D0oFTAJ/NeSFqA+Js f607rvQRH1tS+Vv8RIjVPOUqvsrySM4Hoq6giwkqyxhsi1+Bj0IqziGmMQmLw7qtr9MS Tm1g== X-Gm-Message-State: APjAAAX6QAlHeMfjnBwc34uh7rE8Wa9X7idML1JejIttbf/pyrZvDzOf L4GGYtQVy0Wu2vnZfImEyiM1Xg== X-Received: by 2002:a17:906:4bc3:: with SMTP id x3mr19989451ejv.150.1553720557662; Wed, 27 Mar 2019 14:02:37 -0700 (PDT) Received: from brauner.io ([2a02:8109:b6bf:d24a:b136:35b0:7c8c:280a]) by smtp.gmail.com with ESMTPSA id e2sm4872787ejc.53.2019.03.27.14.02.36 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 27 Mar 2019 14:02:37 -0700 (PDT) Date: Wed, 27 Mar 2019 22:02:35 +0100 From: Christian Brauner To: Jann Horn Cc: Konstantin Khlebnikov , Andy Lutomirski , David Howells , "Serge E. Hallyn" , "Eric W. Biederman" , Linux API , kernel list , Arnd Bergmann , Kees Cook , Alexey Dobriyan , Thomas Gleixner , Michael Kerrisk-manpages , Jonathan Kowalski , "Dmitry V. Levin" , Andrew Morton , Oleg Nesterov , Nagarathnam Muthusamy , Aleksa Sarai , Al Viro , "Joel Fernandes (Google)" , Daniel Colascione Subject: Re: [PATCH 2/4] pid: add pidfd_open() Message-ID: <20190327210234.po2t3m3575tqdcac@brauner.io> References: <20190327162147.23198-1-christian@brauner.io> <20190327162147.23198-3-christian@brauner.io> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 27, 2019 at 06:07:54PM +0100, Jann Horn wrote: > On Wed, Mar 27, 2019 at 5:22 PM Christian Brauner wrote: > > pidfd_open() allows to retrieve pidfds for processes and removes the > > dependency of pidfd on procfs. Multiple people have expressed a desire to > > do this even when pidfd_send_signal() was merged. It is even recorded in > [...] > > IF PROCFD_TO_PIDFD is passed as a flag together with a file descriptor to a > > /proc mount in a given pid namespace and a pidfd pidfd_open() will return a > > file descriptor to the corresponding /proc/ directory in procfs > > mounts' pid namespace. pidfd_open() is very careful to verify that the pid > > nit: s/mounts'/mount's/ Thanks. > > > hasn't been recycled in between. > > IF PIDFD_TO_PROCFD is passed as a flag together with a file descriptor > > referencing a /proc/ directory a pidfd referencing the struct pid > > stashed in /proc/ of the process will be returned. > > nit: s/of the process //? Yes. > > > The pidfd_open() syscalls in that manner resembles openat() as it uses a > > nit: s/syscalls/syscall/ Thanks. > > [...] > > diff --git a/kernel/pid.c b/kernel/pid.c > > index 20881598bdfa..c9e24e726aba 100644 > > --- a/kernel/pid.c > > +++ b/kernel/pid.c > [...] > > +static struct file *pidfd_open_proc_pid(const struct file *procf, pid_t pid, > > + const struct pid *pidfd_pid) > > +{ > > + char name[11]; /* int to strlen + \0 */ > > nit: The comment is a bit off; an unconstrained int needs 1+10+1 > bytes, I think? minus sign, 10 digits, nullbyte? But of course that > can't actually happen here. Yes, the comment is misleading. > > > + struct file *file; > > + struct pid *proc_pid; > > + > > + snprintf(name, sizeof(name), "%d", pid); > > + file = file_open_root(procf->f_path.dentry, procf->f_path.mnt, name, > > + O_DIRECTORY | O_NOFOLLOW, 0); > > Maybe explicitly write the implied O_RDONLY (which is 0) here for clarity? Yes. > > [...] > > +static int pidfd_to_procfd(pid_t pid, int procfd, int pidfd) > > +{ > > + long fd; > > + pid_t ns_pid; > > + struct fd fdproc, fdpid; > > + struct file *file = NULL; > > + struct pid *pidfd_pid = NULL; > > + struct pid_namespace *proc_pid_ns = NULL; > > + > > + fdproc = fdget(procfd); > > + if (!fdproc.file) > > + return -EBADF; > > + > > + fdpid = fdget(pidfd); > > + if (!fdpid.file) { > > + fdput(fdpid); > > Typo: s/fdput(fdpid)/fdput(fdproc)/ Good catch! > > [...] > > +SYSCALL_DEFINE4(pidfd_open, pid_t, pid, int, procfd, int, pidfd, unsigned int, > > + flags) > [...] > > + if (!flags) { > [...] > > + rcu_read_lock(); > > + pidfd_pid = get_pid(find_pid_ns(pid, task_active_pid_ns(current))); > > + rcu_read_unlock(); > > The previous three lines are equivalent to `pidfd_pid = find_get_pid(pid)`. Perfect, will replace. > > > + fd = pidfd_create_fd(pidfd_pid, O_CLOEXEC); > > Nit: You could hardcode O_CLOEXEC in pidfd_create_fd() and get rid of > the second function argument if you want to. Hm, let me rename this to pidfd_create_cloexec(pidfd_pid) then. > > > + put_pid(pidfd_pid); > > + } else if (flags & PIDFD_TO_PROCFD) { > [...] > > + fd = pidfd_to_procfd(pid, procfd, pidfd); > > The `pid` argument of pidfd_to_procfd() looks unused, maybe it makes > sense to get rid of that? Yes.