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