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