1

pidfd: cleanup the usage of __pidfd_prepare's flags

- make pidfd_create() static.

- Don't pass O_RDWR | O_CLOEXEC to __pidfd_prepare() in copy_process(),
  __pidfd_prepare() adds these flags unconditionally.

- Kill the flags check in __pidfd_prepare(). sys_pidfd_open() checks the
  flags itself, all other users of pidfd_prepare() pass flags = 0.

  If we need a sanity check for those other in kernel users then
  WARN_ON_ONCE(flags & ~PIDFD_NONBLOCK) makes more sense.

- Don't pass O_RDWR to get_unused_fd_flags(), it ignores everything except
  O_CLOEXEC.

- Don't pass O_CLOEXEC to anon_inode_getfile(), it ignores everything except
  O_ACCMODE | O_NONBLOCK.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Link: https://lore.kernel.org/r/20240125161734.GA778@redhat.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Oleg Nesterov 2024-01-25 17:17:34 +01:00 committed by Christian Brauner
parent b639585e71
commit cdefbf2324
3 changed files with 4 additions and 8 deletions

View File

@ -73,7 +73,6 @@ struct file;
extern struct pid *pidfd_pid(const struct file *file); extern struct pid *pidfd_pid(const struct file *file);
struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags); struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags);
struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags); struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags);
int pidfd_create(struct pid *pid, unsigned int flags);
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret); int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret);
static inline struct pid *get_pid(struct pid *pid) static inline struct pid *get_pid(struct pid *pid)

View File

@ -2130,15 +2130,12 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re
int pidfd; int pidfd;
struct file *pidfd_file; struct file *pidfd_file;
if (flags & ~(O_NONBLOCK | O_RDWR | O_CLOEXEC)) pidfd = get_unused_fd_flags(O_CLOEXEC);
return -EINVAL;
pidfd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
if (pidfd < 0) if (pidfd < 0)
return pidfd; return pidfd;
pidfd_file = anon_inode_getfile("[pidfd]", &pidfd_fops, pid, pidfd_file = anon_inode_getfile("[pidfd]", &pidfd_fops, pid,
flags | O_RDWR | O_CLOEXEC); flags | O_RDWR);
if (IS_ERR(pidfd_file)) { if (IS_ERR(pidfd_file)) {
put_unused_fd(pidfd); put_unused_fd(pidfd);
return PTR_ERR(pidfd_file); return PTR_ERR(pidfd_file);
@ -2524,7 +2521,7 @@ __latent_entropy struct task_struct *copy_process(
*/ */
if (clone_flags & CLONE_PIDFD) { if (clone_flags & CLONE_PIDFD) {
/* Note that no task has been attached to @pid yet. */ /* Note that no task has been attached to @pid yet. */
retval = __pidfd_prepare(pid, O_RDWR | O_CLOEXEC, &pidfile); retval = __pidfd_prepare(pid, 0, &pidfile);
if (retval < 0) if (retval < 0)
goto bad_fork_free_pid; goto bad_fork_free_pid;
pidfd = retval; pidfd = retval;

View File

@ -595,7 +595,7 @@ struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
* Return: On success, a cloexec pidfd is returned. * Return: On success, a cloexec pidfd is returned.
* On error, a negative errno number will be returned. * On error, a negative errno number will be returned.
*/ */
int pidfd_create(struct pid *pid, unsigned int flags) static int pidfd_create(struct pid *pid, unsigned int flags)
{ {
int pidfd; int pidfd;
struct file *pidfd_file; struct file *pidfd_file;