blob: 87782e874b6af4523adff3e7e721046d5f857ee0 [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 Dobriyanc2c0bb42015-06-25 15:00:54 -0700199static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
200 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700202 struct task_struct *tsk;
203 struct mm_struct *mm;
204 char *page;
205 unsigned long count = _count;
206 unsigned long arg_start, arg_end, env_start, env_end;
207 unsigned long len1, len2, len;
208 unsigned long p;
209 char c;
210 ssize_t rv;
211
212 BUG_ON(*pos < 0);
213
214 tsk = get_proc_task(file_inode(file));
215 if (!tsk)
216 return -ESRCH;
217 mm = get_task_mm(tsk);
218 put_task_struct(tsk);
219 if (!mm)
220 return 0;
221 /* Check if process spawned far enough to have cmdline. */
222 if (!mm->env_end) {
223 rv = 0;
224 goto out_mmput;
225 }
226
227 page = (char *)__get_free_page(GFP_TEMPORARY);
228 if (!page) {
229 rv = -ENOMEM;
230 goto out_mmput;
231 }
232
233 down_read(&mm->mmap_sem);
234 arg_start = mm->arg_start;
235 arg_end = mm->arg_end;
236 env_start = mm->env_start;
237 env_end = mm->env_end;
238 up_read(&mm->mmap_sem);
239
240 BUG_ON(arg_start > arg_end);
241 BUG_ON(env_start > env_end);
242
243 len1 = arg_end - arg_start;
244 len2 = env_end - env_start;
245
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700246 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700247 * Inherently racy -- command line shares address space
248 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700249 */
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700250 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
251 if (rv <= 0)
252 goto out_free_page;
253
254 rv = 0;
255
256 if (c == '\0') {
257 /* Command line (set of strings) occupies whole ARGV. */
258 if (len1 <= *pos)
259 goto out_free_page;
260
261 p = arg_start + *pos;
262 len = len1 - *pos;
263 while (count > 0 && len > 0) {
264 unsigned int _count;
265 int nr_read;
266
267 _count = min3(count, len, PAGE_SIZE);
268 nr_read = access_remote_vm(mm, p, page, _count, 0);
269 if (nr_read < 0)
270 rv = nr_read;
271 if (nr_read <= 0)
272 goto out_free_page;
273
274 if (copy_to_user(buf, page, nr_read)) {
275 rv = -EFAULT;
276 goto out_free_page;
277 }
278
279 p += nr_read;
280 len -= nr_read;
281 buf += nr_read;
282 count -= nr_read;
283 rv += nr_read;
284 }
285 } else {
286 /*
287 * Command line (1 string) occupies ARGV and maybe
288 * extends into ENVP.
289 */
290 if (len1 + len2 <= *pos)
291 goto skip_argv_envp;
292 if (len1 <= *pos)
293 goto skip_argv;
294
295 p = arg_start + *pos;
296 len = len1 - *pos;
297 while (count > 0 && len > 0) {
298 unsigned int _count, l;
299 int nr_read;
300 bool final;
301
302 _count = min3(count, len, PAGE_SIZE);
303 nr_read = access_remote_vm(mm, p, page, _count, 0);
304 if (nr_read < 0)
305 rv = nr_read;
306 if (nr_read <= 0)
307 goto out_free_page;
308
309 /*
310 * Command line can be shorter than whole ARGV
311 * even if last "marker" byte says it is not.
312 */
313 final = false;
314 l = strnlen(page, nr_read);
315 if (l < nr_read) {
316 nr_read = l;
317 final = true;
318 }
319
320 if (copy_to_user(buf, page, nr_read)) {
321 rv = -EFAULT;
322 goto out_free_page;
323 }
324
325 p += nr_read;
326 len -= nr_read;
327 buf += nr_read;
328 count -= nr_read;
329 rv += nr_read;
330
331 if (final)
332 goto out_free_page;
333 }
334skip_argv:
335 /*
336 * Command line (1 string) occupies ARGV and
337 * extends into ENVP.
338 */
339 if (len1 <= *pos) {
340 p = env_start + *pos - len1;
341 len = len1 + len2 - *pos;
342 } else {
343 p = env_start;
344 len = len2;
345 }
346 while (count > 0 && len > 0) {
347 unsigned int _count, l;
348 int nr_read;
349 bool final;
350
351 _count = min3(count, len, PAGE_SIZE);
352 nr_read = access_remote_vm(mm, p, page, _count, 0);
353 if (nr_read < 0)
354 rv = nr_read;
355 if (nr_read <= 0)
356 goto out_free_page;
357
358 /* Find EOS. */
359 final = false;
360 l = strnlen(page, nr_read);
361 if (l < nr_read) {
362 nr_read = l;
363 final = true;
364 }
365
366 if (copy_to_user(buf, page, nr_read)) {
367 rv = -EFAULT;
368 goto out_free_page;
369 }
370
371 p += nr_read;
372 len -= nr_read;
373 buf += nr_read;
374 count -= nr_read;
375 rv += nr_read;
376
377 if (final)
378 goto out_free_page;
379 }
380skip_argv_envp:
381 ;
382 }
383
384out_free_page:
385 free_page((unsigned long)page);
386out_mmput:
387 mmput(mm);
388 if (rv > 0)
389 *pos += rv;
390 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700393static const struct file_operations proc_pid_cmdline_ops = {
394 .read = proc_pid_cmdline_read,
395 .llseek = generic_file_llseek,
396};
397
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700398static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
399 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Cong Wange7dcd992012-05-31 16:26:17 -0700401 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500402 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300404 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300406 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700407 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 mmput(mm);
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700409 return 0;
410 } else
411 return PTR_ERR(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414
415#ifdef CONFIG_KALLSYMS
416/*
417 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
418 * Returns the resolved symbol. If that fails, simply return the address.
419 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700420static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
421 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700423 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700424 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 wchan = get_wchan(task);
427
Joe Perches25ce3192015-04-15 16:18:17 -0700428 if (lookup_symbol_name(wchan, symname) < 0) {
Jake Edgef83ce3e2009-05-04 12:51:14 -0600429 if (!ptrace_may_access(task, PTRACE_MODE_READ))
430 return 0;
Joe Perches25ce3192015-04-15 16:18:17 -0700431 seq_printf(m, "%lu", wchan);
432 } else {
433 seq_printf(m, "%s", symname);
434 }
435
436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438#endif /* CONFIG_KALLSYMS */
439
Al Viroa9712bc2011-03-23 15:52:50 -0400440static int lock_trace(struct task_struct *task)
441{
442 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
443 if (err)
444 return err;
445 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
446 mutex_unlock(&task->signal->cred_guard_mutex);
447 return -EPERM;
448 }
449 return 0;
450}
451
452static void unlock_trace(struct task_struct *task)
453{
454 mutex_unlock(&task->signal->cred_guard_mutex);
455}
456
Ken Chen2ec220e2008-11-10 11:26:08 +0300457#ifdef CONFIG_STACKTRACE
458
459#define MAX_STACK_TRACE_DEPTH 64
460
461static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
462 struct pid *pid, struct task_struct *task)
463{
464 struct stack_trace trace;
465 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400466 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300467 int i;
468
469 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
470 if (!entries)
471 return -ENOMEM;
472
473 trace.nr_entries = 0;
474 trace.max_entries = MAX_STACK_TRACE_DEPTH;
475 trace.entries = entries;
476 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300477
Al Viroa9712bc2011-03-23 15:52:50 -0400478 err = lock_trace(task);
479 if (!err) {
480 save_stack_trace_tsk(task, &trace);
481
482 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700483 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400484 (void *)entries[i], (void *)entries[i]);
485 }
486 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300487 }
488 kfree(entries);
489
Al Viroa9712bc2011-03-23 15:52:50 -0400490 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300491}
492#endif
493
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530494#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/*
496 * Provides /proc/PID/schedstat
497 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700498static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
499 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530501 if (unlikely(!sched_info_on()))
502 seq_printf(m, "0 0 0\n");
503 else
504 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700505 (unsigned long long)task->se.sum_exec_runtime,
506 (unsigned long long)task->sched_info.run_delay,
507 task->sched_info.pcount);
508
509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511#endif
512
Arjan van de Ven97455122008-01-25 21:08:34 +0100513#ifdef CONFIG_LATENCYTOP
514static int lstats_show_proc(struct seq_file *m, void *v)
515{
516 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800517 struct inode *inode = m->private;
518 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100519
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800520 if (!task)
521 return -ESRCH;
522 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100523 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800524 struct latency_record *lr = &task->latency_record[i];
525 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100526 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800527 seq_printf(m, "%i %li %li",
528 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100529 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800530 unsigned long bt = lr->backtrace[q];
531 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100532 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800533 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100534 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800535 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100536 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800537 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100538 }
539
540 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800541 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100542 return 0;
543}
544
545static int lstats_open(struct inode *inode, struct file *file)
546{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800547 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800548}
549
Arjan van de Ven97455122008-01-25 21:08:34 +0100550static ssize_t lstats_write(struct file *file, const char __user *buf,
551 size_t count, loff_t *offs)
552{
Al Viro496ad9a2013-01-23 17:07:38 -0500553 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100554
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800555 if (!task)
556 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100557 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800558 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100559
560 return count;
561}
562
563static const struct file_operations proc_lstats_operations = {
564 .open = lstats_open,
565 .read = seq_read,
566 .write = lstats_write,
567 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800568 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100569};
570
571#endif
572
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700573static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
574 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
David Rientjesa7f638f2012-05-29 15:06:47 -0700576 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200577 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700579 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200580 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700581 points = oom_badness(task, NULL, NULL, totalpages) *
582 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700583 read_unlock(&tasklist_lock);
Joe Perches25ce3192015-04-15 16:18:17 -0700584 seq_printf(m, "%lu\n", points);
585
586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Neil Hormand85f50d2007-10-18 23:40:37 -0700589struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700590 const char *name;
591 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700592};
593
594static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700595 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700596 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
597 [RLIMIT_DATA] = {"Max data size", "bytes"},
598 [RLIMIT_STACK] = {"Max stack size", "bytes"},
599 [RLIMIT_CORE] = {"Max core file size", "bytes"},
600 [RLIMIT_RSS] = {"Max resident set", "bytes"},
601 [RLIMIT_NPROC] = {"Max processes", "processes"},
602 [RLIMIT_NOFILE] = {"Max open files", "files"},
603 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
604 [RLIMIT_AS] = {"Max address space", "bytes"},
605 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
606 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
607 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
608 [RLIMIT_NICE] = {"Max nice priority", NULL},
609 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800610 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700611};
612
613/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700614static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
615 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700616{
617 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700618 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700619
620 struct rlimit rlim[RLIM_NLIMITS];
621
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400622 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700623 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700624 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
625 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700626
627 /*
628 * print the file header
629 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700630 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700631 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700632
633 for (i = 0; i < RLIM_NLIMITS; i++) {
634 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700635 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700636 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700637 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700638 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700639 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700640
641 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700642 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700643 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700644 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700645
646 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700647 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700648 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700649 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700650 }
651
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700652 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700653}
654
Roland McGrathebcb6732008-07-25 19:46:00 -0700655#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700656static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
657 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700658{
659 long nr;
660 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700661 int res;
662
663 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400664 if (res)
665 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700666
667 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700668 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400669 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700670 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400671 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700672 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700673 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
674 nr,
675 args[0], args[1], args[2], args[3], args[4], args[5],
676 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400677 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700678
679 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700680}
681#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/************************************************************************/
684/* Here the fs part begins */
685/************************************************************************/
686
687/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700688static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700690 struct task_struct *task;
691 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700692 /* Allow access to a task's file descriptors if it is us or we
693 * may use ptrace attach to the process and find out that
694 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700695 */
696 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700697 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400698 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700699 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700700 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700701 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800704int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700705{
706 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000707 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700708
709 if (attr->ia_valid & ATTR_MODE)
710 return -EPERM;
711
712 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200713 if (error)
714 return error;
715
Christoph Hellwig10257742010-06-04 11:30:02 +0200716 setattr_copy(inode, attr);
717 mark_inode_dirty(inode);
718 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700719}
720
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800721/*
722 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
723 * or euid/egid (for hide_pid_min=2)?
724 */
725static bool has_pid_permissions(struct pid_namespace *pid,
726 struct task_struct *task,
727 int hide_pid_min)
728{
729 if (pid->hide_pid < hide_pid_min)
730 return true;
731 if (in_group_p(pid->pid_gid))
732 return true;
733 return ptrace_may_access(task, PTRACE_MODE_READ);
734}
735
736
737static int proc_pid_permission(struct inode *inode, int mask)
738{
739 struct pid_namespace *pid = inode->i_sb->s_fs_info;
740 struct task_struct *task;
741 bool has_perms;
742
743 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800744 if (!task)
745 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800746 has_perms = has_pid_permissions(pid, task, 1);
747 put_task_struct(task);
748
749 if (!has_perms) {
750 if (pid->hide_pid == 2) {
751 /*
752 * Let's make getdents(), stat(), and open()
753 * consistent with each other. If a process
754 * may not stat() a file, it shouldn't be seen
755 * in procfs at all.
756 */
757 return -ENOENT;
758 }
759
760 return -EPERM;
761 }
762 return generic_permission(inode, mask);
763}
764
765
766
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800767static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700768 .setattr = proc_setattr,
769};
770
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800771static int proc_single_show(struct seq_file *m, void *v)
772{
773 struct inode *inode = m->private;
774 struct pid_namespace *ns;
775 struct pid *pid;
776 struct task_struct *task;
777 int ret;
778
779 ns = inode->i_sb->s_fs_info;
780 pid = proc_pid(inode);
781 task = get_pid_task(pid, PIDTYPE_PID);
782 if (!task)
783 return -ESRCH;
784
785 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
786
787 put_task_struct(task);
788 return ret;
789}
790
791static int proc_single_open(struct inode *inode, struct file *filp)
792{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800793 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800794}
795
796static const struct file_operations proc_single_file_operations = {
797 .open = proc_single_open,
798 .read = seq_read,
799 .llseek = seq_lseek,
800 .release = single_release,
801};
802
Oleg Nesterov5381e162014-10-09 15:25:24 -0700803
804struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
805{
806 struct task_struct *task = get_proc_task(inode);
807 struct mm_struct *mm = ERR_PTR(-ESRCH);
808
809 if (task) {
810 mm = mm_access(task, mode);
811 put_task_struct(task);
812
813 if (!IS_ERR_OR_NULL(mm)) {
814 /* ensure this mm_struct can't be freed */
815 atomic_inc(&mm->mm_count);
816 /* but do not pin its memory */
817 mmput(mm);
818 }
819 }
820
821 return mm;
822}
823
Cong Wangb409e572012-05-31 16:26:17 -0700824static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700826 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800827
828 if (IS_ERR(mm))
829 return PTR_ERR(mm);
830
Linus Torvaldse2683372012-01-17 15:21:19 -0800831 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833}
834
Cong Wangb409e572012-05-31 16:26:17 -0700835static int mem_open(struct inode *inode, struct file *file)
836{
Djalal Harounibc452b42012-07-30 14:42:28 -0700837 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
838
839 /* OK to pass negative loff_t, we can catch out-of-range */
840 file->f_mode |= FMODE_UNSIGNED_OFFSET;
841
842 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700843}
844
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100845static ssize_t mem_rw(struct file *file, char __user *buf,
846 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvaldse2683372012-01-17 15:21:19 -0800848 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100849 unsigned long addr = *ppos;
850 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvaldse2683372012-01-17 15:21:19 -0800853 if (!mm)
854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Mel Gormane12ba742007-10-16 01:25:52 -0700856 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800858 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700860 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100861 if (!atomic_inc_not_zero(&mm->mm_users))
862 goto free;
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100865 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100867 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 copied = -EFAULT;
869 break;
870 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100871
872 this_len = access_remote_vm(mm, addr, page, this_len, write);
873 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (!copied)
875 copied = -EIO;
876 break;
877 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100878
879 if (!write && copy_to_user(buf, page, this_len)) {
880 copied = -EFAULT;
881 break;
882 }
883
884 buf += this_len;
885 addr += this_len;
886 copied += this_len;
887 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100889 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700890
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100891 mmput(mm);
892free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700893 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return copied;
895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100897static ssize_t mem_read(struct file *file, char __user *buf,
898 size_t count, loff_t *ppos)
899{
900 return mem_rw(file, buf, count, ppos, 0);
901}
902
903static ssize_t mem_write(struct file *file, const char __user *buf,
904 size_t count, loff_t *ppos)
905{
906 return mem_rw(file, (char __user*)buf, count, ppos, 1);
907}
908
Matt Mackall85863e42008-02-04 22:29:04 -0800909loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 switch (orig) {
912 case 0:
913 file->f_pos = offset;
914 break;
915 case 1:
916 file->f_pos += offset;
917 break;
918 default:
919 return -EINVAL;
920 }
921 force_successful_syscall_return();
922 return file->f_pos;
923}
924
Linus Torvaldse2683372012-01-17 15:21:19 -0800925static int mem_release(struct inode *inode, struct file *file)
926{
927 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100928 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100929 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800930 return 0;
931}
932
Arjan van de Ven00977a52007-02-12 00:55:34 -0800933static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 .llseek = mem_lseek,
935 .read = mem_read,
936 .write = mem_write,
937 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800938 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939};
940
Cong Wangb409e572012-05-31 16:26:17 -0700941static int environ_open(struct inode *inode, struct file *file)
942{
943 return __mem_open(inode, file, PTRACE_MODE_READ);
944}
945
James Pearson315e28c2007-10-16 23:30:17 -0700946static ssize_t environ_read(struct file *file, char __user *buf,
947 size_t count, loff_t *ppos)
948{
James Pearson315e28c2007-10-16 23:30:17 -0700949 char *page;
950 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700951 int ret = 0;
952 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700953
Cong Wangb409e572012-05-31 16:26:17 -0700954 if (!mm)
955 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700956
James Pearson315e28c2007-10-16 23:30:17 -0700957 page = (char *)__get_free_page(GFP_TEMPORARY);
958 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700959 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700960
Al Virod6f64b82011-02-15 22:26:01 -0500961 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700962 if (!atomic_inc_not_zero(&mm->mm_users))
963 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700964 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700965 size_t this_len, max_len;
966 int retval;
967
968 if (src >= (mm->env_end - mm->env_start))
969 break;
James Pearson315e28c2007-10-16 23:30:17 -0700970
971 this_len = mm->env_end - (mm->env_start + src);
972
Djalal Harounie8905ec2012-07-30 14:42:26 -0700973 max_len = min_t(size_t, PAGE_SIZE, count);
974 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700975
Cong Wangb409e572012-05-31 16:26:17 -0700976 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700977 page, this_len, 0);
978
979 if (retval <= 0) {
980 ret = retval;
981 break;
982 }
983
984 if (copy_to_user(buf, page, retval)) {
985 ret = -EFAULT;
986 break;
987 }
988
989 ret += retval;
990 src += retval;
991 buf += retval;
992 count -= retval;
993 }
994 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700995 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700996
997free:
James Pearson315e28c2007-10-16 23:30:17 -0700998 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700999 return ret;
1000}
1001
1002static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -07001003 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -07001004 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001005 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -07001006 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -07001007};
1008
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001009static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1010 loff_t *ppos)
1011{
Al Viro496ad9a2013-01-23 17:07:38 -05001012 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001013 char buffer[PROC_NUMBUF];
1014 int oom_adj = OOM_ADJUST_MIN;
1015 size_t len;
1016 unsigned long flags;
1017
1018 if (!task)
1019 return -ESRCH;
1020 if (lock_task_sighand(task, &flags)) {
1021 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1022 oom_adj = OOM_ADJUST_MAX;
1023 else
1024 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1025 OOM_SCORE_ADJ_MAX;
1026 unlock_task_sighand(task, &flags);
1027 }
1028 put_task_struct(task);
1029 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1030 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1031}
1032
1033static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1034 size_t count, loff_t *ppos)
1035{
1036 struct task_struct *task;
1037 char buffer[PROC_NUMBUF];
1038 int oom_adj;
1039 unsigned long flags;
1040 int err;
1041
1042 memset(buffer, 0, sizeof(buffer));
1043 if (count > sizeof(buffer) - 1)
1044 count = sizeof(buffer) - 1;
1045 if (copy_from_user(buffer, buf, count)) {
1046 err = -EFAULT;
1047 goto out;
1048 }
1049
1050 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1051 if (err)
1052 goto out;
1053 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1054 oom_adj != OOM_DISABLE) {
1055 err = -EINVAL;
1056 goto out;
1057 }
1058
Al Viro496ad9a2013-01-23 17:07:38 -05001059 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001060 if (!task) {
1061 err = -ESRCH;
1062 goto out;
1063 }
1064
1065 task_lock(task);
1066 if (!task->mm) {
1067 err = -EINVAL;
1068 goto err_task_lock;
1069 }
1070
1071 if (!lock_task_sighand(task, &flags)) {
1072 err = -ESRCH;
1073 goto err_task_lock;
1074 }
1075
1076 /*
1077 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1078 * value is always attainable.
1079 */
1080 if (oom_adj == OOM_ADJUST_MAX)
1081 oom_adj = OOM_SCORE_ADJ_MAX;
1082 else
1083 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1084
1085 if (oom_adj < task->signal->oom_score_adj &&
1086 !capable(CAP_SYS_RESOURCE)) {
1087 err = -EACCES;
1088 goto err_sighand;
1089 }
1090
1091 /*
1092 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1093 * /proc/pid/oom_score_adj instead.
1094 */
Andrew Morton87ebdc02013-02-27 17:03:16 -08001095 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 -08001096 current->comm, task_pid_nr(current), task_pid_nr(task),
1097 task_pid_nr(task));
1098
1099 task->signal->oom_score_adj = oom_adj;
1100 trace_oom_score_adj_update(task);
1101err_sighand:
1102 unlock_task_sighand(task, &flags);
1103err_task_lock:
1104 task_unlock(task);
1105 put_task_struct(task);
1106out:
1107 return err < 0 ? err : count;
1108}
1109
1110static const struct file_operations proc_oom_adj_operations = {
1111 .read = oom_adj_read,
1112 .write = oom_adj_write,
1113 .llseek = generic_file_llseek,
1114};
1115
David Rientjesa63d83f2010-08-09 17:19:46 -07001116static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1117 size_t count, loff_t *ppos)
1118{
Al Viro496ad9a2013-01-23 17:07:38 -05001119 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001120 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001121 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001122 unsigned long flags;
1123 size_t len;
1124
1125 if (!task)
1126 return -ESRCH;
1127 if (lock_task_sighand(task, &flags)) {
1128 oom_score_adj = task->signal->oom_score_adj;
1129 unlock_task_sighand(task, &flags);
1130 }
1131 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001132 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001133 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1134}
1135
1136static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1137 size_t count, loff_t *ppos)
1138{
1139 struct task_struct *task;
1140 char buffer[PROC_NUMBUF];
1141 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001142 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001143 int err;
1144
1145 memset(buffer, 0, sizeof(buffer));
1146 if (count > sizeof(buffer) - 1)
1147 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001148 if (copy_from_user(buffer, buf, count)) {
1149 err = -EFAULT;
1150 goto out;
1151 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001152
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001153 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001154 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001155 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001156 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001157 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1158 err = -EINVAL;
1159 goto out;
1160 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001161
Al Viro496ad9a2013-01-23 17:07:38 -05001162 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001163 if (!task) {
1164 err = -ESRCH;
1165 goto out;
1166 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001167
Ying Han3d5992d2010-10-26 14:21:23 -07001168 task_lock(task);
1169 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001170 err = -EINVAL;
1171 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001172 }
David Rientjesd19d5472010-10-26 14:21:26 -07001173
1174 if (!lock_task_sighand(task, &flags)) {
1175 err = -ESRCH;
1176 goto err_task_lock;
1177 }
1178
David Rientjesa9c58b902012-12-11 16:02:54 -08001179 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001180 !capable(CAP_SYS_RESOURCE)) {
1181 err = -EACCES;
1182 goto err_sighand;
1183 }
1184
David Rientjesa9c58b902012-12-11 16:02:54 -08001185 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001186 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001187 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001188 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001189
David Rientjes723548b2010-10-26 14:21:25 -07001190err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001191 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001192err_task_lock:
1193 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001194 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001195out:
1196 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001197}
1198
1199static const struct file_operations proc_oom_score_adj_operations = {
1200 .read = oom_score_adj_read,
1201 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001202 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001203};
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205#ifdef CONFIG_AUDITSYSCALL
1206#define TMPBUFLEN 21
1207static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1208 size_t count, loff_t *ppos)
1209{
Al Viro496ad9a2013-01-23 17:07:38 -05001210 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001211 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ssize_t length;
1213 char tmpbuf[TMPBUFLEN];
1214
Eric W. Biederman99f89552006-06-26 00:25:55 -07001215 if (!task)
1216 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001218 from_kuid(file->f_cred->user_ns,
1219 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001220 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1222}
1223
1224static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1225 size_t count, loff_t *ppos)
1226{
Al Viro496ad9a2013-01-23 17:07:38 -05001227 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 char *page, *tmp;
1229 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001231 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001233 rcu_read_lock();
1234 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1235 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001237 }
1238 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Al Viroe0182902006-05-18 08:28:02 -04001240 if (count >= PAGE_SIZE)
1241 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (*ppos != 0) {
1244 /* No partial writes. */
1245 return -EINVAL;
1246 }
Mel Gormane12ba742007-10-16 01:25:52 -07001247 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (!page)
1249 return -ENOMEM;
1250 length = -EFAULT;
1251 if (copy_from_user(page, buf, count))
1252 goto out_free_page;
1253
Al Viroe0182902006-05-18 08:28:02 -04001254 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 loginuid = simple_strtoul(page, &tmp, 10);
1256 if (tmp == page) {
1257 length = -EINVAL;
1258 goto out_free_page;
1259
1260 }
Eric Paris81407c82013-05-24 09:49:14 -04001261
1262 /* is userspace tring to explicitly UNSET the loginuid? */
1263 if (loginuid == AUDIT_UID_UNSET) {
1264 kloginuid = INVALID_UID;
1265 } else {
1266 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1267 if (!uid_valid(kloginuid)) {
1268 length = -EINVAL;
1269 goto out_free_page;
1270 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001271 }
1272
1273 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (likely(length == 0))
1275 length = count;
1276
1277out_free_page:
1278 free_page((unsigned long) page);
1279 return length;
1280}
1281
Arjan van de Ven00977a52007-02-12 00:55:34 -08001282static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 .read = proc_loginuid_read,
1284 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001285 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286};
Eric Paris1e0bd752008-03-13 08:15:31 -04001287
1288static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1289 size_t count, loff_t *ppos)
1290{
Al Viro496ad9a2013-01-23 17:07:38 -05001291 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001292 struct task_struct *task = get_proc_task(inode);
1293 ssize_t length;
1294 char tmpbuf[TMPBUFLEN];
1295
1296 if (!task)
1297 return -ESRCH;
1298 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1299 audit_get_sessionid(task));
1300 put_task_struct(task);
1301 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1302}
1303
1304static const struct file_operations proc_sessionid_operations = {
1305 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001306 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001307};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308#endif
1309
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001310#ifdef CONFIG_FAULT_INJECTION
1311static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1312 size_t count, loff_t *ppos)
1313{
Al Viro496ad9a2013-01-23 17:07:38 -05001314 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001315 char buffer[PROC_NUMBUF];
1316 size_t len;
1317 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001318
1319 if (!task)
1320 return -ESRCH;
1321 make_it_fail = task->make_it_fail;
1322 put_task_struct(task);
1323
1324 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001325
1326 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001327}
1328
1329static ssize_t proc_fault_inject_write(struct file * file,
1330 const char __user * buf, size_t count, loff_t *ppos)
1331{
1332 struct task_struct *task;
1333 char buffer[PROC_NUMBUF], *end;
1334 int make_it_fail;
1335
1336 if (!capable(CAP_SYS_RESOURCE))
1337 return -EPERM;
1338 memset(buffer, 0, sizeof(buffer));
1339 if (count > sizeof(buffer) - 1)
1340 count = sizeof(buffer) - 1;
1341 if (copy_from_user(buffer, buf, count))
1342 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001343 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1344 if (*end)
1345 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001346 if (make_it_fail < 0 || make_it_fail > 1)
1347 return -EINVAL;
1348
Al Viro496ad9a2013-01-23 17:07:38 -05001349 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001350 if (!task)
1351 return -ESRCH;
1352 task->make_it_fail = make_it_fail;
1353 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001354
1355 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001356}
1357
Arjan van de Ven00977a52007-02-12 00:55:34 -08001358static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001359 .read = proc_fault_inject_read,
1360 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001361 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001362};
1363#endif
1364
Arjan van de Ven97455122008-01-25 21:08:34 +01001365
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001366#ifdef CONFIG_SCHED_DEBUG
1367/*
1368 * Print out various scheduling related per-task fields:
1369 */
1370static int sched_show(struct seq_file *m, void *v)
1371{
1372 struct inode *inode = m->private;
1373 struct task_struct *p;
1374
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001375 p = get_proc_task(inode);
1376 if (!p)
1377 return -ESRCH;
1378 proc_sched_show_task(p, m);
1379
1380 put_task_struct(p);
1381
1382 return 0;
1383}
1384
1385static ssize_t
1386sched_write(struct file *file, const char __user *buf,
1387 size_t count, loff_t *offset)
1388{
Al Viro496ad9a2013-01-23 17:07:38 -05001389 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001390 struct task_struct *p;
1391
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001392 p = get_proc_task(inode);
1393 if (!p)
1394 return -ESRCH;
1395 proc_sched_set_task(p);
1396
1397 put_task_struct(p);
1398
1399 return count;
1400}
1401
1402static int sched_open(struct inode *inode, struct file *filp)
1403{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001404 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001405}
1406
1407static const struct file_operations proc_pid_sched_operations = {
1408 .open = sched_open,
1409 .read = seq_read,
1410 .write = sched_write,
1411 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001412 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001413};
1414
1415#endif
1416
Mike Galbraith5091faa2010-11-30 14:18:03 +01001417#ifdef CONFIG_SCHED_AUTOGROUP
1418/*
1419 * Print out autogroup related information:
1420 */
1421static int sched_autogroup_show(struct seq_file *m, void *v)
1422{
1423 struct inode *inode = m->private;
1424 struct task_struct *p;
1425
1426 p = get_proc_task(inode);
1427 if (!p)
1428 return -ESRCH;
1429 proc_sched_autogroup_show_task(p, m);
1430
1431 put_task_struct(p);
1432
1433 return 0;
1434}
1435
1436static ssize_t
1437sched_autogroup_write(struct file *file, const char __user *buf,
1438 size_t count, loff_t *offset)
1439{
Al Viro496ad9a2013-01-23 17:07:38 -05001440 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001441 struct task_struct *p;
1442 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001443 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001444 int err;
1445
1446 memset(buffer, 0, sizeof(buffer));
1447 if (count > sizeof(buffer) - 1)
1448 count = sizeof(buffer) - 1;
1449 if (copy_from_user(buffer, buf, count))
1450 return -EFAULT;
1451
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001452 err = kstrtoint(strstrip(buffer), 0, &nice);
1453 if (err < 0)
1454 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001455
1456 p = get_proc_task(inode);
1457 if (!p)
1458 return -ESRCH;
1459
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001460 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001461 if (err)
1462 count = err;
1463
1464 put_task_struct(p);
1465
1466 return count;
1467}
1468
1469static int sched_autogroup_open(struct inode *inode, struct file *filp)
1470{
1471 int ret;
1472
1473 ret = single_open(filp, sched_autogroup_show, NULL);
1474 if (!ret) {
1475 struct seq_file *m = filp->private_data;
1476
1477 m->private = inode;
1478 }
1479 return ret;
1480}
1481
1482static const struct file_operations proc_pid_sched_autogroup_operations = {
1483 .open = sched_autogroup_open,
1484 .read = seq_read,
1485 .write = sched_autogroup_write,
1486 .llseek = seq_lseek,
1487 .release = single_release,
1488};
1489
1490#endif /* CONFIG_SCHED_AUTOGROUP */
1491
john stultz4614a696b2009-12-14 18:00:05 -08001492static ssize_t comm_write(struct file *file, const char __user *buf,
1493 size_t count, loff_t *offset)
1494{
Al Viro496ad9a2013-01-23 17:07:38 -05001495 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001496 struct task_struct *p;
1497 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001498 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001499
1500 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001501 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001502 return -EFAULT;
1503
1504 p = get_proc_task(inode);
1505 if (!p)
1506 return -ESRCH;
1507
1508 if (same_thread_group(current, p))
1509 set_task_comm(p, buffer);
1510 else
1511 count = -EINVAL;
1512
1513 put_task_struct(p);
1514
1515 return count;
1516}
1517
1518static int comm_show(struct seq_file *m, void *v)
1519{
1520 struct inode *inode = m->private;
1521 struct task_struct *p;
1522
1523 p = get_proc_task(inode);
1524 if (!p)
1525 return -ESRCH;
1526
1527 task_lock(p);
1528 seq_printf(m, "%s\n", p->comm);
1529 task_unlock(p);
1530
1531 put_task_struct(p);
1532
1533 return 0;
1534}
1535
1536static int comm_open(struct inode *inode, struct file *filp)
1537{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001538 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001539}
1540
1541static const struct file_operations proc_pid_set_comm_operations = {
1542 .open = comm_open,
1543 .read = seq_read,
1544 .write = comm_write,
1545 .llseek = seq_lseek,
1546 .release = single_release,
1547};
1548
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001549static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001550{
1551 struct task_struct *task;
1552 struct mm_struct *mm;
1553 struct file *exe_file;
1554
David Howells2b0143b2015-03-17 22:25:59 +00001555 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001556 if (!task)
1557 return -ENOENT;
1558 mm = get_task_mm(task);
1559 put_task_struct(task);
1560 if (!mm)
1561 return -ENOENT;
1562 exe_file = get_mm_exe_file(mm);
1563 mmput(mm);
1564 if (exe_file) {
1565 *exe_path = exe_file->f_path;
1566 path_get(&exe_file->f_path);
1567 fput(exe_file);
1568 return 0;
1569 } else
1570 return -ENOENT;
1571}
1572
Al Viro6e77137b2015-05-02 13:37:52 -04001573static const char *proc_pid_follow_link(struct dentry *dentry, void **cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
David Howells2b0143b2015-03-17 22:25:59 +00001575 struct inode *inode = d_inode(dentry);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001576 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 int error = -EACCES;
1578
Eric W. Biederman778c1142006-06-26 00:25:58 -07001579 /* Are we allowed to snoop on the tasks file descriptors? */
1580 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Christoph Hellwig408ef012012-06-18 10:47:03 -04001583 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1584 if (error)
1585 goto out;
1586
Al Viro6e77137b2015-05-02 13:37:52 -04001587 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589out:
Al Viro008b1502005-08-20 00:17:39 +01001590 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001593static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Mel Gormane12ba742007-10-16 01:25:52 -07001595 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001596 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 int len;
1598
1599 if (!tmp)
1600 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001601
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001602 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001603 len = PTR_ERR(pathname);
1604 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001606 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 if (len > buflen)
1609 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001610 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 len = -EFAULT;
1612 out:
1613 free_page((unsigned long)tmp);
1614 return len;
1615}
1616
1617static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1618{
1619 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001620 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001621 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Eric W. Biederman778c1142006-06-26 00:25:58 -07001623 /* Are we allowed to snoop on the tasks file descriptors? */
1624 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001627 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (error)
1629 goto out;
1630
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001631 error = do_proc_readlink(&path, buffer, buflen);
1632 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return error;
1635}
1636
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001637const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001639 .follow_link = proc_pid_follow_link,
1640 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641};
1642
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001643
1644/* building an inode */
1645
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001646struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001647{
1648 struct inode * inode;
1649 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001650 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651
1652 /* We need a new inode */
1653
1654 inode = new_inode(sb);
1655 if (!inode)
1656 goto out;
1657
1658 /* Common stuff */
1659 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001660 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001661 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001662 inode->i_op = &proc_def_inode_operations;
1663
1664 /*
1665 * grab the reference to task.
1666 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001667 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001668 if (!ei->pid)
1669 goto out_unlock;
1670
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001671 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001672 rcu_read_lock();
1673 cred = __task_cred(task);
1674 inode->i_uid = cred->euid;
1675 inode->i_gid = cred->egid;
1676 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001677 }
1678 security_task_to_inode(task, inode);
1679
1680out:
1681 return inode;
1682
1683out_unlock:
1684 iput(inode);
1685 return NULL;
1686}
1687
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001688int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001689{
David Howells2b0143b2015-03-17 22:25:59 +00001690 struct inode *inode = d_inode(dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001691 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001692 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001693 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001694
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001695 generic_fillattr(inode, stat);
1696
1697 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001698 stat->uid = GLOBAL_ROOT_UID;
1699 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001700 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1701 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001702 if (!has_pid_permissions(pid, task, 2)) {
1703 rcu_read_unlock();
1704 /*
1705 * This doesn't prevent learning whether PID exists,
1706 * it only makes getattr() consistent with readdir().
1707 */
1708 return -ENOENT;
1709 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001710 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1711 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001712 cred = __task_cred(task);
1713 stat->uid = cred->euid;
1714 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001715 }
1716 }
1717 rcu_read_unlock();
1718 return 0;
1719}
1720
1721/* dentry stuff */
1722
1723/*
1724 * Exceptional case: normally we are not allowed to unhash a busy
1725 * directory. In this case, however, we can do it - no aliasing problems
1726 * due to the way we treat inodes.
1727 *
1728 * Rewrite the inode's ownerships here because the owning task may have
1729 * performed a setuid(), etc.
1730 *
1731 * Before the /proc/pid/status file was created the only way to read
1732 * the effective uid of a /process was to stat /proc/pid. Reading
1733 * /proc/pid/status is slow enough that procps and other packages
1734 * kept stating /proc/pid. To keep the rules in /proc simple I have
1735 * made this apply to all per process world readable and executable
1736 * directories.
1737 */
Al Viro0b728e12012-06-10 16:03:43 -04001738int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739{
Nick Piggin34286d62011-01-07 17:49:57 +11001740 struct inode *inode;
1741 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001742 const struct cred *cred;
1743
Al Viro0b728e12012-06-10 16:03:43 -04001744 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001745 return -ECHILD;
1746
David Howells2b0143b2015-03-17 22:25:59 +00001747 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001748 task = get_proc_task(inode);
1749
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001750 if (task) {
1751 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1752 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001753 rcu_read_lock();
1754 cred = __task_cred(task);
1755 inode->i_uid = cred->euid;
1756 inode->i_gid = cred->egid;
1757 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001758 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001759 inode->i_uid = GLOBAL_ROOT_UID;
1760 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761 }
1762 inode->i_mode &= ~(S_ISUID | S_ISGID);
1763 security_task_to_inode(task, inode);
1764 put_task_struct(task);
1765 return 1;
1766 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001767 return 0;
1768}
1769
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001770static inline bool proc_inode_is_dead(struct inode *inode)
1771{
1772 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1773}
1774
David Howells1dd704b2013-04-12 01:08:50 +01001775int pid_delete_dentry(const struct dentry *dentry)
1776{
1777 /* Is the task we represent dead?
1778 * If so, then don't put the dentry on the lru list,
1779 * kill it immediately.
1780 */
David Howells2b0143b2015-03-17 22:25:59 +00001781 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001782}
1783
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001784const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001785{
1786 .d_revalidate = pid_revalidate,
1787 .d_delete = pid_delete_dentry,
1788};
1789
1790/* Lookups */
1791
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001792/*
1793 * Fill a directory entry.
1794 *
1795 * If possible create the dcache entry and derive our inode number and
1796 * file type from dcache entry.
1797 *
1798 * Since all of the proc inode numbers are dynamically generated, the inode
1799 * numbers do not exist until the inode is cache. This means creating the
1800 * the dcache entry in readdir is necessary to keep the inode numbers
1801 * reported by readdir in sync with the inode numbers reported
1802 * by stat.
1803 */
Al Virof0c3b502013-05-16 12:07:31 -04001804bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001805 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001806 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001807{
Al Virof0c3b502013-05-16 12:07:31 -04001808 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001809 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001810 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001811 unsigned type;
1812 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001813
Al Viro1df98b82013-06-15 11:33:10 +04001814 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001815 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001816 child = d_alloc(dir, &qname);
1817 if (!child)
1818 goto end_instantiate;
David Howells2b0143b2015-03-17 22:25:59 +00001819 if (instantiate(d_inode(dir), child, task, ptr) < 0) {
Al Viro1df98b82013-06-15 11:33:10 +04001820 dput(child);
1821 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001822 }
1823 }
David Howells2b0143b2015-03-17 22:25:59 +00001824 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001825 ino = inode->i_ino;
1826 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001827 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001828 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001829
1830end_instantiate:
1831 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001832}
1833
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001834#ifdef CONFIG_CHECKPOINT_RESTORE
1835
1836/*
1837 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1838 * which represent vma start and end addresses.
1839 */
1840static int dname_to_vma_addr(struct dentry *dentry,
1841 unsigned long *start, unsigned long *end)
1842{
1843 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1844 return -EINVAL;
1845
1846 return 0;
1847}
1848
Al Viro0b728e12012-06-10 16:03:43 -04001849static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001850{
1851 unsigned long vm_start, vm_end;
1852 bool exact_vma_exists = false;
1853 struct mm_struct *mm = NULL;
1854 struct task_struct *task;
1855 const struct cred *cred;
1856 struct inode *inode;
1857 int status = 0;
1858
Al Viro0b728e12012-06-10 16:03:43 -04001859 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001860 return -ECHILD;
1861
1862 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001863 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001864 goto out_notask;
1865 }
1866
David Howells2b0143b2015-03-17 22:25:59 +00001867 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001868 task = get_proc_task(inode);
1869 if (!task)
1870 goto out_notask;
1871
Cong Wang2344bec2012-05-31 16:26:18 -07001872 mm = mm_access(task, PTRACE_MODE_READ);
1873 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001874 goto out;
1875
1876 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1877 down_read(&mm->mmap_sem);
1878 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1879 up_read(&mm->mmap_sem);
1880 }
1881
1882 mmput(mm);
1883
1884 if (exact_vma_exists) {
1885 if (task_dumpable(task)) {
1886 rcu_read_lock();
1887 cred = __task_cred(task);
1888 inode->i_uid = cred->euid;
1889 inode->i_gid = cred->egid;
1890 rcu_read_unlock();
1891 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001892 inode->i_uid = GLOBAL_ROOT_UID;
1893 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001894 }
1895 security_task_to_inode(task, inode);
1896 status = 1;
1897 }
1898
1899out:
1900 put_task_struct(task);
1901
1902out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001903 return status;
1904}
1905
1906static const struct dentry_operations tid_map_files_dentry_operations = {
1907 .d_revalidate = map_files_d_revalidate,
1908 .d_delete = pid_delete_dentry,
1909};
1910
1911static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1912{
1913 unsigned long vm_start, vm_end;
1914 struct vm_area_struct *vma;
1915 struct task_struct *task;
1916 struct mm_struct *mm;
1917 int rc;
1918
1919 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001920 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001921 if (!task)
1922 goto out;
1923
1924 mm = get_task_mm(task);
1925 put_task_struct(task);
1926 if (!mm)
1927 goto out;
1928
1929 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1930 if (rc)
1931 goto out_mmput;
1932
Artem Fetishev70335ab2014-03-10 15:49:45 -07001933 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001934 down_read(&mm->mmap_sem);
1935 vma = find_exact_vma(mm, vm_start, vm_end);
1936 if (vma && vma->vm_file) {
1937 *path = vma->vm_file->f_path;
1938 path_get(path);
1939 rc = 0;
1940 }
1941 up_read(&mm->mmap_sem);
1942
1943out_mmput:
1944 mmput(mm);
1945out:
1946 return rc;
1947}
1948
1949struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001950 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001951 unsigned long len;
1952 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1953};
1954
Al Viroc52a47a2013-06-15 11:15:20 +04001955static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001956proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1957 struct task_struct *task, const void *ptr)
1958{
Al Viro7b540d02012-08-27 14:55:26 -04001959 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001960 struct proc_inode *ei;
1961 struct inode *inode;
1962
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001963 inode = proc_pid_make_inode(dir->i_sb, task);
1964 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001965 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001966
1967 ei = PROC_I(inode);
1968 ei->op.proc_get_link = proc_map_files_get_link;
1969
1970 inode->i_op = &proc_pid_link_inode_operations;
1971 inode->i_size = 64;
1972 inode->i_mode = S_IFLNK;
1973
Al Viro7b540d02012-08-27 14:55:26 -04001974 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001975 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001976 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001977 inode->i_mode |= S_IWUSR;
1978
1979 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1980 d_add(dentry, inode);
1981
Al Viroc52a47a2013-06-15 11:15:20 +04001982 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001983}
1984
1985static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001986 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001987{
1988 unsigned long vm_start, vm_end;
1989 struct vm_area_struct *vma;
1990 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001991 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001992 struct mm_struct *mm;
1993
Al Viroc52a47a2013-06-15 11:15:20 +04001994 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001995 if (!capable(CAP_SYS_ADMIN))
1996 goto out;
1997
Al Viroc52a47a2013-06-15 11:15:20 +04001998 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001999 task = get_proc_task(dir);
2000 if (!task)
2001 goto out;
2002
Al Viroc52a47a2013-06-15 11:15:20 +04002003 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002004 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002005 goto out_put_task;
2006
Al Viroc52a47a2013-06-15 11:15:20 +04002007 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002008 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002009 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002010
2011 mm = get_task_mm(task);
2012 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002013 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002014
2015 down_read(&mm->mmap_sem);
2016 vma = find_exact_vma(mm, vm_start, vm_end);
2017 if (!vma)
2018 goto out_no_vma;
2019
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002020 if (vma->vm_file)
2021 result = proc_map_files_instantiate(dir, dentry, task,
2022 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002023
2024out_no_vma:
2025 up_read(&mm->mmap_sem);
2026 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002027out_put_task:
2028 put_task_struct(task);
2029out:
Al Viroc52a47a2013-06-15 11:15:20 +04002030 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002031}
2032
2033static const struct inode_operations proc_map_files_inode_operations = {
2034 .lookup = proc_map_files_lookup,
2035 .permission = proc_fd_permission,
2036 .setattr = proc_setattr,
2037};
2038
2039static int
Al Virof0c3b502013-05-16 12:07:31 -04002040proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002041{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002042 struct vm_area_struct *vma;
2043 struct task_struct *task;
2044 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002045 unsigned long nr_files, pos, i;
2046 struct flex_array *fa = NULL;
2047 struct map_files_info info;
2048 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002049 int ret;
2050
Zhao Hongjiang41735812013-02-20 13:13:55 +11002051 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002052 if (!capable(CAP_SYS_ADMIN))
2053 goto out;
2054
2055 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002056 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002057 if (!task)
2058 goto out;
2059
2060 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002061 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002062 goto out_put_task;
2063
2064 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002065 if (!dir_emit_dots(file, ctx))
2066 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002067
Al Virof0c3b502013-05-16 12:07:31 -04002068 mm = get_task_mm(task);
2069 if (!mm)
2070 goto out_put_task;
2071 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072
Al Virof0c3b502013-05-16 12:07:31 -04002073 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002074
Al Virof0c3b502013-05-16 12:07:31 -04002075 /*
2076 * We need two passes here:
2077 *
2078 * 1) Collect vmas of mapped files with mmap_sem taken
2079 * 2) Release mmap_sem and instantiate entries
2080 *
2081 * otherwise we get lockdep complained, since filldir()
2082 * routine might require mmap_sem taken in might_fault().
2083 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002084
Al Virof0c3b502013-05-16 12:07:31 -04002085 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2086 if (vma->vm_file && ++pos > ctx->pos)
2087 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002088 }
Al Virof0c3b502013-05-16 12:07:31 -04002089
2090 if (nr_files) {
2091 fa = flex_array_alloc(sizeof(info), nr_files,
2092 GFP_KERNEL);
2093 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2094 GFP_KERNEL)) {
2095 ret = -ENOMEM;
2096 if (fa)
2097 flex_array_free(fa);
2098 up_read(&mm->mmap_sem);
2099 mmput(mm);
2100 goto out_put_task;
2101 }
2102 for (i = 0, vma = mm->mmap, pos = 2; vma;
2103 vma = vma->vm_next) {
2104 if (!vma->vm_file)
2105 continue;
2106 if (++pos <= ctx->pos)
2107 continue;
2108
2109 info.mode = vma->vm_file->f_mode;
2110 info.len = snprintf(info.name,
2111 sizeof(info.name), "%lx-%lx",
2112 vma->vm_start, vma->vm_end);
2113 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2114 BUG();
2115 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002116 }
Al Virof0c3b502013-05-16 12:07:31 -04002117 up_read(&mm->mmap_sem);
2118
2119 for (i = 0; i < nr_files; i++) {
2120 p = flex_array_get(fa, i);
2121 if (!proc_fill_cache(file, ctx,
2122 p->name, p->len,
2123 proc_map_files_instantiate,
2124 task,
2125 (void *)(unsigned long)p->mode))
2126 break;
2127 ctx->pos++;
2128 }
2129 if (fa)
2130 flex_array_free(fa);
2131 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002132
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002133out_put_task:
2134 put_task_struct(task);
2135out:
2136 return ret;
2137}
2138
2139static const struct file_operations proc_map_files_operations = {
2140 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002141 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002142 .llseek = default_llseek,
2143};
2144
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002145struct timers_private {
2146 struct pid *pid;
2147 struct task_struct *task;
2148 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002149 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002150 unsigned long flags;
2151};
2152
2153static void *timers_start(struct seq_file *m, loff_t *pos)
2154{
2155 struct timers_private *tp = m->private;
2156
2157 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2158 if (!tp->task)
2159 return ERR_PTR(-ESRCH);
2160
2161 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2162 if (!tp->sighand)
2163 return ERR_PTR(-ESRCH);
2164
2165 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2166}
2167
2168static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2169{
2170 struct timers_private *tp = m->private;
2171 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2172}
2173
2174static void timers_stop(struct seq_file *m, void *v)
2175{
2176 struct timers_private *tp = m->private;
2177
2178 if (tp->sighand) {
2179 unlock_task_sighand(tp->task, &tp->flags);
2180 tp->sighand = NULL;
2181 }
2182
2183 if (tp->task) {
2184 put_task_struct(tp->task);
2185 tp->task = NULL;
2186 }
2187}
2188
2189static int show_timer(struct seq_file *m, void *v)
2190{
2191 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002192 struct timers_private *tp = m->private;
2193 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002194 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002195 [SIGEV_SIGNAL] = "signal",
2196 [SIGEV_NONE] = "none",
2197 [SIGEV_THREAD] = "thread",
2198 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002199
2200 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002201 notify = timer->it_sigev_notify;
2202
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002203 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002204 seq_printf(m, "signal: %d/%p\n",
2205 timer->sigq->info.si_signo,
2206 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002207 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002208 nstr[notify & ~SIGEV_THREAD_ID],
2209 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2210 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002211 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002212
2213 return 0;
2214}
2215
2216static const struct seq_operations proc_timers_seq_ops = {
2217 .start = timers_start,
2218 .next = timers_next,
2219 .stop = timers_stop,
2220 .show = show_timer,
2221};
2222
2223static int proc_timers_open(struct inode *inode, struct file *file)
2224{
2225 struct timers_private *tp;
2226
2227 tp = __seq_open_private(file, &proc_timers_seq_ops,
2228 sizeof(struct timers_private));
2229 if (!tp)
2230 return -ENOMEM;
2231
2232 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002233 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002234 return 0;
2235}
2236
2237static const struct file_operations proc_timers_operations = {
2238 .open = proc_timers_open,
2239 .read = seq_read,
2240 .llseek = seq_lseek,
2241 .release = seq_release_private,
2242};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002243#endif /* CONFIG_CHECKPOINT_RESTORE */
2244
Al Viroc52a47a2013-06-15 11:15:20 +04002245static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002246 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002247{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002248 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002249 struct inode *inode;
2250 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002251
Eric W. Biederman61a28782006-10-02 02:18:49 -07002252 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002253 if (!inode)
2254 goto out;
2255
2256 ei = PROC_I(inode);
2257 inode->i_mode = p->mode;
2258 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002259 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002260 if (p->iop)
2261 inode->i_op = p->iop;
2262 if (p->fop)
2263 inode->i_fop = p->fop;
2264 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002265 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002266 d_add(dentry, inode);
2267 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002268 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002269 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002270out:
Al Viroc52a47a2013-06-15 11:15:20 +04002271 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002272}
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274static struct dentry *proc_pident_lookup(struct inode *dir,
2275 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002276 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002277 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Al Viroc52a47a2013-06-15 11:15:20 +04002279 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002280 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002281 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Al Viroc52a47a2013-06-15 11:15:20 +04002283 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Eric W. Biederman99f89552006-06-26 00:25:55 -07002285 if (!task)
2286 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002288 /*
2289 * Yes, it does not scale. And it should not. Don't add
2290 * new entries into /proc/<tgid>/ without very good reasons.
2291 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002292 last = &ents[nents - 1];
2293 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (p->len != dentry->d_name.len)
2295 continue;
2296 if (!memcmp(dentry->d_name.name, p->name, p->len))
2297 break;
2298 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002299 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto out;
2301
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002302 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002304 put_task_struct(task);
2305out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002306 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Al Virof0c3b502013-05-16 12:07:31 -04002309static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002310 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002311{
Al Virof0c3b502013-05-16 12:07:31 -04002312 struct task_struct *task = get_proc_task(file_inode(file));
2313 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002314
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002315 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002316 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002317
Al Virof0c3b502013-05-16 12:07:31 -04002318 if (!dir_emit_dots(file, ctx))
2319 goto out;
2320
2321 if (ctx->pos >= nents + 2)
2322 goto out;
2323
2324 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2325 if (!proc_fill_cache(file, ctx, p->name, p->len,
2326 proc_pident_instantiate, task, p))
2327 break;
2328 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002329 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002330out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002331 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002336static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2337 size_t count, loff_t *ppos)
2338{
Al Viro496ad9a2013-01-23 17:07:38 -05002339 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002340 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002341 ssize_t length;
2342 struct task_struct *task = get_proc_task(inode);
2343
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002344 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002345 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002346
2347 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002348 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002349 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002350 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002351 if (length > 0)
2352 length = simple_read_from_buffer(buf, count, ppos, p, length);
2353 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002354 return length;
2355}
2356
2357static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2358 size_t count, loff_t *ppos)
2359{
Al Viro496ad9a2013-01-23 17:07:38 -05002360 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002361 char *page;
2362 ssize_t length;
2363 struct task_struct *task = get_proc_task(inode);
2364
2365 length = -ESRCH;
2366 if (!task)
2367 goto out_no_task;
2368 if (count > PAGE_SIZE)
2369 count = PAGE_SIZE;
2370
2371 /* No partial writes. */
2372 length = -EINVAL;
2373 if (*ppos != 0)
2374 goto out;
2375
2376 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002377 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002378 if (!page)
2379 goto out;
2380
2381 length = -EFAULT;
2382 if (copy_from_user(page, buf, count))
2383 goto out_free;
2384
David Howells107db7c2009-05-08 13:55:27 +01002385 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002386 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002387 if (length < 0)
2388 goto out_free;
2389
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002390 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002391 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002392 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002393 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002394out_free:
2395 free_page((unsigned long) page);
2396out:
2397 put_task_struct(task);
2398out_no_task:
2399 return length;
2400}
2401
Arjan van de Ven00977a52007-02-12 00:55:34 -08002402static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002403 .read = proc_pid_attr_read,
2404 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002405 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002406};
2407
Eric Dumazetc5141e62007-05-08 00:26:15 -07002408static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002409 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2410 REG("prev", S_IRUGO, proc_pid_attr_operations),
2411 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2412 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2413 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2414 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002415};
2416
Al Virof0c3b502013-05-16 12:07:31 -04002417static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Al Virof0c3b502013-05-16 12:07:31 -04002419 return proc_pident_readdir(file, ctx,
2420 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Arjan van de Ven00977a52007-02-12 00:55:34 -08002423static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002425 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002426 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427};
2428
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002429static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002430 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002432 return proc_pident_lookup(dir, dentry,
2433 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002436static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002437 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002438 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002439 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440};
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442#endif
2443
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002444#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002445static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2446 size_t count, loff_t *ppos)
2447{
Al Viro496ad9a2013-01-23 17:07:38 -05002448 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002449 struct mm_struct *mm;
2450 char buffer[PROC_NUMBUF];
2451 size_t len;
2452 int ret;
2453
2454 if (!task)
2455 return -ESRCH;
2456
2457 ret = 0;
2458 mm = get_task_mm(task);
2459 if (mm) {
2460 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2461 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2462 MMF_DUMP_FILTER_SHIFT));
2463 mmput(mm);
2464 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2465 }
2466
2467 put_task_struct(task);
2468
2469 return ret;
2470}
2471
2472static ssize_t proc_coredump_filter_write(struct file *file,
2473 const char __user *buf,
2474 size_t count,
2475 loff_t *ppos)
2476{
2477 struct task_struct *task;
2478 struct mm_struct *mm;
2479 char buffer[PROC_NUMBUF], *end;
2480 unsigned int val;
2481 int ret;
2482 int i;
2483 unsigned long mask;
2484
2485 ret = -EFAULT;
2486 memset(buffer, 0, sizeof(buffer));
2487 if (count > sizeof(buffer) - 1)
2488 count = sizeof(buffer) - 1;
2489 if (copy_from_user(buffer, buf, count))
2490 goto out_no_task;
2491
2492 ret = -EINVAL;
2493 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2494 if (*end == '\n')
2495 end++;
2496 if (end - buffer == 0)
2497 goto out_no_task;
2498
2499 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002500 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002501 if (!task)
2502 goto out_no_task;
2503
2504 ret = end - buffer;
2505 mm = get_task_mm(task);
2506 if (!mm)
2507 goto out_no_mm;
2508
2509 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2510 if (val & mask)
2511 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2512 else
2513 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2514 }
2515
2516 mmput(mm);
2517 out_no_mm:
2518 put_task_struct(task);
2519 out_no_task:
2520 return ret;
2521}
2522
2523static const struct file_operations proc_coredump_filter_operations = {
2524 .read = proc_coredump_filter_read,
2525 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002526 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002527};
2528#endif
2529
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002530#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002531static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002532{
Andrea Righi940389b2008-07-28 00:48:12 +02002533 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002534 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002535 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002536
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002537 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2538 if (result)
2539 return result;
2540
2541 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2542 result = -EACCES;
2543 goto out_unlock;
2544 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002545
Andrea Righi59954772008-07-27 17:29:15 +02002546 if (whole && lock_task_sighand(task, &flags)) {
2547 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002548
Andrea Righi59954772008-07-27 17:29:15 +02002549 task_io_accounting_add(&acct, &task->signal->ioac);
2550 while_each_thread(task, t)
2551 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002552
Andrea Righi59954772008-07-27 17:29:15 +02002553 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002554 }
Joe Perches25ce3192015-04-15 16:18:17 -07002555 seq_printf(m,
2556 "rchar: %llu\n"
2557 "wchar: %llu\n"
2558 "syscr: %llu\n"
2559 "syscw: %llu\n"
2560 "read_bytes: %llu\n"
2561 "write_bytes: %llu\n"
2562 "cancelled_write_bytes: %llu\n",
2563 (unsigned long long)acct.rchar,
2564 (unsigned long long)acct.wchar,
2565 (unsigned long long)acct.syscr,
2566 (unsigned long long)acct.syscw,
2567 (unsigned long long)acct.read_bytes,
2568 (unsigned long long)acct.write_bytes,
2569 (unsigned long long)acct.cancelled_write_bytes);
2570 result = 0;
2571
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002572out_unlock:
2573 mutex_unlock(&task->signal->cred_guard_mutex);
2574 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002575}
Andrea Righi297c5d92008-07-25 01:48:49 -07002576
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002577static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2578 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002579{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002580 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002581}
2582
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002583static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2584 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002585{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002586 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002587}
2588#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002589
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002590#ifdef CONFIG_USER_NS
2591static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002592 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002593{
2594 struct user_namespace *ns = NULL;
2595 struct task_struct *task;
2596 struct seq_file *seq;
2597 int ret = -EINVAL;
2598
2599 task = get_proc_task(inode);
2600 if (task) {
2601 rcu_read_lock();
2602 ns = get_user_ns(task_cred_xxx(task, user_ns));
2603 rcu_read_unlock();
2604 put_task_struct(task);
2605 }
2606 if (!ns)
2607 goto err;
2608
2609 ret = seq_open(file, seq_ops);
2610 if (ret)
2611 goto err_put_ns;
2612
2613 seq = file->private_data;
2614 seq->private = ns;
2615
2616 return 0;
2617err_put_ns:
2618 put_user_ns(ns);
2619err:
2620 return ret;
2621}
2622
2623static int proc_id_map_release(struct inode *inode, struct file *file)
2624{
2625 struct seq_file *seq = file->private_data;
2626 struct user_namespace *ns = seq->private;
2627 put_user_ns(ns);
2628 return seq_release(inode, file);
2629}
2630
2631static int proc_uid_map_open(struct inode *inode, struct file *file)
2632{
2633 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2634}
2635
2636static int proc_gid_map_open(struct inode *inode, struct file *file)
2637{
2638 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2639}
2640
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002641static int proc_projid_map_open(struct inode *inode, struct file *file)
2642{
2643 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2644}
2645
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002646static const struct file_operations proc_uid_map_operations = {
2647 .open = proc_uid_map_open,
2648 .write = proc_uid_map_write,
2649 .read = seq_read,
2650 .llseek = seq_lseek,
2651 .release = proc_id_map_release,
2652};
2653
2654static const struct file_operations proc_gid_map_operations = {
2655 .open = proc_gid_map_open,
2656 .write = proc_gid_map_write,
2657 .read = seq_read,
2658 .llseek = seq_lseek,
2659 .release = proc_id_map_release,
2660};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002661
2662static const struct file_operations proc_projid_map_operations = {
2663 .open = proc_projid_map_open,
2664 .write = proc_projid_map_write,
2665 .read = seq_read,
2666 .llseek = seq_lseek,
2667 .release = proc_id_map_release,
2668};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002669
2670static int proc_setgroups_open(struct inode *inode, struct file *file)
2671{
2672 struct user_namespace *ns = NULL;
2673 struct task_struct *task;
2674 int ret;
2675
2676 ret = -ESRCH;
2677 task = get_proc_task(inode);
2678 if (task) {
2679 rcu_read_lock();
2680 ns = get_user_ns(task_cred_xxx(task, user_ns));
2681 rcu_read_unlock();
2682 put_task_struct(task);
2683 }
2684 if (!ns)
2685 goto err;
2686
2687 if (file->f_mode & FMODE_WRITE) {
2688 ret = -EACCES;
2689 if (!ns_capable(ns, CAP_SYS_ADMIN))
2690 goto err_put_ns;
2691 }
2692
2693 ret = single_open(file, &proc_setgroups_show, ns);
2694 if (ret)
2695 goto err_put_ns;
2696
2697 return 0;
2698err_put_ns:
2699 put_user_ns(ns);
2700err:
2701 return ret;
2702}
2703
2704static int proc_setgroups_release(struct inode *inode, struct file *file)
2705{
2706 struct seq_file *seq = file->private_data;
2707 struct user_namespace *ns = seq->private;
2708 int ret = single_release(inode, file);
2709 put_user_ns(ns);
2710 return ret;
2711}
2712
2713static const struct file_operations proc_setgroups_operations = {
2714 .open = proc_setgroups_open,
2715 .write = proc_setgroups_write,
2716 .read = seq_read,
2717 .llseek = seq_lseek,
2718 .release = proc_setgroups_release,
2719};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002720#endif /* CONFIG_USER_NS */
2721
Kees Cook47830722008-10-06 03:11:58 +04002722static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2723 struct pid *pid, struct task_struct *task)
2724{
Al Viroa9712bc2011-03-23 15:52:50 -04002725 int err = lock_trace(task);
2726 if (!err) {
2727 seq_printf(m, "%08x\n", task->personality);
2728 unlock_trace(task);
2729 }
2730 return err;
Kees Cook47830722008-10-06 03:11:58 +04002731}
2732
Eric W. Biederman801199c2006-10-02 02:18:48 -07002733/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002734 * Thread groups
2735 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002736static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002737static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002738
Eric Dumazetc5141e62007-05-08 00:26:15 -07002739static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002740 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2741 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002742#ifdef CONFIG_CHECKPOINT_RESTORE
2743 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2744#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002745 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002746 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002747#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002748 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002749#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002750 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002751 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002752 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002753 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002754 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002755#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002756 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002757#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002758#ifdef CONFIG_SCHED_AUTOGROUP
2759 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2760#endif
john stultz4614a696b2009-12-14 18:00:05 -08002761 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002762#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002763 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002764#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002765 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002766 ONE("stat", S_IRUGO, proc_tgid_stat),
2767 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002768 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002769#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002770 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002771#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002772 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2773 LNK("cwd", proc_cwd_link),
2774 LNK("root", proc_root_link),
2775 LNK("exe", proc_exe_link),
2776 REG("mounts", S_IRUGO, proc_mounts_operations),
2777 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2778 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002779#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002780 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002781 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002782 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002783#endif
2784#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002785 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002786#endif
2787#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002788 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002789#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002790#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002791 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002792#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302793#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002794 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002795#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002796#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002797 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002798#endif
Paul Menage8793d852007-10-18 23:39:39 -07002799#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002800 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002801#endif
Paul Menagea4243162007-10-18 23:39:35 -07002802#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002803 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002804#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002805 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002806 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002807 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002808#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002809 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2810 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002811#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002812#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002813 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002814#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002815#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002816 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002817#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002818#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002819 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002820#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002821#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002822 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002823#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002824#ifdef CONFIG_USER_NS
2825 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2826 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002827 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002828 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002829#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002830#ifdef CONFIG_CHECKPOINT_RESTORE
2831 REG("timers", S_IRUGO, proc_timers_operations),
2832#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002833};
2834
Al Virof0c3b502013-05-16 12:07:31 -04002835static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002836{
Al Virof0c3b502013-05-16 12:07:31 -04002837 return proc_pident_readdir(file, ctx,
2838 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002839}
2840
Arjan van de Ven00977a52007-02-12 00:55:34 -08002841static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002842 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002843 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002844 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002845};
2846
Al Viro00cd8dd2012-06-10 17:13:09 -04002847static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2848{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002849 return proc_pident_lookup(dir, dentry,
2850 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002851}
2852
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002853static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002854 .lookup = proc_tgid_base_lookup,
2855 .getattr = pid_getattr,
2856 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002857 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002858};
2859
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002860static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002862 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002863 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002864 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Eric W. Biederman48e64842006-06-26 00:25:48 -07002866 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002867 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002868 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002869 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002870 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08002871 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002872 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08002875 if (pid == tgid)
2876 return;
2877
Eric W. Biederman48e64842006-06-26 00:25:48 -07002878 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002879 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2880 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002881 if (!leader)
2882 goto out;
2883
2884 name.name = "task";
2885 name.len = strlen(name.name);
2886 dir = d_hash_and_lookup(leader, &name);
2887 if (!dir)
2888 goto out_put_leader;
2889
2890 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002891 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002892 dentry = d_hash_and_lookup(dir, &name);
2893 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08002894 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002895 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002897
2898 dput(dir);
2899out_put_leader:
2900 dput(leader);
2901out:
2902 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
Randy Dunlap0895e912007-10-21 21:00:10 -07002905/**
2906 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2907 * @task: task that should be flushed.
2908 *
2909 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002910 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002911 * in. This call is supposed to do all of this job.
2912 *
2913 * Looks in the dcache for
2914 * /proc/@pid
2915 * /proc/@tgid/task/@pid
2916 * if either directory is present flushes it and all of it'ts children
2917 * from the dcache.
2918 *
2919 * It is safe and reasonable to cache /proc entries for a task until
2920 * that task exits. After that they just clog up the dcache with
2921 * useless entries, possibly causing useful dcache entries to be
2922 * flushed instead. This routine is proved to flush those useless
2923 * dcache entries at process exit time.
2924 *
2925 * NOTE: This routine is just an optimization so it does not guarantee
2926 * that no dcache entries will exist at process exit time it
2927 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002928 */
2929
2930void proc_flush_task(struct task_struct *task)
2931{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002932 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002933 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002934 struct upid *upid;
2935
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002936 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002937 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002938
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002939 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002940 upid = &pid->numbers[i];
2941 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002942 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002943 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002944}
2945
Al Viroc52a47a2013-06-15 11:15:20 +04002946static int proc_pid_instantiate(struct inode *dir,
2947 struct dentry * dentry,
2948 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002949{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002950 struct inode *inode;
2951
Eric W. Biederman61a28782006-10-02 02:18:49 -07002952 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002953 if (!inode)
2954 goto out;
2955
2956 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2957 inode->i_op = &proc_tgid_base_inode_operations;
2958 inode->i_fop = &proc_tgid_base_operations;
2959 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002960
Miklos Szeredibfe86842011-10-28 14:13:29 +02002961 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2962 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002963
Nick Pigginfb045ad2011-01-07 17:49:55 +11002964 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002965
2966 d_add(dentry, inode);
2967 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002968 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002969 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002970out:
Al Viroc52a47a2013-06-15 11:15:20 +04002971 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002972}
2973
Al Viro00cd8dd2012-06-10 17:13:09 -04002974struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002976 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002979 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002981 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (tgid == ~0U)
2983 goto out;
2984
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002985 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002986 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002987 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (task)
2989 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002990 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (!task)
2992 goto out;
2993
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002994 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002995 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996out:
Al Viroc52a47a2013-06-15 11:15:20 +04002997 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003001 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003004struct tgid_iter {
3005 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003006 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003007};
3008static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3009{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003010 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003012 if (iter.task)
3013 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003014 rcu_read_lock();
3015retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003016 iter.task = NULL;
3017 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003018 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003019 iter.tgid = pid_nr_ns(pid, ns);
3020 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003021 /* What we to know is if the pid we have find is the
3022 * pid of a thread_group_leader. Testing for task
3023 * being a thread_group_leader is the obvious thing
3024 * todo but there is a window when it fails, due to
3025 * the pid transfer logic in de_thread.
3026 *
3027 * So we perform the straight forward test of seeing
3028 * if the pid we have found is the pid of a thread
3029 * group leader, and don't worry if the task we have
3030 * found doesn't happen to be a thread group leader.
3031 * As we don't care in the case of readdir.
3032 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003033 if (!iter.task || !has_group_leader_pid(iter.task)) {
3034 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003035 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003036 }
3037 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003039 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003040 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Eric W. Biederman00978752014-07-31 03:10:50 -07003043#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003046int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003048 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003049 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003050 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Al Viro021ada72013-03-29 19:27:05 -04003052 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Eric W. Biederman00978752014-07-31 03:10:50 -07003055 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003056 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003057 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003058 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003059 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003060 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003061 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003062 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003063 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3064 return 0;
3065 ctx->pos = pos = pos + 1;
3066 }
3067 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003068 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003069 for (iter = next_tgid(ns, iter);
3070 iter.task;
3071 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003072 char name[PROC_NUMBUF];
3073 int len;
3074 if (!has_pid_permissions(ns, iter.task, 2))
3075 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003076
Al Virof0c3b502013-05-16 12:07:31 -04003077 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3078 ctx->pos = iter.tgid + TGID_OFFSET;
3079 if (!proc_fill_cache(file, ctx, name, len,
3080 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003081 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
Al Virof0c3b502013-05-16 12:07:31 -04003085 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return 0;
3087}
3088
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003089/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003090 * Tasks
3091 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003092static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003093 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003094 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003095 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003096#ifdef CONFIG_NET
3097 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3098#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003099 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07003100 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003101 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003102 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003103 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003104#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003105 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003106#endif
john stultz4614a696b2009-12-14 18:00:05 -08003107 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003108#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003109 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003110#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003111 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003112 ONE("stat", S_IRUGO, proc_tid_stat),
3113 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003114 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003115#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003116 REG("children", S_IRUGO, proc_tid_children_operations),
3117#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003118#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003119 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003120#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003121 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3122 LNK("cwd", proc_cwd_link),
3123 LNK("root", proc_root_link),
3124 LNK("exe", proc_exe_link),
3125 REG("mounts", S_IRUGO, proc_mounts_operations),
3126 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003127#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003128 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003129 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003130 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003131#endif
3132#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003133 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003134#endif
3135#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003136 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003137#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003138#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003139 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003140#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303141#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003142 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003143#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003144#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003145 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003146#endif
Paul Menage8793d852007-10-18 23:39:39 -07003147#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003148 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003149#endif
Paul Menagea4243162007-10-18 23:39:35 -07003150#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003151 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003152#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003153 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003154 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003155 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003156#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003157 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003158 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003159#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003160#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003161 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003162#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003163#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003164 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003165#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003166#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003167 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003168#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003169#ifdef CONFIG_USER_NS
3170 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3171 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003172 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003173 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003174#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003175};
3176
Al Virof0c3b502013-05-16 12:07:31 -04003177static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003178{
Al Virof0c3b502013-05-16 12:07:31 -04003179 return proc_pident_readdir(file, ctx,
3180 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003181}
3182
Al Viro00cd8dd2012-06-10 17:13:09 -04003183static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3184{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003185 return proc_pident_lookup(dir, dentry,
3186 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003187}
3188
Arjan van de Ven00977a52007-02-12 00:55:34 -08003189static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003190 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003191 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003192 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003193};
3194
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003195static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003196 .lookup = proc_tid_base_lookup,
3197 .getattr = pid_getattr,
3198 .setattr = proc_setattr,
3199};
3200
Al Viroc52a47a2013-06-15 11:15:20 +04003201static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003202 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003203{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003204 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003205 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003206
3207 if (!inode)
3208 goto out;
3209 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3210 inode->i_op = &proc_tid_base_inode_operations;
3211 inode->i_fop = &proc_tid_base_operations;
3212 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003213
Miklos Szeredibfe86842011-10-28 14:13:29 +02003214 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3215 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003216
Nick Pigginfb045ad2011-01-07 17:49:55 +11003217 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003218
3219 d_add(dentry, inode);
3220 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003221 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003222 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003223out:
Al Viroc52a47a2013-06-15 11:15:20 +04003224 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003225}
3226
Al Viro00cd8dd2012-06-10 17:13:09 -04003227static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003228{
Al Viroc52a47a2013-06-15 11:15:20 +04003229 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003230 struct task_struct *task;
3231 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003232 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003233 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003234
3235 if (!leader)
3236 goto out_no_task;
3237
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003238 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003239 if (tid == ~0U)
3240 goto out;
3241
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003242 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003243 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003244 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003245 if (task)
3246 get_task_struct(task);
3247 rcu_read_unlock();
3248 if (!task)
3249 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003250 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003251 goto out_drop_task;
3252
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003253 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003254out_drop_task:
3255 put_task_struct(task);
3256out:
3257 put_task_struct(leader);
3258out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003259 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003260}
3261
3262/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003263 * Find the first tid of a thread group to return to user space.
3264 *
3265 * Usually this is just the thread group leader, but if the users
3266 * buffer was too small or there was a seek into the middle of the
3267 * directory we have more work todo.
3268 *
3269 * In the case of a short read we start with find_task_by_pid.
3270 *
3271 * In the case of a seek we start with the leader and walk nr
3272 * threads past it.
3273 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003274static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3275 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003276{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003277 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003278 unsigned long nr = f_pos;
3279
3280 if (nr != f_pos) /* 32bit overflow? */
3281 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003282
Eric W. Biedermancc288732006-06-26 00:26:01 -07003283 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003284 task = pid_task(pid, PIDTYPE_PID);
3285 if (!task)
3286 goto fail;
3287
3288 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003289 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003290 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003291 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003292 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003293 }
3294
3295 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003296 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003297 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003298
3299 /* If we haven't found our starting place yet start
3300 * with the leader and walk nr threads forward.
3301 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003302 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003303 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003304 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003305 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003306 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003307fail:
3308 pos = NULL;
3309 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003310found:
3311 get_task_struct(pos);
3312out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003313 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003314 return pos;
3315}
3316
3317/*
3318 * Find the next thread in the thread list.
3319 * Return NULL if there is an error or no next thread.
3320 *
3321 * The reference to the input task_struct is released.
3322 */
3323static struct task_struct *next_tid(struct task_struct *start)
3324{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003325 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003326 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003327 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003328 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003329 if (thread_group_leader(pos))
3330 pos = NULL;
3331 else
3332 get_task_struct(pos);
3333 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003334 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003335 put_task_struct(start);
3336 return pos;
3337}
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003340static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003342 struct inode *inode = file_inode(file);
3343 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003344 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003345 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003347 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003348 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Al Virof0c3b502013-05-16 12:07:31 -04003350 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003353 /* f_version caches the tgid value that the last readdir call couldn't
3354 * return. lseek aka telldir automagically resets f_version to 0.
3355 */
Al Viro3aa33772014-10-31 00:42:35 -04003356 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003357 tid = (int)file->f_version;
3358 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003359 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003360 task;
Al Virof0c3b502013-05-16 12:07:31 -04003361 task = next_tid(task), ctx->pos++) {
3362 char name[PROC_NUMBUF];
3363 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003364 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003365 len = snprintf(name, sizeof(name), "%d", tid);
3366 if (!proc_fill_cache(file, ctx, name, len,
3367 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003368 /* returning this tgid failed, save it as the first
3369 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003370 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003371 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003375
Al Virof0c3b502013-05-16 12:07:31 -04003376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003378
3379static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3380{
David Howells2b0143b2015-03-17 22:25:59 +00003381 struct inode *inode = d_inode(dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003382 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003383 generic_fillattr(inode, stat);
3384
Eric W. Biederman99f89552006-06-26 00:25:55 -07003385 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003386 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003387 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003388 }
3389
3390 return 0;
3391}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003392
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003393static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003394 .lookup = proc_task_lookup,
3395 .getattr = proc_task_getattr,
3396 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003397 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003398};
3399
Arjan van de Ven00977a52007-02-12 00:55:34 -08003400static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003401 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003402 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003403 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003404};