Re: [PATCH v2 3/3] coredump: hand a pidfd to the usermode coredump helper
From: Christian Brauner
Date: Mon Apr 14 2025 - 10:47:03 EST
On Mon, Apr 14, 2025 at 04:14:50PM +0200, Oleg Nesterov wrote:
> On 04/14, Christian Brauner wrote:
> >
> > -static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
> > +static int umh_coredump_setup(struct subprocess_info *info, struct cred *new)
> > {
> > struct file *files[2];
> > struct coredump_params *cp = (struct coredump_params *)info->data;
> > int err;
> >
> > + if (cp->pid) {
> > + struct file *pidfs_file __free(fput) = NULL;
> > +
> > + pidfs_file = pidfs_alloc_file(cp->pid, 0);
> > + if (IS_ERR(pidfs_file))
> > + return PTR_ERR(pidfs_file);
> > +
> > + /*
> > + * Usermode helpers are childen of either
> > + * system_unbound_wq or of kthreadd. So we know that
> > + * we're starting off with a clean file descriptor
> > + * table. So we should always be able to use
> > + * COREDUMP_PIDFD_NUMBER as our file descriptor value.
> > + */
> > + VFS_WARN_ON_ONCE((pidfs_file = fget_raw(COREDUMP_PIDFD_NUMBER)) != NULL);
> > +
> > + err = replace_fd(COREDUMP_PIDFD_NUMBER, pidfs_file, 0);
> > + if (err < 0)
> > + return err;
>
> Yes, but if replace_fd() succeeds we need to nullify pidfs_file
> to avoid fput from __free(fput) ?
No, since replace_fd() takes its own reference via do_dup2():
replace_fd()
-> do_dup2()
{
get_file(file)
rcu_assign_pointer(fdt->fd[fd], file);
}
so we always need to call it. I had a comment about this in the previous
patchset so people don't get confused. I can add it back.
Let me know if you're happy with this otherwise.
>
> And I think in this case __free(fput) doesn't buy too much, but
> up to you.
>
> Oleg.
>