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