blob: f5d0f8f39a57f3bc279f79692dad961af689b9e3 [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, {})
133#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700134 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700135 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_read = read } )
137#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300140 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Vegard Nossumaed54172008-06-05 22:46:53 -0700142/*
143 * Count the number of hardlinks for the pid_entry table, excluding the .
144 * and .. links.
145 */
146static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147 unsigned int n)
148{
149 unsigned int i;
150 unsigned int count;
151
152 count = 0;
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
155 ++count;
156 }
157
158 return count;
159}
160
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200161static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000163 int result = -ENOENT;
164
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700165 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200166 if (task->fs) {
167 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000168 result = 0;
169 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000171 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700172}
173
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800174static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800176 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700177 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700178
179 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200180 task_lock(task);
181 if (task->fs) {
182 get_fs_pwd(task->fs, path);
183 result = 0;
184 }
185 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700186 put_task_struct(task);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return result;
189}
190
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800191static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800193 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700195
196 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200197 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700198 put_task_struct(task);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return result;
201}
202
William Roberts21a64572014-02-11 10:12:00 -0800203static int proc_pid_cmdline(struct task_struct *task, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
William Roberts21a64572014-02-11 10:12:00 -0800205 return get_cmdline(task, buffer, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700208static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
209 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Cong Wange7dcd992012-05-31 16:26:17 -0700211 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500212 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300214 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300216 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700217 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 mmput(mm);
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700219 return 0;
220 } else
221 return PTR_ERR(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224
225#ifdef CONFIG_KALLSYMS
226/*
227 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
228 * Returns the resolved symbol. If that fails, simply return the address.
229 */
230static int proc_pid_wchan(struct task_struct *task, char *buffer)
231{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700232 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700233 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 wchan = get_wchan(task);
236
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700237 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600238 if (!ptrace_may_access(task, PTRACE_MODE_READ))
239 return 0;
240 else
241 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700242 else
243 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245#endif /* CONFIG_KALLSYMS */
246
Al Viroa9712bc2011-03-23 15:52:50 -0400247static int lock_trace(struct task_struct *task)
248{
249 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
250 if (err)
251 return err;
252 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
253 mutex_unlock(&task->signal->cred_guard_mutex);
254 return -EPERM;
255 }
256 return 0;
257}
258
259static void unlock_trace(struct task_struct *task)
260{
261 mutex_unlock(&task->signal->cred_guard_mutex);
262}
263
Ken Chen2ec220e2008-11-10 11:26:08 +0300264#ifdef CONFIG_STACKTRACE
265
266#define MAX_STACK_TRACE_DEPTH 64
267
268static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
269 struct pid *pid, struct task_struct *task)
270{
271 struct stack_trace trace;
272 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400273 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300274 int i;
275
276 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
277 if (!entries)
278 return -ENOMEM;
279
280 trace.nr_entries = 0;
281 trace.max_entries = MAX_STACK_TRACE_DEPTH;
282 trace.entries = entries;
283 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300284
Al Viroa9712bc2011-03-23 15:52:50 -0400285 err = lock_trace(task);
286 if (!err) {
287 save_stack_trace_tsk(task, &trace);
288
289 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700290 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400291 (void *)entries[i], (void *)entries[i]);
292 }
293 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300294 }
295 kfree(entries);
296
Al Viroa9712bc2011-03-23 15:52:50 -0400297 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300298}
299#endif
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#ifdef CONFIG_SCHEDSTATS
302/*
303 * Provides /proc/PID/schedstat
304 */
305static int proc_pid_schedstat(struct task_struct *task, char *buffer)
306{
Balbir Singh172ba842007-07-09 18:52:00 +0200307 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100308 (unsigned long long)task->se.sum_exec_runtime,
309 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200310 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312#endif
313
Arjan van de Ven97455122008-01-25 21:08:34 +0100314#ifdef CONFIG_LATENCYTOP
315static int lstats_show_proc(struct seq_file *m, void *v)
316{
317 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800318 struct inode *inode = m->private;
319 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100320
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800321 if (!task)
322 return -ESRCH;
323 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100324 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800325 struct latency_record *lr = &task->latency_record[i];
326 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100327 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800328 seq_printf(m, "%i %li %li",
329 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100330 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800331 unsigned long bt = lr->backtrace[q];
332 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100333 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800334 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100335 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800336 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100337 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800338 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100339 }
340
341 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800342 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100343 return 0;
344}
345
346static int lstats_open(struct inode *inode, struct file *file)
347{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800348 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800349}
350
Arjan van de Ven97455122008-01-25 21:08:34 +0100351static ssize_t lstats_write(struct file *file, const char __user *buf,
352 size_t count, loff_t *offs)
353{
Al Viro496ad9a2013-01-23 17:07:38 -0500354 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100355
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800356 if (!task)
357 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100358 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800359 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100360
361 return count;
362}
363
364static const struct file_operations proc_lstats_operations = {
365 .open = lstats_open,
366 .read = seq_read,
367 .write = lstats_write,
368 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800369 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100370};
371
372#endif
373
Al Viro8d8b97b2013-04-19 23:11:24 -0400374#ifdef CONFIG_CGROUPS
375static int cgroup_open(struct inode *inode, struct file *file)
376{
377 struct pid *pid = PROC_I(inode)->pid;
378 return single_open(file, proc_cgroup_show, pid);
379}
380
381static const struct file_operations proc_cgroup_operations = {
382 .open = cgroup_open,
383 .read = seq_read,
384 .llseek = seq_lseek,
385 .release = single_release,
386};
387#endif
388
389#ifdef CONFIG_PROC_PID_CPUSET
390
391static int cpuset_open(struct inode *inode, struct file *file)
392{
393 struct pid *pid = PROC_I(inode)->pid;
394 return single_open(file, proc_cpuset_show, pid);
395}
396
397static const struct file_operations proc_cpuset_operations = {
398 .open = cpuset_open,
399 .read = seq_read,
400 .llseek = seq_lseek,
401 .release = single_release,
402};
403#endif
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static int proc_oom_score(struct task_struct *task, char *buffer)
406{
David Rientjesa7f638f2012-05-29 15:06:47 -0700407 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200408 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700410 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200411 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700412 points = oom_badness(task, NULL, NULL, totalpages) *
413 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700414 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return sprintf(buffer, "%lu\n", points);
416}
417
Neil Hormand85f50d2007-10-18 23:40:37 -0700418struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700419 const char *name;
420 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700421};
422
423static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700424 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700425 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
426 [RLIMIT_DATA] = {"Max data size", "bytes"},
427 [RLIMIT_STACK] = {"Max stack size", "bytes"},
428 [RLIMIT_CORE] = {"Max core file size", "bytes"},
429 [RLIMIT_RSS] = {"Max resident set", "bytes"},
430 [RLIMIT_NPROC] = {"Max processes", "processes"},
431 [RLIMIT_NOFILE] = {"Max open files", "files"},
432 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
433 [RLIMIT_AS] = {"Max address space", "bytes"},
434 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
435 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
436 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
437 [RLIMIT_NICE] = {"Max nice priority", NULL},
438 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800439 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700440};
441
442/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700443static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
444 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700445{
446 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700447 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700448
449 struct rlimit rlim[RLIM_NLIMITS];
450
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400451 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700452 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700453 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
454 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700455
456 /*
457 * print the file header
458 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700459 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Neil Hormand85f50d2007-10-18 23:40:37 -0700460 "Limit", "Soft Limit", "Hard Limit", "Units");
461
462 for (i = 0; i < RLIM_NLIMITS; i++) {
463 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700464 seq_printf(m, "%-25s %-20s ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700465 lnames[i].name, "unlimited");
466 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700467 seq_printf(m, "%-25s %-20lu ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700468 lnames[i].name, rlim[i].rlim_cur);
469
470 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700471 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700472 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700473 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700474
475 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700476 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700477 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700478 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700479 }
480
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700481 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700482}
483
Roland McGrathebcb6732008-07-25 19:46:00 -0700484#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700485static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
486 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700487{
488 long nr;
489 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400490 int res = lock_trace(task);
491 if (res)
492 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700493
494 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700495 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400496 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700497 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400498 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700499 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700500 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
501 nr,
502 args[0], args[1], args[2], args[3], args[4], args[5],
503 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400504 unlock_trace(task);
505 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700506}
507#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/************************************************************************/
510/* Here the fs part begins */
511/************************************************************************/
512
513/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700514static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700516 struct task_struct *task;
517 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700518 /* Allow access to a task's file descriptors if it is us or we
519 * may use ptrace attach to the process and find out that
520 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700521 */
522 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700523 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400524 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700525 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700526 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700527 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800530int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700531{
532 int error;
533 struct inode *inode = dentry->d_inode;
534
535 if (attr->ia_valid & ATTR_MODE)
536 return -EPERM;
537
538 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200539 if (error)
540 return error;
541
Christoph Hellwig10257742010-06-04 11:30:02 +0200542 setattr_copy(inode, attr);
543 mark_inode_dirty(inode);
544 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700545}
546
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800547/*
548 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
549 * or euid/egid (for hide_pid_min=2)?
550 */
551static bool has_pid_permissions(struct pid_namespace *pid,
552 struct task_struct *task,
553 int hide_pid_min)
554{
555 if (pid->hide_pid < hide_pid_min)
556 return true;
557 if (in_group_p(pid->pid_gid))
558 return true;
559 return ptrace_may_access(task, PTRACE_MODE_READ);
560}
561
562
563static int proc_pid_permission(struct inode *inode, int mask)
564{
565 struct pid_namespace *pid = inode->i_sb->s_fs_info;
566 struct task_struct *task;
567 bool has_perms;
568
569 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800570 if (!task)
571 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800572 has_perms = has_pid_permissions(pid, task, 1);
573 put_task_struct(task);
574
575 if (!has_perms) {
576 if (pid->hide_pid == 2) {
577 /*
578 * Let's make getdents(), stat(), and open()
579 * consistent with each other. If a process
580 * may not stat() a file, it shouldn't be seen
581 * in procfs at all.
582 */
583 return -ENOENT;
584 }
585
586 return -EPERM;
587 }
588 return generic_permission(inode, mask);
589}
590
591
592
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800593static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700594 .setattr = proc_setattr,
595};
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
598
599static ssize_t proc_info_read(struct file * file, char __user * buf,
600 size_t count, loff_t *ppos)
601{
Al Viro496ad9a2013-01-23 17:07:38 -0500602 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unsigned long page;
604 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700605 struct task_struct *task = get_proc_task(inode);
606
607 length = -ESRCH;
608 if (!task)
609 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (count > PROC_BLOCK_SIZE)
612 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700613
614 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700615 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700616 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 length = PROC_I(inode)->op.proc_read(task, (char*)page);
619
620 if (length >= 0)
621 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
622 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700623out:
624 put_task_struct(task);
625out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return length;
627}
628
Arjan van de Ven00977a52007-02-12 00:55:34 -0800629static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100631 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632};
633
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800634static int proc_single_show(struct seq_file *m, void *v)
635{
636 struct inode *inode = m->private;
637 struct pid_namespace *ns;
638 struct pid *pid;
639 struct task_struct *task;
640 int ret;
641
642 ns = inode->i_sb->s_fs_info;
643 pid = proc_pid(inode);
644 task = get_pid_task(pid, PIDTYPE_PID);
645 if (!task)
646 return -ESRCH;
647
648 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
649
650 put_task_struct(task);
651 return ret;
652}
653
654static int proc_single_open(struct inode *inode, struct file *filp)
655{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800656 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800657}
658
659static const struct file_operations proc_single_file_operations = {
660 .open = proc_single_open,
661 .read = seq_read,
662 .llseek = seq_lseek,
663 .release = single_release,
664};
665
Cong Wangb409e572012-05-31 16:26:17 -0700666static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Al Viro496ad9a2013-01-23 17:07:38 -0500668 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800669 struct mm_struct *mm;
670
671 if (!task)
672 return -ESRCH;
673
Cong Wangb409e572012-05-31 16:26:17 -0700674 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800675 put_task_struct(task);
676
677 if (IS_ERR(mm))
678 return PTR_ERR(mm);
679
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100680 if (mm) {
681 /* ensure this mm_struct can't be freed */
682 atomic_inc(&mm->mm_count);
683 /* but do not pin its memory */
684 mmput(mm);
685 }
686
Linus Torvaldse2683372012-01-17 15:21:19 -0800687 file->private_data = mm;
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
691
Cong Wangb409e572012-05-31 16:26:17 -0700692static int mem_open(struct inode *inode, struct file *file)
693{
Djalal Harounibc452b42012-07-30 14:42:28 -0700694 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
695
696 /* OK to pass negative loff_t, we can catch out-of-range */
697 file->f_mode |= FMODE_UNSIGNED_OFFSET;
698
699 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700700}
701
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100702static ssize_t mem_rw(struct file *file, char __user *buf,
703 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Linus Torvaldse2683372012-01-17 15:21:19 -0800705 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100706 unsigned long addr = *ppos;
707 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvaldse2683372012-01-17 15:21:19 -0800710 if (!mm)
711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Mel Gormane12ba742007-10-16 01:25:52 -0700713 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800715 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700717 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100718 if (!atomic_inc_not_zero(&mm->mm_users))
719 goto free;
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100722 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100724 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 copied = -EFAULT;
726 break;
727 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100728
729 this_len = access_remote_vm(mm, addr, page, this_len, write);
730 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!copied)
732 copied = -EIO;
733 break;
734 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100735
736 if (!write && copy_to_user(buf, page, this_len)) {
737 copied = -EFAULT;
738 break;
739 }
740
741 buf += this_len;
742 addr += this_len;
743 copied += this_len;
744 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100746 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700747
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100748 mmput(mm);
749free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700750 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return copied;
752}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100754static ssize_t mem_read(struct file *file, char __user *buf,
755 size_t count, loff_t *ppos)
756{
757 return mem_rw(file, buf, count, ppos, 0);
758}
759
760static ssize_t mem_write(struct file *file, const char __user *buf,
761 size_t count, loff_t *ppos)
762{
763 return mem_rw(file, (char __user*)buf, count, ppos, 1);
764}
765
Matt Mackall85863e42008-02-04 22:29:04 -0800766loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 switch (orig) {
769 case 0:
770 file->f_pos = offset;
771 break;
772 case 1:
773 file->f_pos += offset;
774 break;
775 default:
776 return -EINVAL;
777 }
778 force_successful_syscall_return();
779 return file->f_pos;
780}
781
Linus Torvaldse2683372012-01-17 15:21:19 -0800782static int mem_release(struct inode *inode, struct file *file)
783{
784 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100785 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100786 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800787 return 0;
788}
789
Arjan van de Ven00977a52007-02-12 00:55:34 -0800790static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 .llseek = mem_lseek,
792 .read = mem_read,
793 .write = mem_write,
794 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800795 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796};
797
Cong Wangb409e572012-05-31 16:26:17 -0700798static int environ_open(struct inode *inode, struct file *file)
799{
800 return __mem_open(inode, file, PTRACE_MODE_READ);
801}
802
James Pearson315e28c2007-10-16 23:30:17 -0700803static ssize_t environ_read(struct file *file, char __user *buf,
804 size_t count, loff_t *ppos)
805{
James Pearson315e28c2007-10-16 23:30:17 -0700806 char *page;
807 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700808 int ret = 0;
809 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700810
Cong Wangb409e572012-05-31 16:26:17 -0700811 if (!mm)
812 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700813
James Pearson315e28c2007-10-16 23:30:17 -0700814 page = (char *)__get_free_page(GFP_TEMPORARY);
815 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700816 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700817
Al Virod6f64b82011-02-15 22:26:01 -0500818 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700819 if (!atomic_inc_not_zero(&mm->mm_users))
820 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700821 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700822 size_t this_len, max_len;
823 int retval;
824
825 if (src >= (mm->env_end - mm->env_start))
826 break;
James Pearson315e28c2007-10-16 23:30:17 -0700827
828 this_len = mm->env_end - (mm->env_start + src);
829
Djalal Harounie8905ec2012-07-30 14:42:26 -0700830 max_len = min_t(size_t, PAGE_SIZE, count);
831 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700832
Cong Wangb409e572012-05-31 16:26:17 -0700833 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700834 page, this_len, 0);
835
836 if (retval <= 0) {
837 ret = retval;
838 break;
839 }
840
841 if (copy_to_user(buf, page, retval)) {
842 ret = -EFAULT;
843 break;
844 }
845
846 ret += retval;
847 src += retval;
848 buf += retval;
849 count -= retval;
850 }
851 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700852 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700853
854free:
James Pearson315e28c2007-10-16 23:30:17 -0700855 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700856 return ret;
857}
858
859static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700860 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700861 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100862 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700863 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700864};
865
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800866static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
867 loff_t *ppos)
868{
Al Viro496ad9a2013-01-23 17:07:38 -0500869 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800870 char buffer[PROC_NUMBUF];
871 int oom_adj = OOM_ADJUST_MIN;
872 size_t len;
873 unsigned long flags;
874
875 if (!task)
876 return -ESRCH;
877 if (lock_task_sighand(task, &flags)) {
878 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
879 oom_adj = OOM_ADJUST_MAX;
880 else
881 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
882 OOM_SCORE_ADJ_MAX;
883 unlock_task_sighand(task, &flags);
884 }
885 put_task_struct(task);
886 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
887 return simple_read_from_buffer(buf, count, ppos, buffer, len);
888}
889
890static ssize_t oom_adj_write(struct file *file, const char __user *buf,
891 size_t count, loff_t *ppos)
892{
893 struct task_struct *task;
894 char buffer[PROC_NUMBUF];
895 int oom_adj;
896 unsigned long flags;
897 int err;
898
899 memset(buffer, 0, sizeof(buffer));
900 if (count > sizeof(buffer) - 1)
901 count = sizeof(buffer) - 1;
902 if (copy_from_user(buffer, buf, count)) {
903 err = -EFAULT;
904 goto out;
905 }
906
907 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
908 if (err)
909 goto out;
910 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
911 oom_adj != OOM_DISABLE) {
912 err = -EINVAL;
913 goto out;
914 }
915
Al Viro496ad9a2013-01-23 17:07:38 -0500916 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800917 if (!task) {
918 err = -ESRCH;
919 goto out;
920 }
921
922 task_lock(task);
923 if (!task->mm) {
924 err = -EINVAL;
925 goto err_task_lock;
926 }
927
928 if (!lock_task_sighand(task, &flags)) {
929 err = -ESRCH;
930 goto err_task_lock;
931 }
932
933 /*
934 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
935 * value is always attainable.
936 */
937 if (oom_adj == OOM_ADJUST_MAX)
938 oom_adj = OOM_SCORE_ADJ_MAX;
939 else
940 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
941
942 if (oom_adj < task->signal->oom_score_adj &&
943 !capable(CAP_SYS_RESOURCE)) {
944 err = -EACCES;
945 goto err_sighand;
946 }
947
948 /*
949 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
950 * /proc/pid/oom_score_adj instead.
951 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800952 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 -0800953 current->comm, task_pid_nr(current), task_pid_nr(task),
954 task_pid_nr(task));
955
956 task->signal->oom_score_adj = oom_adj;
957 trace_oom_score_adj_update(task);
958err_sighand:
959 unlock_task_sighand(task, &flags);
960err_task_lock:
961 task_unlock(task);
962 put_task_struct(task);
963out:
964 return err < 0 ? err : count;
965}
966
967static const struct file_operations proc_oom_adj_operations = {
968 .read = oom_adj_read,
969 .write = oom_adj_write,
970 .llseek = generic_file_llseek,
971};
972
David Rientjesa63d83f2010-08-09 17:19:46 -0700973static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
974 size_t count, loff_t *ppos)
975{
Al Viro496ad9a2013-01-23 17:07:38 -0500976 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700977 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800978 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700979 unsigned long flags;
980 size_t len;
981
982 if (!task)
983 return -ESRCH;
984 if (lock_task_sighand(task, &flags)) {
985 oom_score_adj = task->signal->oom_score_adj;
986 unlock_task_sighand(task, &flags);
987 }
988 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800989 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700990 return simple_read_from_buffer(buf, count, ppos, buffer, len);
991}
992
993static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
994 size_t count, loff_t *ppos)
995{
996 struct task_struct *task;
997 char buffer[PROC_NUMBUF];
998 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700999 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001000 int err;
1001
1002 memset(buffer, 0, sizeof(buffer));
1003 if (count > sizeof(buffer) - 1)
1004 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001005 if (copy_from_user(buffer, buf, count)) {
1006 err = -EFAULT;
1007 goto out;
1008 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001009
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001010 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001011 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001012 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001013 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001014 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1015 err = -EINVAL;
1016 goto out;
1017 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001018
Al Viro496ad9a2013-01-23 17:07:38 -05001019 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001020 if (!task) {
1021 err = -ESRCH;
1022 goto out;
1023 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001024
Ying Han3d5992d2010-10-26 14:21:23 -07001025 task_lock(task);
1026 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001027 err = -EINVAL;
1028 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001029 }
David Rientjesd19d5472010-10-26 14:21:26 -07001030
1031 if (!lock_task_sighand(task, &flags)) {
1032 err = -ESRCH;
1033 goto err_task_lock;
1034 }
1035
David Rientjesa9c58b902012-12-11 16:02:54 -08001036 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001037 !capable(CAP_SYS_RESOURCE)) {
1038 err = -EACCES;
1039 goto err_sighand;
1040 }
1041
David Rientjesa9c58b902012-12-11 16:02:54 -08001042 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f632011-01-13 15:46:05 -08001043 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001044 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001045 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001046
David Rientjes723548b2010-10-26 14:21:25 -07001047err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001048 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001049err_task_lock:
1050 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001051 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001052out:
1053 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001054}
1055
1056static const struct file_operations proc_oom_score_adj_operations = {
1057 .read = oom_score_adj_read,
1058 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001059 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001060};
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#ifdef CONFIG_AUDITSYSCALL
1063#define TMPBUFLEN 21
1064static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1065 size_t count, loff_t *ppos)
1066{
Al Viro496ad9a2013-01-23 17:07:38 -05001067 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001068 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ssize_t length;
1070 char tmpbuf[TMPBUFLEN];
1071
Eric W. Biederman99f89552006-06-26 00:25:55 -07001072 if (!task)
1073 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001075 from_kuid(file->f_cred->user_ns,
1076 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001077 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1079}
1080
1081static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1082 size_t count, loff_t *ppos)
1083{
Al Viro496ad9a2013-01-23 17:07:38 -05001084 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 char *page, *tmp;
1086 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001088 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001090 rcu_read_lock();
1091 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1092 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001094 }
1095 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Al Viroe0182902006-05-18 08:28:02 -04001097 if (count >= PAGE_SIZE)
1098 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (*ppos != 0) {
1101 /* No partial writes. */
1102 return -EINVAL;
1103 }
Mel Gormane12ba742007-10-16 01:25:52 -07001104 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (!page)
1106 return -ENOMEM;
1107 length = -EFAULT;
1108 if (copy_from_user(page, buf, count))
1109 goto out_free_page;
1110
Al Viroe0182902006-05-18 08:28:02 -04001111 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 loginuid = simple_strtoul(page, &tmp, 10);
1113 if (tmp == page) {
1114 length = -EINVAL;
1115 goto out_free_page;
1116
1117 }
Eric Paris81407c82013-05-24 09:49:14 -04001118
1119 /* is userspace tring to explicitly UNSET the loginuid? */
1120 if (loginuid == AUDIT_UID_UNSET) {
1121 kloginuid = INVALID_UID;
1122 } else {
1123 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1124 if (!uid_valid(kloginuid)) {
1125 length = -EINVAL;
1126 goto out_free_page;
1127 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001128 }
1129
1130 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (likely(length == 0))
1132 length = count;
1133
1134out_free_page:
1135 free_page((unsigned long) page);
1136 return length;
1137}
1138
Arjan van de Ven00977a52007-02-12 00:55:34 -08001139static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 .read = proc_loginuid_read,
1141 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001142 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143};
Eric Paris1e0bd752008-03-13 08:15:31 -04001144
1145static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1146 size_t count, loff_t *ppos)
1147{
Al Viro496ad9a2013-01-23 17:07:38 -05001148 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001149 struct task_struct *task = get_proc_task(inode);
1150 ssize_t length;
1151 char tmpbuf[TMPBUFLEN];
1152
1153 if (!task)
1154 return -ESRCH;
1155 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1156 audit_get_sessionid(task));
1157 put_task_struct(task);
1158 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1159}
1160
1161static const struct file_operations proc_sessionid_operations = {
1162 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001163 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001164};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165#endif
1166
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001167#ifdef CONFIG_FAULT_INJECTION
1168static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1169 size_t count, loff_t *ppos)
1170{
Al Viro496ad9a2013-01-23 17:07:38 -05001171 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001172 char buffer[PROC_NUMBUF];
1173 size_t len;
1174 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001175
1176 if (!task)
1177 return -ESRCH;
1178 make_it_fail = task->make_it_fail;
1179 put_task_struct(task);
1180
1181 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001182
1183 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001184}
1185
1186static ssize_t proc_fault_inject_write(struct file * file,
1187 const char __user * buf, size_t count, loff_t *ppos)
1188{
1189 struct task_struct *task;
1190 char buffer[PROC_NUMBUF], *end;
1191 int make_it_fail;
1192
1193 if (!capable(CAP_SYS_RESOURCE))
1194 return -EPERM;
1195 memset(buffer, 0, sizeof(buffer));
1196 if (count > sizeof(buffer) - 1)
1197 count = sizeof(buffer) - 1;
1198 if (copy_from_user(buffer, buf, count))
1199 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001200 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1201 if (*end)
1202 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001203 if (make_it_fail < 0 || make_it_fail > 1)
1204 return -EINVAL;
1205
Al Viro496ad9a2013-01-23 17:07:38 -05001206 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001207 if (!task)
1208 return -ESRCH;
1209 task->make_it_fail = make_it_fail;
1210 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001211
1212 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001213}
1214
Arjan van de Ven00977a52007-02-12 00:55:34 -08001215static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001216 .read = proc_fault_inject_read,
1217 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001218 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001219};
1220#endif
1221
Arjan van de Ven97455122008-01-25 21:08:34 +01001222
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001223#ifdef CONFIG_SCHED_DEBUG
1224/*
1225 * Print out various scheduling related per-task fields:
1226 */
1227static int sched_show(struct seq_file *m, void *v)
1228{
1229 struct inode *inode = m->private;
1230 struct task_struct *p;
1231
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001232 p = get_proc_task(inode);
1233 if (!p)
1234 return -ESRCH;
1235 proc_sched_show_task(p, m);
1236
1237 put_task_struct(p);
1238
1239 return 0;
1240}
1241
1242static ssize_t
1243sched_write(struct file *file, const char __user *buf,
1244 size_t count, loff_t *offset)
1245{
Al Viro496ad9a2013-01-23 17:07:38 -05001246 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001247 struct task_struct *p;
1248
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001249 p = get_proc_task(inode);
1250 if (!p)
1251 return -ESRCH;
1252 proc_sched_set_task(p);
1253
1254 put_task_struct(p);
1255
1256 return count;
1257}
1258
1259static int sched_open(struct inode *inode, struct file *filp)
1260{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001261 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001262}
1263
1264static const struct file_operations proc_pid_sched_operations = {
1265 .open = sched_open,
1266 .read = seq_read,
1267 .write = sched_write,
1268 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001269 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001270};
1271
1272#endif
1273
Mike Galbraith5091faa2010-11-30 14:18:03 +01001274#ifdef CONFIG_SCHED_AUTOGROUP
1275/*
1276 * Print out autogroup related information:
1277 */
1278static int sched_autogroup_show(struct seq_file *m, void *v)
1279{
1280 struct inode *inode = m->private;
1281 struct task_struct *p;
1282
1283 p = get_proc_task(inode);
1284 if (!p)
1285 return -ESRCH;
1286 proc_sched_autogroup_show_task(p, m);
1287
1288 put_task_struct(p);
1289
1290 return 0;
1291}
1292
1293static ssize_t
1294sched_autogroup_write(struct file *file, const char __user *buf,
1295 size_t count, loff_t *offset)
1296{
Al Viro496ad9a2013-01-23 17:07:38 -05001297 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001298 struct task_struct *p;
1299 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001300 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001301 int err;
1302
1303 memset(buffer, 0, sizeof(buffer));
1304 if (count > sizeof(buffer) - 1)
1305 count = sizeof(buffer) - 1;
1306 if (copy_from_user(buffer, buf, count))
1307 return -EFAULT;
1308
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001309 err = kstrtoint(strstrip(buffer), 0, &nice);
1310 if (err < 0)
1311 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001312
1313 p = get_proc_task(inode);
1314 if (!p)
1315 return -ESRCH;
1316
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001317 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001318 if (err)
1319 count = err;
1320
1321 put_task_struct(p);
1322
1323 return count;
1324}
1325
1326static int sched_autogroup_open(struct inode *inode, struct file *filp)
1327{
1328 int ret;
1329
1330 ret = single_open(filp, sched_autogroup_show, NULL);
1331 if (!ret) {
1332 struct seq_file *m = filp->private_data;
1333
1334 m->private = inode;
1335 }
1336 return ret;
1337}
1338
1339static const struct file_operations proc_pid_sched_autogroup_operations = {
1340 .open = sched_autogroup_open,
1341 .read = seq_read,
1342 .write = sched_autogroup_write,
1343 .llseek = seq_lseek,
1344 .release = single_release,
1345};
1346
1347#endif /* CONFIG_SCHED_AUTOGROUP */
1348
john stultz4614a696b2009-12-14 18:00:05 -08001349static ssize_t comm_write(struct file *file, const char __user *buf,
1350 size_t count, loff_t *offset)
1351{
Al Viro496ad9a2013-01-23 17:07:38 -05001352 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001353 struct task_struct *p;
1354 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001355 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001356
1357 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001358 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001359 return -EFAULT;
1360
1361 p = get_proc_task(inode);
1362 if (!p)
1363 return -ESRCH;
1364
1365 if (same_thread_group(current, p))
1366 set_task_comm(p, buffer);
1367 else
1368 count = -EINVAL;
1369
1370 put_task_struct(p);
1371
1372 return count;
1373}
1374
1375static int comm_show(struct seq_file *m, void *v)
1376{
1377 struct inode *inode = m->private;
1378 struct task_struct *p;
1379
1380 p = get_proc_task(inode);
1381 if (!p)
1382 return -ESRCH;
1383
1384 task_lock(p);
1385 seq_printf(m, "%s\n", p->comm);
1386 task_unlock(p);
1387
1388 put_task_struct(p);
1389
1390 return 0;
1391}
1392
1393static int comm_open(struct inode *inode, struct file *filp)
1394{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001395 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001396}
1397
1398static const struct file_operations proc_pid_set_comm_operations = {
1399 .open = comm_open,
1400 .read = seq_read,
1401 .write = comm_write,
1402 .llseek = seq_lseek,
1403 .release = single_release,
1404};
1405
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001406static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001407{
1408 struct task_struct *task;
1409 struct mm_struct *mm;
1410 struct file *exe_file;
1411
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001412 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001413 if (!task)
1414 return -ENOENT;
1415 mm = get_task_mm(task);
1416 put_task_struct(task);
1417 if (!mm)
1418 return -ENOENT;
1419 exe_file = get_mm_exe_file(mm);
1420 mmput(mm);
1421 if (exe_file) {
1422 *exe_path = exe_file->f_path;
1423 path_get(&exe_file->f_path);
1424 fput(exe_file);
1425 return 0;
1426 } else
1427 return -ENOENT;
1428}
1429
Al Viro008b1502005-08-20 00:17:39 +01001430static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001433 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int error = -EACCES;
1435
Eric W. Biederman778c1142006-06-26 00:25:58 -07001436 /* Are we allowed to snoop on the tasks file descriptors? */
1437 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Christoph Hellwig408ef012012-06-18 10:47:03 -04001440 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1441 if (error)
1442 goto out;
1443
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001444 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001445 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446out:
Al Viro008b1502005-08-20 00:17:39 +01001447 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001450static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Mel Gormane12ba742007-10-16 01:25:52 -07001452 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001453 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int len;
1455
1456 if (!tmp)
1457 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001458
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001459 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001460 len = PTR_ERR(pathname);
1461 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001463 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 if (len > buflen)
1466 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001467 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 len = -EFAULT;
1469 out:
1470 free_page((unsigned long)tmp);
1471 return len;
1472}
1473
1474static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1475{
1476 int error = -EACCES;
1477 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001478 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Eric W. Biederman778c1142006-06-26 00:25:58 -07001480 /* Are we allowed to snoop on the tasks file descriptors? */
1481 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001484 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (error)
1486 goto out;
1487
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001488 error = do_proc_readlink(&path, buffer, buflen);
1489 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return error;
1492}
1493
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001494const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001496 .follow_link = proc_pid_follow_link,
1497 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498};
1499
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001500
1501/* building an inode */
1502
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001503struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001504{
1505 struct inode * inode;
1506 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001507 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001508
1509 /* We need a new inode */
1510
1511 inode = new_inode(sb);
1512 if (!inode)
1513 goto out;
1514
1515 /* Common stuff */
1516 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001517 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001518 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001519 inode->i_op = &proc_def_inode_operations;
1520
1521 /*
1522 * grab the reference to task.
1523 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001524 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001525 if (!ei->pid)
1526 goto out_unlock;
1527
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001528 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001529 rcu_read_lock();
1530 cred = __task_cred(task);
1531 inode->i_uid = cred->euid;
1532 inode->i_gid = cred->egid;
1533 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001534 }
1535 security_task_to_inode(task, inode);
1536
1537out:
1538 return inode;
1539
1540out_unlock:
1541 iput(inode);
1542 return NULL;
1543}
1544
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001545int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001546{
1547 struct inode *inode = dentry->d_inode;
1548 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001549 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001550 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001551
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001552 generic_fillattr(inode, stat);
1553
1554 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001555 stat->uid = GLOBAL_ROOT_UID;
1556 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001557 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1558 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001559 if (!has_pid_permissions(pid, task, 2)) {
1560 rcu_read_unlock();
1561 /*
1562 * This doesn't prevent learning whether PID exists,
1563 * it only makes getattr() consistent with readdir().
1564 */
1565 return -ENOENT;
1566 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001567 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1568 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001569 cred = __task_cred(task);
1570 stat->uid = cred->euid;
1571 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001572 }
1573 }
1574 rcu_read_unlock();
1575 return 0;
1576}
1577
1578/* dentry stuff */
1579
1580/*
1581 * Exceptional case: normally we are not allowed to unhash a busy
1582 * directory. In this case, however, we can do it - no aliasing problems
1583 * due to the way we treat inodes.
1584 *
1585 * Rewrite the inode's ownerships here because the owning task may have
1586 * performed a setuid(), etc.
1587 *
1588 * Before the /proc/pid/status file was created the only way to read
1589 * the effective uid of a /process was to stat /proc/pid. Reading
1590 * /proc/pid/status is slow enough that procps and other packages
1591 * kept stating /proc/pid. To keep the rules in /proc simple I have
1592 * made this apply to all per process world readable and executable
1593 * directories.
1594 */
Al Viro0b728e12012-06-10 16:03:43 -04001595int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001596{
Nick Piggin34286d62011-01-07 17:49:57 +11001597 struct inode *inode;
1598 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001599 const struct cred *cred;
1600
Al Viro0b728e12012-06-10 16:03:43 -04001601 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001602 return -ECHILD;
1603
1604 inode = dentry->d_inode;
1605 task = get_proc_task(inode);
1606
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001607 if (task) {
1608 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1609 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001610 rcu_read_lock();
1611 cred = __task_cred(task);
1612 inode->i_uid = cred->euid;
1613 inode->i_gid = cred->egid;
1614 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001615 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001616 inode->i_uid = GLOBAL_ROOT_UID;
1617 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001618 }
1619 inode->i_mode &= ~(S_ISUID | S_ISGID);
1620 security_task_to_inode(task, inode);
1621 put_task_struct(task);
1622 return 1;
1623 }
1624 d_drop(dentry);
1625 return 0;
1626}
1627
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001628static inline bool proc_inode_is_dead(struct inode *inode)
1629{
1630 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1631}
1632
David Howells1dd704b2013-04-12 01:08:50 +01001633int pid_delete_dentry(const struct dentry *dentry)
1634{
1635 /* Is the task we represent dead?
1636 * If so, then don't put the dentry on the lru list,
1637 * kill it immediately.
1638 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001639 return proc_inode_is_dead(dentry->d_inode);
David Howells1dd704b2013-04-12 01:08:50 +01001640}
1641
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001642const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001643{
1644 .d_revalidate = pid_revalidate,
1645 .d_delete = pid_delete_dentry,
1646};
1647
1648/* Lookups */
1649
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001650/*
1651 * Fill a directory entry.
1652 *
1653 * If possible create the dcache entry and derive our inode number and
1654 * file type from dcache entry.
1655 *
1656 * Since all of the proc inode numbers are dynamically generated, the inode
1657 * numbers do not exist until the inode is cache. This means creating the
1658 * the dcache entry in readdir is necessary to keep the inode numbers
1659 * reported by readdir in sync with the inode numbers reported
1660 * by stat.
1661 */
Al Virof0c3b502013-05-16 12:07:31 -04001662bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001663 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001664 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001665{
Al Virof0c3b502013-05-16 12:07:31 -04001666 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001667 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001668 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001669 unsigned type;
1670 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001671
Al Viro1df98b82013-06-15 11:33:10 +04001672 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001673 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001674 child = d_alloc(dir, &qname);
1675 if (!child)
1676 goto end_instantiate;
1677 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1678 dput(child);
1679 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001680 }
1681 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001682 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001683 ino = inode->i_ino;
1684 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001685 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001686 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001687
1688end_instantiate:
1689 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001690}
1691
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001692#ifdef CONFIG_CHECKPOINT_RESTORE
1693
1694/*
1695 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1696 * which represent vma start and end addresses.
1697 */
1698static int dname_to_vma_addr(struct dentry *dentry,
1699 unsigned long *start, unsigned long *end)
1700{
1701 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1702 return -EINVAL;
1703
1704 return 0;
1705}
1706
Al Viro0b728e12012-06-10 16:03:43 -04001707static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001708{
1709 unsigned long vm_start, vm_end;
1710 bool exact_vma_exists = false;
1711 struct mm_struct *mm = NULL;
1712 struct task_struct *task;
1713 const struct cred *cred;
1714 struct inode *inode;
1715 int status = 0;
1716
Al Viro0b728e12012-06-10 16:03:43 -04001717 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001718 return -ECHILD;
1719
1720 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001721 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001722 goto out_notask;
1723 }
1724
1725 inode = dentry->d_inode;
1726 task = get_proc_task(inode);
1727 if (!task)
1728 goto out_notask;
1729
Cong Wang2344bec2012-05-31 16:26:18 -07001730 mm = mm_access(task, PTRACE_MODE_READ);
1731 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001732 goto out;
1733
1734 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1735 down_read(&mm->mmap_sem);
1736 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1737 up_read(&mm->mmap_sem);
1738 }
1739
1740 mmput(mm);
1741
1742 if (exact_vma_exists) {
1743 if (task_dumpable(task)) {
1744 rcu_read_lock();
1745 cred = __task_cred(task);
1746 inode->i_uid = cred->euid;
1747 inode->i_gid = cred->egid;
1748 rcu_read_unlock();
1749 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001750 inode->i_uid = GLOBAL_ROOT_UID;
1751 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001752 }
1753 security_task_to_inode(task, inode);
1754 status = 1;
1755 }
1756
1757out:
1758 put_task_struct(task);
1759
1760out_notask:
1761 if (status <= 0)
1762 d_drop(dentry);
1763
1764 return status;
1765}
1766
1767static const struct dentry_operations tid_map_files_dentry_operations = {
1768 .d_revalidate = map_files_d_revalidate,
1769 .d_delete = pid_delete_dentry,
1770};
1771
1772static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1773{
1774 unsigned long vm_start, vm_end;
1775 struct vm_area_struct *vma;
1776 struct task_struct *task;
1777 struct mm_struct *mm;
1778 int rc;
1779
1780 rc = -ENOENT;
1781 task = get_proc_task(dentry->d_inode);
1782 if (!task)
1783 goto out;
1784
1785 mm = get_task_mm(task);
1786 put_task_struct(task);
1787 if (!mm)
1788 goto out;
1789
1790 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1791 if (rc)
1792 goto out_mmput;
1793
Artem Fetishev70335ab2014-03-10 15:49:45 -07001794 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001795 down_read(&mm->mmap_sem);
1796 vma = find_exact_vma(mm, vm_start, vm_end);
1797 if (vma && vma->vm_file) {
1798 *path = vma->vm_file->f_path;
1799 path_get(path);
1800 rc = 0;
1801 }
1802 up_read(&mm->mmap_sem);
1803
1804out_mmput:
1805 mmput(mm);
1806out:
1807 return rc;
1808}
1809
1810struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001811 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001812 unsigned long len;
1813 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1814};
1815
Al Viroc52a47a2013-06-15 11:15:20 +04001816static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001817proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1818 struct task_struct *task, const void *ptr)
1819{
Al Viro7b540d02012-08-27 14:55:26 -04001820 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001821 struct proc_inode *ei;
1822 struct inode *inode;
1823
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001824 inode = proc_pid_make_inode(dir->i_sb, task);
1825 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001826 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001827
1828 ei = PROC_I(inode);
1829 ei->op.proc_get_link = proc_map_files_get_link;
1830
1831 inode->i_op = &proc_pid_link_inode_operations;
1832 inode->i_size = 64;
1833 inode->i_mode = S_IFLNK;
1834
Al Viro7b540d02012-08-27 14:55:26 -04001835 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001836 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001837 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001838 inode->i_mode |= S_IWUSR;
1839
1840 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1841 d_add(dentry, inode);
1842
Al Viroc52a47a2013-06-15 11:15:20 +04001843 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001844}
1845
1846static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001847 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001848{
1849 unsigned long vm_start, vm_end;
1850 struct vm_area_struct *vma;
1851 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001852 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001853 struct mm_struct *mm;
1854
Al Viroc52a47a2013-06-15 11:15:20 +04001855 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001856 if (!capable(CAP_SYS_ADMIN))
1857 goto out;
1858
Al Viroc52a47a2013-06-15 11:15:20 +04001859 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001860 task = get_proc_task(dir);
1861 if (!task)
1862 goto out;
1863
Al Viroc52a47a2013-06-15 11:15:20 +04001864 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001865 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001866 goto out_put_task;
1867
Al Viroc52a47a2013-06-15 11:15:20 +04001868 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001869 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001870 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001871
1872 mm = get_task_mm(task);
1873 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001874 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001875
1876 down_read(&mm->mmap_sem);
1877 vma = find_exact_vma(mm, vm_start, vm_end);
1878 if (!vma)
1879 goto out_no_vma;
1880
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001881 if (vma->vm_file)
1882 result = proc_map_files_instantiate(dir, dentry, task,
1883 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001884
1885out_no_vma:
1886 up_read(&mm->mmap_sem);
1887 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001888out_put_task:
1889 put_task_struct(task);
1890out:
Al Viroc52a47a2013-06-15 11:15:20 +04001891 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001892}
1893
1894static const struct inode_operations proc_map_files_inode_operations = {
1895 .lookup = proc_map_files_lookup,
1896 .permission = proc_fd_permission,
1897 .setattr = proc_setattr,
1898};
1899
1900static int
Al Virof0c3b502013-05-16 12:07:31 -04001901proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001902{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001903 struct vm_area_struct *vma;
1904 struct task_struct *task;
1905 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001906 unsigned long nr_files, pos, i;
1907 struct flex_array *fa = NULL;
1908 struct map_files_info info;
1909 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001910 int ret;
1911
Zhao Hongjiang41735812013-02-20 13:13:55 +11001912 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001913 if (!capable(CAP_SYS_ADMIN))
1914 goto out;
1915
1916 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001917 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001918 if (!task)
1919 goto out;
1920
1921 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001922 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001923 goto out_put_task;
1924
1925 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001926 if (!dir_emit_dots(file, ctx))
1927 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001928
Al Virof0c3b502013-05-16 12:07:31 -04001929 mm = get_task_mm(task);
1930 if (!mm)
1931 goto out_put_task;
1932 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001933
Al Virof0c3b502013-05-16 12:07:31 -04001934 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001935
Al Virof0c3b502013-05-16 12:07:31 -04001936 /*
1937 * We need two passes here:
1938 *
1939 * 1) Collect vmas of mapped files with mmap_sem taken
1940 * 2) Release mmap_sem and instantiate entries
1941 *
1942 * otherwise we get lockdep complained, since filldir()
1943 * routine might require mmap_sem taken in might_fault().
1944 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001945
Al Virof0c3b502013-05-16 12:07:31 -04001946 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1947 if (vma->vm_file && ++pos > ctx->pos)
1948 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001949 }
Al Virof0c3b502013-05-16 12:07:31 -04001950
1951 if (nr_files) {
1952 fa = flex_array_alloc(sizeof(info), nr_files,
1953 GFP_KERNEL);
1954 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1955 GFP_KERNEL)) {
1956 ret = -ENOMEM;
1957 if (fa)
1958 flex_array_free(fa);
1959 up_read(&mm->mmap_sem);
1960 mmput(mm);
1961 goto out_put_task;
1962 }
1963 for (i = 0, vma = mm->mmap, pos = 2; vma;
1964 vma = vma->vm_next) {
1965 if (!vma->vm_file)
1966 continue;
1967 if (++pos <= ctx->pos)
1968 continue;
1969
1970 info.mode = vma->vm_file->f_mode;
1971 info.len = snprintf(info.name,
1972 sizeof(info.name), "%lx-%lx",
1973 vma->vm_start, vma->vm_end);
1974 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1975 BUG();
1976 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001977 }
Al Virof0c3b502013-05-16 12:07:31 -04001978 up_read(&mm->mmap_sem);
1979
1980 for (i = 0; i < nr_files; i++) {
1981 p = flex_array_get(fa, i);
1982 if (!proc_fill_cache(file, ctx,
1983 p->name, p->len,
1984 proc_map_files_instantiate,
1985 task,
1986 (void *)(unsigned long)p->mode))
1987 break;
1988 ctx->pos++;
1989 }
1990 if (fa)
1991 flex_array_free(fa);
1992 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001993
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001994out_put_task:
1995 put_task_struct(task);
1996out:
1997 return ret;
1998}
1999
2000static const struct file_operations proc_map_files_operations = {
2001 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002002 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002003 .llseek = default_llseek,
2004};
2005
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002006struct timers_private {
2007 struct pid *pid;
2008 struct task_struct *task;
2009 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002010 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002011 unsigned long flags;
2012};
2013
2014static void *timers_start(struct seq_file *m, loff_t *pos)
2015{
2016 struct timers_private *tp = m->private;
2017
2018 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2019 if (!tp->task)
2020 return ERR_PTR(-ESRCH);
2021
2022 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2023 if (!tp->sighand)
2024 return ERR_PTR(-ESRCH);
2025
2026 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2027}
2028
2029static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2030{
2031 struct timers_private *tp = m->private;
2032 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2033}
2034
2035static void timers_stop(struct seq_file *m, void *v)
2036{
2037 struct timers_private *tp = m->private;
2038
2039 if (tp->sighand) {
2040 unlock_task_sighand(tp->task, &tp->flags);
2041 tp->sighand = NULL;
2042 }
2043
2044 if (tp->task) {
2045 put_task_struct(tp->task);
2046 tp->task = NULL;
2047 }
2048}
2049
2050static int show_timer(struct seq_file *m, void *v)
2051{
2052 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002053 struct timers_private *tp = m->private;
2054 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002055 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002056 [SIGEV_SIGNAL] = "signal",
2057 [SIGEV_NONE] = "none",
2058 [SIGEV_THREAD] = "thread",
2059 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002060
2061 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002062 notify = timer->it_sigev_notify;
2063
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002064 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002065 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2066 timer->sigq->info.si_value.sival_ptr);
2067 seq_printf(m, "notify: %s/%s.%d\n",
2068 nstr[notify & ~SIGEV_THREAD_ID],
2069 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2070 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002071 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002072
2073 return 0;
2074}
2075
2076static const struct seq_operations proc_timers_seq_ops = {
2077 .start = timers_start,
2078 .next = timers_next,
2079 .stop = timers_stop,
2080 .show = show_timer,
2081};
2082
2083static int proc_timers_open(struct inode *inode, struct file *file)
2084{
2085 struct timers_private *tp;
2086
2087 tp = __seq_open_private(file, &proc_timers_seq_ops,
2088 sizeof(struct timers_private));
2089 if (!tp)
2090 return -ENOMEM;
2091
2092 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002093 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002094 return 0;
2095}
2096
2097static const struct file_operations proc_timers_operations = {
2098 .open = proc_timers_open,
2099 .read = seq_read,
2100 .llseek = seq_lseek,
2101 .release = seq_release_private,
2102};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002103#endif /* CONFIG_CHECKPOINT_RESTORE */
2104
Al Viroc52a47a2013-06-15 11:15:20 +04002105static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002106 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002107{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002108 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002109 struct inode *inode;
2110 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002111
Eric W. Biederman61a28782006-10-02 02:18:49 -07002112 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002113 if (!inode)
2114 goto out;
2115
2116 ei = PROC_I(inode);
2117 inode->i_mode = p->mode;
2118 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002119 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002120 if (p->iop)
2121 inode->i_op = p->iop;
2122 if (p->fop)
2123 inode->i_fop = p->fop;
2124 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002125 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002126 d_add(dentry, inode);
2127 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002128 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002129 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002130out:
Al Viroc52a47a2013-06-15 11:15:20 +04002131 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002132}
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134static struct dentry *proc_pident_lookup(struct inode *dir,
2135 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002136 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002137 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Al Viroc52a47a2013-06-15 11:15:20 +04002139 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002140 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002141 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Al Viroc52a47a2013-06-15 11:15:20 +04002143 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Eric W. Biederman99f89552006-06-26 00:25:55 -07002145 if (!task)
2146 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002148 /*
2149 * Yes, it does not scale. And it should not. Don't add
2150 * new entries into /proc/<tgid>/ without very good reasons.
2151 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002152 last = &ents[nents - 1];
2153 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (p->len != dentry->d_name.len)
2155 continue;
2156 if (!memcmp(dentry->d_name.name, p->name, p->len))
2157 break;
2158 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002159 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto out;
2161
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002162 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002164 put_task_struct(task);
2165out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002166 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
Al Virof0c3b502013-05-16 12:07:31 -04002169static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002170 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002171{
Al Virof0c3b502013-05-16 12:07:31 -04002172 struct task_struct *task = get_proc_task(file_inode(file));
2173 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002174
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002175 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002176 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002177
Al Virof0c3b502013-05-16 12:07:31 -04002178 if (!dir_emit_dots(file, ctx))
2179 goto out;
2180
2181 if (ctx->pos >= nents + 2)
2182 goto out;
2183
2184 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2185 if (!proc_fill_cache(file, ctx, p->name, p->len,
2186 proc_pident_instantiate, task, p))
2187 break;
2188 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002189 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002190out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002191 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002196static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2197 size_t count, loff_t *ppos)
2198{
Al Viro496ad9a2013-01-23 17:07:38 -05002199 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002200 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002201 ssize_t length;
2202 struct task_struct *task = get_proc_task(inode);
2203
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002204 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002205 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002206
2207 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002208 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002209 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002210 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002211 if (length > 0)
2212 length = simple_read_from_buffer(buf, count, ppos, p, length);
2213 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002214 return length;
2215}
2216
2217static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2218 size_t count, loff_t *ppos)
2219{
Al Viro496ad9a2013-01-23 17:07:38 -05002220 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002221 char *page;
2222 ssize_t length;
2223 struct task_struct *task = get_proc_task(inode);
2224
2225 length = -ESRCH;
2226 if (!task)
2227 goto out_no_task;
2228 if (count > PAGE_SIZE)
2229 count = PAGE_SIZE;
2230
2231 /* No partial writes. */
2232 length = -EINVAL;
2233 if (*ppos != 0)
2234 goto out;
2235
2236 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002237 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002238 if (!page)
2239 goto out;
2240
2241 length = -EFAULT;
2242 if (copy_from_user(page, buf, count))
2243 goto out_free;
2244
David Howells107db7c2009-05-08 13:55:27 +01002245 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002246 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002247 if (length < 0)
2248 goto out_free;
2249
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002250 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002251 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002252 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002253 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002254out_free:
2255 free_page((unsigned long) page);
2256out:
2257 put_task_struct(task);
2258out_no_task:
2259 return length;
2260}
2261
Arjan van de Ven00977a52007-02-12 00:55:34 -08002262static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002263 .read = proc_pid_attr_read,
2264 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002265 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002266};
2267
Eric Dumazetc5141e62007-05-08 00:26:15 -07002268static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002269 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2270 REG("prev", S_IRUGO, proc_pid_attr_operations),
2271 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2272 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2273 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2274 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002275};
2276
Al Virof0c3b502013-05-16 12:07:31 -04002277static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Al Virof0c3b502013-05-16 12:07:31 -04002279 return proc_pident_readdir(file, ctx,
2280 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Arjan van de Ven00977a52007-02-12 00:55:34 -08002283static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002285 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002286 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287};
2288
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002289static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002290 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002292 return proc_pident_lookup(dir, dentry,
2293 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002296static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002297 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002298 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002299 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300};
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302#endif
2303
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002304#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002305static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2306 size_t count, loff_t *ppos)
2307{
Al Viro496ad9a2013-01-23 17:07:38 -05002308 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002309 struct mm_struct *mm;
2310 char buffer[PROC_NUMBUF];
2311 size_t len;
2312 int ret;
2313
2314 if (!task)
2315 return -ESRCH;
2316
2317 ret = 0;
2318 mm = get_task_mm(task);
2319 if (mm) {
2320 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2321 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2322 MMF_DUMP_FILTER_SHIFT));
2323 mmput(mm);
2324 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2325 }
2326
2327 put_task_struct(task);
2328
2329 return ret;
2330}
2331
2332static ssize_t proc_coredump_filter_write(struct file *file,
2333 const char __user *buf,
2334 size_t count,
2335 loff_t *ppos)
2336{
2337 struct task_struct *task;
2338 struct mm_struct *mm;
2339 char buffer[PROC_NUMBUF], *end;
2340 unsigned int val;
2341 int ret;
2342 int i;
2343 unsigned long mask;
2344
2345 ret = -EFAULT;
2346 memset(buffer, 0, sizeof(buffer));
2347 if (count > sizeof(buffer) - 1)
2348 count = sizeof(buffer) - 1;
2349 if (copy_from_user(buffer, buf, count))
2350 goto out_no_task;
2351
2352 ret = -EINVAL;
2353 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2354 if (*end == '\n')
2355 end++;
2356 if (end - buffer == 0)
2357 goto out_no_task;
2358
2359 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002360 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002361 if (!task)
2362 goto out_no_task;
2363
2364 ret = end - buffer;
2365 mm = get_task_mm(task);
2366 if (!mm)
2367 goto out_no_mm;
2368
2369 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2370 if (val & mask)
2371 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2372 else
2373 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2374 }
2375
2376 mmput(mm);
2377 out_no_mm:
2378 put_task_struct(task);
2379 out_no_task:
2380 return ret;
2381}
2382
2383static const struct file_operations proc_coredump_filter_operations = {
2384 .read = proc_coredump_filter_read,
2385 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002386 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002387};
2388#endif
2389
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002390#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002391static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002392{
Andrea Righi940389b2008-07-28 00:48:12 +02002393 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002394 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002395 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002396
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002397 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2398 if (result)
2399 return result;
2400
2401 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2402 result = -EACCES;
2403 goto out_unlock;
2404 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002405
Andrea Righi59954772008-07-27 17:29:15 +02002406 if (whole && lock_task_sighand(task, &flags)) {
2407 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002408
Andrea Righi59954772008-07-27 17:29:15 +02002409 task_io_accounting_add(&acct, &task->signal->ioac);
2410 while_each_thread(task, t)
2411 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002412
Andrea Righi59954772008-07-27 17:29:15 +02002413 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002414 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002415 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002416 "rchar: %llu\n"
2417 "wchar: %llu\n"
2418 "syscr: %llu\n"
2419 "syscw: %llu\n"
2420 "read_bytes: %llu\n"
2421 "write_bytes: %llu\n"
2422 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002423 (unsigned long long)acct.rchar,
2424 (unsigned long long)acct.wchar,
2425 (unsigned long long)acct.syscr,
2426 (unsigned long long)acct.syscw,
2427 (unsigned long long)acct.read_bytes,
2428 (unsigned long long)acct.write_bytes,
2429 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002430out_unlock:
2431 mutex_unlock(&task->signal->cred_guard_mutex);
2432 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002433}
Andrea Righi297c5d92008-07-25 01:48:49 -07002434
2435static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2436{
2437 return do_io_accounting(task, buffer, 0);
2438}
2439
2440static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2441{
2442 return do_io_accounting(task, buffer, 1);
2443}
2444#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002445
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002446#ifdef CONFIG_USER_NS
2447static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002448 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002449{
2450 struct user_namespace *ns = NULL;
2451 struct task_struct *task;
2452 struct seq_file *seq;
2453 int ret = -EINVAL;
2454
2455 task = get_proc_task(inode);
2456 if (task) {
2457 rcu_read_lock();
2458 ns = get_user_ns(task_cred_xxx(task, user_ns));
2459 rcu_read_unlock();
2460 put_task_struct(task);
2461 }
2462 if (!ns)
2463 goto err;
2464
2465 ret = seq_open(file, seq_ops);
2466 if (ret)
2467 goto err_put_ns;
2468
2469 seq = file->private_data;
2470 seq->private = ns;
2471
2472 return 0;
2473err_put_ns:
2474 put_user_ns(ns);
2475err:
2476 return ret;
2477}
2478
2479static int proc_id_map_release(struct inode *inode, struct file *file)
2480{
2481 struct seq_file *seq = file->private_data;
2482 struct user_namespace *ns = seq->private;
2483 put_user_ns(ns);
2484 return seq_release(inode, file);
2485}
2486
2487static int proc_uid_map_open(struct inode *inode, struct file *file)
2488{
2489 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2490}
2491
2492static int proc_gid_map_open(struct inode *inode, struct file *file)
2493{
2494 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2495}
2496
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002497static int proc_projid_map_open(struct inode *inode, struct file *file)
2498{
2499 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2500}
2501
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002502static const struct file_operations proc_uid_map_operations = {
2503 .open = proc_uid_map_open,
2504 .write = proc_uid_map_write,
2505 .read = seq_read,
2506 .llseek = seq_lseek,
2507 .release = proc_id_map_release,
2508};
2509
2510static const struct file_operations proc_gid_map_operations = {
2511 .open = proc_gid_map_open,
2512 .write = proc_gid_map_write,
2513 .read = seq_read,
2514 .llseek = seq_lseek,
2515 .release = proc_id_map_release,
2516};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002517
2518static const struct file_operations proc_projid_map_operations = {
2519 .open = proc_projid_map_open,
2520 .write = proc_projid_map_write,
2521 .read = seq_read,
2522 .llseek = seq_lseek,
2523 .release = proc_id_map_release,
2524};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002525#endif /* CONFIG_USER_NS */
2526
Kees Cook47830722008-10-06 03:11:58 +04002527static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2528 struct pid *pid, struct task_struct *task)
2529{
Al Viroa9712bc2011-03-23 15:52:50 -04002530 int err = lock_trace(task);
2531 if (!err) {
2532 seq_printf(m, "%08x\n", task->personality);
2533 unlock_trace(task);
2534 }
2535 return err;
Kees Cook47830722008-10-06 03:11:58 +04002536}
2537
Eric W. Biederman801199c2006-10-02 02:18:48 -07002538/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002539 * Thread groups
2540 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002541static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002542static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002543
Eric Dumazetc5141e62007-05-08 00:26:15 -07002544static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002545 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2546 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002547#ifdef CONFIG_CHECKPOINT_RESTORE
2548 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2549#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002550 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002551 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002552#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002553 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002554#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002555 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002556 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002557 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002558 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002559 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002560#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002561 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002562#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002563#ifdef CONFIG_SCHED_AUTOGROUP
2564 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2565#endif
john stultz4614a696b2009-12-14 18:00:05 -08002566 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002567#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002568 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002569#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002570 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2571 ONE("stat", S_IRUGO, proc_tgid_stat),
2572 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002573 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002574#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002575 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002576#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002577 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2578 LNK("cwd", proc_cwd_link),
2579 LNK("root", proc_root_link),
2580 LNK("exe", proc_exe_link),
2581 REG("mounts", S_IRUGO, proc_mounts_operations),
2582 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2583 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002584#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002585 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002586 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002587 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002588#endif
2589#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002590 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002591#endif
2592#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002593 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002594#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002595#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002596 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002597#endif
2598#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002599 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002600#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002601#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002602 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002603#endif
Paul Menage8793d852007-10-18 23:39:39 -07002604#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002605 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002606#endif
Paul Menagea4243162007-10-18 23:39:35 -07002607#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002608 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002609#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002610 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002611 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002612 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002613#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002614 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2615 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002616#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002617#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002618 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002619#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002620#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002621 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002622#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002623#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002624 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002625#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002626#ifdef CONFIG_HARDWALL
2627 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2628#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002629#ifdef CONFIG_USER_NS
2630 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2631 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002632 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002633#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002634#ifdef CONFIG_CHECKPOINT_RESTORE
2635 REG("timers", S_IRUGO, proc_timers_operations),
2636#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002637};
2638
Al Virof0c3b502013-05-16 12:07:31 -04002639static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002640{
Al Virof0c3b502013-05-16 12:07:31 -04002641 return proc_pident_readdir(file, ctx,
2642 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002643}
2644
Arjan van de Ven00977a52007-02-12 00:55:34 -08002645static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002646 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002647 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002648 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002649};
2650
Al Viro00cd8dd2012-06-10 17:13:09 -04002651static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2652{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002653 return proc_pident_lookup(dir, dentry,
2654 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002655}
2656
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002657static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002658 .lookup = proc_tgid_base_lookup,
2659 .getattr = pid_getattr,
2660 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002661 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002662};
2663
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002664static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002666 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002667 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002668 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Eric W. Biederman48e64842006-06-26 00:25:48 -07002670 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002671 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002672 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002673 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002674 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002675 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002676 d_drop(dentry);
2677 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Eric W. Biederman48e64842006-06-26 00:25:48 -07002680 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002681 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2682 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002683 if (!leader)
2684 goto out;
2685
2686 name.name = "task";
2687 name.len = strlen(name.name);
2688 dir = d_hash_and_lookup(leader, &name);
2689 if (!dir)
2690 goto out_put_leader;
2691
2692 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002693 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002694 dentry = d_hash_and_lookup(dir, &name);
2695 if (dentry) {
2696 shrink_dcache_parent(dentry);
2697 d_drop(dentry);
2698 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002700
2701 dput(dir);
2702out_put_leader:
2703 dput(leader);
2704out:
2705 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Randy Dunlap0895e912007-10-21 21:00:10 -07002708/**
2709 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2710 * @task: task that should be flushed.
2711 *
2712 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002713 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002714 * in. This call is supposed to do all of this job.
2715 *
2716 * Looks in the dcache for
2717 * /proc/@pid
2718 * /proc/@tgid/task/@pid
2719 * if either directory is present flushes it and all of it'ts children
2720 * from the dcache.
2721 *
2722 * It is safe and reasonable to cache /proc entries for a task until
2723 * that task exits. After that they just clog up the dcache with
2724 * useless entries, possibly causing useful dcache entries to be
2725 * flushed instead. This routine is proved to flush those useless
2726 * dcache entries at process exit time.
2727 *
2728 * NOTE: This routine is just an optimization so it does not guarantee
2729 * that no dcache entries will exist at process exit time it
2730 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002731 */
2732
2733void proc_flush_task(struct task_struct *task)
2734{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002735 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002736 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002737 struct upid *upid;
2738
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002739 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002740 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002741
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002742 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002743 upid = &pid->numbers[i];
2744 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002745 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002746 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002747}
2748
Al Viroc52a47a2013-06-15 11:15:20 +04002749static int proc_pid_instantiate(struct inode *dir,
2750 struct dentry * dentry,
2751 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002752{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002753 struct inode *inode;
2754
Eric W. Biederman61a28782006-10-02 02:18:49 -07002755 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002756 if (!inode)
2757 goto out;
2758
2759 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2760 inode->i_op = &proc_tgid_base_inode_operations;
2761 inode->i_fop = &proc_tgid_base_operations;
2762 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002763
Miklos Szeredibfe86842011-10-28 14:13:29 +02002764 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2765 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002766
Nick Pigginfb045ad2011-01-07 17:49:55 +11002767 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002768
2769 d_add(dentry, inode);
2770 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002771 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002772 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002773out:
Al Viroc52a47a2013-06-15 11:15:20 +04002774 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002775}
2776
Al Viro00cd8dd2012-06-10 17:13:09 -04002777struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002779 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002782 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002784 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (tgid == ~0U)
2786 goto out;
2787
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002788 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002789 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002790 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (task)
2792 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002793 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (!task)
2795 goto out;
2796
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002797 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002798 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799out:
Al Viroc52a47a2013-06-15 11:15:20 +04002800 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002804 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002805 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002807struct tgid_iter {
2808 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002809 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002810};
2811static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2812{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002813 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002815 if (iter.task)
2816 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002817 rcu_read_lock();
2818retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002819 iter.task = NULL;
2820 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002821 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002822 iter.tgid = pid_nr_ns(pid, ns);
2823 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002824 /* What we to know is if the pid we have find is the
2825 * pid of a thread_group_leader. Testing for task
2826 * being a thread_group_leader is the obvious thing
2827 * todo but there is a window when it fails, due to
2828 * the pid transfer logic in de_thread.
2829 *
2830 * So we perform the straight forward test of seeing
2831 * if the pid we have found is the pid of a thread
2832 * group leader, and don't worry if the task we have
2833 * found doesn't happen to be a thread group leader.
2834 * As we don't care in the case of readdir.
2835 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002836 if (!iter.task || !has_group_leader_pid(iter.task)) {
2837 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002838 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002839 }
2840 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002842 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002843 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
Al Viro021ada72013-03-29 19:27:05 -04002846#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002849int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002851 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002852 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002853 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Al Viro021ada72013-03-29 19:27:05 -04002855 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Al Viro021ada72013-03-29 19:27:05 -04002858 if (pos == TGID_OFFSET - 1) {
Al Virodb963162013-06-15 10:45:10 +04002859 struct inode *inode = ns->proc_self->d_inode;
2860 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002861 return 0;
Al Viro021ada72013-03-29 19:27:05 -04002862 iter.tgid = 0;
2863 } else {
2864 iter.tgid = pos - TGID_OFFSET;
2865 }
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002866 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002867 for (iter = next_tgid(ns, iter);
2868 iter.task;
2869 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002870 char name[PROC_NUMBUF];
2871 int len;
2872 if (!has_pid_permissions(ns, iter.task, 2))
2873 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002874
Al Virof0c3b502013-05-16 12:07:31 -04002875 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2876 ctx->pos = iter.tgid + TGID_OFFSET;
2877 if (!proc_fill_cache(file, ctx, name, len,
2878 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002879 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
Al Virof0c3b502013-05-16 12:07:31 -04002883 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return 0;
2885}
2886
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002887/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002888 * Tasks
2889 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002890static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002891 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002892 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002893 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002894 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002895 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002896 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002897 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002898 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002899#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002900 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002901#endif
john stultz4614a696b2009-12-14 18:00:05 -08002902 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002903#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002904 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002905#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002906 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2907 ONE("stat", S_IRUGO, proc_tid_stat),
2908 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002909 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002910#ifdef CONFIG_CHECKPOINT_RESTORE
2911 REG("children", S_IRUGO, proc_tid_children_operations),
2912#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002913#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002914 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002915#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002916 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2917 LNK("cwd", proc_cwd_link),
2918 LNK("root", proc_root_link),
2919 LNK("exe", proc_exe_link),
2920 REG("mounts", S_IRUGO, proc_mounts_operations),
2921 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002922#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002924 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002925 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002926#endif
2927#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002928 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002929#endif
2930#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002931 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002932#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002933#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002934 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002935#endif
2936#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002937 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002938#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002939#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002940 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002941#endif
Paul Menage8793d852007-10-18 23:39:39 -07002942#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002943 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944#endif
Paul Menagea4243162007-10-18 23:39:35 -07002945#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002946 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002947#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002948 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002949 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002950 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002951#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002952 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002953 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002955#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002956 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002957#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002958#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002959 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002960#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002961#ifdef CONFIG_HARDWALL
2962 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2963#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002964#ifdef CONFIG_USER_NS
2965 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2966 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002967 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002968#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002969};
2970
Al Virof0c3b502013-05-16 12:07:31 -04002971static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002972{
Al Virof0c3b502013-05-16 12:07:31 -04002973 return proc_pident_readdir(file, ctx,
2974 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002975}
2976
Al Viro00cd8dd2012-06-10 17:13:09 -04002977static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2978{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002979 return proc_pident_lookup(dir, dentry,
2980 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002981}
2982
Arjan van de Ven00977a52007-02-12 00:55:34 -08002983static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002984 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002985 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002986 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002987};
2988
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002989static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990 .lookup = proc_tid_base_lookup,
2991 .getattr = pid_getattr,
2992 .setattr = proc_setattr,
2993};
2994
Al Viroc52a47a2013-06-15 11:15:20 +04002995static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002996 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002997{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002998 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002999 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003000
3001 if (!inode)
3002 goto out;
3003 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3004 inode->i_op = &proc_tid_base_inode_operations;
3005 inode->i_fop = &proc_tid_base_operations;
3006 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003007
Miklos Szeredibfe86842011-10-28 14:13:29 +02003008 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3009 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003010
Nick Pigginfb045ad2011-01-07 17:49:55 +11003011 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003012
3013 d_add(dentry, inode);
3014 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003015 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003016 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003017out:
Al Viroc52a47a2013-06-15 11:15:20 +04003018 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003019}
3020
Al Viro00cd8dd2012-06-10 17:13:09 -04003021static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003022{
Al Viroc52a47a2013-06-15 11:15:20 +04003023 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003024 struct task_struct *task;
3025 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003026 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003027 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003028
3029 if (!leader)
3030 goto out_no_task;
3031
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003032 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003033 if (tid == ~0U)
3034 goto out;
3035
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003036 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003037 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003038 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003039 if (task)
3040 get_task_struct(task);
3041 rcu_read_unlock();
3042 if (!task)
3043 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003044 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003045 goto out_drop_task;
3046
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003047 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003048out_drop_task:
3049 put_task_struct(task);
3050out:
3051 put_task_struct(leader);
3052out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003053 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003054}
3055
3056/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003057 * Find the first tid of a thread group to return to user space.
3058 *
3059 * Usually this is just the thread group leader, but if the users
3060 * buffer was too small or there was a seek into the middle of the
3061 * directory we have more work todo.
3062 *
3063 * In the case of a short read we start with find_task_by_pid.
3064 *
3065 * In the case of a seek we start with the leader and walk nr
3066 * threads past it.
3067 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003068static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3069 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003070{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003071 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003072 unsigned long nr = f_pos;
3073
3074 if (nr != f_pos) /* 32bit overflow? */
3075 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003076
Eric W. Biedermancc288732006-06-26 00:26:01 -07003077 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003078 task = pid_task(pid, PIDTYPE_PID);
3079 if (!task)
3080 goto fail;
3081
3082 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003083 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003084 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003085 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003086 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003087 }
3088
3089 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003090 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003091 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003092
3093 /* If we haven't found our starting place yet start
3094 * with the leader and walk nr threads forward.
3095 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003096 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003097 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003098 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003099 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003100 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003101fail:
3102 pos = NULL;
3103 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003104found:
3105 get_task_struct(pos);
3106out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003107 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003108 return pos;
3109}
3110
3111/*
3112 * Find the next thread in the thread list.
3113 * Return NULL if there is an error or no next thread.
3114 *
3115 * The reference to the input task_struct is released.
3116 */
3117static struct task_struct *next_tid(struct task_struct *start)
3118{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003119 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003120 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003121 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003122 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003123 if (thread_group_leader(pos))
3124 pos = NULL;
3125 else
3126 get_task_struct(pos);
3127 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003128 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003129 put_task_struct(start);
3130 return pos;
3131}
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003134static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003136 struct inode *inode = file_inode(file);
3137 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003138 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003139 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003141 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003142 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Al Virof0c3b502013-05-16 12:07:31 -04003144 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003147 /* f_version caches the tgid value that the last readdir call couldn't
3148 * return. lseek aka telldir automagically resets f_version to 0.
3149 */
Al Virof0c3b502013-05-16 12:07:31 -04003150 ns = file->f_dentry->d_sb->s_fs_info;
3151 tid = (int)file->f_version;
3152 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003153 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003154 task;
Al Virof0c3b502013-05-16 12:07:31 -04003155 task = next_tid(task), ctx->pos++) {
3156 char name[PROC_NUMBUF];
3157 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003158 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003159 len = snprintf(name, sizeof(name), "%d", tid);
3160 if (!proc_fill_cache(file, ctx, name, len,
3161 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003162 /* returning this tgid failed, save it as the first
3163 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003164 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003165 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003169
Al Virof0c3b502013-05-16 12:07:31 -04003170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003172
3173static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3174{
3175 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003176 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003177 generic_fillattr(inode, stat);
3178
Eric W. Biederman99f89552006-06-26 00:25:55 -07003179 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003180 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003181 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003182 }
3183
3184 return 0;
3185}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003186
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003187static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003188 .lookup = proc_task_lookup,
3189 .getattr = proc_task_getattr,
3190 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003191 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003192};
3193
Arjan van de Ven00977a52007-02-12 00:55:34 -08003194static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003195 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003196 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003197 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003198};