xref: /openbmc/linux/fs/proc/base.c (revision 30d158b143b6575261ab610ae7b1b4f7fe3830b3)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/proc/base.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992 Linus Torvalds
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  proc base directory handling functions
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
101da177e4SLinus Torvalds  *  Instead of using magical inumbers to determine the kind of object
111da177e4SLinus Torvalds  *  we allocate and fill in-core inodes upon lookup. They don't even
121da177e4SLinus Torvalds  *  go into icache. We cache the reference to task_struct upon lookup too.
131da177e4SLinus Torvalds  *  Eventually it should become a filesystem in its own. We don't use the
141da177e4SLinus Torvalds  *  rest of procfs anymore.
15e070ad49SMauricio Lin  *
16e070ad49SMauricio Lin  *
17e070ad49SMauricio Lin  *  Changelog:
18e070ad49SMauricio Lin  *  17-Jan-2005
19e070ad49SMauricio Lin  *  Allan Bezerra
20e070ad49SMauricio Lin  *  Bruna Moreira <bruna.moreira@indt.org.br>
21e070ad49SMauricio Lin  *  Edjard Mota <edjard.mota@indt.org.br>
22e070ad49SMauricio Lin  *  Ilias Biris <ilias.biris@indt.org.br>
23e070ad49SMauricio Lin  *  Mauricio Lin <mauricio.lin@indt.org.br>
24e070ad49SMauricio Lin  *
25e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26e070ad49SMauricio Lin  *
27e070ad49SMauricio Lin  *  A new process specific entry (smaps) included in /proc. It shows the
28e070ad49SMauricio Lin  *  size of rss for each memory area. The maps entry lacks information
29e070ad49SMauricio Lin  *  about physical memory size (rss) for each mapped file, i.e.,
30e070ad49SMauricio Lin  *  rss information for executables and library files.
31e070ad49SMauricio Lin  *  This additional information is useful for any tools that need to know
32e070ad49SMauricio Lin  *  about physical memory consumption for a process specific library.
33e070ad49SMauricio Lin  *
34e070ad49SMauricio Lin  *  Changelog:
35e070ad49SMauricio Lin  *  21-Feb-2005
36e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37e070ad49SMauricio Lin  *  Pud inclusion in the page table walking.
38e070ad49SMauricio Lin  *
39e070ad49SMauricio Lin  *  ChangeLog:
40e070ad49SMauricio Lin  *  10-Mar-2005
41e070ad49SMauricio Lin  *  10LE Instituto Nokia de Tecnologia - INdT:
42e070ad49SMauricio Lin  *  A better way to walks through the page table as suggested by Hugh Dickins.
43e070ad49SMauricio Lin  *
44e070ad49SMauricio Lin  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
45e070ad49SMauricio Lin  *  Smaps information related to shared, private, clean and dirty pages.
46e070ad49SMauricio Lin  *
47e070ad49SMauricio Lin  *  Paul Mundt <paul.mundt@nokia.com>:
48e070ad49SMauricio Lin  *  Overall revision about smaps.
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds 
517c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include <linux/errno.h>
541da177e4SLinus Torvalds #include <linux/time.h>
551da177e4SLinus Torvalds #include <linux/proc_fs.h>
561da177e4SLinus Torvalds #include <linux/stat.h>
575995477aSAndrea Righi #include <linux/task_io_accounting_ops.h>
581da177e4SLinus Torvalds #include <linux/init.h>
5916f7e0feSRandy Dunlap #include <linux/capability.h>
601da177e4SLinus Torvalds #include <linux/file.h>
619f3acc31SAl Viro #include <linux/fdtable.h>
6294f8f3b0SKent Overstreet #include <linux/generic-radix-tree.h>
631da177e4SLinus Torvalds #include <linux/string.h>
641da177e4SLinus Torvalds #include <linux/seq_file.h>
651da177e4SLinus Torvalds #include <linux/namei.h>
666b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
671da177e4SLinus Torvalds #include <linux/mm.h>
68a63d83f4SDavid Rientjes #include <linux/swap.h>
69b835996fSDipankar Sarma #include <linux/rcupdate.h>
701da177e4SLinus Torvalds #include <linux/kallsyms.h>
712ec220e2SKen Chen #include <linux/stacktrace.h>
72d85f50d5SNeil Horman #include <linux/resource.h>
735096add8SKees Cook #include <linux/module.h>
741da177e4SLinus Torvalds #include <linux/mount.h>
751da177e4SLinus Torvalds #include <linux/security.h>
761da177e4SLinus Torvalds #include <linux/ptrace.h>
770d094efeSRoland McGrath #include <linux/tracehook.h>
7887ebdc00SAndrew Morton #include <linux/printk.h>
79efb1a57dSAlexey Dobriyan #include <linux/cache.h>
80a424316cSPaul Menage #include <linux/cgroup.h>
811da177e4SLinus Torvalds #include <linux/cpuset.h>
821da177e4SLinus Torvalds #include <linux/audit.h>
835addc5ddSAl Viro #include <linux/poll.h>
841651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
858ac773b4SAlexey Dobriyan #include <linux/oom.h>
863cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8760347f67SPavel Emelyanov #include <linux/pid_namespace.h>
8822d917d8SEric W. Biederman #include <linux/user_namespace.h>
895ad4e53bSAl Viro #include <linux/fs_struct.h>
905a0e3ad6STejun Heo #include <linux/slab.h>
914eb5aaa3SIngo Molnar #include <linux/sched/autogroup.h>
926e84f315SIngo Molnar #include <linux/sched/mm.h>
93f7ccbae4SIngo Molnar #include <linux/sched/coredump.h>
94b17b0153SIngo Molnar #include <linux/sched/debug.h>
953905f9adSIngo Molnar #include <linux/sched/stat.h>
9648f6a7a5SPavel Emelyanov #include <linux/posix-timers.h>
9743d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
981da177e4SLinus Torvalds #include "internal.h"
99faf60af1SCyrill Gorcunov #include "fd.h"
1001da177e4SLinus Torvalds 
101ac7f1061SAlexey Dobriyan #include "../../lib/kstrtox.h"
102ac7f1061SAlexey Dobriyan 
1030f2fe20fSEric W. Biederman /* NOTE:
1040f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
1050f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
1060f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
1070f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
1080f2fe20fSEric W. Biederman  *
1090f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
1100f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
1110f2fe20fSEric W. Biederman  */
1120f2fe20fSEric W. Biederman 
113efb1a57dSAlexey Dobriyan static u8 nlink_tid __ro_after_init;
114efb1a57dSAlexey Dobriyan static u8 nlink_tgid __ro_after_init;
1151270dd8dSAlexey Dobriyan 
1161da177e4SLinus Torvalds struct pid_entry {
117cedbccabSAlexey Dobriyan 	const char *name;
118623f594eSAlexey Dobriyan 	unsigned int len;
119d161a13fSAl Viro 	umode_t mode;
120c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
12100977a59SArjan van de Ven 	const struct file_operations *fop;
12220cdc894SEric W. Biederman 	union proc_op op;
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
12561a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
12620cdc894SEric W. Biederman 	.name = (NAME),					\
127c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
12820cdc894SEric W. Biederman 	.mode = MODE,					\
12920cdc894SEric W. Biederman 	.iop  = IOP,					\
13020cdc894SEric W. Biederman 	.fop  = FOP,					\
13120cdc894SEric W. Biederman 	.op   = OP,					\
13220cdc894SEric W. Biederman }
13320cdc894SEric W. Biederman 
134631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
135631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
13761a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
13820cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
139631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
140631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
141631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
143be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)),			\
144be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
145631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1466d9c939dSCasey Schaufler #define ATTR(LSM, NAME, MODE)				\
1476d9c939dSCasey Schaufler 	NOD(NAME, (S_IFREG|(MODE)),			\
1486d9c939dSCasey Schaufler 		NULL, &proc_pid_attr_operations,	\
1496d9c939dSCasey Schaufler 		{ .lsm = LSM })
1501da177e4SLinus Torvalds 
151aed54175SVegard Nossum /*
152aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
153aed54175SVegard Nossum  * and .. links.
154aed54175SVegard Nossum  */
1551270dd8dSAlexey Dobriyan static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
156aed54175SVegard Nossum 	unsigned int n)
157aed54175SVegard Nossum {
158aed54175SVegard Nossum 	unsigned int i;
159aed54175SVegard Nossum 	unsigned int count;
160aed54175SVegard Nossum 
1611270dd8dSAlexey Dobriyan 	count = 2;
162aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
163aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
164aed54175SVegard Nossum 			++count;
165aed54175SVegard Nossum 	}
166aed54175SVegard Nossum 
167aed54175SVegard Nossum 	return count;
168aed54175SVegard Nossum }
169aed54175SVegard Nossum 
170f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1711da177e4SLinus Torvalds {
1727c2c7d99SHugh Dickins 	int result = -ENOENT;
1737c2c7d99SHugh Dickins 
1740494f6ecSMiklos Szeredi 	task_lock(task);
175f7ad3c6bSMiklos Szeredi 	if (task->fs) {
176f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1777c2c7d99SHugh Dickins 		result = 0;
1787c2c7d99SHugh Dickins 	}
1790494f6ecSMiklos Szeredi 	task_unlock(task);
1807c2c7d99SHugh Dickins 	return result;
1810494f6ecSMiklos Szeredi }
1820494f6ecSMiklos Szeredi 
1837773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1840494f6ecSMiklos Szeredi {
1852b0143b5SDavid Howells 	struct task_struct *task = get_proc_task(d_inode(dentry));
1860494f6ecSMiklos Szeredi 	int result = -ENOENT;
18799f89551SEric W. Biederman 
18899f89551SEric W. Biederman 	if (task) {
189f7ad3c6bSMiklos Szeredi 		task_lock(task);
190f7ad3c6bSMiklos Szeredi 		if (task->fs) {
191f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
192f7ad3c6bSMiklos Szeredi 			result = 0;
193f7ad3c6bSMiklos Szeredi 		}
194f7ad3c6bSMiklos Szeredi 		task_unlock(task);
19599f89551SEric W. Biederman 		put_task_struct(task);
19699f89551SEric W. Biederman 	}
1971da177e4SLinus Torvalds 	return result;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2007773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
2011da177e4SLinus Torvalds {
2022b0143b5SDavid Howells 	struct task_struct *task = get_proc_task(d_inode(dentry));
2031da177e4SLinus Torvalds 	int result = -ENOENT;
20499f89551SEric W. Biederman 
20599f89551SEric W. Biederman 	if (task) {
206f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
20799f89551SEric W. Biederman 		put_task_struct(task);
20899f89551SEric W. Biederman 	}
2091da177e4SLinus Torvalds 	return result;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
212e4b4e441SLinus Torvalds static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
2135ab82718SLinus Torvalds 			      size_t count, loff_t *ppos)
2141da177e4SLinus Torvalds {
215c2c0bb44SAlexey Dobriyan 	unsigned long arg_start, arg_end, env_start, env_end;
2165ab82718SLinus Torvalds 	unsigned long pos, len;
2175ab82718SLinus Torvalds 	char *page;
218c2c0bb44SAlexey Dobriyan 
219c2c0bb44SAlexey Dobriyan 	/* Check if process spawned far enough to have cmdline. */
220e4b4e441SLinus Torvalds 	if (!mm->env_end)
221e4b4e441SLinus Torvalds 		return 0;
222c2c0bb44SAlexey Dobriyan 
22388aa7cc6SYang Shi 	spin_lock(&mm->arg_lock);
224c2c0bb44SAlexey Dobriyan 	arg_start = mm->arg_start;
225c2c0bb44SAlexey Dobriyan 	arg_end = mm->arg_end;
226c2c0bb44SAlexey Dobriyan 	env_start = mm->env_start;
227c2c0bb44SAlexey Dobriyan 	env_end = mm->env_end;
22888aa7cc6SYang Shi 	spin_unlock(&mm->arg_lock);
229c2c0bb44SAlexey Dobriyan 
2305ab82718SLinus Torvalds 	if (arg_start >= arg_end)
2315ab82718SLinus Torvalds 		return 0;
2326a6cbe75SAlexey Dobriyan 
233c2c0bb44SAlexey Dobriyan 	/*
2345ab82718SLinus Torvalds 	 * We have traditionally allowed the user to re-write
2355ab82718SLinus Torvalds 	 * the argument strings and overflow the end result
2365ab82718SLinus Torvalds 	 * into the environment section. But only do that if
2375ab82718SLinus Torvalds 	 * the environment area is contiguous to the arguments.
238c2c0bb44SAlexey Dobriyan 	 */
2395ab82718SLinus Torvalds 	if (env_start != arg_end || env_start >= env_end)
2405ab82718SLinus Torvalds 		env_start = env_end = arg_end;
241c2c0bb44SAlexey Dobriyan 
242f5b65348SLinus Torvalds 	/* .. and limit it to a maximum of one page of slop */
243f5b65348SLinus Torvalds 	if (env_end >= arg_end + PAGE_SIZE)
244f5b65348SLinus Torvalds 		env_end = arg_end + PAGE_SIZE - 1;
245f5b65348SLinus Torvalds 
2465ab82718SLinus Torvalds 	/* We're not going to care if "*ppos" has high bits set */
2475ab82718SLinus Torvalds 	pos = arg_start + *ppos;
2483cb4e162SAlexey Dobriyan 
2495ab82718SLinus Torvalds 	/* .. but we do check the result is in the proper range */
2505ab82718SLinus Torvalds 	if (pos < arg_start || pos >= env_end)
2515ab82718SLinus Torvalds 		return 0;
252c2c0bb44SAlexey Dobriyan 
2535ab82718SLinus Torvalds 	/* .. and we never go past env_end */
2545ab82718SLinus Torvalds 	if (env_end - pos < count)
2555ab82718SLinus Torvalds 		count = env_end - pos;
2563cb4e162SAlexey Dobriyan 
2575ab82718SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
2585ab82718SLinus Torvalds 	if (!page)
2595ab82718SLinus Torvalds 		return -ENOMEM;
260c2c0bb44SAlexey Dobriyan 
2615ab82718SLinus Torvalds 	len = 0;
2625ab82718SLinus Torvalds 	while (count) {
2635ab82718SLinus Torvalds 		int got;
2645ab82718SLinus Torvalds 		size_t size = min_t(size_t, PAGE_SIZE, count);
265f5b65348SLinus Torvalds 		long offset;
2665ab82718SLinus Torvalds 
267f5b65348SLinus Torvalds 		/*
268f5b65348SLinus Torvalds 		 * Are we already starting past the official end?
269f5b65348SLinus Torvalds 		 * We always include the last byte that is *supposed*
270f5b65348SLinus Torvalds 		 * to be NUL
271f5b65348SLinus Torvalds 		 */
272f5b65348SLinus Torvalds 		offset = (pos >= arg_end) ? pos - arg_end + 1 : 0;
273f5b65348SLinus Torvalds 
274f5b65348SLinus Torvalds 		got = access_remote_vm(mm, pos - offset, page, size + offset, FOLL_ANON);
275f5b65348SLinus Torvalds 		if (got <= offset)
2765ab82718SLinus Torvalds 			break;
277f5b65348SLinus Torvalds 		got -= offset;
2785ab82718SLinus Torvalds 
2795ab82718SLinus Torvalds 		/* Don't walk past a NUL character once you hit arg_end */
2805ab82718SLinus Torvalds 		if (pos + got >= arg_end) {
2815ab82718SLinus Torvalds 			int n = 0;
282c2c0bb44SAlexey Dobriyan 
283c2c0bb44SAlexey Dobriyan 			/*
2845ab82718SLinus Torvalds 			 * If we started before 'arg_end' but ended up
2855ab82718SLinus Torvalds 			 * at or after it, we start the NUL character
2865ab82718SLinus Torvalds 			 * check at arg_end-1 (where we expect the normal
2875ab82718SLinus Torvalds 			 * EOF to be).
2885ab82718SLinus Torvalds 			 *
2895ab82718SLinus Torvalds 			 * NOTE! This is smaller than 'got', because
2905ab82718SLinus Torvalds 			 * pos + got >= arg_end
291c2c0bb44SAlexey Dobriyan 			 */
2925ab82718SLinus Torvalds 			if (pos < arg_end)
2935ab82718SLinus Torvalds 				n = arg_end - pos - 1;
294c2c0bb44SAlexey Dobriyan 
2955ab82718SLinus Torvalds 			/* Cut off at first NUL after 'n' */
296f5b65348SLinus Torvalds 			got = n + strnlen(page+n, offset+got-n);
297f5b65348SLinus Torvalds 			if (got < offset)
2985ab82718SLinus Torvalds 				break;
299f5b65348SLinus Torvalds 			got -= offset;
300f5b65348SLinus Torvalds 
301f5b65348SLinus Torvalds 			/* Include the NUL if it existed */
302f5b65348SLinus Torvalds 			if (got < size)
303f5b65348SLinus Torvalds 				got++;
304c2c0bb44SAlexey Dobriyan 		}
305c2c0bb44SAlexey Dobriyan 
306f5b65348SLinus Torvalds 		got -= copy_to_user(buf, page+offset, got);
3075ab82718SLinus Torvalds 		if (unlikely(!got)) {
3085ab82718SLinus Torvalds 			if (!len)
3095ab82718SLinus Torvalds 				len = -EFAULT;
3105ab82718SLinus Torvalds 			break;
3115ab82718SLinus Torvalds 		}
3125ab82718SLinus Torvalds 		pos += got;
3135ab82718SLinus Torvalds 		buf += got;
3145ab82718SLinus Torvalds 		len += got;
3155ab82718SLinus Torvalds 		count -= got;
316c2c0bb44SAlexey Dobriyan 	}
317a0a07b87SAlexey Dobriyan 
318c2c0bb44SAlexey Dobriyan 	free_page((unsigned long)page);
3195ab82718SLinus Torvalds 	return len;
320c2c0bb44SAlexey Dobriyan }
321c2c0bb44SAlexey Dobriyan 
322e4b4e441SLinus Torvalds static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
323e4b4e441SLinus Torvalds 				size_t count, loff_t *pos)
324e4b4e441SLinus Torvalds {
325e4b4e441SLinus Torvalds 	struct mm_struct *mm;
326e4b4e441SLinus Torvalds 	ssize_t ret;
327e4b4e441SLinus Torvalds 
328e4b4e441SLinus Torvalds 	mm = get_task_mm(tsk);
329e4b4e441SLinus Torvalds 	if (!mm)
330e4b4e441SLinus Torvalds 		return 0;
331e4b4e441SLinus Torvalds 
332e4b4e441SLinus Torvalds 	ret = get_mm_cmdline(mm, buf, count, pos);
333c2c0bb44SAlexey Dobriyan 	mmput(mm);
334e4b4e441SLinus Torvalds 	return ret;
335e4b4e441SLinus Torvalds }
336e4b4e441SLinus Torvalds 
337e4b4e441SLinus Torvalds static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
338e4b4e441SLinus Torvalds 				     size_t count, loff_t *pos)
339e4b4e441SLinus Torvalds {
340e4b4e441SLinus Torvalds 	struct task_struct *tsk;
341e4b4e441SLinus Torvalds 	ssize_t ret;
342e4b4e441SLinus Torvalds 
343e4b4e441SLinus Torvalds 	BUG_ON(*pos < 0);
344e4b4e441SLinus Torvalds 
345e4b4e441SLinus Torvalds 	tsk = get_proc_task(file_inode(file));
346e4b4e441SLinus Torvalds 	if (!tsk)
347e4b4e441SLinus Torvalds 		return -ESRCH;
348e4b4e441SLinus Torvalds 	ret = get_task_cmdline(tsk, buf, count, pos);
349e4b4e441SLinus Torvalds 	put_task_struct(tsk);
350e4b4e441SLinus Torvalds 	if (ret > 0)
351e4b4e441SLinus Torvalds 		*pos += ret;
352e4b4e441SLinus Torvalds 	return ret;
353c2c0bb44SAlexey Dobriyan }
354c2c0bb44SAlexey Dobriyan 
355c2c0bb44SAlexey Dobriyan static const struct file_operations proc_pid_cmdline_ops = {
356c2c0bb44SAlexey Dobriyan 	.read	= proc_pid_cmdline_read,
357c2c0bb44SAlexey Dobriyan 	.llseek	= generic_file_llseek,
358c2c0bb44SAlexey Dobriyan };
359c2c0bb44SAlexey Dobriyan 
3601da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3611da177e4SLinus Torvalds /*
3621da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3631da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3641da177e4SLinus Torvalds  */
365edfcd606SAlexey Dobriyan static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
366edfcd606SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
3671da177e4SLinus Torvalds {
368ffb45122SAlexey Dobriyan 	unsigned long wchan;
3699281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3701da177e4SLinus Torvalds 
37124b2ec21SAlexey Dobriyan 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
37224b2ec21SAlexey Dobriyan 		goto print0;
37324b2ec21SAlexey Dobriyan 
3741da177e4SLinus Torvalds 	wchan = get_wchan(task);
37524b2ec21SAlexey Dobriyan 	if (wchan && !lookup_symbol_name(wchan, symname)) {
37621dae0adSAlexey Dobriyan 		seq_puts(m, symname);
37724b2ec21SAlexey Dobriyan 		return 0;
37824b2ec21SAlexey Dobriyan 	}
37925ce3191SJoe Perches 
38024b2ec21SAlexey Dobriyan print0:
38124b2ec21SAlexey Dobriyan 	seq_putc(m, '0');
38225ce3191SJoe Perches 	return 0;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3851da177e4SLinus Torvalds 
386a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
387a9712bc1SAl Viro {
388a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
389a9712bc1SAl Viro 	if (err)
390a9712bc1SAl Viro 		return err;
391caaee623SJann Horn 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
392a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
393a9712bc1SAl Viro 		return -EPERM;
394a9712bc1SAl Viro 	}
395a9712bc1SAl Viro 	return 0;
396a9712bc1SAl Viro }
397a9712bc1SAl Viro 
398a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
399a9712bc1SAl Viro {
400a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
401a9712bc1SAl Viro }
402a9712bc1SAl Viro 
4032ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
4042ec220e2SKen Chen 
4052ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
4062ec220e2SKen Chen 
4072ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
4082ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
4092ec220e2SKen Chen {
4102ec220e2SKen Chen 	unsigned long *entries;
411a9712bc1SAl Viro 	int err;
4122ec220e2SKen Chen 
413f8a00cefSJann Horn 	/*
414f8a00cefSJann Horn 	 * The ability to racily run the kernel stack unwinder on a running task
415f8a00cefSJann Horn 	 * and then observe the unwinder output is scary; while it is useful for
416f8a00cefSJann Horn 	 * debugging kernel issues, it can also allow an attacker to leak kernel
417f8a00cefSJann Horn 	 * stack contents.
418f8a00cefSJann Horn 	 * Doing this in a manner that is at least safe from races would require
419f8a00cefSJann Horn 	 * some work to ensure that the remote task can not be scheduled; and
420f8a00cefSJann Horn 	 * even then, this would still expose the unwinder as local attack
421f8a00cefSJann Horn 	 * surface.
422f8a00cefSJann Horn 	 * Therefore, this interface is restricted to root.
423f8a00cefSJann Horn 	 */
424f8a00cefSJann Horn 	if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
425f8a00cefSJann Horn 		return -EACCES;
426f8a00cefSJann Horn 
4276da2ec56SKees Cook 	entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
4286da2ec56SKees Cook 				GFP_KERNEL);
4292ec220e2SKen Chen 	if (!entries)
4302ec220e2SKen Chen 		return -ENOMEM;
4312ec220e2SKen Chen 
432a9712bc1SAl Viro 	err = lock_trace(task);
433a9712bc1SAl Viro 	if (!err) {
434e988e5ecSThomas Gleixner 		unsigned int i, nr_entries;
4355d008fb4SAlexey Dobriyan 
436e988e5ecSThomas Gleixner 		nr_entries = stack_trace_save_tsk(task, entries,
437e988e5ecSThomas Gleixner 						  MAX_STACK_TRACE_DEPTH, 0);
4382ec220e2SKen Chen 
439e988e5ecSThomas Gleixner 		for (i = 0; i < nr_entries; i++) {
4408f5abe84SLinus Torvalds 			seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
4412ec220e2SKen Chen 		}
442e988e5ecSThomas Gleixner 
443a9712bc1SAl Viro 		unlock_trace(task);
444a9712bc1SAl Viro 	}
4452ec220e2SKen Chen 	kfree(entries);
4462ec220e2SKen Chen 
447a9712bc1SAl Viro 	return err;
4482ec220e2SKen Chen }
4492ec220e2SKen Chen #endif
4502ec220e2SKen Chen 
4515968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
4541da177e4SLinus Torvalds  */
455f6e826caSAlexey Dobriyan static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
456f6e826caSAlexey Dobriyan 			      struct pid *pid, struct task_struct *task)
4571da177e4SLinus Torvalds {
4585968ceceSNaveen N. Rao 	if (unlikely(!sched_info_on()))
45908b55775SAlexey Dobriyan 		seq_puts(m, "0 0 0\n");
4605968ceceSNaveen N. Rao 	else
46125ce3191SJoe Perches 		seq_printf(m, "%llu %llu %lu\n",
462826e08b0SIngo Molnar 		   (unsigned long long)task->se.sum_exec_runtime,
463826e08b0SIngo Molnar 		   (unsigned long long)task->sched_info.run_delay,
4642d72376bSIngo Molnar 		   task->sched_info.pcount);
46525ce3191SJoe Perches 
46625ce3191SJoe Perches 	return 0;
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds #endif
4691da177e4SLinus Torvalds 
4709745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
4719745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
4729745512cSArjan van de Ven {
4739745512cSArjan van de Ven 	int i;
47413d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
47513d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
4769745512cSArjan van de Ven 
47713d77c37SHiroshi Shimamoto 	if (!task)
47813d77c37SHiroshi Shimamoto 		return -ESRCH;
47913d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
480f6d2f584SAlexey Dobriyan 	for (i = 0; i < LT_SAVECOUNT; i++) {
48134e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
48234e49d4fSJoe Perches 		if (lr->backtrace[0]) {
4839745512cSArjan van de Ven 			int q;
4849745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
48534e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
4869745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
48734e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
488accddc41SThomas Gleixner 
48934e49d4fSJoe Perches 				if (!bt)
4909745512cSArjan van de Ven 					break;
49134e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
4929745512cSArjan van de Ven 			}
4939d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
4949745512cSArjan van de Ven 		}
4959745512cSArjan van de Ven 
4969745512cSArjan van de Ven 	}
49713d77c37SHiroshi Shimamoto 	put_task_struct(task);
4989745512cSArjan van de Ven 	return 0;
4999745512cSArjan van de Ven }
5009745512cSArjan van de Ven 
5019745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
5029745512cSArjan van de Ven {
50313d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
504d6643d12SHiroshi Shimamoto }
505d6643d12SHiroshi Shimamoto 
5069745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
5079745512cSArjan van de Ven 			    size_t count, loff_t *offs)
5089745512cSArjan van de Ven {
509496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
5109745512cSArjan van de Ven 
51113d77c37SHiroshi Shimamoto 	if (!task)
51213d77c37SHiroshi Shimamoto 		return -ESRCH;
513e02c9b0dSLin Feng 	clear_tsk_latency_tracing(task);
51413d77c37SHiroshi Shimamoto 	put_task_struct(task);
5159745512cSArjan van de Ven 
5169745512cSArjan van de Ven 	return count;
5179745512cSArjan van de Ven }
5189745512cSArjan van de Ven 
5199745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
5209745512cSArjan van de Ven 	.open		= lstats_open,
5219745512cSArjan van de Ven 	.read		= seq_read,
5229745512cSArjan van de Ven 	.write		= lstats_write,
5239745512cSArjan van de Ven 	.llseek		= seq_lseek,
52413d77c37SHiroshi Shimamoto 	.release	= single_release,
5259745512cSArjan van de Ven };
5269745512cSArjan van de Ven 
5279745512cSArjan van de Ven #endif
5289745512cSArjan van de Ven 
5296ba51e37SAlexey Dobriyan static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
5306ba51e37SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
5311da177e4SLinus Torvalds {
532ca79b0c2SArun KS 	unsigned long totalpages = totalram_pages() + total_swap_pages;
533b95c35e7SOleg Nesterov 	unsigned long points = 0;
5341da177e4SLinus Torvalds 
535a7f638f9SDavid Rientjes 	points = oom_badness(task, NULL, NULL, totalpages) *
536a7f638f9SDavid Rientjes 					1000 / totalpages;
53725ce3191SJoe Perches 	seq_printf(m, "%lu\n", points);
53825ce3191SJoe Perches 
53925ce3191SJoe Perches 	return 0;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
542d85f50d5SNeil Horman struct limit_names {
543cedbccabSAlexey Dobriyan 	const char *name;
544cedbccabSAlexey Dobriyan 	const char *unit;
545d85f50d5SNeil Horman };
546d85f50d5SNeil Horman 
547d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
548cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
549d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
550d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
551d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
552d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
553d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
554d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
555d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
556d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
557d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
558d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
559d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
560d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
561d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
562d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
5638808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
564d85f50d5SNeil Horman };
565d85f50d5SNeil Horman 
566d85f50d5SNeil Horman /* Display limits for a process */
5671c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
5681c963eb1SAlexey Dobriyan 			   struct pid *pid, struct task_struct *task)
569d85f50d5SNeil Horman {
570d85f50d5SNeil Horman 	unsigned int i;
571d85f50d5SNeil Horman 	unsigned long flags;
572d85f50d5SNeil Horman 
573d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
574d85f50d5SNeil Horman 
575a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
576d85f50d5SNeil Horman 		return 0;
577d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
578d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
579d85f50d5SNeil Horman 
580d85f50d5SNeil Horman 	/*
581d85f50d5SNeil Horman 	 * print the file header
582d85f50d5SNeil Horman 	 */
583afe922c2SAlexey Dobriyan 	seq_puts(m, "Limit                     "
584afe922c2SAlexey Dobriyan 		"Soft Limit           "
585afe922c2SAlexey Dobriyan 		"Hard Limit           "
586afe922c2SAlexey Dobriyan 		"Units     \n");
587d85f50d5SNeil Horman 
588d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
589d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
5901c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20s ",
591d85f50d5SNeil Horman 				   lnames[i].name, "unlimited");
592d85f50d5SNeil Horman 		else
5931c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20lu ",
594d85f50d5SNeil Horman 				   lnames[i].name, rlim[i].rlim_cur);
595d85f50d5SNeil Horman 
596d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
5971c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20s ", "unlimited");
598d85f50d5SNeil Horman 		else
5991c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20lu ", rlim[i].rlim_max);
600d85f50d5SNeil Horman 
601d85f50d5SNeil Horman 		if (lnames[i].unit)
6021c963eb1SAlexey Dobriyan 			seq_printf(m, "%-10s\n", lnames[i].unit);
603d85f50d5SNeil Horman 		else
6041c963eb1SAlexey Dobriyan 			seq_putc(m, '\n');
605d85f50d5SNeil Horman 	}
606d85f50d5SNeil Horman 
6071c963eb1SAlexey Dobriyan 	return 0;
608d85f50d5SNeil Horman }
609d85f50d5SNeil Horman 
610ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
61109d93bd6SAlexey Dobriyan static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
61209d93bd6SAlexey Dobriyan 			    struct pid *pid, struct task_struct *task)
613ebcb6734SRoland McGrath {
614631b7abaSSteven Rostedt (Red Hat) 	struct syscall_info info;
615631b7abaSSteven Rostedt (Red Hat) 	u64 *args = &info.data.args[0];
61625ce3191SJoe Perches 	int res;
61725ce3191SJoe Perches 
61825ce3191SJoe Perches 	res = lock_trace(task);
619a9712bc1SAl Viro 	if (res)
620a9712bc1SAl Viro 		return res;
621ebcb6734SRoland McGrath 
622631b7abaSSteven Rostedt (Red Hat) 	if (task_current_syscall(task, &info))
62309d93bd6SAlexey Dobriyan 		seq_puts(m, "running\n");
624631b7abaSSteven Rostedt (Red Hat) 	else if (info.data.nr < 0)
625631b7abaSSteven Rostedt (Red Hat) 		seq_printf(m, "%d 0x%llx 0x%llx\n",
626631b7abaSSteven Rostedt (Red Hat) 			   info.data.nr, info.sp, info.data.instruction_pointer);
627a9712bc1SAl Viro 	else
62809d93bd6SAlexey Dobriyan 		seq_printf(m,
629631b7abaSSteven Rostedt (Red Hat) 		       "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
630631b7abaSSteven Rostedt (Red Hat) 		       info.data.nr,
631ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
632631b7abaSSteven Rostedt (Red Hat) 		       info.sp, info.data.instruction_pointer);
633a9712bc1SAl Viro 	unlock_trace(task);
63425ce3191SJoe Perches 
63525ce3191SJoe Perches 	return 0;
636ebcb6734SRoland McGrath }
637ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
638ebcb6734SRoland McGrath 
6391da177e4SLinus Torvalds /************************************************************************/
6401da177e4SLinus Torvalds /*                       Here the fs part begins                        */
6411da177e4SLinus Torvalds /************************************************************************/
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds /* permission checks */
644778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
6451da177e4SLinus Torvalds {
646778c1144SEric W. Biederman 	struct task_struct *task;
647778c1144SEric W. Biederman 	int allowed = 0;
648df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
649df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
650df26c40eSEric W. Biederman 	 * information.
651778c1144SEric W. Biederman 	 */
652778c1144SEric W. Biederman 	task = get_proc_task(inode);
653df26c40eSEric W. Biederman 	if (task) {
654caaee623SJann Horn 		allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
655778c1144SEric W. Biederman 		put_task_struct(task);
656df26c40eSEric W. Biederman 	}
657778c1144SEric W. Biederman 	return allowed;
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
6606b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
6616d76fa58SLinus Torvalds {
6626d76fa58SLinus Torvalds 	int error;
6632b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
6646d76fa58SLinus Torvalds 
6656d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
6666d76fa58SLinus Torvalds 		return -EPERM;
6676d76fa58SLinus Torvalds 
66831051c85SJan Kara 	error = setattr_prepare(dentry, attr);
6691025774cSChristoph Hellwig 	if (error)
6706d76fa58SLinus Torvalds 		return error;
6711025774cSChristoph Hellwig 
6721025774cSChristoph Hellwig 	setattr_copy(inode, attr);
6731025774cSChristoph Hellwig 	mark_inode_dirty(inode);
6741025774cSChristoph Hellwig 	return 0;
6756d76fa58SLinus Torvalds }
6766d76fa58SLinus Torvalds 
6770499680aSVasiliy Kulikov /*
6780499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
6790499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
6800499680aSVasiliy Kulikov  */
6810499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
6820499680aSVasiliy Kulikov 				 struct task_struct *task,
6830499680aSVasiliy Kulikov 				 int hide_pid_min)
6840499680aSVasiliy Kulikov {
6850499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
6860499680aSVasiliy Kulikov 		return true;
6870499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
6880499680aSVasiliy Kulikov 		return true;
689caaee623SJann Horn 	return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
6900499680aSVasiliy Kulikov }
6910499680aSVasiliy Kulikov 
6920499680aSVasiliy Kulikov 
6930499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
6940499680aSVasiliy Kulikov {
69576f668beSChristoph Hellwig 	struct pid_namespace *pid = proc_pid_ns(inode);
6960499680aSVasiliy Kulikov 	struct task_struct *task;
6970499680aSVasiliy Kulikov 	bool has_perms;
6980499680aSVasiliy Kulikov 
6990499680aSVasiliy Kulikov 	task = get_proc_task(inode);
700a2ef990aSXiaotian Feng 	if (!task)
701a2ef990aSXiaotian Feng 		return -ESRCH;
702796f571bSLafcadio Wluiki 	has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
7030499680aSVasiliy Kulikov 	put_task_struct(task);
7040499680aSVasiliy Kulikov 
7050499680aSVasiliy Kulikov 	if (!has_perms) {
706796f571bSLafcadio Wluiki 		if (pid->hide_pid == HIDEPID_INVISIBLE) {
7070499680aSVasiliy Kulikov 			/*
7080499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
7090499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
7100499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
7110499680aSVasiliy Kulikov 			 * in procfs at all.
7120499680aSVasiliy Kulikov 			 */
7130499680aSVasiliy Kulikov 			return -ENOENT;
7140499680aSVasiliy Kulikov 		}
7150499680aSVasiliy Kulikov 
7160499680aSVasiliy Kulikov 		return -EPERM;
7170499680aSVasiliy Kulikov 	}
7180499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
7190499680aSVasiliy Kulikov }
7200499680aSVasiliy Kulikov 
7210499680aSVasiliy Kulikov 
7220499680aSVasiliy Kulikov 
723c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
7246d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
7256d76fa58SLinus Torvalds };
7266d76fa58SLinus Torvalds 
727be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
728be614086SEric W. Biederman {
729be614086SEric W. Biederman 	struct inode *inode = m->private;
73076f668beSChristoph Hellwig 	struct pid_namespace *ns = proc_pid_ns(inode);
73176f668beSChristoph Hellwig 	struct pid *pid = proc_pid(inode);
732be614086SEric W. Biederman 	struct task_struct *task;
733be614086SEric W. Biederman 	int ret;
734be614086SEric W. Biederman 
735be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
736be614086SEric W. Biederman 	if (!task)
737be614086SEric W. Biederman 		return -ESRCH;
738be614086SEric W. Biederman 
739be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
740be614086SEric W. Biederman 
741be614086SEric W. Biederman 	put_task_struct(task);
742be614086SEric W. Biederman 	return ret;
743be614086SEric W. Biederman }
744be614086SEric W. Biederman 
745be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
746be614086SEric W. Biederman {
747c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
748be614086SEric W. Biederman }
749be614086SEric W. Biederman 
750be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
751be614086SEric W. Biederman 	.open		= proc_single_open,
752be614086SEric W. Biederman 	.read		= seq_read,
753be614086SEric W. Biederman 	.llseek		= seq_lseek,
754be614086SEric W. Biederman 	.release	= single_release,
755be614086SEric W. Biederman };
756be614086SEric W. Biederman 
7575381e169SOleg Nesterov 
7585381e169SOleg Nesterov struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
7591da177e4SLinus Torvalds {
7605381e169SOleg Nesterov 	struct task_struct *task = get_proc_task(inode);
7615381e169SOleg Nesterov 	struct mm_struct *mm = ERR_PTR(-ESRCH);
762e268337dSLinus Torvalds 
7635381e169SOleg Nesterov 	if (task) {
764caaee623SJann Horn 		mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
765e268337dSLinus Torvalds 		put_task_struct(task);
766e268337dSLinus Torvalds 
7675381e169SOleg Nesterov 		if (!IS_ERR_OR_NULL(mm)) {
7686d08f2c7SOleg Nesterov 			/* ensure this mm_struct can't be freed */
769f1f10076SVegard Nossum 			mmgrab(mm);
7706d08f2c7SOleg Nesterov 			/* but do not pin its memory */
7716d08f2c7SOleg Nesterov 			mmput(mm);
7726d08f2c7SOleg Nesterov 		}
7735381e169SOleg Nesterov 	}
7745381e169SOleg Nesterov 
7755381e169SOleg Nesterov 	return mm;
7765381e169SOleg Nesterov }
7775381e169SOleg Nesterov 
7785381e169SOleg Nesterov static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
7795381e169SOleg Nesterov {
7805381e169SOleg Nesterov 	struct mm_struct *mm = proc_mem_open(inode, mode);
7815381e169SOleg Nesterov 
7825381e169SOleg Nesterov 	if (IS_ERR(mm))
7835381e169SOleg Nesterov 		return PTR_ERR(mm);
7846d08f2c7SOleg Nesterov 
785e268337dSLinus Torvalds 	file->private_data = mm;
7861da177e4SLinus Torvalds 	return 0;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
789b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
790b409e578SCong Wang {
791bc452b4bSDjalal Harouni 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
792bc452b4bSDjalal Harouni 
793bc452b4bSDjalal Harouni 	/* OK to pass negative loff_t, we can catch out-of-range */
794bc452b4bSDjalal Harouni 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
795bc452b4bSDjalal Harouni 
796bc452b4bSDjalal Harouni 	return ret;
797b409e578SCong Wang }
798b409e578SCong Wang 
799572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
800572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
8011da177e4SLinus Torvalds {
802e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
803572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
804572d34b9SOleg Nesterov 	ssize_t copied;
8051da177e4SLinus Torvalds 	char *page;
806272ddc8bSLinus Torvalds 	unsigned int flags;
8071da177e4SLinus Torvalds 
808e268337dSLinus Torvalds 	if (!mm)
809e268337dSLinus Torvalds 		return 0;
8101da177e4SLinus Torvalds 
8110ee931c4SMichal Hocko 	page = (char *)__get_free_page(GFP_KERNEL);
8121da177e4SLinus Torvalds 	if (!page)
813e268337dSLinus Torvalds 		return -ENOMEM;
8141da177e4SLinus Torvalds 
815f7ca54f4SFrederik Deweerdt 	copied = 0;
816388f7934SVegard Nossum 	if (!mmget_not_zero(mm))
8176d08f2c7SOleg Nesterov 		goto free;
8186d08f2c7SOleg Nesterov 
819f511c0b1SLinus Torvalds 	flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
8206347e8d5SLorenzo Stoakes 
8211da177e4SLinus Torvalds 	while (count > 0) {
822572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
8231da177e4SLinus Torvalds 
824572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
8251da177e4SLinus Torvalds 			copied = -EFAULT;
8261da177e4SLinus Torvalds 			break;
8271da177e4SLinus Torvalds 		}
828572d34b9SOleg Nesterov 
8296347e8d5SLorenzo Stoakes 		this_len = access_remote_vm(mm, addr, page, this_len, flags);
830572d34b9SOleg Nesterov 		if (!this_len) {
8311da177e4SLinus Torvalds 			if (!copied)
8321da177e4SLinus Torvalds 				copied = -EIO;
8331da177e4SLinus Torvalds 			break;
8341da177e4SLinus Torvalds 		}
835572d34b9SOleg Nesterov 
836572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
837572d34b9SOleg Nesterov 			copied = -EFAULT;
838572d34b9SOleg Nesterov 			break;
8391da177e4SLinus Torvalds 		}
840572d34b9SOleg Nesterov 
841572d34b9SOleg Nesterov 		buf += this_len;
842572d34b9SOleg Nesterov 		addr += this_len;
843572d34b9SOleg Nesterov 		copied += this_len;
844572d34b9SOleg Nesterov 		count -= this_len;
845572d34b9SOleg Nesterov 	}
846572d34b9SOleg Nesterov 	*ppos = addr;
84730cd8903SKOSAKI Motohiro 
8486d08f2c7SOleg Nesterov 	mmput(mm);
8496d08f2c7SOleg Nesterov free:
85030cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
8511da177e4SLinus Torvalds 	return copied;
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
854572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
855572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
856572d34b9SOleg Nesterov {
857572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
858572d34b9SOleg Nesterov }
859572d34b9SOleg Nesterov 
860572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
861572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
862572d34b9SOleg Nesterov {
863572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
864572d34b9SOleg Nesterov }
865572d34b9SOleg Nesterov 
86685863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8671da177e4SLinus Torvalds {
8681da177e4SLinus Torvalds 	switch (orig) {
8691da177e4SLinus Torvalds 	case 0:
8701da177e4SLinus Torvalds 		file->f_pos = offset;
8711da177e4SLinus Torvalds 		break;
8721da177e4SLinus Torvalds 	case 1:
8731da177e4SLinus Torvalds 		file->f_pos += offset;
8741da177e4SLinus Torvalds 		break;
8751da177e4SLinus Torvalds 	default:
8761da177e4SLinus Torvalds 		return -EINVAL;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 	force_successful_syscall_return();
8791da177e4SLinus Torvalds 	return file->f_pos;
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
882e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
883e268337dSLinus Torvalds {
884e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
88571879d3cSOleg Nesterov 	if (mm)
8866d08f2c7SOleg Nesterov 		mmdrop(mm);
887e268337dSLinus Torvalds 	return 0;
888e268337dSLinus Torvalds }
889e268337dSLinus Torvalds 
89000977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8911da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8921da177e4SLinus Torvalds 	.read		= mem_read,
8931da177e4SLinus Torvalds 	.write		= mem_write,
8941da177e4SLinus Torvalds 	.open		= mem_open,
895e268337dSLinus Torvalds 	.release	= mem_release,
8961da177e4SLinus Torvalds };
8971da177e4SLinus Torvalds 
898b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
899b409e578SCong Wang {
900b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
901b409e578SCong Wang }
902b409e578SCong Wang 
903315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
904315e28c8SJames Pearson 			size_t count, loff_t *ppos)
905315e28c8SJames Pearson {
906315e28c8SJames Pearson 	char *page;
907315e28c8SJames Pearson 	unsigned long src = *ppos;
908b409e578SCong Wang 	int ret = 0;
909b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
910a3b609efSMateusz Guzik 	unsigned long env_start, env_end;
911315e28c8SJames Pearson 
9128148a73cSMathias Krause 	/* Ensure the process spawned far enough to have an environment. */
9138148a73cSMathias Krause 	if (!mm || !mm->env_end)
914b409e578SCong Wang 		return 0;
915315e28c8SJames Pearson 
9160ee931c4SMichal Hocko 	page = (char *)__get_free_page(GFP_KERNEL);
917315e28c8SJames Pearson 	if (!page)
918b409e578SCong Wang 		return -ENOMEM;
919315e28c8SJames Pearson 
920d6f64b89SAl Viro 	ret = 0;
921388f7934SVegard Nossum 	if (!mmget_not_zero(mm))
922b409e578SCong Wang 		goto free;
923a3b609efSMateusz Guzik 
92488aa7cc6SYang Shi 	spin_lock(&mm->arg_lock);
925a3b609efSMateusz Guzik 	env_start = mm->env_start;
926a3b609efSMateusz Guzik 	env_end = mm->env_end;
92788aa7cc6SYang Shi 	spin_unlock(&mm->arg_lock);
928a3b609efSMateusz Guzik 
929315e28c8SJames Pearson 	while (count > 0) {
930e8905ec2SDjalal Harouni 		size_t this_len, max_len;
931e8905ec2SDjalal Harouni 		int retval;
932e8905ec2SDjalal Harouni 
933a3b609efSMateusz Guzik 		if (src >= (env_end - env_start))
934e8905ec2SDjalal Harouni 			break;
935315e28c8SJames Pearson 
936a3b609efSMateusz Guzik 		this_len = env_end - (env_start + src);
937315e28c8SJames Pearson 
938e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
939e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
940315e28c8SJames Pearson 
9417f7ccc2cSWilly Tarreau 		retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
942315e28c8SJames Pearson 
943315e28c8SJames Pearson 		if (retval <= 0) {
944315e28c8SJames Pearson 			ret = retval;
945315e28c8SJames Pearson 			break;
946315e28c8SJames Pearson 		}
947315e28c8SJames Pearson 
948315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
949315e28c8SJames Pearson 			ret = -EFAULT;
950315e28c8SJames Pearson 			break;
951315e28c8SJames Pearson 		}
952315e28c8SJames Pearson 
953315e28c8SJames Pearson 		ret += retval;
954315e28c8SJames Pearson 		src += retval;
955315e28c8SJames Pearson 		buf += retval;
956315e28c8SJames Pearson 		count -= retval;
957315e28c8SJames Pearson 	}
958315e28c8SJames Pearson 	*ppos = src;
959315e28c8SJames Pearson 	mmput(mm);
960b409e578SCong Wang 
961b409e578SCong Wang free:
962315e28c8SJames Pearson 	free_page((unsigned long) page);
963315e28c8SJames Pearson 	return ret;
964315e28c8SJames Pearson }
965315e28c8SJames Pearson 
966315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
967b409e578SCong Wang 	.open		= environ_open,
968315e28c8SJames Pearson 	.read		= environ_read,
96987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
970b409e578SCong Wang 	.release	= mem_release,
971315e28c8SJames Pearson };
972315e28c8SJames Pearson 
973c5317167SAl Viro static int auxv_open(struct inode *inode, struct file *file)
974c5317167SAl Viro {
975c5317167SAl Viro 	return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
976c5317167SAl Viro }
977c5317167SAl Viro 
978c5317167SAl Viro static ssize_t auxv_read(struct file *file, char __user *buf,
979c5317167SAl Viro 			size_t count, loff_t *ppos)
980c5317167SAl Viro {
981c5317167SAl Viro 	struct mm_struct *mm = file->private_data;
982c5317167SAl Viro 	unsigned int nwords = 0;
98306b2849dSLeon Yu 
98406b2849dSLeon Yu 	if (!mm)
98506b2849dSLeon Yu 		return 0;
986c5317167SAl Viro 	do {
987c5317167SAl Viro 		nwords += 2;
988c5317167SAl Viro 	} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
989c5317167SAl Viro 	return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
990c5317167SAl Viro 				       nwords * sizeof(mm->saved_auxv[0]));
991c5317167SAl Viro }
992c5317167SAl Viro 
993c5317167SAl Viro static const struct file_operations proc_auxv_operations = {
994c5317167SAl Viro 	.open		= auxv_open,
995c5317167SAl Viro 	.read		= auxv_read,
996c5317167SAl Viro 	.llseek		= generic_file_llseek,
997c5317167SAl Viro 	.release	= mem_release,
998c5317167SAl Viro };
999c5317167SAl Viro 
1000fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1001fa0cbbf1SDavid Rientjes 			    loff_t *ppos)
1002fa0cbbf1SDavid Rientjes {
1003496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1004fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
1005fa0cbbf1SDavid Rientjes 	int oom_adj = OOM_ADJUST_MIN;
1006fa0cbbf1SDavid Rientjes 	size_t len;
1007fa0cbbf1SDavid Rientjes 
1008fa0cbbf1SDavid Rientjes 	if (!task)
1009fa0cbbf1SDavid Rientjes 		return -ESRCH;
1010fa0cbbf1SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1011fa0cbbf1SDavid Rientjes 		oom_adj = OOM_ADJUST_MAX;
1012fa0cbbf1SDavid Rientjes 	else
1013fa0cbbf1SDavid Rientjes 		oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1014fa0cbbf1SDavid Rientjes 			  OOM_SCORE_ADJ_MAX;
1015fa0cbbf1SDavid Rientjes 	put_task_struct(task);
1016fa0cbbf1SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1017fa0cbbf1SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1018fa0cbbf1SDavid Rientjes }
1019fa0cbbf1SDavid Rientjes 
10201d5f0acbSMichal Hocko static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
10211d5f0acbSMichal Hocko {
1022f913da59SMichal Hocko 	static DEFINE_MUTEX(oom_adj_mutex);
102344a70adeSMichal Hocko 	struct mm_struct *mm = NULL;
10241d5f0acbSMichal Hocko 	struct task_struct *task;
10251d5f0acbSMichal Hocko 	int err = 0;
10261d5f0acbSMichal Hocko 
10271d5f0acbSMichal Hocko 	task = get_proc_task(file_inode(file));
10281d5f0acbSMichal Hocko 	if (!task)
10291d5f0acbSMichal Hocko 		return -ESRCH;
10301d5f0acbSMichal Hocko 
10311d5f0acbSMichal Hocko 	mutex_lock(&oom_adj_mutex);
10321d5f0acbSMichal Hocko 	if (legacy) {
10331d5f0acbSMichal Hocko 		if (oom_adj < task->signal->oom_score_adj &&
10341d5f0acbSMichal Hocko 				!capable(CAP_SYS_RESOURCE)) {
10351d5f0acbSMichal Hocko 			err = -EACCES;
10361d5f0acbSMichal Hocko 			goto err_unlock;
10371d5f0acbSMichal Hocko 		}
10381d5f0acbSMichal Hocko 		/*
10391d5f0acbSMichal Hocko 		 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
10401d5f0acbSMichal Hocko 		 * /proc/pid/oom_score_adj instead.
10411d5f0acbSMichal Hocko 		 */
10421d5f0acbSMichal Hocko 		pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
10431d5f0acbSMichal Hocko 			  current->comm, task_pid_nr(current), task_pid_nr(task),
10441d5f0acbSMichal Hocko 			  task_pid_nr(task));
10451d5f0acbSMichal Hocko 	} else {
10461d5f0acbSMichal Hocko 		if ((short)oom_adj < task->signal->oom_score_adj_min &&
10471d5f0acbSMichal Hocko 				!capable(CAP_SYS_RESOURCE)) {
10481d5f0acbSMichal Hocko 			err = -EACCES;
10491d5f0acbSMichal Hocko 			goto err_unlock;
10501d5f0acbSMichal Hocko 		}
10511d5f0acbSMichal Hocko 	}
10521d5f0acbSMichal Hocko 
105344a70adeSMichal Hocko 	/*
105444a70adeSMichal Hocko 	 * Make sure we will check other processes sharing the mm if this is
105544a70adeSMichal Hocko 	 * not vfrok which wants its own oom_score_adj.
105644a70adeSMichal Hocko 	 * pin the mm so it doesn't go away and get reused after task_unlock
105744a70adeSMichal Hocko 	 */
105844a70adeSMichal Hocko 	if (!task->vfork_done) {
105944a70adeSMichal Hocko 		struct task_struct *p = find_lock_task_mm(task);
106044a70adeSMichal Hocko 
106144a70adeSMichal Hocko 		if (p) {
106244a70adeSMichal Hocko 			if (atomic_read(&p->mm->mm_users) > 1) {
106344a70adeSMichal Hocko 				mm = p->mm;
1064f1f10076SVegard Nossum 				mmgrab(mm);
106544a70adeSMichal Hocko 			}
106644a70adeSMichal Hocko 			task_unlock(p);
106744a70adeSMichal Hocko 		}
106844a70adeSMichal Hocko 	}
106944a70adeSMichal Hocko 
10701d5f0acbSMichal Hocko 	task->signal->oom_score_adj = oom_adj;
10711d5f0acbSMichal Hocko 	if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
10721d5f0acbSMichal Hocko 		task->signal->oom_score_adj_min = (short)oom_adj;
10731d5f0acbSMichal Hocko 	trace_oom_score_adj_update(task);
107444a70adeSMichal Hocko 
107544a70adeSMichal Hocko 	if (mm) {
107644a70adeSMichal Hocko 		struct task_struct *p;
107744a70adeSMichal Hocko 
107844a70adeSMichal Hocko 		rcu_read_lock();
107944a70adeSMichal Hocko 		for_each_process(p) {
108044a70adeSMichal Hocko 			if (same_thread_group(task, p))
108144a70adeSMichal Hocko 				continue;
108244a70adeSMichal Hocko 
108344a70adeSMichal Hocko 			/* do not touch kernel threads or the global init */
108444a70adeSMichal Hocko 			if (p->flags & PF_KTHREAD || is_global_init(p))
108544a70adeSMichal Hocko 				continue;
108644a70adeSMichal Hocko 
108744a70adeSMichal Hocko 			task_lock(p);
108844a70adeSMichal Hocko 			if (!p->vfork_done && process_shares_mm(p, mm)) {
108944a70adeSMichal Hocko 				p->signal->oom_score_adj = oom_adj;
109044a70adeSMichal Hocko 				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
109144a70adeSMichal Hocko 					p->signal->oom_score_adj_min = (short)oom_adj;
109244a70adeSMichal Hocko 			}
109344a70adeSMichal Hocko 			task_unlock(p);
109444a70adeSMichal Hocko 		}
109544a70adeSMichal Hocko 		rcu_read_unlock();
109644a70adeSMichal Hocko 		mmdrop(mm);
109744a70adeSMichal Hocko 	}
10981d5f0acbSMichal Hocko err_unlock:
10991d5f0acbSMichal Hocko 	mutex_unlock(&oom_adj_mutex);
11001d5f0acbSMichal Hocko 	put_task_struct(task);
11011d5f0acbSMichal Hocko 	return err;
11021d5f0acbSMichal Hocko }
1103f913da59SMichal Hocko 
1104b72bdfa7SDavid Rientjes /*
1105b72bdfa7SDavid Rientjes  * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1106b72bdfa7SDavid Rientjes  * kernels.  The effective policy is defined by oom_score_adj, which has a
1107b72bdfa7SDavid Rientjes  * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1108b72bdfa7SDavid Rientjes  * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1109b72bdfa7SDavid Rientjes  * Processes that become oom disabled via oom_adj will still be oom disabled
1110b72bdfa7SDavid Rientjes  * with this implementation.
1111b72bdfa7SDavid Rientjes  *
1112b72bdfa7SDavid Rientjes  * oom_adj cannot be removed since existing userspace binaries use it.
1113b72bdfa7SDavid Rientjes  */
1114fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1115fa0cbbf1SDavid Rientjes 			     size_t count, loff_t *ppos)
1116fa0cbbf1SDavid Rientjes {
1117fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
1118fa0cbbf1SDavid Rientjes 	int oom_adj;
1119fa0cbbf1SDavid Rientjes 	int err;
1120fa0cbbf1SDavid Rientjes 
1121fa0cbbf1SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1122fa0cbbf1SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1123fa0cbbf1SDavid Rientjes 		count = sizeof(buffer) - 1;
1124fa0cbbf1SDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1125fa0cbbf1SDavid Rientjes 		err = -EFAULT;
1126fa0cbbf1SDavid Rientjes 		goto out;
1127fa0cbbf1SDavid Rientjes 	}
1128fa0cbbf1SDavid Rientjes 
1129fa0cbbf1SDavid Rientjes 	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1130fa0cbbf1SDavid Rientjes 	if (err)
1131fa0cbbf1SDavid Rientjes 		goto out;
1132fa0cbbf1SDavid Rientjes 	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1133fa0cbbf1SDavid Rientjes 	     oom_adj != OOM_DISABLE) {
1134fa0cbbf1SDavid Rientjes 		err = -EINVAL;
1135fa0cbbf1SDavid Rientjes 		goto out;
1136fa0cbbf1SDavid Rientjes 	}
1137fa0cbbf1SDavid Rientjes 
1138fa0cbbf1SDavid Rientjes 	/*
1139fa0cbbf1SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1140fa0cbbf1SDavid Rientjes 	 * value is always attainable.
1141fa0cbbf1SDavid Rientjes 	 */
1142fa0cbbf1SDavid Rientjes 	if (oom_adj == OOM_ADJUST_MAX)
1143fa0cbbf1SDavid Rientjes 		oom_adj = OOM_SCORE_ADJ_MAX;
1144fa0cbbf1SDavid Rientjes 	else
1145fa0cbbf1SDavid Rientjes 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1146fa0cbbf1SDavid Rientjes 
11471d5f0acbSMichal Hocko 	err = __set_oom_adj(file, oom_adj, true);
1148fa0cbbf1SDavid Rientjes out:
1149fa0cbbf1SDavid Rientjes 	return err < 0 ? err : count;
1150fa0cbbf1SDavid Rientjes }
1151fa0cbbf1SDavid Rientjes 
1152fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = {
1153fa0cbbf1SDavid Rientjes 	.read		= oom_adj_read,
1154fa0cbbf1SDavid Rientjes 	.write		= oom_adj_write,
1155fa0cbbf1SDavid Rientjes 	.llseek		= generic_file_llseek,
1156fa0cbbf1SDavid Rientjes };
1157fa0cbbf1SDavid Rientjes 
1158a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1159a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1160a63d83f4SDavid Rientjes {
1161496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1162a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1163a9c58b90SDavid Rientjes 	short oom_score_adj = OOM_SCORE_ADJ_MIN;
1164a63d83f4SDavid Rientjes 	size_t len;
1165a63d83f4SDavid Rientjes 
1166a63d83f4SDavid Rientjes 	if (!task)
1167a63d83f4SDavid Rientjes 		return -ESRCH;
1168a63d83f4SDavid Rientjes 	oom_score_adj = task->signal->oom_score_adj;
1169a63d83f4SDavid Rientjes 	put_task_struct(task);
1170a9c58b90SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1171a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1172a63d83f4SDavid Rientjes }
1173a63d83f4SDavid Rientjes 
1174a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1175a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1176a63d83f4SDavid Rientjes {
1177a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
11780a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1179a63d83f4SDavid Rientjes 	int err;
1180a63d83f4SDavid Rientjes 
1181a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1182a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1183a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1184723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1185723548bfSDavid Rientjes 		err = -EFAULT;
1186723548bfSDavid Rientjes 		goto out;
1187723548bfSDavid Rientjes 	}
1188a63d83f4SDavid Rientjes 
11890a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1190a63d83f4SDavid Rientjes 	if (err)
1191723548bfSDavid Rientjes 		goto out;
1192a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1193723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1194723548bfSDavid Rientjes 		err = -EINVAL;
1195723548bfSDavid Rientjes 		goto out;
1196723548bfSDavid Rientjes 	}
1197a63d83f4SDavid Rientjes 
11981d5f0acbSMichal Hocko 	err = __set_oom_adj(file, oom_score_adj, false);
1199723548bfSDavid Rientjes out:
1200723548bfSDavid Rientjes 	return err < 0 ? err : count;
1201a63d83f4SDavid Rientjes }
1202a63d83f4SDavid Rientjes 
1203a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1204a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1205a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
12066038f373SArnd Bergmann 	.llseek		= default_llseek,
1207a63d83f4SDavid Rientjes };
1208a63d83f4SDavid Rientjes 
12094b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
1210b4eb4f7fSAlexey Dobriyan #define TMPBUFLEN 11
12111da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
12121da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
12131da177e4SLinus Torvalds {
1214496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
121599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
12161da177e4SLinus Torvalds 	ssize_t length;
12171da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
12181da177e4SLinus Torvalds 
121999f89551SEric W. Biederman 	if (!task)
122099f89551SEric W. Biederman 		return -ESRCH;
12211da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1222e1760bd5SEric W. Biederman 			   from_kuid(file->f_cred->user_ns,
1223e1760bd5SEric W. Biederman 				     audit_get_loginuid(task)));
122499f89551SEric W. Biederman 	put_task_struct(task);
12251da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
12291da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
12301da177e4SLinus Torvalds {
1231496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
12321da177e4SLinus Torvalds 	uid_t loginuid;
1233e1760bd5SEric W. Biederman 	kuid_t kloginuid;
1234774636e1SAlexey Dobriyan 	int rv;
12351da177e4SLinus Torvalds 
12367dc52157SPaul E. McKenney 	rcu_read_lock();
12377dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
12387dc52157SPaul E. McKenney 		rcu_read_unlock();
12391da177e4SLinus Torvalds 		return -EPERM;
12407dc52157SPaul E. McKenney 	}
12417dc52157SPaul E. McKenney 	rcu_read_unlock();
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	if (*ppos != 0) {
12441da177e4SLinus Torvalds 		/* No partial writes. */
12451da177e4SLinus Torvalds 		return -EINVAL;
12461da177e4SLinus Torvalds 	}
12471da177e4SLinus Torvalds 
1248774636e1SAlexey Dobriyan 	rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1249774636e1SAlexey Dobriyan 	if (rv < 0)
1250774636e1SAlexey Dobriyan 		return rv;
125181407c84SEric Paris 
125281407c84SEric Paris 	/* is userspace tring to explicitly UNSET the loginuid? */
125381407c84SEric Paris 	if (loginuid == AUDIT_UID_UNSET) {
125481407c84SEric Paris 		kloginuid = INVALID_UID;
125581407c84SEric Paris 	} else {
1256e1760bd5SEric W. Biederman 		kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1257774636e1SAlexey Dobriyan 		if (!uid_valid(kloginuid))
1258774636e1SAlexey Dobriyan 			return -EINVAL;
125981407c84SEric Paris 	}
1260e1760bd5SEric W. Biederman 
1261774636e1SAlexey Dobriyan 	rv = audit_set_loginuid(kloginuid);
1262774636e1SAlexey Dobriyan 	if (rv < 0)
1263774636e1SAlexey Dobriyan 		return rv;
1264774636e1SAlexey Dobriyan 	return count;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
126700977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
12681da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
12691da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
127087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12711da177e4SLinus Torvalds };
12721e0bd755SEric Paris 
12731e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
12741e0bd755SEric Paris 				  size_t count, loff_t *ppos)
12751e0bd755SEric Paris {
1276496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
12771e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
12781e0bd755SEric Paris 	ssize_t length;
12791e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
12801e0bd755SEric Paris 
12811e0bd755SEric Paris 	if (!task)
12821e0bd755SEric Paris 		return -ESRCH;
12831e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12841e0bd755SEric Paris 				audit_get_sessionid(task));
12851e0bd755SEric Paris 	put_task_struct(task);
12861e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12871e0bd755SEric Paris }
12881e0bd755SEric Paris 
12891e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
12901e0bd755SEric Paris 	.read		= proc_sessionid_read,
129187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12921e0bd755SEric Paris };
12931da177e4SLinus Torvalds #endif
12941da177e4SLinus Torvalds 
1295f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1296f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1297f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1298f4f154fdSAkinobu Mita {
1299496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1300f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1301f4f154fdSAkinobu Mita 	size_t len;
1302f4f154fdSAkinobu Mita 	int make_it_fail;
1303f4f154fdSAkinobu Mita 
1304f4f154fdSAkinobu Mita 	if (!task)
1305f4f154fdSAkinobu Mita 		return -ESRCH;
1306f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1307f4f154fdSAkinobu Mita 	put_task_struct(task);
1308f4f154fdSAkinobu Mita 
1309f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
13100c28f287SAkinobu Mita 
13110c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1312f4f154fdSAkinobu Mita }
1313f4f154fdSAkinobu Mita 
1314f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1315f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1316f4f154fdSAkinobu Mita {
1317f4f154fdSAkinobu Mita 	struct task_struct *task;
1318774636e1SAlexey Dobriyan 	char buffer[PROC_NUMBUF];
1319f4f154fdSAkinobu Mita 	int make_it_fail;
1320774636e1SAlexey Dobriyan 	int rv;
1321f4f154fdSAkinobu Mita 
1322f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1323f4f154fdSAkinobu Mita 		return -EPERM;
1324f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1325f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1326f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1327f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1328f4f154fdSAkinobu Mita 		return -EFAULT;
1329774636e1SAlexey Dobriyan 	rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1330774636e1SAlexey Dobriyan 	if (rv < 0)
1331774636e1SAlexey Dobriyan 		return rv;
133216caed31SDave Jones 	if (make_it_fail < 0 || make_it_fail > 1)
133316caed31SDave Jones 		return -EINVAL;
133416caed31SDave Jones 
1335496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1336f4f154fdSAkinobu Mita 	if (!task)
1337f4f154fdSAkinobu Mita 		return -ESRCH;
1338f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1339f4f154fdSAkinobu Mita 	put_task_struct(task);
1340cba8aafeSVincent Li 
1341cba8aafeSVincent Li 	return count;
1342f4f154fdSAkinobu Mita }
1343f4f154fdSAkinobu Mita 
134400977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1345f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1346f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
134787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1348f4f154fdSAkinobu Mita };
1349e41d5818SDmitry Vyukov 
1350e41d5818SDmitry Vyukov static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1351e41d5818SDmitry Vyukov 				   size_t count, loff_t *ppos)
1352e41d5818SDmitry Vyukov {
1353e41d5818SDmitry Vyukov 	struct task_struct *task;
13549049f2f6SAkinobu Mita 	int err;
13559049f2f6SAkinobu Mita 	unsigned int n;
1356e41d5818SDmitry Vyukov 
13579049f2f6SAkinobu Mita 	err = kstrtouint_from_user(buf, count, 0, &n);
1358e41d5818SDmitry Vyukov 	if (err)
1359e41d5818SDmitry Vyukov 		return err;
13601203c8e6SAkinobu Mita 
13611203c8e6SAkinobu Mita 	task = get_proc_task(file_inode(file));
13621203c8e6SAkinobu Mita 	if (!task)
13631203c8e6SAkinobu Mita 		return -ESRCH;
13649f7118b2SAlexey Dobriyan 	task->fail_nth = n;
13651203c8e6SAkinobu Mita 	put_task_struct(task);
13661203c8e6SAkinobu Mita 
1367e41d5818SDmitry Vyukov 	return count;
1368e41d5818SDmitry Vyukov }
1369e41d5818SDmitry Vyukov 
1370e41d5818SDmitry Vyukov static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1371e41d5818SDmitry Vyukov 				  size_t count, loff_t *ppos)
1372e41d5818SDmitry Vyukov {
1373e41d5818SDmitry Vyukov 	struct task_struct *task;
1374bfc74093SAkinobu Mita 	char numbuf[PROC_NUMBUF];
1375bfc74093SAkinobu Mita 	ssize_t len;
1376e41d5818SDmitry Vyukov 
1377e41d5818SDmitry Vyukov 	task = get_proc_task(file_inode(file));
1378e41d5818SDmitry Vyukov 	if (!task)
1379e41d5818SDmitry Vyukov 		return -ESRCH;
13809f7118b2SAlexey Dobriyan 	len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
13811203c8e6SAkinobu Mita 	put_task_struct(task);
1382a44937feSAlexey Dobriyan 	return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1383e41d5818SDmitry Vyukov }
1384e41d5818SDmitry Vyukov 
1385e41d5818SDmitry Vyukov static const struct file_operations proc_fail_nth_operations = {
1386e41d5818SDmitry Vyukov 	.read		= proc_fail_nth_read,
1387e41d5818SDmitry Vyukov 	.write		= proc_fail_nth_write,
1388e41d5818SDmitry Vyukov };
1389f4f154fdSAkinobu Mita #endif
1390f4f154fdSAkinobu Mita 
13919745512cSArjan van de Ven 
139243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
139343ae34cbSIngo Molnar /*
139443ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
139543ae34cbSIngo Molnar  */
139643ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
139743ae34cbSIngo Molnar {
139843ae34cbSIngo Molnar 	struct inode *inode = m->private;
139976f668beSChristoph Hellwig 	struct pid_namespace *ns = proc_pid_ns(inode);
140043ae34cbSIngo Molnar 	struct task_struct *p;
140143ae34cbSIngo Molnar 
140243ae34cbSIngo Molnar 	p = get_proc_task(inode);
140343ae34cbSIngo Molnar 	if (!p)
140443ae34cbSIngo Molnar 		return -ESRCH;
140574dc3384SAleksa Sarai 	proc_sched_show_task(p, ns, m);
140643ae34cbSIngo Molnar 
140743ae34cbSIngo Molnar 	put_task_struct(p);
140843ae34cbSIngo Molnar 
140943ae34cbSIngo Molnar 	return 0;
141043ae34cbSIngo Molnar }
141143ae34cbSIngo Molnar 
141243ae34cbSIngo Molnar static ssize_t
141343ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
141443ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
141543ae34cbSIngo Molnar {
1416496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
141743ae34cbSIngo Molnar 	struct task_struct *p;
141843ae34cbSIngo Molnar 
141943ae34cbSIngo Molnar 	p = get_proc_task(inode);
142043ae34cbSIngo Molnar 	if (!p)
142143ae34cbSIngo Molnar 		return -ESRCH;
142243ae34cbSIngo Molnar 	proc_sched_set_task(p);
142343ae34cbSIngo Molnar 
142443ae34cbSIngo Molnar 	put_task_struct(p);
142543ae34cbSIngo Molnar 
142643ae34cbSIngo Molnar 	return count;
142743ae34cbSIngo Molnar }
142843ae34cbSIngo Molnar 
142943ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
143043ae34cbSIngo Molnar {
1431c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
143243ae34cbSIngo Molnar }
143343ae34cbSIngo Molnar 
143443ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
143543ae34cbSIngo Molnar 	.open		= sched_open,
143643ae34cbSIngo Molnar 	.read		= seq_read,
143743ae34cbSIngo Molnar 	.write		= sched_write,
143843ae34cbSIngo Molnar 	.llseek		= seq_lseek,
14395ea473a1SAlexey Dobriyan 	.release	= single_release,
144043ae34cbSIngo Molnar };
144143ae34cbSIngo Molnar 
144243ae34cbSIngo Molnar #endif
144343ae34cbSIngo Molnar 
14445091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
14455091faa4SMike Galbraith /*
14465091faa4SMike Galbraith  * Print out autogroup related information:
14475091faa4SMike Galbraith  */
14485091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
14495091faa4SMike Galbraith {
14505091faa4SMike Galbraith 	struct inode *inode = m->private;
14515091faa4SMike Galbraith 	struct task_struct *p;
14525091faa4SMike Galbraith 
14535091faa4SMike Galbraith 	p = get_proc_task(inode);
14545091faa4SMike Galbraith 	if (!p)
14555091faa4SMike Galbraith 		return -ESRCH;
14565091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
14575091faa4SMike Galbraith 
14585091faa4SMike Galbraith 	put_task_struct(p);
14595091faa4SMike Galbraith 
14605091faa4SMike Galbraith 	return 0;
14615091faa4SMike Galbraith }
14625091faa4SMike Galbraith 
14635091faa4SMike Galbraith static ssize_t
14645091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
14655091faa4SMike Galbraith 	    size_t count, loff_t *offset)
14665091faa4SMike Galbraith {
1467496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
14685091faa4SMike Galbraith 	struct task_struct *p;
14695091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
14700a8cb8e3SAlexey Dobriyan 	int nice;
14715091faa4SMike Galbraith 	int err;
14725091faa4SMike Galbraith 
14735091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
14745091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
14755091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
14765091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
14775091faa4SMike Galbraith 		return -EFAULT;
14785091faa4SMike Galbraith 
14790a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
14800a8cb8e3SAlexey Dobriyan 	if (err < 0)
14810a8cb8e3SAlexey Dobriyan 		return err;
14825091faa4SMike Galbraith 
14835091faa4SMike Galbraith 	p = get_proc_task(inode);
14845091faa4SMike Galbraith 	if (!p)
14855091faa4SMike Galbraith 		return -ESRCH;
14865091faa4SMike Galbraith 
14872e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
14885091faa4SMike Galbraith 	if (err)
14895091faa4SMike Galbraith 		count = err;
14905091faa4SMike Galbraith 
14915091faa4SMike Galbraith 	put_task_struct(p);
14925091faa4SMike Galbraith 
14935091faa4SMike Galbraith 	return count;
14945091faa4SMike Galbraith }
14955091faa4SMike Galbraith 
14965091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
14975091faa4SMike Galbraith {
14985091faa4SMike Galbraith 	int ret;
14995091faa4SMike Galbraith 
15005091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
15015091faa4SMike Galbraith 	if (!ret) {
15025091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
15035091faa4SMike Galbraith 
15045091faa4SMike Galbraith 		m->private = inode;
15055091faa4SMike Galbraith 	}
15065091faa4SMike Galbraith 	return ret;
15075091faa4SMike Galbraith }
15085091faa4SMike Galbraith 
15095091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
15105091faa4SMike Galbraith 	.open		= sched_autogroup_open,
15115091faa4SMike Galbraith 	.read		= seq_read,
15125091faa4SMike Galbraith 	.write		= sched_autogroup_write,
15135091faa4SMike Galbraith 	.llseek		= seq_lseek,
15145091faa4SMike Galbraith 	.release	= single_release,
15155091faa4SMike Galbraith };
15165091faa4SMike Galbraith 
15175091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
15185091faa4SMike Galbraith 
15194614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
15204614a696Sjohn stultz 				size_t count, loff_t *offset)
15214614a696Sjohn stultz {
1522496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
15234614a696Sjohn stultz 	struct task_struct *p;
15244614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
1525830e0fc9SDavid Rientjes 	const size_t maxlen = sizeof(buffer) - 1;
15264614a696Sjohn stultz 
15274614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
1528830e0fc9SDavid Rientjes 	if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
15294614a696Sjohn stultz 		return -EFAULT;
15304614a696Sjohn stultz 
15314614a696Sjohn stultz 	p = get_proc_task(inode);
15324614a696Sjohn stultz 	if (!p)
15334614a696Sjohn stultz 		return -ESRCH;
15344614a696Sjohn stultz 
15354614a696Sjohn stultz 	if (same_thread_group(current, p))
15364614a696Sjohn stultz 		set_task_comm(p, buffer);
15374614a696Sjohn stultz 	else
15384614a696Sjohn stultz 		count = -EINVAL;
15394614a696Sjohn stultz 
15404614a696Sjohn stultz 	put_task_struct(p);
15414614a696Sjohn stultz 
15424614a696Sjohn stultz 	return count;
15434614a696Sjohn stultz }
15444614a696Sjohn stultz 
15454614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
15464614a696Sjohn stultz {
15474614a696Sjohn stultz 	struct inode *inode = m->private;
15484614a696Sjohn stultz 	struct task_struct *p;
15494614a696Sjohn stultz 
15504614a696Sjohn stultz 	p = get_proc_task(inode);
15514614a696Sjohn stultz 	if (!p)
15524614a696Sjohn stultz 		return -ESRCH;
15534614a696Sjohn stultz 
155488b72b31STejun Heo 	proc_task_name(m, p, false);
155588b72b31STejun Heo 	seq_putc(m, '\n');
15564614a696Sjohn stultz 
15574614a696Sjohn stultz 	put_task_struct(p);
15584614a696Sjohn stultz 
15594614a696Sjohn stultz 	return 0;
15604614a696Sjohn stultz }
15614614a696Sjohn stultz 
15624614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
15634614a696Sjohn stultz {
1564c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
15654614a696Sjohn stultz }
15664614a696Sjohn stultz 
15674614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
15684614a696Sjohn stultz 	.open		= comm_open,
15694614a696Sjohn stultz 	.read		= seq_read,
15704614a696Sjohn stultz 	.write		= comm_write,
15714614a696Sjohn stultz 	.llseek		= seq_lseek,
15724614a696Sjohn stultz 	.release	= single_release,
15734614a696Sjohn stultz };
15744614a696Sjohn stultz 
15757773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1576925d1c40SMatt Helsley {
1577925d1c40SMatt Helsley 	struct task_struct *task;
1578925d1c40SMatt Helsley 	struct file *exe_file;
1579925d1c40SMatt Helsley 
15802b0143b5SDavid Howells 	task = get_proc_task(d_inode(dentry));
1581925d1c40SMatt Helsley 	if (!task)
1582925d1c40SMatt Helsley 		return -ENOENT;
1583cd81a917SMateusz Guzik 	exe_file = get_task_exe_file(task);
1584925d1c40SMatt Helsley 	put_task_struct(task);
1585925d1c40SMatt Helsley 	if (exe_file) {
1586925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1587925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1588925d1c40SMatt Helsley 		fput(exe_file);
1589925d1c40SMatt Helsley 		return 0;
1590925d1c40SMatt Helsley 	} else
1591925d1c40SMatt Helsley 		return -ENOENT;
1592925d1c40SMatt Helsley }
1593925d1c40SMatt Helsley 
15946b255391SAl Viro static const char *proc_pid_get_link(struct dentry *dentry,
1595fceef393SAl Viro 				     struct inode *inode,
1596fceef393SAl Viro 				     struct delayed_call *done)
15971da177e4SLinus Torvalds {
1598408ef013SChristoph Hellwig 	struct path path;
15991da177e4SLinus Torvalds 	int error = -EACCES;
16001da177e4SLinus Torvalds 
16016b255391SAl Viro 	if (!dentry)
16026b255391SAl Viro 		return ERR_PTR(-ECHILD);
16036b255391SAl Viro 
1604778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1605778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
16061da177e4SLinus Torvalds 		goto out;
16071da177e4SLinus Torvalds 
1608408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1609408ef013SChristoph Hellwig 	if (error)
1610408ef013SChristoph Hellwig 		goto out;
1611408ef013SChristoph Hellwig 
16126e77137bSAl Viro 	nd_jump_link(&path);
1613408ef013SChristoph Hellwig 	return NULL;
16141da177e4SLinus Torvalds out:
1615008b150aSAl Viro 	return ERR_PTR(error);
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
16183dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
16191da177e4SLinus Torvalds {
16200ee931c4SMichal Hocko 	char *tmp = (char *)__get_free_page(GFP_KERNEL);
16213dcd25f3SJan Blunck 	char *pathname;
16221da177e4SLinus Torvalds 	int len;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	if (!tmp)
16251da177e4SLinus Torvalds 		return -ENOMEM;
16261da177e4SLinus Torvalds 
16277b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
16283dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
16293dcd25f3SJan Blunck 	if (IS_ERR(pathname))
16301da177e4SLinus Torvalds 		goto out;
16313dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	if (len > buflen)
16341da177e4SLinus Torvalds 		len = buflen;
16353dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
16361da177e4SLinus Torvalds 		len = -EFAULT;
16371da177e4SLinus Torvalds  out:
16381da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
16391da177e4SLinus Torvalds 	return len;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	int error = -EACCES;
16452b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
16463dcd25f3SJan Blunck 	struct path path;
16471da177e4SLinus Torvalds 
1648778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1649778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
16501da177e4SLinus Torvalds 		goto out;
16511da177e4SLinus Torvalds 
16527773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
16531da177e4SLinus Torvalds 	if (error)
16541da177e4SLinus Torvalds 		goto out;
16551da177e4SLinus Torvalds 
16563dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
16573dcd25f3SJan Blunck 	path_put(&path);
16581da177e4SLinus Torvalds out:
16591da177e4SLinus Torvalds 	return error;
16601da177e4SLinus Torvalds }
16611da177e4SLinus Torvalds 
1662faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = {
16631da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
16646b255391SAl Viro 	.get_link	= proc_pid_get_link,
16656d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
16661da177e4SLinus Torvalds };
16671da177e4SLinus Torvalds 
166828a6d671SEric W. Biederman 
166928a6d671SEric W. Biederman /* building an inode */
167028a6d671SEric W. Biederman 
1671c6eb50d2SAl Viro void task_dump_owner(struct task_struct *task, umode_t mode,
167268eb94f1SEric W. Biederman 		     kuid_t *ruid, kgid_t *rgid)
167368eb94f1SEric W. Biederman {
167468eb94f1SEric W. Biederman 	/* Depending on the state of dumpable compute who should own a
167568eb94f1SEric W. Biederman 	 * proc file for a task.
167668eb94f1SEric W. Biederman 	 */
167768eb94f1SEric W. Biederman 	const struct cred *cred;
167868eb94f1SEric W. Biederman 	kuid_t uid;
167968eb94f1SEric W. Biederman 	kgid_t gid;
168068eb94f1SEric W. Biederman 
16812e0ad552SAlexey Dobriyan 	if (unlikely(task->flags & PF_KTHREAD)) {
16822e0ad552SAlexey Dobriyan 		*ruid = GLOBAL_ROOT_UID;
16832e0ad552SAlexey Dobriyan 		*rgid = GLOBAL_ROOT_GID;
16842e0ad552SAlexey Dobriyan 		return;
16852e0ad552SAlexey Dobriyan 	}
16862e0ad552SAlexey Dobriyan 
168768eb94f1SEric W. Biederman 	/* Default to the tasks effective ownership */
168868eb94f1SEric W. Biederman 	rcu_read_lock();
168968eb94f1SEric W. Biederman 	cred = __task_cred(task);
169068eb94f1SEric W. Biederman 	uid = cred->euid;
169168eb94f1SEric W. Biederman 	gid = cred->egid;
169268eb94f1SEric W. Biederman 	rcu_read_unlock();
169368eb94f1SEric W. Biederman 
169468eb94f1SEric W. Biederman 	/*
169568eb94f1SEric W. Biederman 	 * Before the /proc/pid/status file was created the only way to read
169668eb94f1SEric W. Biederman 	 * the effective uid of a /process was to stat /proc/pid.  Reading
169768eb94f1SEric W. Biederman 	 * /proc/pid/status is slow enough that procps and other packages
169868eb94f1SEric W. Biederman 	 * kept stating /proc/pid.  To keep the rules in /proc simple I have
169968eb94f1SEric W. Biederman 	 * made this apply to all per process world readable and executable
170068eb94f1SEric W. Biederman 	 * directories.
170168eb94f1SEric W. Biederman 	 */
170268eb94f1SEric W. Biederman 	if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
170368eb94f1SEric W. Biederman 		struct mm_struct *mm;
170468eb94f1SEric W. Biederman 		task_lock(task);
170568eb94f1SEric W. Biederman 		mm = task->mm;
170668eb94f1SEric W. Biederman 		/* Make non-dumpable tasks owned by some root */
170768eb94f1SEric W. Biederman 		if (mm) {
170868eb94f1SEric W. Biederman 			if (get_dumpable(mm) != SUID_DUMP_USER) {
170968eb94f1SEric W. Biederman 				struct user_namespace *user_ns = mm->user_ns;
171068eb94f1SEric W. Biederman 
171168eb94f1SEric W. Biederman 				uid = make_kuid(user_ns, 0);
171268eb94f1SEric W. Biederman 				if (!uid_valid(uid))
171368eb94f1SEric W. Biederman 					uid = GLOBAL_ROOT_UID;
171468eb94f1SEric W. Biederman 
171568eb94f1SEric W. Biederman 				gid = make_kgid(user_ns, 0);
171668eb94f1SEric W. Biederman 				if (!gid_valid(gid))
171768eb94f1SEric W. Biederman 					gid = GLOBAL_ROOT_GID;
171868eb94f1SEric W. Biederman 			}
171968eb94f1SEric W. Biederman 		} else {
172068eb94f1SEric W. Biederman 			uid = GLOBAL_ROOT_UID;
172168eb94f1SEric W. Biederman 			gid = GLOBAL_ROOT_GID;
172268eb94f1SEric W. Biederman 		}
172368eb94f1SEric W. Biederman 		task_unlock(task);
172468eb94f1SEric W. Biederman 	}
172568eb94f1SEric W. Biederman 	*ruid = uid;
172668eb94f1SEric W. Biederman 	*rgid = gid;
172768eb94f1SEric W. Biederman }
172868eb94f1SEric W. Biederman 
1729db978da8SAndreas Gruenbacher struct inode *proc_pid_make_inode(struct super_block * sb,
1730db978da8SAndreas Gruenbacher 				  struct task_struct *task, umode_t mode)
173128a6d671SEric W. Biederman {
173228a6d671SEric W. Biederman 	struct inode * inode;
173328a6d671SEric W. Biederman 	struct proc_inode *ei;
173428a6d671SEric W. Biederman 
173528a6d671SEric W. Biederman 	/* We need a new inode */
173628a6d671SEric W. Biederman 
173728a6d671SEric W. Biederman 	inode = new_inode(sb);
173828a6d671SEric W. Biederman 	if (!inode)
173928a6d671SEric W. Biederman 		goto out;
174028a6d671SEric W. Biederman 
174128a6d671SEric W. Biederman 	/* Common stuff */
174228a6d671SEric W. Biederman 	ei = PROC_I(inode);
1743db978da8SAndreas Gruenbacher 	inode->i_mode = mode;
174485fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
1745078cd827SDeepa Dinamani 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
174628a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
174728a6d671SEric W. Biederman 
174828a6d671SEric W. Biederman 	/*
174928a6d671SEric W. Biederman 	 * grab the reference to task.
175028a6d671SEric W. Biederman 	 */
17511a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
175228a6d671SEric W. Biederman 	if (!ei->pid)
175328a6d671SEric W. Biederman 		goto out_unlock;
175428a6d671SEric W. Biederman 
175568eb94f1SEric W. Biederman 	task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
175628a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
175728a6d671SEric W. Biederman 
175828a6d671SEric W. Biederman out:
175928a6d671SEric W. Biederman 	return inode;
176028a6d671SEric W. Biederman 
176128a6d671SEric W. Biederman out_unlock:
176228a6d671SEric W. Biederman 	iput(inode);
176328a6d671SEric W. Biederman 	return NULL;
176428a6d671SEric W. Biederman }
176528a6d671SEric W. Biederman 
1766a528d35eSDavid Howells int pid_getattr(const struct path *path, struct kstat *stat,
1767a528d35eSDavid Howells 		u32 request_mask, unsigned int query_flags)
176828a6d671SEric W. Biederman {
1769a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
177076f668beSChristoph Hellwig 	struct pid_namespace *pid = proc_pid_ns(inode);
177128a6d671SEric W. Biederman 	struct task_struct *task;
1772c69e8d9cSDavid Howells 
177328a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
177428a6d671SEric W. Biederman 
1775dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1776dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
177794116929SAlexey Dobriyan 	rcu_read_lock();
177828a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
177928a6d671SEric W. Biederman 	if (task) {
1780796f571bSLafcadio Wluiki 		if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
17810499680aSVasiliy Kulikov 			rcu_read_unlock();
17820499680aSVasiliy Kulikov 			/*
17830499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
17840499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
17850499680aSVasiliy Kulikov 			 */
17860499680aSVasiliy Kulikov 			return -ENOENT;
17870499680aSVasiliy Kulikov 		}
178868eb94f1SEric W. Biederman 		task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
178928a6d671SEric W. Biederman 	}
179028a6d671SEric W. Biederman 	rcu_read_unlock();
179128a6d671SEric W. Biederman 	return 0;
179228a6d671SEric W. Biederman }
179328a6d671SEric W. Biederman 
179428a6d671SEric W. Biederman /* dentry stuff */
179528a6d671SEric W. Biederman 
179628a6d671SEric W. Biederman /*
17971bbc5513SAl Viro  * Set <pid>/... inode ownership (can change due to setuid(), etc.)
17981bbc5513SAl Viro  */
17991bbc5513SAl Viro void pid_update_inode(struct task_struct *task, struct inode *inode)
18001bbc5513SAl Viro {
18011bbc5513SAl Viro 	task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
18021bbc5513SAl Viro 
18031bbc5513SAl Viro 	inode->i_mode &= ~(S_ISUID | S_ISGID);
18041bbc5513SAl Viro 	security_task_to_inode(task, inode);
18051bbc5513SAl Viro }
18061bbc5513SAl Viro 
18071bbc5513SAl Viro /*
180828a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
180928a6d671SEric W. Biederman  * performed a setuid(), etc.
181028a6d671SEric W. Biederman  *
181128a6d671SEric W. Biederman  */
18121bbc5513SAl Viro static int pid_revalidate(struct dentry *dentry, unsigned int flags)
181328a6d671SEric W. Biederman {
181434286d66SNick Piggin 	struct inode *inode;
181534286d66SNick Piggin 	struct task_struct *task;
1816c69e8d9cSDavid Howells 
18170b728e19SAl Viro 	if (flags & LOOKUP_RCU)
181834286d66SNick Piggin 		return -ECHILD;
181934286d66SNick Piggin 
18202b0143b5SDavid Howells 	inode = d_inode(dentry);
182134286d66SNick Piggin 	task = get_proc_task(inode);
182234286d66SNick Piggin 
182328a6d671SEric W. Biederman 	if (task) {
18241bbc5513SAl Viro 		pid_update_inode(task, inode);
182528a6d671SEric W. Biederman 		put_task_struct(task);
182628a6d671SEric W. Biederman 		return 1;
182728a6d671SEric W. Biederman 	}
182828a6d671SEric W. Biederman 	return 0;
182928a6d671SEric W. Biederman }
183028a6d671SEric W. Biederman 
1831d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode)
1832d855a4b7SOleg Nesterov {
1833d855a4b7SOleg Nesterov 	return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1834d855a4b7SOleg Nesterov }
1835d855a4b7SOleg Nesterov 
18361dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry)
18371dd704b6SDavid Howells {
18381dd704b6SDavid Howells 	/* Is the task we represent dead?
18391dd704b6SDavid Howells 	 * If so, then don't put the dentry on the lru list,
18401dd704b6SDavid Howells 	 * kill it immediately.
18411dd704b6SDavid Howells 	 */
18422b0143b5SDavid Howells 	return proc_inode_is_dead(d_inode(dentry));
18431dd704b6SDavid Howells }
18441dd704b6SDavid Howells 
18456b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
184628a6d671SEric W. Biederman {
184728a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
184828a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
184928a6d671SEric W. Biederman };
185028a6d671SEric W. Biederman 
185128a6d671SEric W. Biederman /* Lookups */
185228a6d671SEric W. Biederman 
18531c0d04c9SEric W. Biederman /*
18541c0d04c9SEric W. Biederman  * Fill a directory entry.
18551c0d04c9SEric W. Biederman  *
18561c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
18571c0d04c9SEric W. Biederman  * file type from dcache entry.
18581c0d04c9SEric W. Biederman  *
18591c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
18601c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
18611c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
18621c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
18631c0d04c9SEric W. Biederman  * by stat.
18641c0d04c9SEric W. Biederman  */
1865f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1866a4ef3895SAlexey Dobriyan 	const char *name, unsigned int len,
1867c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
186861a28784SEric W. Biederman {
1869f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
18701df98b8bSAl Viro 	struct qstr qname = QSTR_INIT(name, len);
187161a28784SEric W. Biederman 	struct inode *inode;
18720168b9e3SAl Viro 	unsigned type = DT_UNKNOWN;
18730168b9e3SAl Viro 	ino_t ino = 1;
187461a28784SEric W. Biederman 
18751df98b8bSAl Viro 	child = d_hash_and_lookup(dir, &qname);
187661a28784SEric W. Biederman 	if (!child) {
18773781764bSAl Viro 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
18783781764bSAl Viro 		child = d_alloc_parallel(dir, &qname, &wq);
18793781764bSAl Viro 		if (IS_ERR(child))
188061a28784SEric W. Biederman 			goto end_instantiate;
18813781764bSAl Viro 		if (d_in_lookup(child)) {
18820168b9e3SAl Viro 			struct dentry *res;
18830168b9e3SAl Viro 			res = instantiate(child, task, ptr);
18843781764bSAl Viro 			d_lookup_done(child);
18850168b9e3SAl Viro 			if (unlikely(res)) {
18860168b9e3SAl Viro 				dput(child);
18870168b9e3SAl Viro 				child = res;
1888d85b399bSAl Viro 				if (IS_ERR(child))
1889d85b399bSAl Viro 					goto end_instantiate;
18901df98b8bSAl Viro 			}
18911df98b8bSAl Viro 		}
18923781764bSAl Viro 	}
18932b0143b5SDavid Howells 	inode = d_inode(child);
189461a28784SEric W. Biederman 	ino = inode->i_ino;
189561a28784SEric W. Biederman 	type = inode->i_mode >> 12;
189661a28784SEric W. Biederman 	dput(child);
1897d85b399bSAl Viro end_instantiate:
1898f0c3b509SAl Viro 	return dir_emit(ctx, name, len, ino, type);
189961a28784SEric W. Biederman }
190061a28784SEric W. Biederman 
1901640708a2SPavel Emelyanov /*
1902640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1903640708a2SPavel Emelyanov  * which represent vma start and end addresses.
1904640708a2SPavel Emelyanov  */
1905640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
1906640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
1907640708a2SPavel Emelyanov {
1908ac7f1061SAlexey Dobriyan 	const char *str = dentry->d_name.name;
1909ac7f1061SAlexey Dobriyan 	unsigned long long sval, eval;
1910ac7f1061SAlexey Dobriyan 	unsigned int len;
1911ac7f1061SAlexey Dobriyan 
191235318db5SAlexey Dobriyan 	if (str[0] == '0' && str[1] != '-')
191335318db5SAlexey Dobriyan 		return -EINVAL;
1914ac7f1061SAlexey Dobriyan 	len = _parse_integer(str, 16, &sval);
1915ac7f1061SAlexey Dobriyan 	if (len & KSTRTOX_OVERFLOW)
1916640708a2SPavel Emelyanov 		return -EINVAL;
1917ac7f1061SAlexey Dobriyan 	if (sval != (unsigned long)sval)
1918ac7f1061SAlexey Dobriyan 		return -EINVAL;
1919ac7f1061SAlexey Dobriyan 	str += len;
1920ac7f1061SAlexey Dobriyan 
1921ac7f1061SAlexey Dobriyan 	if (*str != '-')
1922ac7f1061SAlexey Dobriyan 		return -EINVAL;
1923ac7f1061SAlexey Dobriyan 	str++;
1924ac7f1061SAlexey Dobriyan 
192535318db5SAlexey Dobriyan 	if (str[0] == '0' && str[1])
192635318db5SAlexey Dobriyan 		return -EINVAL;
1927ac7f1061SAlexey Dobriyan 	len = _parse_integer(str, 16, &eval);
1928ac7f1061SAlexey Dobriyan 	if (len & KSTRTOX_OVERFLOW)
1929ac7f1061SAlexey Dobriyan 		return -EINVAL;
1930ac7f1061SAlexey Dobriyan 	if (eval != (unsigned long)eval)
1931ac7f1061SAlexey Dobriyan 		return -EINVAL;
1932ac7f1061SAlexey Dobriyan 	str += len;
1933ac7f1061SAlexey Dobriyan 
1934ac7f1061SAlexey Dobriyan 	if (*str != '\0')
1935ac7f1061SAlexey Dobriyan 		return -EINVAL;
1936ac7f1061SAlexey Dobriyan 
1937ac7f1061SAlexey Dobriyan 	*start = sval;
1938ac7f1061SAlexey Dobriyan 	*end = eval;
1939640708a2SPavel Emelyanov 
1940640708a2SPavel Emelyanov 	return 0;
1941640708a2SPavel Emelyanov }
1942640708a2SPavel Emelyanov 
19430b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1944640708a2SPavel Emelyanov {
1945640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1946640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
1947640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
1948640708a2SPavel Emelyanov 	struct task_struct *task;
1949640708a2SPavel Emelyanov 	struct inode *inode;
1950640708a2SPavel Emelyanov 	int status = 0;
1951640708a2SPavel Emelyanov 
19520b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1953640708a2SPavel Emelyanov 		return -ECHILD;
1954640708a2SPavel Emelyanov 
19552b0143b5SDavid Howells 	inode = d_inode(dentry);
1956640708a2SPavel Emelyanov 	task = get_proc_task(inode);
1957640708a2SPavel Emelyanov 	if (!task)
1958640708a2SPavel Emelyanov 		goto out_notask;
1959640708a2SPavel Emelyanov 
1960caaee623SJann Horn 	mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
19612344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
1962640708a2SPavel Emelyanov 		goto out;
1963640708a2SPavel Emelyanov 
1964640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1965640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
1966640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1967640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
1968640708a2SPavel Emelyanov 	}
1969640708a2SPavel Emelyanov 
1970640708a2SPavel Emelyanov 	mmput(mm);
1971640708a2SPavel Emelyanov 
1972640708a2SPavel Emelyanov 	if (exact_vma_exists) {
197368eb94f1SEric W. Biederman 		task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
197468eb94f1SEric W. Biederman 
1975640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
1976640708a2SPavel Emelyanov 		status = 1;
1977640708a2SPavel Emelyanov 	}
1978640708a2SPavel Emelyanov 
1979640708a2SPavel Emelyanov out:
1980640708a2SPavel Emelyanov 	put_task_struct(task);
1981640708a2SPavel Emelyanov 
1982640708a2SPavel Emelyanov out_notask:
1983640708a2SPavel Emelyanov 	return status;
1984640708a2SPavel Emelyanov }
1985640708a2SPavel Emelyanov 
1986640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
1987640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
1988640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
1989640708a2SPavel Emelyanov };
1990640708a2SPavel Emelyanov 
19916b255391SAl Viro static int map_files_get_link(struct dentry *dentry, struct path *path)
1992640708a2SPavel Emelyanov {
1993640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1994640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1995640708a2SPavel Emelyanov 	struct task_struct *task;
1996640708a2SPavel Emelyanov 	struct mm_struct *mm;
1997640708a2SPavel Emelyanov 	int rc;
1998640708a2SPavel Emelyanov 
1999640708a2SPavel Emelyanov 	rc = -ENOENT;
20002b0143b5SDavid Howells 	task = get_proc_task(d_inode(dentry));
2001640708a2SPavel Emelyanov 	if (!task)
2002640708a2SPavel Emelyanov 		goto out;
2003640708a2SPavel Emelyanov 
2004640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2005640708a2SPavel Emelyanov 	put_task_struct(task);
2006640708a2SPavel Emelyanov 	if (!mm)
2007640708a2SPavel Emelyanov 		goto out;
2008640708a2SPavel Emelyanov 
2009640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2010640708a2SPavel Emelyanov 	if (rc)
2011640708a2SPavel Emelyanov 		goto out_mmput;
2012640708a2SPavel Emelyanov 
201370335abbSArtem Fetishev 	rc = -ENOENT;
2014640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2015640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2016640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
2017640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
2018640708a2SPavel Emelyanov 		path_get(path);
2019640708a2SPavel Emelyanov 		rc = 0;
2020640708a2SPavel Emelyanov 	}
2021640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2022640708a2SPavel Emelyanov 
2023640708a2SPavel Emelyanov out_mmput:
2024640708a2SPavel Emelyanov 	mmput(mm);
2025640708a2SPavel Emelyanov out:
2026640708a2SPavel Emelyanov 	return rc;
2027640708a2SPavel Emelyanov }
2028640708a2SPavel Emelyanov 
2029640708a2SPavel Emelyanov struct map_files_info {
203020d28cdeSAlexey Dobriyan 	unsigned long	start;
203120d28cdeSAlexey Dobriyan 	unsigned long	end;
20327b540d06SAl Viro 	fmode_t		mode;
2033640708a2SPavel Emelyanov };
2034640708a2SPavel Emelyanov 
2035bdb4d100SCalvin Owens /*
2036bdb4d100SCalvin Owens  * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2037bdb4d100SCalvin Owens  * symlinks may be used to bypass permissions on ancestor directories in the
2038bdb4d100SCalvin Owens  * path to the file in question.
2039bdb4d100SCalvin Owens  */
2040bdb4d100SCalvin Owens static const char *
20416b255391SAl Viro proc_map_files_get_link(struct dentry *dentry,
2042fceef393SAl Viro 			struct inode *inode,
2043fceef393SAl Viro 		        struct delayed_call *done)
2044bdb4d100SCalvin Owens {
2045bdb4d100SCalvin Owens 	if (!capable(CAP_SYS_ADMIN))
2046bdb4d100SCalvin Owens 		return ERR_PTR(-EPERM);
2047bdb4d100SCalvin Owens 
2048fceef393SAl Viro 	return proc_pid_get_link(dentry, inode, done);
2049bdb4d100SCalvin Owens }
2050bdb4d100SCalvin Owens 
2051bdb4d100SCalvin Owens /*
20526b255391SAl Viro  * Identical to proc_pid_link_inode_operations except for get_link()
2053bdb4d100SCalvin Owens  */
2054bdb4d100SCalvin Owens static const struct inode_operations proc_map_files_link_inode_operations = {
2055bdb4d100SCalvin Owens 	.readlink	= proc_pid_readlink,
20566b255391SAl Viro 	.get_link	= proc_map_files_get_link,
2057bdb4d100SCalvin Owens 	.setattr	= proc_setattr,
2058bdb4d100SCalvin Owens };
2059bdb4d100SCalvin Owens 
20600168b9e3SAl Viro static struct dentry *
20610168b9e3SAl Viro proc_map_files_instantiate(struct dentry *dentry,
2062640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
2063640708a2SPavel Emelyanov {
20647b540d06SAl Viro 	fmode_t mode = (fmode_t)(unsigned long)ptr;
2065640708a2SPavel Emelyanov 	struct proc_inode *ei;
2066640708a2SPavel Emelyanov 	struct inode *inode;
2067640708a2SPavel Emelyanov 
20680168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2069db978da8SAndreas Gruenbacher 				    ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2070db978da8SAndreas Gruenbacher 				    ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2071640708a2SPavel Emelyanov 	if (!inode)
20720168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
2073640708a2SPavel Emelyanov 
2074640708a2SPavel Emelyanov 	ei = PROC_I(inode);
20756b255391SAl Viro 	ei->op.proc_get_link = map_files_get_link;
2076640708a2SPavel Emelyanov 
2077bdb4d100SCalvin Owens 	inode->i_op = &proc_map_files_link_inode_operations;
2078640708a2SPavel Emelyanov 	inode->i_size = 64;
2079640708a2SPavel Emelyanov 
2080640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
20810168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
2082640708a2SPavel Emelyanov }
2083640708a2SPavel Emelyanov 
2084640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
208500cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
2086640708a2SPavel Emelyanov {
2087640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2088640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2089640708a2SPavel Emelyanov 	struct task_struct *task;
20900168b9e3SAl Viro 	struct dentry *result;
2091640708a2SPavel Emelyanov 	struct mm_struct *mm;
2092640708a2SPavel Emelyanov 
20930168b9e3SAl Viro 	result = ERR_PTR(-ENOENT);
2094640708a2SPavel Emelyanov 	task = get_proc_task(dir);
2095640708a2SPavel Emelyanov 	if (!task)
2096640708a2SPavel Emelyanov 		goto out;
2097640708a2SPavel Emelyanov 
20980168b9e3SAl Viro 	result = ERR_PTR(-EACCES);
2099caaee623SJann Horn 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2100640708a2SPavel Emelyanov 		goto out_put_task;
2101640708a2SPavel Emelyanov 
21020168b9e3SAl Viro 	result = ERR_PTR(-ENOENT);
2103640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2104eb94cd96SCyrill Gorcunov 		goto out_put_task;
2105640708a2SPavel Emelyanov 
2106640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2107640708a2SPavel Emelyanov 	if (!mm)
2108eb94cd96SCyrill Gorcunov 		goto out_put_task;
2109640708a2SPavel Emelyanov 
2110640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2111640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2112640708a2SPavel Emelyanov 	if (!vma)
2113640708a2SPavel Emelyanov 		goto out_no_vma;
2114640708a2SPavel Emelyanov 
211505f56484SStanislav Kinsbursky 	if (vma->vm_file)
21160168b9e3SAl Viro 		result = proc_map_files_instantiate(dentry, task,
21177b540d06SAl Viro 				(void *)(unsigned long)vma->vm_file->f_mode);
2118640708a2SPavel Emelyanov 
2119640708a2SPavel Emelyanov out_no_vma:
2120640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2121640708a2SPavel Emelyanov 	mmput(mm);
2122640708a2SPavel Emelyanov out_put_task:
2123640708a2SPavel Emelyanov 	put_task_struct(task);
2124640708a2SPavel Emelyanov out:
21250168b9e3SAl Viro 	return result;
2126640708a2SPavel Emelyanov }
2127640708a2SPavel Emelyanov 
2128640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
2129640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
2130640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
2131640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
2132640708a2SPavel Emelyanov };
2133640708a2SPavel Emelyanov 
2134640708a2SPavel Emelyanov static int
2135f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2136640708a2SPavel Emelyanov {
2137640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2138640708a2SPavel Emelyanov 	struct task_struct *task;
2139640708a2SPavel Emelyanov 	struct mm_struct *mm;
2140f0c3b509SAl Viro 	unsigned long nr_files, pos, i;
214194f8f3b0SKent Overstreet 	GENRADIX(struct map_files_info) fa;
2142f0c3b509SAl Viro 	struct map_files_info *p;
2143640708a2SPavel Emelyanov 	int ret;
2144640708a2SPavel Emelyanov 
214594f8f3b0SKent Overstreet 	genradix_init(&fa);
214694f8f3b0SKent Overstreet 
2147640708a2SPavel Emelyanov 	ret = -ENOENT;
2148f0c3b509SAl Viro 	task = get_proc_task(file_inode(file));
2149640708a2SPavel Emelyanov 	if (!task)
2150640708a2SPavel Emelyanov 		goto out;
2151640708a2SPavel Emelyanov 
2152640708a2SPavel Emelyanov 	ret = -EACCES;
2153caaee623SJann Horn 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2154640708a2SPavel Emelyanov 		goto out_put_task;
2155640708a2SPavel Emelyanov 
2156640708a2SPavel Emelyanov 	ret = 0;
2157f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
2158eb94cd96SCyrill Gorcunov 		goto out_put_task;
2159640708a2SPavel Emelyanov 
2160640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2161640708a2SPavel Emelyanov 	if (!mm)
2162eb94cd96SCyrill Gorcunov 		goto out_put_task;
2163640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2164640708a2SPavel Emelyanov 
2165640708a2SPavel Emelyanov 	nr_files = 0;
2166640708a2SPavel Emelyanov 
2167640708a2SPavel Emelyanov 	/*
2168640708a2SPavel Emelyanov 	 * We need two passes here:
2169640708a2SPavel Emelyanov 	 *
2170640708a2SPavel Emelyanov 	 *  1) Collect vmas of mapped files with mmap_sem taken
2171640708a2SPavel Emelyanov 	 *  2) Release mmap_sem and instantiate entries
2172640708a2SPavel Emelyanov 	 *
2173640708a2SPavel Emelyanov 	 * otherwise we get lockdep complained, since filldir()
2174640708a2SPavel Emelyanov 	 * routine might require mmap_sem taken in might_fault().
2175640708a2SPavel Emelyanov 	 */
2176640708a2SPavel Emelyanov 
2177640708a2SPavel Emelyanov 	for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2178640708a2SPavel Emelyanov 		if (!vma->vm_file)
2179640708a2SPavel Emelyanov 			continue;
2180f0c3b509SAl Viro 		if (++pos <= ctx->pos)
2181640708a2SPavel Emelyanov 			continue;
2182640708a2SPavel Emelyanov 
218394f8f3b0SKent Overstreet 		p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
218494f8f3b0SKent Overstreet 		if (!p) {
218594f8f3b0SKent Overstreet 			ret = -ENOMEM;
218694f8f3b0SKent Overstreet 			up_read(&mm->mmap_sem);
218794f8f3b0SKent Overstreet 			mmput(mm);
218894f8f3b0SKent Overstreet 			goto out_put_task;
2189640708a2SPavel Emelyanov 		}
219094f8f3b0SKent Overstreet 
219194f8f3b0SKent Overstreet 		p->start = vma->vm_start;
219294f8f3b0SKent Overstreet 		p->end = vma->vm_end;
219394f8f3b0SKent Overstreet 		p->mode = vma->vm_file->f_mode;
2194640708a2SPavel Emelyanov 	}
2195640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2196fe079a5eSAlexey Dobriyan 	mmput(mm);
2197640708a2SPavel Emelyanov 
2198640708a2SPavel Emelyanov 	for (i = 0; i < nr_files; i++) {
219920d28cdeSAlexey Dobriyan 		char buf[4 * sizeof(long) + 2];	/* max: %lx-%lx\0 */
220020d28cdeSAlexey Dobriyan 		unsigned int len;
220120d28cdeSAlexey Dobriyan 
220294f8f3b0SKent Overstreet 		p = genradix_ptr(&fa, i);
220320d28cdeSAlexey Dobriyan 		len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2204f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
220520d28cdeSAlexey Dobriyan 				      buf, len,
2206640708a2SPavel Emelyanov 				      proc_map_files_instantiate,
22077b540d06SAl Viro 				      task,
2208f0c3b509SAl Viro 				      (void *)(unsigned long)p->mode))
2209640708a2SPavel Emelyanov 			break;
2210f0c3b509SAl Viro 		ctx->pos++;
2211640708a2SPavel Emelyanov 	}
2212640708a2SPavel Emelyanov 
2213640708a2SPavel Emelyanov out_put_task:
2214640708a2SPavel Emelyanov 	put_task_struct(task);
2215640708a2SPavel Emelyanov out:
221694f8f3b0SKent Overstreet 	genradix_free(&fa);
2217640708a2SPavel Emelyanov 	return ret;
2218640708a2SPavel Emelyanov }
2219640708a2SPavel Emelyanov 
2220640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2221640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2222f50752eaSAl Viro 	.iterate_shared	= proc_map_files_readdir,
2223f50752eaSAl Viro 	.llseek		= generic_file_llseek,
2224640708a2SPavel Emelyanov };
2225640708a2SPavel Emelyanov 
2226b18b6a9cSNicolas Pitre #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
222748f6a7a5SPavel Emelyanov struct timers_private {
222848f6a7a5SPavel Emelyanov 	struct pid *pid;
222948f6a7a5SPavel Emelyanov 	struct task_struct *task;
223048f6a7a5SPavel Emelyanov 	struct sighand_struct *sighand;
223157b8015eSPavel Emelyanov 	struct pid_namespace *ns;
223248f6a7a5SPavel Emelyanov 	unsigned long flags;
223348f6a7a5SPavel Emelyanov };
223448f6a7a5SPavel Emelyanov 
223548f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos)
223648f6a7a5SPavel Emelyanov {
223748f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
223848f6a7a5SPavel Emelyanov 
223948f6a7a5SPavel Emelyanov 	tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
224048f6a7a5SPavel Emelyanov 	if (!tp->task)
224148f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
224248f6a7a5SPavel Emelyanov 
224348f6a7a5SPavel Emelyanov 	tp->sighand = lock_task_sighand(tp->task, &tp->flags);
224448f6a7a5SPavel Emelyanov 	if (!tp->sighand)
224548f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
224648f6a7a5SPavel Emelyanov 
224748f6a7a5SPavel Emelyanov 	return seq_list_start(&tp->task->signal->posix_timers, *pos);
224848f6a7a5SPavel Emelyanov }
224948f6a7a5SPavel Emelyanov 
225048f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
225148f6a7a5SPavel Emelyanov {
225248f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
225348f6a7a5SPavel Emelyanov 	return seq_list_next(v, &tp->task->signal->posix_timers, pos);
225448f6a7a5SPavel Emelyanov }
225548f6a7a5SPavel Emelyanov 
225648f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v)
225748f6a7a5SPavel Emelyanov {
225848f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
225948f6a7a5SPavel Emelyanov 
226048f6a7a5SPavel Emelyanov 	if (tp->sighand) {
226148f6a7a5SPavel Emelyanov 		unlock_task_sighand(tp->task, &tp->flags);
226248f6a7a5SPavel Emelyanov 		tp->sighand = NULL;
226348f6a7a5SPavel Emelyanov 	}
226448f6a7a5SPavel Emelyanov 
226548f6a7a5SPavel Emelyanov 	if (tp->task) {
226648f6a7a5SPavel Emelyanov 		put_task_struct(tp->task);
226748f6a7a5SPavel Emelyanov 		tp->task = NULL;
226848f6a7a5SPavel Emelyanov 	}
226948f6a7a5SPavel Emelyanov }
227048f6a7a5SPavel Emelyanov 
227148f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v)
227248f6a7a5SPavel Emelyanov {
227348f6a7a5SPavel Emelyanov 	struct k_itimer *timer;
227457b8015eSPavel Emelyanov 	struct timers_private *tp = m->private;
227557b8015eSPavel Emelyanov 	int notify;
2276cedbccabSAlexey Dobriyan 	static const char * const nstr[] = {
227757b8015eSPavel Emelyanov 		[SIGEV_SIGNAL] = "signal",
227857b8015eSPavel Emelyanov 		[SIGEV_NONE] = "none",
227957b8015eSPavel Emelyanov 		[SIGEV_THREAD] = "thread",
228057b8015eSPavel Emelyanov 	};
228148f6a7a5SPavel Emelyanov 
228248f6a7a5SPavel Emelyanov 	timer = list_entry((struct list_head *)v, struct k_itimer, list);
228357b8015eSPavel Emelyanov 	notify = timer->it_sigev_notify;
228457b8015eSPavel Emelyanov 
228548f6a7a5SPavel Emelyanov 	seq_printf(m, "ID: %d\n", timer->it_id);
2286ba3edf1fSLinus Torvalds 	seq_printf(m, "signal: %d/%px\n",
228725ce3191SJoe Perches 		   timer->sigq->info.si_signo,
228857b8015eSPavel Emelyanov 		   timer->sigq->info.si_value.sival_ptr);
228957b8015eSPavel Emelyanov 	seq_printf(m, "notify: %s/%s.%d\n",
229057b8015eSPavel Emelyanov 		   nstr[notify & ~SIGEV_THREAD_ID],
229157b8015eSPavel Emelyanov 		   (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
229257b8015eSPavel Emelyanov 		   pid_nr_ns(timer->it_pid, tp->ns));
229315ef0298SPavel Tikhomirov 	seq_printf(m, "ClockID: %d\n", timer->it_clock);
229448f6a7a5SPavel Emelyanov 
229548f6a7a5SPavel Emelyanov 	return 0;
229648f6a7a5SPavel Emelyanov }
229748f6a7a5SPavel Emelyanov 
229848f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = {
229948f6a7a5SPavel Emelyanov 	.start	= timers_start,
230048f6a7a5SPavel Emelyanov 	.next	= timers_next,
230148f6a7a5SPavel Emelyanov 	.stop	= timers_stop,
230248f6a7a5SPavel Emelyanov 	.show	= show_timer,
230348f6a7a5SPavel Emelyanov };
230448f6a7a5SPavel Emelyanov 
230548f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file)
230648f6a7a5SPavel Emelyanov {
230748f6a7a5SPavel Emelyanov 	struct timers_private *tp;
230848f6a7a5SPavel Emelyanov 
230948f6a7a5SPavel Emelyanov 	tp = __seq_open_private(file, &proc_timers_seq_ops,
231048f6a7a5SPavel Emelyanov 			sizeof(struct timers_private));
231148f6a7a5SPavel Emelyanov 	if (!tp)
231248f6a7a5SPavel Emelyanov 		return -ENOMEM;
231348f6a7a5SPavel Emelyanov 
231448f6a7a5SPavel Emelyanov 	tp->pid = proc_pid(inode);
231576f668beSChristoph Hellwig 	tp->ns = proc_pid_ns(inode);
231648f6a7a5SPavel Emelyanov 	return 0;
231748f6a7a5SPavel Emelyanov }
231848f6a7a5SPavel Emelyanov 
231948f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = {
232048f6a7a5SPavel Emelyanov 	.open		= proc_timers_open,
232148f6a7a5SPavel Emelyanov 	.read		= seq_read,
232248f6a7a5SPavel Emelyanov 	.llseek		= seq_lseek,
232348f6a7a5SPavel Emelyanov 	.release	= seq_release_private,
232448f6a7a5SPavel Emelyanov };
2325b5946beaSEric Engestrom #endif
2326640708a2SPavel Emelyanov 
23275de23d43SJohn Stultz static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
23285de23d43SJohn Stultz 					size_t count, loff_t *offset)
23295de23d43SJohn Stultz {
23305de23d43SJohn Stultz 	struct inode *inode = file_inode(file);
23315de23d43SJohn Stultz 	struct task_struct *p;
23325de23d43SJohn Stultz 	u64 slack_ns;
23335de23d43SJohn Stultz 	int err;
23345de23d43SJohn Stultz 
23355de23d43SJohn Stultz 	err = kstrtoull_from_user(buf, count, 10, &slack_ns);
23365de23d43SJohn Stultz 	if (err < 0)
23375de23d43SJohn Stultz 		return err;
23385de23d43SJohn Stultz 
23395de23d43SJohn Stultz 	p = get_proc_task(inode);
23405de23d43SJohn Stultz 	if (!p)
23415de23d43SJohn Stultz 		return -ESRCH;
23425de23d43SJohn Stultz 
23434b2bd5feSJohn Stultz 	if (p != current) {
23448da0b4f6SBenjamin Gordon 		rcu_read_lock();
23458da0b4f6SBenjamin Gordon 		if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
23468da0b4f6SBenjamin Gordon 			rcu_read_unlock();
23477abbaf94SJohn Stultz 			count = -EPERM;
23487abbaf94SJohn Stultz 			goto out;
23497abbaf94SJohn Stultz 		}
23508da0b4f6SBenjamin Gordon 		rcu_read_unlock();
23517abbaf94SJohn Stultz 
2352904763e1SJohn Stultz 		err = security_task_setscheduler(p);
2353904763e1SJohn Stultz 		if (err) {
2354904763e1SJohn Stultz 			count = err;
2355904763e1SJohn Stultz 			goto out;
2356904763e1SJohn Stultz 		}
23574b2bd5feSJohn Stultz 	}
2358904763e1SJohn Stultz 
23595de23d43SJohn Stultz 	task_lock(p);
23605de23d43SJohn Stultz 	if (slack_ns == 0)
23615de23d43SJohn Stultz 		p->timer_slack_ns = p->default_timer_slack_ns;
23625de23d43SJohn Stultz 	else
23635de23d43SJohn Stultz 		p->timer_slack_ns = slack_ns;
23645de23d43SJohn Stultz 	task_unlock(p);
23655de23d43SJohn Stultz 
23667abbaf94SJohn Stultz out:
23675de23d43SJohn Stultz 	put_task_struct(p);
23685de23d43SJohn Stultz 
23695de23d43SJohn Stultz 	return count;
23705de23d43SJohn Stultz }
23715de23d43SJohn Stultz 
23725de23d43SJohn Stultz static int timerslack_ns_show(struct seq_file *m, void *v)
23735de23d43SJohn Stultz {
23745de23d43SJohn Stultz 	struct inode *inode = m->private;
23755de23d43SJohn Stultz 	struct task_struct *p;
23765de23d43SJohn Stultz 	int err = 0;
23775de23d43SJohn Stultz 
23785de23d43SJohn Stultz 	p = get_proc_task(inode);
23795de23d43SJohn Stultz 	if (!p)
23805de23d43SJohn Stultz 		return -ESRCH;
23815de23d43SJohn Stultz 
23824b2bd5feSJohn Stultz 	if (p != current) {
23838da0b4f6SBenjamin Gordon 		rcu_read_lock();
23848da0b4f6SBenjamin Gordon 		if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
23858da0b4f6SBenjamin Gordon 			rcu_read_unlock();
23867abbaf94SJohn Stultz 			err = -EPERM;
23877abbaf94SJohn Stultz 			goto out;
23887abbaf94SJohn Stultz 		}
23898da0b4f6SBenjamin Gordon 		rcu_read_unlock();
23908da0b4f6SBenjamin Gordon 
2391904763e1SJohn Stultz 		err = security_task_getscheduler(p);
2392904763e1SJohn Stultz 		if (err)
2393904763e1SJohn Stultz 			goto out;
23944b2bd5feSJohn Stultz 	}
2395904763e1SJohn Stultz 
23965de23d43SJohn Stultz 	task_lock(p);
23975de23d43SJohn Stultz 	seq_printf(m, "%llu\n", p->timer_slack_ns);
23985de23d43SJohn Stultz 	task_unlock(p);
23995de23d43SJohn Stultz 
24007abbaf94SJohn Stultz out:
24015de23d43SJohn Stultz 	put_task_struct(p);
24025de23d43SJohn Stultz 
24035de23d43SJohn Stultz 	return err;
24045de23d43SJohn Stultz }
24055de23d43SJohn Stultz 
24065de23d43SJohn Stultz static int timerslack_ns_open(struct inode *inode, struct file *filp)
24075de23d43SJohn Stultz {
24085de23d43SJohn Stultz 	return single_open(filp, timerslack_ns_show, inode);
24095de23d43SJohn Stultz }
24105de23d43SJohn Stultz 
24115de23d43SJohn Stultz static const struct file_operations proc_pid_set_timerslack_ns_operations = {
24125de23d43SJohn Stultz 	.open		= timerslack_ns_open,
24135de23d43SJohn Stultz 	.read		= seq_read,
24145de23d43SJohn Stultz 	.write		= timerslack_ns_write,
24155de23d43SJohn Stultz 	.llseek		= seq_lseek,
24165de23d43SJohn Stultz 	.release	= single_release,
24175de23d43SJohn Stultz };
24185de23d43SJohn Stultz 
24190168b9e3SAl Viro static struct dentry *proc_pident_instantiate(struct dentry *dentry,
24200168b9e3SAl Viro 	struct task_struct *task, const void *ptr)
2421444ceed8SEric W. Biederman {
2422c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2423444ceed8SEric W. Biederman 	struct inode *inode;
2424444ceed8SEric W. Biederman 	struct proc_inode *ei;
2425444ceed8SEric W. Biederman 
24260168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2427444ceed8SEric W. Biederman 	if (!inode)
24280168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
2429444ceed8SEric W. Biederman 
2430444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2431444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2432bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2433444ceed8SEric W. Biederman 	if (p->iop)
2434444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2435444ceed8SEric W. Biederman 	if (p->fop)
2436444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2437444ceed8SEric W. Biederman 	ei->op = p->op;
24381bbc5513SAl Viro 	pid_update_inode(task, inode);
2439fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
24400168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
2441444ceed8SEric W. Biederman }
2442444ceed8SEric W. Biederman 
24431da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
24441da177e4SLinus Torvalds 					 struct dentry *dentry,
2445d5a572a4SAlexey Dobriyan 					 const struct pid_entry *p,
2446d5a572a4SAlexey Dobriyan 					 const struct pid_entry *end)
24471da177e4SLinus Torvalds {
244899f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
24490168b9e3SAl Viro 	struct dentry *res = ERR_PTR(-ENOENT);
24501da177e4SLinus Torvalds 
245199f89551SEric W. Biederman 	if (!task)
245299f89551SEric W. Biederman 		goto out_no_task;
24531da177e4SLinus Torvalds 
245420cdc894SEric W. Biederman 	/*
245520cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
245620cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
245720cdc894SEric W. Biederman 	 */
2458d5a572a4SAlexey Dobriyan 	for (; p < end; p++) {
24591da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
24601da177e4SLinus Torvalds 			continue;
246126b95137SAlexey Dobriyan 		if (!memcmp(dentry->d_name.name, p->name, p->len)) {
246226b95137SAlexey Dobriyan 			res = proc_pident_instantiate(dentry, task, p);
24631da177e4SLinus Torvalds 			break;
24641da177e4SLinus Torvalds 		}
246526b95137SAlexey Dobriyan 	}
246699f89551SEric W. Biederman 	put_task_struct(task);
246799f89551SEric W. Biederman out_no_task:
24680168b9e3SAl Viro 	return res;
24691da177e4SLinus Torvalds }
24701da177e4SLinus Torvalds 
2471f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2472c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
247328a6d671SEric W. Biederman {
2474f0c3b509SAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
2475f0c3b509SAl Viro 	const struct pid_entry *p;
247628a6d671SEric W. Biederman 
247728a6d671SEric W. Biederman 	if (!task)
2478f0c3b509SAl Viro 		return -ENOENT;
247928a6d671SEric W. Biederman 
2480f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
248128a6d671SEric W. Biederman 		goto out;
24821da177e4SLinus Torvalds 
2483f0c3b509SAl Viro 	if (ctx->pos >= nents + 2)
2484f0c3b509SAl Viro 		goto out;
2485f0c3b509SAl Viro 
2486bac5f5d5SAlexey Dobriyan 	for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2487f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, p->name, p->len,
2488f0c3b509SAl Viro 				proc_pident_instantiate, task, p))
2489f0c3b509SAl Viro 			break;
2490f0c3b509SAl Viro 		ctx->pos++;
2491f0c3b509SAl Viro 	}
249228a6d671SEric W. Biederman out:
249361a28784SEric W. Biederman 	put_task_struct(task);
2494f0c3b509SAl Viro 	return 0;
24951da177e4SLinus Torvalds }
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
249828a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
249928a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
250028a6d671SEric W. Biederman {
2501496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
250204ff9708SAl Viro 	char *p = NULL;
250328a6d671SEric W. Biederman 	ssize_t length;
250428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
250528a6d671SEric W. Biederman 
250628a6d671SEric W. Biederman 	if (!task)
250704ff9708SAl Viro 		return -ESRCH;
250828a6d671SEric W. Biederman 
25096d9c939dSCasey Schaufler 	length = security_getprocattr(task, PROC_I(inode)->op.lsm,
25102fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
251104ff9708SAl Viro 				      &p);
251228a6d671SEric W. Biederman 	put_task_struct(task);
251304ff9708SAl Viro 	if (length > 0)
251404ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
251504ff9708SAl Viro 	kfree(p);
251628a6d671SEric W. Biederman 	return length;
251728a6d671SEric W. Biederman }
251828a6d671SEric W. Biederman 
251928a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
252028a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
252128a6d671SEric W. Biederman {
2522496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
252341089b6dSAlexey Dobriyan 	struct task_struct *task;
2524bb646cdbSAl Viro 	void *page;
252541089b6dSAlexey Dobriyan 	int rv;
252628a6d671SEric W. Biederman 
252741089b6dSAlexey Dobriyan 	rcu_read_lock();
252841089b6dSAlexey Dobriyan 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
252941089b6dSAlexey Dobriyan 	if (!task) {
253041089b6dSAlexey Dobriyan 		rcu_read_unlock();
253141089b6dSAlexey Dobriyan 		return -ESRCH;
253241089b6dSAlexey Dobriyan 	}
2533b21507e2SStephen Smalley 	/* A task may only write its own attributes. */
253441089b6dSAlexey Dobriyan 	if (current != task) {
253541089b6dSAlexey Dobriyan 		rcu_read_unlock();
253641089b6dSAlexey Dobriyan 		return -EACCES;
253741089b6dSAlexey Dobriyan 	}
253835a196beSPaul Moore 	/* Prevent changes to overridden credentials. */
253935a196beSPaul Moore 	if (current_cred() != current_real_cred()) {
254035a196beSPaul Moore 		rcu_read_unlock();
254135a196beSPaul Moore 		return -EBUSY;
254235a196beSPaul Moore 	}
254341089b6dSAlexey Dobriyan 	rcu_read_unlock();
2544b21507e2SStephen Smalley 
254528a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
254628a6d671SEric W. Biederman 		count = PAGE_SIZE;
254728a6d671SEric W. Biederman 
254828a6d671SEric W. Biederman 	/* No partial writes. */
254928a6d671SEric W. Biederman 	if (*ppos != 0)
255041089b6dSAlexey Dobriyan 		return -EINVAL;
255128a6d671SEric W. Biederman 
2552bb646cdbSAl Viro 	page = memdup_user(buf, count);
2553bb646cdbSAl Viro 	if (IS_ERR(page)) {
255441089b6dSAlexey Dobriyan 		rv = PTR_ERR(page);
255528a6d671SEric W. Biederman 		goto out;
2556bb646cdbSAl Viro 	}
255728a6d671SEric W. Biederman 
2558107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
255941089b6dSAlexey Dobriyan 	rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
256041089b6dSAlexey Dobriyan 	if (rv < 0)
2561107db7c7SDavid Howells 		goto out_free;
2562107db7c7SDavid Howells 
25636d9c939dSCasey Schaufler 	rv = security_setprocattr(PROC_I(inode)->op.lsm,
25646d9c939dSCasey Schaufler 				  file->f_path.dentry->d_name.name, page,
25656d9c939dSCasey Schaufler 				  count);
2566b21507e2SStephen Smalley 	mutex_unlock(&current->signal->cred_guard_mutex);
256728a6d671SEric W. Biederman out_free:
2568bb646cdbSAl Viro 	kfree(page);
256928a6d671SEric W. Biederman out:
257041089b6dSAlexey Dobriyan 	return rv;
257128a6d671SEric W. Biederman }
257228a6d671SEric W. Biederman 
257300977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
257428a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
257528a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
257687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
257728a6d671SEric W. Biederman };
257828a6d671SEric W. Biederman 
25796d9c939dSCasey Schaufler #define LSM_DIR_OPS(LSM) \
25806d9c939dSCasey Schaufler static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
25816d9c939dSCasey Schaufler 			     struct dir_context *ctx) \
25826d9c939dSCasey Schaufler { \
25836d9c939dSCasey Schaufler 	return proc_pident_readdir(filp, ctx, \
25846d9c939dSCasey Schaufler 				   LSM##_attr_dir_stuff, \
25856d9c939dSCasey Schaufler 				   ARRAY_SIZE(LSM##_attr_dir_stuff)); \
25866d9c939dSCasey Schaufler } \
25876d9c939dSCasey Schaufler \
25886d9c939dSCasey Schaufler static const struct file_operations proc_##LSM##_attr_dir_ops = { \
25896d9c939dSCasey Schaufler 	.read		= generic_read_dir, \
25906d9c939dSCasey Schaufler 	.iterate	= proc_##LSM##_attr_dir_iterate, \
25916d9c939dSCasey Schaufler 	.llseek		= default_llseek, \
25926d9c939dSCasey Schaufler }; \
25936d9c939dSCasey Schaufler \
25946d9c939dSCasey Schaufler static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
25956d9c939dSCasey Schaufler 				struct dentry *dentry, unsigned int flags) \
25966d9c939dSCasey Schaufler { \
25976d9c939dSCasey Schaufler 	return proc_pident_lookup(dir, dentry, \
25986d9c939dSCasey Schaufler 				  LSM##_attr_dir_stuff, \
2599d5a572a4SAlexey Dobriyan 				  LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
26006d9c939dSCasey Schaufler } \
26016d9c939dSCasey Schaufler \
26026d9c939dSCasey Schaufler static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
26036d9c939dSCasey Schaufler 	.lookup		= proc_##LSM##_attr_dir_lookup, \
26046d9c939dSCasey Schaufler 	.getattr	= pid_getattr, \
26056d9c939dSCasey Schaufler 	.setattr	= proc_setattr, \
26066d9c939dSCasey Schaufler }
26076d9c939dSCasey Schaufler 
26086d9c939dSCasey Schaufler #ifdef CONFIG_SECURITY_SMACK
26096d9c939dSCasey Schaufler static const struct pid_entry smack_attr_dir_stuff[] = {
26106d9c939dSCasey Schaufler 	ATTR("smack", "current",	0666),
26116d9c939dSCasey Schaufler };
26126d9c939dSCasey Schaufler LSM_DIR_OPS(smack);
26136d9c939dSCasey Schaufler #endif
26146d9c939dSCasey Schaufler 
2615c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
26166d9c939dSCasey Schaufler 	ATTR(NULL, "current",		0666),
26176d9c939dSCasey Schaufler 	ATTR(NULL, "prev",		0444),
26186d9c939dSCasey Schaufler 	ATTR(NULL, "exec",		0666),
26196d9c939dSCasey Schaufler 	ATTR(NULL, "fscreate",		0666),
26206d9c939dSCasey Schaufler 	ATTR(NULL, "keycreate",		0666),
26216d9c939dSCasey Schaufler 	ATTR(NULL, "sockcreate",	0666),
26226d9c939dSCasey Schaufler #ifdef CONFIG_SECURITY_SMACK
26236d9c939dSCasey Schaufler 	DIR("smack",			0555,
26246d9c939dSCasey Schaufler 	    proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
26256d9c939dSCasey Schaufler #endif
262628a6d671SEric W. Biederman };
262728a6d671SEric W. Biederman 
2628f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
26291da177e4SLinus Torvalds {
2630f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
263172d9dcfcSEric W. Biederman 				   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
26321da177e4SLinus Torvalds }
26331da177e4SLinus Torvalds 
263400977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
26351da177e4SLinus Torvalds 	.read		= generic_read_dir,
2636f50752eaSAl Viro 	.iterate_shared	= proc_attr_dir_readdir,
2637f50752eaSAl Viro 	.llseek		= generic_file_llseek,
26381da177e4SLinus Torvalds };
26391da177e4SLinus Torvalds 
264072d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
264100cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
26421da177e4SLinus Torvalds {
26437bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
2644d5a572a4SAlexey Dobriyan 				  attr_dir_stuff,
2645d5a572a4SAlexey Dobriyan 				  attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
26461da177e4SLinus Torvalds }
26471da177e4SLinus Torvalds 
2648c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
264972d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
265099f89551SEric W. Biederman 	.getattr	= pid_getattr,
26516d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
26521da177e4SLinus Torvalds };
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds #endif
26551da177e4SLinus Torvalds 
2656698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
26573cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
26583cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
26593cb4a0bbSKawai, Hidehiro {
2660496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
26613cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26623cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
26633cb4a0bbSKawai, Hidehiro 	size_t len;
26643cb4a0bbSKawai, Hidehiro 	int ret;
26653cb4a0bbSKawai, Hidehiro 
26663cb4a0bbSKawai, Hidehiro 	if (!task)
26673cb4a0bbSKawai, Hidehiro 		return -ESRCH;
26683cb4a0bbSKawai, Hidehiro 
26693cb4a0bbSKawai, Hidehiro 	ret = 0;
26703cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
26713cb4a0bbSKawai, Hidehiro 	if (mm) {
26723cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
26733cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
26743cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
26753cb4a0bbSKawai, Hidehiro 		mmput(mm);
26763cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
26773cb4a0bbSKawai, Hidehiro 	}
26783cb4a0bbSKawai, Hidehiro 
26793cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
26803cb4a0bbSKawai, Hidehiro 
26813cb4a0bbSKawai, Hidehiro 	return ret;
26823cb4a0bbSKawai, Hidehiro }
26833cb4a0bbSKawai, Hidehiro 
26843cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
26853cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
26863cb4a0bbSKawai, Hidehiro 					  size_t count,
26873cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
26883cb4a0bbSKawai, Hidehiro {
26893cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
26903cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26913cb4a0bbSKawai, Hidehiro 	unsigned int val;
26923cb4a0bbSKawai, Hidehiro 	int ret;
26933cb4a0bbSKawai, Hidehiro 	int i;
26943cb4a0bbSKawai, Hidehiro 	unsigned long mask;
26953cb4a0bbSKawai, Hidehiro 
2696774636e1SAlexey Dobriyan 	ret = kstrtouint_from_user(buf, count, 0, &val);
2697774636e1SAlexey Dobriyan 	if (ret < 0)
2698774636e1SAlexey Dobriyan 		return ret;
26993cb4a0bbSKawai, Hidehiro 
27003cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
2701496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
27023cb4a0bbSKawai, Hidehiro 	if (!task)
27033cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27043cb4a0bbSKawai, Hidehiro 
27053cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
27063cb4a0bbSKawai, Hidehiro 	if (!mm)
27073cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
270841a0c249SColin Ian King 	ret = 0;
27093cb4a0bbSKawai, Hidehiro 
27103cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
27113cb4a0bbSKawai, Hidehiro 		if (val & mask)
27123cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27133cb4a0bbSKawai, Hidehiro 		else
27143cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27153cb4a0bbSKawai, Hidehiro 	}
27163cb4a0bbSKawai, Hidehiro 
27173cb4a0bbSKawai, Hidehiro 	mmput(mm);
27183cb4a0bbSKawai, Hidehiro  out_no_mm:
27193cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
27203cb4a0bbSKawai, Hidehiro  out_no_task:
2721774636e1SAlexey Dobriyan 	if (ret < 0)
27223cb4a0bbSKawai, Hidehiro 		return ret;
2723774636e1SAlexey Dobriyan 	return count;
27243cb4a0bbSKawai, Hidehiro }
27253cb4a0bbSKawai, Hidehiro 
27263cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
27273cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
27283cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
272987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
27303cb4a0bbSKawai, Hidehiro };
27313cb4a0bbSKawai, Hidehiro #endif
27323cb4a0bbSKawai, Hidehiro 
2733aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
273419aadc98SAlexey Dobriyan static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2735aba76fdbSAndrew Morton {
2736940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2737297c5d92SAndrea Righi 	unsigned long flags;
2738293eb1e7SVasiliy Kulikov 	int result;
2739297c5d92SAndrea Righi 
2740293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2741293eb1e7SVasiliy Kulikov 	if (result)
2742293eb1e7SVasiliy Kulikov 		return result;
2743293eb1e7SVasiliy Kulikov 
2744caaee623SJann Horn 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2745293eb1e7SVasiliy Kulikov 		result = -EACCES;
2746293eb1e7SVasiliy Kulikov 		goto out_unlock;
2747293eb1e7SVasiliy Kulikov 	}
27481d1221f3SVasiliy Kulikov 
27495995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2750b2d002dbSAndrea Righi 		struct task_struct *t = task;
2751297c5d92SAndrea Righi 
27525995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
27535995477aSAndrea Righi 		while_each_thread(task, t)
27545995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2755297c5d92SAndrea Righi 
2756297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2757297c5d92SAndrea Righi 	}
275825ce3191SJoe Perches 	seq_printf(m,
2759aba76fdbSAndrew Morton 		   "rchar: %llu\n"
2760aba76fdbSAndrew Morton 		   "wchar: %llu\n"
2761aba76fdbSAndrew Morton 		   "syscr: %llu\n"
2762aba76fdbSAndrew Morton 		   "syscw: %llu\n"
2763aba76fdbSAndrew Morton 		   "read_bytes: %llu\n"
2764aba76fdbSAndrew Morton 		   "write_bytes: %llu\n"
2765aba76fdbSAndrew Morton 		   "cancelled_write_bytes: %llu\n",
27667c44319dSAlexander Beregalov 		   (unsigned long long)acct.rchar,
27677c44319dSAlexander Beregalov 		   (unsigned long long)acct.wchar,
27687c44319dSAlexander Beregalov 		   (unsigned long long)acct.syscr,
27697c44319dSAlexander Beregalov 		   (unsigned long long)acct.syscw,
27707c44319dSAlexander Beregalov 		   (unsigned long long)acct.read_bytes,
27717c44319dSAlexander Beregalov 		   (unsigned long long)acct.write_bytes,
27727c44319dSAlexander Beregalov 		   (unsigned long long)acct.cancelled_write_bytes);
277325ce3191SJoe Perches 	result = 0;
277425ce3191SJoe Perches 
2775293eb1e7SVasiliy Kulikov out_unlock:
2776293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2777293eb1e7SVasiliy Kulikov 	return result;
2778aba76fdbSAndrew Morton }
2779297c5d92SAndrea Righi 
278019aadc98SAlexey Dobriyan static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
278119aadc98SAlexey Dobriyan 				  struct pid *pid, struct task_struct *task)
2782297c5d92SAndrea Righi {
278319aadc98SAlexey Dobriyan 	return do_io_accounting(task, m, 0);
2784297c5d92SAndrea Righi }
2785297c5d92SAndrea Righi 
278619aadc98SAlexey Dobriyan static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
278719aadc98SAlexey Dobriyan 				   struct pid *pid, struct task_struct *task)
2788297c5d92SAndrea Righi {
278919aadc98SAlexey Dobriyan 	return do_io_accounting(task, m, 1);
2790297c5d92SAndrea Righi }
2791297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2792aba76fdbSAndrew Morton 
279322d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
279422d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
2795ccf94f1bSFabian Frederick 	const struct seq_operations *seq_ops)
279622d917d8SEric W. Biederman {
279722d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
279822d917d8SEric W. Biederman 	struct task_struct *task;
279922d917d8SEric W. Biederman 	struct seq_file *seq;
280022d917d8SEric W. Biederman 	int ret = -EINVAL;
280122d917d8SEric W. Biederman 
280222d917d8SEric W. Biederman 	task = get_proc_task(inode);
280322d917d8SEric W. Biederman 	if (task) {
280422d917d8SEric W. Biederman 		rcu_read_lock();
280522d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
280622d917d8SEric W. Biederman 		rcu_read_unlock();
280722d917d8SEric W. Biederman 		put_task_struct(task);
280822d917d8SEric W. Biederman 	}
280922d917d8SEric W. Biederman 	if (!ns)
281022d917d8SEric W. Biederman 		goto err;
281122d917d8SEric W. Biederman 
281222d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
281322d917d8SEric W. Biederman 	if (ret)
281422d917d8SEric W. Biederman 		goto err_put_ns;
281522d917d8SEric W. Biederman 
281622d917d8SEric W. Biederman 	seq = file->private_data;
281722d917d8SEric W. Biederman 	seq->private = ns;
281822d917d8SEric W. Biederman 
281922d917d8SEric W. Biederman 	return 0;
282022d917d8SEric W. Biederman err_put_ns:
282122d917d8SEric W. Biederman 	put_user_ns(ns);
282222d917d8SEric W. Biederman err:
282322d917d8SEric W. Biederman 	return ret;
282422d917d8SEric W. Biederman }
282522d917d8SEric W. Biederman 
282622d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
282722d917d8SEric W. Biederman {
282822d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
282922d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
283022d917d8SEric W. Biederman 	put_user_ns(ns);
283122d917d8SEric W. Biederman 	return seq_release(inode, file);
283222d917d8SEric W. Biederman }
283322d917d8SEric W. Biederman 
283422d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
283522d917d8SEric W. Biederman {
283622d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
283722d917d8SEric W. Biederman }
283822d917d8SEric W. Biederman 
283922d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
284022d917d8SEric W. Biederman {
284122d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
284222d917d8SEric W. Biederman }
284322d917d8SEric W. Biederman 
2844f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file)
2845f76d207aSEric W. Biederman {
2846f76d207aSEric W. Biederman 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2847f76d207aSEric W. Biederman }
2848f76d207aSEric W. Biederman 
284922d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
285022d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
285122d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
285222d917d8SEric W. Biederman 	.read		= seq_read,
285322d917d8SEric W. Biederman 	.llseek		= seq_lseek,
285422d917d8SEric W. Biederman 	.release	= proc_id_map_release,
285522d917d8SEric W. Biederman };
285622d917d8SEric W. Biederman 
285722d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
285822d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
285922d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
286022d917d8SEric W. Biederman 	.read		= seq_read,
286122d917d8SEric W. Biederman 	.llseek		= seq_lseek,
286222d917d8SEric W. Biederman 	.release	= proc_id_map_release,
286322d917d8SEric W. Biederman };
2864f76d207aSEric W. Biederman 
2865f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = {
2866f76d207aSEric W. Biederman 	.open		= proc_projid_map_open,
2867f76d207aSEric W. Biederman 	.write		= proc_projid_map_write,
2868f76d207aSEric W. Biederman 	.read		= seq_read,
2869f76d207aSEric W. Biederman 	.llseek		= seq_lseek,
2870f76d207aSEric W. Biederman 	.release	= proc_id_map_release,
2871f76d207aSEric W. Biederman };
28729cc46516SEric W. Biederman 
28739cc46516SEric W. Biederman static int proc_setgroups_open(struct inode *inode, struct file *file)
28749cc46516SEric W. Biederman {
28759cc46516SEric W. Biederman 	struct user_namespace *ns = NULL;
28769cc46516SEric W. Biederman 	struct task_struct *task;
28779cc46516SEric W. Biederman 	int ret;
28789cc46516SEric W. Biederman 
28799cc46516SEric W. Biederman 	ret = -ESRCH;
28809cc46516SEric W. Biederman 	task = get_proc_task(inode);
28819cc46516SEric W. Biederman 	if (task) {
28829cc46516SEric W. Biederman 		rcu_read_lock();
28839cc46516SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
28849cc46516SEric W. Biederman 		rcu_read_unlock();
28859cc46516SEric W. Biederman 		put_task_struct(task);
28869cc46516SEric W. Biederman 	}
28879cc46516SEric W. Biederman 	if (!ns)
28889cc46516SEric W. Biederman 		goto err;
28899cc46516SEric W. Biederman 
28909cc46516SEric W. Biederman 	if (file->f_mode & FMODE_WRITE) {
28919cc46516SEric W. Biederman 		ret = -EACCES;
28929cc46516SEric W. Biederman 		if (!ns_capable(ns, CAP_SYS_ADMIN))
28939cc46516SEric W. Biederman 			goto err_put_ns;
28949cc46516SEric W. Biederman 	}
28959cc46516SEric W. Biederman 
28969cc46516SEric W. Biederman 	ret = single_open(file, &proc_setgroups_show, ns);
28979cc46516SEric W. Biederman 	if (ret)
28989cc46516SEric W. Biederman 		goto err_put_ns;
28999cc46516SEric W. Biederman 
29009cc46516SEric W. Biederman 	return 0;
29019cc46516SEric W. Biederman err_put_ns:
29029cc46516SEric W. Biederman 	put_user_ns(ns);
29039cc46516SEric W. Biederman err:
29049cc46516SEric W. Biederman 	return ret;
29059cc46516SEric W. Biederman }
29069cc46516SEric W. Biederman 
29079cc46516SEric W. Biederman static int proc_setgroups_release(struct inode *inode, struct file *file)
29089cc46516SEric W. Biederman {
29099cc46516SEric W. Biederman 	struct seq_file *seq = file->private_data;
29109cc46516SEric W. Biederman 	struct user_namespace *ns = seq->private;
29119cc46516SEric W. Biederman 	int ret = single_release(inode, file);
29129cc46516SEric W. Biederman 	put_user_ns(ns);
29139cc46516SEric W. Biederman 	return ret;
29149cc46516SEric W. Biederman }
29159cc46516SEric W. Biederman 
29169cc46516SEric W. Biederman static const struct file_operations proc_setgroups_operations = {
29179cc46516SEric W. Biederman 	.open		= proc_setgroups_open,
29189cc46516SEric W. Biederman 	.write		= proc_setgroups_write,
29199cc46516SEric W. Biederman 	.read		= seq_read,
29209cc46516SEric W. Biederman 	.llseek		= seq_lseek,
29219cc46516SEric W. Biederman 	.release	= proc_setgroups_release,
29229cc46516SEric W. Biederman };
292322d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
292422d917d8SEric W. Biederman 
292547830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
292647830723SKees Cook 				struct pid *pid, struct task_struct *task)
292747830723SKees Cook {
2928a9712bc1SAl Viro 	int err = lock_trace(task);
2929a9712bc1SAl Viro 	if (!err) {
293047830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2931a9712bc1SAl Viro 		unlock_trace(task);
2932a9712bc1SAl Viro 	}
2933a9712bc1SAl Viro 	return err;
293447830723SKees Cook }
293547830723SKees Cook 
29367c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
29377c23b330SJosh Poimboeuf static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
29387c23b330SJosh Poimboeuf 				struct pid *pid, struct task_struct *task)
29397c23b330SJosh Poimboeuf {
29407c23b330SJosh Poimboeuf 	seq_printf(m, "%d\n", task->patch_state);
29417c23b330SJosh Poimboeuf 	return 0;
29427c23b330SJosh Poimboeuf }
29437c23b330SJosh Poimboeuf #endif /* CONFIG_LIVEPATCH */
29447c23b330SJosh Poimboeuf 
2945c8d12627SAlexander Popov #ifdef CONFIG_STACKLEAK_METRICS
2946c8d12627SAlexander Popov static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2947c8d12627SAlexander Popov 				struct pid *pid, struct task_struct *task)
2948c8d12627SAlexander Popov {
2949c8d12627SAlexander Popov 	unsigned long prev_depth = THREAD_SIZE -
2950c8d12627SAlexander Popov 				(task->prev_lowest_stack & (THREAD_SIZE - 1));
2951c8d12627SAlexander Popov 	unsigned long depth = THREAD_SIZE -
2952c8d12627SAlexander Popov 				(task->lowest_stack & (THREAD_SIZE - 1));
2953c8d12627SAlexander Popov 
2954c8d12627SAlexander Popov 	seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2955c8d12627SAlexander Popov 							prev_depth, depth);
2956c8d12627SAlexander Popov 	return 0;
2957c8d12627SAlexander Popov }
2958c8d12627SAlexander Popov #endif /* CONFIG_STACKLEAK_METRICS */
2959c8d12627SAlexander Popov 
2960801199ceSEric W. Biederman /*
296128a6d671SEric W. Biederman  * Thread groups
296228a6d671SEric W. Biederman  */
296300977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2964c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
296520cdc894SEric W. Biederman 
2966c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2967631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2968631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2969640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2970631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
29716b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2972b2211a36SAndrew Morton #ifdef CONFIG_NET
2973631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2974b2211a36SAndrew Morton #endif
2975631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2976c5317167SAl Viro 	REG("auxv",       S_IRUSR, proc_auxv_operations),
2977631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
297835a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
29791c963eb1SAlexey Dobriyan 	ONE("limits",	  S_IRUGO, proc_pid_limits),
298043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2981631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
298243ae34cbSIngo Molnar #endif
29835091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
29845091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
29855091faa4SMike Galbraith #endif
29864614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2987ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
298809d93bd6SAlexey Dobriyan 	ONE("syscall",    S_IRUSR, proc_pid_syscall),
2989ebcb6734SRoland McGrath #endif
2990c2c0bb44SAlexey Dobriyan 	REG("cmdline",    S_IRUGO, proc_pid_cmdline_ops),
2991631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2992631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2993b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
299428a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2995b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
299628a6d671SEric W. Biederman #endif
2997631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2998631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2999631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
3000631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
3001631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
3002631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3003631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
30041e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3005631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3006b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
3007493b0e9dSDaniel Colascione 	REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
300832ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
300928a6d671SEric W. Biederman #endif
301028a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3011631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
301228a6d671SEric W. Biederman #endif
301328a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3014edfcd606SAlexey Dobriyan 	ONE("wchan",      S_IRUGO, proc_pid_wchan),
301528a6d671SEric W. Biederman #endif
30162ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
301735a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
301828a6d671SEric W. Biederman #endif
30195968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
3020f6e826caSAlexey Dobriyan 	ONE("schedstat",  S_IRUGO, proc_pid_schedstat),
302128a6d671SEric W. Biederman #endif
30229745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3023631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30249745512cSArjan van de Ven #endif
30258793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
302652de4779SZefan Li 	ONE("cpuset",     S_IRUGO, proc_cpuset_show),
302728a6d671SEric W. Biederman #endif
3028a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3029006f4ac4SZefan Li 	ONE("cgroup",  S_IRUGO, proc_cgroup_show),
3030a424316cSPaul Menage #endif
30316ba51e37SAlexey Dobriyan 	ONE("oom_score",  S_IRUGO, proc_oom_score),
3032fa0cbbf1SDavid Rientjes 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3033a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
30344b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
3035631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3036631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
303728a6d671SEric W. Biederman #endif
3038f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3039631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3040168c42bcSAkinobu Mita 	REG("fail-nth", 0644, proc_fail_nth_operations),
3041f4f154fdSAkinobu Mita #endif
3042698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
3043631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
30443cb4a0bbSKawai, Hidehiro #endif
3045aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
304619aadc98SAlexey Dobriyan 	ONE("io",	S_IRUSR, proc_tgid_io_accounting),
3047aba76fdbSAndrew Morton #endif
304822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
304922d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
305022d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
3051f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
30529cc46516SEric W. Biederman 	REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
305322d917d8SEric W. Biederman #endif
3054b18b6a9cSNicolas Pitre #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
305548f6a7a5SPavel Emelyanov 	REG("timers",	  S_IRUGO, proc_timers_operations),
305648f6a7a5SPavel Emelyanov #endif
30575de23d43SJohn Stultz 	REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
30587c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
30597c23b330SJosh Poimboeuf 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
30607c23b330SJosh Poimboeuf #endif
3061c8d12627SAlexander Popov #ifdef CONFIG_STACKLEAK_METRICS
3062c8d12627SAlexander Popov 	ONE("stack_depth", S_IRUGO, proc_stack_depth),
3063c8d12627SAlexander Popov #endif
306428a6d671SEric W. Biederman };
306528a6d671SEric W. Biederman 
3066f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
306728a6d671SEric W. Biederman {
3068f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
306928a6d671SEric W. Biederman 				   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
307028a6d671SEric W. Biederman }
307128a6d671SEric W. Biederman 
307200977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
307328a6d671SEric W. Biederman 	.read		= generic_read_dir,
3074f50752eaSAl Viro 	.iterate_shared	= proc_tgid_base_readdir,
3075f50752eaSAl Viro 	.llseek		= generic_file_llseek,
307628a6d671SEric W. Biederman };
307728a6d671SEric W. Biederman 
30783eb39f47SChristian Brauner struct pid *tgid_pidfd_to_pid(const struct file *file)
30793eb39f47SChristian Brauner {
3080*30d158b1SChristian Brauner 	if (file->f_op != &proc_tgid_base_operations)
30813eb39f47SChristian Brauner 		return ERR_PTR(-EBADF);
30823eb39f47SChristian Brauner 
30833eb39f47SChristian Brauner 	return proc_pid(file_inode(file));
30843eb39f47SChristian Brauner }
30853eb39f47SChristian Brauner 
308600cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
308700cd8dd3SAl Viro {
30887bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
3089d5a572a4SAlexey Dobriyan 				  tgid_base_stuff,
3090d5a572a4SAlexey Dobriyan 				  tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
309128a6d671SEric W. Biederman }
309228a6d671SEric W. Biederman 
3093c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
309428a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
309528a6d671SEric W. Biederman 	.getattr	= pid_getattr,
309628a6d671SEric W. Biederman 	.setattr	= proc_setattr,
30970499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
309828a6d671SEric W. Biederman };
309928a6d671SEric W. Biederman 
310060347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
31011da177e4SLinus Torvalds {
310248e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
3103e3912ac3SAlexey Dobriyan 	char buf[10 + 1];
310448e6484dSEric W. Biederman 	struct qstr name;
31051da177e4SLinus Torvalds 
310648e6484dSEric W. Biederman 	name.name = buf;
3107e3912ac3SAlexey Dobriyan 	name.len = snprintf(buf, sizeof(buf), "%u", pid);
31084f522a24SAl Viro 	/* no ->d_hash() rejects on procfs */
310960347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
311048e6484dSEric W. Biederman 	if (dentry) {
3111bbd51924SEric W. Biederman 		d_invalidate(dentry);
311248e6484dSEric W. Biederman 		dput(dentry);
31131da177e4SLinus Torvalds 	}
31141da177e4SLinus Torvalds 
3115c35a7f18SOleg Nesterov 	if (pid == tgid)
3116c35a7f18SOleg Nesterov 		return;
3117c35a7f18SOleg Nesterov 
311848e6484dSEric W. Biederman 	name.name = buf;
3119e3912ac3SAlexey Dobriyan 	name.len = snprintf(buf, sizeof(buf), "%u", tgid);
312060347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
312148e6484dSEric W. Biederman 	if (!leader)
312248e6484dSEric W. Biederman 		goto out;
312348e6484dSEric W. Biederman 
312448e6484dSEric W. Biederman 	name.name = "task";
312548e6484dSEric W. Biederman 	name.len = strlen(name.name);
312648e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
312748e6484dSEric W. Biederman 	if (!dir)
312848e6484dSEric W. Biederman 		goto out_put_leader;
312948e6484dSEric W. Biederman 
313048e6484dSEric W. Biederman 	name.name = buf;
3131e3912ac3SAlexey Dobriyan 	name.len = snprintf(buf, sizeof(buf), "%u", pid);
313248e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
313348e6484dSEric W. Biederman 	if (dentry) {
3134bbd51924SEric W. Biederman 		d_invalidate(dentry);
313548e6484dSEric W. Biederman 		dput(dentry);
31361da177e4SLinus Torvalds 	}
313748e6484dSEric W. Biederman 
313848e6484dSEric W. Biederman 	dput(dir);
313948e6484dSEric W. Biederman out_put_leader:
314048e6484dSEric W. Biederman 	dput(leader);
314148e6484dSEric W. Biederman out:
314248e6484dSEric W. Biederman 	return;
31431da177e4SLinus Torvalds }
31441da177e4SLinus Torvalds 
31450895e91dSRandy Dunlap /**
31460895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
31470895e91dSRandy Dunlap  * @task: task that should be flushed.
31480895e91dSRandy Dunlap  *
31490895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
315060347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
31510895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
31520895e91dSRandy Dunlap  *
31530895e91dSRandy Dunlap  * Looks in the dcache for
31540895e91dSRandy Dunlap  * /proc/@pid
31550895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
31560895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
31570895e91dSRandy Dunlap  * from the dcache.
31580895e91dSRandy Dunlap  *
31590895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
31600895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
31610895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
31620895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
31630895e91dSRandy Dunlap  * dcache entries at process exit time.
31640895e91dSRandy Dunlap  *
31650895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
31660895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
31670895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
316860347f67SPavel Emelyanov  */
316960347f67SPavel Emelyanov 
317060347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
317160347f67SPavel Emelyanov {
31729fcc2d15SEric W. Biederman 	int i;
31739b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
3174130f77ecSPavel Emelyanov 	struct upid *upid;
3175130f77ecSPavel Emelyanov 
3176130f77ecSPavel Emelyanov 	pid = task_pid(task);
3177130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
31789fcc2d15SEric W. Biederman 
31799fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
3180130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
3181130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
31829b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
3183130f77ecSPavel Emelyanov 	}
318460347f67SPavel Emelyanov }
318560347f67SPavel Emelyanov 
31860168b9e3SAl Viro static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3187c5141e6dSEric Dumazet 				   struct task_struct *task, const void *ptr)
3188444ceed8SEric W. Biederman {
3189444ceed8SEric W. Biederman 	struct inode *inode;
3190444ceed8SEric W. Biederman 
31910168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3192444ceed8SEric W. Biederman 	if (!inode)
31930168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
3194444ceed8SEric W. Biederman 
3195444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
3196444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
3197444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3198aed54175SVegard Nossum 
31991270dd8dSAlexey Dobriyan 	set_nlink(inode, nlink_tgid);
32001bbc5513SAl Viro 	pid_update_inode(task, inode);
3201444ceed8SEric W. Biederman 
3202fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
32030168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
3204444ceed8SEric W. Biederman }
3205444ceed8SEric W. Biederman 
3206867aaccfSZhikang Zhang struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
32071da177e4SLinus Torvalds {
32081da177e4SLinus Torvalds 	struct task_struct *task;
32091da177e4SLinus Torvalds 	unsigned tgid;
3210b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32110168b9e3SAl Viro 	struct dentry *result = ERR_PTR(-ENOENT);
32121da177e4SLinus Torvalds 
3213dbcdb504SAlexey Dobriyan 	tgid = name_to_int(&dentry->d_name);
32141da177e4SLinus Torvalds 	if (tgid == ~0U)
32151da177e4SLinus Torvalds 		goto out;
32161da177e4SLinus Torvalds 
3217b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3218de758734SEric W. Biederman 	rcu_read_lock();
3219b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
32201da177e4SLinus Torvalds 	if (task)
32211da177e4SLinus Torvalds 		get_task_struct(task);
3222de758734SEric W. Biederman 	rcu_read_unlock();
32231da177e4SLinus Torvalds 	if (!task)
32241da177e4SLinus Torvalds 		goto out;
32251da177e4SLinus Torvalds 
32260168b9e3SAl Viro 	result = proc_pid_instantiate(dentry, task, NULL);
322748e6484dSEric W. Biederman 	put_task_struct(task);
32281da177e4SLinus Torvalds out:
32290168b9e3SAl Viro 	return result;
32301da177e4SLinus Torvalds }
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds /*
32330804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
32340bc58a91SEric W. Biederman  *
32351da177e4SLinus Torvalds  */
323619fd4bb2SEric W. Biederman struct tgid_iter {
323719fd4bb2SEric W. Biederman 	unsigned int tgid;
32380804ef4bSEric W. Biederman 	struct task_struct *task;
323919fd4bb2SEric W. Biederman };
324019fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
324119fd4bb2SEric W. Biederman {
32420804ef4bSEric W. Biederman 	struct pid *pid;
32431da177e4SLinus Torvalds 
324419fd4bb2SEric W. Biederman 	if (iter.task)
324519fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
32460804ef4bSEric W. Biederman 	rcu_read_lock();
32470804ef4bSEric W. Biederman retry:
324819fd4bb2SEric W. Biederman 	iter.task = NULL;
324919fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
32500804ef4bSEric W. Biederman 	if (pid) {
325119fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
325219fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
32530804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
32540804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
32550804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
32560804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
32570804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
32580804ef4bSEric W. Biederman 		 *
32590804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
32600804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
32610804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
32620804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
32630804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
32640bc58a91SEric W. Biederman 		 */
326519fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
326619fd4bb2SEric W. Biederman 			iter.tgid += 1;
32670804ef4bSEric W. Biederman 			goto retry;
326819fd4bb2SEric W. Biederman 		}
326919fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
32701da177e4SLinus Torvalds 	}
3271454cc105SEric W. Biederman 	rcu_read_unlock();
327219fd4bb2SEric W. Biederman 	return iter;
32731da177e4SLinus Torvalds }
32741da177e4SLinus Torvalds 
32750097875bSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
3278f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx)
32791da177e4SLinus Torvalds {
328019fd4bb2SEric W. Biederman 	struct tgid_iter iter;
328176f668beSChristoph Hellwig 	struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3282f0c3b509SAl Viro 	loff_t pos = ctx->pos;
32831da177e4SLinus Torvalds 
3284021ada7dSAl Viro 	if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3285f0c3b509SAl Viro 		return 0;
32861da177e4SLinus Torvalds 
32870097875bSEric W. Biederman 	if (pos == TGID_OFFSET - 2) {
32882b0143b5SDavid Howells 		struct inode *inode = d_inode(ns->proc_self);
3289db963164SAl Viro 		if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3290f0c3b509SAl Viro 			return 0;
32910097875bSEric W. Biederman 		ctx->pos = pos = pos + 1;
3292021ada7dSAl Viro 	}
32930097875bSEric W. Biederman 	if (pos == TGID_OFFSET - 1) {
32942b0143b5SDavid Howells 		struct inode *inode = d_inode(ns->proc_thread_self);
32950097875bSEric W. Biederman 		if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
32960097875bSEric W. Biederman 			return 0;
32970097875bSEric W. Biederman 		ctx->pos = pos = pos + 1;
32980097875bSEric W. Biederman 	}
32990097875bSEric W. Biederman 	iter.tgid = pos - TGID_OFFSET;
330019fd4bb2SEric W. Biederman 	iter.task = NULL;
330119fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
330219fd4bb2SEric W. Biederman 	     iter.task;
330319fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
3304e3912ac3SAlexey Dobriyan 		char name[10 + 1];
3305a4ef3895SAlexey Dobriyan 		unsigned int len;
33063ba4bceeSEric Dumazet 
33073ba4bceeSEric Dumazet 		cond_resched();
3308796f571bSLafcadio Wluiki 		if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3309f0c3b509SAl Viro 			continue;
33100499680aSVasiliy Kulikov 
3311e3912ac3SAlexey Dobriyan 		len = snprintf(name, sizeof(name), "%u", iter.tgid);
3312f0c3b509SAl Viro 		ctx->pos = iter.tgid + TGID_OFFSET;
3313f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
3314f0c3b509SAl Viro 				     proc_pid_instantiate, iter.task, NULL)) {
331519fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
3316f0c3b509SAl Viro 			return 0;
33171da177e4SLinus Torvalds 		}
33181da177e4SLinus Torvalds 	}
3319f0c3b509SAl Viro 	ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
33201da177e4SLinus Torvalds 	return 0;
33211da177e4SLinus Torvalds }
33221da177e4SLinus Torvalds 
33230bc58a91SEric W. Biederman /*
33241b3044e3SJanis Danisevskis  * proc_tid_comm_permission is a special permission function exclusively
33251b3044e3SJanis Danisevskis  * used for the node /proc/<pid>/task/<tid>/comm.
33261b3044e3SJanis Danisevskis  * It bypasses generic permission checks in the case where a task of the same
33271b3044e3SJanis Danisevskis  * task group attempts to access the node.
33281b3044e3SJanis Danisevskis  * The rationale behind this is that glibc and bionic access this node for
33291b3044e3SJanis Danisevskis  * cross thread naming (pthread_set/getname_np(!self)). However, if
33301b3044e3SJanis Danisevskis  * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
33311b3044e3SJanis Danisevskis  * which locks out the cross thread naming implementation.
33321b3044e3SJanis Danisevskis  * This function makes sure that the node is always accessible for members of
33331b3044e3SJanis Danisevskis  * same thread group.
33341b3044e3SJanis Danisevskis  */
33351b3044e3SJanis Danisevskis static int proc_tid_comm_permission(struct inode *inode, int mask)
33361b3044e3SJanis Danisevskis {
33371b3044e3SJanis Danisevskis 	bool is_same_tgroup;
33381b3044e3SJanis Danisevskis 	struct task_struct *task;
33391b3044e3SJanis Danisevskis 
33401b3044e3SJanis Danisevskis 	task = get_proc_task(inode);
33411b3044e3SJanis Danisevskis 	if (!task)
33421b3044e3SJanis Danisevskis 		return -ESRCH;
33431b3044e3SJanis Danisevskis 	is_same_tgroup = same_thread_group(current, task);
33441b3044e3SJanis Danisevskis 	put_task_struct(task);
33451b3044e3SJanis Danisevskis 
33461b3044e3SJanis Danisevskis 	if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
33471b3044e3SJanis Danisevskis 		/* This file (/proc/<pid>/task/<tid>/comm) can always be
33481b3044e3SJanis Danisevskis 		 * read or written by the members of the corresponding
33491b3044e3SJanis Danisevskis 		 * thread group.
33501b3044e3SJanis Danisevskis 		 */
33511b3044e3SJanis Danisevskis 		return 0;
33521b3044e3SJanis Danisevskis 	}
33531b3044e3SJanis Danisevskis 
33541b3044e3SJanis Danisevskis 	return generic_permission(inode, mask);
33551b3044e3SJanis Danisevskis }
33561b3044e3SJanis Danisevskis 
33571b3044e3SJanis Danisevskis static const struct inode_operations proc_tid_comm_inode_operations = {
33581b3044e3SJanis Danisevskis 		.permission = proc_tid_comm_permission,
33591b3044e3SJanis Danisevskis };
33601b3044e3SJanis Danisevskis 
33611b3044e3SJanis Danisevskis /*
336228a6d671SEric W. Biederman  * Tasks
336328a6d671SEric W. Biederman  */
3364c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3365631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
33663835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
33676b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
33686ba8ed79SEric W. Biederman #ifdef CONFIG_NET
33696ba8ed79SEric W. Biederman 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
33706ba8ed79SEric W. Biederman #endif
3371631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3372c5317167SAl Viro 	REG("auxv",      S_IRUSR, proc_auxv_operations),
3373631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
337435a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
33751c963eb1SAlexey Dobriyan 	ONE("limits",	 S_IRUGO, proc_pid_limits),
337643ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3377631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
337843ae34cbSIngo Molnar #endif
33791b3044e3SJanis Danisevskis 	NOD("comm",      S_IFREG|S_IRUGO|S_IWUSR,
33801b3044e3SJanis Danisevskis 			 &proc_tid_comm_inode_operations,
33811b3044e3SJanis Danisevskis 			 &proc_pid_set_comm_operations, {}),
3382ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
338309d93bd6SAlexey Dobriyan 	ONE("syscall",   S_IRUSR, proc_pid_syscall),
3384ebcb6734SRoland McGrath #endif
3385c2c0bb44SAlexey Dobriyan 	REG("cmdline",   S_IRUGO, proc_pid_cmdline_ops),
3386631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3387631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3388871305bbSVlastimil Babka 	REG("maps",      S_IRUGO, proc_pid_maps_operations),
33892e13ba54SIago López Galeiras #ifdef CONFIG_PROC_CHILDREN
339081841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
339181841161SCyrill Gorcunov #endif
339228a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3393871305bbSVlastimil Babka 	REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
339428a6d671SEric W. Biederman #endif
3395631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3396631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3397631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3398631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3399631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3400631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
34011e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3402631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3403871305bbSVlastimil Babka 	REG("smaps",     S_IRUGO, proc_pid_smaps_operations),
3404493b0e9dSDaniel Colascione 	REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
340532ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
340628a6d671SEric W. Biederman #endif
340728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3408631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
340928a6d671SEric W. Biederman #endif
341028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3411edfcd606SAlexey Dobriyan 	ONE("wchan",     S_IRUGO, proc_pid_wchan),
341228a6d671SEric W. Biederman #endif
34132ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
341435a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
341528a6d671SEric W. Biederman #endif
34165968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
3417f6e826caSAlexey Dobriyan 	ONE("schedstat", S_IRUGO, proc_pid_schedstat),
341828a6d671SEric W. Biederman #endif
34199745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3420631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
34219745512cSArjan van de Ven #endif
34228793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
342352de4779SZefan Li 	ONE("cpuset",    S_IRUGO, proc_cpuset_show),
342428a6d671SEric W. Biederman #endif
3425a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3426006f4ac4SZefan Li 	ONE("cgroup",  S_IRUGO, proc_cgroup_show),
3427a424316cSPaul Menage #endif
34286ba51e37SAlexey Dobriyan 	ONE("oom_score", S_IRUGO, proc_oom_score),
3429fa0cbbf1SDavid Rientjes 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3430a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
34314b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
3432631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
343326ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
343428a6d671SEric W. Biederman #endif
3435f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3436631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
34371203c8e6SAkinobu Mita 	REG("fail-nth", 0644, proc_fail_nth_operations),
3438f4f154fdSAkinobu Mita #endif
3439297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
344019aadc98SAlexey Dobriyan 	ONE("io",	S_IRUSR, proc_tid_io_accounting),
3441297c5d92SAndrea Righi #endif
344222d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
344322d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
344422d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
3445f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
34469cc46516SEric W. Biederman 	REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
344722d917d8SEric W. Biederman #endif
34487c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
34497c23b330SJosh Poimboeuf 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
34507c23b330SJosh Poimboeuf #endif
345128a6d671SEric W. Biederman };
345228a6d671SEric W. Biederman 
3453f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
345428a6d671SEric W. Biederman {
3455f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
345628a6d671SEric W. Biederman 				   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
345728a6d671SEric W. Biederman }
345828a6d671SEric W. Biederman 
345900cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
346000cd8dd3SAl Viro {
34617bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
3462d5a572a4SAlexey Dobriyan 				  tid_base_stuff,
3463d5a572a4SAlexey Dobriyan 				  tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
346428a6d671SEric W. Biederman }
346528a6d671SEric W. Biederman 
346600977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
346728a6d671SEric W. Biederman 	.read		= generic_read_dir,
3468f50752eaSAl Viro 	.iterate_shared	= proc_tid_base_readdir,
3469f50752eaSAl Viro 	.llseek		= generic_file_llseek,
347028a6d671SEric W. Biederman };
347128a6d671SEric W. Biederman 
3472c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
347328a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
347428a6d671SEric W. Biederman 	.getattr	= pid_getattr,
347528a6d671SEric W. Biederman 	.setattr	= proc_setattr,
347628a6d671SEric W. Biederman };
347728a6d671SEric W. Biederman 
34780168b9e3SAl Viro static struct dentry *proc_task_instantiate(struct dentry *dentry,
34790168b9e3SAl Viro 	struct task_struct *task, const void *ptr)
3480444ceed8SEric W. Biederman {
3481444ceed8SEric W. Biederman 	struct inode *inode;
34820168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3483444ceed8SEric W. Biederman 	if (!inode)
34840168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
34851bbc5513SAl Viro 
3486444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3487444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3488444ceed8SEric W. Biederman 	inode->i_flags |= S_IMMUTABLE;
3489aed54175SVegard Nossum 
34901270dd8dSAlexey Dobriyan 	set_nlink(inode, nlink_tid);
34911bbc5513SAl Viro 	pid_update_inode(task, inode);
3492444ceed8SEric W. Biederman 
3493fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
34940168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
3495444ceed8SEric W. Biederman }
3496444ceed8SEric W. Biederman 
349700cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
349828a6d671SEric W. Biederman {
349928a6d671SEric W. Biederman 	struct task_struct *task;
350028a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
350128a6d671SEric W. Biederman 	unsigned tid;
3502b488893aSPavel Emelyanov 	struct pid_namespace *ns;
35030168b9e3SAl Viro 	struct dentry *result = ERR_PTR(-ENOENT);
350428a6d671SEric W. Biederman 
350528a6d671SEric W. Biederman 	if (!leader)
350628a6d671SEric W. Biederman 		goto out_no_task;
350728a6d671SEric W. Biederman 
3508dbcdb504SAlexey Dobriyan 	tid = name_to_int(&dentry->d_name);
350928a6d671SEric W. Biederman 	if (tid == ~0U)
351028a6d671SEric W. Biederman 		goto out;
351128a6d671SEric W. Biederman 
3512b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
351328a6d671SEric W. Biederman 	rcu_read_lock();
3514b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
351528a6d671SEric W. Biederman 	if (task)
351628a6d671SEric W. Biederman 		get_task_struct(task);
351728a6d671SEric W. Biederman 	rcu_read_unlock();
351828a6d671SEric W. Biederman 	if (!task)
351928a6d671SEric W. Biederman 		goto out;
3520bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
352128a6d671SEric W. Biederman 		goto out_drop_task;
352228a6d671SEric W. Biederman 
35230168b9e3SAl Viro 	result = proc_task_instantiate(dentry, task, NULL);
352428a6d671SEric W. Biederman out_drop_task:
352528a6d671SEric W. Biederman 	put_task_struct(task);
352628a6d671SEric W. Biederman out:
352728a6d671SEric W. Biederman 	put_task_struct(leader);
352828a6d671SEric W. Biederman out_no_task:
35290168b9e3SAl Viro 	return result;
353028a6d671SEric W. Biederman }
353128a6d671SEric W. Biederman 
353228a6d671SEric W. Biederman /*
35330bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
35340bc58a91SEric W. Biederman  *
35350bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
35360bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
35370bc58a91SEric W. Biederman  * directory we have more work todo.
35380bc58a91SEric W. Biederman  *
35390bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
35400bc58a91SEric W. Biederman  *
35410bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
35420bc58a91SEric W. Biederman  * threads past it.
35430bc58a91SEric W. Biederman  */
35449f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
35459f6e963fSOleg Nesterov 					struct pid_namespace *ns)
35460bc58a91SEric W. Biederman {
3547d855a4b7SOleg Nesterov 	struct task_struct *pos, *task;
35489f6e963fSOleg Nesterov 	unsigned long nr = f_pos;
35499f6e963fSOleg Nesterov 
35509f6e963fSOleg Nesterov 	if (nr != f_pos)	/* 32bit overflow? */
35519f6e963fSOleg Nesterov 		return NULL;
35520bc58a91SEric W. Biederman 
3553cc288738SEric W. Biederman 	rcu_read_lock();
3554d855a4b7SOleg Nesterov 	task = pid_task(pid, PIDTYPE_PID);
3555d855a4b7SOleg Nesterov 	if (!task)
3556d855a4b7SOleg Nesterov 		goto fail;
3557d855a4b7SOleg Nesterov 
3558d855a4b7SOleg Nesterov 	/* Attempt to start with the tid of a thread */
35599f6e963fSOleg Nesterov 	if (tid && nr) {
3560b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3561d855a4b7SOleg Nesterov 		if (pos && same_thread_group(pos, task))
3562a872ff0cSOleg Nesterov 			goto found;
35630bc58a91SEric W. Biederman 	}
35640bc58a91SEric W. Biederman 
35650bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
35669f6e963fSOleg Nesterov 	if (nr >= get_nr_threads(task))
3567c986c14aSOleg Nesterov 		goto fail;
3568a872ff0cSOleg Nesterov 
3569a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3570a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3571a872ff0cSOleg Nesterov 	 */
3572d855a4b7SOleg Nesterov 	pos = task = task->group_leader;
3573c986c14aSOleg Nesterov 	do {
35749f6e963fSOleg Nesterov 		if (!nr--)
3575c986c14aSOleg Nesterov 			goto found;
3576d855a4b7SOleg Nesterov 	} while_each_thread(task, pos);
3577c986c14aSOleg Nesterov fail:
3578a872ff0cSOleg Nesterov 	pos = NULL;
3579a872ff0cSOleg Nesterov 	goto out;
3580a872ff0cSOleg Nesterov found:
3581a872ff0cSOleg Nesterov 	get_task_struct(pos);
3582a872ff0cSOleg Nesterov out:
3583cc288738SEric W. Biederman 	rcu_read_unlock();
35840bc58a91SEric W. Biederman 	return pos;
35850bc58a91SEric W. Biederman }
35860bc58a91SEric W. Biederman 
35870bc58a91SEric W. Biederman /*
35880bc58a91SEric W. Biederman  * Find the next thread in the thread list.
35890bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
35900bc58a91SEric W. Biederman  *
35910bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
35920bc58a91SEric W. Biederman  */
35930bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
35940bc58a91SEric W. Biederman {
3595c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3596cc288738SEric W. Biederman 	rcu_read_lock();
3597c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
35980bc58a91SEric W. Biederman 		pos = next_thread(start);
3599c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
36000bc58a91SEric W. Biederman 			pos = NULL;
3601c1df7fb8SOleg Nesterov 		else
3602c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3603c1df7fb8SOleg Nesterov 	}
3604cc288738SEric W. Biederman 	rcu_read_unlock();
36050bc58a91SEric W. Biederman 	put_task_struct(start);
36060bc58a91SEric W. Biederman 	return pos;
36070bc58a91SEric W. Biederman }
36080bc58a91SEric W. Biederman 
36091da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
3610f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx)
36111da177e4SLinus Torvalds {
3612d855a4b7SOleg Nesterov 	struct inode *inode = file_inode(file);
3613d855a4b7SOleg Nesterov 	struct task_struct *task;
3614b488893aSPavel Emelyanov 	struct pid_namespace *ns;
3615f0c3b509SAl Viro 	int tid;
36161da177e4SLinus Torvalds 
3617d855a4b7SOleg Nesterov 	if (proc_inode_is_dead(inode))
3618f0c3b509SAl Viro 		return -ENOENT;
36191da177e4SLinus Torvalds 
3620f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
3621d855a4b7SOleg Nesterov 		return 0;
36221da177e4SLinus Torvalds 
36230bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
36240bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
36250bc58a91SEric W. Biederman 	 */
362676f668beSChristoph Hellwig 	ns = proc_pid_ns(inode);
3627f0c3b509SAl Viro 	tid = (int)file->f_version;
3628f0c3b509SAl Viro 	file->f_version = 0;
3629d855a4b7SOleg Nesterov 	for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
36300bc58a91SEric W. Biederman 	     task;
3631f0c3b509SAl Viro 	     task = next_tid(task), ctx->pos++) {
3632e3912ac3SAlexey Dobriyan 		char name[10 + 1];
3633a4ef3895SAlexey Dobriyan 		unsigned int len;
3634b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
3635e3912ac3SAlexey Dobriyan 		len = snprintf(name, sizeof(name), "%u", tid);
3636f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
3637f0c3b509SAl Viro 				proc_task_instantiate, task, NULL)) {
36380bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
36390bc58a91SEric W. Biederman 			 * pid for the next readir call */
3640f0c3b509SAl Viro 			file->f_version = (u64)tid;
36410bc58a91SEric W. Biederman 			put_task_struct(task);
36421da177e4SLinus Torvalds 			break;
36430bc58a91SEric W. Biederman 		}
36441da177e4SLinus Torvalds 	}
3645d855a4b7SOleg Nesterov 
3646f0c3b509SAl Viro 	return 0;
36471da177e4SLinus Torvalds }
36486e66b52bSEric W. Biederman 
3649a528d35eSDavid Howells static int proc_task_getattr(const struct path *path, struct kstat *stat,
3650a528d35eSDavid Howells 			     u32 request_mask, unsigned int query_flags)
36516e66b52bSEric W. Biederman {
3652a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
365399f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
36546e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
36556e66b52bSEric W. Biederman 
365699f89551SEric W. Biederman 	if (p) {
365799f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
365899f89551SEric W. Biederman 		put_task_struct(p);
36596e66b52bSEric W. Biederman 	}
36606e66b52bSEric W. Biederman 
36616e66b52bSEric W. Biederman 	return 0;
36626e66b52bSEric W. Biederman }
366328a6d671SEric W. Biederman 
3664c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
366528a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
366628a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
366728a6d671SEric W. Biederman 	.setattr	= proc_setattr,
36680499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
366928a6d671SEric W. Biederman };
367028a6d671SEric W. Biederman 
367100977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
367228a6d671SEric W. Biederman 	.read		= generic_read_dir,
3673f50752eaSAl Viro 	.iterate_shared	= proc_task_readdir,
3674f50752eaSAl Viro 	.llseek		= generic_file_llseek,
367528a6d671SEric W. Biederman };
36761270dd8dSAlexey Dobriyan 
36771270dd8dSAlexey Dobriyan void __init set_proc_pid_nlink(void)
36781270dd8dSAlexey Dobriyan {
36791270dd8dSAlexey Dobriyan 	nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
36801270dd8dSAlexey Dobriyan 	nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
36811270dd8dSAlexey Dobriyan }
3682