mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Erin Shepherd <erin.shepherd@e43.eu>
To: Amir Goldstein <amir73il@gmail.com>
Cc: Christian Brauner <brauner@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>, Jan Kara <jack@suse.cz>,
	Chuck Lever <chuck.lever@oracle.com>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Jeff Layton <jlayton@kernel.org>,
	linux-nfs@vger.kernel.org
Subject: Re: [PATCH v2 3/3] pidfs: implement file handle support
Date: Thu, 14 Nov 2024 13:42:26 +0100	[thread overview]
Message-ID: <600bbf1c-5225-4e24-8c93-26fa46fd7990@e43.eu> (raw)
In-Reply-To: <CAOQ4uxh2HWkVE_aMeYSTsYRO9_sKMPH7V2uksWFSo3ucWOoJ2g@mail.gmail.com>


On 14/11/2024 08:07, Amir Goldstein wrote:
> On Wed, Nov 13, 2024 at 7:11 PM Erin Shepherd <erin.shepherd@e43.eu> wrote:
>> On 64-bit platforms, userspace can read the pidfd's inode in order to
>> get a never-repeated PID identifier. On 32-bit platforms this identifier
>> is not exposed, as inodes are limited to 32 bits. Instead expose the
>> identifier via export_fh, which makes it available to userspace via
>> name_to_handle_at
>>
>> In addition we implement fh_to_dentry, which allows userspace to
>> recover a pidfd from a PID file handle.
> "In addition" is a good indication that a separate patch was a good idea..
>
>> We stash the process' PID in the root pid namespace inside the handle,
>> and use that to recover the pid (validating that pid->ino matches the
>> value in the handle, i.e. that the pid has not been reused).
>>
>> We use the root namespace in order to ensure that file handles can be
>> moved across namespaces; however, we validate that the PID exists in
>> the current namespace before returning the inode.
>>
>> Reviewed-by: Amir Goldstein <amir73il@gmail.com>
> This patch has changed enough that you should not have kept my RVB.
>
> BTW, please refrain from using the same subject for the cover letter and
> a single patch. They are not the same thing, so if they get the same
> name, one of them has an inaccurate description.
>
ACK to all three.


