History

pidfs: allow to retrieve exit information

Some tools like systemd's jounral need to retrieve the exit and cgroup information after a process has already been reaped. This can e.g., happen when retrieving a pidfd via SCM_PIDFD or SCM_PEERPIDFD.

Christian Brauner committed 1 year ago

Commit 7477d7d1 parent 45135222 files · +70 −19Browse files

Unified · Split

2 files
  1. fs/pidfs.c +68 −18
  2. include/uapi/linux/pidfd.h +2 −1
fs/pidfs.c+68−18
@@ -36,7 +36,8 @@ struct pidfs_exit_info { · 35 unmodified lines
36};36};
37 37
38struct pidfs_inode {38struct pidfs_inode {
39 struct pidfs_exit_info exit_info;39 struct pidfs_exit_info __pei;
40 struct pidfs_exit_info *exit_info;
40 struct inode vfs_inode;41 struct inode vfs_inode;
41};42};
42 43
@@ -228,17 +229,28 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) · 185 unmodified lines
228 return poll_flags;229 return poll_flags;
229}230}
230 231
231static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long arg)232static inline bool pid_in_current_pidns(const struct pid *pid)
233{
234 const struct pid_namespace *ns = task_active_pid_ns(current);
235
236 if (ns->level <= pid->level)
237 return pid->numbers[ns->level].ns == ns;
238
239 return false;
240}
241
242static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
232{243{
233 struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg;244 struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg;
245 struct inode *inode = file_inode(file);
246 struct pid *pid = pidfd_pid(file);
234 size_t usize = _IOC_SIZE(cmd);247 size_t usize = _IOC_SIZE(cmd);
235 struct pidfd_info kinfo = {};248 struct pidfd_info kinfo = {};
249 struct pidfs_exit_info *exit_info;
236 struct user_namespace *user_ns;250 struct user_namespace *user_ns;
251 struct task_struct *task;
237 const struct cred *c;252 const struct cred *c;
238 __u64 mask;253 __u64 mask;
239#ifdef CONFIG_CGROUPS
240 struct cgroup *cgrp;
241#endif
242 254
243 if (!uinfo)255 if (!uinfo)
244 return -EINVAL;256 return -EINVAL;
@@ -248,6 +260,37 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · 3 unmodified lines
248 if (copy_from_user(&mask, &uinfo->mask, sizeof(mask)))260 if (copy_from_user(&mask, &uinfo->mask, sizeof(mask)))
249 return -EFAULT;261 return -EFAULT;
250 262
263 /*
264 * Restrict information retrieval to tasks within the caller's pid
265 * namespace hierarchy.
266 */
267 if (!pid_in_current_pidns(pid))
268 return -ESRCH;
269
270 if (mask & PIDFD_INFO_EXIT) {
271 exit_info = READ_ONCE(pidfs_i(inode)->exit_info);
272 if (exit_info) {
273 kinfo.mask |= PIDFD_INFO_EXIT;
274#ifdef CONFIG_CGROUPS
275 kinfo.cgroupid = exit_info->cgroupid;
276 kinfo.mask |= PIDFD_INFO_CGROUPID;
277#endif
278 kinfo.exit_code = exit_info->exit_code;
279 }
280 }
281
282 task = get_pid_task(pid, PIDTYPE_PID);
283 if (!task) {
284 /*
285 * If the task has already been reaped, only exit
286 * information is available
287 */
288 if (!(mask & PIDFD_INFO_EXIT))
289 return -ESRCH;
290
291 goto copy_out;
292 }
293
251 c = get_task_cred(task);294 c = get_task_cred(task);
252 if (!c)295 if (!c)
253 return -ESRCH;296 return -ESRCH;
@@ -267,11 +310,15 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · 13 unmodified lines
267 put_cred(c);310 put_cred(c);
268 311
269#ifdef CONFIG_CGROUPS312#ifdef CONFIG_CGROUPS
270 rcu_read_lock();313 if (!kinfo.cgroupid) {
271 cgrp = task_dfl_cgroup(task);314 struct cgroup *cgrp;
272 kinfo.cgroupid = cgroup_id(cgrp);315
273 kinfo.mask |= PIDFD_INFO_CGROUPID;316 rcu_read_lock();
274 rcu_read_unlock();317 cgrp = task_dfl_cgroup(task);
318 kinfo.cgroupid = cgroup_id(cgrp);
319 kinfo.mask |= PIDFD_INFO_CGROUPID;
320 rcu_read_unlock();
321 }
275#endif322#endif
276 323
277 /*324 /*
@@ -291,6 +338,7 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · 13 unmodified lines
291 if (kinfo.pid == 0 || kinfo.tgid == 0 || (kinfo.ppid == 0 && kinfo.pid != 1))338 if (kinfo.pid == 0 || kinfo.tgid == 0 || (kinfo.ppid == 0 && kinfo.pid != 1))
292 return -ESRCH;339 return -ESRCH;
293 340
341copy_out:
294 /*342 /*
295 * If userspace and the kernel have the same struct size it can just343 * If userspace and the kernel have the same struct size it can just
296 * be copied. If userspace provides an older struct, only the bits that344 * be copied. If userspace provides an older struct, only the bits that
@@ -325,7 +373,6 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) · 28 unmodified lines
325{373{
326 struct task_struct *task __free(put_task) = NULL;374 struct task_struct *task __free(put_task) = NULL;
327 struct nsproxy *nsp __free(put_nsproxy) = NULL;375 struct nsproxy *nsp __free(put_nsproxy) = NULL;
328 struct pid *pid = pidfd_pid(file);
329 struct ns_common *ns_common = NULL;376 struct ns_common *ns_common = NULL;
330 struct pid_namespace *pid_ns;377 struct pid_namespace *pid_ns;
331 378
@@ -340,13 +387,13 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) · 8 unmodified lines
340 return put_user(file_inode(file)->i_generation, argp);387 return put_user(file_inode(file)->i_generation, argp);
341 }388 }
342 389
343 task = get_pid_task(pid, PIDTYPE_PID);
344 if (!task)
345 return -ESRCH;
346
347 /* Extensible IOCTL that does not open namespace FDs, take a shortcut */390 /* Extensible IOCTL that does not open namespace FDs, take a shortcut */
348 if (_IOC_NR(cmd) == _IOC_NR(PIDFD_GET_INFO))391 if (_IOC_NR(cmd) == _IOC_NR(PIDFD_GET_INFO))
349 return pidfd_info(task, cmd, arg);392 return pidfd_info(file, cmd, arg);
393
394 task = get_pid_task(pidfd_pid(file), PIDTYPE_PID);
395 if (!task)
396 return -ESRCH;
350 397
351 if (arg)398 if (arg)
352 return -EINVAL;399 return -EINVAL;
@@ -484,7 +531,7 @@ void pidfs_exit(struct task_struct *tsk) · 131 unmodified lines
484 dentry = stashed_dentry_get(&task_pid(tsk)->stashed);531 dentry = stashed_dentry_get(&task_pid(tsk)->stashed);
485 if (dentry) {532 if (dentry) {
486 struct inode *inode = d_inode(dentry);533 struct inode *inode = d_inode(dentry);
487 struct pidfs_exit_info *exit_info = &pidfs_i(inode)->exit_info;534 struct pidfs_exit_info *exit_info = &pidfs_i(inode)->__pei;
488#ifdef CONFIG_CGROUPS535#ifdef CONFIG_CGROUPS
489 struct cgroup *cgrp;536 struct cgroup *cgrp;
490 537
@@ -495,6 +542,8 @@ void pidfs_exit(struct task_struct *tsk) · 4 unmodified lines
495#endif542#endif
496 exit_info->exit_code = tsk->exit_code;543 exit_info->exit_code = tsk->exit_code;
497 544
545 /* Ensure that PIDFD_GET_INFO sees either all or nothing. */
546 smp_store_release(&pidfs_i(inode)->exit_info, &pidfs_i(inode)->__pei);
498 dput(dentry);547 dput(dentry);
499 }548 }
500}549}
@@ -562,7 +611,8 @@ static struct inode *pidfs_alloc_inode(struct super_block *sb) · 61 unmodified lines
562 if (!pi)611 if (!pi)
563 return NULL;612 return NULL;
564 613
565 memset(&pi->exit_info, 0, sizeof(pi->exit_info));614 memset(&pi->__pei, 0, sizeof(pi->__pei));
615 pi->exit_info = NULL;
566 616
567 return &pi->vfs_inode;617 return &pi->vfs_inode;
568}618}
include/uapi/linux/pidfd.h+2−1
@@ -20,6 +20,7 @@ · 19 unmodified lines
20#define PIDFD_INFO_PID (1UL << 0) /* Always returned, even if not requested */20#define PIDFD_INFO_PID (1UL << 0) /* Always returned, even if not requested */
21#define PIDFD_INFO_CREDS (1UL << 1) /* Always returned, even if not requested */21#define PIDFD_INFO_CREDS (1UL << 1) /* Always returned, even if not requested */
22#define PIDFD_INFO_CGROUPID (1UL << 2) /* Always returned if available, even if not requested */22#define PIDFD_INFO_CGROUPID (1UL << 2) /* Always returned if available, even if not requested */
23#define PIDFD_INFO_EXIT (1UL << 3) /* Only returned if requested. */
23 24
24#define PIDFD_INFO_SIZE_VER0 64 /* sizeof first published struct */25#define PIDFD_INFO_SIZE_VER0 64 /* sizeof first published struct */
25 26
@@ -86,7 +87,7 @@ struct pidfd_info { · 60 unmodified lines
86 __u32 sgid;87 __u32 sgid;
87 __u32 fsuid;88 __u32 fsuid;
88 __u32 fsgid;89 __u32 fsgid;
89 __u32 spare0[1];90 __s32 exit_code;
90};91};
91 92
92#define PIDFS_IOCTL_MAGIC 0xFF93#define PIDFS_IOCTL_MAGIC 0xFF

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t