~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/proc/base.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php