blob: ad63fa3306df0c4c1a7491285db651a7149864a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080076#include <linux/printk.h>
Paul Menagea4243162007-10-18 23:39:35 -070077#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpuset.h>
79#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050080#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070081#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070082#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070083#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070084#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080085#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040086#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080088#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040089#include <linux/posix-timers.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040090#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080093#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040095#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070097/* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700108 const char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700109 int len;
Al Virod161a132011-07-24 03:36:29 -0400110 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800111 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800112 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700118 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 .mode = MODE, \
120 .iop = IOP, \
121 .fop = FOP, \
122 .op = OP, \
123}
124
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300125#define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700129 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300130 { .proc_get_link = get_link } )
131#define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300133#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Vegard Nossumaed54172008-06-05 22:46:53 -0700138/*
139 * Count the number of hardlinks for the pid_entry table, excluding the .
140 * and .. links.
141 */
142static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
143 unsigned int n)
144{
145 unsigned int i;
146 unsigned int count;
147
148 count = 0;
149 for (i = 0; i < n; ++i) {
150 if (S_ISDIR(entries[i].mode))
151 ++count;
152 }
153
154 return count;
155}
156
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200157static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000159 int result = -ENOENT;
160
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200162 if (task->fs) {
163 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000164 result = 0;
165 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700166 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168}
169
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800170static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171{
David Howells2b0143b2015-03-17 22:25:59 +0000172 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700174
175 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200176 task_lock(task);
177 if (task->fs) {
178 get_fs_pwd(task->fs, path);
179 result = 0;
180 }
181 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182 put_task_struct(task);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return result;
185}
186
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800187static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
David Howells2b0143b2015-03-17 22:25:59 +0000189 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700191
192 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200193 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194 put_task_struct(task);
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return result;
197}
198
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700199static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
200 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700202 /*
203 * Rely on struct seq_operations::show() being called once
204 * per internal buffer allocation. See single_open(), traverse().
205 */
206 BUG_ON(m->size < PAGE_SIZE);
207 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700211static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
212 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Cong Wange7dcd992012-05-31 16:26:17 -0700214 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500215 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300217 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300219 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700220 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 mmput(mm);
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700222 return 0;
223 } else
224 return PTR_ERR(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227
228#ifdef CONFIG_KALLSYMS
229/*
230 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
231 * Returns the resolved symbol. If that fails, simply return the address.
232 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700233static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
234 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700236 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700237 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 wchan = get_wchan(task);
240
Joe Perches25ce3192015-04-15 16:18:17 -0700241 if (lookup_symbol_name(wchan, symname) < 0) {
Jake Edgef83ce3e2009-05-04 12:51:14 -0600242 if (!ptrace_may_access(task, PTRACE_MODE_READ))
243 return 0;
Joe Perches25ce3192015-04-15 16:18:17 -0700244 seq_printf(m, "%lu", wchan);
245 } else {
246 seq_printf(m, "%s", symname);
247 }
248
249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251#endif /* CONFIG_KALLSYMS */
252
Al Viroa9712bc2011-03-23 15:52:50 -0400253static int lock_trace(struct task_struct *task)
254{
255 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
256 if (err)
257 return err;
258 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
259 mutex_unlock(&task->signal->cred_guard_mutex);
260 return -EPERM;
261 }
262 return 0;
263}
264
265static void unlock_trace(struct task_struct *task)
266{
267 mutex_unlock(&task->signal->cred_guard_mutex);
268}
269
Ken Chen2ec220e2008-11-10 11:26:08 +0300270#ifdef CONFIG_STACKTRACE
271
272#define MAX_STACK_TRACE_DEPTH 64
273
274static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
275 struct pid *pid, struct task_struct *task)
276{
277 struct stack_trace trace;
278 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400279 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300280 int i;
281
282 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
283 if (!entries)
284 return -ENOMEM;
285
286 trace.nr_entries = 0;
287 trace.max_entries = MAX_STACK_TRACE_DEPTH;
288 trace.entries = entries;
289 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300290
Al Viroa9712bc2011-03-23 15:52:50 -0400291 err = lock_trace(task);
292 if (!err) {
293 save_stack_trace_tsk(task, &trace);
294
295 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700296 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400297 (void *)entries[i], (void *)entries[i]);
298 }
299 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300300 }
301 kfree(entries);
302
Al Viroa9712bc2011-03-23 15:52:50 -0400303 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300304}
305#endif
306
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530307#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
309 * Provides /proc/PID/schedstat
310 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700311static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
312 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530314 if (unlikely(!sched_info_on()))
315 seq_printf(m, "0 0 0\n");
316 else
317 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700318 (unsigned long long)task->se.sum_exec_runtime,
319 (unsigned long long)task->sched_info.run_delay,
320 task->sched_info.pcount);
321
322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324#endif
325
Arjan van de Ven97455122008-01-25 21:08:34 +0100326#ifdef CONFIG_LATENCYTOP
327static int lstats_show_proc(struct seq_file *m, void *v)
328{
329 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800330 struct inode *inode = m->private;
331 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100332
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800333 if (!task)
334 return -ESRCH;
335 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100336 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800337 struct latency_record *lr = &task->latency_record[i];
338 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100339 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800340 seq_printf(m, "%i %li %li",
341 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100342 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800343 unsigned long bt = lr->backtrace[q];
344 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100345 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800346 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100347 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800348 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100349 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800350 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100351 }
352
353 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800354 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100355 return 0;
356}
357
358static int lstats_open(struct inode *inode, struct file *file)
359{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800360 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800361}
362
Arjan van de Ven97455122008-01-25 21:08:34 +0100363static ssize_t lstats_write(struct file *file, const char __user *buf,
364 size_t count, loff_t *offs)
365{
Al Viro496ad9a2013-01-23 17:07:38 -0500366 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100367
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800368 if (!task)
369 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100370 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800371 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100372
373 return count;
374}
375
376static const struct file_operations proc_lstats_operations = {
377 .open = lstats_open,
378 .read = seq_read,
379 .write = lstats_write,
380 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800381 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100382};
383
384#endif
385
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700386static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
387 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
David Rientjesa7f638f2012-05-29 15:06:47 -0700389 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200390 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700392 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200393 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700394 points = oom_badness(task, NULL, NULL, totalpages) *
395 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700396 read_unlock(&tasklist_lock);
Joe Perches25ce3192015-04-15 16:18:17 -0700397 seq_printf(m, "%lu\n", points);
398
399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Neil Hormand85f50d2007-10-18 23:40:37 -0700402struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700403 const char *name;
404 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700405};
406
407static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700408 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700409 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
410 [RLIMIT_DATA] = {"Max data size", "bytes"},
411 [RLIMIT_STACK] = {"Max stack size", "bytes"},
412 [RLIMIT_CORE] = {"Max core file size", "bytes"},
413 [RLIMIT_RSS] = {"Max resident set", "bytes"},
414 [RLIMIT_NPROC] = {"Max processes", "processes"},
415 [RLIMIT_NOFILE] = {"Max open files", "files"},
416 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
417 [RLIMIT_AS] = {"Max address space", "bytes"},
418 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
419 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
420 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
421 [RLIMIT_NICE] = {"Max nice priority", NULL},
422 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800423 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700424};
425
426/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700427static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
428 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700429{
430 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700431 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700432
433 struct rlimit rlim[RLIM_NLIMITS];
434
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400435 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700436 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700437 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
438 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700439
440 /*
441 * print the file header
442 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700443 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700444 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700445
446 for (i = 0; i < RLIM_NLIMITS; i++) {
447 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700448 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700449 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700450 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700451 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700452 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700453
454 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700455 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700456 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700457 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700458
459 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700460 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700461 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700462 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700463 }
464
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700465 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700466}
467
Roland McGrathebcb6732008-07-25 19:46:00 -0700468#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700469static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
470 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700471{
472 long nr;
473 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700474 int res;
475
476 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400477 if (res)
478 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700479
480 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700481 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400482 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700483 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400484 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700485 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700486 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
487 nr,
488 args[0], args[1], args[2], args[3], args[4], args[5],
489 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400490 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700491
492 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700493}
494#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/************************************************************************/
497/* Here the fs part begins */
498/************************************************************************/
499
500/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700501static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700503 struct task_struct *task;
504 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700505 /* Allow access to a task's file descriptors if it is us or we
506 * may use ptrace attach to the process and find out that
507 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700508 */
509 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700510 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400511 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700512 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700513 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700514 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800517int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700518{
519 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000520 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700521
522 if (attr->ia_valid & ATTR_MODE)
523 return -EPERM;
524
525 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200526 if (error)
527 return error;
528
Christoph Hellwig10257742010-06-04 11:30:02 +0200529 setattr_copy(inode, attr);
530 mark_inode_dirty(inode);
531 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700532}
533
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800534/*
535 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
536 * or euid/egid (for hide_pid_min=2)?
537 */
538static bool has_pid_permissions(struct pid_namespace *pid,
539 struct task_struct *task,
540 int hide_pid_min)
541{
542 if (pid->hide_pid < hide_pid_min)
543 return true;
544 if (in_group_p(pid->pid_gid))
545 return true;
546 return ptrace_may_access(task, PTRACE_MODE_READ);
547}
548
549
550static int proc_pid_permission(struct inode *inode, int mask)
551{
552 struct pid_namespace *pid = inode->i_sb->s_fs_info;
553 struct task_struct *task;
554 bool has_perms;
555
556 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800557 if (!task)
558 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800559 has_perms = has_pid_permissions(pid, task, 1);
560 put_task_struct(task);
561
562 if (!has_perms) {
563 if (pid->hide_pid == 2) {
564 /*
565 * Let's make getdents(), stat(), and open()
566 * consistent with each other. If a process
567 * may not stat() a file, it shouldn't be seen
568 * in procfs at all.
569 */
570 return -ENOENT;
571 }
572
573 return -EPERM;
574 }
575 return generic_permission(inode, mask);
576}
577
578
579
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800580static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700581 .setattr = proc_setattr,
582};
583
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800584static int proc_single_show(struct seq_file *m, void *v)
585{
586 struct inode *inode = m->private;
587 struct pid_namespace *ns;
588 struct pid *pid;
589 struct task_struct *task;
590 int ret;
591
592 ns = inode->i_sb->s_fs_info;
593 pid = proc_pid(inode);
594 task = get_pid_task(pid, PIDTYPE_PID);
595 if (!task)
596 return -ESRCH;
597
598 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
599
600 put_task_struct(task);
601 return ret;
602}
603
604static int proc_single_open(struct inode *inode, struct file *filp)
605{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800606 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800607}
608
609static const struct file_operations proc_single_file_operations = {
610 .open = proc_single_open,
611 .read = seq_read,
612 .llseek = seq_lseek,
613 .release = single_release,
614};
615
Oleg Nesterov5381e162014-10-09 15:25:24 -0700616
617struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
618{
619 struct task_struct *task = get_proc_task(inode);
620 struct mm_struct *mm = ERR_PTR(-ESRCH);
621
622 if (task) {
623 mm = mm_access(task, mode);
624 put_task_struct(task);
625
626 if (!IS_ERR_OR_NULL(mm)) {
627 /* ensure this mm_struct can't be freed */
628 atomic_inc(&mm->mm_count);
629 /* but do not pin its memory */
630 mmput(mm);
631 }
632 }
633
634 return mm;
635}
636
Cong Wangb409e572012-05-31 16:26:17 -0700637static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700639 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800640
641 if (IS_ERR(mm))
642 return PTR_ERR(mm);
643
Linus Torvaldse2683372012-01-17 15:21:19 -0800644 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
647
Cong Wangb409e572012-05-31 16:26:17 -0700648static int mem_open(struct inode *inode, struct file *file)
649{
Djalal Harounibc452b4b2012-07-30 14:42:28 -0700650 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
651
652 /* OK to pass negative loff_t, we can catch out-of-range */
653 file->f_mode |= FMODE_UNSIGNED_OFFSET;
654
655 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700656}
657
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100658static ssize_t mem_rw(struct file *file, char __user *buf,
659 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Linus Torvaldse2683372012-01-17 15:21:19 -0800661 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100662 unsigned long addr = *ppos;
663 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Linus Torvaldse2683372012-01-17 15:21:19 -0800666 if (!mm)
667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Mel Gormane12ba742007-10-16 01:25:52 -0700669 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800671 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700673 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100674 if (!atomic_inc_not_zero(&mm->mm_users))
675 goto free;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100678 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100680 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 copied = -EFAULT;
682 break;
683 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100684
685 this_len = access_remote_vm(mm, addr, page, this_len, write);
686 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (!copied)
688 copied = -EIO;
689 break;
690 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100691
692 if (!write && copy_to_user(buf, page, this_len)) {
693 copied = -EFAULT;
694 break;
695 }
696
697 buf += this_len;
698 addr += this_len;
699 copied += this_len;
700 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100702 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700703
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100704 mmput(mm);
705free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700706 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return copied;
708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100710static ssize_t mem_read(struct file *file, char __user *buf,
711 size_t count, loff_t *ppos)
712{
713 return mem_rw(file, buf, count, ppos, 0);
714}
715
716static ssize_t mem_write(struct file *file, const char __user *buf,
717 size_t count, loff_t *ppos)
718{
719 return mem_rw(file, (char __user*)buf, count, ppos, 1);
720}
721
Matt Mackall85863e42008-02-04 22:29:04 -0800722loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 switch (orig) {
725 case 0:
726 file->f_pos = offset;
727 break;
728 case 1:
729 file->f_pos += offset;
730 break;
731 default:
732 return -EINVAL;
733 }
734 force_successful_syscall_return();
735 return file->f_pos;
736}
737
Linus Torvaldse2683372012-01-17 15:21:19 -0800738static int mem_release(struct inode *inode, struct file *file)
739{
740 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100741 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100742 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800743 return 0;
744}
745
Arjan van de Ven00977a52007-02-12 00:55:34 -0800746static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .llseek = mem_lseek,
748 .read = mem_read,
749 .write = mem_write,
750 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800751 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752};
753
Cong Wangb409e572012-05-31 16:26:17 -0700754static int environ_open(struct inode *inode, struct file *file)
755{
756 return __mem_open(inode, file, PTRACE_MODE_READ);
757}
758
James Pearson315e28c2007-10-16 23:30:17 -0700759static ssize_t environ_read(struct file *file, char __user *buf,
760 size_t count, loff_t *ppos)
761{
James Pearson315e28c2007-10-16 23:30:17 -0700762 char *page;
763 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700764 int ret = 0;
765 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700766
Cong Wangb409e572012-05-31 16:26:17 -0700767 if (!mm)
768 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700769
James Pearson315e28c2007-10-16 23:30:17 -0700770 page = (char *)__get_free_page(GFP_TEMPORARY);
771 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700772 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700773
Al Virod6f64b82011-02-15 22:26:01 -0500774 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700775 if (!atomic_inc_not_zero(&mm->mm_users))
776 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700777 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700778 size_t this_len, max_len;
779 int retval;
780
781 if (src >= (mm->env_end - mm->env_start))
782 break;
James Pearson315e28c2007-10-16 23:30:17 -0700783
784 this_len = mm->env_end - (mm->env_start + src);
785
Djalal Harounie8905ec2012-07-30 14:42:26 -0700786 max_len = min_t(size_t, PAGE_SIZE, count);
787 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700788
Cong Wangb409e572012-05-31 16:26:17 -0700789 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700790 page, this_len, 0);
791
792 if (retval <= 0) {
793 ret = retval;
794 break;
795 }
796
797 if (copy_to_user(buf, page, retval)) {
798 ret = -EFAULT;
799 break;
800 }
801
802 ret += retval;
803 src += retval;
804 buf += retval;
805 count -= retval;
806 }
807 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700808 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700809
810free:
James Pearson315e28c2007-10-16 23:30:17 -0700811 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700812 return ret;
813}
814
815static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700816 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700817 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100818 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700819 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700820};
821
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800822static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
823 loff_t *ppos)
824{
Al Viro496ad9a2013-01-23 17:07:38 -0500825 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800826 char buffer[PROC_NUMBUF];
827 int oom_adj = OOM_ADJUST_MIN;
828 size_t len;
829 unsigned long flags;
830
831 if (!task)
832 return -ESRCH;
833 if (lock_task_sighand(task, &flags)) {
834 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
835 oom_adj = OOM_ADJUST_MAX;
836 else
837 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
838 OOM_SCORE_ADJ_MAX;
839 unlock_task_sighand(task, &flags);
840 }
841 put_task_struct(task);
842 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
843 return simple_read_from_buffer(buf, count, ppos, buffer, len);
844}
845
846static ssize_t oom_adj_write(struct file *file, const char __user *buf,
847 size_t count, loff_t *ppos)
848{
849 struct task_struct *task;
850 char buffer[PROC_NUMBUF];
851 int oom_adj;
852 unsigned long flags;
853 int err;
854
855 memset(buffer, 0, sizeof(buffer));
856 if (count > sizeof(buffer) - 1)
857 count = sizeof(buffer) - 1;
858 if (copy_from_user(buffer, buf, count)) {
859 err = -EFAULT;
860 goto out;
861 }
862
863 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
864 if (err)
865 goto out;
866 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
867 oom_adj != OOM_DISABLE) {
868 err = -EINVAL;
869 goto out;
870 }
871
Al Viro496ad9a2013-01-23 17:07:38 -0500872 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800873 if (!task) {
874 err = -ESRCH;
875 goto out;
876 }
877
878 task_lock(task);
879 if (!task->mm) {
880 err = -EINVAL;
881 goto err_task_lock;
882 }
883
884 if (!lock_task_sighand(task, &flags)) {
885 err = -ESRCH;
886 goto err_task_lock;
887 }
888
889 /*
890 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
891 * value is always attainable.
892 */
893 if (oom_adj == OOM_ADJUST_MAX)
894 oom_adj = OOM_SCORE_ADJ_MAX;
895 else
896 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
897
898 if (oom_adj < task->signal->oom_score_adj &&
899 !capable(CAP_SYS_RESOURCE)) {
900 err = -EACCES;
901 goto err_sighand;
902 }
903
904 /*
905 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
906 * /proc/pid/oom_score_adj instead.
907 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800908 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 -0800909 current->comm, task_pid_nr(current), task_pid_nr(task),
910 task_pid_nr(task));
911
912 task->signal->oom_score_adj = oom_adj;
913 trace_oom_score_adj_update(task);
914err_sighand:
915 unlock_task_sighand(task, &flags);
916err_task_lock:
917 task_unlock(task);
918 put_task_struct(task);
919out:
920 return err < 0 ? err : count;
921}
922
923static const struct file_operations proc_oom_adj_operations = {
924 .read = oom_adj_read,
925 .write = oom_adj_write,
926 .llseek = generic_file_llseek,
927};
928
David Rientjesa63d83f2010-08-09 17:19:46 -0700929static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
930 size_t count, loff_t *ppos)
931{
Al Viro496ad9a2013-01-23 17:07:38 -0500932 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700933 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800934 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700935 unsigned long flags;
936 size_t len;
937
938 if (!task)
939 return -ESRCH;
940 if (lock_task_sighand(task, &flags)) {
941 oom_score_adj = task->signal->oom_score_adj;
942 unlock_task_sighand(task, &flags);
943 }
944 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800945 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700946 return simple_read_from_buffer(buf, count, ppos, buffer, len);
947}
948
949static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
950 size_t count, loff_t *ppos)
951{
952 struct task_struct *task;
953 char buffer[PROC_NUMBUF];
954 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700955 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -0700956 int err;
957
958 memset(buffer, 0, sizeof(buffer));
959 if (count > sizeof(buffer) - 1)
960 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700961 if (copy_from_user(buffer, buf, count)) {
962 err = -EFAULT;
963 goto out;
964 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700965
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700966 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700967 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700968 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -0700969 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -0700970 oom_score_adj > OOM_SCORE_ADJ_MAX) {
971 err = -EINVAL;
972 goto out;
973 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700974
Al Viro496ad9a2013-01-23 17:07:38 -0500975 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -0700976 if (!task) {
977 err = -ESRCH;
978 goto out;
979 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700980
Ying Han3d5992d2010-10-26 14:21:23 -0700981 task_lock(task);
982 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700983 err = -EINVAL;
984 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700985 }
David Rientjesd19d5472010-10-26 14:21:26 -0700986
987 if (!lock_task_sighand(task, &flags)) {
988 err = -ESRCH;
989 goto err_task_lock;
990 }
991
David Rientjesa9c58b902012-12-11 16:02:54 -0800992 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -0700993 !capable(CAP_SYS_RESOURCE)) {
994 err = -EACCES;
995 goto err_sighand;
996 }
997
David Rientjesa9c58b902012-12-11 16:02:54 -0800998 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -0800999 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001000 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001001 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001002
David Rientjes723548b2010-10-26 14:21:25 -07001003err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001004 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001005err_task_lock:
1006 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001007 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001008out:
1009 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001010}
1011
1012static const struct file_operations proc_oom_score_adj_operations = {
1013 .read = oom_score_adj_read,
1014 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001015 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001016};
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018#ifdef CONFIG_AUDITSYSCALL
1019#define TMPBUFLEN 21
1020static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1021 size_t count, loff_t *ppos)
1022{
Al Viro496ad9a2013-01-23 17:07:38 -05001023 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001024 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 ssize_t length;
1026 char tmpbuf[TMPBUFLEN];
1027
Eric W. Biederman99f89552006-06-26 00:25:55 -07001028 if (!task)
1029 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001031 from_kuid(file->f_cred->user_ns,
1032 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001033 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1035}
1036
1037static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1038 size_t count, loff_t *ppos)
1039{
Al Viro496ad9a2013-01-23 17:07:38 -05001040 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 char *page, *tmp;
1042 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001044 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001046 rcu_read_lock();
1047 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1048 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001050 }
1051 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Al Viroe0182902006-05-18 08:28:02 -04001053 if (count >= PAGE_SIZE)
1054 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (*ppos != 0) {
1057 /* No partial writes. */
1058 return -EINVAL;
1059 }
Mel Gormane12ba742007-10-16 01:25:52 -07001060 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (!page)
1062 return -ENOMEM;
1063 length = -EFAULT;
1064 if (copy_from_user(page, buf, count))
1065 goto out_free_page;
1066
Al Viroe0182902006-05-18 08:28:02 -04001067 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 loginuid = simple_strtoul(page, &tmp, 10);
1069 if (tmp == page) {
1070 length = -EINVAL;
1071 goto out_free_page;
1072
1073 }
Eric Paris81407c82013-05-24 09:49:14 -04001074
1075 /* is userspace tring to explicitly UNSET the loginuid? */
1076 if (loginuid == AUDIT_UID_UNSET) {
1077 kloginuid = INVALID_UID;
1078 } else {
1079 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1080 if (!uid_valid(kloginuid)) {
1081 length = -EINVAL;
1082 goto out_free_page;
1083 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001084 }
1085
1086 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (likely(length == 0))
1088 length = count;
1089
1090out_free_page:
1091 free_page((unsigned long) page);
1092 return length;
1093}
1094
Arjan van de Ven00977a52007-02-12 00:55:34 -08001095static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 .read = proc_loginuid_read,
1097 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001098 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099};
Eric Paris1e0bd752008-03-13 08:15:31 -04001100
1101static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1102 size_t count, loff_t *ppos)
1103{
Al Viro496ad9a2013-01-23 17:07:38 -05001104 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001105 struct task_struct *task = get_proc_task(inode);
1106 ssize_t length;
1107 char tmpbuf[TMPBUFLEN];
1108
1109 if (!task)
1110 return -ESRCH;
1111 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1112 audit_get_sessionid(task));
1113 put_task_struct(task);
1114 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1115}
1116
1117static const struct file_operations proc_sessionid_operations = {
1118 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001119 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001120};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121#endif
1122
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001123#ifdef CONFIG_FAULT_INJECTION
1124static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1125 size_t count, loff_t *ppos)
1126{
Al Viro496ad9a2013-01-23 17:07:38 -05001127 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001128 char buffer[PROC_NUMBUF];
1129 size_t len;
1130 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001131
1132 if (!task)
1133 return -ESRCH;
1134 make_it_fail = task->make_it_fail;
1135 put_task_struct(task);
1136
1137 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001138
1139 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001140}
1141
1142static ssize_t proc_fault_inject_write(struct file * file,
1143 const char __user * buf, size_t count, loff_t *ppos)
1144{
1145 struct task_struct *task;
1146 char buffer[PROC_NUMBUF], *end;
1147 int make_it_fail;
1148
1149 if (!capable(CAP_SYS_RESOURCE))
1150 return -EPERM;
1151 memset(buffer, 0, sizeof(buffer));
1152 if (count > sizeof(buffer) - 1)
1153 count = sizeof(buffer) - 1;
1154 if (copy_from_user(buffer, buf, count))
1155 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001156 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1157 if (*end)
1158 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001159 if (make_it_fail < 0 || make_it_fail > 1)
1160 return -EINVAL;
1161
Al Viro496ad9a2013-01-23 17:07:38 -05001162 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001163 if (!task)
1164 return -ESRCH;
1165 task->make_it_fail = make_it_fail;
1166 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001167
1168 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001169}
1170
Arjan van de Ven00977a52007-02-12 00:55:34 -08001171static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001172 .read = proc_fault_inject_read,
1173 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001174 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001175};
1176#endif
1177
Arjan van de Ven97455122008-01-25 21:08:34 +01001178
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001179#ifdef CONFIG_SCHED_DEBUG
1180/*
1181 * Print out various scheduling related per-task fields:
1182 */
1183static int sched_show(struct seq_file *m, void *v)
1184{
1185 struct inode *inode = m->private;
1186 struct task_struct *p;
1187
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001188 p = get_proc_task(inode);
1189 if (!p)
1190 return -ESRCH;
1191 proc_sched_show_task(p, m);
1192
1193 put_task_struct(p);
1194
1195 return 0;
1196}
1197
1198static ssize_t
1199sched_write(struct file *file, const char __user *buf,
1200 size_t count, loff_t *offset)
1201{
Al Viro496ad9a2013-01-23 17:07:38 -05001202 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001203 struct task_struct *p;
1204
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001205 p = get_proc_task(inode);
1206 if (!p)
1207 return -ESRCH;
1208 proc_sched_set_task(p);
1209
1210 put_task_struct(p);
1211
1212 return count;
1213}
1214
1215static int sched_open(struct inode *inode, struct file *filp)
1216{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001217 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001218}
1219
1220static const struct file_operations proc_pid_sched_operations = {
1221 .open = sched_open,
1222 .read = seq_read,
1223 .write = sched_write,
1224 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001225 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001226};
1227
1228#endif
1229
Mike Galbraith5091faa2010-11-30 14:18:03 +01001230#ifdef CONFIG_SCHED_AUTOGROUP
1231/*
1232 * Print out autogroup related information:
1233 */
1234static int sched_autogroup_show(struct seq_file *m, void *v)
1235{
1236 struct inode *inode = m->private;
1237 struct task_struct *p;
1238
1239 p = get_proc_task(inode);
1240 if (!p)
1241 return -ESRCH;
1242 proc_sched_autogroup_show_task(p, m);
1243
1244 put_task_struct(p);
1245
1246 return 0;
1247}
1248
1249static ssize_t
1250sched_autogroup_write(struct file *file, const char __user *buf,
1251 size_t count, loff_t *offset)
1252{
Al Viro496ad9a2013-01-23 17:07:38 -05001253 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001254 struct task_struct *p;
1255 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001256 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001257 int err;
1258
1259 memset(buffer, 0, sizeof(buffer));
1260 if (count > sizeof(buffer) - 1)
1261 count = sizeof(buffer) - 1;
1262 if (copy_from_user(buffer, buf, count))
1263 return -EFAULT;
1264
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001265 err = kstrtoint(strstrip(buffer), 0, &nice);
1266 if (err < 0)
1267 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001268
1269 p = get_proc_task(inode);
1270 if (!p)
1271 return -ESRCH;
1272
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001273 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001274 if (err)
1275 count = err;
1276
1277 put_task_struct(p);
1278
1279 return count;
1280}
1281
1282static int sched_autogroup_open(struct inode *inode, struct file *filp)
1283{
1284 int ret;
1285
1286 ret = single_open(filp, sched_autogroup_show, NULL);
1287 if (!ret) {
1288 struct seq_file *m = filp->private_data;
1289
1290 m->private = inode;
1291 }
1292 return ret;
1293}
1294
1295static const struct file_operations proc_pid_sched_autogroup_operations = {
1296 .open = sched_autogroup_open,
1297 .read = seq_read,
1298 .write = sched_autogroup_write,
1299 .llseek = seq_lseek,
1300 .release = single_release,
1301};
1302
1303#endif /* CONFIG_SCHED_AUTOGROUP */
1304
john stultz4614a696b2009-12-14 18:00:05 -08001305static ssize_t comm_write(struct file *file, const char __user *buf,
1306 size_t count, loff_t *offset)
1307{
Al Viro496ad9a2013-01-23 17:07:38 -05001308 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001309 struct task_struct *p;
1310 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001311 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001312
1313 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001314 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001315 return -EFAULT;
1316
1317 p = get_proc_task(inode);
1318 if (!p)
1319 return -ESRCH;
1320
1321 if (same_thread_group(current, p))
1322 set_task_comm(p, buffer);
1323 else
1324 count = -EINVAL;
1325
1326 put_task_struct(p);
1327
1328 return count;
1329}
1330
1331static int comm_show(struct seq_file *m, void *v)
1332{
1333 struct inode *inode = m->private;
1334 struct task_struct *p;
1335
1336 p = get_proc_task(inode);
1337 if (!p)
1338 return -ESRCH;
1339
1340 task_lock(p);
1341 seq_printf(m, "%s\n", p->comm);
1342 task_unlock(p);
1343
1344 put_task_struct(p);
1345
1346 return 0;
1347}
1348
1349static int comm_open(struct inode *inode, struct file *filp)
1350{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001351 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001352}
1353
1354static const struct file_operations proc_pid_set_comm_operations = {
1355 .open = comm_open,
1356 .read = seq_read,
1357 .write = comm_write,
1358 .llseek = seq_lseek,
1359 .release = single_release,
1360};
1361
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001362static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001363{
1364 struct task_struct *task;
1365 struct mm_struct *mm;
1366 struct file *exe_file;
1367
David Howells2b0143b2015-03-17 22:25:59 +00001368 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001369 if (!task)
1370 return -ENOENT;
1371 mm = get_task_mm(task);
1372 put_task_struct(task);
1373 if (!mm)
1374 return -ENOENT;
1375 exe_file = get_mm_exe_file(mm);
1376 mmput(mm);
1377 if (exe_file) {
1378 *exe_path = exe_file->f_path;
1379 path_get(&exe_file->f_path);
1380 fput(exe_file);
1381 return 0;
1382 } else
1383 return -ENOENT;
1384}
1385
Al Viro6e771372015-05-02 13:37:52 -04001386static const char *proc_pid_follow_link(struct dentry *dentry, void **cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
David Howells2b0143b2015-03-17 22:25:59 +00001388 struct inode *inode = d_inode(dentry);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001389 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int error = -EACCES;
1391
Eric W. Biederman778c1142006-06-26 00:25:58 -07001392 /* Are we allowed to snoop on the tasks file descriptors? */
1393 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Christoph Hellwig408ef012012-06-18 10:47:03 -04001396 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1397 if (error)
1398 goto out;
1399
Al Viro6e771372015-05-02 13:37:52 -04001400 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001401 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402out:
Al Viro008b1502005-08-20 00:17:39 +01001403 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001406static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Mel Gormane12ba742007-10-16 01:25:52 -07001408 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001409 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int len;
1411
1412 if (!tmp)
1413 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001414
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001415 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001416 len = PTR_ERR(pathname);
1417 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001419 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 if (len > buflen)
1422 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001423 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 len = -EFAULT;
1425 out:
1426 free_page((unsigned long)tmp);
1427 return len;
1428}
1429
1430static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1431{
1432 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001433 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001434 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
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
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001440 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (error)
1442 goto out;
1443
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001444 error = do_proc_readlink(&path, buffer, buflen);
1445 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return error;
1448}
1449
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001450const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001452 .follow_link = proc_pid_follow_link,
1453 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454};
1455
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001456
1457/* building an inode */
1458
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001459struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001460{
1461 struct inode * inode;
1462 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001463 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001464
1465 /* We need a new inode */
1466
1467 inode = new_inode(sb);
1468 if (!inode)
1469 goto out;
1470
1471 /* Common stuff */
1472 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001473 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001474 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001475 inode->i_op = &proc_def_inode_operations;
1476
1477 /*
1478 * grab the reference to task.
1479 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001480 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001481 if (!ei->pid)
1482 goto out_unlock;
1483
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001484 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001485 rcu_read_lock();
1486 cred = __task_cred(task);
1487 inode->i_uid = cred->euid;
1488 inode->i_gid = cred->egid;
1489 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001490 }
1491 security_task_to_inode(task, inode);
1492
1493out:
1494 return inode;
1495
1496out_unlock:
1497 iput(inode);
1498 return NULL;
1499}
1500
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001501int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001502{
David Howells2b0143b2015-03-17 22:25:59 +00001503 struct inode *inode = d_inode(dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001504 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001505 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001506 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001507
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001508 generic_fillattr(inode, stat);
1509
1510 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001511 stat->uid = GLOBAL_ROOT_UID;
1512 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001513 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1514 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001515 if (!has_pid_permissions(pid, task, 2)) {
1516 rcu_read_unlock();
1517 /*
1518 * This doesn't prevent learning whether PID exists,
1519 * it only makes getattr() consistent with readdir().
1520 */
1521 return -ENOENT;
1522 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001523 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1524 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001525 cred = __task_cred(task);
1526 stat->uid = cred->euid;
1527 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001528 }
1529 }
1530 rcu_read_unlock();
1531 return 0;
1532}
1533
1534/* dentry stuff */
1535
1536/*
1537 * Exceptional case: normally we are not allowed to unhash a busy
1538 * directory. In this case, however, we can do it - no aliasing problems
1539 * due to the way we treat inodes.
1540 *
1541 * Rewrite the inode's ownerships here because the owning task may have
1542 * performed a setuid(), etc.
1543 *
1544 * Before the /proc/pid/status file was created the only way to read
1545 * the effective uid of a /process was to stat /proc/pid. Reading
1546 * /proc/pid/status is slow enough that procps and other packages
1547 * kept stating /proc/pid. To keep the rules in /proc simple I have
1548 * made this apply to all per process world readable and executable
1549 * directories.
1550 */
Al Viro0b728e12012-06-10 16:03:43 -04001551int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001552{
Nick Piggin34286d62011-01-07 17:49:57 +11001553 struct inode *inode;
1554 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001555 const struct cred *cred;
1556
Al Viro0b728e12012-06-10 16:03:43 -04001557 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001558 return -ECHILD;
1559
David Howells2b0143b2015-03-17 22:25:59 +00001560 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001561 task = get_proc_task(inode);
1562
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001563 if (task) {
1564 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1565 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001566 rcu_read_lock();
1567 cred = __task_cred(task);
1568 inode->i_uid = cred->euid;
1569 inode->i_gid = cred->egid;
1570 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001571 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001572 inode->i_uid = GLOBAL_ROOT_UID;
1573 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001574 }
1575 inode->i_mode &= ~(S_ISUID | S_ISGID);
1576 security_task_to_inode(task, inode);
1577 put_task_struct(task);
1578 return 1;
1579 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001580 return 0;
1581}
1582
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001583static inline bool proc_inode_is_dead(struct inode *inode)
1584{
1585 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1586}
1587
David Howells1dd704b2013-04-12 01:08:50 +01001588int pid_delete_dentry(const struct dentry *dentry)
1589{
1590 /* Is the task we represent dead?
1591 * If so, then don't put the dentry on the lru list,
1592 * kill it immediately.
1593 */
David Howells2b0143b2015-03-17 22:25:59 +00001594 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001595}
1596
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001597const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001598{
1599 .d_revalidate = pid_revalidate,
1600 .d_delete = pid_delete_dentry,
1601};
1602
1603/* Lookups */
1604
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001605/*
1606 * Fill a directory entry.
1607 *
1608 * If possible create the dcache entry and derive our inode number and
1609 * file type from dcache entry.
1610 *
1611 * Since all of the proc inode numbers are dynamically generated, the inode
1612 * numbers do not exist until the inode is cache. This means creating the
1613 * the dcache entry in readdir is necessary to keep the inode numbers
1614 * reported by readdir in sync with the inode numbers reported
1615 * by stat.
1616 */
Al Virof0c3b502013-05-16 12:07:31 -04001617bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001618 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001619 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001620{
Al Virof0c3b502013-05-16 12:07:31 -04001621 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001622 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001623 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001624 unsigned type;
1625 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001626
Al Viro1df98b82013-06-15 11:33:10 +04001627 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001628 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001629 child = d_alloc(dir, &qname);
1630 if (!child)
1631 goto end_instantiate;
David Howells2b0143b2015-03-17 22:25:59 +00001632 if (instantiate(d_inode(dir), child, task, ptr) < 0) {
Al Viro1df98b82013-06-15 11:33:10 +04001633 dput(child);
1634 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001635 }
1636 }
David Howells2b0143b2015-03-17 22:25:59 +00001637 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001638 ino = inode->i_ino;
1639 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001640 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001641 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001642
1643end_instantiate:
1644 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001645}
1646
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001647#ifdef CONFIG_CHECKPOINT_RESTORE
1648
1649/*
1650 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1651 * which represent vma start and end addresses.
1652 */
1653static int dname_to_vma_addr(struct dentry *dentry,
1654 unsigned long *start, unsigned long *end)
1655{
1656 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1657 return -EINVAL;
1658
1659 return 0;
1660}
1661
Al Viro0b728e12012-06-10 16:03:43 -04001662static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001663{
1664 unsigned long vm_start, vm_end;
1665 bool exact_vma_exists = false;
1666 struct mm_struct *mm = NULL;
1667 struct task_struct *task;
1668 const struct cred *cred;
1669 struct inode *inode;
1670 int status = 0;
1671
Al Viro0b728e12012-06-10 16:03:43 -04001672 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001673 return -ECHILD;
1674
1675 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001676 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001677 goto out_notask;
1678 }
1679
David Howells2b0143b2015-03-17 22:25:59 +00001680 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001681 task = get_proc_task(inode);
1682 if (!task)
1683 goto out_notask;
1684
Cong Wang2344bec2012-05-31 16:26:18 -07001685 mm = mm_access(task, PTRACE_MODE_READ);
1686 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001687 goto out;
1688
1689 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1690 down_read(&mm->mmap_sem);
1691 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1692 up_read(&mm->mmap_sem);
1693 }
1694
1695 mmput(mm);
1696
1697 if (exact_vma_exists) {
1698 if (task_dumpable(task)) {
1699 rcu_read_lock();
1700 cred = __task_cred(task);
1701 inode->i_uid = cred->euid;
1702 inode->i_gid = cred->egid;
1703 rcu_read_unlock();
1704 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001705 inode->i_uid = GLOBAL_ROOT_UID;
1706 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001707 }
1708 security_task_to_inode(task, inode);
1709 status = 1;
1710 }
1711
1712out:
1713 put_task_struct(task);
1714
1715out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001716 return status;
1717}
1718
1719static const struct dentry_operations tid_map_files_dentry_operations = {
1720 .d_revalidate = map_files_d_revalidate,
1721 .d_delete = pid_delete_dentry,
1722};
1723
1724static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1725{
1726 unsigned long vm_start, vm_end;
1727 struct vm_area_struct *vma;
1728 struct task_struct *task;
1729 struct mm_struct *mm;
1730 int rc;
1731
1732 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001733 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001734 if (!task)
1735 goto out;
1736
1737 mm = get_task_mm(task);
1738 put_task_struct(task);
1739 if (!mm)
1740 goto out;
1741
1742 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1743 if (rc)
1744 goto out_mmput;
1745
Artem Fetishev70335ab2014-03-10 15:49:45 -07001746 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001747 down_read(&mm->mmap_sem);
1748 vma = find_exact_vma(mm, vm_start, vm_end);
1749 if (vma && vma->vm_file) {
1750 *path = vma->vm_file->f_path;
1751 path_get(path);
1752 rc = 0;
1753 }
1754 up_read(&mm->mmap_sem);
1755
1756out_mmput:
1757 mmput(mm);
1758out:
1759 return rc;
1760}
1761
1762struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001763 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001764 unsigned long len;
1765 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1766};
1767
Al Viroc52a47a2013-06-15 11:15:20 +04001768static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001769proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1770 struct task_struct *task, const void *ptr)
1771{
Al Viro7b540d02012-08-27 14:55:26 -04001772 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001773 struct proc_inode *ei;
1774 struct inode *inode;
1775
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001776 inode = proc_pid_make_inode(dir->i_sb, task);
1777 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001778 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001779
1780 ei = PROC_I(inode);
1781 ei->op.proc_get_link = proc_map_files_get_link;
1782
1783 inode->i_op = &proc_pid_link_inode_operations;
1784 inode->i_size = 64;
1785 inode->i_mode = S_IFLNK;
1786
Al Viro7b540d02012-08-27 14:55:26 -04001787 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001788 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001789 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001790 inode->i_mode |= S_IWUSR;
1791
1792 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1793 d_add(dentry, inode);
1794
Al Viroc52a47a2013-06-15 11:15:20 +04001795 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001796}
1797
1798static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001799 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001800{
1801 unsigned long vm_start, vm_end;
1802 struct vm_area_struct *vma;
1803 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001804 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001805 struct mm_struct *mm;
1806
Al Viroc52a47a2013-06-15 11:15:20 +04001807 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001808 if (!capable(CAP_SYS_ADMIN))
1809 goto out;
1810
Al Viroc52a47a2013-06-15 11:15:20 +04001811 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001812 task = get_proc_task(dir);
1813 if (!task)
1814 goto out;
1815
Al Viroc52a47a2013-06-15 11:15:20 +04001816 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001817 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001818 goto out_put_task;
1819
Al Viroc52a47a2013-06-15 11:15:20 +04001820 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001821 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001822 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001823
1824 mm = get_task_mm(task);
1825 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001826 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001827
1828 down_read(&mm->mmap_sem);
1829 vma = find_exact_vma(mm, vm_start, vm_end);
1830 if (!vma)
1831 goto out_no_vma;
1832
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001833 if (vma->vm_file)
1834 result = proc_map_files_instantiate(dir, dentry, task,
1835 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001836
1837out_no_vma:
1838 up_read(&mm->mmap_sem);
1839 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001840out_put_task:
1841 put_task_struct(task);
1842out:
Al Viroc52a47a2013-06-15 11:15:20 +04001843 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001844}
1845
1846static const struct inode_operations proc_map_files_inode_operations = {
1847 .lookup = proc_map_files_lookup,
1848 .permission = proc_fd_permission,
1849 .setattr = proc_setattr,
1850};
1851
1852static int
Al Virof0c3b502013-05-16 12:07:31 -04001853proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001854{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001855 struct vm_area_struct *vma;
1856 struct task_struct *task;
1857 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001858 unsigned long nr_files, pos, i;
1859 struct flex_array *fa = NULL;
1860 struct map_files_info info;
1861 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001862 int ret;
1863
Zhao Hongjiang41735812013-02-20 13:13:55 +11001864 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001865 if (!capable(CAP_SYS_ADMIN))
1866 goto out;
1867
1868 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001869 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001870 if (!task)
1871 goto out;
1872
1873 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001874 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001875 goto out_put_task;
1876
1877 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001878 if (!dir_emit_dots(file, ctx))
1879 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001880
Al Virof0c3b502013-05-16 12:07:31 -04001881 mm = get_task_mm(task);
1882 if (!mm)
1883 goto out_put_task;
1884 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001885
Al Virof0c3b502013-05-16 12:07:31 -04001886 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001887
Al Virof0c3b502013-05-16 12:07:31 -04001888 /*
1889 * We need two passes here:
1890 *
1891 * 1) Collect vmas of mapped files with mmap_sem taken
1892 * 2) Release mmap_sem and instantiate entries
1893 *
1894 * otherwise we get lockdep complained, since filldir()
1895 * routine might require mmap_sem taken in might_fault().
1896 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001897
Al Virof0c3b502013-05-16 12:07:31 -04001898 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1899 if (vma->vm_file && ++pos > ctx->pos)
1900 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001901 }
Al Virof0c3b502013-05-16 12:07:31 -04001902
1903 if (nr_files) {
1904 fa = flex_array_alloc(sizeof(info), nr_files,
1905 GFP_KERNEL);
1906 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1907 GFP_KERNEL)) {
1908 ret = -ENOMEM;
1909 if (fa)
1910 flex_array_free(fa);
1911 up_read(&mm->mmap_sem);
1912 mmput(mm);
1913 goto out_put_task;
1914 }
1915 for (i = 0, vma = mm->mmap, pos = 2; vma;
1916 vma = vma->vm_next) {
1917 if (!vma->vm_file)
1918 continue;
1919 if (++pos <= ctx->pos)
1920 continue;
1921
1922 info.mode = vma->vm_file->f_mode;
1923 info.len = snprintf(info.name,
1924 sizeof(info.name), "%lx-%lx",
1925 vma->vm_start, vma->vm_end);
1926 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1927 BUG();
1928 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001929 }
Al Virof0c3b502013-05-16 12:07:31 -04001930 up_read(&mm->mmap_sem);
1931
1932 for (i = 0; i < nr_files; i++) {
1933 p = flex_array_get(fa, i);
1934 if (!proc_fill_cache(file, ctx,
1935 p->name, p->len,
1936 proc_map_files_instantiate,
1937 task,
1938 (void *)(unsigned long)p->mode))
1939 break;
1940 ctx->pos++;
1941 }
1942 if (fa)
1943 flex_array_free(fa);
1944 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001945
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001946out_put_task:
1947 put_task_struct(task);
1948out:
1949 return ret;
1950}
1951
1952static const struct file_operations proc_map_files_operations = {
1953 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04001954 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001955 .llseek = default_llseek,
1956};
1957
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001958struct timers_private {
1959 struct pid *pid;
1960 struct task_struct *task;
1961 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04001962 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001963 unsigned long flags;
1964};
1965
1966static void *timers_start(struct seq_file *m, loff_t *pos)
1967{
1968 struct timers_private *tp = m->private;
1969
1970 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
1971 if (!tp->task)
1972 return ERR_PTR(-ESRCH);
1973
1974 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
1975 if (!tp->sighand)
1976 return ERR_PTR(-ESRCH);
1977
1978 return seq_list_start(&tp->task->signal->posix_timers, *pos);
1979}
1980
1981static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
1982{
1983 struct timers_private *tp = m->private;
1984 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
1985}
1986
1987static void timers_stop(struct seq_file *m, void *v)
1988{
1989 struct timers_private *tp = m->private;
1990
1991 if (tp->sighand) {
1992 unlock_task_sighand(tp->task, &tp->flags);
1993 tp->sighand = NULL;
1994 }
1995
1996 if (tp->task) {
1997 put_task_struct(tp->task);
1998 tp->task = NULL;
1999 }
2000}
2001
2002static int show_timer(struct seq_file *m, void *v)
2003{
2004 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002005 struct timers_private *tp = m->private;
2006 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002007 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002008 [SIGEV_SIGNAL] = "signal",
2009 [SIGEV_NONE] = "none",
2010 [SIGEV_THREAD] = "thread",
2011 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002012
2013 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002014 notify = timer->it_sigev_notify;
2015
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002016 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002017 seq_printf(m, "signal: %d/%p\n",
2018 timer->sigq->info.si_signo,
2019 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002020 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002021 nstr[notify & ~SIGEV_THREAD_ID],
2022 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2023 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002024 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002025
2026 return 0;
2027}
2028
2029static const struct seq_operations proc_timers_seq_ops = {
2030 .start = timers_start,
2031 .next = timers_next,
2032 .stop = timers_stop,
2033 .show = show_timer,
2034};
2035
2036static int proc_timers_open(struct inode *inode, struct file *file)
2037{
2038 struct timers_private *tp;
2039
2040 tp = __seq_open_private(file, &proc_timers_seq_ops,
2041 sizeof(struct timers_private));
2042 if (!tp)
2043 return -ENOMEM;
2044
2045 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002046 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002047 return 0;
2048}
2049
2050static const struct file_operations proc_timers_operations = {
2051 .open = proc_timers_open,
2052 .read = seq_read,
2053 .llseek = seq_lseek,
2054 .release = seq_release_private,
2055};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002056#endif /* CONFIG_CHECKPOINT_RESTORE */
2057
Al Viroc52a47a2013-06-15 11:15:20 +04002058static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002059 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002060{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002061 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002062 struct inode *inode;
2063 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002064
Eric W. Biederman61a28782006-10-02 02:18:49 -07002065 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002066 if (!inode)
2067 goto out;
2068
2069 ei = PROC_I(inode);
2070 inode->i_mode = p->mode;
2071 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002072 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002073 if (p->iop)
2074 inode->i_op = p->iop;
2075 if (p->fop)
2076 inode->i_fop = p->fop;
2077 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002078 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002079 d_add(dentry, inode);
2080 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002081 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002082 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002083out:
Al Viroc52a47a2013-06-15 11:15:20 +04002084 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002085}
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087static struct dentry *proc_pident_lookup(struct inode *dir,
2088 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002089 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002090 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Al Viroc52a47a2013-06-15 11:15:20 +04002092 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002093 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002094 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Al Viroc52a47a2013-06-15 11:15:20 +04002096 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Eric W. Biederman99f89552006-06-26 00:25:55 -07002098 if (!task)
2099 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002101 /*
2102 * Yes, it does not scale. And it should not. Don't add
2103 * new entries into /proc/<tgid>/ without very good reasons.
2104 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002105 last = &ents[nents - 1];
2106 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (p->len != dentry->d_name.len)
2108 continue;
2109 if (!memcmp(dentry->d_name.name, p->name, p->len))
2110 break;
2111 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002112 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 goto out;
2114
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002115 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002117 put_task_struct(task);
2118out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002119 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Al Virof0c3b502013-05-16 12:07:31 -04002122static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002123 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002124{
Al Virof0c3b502013-05-16 12:07:31 -04002125 struct task_struct *task = get_proc_task(file_inode(file));
2126 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002127
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002128 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002129 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002130
Al Virof0c3b502013-05-16 12:07:31 -04002131 if (!dir_emit_dots(file, ctx))
2132 goto out;
2133
2134 if (ctx->pos >= nents + 2)
2135 goto out;
2136
2137 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2138 if (!proc_fill_cache(file, ctx, p->name, p->len,
2139 proc_pident_instantiate, task, p))
2140 break;
2141 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002142 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002143out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002144 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002149static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2150 size_t count, loff_t *ppos)
2151{
Al Viro496ad9a2013-01-23 17:07:38 -05002152 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002153 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002154 ssize_t length;
2155 struct task_struct *task = get_proc_task(inode);
2156
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002157 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002158 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002159
2160 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002161 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002162 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002163 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002164 if (length > 0)
2165 length = simple_read_from_buffer(buf, count, ppos, p, length);
2166 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002167 return length;
2168}
2169
2170static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2171 size_t count, loff_t *ppos)
2172{
Al Viro496ad9a2013-01-23 17:07:38 -05002173 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002174 char *page;
2175 ssize_t length;
2176 struct task_struct *task = get_proc_task(inode);
2177
2178 length = -ESRCH;
2179 if (!task)
2180 goto out_no_task;
2181 if (count > PAGE_SIZE)
2182 count = PAGE_SIZE;
2183
2184 /* No partial writes. */
2185 length = -EINVAL;
2186 if (*ppos != 0)
2187 goto out;
2188
2189 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002190 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002191 if (!page)
2192 goto out;
2193
2194 length = -EFAULT;
2195 if (copy_from_user(page, buf, count))
2196 goto out_free;
2197
David Howells107db7c2009-05-08 13:55:27 +01002198 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002199 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002200 if (length < 0)
2201 goto out_free;
2202
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002203 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002204 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002205 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002206 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002207out_free:
2208 free_page((unsigned long) page);
2209out:
2210 put_task_struct(task);
2211out_no_task:
2212 return length;
2213}
2214
Arjan van de Ven00977a52007-02-12 00:55:34 -08002215static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002216 .read = proc_pid_attr_read,
2217 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002218 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002219};
2220
Eric Dumazetc5141e62007-05-08 00:26:15 -07002221static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002222 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2223 REG("prev", S_IRUGO, proc_pid_attr_operations),
2224 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2225 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2226 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2227 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002228};
2229
Al Virof0c3b502013-05-16 12:07:31 -04002230static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Al Virof0c3b502013-05-16 12:07:31 -04002232 return proc_pident_readdir(file, ctx,
2233 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
Arjan van de Ven00977a52007-02-12 00:55:34 -08002236static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002238 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002239 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240};
2241
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002242static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002243 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002245 return proc_pident_lookup(dir, dentry,
2246 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002249static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002250 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002251 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002252 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253};
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255#endif
2256
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002257#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002258static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2259 size_t count, loff_t *ppos)
2260{
Al Viro496ad9a2013-01-23 17:07:38 -05002261 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002262 struct mm_struct *mm;
2263 char buffer[PROC_NUMBUF];
2264 size_t len;
2265 int ret;
2266
2267 if (!task)
2268 return -ESRCH;
2269
2270 ret = 0;
2271 mm = get_task_mm(task);
2272 if (mm) {
2273 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2274 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2275 MMF_DUMP_FILTER_SHIFT));
2276 mmput(mm);
2277 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2278 }
2279
2280 put_task_struct(task);
2281
2282 return ret;
2283}
2284
2285static ssize_t proc_coredump_filter_write(struct file *file,
2286 const char __user *buf,
2287 size_t count,
2288 loff_t *ppos)
2289{
2290 struct task_struct *task;
2291 struct mm_struct *mm;
2292 char buffer[PROC_NUMBUF], *end;
2293 unsigned int val;
2294 int ret;
2295 int i;
2296 unsigned long mask;
2297
2298 ret = -EFAULT;
2299 memset(buffer, 0, sizeof(buffer));
2300 if (count > sizeof(buffer) - 1)
2301 count = sizeof(buffer) - 1;
2302 if (copy_from_user(buffer, buf, count))
2303 goto out_no_task;
2304
2305 ret = -EINVAL;
2306 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2307 if (*end == '\n')
2308 end++;
2309 if (end - buffer == 0)
2310 goto out_no_task;
2311
2312 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002313 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002314 if (!task)
2315 goto out_no_task;
2316
2317 ret = end - buffer;
2318 mm = get_task_mm(task);
2319 if (!mm)
2320 goto out_no_mm;
2321
2322 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2323 if (val & mask)
2324 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2325 else
2326 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2327 }
2328
2329 mmput(mm);
2330 out_no_mm:
2331 put_task_struct(task);
2332 out_no_task:
2333 return ret;
2334}
2335
2336static const struct file_operations proc_coredump_filter_operations = {
2337 .read = proc_coredump_filter_read,
2338 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002339 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002340};
2341#endif
2342
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002343#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002344static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002345{
Andrea Righi940389b2008-07-28 00:48:12 +02002346 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002347 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002348 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002349
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002350 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2351 if (result)
2352 return result;
2353
2354 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2355 result = -EACCES;
2356 goto out_unlock;
2357 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002358
Andrea Righi59954772008-07-27 17:29:15 +02002359 if (whole && lock_task_sighand(task, &flags)) {
2360 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002361
Andrea Righi59954772008-07-27 17:29:15 +02002362 task_io_accounting_add(&acct, &task->signal->ioac);
2363 while_each_thread(task, t)
2364 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002365
Andrea Righi59954772008-07-27 17:29:15 +02002366 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002367 }
Joe Perches25ce3192015-04-15 16:18:17 -07002368 seq_printf(m,
2369 "rchar: %llu\n"
2370 "wchar: %llu\n"
2371 "syscr: %llu\n"
2372 "syscw: %llu\n"
2373 "read_bytes: %llu\n"
2374 "write_bytes: %llu\n"
2375 "cancelled_write_bytes: %llu\n",
2376 (unsigned long long)acct.rchar,
2377 (unsigned long long)acct.wchar,
2378 (unsigned long long)acct.syscr,
2379 (unsigned long long)acct.syscw,
2380 (unsigned long long)acct.read_bytes,
2381 (unsigned long long)acct.write_bytes,
2382 (unsigned long long)acct.cancelled_write_bytes);
2383 result = 0;
2384
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002385out_unlock:
2386 mutex_unlock(&task->signal->cred_guard_mutex);
2387 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002388}
Andrea Righi297c5d92008-07-25 01:48:49 -07002389
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002390static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2391 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002392{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002393 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002394}
2395
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002396static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2397 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002398{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002399 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002400}
2401#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002402
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002403#ifdef CONFIG_USER_NS
2404static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002405 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002406{
2407 struct user_namespace *ns = NULL;
2408 struct task_struct *task;
2409 struct seq_file *seq;
2410 int ret = -EINVAL;
2411
2412 task = get_proc_task(inode);
2413 if (task) {
2414 rcu_read_lock();
2415 ns = get_user_ns(task_cred_xxx(task, user_ns));
2416 rcu_read_unlock();
2417 put_task_struct(task);
2418 }
2419 if (!ns)
2420 goto err;
2421
2422 ret = seq_open(file, seq_ops);
2423 if (ret)
2424 goto err_put_ns;
2425
2426 seq = file->private_data;
2427 seq->private = ns;
2428
2429 return 0;
2430err_put_ns:
2431 put_user_ns(ns);
2432err:
2433 return ret;
2434}
2435
2436static int proc_id_map_release(struct inode *inode, struct file *file)
2437{
2438 struct seq_file *seq = file->private_data;
2439 struct user_namespace *ns = seq->private;
2440 put_user_ns(ns);
2441 return seq_release(inode, file);
2442}
2443
2444static int proc_uid_map_open(struct inode *inode, struct file *file)
2445{
2446 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2447}
2448
2449static int proc_gid_map_open(struct inode *inode, struct file *file)
2450{
2451 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2452}
2453
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002454static int proc_projid_map_open(struct inode *inode, struct file *file)
2455{
2456 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2457}
2458
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002459static const struct file_operations proc_uid_map_operations = {
2460 .open = proc_uid_map_open,
2461 .write = proc_uid_map_write,
2462 .read = seq_read,
2463 .llseek = seq_lseek,
2464 .release = proc_id_map_release,
2465};
2466
2467static const struct file_operations proc_gid_map_operations = {
2468 .open = proc_gid_map_open,
2469 .write = proc_gid_map_write,
2470 .read = seq_read,
2471 .llseek = seq_lseek,
2472 .release = proc_id_map_release,
2473};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002474
2475static const struct file_operations proc_projid_map_operations = {
2476 .open = proc_projid_map_open,
2477 .write = proc_projid_map_write,
2478 .read = seq_read,
2479 .llseek = seq_lseek,
2480 .release = proc_id_map_release,
2481};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002482
2483static int proc_setgroups_open(struct inode *inode, struct file *file)
2484{
2485 struct user_namespace *ns = NULL;
2486 struct task_struct *task;
2487 int ret;
2488
2489 ret = -ESRCH;
2490 task = get_proc_task(inode);
2491 if (task) {
2492 rcu_read_lock();
2493 ns = get_user_ns(task_cred_xxx(task, user_ns));
2494 rcu_read_unlock();
2495 put_task_struct(task);
2496 }
2497 if (!ns)
2498 goto err;
2499
2500 if (file->f_mode & FMODE_WRITE) {
2501 ret = -EACCES;
2502 if (!ns_capable(ns, CAP_SYS_ADMIN))
2503 goto err_put_ns;
2504 }
2505
2506 ret = single_open(file, &proc_setgroups_show, ns);
2507 if (ret)
2508 goto err_put_ns;
2509
2510 return 0;
2511err_put_ns:
2512 put_user_ns(ns);
2513err:
2514 return ret;
2515}
2516
2517static int proc_setgroups_release(struct inode *inode, struct file *file)
2518{
2519 struct seq_file *seq = file->private_data;
2520 struct user_namespace *ns = seq->private;
2521 int ret = single_release(inode, file);
2522 put_user_ns(ns);
2523 return ret;
2524}
2525
2526static const struct file_operations proc_setgroups_operations = {
2527 .open = proc_setgroups_open,
2528 .write = proc_setgroups_write,
2529 .read = seq_read,
2530 .llseek = seq_lseek,
2531 .release = proc_setgroups_release,
2532};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002533#endif /* CONFIG_USER_NS */
2534
Kees Cook47830722008-10-06 03:11:58 +04002535static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2536 struct pid *pid, struct task_struct *task)
2537{
Al Viroa9712bc2011-03-23 15:52:50 -04002538 int err = lock_trace(task);
2539 if (!err) {
2540 seq_printf(m, "%08x\n", task->personality);
2541 unlock_trace(task);
2542 }
2543 return err;
Kees Cook47830722008-10-06 03:11:58 +04002544}
2545
Eric W. Biederman801199c2006-10-02 02:18:48 -07002546/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002547 * Thread groups
2548 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002549static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002550static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002551
Eric Dumazetc5141e62007-05-08 00:26:15 -07002552static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002553 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2554 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002555#ifdef CONFIG_CHECKPOINT_RESTORE
2556 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2557#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002558 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002559 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002560#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002561 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002562#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002563 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002564 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002565 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002566 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002567 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002568#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002569 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002570#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002571#ifdef CONFIG_SCHED_AUTOGROUP
2572 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2573#endif
john stultz4614a696b2009-12-14 18:00:05 -08002574 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002575#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002576 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002577#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002578 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002579 ONE("stat", S_IRUGO, proc_tgid_stat),
2580 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002581 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002582#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002583 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002584#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002585 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2586 LNK("cwd", proc_cwd_link),
2587 LNK("root", proc_root_link),
2588 LNK("exe", proc_exe_link),
2589 REG("mounts", S_IRUGO, proc_mounts_operations),
2590 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2591 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002592#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002593 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002594 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002595 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002596#endif
2597#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002598 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002599#endif
2600#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002601 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002603#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002604 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002605#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302606#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002607 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002608#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002609#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002610 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002611#endif
Paul Menage8793d852007-10-18 23:39:39 -07002612#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002613 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002614#endif
Paul Menagea4243162007-10-18 23:39:35 -07002615#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002616 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002617#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002618 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002619 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002620 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002621#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002622 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2623 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002624#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002625#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002626 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002627#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002628#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002629 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002630#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002631#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002632 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002633#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002634#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002635 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002636#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002637#ifdef CONFIG_USER_NS
2638 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2639 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002640 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002641 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002642#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002643#ifdef CONFIG_CHECKPOINT_RESTORE
2644 REG("timers", S_IRUGO, proc_timers_operations),
2645#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002646};
2647
Al Virof0c3b502013-05-16 12:07:31 -04002648static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002649{
Al Virof0c3b502013-05-16 12:07:31 -04002650 return proc_pident_readdir(file, ctx,
2651 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002652}
2653
Arjan van de Ven00977a52007-02-12 00:55:34 -08002654static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002655 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002656 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002657 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002658};
2659
Al Viro00cd8dd2012-06-10 17:13:09 -04002660static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2661{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002662 return proc_pident_lookup(dir, dentry,
2663 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002664}
2665
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002666static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002667 .lookup = proc_tgid_base_lookup,
2668 .getattr = pid_getattr,
2669 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002670 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002671};
2672
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002673static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002675 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002676 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002677 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Eric W. Biederman48e64842006-06-26 00:25:48 -07002679 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002680 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002681 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002682 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002683 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08002684 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002685 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08002688 if (pid == tgid)
2689 return;
2690
Eric W. Biederman48e64842006-06-26 00:25:48 -07002691 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002692 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2693 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002694 if (!leader)
2695 goto out;
2696
2697 name.name = "task";
2698 name.len = strlen(name.name);
2699 dir = d_hash_and_lookup(leader, &name);
2700 if (!dir)
2701 goto out_put_leader;
2702
2703 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002704 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002705 dentry = d_hash_and_lookup(dir, &name);
2706 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08002707 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002708 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002710
2711 dput(dir);
2712out_put_leader:
2713 dput(leader);
2714out:
2715 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Randy Dunlap0895e912007-10-21 21:00:10 -07002718/**
2719 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2720 * @task: task that should be flushed.
2721 *
2722 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002723 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002724 * in. This call is supposed to do all of this job.
2725 *
2726 * Looks in the dcache for
2727 * /proc/@pid
2728 * /proc/@tgid/task/@pid
2729 * if either directory is present flushes it and all of it'ts children
2730 * from the dcache.
2731 *
2732 * It is safe and reasonable to cache /proc entries for a task until
2733 * that task exits. After that they just clog up the dcache with
2734 * useless entries, possibly causing useful dcache entries to be
2735 * flushed instead. This routine is proved to flush those useless
2736 * dcache entries at process exit time.
2737 *
2738 * NOTE: This routine is just an optimization so it does not guarantee
2739 * that no dcache entries will exist at process exit time it
2740 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002741 */
2742
2743void proc_flush_task(struct task_struct *task)
2744{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002745 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002746 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002747 struct upid *upid;
2748
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002749 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002750 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002751
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002752 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002753 upid = &pid->numbers[i];
2754 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002755 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002756 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002757}
2758
Al Viroc52a47a2013-06-15 11:15:20 +04002759static int proc_pid_instantiate(struct inode *dir,
2760 struct dentry * dentry,
2761 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002762{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002763 struct inode *inode;
2764
Eric W. Biederman61a28782006-10-02 02:18:49 -07002765 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002766 if (!inode)
2767 goto out;
2768
2769 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2770 inode->i_op = &proc_tgid_base_inode_operations;
2771 inode->i_fop = &proc_tgid_base_operations;
2772 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002773
Miklos Szeredibfe86842011-10-28 14:13:29 +02002774 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2775 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002776
Nick Pigginfb045ad2011-01-07 17:49:55 +11002777 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002778
2779 d_add(dentry, inode);
2780 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002781 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002782 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002783out:
Al Viroc52a47a2013-06-15 11:15:20 +04002784 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002785}
2786
Al Viro00cd8dd2012-06-10 17:13:09 -04002787struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002789 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002792 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002794 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (tgid == ~0U)
2796 goto out;
2797
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002798 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002799 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002800 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (task)
2802 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002803 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 if (!task)
2805 goto out;
2806
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002807 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002808 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809out:
Al Viroc52a47a2013-06-15 11:15:20 +04002810 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002814 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002815 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002817struct tgid_iter {
2818 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002819 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002820};
2821static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2822{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002823 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002825 if (iter.task)
2826 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002827 rcu_read_lock();
2828retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002829 iter.task = NULL;
2830 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002831 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002832 iter.tgid = pid_nr_ns(pid, ns);
2833 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002834 /* What we to know is if the pid we have find is the
2835 * pid of a thread_group_leader. Testing for task
2836 * being a thread_group_leader is the obvious thing
2837 * todo but there is a window when it fails, due to
2838 * the pid transfer logic in de_thread.
2839 *
2840 * So we perform the straight forward test of seeing
2841 * if the pid we have found is the pid of a thread
2842 * group leader, and don't worry if the task we have
2843 * found doesn't happen to be a thread group leader.
2844 * As we don't care in the case of readdir.
2845 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002846 if (!iter.task || !has_group_leader_pid(iter.task)) {
2847 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002848 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002849 }
2850 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002852 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002853 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
Eric W. Biederman00978752014-07-31 03:10:50 -07002856#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002859int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002861 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04002862 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002863 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Al Viro021ada72013-03-29 19:27:05 -04002865 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Eric W. Biederman00978752014-07-31 03:10:50 -07002868 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00002869 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04002870 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002871 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07002872 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04002873 }
Eric W. Biederman00978752014-07-31 03:10:50 -07002874 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00002875 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07002876 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
2877 return 0;
2878 ctx->pos = pos = pos + 1;
2879 }
2880 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002881 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002882 for (iter = next_tgid(ns, iter);
2883 iter.task;
2884 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002885 char name[PROC_NUMBUF];
2886 int len;
2887 if (!has_pid_permissions(ns, iter.task, 2))
2888 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002889
Al Virof0c3b502013-05-16 12:07:31 -04002890 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2891 ctx->pos = iter.tgid + TGID_OFFSET;
2892 if (!proc_fill_cache(file, ctx, name, len,
2893 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002894 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
Al Virof0c3b502013-05-16 12:07:31 -04002898 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return 0;
2900}
2901
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002902/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002903 * Tasks
2904 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002905static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002906 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002907 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002908 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07002909#ifdef CONFIG_NET
2910 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2911#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002912 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002913 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002914 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002915 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002916 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002917#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002918 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002919#endif
john stultz4614a696b2009-12-14 18:00:05 -08002920 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002921#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002922 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002923#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002924 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002925 ONE("stat", S_IRUGO, proc_tid_stat),
2926 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002927 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002928#ifdef CONFIG_CHECKPOINT_RESTORE
2929 REG("children", S_IRUGO, proc_tid_children_operations),
2930#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002931#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002932 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002934 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2935 LNK("cwd", proc_cwd_link),
2936 LNK("root", proc_root_link),
2937 LNK("exe", proc_exe_link),
2938 REG("mounts", S_IRUGO, proc_mounts_operations),
2939 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002940#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002941 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002942 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002943 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944#endif
2945#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002946 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002947#endif
2948#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002949 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002950#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002951#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002952 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002953#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302954#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002955 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002956#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002957#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002958 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002959#endif
Paul Menage8793d852007-10-18 23:39:39 -07002960#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002961 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002962#endif
Paul Menagea4243162007-10-18 23:39:35 -07002963#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002964 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002965#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002966 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002967 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002968 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002969#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002970 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002971 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002972#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002973#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002974 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002975#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002976#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002977 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002978#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002979#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002980 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002981#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002982#ifdef CONFIG_USER_NS
2983 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2984 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002985 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002986 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002987#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002988};
2989
Al Virof0c3b502013-05-16 12:07:31 -04002990static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002991{
Al Virof0c3b502013-05-16 12:07:31 -04002992 return proc_pident_readdir(file, ctx,
2993 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002994}
2995
Al Viro00cd8dd2012-06-10 17:13:09 -04002996static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2997{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002998 return proc_pident_lookup(dir, dentry,
2999 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003000}
3001
Arjan van de Ven00977a52007-02-12 00:55:34 -08003002static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003003 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003004 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003005 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003006};
3007
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003008static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009 .lookup = proc_tid_base_lookup,
3010 .getattr = pid_getattr,
3011 .setattr = proc_setattr,
3012};
3013
Al Viroc52a47a2013-06-15 11:15:20 +04003014static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003015 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003016{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003017 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003018 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003019
3020 if (!inode)
3021 goto out;
3022 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3023 inode->i_op = &proc_tid_base_inode_operations;
3024 inode->i_fop = &proc_tid_base_operations;
3025 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003026
Miklos Szeredibfe86842011-10-28 14:13:29 +02003027 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3028 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003029
Nick Pigginfb045ad2011-01-07 17:49:55 +11003030 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003031
3032 d_add(dentry, inode);
3033 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003034 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003035 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003036out:
Al Viroc52a47a2013-06-15 11:15:20 +04003037 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003038}
3039
Al Viro00cd8dd2012-06-10 17:13:09 -04003040static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003041{
Al Viroc52a47a2013-06-15 11:15:20 +04003042 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003043 struct task_struct *task;
3044 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003045 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003046 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003047
3048 if (!leader)
3049 goto out_no_task;
3050
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003051 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003052 if (tid == ~0U)
3053 goto out;
3054
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003055 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003056 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003057 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003058 if (task)
3059 get_task_struct(task);
3060 rcu_read_unlock();
3061 if (!task)
3062 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003063 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003064 goto out_drop_task;
3065
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003066 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003067out_drop_task:
3068 put_task_struct(task);
3069out:
3070 put_task_struct(leader);
3071out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003072 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003073}
3074
3075/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003076 * Find the first tid of a thread group to return to user space.
3077 *
3078 * Usually this is just the thread group leader, but if the users
3079 * buffer was too small or there was a seek into the middle of the
3080 * directory we have more work todo.
3081 *
3082 * In the case of a short read we start with find_task_by_pid.
3083 *
3084 * In the case of a seek we start with the leader and walk nr
3085 * threads past it.
3086 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003087static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3088 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003089{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003090 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003091 unsigned long nr = f_pos;
3092
3093 if (nr != f_pos) /* 32bit overflow? */
3094 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003095
Eric W. Biedermancc288732006-06-26 00:26:01 -07003096 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003097 task = pid_task(pid, PIDTYPE_PID);
3098 if (!task)
3099 goto fail;
3100
3101 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003102 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003103 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003104 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003105 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003106 }
3107
3108 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003109 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003110 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003111
3112 /* If we haven't found our starting place yet start
3113 * with the leader and walk nr threads forward.
3114 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003115 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003116 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003117 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003118 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003119 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003120fail:
3121 pos = NULL;
3122 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003123found:
3124 get_task_struct(pos);
3125out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003126 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003127 return pos;
3128}
3129
3130/*
3131 * Find the next thread in the thread list.
3132 * Return NULL if there is an error or no next thread.
3133 *
3134 * The reference to the input task_struct is released.
3135 */
3136static struct task_struct *next_tid(struct task_struct *start)
3137{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003138 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003139 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003140 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003141 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003142 if (thread_group_leader(pos))
3143 pos = NULL;
3144 else
3145 get_task_struct(pos);
3146 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003147 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003148 put_task_struct(start);
3149 return pos;
3150}
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003153static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003155 struct inode *inode = file_inode(file);
3156 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003157 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003158 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003160 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003161 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Al Virof0c3b502013-05-16 12:07:31 -04003163 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003166 /* f_version caches the tgid value that the last readdir call couldn't
3167 * return. lseek aka telldir automagically resets f_version to 0.
3168 */
Al Viro3aa33772014-10-31 00:42:35 -04003169 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003170 tid = (int)file->f_version;
3171 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003172 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003173 task;
Al Virof0c3b502013-05-16 12:07:31 -04003174 task = next_tid(task), ctx->pos++) {
3175 char name[PROC_NUMBUF];
3176 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003177 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003178 len = snprintf(name, sizeof(name), "%d", tid);
3179 if (!proc_fill_cache(file, ctx, name, len,
3180 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003181 /* returning this tgid failed, save it as the first
3182 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003183 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003184 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003188
Al Virof0c3b502013-05-16 12:07:31 -04003189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003191
3192static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3193{
David Howells2b0143b2015-03-17 22:25:59 +00003194 struct inode *inode = d_inode(dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003195 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003196 generic_fillattr(inode, stat);
3197
Eric W. Biederman99f89552006-06-26 00:25:55 -07003198 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003199 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003200 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003201 }
3202
3203 return 0;
3204}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003205
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003206static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003207 .lookup = proc_task_lookup,
3208 .getattr = proc_task_getattr,
3209 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003210 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003211};
3212
Arjan van de Ven00977a52007-02-12 00:55:34 -08003213static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003214 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003215 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003216 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003217};