1195 lines · 30.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0#include <linux/anon_inodes.h>#include <linux/compat.h>#include <linux/exportfs.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>#include <linux/pidfs.h>#include <linux/sched/signal.h>#include <linux/signal.h>#include <linux/pid_namespace.h>#include <linux/poll.h>#include <linux/proc_fs.h>#include <linux/proc_ns.h>#include <linux/pseudo_fs.h>#include <linux/ptrace.h>#include <linux/seq_file.h>#include <uapi/linux/pidfd.h>#include <linux/ipc_namespace.h>#include <linux/time_namespace.h>#include <linux/utsname.h>#include <net/net_namespace.h>#include <linux/coredump.h>#include <linux/rhashtable.h>#include <linux/llist.h>#include <linux/xattr.h>#include <linux/cookie.h> #include "internal.h"#include "mount.h" #define PIDFS_PID_DEAD ERR_PTR(-ESRCH) static struct kmem_cache *pidfs_attr_cachep __ro_after_init; static struct path pidfs_root_path = {}; static struct simple_xattr_cache pidfs_xa_cache; void pidfs_get_root(struct path *path){	*path = pidfs_root_path;	path_get(path);} enum pidfs_attr_mask_bits {	PIDFS_ATTR_BIT_EXIT	= 0,	PIDFS_ATTR_BIT_COREDUMP	= 1,}; struct pidfs_anon_attr {	unsigned long attr_mask;	struct /* exit info */ {		__u64 cgroupid;		__s32 exit_code;	};	__u32 coredump_mask;	__u32 coredump_signal;	__u32 coredump_code;}; static struct rhashtable pidfs_ino_ht; static const struct rhashtable_params pidfs_ino_ht_params = {	.key_offset		= offsetof(struct pid, ino),	.key_len		= sizeof(u64),	.head_offset		= offsetof(struct pid, pidfs_hash),	.automatic_shrinking	= true,}; /* * inode number handling * * On 64 bit nothing special happens. The 64bit number assigned * to struct pid is the inode number. * * On 32 bit the 64 bit number assigned to struct pid is split * into two 32 bit numbers. The lower 32 bits are used as the * inode number and the upper 32 bits are used as the inode * generation number. * * On 32 bit pidfs_ino() will return the lower 32 bit. When * pidfs_ino() returns zero a wrap around happened. When a * wraparound happens the 64 bit number will be incremented by 1 * so inode numbering starts at 1 again. * * On 64 bit comparing two pidfds is as simple as comparing * inode numbers. * * When a wraparound happens on 32 bit multiple pidfds with the * same inode number are likely to exist (This isn't a problem * since before pidfs pidfds used the anonymous inode meaning * all pidfds had the same inode number.). Userspace can * reconstruct the 64 bit identifier by retrieving both the * inode number and the inode generation number to compare or * use file handles. */struct pidfs_attr {	struct list_head xattrs;	union {		struct pidfs_anon_attr;		struct llist_node pidfs_llist;	};}; #if BITS_PER_LONG == 32 static DEFINE_SPINLOCK(pidfs_ino_lock);static u64 pidfs_ino_nr = 1; static inline unsigned long pidfs_ino(u64 ino){	return lower_32_bits(ino);} /* On 32 bit the generation number are the upper 32 bits. */static inline u32 pidfs_gen(u64 ino){	return upper_32_bits(ino);} static inline u64 pidfs_alloc_ino(void){	u64 ino; 	spin_lock(&pidfs_ino_lock);	if (pidfs_ino(pidfs_ino_nr) == 0)		pidfs_ino_nr++;	ino = pidfs_ino_nr++;	spin_unlock(&pidfs_ino_lock);	return ino;} #else /* On 64 bit simply return ino. */static inline unsigned long pidfs_ino(u64 ino){	return ino;} /* On 64 bit the generation number is 0. */static inline u32 pidfs_gen(u64 ino){	return 0;} DEFINE_COOKIE(pidfs_ino_cookie); static u64 pidfs_alloc_ino(void){	u64 ino; 	preempt_disable();	ino = gen_cookie_next(&pidfs_ino_cookie);	preempt_enable(); 	VFS_WARN_ON_ONCE(ino < 1);	return ino;} #endif void pidfs_prepare_pid(struct pid *pid){	pid->stashed = NULL;	pid->attr = NULL;	pid->ino = 0;} int pidfs_add_pid(struct pid *pid){	int ret; 	pid->ino = pidfs_alloc_ino();	ret = rhashtable_insert_fast(&pidfs_ino_ht, &pid->pidfs_hash,				     pidfs_ino_ht_params);	if (unlikely(ret))		pid->ino = 0;	return ret;} void pidfs_remove_pid(struct pid *pid){	if (likely(pid->ino))		rhashtable_remove_fast(&pidfs_ino_ht, &pid->pidfs_hash,				       pidfs_ino_ht_params);} static LLIST_HEAD(pidfs_free_list); static void pidfs_free_attr_work(struct work_struct *work){	struct pidfs_attr *attr, *next;	struct llist_node *head; 	head = llist_del_all(&pidfs_free_list);	llist_for_each_entry_safe(attr, next, head, pidfs_llist) {		simple_xattrs_free(&pidfs_xa_cache, &attr->xattrs, NULL);		kfree(attr);	}} static DECLARE_WORK(pidfs_free_work, pidfs_free_attr_work); void pidfs_free_pid(struct pid *pid){	struct pidfs_attr *attr = pid->attr; 	/*	 * Any dentry must've been wiped from the pid by now.	 * Otherwise there's a reference count bug.	 */	VFS_WARN_ON_ONCE(pid->stashed); 	/*	 * This if an error occurred during e.g., task creation that	 * causes us to never go through the exit path.	 */	if (unlikely(!attr))		return; 	/* This never had a pidfd created. */	if (IS_ERR(attr))		return; 	if (likely(list_empty(&attr->xattrs)))		kfree(attr);	else if (llist_add(&attr->pidfs_llist, &pidfs_free_list))		schedule_work(&pidfs_free_work);} #ifdef CONFIG_PROC_FS/** * pidfd_show_fdinfo - print information about a pidfd * @m: proc fdinfo file * @f: file referencing a pidfd * * Pid: * This function will print the pid that a given pidfd refers to in the * pid namespace of the procfs instance. * If the pid namespace of the process is not a descendant of the pid * namespace of the procfs instance 0 will be shown as its pid. This is * similar to calling getppid() on a process whose parent is outside of * its pid namespace. * * NSpid: * If pid namespaces are supported then this function will also print * the pid of a given pidfd refers to for all descendant pid namespaces * starting from the current pid namespace of the instance, i.e. the * Pid field and the first entry in the NSpid field will be identical. * If the pid namespace of the process is not a descendant of the pid * namespace of the procfs instance 0 will be shown as its first NSpid * entry and no others will be shown. * Note that this differs from the Pid and NSpid fields in * /proc/<pid>/status where Pid and NSpid are always shown relative to * the  pid namespace of the procfs instance. The difference becomes * obvious when sending around a pidfd between pid namespaces from a * different branch of the tree, i.e. where no ancestral relation is * present between the pid namespaces: * - create two new pid namespaces ns1 and ns2 in the initial pid *   namespace (also take care to create new mount namespaces in the *   new pid namespace and mount procfs) * - create a process with a pidfd in ns1 * - send pidfd from ns1 to ns2 * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid *   have exactly one entry, which is 0 */static void pidfd_show_fdinfo(struct seq_file *m, struct file *f){	struct pid *pid = pidfd_pid(f);	struct pid_namespace *ns;	pid_t nr = -1; 	if (likely(pid_has_task(pid, PIDTYPE_PID))) {		ns = proc_pid_ns(file_inode(m->file)->i_sb);		nr = pid_nr_ns(pid, ns);	} 	seq_put_decimal_ll(m, "Pid:\t", nr); #ifdef CONFIG_PID_NS	seq_put_decimal_ll(m, "\nNSpid:\t", nr);	if (nr > 0) {		int i; 		/* If nr is non-zero it means that 'pid' is valid and that		 * ns, i.e. the pid namespace associated with the procfs		 * instance, is in the pid namespace hierarchy of pid.		 * Start at one below the already printed level.		 */		for (i = ns->level + 1; i <= pid->level; i++)			seq_put_decimal_ll(m, "\t", pid->numbers[i].nr);	}#endif	seq_putc(m, '\n');}#endif /* * Poll support for process exit notification. */static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts){	struct pid *pid = pidfd_pid(file);	struct task_struct *task;	__poll_t poll_flags = 0; 	poll_wait(file, &pid->wait_pidfd, pts);	/*	 * Don't wake waiters if the thread-group leader exited	 * prematurely. They either get notified when the last subthread	 * exits or not at all if one of the remaining subthreads execs	 * and assumes the struct pid of the old thread-group leader.	 */	guard(rcu)();	task = pid_task(pid, PIDTYPE_PID);	if (!task)		poll_flags = EPOLLIN | EPOLLRDNORM | EPOLLHUP;	else if (task->exit_state && !delay_group_leader(task))		poll_flags = EPOLLIN | EPOLLRDNORM; 	return poll_flags;} static inline bool pid_in_current_pidns(const struct pid *pid){	const struct pid_namespace *ns = task_active_pid_ns(current); 	if (ns->level <= pid->level)		return pid->numbers[ns->level].ns == ns; 	return false;} static __u32 pidfs_coredump_mask(enum task_dumpable dumpable){	switch (dumpable) {	case TASK_DUMPABLE_OWNER:		return PIDFD_COREDUMP_USER;	case TASK_DUMPABLE_ROOT:		return PIDFD_COREDUMP_ROOT;	case TASK_DUMPABLE_OFF:		return PIDFD_COREDUMP_SKIP;	default:		WARN_ON_ONCE(true);	} 	return 0;} /* This must be updated whenever a new flag is added */#define PIDFD_INFO_SUPPORTED (PIDFD_INFO_PID | \			      PIDFD_INFO_CREDS | \			      PIDFD_INFO_CGROUPID | \			      PIDFD_INFO_EXIT | \			      PIDFD_INFO_COREDUMP | \			      PIDFD_INFO_SUPPORTED_MASK | \			      PIDFD_INFO_COREDUMP_SIGNAL | \			      PIDFD_INFO_COREDUMP_CODE) static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg){	struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg;	struct task_struct *task __free(put_task) = NULL;	struct pid *pid = pidfd_pid(file);	size_t usize = _IOC_SIZE(cmd);	struct pidfd_info kinfo = {};	struct user_namespace *user_ns;	struct pidfs_attr *attr;	const struct cred *c;	__u64 mask; 	BUILD_BUG_ON(sizeof(struct pidfd_info) != PIDFD_INFO_SIZE_VER3); 	if (!uinfo)		return -EINVAL;	if (usize < PIDFD_INFO_SIZE_VER0)		return -EINVAL; /* First version, no smaller struct possible */ 	if (copy_from_user(&mask, &uinfo->mask, sizeof(mask)))		return -EFAULT; 	/*	 * Restrict information retrieval to tasks within the caller's pid	 * namespace hierarchy.	 */	if (!pid_in_current_pidns(pid))		return -EREMOTE; 	attr = READ_ONCE(pid->attr);	if (mask & PIDFD_INFO_EXIT) {		if (test_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask)) {			smp_rmb();			kinfo.mask |= PIDFD_INFO_EXIT;#ifdef CONFIG_CGROUPS			kinfo.cgroupid = attr->cgroupid;			kinfo.mask |= PIDFD_INFO_CGROUPID;#endif			kinfo.exit_code = attr->exit_code;		}	} 	if (mask & PIDFD_INFO_COREDUMP) {		if (test_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask)) {			smp_rmb();			kinfo.mask |= PIDFD_INFO_COREDUMP | PIDFD_INFO_COREDUMP_SIGNAL | PIDFD_INFO_COREDUMP_CODE;			kinfo.coredump_mask = attr->coredump_mask;			kinfo.coredump_signal = attr->coredump_signal;			kinfo.coredump_code = attr->coredump_code;		}	} 	task = get_pid_task(pid, PIDTYPE_PID);	if (!task) {		/*		 * If the task has already been reaped, only exit		 * information is available		 */		if (!(mask & PIDFD_INFO_EXIT))			return -ESRCH; 		goto copy_out;	} 	c = get_task_cred(task);	if (!c)		return -ESRCH; 	if ((mask & PIDFD_INFO_COREDUMP) && !kinfo.coredump_mask) {		kinfo.coredump_mask = pidfs_coredump_mask(task_exec_state_get_dumpable(task));		kinfo.mask |= PIDFD_INFO_COREDUMP;		/* No coredump actually took place, so no coredump signal. */	} 	/* 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.mask |= PIDFD_INFO_CREDS;	put_cred(c); #ifdef CONFIG_CGROUPS	if (!kinfo.cgroupid) {		struct cgroup *cgrp; 		rcu_read_lock();		cgrp = task_dfl_cgroup(task);		kinfo.cgroupid = cgroup_id(cgrp);		kinfo.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_vnr(task);	kinfo.tgid = task_tgid_vnr(task);	kinfo.pid = task_pid_vnr(task);	kinfo.mask |= PIDFD_INFO_PID; 	if (kinfo.pid == 0 || kinfo.tgid == 0)		return -ESRCH; copy_out:	if (mask & PIDFD_INFO_SUPPORTED_MASK) {		kinfo.mask |= PIDFD_INFO_SUPPORTED_MASK;		kinfo.supported_mask = PIDFD_INFO_SUPPORTED;	} 	/* Are there bits in the return mask not present in PIDFD_INFO_SUPPORTED? */	WARN_ON_ONCE(~PIDFD_INFO_SUPPORTED & kinfo.mask);	/*	 * 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.	 */	return copy_struct_to_user(uinfo, usize, &kinfo, sizeof(kinfo), NULL);} static bool pidfs_ioctl_valid(unsigned int cmd){

Lines 1–500 of 1195.