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