xref: /openbmc/linux/fs/proc/base.c (revision 1c963eb135075ecc5126c1ef695090cb0aea95c4)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/proc/base.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992 Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  proc base directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
91da177e4SLinus Torvalds  *  Instead of using magical inumbers to determine the kind of object
101da177e4SLinus Torvalds  *  we allocate and fill in-core inodes upon lookup. They don't even
111da177e4SLinus Torvalds  *  go into icache. We cache the reference to task_struct upon lookup too.
121da177e4SLinus Torvalds  *  Eventually it should become a filesystem in its own. We don't use the
131da177e4SLinus Torvalds  *  rest of procfs anymore.
14e070ad49SMauricio Lin  *
15e070ad49SMauricio Lin  *
16e070ad49SMauricio Lin  *  Changelog:
17e070ad49SMauricio Lin  *  17-Jan-2005
18e070ad49SMauricio Lin  *  Allan Bezerra
19e070ad49SMauricio Lin  *  Bruna Moreira <bruna.moreira@indt.org.br>
20e070ad49SMauricio Lin  *  Edjard Mota <edjard.mota@indt.org.br>
21e070ad49SMauricio Lin  *  Ilias Biris <ilias.biris@indt.org.br>
22e070ad49SMauricio Lin  *  Mauricio Lin <mauricio.lin@indt.org.br>
23e070ad49SMauricio Lin  *
24e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25e070ad49SMauricio Lin  *
26e070ad49SMauricio Lin  *  A new process specific entry (smaps) included in /proc. It shows the
27e070ad49SMauricio Lin  *  size of rss for each memory area. The maps entry lacks information
28e070ad49SMauricio Lin  *  about physical memory size (rss) for each mapped file, i.e.,
29e070ad49SMauricio Lin  *  rss information for executables and library files.
30e070ad49SMauricio Lin  *  This additional information is useful for any tools that need to know
31e070ad49SMauricio Lin  *  about physical memory consumption for a process specific library.
32e070ad49SMauricio Lin  *
33e070ad49SMauricio Lin  *  Changelog:
34e070ad49SMauricio Lin  *  21-Feb-2005
35e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36e070ad49SMauricio Lin  *  Pud inclusion in the page table walking.
37e070ad49SMauricio Lin  *
38e070ad49SMauricio Lin  *  ChangeLog:
39e070ad49SMauricio Lin  *  10-Mar-2005
40e070ad49SMauricio Lin  *  10LE Instituto Nokia de Tecnologia - INdT:
41e070ad49SMauricio Lin  *  A better way to walks through the page table as suggested by Hugh Dickins.
42e070ad49SMauricio Lin  *
43e070ad49SMauricio Lin  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44e070ad49SMauricio Lin  *  Smaps information related to shared, private, clean and dirty pages.
45e070ad49SMauricio Lin  *
46e070ad49SMauricio Lin  *  Paul Mundt <paul.mundt@nokia.com>:
47e070ad49SMauricio Lin  *  Overall revision about smaps.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <asm/uaccess.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/errno.h>
531da177e4SLinus Torvalds #include <linux/time.h>
541da177e4SLinus Torvalds #include <linux/proc_fs.h>
551da177e4SLinus Torvalds #include <linux/stat.h>
565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h>
571da177e4SLinus Torvalds #include <linux/init.h>
5816f7e0feSRandy Dunlap #include <linux/capability.h>
591da177e4SLinus Torvalds #include <linux/file.h>
609f3acc31SAl Viro #include <linux/fdtable.h>
611da177e4SLinus Torvalds #include <linux/string.h>
621da177e4SLinus Torvalds #include <linux/seq_file.h>
631da177e4SLinus Torvalds #include <linux/namei.h>
646b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
651da177e4SLinus Torvalds #include <linux/mm.h>
66a63d83f4SDavid Rientjes #include <linux/swap.h>
67b835996fSDipankar Sarma #include <linux/rcupdate.h>
681da177e4SLinus Torvalds #include <linux/kallsyms.h>
692ec220e2SKen Chen #include <linux/stacktrace.h>
70d85f50d5SNeil Horman #include <linux/resource.h>
715096add8SKees Cook #include <linux/module.h>
721da177e4SLinus Torvalds #include <linux/mount.h>
731da177e4SLinus Torvalds #include <linux/security.h>
741da177e4SLinus Torvalds #include <linux/ptrace.h>
750d094efeSRoland McGrath #include <linux/tracehook.h>
7687ebdc00SAndrew Morton #include <linux/printk.h>
77a424316cSPaul Menage #include <linux/cgroup.h>
781da177e4SLinus Torvalds #include <linux/cpuset.h>
791da177e4SLinus Torvalds #include <linux/audit.h>
805addc5ddSAl Viro #include <linux/poll.h>
811651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
828ac773b4SAlexey Dobriyan #include <linux/oom.h>
833cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8460347f67SPavel Emelyanov #include <linux/pid_namespace.h>
8522d917d8SEric W. Biederman #include <linux/user_namespace.h>
865ad4e53bSAl Viro #include <linux/fs_struct.h>
875a0e3ad6STejun Heo #include <linux/slab.h>
88640708a2SPavel Emelyanov #include <linux/flex_array.h>
8948f6a7a5SPavel Emelyanov #include <linux/posix-timers.h>
90f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
91f133eccaSChris Metcalf #include <asm/hardwall.h>
92f133eccaSChris Metcalf #endif
9343d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
941da177e4SLinus Torvalds #include "internal.h"
95faf60af1SCyrill Gorcunov #include "fd.h"
961da177e4SLinus Torvalds 
970f2fe20fSEric W. Biederman /* NOTE:
980f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
990f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
1000f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
1010f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
1020f2fe20fSEric W. Biederman  *
1030f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
1040f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
1050f2fe20fSEric W. Biederman  */
1060f2fe20fSEric W. Biederman 
1071da177e4SLinus Torvalds struct pid_entry {
108cedbccabSAlexey Dobriyan 	const char *name;
109c5141e6dSEric Dumazet 	int len;
110d161a13fSAl Viro 	umode_t mode;
111c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
11200977a59SArjan van de Ven 	const struct file_operations *fop;
11320cdc894SEric W. Biederman 	union proc_op op;
1141da177e4SLinus Torvalds };
1151da177e4SLinus Torvalds 
11661a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
11720cdc894SEric W. Biederman 	.name = (NAME),					\
118c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11920cdc894SEric W. Biederman 	.mode = MODE,					\
12020cdc894SEric W. Biederman 	.iop  = IOP,					\
12120cdc894SEric W. Biederman 	.fop  = FOP,					\
12220cdc894SEric W. Biederman 	.op   = OP,					\
12320cdc894SEric W. Biederman }
12420cdc894SEric W. Biederman 
125631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
126631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
12861a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12920cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
130631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
131631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
132631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
13461a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
13520cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
136631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
137631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
138be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
139be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
140631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1411da177e4SLinus Torvalds 
142aed54175SVegard Nossum /*
143aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
144aed54175SVegard Nossum  * and .. links.
145aed54175SVegard Nossum  */
146aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147aed54175SVegard Nossum 	unsigned int n)
148aed54175SVegard Nossum {
149aed54175SVegard Nossum 	unsigned int i;
150aed54175SVegard Nossum 	unsigned int count;
151aed54175SVegard Nossum 
152aed54175SVegard Nossum 	count = 0;
153aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
154aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
155aed54175SVegard Nossum 			++count;
156aed54175SVegard Nossum 	}
157aed54175SVegard Nossum 
158aed54175SVegard Nossum 	return count;
159aed54175SVegard Nossum }
160aed54175SVegard Nossum 
161f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1621da177e4SLinus Torvalds {
1637c2c7d99SHugh Dickins 	int result = -ENOENT;
1647c2c7d99SHugh Dickins 
1650494f6ecSMiklos Szeredi 	task_lock(task);
166f7ad3c6bSMiklos Szeredi 	if (task->fs) {
167f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1687c2c7d99SHugh Dickins 		result = 0;
1697c2c7d99SHugh Dickins 	}
1700494f6ecSMiklos Szeredi 	task_unlock(task);
1717c2c7d99SHugh Dickins 	return result;
1720494f6ecSMiklos Szeredi }
1730494f6ecSMiklos Szeredi 
1747773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1750494f6ecSMiklos Szeredi {
1767773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1770494f6ecSMiklos Szeredi 	int result = -ENOENT;
17899f89551SEric W. Biederman 
17999f89551SEric W. Biederman 	if (task) {
180f7ad3c6bSMiklos Szeredi 		task_lock(task);
181f7ad3c6bSMiklos Szeredi 		if (task->fs) {
182f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
183f7ad3c6bSMiklos Szeredi 			result = 0;
184f7ad3c6bSMiklos Szeredi 		}
185f7ad3c6bSMiklos Szeredi 		task_unlock(task);
18699f89551SEric W. Biederman 		put_task_struct(task);
18799f89551SEric W. Biederman 	}
1881da177e4SLinus Torvalds 	return result;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1917773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
1921da177e4SLinus Torvalds {
1937773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1941da177e4SLinus Torvalds 	int result = -ENOENT;
19599f89551SEric W. Biederman 
19699f89551SEric W. Biederman 	if (task) {
197f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
19899f89551SEric W. Biederman 		put_task_struct(task);
19999f89551SEric W. Biederman 	}
2001da177e4SLinus Torvalds 	return result;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char *buffer)
2041da177e4SLinus Torvalds {
20521a6457aSWilliam Roberts 	return get_cmdline(task, buffer, PAGE_SIZE);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
208f9ea536eSAlexey Dobriyan static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
209f9ea536eSAlexey Dobriyan 			 struct pid *pid, struct task_struct *task)
2101da177e4SLinus Torvalds {
211e7dcd999SCong Wang 	struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
2122fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
2131da177e4SLinus Torvalds 		unsigned int nwords = 0;
214dfe6b7d9SHannes Eder 		do {
2151da177e4SLinus Torvalds 			nwords += 2;
216dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
217f9ea536eSAlexey Dobriyan 		seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
2181da177e4SLinus Torvalds 		mmput(mm);
219f9ea536eSAlexey Dobriyan 		return 0;
220f9ea536eSAlexey Dobriyan 	} else
221f9ea536eSAlexey Dobriyan 		return PTR_ERR(mm);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2261da177e4SLinus Torvalds /*
2271da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2281da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2291da177e4SLinus Torvalds  */
2301da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
2311da177e4SLinus Torvalds {
232ffb45122SAlexey Dobriyan 	unsigned long wchan;
2339281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	wchan = get_wchan(task);
2361da177e4SLinus Torvalds 
2379d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
238f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
239f83ce3e6SJake Edge 			return 0;
240f83ce3e6SJake Edge 		else
2411da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
2429d65cb4aSAlexey Dobriyan 	else
2439d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2461da177e4SLinus Torvalds 
247a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
248a9712bc1SAl Viro {
249a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
250a9712bc1SAl Viro 	if (err)
251a9712bc1SAl Viro 		return err;
252a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
253a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
254a9712bc1SAl Viro 		return -EPERM;
255a9712bc1SAl Viro 	}
256a9712bc1SAl Viro 	return 0;
257a9712bc1SAl Viro }
258a9712bc1SAl Viro 
259a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
260a9712bc1SAl Viro {
261a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
262a9712bc1SAl Viro }
263a9712bc1SAl Viro 
2642ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
2652ec220e2SKen Chen 
2662ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
2672ec220e2SKen Chen 
2682ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
2692ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
2702ec220e2SKen Chen {
2712ec220e2SKen Chen 	struct stack_trace trace;
2722ec220e2SKen Chen 	unsigned long *entries;
273a9712bc1SAl Viro 	int err;
2742ec220e2SKen Chen 	int i;
2752ec220e2SKen Chen 
2762ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
2772ec220e2SKen Chen 	if (!entries)
2782ec220e2SKen Chen 		return -ENOMEM;
2792ec220e2SKen Chen 
2802ec220e2SKen Chen 	trace.nr_entries	= 0;
2812ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
2822ec220e2SKen Chen 	trace.entries		= entries;
2832ec220e2SKen Chen 	trace.skip		= 0;
284a9712bc1SAl Viro 
285a9712bc1SAl Viro 	err = lock_trace(task);
286a9712bc1SAl Viro 	if (!err) {
2872ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
2882ec220e2SKen Chen 
2892ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
29051e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
2912ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
2922ec220e2SKen Chen 		}
293a9712bc1SAl Viro 		unlock_trace(task);
294a9712bc1SAl Viro 	}
2952ec220e2SKen Chen 	kfree(entries);
2962ec220e2SKen Chen 
297a9712bc1SAl Viro 	return err;
2982ec220e2SKen Chen }
2992ec220e2SKen Chen #endif
3002ec220e2SKen Chen 
3011da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3021da177e4SLinus Torvalds /*
3031da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3041da177e4SLinus Torvalds  */
3051da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3061da177e4SLinus Torvalds {
307172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
308826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
309826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3102d72376bSIngo Molnar 			task->sched_info.pcount);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds #endif
3131da177e4SLinus Torvalds 
3149745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3159745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3169745512cSArjan van de Ven {
3179745512cSArjan van de Ven 	int i;
31813d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
31913d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3209745512cSArjan van de Ven 
32113d77c37SHiroshi Shimamoto 	if (!task)
32213d77c37SHiroshi Shimamoto 		return -ESRCH;
32313d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3249745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
32534e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
32634e49d4fSJoe Perches 		if (lr->backtrace[0]) {
3279745512cSArjan van de Ven 			int q;
3289745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
32934e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
3309745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
33134e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
33234e49d4fSJoe Perches 				if (!bt)
3339745512cSArjan van de Ven 					break;
33434e49d4fSJoe Perches 				if (bt == ULONG_MAX)
3359745512cSArjan van de Ven 					break;
33634e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
3379745512cSArjan van de Ven 			}
3389d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
3399745512cSArjan van de Ven 		}
3409745512cSArjan van de Ven 
3419745512cSArjan van de Ven 	}
34213d77c37SHiroshi Shimamoto 	put_task_struct(task);
3439745512cSArjan van de Ven 	return 0;
3449745512cSArjan van de Ven }
3459745512cSArjan van de Ven 
3469745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3479745512cSArjan van de Ven {
34813d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
349d6643d12SHiroshi Shimamoto }
350d6643d12SHiroshi Shimamoto 
3519745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3529745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3539745512cSArjan van de Ven {
354496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
3559745512cSArjan van de Ven 
35613d77c37SHiroshi Shimamoto 	if (!task)
35713d77c37SHiroshi Shimamoto 		return -ESRCH;
3589745512cSArjan van de Ven 	clear_all_latency_tracing(task);
35913d77c37SHiroshi Shimamoto 	put_task_struct(task);
3609745512cSArjan van de Ven 
3619745512cSArjan van de Ven 	return count;
3629745512cSArjan van de Ven }
3639745512cSArjan van de Ven 
3649745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3659745512cSArjan van de Ven 	.open		= lstats_open,
3669745512cSArjan van de Ven 	.read		= seq_read,
3679745512cSArjan van de Ven 	.write		= lstats_write,
3689745512cSArjan van de Ven 	.llseek		= seq_lseek,
36913d77c37SHiroshi Shimamoto 	.release	= single_release,
3709745512cSArjan van de Ven };
3719745512cSArjan van de Ven 
3729745512cSArjan van de Ven #endif
3739745512cSArjan van de Ven 
3748d8b97baSAl Viro #ifdef CONFIG_CGROUPS
3758d8b97baSAl Viro static int cgroup_open(struct inode *inode, struct file *file)
3768d8b97baSAl Viro {
3778d8b97baSAl Viro 	struct pid *pid = PROC_I(inode)->pid;
3788d8b97baSAl Viro 	return single_open(file, proc_cgroup_show, pid);
3798d8b97baSAl Viro }
3808d8b97baSAl Viro 
3818d8b97baSAl Viro static const struct file_operations proc_cgroup_operations = {
3828d8b97baSAl Viro 	.open		= cgroup_open,
3838d8b97baSAl Viro 	.read		= seq_read,
3848d8b97baSAl Viro 	.llseek		= seq_lseek,
3858d8b97baSAl Viro 	.release	= single_release,
3868d8b97baSAl Viro };
3878d8b97baSAl Viro #endif
3888d8b97baSAl Viro 
3898d8b97baSAl Viro #ifdef CONFIG_PROC_PID_CPUSET
3908d8b97baSAl Viro 
3918d8b97baSAl Viro static int cpuset_open(struct inode *inode, struct file *file)
3928d8b97baSAl Viro {
3938d8b97baSAl Viro 	struct pid *pid = PROC_I(inode)->pid;
3948d8b97baSAl Viro 	return single_open(file, proc_cpuset_show, pid);
3958d8b97baSAl Viro }
3968d8b97baSAl Viro 
3978d8b97baSAl Viro static const struct file_operations proc_cpuset_operations = {
3988d8b97baSAl Viro 	.open		= cpuset_open,
3998d8b97baSAl Viro 	.read		= seq_read,
4008d8b97baSAl Viro 	.llseek		= seq_lseek,
4018d8b97baSAl Viro 	.release	= single_release,
4028d8b97baSAl Viro };
4038d8b97baSAl Viro #endif
4048d8b97baSAl Viro 
4051da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4061da177e4SLinus Torvalds {
407a7f638f9SDavid Rientjes 	unsigned long totalpages = totalram_pages + total_swap_pages;
408b95c35e7SOleg Nesterov 	unsigned long points = 0;
4091da177e4SLinus Torvalds 
41019c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
411b95c35e7SOleg Nesterov 	if (pid_alive(task))
412a7f638f9SDavid Rientjes 		points = oom_badness(task, NULL, NULL, totalpages) *
413a7f638f9SDavid Rientjes 						1000 / totalpages;
41419c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4151da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
418d85f50d5SNeil Horman struct limit_names {
419cedbccabSAlexey Dobriyan 	const char *name;
420cedbccabSAlexey Dobriyan 	const char *unit;
421d85f50d5SNeil Horman };
422d85f50d5SNeil Horman 
423d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
424cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
425d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
426d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
427d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
428d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
429d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
430d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
431d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
432d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
433d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
434d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
435d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
436d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
437d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
438d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4398808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
440d85f50d5SNeil Horman };
441d85f50d5SNeil Horman 
442d85f50d5SNeil Horman /* Display limits for a process */
443*1c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
444*1c963eb1SAlexey Dobriyan 			   struct pid *pid, struct task_struct *task)
445d85f50d5SNeil Horman {
446d85f50d5SNeil Horman 	unsigned int i;
447d85f50d5SNeil Horman 	unsigned long flags;
448d85f50d5SNeil Horman 
449d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
450d85f50d5SNeil Horman 
451a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
452d85f50d5SNeil Horman 		return 0;
453d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
454d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
455d85f50d5SNeil Horman 
456d85f50d5SNeil Horman 	/*
457d85f50d5SNeil Horman 	 * print the file header
458d85f50d5SNeil Horman 	 */
459*1c963eb1SAlexey Dobriyan        seq_printf(m, "%-25s %-20s %-20s %-10s\n",
460d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
461d85f50d5SNeil Horman 
462d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
463d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
464*1c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20s ",
465d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
466d85f50d5SNeil Horman 		else
467*1c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20lu ",
468d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
469d85f50d5SNeil Horman 
470d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
471*1c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20s ", "unlimited");
472d85f50d5SNeil Horman 		else
473*1c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20lu ", rlim[i].rlim_max);
474d85f50d5SNeil Horman 
475d85f50d5SNeil Horman 		if (lnames[i].unit)
476*1c963eb1SAlexey Dobriyan 			seq_printf(m, "%-10s\n", lnames[i].unit);
477d85f50d5SNeil Horman 		else
478*1c963eb1SAlexey Dobriyan 			seq_putc(m, '\n');
479d85f50d5SNeil Horman 	}
480d85f50d5SNeil Horman 
481*1c963eb1SAlexey Dobriyan 	return 0;
482d85f50d5SNeil Horman }
483d85f50d5SNeil Horman 
484ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
485ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
486ebcb6734SRoland McGrath {
487ebcb6734SRoland McGrath 	long nr;
488ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
489a9712bc1SAl Viro 	int res = lock_trace(task);
490a9712bc1SAl Viro 	if (res)
491a9712bc1SAl Viro 		return res;
492ebcb6734SRoland McGrath 
493ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
494a9712bc1SAl Viro 		res = sprintf(buffer, "running\n");
495a9712bc1SAl Viro 	else if (nr < 0)
496a9712bc1SAl Viro 		res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
497a9712bc1SAl Viro 	else
498a9712bc1SAl Viro 		res = sprintf(buffer,
499ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
500ebcb6734SRoland McGrath 		       nr,
501ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
502ebcb6734SRoland McGrath 		       sp, pc);
503a9712bc1SAl Viro 	unlock_trace(task);
504a9712bc1SAl Viro 	return res;
505ebcb6734SRoland McGrath }
506ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
507ebcb6734SRoland McGrath 
5081da177e4SLinus Torvalds /************************************************************************/
5091da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5101da177e4SLinus Torvalds /************************************************************************/
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds /* permission checks */
513778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5141da177e4SLinus Torvalds {
515778c1144SEric W. Biederman 	struct task_struct *task;
516778c1144SEric W. Biederman 	int allowed = 0;
517df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
518df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
519df26c40eSEric W. Biederman 	 * information.
520778c1144SEric W. Biederman 	 */
521778c1144SEric W. Biederman 	task = get_proc_task(inode);
522df26c40eSEric W. Biederman 	if (task) {
523006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
524778c1144SEric W. Biederman 		put_task_struct(task);
525df26c40eSEric W. Biederman 	}
526778c1144SEric W. Biederman 	return allowed;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5296b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
5306d76fa58SLinus Torvalds {
5316d76fa58SLinus Torvalds 	int error;
5326d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5336d76fa58SLinus Torvalds 
5346d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5356d76fa58SLinus Torvalds 		return -EPERM;
5366d76fa58SLinus Torvalds 
5376d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5381025774cSChristoph Hellwig 	if (error)
5396d76fa58SLinus Torvalds 		return error;
5401025774cSChristoph Hellwig 
5411025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5421025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5431025774cSChristoph Hellwig 	return 0;
5446d76fa58SLinus Torvalds }
5456d76fa58SLinus Torvalds 
5460499680aSVasiliy Kulikov /*
5470499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
5480499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
5490499680aSVasiliy Kulikov  */
5500499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
5510499680aSVasiliy Kulikov 				 struct task_struct *task,
5520499680aSVasiliy Kulikov 				 int hide_pid_min)
5530499680aSVasiliy Kulikov {
5540499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
5550499680aSVasiliy Kulikov 		return true;
5560499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
5570499680aSVasiliy Kulikov 		return true;
5580499680aSVasiliy Kulikov 	return ptrace_may_access(task, PTRACE_MODE_READ);
5590499680aSVasiliy Kulikov }
5600499680aSVasiliy Kulikov 
5610499680aSVasiliy Kulikov 
5620499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
5630499680aSVasiliy Kulikov {
5640499680aSVasiliy Kulikov 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
5650499680aSVasiliy Kulikov 	struct task_struct *task;
5660499680aSVasiliy Kulikov 	bool has_perms;
5670499680aSVasiliy Kulikov 
5680499680aSVasiliy Kulikov 	task = get_proc_task(inode);
569a2ef990aSXiaotian Feng 	if (!task)
570a2ef990aSXiaotian Feng 		return -ESRCH;
5710499680aSVasiliy Kulikov 	has_perms = has_pid_permissions(pid, task, 1);
5720499680aSVasiliy Kulikov 	put_task_struct(task);
5730499680aSVasiliy Kulikov 
5740499680aSVasiliy Kulikov 	if (!has_perms) {
5750499680aSVasiliy Kulikov 		if (pid->hide_pid == 2) {
5760499680aSVasiliy Kulikov 			/*
5770499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
5780499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
5790499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
5800499680aSVasiliy Kulikov 			 * in procfs at all.
5810499680aSVasiliy Kulikov 			 */
5820499680aSVasiliy Kulikov 			return -ENOENT;
5830499680aSVasiliy Kulikov 		}
5840499680aSVasiliy Kulikov 
5850499680aSVasiliy Kulikov 		return -EPERM;
5860499680aSVasiliy Kulikov 	}
5870499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
5880499680aSVasiliy Kulikov }
5890499680aSVasiliy Kulikov 
5900499680aSVasiliy Kulikov 
5910499680aSVasiliy Kulikov 
592c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5936d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5946d76fa58SLinus Torvalds };
5956d76fa58SLinus Torvalds 
5961da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
5991da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6001da177e4SLinus Torvalds {
601496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
6021da177e4SLinus Torvalds 	unsigned long page;
6031da177e4SLinus Torvalds 	ssize_t length;
60499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
60599f89551SEric W. Biederman 
60699f89551SEric W. Biederman 	length = -ESRCH;
60799f89551SEric W. Biederman 	if (!task)
60899f89551SEric W. Biederman 		goto out_no_task;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6111da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
61299f89551SEric W. Biederman 
61399f89551SEric W. Biederman 	length = -ENOMEM;
614e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
61599f89551SEric W. Biederman 		goto out;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	if (length >= 0)
6201da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6211da177e4SLinus Torvalds 	free_page(page);
62299f89551SEric W. Biederman out:
62399f89551SEric W. Biederman 	put_task_struct(task);
62499f89551SEric W. Biederman out_no_task:
6251da177e4SLinus Torvalds 	return length;
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
62800977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6291da177e4SLinus Torvalds 	.read		= proc_info_read,
63087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
6311da177e4SLinus Torvalds };
6321da177e4SLinus Torvalds 
633be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
634be614086SEric W. Biederman {
635be614086SEric W. Biederman 	struct inode *inode = m->private;
636be614086SEric W. Biederman 	struct pid_namespace *ns;
637be614086SEric W. Biederman 	struct pid *pid;
638be614086SEric W. Biederman 	struct task_struct *task;
639be614086SEric W. Biederman 	int ret;
640be614086SEric W. Biederman 
641be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
642be614086SEric W. Biederman 	pid = proc_pid(inode);
643be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
644be614086SEric W. Biederman 	if (!task)
645be614086SEric W. Biederman 		return -ESRCH;
646be614086SEric W. Biederman 
647be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
648be614086SEric W. Biederman 
649be614086SEric W. Biederman 	put_task_struct(task);
650be614086SEric W. Biederman 	return ret;
651be614086SEric W. Biederman }
652be614086SEric W. Biederman 
653be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
654be614086SEric W. Biederman {
655c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
656be614086SEric W. Biederman }
657be614086SEric W. Biederman 
658be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
659be614086SEric W. Biederman 	.open		= proc_single_open,
660be614086SEric W. Biederman 	.read		= seq_read,
661be614086SEric W. Biederman 	.llseek		= seq_lseek,
662be614086SEric W. Biederman 	.release	= single_release,
663be614086SEric W. Biederman };
664be614086SEric W. Biederman 
665b409e578SCong Wang static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
6661da177e4SLinus Torvalds {
667496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
668e268337dSLinus Torvalds 	struct mm_struct *mm;
669e268337dSLinus Torvalds 
670e268337dSLinus Torvalds 	if (!task)
671e268337dSLinus Torvalds 		return -ESRCH;
672e268337dSLinus Torvalds 
673b409e578SCong Wang 	mm = mm_access(task, mode);
674e268337dSLinus Torvalds 	put_task_struct(task);
675e268337dSLinus Torvalds 
676e268337dSLinus Torvalds 	if (IS_ERR(mm))
677e268337dSLinus Torvalds 		return PTR_ERR(mm);
678e268337dSLinus Torvalds 
6796d08f2c7SOleg Nesterov 	if (mm) {
6806d08f2c7SOleg Nesterov 		/* ensure this mm_struct can't be freed */
6816d08f2c7SOleg Nesterov 		atomic_inc(&mm->mm_count);
6826d08f2c7SOleg Nesterov 		/* but do not pin its memory */
6836d08f2c7SOleg Nesterov 		mmput(mm);
6846d08f2c7SOleg Nesterov 	}
6856d08f2c7SOleg Nesterov 
686e268337dSLinus Torvalds 	file->private_data = mm;
687e268337dSLinus Torvalds 
6881da177e4SLinus Torvalds 	return 0;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
691b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
692b409e578SCong Wang {
693bc452b4bSDjalal Harouni 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
694bc452b4bSDjalal Harouni 
695bc452b4bSDjalal Harouni 	/* OK to pass negative loff_t, we can catch out-of-range */
696bc452b4bSDjalal Harouni 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
697bc452b4bSDjalal Harouni 
698bc452b4bSDjalal Harouni 	return ret;
699b409e578SCong Wang }
700b409e578SCong Wang 
701572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
702572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
7031da177e4SLinus Torvalds {
704e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
705572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
706572d34b9SOleg Nesterov 	ssize_t copied;
7071da177e4SLinus Torvalds 	char *page;
7081da177e4SLinus Torvalds 
709e268337dSLinus Torvalds 	if (!mm)
710e268337dSLinus Torvalds 		return 0;
7111da177e4SLinus Torvalds 
712e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7131da177e4SLinus Torvalds 	if (!page)
714e268337dSLinus Torvalds 		return -ENOMEM;
7151da177e4SLinus Torvalds 
716f7ca54f4SFrederik Deweerdt 	copied = 0;
7176d08f2c7SOleg Nesterov 	if (!atomic_inc_not_zero(&mm->mm_users))
7186d08f2c7SOleg Nesterov 		goto free;
7196d08f2c7SOleg Nesterov 
7201da177e4SLinus Torvalds 	while (count > 0) {
721572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
7221da177e4SLinus Torvalds 
723572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
7241da177e4SLinus Torvalds 			copied = -EFAULT;
7251da177e4SLinus Torvalds 			break;
7261da177e4SLinus Torvalds 		}
727572d34b9SOleg Nesterov 
728572d34b9SOleg Nesterov 		this_len = access_remote_vm(mm, addr, page, this_len, write);
729572d34b9SOleg Nesterov 		if (!this_len) {
7301da177e4SLinus Torvalds 			if (!copied)
7311da177e4SLinus Torvalds 				copied = -EIO;
7321da177e4SLinus Torvalds 			break;
7331da177e4SLinus Torvalds 		}
734572d34b9SOleg Nesterov 
735572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
736572d34b9SOleg Nesterov 			copied = -EFAULT;
737572d34b9SOleg Nesterov 			break;
7381da177e4SLinus Torvalds 		}
739572d34b9SOleg Nesterov 
740572d34b9SOleg Nesterov 		buf += this_len;
741572d34b9SOleg Nesterov 		addr += this_len;
742572d34b9SOleg Nesterov 		copied += this_len;
743572d34b9SOleg Nesterov 		count -= this_len;
744572d34b9SOleg Nesterov 	}
745572d34b9SOleg Nesterov 	*ppos = addr;
74630cd8903SKOSAKI Motohiro 
7476d08f2c7SOleg Nesterov 	mmput(mm);
7486d08f2c7SOleg Nesterov free:
74930cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
7501da177e4SLinus Torvalds 	return copied;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
753572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
754572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
755572d34b9SOleg Nesterov {
756572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
757572d34b9SOleg Nesterov }
758572d34b9SOleg Nesterov 
759572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
760572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
761572d34b9SOleg Nesterov {
762572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
763572d34b9SOleg Nesterov }
764572d34b9SOleg Nesterov 
76585863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	switch (orig) {
7681da177e4SLinus Torvalds 	case 0:
7691da177e4SLinus Torvalds 		file->f_pos = offset;
7701da177e4SLinus Torvalds 		break;
7711da177e4SLinus Torvalds 	case 1:
7721da177e4SLinus Torvalds 		file->f_pos += offset;
7731da177e4SLinus Torvalds 		break;
7741da177e4SLinus Torvalds 	default:
7751da177e4SLinus Torvalds 		return -EINVAL;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 	force_successful_syscall_return();
7781da177e4SLinus Torvalds 	return file->f_pos;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
781e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
782e268337dSLinus Torvalds {
783e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
78471879d3cSOleg Nesterov 	if (mm)
7856d08f2c7SOleg Nesterov 		mmdrop(mm);
786e268337dSLinus Torvalds 	return 0;
787e268337dSLinus Torvalds }
788e268337dSLinus Torvalds 
78900977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
7901da177e4SLinus Torvalds 	.llseek		= mem_lseek,
7911da177e4SLinus Torvalds 	.read		= mem_read,
7921da177e4SLinus Torvalds 	.write		= mem_write,
7931da177e4SLinus Torvalds 	.open		= mem_open,
794e268337dSLinus Torvalds 	.release	= mem_release,
7951da177e4SLinus Torvalds };
7961da177e4SLinus Torvalds 
797b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
798b409e578SCong Wang {
799b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
800b409e578SCong Wang }
801b409e578SCong Wang 
802315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
803315e28c8SJames Pearson 			size_t count, loff_t *ppos)
804315e28c8SJames Pearson {
805315e28c8SJames Pearson 	char *page;
806315e28c8SJames Pearson 	unsigned long src = *ppos;
807b409e578SCong Wang 	int ret = 0;
808b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
809315e28c8SJames Pearson 
810b409e578SCong Wang 	if (!mm)
811b409e578SCong Wang 		return 0;
812315e28c8SJames Pearson 
813315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
814315e28c8SJames Pearson 	if (!page)
815b409e578SCong Wang 		return -ENOMEM;
816315e28c8SJames Pearson 
817d6f64b89SAl Viro 	ret = 0;
818b409e578SCong Wang 	if (!atomic_inc_not_zero(&mm->mm_users))
819b409e578SCong Wang 		goto free;
820315e28c8SJames Pearson 	while (count > 0) {
821e8905ec2SDjalal Harouni 		size_t this_len, max_len;
822e8905ec2SDjalal Harouni 		int retval;
823e8905ec2SDjalal Harouni 
824e8905ec2SDjalal Harouni 		if (src >= (mm->env_end - mm->env_start))
825e8905ec2SDjalal Harouni 			break;
826315e28c8SJames Pearson 
827315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
828315e28c8SJames Pearson 
829e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
830e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
831315e28c8SJames Pearson 
832b409e578SCong Wang 		retval = access_remote_vm(mm, (mm->env_start + src),
833315e28c8SJames Pearson 			page, this_len, 0);
834315e28c8SJames Pearson 
835315e28c8SJames Pearson 		if (retval <= 0) {
836315e28c8SJames Pearson 			ret = retval;
837315e28c8SJames Pearson 			break;
838315e28c8SJames Pearson 		}
839315e28c8SJames Pearson 
840315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
841315e28c8SJames Pearson 			ret = -EFAULT;
842315e28c8SJames Pearson 			break;
843315e28c8SJames Pearson 		}
844315e28c8SJames Pearson 
845315e28c8SJames Pearson 		ret += retval;
846315e28c8SJames Pearson 		src += retval;
847315e28c8SJames Pearson 		buf += retval;
848315e28c8SJames Pearson 		count -= retval;
849315e28c8SJames Pearson 	}
850315e28c8SJames Pearson 	*ppos = src;
851315e28c8SJames Pearson 	mmput(mm);
852b409e578SCong Wang 
853b409e578SCong Wang free:
854315e28c8SJames Pearson 	free_page((unsigned long) page);
855315e28c8SJames Pearson 	return ret;
856315e28c8SJames Pearson }
857315e28c8SJames Pearson 
858315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
859b409e578SCong Wang 	.open		= environ_open,
860315e28c8SJames Pearson 	.read		= environ_read,
86187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
862b409e578SCong Wang 	.release	= mem_release,
863315e28c8SJames Pearson };
864315e28c8SJames Pearson 
865fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
866fa0cbbf1SDavid Rientjes 			    loff_t *ppos)
867fa0cbbf1SDavid Rientjes {
868496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
869fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
870fa0cbbf1SDavid Rientjes 	int oom_adj = OOM_ADJUST_MIN;
871fa0cbbf1SDavid Rientjes 	size_t len;
872fa0cbbf1SDavid Rientjes 	unsigned long flags;
873fa0cbbf1SDavid Rientjes 
874fa0cbbf1SDavid Rientjes 	if (!task)
875fa0cbbf1SDavid Rientjes 		return -ESRCH;
876fa0cbbf1SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
877fa0cbbf1SDavid Rientjes 		if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
878fa0cbbf1SDavid Rientjes 			oom_adj = OOM_ADJUST_MAX;
879fa0cbbf1SDavid Rientjes 		else
880fa0cbbf1SDavid Rientjes 			oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
881fa0cbbf1SDavid Rientjes 				  OOM_SCORE_ADJ_MAX;
882fa0cbbf1SDavid Rientjes 		unlock_task_sighand(task, &flags);
883fa0cbbf1SDavid Rientjes 	}
884fa0cbbf1SDavid Rientjes 	put_task_struct(task);
885fa0cbbf1SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
886fa0cbbf1SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
887fa0cbbf1SDavid Rientjes }
888fa0cbbf1SDavid Rientjes 
889fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf,
890fa0cbbf1SDavid Rientjes 			     size_t count, loff_t *ppos)
891fa0cbbf1SDavid Rientjes {
892fa0cbbf1SDavid Rientjes 	struct task_struct *task;
893fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
894fa0cbbf1SDavid Rientjes 	int oom_adj;
895fa0cbbf1SDavid Rientjes 	unsigned long flags;
896fa0cbbf1SDavid Rientjes 	int err;
897fa0cbbf1SDavid Rientjes 
898fa0cbbf1SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
899fa0cbbf1SDavid Rientjes 	if (count > sizeof(buffer) - 1)
900fa0cbbf1SDavid Rientjes 		count = sizeof(buffer) - 1;
901fa0cbbf1SDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
902fa0cbbf1SDavid Rientjes 		err = -EFAULT;
903fa0cbbf1SDavid Rientjes 		goto out;
904fa0cbbf1SDavid Rientjes 	}
905fa0cbbf1SDavid Rientjes 
906fa0cbbf1SDavid Rientjes 	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
907fa0cbbf1SDavid Rientjes 	if (err)
908fa0cbbf1SDavid Rientjes 		goto out;
909fa0cbbf1SDavid Rientjes 	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
910fa0cbbf1SDavid Rientjes 	     oom_adj != OOM_DISABLE) {
911fa0cbbf1SDavid Rientjes 		err = -EINVAL;
912fa0cbbf1SDavid Rientjes 		goto out;
913fa0cbbf1SDavid Rientjes 	}
914fa0cbbf1SDavid Rientjes 
915496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
916fa0cbbf1SDavid Rientjes 	if (!task) {
917fa0cbbf1SDavid Rientjes 		err = -ESRCH;
918fa0cbbf1SDavid Rientjes 		goto out;
919fa0cbbf1SDavid Rientjes 	}
920fa0cbbf1SDavid Rientjes 
921fa0cbbf1SDavid Rientjes 	task_lock(task);
922fa0cbbf1SDavid Rientjes 	if (!task->mm) {
923fa0cbbf1SDavid Rientjes 		err = -EINVAL;
924fa0cbbf1SDavid Rientjes 		goto err_task_lock;
925fa0cbbf1SDavid Rientjes 	}
926fa0cbbf1SDavid Rientjes 
927fa0cbbf1SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
928fa0cbbf1SDavid Rientjes 		err = -ESRCH;
929fa0cbbf1SDavid Rientjes 		goto err_task_lock;
930fa0cbbf1SDavid Rientjes 	}
931fa0cbbf1SDavid Rientjes 
932fa0cbbf1SDavid Rientjes 	/*
933fa0cbbf1SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
934fa0cbbf1SDavid Rientjes 	 * value is always attainable.
935fa0cbbf1SDavid Rientjes 	 */
936fa0cbbf1SDavid Rientjes 	if (oom_adj == OOM_ADJUST_MAX)
937fa0cbbf1SDavid Rientjes 		oom_adj = OOM_SCORE_ADJ_MAX;
938fa0cbbf1SDavid Rientjes 	else
939fa0cbbf1SDavid Rientjes 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
940fa0cbbf1SDavid Rientjes 
941fa0cbbf1SDavid Rientjes 	if (oom_adj < task->signal->oom_score_adj &&
942fa0cbbf1SDavid Rientjes 	    !capable(CAP_SYS_RESOURCE)) {
943fa0cbbf1SDavid Rientjes 		err = -EACCES;
944fa0cbbf1SDavid Rientjes 		goto err_sighand;
945fa0cbbf1SDavid Rientjes 	}
946fa0cbbf1SDavid Rientjes 
947fa0cbbf1SDavid Rientjes 	/*
948fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
949fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_score_adj instead.
950fa0cbbf1SDavid Rientjes 	 */
95187ebdc00SAndrew Morton 	pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
952fa0cbbf1SDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
953fa0cbbf1SDavid Rientjes 		  task_pid_nr(task));
954fa0cbbf1SDavid Rientjes 
955fa0cbbf1SDavid Rientjes 	task->signal->oom_score_adj = oom_adj;
956fa0cbbf1SDavid Rientjes 	trace_oom_score_adj_update(task);
957fa0cbbf1SDavid Rientjes err_sighand:
958fa0cbbf1SDavid Rientjes 	unlock_task_sighand(task, &flags);
959fa0cbbf1SDavid Rientjes err_task_lock:
960fa0cbbf1SDavid Rientjes 	task_unlock(task);
961fa0cbbf1SDavid Rientjes 	put_task_struct(task);
962fa0cbbf1SDavid Rientjes out:
963fa0cbbf1SDavid Rientjes 	return err < 0 ? err : count;
964fa0cbbf1SDavid Rientjes }
965fa0cbbf1SDavid Rientjes 
966fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = {
967fa0cbbf1SDavid Rientjes 	.read		= oom_adj_read,
968fa0cbbf1SDavid Rientjes 	.write		= oom_adj_write,
969fa0cbbf1SDavid Rientjes 	.llseek		= generic_file_llseek,
970fa0cbbf1SDavid Rientjes };
971fa0cbbf1SDavid Rientjes 
972a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
973a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
974a63d83f4SDavid Rientjes {
975496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
976a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
977a9c58b90SDavid Rientjes 	short oom_score_adj = OOM_SCORE_ADJ_MIN;
978a63d83f4SDavid Rientjes 	unsigned long flags;
979a63d83f4SDavid Rientjes 	size_t len;
980a63d83f4SDavid Rientjes 
981a63d83f4SDavid Rientjes 	if (!task)
982a63d83f4SDavid Rientjes 		return -ESRCH;
983a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
984a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
985a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
986a63d83f4SDavid Rientjes 	}
987a63d83f4SDavid Rientjes 	put_task_struct(task);
988a9c58b90SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
989a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
990a63d83f4SDavid Rientjes }
991a63d83f4SDavid Rientjes 
992a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
993a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
994a63d83f4SDavid Rientjes {
995a63d83f4SDavid Rientjes 	struct task_struct *task;
996a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
997a63d83f4SDavid Rientjes 	unsigned long flags;
9980a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
999a63d83f4SDavid Rientjes 	int err;
1000a63d83f4SDavid Rientjes 
1001a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1002a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1003a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1004723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1005723548bfSDavid Rientjes 		err = -EFAULT;
1006723548bfSDavid Rientjes 		goto out;
1007723548bfSDavid Rientjes 	}
1008a63d83f4SDavid Rientjes 
10090a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1010a63d83f4SDavid Rientjes 	if (err)
1011723548bfSDavid Rientjes 		goto out;
1012a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1013723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1014723548bfSDavid Rientjes 		err = -EINVAL;
1015723548bfSDavid Rientjes 		goto out;
1016723548bfSDavid Rientjes 	}
1017a63d83f4SDavid Rientjes 
1018496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1019723548bfSDavid Rientjes 	if (!task) {
1020723548bfSDavid Rientjes 		err = -ESRCH;
1021723548bfSDavid Rientjes 		goto out;
1022723548bfSDavid Rientjes 	}
1023a63d83f4SDavid Rientjes 
10243d5992d2SYing Han 	task_lock(task);
10253d5992d2SYing Han 	if (!task->mm) {
1026723548bfSDavid Rientjes 		err = -EINVAL;
1027723548bfSDavid Rientjes 		goto err_task_lock;
10283d5992d2SYing Han 	}
1029d19d5476SDavid Rientjes 
1030d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1031d19d5476SDavid Rientjes 		err = -ESRCH;
1032d19d5476SDavid Rientjes 		goto err_task_lock;
1033d19d5476SDavid Rientjes 	}
1034d19d5476SDavid Rientjes 
1035a9c58b90SDavid Rientjes 	if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1036d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1037d19d5476SDavid Rientjes 		err = -EACCES;
1038d19d5476SDavid Rientjes 		goto err_sighand;
1039d19d5476SDavid Rientjes 	}
1040d19d5476SDavid Rientjes 
1041a9c58b90SDavid Rientjes 	task->signal->oom_score_adj = (short)oom_score_adj;
1042dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1043a9c58b90SDavid Rientjes 		task->signal->oom_score_adj_min = (short)oom_score_adj;
104443d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
104501dc52ebSDavidlohr Bueso 
1046723548bfSDavid Rientjes err_sighand:
1047a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1048d19d5476SDavid Rientjes err_task_lock:
1049d19d5476SDavid Rientjes 	task_unlock(task);
1050a63d83f4SDavid Rientjes 	put_task_struct(task);
1051723548bfSDavid Rientjes out:
1052723548bfSDavid Rientjes 	return err < 0 ? err : count;
1053a63d83f4SDavid Rientjes }
1054a63d83f4SDavid Rientjes 
1055a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1056a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1057a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10586038f373SArnd Bergmann 	.llseek		= default_llseek,
1059a63d83f4SDavid Rientjes };
1060a63d83f4SDavid Rientjes 
10611da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10621da177e4SLinus Torvalds #define TMPBUFLEN 21
10631da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10641da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10651da177e4SLinus Torvalds {
1066496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
106799f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10681da177e4SLinus Torvalds 	ssize_t length;
10691da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10701da177e4SLinus Torvalds 
107199f89551SEric W. Biederman 	if (!task)
107299f89551SEric W. Biederman 		return -ESRCH;
10731da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1074e1760bd5SEric W. Biederman 			   from_kuid(file->f_cred->user_ns,
1075e1760bd5SEric W. Biederman 				     audit_get_loginuid(task)));
107699f89551SEric W. Biederman 	put_task_struct(task);
10771da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10811da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10821da177e4SLinus Torvalds {
1083496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
10841da177e4SLinus Torvalds 	char *page, *tmp;
10851da177e4SLinus Torvalds 	ssize_t length;
10861da177e4SLinus Torvalds 	uid_t loginuid;
1087e1760bd5SEric W. Biederman 	kuid_t kloginuid;
10881da177e4SLinus Torvalds 
10897dc52157SPaul E. McKenney 	rcu_read_lock();
10907dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
10917dc52157SPaul E. McKenney 		rcu_read_unlock();
10921da177e4SLinus Torvalds 		return -EPERM;
10937dc52157SPaul E. McKenney 	}
10947dc52157SPaul E. McKenney 	rcu_read_unlock();
10951da177e4SLinus Torvalds 
1096e0182909SAl Viro 	if (count >= PAGE_SIZE)
1097e0182909SAl Viro 		count = PAGE_SIZE - 1;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	if (*ppos != 0) {
11001da177e4SLinus Torvalds 		/* No partial writes. */
11011da177e4SLinus Torvalds 		return -EINVAL;
11021da177e4SLinus Torvalds 	}
1103e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11041da177e4SLinus Torvalds 	if (!page)
11051da177e4SLinus Torvalds 		return -ENOMEM;
11061da177e4SLinus Torvalds 	length = -EFAULT;
11071da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11081da177e4SLinus Torvalds 		goto out_free_page;
11091da177e4SLinus Torvalds 
1110e0182909SAl Viro 	page[count] = '\0';
11111da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11121da177e4SLinus Torvalds 	if (tmp == page) {
11131da177e4SLinus Torvalds 		length = -EINVAL;
11141da177e4SLinus Torvalds 		goto out_free_page;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	}
111781407c84SEric Paris 
111881407c84SEric Paris 	/* is userspace tring to explicitly UNSET the loginuid? */
111981407c84SEric Paris 	if (loginuid == AUDIT_UID_UNSET) {
112081407c84SEric Paris 		kloginuid = INVALID_UID;
112181407c84SEric Paris 	} else {
1122e1760bd5SEric W. Biederman 		kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1123e1760bd5SEric W. Biederman 		if (!uid_valid(kloginuid)) {
1124e1760bd5SEric W. Biederman 			length = -EINVAL;
1125e1760bd5SEric W. Biederman 			goto out_free_page;
1126e1760bd5SEric W. Biederman 		}
112781407c84SEric Paris 	}
1128e1760bd5SEric W. Biederman 
1129e1760bd5SEric W. Biederman 	length = audit_set_loginuid(kloginuid);
11301da177e4SLinus Torvalds 	if (likely(length == 0))
11311da177e4SLinus Torvalds 		length = count;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds out_free_page:
11341da177e4SLinus Torvalds 	free_page((unsigned long) page);
11351da177e4SLinus Torvalds 	return length;
11361da177e4SLinus Torvalds }
11371da177e4SLinus Torvalds 
113800977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11391da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11401da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
114187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11421da177e4SLinus Torvalds };
11431e0bd755SEric Paris 
11441e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11451e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11461e0bd755SEric Paris {
1147496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
11481e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11491e0bd755SEric Paris 	ssize_t length;
11501e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11511e0bd755SEric Paris 
11521e0bd755SEric Paris 	if (!task)
11531e0bd755SEric Paris 		return -ESRCH;
11541e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11551e0bd755SEric Paris 				audit_get_sessionid(task));
11561e0bd755SEric Paris 	put_task_struct(task);
11571e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11581e0bd755SEric Paris }
11591e0bd755SEric Paris 
11601e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11611e0bd755SEric Paris 	.read		= proc_sessionid_read,
116287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11631e0bd755SEric Paris };
11641da177e4SLinus Torvalds #endif
11651da177e4SLinus Torvalds 
1166f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1167f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1168f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1169f4f154fdSAkinobu Mita {
1170496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1171f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1172f4f154fdSAkinobu Mita 	size_t len;
1173f4f154fdSAkinobu Mita 	int make_it_fail;
1174f4f154fdSAkinobu Mita 
1175f4f154fdSAkinobu Mita 	if (!task)
1176f4f154fdSAkinobu Mita 		return -ESRCH;
1177f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1178f4f154fdSAkinobu Mita 	put_task_struct(task);
1179f4f154fdSAkinobu Mita 
1180f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11810c28f287SAkinobu Mita 
11820c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1183f4f154fdSAkinobu Mita }
1184f4f154fdSAkinobu Mita 
1185f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1186f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1187f4f154fdSAkinobu Mita {
1188f4f154fdSAkinobu Mita 	struct task_struct *task;
1189f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1190f4f154fdSAkinobu Mita 	int make_it_fail;
1191f4f154fdSAkinobu Mita 
1192f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1193f4f154fdSAkinobu Mita 		return -EPERM;
1194f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1195f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1196f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1197f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1198f4f154fdSAkinobu Mita 		return -EFAULT;
1199cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1200cba8aafeSVincent Li 	if (*end)
1201cba8aafeSVincent Li 		return -EINVAL;
120216caed31SDave Jones 	if (make_it_fail < 0 || make_it_fail > 1)
120316caed31SDave Jones 		return -EINVAL;
120416caed31SDave Jones 
1205496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1206f4f154fdSAkinobu Mita 	if (!task)
1207f4f154fdSAkinobu Mita 		return -ESRCH;
1208f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1209f4f154fdSAkinobu Mita 	put_task_struct(task);
1210cba8aafeSVincent Li 
1211cba8aafeSVincent Li 	return count;
1212f4f154fdSAkinobu Mita }
1213f4f154fdSAkinobu Mita 
121400977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1215f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1216f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
121787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1218f4f154fdSAkinobu Mita };
1219f4f154fdSAkinobu Mita #endif
1220f4f154fdSAkinobu Mita 
12219745512cSArjan van de Ven 
122243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
122343ae34cbSIngo Molnar /*
122443ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
122543ae34cbSIngo Molnar  */
122643ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
122743ae34cbSIngo Molnar {
122843ae34cbSIngo Molnar 	struct inode *inode = m->private;
122943ae34cbSIngo Molnar 	struct task_struct *p;
123043ae34cbSIngo Molnar 
123143ae34cbSIngo Molnar 	p = get_proc_task(inode);
123243ae34cbSIngo Molnar 	if (!p)
123343ae34cbSIngo Molnar 		return -ESRCH;
123443ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
123543ae34cbSIngo Molnar 
123643ae34cbSIngo Molnar 	put_task_struct(p);
123743ae34cbSIngo Molnar 
123843ae34cbSIngo Molnar 	return 0;
123943ae34cbSIngo Molnar }
124043ae34cbSIngo Molnar 
124143ae34cbSIngo Molnar static ssize_t
124243ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
124343ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
124443ae34cbSIngo Molnar {
1245496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
124643ae34cbSIngo Molnar 	struct task_struct *p;
124743ae34cbSIngo Molnar 
124843ae34cbSIngo Molnar 	p = get_proc_task(inode);
124943ae34cbSIngo Molnar 	if (!p)
125043ae34cbSIngo Molnar 		return -ESRCH;
125143ae34cbSIngo Molnar 	proc_sched_set_task(p);
125243ae34cbSIngo Molnar 
125343ae34cbSIngo Molnar 	put_task_struct(p);
125443ae34cbSIngo Molnar 
125543ae34cbSIngo Molnar 	return count;
125643ae34cbSIngo Molnar }
125743ae34cbSIngo Molnar 
125843ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
125943ae34cbSIngo Molnar {
1260c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
126143ae34cbSIngo Molnar }
126243ae34cbSIngo Molnar 
126343ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
126443ae34cbSIngo Molnar 	.open		= sched_open,
126543ae34cbSIngo Molnar 	.read		= seq_read,
126643ae34cbSIngo Molnar 	.write		= sched_write,
126743ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12685ea473a1SAlexey Dobriyan 	.release	= single_release,
126943ae34cbSIngo Molnar };
127043ae34cbSIngo Molnar 
127143ae34cbSIngo Molnar #endif
127243ae34cbSIngo Molnar 
12735091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
12745091faa4SMike Galbraith /*
12755091faa4SMike Galbraith  * Print out autogroup related information:
12765091faa4SMike Galbraith  */
12775091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
12785091faa4SMike Galbraith {
12795091faa4SMike Galbraith 	struct inode *inode = m->private;
12805091faa4SMike Galbraith 	struct task_struct *p;
12815091faa4SMike Galbraith 
12825091faa4SMike Galbraith 	p = get_proc_task(inode);
12835091faa4SMike Galbraith 	if (!p)
12845091faa4SMike Galbraith 		return -ESRCH;
12855091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
12865091faa4SMike Galbraith 
12875091faa4SMike Galbraith 	put_task_struct(p);
12885091faa4SMike Galbraith 
12895091faa4SMike Galbraith 	return 0;
12905091faa4SMike Galbraith }
12915091faa4SMike Galbraith 
12925091faa4SMike Galbraith static ssize_t
12935091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
12945091faa4SMike Galbraith 	    size_t count, loff_t *offset)
12955091faa4SMike Galbraith {
1296496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
12975091faa4SMike Galbraith 	struct task_struct *p;
12985091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
12990a8cb8e3SAlexey Dobriyan 	int nice;
13005091faa4SMike Galbraith 	int err;
13015091faa4SMike Galbraith 
13025091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
13035091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
13045091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
13055091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
13065091faa4SMike Galbraith 		return -EFAULT;
13075091faa4SMike Galbraith 
13080a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
13090a8cb8e3SAlexey Dobriyan 	if (err < 0)
13100a8cb8e3SAlexey Dobriyan 		return err;
13115091faa4SMike Galbraith 
13125091faa4SMike Galbraith 	p = get_proc_task(inode);
13135091faa4SMike Galbraith 	if (!p)
13145091faa4SMike Galbraith 		return -ESRCH;
13155091faa4SMike Galbraith 
13162e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
13175091faa4SMike Galbraith 	if (err)
13185091faa4SMike Galbraith 		count = err;
13195091faa4SMike Galbraith 
13205091faa4SMike Galbraith 	put_task_struct(p);
13215091faa4SMike Galbraith 
13225091faa4SMike Galbraith 	return count;
13235091faa4SMike Galbraith }
13245091faa4SMike Galbraith 
13255091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
13265091faa4SMike Galbraith {
13275091faa4SMike Galbraith 	int ret;
13285091faa4SMike Galbraith 
13295091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13305091faa4SMike Galbraith 	if (!ret) {
13315091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13325091faa4SMike Galbraith 
13335091faa4SMike Galbraith 		m->private = inode;
13345091faa4SMike Galbraith 	}
13355091faa4SMike Galbraith 	return ret;
13365091faa4SMike Galbraith }
13375091faa4SMike Galbraith 
13385091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13395091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13405091faa4SMike Galbraith 	.read		= seq_read,
13415091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13425091faa4SMike Galbraith 	.llseek		= seq_lseek,
13435091faa4SMike Galbraith 	.release	= single_release,
13445091faa4SMike Galbraith };
13455091faa4SMike Galbraith 
13465091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
13475091faa4SMike Galbraith 
13484614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13494614a696Sjohn stultz 				size_t count, loff_t *offset)
13504614a696Sjohn stultz {
1351496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
13524614a696Sjohn stultz 	struct task_struct *p;
13534614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
1354830e0fc9SDavid Rientjes 	const size_t maxlen = sizeof(buffer) - 1;
13554614a696Sjohn stultz 
13564614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
1357830e0fc9SDavid Rientjes 	if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
13584614a696Sjohn stultz 		return -EFAULT;
13594614a696Sjohn stultz 
13604614a696Sjohn stultz 	p = get_proc_task(inode);
13614614a696Sjohn stultz 	if (!p)
13624614a696Sjohn stultz 		return -ESRCH;
13634614a696Sjohn stultz 
13644614a696Sjohn stultz 	if (same_thread_group(current, p))
13654614a696Sjohn stultz 		set_task_comm(p, buffer);
13664614a696Sjohn stultz 	else
13674614a696Sjohn stultz 		count = -EINVAL;
13684614a696Sjohn stultz 
13694614a696Sjohn stultz 	put_task_struct(p);
13704614a696Sjohn stultz 
13714614a696Sjohn stultz 	return count;
13724614a696Sjohn stultz }
13734614a696Sjohn stultz 
13744614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13754614a696Sjohn stultz {
13764614a696Sjohn stultz 	struct inode *inode = m->private;
13774614a696Sjohn stultz 	struct task_struct *p;
13784614a696Sjohn stultz 
13794614a696Sjohn stultz 	p = get_proc_task(inode);
13804614a696Sjohn stultz 	if (!p)
13814614a696Sjohn stultz 		return -ESRCH;
13824614a696Sjohn stultz 
13834614a696Sjohn stultz 	task_lock(p);
13844614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13854614a696Sjohn stultz 	task_unlock(p);
13864614a696Sjohn stultz 
13874614a696Sjohn stultz 	put_task_struct(p);
13884614a696Sjohn stultz 
13894614a696Sjohn stultz 	return 0;
13904614a696Sjohn stultz }
13914614a696Sjohn stultz 
13924614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
13934614a696Sjohn stultz {
1394c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
13954614a696Sjohn stultz }
13964614a696Sjohn stultz 
13974614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
13984614a696Sjohn stultz 	.open		= comm_open,
13994614a696Sjohn stultz 	.read		= seq_read,
14004614a696Sjohn stultz 	.write		= comm_write,
14014614a696Sjohn stultz 	.llseek		= seq_lseek,
14024614a696Sjohn stultz 	.release	= single_release,
14034614a696Sjohn stultz };
14044614a696Sjohn stultz 
14057773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1406925d1c40SMatt Helsley {
1407925d1c40SMatt Helsley 	struct task_struct *task;
1408925d1c40SMatt Helsley 	struct mm_struct *mm;
1409925d1c40SMatt Helsley 	struct file *exe_file;
1410925d1c40SMatt Helsley 
14117773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1412925d1c40SMatt Helsley 	if (!task)
1413925d1c40SMatt Helsley 		return -ENOENT;
1414925d1c40SMatt Helsley 	mm = get_task_mm(task);
1415925d1c40SMatt Helsley 	put_task_struct(task);
1416925d1c40SMatt Helsley 	if (!mm)
1417925d1c40SMatt Helsley 		return -ENOENT;
1418925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1419925d1c40SMatt Helsley 	mmput(mm);
1420925d1c40SMatt Helsley 	if (exe_file) {
1421925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1422925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1423925d1c40SMatt Helsley 		fput(exe_file);
1424925d1c40SMatt Helsley 		return 0;
1425925d1c40SMatt Helsley 	} else
1426925d1c40SMatt Helsley 		return -ENOENT;
1427925d1c40SMatt Helsley }
1428925d1c40SMatt Helsley 
1429008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14301da177e4SLinus Torvalds {
14311da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1432408ef013SChristoph Hellwig 	struct path path;
14331da177e4SLinus Torvalds 	int error = -EACCES;
14341da177e4SLinus Torvalds 
1435778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1436778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14371da177e4SLinus Torvalds 		goto out;
14381da177e4SLinus Torvalds 
1439408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1440408ef013SChristoph Hellwig 	if (error)
1441408ef013SChristoph Hellwig 		goto out;
1442408ef013SChristoph Hellwig 
1443b5fb63c1SChristoph Hellwig 	nd_jump_link(nd, &path);
1444408ef013SChristoph Hellwig 	return NULL;
14451da177e4SLinus Torvalds out:
1446008b150aSAl Viro 	return ERR_PTR(error);
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14493dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14501da177e4SLinus Torvalds {
1451e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14523dcd25f3SJan Blunck 	char *pathname;
14531da177e4SLinus Torvalds 	int len;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	if (!tmp)
14561da177e4SLinus Torvalds 		return -ENOMEM;
14571da177e4SLinus Torvalds 
14587b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14593dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14603dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14611da177e4SLinus Torvalds 		goto out;
14623dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	if (len > buflen)
14651da177e4SLinus Torvalds 		len = buflen;
14663dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14671da177e4SLinus Torvalds 		len = -EFAULT;
14681da177e4SLinus Torvalds  out:
14691da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14701da177e4SLinus Torvalds 	return len;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	int error = -EACCES;
14761da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14773dcd25f3SJan Blunck 	struct path path;
14781da177e4SLinus Torvalds 
1479778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1480778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14811da177e4SLinus Torvalds 		goto out;
14821da177e4SLinus Torvalds 
14837773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
14841da177e4SLinus Torvalds 	if (error)
14851da177e4SLinus Torvalds 		goto out;
14861da177e4SLinus Torvalds 
14873dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14883dcd25f3SJan Blunck 	path_put(&path);
14891da177e4SLinus Torvalds out:
14901da177e4SLinus Torvalds 	return error;
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds 
1493faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = {
14941da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
14956d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
14966d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
14971da177e4SLinus Torvalds };
14981da177e4SLinus Torvalds 
149928a6d671SEric W. Biederman 
150028a6d671SEric W. Biederman /* building an inode */
150128a6d671SEric W. Biederman 
15026b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
150328a6d671SEric W. Biederman {
150428a6d671SEric W. Biederman 	struct inode * inode;
150528a6d671SEric W. Biederman 	struct proc_inode *ei;
1506c69e8d9cSDavid Howells 	const struct cred *cred;
150728a6d671SEric W. Biederman 
150828a6d671SEric W. Biederman 	/* We need a new inode */
150928a6d671SEric W. Biederman 
151028a6d671SEric W. Biederman 	inode = new_inode(sb);
151128a6d671SEric W. Biederman 	if (!inode)
151228a6d671SEric W. Biederman 		goto out;
151328a6d671SEric W. Biederman 
151428a6d671SEric W. Biederman 	/* Common stuff */
151528a6d671SEric W. Biederman 	ei = PROC_I(inode);
151685fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
151728a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
151828a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
151928a6d671SEric W. Biederman 
152028a6d671SEric W. Biederman 	/*
152128a6d671SEric W. Biederman 	 * grab the reference to task.
152228a6d671SEric W. Biederman 	 */
15231a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
152428a6d671SEric W. Biederman 	if (!ei->pid)
152528a6d671SEric W. Biederman 		goto out_unlock;
152628a6d671SEric W. Biederman 
152728a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1528c69e8d9cSDavid Howells 		rcu_read_lock();
1529c69e8d9cSDavid Howells 		cred = __task_cred(task);
1530c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1531c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1532c69e8d9cSDavid Howells 		rcu_read_unlock();
153328a6d671SEric W. Biederman 	}
153428a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
153528a6d671SEric W. Biederman 
153628a6d671SEric W. Biederman out:
153728a6d671SEric W. Biederman 	return inode;
153828a6d671SEric W. Biederman 
153928a6d671SEric W. Biederman out_unlock:
154028a6d671SEric W. Biederman 	iput(inode);
154128a6d671SEric W. Biederman 	return NULL;
154228a6d671SEric W. Biederman }
154328a6d671SEric W. Biederman 
15446b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
154528a6d671SEric W. Biederman {
154628a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
154728a6d671SEric W. Biederman 	struct task_struct *task;
1548c69e8d9cSDavid Howells 	const struct cred *cred;
15490499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1550c69e8d9cSDavid Howells 
155128a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
155228a6d671SEric W. Biederman 
155328a6d671SEric W. Biederman 	rcu_read_lock();
1554dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1555dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
155628a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
155728a6d671SEric W. Biederman 	if (task) {
15580499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15590499680aSVasiliy Kulikov 			rcu_read_unlock();
15600499680aSVasiliy Kulikov 			/*
15610499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15620499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15630499680aSVasiliy Kulikov 			 */
15640499680aSVasiliy Kulikov 			return -ENOENT;
15650499680aSVasiliy Kulikov 		}
156628a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
156728a6d671SEric W. Biederman 		    task_dumpable(task)) {
1568c69e8d9cSDavid Howells 			cred = __task_cred(task);
1569c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1570c69e8d9cSDavid Howells 			stat->gid = cred->egid;
157128a6d671SEric W. Biederman 		}
157228a6d671SEric W. Biederman 	}
157328a6d671SEric W. Biederman 	rcu_read_unlock();
157428a6d671SEric W. Biederman 	return 0;
157528a6d671SEric W. Biederman }
157628a6d671SEric W. Biederman 
157728a6d671SEric W. Biederman /* dentry stuff */
157828a6d671SEric W. Biederman 
157928a6d671SEric W. Biederman /*
158028a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
158128a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
158228a6d671SEric W. Biederman  * due to the way we treat inodes.
158328a6d671SEric W. Biederman  *
158428a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
158528a6d671SEric W. Biederman  * performed a setuid(), etc.
158628a6d671SEric W. Biederman  *
158728a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
158828a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
158928a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
159028a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
159128a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
159228a6d671SEric W. Biederman  * directories.
159328a6d671SEric W. Biederman  */
15940b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags)
159528a6d671SEric W. Biederman {
159634286d66SNick Piggin 	struct inode *inode;
159734286d66SNick Piggin 	struct task_struct *task;
1598c69e8d9cSDavid Howells 	const struct cred *cred;
1599c69e8d9cSDavid Howells 
16000b728e19SAl Viro 	if (flags & LOOKUP_RCU)
160134286d66SNick Piggin 		return -ECHILD;
160234286d66SNick Piggin 
160334286d66SNick Piggin 	inode = dentry->d_inode;
160434286d66SNick Piggin 	task = get_proc_task(inode);
160534286d66SNick Piggin 
160628a6d671SEric W. Biederman 	if (task) {
160728a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
160828a6d671SEric W. Biederman 		    task_dumpable(task)) {
1609c69e8d9cSDavid Howells 			rcu_read_lock();
1610c69e8d9cSDavid Howells 			cred = __task_cred(task);
1611c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1612c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1613c69e8d9cSDavid Howells 			rcu_read_unlock();
161428a6d671SEric W. Biederman 		} else {
1615dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1616dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
161728a6d671SEric W. Biederman 		}
161828a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
161928a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
162028a6d671SEric W. Biederman 		put_task_struct(task);
162128a6d671SEric W. Biederman 		return 1;
162228a6d671SEric W. Biederman 	}
162328a6d671SEric W. Biederman 	d_drop(dentry);
162428a6d671SEric W. Biederman 	return 0;
162528a6d671SEric W. Biederman }
162628a6d671SEric W. Biederman 
1627d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode)
1628d855a4b7SOleg Nesterov {
1629d855a4b7SOleg Nesterov 	return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1630d855a4b7SOleg Nesterov }
1631d855a4b7SOleg Nesterov 
16321dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry)
16331dd704b6SDavid Howells {
16341dd704b6SDavid Howells 	/* Is the task we represent dead?
16351dd704b6SDavid Howells 	 * If so, then don't put the dentry on the lru list,
16361dd704b6SDavid Howells 	 * kill it immediately.
16371dd704b6SDavid Howells 	 */
1638d855a4b7SOleg Nesterov 	return proc_inode_is_dead(dentry->d_inode);
16391dd704b6SDavid Howells }
16401dd704b6SDavid Howells 
16416b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
164228a6d671SEric W. Biederman {
164328a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
164428a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
164528a6d671SEric W. Biederman };
164628a6d671SEric W. Biederman 
164728a6d671SEric W. Biederman /* Lookups */
164828a6d671SEric W. Biederman 
16491c0d04c9SEric W. Biederman /*
16501c0d04c9SEric W. Biederman  * Fill a directory entry.
16511c0d04c9SEric W. Biederman  *
16521c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16531c0d04c9SEric W. Biederman  * file type from dcache entry.
16541c0d04c9SEric W. Biederman  *
16551c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16561c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16571c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16581c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16591c0d04c9SEric W. Biederman  * by stat.
16601c0d04c9SEric W. Biederman  */
1661f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx,
16626b4e306aSEric W. Biederman 	const char *name, int len,
1663c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
166461a28784SEric W. Biederman {
1665f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
16661df98b8bSAl Viro 	struct qstr qname = QSTR_INIT(name, len);
166761a28784SEric W. Biederman 	struct inode *inode;
16681df98b8bSAl Viro 	unsigned type;
16691df98b8bSAl Viro 	ino_t ino;
167061a28784SEric W. Biederman 
16711df98b8bSAl Viro 	child = d_hash_and_lookup(dir, &qname);
167261a28784SEric W. Biederman 	if (!child) {
16731df98b8bSAl Viro 		child = d_alloc(dir, &qname);
16741df98b8bSAl Viro 		if (!child)
167561a28784SEric W. Biederman 			goto end_instantiate;
16761df98b8bSAl Viro 		if (instantiate(dir->d_inode, child, task, ptr) < 0) {
16771df98b8bSAl Viro 			dput(child);
16781df98b8bSAl Viro 			goto end_instantiate;
16791df98b8bSAl Viro 		}
16801df98b8bSAl Viro 	}
168161a28784SEric W. Biederman 	inode = child->d_inode;
168261a28784SEric W. Biederman 	ino = inode->i_ino;
168361a28784SEric W. Biederman 	type = inode->i_mode >> 12;
168461a28784SEric W. Biederman 	dput(child);
1685f0c3b509SAl Viro 	return dir_emit(ctx, name, len, ino, type);
16861df98b8bSAl Viro 
16871df98b8bSAl Viro end_instantiate:
16881df98b8bSAl Viro 	return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
168961a28784SEric W. Biederman }
169061a28784SEric W. Biederman 
1691640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
1692640708a2SPavel Emelyanov 
1693640708a2SPavel Emelyanov /*
1694640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1695640708a2SPavel Emelyanov  * which represent vma start and end addresses.
1696640708a2SPavel Emelyanov  */
1697640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
1698640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
1699640708a2SPavel Emelyanov {
1700640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1701640708a2SPavel Emelyanov 		return -EINVAL;
1702640708a2SPavel Emelyanov 
1703640708a2SPavel Emelyanov 	return 0;
1704640708a2SPavel Emelyanov }
1705640708a2SPavel Emelyanov 
17060b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1707640708a2SPavel Emelyanov {
1708640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1709640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
1710640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
1711640708a2SPavel Emelyanov 	struct task_struct *task;
1712640708a2SPavel Emelyanov 	const struct cred *cred;
1713640708a2SPavel Emelyanov 	struct inode *inode;
1714640708a2SPavel Emelyanov 	int status = 0;
1715640708a2SPavel Emelyanov 
17160b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1717640708a2SPavel Emelyanov 		return -ECHILD;
1718640708a2SPavel Emelyanov 
1719640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
172041735818SZhao Hongjiang 		status = -EPERM;
1721640708a2SPavel Emelyanov 		goto out_notask;
1722640708a2SPavel Emelyanov 	}
1723640708a2SPavel Emelyanov 
1724640708a2SPavel Emelyanov 	inode = dentry->d_inode;
1725640708a2SPavel Emelyanov 	task = get_proc_task(inode);
1726640708a2SPavel Emelyanov 	if (!task)
1727640708a2SPavel Emelyanov 		goto out_notask;
1728640708a2SPavel Emelyanov 
17292344bec7SCong Wang 	mm = mm_access(task, PTRACE_MODE_READ);
17302344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
1731640708a2SPavel Emelyanov 		goto out;
1732640708a2SPavel Emelyanov 
1733640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1734640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
1735640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1736640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
1737640708a2SPavel Emelyanov 	}
1738640708a2SPavel Emelyanov 
1739640708a2SPavel Emelyanov 	mmput(mm);
1740640708a2SPavel Emelyanov 
1741640708a2SPavel Emelyanov 	if (exact_vma_exists) {
1742640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
1743640708a2SPavel Emelyanov 			rcu_read_lock();
1744640708a2SPavel Emelyanov 			cred = __task_cred(task);
1745640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
1746640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
1747640708a2SPavel Emelyanov 			rcu_read_unlock();
1748640708a2SPavel Emelyanov 		} else {
1749dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1750dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
1751640708a2SPavel Emelyanov 		}
1752640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
1753640708a2SPavel Emelyanov 		status = 1;
1754640708a2SPavel Emelyanov 	}
1755640708a2SPavel Emelyanov 
1756640708a2SPavel Emelyanov out:
1757640708a2SPavel Emelyanov 	put_task_struct(task);
1758640708a2SPavel Emelyanov 
1759640708a2SPavel Emelyanov out_notask:
1760640708a2SPavel Emelyanov 	if (status <= 0)
1761640708a2SPavel Emelyanov 		d_drop(dentry);
1762640708a2SPavel Emelyanov 
1763640708a2SPavel Emelyanov 	return status;
1764640708a2SPavel Emelyanov }
1765640708a2SPavel Emelyanov 
1766640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
1767640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
1768640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
1769640708a2SPavel Emelyanov };
1770640708a2SPavel Emelyanov 
1771640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1772640708a2SPavel Emelyanov {
1773640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1774640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1775640708a2SPavel Emelyanov 	struct task_struct *task;
1776640708a2SPavel Emelyanov 	struct mm_struct *mm;
1777640708a2SPavel Emelyanov 	int rc;
1778640708a2SPavel Emelyanov 
1779640708a2SPavel Emelyanov 	rc = -ENOENT;
1780640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
1781640708a2SPavel Emelyanov 	if (!task)
1782640708a2SPavel Emelyanov 		goto out;
1783640708a2SPavel Emelyanov 
1784640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1785640708a2SPavel Emelyanov 	put_task_struct(task);
1786640708a2SPavel Emelyanov 	if (!mm)
1787640708a2SPavel Emelyanov 		goto out;
1788640708a2SPavel Emelyanov 
1789640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1790640708a2SPavel Emelyanov 	if (rc)
1791640708a2SPavel Emelyanov 		goto out_mmput;
1792640708a2SPavel Emelyanov 
179370335abbSArtem Fetishev 	rc = -ENOENT;
1794640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1795640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1796640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
1797640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
1798640708a2SPavel Emelyanov 		path_get(path);
1799640708a2SPavel Emelyanov 		rc = 0;
1800640708a2SPavel Emelyanov 	}
1801640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1802640708a2SPavel Emelyanov 
1803640708a2SPavel Emelyanov out_mmput:
1804640708a2SPavel Emelyanov 	mmput(mm);
1805640708a2SPavel Emelyanov out:
1806640708a2SPavel Emelyanov 	return rc;
1807640708a2SPavel Emelyanov }
1808640708a2SPavel Emelyanov 
1809640708a2SPavel Emelyanov struct map_files_info {
18107b540d06SAl Viro 	fmode_t		mode;
1811640708a2SPavel Emelyanov 	unsigned long	len;
1812640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1813640708a2SPavel Emelyanov };
1814640708a2SPavel Emelyanov 
1815c52a47acSAl Viro static int
1816640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1817640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
1818640708a2SPavel Emelyanov {
18197b540d06SAl Viro 	fmode_t mode = (fmode_t)(unsigned long)ptr;
1820640708a2SPavel Emelyanov 	struct proc_inode *ei;
1821640708a2SPavel Emelyanov 	struct inode *inode;
1822640708a2SPavel Emelyanov 
1823640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
1824640708a2SPavel Emelyanov 	if (!inode)
1825c52a47acSAl Viro 		return -ENOENT;
1826640708a2SPavel Emelyanov 
1827640708a2SPavel Emelyanov 	ei = PROC_I(inode);
1828640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
1829640708a2SPavel Emelyanov 
1830640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
1831640708a2SPavel Emelyanov 	inode->i_size = 64;
1832640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
1833640708a2SPavel Emelyanov 
18347b540d06SAl Viro 	if (mode & FMODE_READ)
1835640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
18367b540d06SAl Viro 	if (mode & FMODE_WRITE)
1837640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
1838640708a2SPavel Emelyanov 
1839640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
1840640708a2SPavel Emelyanov 	d_add(dentry, inode);
1841640708a2SPavel Emelyanov 
1842c52a47acSAl Viro 	return 0;
1843640708a2SPavel Emelyanov }
1844640708a2SPavel Emelyanov 
1845640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
184600cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
1847640708a2SPavel Emelyanov {
1848640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1849640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1850640708a2SPavel Emelyanov 	struct task_struct *task;
1851c52a47acSAl Viro 	int result;
1852640708a2SPavel Emelyanov 	struct mm_struct *mm;
1853640708a2SPavel Emelyanov 
1854c52a47acSAl Viro 	result = -EPERM;
1855640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1856640708a2SPavel Emelyanov 		goto out;
1857640708a2SPavel Emelyanov 
1858c52a47acSAl Viro 	result = -ENOENT;
1859640708a2SPavel Emelyanov 	task = get_proc_task(dir);
1860640708a2SPavel Emelyanov 	if (!task)
1861640708a2SPavel Emelyanov 		goto out;
1862640708a2SPavel Emelyanov 
1863c52a47acSAl Viro 	result = -EACCES;
1864eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1865640708a2SPavel Emelyanov 		goto out_put_task;
1866640708a2SPavel Emelyanov 
1867c52a47acSAl Viro 	result = -ENOENT;
1868640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1869eb94cd96SCyrill Gorcunov 		goto out_put_task;
1870640708a2SPavel Emelyanov 
1871640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1872640708a2SPavel Emelyanov 	if (!mm)
1873eb94cd96SCyrill Gorcunov 		goto out_put_task;
1874640708a2SPavel Emelyanov 
1875640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1876640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1877640708a2SPavel Emelyanov 	if (!vma)
1878640708a2SPavel Emelyanov 		goto out_no_vma;
1879640708a2SPavel Emelyanov 
188005f56484SStanislav Kinsbursky 	if (vma->vm_file)
18817b540d06SAl Viro 		result = proc_map_files_instantiate(dir, dentry, task,
18827b540d06SAl Viro 				(void *)(unsigned long)vma->vm_file->f_mode);
1883640708a2SPavel Emelyanov 
1884640708a2SPavel Emelyanov out_no_vma:
1885640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1886640708a2SPavel Emelyanov 	mmput(mm);
1887640708a2SPavel Emelyanov out_put_task:
1888640708a2SPavel Emelyanov 	put_task_struct(task);
1889640708a2SPavel Emelyanov out:
1890c52a47acSAl Viro 	return ERR_PTR(result);
1891640708a2SPavel Emelyanov }
1892640708a2SPavel Emelyanov 
1893640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
1894640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
1895640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
1896640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
1897640708a2SPavel Emelyanov };
1898640708a2SPavel Emelyanov 
1899640708a2SPavel Emelyanov static int
1900f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1901640708a2SPavel Emelyanov {
1902640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1903640708a2SPavel Emelyanov 	struct task_struct *task;
1904640708a2SPavel Emelyanov 	struct mm_struct *mm;
1905f0c3b509SAl Viro 	unsigned long nr_files, pos, i;
1906f0c3b509SAl Viro 	struct flex_array *fa = NULL;
1907f0c3b509SAl Viro 	struct map_files_info info;
1908f0c3b509SAl Viro 	struct map_files_info *p;
1909640708a2SPavel Emelyanov 	int ret;
1910640708a2SPavel Emelyanov 
191141735818SZhao Hongjiang 	ret = -EPERM;
1912640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1913640708a2SPavel Emelyanov 		goto out;
1914640708a2SPavel Emelyanov 
1915640708a2SPavel Emelyanov 	ret = -ENOENT;
1916f0c3b509SAl Viro 	task = get_proc_task(file_inode(file));
1917640708a2SPavel Emelyanov 	if (!task)
1918640708a2SPavel Emelyanov 		goto out;
1919640708a2SPavel Emelyanov 
1920640708a2SPavel Emelyanov 	ret = -EACCES;
1921eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1922640708a2SPavel Emelyanov 		goto out_put_task;
1923640708a2SPavel Emelyanov 
1924640708a2SPavel Emelyanov 	ret = 0;
1925f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
1926eb94cd96SCyrill Gorcunov 		goto out_put_task;
1927640708a2SPavel Emelyanov 
1928640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1929640708a2SPavel Emelyanov 	if (!mm)
1930eb94cd96SCyrill Gorcunov 		goto out_put_task;
1931640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1932640708a2SPavel Emelyanov 
1933640708a2SPavel Emelyanov 	nr_files = 0;
1934640708a2SPavel Emelyanov 
1935640708a2SPavel Emelyanov 	/*
1936640708a2SPavel Emelyanov 	 * We need two passes here:
1937640708a2SPavel Emelyanov 	 *
1938640708a2SPavel Emelyanov 	 *  1) Collect vmas of mapped files with mmap_sem taken
1939640708a2SPavel Emelyanov 	 *  2) Release mmap_sem and instantiate entries
1940640708a2SPavel Emelyanov 	 *
1941640708a2SPavel Emelyanov 	 * otherwise we get lockdep complained, since filldir()
1942640708a2SPavel Emelyanov 	 * routine might require mmap_sem taken in might_fault().
1943640708a2SPavel Emelyanov 	 */
1944640708a2SPavel Emelyanov 
1945640708a2SPavel Emelyanov 	for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1946f0c3b509SAl Viro 		if (vma->vm_file && ++pos > ctx->pos)
1947640708a2SPavel Emelyanov 			nr_files++;
1948640708a2SPavel Emelyanov 	}
1949640708a2SPavel Emelyanov 
1950640708a2SPavel Emelyanov 	if (nr_files) {
1951640708a2SPavel Emelyanov 		fa = flex_array_alloc(sizeof(info), nr_files,
1952640708a2SPavel Emelyanov 					GFP_KERNEL);
1953640708a2SPavel Emelyanov 		if (!fa || flex_array_prealloc(fa, 0, nr_files,
1954640708a2SPavel Emelyanov 						GFP_KERNEL)) {
1955640708a2SPavel Emelyanov 			ret = -ENOMEM;
1956640708a2SPavel Emelyanov 			if (fa)
1957640708a2SPavel Emelyanov 				flex_array_free(fa);
1958640708a2SPavel Emelyanov 			up_read(&mm->mmap_sem);
1959640708a2SPavel Emelyanov 			mmput(mm);
1960eb94cd96SCyrill Gorcunov 			goto out_put_task;
1961640708a2SPavel Emelyanov 		}
1962640708a2SPavel Emelyanov 		for (i = 0, vma = mm->mmap, pos = 2; vma;
1963640708a2SPavel Emelyanov 				vma = vma->vm_next) {
1964640708a2SPavel Emelyanov 			if (!vma->vm_file)
1965640708a2SPavel Emelyanov 				continue;
1966f0c3b509SAl Viro 			if (++pos <= ctx->pos)
1967640708a2SPavel Emelyanov 				continue;
1968640708a2SPavel Emelyanov 
19697b540d06SAl Viro 			info.mode = vma->vm_file->f_mode;
1970640708a2SPavel Emelyanov 			info.len = snprintf(info.name,
1971640708a2SPavel Emelyanov 					sizeof(info.name), "%lx-%lx",
1972640708a2SPavel Emelyanov 					vma->vm_start, vma->vm_end);
1973640708a2SPavel Emelyanov 			if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1974640708a2SPavel Emelyanov 				BUG();
1975640708a2SPavel Emelyanov 		}
1976640708a2SPavel Emelyanov 	}
1977640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1978640708a2SPavel Emelyanov 
1979640708a2SPavel Emelyanov 	for (i = 0; i < nr_files; i++) {
1980640708a2SPavel Emelyanov 		p = flex_array_get(fa, i);
1981f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
1982640708a2SPavel Emelyanov 				      p->name, p->len,
1983640708a2SPavel Emelyanov 				      proc_map_files_instantiate,
19847b540d06SAl Viro 				      task,
1985f0c3b509SAl Viro 				      (void *)(unsigned long)p->mode))
1986640708a2SPavel Emelyanov 			break;
1987f0c3b509SAl Viro 		ctx->pos++;
1988640708a2SPavel Emelyanov 	}
1989640708a2SPavel Emelyanov 	if (fa)
1990640708a2SPavel Emelyanov 		flex_array_free(fa);
1991640708a2SPavel Emelyanov 	mmput(mm);
1992640708a2SPavel Emelyanov 
1993640708a2SPavel Emelyanov out_put_task:
1994640708a2SPavel Emelyanov 	put_task_struct(task);
1995640708a2SPavel Emelyanov out:
1996640708a2SPavel Emelyanov 	return ret;
1997640708a2SPavel Emelyanov }
1998640708a2SPavel Emelyanov 
1999640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2000640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2001f0c3b509SAl Viro 	.iterate	= proc_map_files_readdir,
2002640708a2SPavel Emelyanov 	.llseek		= default_llseek,
2003640708a2SPavel Emelyanov };
2004640708a2SPavel Emelyanov 
200548f6a7a5SPavel Emelyanov struct timers_private {
200648f6a7a5SPavel Emelyanov 	struct pid *pid;
200748f6a7a5SPavel Emelyanov 	struct task_struct *task;
200848f6a7a5SPavel Emelyanov 	struct sighand_struct *sighand;
200957b8015eSPavel Emelyanov 	struct pid_namespace *ns;
201048f6a7a5SPavel Emelyanov 	unsigned long flags;
201148f6a7a5SPavel Emelyanov };
201248f6a7a5SPavel Emelyanov 
201348f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos)
201448f6a7a5SPavel Emelyanov {
201548f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
201648f6a7a5SPavel Emelyanov 
201748f6a7a5SPavel Emelyanov 	tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
201848f6a7a5SPavel Emelyanov 	if (!tp->task)
201948f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
202048f6a7a5SPavel Emelyanov 
202148f6a7a5SPavel Emelyanov 	tp->sighand = lock_task_sighand(tp->task, &tp->flags);
202248f6a7a5SPavel Emelyanov 	if (!tp->sighand)
202348f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
202448f6a7a5SPavel Emelyanov 
202548f6a7a5SPavel Emelyanov 	return seq_list_start(&tp->task->signal->posix_timers, *pos);
202648f6a7a5SPavel Emelyanov }
202748f6a7a5SPavel Emelyanov 
202848f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
202948f6a7a5SPavel Emelyanov {
203048f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
203148f6a7a5SPavel Emelyanov 	return seq_list_next(v, &tp->task->signal->posix_timers, pos);
203248f6a7a5SPavel Emelyanov }
203348f6a7a5SPavel Emelyanov 
203448f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v)
203548f6a7a5SPavel Emelyanov {
203648f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
203748f6a7a5SPavel Emelyanov 
203848f6a7a5SPavel Emelyanov 	if (tp->sighand) {
203948f6a7a5SPavel Emelyanov 		unlock_task_sighand(tp->task, &tp->flags);
204048f6a7a5SPavel Emelyanov 		tp->sighand = NULL;
204148f6a7a5SPavel Emelyanov 	}
204248f6a7a5SPavel Emelyanov 
204348f6a7a5SPavel Emelyanov 	if (tp->task) {
204448f6a7a5SPavel Emelyanov 		put_task_struct(tp->task);
204548f6a7a5SPavel Emelyanov 		tp->task = NULL;
204648f6a7a5SPavel Emelyanov 	}
204748f6a7a5SPavel Emelyanov }
204848f6a7a5SPavel Emelyanov 
204948f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v)
205048f6a7a5SPavel Emelyanov {
205148f6a7a5SPavel Emelyanov 	struct k_itimer *timer;
205257b8015eSPavel Emelyanov 	struct timers_private *tp = m->private;
205357b8015eSPavel Emelyanov 	int notify;
2054cedbccabSAlexey Dobriyan 	static const char * const nstr[] = {
205557b8015eSPavel Emelyanov 		[SIGEV_SIGNAL] = "signal",
205657b8015eSPavel Emelyanov 		[SIGEV_NONE] = "none",
205757b8015eSPavel Emelyanov 		[SIGEV_THREAD] = "thread",
205857b8015eSPavel Emelyanov 	};
205948f6a7a5SPavel Emelyanov 
206048f6a7a5SPavel Emelyanov 	timer = list_entry((struct list_head *)v, struct k_itimer, list);
206157b8015eSPavel Emelyanov 	notify = timer->it_sigev_notify;
206257b8015eSPavel Emelyanov 
206348f6a7a5SPavel Emelyanov 	seq_printf(m, "ID: %d\n", timer->it_id);
206457b8015eSPavel Emelyanov 	seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
206557b8015eSPavel Emelyanov 			timer->sigq->info.si_value.sival_ptr);
206657b8015eSPavel Emelyanov 	seq_printf(m, "notify: %s/%s.%d\n",
206757b8015eSPavel Emelyanov 		nstr[notify & ~SIGEV_THREAD_ID],
206857b8015eSPavel Emelyanov 		(notify & SIGEV_THREAD_ID) ? "tid" : "pid",
206957b8015eSPavel Emelyanov 		pid_nr_ns(timer->it_pid, tp->ns));
207015ef0298SPavel Tikhomirov 	seq_printf(m, "ClockID: %d\n", timer->it_clock);
207148f6a7a5SPavel Emelyanov 
207248f6a7a5SPavel Emelyanov 	return 0;
207348f6a7a5SPavel Emelyanov }
207448f6a7a5SPavel Emelyanov 
207548f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = {
207648f6a7a5SPavel Emelyanov 	.start	= timers_start,
207748f6a7a5SPavel Emelyanov 	.next	= timers_next,
207848f6a7a5SPavel Emelyanov 	.stop	= timers_stop,
207948f6a7a5SPavel Emelyanov 	.show	= show_timer,
208048f6a7a5SPavel Emelyanov };
208148f6a7a5SPavel Emelyanov 
208248f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file)
208348f6a7a5SPavel Emelyanov {
208448f6a7a5SPavel Emelyanov 	struct timers_private *tp;
208548f6a7a5SPavel Emelyanov 
208648f6a7a5SPavel Emelyanov 	tp = __seq_open_private(file, &proc_timers_seq_ops,
208748f6a7a5SPavel Emelyanov 			sizeof(struct timers_private));
208848f6a7a5SPavel Emelyanov 	if (!tp)
208948f6a7a5SPavel Emelyanov 		return -ENOMEM;
209048f6a7a5SPavel Emelyanov 
209148f6a7a5SPavel Emelyanov 	tp->pid = proc_pid(inode);
209257b8015eSPavel Emelyanov 	tp->ns = inode->i_sb->s_fs_info;
209348f6a7a5SPavel Emelyanov 	return 0;
209448f6a7a5SPavel Emelyanov }
209548f6a7a5SPavel Emelyanov 
209648f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = {
209748f6a7a5SPavel Emelyanov 	.open		= proc_timers_open,
209848f6a7a5SPavel Emelyanov 	.read		= seq_read,
209948f6a7a5SPavel Emelyanov 	.llseek		= seq_lseek,
210048f6a7a5SPavel Emelyanov 	.release	= seq_release_private,
210148f6a7a5SPavel Emelyanov };
2102640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2103640708a2SPavel Emelyanov 
2104c52a47acSAl Viro static int proc_pident_instantiate(struct inode *dir,
2105c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2106444ceed8SEric W. Biederman {
2107c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2108444ceed8SEric W. Biederman 	struct inode *inode;
2109444ceed8SEric W. Biederman 	struct proc_inode *ei;
2110444ceed8SEric W. Biederman 
211161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2112444ceed8SEric W. Biederman 	if (!inode)
2113444ceed8SEric W. Biederman 		goto out;
2114444ceed8SEric W. Biederman 
2115444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2116444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2117444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2118bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2119444ceed8SEric W. Biederman 	if (p->iop)
2120444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2121444ceed8SEric W. Biederman 	if (p->fop)
2122444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2123444ceed8SEric W. Biederman 	ei->op = p->op;
2124fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2125444ceed8SEric W. Biederman 	d_add(dentry, inode);
2126444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
21270b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2128c52a47acSAl Viro 		return 0;
2129444ceed8SEric W. Biederman out:
2130c52a47acSAl Viro 	return -ENOENT;
2131444ceed8SEric W. Biederman }
2132444ceed8SEric W. Biederman 
21331da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
21341da177e4SLinus Torvalds 					 struct dentry *dentry,
2135c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
21367bcd6b0eSEric W. Biederman 					 unsigned int nents)
21371da177e4SLinus Torvalds {
2138c52a47acSAl Viro 	int error;
213999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2140c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
21411da177e4SLinus Torvalds 
2142c52a47acSAl Viro 	error = -ENOENT;
21431da177e4SLinus Torvalds 
214499f89551SEric W. Biederman 	if (!task)
214599f89551SEric W. Biederman 		goto out_no_task;
21461da177e4SLinus Torvalds 
214720cdc894SEric W. Biederman 	/*
214820cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
214920cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
215020cdc894SEric W. Biederman 	 */
21517bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
21527bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
21531da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
21541da177e4SLinus Torvalds 			continue;
21551da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
21561da177e4SLinus Torvalds 			break;
21571da177e4SLinus Torvalds 	}
21587bcd6b0eSEric W. Biederman 	if (p > last)
21591da177e4SLinus Torvalds 		goto out;
21601da177e4SLinus Torvalds 
2161444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
21621da177e4SLinus Torvalds out:
216399f89551SEric W. Biederman 	put_task_struct(task);
216499f89551SEric W. Biederman out_no_task:
2165c52a47acSAl Viro 	return ERR_PTR(error);
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds 
2168f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2169c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
217028a6d671SEric W. Biederman {
2171f0c3b509SAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
2172f0c3b509SAl Viro 	const struct pid_entry *p;
217328a6d671SEric W. Biederman 
217428a6d671SEric W. Biederman 	if (!task)
2175f0c3b509SAl Viro 		return -ENOENT;
217628a6d671SEric W. Biederman 
2177f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
217828a6d671SEric W. Biederman 		goto out;
21791da177e4SLinus Torvalds 
2180f0c3b509SAl Viro 	if (ctx->pos >= nents + 2)
2181f0c3b509SAl Viro 		goto out;
2182f0c3b509SAl Viro 
2183f0c3b509SAl Viro 	for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2184f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, p->name, p->len,
2185f0c3b509SAl Viro 				proc_pident_instantiate, task, p))
2186f0c3b509SAl Viro 			break;
2187f0c3b509SAl Viro 		ctx->pos++;
2188f0c3b509SAl Viro 	}
218928a6d671SEric W. Biederman out:
219061a28784SEric W. Biederman 	put_task_struct(task);
2191f0c3b509SAl Viro 	return 0;
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
219528a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
219628a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
219728a6d671SEric W. Biederman {
2198496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
219904ff9708SAl Viro 	char *p = NULL;
220028a6d671SEric W. Biederman 	ssize_t length;
220128a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
220228a6d671SEric W. Biederman 
220328a6d671SEric W. Biederman 	if (!task)
220404ff9708SAl Viro 		return -ESRCH;
220528a6d671SEric W. Biederman 
220628a6d671SEric W. Biederman 	length = security_getprocattr(task,
22072fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
220804ff9708SAl Viro 				      &p);
220928a6d671SEric W. Biederman 	put_task_struct(task);
221004ff9708SAl Viro 	if (length > 0)
221104ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
221204ff9708SAl Viro 	kfree(p);
221328a6d671SEric W. Biederman 	return length;
221428a6d671SEric W. Biederman }
221528a6d671SEric W. Biederman 
221628a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
221728a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
221828a6d671SEric W. Biederman {
2219496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
222028a6d671SEric W. Biederman 	char *page;
222128a6d671SEric W. Biederman 	ssize_t length;
222228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
222328a6d671SEric W. Biederman 
222428a6d671SEric W. Biederman 	length = -ESRCH;
222528a6d671SEric W. Biederman 	if (!task)
222628a6d671SEric W. Biederman 		goto out_no_task;
222728a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
222828a6d671SEric W. Biederman 		count = PAGE_SIZE;
222928a6d671SEric W. Biederman 
223028a6d671SEric W. Biederman 	/* No partial writes. */
223128a6d671SEric W. Biederman 	length = -EINVAL;
223228a6d671SEric W. Biederman 	if (*ppos != 0)
223328a6d671SEric W. Biederman 		goto out;
223428a6d671SEric W. Biederman 
223528a6d671SEric W. Biederman 	length = -ENOMEM;
2236e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
223728a6d671SEric W. Biederman 	if (!page)
223828a6d671SEric W. Biederman 		goto out;
223928a6d671SEric W. Biederman 
224028a6d671SEric W. Biederman 	length = -EFAULT;
224128a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
224228a6d671SEric W. Biederman 		goto out_free;
224328a6d671SEric W. Biederman 
2244107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
22459b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2246107db7c7SDavid Howells 	if (length < 0)
2247107db7c7SDavid Howells 		goto out_free;
2248107db7c7SDavid Howells 
224928a6d671SEric W. Biederman 	length = security_setprocattr(task,
22502fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
225128a6d671SEric W. Biederman 				      (void*)page, count);
22529b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
225328a6d671SEric W. Biederman out_free:
225428a6d671SEric W. Biederman 	free_page((unsigned long) page);
225528a6d671SEric W. Biederman out:
225628a6d671SEric W. Biederman 	put_task_struct(task);
225728a6d671SEric W. Biederman out_no_task:
225828a6d671SEric W. Biederman 	return length;
225928a6d671SEric W. Biederman }
226028a6d671SEric W. Biederman 
226100977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
226228a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
226328a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
226487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
226528a6d671SEric W. Biederman };
226628a6d671SEric W. Biederman 
2267c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2268631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2269631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2270631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2271631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2272631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2273631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
227428a6d671SEric W. Biederman };
227528a6d671SEric W. Biederman 
2276f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
22771da177e4SLinus Torvalds {
2278f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
227972d9dcfcSEric W. Biederman 				   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22801da177e4SLinus Torvalds }
22811da177e4SLinus Torvalds 
228200977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
22831da177e4SLinus Torvalds 	.read		= generic_read_dir,
2284f0c3b509SAl Viro 	.iterate	= proc_attr_dir_readdir,
22856038f373SArnd Bergmann 	.llseek		= default_llseek,
22861da177e4SLinus Torvalds };
22871da177e4SLinus Torvalds 
228872d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
228900cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
22901da177e4SLinus Torvalds {
22917bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
22927bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22931da177e4SLinus Torvalds }
22941da177e4SLinus Torvalds 
2295c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
229672d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
229799f89551SEric W. Biederman 	.getattr	= pid_getattr,
22986d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
22991da177e4SLinus Torvalds };
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds #endif
23021da177e4SLinus Torvalds 
2303698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
23043cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
23053cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
23063cb4a0bbSKawai, Hidehiro {
2307496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
23083cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23093cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
23103cb4a0bbSKawai, Hidehiro 	size_t len;
23113cb4a0bbSKawai, Hidehiro 	int ret;
23123cb4a0bbSKawai, Hidehiro 
23133cb4a0bbSKawai, Hidehiro 	if (!task)
23143cb4a0bbSKawai, Hidehiro 		return -ESRCH;
23153cb4a0bbSKawai, Hidehiro 
23163cb4a0bbSKawai, Hidehiro 	ret = 0;
23173cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23183cb4a0bbSKawai, Hidehiro 	if (mm) {
23193cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
23203cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
23213cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
23223cb4a0bbSKawai, Hidehiro 		mmput(mm);
23233cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
23243cb4a0bbSKawai, Hidehiro 	}
23253cb4a0bbSKawai, Hidehiro 
23263cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23273cb4a0bbSKawai, Hidehiro 
23283cb4a0bbSKawai, Hidehiro 	return ret;
23293cb4a0bbSKawai, Hidehiro }
23303cb4a0bbSKawai, Hidehiro 
23313cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
23323cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
23333cb4a0bbSKawai, Hidehiro 					  size_t count,
23343cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
23353cb4a0bbSKawai, Hidehiro {
23363cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
23373cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23383cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
23393cb4a0bbSKawai, Hidehiro 	unsigned int val;
23403cb4a0bbSKawai, Hidehiro 	int ret;
23413cb4a0bbSKawai, Hidehiro 	int i;
23423cb4a0bbSKawai, Hidehiro 	unsigned long mask;
23433cb4a0bbSKawai, Hidehiro 
23443cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
23453cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
23463cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
23473cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
23483cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
23493cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23503cb4a0bbSKawai, Hidehiro 
23513cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
23523cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
23533cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
23543cb4a0bbSKawai, Hidehiro 		end++;
23553cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
23563cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23573cb4a0bbSKawai, Hidehiro 
23583cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
2359496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
23603cb4a0bbSKawai, Hidehiro 	if (!task)
23613cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23623cb4a0bbSKawai, Hidehiro 
23633cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
23643cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23653cb4a0bbSKawai, Hidehiro 	if (!mm)
23663cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
23673cb4a0bbSKawai, Hidehiro 
23683cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
23693cb4a0bbSKawai, Hidehiro 		if (val & mask)
23703cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23713cb4a0bbSKawai, Hidehiro 		else
23723cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23733cb4a0bbSKawai, Hidehiro 	}
23743cb4a0bbSKawai, Hidehiro 
23753cb4a0bbSKawai, Hidehiro 	mmput(mm);
23763cb4a0bbSKawai, Hidehiro  out_no_mm:
23773cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23783cb4a0bbSKawai, Hidehiro  out_no_task:
23793cb4a0bbSKawai, Hidehiro 	return ret;
23803cb4a0bbSKawai, Hidehiro }
23813cb4a0bbSKawai, Hidehiro 
23823cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
23833cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
23843cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
238587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
23863cb4a0bbSKawai, Hidehiro };
23873cb4a0bbSKawai, Hidehiro #endif
23883cb4a0bbSKawai, Hidehiro 
2389aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2390297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2391aba76fdbSAndrew Morton {
2392940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2393297c5d92SAndrea Righi 	unsigned long flags;
2394293eb1e7SVasiliy Kulikov 	int result;
2395297c5d92SAndrea Righi 
2396293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2397293eb1e7SVasiliy Kulikov 	if (result)
2398293eb1e7SVasiliy Kulikov 		return result;
2399293eb1e7SVasiliy Kulikov 
2400293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2401293eb1e7SVasiliy Kulikov 		result = -EACCES;
2402293eb1e7SVasiliy Kulikov 		goto out_unlock;
2403293eb1e7SVasiliy Kulikov 	}
24041d1221f3SVasiliy Kulikov 
24055995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2406b2d002dbSAndrea Righi 		struct task_struct *t = task;
2407297c5d92SAndrea Righi 
24085995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
24095995477aSAndrea Righi 		while_each_thread(task, t)
24105995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2411297c5d92SAndrea Righi 
2412297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2413297c5d92SAndrea Righi 	}
2414293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2415aba76fdbSAndrew Morton 			"rchar: %llu\n"
2416aba76fdbSAndrew Morton 			"wchar: %llu\n"
2417aba76fdbSAndrew Morton 			"syscr: %llu\n"
2418aba76fdbSAndrew Morton 			"syscw: %llu\n"
2419aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2420aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2421aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
24227c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
24237c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
24247c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
24257c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
24267c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
24277c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
24287c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2429293eb1e7SVasiliy Kulikov out_unlock:
2430293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2431293eb1e7SVasiliy Kulikov 	return result;
2432aba76fdbSAndrew Morton }
2433297c5d92SAndrea Righi 
2434297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2435297c5d92SAndrea Righi {
2436297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2437297c5d92SAndrea Righi }
2438297c5d92SAndrea Righi 
2439297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2440297c5d92SAndrea Righi {
2441297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2442297c5d92SAndrea Righi }
2443297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2444aba76fdbSAndrew Morton 
244522d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
244622d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
2447ccf94f1bSFabian Frederick 	const struct seq_operations *seq_ops)
244822d917d8SEric W. Biederman {
244922d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
245022d917d8SEric W. Biederman 	struct task_struct *task;
245122d917d8SEric W. Biederman 	struct seq_file *seq;
245222d917d8SEric W. Biederman 	int ret = -EINVAL;
245322d917d8SEric W. Biederman 
245422d917d8SEric W. Biederman 	task = get_proc_task(inode);
245522d917d8SEric W. Biederman 	if (task) {
245622d917d8SEric W. Biederman 		rcu_read_lock();
245722d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
245822d917d8SEric W. Biederman 		rcu_read_unlock();
245922d917d8SEric W. Biederman 		put_task_struct(task);
246022d917d8SEric W. Biederman 	}
246122d917d8SEric W. Biederman 	if (!ns)
246222d917d8SEric W. Biederman 		goto err;
246322d917d8SEric W. Biederman 
246422d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
246522d917d8SEric W. Biederman 	if (ret)
246622d917d8SEric W. Biederman 		goto err_put_ns;
246722d917d8SEric W. Biederman 
246822d917d8SEric W. Biederman 	seq = file->private_data;
246922d917d8SEric W. Biederman 	seq->private = ns;
247022d917d8SEric W. Biederman 
247122d917d8SEric W. Biederman 	return 0;
247222d917d8SEric W. Biederman err_put_ns:
247322d917d8SEric W. Biederman 	put_user_ns(ns);
247422d917d8SEric W. Biederman err:
247522d917d8SEric W. Biederman 	return ret;
247622d917d8SEric W. Biederman }
247722d917d8SEric W. Biederman 
247822d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
247922d917d8SEric W. Biederman {
248022d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
248122d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
248222d917d8SEric W. Biederman 	put_user_ns(ns);
248322d917d8SEric W. Biederman 	return seq_release(inode, file);
248422d917d8SEric W. Biederman }
248522d917d8SEric W. Biederman 
248622d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
248722d917d8SEric W. Biederman {
248822d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
248922d917d8SEric W. Biederman }
249022d917d8SEric W. Biederman 
249122d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
249222d917d8SEric W. Biederman {
249322d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
249422d917d8SEric W. Biederman }
249522d917d8SEric W. Biederman 
2496f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file)
2497f76d207aSEric W. Biederman {
2498f76d207aSEric W. Biederman 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2499f76d207aSEric W. Biederman }
2500f76d207aSEric W. Biederman 
250122d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
250222d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
250322d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
250422d917d8SEric W. Biederman 	.read		= seq_read,
250522d917d8SEric W. Biederman 	.llseek		= seq_lseek,
250622d917d8SEric W. Biederman 	.release	= proc_id_map_release,
250722d917d8SEric W. Biederman };
250822d917d8SEric W. Biederman 
250922d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
251022d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
251122d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
251222d917d8SEric W. Biederman 	.read		= seq_read,
251322d917d8SEric W. Biederman 	.llseek		= seq_lseek,
251422d917d8SEric W. Biederman 	.release	= proc_id_map_release,
251522d917d8SEric W. Biederman };
2516f76d207aSEric W. Biederman 
2517f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = {
2518f76d207aSEric W. Biederman 	.open		= proc_projid_map_open,
2519f76d207aSEric W. Biederman 	.write		= proc_projid_map_write,
2520f76d207aSEric W. Biederman 	.read		= seq_read,
2521f76d207aSEric W. Biederman 	.llseek		= seq_lseek,
2522f76d207aSEric W. Biederman 	.release	= proc_id_map_release,
2523f76d207aSEric W. Biederman };
252422d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
252522d917d8SEric W. Biederman 
252647830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
252747830723SKees Cook 				struct pid *pid, struct task_struct *task)
252847830723SKees Cook {
2529a9712bc1SAl Viro 	int err = lock_trace(task);
2530a9712bc1SAl Viro 	if (!err) {
253147830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2532a9712bc1SAl Viro 		unlock_trace(task);
2533a9712bc1SAl Viro 	}
2534a9712bc1SAl Viro 	return err;
253547830723SKees Cook }
253647830723SKees Cook 
2537801199ceSEric W. Biederman /*
253828a6d671SEric W. Biederman  * Thread groups
253928a6d671SEric W. Biederman  */
254000977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2541c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
254220cdc894SEric W. Biederman 
2543c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2544631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2545631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2546640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2547640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2548640708a2SPavel Emelyanov #endif
2549631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
25506b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2551b2211a36SAndrew Morton #ifdef CONFIG_NET
2552631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2553b2211a36SAndrew Morton #endif
2554631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2555f9ea536eSAlexey Dobriyan 	ONE("auxv",       S_IRUSR, proc_pid_auxv),
2556631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
255735a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
2558*1c963eb1SAlexey Dobriyan 	ONE("limits",	  S_IRUGO, proc_pid_limits),
255943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2560631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
256143ae34cbSIngo Molnar #endif
25625091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
25635091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
25645091faa4SMike Galbraith #endif
25654614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2566ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
256735a35046SDjalal Harouni 	INF("syscall",    S_IRUSR, proc_pid_syscall),
2568ebcb6734SRoland McGrath #endif
2569631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2570631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2571631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2572b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
257328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2574b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
257528a6d671SEric W. Biederman #endif
2576631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2577631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2578631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2579631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2580631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2581631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2582631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
25831e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2584631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2585b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
258632ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
258728a6d671SEric W. Biederman #endif
258828a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2589631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
259028a6d671SEric W. Biederman #endif
259128a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2592631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
259328a6d671SEric W. Biederman #endif
25942ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
259535a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
259628a6d671SEric W. Biederman #endif
259728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2598631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
259928a6d671SEric W. Biederman #endif
26009745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2601631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
26029745512cSArjan van de Ven #endif
26038793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2604631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
260528a6d671SEric W. Biederman #endif
2606a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2607631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2608a424316cSPaul Menage #endif
2609631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
2610fa0cbbf1SDavid Rientjes 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2611a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
261228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2613631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2614631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
261528a6d671SEric W. Biederman #endif
2616f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2617631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2618f4f154fdSAkinobu Mita #endif
2619698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2620631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
26213cb4a0bbSKawai, Hidehiro #endif
2622aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
26231d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
2624aba76fdbSAndrew Morton #endif
2625f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2626f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
2627f133eccaSChris Metcalf #endif
262822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
262922d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
263022d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2631f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
263222d917d8SEric W. Biederman #endif
263348f6a7a5SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
263448f6a7a5SPavel Emelyanov 	REG("timers",	  S_IRUGO, proc_timers_operations),
263548f6a7a5SPavel Emelyanov #endif
263628a6d671SEric W. Biederman };
263728a6d671SEric W. Biederman 
2638f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
263928a6d671SEric W. Biederman {
2640f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
264128a6d671SEric W. Biederman 				   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
264228a6d671SEric W. Biederman }
264328a6d671SEric W. Biederman 
264400977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
264528a6d671SEric W. Biederman 	.read		= generic_read_dir,
2646f0c3b509SAl Viro 	.iterate	= proc_tgid_base_readdir,
26476038f373SArnd Bergmann 	.llseek		= default_llseek,
264828a6d671SEric W. Biederman };
264928a6d671SEric W. Biederman 
265000cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
265100cd8dd3SAl Viro {
26527bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26537bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
265428a6d671SEric W. Biederman }
265528a6d671SEric W. Biederman 
2656c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
265728a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
265828a6d671SEric W. Biederman 	.getattr	= pid_getattr,
265928a6d671SEric W. Biederman 	.setattr	= proc_setattr,
26600499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
266128a6d671SEric W. Biederman };
266228a6d671SEric W. Biederman 
266360347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
26641da177e4SLinus Torvalds {
266548e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
26668578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
266748e6484dSEric W. Biederman 	struct qstr name;
26681da177e4SLinus Torvalds 
266948e6484dSEric W. Biederman 	name.name = buf;
267060347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
26714f522a24SAl Viro 	/* no ->d_hash() rejects on procfs */
267260347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
267348e6484dSEric W. Biederman 	if (dentry) {
267448e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
267548e6484dSEric W. Biederman 		d_drop(dentry);
267648e6484dSEric W. Biederman 		dput(dentry);
26771da177e4SLinus Torvalds 	}
26781da177e4SLinus Torvalds 
267948e6484dSEric W. Biederman 	name.name = buf;
268060347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
268160347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
268248e6484dSEric W. Biederman 	if (!leader)
268348e6484dSEric W. Biederman 		goto out;
268448e6484dSEric W. Biederman 
268548e6484dSEric W. Biederman 	name.name = "task";
268648e6484dSEric W. Biederman 	name.len = strlen(name.name);
268748e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
268848e6484dSEric W. Biederman 	if (!dir)
268948e6484dSEric W. Biederman 		goto out_put_leader;
269048e6484dSEric W. Biederman 
269148e6484dSEric W. Biederman 	name.name = buf;
269260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
269348e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
269448e6484dSEric W. Biederman 	if (dentry) {
269548e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
269648e6484dSEric W. Biederman 		d_drop(dentry);
269748e6484dSEric W. Biederman 		dput(dentry);
26981da177e4SLinus Torvalds 	}
269948e6484dSEric W. Biederman 
270048e6484dSEric W. Biederman 	dput(dir);
270148e6484dSEric W. Biederman out_put_leader:
270248e6484dSEric W. Biederman 	dput(leader);
270348e6484dSEric W. Biederman out:
270448e6484dSEric W. Biederman 	return;
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
27070895e91dSRandy Dunlap /**
27080895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
27090895e91dSRandy Dunlap  * @task: task that should be flushed.
27100895e91dSRandy Dunlap  *
27110895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
271260347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
27130895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
27140895e91dSRandy Dunlap  *
27150895e91dSRandy Dunlap  * Looks in the dcache for
27160895e91dSRandy Dunlap  * /proc/@pid
27170895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
27180895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
27190895e91dSRandy Dunlap  * from the dcache.
27200895e91dSRandy Dunlap  *
27210895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
27220895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
27230895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
27240895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
27250895e91dSRandy Dunlap  * dcache entries at process exit time.
27260895e91dSRandy Dunlap  *
27270895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
27280895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
27290895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
273060347f67SPavel Emelyanov  */
273160347f67SPavel Emelyanov 
273260347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
273360347f67SPavel Emelyanov {
27349fcc2d15SEric W. Biederman 	int i;
27359b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2736130f77ecSPavel Emelyanov 	struct upid *upid;
2737130f77ecSPavel Emelyanov 
2738130f77ecSPavel Emelyanov 	pid = task_pid(task);
2739130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
27409fcc2d15SEric W. Biederman 
27419fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2742130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2743130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
27449b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2745130f77ecSPavel Emelyanov 	}
274660347f67SPavel Emelyanov }
274760347f67SPavel Emelyanov 
2748c52a47acSAl Viro static int proc_pid_instantiate(struct inode *dir,
27499711ef99SAdrian Bunk 				   struct dentry * dentry,
2750c5141e6dSEric Dumazet 				   struct task_struct *task, const void *ptr)
2751444ceed8SEric W. Biederman {
2752444ceed8SEric W. Biederman 	struct inode *inode;
2753444ceed8SEric W. Biederman 
275461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2755444ceed8SEric W. Biederman 	if (!inode)
2756444ceed8SEric W. Biederman 		goto out;
2757444ceed8SEric W. Biederman 
2758444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2759444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2760444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2761444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2762aed54175SVegard Nossum 
2763bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2764bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
2765444ceed8SEric W. Biederman 
2766fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2767444ceed8SEric W. Biederman 
2768444ceed8SEric W. Biederman 	d_add(dentry, inode);
2769444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
27700b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2771c52a47acSAl Viro 		return 0;
2772444ceed8SEric W. Biederman out:
2773c52a47acSAl Viro 	return -ENOENT;
2774444ceed8SEric W. Biederman }
2775444ceed8SEric W. Biederman 
277600cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
27771da177e4SLinus Torvalds {
2778335eb531SAlexey Dobriyan 	int result = -ENOENT;
27791da177e4SLinus Torvalds 	struct task_struct *task;
27801da177e4SLinus Torvalds 	unsigned tgid;
2781b488893aSPavel Emelyanov 	struct pid_namespace *ns;
27821da177e4SLinus Torvalds 
2783dbcdb504SAlexey Dobriyan 	tgid = name_to_int(&dentry->d_name);
27841da177e4SLinus Torvalds 	if (tgid == ~0U)
27851da177e4SLinus Torvalds 		goto out;
27861da177e4SLinus Torvalds 
2787b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2788de758734SEric W. Biederman 	rcu_read_lock();
2789b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
27901da177e4SLinus Torvalds 	if (task)
27911da177e4SLinus Torvalds 		get_task_struct(task);
2792de758734SEric W. Biederman 	rcu_read_unlock();
27931da177e4SLinus Torvalds 	if (!task)
27941da177e4SLinus Torvalds 		goto out;
27951da177e4SLinus Torvalds 
2796444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
279748e6484dSEric W. Biederman 	put_task_struct(task);
27981da177e4SLinus Torvalds out:
2799c52a47acSAl Viro 	return ERR_PTR(result);
28001da177e4SLinus Torvalds }
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds /*
28030804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
28040bc58a91SEric W. Biederman  *
28051da177e4SLinus Torvalds  */
280619fd4bb2SEric W. Biederman struct tgid_iter {
280719fd4bb2SEric W. Biederman 	unsigned int tgid;
28080804ef4bSEric W. Biederman 	struct task_struct *task;
280919fd4bb2SEric W. Biederman };
281019fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
281119fd4bb2SEric W. Biederman {
28120804ef4bSEric W. Biederman 	struct pid *pid;
28131da177e4SLinus Torvalds 
281419fd4bb2SEric W. Biederman 	if (iter.task)
281519fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
28160804ef4bSEric W. Biederman 	rcu_read_lock();
28170804ef4bSEric W. Biederman retry:
281819fd4bb2SEric W. Biederman 	iter.task = NULL;
281919fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
28200804ef4bSEric W. Biederman 	if (pid) {
282119fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
282219fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
28230804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
28240804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
28250804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
28260804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
28270804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
28280804ef4bSEric W. Biederman 		 *
28290804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
28300804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
28310804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
28320804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
28330804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
28340bc58a91SEric W. Biederman 		 */
283519fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
283619fd4bb2SEric W. Biederman 			iter.tgid += 1;
28370804ef4bSEric W. Biederman 			goto retry;
283819fd4bb2SEric W. Biederman 		}
283919fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
28401da177e4SLinus Torvalds 	}
2841454cc105SEric W. Biederman 	rcu_read_unlock();
284219fd4bb2SEric W. Biederman 	return iter;
28431da177e4SLinus Torvalds }
28441da177e4SLinus Torvalds 
2845021ada7dSAl Viro #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
2848f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx)
28491da177e4SLinus Torvalds {
285019fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2851db963164SAl Viro 	struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2852f0c3b509SAl Viro 	loff_t pos = ctx->pos;
28531da177e4SLinus Torvalds 
2854021ada7dSAl Viro 	if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2855f0c3b509SAl Viro 		return 0;
28561da177e4SLinus Torvalds 
2857021ada7dSAl Viro 	if (pos == TGID_OFFSET - 1) {
2858db963164SAl Viro 		struct inode *inode = ns->proc_self->d_inode;
2859db963164SAl Viro 		if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2860f0c3b509SAl Viro 			return 0;
2861021ada7dSAl Viro 		iter.tgid = 0;
2862021ada7dSAl Viro 	} else {
2863021ada7dSAl Viro 		iter.tgid = pos - TGID_OFFSET;
2864021ada7dSAl Viro 	}
286519fd4bb2SEric W. Biederman 	iter.task = NULL;
286619fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
286719fd4bb2SEric W. Biederman 	     iter.task;
286819fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
2869f0c3b509SAl Viro 		char name[PROC_NUMBUF];
2870f0c3b509SAl Viro 		int len;
2871f0c3b509SAl Viro 		if (!has_pid_permissions(ns, iter.task, 2))
2872f0c3b509SAl Viro 			continue;
28730499680aSVasiliy Kulikov 
2874f0c3b509SAl Viro 		len = snprintf(name, sizeof(name), "%d", iter.tgid);
2875f0c3b509SAl Viro 		ctx->pos = iter.tgid + TGID_OFFSET;
2876f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
2877f0c3b509SAl Viro 				     proc_pid_instantiate, iter.task, NULL)) {
287819fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
2879f0c3b509SAl Viro 			return 0;
28801da177e4SLinus Torvalds 		}
28811da177e4SLinus Torvalds 	}
2882f0c3b509SAl Viro 	ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
28831da177e4SLinus Torvalds 	return 0;
28841da177e4SLinus Torvalds }
28851da177e4SLinus Torvalds 
28860bc58a91SEric W. Biederman /*
288728a6d671SEric W. Biederman  * Tasks
288828a6d671SEric W. Biederman  */
2889c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
2890631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
28913835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
28926b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2893631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
2894f9ea536eSAlexey Dobriyan 	ONE("auxv",      S_IRUSR, proc_pid_auxv),
2895631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
289635a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
2897*1c963eb1SAlexey Dobriyan 	ONE("limits",	 S_IRUGO, proc_pid_limits),
289843ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2899631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
290043ae34cbSIngo Molnar #endif
29014614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2902ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
290335a35046SDjalal Harouni 	INF("syscall",   S_IRUSR, proc_pid_syscall),
2904ebcb6734SRoland McGrath #endif
2905631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
2906631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
2907631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
2908b7643757SSiddhesh Poyarekar 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
290981841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
291081841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
291181841161SCyrill Gorcunov #endif
291228a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2913b7643757SSiddhesh Poyarekar 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
291428a6d671SEric W. Biederman #endif
2915631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
2916631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
2917631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
2918631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
2919631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
2920631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
29211e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2922631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2923b7643757SSiddhesh Poyarekar 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
292432ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
292528a6d671SEric W. Biederman #endif
292628a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2927631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
292828a6d671SEric W. Biederman #endif
292928a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2930631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
293128a6d671SEric W. Biederman #endif
29322ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
293335a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
293428a6d671SEric W. Biederman #endif
293528a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2936631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
293728a6d671SEric W. Biederman #endif
29389745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2939631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
29409745512cSArjan van de Ven #endif
29418793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2942631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
294328a6d671SEric W. Biederman #endif
2944a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2945631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2946a424316cSPaul Menage #endif
2947631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
2948fa0cbbf1SDavid Rientjes 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2949a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
295028a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2951631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
295226ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
295328a6d671SEric W. Biederman #endif
2954f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2955631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2956f4f154fdSAkinobu Mita #endif
2957297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
29581d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
2959297c5d92SAndrea Righi #endif
2960f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2961f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
2962f133eccaSChris Metcalf #endif
296322d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
296422d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
296522d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2966f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
296722d917d8SEric W. Biederman #endif
296828a6d671SEric W. Biederman };
296928a6d671SEric W. Biederman 
2970f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
297128a6d671SEric W. Biederman {
2972f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
297328a6d671SEric W. Biederman 				   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
297428a6d671SEric W. Biederman }
297528a6d671SEric W. Biederman 
297600cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
297700cd8dd3SAl Viro {
29787bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
29797bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
298028a6d671SEric W. Biederman }
298128a6d671SEric W. Biederman 
298200977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
298328a6d671SEric W. Biederman 	.read		= generic_read_dir,
2984f0c3b509SAl Viro 	.iterate	= proc_tid_base_readdir,
29856038f373SArnd Bergmann 	.llseek		= default_llseek,
298628a6d671SEric W. Biederman };
298728a6d671SEric W. Biederman 
2988c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
298928a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
299028a6d671SEric W. Biederman 	.getattr	= pid_getattr,
299128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
299228a6d671SEric W. Biederman };
299328a6d671SEric W. Biederman 
2994c52a47acSAl Viro static int proc_task_instantiate(struct inode *dir,
2995c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2996444ceed8SEric W. Biederman {
2997444ceed8SEric W. Biederman 	struct inode *inode;
299861a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2999444ceed8SEric W. Biederman 
3000444ceed8SEric W. Biederman 	if (!inode)
3001444ceed8SEric W. Biederman 		goto out;
3002444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3003444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3004444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3005444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3006aed54175SVegard Nossum 
3007bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3008bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3009444ceed8SEric W. Biederman 
3010fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3011444ceed8SEric W. Biederman 
3012444ceed8SEric W. Biederman 	d_add(dentry, inode);
3013444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
30140b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3015c52a47acSAl Viro 		return 0;
3016444ceed8SEric W. Biederman out:
3017c52a47acSAl Viro 	return -ENOENT;
3018444ceed8SEric W. Biederman }
3019444ceed8SEric W. Biederman 
302000cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
302128a6d671SEric W. Biederman {
3022c52a47acSAl Viro 	int result = -ENOENT;
302328a6d671SEric W. Biederman 	struct task_struct *task;
302428a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
302528a6d671SEric W. Biederman 	unsigned tid;
3026b488893aSPavel Emelyanov 	struct pid_namespace *ns;
302728a6d671SEric W. Biederman 
302828a6d671SEric W. Biederman 	if (!leader)
302928a6d671SEric W. Biederman 		goto out_no_task;
303028a6d671SEric W. Biederman 
3031dbcdb504SAlexey Dobriyan 	tid = name_to_int(&dentry->d_name);
303228a6d671SEric W. Biederman 	if (tid == ~0U)
303328a6d671SEric W. Biederman 		goto out;
303428a6d671SEric W. Biederman 
3035b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
303628a6d671SEric W. Biederman 	rcu_read_lock();
3037b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
303828a6d671SEric W. Biederman 	if (task)
303928a6d671SEric W. Biederman 		get_task_struct(task);
304028a6d671SEric W. Biederman 	rcu_read_unlock();
304128a6d671SEric W. Biederman 	if (!task)
304228a6d671SEric W. Biederman 		goto out;
3043bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
304428a6d671SEric W. Biederman 		goto out_drop_task;
304528a6d671SEric W. Biederman 
3046444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
304728a6d671SEric W. Biederman out_drop_task:
304828a6d671SEric W. Biederman 	put_task_struct(task);
304928a6d671SEric W. Biederman out:
305028a6d671SEric W. Biederman 	put_task_struct(leader);
305128a6d671SEric W. Biederman out_no_task:
3052c52a47acSAl Viro 	return ERR_PTR(result);
305328a6d671SEric W. Biederman }
305428a6d671SEric W. Biederman 
305528a6d671SEric W. Biederman /*
30560bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
30570bc58a91SEric W. Biederman  *
30580bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
30590bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
30600bc58a91SEric W. Biederman  * directory we have more work todo.
30610bc58a91SEric W. Biederman  *
30620bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
30630bc58a91SEric W. Biederman  *
30640bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
30650bc58a91SEric W. Biederman  * threads past it.
30660bc58a91SEric W. Biederman  */
30679f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
30689f6e963fSOleg Nesterov 					struct pid_namespace *ns)
30690bc58a91SEric W. Biederman {
3070d855a4b7SOleg Nesterov 	struct task_struct *pos, *task;
30719f6e963fSOleg Nesterov 	unsigned long nr = f_pos;
30729f6e963fSOleg Nesterov 
30739f6e963fSOleg Nesterov 	if (nr != f_pos)	/* 32bit overflow? */
30749f6e963fSOleg Nesterov 		return NULL;
30750bc58a91SEric W. Biederman 
3076cc288738SEric W. Biederman 	rcu_read_lock();
3077d855a4b7SOleg Nesterov 	task = pid_task(pid, PIDTYPE_PID);
3078d855a4b7SOleg Nesterov 	if (!task)
3079d855a4b7SOleg Nesterov 		goto fail;
3080d855a4b7SOleg Nesterov 
3081d855a4b7SOleg Nesterov 	/* Attempt to start with the tid of a thread */
30829f6e963fSOleg Nesterov 	if (tid && nr) {
3083b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3084d855a4b7SOleg Nesterov 		if (pos && same_thread_group(pos, task))
3085a872ff0cSOleg Nesterov 			goto found;
30860bc58a91SEric W. Biederman 	}
30870bc58a91SEric W. Biederman 
30880bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
30899f6e963fSOleg Nesterov 	if (nr >= get_nr_threads(task))
3090c986c14aSOleg Nesterov 		goto fail;
3091a872ff0cSOleg Nesterov 
3092a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3093a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3094a872ff0cSOleg Nesterov 	 */
3095d855a4b7SOleg Nesterov 	pos = task = task->group_leader;
3096c986c14aSOleg Nesterov 	do {
30979f6e963fSOleg Nesterov 		if (!nr--)
3098c986c14aSOleg Nesterov 			goto found;
3099d855a4b7SOleg Nesterov 	} while_each_thread(task, pos);
3100c986c14aSOleg Nesterov fail:
3101a872ff0cSOleg Nesterov 	pos = NULL;
3102a872ff0cSOleg Nesterov 	goto out;
3103a872ff0cSOleg Nesterov found:
3104a872ff0cSOleg Nesterov 	get_task_struct(pos);
3105a872ff0cSOleg Nesterov out:
3106cc288738SEric W. Biederman 	rcu_read_unlock();
31070bc58a91SEric W. Biederman 	return pos;
31080bc58a91SEric W. Biederman }
31090bc58a91SEric W. Biederman 
31100bc58a91SEric W. Biederman /*
31110bc58a91SEric W. Biederman  * Find the next thread in the thread list.
31120bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
31130bc58a91SEric W. Biederman  *
31140bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
31150bc58a91SEric W. Biederman  */
31160bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
31170bc58a91SEric W. Biederman {
3118c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3119cc288738SEric W. Biederman 	rcu_read_lock();
3120c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
31210bc58a91SEric W. Biederman 		pos = next_thread(start);
3122c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
31230bc58a91SEric W. Biederman 			pos = NULL;
3124c1df7fb8SOleg Nesterov 		else
3125c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3126c1df7fb8SOleg Nesterov 	}
3127cc288738SEric W. Biederman 	rcu_read_unlock();
31280bc58a91SEric W. Biederman 	put_task_struct(start);
31290bc58a91SEric W. Biederman 	return pos;
31300bc58a91SEric W. Biederman }
31310bc58a91SEric W. Biederman 
31321da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
3133f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx)
31341da177e4SLinus Torvalds {
3135d855a4b7SOleg Nesterov 	struct inode *inode = file_inode(file);
3136d855a4b7SOleg Nesterov 	struct task_struct *task;
3137b488893aSPavel Emelyanov 	struct pid_namespace *ns;
3138f0c3b509SAl Viro 	int tid;
31391da177e4SLinus Torvalds 
3140d855a4b7SOleg Nesterov 	if (proc_inode_is_dead(inode))
3141f0c3b509SAl Viro 		return -ENOENT;
31421da177e4SLinus Torvalds 
3143f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
3144d855a4b7SOleg Nesterov 		return 0;
31451da177e4SLinus Torvalds 
31460bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
31470bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
31480bc58a91SEric W. Biederman 	 */
3149f0c3b509SAl Viro 	ns = file->f_dentry->d_sb->s_fs_info;
3150f0c3b509SAl Viro 	tid = (int)file->f_version;
3151f0c3b509SAl Viro 	file->f_version = 0;
3152d855a4b7SOleg Nesterov 	for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
31530bc58a91SEric W. Biederman 	     task;
3154f0c3b509SAl Viro 	     task = next_tid(task), ctx->pos++) {
3155f0c3b509SAl Viro 		char name[PROC_NUMBUF];
3156f0c3b509SAl Viro 		int len;
3157b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
3158f0c3b509SAl Viro 		len = snprintf(name, sizeof(name), "%d", tid);
3159f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
3160f0c3b509SAl Viro 				proc_task_instantiate, task, NULL)) {
31610bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
31620bc58a91SEric W. Biederman 			 * pid for the next readir call */
3163f0c3b509SAl Viro 			file->f_version = (u64)tid;
31640bc58a91SEric W. Biederman 			put_task_struct(task);
31651da177e4SLinus Torvalds 			break;
31660bc58a91SEric W. Biederman 		}
31671da177e4SLinus Torvalds 	}
3168d855a4b7SOleg Nesterov 
3169f0c3b509SAl Viro 	return 0;
31701da177e4SLinus Torvalds }
31716e66b52bSEric W. Biederman 
31726e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
31736e66b52bSEric W. Biederman {
31746e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
317599f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
31766e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
31776e66b52bSEric W. Biederman 
317899f89551SEric W. Biederman 	if (p) {
317999f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
318099f89551SEric W. Biederman 		put_task_struct(p);
31816e66b52bSEric W. Biederman 	}
31826e66b52bSEric W. Biederman 
31836e66b52bSEric W. Biederman 	return 0;
31846e66b52bSEric W. Biederman }
318528a6d671SEric W. Biederman 
3186c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
318728a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
318828a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
318928a6d671SEric W. Biederman 	.setattr	= proc_setattr,
31900499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
319128a6d671SEric W. Biederman };
319228a6d671SEric W. Biederman 
319300977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
319428a6d671SEric W. Biederman 	.read		= generic_read_dir,
3195f0c3b509SAl Viro 	.iterate	= proc_task_readdir,
31966038f373SArnd Bergmann 	.llseek		= default_llseek,
319728a6d671SEric W. Biederman };
3198