]> git.ipfire.org Git - people/ms/linux.git/blame - fs/proc/base.c
mm: add new mmget() helper
[people/ms/linux.git] / fs / proc / base.c
CommitLineData
1da177e4
LT
1/*
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.
e070ad49
ML
14 *
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.
1da177e4
LT
48 */
49
7c0f6ba6 50#include <linux/uaccess.h>
1da177e4 51
1da177e4
LT
52#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
5995477a 56#include <linux/task_io_accounting_ops.h>
1da177e4 57#include <linux/init.h>
16f7e0fe 58#include <linux/capability.h>
1da177e4 59#include <linux/file.h>
9f3acc31 60#include <linux/fdtable.h>
1da177e4
LT
61#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
6b3286ed 64#include <linux/mnt_namespace.h>
1da177e4 65#include <linux/mm.h>
a63d83f4 66#include <linux/swap.h>
b835996f 67#include <linux/rcupdate.h>
1da177e4 68#include <linux/kallsyms.h>
2ec220e2 69#include <linux/stacktrace.h>
d85f50d5 70#include <linux/resource.h>
5096add8 71#include <linux/module.h>
1da177e4
LT
72#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
0d094efe 75#include <linux/tracehook.h>
87ebdc00 76#include <linux/printk.h>
a424316c 77#include <linux/cgroup.h>
1da177e4
LT
78#include <linux/cpuset.h>
79#include <linux/audit.h>
5addc5dd 80#include <linux/poll.h>
1651e14e 81#include <linux/nsproxy.h>
8ac773b4 82#include <linux/oom.h>
3cb4a0bb 83#include <linux/elf.h>
60347f67 84#include <linux/pid_namespace.h>
22d917d8 85#include <linux/user_namespace.h>
5ad4e53b 86#include <linux/fs_struct.h>
5a0e3ad6 87#include <linux/slab.h>
640708a2 88#include <linux/flex_array.h>
48f6a7a5 89#include <linux/posix-timers.h>
f133ecca
CM
90#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
43d2b113 93#include <trace/events/oom.h>
1da177e4 94#include "internal.h"
faf60af1 95#include "fd.h"
1da177e4 96
0f2fe20f
EB
97/* 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
1270dd8d
AD
107static u8 nlink_tid;
108static u8 nlink_tgid;
109
1da177e4 110struct pid_entry {
cedbccab 111 const char *name;
623f594e 112 unsigned int len;
d161a13f 113 umode_t mode;
c5ef1c42 114 const struct inode_operations *iop;
00977a59 115 const struct file_operations *fop;
20cdc894 116 union proc_op op;
1da177e4
LT
117};
118
61a28784 119#define NOD(NAME, MODE, IOP, FOP, OP) { \
20cdc894 120 .name = (NAME), \
c5141e6d 121 .len = sizeof(NAME) - 1, \
20cdc894
EB
122 .mode = MODE, \
123 .iop = IOP, \
124 .fop = FOP, \
125 .op = OP, \
126}
127
631f9c18
AD
128#define DIR(NAME, MODE, iops, fops) \
129 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
130#define LNK(NAME, get_link) \
61a28784 131 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
20cdc894 132 &proc_pid_link_inode_operations, NULL, \
631f9c18
AD
133 { .proc_get_link = get_link } )
134#define REG(NAME, MODE, fops) \
135 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
631f9c18 136#define ONE(NAME, MODE, show) \
be614086
EB
137 NOD(NAME, (S_IFREG|(MODE)), \
138 NULL, &proc_single_file_operations, \
631f9c18 139 { .proc_show = show } )
1da177e4 140
aed54175
VN
141/*
142 * Count the number of hardlinks for the pid_entry table, excluding the .
143 * and .. links.
144 */
1270dd8d 145static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
aed54175
VN
146 unsigned int n)
147{
148 unsigned int i;
149 unsigned int count;
150
1270dd8d 151 count = 2;
aed54175
VN
152 for (i = 0; i < n; ++i) {
153 if (S_ISDIR(entries[i].mode))
154 ++count;
155 }
156
157 return count;
158}
159
f7ad3c6b 160static int get_task_root(struct task_struct *task, struct path *root)
1da177e4 161{
7c2c7d99
HD
162 int result = -ENOENT;
163
0494f6ec 164 task_lock(task);
f7ad3c6b
MS
165 if (task->fs) {
166 get_fs_root(task->fs, root);
7c2c7d99
HD
167 result = 0;
168 }
0494f6ec 169 task_unlock(task);
7c2c7d99 170 return result;
0494f6ec
MS
171}
172
7773fbc5 173static int proc_cwd_link(struct dentry *dentry, struct path *path)
0494f6ec 174{
2b0143b5 175 struct task_struct *task = get_proc_task(d_inode(dentry));
0494f6ec 176 int result = -ENOENT;
99f89551
EB
177
178 if (task) {
f7ad3c6b
MS
179 task_lock(task);
180 if (task->fs) {
181 get_fs_pwd(task->fs, path);
182 result = 0;
183 }
184 task_unlock(task);
99f89551
EB
185 put_task_struct(task);
186 }
1da177e4
LT
187 return result;
188}
189
7773fbc5 190static int proc_root_link(struct dentry *dentry, struct path *path)
1da177e4 191{
2b0143b5 192 struct task_struct *task = get_proc_task(d_inode(dentry));
1da177e4 193 int result = -ENOENT;
99f89551
EB
194
195 if (task) {
f7ad3c6b 196 result = get_task_root(task, path);
99f89551
EB
197 put_task_struct(task);
198 }
1da177e4
LT
199 return result;
200}
201
c2c0bb44
AD
202static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
203 size_t _count, loff_t *pos)
1da177e4 204{
c2c0bb44
AD
205 struct task_struct *tsk;
206 struct mm_struct *mm;
207 char *page;
208 unsigned long count = _count;
209 unsigned long arg_start, arg_end, env_start, env_end;
210 unsigned long len1, len2, len;
211 unsigned long p;
212 char c;
213 ssize_t rv;
214
215 BUG_ON(*pos < 0);
216
217 tsk = get_proc_task(file_inode(file));
218 if (!tsk)
219 return -ESRCH;
220 mm = get_task_mm(tsk);
221 put_task_struct(tsk);
222 if (!mm)
223 return 0;
224 /* Check if process spawned far enough to have cmdline. */
225 if (!mm->env_end) {
226 rv = 0;
227 goto out_mmput;
228 }
229
230 page = (char *)__get_free_page(GFP_TEMPORARY);
231 if (!page) {
232 rv = -ENOMEM;
233 goto out_mmput;
234 }
235
236 down_read(&mm->mmap_sem);
237 arg_start = mm->arg_start;
238 arg_end = mm->arg_end;
239 env_start = mm->env_start;
240 env_end = mm->env_end;
241 up_read(&mm->mmap_sem);
242
243 BUG_ON(arg_start > arg_end);
244 BUG_ON(env_start > env_end);
245
246 len1 = arg_end - arg_start;
247 len2 = env_end - env_start;
248
3581d458
AD
249 /* Empty ARGV. */
250 if (len1 == 0) {
251 rv = 0;
252 goto out_free_page;
253 }
2ca66ff7 254 /*
c2c0bb44
AD
255 * Inherently racy -- command line shares address space
256 * with code and data.
2ca66ff7 257 */
272ddc8b 258 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
c2c0bb44
AD
259 if (rv <= 0)
260 goto out_free_page;
261
262 rv = 0;
263
264 if (c == '\0') {
265 /* Command line (set of strings) occupies whole ARGV. */
266 if (len1 <= *pos)
267 goto out_free_page;
268
269 p = arg_start + *pos;
270 len = len1 - *pos;
271 while (count > 0 && len > 0) {
272 unsigned int _count;
273 int nr_read;
274
275 _count = min3(count, len, PAGE_SIZE);
272ddc8b 276 nr_read = access_remote_vm(mm, p, page, _count, 0);
c2c0bb44
AD
277 if (nr_read < 0)
278 rv = nr_read;
279 if (nr_read <= 0)
280 goto out_free_page;
281
282 if (copy_to_user(buf, page, nr_read)) {
283 rv = -EFAULT;
284 goto out_free_page;
285 }
286
287 p += nr_read;
288 len -= nr_read;
289 buf += nr_read;
290 count -= nr_read;
291 rv += nr_read;
292 }
293 } else {
c2c0bb44
AD
294 /*
295 * Command line (1 string) occupies ARGV and
296 * extends into ENVP.
297 */
a0a07b87
AD
298 struct {
299 unsigned long p;
300 unsigned long len;
301 } cmdline[2] = {
302 { .p = arg_start, .len = len1 },
303 { .p = env_start, .len = len2 },
304 };
305 loff_t pos1 = *pos;
306 unsigned int i;
307
308 i = 0;
309 while (i < 2 && pos1 >= cmdline[i].len) {
310 pos1 -= cmdline[i].len;
311 i++;
c2c0bb44 312 }
a0a07b87
AD
313 while (i < 2) {
314 p = cmdline[i].p + pos1;
315 len = cmdline[i].len - pos1;
316 while (count > 0 && len > 0) {
317 unsigned int _count, l;
318 int nr_read;
319 bool final;
320
321 _count = min3(count, len, PAGE_SIZE);
322 nr_read = access_remote_vm(mm, p, page, _count, 0);
323 if (nr_read < 0)
324 rv = nr_read;
325 if (nr_read <= 0)
326 goto out_free_page;
327
328 /*
329 * Command line can be shorter than whole ARGV
330 * even if last "marker" byte says it is not.
331 */
332 final = false;
333 l = strnlen(page, nr_read);
334 if (l < nr_read) {
335 nr_read = l;
336 final = true;
337 }
338
339 if (copy_to_user(buf, page, nr_read)) {
340 rv = -EFAULT;
341 goto out_free_page;
342 }
343
344 p += nr_read;
345 len -= nr_read;
346 buf += nr_read;
347 count -= nr_read;
348 rv += nr_read;
349
350 if (final)
351 goto out_free_page;
c2c0bb44
AD
352 }
353
a0a07b87
AD
354 /* Only first chunk can be read partially. */
355 pos1 = 0;
356 i++;
c2c0bb44 357 }
c2c0bb44
AD
358 }
359
360out_free_page:
361 free_page((unsigned long)page);
362out_mmput:
363 mmput(mm);
364 if (rv > 0)
365 *pos += rv;
366 return rv;
1da177e4
LT
367}
368
c2c0bb44
AD
369static const struct file_operations proc_pid_cmdline_ops = {
370 .read = proc_pid_cmdline_read,
371 .llseek = generic_file_llseek,
372};
373
1da177e4
LT
374#ifdef CONFIG_KALLSYMS
375/*
376 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
377 * Returns the resolved symbol. If that fails, simply return the address.
378 */
edfcd606
AD
379static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
380 struct pid *pid, struct task_struct *task)
1da177e4 381{
ffb45122 382 unsigned long wchan;
9281acea 383 char symname[KSYM_NAME_LEN];
1da177e4
LT
384
385 wchan = get_wchan(task);
386
caaee623
JH
387 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
388 && !lookup_symbol_name(wchan, symname))
25ce3191 389 seq_printf(m, "%s", symname);
b2f73922 390 else
1e92a61c 391 seq_putc(m, '0');
25ce3191
JP
392
393 return 0;
1da177e4
LT
394}
395#endif /* CONFIG_KALLSYMS */
396
a9712bc1
AV
397static int lock_trace(struct task_struct *task)
398{
399 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
400 if (err)
401 return err;
caaee623 402 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
a9712bc1
AV
403 mutex_unlock(&task->signal->cred_guard_mutex);
404 return -EPERM;
405 }
406 return 0;
407}
408
409static void unlock_trace(struct task_struct *task)
410{
411 mutex_unlock(&task->signal->cred_guard_mutex);
412}
413
2ec220e2
KC
414#ifdef CONFIG_STACKTRACE
415
416#define MAX_STACK_TRACE_DEPTH 64
417
418static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
419 struct pid *pid, struct task_struct *task)
420{
421 struct stack_trace trace;
422 unsigned long *entries;
a9712bc1 423 int err;
2ec220e2
KC
424 int i;
425
426 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
427 if (!entries)
428 return -ENOMEM;
429
430 trace.nr_entries = 0;
431 trace.max_entries = MAX_STACK_TRACE_DEPTH;
432 trace.entries = entries;
433 trace.skip = 0;
2ec220e2 434
a9712bc1
AV
435 err = lock_trace(task);
436 if (!err) {
437 save_stack_trace_tsk(task, &trace);
438
439 for (i = 0; i < trace.nr_entries; i++) {
8b927d73 440 seq_printf(m, "[<%pK>] %pB\n",
a9712bc1
AV
441 (void *)entries[i], (void *)entries[i]);
442 }
443 unlock_trace(task);
2ec220e2
KC
444 }
445 kfree(entries);
446
a9712bc1 447 return err;
2ec220e2
KC
448}
449#endif
450
5968cece 451#ifdef CONFIG_SCHED_INFO
1da177e4
LT
452/*
453 * Provides /proc/PID/schedstat
454 */
f6e826ca
AD
455static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
456 struct pid *pid, struct task_struct *task)
1da177e4 457{
5968cece
NR
458 if (unlikely(!sched_info_on()))
459 seq_printf(m, "0 0 0\n");
460 else
461 seq_printf(m, "%llu %llu %lu\n",
25ce3191
JP
462 (unsigned long long)task->se.sum_exec_runtime,
463 (unsigned long long)task->sched_info.run_delay,
464 task->sched_info.pcount);
465
466 return 0;
1da177e4
LT
467}
468#endif
469
9745512c
AV
470#ifdef CONFIG_LATENCYTOP
471static int lstats_show_proc(struct seq_file *m, void *v)
472{
473 int i;
13d77c37
HS
474 struct inode *inode = m->private;
475 struct task_struct *task = get_proc_task(inode);
9745512c 476
13d77c37
HS
477 if (!task)
478 return -ESRCH;
479 seq_puts(m, "Latency Top version : v0.1\n");
9745512c 480 for (i = 0; i < 32; i++) {
34e49d4f
JP
481 struct latency_record *lr = &task->latency_record[i];
482 if (lr->backtrace[0]) {
9745512c 483 int q;
34e49d4f
JP
484 seq_printf(m, "%i %li %li",
485 lr->count, lr->time, lr->max);
9745512c 486 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
34e49d4f
JP
487 unsigned long bt = lr->backtrace[q];
488 if (!bt)
9745512c 489 break;
34e49d4f 490 if (bt == ULONG_MAX)
9745512c 491 break;
34e49d4f 492 seq_printf(m, " %ps", (void *)bt);
9745512c 493 }
9d6de12f 494 seq_putc(m, '\n');
9745512c
AV
495 }
496
497 }
13d77c37 498 put_task_struct(task);
9745512c
AV
499 return 0;
500}
501
502static int lstats_open(struct inode *inode, struct file *file)
503{
13d77c37 504 return single_open(file, lstats_show_proc, inode);
d6643d12
HS
505}
506
9745512c
AV
507static ssize_t lstats_write(struct file *file, const char __user *buf,
508 size_t count, loff_t *offs)
509{
496ad9aa 510 struct task_struct *task = get_proc_task(file_inode(file));
9745512c 511
13d77c37
HS
512 if (!task)
513 return -ESRCH;
9745512c 514 clear_all_latency_tracing(task);
13d77c37 515 put_task_struct(task);
9745512c
AV
516
517 return count;
518}
519
520static const struct file_operations proc_lstats_operations = {
521 .open = lstats_open,
522 .read = seq_read,
523 .write = lstats_write,
524 .llseek = seq_lseek,
13d77c37 525 .release = single_release,
9745512c
AV
526};
527
528#endif
529
6ba51e37
AD
530static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
531 struct pid *pid, struct task_struct *task)
1da177e4 532{
a7f638f9 533 unsigned long totalpages = totalram_pages + total_swap_pages;
b95c35e7 534 unsigned long points = 0;
1da177e4 535
ef419398
ON
536 points = oom_badness(task, NULL, NULL, totalpages) *
537 1000 / totalpages;
25ce3191
JP
538 seq_printf(m, "%lu\n", points);
539
540 return 0;
1da177e4
LT
541}
542
d85f50d5 543struct limit_names {
cedbccab
AD
544 const char *name;
545 const char *unit;
d85f50d5
NH
546};
547
548static const struct limit_names lnames[RLIM_NLIMITS] = {
cff4edb5 549 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
d85f50d5
NH
550 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
551 [RLIMIT_DATA] = {"Max data size", "bytes"},
552 [RLIMIT_STACK] = {"Max stack size", "bytes"},
553 [RLIMIT_CORE] = {"Max core file size", "bytes"},
554 [RLIMIT_RSS] = {"Max resident set", "bytes"},
555 [RLIMIT_NPROC] = {"Max processes", "processes"},
556 [RLIMIT_NOFILE] = {"Max open files", "files"},
557 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
558 [RLIMIT_AS] = {"Max address space", "bytes"},
559 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
560 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
561 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
562 [RLIMIT_NICE] = {"Max nice priority", NULL},
563 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
8808117c 564 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
d85f50d5
NH
565};
566
567/* Display limits for a process */
1c963eb1
AD
568static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
569 struct pid *pid, struct task_struct *task)
d85f50d5
NH
570{
571 unsigned int i;
d85f50d5 572 unsigned long flags;
d85f50d5
NH
573
574 struct rlimit rlim[RLIM_NLIMITS];
575
a6bebbc8 576 if (!lock_task_sighand(task, &flags))
d85f50d5 577 return 0;
d85f50d5
NH
578 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
579 unlock_task_sighand(task, &flags);
d85f50d5
NH
580
581 /*
582 * print the file header
583 */
1c963eb1 584 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
25ce3191 585 "Limit", "Soft Limit", "Hard Limit", "Units");
d85f50d5
NH
586
587 for (i = 0; i < RLIM_NLIMITS; i++) {
588 if (rlim[i].rlim_cur == RLIM_INFINITY)
1c963eb1 589 seq_printf(m, "%-25s %-20s ",
25ce3191 590 lnames[i].name, "unlimited");
d85f50d5 591 else
1c963eb1 592 seq_printf(m, "%-25s %-20lu ",
25ce3191 593 lnames[i].name, rlim[i].rlim_cur);
d85f50d5
NH
594
595 if (rlim[i].rlim_max == RLIM_INFINITY)
1c963eb1 596 seq_printf(m, "%-20s ", "unlimited");
d85f50d5 597 else
1c963eb1 598 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
d85f50d5
NH
599
600 if (lnames[i].unit)
1c963eb1 601 seq_printf(m, "%-10s\n", lnames[i].unit);
d85f50d5 602 else
1c963eb1 603 seq_putc(m, '\n');
d85f50d5
NH
604 }
605
1c963eb1 606 return 0;
d85f50d5
NH
607}
608
ebcb6734 609#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
09d93bd6
AD
610static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
611 struct pid *pid, struct task_struct *task)
ebcb6734
RM
612{
613 long nr;
614 unsigned long args[6], sp, pc;
25ce3191
JP
615 int res;
616
617 res = lock_trace(task);
a9712bc1
AV
618 if (res)
619 return res;
ebcb6734
RM
620
621 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
09d93bd6 622 seq_puts(m, "running\n");
a9712bc1 623 else if (nr < 0)
09d93bd6 624 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
a9712bc1 625 else
09d93bd6 626 seq_printf(m,
ebcb6734
RM
627 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
628 nr,
629 args[0], args[1], args[2], args[3], args[4], args[5],
630 sp, pc);
a9712bc1 631 unlock_trace(task);
25ce3191
JP
632
633 return 0;
ebcb6734
RM
634}
635#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
636
1da177e4
LT
637/************************************************************************/
638/* Here the fs part begins */
639/************************************************************************/
640
641/* permission checks */
778c1144 642static int proc_fd_access_allowed(struct inode *inode)
1da177e4 643{
778c1144
EB
644 struct task_struct *task;
645 int allowed = 0;
df26c40e
EB
646 /* Allow access to a task's file descriptors if it is us or we
647 * may use ptrace attach to the process and find out that
648 * information.
778c1144
EB
649 */
650 task = get_proc_task(inode);
df26c40e 651 if (task) {
caaee623 652 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
778c1144 653 put_task_struct(task);
df26c40e 654 }
778c1144 655 return allowed;
1da177e4
LT
656}
657
6b4e306a 658int proc_setattr(struct dentry *dentry, struct iattr *attr)
6d76fa58
LT
659{
660 int error;
2b0143b5 661 struct inode *inode = d_inode(dentry);
6d76fa58
LT
662
663 if (attr->ia_valid & ATTR_MODE)
664 return -EPERM;
665
31051c85 666 error = setattr_prepare(dentry, attr);
1025774c
CH
667 if (error)
668 return error;
669
1025774c
CH
670 setattr_copy(inode, attr);
671 mark_inode_dirty(inode);
672 return 0;
6d76fa58
LT
673}
674
0499680a
VK
675/*
676 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
677 * or euid/egid (for hide_pid_min=2)?
678 */
679static bool has_pid_permissions(struct pid_namespace *pid,
680 struct task_struct *task,
681 int hide_pid_min)
682{
683 if (pid->hide_pid < hide_pid_min)
684 return true;
685 if (in_group_p(pid->pid_gid))
686 return true;
caaee623 687 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
0499680a
VK
688}
689
690
691static int proc_pid_permission(struct inode *inode, int mask)
692{
693 struct pid_namespace *pid = inode->i_sb->s_fs_info;
694 struct task_struct *task;
695 bool has_perms;
696
697 task = get_proc_task(inode);
a2ef990a
XF
698 if (!task)
699 return -ESRCH;
796f571b 700 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
0499680a
VK
701 put_task_struct(task);
702
703 if (!has_perms) {
796f571b 704 if (pid->hide_pid == HIDEPID_INVISIBLE) {
0499680a
VK
705 /*
706 * Let's make getdents(), stat(), and open()
707 * consistent with each other. If a process
708 * may not stat() a file, it shouldn't be seen
709 * in procfs at all.
710 */
711 return -ENOENT;
712 }
713
714 return -EPERM;
715 }
716 return generic_permission(inode, mask);
717}
718
719
720
c5ef1c42 721static const struct inode_operations proc_def_inode_operations = {
6d76fa58
LT
722 .setattr = proc_setattr,
723};
724
be614086
EB
725static int proc_single_show(struct seq_file *m, void *v)
726{
727 struct inode *inode = m->private;
728 struct pid_namespace *ns;
729 struct pid *pid;
730 struct task_struct *task;
731 int ret;
732
733 ns = inode->i_sb->s_fs_info;
734 pid = proc_pid(inode);
735 task = get_pid_task(pid, PIDTYPE_PID);
736 if (!task)
737 return -ESRCH;
738
739 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
740
741 put_task_struct(task);
742 return ret;
743}
744
745static int proc_single_open(struct inode *inode, struct file *filp)
746{
c6a34058 747 return single_open(filp, proc_single_show, inode);
be614086
EB
748}
749
750static const struct file_operations proc_single_file_operations = {
751 .open = proc_single_open,
752 .read = seq_read,
753 .llseek = seq_lseek,
754 .release = single_release,
755};
756
5381e169
ON
757
758struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
1da177e4 759{
5381e169
ON
760 struct task_struct *task = get_proc_task(inode);
761 struct mm_struct *mm = ERR_PTR(-ESRCH);
e268337d 762
5381e169 763 if (task) {
caaee623 764 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
5381e169 765 put_task_struct(task);
e268337d 766
5381e169
ON
767 if (!IS_ERR_OR_NULL(mm)) {
768 /* ensure this mm_struct can't be freed */
f1f10076 769 mmgrab(mm);
5381e169
ON
770 /* but do not pin its memory */
771 mmput(mm);
772 }
773 }
774
775 return mm;
776}
777
778static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
779{
780 struct mm_struct *mm = proc_mem_open(inode, mode);
e268337d
LT
781
782 if (IS_ERR(mm))
783 return PTR_ERR(mm);
784
e268337d 785 file->private_data = mm;
1da177e4
LT
786 return 0;
787}
788
b409e578
CW
789static int mem_open(struct inode *inode, struct file *file)
790{
bc452b4b
DH
791 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
792
793 /* OK to pass negative loff_t, we can catch out-of-range */
794 file->f_mode |= FMODE_UNSIGNED_OFFSET;
795
796 return ret;
b409e578
CW
797}
798
572d34b9
ON
799static ssize_t mem_rw(struct file *file, char __user *buf,
800 size_t count, loff_t *ppos, int write)
1da177e4 801{
e268337d 802 struct mm_struct *mm = file->private_data;
572d34b9
ON
803 unsigned long addr = *ppos;
804 ssize_t copied;
1da177e4 805 char *page;
272ddc8b 806 unsigned int flags;
1da177e4 807
e268337d
LT
808 if (!mm)
809 return 0;
99f89551 810
30cd8903
KM
811 page = (char *)__get_free_page(GFP_TEMPORARY);
812 if (!page)
e268337d 813 return -ENOMEM;
1da177e4 814
f7ca54f4 815 copied = 0;
6d08f2c7
ON
816 if (!atomic_inc_not_zero(&mm->mm_users))
817 goto free;
818
272ddc8b
LT
819 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
820 flags = FOLL_FORCE;
6347e8d5
LS
821 if (write)
822 flags |= FOLL_WRITE;
823
1da177e4 824 while (count > 0) {
572d34b9 825 int this_len = min_t(int, count, PAGE_SIZE);
1da177e4 826
572d34b9 827 if (write && copy_from_user(page, buf, this_len)) {
1da177e4
LT
828 copied = -EFAULT;
829 break;
830 }
572d34b9 831
6347e8d5 832 this_len = access_remote_vm(mm, addr, page, this_len, flags);
572d34b9 833 if (!this_len) {
1da177e4
LT
834 if (!copied)
835 copied = -EIO;
836 break;
837 }
572d34b9
ON
838
839 if (!write && copy_to_user(buf, page, this_len)) {
840 copied = -EFAULT;
841 break;
842 }
843
844 buf += this_len;
845 addr += this_len;
846 copied += this_len;
847 count -= this_len;
1da177e4 848 }
572d34b9 849 *ppos = addr;
30cd8903 850
6d08f2c7
ON
851 mmput(mm);
852free:
30cd8903 853 free_page((unsigned long) page);
1da177e4
LT
854 return copied;
855}
1da177e4 856
572d34b9
ON
857static ssize_t mem_read(struct file *file, char __user *buf,
858 size_t count, loff_t *ppos)
859{
860 return mem_rw(file, buf, count, ppos, 0);
861}
862
863static ssize_t mem_write(struct file *file, const char __user *buf,
864 size_t count, loff_t *ppos)
865{
866 return mem_rw(file, (char __user*)buf, count, ppos, 1);
867}
868
85863e47 869loff_t mem_lseek(struct file *file, loff_t offset, int orig)
1da177e4
LT
870{
871 switch (orig) {
872 case 0:
873 file->f_pos = offset;
874 break;
875 case 1:
876 file->f_pos += offset;
877 break;
878 default:
879 return -EINVAL;
880 }
881 force_successful_syscall_return();
882 return file->f_pos;
883}
884
e268337d
LT
885static int mem_release(struct inode *inode, struct file *file)
886{
887 struct mm_struct *mm = file->private_data;
71879d3c 888 if (mm)
6d08f2c7 889 mmdrop(mm);
e268337d
LT
890 return 0;
891}
892
00977a59 893static const struct file_operations proc_mem_operations = {
1da177e4
LT
894 .llseek = mem_lseek,
895 .read = mem_read,
896 .write = mem_write,
897 .open = mem_open,
e268337d 898 .release = mem_release,
1da177e4
LT
899};
900
b409e578
CW
901static int environ_open(struct inode *inode, struct file *file)
902{
903 return __mem_open(inode, file, PTRACE_MODE_READ);
904}
905
315e28c8
JP
906static ssize_t environ_read(struct file *file, char __user *buf,
907 size_t count, loff_t *ppos)
908{
315e28c8
JP
909 char *page;
910 unsigned long src = *ppos;
b409e578
CW
911 int ret = 0;
912 struct mm_struct *mm = file->private_data;
a3b609ef 913 unsigned long env_start, env_end;
315e28c8 914
8148a73c
MK
915 /* Ensure the process spawned far enough to have an environment. */
916 if (!mm || !mm->env_end)
b409e578 917 return 0;
315e28c8 918
315e28c8
JP
919 page = (char *)__get_free_page(GFP_TEMPORARY);
920 if (!page)
b409e578 921 return -ENOMEM;
315e28c8 922
d6f64b89 923 ret = 0;
b409e578
CW
924 if (!atomic_inc_not_zero(&mm->mm_users))
925 goto free;
a3b609ef
MG
926
927 down_read(&mm->mmap_sem);
928 env_start = mm->env_start;
929 env_end = mm->env_end;
930 up_read(&mm->mmap_sem);
931
315e28c8 932 while (count > 0) {
e8905ec2
DH
933 size_t this_len, max_len;
934 int retval;
315e28c8 935
a3b609ef 936 if (src >= (env_end - env_start))
315e28c8
JP
937 break;
938
a3b609ef 939 this_len = env_end - (env_start + src);
e8905ec2
DH
940
941 max_len = min_t(size_t, PAGE_SIZE, count);
942 this_len = min(max_len, this_len);
315e28c8 943
272ddc8b 944 retval = access_remote_vm(mm, (env_start + src), page, this_len, 0);
315e28c8
JP
945
946 if (retval <= 0) {
947 ret = retval;
948 break;
949 }
950
951 if (copy_to_user(buf, page, retval)) {
952 ret = -EFAULT;
953 break;
954 }
955
956 ret += retval;
957 src += retval;
958 buf += retval;
959 count -= retval;
960 }
961 *ppos = src;
315e28c8 962 mmput(mm);
b409e578
CW
963
964free:
315e28c8 965 free_page((unsigned long) page);
315e28c8
JP
966 return ret;
967}
968
969static const struct file_operations proc_environ_operations = {
b409e578 970 .open = environ_open,
315e28c8 971 .read = environ_read,
87df8424 972 .llseek = generic_file_llseek,
b409e578 973 .release = mem_release,
315e28c8
JP
974};
975
c5317167
AV
976static int auxv_open(struct inode *inode, struct file *file)
977{
978 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
979}
980
981static ssize_t auxv_read(struct file *file, char __user *buf,
982 size_t count, loff_t *ppos)
983{
984 struct mm_struct *mm = file->private_data;
985 unsigned int nwords = 0;
06b2849d
LY
986
987 if (!mm)
988 return 0;
c5317167
AV
989 do {
990 nwords += 2;
991 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
992 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
993 nwords * sizeof(mm->saved_auxv[0]));
994}
995
996static const struct file_operations proc_auxv_operations = {
997 .open = auxv_open,
998 .read = auxv_read,
999 .llseek = generic_file_llseek,
1000 .release = mem_release,
1001};
1002
fa0cbbf1
DR
1003static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1004 loff_t *ppos)
1005{
496ad9aa 1006 struct task_struct *task = get_proc_task(file_inode(file));
fa0cbbf1
DR
1007 char buffer[PROC_NUMBUF];
1008 int oom_adj = OOM_ADJUST_MIN;
1009 size_t len;
fa0cbbf1
DR
1010
1011 if (!task)
1012 return -ESRCH;
f913da59
MH
1013 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1014 oom_adj = OOM_ADJUST_MAX;
1015 else
1016 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1017 OOM_SCORE_ADJ_MAX;
fa0cbbf1
DR
1018 put_task_struct(task);
1019 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1020 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1021}
1022
1d5f0acb
MH
1023static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1024{
1025 static DEFINE_MUTEX(oom_adj_mutex);
44a70ade 1026 struct mm_struct *mm = NULL;
1d5f0acb
MH
1027 struct task_struct *task;
1028 int err = 0;
1029
1030 task = get_proc_task(file_inode(file));
1031 if (!task)
1032 return -ESRCH;
1033
1034 mutex_lock(&oom_adj_mutex);
1035 if (legacy) {
1036 if (oom_adj < task->signal->oom_score_adj &&
1037 !capable(CAP_SYS_RESOURCE)) {
1038 err = -EACCES;
1039 goto err_unlock;
1040 }
1041 /*
1042 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1043 * /proc/pid/oom_score_adj instead.
1044 */
1045 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1046 current->comm, task_pid_nr(current), task_pid_nr(task),
1047 task_pid_nr(task));
1048 } else {
1049 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1050 !capable(CAP_SYS_RESOURCE)) {
1051 err = -EACCES;
1052 goto err_unlock;
1053 }
1054 }
1055
44a70ade
MH
1056 /*
1057 * Make sure we will check other processes sharing the mm if this is
1058 * not vfrok which wants its own oom_score_adj.
1059 * pin the mm so it doesn't go away and get reused after task_unlock
1060 */
1061 if (!task->vfork_done) {
1062 struct task_struct *p = find_lock_task_mm(task);
1063
1064 if (p) {
1065 if (atomic_read(&p->mm->mm_users) > 1) {
1066 mm = p->mm;
f1f10076 1067 mmgrab(mm);
44a70ade
MH
1068 }
1069 task_unlock(p);
1070 }
1071 }
1072
1d5f0acb
MH
1073 task->signal->oom_score_adj = oom_adj;
1074 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1075 task->signal->oom_score_adj_min = (short)oom_adj;
1076 trace_oom_score_adj_update(task);
44a70ade
MH
1077
1078 if (mm) {
1079 struct task_struct *p;
1080
1081 rcu_read_lock();
1082 for_each_process(p) {
1083 if (same_thread_group(task, p))
1084 continue;
1085
1086 /* do not touch kernel threads or the global init */
1087 if (p->flags & PF_KTHREAD || is_global_init(p))
1088 continue;
1089
1090 task_lock(p);
1091 if (!p->vfork_done && process_shares_mm(p, mm)) {
1092 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1093 task_pid_nr(p), p->comm,
1094 p->signal->oom_score_adj, oom_adj,
1095 task_pid_nr(task), task->comm);
1096 p->signal->oom_score_adj = oom_adj;
1097 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1098 p->signal->oom_score_adj_min = (short)oom_adj;
1099 }
1100 task_unlock(p);
1101 }
1102 rcu_read_unlock();
1103 mmdrop(mm);
1104 }
1d5f0acb
MH
1105err_unlock:
1106 mutex_unlock(&oom_adj_mutex);
1107 put_task_struct(task);
1108 return err;
1109}
f913da59 1110
b72bdfa7
DR
1111/*
1112 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1113 * kernels. The effective policy is defined by oom_score_adj, which has a
1114 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1115 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1116 * Processes that become oom disabled via oom_adj will still be oom disabled
1117 * with this implementation.
1118 *
1119 * oom_adj cannot be removed since existing userspace binaries use it.
1120 */
fa0cbbf1
DR
1121static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1122 size_t count, loff_t *ppos)
1123{
fa0cbbf1
DR
1124 char buffer[PROC_NUMBUF];
1125 int oom_adj;
fa0cbbf1
DR
1126 int err;
1127
1128 memset(buffer, 0, sizeof(buffer));
1129 if (count > sizeof(buffer) - 1)
1130 count = sizeof(buffer) - 1;
1131 if (copy_from_user(buffer, buf, count)) {
1132 err = -EFAULT;
1133 goto out;
1134 }
1135
1136 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1137 if (err)
1138 goto out;
1139 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1140 oom_adj != OOM_DISABLE) {
1141 err = -EINVAL;
1142 goto out;
1143 }
1144
fa0cbbf1
DR
1145 /*
1146 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1147 * value is always attainable.
1148 */
1149 if (oom_adj == OOM_ADJUST_MAX)
1150 oom_adj = OOM_SCORE_ADJ_MAX;
1151 else
1152 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1153
1d5f0acb 1154 err = __set_oom_adj(file, oom_adj, true);
fa0cbbf1
DR
1155out:
1156 return err < 0 ? err : count;
1157}
1158
1159static const struct file_operations proc_oom_adj_operations = {
1160 .read = oom_adj_read,
1161 .write = oom_adj_write,
1162 .llseek = generic_file_llseek,
1163};
1164
a63d83f4
DR
1165static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1166 size_t count, loff_t *ppos)
1167{
496ad9aa 1168 struct task_struct *task = get_proc_task(file_inode(file));
a63d83f4 1169 char buffer[PROC_NUMBUF];
a9c58b90 1170 short oom_score_adj = OOM_SCORE_ADJ_MIN;
a63d83f4
DR
1171 size_t len;
1172
1173 if (!task)
1174 return -ESRCH;
f913da59 1175 oom_score_adj = task->signal->oom_score_adj;
a63d83f4 1176 put_task_struct(task);
a9c58b90 1177 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
a63d83f4
DR
1178 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1179}
1180
1181static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1182 size_t count, loff_t *ppos)
1183{
a63d83f4 1184 char buffer[PROC_NUMBUF];
0a8cb8e3 1185 int oom_score_adj;
a63d83f4
DR
1186 int err;
1187
1188 memset(buffer, 0, sizeof(buffer));
1189 if (count > sizeof(buffer) - 1)
1190 count = sizeof(buffer) - 1;
723548bf
DR
1191 if (copy_from_user(buffer, buf, count)) {
1192 err = -EFAULT;
1193 goto out;
1194 }
a63d83f4 1195
0a8cb8e3 1196 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
a63d83f4 1197 if (err)
723548bf 1198 goto out;
a63d83f4 1199 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
723548bf
DR
1200 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1201 err = -EINVAL;
1202 goto out;
1203 }
a63d83f4 1204
1d5f0acb 1205 err = __set_oom_adj(file, oom_score_adj, false);
723548bf
DR
1206out:
1207 return err < 0 ? err : count;
a63d83f4
DR
1208}
1209
1210static const struct file_operations proc_oom_score_adj_operations = {
1211 .read = oom_score_adj_read,
1212 .write = oom_score_adj_write,
6038f373 1213 .llseek = default_llseek,
a63d83f4
DR
1214};
1215
1da177e4 1216#ifdef CONFIG_AUDITSYSCALL
b4eb4f7f 1217#define TMPBUFLEN 11
1da177e4
LT
1218static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1219 size_t count, loff_t *ppos)
1220{
496ad9aa 1221 struct inode * inode = file_inode(file);
99f89551 1222 struct task_struct *task = get_proc_task(inode);
1da177e4
LT
1223 ssize_t length;
1224 char tmpbuf[TMPBUFLEN];
1225
99f89551
EB
1226 if (!task)
1227 return -ESRCH;
1da177e4 1228 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
e1760bd5
EB
1229 from_kuid(file->f_cred->user_ns,
1230 audit_get_loginuid(task)));
99f89551 1231 put_task_struct(task);
1da177e4
LT
1232 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1233}
1234
1235static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1236 size_t count, loff_t *ppos)
1237{
496ad9aa 1238 struct inode * inode = file_inode(file);
1da177e4 1239 uid_t loginuid;
e1760bd5 1240 kuid_t kloginuid;
774636e1 1241 int rv;
1da177e4 1242
7dc52157
PM
1243 rcu_read_lock();
1244 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1245 rcu_read_unlock();
1da177e4 1246 return -EPERM;
7dc52157
PM
1247 }
1248 rcu_read_unlock();
1da177e4 1249
1da177e4
LT
1250 if (*ppos != 0) {
1251 /* No partial writes. */
1252 return -EINVAL;
1253 }
1da177e4 1254
774636e1
AD
1255 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1256 if (rv < 0)
1257 return rv;
81407c84
EP
1258
1259 /* is userspace tring to explicitly UNSET the loginuid? */
1260 if (loginuid == AUDIT_UID_UNSET) {
1261 kloginuid = INVALID_UID;
1262 } else {
1263 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
774636e1
AD
1264 if (!uid_valid(kloginuid))
1265 return -EINVAL;
e1760bd5
EB
1266 }
1267
774636e1
AD
1268 rv = audit_set_loginuid(kloginuid);
1269 if (rv < 0)
1270 return rv;
1271 return count;
1da177e4
LT
1272}
1273
00977a59 1274static const struct file_operations proc_loginuid_operations = {
1da177e4
LT
1275 .read = proc_loginuid_read,
1276 .write = proc_loginuid_write,
87df8424 1277 .llseek = generic_file_llseek,
1da177e4 1278};
1e0bd755
EP
1279
1280static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1281 size_t count, loff_t *ppos)
1282{
496ad9aa 1283 struct inode * inode = file_inode(file);
1e0bd755
EP
1284 struct task_struct *task = get_proc_task(inode);
1285 ssize_t length;
1286 char tmpbuf[TMPBUFLEN];
1287
1288 if (!task)
1289 return -ESRCH;
1290 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1291 audit_get_sessionid(task));
1292 put_task_struct(task);
1293 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1294}
1295
1296static const struct file_operations proc_sessionid_operations = {
1297 .read = proc_sessionid_read,
87df8424 1298 .llseek = generic_file_llseek,
1e0bd755 1299};
1da177e4
LT
1300#endif
1301
f4f154fd
AM
1302#ifdef CONFIG_FAULT_INJECTION
1303static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1304 size_t count, loff_t *ppos)
1305{
496ad9aa 1306 struct task_struct *task = get_proc_task(file_inode(file));
f4f154fd
AM
1307 char buffer[PROC_NUMBUF];
1308 size_t len;
1309 int make_it_fail;
f4f154fd
AM
1310
1311 if (!task)
1312 return -ESRCH;
1313 make_it_fail = task->make_it_fail;
1314 put_task_struct(task);
1315
1316 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
0c28f287
AM
1317
1318 return simple_read_from_buffer(buf, count, ppos, buffer, len);
f4f154fd
AM
1319}
1320
1321static ssize_t proc_fault_inject_write(struct file * file,
1322 const char __user * buf, size_t count, loff_t *ppos)
1323{
1324 struct task_struct *task;
774636e1 1325 char buffer[PROC_NUMBUF];
f4f154fd 1326 int make_it_fail;
774636e1 1327 int rv;
f4f154fd
AM
1328
1329 if (!capable(CAP_SYS_RESOURCE))
1330 return -EPERM;
1331 memset(buffer, 0, sizeof(buffer));
1332 if (count > sizeof(buffer) - 1)
1333 count = sizeof(buffer) - 1;
1334 if (copy_from_user(buffer, buf, count))
1335 return -EFAULT;
774636e1
AD
1336 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1337 if (rv < 0)
1338 return rv;
16caed31
DJ
1339 if (make_it_fail < 0 || make_it_fail > 1)
1340 return -EINVAL;
1341
496ad9aa 1342 task = get_proc_task(file_inode(file));
f4f154fd
AM
1343 if (!task)
1344 return -ESRCH;
1345 task->make_it_fail = make_it_fail;
1346 put_task_struct(task);
cba8aafe
VL
1347
1348 return count;
f4f154fd
AM
1349}
1350
00977a59 1351static const struct file_operations proc_fault_inject_operations = {
f4f154fd
AM
1352 .read = proc_fault_inject_read,
1353 .write = proc_fault_inject_write,
87df8424 1354 .llseek = generic_file_llseek,
f4f154fd
AM
1355};
1356#endif
1357
9745512c 1358
43ae34cb
IM
1359#ifdef CONFIG_SCHED_DEBUG
1360/*
1361 * Print out various scheduling related per-task fields:
1362 */
1363static int sched_show(struct seq_file *m, void *v)
1364{
1365 struct inode *inode = m->private;
1366 struct task_struct *p;
1367
43ae34cb
IM
1368 p = get_proc_task(inode);
1369 if (!p)
1370 return -ESRCH;
1371 proc_sched_show_task(p, m);
1372
1373 put_task_struct(p);
1374
1375 return 0;
1376}
1377
1378static ssize_t
1379sched_write(struct file *file, const char __user *buf,
1380 size_t count, loff_t *offset)
1381{
496ad9aa 1382 struct inode *inode = file_inode(file);
43ae34cb
IM
1383 struct task_struct *p;
1384
43ae34cb
IM
1385 p = get_proc_task(inode);
1386 if (!p)
1387 return -ESRCH;
1388 proc_sched_set_task(p);
1389
1390 put_task_struct(p);
1391
1392 return count;
1393}
1394
1395static int sched_open(struct inode *inode, struct file *filp)
1396{
c6a34058 1397 return single_open(filp, sched_show, inode);
43ae34cb
IM
1398}
1399
1400static const struct file_operations proc_pid_sched_operations = {
1401 .open = sched_open,
1402 .read = seq_read,
1403 .write = sched_write,
1404 .llseek = seq_lseek,
5ea473a1 1405 .release = single_release,
43ae34cb
IM
1406};
1407
1408#endif
1409
5091faa4
MG
1410#ifdef CONFIG_SCHED_AUTOGROUP
1411/*
1412 * Print out autogroup related information:
1413 */
1414static int sched_autogroup_show(struct seq_file *m, void *v)
1415{
1416 struct inode *inode = m->private;
1417 struct task_struct *p;
1418
1419 p = get_proc_task(inode);
1420 if (!p)
1421 return -ESRCH;
1422 proc_sched_autogroup_show_task(p, m);
1423
1424 put_task_struct(p);
1425
1426 return 0;
1427}
1428
1429static ssize_t
1430sched_autogroup_write(struct file *file, const char __user *buf,
1431 size_t count, loff_t *offset)
1432{
496ad9aa 1433 struct inode *inode = file_inode(file);
5091faa4
MG
1434 struct task_struct *p;
1435 char buffer[PROC_NUMBUF];
0a8cb8e3 1436 int nice;
5091faa4
MG
1437 int err;
1438
1439 memset(buffer, 0, sizeof(buffer));
1440 if (count > sizeof(buffer) - 1)
1441 count = sizeof(buffer) - 1;
1442 if (copy_from_user(buffer, buf, count))
1443 return -EFAULT;
1444
0a8cb8e3
AD
1445 err = kstrtoint(strstrip(buffer), 0, &nice);
1446 if (err < 0)
1447 return err;
5091faa4
MG
1448
1449 p = get_proc_task(inode);
1450 if (!p)
1451 return -ESRCH;
1452
2e5b5b3a 1453 err = proc_sched_autogroup_set_nice(p, nice);
5091faa4
MG
1454 if (err)
1455 count = err;
1456
1457 put_task_struct(p);
1458
1459 return count;
1460}
1461
1462static int sched_autogroup_open(struct inode *inode, struct file *filp)
1463{
1464 int ret;
1465
1466 ret = single_open(filp, sched_autogroup_show, NULL);
1467 if (!ret) {
1468 struct seq_file *m = filp->private_data;
1469
1470 m->private = inode;
1471 }
1472 return ret;
1473}
1474
1475static const struct file_operations proc_pid_sched_autogroup_operations = {
1476 .open = sched_autogroup_open,
1477 .read = seq_read,
1478 .write = sched_autogroup_write,
1479 .llseek = seq_lseek,
1480 .release = single_release,
1481};
1482
1483#endif /* CONFIG_SCHED_AUTOGROUP */
1484
4614a696
JS
1485static ssize_t comm_write(struct file *file, const char __user *buf,
1486 size_t count, loff_t *offset)
1487{
496ad9aa 1488 struct inode *inode = file_inode(file);
4614a696
JS
1489 struct task_struct *p;
1490 char buffer[TASK_COMM_LEN];
830e0fc9 1491 const size_t maxlen = sizeof(buffer) - 1;
4614a696
JS
1492
1493 memset(buffer, 0, sizeof(buffer));
830e0fc9 1494 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
4614a696
JS
1495 return -EFAULT;
1496
1497 p = get_proc_task(inode);
1498 if (!p)
1499 return -ESRCH;
1500
1501 if (same_thread_group(current, p))
1502 set_task_comm(p, buffer);
1503 else
1504 count = -EINVAL;
1505
1506 put_task_struct(p);
1507
1508 return count;
1509}
1510
1511static int comm_show(struct seq_file *m, void *v)
1512{
1513 struct inode *inode = m->private;
1514 struct task_struct *p;
1515
1516 p = get_proc_task(inode);
1517 if (!p)
1518 return -ESRCH;
1519
1520 task_lock(p);
1521 seq_printf(m, "%s\n", p->comm);
1522 task_unlock(p);
1523
1524 put_task_struct(p);
1525
1526 return 0;
1527}
1528
1529static int comm_open(struct inode *inode, struct file *filp)
1530{
c6a34058 1531 return single_open(filp, comm_show, inode);
4614a696
JS
1532}
1533
1534static const struct file_operations proc_pid_set_comm_operations = {
1535 .open = comm_open,
1536 .read = seq_read,
1537 .write = comm_write,
1538 .llseek = seq_lseek,
1539 .release = single_release,
1540};
1541
7773fbc5 1542static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
925d1c40
MH
1543{
1544 struct task_struct *task;
925d1c40
MH
1545 struct file *exe_file;
1546
2b0143b5 1547 task = get_proc_task(d_inode(dentry));
925d1c40
MH
1548 if (!task)
1549 return -ENOENT;
cd81a917 1550 exe_file = get_task_exe_file(task);
925d1c40 1551 put_task_struct(task);
925d1c40
MH
1552 if (exe_file) {
1553 *exe_path = exe_file->f_path;
1554 path_get(&exe_file->f_path);
1555 fput(exe_file);
1556 return 0;
1557 } else
1558 return -ENOENT;
1559}
1560
6b255391 1561static const char *proc_pid_get_link(struct dentry *dentry,
fceef393
AV
1562 struct inode *inode,
1563 struct delayed_call *done)
1da177e4 1564{
408ef013 1565 struct path path;
1da177e4
LT
1566 int error = -EACCES;
1567
6b255391
AV
1568 if (!dentry)
1569 return ERR_PTR(-ECHILD);
1570
778c1144
EB
1571 /* Are we allowed to snoop on the tasks file descriptors? */
1572 if (!proc_fd_access_allowed(inode))
1da177e4 1573 goto out;
1da177e4 1574
408ef013
CH
1575 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1576 if (error)
1577 goto out;
1578
6e77137b 1579 nd_jump_link(&path);
408ef013 1580 return NULL;
1da177e4 1581out:
008b150a 1582 return ERR_PTR(error);
1da177e4
LT
1583}
1584
3dcd25f3 1585static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1da177e4 1586{
e12ba74d 1587 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
3dcd25f3 1588 char *pathname;
1da177e4
LT
1589 int len;
1590
1591 if (!tmp)
1592 return -ENOMEM;
0c28f287 1593
7b2a69ba 1594 pathname = d_path(path, tmp, PAGE_SIZE);
3dcd25f3
JB
1595 len = PTR_ERR(pathname);
1596 if (IS_ERR(pathname))
1da177e4 1597 goto out;
3dcd25f3 1598 len = tmp + PAGE_SIZE - 1 - pathname;
1da177e4
LT
1599
1600 if (len > buflen)
1601 len = buflen;
3dcd25f3 1602 if (copy_to_user(buffer, pathname, len))
1da177e4
LT
1603 len = -EFAULT;
1604 out:
1605 free_page((unsigned long)tmp);
1606 return len;
1607}
1608
1609static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1610{
1611 int error = -EACCES;
2b0143b5 1612 struct inode *inode = d_inode(dentry);
3dcd25f3 1613 struct path path;
1da177e4 1614
778c1144
EB
1615 /* Are we allowed to snoop on the tasks file descriptors? */
1616 if (!proc_fd_access_allowed(inode))
1da177e4 1617 goto out;
1da177e4 1618
7773fbc5 1619 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1da177e4
LT
1620 if (error)
1621 goto out;
1622
3dcd25f3
JB
1623 error = do_proc_readlink(&path, buffer, buflen);
1624 path_put(&path);
1da177e4 1625out:
1da177e4
LT
1626 return error;
1627}
1628
faf60af1 1629const struct inode_operations proc_pid_link_inode_operations = {
1da177e4 1630 .readlink = proc_pid_readlink,
6b255391 1631 .get_link = proc_pid_get_link,
6d76fa58 1632 .setattr = proc_setattr,
1da177e4
LT
1633};
1634
28a6d671
EB
1635
1636/* building an inode */
1637
68eb94f1
EB
1638void task_dump_owner(struct task_struct *task, mode_t mode,
1639 kuid_t *ruid, kgid_t *rgid)
1640{
1641 /* Depending on the state of dumpable compute who should own a
1642 * proc file for a task.
1643 */
1644 const struct cred *cred;
1645 kuid_t uid;
1646 kgid_t gid;
1647
1648 /* Default to the tasks effective ownership */
1649 rcu_read_lock();
1650 cred = __task_cred(task);
1651 uid = cred->euid;
1652 gid = cred->egid;
1653 rcu_read_unlock();
1654
1655 /*
1656 * Before the /proc/pid/status file was created the only way to read
1657 * the effective uid of a /process was to stat /proc/pid. Reading
1658 * /proc/pid/status is slow enough that procps and other packages
1659 * kept stating /proc/pid. To keep the rules in /proc simple I have
1660 * made this apply to all per process world readable and executable
1661 * directories.
1662 */
1663 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1664 struct mm_struct *mm;
1665 task_lock(task);
1666 mm = task->mm;
1667 /* Make non-dumpable tasks owned by some root */
1668 if (mm) {
1669 if (get_dumpable(mm) != SUID_DUMP_USER) {
1670 struct user_namespace *user_ns = mm->user_ns;
1671
1672 uid = make_kuid(user_ns, 0);
1673 if (!uid_valid(uid))
1674 uid = GLOBAL_ROOT_UID;
1675
1676 gid = make_kgid(user_ns, 0);
1677 if (!gid_valid(gid))
1678 gid = GLOBAL_ROOT_GID;
1679 }
1680 } else {
1681 uid = GLOBAL_ROOT_UID;
1682 gid = GLOBAL_ROOT_GID;
1683 }
1684 task_unlock(task);
1685 }
1686 *ruid = uid;
1687 *rgid = gid;
1688}
1689
db978da8
AG
1690struct inode *proc_pid_make_inode(struct super_block * sb,
1691 struct task_struct *task, umode_t mode)
28a6d671
EB
1692{
1693 struct inode * inode;
1694 struct proc_inode *ei;
1da177e4 1695
28a6d671 1696 /* We need a new inode */
1da177e4 1697
28a6d671
EB
1698 inode = new_inode(sb);
1699 if (!inode)
1700 goto out;
1701
1702 /* Common stuff */
1703 ei = PROC_I(inode);
db978da8 1704 inode->i_mode = mode;
85fe4025 1705 inode->i_ino = get_next_ino();
078cd827 1706 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
28a6d671
EB
1707 inode->i_op = &proc_def_inode_operations;
1708
1709 /*
1710 * grab the reference to task.
1711 */
1a657f78 1712 ei->pid = get_task_pid(task, PIDTYPE_PID);
28a6d671
EB
1713 if (!ei->pid)
1714 goto out_unlock;
1715
68eb94f1 1716 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
28a6d671
EB
1717 security_task_to_inode(task, inode);
1718
1da177e4 1719out:
28a6d671
EB
1720 return inode;
1721
1722out_unlock:
1723 iput(inode);
1724 return NULL;
1da177e4
LT
1725}
1726
6b4e306a 1727int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1da177e4 1728{
2b0143b5 1729 struct inode *inode = d_inode(dentry);
28a6d671 1730 struct task_struct *task;
0499680a 1731 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
c69e8d9c 1732
28a6d671 1733 generic_fillattr(inode, stat);
1da177e4 1734
28a6d671 1735 rcu_read_lock();
dcb0f222
EB
1736 stat->uid = GLOBAL_ROOT_UID;
1737 stat->gid = GLOBAL_ROOT_GID;
28a6d671
EB
1738 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1739 if (task) {
796f571b 1740 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
0499680a
VK
1741 rcu_read_unlock();
1742 /*
1743 * This doesn't prevent learning whether PID exists,
1744 * it only makes getattr() consistent with readdir().
1745 */
1746 return -ENOENT;
1747 }
68eb94f1 1748 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1da177e4 1749 }
28a6d671 1750 rcu_read_unlock();
d6e71144 1751 return 0;
1da177e4
LT
1752}
1753
1da177e4
LT
1754/* dentry stuff */
1755
1756/*
1757 * Exceptional case: normally we are not allowed to unhash a busy
1758 * directory. In this case, however, we can do it - no aliasing problems
1759 * due to the way we treat inodes.
1760 *
1761 * Rewrite the inode's ownerships here because the owning task may have
1762 * performed a setuid(), etc.
99f89551 1763 *
1da177e4 1764 */
0b728e19 1765int pid_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4 1766{
34286d66
NP
1767 struct inode *inode;
1768 struct task_struct *task;
c69e8d9c 1769
0b728e19 1770 if (flags & LOOKUP_RCU)
34286d66
NP
1771 return -ECHILD;
1772
2b0143b5 1773 inode = d_inode(dentry);
34286d66
NP
1774 task = get_proc_task(inode);
1775
99f89551 1776 if (task) {
68eb94f1
EB
1777 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1778
9ee8ab9f 1779 inode->i_mode &= ~(S_ISUID | S_ISGID);
1da177e4 1780 security_task_to_inode(task, inode);
99f89551 1781 put_task_struct(task);
1da177e4
LT
1782 return 1;
1783 }
1da177e4
LT
1784 return 0;
1785}
1786
d855a4b7
ON
1787static inline bool proc_inode_is_dead(struct inode *inode)
1788{
1789 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1790}
1791
1dd704b6
DH
1792int pid_delete_dentry(const struct dentry *dentry)
1793{
1794 /* Is the task we represent dead?
1795 * If so, then don't put the dentry on the lru list,
1796 * kill it immediately.
1797 */
2b0143b5 1798 return proc_inode_is_dead(d_inode(dentry));
1dd704b6
DH
1799}
1800
6b4e306a 1801const struct dentry_operations pid_dentry_operations =
28a6d671
EB
1802{
1803 .d_revalidate = pid_revalidate,
1804 .d_delete = pid_delete_dentry,
1805};
1806
1807/* Lookups */
1808
1c0d04c9
EB
1809/*
1810 * Fill a directory entry.
1811 *
1812 * If possible create the dcache entry and derive our inode number and
1813 * file type from dcache entry.
1814 *
1815 * Since all of the proc inode numbers are dynamically generated, the inode
1816 * numbers do not exist until the inode is cache. This means creating the
1817 * the dcache entry in readdir is necessary to keep the inode numbers
1818 * reported by readdir in sync with the inode numbers reported
1819 * by stat.
1820 */
f0c3b509 1821bool proc_fill_cache(struct file *file, struct dir_context *ctx,
6b4e306a 1822 const char *name, int len,
c5141e6d 1823 instantiate_t instantiate, struct task_struct *task, const void *ptr)
61a28784 1824{
f0c3b509 1825 struct dentry *child, *dir = file->f_path.dentry;
1df98b8b 1826 struct qstr qname = QSTR_INIT(name, len);
61a28784 1827 struct inode *inode;
1df98b8b
AV
1828 unsigned type;
1829 ino_t ino;
61a28784 1830
1df98b8b 1831 child = d_hash_and_lookup(dir, &qname);
61a28784 1832 if (!child) {
3781764b
AV
1833 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1834 child = d_alloc_parallel(dir, &qname, &wq);
1835 if (IS_ERR(child))
1df98b8b 1836 goto end_instantiate;
3781764b
AV
1837 if (d_in_lookup(child)) {
1838 int err = instantiate(d_inode(dir), child, task, ptr);
1839 d_lookup_done(child);
1840 if (err < 0) {
1841 dput(child);
1842 goto end_instantiate;
1843 }
61a28784
EB
1844 }
1845 }
2b0143b5 1846 inode = d_inode(child);
147ce699
AV
1847 ino = inode->i_ino;
1848 type = inode->i_mode >> 12;
61a28784 1849 dput(child);
f0c3b509 1850 return dir_emit(ctx, name, len, ino, type);
1df98b8b
AV
1851
1852end_instantiate:
1853 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
61a28784
EB
1854}
1855
640708a2
PE
1856/*
1857 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1858 * which represent vma start and end addresses.
1859 */
1860static int dname_to_vma_addr(struct dentry *dentry,
1861 unsigned long *start, unsigned long *end)
1862{
1863 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1864 return -EINVAL;
1865
1866 return 0;
1867}
1868
0b728e19 1869static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
640708a2
PE
1870{
1871 unsigned long vm_start, vm_end;
1872 bool exact_vma_exists = false;
1873 struct mm_struct *mm = NULL;
1874 struct task_struct *task;
640708a2
PE
1875 struct inode *inode;
1876 int status = 0;
1877
0b728e19 1878 if (flags & LOOKUP_RCU)
640708a2
PE
1879 return -ECHILD;
1880
2b0143b5 1881 inode = d_inode(dentry);
640708a2
PE
1882 task = get_proc_task(inode);
1883 if (!task)
1884 goto out_notask;
1885
caaee623 1886 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
2344bec7 1887 if (IS_ERR_OR_NULL(mm))
640708a2
PE
1888 goto out;
1889
1890 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1891 down_read(&mm->mmap_sem);
1892 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1893 up_read(&mm->mmap_sem);
1894 }
1895
1896 mmput(mm);
1897
1898 if (exact_vma_exists) {
68eb94f1
EB
1899 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1900
640708a2
PE
1901 security_task_to_inode(task, inode);
1902 status = 1;
1903 }
1904
1905out:
1906 put_task_struct(task);
1907
1908out_notask:
640708a2
PE
1909 return status;
1910}
1911
1912static const struct dentry_operations tid_map_files_dentry_operations = {
1913 .d_revalidate = map_files_d_revalidate,
1914 .d_delete = pid_delete_dentry,
1915};
1916
6b255391 1917static int map_files_get_link(struct dentry *dentry, struct path *path)
640708a2
PE
1918{
1919 unsigned long vm_start, vm_end;
1920 struct vm_area_struct *vma;
1921 struct task_struct *task;
1922 struct mm_struct *mm;
1923 int rc;
1924
1925 rc = -ENOENT;
2b0143b5 1926 task = get_proc_task(d_inode(dentry));
640708a2
PE
1927 if (!task)
1928 goto out;
1929
1930 mm = get_task_mm(task);
1931 put_task_struct(task);
1932 if (!mm)
1933 goto out;
1934
1935 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1936 if (rc)
1937 goto out_mmput;
1938
70335abb 1939 rc = -ENOENT;
640708a2
PE
1940 down_read(&mm->mmap_sem);
1941 vma = find_exact_vma(mm, vm_start, vm_end);
1942 if (vma && vma->vm_file) {
1943 *path = vma->vm_file->f_path;
1944 path_get(path);
1945 rc = 0;
1946 }
1947 up_read(&mm->mmap_sem);
1948
1949out_mmput:
1950 mmput(mm);
1951out:
1952 return rc;
1953}
1954
1955struct map_files_info {
7b540d06 1956 fmode_t mode;
9a87fe0d 1957 unsigned int len;
640708a2
PE
1958 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1959};
1960
bdb4d100
CO
1961/*
1962 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1963 * symlinks may be used to bypass permissions on ancestor directories in the
1964 * path to the file in question.
1965 */
1966static const char *
6b255391 1967proc_map_files_get_link(struct dentry *dentry,
fceef393
AV
1968 struct inode *inode,
1969 struct delayed_call *done)
bdb4d100
CO
1970{
1971 if (!capable(CAP_SYS_ADMIN))
1972 return ERR_PTR(-EPERM);
1973
fceef393 1974 return proc_pid_get_link(dentry, inode, done);
bdb4d100
CO
1975}
1976
1977/*
6b255391 1978 * Identical to proc_pid_link_inode_operations except for get_link()
bdb4d100
CO
1979 */
1980static const struct inode_operations proc_map_files_link_inode_operations = {
1981 .readlink = proc_pid_readlink,
6b255391 1982 .get_link = proc_map_files_get_link,
bdb4d100
CO
1983 .setattr = proc_setattr,
1984};
1985
c52a47ac 1986static int
640708a2
PE
1987proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1988 struct task_struct *task, const void *ptr)
1989{
7b540d06 1990 fmode_t mode = (fmode_t)(unsigned long)ptr;
640708a2
PE
1991 struct proc_inode *ei;
1992 struct inode *inode;
1993
db978da8
AG
1994 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
1995 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
1996 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
640708a2 1997 if (!inode)
c52a47ac 1998 return -ENOENT;
640708a2
PE
1999
2000 ei = PROC_I(inode);
6b255391 2001 ei->op.proc_get_link = map_files_get_link;
640708a2 2002
bdb4d100 2003 inode->i_op = &proc_map_files_link_inode_operations;
640708a2 2004 inode->i_size = 64;
640708a2
PE
2005
2006 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2007 d_add(dentry, inode);
2008
c52a47ac 2009 return 0;
640708a2
PE
2010}
2011
2012static struct dentry *proc_map_files_lookup(struct inode *dir,
00cd8dd3 2013 struct dentry *dentry, unsigned int flags)
640708a2
PE
2014{
2015 unsigned long vm_start, vm_end;
2016 struct vm_area_struct *vma;
2017 struct task_struct *task;
c52a47ac 2018 int result;
640708a2
PE
2019 struct mm_struct *mm;
2020
c52a47ac 2021 result = -ENOENT;
640708a2
PE
2022 task = get_proc_task(dir);
2023 if (!task)
2024 goto out;
2025
c52a47ac 2026 result = -EACCES;
caaee623 2027 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
640708a2
PE
2028 goto out_put_task;
2029
c52a47ac 2030 result = -ENOENT;
640708a2 2031 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
eb94cd96 2032 goto out_put_task;
640708a2
PE
2033
2034 mm = get_task_mm(task);
2035 if (!mm)
eb94cd96 2036 goto out_put_task;
640708a2
PE
2037
2038 down_read(&mm->mmap_sem);
2039 vma = find_exact_vma(mm, vm_start, vm_end);
2040 if (!vma)
2041 goto out_no_vma;
2042
05f56484
SK
2043 if (vma->vm_file)
2044 result = proc_map_files_instantiate(dir, dentry, task,
2045 (void *)(unsigned long)vma->vm_file->f_mode);
640708a2
PE
2046
2047out_no_vma:
2048 up_read(&mm->mmap_sem);
2049 mmput(mm);
640708a2
PE
2050out_put_task:
2051 put_task_struct(task);
2052out:
c52a47ac 2053 return ERR_PTR(result);
640708a2
PE
2054}
2055
2056static const struct inode_operations proc_map_files_inode_operations = {
2057 .lookup = proc_map_files_lookup,
2058 .permission = proc_fd_permission,
2059 .setattr = proc_setattr,
2060};
2061
2062static int
f0c3b509 2063proc_map_files_readdir(struct file *file, struct dir_context *ctx)
640708a2 2064{
640708a2
PE
2065 struct vm_area_struct *vma;
2066 struct task_struct *task;
2067 struct mm_struct *mm;
f0c3b509
AV
2068 unsigned long nr_files, pos, i;
2069 struct flex_array *fa = NULL;
2070 struct map_files_info info;
2071 struct map_files_info *p;
640708a2
PE
2072 int ret;
2073
640708a2 2074 ret = -ENOENT;
f0c3b509 2075 task = get_proc_task(file_inode(file));
640708a2
PE
2076 if (!task)
2077 goto out;
2078
2079 ret = -EACCES;
caaee623 2080 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
640708a2
PE
2081 goto out_put_task;
2082
2083 ret = 0;
f0c3b509
AV
2084 if (!dir_emit_dots(file, ctx))
2085 goto out_put_task;
640708a2 2086
f0c3b509
AV
2087 mm = get_task_mm(task);
2088 if (!mm)
2089 goto out_put_task;
2090 down_read(&mm->mmap_sem);
640708a2 2091
f0c3b509 2092 nr_files = 0;
640708a2 2093
f0c3b509
AV
2094 /*
2095 * We need two passes here:
2096 *
2097 * 1) Collect vmas of mapped files with mmap_sem taken
2098 * 2) Release mmap_sem and instantiate entries
2099 *
2100 * otherwise we get lockdep complained, since filldir()
2101 * routine might require mmap_sem taken in might_fault().
2102 */
640708a2 2103
f0c3b509
AV
2104 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2105 if (vma->vm_file && ++pos > ctx->pos)
2106 nr_files++;
2107 }
2108
2109 if (nr_files) {
2110 fa = flex_array_alloc(sizeof(info), nr_files,
2111 GFP_KERNEL);
2112 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2113 GFP_KERNEL)) {
2114 ret = -ENOMEM;
2115 if (fa)
2116 flex_array_free(fa);
2117 up_read(&mm->mmap_sem);
2118 mmput(mm);
2119 goto out_put_task;
640708a2 2120 }
f0c3b509
AV
2121 for (i = 0, vma = mm->mmap, pos = 2; vma;
2122 vma = vma->vm_next) {
2123 if (!vma->vm_file)
2124 continue;
2125 if (++pos <= ctx->pos)
2126 continue;
2127
2128 info.mode = vma->vm_file->f_mode;
2129 info.len = snprintf(info.name,
2130 sizeof(info.name), "%lx-%lx",
2131 vma->vm_start, vma->vm_end);
2132 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2133 BUG();
640708a2 2134 }
640708a2 2135 }
f0c3b509
AV
2136 up_read(&mm->mmap_sem);
2137
2138 for (i = 0; i < nr_files; i++) {
2139 p = flex_array_get(fa, i);
2140 if (!proc_fill_cache(file, ctx,
2141 p->name, p->len,
2142 proc_map_files_instantiate,
2143 task,
2144 (void *)(unsigned long)p->mode))
2145 break;
2146 ctx->pos++;
640708a2 2147 }
f0c3b509
AV
2148 if (fa)
2149 flex_array_free(fa);
2150 mmput(mm);
640708a2 2151
640708a2
PE
2152out_put_task:
2153 put_task_struct(task);
2154out:
2155 return ret;
2156}
2157
2158static const struct file_operations proc_map_files_operations = {
2159 .read = generic_read_dir,
f50752ea
AV
2160 .iterate_shared = proc_map_files_readdir,
2161 .llseek = generic_file_llseek,
640708a2
PE
2162};
2163
b18b6a9c 2164#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
48f6a7a5
PE
2165struct timers_private {
2166 struct pid *pid;
2167 struct task_struct *task;
2168 struct sighand_struct *sighand;
57b8015e 2169 struct pid_namespace *ns;
48f6a7a5
PE
2170 unsigned long flags;
2171};
2172
2173static void *timers_start(struct seq_file *m, loff_t *pos)
2174{
2175 struct timers_private *tp = m->private;
2176
2177 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2178 if (!tp->task)
2179 return ERR_PTR(-ESRCH);
2180
2181 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2182 if (!tp->sighand)
2183 return ERR_PTR(-ESRCH);
2184
2185 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2186}
2187
2188static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2189{
2190 struct timers_private *tp = m->private;
2191 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2192}
2193
2194static void timers_stop(struct seq_file *m, void *v)
2195{
2196 struct timers_private *tp = m->private;
2197
2198 if (tp->sighand) {
2199 unlock_task_sighand(tp->task, &tp->flags);
2200 tp->sighand = NULL;
2201 }
2202
2203 if (tp->task) {
2204 put_task_struct(tp->task);
2205 tp->task = NULL;
2206 }
2207}
2208
2209static int show_timer(struct seq_file *m, void *v)
2210{
2211 struct k_itimer *timer;
57b8015e
PE
2212 struct timers_private *tp = m->private;
2213 int notify;
cedbccab 2214 static const char * const nstr[] = {
57b8015e
PE
2215 [SIGEV_SIGNAL] = "signal",
2216 [SIGEV_NONE] = "none",
2217 [SIGEV_THREAD] = "thread",
2218 };
48f6a7a5
PE
2219
2220 timer = list_entry((struct list_head *)v, struct k_itimer, list);
57b8015e
PE
2221 notify = timer->it_sigev_notify;
2222
48f6a7a5 2223 seq_printf(m, "ID: %d\n", timer->it_id);
25ce3191
JP
2224 seq_printf(m, "signal: %d/%p\n",
2225 timer->sigq->info.si_signo,
2226 timer->sigq->info.si_value.sival_ptr);
57b8015e 2227 seq_printf(m, "notify: %s/%s.%d\n",
25ce3191
JP
2228 nstr[notify & ~SIGEV_THREAD_ID],
2229 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2230 pid_nr_ns(timer->it_pid, tp->ns));
15ef0298 2231 seq_printf(m, "ClockID: %d\n", timer->it_clock);
48f6a7a5
PE
2232
2233 return 0;
2234}
2235
2236static const struct seq_operations proc_timers_seq_ops = {
2237 .start = timers_start,
2238 .next = timers_next,
2239 .stop = timers_stop,
2240 .show = show_timer,
2241};
2242
2243static int proc_timers_open(struct inode *inode, struct file *file)
2244{
2245 struct timers_private *tp;
2246
2247 tp = __seq_open_private(file, &proc_timers_seq_ops,
2248 sizeof(struct timers_private));
2249 if (!tp)
2250 return -ENOMEM;
2251
2252 tp->pid = proc_pid(inode);
57b8015e 2253 tp->ns = inode->i_sb->s_fs_info;
48f6a7a5
PE
2254 return 0;
2255}
2256
2257static const struct file_operations proc_timers_operations = {
2258 .open = proc_timers_open,
2259 .read = seq_read,
2260 .llseek = seq_lseek,
2261 .release = seq_release_private,
2262};
b5946bea 2263#endif
640708a2 2264
5de23d43
JS
2265static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2266 size_t count, loff_t *offset)
2267{
2268 struct inode *inode = file_inode(file);
2269 struct task_struct *p;
2270 u64 slack_ns;
2271 int err;
2272
2273 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2274 if (err < 0)
2275 return err;
2276
2277 p = get_proc_task(inode);
2278 if (!p)
2279 return -ESRCH;
2280
4b2bd5fe
JS
2281 if (p != current) {
2282 if (!capable(CAP_SYS_NICE)) {
2283 count = -EPERM;
2284 goto out;
2285 }
5de23d43 2286
4b2bd5fe
JS
2287 err = security_task_setscheduler(p);
2288 if (err) {
2289 count = err;
2290 goto out;
2291 }
904763e1
JS
2292 }
2293
7abbaf94
JS
2294 task_lock(p);
2295 if (slack_ns == 0)
2296 p->timer_slack_ns = p->default_timer_slack_ns;
2297 else
2298 p->timer_slack_ns = slack_ns;
2299 task_unlock(p);
2300
2301out:
5de23d43
JS
2302 put_task_struct(p);
2303
2304 return count;
2305}
2306
2307static int timerslack_ns_show(struct seq_file *m, void *v)
2308{
2309 struct inode *inode = m->private;
2310 struct task_struct *p;
7abbaf94 2311 int err = 0;
5de23d43
JS
2312
2313 p = get_proc_task(inode);
2314 if (!p)
2315 return -ESRCH;
2316
4b2bd5fe 2317 if (p != current) {
5de23d43 2318
4b2bd5fe
JS
2319 if (!capable(CAP_SYS_NICE)) {
2320 err = -EPERM;
2321 goto out;
2322 }
2323 err = security_task_getscheduler(p);
2324 if (err)
2325 goto out;
2326 }
904763e1 2327
7abbaf94
JS
2328 task_lock(p);
2329 seq_printf(m, "%llu\n", p->timer_slack_ns);
2330 task_unlock(p);
2331
2332out:
5de23d43
JS
2333 put_task_struct(p);
2334
2335 return err;
2336}
2337
2338static int timerslack_ns_open(struct inode *inode, struct file *filp)
2339{
2340 return single_open(filp, timerslack_ns_show, inode);
2341}
2342
2343static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2344 .open = timerslack_ns_open,
2345 .read = seq_read,
2346 .write = timerslack_ns_write,
2347 .llseek = seq_lseek,
2348 .release = single_release,
2349};
2350
c52a47ac 2351static int proc_pident_instantiate(struct inode *dir,
c5141e6d 2352 struct dentry *dentry, struct task_struct *task, const void *ptr)
444ceed8 2353{
c5141e6d 2354 const struct pid_entry *p = ptr;
444ceed8
EB
2355 struct inode *inode;
2356 struct proc_inode *ei;
444ceed8 2357
db978da8 2358 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
444ceed8
EB
2359 if (!inode)
2360 goto out;
2361
2362 ei = PROC_I(inode);
444ceed8 2363 if (S_ISDIR(inode->i_mode))
bfe86848 2364 set_nlink(inode, 2); /* Use getattr to fix if necessary */
444ceed8
EB
2365 if (p->iop)
2366 inode->i_op = p->iop;
2367 if (p->fop)
2368 inode->i_fop = p->fop;
2369 ei->op = p->op;
fb045adb 2370 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
2371 d_add(dentry, inode);
2372 /* Close the race of the process dying before we return the dentry */
0b728e19 2373 if (pid_revalidate(dentry, 0))
c52a47ac 2374 return 0;
444ceed8 2375out:
c52a47ac 2376 return -ENOENT;
444ceed8
EB
2377}
2378
1da177e4
LT
2379static struct dentry *proc_pident_lookup(struct inode *dir,
2380 struct dentry *dentry,
c5141e6d 2381 const struct pid_entry *ents,
7bcd6b0e 2382 unsigned int nents)
1da177e4 2383{
c52a47ac 2384 int error;
99f89551 2385 struct task_struct *task = get_proc_task(dir);
c5141e6d 2386 const struct pid_entry *p, *last;
1da177e4 2387
c52a47ac 2388 error = -ENOENT;
1da177e4 2389
99f89551
EB
2390 if (!task)
2391 goto out_no_task;
1da177e4 2392
20cdc894
EB
2393 /*
2394 * Yes, it does not scale. And it should not. Don't add
2395 * new entries into /proc/<tgid>/ without very good reasons.
2396 */
bac5f5d5
AD
2397 last = &ents[nents];
2398 for (p = ents; p < last; p++) {
1da177e4
LT
2399 if (p->len != dentry->d_name.len)
2400 continue;
2401 if (!memcmp(dentry->d_name.name, p->name, p->len))
2402 break;
2403 }
bac5f5d5 2404 if (p >= last)
1da177e4
LT
2405 goto out;
2406
444ceed8 2407 error = proc_pident_instantiate(dir, dentry, task, p);
1da177e4 2408out:
99f89551
EB
2409 put_task_struct(task);
2410out_no_task:
c52a47ac 2411 return ERR_PTR(error);
1da177e4
LT
2412}
2413
f0c3b509 2414static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
c5141e6d 2415 const struct pid_entry *ents, unsigned int nents)
28a6d671 2416{
f0c3b509
AV
2417 struct task_struct *task = get_proc_task(file_inode(file));
2418 const struct pid_entry *p;
28a6d671 2419
28a6d671 2420 if (!task)
f0c3b509 2421 return -ENOENT;
28a6d671 2422
f0c3b509
AV
2423 if (!dir_emit_dots(file, ctx))
2424 goto out;
2425
2426 if (ctx->pos >= nents + 2)
2427 goto out;
28a6d671 2428
bac5f5d5 2429 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
f0c3b509
AV
2430 if (!proc_fill_cache(file, ctx, p->name, p->len,
2431 proc_pident_instantiate, task, p))
2432 break;
2433 ctx->pos++;
2434 }
28a6d671 2435out:
61a28784 2436 put_task_struct(task);
f0c3b509 2437 return 0;
1da177e4
LT
2438}
2439
28a6d671
EB
2440#ifdef CONFIG_SECURITY
2441static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2442 size_t count, loff_t *ppos)
2443{
496ad9aa 2444 struct inode * inode = file_inode(file);
04ff9708 2445 char *p = NULL;
28a6d671
EB
2446 ssize_t length;
2447 struct task_struct *task = get_proc_task(inode);
2448
28a6d671 2449 if (!task)
04ff9708 2450 return -ESRCH;
28a6d671
EB
2451
2452 length = security_getprocattr(task,
2fddfeef 2453 (char*)file->f_path.dentry->d_name.name,
04ff9708 2454 &p);
28a6d671 2455 put_task_struct(task);
04ff9708
AV
2456 if (length > 0)
2457 length = simple_read_from_buffer(buf, count, ppos, p, length);
2458 kfree(p);
28a6d671 2459 return length;
1da177e4
LT
2460}
2461
28a6d671
EB
2462static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2463 size_t count, loff_t *ppos)
2464{
496ad9aa 2465 struct inode * inode = file_inode(file);
bb646cdb 2466 void *page;
28a6d671
EB
2467 ssize_t length;
2468 struct task_struct *task = get_proc_task(inode);
2469
2470 length = -ESRCH;
2471 if (!task)
2472 goto out_no_task;
b21507e2
SS
2473
2474 /* A task may only write its own attributes. */
2475 length = -EACCES;
2476 if (current != task)
2477 goto out;
2478
28a6d671
EB
2479 if (count > PAGE_SIZE)
2480 count = PAGE_SIZE;
2481
2482 /* No partial writes. */
2483 length = -EINVAL;
2484 if (*ppos != 0)
2485 goto out;
2486
bb646cdb
AV
2487 page = memdup_user(buf, count);
2488 if (IS_ERR(page)) {
2489 length = PTR_ERR(page);
28a6d671 2490 goto out;
bb646cdb 2491 }
28a6d671 2492
107db7c7 2493 /* Guard against adverse ptrace interaction */
b21507e2 2494 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
107db7c7
DH
2495 if (length < 0)
2496 goto out_free;
2497
b21507e2 2498 length = security_setprocattr(file->f_path.dentry->d_name.name,
bb646cdb 2499 page, count);
b21507e2 2500 mutex_unlock(&current->signal->cred_guard_mutex);
28a6d671 2501out_free:
bb646cdb 2502 kfree(page);
28a6d671
EB
2503out:
2504 put_task_struct(task);
2505out_no_task:
2506 return length;
2507}
2508
00977a59 2509static const struct file_operations proc_pid_attr_operations = {
28a6d671
EB
2510 .read = proc_pid_attr_read,
2511 .write = proc_pid_attr_write,
87df8424 2512 .llseek = generic_file_llseek,
28a6d671
EB
2513};
2514
c5141e6d 2515static const struct pid_entry attr_dir_stuff[] = {
631f9c18
AD
2516 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2517 REG("prev", S_IRUGO, proc_pid_attr_operations),
2518 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2519 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2520 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2521 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
28a6d671
EB
2522};
2523
f0c3b509 2524static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
28a6d671 2525{
f0c3b509
AV
2526 return proc_pident_readdir(file, ctx,
2527 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
28a6d671
EB
2528}
2529
00977a59 2530static const struct file_operations proc_attr_dir_operations = {
1da177e4 2531 .read = generic_read_dir,
f50752ea
AV
2532 .iterate_shared = proc_attr_dir_readdir,
2533 .llseek = generic_file_llseek,
1da177e4
LT
2534};
2535
72d9dcfc 2536static struct dentry *proc_attr_dir_lookup(struct inode *dir,
00cd8dd3 2537 struct dentry *dentry, unsigned int flags)
28a6d671 2538{
7bcd6b0e
EB
2539 return proc_pident_lookup(dir, dentry,
2540 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
28a6d671
EB
2541}
2542
c5ef1c42 2543static const struct inode_operations proc_attr_dir_inode_operations = {
72d9dcfc 2544 .lookup = proc_attr_dir_lookup,
99f89551 2545 .getattr = pid_getattr,
6d76fa58 2546 .setattr = proc_setattr,
1da177e4
LT
2547};
2548
28a6d671
EB
2549#endif
2550
698ba7b5 2551#ifdef CONFIG_ELF_CORE
3cb4a0bb
KH
2552static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2553 size_t count, loff_t *ppos)
2554{
496ad9aa 2555 struct task_struct *task = get_proc_task(file_inode(file));
3cb4a0bb
KH
2556 struct mm_struct *mm;
2557 char buffer[PROC_NUMBUF];
2558 size_t len;
2559 int ret;
2560
2561 if (!task)
2562 return -ESRCH;
2563
2564 ret = 0;
2565 mm = get_task_mm(task);
2566 if (mm) {
2567 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2568 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2569 MMF_DUMP_FILTER_SHIFT));
2570 mmput(mm);
2571 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2572 }
2573
2574 put_task_struct(task);
2575
2576 return ret;
2577}
2578
2579static ssize_t proc_coredump_filter_write(struct file *file,
2580 const char __user *buf,
2581 size_t count,
2582 loff_t *ppos)
2583{
2584 struct task_struct *task;
2585 struct mm_struct *mm;
3cb4a0bb
KH
2586 unsigned int val;
2587 int ret;
2588 int i;
2589 unsigned long mask;
2590
774636e1
AD
2591 ret = kstrtouint_from_user(buf, count, 0, &val);
2592 if (ret < 0)
2593 return ret;
3cb4a0bb
KH
2594
2595 ret = -ESRCH;
496ad9aa 2596 task = get_proc_task(file_inode(file));
3cb4a0bb
KH
2597 if (!task)
2598 goto out_no_task;
2599
3cb4a0bb
KH
2600 mm = get_task_mm(task);
2601 if (!mm)
2602 goto out_no_mm;
41a0c249 2603 ret = 0;
3cb4a0bb
KH
2604
2605 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2606 if (val & mask)
2607 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2608 else
2609 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2610 }
2611
2612 mmput(mm);
2613 out_no_mm:
2614 put_task_struct(task);
2615 out_no_task:
774636e1
AD
2616 if (ret < 0)
2617 return ret;
2618 return count;
3cb4a0bb
KH
2619}
2620
2621static const struct file_operations proc_coredump_filter_operations = {
2622 .read = proc_coredump_filter_read,
2623 .write = proc_coredump_filter_write,
87df8424 2624 .llseek = generic_file_llseek,
3cb4a0bb
KH
2625};
2626#endif
2627
aba76fdb 2628#ifdef CONFIG_TASK_IO_ACCOUNTING
19aadc98 2629static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
297c5d92 2630{
940389b8 2631 struct task_io_accounting acct = task->ioac;
5995477a 2632 unsigned long flags;
293eb1e7 2633 int result;
5995477a 2634
293eb1e7
VK
2635 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2636 if (result)
2637 return result;
2638
caaee623 2639 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
293eb1e7
VK
2640 result = -EACCES;
2641 goto out_unlock;
2642 }
1d1221f3 2643
5995477a
AR
2644 if (whole && lock_task_sighand(task, &flags)) {
2645 struct task_struct *t = task;
2646
2647 task_io_accounting_add(&acct, &task->signal->ioac);
2648 while_each_thread(task, t)
2649 task_io_accounting_add(&acct, &t->ioac);
2650
2651 unlock_task_sighand(task, &flags);
297c5d92 2652 }
25ce3191
JP
2653 seq_printf(m,
2654 "rchar: %llu\n"
2655 "wchar: %llu\n"
2656 "syscr: %llu\n"
2657 "syscw: %llu\n"
2658 "read_bytes: %llu\n"
2659 "write_bytes: %llu\n"
2660 "cancelled_write_bytes: %llu\n",
2661 (unsigned long long)acct.rchar,
2662 (unsigned long long)acct.wchar,
2663 (unsigned long long)acct.syscr,
2664 (unsigned long long)acct.syscw,
2665 (unsigned long long)acct.read_bytes,
2666 (unsigned long long)acct.write_bytes,
2667 (unsigned long long)acct.cancelled_write_bytes);
2668 result = 0;
2669
293eb1e7
VK
2670out_unlock:
2671 mutex_unlock(&task->signal->cred_guard_mutex);
2672 return result;
297c5d92
AR
2673}
2674
19aadc98
AD
2675static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2676 struct pid *pid, struct task_struct *task)
297c5d92 2677{
19aadc98 2678 return do_io_accounting(task, m, 0);
aba76fdb 2679}
297c5d92 2680
19aadc98
AD
2681static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2682 struct pid *pid, struct task_struct *task)
297c5d92 2683{
19aadc98 2684 return do_io_accounting(task, m, 1);
297c5d92
AR
2685}
2686#endif /* CONFIG_TASK_IO_ACCOUNTING */
aba76fdb 2687
22d917d8
EB
2688#ifdef CONFIG_USER_NS
2689static int proc_id_map_open(struct inode *inode, struct file *file,
ccf94f1b 2690 const struct seq_operations *seq_ops)
22d917d8
EB
2691{
2692 struct user_namespace *ns = NULL;
2693 struct task_struct *task;
2694 struct seq_file *seq;
2695 int ret = -EINVAL;
2696
2697 task = get_proc_task(inode);
2698 if (task) {
2699 rcu_read_lock();
2700 ns = get_user_ns(task_cred_xxx(task, user_ns));
2701 rcu_read_unlock();
2702 put_task_struct(task);
2703 }
2704 if (!ns)
2705 goto err;
2706
2707 ret = seq_open(file, seq_ops);
2708 if (ret)
2709 goto err_put_ns;
2710
2711 seq = file->private_data;
2712 seq->private = ns;
2713
2714 return 0;
2715err_put_ns:
2716 put_user_ns(ns);
2717err:
2718 return ret;
2719}
2720
2721static int proc_id_map_release(struct inode *inode, struct file *file)
2722{
2723 struct seq_file *seq = file->private_data;
2724 struct user_namespace *ns = seq->private;
2725 put_user_ns(ns);
2726 return seq_release(inode, file);
2727}
2728
2729static int proc_uid_map_open(struct inode *inode, struct file *file)
2730{
2731 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2732}
2733
2734static int proc_gid_map_open(struct inode *inode, struct file *file)
2735{
2736 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2737}
2738
f76d207a
EB
2739static int proc_projid_map_open(struct inode *inode, struct file *file)
2740{
2741 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2742}
2743
22d917d8
EB
2744static const struct file_operations proc_uid_map_operations = {
2745 .open = proc_uid_map_open,
2746 .write = proc_uid_map_write,
2747 .read = seq_read,
2748 .llseek = seq_lseek,
2749 .release = proc_id_map_release,
2750};
2751
2752static const struct file_operations proc_gid_map_operations = {
2753 .open = proc_gid_map_open,
2754 .write = proc_gid_map_write,
2755 .read = seq_read,
2756 .llseek = seq_lseek,
2757 .release = proc_id_map_release,
2758};
f76d207a
EB
2759
2760static const struct file_operations proc_projid_map_operations = {
2761 .open = proc_projid_map_open,
2762 .write = proc_projid_map_write,
2763 .read = seq_read,
2764 .llseek = seq_lseek,
2765 .release = proc_id_map_release,
2766};
9cc46516
EB
2767
2768static int proc_setgroups_open(struct inode *inode, struct file *file)
2769{
2770 struct user_namespace *ns = NULL;
2771 struct task_struct *task;
2772 int ret;
2773
2774 ret = -ESRCH;
2775 task = get_proc_task(inode);
2776 if (task) {
2777 rcu_read_lock();
2778 ns = get_user_ns(task_cred_xxx(task, user_ns));
2779 rcu_read_unlock();
2780 put_task_struct(task);
2781 }
2782 if (!ns)
2783 goto err;
2784
2785 if (file->f_mode & FMODE_WRITE) {
2786 ret = -EACCES;
2787 if (!ns_capable(ns, CAP_SYS_ADMIN))
2788 goto err_put_ns;
2789 }
2790
2791 ret = single_open(file, &proc_setgroups_show, ns);
2792 if (ret)
2793 goto err_put_ns;
2794
2795 return 0;
2796err_put_ns:
2797 put_user_ns(ns);
2798err:
2799 return ret;
2800}
2801
2802static int proc_setgroups_release(struct inode *inode, struct file *file)
2803{
2804 struct seq_file *seq = file->private_data;
2805 struct user_namespace *ns = seq->private;
2806 int ret = single_release(inode, file);
2807 put_user_ns(ns);
2808 return ret;
2809}
2810
2811static const struct file_operations proc_setgroups_operations = {
2812 .open = proc_setgroups_open,
2813 .write = proc_setgroups_write,
2814 .read = seq_read,
2815 .llseek = seq_lseek,
2816 .release = proc_setgroups_release,
2817};
22d917d8
EB
2818#endif /* CONFIG_USER_NS */
2819
47830723
KC
2820static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2821 struct pid *pid, struct task_struct *task)
2822{
a9712bc1
AV
2823 int err = lock_trace(task);
2824 if (!err) {
2825 seq_printf(m, "%08x\n", task->personality);
2826 unlock_trace(task);
2827 }
2828 return err;
47830723
KC
2829}
2830
28a6d671
EB
2831/*
2832 * Thread groups
2833 */
00977a59 2834static const struct file_operations proc_task_operations;
c5ef1c42 2835static const struct inode_operations proc_task_inode_operations;
20cdc894 2836
c5141e6d 2837static const struct pid_entry tgid_base_stuff[] = {
631f9c18
AD
2838 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2839 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
640708a2 2840 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
631f9c18 2841 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
6b4e306a 2842 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
b2211a36 2843#ifdef CONFIG_NET
631f9c18 2844 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
b2211a36 2845#endif
631f9c18 2846 REG("environ", S_IRUSR, proc_environ_operations),
c5317167 2847 REG("auxv", S_IRUSR, proc_auxv_operations),
631f9c18 2848 ONE("status", S_IRUGO, proc_pid_status),
35a35046 2849 ONE("personality", S_IRUSR, proc_pid_personality),
1c963eb1 2850 ONE("limits", S_IRUGO, proc_pid_limits),
43ae34cb 2851#ifdef CONFIG_SCHED_DEBUG
631f9c18 2852 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
5091faa4
MG
2853#endif
2854#ifdef CONFIG_SCHED_AUTOGROUP
2855 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
ebcb6734 2856#endif
4614a696 2857 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
ebcb6734 2858#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
09d93bd6 2859 ONE("syscall", S_IRUSR, proc_pid_syscall),
43ae34cb 2860#endif
c2c0bb44 2861 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
631f9c18
AD
2862 ONE("stat", S_IRUGO, proc_tgid_stat),
2863 ONE("statm", S_IRUGO, proc_pid_statm),
b7643757 2864 REG("maps", S_IRUGO, proc_pid_maps_operations),
28a6d671 2865#ifdef CONFIG_NUMA
b7643757 2866 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
28a6d671 2867#endif
631f9c18
AD
2868 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2869 LNK("cwd", proc_cwd_link),
2870 LNK("root", proc_root_link),
2871 LNK("exe", proc_exe_link),
2872 REG("mounts", S_IRUGO, proc_mounts_operations),
2873 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2874 REG("mountstats", S_IRUSR, proc_mountstats_operations),
1e883281 2875#ifdef CONFIG_PROC_PAGE_MONITOR
631f9c18 2876 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
b7643757 2877 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
32ed74a4 2878 REG("pagemap", S_IRUSR, proc_pagemap_operations),
28a6d671
EB
2879#endif
2880#ifdef CONFIG_SECURITY
631f9c18 2881 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
28a6d671
EB
2882#endif
2883#ifdef CONFIG_KALLSYMS
edfcd606 2884 ONE("wchan", S_IRUGO, proc_pid_wchan),
28a6d671 2885#endif
2ec220e2 2886#ifdef CONFIG_STACKTRACE
35a35046 2887 ONE("stack", S_IRUSR, proc_pid_stack),
28a6d671 2888#endif
5968cece 2889#ifdef CONFIG_SCHED_INFO
f6e826ca 2890 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
28a6d671 2891#endif
9745512c 2892#ifdef CONFIG_LATENCYTOP
631f9c18 2893 REG("latency", S_IRUGO, proc_lstats_operations),
9745512c 2894#endif
8793d854 2895#ifdef CONFIG_PROC_PID_CPUSET
52de4779 2896 ONE("cpuset", S_IRUGO, proc_cpuset_show),
a424316c
PM
2897#endif
2898#ifdef CONFIG_CGROUPS
006f4ac4 2899 ONE("cgroup", S_IRUGO, proc_cgroup_show),
28a6d671 2900#endif
6ba51e37 2901 ONE("oom_score", S_IRUGO, proc_oom_score),
fa0cbbf1 2902 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
a63d83f4 2903 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
28a6d671 2904#ifdef CONFIG_AUDITSYSCALL
631f9c18
AD
2905 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2906 REG("sessionid", S_IRUGO, proc_sessionid_operations),
28a6d671 2907#endif
f4f154fd 2908#ifdef CONFIG_FAULT_INJECTION
631f9c18 2909 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
f4f154fd 2910#endif
698ba7b5 2911#ifdef CONFIG_ELF_CORE
631f9c18 2912 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3cb4a0bb 2913#endif
aba76fdb 2914#ifdef CONFIG_TASK_IO_ACCOUNTING
19aadc98 2915 ONE("io", S_IRUSR, proc_tgid_io_accounting),
aba76fdb 2916#endif
f133ecca 2917#ifdef CONFIG_HARDWALL
d962c144 2918 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
f133ecca 2919#endif
22d917d8
EB
2920#ifdef CONFIG_USER_NS
2921 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2922 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
f76d207a 2923 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
9cc46516 2924 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
22d917d8 2925#endif
b18b6a9c 2926#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
48f6a7a5
PE
2927 REG("timers", S_IRUGO, proc_timers_operations),
2928#endif
5de23d43 2929 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
28a6d671 2930};
1da177e4 2931
f0c3b509 2932static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
1da177e4 2933{
f0c3b509
AV
2934 return proc_pident_readdir(file, ctx,
2935 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
2936}
2937
00977a59 2938static const struct file_operations proc_tgid_base_operations = {
1da177e4 2939 .read = generic_read_dir,
f50752ea
AV
2940 .iterate_shared = proc_tgid_base_readdir,
2941 .llseek = generic_file_llseek,
1da177e4
LT
2942};
2943
00cd8dd3
AV
2944static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2945{
7bcd6b0e
EB
2946 return proc_pident_lookup(dir, dentry,
2947 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
2948}
2949
c5ef1c42 2950static const struct inode_operations proc_tgid_base_inode_operations = {
28a6d671 2951 .lookup = proc_tgid_base_lookup,
99f89551 2952 .getattr = pid_getattr,
6d76fa58 2953 .setattr = proc_setattr,
0499680a 2954 .permission = proc_pid_permission,
1da177e4 2955};
1da177e4 2956
60347f67 2957static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
1da177e4 2958{
48e6484d 2959 struct dentry *dentry, *leader, *dir;
8578cea7 2960 char buf[PROC_NUMBUF];
48e6484d
EB
2961 struct qstr name;
2962
2963 name.name = buf;
60347f67 2964 name.len = snprintf(buf, sizeof(buf), "%d", pid);
4f522a24 2965 /* no ->d_hash() rejects on procfs */
60347f67 2966 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
48e6484d 2967 if (dentry) {
bbd51924 2968 d_invalidate(dentry);
48e6484d
EB
2969 dput(dentry);
2970 }
1da177e4 2971
c35a7f18
ON
2972 if (pid == tgid)
2973 return;
2974
48e6484d 2975 name.name = buf;
60347f67
PE
2976 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2977 leader = d_hash_and_lookup(mnt->mnt_root, &name);
48e6484d
EB
2978 if (!leader)
2979 goto out;
1da177e4 2980
48e6484d
EB
2981 name.name = "task";
2982 name.len = strlen(name.name);
2983 dir = d_hash_and_lookup(leader, &name);
2984 if (!dir)
2985 goto out_put_leader;
2986
2987 name.name = buf;
60347f67 2988 name.len = snprintf(buf, sizeof(buf), "%d", pid);
48e6484d
EB
2989 dentry = d_hash_and_lookup(dir, &name);
2990 if (dentry) {
bbd51924 2991 d_invalidate(dentry);
48e6484d 2992 dput(dentry);
1da177e4 2993 }
48e6484d
EB
2994
2995 dput(dir);
2996out_put_leader:
2997 dput(leader);
2998out:
2999 return;
1da177e4
LT
3000}
3001
0895e91d
RD
3002/**
3003 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3004 * @task: task that should be flushed.
3005 *
3006 * When flushing dentries from proc, one needs to flush them from global
60347f67 3007 * proc (proc_mnt) and from all the namespaces' procs this task was seen
0895e91d
RD
3008 * in. This call is supposed to do all of this job.
3009 *
3010 * Looks in the dcache for
3011 * /proc/@pid
3012 * /proc/@tgid/task/@pid
3013 * if either directory is present flushes it and all of it'ts children
3014 * from the dcache.
3015 *
3016 * It is safe and reasonable to cache /proc entries for a task until
3017 * that task exits. After that they just clog up the dcache with
3018 * useless entries, possibly causing useful dcache entries to be
3019 * flushed instead. This routine is proved to flush those useless
3020 * dcache entries at process exit time.
3021 *
3022 * NOTE: This routine is just an optimization so it does not guarantee
3023 * that no dcache entries will exist at process exit time it
3024 * just makes it very unlikely that any will persist.
60347f67
PE
3025 */
3026
3027void proc_flush_task(struct task_struct *task)
3028{
9fcc2d15 3029 int i;
9b4d1cbe 3030 struct pid *pid, *tgid;
130f77ec
PE
3031 struct upid *upid;
3032
130f77ec 3033 pid = task_pid(task);
9b4d1cbe 3034 tgid = task_tgid(task);
130f77ec 3035
9fcc2d15 3036 for (i = 0; i <= pid->level; i++) {
130f77ec
PE
3037 upid = &pid->numbers[i];
3038 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
9b4d1cbe 3039 tgid->numbers[i].nr);
130f77ec 3040 }
60347f67
PE
3041}
3042
c52a47ac
AV
3043static int proc_pid_instantiate(struct inode *dir,
3044 struct dentry * dentry,
3045 struct task_struct *task, const void *ptr)
444ceed8 3046{
444ceed8
EB
3047 struct inode *inode;
3048
db978da8 3049 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
444ceed8
EB
3050 if (!inode)
3051 goto out;
3052
444ceed8
EB
3053 inode->i_op = &proc_tgid_base_inode_operations;
3054 inode->i_fop = &proc_tgid_base_operations;
3055 inode->i_flags|=S_IMMUTABLE;
aed54175 3056
1270dd8d 3057 set_nlink(inode, nlink_tgid);
444ceed8 3058
fb045adb 3059 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
3060
3061 d_add(dentry, inode);
3062 /* Close the race of the process dying before we return the dentry */
0b728e19 3063 if (pid_revalidate(dentry, 0))
c52a47ac 3064 return 0;
444ceed8 3065out:
c52a47ac 3066 return -ENOENT;
444ceed8
EB
3067}
3068
00cd8dd3 3069struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1da177e4 3070{
335eb531 3071 int result = -ENOENT;
1da177e4 3072 struct task_struct *task;
1da177e4 3073 unsigned tgid;
b488893a 3074 struct pid_namespace *ns;
1da177e4 3075
dbcdb504 3076 tgid = name_to_int(&dentry->d_name);
1da177e4
LT
3077 if (tgid == ~0U)
3078 goto out;
3079
b488893a 3080 ns = dentry->d_sb->s_fs_info;
de758734 3081 rcu_read_lock();
b488893a 3082 task = find_task_by_pid_ns(tgid, ns);
1da177e4
LT
3083 if (task)
3084 get_task_struct(task);
de758734 3085 rcu_read_unlock();
1da177e4
LT
3086 if (!task)
3087 goto out;
3088
444ceed8 3089 result = proc_pid_instantiate(dir, dentry, task, NULL);
1da177e4 3090 put_task_struct(task);
1da177e4 3091out:
c52a47ac 3092 return ERR_PTR(result);
1da177e4
LT
3093}
3094
1da177e4 3095/*
0804ef4b 3096 * Find the first task with tgid >= tgid
0bc58a91 3097 *
1da177e4 3098 */
19fd4bb2
EB
3099struct tgid_iter {
3100 unsigned int tgid;
0804ef4b 3101 struct task_struct *task;
19fd4bb2
EB
3102};
3103static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3104{
0804ef4b 3105 struct pid *pid;
1da177e4 3106
19fd4bb2
EB
3107 if (iter.task)
3108 put_task_struct(iter.task);
454cc105 3109 rcu_read_lock();
0804ef4b 3110retry:
19fd4bb2
EB
3111 iter.task = NULL;
3112 pid = find_ge_pid(iter.tgid, ns);
0804ef4b 3113 if (pid) {
19fd4bb2
EB
3114 iter.tgid = pid_nr_ns(pid, ns);
3115 iter.task = pid_task(pid, PIDTYPE_PID);
0804ef4b
EB
3116 /* What we to know is if the pid we have find is the
3117 * pid of a thread_group_leader. Testing for task
3118 * being a thread_group_leader is the obvious thing
3119 * todo but there is a window when it fails, due to
3120 * the pid transfer logic in de_thread.
3121 *
3122 * So we perform the straight forward test of seeing
3123 * if the pid we have found is the pid of a thread
3124 * group leader, and don't worry if the task we have
3125 * found doesn't happen to be a thread group leader.
3126 * As we don't care in the case of readdir.
3127 */
19fd4bb2
EB
3128 if (!iter.task || !has_group_leader_pid(iter.task)) {
3129 iter.tgid += 1;
0804ef4b 3130 goto retry;
19fd4bb2
EB
3131 }
3132 get_task_struct(iter.task);
0bc58a91 3133 }
454cc105 3134 rcu_read_unlock();
19fd4bb2 3135 return iter;
1da177e4
LT
3136}
3137
0097875b 3138#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
0804ef4b 3139
1da177e4 3140/* for the /proc/ directory itself, after non-process stuff has been done */
f0c3b509 3141int proc_pid_readdir(struct file *file, struct dir_context *ctx)
1da177e4 3142{
19fd4bb2 3143 struct tgid_iter iter;
3aa3377f 3144 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
f0c3b509 3145 loff_t pos = ctx->pos;
1da177e4 3146
021ada7d 3147 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
f0c3b509 3148 return 0;
1da177e4 3149
0097875b 3150 if (pos == TGID_OFFSET - 2) {
2b0143b5 3151 struct inode *inode = d_inode(ns->proc_self);
db963164 3152 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
f0c3b509 3153 return 0;
0097875b
EB
3154 ctx->pos = pos = pos + 1;
3155 }
3156 if (pos == TGID_OFFSET - 1) {
2b0143b5 3157 struct inode *inode = d_inode(ns->proc_thread_self);
0097875b
EB
3158 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3159 return 0;
3160 ctx->pos = pos = pos + 1;
021ada7d 3161 }
0097875b 3162 iter.tgid = pos - TGID_OFFSET;
19fd4bb2 3163 iter.task = NULL;
19fd4bb2
EB
3164 for (iter = next_tgid(ns, iter);
3165 iter.task;
3166 iter.tgid += 1, iter = next_tgid(ns, iter)) {
f0c3b509
AV
3167 char name[PROC_NUMBUF];
3168 int len;
3ba4bcee
ED
3169
3170 cond_resched();
796f571b 3171 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
f0c3b509 3172 continue;
0499680a 3173
f0c3b509
AV
3174 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3175 ctx->pos = iter.tgid + TGID_OFFSET;
3176 if (!proc_fill_cache(file, ctx, name, len,
3177 proc_pid_instantiate, iter.task, NULL)) {
19fd4bb2 3178 put_task_struct(iter.task);
f0c3b509 3179 return 0;
1da177e4 3180 }
0bc58a91 3181 }
f0c3b509 3182 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
0bc58a91
EB
3183 return 0;
3184}
1da177e4 3185
1b3044e3
JD
3186/*
3187 * proc_tid_comm_permission is a special permission function exclusively
3188 * used for the node /proc/<pid>/task/<tid>/comm.
3189 * It bypasses generic permission checks in the case where a task of the same
3190 * task group attempts to access the node.
3191 * The rationale behind this is that glibc and bionic access this node for
3192 * cross thread naming (pthread_set/getname_np(!self)). However, if
3193 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3194 * which locks out the cross thread naming implementation.
3195 * This function makes sure that the node is always accessible for members of
3196 * same thread group.
3197 */
3198static int proc_tid_comm_permission(struct inode *inode, int mask)
3199{
3200 bool is_same_tgroup;
3201 struct task_struct *task;
3202
3203 task = get_proc_task(inode);
3204 if (!task)
3205 return -ESRCH;
3206 is_same_tgroup = same_thread_group(current, task);
3207 put_task_struct(task);
3208
3209 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3210 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3211 * read or written by the members of the corresponding
3212 * thread group.
3213 */
3214 return 0;
3215 }
3216
3217 return generic_permission(inode, mask);
3218}
3219
3220static const struct inode_operations proc_tid_comm_inode_operations = {
3221 .permission = proc_tid_comm_permission,
3222};
3223
28a6d671
EB
3224/*
3225 * Tasks
3226 */
c5141e6d 3227static const struct pid_entry tid_base_stuff[] = {
631f9c18 3228 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3835541d 3229 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
6b4e306a 3230 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
6ba8ed79
EB
3231#ifdef CONFIG_NET
3232 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3233#endif
631f9c18 3234 REG("environ", S_IRUSR, proc_environ_operations),
c5317167 3235 REG("auxv", S_IRUSR, proc_auxv_operations),
631f9c18 3236 ONE("status", S_IRUGO, proc_pid_status),
35a35046 3237 ONE("personality", S_IRUSR, proc_pid_personality),
1c963eb1 3238 ONE("limits", S_IRUGO, proc_pid_limits),
43ae34cb 3239#ifdef CONFIG_SCHED_DEBUG
631f9c18 3240 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
ebcb6734 3241#endif
1b3044e3
JD
3242 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3243 &proc_tid_comm_inode_operations,
3244 &proc_pid_set_comm_operations, {}),
ebcb6734 3245#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
09d93bd6 3246 ONE("syscall", S_IRUSR, proc_pid_syscall),
43ae34cb 3247#endif
c2c0bb44 3248 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
631f9c18
AD
3249 ONE("stat", S_IRUGO, proc_tid_stat),
3250 ONE("statm", S_IRUGO, proc_pid_statm),
b7643757 3251 REG("maps", S_IRUGO, proc_tid_maps_operations),
2e13ba54 3252#ifdef CONFIG_PROC_CHILDREN
81841161
CG
3253 REG("children", S_IRUGO, proc_tid_children_operations),
3254#endif
28a6d671 3255#ifdef CONFIG_NUMA
b7643757 3256 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
28a6d671 3257#endif
631f9c18
AD
3258 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3259 LNK("cwd", proc_cwd_link),
3260 LNK("root", proc_root_link),
3261 LNK("exe", proc_exe_link),
3262 REG("mounts", S_IRUGO, proc_mounts_operations),
3263 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
1e883281 3264#ifdef CONFIG_PROC_PAGE_MONITOR
631f9c18 3265 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
b7643757 3266 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
32ed74a4 3267 REG("pagemap", S_IRUSR, proc_pagemap_operations),
28a6d671
EB
3268#endif
3269#ifdef CONFIG_SECURITY
631f9c18 3270 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
28a6d671
EB
3271#endif
3272#ifdef CONFIG_KALLSYMS
edfcd606 3273 ONE("wchan", S_IRUGO, proc_pid_wchan),
28a6d671 3274#endif
2ec220e2 3275#ifdef CONFIG_STACKTRACE
35a35046 3276 ONE("stack", S_IRUSR, proc_pid_stack),
28a6d671 3277#endif
5968cece 3278#ifdef CONFIG_SCHED_INFO
f6e826ca 3279 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
28a6d671 3280#endif
9745512c 3281#ifdef CONFIG_LATENCYTOP
631f9c18 3282 REG("latency", S_IRUGO, proc_lstats_operations),
9745512c 3283#endif
8793d854 3284#ifdef CONFIG_PROC_PID_CPUSET
52de4779 3285 ONE("cpuset", S_IRUGO, proc_cpuset_show),
a424316c
PM
3286#endif
3287#ifdef CONFIG_CGROUPS
006f4ac4 3288 ONE("cgroup", S_IRUGO, proc_cgroup_show),
28a6d671 3289#endif
6ba51e37 3290 ONE("oom_score", S_IRUGO, proc_oom_score),
fa0cbbf1 3291 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
a63d83f4 3292 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
28a6d671 3293#ifdef CONFIG_AUDITSYSCALL
631f9c18 3294 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
26ec3c64 3295 REG("sessionid", S_IRUGO, proc_sessionid_operations),
28a6d671 3296#endif
f4f154fd 3297#ifdef CONFIG_FAULT_INJECTION
631f9c18 3298 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
f4f154fd 3299#endif
297c5d92 3300#ifdef CONFIG_TASK_IO_ACCOUNTING
19aadc98 3301 ONE("io", S_IRUSR, proc_tid_io_accounting),
297c5d92 3302#endif
f133ecca 3303#ifdef CONFIG_HARDWALL
d962c144 3304 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
f133ecca 3305#endif
22d917d8
EB
3306#ifdef CONFIG_USER_NS
3307 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3308 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
f76d207a 3309 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
9cc46516 3310 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
22d917d8 3311#endif
28a6d671
EB
3312};
3313
f0c3b509 3314static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
28a6d671 3315{
f0c3b509
AV
3316 return proc_pident_readdir(file, ctx,
3317 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
28a6d671
EB
3318}
3319
00cd8dd3
AV
3320static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3321{
7bcd6b0e
EB
3322 return proc_pident_lookup(dir, dentry,
3323 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
28a6d671
EB
3324}
3325
00977a59 3326static const struct file_operations proc_tid_base_operations = {
28a6d671 3327 .read = generic_read_dir,
f50752ea
AV
3328 .iterate_shared = proc_tid_base_readdir,
3329 .llseek = generic_file_llseek,
28a6d671
EB
3330};
3331
c5ef1c42 3332static const struct inode_operations proc_tid_base_inode_operations = {
28a6d671
EB
3333 .lookup = proc_tid_base_lookup,
3334 .getattr = pid_getattr,
3335 .setattr = proc_setattr,
3336};
3337
c52a47ac 3338static int proc_task_instantiate(struct inode *dir,
c5141e6d 3339 struct dentry *dentry, struct task_struct *task, const void *ptr)
444ceed8 3340{
444ceed8 3341 struct inode *inode;
db978da8 3342 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
444ceed8
EB
3343
3344 if (!inode)
3345 goto out;
444ceed8
EB
3346 inode->i_op = &proc_tid_base_inode_operations;
3347 inode->i_fop = &proc_tid_base_operations;
3348 inode->i_flags|=S_IMMUTABLE;
aed54175 3349
1270dd8d 3350 set_nlink(inode, nlink_tid);
444ceed8 3351
fb045adb 3352 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
3353
3354 d_add(dentry, inode);
3355 /* Close the race of the process dying before we return the dentry */
0b728e19 3356 if (pid_revalidate(dentry, 0))
c52a47ac 3357 return 0;
444ceed8 3358out:
c52a47ac 3359 return -ENOENT;
444ceed8
EB
3360}
3361
00cd8dd3 3362static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
28a6d671 3363{
c52a47ac 3364 int result = -ENOENT;
28a6d671
EB
3365 struct task_struct *task;
3366 struct task_struct *leader = get_proc_task(dir);
28a6d671 3367 unsigned tid;
b488893a 3368 struct pid_namespace *ns;
28a6d671
EB
3369
3370 if (!leader)
3371 goto out_no_task;
3372
dbcdb504 3373 tid = name_to_int(&dentry->d_name);
28a6d671
EB
3374 if (tid == ~0U)
3375 goto out;
3376
b488893a 3377 ns = dentry->d_sb->s_fs_info;
28a6d671 3378 rcu_read_lock();
b488893a 3379 task = find_task_by_pid_ns(tid, ns);
28a6d671
EB
3380 if (task)
3381 get_task_struct(task);
3382 rcu_read_unlock();
3383 if (!task)
3384 goto out;
bac0abd6 3385 if (!same_thread_group(leader, task))
28a6d671
EB
3386 goto out_drop_task;
3387
444ceed8 3388 result = proc_task_instantiate(dir, dentry, task, NULL);
28a6d671
EB
3389out_drop_task:
3390 put_task_struct(task);
3391out:
3392 put_task_struct(leader);
3393out_no_task:
c52a47ac 3394 return ERR_PTR(result);
28a6d671
EB
3395}
3396
0bc58a91
EB
3397/*
3398 * Find the first tid of a thread group to return to user space.
3399 *
3400 * Usually this is just the thread group leader, but if the users
3401 * buffer was too small or there was a seek into the middle of the
3402 * directory we have more work todo.
3403 *
3404 * In the case of a short read we start with find_task_by_pid.
3405 *
3406 * In the case of a seek we start with the leader and walk nr
3407 * threads past it.
3408 */
9f6e963f
ON
3409static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3410 struct pid_namespace *ns)
0bc58a91 3411{
d855a4b7 3412 struct task_struct *pos, *task;
9f6e963f
ON
3413 unsigned long nr = f_pos;
3414
3415 if (nr != f_pos) /* 32bit overflow? */
3416 return NULL;
1da177e4 3417
cc288738 3418 rcu_read_lock();
d855a4b7
ON
3419 task = pid_task(pid, PIDTYPE_PID);
3420 if (!task)
3421 goto fail;
3422
3423 /* Attempt to start with the tid of a thread */
9f6e963f 3424 if (tid && nr) {
b488893a 3425 pos = find_task_by_pid_ns(tid, ns);
d855a4b7 3426 if (pos && same_thread_group(pos, task))
a872ff0c 3427 goto found;
0bc58a91 3428 }
1da177e4 3429
0bc58a91 3430 /* If nr exceeds the number of threads there is nothing todo */
9f6e963f 3431 if (nr >= get_nr_threads(task))
c986c14a 3432 goto fail;
1da177e4 3433
a872ff0c
ON
3434 /* If we haven't found our starting place yet start
3435 * with the leader and walk nr threads forward.
0bc58a91 3436 */
d855a4b7 3437 pos = task = task->group_leader;
c986c14a 3438 do {
9f6e963f 3439 if (!nr--)
c986c14a 3440 goto found;
d855a4b7 3441 } while_each_thread(task, pos);
c986c14a
ON
3442fail:
3443 pos = NULL;
3444 goto out;
a872ff0c
ON
3445found:
3446 get_task_struct(pos);
3447out:
cc288738 3448 rcu_read_unlock();
0bc58a91
EB
3449 return pos;
3450}
3451
3452/*
3453 * Find the next thread in the thread list.
3454 * Return NULL if there is an error or no next thread.
3455 *
3456 * The reference to the input task_struct is released.
3457 */
3458static struct task_struct *next_tid(struct task_struct *start)
3459{
c1df7fb8 3460 struct task_struct *pos = NULL;
cc288738 3461 rcu_read_lock();
c1df7fb8 3462 if (pid_alive(start)) {
0bc58a91 3463 pos = next_thread(start);
c1df7fb8
ON
3464 if (thread_group_leader(pos))
3465 pos = NULL;
3466 else
3467 get_task_struct(pos);
3468 }
cc288738 3469 rcu_read_unlock();
0bc58a91
EB
3470 put_task_struct(start);
3471 return pos;
1da177e4
LT
3472}
3473
3474/* for the /proc/TGID/task/ directories */
f0c3b509 3475static int proc_task_readdir(struct file *file, struct dir_context *ctx)
1da177e4 3476{
d855a4b7
ON
3477 struct inode *inode = file_inode(file);
3478 struct task_struct *task;
b488893a 3479 struct pid_namespace *ns;
f0c3b509 3480 int tid;
1da177e4 3481
d855a4b7 3482 if (proc_inode_is_dead(inode))
f0c3b509 3483 return -ENOENT;
1da177e4 3484
f0c3b509 3485 if (!dir_emit_dots(file, ctx))
d855a4b7 3486 return 0;
1da177e4 3487
0bc58a91
EB
3488 /* f_version caches the tgid value that the last readdir call couldn't
3489 * return. lseek aka telldir automagically resets f_version to 0.
3490 */
3aa3377f 3491 ns = inode->i_sb->s_fs_info;
f0c3b509
AV
3492 tid = (int)file->f_version;
3493 file->f_version = 0;
d855a4b7 3494 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
0bc58a91 3495 task;
f0c3b509
AV
3496 task = next_tid(task), ctx->pos++) {
3497 char name[PROC_NUMBUF];
3498 int len;
b488893a 3499 tid = task_pid_nr_ns(task, ns);
f0c3b509
AV
3500 len = snprintf(name, sizeof(name), "%d", tid);
3501 if (!proc_fill_cache(file, ctx, name, len,
3502 proc_task_instantiate, task, NULL)) {
0bc58a91
EB
3503 /* returning this tgid failed, save it as the first
3504 * pid for the next readir call */
f0c3b509 3505 file->f_version = (u64)tid;
0bc58a91 3506 put_task_struct(task);
1da177e4 3507 break;
0bc58a91 3508 }
1da177e4 3509 }
d855a4b7 3510
f0c3b509 3511 return 0;
1da177e4 3512}
6e66b52b
EB
3513
3514static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3515{
2b0143b5 3516 struct inode *inode = d_inode(dentry);
99f89551 3517 struct task_struct *p = get_proc_task(inode);
6e66b52b
EB
3518 generic_fillattr(inode, stat);
3519
99f89551 3520 if (p) {
99f89551 3521 stat->nlink += get_nr_threads(p);
99f89551 3522 put_task_struct(p);
6e66b52b
EB
3523 }
3524
3525 return 0;
3526}
28a6d671 3527
c5ef1c42 3528static const struct inode_operations proc_task_inode_operations = {
28a6d671
EB
3529 .lookup = proc_task_lookup,
3530 .getattr = proc_task_getattr,
3531 .setattr = proc_setattr,
0499680a 3532 .permission = proc_pid_permission,
28a6d671
EB
3533};
3534
00977a59 3535static const struct file_operations proc_task_operations = {
28a6d671 3536 .read = generic_read_dir,
f50752ea
AV
3537 .iterate_shared = proc_task_readdir,
3538 .llseek = generic_file_llseek,
28a6d671 3539};
1270dd8d
AD
3540
3541void __init set_proc_pid_nlink(void)
3542{
3543 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3544 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3545}