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