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