xref: /linux/fs/pidfs.c (revision 26ba30221c03364d6ed9910be8da4c1fd871b07b)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/anon_inodes.h>
3 #include <linux/compat.h>
4 #include <linux/exportfs.h>
5 #include <linux/file.h>
6 #include <linux/fs.h>
7 #include <linux/cgroup.h>
8 #include <linux/magic.h>
9 #include <linux/mount.h>
10 #include <linux/pid.h>
11 #include <linux/pidfs.h>
12 #include <linux/sched/signal.h>
13 #include <linux/signal.h>
14 #include <linux/pid_namespace.h>
15 #include <linux/poll.h>
16 #include <linux/proc_fs.h>
17 #include <linux/proc_ns.h>
18 #include <linux/pseudo_fs.h>
19 #include <linux/ptrace.h>
20 #include <linux/seq_file.h>
21 #include <uapi/linux/pidfd.h>
22 #include <linux/ipc_namespace.h>
23 #include <linux/time_namespace.h>
24 #include <linux/utsname.h>
25 #include <net/net_namespace.h>
26 #include <linux/coredump.h>
27 #include <linux/rhashtable.h>
28 #include <linux/llist.h>
29 #include <linux/xattr.h>
30 #include <linux/cookie.h>
31 
32 #include "internal.h"
33 #include "mount.h"
34 
35 #define PIDFS_PID_DEAD ERR_PTR(-ESRCH)
36 
37 static struct kmem_cache *pidfs_attr_cachep __ro_after_init;
38 
39 static struct path pidfs_root_path = {};
40 
41 static struct simple_xattr_cache pidfs_xa_cache;
42 
43 void pidfs_get_root(struct path *path)
44 {
45 	*path = pidfs_root_path;
46 	path_get(path);
47 }
48 
49 enum pidfs_attr_mask_bits {
50 	PIDFS_ATTR_BIT_EXIT	= 0,
51 	PIDFS_ATTR_BIT_COREDUMP	= 1,
52 };
53 
54 struct pidfs_anon_attr {
55 	unsigned long attr_mask;
56 	struct /* exit info */ {
57 		__u64 cgroupid;
58 		__s32 exit_code;
59 	};
60 	__u32 coredump_mask;
61 	__u32 coredump_signal;
62 	__u32 coredump_code;
63 };
64 
65 static struct rhashtable pidfs_ino_ht;
66 
67 static const struct rhashtable_params pidfs_ino_ht_params = {
68 	.key_offset		= offsetof(struct pid, ino),
69 	.key_len		= sizeof(u64),
70 	.head_offset		= offsetof(struct pid, pidfs_hash),
71 	.automatic_shrinking	= true,
72 };
73 
74 /*
75  * inode number handling
76  *
77  * On 64 bit nothing special happens. The 64bit number assigned
78  * to struct pid is the inode number.
79  *
80  * On 32 bit the 64 bit number assigned to struct pid is split
81  * into two 32 bit numbers. The lower 32 bits are used as the
82  * inode number and the upper 32 bits are used as the inode
83  * generation number.
84  *
85  * On 32 bit pidfs_ino() will return the lower 32 bit. When
86  * pidfs_ino() returns zero a wrap around happened. When a
87  * wraparound happens the 64 bit number will be incremented by 1
88  * so inode numbering starts at 1 again.
89  *
90  * On 64 bit comparing two pidfds is as simple as comparing
91  * inode numbers.
92  *
93  * When a wraparound happens on 32 bit multiple pidfds with the
94  * same inode number are likely to exist (This isn't a problem
95  * since before pidfs pidfds used the anonymous inode meaning
96  * all pidfds had the same inode number.). Userspace can
97  * reconstruct the 64 bit identifier by retrieving both the
98  * inode number and the inode generation number to compare or
99  * use file handles.
100  */
101 struct pidfs_attr {
102 	struct list_head xattrs;
103 	union {
104 		struct pidfs_anon_attr;
105 		struct llist_node pidfs_llist;
106 	};
107 };
108 
109 #if BITS_PER_LONG == 32
110 
111 static DEFINE_SPINLOCK(pidfs_ino_lock);
112 static u64 pidfs_ino_nr = 1;
113 
114 static inline unsigned long pidfs_ino(u64 ino)
115 {
116 	return lower_32_bits(ino);
117 }
118 
119 /* On 32 bit the generation number are the upper 32 bits. */
120 static inline u32 pidfs_gen(u64 ino)
121 {
122 	return upper_32_bits(ino);
123 }
124 
125 static inline u64 pidfs_alloc_ino(void)
126 {
127 	u64 ino;
128 
129 	spin_lock(&pidfs_ino_lock);
130 	if (pidfs_ino(pidfs_ino_nr) == 0)
131 		pidfs_ino_nr++;
132 	ino = pidfs_ino_nr++;
133 	spin_unlock(&pidfs_ino_lock);
134 	return ino;
135 }
136 
137 #else
138 
139 /* On 64 bit simply return ino. */
140 static inline unsigned long pidfs_ino(u64 ino)
141 {
142 	return ino;
143 }
144 
145 /* On 64 bit the generation number is 0. */
146 static inline u32 pidfs_gen(u64 ino)
147 {
148 	return 0;
149 }
150 
151 DEFINE_COOKIE(pidfs_ino_cookie);
152 
153 static u64 pidfs_alloc_ino(void)
154 {
155 	u64 ino;
156 
157 	preempt_disable();
158 	ino = gen_cookie_next(&pidfs_ino_cookie);
159 	preempt_enable();
160 
161 	VFS_WARN_ON_ONCE(ino < 1);
162 	return ino;
163 }
164 
165 #endif
166 
167 void pidfs_prepare_pid(struct pid *pid)
168 {
169 	pid->stashed = NULL;
170 	pid->attr = NULL;
171 	pid->ino = 0;
172 }
173 
174 int pidfs_add_pid(struct pid *pid)
175 {
176 	int ret;
177 
178 	pid->ino = pidfs_alloc_ino();
179 	ret = rhashtable_insert_fast(&pidfs_ino_ht, &pid->pidfs_hash,
180 				     pidfs_ino_ht_params);
181 	if (unlikely(ret))
182 		pid->ino = 0;
183 	return ret;
184 }
185 
186 void pidfs_remove_pid(struct pid *pid)
187 {
188 	if (likely(pid->ino))
189 		rhashtable_remove_fast(&pidfs_ino_ht, &pid->pidfs_hash,
190 				       pidfs_ino_ht_params);
191 }
192 
193 static LLIST_HEAD(pidfs_free_list);
194 
195 static void pidfs_free_attr_work(struct work_struct *work)
196 {
197 	struct pidfs_attr *attr, *next;
198 	struct llist_node *head;
199 
200 	head = llist_del_all(&pidfs_free_list);
201 	llist_for_each_entry_safe(attr, next, head, pidfs_llist) {
202 		simple_xattrs_free(&pidfs_xa_cache, &attr->xattrs, NULL);
203 		kfree(attr);
204 	}
205 }
206 
207 static DECLARE_WORK(pidfs_free_work, pidfs_free_attr_work);
208 
209 void pidfs_free_pid(struct pid *pid)
210 {
211 	struct pidfs_attr *attr = pid->attr;
212 
213 	/*
214 	 * Any dentry must've been wiped from the pid by now.
215 	 * Otherwise there's a reference count bug.
216 	 */
217 	VFS_WARN_ON_ONCE(pid->stashed);
218 
219 	/*
220 	 * This if an error occurred during e.g., task creation that
221 	 * causes us to never go through the exit path.
222 	 */
223 	if (unlikely(!attr))
224 		return;
225 
226 	/* This never had a pidfd created. */
227 	if (IS_ERR(attr))
228 		return;
229 
230 	if (likely(list_empty(&attr->xattrs)))
231 		kfree(attr);
232 	else if (llist_add(&attr->pidfs_llist, &pidfs_free_list))
233 		schedule_work(&pidfs_free_work);
234 }
235 
236 #ifdef CONFIG_PROC_FS
237 /**
238  * pidfd_show_fdinfo - print information about a pidfd
239  * @m: proc fdinfo file
240  * @f: file referencing a pidfd
241  *
242  * Pid:
243  * This function will print the pid that a given pidfd refers to in the
244  * pid namespace of the procfs instance.
245  * If the pid namespace of the process is not a descendant of the pid
246  * namespace of the procfs instance 0 will be shown as its pid. This is
247  * similar to calling getppid() on a process whose parent is outside of
248  * its pid namespace.
249  *
250  * NSpid:
251  * If pid namespaces are supported then this function will also print
252  * the pid of a given pidfd refers to for all descendant pid namespaces
253  * starting from the current pid namespace of the instance, i.e. the
254  * Pid field and the first entry in the NSpid field will be identical.
255  * If the pid namespace of the process is not a descendant of the pid
256  * namespace of the procfs instance 0 will be shown as its first NSpid
257  * entry and no others will be shown.
258  * Note that this differs from the Pid and NSpid fields in
259  * /proc/<pid>/status where Pid and NSpid are always shown relative to
260  * the  pid namespace of the procfs instance. The difference becomes
261  * obvious when sending around a pidfd between pid namespaces from a
262  * different branch of the tree, i.e. where no ancestral relation is
263  * present between the pid namespaces:
264  * - create two new pid namespaces ns1 and ns2 in the initial pid
265  *   namespace (also take care to create new mount namespaces in the
266  *   new pid namespace and mount procfs)
267  * - create a process with a pidfd in ns1
268  * - send pidfd from ns1 to ns2
269  * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid
270  *   have exactly one entry, which is 0
271  */
272 static void pidfd_show_fdinfo(struct seq_file *m, struct file *f)
273 {
274 	struct pid *pid = pidfd_pid(f);
275 	struct pid_namespace *ns;
276 	pid_t nr = -1;
277 
278 	if (likely(pid_has_task(pid, PIDTYPE_PID))) {
279 		ns = proc_pid_ns(file_inode(m->file)->i_sb);
280 		nr = pid_nr_ns(pid, ns);
281 	}
282 
283 	seq_put_decimal_ll(m, "Pid:\t", nr);
284 
285 #ifdef CONFIG_PID_NS
286 	seq_put_decimal_ll(m, "\nNSpid:\t", nr);
287 	if (nr > 0) {
288 		int i;
289 
290 		/* If nr is non-zero it means that 'pid' is valid and that
291 		 * ns, i.e. the pid namespace associated with the procfs
292 		 * instance, is in the pid namespace hierarchy of pid.
293 		 * Start at one below the already printed level.
294 		 */
295 		for (i = ns->level + 1; i <= pid->level; i++)
296 			seq_put_decimal_ll(m, "\t", pid->numbers[i].nr);
297 	}
298 #endif
299 	seq_putc(m, '\n');
300 }
301 #endif
302 
303 /*
304  * Poll support for process exit notification.
305  */
306 static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)
307 {
308 	struct pid *pid = pidfd_pid(file);
309 	struct task_struct *task;
310 	__poll_t poll_flags = 0;
311 
312 	poll_wait(file, &pid->wait_pidfd, pts);
313 	/*
314 	 * Don't wake waiters if the thread-group leader exited
315 	 * prematurely. They either get notified when the last subthread
316 	 * exits or not at all if one of the remaining subthreads execs
317 	 * and assumes the struct pid of the old thread-group leader.
318 	 */
319 	guard(rcu)();
320 	task = pid_task(pid, PIDTYPE_PID);
321 	if (!task)
322 		poll_flags = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
323 	else if (task->exit_state && !delay_group_leader(task))
324 		poll_flags = EPOLLIN | EPOLLRDNORM;
325 
326 	return poll_flags;
327 }
328 
329 static inline bool pid_in_current_pidns(const struct pid *pid)
330 {
331 	const struct pid_namespace *ns = task_active_pid_ns(current);
332 
333 	if (ns->level <= pid->level)
334 		return pid->numbers[ns->level].ns == ns;
335 
336 	return false;
337 }
338 
339 static __u32 pidfs_coredump_mask(enum task_dumpable dumpable)
340 {
341 	switch (dumpable) {
342 	case TASK_DUMPABLE_OWNER:
343 		return PIDFD_COREDUMP_USER;
344 	case TASK_DUMPABLE_ROOT:
345 		return PIDFD_COREDUMP_ROOT;
346 	case TASK_DUMPABLE_OFF:
347 		return PIDFD_COREDUMP_SKIP;
348 	default:
349 		WARN_ON_ONCE(true);
350 	}
351 
352 	return 0;
353 }
354 
355 /* This must be updated whenever a new flag is added */
356 #define PIDFD_INFO_SUPPORTED (PIDFD_INFO_PID | \
357 			      PIDFD_INFO_CREDS | \
358 			      PIDFD_INFO_CGROUPID | \
359 			      PIDFD_INFO_EXIT | \
360 			      PIDFD_INFO_COREDUMP | \
361 			      PIDFD_INFO_SUPPORTED_MASK | \
362 			      PIDFD_INFO_COREDUMP_SIGNAL | \
363 			      PIDFD_INFO_COREDUMP_CODE)
364 
365 static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
366 {
367 	struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg;
368 	struct task_struct *task __free(put_task) = NULL;
369 	struct pid *pid = pidfd_pid(file);
370 	size_t usize = _IOC_SIZE(cmd);
371 	struct pidfd_info kinfo = {};
372 	struct user_namespace *user_ns;
373 	struct pidfs_attr *attr;
374 	const struct cred *c;
375 	__u64 mask;
376 
377 	BUILD_BUG_ON(sizeof(struct pidfd_info) != PIDFD_INFO_SIZE_VER3);
378 
379 	if (!uinfo)
380 		return -EINVAL;
381 	if (usize < PIDFD_INFO_SIZE_VER0)
382 		return -EINVAL; /* First version, no smaller struct possible */
383 
384 	if (copy_from_user(&mask, &uinfo->mask, sizeof(mask)))
385 		return -EFAULT;
386 
387 	/*
388 	 * Restrict information retrieval to tasks within the caller's pid
389 	 * namespace hierarchy.
390 	 */
391 	if (!pid_in_current_pidns(pid))
392 		return -EREMOTE;
393 
394 	attr = READ_ONCE(pid->attr);
395 	if (mask & PIDFD_INFO_EXIT) {
396 		if (test_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask)) {
397 			smp_rmb();
398 			kinfo.mask |= PIDFD_INFO_EXIT;
399 #ifdef CONFIG_CGROUPS
400 			kinfo.cgroupid = attr->cgroupid;
401 			kinfo.mask |= PIDFD_INFO_CGROUPID;
402 #endif
403 			kinfo.exit_code = attr->exit_code;
404 		}
405 	}
406 
407 	if (mask & PIDFD_INFO_COREDUMP) {
408 		if (test_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask)) {
409 			smp_rmb();
410 			kinfo.mask |= PIDFD_INFO_COREDUMP | PIDFD_INFO_COREDUMP_SIGNAL | PIDFD_INFO_COREDUMP_CODE;
411 			kinfo.coredump_mask = attr->coredump_mask;
412 			kinfo.coredump_signal = attr->coredump_signal;
413 			kinfo.coredump_code = attr->coredump_code;
414 		}
415 	}
416 
417 	task = get_pid_task(pid, PIDTYPE_PID);
418 	if (!task) {
419 		/*
420 		 * If the task has already been reaped, only exit
421 		 * information is available
422 		 */
423 		if (!(mask & PIDFD_INFO_EXIT))
424 			return -ESRCH;
425 
426 		goto copy_out;
427 	}
428 
429 	c = get_task_cred(task);
430 	if (!c)
431 		return -ESRCH;
432 
433 	if ((mask & PIDFD_INFO_COREDUMP) && !kinfo.coredump_mask) {
434 		kinfo.coredump_mask = pidfs_coredump_mask(task_exec_state_get_dumpable(task));
435 		kinfo.mask |= PIDFD_INFO_COREDUMP;
436 		/* No coredump actually took place, so no coredump signal. */
437 	}
438 
439 	/* Unconditionally return identifiers and credentials, the rest only on request */
440 
441 	user_ns = current_user_ns();
442 	kinfo.ruid = from_kuid_munged(user_ns, c->uid);
443 	kinfo.rgid = from_kgid_munged(user_ns, c->gid);
444 	kinfo.euid = from_kuid_munged(user_ns, c->euid);
445 	kinfo.egid = from_kgid_munged(user_ns, c->egid);
446 	kinfo.suid = from_kuid_munged(user_ns, c->suid);
447 	kinfo.sgid = from_kgid_munged(user_ns, c->sgid);
448 	kinfo.fsuid = from_kuid_munged(user_ns, c->fsuid);
449 	kinfo.fsgid = from_kgid_munged(user_ns, c->fsgid);
450 	kinfo.mask |= PIDFD_INFO_CREDS;
451 	put_cred(c);
452 
453 #ifdef CONFIG_CGROUPS
454 	if (!kinfo.cgroupid) {
455 		struct cgroup *cgrp;
456 
457 		rcu_read_lock();
458 		cgrp = task_dfl_cgroup(task);
459 		kinfo.cgroupid = cgroup_id(cgrp);
460 		kinfo.mask |= PIDFD_INFO_CGROUPID;
461 		rcu_read_unlock();
462 	}
463 #endif
464 
465 	/*
466 	 * Copy pid/tgid last, to reduce the chances the information might be
467 	 * stale. Note that it is not possible to ensure it will be valid as the
468 	 * task might return as soon as the copy_to_user finishes, but that's ok
469 	 * and userspace expects that might happen and can act accordingly, so
470 	 * this is just best-effort. What we can do however is checking that all
471 	 * the fields are set correctly, or return ESRCH to avoid providing
472 	 * incomplete information. */
473 
474 	kinfo.ppid = task_ppid_vnr(task);
475 	kinfo.tgid = task_tgid_vnr(task);
476 	kinfo.pid = task_pid_vnr(task);
477 	kinfo.mask |= PIDFD_INFO_PID;
478 
479 	if (kinfo.pid == 0 || kinfo.tgid == 0)
480 		return -ESRCH;
481 
482 copy_out:
483 	if (mask & PIDFD_INFO_SUPPORTED_MASK) {
484 		kinfo.mask |= PIDFD_INFO_SUPPORTED_MASK;
485 		kinfo.supported_mask = PIDFD_INFO_SUPPORTED;
486 	}
487 
488 	/* Are there bits in the return mask not present in PIDFD_INFO_SUPPORTED? */
489 	WARN_ON_ONCE(~PIDFD_INFO_SUPPORTED & kinfo.mask);
490 	/*
491 	 * If userspace and the kernel have the same struct size it can just
492 	 * be copied. If userspace provides an older struct, only the bits that
493 	 * userspace knows about will be copied. If userspace provides a new
494 	 * struct, only the bits that the kernel knows about will be copied.
495 	 */
496 	return copy_struct_to_user(uinfo, usize, &kinfo, sizeof(kinfo), NULL);
497 }
498 
499 static bool pidfs_ioctl_valid(unsigned int cmd)
500 {
501 	switch (cmd) {
502 	case FS_IOC_GETVERSION:
503 	case PIDFD_GET_CGROUP_NAMESPACE:
504 	case PIDFD_GET_IPC_NAMESPACE:
505 	case PIDFD_GET_MNT_NAMESPACE:
506 	case PIDFD_GET_NET_NAMESPACE:
507 	case PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE:
508 	case PIDFD_GET_TIME_NAMESPACE:
509 	case PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE:
510 	case PIDFD_GET_UTS_NAMESPACE:
511 	case PIDFD_GET_USER_NAMESPACE:
512 	case PIDFD_GET_PID_NAMESPACE:
513 		return true;
514 	}
515 
516 	/* Extensible ioctls require some more careful checks. */
517 	switch (_IOC_NR(cmd)) {
518 	case _IOC_NR(PIDFD_GET_INFO):
519 		/*
520 		 * Try to prevent performing a pidfd ioctl when someone
521 		 * erronously mistook the file descriptor for a pidfd.
522 		 * This is not perfect but will catch most cases.
523 		 */
524 		return extensible_ioctl_valid(cmd, PIDFD_GET_INFO, PIDFD_INFO_SIZE_VER0);
525 	}
526 
527 	return false;
528 }
529 
530 static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
531 {
532 	struct task_struct *task __free(put_task) = NULL;
533 	struct nsproxy *nsp __free(put_nsproxy) = NULL;
534 	struct ns_common *ns_common = NULL;
535 
536 	if (!pidfs_ioctl_valid(cmd))
537 		return -ENOIOCTLCMD;
538 
539 	if (cmd == FS_IOC_GETVERSION) {
540 		if (!arg)
541 			return -EINVAL;
542 
543 		__u32 __user *argp = (__u32 __user *)arg;
544 		return put_user(file_inode(file)->i_generation, argp);
545 	}
546 
547 	/* Extensible IOCTL that does not open namespace FDs, take a shortcut */
548 	if (_IOC_NR(cmd) == _IOC_NR(PIDFD_GET_INFO))
549 		return pidfd_info(file, cmd, arg);
550 
551 	task = get_pid_task(pidfd_pid(file), PIDTYPE_PID);
552 	if (!task)
553 		return -ESRCH;
554 
555 	if (arg)
556 		return -EINVAL;
557 
558 	scoped_guard(task_lock, task) {
559 		nsp = task->nsproxy;
560 		if (nsp)
561 			get_nsproxy(nsp);
562 	}
563 	if (!nsp)
564 		return -ESRCH; /* just pretend it didn't exist */
565 
566 	/*
567 	 * We're trying to open a file descriptor to the namespace so perform a
568 	 * filesystem cred ptrace check. Also, we mirror nsfs behavior.
569 	 */
570 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
571 		return -EACCES;
572 
573 	switch (cmd) {
574 	/* Namespaces that hang of nsproxy. */
575 	case PIDFD_GET_CGROUP_NAMESPACE:
576 #ifdef CONFIG_CGROUPS
577 		if (!ns_ref_get(nsp->cgroup_ns))
578 			break;
579 		ns_common = to_ns_common(nsp->cgroup_ns);
580 #endif
581 		break;
582 	case PIDFD_GET_IPC_NAMESPACE:
583 #ifdef CONFIG_IPC_NS
584 		if (!ns_ref_get(nsp->ipc_ns))
585 			break;
586 		ns_common = to_ns_common(nsp->ipc_ns);
587 #endif
588 		break;
589 	case PIDFD_GET_MNT_NAMESPACE:
590 		if (!ns_ref_get(nsp->mnt_ns))
591 			break;
592 		ns_common = to_ns_common(nsp->mnt_ns);
593 		break;
594 	case PIDFD_GET_NET_NAMESPACE:
595 #ifdef CONFIG_NET_NS
596 		if (!ns_ref_get(nsp->net_ns))
597 			break;
598 		ns_common = to_ns_common(nsp->net_ns);
599 #endif
600 		break;
601 	case PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE:
602 #ifdef CONFIG_PID_NS
603 		if (!ns_ref_get(nsp->pid_ns_for_children))
604 			break;
605 		ns_common = to_ns_common(nsp->pid_ns_for_children);
606 #endif
607 		break;
608 	case PIDFD_GET_TIME_NAMESPACE:
609 #ifdef CONFIG_TIME_NS
610 		if (!ns_ref_get(nsp->time_ns))
611 			break;
612 		ns_common = to_ns_common(nsp->time_ns);
613 #endif
614 		break;
615 	case PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE:
616 #ifdef CONFIG_TIME_NS
617 		if (!ns_ref_get(nsp->time_ns_for_children))
618 			break;
619 		ns_common = to_ns_common(nsp->time_ns_for_children);
620 #endif
621 		break;
622 	case PIDFD_GET_UTS_NAMESPACE:
623 #ifdef CONFIG_UTS_NS
624 		if (!ns_ref_get(nsp->uts_ns))
625 			break;
626 		ns_common = to_ns_common(nsp->uts_ns);
627 #endif
628 		break;
629 	/* Namespaces that don't hang of nsproxy. */
630 	case PIDFD_GET_USER_NAMESPACE:
631 #ifdef CONFIG_USER_NS
632 		scoped_guard(rcu) {
633 			struct user_namespace *user_ns;
634 
635 			user_ns = task_cred_xxx(task, user_ns);
636 			if (ns_ref_get(user_ns))
637 				ns_common = to_ns_common(user_ns);
638 		}
639 #endif
640 		break;
641 	case PIDFD_GET_PID_NAMESPACE:
642 #ifdef CONFIG_PID_NS
643 		scoped_guard(rcu) {
644 			struct pid_namespace *pid_ns;
645 
646 			pid_ns = task_active_pid_ns(task);
647 			if (ns_ref_get(pid_ns))
648 				ns_common = to_ns_common(pid_ns);
649 		}
650 #endif
651 		break;
652 	default:
653 		return -ENOIOCTLCMD;
654 	}
655 
656 	if (!ns_common)
657 		return -EOPNOTSUPP;
658 
659 	/* open_namespace() unconditionally consumes the reference */
660 	return open_namespace(ns_common);
661 }
662 
663 #ifdef CONFIG_COMPAT
664 static long pidfd_compat_ioctl(struct file *file, unsigned int cmd,
665 			       unsigned long arg)
666 {
667 	if (cmd == FS_IOC32_GETVERSION)
668 		cmd = FS_IOC_GETVERSION;
669 
670 	return pidfd_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
671 }
672 #endif
673 
674 static int pidfs_file_release(struct inode *inode, struct file *file)
675 {
676 	struct pid *pid = inode->i_private;
677 	struct task_struct *task;
678 
679 	if (!(file->f_flags & PIDFD_AUTOKILL))
680 		return 0;
681 
682 	guard(rcu)();
683 	task = pid_task(pid, PIDTYPE_TGID);
684 	if (!task)
685 		return 0;
686 
687 	/* Not available for kthreads or user workers for now. */
688 	if (WARN_ON_ONCE(task->flags & (PF_KTHREAD | PF_USER_WORKER)))
689 		return 0;
690 	do_send_sig_info(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_TGID);
691 	return 0;
692 }
693 
694 static const struct file_operations pidfs_file_operations = {
695 	.release	= pidfs_file_release,
696 	.poll		= pidfd_poll,
697 #ifdef CONFIG_PROC_FS
698 	.show_fdinfo	= pidfd_show_fdinfo,
699 #endif
700 	.unlocked_ioctl	= pidfd_ioctl,
701 #ifdef CONFIG_COMPAT
702 	.compat_ioctl	= pidfd_compat_ioctl,
703 #endif
704 };
705 
706 struct pid *pidfd_pid(const struct file *file)
707 {
708 	if (file->f_op != &pidfs_file_operations)
709 		return ERR_PTR(-EBADF);
710 	return file_inode(file)->i_private;
711 }
712 
713 /*
714  * We're called from release_task(). We know there's at least one
715  * reference to struct pid being held that won't be released until the
716  * task has been reaped which cannot happen until we're out of
717  * release_task().
718  *
719  * If this struct pid has at least once been referred to by a pidfd then
720  * pid->attr will be allocated. If not we mark the struct pid as dead so
721  * anyone who is trying to register it with pidfs will fail to do so.
722  * Otherwise we would hand out pidfs for reaped tasks without having
723  * exit information available.
724  *
725  * Worst case is that we've filled in the info and the pid gets freed
726  * right away in free_pid() when no one holds a pidfd anymore. Since
727  * pidfs_exit() currently is placed after exit_task_work() we know that
728  * it cannot be us aka the exiting task holding a pidfd to itself.
729  */
730 void pidfs_exit(struct task_struct *tsk)
731 {
732 	struct pid *pid = task_pid(tsk);
733 	struct pidfs_attr *attr;
734 #ifdef CONFIG_CGROUPS
735 	struct cgroup *cgrp;
736 #endif
737 
738 	might_sleep();
739 
740 	/* Synchronize with pidfs_register_pid(). */
741 	scoped_guard(spinlock_irq, &pid->wait_pidfd.lock) {
742 		attr = pid->attr;
743 		if (!attr) {
744 			/*
745 			 * No one ever held a pidfd for this struct pid.
746 			 * Mark it as dead so no one can add a pidfs
747 			 * entry anymore. We're about to be reaped and
748 			 * so no exit information would be available.
749 			 */
750 			pid->attr = PIDFS_PID_DEAD;
751 			return;
752 		}
753 	}
754 
755 	/*
756 	 * If @pid->attr is set someone might still legitimately hold a
757 	 * pidfd to @pid or someone might concurrently still be getting
758 	 * a reference to an already stashed dentry from @pid->stashed.
759 	 * So defer cleaning @pid->attr until the last reference to @pid
760 	 * is put
761 	 */
762 
763 #ifdef CONFIG_CGROUPS
764 	rcu_read_lock();
765 	cgrp = task_dfl_cgroup(tsk);
766 	attr->cgroupid = cgroup_id(cgrp);
767 	rcu_read_unlock();
768 #endif
769 	attr->exit_code = tsk->exit_code;
770 
771 	/* Ensure that PIDFD_GET_INFO sees either all or nothing. */
772 	smp_wmb();
773 	set_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask);
774 }
775 
776 #ifdef CONFIG_COREDUMP
777 void pidfs_coredump(const struct coredump_params *cprm)
778 {
779 	struct pid *pid = cprm->pid;
780 	struct pidfs_attr *attr;
781 
782 	attr = READ_ONCE(pid->attr);
783 
784 	VFS_WARN_ON_ONCE(!attr);
785 	VFS_WARN_ON_ONCE(attr == PIDFS_PID_DEAD);
786 
787 	/* Note how we were coredumped and that we coredumped. */
788 	attr->coredump_mask = pidfs_coredump_mask(cprm->dumpable) |
789 			      PIDFD_COREDUMPED;
790 	/* If coredumping is set to skip we should never end up here. */
791 	VFS_WARN_ON_ONCE(attr->coredump_mask & PIDFD_COREDUMP_SKIP);
792 	/* Expose the signal number and code that caused the coredump. */
793 	attr->coredump_signal = cprm->siginfo->si_signo;
794 	attr->coredump_code = cprm->siginfo->si_code;
795 	smp_wmb();
796 	set_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask);
797 }
798 #endif
799 
800 static struct vfsmount *pidfs_mnt __ro_after_init;
801 
802 /*
803  * The vfs falls back to simple_setattr() if i_op->setattr() isn't
804  * implemented. Let's reject it completely until we have a clean
805  * permission concept for pidfds.
806  */
807 static int pidfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
808 			 struct iattr *attr)
809 {
810 	return anon_inode_setattr(idmap, dentry, attr);
811 }
812 
813 static int pidfs_getattr(struct mnt_idmap *idmap, const struct path *path,
814 			 struct kstat *stat, u32 request_mask,
815 			 unsigned int query_flags)
816 {
817 	return anon_inode_getattr(idmap, path, stat, request_mask, query_flags);
818 }
819 
820 static ssize_t pidfs_listxattr(struct dentry *dentry, char *buf, size_t size)
821 {
822 	struct inode *inode = d_inode(dentry);
823 	struct pid *pid = inode->i_private;
824 
825 	return simple_xattr_list(inode, &pid->attr->xattrs, buf, size);
826 }
827 
828 static const struct inode_operations pidfs_inode_operations = {
829 	.getattr	= pidfs_getattr,
830 	.setattr	= pidfs_setattr,
831 	.listxattr	= pidfs_listxattr,
832 };
833 
834 static void pidfs_evict_inode(struct inode *inode)
835 {
836 	struct pid *pid = inode->i_private;
837 
838 	clear_inode(inode);
839 	put_pid(pid);
840 }
841 
842 static const struct super_operations pidfs_sops = {
843 	.drop_inode	= inode_just_drop,
844 	.evict_inode	= pidfs_evict_inode,
845 	.statfs		= simple_statfs,
846 };
847 
848 /*
849  * 'lsof' has knowledge of out historical anon_inode use, and expects
850  * the pidfs dentry name to start with 'anon_inode'.
851  */
852 static char *pidfs_dname(struct dentry *dentry, char *buffer, int buflen)
853 {
854 	return dynamic_dname(buffer, buflen, "anon_inode:[pidfd]");
855 }
856 
857 const struct dentry_operations pidfs_dentry_operations = {
858 	.d_dname	= pidfs_dname,
859 	.d_prune	= stashed_dentry_prune,
860 };
861 
862 static int pidfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
863 			   struct inode *parent)
864 {
865 	const struct pid *pid = inode->i_private;
866 
867 	if (*max_len < 2) {
868 		*max_len = 2;
869 		return FILEID_INVALID;
870 	}
871 
872 	*max_len = 2;
873 	*(u64 *)fh = pid->ino;
874 	return FILEID_KERNFS;
875 }
876 
877 /* Find a struct pid based on the inode number. */
878 static struct pid *pidfs_ino_get_pid(u64 ino)
879 {
880 	struct pid *pid;
881 	struct pidfs_attr *attr;
882 
883 	guard(rcu)();
884 	pid = rhashtable_lookup(&pidfs_ino_ht, &ino, pidfs_ino_ht_params);
885 	if (!pid)
886 		return NULL;
887 	attr = READ_ONCE(pid->attr);
888 	if (IS_ERR_OR_NULL(attr))
889 		return NULL;
890 	if (test_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask))
891 		return NULL;
892 	/* Within our pid namespace hierarchy? */
893 	if (pid_vnr(pid) == 0)
894 		return NULL;
895 	return get_pid(pid);
896 }
897 
898 static struct dentry *pidfs_fh_to_dentry(struct super_block *sb,
899 					 struct fid *fid, int fh_len,
900 					 int fh_type)
901 {
902 	int ret;
903 	u64 pid_ino;
904 	struct path path;
905 	struct pid *pid;
906 
907 	if (fh_len < 2)
908 		return NULL;
909 
910 	switch (fh_type) {
911 	case FILEID_KERNFS:
912 		pid_ino = *(u64 *)fid;
913 		break;
914 	default:
915 		return NULL;
916 	}
917 
918 	pid = pidfs_ino_get_pid(pid_ino);
919 	if (!pid)
920 		return NULL;
921 
922 	ret = path_from_stashed(&pid->stashed, pidfs_mnt, pid, &path);
923 	if (ret < 0)
924 		return ERR_PTR(ret);
925 
926 	VFS_WARN_ON_ONCE(!pid->attr);
927 
928 	mntput(path.mnt);
929 	return path.dentry;
930 }
931 
932 static struct file *pidfs_dentry_open(const struct path *path,
933 				      unsigned int flags,
934 				      const struct cred *cred)
935 {
936 	struct file *file;
937 
938 	/* pidfds are always O_RDWR. */
939 	file = dentry_open(path, flags | O_RDWR, cred);
940 	/* do_dentry_open() strips O_EXCL and O_TRUNC. */
941 	if (!IS_ERR(file))
942 		file->f_flags |= flags & (PIDFD_THREAD | PIDFD_AUTOKILL);
943 	return file;
944 }
945 
946 /*
947  * Make sure that we reject any nonsensical flags that users pass via
948  * open_by_handle_at(). Note that PIDFD_THREAD is defined as O_EXCL, and
949  * PIDFD_NONBLOCK as O_NONBLOCK.
950  */
951 #define VALID_FILE_HANDLE_OPEN_FLAGS \
952 	(O_RDONLY | O_WRONLY | O_RDWR | O_NONBLOCK | O_CLOEXEC | O_EXCL)
953 
954 static int pidfs_export_permission(struct handle_to_path_ctx *ctx,
955 				   unsigned int oflags)
956 {
957 	if (oflags & ~(VALID_FILE_HANDLE_OPEN_FLAGS | O_LARGEFILE))
958 		return -EINVAL;
959 
960 	/*
961 	 * pidfd_ino_get_pid() will verify that the struct pid is part
962 	 * of the caller's pid namespace hierarchy. No further
963 	 * permission checks are needed.
964 	 */
965 	return 0;
966 }
967 
968 static struct file *pidfs_export_open(const struct path *path, unsigned int oflags)
969 {
970 	/*
971 	 * Opening via file handle may never raise PIDFD_AUTOKILL. That can
972 	 * only be done at task creation!
973 	 */
974 	if (WARN_ON_ONCE(oflags & PIDFD_AUTOKILL))
975 		return ERR_PTR(-EINVAL);
976 	/* Clear O_LARGEFILE as open_by_handle_at() forces it. */
977 	return pidfs_dentry_open(path, oflags & ~O_LARGEFILE, current_cred());
978 }
979 
980 static const struct export_operations pidfs_export_operations = {
981 	.encode_fh	= pidfs_encode_fh,
982 	.fh_to_dentry	= pidfs_fh_to_dentry,
983 	.open		= pidfs_export_open,
984 	.permission	= pidfs_export_permission,
985 };
986 
987 static int pidfs_init_inode(struct inode *inode, void *data)
988 {
989 	const struct pid *pid = data;
990 
991 	inode->i_private = data;
992 	inode->i_flags |= S_PRIVATE | S_ANON_INODE;
993 	/* We allow to set xattrs. */
994 	inode->i_flags &= ~S_IMMUTABLE;
995 	inode->i_mode |= S_IRWXU;
996 	inode->i_op = &pidfs_inode_operations;
997 	inode->i_fop = &pidfs_file_operations;
998 	inode->i_ino = pidfs_ino(pid->ino);
999 	inode->i_generation = pidfs_gen(pid->ino);
1000 	return 0;
1001 }
1002 
1003 static void pidfs_put_data(void *data)
1004 {
1005 	struct pid *pid = data;
1006 	put_pid(pid);
1007 }
1008 
1009 /**
1010  * pidfs_register_pid_gfp - register a struct pid in pidfs with custom GFP
1011  * flags
1012  * @pid: pid to pin
1013  * @gfp: GFP flags for memory allocation
1014  *
1015  * Register a struct pid in pidfs with custom GFP flags.
1016  *
1017  * Return: On success zero, on error a negative error code is returned.
1018  */
1019 int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp)
1020 {
1021 	struct pidfs_attr *new_attr __free(kfree) = NULL;
1022 	struct pidfs_attr *attr;
1023 
1024 	might_sleep();
1025 
1026 	if (!pid)
1027 		return 0;
1028 
1029 	attr = READ_ONCE(pid->attr);
1030 	if (unlikely(attr == PIDFS_PID_DEAD))
1031 		return PTR_ERR(PIDFS_PID_DEAD);
1032 	if (attr)
1033 		return 0;
1034 
1035 	new_attr = kmem_cache_zalloc(pidfs_attr_cachep, gfp);
1036 	if (!new_attr)
1037 		return -ENOMEM;
1038 
1039 	INIT_LIST_HEAD_RCU(&new_attr->xattrs);
1040 
1041 	/* Synchronize with pidfs_exit(). */
1042 	guard(spinlock_irq)(&pid->wait_pidfd.lock);
1043 
1044 	attr = pid->attr;
1045 	if (unlikely(attr == PIDFS_PID_DEAD))
1046 		return PTR_ERR(PIDFS_PID_DEAD);
1047 	if (unlikely(attr))
1048 		return 0;
1049 
1050 	pid->attr = no_free_ptr(new_attr);
1051 	return 0;
1052 }
1053 
1054 static struct dentry *pidfs_stash_dentry(struct dentry **stashed,
1055 					 struct dentry *dentry)
1056 {
1057 	int ret;
1058 	struct pid *pid = d_inode(dentry)->i_private;
1059 
1060 	VFS_WARN_ON_ONCE(stashed != &pid->stashed);
1061 
1062 	ret = pidfs_register_pid(pid);
1063 	if (ret)
1064 		return ERR_PTR(ret);
1065 
1066 	return stash_dentry(stashed, dentry);
1067 }
1068 
1069 static const struct stashed_operations pidfs_stashed_ops = {
1070 	.stash_dentry	= pidfs_stash_dentry,
1071 	.init_inode	= pidfs_init_inode,
1072 	.put_data	= pidfs_put_data,
1073 };
1074 
1075 static int pidfs_xattr_get(const struct xattr_handler *handler,
1076 			   struct dentry *unused, struct inode *inode,
1077 			   const char *suffix, void *value, size_t size)
1078 {
1079 	struct pid *pid = inode->i_private;
1080 	const char *name = xattr_full_name(handler, suffix);
1081 
1082 	return simple_xattr_get(&pidfs_xa_cache, &pid->attr->xattrs, name, value, size);
1083 }
1084 
1085 static int pidfs_xattr_set(const struct xattr_handler *handler,
1086 			   struct mnt_idmap *idmap, struct dentry *unused,
1087 			   struct inode *inode, const char *suffix,
1088 			   const void *value, size_t size, int flags)
1089 {
1090 	struct pid *pid = inode->i_private;
1091 	const char *name = xattr_full_name(handler, suffix);
1092 	struct simple_xattr *old_xattr;
1093 
1094 	/* Ensure we're the only one to set @attr->xattrs. */
1095 	WARN_ON_ONCE(!inode_is_locked(inode));
1096 
1097 	old_xattr = simple_xattr_set(&pidfs_xa_cache, &pid->attr->xattrs, name, value, size, flags);
1098 	if (IS_ERR(old_xattr))
1099 		return PTR_ERR(old_xattr);
1100 
1101 	simple_xattr_free_rcu(old_xattr);
1102 	return 0;
1103 }
1104 
1105 static const struct xattr_handler pidfs_trusted_xattr_handler = {
1106 	.prefix = XATTR_TRUSTED_PREFIX,
1107 	.get	= pidfs_xattr_get,
1108 	.set	= pidfs_xattr_set,
1109 };
1110 
1111 static const struct xattr_handler *const pidfs_xattr_handlers[] = {
1112 	&pidfs_trusted_xattr_handler,
1113 	NULL
1114 };
1115 
1116 static int pidfs_init_fs_context(struct fs_context *fc)
1117 {
1118 	struct pseudo_fs_context *ctx;
1119 
1120 	ctx = init_pseudo(fc, PID_FS_MAGIC);
1121 	if (!ctx)
1122 		return -ENOMEM;
1123 
1124 	ctx->s_d_flags |= DCACHE_DONTCACHE;
1125 	ctx->ops = &pidfs_sops;
1126 	ctx->eops = &pidfs_export_operations;
1127 	ctx->dops = &pidfs_dentry_operations;
1128 	ctx->xattr = pidfs_xattr_handlers;
1129 	fc->s_fs_info = (void *)&pidfs_stashed_ops;
1130 	return 0;
1131 }
1132 
1133 static struct file_system_type pidfs_type = {
1134 	.name			= "pidfs",
1135 	.init_fs_context	= pidfs_init_fs_context,
1136 	.kill_sb		= kill_anon_super,
1137 };
1138 
1139 struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)
1140 {
1141 	struct path path __free(path_put) = {};
1142 	int ret;
1143 
1144 	/*
1145 	 * Ensure that internal pidfd flags don't overlap with each
1146 	 * other or with uapi pidfd flags.
1147 	 */
1148 	BUILD_BUG_ON(hweight32(PIDFD_THREAD | PIDFD_NONBLOCK |
1149 				PIDFD_STALE | PIDFD_AUTOKILL) != 4);
1150 
1151 	ret = path_from_stashed(&pid->stashed, pidfs_mnt, get_pid(pid), &path);
1152 	if (ret < 0)
1153 		return ERR_PTR(ret);
1154 
1155 	VFS_WARN_ON_ONCE(!pid->attr);
1156 
1157 	flags &= ~PIDFD_STALE;
1158 	return pidfs_dentry_open(&path, flags, current_cred());
1159 }
1160 
1161 void __init pidfs_init(void)
1162 {
1163 	if (rhashtable_init(&pidfs_ino_ht, &pidfs_ino_ht_params))
1164 		panic("Failed to initialize pidfs hashtable");
1165 
1166 	pidfs_attr_cachep = kmem_cache_create("pidfs_attr_cache", sizeof(struct pidfs_attr), 0,
1167 					 (SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT |
1168 					  SLAB_ACCOUNT | SLAB_PANIC), NULL);
1169 
1170 	pidfs_mnt = kern_mount(&pidfs_type);
1171 	if (IS_ERR(pidfs_mnt))
1172 		panic("Failed to mount pidfs pseudo filesystem");
1173 
1174 	pidfs_root_path.mnt = pidfs_mnt;
1175 	pidfs_root_path.dentry = pidfs_mnt->mnt_root;
1176 }
1177