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