>> Signed-off-by: Erin Shepherd <erin.shepherd@e43.eu>
>> ---
>>  fs/pidfs.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>>  1 file changed, 61 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/pidfs.c b/fs/pidfs.c
>> index 80675b6bf88459c22787edaa68db360bdc0d0782..0684a9b8fe71c5205fb153b2714bc9c672045fd5 100644
>> --- a/fs/pidfs.c
>> +++ b/fs/pidfs.c
>> @@ -1,5 +1,6 @@
>>  // SPDX-License-Identifier: GPL-2.0
>>  #include <linux/anon_inodes.h>
>> +#include <linux/exportfs.h>
>>  #include <linux/file.h>
>>  #include <linux/fs.h>
>>  #include <linux/magic.h>
>> @@ -347,11 +348,69 @@ static const struct dentry_operations pidfs_dentry_operations = {
>>         .d_prune        = stashed_dentry_prune,
>>  };
>>
>> +#define PIDFD_FID_LEN 3
>> +
>> +struct pidfd_fid {
>> +       u64 ino;
>> +       s32 pid;
>> +} __packed;
>> +
>> +static int pidfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
>> +                          struct inode *parent)
>> +{
>> +       struct pid *pid = inode->i_private;
>> +       struct pidfd_fid *fid = (struct pidfd_fid *)fh;
>> +
>> +       if (*max_len < PIDFD_FID_LEN) {
>> +               *max_len = PIDFD_FID_LEN;
>> +               return FILEID_INVALID;
>> +       }
>> +
>> +       fid->ino = pid->ino;
>> +       fid->pid = pid_nr(pid);
>> +       *max_len = PIDFD_FID_LEN;
>> +       return FILEID_INO64_GEN;
>> +}
>> +
>> +static struct dentry *pidfs_fh_to_dentry(struct super_block *sb,
>> +                                        struct fid *gen_fid,
>> +                                        int fh_len, int fh_type)
>> +{
>> +       int ret;
>> +       struct path path;
>> +       struct pidfd_fid *fid = (struct pidfd_fid *)gen_fid;
>> +       struct pid *pid;
>> +
>> +       if (fh_type != FILEID_INO64_GEN || fh_len < PIDFD_FID_LEN)
>> +               return NULL;
>> +
>> +       scoped_guard(rcu) {
>> +               pid = find_pid_ns(fid->pid, &init_pid_ns);
>> +               if (!pid || pid->ino != fid->ino || pid_vnr(pid) == 0)
>> +                       return NULL;
>> +
>> +               pid = get_pid(pid);
>> +       }
>> +
>> +       ret = path_from_stashed(&pid->stashed, pidfs_mnt, pid, &path);
>> +       if (ret < 0)
>> +               return ERR_PTR(ret);
> How come no need to put_pid() in case of error?

This one confused me at first too, but path_from_stashed frees it (via
stashed_ops.put_data) on error. You can see the same pattern in
pidfs_alloc_file.

(It already needs to know how to free it for the case where a stashed
dentry already exists)

>> +
>> +       mntput(path.mnt);
>> +       return path.dentry;
>> +}
>> +
>> +static const struct export_operations pidfs_export_operations = {
>> +       .encode_fh = pidfs_encode_fh,
>> +       .fh_to_dentry = pidfs_fh_to_dentry,
>> +       .flags = EXPORT_OP_UNRESTRICTED_OPEN,
>> +};
>> +
>>  static int pidfs_init_inode(struct inode *inode, void *data)
>>  {
>>         inode->i_private = data;
>>         inode->i_flags |= S_PRIVATE;
>> -       inode->i_mode |= S_IRWXU;
>> +       inode->i_mode |= S_IRWXU | S_IRWXG | S_IRWXO;
> This change is not explained.
> Why is it here?

open_by_handle_at eventually passes through the may_open permission check.
The existing permissions only permits root to open them (since the owning
uid & gid is 0). So, it was necessary to widen them to align with how
pidfd_open works.

If I stick with this approach (see [1]) I'll ensure to document this change
better in the commit message.

[1] https://lore.kernel.org/all/6a3ed633-311d-47ff-8a7e-5121d6186139@e43.eu/



  reply	other threads:[~2024-11-14 12:42 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-01 13:54 [PATCH 0/4] " Erin Shepherd
2024-11-01 13:54 ` [PATCH 1/4] pseudofs: add support for export_ops Erin Shepherd
2024-11-12 15:56   ` Amir Goldstein
2024-11-01 13:54 ` [PATCH 2/4] pidfs: implement file handle export support Erin Shepherd
2024-11-12 15:55   ` Amir Goldstein
2024-11-01 13:54 ` [PATCH 3/4] pid: introduce find_get_pid_ns Erin Shepherd
2024-11-12 15:59   ` Amir Goldstein
2024-11-01 13:54 ` [PATCH 4/4] pidfs: implement fh_to_dentry Erin Shepherd
2024-11-12 16:33   ` Amir Goldstein
2024-11-12 23:51   ` Jeff Layton
2024-11-13  8:01     ` Amir Goldstein
2024-11-13 10:11       ` Erin Shepherd
2024-11-13 12:21         ` Christian Brauner
2024-11-13 12:09   ` Christian Brauner
2024-11-13 13:06     ` Erin Shepherd
2024-11-13 13:26       ` Christian Brauner
2024-11-13 13:48         ` Erin Shepherd
2024-11-14 10:29           ` Christian Brauner
2024-11-14 12:21             ` Erin Shepherd
2024-11-12 13:10 ` [PATCH 0/4] pidfs: implement file handle support Christian Brauner
2024-11-12 13:57   ` Jeff Layton
2024-11-12 22:43     ` Erin Shepherd
2024-11-13  0:37       ` Darrick J. Wong
2024-11-13 11:35       ` Christian Brauner
2024-11-13 17:55       ` [PATCH v2 0/3] " Erin Shepherd
2024-11-13 17:55         ` [PATCH v2 1/3] pseudofs: add support for export_ops Erin Shepherd
2024-11-13 17:55         ` [PATCH v2 2/3] exportfs: allow fs to disable CAP_DAC_READ_SEARCH check Erin Shepherd
2024-11-13 22:50           ` kernel test robot
2024-11-14  1:29           ` kernel test robot
2024-11-14  4:37           ` Christoph Hellwig
2024-11-14 12:56             ` Erin Shepherd
2024-11-14  6:37           ` Amir Goldstein
2024-11-14 14:16             ` Christian Brauner
2024-11-13 17:55         ` [PATCH v2 3/3] pidfs: implement file handle support Erin Shepherd
2024-11-14  7:07           ` Amir Goldstein
2024-11-14 12:42             ` Erin Shepherd [this message]
2024-11-14 12:52           ` Christian Brauner
2024-11-14 13:13             ` Erin Shepherd
2024-11-14 14:13               ` Christian Brauner
2024-11-14 21:52                 ` Erin Shepherd
2024-11-15  7:50                   ` Amir Goldstein
2024-11-14  7:02         ` [PATCH v2 0/3] " Amir Goldstein
2024-11-14 12:48           ` Erin Shepherd
2024-11-14 14:27             ` Christian Brauner
2024-11-28 12:33               ` [PATCH RFC 0/2] pidfs: file handle preliminaries Christian Brauner
2024-11-28 12:33                 ` [PATCH RFC 1/2] pidfs: rework inode number allocation Christian Brauner
2024-11-28 17:19                   ` Amir Goldstein
2024-11-28 12:33                 ` [PATCH RFC 2/2] pidfs: remove 32bit inode number handling Christian Brauner
2024-11-28 17:06                 ` [PATCH RFC 0/2] pidfs: file handle preliminaries Amir Goldstein
2024-11-14 16:10             ` [PATCH v2 0/3] pidfs: implement file handle support Amir Goldstein
2024-11-12 23:03   ` [PATCH 0/4] " Erin Shepherd
2024-11-13  0:40     ` Darrick J. Wong
2024-11-13 10:17       ` Erin Shepherd
2024-11-13 13:29         ` Jeff Layton
2024-11-13 14:41           ` Chuck Lever III
2024-11-14 10:39             ` Christian Brauner
2024-11-14  6:55           ` Amir Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=600bbf1c-5225-4e24-8c93-26fa46fd7990@e43.eu \
    --to=erin.shepherd@e43.eu \
    --cc=amir73il@gmail.com \
    --cc=brauner@kernel.org \
    --cc=chuck.lever@oracle.com \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®