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