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

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

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

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

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | 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.

osdn.jp