Commit 6752de1a authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'pidfd.v5.16' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux

Pull pidfd updates from Christian Brauner:
 "Various places in the kernel have picked up pidfds.

  The two most recent additions have probably been the ability to use
  pidfds in bpf maps and the usage of pidfds in mm-based syscalls such
  as process_mrelease() and process_madvise().

  The same pattern to turn a pidfd into a struct task exists in two
  places. One of those places used PIDTYPE_TGID while the other one used
  PIDTYPE_PID even though it is clearly documented in all pidfd-helpers
  that pidfds __currently__ only refer to thread-group leaders (subject
  to change in the future if need be).

  This isn't a bug per se but has the potential to be one if we allow
  pidfds to refer to individual threads. If that happens we want to
  audit all codepaths that make use of them to ensure they can deal with
  pidfds refering to individual threads.

  This adds a simple helper to turn a pidfd into a struct task making it
  easy to grep for such places. Plus, it gets rid of code-duplication"

* tag 'pidfd.v5.16' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux:
  mm: use pidfd_get_task()
  pid: add pidfd_get_task() helper
parents 88100752 ee9955d6
...@@ -78,6 +78,7 @@ struct file; ...@@ -78,6 +78,7 @@ 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);
int pidfd_create(struct pid *pid, unsigned int flags); int pidfd_create(struct pid *pid, unsigned int flags);
static inline struct pid *get_pid(struct pid *pid) static inline struct pid *get_pid(struct pid *pid)
......
...@@ -539,6 +539,42 @@ struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags) ...@@ -539,6 +539,42 @@ struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags)
return pid; return pid;
} }
/**
* pidfd_get_task() - Get the task associated with a pidfd
*
* @pidfd: pidfd for which to get the task
* @flags: flags associated with this pidfd
*
* Return the task associated with @pidfd. The function takes a reference on
* the returned task. The caller is responsible for releasing that reference.
*
* Currently, the process identified by @pidfd is always a thread-group leader.
* This restriction currently exists for all aspects of pidfds including pidfd
* creation (CLONE_PIDFD cannot be used with CLONE_THREAD) and pidfd polling
* (only supports thread group leaders).
*
* Return: On success, the task_struct associated with the pidfd.
* On error, a negative errno number will be returned.
*/
struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
{
unsigned int f_flags;
struct pid *pid;
struct task_struct *task;
pid = pidfd_get_pid(pidfd, &f_flags);
if (IS_ERR(pid))
return ERR_CAST(pid);
task = get_pid_task(pid, PIDTYPE_TGID);
put_pid(pid);
if (!task)
return ERR_PTR(-ESRCH);
*flags = f_flags;
return task;
}
/** /**
* pidfd_create() - Create a new pid file descriptor. * pidfd_create() - Create a new pid file descriptor.
* *
......
...@@ -1235,7 +1235,6 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, ...@@ -1235,7 +1235,6 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
struct iovec iovstack[UIO_FASTIOV], iovec; struct iovec iovstack[UIO_FASTIOV], iovec;
struct iovec *iov = iovstack; struct iovec *iov = iovstack;
struct iov_iter iter; struct iov_iter iter;
struct pid *pid;
struct task_struct *task; struct task_struct *task;
struct mm_struct *mm; struct mm_struct *mm;
size_t total_len; size_t total_len;
...@@ -1250,18 +1249,12 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, ...@@ -1250,18 +1249,12 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
if (ret < 0) if (ret < 0)
goto out; goto out;
pid = pidfd_get_pid(pidfd, &f_flags); task = pidfd_get_task(pidfd, &f_flags);
if (IS_ERR(pid)) { if (IS_ERR(task)) {
ret = PTR_ERR(pid); ret = PTR_ERR(task);
goto free_iov; goto free_iov;
} }
task = get_pid_task(pid, PIDTYPE_PID);
if (!task) {
ret = -ESRCH;
goto put_pid;
}
if (!process_madvise_behavior_valid(behavior)) { if (!process_madvise_behavior_valid(behavior)) {
ret = -EINVAL; ret = -EINVAL;
goto release_task; goto release_task;
...@@ -1301,8 +1294,6 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, ...@@ -1301,8 +1294,6 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
mmput(mm); mmput(mm);
release_task: release_task:
put_task_struct(task); put_task_struct(task);
put_pid:
put_pid(pid);
free_iov: free_iov:
kfree(iov); kfree(iov);
out: out:
......
...@@ -1150,21 +1150,14 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) ...@@ -1150,21 +1150,14 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
struct task_struct *p; struct task_struct *p;
unsigned int f_flags; unsigned int f_flags;
bool reap = false; bool reap = false;
struct pid *pid;
long ret = 0; long ret = 0;
if (flags) if (flags)
return -EINVAL; return -EINVAL;
pid = pidfd_get_pid(pidfd, &f_flags); task = pidfd_get_task(pidfd, &f_flags);
if (IS_ERR(pid)) if (IS_ERR(task))
return PTR_ERR(pid); return PTR_ERR(task);
task = get_pid_task(pid, PIDTYPE_TGID);
if (!task) {
ret = -ESRCH;
goto put_pid;
}
/* /*
* Make sure to choose a thread which still has a reference to mm * Make sure to choose a thread which still has a reference to mm
...@@ -1204,8 +1197,6 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) ...@@ -1204,8 +1197,6 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
mmput(mm); mmput(mm);
put_task: put_task:
put_task_struct(task); put_task_struct(task);
put_pid:
put_pid(pid);
return ret; return ret;
#else #else
return -ENOSYS; return -ENOSYS;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment