pidfd: add ioctl to retrieve pid info
PATCH v11 1/1 pidfd: add ioctl to retrieve pid info
From: Luca Boccassi <[email protected]>
A common pattern when using pid fds is having to get information about the process, which currently requires /proc being mounted, resolving the fd to a pid, and then do manual string parsing of /proc/N/status and friends. This needs to be reimplemented over and over in all userspace projects (e.g.: I have reimplemented resolving in systemd, dbus, dbus-daemon, polkit so far), and requires additional care in checking that the fd is still valid after having parsed the data, to avoid races.
Having a programmatic API that can be used directly removes all these requirements, including having /proc mounted.
As discussed at LPC24, add an ioctl with an extensible struct so that more parameters can be added later if needed. Start with returning pid/tgid/ppid and creds unconditionally, and cgroupid optionally.
---
v11: add back flags for PID and CREDS, but always set them unconditionally
always return cgroupid if possible
update comments
v10: do not fail if we cannot get cgroupid, simply skip it (and document it)
v9: drop result_mask and reuse request_mask instead
v8: use RAII guard for rcu, call put_cred()
v7: fix RCU issue and style issue introduced by v6 found by reviewer
v6: use rcu_read_lock() when fetching cgroupid, use task_ppid_nr_ns() to
get the ppid, return ESCHR if any of pid/tgid/ppid are 0 at the end of the call to avoid providing incomplete data, document what the callers should expect
v5: check again that the task hasn't exited immediately before copying
the result out to userspace, to ensure we are not returning stale data add an ifdef around the cgroup structs usage to fix build errors when the feature is disabled
v4: fix arg check in pidfd_ioctl() by moving it after the new call
v3: switch from pid_vnr() to task_pid_vnr()
v2: Apply comments from Christian, apart from the one about pid namespaces
as I need additional hints on how to implement it.
Drop the security_context string as it is not the appropriate metadata to give userspace these days.
fs/pidfs.c | 92 ++++++++++++++++++- include/uapi/linux/pidfd.h | 48 ++++++++++ .../testing/selftests/pidfd/pidfd_open_test.c | 82 ++++++++++++++++- 3 files changed, 218 insertions(+), 4 deletions(-)
fs/pidfs.c +89 -3
@@ -2,6 +2,7 @@#include <linux/anon_inodes.h> #include <linux/file.h> #include <linux/fs.h> +#include <linux/cgroup.h> #include <linux/magic.h> #include <linux/mount.h> #include <linux/pid.h>
@@ -114,6 +115,87 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)return poll_flags; } +static long pidfd_info (struct task_struct *task,unsigned int cmd,unsigned long arg) +{ +struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg; +size_t usize =_IOC_SIZE (cmd); +struct pidfd_info kinfo = {}; +struct user_namespace *user_ns; +const struct cred *c; +__u64 request_mask; +#ifdef CONFIG_CGROUPS +struct cgroup *cgrp; +#endif + +if (!uinfo) +return -EINVAL ; +if (usize <sizeof (struct pidfd_info )) +return -EINVAL ;/* First version, no smaller struct possible */ + +if (copy_from_user (&request_mask, &uinfo->request_mask,sizeof (request_mask))) +return -EFAULT ; + + c =get_task_cred (task); +if (!c) +return -ESRCH ; + +/* Unconditionally return identifiers and credentials, the rest only on request */ + + user_ns =current_user_ns (); + kinfo.ruid =from_kuid_munged (user_ns, c->uid); + kinfo.rgid =from_kgid_munged (user_ns, c->gid); + kinfo.euid =from_kuid_munged (user_ns, c->euid); + kinfo.egid =from_kgid_munged (user_ns, c->egid); + kinfo.suid =from_kuid_munged (user_ns, c->suid); + kinfo.sgid =from_kgid_munged (user_ns, c->sgid); + kinfo.fsuid =from_kuid_munged (user_ns, c->fsuid); + kinfo.fsgid =from_kgid_munged (user_ns, c->fsgid); + kinfo.request_mask |=PIDFD_INFO_CREDS ; +put_cred (c); + +#ifdef CONFIG_CGROUPS +/* +* The cgroup id cannot be retrieved anymore after the task has exited +* (even if it has not been reaped yet), contrary to other fields. Set +* the flag only if we can still access it. */ +rcu_read_lock (); + cgrp =task_cgroup (task, pids_cgrp_id); +if (cgrp) { + kinfo.cgroupid =cgroup_id (cgrp); + kinfo.request_mask |=PIDFD_INFO_CGROUPID ; + } +rcu_read_unlock (); +#endif + +/* +* Copy pid/tgid last, to reduce the chances the information might be +* stale. Note that it is not possible to ensure it will be valid as the +* task might return as soon as the copy_to_user finishes, but that's ok +* and userspace expects that might happen and can act accordingly, so +* this is just best-effort. What we can do however is checking that all +* the fields are set correctly, or return ESRCH to avoid providing +* incomplete information. */ + + kinfo.ppid =task_ppid_nr_ns (task,NULL ); + kinfo.tgid =task_tgid_vnr (task); + kinfo.pid =task_pid_vnr (task); + kinfo.request_mask |=PIDFD_INFO_PID ; + +if (kinfo.pid ==0 || kinfo.tgid ==0 || (kinfo.ppid ==0 && kinfo.pid !=1 )) +return -ESRCH ; + +/* +* If userspace and the kernel have the same struct size it can just +* be copied. If userspace provides an older struct, only the bits that +* userspace knows about will be copied. If userspace provides a new +* struct, only the bits that the kernel knows about will be copied. +*/ +if (copy_to_user (uinfo, &kinfo,min (usize,sizeof (kinfo)))) +return -EFAULT ; + +return 0 ; +} +static long pidfd_ioctl (struct file *file,unsigned int cmd,unsigned long arg) { struct task_struct *task __free(put_task) = NULL;
@@ -122,13 +204,17 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)struct ns_common *ns_common =NULL ;struct pid_namespace *pid_ns; -if (arg) -return -EINVAL ; - task =get_pid_task (pid,PIDTYPE_PID );if (!task)return -ESRCH ; +/* Extensible IOCTL that does not open namespace FDs, take a shortcut */ +if (_IOC_NR (cmd) ==_IOC_NR (PIDFD_GET_INFO )) +return pidfd_info (task, cmd, arg); + +if (arg) +return -EINVAL ; + scoped_guard(task_lock, task) { nsp = task->nsproxy;if (nsp)
include/uapi/linux/pidfd.h +48 -0
59 lines not on this page · raw message
tools/testing/selftests/pidfd/pidfd_open_test.c +81 -1
109 lines not on this page · raw message
base-commit: 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b
Signature · 1 line
2.45.2
kernel test robot
build botkernel test robot noticed the following build errors:
Hi,
kernel test robot noticed the following build errors:
[auto build test ERROR on 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b]
url: https://github.com/intel-lab-lkp/linux/commits/luca-boccassi-gmail-com/pidfd-add-ioctl-to-retrieve-pid-info/20241010-235621 base: 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b patch link: https://lore.kernel.org/r/20241010155401.2268522-1-luca.boccassi%40gmail.com patch subject: [PATCH v11] pidfd: add ioctl to retrieve pid info config: alpha-randconfig-r054-20241013 (https://download.01.org/0day-ci/archive/20241013/[email protected]/config) compiler: alpha-linux-gcc (GCC) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241013/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
fs/pidfs.c: In function 'pidfd_info':
>> fs/pidfs.c:162:34: error: 'pids_cgrp_id' undeclared (first use in this function); did you mean 'misc_cgrp_id'?
162 | cgrp = task_cgroup(task, pids_cgrp_id);
| ^~~~~~~~~~~~
| misc_cgrp_id fs/pidfs.c:162:34: note: each undeclared identifier is reported only once for each function it appears in
vim +162 fs/pidfs.c
129
130 if (!uinfo)
131 return -EINVAL;
132 if (usize < sizeof(struct pidfd_info))
133 return -EINVAL; /* First version, no smaller struct possible */
134
135 if (copy_from_user(&request_mask, &uinfo->request_mask, sizeof(request_mask)))
136 return -EFAULT;
137
138 c = get_task_cred(task);
139 if (!c)
140 return -ESRCH;
141
142 /* Unconditionally return identifiers and credentials, the rest only on request */
143
144 user_ns = current_user_ns();
145 kinfo.ruid = from_kuid_munged(user_ns, c->uid);
146 kinfo.rgid = from_kgid_munged(user_ns, c->gid);
147 kinfo.euid = from_kuid_munged(user_ns, c->euid);
148 kinfo.egid = from_kgid_munged(user_ns, c->egid);
149 kinfo.suid = from_kuid_munged(user_ns, c->suid);
150 kinfo.sgid = from_kgid_munged(user_ns, c->sgid);
151 kinfo.fsuid = from_kuid_munged(user_ns, c->fsuid);
152 kinfo.fsgid = from_kgid_munged(user_ns, c->fsgid);
153 kinfo.request_mask |= PIDFD_INFO_CREDS;
154 put_cred(c);
155
156 #ifdef CONFIG_CGROUPS
157 /*
158 * The cgroup id cannot be retrieved anymore after the task has exited
159 * (even if it has not been reaped yet), contrary to other fields. Set
160 * the flag only if we can still access it. */
161 rcu_read_lock();
> 162 cgrp = task_cgroup(task, pids_cgrp_id);
163 if (cgrp) {
164 kinfo.cgroupid = cgroup_id(cgrp);
165 kinfo.request_mask |= PIDFD_INFO_CGROUPID;
166 }
167 rcu_read_unlock();
168 #endif
169
170 /*
171 * Copy pid/tgid last, to reduce the chances the information might be
172 * stale. Note that it is not possible to ensure it will be valid as the
173 * task might return as soon as the copy_to_user finishes, but that's ok
174 * and userspace expects that might happen and can act accordingly, so
175 * this is just best-effort. What we can do however is checking that all
176 * the fields are set correctly, or return ESRCH to avoid providing
177 * incomplete information. */
178
179 kinfo.ppid = task_ppid_nr_ns(task, NULL);
180 kinfo.tgid = task_tgid_vnr(task);
181 kinfo.pid = task_pid_vnr(task);
182 kinfo.request_mask |= PIDFD_INFO_PID;
183
184 if (kinfo.pid == 0 || kinfo.tgid == 0 || (kinfo.ppid == 0 && kinfo.pid != 1))
185 return -ESRCH;
186
187 /*
188 * If userspace and the kernel have the same struct size it can just
189 * be copied. If userspace provides an older struct, only the bits that
190 * userspace knows about will be copied. If userspace provides a new
191 * struct, only the bits that the kernel knows about will be copied.
192 */
193 if (copy_to_user(uinfo, &kinfo, min(usize, sizeof(kinfo))))
194 return -EFAULT;
195
196 return 0;
197 }
198
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 10 lines · On Thu, 10 Oct 2024 16:52:32 +0100, [email protected] wrote:
A common pattern when using pid fds is having to get information about the process, which currently requires /proc being mounted, resolving the fd to a pid, and then do manual string parsing of
/proc/N/statusand friends. This needs to be reimplemented over and over in all userspace projects (e.g.: I have reimplemented resolving in systemd, dbus, dbus-daemon, polkit so far), and requires additional care in checking that the fd is still valid after having parsed the data, to avoid races.[...]
Applied with some minor changes as mentioned elsewhere.
---
Applied to the vfs.pidfs branch of the vfs/vfs.git tree. Patches in the vfs.pidfs branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.pidfs
[1/1] pidfd: add ioctl to retrieve pid info
https://git.kernel.org/vfs/vfs/c/12506679be68