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