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