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