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