blob: 6b96892015ecbe6bef9a53368e71657a4c984cf4 [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
Al Viro8d8b97b2013-04-19 23:11:24 -0400379#ifdef CONFIG_PROC_PID_CPUSET
380
381static int cpuset_open(struct inode *inode, struct file *file)
382{
383 struct pid *pid = PROC_I(inode)->pid;
384 return single_open(file, proc_cpuset_show, pid);
385}
386
387static const struct file_operations proc_cpuset_operations = {
388 .open = cpuset_open,
389 .read = seq_read,
390 .llseek = seq_lseek,
391 .release = single_release,
392};
393#endif
394
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700395static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
396 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
David Rientjesa7f638f2012-05-29 15:06:47 -0700398 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200399 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700401 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200402 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700403 points = oom_badness(task, NULL, NULL, totalpages) *
404 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700405 read_unlock(&tasklist_lock);
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700406 return seq_printf(m, "%lu\n", points);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Neil Hormand85f50d2007-10-18 23:40:37 -0700409struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700410 const char *name;
411 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700412};
413
414static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700415 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700416 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
417 [RLIMIT_DATA] = {"Max data size", "bytes"},
418 [RLIMIT_STACK] = {"Max stack size", "bytes"},
419 [RLIMIT_CORE] = {"Max core file size", "bytes"},
420 [RLIMIT_RSS] = {"Max resident set", "bytes"},
421 [RLIMIT_NPROC] = {"Max processes", "processes"},
422 [RLIMIT_NOFILE] = {"Max open files", "files"},
423 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
424 [RLIMIT_AS] = {"Max address space", "bytes"},
425 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
426 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
427 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
428 [RLIMIT_NICE] = {"Max nice priority", NULL},
429 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800430 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700431};
432
433/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700434static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
435 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700436{
437 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700438 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700439
440 struct rlimit rlim[RLIM_NLIMITS];
441
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400442 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700443 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700444 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
445 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700446
447 /*
448 * print the file header
449 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700450 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Neil Hormand85f50d2007-10-18 23:40:37 -0700451 "Limit", "Soft Limit", "Hard Limit", "Units");
452
453 for (i = 0; i < RLIM_NLIMITS; i++) {
454 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700455 seq_printf(m, "%-25s %-20s ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700456 lnames[i].name, "unlimited");
457 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700458 seq_printf(m, "%-25s %-20lu ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700459 lnames[i].name, rlim[i].rlim_cur);
460
461 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700462 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700463 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700464 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700465
466 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700467 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700468 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700469 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700470 }
471
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700472 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700473}
474
Roland McGrathebcb6732008-07-25 19:46:00 -0700475#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700476static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
477 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700478{
479 long nr;
480 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400481 int res = lock_trace(task);
482 if (res)
483 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700484
485 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700486 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400487 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700488 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400489 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700490 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700491 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
492 nr,
493 args[0], args[1], args[2], args[3], args[4], args[5],
494 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400495 unlock_trace(task);
496 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700497}
498#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/************************************************************************/
501/* Here the fs part begins */
502/************************************************************************/
503
504/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700505static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700507 struct task_struct *task;
508 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700509 /* Allow access to a task's file descriptors if it is us or we
510 * may use ptrace attach to the process and find out that
511 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700512 */
513 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700514 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400515 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700516 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700517 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700518 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800521int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700522{
523 int error;
524 struct inode *inode = dentry->d_inode;
525
526 if (attr->ia_valid & ATTR_MODE)
527 return -EPERM;
528
529 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200530 if (error)
531 return error;
532
Christoph Hellwig10257742010-06-04 11:30:02 +0200533 setattr_copy(inode, attr);
534 mark_inode_dirty(inode);
535 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700536}
537
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800538/*
539 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
540 * or euid/egid (for hide_pid_min=2)?
541 */
542static bool has_pid_permissions(struct pid_namespace *pid,
543 struct task_struct *task,
544 int hide_pid_min)
545{
546 if (pid->hide_pid < hide_pid_min)
547 return true;
548 if (in_group_p(pid->pid_gid))
549 return true;
550 return ptrace_may_access(task, PTRACE_MODE_READ);
551}
552
553
554static int proc_pid_permission(struct inode *inode, int mask)
555{
556 struct pid_namespace *pid = inode->i_sb->s_fs_info;
557 struct task_struct *task;
558 bool has_perms;
559
560 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800561 if (!task)
562 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800563 has_perms = has_pid_permissions(pid, task, 1);
564 put_task_struct(task);
565
566 if (!has_perms) {
567 if (pid->hide_pid == 2) {
568 /*
569 * Let's make getdents(), stat(), and open()
570 * consistent with each other. If a process
571 * may not stat() a file, it shouldn't be seen
572 * in procfs at all.
573 */
574 return -ENOENT;
575 }
576
577 return -EPERM;
578 }
579 return generic_permission(inode, mask);
580}
581
582
583
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800584static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700585 .setattr = proc_setattr,
586};
587
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800588static int proc_single_show(struct seq_file *m, void *v)
589{
590 struct inode *inode = m->private;
591 struct pid_namespace *ns;
592 struct pid *pid;
593 struct task_struct *task;
594 int ret;
595
596 ns = inode->i_sb->s_fs_info;
597 pid = proc_pid(inode);
598 task = get_pid_task(pid, PIDTYPE_PID);
599 if (!task)
600 return -ESRCH;
601
602 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
603
604 put_task_struct(task);
605 return ret;
606}
607
608static int proc_single_open(struct inode *inode, struct file *filp)
609{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800610 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800611}
612
613static const struct file_operations proc_single_file_operations = {
614 .open = proc_single_open,
615 .read = seq_read,
616 .llseek = seq_lseek,
617 .release = single_release,
618};
619
Cong Wangb409e572012-05-31 16:26:17 -0700620static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Al Viro496ad9a2013-01-23 17:07:38 -0500622 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800623 struct mm_struct *mm;
624
625 if (!task)
626 return -ESRCH;
627
Cong Wangb409e572012-05-31 16:26:17 -0700628 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800629 put_task_struct(task);
630
631 if (IS_ERR(mm))
632 return PTR_ERR(mm);
633
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100634 if (mm) {
635 /* ensure this mm_struct can't be freed */
636 atomic_inc(&mm->mm_count);
637 /* but do not pin its memory */
638 mmput(mm);
639 }
640
Linus Torvaldse2683372012-01-17 15:21:19 -0800641 file->private_data = mm;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
644}
645
Cong Wangb409e572012-05-31 16:26:17 -0700646static int mem_open(struct inode *inode, struct file *file)
647{
Djalal Harounibc452b42012-07-30 14:42:28 -0700648 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
649
650 /* OK to pass negative loff_t, we can catch out-of-range */
651 file->f_mode |= FMODE_UNSIGNED_OFFSET;
652
653 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700654}
655
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100656static ssize_t mem_rw(struct file *file, char __user *buf,
657 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Linus Torvaldse2683372012-01-17 15:21:19 -0800659 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100660 unsigned long addr = *ppos;
661 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvaldse2683372012-01-17 15:21:19 -0800664 if (!mm)
665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Mel Gormane12ba742007-10-16 01:25:52 -0700667 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800669 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700671 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100672 if (!atomic_inc_not_zero(&mm->mm_users))
673 goto free;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100676 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100678 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 copied = -EFAULT;
680 break;
681 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100682
683 this_len = access_remote_vm(mm, addr, page, this_len, write);
684 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!copied)
686 copied = -EIO;
687 break;
688 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100689
690 if (!write && copy_to_user(buf, page, this_len)) {
691 copied = -EFAULT;
692 break;
693 }
694
695 buf += this_len;
696 addr += this_len;
697 copied += this_len;
698 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100700 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700701
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100702 mmput(mm);
703free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700704 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return copied;
706}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100708static ssize_t mem_read(struct file *file, char __user *buf,
709 size_t count, loff_t *ppos)
710{
711 return mem_rw(file, buf, count, ppos, 0);
712}
713
714static ssize_t mem_write(struct file *file, const char __user *buf,
715 size_t count, loff_t *ppos)
716{
717 return mem_rw(file, (char __user*)buf, count, ppos, 1);
718}
719
Matt Mackall85863e42008-02-04 22:29:04 -0800720loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 switch (orig) {
723 case 0:
724 file->f_pos = offset;
725 break;
726 case 1:
727 file->f_pos += offset;
728 break;
729 default:
730 return -EINVAL;
731 }
732 force_successful_syscall_return();
733 return file->f_pos;
734}
735
Linus Torvaldse2683372012-01-17 15:21:19 -0800736static int mem_release(struct inode *inode, struct file *file)
737{
738 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100739 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100740 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800741 return 0;
742}
743
Arjan van de Ven00977a52007-02-12 00:55:34 -0800744static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .llseek = mem_lseek,
746 .read = mem_read,
747 .write = mem_write,
748 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800749 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750};
751
Cong Wangb409e572012-05-31 16:26:17 -0700752static int environ_open(struct inode *inode, struct file *file)
753{
754 return __mem_open(inode, file, PTRACE_MODE_READ);
755}
756
James Pearson315e28c2007-10-16 23:30:17 -0700757static ssize_t environ_read(struct file *file, char __user *buf,
758 size_t count, loff_t *ppos)
759{
James Pearson315e28c2007-10-16 23:30:17 -0700760 char *page;
761 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700762 int ret = 0;
763 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700764
Cong Wangb409e572012-05-31 16:26:17 -0700765 if (!mm)
766 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700767
James Pearson315e28c2007-10-16 23:30:17 -0700768 page = (char *)__get_free_page(GFP_TEMPORARY);
769 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700770 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700771
Al Virod6f64b82011-02-15 22:26:01 -0500772 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700773 if (!atomic_inc_not_zero(&mm->mm_users))
774 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700775 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700776 size_t this_len, max_len;
777 int retval;
778
779 if (src >= (mm->env_end - mm->env_start))
780 break;
James Pearson315e28c2007-10-16 23:30:17 -0700781
782 this_len = mm->env_end - (mm->env_start + src);
783
Djalal Harounie8905ec2012-07-30 14:42:26 -0700784 max_len = min_t(size_t, PAGE_SIZE, count);
785 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700786
Cong Wangb409e572012-05-31 16:26:17 -0700787 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700788 page, this_len, 0);
789
790 if (retval <= 0) {
791 ret = retval;
792 break;
793 }
794
795 if (copy_to_user(buf, page, retval)) {
796 ret = -EFAULT;
797 break;
798 }
799
800 ret += retval;
801 src += retval;
802 buf += retval;
803 count -= retval;
804 }
805 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700806 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700807
808free:
James Pearson315e28c2007-10-16 23:30:17 -0700809 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700810 return ret;
811}
812
813static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700814 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700815 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100816 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700817 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700818};
819
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800820static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
821 loff_t *ppos)
822{
Al Viro496ad9a2013-01-23 17:07:38 -0500823 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800824 char buffer[PROC_NUMBUF];
825 int oom_adj = OOM_ADJUST_MIN;
826 size_t len;
827 unsigned long flags;
828
829 if (!task)
830 return -ESRCH;
831 if (lock_task_sighand(task, &flags)) {
832 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
833 oom_adj = OOM_ADJUST_MAX;
834 else
835 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
836 OOM_SCORE_ADJ_MAX;
837 unlock_task_sighand(task, &flags);
838 }
839 put_task_struct(task);
840 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
841 return simple_read_from_buffer(buf, count, ppos, buffer, len);
842}
843
844static ssize_t oom_adj_write(struct file *file, const char __user *buf,
845 size_t count, loff_t *ppos)
846{
847 struct task_struct *task;
848 char buffer[PROC_NUMBUF];
849 int oom_adj;
850 unsigned long flags;
851 int err;
852
853 memset(buffer, 0, sizeof(buffer));
854 if (count > sizeof(buffer) - 1)
855 count = sizeof(buffer) - 1;
856 if (copy_from_user(buffer, buf, count)) {
857 err = -EFAULT;
858 goto out;
859 }
860
861 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
862 if (err)
863 goto out;
864 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
865 oom_adj != OOM_DISABLE) {
866 err = -EINVAL;
867 goto out;
868 }
869
Al Viro496ad9a2013-01-23 17:07:38 -0500870 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800871 if (!task) {
872 err = -ESRCH;
873 goto out;
874 }
875
876 task_lock(task);
877 if (!task->mm) {
878 err = -EINVAL;
879 goto err_task_lock;
880 }
881
882 if (!lock_task_sighand(task, &flags)) {
883 err = -ESRCH;
884 goto err_task_lock;
885 }
886
887 /*
888 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
889 * value is always attainable.
890 */
891 if (oom_adj == OOM_ADJUST_MAX)
892 oom_adj = OOM_SCORE_ADJ_MAX;
893 else
894 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
895
896 if (oom_adj < task->signal->oom_score_adj &&
897 !capable(CAP_SYS_RESOURCE)) {
898 err = -EACCES;
899 goto err_sighand;
900 }
901
902 /*
903 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
904 * /proc/pid/oom_score_adj instead.
905 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800906 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 -0800907 current->comm, task_pid_nr(current), task_pid_nr(task),
908 task_pid_nr(task));
909
910 task->signal->oom_score_adj = oom_adj;
911 trace_oom_score_adj_update(task);
912err_sighand:
913 unlock_task_sighand(task, &flags);
914err_task_lock:
915 task_unlock(task);
916 put_task_struct(task);
917out:
918 return err < 0 ? err : count;
919}
920
921static const struct file_operations proc_oom_adj_operations = {
922 .read = oom_adj_read,
923 .write = oom_adj_write,
924 .llseek = generic_file_llseek,
925};
926
David Rientjesa63d83f2010-08-09 17:19:46 -0700927static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
928 size_t count, loff_t *ppos)
929{
Al Viro496ad9a2013-01-23 17:07:38 -0500930 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700931 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800932 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700933 unsigned long flags;
934 size_t len;
935
936 if (!task)
937 return -ESRCH;
938 if (lock_task_sighand(task, &flags)) {
939 oom_score_adj = task->signal->oom_score_adj;
940 unlock_task_sighand(task, &flags);
941 }
942 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800943 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700944 return simple_read_from_buffer(buf, count, ppos, buffer, len);
945}
946
947static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
948 size_t count, loff_t *ppos)
949{
950 struct task_struct *task;
951 char buffer[PROC_NUMBUF];
952 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700953 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -0700954 int err;
955
956 memset(buffer, 0, sizeof(buffer));
957 if (count > sizeof(buffer) - 1)
958 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700959 if (copy_from_user(buffer, buf, count)) {
960 err = -EFAULT;
961 goto out;
962 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700963
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700964 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700965 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700966 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -0700967 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -0700968 oom_score_adj > OOM_SCORE_ADJ_MAX) {
969 err = -EINVAL;
970 goto out;
971 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700972
Al Viro496ad9a2013-01-23 17:07:38 -0500973 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -0700974 if (!task) {
975 err = -ESRCH;
976 goto out;
977 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700978
Ying Han3d5992d2010-10-26 14:21:23 -0700979 task_lock(task);
980 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700981 err = -EINVAL;
982 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700983 }
David Rientjesd19d5472010-10-26 14:21:26 -0700984
985 if (!lock_task_sighand(task, &flags)) {
986 err = -ESRCH;
987 goto err_task_lock;
988 }
989
David Rientjesa9c58b902012-12-11 16:02:54 -0800990 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -0700991 !capable(CAP_SYS_RESOURCE)) {
992 err = -EACCES;
993 goto err_sighand;
994 }
995
David Rientjesa9c58b902012-12-11 16:02:54 -0800996 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f632011-01-13 15:46:05 -0800997 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -0800998 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800999 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001000
David Rientjes723548b2010-10-26 14:21:25 -07001001err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001002 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001003err_task_lock:
1004 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001005 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001006out:
1007 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001008}
1009
1010static const struct file_operations proc_oom_score_adj_operations = {
1011 .read = oom_score_adj_read,
1012 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001013 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001014};
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016#ifdef CONFIG_AUDITSYSCALL
1017#define TMPBUFLEN 21
1018static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1019 size_t count, loff_t *ppos)
1020{
Al Viro496ad9a2013-01-23 17:07:38 -05001021 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001022 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 ssize_t length;
1024 char tmpbuf[TMPBUFLEN];
1025
Eric W. Biederman99f89552006-06-26 00:25:55 -07001026 if (!task)
1027 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001029 from_kuid(file->f_cred->user_ns,
1030 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001031 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1033}
1034
1035static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1036 size_t count, loff_t *ppos)
1037{
Al Viro496ad9a2013-01-23 17:07:38 -05001038 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 char *page, *tmp;
1040 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001042 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001044 rcu_read_lock();
1045 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1046 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001048 }
1049 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Al Viroe0182902006-05-18 08:28:02 -04001051 if (count >= PAGE_SIZE)
1052 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (*ppos != 0) {
1055 /* No partial writes. */
1056 return -EINVAL;
1057 }
Mel Gormane12ba742007-10-16 01:25:52 -07001058 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!page)
1060 return -ENOMEM;
1061 length = -EFAULT;
1062 if (copy_from_user(page, buf, count))
1063 goto out_free_page;
1064
Al Viroe0182902006-05-18 08:28:02 -04001065 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 loginuid = simple_strtoul(page, &tmp, 10);
1067 if (tmp == page) {
1068 length = -EINVAL;
1069 goto out_free_page;
1070
1071 }
Eric Paris81407c82013-05-24 09:49:14 -04001072
1073 /* is userspace tring to explicitly UNSET the loginuid? */
1074 if (loginuid == AUDIT_UID_UNSET) {
1075 kloginuid = INVALID_UID;
1076 } else {
1077 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1078 if (!uid_valid(kloginuid)) {
1079 length = -EINVAL;
1080 goto out_free_page;
1081 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001082 }
1083
1084 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (likely(length == 0))
1086 length = count;
1087
1088out_free_page:
1089 free_page((unsigned long) page);
1090 return length;
1091}
1092
Arjan van de Ven00977a52007-02-12 00:55:34 -08001093static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 .read = proc_loginuid_read,
1095 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001096 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097};
Eric Paris1e0bd752008-03-13 08:15:31 -04001098
1099static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1100 size_t count, loff_t *ppos)
1101{
Al Viro496ad9a2013-01-23 17:07:38 -05001102 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001103 struct task_struct *task = get_proc_task(inode);
1104 ssize_t length;
1105 char tmpbuf[TMPBUFLEN];
1106
1107 if (!task)
1108 return -ESRCH;
1109 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1110 audit_get_sessionid(task));
1111 put_task_struct(task);
1112 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1113}
1114
1115static const struct file_operations proc_sessionid_operations = {
1116 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001117 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001118};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119#endif
1120
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001121#ifdef CONFIG_FAULT_INJECTION
1122static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1123 size_t count, loff_t *ppos)
1124{
Al Viro496ad9a2013-01-23 17:07:38 -05001125 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001126 char buffer[PROC_NUMBUF];
1127 size_t len;
1128 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001129
1130 if (!task)
1131 return -ESRCH;
1132 make_it_fail = task->make_it_fail;
1133 put_task_struct(task);
1134
1135 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001136
1137 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001138}
1139
1140static ssize_t proc_fault_inject_write(struct file * file,
1141 const char __user * buf, size_t count, loff_t *ppos)
1142{
1143 struct task_struct *task;
1144 char buffer[PROC_NUMBUF], *end;
1145 int make_it_fail;
1146
1147 if (!capable(CAP_SYS_RESOURCE))
1148 return -EPERM;
1149 memset(buffer, 0, sizeof(buffer));
1150 if (count > sizeof(buffer) - 1)
1151 count = sizeof(buffer) - 1;
1152 if (copy_from_user(buffer, buf, count))
1153 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001154 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1155 if (*end)
1156 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001157 if (make_it_fail < 0 || make_it_fail > 1)
1158 return -EINVAL;
1159
Al Viro496ad9a2013-01-23 17:07:38 -05001160 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001161 if (!task)
1162 return -ESRCH;
1163 task->make_it_fail = make_it_fail;
1164 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001165
1166 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001167}
1168
Arjan van de Ven00977a52007-02-12 00:55:34 -08001169static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001170 .read = proc_fault_inject_read,
1171 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001172 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001173};
1174#endif
1175
Arjan van de Ven97455122008-01-25 21:08:34 +01001176
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001177#ifdef CONFIG_SCHED_DEBUG
1178/*
1179 * Print out various scheduling related per-task fields:
1180 */
1181static int sched_show(struct seq_file *m, void *v)
1182{
1183 struct inode *inode = m->private;
1184 struct task_struct *p;
1185
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001186 p = get_proc_task(inode);
1187 if (!p)
1188 return -ESRCH;
1189 proc_sched_show_task(p, m);
1190
1191 put_task_struct(p);
1192
1193 return 0;
1194}
1195
1196static ssize_t
1197sched_write(struct file *file, const char __user *buf,
1198 size_t count, loff_t *offset)
1199{
Al Viro496ad9a2013-01-23 17:07:38 -05001200 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001201 struct task_struct *p;
1202
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001203 p = get_proc_task(inode);
1204 if (!p)
1205 return -ESRCH;
1206 proc_sched_set_task(p);
1207
1208 put_task_struct(p);
1209
1210 return count;
1211}
1212
1213static int sched_open(struct inode *inode, struct file *filp)
1214{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001215 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001216}
1217
1218static const struct file_operations proc_pid_sched_operations = {
1219 .open = sched_open,
1220 .read = seq_read,
1221 .write = sched_write,
1222 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001223 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001224};
1225
1226#endif
1227
Mike Galbraith5091faa2010-11-30 14:18:03 +01001228#ifdef CONFIG_SCHED_AUTOGROUP
1229/*
1230 * Print out autogroup related information:
1231 */
1232static int sched_autogroup_show(struct seq_file *m, void *v)
1233{
1234 struct inode *inode = m->private;
1235 struct task_struct *p;
1236
1237 p = get_proc_task(inode);
1238 if (!p)
1239 return -ESRCH;
1240 proc_sched_autogroup_show_task(p, m);
1241
1242 put_task_struct(p);
1243
1244 return 0;
1245}
1246
1247static ssize_t
1248sched_autogroup_write(struct file *file, const char __user *buf,
1249 size_t count, loff_t *offset)
1250{
Al Viro496ad9a2013-01-23 17:07:38 -05001251 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001252 struct task_struct *p;
1253 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001254 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001255 int err;
1256
1257 memset(buffer, 0, sizeof(buffer));
1258 if (count > sizeof(buffer) - 1)
1259 count = sizeof(buffer) - 1;
1260 if (copy_from_user(buffer, buf, count))
1261 return -EFAULT;
1262
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001263 err = kstrtoint(strstrip(buffer), 0, &nice);
1264 if (err < 0)
1265 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001266
1267 p = get_proc_task(inode);
1268 if (!p)
1269 return -ESRCH;
1270
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001271 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001272 if (err)
1273 count = err;
1274
1275 put_task_struct(p);
1276
1277 return count;
1278}
1279
1280static int sched_autogroup_open(struct inode *inode, struct file *filp)
1281{
1282 int ret;
1283
1284 ret = single_open(filp, sched_autogroup_show, NULL);
1285 if (!ret) {
1286 struct seq_file *m = filp->private_data;
1287
1288 m->private = inode;
1289 }
1290 return ret;
1291}
1292
1293static const struct file_operations proc_pid_sched_autogroup_operations = {
1294 .open = sched_autogroup_open,
1295 .read = seq_read,
1296 .write = sched_autogroup_write,
1297 .llseek = seq_lseek,
1298 .release = single_release,
1299};
1300
1301#endif /* CONFIG_SCHED_AUTOGROUP */
1302
john stultz4614a696b2009-12-14 18:00:05 -08001303static ssize_t comm_write(struct file *file, const char __user *buf,
1304 size_t count, loff_t *offset)
1305{
Al Viro496ad9a2013-01-23 17:07:38 -05001306 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001307 struct task_struct *p;
1308 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001309 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001310
1311 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001312 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001313 return -EFAULT;
1314
1315 p = get_proc_task(inode);
1316 if (!p)
1317 return -ESRCH;
1318
1319 if (same_thread_group(current, p))
1320 set_task_comm(p, buffer);
1321 else
1322 count = -EINVAL;
1323
1324 put_task_struct(p);
1325
1326 return count;
1327}
1328
1329static int comm_show(struct seq_file *m, void *v)
1330{
1331 struct inode *inode = m->private;
1332 struct task_struct *p;
1333
1334 p = get_proc_task(inode);
1335 if (!p)
1336 return -ESRCH;
1337
1338 task_lock(p);
1339 seq_printf(m, "%s\n", p->comm);
1340 task_unlock(p);
1341
1342 put_task_struct(p);
1343
1344 return 0;
1345}
1346
1347static int comm_open(struct inode *inode, struct file *filp)
1348{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001349 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001350}
1351
1352static const struct file_operations proc_pid_set_comm_operations = {
1353 .open = comm_open,
1354 .read = seq_read,
1355 .write = comm_write,
1356 .llseek = seq_lseek,
1357 .release = single_release,
1358};
1359
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001360static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001361{
1362 struct task_struct *task;
1363 struct mm_struct *mm;
1364 struct file *exe_file;
1365
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001366 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001367 if (!task)
1368 return -ENOENT;
1369 mm = get_task_mm(task);
1370 put_task_struct(task);
1371 if (!mm)
1372 return -ENOENT;
1373 exe_file = get_mm_exe_file(mm);
1374 mmput(mm);
1375 if (exe_file) {
1376 *exe_path = exe_file->f_path;
1377 path_get(&exe_file->f_path);
1378 fput(exe_file);
1379 return 0;
1380 } else
1381 return -ENOENT;
1382}
1383
Al Viro008b1502005-08-20 00:17:39 +01001384static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001387 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int error = -EACCES;
1389
Eric W. Biederman778c1142006-06-26 00:25:58 -07001390 /* Are we allowed to snoop on the tasks file descriptors? */
1391 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Christoph Hellwig408ef012012-06-18 10:47:03 -04001394 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1395 if (error)
1396 goto out;
1397
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001398 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001399 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400out:
Al Viro008b1502005-08-20 00:17:39 +01001401 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001404static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Mel Gormane12ba742007-10-16 01:25:52 -07001406 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001407 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int len;
1409
1410 if (!tmp)
1411 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001412
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001413 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001414 len = PTR_ERR(pathname);
1415 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001417 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (len > buflen)
1420 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001421 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 len = -EFAULT;
1423 out:
1424 free_page((unsigned long)tmp);
1425 return len;
1426}
1427
1428static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1429{
1430 int error = -EACCES;
1431 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001432 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Eric W. Biederman778c1142006-06-26 00:25:58 -07001434 /* Are we allowed to snoop on the tasks file descriptors? */
1435 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001438 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (error)
1440 goto out;
1441
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001442 error = do_proc_readlink(&path, buffer, buflen);
1443 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return error;
1446}
1447
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001448const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001450 .follow_link = proc_pid_follow_link,
1451 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452};
1453
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001454
1455/* building an inode */
1456
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001457struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001458{
1459 struct inode * inode;
1460 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001461 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001462
1463 /* We need a new inode */
1464
1465 inode = new_inode(sb);
1466 if (!inode)
1467 goto out;
1468
1469 /* Common stuff */
1470 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001471 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001472 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001473 inode->i_op = &proc_def_inode_operations;
1474
1475 /*
1476 * grab the reference to task.
1477 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001478 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001479 if (!ei->pid)
1480 goto out_unlock;
1481
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001482 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001483 rcu_read_lock();
1484 cred = __task_cred(task);
1485 inode->i_uid = cred->euid;
1486 inode->i_gid = cred->egid;
1487 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001488 }
1489 security_task_to_inode(task, inode);
1490
1491out:
1492 return inode;
1493
1494out_unlock:
1495 iput(inode);
1496 return NULL;
1497}
1498
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001499int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001500{
1501 struct inode *inode = dentry->d_inode;
1502 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001503 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001504 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001505
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001506 generic_fillattr(inode, stat);
1507
1508 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001509 stat->uid = GLOBAL_ROOT_UID;
1510 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001511 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1512 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001513 if (!has_pid_permissions(pid, task, 2)) {
1514 rcu_read_unlock();
1515 /*
1516 * This doesn't prevent learning whether PID exists,
1517 * it only makes getattr() consistent with readdir().
1518 */
1519 return -ENOENT;
1520 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001521 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1522 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001523 cred = __task_cred(task);
1524 stat->uid = cred->euid;
1525 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001526 }
1527 }
1528 rcu_read_unlock();
1529 return 0;
1530}
1531
1532/* dentry stuff */
1533
1534/*
1535 * Exceptional case: normally we are not allowed to unhash a busy
1536 * directory. In this case, however, we can do it - no aliasing problems
1537 * due to the way we treat inodes.
1538 *
1539 * Rewrite the inode's ownerships here because the owning task may have
1540 * performed a setuid(), etc.
1541 *
1542 * Before the /proc/pid/status file was created the only way to read
1543 * the effective uid of a /process was to stat /proc/pid. Reading
1544 * /proc/pid/status is slow enough that procps and other packages
1545 * kept stating /proc/pid. To keep the rules in /proc simple I have
1546 * made this apply to all per process world readable and executable
1547 * directories.
1548 */
Al Viro0b728e12012-06-10 16:03:43 -04001549int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001550{
Nick Piggin34286d62011-01-07 17:49:57 +11001551 struct inode *inode;
1552 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001553 const struct cred *cred;
1554
Al Viro0b728e12012-06-10 16:03:43 -04001555 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001556 return -ECHILD;
1557
1558 inode = dentry->d_inode;
1559 task = get_proc_task(inode);
1560
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001561 if (task) {
1562 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1563 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001564 rcu_read_lock();
1565 cred = __task_cred(task);
1566 inode->i_uid = cred->euid;
1567 inode->i_gid = cred->egid;
1568 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001569 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001570 inode->i_uid = GLOBAL_ROOT_UID;
1571 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001572 }
1573 inode->i_mode &= ~(S_ISUID | S_ISGID);
1574 security_task_to_inode(task, inode);
1575 put_task_struct(task);
1576 return 1;
1577 }
1578 d_drop(dentry);
1579 return 0;
1580}
1581
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001582static inline bool proc_inode_is_dead(struct inode *inode)
1583{
1584 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1585}
1586
David Howells1dd704b2013-04-12 01:08:50 +01001587int pid_delete_dentry(const struct dentry *dentry)
1588{
1589 /* Is the task we represent dead?
1590 * If so, then don't put the dentry on the lru list,
1591 * kill it immediately.
1592 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001593 return proc_inode_is_dead(dentry->d_inode);
David Howells1dd704b2013-04-12 01:08:50 +01001594}
1595
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001596const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001597{
1598 .d_revalidate = pid_revalidate,
1599 .d_delete = pid_delete_dentry,
1600};
1601
1602/* Lookups */
1603
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001604/*
1605 * Fill a directory entry.
1606 *
1607 * If possible create the dcache entry and derive our inode number and
1608 * file type from dcache entry.
1609 *
1610 * Since all of the proc inode numbers are dynamically generated, the inode
1611 * numbers do not exist until the inode is cache. This means creating the
1612 * the dcache entry in readdir is necessary to keep the inode numbers
1613 * reported by readdir in sync with the inode numbers reported
1614 * by stat.
1615 */
Al Virof0c3b502013-05-16 12:07:31 -04001616bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001617 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001618 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001619{
Al Virof0c3b502013-05-16 12:07:31 -04001620 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001621 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001622 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001623 unsigned type;
1624 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001625
Al Viro1df98b82013-06-15 11:33:10 +04001626 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001627 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001628 child = d_alloc(dir, &qname);
1629 if (!child)
1630 goto end_instantiate;
1631 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1632 dput(child);
1633 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001634 }
1635 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001636 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001637 ino = inode->i_ino;
1638 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001639 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001640 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001641
1642end_instantiate:
1643 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001644}
1645
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001646#ifdef CONFIG_CHECKPOINT_RESTORE
1647
1648/*
1649 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1650 * which represent vma start and end addresses.
1651 */
1652static int dname_to_vma_addr(struct dentry *dentry,
1653 unsigned long *start, unsigned long *end)
1654{
1655 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1656 return -EINVAL;
1657
1658 return 0;
1659}
1660
Al Viro0b728e12012-06-10 16:03:43 -04001661static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001662{
1663 unsigned long vm_start, vm_end;
1664 bool exact_vma_exists = false;
1665 struct mm_struct *mm = NULL;
1666 struct task_struct *task;
1667 const struct cred *cred;
1668 struct inode *inode;
1669 int status = 0;
1670
Al Viro0b728e12012-06-10 16:03:43 -04001671 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001672 return -ECHILD;
1673
1674 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001675 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001676 goto out_notask;
1677 }
1678
1679 inode = dentry->d_inode;
1680 task = get_proc_task(inode);
1681 if (!task)
1682 goto out_notask;
1683
Cong Wang2344bec2012-05-31 16:26:18 -07001684 mm = mm_access(task, PTRACE_MODE_READ);
1685 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001686 goto out;
1687
1688 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1689 down_read(&mm->mmap_sem);
1690 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1691 up_read(&mm->mmap_sem);
1692 }
1693
1694 mmput(mm);
1695
1696 if (exact_vma_exists) {
1697 if (task_dumpable(task)) {
1698 rcu_read_lock();
1699 cred = __task_cred(task);
1700 inode->i_uid = cred->euid;
1701 inode->i_gid = cred->egid;
1702 rcu_read_unlock();
1703 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001704 inode->i_uid = GLOBAL_ROOT_UID;
1705 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001706 }
1707 security_task_to_inode(task, inode);
1708 status = 1;
1709 }
1710
1711out:
1712 put_task_struct(task);
1713
1714out_notask:
1715 if (status <= 0)
1716 d_drop(dentry);
1717
1718 return status;
1719}
1720
1721static const struct dentry_operations tid_map_files_dentry_operations = {
1722 .d_revalidate = map_files_d_revalidate,
1723 .d_delete = pid_delete_dentry,
1724};
1725
1726static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1727{
1728 unsigned long vm_start, vm_end;
1729 struct vm_area_struct *vma;
1730 struct task_struct *task;
1731 struct mm_struct *mm;
1732 int rc;
1733
1734 rc = -ENOENT;
1735 task = get_proc_task(dentry->d_inode);
1736 if (!task)
1737 goto out;
1738
1739 mm = get_task_mm(task);
1740 put_task_struct(task);
1741 if (!mm)
1742 goto out;
1743
1744 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1745 if (rc)
1746 goto out_mmput;
1747
Artem Fetishev70335ab2014-03-10 15:49:45 -07001748 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001749 down_read(&mm->mmap_sem);
1750 vma = find_exact_vma(mm, vm_start, vm_end);
1751 if (vma && vma->vm_file) {
1752 *path = vma->vm_file->f_path;
1753 path_get(path);
1754 rc = 0;
1755 }
1756 up_read(&mm->mmap_sem);
1757
1758out_mmput:
1759 mmput(mm);
1760out:
1761 return rc;
1762}
1763
1764struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001765 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001766 unsigned long len;
1767 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1768};
1769
Al Viroc52a47a2013-06-15 11:15:20 +04001770static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001771proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1772 struct task_struct *task, const void *ptr)
1773{
Al Viro7b540d02012-08-27 14:55:26 -04001774 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001775 struct proc_inode *ei;
1776 struct inode *inode;
1777
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001778 inode = proc_pid_make_inode(dir->i_sb, task);
1779 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001780 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001781
1782 ei = PROC_I(inode);
1783 ei->op.proc_get_link = proc_map_files_get_link;
1784
1785 inode->i_op = &proc_pid_link_inode_operations;
1786 inode->i_size = 64;
1787 inode->i_mode = S_IFLNK;
1788
Al Viro7b540d02012-08-27 14:55:26 -04001789 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001790 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001791 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001792 inode->i_mode |= S_IWUSR;
1793
1794 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1795 d_add(dentry, inode);
1796
Al Viroc52a47a2013-06-15 11:15:20 +04001797 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001798}
1799
1800static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001801 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001802{
1803 unsigned long vm_start, vm_end;
1804 struct vm_area_struct *vma;
1805 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001806 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001807 struct mm_struct *mm;
1808
Al Viroc52a47a2013-06-15 11:15:20 +04001809 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001810 if (!capable(CAP_SYS_ADMIN))
1811 goto out;
1812
Al Viroc52a47a2013-06-15 11:15:20 +04001813 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001814 task = get_proc_task(dir);
1815 if (!task)
1816 goto out;
1817
Al Viroc52a47a2013-06-15 11:15:20 +04001818 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001819 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001820 goto out_put_task;
1821
Al Viroc52a47a2013-06-15 11:15:20 +04001822 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001823 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001824 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001825
1826 mm = get_task_mm(task);
1827 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001828 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001829
1830 down_read(&mm->mmap_sem);
1831 vma = find_exact_vma(mm, vm_start, vm_end);
1832 if (!vma)
1833 goto out_no_vma;
1834
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001835 if (vma->vm_file)
1836 result = proc_map_files_instantiate(dir, dentry, task,
1837 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001838
1839out_no_vma:
1840 up_read(&mm->mmap_sem);
1841 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001842out_put_task:
1843 put_task_struct(task);
1844out:
Al Viroc52a47a2013-06-15 11:15:20 +04001845 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001846}
1847
1848static const struct inode_operations proc_map_files_inode_operations = {
1849 .lookup = proc_map_files_lookup,
1850 .permission = proc_fd_permission,
1851 .setattr = proc_setattr,
1852};
1853
1854static int
Al Virof0c3b502013-05-16 12:07:31 -04001855proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001856{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001857 struct vm_area_struct *vma;
1858 struct task_struct *task;
1859 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001860 unsigned long nr_files, pos, i;
1861 struct flex_array *fa = NULL;
1862 struct map_files_info info;
1863 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001864 int ret;
1865
Zhao Hongjiang41735812013-02-20 13:13:55 +11001866 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001867 if (!capable(CAP_SYS_ADMIN))
1868 goto out;
1869
1870 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001871 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001872 if (!task)
1873 goto out;
1874
1875 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001876 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001877 goto out_put_task;
1878
1879 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001880 if (!dir_emit_dots(file, ctx))
1881 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001882
Al Virof0c3b502013-05-16 12:07:31 -04001883 mm = get_task_mm(task);
1884 if (!mm)
1885 goto out_put_task;
1886 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001887
Al Virof0c3b502013-05-16 12:07:31 -04001888 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001889
Al Virof0c3b502013-05-16 12:07:31 -04001890 /*
1891 * We need two passes here:
1892 *
1893 * 1) Collect vmas of mapped files with mmap_sem taken
1894 * 2) Release mmap_sem and instantiate entries
1895 *
1896 * otherwise we get lockdep complained, since filldir()
1897 * routine might require mmap_sem taken in might_fault().
1898 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001899
Al Virof0c3b502013-05-16 12:07:31 -04001900 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1901 if (vma->vm_file && ++pos > ctx->pos)
1902 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001903 }
Al Virof0c3b502013-05-16 12:07:31 -04001904
1905 if (nr_files) {
1906 fa = flex_array_alloc(sizeof(info), nr_files,
1907 GFP_KERNEL);
1908 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1909 GFP_KERNEL)) {
1910 ret = -ENOMEM;
1911 if (fa)
1912 flex_array_free(fa);
1913 up_read(&mm->mmap_sem);
1914 mmput(mm);
1915 goto out_put_task;
1916 }
1917 for (i = 0, vma = mm->mmap, pos = 2; vma;
1918 vma = vma->vm_next) {
1919 if (!vma->vm_file)
1920 continue;
1921 if (++pos <= ctx->pos)
1922 continue;
1923
1924 info.mode = vma->vm_file->f_mode;
1925 info.len = snprintf(info.name,
1926 sizeof(info.name), "%lx-%lx",
1927 vma->vm_start, vma->vm_end);
1928 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1929 BUG();
1930 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001931 }
Al Virof0c3b502013-05-16 12:07:31 -04001932 up_read(&mm->mmap_sem);
1933
1934 for (i = 0; i < nr_files; i++) {
1935 p = flex_array_get(fa, i);
1936 if (!proc_fill_cache(file, ctx,
1937 p->name, p->len,
1938 proc_map_files_instantiate,
1939 task,
1940 (void *)(unsigned long)p->mode))
1941 break;
1942 ctx->pos++;
1943 }
1944 if (fa)
1945 flex_array_free(fa);
1946 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001947
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001948out_put_task:
1949 put_task_struct(task);
1950out:
1951 return ret;
1952}
1953
1954static const struct file_operations proc_map_files_operations = {
1955 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04001956 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001957 .llseek = default_llseek,
1958};
1959
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001960struct timers_private {
1961 struct pid *pid;
1962 struct task_struct *task;
1963 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04001964 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001965 unsigned long flags;
1966};
1967
1968static void *timers_start(struct seq_file *m, loff_t *pos)
1969{
1970 struct timers_private *tp = m->private;
1971
1972 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
1973 if (!tp->task)
1974 return ERR_PTR(-ESRCH);
1975
1976 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
1977 if (!tp->sighand)
1978 return ERR_PTR(-ESRCH);
1979
1980 return seq_list_start(&tp->task->signal->posix_timers, *pos);
1981}
1982
1983static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
1984{
1985 struct timers_private *tp = m->private;
1986 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
1987}
1988
1989static void timers_stop(struct seq_file *m, void *v)
1990{
1991 struct timers_private *tp = m->private;
1992
1993 if (tp->sighand) {
1994 unlock_task_sighand(tp->task, &tp->flags);
1995 tp->sighand = NULL;
1996 }
1997
1998 if (tp->task) {
1999 put_task_struct(tp->task);
2000 tp->task = NULL;
2001 }
2002}
2003
2004static int show_timer(struct seq_file *m, void *v)
2005{
2006 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002007 struct timers_private *tp = m->private;
2008 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002009 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002010 [SIGEV_SIGNAL] = "signal",
2011 [SIGEV_NONE] = "none",
2012 [SIGEV_THREAD] = "thread",
2013 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002014
2015 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002016 notify = timer->it_sigev_notify;
2017
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002018 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002019 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2020 timer->sigq->info.si_value.sival_ptr);
2021 seq_printf(m, "notify: %s/%s.%d\n",
2022 nstr[notify & ~SIGEV_THREAD_ID],
2023 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2024 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002025 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002026
2027 return 0;
2028}
2029
2030static const struct seq_operations proc_timers_seq_ops = {
2031 .start = timers_start,
2032 .next = timers_next,
2033 .stop = timers_stop,
2034 .show = show_timer,
2035};
2036
2037static int proc_timers_open(struct inode *inode, struct file *file)
2038{
2039 struct timers_private *tp;
2040
2041 tp = __seq_open_private(file, &proc_timers_seq_ops,
2042 sizeof(struct timers_private));
2043 if (!tp)
2044 return -ENOMEM;
2045
2046 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002047 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002048 return 0;
2049}
2050
2051static const struct file_operations proc_timers_operations = {
2052 .open = proc_timers_open,
2053 .read = seq_read,
2054 .llseek = seq_lseek,
2055 .release = seq_release_private,
2056};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002057#endif /* CONFIG_CHECKPOINT_RESTORE */
2058
Al Viroc52a47a2013-06-15 11:15:20 +04002059static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002060 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002061{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002062 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002063 struct inode *inode;
2064 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002065
Eric W. Biederman61a28782006-10-02 02:18:49 -07002066 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002067 if (!inode)
2068 goto out;
2069
2070 ei = PROC_I(inode);
2071 inode->i_mode = p->mode;
2072 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002073 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002074 if (p->iop)
2075 inode->i_op = p->iop;
2076 if (p->fop)
2077 inode->i_fop = p->fop;
2078 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002079 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002080 d_add(dentry, inode);
2081 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002082 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002083 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002084out:
Al Viroc52a47a2013-06-15 11:15:20 +04002085 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002086}
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static struct dentry *proc_pident_lookup(struct inode *dir,
2089 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002090 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002091 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Al Viroc52a47a2013-06-15 11:15:20 +04002093 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002094 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002095 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Al Viroc52a47a2013-06-15 11:15:20 +04002097 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Eric W. Biederman99f89552006-06-26 00:25:55 -07002099 if (!task)
2100 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002102 /*
2103 * Yes, it does not scale. And it should not. Don't add
2104 * new entries into /proc/<tgid>/ without very good reasons.
2105 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002106 last = &ents[nents - 1];
2107 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (p->len != dentry->d_name.len)
2109 continue;
2110 if (!memcmp(dentry->d_name.name, p->name, p->len))
2111 break;
2112 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002113 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 goto out;
2115
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002116 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002118 put_task_struct(task);
2119out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002120 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Al Virof0c3b502013-05-16 12:07:31 -04002123static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002124 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002125{
Al Virof0c3b502013-05-16 12:07:31 -04002126 struct task_struct *task = get_proc_task(file_inode(file));
2127 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002128
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002129 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002130 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002131
Al Virof0c3b502013-05-16 12:07:31 -04002132 if (!dir_emit_dots(file, ctx))
2133 goto out;
2134
2135 if (ctx->pos >= nents + 2)
2136 goto out;
2137
2138 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2139 if (!proc_fill_cache(file, ctx, p->name, p->len,
2140 proc_pident_instantiate, task, p))
2141 break;
2142 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002143 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002144out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002145 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002150static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2151 size_t count, loff_t *ppos)
2152{
Al Viro496ad9a2013-01-23 17:07:38 -05002153 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002154 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002155 ssize_t length;
2156 struct task_struct *task = get_proc_task(inode);
2157
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002158 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002159 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002160
2161 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002162 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002163 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002164 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002165 if (length > 0)
2166 length = simple_read_from_buffer(buf, count, ppos, p, length);
2167 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002168 return length;
2169}
2170
2171static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2172 size_t count, loff_t *ppos)
2173{
Al Viro496ad9a2013-01-23 17:07:38 -05002174 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002175 char *page;
2176 ssize_t length;
2177 struct task_struct *task = get_proc_task(inode);
2178
2179 length = -ESRCH;
2180 if (!task)
2181 goto out_no_task;
2182 if (count > PAGE_SIZE)
2183 count = PAGE_SIZE;
2184
2185 /* No partial writes. */
2186 length = -EINVAL;
2187 if (*ppos != 0)
2188 goto out;
2189
2190 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002191 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002192 if (!page)
2193 goto out;
2194
2195 length = -EFAULT;
2196 if (copy_from_user(page, buf, count))
2197 goto out_free;
2198
David Howells107db7c2009-05-08 13:55:27 +01002199 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002200 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002201 if (length < 0)
2202 goto out_free;
2203
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002204 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002205 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002206 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002207 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002208out_free:
2209 free_page((unsigned long) page);
2210out:
2211 put_task_struct(task);
2212out_no_task:
2213 return length;
2214}
2215
Arjan van de Ven00977a52007-02-12 00:55:34 -08002216static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002217 .read = proc_pid_attr_read,
2218 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002219 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002220};
2221
Eric Dumazetc5141e62007-05-08 00:26:15 -07002222static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002223 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2224 REG("prev", S_IRUGO, proc_pid_attr_operations),
2225 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2226 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2227 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2228 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002229};
2230
Al Virof0c3b502013-05-16 12:07:31 -04002231static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Al Virof0c3b502013-05-16 12:07:31 -04002233 return proc_pident_readdir(file, ctx,
2234 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Arjan van de Ven00977a52007-02-12 00:55:34 -08002237static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002239 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002240 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241};
2242
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002243static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002244 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002246 return proc_pident_lookup(dir, dentry,
2247 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002250static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002251 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002252 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002253 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254};
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256#endif
2257
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002258#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002259static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2260 size_t count, loff_t *ppos)
2261{
Al Viro496ad9a2013-01-23 17:07:38 -05002262 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002263 struct mm_struct *mm;
2264 char buffer[PROC_NUMBUF];
2265 size_t len;
2266 int ret;
2267
2268 if (!task)
2269 return -ESRCH;
2270
2271 ret = 0;
2272 mm = get_task_mm(task);
2273 if (mm) {
2274 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2275 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2276 MMF_DUMP_FILTER_SHIFT));
2277 mmput(mm);
2278 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2279 }
2280
2281 put_task_struct(task);
2282
2283 return ret;
2284}
2285
2286static ssize_t proc_coredump_filter_write(struct file *file,
2287 const char __user *buf,
2288 size_t count,
2289 loff_t *ppos)
2290{
2291 struct task_struct *task;
2292 struct mm_struct *mm;
2293 char buffer[PROC_NUMBUF], *end;
2294 unsigned int val;
2295 int ret;
2296 int i;
2297 unsigned long mask;
2298
2299 ret = -EFAULT;
2300 memset(buffer, 0, sizeof(buffer));
2301 if (count > sizeof(buffer) - 1)
2302 count = sizeof(buffer) - 1;
2303 if (copy_from_user(buffer, buf, count))
2304 goto out_no_task;
2305
2306 ret = -EINVAL;
2307 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2308 if (*end == '\n')
2309 end++;
2310 if (end - buffer == 0)
2311 goto out_no_task;
2312
2313 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002314 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002315 if (!task)
2316 goto out_no_task;
2317
2318 ret = end - buffer;
2319 mm = get_task_mm(task);
2320 if (!mm)
2321 goto out_no_mm;
2322
2323 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2324 if (val & mask)
2325 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2326 else
2327 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2328 }
2329
2330 mmput(mm);
2331 out_no_mm:
2332 put_task_struct(task);
2333 out_no_task:
2334 return ret;
2335}
2336
2337static const struct file_operations proc_coredump_filter_operations = {
2338 .read = proc_coredump_filter_read,
2339 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002340 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002341};
2342#endif
2343
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002344#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002345static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002346{
Andrea Righi940389b2008-07-28 00:48:12 +02002347 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002348 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002349 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002350
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002351 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2352 if (result)
2353 return result;
2354
2355 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2356 result = -EACCES;
2357 goto out_unlock;
2358 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002359
Andrea Righi59954772008-07-27 17:29:15 +02002360 if (whole && lock_task_sighand(task, &flags)) {
2361 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002362
Andrea Righi59954772008-07-27 17:29:15 +02002363 task_io_accounting_add(&acct, &task->signal->ioac);
2364 while_each_thread(task, t)
2365 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002366
Andrea Righi59954772008-07-27 17:29:15 +02002367 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002368 }
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002369 result = seq_printf(m,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002370 "rchar: %llu\n"
2371 "wchar: %llu\n"
2372 "syscr: %llu\n"
2373 "syscw: %llu\n"
2374 "read_bytes: %llu\n"
2375 "write_bytes: %llu\n"
2376 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002377 (unsigned long long)acct.rchar,
2378 (unsigned long long)acct.wchar,
2379 (unsigned long long)acct.syscr,
2380 (unsigned long long)acct.syscw,
2381 (unsigned long long)acct.read_bytes,
2382 (unsigned long long)acct.write_bytes,
2383 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002384out_unlock:
2385 mutex_unlock(&task->signal->cred_guard_mutex);
2386 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002387}
Andrea Righi297c5d92008-07-25 01:48:49 -07002388
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002389static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2390 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002391{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002392 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002393}
2394
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002395static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2396 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002397{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002398 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002399}
2400#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002401
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002402#ifdef CONFIG_USER_NS
2403static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002404 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002405{
2406 struct user_namespace *ns = NULL;
2407 struct task_struct *task;
2408 struct seq_file *seq;
2409 int ret = -EINVAL;
2410
2411 task = get_proc_task(inode);
2412 if (task) {
2413 rcu_read_lock();
2414 ns = get_user_ns(task_cred_xxx(task, user_ns));
2415 rcu_read_unlock();
2416 put_task_struct(task);
2417 }
2418 if (!ns)
2419 goto err;
2420
2421 ret = seq_open(file, seq_ops);
2422 if (ret)
2423 goto err_put_ns;
2424
2425 seq = file->private_data;
2426 seq->private = ns;
2427
2428 return 0;
2429err_put_ns:
2430 put_user_ns(ns);
2431err:
2432 return ret;
2433}
2434
2435static int proc_id_map_release(struct inode *inode, struct file *file)
2436{
2437 struct seq_file *seq = file->private_data;
2438 struct user_namespace *ns = seq->private;
2439 put_user_ns(ns);
2440 return seq_release(inode, file);
2441}
2442
2443static int proc_uid_map_open(struct inode *inode, struct file *file)
2444{
2445 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2446}
2447
2448static int proc_gid_map_open(struct inode *inode, struct file *file)
2449{
2450 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2451}
2452
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002453static int proc_projid_map_open(struct inode *inode, struct file *file)
2454{
2455 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2456}
2457
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002458static const struct file_operations proc_uid_map_operations = {
2459 .open = proc_uid_map_open,
2460 .write = proc_uid_map_write,
2461 .read = seq_read,
2462 .llseek = seq_lseek,
2463 .release = proc_id_map_release,
2464};
2465
2466static const struct file_operations proc_gid_map_operations = {
2467 .open = proc_gid_map_open,
2468 .write = proc_gid_map_write,
2469 .read = seq_read,
2470 .llseek = seq_lseek,
2471 .release = proc_id_map_release,
2472};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002473
2474static const struct file_operations proc_projid_map_operations = {
2475 .open = proc_projid_map_open,
2476 .write = proc_projid_map_write,
2477 .read = seq_read,
2478 .llseek = seq_lseek,
2479 .release = proc_id_map_release,
2480};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002481#endif /* CONFIG_USER_NS */
2482
Kees Cook47830722008-10-06 03:11:58 +04002483static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2484 struct pid *pid, struct task_struct *task)
2485{
Al Viroa9712bc2011-03-23 15:52:50 -04002486 int err = lock_trace(task);
2487 if (!err) {
2488 seq_printf(m, "%08x\n", task->personality);
2489 unlock_trace(task);
2490 }
2491 return err;
Kees Cook47830722008-10-06 03:11:58 +04002492}
2493
Eric W. Biederman801199c2006-10-02 02:18:48 -07002494/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002495 * Thread groups
2496 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002497static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002498static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002499
Eric Dumazetc5141e62007-05-08 00:26:15 -07002500static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002501 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2502 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002503#ifdef CONFIG_CHECKPOINT_RESTORE
2504 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2505#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002506 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002507 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002508#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002509 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002510#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002511 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002512 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002513 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002514 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002515 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002516#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002517 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002518#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002519#ifdef CONFIG_SCHED_AUTOGROUP
2520 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2521#endif
john stultz4614a696b2009-12-14 18:00:05 -08002522 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002523#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002524 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002525#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002526 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002527 ONE("stat", S_IRUGO, proc_tgid_stat),
2528 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002529 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002530#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002531 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002532#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002533 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2534 LNK("cwd", proc_cwd_link),
2535 LNK("root", proc_root_link),
2536 LNK("exe", proc_exe_link),
2537 REG("mounts", S_IRUGO, proc_mounts_operations),
2538 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2539 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002540#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002541 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002542 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002543 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002544#endif
2545#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002546 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002547#endif
2548#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002549 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002550#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002551#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002552 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002553#endif
2554#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002555 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002556#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002557#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002558 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002559#endif
Paul Menage8793d852007-10-18 23:39:39 -07002560#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002561 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002562#endif
Paul Menagea4243162007-10-18 23:39:35 -07002563#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002564 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002565#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002566 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002567 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002568 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002569#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002570 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2571 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002572#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002573#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002574 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002575#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002576#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002577 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002578#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002579#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002580 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002581#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002582#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002583 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002584#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002585#ifdef CONFIG_USER_NS
2586 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2587 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002588 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002589#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002590#ifdef CONFIG_CHECKPOINT_RESTORE
2591 REG("timers", S_IRUGO, proc_timers_operations),
2592#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002593};
2594
Al Virof0c3b502013-05-16 12:07:31 -04002595static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002596{
Al Virof0c3b502013-05-16 12:07:31 -04002597 return proc_pident_readdir(file, ctx,
2598 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002599}
2600
Arjan van de Ven00977a52007-02-12 00:55:34 -08002601static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002603 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002604 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002605};
2606
Al Viro00cd8dd2012-06-10 17:13:09 -04002607static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2608{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002609 return proc_pident_lookup(dir, dentry,
2610 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002611}
2612
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002613static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002614 .lookup = proc_tgid_base_lookup,
2615 .getattr = pid_getattr,
2616 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002617 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002618};
2619
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002620static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002622 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002623 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002624 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Eric W. Biederman48e64842006-06-26 00:25:48 -07002626 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002627 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002628 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002629 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002630 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002631 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002632 d_drop(dentry);
2633 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Eric W. Biederman48e64842006-06-26 00:25:48 -07002636 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002637 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2638 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002639 if (!leader)
2640 goto out;
2641
2642 name.name = "task";
2643 name.len = strlen(name.name);
2644 dir = d_hash_and_lookup(leader, &name);
2645 if (!dir)
2646 goto out_put_leader;
2647
2648 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002649 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002650 dentry = d_hash_and_lookup(dir, &name);
2651 if (dentry) {
2652 shrink_dcache_parent(dentry);
2653 d_drop(dentry);
2654 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002656
2657 dput(dir);
2658out_put_leader:
2659 dput(leader);
2660out:
2661 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
Randy Dunlap0895e912007-10-21 21:00:10 -07002664/**
2665 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2666 * @task: task that should be flushed.
2667 *
2668 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002669 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002670 * in. This call is supposed to do all of this job.
2671 *
2672 * Looks in the dcache for
2673 * /proc/@pid
2674 * /proc/@tgid/task/@pid
2675 * if either directory is present flushes it and all of it'ts children
2676 * from the dcache.
2677 *
2678 * It is safe and reasonable to cache /proc entries for a task until
2679 * that task exits. After that they just clog up the dcache with
2680 * useless entries, possibly causing useful dcache entries to be
2681 * flushed instead. This routine is proved to flush those useless
2682 * dcache entries at process exit time.
2683 *
2684 * NOTE: This routine is just an optimization so it does not guarantee
2685 * that no dcache entries will exist at process exit time it
2686 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002687 */
2688
2689void proc_flush_task(struct task_struct *task)
2690{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002691 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002692 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002693 struct upid *upid;
2694
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002695 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002696 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002697
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002698 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002699 upid = &pid->numbers[i];
2700 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002701 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002702 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002703}
2704
Al Viroc52a47a2013-06-15 11:15:20 +04002705static int proc_pid_instantiate(struct inode *dir,
2706 struct dentry * dentry,
2707 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002708{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002709 struct inode *inode;
2710
Eric W. Biederman61a28782006-10-02 02:18:49 -07002711 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002712 if (!inode)
2713 goto out;
2714
2715 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2716 inode->i_op = &proc_tgid_base_inode_operations;
2717 inode->i_fop = &proc_tgid_base_operations;
2718 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002719
Miklos Szeredibfe86842011-10-28 14:13:29 +02002720 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2721 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002722
Nick Pigginfb045ad2011-01-07 17:49:55 +11002723 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002724
2725 d_add(dentry, inode);
2726 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002727 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002728 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002729out:
Al Viroc52a47a2013-06-15 11:15:20 +04002730 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002731}
2732
Al Viro00cd8dd2012-06-10 17:13:09 -04002733struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002735 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002738 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002740 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (tgid == ~0U)
2742 goto out;
2743
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002744 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002745 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002746 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 if (task)
2748 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002749 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 if (!task)
2751 goto out;
2752
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002753 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002754 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755out:
Al Viroc52a47a2013-06-15 11:15:20 +04002756 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002760 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002763struct tgid_iter {
2764 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002765 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002766};
2767static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2768{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002769 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002771 if (iter.task)
2772 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002773 rcu_read_lock();
2774retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002775 iter.task = NULL;
2776 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002777 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002778 iter.tgid = pid_nr_ns(pid, ns);
2779 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002780 /* What we to know is if the pid we have find is the
2781 * pid of a thread_group_leader. Testing for task
2782 * being a thread_group_leader is the obvious thing
2783 * todo but there is a window when it fails, due to
2784 * the pid transfer logic in de_thread.
2785 *
2786 * So we perform the straight forward test of seeing
2787 * if the pid we have found is the pid of a thread
2788 * group leader, and don't worry if the task we have
2789 * found doesn't happen to be a thread group leader.
2790 * As we don't care in the case of readdir.
2791 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002792 if (!iter.task || !has_group_leader_pid(iter.task)) {
2793 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002794 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002795 }
2796 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002798 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002799 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
2801
Eric W. Biederman00978752014-07-31 03:10:50 -07002802#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002805int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002807 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002808 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002809 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Al Viro021ada72013-03-29 19:27:05 -04002811 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Eric W. Biederman00978752014-07-31 03:10:50 -07002814 if (pos == TGID_OFFSET - 2) {
Al Virodb963162013-06-15 10:45:10 +04002815 struct inode *inode = ns->proc_self->d_inode;
2816 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002817 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07002818 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04002819 }
Eric W. Biederman00978752014-07-31 03:10:50 -07002820 if (pos == TGID_OFFSET - 1) {
2821 struct inode *inode = ns->proc_thread_self->d_inode;
2822 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
2823 return 0;
2824 ctx->pos = pos = pos + 1;
2825 }
2826 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002827 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002828 for (iter = next_tgid(ns, iter);
2829 iter.task;
2830 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002831 char name[PROC_NUMBUF];
2832 int len;
2833 if (!has_pid_permissions(ns, iter.task, 2))
2834 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002835
Al Virof0c3b502013-05-16 12:07:31 -04002836 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2837 ctx->pos = iter.tgid + TGID_OFFSET;
2838 if (!proc_fill_cache(file, ctx, name, len,
2839 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002840 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Al Virof0c3b502013-05-16 12:07:31 -04002844 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return 0;
2846}
2847
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002848/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002849 * Tasks
2850 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002851static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002852 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002853 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002854 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07002855#ifdef CONFIG_NET
2856 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2857#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002858 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002859 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002860 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002861 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002862 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002863#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002864 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002865#endif
john stultz4614a696b2009-12-14 18:00:05 -08002866 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002867#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002868 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002869#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002870 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002871 ONE("stat", S_IRUGO, proc_tid_stat),
2872 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002873 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002874#ifdef CONFIG_CHECKPOINT_RESTORE
2875 REG("children", S_IRUGO, proc_tid_children_operations),
2876#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002877#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002878 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002879#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002880 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2881 LNK("cwd", proc_cwd_link),
2882 LNK("root", proc_root_link),
2883 LNK("exe", proc_exe_link),
2884 REG("mounts", S_IRUGO, proc_mounts_operations),
2885 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002886#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002887 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002888 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002889 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002890#endif
2891#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002892 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002893#endif
2894#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002895 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002896#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002897#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002898 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002899#endif
2900#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002901 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002902#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002903#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002904 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002905#endif
Paul Menage8793d852007-10-18 23:39:39 -07002906#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002907 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002908#endif
Paul Menagea4243162007-10-18 23:39:35 -07002909#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002910 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002911#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002912 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002913 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002914 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002915#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002916 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002917 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002918#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002919#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002920 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002921#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002922#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002923 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002924#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002925#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002926 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002927#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002928#ifdef CONFIG_USER_NS
2929 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2930 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002931 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002932#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933};
2934
Al Virof0c3b502013-05-16 12:07:31 -04002935static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002936{
Al Virof0c3b502013-05-16 12:07:31 -04002937 return proc_pident_readdir(file, ctx,
2938 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939}
2940
Al Viro00cd8dd2012-06-10 17:13:09 -04002941static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2942{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002943 return proc_pident_lookup(dir, dentry,
2944 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945}
2946
Arjan van de Ven00977a52007-02-12 00:55:34 -08002947static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002948 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002949 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002950 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002951};
2952
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002953static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954 .lookup = proc_tid_base_lookup,
2955 .getattr = pid_getattr,
2956 .setattr = proc_setattr,
2957};
2958
Al Viroc52a47a2013-06-15 11:15:20 +04002959static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002960 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002961{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002962 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002963 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002964
2965 if (!inode)
2966 goto out;
2967 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2968 inode->i_op = &proc_tid_base_inode_operations;
2969 inode->i_fop = &proc_tid_base_operations;
2970 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002971
Miklos Szeredibfe86842011-10-28 14:13:29 +02002972 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
2973 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002974
Nick Pigginfb045ad2011-01-07 17:49:55 +11002975 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002976
2977 d_add(dentry, inode);
2978 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002979 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002980 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002981out:
Al Viroc52a47a2013-06-15 11:15:20 +04002982 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002983}
2984
Al Viro00cd8dd2012-06-10 17:13:09 -04002985static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002986{
Al Viroc52a47a2013-06-15 11:15:20 +04002987 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002988 struct task_struct *task;
2989 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002991 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002992
2993 if (!leader)
2994 goto out_no_task;
2995
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002996 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002997 if (tid == ~0U)
2998 goto out;
2999
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003000 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003001 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003002 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003003 if (task)
3004 get_task_struct(task);
3005 rcu_read_unlock();
3006 if (!task)
3007 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003008 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009 goto out_drop_task;
3010
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003011 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012out_drop_task:
3013 put_task_struct(task);
3014out:
3015 put_task_struct(leader);
3016out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003017 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018}
3019
3020/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003021 * Find the first tid of a thread group to return to user space.
3022 *
3023 * Usually this is just the thread group leader, but if the users
3024 * buffer was too small or there was a seek into the middle of the
3025 * directory we have more work todo.
3026 *
3027 * In the case of a short read we start with find_task_by_pid.
3028 *
3029 * In the case of a seek we start with the leader and walk nr
3030 * threads past it.
3031 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003032static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3033 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003034{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003035 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003036 unsigned long nr = f_pos;
3037
3038 if (nr != f_pos) /* 32bit overflow? */
3039 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003040
Eric W. Biedermancc288732006-06-26 00:26:01 -07003041 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003042 task = pid_task(pid, PIDTYPE_PID);
3043 if (!task)
3044 goto fail;
3045
3046 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003047 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003048 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003049 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003050 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003051 }
3052
3053 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003054 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003055 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003056
3057 /* If we haven't found our starting place yet start
3058 * with the leader and walk nr threads forward.
3059 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003060 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003061 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003062 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003063 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003064 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003065fail:
3066 pos = NULL;
3067 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003068found:
3069 get_task_struct(pos);
3070out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003071 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003072 return pos;
3073}
3074
3075/*
3076 * Find the next thread in the thread list.
3077 * Return NULL if there is an error or no next thread.
3078 *
3079 * The reference to the input task_struct is released.
3080 */
3081static struct task_struct *next_tid(struct task_struct *start)
3082{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003083 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003084 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003085 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003086 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003087 if (thread_group_leader(pos))
3088 pos = NULL;
3089 else
3090 get_task_struct(pos);
3091 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003092 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003093 put_task_struct(start);
3094 return pos;
3095}
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003098static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003100 struct inode *inode = file_inode(file);
3101 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003102 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003103 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003105 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003106 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Al Virof0c3b502013-05-16 12:07:31 -04003108 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003111 /* f_version caches the tgid value that the last readdir call couldn't
3112 * return. lseek aka telldir automagically resets f_version to 0.
3113 */
Al Virof0c3b502013-05-16 12:07:31 -04003114 ns = file->f_dentry->d_sb->s_fs_info;
3115 tid = (int)file->f_version;
3116 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003117 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003118 task;
Al Virof0c3b502013-05-16 12:07:31 -04003119 task = next_tid(task), ctx->pos++) {
3120 char name[PROC_NUMBUF];
3121 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003122 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003123 len = snprintf(name, sizeof(name), "%d", tid);
3124 if (!proc_fill_cache(file, ctx, name, len,
3125 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003126 /* returning this tgid failed, save it as the first
3127 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003128 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003129 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003133
Al Virof0c3b502013-05-16 12:07:31 -04003134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003136
3137static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3138{
3139 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003140 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003141 generic_fillattr(inode, stat);
3142
Eric W. Biederman99f89552006-06-26 00:25:55 -07003143 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003144 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003145 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003146 }
3147
3148 return 0;
3149}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003150
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003151static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003152 .lookup = proc_task_lookup,
3153 .getattr = proc_task_getattr,
3154 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003155 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003156};
3157
Arjan van de Ven00977a52007-02-12 00:55:34 -08003158static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003159 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003160 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003161 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003162};