xref: /openbmc/linux/fs/proc/base.c (revision 3d712546d8ba9f25cdf080d79f90482aa4231ed4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/proc/base.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992 Linus Torvalds
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  proc base directory handling functions
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
101da177e4SLinus Torvalds  *  Instead of using magical inumbers to determine the kind of object
111da177e4SLinus Torvalds  *  we allocate and fill in-core inodes upon lookup. They don't even
121da177e4SLinus Torvalds  *  go into icache. We cache the reference to task_struct upon lookup too.
131da177e4SLinus Torvalds  *  Eventually it should become a filesystem in its own. We don't use the
141da177e4SLinus Torvalds  *  rest of procfs anymore.
15e070ad49SMauricio Lin  *
16e070ad49SMauricio Lin  *
17e070ad49SMauricio Lin  *  Changelog:
18e070ad49SMauricio Lin  *  17-Jan-2005
19e070ad49SMauricio Lin  *  Allan Bezerra
20e070ad49SMauricio Lin  *  Bruna Moreira <bruna.moreira@indt.org.br>
21e070ad49SMauricio Lin  *  Edjard Mota <edjard.mota@indt.org.br>
22e070ad49SMauricio Lin  *  Ilias Biris <ilias.biris@indt.org.br>
23e070ad49SMauricio Lin  *  Mauricio Lin <mauricio.lin@indt.org.br>
24e070ad49SMauricio Lin  *
25e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26e070ad49SMauricio Lin  *
27e070ad49SMauricio Lin  *  A new process specific entry (smaps) included in /proc. It shows the
28e070ad49SMauricio Lin  *  size of rss for each memory area. The maps entry lacks information
29e070ad49SMauricio Lin  *  about physical memory size (rss) for each mapped file, i.e.,
30e070ad49SMauricio Lin  *  rss information for executables and library files.
31e070ad49SMauricio Lin  *  This additional information is useful for any tools that need to know
32e070ad49SMauricio Lin  *  about physical memory consumption for a process specific library.
33e070ad49SMauricio Lin  *
34e070ad49SMauricio Lin  *  Changelog:
35e070ad49SMauricio Lin  *  21-Feb-2005
36e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37e070ad49SMauricio Lin  *  Pud inclusion in the page table walking.
38e070ad49SMauricio Lin  *
39e070ad49SMauricio Lin  *  ChangeLog:
40e070ad49SMauricio Lin  *  10-Mar-2005
41e070ad49SMauricio Lin  *  10LE Instituto Nokia de Tecnologia - INdT:
42e070ad49SMauricio Lin  *  A better way to walks through the page table as suggested by Hugh Dickins.
43e070ad49SMauricio Lin  *
44e070ad49SMauricio Lin  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
45e070ad49SMauricio Lin  *  Smaps information related to shared, private, clean and dirty pages.
46e070ad49SMauricio Lin  *
47e070ad49SMauricio Lin  *  Paul Mundt <paul.mundt@nokia.com>:
48e070ad49SMauricio Lin  *  Overall revision about smaps.
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds 
517c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include <linux/errno.h>
541da177e4SLinus Torvalds #include <linux/time.h>
551da177e4SLinus Torvalds #include <linux/proc_fs.h>
561da177e4SLinus Torvalds #include <linux/stat.h>
575995477aSAndrea Righi #include <linux/task_io_accounting_ops.h>
581da177e4SLinus Torvalds #include <linux/init.h>
5916f7e0feSRandy Dunlap #include <linux/capability.h>
601da177e4SLinus Torvalds #include <linux/file.h>
619f3acc31SAl Viro #include <linux/fdtable.h>
6294f8f3b0SKent Overstreet #include <linux/generic-radix-tree.h>
631da177e4SLinus Torvalds #include <linux/string.h>
641da177e4SLinus Torvalds #include <linux/seq_file.h>
651da177e4SLinus Torvalds #include <linux/namei.h>
666b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
671da177e4SLinus Torvalds #include <linux/mm.h>
68a63d83f4SDavid Rientjes #include <linux/swap.h>
69b835996fSDipankar Sarma #include <linux/rcupdate.h>
701da177e4SLinus Torvalds #include <linux/kallsyms.h>
712ec220e2SKen Chen #include <linux/stacktrace.h>
72d85f50d5SNeil Horman #include <linux/resource.h>
735096add8SKees Cook #include <linux/module.h>
741da177e4SLinus Torvalds #include <linux/mount.h>
751da177e4SLinus Torvalds #include <linux/security.h>
761da177e4SLinus Torvalds #include <linux/ptrace.h>
770d094efeSRoland McGrath #include <linux/tracehook.h>
7887ebdc00SAndrew Morton #include <linux/printk.h>
79efb1a57dSAlexey Dobriyan #include <linux/cache.h>
80a424316cSPaul Menage #include <linux/cgroup.h>
811da177e4SLinus Torvalds #include <linux/cpuset.h>
821da177e4SLinus Torvalds #include <linux/audit.h>
835addc5ddSAl Viro #include <linux/poll.h>
841651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
858ac773b4SAlexey Dobriyan #include <linux/oom.h>
863cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8760347f67SPavel Emelyanov #include <linux/pid_namespace.h>
8822d917d8SEric W. Biederman #include <linux/user_namespace.h>
895ad4e53bSAl Viro #include <linux/fs_struct.h>
905a0e3ad6STejun Heo #include <linux/slab.h>
914eb5aaa3SIngo Molnar #include <linux/sched/autogroup.h>
926e84f315SIngo Molnar #include <linux/sched/mm.h>
93f7ccbae4SIngo Molnar #include <linux/sched/coredump.h>
94b17b0153SIngo Molnar #include <linux/sched/debug.h>
953905f9adSIngo Molnar #include <linux/sched/stat.h>
9648f6a7a5SPavel Emelyanov #include <linux/posix-timers.h>
9743d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
981da177e4SLinus Torvalds #include "internal.h"
99faf60af1SCyrill Gorcunov #include "fd.h"
1001da177e4SLinus Torvalds 
101ac7f1061SAlexey Dobriyan #include "../../lib/kstrtox.h"
102ac7f1061SAlexey Dobriyan 
1030f2fe20fSEric W. Biederman /* NOTE:
1040f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
1050f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
1060f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
1070f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
1080f2fe20fSEric W. Biederman  *
1090f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
1100f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
1110f2fe20fSEric W. Biederman  */
1120f2fe20fSEric W. Biederman 
113efb1a57dSAlexey Dobriyan static u8 nlink_tid __ro_after_init;
114efb1a57dSAlexey Dobriyan static u8 nlink_tgid __ro_after_init;
1151270dd8dSAlexey Dobriyan 
1161da177e4SLinus Torvalds struct pid_entry {
117cedbccabSAlexey Dobriyan 	const char *name;
118623f594eSAlexey Dobriyan 	unsigned int len;
119d161a13fSAl Viro 	umode_t mode;
120c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
12100977a59SArjan van de Ven 	const struct file_operations *fop;
12220cdc894SEric W. Biederman 	union proc_op op;
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
12561a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
12620cdc894SEric W. Biederman 	.name = (NAME),					\
127c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
12820cdc894SEric W. Biederman 	.mode = MODE,					\
12920cdc894SEric W. Biederman 	.iop  = IOP,					\
13020cdc894SEric W. Biederman 	.fop  = FOP,					\
13120cdc894SEric W. Biederman 	.op   = OP,					\
13220cdc894SEric W. Biederman }
13320cdc894SEric W. Biederman 
134631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
135631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
13761a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
13820cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
139631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
140631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
141631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
143be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)),			\
144be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
145631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1466d9c939dSCasey Schaufler #define ATTR(LSM, NAME, MODE)				\
1476d9c939dSCasey Schaufler 	NOD(NAME, (S_IFREG|(MODE)),			\
1486d9c939dSCasey Schaufler 		NULL, &proc_pid_attr_operations,	\
1496d9c939dSCasey Schaufler 		{ .lsm = LSM })
1501da177e4SLinus Torvalds 
151aed54175SVegard Nossum /*
152aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
153aed54175SVegard Nossum  * and .. links.
154aed54175SVegard Nossum  */
1551270dd8dSAlexey Dobriyan static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
156aed54175SVegard Nossum 	unsigned int n)
157aed54175SVegard Nossum {
158aed54175SVegard Nossum 	unsigned int i;
159aed54175SVegard Nossum 	unsigned int count;
160aed54175SVegard Nossum 
1611270dd8dSAlexey Dobriyan 	count = 2;
162aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
163aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
164aed54175SVegard Nossum 			++count;
165aed54175SVegard Nossum 	}
166aed54175SVegard Nossum 
167aed54175SVegard Nossum 	return count;
168aed54175SVegard Nossum }
169aed54175SVegard Nossum 
170f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1711da177e4SLinus Torvalds {
1727c2c7d99SHugh Dickins 	int result = -ENOENT;
1737c2c7d99SHugh Dickins 
1740494f6ecSMiklos Szeredi 	task_lock(task);
175f7ad3c6bSMiklos Szeredi 	if (task->fs) {
176f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1777c2c7d99SHugh Dickins 		result = 0;
1787c2c7d99SHugh Dickins 	}
1790494f6ecSMiklos Szeredi 	task_unlock(task);
1807c2c7d99SHugh Dickins 	return result;
1810494f6ecSMiklos Szeredi }
1820494f6ecSMiklos Szeredi 
1837773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1840494f6ecSMiklos Szeredi {
1852b0143b5SDavid Howells 	struct task_struct *task = get_proc_task(d_inode(dentry));
1860494f6ecSMiklos Szeredi 	int result = -ENOENT;
18799f89551SEric W. Biederman 
18899f89551SEric W. Biederman 	if (task) {
189f7ad3c6bSMiklos Szeredi 		task_lock(task);
190f7ad3c6bSMiklos Szeredi 		if (task->fs) {
191f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
192f7ad3c6bSMiklos Szeredi 			result = 0;
193f7ad3c6bSMiklos Szeredi 		}
194f7ad3c6bSMiklos Szeredi 		task_unlock(task);
19599f89551SEric W. Biederman 		put_task_struct(task);
19699f89551SEric W. Biederman 	}
1971da177e4SLinus Torvalds 	return result;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2007773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
2011da177e4SLinus Torvalds {
2022b0143b5SDavid Howells 	struct task_struct *task = get_proc_task(d_inode(dentry));
2031da177e4SLinus Torvalds 	int result = -ENOENT;
20499f89551SEric W. Biederman 
20599f89551SEric W. Biederman 	if (task) {
206f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
20799f89551SEric W. Biederman 		put_task_struct(task);
20899f89551SEric W. Biederman 	}
2091da177e4SLinus Torvalds 	return result;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
212e4b4e441SLinus Torvalds static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
2135ab82718SLinus Torvalds 			      size_t count, loff_t *ppos)
2141da177e4SLinus Torvalds {
215*3d712546SLinus Torvalds 	unsigned long arg_start, arg_end;
2165ab82718SLinus Torvalds 	unsigned long pos, len;
2175ab82718SLinus Torvalds 	char *page;
218c2c0bb44SAlexey Dobriyan 
219c2c0bb44SAlexey Dobriyan 	/* Check if process spawned far enough to have cmdline. */
220e4b4e441SLinus Torvalds 	if (!mm->env_end)
221e4b4e441SLinus Torvalds 		return 0;
222c2c0bb44SAlexey Dobriyan 
22388aa7cc6SYang Shi 	spin_lock(&mm->arg_lock);
224c2c0bb44SAlexey Dobriyan 	arg_start = mm->arg_start;
225c2c0bb44SAlexey Dobriyan 	arg_end = mm->arg_end;
22688aa7cc6SYang Shi 	spin_unlock(&mm->arg_lock);
227c2c0bb44SAlexey Dobriyan 
2285ab82718SLinus Torvalds 	if (arg_start >= arg_end)
2295ab82718SLinus Torvalds 		return 0;
2306a6cbe75SAlexey Dobriyan 
2315ab82718SLinus Torvalds 	/* We're not going to care if "*ppos" has high bits set */
2325ab82718SLinus Torvalds 	/* .. but we do check the result is in the proper range */
233*3d712546SLinus Torvalds 	pos = arg_start + *ppos;
234*3d712546SLinus Torvalds 	if (pos < arg_start || pos >= arg_end)
2355ab82718SLinus Torvalds 		return 0;
236*3d712546SLinus Torvalds 	if (count > arg_end - pos)
237*3d712546SLinus Torvalds 		count = arg_end - pos;
2383cb4e162SAlexey Dobriyan 
2395ab82718SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
2405ab82718SLinus Torvalds 	if (!page)
2415ab82718SLinus Torvalds 		return -ENOMEM;
242c2c0bb44SAlexey Dobriyan 
2435ab82718SLinus Torvalds 	len = 0;
2445ab82718SLinus Torvalds 	while (count) {
2455ab82718SLinus Torvalds 		int got;
2465ab82718SLinus Torvalds 		size_t size = min_t(size_t, PAGE_SIZE, count);
2475ab82718SLinus Torvalds 
248*3d712546SLinus Torvalds 		got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
249*3d712546SLinus Torvalds 		if (got <= 0)
2505ab82718SLinus Torvalds 			break;
251*3d712546SLinus Torvalds 		got -= copy_to_user(buf, page, got);
2525ab82718SLinus Torvalds 		if (unlikely(!got)) {
2535ab82718SLinus Torvalds 			if (!len)
2545ab82718SLinus Torvalds 				len = -EFAULT;
2555ab82718SLinus Torvalds 			break;
2565ab82718SLinus Torvalds 		}
2575ab82718SLinus Torvalds 		pos += got;
2585ab82718SLinus Torvalds 		buf += got;
2595ab82718SLinus Torvalds 		len += got;
2605ab82718SLinus Torvalds 		count -= got;
261c2c0bb44SAlexey Dobriyan 	}
262a0a07b87SAlexey Dobriyan 
263c2c0bb44SAlexey Dobriyan 	free_page((unsigned long)page);
2645ab82718SLinus Torvalds 	return len;
265c2c0bb44SAlexey Dobriyan }
266c2c0bb44SAlexey Dobriyan 
267e4b4e441SLinus Torvalds static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
268e4b4e441SLinus Torvalds 				size_t count, loff_t *pos)
269e4b4e441SLinus Torvalds {
270e4b4e441SLinus Torvalds 	struct mm_struct *mm;
271e4b4e441SLinus Torvalds 	ssize_t ret;
272e4b4e441SLinus Torvalds 
273e4b4e441SLinus Torvalds 	mm = get_task_mm(tsk);
274e4b4e441SLinus Torvalds 	if (!mm)
275e4b4e441SLinus Torvalds 		return 0;
276e4b4e441SLinus Torvalds 
277e4b4e441SLinus Torvalds 	ret = get_mm_cmdline(mm, buf, count, pos);
278c2c0bb44SAlexey Dobriyan 	mmput(mm);
279e4b4e441SLinus Torvalds 	return ret;
280e4b4e441SLinus Torvalds }
281e4b4e441SLinus Torvalds 
282e4b4e441SLinus Torvalds static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
283e4b4e441SLinus Torvalds 				     size_t count, loff_t *pos)
284e4b4e441SLinus Torvalds {
285e4b4e441SLinus Torvalds 	struct task_struct *tsk;
286e4b4e441SLinus Torvalds 	ssize_t ret;
287e4b4e441SLinus Torvalds 
288e4b4e441SLinus Torvalds 	BUG_ON(*pos < 0);
289e4b4e441SLinus Torvalds 
290e4b4e441SLinus Torvalds 	tsk = get_proc_task(file_inode(file));
291e4b4e441SLinus Torvalds 	if (!tsk)
292e4b4e441SLinus Torvalds 		return -ESRCH;
293e4b4e441SLinus Torvalds 	ret = get_task_cmdline(tsk, buf, count, pos);
294e4b4e441SLinus Torvalds 	put_task_struct(tsk);
295e4b4e441SLinus Torvalds 	if (ret > 0)
296e4b4e441SLinus Torvalds 		*pos += ret;
297e4b4e441SLinus Torvalds 	return ret;
298c2c0bb44SAlexey Dobriyan }
299c2c0bb44SAlexey Dobriyan 
300c2c0bb44SAlexey Dobriyan static const struct file_operations proc_pid_cmdline_ops = {
301c2c0bb44SAlexey Dobriyan 	.read	= proc_pid_cmdline_read,
302c2c0bb44SAlexey Dobriyan 	.llseek	= generic_file_llseek,
303c2c0bb44SAlexey Dobriyan };
304c2c0bb44SAlexey Dobriyan 
3051da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3061da177e4SLinus Torvalds /*
3071da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3081da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3091da177e4SLinus Torvalds  */
310edfcd606SAlexey Dobriyan static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
311edfcd606SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
3121da177e4SLinus Torvalds {
313ffb45122SAlexey Dobriyan 	unsigned long wchan;
3149281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3151da177e4SLinus Torvalds 
31624b2ec21SAlexey Dobriyan 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
31724b2ec21SAlexey Dobriyan 		goto print0;
31824b2ec21SAlexey Dobriyan 
3191da177e4SLinus Torvalds 	wchan = get_wchan(task);
32024b2ec21SAlexey Dobriyan 	if (wchan && !lookup_symbol_name(wchan, symname)) {
32121dae0adSAlexey Dobriyan 		seq_puts(m, symname);
32224b2ec21SAlexey Dobriyan 		return 0;
32324b2ec21SAlexey Dobriyan 	}
32425ce3191SJoe Perches 
32524b2ec21SAlexey Dobriyan print0:
32624b2ec21SAlexey Dobriyan 	seq_putc(m, '0');
32725ce3191SJoe Perches 	return 0;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3301da177e4SLinus Torvalds 
331a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
332a9712bc1SAl Viro {
333a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
334a9712bc1SAl Viro 	if (err)
335a9712bc1SAl Viro 		return err;
336caaee623SJann Horn 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
337a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
338a9712bc1SAl Viro 		return -EPERM;
339a9712bc1SAl Viro 	}
340a9712bc1SAl Viro 	return 0;
341a9712bc1SAl Viro }
342a9712bc1SAl Viro 
343a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
344a9712bc1SAl Viro {
345a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
346a9712bc1SAl Viro }
347a9712bc1SAl Viro 
3482ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3492ec220e2SKen Chen 
3502ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3512ec220e2SKen Chen 
3522ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3532ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3542ec220e2SKen Chen {
3552ec220e2SKen Chen 	unsigned long *entries;
356a9712bc1SAl Viro 	int err;
3572ec220e2SKen Chen 
358f8a00cefSJann Horn 	/*
359f8a00cefSJann Horn 	 * The ability to racily run the kernel stack unwinder on a running task
360f8a00cefSJann Horn 	 * and then observe the unwinder output is scary; while it is useful for
361f8a00cefSJann Horn 	 * debugging kernel issues, it can also allow an attacker to leak kernel
362f8a00cefSJann Horn 	 * stack contents.
363f8a00cefSJann Horn 	 * Doing this in a manner that is at least safe from races would require
364f8a00cefSJann Horn 	 * some work to ensure that the remote task can not be scheduled; and
365f8a00cefSJann Horn 	 * even then, this would still expose the unwinder as local attack
366f8a00cefSJann Horn 	 * surface.
367f8a00cefSJann Horn 	 * Therefore, this interface is restricted to root.
368f8a00cefSJann Horn 	 */
369f8a00cefSJann Horn 	if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
370f8a00cefSJann Horn 		return -EACCES;
371f8a00cefSJann Horn 
3726da2ec56SKees Cook 	entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
3736da2ec56SKees Cook 				GFP_KERNEL);
3742ec220e2SKen Chen 	if (!entries)
3752ec220e2SKen Chen 		return -ENOMEM;
3762ec220e2SKen Chen 
377a9712bc1SAl Viro 	err = lock_trace(task);
378a9712bc1SAl Viro 	if (!err) {
379e988e5ecSThomas Gleixner 		unsigned int i, nr_entries;
3805d008fb4SAlexey Dobriyan 
381e988e5ecSThomas Gleixner 		nr_entries = stack_trace_save_tsk(task, entries,
382e988e5ecSThomas Gleixner 						  MAX_STACK_TRACE_DEPTH, 0);
3832ec220e2SKen Chen 
384e988e5ecSThomas Gleixner 		for (i = 0; i < nr_entries; i++) {
3858f5abe84SLinus Torvalds 			seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
3862ec220e2SKen Chen 		}
387e988e5ecSThomas Gleixner 
388a9712bc1SAl Viro 		unlock_trace(task);
389a9712bc1SAl Viro 	}
3902ec220e2SKen Chen 	kfree(entries);
3912ec220e2SKen Chen 
392a9712bc1SAl Viro 	return err;
3932ec220e2SKen Chen }
3942ec220e2SKen Chen #endif
3952ec220e2SKen Chen 
3965968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
3971da177e4SLinus Torvalds /*
3981da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3991da177e4SLinus Torvalds  */
400f6e826caSAlexey Dobriyan static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
401f6e826caSAlexey Dobriyan 			      struct pid *pid, struct task_struct *task)
4021da177e4SLinus Torvalds {
4035968ceceSNaveen N. Rao 	if (unlikely(!sched_info_on()))
40408b55775SAlexey Dobriyan 		seq_puts(m, "0 0 0\n");
4055968ceceSNaveen N. Rao 	else
40625ce3191SJoe Perches 		seq_printf(m, "%llu %llu %lu\n",
407826e08b0SIngo Molnar 		   (unsigned long long)task->se.sum_exec_runtime,
408826e08b0SIngo Molnar 		   (unsigned long long)task->sched_info.run_delay,
4092d72376bSIngo Molnar 		   task->sched_info.pcount);
41025ce3191SJoe Perches 
41125ce3191SJoe Perches 	return 0;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds #endif
4141da177e4SLinus Torvalds 
4159745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
4169745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
4179745512cSArjan van de Ven {
4189745512cSArjan van de Ven 	int i;
41913d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
42013d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
4219745512cSArjan van de Ven 
42213d77c37SHiroshi Shimamoto 	if (!task)
42313d77c37SHiroshi Shimamoto 		return -ESRCH;
42413d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
425f6d2f584SAlexey Dobriyan 	for (i = 0; i < LT_SAVECOUNT; i++) {
42634e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
42734e49d4fSJoe Perches 		if (lr->backtrace[0]) {
4289745512cSArjan van de Ven 			int q;
4299745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
43034e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
4319745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
43234e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
433accddc41SThomas Gleixner 
43434e49d4fSJoe Perches 				if (!bt)
4359745512cSArjan van de Ven 					break;
43634e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
4379745512cSArjan van de Ven 			}
4389d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
4399745512cSArjan van de Ven 		}
4409745512cSArjan van de Ven 
4419745512cSArjan van de Ven 	}
44213d77c37SHiroshi Shimamoto 	put_task_struct(task);
4439745512cSArjan van de Ven 	return 0;
4449745512cSArjan van de Ven }
4459745512cSArjan van de Ven 
4469745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
4479745512cSArjan van de Ven {
44813d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
449d6643d12SHiroshi Shimamoto }
450d6643d12SHiroshi Shimamoto 
4519745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4529745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4539745512cSArjan van de Ven {
454496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
4559745512cSArjan van de Ven 
45613d77c37SHiroshi Shimamoto 	if (!task)
45713d77c37SHiroshi Shimamoto 		return -ESRCH;
458e02c9b0dSLin Feng 	clear_tsk_latency_tracing(task);
45913d77c37SHiroshi Shimamoto 	put_task_struct(task);
4609745512cSArjan van de Ven 
4619745512cSArjan van de Ven 	return count;
4629745512cSArjan van de Ven }
4639745512cSArjan van de Ven 
4649745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4659745512cSArjan van de Ven 	.open		= lstats_open,
4669745512cSArjan van de Ven 	.read		= seq_read,
4679745512cSArjan van de Ven 	.write		= lstats_write,
4689745512cSArjan van de Ven 	.llseek		= seq_lseek,
46913d77c37SHiroshi Shimamoto 	.release	= single_release,
4709745512cSArjan van de Ven };
4719745512cSArjan van de Ven 
4729745512cSArjan van de Ven #endif
4739745512cSArjan van de Ven 
4746ba51e37SAlexey Dobriyan static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
4756ba51e37SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
4761da177e4SLinus Torvalds {
477ca79b0c2SArun KS 	unsigned long totalpages = totalram_pages() + total_swap_pages;
478b95c35e7SOleg Nesterov 	unsigned long points = 0;
4791da177e4SLinus Torvalds 
480a7f638f9SDavid Rientjes 	points = oom_badness(task, NULL, NULL, totalpages) *
481a7f638f9SDavid Rientjes 					1000 / totalpages;
48225ce3191SJoe Perches 	seq_printf(m, "%lu\n", points);
48325ce3191SJoe Perches 
48425ce3191SJoe Perches 	return 0;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
487d85f50d5SNeil Horman struct limit_names {
488cedbccabSAlexey Dobriyan 	const char *name;
489cedbccabSAlexey Dobriyan 	const char *unit;
490d85f50d5SNeil Horman };
491d85f50d5SNeil Horman 
492d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
493cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
494d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
495d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
496d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
497d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
498d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
499d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
500d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
501d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
502d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
503d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
504d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
505d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
506d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
507d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
5088808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
509d85f50d5SNeil Horman };
510d85f50d5SNeil Horman 
511d85f50d5SNeil Horman /* Display limits for a process */
5121c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
5131c963eb1SAlexey Dobriyan 			   struct pid *pid, struct task_struct *task)
514d85f50d5SNeil Horman {
515d85f50d5SNeil Horman 	unsigned int i;
516d85f50d5SNeil Horman 	unsigned long flags;
517d85f50d5SNeil Horman 
518d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
519d85f50d5SNeil Horman 
520a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
521d85f50d5SNeil Horman 		return 0;
522d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
523d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
524d85f50d5SNeil Horman 
525d85f50d5SNeil Horman 	/*
526d85f50d5SNeil Horman 	 * print the file header
527d85f50d5SNeil Horman 	 */
528afe922c2SAlexey Dobriyan 	seq_puts(m, "Limit                     "
529afe922c2SAlexey Dobriyan 		"Soft Limit           "
530afe922c2SAlexey Dobriyan 		"Hard Limit           "
531afe922c2SAlexey Dobriyan 		"Units     \n");
532d85f50d5SNeil Horman 
533d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
534d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
5351c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20s ",
536d85f50d5SNeil Horman 				   lnames[i].name, "unlimited");
537d85f50d5SNeil Horman 		else
5381c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20lu ",
539d85f50d5SNeil Horman 				   lnames[i].name, rlim[i].rlim_cur);
540d85f50d5SNeil Horman 
541d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
5421c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20s ", "unlimited");
543d85f50d5SNeil Horman 		else
5441c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20lu ", rlim[i].rlim_max);
545d85f50d5SNeil Horman 
546d85f50d5SNeil Horman 		if (lnames[i].unit)
5471c963eb1SAlexey Dobriyan 			seq_printf(m, "%-10s\n", lnames[i].unit);
548d85f50d5SNeil Horman 		else
5491c963eb1SAlexey Dobriyan 			seq_putc(m, '\n');
550d85f50d5SNeil Horman 	}
551d85f50d5SNeil Horman 
5521c963eb1SAlexey Dobriyan 	return 0;
553d85f50d5SNeil Horman }
554d85f50d5SNeil Horman 
555ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
55609d93bd6SAlexey Dobriyan static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
55709d93bd6SAlexey Dobriyan 			    struct pid *pid, struct task_struct *task)
558ebcb6734SRoland McGrath {
559631b7abaSSteven Rostedt (Red Hat) 	struct syscall_info info;
560631b7abaSSteven Rostedt (Red Hat) 	u64 *args = &info.data.args[0];
56125ce3191SJoe Perches 	int res;
56225ce3191SJoe Perches 
56325ce3191SJoe Perches 	res = lock_trace(task);
564a9712bc1SAl Viro 	if (res)
565a9712bc1SAl Viro 		return res;
566ebcb6734SRoland McGrath 
567631b7abaSSteven Rostedt (Red Hat) 	if (task_current_syscall(task, &info))
56809d93bd6SAlexey Dobriyan 		seq_puts(m, "running\n");
569631b7abaSSteven Rostedt (Red Hat) 	else if (info.data.nr < 0)
570631b7abaSSteven Rostedt (Red Hat) 		seq_printf(m, "%d 0x%llx 0x%llx\n",
571631b7abaSSteven Rostedt (Red Hat) 			   info.data.nr, info.sp, info.data.instruction_pointer);
572a9712bc1SAl Viro 	else
57309d93bd6SAlexey Dobriyan 		seq_printf(m,
574631b7abaSSteven Rostedt (Red Hat) 		       "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
575631b7abaSSteven Rostedt (Red Hat) 		       info.data.nr,
576ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
577631b7abaSSteven Rostedt (Red Hat) 		       info.sp, info.data.instruction_pointer);
578a9712bc1SAl Viro 	unlock_trace(task);
57925ce3191SJoe Perches 
58025ce3191SJoe Perches 	return 0;
581ebcb6734SRoland McGrath }
582ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
583ebcb6734SRoland McGrath 
5841da177e4SLinus Torvalds /************************************************************************/
5851da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5861da177e4SLinus Torvalds /************************************************************************/
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /* permission checks */
589778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5901da177e4SLinus Torvalds {
591778c1144SEric W. Biederman 	struct task_struct *task;
592778c1144SEric W. Biederman 	int allowed = 0;
593df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
594df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
595df26c40eSEric W. Biederman 	 * information.
596778c1144SEric W. Biederman 	 */
597778c1144SEric W. Biederman 	task = get_proc_task(inode);
598df26c40eSEric W. Biederman 	if (task) {
599caaee623SJann Horn 		allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
600778c1144SEric W. Biederman 		put_task_struct(task);
601df26c40eSEric W. Biederman 	}
602778c1144SEric W. Biederman 	return allowed;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6056b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
6066d76fa58SLinus Torvalds {
6076d76fa58SLinus Torvalds 	int error;
6082b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
6096d76fa58SLinus Torvalds 
6106d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
6116d76fa58SLinus Torvalds 		return -EPERM;
6126d76fa58SLinus Torvalds 
61331051c85SJan Kara 	error = setattr_prepare(dentry, attr);
6141025774cSChristoph Hellwig 	if (error)
6156d76fa58SLinus Torvalds 		return error;
6161025774cSChristoph Hellwig 
6171025774cSChristoph Hellwig 	setattr_copy(inode, attr);
6181025774cSChristoph Hellwig 	mark_inode_dirty(inode);
6191025774cSChristoph Hellwig 	return 0;
6206d76fa58SLinus Torvalds }
6216d76fa58SLinus Torvalds 
6220499680aSVasiliy Kulikov /*
6230499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
6240499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
6250499680aSVasiliy Kulikov  */
6260499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
6270499680aSVasiliy Kulikov 				 struct task_struct *task,
6280499680aSVasiliy Kulikov 				 int hide_pid_min)
6290499680aSVasiliy Kulikov {
6300499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
6310499680aSVasiliy Kulikov 		return true;
6320499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
6330499680aSVasiliy Kulikov 		return true;
634caaee623SJann Horn 	return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
6350499680aSVasiliy Kulikov }
6360499680aSVasiliy Kulikov 
6370499680aSVasiliy Kulikov 
6380499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
6390499680aSVasiliy Kulikov {
64076f668beSChristoph Hellwig 	struct pid_namespace *pid = proc_pid_ns(inode);
6410499680aSVasiliy Kulikov 	struct task_struct *task;
6420499680aSVasiliy Kulikov 	bool has_perms;
6430499680aSVasiliy Kulikov 
6440499680aSVasiliy Kulikov 	task = get_proc_task(inode);
645a2ef990aSXiaotian Feng 	if (!task)
646a2ef990aSXiaotian Feng 		return -ESRCH;
647796f571bSLafcadio Wluiki 	has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
6480499680aSVasiliy Kulikov 	put_task_struct(task);
6490499680aSVasiliy Kulikov 
6500499680aSVasiliy Kulikov 	if (!has_perms) {
651796f571bSLafcadio Wluiki 		if (pid->hide_pid == HIDEPID_INVISIBLE) {
6520499680aSVasiliy Kulikov 			/*
6530499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
6540499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
6550499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
6560499680aSVasiliy Kulikov 			 * in procfs at all.
6570499680aSVasiliy Kulikov 			 */
6580499680aSVasiliy Kulikov 			return -ENOENT;
6590499680aSVasiliy Kulikov 		}
6600499680aSVasiliy Kulikov 
6610499680aSVasiliy Kulikov 		return -EPERM;
6620499680aSVasiliy Kulikov 	}
6630499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
6640499680aSVasiliy Kulikov }
6650499680aSVasiliy Kulikov 
6660499680aSVasiliy Kulikov 
6670499680aSVasiliy Kulikov 
668c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6696d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6706d76fa58SLinus Torvalds };
6716d76fa58SLinus Torvalds 
672be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
673be614086SEric W. Biederman {
674be614086SEric W. Biederman 	struct inode *inode = m->private;
67576f668beSChristoph Hellwig 	struct pid_namespace *ns = proc_pid_ns(inode);
67676f668beSChristoph Hellwig 	struct pid *pid = proc_pid(inode);
677be614086SEric W. Biederman 	struct task_struct *task;
678be614086SEric W. Biederman 	int ret;
679be614086SEric W. Biederman 
680be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
681be614086SEric W. Biederman 	if (!task)
682be614086SEric W. Biederman 		return -ESRCH;
683be614086SEric W. Biederman 
684be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
685be614086SEric W. Biederman 
686be614086SEric W. Biederman 	put_task_struct(task);
687be614086SEric W. Biederman 	return ret;
688be614086SEric W. Biederman }
689be614086SEric W. Biederman 
690be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
691be614086SEric W. Biederman {
692c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
693be614086SEric W. Biederman }
694be614086SEric W. Biederman 
695be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
696be614086SEric W. Biederman 	.open		= proc_single_open,
697be614086SEric W. Biederman 	.read		= seq_read,
698be614086SEric W. Biederman 	.llseek		= seq_lseek,
699be614086SEric W. Biederman 	.release	= single_release,
700be614086SEric W. Biederman };
701be614086SEric W. Biederman 
7025381e169SOleg Nesterov 
7035381e169SOleg Nesterov struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
7041da177e4SLinus Torvalds {
7055381e169SOleg Nesterov 	struct task_struct *task = get_proc_task(inode);
7065381e169SOleg Nesterov 	struct mm_struct *mm = ERR_PTR(-ESRCH);
707e268337dSLinus Torvalds 
7085381e169SOleg Nesterov 	if (task) {
709caaee623SJann Horn 		mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
710e268337dSLinus Torvalds 		put_task_struct(task);
711e268337dSLinus Torvalds 
7125381e169SOleg Nesterov 		if (!IS_ERR_OR_NULL(mm)) {
7136d08f2c7SOleg Nesterov 			/* ensure this mm_struct can't be freed */
714f1f10076SVegard Nossum 			mmgrab(mm);
7156d08f2c7SOleg Nesterov 			/* but do not pin its memory */
7166d08f2c7SOleg Nesterov 			mmput(mm);
7176d08f2c7SOleg Nesterov 		}
7185381e169SOleg Nesterov 	}
7195381e169SOleg Nesterov 
7205381e169SOleg Nesterov 	return mm;
7215381e169SOleg Nesterov }
7225381e169SOleg Nesterov 
7235381e169SOleg Nesterov static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
7245381e169SOleg Nesterov {
7255381e169SOleg Nesterov 	struct mm_struct *mm = proc_mem_open(inode, mode);
7265381e169SOleg Nesterov 
7275381e169SOleg Nesterov 	if (IS_ERR(mm))
7285381e169SOleg Nesterov 		return PTR_ERR(mm);
7296d08f2c7SOleg Nesterov 
730e268337dSLinus Torvalds 	file->private_data = mm;
7311da177e4SLinus Torvalds 	return 0;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
735b409e578SCong Wang {
736bc452b4bSDjalal Harouni 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
737bc452b4bSDjalal Harouni 
738bc452b4bSDjalal Harouni 	/* OK to pass negative loff_t, we can catch out-of-range */
739bc452b4bSDjalal Harouni 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
740bc452b4bSDjalal Harouni 
741bc452b4bSDjalal Harouni 	return ret;
742b409e578SCong Wang }
743b409e578SCong Wang 
744572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
745572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
7461da177e4SLinus Torvalds {
747e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
748572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
749572d34b9SOleg Nesterov 	ssize_t copied;
7501da177e4SLinus Torvalds 	char *page;
751272ddc8bSLinus Torvalds 	unsigned int flags;
7521da177e4SLinus Torvalds 
753e268337dSLinus Torvalds 	if (!mm)
754e268337dSLinus Torvalds 		return 0;
7551da177e4SLinus Torvalds 
7560ee931c4SMichal Hocko 	page = (char *)__get_free_page(GFP_KERNEL);
7571da177e4SLinus Torvalds 	if (!page)
758e268337dSLinus Torvalds 		return -ENOMEM;
7591da177e4SLinus Torvalds 
760f7ca54f4SFrederik Deweerdt 	copied = 0;
761388f7934SVegard Nossum 	if (!mmget_not_zero(mm))
7626d08f2c7SOleg Nesterov 		goto free;
7636d08f2c7SOleg Nesterov 
764f511c0b1SLinus Torvalds 	flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
7656347e8d5SLorenzo Stoakes 
7661da177e4SLinus Torvalds 	while (count > 0) {
767572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
7681da177e4SLinus Torvalds 
769572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
7701da177e4SLinus Torvalds 			copied = -EFAULT;
7711da177e4SLinus Torvalds 			break;
7721da177e4SLinus Torvalds 		}
773572d34b9SOleg Nesterov 
7746347e8d5SLorenzo Stoakes 		this_len = access_remote_vm(mm, addr, page, this_len, flags);
775572d34b9SOleg Nesterov 		if (!this_len) {
7761da177e4SLinus Torvalds 			if (!copied)
7771da177e4SLinus Torvalds 				copied = -EIO;
7781da177e4SLinus Torvalds 			break;
7791da177e4SLinus Torvalds 		}
780572d34b9SOleg Nesterov 
781572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
782572d34b9SOleg Nesterov 			copied = -EFAULT;
783572d34b9SOleg Nesterov 			break;
7841da177e4SLinus Torvalds 		}
785572d34b9SOleg Nesterov 
786572d34b9SOleg Nesterov 		buf += this_len;
787572d34b9SOleg Nesterov 		addr += this_len;
788572d34b9SOleg Nesterov 		copied += this_len;
789572d34b9SOleg Nesterov 		count -= this_len;
790572d34b9SOleg Nesterov 	}
791572d34b9SOleg Nesterov 	*ppos = addr;
79230cd8903SKOSAKI Motohiro 
7936d08f2c7SOleg Nesterov 	mmput(mm);
7946d08f2c7SOleg Nesterov free:
79530cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
7961da177e4SLinus Torvalds 	return copied;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
799572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
800572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
801572d34b9SOleg Nesterov {
802572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
803572d34b9SOleg Nesterov }
804572d34b9SOleg Nesterov 
805572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
806572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
807572d34b9SOleg Nesterov {
808572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
809572d34b9SOleg Nesterov }
810572d34b9SOleg Nesterov 
81185863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	switch (orig) {
8141da177e4SLinus Torvalds 	case 0:
8151da177e4SLinus Torvalds 		file->f_pos = offset;
8161da177e4SLinus Torvalds 		break;
8171da177e4SLinus Torvalds 	case 1:
8181da177e4SLinus Torvalds 		file->f_pos += offset;
8191da177e4SLinus Torvalds 		break;
8201da177e4SLinus Torvalds 	default:
8211da177e4SLinus Torvalds 		return -EINVAL;
8221da177e4SLinus Torvalds 	}
8231da177e4SLinus Torvalds 	force_successful_syscall_return();
8241da177e4SLinus Torvalds 	return file->f_pos;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
827e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
828e268337dSLinus Torvalds {
829e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
83071879d3cSOleg Nesterov 	if (mm)
8316d08f2c7SOleg Nesterov 		mmdrop(mm);
832e268337dSLinus Torvalds 	return 0;
833e268337dSLinus Torvalds }
834e268337dSLinus Torvalds 
83500977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8361da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8371da177e4SLinus Torvalds 	.read		= mem_read,
8381da177e4SLinus Torvalds 	.write		= mem_write,
8391da177e4SLinus Torvalds 	.open		= mem_open,
840e268337dSLinus Torvalds 	.release	= mem_release,
8411da177e4SLinus Torvalds };
8421da177e4SLinus Torvalds 
843b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
844b409e578SCong Wang {
845b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
846b409e578SCong Wang }
847b409e578SCong Wang 
848315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
849315e28c8SJames Pearson 			size_t count, loff_t *ppos)
850315e28c8SJames Pearson {
851315e28c8SJames Pearson 	char *page;
852315e28c8SJames Pearson 	unsigned long src = *ppos;
853b409e578SCong Wang 	int ret = 0;
854b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
855a3b609efSMateusz Guzik 	unsigned long env_start, env_end;
856315e28c8SJames Pearson 
8578148a73cSMathias Krause 	/* Ensure the process spawned far enough to have an environment. */
8588148a73cSMathias Krause 	if (!mm || !mm->env_end)
859b409e578SCong Wang 		return 0;
860315e28c8SJames Pearson 
8610ee931c4SMichal Hocko 	page = (char *)__get_free_page(GFP_KERNEL);
862315e28c8SJames Pearson 	if (!page)
863b409e578SCong Wang 		return -ENOMEM;
864315e28c8SJames Pearson 
865d6f64b89SAl Viro 	ret = 0;
866388f7934SVegard Nossum 	if (!mmget_not_zero(mm))
867b409e578SCong Wang 		goto free;
868a3b609efSMateusz Guzik 
86988aa7cc6SYang Shi 	spin_lock(&mm->arg_lock);
870a3b609efSMateusz Guzik 	env_start = mm->env_start;
871a3b609efSMateusz Guzik 	env_end = mm->env_end;
87288aa7cc6SYang Shi 	spin_unlock(&mm->arg_lock);
873a3b609efSMateusz Guzik 
874315e28c8SJames Pearson 	while (count > 0) {
875e8905ec2SDjalal Harouni 		size_t this_len, max_len;
876e8905ec2SDjalal Harouni 		int retval;
877e8905ec2SDjalal Harouni 
878a3b609efSMateusz Guzik 		if (src >= (env_end - env_start))
879e8905ec2SDjalal Harouni 			break;
880315e28c8SJames Pearson 
881a3b609efSMateusz Guzik 		this_len = env_end - (env_start + src);
882315e28c8SJames Pearson 
883e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
884e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
885315e28c8SJames Pearson 
8867f7ccc2cSWilly Tarreau 		retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
887315e28c8SJames Pearson 
888315e28c8SJames Pearson 		if (retval <= 0) {
889315e28c8SJames Pearson 			ret = retval;
890315e28c8SJames Pearson 			break;
891315e28c8SJames Pearson 		}
892315e28c8SJames Pearson 
893315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
894315e28c8SJames Pearson 			ret = -EFAULT;
895315e28c8SJames Pearson 			break;
896315e28c8SJames Pearson 		}
897315e28c8SJames Pearson 
898315e28c8SJames Pearson 		ret += retval;
899315e28c8SJames Pearson 		src += retval;
900315e28c8SJames Pearson 		buf += retval;
901315e28c8SJames Pearson 		count -= retval;
902315e28c8SJames Pearson 	}
903315e28c8SJames Pearson 	*ppos = src;
904315e28c8SJames Pearson 	mmput(mm);
905b409e578SCong Wang 
906b409e578SCong Wang free:
907315e28c8SJames Pearson 	free_page((unsigned long) page);
908315e28c8SJames Pearson 	return ret;
909315e28c8SJames Pearson }
910315e28c8SJames Pearson 
911315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
912b409e578SCong Wang 	.open		= environ_open,
913315e28c8SJames Pearson 	.read		= environ_read,
91487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
915b409e578SCong Wang 	.release	= mem_release,
916315e28c8SJames Pearson };
917315e28c8SJames Pearson 
918c5317167SAl Viro static int auxv_open(struct inode *inode, struct file *file)
919c5317167SAl Viro {
920c5317167SAl Viro 	return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
921c5317167SAl Viro }
922c5317167SAl Viro 
923c5317167SAl Viro static ssize_t auxv_read(struct file *file, char __user *buf,
924c5317167SAl Viro 			size_t count, loff_t *ppos)
925c5317167SAl Viro {
926c5317167SAl Viro 	struct mm_struct *mm = file->private_data;
927c5317167SAl Viro 	unsigned int nwords = 0;
92806b2849dSLeon Yu 
92906b2849dSLeon Yu 	if (!mm)
93006b2849dSLeon Yu 		return 0;
931c5317167SAl Viro 	do {
932c5317167SAl Viro 		nwords += 2;
933c5317167SAl Viro 	} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
934c5317167SAl Viro 	return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
935c5317167SAl Viro 				       nwords * sizeof(mm->saved_auxv[0]));
936c5317167SAl Viro }
937c5317167SAl Viro 
938c5317167SAl Viro static const struct file_operations proc_auxv_operations = {
939c5317167SAl Viro 	.open		= auxv_open,
940c5317167SAl Viro 	.read		= auxv_read,
941c5317167SAl Viro 	.llseek		= generic_file_llseek,
942c5317167SAl Viro 	.release	= mem_release,
943c5317167SAl Viro };
944c5317167SAl Viro 
945fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
946fa0cbbf1SDavid Rientjes 			    loff_t *ppos)
947fa0cbbf1SDavid Rientjes {
948496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
949fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
950fa0cbbf1SDavid Rientjes 	int oom_adj = OOM_ADJUST_MIN;
951fa0cbbf1SDavid Rientjes 	size_t len;
952fa0cbbf1SDavid Rientjes 
953fa0cbbf1SDavid Rientjes 	if (!task)
954fa0cbbf1SDavid Rientjes 		return -ESRCH;
955fa0cbbf1SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
956fa0cbbf1SDavid Rientjes 		oom_adj = OOM_ADJUST_MAX;
957fa0cbbf1SDavid Rientjes 	else
958fa0cbbf1SDavid Rientjes 		oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
959fa0cbbf1SDavid Rientjes 			  OOM_SCORE_ADJ_MAX;
960fa0cbbf1SDavid Rientjes 	put_task_struct(task);
961fa0cbbf1SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
962fa0cbbf1SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
963fa0cbbf1SDavid Rientjes }
964fa0cbbf1SDavid Rientjes 
9651d5f0acbSMichal Hocko static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
9661d5f0acbSMichal Hocko {
967f913da59SMichal Hocko 	static DEFINE_MUTEX(oom_adj_mutex);
96844a70adeSMichal Hocko 	struct mm_struct *mm = NULL;
9691d5f0acbSMichal Hocko 	struct task_struct *task;
9701d5f0acbSMichal Hocko 	int err = 0;
9711d5f0acbSMichal Hocko 
9721d5f0acbSMichal Hocko 	task = get_proc_task(file_inode(file));
9731d5f0acbSMichal Hocko 	if (!task)
9741d5f0acbSMichal Hocko 		return -ESRCH;
9751d5f0acbSMichal Hocko 
9761d5f0acbSMichal Hocko 	mutex_lock(&oom_adj_mutex);
9771d5f0acbSMichal Hocko 	if (legacy) {
9781d5f0acbSMichal Hocko 		if (oom_adj < task->signal->oom_score_adj &&
9791d5f0acbSMichal Hocko 				!capable(CAP_SYS_RESOURCE)) {
9801d5f0acbSMichal Hocko 			err = -EACCES;
9811d5f0acbSMichal Hocko 			goto err_unlock;
9821d5f0acbSMichal Hocko 		}
9831d5f0acbSMichal Hocko 		/*
9841d5f0acbSMichal Hocko 		 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
9851d5f0acbSMichal Hocko 		 * /proc/pid/oom_score_adj instead.
9861d5f0acbSMichal Hocko 		 */
9871d5f0acbSMichal Hocko 		pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
9881d5f0acbSMichal Hocko 			  current->comm, task_pid_nr(current), task_pid_nr(task),
9891d5f0acbSMichal Hocko 			  task_pid_nr(task));
9901d5f0acbSMichal Hocko 	} else {
9911d5f0acbSMichal Hocko 		if ((short)oom_adj < task->signal->oom_score_adj_min &&
9921d5f0acbSMichal Hocko 				!capable(CAP_SYS_RESOURCE)) {
9931d5f0acbSMichal Hocko 			err = -EACCES;
9941d5f0acbSMichal Hocko 			goto err_unlock;
9951d5f0acbSMichal Hocko 		}
9961d5f0acbSMichal Hocko 	}
9971d5f0acbSMichal Hocko 
99844a70adeSMichal Hocko 	/*
99944a70adeSMichal Hocko 	 * Make sure we will check other processes sharing the mm if this is
100044a70adeSMichal Hocko 	 * not vfrok which wants its own oom_score_adj.
100144a70adeSMichal Hocko 	 * pin the mm so it doesn't go away and get reused after task_unlock
100244a70adeSMichal Hocko 	 */
100344a70adeSMichal Hocko 	if (!task->vfork_done) {
100444a70adeSMichal Hocko 		struct task_struct *p = find_lock_task_mm(task);
100544a70adeSMichal Hocko 
100644a70adeSMichal Hocko 		if (p) {
100744a70adeSMichal Hocko 			if (atomic_read(&p->mm->mm_users) > 1) {
100844a70adeSMichal Hocko 				mm = p->mm;
1009f1f10076SVegard Nossum 				mmgrab(mm);
101044a70adeSMichal Hocko 			}
101144a70adeSMichal Hocko 			task_unlock(p);
101244a70adeSMichal Hocko 		}
101344a70adeSMichal Hocko 	}
101444a70adeSMichal Hocko 
10151d5f0acbSMichal Hocko 	task->signal->oom_score_adj = oom_adj;
10161d5f0acbSMichal Hocko 	if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
10171d5f0acbSMichal Hocko 		task->signal->oom_score_adj_min = (short)oom_adj;
10181d5f0acbSMichal Hocko 	trace_oom_score_adj_update(task);
101944a70adeSMichal Hocko 
102044a70adeSMichal Hocko 	if (mm) {
102144a70adeSMichal Hocko 		struct task_struct *p;
102244a70adeSMichal Hocko 
102344a70adeSMichal Hocko 		rcu_read_lock();
102444a70adeSMichal Hocko 		for_each_process(p) {
102544a70adeSMichal Hocko 			if (same_thread_group(task, p))
102644a70adeSMichal Hocko 				continue;
102744a70adeSMichal Hocko 
102844a70adeSMichal Hocko 			/* do not touch kernel threads or the global init */
102944a70adeSMichal Hocko 			if (p->flags & PF_KTHREAD || is_global_init(p))
103044a70adeSMichal Hocko 				continue;
103144a70adeSMichal Hocko 
103244a70adeSMichal Hocko 			task_lock(p);
103344a70adeSMichal Hocko 			if (!p->vfork_done && process_shares_mm(p, mm)) {
103444a70adeSMichal Hocko 				p->signal->oom_score_adj = oom_adj;
103544a70adeSMichal Hocko 				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
103644a70adeSMichal Hocko 					p->signal->oom_score_adj_min = (short)oom_adj;
103744a70adeSMichal Hocko 			}
103844a70adeSMichal Hocko 			task_unlock(p);
103944a70adeSMichal Hocko 		}
104044a70adeSMichal Hocko 		rcu_read_unlock();
104144a70adeSMichal Hocko 		mmdrop(mm);
104244a70adeSMichal Hocko 	}
10431d5f0acbSMichal Hocko err_unlock:
10441d5f0acbSMichal Hocko 	mutex_unlock(&oom_adj_mutex);
10451d5f0acbSMichal Hocko 	put_task_struct(task);
10461d5f0acbSMichal Hocko 	return err;
10471d5f0acbSMichal Hocko }
1048f913da59SMichal Hocko 
1049b72bdfa7SDavid Rientjes /*
1050b72bdfa7SDavid Rientjes  * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1051b72bdfa7SDavid Rientjes  * kernels.  The effective policy is defined by oom_score_adj, which has a
1052b72bdfa7SDavid Rientjes  * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1053b72bdfa7SDavid Rientjes  * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1054b72bdfa7SDavid Rientjes  * Processes that become oom disabled via oom_adj will still be oom disabled
1055b72bdfa7SDavid Rientjes  * with this implementation.
1056b72bdfa7SDavid Rientjes  *
1057b72bdfa7SDavid Rientjes  * oom_adj cannot be removed since existing userspace binaries use it.
1058b72bdfa7SDavid Rientjes  */
1059fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1060fa0cbbf1SDavid Rientjes 			     size_t count, loff_t *ppos)
1061fa0cbbf1SDavid Rientjes {
1062fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
1063fa0cbbf1SDavid Rientjes 	int oom_adj;
1064fa0cbbf1SDavid Rientjes 	int err;
1065fa0cbbf1SDavid Rientjes 
1066fa0cbbf1SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1067fa0cbbf1SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1068fa0cbbf1SDavid Rientjes 		count = sizeof(buffer) - 1;
1069fa0cbbf1SDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1070fa0cbbf1SDavid Rientjes 		err = -EFAULT;
1071fa0cbbf1SDavid Rientjes 		goto out;
1072fa0cbbf1SDavid Rientjes 	}
1073fa0cbbf1SDavid Rientjes 
1074fa0cbbf1SDavid Rientjes 	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1075fa0cbbf1SDavid Rientjes 	if (err)
1076fa0cbbf1SDavid Rientjes 		goto out;
1077fa0cbbf1SDavid Rientjes 	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1078fa0cbbf1SDavid Rientjes 	     oom_adj != OOM_DISABLE) {
1079fa0cbbf1SDavid Rientjes 		err = -EINVAL;
1080fa0cbbf1SDavid Rientjes 		goto out;
1081fa0cbbf1SDavid Rientjes 	}
1082fa0cbbf1SDavid Rientjes 
1083fa0cbbf1SDavid Rientjes 	/*
1084fa0cbbf1SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1085fa0cbbf1SDavid Rientjes 	 * value is always attainable.
1086fa0cbbf1SDavid Rientjes 	 */
1087fa0cbbf1SDavid Rientjes 	if (oom_adj == OOM_ADJUST_MAX)
1088fa0cbbf1SDavid Rientjes 		oom_adj = OOM_SCORE_ADJ_MAX;
1089fa0cbbf1SDavid Rientjes 	else
1090fa0cbbf1SDavid Rientjes 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1091fa0cbbf1SDavid Rientjes 
10921d5f0acbSMichal Hocko 	err = __set_oom_adj(file, oom_adj, true);
1093fa0cbbf1SDavid Rientjes out:
1094fa0cbbf1SDavid Rientjes 	return err < 0 ? err : count;
1095fa0cbbf1SDavid Rientjes }
1096fa0cbbf1SDavid Rientjes 
1097fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = {
1098fa0cbbf1SDavid Rientjes 	.read		= oom_adj_read,
1099fa0cbbf1SDavid Rientjes 	.write		= oom_adj_write,
1100fa0cbbf1SDavid Rientjes 	.llseek		= generic_file_llseek,
1101fa0cbbf1SDavid Rientjes };
1102fa0cbbf1SDavid Rientjes 
1103a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1104a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1105a63d83f4SDavid Rientjes {
1106496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1107a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1108a9c58b90SDavid Rientjes 	short oom_score_adj = OOM_SCORE_ADJ_MIN;
1109a63d83f4SDavid Rientjes 	size_t len;
1110a63d83f4SDavid Rientjes 
1111a63d83f4SDavid Rientjes 	if (!task)
1112a63d83f4SDavid Rientjes 		return -ESRCH;
1113a63d83f4SDavid Rientjes 	oom_score_adj = task->signal->oom_score_adj;
1114a63d83f4SDavid Rientjes 	put_task_struct(task);
1115a9c58b90SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1116a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1117a63d83f4SDavid Rientjes }
1118a63d83f4SDavid Rientjes 
1119a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1120a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1121a63d83f4SDavid Rientjes {
1122a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
11230a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1124a63d83f4SDavid Rientjes 	int err;
1125a63d83f4SDavid Rientjes 
1126a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1127a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1128a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1129723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1130723548bfSDavid Rientjes 		err = -EFAULT;
1131723548bfSDavid Rientjes 		goto out;
1132723548bfSDavid Rientjes 	}
1133a63d83f4SDavid Rientjes 
11340a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1135a63d83f4SDavid Rientjes 	if (err)
1136723548bfSDavid Rientjes 		goto out;
1137a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1138723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1139723548bfSDavid Rientjes 		err = -EINVAL;
1140723548bfSDavid Rientjes 		goto out;
1141723548bfSDavid Rientjes 	}
1142a63d83f4SDavid Rientjes 
11431d5f0acbSMichal Hocko 	err = __set_oom_adj(file, oom_score_adj, false);
1144723548bfSDavid Rientjes out:
1145723548bfSDavid Rientjes 	return err < 0 ? err : count;
1146a63d83f4SDavid Rientjes }
1147a63d83f4SDavid Rientjes 
1148a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1149a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1150a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
11516038f373SArnd Bergmann 	.llseek		= default_llseek,
1152a63d83f4SDavid Rientjes };
1153a63d83f4SDavid Rientjes 
11544b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
1155b4eb4f7fSAlexey Dobriyan #define TMPBUFLEN 11
11561da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
11571da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
11581da177e4SLinus Torvalds {
1159496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
116099f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
11611da177e4SLinus Torvalds 	ssize_t length;
11621da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
11631da177e4SLinus Torvalds 
116499f89551SEric W. Biederman 	if (!task)
116599f89551SEric W. Biederman 		return -ESRCH;
11661da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1167e1760bd5SEric W. Biederman 			   from_kuid(file->f_cred->user_ns,
1168e1760bd5SEric W. Biederman 				     audit_get_loginuid(task)));
116999f89551SEric W. Biederman 	put_task_struct(task);
11701da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11711da177e4SLinus Torvalds }
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
11741da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
11751da177e4SLinus Torvalds {
1176496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
11771da177e4SLinus Torvalds 	uid_t loginuid;
1178e1760bd5SEric W. Biederman 	kuid_t kloginuid;
1179774636e1SAlexey Dobriyan 	int rv;
11801da177e4SLinus Torvalds 
11817dc52157SPaul E. McKenney 	rcu_read_lock();
11827dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11837dc52157SPaul E. McKenney 		rcu_read_unlock();
11841da177e4SLinus Torvalds 		return -EPERM;
11857dc52157SPaul E. McKenney 	}
11867dc52157SPaul E. McKenney 	rcu_read_unlock();
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	if (*ppos != 0) {
11891da177e4SLinus Torvalds 		/* No partial writes. */
11901da177e4SLinus Torvalds 		return -EINVAL;
11911da177e4SLinus Torvalds 	}
11921da177e4SLinus Torvalds 
1193774636e1SAlexey Dobriyan 	rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1194774636e1SAlexey Dobriyan 	if (rv < 0)
1195774636e1SAlexey Dobriyan 		return rv;
119681407c84SEric Paris 
119781407c84SEric Paris 	/* is userspace tring to explicitly UNSET the loginuid? */
119881407c84SEric Paris 	if (loginuid == AUDIT_UID_UNSET) {
119981407c84SEric Paris 		kloginuid = INVALID_UID;
120081407c84SEric Paris 	} else {
1201e1760bd5SEric W. Biederman 		kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1202774636e1SAlexey Dobriyan 		if (!uid_valid(kloginuid))
1203774636e1SAlexey Dobriyan 			return -EINVAL;
120481407c84SEric Paris 	}
1205e1760bd5SEric W. Biederman 
1206774636e1SAlexey Dobriyan 	rv = audit_set_loginuid(kloginuid);
1207774636e1SAlexey Dobriyan 	if (rv < 0)
1208774636e1SAlexey Dobriyan 		return rv;
1209774636e1SAlexey Dobriyan 	return count;
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
121200977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
12131da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
12141da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
121587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12161da177e4SLinus Torvalds };
12171e0bd755SEric Paris 
12181e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
12191e0bd755SEric Paris 				  size_t count, loff_t *ppos)
12201e0bd755SEric Paris {
1221496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
12221e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
12231e0bd755SEric Paris 	ssize_t length;
12241e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
12251e0bd755SEric Paris 
12261e0bd755SEric Paris 	if (!task)
12271e0bd755SEric Paris 		return -ESRCH;
12281e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12291e0bd755SEric Paris 				audit_get_sessionid(task));
12301e0bd755SEric Paris 	put_task_struct(task);
12311e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12321e0bd755SEric Paris }
12331e0bd755SEric Paris 
12341e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
12351e0bd755SEric Paris 	.read		= proc_sessionid_read,
123687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12371e0bd755SEric Paris };
12381da177e4SLinus Torvalds #endif
12391da177e4SLinus Torvalds 
1240f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1241f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1242f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1243f4f154fdSAkinobu Mita {
1244496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1245f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1246f4f154fdSAkinobu Mita 	size_t len;
1247f4f154fdSAkinobu Mita 	int make_it_fail;
1248f4f154fdSAkinobu Mita 
1249f4f154fdSAkinobu Mita 	if (!task)
1250f4f154fdSAkinobu Mita 		return -ESRCH;
1251f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1252f4f154fdSAkinobu Mita 	put_task_struct(task);
1253f4f154fdSAkinobu Mita 
1254f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
12550c28f287SAkinobu Mita 
12560c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1257f4f154fdSAkinobu Mita }
1258f4f154fdSAkinobu Mita 
1259f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1260f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1261f4f154fdSAkinobu Mita {
1262f4f154fdSAkinobu Mita 	struct task_struct *task;
1263774636e1SAlexey Dobriyan 	char buffer[PROC_NUMBUF];
1264f4f154fdSAkinobu Mita 	int make_it_fail;
1265774636e1SAlexey Dobriyan 	int rv;
1266f4f154fdSAkinobu Mita 
1267f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1268f4f154fdSAkinobu Mita 		return -EPERM;
1269f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1270f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1271f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1272f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1273f4f154fdSAkinobu Mita 		return -EFAULT;
1274774636e1SAlexey Dobriyan 	rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1275774636e1SAlexey Dobriyan 	if (rv < 0)
1276774636e1SAlexey Dobriyan 		return rv;
127716caed31SDave Jones 	if (make_it_fail < 0 || make_it_fail > 1)
127816caed31SDave Jones 		return -EINVAL;
127916caed31SDave Jones 
1280496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1281f4f154fdSAkinobu Mita 	if (!task)
1282f4f154fdSAkinobu Mita 		return -ESRCH;
1283f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1284f4f154fdSAkinobu Mita 	put_task_struct(task);
1285cba8aafeSVincent Li 
1286cba8aafeSVincent Li 	return count;
1287f4f154fdSAkinobu Mita }
1288f4f154fdSAkinobu Mita 
128900977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1290f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1291f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
129287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1293f4f154fdSAkinobu Mita };
1294e41d5818SDmitry Vyukov 
1295e41d5818SDmitry Vyukov static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1296e41d5818SDmitry Vyukov 				   size_t count, loff_t *ppos)
1297e41d5818SDmitry Vyukov {
1298e41d5818SDmitry Vyukov 	struct task_struct *task;
12999049f2f6SAkinobu Mita 	int err;
13009049f2f6SAkinobu Mita 	unsigned int n;
1301e41d5818SDmitry Vyukov 
13029049f2f6SAkinobu Mita 	err = kstrtouint_from_user(buf, count, 0, &n);
1303e41d5818SDmitry Vyukov 	if (err)
1304e41d5818SDmitry Vyukov 		return err;
13051203c8e6SAkinobu Mita 
13061203c8e6SAkinobu Mita 	task = get_proc_task(file_inode(file));
13071203c8e6SAkinobu Mita 	if (!task)
13081203c8e6SAkinobu Mita 		return -ESRCH;
13099f7118b2SAlexey Dobriyan 	task->fail_nth = n;
13101203c8e6SAkinobu Mita 	put_task_struct(task);
13111203c8e6SAkinobu Mita 
1312e41d5818SDmitry Vyukov 	return count;
1313e41d5818SDmitry Vyukov }
1314e41d5818SDmitry Vyukov 
1315e41d5818SDmitry Vyukov static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1316e41d5818SDmitry Vyukov 				  size_t count, loff_t *ppos)
1317e41d5818SDmitry Vyukov {
1318e41d5818SDmitry Vyukov 	struct task_struct *task;
1319bfc74093SAkinobu Mita 	char numbuf[PROC_NUMBUF];
1320bfc74093SAkinobu Mita 	ssize_t len;
1321e41d5818SDmitry Vyukov 
1322e41d5818SDmitry Vyukov 	task = get_proc_task(file_inode(file));
1323e41d5818SDmitry Vyukov 	if (!task)
1324e41d5818SDmitry Vyukov 		return -ESRCH;
13259f7118b2SAlexey Dobriyan 	len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
13261203c8e6SAkinobu Mita 	put_task_struct(task);
1327a44937feSAlexey Dobriyan 	return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1328e41d5818SDmitry Vyukov }
1329e41d5818SDmitry Vyukov 
1330e41d5818SDmitry Vyukov static const struct file_operations proc_fail_nth_operations = {
1331e41d5818SDmitry Vyukov 	.read		= proc_fail_nth_read,
1332e41d5818SDmitry Vyukov 	.write		= proc_fail_nth_write,
1333e41d5818SDmitry Vyukov };
1334f4f154fdSAkinobu Mita #endif
1335f4f154fdSAkinobu Mita 
13369745512cSArjan van de Ven 
133743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
133843ae34cbSIngo Molnar /*
133943ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
134043ae34cbSIngo Molnar  */
134143ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
134243ae34cbSIngo Molnar {
134343ae34cbSIngo Molnar 	struct inode *inode = m->private;
134476f668beSChristoph Hellwig 	struct pid_namespace *ns = proc_pid_ns(inode);
134543ae34cbSIngo Molnar 	struct task_struct *p;
134643ae34cbSIngo Molnar 
134743ae34cbSIngo Molnar 	p = get_proc_task(inode);
134843ae34cbSIngo Molnar 	if (!p)
134943ae34cbSIngo Molnar 		return -ESRCH;
135074dc3384SAleksa Sarai 	proc_sched_show_task(p, ns, m);
135143ae34cbSIngo Molnar 
135243ae34cbSIngo Molnar 	put_task_struct(p);
135343ae34cbSIngo Molnar 
135443ae34cbSIngo Molnar 	return 0;
135543ae34cbSIngo Molnar }
135643ae34cbSIngo Molnar 
135743ae34cbSIngo Molnar static ssize_t
135843ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
135943ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
136043ae34cbSIngo Molnar {
1361496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
136243ae34cbSIngo Molnar 	struct task_struct *p;
136343ae34cbSIngo Molnar 
136443ae34cbSIngo Molnar 	p = get_proc_task(inode);
136543ae34cbSIngo Molnar 	if (!p)
136643ae34cbSIngo Molnar 		return -ESRCH;
136743ae34cbSIngo Molnar 	proc_sched_set_task(p);
136843ae34cbSIngo Molnar 
136943ae34cbSIngo Molnar 	put_task_struct(p);
137043ae34cbSIngo Molnar 
137143ae34cbSIngo Molnar 	return count;
137243ae34cbSIngo Molnar }
137343ae34cbSIngo Molnar 
137443ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
137543ae34cbSIngo Molnar {
1376c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
137743ae34cbSIngo Molnar }
137843ae34cbSIngo Molnar 
137943ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
138043ae34cbSIngo Molnar 	.open		= sched_open,
138143ae34cbSIngo Molnar 	.read		= seq_read,
138243ae34cbSIngo Molnar 	.write		= sched_write,
138343ae34cbSIngo Molnar 	.llseek		= seq_lseek,
13845ea473a1SAlexey Dobriyan 	.release	= single_release,
138543ae34cbSIngo Molnar };
138643ae34cbSIngo Molnar 
138743ae34cbSIngo Molnar #endif
138843ae34cbSIngo Molnar 
13895091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
13905091faa4SMike Galbraith /*
13915091faa4SMike Galbraith  * Print out autogroup related information:
13925091faa4SMike Galbraith  */
13935091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
13945091faa4SMike Galbraith {
13955091faa4SMike Galbraith 	struct inode *inode = m->private;
13965091faa4SMike Galbraith 	struct task_struct *p;
13975091faa4SMike Galbraith 
13985091faa4SMike Galbraith 	p = get_proc_task(inode);
13995091faa4SMike Galbraith 	if (!p)
14005091faa4SMike Galbraith 		return -ESRCH;
14015091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
14025091faa4SMike Galbraith 
14035091faa4SMike Galbraith 	put_task_struct(p);
14045091faa4SMike Galbraith 
14055091faa4SMike Galbraith 	return 0;
14065091faa4SMike Galbraith }
14075091faa4SMike Galbraith 
14085091faa4SMike Galbraith static ssize_t
14095091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
14105091faa4SMike Galbraith 	    size_t count, loff_t *offset)
14115091faa4SMike Galbraith {
1412496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
14135091faa4SMike Galbraith 	struct task_struct *p;
14145091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
14150a8cb8e3SAlexey Dobriyan 	int nice;
14165091faa4SMike Galbraith 	int err;
14175091faa4SMike Galbraith 
14185091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
14195091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
14205091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
14215091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
14225091faa4SMike Galbraith 		return -EFAULT;
14235091faa4SMike Galbraith 
14240a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
14250a8cb8e3SAlexey Dobriyan 	if (err < 0)
14260a8cb8e3SAlexey Dobriyan 		return err;
14275091faa4SMike Galbraith 
14285091faa4SMike Galbraith 	p = get_proc_task(inode);
14295091faa4SMike Galbraith 	if (!p)
14305091faa4SMike Galbraith 		return -ESRCH;
14315091faa4SMike Galbraith 
14322e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
14335091faa4SMike Galbraith 	if (err)
14345091faa4SMike Galbraith 		count = err;
14355091faa4SMike Galbraith 
14365091faa4SMike Galbraith 	put_task_struct(p);
14375091faa4SMike Galbraith 
14385091faa4SMike Galbraith 	return count;
14395091faa4SMike Galbraith }
14405091faa4SMike Galbraith 
14415091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
14425091faa4SMike Galbraith {
14435091faa4SMike Galbraith 	int ret;
14445091faa4SMike Galbraith 
14455091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
14465091faa4SMike Galbraith 	if (!ret) {
14475091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
14485091faa4SMike Galbraith 
14495091faa4SMike Galbraith 		m->private = inode;
14505091faa4SMike Galbraith 	}
14515091faa4SMike Galbraith 	return ret;
14525091faa4SMike Galbraith }
14535091faa4SMike Galbraith 
14545091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
14555091faa4SMike Galbraith 	.open		= sched_autogroup_open,
14565091faa4SMike Galbraith 	.read		= seq_read,
14575091faa4SMike Galbraith 	.write		= sched_autogroup_write,
14585091faa4SMike Galbraith 	.llseek		= seq_lseek,
14595091faa4SMike Galbraith 	.release	= single_release,
14605091faa4SMike Galbraith };
14615091faa4SMike Galbraith 
14625091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
14635091faa4SMike Galbraith 
14644614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
14654614a696Sjohn stultz 				size_t count, loff_t *offset)
14664614a696Sjohn stultz {
1467496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
14684614a696Sjohn stultz 	struct task_struct *p;
14694614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
1470830e0fc9SDavid Rientjes 	const size_t maxlen = sizeof(buffer) - 1;
14714614a696Sjohn stultz 
14724614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
1473830e0fc9SDavid Rientjes 	if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
14744614a696Sjohn stultz 		return -EFAULT;
14754614a696Sjohn stultz 
14764614a696Sjohn stultz 	p = get_proc_task(inode);
14774614a696Sjohn stultz 	if (!p)
14784614a696Sjohn stultz 		return -ESRCH;
14794614a696Sjohn stultz 
14804614a696Sjohn stultz 	if (same_thread_group(current, p))
14814614a696Sjohn stultz 		set_task_comm(p, buffer);
14824614a696Sjohn stultz 	else
14834614a696Sjohn stultz 		count = -EINVAL;
14844614a696Sjohn stultz 
14854614a696Sjohn stultz 	put_task_struct(p);
14864614a696Sjohn stultz 
14874614a696Sjohn stultz 	return count;
14884614a696Sjohn stultz }
14894614a696Sjohn stultz 
14904614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
14914614a696Sjohn stultz {
14924614a696Sjohn stultz 	struct inode *inode = m->private;
14934614a696Sjohn stultz 	struct task_struct *p;
14944614a696Sjohn stultz 
14954614a696Sjohn stultz 	p = get_proc_task(inode);
14964614a696Sjohn stultz 	if (!p)
14974614a696Sjohn stultz 		return -ESRCH;
14984614a696Sjohn stultz 
149988b72b31STejun Heo 	proc_task_name(m, p, false);
150088b72b31STejun Heo 	seq_putc(m, '\n');
15014614a696Sjohn stultz 
15024614a696Sjohn stultz 	put_task_struct(p);
15034614a696Sjohn stultz 
15044614a696Sjohn stultz 	return 0;
15054614a696Sjohn stultz }
15064614a696Sjohn stultz 
15074614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
15084614a696Sjohn stultz {
1509c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
15104614a696Sjohn stultz }
15114614a696Sjohn stultz 
15124614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
15134614a696Sjohn stultz 	.open		= comm_open,
15144614a696Sjohn stultz 	.read		= seq_read,
15154614a696Sjohn stultz 	.write		= comm_write,
15164614a696Sjohn stultz 	.llseek		= seq_lseek,
15174614a696Sjohn stultz 	.release	= single_release,
15184614a696Sjohn stultz };
15194614a696Sjohn stultz 
15207773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1521925d1c40SMatt Helsley {
1522925d1c40SMatt Helsley 	struct task_struct *task;
1523925d1c40SMatt Helsley 	struct file *exe_file;
1524925d1c40SMatt Helsley 
15252b0143b5SDavid Howells 	task = get_proc_task(d_inode(dentry));
1526925d1c40SMatt Helsley 	if (!task)
1527925d1c40SMatt Helsley 		return -ENOENT;
1528cd81a917SMateusz Guzik 	exe_file = get_task_exe_file(task);
1529925d1c40SMatt Helsley 	put_task_struct(task);
1530925d1c40SMatt Helsley 	if (exe_file) {
1531925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1532925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1533925d1c40SMatt Helsley 		fput(exe_file);
1534925d1c40SMatt Helsley 		return 0;
1535925d1c40SMatt Helsley 	} else
1536925d1c40SMatt Helsley 		return -ENOENT;
1537925d1c40SMatt Helsley }
1538925d1c40SMatt Helsley 
15396b255391SAl Viro static const char *proc_pid_get_link(struct dentry *dentry,
1540fceef393SAl Viro 				     struct inode *inode,
1541fceef393SAl Viro 				     struct delayed_call *done)
15421da177e4SLinus Torvalds {
1543408ef013SChristoph Hellwig 	struct path path;
15441da177e4SLinus Torvalds 	int error = -EACCES;
15451da177e4SLinus Torvalds 
15466b255391SAl Viro 	if (!dentry)
15476b255391SAl Viro 		return ERR_PTR(-ECHILD);
15486b255391SAl Viro 
1549778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1550778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
15511da177e4SLinus Torvalds 		goto out;
15521da177e4SLinus Torvalds 
1553408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1554408ef013SChristoph Hellwig 	if (error)
1555408ef013SChristoph Hellwig 		goto out;
1556408ef013SChristoph Hellwig 
15576e77137bSAl Viro 	nd_jump_link(&path);
1558408ef013SChristoph Hellwig 	return NULL;
15591da177e4SLinus Torvalds out:
1560008b150aSAl Viro 	return ERR_PTR(error);
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
15633dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
15641da177e4SLinus Torvalds {
15650ee931c4SMichal Hocko 	char *tmp = (char *)__get_free_page(GFP_KERNEL);
15663dcd25f3SJan Blunck 	char *pathname;
15671da177e4SLinus Torvalds 	int len;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	if (!tmp)
15701da177e4SLinus Torvalds 		return -ENOMEM;
15711da177e4SLinus Torvalds 
15727b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
15733dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
15743dcd25f3SJan Blunck 	if (IS_ERR(pathname))
15751da177e4SLinus Torvalds 		goto out;
15763dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	if (len > buflen)
15791da177e4SLinus Torvalds 		len = buflen;
15803dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
15811da177e4SLinus Torvalds 		len = -EFAULT;
15821da177e4SLinus Torvalds  out:
15831da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
15841da177e4SLinus Torvalds 	return len;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	int error = -EACCES;
15902b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
15913dcd25f3SJan Blunck 	struct path path;
15921da177e4SLinus Torvalds 
1593778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1594778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
15951da177e4SLinus Torvalds 		goto out;
15961da177e4SLinus Torvalds 
15977773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
15981da177e4SLinus Torvalds 	if (error)
15991da177e4SLinus Torvalds 		goto out;
16001da177e4SLinus Torvalds 
16013dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
16023dcd25f3SJan Blunck 	path_put(&path);
16031da177e4SLinus Torvalds out:
16041da177e4SLinus Torvalds 	return error;
16051da177e4SLinus Torvalds }
16061da177e4SLinus Torvalds 
1607faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = {
16081da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
16096b255391SAl Viro 	.get_link	= proc_pid_get_link,
16106d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
16111da177e4SLinus Torvalds };
16121da177e4SLinus Torvalds 
161328a6d671SEric W. Biederman 
161428a6d671SEric W. Biederman /* building an inode */
161528a6d671SEric W. Biederman 
1616c6eb50d2SAl Viro void task_dump_owner(struct task_struct *task, umode_t mode,
161768eb94f1SEric W. Biederman 		     kuid_t *ruid, kgid_t *rgid)
161868eb94f1SEric W. Biederman {
161968eb94f1SEric W. Biederman 	/* Depending on the state of dumpable compute who should own a
162068eb94f1SEric W. Biederman 	 * proc file for a task.
162168eb94f1SEric W. Biederman 	 */
162268eb94f1SEric W. Biederman 	const struct cred *cred;
162368eb94f1SEric W. Biederman 	kuid_t uid;
162468eb94f1SEric W. Biederman 	kgid_t gid;
162568eb94f1SEric W. Biederman 
16262e0ad552SAlexey Dobriyan 	if (unlikely(task->flags & PF_KTHREAD)) {
16272e0ad552SAlexey Dobriyan 		*ruid = GLOBAL_ROOT_UID;
16282e0ad552SAlexey Dobriyan 		*rgid = GLOBAL_ROOT_GID;
16292e0ad552SAlexey Dobriyan 		return;
16302e0ad552SAlexey Dobriyan 	}
16312e0ad552SAlexey Dobriyan 
163268eb94f1SEric W. Biederman 	/* Default to the tasks effective ownership */
163368eb94f1SEric W. Biederman 	rcu_read_lock();
163468eb94f1SEric W. Biederman 	cred = __task_cred(task);
163568eb94f1SEric W. Biederman 	uid = cred->euid;
163668eb94f1SEric W. Biederman 	gid = cred->egid;
163768eb94f1SEric W. Biederman 	rcu_read_unlock();
163868eb94f1SEric W. Biederman 
163968eb94f1SEric W. Biederman 	/*
164068eb94f1SEric W. Biederman 	 * Before the /proc/pid/status file was created the only way to read
164168eb94f1SEric W. Biederman 	 * the effective uid of a /process was to stat /proc/pid.  Reading
164268eb94f1SEric W. Biederman 	 * /proc/pid/status is slow enough that procps and other packages
164368eb94f1SEric W. Biederman 	 * kept stating /proc/pid.  To keep the rules in /proc simple I have
164468eb94f1SEric W. Biederman 	 * made this apply to all per process world readable and executable
164568eb94f1SEric W. Biederman 	 * directories.
164668eb94f1SEric W. Biederman 	 */
164768eb94f1SEric W. Biederman 	if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
164868eb94f1SEric W. Biederman 		struct mm_struct *mm;
164968eb94f1SEric W. Biederman 		task_lock(task);
165068eb94f1SEric W. Biederman 		mm = task->mm;
165168eb94f1SEric W. Biederman 		/* Make non-dumpable tasks owned by some root */
165268eb94f1SEric W. Biederman 		if (mm) {
165368eb94f1SEric W. Biederman 			if (get_dumpable(mm) != SUID_DUMP_USER) {
165468eb94f1SEric W. Biederman 				struct user_namespace *user_ns = mm->user_ns;
165568eb94f1SEric W. Biederman 
165668eb94f1SEric W. Biederman 				uid = make_kuid(user_ns, 0);
165768eb94f1SEric W. Biederman 				if (!uid_valid(uid))
165868eb94f1SEric W. Biederman 					uid = GLOBAL_ROOT_UID;
165968eb94f1SEric W. Biederman 
166068eb94f1SEric W. Biederman 				gid = make_kgid(user_ns, 0);
166168eb94f1SEric W. Biederman 				if (!gid_valid(gid))
166268eb94f1SEric W. Biederman 					gid = GLOBAL_ROOT_GID;
166368eb94f1SEric W. Biederman 			}
166468eb94f1SEric W. Biederman 		} else {
166568eb94f1SEric W. Biederman 			uid = GLOBAL_ROOT_UID;
166668eb94f1SEric W. Biederman 			gid = GLOBAL_ROOT_GID;
166768eb94f1SEric W. Biederman 		}
166868eb94f1SEric W. Biederman 		task_unlock(task);
166968eb94f1SEric W. Biederman 	}
167068eb94f1SEric W. Biederman 	*ruid = uid;
167168eb94f1SEric W. Biederman 	*rgid = gid;
167268eb94f1SEric W. Biederman }
167368eb94f1SEric W. Biederman 
1674db978da8SAndreas Gruenbacher struct inode *proc_pid_make_inode(struct super_block * sb,
1675db978da8SAndreas Gruenbacher 				  struct task_struct *task, umode_t mode)
167628a6d671SEric W. Biederman {
167728a6d671SEric W. Biederman 	struct inode * inode;
167828a6d671SEric W. Biederman 	struct proc_inode *ei;
167928a6d671SEric W. Biederman 
168028a6d671SEric W. Biederman 	/* We need a new inode */
168128a6d671SEric W. Biederman 
168228a6d671SEric W. Biederman 	inode = new_inode(sb);
168328a6d671SEric W. Biederman 	if (!inode)
168428a6d671SEric W. Biederman 		goto out;
168528a6d671SEric W. Biederman 
168628a6d671SEric W. Biederman 	/* Common stuff */
168728a6d671SEric W. Biederman 	ei = PROC_I(inode);
1688db978da8SAndreas Gruenbacher 	inode->i_mode = mode;
168985fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
1690078cd827SDeepa Dinamani 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
169128a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
169228a6d671SEric W. Biederman 
169328a6d671SEric W. Biederman 	/*
169428a6d671SEric W. Biederman 	 * grab the reference to task.
169528a6d671SEric W. Biederman 	 */
16961a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
169728a6d671SEric W. Biederman 	if (!ei->pid)
169828a6d671SEric W. Biederman 		goto out_unlock;
169928a6d671SEric W. Biederman 
170068eb94f1SEric W. Biederman 	task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
170128a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
170228a6d671SEric W. Biederman 
170328a6d671SEric W. Biederman out:
170428a6d671SEric W. Biederman 	return inode;
170528a6d671SEric W. Biederman 
170628a6d671SEric W. Biederman out_unlock:
170728a6d671SEric W. Biederman 	iput(inode);
170828a6d671SEric W. Biederman 	return NULL;
170928a6d671SEric W. Biederman }
171028a6d671SEric W. Biederman 
1711a528d35eSDavid Howells int pid_getattr(const struct path *path, struct kstat *stat,
1712a528d35eSDavid Howells 		u32 request_mask, unsigned int query_flags)
171328a6d671SEric W. Biederman {
1714a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
171576f668beSChristoph Hellwig 	struct pid_namespace *pid = proc_pid_ns(inode);
171628a6d671SEric W. Biederman 	struct task_struct *task;
1717c69e8d9cSDavid Howells 
171828a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
171928a6d671SEric W. Biederman 
1720dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1721dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
172294116929SAlexey Dobriyan 	rcu_read_lock();
172328a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
172428a6d671SEric W. Biederman 	if (task) {
1725796f571bSLafcadio Wluiki 		if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
17260499680aSVasiliy Kulikov 			rcu_read_unlock();
17270499680aSVasiliy Kulikov 			/*
17280499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
17290499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
17300499680aSVasiliy Kulikov 			 */
17310499680aSVasiliy Kulikov 			return -ENOENT;
17320499680aSVasiliy Kulikov 		}
173368eb94f1SEric W. Biederman 		task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
173428a6d671SEric W. Biederman 	}
173528a6d671SEric W. Biederman 	rcu_read_unlock();
173628a6d671SEric W. Biederman 	return 0;
173728a6d671SEric W. Biederman }
173828a6d671SEric W. Biederman 
173928a6d671SEric W. Biederman /* dentry stuff */
174028a6d671SEric W. Biederman 
174128a6d671SEric W. Biederman /*
17421bbc5513SAl Viro  * Set <pid>/... inode ownership (can change due to setuid(), etc.)
17431bbc5513SAl Viro  */
17441bbc5513SAl Viro void pid_update_inode(struct task_struct *task, struct inode *inode)
17451bbc5513SAl Viro {
17461bbc5513SAl Viro 	task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
17471bbc5513SAl Viro 
17481bbc5513SAl Viro 	inode->i_mode &= ~(S_ISUID | S_ISGID);
17491bbc5513SAl Viro 	security_task_to_inode(task, inode);
17501bbc5513SAl Viro }
17511bbc5513SAl Viro 
17521bbc5513SAl Viro /*
175328a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
175428a6d671SEric W. Biederman  * performed a setuid(), etc.
175528a6d671SEric W. Biederman  *
175628a6d671SEric W. Biederman  */
17571bbc5513SAl Viro static int pid_revalidate(struct dentry *dentry, unsigned int flags)
175828a6d671SEric W. Biederman {
175934286d66SNick Piggin 	struct inode *inode;
176034286d66SNick Piggin 	struct task_struct *task;
1761c69e8d9cSDavid Howells 
17620b728e19SAl Viro 	if (flags & LOOKUP_RCU)
176334286d66SNick Piggin 		return -ECHILD;
176434286d66SNick Piggin 
17652b0143b5SDavid Howells 	inode = d_inode(dentry);
176634286d66SNick Piggin 	task = get_proc_task(inode);
176734286d66SNick Piggin 
176828a6d671SEric W. Biederman 	if (task) {
17691bbc5513SAl Viro 		pid_update_inode(task, inode);
177028a6d671SEric W. Biederman 		put_task_struct(task);
177128a6d671SEric W. Biederman 		return 1;
177228a6d671SEric W. Biederman 	}
177328a6d671SEric W. Biederman 	return 0;
177428a6d671SEric W. Biederman }
177528a6d671SEric W. Biederman 
1776d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode)
1777d855a4b7SOleg Nesterov {
1778d855a4b7SOleg Nesterov 	return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1779d855a4b7SOleg Nesterov }
1780d855a4b7SOleg Nesterov 
17811dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry)
17821dd704b6SDavid Howells {
17831dd704b6SDavid Howells 	/* Is the task we represent dead?
17841dd704b6SDavid Howells 	 * If so, then don't put the dentry on the lru list,
17851dd704b6SDavid Howells 	 * kill it immediately.
17861dd704b6SDavid Howells 	 */
17872b0143b5SDavid Howells 	return proc_inode_is_dead(d_inode(dentry));
17881dd704b6SDavid Howells }
17891dd704b6SDavid Howells 
17906b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
179128a6d671SEric W. Biederman {
179228a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
179328a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
179428a6d671SEric W. Biederman };
179528a6d671SEric W. Biederman 
179628a6d671SEric W. Biederman /* Lookups */
179728a6d671SEric W. Biederman 
17981c0d04c9SEric W. Biederman /*
17991c0d04c9SEric W. Biederman  * Fill a directory entry.
18001c0d04c9SEric W. Biederman  *
18011c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
18021c0d04c9SEric W. Biederman  * file type from dcache entry.
18031c0d04c9SEric W. Biederman  *
18041c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
18051c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
18061c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
18071c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
18081c0d04c9SEric W. Biederman  * by stat.
18091c0d04c9SEric W. Biederman  */
1810f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1811a4ef3895SAlexey Dobriyan 	const char *name, unsigned int len,
1812c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
181361a28784SEric W. Biederman {
1814f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
18151df98b8bSAl Viro 	struct qstr qname = QSTR_INIT(name, len);
181661a28784SEric W. Biederman 	struct inode *inode;
18170168b9e3SAl Viro 	unsigned type = DT_UNKNOWN;
18180168b9e3SAl Viro 	ino_t ino = 1;
181961a28784SEric W. Biederman 
18201df98b8bSAl Viro 	child = d_hash_and_lookup(dir, &qname);
182161a28784SEric W. Biederman 	if (!child) {
18223781764bSAl Viro 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
18233781764bSAl Viro 		child = d_alloc_parallel(dir, &qname, &wq);
18243781764bSAl Viro 		if (IS_ERR(child))
182561a28784SEric W. Biederman 			goto end_instantiate;
18263781764bSAl Viro 		if (d_in_lookup(child)) {
18270168b9e3SAl Viro 			struct dentry *res;
18280168b9e3SAl Viro 			res = instantiate(child, task, ptr);
18293781764bSAl Viro 			d_lookup_done(child);
18300168b9e3SAl Viro 			if (unlikely(res)) {
18310168b9e3SAl Viro 				dput(child);
18320168b9e3SAl Viro 				child = res;
1833d85b399bSAl Viro 				if (IS_ERR(child))
1834d85b399bSAl Viro 					goto end_instantiate;
18351df98b8bSAl Viro 			}
18361df98b8bSAl Viro 		}
18373781764bSAl Viro 	}
18382b0143b5SDavid Howells 	inode = d_inode(child);
183961a28784SEric W. Biederman 	ino = inode->i_ino;
184061a28784SEric W. Biederman 	type = inode->i_mode >> 12;
184161a28784SEric W. Biederman 	dput(child);
1842d85b399bSAl Viro end_instantiate:
1843f0c3b509SAl Viro 	return dir_emit(ctx, name, len, ino, type);
184461a28784SEric W. Biederman }
184561a28784SEric W. Biederman 
1846640708a2SPavel Emelyanov /*
1847640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1848640708a2SPavel Emelyanov  * which represent vma start and end addresses.
1849640708a2SPavel Emelyanov  */
1850640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
1851640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
1852640708a2SPavel Emelyanov {
1853ac7f1061SAlexey Dobriyan 	const char *str = dentry->d_name.name;
1854ac7f1061SAlexey Dobriyan 	unsigned long long sval, eval;
1855ac7f1061SAlexey Dobriyan 	unsigned int len;
1856ac7f1061SAlexey Dobriyan 
185735318db5SAlexey Dobriyan 	if (str[0] == '0' && str[1] != '-')
185835318db5SAlexey Dobriyan 		return -EINVAL;
1859ac7f1061SAlexey Dobriyan 	len = _parse_integer(str, 16, &sval);
1860ac7f1061SAlexey Dobriyan 	if (len & KSTRTOX_OVERFLOW)
1861640708a2SPavel Emelyanov 		return -EINVAL;
1862ac7f1061SAlexey Dobriyan 	if (sval != (unsigned long)sval)
1863ac7f1061SAlexey Dobriyan 		return -EINVAL;
1864ac7f1061SAlexey Dobriyan 	str += len;
1865ac7f1061SAlexey Dobriyan 
1866ac7f1061SAlexey Dobriyan 	if (*str != '-')
1867ac7f1061SAlexey Dobriyan 		return -EINVAL;
1868ac7f1061SAlexey Dobriyan 	str++;
1869ac7f1061SAlexey Dobriyan 
187035318db5SAlexey Dobriyan 	if (str[0] == '0' && str[1])
187135318db5SAlexey Dobriyan 		return -EINVAL;
1872ac7f1061SAlexey Dobriyan 	len = _parse_integer(str, 16, &eval);
1873ac7f1061SAlexey Dobriyan 	if (len & KSTRTOX_OVERFLOW)
1874ac7f1061SAlexey Dobriyan 		return -EINVAL;
1875ac7f1061SAlexey Dobriyan 	if (eval != (unsigned long)eval)
1876ac7f1061SAlexey Dobriyan 		return -EINVAL;
1877ac7f1061SAlexey Dobriyan 	str += len;
1878ac7f1061SAlexey Dobriyan 
1879ac7f1061SAlexey Dobriyan 	if (*str != '\0')
1880ac7f1061SAlexey Dobriyan 		return -EINVAL;
1881ac7f1061SAlexey Dobriyan 
1882ac7f1061SAlexey Dobriyan 	*start = sval;
1883ac7f1061SAlexey Dobriyan 	*end = eval;
1884640708a2SPavel Emelyanov 
1885640708a2SPavel Emelyanov 	return 0;
1886640708a2SPavel Emelyanov }
1887640708a2SPavel Emelyanov 
18880b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1889640708a2SPavel Emelyanov {
1890640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1891640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
1892640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
1893640708a2SPavel Emelyanov 	struct task_struct *task;
1894640708a2SPavel Emelyanov 	struct inode *inode;
1895640708a2SPavel Emelyanov 	int status = 0;
1896640708a2SPavel Emelyanov 
18970b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1898640708a2SPavel Emelyanov 		return -ECHILD;
1899640708a2SPavel Emelyanov 
19002b0143b5SDavid Howells 	inode = d_inode(dentry);
1901640708a2SPavel Emelyanov 	task = get_proc_task(inode);
1902640708a2SPavel Emelyanov 	if (!task)
1903640708a2SPavel Emelyanov 		goto out_notask;
1904640708a2SPavel Emelyanov 
1905caaee623SJann Horn 	mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
19062344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
1907640708a2SPavel Emelyanov 		goto out;
1908640708a2SPavel Emelyanov 
1909640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1910640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
1911640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1912640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
1913640708a2SPavel Emelyanov 	}
1914640708a2SPavel Emelyanov 
1915640708a2SPavel Emelyanov 	mmput(mm);
1916640708a2SPavel Emelyanov 
1917640708a2SPavel Emelyanov 	if (exact_vma_exists) {
191868eb94f1SEric W. Biederman 		task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
191968eb94f1SEric W. Biederman 
1920640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
1921640708a2SPavel Emelyanov 		status = 1;
1922640708a2SPavel Emelyanov 	}
1923640708a2SPavel Emelyanov 
1924640708a2SPavel Emelyanov out:
1925640708a2SPavel Emelyanov 	put_task_struct(task);
1926640708a2SPavel Emelyanov 
1927640708a2SPavel Emelyanov out_notask:
1928640708a2SPavel Emelyanov 	return status;
1929640708a2SPavel Emelyanov }
1930640708a2SPavel Emelyanov 
1931640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
1932640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
1933640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
1934640708a2SPavel Emelyanov };
1935640708a2SPavel Emelyanov 
19366b255391SAl Viro static int map_files_get_link(struct dentry *dentry, struct path *path)
1937640708a2SPavel Emelyanov {
1938640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1939640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1940640708a2SPavel Emelyanov 	struct task_struct *task;
1941640708a2SPavel Emelyanov 	struct mm_struct *mm;
1942640708a2SPavel Emelyanov 	int rc;
1943640708a2SPavel Emelyanov 
1944640708a2SPavel Emelyanov 	rc = -ENOENT;
19452b0143b5SDavid Howells 	task = get_proc_task(d_inode(dentry));
1946640708a2SPavel Emelyanov 	if (!task)
1947640708a2SPavel Emelyanov 		goto out;
1948640708a2SPavel Emelyanov 
1949640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1950640708a2SPavel Emelyanov 	put_task_struct(task);
1951640708a2SPavel Emelyanov 	if (!mm)
1952640708a2SPavel Emelyanov 		goto out;
1953640708a2SPavel Emelyanov 
1954640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1955640708a2SPavel Emelyanov 	if (rc)
1956640708a2SPavel Emelyanov 		goto out_mmput;
1957640708a2SPavel Emelyanov 
195870335abbSArtem Fetishev 	rc = -ENOENT;
1959640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1960640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1961640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
1962640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
1963640708a2SPavel Emelyanov 		path_get(path);
1964640708a2SPavel Emelyanov 		rc = 0;
1965640708a2SPavel Emelyanov 	}
1966640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1967640708a2SPavel Emelyanov 
1968640708a2SPavel Emelyanov out_mmput:
1969640708a2SPavel Emelyanov 	mmput(mm);
1970640708a2SPavel Emelyanov out:
1971640708a2SPavel Emelyanov 	return rc;
1972640708a2SPavel Emelyanov }
1973640708a2SPavel Emelyanov 
1974640708a2SPavel Emelyanov struct map_files_info {
197520d28cdeSAlexey Dobriyan 	unsigned long	start;
197620d28cdeSAlexey Dobriyan 	unsigned long	end;
19777b540d06SAl Viro 	fmode_t		mode;
1978640708a2SPavel Emelyanov };
1979640708a2SPavel Emelyanov 
1980bdb4d100SCalvin Owens /*
1981bdb4d100SCalvin Owens  * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1982bdb4d100SCalvin Owens  * symlinks may be used to bypass permissions on ancestor directories in the
1983bdb4d100SCalvin Owens  * path to the file in question.
1984bdb4d100SCalvin Owens  */
1985bdb4d100SCalvin Owens static const char *
19866b255391SAl Viro proc_map_files_get_link(struct dentry *dentry,
1987fceef393SAl Viro 			struct inode *inode,
1988fceef393SAl Viro 		        struct delayed_call *done)
1989bdb4d100SCalvin Owens {
1990bdb4d100SCalvin Owens 	if (!capable(CAP_SYS_ADMIN))
1991bdb4d100SCalvin Owens 		return ERR_PTR(-EPERM);
1992bdb4d100SCalvin Owens 
1993fceef393SAl Viro 	return proc_pid_get_link(dentry, inode, done);
1994bdb4d100SCalvin Owens }
1995bdb4d100SCalvin Owens 
1996bdb4d100SCalvin Owens /*
19976b255391SAl Viro  * Identical to proc_pid_link_inode_operations except for get_link()
1998bdb4d100SCalvin Owens  */
1999bdb4d100SCalvin Owens static const struct inode_operations proc_map_files_link_inode_operations = {
2000bdb4d100SCalvin Owens 	.readlink	= proc_pid_readlink,
20016b255391SAl Viro 	.get_link	= proc_map_files_get_link,
2002bdb4d100SCalvin Owens 	.setattr	= proc_setattr,
2003bdb4d100SCalvin Owens };
2004bdb4d100SCalvin Owens 
20050168b9e3SAl Viro static struct dentry *
20060168b9e3SAl Viro proc_map_files_instantiate(struct dentry *dentry,
2007640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
2008640708a2SPavel Emelyanov {
20097b540d06SAl Viro 	fmode_t mode = (fmode_t)(unsigned long)ptr;
2010640708a2SPavel Emelyanov 	struct proc_inode *ei;
2011640708a2SPavel Emelyanov 	struct inode *inode;
2012640708a2SPavel Emelyanov 
20130168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2014db978da8SAndreas Gruenbacher 				    ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2015db978da8SAndreas Gruenbacher 				    ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2016640708a2SPavel Emelyanov 	if (!inode)
20170168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
2018640708a2SPavel Emelyanov 
2019640708a2SPavel Emelyanov 	ei = PROC_I(inode);
20206b255391SAl Viro 	ei->op.proc_get_link = map_files_get_link;
2021640708a2SPavel Emelyanov 
2022bdb4d100SCalvin Owens 	inode->i_op = &proc_map_files_link_inode_operations;
2023640708a2SPavel Emelyanov 	inode->i_size = 64;
2024640708a2SPavel Emelyanov 
2025640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
20260168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
2027640708a2SPavel Emelyanov }
2028640708a2SPavel Emelyanov 
2029640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
203000cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
2031640708a2SPavel Emelyanov {
2032640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2033640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2034640708a2SPavel Emelyanov 	struct task_struct *task;
20350168b9e3SAl Viro 	struct dentry *result;
2036640708a2SPavel Emelyanov 	struct mm_struct *mm;
2037640708a2SPavel Emelyanov 
20380168b9e3SAl Viro 	result = ERR_PTR(-ENOENT);
2039640708a2SPavel Emelyanov 	task = get_proc_task(dir);
2040640708a2SPavel Emelyanov 	if (!task)
2041640708a2SPavel Emelyanov 		goto out;
2042640708a2SPavel Emelyanov 
20430168b9e3SAl Viro 	result = ERR_PTR(-EACCES);
2044caaee623SJann Horn 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2045640708a2SPavel Emelyanov 		goto out_put_task;
2046640708a2SPavel Emelyanov 
20470168b9e3SAl Viro 	result = ERR_PTR(-ENOENT);
2048640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2049eb94cd96SCyrill Gorcunov 		goto out_put_task;
2050640708a2SPavel Emelyanov 
2051640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2052640708a2SPavel Emelyanov 	if (!mm)
2053eb94cd96SCyrill Gorcunov 		goto out_put_task;
2054640708a2SPavel Emelyanov 
2055640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2056640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2057640708a2SPavel Emelyanov 	if (!vma)
2058640708a2SPavel Emelyanov 		goto out_no_vma;
2059640708a2SPavel Emelyanov 
206005f56484SStanislav Kinsbursky 	if (vma->vm_file)
20610168b9e3SAl Viro 		result = proc_map_files_instantiate(dentry, task,
20627b540d06SAl Viro 				(void *)(unsigned long)vma->vm_file->f_mode);
2063640708a2SPavel Emelyanov 
2064640708a2SPavel Emelyanov out_no_vma:
2065640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2066640708a2SPavel Emelyanov 	mmput(mm);
2067640708a2SPavel Emelyanov out_put_task:
2068640708a2SPavel Emelyanov 	put_task_struct(task);
2069640708a2SPavel Emelyanov out:
20700168b9e3SAl Viro 	return result;
2071640708a2SPavel Emelyanov }
2072640708a2SPavel Emelyanov 
2073640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
2074640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
2075640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
2076640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
2077640708a2SPavel Emelyanov };
2078640708a2SPavel Emelyanov 
2079640708a2SPavel Emelyanov static int
2080f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2081640708a2SPavel Emelyanov {
2082640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2083640708a2SPavel Emelyanov 	struct task_struct *task;
2084640708a2SPavel Emelyanov 	struct mm_struct *mm;
2085f0c3b509SAl Viro 	unsigned long nr_files, pos, i;
208694f8f3b0SKent Overstreet 	GENRADIX(struct map_files_info) fa;
2087f0c3b509SAl Viro 	struct map_files_info *p;
2088640708a2SPavel Emelyanov 	int ret;
2089640708a2SPavel Emelyanov 
209094f8f3b0SKent Overstreet 	genradix_init(&fa);
209194f8f3b0SKent Overstreet 
2092640708a2SPavel Emelyanov 	ret = -ENOENT;
2093f0c3b509SAl Viro 	task = get_proc_task(file_inode(file));
2094640708a2SPavel Emelyanov 	if (!task)
2095640708a2SPavel Emelyanov 		goto out;
2096640708a2SPavel Emelyanov 
2097640708a2SPavel Emelyanov 	ret = -EACCES;
2098caaee623SJann Horn 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2099640708a2SPavel Emelyanov 		goto out_put_task;
2100640708a2SPavel Emelyanov 
2101640708a2SPavel Emelyanov 	ret = 0;
2102f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
2103eb94cd96SCyrill Gorcunov 		goto out_put_task;
2104640708a2SPavel Emelyanov 
2105640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2106640708a2SPavel Emelyanov 	if (!mm)
2107eb94cd96SCyrill Gorcunov 		goto out_put_task;
2108640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2109640708a2SPavel Emelyanov 
2110640708a2SPavel Emelyanov 	nr_files = 0;
2111640708a2SPavel Emelyanov 
2112640708a2SPavel Emelyanov 	/*
2113640708a2SPavel Emelyanov 	 * We need two passes here:
2114640708a2SPavel Emelyanov 	 *
2115640708a2SPavel Emelyanov 	 *  1) Collect vmas of mapped files with mmap_sem taken
2116640708a2SPavel Emelyanov 	 *  2) Release mmap_sem and instantiate entries
2117640708a2SPavel Emelyanov 	 *
2118640708a2SPavel Emelyanov 	 * otherwise we get lockdep complained, since filldir()
2119640708a2SPavel Emelyanov 	 * routine might require mmap_sem taken in might_fault().
2120640708a2SPavel Emelyanov 	 */
2121640708a2SPavel Emelyanov 
2122640708a2SPavel Emelyanov 	for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2123640708a2SPavel Emelyanov 		if (!vma->vm_file)
2124640708a2SPavel Emelyanov 			continue;
2125f0c3b509SAl Viro 		if (++pos <= ctx->pos)
2126640708a2SPavel Emelyanov 			continue;
2127640708a2SPavel Emelyanov 
212894f8f3b0SKent Overstreet 		p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
212994f8f3b0SKent Overstreet 		if (!p) {
213094f8f3b0SKent Overstreet 			ret = -ENOMEM;
213194f8f3b0SKent Overstreet 			up_read(&mm->mmap_sem);
213294f8f3b0SKent Overstreet 			mmput(mm);
213394f8f3b0SKent Overstreet 			goto out_put_task;
2134640708a2SPavel Emelyanov 		}
213594f8f3b0SKent Overstreet 
213694f8f3b0SKent Overstreet 		p->start = vma->vm_start;
213794f8f3b0SKent Overstreet 		p->end = vma->vm_end;
213894f8f3b0SKent Overstreet 		p->mode = vma->vm_file->f_mode;
2139640708a2SPavel Emelyanov 	}
2140640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2141fe079a5eSAlexey Dobriyan 	mmput(mm);
2142640708a2SPavel Emelyanov 
2143640708a2SPavel Emelyanov 	for (i = 0; i < nr_files; i++) {
214420d28cdeSAlexey Dobriyan 		char buf[4 * sizeof(long) + 2];	/* max: %lx-%lx\0 */
214520d28cdeSAlexey Dobriyan 		unsigned int len;
214620d28cdeSAlexey Dobriyan 
214794f8f3b0SKent Overstreet 		p = genradix_ptr(&fa, i);
214820d28cdeSAlexey Dobriyan 		len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2149f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
215020d28cdeSAlexey Dobriyan 				      buf, len,
2151640708a2SPavel Emelyanov 				      proc_map_files_instantiate,
21527b540d06SAl Viro 				      task,
2153f0c3b509SAl Viro 				      (void *)(unsigned long)p->mode))
2154640708a2SPavel Emelyanov 			break;
2155f0c3b509SAl Viro 		ctx->pos++;
2156640708a2SPavel Emelyanov 	}
2157640708a2SPavel Emelyanov 
2158640708a2SPavel Emelyanov out_put_task:
2159640708a2SPavel Emelyanov 	put_task_struct(task);
2160640708a2SPavel Emelyanov out:
216194f8f3b0SKent Overstreet 	genradix_free(&fa);
2162640708a2SPavel Emelyanov 	return ret;
2163640708a2SPavel Emelyanov }
2164640708a2SPavel Emelyanov 
2165640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2166640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2167f50752eaSAl Viro 	.iterate_shared	= proc_map_files_readdir,
2168f50752eaSAl Viro 	.llseek		= generic_file_llseek,
2169640708a2SPavel Emelyanov };
2170640708a2SPavel Emelyanov 
2171b18b6a9cSNicolas Pitre #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
217248f6a7a5SPavel Emelyanov struct timers_private {
217348f6a7a5SPavel Emelyanov 	struct pid *pid;
217448f6a7a5SPavel Emelyanov 	struct task_struct *task;
217548f6a7a5SPavel Emelyanov 	struct sighand_struct *sighand;
217657b8015eSPavel Emelyanov 	struct pid_namespace *ns;
217748f6a7a5SPavel Emelyanov 	unsigned long flags;
217848f6a7a5SPavel Emelyanov };
217948f6a7a5SPavel Emelyanov 
218048f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos)
218148f6a7a5SPavel Emelyanov {
218248f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
218348f6a7a5SPavel Emelyanov 
218448f6a7a5SPavel Emelyanov 	tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
218548f6a7a5SPavel Emelyanov 	if (!tp->task)
218648f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
218748f6a7a5SPavel Emelyanov 
218848f6a7a5SPavel Emelyanov 	tp->sighand = lock_task_sighand(tp->task, &tp->flags);
218948f6a7a5SPavel Emelyanov 	if (!tp->sighand)
219048f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
219148f6a7a5SPavel Emelyanov 
219248f6a7a5SPavel Emelyanov 	return seq_list_start(&tp->task->signal->posix_timers, *pos);
219348f6a7a5SPavel Emelyanov }
219448f6a7a5SPavel Emelyanov 
219548f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
219648f6a7a5SPavel Emelyanov {
219748f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
219848f6a7a5SPavel Emelyanov 	return seq_list_next(v, &tp->task->signal->posix_timers, pos);
219948f6a7a5SPavel Emelyanov }
220048f6a7a5SPavel Emelyanov 
220148f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v)
220248f6a7a5SPavel Emelyanov {
220348f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
220448f6a7a5SPavel Emelyanov 
220548f6a7a5SPavel Emelyanov 	if (tp->sighand) {
220648f6a7a5SPavel Emelyanov 		unlock_task_sighand(tp->task, &tp->flags);
220748f6a7a5SPavel Emelyanov 		tp->sighand = NULL;
220848f6a7a5SPavel Emelyanov 	}
220948f6a7a5SPavel Emelyanov 
221048f6a7a5SPavel Emelyanov 	if (tp->task) {
221148f6a7a5SPavel Emelyanov 		put_task_struct(tp->task);
221248f6a7a5SPavel Emelyanov 		tp->task = NULL;
221348f6a7a5SPavel Emelyanov 	}
221448f6a7a5SPavel Emelyanov }
221548f6a7a5SPavel Emelyanov 
221648f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v)
221748f6a7a5SPavel Emelyanov {
221848f6a7a5SPavel Emelyanov 	struct k_itimer *timer;
221957b8015eSPavel Emelyanov 	struct timers_private *tp = m->private;
222057b8015eSPavel Emelyanov 	int notify;
2221cedbccabSAlexey Dobriyan 	static const char * const nstr[] = {
222257b8015eSPavel Emelyanov 		[SIGEV_SIGNAL] = "signal",
222357b8015eSPavel Emelyanov 		[SIGEV_NONE] = "none",
222457b8015eSPavel Emelyanov 		[SIGEV_THREAD] = "thread",
222557b8015eSPavel Emelyanov 	};
222648f6a7a5SPavel Emelyanov 
222748f6a7a5SPavel Emelyanov 	timer = list_entry((struct list_head *)v, struct k_itimer, list);
222857b8015eSPavel Emelyanov 	notify = timer->it_sigev_notify;
222957b8015eSPavel Emelyanov 
223048f6a7a5SPavel Emelyanov 	seq_printf(m, "ID: %d\n", timer->it_id);
2231ba3edf1fSLinus Torvalds 	seq_printf(m, "signal: %d/%px\n",
223225ce3191SJoe Perches 		   timer->sigq->info.si_signo,
223357b8015eSPavel Emelyanov 		   timer->sigq->info.si_value.sival_ptr);
223457b8015eSPavel Emelyanov 	seq_printf(m, "notify: %s/%s.%d\n",
223557b8015eSPavel Emelyanov 		   nstr[notify & ~SIGEV_THREAD_ID],
223657b8015eSPavel Emelyanov 		   (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
223757b8015eSPavel Emelyanov 		   pid_nr_ns(timer->it_pid, tp->ns));
223815ef0298SPavel Tikhomirov 	seq_printf(m, "ClockID: %d\n", timer->it_clock);
223948f6a7a5SPavel Emelyanov 
224048f6a7a5SPavel Emelyanov 	return 0;
224148f6a7a5SPavel Emelyanov }
224248f6a7a5SPavel Emelyanov 
224348f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = {
224448f6a7a5SPavel Emelyanov 	.start	= timers_start,
224548f6a7a5SPavel Emelyanov 	.next	= timers_next,
224648f6a7a5SPavel Emelyanov 	.stop	= timers_stop,
224748f6a7a5SPavel Emelyanov 	.show	= show_timer,
224848f6a7a5SPavel Emelyanov };
224948f6a7a5SPavel Emelyanov 
225048f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file)
225148f6a7a5SPavel Emelyanov {
225248f6a7a5SPavel Emelyanov 	struct timers_private *tp;
225348f6a7a5SPavel Emelyanov 
225448f6a7a5SPavel Emelyanov 	tp = __seq_open_private(file, &proc_timers_seq_ops,
225548f6a7a5SPavel Emelyanov 			sizeof(struct timers_private));
225648f6a7a5SPavel Emelyanov 	if (!tp)
225748f6a7a5SPavel Emelyanov 		return -ENOMEM;
225848f6a7a5SPavel Emelyanov 
225948f6a7a5SPavel Emelyanov 	tp->pid = proc_pid(inode);
226076f668beSChristoph Hellwig 	tp->ns = proc_pid_ns(inode);
226148f6a7a5SPavel Emelyanov 	return 0;
226248f6a7a5SPavel Emelyanov }
226348f6a7a5SPavel Emelyanov 
226448f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = {
226548f6a7a5SPavel Emelyanov 	.open		= proc_timers_open,
226648f6a7a5SPavel Emelyanov 	.read		= seq_read,
226748f6a7a5SPavel Emelyanov 	.llseek		= seq_lseek,
226848f6a7a5SPavel Emelyanov 	.release	= seq_release_private,
226948f6a7a5SPavel Emelyanov };
2270b5946beaSEric Engestrom #endif
2271640708a2SPavel Emelyanov 
22725de23d43SJohn Stultz static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
22735de23d43SJohn Stultz 					size_t count, loff_t *offset)
22745de23d43SJohn Stultz {
22755de23d43SJohn Stultz 	struct inode *inode = file_inode(file);
22765de23d43SJohn Stultz 	struct task_struct *p;
22775de23d43SJohn Stultz 	u64 slack_ns;
22785de23d43SJohn Stultz 	int err;
22795de23d43SJohn Stultz 
22805de23d43SJohn Stultz 	err = kstrtoull_from_user(buf, count, 10, &slack_ns);
22815de23d43SJohn Stultz 	if (err < 0)
22825de23d43SJohn Stultz 		return err;
22835de23d43SJohn Stultz 
22845de23d43SJohn Stultz 	p = get_proc_task(inode);
22855de23d43SJohn Stultz 	if (!p)
22865de23d43SJohn Stultz 		return -ESRCH;
22875de23d43SJohn Stultz 
22884b2bd5feSJohn Stultz 	if (p != current) {
22898da0b4f6SBenjamin Gordon 		rcu_read_lock();
22908da0b4f6SBenjamin Gordon 		if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
22918da0b4f6SBenjamin Gordon 			rcu_read_unlock();
22927abbaf94SJohn Stultz 			count = -EPERM;
22937abbaf94SJohn Stultz 			goto out;
22947abbaf94SJohn Stultz 		}
22958da0b4f6SBenjamin Gordon 		rcu_read_unlock();
22967abbaf94SJohn Stultz 
2297904763e1SJohn Stultz 		err = security_task_setscheduler(p);
2298904763e1SJohn Stultz 		if (err) {
2299904763e1SJohn Stultz 			count = err;
2300904763e1SJohn Stultz 			goto out;
2301904763e1SJohn Stultz 		}
23024b2bd5feSJohn Stultz 	}
2303904763e1SJohn Stultz 
23045de23d43SJohn Stultz 	task_lock(p);
23055de23d43SJohn Stultz 	if (slack_ns == 0)
23065de23d43SJohn Stultz 		p->timer_slack_ns = p->default_timer_slack_ns;
23075de23d43SJohn Stultz 	else
23085de23d43SJohn Stultz 		p->timer_slack_ns = slack_ns;
23095de23d43SJohn Stultz 	task_unlock(p);
23105de23d43SJohn Stultz 
23117abbaf94SJohn Stultz out:
23125de23d43SJohn Stultz 	put_task_struct(p);
23135de23d43SJohn Stultz 
23145de23d43SJohn Stultz 	return count;
23155de23d43SJohn Stultz }
23165de23d43SJohn Stultz 
23175de23d43SJohn Stultz static int timerslack_ns_show(struct seq_file *m, void *v)
23185de23d43SJohn Stultz {
23195de23d43SJohn Stultz 	struct inode *inode = m->private;
23205de23d43SJohn Stultz 	struct task_struct *p;
23215de23d43SJohn Stultz 	int err = 0;
23225de23d43SJohn Stultz 
23235de23d43SJohn Stultz 	p = get_proc_task(inode);
23245de23d43SJohn Stultz 	if (!p)
23255de23d43SJohn Stultz 		return -ESRCH;
23265de23d43SJohn Stultz 
23274b2bd5feSJohn Stultz 	if (p != current) {
23288da0b4f6SBenjamin Gordon 		rcu_read_lock();
23298da0b4f6SBenjamin Gordon 		if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
23308da0b4f6SBenjamin Gordon 			rcu_read_unlock();
23317abbaf94SJohn Stultz 			err = -EPERM;
23327abbaf94SJohn Stultz 			goto out;
23337abbaf94SJohn Stultz 		}
23348da0b4f6SBenjamin Gordon 		rcu_read_unlock();
23358da0b4f6SBenjamin Gordon 
2336904763e1SJohn Stultz 		err = security_task_getscheduler(p);
2337904763e1SJohn Stultz 		if (err)
2338904763e1SJohn Stultz 			goto out;
23394b2bd5feSJohn Stultz 	}
2340904763e1SJohn Stultz 
23415de23d43SJohn Stultz 	task_lock(p);
23425de23d43SJohn Stultz 	seq_printf(m, "%llu\n", p->timer_slack_ns);
23435de23d43SJohn Stultz 	task_unlock(p);
23445de23d43SJohn Stultz 
23457abbaf94SJohn Stultz out:
23465de23d43SJohn Stultz 	put_task_struct(p);
23475de23d43SJohn Stultz 
23485de23d43SJohn Stultz 	return err;
23495de23d43SJohn Stultz }
23505de23d43SJohn Stultz 
23515de23d43SJohn Stultz static int timerslack_ns_open(struct inode *inode, struct file *filp)
23525de23d43SJohn Stultz {
23535de23d43SJohn Stultz 	return single_open(filp, timerslack_ns_show, inode);
23545de23d43SJohn Stultz }
23555de23d43SJohn Stultz 
23565de23d43SJohn Stultz static const struct file_operations proc_pid_set_timerslack_ns_operations = {
23575de23d43SJohn Stultz 	.open		= timerslack_ns_open,
23585de23d43SJohn Stultz 	.read		= seq_read,
23595de23d43SJohn Stultz 	.write		= timerslack_ns_write,
23605de23d43SJohn Stultz 	.llseek		= seq_lseek,
23615de23d43SJohn Stultz 	.release	= single_release,
23625de23d43SJohn Stultz };
23635de23d43SJohn Stultz 
23640168b9e3SAl Viro static struct dentry *proc_pident_instantiate(struct dentry *dentry,
23650168b9e3SAl Viro 	struct task_struct *task, const void *ptr)
2366444ceed8SEric W. Biederman {
2367c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2368444ceed8SEric W. Biederman 	struct inode *inode;
2369444ceed8SEric W. Biederman 	struct proc_inode *ei;
2370444ceed8SEric W. Biederman 
23710168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2372444ceed8SEric W. Biederman 	if (!inode)
23730168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
2374444ceed8SEric W. Biederman 
2375444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2376444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2377bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2378444ceed8SEric W. Biederman 	if (p->iop)
2379444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2380444ceed8SEric W. Biederman 	if (p->fop)
2381444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2382444ceed8SEric W. Biederman 	ei->op = p->op;
23831bbc5513SAl Viro 	pid_update_inode(task, inode);
2384fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
23850168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
2386444ceed8SEric W. Biederman }
2387444ceed8SEric W. Biederman 
23881da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
23891da177e4SLinus Torvalds 					 struct dentry *dentry,
2390d5a572a4SAlexey Dobriyan 					 const struct pid_entry *p,
2391d5a572a4SAlexey Dobriyan 					 const struct pid_entry *end)
23921da177e4SLinus Torvalds {
239399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
23940168b9e3SAl Viro 	struct dentry *res = ERR_PTR(-ENOENT);
23951da177e4SLinus Torvalds 
239699f89551SEric W. Biederman 	if (!task)
239799f89551SEric W. Biederman 		goto out_no_task;
23981da177e4SLinus Torvalds 
239920cdc894SEric W. Biederman 	/*
240020cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
240120cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
240220cdc894SEric W. Biederman 	 */
2403d5a572a4SAlexey Dobriyan 	for (; p < end; p++) {
24041da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
24051da177e4SLinus Torvalds 			continue;
240626b95137SAlexey Dobriyan 		if (!memcmp(dentry->d_name.name, p->name, p->len)) {
240726b95137SAlexey Dobriyan 			res = proc_pident_instantiate(dentry, task, p);
24081da177e4SLinus Torvalds 			break;
24091da177e4SLinus Torvalds 		}
241026b95137SAlexey Dobriyan 	}
241199f89551SEric W. Biederman 	put_task_struct(task);
241299f89551SEric W. Biederman out_no_task:
24130168b9e3SAl Viro 	return res;
24141da177e4SLinus Torvalds }
24151da177e4SLinus Torvalds 
2416f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2417c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
241828a6d671SEric W. Biederman {
2419f0c3b509SAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
2420f0c3b509SAl Viro 	const struct pid_entry *p;
242128a6d671SEric W. Biederman 
242228a6d671SEric W. Biederman 	if (!task)
2423f0c3b509SAl Viro 		return -ENOENT;
242428a6d671SEric W. Biederman 
2425f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
242628a6d671SEric W. Biederman 		goto out;
24271da177e4SLinus Torvalds 
2428f0c3b509SAl Viro 	if (ctx->pos >= nents + 2)
2429f0c3b509SAl Viro 		goto out;
2430f0c3b509SAl Viro 
2431bac5f5d5SAlexey Dobriyan 	for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2432f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, p->name, p->len,
2433f0c3b509SAl Viro 				proc_pident_instantiate, task, p))
2434f0c3b509SAl Viro 			break;
2435f0c3b509SAl Viro 		ctx->pos++;
2436f0c3b509SAl Viro 	}
243728a6d671SEric W. Biederman out:
243861a28784SEric W. Biederman 	put_task_struct(task);
2439f0c3b509SAl Viro 	return 0;
24401da177e4SLinus Torvalds }
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
244328a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
244428a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
244528a6d671SEric W. Biederman {
2446496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
244704ff9708SAl Viro 	char *p = NULL;
244828a6d671SEric W. Biederman 	ssize_t length;
244928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
245028a6d671SEric W. Biederman 
245128a6d671SEric W. Biederman 	if (!task)
245204ff9708SAl Viro 		return -ESRCH;
245328a6d671SEric W. Biederman 
24546d9c939dSCasey Schaufler 	length = security_getprocattr(task, PROC_I(inode)->op.lsm,
24552fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
245604ff9708SAl Viro 				      &p);
245728a6d671SEric W. Biederman 	put_task_struct(task);
245804ff9708SAl Viro 	if (length > 0)
245904ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
246004ff9708SAl Viro 	kfree(p);
246128a6d671SEric W. Biederman 	return length;
246228a6d671SEric W. Biederman }
246328a6d671SEric W. Biederman 
246428a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
246528a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
246628a6d671SEric W. Biederman {
2467496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
246841089b6dSAlexey Dobriyan 	struct task_struct *task;
2469bb646cdbSAl Viro 	void *page;
247041089b6dSAlexey Dobriyan 	int rv;
247128a6d671SEric W. Biederman 
247241089b6dSAlexey Dobriyan 	rcu_read_lock();
247341089b6dSAlexey Dobriyan 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
247441089b6dSAlexey Dobriyan 	if (!task) {
247541089b6dSAlexey Dobriyan 		rcu_read_unlock();
247641089b6dSAlexey Dobriyan 		return -ESRCH;
247741089b6dSAlexey Dobriyan 	}
2478b21507e2SStephen Smalley 	/* A task may only write its own attributes. */
247941089b6dSAlexey Dobriyan 	if (current != task) {
248041089b6dSAlexey Dobriyan 		rcu_read_unlock();
248141089b6dSAlexey Dobriyan 		return -EACCES;
248241089b6dSAlexey Dobriyan 	}
248335a196beSPaul Moore 	/* Prevent changes to overridden credentials. */
248435a196beSPaul Moore 	if (current_cred() != current_real_cred()) {
248535a196beSPaul Moore 		rcu_read_unlock();
248635a196beSPaul Moore 		return -EBUSY;
248735a196beSPaul Moore 	}
248841089b6dSAlexey Dobriyan 	rcu_read_unlock();
2489b21507e2SStephen Smalley 
249028a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
249128a6d671SEric W. Biederman 		count = PAGE_SIZE;
249228a6d671SEric W. Biederman 
249328a6d671SEric W. Biederman 	/* No partial writes. */
249428a6d671SEric W. Biederman 	if (*ppos != 0)
249541089b6dSAlexey Dobriyan 		return -EINVAL;
249628a6d671SEric W. Biederman 
2497bb646cdbSAl Viro 	page = memdup_user(buf, count);
2498bb646cdbSAl Viro 	if (IS_ERR(page)) {
249941089b6dSAlexey Dobriyan 		rv = PTR_ERR(page);
250028a6d671SEric W. Biederman 		goto out;
2501bb646cdbSAl Viro 	}
250228a6d671SEric W. Biederman 
2503107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
250441089b6dSAlexey Dobriyan 	rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
250541089b6dSAlexey Dobriyan 	if (rv < 0)
2506107db7c7SDavid Howells 		goto out_free;
2507107db7c7SDavid Howells 
25086d9c939dSCasey Schaufler 	rv = security_setprocattr(PROC_I(inode)->op.lsm,
25096d9c939dSCasey Schaufler 				  file->f_path.dentry->d_name.name, page,
25106d9c939dSCasey Schaufler 				  count);
2511b21507e2SStephen Smalley 	mutex_unlock(&current->signal->cred_guard_mutex);
251228a6d671SEric W. Biederman out_free:
2513bb646cdbSAl Viro 	kfree(page);
251428a6d671SEric W. Biederman out:
251541089b6dSAlexey Dobriyan 	return rv;
251628a6d671SEric W. Biederman }
251728a6d671SEric W. Biederman 
251800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
251928a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
252028a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
252187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
252228a6d671SEric W. Biederman };
252328a6d671SEric W. Biederman 
25246d9c939dSCasey Schaufler #define LSM_DIR_OPS(LSM) \
25256d9c939dSCasey Schaufler static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
25266d9c939dSCasey Schaufler 			     struct dir_context *ctx) \
25276d9c939dSCasey Schaufler { \
25286d9c939dSCasey Schaufler 	return proc_pident_readdir(filp, ctx, \
25296d9c939dSCasey Schaufler 				   LSM##_attr_dir_stuff, \
25306d9c939dSCasey Schaufler 				   ARRAY_SIZE(LSM##_attr_dir_stuff)); \
25316d9c939dSCasey Schaufler } \
25326d9c939dSCasey Schaufler \
25336d9c939dSCasey Schaufler static const struct file_operations proc_##LSM##_attr_dir_ops = { \
25346d9c939dSCasey Schaufler 	.read		= generic_read_dir, \
25356d9c939dSCasey Schaufler 	.iterate	= proc_##LSM##_attr_dir_iterate, \
25366d9c939dSCasey Schaufler 	.llseek		= default_llseek, \
25376d9c939dSCasey Schaufler }; \
25386d9c939dSCasey Schaufler \
25396d9c939dSCasey Schaufler static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
25406d9c939dSCasey Schaufler 				struct dentry *dentry, unsigned int flags) \
25416d9c939dSCasey Schaufler { \
25426d9c939dSCasey Schaufler 	return proc_pident_lookup(dir, dentry, \
25436d9c939dSCasey Schaufler 				  LSM##_attr_dir_stuff, \
2544d5a572a4SAlexey Dobriyan 				  LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
25456d9c939dSCasey Schaufler } \
25466d9c939dSCasey Schaufler \
25476d9c939dSCasey Schaufler static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
25486d9c939dSCasey Schaufler 	.lookup		= proc_##LSM##_attr_dir_lookup, \
25496d9c939dSCasey Schaufler 	.getattr	= pid_getattr, \
25506d9c939dSCasey Schaufler 	.setattr	= proc_setattr, \
25516d9c939dSCasey Schaufler }
25526d9c939dSCasey Schaufler 
25536d9c939dSCasey Schaufler #ifdef CONFIG_SECURITY_SMACK
25546d9c939dSCasey Schaufler static const struct pid_entry smack_attr_dir_stuff[] = {
25556d9c939dSCasey Schaufler 	ATTR("smack", "current",	0666),
25566d9c939dSCasey Schaufler };
25576d9c939dSCasey Schaufler LSM_DIR_OPS(smack);
25586d9c939dSCasey Schaufler #endif
25596d9c939dSCasey Schaufler 
2560c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
25616d9c939dSCasey Schaufler 	ATTR(NULL, "current",		0666),
25626d9c939dSCasey Schaufler 	ATTR(NULL, "prev",		0444),
25636d9c939dSCasey Schaufler 	ATTR(NULL, "exec",		0666),
25646d9c939dSCasey Schaufler 	ATTR(NULL, "fscreate",		0666),
25656d9c939dSCasey Schaufler 	ATTR(NULL, "keycreate",		0666),
25666d9c939dSCasey Schaufler 	ATTR(NULL, "sockcreate",	0666),
25676d9c939dSCasey Schaufler #ifdef CONFIG_SECURITY_SMACK
25686d9c939dSCasey Schaufler 	DIR("smack",			0555,
25696d9c939dSCasey Schaufler 	    proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
25706d9c939dSCasey Schaufler #endif
257128a6d671SEric W. Biederman };
257228a6d671SEric W. Biederman 
2573f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
25741da177e4SLinus Torvalds {
2575f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
257672d9dcfcSEric W. Biederman 				   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
25771da177e4SLinus Torvalds }
25781da177e4SLinus Torvalds 
257900977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
25801da177e4SLinus Torvalds 	.read		= generic_read_dir,
2581f50752eaSAl Viro 	.iterate_shared	= proc_attr_dir_readdir,
2582f50752eaSAl Viro 	.llseek		= generic_file_llseek,
25831da177e4SLinus Torvalds };
25841da177e4SLinus Torvalds 
258572d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
258600cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
25871da177e4SLinus Torvalds {
25887bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
2589d5a572a4SAlexey Dobriyan 				  attr_dir_stuff,
2590d5a572a4SAlexey Dobriyan 				  attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
25911da177e4SLinus Torvalds }
25921da177e4SLinus Torvalds 
2593c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
259472d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
259599f89551SEric W. Biederman 	.getattr	= pid_getattr,
25966d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
25971da177e4SLinus Torvalds };
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds #endif
26001da177e4SLinus Torvalds 
2601698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
26023cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
26033cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
26043cb4a0bbSKawai, Hidehiro {
2605496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
26063cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26073cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
26083cb4a0bbSKawai, Hidehiro 	size_t len;
26093cb4a0bbSKawai, Hidehiro 	int ret;
26103cb4a0bbSKawai, Hidehiro 
26113cb4a0bbSKawai, Hidehiro 	if (!task)
26123cb4a0bbSKawai, Hidehiro 		return -ESRCH;
26133cb4a0bbSKawai, Hidehiro 
26143cb4a0bbSKawai, Hidehiro 	ret = 0;
26153cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
26163cb4a0bbSKawai, Hidehiro 	if (mm) {
26173cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
26183cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
26193cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
26203cb4a0bbSKawai, Hidehiro 		mmput(mm);
26213cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
26223cb4a0bbSKawai, Hidehiro 	}
26233cb4a0bbSKawai, Hidehiro 
26243cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
26253cb4a0bbSKawai, Hidehiro 
26263cb4a0bbSKawai, Hidehiro 	return ret;
26273cb4a0bbSKawai, Hidehiro }
26283cb4a0bbSKawai, Hidehiro 
26293cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
26303cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
26313cb4a0bbSKawai, Hidehiro 					  size_t count,
26323cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
26333cb4a0bbSKawai, Hidehiro {
26343cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
26353cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26363cb4a0bbSKawai, Hidehiro 	unsigned int val;
26373cb4a0bbSKawai, Hidehiro 	int ret;
26383cb4a0bbSKawai, Hidehiro 	int i;
26393cb4a0bbSKawai, Hidehiro 	unsigned long mask;
26403cb4a0bbSKawai, Hidehiro 
2641774636e1SAlexey Dobriyan 	ret = kstrtouint_from_user(buf, count, 0, &val);
2642774636e1SAlexey Dobriyan 	if (ret < 0)
2643774636e1SAlexey Dobriyan 		return ret;
26443cb4a0bbSKawai, Hidehiro 
26453cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
2646496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
26473cb4a0bbSKawai, Hidehiro 	if (!task)
26483cb4a0bbSKawai, Hidehiro 		goto out_no_task;
26493cb4a0bbSKawai, Hidehiro 
26503cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
26513cb4a0bbSKawai, Hidehiro 	if (!mm)
26523cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
265341a0c249SColin Ian King 	ret = 0;
26543cb4a0bbSKawai, Hidehiro 
26553cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
26563cb4a0bbSKawai, Hidehiro 		if (val & mask)
26573cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
26583cb4a0bbSKawai, Hidehiro 		else
26593cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
26603cb4a0bbSKawai, Hidehiro 	}
26613cb4a0bbSKawai, Hidehiro 
26623cb4a0bbSKawai, Hidehiro 	mmput(mm);
26633cb4a0bbSKawai, Hidehiro  out_no_mm:
26643cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
26653cb4a0bbSKawai, Hidehiro  out_no_task:
2666774636e1SAlexey Dobriyan 	if (ret < 0)
26673cb4a0bbSKawai, Hidehiro 		return ret;
2668774636e1SAlexey Dobriyan 	return count;
26693cb4a0bbSKawai, Hidehiro }
26703cb4a0bbSKawai, Hidehiro 
26713cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
26723cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
26733cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
267487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
26753cb4a0bbSKawai, Hidehiro };
26763cb4a0bbSKawai, Hidehiro #endif
26773cb4a0bbSKawai, Hidehiro 
2678aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
267919aadc98SAlexey Dobriyan static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2680aba76fdbSAndrew Morton {
2681940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2682297c5d92SAndrea Righi 	unsigned long flags;
2683293eb1e7SVasiliy Kulikov 	int result;
2684297c5d92SAndrea Righi 
2685293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2686293eb1e7SVasiliy Kulikov 	if (result)
2687293eb1e7SVasiliy Kulikov 		return result;
2688293eb1e7SVasiliy Kulikov 
2689caaee623SJann Horn 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2690293eb1e7SVasiliy Kulikov 		result = -EACCES;
2691293eb1e7SVasiliy Kulikov 		goto out_unlock;
2692293eb1e7SVasiliy Kulikov 	}
26931d1221f3SVasiliy Kulikov 
26945995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2695b2d002dbSAndrea Righi 		struct task_struct *t = task;
2696297c5d92SAndrea Righi 
26975995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
26985995477aSAndrea Righi 		while_each_thread(task, t)
26995995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2700297c5d92SAndrea Righi 
2701297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2702297c5d92SAndrea Righi 	}
270325ce3191SJoe Perches 	seq_printf(m,
2704aba76fdbSAndrew Morton 		   "rchar: %llu\n"
2705aba76fdbSAndrew Morton 		   "wchar: %llu\n"
2706aba76fdbSAndrew Morton 		   "syscr: %llu\n"
2707aba76fdbSAndrew Morton 		   "syscw: %llu\n"
2708aba76fdbSAndrew Morton 		   "read_bytes: %llu\n"
2709aba76fdbSAndrew Morton 		   "write_bytes: %llu\n"
2710aba76fdbSAndrew Morton 		   "cancelled_write_bytes: %llu\n",
27117c44319dSAlexander Beregalov 		   (unsigned long long)acct.rchar,
27127c44319dSAlexander Beregalov 		   (unsigned long long)acct.wchar,
27137c44319dSAlexander Beregalov 		   (unsigned long long)acct.syscr,
27147c44319dSAlexander Beregalov 		   (unsigned long long)acct.syscw,
27157c44319dSAlexander Beregalov 		   (unsigned long long)acct.read_bytes,
27167c44319dSAlexander Beregalov 		   (unsigned long long)acct.write_bytes,
27177c44319dSAlexander Beregalov 		   (unsigned long long)acct.cancelled_write_bytes);
271825ce3191SJoe Perches 	result = 0;
271925ce3191SJoe Perches 
2720293eb1e7SVasiliy Kulikov out_unlock:
2721293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2722293eb1e7SVasiliy Kulikov 	return result;
2723aba76fdbSAndrew Morton }
2724297c5d92SAndrea Righi 
272519aadc98SAlexey Dobriyan static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
272619aadc98SAlexey Dobriyan 				  struct pid *pid, struct task_struct *task)
2727297c5d92SAndrea Righi {
272819aadc98SAlexey Dobriyan 	return do_io_accounting(task, m, 0);
2729297c5d92SAndrea Righi }
2730297c5d92SAndrea Righi 
273119aadc98SAlexey Dobriyan static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
273219aadc98SAlexey Dobriyan 				   struct pid *pid, struct task_struct *task)
2733297c5d92SAndrea Righi {
273419aadc98SAlexey Dobriyan 	return do_io_accounting(task, m, 1);
2735297c5d92SAndrea Righi }
2736297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2737aba76fdbSAndrew Morton 
273822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
273922d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
2740ccf94f1bSFabian Frederick 	const struct seq_operations *seq_ops)
274122d917d8SEric W. Biederman {
274222d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
274322d917d8SEric W. Biederman 	struct task_struct *task;
274422d917d8SEric W. Biederman 	struct seq_file *seq;
274522d917d8SEric W. Biederman 	int ret = -EINVAL;
274622d917d8SEric W. Biederman 
274722d917d8SEric W. Biederman 	task = get_proc_task(inode);
274822d917d8SEric W. Biederman 	if (task) {
274922d917d8SEric W. Biederman 		rcu_read_lock();
275022d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
275122d917d8SEric W. Biederman 		rcu_read_unlock();
275222d917d8SEric W. Biederman 		put_task_struct(task);
275322d917d8SEric W. Biederman 	}
275422d917d8SEric W. Biederman 	if (!ns)
275522d917d8SEric W. Biederman 		goto err;
275622d917d8SEric W. Biederman 
275722d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
275822d917d8SEric W. Biederman 	if (ret)
275922d917d8SEric W. Biederman 		goto err_put_ns;
276022d917d8SEric W. Biederman 
276122d917d8SEric W. Biederman 	seq = file->private_data;
276222d917d8SEric W. Biederman 	seq->private = ns;
276322d917d8SEric W. Biederman 
276422d917d8SEric W. Biederman 	return 0;
276522d917d8SEric W. Biederman err_put_ns:
276622d917d8SEric W. Biederman 	put_user_ns(ns);
276722d917d8SEric W. Biederman err:
276822d917d8SEric W. Biederman 	return ret;
276922d917d8SEric W. Biederman }
277022d917d8SEric W. Biederman 
277122d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
277222d917d8SEric W. Biederman {
277322d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
277422d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
277522d917d8SEric W. Biederman 	put_user_ns(ns);
277622d917d8SEric W. Biederman 	return seq_release(inode, file);
277722d917d8SEric W. Biederman }
277822d917d8SEric W. Biederman 
277922d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
278022d917d8SEric W. Biederman {
278122d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
278222d917d8SEric W. Biederman }
278322d917d8SEric W. Biederman 
278422d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
278522d917d8SEric W. Biederman {
278622d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
278722d917d8SEric W. Biederman }
278822d917d8SEric W. Biederman 
2789f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file)
2790f76d207aSEric W. Biederman {
2791f76d207aSEric W. Biederman 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2792f76d207aSEric W. Biederman }
2793f76d207aSEric W. Biederman 
279422d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
279522d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
279622d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
279722d917d8SEric W. Biederman 	.read		= seq_read,
279822d917d8SEric W. Biederman 	.llseek		= seq_lseek,
279922d917d8SEric W. Biederman 	.release	= proc_id_map_release,
280022d917d8SEric W. Biederman };
280122d917d8SEric W. Biederman 
280222d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
280322d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
280422d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
280522d917d8SEric W. Biederman 	.read		= seq_read,
280622d917d8SEric W. Biederman 	.llseek		= seq_lseek,
280722d917d8SEric W. Biederman 	.release	= proc_id_map_release,
280822d917d8SEric W. Biederman };
2809f76d207aSEric W. Biederman 
2810f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = {
2811f76d207aSEric W. Biederman 	.open		= proc_projid_map_open,
2812f76d207aSEric W. Biederman 	.write		= proc_projid_map_write,
2813f76d207aSEric W. Biederman 	.read		= seq_read,
2814f76d207aSEric W. Biederman 	.llseek		= seq_lseek,
2815f76d207aSEric W. Biederman 	.release	= proc_id_map_release,
2816f76d207aSEric W. Biederman };
28179cc46516SEric W. Biederman 
28189cc46516SEric W. Biederman static int proc_setgroups_open(struct inode *inode, struct file *file)
28199cc46516SEric W. Biederman {
28209cc46516SEric W. Biederman 	struct user_namespace *ns = NULL;
28219cc46516SEric W. Biederman 	struct task_struct *task;
28229cc46516SEric W. Biederman 	int ret;
28239cc46516SEric W. Biederman 
28249cc46516SEric W. Biederman 	ret = -ESRCH;
28259cc46516SEric W. Biederman 	task = get_proc_task(inode);
28269cc46516SEric W. Biederman 	if (task) {
28279cc46516SEric W. Biederman 		rcu_read_lock();
28289cc46516SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
28299cc46516SEric W. Biederman 		rcu_read_unlock();
28309cc46516SEric W. Biederman 		put_task_struct(task);
28319cc46516SEric W. Biederman 	}
28329cc46516SEric W. Biederman 	if (!ns)
28339cc46516SEric W. Biederman 		goto err;
28349cc46516SEric W. Biederman 
28359cc46516SEric W. Biederman 	if (file->f_mode & FMODE_WRITE) {
28369cc46516SEric W. Biederman 		ret = -EACCES;
28379cc46516SEric W. Biederman 		if (!ns_capable(ns, CAP_SYS_ADMIN))
28389cc46516SEric W. Biederman 			goto err_put_ns;
28399cc46516SEric W. Biederman 	}
28409cc46516SEric W. Biederman 
28419cc46516SEric W. Biederman 	ret = single_open(file, &proc_setgroups_show, ns);
28429cc46516SEric W. Biederman 	if (ret)
28439cc46516SEric W. Biederman 		goto err_put_ns;
28449cc46516SEric W. Biederman 
28459cc46516SEric W. Biederman 	return 0;
28469cc46516SEric W. Biederman err_put_ns:
28479cc46516SEric W. Biederman 	put_user_ns(ns);
28489cc46516SEric W. Biederman err:
28499cc46516SEric W. Biederman 	return ret;
28509cc46516SEric W. Biederman }
28519cc46516SEric W. Biederman 
28529cc46516SEric W. Biederman static int proc_setgroups_release(struct inode *inode, struct file *file)
28539cc46516SEric W. Biederman {
28549cc46516SEric W. Biederman 	struct seq_file *seq = file->private_data;
28559cc46516SEric W. Biederman 	struct user_namespace *ns = seq->private;
28569cc46516SEric W. Biederman 	int ret = single_release(inode, file);
28579cc46516SEric W. Biederman 	put_user_ns(ns);
28589cc46516SEric W. Biederman 	return ret;
28599cc46516SEric W. Biederman }
28609cc46516SEric W. Biederman 
28619cc46516SEric W. Biederman static const struct file_operations proc_setgroups_operations = {
28629cc46516SEric W. Biederman 	.open		= proc_setgroups_open,
28639cc46516SEric W. Biederman 	.write		= proc_setgroups_write,
28649cc46516SEric W. Biederman 	.read		= seq_read,
28659cc46516SEric W. Biederman 	.llseek		= seq_lseek,
28669cc46516SEric W. Biederman 	.release	= proc_setgroups_release,
28679cc46516SEric W. Biederman };
286822d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
286922d917d8SEric W. Biederman 
287047830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
287147830723SKees Cook 				struct pid *pid, struct task_struct *task)
287247830723SKees Cook {
2873a9712bc1SAl Viro 	int err = lock_trace(task);
2874a9712bc1SAl Viro 	if (!err) {
287547830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2876a9712bc1SAl Viro 		unlock_trace(task);
2877a9712bc1SAl Viro 	}
2878a9712bc1SAl Viro 	return err;
287947830723SKees Cook }
288047830723SKees Cook 
28817c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
28827c23b330SJosh Poimboeuf static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
28837c23b330SJosh Poimboeuf 				struct pid *pid, struct task_struct *task)
28847c23b330SJosh Poimboeuf {
28857c23b330SJosh Poimboeuf 	seq_printf(m, "%d\n", task->patch_state);
28867c23b330SJosh Poimboeuf 	return 0;
28877c23b330SJosh Poimboeuf }
28887c23b330SJosh Poimboeuf #endif /* CONFIG_LIVEPATCH */
28897c23b330SJosh Poimboeuf 
2890c8d12627SAlexander Popov #ifdef CONFIG_STACKLEAK_METRICS
2891c8d12627SAlexander Popov static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2892c8d12627SAlexander Popov 				struct pid *pid, struct task_struct *task)
2893c8d12627SAlexander Popov {
2894c8d12627SAlexander Popov 	unsigned long prev_depth = THREAD_SIZE -
2895c8d12627SAlexander Popov 				(task->prev_lowest_stack & (THREAD_SIZE - 1));
2896c8d12627SAlexander Popov 	unsigned long depth = THREAD_SIZE -
2897c8d12627SAlexander Popov 				(task->lowest_stack & (THREAD_SIZE - 1));
2898c8d12627SAlexander Popov 
2899c8d12627SAlexander Popov 	seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2900c8d12627SAlexander Popov 							prev_depth, depth);
2901c8d12627SAlexander Popov 	return 0;
2902c8d12627SAlexander Popov }
2903c8d12627SAlexander Popov #endif /* CONFIG_STACKLEAK_METRICS */
2904c8d12627SAlexander Popov 
2905801199ceSEric W. Biederman /*
290628a6d671SEric W. Biederman  * Thread groups
290728a6d671SEric W. Biederman  */
290800977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2909c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
291020cdc894SEric W. Biederman 
2911c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2912631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2913631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2914640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2915631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
29166b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2917b2211a36SAndrew Morton #ifdef CONFIG_NET
2918631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2919b2211a36SAndrew Morton #endif
2920631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2921c5317167SAl Viro 	REG("auxv",       S_IRUSR, proc_auxv_operations),
2922631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
292335a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
29241c963eb1SAlexey Dobriyan 	ONE("limits",	  S_IRUGO, proc_pid_limits),
292543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2926631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
292743ae34cbSIngo Molnar #endif
29285091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
29295091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
29305091faa4SMike Galbraith #endif
29314614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2932ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
293309d93bd6SAlexey Dobriyan 	ONE("syscall",    S_IRUSR, proc_pid_syscall),
2934ebcb6734SRoland McGrath #endif
2935c2c0bb44SAlexey Dobriyan 	REG("cmdline",    S_IRUGO, proc_pid_cmdline_ops),
2936631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2937631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2938b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
293928a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2940b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
294128a6d671SEric W. Biederman #endif
2942631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2943631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2944631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2945631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2946631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2947631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2948631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
29491e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2950631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2951b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
2952493b0e9dSDaniel Colascione 	REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
295332ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
295428a6d671SEric W. Biederman #endif
295528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2956631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
295728a6d671SEric W. Biederman #endif
295828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2959edfcd606SAlexey Dobriyan 	ONE("wchan",      S_IRUGO, proc_pid_wchan),
296028a6d671SEric W. Biederman #endif
29612ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
296235a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
296328a6d671SEric W. Biederman #endif
29645968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
2965f6e826caSAlexey Dobriyan 	ONE("schedstat",  S_IRUGO, proc_pid_schedstat),
296628a6d671SEric W. Biederman #endif
29679745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2968631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
29699745512cSArjan van de Ven #endif
29708793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
297152de4779SZefan Li 	ONE("cpuset",     S_IRUGO, proc_cpuset_show),
297228a6d671SEric W. Biederman #endif
2973a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2974006f4ac4SZefan Li 	ONE("cgroup",  S_IRUGO, proc_cgroup_show),
2975a424316cSPaul Menage #endif
29766ba51e37SAlexey Dobriyan 	ONE("oom_score",  S_IRUGO, proc_oom_score),
2977fa0cbbf1SDavid Rientjes 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2978a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
29794b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
2980631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2981631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
298228a6d671SEric W. Biederman #endif
2983f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2984631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2985168c42bcSAkinobu Mita 	REG("fail-nth", 0644, proc_fail_nth_operations),
2986f4f154fdSAkinobu Mita #endif
2987698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2988631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
29893cb4a0bbSKawai, Hidehiro #endif
2990aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
299119aadc98SAlexey Dobriyan 	ONE("io",	S_IRUSR, proc_tgid_io_accounting),
2992aba76fdbSAndrew Morton #endif
299322d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
299422d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
299522d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2996f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
29979cc46516SEric W. Biederman 	REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
299822d917d8SEric W. Biederman #endif
2999b18b6a9cSNicolas Pitre #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
300048f6a7a5SPavel Emelyanov 	REG("timers",	  S_IRUGO, proc_timers_operations),
300148f6a7a5SPavel Emelyanov #endif
30025de23d43SJohn Stultz 	REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
30037c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
30047c23b330SJosh Poimboeuf 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
30057c23b330SJosh Poimboeuf #endif
3006c8d12627SAlexander Popov #ifdef CONFIG_STACKLEAK_METRICS
3007c8d12627SAlexander Popov 	ONE("stack_depth", S_IRUGO, proc_stack_depth),
3008c8d12627SAlexander Popov #endif
300928a6d671SEric W. Biederman };
301028a6d671SEric W. Biederman 
3011f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
301228a6d671SEric W. Biederman {
3013f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
301428a6d671SEric W. Biederman 				   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
301528a6d671SEric W. Biederman }
301628a6d671SEric W. Biederman 
301700977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
301828a6d671SEric W. Biederman 	.read		= generic_read_dir,
3019f50752eaSAl Viro 	.iterate_shared	= proc_tgid_base_readdir,
3020f50752eaSAl Viro 	.llseek		= generic_file_llseek,
302128a6d671SEric W. Biederman };
302228a6d671SEric W. Biederman 
30233eb39f47SChristian Brauner struct pid *tgid_pidfd_to_pid(const struct file *file)
30243eb39f47SChristian Brauner {
302530d158b1SChristian Brauner 	if (file->f_op != &proc_tgid_base_operations)
30263eb39f47SChristian Brauner 		return ERR_PTR(-EBADF);
30273eb39f47SChristian Brauner 
30283eb39f47SChristian Brauner 	return proc_pid(file_inode(file));
30293eb39f47SChristian Brauner }
30303eb39f47SChristian Brauner 
303100cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
303200cd8dd3SAl Viro {
30337bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
3034d5a572a4SAlexey Dobriyan 				  tgid_base_stuff,
3035d5a572a4SAlexey Dobriyan 				  tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
303628a6d671SEric W. Biederman }
303728a6d671SEric W. Biederman 
3038c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
303928a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
304028a6d671SEric W. Biederman 	.getattr	= pid_getattr,
304128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
30420499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
304328a6d671SEric W. Biederman };
304428a6d671SEric W. Biederman 
304560347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
30461da177e4SLinus Torvalds {
304748e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
3048e3912ac3SAlexey Dobriyan 	char buf[10 + 1];
304948e6484dSEric W. Biederman 	struct qstr name;
30501da177e4SLinus Torvalds 
305148e6484dSEric W. Biederman 	name.name = buf;
3052e3912ac3SAlexey Dobriyan 	name.len = snprintf(buf, sizeof(buf), "%u", pid);
30534f522a24SAl Viro 	/* no ->d_hash() rejects on procfs */
305460347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
305548e6484dSEric W. Biederman 	if (dentry) {
3056bbd51924SEric W. Biederman 		d_invalidate(dentry);
305748e6484dSEric W. Biederman 		dput(dentry);
30581da177e4SLinus Torvalds 	}
30591da177e4SLinus Torvalds 
3060c35a7f18SOleg Nesterov 	if (pid == tgid)
3061c35a7f18SOleg Nesterov 		return;
3062c35a7f18SOleg Nesterov 
306348e6484dSEric W. Biederman 	name.name = buf;
3064e3912ac3SAlexey Dobriyan 	name.len = snprintf(buf, sizeof(buf), "%u", tgid);
306560347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
306648e6484dSEric W. Biederman 	if (!leader)
306748e6484dSEric W. Biederman 		goto out;
306848e6484dSEric W. Biederman 
306948e6484dSEric W. Biederman 	name.name = "task";
307048e6484dSEric W. Biederman 	name.len = strlen(name.name);
307148e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
307248e6484dSEric W. Biederman 	if (!dir)
307348e6484dSEric W. Biederman 		goto out_put_leader;
307448e6484dSEric W. Biederman 
307548e6484dSEric W. Biederman 	name.name = buf;
3076e3912ac3SAlexey Dobriyan 	name.len = snprintf(buf, sizeof(buf), "%u", pid);
307748e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
307848e6484dSEric W. Biederman 	if (dentry) {
3079bbd51924SEric W. Biederman 		d_invalidate(dentry);
308048e6484dSEric W. Biederman 		dput(dentry);
30811da177e4SLinus Torvalds 	}
308248e6484dSEric W. Biederman 
308348e6484dSEric W. Biederman 	dput(dir);
308448e6484dSEric W. Biederman out_put_leader:
308548e6484dSEric W. Biederman 	dput(leader);
308648e6484dSEric W. Biederman out:
308748e6484dSEric W. Biederman 	return;
30881da177e4SLinus Torvalds }
30891da177e4SLinus Torvalds 
30900895e91dSRandy Dunlap /**
30910895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
30920895e91dSRandy Dunlap  * @task: task that should be flushed.
30930895e91dSRandy Dunlap  *
30940895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
309560347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
30960895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
30970895e91dSRandy Dunlap  *
30980895e91dSRandy Dunlap  * Looks in the dcache for
30990895e91dSRandy Dunlap  * /proc/@pid
31000895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
31010895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
31020895e91dSRandy Dunlap  * from the dcache.
31030895e91dSRandy Dunlap  *
31040895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
31050895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
31060895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
31070895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
31080895e91dSRandy Dunlap  * dcache entries at process exit time.
31090895e91dSRandy Dunlap  *
31100895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
31110895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
31120895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
311360347f67SPavel Emelyanov  */
311460347f67SPavel Emelyanov 
311560347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
311660347f67SPavel Emelyanov {
31179fcc2d15SEric W. Biederman 	int i;
31189b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
3119130f77ecSPavel Emelyanov 	struct upid *upid;
3120130f77ecSPavel Emelyanov 
3121130f77ecSPavel Emelyanov 	pid = task_pid(task);
3122130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
31239fcc2d15SEric W. Biederman 
31249fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
3125130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
3126130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
31279b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
3128130f77ecSPavel Emelyanov 	}
312960347f67SPavel Emelyanov }
313060347f67SPavel Emelyanov 
31310168b9e3SAl Viro static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3132c5141e6dSEric Dumazet 				   struct task_struct *task, const void *ptr)
3133444ceed8SEric W. Biederman {
3134444ceed8SEric W. Biederman 	struct inode *inode;
3135444ceed8SEric W. Biederman 
31360168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3137444ceed8SEric W. Biederman 	if (!inode)
31380168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
3139444ceed8SEric W. Biederman 
3140444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
3141444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
3142444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3143aed54175SVegard Nossum 
31441270dd8dSAlexey Dobriyan 	set_nlink(inode, nlink_tgid);
31451bbc5513SAl Viro 	pid_update_inode(task, inode);
3146444ceed8SEric W. Biederman 
3147fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
31480168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
3149444ceed8SEric W. Biederman }
3150444ceed8SEric W. Biederman 
3151867aaccfSZhikang Zhang struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
31521da177e4SLinus Torvalds {
31531da177e4SLinus Torvalds 	struct task_struct *task;
31541da177e4SLinus Torvalds 	unsigned tgid;
3155b488893aSPavel Emelyanov 	struct pid_namespace *ns;
31560168b9e3SAl Viro 	struct dentry *result = ERR_PTR(-ENOENT);
31571da177e4SLinus Torvalds 
3158dbcdb504SAlexey Dobriyan 	tgid = name_to_int(&dentry->d_name);
31591da177e4SLinus Torvalds 	if (tgid == ~0U)
31601da177e4SLinus Torvalds 		goto out;
31611da177e4SLinus Torvalds 
3162b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3163de758734SEric W. Biederman 	rcu_read_lock();
3164b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
31651da177e4SLinus Torvalds 	if (task)
31661da177e4SLinus Torvalds 		get_task_struct(task);
3167de758734SEric W. Biederman 	rcu_read_unlock();
31681da177e4SLinus Torvalds 	if (!task)
31691da177e4SLinus Torvalds 		goto out;
31701da177e4SLinus Torvalds 
31710168b9e3SAl Viro 	result = proc_pid_instantiate(dentry, task, NULL);
317248e6484dSEric W. Biederman 	put_task_struct(task);
31731da177e4SLinus Torvalds out:
31740168b9e3SAl Viro 	return result;
31751da177e4SLinus Torvalds }
31761da177e4SLinus Torvalds 
31771da177e4SLinus Torvalds /*
31780804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
31790bc58a91SEric W. Biederman  *
31801da177e4SLinus Torvalds  */
318119fd4bb2SEric W. Biederman struct tgid_iter {
318219fd4bb2SEric W. Biederman 	unsigned int tgid;
31830804ef4bSEric W. Biederman 	struct task_struct *task;
318419fd4bb2SEric W. Biederman };
318519fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
318619fd4bb2SEric W. Biederman {
31870804ef4bSEric W. Biederman 	struct pid *pid;
31881da177e4SLinus Torvalds 
318919fd4bb2SEric W. Biederman 	if (iter.task)
319019fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
31910804ef4bSEric W. Biederman 	rcu_read_lock();
31920804ef4bSEric W. Biederman retry:
319319fd4bb2SEric W. Biederman 	iter.task = NULL;
319419fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
31950804ef4bSEric W. Biederman 	if (pid) {
319619fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
319719fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
31980804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
31990804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
32000804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
32010804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
32020804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
32030804ef4bSEric W. Biederman 		 *
32040804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
32050804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
32060804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
32070804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
32080804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
32090bc58a91SEric W. Biederman 		 */
321019fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
321119fd4bb2SEric W. Biederman 			iter.tgid += 1;
32120804ef4bSEric W. Biederman 			goto retry;
321319fd4bb2SEric W. Biederman 		}
321419fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
32151da177e4SLinus Torvalds 	}
3216454cc105SEric W. Biederman 	rcu_read_unlock();
321719fd4bb2SEric W. Biederman 	return iter;
32181da177e4SLinus Torvalds }
32191da177e4SLinus Torvalds 
32200097875bSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
3223f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx)
32241da177e4SLinus Torvalds {
322519fd4bb2SEric W. Biederman 	struct tgid_iter iter;
322676f668beSChristoph Hellwig 	struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3227f0c3b509SAl Viro 	loff_t pos = ctx->pos;
32281da177e4SLinus Torvalds 
3229021ada7dSAl Viro 	if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3230f0c3b509SAl Viro 		return 0;
32311da177e4SLinus Torvalds 
32320097875bSEric W. Biederman 	if (pos == TGID_OFFSET - 2) {
32332b0143b5SDavid Howells 		struct inode *inode = d_inode(ns->proc_self);
3234db963164SAl Viro 		if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3235f0c3b509SAl Viro 			return 0;
32360097875bSEric W. Biederman 		ctx->pos = pos = pos + 1;
3237021ada7dSAl Viro 	}
32380097875bSEric W. Biederman 	if (pos == TGID_OFFSET - 1) {
32392b0143b5SDavid Howells 		struct inode *inode = d_inode(ns->proc_thread_self);
32400097875bSEric W. Biederman 		if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
32410097875bSEric W. Biederman 			return 0;
32420097875bSEric W. Biederman 		ctx->pos = pos = pos + 1;
32430097875bSEric W. Biederman 	}
32440097875bSEric W. Biederman 	iter.tgid = pos - TGID_OFFSET;
324519fd4bb2SEric W. Biederman 	iter.task = NULL;
324619fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
324719fd4bb2SEric W. Biederman 	     iter.task;
324819fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
3249e3912ac3SAlexey Dobriyan 		char name[10 + 1];
3250a4ef3895SAlexey Dobriyan 		unsigned int len;
32513ba4bceeSEric Dumazet 
32523ba4bceeSEric Dumazet 		cond_resched();
3253796f571bSLafcadio Wluiki 		if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3254f0c3b509SAl Viro 			continue;
32550499680aSVasiliy Kulikov 
3256e3912ac3SAlexey Dobriyan 		len = snprintf(name, sizeof(name), "%u", iter.tgid);
3257f0c3b509SAl Viro 		ctx->pos = iter.tgid + TGID_OFFSET;
3258f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
3259f0c3b509SAl Viro 				     proc_pid_instantiate, iter.task, NULL)) {
326019fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
3261f0c3b509SAl Viro 			return 0;
32621da177e4SLinus Torvalds 		}
32631da177e4SLinus Torvalds 	}
3264f0c3b509SAl Viro 	ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
32651da177e4SLinus Torvalds 	return 0;
32661da177e4SLinus Torvalds }
32671da177e4SLinus Torvalds 
32680bc58a91SEric W. Biederman /*
32691b3044e3SJanis Danisevskis  * proc_tid_comm_permission is a special permission function exclusively
32701b3044e3SJanis Danisevskis  * used for the node /proc/<pid>/task/<tid>/comm.
32711b3044e3SJanis Danisevskis  * It bypasses generic permission checks in the case where a task of the same
32721b3044e3SJanis Danisevskis  * task group attempts to access the node.
32731b3044e3SJanis Danisevskis  * The rationale behind this is that glibc and bionic access this node for
32741b3044e3SJanis Danisevskis  * cross thread naming (pthread_set/getname_np(!self)). However, if
32751b3044e3SJanis Danisevskis  * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
32761b3044e3SJanis Danisevskis  * which locks out the cross thread naming implementation.
32771b3044e3SJanis Danisevskis  * This function makes sure that the node is always accessible for members of
32781b3044e3SJanis Danisevskis  * same thread group.
32791b3044e3SJanis Danisevskis  */
32801b3044e3SJanis Danisevskis static int proc_tid_comm_permission(struct inode *inode, int mask)
32811b3044e3SJanis Danisevskis {
32821b3044e3SJanis Danisevskis 	bool is_same_tgroup;
32831b3044e3SJanis Danisevskis 	struct task_struct *task;
32841b3044e3SJanis Danisevskis 
32851b3044e3SJanis Danisevskis 	task = get_proc_task(inode);
32861b3044e3SJanis Danisevskis 	if (!task)
32871b3044e3SJanis Danisevskis 		return -ESRCH;
32881b3044e3SJanis Danisevskis 	is_same_tgroup = same_thread_group(current, task);
32891b3044e3SJanis Danisevskis 	put_task_struct(task);
32901b3044e3SJanis Danisevskis 
32911b3044e3SJanis Danisevskis 	if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
32921b3044e3SJanis Danisevskis 		/* This file (/proc/<pid>/task/<tid>/comm) can always be
32931b3044e3SJanis Danisevskis 		 * read or written by the members of the corresponding
32941b3044e3SJanis Danisevskis 		 * thread group.
32951b3044e3SJanis Danisevskis 		 */
32961b3044e3SJanis Danisevskis 		return 0;
32971b3044e3SJanis Danisevskis 	}
32981b3044e3SJanis Danisevskis 
32991b3044e3SJanis Danisevskis 	return generic_permission(inode, mask);
33001b3044e3SJanis Danisevskis }
33011b3044e3SJanis Danisevskis 
33021b3044e3SJanis Danisevskis static const struct inode_operations proc_tid_comm_inode_operations = {
33031b3044e3SJanis Danisevskis 		.permission = proc_tid_comm_permission,
33041b3044e3SJanis Danisevskis };
33051b3044e3SJanis Danisevskis 
33061b3044e3SJanis Danisevskis /*
330728a6d671SEric W. Biederman  * Tasks
330828a6d671SEric W. Biederman  */
3309c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3310631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
33113835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
33126b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
33136ba8ed79SEric W. Biederman #ifdef CONFIG_NET
33146ba8ed79SEric W. Biederman 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
33156ba8ed79SEric W. Biederman #endif
3316631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3317c5317167SAl Viro 	REG("auxv",      S_IRUSR, proc_auxv_operations),
3318631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
331935a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
33201c963eb1SAlexey Dobriyan 	ONE("limits",	 S_IRUGO, proc_pid_limits),
332143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3322631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
332343ae34cbSIngo Molnar #endif
33241b3044e3SJanis Danisevskis 	NOD("comm",      S_IFREG|S_IRUGO|S_IWUSR,
33251b3044e3SJanis Danisevskis 			 &proc_tid_comm_inode_operations,
33261b3044e3SJanis Danisevskis 			 &proc_pid_set_comm_operations, {}),
3327ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
332809d93bd6SAlexey Dobriyan 	ONE("syscall",   S_IRUSR, proc_pid_syscall),
3329ebcb6734SRoland McGrath #endif
3330c2c0bb44SAlexey Dobriyan 	REG("cmdline",   S_IRUGO, proc_pid_cmdline_ops),
3331631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3332631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3333871305bbSVlastimil Babka 	REG("maps",      S_IRUGO, proc_pid_maps_operations),
33342e13ba54SIago López Galeiras #ifdef CONFIG_PROC_CHILDREN
333581841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
333681841161SCyrill Gorcunov #endif
333728a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3338871305bbSVlastimil Babka 	REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
333928a6d671SEric W. Biederman #endif
3340631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3341631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3342631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3343631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3344631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3345631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
33461e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3347631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3348871305bbSVlastimil Babka 	REG("smaps",     S_IRUGO, proc_pid_smaps_operations),
3349493b0e9dSDaniel Colascione 	REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
335032ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
335128a6d671SEric W. Biederman #endif
335228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3353631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
335428a6d671SEric W. Biederman #endif
335528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3356edfcd606SAlexey Dobriyan 	ONE("wchan",     S_IRUGO, proc_pid_wchan),
335728a6d671SEric W. Biederman #endif
33582ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
335935a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
336028a6d671SEric W. Biederman #endif
33615968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
3362f6e826caSAlexey Dobriyan 	ONE("schedstat", S_IRUGO, proc_pid_schedstat),
336328a6d671SEric W. Biederman #endif
33649745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3365631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
33669745512cSArjan van de Ven #endif
33678793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
336852de4779SZefan Li 	ONE("cpuset",    S_IRUGO, proc_cpuset_show),
336928a6d671SEric W. Biederman #endif
3370a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3371006f4ac4SZefan Li 	ONE("cgroup",  S_IRUGO, proc_cgroup_show),
3372a424316cSPaul Menage #endif
33736ba51e37SAlexey Dobriyan 	ONE("oom_score", S_IRUGO, proc_oom_score),
3374fa0cbbf1SDavid Rientjes 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3375a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
33764b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
3377631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
337826ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
337928a6d671SEric W. Biederman #endif
3380f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3381631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
33821203c8e6SAkinobu Mita 	REG("fail-nth", 0644, proc_fail_nth_operations),
3383f4f154fdSAkinobu Mita #endif
3384297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
338519aadc98SAlexey Dobriyan 	ONE("io",	S_IRUSR, proc_tid_io_accounting),
3386297c5d92SAndrea Righi #endif
338722d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
338822d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
338922d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
3390f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
33919cc46516SEric W. Biederman 	REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
339222d917d8SEric W. Biederman #endif
33937c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
33947c23b330SJosh Poimboeuf 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
33957c23b330SJosh Poimboeuf #endif
339628a6d671SEric W. Biederman };
339728a6d671SEric W. Biederman 
3398f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
339928a6d671SEric W. Biederman {
3400f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
340128a6d671SEric W. Biederman 				   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
340228a6d671SEric W. Biederman }
340328a6d671SEric W. Biederman 
340400cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
340500cd8dd3SAl Viro {
34067bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
3407d5a572a4SAlexey Dobriyan 				  tid_base_stuff,
3408d5a572a4SAlexey Dobriyan 				  tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
340928a6d671SEric W. Biederman }
341028a6d671SEric W. Biederman 
341100977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
341228a6d671SEric W. Biederman 	.read		= generic_read_dir,
3413f50752eaSAl Viro 	.iterate_shared	= proc_tid_base_readdir,
3414f50752eaSAl Viro 	.llseek		= generic_file_llseek,
341528a6d671SEric W. Biederman };
341628a6d671SEric W. Biederman 
3417c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
341828a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
341928a6d671SEric W. Biederman 	.getattr	= pid_getattr,
342028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
342128a6d671SEric W. Biederman };
342228a6d671SEric W. Biederman 
34230168b9e3SAl Viro static struct dentry *proc_task_instantiate(struct dentry *dentry,
34240168b9e3SAl Viro 	struct task_struct *task, const void *ptr)
3425444ceed8SEric W. Biederman {
3426444ceed8SEric W. Biederman 	struct inode *inode;
34270168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3428444ceed8SEric W. Biederman 	if (!inode)
34290168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
34301bbc5513SAl Viro 
3431444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3432444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3433444ceed8SEric W. Biederman 	inode->i_flags |= S_IMMUTABLE;
3434aed54175SVegard Nossum 
34351270dd8dSAlexey Dobriyan 	set_nlink(inode, nlink_tid);
34361bbc5513SAl Viro 	pid_update_inode(task, inode);
3437444ceed8SEric W. Biederman 
3438fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
34390168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
3440444ceed8SEric W. Biederman }
3441444ceed8SEric W. Biederman 
344200cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
344328a6d671SEric W. Biederman {
344428a6d671SEric W. Biederman 	struct task_struct *task;
344528a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
344628a6d671SEric W. Biederman 	unsigned tid;
3447b488893aSPavel Emelyanov 	struct pid_namespace *ns;
34480168b9e3SAl Viro 	struct dentry *result = ERR_PTR(-ENOENT);
344928a6d671SEric W. Biederman 
345028a6d671SEric W. Biederman 	if (!leader)
345128a6d671SEric W. Biederman 		goto out_no_task;
345228a6d671SEric W. Biederman 
3453dbcdb504SAlexey Dobriyan 	tid = name_to_int(&dentry->d_name);
345428a6d671SEric W. Biederman 	if (tid == ~0U)
345528a6d671SEric W. Biederman 		goto out;
345628a6d671SEric W. Biederman 
3457b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
345828a6d671SEric W. Biederman 	rcu_read_lock();
3459b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
346028a6d671SEric W. Biederman 	if (task)
346128a6d671SEric W. Biederman 		get_task_struct(task);
346228a6d671SEric W. Biederman 	rcu_read_unlock();
346328a6d671SEric W. Biederman 	if (!task)
346428a6d671SEric W. Biederman 		goto out;
3465bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
346628a6d671SEric W. Biederman 		goto out_drop_task;
346728a6d671SEric W. Biederman 
34680168b9e3SAl Viro 	result = proc_task_instantiate(dentry, task, NULL);
346928a6d671SEric W. Biederman out_drop_task:
347028a6d671SEric W. Biederman 	put_task_struct(task);
347128a6d671SEric W. Biederman out:
347228a6d671SEric W. Biederman 	put_task_struct(leader);
347328a6d671SEric W. Biederman out_no_task:
34740168b9e3SAl Viro 	return result;
347528a6d671SEric W. Biederman }
347628a6d671SEric W. Biederman 
347728a6d671SEric W. Biederman /*
34780bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
34790bc58a91SEric W. Biederman  *
34800bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
34810bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
34820bc58a91SEric W. Biederman  * directory we have more work todo.
34830bc58a91SEric W. Biederman  *
34840bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
34850bc58a91SEric W. Biederman  *
34860bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
34870bc58a91SEric W. Biederman  * threads past it.
34880bc58a91SEric W. Biederman  */
34899f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
34909f6e963fSOleg Nesterov 					struct pid_namespace *ns)
34910bc58a91SEric W. Biederman {
3492d855a4b7SOleg Nesterov 	struct task_struct *pos, *task;
34939f6e963fSOleg Nesterov 	unsigned long nr = f_pos;
34949f6e963fSOleg Nesterov 
34959f6e963fSOleg Nesterov 	if (nr != f_pos)	/* 32bit overflow? */
34969f6e963fSOleg Nesterov 		return NULL;
34970bc58a91SEric W. Biederman 
3498cc288738SEric W. Biederman 	rcu_read_lock();
3499d855a4b7SOleg Nesterov 	task = pid_task(pid, PIDTYPE_PID);
3500d855a4b7SOleg Nesterov 	if (!task)
3501d855a4b7SOleg Nesterov 		goto fail;
3502d855a4b7SOleg Nesterov 
3503d855a4b7SOleg Nesterov 	/* Attempt to start with the tid of a thread */
35049f6e963fSOleg Nesterov 	if (tid && nr) {
3505b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3506d855a4b7SOleg Nesterov 		if (pos && same_thread_group(pos, task))
3507a872ff0cSOleg Nesterov 			goto found;
35080bc58a91SEric W. Biederman 	}
35090bc58a91SEric W. Biederman 
35100bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
35119f6e963fSOleg Nesterov 	if (nr >= get_nr_threads(task))
3512c986c14aSOleg Nesterov 		goto fail;
3513a872ff0cSOleg Nesterov 
3514a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3515a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3516a872ff0cSOleg Nesterov 	 */
3517d855a4b7SOleg Nesterov 	pos = task = task->group_leader;
3518c986c14aSOleg Nesterov 	do {
35199f6e963fSOleg Nesterov 		if (!nr--)
3520c986c14aSOleg Nesterov 			goto found;
3521d855a4b7SOleg Nesterov 	} while_each_thread(task, pos);
3522c986c14aSOleg Nesterov fail:
3523a872ff0cSOleg Nesterov 	pos = NULL;
3524a872ff0cSOleg Nesterov 	goto out;
3525a872ff0cSOleg Nesterov found:
3526a872ff0cSOleg Nesterov 	get_task_struct(pos);
3527a872ff0cSOleg Nesterov out:
3528cc288738SEric W. Biederman 	rcu_read_unlock();
35290bc58a91SEric W. Biederman 	return pos;
35300bc58a91SEric W. Biederman }
35310bc58a91SEric W. Biederman 
35320bc58a91SEric W. Biederman /*
35330bc58a91SEric W. Biederman  * Find the next thread in the thread list.
35340bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
35350bc58a91SEric W. Biederman  *
35360bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
35370bc58a91SEric W. Biederman  */
35380bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
35390bc58a91SEric W. Biederman {
3540c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3541cc288738SEric W. Biederman 	rcu_read_lock();
3542c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
35430bc58a91SEric W. Biederman 		pos = next_thread(start);
3544c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
35450bc58a91SEric W. Biederman 			pos = NULL;
3546c1df7fb8SOleg Nesterov 		else
3547c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3548c1df7fb8SOleg Nesterov 	}
3549cc288738SEric W. Biederman 	rcu_read_unlock();
35500bc58a91SEric W. Biederman 	put_task_struct(start);
35510bc58a91SEric W. Biederman 	return pos;
35520bc58a91SEric W. Biederman }
35530bc58a91SEric W. Biederman 
35541da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
3555f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx)
35561da177e4SLinus Torvalds {
3557d855a4b7SOleg Nesterov 	struct inode *inode = file_inode(file);
3558d855a4b7SOleg Nesterov 	struct task_struct *task;
3559b488893aSPavel Emelyanov 	struct pid_namespace *ns;
3560f0c3b509SAl Viro 	int tid;
35611da177e4SLinus Torvalds 
3562d855a4b7SOleg Nesterov 	if (proc_inode_is_dead(inode))
3563f0c3b509SAl Viro 		return -ENOENT;
35641da177e4SLinus Torvalds 
3565f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
3566d855a4b7SOleg Nesterov 		return 0;
35671da177e4SLinus Torvalds 
35680bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
35690bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
35700bc58a91SEric W. Biederman 	 */
357176f668beSChristoph Hellwig 	ns = proc_pid_ns(inode);
3572f0c3b509SAl Viro 	tid = (int)file->f_version;
3573f0c3b509SAl Viro 	file->f_version = 0;
3574d855a4b7SOleg Nesterov 	for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
35750bc58a91SEric W. Biederman 	     task;
3576f0c3b509SAl Viro 	     task = next_tid(task), ctx->pos++) {
3577e3912ac3SAlexey Dobriyan 		char name[10 + 1];
3578a4ef3895SAlexey Dobriyan 		unsigned int len;
3579b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
3580e3912ac3SAlexey Dobriyan 		len = snprintf(name, sizeof(name), "%u", tid);
3581f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
3582f0c3b509SAl Viro 				proc_task_instantiate, task, NULL)) {
35830bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
35840bc58a91SEric W. Biederman 			 * pid for the next readir call */
3585f0c3b509SAl Viro 			file->f_version = (u64)tid;
35860bc58a91SEric W. Biederman 			put_task_struct(task);
35871da177e4SLinus Torvalds 			break;
35880bc58a91SEric W. Biederman 		}
35891da177e4SLinus Torvalds 	}
3590d855a4b7SOleg Nesterov 
3591f0c3b509SAl Viro 	return 0;
35921da177e4SLinus Torvalds }
35936e66b52bSEric W. Biederman 
3594a528d35eSDavid Howells static int proc_task_getattr(const struct path *path, struct kstat *stat,
3595a528d35eSDavid Howells 			     u32 request_mask, unsigned int query_flags)
35966e66b52bSEric W. Biederman {
3597a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
359899f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
35996e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
36006e66b52bSEric W. Biederman 
360199f89551SEric W. Biederman 	if (p) {
360299f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
360399f89551SEric W. Biederman 		put_task_struct(p);
36046e66b52bSEric W. Biederman 	}
36056e66b52bSEric W. Biederman 
36066e66b52bSEric W. Biederman 	return 0;
36076e66b52bSEric W. Biederman }
360828a6d671SEric W. Biederman 
3609c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
361028a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
361128a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
361228a6d671SEric W. Biederman 	.setattr	= proc_setattr,
36130499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
361428a6d671SEric W. Biederman };
361528a6d671SEric W. Biederman 
361600977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
361728a6d671SEric W. Biederman 	.read		= generic_read_dir,
3618f50752eaSAl Viro 	.iterate_shared	= proc_task_readdir,
3619f50752eaSAl Viro 	.llseek		= generic_file_llseek,
362028a6d671SEric W. Biederman };
36211270dd8dSAlexey Dobriyan 
36221270dd8dSAlexey Dobriyan void __init set_proc_pid_nlink(void)
36231270dd8dSAlexey Dobriyan {
36241270dd8dSAlexey Dobriyan 	nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
36251270dd8dSAlexey Dobriyan 	nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
36261270dd8dSAlexey Dobriyan }
3627