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

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

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

~ [ 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