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