xref: /openbmc/linux/fs/proc/base.c (revision e8905ec27e2f4ea1b9f7e03df68a060b3ae6fca8)
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) {
830*e8905ec2SDjalal Harouni 		size_t this_len, max_len;
831*e8905ec2SDjalal Harouni 		int retval;
832*e8905ec2SDjalal Harouni 
833*e8905ec2SDjalal Harouni 		if (src >= (mm->env_end - mm->env_start))
834*e8905ec2SDjalal Harouni 			break;
835315e28c8SJames Pearson 
836315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
837315e28c8SJames Pearson 
838*e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
839*e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
840315e28c8SJames Pearson 
841b409e578SCong Wang 		retval = access_remote_vm(mm, (mm->env_start + src),
842315e28c8SJames Pearson 			page, this_len, 0);
843315e28c8SJames Pearson 
844315e28c8SJames Pearson 		if (retval <= 0) {
845315e28c8SJames Pearson 			ret = retval;
846315e28c8SJames Pearson 			break;
847315e28c8SJames Pearson 		}
848315e28c8SJames Pearson 
849315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
850315e28c8SJames Pearson 			ret = -EFAULT;
851315e28c8SJames Pearson 			break;
852315e28c8SJames Pearson 		}
853315e28c8SJames Pearson 
854315e28c8SJames Pearson 		ret += retval;
855315e28c8SJames Pearson 		src += retval;
856315e28c8SJames Pearson 		buf += retval;
857315e28c8SJames Pearson 		count -= retval;
858315e28c8SJames Pearson 	}
859315e28c8SJames Pearson 	*ppos = src;
860315e28c8SJames Pearson 	mmput(mm);
861b409e578SCong Wang 
862b409e578SCong Wang free:
863315e28c8SJames Pearson 	free_page((unsigned long) page);
864315e28c8SJames Pearson 	return ret;
865315e28c8SJames Pearson }
866315e28c8SJames Pearson 
867315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
868b409e578SCong Wang 	.open		= environ_open,
869315e28c8SJames Pearson 	.read		= environ_read,
87087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
871b409e578SCong Wang 	.release	= mem_release,
872315e28c8SJames Pearson };
873315e28c8SJames Pearson 
8741da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
8751da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8761da177e4SLinus Torvalds {
8772fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8788578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
8791da177e4SLinus Torvalds 	size_t len;
88028b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
88128b83c51SKOSAKI Motohiro 	unsigned long flags;
8821da177e4SLinus Torvalds 
88399f89551SEric W. Biederman 	if (!task)
88499f89551SEric W. Biederman 		return -ESRCH;
88528b83c51SKOSAKI Motohiro 
88628b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
88728b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
88828b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
88928b83c51SKOSAKI Motohiro 	}
89028b83c51SKOSAKI Motohiro 
89199f89551SEric W. Biederman 	put_task_struct(task);
89299f89551SEric W. Biederman 
8938578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
8940c28f287SAkinobu Mita 
8950c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
8991da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9001da177e4SLinus Torvalds {
90199f89551SEric W. Biederman 	struct task_struct *task;
9025d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
9030a8cb8e3SAlexey Dobriyan 	int oom_adjust;
90428b83c51SKOSAKI Motohiro 	unsigned long flags;
9055d863b89SKOSAKI Motohiro 	int err;
9061da177e4SLinus Torvalds 
9078578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
9088578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
9098578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
910723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
911723548bfSDavid Rientjes 		err = -EFAULT;
912723548bfSDavid Rientjes 		goto out;
913723548bfSDavid Rientjes 	}
9145d863b89SKOSAKI Motohiro 
9150a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
9165d863b89SKOSAKI Motohiro 	if (err)
917723548bfSDavid Rientjes 		goto out;
9188ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
919723548bfSDavid Rientjes 	     oom_adjust != OOM_DISABLE) {
920723548bfSDavid Rientjes 		err = -EINVAL;
921723548bfSDavid Rientjes 		goto out;
922723548bfSDavid Rientjes 	}
9235d863b89SKOSAKI Motohiro 
9242fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
925723548bfSDavid Rientjes 	if (!task) {
926723548bfSDavid Rientjes 		err = -ESRCH;
927723548bfSDavid Rientjes 		goto out;
928723548bfSDavid Rientjes 	}
92928b83c51SKOSAKI Motohiro 
9303d5992d2SYing Han 	task_lock(task);
9313d5992d2SYing Han 	if (!task->mm) {
932723548bfSDavid Rientjes 		err = -EINVAL;
933723548bfSDavid Rientjes 		goto err_task_lock;
9343d5992d2SYing Han 	}
9353d5992d2SYing Han 
936d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
937d19d5476SDavid Rientjes 		err = -ESRCH;
938d19d5476SDavid Rientjes 		goto err_task_lock;
939d19d5476SDavid Rientjes 	}
940d19d5476SDavid Rientjes 
941d19d5476SDavid Rientjes 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
942d19d5476SDavid Rientjes 		err = -EACCES;
943d19d5476SDavid Rientjes 		goto err_sighand;
944d19d5476SDavid Rientjes 	}
945d19d5476SDavid Rientjes 
94651b1bd2aSDavid Rientjes 	/*
94751b1bd2aSDavid Rientjes 	 * Warn that /proc/pid/oom_adj is deprecated, see
94851b1bd2aSDavid Rientjes 	 * Documentation/feature-removal-schedule.txt.
94951b1bd2aSDavid Rientjes 	 */
950c2142704SLinus Torvalds 	printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
951be8f684dSDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
952be8f684dSDavid Rientjes 		  task_pid_nr(task));
95328b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
954a63d83f4SDavid Rientjes 	/*
955a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
956a63d83f4SDavid Rientjes 	 * value is always attainable.
957a63d83f4SDavid Rientjes 	 */
958a63d83f4SDavid Rientjes 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
959a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
960a63d83f4SDavid Rientjes 	else
961a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
962a63d83f4SDavid Rientjes 								-OOM_DISABLE;
96343d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
964723548bfSDavid Rientjes err_sighand:
96528b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
966d19d5476SDavid Rientjes err_task_lock:
967d19d5476SDavid Rientjes 	task_unlock(task);
96899f89551SEric W. Biederman 	put_task_struct(task);
969723548bfSDavid Rientjes out:
970723548bfSDavid Rientjes 	return err < 0 ? err : count;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
97300977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
9741da177e4SLinus Torvalds 	.read		= oom_adjust_read,
9751da177e4SLinus Torvalds 	.write		= oom_adjust_write,
97687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
9771da177e4SLinus Torvalds };
9781da177e4SLinus Torvalds 
979a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
980a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
981a63d83f4SDavid Rientjes {
982a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
983a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
984a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
985a63d83f4SDavid Rientjes 	unsigned long flags;
986a63d83f4SDavid Rientjes 	size_t len;
987a63d83f4SDavid Rientjes 
988a63d83f4SDavid Rientjes 	if (!task)
989a63d83f4SDavid Rientjes 		return -ESRCH;
990a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
991a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
992a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
993a63d83f4SDavid Rientjes 	}
994a63d83f4SDavid Rientjes 	put_task_struct(task);
995a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
996a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
997a63d83f4SDavid Rientjes }
998a63d83f4SDavid Rientjes 
999a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1000a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1001a63d83f4SDavid Rientjes {
1002a63d83f4SDavid Rientjes 	struct task_struct *task;
1003a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1004a63d83f4SDavid Rientjes 	unsigned long flags;
10050a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1006a63d83f4SDavid Rientjes 	int err;
1007a63d83f4SDavid Rientjes 
1008a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1009a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1010a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1011723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1012723548bfSDavid Rientjes 		err = -EFAULT;
1013723548bfSDavid Rientjes 		goto out;
1014723548bfSDavid Rientjes 	}
1015a63d83f4SDavid Rientjes 
10160a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1017a63d83f4SDavid Rientjes 	if (err)
1018723548bfSDavid Rientjes 		goto out;
1019a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1020723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1021723548bfSDavid Rientjes 		err = -EINVAL;
1022723548bfSDavid Rientjes 		goto out;
1023723548bfSDavid Rientjes 	}
1024a63d83f4SDavid Rientjes 
1025a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1026723548bfSDavid Rientjes 	if (!task) {
1027723548bfSDavid Rientjes 		err = -ESRCH;
1028723548bfSDavid Rientjes 		goto out;
1029723548bfSDavid Rientjes 	}
1030a63d83f4SDavid Rientjes 
10313d5992d2SYing Han 	task_lock(task);
10323d5992d2SYing Han 	if (!task->mm) {
1033723548bfSDavid Rientjes 		err = -EINVAL;
1034723548bfSDavid Rientjes 		goto err_task_lock;
10353d5992d2SYing Han 	}
1036d19d5476SDavid Rientjes 
1037d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1038d19d5476SDavid Rientjes 		err = -ESRCH;
1039d19d5476SDavid Rientjes 		goto err_task_lock;
1040d19d5476SDavid Rientjes 	}
1041d19d5476SDavid Rientjes 
1042dabb16f6SMandeep Singh Baines 	if (oom_score_adj < task->signal->oom_score_adj_min &&
1043d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1044d19d5476SDavid Rientjes 		err = -EACCES;
1045d19d5476SDavid Rientjes 		goto err_sighand;
1046d19d5476SDavid Rientjes 	}
1047d19d5476SDavid Rientjes 
1048a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1049dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1050dabb16f6SMandeep Singh Baines 		task->signal->oom_score_adj_min = oom_score_adj;
105143d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
1052a63d83f4SDavid Rientjes 	/*
1053a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1054a63d83f4SDavid Rientjes 	 * always attainable.
1055a63d83f4SDavid Rientjes 	 */
1056a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1057a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1058a63d83f4SDavid Rientjes 	else
1059a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1060a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1061723548bfSDavid Rientjes err_sighand:
1062a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1063d19d5476SDavid Rientjes err_task_lock:
1064d19d5476SDavid Rientjes 	task_unlock(task);
1065a63d83f4SDavid Rientjes 	put_task_struct(task);
1066723548bfSDavid Rientjes out:
1067723548bfSDavid Rientjes 	return err < 0 ? err : count;
1068a63d83f4SDavid Rientjes }
1069a63d83f4SDavid Rientjes 
1070a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1071a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1072a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10736038f373SArnd Bergmann 	.llseek		= default_llseek,
1074a63d83f4SDavid Rientjes };
1075a63d83f4SDavid Rientjes 
10761da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10771da177e4SLinus Torvalds #define TMPBUFLEN 21
10781da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10791da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10801da177e4SLinus Torvalds {
10812fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
108299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10831da177e4SLinus Torvalds 	ssize_t length;
10841da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10851da177e4SLinus Torvalds 
108699f89551SEric W. Biederman 	if (!task)
108799f89551SEric W. Biederman 		return -ESRCH;
10881da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
10890c11b942SAl Viro 				audit_get_loginuid(task));
109099f89551SEric W. Biederman 	put_task_struct(task);
10911da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10951da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10961da177e4SLinus Torvalds {
10972fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
10981da177e4SLinus Torvalds 	char *page, *tmp;
10991da177e4SLinus Torvalds 	ssize_t length;
11001da177e4SLinus Torvalds 	uid_t loginuid;
11011da177e4SLinus Torvalds 
11027dc52157SPaul E. McKenney 	rcu_read_lock();
11037dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11047dc52157SPaul E. McKenney 		rcu_read_unlock();
11051da177e4SLinus Torvalds 		return -EPERM;
11067dc52157SPaul E. McKenney 	}
11077dc52157SPaul E. McKenney 	rcu_read_unlock();
11081da177e4SLinus Torvalds 
1109e0182909SAl Viro 	if (count >= PAGE_SIZE)
1110e0182909SAl Viro 		count = PAGE_SIZE - 1;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	if (*ppos != 0) {
11131da177e4SLinus Torvalds 		/* No partial writes. */
11141da177e4SLinus Torvalds 		return -EINVAL;
11151da177e4SLinus Torvalds 	}
1116e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11171da177e4SLinus Torvalds 	if (!page)
11181da177e4SLinus Torvalds 		return -ENOMEM;
11191da177e4SLinus Torvalds 	length = -EFAULT;
11201da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11211da177e4SLinus Torvalds 		goto out_free_page;
11221da177e4SLinus Torvalds 
1123e0182909SAl Viro 	page[count] = '\0';
11241da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11251da177e4SLinus Torvalds 	if (tmp == page) {
11261da177e4SLinus Torvalds 		length = -EINVAL;
11271da177e4SLinus Torvalds 		goto out_free_page;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	}
11300a300be6SEric Paris 	length = audit_set_loginuid(loginuid);
11311da177e4SLinus Torvalds 	if (likely(length == 0))
11321da177e4SLinus Torvalds 		length = count;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds out_free_page:
11351da177e4SLinus Torvalds 	free_page((unsigned long) page);
11361da177e4SLinus Torvalds 	return length;
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
113900977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11401da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11411da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
114287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11431da177e4SLinus Torvalds };
11441e0bd755SEric Paris 
11451e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11461e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11471e0bd755SEric Paris {
11481e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
11491e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11501e0bd755SEric Paris 	ssize_t length;
11511e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11521e0bd755SEric Paris 
11531e0bd755SEric Paris 	if (!task)
11541e0bd755SEric Paris 		return -ESRCH;
11551e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11561e0bd755SEric Paris 				audit_get_sessionid(task));
11571e0bd755SEric Paris 	put_task_struct(task);
11581e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11591e0bd755SEric Paris }
11601e0bd755SEric Paris 
11611e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11621e0bd755SEric Paris 	.read		= proc_sessionid_read,
116387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11641e0bd755SEric Paris };
11651da177e4SLinus Torvalds #endif
11661da177e4SLinus Torvalds 
1167f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1168f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1169f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1170f4f154fdSAkinobu Mita {
1171f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1172f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1173f4f154fdSAkinobu Mita 	size_t len;
1174f4f154fdSAkinobu Mita 	int make_it_fail;
1175f4f154fdSAkinobu Mita 
1176f4f154fdSAkinobu Mita 	if (!task)
1177f4f154fdSAkinobu Mita 		return -ESRCH;
1178f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1179f4f154fdSAkinobu Mita 	put_task_struct(task);
1180f4f154fdSAkinobu Mita 
1181f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11820c28f287SAkinobu Mita 
11830c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1184f4f154fdSAkinobu Mita }
1185f4f154fdSAkinobu Mita 
1186f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1187f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1188f4f154fdSAkinobu Mita {
1189f4f154fdSAkinobu Mita 	struct task_struct *task;
1190f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1191f4f154fdSAkinobu Mita 	int make_it_fail;
1192f4f154fdSAkinobu Mita 
1193f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1194f4f154fdSAkinobu Mita 		return -EPERM;
1195f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1196f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1197f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1198f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1199f4f154fdSAkinobu Mita 		return -EFAULT;
1200cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1201cba8aafeSVincent Li 	if (*end)
1202cba8aafeSVincent Li 		return -EINVAL;
1203f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1204f4f154fdSAkinobu Mita 	if (!task)
1205f4f154fdSAkinobu Mita 		return -ESRCH;
1206f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1207f4f154fdSAkinobu Mita 	put_task_struct(task);
1208cba8aafeSVincent Li 
1209cba8aafeSVincent Li 	return count;
1210f4f154fdSAkinobu Mita }
1211f4f154fdSAkinobu Mita 
121200977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1213f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1214f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
121587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1216f4f154fdSAkinobu Mita };
1217f4f154fdSAkinobu Mita #endif
1218f4f154fdSAkinobu Mita 
12199745512cSArjan van de Ven 
122043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
122143ae34cbSIngo Molnar /*
122243ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
122343ae34cbSIngo Molnar  */
122443ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
122543ae34cbSIngo Molnar {
122643ae34cbSIngo Molnar 	struct inode *inode = m->private;
122743ae34cbSIngo Molnar 	struct task_struct *p;
122843ae34cbSIngo Molnar 
122943ae34cbSIngo Molnar 	p = get_proc_task(inode);
123043ae34cbSIngo Molnar 	if (!p)
123143ae34cbSIngo Molnar 		return -ESRCH;
123243ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
123343ae34cbSIngo Molnar 
123443ae34cbSIngo Molnar 	put_task_struct(p);
123543ae34cbSIngo Molnar 
123643ae34cbSIngo Molnar 	return 0;
123743ae34cbSIngo Molnar }
123843ae34cbSIngo Molnar 
123943ae34cbSIngo Molnar static ssize_t
124043ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
124143ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
124243ae34cbSIngo Molnar {
124343ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
124443ae34cbSIngo Molnar 	struct task_struct *p;
124543ae34cbSIngo Molnar 
124643ae34cbSIngo Molnar 	p = get_proc_task(inode);
124743ae34cbSIngo Molnar 	if (!p)
124843ae34cbSIngo Molnar 		return -ESRCH;
124943ae34cbSIngo Molnar 	proc_sched_set_task(p);
125043ae34cbSIngo Molnar 
125143ae34cbSIngo Molnar 	put_task_struct(p);
125243ae34cbSIngo Molnar 
125343ae34cbSIngo Molnar 	return count;
125443ae34cbSIngo Molnar }
125543ae34cbSIngo Molnar 
125643ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
125743ae34cbSIngo Molnar {
1258c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
125943ae34cbSIngo Molnar }
126043ae34cbSIngo Molnar 
126143ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
126243ae34cbSIngo Molnar 	.open		= sched_open,
126343ae34cbSIngo Molnar 	.read		= seq_read,
126443ae34cbSIngo Molnar 	.write		= sched_write,
126543ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12665ea473a1SAlexey Dobriyan 	.release	= single_release,
126743ae34cbSIngo Molnar };
126843ae34cbSIngo Molnar 
126943ae34cbSIngo Molnar #endif
127043ae34cbSIngo Molnar 
12715091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
12725091faa4SMike Galbraith /*
12735091faa4SMike Galbraith  * Print out autogroup related information:
12745091faa4SMike Galbraith  */
12755091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
12765091faa4SMike Galbraith {
12775091faa4SMike Galbraith 	struct inode *inode = m->private;
12785091faa4SMike Galbraith 	struct task_struct *p;
12795091faa4SMike Galbraith 
12805091faa4SMike Galbraith 	p = get_proc_task(inode);
12815091faa4SMike Galbraith 	if (!p)
12825091faa4SMike Galbraith 		return -ESRCH;
12835091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
12845091faa4SMike Galbraith 
12855091faa4SMike Galbraith 	put_task_struct(p);
12865091faa4SMike Galbraith 
12875091faa4SMike Galbraith 	return 0;
12885091faa4SMike Galbraith }
12895091faa4SMike Galbraith 
12905091faa4SMike Galbraith static ssize_t
12915091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
12925091faa4SMike Galbraith 	    size_t count, loff_t *offset)
12935091faa4SMike Galbraith {
12945091faa4SMike Galbraith 	struct inode *inode = file->f_path.dentry->d_inode;
12955091faa4SMike Galbraith 	struct task_struct *p;
12965091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
12970a8cb8e3SAlexey Dobriyan 	int nice;
12985091faa4SMike Galbraith 	int err;
12995091faa4SMike Galbraith 
13005091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
13015091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
13025091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
13035091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
13045091faa4SMike Galbraith 		return -EFAULT;
13055091faa4SMike Galbraith 
13060a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
13070a8cb8e3SAlexey Dobriyan 	if (err < 0)
13080a8cb8e3SAlexey Dobriyan 		return err;
13095091faa4SMike Galbraith 
13105091faa4SMike Galbraith 	p = get_proc_task(inode);
13115091faa4SMike Galbraith 	if (!p)
13125091faa4SMike Galbraith 		return -ESRCH;
13135091faa4SMike Galbraith 
13142e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
13155091faa4SMike Galbraith 	if (err)
13165091faa4SMike Galbraith 		count = err;
13175091faa4SMike Galbraith 
13185091faa4SMike Galbraith 	put_task_struct(p);
13195091faa4SMike Galbraith 
13205091faa4SMike Galbraith 	return count;
13215091faa4SMike Galbraith }
13225091faa4SMike Galbraith 
13235091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
13245091faa4SMike Galbraith {
13255091faa4SMike Galbraith 	int ret;
13265091faa4SMike Galbraith 
13275091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13285091faa4SMike Galbraith 	if (!ret) {
13295091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13305091faa4SMike Galbraith 
13315091faa4SMike Galbraith 		m->private = inode;
13325091faa4SMike Galbraith 	}
13335091faa4SMike Galbraith 	return ret;
13345091faa4SMike Galbraith }
13355091faa4SMike Galbraith 
13365091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13375091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13385091faa4SMike Galbraith 	.read		= seq_read,
13395091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13405091faa4SMike Galbraith 	.llseek		= seq_lseek,
13415091faa4SMike Galbraith 	.release	= single_release,
13425091faa4SMike Galbraith };
13435091faa4SMike Galbraith 
13445091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
13455091faa4SMike Galbraith 
13464614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13474614a696Sjohn stultz 				size_t count, loff_t *offset)
13484614a696Sjohn stultz {
13494614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
13504614a696Sjohn stultz 	struct task_struct *p;
13514614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
13524614a696Sjohn stultz 
13534614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
13544614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
13554614a696Sjohn stultz 		count = sizeof(buffer) - 1;
13564614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
13574614a696Sjohn stultz 		return -EFAULT;
13584614a696Sjohn stultz 
13594614a696Sjohn stultz 	p = get_proc_task(inode);
13604614a696Sjohn stultz 	if (!p)
13614614a696Sjohn stultz 		return -ESRCH;
13624614a696Sjohn stultz 
13634614a696Sjohn stultz 	if (same_thread_group(current, p))
13644614a696Sjohn stultz 		set_task_comm(p, buffer);
13654614a696Sjohn stultz 	else
13664614a696Sjohn stultz 		count = -EINVAL;
13674614a696Sjohn stultz 
13684614a696Sjohn stultz 	put_task_struct(p);
13694614a696Sjohn stultz 
13704614a696Sjohn stultz 	return count;
13714614a696Sjohn stultz }
13724614a696Sjohn stultz 
13734614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13744614a696Sjohn stultz {
13754614a696Sjohn stultz 	struct inode *inode = m->private;
13764614a696Sjohn stultz 	struct task_struct *p;
13774614a696Sjohn stultz 
13784614a696Sjohn stultz 	p = get_proc_task(inode);
13794614a696Sjohn stultz 	if (!p)
13804614a696Sjohn stultz 		return -ESRCH;
13814614a696Sjohn stultz 
13824614a696Sjohn stultz 	task_lock(p);
13834614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13844614a696Sjohn stultz 	task_unlock(p);
13854614a696Sjohn stultz 
13864614a696Sjohn stultz 	put_task_struct(p);
13874614a696Sjohn stultz 
13884614a696Sjohn stultz 	return 0;
13894614a696Sjohn stultz }
13904614a696Sjohn stultz 
13914614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
13924614a696Sjohn stultz {
1393c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
13944614a696Sjohn stultz }
13954614a696Sjohn stultz 
13964614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
13974614a696Sjohn stultz 	.open		= comm_open,
13984614a696Sjohn stultz 	.read		= seq_read,
13994614a696Sjohn stultz 	.write		= comm_write,
14004614a696Sjohn stultz 	.llseek		= seq_lseek,
14014614a696Sjohn stultz 	.release	= single_release,
14024614a696Sjohn stultz };
14034614a696Sjohn stultz 
14047773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1405925d1c40SMatt Helsley {
1406925d1c40SMatt Helsley 	struct task_struct *task;
1407925d1c40SMatt Helsley 	struct mm_struct *mm;
1408925d1c40SMatt Helsley 	struct file *exe_file;
1409925d1c40SMatt Helsley 
14107773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1411925d1c40SMatt Helsley 	if (!task)
1412925d1c40SMatt Helsley 		return -ENOENT;
1413925d1c40SMatt Helsley 	mm = get_task_mm(task);
1414925d1c40SMatt Helsley 	put_task_struct(task);
1415925d1c40SMatt Helsley 	if (!mm)
1416925d1c40SMatt Helsley 		return -ENOENT;
1417925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1418925d1c40SMatt Helsley 	mmput(mm);
1419925d1c40SMatt Helsley 	if (exe_file) {
1420925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1421925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1422925d1c40SMatt Helsley 		fput(exe_file);
1423925d1c40SMatt Helsley 		return 0;
1424925d1c40SMatt Helsley 	} else
1425925d1c40SMatt Helsley 		return -ENOENT;
1426925d1c40SMatt Helsley }
1427925d1c40SMatt Helsley 
1428008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14291da177e4SLinus Torvalds {
14301da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1431408ef013SChristoph Hellwig 	struct path path;
14321da177e4SLinus Torvalds 	int error = -EACCES;
14331da177e4SLinus Torvalds 
1434778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1435778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14361da177e4SLinus Torvalds 		goto out;
14371da177e4SLinus Torvalds 
1438408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1439408ef013SChristoph Hellwig 	if (error)
1440408ef013SChristoph Hellwig 		goto out;
1441408ef013SChristoph Hellwig 
1442b5fb63c1SChristoph Hellwig 	nd_jump_link(nd, &path);
1443408ef013SChristoph Hellwig 	return NULL;
14441da177e4SLinus Torvalds out:
1445008b150aSAl Viro 	return ERR_PTR(error);
14461da177e4SLinus Torvalds }
14471da177e4SLinus Torvalds 
14483dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14491da177e4SLinus Torvalds {
1450e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14513dcd25f3SJan Blunck 	char *pathname;
14521da177e4SLinus Torvalds 	int len;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	if (!tmp)
14551da177e4SLinus Torvalds 		return -ENOMEM;
14561da177e4SLinus Torvalds 
14577b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14583dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14593dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14601da177e4SLinus Torvalds 		goto out;
14613dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	if (len > buflen)
14641da177e4SLinus Torvalds 		len = buflen;
14653dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14661da177e4SLinus Torvalds 		len = -EFAULT;
14671da177e4SLinus Torvalds  out:
14681da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14691da177e4SLinus Torvalds 	return len;
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14731da177e4SLinus Torvalds {
14741da177e4SLinus Torvalds 	int error = -EACCES;
14751da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14763dcd25f3SJan Blunck 	struct path path;
14771da177e4SLinus Torvalds 
1478778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1479778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14801da177e4SLinus Torvalds 		goto out;
14811da177e4SLinus Torvalds 
14827773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
14831da177e4SLinus Torvalds 	if (error)
14841da177e4SLinus Torvalds 		goto out;
14851da177e4SLinus Torvalds 
14863dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14873dcd25f3SJan Blunck 	path_put(&path);
14881da177e4SLinus Torvalds out:
14891da177e4SLinus Torvalds 	return error;
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
1492c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
14931da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
14946d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
14956d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
14961da177e4SLinus Torvalds };
14971da177e4SLinus Torvalds 
149828a6d671SEric W. Biederman 
149928a6d671SEric W. Biederman /* building an inode */
150028a6d671SEric W. Biederman 
150128a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
150228a6d671SEric W. Biederman {
150328a6d671SEric W. Biederman 	int dumpable = 0;
150428a6d671SEric W. Biederman 	struct mm_struct *mm;
150528a6d671SEric W. Biederman 
150628a6d671SEric W. Biederman 	task_lock(task);
150728a6d671SEric W. Biederman 	mm = task->mm;
150828a6d671SEric W. Biederman 	if (mm)
15096c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
151028a6d671SEric W. Biederman 	task_unlock(task);
151128a6d671SEric W. Biederman 	if(dumpable == 1)
151228a6d671SEric W. Biederman 		return 1;
151328a6d671SEric W. Biederman 	return 0;
151428a6d671SEric W. Biederman }
151528a6d671SEric W. Biederman 
15166b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
151728a6d671SEric W. Biederman {
151828a6d671SEric W. Biederman 	struct inode * inode;
151928a6d671SEric W. Biederman 	struct proc_inode *ei;
1520c69e8d9cSDavid Howells 	const struct cred *cred;
152128a6d671SEric W. Biederman 
152228a6d671SEric W. Biederman 	/* We need a new inode */
152328a6d671SEric W. Biederman 
152428a6d671SEric W. Biederman 	inode = new_inode(sb);
152528a6d671SEric W. Biederman 	if (!inode)
152628a6d671SEric W. Biederman 		goto out;
152728a6d671SEric W. Biederman 
152828a6d671SEric W. Biederman 	/* Common stuff */
152928a6d671SEric W. Biederman 	ei = PROC_I(inode);
153085fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
153128a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
153228a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
153328a6d671SEric W. Biederman 
153428a6d671SEric W. Biederman 	/*
153528a6d671SEric W. Biederman 	 * grab the reference to task.
153628a6d671SEric W. Biederman 	 */
15371a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
153828a6d671SEric W. Biederman 	if (!ei->pid)
153928a6d671SEric W. Biederman 		goto out_unlock;
154028a6d671SEric W. Biederman 
154128a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1542c69e8d9cSDavid Howells 		rcu_read_lock();
1543c69e8d9cSDavid Howells 		cred = __task_cred(task);
1544c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1545c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1546c69e8d9cSDavid Howells 		rcu_read_unlock();
154728a6d671SEric W. Biederman 	}
154828a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
154928a6d671SEric W. Biederman 
155028a6d671SEric W. Biederman out:
155128a6d671SEric W. Biederman 	return inode;
155228a6d671SEric W. Biederman 
155328a6d671SEric W. Biederman out_unlock:
155428a6d671SEric W. Biederman 	iput(inode);
155528a6d671SEric W. Biederman 	return NULL;
155628a6d671SEric W. Biederman }
155728a6d671SEric W. Biederman 
15586b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
155928a6d671SEric W. Biederman {
156028a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
156128a6d671SEric W. Biederman 	struct task_struct *task;
1562c69e8d9cSDavid Howells 	const struct cred *cred;
15630499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1564c69e8d9cSDavid Howells 
156528a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
156628a6d671SEric W. Biederman 
156728a6d671SEric W. Biederman 	rcu_read_lock();
1568dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1569dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
157028a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
157128a6d671SEric W. Biederman 	if (task) {
15720499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15730499680aSVasiliy Kulikov 			rcu_read_unlock();
15740499680aSVasiliy Kulikov 			/*
15750499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15760499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15770499680aSVasiliy Kulikov 			 */
15780499680aSVasiliy Kulikov 			return -ENOENT;
15790499680aSVasiliy Kulikov 		}
158028a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
158128a6d671SEric W. Biederman 		    task_dumpable(task)) {
1582c69e8d9cSDavid Howells 			cred = __task_cred(task);
1583c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1584c69e8d9cSDavid Howells 			stat->gid = cred->egid;
158528a6d671SEric W. Biederman 		}
158628a6d671SEric W. Biederman 	}
158728a6d671SEric W. Biederman 	rcu_read_unlock();
158828a6d671SEric W. Biederman 	return 0;
158928a6d671SEric W. Biederman }
159028a6d671SEric W. Biederman 
159128a6d671SEric W. Biederman /* dentry stuff */
159228a6d671SEric W. Biederman 
159328a6d671SEric W. Biederman /*
159428a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
159528a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
159628a6d671SEric W. Biederman  * due to the way we treat inodes.
159728a6d671SEric W. Biederman  *
159828a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
159928a6d671SEric W. Biederman  * performed a setuid(), etc.
160028a6d671SEric W. Biederman  *
160128a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
160228a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
160328a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
160428a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
160528a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
160628a6d671SEric W. Biederman  * directories.
160728a6d671SEric W. Biederman  */
16080b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags)
160928a6d671SEric W. Biederman {
161034286d66SNick Piggin 	struct inode *inode;
161134286d66SNick Piggin 	struct task_struct *task;
1612c69e8d9cSDavid Howells 	const struct cred *cred;
1613c69e8d9cSDavid Howells 
16140b728e19SAl Viro 	if (flags & LOOKUP_RCU)
161534286d66SNick Piggin 		return -ECHILD;
161634286d66SNick Piggin 
161734286d66SNick Piggin 	inode = dentry->d_inode;
161834286d66SNick Piggin 	task = get_proc_task(inode);
161934286d66SNick Piggin 
162028a6d671SEric W. Biederman 	if (task) {
162128a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
162228a6d671SEric W. Biederman 		    task_dumpable(task)) {
1623c69e8d9cSDavid Howells 			rcu_read_lock();
1624c69e8d9cSDavid Howells 			cred = __task_cred(task);
1625c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1626c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1627c69e8d9cSDavid Howells 			rcu_read_unlock();
162828a6d671SEric W. Biederman 		} else {
1629dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1630dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
163128a6d671SEric W. Biederman 		}
163228a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
163328a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
163428a6d671SEric W. Biederman 		put_task_struct(task);
163528a6d671SEric W. Biederman 		return 1;
163628a6d671SEric W. Biederman 	}
163728a6d671SEric W. Biederman 	d_drop(dentry);
163828a6d671SEric W. Biederman 	return 0;
163928a6d671SEric W. Biederman }
164028a6d671SEric W. Biederman 
1641fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry)
164228a6d671SEric W. Biederman {
164328a6d671SEric W. Biederman 	/* Is the task we represent dead?
164428a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
164528a6d671SEric W. Biederman 	 * kill it immediately.
164628a6d671SEric W. Biederman 	 */
164728a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
164828a6d671SEric W. Biederman }
164928a6d671SEric W. Biederman 
16506b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
165128a6d671SEric W. Biederman {
165228a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
165328a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
165428a6d671SEric W. Biederman };
165528a6d671SEric W. Biederman 
165628a6d671SEric W. Biederman /* Lookups */
165728a6d671SEric W. Biederman 
16581c0d04c9SEric W. Biederman /*
16591c0d04c9SEric W. Biederman  * Fill a directory entry.
16601c0d04c9SEric W. Biederman  *
16611c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16621c0d04c9SEric W. Biederman  * file type from dcache entry.
16631c0d04c9SEric W. Biederman  *
16641c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16651c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16661c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16671c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16681c0d04c9SEric W. Biederman  * by stat.
16691c0d04c9SEric W. Biederman  */
16706b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
16716b4e306aSEric W. Biederman 	const char *name, int len,
1672c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
167361a28784SEric W. Biederman {
16742fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
167561a28784SEric W. Biederman 	struct inode *inode;
167661a28784SEric W. Biederman 	struct qstr qname;
167761a28784SEric W. Biederman 	ino_t ino = 0;
167861a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
167961a28784SEric W. Biederman 
168061a28784SEric W. Biederman 	qname.name = name;
168161a28784SEric W. Biederman 	qname.len  = len;
168261a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
168361a28784SEric W. Biederman 
168461a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
168561a28784SEric W. Biederman 	if (!child) {
168661a28784SEric W. Biederman 		struct dentry *new;
168761a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
168861a28784SEric W. Biederman 		if (new) {
168961a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
169061a28784SEric W. Biederman 			if (child)
169161a28784SEric W. Biederman 				dput(new);
169261a28784SEric W. Biederman 			else
169361a28784SEric W. Biederman 				child = new;
169461a28784SEric W. Biederman 		}
169561a28784SEric W. Biederman 	}
169661a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
169761a28784SEric W. Biederman 		goto end_instantiate;
169861a28784SEric W. Biederman 	inode = child->d_inode;
169961a28784SEric W. Biederman 	if (inode) {
170061a28784SEric W. Biederman 		ino = inode->i_ino;
170161a28784SEric W. Biederman 		type = inode->i_mode >> 12;
170261a28784SEric W. Biederman 	}
170361a28784SEric W. Biederman 	dput(child);
170461a28784SEric W. Biederman end_instantiate:
170561a28784SEric W. Biederman 	if (!ino)
170661a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
170761a28784SEric W. Biederman 	if (!ino)
170861a28784SEric W. Biederman 		ino = 1;
170961a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
171061a28784SEric W. Biederman }
171161a28784SEric W. Biederman 
171228a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
171328a6d671SEric W. Biederman {
171428a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
171528a6d671SEric W. Biederman 	int len = dentry->d_name.len;
171628a6d671SEric W. Biederman 	unsigned n = 0;
171728a6d671SEric W. Biederman 
171828a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
171928a6d671SEric W. Biederman 		goto out;
172028a6d671SEric W. Biederman 	while (len-- > 0) {
172128a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
172228a6d671SEric W. Biederman 		if (c > 9)
172328a6d671SEric W. Biederman 			goto out;
172428a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
172528a6d671SEric W. Biederman 			goto out;
172628a6d671SEric W. Biederman 		n *= 10;
172728a6d671SEric W. Biederman 		n += c;
172828a6d671SEric W. Biederman 	}
172928a6d671SEric W. Biederman 	return n;
173028a6d671SEric W. Biederman out:
173128a6d671SEric W. Biederman 	return ~0U;
173228a6d671SEric W. Biederman }
173328a6d671SEric W. Biederman 
173427932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
173527932742SMiklos Szeredi 
17363dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
173728a6d671SEric W. Biederman {
17385e442a49SLinus Torvalds 	struct task_struct *task = get_proc_task(inode);
17395e442a49SLinus Torvalds 	struct files_struct *files = NULL;
174028a6d671SEric W. Biederman 	struct file *file;
174128a6d671SEric W. Biederman 	int fd = proc_fd(inode);
174228a6d671SEric W. Biederman 
17435e442a49SLinus Torvalds 	if (task) {
174428a6d671SEric W. Biederman 		files = get_files_struct(task);
17455e442a49SLinus Torvalds 		put_task_struct(task);
17465e442a49SLinus Torvalds 	}
17475e442a49SLinus Torvalds 	if (files) {
174828a6d671SEric W. Biederman 		/*
174928a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
175028a6d671SEric W. Biederman 		 * hold ->file_lock.
175128a6d671SEric W. Biederman 		 */
175228a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
175328a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
175428a6d671SEric W. Biederman 		if (file) {
17551117f72eSLinus Torvalds 			unsigned int f_flags;
17561117f72eSLinus Torvalds 			struct fdtable *fdt;
17571117f72eSLinus Torvalds 
17581117f72eSLinus Torvalds 			fdt = files_fdtable(files);
17591117f72eSLinus Torvalds 			f_flags = file->f_flags & ~O_CLOEXEC;
17601dce27c5SDavid Howells 			if (close_on_exec(fd, fdt))
17611117f72eSLinus Torvalds 				f_flags |= O_CLOEXEC;
17621117f72eSLinus Torvalds 
17633dcd25f3SJan Blunck 			if (path) {
17643dcd25f3SJan Blunck 				*path = file->f_path;
17653dcd25f3SJan Blunck 				path_get(&file->f_path);
17663dcd25f3SJan Blunck 			}
176727932742SMiklos Szeredi 			if (info)
176827932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
176927932742SMiklos Szeredi 					 "pos:\t%lli\n"
177027932742SMiklos Szeredi 					 "flags:\t0%o\n",
177127932742SMiklos Szeredi 					 (long long) file->f_pos,
17721117f72eSLinus Torvalds 					 f_flags);
177328a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
177428a6d671SEric W. Biederman 			put_files_struct(files);
17755e442a49SLinus Torvalds 			return 0;
17765e442a49SLinus Torvalds 		}
17775e442a49SLinus Torvalds 		spin_unlock(&files->file_lock);
17785e442a49SLinus Torvalds 		put_files_struct(files);
17795e442a49SLinus Torvalds 	}
17805e442a49SLinus Torvalds 	return -ENOENT;
178128a6d671SEric W. Biederman }
178228a6d671SEric W. Biederman 
17837773fbc5SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path)
178427932742SMiklos Szeredi {
17857773fbc5SCyrill Gorcunov 	return proc_fd_info(dentry->d_inode, path, NULL);
178627932742SMiklos Szeredi }
178727932742SMiklos Szeredi 
17880b728e19SAl Viro static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
178928a6d671SEric W. Biederman {
179034286d66SNick Piggin 	struct inode *inode;
179134286d66SNick Piggin 	struct task_struct *task;
179234286d66SNick Piggin 	int fd;
179328a6d671SEric W. Biederman 	struct files_struct *files;
1794c69e8d9cSDavid Howells 	const struct cred *cred;
179528a6d671SEric W. Biederman 
17960b728e19SAl Viro 	if (flags & LOOKUP_RCU)
179734286d66SNick Piggin 		return -ECHILD;
179834286d66SNick Piggin 
179934286d66SNick Piggin 	inode = dentry->d_inode;
180034286d66SNick Piggin 	task = get_proc_task(inode);
180134286d66SNick Piggin 	fd = proc_fd(inode);
180234286d66SNick Piggin 
180328a6d671SEric W. Biederman 	if (task) {
180428a6d671SEric W. Biederman 		files = get_files_struct(task);
180528a6d671SEric W. Biederman 		if (files) {
180630a08bf2SLinus Torvalds 			struct file *file;
180728a6d671SEric W. Biederman 			rcu_read_lock();
180830a08bf2SLinus Torvalds 			file = fcheck_files(files, fd);
180930a08bf2SLinus Torvalds 			if (file) {
18100640113bSLinus Torvalds 				unsigned f_mode = file->f_mode;
181130a08bf2SLinus Torvalds 
181228a6d671SEric W. Biederman 				rcu_read_unlock();
181328a6d671SEric W. Biederman 				put_files_struct(files);
181430a08bf2SLinus Torvalds 
181528a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1816c69e8d9cSDavid Howells 					rcu_read_lock();
1817c69e8d9cSDavid Howells 					cred = __task_cred(task);
1818c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1819c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1820c69e8d9cSDavid Howells 					rcu_read_unlock();
182128a6d671SEric W. Biederman 				} else {
1822dcb0f222SEric W. Biederman 					inode->i_uid = GLOBAL_ROOT_UID;
1823dcb0f222SEric W. Biederman 					inode->i_gid = GLOBAL_ROOT_GID;
182428a6d671SEric W. Biederman 				}
182530a08bf2SLinus Torvalds 
18260640113bSLinus Torvalds 				if (S_ISLNK(inode->i_mode)) {
18270640113bSLinus Torvalds 					unsigned i_mode = S_IFLNK;
182830a08bf2SLinus Torvalds 					if (f_mode & FMODE_READ)
182930a08bf2SLinus Torvalds 						i_mode |= S_IRUSR | S_IXUSR;
183030a08bf2SLinus Torvalds 					if (f_mode & FMODE_WRITE)
183130a08bf2SLinus Torvalds 						i_mode |= S_IWUSR | S_IXUSR;
183230a08bf2SLinus Torvalds 					inode->i_mode = i_mode;
18330640113bSLinus Torvalds 				}
183430a08bf2SLinus Torvalds 
183528a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
183628a6d671SEric W. Biederman 				put_task_struct(task);
183728a6d671SEric W. Biederman 				return 1;
183828a6d671SEric W. Biederman 			}
183928a6d671SEric W. Biederman 			rcu_read_unlock();
184028a6d671SEric W. Biederman 			put_files_struct(files);
184128a6d671SEric W. Biederman 		}
184228a6d671SEric W. Biederman 		put_task_struct(task);
184328a6d671SEric W. Biederman 	}
184428a6d671SEric W. Biederman 	d_drop(dentry);
184528a6d671SEric W. Biederman 	return 0;
184628a6d671SEric W. Biederman }
184728a6d671SEric W. Biederman 
1848d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
184928a6d671SEric W. Biederman {
185028a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
185128a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
185228a6d671SEric W. Biederman };
185328a6d671SEric W. Biederman 
1854444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1855c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
185628a6d671SEric W. Biederman {
1857af5e6171SAlexey Dobriyan 	unsigned fd = (unsigned long)ptr;
185828a6d671SEric W. Biederman  	struct inode *inode;
185928a6d671SEric W. Biederman  	struct proc_inode *ei;
1860444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
186128a6d671SEric W. Biederman 
186261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
186328a6d671SEric W. Biederman 	if (!inode)
186428a6d671SEric W. Biederman 		goto out;
186528a6d671SEric W. Biederman 	ei = PROC_I(inode);
186628a6d671SEric W. Biederman 	ei->fd = fd;
1867444ceed8SEric W. Biederman 
18680640113bSLinus Torvalds 	inode->i_mode = S_IFLNK;
18695e442a49SLinus Torvalds 	inode->i_op = &proc_pid_link_inode_operations;
187028a6d671SEric W. Biederman 	inode->i_size = 64;
187128a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
1872fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
187328a6d671SEric W. Biederman 	d_add(dentry, inode);
187428a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
18750b728e19SAl Viro 	if (tid_fd_revalidate(dentry, 0))
1876444ceed8SEric W. Biederman 		error = NULL;
1877444ceed8SEric W. Biederman 
1878444ceed8SEric W. Biederman  out:
1879444ceed8SEric W. Biederman 	return error;
1880444ceed8SEric W. Biederman }
1881444ceed8SEric W. Biederman 
188227932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
188327932742SMiklos Szeredi 					   struct dentry *dentry,
188427932742SMiklos Szeredi 					   instantiate_t instantiate)
1885444ceed8SEric W. Biederman {
1886444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1887444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1888444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1889444ceed8SEric W. Biederman 
1890444ceed8SEric W. Biederman 	if (!task)
1891444ceed8SEric W. Biederman 		goto out_no_task;
1892444ceed8SEric W. Biederman 	if (fd == ~0U)
1893444ceed8SEric W. Biederman 		goto out;
1894444ceed8SEric W. Biederman 
1895af5e6171SAlexey Dobriyan 	result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
189628a6d671SEric W. Biederman out:
189728a6d671SEric W. Biederman 	put_task_struct(task);
189828a6d671SEric W. Biederman out_no_task:
189928a6d671SEric W. Biederman 	return result;
190028a6d671SEric W. Biederman }
190128a6d671SEric W. Biederman 
190227932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
190327932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
19041da177e4SLinus Torvalds {
19052fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
19065634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
190799f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1908457c2510SPavel Emelyanov 	unsigned int fd, ino;
19091da177e4SLinus Torvalds 	int retval;
19101da177e4SLinus Torvalds 	struct files_struct * files;
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	retval = -ENOENT;
191399f89551SEric W. Biederman 	if (!p)
191499f89551SEric W. Biederman 		goto out_no_task;
19151da177e4SLinus Torvalds 	retval = 0;
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	fd = filp->f_pos;
19181da177e4SLinus Torvalds 	switch (fd) {
19191da177e4SLinus Torvalds 		case 0:
19201da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
19215e442a49SLinus Torvalds 				goto out;
19221da177e4SLinus Torvalds 			filp->f_pos++;
19231da177e4SLinus Torvalds 		case 1:
19245634708bSEric W. Biederman 			ino = parent_ino(dentry);
19251da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
19265e442a49SLinus Torvalds 				goto out;
19271da177e4SLinus Torvalds 			filp->f_pos++;
19281da177e4SLinus Torvalds 		default:
19291da177e4SLinus Torvalds 			files = get_files_struct(p);
19301da177e4SLinus Torvalds 			if (!files)
19315e442a49SLinus Torvalds 				goto out;
1932b835996fSDipankar Sarma 			rcu_read_lock();
19331da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
19349b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
19351da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
193627932742SMiklos Szeredi 				char name[PROC_NUMBUF];
193727932742SMiklos Szeredi 				int len;
1938f05ed3f1SAlexey Dobriyan 				int rv;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
19411da177e4SLinus Torvalds 					continue;
1942b835996fSDipankar Sarma 				rcu_read_unlock();
19431da177e4SLinus Torvalds 
194427932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
1945f05ed3f1SAlexey Dobriyan 				rv = proc_fill_cache(filp, dirent, filldir,
1946f05ed3f1SAlexey Dobriyan 						     name, len, instantiate, p,
1947af5e6171SAlexey Dobriyan 						     (void *)(unsigned long)fd);
1948f05ed3f1SAlexey Dobriyan 				if (rv < 0)
1949f05ed3f1SAlexey Dobriyan 					goto out_fd_loop;
1950b835996fSDipankar Sarma 				rcu_read_lock();
19511da177e4SLinus Torvalds 			}
1952b835996fSDipankar Sarma 			rcu_read_unlock();
1953f05ed3f1SAlexey Dobriyan out_fd_loop:
19541da177e4SLinus Torvalds 			put_files_struct(files);
19551da177e4SLinus Torvalds 	}
19561da177e4SLinus Torvalds out:
195799f89551SEric W. Biederman 	put_task_struct(p);
195899f89551SEric W. Biederman out_no_task:
19591da177e4SLinus Torvalds 	return retval;
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds 
196227932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
196300cd8dd3SAl Viro 				    unsigned int flags)
196427932742SMiklos Szeredi {
196527932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
196627932742SMiklos Szeredi }
196727932742SMiklos Szeredi 
196827932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
196927932742SMiklos Szeredi {
197027932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
197127932742SMiklos Szeredi }
197227932742SMiklos Szeredi 
197327932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
197427932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
197527932742SMiklos Szeredi {
197627932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
19773dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
197827932742SMiklos Szeredi 	if (!err)
197927932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
198027932742SMiklos Szeredi 	return err;
198127932742SMiklos Szeredi }
198227932742SMiklos Szeredi 
198327932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
198427932742SMiklos Szeredi 	.open           = nonseekable_open,
198527932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
19866038f373SArnd Bergmann 	.llseek		= no_llseek,
198727932742SMiklos Szeredi };
198827932742SMiklos Szeredi 
198900977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
19901da177e4SLinus Torvalds 	.read		= generic_read_dir,
19911da177e4SLinus Torvalds 	.readdir	= proc_readfd,
19926038f373SArnd Bergmann 	.llseek		= default_llseek,
19931da177e4SLinus Torvalds };
19941da177e4SLinus Torvalds 
1995640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
1996640708a2SPavel Emelyanov 
1997640708a2SPavel Emelyanov /*
1998640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1999640708a2SPavel Emelyanov  * which represent vma start and end addresses.
2000640708a2SPavel Emelyanov  */
2001640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
2002640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
2003640708a2SPavel Emelyanov {
2004640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2005640708a2SPavel Emelyanov 		return -EINVAL;
2006640708a2SPavel Emelyanov 
2007640708a2SPavel Emelyanov 	return 0;
2008640708a2SPavel Emelyanov }
2009640708a2SPavel Emelyanov 
20100b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
2011640708a2SPavel Emelyanov {
2012640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2013640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
2014640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
2015640708a2SPavel Emelyanov 	struct task_struct *task;
2016640708a2SPavel Emelyanov 	const struct cred *cred;
2017640708a2SPavel Emelyanov 	struct inode *inode;
2018640708a2SPavel Emelyanov 	int status = 0;
2019640708a2SPavel Emelyanov 
20200b728e19SAl Viro 	if (flags & LOOKUP_RCU)
2021640708a2SPavel Emelyanov 		return -ECHILD;
2022640708a2SPavel Emelyanov 
2023640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
2024640708a2SPavel Emelyanov 		status = -EACCES;
2025640708a2SPavel Emelyanov 		goto out_notask;
2026640708a2SPavel Emelyanov 	}
2027640708a2SPavel Emelyanov 
2028640708a2SPavel Emelyanov 	inode = dentry->d_inode;
2029640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2030640708a2SPavel Emelyanov 	if (!task)
2031640708a2SPavel Emelyanov 		goto out_notask;
2032640708a2SPavel Emelyanov 
20332344bec7SCong Wang 	mm = mm_access(task, PTRACE_MODE_READ);
20342344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
2035640708a2SPavel Emelyanov 		goto out;
2036640708a2SPavel Emelyanov 
2037640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2038640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2039640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2040640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2041640708a2SPavel Emelyanov 	}
2042640708a2SPavel Emelyanov 
2043640708a2SPavel Emelyanov 	mmput(mm);
2044640708a2SPavel Emelyanov 
2045640708a2SPavel Emelyanov 	if (exact_vma_exists) {
2046640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
2047640708a2SPavel Emelyanov 			rcu_read_lock();
2048640708a2SPavel Emelyanov 			cred = __task_cred(task);
2049640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
2050640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
2051640708a2SPavel Emelyanov 			rcu_read_unlock();
2052640708a2SPavel Emelyanov 		} else {
2053dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
2054dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
2055640708a2SPavel Emelyanov 		}
2056640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
2057640708a2SPavel Emelyanov 		status = 1;
2058640708a2SPavel Emelyanov 	}
2059640708a2SPavel Emelyanov 
2060640708a2SPavel Emelyanov out:
2061640708a2SPavel Emelyanov 	put_task_struct(task);
2062640708a2SPavel Emelyanov 
2063640708a2SPavel Emelyanov out_notask:
2064640708a2SPavel Emelyanov 	if (status <= 0)
2065640708a2SPavel Emelyanov 		d_drop(dentry);
2066640708a2SPavel Emelyanov 
2067640708a2SPavel Emelyanov 	return status;
2068640708a2SPavel Emelyanov }
2069640708a2SPavel Emelyanov 
2070640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
2071640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
2072640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
2073640708a2SPavel Emelyanov };
2074640708a2SPavel Emelyanov 
2075640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2076640708a2SPavel Emelyanov {
2077640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2078640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2079640708a2SPavel Emelyanov 	struct task_struct *task;
2080640708a2SPavel Emelyanov 	struct mm_struct *mm;
2081640708a2SPavel Emelyanov 	int rc;
2082640708a2SPavel Emelyanov 
2083640708a2SPavel Emelyanov 	rc = -ENOENT;
2084640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
2085640708a2SPavel Emelyanov 	if (!task)
2086640708a2SPavel Emelyanov 		goto out;
2087640708a2SPavel Emelyanov 
2088640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2089640708a2SPavel Emelyanov 	put_task_struct(task);
2090640708a2SPavel Emelyanov 	if (!mm)
2091640708a2SPavel Emelyanov 		goto out;
2092640708a2SPavel Emelyanov 
2093640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2094640708a2SPavel Emelyanov 	if (rc)
2095640708a2SPavel Emelyanov 		goto out_mmput;
2096640708a2SPavel Emelyanov 
2097640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2098640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2099640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
2100640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
2101640708a2SPavel Emelyanov 		path_get(path);
2102640708a2SPavel Emelyanov 		rc = 0;
2103640708a2SPavel Emelyanov 	}
2104640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2105640708a2SPavel Emelyanov 
2106640708a2SPavel Emelyanov out_mmput:
2107640708a2SPavel Emelyanov 	mmput(mm);
2108640708a2SPavel Emelyanov out:
2109640708a2SPavel Emelyanov 	return rc;
2110640708a2SPavel Emelyanov }
2111640708a2SPavel Emelyanov 
2112640708a2SPavel Emelyanov struct map_files_info {
2113640708a2SPavel Emelyanov 	struct file	*file;
2114640708a2SPavel Emelyanov 	unsigned long	len;
2115640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2116640708a2SPavel Emelyanov };
2117640708a2SPavel Emelyanov 
2118640708a2SPavel Emelyanov static struct dentry *
2119640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2120640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
2121640708a2SPavel Emelyanov {
2122640708a2SPavel Emelyanov 	const struct file *file = ptr;
2123640708a2SPavel Emelyanov 	struct proc_inode *ei;
2124640708a2SPavel Emelyanov 	struct inode *inode;
2125640708a2SPavel Emelyanov 
2126640708a2SPavel Emelyanov 	if (!file)
2127640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2128640708a2SPavel Emelyanov 
2129640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
2130640708a2SPavel Emelyanov 	if (!inode)
2131640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2132640708a2SPavel Emelyanov 
2133640708a2SPavel Emelyanov 	ei = PROC_I(inode);
2134640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
2135640708a2SPavel Emelyanov 
2136640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
2137640708a2SPavel Emelyanov 	inode->i_size = 64;
2138640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
2139640708a2SPavel Emelyanov 
2140640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_READ)
2141640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
2142640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_WRITE)
2143640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
2144640708a2SPavel Emelyanov 
2145640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
2146640708a2SPavel Emelyanov 	d_add(dentry, inode);
2147640708a2SPavel Emelyanov 
2148640708a2SPavel Emelyanov 	return NULL;
2149640708a2SPavel Emelyanov }
2150640708a2SPavel Emelyanov 
2151640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
215200cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
2153640708a2SPavel Emelyanov {
2154640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2155640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2156640708a2SPavel Emelyanov 	struct task_struct *task;
2157640708a2SPavel Emelyanov 	struct dentry *result;
2158640708a2SPavel Emelyanov 	struct mm_struct *mm;
2159640708a2SPavel Emelyanov 
2160640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2161640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2162640708a2SPavel Emelyanov 		goto out;
2163640708a2SPavel Emelyanov 
2164640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2165640708a2SPavel Emelyanov 	task = get_proc_task(dir);
2166640708a2SPavel Emelyanov 	if (!task)
2167640708a2SPavel Emelyanov 		goto out;
2168640708a2SPavel Emelyanov 
2169640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2170eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
2171640708a2SPavel Emelyanov 		goto out_put_task;
2172640708a2SPavel Emelyanov 
2173640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2174640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2175eb94cd96SCyrill Gorcunov 		goto out_put_task;
2176640708a2SPavel Emelyanov 
2177640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2178640708a2SPavel Emelyanov 	if (!mm)
2179eb94cd96SCyrill Gorcunov 		goto out_put_task;
2180640708a2SPavel Emelyanov 
2181640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2182640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2183640708a2SPavel Emelyanov 	if (!vma)
2184640708a2SPavel Emelyanov 		goto out_no_vma;
2185640708a2SPavel Emelyanov 
2186640708a2SPavel Emelyanov 	result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2187640708a2SPavel Emelyanov 
2188640708a2SPavel Emelyanov out_no_vma:
2189640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2190640708a2SPavel Emelyanov 	mmput(mm);
2191640708a2SPavel Emelyanov out_put_task:
2192640708a2SPavel Emelyanov 	put_task_struct(task);
2193640708a2SPavel Emelyanov out:
2194640708a2SPavel Emelyanov 	return result;
2195640708a2SPavel Emelyanov }
2196640708a2SPavel Emelyanov 
2197640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
2198640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
2199640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
2200640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
2201640708a2SPavel Emelyanov };
2202640708a2SPavel Emelyanov 
2203640708a2SPavel Emelyanov static int
2204640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2205640708a2SPavel Emelyanov {
2206640708a2SPavel Emelyanov 	struct dentry *dentry = filp->f_path.dentry;
2207640708a2SPavel Emelyanov 	struct inode *inode = dentry->d_inode;
2208640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2209640708a2SPavel Emelyanov 	struct task_struct *task;
2210640708a2SPavel Emelyanov 	struct mm_struct *mm;
2211640708a2SPavel Emelyanov 	ino_t ino;
2212640708a2SPavel Emelyanov 	int ret;
2213640708a2SPavel Emelyanov 
2214640708a2SPavel Emelyanov 	ret = -EACCES;
2215640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2216640708a2SPavel Emelyanov 		goto out;
2217640708a2SPavel Emelyanov 
2218640708a2SPavel Emelyanov 	ret = -ENOENT;
2219640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2220640708a2SPavel Emelyanov 	if (!task)
2221640708a2SPavel Emelyanov 		goto out;
2222640708a2SPavel Emelyanov 
2223640708a2SPavel Emelyanov 	ret = -EACCES;
2224eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
2225640708a2SPavel Emelyanov 		goto out_put_task;
2226640708a2SPavel Emelyanov 
2227640708a2SPavel Emelyanov 	ret = 0;
2228640708a2SPavel Emelyanov 	switch (filp->f_pos) {
2229640708a2SPavel Emelyanov 	case 0:
2230640708a2SPavel Emelyanov 		ino = inode->i_ino;
2231640708a2SPavel Emelyanov 		if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2232eb94cd96SCyrill Gorcunov 			goto out_put_task;
2233640708a2SPavel Emelyanov 		filp->f_pos++;
2234640708a2SPavel Emelyanov 	case 1:
2235640708a2SPavel Emelyanov 		ino = parent_ino(dentry);
2236640708a2SPavel Emelyanov 		if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2237eb94cd96SCyrill Gorcunov 			goto out_put_task;
2238640708a2SPavel Emelyanov 		filp->f_pos++;
2239640708a2SPavel Emelyanov 	default:
2240640708a2SPavel Emelyanov 	{
2241640708a2SPavel Emelyanov 		unsigned long nr_files, pos, i;
2242640708a2SPavel Emelyanov 		struct flex_array *fa = NULL;
2243640708a2SPavel Emelyanov 		struct map_files_info info;
2244640708a2SPavel Emelyanov 		struct map_files_info *p;
2245640708a2SPavel Emelyanov 
2246640708a2SPavel Emelyanov 		mm = get_task_mm(task);
2247640708a2SPavel Emelyanov 		if (!mm)
2248eb94cd96SCyrill Gorcunov 			goto out_put_task;
2249640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2250640708a2SPavel Emelyanov 
2251640708a2SPavel Emelyanov 		nr_files = 0;
2252640708a2SPavel Emelyanov 
2253640708a2SPavel Emelyanov 		/*
2254640708a2SPavel Emelyanov 		 * We need two passes here:
2255640708a2SPavel Emelyanov 		 *
2256640708a2SPavel Emelyanov 		 *  1) Collect vmas of mapped files with mmap_sem taken
2257640708a2SPavel Emelyanov 		 *  2) Release mmap_sem and instantiate entries
2258640708a2SPavel Emelyanov 		 *
2259640708a2SPavel Emelyanov 		 * otherwise we get lockdep complained, since filldir()
2260640708a2SPavel Emelyanov 		 * routine might require mmap_sem taken in might_fault().
2261640708a2SPavel Emelyanov 		 */
2262640708a2SPavel Emelyanov 
2263640708a2SPavel Emelyanov 		for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2264640708a2SPavel Emelyanov 			if (vma->vm_file && ++pos > filp->f_pos)
2265640708a2SPavel Emelyanov 				nr_files++;
2266640708a2SPavel Emelyanov 		}
2267640708a2SPavel Emelyanov 
2268640708a2SPavel Emelyanov 		if (nr_files) {
2269640708a2SPavel Emelyanov 			fa = flex_array_alloc(sizeof(info), nr_files,
2270640708a2SPavel Emelyanov 						GFP_KERNEL);
2271640708a2SPavel Emelyanov 			if (!fa || flex_array_prealloc(fa, 0, nr_files,
2272640708a2SPavel Emelyanov 							GFP_KERNEL)) {
2273640708a2SPavel Emelyanov 				ret = -ENOMEM;
2274640708a2SPavel Emelyanov 				if (fa)
2275640708a2SPavel Emelyanov 					flex_array_free(fa);
2276640708a2SPavel Emelyanov 				up_read(&mm->mmap_sem);
2277640708a2SPavel Emelyanov 				mmput(mm);
2278eb94cd96SCyrill Gorcunov 				goto out_put_task;
2279640708a2SPavel Emelyanov 			}
2280640708a2SPavel Emelyanov 			for (i = 0, vma = mm->mmap, pos = 2; vma;
2281640708a2SPavel Emelyanov 					vma = vma->vm_next) {
2282640708a2SPavel Emelyanov 				if (!vma->vm_file)
2283640708a2SPavel Emelyanov 					continue;
2284640708a2SPavel Emelyanov 				if (++pos <= filp->f_pos)
2285640708a2SPavel Emelyanov 					continue;
2286640708a2SPavel Emelyanov 
2287640708a2SPavel Emelyanov 				get_file(vma->vm_file);
2288640708a2SPavel Emelyanov 				info.file = vma->vm_file;
2289640708a2SPavel Emelyanov 				info.len = snprintf(info.name,
2290640708a2SPavel Emelyanov 						sizeof(info.name), "%lx-%lx",
2291640708a2SPavel Emelyanov 						vma->vm_start, vma->vm_end);
2292640708a2SPavel Emelyanov 				if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2293640708a2SPavel Emelyanov 					BUG();
2294640708a2SPavel Emelyanov 			}
2295640708a2SPavel Emelyanov 		}
2296640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2297640708a2SPavel Emelyanov 
2298640708a2SPavel Emelyanov 		for (i = 0; i < nr_files; i++) {
2299640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2300640708a2SPavel Emelyanov 			ret = proc_fill_cache(filp, dirent, filldir,
2301640708a2SPavel Emelyanov 					      p->name, p->len,
2302640708a2SPavel Emelyanov 					      proc_map_files_instantiate,
2303640708a2SPavel Emelyanov 					      task, p->file);
2304640708a2SPavel Emelyanov 			if (ret)
2305640708a2SPavel Emelyanov 				break;
2306640708a2SPavel Emelyanov 			filp->f_pos++;
2307640708a2SPavel Emelyanov 			fput(p->file);
2308640708a2SPavel Emelyanov 		}
2309640708a2SPavel Emelyanov 		for (; i < nr_files; i++) {
2310640708a2SPavel Emelyanov 			/*
2311640708a2SPavel Emelyanov 			 * In case of error don't forget
2312640708a2SPavel Emelyanov 			 * to put rest of file refs.
2313640708a2SPavel Emelyanov 			 */
2314640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2315640708a2SPavel Emelyanov 			fput(p->file);
2316640708a2SPavel Emelyanov 		}
2317640708a2SPavel Emelyanov 		if (fa)
2318640708a2SPavel Emelyanov 			flex_array_free(fa);
2319640708a2SPavel Emelyanov 		mmput(mm);
2320640708a2SPavel Emelyanov 	}
2321640708a2SPavel Emelyanov 	}
2322640708a2SPavel Emelyanov 
2323640708a2SPavel Emelyanov out_put_task:
2324640708a2SPavel Emelyanov 	put_task_struct(task);
2325640708a2SPavel Emelyanov out:
2326640708a2SPavel Emelyanov 	return ret;
2327640708a2SPavel Emelyanov }
2328640708a2SPavel Emelyanov 
2329640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2330640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2331640708a2SPavel Emelyanov 	.readdir	= proc_map_files_readdir,
2332640708a2SPavel Emelyanov 	.llseek		= default_llseek,
2333640708a2SPavel Emelyanov };
2334640708a2SPavel Emelyanov 
2335640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2336640708a2SPavel Emelyanov 
23371da177e4SLinus Torvalds /*
23388948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
23398948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
23408948e11fSAlexey Dobriyan  */
234110556cb2SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
23428948e11fSAlexey Dobriyan {
23432830ba7fSAl Viro 	int rv = generic_permission(inode, mask);
23448948e11fSAlexey Dobriyan 	if (rv == 0)
23458948e11fSAlexey Dobriyan 		return 0;
23468948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
23478948e11fSAlexey Dobriyan 		rv = 0;
23488948e11fSAlexey Dobriyan 	return rv;
23498948e11fSAlexey Dobriyan }
23508948e11fSAlexey Dobriyan 
23518948e11fSAlexey Dobriyan /*
23521da177e4SLinus Torvalds  * proc directories can do almost nothing..
23531da177e4SLinus Torvalds  */
2354c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
23551da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
23568948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
23576d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
23581da177e4SLinus Torvalds };
23591da177e4SLinus Torvalds 
236027932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
236127932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
236227932742SMiklos Szeredi {
2363af5e6171SAlexey Dobriyan 	unsigned fd = (unsigned long)ptr;
236427932742SMiklos Szeredi  	struct inode *inode;
236527932742SMiklos Szeredi  	struct proc_inode *ei;
236627932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
236727932742SMiklos Szeredi 
236827932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
236927932742SMiklos Szeredi 	if (!inode)
237027932742SMiklos Szeredi 		goto out;
237127932742SMiklos Szeredi 	ei = PROC_I(inode);
237227932742SMiklos Szeredi 	ei->fd = fd;
237327932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
237427932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
2375fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
237627932742SMiklos Szeredi 	d_add(dentry, inode);
237727932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
23780b728e19SAl Viro 	if (tid_fd_revalidate(dentry, 0))
237927932742SMiklos Szeredi 		error = NULL;
238027932742SMiklos Szeredi 
238127932742SMiklos Szeredi  out:
238227932742SMiklos Szeredi 	return error;
238327932742SMiklos Szeredi }
238427932742SMiklos Szeredi 
238527932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
238627932742SMiklos Szeredi 					struct dentry *dentry,
238700cd8dd3SAl Viro 					unsigned int flags)
238827932742SMiklos Szeredi {
238927932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
239027932742SMiklos Szeredi }
239127932742SMiklos Szeredi 
239227932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
239327932742SMiklos Szeredi {
239427932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
239527932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
239627932742SMiklos Szeredi }
239727932742SMiklos Szeredi 
239827932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
239927932742SMiklos Szeredi 	.read		= generic_read_dir,
240027932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
24016038f373SArnd Bergmann 	.llseek		= default_llseek,
240227932742SMiklos Szeredi };
240327932742SMiklos Szeredi 
240427932742SMiklos Szeredi /*
240527932742SMiklos Szeredi  * proc directories can do almost nothing..
240627932742SMiklos Szeredi  */
240727932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
240827932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
240927932742SMiklos Szeredi 	.setattr	= proc_setattr,
241027932742SMiklos Szeredi };
241127932742SMiklos Szeredi 
241227932742SMiklos Szeredi 
2413444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2414c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2415444ceed8SEric W. Biederman {
2416c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2417444ceed8SEric W. Biederman 	struct inode *inode;
2418444ceed8SEric W. Biederman 	struct proc_inode *ei;
2419bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2420444ceed8SEric W. Biederman 
242161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2422444ceed8SEric W. Biederman 	if (!inode)
2423444ceed8SEric W. Biederman 		goto out;
2424444ceed8SEric W. Biederman 
2425444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2426444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2427444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2428bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2429444ceed8SEric W. Biederman 	if (p->iop)
2430444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2431444ceed8SEric W. Biederman 	if (p->fop)
2432444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2433444ceed8SEric W. Biederman 	ei->op = p->op;
2434fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2435444ceed8SEric W. Biederman 	d_add(dentry, inode);
2436444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
24370b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2438444ceed8SEric W. Biederman 		error = NULL;
2439444ceed8SEric W. Biederman out:
2440444ceed8SEric W. Biederman 	return error;
2441444ceed8SEric W. Biederman }
2442444ceed8SEric W. Biederman 
24431da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
24441da177e4SLinus Torvalds 					 struct dentry *dentry,
2445c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
24467bcd6b0eSEric W. Biederman 					 unsigned int nents)
24471da177e4SLinus Torvalds {
2448cd6a3ce9SEric W. Biederman 	struct dentry *error;
244999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2450c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
24511da177e4SLinus Torvalds 
2452cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
24531da177e4SLinus Torvalds 
245499f89551SEric W. Biederman 	if (!task)
245599f89551SEric W. Biederman 		goto out_no_task;
24561da177e4SLinus Torvalds 
245720cdc894SEric W. Biederman 	/*
245820cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
245920cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
246020cdc894SEric W. Biederman 	 */
24617bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
24627bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
24631da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
24641da177e4SLinus Torvalds 			continue;
24651da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
24661da177e4SLinus Torvalds 			break;
24671da177e4SLinus Torvalds 	}
24687bcd6b0eSEric W. Biederman 	if (p > last)
24691da177e4SLinus Torvalds 		goto out;
24701da177e4SLinus Torvalds 
2471444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
24721da177e4SLinus Torvalds out:
247399f89551SEric W. Biederman 	put_task_struct(task);
247499f89551SEric W. Biederman out_no_task:
2475cd6a3ce9SEric W. Biederman 	return error;
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds 
2478c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2479c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
248061a28784SEric W. Biederman {
248161a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
248261a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
248361a28784SEric W. Biederman }
248461a28784SEric W. Biederman 
248528a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
248628a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2487c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
248828a6d671SEric W. Biederman {
248928a6d671SEric W. Biederman 	int i;
24902fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
249128a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
249228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2493c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
249428a6d671SEric W. Biederman 	ino_t ino;
249528a6d671SEric W. Biederman 	int ret;
249628a6d671SEric W. Biederman 
249728a6d671SEric W. Biederman 	ret = -ENOENT;
249828a6d671SEric W. Biederman 	if (!task)
249961a28784SEric W. Biederman 		goto out_no_task;
250028a6d671SEric W. Biederman 
250128a6d671SEric W. Biederman 	ret = 0;
250228a6d671SEric W. Biederman 	i = filp->f_pos;
250328a6d671SEric W. Biederman 	switch (i) {
250428a6d671SEric W. Biederman 	case 0:
250528a6d671SEric W. Biederman 		ino = inode->i_ino;
250628a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
250728a6d671SEric W. Biederman 			goto out;
250828a6d671SEric W. Biederman 		i++;
250928a6d671SEric W. Biederman 		filp->f_pos++;
251028a6d671SEric W. Biederman 		/* fall through */
251128a6d671SEric W. Biederman 	case 1:
251228a6d671SEric W. Biederman 		ino = parent_ino(dentry);
251328a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
251428a6d671SEric W. Biederman 			goto out;
251528a6d671SEric W. Biederman 		i++;
251628a6d671SEric W. Biederman 		filp->f_pos++;
251728a6d671SEric W. Biederman 		/* fall through */
251828a6d671SEric W. Biederman 	default:
251928a6d671SEric W. Biederman 		i -= 2;
252028a6d671SEric W. Biederman 		if (i >= nents) {
252128a6d671SEric W. Biederman 			ret = 1;
252228a6d671SEric W. Biederman 			goto out;
252328a6d671SEric W. Biederman 		}
252428a6d671SEric W. Biederman 		p = ents + i;
25257bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
25267bcd6b0eSEric W. Biederman 		while (p <= last) {
252761a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
252828a6d671SEric W. Biederman 				goto out;
252928a6d671SEric W. Biederman 			filp->f_pos++;
253028a6d671SEric W. Biederman 			p++;
253128a6d671SEric W. Biederman 		}
25321da177e4SLinus Torvalds 	}
25331da177e4SLinus Torvalds 
253428a6d671SEric W. Biederman 	ret = 1;
253528a6d671SEric W. Biederman out:
253661a28784SEric W. Biederman 	put_task_struct(task);
253761a28784SEric W. Biederman out_no_task:
253828a6d671SEric W. Biederman 	return ret;
25391da177e4SLinus Torvalds }
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
254228a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
254328a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
254428a6d671SEric W. Biederman {
25452fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
254604ff9708SAl Viro 	char *p = NULL;
254728a6d671SEric W. Biederman 	ssize_t length;
254828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
254928a6d671SEric W. Biederman 
255028a6d671SEric W. Biederman 	if (!task)
255104ff9708SAl Viro 		return -ESRCH;
255228a6d671SEric W. Biederman 
255328a6d671SEric W. Biederman 	length = security_getprocattr(task,
25542fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
255504ff9708SAl Viro 				      &p);
255628a6d671SEric W. Biederman 	put_task_struct(task);
255704ff9708SAl Viro 	if (length > 0)
255804ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
255904ff9708SAl Viro 	kfree(p);
256028a6d671SEric W. Biederman 	return length;
256128a6d671SEric W. Biederman }
256228a6d671SEric W. Biederman 
256328a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
256428a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
256528a6d671SEric W. Biederman {
25662fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
256728a6d671SEric W. Biederman 	char *page;
256828a6d671SEric W. Biederman 	ssize_t length;
256928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
257028a6d671SEric W. Biederman 
257128a6d671SEric W. Biederman 	length = -ESRCH;
257228a6d671SEric W. Biederman 	if (!task)
257328a6d671SEric W. Biederman 		goto out_no_task;
257428a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
257528a6d671SEric W. Biederman 		count = PAGE_SIZE;
257628a6d671SEric W. Biederman 
257728a6d671SEric W. Biederman 	/* No partial writes. */
257828a6d671SEric W. Biederman 	length = -EINVAL;
257928a6d671SEric W. Biederman 	if (*ppos != 0)
258028a6d671SEric W. Biederman 		goto out;
258128a6d671SEric W. Biederman 
258228a6d671SEric W. Biederman 	length = -ENOMEM;
2583e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
258428a6d671SEric W. Biederman 	if (!page)
258528a6d671SEric W. Biederman 		goto out;
258628a6d671SEric W. Biederman 
258728a6d671SEric W. Biederman 	length = -EFAULT;
258828a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
258928a6d671SEric W. Biederman 		goto out_free;
259028a6d671SEric W. Biederman 
2591107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
25929b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2593107db7c7SDavid Howells 	if (length < 0)
2594107db7c7SDavid Howells 		goto out_free;
2595107db7c7SDavid Howells 
259628a6d671SEric W. Biederman 	length = security_setprocattr(task,
25972fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
259828a6d671SEric W. Biederman 				      (void*)page, count);
25999b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
260028a6d671SEric W. Biederman out_free:
260128a6d671SEric W. Biederman 	free_page((unsigned long) page);
260228a6d671SEric W. Biederman out:
260328a6d671SEric W. Biederman 	put_task_struct(task);
260428a6d671SEric W. Biederman out_no_task:
260528a6d671SEric W. Biederman 	return length;
260628a6d671SEric W. Biederman }
260728a6d671SEric W. Biederman 
260800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
260928a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
261028a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
261187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
261228a6d671SEric W. Biederman };
261328a6d671SEric W. Biederman 
2614c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2615631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2616631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2617631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2618631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2619631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2620631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
262128a6d671SEric W. Biederman };
262228a6d671SEric W. Biederman 
262372d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
26241da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
26251da177e4SLinus Torvalds {
26261da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
262772d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
26281da177e4SLinus Torvalds }
26291da177e4SLinus Torvalds 
263000977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
26311da177e4SLinus Torvalds 	.read		= generic_read_dir,
263272d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
26336038f373SArnd Bergmann 	.llseek		= default_llseek,
26341da177e4SLinus Torvalds };
26351da177e4SLinus Torvalds 
263672d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
263700cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
26381da177e4SLinus Torvalds {
26397bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26407bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
26411da177e4SLinus Torvalds }
26421da177e4SLinus Torvalds 
2643c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
264472d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
264599f89551SEric W. Biederman 	.getattr	= pid_getattr,
26466d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
26471da177e4SLinus Torvalds };
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds #endif
26501da177e4SLinus Torvalds 
2651698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
26523cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
26533cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
26543cb4a0bbSKawai, Hidehiro {
26553cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
26563cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26573cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
26583cb4a0bbSKawai, Hidehiro 	size_t len;
26593cb4a0bbSKawai, Hidehiro 	int ret;
26603cb4a0bbSKawai, Hidehiro 
26613cb4a0bbSKawai, Hidehiro 	if (!task)
26623cb4a0bbSKawai, Hidehiro 		return -ESRCH;
26633cb4a0bbSKawai, Hidehiro 
26643cb4a0bbSKawai, Hidehiro 	ret = 0;
26653cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
26663cb4a0bbSKawai, Hidehiro 	if (mm) {
26673cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
26683cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
26693cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
26703cb4a0bbSKawai, Hidehiro 		mmput(mm);
26713cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
26723cb4a0bbSKawai, Hidehiro 	}
26733cb4a0bbSKawai, Hidehiro 
26743cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
26753cb4a0bbSKawai, Hidehiro 
26763cb4a0bbSKawai, Hidehiro 	return ret;
26773cb4a0bbSKawai, Hidehiro }
26783cb4a0bbSKawai, Hidehiro 
26793cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
26803cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
26813cb4a0bbSKawai, Hidehiro 					  size_t count,
26823cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
26833cb4a0bbSKawai, Hidehiro {
26843cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
26853cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26863cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
26873cb4a0bbSKawai, Hidehiro 	unsigned int val;
26883cb4a0bbSKawai, Hidehiro 	int ret;
26893cb4a0bbSKawai, Hidehiro 	int i;
26903cb4a0bbSKawai, Hidehiro 	unsigned long mask;
26913cb4a0bbSKawai, Hidehiro 
26923cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
26933cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
26943cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
26953cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
26963cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
26973cb4a0bbSKawai, Hidehiro 		goto out_no_task;
26983cb4a0bbSKawai, Hidehiro 
26993cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
27003cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
27013cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
27023cb4a0bbSKawai, Hidehiro 		end++;
27033cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
27043cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27053cb4a0bbSKawai, Hidehiro 
27063cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
27073cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
27083cb4a0bbSKawai, Hidehiro 	if (!task)
27093cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27103cb4a0bbSKawai, Hidehiro 
27113cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
27123cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
27133cb4a0bbSKawai, Hidehiro 	if (!mm)
27143cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
27153cb4a0bbSKawai, Hidehiro 
27163cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
27173cb4a0bbSKawai, Hidehiro 		if (val & mask)
27183cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27193cb4a0bbSKawai, Hidehiro 		else
27203cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27213cb4a0bbSKawai, Hidehiro 	}
27223cb4a0bbSKawai, Hidehiro 
27233cb4a0bbSKawai, Hidehiro 	mmput(mm);
27243cb4a0bbSKawai, Hidehiro  out_no_mm:
27253cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
27263cb4a0bbSKawai, Hidehiro  out_no_task:
27273cb4a0bbSKawai, Hidehiro 	return ret;
27283cb4a0bbSKawai, Hidehiro }
27293cb4a0bbSKawai, Hidehiro 
27303cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
27313cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
27323cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
273387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
27343cb4a0bbSKawai, Hidehiro };
27353cb4a0bbSKawai, Hidehiro #endif
27363cb4a0bbSKawai, Hidehiro 
27371da177e4SLinus Torvalds /*
27381da177e4SLinus Torvalds  * /proc/self:
27391da177e4SLinus Torvalds  */
27401da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
27411da177e4SLinus Torvalds 			      int buflen)
27421da177e4SLinus Torvalds {
2743488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2744b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
27458578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2746b55fcb22SAndrew Morton 	if (!tgid)
2747488e5bc4SEric W. Biederman 		return -ENOENT;
2748b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
27491da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
27501da177e4SLinus Torvalds }
27511da177e4SLinus Torvalds 
2752008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
27531da177e4SLinus Torvalds {
2754488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2755b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
27567fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
27577fee4868SAl Viro 	if (tgid) {
27587fee4868SAl Viro 		name = __getname();
27597fee4868SAl Viro 		if (!name)
27607fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
27617fee4868SAl Viro 		else
27627fee4868SAl Viro 			sprintf(name, "%d", tgid);
27637fee4868SAl Viro 	}
27647fee4868SAl Viro 	nd_set_link(nd, name);
27657fee4868SAl Viro 	return NULL;
27667fee4868SAl Viro }
27677fee4868SAl Viro 
27687fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
27697fee4868SAl Viro 				void *cookie)
27707fee4868SAl Viro {
27717fee4868SAl Viro 	char *s = nd_get_link(nd);
27727fee4868SAl Viro 	if (!IS_ERR(s))
27737fee4868SAl Viro 		__putname(s);
27741da177e4SLinus Torvalds }
27751da177e4SLinus Torvalds 
2776c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
27771da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
27781da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
27797fee4868SAl Viro 	.put_link	= proc_self_put_link,
27801da177e4SLinus Torvalds };
27811da177e4SLinus Torvalds 
278228a6d671SEric W. Biederman /*
2783801199ceSEric W. Biederman  * proc base
2784801199ceSEric W. Biederman  *
2785801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2786801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2787801199ceSEric W. Biederman  * describe something that is process related.
2788801199ceSEric W. Biederman  */
2789c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
279061a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2791801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2792801199ceSEric W. Biederman };
2793801199ceSEric W. Biederman 
2794444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2795c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2796801199ceSEric W. Biederman {
2797c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2798801199ceSEric W. Biederman 	struct inode *inode;
2799801199ceSEric W. Biederman 	struct proc_inode *ei;
280073d36460SDan Carpenter 	struct dentry *error;
2801801199ceSEric W. Biederman 
2802801199ceSEric W. Biederman 	/* Allocate the inode */
2803801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2804801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2805801199ceSEric W. Biederman 	if (!inode)
2806801199ceSEric W. Biederman 		goto out;
2807801199ceSEric W. Biederman 
2808801199ceSEric W. Biederman 	/* Initialize the inode */
2809801199ceSEric W. Biederman 	ei = PROC_I(inode);
281085fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2811801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2812801199ceSEric W. Biederman 
2813801199ceSEric W. Biederman 	/*
2814801199ceSEric W. Biederman 	 * grab the reference to the task.
2815801199ceSEric W. Biederman 	 */
28161a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2817801199ceSEric W. Biederman 	if (!ei->pid)
2818801199ceSEric W. Biederman 		goto out_iput;
2819801199ceSEric W. Biederman 
2820801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2821801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2822bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
2823801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2824801199ceSEric W. Biederman 		inode->i_size = 64;
2825801199ceSEric W. Biederman 	if (p->iop)
2826801199ceSEric W. Biederman 		inode->i_op = p->iop;
2827801199ceSEric W. Biederman 	if (p->fop)
2828801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2829801199ceSEric W. Biederman 	ei->op = p->op;
2830801199ceSEric W. Biederman 	d_add(dentry, inode);
2831801199ceSEric W. Biederman 	error = NULL;
2832801199ceSEric W. Biederman out:
2833801199ceSEric W. Biederman 	return error;
2834801199ceSEric W. Biederman out_iput:
2835801199ceSEric W. Biederman 	iput(inode);
2836801199ceSEric W. Biederman 	goto out;
2837801199ceSEric W. Biederman }
2838801199ceSEric W. Biederman 
2839444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2840444ceed8SEric W. Biederman {
2841444ceed8SEric W. Biederman 	struct dentry *error;
2842444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2843c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2844444ceed8SEric W. Biederman 
2845444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2846444ceed8SEric W. Biederman 
2847444ceed8SEric W. Biederman 	if (!task)
2848444ceed8SEric W. Biederman 		goto out_no_task;
2849444ceed8SEric W. Biederman 
2850444ceed8SEric W. Biederman 	/* Lookup the directory entry */
28517bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
28527bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2853444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2854444ceed8SEric W. Biederman 			continue;
2855444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2856444ceed8SEric W. Biederman 			break;
2857444ceed8SEric W. Biederman 	}
28587bcd6b0eSEric W. Biederman 	if (p > last)
2859444ceed8SEric W. Biederman 		goto out;
2860444ceed8SEric W. Biederman 
2861444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2862444ceed8SEric W. Biederman 
2863444ceed8SEric W. Biederman out:
2864444ceed8SEric W. Biederman 	put_task_struct(task);
2865444ceed8SEric W. Biederman out_no_task:
2866444ceed8SEric W. Biederman 	return error;
2867444ceed8SEric W. Biederman }
2868444ceed8SEric W. Biederman 
2869c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2870c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
287161a28784SEric W. Biederman {
287261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
287361a28784SEric W. Biederman 				proc_base_instantiate, task, p);
287461a28784SEric W. Biederman }
287561a28784SEric W. Biederman 
2876aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2877297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2878aba76fdbSAndrew Morton {
2879940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2880297c5d92SAndrea Righi 	unsigned long flags;
2881293eb1e7SVasiliy Kulikov 	int result;
2882297c5d92SAndrea Righi 
2883293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2884293eb1e7SVasiliy Kulikov 	if (result)
2885293eb1e7SVasiliy Kulikov 		return result;
2886293eb1e7SVasiliy Kulikov 
2887293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2888293eb1e7SVasiliy Kulikov 		result = -EACCES;
2889293eb1e7SVasiliy Kulikov 		goto out_unlock;
2890293eb1e7SVasiliy Kulikov 	}
28911d1221f3SVasiliy Kulikov 
28925995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2893b2d002dbSAndrea Righi 		struct task_struct *t = task;
2894297c5d92SAndrea Righi 
28955995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
28965995477aSAndrea Righi 		while_each_thread(task, t)
28975995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2898297c5d92SAndrea Righi 
2899297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2900297c5d92SAndrea Righi 	}
2901293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2902aba76fdbSAndrew Morton 			"rchar: %llu\n"
2903aba76fdbSAndrew Morton 			"wchar: %llu\n"
2904aba76fdbSAndrew Morton 			"syscr: %llu\n"
2905aba76fdbSAndrew Morton 			"syscw: %llu\n"
2906aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2907aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2908aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
29097c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
29107c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
29117c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
29127c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
29137c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
29147c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
29157c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2916293eb1e7SVasiliy Kulikov out_unlock:
2917293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2918293eb1e7SVasiliy Kulikov 	return result;
2919aba76fdbSAndrew Morton }
2920297c5d92SAndrea Righi 
2921297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2922297c5d92SAndrea Righi {
2923297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2924297c5d92SAndrea Righi }
2925297c5d92SAndrea Righi 
2926297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2927297c5d92SAndrea Righi {
2928297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2929297c5d92SAndrea Righi }
2930297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2931aba76fdbSAndrew Morton 
293222d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
293322d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
293422d917d8SEric W. Biederman 	struct seq_operations *seq_ops)
293522d917d8SEric W. Biederman {
293622d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
293722d917d8SEric W. Biederman 	struct task_struct *task;
293822d917d8SEric W. Biederman 	struct seq_file *seq;
293922d917d8SEric W. Biederman 	int ret = -EINVAL;
294022d917d8SEric W. Biederman 
294122d917d8SEric W. Biederman 	task = get_proc_task(inode);
294222d917d8SEric W. Biederman 	if (task) {
294322d917d8SEric W. Biederman 		rcu_read_lock();
294422d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
294522d917d8SEric W. Biederman 		rcu_read_unlock();
294622d917d8SEric W. Biederman 		put_task_struct(task);
294722d917d8SEric W. Biederman 	}
294822d917d8SEric W. Biederman 	if (!ns)
294922d917d8SEric W. Biederman 		goto err;
295022d917d8SEric W. Biederman 
295122d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
295222d917d8SEric W. Biederman 	if (ret)
295322d917d8SEric W. Biederman 		goto err_put_ns;
295422d917d8SEric W. Biederman 
295522d917d8SEric W. Biederman 	seq = file->private_data;
295622d917d8SEric W. Biederman 	seq->private = ns;
295722d917d8SEric W. Biederman 
295822d917d8SEric W. Biederman 	return 0;
295922d917d8SEric W. Biederman err_put_ns:
296022d917d8SEric W. Biederman 	put_user_ns(ns);
296122d917d8SEric W. Biederman err:
296222d917d8SEric W. Biederman 	return ret;
296322d917d8SEric W. Biederman }
296422d917d8SEric W. Biederman 
296522d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
296622d917d8SEric W. Biederman {
296722d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
296822d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
296922d917d8SEric W. Biederman 	put_user_ns(ns);
297022d917d8SEric W. Biederman 	return seq_release(inode, file);
297122d917d8SEric W. Biederman }
297222d917d8SEric W. Biederman 
297322d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
297422d917d8SEric W. Biederman {
297522d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
297622d917d8SEric W. Biederman }
297722d917d8SEric W. Biederman 
297822d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
297922d917d8SEric W. Biederman {
298022d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
298122d917d8SEric W. Biederman }
298222d917d8SEric W. Biederman 
298322d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
298422d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
298522d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
298622d917d8SEric W. Biederman 	.read		= seq_read,
298722d917d8SEric W. Biederman 	.llseek		= seq_lseek,
298822d917d8SEric W. Biederman 	.release	= proc_id_map_release,
298922d917d8SEric W. Biederman };
299022d917d8SEric W. Biederman 
299122d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
299222d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
299322d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
299422d917d8SEric W. Biederman 	.read		= seq_read,
299522d917d8SEric W. Biederman 	.llseek		= seq_lseek,
299622d917d8SEric W. Biederman 	.release	= proc_id_map_release,
299722d917d8SEric W. Biederman };
299822d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
299922d917d8SEric W. Biederman 
300047830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
300147830723SKees Cook 				struct pid *pid, struct task_struct *task)
300247830723SKees Cook {
3003a9712bc1SAl Viro 	int err = lock_trace(task);
3004a9712bc1SAl Viro 	if (!err) {
300547830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
3006a9712bc1SAl Viro 		unlock_trace(task);
3007a9712bc1SAl Viro 	}
3008a9712bc1SAl Viro 	return err;
300947830723SKees Cook }
301047830723SKees Cook 
3011801199ceSEric W. Biederman /*
301228a6d671SEric W. Biederman  * Thread groups
301328a6d671SEric W. Biederman  */
301400977a59SArjan van de Ven static const struct file_operations proc_task_operations;
3015c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
301620cdc894SEric W. Biederman 
3017c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
3018631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3019631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3020640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
3021640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3022640708a2SPavel Emelyanov #endif
3023631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
30246b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3025b2211a36SAndrew Morton #ifdef CONFIG_NET
3026631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3027b2211a36SAndrew Morton #endif
3028631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
3029631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
3030631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
3031a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
30323036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
303343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3034631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
303543ae34cbSIngo Molnar #endif
30365091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
30375091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
30385091faa4SMike Galbraith #endif
30394614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3040ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3041a9712bc1SAl Viro 	INF("syscall",    S_IRUGO, proc_pid_syscall),
3042ebcb6734SRoland McGrath #endif
3043631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
3044631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
3045631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
3046b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
304728a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3048b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
304928a6d671SEric W. Biederman #endif
3050631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3051631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
3052631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
3053631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
3054631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
3055631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3056631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
30571e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3058631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3059b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
3060ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
306128a6d671SEric W. Biederman #endif
306228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3063631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
306428a6d671SEric W. Biederman #endif
306528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3066631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
306728a6d671SEric W. Biederman #endif
30682ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3069a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
307028a6d671SEric W. Biederman #endif
307128a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3072631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
307328a6d671SEric W. Biederman #endif
30749745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3075631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30769745512cSArjan van de Ven #endif
30778793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3078631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
307928a6d671SEric W. Biederman #endif
3080a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3081631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3082a424316cSPaul Menage #endif
3083631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
3084631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3085a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
308628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3087631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3088631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
308928a6d671SEric W. Biederman #endif
3090f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3091631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3092f4f154fdSAkinobu Mita #endif
3093698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
3094631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
30953cb4a0bbSKawai, Hidehiro #endif
3096aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
30971d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
3098aba76fdbSAndrew Morton #endif
3099f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3100f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3101f133eccaSChris Metcalf #endif
310222d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
310322d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
310422d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
310522d917d8SEric W. Biederman #endif
310628a6d671SEric W. Biederman };
310728a6d671SEric W. Biederman 
310828a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
310928a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
311028a6d671SEric W. Biederman {
311128a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
311228a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
311328a6d671SEric W. Biederman }
311428a6d671SEric W. Biederman 
311500977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
311628a6d671SEric W. Biederman 	.read		= generic_read_dir,
311728a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
31186038f373SArnd Bergmann 	.llseek		= default_llseek,
311928a6d671SEric W. Biederman };
312028a6d671SEric W. Biederman 
312100cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
312200cd8dd3SAl Viro {
31237bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
31247bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
312528a6d671SEric W. Biederman }
312628a6d671SEric W. Biederman 
3127c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
312828a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
312928a6d671SEric W. Biederman 	.getattr	= pid_getattr,
313028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
31310499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
313228a6d671SEric W. Biederman };
313328a6d671SEric W. Biederman 
313460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
31351da177e4SLinus Torvalds {
313648e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
31378578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
313848e6484dSEric W. Biederman 	struct qstr name;
31391da177e4SLinus Torvalds 
314048e6484dSEric W. Biederman 	name.name = buf;
314160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
314260347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
314348e6484dSEric W. Biederman 	if (dentry) {
314448e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
314548e6484dSEric W. Biederman 		d_drop(dentry);
314648e6484dSEric W. Biederman 		dput(dentry);
31471da177e4SLinus Torvalds 	}
31481da177e4SLinus Torvalds 
314948e6484dSEric W. Biederman 	name.name = buf;
315060347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
315160347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
315248e6484dSEric W. Biederman 	if (!leader)
315348e6484dSEric W. Biederman 		goto out;
315448e6484dSEric W. Biederman 
315548e6484dSEric W. Biederman 	name.name = "task";
315648e6484dSEric W. Biederman 	name.len = strlen(name.name);
315748e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
315848e6484dSEric W. Biederman 	if (!dir)
315948e6484dSEric W. Biederman 		goto out_put_leader;
316048e6484dSEric W. Biederman 
316148e6484dSEric W. Biederman 	name.name = buf;
316260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
316348e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
316448e6484dSEric W. Biederman 	if (dentry) {
316548e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
316648e6484dSEric W. Biederman 		d_drop(dentry);
316748e6484dSEric W. Biederman 		dput(dentry);
31681da177e4SLinus Torvalds 	}
316948e6484dSEric W. Biederman 
317048e6484dSEric W. Biederman 	dput(dir);
317148e6484dSEric W. Biederman out_put_leader:
317248e6484dSEric W. Biederman 	dput(leader);
317348e6484dSEric W. Biederman out:
317448e6484dSEric W. Biederman 	return;
31751da177e4SLinus Torvalds }
31761da177e4SLinus Torvalds 
31770895e91dSRandy Dunlap /**
31780895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
31790895e91dSRandy Dunlap  * @task: task that should be flushed.
31800895e91dSRandy Dunlap  *
31810895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
318260347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
31830895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
31840895e91dSRandy Dunlap  *
31850895e91dSRandy Dunlap  * Looks in the dcache for
31860895e91dSRandy Dunlap  * /proc/@pid
31870895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
31880895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
31890895e91dSRandy Dunlap  * from the dcache.
31900895e91dSRandy Dunlap  *
31910895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
31920895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
31930895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
31940895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
31950895e91dSRandy Dunlap  * dcache entries at process exit time.
31960895e91dSRandy Dunlap  *
31970895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
31980895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
31990895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
320060347f67SPavel Emelyanov  */
320160347f67SPavel Emelyanov 
320260347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
320360347f67SPavel Emelyanov {
32049fcc2d15SEric W. Biederman 	int i;
32059b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
3206130f77ecSPavel Emelyanov 	struct upid *upid;
3207130f77ecSPavel Emelyanov 
3208130f77ecSPavel Emelyanov 	pid = task_pid(task);
3209130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
32109fcc2d15SEric W. Biederman 
32119fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
3212130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
3213130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
32149b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
3215130f77ecSPavel Emelyanov 	}
32166f4e6433SPavel Emelyanov 
32176f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
32186f4e6433SPavel Emelyanov 	if (upid->nr == 1)
32196f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
322060347f67SPavel Emelyanov }
322160347f67SPavel Emelyanov 
32229711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
32239711ef99SAdrian Bunk 					   struct dentry * dentry,
3224c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
3225444ceed8SEric W. Biederman {
3226444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3227444ceed8SEric W. Biederman 	struct inode *inode;
3228444ceed8SEric W. Biederman 
322961a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3230444ceed8SEric W. Biederman 	if (!inode)
3231444ceed8SEric W. Biederman 		goto out;
3232444ceed8SEric W. Biederman 
3233444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3234444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
3235444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
3236444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3237aed54175SVegard Nossum 
3238bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3239bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
3240444ceed8SEric W. Biederman 
3241fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3242444ceed8SEric W. Biederman 
3243444ceed8SEric W. Biederman 	d_add(dentry, inode);
3244444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
32450b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3246444ceed8SEric W. Biederman 		error = NULL;
3247444ceed8SEric W. Biederman out:
3248444ceed8SEric W. Biederman 	return error;
3249444ceed8SEric W. Biederman }
3250444ceed8SEric W. Biederman 
325100cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
32521da177e4SLinus Torvalds {
325373d36460SDan Carpenter 	struct dentry *result;
32541da177e4SLinus Torvalds 	struct task_struct *task;
32551da177e4SLinus Torvalds 	unsigned tgid;
3256b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32571da177e4SLinus Torvalds 
3258801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
3259801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3260801199ceSEric W. Biederman 		goto out;
3261801199ceSEric W. Biederman 
32621da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
32631da177e4SLinus Torvalds 	if (tgid == ~0U)
32641da177e4SLinus Torvalds 		goto out;
32651da177e4SLinus Torvalds 
3266b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3267de758734SEric W. Biederman 	rcu_read_lock();
3268b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
32691da177e4SLinus Torvalds 	if (task)
32701da177e4SLinus Torvalds 		get_task_struct(task);
3271de758734SEric W. Biederman 	rcu_read_unlock();
32721da177e4SLinus Torvalds 	if (!task)
32731da177e4SLinus Torvalds 		goto out;
32741da177e4SLinus Torvalds 
3275444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
327648e6484dSEric W. Biederman 	put_task_struct(task);
32771da177e4SLinus Torvalds out:
3278cd6a3ce9SEric W. Biederman 	return result;
32791da177e4SLinus Torvalds }
32801da177e4SLinus Torvalds 
32811da177e4SLinus Torvalds /*
32820804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
32830bc58a91SEric W. Biederman  *
32841da177e4SLinus Torvalds  */
328519fd4bb2SEric W. Biederman struct tgid_iter {
328619fd4bb2SEric W. Biederman 	unsigned int tgid;
32870804ef4bSEric W. Biederman 	struct task_struct *task;
328819fd4bb2SEric W. Biederman };
328919fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
329019fd4bb2SEric W. Biederman {
32910804ef4bSEric W. Biederman 	struct pid *pid;
32921da177e4SLinus Torvalds 
329319fd4bb2SEric W. Biederman 	if (iter.task)
329419fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
32950804ef4bSEric W. Biederman 	rcu_read_lock();
32960804ef4bSEric W. Biederman retry:
329719fd4bb2SEric W. Biederman 	iter.task = NULL;
329819fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
32990804ef4bSEric W. Biederman 	if (pid) {
330019fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
330119fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
33020804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
33030804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
33040804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
33050804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
33060804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
33070804ef4bSEric W. Biederman 		 *
33080804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
33090804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
33100804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
33110804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
33120804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
33130bc58a91SEric W. Biederman 		 */
331419fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
331519fd4bb2SEric W. Biederman 			iter.tgid += 1;
33160804ef4bSEric W. Biederman 			goto retry;
331719fd4bb2SEric W. Biederman 		}
331819fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
33191da177e4SLinus Torvalds 	}
3320454cc105SEric W. Biederman 	rcu_read_unlock();
332119fd4bb2SEric W. Biederman 	return iter;
33221da177e4SLinus Torvalds }
33231da177e4SLinus Torvalds 
33247bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
33251da177e4SLinus Torvalds 
332661a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
332719fd4bb2SEric W. Biederman 	struct tgid_iter iter)
332861a28784SEric W. Biederman {
332961a28784SEric W. Biederman 	char name[PROC_NUMBUF];
333019fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
333161a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
333219fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
333361a28784SEric W. Biederman }
333461a28784SEric W. Biederman 
33350499680aSVasiliy Kulikov static int fake_filldir(void *buf, const char *name, int namelen,
33360499680aSVasiliy Kulikov 			loff_t offset, u64 ino, unsigned d_type)
33370499680aSVasiliy Kulikov {
33380499680aSVasiliy Kulikov 	return 0;
33390499680aSVasiliy Kulikov }
33400499680aSVasiliy Kulikov 
33411da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
33421da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
33431da177e4SLinus Torvalds {
3344d8bdc59fSLinus Torvalds 	unsigned int nr;
3345d8bdc59fSLinus Torvalds 	struct task_struct *reaper;
334619fd4bb2SEric W. Biederman 	struct tgid_iter iter;
3347b488893aSPavel Emelyanov 	struct pid_namespace *ns;
33480499680aSVasiliy Kulikov 	filldir_t __filldir;
33491da177e4SLinus Torvalds 
3350d8bdc59fSLinus Torvalds 	if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3351d8bdc59fSLinus Torvalds 		goto out_no_task;
3352d8bdc59fSLinus Torvalds 	nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3353d8bdc59fSLinus Torvalds 
3354d8bdc59fSLinus Torvalds 	reaper = get_proc_task(filp->f_path.dentry->d_inode);
335561a28784SEric W. Biederman 	if (!reaper)
335661a28784SEric W. Biederman 		goto out_no_task;
335761a28784SEric W. Biederman 
33587bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3359c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
336061a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3361801199ceSEric W. Biederman 			goto out;
33621da177e4SLinus Torvalds 	}
33631da177e4SLinus Torvalds 
3364b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
336519fd4bb2SEric W. Biederman 	iter.task = NULL;
336619fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
336719fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
336819fd4bb2SEric W. Biederman 	     iter.task;
336919fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
33700499680aSVasiliy Kulikov 		if (has_pid_permissions(ns, iter.task, 2))
33710499680aSVasiliy Kulikov 			__filldir = filldir;
33720499680aSVasiliy Kulikov 		else
33730499680aSVasiliy Kulikov 			__filldir = fake_filldir;
33740499680aSVasiliy Kulikov 
337519fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
33760499680aSVasiliy Kulikov 		if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
337719fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
33780804ef4bSEric W. Biederman 			goto out;
33791da177e4SLinus Torvalds 		}
33801da177e4SLinus Torvalds 	}
33810804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
33820804ef4bSEric W. Biederman out:
338361a28784SEric W. Biederman 	put_task_struct(reaper);
338461a28784SEric W. Biederman out_no_task:
33851da177e4SLinus Torvalds 	return 0;
33861da177e4SLinus Torvalds }
33871da177e4SLinus Torvalds 
33880bc58a91SEric W. Biederman /*
338928a6d671SEric W. Biederman  * Tasks
339028a6d671SEric W. Biederman  */
3391c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3392631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
33933835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
33946b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3395631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3396631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3397631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3398a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
33993036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
340043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3401631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
340243ae34cbSIngo Molnar #endif
34034614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3404ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3405a9712bc1SAl Viro 	INF("syscall",   S_IRUGO, proc_pid_syscall),
3406ebcb6734SRoland McGrath #endif
3407631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3408631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3409631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3410b7643757SSiddhesh Poyarekar 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
341181841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
341281841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
341381841161SCyrill Gorcunov #endif
341428a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3415b7643757SSiddhesh Poyarekar 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
341628a6d671SEric W. Biederman #endif
3417631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3418631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3419631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3420631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3421631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3422631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
34231e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3424631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3425b7643757SSiddhesh Poyarekar 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
3426ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
342728a6d671SEric W. Biederman #endif
342828a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3429631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
343028a6d671SEric W. Biederman #endif
343128a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3432631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
343328a6d671SEric W. Biederman #endif
34342ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3435a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
343628a6d671SEric W. Biederman #endif
343728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3438631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
343928a6d671SEric W. Biederman #endif
34409745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3441631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
34429745512cSArjan van de Ven #endif
34438793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3444631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
344528a6d671SEric W. Biederman #endif
3446a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3447631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3448a424316cSPaul Menage #endif
3449631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3450631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3451a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
345228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3453631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
345426ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
345528a6d671SEric W. Biederman #endif
3456f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3457631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3458f4f154fdSAkinobu Mita #endif
3459297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
34601d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
3461297c5d92SAndrea Righi #endif
3462f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3463f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3464f133eccaSChris Metcalf #endif
346522d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
346622d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
346722d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
346822d917d8SEric W. Biederman #endif
346928a6d671SEric W. Biederman };
347028a6d671SEric W. Biederman 
347128a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
347228a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
347328a6d671SEric W. Biederman {
347428a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
347528a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
347628a6d671SEric W. Biederman }
347728a6d671SEric W. Biederman 
347800cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
347900cd8dd3SAl Viro {
34807bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
34817bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
348228a6d671SEric W. Biederman }
348328a6d671SEric W. Biederman 
348400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
348528a6d671SEric W. Biederman 	.read		= generic_read_dir,
348628a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
34876038f373SArnd Bergmann 	.llseek		= default_llseek,
348828a6d671SEric W. Biederman };
348928a6d671SEric W. Biederman 
3490c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
349128a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
349228a6d671SEric W. Biederman 	.getattr	= pid_getattr,
349328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
349428a6d671SEric W. Biederman };
349528a6d671SEric W. Biederman 
3496444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3497c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3498444ceed8SEric W. Biederman {
3499444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3500444ceed8SEric W. Biederman 	struct inode *inode;
350161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3502444ceed8SEric W. Biederman 
3503444ceed8SEric W. Biederman 	if (!inode)
3504444ceed8SEric W. Biederman 		goto out;
3505444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3506444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3507444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3508444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3509aed54175SVegard Nossum 
3510bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3511bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3512444ceed8SEric W. Biederman 
3513fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3514444ceed8SEric W. Biederman 
3515444ceed8SEric W. Biederman 	d_add(dentry, inode);
3516444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
35170b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3518444ceed8SEric W. Biederman 		error = NULL;
3519444ceed8SEric W. Biederman out:
3520444ceed8SEric W. Biederman 	return error;
3521444ceed8SEric W. Biederman }
3522444ceed8SEric W. Biederman 
352300cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
352428a6d671SEric W. Biederman {
352528a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
352628a6d671SEric W. Biederman 	struct task_struct *task;
352728a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
352828a6d671SEric W. Biederman 	unsigned tid;
3529b488893aSPavel Emelyanov 	struct pid_namespace *ns;
353028a6d671SEric W. Biederman 
353128a6d671SEric W. Biederman 	if (!leader)
353228a6d671SEric W. Biederman 		goto out_no_task;
353328a6d671SEric W. Biederman 
353428a6d671SEric W. Biederman 	tid = name_to_int(dentry);
353528a6d671SEric W. Biederman 	if (tid == ~0U)
353628a6d671SEric W. Biederman 		goto out;
353728a6d671SEric W. Biederman 
3538b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
353928a6d671SEric W. Biederman 	rcu_read_lock();
3540b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
354128a6d671SEric W. Biederman 	if (task)
354228a6d671SEric W. Biederman 		get_task_struct(task);
354328a6d671SEric W. Biederman 	rcu_read_unlock();
354428a6d671SEric W. Biederman 	if (!task)
354528a6d671SEric W. Biederman 		goto out;
3546bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
354728a6d671SEric W. Biederman 		goto out_drop_task;
354828a6d671SEric W. Biederman 
3549444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
355028a6d671SEric W. Biederman out_drop_task:
355128a6d671SEric W. Biederman 	put_task_struct(task);
355228a6d671SEric W. Biederman out:
355328a6d671SEric W. Biederman 	put_task_struct(leader);
355428a6d671SEric W. Biederman out_no_task:
355528a6d671SEric W. Biederman 	return result;
355628a6d671SEric W. Biederman }
355728a6d671SEric W. Biederman 
355828a6d671SEric W. Biederman /*
35590bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
35600bc58a91SEric W. Biederman  *
35610bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
35620bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
35630bc58a91SEric W. Biederman  * directory we have more work todo.
35640bc58a91SEric W. Biederman  *
35650bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
35660bc58a91SEric W. Biederman  *
35670bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
35680bc58a91SEric W. Biederman  * threads past it.
35690bc58a91SEric W. Biederman  */
3570cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3571b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
35720bc58a91SEric W. Biederman {
3573a872ff0cSOleg Nesterov 	struct task_struct *pos;
35740bc58a91SEric W. Biederman 
3575cc288738SEric W. Biederman 	rcu_read_lock();
35760bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
35770bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3578b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3579a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3580a872ff0cSOleg Nesterov 			goto found;
35810bc58a91SEric W. Biederman 	}
35820bc58a91SEric W. Biederman 
35830bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
35840bc58a91SEric W. Biederman 	pos = NULL;
3585a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3586a872ff0cSOleg Nesterov 		goto out;
3587a872ff0cSOleg Nesterov 
3588a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3589a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3590a872ff0cSOleg Nesterov 	 */
3591a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3592a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3593a872ff0cSOleg Nesterov 		if (pos == leader) {
3594a872ff0cSOleg Nesterov 			pos = NULL;
3595a872ff0cSOleg Nesterov 			goto out;
3596a872ff0cSOleg Nesterov 		}
3597a872ff0cSOleg Nesterov 	}
3598a872ff0cSOleg Nesterov found:
3599a872ff0cSOleg Nesterov 	get_task_struct(pos);
3600a872ff0cSOleg Nesterov out:
3601cc288738SEric W. Biederman 	rcu_read_unlock();
36020bc58a91SEric W. Biederman 	return pos;
36030bc58a91SEric W. Biederman }
36040bc58a91SEric W. Biederman 
36050bc58a91SEric W. Biederman /*
36060bc58a91SEric W. Biederman  * Find the next thread in the thread list.
36070bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
36080bc58a91SEric W. Biederman  *
36090bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
36100bc58a91SEric W. Biederman  */
36110bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
36120bc58a91SEric W. Biederman {
3613c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3614cc288738SEric W. Biederman 	rcu_read_lock();
3615c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
36160bc58a91SEric W. Biederman 		pos = next_thread(start);
3617c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
36180bc58a91SEric W. Biederman 			pos = NULL;
3619c1df7fb8SOleg Nesterov 		else
3620c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3621c1df7fb8SOleg Nesterov 	}
3622cc288738SEric W. Biederman 	rcu_read_unlock();
36230bc58a91SEric W. Biederman 	put_task_struct(start);
36240bc58a91SEric W. Biederman 	return pos;
36250bc58a91SEric W. Biederman }
36260bc58a91SEric W. Biederman 
362761a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
362861a28784SEric W. Biederman 	struct task_struct *task, int tid)
362961a28784SEric W. Biederman {
363061a28784SEric W. Biederman 	char name[PROC_NUMBUF];
363161a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
363261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
363361a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
363461a28784SEric W. Biederman }
363561a28784SEric W. Biederman 
36361da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
36371da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
36381da177e4SLinus Torvalds {
36392fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
36401da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
36417d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
36420bc58a91SEric W. Biederman 	struct task_struct *task;
36431da177e4SLinus Torvalds 	int retval = -ENOENT;
36441da177e4SLinus Torvalds 	ino_t ino;
36450bc58a91SEric W. Biederman 	int tid;
3646b488893aSPavel Emelyanov 	struct pid_namespace *ns;
36471da177e4SLinus Torvalds 
36487d895244SGuillaume Chazarain 	task = get_proc_task(inode);
36497d895244SGuillaume Chazarain 	if (!task)
36507d895244SGuillaume Chazarain 		goto out_no_task;
36517d895244SGuillaume Chazarain 	rcu_read_lock();
36527d895244SGuillaume Chazarain 	if (pid_alive(task)) {
36537d895244SGuillaume Chazarain 		leader = task->group_leader;
36547d895244SGuillaume Chazarain 		get_task_struct(leader);
36557d895244SGuillaume Chazarain 	}
36567d895244SGuillaume Chazarain 	rcu_read_unlock();
36577d895244SGuillaume Chazarain 	put_task_struct(task);
365899f89551SEric W. Biederman 	if (!leader)
365999f89551SEric W. Biederman 		goto out_no_task;
36601da177e4SLinus Torvalds 	retval = 0;
36611da177e4SLinus Torvalds 
3662ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
36631da177e4SLinus Torvalds 	case 0:
36641da177e4SLinus Torvalds 		ino = inode->i_ino;
3665ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
36661da177e4SLinus Torvalds 			goto out;
3667ee6f779bSZhang Le 		filp->f_pos++;
36681da177e4SLinus Torvalds 		/* fall through */
36691da177e4SLinus Torvalds 	case 1:
36701da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3671ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
36721da177e4SLinus Torvalds 			goto out;
3673ee6f779bSZhang Le 		filp->f_pos++;
36741da177e4SLinus Torvalds 		/* fall through */
36751da177e4SLinus Torvalds 	}
36761da177e4SLinus Torvalds 
36770bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
36780bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
36790bc58a91SEric W. Biederman 	 */
3680b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
36812b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
36820bc58a91SEric W. Biederman 	filp->f_version = 0;
3683ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
36840bc58a91SEric W. Biederman 	     task;
3685ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3686b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
368761a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
36880bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
36890bc58a91SEric W. Biederman 			 * pid for the next readir call */
36902b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
36910bc58a91SEric W. Biederman 			put_task_struct(task);
36921da177e4SLinus Torvalds 			break;
36930bc58a91SEric W. Biederman 		}
36941da177e4SLinus Torvalds 	}
36951da177e4SLinus Torvalds out:
369699f89551SEric W. Biederman 	put_task_struct(leader);
369799f89551SEric W. Biederman out_no_task:
36981da177e4SLinus Torvalds 	return retval;
36991da177e4SLinus Torvalds }
37006e66b52bSEric W. Biederman 
37016e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
37026e66b52bSEric W. Biederman {
37036e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
370499f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
37056e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
37066e66b52bSEric W. Biederman 
370799f89551SEric W. Biederman 	if (p) {
370899f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
370999f89551SEric W. Biederman 		put_task_struct(p);
37106e66b52bSEric W. Biederman 	}
37116e66b52bSEric W. Biederman 
37126e66b52bSEric W. Biederman 	return 0;
37136e66b52bSEric W. Biederman }
371428a6d671SEric W. Biederman 
3715c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
371628a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
371728a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
371828a6d671SEric W. Biederman 	.setattr	= proc_setattr,
37190499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
372028a6d671SEric W. Biederman };
372128a6d671SEric W. Biederman 
372200977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
372328a6d671SEric W. Biederman 	.read		= generic_read_dir,
372428a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
37256038f373SArnd Bergmann 	.llseek		= default_llseek,
372628a6d671SEric W. Biederman };
3727