xref: /openbmc/linux/fs/proc/base.c (revision 00cd8dd3bf95f2cc8435b4cac01d9995635c6d0b)
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>
76a424316cSPaul Menage #include <linux/cgroup.h>
771da177e4SLinus Torvalds #include <linux/cpuset.h>
781da177e4SLinus Torvalds #include <linux/audit.h>
795addc5ddSAl Viro #include <linux/poll.h>
801651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
818ac773b4SAlexey Dobriyan #include <linux/oom.h>
823cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8360347f67SPavel Emelyanov #include <linux/pid_namespace.h>
8422d917d8SEric W. Biederman #include <linux/user_namespace.h>
855ad4e53bSAl Viro #include <linux/fs_struct.h>
865a0e3ad6STejun Heo #include <linux/slab.h>
87640708a2SPavel Emelyanov #include <linux/flex_array.h>
88f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
89f133eccaSChris Metcalf #include <asm/hardwall.h>
90f133eccaSChris Metcalf #endif
9143d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
921da177e4SLinus Torvalds #include "internal.h"
931da177e4SLinus Torvalds 
940f2fe20fSEric W. Biederman /* NOTE:
950f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
960f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
970f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
980f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
990f2fe20fSEric W. Biederman  *
1000f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
1010f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
1020f2fe20fSEric W. Biederman  */
1030f2fe20fSEric W. Biederman 
1041da177e4SLinus Torvalds struct pid_entry {
1051da177e4SLinus Torvalds 	char *name;
106c5141e6dSEric Dumazet 	int len;
107d161a13fSAl Viro 	umode_t mode;
108c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10900977a59SArjan van de Ven 	const struct file_operations *fop;
11020cdc894SEric W. Biederman 	union proc_op op;
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
11361a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
11420cdc894SEric W. Biederman 	.name = (NAME),					\
115c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11620cdc894SEric W. Biederman 	.mode = MODE,					\
11720cdc894SEric W. Biederman 	.iop  = IOP,					\
11820cdc894SEric W. Biederman 	.fop  = FOP,					\
11920cdc894SEric W. Biederman 	.op   = OP,					\
12020cdc894SEric W. Biederman }
12120cdc894SEric W. Biederman 
122631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
123631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
124631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
12561a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12620cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
127631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
128631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
129631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
130631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
13161a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
13220cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
133631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
134631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
135be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
136be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
137631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1381da177e4SLinus Torvalds 
139640708a2SPavel Emelyanov static int proc_fd_permission(struct inode *inode, int mask);
140640708a2SPavel Emelyanov 
141aed54175SVegard Nossum /*
142aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
143aed54175SVegard Nossum  * and .. links.
144aed54175SVegard Nossum  */
145aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
146aed54175SVegard Nossum 	unsigned int n)
147aed54175SVegard Nossum {
148aed54175SVegard Nossum 	unsigned int i;
149aed54175SVegard Nossum 	unsigned int count;
150aed54175SVegard Nossum 
151aed54175SVegard Nossum 	count = 0;
152aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
153aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
154aed54175SVegard Nossum 			++count;
155aed54175SVegard Nossum 	}
156aed54175SVegard Nossum 
157aed54175SVegard Nossum 	return count;
158aed54175SVegard Nossum }
159aed54175SVegard Nossum 
160f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1611da177e4SLinus Torvalds {
1627c2c7d99SHugh Dickins 	int result = -ENOENT;
1637c2c7d99SHugh Dickins 
1640494f6ecSMiklos Szeredi 	task_lock(task);
165f7ad3c6bSMiklos Szeredi 	if (task->fs) {
166f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1677c2c7d99SHugh Dickins 		result = 0;
1687c2c7d99SHugh Dickins 	}
1690494f6ecSMiklos Szeredi 	task_unlock(task);
1707c2c7d99SHugh Dickins 	return result;
1710494f6ecSMiklos Szeredi }
1720494f6ecSMiklos Szeredi 
1737773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1740494f6ecSMiklos Szeredi {
1757773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1760494f6ecSMiklos Szeredi 	int result = -ENOENT;
17799f89551SEric W. Biederman 
17899f89551SEric W. Biederman 	if (task) {
179f7ad3c6bSMiklos Szeredi 		task_lock(task);
180f7ad3c6bSMiklos Szeredi 		if (task->fs) {
181f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
182f7ad3c6bSMiklos Szeredi 			result = 0;
183f7ad3c6bSMiklos Szeredi 		}
184f7ad3c6bSMiklos Szeredi 		task_unlock(task);
18599f89551SEric W. Biederman 		put_task_struct(task);
18699f89551SEric W. Biederman 	}
1871da177e4SLinus Torvalds 	return result;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1907773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
1911da177e4SLinus Torvalds {
1927773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1931da177e4SLinus Torvalds 	int result = -ENOENT;
19499f89551SEric W. Biederman 
19599f89551SEric W. Biederman 	if (task) {
196f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
19799f89551SEric W. Biederman 		put_task_struct(task);
19899f89551SEric W. Biederman 	}
1991da177e4SLinus Torvalds 	return result;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	int res = 0;
2051da177e4SLinus Torvalds 	unsigned int len;
2061da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2071da177e4SLinus Torvalds 	if (!mm)
2081da177e4SLinus Torvalds 		goto out;
2091da177e4SLinus Torvalds 	if (!mm->arg_end)
2101da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2151da177e4SLinus Torvalds 		len = PAGE_SIZE;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2201da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2211da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2221da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2231da177e4SLinus Torvalds 		if (len < res) {
2241da177e4SLinus Torvalds 		    res = len;
2251da177e4SLinus Torvalds 		} else {
2261da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2271da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2281da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2291da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2301da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2311da177e4SLinus Torvalds 		}
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds out_mm:
2341da177e4SLinus Torvalds 	mmput(mm);
2351da177e4SLinus Torvalds out:
2361da177e4SLinus Torvalds 	return res;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2401da177e4SLinus Torvalds {
241e7dcd999SCong Wang 	struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
2422fadaef4SAl Viro 	int res = PTR_ERR(mm);
2432fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
2441da177e4SLinus Torvalds 		unsigned int nwords = 0;
245dfe6b7d9SHannes Eder 		do {
2461da177e4SLinus Torvalds 			nwords += 2;
247dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2481da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2491da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2501da177e4SLinus Torvalds 			res = PAGE_SIZE;
2511da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2521da177e4SLinus Torvalds 		mmput(mm);
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	return res;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2591da177e4SLinus Torvalds /*
2601da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2611da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2621da177e4SLinus Torvalds  */
2631da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
2641da177e4SLinus Torvalds {
265ffb45122SAlexey Dobriyan 	unsigned long wchan;
2669281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	wchan = get_wchan(task);
2691da177e4SLinus Torvalds 
2709d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
271f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
272f83ce3e6SJake Edge 			return 0;
273f83ce3e6SJake Edge 		else
2741da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
2759d65cb4aSAlexey Dobriyan 	else
2769d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2791da177e4SLinus Torvalds 
280a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
281a9712bc1SAl Viro {
282a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
283a9712bc1SAl Viro 	if (err)
284a9712bc1SAl Viro 		return err;
285a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
286a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
287a9712bc1SAl Viro 		return -EPERM;
288a9712bc1SAl Viro 	}
289a9712bc1SAl Viro 	return 0;
290a9712bc1SAl Viro }
291a9712bc1SAl Viro 
292a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
293a9712bc1SAl Viro {
294a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
295a9712bc1SAl Viro }
296a9712bc1SAl Viro 
2972ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
2982ec220e2SKen Chen 
2992ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3002ec220e2SKen Chen 
3012ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3022ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3032ec220e2SKen Chen {
3042ec220e2SKen Chen 	struct stack_trace trace;
3052ec220e2SKen Chen 	unsigned long *entries;
306a9712bc1SAl Viro 	int err;
3072ec220e2SKen Chen 	int i;
3082ec220e2SKen Chen 
3092ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3102ec220e2SKen Chen 	if (!entries)
3112ec220e2SKen Chen 		return -ENOMEM;
3122ec220e2SKen Chen 
3132ec220e2SKen Chen 	trace.nr_entries	= 0;
3142ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3152ec220e2SKen Chen 	trace.entries		= entries;
3162ec220e2SKen Chen 	trace.skip		= 0;
317a9712bc1SAl Viro 
318a9712bc1SAl Viro 	err = lock_trace(task);
319a9712bc1SAl Viro 	if (!err) {
3202ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
3212ec220e2SKen Chen 
3222ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
32351e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
3242ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
3252ec220e2SKen Chen 		}
326a9712bc1SAl Viro 		unlock_trace(task);
327a9712bc1SAl Viro 	}
3282ec220e2SKen Chen 	kfree(entries);
3292ec220e2SKen Chen 
330a9712bc1SAl Viro 	return err;
3312ec220e2SKen Chen }
3322ec220e2SKen Chen #endif
3332ec220e2SKen Chen 
3341da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3351da177e4SLinus Torvalds /*
3361da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3371da177e4SLinus Torvalds  */
3381da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3391da177e4SLinus Torvalds {
340172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
341826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
342826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3432d72376bSIngo Molnar 			task->sched_info.pcount);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds #endif
3461da177e4SLinus Torvalds 
3479745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3489745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3499745512cSArjan van de Ven {
3509745512cSArjan van de Ven 	int i;
35113d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
35213d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3539745512cSArjan van de Ven 
35413d77c37SHiroshi Shimamoto 	if (!task)
35513d77c37SHiroshi Shimamoto 		return -ESRCH;
35613d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3579745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
35834e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
35934e49d4fSJoe Perches 		if (lr->backtrace[0]) {
3609745512cSArjan van de Ven 			int q;
3619745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
36234e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
3639745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
36434e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
36534e49d4fSJoe Perches 				if (!bt)
3669745512cSArjan van de Ven 					break;
36734e49d4fSJoe Perches 				if (bt == ULONG_MAX)
3689745512cSArjan van de Ven 					break;
36934e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
3709745512cSArjan van de Ven 			}
3719d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
3729745512cSArjan van de Ven 		}
3739745512cSArjan van de Ven 
3749745512cSArjan van de Ven 	}
37513d77c37SHiroshi Shimamoto 	put_task_struct(task);
3769745512cSArjan van de Ven 	return 0;
3779745512cSArjan van de Ven }
3789745512cSArjan van de Ven 
3799745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3809745512cSArjan van de Ven {
38113d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
382d6643d12SHiroshi Shimamoto }
383d6643d12SHiroshi Shimamoto 
3849745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3859745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3869745512cSArjan van de Ven {
38713d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
3889745512cSArjan van de Ven 
38913d77c37SHiroshi Shimamoto 	if (!task)
39013d77c37SHiroshi Shimamoto 		return -ESRCH;
3919745512cSArjan van de Ven 	clear_all_latency_tracing(task);
39213d77c37SHiroshi Shimamoto 	put_task_struct(task);
3939745512cSArjan van de Ven 
3949745512cSArjan van de Ven 	return count;
3959745512cSArjan van de Ven }
3969745512cSArjan van de Ven 
3979745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3989745512cSArjan van de Ven 	.open		= lstats_open,
3999745512cSArjan van de Ven 	.read		= seq_read,
4009745512cSArjan van de Ven 	.write		= lstats_write,
4019745512cSArjan van de Ven 	.llseek		= seq_lseek,
40213d77c37SHiroshi Shimamoto 	.release	= single_release,
4039745512cSArjan van de Ven };
4049745512cSArjan van de Ven 
4059745512cSArjan van de Ven #endif
4069745512cSArjan van de Ven 
4071da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4081da177e4SLinus Torvalds {
409a7f638f9SDavid Rientjes 	unsigned long totalpages = totalram_pages + total_swap_pages;
410b95c35e7SOleg Nesterov 	unsigned long points = 0;
4111da177e4SLinus Torvalds 
41219c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
413b95c35e7SOleg Nesterov 	if (pid_alive(task))
414a7f638f9SDavid Rientjes 		points = oom_badness(task, NULL, NULL, totalpages) *
415a7f638f9SDavid Rientjes 						1000 / totalpages;
41619c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4171da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
420d85f50d5SNeil Horman struct limit_names {
421d85f50d5SNeil Horman 	char *name;
422d85f50d5SNeil Horman 	char *unit;
423d85f50d5SNeil Horman };
424d85f50d5SNeil Horman 
425d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
426cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
427d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
428d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
429d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
430d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
431d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
432d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
433d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
434d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
435d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
436d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
437d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
438d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
439d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
440d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4418808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
442d85f50d5SNeil Horman };
443d85f50d5SNeil Horman 
444d85f50d5SNeil Horman /* Display limits for a process */
445d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
446d85f50d5SNeil Horman {
447d85f50d5SNeil Horman 	unsigned int i;
448d85f50d5SNeil Horman 	int count = 0;
449d85f50d5SNeil Horman 	unsigned long flags;
450d85f50d5SNeil Horman 	char *bufptr = buffer;
451d85f50d5SNeil Horman 
452d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
453d85f50d5SNeil Horman 
454a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
455d85f50d5SNeil Horman 		return 0;
456d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
457d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
458d85f50d5SNeil Horman 
459d85f50d5SNeil Horman 	/*
460d85f50d5SNeil Horman 	 * print the file header
461d85f50d5SNeil Horman 	 */
462d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
463d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
464d85f50d5SNeil Horman 
465d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
466d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
467d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
468d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
469d85f50d5SNeil Horman 		else
470d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
471d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
472d85f50d5SNeil Horman 
473d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
474d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
475d85f50d5SNeil Horman 		else
476d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
477d85f50d5SNeil Horman 					 rlim[i].rlim_max);
478d85f50d5SNeil Horman 
479d85f50d5SNeil Horman 		if (lnames[i].unit)
480d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
481d85f50d5SNeil Horman 					 lnames[i].unit);
482d85f50d5SNeil Horman 		else
483d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
484d85f50d5SNeil Horman 	}
485d85f50d5SNeil Horman 
486d85f50d5SNeil Horman 	return count;
487d85f50d5SNeil Horman }
488d85f50d5SNeil Horman 
489ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
490ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
491ebcb6734SRoland McGrath {
492ebcb6734SRoland McGrath 	long nr;
493ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
494a9712bc1SAl Viro 	int res = lock_trace(task);
495a9712bc1SAl Viro 	if (res)
496a9712bc1SAl Viro 		return res;
497ebcb6734SRoland McGrath 
498ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
499a9712bc1SAl Viro 		res = sprintf(buffer, "running\n");
500a9712bc1SAl Viro 	else if (nr < 0)
501a9712bc1SAl Viro 		res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
502a9712bc1SAl Viro 	else
503a9712bc1SAl Viro 		res = sprintf(buffer,
504ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
505ebcb6734SRoland McGrath 		       nr,
506ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
507ebcb6734SRoland McGrath 		       sp, pc);
508a9712bc1SAl Viro 	unlock_trace(task);
509a9712bc1SAl Viro 	return res;
510ebcb6734SRoland McGrath }
511ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
512ebcb6734SRoland McGrath 
5131da177e4SLinus Torvalds /************************************************************************/
5141da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5151da177e4SLinus Torvalds /************************************************************************/
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds /* permission checks */
518778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5191da177e4SLinus Torvalds {
520778c1144SEric W. Biederman 	struct task_struct *task;
521778c1144SEric W. Biederman 	int allowed = 0;
522df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
523df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
524df26c40eSEric W. Biederman 	 * information.
525778c1144SEric W. Biederman 	 */
526778c1144SEric W. Biederman 	task = get_proc_task(inode);
527df26c40eSEric W. Biederman 	if (task) {
528006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
529778c1144SEric W. Biederman 		put_task_struct(task);
530df26c40eSEric W. Biederman 	}
531778c1144SEric W. Biederman 	return allowed;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
5346b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
5356d76fa58SLinus Torvalds {
5366d76fa58SLinus Torvalds 	int error;
5376d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5386d76fa58SLinus Torvalds 
5396d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5406d76fa58SLinus Torvalds 		return -EPERM;
5416d76fa58SLinus Torvalds 
5426d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5431025774cSChristoph Hellwig 	if (error)
5446d76fa58SLinus Torvalds 		return error;
5451025774cSChristoph Hellwig 
5461025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
5471025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
5481025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
5491025774cSChristoph Hellwig 		if (error)
5501025774cSChristoph Hellwig 			return error;
5511025774cSChristoph Hellwig 	}
5521025774cSChristoph Hellwig 
5531025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5541025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5551025774cSChristoph Hellwig 	return 0;
5566d76fa58SLinus Torvalds }
5576d76fa58SLinus Torvalds 
5580499680aSVasiliy Kulikov /*
5590499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
5600499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
5610499680aSVasiliy Kulikov  */
5620499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
5630499680aSVasiliy Kulikov 				 struct task_struct *task,
5640499680aSVasiliy Kulikov 				 int hide_pid_min)
5650499680aSVasiliy Kulikov {
5660499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
5670499680aSVasiliy Kulikov 		return true;
5680499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
5690499680aSVasiliy Kulikov 		return true;
5700499680aSVasiliy Kulikov 	return ptrace_may_access(task, PTRACE_MODE_READ);
5710499680aSVasiliy Kulikov }
5720499680aSVasiliy Kulikov 
5730499680aSVasiliy Kulikov 
5740499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
5750499680aSVasiliy Kulikov {
5760499680aSVasiliy Kulikov 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
5770499680aSVasiliy Kulikov 	struct task_struct *task;
5780499680aSVasiliy Kulikov 	bool has_perms;
5790499680aSVasiliy Kulikov 
5800499680aSVasiliy Kulikov 	task = get_proc_task(inode);
581a2ef990aSXiaotian Feng 	if (!task)
582a2ef990aSXiaotian Feng 		return -ESRCH;
5830499680aSVasiliy Kulikov 	has_perms = has_pid_permissions(pid, task, 1);
5840499680aSVasiliy Kulikov 	put_task_struct(task);
5850499680aSVasiliy Kulikov 
5860499680aSVasiliy Kulikov 	if (!has_perms) {
5870499680aSVasiliy Kulikov 		if (pid->hide_pid == 2) {
5880499680aSVasiliy Kulikov 			/*
5890499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
5900499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
5910499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
5920499680aSVasiliy Kulikov 			 * in procfs at all.
5930499680aSVasiliy Kulikov 			 */
5940499680aSVasiliy Kulikov 			return -ENOENT;
5950499680aSVasiliy Kulikov 		}
5960499680aSVasiliy Kulikov 
5970499680aSVasiliy Kulikov 		return -EPERM;
5980499680aSVasiliy Kulikov 	}
5990499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
6000499680aSVasiliy Kulikov }
6010499680aSVasiliy Kulikov 
6020499680aSVasiliy Kulikov 
6030499680aSVasiliy Kulikov 
604c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6056d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6066d76fa58SLinus Torvalds };
6076d76fa58SLinus Torvalds 
6081da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6111da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6121da177e4SLinus Torvalds {
6132fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6141da177e4SLinus Torvalds 	unsigned long page;
6151da177e4SLinus Torvalds 	ssize_t length;
61699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
61799f89551SEric W. Biederman 
61899f89551SEric W. Biederman 	length = -ESRCH;
61999f89551SEric W. Biederman 	if (!task)
62099f89551SEric W. Biederman 		goto out_no_task;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6231da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
62499f89551SEric W. Biederman 
62599f89551SEric W. Biederman 	length = -ENOMEM;
626e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
62799f89551SEric W. Biederman 		goto out;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (length >= 0)
6321da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6331da177e4SLinus Torvalds 	free_page(page);
63499f89551SEric W. Biederman out:
63599f89551SEric W. Biederman 	put_task_struct(task);
63699f89551SEric W. Biederman out_no_task:
6371da177e4SLinus Torvalds 	return length;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
64000977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6411da177e4SLinus Torvalds 	.read		= proc_info_read,
64287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
6431da177e4SLinus Torvalds };
6441da177e4SLinus Torvalds 
645be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
646be614086SEric W. Biederman {
647be614086SEric W. Biederman 	struct inode *inode = m->private;
648be614086SEric W. Biederman 	struct pid_namespace *ns;
649be614086SEric W. Biederman 	struct pid *pid;
650be614086SEric W. Biederman 	struct task_struct *task;
651be614086SEric W. Biederman 	int ret;
652be614086SEric W. Biederman 
653be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
654be614086SEric W. Biederman 	pid = proc_pid(inode);
655be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
656be614086SEric W. Biederman 	if (!task)
657be614086SEric W. Biederman 		return -ESRCH;
658be614086SEric W. Biederman 
659be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
660be614086SEric W. Biederman 
661be614086SEric W. Biederman 	put_task_struct(task);
662be614086SEric W. Biederman 	return ret;
663be614086SEric W. Biederman }
664be614086SEric W. Biederman 
665be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
666be614086SEric W. Biederman {
667c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
668be614086SEric W. Biederman }
669be614086SEric W. Biederman 
670be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
671be614086SEric W. Biederman 	.open		= proc_single_open,
672be614086SEric W. Biederman 	.read		= seq_read,
673be614086SEric W. Biederman 	.llseek		= seq_lseek,
674be614086SEric W. Biederman 	.release	= single_release,
675be614086SEric W. Biederman };
676be614086SEric W. Biederman 
677b409e578SCong Wang static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
6781da177e4SLinus Torvalds {
679e268337dSLinus Torvalds 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
680e268337dSLinus Torvalds 	struct mm_struct *mm;
681e268337dSLinus Torvalds 
682e268337dSLinus Torvalds 	if (!task)
683e268337dSLinus Torvalds 		return -ESRCH;
684e268337dSLinus Torvalds 
685b409e578SCong Wang 	mm = mm_access(task, mode);
686e268337dSLinus Torvalds 	put_task_struct(task);
687e268337dSLinus Torvalds 
688e268337dSLinus Torvalds 	if (IS_ERR(mm))
689e268337dSLinus Torvalds 		return PTR_ERR(mm);
690e268337dSLinus Torvalds 
6916d08f2c7SOleg Nesterov 	if (mm) {
6926d08f2c7SOleg Nesterov 		/* ensure this mm_struct can't be freed */
6936d08f2c7SOleg Nesterov 		atomic_inc(&mm->mm_count);
6946d08f2c7SOleg Nesterov 		/* but do not pin its memory */
6956d08f2c7SOleg Nesterov 		mmput(mm);
6966d08f2c7SOleg Nesterov 	}
6976d08f2c7SOleg Nesterov 
6984a3956c7SKAMEZAWA Hiroyuki 	/* OK to pass negative loff_t, we can catch out-of-range */
6994a3956c7SKAMEZAWA Hiroyuki 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
700e268337dSLinus Torvalds 	file->private_data = mm;
701e268337dSLinus Torvalds 
7021da177e4SLinus Torvalds 	return 0;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
705b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
706b409e578SCong Wang {
707b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_ATTACH);
708b409e578SCong Wang }
709b409e578SCong Wang 
710572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
711572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
7121da177e4SLinus Torvalds {
713e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
714572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
715572d34b9SOleg Nesterov 	ssize_t copied;
7161da177e4SLinus Torvalds 	char *page;
7171da177e4SLinus Torvalds 
718e268337dSLinus Torvalds 	if (!mm)
719e268337dSLinus Torvalds 		return 0;
7201da177e4SLinus Torvalds 
721e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7221da177e4SLinus Torvalds 	if (!page)
723e268337dSLinus Torvalds 		return -ENOMEM;
7241da177e4SLinus Torvalds 
725f7ca54f4SFrederik Deweerdt 	copied = 0;
7266d08f2c7SOleg Nesterov 	if (!atomic_inc_not_zero(&mm->mm_users))
7276d08f2c7SOleg Nesterov 		goto free;
7286d08f2c7SOleg Nesterov 
7291da177e4SLinus Torvalds 	while (count > 0) {
730572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
7311da177e4SLinus Torvalds 
732572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
7331da177e4SLinus Torvalds 			copied = -EFAULT;
7341da177e4SLinus Torvalds 			break;
7351da177e4SLinus Torvalds 		}
736572d34b9SOleg Nesterov 
737572d34b9SOleg Nesterov 		this_len = access_remote_vm(mm, addr, page, this_len, write);
738572d34b9SOleg Nesterov 		if (!this_len) {
7391da177e4SLinus Torvalds 			if (!copied)
7401da177e4SLinus Torvalds 				copied = -EIO;
7411da177e4SLinus Torvalds 			break;
7421da177e4SLinus Torvalds 		}
743572d34b9SOleg Nesterov 
744572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
745572d34b9SOleg Nesterov 			copied = -EFAULT;
746572d34b9SOleg Nesterov 			break;
7471da177e4SLinus Torvalds 		}
748572d34b9SOleg Nesterov 
749572d34b9SOleg Nesterov 		buf += this_len;
750572d34b9SOleg Nesterov 		addr += this_len;
751572d34b9SOleg Nesterov 		copied += this_len;
752572d34b9SOleg Nesterov 		count -= this_len;
753572d34b9SOleg Nesterov 	}
754572d34b9SOleg Nesterov 	*ppos = addr;
75530cd8903SKOSAKI Motohiro 
7566d08f2c7SOleg Nesterov 	mmput(mm);
7576d08f2c7SOleg Nesterov free:
75830cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
7591da177e4SLinus Torvalds 	return copied;
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
762572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
763572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
764572d34b9SOleg Nesterov {
765572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
766572d34b9SOleg Nesterov }
767572d34b9SOleg Nesterov 
768572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
769572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
770572d34b9SOleg Nesterov {
771572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
772572d34b9SOleg Nesterov }
773572d34b9SOleg Nesterov 
77485863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
7751da177e4SLinus Torvalds {
7761da177e4SLinus Torvalds 	switch (orig) {
7771da177e4SLinus Torvalds 	case 0:
7781da177e4SLinus Torvalds 		file->f_pos = offset;
7791da177e4SLinus Torvalds 		break;
7801da177e4SLinus Torvalds 	case 1:
7811da177e4SLinus Torvalds 		file->f_pos += offset;
7821da177e4SLinus Torvalds 		break;
7831da177e4SLinus Torvalds 	default:
7841da177e4SLinus Torvalds 		return -EINVAL;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 	force_successful_syscall_return();
7871da177e4SLinus Torvalds 	return file->f_pos;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
790e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
791e268337dSLinus Torvalds {
792e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
79371879d3cSOleg Nesterov 	if (mm)
7946d08f2c7SOleg Nesterov 		mmdrop(mm);
795e268337dSLinus Torvalds 	return 0;
796e268337dSLinus Torvalds }
797e268337dSLinus Torvalds 
79800977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
7991da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8001da177e4SLinus Torvalds 	.read		= mem_read,
8011da177e4SLinus Torvalds 	.write		= mem_write,
8021da177e4SLinus Torvalds 	.open		= mem_open,
803e268337dSLinus Torvalds 	.release	= mem_release,
8041da177e4SLinus Torvalds };
8051da177e4SLinus Torvalds 
806b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
807b409e578SCong Wang {
808b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
809b409e578SCong Wang }
810b409e578SCong Wang 
811315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
812315e28c8SJames Pearson 			size_t count, loff_t *ppos)
813315e28c8SJames Pearson {
814315e28c8SJames Pearson 	char *page;
815315e28c8SJames Pearson 	unsigned long src = *ppos;
816b409e578SCong Wang 	int ret = 0;
817b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
818315e28c8SJames Pearson 
819b409e578SCong Wang 	if (!mm)
820b409e578SCong Wang 		return 0;
821315e28c8SJames Pearson 
822315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
823315e28c8SJames Pearson 	if (!page)
824b409e578SCong Wang 		return -ENOMEM;
825315e28c8SJames Pearson 
826d6f64b89SAl Viro 	ret = 0;
827b409e578SCong Wang 	if (!atomic_inc_not_zero(&mm->mm_users))
828b409e578SCong Wang 		goto free;
829315e28c8SJames Pearson 	while (count > 0) {
830315e28c8SJames Pearson 		int this_len, retval, max_len;
831315e28c8SJames Pearson 
832315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
833315e28c8SJames Pearson 
834315e28c8SJames Pearson 		if (this_len <= 0)
835315e28c8SJames Pearson 			break;
836315e28c8SJames Pearson 
837315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
838315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
839315e28c8SJames Pearson 
840b409e578SCong Wang 		retval = access_remote_vm(mm, (mm->env_start + src),
841315e28c8SJames Pearson 			page, this_len, 0);
842315e28c8SJames Pearson 
843315e28c8SJames Pearson 		if (retval <= 0) {
844315e28c8SJames Pearson 			ret = retval;
845315e28c8SJames Pearson 			break;
846315e28c8SJames Pearson 		}
847315e28c8SJames Pearson 
848315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
849315e28c8SJames Pearson 			ret = -EFAULT;
850315e28c8SJames Pearson 			break;
851315e28c8SJames Pearson 		}
852315e28c8SJames Pearson 
853315e28c8SJames Pearson 		ret += retval;
854315e28c8SJames Pearson 		src += retval;
855315e28c8SJames Pearson 		buf += retval;
856315e28c8SJames Pearson 		count -= retval;
857315e28c8SJames Pearson 	}
858315e28c8SJames Pearson 	*ppos = src;
859315e28c8SJames Pearson 	mmput(mm);
860b409e578SCong Wang 
861b409e578SCong Wang free:
862315e28c8SJames Pearson 	free_page((unsigned long) page);
863315e28c8SJames Pearson 	return ret;
864315e28c8SJames Pearson }
865315e28c8SJames Pearson 
866315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
867b409e578SCong Wang 	.open		= environ_open,
868315e28c8SJames Pearson 	.read		= environ_read,
86987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
870b409e578SCong Wang 	.release	= mem_release,
871315e28c8SJames Pearson };
872315e28c8SJames Pearson 
8731da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
8741da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8751da177e4SLinus Torvalds {
8762fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8778578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
8781da177e4SLinus Torvalds 	size_t len;
87928b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
88028b83c51SKOSAKI Motohiro 	unsigned long flags;
8811da177e4SLinus Torvalds 
88299f89551SEric W. Biederman 	if (!task)
88399f89551SEric W. Biederman 		return -ESRCH;
88428b83c51SKOSAKI Motohiro 
88528b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
88628b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
88728b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
88828b83c51SKOSAKI Motohiro 	}
88928b83c51SKOSAKI Motohiro 
89099f89551SEric W. Biederman 	put_task_struct(task);
89199f89551SEric W. Biederman 
8928578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
8930c28f287SAkinobu Mita 
8940c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
8981da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8991da177e4SLinus Torvalds {
90099f89551SEric W. Biederman 	struct task_struct *task;
9015d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
9020a8cb8e3SAlexey Dobriyan 	int oom_adjust;
90328b83c51SKOSAKI Motohiro 	unsigned long flags;
9045d863b89SKOSAKI Motohiro 	int err;
9051da177e4SLinus Torvalds 
9068578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
9078578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
9088578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
909723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
910723548bfSDavid Rientjes 		err = -EFAULT;
911723548bfSDavid Rientjes 		goto out;
912723548bfSDavid Rientjes 	}
9135d863b89SKOSAKI Motohiro 
9140a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
9155d863b89SKOSAKI Motohiro 	if (err)
916723548bfSDavid Rientjes 		goto out;
9178ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
918723548bfSDavid Rientjes 	     oom_adjust != OOM_DISABLE) {
919723548bfSDavid Rientjes 		err = -EINVAL;
920723548bfSDavid Rientjes 		goto out;
921723548bfSDavid Rientjes 	}
9225d863b89SKOSAKI Motohiro 
9232fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
924723548bfSDavid Rientjes 	if (!task) {
925723548bfSDavid Rientjes 		err = -ESRCH;
926723548bfSDavid Rientjes 		goto out;
927723548bfSDavid Rientjes 	}
92828b83c51SKOSAKI Motohiro 
9293d5992d2SYing Han 	task_lock(task);
9303d5992d2SYing Han 	if (!task->mm) {
931723548bfSDavid Rientjes 		err = -EINVAL;
932723548bfSDavid Rientjes 		goto err_task_lock;
9333d5992d2SYing Han 	}
9343d5992d2SYing Han 
935d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
936d19d5476SDavid Rientjes 		err = -ESRCH;
937d19d5476SDavid Rientjes 		goto err_task_lock;
938d19d5476SDavid Rientjes 	}
939d19d5476SDavid Rientjes 
940d19d5476SDavid Rientjes 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
941d19d5476SDavid Rientjes 		err = -EACCES;
942d19d5476SDavid Rientjes 		goto err_sighand;
943d19d5476SDavid Rientjes 	}
944d19d5476SDavid Rientjes 
94551b1bd2aSDavid Rientjes 	/*
94651b1bd2aSDavid Rientjes 	 * Warn that /proc/pid/oom_adj is deprecated, see
94751b1bd2aSDavid Rientjes 	 * Documentation/feature-removal-schedule.txt.
94851b1bd2aSDavid Rientjes 	 */
949c2142704SLinus Torvalds 	printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
950be8f684dSDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
951be8f684dSDavid Rientjes 		  task_pid_nr(task));
95228b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
953a63d83f4SDavid Rientjes 	/*
954a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
955a63d83f4SDavid Rientjes 	 * value is always attainable.
956a63d83f4SDavid Rientjes 	 */
957a63d83f4SDavid Rientjes 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
958a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
959a63d83f4SDavid Rientjes 	else
960a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
961a63d83f4SDavid Rientjes 								-OOM_DISABLE;
96243d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
963723548bfSDavid Rientjes err_sighand:
96428b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
965d19d5476SDavid Rientjes err_task_lock:
966d19d5476SDavid Rientjes 	task_unlock(task);
96799f89551SEric W. Biederman 	put_task_struct(task);
968723548bfSDavid Rientjes out:
969723548bfSDavid Rientjes 	return err < 0 ? err : count;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
97200977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
9731da177e4SLinus Torvalds 	.read		= oom_adjust_read,
9741da177e4SLinus Torvalds 	.write		= oom_adjust_write,
97587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
9761da177e4SLinus Torvalds };
9771da177e4SLinus Torvalds 
978a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
979a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
980a63d83f4SDavid Rientjes {
981a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
982a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
983a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
984a63d83f4SDavid Rientjes 	unsigned long flags;
985a63d83f4SDavid Rientjes 	size_t len;
986a63d83f4SDavid Rientjes 
987a63d83f4SDavid Rientjes 	if (!task)
988a63d83f4SDavid Rientjes 		return -ESRCH;
989a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
990a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
991a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
992a63d83f4SDavid Rientjes 	}
993a63d83f4SDavid Rientjes 	put_task_struct(task);
994a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
995a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
996a63d83f4SDavid Rientjes }
997a63d83f4SDavid Rientjes 
998a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
999a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1000a63d83f4SDavid Rientjes {
1001a63d83f4SDavid Rientjes 	struct task_struct *task;
1002a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1003a63d83f4SDavid Rientjes 	unsigned long flags;
10040a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1005a63d83f4SDavid Rientjes 	int err;
1006a63d83f4SDavid Rientjes 
1007a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1008a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1009a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1010723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1011723548bfSDavid Rientjes 		err = -EFAULT;
1012723548bfSDavid Rientjes 		goto out;
1013723548bfSDavid Rientjes 	}
1014a63d83f4SDavid Rientjes 
10150a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1016a63d83f4SDavid Rientjes 	if (err)
1017723548bfSDavid Rientjes 		goto out;
1018a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1019723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1020723548bfSDavid Rientjes 		err = -EINVAL;
1021723548bfSDavid Rientjes 		goto out;
1022723548bfSDavid Rientjes 	}
1023a63d83f4SDavid Rientjes 
1024a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1025723548bfSDavid Rientjes 	if (!task) {
1026723548bfSDavid Rientjes 		err = -ESRCH;
1027723548bfSDavid Rientjes 		goto out;
1028723548bfSDavid Rientjes 	}
1029a63d83f4SDavid Rientjes 
10303d5992d2SYing Han 	task_lock(task);
10313d5992d2SYing Han 	if (!task->mm) {
1032723548bfSDavid Rientjes 		err = -EINVAL;
1033723548bfSDavid Rientjes 		goto err_task_lock;
10343d5992d2SYing Han 	}
1035d19d5476SDavid Rientjes 
1036d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1037d19d5476SDavid Rientjes 		err = -ESRCH;
1038d19d5476SDavid Rientjes 		goto err_task_lock;
1039d19d5476SDavid Rientjes 	}
1040d19d5476SDavid Rientjes 
1041dabb16f6SMandeep Singh Baines 	if (oom_score_adj < task->signal->oom_score_adj_min &&
1042d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1043d19d5476SDavid Rientjes 		err = -EACCES;
1044d19d5476SDavid Rientjes 		goto err_sighand;
1045d19d5476SDavid Rientjes 	}
1046d19d5476SDavid Rientjes 
1047a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1048dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1049dabb16f6SMandeep Singh Baines 		task->signal->oom_score_adj_min = oom_score_adj;
105043d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
1051a63d83f4SDavid Rientjes 	/*
1052a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1053a63d83f4SDavid Rientjes 	 * always attainable.
1054a63d83f4SDavid Rientjes 	 */
1055a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1056a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1057a63d83f4SDavid Rientjes 	else
1058a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1059a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1060723548bfSDavid Rientjes err_sighand:
1061a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1062d19d5476SDavid Rientjes err_task_lock:
1063d19d5476SDavid Rientjes 	task_unlock(task);
1064a63d83f4SDavid Rientjes 	put_task_struct(task);
1065723548bfSDavid Rientjes out:
1066723548bfSDavid Rientjes 	return err < 0 ? err : count;
1067a63d83f4SDavid Rientjes }
1068a63d83f4SDavid Rientjes 
1069a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1070a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1071a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10726038f373SArnd Bergmann 	.llseek		= default_llseek,
1073a63d83f4SDavid Rientjes };
1074a63d83f4SDavid Rientjes 
10751da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10761da177e4SLinus Torvalds #define TMPBUFLEN 21
10771da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10781da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10791da177e4SLinus Torvalds {
10802fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
108199f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10821da177e4SLinus Torvalds 	ssize_t length;
10831da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10841da177e4SLinus Torvalds 
108599f89551SEric W. Biederman 	if (!task)
108699f89551SEric W. Biederman 		return -ESRCH;
10871da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
10880c11b942SAl Viro 				audit_get_loginuid(task));
108999f89551SEric W. Biederman 	put_task_struct(task);
10901da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10911da177e4SLinus Torvalds }
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10941da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10951da177e4SLinus Torvalds {
10962fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
10971da177e4SLinus Torvalds 	char *page, *tmp;
10981da177e4SLinus Torvalds 	ssize_t length;
10991da177e4SLinus Torvalds 	uid_t loginuid;
11001da177e4SLinus Torvalds 
11017dc52157SPaul E. McKenney 	rcu_read_lock();
11027dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11037dc52157SPaul E. McKenney 		rcu_read_unlock();
11041da177e4SLinus Torvalds 		return -EPERM;
11057dc52157SPaul E. McKenney 	}
11067dc52157SPaul E. McKenney 	rcu_read_unlock();
11071da177e4SLinus Torvalds 
1108e0182909SAl Viro 	if (count >= PAGE_SIZE)
1109e0182909SAl Viro 		count = PAGE_SIZE - 1;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	if (*ppos != 0) {
11121da177e4SLinus Torvalds 		/* No partial writes. */
11131da177e4SLinus Torvalds 		return -EINVAL;
11141da177e4SLinus Torvalds 	}
1115e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11161da177e4SLinus Torvalds 	if (!page)
11171da177e4SLinus Torvalds 		return -ENOMEM;
11181da177e4SLinus Torvalds 	length = -EFAULT;
11191da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11201da177e4SLinus Torvalds 		goto out_free_page;
11211da177e4SLinus Torvalds 
1122e0182909SAl Viro 	page[count] = '\0';
11231da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11241da177e4SLinus Torvalds 	if (tmp == page) {
11251da177e4SLinus Torvalds 		length = -EINVAL;
11261da177e4SLinus Torvalds 		goto out_free_page;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	}
11290a300be6SEric Paris 	length = audit_set_loginuid(loginuid);
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 {
11471e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
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 {
1170f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
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;
1202f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1203f4f154fdSAkinobu Mita 	if (!task)
1204f4f154fdSAkinobu Mita 		return -ESRCH;
1205f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1206f4f154fdSAkinobu Mita 	put_task_struct(task);
1207cba8aafeSVincent Li 
1208cba8aafeSVincent Li 	return count;
1209f4f154fdSAkinobu Mita }
1210f4f154fdSAkinobu Mita 
121100977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1212f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1213f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
121487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1215f4f154fdSAkinobu Mita };
1216f4f154fdSAkinobu Mita #endif
1217f4f154fdSAkinobu Mita 
12189745512cSArjan van de Ven 
121943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
122043ae34cbSIngo Molnar /*
122143ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
122243ae34cbSIngo Molnar  */
122343ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
122443ae34cbSIngo Molnar {
122543ae34cbSIngo Molnar 	struct inode *inode = m->private;
122643ae34cbSIngo Molnar 	struct task_struct *p;
122743ae34cbSIngo Molnar 
122843ae34cbSIngo Molnar 	p = get_proc_task(inode);
122943ae34cbSIngo Molnar 	if (!p)
123043ae34cbSIngo Molnar 		return -ESRCH;
123143ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
123243ae34cbSIngo Molnar 
123343ae34cbSIngo Molnar 	put_task_struct(p);
123443ae34cbSIngo Molnar 
123543ae34cbSIngo Molnar 	return 0;
123643ae34cbSIngo Molnar }
123743ae34cbSIngo Molnar 
123843ae34cbSIngo Molnar static ssize_t
123943ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
124043ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
124143ae34cbSIngo Molnar {
124243ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
124343ae34cbSIngo Molnar 	struct task_struct *p;
124443ae34cbSIngo Molnar 
124543ae34cbSIngo Molnar 	p = get_proc_task(inode);
124643ae34cbSIngo Molnar 	if (!p)
124743ae34cbSIngo Molnar 		return -ESRCH;
124843ae34cbSIngo Molnar 	proc_sched_set_task(p);
124943ae34cbSIngo Molnar 
125043ae34cbSIngo Molnar 	put_task_struct(p);
125143ae34cbSIngo Molnar 
125243ae34cbSIngo Molnar 	return count;
125343ae34cbSIngo Molnar }
125443ae34cbSIngo Molnar 
125543ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
125643ae34cbSIngo Molnar {
1257c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
125843ae34cbSIngo Molnar }
125943ae34cbSIngo Molnar 
126043ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
126143ae34cbSIngo Molnar 	.open		= sched_open,
126243ae34cbSIngo Molnar 	.read		= seq_read,
126343ae34cbSIngo Molnar 	.write		= sched_write,
126443ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12655ea473a1SAlexey Dobriyan 	.release	= single_release,
126643ae34cbSIngo Molnar };
126743ae34cbSIngo Molnar 
126843ae34cbSIngo Molnar #endif
126943ae34cbSIngo Molnar 
12705091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
12715091faa4SMike Galbraith /*
12725091faa4SMike Galbraith  * Print out autogroup related information:
12735091faa4SMike Galbraith  */
12745091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
12755091faa4SMike Galbraith {
12765091faa4SMike Galbraith 	struct inode *inode = m->private;
12775091faa4SMike Galbraith 	struct task_struct *p;
12785091faa4SMike Galbraith 
12795091faa4SMike Galbraith 	p = get_proc_task(inode);
12805091faa4SMike Galbraith 	if (!p)
12815091faa4SMike Galbraith 		return -ESRCH;
12825091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
12835091faa4SMike Galbraith 
12845091faa4SMike Galbraith 	put_task_struct(p);
12855091faa4SMike Galbraith 
12865091faa4SMike Galbraith 	return 0;
12875091faa4SMike Galbraith }
12885091faa4SMike Galbraith 
12895091faa4SMike Galbraith static ssize_t
12905091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
12915091faa4SMike Galbraith 	    size_t count, loff_t *offset)
12925091faa4SMike Galbraith {
12935091faa4SMike Galbraith 	struct inode *inode = file->f_path.dentry->d_inode;
12945091faa4SMike Galbraith 	struct task_struct *p;
12955091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
12960a8cb8e3SAlexey Dobriyan 	int nice;
12975091faa4SMike Galbraith 	int err;
12985091faa4SMike Galbraith 
12995091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
13005091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
13015091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
13025091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
13035091faa4SMike Galbraith 		return -EFAULT;
13045091faa4SMike Galbraith 
13050a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
13060a8cb8e3SAlexey Dobriyan 	if (err < 0)
13070a8cb8e3SAlexey Dobriyan 		return err;
13085091faa4SMike Galbraith 
13095091faa4SMike Galbraith 	p = get_proc_task(inode);
13105091faa4SMike Galbraith 	if (!p)
13115091faa4SMike Galbraith 		return -ESRCH;
13125091faa4SMike Galbraith 
13132e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
13145091faa4SMike Galbraith 	if (err)
13155091faa4SMike Galbraith 		count = err;
13165091faa4SMike Galbraith 
13175091faa4SMike Galbraith 	put_task_struct(p);
13185091faa4SMike Galbraith 
13195091faa4SMike Galbraith 	return count;
13205091faa4SMike Galbraith }
13215091faa4SMike Galbraith 
13225091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
13235091faa4SMike Galbraith {
13245091faa4SMike Galbraith 	int ret;
13255091faa4SMike Galbraith 
13265091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13275091faa4SMike Galbraith 	if (!ret) {
13285091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13295091faa4SMike Galbraith 
13305091faa4SMike Galbraith 		m->private = inode;
13315091faa4SMike Galbraith 	}
13325091faa4SMike Galbraith 	return ret;
13335091faa4SMike Galbraith }
13345091faa4SMike Galbraith 
13355091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13365091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13375091faa4SMike Galbraith 	.read		= seq_read,
13385091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13395091faa4SMike Galbraith 	.llseek		= seq_lseek,
13405091faa4SMike Galbraith 	.release	= single_release,
13415091faa4SMike Galbraith };
13425091faa4SMike Galbraith 
13435091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
13445091faa4SMike Galbraith 
13454614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13464614a696Sjohn stultz 				size_t count, loff_t *offset)
13474614a696Sjohn stultz {
13484614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
13494614a696Sjohn stultz 	struct task_struct *p;
13504614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
13514614a696Sjohn stultz 
13524614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
13534614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
13544614a696Sjohn stultz 		count = sizeof(buffer) - 1;
13554614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
13564614a696Sjohn stultz 		return -EFAULT;
13574614a696Sjohn stultz 
13584614a696Sjohn stultz 	p = get_proc_task(inode);
13594614a696Sjohn stultz 	if (!p)
13604614a696Sjohn stultz 		return -ESRCH;
13614614a696Sjohn stultz 
13624614a696Sjohn stultz 	if (same_thread_group(current, p))
13634614a696Sjohn stultz 		set_task_comm(p, buffer);
13644614a696Sjohn stultz 	else
13654614a696Sjohn stultz 		count = -EINVAL;
13664614a696Sjohn stultz 
13674614a696Sjohn stultz 	put_task_struct(p);
13684614a696Sjohn stultz 
13694614a696Sjohn stultz 	return count;
13704614a696Sjohn stultz }
13714614a696Sjohn stultz 
13724614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13734614a696Sjohn stultz {
13744614a696Sjohn stultz 	struct inode *inode = m->private;
13754614a696Sjohn stultz 	struct task_struct *p;
13764614a696Sjohn stultz 
13774614a696Sjohn stultz 	p = get_proc_task(inode);
13784614a696Sjohn stultz 	if (!p)
13794614a696Sjohn stultz 		return -ESRCH;
13804614a696Sjohn stultz 
13814614a696Sjohn stultz 	task_lock(p);
13824614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13834614a696Sjohn stultz 	task_unlock(p);
13844614a696Sjohn stultz 
13854614a696Sjohn stultz 	put_task_struct(p);
13864614a696Sjohn stultz 
13874614a696Sjohn stultz 	return 0;
13884614a696Sjohn stultz }
13894614a696Sjohn stultz 
13904614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
13914614a696Sjohn stultz {
1392c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
13934614a696Sjohn stultz }
13944614a696Sjohn stultz 
13954614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
13964614a696Sjohn stultz 	.open		= comm_open,
13974614a696Sjohn stultz 	.read		= seq_read,
13984614a696Sjohn stultz 	.write		= comm_write,
13994614a696Sjohn stultz 	.llseek		= seq_lseek,
14004614a696Sjohn stultz 	.release	= single_release,
14014614a696Sjohn stultz };
14024614a696Sjohn stultz 
14037773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1404925d1c40SMatt Helsley {
1405925d1c40SMatt Helsley 	struct task_struct *task;
1406925d1c40SMatt Helsley 	struct mm_struct *mm;
1407925d1c40SMatt Helsley 	struct file *exe_file;
1408925d1c40SMatt Helsley 
14097773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1410925d1c40SMatt Helsley 	if (!task)
1411925d1c40SMatt Helsley 		return -ENOENT;
1412925d1c40SMatt Helsley 	mm = get_task_mm(task);
1413925d1c40SMatt Helsley 	put_task_struct(task);
1414925d1c40SMatt Helsley 	if (!mm)
1415925d1c40SMatt Helsley 		return -ENOENT;
1416925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1417925d1c40SMatt Helsley 	mmput(mm);
1418925d1c40SMatt Helsley 	if (exe_file) {
1419925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1420925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1421925d1c40SMatt Helsley 		fput(exe_file);
1422925d1c40SMatt Helsley 		return 0;
1423925d1c40SMatt Helsley 	} else
1424925d1c40SMatt Helsley 		return -ENOENT;
1425925d1c40SMatt Helsley }
1426925d1c40SMatt Helsley 
1427008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14281da177e4SLinus Torvalds {
14291da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14301da177e4SLinus Torvalds 	int error = -EACCES;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
14331d957f9bSJan Blunck 	path_put(&nd->path);
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 
14397773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
14401da177e4SLinus Torvalds out:
1441008b150aSAl Viro 	return ERR_PTR(error);
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14443dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14451da177e4SLinus Torvalds {
1446e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14473dcd25f3SJan Blunck 	char *pathname;
14481da177e4SLinus Torvalds 	int len;
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	if (!tmp)
14511da177e4SLinus Torvalds 		return -ENOMEM;
14521da177e4SLinus Torvalds 
14537b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14543dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14553dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14561da177e4SLinus Torvalds 		goto out;
14573dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	if (len > buflen)
14601da177e4SLinus Torvalds 		len = buflen;
14613dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14621da177e4SLinus Torvalds 		len = -EFAULT;
14631da177e4SLinus Torvalds  out:
14641da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14651da177e4SLinus Torvalds 	return len;
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14691da177e4SLinus Torvalds {
14701da177e4SLinus Torvalds 	int error = -EACCES;
14711da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14723dcd25f3SJan Blunck 	struct path path;
14731da177e4SLinus Torvalds 
1474778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1475778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14761da177e4SLinus Torvalds 		goto out;
14771da177e4SLinus Torvalds 
14787773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
14791da177e4SLinus Torvalds 	if (error)
14801da177e4SLinus Torvalds 		goto out;
14811da177e4SLinus Torvalds 
14823dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14833dcd25f3SJan Blunck 	path_put(&path);
14841da177e4SLinus Torvalds out:
14851da177e4SLinus Torvalds 	return error;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
1488c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
14891da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
14906d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
14916d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
14921da177e4SLinus Torvalds };
14931da177e4SLinus Torvalds 
149428a6d671SEric W. Biederman 
149528a6d671SEric W. Biederman /* building an inode */
149628a6d671SEric W. Biederman 
149728a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
149828a6d671SEric W. Biederman {
149928a6d671SEric W. Biederman 	int dumpable = 0;
150028a6d671SEric W. Biederman 	struct mm_struct *mm;
150128a6d671SEric W. Biederman 
150228a6d671SEric W. Biederman 	task_lock(task);
150328a6d671SEric W. Biederman 	mm = task->mm;
150428a6d671SEric W. Biederman 	if (mm)
15056c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
150628a6d671SEric W. Biederman 	task_unlock(task);
150728a6d671SEric W. Biederman 	if(dumpable == 1)
150828a6d671SEric W. Biederman 		return 1;
150928a6d671SEric W. Biederman 	return 0;
151028a6d671SEric W. Biederman }
151128a6d671SEric W. Biederman 
15126b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
151328a6d671SEric W. Biederman {
151428a6d671SEric W. Biederman 	struct inode * inode;
151528a6d671SEric W. Biederman 	struct proc_inode *ei;
1516c69e8d9cSDavid Howells 	const struct cred *cred;
151728a6d671SEric W. Biederman 
151828a6d671SEric W. Biederman 	/* We need a new inode */
151928a6d671SEric W. Biederman 
152028a6d671SEric W. Biederman 	inode = new_inode(sb);
152128a6d671SEric W. Biederman 	if (!inode)
152228a6d671SEric W. Biederman 		goto out;
152328a6d671SEric W. Biederman 
152428a6d671SEric W. Biederman 	/* Common stuff */
152528a6d671SEric W. Biederman 	ei = PROC_I(inode);
152685fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
152728a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
152828a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
152928a6d671SEric W. Biederman 
153028a6d671SEric W. Biederman 	/*
153128a6d671SEric W. Biederman 	 * grab the reference to task.
153228a6d671SEric W. Biederman 	 */
15331a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
153428a6d671SEric W. Biederman 	if (!ei->pid)
153528a6d671SEric W. Biederman 		goto out_unlock;
153628a6d671SEric W. Biederman 
153728a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1538c69e8d9cSDavid Howells 		rcu_read_lock();
1539c69e8d9cSDavid Howells 		cred = __task_cred(task);
1540c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1541c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1542c69e8d9cSDavid Howells 		rcu_read_unlock();
154328a6d671SEric W. Biederman 	}
154428a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
154528a6d671SEric W. Biederman 
154628a6d671SEric W. Biederman out:
154728a6d671SEric W. Biederman 	return inode;
154828a6d671SEric W. Biederman 
154928a6d671SEric W. Biederman out_unlock:
155028a6d671SEric W. Biederman 	iput(inode);
155128a6d671SEric W. Biederman 	return NULL;
155228a6d671SEric W. Biederman }
155328a6d671SEric W. Biederman 
15546b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
155528a6d671SEric W. Biederman {
155628a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
155728a6d671SEric W. Biederman 	struct task_struct *task;
1558c69e8d9cSDavid Howells 	const struct cred *cred;
15590499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1560c69e8d9cSDavid Howells 
156128a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
156228a6d671SEric W. Biederman 
156328a6d671SEric W. Biederman 	rcu_read_lock();
1564dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1565dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
156628a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
156728a6d671SEric W. Biederman 	if (task) {
15680499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15690499680aSVasiliy Kulikov 			rcu_read_unlock();
15700499680aSVasiliy Kulikov 			/*
15710499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15720499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15730499680aSVasiliy Kulikov 			 */
15740499680aSVasiliy Kulikov 			return -ENOENT;
15750499680aSVasiliy Kulikov 		}
157628a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
157728a6d671SEric W. Biederman 		    task_dumpable(task)) {
1578c69e8d9cSDavid Howells 			cred = __task_cred(task);
1579c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1580c69e8d9cSDavid Howells 			stat->gid = cred->egid;
158128a6d671SEric W. Biederman 		}
158228a6d671SEric W. Biederman 	}
158328a6d671SEric W. Biederman 	rcu_read_unlock();
158428a6d671SEric W. Biederman 	return 0;
158528a6d671SEric W. Biederman }
158628a6d671SEric W. Biederman 
158728a6d671SEric W. Biederman /* dentry stuff */
158828a6d671SEric W. Biederman 
158928a6d671SEric W. Biederman /*
159028a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
159128a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
159228a6d671SEric W. Biederman  * due to the way we treat inodes.
159328a6d671SEric W. Biederman  *
159428a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
159528a6d671SEric W. Biederman  * performed a setuid(), etc.
159628a6d671SEric W. Biederman  *
159728a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
159828a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
159928a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
160028a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
160128a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
160228a6d671SEric W. Biederman  * directories.
160328a6d671SEric W. Biederman  */
16040b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags)
160528a6d671SEric W. Biederman {
160634286d66SNick Piggin 	struct inode *inode;
160734286d66SNick Piggin 	struct task_struct *task;
1608c69e8d9cSDavid Howells 	const struct cred *cred;
1609c69e8d9cSDavid Howells 
16100b728e19SAl Viro 	if (flags & LOOKUP_RCU)
161134286d66SNick Piggin 		return -ECHILD;
161234286d66SNick Piggin 
161334286d66SNick Piggin 	inode = dentry->d_inode;
161434286d66SNick Piggin 	task = get_proc_task(inode);
161534286d66SNick Piggin 
161628a6d671SEric W. Biederman 	if (task) {
161728a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
161828a6d671SEric W. Biederman 		    task_dumpable(task)) {
1619c69e8d9cSDavid Howells 			rcu_read_lock();
1620c69e8d9cSDavid Howells 			cred = __task_cred(task);
1621c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1622c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1623c69e8d9cSDavid Howells 			rcu_read_unlock();
162428a6d671SEric W. Biederman 		} else {
1625dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1626dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
162728a6d671SEric W. Biederman 		}
162828a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
162928a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
163028a6d671SEric W. Biederman 		put_task_struct(task);
163128a6d671SEric W. Biederman 		return 1;
163228a6d671SEric W. Biederman 	}
163328a6d671SEric W. Biederman 	d_drop(dentry);
163428a6d671SEric W. Biederman 	return 0;
163528a6d671SEric W. Biederman }
163628a6d671SEric W. Biederman 
1637fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry)
163828a6d671SEric W. Biederman {
163928a6d671SEric W. Biederman 	/* Is the task we represent dead?
164028a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
164128a6d671SEric W. Biederman 	 * kill it immediately.
164228a6d671SEric W. Biederman 	 */
164328a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
164428a6d671SEric W. Biederman }
164528a6d671SEric W. Biederman 
16466b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
164728a6d671SEric W. Biederman {
164828a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
164928a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
165028a6d671SEric W. Biederman };
165128a6d671SEric W. Biederman 
165228a6d671SEric W. Biederman /* Lookups */
165328a6d671SEric W. Biederman 
16541c0d04c9SEric W. Biederman /*
16551c0d04c9SEric W. Biederman  * Fill a directory entry.
16561c0d04c9SEric W. Biederman  *
16571c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16581c0d04c9SEric W. Biederman  * file type from dcache entry.
16591c0d04c9SEric W. Biederman  *
16601c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16611c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16621c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16631c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16641c0d04c9SEric W. Biederman  * by stat.
16651c0d04c9SEric W. Biederman  */
16666b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
16676b4e306aSEric W. Biederman 	const char *name, int len,
1668c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
166961a28784SEric W. Biederman {
16702fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
167161a28784SEric W. Biederman 	struct inode *inode;
167261a28784SEric W. Biederman 	struct qstr qname;
167361a28784SEric W. Biederman 	ino_t ino = 0;
167461a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
167561a28784SEric W. Biederman 
167661a28784SEric W. Biederman 	qname.name = name;
167761a28784SEric W. Biederman 	qname.len  = len;
167861a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
167961a28784SEric W. Biederman 
168061a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
168161a28784SEric W. Biederman 	if (!child) {
168261a28784SEric W. Biederman 		struct dentry *new;
168361a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
168461a28784SEric W. Biederman 		if (new) {
168561a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
168661a28784SEric W. Biederman 			if (child)
168761a28784SEric W. Biederman 				dput(new);
168861a28784SEric W. Biederman 			else
168961a28784SEric W. Biederman 				child = new;
169061a28784SEric W. Biederman 		}
169161a28784SEric W. Biederman 	}
169261a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
169361a28784SEric W. Biederman 		goto end_instantiate;
169461a28784SEric W. Biederman 	inode = child->d_inode;
169561a28784SEric W. Biederman 	if (inode) {
169661a28784SEric W. Biederman 		ino = inode->i_ino;
169761a28784SEric W. Biederman 		type = inode->i_mode >> 12;
169861a28784SEric W. Biederman 	}
169961a28784SEric W. Biederman 	dput(child);
170061a28784SEric W. Biederman end_instantiate:
170161a28784SEric W. Biederman 	if (!ino)
170261a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
170361a28784SEric W. Biederman 	if (!ino)
170461a28784SEric W. Biederman 		ino = 1;
170561a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
170661a28784SEric W. Biederman }
170761a28784SEric W. Biederman 
170828a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
170928a6d671SEric W. Biederman {
171028a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
171128a6d671SEric W. Biederman 	int len = dentry->d_name.len;
171228a6d671SEric W. Biederman 	unsigned n = 0;
171328a6d671SEric W. Biederman 
171428a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
171528a6d671SEric W. Biederman 		goto out;
171628a6d671SEric W. Biederman 	while (len-- > 0) {
171728a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
171828a6d671SEric W. Biederman 		if (c > 9)
171928a6d671SEric W. Biederman 			goto out;
172028a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
172128a6d671SEric W. Biederman 			goto out;
172228a6d671SEric W. Biederman 		n *= 10;
172328a6d671SEric W. Biederman 		n += c;
172428a6d671SEric W. Biederman 	}
172528a6d671SEric W. Biederman 	return n;
172628a6d671SEric W. Biederman out:
172728a6d671SEric W. Biederman 	return ~0U;
172828a6d671SEric W. Biederman }
172928a6d671SEric W. Biederman 
173027932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
173127932742SMiklos Szeredi 
17323dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
173328a6d671SEric W. Biederman {
17345e442a49SLinus Torvalds 	struct task_struct *task = get_proc_task(inode);
17355e442a49SLinus Torvalds 	struct files_struct *files = NULL;
173628a6d671SEric W. Biederman 	struct file *file;
173728a6d671SEric W. Biederman 	int fd = proc_fd(inode);
173828a6d671SEric W. Biederman 
17395e442a49SLinus Torvalds 	if (task) {
174028a6d671SEric W. Biederman 		files = get_files_struct(task);
17415e442a49SLinus Torvalds 		put_task_struct(task);
17425e442a49SLinus Torvalds 	}
17435e442a49SLinus Torvalds 	if (files) {
174428a6d671SEric W. Biederman 		/*
174528a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
174628a6d671SEric W. Biederman 		 * hold ->file_lock.
174728a6d671SEric W. Biederman 		 */
174828a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
174928a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
175028a6d671SEric W. Biederman 		if (file) {
17511117f72eSLinus Torvalds 			unsigned int f_flags;
17521117f72eSLinus Torvalds 			struct fdtable *fdt;
17531117f72eSLinus Torvalds 
17541117f72eSLinus Torvalds 			fdt = files_fdtable(files);
17551117f72eSLinus Torvalds 			f_flags = file->f_flags & ~O_CLOEXEC;
17561dce27c5SDavid Howells 			if (close_on_exec(fd, fdt))
17571117f72eSLinus Torvalds 				f_flags |= O_CLOEXEC;
17581117f72eSLinus Torvalds 
17593dcd25f3SJan Blunck 			if (path) {
17603dcd25f3SJan Blunck 				*path = file->f_path;
17613dcd25f3SJan Blunck 				path_get(&file->f_path);
17623dcd25f3SJan Blunck 			}
176327932742SMiklos Szeredi 			if (info)
176427932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
176527932742SMiklos Szeredi 					 "pos:\t%lli\n"
176627932742SMiklos Szeredi 					 "flags:\t0%o\n",
176727932742SMiklos Szeredi 					 (long long) file->f_pos,
17681117f72eSLinus Torvalds 					 f_flags);
176928a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
177028a6d671SEric W. Biederman 			put_files_struct(files);
17715e442a49SLinus Torvalds 			return 0;
17725e442a49SLinus Torvalds 		}
17735e442a49SLinus Torvalds 		spin_unlock(&files->file_lock);
17745e442a49SLinus Torvalds 		put_files_struct(files);
17755e442a49SLinus Torvalds 	}
17765e442a49SLinus Torvalds 	return -ENOENT;
177728a6d671SEric W. Biederman }
177828a6d671SEric W. Biederman 
17797773fbc5SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path)
178027932742SMiklos Szeredi {
17817773fbc5SCyrill Gorcunov 	return proc_fd_info(dentry->d_inode, path, NULL);
178227932742SMiklos Szeredi }
178327932742SMiklos Szeredi 
17840b728e19SAl Viro static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
178528a6d671SEric W. Biederman {
178634286d66SNick Piggin 	struct inode *inode;
178734286d66SNick Piggin 	struct task_struct *task;
178834286d66SNick Piggin 	int fd;
178928a6d671SEric W. Biederman 	struct files_struct *files;
1790c69e8d9cSDavid Howells 	const struct cred *cred;
179128a6d671SEric W. Biederman 
17920b728e19SAl Viro 	if (flags & LOOKUP_RCU)
179334286d66SNick Piggin 		return -ECHILD;
179434286d66SNick Piggin 
179534286d66SNick Piggin 	inode = dentry->d_inode;
179634286d66SNick Piggin 	task = get_proc_task(inode);
179734286d66SNick Piggin 	fd = proc_fd(inode);
179834286d66SNick Piggin 
179928a6d671SEric W. Biederman 	if (task) {
180028a6d671SEric W. Biederman 		files = get_files_struct(task);
180128a6d671SEric W. Biederman 		if (files) {
180230a08bf2SLinus Torvalds 			struct file *file;
180328a6d671SEric W. Biederman 			rcu_read_lock();
180430a08bf2SLinus Torvalds 			file = fcheck_files(files, fd);
180530a08bf2SLinus Torvalds 			if (file) {
18060640113bSLinus Torvalds 				unsigned f_mode = file->f_mode;
180730a08bf2SLinus Torvalds 
180828a6d671SEric W. Biederman 				rcu_read_unlock();
180928a6d671SEric W. Biederman 				put_files_struct(files);
181030a08bf2SLinus Torvalds 
181128a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1812c69e8d9cSDavid Howells 					rcu_read_lock();
1813c69e8d9cSDavid Howells 					cred = __task_cred(task);
1814c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1815c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1816c69e8d9cSDavid Howells 					rcu_read_unlock();
181728a6d671SEric W. Biederman 				} else {
1818dcb0f222SEric W. Biederman 					inode->i_uid = GLOBAL_ROOT_UID;
1819dcb0f222SEric W. Biederman 					inode->i_gid = GLOBAL_ROOT_GID;
182028a6d671SEric W. Biederman 				}
182130a08bf2SLinus Torvalds 
18220640113bSLinus Torvalds 				if (S_ISLNK(inode->i_mode)) {
18230640113bSLinus Torvalds 					unsigned i_mode = S_IFLNK;
182430a08bf2SLinus Torvalds 					if (f_mode & FMODE_READ)
182530a08bf2SLinus Torvalds 						i_mode |= S_IRUSR | S_IXUSR;
182630a08bf2SLinus Torvalds 					if (f_mode & FMODE_WRITE)
182730a08bf2SLinus Torvalds 						i_mode |= S_IWUSR | S_IXUSR;
182830a08bf2SLinus Torvalds 					inode->i_mode = i_mode;
18290640113bSLinus Torvalds 				}
183030a08bf2SLinus Torvalds 
183128a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
183228a6d671SEric W. Biederman 				put_task_struct(task);
183328a6d671SEric W. Biederman 				return 1;
183428a6d671SEric W. Biederman 			}
183528a6d671SEric W. Biederman 			rcu_read_unlock();
183628a6d671SEric W. Biederman 			put_files_struct(files);
183728a6d671SEric W. Biederman 		}
183828a6d671SEric W. Biederman 		put_task_struct(task);
183928a6d671SEric W. Biederman 	}
184028a6d671SEric W. Biederman 	d_drop(dentry);
184128a6d671SEric W. Biederman 	return 0;
184228a6d671SEric W. Biederman }
184328a6d671SEric W. Biederman 
1844d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
184528a6d671SEric W. Biederman {
184628a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
184728a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
184828a6d671SEric W. Biederman };
184928a6d671SEric W. Biederman 
1850444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1851c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
185228a6d671SEric W. Biederman {
1853af5e6171SAlexey Dobriyan 	unsigned fd = (unsigned long)ptr;
185428a6d671SEric W. Biederman  	struct inode *inode;
185528a6d671SEric W. Biederman  	struct proc_inode *ei;
1856444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
185728a6d671SEric W. Biederman 
185861a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
185928a6d671SEric W. Biederman 	if (!inode)
186028a6d671SEric W. Biederman 		goto out;
186128a6d671SEric W. Biederman 	ei = PROC_I(inode);
186228a6d671SEric W. Biederman 	ei->fd = fd;
1863444ceed8SEric W. Biederman 
18640640113bSLinus Torvalds 	inode->i_mode = S_IFLNK;
18655e442a49SLinus Torvalds 	inode->i_op = &proc_pid_link_inode_operations;
186628a6d671SEric W. Biederman 	inode->i_size = 64;
186728a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
1868fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
186928a6d671SEric W. Biederman 	d_add(dentry, inode);
187028a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
18710b728e19SAl Viro 	if (tid_fd_revalidate(dentry, 0))
1872444ceed8SEric W. Biederman 		error = NULL;
1873444ceed8SEric W. Biederman 
1874444ceed8SEric W. Biederman  out:
1875444ceed8SEric W. Biederman 	return error;
1876444ceed8SEric W. Biederman }
1877444ceed8SEric W. Biederman 
187827932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
187927932742SMiklos Szeredi 					   struct dentry *dentry,
188027932742SMiklos Szeredi 					   instantiate_t instantiate)
1881444ceed8SEric W. Biederman {
1882444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1883444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1884444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1885444ceed8SEric W. Biederman 
1886444ceed8SEric W. Biederman 	if (!task)
1887444ceed8SEric W. Biederman 		goto out_no_task;
1888444ceed8SEric W. Biederman 	if (fd == ~0U)
1889444ceed8SEric W. Biederman 		goto out;
1890444ceed8SEric W. Biederman 
1891af5e6171SAlexey Dobriyan 	result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
189228a6d671SEric W. Biederman out:
189328a6d671SEric W. Biederman 	put_task_struct(task);
189428a6d671SEric W. Biederman out_no_task:
189528a6d671SEric W. Biederman 	return result;
189628a6d671SEric W. Biederman }
189728a6d671SEric W. Biederman 
189827932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
189927932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
19001da177e4SLinus Torvalds {
19012fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
19025634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
190399f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1904457c2510SPavel Emelyanov 	unsigned int fd, ino;
19051da177e4SLinus Torvalds 	int retval;
19061da177e4SLinus Torvalds 	struct files_struct * files;
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 	retval = -ENOENT;
190999f89551SEric W. Biederman 	if (!p)
191099f89551SEric W. Biederman 		goto out_no_task;
19111da177e4SLinus Torvalds 	retval = 0;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	fd = filp->f_pos;
19141da177e4SLinus Torvalds 	switch (fd) {
19151da177e4SLinus Torvalds 		case 0:
19161da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
19175e442a49SLinus Torvalds 				goto out;
19181da177e4SLinus Torvalds 			filp->f_pos++;
19191da177e4SLinus Torvalds 		case 1:
19205634708bSEric W. Biederman 			ino = parent_ino(dentry);
19211da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
19225e442a49SLinus Torvalds 				goto out;
19231da177e4SLinus Torvalds 			filp->f_pos++;
19241da177e4SLinus Torvalds 		default:
19251da177e4SLinus Torvalds 			files = get_files_struct(p);
19261da177e4SLinus Torvalds 			if (!files)
19275e442a49SLinus Torvalds 				goto out;
1928b835996fSDipankar Sarma 			rcu_read_lock();
19291da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
19309b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
19311da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
193227932742SMiklos Szeredi 				char name[PROC_NUMBUF];
193327932742SMiklos Szeredi 				int len;
1934f05ed3f1SAlexey Dobriyan 				int rv;
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
19371da177e4SLinus Torvalds 					continue;
1938b835996fSDipankar Sarma 				rcu_read_unlock();
19391da177e4SLinus Torvalds 
194027932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
1941f05ed3f1SAlexey Dobriyan 				rv = proc_fill_cache(filp, dirent, filldir,
1942f05ed3f1SAlexey Dobriyan 						     name, len, instantiate, p,
1943af5e6171SAlexey Dobriyan 						     (void *)(unsigned long)fd);
1944f05ed3f1SAlexey Dobriyan 				if (rv < 0)
1945f05ed3f1SAlexey Dobriyan 					goto out_fd_loop;
1946b835996fSDipankar Sarma 				rcu_read_lock();
19471da177e4SLinus Torvalds 			}
1948b835996fSDipankar Sarma 			rcu_read_unlock();
1949f05ed3f1SAlexey Dobriyan out_fd_loop:
19501da177e4SLinus Torvalds 			put_files_struct(files);
19511da177e4SLinus Torvalds 	}
19521da177e4SLinus Torvalds out:
195399f89551SEric W. Biederman 	put_task_struct(p);
195499f89551SEric W. Biederman out_no_task:
19551da177e4SLinus Torvalds 	return retval;
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds 
195827932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1959*00cd8dd3SAl Viro 				    unsigned int flags)
196027932742SMiklos Szeredi {
196127932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
196227932742SMiklos Szeredi }
196327932742SMiklos Szeredi 
196427932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
196527932742SMiklos Szeredi {
196627932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
196727932742SMiklos Szeredi }
196827932742SMiklos Szeredi 
196927932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
197027932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
197127932742SMiklos Szeredi {
197227932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
19733dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
197427932742SMiklos Szeredi 	if (!err)
197527932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
197627932742SMiklos Szeredi 	return err;
197727932742SMiklos Szeredi }
197827932742SMiklos Szeredi 
197927932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
198027932742SMiklos Szeredi 	.open           = nonseekable_open,
198127932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
19826038f373SArnd Bergmann 	.llseek		= no_llseek,
198327932742SMiklos Szeredi };
198427932742SMiklos Szeredi 
198500977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
19861da177e4SLinus Torvalds 	.read		= generic_read_dir,
19871da177e4SLinus Torvalds 	.readdir	= proc_readfd,
19886038f373SArnd Bergmann 	.llseek		= default_llseek,
19891da177e4SLinus Torvalds };
19901da177e4SLinus Torvalds 
1991640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
1992640708a2SPavel Emelyanov 
1993640708a2SPavel Emelyanov /*
1994640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1995640708a2SPavel Emelyanov  * which represent vma start and end addresses.
1996640708a2SPavel Emelyanov  */
1997640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
1998640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
1999640708a2SPavel Emelyanov {
2000640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2001640708a2SPavel Emelyanov 		return -EINVAL;
2002640708a2SPavel Emelyanov 
2003640708a2SPavel Emelyanov 	return 0;
2004640708a2SPavel Emelyanov }
2005640708a2SPavel Emelyanov 
20060b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
2007640708a2SPavel Emelyanov {
2008640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2009640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
2010640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
2011640708a2SPavel Emelyanov 	struct task_struct *task;
2012640708a2SPavel Emelyanov 	const struct cred *cred;
2013640708a2SPavel Emelyanov 	struct inode *inode;
2014640708a2SPavel Emelyanov 	int status = 0;
2015640708a2SPavel Emelyanov 
20160b728e19SAl Viro 	if (flags & LOOKUP_RCU)
2017640708a2SPavel Emelyanov 		return -ECHILD;
2018640708a2SPavel Emelyanov 
2019640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
2020640708a2SPavel Emelyanov 		status = -EACCES;
2021640708a2SPavel Emelyanov 		goto out_notask;
2022640708a2SPavel Emelyanov 	}
2023640708a2SPavel Emelyanov 
2024640708a2SPavel Emelyanov 	inode = dentry->d_inode;
2025640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2026640708a2SPavel Emelyanov 	if (!task)
2027640708a2SPavel Emelyanov 		goto out_notask;
2028640708a2SPavel Emelyanov 
20292344bec7SCong Wang 	mm = mm_access(task, PTRACE_MODE_READ);
20302344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
2031640708a2SPavel Emelyanov 		goto out;
2032640708a2SPavel Emelyanov 
2033640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2034640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2035640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2036640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2037640708a2SPavel Emelyanov 	}
2038640708a2SPavel Emelyanov 
2039640708a2SPavel Emelyanov 	mmput(mm);
2040640708a2SPavel Emelyanov 
2041640708a2SPavel Emelyanov 	if (exact_vma_exists) {
2042640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
2043640708a2SPavel Emelyanov 			rcu_read_lock();
2044640708a2SPavel Emelyanov 			cred = __task_cred(task);
2045640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
2046640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
2047640708a2SPavel Emelyanov 			rcu_read_unlock();
2048640708a2SPavel Emelyanov 		} else {
2049dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
2050dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
2051640708a2SPavel Emelyanov 		}
2052640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
2053640708a2SPavel Emelyanov 		status = 1;
2054640708a2SPavel Emelyanov 	}
2055640708a2SPavel Emelyanov 
2056640708a2SPavel Emelyanov out:
2057640708a2SPavel Emelyanov 	put_task_struct(task);
2058640708a2SPavel Emelyanov 
2059640708a2SPavel Emelyanov out_notask:
2060640708a2SPavel Emelyanov 	if (status <= 0)
2061640708a2SPavel Emelyanov 		d_drop(dentry);
2062640708a2SPavel Emelyanov 
2063640708a2SPavel Emelyanov 	return status;
2064640708a2SPavel Emelyanov }
2065640708a2SPavel Emelyanov 
2066640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
2067640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
2068640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
2069640708a2SPavel Emelyanov };
2070640708a2SPavel Emelyanov 
2071640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2072640708a2SPavel Emelyanov {
2073640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2074640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2075640708a2SPavel Emelyanov 	struct task_struct *task;
2076640708a2SPavel Emelyanov 	struct mm_struct *mm;
2077640708a2SPavel Emelyanov 	int rc;
2078640708a2SPavel Emelyanov 
2079640708a2SPavel Emelyanov 	rc = -ENOENT;
2080640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
2081640708a2SPavel Emelyanov 	if (!task)
2082640708a2SPavel Emelyanov 		goto out;
2083640708a2SPavel Emelyanov 
2084640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2085640708a2SPavel Emelyanov 	put_task_struct(task);
2086640708a2SPavel Emelyanov 	if (!mm)
2087640708a2SPavel Emelyanov 		goto out;
2088640708a2SPavel Emelyanov 
2089640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2090640708a2SPavel Emelyanov 	if (rc)
2091640708a2SPavel Emelyanov 		goto out_mmput;
2092640708a2SPavel Emelyanov 
2093640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2094640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2095640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
2096640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
2097640708a2SPavel Emelyanov 		path_get(path);
2098640708a2SPavel Emelyanov 		rc = 0;
2099640708a2SPavel Emelyanov 	}
2100640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2101640708a2SPavel Emelyanov 
2102640708a2SPavel Emelyanov out_mmput:
2103640708a2SPavel Emelyanov 	mmput(mm);
2104640708a2SPavel Emelyanov out:
2105640708a2SPavel Emelyanov 	return rc;
2106640708a2SPavel Emelyanov }
2107640708a2SPavel Emelyanov 
2108640708a2SPavel Emelyanov struct map_files_info {
2109640708a2SPavel Emelyanov 	struct file	*file;
2110640708a2SPavel Emelyanov 	unsigned long	len;
2111640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2112640708a2SPavel Emelyanov };
2113640708a2SPavel Emelyanov 
2114640708a2SPavel Emelyanov static struct dentry *
2115640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2116640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
2117640708a2SPavel Emelyanov {
2118640708a2SPavel Emelyanov 	const struct file *file = ptr;
2119640708a2SPavel Emelyanov 	struct proc_inode *ei;
2120640708a2SPavel Emelyanov 	struct inode *inode;
2121640708a2SPavel Emelyanov 
2122640708a2SPavel Emelyanov 	if (!file)
2123640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2124640708a2SPavel Emelyanov 
2125640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
2126640708a2SPavel Emelyanov 	if (!inode)
2127640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2128640708a2SPavel Emelyanov 
2129640708a2SPavel Emelyanov 	ei = PROC_I(inode);
2130640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
2131640708a2SPavel Emelyanov 
2132640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
2133640708a2SPavel Emelyanov 	inode->i_size = 64;
2134640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
2135640708a2SPavel Emelyanov 
2136640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_READ)
2137640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
2138640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_WRITE)
2139640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
2140640708a2SPavel Emelyanov 
2141640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
2142640708a2SPavel Emelyanov 	d_add(dentry, inode);
2143640708a2SPavel Emelyanov 
2144640708a2SPavel Emelyanov 	return NULL;
2145640708a2SPavel Emelyanov }
2146640708a2SPavel Emelyanov 
2147640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
2148*00cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
2149640708a2SPavel Emelyanov {
2150640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2151640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2152640708a2SPavel Emelyanov 	struct task_struct *task;
2153640708a2SPavel Emelyanov 	struct dentry *result;
2154640708a2SPavel Emelyanov 	struct mm_struct *mm;
2155640708a2SPavel Emelyanov 
2156640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2157640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2158640708a2SPavel Emelyanov 		goto out;
2159640708a2SPavel Emelyanov 
2160640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2161640708a2SPavel Emelyanov 	task = get_proc_task(dir);
2162640708a2SPavel Emelyanov 	if (!task)
2163640708a2SPavel Emelyanov 		goto out;
2164640708a2SPavel Emelyanov 
2165640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2166eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
2167640708a2SPavel Emelyanov 		goto out_put_task;
2168640708a2SPavel Emelyanov 
2169640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2170640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2171eb94cd96SCyrill Gorcunov 		goto out_put_task;
2172640708a2SPavel Emelyanov 
2173640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2174640708a2SPavel Emelyanov 	if (!mm)
2175eb94cd96SCyrill Gorcunov 		goto out_put_task;
2176640708a2SPavel Emelyanov 
2177640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2178640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2179640708a2SPavel Emelyanov 	if (!vma)
2180640708a2SPavel Emelyanov 		goto out_no_vma;
2181640708a2SPavel Emelyanov 
2182640708a2SPavel Emelyanov 	result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2183640708a2SPavel Emelyanov 
2184640708a2SPavel Emelyanov out_no_vma:
2185640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2186640708a2SPavel Emelyanov 	mmput(mm);
2187640708a2SPavel Emelyanov out_put_task:
2188640708a2SPavel Emelyanov 	put_task_struct(task);
2189640708a2SPavel Emelyanov out:
2190640708a2SPavel Emelyanov 	return result;
2191640708a2SPavel Emelyanov }
2192640708a2SPavel Emelyanov 
2193640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
2194640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
2195640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
2196640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
2197640708a2SPavel Emelyanov };
2198640708a2SPavel Emelyanov 
2199640708a2SPavel Emelyanov static int
2200640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2201640708a2SPavel Emelyanov {
2202640708a2SPavel Emelyanov 	struct dentry *dentry = filp->f_path.dentry;
2203640708a2SPavel Emelyanov 	struct inode *inode = dentry->d_inode;
2204640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2205640708a2SPavel Emelyanov 	struct task_struct *task;
2206640708a2SPavel Emelyanov 	struct mm_struct *mm;
2207640708a2SPavel Emelyanov 	ino_t ino;
2208640708a2SPavel Emelyanov 	int ret;
2209640708a2SPavel Emelyanov 
2210640708a2SPavel Emelyanov 	ret = -EACCES;
2211640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2212640708a2SPavel Emelyanov 		goto out;
2213640708a2SPavel Emelyanov 
2214640708a2SPavel Emelyanov 	ret = -ENOENT;
2215640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2216640708a2SPavel Emelyanov 	if (!task)
2217640708a2SPavel Emelyanov 		goto out;
2218640708a2SPavel Emelyanov 
2219640708a2SPavel Emelyanov 	ret = -EACCES;
2220eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
2221640708a2SPavel Emelyanov 		goto out_put_task;
2222640708a2SPavel Emelyanov 
2223640708a2SPavel Emelyanov 	ret = 0;
2224640708a2SPavel Emelyanov 	switch (filp->f_pos) {
2225640708a2SPavel Emelyanov 	case 0:
2226640708a2SPavel Emelyanov 		ino = inode->i_ino;
2227640708a2SPavel Emelyanov 		if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2228eb94cd96SCyrill Gorcunov 			goto out_put_task;
2229640708a2SPavel Emelyanov 		filp->f_pos++;
2230640708a2SPavel Emelyanov 	case 1:
2231640708a2SPavel Emelyanov 		ino = parent_ino(dentry);
2232640708a2SPavel Emelyanov 		if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2233eb94cd96SCyrill Gorcunov 			goto out_put_task;
2234640708a2SPavel Emelyanov 		filp->f_pos++;
2235640708a2SPavel Emelyanov 	default:
2236640708a2SPavel Emelyanov 	{
2237640708a2SPavel Emelyanov 		unsigned long nr_files, pos, i;
2238640708a2SPavel Emelyanov 		struct flex_array *fa = NULL;
2239640708a2SPavel Emelyanov 		struct map_files_info info;
2240640708a2SPavel Emelyanov 		struct map_files_info *p;
2241640708a2SPavel Emelyanov 
2242640708a2SPavel Emelyanov 		mm = get_task_mm(task);
2243640708a2SPavel Emelyanov 		if (!mm)
2244eb94cd96SCyrill Gorcunov 			goto out_put_task;
2245640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2246640708a2SPavel Emelyanov 
2247640708a2SPavel Emelyanov 		nr_files = 0;
2248640708a2SPavel Emelyanov 
2249640708a2SPavel Emelyanov 		/*
2250640708a2SPavel Emelyanov 		 * We need two passes here:
2251640708a2SPavel Emelyanov 		 *
2252640708a2SPavel Emelyanov 		 *  1) Collect vmas of mapped files with mmap_sem taken
2253640708a2SPavel Emelyanov 		 *  2) Release mmap_sem and instantiate entries
2254640708a2SPavel Emelyanov 		 *
2255640708a2SPavel Emelyanov 		 * otherwise we get lockdep complained, since filldir()
2256640708a2SPavel Emelyanov 		 * routine might require mmap_sem taken in might_fault().
2257640708a2SPavel Emelyanov 		 */
2258640708a2SPavel Emelyanov 
2259640708a2SPavel Emelyanov 		for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2260640708a2SPavel Emelyanov 			if (vma->vm_file && ++pos > filp->f_pos)
2261640708a2SPavel Emelyanov 				nr_files++;
2262640708a2SPavel Emelyanov 		}
2263640708a2SPavel Emelyanov 
2264640708a2SPavel Emelyanov 		if (nr_files) {
2265640708a2SPavel Emelyanov 			fa = flex_array_alloc(sizeof(info), nr_files,
2266640708a2SPavel Emelyanov 						GFP_KERNEL);
2267640708a2SPavel Emelyanov 			if (!fa || flex_array_prealloc(fa, 0, nr_files,
2268640708a2SPavel Emelyanov 							GFP_KERNEL)) {
2269640708a2SPavel Emelyanov 				ret = -ENOMEM;
2270640708a2SPavel Emelyanov 				if (fa)
2271640708a2SPavel Emelyanov 					flex_array_free(fa);
2272640708a2SPavel Emelyanov 				up_read(&mm->mmap_sem);
2273640708a2SPavel Emelyanov 				mmput(mm);
2274eb94cd96SCyrill Gorcunov 				goto out_put_task;
2275640708a2SPavel Emelyanov 			}
2276640708a2SPavel Emelyanov 			for (i = 0, vma = mm->mmap, pos = 2; vma;
2277640708a2SPavel Emelyanov 					vma = vma->vm_next) {
2278640708a2SPavel Emelyanov 				if (!vma->vm_file)
2279640708a2SPavel Emelyanov 					continue;
2280640708a2SPavel Emelyanov 				if (++pos <= filp->f_pos)
2281640708a2SPavel Emelyanov 					continue;
2282640708a2SPavel Emelyanov 
2283640708a2SPavel Emelyanov 				get_file(vma->vm_file);
2284640708a2SPavel Emelyanov 				info.file = vma->vm_file;
2285640708a2SPavel Emelyanov 				info.len = snprintf(info.name,
2286640708a2SPavel Emelyanov 						sizeof(info.name), "%lx-%lx",
2287640708a2SPavel Emelyanov 						vma->vm_start, vma->vm_end);
2288640708a2SPavel Emelyanov 				if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2289640708a2SPavel Emelyanov 					BUG();
2290640708a2SPavel Emelyanov 			}
2291640708a2SPavel Emelyanov 		}
2292640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2293640708a2SPavel Emelyanov 
2294640708a2SPavel Emelyanov 		for (i = 0; i < nr_files; i++) {
2295640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2296640708a2SPavel Emelyanov 			ret = proc_fill_cache(filp, dirent, filldir,
2297640708a2SPavel Emelyanov 					      p->name, p->len,
2298640708a2SPavel Emelyanov 					      proc_map_files_instantiate,
2299640708a2SPavel Emelyanov 					      task, p->file);
2300640708a2SPavel Emelyanov 			if (ret)
2301640708a2SPavel Emelyanov 				break;
2302640708a2SPavel Emelyanov 			filp->f_pos++;
2303640708a2SPavel Emelyanov 			fput(p->file);
2304640708a2SPavel Emelyanov 		}
2305640708a2SPavel Emelyanov 		for (; i < nr_files; i++) {
2306640708a2SPavel Emelyanov 			/*
2307640708a2SPavel Emelyanov 			 * In case of error don't forget
2308640708a2SPavel Emelyanov 			 * to put rest of file refs.
2309640708a2SPavel Emelyanov 			 */
2310640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2311640708a2SPavel Emelyanov 			fput(p->file);
2312640708a2SPavel Emelyanov 		}
2313640708a2SPavel Emelyanov 		if (fa)
2314640708a2SPavel Emelyanov 			flex_array_free(fa);
2315640708a2SPavel Emelyanov 		mmput(mm);
2316640708a2SPavel Emelyanov 	}
2317640708a2SPavel Emelyanov 	}
2318640708a2SPavel Emelyanov 
2319640708a2SPavel Emelyanov out_put_task:
2320640708a2SPavel Emelyanov 	put_task_struct(task);
2321640708a2SPavel Emelyanov out:
2322640708a2SPavel Emelyanov 	return ret;
2323640708a2SPavel Emelyanov }
2324640708a2SPavel Emelyanov 
2325640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2326640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2327640708a2SPavel Emelyanov 	.readdir	= proc_map_files_readdir,
2328640708a2SPavel Emelyanov 	.llseek		= default_llseek,
2329640708a2SPavel Emelyanov };
2330640708a2SPavel Emelyanov 
2331640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2332640708a2SPavel Emelyanov 
23331da177e4SLinus Torvalds /*
23348948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
23358948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
23368948e11fSAlexey Dobriyan  */
233710556cb2SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
23388948e11fSAlexey Dobriyan {
23392830ba7fSAl Viro 	int rv = generic_permission(inode, mask);
23408948e11fSAlexey Dobriyan 	if (rv == 0)
23418948e11fSAlexey Dobriyan 		return 0;
23428948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
23438948e11fSAlexey Dobriyan 		rv = 0;
23448948e11fSAlexey Dobriyan 	return rv;
23458948e11fSAlexey Dobriyan }
23468948e11fSAlexey Dobriyan 
23478948e11fSAlexey Dobriyan /*
23481da177e4SLinus Torvalds  * proc directories can do almost nothing..
23491da177e4SLinus Torvalds  */
2350c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
23511da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
23528948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
23536d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
23541da177e4SLinus Torvalds };
23551da177e4SLinus Torvalds 
235627932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
235727932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
235827932742SMiklos Szeredi {
2359af5e6171SAlexey Dobriyan 	unsigned fd = (unsigned long)ptr;
236027932742SMiklos Szeredi  	struct inode *inode;
236127932742SMiklos Szeredi  	struct proc_inode *ei;
236227932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
236327932742SMiklos Szeredi 
236427932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
236527932742SMiklos Szeredi 	if (!inode)
236627932742SMiklos Szeredi 		goto out;
236727932742SMiklos Szeredi 	ei = PROC_I(inode);
236827932742SMiklos Szeredi 	ei->fd = fd;
236927932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
237027932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
2371fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
237227932742SMiklos Szeredi 	d_add(dentry, inode);
237327932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
23740b728e19SAl Viro 	if (tid_fd_revalidate(dentry, 0))
237527932742SMiklos Szeredi 		error = NULL;
237627932742SMiklos Szeredi 
237727932742SMiklos Szeredi  out:
237827932742SMiklos Szeredi 	return error;
237927932742SMiklos Szeredi }
238027932742SMiklos Szeredi 
238127932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
238227932742SMiklos Szeredi 					struct dentry *dentry,
2383*00cd8dd3SAl Viro 					unsigned int flags)
238427932742SMiklos Szeredi {
238527932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
238627932742SMiklos Szeredi }
238727932742SMiklos Szeredi 
238827932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
238927932742SMiklos Szeredi {
239027932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
239127932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
239227932742SMiklos Szeredi }
239327932742SMiklos Szeredi 
239427932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
239527932742SMiklos Szeredi 	.read		= generic_read_dir,
239627932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
23976038f373SArnd Bergmann 	.llseek		= default_llseek,
239827932742SMiklos Szeredi };
239927932742SMiklos Szeredi 
240027932742SMiklos Szeredi /*
240127932742SMiklos Szeredi  * proc directories can do almost nothing..
240227932742SMiklos Szeredi  */
240327932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
240427932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
240527932742SMiklos Szeredi 	.setattr	= proc_setattr,
240627932742SMiklos Szeredi };
240727932742SMiklos Szeredi 
240827932742SMiklos Szeredi 
2409444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2410c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2411444ceed8SEric W. Biederman {
2412c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2413444ceed8SEric W. Biederman 	struct inode *inode;
2414444ceed8SEric W. Biederman 	struct proc_inode *ei;
2415bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2416444ceed8SEric W. Biederman 
241761a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2418444ceed8SEric W. Biederman 	if (!inode)
2419444ceed8SEric W. Biederman 		goto out;
2420444ceed8SEric W. Biederman 
2421444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2422444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2423444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2424bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2425444ceed8SEric W. Biederman 	if (p->iop)
2426444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2427444ceed8SEric W. Biederman 	if (p->fop)
2428444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2429444ceed8SEric W. Biederman 	ei->op = p->op;
2430fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2431444ceed8SEric W. Biederman 	d_add(dentry, inode);
2432444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
24330b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2434444ceed8SEric W. Biederman 		error = NULL;
2435444ceed8SEric W. Biederman out:
2436444ceed8SEric W. Biederman 	return error;
2437444ceed8SEric W. Biederman }
2438444ceed8SEric W. Biederman 
24391da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
24401da177e4SLinus Torvalds 					 struct dentry *dentry,
2441c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
24427bcd6b0eSEric W. Biederman 					 unsigned int nents)
24431da177e4SLinus Torvalds {
2444cd6a3ce9SEric W. Biederman 	struct dentry *error;
244599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2446c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
24471da177e4SLinus Torvalds 
2448cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
24491da177e4SLinus Torvalds 
245099f89551SEric W. Biederman 	if (!task)
245199f89551SEric W. Biederman 		goto out_no_task;
24521da177e4SLinus Torvalds 
245320cdc894SEric W. Biederman 	/*
245420cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
245520cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
245620cdc894SEric W. Biederman 	 */
24577bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
24587bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
24591da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
24601da177e4SLinus Torvalds 			continue;
24611da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
24621da177e4SLinus Torvalds 			break;
24631da177e4SLinus Torvalds 	}
24647bcd6b0eSEric W. Biederman 	if (p > last)
24651da177e4SLinus Torvalds 		goto out;
24661da177e4SLinus Torvalds 
2467444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
24681da177e4SLinus Torvalds out:
246999f89551SEric W. Biederman 	put_task_struct(task);
247099f89551SEric W. Biederman out_no_task:
2471cd6a3ce9SEric W. Biederman 	return error;
24721da177e4SLinus Torvalds }
24731da177e4SLinus Torvalds 
2474c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2475c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
247661a28784SEric W. Biederman {
247761a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
247861a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
247961a28784SEric W. Biederman }
248061a28784SEric W. Biederman 
248128a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
248228a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2483c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
248428a6d671SEric W. Biederman {
248528a6d671SEric W. Biederman 	int i;
24862fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
248728a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
248828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2489c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
249028a6d671SEric W. Biederman 	ino_t ino;
249128a6d671SEric W. Biederman 	int ret;
249228a6d671SEric W. Biederman 
249328a6d671SEric W. Biederman 	ret = -ENOENT;
249428a6d671SEric W. Biederman 	if (!task)
249561a28784SEric W. Biederman 		goto out_no_task;
249628a6d671SEric W. Biederman 
249728a6d671SEric W. Biederman 	ret = 0;
249828a6d671SEric W. Biederman 	i = filp->f_pos;
249928a6d671SEric W. Biederman 	switch (i) {
250028a6d671SEric W. Biederman 	case 0:
250128a6d671SEric W. Biederman 		ino = inode->i_ino;
250228a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
250328a6d671SEric W. Biederman 			goto out;
250428a6d671SEric W. Biederman 		i++;
250528a6d671SEric W. Biederman 		filp->f_pos++;
250628a6d671SEric W. Biederman 		/* fall through */
250728a6d671SEric W. Biederman 	case 1:
250828a6d671SEric W. Biederman 		ino = parent_ino(dentry);
250928a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
251028a6d671SEric W. Biederman 			goto out;
251128a6d671SEric W. Biederman 		i++;
251228a6d671SEric W. Biederman 		filp->f_pos++;
251328a6d671SEric W. Biederman 		/* fall through */
251428a6d671SEric W. Biederman 	default:
251528a6d671SEric W. Biederman 		i -= 2;
251628a6d671SEric W. Biederman 		if (i >= nents) {
251728a6d671SEric W. Biederman 			ret = 1;
251828a6d671SEric W. Biederman 			goto out;
251928a6d671SEric W. Biederman 		}
252028a6d671SEric W. Biederman 		p = ents + i;
25217bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
25227bcd6b0eSEric W. Biederman 		while (p <= last) {
252361a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
252428a6d671SEric W. Biederman 				goto out;
252528a6d671SEric W. Biederman 			filp->f_pos++;
252628a6d671SEric W. Biederman 			p++;
252728a6d671SEric W. Biederman 		}
25281da177e4SLinus Torvalds 	}
25291da177e4SLinus Torvalds 
253028a6d671SEric W. Biederman 	ret = 1;
253128a6d671SEric W. Biederman out:
253261a28784SEric W. Biederman 	put_task_struct(task);
253361a28784SEric W. Biederman out_no_task:
253428a6d671SEric W. Biederman 	return ret;
25351da177e4SLinus Torvalds }
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
253828a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
253928a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
254028a6d671SEric W. Biederman {
25412fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
254204ff9708SAl Viro 	char *p = NULL;
254328a6d671SEric W. Biederman 	ssize_t length;
254428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
254528a6d671SEric W. Biederman 
254628a6d671SEric W. Biederman 	if (!task)
254704ff9708SAl Viro 		return -ESRCH;
254828a6d671SEric W. Biederman 
254928a6d671SEric W. Biederman 	length = security_getprocattr(task,
25502fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
255104ff9708SAl Viro 				      &p);
255228a6d671SEric W. Biederman 	put_task_struct(task);
255304ff9708SAl Viro 	if (length > 0)
255404ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
255504ff9708SAl Viro 	kfree(p);
255628a6d671SEric W. Biederman 	return length;
255728a6d671SEric W. Biederman }
255828a6d671SEric W. Biederman 
255928a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
256028a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
256128a6d671SEric W. Biederman {
25622fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
256328a6d671SEric W. Biederman 	char *page;
256428a6d671SEric W. Biederman 	ssize_t length;
256528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
256628a6d671SEric W. Biederman 
256728a6d671SEric W. Biederman 	length = -ESRCH;
256828a6d671SEric W. Biederman 	if (!task)
256928a6d671SEric W. Biederman 		goto out_no_task;
257028a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
257128a6d671SEric W. Biederman 		count = PAGE_SIZE;
257228a6d671SEric W. Biederman 
257328a6d671SEric W. Biederman 	/* No partial writes. */
257428a6d671SEric W. Biederman 	length = -EINVAL;
257528a6d671SEric W. Biederman 	if (*ppos != 0)
257628a6d671SEric W. Biederman 		goto out;
257728a6d671SEric W. Biederman 
257828a6d671SEric W. Biederman 	length = -ENOMEM;
2579e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
258028a6d671SEric W. Biederman 	if (!page)
258128a6d671SEric W. Biederman 		goto out;
258228a6d671SEric W. Biederman 
258328a6d671SEric W. Biederman 	length = -EFAULT;
258428a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
258528a6d671SEric W. Biederman 		goto out_free;
258628a6d671SEric W. Biederman 
2587107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
25889b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2589107db7c7SDavid Howells 	if (length < 0)
2590107db7c7SDavid Howells 		goto out_free;
2591107db7c7SDavid Howells 
259228a6d671SEric W. Biederman 	length = security_setprocattr(task,
25932fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
259428a6d671SEric W. Biederman 				      (void*)page, count);
25959b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
259628a6d671SEric W. Biederman out_free:
259728a6d671SEric W. Biederman 	free_page((unsigned long) page);
259828a6d671SEric W. Biederman out:
259928a6d671SEric W. Biederman 	put_task_struct(task);
260028a6d671SEric W. Biederman out_no_task:
260128a6d671SEric W. Biederman 	return length;
260228a6d671SEric W. Biederman }
260328a6d671SEric W. Biederman 
260400977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
260528a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
260628a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
260787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
260828a6d671SEric W. Biederman };
260928a6d671SEric W. Biederman 
2610c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2611631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2612631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2613631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2614631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2615631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2616631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
261728a6d671SEric W. Biederman };
261828a6d671SEric W. Biederman 
261972d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
26201da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
26211da177e4SLinus Torvalds {
26221da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
262372d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
26241da177e4SLinus Torvalds }
26251da177e4SLinus Torvalds 
262600977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
26271da177e4SLinus Torvalds 	.read		= generic_read_dir,
262872d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
26296038f373SArnd Bergmann 	.llseek		= default_llseek,
26301da177e4SLinus Torvalds };
26311da177e4SLinus Torvalds 
263272d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2633*00cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
26341da177e4SLinus Torvalds {
26357bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26367bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
26371da177e4SLinus Torvalds }
26381da177e4SLinus Torvalds 
2639c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
264072d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
264199f89551SEric W. Biederman 	.getattr	= pid_getattr,
26426d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
26431da177e4SLinus Torvalds };
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds #endif
26461da177e4SLinus Torvalds 
2647698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
26483cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
26493cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
26503cb4a0bbSKawai, Hidehiro {
26513cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
26523cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26533cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
26543cb4a0bbSKawai, Hidehiro 	size_t len;
26553cb4a0bbSKawai, Hidehiro 	int ret;
26563cb4a0bbSKawai, Hidehiro 
26573cb4a0bbSKawai, Hidehiro 	if (!task)
26583cb4a0bbSKawai, Hidehiro 		return -ESRCH;
26593cb4a0bbSKawai, Hidehiro 
26603cb4a0bbSKawai, Hidehiro 	ret = 0;
26613cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
26623cb4a0bbSKawai, Hidehiro 	if (mm) {
26633cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
26643cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
26653cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
26663cb4a0bbSKawai, Hidehiro 		mmput(mm);
26673cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
26683cb4a0bbSKawai, Hidehiro 	}
26693cb4a0bbSKawai, Hidehiro 
26703cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
26713cb4a0bbSKawai, Hidehiro 
26723cb4a0bbSKawai, Hidehiro 	return ret;
26733cb4a0bbSKawai, Hidehiro }
26743cb4a0bbSKawai, Hidehiro 
26753cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
26763cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
26773cb4a0bbSKawai, Hidehiro 					  size_t count,
26783cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
26793cb4a0bbSKawai, Hidehiro {
26803cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
26813cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26823cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
26833cb4a0bbSKawai, Hidehiro 	unsigned int val;
26843cb4a0bbSKawai, Hidehiro 	int ret;
26853cb4a0bbSKawai, Hidehiro 	int i;
26863cb4a0bbSKawai, Hidehiro 	unsigned long mask;
26873cb4a0bbSKawai, Hidehiro 
26883cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
26893cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
26903cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
26913cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
26923cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
26933cb4a0bbSKawai, Hidehiro 		goto out_no_task;
26943cb4a0bbSKawai, Hidehiro 
26953cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
26963cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
26973cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
26983cb4a0bbSKawai, Hidehiro 		end++;
26993cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
27003cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27013cb4a0bbSKawai, Hidehiro 
27023cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
27033cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
27043cb4a0bbSKawai, Hidehiro 	if (!task)
27053cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27063cb4a0bbSKawai, Hidehiro 
27073cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
27083cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
27093cb4a0bbSKawai, Hidehiro 	if (!mm)
27103cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
27113cb4a0bbSKawai, Hidehiro 
27123cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
27133cb4a0bbSKawai, Hidehiro 		if (val & mask)
27143cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27153cb4a0bbSKawai, Hidehiro 		else
27163cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27173cb4a0bbSKawai, Hidehiro 	}
27183cb4a0bbSKawai, Hidehiro 
27193cb4a0bbSKawai, Hidehiro 	mmput(mm);
27203cb4a0bbSKawai, Hidehiro  out_no_mm:
27213cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
27223cb4a0bbSKawai, Hidehiro  out_no_task:
27233cb4a0bbSKawai, Hidehiro 	return ret;
27243cb4a0bbSKawai, Hidehiro }
27253cb4a0bbSKawai, Hidehiro 
27263cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
27273cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
27283cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
272987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
27303cb4a0bbSKawai, Hidehiro };
27313cb4a0bbSKawai, Hidehiro #endif
27323cb4a0bbSKawai, Hidehiro 
27331da177e4SLinus Torvalds /*
27341da177e4SLinus Torvalds  * /proc/self:
27351da177e4SLinus Torvalds  */
27361da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
27371da177e4SLinus Torvalds 			      int buflen)
27381da177e4SLinus Torvalds {
2739488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2740b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
27418578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2742b55fcb22SAndrew Morton 	if (!tgid)
2743488e5bc4SEric W. Biederman 		return -ENOENT;
2744b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
27451da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
2748008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
27491da177e4SLinus Torvalds {
2750488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2751b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
27527fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
27537fee4868SAl Viro 	if (tgid) {
27547fee4868SAl Viro 		name = __getname();
27557fee4868SAl Viro 		if (!name)
27567fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
27577fee4868SAl Viro 		else
27587fee4868SAl Viro 			sprintf(name, "%d", tgid);
27597fee4868SAl Viro 	}
27607fee4868SAl Viro 	nd_set_link(nd, name);
27617fee4868SAl Viro 	return NULL;
27627fee4868SAl Viro }
27637fee4868SAl Viro 
27647fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
27657fee4868SAl Viro 				void *cookie)
27667fee4868SAl Viro {
27677fee4868SAl Viro 	char *s = nd_get_link(nd);
27687fee4868SAl Viro 	if (!IS_ERR(s))
27697fee4868SAl Viro 		__putname(s);
27701da177e4SLinus Torvalds }
27711da177e4SLinus Torvalds 
2772c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
27731da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
27741da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
27757fee4868SAl Viro 	.put_link	= proc_self_put_link,
27761da177e4SLinus Torvalds };
27771da177e4SLinus Torvalds 
277828a6d671SEric W. Biederman /*
2779801199ceSEric W. Biederman  * proc base
2780801199ceSEric W. Biederman  *
2781801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2782801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2783801199ceSEric W. Biederman  * describe something that is process related.
2784801199ceSEric W. Biederman  */
2785c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
278661a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2787801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2788801199ceSEric W. Biederman };
2789801199ceSEric W. Biederman 
2790444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2791c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2792801199ceSEric W. Biederman {
2793c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2794801199ceSEric W. Biederman 	struct inode *inode;
2795801199ceSEric W. Biederman 	struct proc_inode *ei;
279673d36460SDan Carpenter 	struct dentry *error;
2797801199ceSEric W. Biederman 
2798801199ceSEric W. Biederman 	/* Allocate the inode */
2799801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2800801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2801801199ceSEric W. Biederman 	if (!inode)
2802801199ceSEric W. Biederman 		goto out;
2803801199ceSEric W. Biederman 
2804801199ceSEric W. Biederman 	/* Initialize the inode */
2805801199ceSEric W. Biederman 	ei = PROC_I(inode);
280685fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2807801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2808801199ceSEric W. Biederman 
2809801199ceSEric W. Biederman 	/*
2810801199ceSEric W. Biederman 	 * grab the reference to the task.
2811801199ceSEric W. Biederman 	 */
28121a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2813801199ceSEric W. Biederman 	if (!ei->pid)
2814801199ceSEric W. Biederman 		goto out_iput;
2815801199ceSEric W. Biederman 
2816801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2817801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2818bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
2819801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2820801199ceSEric W. Biederman 		inode->i_size = 64;
2821801199ceSEric W. Biederman 	if (p->iop)
2822801199ceSEric W. Biederman 		inode->i_op = p->iop;
2823801199ceSEric W. Biederman 	if (p->fop)
2824801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2825801199ceSEric W. Biederman 	ei->op = p->op;
2826801199ceSEric W. Biederman 	d_add(dentry, inode);
2827801199ceSEric W. Biederman 	error = NULL;
2828801199ceSEric W. Biederman out:
2829801199ceSEric W. Biederman 	return error;
2830801199ceSEric W. Biederman out_iput:
2831801199ceSEric W. Biederman 	iput(inode);
2832801199ceSEric W. Biederman 	goto out;
2833801199ceSEric W. Biederman }
2834801199ceSEric W. Biederman 
2835444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2836444ceed8SEric W. Biederman {
2837444ceed8SEric W. Biederman 	struct dentry *error;
2838444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2839c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2840444ceed8SEric W. Biederman 
2841444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2842444ceed8SEric W. Biederman 
2843444ceed8SEric W. Biederman 	if (!task)
2844444ceed8SEric W. Biederman 		goto out_no_task;
2845444ceed8SEric W. Biederman 
2846444ceed8SEric W. Biederman 	/* Lookup the directory entry */
28477bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
28487bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2849444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2850444ceed8SEric W. Biederman 			continue;
2851444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2852444ceed8SEric W. Biederman 			break;
2853444ceed8SEric W. Biederman 	}
28547bcd6b0eSEric W. Biederman 	if (p > last)
2855444ceed8SEric W. Biederman 		goto out;
2856444ceed8SEric W. Biederman 
2857444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2858444ceed8SEric W. Biederman 
2859444ceed8SEric W. Biederman out:
2860444ceed8SEric W. Biederman 	put_task_struct(task);
2861444ceed8SEric W. Biederman out_no_task:
2862444ceed8SEric W. Biederman 	return error;
2863444ceed8SEric W. Biederman }
2864444ceed8SEric W. Biederman 
2865c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2866c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
286761a28784SEric W. Biederman {
286861a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
286961a28784SEric W. Biederman 				proc_base_instantiate, task, p);
287061a28784SEric W. Biederman }
287161a28784SEric W. Biederman 
2872aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2873297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2874aba76fdbSAndrew Morton {
2875940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2876297c5d92SAndrea Righi 	unsigned long flags;
2877293eb1e7SVasiliy Kulikov 	int result;
2878297c5d92SAndrea Righi 
2879293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2880293eb1e7SVasiliy Kulikov 	if (result)
2881293eb1e7SVasiliy Kulikov 		return result;
2882293eb1e7SVasiliy Kulikov 
2883293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2884293eb1e7SVasiliy Kulikov 		result = -EACCES;
2885293eb1e7SVasiliy Kulikov 		goto out_unlock;
2886293eb1e7SVasiliy Kulikov 	}
28871d1221f3SVasiliy Kulikov 
28885995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2889b2d002dbSAndrea Righi 		struct task_struct *t = task;
2890297c5d92SAndrea Righi 
28915995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
28925995477aSAndrea Righi 		while_each_thread(task, t)
28935995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2894297c5d92SAndrea Righi 
2895297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2896297c5d92SAndrea Righi 	}
2897293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2898aba76fdbSAndrew Morton 			"rchar: %llu\n"
2899aba76fdbSAndrew Morton 			"wchar: %llu\n"
2900aba76fdbSAndrew Morton 			"syscr: %llu\n"
2901aba76fdbSAndrew Morton 			"syscw: %llu\n"
2902aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2903aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2904aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
29057c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
29067c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
29077c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
29087c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
29097c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
29107c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
29117c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2912293eb1e7SVasiliy Kulikov out_unlock:
2913293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2914293eb1e7SVasiliy Kulikov 	return result;
2915aba76fdbSAndrew Morton }
2916297c5d92SAndrea Righi 
2917297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2918297c5d92SAndrea Righi {
2919297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2920297c5d92SAndrea Righi }
2921297c5d92SAndrea Righi 
2922297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2923297c5d92SAndrea Righi {
2924297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2925297c5d92SAndrea Righi }
2926297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2927aba76fdbSAndrew Morton 
292822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
292922d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
293022d917d8SEric W. Biederman 	struct seq_operations *seq_ops)
293122d917d8SEric W. Biederman {
293222d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
293322d917d8SEric W. Biederman 	struct task_struct *task;
293422d917d8SEric W. Biederman 	struct seq_file *seq;
293522d917d8SEric W. Biederman 	int ret = -EINVAL;
293622d917d8SEric W. Biederman 
293722d917d8SEric W. Biederman 	task = get_proc_task(inode);
293822d917d8SEric W. Biederman 	if (task) {
293922d917d8SEric W. Biederman 		rcu_read_lock();
294022d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
294122d917d8SEric W. Biederman 		rcu_read_unlock();
294222d917d8SEric W. Biederman 		put_task_struct(task);
294322d917d8SEric W. Biederman 	}
294422d917d8SEric W. Biederman 	if (!ns)
294522d917d8SEric W. Biederman 		goto err;
294622d917d8SEric W. Biederman 
294722d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
294822d917d8SEric W. Biederman 	if (ret)
294922d917d8SEric W. Biederman 		goto err_put_ns;
295022d917d8SEric W. Biederman 
295122d917d8SEric W. Biederman 	seq = file->private_data;
295222d917d8SEric W. Biederman 	seq->private = ns;
295322d917d8SEric W. Biederman 
295422d917d8SEric W. Biederman 	return 0;
295522d917d8SEric W. Biederman err_put_ns:
295622d917d8SEric W. Biederman 	put_user_ns(ns);
295722d917d8SEric W. Biederman err:
295822d917d8SEric W. Biederman 	return ret;
295922d917d8SEric W. Biederman }
296022d917d8SEric W. Biederman 
296122d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
296222d917d8SEric W. Biederman {
296322d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
296422d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
296522d917d8SEric W. Biederman 	put_user_ns(ns);
296622d917d8SEric W. Biederman 	return seq_release(inode, file);
296722d917d8SEric W. Biederman }
296822d917d8SEric W. Biederman 
296922d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
297022d917d8SEric W. Biederman {
297122d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
297222d917d8SEric W. Biederman }
297322d917d8SEric W. Biederman 
297422d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
297522d917d8SEric W. Biederman {
297622d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
297722d917d8SEric W. Biederman }
297822d917d8SEric W. Biederman 
297922d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
298022d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
298122d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
298222d917d8SEric W. Biederman 	.read		= seq_read,
298322d917d8SEric W. Biederman 	.llseek		= seq_lseek,
298422d917d8SEric W. Biederman 	.release	= proc_id_map_release,
298522d917d8SEric W. Biederman };
298622d917d8SEric W. Biederman 
298722d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
298822d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
298922d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
299022d917d8SEric W. Biederman 	.read		= seq_read,
299122d917d8SEric W. Biederman 	.llseek		= seq_lseek,
299222d917d8SEric W. Biederman 	.release	= proc_id_map_release,
299322d917d8SEric W. Biederman };
299422d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
299522d917d8SEric W. Biederman 
299647830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
299747830723SKees Cook 				struct pid *pid, struct task_struct *task)
299847830723SKees Cook {
2999a9712bc1SAl Viro 	int err = lock_trace(task);
3000a9712bc1SAl Viro 	if (!err) {
300147830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
3002a9712bc1SAl Viro 		unlock_trace(task);
3003a9712bc1SAl Viro 	}
3004a9712bc1SAl Viro 	return err;
300547830723SKees Cook }
300647830723SKees Cook 
3007801199ceSEric W. Biederman /*
300828a6d671SEric W. Biederman  * Thread groups
300928a6d671SEric W. Biederman  */
301000977a59SArjan van de Ven static const struct file_operations proc_task_operations;
3011c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
301220cdc894SEric W. Biederman 
3013c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
3014631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3015631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3016640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
3017640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3018640708a2SPavel Emelyanov #endif
3019631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
30206b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3021b2211a36SAndrew Morton #ifdef CONFIG_NET
3022631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3023b2211a36SAndrew Morton #endif
3024631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
3025631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
3026631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
3027a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
30283036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
302943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3030631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
303143ae34cbSIngo Molnar #endif
30325091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
30335091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
30345091faa4SMike Galbraith #endif
30354614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3036ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3037a9712bc1SAl Viro 	INF("syscall",    S_IRUGO, proc_pid_syscall),
3038ebcb6734SRoland McGrath #endif
3039631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
3040631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
3041631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
3042b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
304328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3044b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
304528a6d671SEric W. Biederman #endif
3046631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3047631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
3048631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
3049631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
3050631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
3051631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3052631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
30531e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3054631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3055b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
3056ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
305728a6d671SEric W. Biederman #endif
305828a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3059631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
306028a6d671SEric W. Biederman #endif
306128a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3062631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
306328a6d671SEric W. Biederman #endif
30642ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3065a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
306628a6d671SEric W. Biederman #endif
306728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3068631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
306928a6d671SEric W. Biederman #endif
30709745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3071631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30729745512cSArjan van de Ven #endif
30738793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3074631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
307528a6d671SEric W. Biederman #endif
3076a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3077631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3078a424316cSPaul Menage #endif
3079631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
3080631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3081a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
308228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3083631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3084631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
308528a6d671SEric W. Biederman #endif
3086f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3087631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3088f4f154fdSAkinobu Mita #endif
3089698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
3090631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
30913cb4a0bbSKawai, Hidehiro #endif
3092aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
30931d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
3094aba76fdbSAndrew Morton #endif
3095f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3096f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3097f133eccaSChris Metcalf #endif
309822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
309922d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
310022d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
310122d917d8SEric W. Biederman #endif
310228a6d671SEric W. Biederman };
310328a6d671SEric W. Biederman 
310428a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
310528a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
310628a6d671SEric W. Biederman {
310728a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
310828a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
310928a6d671SEric W. Biederman }
311028a6d671SEric W. Biederman 
311100977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
311228a6d671SEric W. Biederman 	.read		= generic_read_dir,
311328a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
31146038f373SArnd Bergmann 	.llseek		= default_llseek,
311528a6d671SEric W. Biederman };
311628a6d671SEric W. Biederman 
3117*00cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3118*00cd8dd3SAl Viro {
31197bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
31207bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
312128a6d671SEric W. Biederman }
312228a6d671SEric W. Biederman 
3123c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
312428a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
312528a6d671SEric W. Biederman 	.getattr	= pid_getattr,
312628a6d671SEric W. Biederman 	.setattr	= proc_setattr,
31270499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
312828a6d671SEric W. Biederman };
312928a6d671SEric W. Biederman 
313060347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
31311da177e4SLinus Torvalds {
313248e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
31338578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
313448e6484dSEric W. Biederman 	struct qstr name;
31351da177e4SLinus Torvalds 
313648e6484dSEric W. Biederman 	name.name = buf;
313760347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
313860347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
313948e6484dSEric W. Biederman 	if (dentry) {
314048e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
314148e6484dSEric W. Biederman 		d_drop(dentry);
314248e6484dSEric W. Biederman 		dput(dentry);
31431da177e4SLinus Torvalds 	}
31441da177e4SLinus Torvalds 
314548e6484dSEric W. Biederman 	name.name = buf;
314660347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
314760347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
314848e6484dSEric W. Biederman 	if (!leader)
314948e6484dSEric W. Biederman 		goto out;
315048e6484dSEric W. Biederman 
315148e6484dSEric W. Biederman 	name.name = "task";
315248e6484dSEric W. Biederman 	name.len = strlen(name.name);
315348e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
315448e6484dSEric W. Biederman 	if (!dir)
315548e6484dSEric W. Biederman 		goto out_put_leader;
315648e6484dSEric W. Biederman 
315748e6484dSEric W. Biederman 	name.name = buf;
315860347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
315948e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
316048e6484dSEric W. Biederman 	if (dentry) {
316148e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
316248e6484dSEric W. Biederman 		d_drop(dentry);
316348e6484dSEric W. Biederman 		dput(dentry);
31641da177e4SLinus Torvalds 	}
316548e6484dSEric W. Biederman 
316648e6484dSEric W. Biederman 	dput(dir);
316748e6484dSEric W. Biederman out_put_leader:
316848e6484dSEric W. Biederman 	dput(leader);
316948e6484dSEric W. Biederman out:
317048e6484dSEric W. Biederman 	return;
31711da177e4SLinus Torvalds }
31721da177e4SLinus Torvalds 
31730895e91dSRandy Dunlap /**
31740895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
31750895e91dSRandy Dunlap  * @task: task that should be flushed.
31760895e91dSRandy Dunlap  *
31770895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
317860347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
31790895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
31800895e91dSRandy Dunlap  *
31810895e91dSRandy Dunlap  * Looks in the dcache for
31820895e91dSRandy Dunlap  * /proc/@pid
31830895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
31840895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
31850895e91dSRandy Dunlap  * from the dcache.
31860895e91dSRandy Dunlap  *
31870895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
31880895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
31890895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
31900895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
31910895e91dSRandy Dunlap  * dcache entries at process exit time.
31920895e91dSRandy Dunlap  *
31930895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
31940895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
31950895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
319660347f67SPavel Emelyanov  */
319760347f67SPavel Emelyanov 
319860347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
319960347f67SPavel Emelyanov {
32009fcc2d15SEric W. Biederman 	int i;
32019b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
3202130f77ecSPavel Emelyanov 	struct upid *upid;
3203130f77ecSPavel Emelyanov 
3204130f77ecSPavel Emelyanov 	pid = task_pid(task);
3205130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
32069fcc2d15SEric W. Biederman 
32079fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
3208130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
3209130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
32109b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
3211130f77ecSPavel Emelyanov 	}
32126f4e6433SPavel Emelyanov 
32136f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
32146f4e6433SPavel Emelyanov 	if (upid->nr == 1)
32156f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
321660347f67SPavel Emelyanov }
321760347f67SPavel Emelyanov 
32189711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
32199711ef99SAdrian Bunk 					   struct dentry * dentry,
3220c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
3221444ceed8SEric W. Biederman {
3222444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3223444ceed8SEric W. Biederman 	struct inode *inode;
3224444ceed8SEric W. Biederman 
322561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3226444ceed8SEric W. Biederman 	if (!inode)
3227444ceed8SEric W. Biederman 		goto out;
3228444ceed8SEric W. Biederman 
3229444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3230444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
3231444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
3232444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3233aed54175SVegard Nossum 
3234bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3235bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
3236444ceed8SEric W. Biederman 
3237fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3238444ceed8SEric W. Biederman 
3239444ceed8SEric W. Biederman 	d_add(dentry, inode);
3240444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
32410b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3242444ceed8SEric W. Biederman 		error = NULL;
3243444ceed8SEric W. Biederman out:
3244444ceed8SEric W. Biederman 	return error;
3245444ceed8SEric W. Biederman }
3246444ceed8SEric W. Biederman 
3247*00cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
32481da177e4SLinus Torvalds {
324973d36460SDan Carpenter 	struct dentry *result;
32501da177e4SLinus Torvalds 	struct task_struct *task;
32511da177e4SLinus Torvalds 	unsigned tgid;
3252b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32531da177e4SLinus Torvalds 
3254801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
3255801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3256801199ceSEric W. Biederman 		goto out;
3257801199ceSEric W. Biederman 
32581da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
32591da177e4SLinus Torvalds 	if (tgid == ~0U)
32601da177e4SLinus Torvalds 		goto out;
32611da177e4SLinus Torvalds 
3262b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3263de758734SEric W. Biederman 	rcu_read_lock();
3264b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
32651da177e4SLinus Torvalds 	if (task)
32661da177e4SLinus Torvalds 		get_task_struct(task);
3267de758734SEric W. Biederman 	rcu_read_unlock();
32681da177e4SLinus Torvalds 	if (!task)
32691da177e4SLinus Torvalds 		goto out;
32701da177e4SLinus Torvalds 
3271444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
327248e6484dSEric W. Biederman 	put_task_struct(task);
32731da177e4SLinus Torvalds out:
3274cd6a3ce9SEric W. Biederman 	return result;
32751da177e4SLinus Torvalds }
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds /*
32780804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
32790bc58a91SEric W. Biederman  *
32801da177e4SLinus Torvalds  */
328119fd4bb2SEric W. Biederman struct tgid_iter {
328219fd4bb2SEric W. Biederman 	unsigned int tgid;
32830804ef4bSEric W. Biederman 	struct task_struct *task;
328419fd4bb2SEric W. Biederman };
328519fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
328619fd4bb2SEric W. Biederman {
32870804ef4bSEric W. Biederman 	struct pid *pid;
32881da177e4SLinus Torvalds 
328919fd4bb2SEric W. Biederman 	if (iter.task)
329019fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
32910804ef4bSEric W. Biederman 	rcu_read_lock();
32920804ef4bSEric W. Biederman retry:
329319fd4bb2SEric W. Biederman 	iter.task = NULL;
329419fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
32950804ef4bSEric W. Biederman 	if (pid) {
329619fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
329719fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
32980804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
32990804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
33000804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
33010804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
33020804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
33030804ef4bSEric W. Biederman 		 *
33040804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
33050804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
33060804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
33070804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
33080804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
33090bc58a91SEric W. Biederman 		 */
331019fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
331119fd4bb2SEric W. Biederman 			iter.tgid += 1;
33120804ef4bSEric W. Biederman 			goto retry;
331319fd4bb2SEric W. Biederman 		}
331419fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
33151da177e4SLinus Torvalds 	}
3316454cc105SEric W. Biederman 	rcu_read_unlock();
331719fd4bb2SEric W. Biederman 	return iter;
33181da177e4SLinus Torvalds }
33191da177e4SLinus Torvalds 
33207bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
33211da177e4SLinus Torvalds 
332261a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
332319fd4bb2SEric W. Biederman 	struct tgid_iter iter)
332461a28784SEric W. Biederman {
332561a28784SEric W. Biederman 	char name[PROC_NUMBUF];
332619fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
332761a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
332819fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
332961a28784SEric W. Biederman }
333061a28784SEric W. Biederman 
33310499680aSVasiliy Kulikov static int fake_filldir(void *buf, const char *name, int namelen,
33320499680aSVasiliy Kulikov 			loff_t offset, u64 ino, unsigned d_type)
33330499680aSVasiliy Kulikov {
33340499680aSVasiliy Kulikov 	return 0;
33350499680aSVasiliy Kulikov }
33360499680aSVasiliy Kulikov 
33371da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
33381da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
33391da177e4SLinus Torvalds {
3340d8bdc59fSLinus Torvalds 	unsigned int nr;
3341d8bdc59fSLinus Torvalds 	struct task_struct *reaper;
334219fd4bb2SEric W. Biederman 	struct tgid_iter iter;
3343b488893aSPavel Emelyanov 	struct pid_namespace *ns;
33440499680aSVasiliy Kulikov 	filldir_t __filldir;
33451da177e4SLinus Torvalds 
3346d8bdc59fSLinus Torvalds 	if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3347d8bdc59fSLinus Torvalds 		goto out_no_task;
3348d8bdc59fSLinus Torvalds 	nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3349d8bdc59fSLinus Torvalds 
3350d8bdc59fSLinus Torvalds 	reaper = get_proc_task(filp->f_path.dentry->d_inode);
335161a28784SEric W. Biederman 	if (!reaper)
335261a28784SEric W. Biederman 		goto out_no_task;
335361a28784SEric W. Biederman 
33547bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3355c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
335661a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3357801199ceSEric W. Biederman 			goto out;
33581da177e4SLinus Torvalds 	}
33591da177e4SLinus Torvalds 
3360b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
336119fd4bb2SEric W. Biederman 	iter.task = NULL;
336219fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
336319fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
336419fd4bb2SEric W. Biederman 	     iter.task;
336519fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
33660499680aSVasiliy Kulikov 		if (has_pid_permissions(ns, iter.task, 2))
33670499680aSVasiliy Kulikov 			__filldir = filldir;
33680499680aSVasiliy Kulikov 		else
33690499680aSVasiliy Kulikov 			__filldir = fake_filldir;
33700499680aSVasiliy Kulikov 
337119fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
33720499680aSVasiliy Kulikov 		if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
337319fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
33740804ef4bSEric W. Biederman 			goto out;
33751da177e4SLinus Torvalds 		}
33761da177e4SLinus Torvalds 	}
33770804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
33780804ef4bSEric W. Biederman out:
337961a28784SEric W. Biederman 	put_task_struct(reaper);
338061a28784SEric W. Biederman out_no_task:
33811da177e4SLinus Torvalds 	return 0;
33821da177e4SLinus Torvalds }
33831da177e4SLinus Torvalds 
33840bc58a91SEric W. Biederman /*
338528a6d671SEric W. Biederman  * Tasks
338628a6d671SEric W. Biederman  */
3387c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3388631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
33893835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
33906b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3391631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3392631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3393631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3394a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
33953036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
339643ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3397631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
339843ae34cbSIngo Molnar #endif
33994614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3400ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3401a9712bc1SAl Viro 	INF("syscall",   S_IRUGO, proc_pid_syscall),
3402ebcb6734SRoland McGrath #endif
3403631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3404631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3405631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3406b7643757SSiddhesh Poyarekar 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
340781841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
340881841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
340981841161SCyrill Gorcunov #endif
341028a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3411b7643757SSiddhesh Poyarekar 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
341228a6d671SEric W. Biederman #endif
3413631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3414631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3415631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3416631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3417631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3418631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
34191e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3420631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3421b7643757SSiddhesh Poyarekar 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
3422ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
342328a6d671SEric W. Biederman #endif
342428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3425631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
342628a6d671SEric W. Biederman #endif
342728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3428631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
342928a6d671SEric W. Biederman #endif
34302ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3431a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
343228a6d671SEric W. Biederman #endif
343328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3434631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
343528a6d671SEric W. Biederman #endif
34369745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3437631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
34389745512cSArjan van de Ven #endif
34398793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3440631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
344128a6d671SEric W. Biederman #endif
3442a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3443631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3444a424316cSPaul Menage #endif
3445631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3446631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3447a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
344828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3449631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
345026ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
345128a6d671SEric W. Biederman #endif
3452f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3453631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3454f4f154fdSAkinobu Mita #endif
3455297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
34561d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
3457297c5d92SAndrea Righi #endif
3458f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3459f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3460f133eccaSChris Metcalf #endif
346122d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
346222d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
346322d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
346422d917d8SEric W. Biederman #endif
346528a6d671SEric W. Biederman };
346628a6d671SEric W. Biederman 
346728a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
346828a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
346928a6d671SEric W. Biederman {
347028a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
347128a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
347228a6d671SEric W. Biederman }
347328a6d671SEric W. Biederman 
3474*00cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3475*00cd8dd3SAl Viro {
34767bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
34777bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
347828a6d671SEric W. Biederman }
347928a6d671SEric W. Biederman 
348000977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
348128a6d671SEric W. Biederman 	.read		= generic_read_dir,
348228a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
34836038f373SArnd Bergmann 	.llseek		= default_llseek,
348428a6d671SEric W. Biederman };
348528a6d671SEric W. Biederman 
3486c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
348728a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
348828a6d671SEric W. Biederman 	.getattr	= pid_getattr,
348928a6d671SEric W. Biederman 	.setattr	= proc_setattr,
349028a6d671SEric W. Biederman };
349128a6d671SEric W. Biederman 
3492444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3493c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3494444ceed8SEric W. Biederman {
3495444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3496444ceed8SEric W. Biederman 	struct inode *inode;
349761a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3498444ceed8SEric W. Biederman 
3499444ceed8SEric W. Biederman 	if (!inode)
3500444ceed8SEric W. Biederman 		goto out;
3501444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3502444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3503444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3504444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3505aed54175SVegard Nossum 
3506bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3507bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3508444ceed8SEric W. Biederman 
3509fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3510444ceed8SEric W. Biederman 
3511444ceed8SEric W. Biederman 	d_add(dentry, inode);
3512444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
35130b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3514444ceed8SEric W. Biederman 		error = NULL;
3515444ceed8SEric W. Biederman out:
3516444ceed8SEric W. Biederman 	return error;
3517444ceed8SEric W. Biederman }
3518444ceed8SEric W. Biederman 
3519*00cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
352028a6d671SEric W. Biederman {
352128a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
352228a6d671SEric W. Biederman 	struct task_struct *task;
352328a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
352428a6d671SEric W. Biederman 	unsigned tid;
3525b488893aSPavel Emelyanov 	struct pid_namespace *ns;
352628a6d671SEric W. Biederman 
352728a6d671SEric W. Biederman 	if (!leader)
352828a6d671SEric W. Biederman 		goto out_no_task;
352928a6d671SEric W. Biederman 
353028a6d671SEric W. Biederman 	tid = name_to_int(dentry);
353128a6d671SEric W. Biederman 	if (tid == ~0U)
353228a6d671SEric W. Biederman 		goto out;
353328a6d671SEric W. Biederman 
3534b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
353528a6d671SEric W. Biederman 	rcu_read_lock();
3536b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
353728a6d671SEric W. Biederman 	if (task)
353828a6d671SEric W. Biederman 		get_task_struct(task);
353928a6d671SEric W. Biederman 	rcu_read_unlock();
354028a6d671SEric W. Biederman 	if (!task)
354128a6d671SEric W. Biederman 		goto out;
3542bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
354328a6d671SEric W. Biederman 		goto out_drop_task;
354428a6d671SEric W. Biederman 
3545444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
354628a6d671SEric W. Biederman out_drop_task:
354728a6d671SEric W. Biederman 	put_task_struct(task);
354828a6d671SEric W. Biederman out:
354928a6d671SEric W. Biederman 	put_task_struct(leader);
355028a6d671SEric W. Biederman out_no_task:
355128a6d671SEric W. Biederman 	return result;
355228a6d671SEric W. Biederman }
355328a6d671SEric W. Biederman 
355428a6d671SEric W. Biederman /*
35550bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
35560bc58a91SEric W. Biederman  *
35570bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
35580bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
35590bc58a91SEric W. Biederman  * directory we have more work todo.
35600bc58a91SEric W. Biederman  *
35610bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
35620bc58a91SEric W. Biederman  *
35630bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
35640bc58a91SEric W. Biederman  * threads past it.
35650bc58a91SEric W. Biederman  */
3566cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3567b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
35680bc58a91SEric W. Biederman {
3569a872ff0cSOleg Nesterov 	struct task_struct *pos;
35700bc58a91SEric W. Biederman 
3571cc288738SEric W. Biederman 	rcu_read_lock();
35720bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
35730bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3574b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3575a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3576a872ff0cSOleg Nesterov 			goto found;
35770bc58a91SEric W. Biederman 	}
35780bc58a91SEric W. Biederman 
35790bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
35800bc58a91SEric W. Biederman 	pos = NULL;
3581a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3582a872ff0cSOleg Nesterov 		goto out;
3583a872ff0cSOleg Nesterov 
3584a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3585a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3586a872ff0cSOleg Nesterov 	 */
3587a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3588a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3589a872ff0cSOleg Nesterov 		if (pos == leader) {
3590a872ff0cSOleg Nesterov 			pos = NULL;
3591a872ff0cSOleg Nesterov 			goto out;
3592a872ff0cSOleg Nesterov 		}
3593a872ff0cSOleg Nesterov 	}
3594a872ff0cSOleg Nesterov found:
3595a872ff0cSOleg Nesterov 	get_task_struct(pos);
3596a872ff0cSOleg Nesterov out:
3597cc288738SEric W. Biederman 	rcu_read_unlock();
35980bc58a91SEric W. Biederman 	return pos;
35990bc58a91SEric W. Biederman }
36000bc58a91SEric W. Biederman 
36010bc58a91SEric W. Biederman /*
36020bc58a91SEric W. Biederman  * Find the next thread in the thread list.
36030bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
36040bc58a91SEric W. Biederman  *
36050bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
36060bc58a91SEric W. Biederman  */
36070bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
36080bc58a91SEric W. Biederman {
3609c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3610cc288738SEric W. Biederman 	rcu_read_lock();
3611c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
36120bc58a91SEric W. Biederman 		pos = next_thread(start);
3613c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
36140bc58a91SEric W. Biederman 			pos = NULL;
3615c1df7fb8SOleg Nesterov 		else
3616c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3617c1df7fb8SOleg Nesterov 	}
3618cc288738SEric W. Biederman 	rcu_read_unlock();
36190bc58a91SEric W. Biederman 	put_task_struct(start);
36200bc58a91SEric W. Biederman 	return pos;
36210bc58a91SEric W. Biederman }
36220bc58a91SEric W. Biederman 
362361a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
362461a28784SEric W. Biederman 	struct task_struct *task, int tid)
362561a28784SEric W. Biederman {
362661a28784SEric W. Biederman 	char name[PROC_NUMBUF];
362761a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
362861a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
362961a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
363061a28784SEric W. Biederman }
363161a28784SEric W. Biederman 
36321da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
36331da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
36341da177e4SLinus Torvalds {
36352fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
36361da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
36377d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
36380bc58a91SEric W. Biederman 	struct task_struct *task;
36391da177e4SLinus Torvalds 	int retval = -ENOENT;
36401da177e4SLinus Torvalds 	ino_t ino;
36410bc58a91SEric W. Biederman 	int tid;
3642b488893aSPavel Emelyanov 	struct pid_namespace *ns;
36431da177e4SLinus Torvalds 
36447d895244SGuillaume Chazarain 	task = get_proc_task(inode);
36457d895244SGuillaume Chazarain 	if (!task)
36467d895244SGuillaume Chazarain 		goto out_no_task;
36477d895244SGuillaume Chazarain 	rcu_read_lock();
36487d895244SGuillaume Chazarain 	if (pid_alive(task)) {
36497d895244SGuillaume Chazarain 		leader = task->group_leader;
36507d895244SGuillaume Chazarain 		get_task_struct(leader);
36517d895244SGuillaume Chazarain 	}
36527d895244SGuillaume Chazarain 	rcu_read_unlock();
36537d895244SGuillaume Chazarain 	put_task_struct(task);
365499f89551SEric W. Biederman 	if (!leader)
365599f89551SEric W. Biederman 		goto out_no_task;
36561da177e4SLinus Torvalds 	retval = 0;
36571da177e4SLinus Torvalds 
3658ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
36591da177e4SLinus Torvalds 	case 0:
36601da177e4SLinus Torvalds 		ino = inode->i_ino;
3661ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
36621da177e4SLinus Torvalds 			goto out;
3663ee6f779bSZhang Le 		filp->f_pos++;
36641da177e4SLinus Torvalds 		/* fall through */
36651da177e4SLinus Torvalds 	case 1:
36661da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3667ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
36681da177e4SLinus Torvalds 			goto out;
3669ee6f779bSZhang Le 		filp->f_pos++;
36701da177e4SLinus Torvalds 		/* fall through */
36711da177e4SLinus Torvalds 	}
36721da177e4SLinus Torvalds 
36730bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
36740bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
36750bc58a91SEric W. Biederman 	 */
3676b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
36772b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
36780bc58a91SEric W. Biederman 	filp->f_version = 0;
3679ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
36800bc58a91SEric W. Biederman 	     task;
3681ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3682b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
368361a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
36840bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
36850bc58a91SEric W. Biederman 			 * pid for the next readir call */
36862b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
36870bc58a91SEric W. Biederman 			put_task_struct(task);
36881da177e4SLinus Torvalds 			break;
36890bc58a91SEric W. Biederman 		}
36901da177e4SLinus Torvalds 	}
36911da177e4SLinus Torvalds out:
369299f89551SEric W. Biederman 	put_task_struct(leader);
369399f89551SEric W. Biederman out_no_task:
36941da177e4SLinus Torvalds 	return retval;
36951da177e4SLinus Torvalds }
36966e66b52bSEric W. Biederman 
36976e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
36986e66b52bSEric W. Biederman {
36996e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
370099f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
37016e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
37026e66b52bSEric W. Biederman 
370399f89551SEric W. Biederman 	if (p) {
370499f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
370599f89551SEric W. Biederman 		put_task_struct(p);
37066e66b52bSEric W. Biederman 	}
37076e66b52bSEric W. Biederman 
37086e66b52bSEric W. Biederman 	return 0;
37096e66b52bSEric W. Biederman }
371028a6d671SEric W. Biederman 
3711c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
371228a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
371328a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
371428a6d671SEric W. Biederman 	.setattr	= proc_setattr,
37150499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
371628a6d671SEric W. Biederman };
371728a6d671SEric W. Biederman 
371800977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
371928a6d671SEric W. Biederman 	.read		= generic_read_dir,
372028a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
37216038f373SArnd Bergmann 	.llseek		= default_llseek,
372228a6d671SEric W. Biederman };
3723