blob: 4e8aa35fc3eb5c9387085ffa941febf0ba03b7a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080076#include <linux/printk.h>
Paul Menagea4243162007-10-18 23:39:35 -070077#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpuset.h>
79#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050080#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070081#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070082#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070083#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070084#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080085#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040086#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080088#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040089#include <linux/posix-timers.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040090#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080093#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040095#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070097/* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700108 const char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700109 int len;
Al Virod161a132011-07-24 03:36:29 -0400110 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800111 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800112 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700118 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 .mode = MODE, \
120 .iop = IOP, \
121 .fop = FOP, \
122 .op = OP, \
123}
124
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300125#define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700129 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300130 { .proc_get_link = get_link } )
131#define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300133#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Vegard Nossumaed54172008-06-05 22:46:53 -0700138/*
139 * Count the number of hardlinks for the pid_entry table, excluding the .
140 * and .. links.
141 */
142static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
143 unsigned int n)
144{
145 unsigned int i;
146 unsigned int count;
147
148 count = 0;
149 for (i = 0; i < n; ++i) {
150 if (S_ISDIR(entries[i].mode))
151 ++count;
152 }
153
154 return count;
155}
156
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200157static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000159 int result = -ENOENT;
160
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200162 if (task->fs) {
163 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000164 result = 0;
165 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700166 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168}
169
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800170static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800172 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700174
175 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200176 task_lock(task);
177 if (task->fs) {
178 get_fs_pwd(task->fs, path);
179 result = 0;
180 }
181 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182 put_task_struct(task);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return result;
185}
186
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800187static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800189 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700191
192 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200193 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194 put_task_struct(task);
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return result;
197}
198
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700199static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
200 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700202 /*
203 * Rely on struct seq_operations::show() being called once
204 * per internal buffer allocation. See single_open(), traverse().
205 */
206 BUG_ON(m->size < PAGE_SIZE);
207 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700211static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
212 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Cong Wange7dcd992012-05-31 16:26:17 -0700214 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500215 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300217 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300219 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700220 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 mmput(mm);
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700222 return 0;
223 } else
224 return PTR_ERR(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227
228#ifdef CONFIG_KALLSYMS
229/*
230 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
231 * Returns the resolved symbol. If that fails, simply return the address.
232 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700233static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
234 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700236 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700237 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 wchan = get_wchan(task);
240
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700241 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600242 if (!ptrace_may_access(task, PTRACE_MODE_READ))
243 return 0;
244 else
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700245 return seq_printf(m, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700246 else
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700247 return seq_printf(m, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249#endif /* CONFIG_KALLSYMS */
250
Al Viroa9712bc2011-03-23 15:52:50 -0400251static int lock_trace(struct task_struct *task)
252{
253 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
254 if (err)
255 return err;
256 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
257 mutex_unlock(&task->signal->cred_guard_mutex);
258 return -EPERM;
259 }
260 return 0;
261}
262
263static void unlock_trace(struct task_struct *task)
264{
265 mutex_unlock(&task->signal->cred_guard_mutex);
266}
267
Ken Chen2ec220e2008-11-10 11:26:08 +0300268#ifdef CONFIG_STACKTRACE
269
270#define MAX_STACK_TRACE_DEPTH 64
271
272static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
273 struct pid *pid, struct task_struct *task)
274{
275 struct stack_trace trace;
276 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400277 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300278 int i;
279
280 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
281 if (!entries)
282 return -ENOMEM;
283
284 trace.nr_entries = 0;
285 trace.max_entries = MAX_STACK_TRACE_DEPTH;
286 trace.entries = entries;
287 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300288
Al Viroa9712bc2011-03-23 15:52:50 -0400289 err = lock_trace(task);
290 if (!err) {
291 save_stack_trace_tsk(task, &trace);
292
293 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700294 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400295 (void *)entries[i], (void *)entries[i]);
296 }
297 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300298 }
299 kfree(entries);
300
Al Viroa9712bc2011-03-23 15:52:50 -0400301 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300302}
303#endif
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#ifdef CONFIG_SCHEDSTATS
306/*
307 * Provides /proc/PID/schedstat
308 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700309static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
310 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700312 return seq_printf(m, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100313 (unsigned long long)task->se.sum_exec_runtime,
314 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200315 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317#endif
318
Arjan van de Ven97455122008-01-25 21:08:34 +0100319#ifdef CONFIG_LATENCYTOP
320static int lstats_show_proc(struct seq_file *m, void *v)
321{
322 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800323 struct inode *inode = m->private;
324 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100325
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800326 if (!task)
327 return -ESRCH;
328 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100329 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800330 struct latency_record *lr = &task->latency_record[i];
331 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100332 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800333 seq_printf(m, "%i %li %li",
334 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100335 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800336 unsigned long bt = lr->backtrace[q];
337 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100338 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800339 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100340 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800341 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100342 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800343 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100344 }
345
346 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800347 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100348 return 0;
349}
350
351static int lstats_open(struct inode *inode, struct file *file)
352{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800353 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800354}
355
Arjan van de Ven97455122008-01-25 21:08:34 +0100356static ssize_t lstats_write(struct file *file, const char __user *buf,
357 size_t count, loff_t *offs)
358{
Al Viro496ad9a2013-01-23 17:07:38 -0500359 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100360
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800361 if (!task)
362 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100363 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800364 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100365
366 return count;
367}
368
369static const struct file_operations proc_lstats_operations = {
370 .open = lstats_open,
371 .read = seq_read,
372 .write = lstats_write,
373 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800374 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100375};
376
377#endif
378
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700379static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
380 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
David Rientjesa7f638f2012-05-29 15:06:47 -0700382 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200383 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700385 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200386 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700387 points = oom_badness(task, NULL, NULL, totalpages) *
388 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700389 read_unlock(&tasklist_lock);
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700390 return seq_printf(m, "%lu\n", points);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Neil Hormand85f50d2007-10-18 23:40:37 -0700393struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700394 const char *name;
395 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700396};
397
398static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700399 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700400 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
401 [RLIMIT_DATA] = {"Max data size", "bytes"},
402 [RLIMIT_STACK] = {"Max stack size", "bytes"},
403 [RLIMIT_CORE] = {"Max core file size", "bytes"},
404 [RLIMIT_RSS] = {"Max resident set", "bytes"},
405 [RLIMIT_NPROC] = {"Max processes", "processes"},
406 [RLIMIT_NOFILE] = {"Max open files", "files"},
407 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
408 [RLIMIT_AS] = {"Max address space", "bytes"},
409 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
410 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
411 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
412 [RLIMIT_NICE] = {"Max nice priority", NULL},
413 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800414 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700415};
416
417/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700418static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
419 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700420{
421 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700422 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700423
424 struct rlimit rlim[RLIM_NLIMITS];
425
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400426 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700427 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700428 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
429 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700430
431 /*
432 * print the file header
433 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700434 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Neil Hormand85f50d2007-10-18 23:40:37 -0700435 "Limit", "Soft Limit", "Hard Limit", "Units");
436
437 for (i = 0; i < RLIM_NLIMITS; i++) {
438 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700439 seq_printf(m, "%-25s %-20s ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700440 lnames[i].name, "unlimited");
441 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700442 seq_printf(m, "%-25s %-20lu ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700443 lnames[i].name, rlim[i].rlim_cur);
444
445 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700446 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700447 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700448 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700449
450 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700451 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700452 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700453 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700454 }
455
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700456 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700457}
458
Roland McGrathebcb6732008-07-25 19:46:00 -0700459#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700460static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
461 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700462{
463 long nr;
464 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400465 int res = lock_trace(task);
466 if (res)
467 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700468
469 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700470 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400471 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700472 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400473 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700474 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700475 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
476 nr,
477 args[0], args[1], args[2], args[3], args[4], args[5],
478 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400479 unlock_trace(task);
480 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700481}
482#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/************************************************************************/
485/* Here the fs part begins */
486/************************************************************************/
487
488/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700489static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700491 struct task_struct *task;
492 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700493 /* Allow access to a task's file descriptors if it is us or we
494 * may use ptrace attach to the process and find out that
495 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700496 */
497 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700498 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400499 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700500 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700501 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700502 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800505int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700506{
507 int error;
508 struct inode *inode = dentry->d_inode;
509
510 if (attr->ia_valid & ATTR_MODE)
511 return -EPERM;
512
513 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200514 if (error)
515 return error;
516
Christoph Hellwig10257742010-06-04 11:30:02 +0200517 setattr_copy(inode, attr);
518 mark_inode_dirty(inode);
519 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700520}
521
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800522/*
523 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
524 * or euid/egid (for hide_pid_min=2)?
525 */
526static bool has_pid_permissions(struct pid_namespace *pid,
527 struct task_struct *task,
528 int hide_pid_min)
529{
530 if (pid->hide_pid < hide_pid_min)
531 return true;
532 if (in_group_p(pid->pid_gid))
533 return true;
534 return ptrace_may_access(task, PTRACE_MODE_READ);
535}
536
537
538static int proc_pid_permission(struct inode *inode, int mask)
539{
540 struct pid_namespace *pid = inode->i_sb->s_fs_info;
541 struct task_struct *task;
542 bool has_perms;
543
544 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800545 if (!task)
546 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800547 has_perms = has_pid_permissions(pid, task, 1);
548 put_task_struct(task);
549
550 if (!has_perms) {
551 if (pid->hide_pid == 2) {
552 /*
553 * Let's make getdents(), stat(), and open()
554 * consistent with each other. If a process
555 * may not stat() a file, it shouldn't be seen
556 * in procfs at all.
557 */
558 return -ENOENT;
559 }
560
561 return -EPERM;
562 }
563 return generic_permission(inode, mask);
564}
565
566
567
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800568static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700569 .setattr = proc_setattr,
570};
571
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800572static int proc_single_show(struct seq_file *m, void *v)
573{
574 struct inode *inode = m->private;
575 struct pid_namespace *ns;
576 struct pid *pid;
577 struct task_struct *task;
578 int ret;
579
580 ns = inode->i_sb->s_fs_info;
581 pid = proc_pid(inode);
582 task = get_pid_task(pid, PIDTYPE_PID);
583 if (!task)
584 return -ESRCH;
585
586 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
587
588 put_task_struct(task);
589 return ret;
590}
591
592static int proc_single_open(struct inode *inode, struct file *filp)
593{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800594 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800595}
596
597static const struct file_operations proc_single_file_operations = {
598 .open = proc_single_open,
599 .read = seq_read,
600 .llseek = seq_lseek,
601 .release = single_release,
602};
603
Cong Wangb409e572012-05-31 16:26:17 -0700604static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Al Viro496ad9a2013-01-23 17:07:38 -0500606 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800607 struct mm_struct *mm;
608
609 if (!task)
610 return -ESRCH;
611
Cong Wangb409e572012-05-31 16:26:17 -0700612 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800613 put_task_struct(task);
614
615 if (IS_ERR(mm))
616 return PTR_ERR(mm);
617
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100618 if (mm) {
619 /* ensure this mm_struct can't be freed */
620 atomic_inc(&mm->mm_count);
621 /* but do not pin its memory */
622 mmput(mm);
623 }
624
Linus Torvaldse2683372012-01-17 15:21:19 -0800625 file->private_data = mm;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628}
629
Cong Wangb409e572012-05-31 16:26:17 -0700630static int mem_open(struct inode *inode, struct file *file)
631{
Djalal Harounibc452b42012-07-30 14:42:28 -0700632 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
633
634 /* OK to pass negative loff_t, we can catch out-of-range */
635 file->f_mode |= FMODE_UNSIGNED_OFFSET;
636
637 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700638}
639
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100640static ssize_t mem_rw(struct file *file, char __user *buf,
641 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Linus Torvaldse2683372012-01-17 15:21:19 -0800643 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100644 unsigned long addr = *ppos;
645 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvaldse2683372012-01-17 15:21:19 -0800648 if (!mm)
649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Mel Gormane12ba742007-10-16 01:25:52 -0700651 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800653 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700655 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100656 if (!atomic_inc_not_zero(&mm->mm_users))
657 goto free;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100660 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100662 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 copied = -EFAULT;
664 break;
665 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100666
667 this_len = access_remote_vm(mm, addr, page, this_len, write);
668 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!copied)
670 copied = -EIO;
671 break;
672 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100673
674 if (!write && copy_to_user(buf, page, this_len)) {
675 copied = -EFAULT;
676 break;
677 }
678
679 buf += this_len;
680 addr += this_len;
681 copied += this_len;
682 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100684 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700685
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100686 mmput(mm);
687free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700688 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return copied;
690}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100692static ssize_t mem_read(struct file *file, char __user *buf,
693 size_t count, loff_t *ppos)
694{
695 return mem_rw(file, buf, count, ppos, 0);
696}
697
698static ssize_t mem_write(struct file *file, const char __user *buf,
699 size_t count, loff_t *ppos)
700{
701 return mem_rw(file, (char __user*)buf, count, ppos, 1);
702}
703
Matt Mackall85863e42008-02-04 22:29:04 -0800704loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 switch (orig) {
707 case 0:
708 file->f_pos = offset;
709 break;
710 case 1:
711 file->f_pos += offset;
712 break;
713 default:
714 return -EINVAL;
715 }
716 force_successful_syscall_return();
717 return file->f_pos;
718}
719
Linus Torvaldse2683372012-01-17 15:21:19 -0800720static int mem_release(struct inode *inode, struct file *file)
721{
722 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100723 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100724 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800725 return 0;
726}
727
Arjan van de Ven00977a52007-02-12 00:55:34 -0800728static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 .llseek = mem_lseek,
730 .read = mem_read,
731 .write = mem_write,
732 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800733 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734};
735
Cong Wangb409e572012-05-31 16:26:17 -0700736static int environ_open(struct inode *inode, struct file *file)
737{
738 return __mem_open(inode, file, PTRACE_MODE_READ);
739}
740
James Pearson315e28c2007-10-16 23:30:17 -0700741static ssize_t environ_read(struct file *file, char __user *buf,
742 size_t count, loff_t *ppos)
743{
James Pearson315e28c2007-10-16 23:30:17 -0700744 char *page;
745 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700746 int ret = 0;
747 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700748
Cong Wangb409e572012-05-31 16:26:17 -0700749 if (!mm)
750 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700751
James Pearson315e28c2007-10-16 23:30:17 -0700752 page = (char *)__get_free_page(GFP_TEMPORARY);
753 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700754 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700755
Al Virod6f64b82011-02-15 22:26:01 -0500756 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700757 if (!atomic_inc_not_zero(&mm->mm_users))
758 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700759 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700760 size_t this_len, max_len;
761 int retval;
762
763 if (src >= (mm->env_end - mm->env_start))
764 break;
James Pearson315e28c2007-10-16 23:30:17 -0700765
766 this_len = mm->env_end - (mm->env_start + src);
767
Djalal Harounie8905ec2012-07-30 14:42:26 -0700768 max_len = min_t(size_t, PAGE_SIZE, count);
769 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700770
Cong Wangb409e572012-05-31 16:26:17 -0700771 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700772 page, this_len, 0);
773
774 if (retval <= 0) {
775 ret = retval;
776 break;
777 }
778
779 if (copy_to_user(buf, page, retval)) {
780 ret = -EFAULT;
781 break;
782 }
783
784 ret += retval;
785 src += retval;
786 buf += retval;
787 count -= retval;
788 }
789 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700790 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700791
792free:
James Pearson315e28c2007-10-16 23:30:17 -0700793 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700794 return ret;
795}
796
797static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700798 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700799 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100800 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700801 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700802};
803
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800804static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
805 loff_t *ppos)
806{
Al Viro496ad9a2013-01-23 17:07:38 -0500807 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800808 char buffer[PROC_NUMBUF];
809 int oom_adj = OOM_ADJUST_MIN;
810 size_t len;
811 unsigned long flags;
812
813 if (!task)
814 return -ESRCH;
815 if (lock_task_sighand(task, &flags)) {
816 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
817 oom_adj = OOM_ADJUST_MAX;
818 else
819 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
820 OOM_SCORE_ADJ_MAX;
821 unlock_task_sighand(task, &flags);
822 }
823 put_task_struct(task);
824 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
825 return simple_read_from_buffer(buf, count, ppos, buffer, len);
826}
827
828static ssize_t oom_adj_write(struct file *file, const char __user *buf,
829 size_t count, loff_t *ppos)
830{
831 struct task_struct *task;
832 char buffer[PROC_NUMBUF];
833 int oom_adj;
834 unsigned long flags;
835 int err;
836
837 memset(buffer, 0, sizeof(buffer));
838 if (count > sizeof(buffer) - 1)
839 count = sizeof(buffer) - 1;
840 if (copy_from_user(buffer, buf, count)) {
841 err = -EFAULT;
842 goto out;
843 }
844
845 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
846 if (err)
847 goto out;
848 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
849 oom_adj != OOM_DISABLE) {
850 err = -EINVAL;
851 goto out;
852 }
853
Al Viro496ad9a2013-01-23 17:07:38 -0500854 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800855 if (!task) {
856 err = -ESRCH;
857 goto out;
858 }
859
860 task_lock(task);
861 if (!task->mm) {
862 err = -EINVAL;
863 goto err_task_lock;
864 }
865
866 if (!lock_task_sighand(task, &flags)) {
867 err = -ESRCH;
868 goto err_task_lock;
869 }
870
871 /*
872 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
873 * value is always attainable.
874 */
875 if (oom_adj == OOM_ADJUST_MAX)
876 oom_adj = OOM_SCORE_ADJ_MAX;
877 else
878 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
879
880 if (oom_adj < task->signal->oom_score_adj &&
881 !capable(CAP_SYS_RESOURCE)) {
882 err = -EACCES;
883 goto err_sighand;
884 }
885
886 /*
887 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
888 * /proc/pid/oom_score_adj instead.
889 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800890 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800891 current->comm, task_pid_nr(current), task_pid_nr(task),
892 task_pid_nr(task));
893
894 task->signal->oom_score_adj = oom_adj;
895 trace_oom_score_adj_update(task);
896err_sighand:
897 unlock_task_sighand(task, &flags);
898err_task_lock:
899 task_unlock(task);
900 put_task_struct(task);
901out:
902 return err < 0 ? err : count;
903}
904
905static const struct file_operations proc_oom_adj_operations = {
906 .read = oom_adj_read,
907 .write = oom_adj_write,
908 .llseek = generic_file_llseek,
909};
910
David Rientjesa63d83f2010-08-09 17:19:46 -0700911static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
912 size_t count, loff_t *ppos)
913{
Al Viro496ad9a2013-01-23 17:07:38 -0500914 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700915 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800916 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700917 unsigned long flags;
918 size_t len;
919
920 if (!task)
921 return -ESRCH;
922 if (lock_task_sighand(task, &flags)) {
923 oom_score_adj = task->signal->oom_score_adj;
924 unlock_task_sighand(task, &flags);
925 }
926 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800927 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700928 return simple_read_from_buffer(buf, count, ppos, buffer, len);
929}
930
931static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
932 size_t count, loff_t *ppos)
933{
934 struct task_struct *task;
935 char buffer[PROC_NUMBUF];
936 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700937 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -0700938 int err;
939
940 memset(buffer, 0, sizeof(buffer));
941 if (count > sizeof(buffer) - 1)
942 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700943 if (copy_from_user(buffer, buf, count)) {
944 err = -EFAULT;
945 goto out;
946 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700947
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700948 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700949 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700950 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -0700951 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -0700952 oom_score_adj > OOM_SCORE_ADJ_MAX) {
953 err = -EINVAL;
954 goto out;
955 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700956
Al Viro496ad9a2013-01-23 17:07:38 -0500957 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -0700958 if (!task) {
959 err = -ESRCH;
960 goto out;
961 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700962
Ying Han3d5992d2010-10-26 14:21:23 -0700963 task_lock(task);
964 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700965 err = -EINVAL;
966 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700967 }
David Rientjesd19d5472010-10-26 14:21:26 -0700968
969 if (!lock_task_sighand(task, &flags)) {
970 err = -ESRCH;
971 goto err_task_lock;
972 }
973
David Rientjesa9c58b902012-12-11 16:02:54 -0800974 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -0700975 !capable(CAP_SYS_RESOURCE)) {
976 err = -EACCES;
977 goto err_sighand;
978 }
979
David Rientjesa9c58b902012-12-11 16:02:54 -0800980 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -0800981 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -0800982 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800983 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -0700984
David Rientjes723548b2010-10-26 14:21:25 -0700985err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -0700986 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -0700987err_task_lock:
988 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -0700989 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -0700990out:
991 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -0700992}
993
994static const struct file_operations proc_oom_score_adj_operations = {
995 .read = oom_score_adj_read,
996 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200997 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -0700998};
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#ifdef CONFIG_AUDITSYSCALL
1001#define TMPBUFLEN 21
1002static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1003 size_t count, loff_t *ppos)
1004{
Al Viro496ad9a2013-01-23 17:07:38 -05001005 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001006 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 ssize_t length;
1008 char tmpbuf[TMPBUFLEN];
1009
Eric W. Biederman99f89552006-06-26 00:25:55 -07001010 if (!task)
1011 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001013 from_kuid(file->f_cred->user_ns,
1014 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001015 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1017}
1018
1019static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1020 size_t count, loff_t *ppos)
1021{
Al Viro496ad9a2013-01-23 17:07:38 -05001022 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 char *page, *tmp;
1024 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001026 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001028 rcu_read_lock();
1029 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1030 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001032 }
1033 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Al Viroe0182902006-05-18 08:28:02 -04001035 if (count >= PAGE_SIZE)
1036 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (*ppos != 0) {
1039 /* No partial writes. */
1040 return -EINVAL;
1041 }
Mel Gormane12ba742007-10-16 01:25:52 -07001042 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!page)
1044 return -ENOMEM;
1045 length = -EFAULT;
1046 if (copy_from_user(page, buf, count))
1047 goto out_free_page;
1048
Al Viroe0182902006-05-18 08:28:02 -04001049 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 loginuid = simple_strtoul(page, &tmp, 10);
1051 if (tmp == page) {
1052 length = -EINVAL;
1053 goto out_free_page;
1054
1055 }
Eric Paris81407c82013-05-24 09:49:14 -04001056
1057 /* is userspace tring to explicitly UNSET the loginuid? */
1058 if (loginuid == AUDIT_UID_UNSET) {
1059 kloginuid = INVALID_UID;
1060 } else {
1061 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1062 if (!uid_valid(kloginuid)) {
1063 length = -EINVAL;
1064 goto out_free_page;
1065 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001066 }
1067
1068 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (likely(length == 0))
1070 length = count;
1071
1072out_free_page:
1073 free_page((unsigned long) page);
1074 return length;
1075}
1076
Arjan van de Ven00977a52007-02-12 00:55:34 -08001077static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 .read = proc_loginuid_read,
1079 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001080 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081};
Eric Paris1e0bd752008-03-13 08:15:31 -04001082
1083static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1084 size_t count, loff_t *ppos)
1085{
Al Viro496ad9a2013-01-23 17:07:38 -05001086 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001087 struct task_struct *task = get_proc_task(inode);
1088 ssize_t length;
1089 char tmpbuf[TMPBUFLEN];
1090
1091 if (!task)
1092 return -ESRCH;
1093 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1094 audit_get_sessionid(task));
1095 put_task_struct(task);
1096 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1097}
1098
1099static const struct file_operations proc_sessionid_operations = {
1100 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001101 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001102};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103#endif
1104
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001105#ifdef CONFIG_FAULT_INJECTION
1106static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1107 size_t count, loff_t *ppos)
1108{
Al Viro496ad9a2013-01-23 17:07:38 -05001109 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001110 char buffer[PROC_NUMBUF];
1111 size_t len;
1112 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001113
1114 if (!task)
1115 return -ESRCH;
1116 make_it_fail = task->make_it_fail;
1117 put_task_struct(task);
1118
1119 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001120
1121 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001122}
1123
1124static ssize_t proc_fault_inject_write(struct file * file,
1125 const char __user * buf, size_t count, loff_t *ppos)
1126{
1127 struct task_struct *task;
1128 char buffer[PROC_NUMBUF], *end;
1129 int make_it_fail;
1130
1131 if (!capable(CAP_SYS_RESOURCE))
1132 return -EPERM;
1133 memset(buffer, 0, sizeof(buffer));
1134 if (count > sizeof(buffer) - 1)
1135 count = sizeof(buffer) - 1;
1136 if (copy_from_user(buffer, buf, count))
1137 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001138 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1139 if (*end)
1140 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001141 if (make_it_fail < 0 || make_it_fail > 1)
1142 return -EINVAL;
1143
Al Viro496ad9a2013-01-23 17:07:38 -05001144 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001145 if (!task)
1146 return -ESRCH;
1147 task->make_it_fail = make_it_fail;
1148 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001149
1150 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001151}
1152
Arjan van de Ven00977a52007-02-12 00:55:34 -08001153static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001154 .read = proc_fault_inject_read,
1155 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001156 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001157};
1158#endif
1159
Arjan van de Ven97455122008-01-25 21:08:34 +01001160
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001161#ifdef CONFIG_SCHED_DEBUG
1162/*
1163 * Print out various scheduling related per-task fields:
1164 */
1165static int sched_show(struct seq_file *m, void *v)
1166{
1167 struct inode *inode = m->private;
1168 struct task_struct *p;
1169
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001170 p = get_proc_task(inode);
1171 if (!p)
1172 return -ESRCH;
1173 proc_sched_show_task(p, m);
1174
1175 put_task_struct(p);
1176
1177 return 0;
1178}
1179
1180static ssize_t
1181sched_write(struct file *file, const char __user *buf,
1182 size_t count, loff_t *offset)
1183{
Al Viro496ad9a2013-01-23 17:07:38 -05001184 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001185 struct task_struct *p;
1186
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001187 p = get_proc_task(inode);
1188 if (!p)
1189 return -ESRCH;
1190 proc_sched_set_task(p);
1191
1192 put_task_struct(p);
1193
1194 return count;
1195}
1196
1197static int sched_open(struct inode *inode, struct file *filp)
1198{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001199 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001200}
1201
1202static const struct file_operations proc_pid_sched_operations = {
1203 .open = sched_open,
1204 .read = seq_read,
1205 .write = sched_write,
1206 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001207 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001208};
1209
1210#endif
1211
Mike Galbraith5091faa2010-11-30 14:18:03 +01001212#ifdef CONFIG_SCHED_AUTOGROUP
1213/*
1214 * Print out autogroup related information:
1215 */
1216static int sched_autogroup_show(struct seq_file *m, void *v)
1217{
1218 struct inode *inode = m->private;
1219 struct task_struct *p;
1220
1221 p = get_proc_task(inode);
1222 if (!p)
1223 return -ESRCH;
1224 proc_sched_autogroup_show_task(p, m);
1225
1226 put_task_struct(p);
1227
1228 return 0;
1229}
1230
1231static ssize_t
1232sched_autogroup_write(struct file *file, const char __user *buf,
1233 size_t count, loff_t *offset)
1234{
Al Viro496ad9a2013-01-23 17:07:38 -05001235 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001236 struct task_struct *p;
1237 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001238 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001239 int err;
1240
1241 memset(buffer, 0, sizeof(buffer));
1242 if (count > sizeof(buffer) - 1)
1243 count = sizeof(buffer) - 1;
1244 if (copy_from_user(buffer, buf, count))
1245 return -EFAULT;
1246
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001247 err = kstrtoint(strstrip(buffer), 0, &nice);
1248 if (err < 0)
1249 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001250
1251 p = get_proc_task(inode);
1252 if (!p)
1253 return -ESRCH;
1254
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001255 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001256 if (err)
1257 count = err;
1258
1259 put_task_struct(p);
1260
1261 return count;
1262}
1263
1264static int sched_autogroup_open(struct inode *inode, struct file *filp)
1265{
1266 int ret;
1267
1268 ret = single_open(filp, sched_autogroup_show, NULL);
1269 if (!ret) {
1270 struct seq_file *m = filp->private_data;
1271
1272 m->private = inode;
1273 }
1274 return ret;
1275}
1276
1277static const struct file_operations proc_pid_sched_autogroup_operations = {
1278 .open = sched_autogroup_open,
1279 .read = seq_read,
1280 .write = sched_autogroup_write,
1281 .llseek = seq_lseek,
1282 .release = single_release,
1283};
1284
1285#endif /* CONFIG_SCHED_AUTOGROUP */
1286
john stultz4614a696b2009-12-14 18:00:05 -08001287static ssize_t comm_write(struct file *file, const char __user *buf,
1288 size_t count, loff_t *offset)
1289{
Al Viro496ad9a2013-01-23 17:07:38 -05001290 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001291 struct task_struct *p;
1292 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001293 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001294
1295 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001296 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001297 return -EFAULT;
1298
1299 p = get_proc_task(inode);
1300 if (!p)
1301 return -ESRCH;
1302
1303 if (same_thread_group(current, p))
1304 set_task_comm(p, buffer);
1305 else
1306 count = -EINVAL;
1307
1308 put_task_struct(p);
1309
1310 return count;
1311}
1312
1313static int comm_show(struct seq_file *m, void *v)
1314{
1315 struct inode *inode = m->private;
1316 struct task_struct *p;
1317
1318 p = get_proc_task(inode);
1319 if (!p)
1320 return -ESRCH;
1321
1322 task_lock(p);
1323 seq_printf(m, "%s\n", p->comm);
1324 task_unlock(p);
1325
1326 put_task_struct(p);
1327
1328 return 0;
1329}
1330
1331static int comm_open(struct inode *inode, struct file *filp)
1332{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001333 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001334}
1335
1336static const struct file_operations proc_pid_set_comm_operations = {
1337 .open = comm_open,
1338 .read = seq_read,
1339 .write = comm_write,
1340 .llseek = seq_lseek,
1341 .release = single_release,
1342};
1343
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001344static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001345{
1346 struct task_struct *task;
1347 struct mm_struct *mm;
1348 struct file *exe_file;
1349
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001350 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001351 if (!task)
1352 return -ENOENT;
1353 mm = get_task_mm(task);
1354 put_task_struct(task);
1355 if (!mm)
1356 return -ENOENT;
1357 exe_file = get_mm_exe_file(mm);
1358 mmput(mm);
1359 if (exe_file) {
1360 *exe_path = exe_file->f_path;
1361 path_get(&exe_file->f_path);
1362 fput(exe_file);
1363 return 0;
1364 } else
1365 return -ENOENT;
1366}
1367
Al Viro008b1502005-08-20 00:17:39 +01001368static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001371 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int error = -EACCES;
1373
Eric W. Biederman778c1142006-06-26 00:25:58 -07001374 /* Are we allowed to snoop on the tasks file descriptors? */
1375 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Christoph Hellwig408ef012012-06-18 10:47:03 -04001378 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1379 if (error)
1380 goto out;
1381
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001382 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001383 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384out:
Al Viro008b1502005-08-20 00:17:39 +01001385 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001388static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Mel Gormane12ba742007-10-16 01:25:52 -07001390 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001391 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int len;
1393
1394 if (!tmp)
1395 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001396
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001397 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001398 len = PTR_ERR(pathname);
1399 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001401 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if (len > buflen)
1404 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001405 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 len = -EFAULT;
1407 out:
1408 free_page((unsigned long)tmp);
1409 return len;
1410}
1411
1412static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1413{
1414 int error = -EACCES;
1415 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001416 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Eric W. Biederman778c1142006-06-26 00:25:58 -07001418 /* Are we allowed to snoop on the tasks file descriptors? */
1419 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001422 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (error)
1424 goto out;
1425
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001426 error = do_proc_readlink(&path, buffer, buflen);
1427 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return error;
1430}
1431
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001432const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001434 .follow_link = proc_pid_follow_link,
1435 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436};
1437
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001438
1439/* building an inode */
1440
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001441struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001442{
1443 struct inode * inode;
1444 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001445 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001446
1447 /* We need a new inode */
1448
1449 inode = new_inode(sb);
1450 if (!inode)
1451 goto out;
1452
1453 /* Common stuff */
1454 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001455 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001456 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001457 inode->i_op = &proc_def_inode_operations;
1458
1459 /*
1460 * grab the reference to task.
1461 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001462 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001463 if (!ei->pid)
1464 goto out_unlock;
1465
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001466 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001467 rcu_read_lock();
1468 cred = __task_cred(task);
1469 inode->i_uid = cred->euid;
1470 inode->i_gid = cred->egid;
1471 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001472 }
1473 security_task_to_inode(task, inode);
1474
1475out:
1476 return inode;
1477
1478out_unlock:
1479 iput(inode);
1480 return NULL;
1481}
1482
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001483int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001484{
1485 struct inode *inode = dentry->d_inode;
1486 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001487 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001488 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001489
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001490 generic_fillattr(inode, stat);
1491
1492 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001493 stat->uid = GLOBAL_ROOT_UID;
1494 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001495 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1496 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001497 if (!has_pid_permissions(pid, task, 2)) {
1498 rcu_read_unlock();
1499 /*
1500 * This doesn't prevent learning whether PID exists,
1501 * it only makes getattr() consistent with readdir().
1502 */
1503 return -ENOENT;
1504 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001505 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1506 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001507 cred = __task_cred(task);
1508 stat->uid = cred->euid;
1509 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001510 }
1511 }
1512 rcu_read_unlock();
1513 return 0;
1514}
1515
1516/* dentry stuff */
1517
1518/*
1519 * Exceptional case: normally we are not allowed to unhash a busy
1520 * directory. In this case, however, we can do it - no aliasing problems
1521 * due to the way we treat inodes.
1522 *
1523 * Rewrite the inode's ownerships here because the owning task may have
1524 * performed a setuid(), etc.
1525 *
1526 * Before the /proc/pid/status file was created the only way to read
1527 * the effective uid of a /process was to stat /proc/pid. Reading
1528 * /proc/pid/status is slow enough that procps and other packages
1529 * kept stating /proc/pid. To keep the rules in /proc simple I have
1530 * made this apply to all per process world readable and executable
1531 * directories.
1532 */
Al Viro0b728e12012-06-10 16:03:43 -04001533int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001534{
Nick Piggin34286d62011-01-07 17:49:57 +11001535 struct inode *inode;
1536 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001537 const struct cred *cred;
1538
Al Viro0b728e12012-06-10 16:03:43 -04001539 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001540 return -ECHILD;
1541
1542 inode = dentry->d_inode;
1543 task = get_proc_task(inode);
1544
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001545 if (task) {
1546 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1547 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001548 rcu_read_lock();
1549 cred = __task_cred(task);
1550 inode->i_uid = cred->euid;
1551 inode->i_gid = cred->egid;
1552 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001553 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001554 inode->i_uid = GLOBAL_ROOT_UID;
1555 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001556 }
1557 inode->i_mode &= ~(S_ISUID | S_ISGID);
1558 security_task_to_inode(task, inode);
1559 put_task_struct(task);
1560 return 1;
1561 }
1562 d_drop(dentry);
1563 return 0;
1564}
1565
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001566static inline bool proc_inode_is_dead(struct inode *inode)
1567{
1568 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1569}
1570
David Howells1dd704b2013-04-12 01:08:50 +01001571int pid_delete_dentry(const struct dentry *dentry)
1572{
1573 /* Is the task we represent dead?
1574 * If so, then don't put the dentry on the lru list,
1575 * kill it immediately.
1576 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001577 return proc_inode_is_dead(dentry->d_inode);
David Howells1dd704b2013-04-12 01:08:50 +01001578}
1579
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001580const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001581{
1582 .d_revalidate = pid_revalidate,
1583 .d_delete = pid_delete_dentry,
1584};
1585
1586/* Lookups */
1587
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001588/*
1589 * Fill a directory entry.
1590 *
1591 * If possible create the dcache entry and derive our inode number and
1592 * file type from dcache entry.
1593 *
1594 * Since all of the proc inode numbers are dynamically generated, the inode
1595 * numbers do not exist until the inode is cache. This means creating the
1596 * the dcache entry in readdir is necessary to keep the inode numbers
1597 * reported by readdir in sync with the inode numbers reported
1598 * by stat.
1599 */
Al Virof0c3b502013-05-16 12:07:31 -04001600bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001601 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001602 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001603{
Al Virof0c3b502013-05-16 12:07:31 -04001604 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001605 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001606 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001607 unsigned type;
1608 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001609
Al Viro1df98b82013-06-15 11:33:10 +04001610 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001611 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001612 child = d_alloc(dir, &qname);
1613 if (!child)
1614 goto end_instantiate;
1615 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1616 dput(child);
1617 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001618 }
1619 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001620 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001621 ino = inode->i_ino;
1622 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001623 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001624 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001625
1626end_instantiate:
1627 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001628}
1629
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001630#ifdef CONFIG_CHECKPOINT_RESTORE
1631
1632/*
1633 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1634 * which represent vma start and end addresses.
1635 */
1636static int dname_to_vma_addr(struct dentry *dentry,
1637 unsigned long *start, unsigned long *end)
1638{
1639 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1640 return -EINVAL;
1641
1642 return 0;
1643}
1644
Al Viro0b728e12012-06-10 16:03:43 -04001645static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001646{
1647 unsigned long vm_start, vm_end;
1648 bool exact_vma_exists = false;
1649 struct mm_struct *mm = NULL;
1650 struct task_struct *task;
1651 const struct cred *cred;
1652 struct inode *inode;
1653 int status = 0;
1654
Al Viro0b728e12012-06-10 16:03:43 -04001655 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001656 return -ECHILD;
1657
1658 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001659 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001660 goto out_notask;
1661 }
1662
1663 inode = dentry->d_inode;
1664 task = get_proc_task(inode);
1665 if (!task)
1666 goto out_notask;
1667
Cong Wang2344bec2012-05-31 16:26:18 -07001668 mm = mm_access(task, PTRACE_MODE_READ);
1669 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001670 goto out;
1671
1672 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1673 down_read(&mm->mmap_sem);
1674 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1675 up_read(&mm->mmap_sem);
1676 }
1677
1678 mmput(mm);
1679
1680 if (exact_vma_exists) {
1681 if (task_dumpable(task)) {
1682 rcu_read_lock();
1683 cred = __task_cred(task);
1684 inode->i_uid = cred->euid;
1685 inode->i_gid = cred->egid;
1686 rcu_read_unlock();
1687 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001688 inode->i_uid = GLOBAL_ROOT_UID;
1689 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001690 }
1691 security_task_to_inode(task, inode);
1692 status = 1;
1693 }
1694
1695out:
1696 put_task_struct(task);
1697
1698out_notask:
1699 if (status <= 0)
1700 d_drop(dentry);
1701
1702 return status;
1703}
1704
1705static const struct dentry_operations tid_map_files_dentry_operations = {
1706 .d_revalidate = map_files_d_revalidate,
1707 .d_delete = pid_delete_dentry,
1708};
1709
1710static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1711{
1712 unsigned long vm_start, vm_end;
1713 struct vm_area_struct *vma;
1714 struct task_struct *task;
1715 struct mm_struct *mm;
1716 int rc;
1717
1718 rc = -ENOENT;
1719 task = get_proc_task(dentry->d_inode);
1720 if (!task)
1721 goto out;
1722
1723 mm = get_task_mm(task);
1724 put_task_struct(task);
1725 if (!mm)
1726 goto out;
1727
1728 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1729 if (rc)
1730 goto out_mmput;
1731
Artem Fetishev70335ab2014-03-10 15:49:45 -07001732 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001733 down_read(&mm->mmap_sem);
1734 vma = find_exact_vma(mm, vm_start, vm_end);
1735 if (vma && vma->vm_file) {
1736 *path = vma->vm_file->f_path;
1737 path_get(path);
1738 rc = 0;
1739 }
1740 up_read(&mm->mmap_sem);
1741
1742out_mmput:
1743 mmput(mm);
1744out:
1745 return rc;
1746}
1747
1748struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001749 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001750 unsigned long len;
1751 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1752};
1753
Al Viroc52a47a2013-06-15 11:15:20 +04001754static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001755proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1756 struct task_struct *task, const void *ptr)
1757{
Al Viro7b540d02012-08-27 14:55:26 -04001758 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001759 struct proc_inode *ei;
1760 struct inode *inode;
1761
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001762 inode = proc_pid_make_inode(dir->i_sb, task);
1763 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001764 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001765
1766 ei = PROC_I(inode);
1767 ei->op.proc_get_link = proc_map_files_get_link;
1768
1769 inode->i_op = &proc_pid_link_inode_operations;
1770 inode->i_size = 64;
1771 inode->i_mode = S_IFLNK;
1772
Al Viro7b540d02012-08-27 14:55:26 -04001773 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001774 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001775 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001776 inode->i_mode |= S_IWUSR;
1777
1778 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1779 d_add(dentry, inode);
1780
Al Viroc52a47a2013-06-15 11:15:20 +04001781 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001782}
1783
1784static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001785 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001786{
1787 unsigned long vm_start, vm_end;
1788 struct vm_area_struct *vma;
1789 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001790 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001791 struct mm_struct *mm;
1792
Al Viroc52a47a2013-06-15 11:15:20 +04001793 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001794 if (!capable(CAP_SYS_ADMIN))
1795 goto out;
1796
Al Viroc52a47a2013-06-15 11:15:20 +04001797 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001798 task = get_proc_task(dir);
1799 if (!task)
1800 goto out;
1801
Al Viroc52a47a2013-06-15 11:15:20 +04001802 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001803 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001804 goto out_put_task;
1805
Al Viroc52a47a2013-06-15 11:15:20 +04001806 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001807 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001808 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001809
1810 mm = get_task_mm(task);
1811 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001812 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001813
1814 down_read(&mm->mmap_sem);
1815 vma = find_exact_vma(mm, vm_start, vm_end);
1816 if (!vma)
1817 goto out_no_vma;
1818
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001819 if (vma->vm_file)
1820 result = proc_map_files_instantiate(dir, dentry, task,
1821 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001822
1823out_no_vma:
1824 up_read(&mm->mmap_sem);
1825 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001826out_put_task:
1827 put_task_struct(task);
1828out:
Al Viroc52a47a2013-06-15 11:15:20 +04001829 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001830}
1831
1832static const struct inode_operations proc_map_files_inode_operations = {
1833 .lookup = proc_map_files_lookup,
1834 .permission = proc_fd_permission,
1835 .setattr = proc_setattr,
1836};
1837
1838static int
Al Virof0c3b502013-05-16 12:07:31 -04001839proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001840{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001841 struct vm_area_struct *vma;
1842 struct task_struct *task;
1843 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001844 unsigned long nr_files, pos, i;
1845 struct flex_array *fa = NULL;
1846 struct map_files_info info;
1847 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001848 int ret;
1849
Zhao Hongjiang41735812013-02-20 13:13:55 +11001850 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001851 if (!capable(CAP_SYS_ADMIN))
1852 goto out;
1853
1854 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001855 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001856 if (!task)
1857 goto out;
1858
1859 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001860 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001861 goto out_put_task;
1862
1863 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001864 if (!dir_emit_dots(file, ctx))
1865 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001866
Al Virof0c3b502013-05-16 12:07:31 -04001867 mm = get_task_mm(task);
1868 if (!mm)
1869 goto out_put_task;
1870 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001871
Al Virof0c3b502013-05-16 12:07:31 -04001872 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001873
Al Virof0c3b502013-05-16 12:07:31 -04001874 /*
1875 * We need two passes here:
1876 *
1877 * 1) Collect vmas of mapped files with mmap_sem taken
1878 * 2) Release mmap_sem and instantiate entries
1879 *
1880 * otherwise we get lockdep complained, since filldir()
1881 * routine might require mmap_sem taken in might_fault().
1882 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001883
Al Virof0c3b502013-05-16 12:07:31 -04001884 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1885 if (vma->vm_file && ++pos > ctx->pos)
1886 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001887 }
Al Virof0c3b502013-05-16 12:07:31 -04001888
1889 if (nr_files) {
1890 fa = flex_array_alloc(sizeof(info), nr_files,
1891 GFP_KERNEL);
1892 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1893 GFP_KERNEL)) {
1894 ret = -ENOMEM;
1895 if (fa)
1896 flex_array_free(fa);
1897 up_read(&mm->mmap_sem);
1898 mmput(mm);
1899 goto out_put_task;
1900 }
1901 for (i = 0, vma = mm->mmap, pos = 2; vma;
1902 vma = vma->vm_next) {
1903 if (!vma->vm_file)
1904 continue;
1905 if (++pos <= ctx->pos)
1906 continue;
1907
1908 info.mode = vma->vm_file->f_mode;
1909 info.len = snprintf(info.name,
1910 sizeof(info.name), "%lx-%lx",
1911 vma->vm_start, vma->vm_end);
1912 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1913 BUG();
1914 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001915 }
Al Virof0c3b502013-05-16 12:07:31 -04001916 up_read(&mm->mmap_sem);
1917
1918 for (i = 0; i < nr_files; i++) {
1919 p = flex_array_get(fa, i);
1920 if (!proc_fill_cache(file, ctx,
1921 p->name, p->len,
1922 proc_map_files_instantiate,
1923 task,
1924 (void *)(unsigned long)p->mode))
1925 break;
1926 ctx->pos++;
1927 }
1928 if (fa)
1929 flex_array_free(fa);
1930 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001931
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001932out_put_task:
1933 put_task_struct(task);
1934out:
1935 return ret;
1936}
1937
1938static const struct file_operations proc_map_files_operations = {
1939 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04001940 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001941 .llseek = default_llseek,
1942};
1943
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001944struct timers_private {
1945 struct pid *pid;
1946 struct task_struct *task;
1947 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04001948 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001949 unsigned long flags;
1950};
1951
1952static void *timers_start(struct seq_file *m, loff_t *pos)
1953{
1954 struct timers_private *tp = m->private;
1955
1956 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
1957 if (!tp->task)
1958 return ERR_PTR(-ESRCH);
1959
1960 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
1961 if (!tp->sighand)
1962 return ERR_PTR(-ESRCH);
1963
1964 return seq_list_start(&tp->task->signal->posix_timers, *pos);
1965}
1966
1967static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
1968{
1969 struct timers_private *tp = m->private;
1970 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
1971}
1972
1973static void timers_stop(struct seq_file *m, void *v)
1974{
1975 struct timers_private *tp = m->private;
1976
1977 if (tp->sighand) {
1978 unlock_task_sighand(tp->task, &tp->flags);
1979 tp->sighand = NULL;
1980 }
1981
1982 if (tp->task) {
1983 put_task_struct(tp->task);
1984 tp->task = NULL;
1985 }
1986}
1987
1988static int show_timer(struct seq_file *m, void *v)
1989{
1990 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04001991 struct timers_private *tp = m->private;
1992 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07001993 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04001994 [SIGEV_SIGNAL] = "signal",
1995 [SIGEV_NONE] = "none",
1996 [SIGEV_THREAD] = "thread",
1997 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001998
1999 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002000 notify = timer->it_sigev_notify;
2001
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002002 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002003 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2004 timer->sigq->info.si_value.sival_ptr);
2005 seq_printf(m, "notify: %s/%s.%d\n",
2006 nstr[notify & ~SIGEV_THREAD_ID],
2007 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2008 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002009 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002010
2011 return 0;
2012}
2013
2014static const struct seq_operations proc_timers_seq_ops = {
2015 .start = timers_start,
2016 .next = timers_next,
2017 .stop = timers_stop,
2018 .show = show_timer,
2019};
2020
2021static int proc_timers_open(struct inode *inode, struct file *file)
2022{
2023 struct timers_private *tp;
2024
2025 tp = __seq_open_private(file, &proc_timers_seq_ops,
2026 sizeof(struct timers_private));
2027 if (!tp)
2028 return -ENOMEM;
2029
2030 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002031 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002032 return 0;
2033}
2034
2035static const struct file_operations proc_timers_operations = {
2036 .open = proc_timers_open,
2037 .read = seq_read,
2038 .llseek = seq_lseek,
2039 .release = seq_release_private,
2040};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002041#endif /* CONFIG_CHECKPOINT_RESTORE */
2042
Al Viroc52a47a2013-06-15 11:15:20 +04002043static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002044 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002045{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002046 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002047 struct inode *inode;
2048 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002049
Eric W. Biederman61a28782006-10-02 02:18:49 -07002050 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002051 if (!inode)
2052 goto out;
2053
2054 ei = PROC_I(inode);
2055 inode->i_mode = p->mode;
2056 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002057 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002058 if (p->iop)
2059 inode->i_op = p->iop;
2060 if (p->fop)
2061 inode->i_fop = p->fop;
2062 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002063 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002064 d_add(dentry, inode);
2065 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002066 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002067 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002068out:
Al Viroc52a47a2013-06-15 11:15:20 +04002069 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002070}
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072static struct dentry *proc_pident_lookup(struct inode *dir,
2073 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002074 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002075 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Al Viroc52a47a2013-06-15 11:15:20 +04002077 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002078 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002079 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Al Viroc52a47a2013-06-15 11:15:20 +04002081 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Eric W. Biederman99f89552006-06-26 00:25:55 -07002083 if (!task)
2084 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002086 /*
2087 * Yes, it does not scale. And it should not. Don't add
2088 * new entries into /proc/<tgid>/ without very good reasons.
2089 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002090 last = &ents[nents - 1];
2091 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (p->len != dentry->d_name.len)
2093 continue;
2094 if (!memcmp(dentry->d_name.name, p->name, p->len))
2095 break;
2096 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002097 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto out;
2099
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002100 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002102 put_task_struct(task);
2103out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002104 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Al Virof0c3b502013-05-16 12:07:31 -04002107static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002108 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002109{
Al Virof0c3b502013-05-16 12:07:31 -04002110 struct task_struct *task = get_proc_task(file_inode(file));
2111 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002112
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002113 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002114 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002115
Al Virof0c3b502013-05-16 12:07:31 -04002116 if (!dir_emit_dots(file, ctx))
2117 goto out;
2118
2119 if (ctx->pos >= nents + 2)
2120 goto out;
2121
2122 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2123 if (!proc_fill_cache(file, ctx, p->name, p->len,
2124 proc_pident_instantiate, task, p))
2125 break;
2126 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002127 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002128out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002129 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002134static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2135 size_t count, loff_t *ppos)
2136{
Al Viro496ad9a2013-01-23 17:07:38 -05002137 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002138 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002139 ssize_t length;
2140 struct task_struct *task = get_proc_task(inode);
2141
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002142 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002143 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002144
2145 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002146 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002147 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002148 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002149 if (length > 0)
2150 length = simple_read_from_buffer(buf, count, ppos, p, length);
2151 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002152 return length;
2153}
2154
2155static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2156 size_t count, loff_t *ppos)
2157{
Al Viro496ad9a2013-01-23 17:07:38 -05002158 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002159 char *page;
2160 ssize_t length;
2161 struct task_struct *task = get_proc_task(inode);
2162
2163 length = -ESRCH;
2164 if (!task)
2165 goto out_no_task;
2166 if (count > PAGE_SIZE)
2167 count = PAGE_SIZE;
2168
2169 /* No partial writes. */
2170 length = -EINVAL;
2171 if (*ppos != 0)
2172 goto out;
2173
2174 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002175 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002176 if (!page)
2177 goto out;
2178
2179 length = -EFAULT;
2180 if (copy_from_user(page, buf, count))
2181 goto out_free;
2182
David Howells107db7c2009-05-08 13:55:27 +01002183 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002184 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002185 if (length < 0)
2186 goto out_free;
2187
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002188 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002189 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002190 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002191 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002192out_free:
2193 free_page((unsigned long) page);
2194out:
2195 put_task_struct(task);
2196out_no_task:
2197 return length;
2198}
2199
Arjan van de Ven00977a52007-02-12 00:55:34 -08002200static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002201 .read = proc_pid_attr_read,
2202 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002203 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002204};
2205
Eric Dumazetc5141e62007-05-08 00:26:15 -07002206static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002207 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2208 REG("prev", S_IRUGO, proc_pid_attr_operations),
2209 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2210 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2211 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2212 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002213};
2214
Al Virof0c3b502013-05-16 12:07:31 -04002215static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Al Virof0c3b502013-05-16 12:07:31 -04002217 return proc_pident_readdir(file, ctx,
2218 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
Arjan van de Ven00977a52007-02-12 00:55:34 -08002221static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002223 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002224 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225};
2226
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002227static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002228 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002230 return proc_pident_lookup(dir, dentry,
2231 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002234static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002235 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002236 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002237 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238};
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240#endif
2241
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002242#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002243static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2244 size_t count, loff_t *ppos)
2245{
Al Viro496ad9a2013-01-23 17:07:38 -05002246 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002247 struct mm_struct *mm;
2248 char buffer[PROC_NUMBUF];
2249 size_t len;
2250 int ret;
2251
2252 if (!task)
2253 return -ESRCH;
2254
2255 ret = 0;
2256 mm = get_task_mm(task);
2257 if (mm) {
2258 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2259 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2260 MMF_DUMP_FILTER_SHIFT));
2261 mmput(mm);
2262 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2263 }
2264
2265 put_task_struct(task);
2266
2267 return ret;
2268}
2269
2270static ssize_t proc_coredump_filter_write(struct file *file,
2271 const char __user *buf,
2272 size_t count,
2273 loff_t *ppos)
2274{
2275 struct task_struct *task;
2276 struct mm_struct *mm;
2277 char buffer[PROC_NUMBUF], *end;
2278 unsigned int val;
2279 int ret;
2280 int i;
2281 unsigned long mask;
2282
2283 ret = -EFAULT;
2284 memset(buffer, 0, sizeof(buffer));
2285 if (count > sizeof(buffer) - 1)
2286 count = sizeof(buffer) - 1;
2287 if (copy_from_user(buffer, buf, count))
2288 goto out_no_task;
2289
2290 ret = -EINVAL;
2291 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2292 if (*end == '\n')
2293 end++;
2294 if (end - buffer == 0)
2295 goto out_no_task;
2296
2297 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002298 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002299 if (!task)
2300 goto out_no_task;
2301
2302 ret = end - buffer;
2303 mm = get_task_mm(task);
2304 if (!mm)
2305 goto out_no_mm;
2306
2307 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2308 if (val & mask)
2309 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2310 else
2311 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2312 }
2313
2314 mmput(mm);
2315 out_no_mm:
2316 put_task_struct(task);
2317 out_no_task:
2318 return ret;
2319}
2320
2321static const struct file_operations proc_coredump_filter_operations = {
2322 .read = proc_coredump_filter_read,
2323 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002324 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002325};
2326#endif
2327
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002328#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002329static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002330{
Andrea Righi940389b2008-07-28 00:48:12 +02002331 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002332 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002333 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002334
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002335 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2336 if (result)
2337 return result;
2338
2339 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2340 result = -EACCES;
2341 goto out_unlock;
2342 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002343
Andrea Righi59954772008-07-27 17:29:15 +02002344 if (whole && lock_task_sighand(task, &flags)) {
2345 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002346
Andrea Righi59954772008-07-27 17:29:15 +02002347 task_io_accounting_add(&acct, &task->signal->ioac);
2348 while_each_thread(task, t)
2349 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002350
Andrea Righi59954772008-07-27 17:29:15 +02002351 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002352 }
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002353 result = seq_printf(m,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002354 "rchar: %llu\n"
2355 "wchar: %llu\n"
2356 "syscr: %llu\n"
2357 "syscw: %llu\n"
2358 "read_bytes: %llu\n"
2359 "write_bytes: %llu\n"
2360 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002361 (unsigned long long)acct.rchar,
2362 (unsigned long long)acct.wchar,
2363 (unsigned long long)acct.syscr,
2364 (unsigned long long)acct.syscw,
2365 (unsigned long long)acct.read_bytes,
2366 (unsigned long long)acct.write_bytes,
2367 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002368out_unlock:
2369 mutex_unlock(&task->signal->cred_guard_mutex);
2370 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002371}
Andrea Righi297c5d92008-07-25 01:48:49 -07002372
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002373static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2374 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002375{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002376 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002377}
2378
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002379static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2380 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002381{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002382 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002383}
2384#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002385
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002386#ifdef CONFIG_USER_NS
2387static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002388 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002389{
2390 struct user_namespace *ns = NULL;
2391 struct task_struct *task;
2392 struct seq_file *seq;
2393 int ret = -EINVAL;
2394
2395 task = get_proc_task(inode);
2396 if (task) {
2397 rcu_read_lock();
2398 ns = get_user_ns(task_cred_xxx(task, user_ns));
2399 rcu_read_unlock();
2400 put_task_struct(task);
2401 }
2402 if (!ns)
2403 goto err;
2404
2405 ret = seq_open(file, seq_ops);
2406 if (ret)
2407 goto err_put_ns;
2408
2409 seq = file->private_data;
2410 seq->private = ns;
2411
2412 return 0;
2413err_put_ns:
2414 put_user_ns(ns);
2415err:
2416 return ret;
2417}
2418
2419static int proc_id_map_release(struct inode *inode, struct file *file)
2420{
2421 struct seq_file *seq = file->private_data;
2422 struct user_namespace *ns = seq->private;
2423 put_user_ns(ns);
2424 return seq_release(inode, file);
2425}
2426
2427static int proc_uid_map_open(struct inode *inode, struct file *file)
2428{
2429 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2430}
2431
2432static int proc_gid_map_open(struct inode *inode, struct file *file)
2433{
2434 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2435}
2436
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002437static int proc_projid_map_open(struct inode *inode, struct file *file)
2438{
2439 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2440}
2441
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002442static const struct file_operations proc_uid_map_operations = {
2443 .open = proc_uid_map_open,
2444 .write = proc_uid_map_write,
2445 .read = seq_read,
2446 .llseek = seq_lseek,
2447 .release = proc_id_map_release,
2448};
2449
2450static const struct file_operations proc_gid_map_operations = {
2451 .open = proc_gid_map_open,
2452 .write = proc_gid_map_write,
2453 .read = seq_read,
2454 .llseek = seq_lseek,
2455 .release = proc_id_map_release,
2456};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002457
2458static const struct file_operations proc_projid_map_operations = {
2459 .open = proc_projid_map_open,
2460 .write = proc_projid_map_write,
2461 .read = seq_read,
2462 .llseek = seq_lseek,
2463 .release = proc_id_map_release,
2464};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002465#endif /* CONFIG_USER_NS */
2466
Kees Cook47830722008-10-06 03:11:58 +04002467static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2468 struct pid *pid, struct task_struct *task)
2469{
Al Viroa9712bc2011-03-23 15:52:50 -04002470 int err = lock_trace(task);
2471 if (!err) {
2472 seq_printf(m, "%08x\n", task->personality);
2473 unlock_trace(task);
2474 }
2475 return err;
Kees Cook47830722008-10-06 03:11:58 +04002476}
2477
Eric W. Biederman801199c2006-10-02 02:18:48 -07002478/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002479 * Thread groups
2480 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002481static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002482static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002483
Eric Dumazetc5141e62007-05-08 00:26:15 -07002484static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002485 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2486 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002487#ifdef CONFIG_CHECKPOINT_RESTORE
2488 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2489#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002490 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002491 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002492#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002493 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002494#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002495 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002496 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002497 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002498 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002499 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002500#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002501 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002502#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002503#ifdef CONFIG_SCHED_AUTOGROUP
2504 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2505#endif
john stultz4614a696b2009-12-14 18:00:05 -08002506 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002507#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002508 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002509#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002510 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002511 ONE("stat", S_IRUGO, proc_tgid_stat),
2512 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002513 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002515 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002516#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002517 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2518 LNK("cwd", proc_cwd_link),
2519 LNK("root", proc_root_link),
2520 LNK("exe", proc_exe_link),
2521 REG("mounts", S_IRUGO, proc_mounts_operations),
2522 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2523 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002524#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002525 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002526 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002527 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002528#endif
2529#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002530 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531#endif
2532#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002533 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002534#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002535#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002536 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002537#endif
2538#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002539 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002540#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002541#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002542 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002543#endif
Paul Menage8793d852007-10-18 23:39:39 -07002544#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002545 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002546#endif
Paul Menagea4243162007-10-18 23:39:35 -07002547#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002548 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002549#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002550 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002551 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002552 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002553#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002554 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2555 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002556#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002557#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002558 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002559#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002560#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002561 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002562#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002563#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002564 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002565#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002566#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002567 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002568#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002569#ifdef CONFIG_USER_NS
2570 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2571 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002572 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002573#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002574#ifdef CONFIG_CHECKPOINT_RESTORE
2575 REG("timers", S_IRUGO, proc_timers_operations),
2576#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002577};
2578
Al Virof0c3b502013-05-16 12:07:31 -04002579static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002580{
Al Virof0c3b502013-05-16 12:07:31 -04002581 return proc_pident_readdir(file, ctx,
2582 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583}
2584
Arjan van de Ven00977a52007-02-12 00:55:34 -08002585static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002586 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002587 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002588 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002589};
2590
Al Viro00cd8dd2012-06-10 17:13:09 -04002591static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2592{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002593 return proc_pident_lookup(dir, dentry,
2594 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002595}
2596
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002597static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002598 .lookup = proc_tgid_base_lookup,
2599 .getattr = pid_getattr,
2600 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002601 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602};
2603
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002604static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002606 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002607 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002608 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Eric W. Biederman48e64842006-06-26 00:25:48 -07002610 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002611 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002612 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002613 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002614 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002615 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002616 d_drop(dentry);
2617 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Eric W. Biederman48e64842006-06-26 00:25:48 -07002620 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002621 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2622 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002623 if (!leader)
2624 goto out;
2625
2626 name.name = "task";
2627 name.len = strlen(name.name);
2628 dir = d_hash_and_lookup(leader, &name);
2629 if (!dir)
2630 goto out_put_leader;
2631
2632 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002633 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002634 dentry = d_hash_and_lookup(dir, &name);
2635 if (dentry) {
2636 shrink_dcache_parent(dentry);
2637 d_drop(dentry);
2638 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002640
2641 dput(dir);
2642out_put_leader:
2643 dput(leader);
2644out:
2645 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
Randy Dunlap0895e912007-10-21 21:00:10 -07002648/**
2649 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2650 * @task: task that should be flushed.
2651 *
2652 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002653 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002654 * in. This call is supposed to do all of this job.
2655 *
2656 * Looks in the dcache for
2657 * /proc/@pid
2658 * /proc/@tgid/task/@pid
2659 * if either directory is present flushes it and all of it'ts children
2660 * from the dcache.
2661 *
2662 * It is safe and reasonable to cache /proc entries for a task until
2663 * that task exits. After that they just clog up the dcache with
2664 * useless entries, possibly causing useful dcache entries to be
2665 * flushed instead. This routine is proved to flush those useless
2666 * dcache entries at process exit time.
2667 *
2668 * NOTE: This routine is just an optimization so it does not guarantee
2669 * that no dcache entries will exist at process exit time it
2670 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002671 */
2672
2673void proc_flush_task(struct task_struct *task)
2674{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002675 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002676 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002677 struct upid *upid;
2678
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002679 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002680 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002681
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002682 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002683 upid = &pid->numbers[i];
2684 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002685 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002686 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002687}
2688
Al Viroc52a47a2013-06-15 11:15:20 +04002689static int proc_pid_instantiate(struct inode *dir,
2690 struct dentry * dentry,
2691 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002692{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002693 struct inode *inode;
2694
Eric W. Biederman61a28782006-10-02 02:18:49 -07002695 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002696 if (!inode)
2697 goto out;
2698
2699 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2700 inode->i_op = &proc_tgid_base_inode_operations;
2701 inode->i_fop = &proc_tgid_base_operations;
2702 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002703
Miklos Szeredibfe86842011-10-28 14:13:29 +02002704 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2705 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002706
Nick Pigginfb045ad2011-01-07 17:49:55 +11002707 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002708
2709 d_add(dentry, inode);
2710 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002711 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002712 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002713out:
Al Viroc52a47a2013-06-15 11:15:20 +04002714 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002715}
2716
Al Viro00cd8dd2012-06-10 17:13:09 -04002717struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002719 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002722 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002724 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (tgid == ~0U)
2726 goto out;
2727
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002728 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002729 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002730 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (task)
2732 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002733 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (!task)
2735 goto out;
2736
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002737 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002738 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739out:
Al Viroc52a47a2013-06-15 11:15:20 +04002740 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002744 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002745 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002747struct tgid_iter {
2748 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002749 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002750};
2751static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2752{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002753 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002755 if (iter.task)
2756 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002757 rcu_read_lock();
2758retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002759 iter.task = NULL;
2760 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002761 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002762 iter.tgid = pid_nr_ns(pid, ns);
2763 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002764 /* What we to know is if the pid we have find is the
2765 * pid of a thread_group_leader. Testing for task
2766 * being a thread_group_leader is the obvious thing
2767 * todo but there is a window when it fails, due to
2768 * the pid transfer logic in de_thread.
2769 *
2770 * So we perform the straight forward test of seeing
2771 * if the pid we have found is the pid of a thread
2772 * group leader, and don't worry if the task we have
2773 * found doesn't happen to be a thread group leader.
2774 * As we don't care in the case of readdir.
2775 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002776 if (!iter.task || !has_group_leader_pid(iter.task)) {
2777 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002778 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002779 }
2780 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002782 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002783 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
Eric W. Biederman00978752014-07-31 03:10:50 -07002786#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002789int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002791 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002792 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002793 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Al Viro021ada72013-03-29 19:27:05 -04002795 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Eric W. Biederman00978752014-07-31 03:10:50 -07002798 if (pos == TGID_OFFSET - 2) {
Al Virodb963162013-06-15 10:45:10 +04002799 struct inode *inode = ns->proc_self->d_inode;
2800 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002801 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07002802 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04002803 }
Eric W. Biederman00978752014-07-31 03:10:50 -07002804 if (pos == TGID_OFFSET - 1) {
2805 struct inode *inode = ns->proc_thread_self->d_inode;
2806 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
2807 return 0;
2808 ctx->pos = pos = pos + 1;
2809 }
2810 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002811 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002812 for (iter = next_tgid(ns, iter);
2813 iter.task;
2814 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002815 char name[PROC_NUMBUF];
2816 int len;
2817 if (!has_pid_permissions(ns, iter.task, 2))
2818 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002819
Al Virof0c3b502013-05-16 12:07:31 -04002820 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2821 ctx->pos = iter.tgid + TGID_OFFSET;
2822 if (!proc_fill_cache(file, ctx, name, len,
2823 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002824 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
Al Virof0c3b502013-05-16 12:07:31 -04002828 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return 0;
2830}
2831
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002832/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002833 * Tasks
2834 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002835static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002836 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002837 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002838 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07002839#ifdef CONFIG_NET
2840 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2841#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002842 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002843 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002844 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002845 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002846 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002847#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002848 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002849#endif
john stultz4614a696b2009-12-14 18:00:05 -08002850 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002851#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002852 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002853#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002854 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002855 ONE("stat", S_IRUGO, proc_tid_stat),
2856 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002857 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002858#ifdef CONFIG_CHECKPOINT_RESTORE
2859 REG("children", S_IRUGO, proc_tid_children_operations),
2860#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002861#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002862 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002863#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002864 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2865 LNK("cwd", proc_cwd_link),
2866 LNK("root", proc_root_link),
2867 LNK("exe", proc_exe_link),
2868 REG("mounts", S_IRUGO, proc_mounts_operations),
2869 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002870#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002871 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002872 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002873 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002874#endif
2875#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002876 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002877#endif
2878#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002879 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002880#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002881#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002882 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002883#endif
2884#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002885 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002886#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002887#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002888 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002889#endif
Paul Menage8793d852007-10-18 23:39:39 -07002890#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002891 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002892#endif
Paul Menagea4243162007-10-18 23:39:35 -07002893#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002894 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002895#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002896 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002897 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002898 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002899#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002900 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002901 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002902#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002903#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002904 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002905#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002906#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002907 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002908#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002909#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002910 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002911#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002912#ifdef CONFIG_USER_NS
2913 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2914 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002915 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002916#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917};
2918
Al Virof0c3b502013-05-16 12:07:31 -04002919static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002920{
Al Virof0c3b502013-05-16 12:07:31 -04002921 return proc_pident_readdir(file, ctx,
2922 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002923}
2924
Al Viro00cd8dd2012-06-10 17:13:09 -04002925static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2926{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002927 return proc_pident_lookup(dir, dentry,
2928 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002929}
2930
Arjan van de Ven00977a52007-02-12 00:55:34 -08002931static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002932 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002933 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002934 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002935};
2936
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002937static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002938 .lookup = proc_tid_base_lookup,
2939 .getattr = pid_getattr,
2940 .setattr = proc_setattr,
2941};
2942
Al Viroc52a47a2013-06-15 11:15:20 +04002943static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002944 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002945{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002946 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002947 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002948
2949 if (!inode)
2950 goto out;
2951 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2952 inode->i_op = &proc_tid_base_inode_operations;
2953 inode->i_fop = &proc_tid_base_operations;
2954 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002955
Miklos Szeredibfe86842011-10-28 14:13:29 +02002956 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
2957 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002958
Nick Pigginfb045ad2011-01-07 17:49:55 +11002959 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002960
2961 d_add(dentry, inode);
2962 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002963 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002964 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002965out:
Al Viroc52a47a2013-06-15 11:15:20 +04002966 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002967}
2968
Al Viro00cd8dd2012-06-10 17:13:09 -04002969static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002970{
Al Viroc52a47a2013-06-15 11:15:20 +04002971 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002972 struct task_struct *task;
2973 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002974 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002975 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002976
2977 if (!leader)
2978 goto out_no_task;
2979
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002980 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002981 if (tid == ~0U)
2982 goto out;
2983
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002984 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002985 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002986 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002987 if (task)
2988 get_task_struct(task);
2989 rcu_read_unlock();
2990 if (!task)
2991 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002992 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002993 goto out_drop_task;
2994
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002995 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002996out_drop_task:
2997 put_task_struct(task);
2998out:
2999 put_task_struct(leader);
3000out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003001 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003002}
3003
3004/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003005 * Find the first tid of a thread group to return to user space.
3006 *
3007 * Usually this is just the thread group leader, but if the users
3008 * buffer was too small or there was a seek into the middle of the
3009 * directory we have more work todo.
3010 *
3011 * In the case of a short read we start with find_task_by_pid.
3012 *
3013 * In the case of a seek we start with the leader and walk nr
3014 * threads past it.
3015 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003016static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3017 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003018{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003019 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003020 unsigned long nr = f_pos;
3021
3022 if (nr != f_pos) /* 32bit overflow? */
3023 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003024
Eric W. Biedermancc288732006-06-26 00:26:01 -07003025 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003026 task = pid_task(pid, PIDTYPE_PID);
3027 if (!task)
3028 goto fail;
3029
3030 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003031 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003032 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003033 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003034 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003035 }
3036
3037 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003038 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003039 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003040
3041 /* If we haven't found our starting place yet start
3042 * with the leader and walk nr threads forward.
3043 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003044 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003045 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003046 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003047 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003048 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003049fail:
3050 pos = NULL;
3051 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003052found:
3053 get_task_struct(pos);
3054out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003055 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003056 return pos;
3057}
3058
3059/*
3060 * Find the next thread in the thread list.
3061 * Return NULL if there is an error or no next thread.
3062 *
3063 * The reference to the input task_struct is released.
3064 */
3065static struct task_struct *next_tid(struct task_struct *start)
3066{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003067 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003068 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003069 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003070 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003071 if (thread_group_leader(pos))
3072 pos = NULL;
3073 else
3074 get_task_struct(pos);
3075 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003076 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003077 put_task_struct(start);
3078 return pos;
3079}
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003082static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003084 struct inode *inode = file_inode(file);
3085 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003086 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003087 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003089 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003090 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Al Virof0c3b502013-05-16 12:07:31 -04003092 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003095 /* f_version caches the tgid value that the last readdir call couldn't
3096 * return. lseek aka telldir automagically resets f_version to 0.
3097 */
Al Virof0c3b502013-05-16 12:07:31 -04003098 ns = file->f_dentry->d_sb->s_fs_info;
3099 tid = (int)file->f_version;
3100 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003101 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003102 task;
Al Virof0c3b502013-05-16 12:07:31 -04003103 task = next_tid(task), ctx->pos++) {
3104 char name[PROC_NUMBUF];
3105 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003106 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003107 len = snprintf(name, sizeof(name), "%d", tid);
3108 if (!proc_fill_cache(file, ctx, name, len,
3109 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003110 /* returning this tgid failed, save it as the first
3111 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003112 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003113 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003117
Al Virof0c3b502013-05-16 12:07:31 -04003118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003120
3121static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3122{
3123 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003124 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003125 generic_fillattr(inode, stat);
3126
Eric W. Biederman99f89552006-06-26 00:25:55 -07003127 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003128 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003129 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003130 }
3131
3132 return 0;
3133}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003134
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003135static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003136 .lookup = proc_task_lookup,
3137 .getattr = proc_task_getattr,
3138 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003139 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003140};
3141
Arjan van de Ven00977a52007-02-12 00:55:34 -08003142static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003143 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003144 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003145 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003146};