Skip to contentSkip to conversation

pidfd: add ioctl to retrieve pid info

PATCH Superseded [email protected] 2 participants 2 messages Oct 4, 2024 – Oct 5, 2024

Subsystems (3)

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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.

---
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                                    | 70 +++++++++++++++-
 include/uapi/linux/pidfd.h                    | 24 ++++++
 .../testing/selftests/pidfd/pidfd_open_test.c | 81 ++++++++++++++++++-
 3 files changed, 171 insertions(+), 4 deletions(-)
fs/pidfs.c +67 -3
index 7ffdc88dfb52..9ef63c1053d4 100644
@@ -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,65 @@ 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;
+
+	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 */
+
+	kinfo.pid = task_pid_vnr(task);
+	kinfo.tgid = task_tgid_vnr(task);
+	kinfo.ppid = task_ppid_nr_ns(task, task_active_pid_ns(task));
+
+	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);
+
+	if (request_mask & PIDFD_INFO_CGROUPID) {
+		struct cgroup *cgrp = task_css_check(task, pids_cgrp_id, 1)->cgroup;
+		if (!cgrp)
+			return -ENODEV;
+
+		kinfo.cgroupid = cgroup_id(cgrp);
+		kinfo.result_mask |= PIDFD_INFO_CGROUPID;
+	}
+
+	/*
+	 * 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 and
+	 * the size value will be set to the size the kernel knows about.
+	 */
+	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;
@@ -121,13 +181,17 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	struct pid *pid = pidfd_pid(file);
 	struct ns_common *ns_common = NULL;
 
-	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 +24 -0
index 565fc0629fff..f278db1a3fe8 100644
@@ -16,6 +16,29 @@
 #define PIDFD_SIGNAL_THREAD_GROUP	(1UL << 1)
 #define PIDFD_SIGNAL_PROCESS_GROUP	(1UL << 2)
 
+/* Flags for pidfd_info. */
+#define PIDFD_INFO_CGROUPID		(1UL << 0)
+
+struct pidfd_info {
+	/* Let userspace request expensive stuff explictly. */
+	__u64 request_mask;
+	/* And let the kernel indicate whether it knows about it. */
+	__u64 result_mask;
+	__u64 cgroupid;
+	__u32 pid;
+	__u32 tgid;
+	__u32 ppid;
+	__u32 ruid;
+	__u32 rgid;
+	__u32 euid;
+	__u32 egid;
+	__u32 suid;
+	__u32 sgid;
+	__u32 fsuid;
+	__u32 fsgid;
+	__u32 spare0[1];
+};
+
 #define PIDFS_IOCTL_MAGIC 0xFF
 
 #define PIDFD_GET_CGROUP_NAMESPACE            _IO(PIDFS_IOCTL_MAGIC, 1)
@@ -28,5 +51,6 @@
 #define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8)
 #define PIDFD_GET_USER_NAMESPACE              _IO(PIDFS_IOCTL_MAGIC, 9)
 #define PIDFD_GET_UTS_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 10)
+#define PIDFD_GET_INFO                        _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info)
 
 #endif /* _UAPI_LINUX_PIDFD_H */
tools/testing/selftests/pidfd/pidfd_open_test.c +80 -1

108 lines not on this page · raw message

base-commit: 9852d85ec9d492ebef56dc5f229416c925758edc

Signature · 1 line
2.45.2
#1 lore raw
kernel test robot noticed the following build errors:

Hi,

kernel test robot noticed the following build errors:

[auto build test ERROR on 9852d85ec9d492ebef56dc5f229416c925758edc]

url:    https://github.com/intel-lab-lkp/linux/commits/luca-boccassi-gmail-com/pidfd-add-ioctl-to-retrieve-pid-info/20241005-041639
base:   9852d85ec9d492ebef56dc5f229416c925758edc
patch link:    https://lore.kernel.org/r/20241004194751.215507-1-luca.boccassi%40gmail.com
patch subject: [PATCH v4] pidfd: add ioctl to retrieve pid info
config: arm-randconfig-002-20241006 (https://download.01.org/0day-ci/archive/20241006/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241006/[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:156:25: error: implicit declaration of function 'task_css_check' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                   struct cgroup *cgrp = task_css_check(task, pids_cgrp_id, 1)->cgroup;
                                         ^
   fs/pidfs.c:156:64: error: member reference type 'int' is not a pointer
                   struct cgroup *cgrp = task_css_check(task, pids_cgrp_id, 1)->cgroup;
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ^
   fs/pidfs.c:156:46: error: use of undeclared identifier 'pids_cgrp_id'
                   struct cgroup *cgrp = task_css_check(task, pids_cgrp_id, 1)->cgroup;
                                                              ^

3 errors generated.

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for GET_FREE_REGION Depends on [n]: SPARSEMEM [=n]
Selected by [m]:
- RESOURCE_KUNIT_TEST [=m] && RUNTIME_TESTING_MENU [=y] && KUNIT [=m]

vim +/task_css_check +156 fs/pidfs.c

117
118 static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long arg)
119 {
120 struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg;
121 size_t usize = _IOC_SIZE(cmd);
122 struct pidfd_info kinfo = {};
123 struct user_namespace *user_ns;
124 const struct cred *c;
125 __u64 request_mask;
126
127 if (!uinfo)
128 return -EINVAL;
129 if (usize < sizeof(struct pidfd_info))
130 return -EINVAL; /* First version, no smaller struct possible */
131
132 if (copy_from_user(&request_mask, &uinfo->request_mask, sizeof(request_mask)))
133 return -EFAULT;
134
135 c = get_task_cred(task);
136 if (!c)
137 return -ESRCH;
138
139 /* Unconditionally return identifiers and credentials, the rest only on request */
140
141 kinfo.pid = task_pid_vnr(task);
142 kinfo.tgid = task_tgid_vnr(task);
143 kinfo.ppid = task_ppid_nr_ns(task, task_active_pid_ns(task));
144
145 user_ns = current_user_ns();
146 kinfo.ruid = from_kuid_munged(user_ns, c->uid);
147 kinfo.rgid = from_kgid_munged(user_ns, c->gid);
148 kinfo.euid = from_kuid_munged(user_ns, c->euid);
149 kinfo.egid = from_kgid_munged(user_ns, c->egid);
150 kinfo.suid = from_kuid_munged(user_ns, c->suid);
151 kinfo.sgid = from_kgid_munged(user_ns, c->sgid);
152 kinfo.fsuid = from_kuid_munged(user_ns, c->fsuid);
153 kinfo.fsgid = from_kgid_munged(user_ns, c->fsgid);
154
155 if (request_mask & PIDFD_INFO_CGROUPID) {

 > 156			struct cgroup *cgrp = task_css_check(task, pids_cgrp_id, 1)->cgroup;

157 if (!cgrp)
158 return -ENODEV;
159
160 kinfo.cgroupid = cgroup_id(cgrp);
161 kinfo.result_mask |= PIDFD_INFO_CGROUPID;
162 }
163
164 /*
165 * If userspace and the kernel have the same struct size it can just
166 * be copied. If userspace provides an older struct, only the bits that
167 * userspace knows about will be copied. If userspace provides a new
168 * struct, only the bits that the kernel knows about will be copied and
169 * the size value will be set to the size the kernel knows about.
170 */
171 if (copy_to_user(uinfo, &kinfo, min(usize, sizeof(kinfo))))
172 return -EFAULT;
173
174 return 0;
175 }
176

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki