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