1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/proc/base.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 * 7 * proc base directory handling functions 8 * 9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 10 * Instead of using magical inumbers to determine the kind of object 11 * we allocate and fill in-core inodes upon lookup. They don't even 12 * go into icache. We cache the reference to task_struct upon lookup too. 13 * Eventually it should become a filesystem in its own. We don't use the 14 * rest of procfs anymore. 15 * 16 * 17 * Changelog: 18 * 17-Jan-2005 19 * Allan Bezerra 20 * Bruna Moreira <bruna.moreira@indt.org.br> 21 * Edjard Mota <edjard.mota@indt.org.br> 22 * Ilias Biris <ilias.biris@indt.org.br> 23 * Mauricio Lin <mauricio.lin@indt.org.br> 24 * 25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 26 * 27 * A new process specific entry (smaps) included in /proc. It shows the 28 * size of rss for each memory area. The maps entry lacks information 29 * about physical memory size (rss) for each mapped file, i.e., 30 * rss information for executables and library files. 31 * This additional information is useful for any tools that need to know 32 * about physical memory consumption for a process specific library. 33 * 34 * Changelog: 35 * 21-Feb-2005 36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 37 * Pud inclusion in the page table walking. 38 * 39 * ChangeLog: 40 * 10-Mar-2005 41 * 10LE Instituto Nokia de Tecnologia - INdT: 42 * A better way to walks through the page table as suggested by Hugh Dickins. 43 * 44 * Simo Piiroinen <simo.piiroinen@nokia.com>: 45 * Smaps information related to shared, private, clean and dirty pages. 46 * 47 * Paul Mundt <paul.mundt@nokia.com>: 48 * Overall revision about smaps. 49 */ 50 51 #include <linux/uaccess.h> 52 53 #include <linux/errno.h> 54 #include <linux/time.h> 55 #include <linux/proc_fs.h> 56 #include <linux/stat.h> 57 #include <linux/task_io_accounting_ops.h> 58 #include <linux/init.h> 59 #include <linux/capability.h> 60 #include <linux/file.h> 61 #include <linux/fdtable.h> 62 #include <linux/generic-radix-tree.h> 63 #include <linux/string.h> 64 #include <linux/seq_file.h> 65 #include <linux/namei.h> 66 #include <linux/mnt_namespace.h> 67 #include <linux/mm.h> 68 #include <linux/swap.h> 69 #include <linux/rcupdate.h> 70 #include <linux/kallsyms.h> 71 #include <linux/stacktrace.h> 72 #include <linux/resource.h> 73 #include <linux/module.h> 74 #include <linux/mount.h> 75 #include <linux/security.h> 76 #include <linux/ptrace.h> 77 #include <linux/tracehook.h> 78 #include <linux/printk.h> 79 #include <linux/cache.h> 80 #include <linux/cgroup.h> 81 #include <linux/cpuset.h> 82 #include <linux/audit.h> 83 #include <linux/poll.h> 84 #include <linux/nsproxy.h> 85 #include <linux/oom.h> 86 #include <linux/elf.h> 87 #include <linux/pid_namespace.h> 88 #include <linux/user_namespace.h> 89 #include <linux/fs_struct.h> 90 #include <linux/slab.h> 91 #include <linux/sched/autogroup.h> 92 #include <linux/sched/mm.h> 93 #include <linux/sched/coredump.h> 94 #include <linux/sched/debug.h> 95 #include <linux/sched/stat.h> 96 #include <linux/posix-timers.h> 97 #include <trace/events/oom.h> 98 #include "internal.h" 99 #include "fd.h" 100 101 #include "../../lib/kstrtox.h" 102 103 /* NOTE: 104 * Implementing inode permission operations in /proc is almost 105 * certainly an error. Permission checks need to happen during 106 * each system call not at open time. The reason is that most of 107 * what we wish to check for permissions in /proc varies at runtime. 108 * 109 * The classic example of a problem is opening file descriptors 110 * in /proc for a task before it execs a suid executable. 111 */ 112 113 static u8 nlink_tid __ro_after_init; 114 static u8 nlink_tgid __ro_after_init; 115 116 struct pid_entry { 117 const char *name; 118 unsigned int len; 119 umode_t mode; 120 const struct inode_operations *iop; 121 const struct file_operations *fop; 122 union proc_op op; 123 }; 124 125 #define NOD(NAME, MODE, IOP, FOP, OP) { \ 126 .name = (NAME), \ 127 .len = sizeof(NAME) - 1, \ 128 .mode = MODE, \ 129 .iop = IOP, \ 130 .fop = FOP, \ 131 .op = OP, \ 132 } 133 134 #define DIR(NAME, MODE, iops, fops) \ 135 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 136 #define LNK(NAME, get_link) \ 137 NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 138 &proc_pid_link_inode_operations, NULL, \ 139 { .proc_get_link = get_link } ) 140 #define REG(NAME, MODE, fops) \ 141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 142 #define ONE(NAME, MODE, show) \ 143 NOD(NAME, (S_IFREG|(MODE)), \ 144 NULL, &proc_single_file_operations, \ 145 { .proc_show = show } ) 146 #define ATTR(LSM, NAME, MODE) \ 147 NOD(NAME, (S_IFREG|(MODE)), \ 148 NULL, &proc_pid_attr_operations, \ 149 { .lsm = LSM }) 150 151 /* 152 * Count the number of hardlinks for the pid_entry table, excluding the . 153 * and .. links. 154 */ 155 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries, 156 unsigned int n) 157 { 158 unsigned int i; 159 unsigned int count; 160 161 count = 2; 162 for (i = 0; i < n; ++i) { 163 if (S_ISDIR(entries[i].mode)) 164 ++count; 165 } 166 167 return count; 168 } 169 170 static int get_task_root(struct task_struct *task, struct path *root) 171 { 172 int result = -ENOENT; 173 174 task_lock(task); 175 if (task->fs) { 176 get_fs_root(task->fs, root); 177 result = 0; 178 } 179 task_unlock(task); 180 return result; 181 } 182 183 static int proc_cwd_link(struct dentry *dentry, struct path *path) 184 { 185 struct task_struct *task = get_proc_task(d_inode(dentry)); 186 int result = -ENOENT; 187 188 if (task) { 189 task_lock(task); 190 if (task->fs) { 191 get_fs_pwd(task->fs, path); 192 result = 0; 193 } 194 task_unlock(task); 195 put_task_struct(task); 196 } 197 return result; 198 } 199 200 static int proc_root_link(struct dentry *dentry, struct path *path) 201 { 202 struct task_struct *task = get_proc_task(d_inode(dentry)); 203 int result = -ENOENT; 204 205 if (task) { 206 result = get_task_root(task, path); 207 put_task_struct(task); 208 } 209 return result; 210 } 211 212 /* 213 * If the user used setproctitle(), we just get the string from 214 * user space at arg_start, and limit it to a maximum of one page. 215 */ 216 static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf, 217 size_t count, unsigned long pos, 218 unsigned long arg_start) 219 { 220 char *page; 221 int ret, got; 222 223 if (pos >= PAGE_SIZE) 224 return 0; 225 226 page = (char *)__get_free_page(GFP_KERNEL); 227 if (!page) 228 return -ENOMEM; 229 230 ret = 0; 231 got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON); 232 if (got > 0) { 233 int len = strnlen(page, got); 234 235 /* Include the NUL character if it was found */ 236 if (len < got) 237 len++; 238 239 if (len > pos) { 240 len -= pos; 241 if (len > count) 242 len = count; 243 len -= copy_to_user(buf, page+pos, len); 244 if (!len) 245 len = -EFAULT; 246 ret = len; 247 } 248 } 249 free_page((unsigned long)page); 250 return ret; 251 } 252 253 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf, 254 size_t count, loff_t *ppos) 255 { 256 unsigned long arg_start, arg_end, env_start, env_end; 257 unsigned long pos, len; 258 char *page, c; 259 260 /* Check if process spawned far enough to have cmdline. */ 261 if (!mm->env_end) 262 return 0; 263 264 spin_lock(&mm->arg_lock); 265 arg_start = mm->arg_start; 266 arg_end = mm->arg_end; 267 env_start = mm->env_start; 268 env_end = mm->env_end; 269 spin_unlock(&mm->arg_lock); 270 271 if (arg_start >= arg_end) 272 return 0; 273 274 /* 275 * We allow setproctitle() to overwrite the argument 276 * strings, and overflow past the original end. But 277 * only when it overflows into the environment area. 278 */ 279 if (env_start != arg_end || env_end < env_start) 280 env_start = env_end = arg_end; 281 len = env_end - arg_start; 282 283 /* We're not going to care if "*ppos" has high bits set */ 284 pos = *ppos; 285 if (pos >= len) 286 return 0; 287 if (count > len - pos) 288 count = len - pos; 289 if (!count) 290 return 0; 291 292 /* 293 * Magical special case: if the argv[] end byte is not 294 * zero, the user has overwritten it with setproctitle(3). 295 * 296 * Possible future enhancement: do this only once when 297 * pos is 0, and set a flag in the 'struct file'. 298 */ 299 if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c) 300 return get_mm_proctitle(mm, buf, count, pos, arg_start); 301 302 /* 303 * For the non-setproctitle() case we limit things strictly 304 * to the [arg_start, arg_end[ range. 305 */ 306 pos += arg_start; 307 if (pos < arg_start || pos >= arg_end) 308 return 0; 309 if (count > arg_end - pos) 310 count = arg_end - pos; 311 312 page = (char *)__get_free_page(GFP_KERNEL); 313 if (!page) 314 return -ENOMEM; 315 316 len = 0; 317 while (count) { 318 int got; 319 size_t size = min_t(size_t, PAGE_SIZE, count); 320 321 got = access_remote_vm(mm, pos, page, size, FOLL_ANON); 322 if (got <= 0) 323 break; 324 got -= copy_to_user(buf, page, got); 325 if (unlikely(!got)) { 326 if (!len) 327 len = -EFAULT; 328 break; 329 } 330 pos += got; 331 buf += got; 332 len += got; 333 count -= got; 334 } 335 336 free_page((unsigned long)page); 337 return len; 338 } 339 340 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf, 341 size_t count, loff_t *pos) 342 { 343 struct mm_struct *mm; 344 ssize_t ret; 345 346 mm = get_task_mm(tsk); 347 if (!mm) 348 return 0; 349 350 ret = get_mm_cmdline(mm, buf, count, pos); 351 mmput(mm); 352 return ret; 353 } 354 355 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf, 356 size_t count, loff_t *pos) 357 { 358 struct task_struct *tsk; 359 ssize_t ret; 360 361 BUG_ON(*pos < 0); 362 363 tsk = get_proc_task(file_inode(file)); 364 if (!tsk) 365 return -ESRCH; 366 ret = get_task_cmdline(tsk, buf, count, pos); 367 put_task_struct(tsk); 368 if (ret > 0) 369 *pos += ret; 370 return ret; 371 } 372 373 static const struct file_operations proc_pid_cmdline_ops = { 374 .read = proc_pid_cmdline_read, 375 .llseek = generic_file_llseek, 376 }; 377 378 #ifdef CONFIG_KALLSYMS 379 /* 380 * Provides a wchan file via kallsyms in a proper one-value-per-file format. 381 * Returns the resolved symbol. If that fails, simply return the address. 382 */ 383 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns, 384 struct pid *pid, struct task_struct *task) 385 { 386 unsigned long wchan; 387 char symname[KSYM_NAME_LEN]; 388 389 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) 390 goto print0; 391 392 wchan = get_wchan(task); 393 if (wchan && !lookup_symbol_name(wchan, symname)) { 394 seq_puts(m, symname); 395 return 0; 396 } 397 398 print0: 399 seq_putc(m, ''); 400 return 0; 401 } 402 #endif /* CONFIG_KALLSYMS */ 403 404 static int lock_trace(struct task_struct *task) 405 { 406 int err = mutex_lock_killable(&task->signal->cred_guard_mutex); 407 if (err) 408 return err; 409 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) { 410 mutex_unlock(&task->signal->cred_guard_mutex); 411 return -EPERM; 412 } 413 return 0; 414 } 415 416 static void unlock_trace(struct task_struct *task) 417 { 418 mutex_unlock(&task->signal->cred_guard_mutex); 419 } 420 421 #ifdef CONFIG_STACKTRACE 422 423 #define MAX_STACK_TRACE_DEPTH 64 424 425 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 426 struct pid *pid, struct task_struct *task) 427 { 428 unsigned long *entries; 429 int err; 430 431 /* 432 * The ability to racily run the kernel stack unwinder on a running task 433 * and then observe the unwinder output is scary; while it is useful for 434 * debugging kernel issues, it can also allow an attacker to leak kernel 435 * stack contents. 436 * Doing this in a manner that is at least safe from races would require 437 * some work to ensure that the remote task can not be scheduled; and 438 * even then, this would still expose the unwinder as local attack 439 * surface. 440 * Therefore, this interface is restricted to root. 441 */ 442 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) 443 return -EACCES; 444 445 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries), 446 GFP_KERNEL); 447 if (!entries) 448 return -ENOMEM; 449 450 err = lock_trace(task); 451 if (!err) { 452 unsigned int i, nr_entries; 453 454 nr_entries = stack_trace_save_tsk(task, entries, 455 MAX_STACK_TRACE_DEPTH, 0); 456 457 for (i = 0; i < nr_entries; i++) { 458 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]); 459 } 460 461 unlock_trace(task); 462 } 463 kfree(entries); 464 465 return err; 466 } 467 #endif 468 469 #ifdef CONFIG_SCHED_INFO 470 /* 471 * Provides /proc/PID/schedstat 472 */ 473 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, 474 struct pid *pid, struct task_struct *task) 475 { 476 if (unlikely(!sched_info_on())) 477 seq_puts(m, "0 0 0\n"); 478 else 479 seq_printf(m, "%llu %llu %lu\n", 480 (unsigned long long)task->se.sum_exec_runtime, 481 (unsigned long long)task->sched_info.run_delay, 482 task->sched_info.pcount); 483 484 return 0; 485 } 486 #endif 487 488 #ifdef CONFIG_LATENCYTOP 489 static int lstats_show_proc(struct seq_file *m, void *v) 490 { 491 int i; 492 struct inode *inode = m->private; 493 struct task_struct *task = get_proc_task(inode); 494 495 if (!task) 496 return -ESRCH; 497 seq_puts(m, "Latency Top version : v0.1\n"); 498 for (i = 0; i < LT_SAVECOUNT; i++) { 499 struct latency_record *lr = &task->latency_record[i]; 500 if (lr->backtrace[0]) { 501 int q; 502 seq_printf(m, "%i %li %li", 503 lr->count, lr->time, lr->max); 504 for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 505 unsigned long bt = lr->backtrace[q]; 506 507 if (!bt) 508 break; 509 seq_printf(m, " %ps", (void *)bt); 510 } 511 seq_putc(m, '\n'); 512 } 513 514 } 515 put_task_struct(task); 516 return 0; 517 } 518 519 static int lstats_open(struct inode *inode, struct file *file) 520 { 521 return single_open(file, lstats_show_proc, inode); 522 } 523 524 static ssize_t lstats_write(struct file *file, const char __user *buf, 525 size_t count, loff_t *offs) 526 { 527 struct task_struct *task = get_proc_task(file_inode(file)); 528 529 if (!task) 530 return -ESRCH; 531 clear_tsk_latency_tracing(task); 532 put_task_struct(task); 533 534 return count; 535 } 536 537 static const struct file_operations proc_lstats_operations = { 538 .open = lstats_open, 539 .read = seq_read, 540 .write = lstats_write, 541 .llseek = seq_lseek, 542 .release = single_release, 543 }; 544 545 #endif 546 547 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns, 548 struct pid *pid, struct task_struct *task) 549 { 550 unsigned long totalpages = totalram_pages() + total_swap_pages; 551 unsigned long points = 0; 552 553 points = oom_badness(task, NULL, NULL, totalpages) * 554 1000 / totalpages; 555 seq_printf(m, "%lu\n", points); 556 557 return 0; 558 } 559 560 struct limit_names { 561 const char *name; 562 const char *unit; 563 }; 564 565 static const struct limit_names lnames[RLIM_NLIMITS] = { 566 [RLIMIT_CPU] = {"Max cpu time", "seconds"}, 567 [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 568 [RLIMIT_DATA] = {"Max data size", "bytes"}, 569 [RLIMIT_STACK] = {"Max stack size", "bytes"}, 570 [RLIMIT_CORE] = {"Max core file size", "bytes"}, 571 [RLIMIT_RSS] = {"Max resident set", "bytes"}, 572 [RLIMIT_NPROC] = {"Max processes", "processes"}, 573 [RLIMIT_NOFILE] = {"Max open files", "files"}, 574 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 575 [RLIMIT_AS] = {"Max address space", "bytes"}, 576 [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 577 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 578 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 579 [RLIMIT_NICE] = {"Max nice priority", NULL}, 580 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 581 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 582 }; 583 584 /* Display limits for a process */ 585 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns, 586 struct pid *pid, struct task_struct *task) 587 { 588 unsigned int i; 589 unsigned long flags; 590 591 struct rlimit rlim[RLIM_NLIMITS]; 592 593 if (!lock_task_sighand(task, &flags)) 594 return 0; 595 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 596 unlock_task_sighand(task, &flags); 597 598 /* 599 * print the file header 600 */ 601 seq_puts(m, "Limit " 602 "Soft Limit " 603 "Hard Limit " 604 "Units \n"); 605 606 for (i = 0; i < RLIM_NLIMITS; i++) { 607 if (rlim[i].rlim_cur == RLIM_INFINITY) 608 seq_printf(m, "%-25s %-20s ", 609 lnames[i].name, "unlimited"); 610 else 611 seq_printf(m, "%-25s %-20lu ", 612 lnames[i].name, rlim[i].rlim_cur); 613 614 if (rlim[i].rlim_max == RLIM_INFINITY) 615 seq_printf(m, "%-20s ", "unlimited"); 616 else 617 seq_printf(m, "%-20lu ", rlim[i].rlim_max); 618 619 if (lnames[i].unit) 620 seq_printf(m, "%-10s\n", lnames[i].unit); 621 else 622 seq_putc(m, '\n'); 623 } 624 625 return 0; 626 } 627 628 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 629 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns, 630 struct pid *pid, struct task_struct *task) 631 { 632 struct syscall_info info; 633 u64 *args = &info.data.args[0]; 634 int res; 635 636 res = lock_trace(task); 637 if (res) 638 return res; 639 640 if (task_current_syscall(task, &info)) 641 seq_puts(m, "running\n"); 642 else if (info.data.nr < 0) 643 seq_printf(m, "%d 0x%llx 0x%llx\n", 644 info.data.nr, info.sp, info.data.instruction_pointer); 645 else 646 seq_printf(m, 647 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n", 648 info.data.nr, 649 args[0], args[1], args[2], args[3], args[4], args[5], 650 info.sp, info.data.instruction_pointer); 651 unlock_trace(task); 652 653 return 0; 654 } 655 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 656 657 /************************************************************************/ 658 /* Here the fs part begins */ 659 /************************************************************************/ 660 661 /* permission checks */ 662 static int proc_fd_access_allowed(struct inode *inode) 663 { 664 struct task_struct *task; 665 int allowed = 0; 666 /* Allow access to a task's file descriptors if it is us or we 667 * may use ptrace attach to the process and find out that 668 * information. 669 */ 670 task = get_proc_task(inode); 671 if (task) { 672 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); 673 put_task_struct(task); 674 } 675 return allowed; 676 } 677 678 int proc_setattr(struct dentry *dentry, struct iattr *attr) 679 { 680 int error; 681 struct inode *inode = d_inode(dentry); 682 683 if (attr->ia_valid & ATTR_MODE) 684 return -EPERM; 685 686 error = setattr_prepare(dentry, attr); 687 if (error) 688 return error; 689 690 setattr_copy(inode, attr); 691 mark_inode_dirty(inode); 692 return 0; 693 } 694 695 /* 696 * May current process learn task's sched/cmdline info (for hide_pid_min=1) 697 * or euid/egid (for hide_pid_min=2)? 698 */ 699 static bool has_pid_permissions(struct pid_namespace *pid, 700 struct task_struct *task, 701 int hide_pid_min) 702 { 703 if (pid->hide_pid < hide_pid_min) 704 return true; 705 if (in_group_p(pid->pid_gid)) 706 return true; 707 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); 708 } 709 710 711 static int proc_pid_permission(struct inode *inode, int mask) 712 { 713 struct pid_namespace *pid = proc_pid_ns(inode); 714 struct task_struct *task; 715 bool has_perms; 716 717 task = get_proc_task(inode); 718 if (!task) 719 return -ESRCH; 720 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS); 721 put_task_struct(task); 722 723 if (!has_perms) { 724 if (pid->hide_pid == HIDEPID_INVISIBLE) { 725 /* 726 * Let's make getdents(), stat(), and open() 727 * consistent with each other. If a process 728 * may not stat() a file, it shouldn't be seen 729 * in procfs at all. 730 */ 731 return -ENOENT; 732 } 733 734 return -EPERM; 735 } 736 return generic_permission(inode, mask); 737 } 738 739 740 741 static const struct inode_operations proc_def_inode_operations = { 742 .setattr = proc_setattr, 743 }; 744 745 static int proc_single_show(struct seq_file *m, void *v) 746 { 747 struct inode *inode = m->private; 748 struct pid_namespace *ns = proc_pid_ns(inode); 749 struct pid *pid = proc_pid(inode); 750 struct task_struct *task; 751 int ret; 752 753 task = get_pid_task(pid, PIDTYPE_PID); 754 if (!task) 755 return -ESRCH; 756 757 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 758 759 put_task_struct(task); 760 return ret; 761 } 762 763 static int proc_single_open(struct inode *inode, struct file *filp) 764 { 765 return single_open(filp, proc_single_show, inode); 766 } 767 768 static const struct file_operations proc_single_file_operations = { 769 .open = proc_single_open, 770 .read = seq_read, 771 .llseek = seq_lseek, 772 .release = single_release, 773 }; 774 775 776 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode) 777 { 778 struct task_struct *task = get_proc_task(inode); 779 struct mm_struct *mm = ERR_PTR(-ESRCH); 780 781 if (task) { 782 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS); 783 put_task_struct(task); 784 785 if (!IS_ERR_OR_NULL(mm)) { 786 /* ensure this mm_struct can't be freed */ 787 mmgrab(mm); 788 /* but do not pin its memory */ 789 mmput(mm); 790 } 791 } 792 793 return mm; 794 } 795 796 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) 797 { 798 struct mm_struct *mm = proc_mem_open(inode, mode); 799 800 if (IS_ERR(mm)) 801 return PTR_ERR(mm); 802 803 file->private_data = mm; 804 return 0; 805 } 806 807 static int mem_open(struct inode *inode, struct file *file) 808 { 809 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); 810 811 /* OK to pass negative loff_t, we can catch out-of-range */ 812 file->f_mode |= FMODE_UNSIGNED_OFFSET; 813 814 return ret; 815 } 816 817 static ssize_t mem_rw(struct file *file, char __user *buf, 818 size_t count, loff_t *ppos, int write) 819 { 820 struct mm_struct *mm = file->private_data; 821 unsigned long addr = *ppos; 822 ssize_t copied; 823 char *page; 824 unsigned int flags; 825 826 if (!mm) 827 return 0; 828 829 page = (char *)__get_free_page(GFP_KERNEL); 830 if (!page) 831 return -ENOMEM; 832 833 copied = 0; 834 if (!mmget_not_zero(mm)) 835 goto free; 836 837 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0); 838 839 while (count > 0) { 840 int this_len = min_t(int, count, PAGE_SIZE); 841 842 if (write && copy_from_user(page, buf, this_len)) { 843 copied = -EFAULT; 844 break; 845 } 846 847 this_len = access_remote_vm(mm, addr, page, this_len, flags); 848 if (!this_len) { 849 if (!copied) 850 copied = -EIO; 851 break; 852 } 853 854 if (!write && copy_to_user(buf, page, this_len)) { 855 copied = -EFAULT; 856 break; 857 } 858 859 buf += this_len; 860 addr += this_len; 861 copied += this_len; 862 count -= this_len; 863 } 864 *ppos = addr; 865 866 mmput(mm); 867 free: 868 free_page((unsigned long) page); 869 return copied; 870 } 871 872 static ssize_t mem_read(struct file *file, char __user *buf, 873 size_t count, loff_t *ppos) 874 { 875 return mem_rw(file, buf, count, ppos, 0); 876 } 877 878 static ssize_t mem_write(struct file *file, const char __user *buf, 879 size_t count, loff_t *ppos) 880 { 881 return mem_rw(file, (char __user*)buf, count, ppos, 1); 882 } 883 884 loff_t mem_lseek(struct file *file, loff_t offset, int orig) 885 { 886 switch (orig) { 887 case 0: 888 file->f_pos = offset; 889 break; 890 case 1: 891 file->f_pos += offset; 892 break; 893 default: 894 return -EINVAL; 895 } 896 force_successful_syscall_return(); 897 return file->f_pos; 898 } 899 900 static int mem_release(struct inode *inode, struct file *file) 901 { 902 struct mm_struct *mm = file->private_data; 903 if (mm) 904 mmdrop(mm); 905 return 0; 906 } 907 908 static const struct file_operations proc_mem_operations = { 909 .llseek = mem_lseek, 910 .read = mem_read, 911 .write = mem_write, 912 .open = mem_open, 913 .release = mem_release, 914 }; 915 916 static int environ_open(struct inode *inode, struct file *file) 917 { 918 return __mem_open(inode, file, PTRACE_MODE_READ); 919 } 920 921 static ssize_t environ_read(struct file *file, char __user *buf, 922 size_t count, loff_t *ppos) 923 { 924 char *page; 925 unsigned long src = *ppos; 926 int ret = 0; 927 struct mm_struct *mm = file->private_data; 928 unsigned long env_start, env_end; 929 930 /* Ensure the process spawned far enough to have an environment. */ 931 if (!mm || !mm->env_end) 932 return 0; 933 934 page = (char *)__get_free_page(GFP_KERNEL); 935 if (!page) 936 return -ENOMEM; 937 938 ret = 0; 939 if (!mmget_not_zero(mm)) 940 goto free; 941 942 spin_lock(&mm->arg_lock); 943 env_start = mm->env_start; 944 env_end = mm->env_end; 945 spin_unlock(&mm->arg_lock); 946 947 while (count > 0) { 948 size_t this_len, max_len; 949 int retval; 950 951 if (src >= (env_end - env_start)) 952 break; 953 954 this_len = env_end - (env_start + src); 955 956 max_len = min_t(size_t, PAGE_SIZE, count); 957 this_len = min(max_len, this_len); 958 959 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON); 960 961 if (retval <= 0) { 962 ret = retval; 963 break; 964 } 965 966 if (copy_to_user(buf, page, retval)) { 967 ret = -EFAULT; 968 break; 969 } 970 971 ret += retval; 972 src += retval; 973 buf += retval; 974 count -= retval; 975 } 976 *ppos = src; 977 mmput(mm); 978 979 free: 980 free_page((unsigned long) page); 981 return ret; 982 } 983 984 static const struct file_operations proc_environ_operations = { 985 .open = environ_open, 986 .read = environ_read, 987 .llseek = generic_file_llseek, 988 .release = mem_release, 989 }; 990 991 static int auxv_open(struct inode *inode, struct file *file) 992 { 993 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS); 994 } 995 996 static ssize_t auxv_read(struct file *file, char __user *buf, 997 size_t count, loff_t *ppos) 998 { 999 struct mm_struct *mm = file->private_data; 1000 unsigned int nwords = 0; 1001 1002 if (!mm) 1003 return 0; 1004 do { 1005 nwords += 2; 1006 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 1007 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv, 1008 nwords * sizeof(mm->saved_auxv[0])); 1009 } 1010 1011 static const struct file_operations proc_auxv_operations = { 1012 .open = auxv_open, 1013 .read = auxv_read, 1014 .llseek = generic_file_llseek, 1015 .release = mem_release, 1016 }; 1017 1018 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count, 1019 loff_t *ppos) 1020 { 1021 struct task_struct *task = get_proc_task(file_inode(file)); 1022 char buffer[PROC_NUMBUF]; 1023 int oom_adj = OOM_ADJUST_MIN; 1024 size_t len; 1025 1026 if (!task) 1027 return -ESRCH; 1028 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX) 1029 oom_adj = OOM_ADJUST_MAX; 1030 else 1031 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) / 1032 OOM_SCORE_ADJ_MAX; 1033 put_task_struct(task); 1034 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj); 1035 return simple_read_from_buffer(buf, count, ppos, buffer, len); 1036 } 1037 1038 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) 1039 { 1040 static DEFINE_MUTEX(oom_adj_mutex); 1041 struct mm_struct *mm = NULL; 1042 struct task_struct *task; 1043 int err = 0; 1044 1045 task = get_proc_task(file_inode(file)); 1046 if (!task) 1047 return -ESRCH; 1048 1049 mutex_lock(&oom_adj_mutex); 1050 if (legacy) { 1051 if (oom_adj < task->signal->oom_score_adj && 1052 !capable(CAP_SYS_RESOURCE)) { 1053 err = -EACCES; 1054 goto err_unlock; 1055 } 1056 /* 1057 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use 1058 * /proc/pid/oom_score_adj instead. 1059 */ 1060 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", 1061 current->comm, task_pid_nr(current), task_pid_nr(task), 1062 task_pid_nr(task)); 1063 } else { 1064 if ((short)oom_adj < task->signal->oom_score_adj_min && 1065 !capable(CAP_SYS_RESOURCE)) { 1066 err = -EACCES; 1067 goto err_unlock; 1068 } 1069 } 1070 1071 /* 1072 * Make sure we will check other processes sharing the mm if this is 1073 * not vfrok which wants its own oom_score_adj. 1074 * pin the mm so it doesn't go away and get reused after task_unlock 1075 */ 1076 if (!task->vfork_done) { 1077 struct task_struct *p = find_lock_task_mm(task); 1078 1079 if (p) { 1080 if (atomic_read(&p->mm->mm_users) > 1) { 1081 mm = p->mm; 1082 mmgrab(mm); 1083 } 1084 task_unlock(p); 1085 } 1086 } 1087 1088 task->signal->oom_score_adj = oom_adj; 1089 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE)) 1090 task->signal->oom_score_adj_min = (short)oom_adj; 1091 trace_oom_score_adj_update(task); 1092 1093 if (mm) { 1094 struct task_struct *p; 1095 1096 rcu_read_lock(); 1097 for_each_process(p) { 1098 if (same_thread_group(task, p)) 1099 continue; 1100 1101 /* do not touch kernel threads or the global init */ 1102 if (p->flags & PF_KTHREAD || is_global_init(p)) 1103 continue; 1104 1105 task_lock(p); 1106 if (!p->vfork_done && process_shares_mm(p, mm)) { 1107 p->signal->oom_score_adj = oom_adj; 1108 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE)) 1109 p->signal->oom_score_adj_min = (short)oom_adj; 1110 } 1111 task_unlock(p); 1112 } 1113 rcu_read_unlock(); 1114 mmdrop(mm); 1115 } 1116 err_unlock: 1117 mutex_unlock(&oom_adj_mutex); 1118 put_task_struct(task); 1119 return err; 1120 } 1121 1122 /* 1123 * /proc/pid/oom_adj exists solely for backwards compatibility with previous 1124 * kernels. The effective policy is defined by oom_score_adj, which has a 1125 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly. 1126 * Values written to oom_adj are simply mapped linearly to oom_score_adj. 1127 * Processes that become oom disabled via oom_adj will still be oom disabled 1128 * with this implementation. 1129 * 1130 * oom_adj cannot be removed since existing userspace binaries use it. 1131 */ 1132 static ssize_t oom_adj_write(struct file *file, const char __user *buf, 1133 size_t count, loff_t *ppos) 1134 { 1135 char buffer[PROC_NUMBUF]; 1136 int oom_adj; 1137 int err; 1138 1139 memset(buffer, 0, sizeof(buffer)); 1140 if (count > sizeof(buffer) - 1) 1141 count = sizeof(buffer) - 1; 1142 if (copy_from_user(buffer, buf, count)) { 1143 err = -EFAULT; 1144 goto out; 1145 } 1146 1147 err = kstrtoint(strstrip(buffer), 0, &oom_adj); 1148 if (err) 1149 goto out; 1150 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) && 1151 oom_adj != OOM_DISABLE) { 1152 err = -EINVAL; 1153 goto out; 1154 } 1155 1156 /* 1157 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum 1158 * value is always attainable. 1159 */ 1160 if (oom_adj == OOM_ADJUST_MAX) 1161 oom_adj = OOM_SCORE_ADJ_MAX; 1162 else 1163 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; 1164 1165 err = __set_oom_adj(file, oom_adj, true); 1166 out: 1167 return err < 0 ? err : count; 1168 } 1169 1170 static const struct file_operations proc_oom_adj_operations = { 1171 .read = oom_adj_read, 1172 .write = oom_adj_write, 1173 .llseek = generic_file_llseek, 1174 }; 1175 1176 static ssize_t oom_score_adj_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 short oom_score_adj = OOM_SCORE_ADJ_MIN; 1182 size_t len; 1183 1184 if (!task) 1185 return -ESRCH; 1186 oom_score_adj = task->signal->oom_score_adj; 1187 put_task_struct(task); 1188 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj); 1189 return simple_read_from_buffer(buf, count, ppos, buffer, len); 1190 } 1191 1192 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, 1193 size_t count, loff_t *ppos) 1194 { 1195 char buffer[PROC_NUMBUF]; 1196 int oom_score_adj; 1197 int err; 1198 1199 memset(buffer, 0, sizeof(buffer)); 1200 if (count > sizeof(buffer) - 1) 1201 count = sizeof(buffer) - 1; 1202 if (copy_from_user(buffer, buf, count)) { 1203 err = -EFAULT; 1204 goto out; 1205 } 1206 1207 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); 1208 if (err) 1209 goto out; 1210 if (oom_score_adj < OOM_SCORE_ADJ_MIN || 1211 oom_score_adj > OOM_SCORE_ADJ_MAX) { 1212 err = -EINVAL; 1213 goto out; 1214 } 1215 1216 err = __set_oom_adj(file, oom_score_adj, false); 1217 out: 1218 return err < 0 ? err : count; 1219 } 1220 1221 static const struct file_operations proc_oom_score_adj_operations = { 1222 .read = oom_score_adj_read, 1223 .write = oom_score_adj_write, 1224 .llseek = default_llseek, 1225 }; 1226 1227 #ifdef CONFIG_AUDIT 1228 #define TMPBUFLEN 11 1229 static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 1230 size_t count, loff_t *ppos) 1231 { 1232 struct inode * inode = file_inode(file); 1233 struct task_struct *task = get_proc_task(inode); 1234 ssize_t length; 1235 char tmpbuf[TMPBUFLEN]; 1236 1237 if (!task) 1238 return -ESRCH; 1239 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 1240 from_kuid(file->f_cred->user_ns, 1241 audit_get_loginuid(task))); 1242 put_task_struct(task); 1243 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1244 } 1245 1246 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 1247 size_t count, loff_t *ppos) 1248 { 1249 struct inode * inode = file_inode(file); 1250 uid_t loginuid; 1251 kuid_t kloginuid; 1252 int rv; 1253 1254 rcu_read_lock(); 1255 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { 1256 rcu_read_unlock(); 1257 return -EPERM; 1258 } 1259 rcu_read_unlock(); 1260 1261 if (*ppos != 0) { 1262 /* No partial writes. */ 1263 return -EINVAL; 1264 } 1265 1266 rv = kstrtou32_from_user(buf, count, 10, &loginuid); 1267 if (rv < 0) 1268 return rv; 1269 1270 /* is userspace tring to explicitly UNSET the loginuid? */ 1271 if (loginuid == AUDIT_UID_UNSET) { 1272 kloginuid = INVALID_UID; 1273 } else { 1274 kloginuid = make_kuid(file->f_cred->user_ns, loginuid); 1275 if (!uid_valid(kloginuid)) 1276 return -EINVAL; 1277 } 1278 1279 rv = audit_set_loginuid(kloginuid); 1280 if (rv < 0) 1281 return rv; 1282 return count; 1283 } 1284 1285 static const struct file_operations proc_loginuid_operations = { 1286 .read = proc_loginuid_read, 1287 .write = proc_loginuid_write, 1288 .llseek = generic_file_llseek, 1289 }; 1290 1291 static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 1292 size_t count, loff_t *ppos) 1293 { 1294 struct inode * inode = file_inode(file); 1295 struct task_struct *task = get_proc_task(inode); 1296 ssize_t length; 1297 char tmpbuf[TMPBUFLEN]; 1298 1299 if (!task) 1300 return -ESRCH; 1301 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 1302 audit_get_sessionid(task)); 1303 put_task_struct(task); 1304 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1305 } 1306 1307 static const struct file_operations proc_sessionid_operations = { 1308 .read = proc_sessionid_read, 1309 .llseek = generic_file_llseek, 1310 }; 1311 #endif 1312 1313 #ifdef CONFIG_FAULT_INJECTION 1314 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1315 size_t count, loff_t *ppos) 1316 { 1317 struct task_struct *task = get_proc_task(file_inode(file)); 1318 char buffer[PROC_NUMBUF]; 1319 size_t len; 1320 int make_it_fail; 1321 1322 if (!task) 1323 return -ESRCH; 1324 make_it_fail = task->make_it_fail; 1325 put_task_struct(task); 1326 1327 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 1328 1329 return simple_read_from_buffer(buf, count, ppos, buffer, len); 1330 } 1331 1332 static ssize_t proc_fault_inject_write(struct file * file, 1333 const char __user * buf, size_t count, loff_t *ppos) 1334 { 1335 struct task_struct *task; 1336 char buffer[PROC_NUMBUF]; 1337 int make_it_fail; 1338 int rv; 1339 1340 if (!capable(CAP_SYS_RESOURCE)) 1341 return -EPERM; 1342 memset(buffer, 0, sizeof(buffer)); 1343 if (count > sizeof(buffer) - 1) 1344 count = sizeof(buffer) - 1; 1345 if (copy_from_user(buffer, buf, count)) 1346 return -EFAULT; 1347 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail); 1348 if (rv < 0) 1349 return rv; 1350 if (make_it_fail < 0 || make_it_fail > 1) 1351 return -EINVAL; 1352 1353 task = get_proc_task(file_inode(file)); 1354 if (!task) 1355 return -ESRCH; 1356 task->make_it_fail = make_it_fail; 1357 put_task_struct(task); 1358 1359 return count; 1360 } 1361 1362 static const struct file_operations proc_fault_inject_operations = { 1363 .read = proc_fault_inject_read, 1364 .write = proc_fault_inject_write, 1365 .llseek = generic_file_llseek, 1366 }; 1367 1368 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf, 1369 size_t count, loff_t *ppos) 1370 { 1371 struct task_struct *task; 1372 int err; 1373 unsigned int n; 1374 1375 err = kstrtouint_from_user(buf, count, 0, &n); 1376 if (err) 1377 return err; 1378 1379 task = get_proc_task(file_inode(file)); 1380 if (!task) 1381 return -ESRCH; 1382 task->fail_nth = n; 1383 put_task_struct(task); 1384 1385 return count; 1386 } 1387 1388 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf, 1389 size_t count, loff_t *ppos) 1390 { 1391 struct task_struct *task; 1392 char numbuf[PROC_NUMBUF]; 1393 ssize_t len; 1394 1395 task = get_proc_task(file_inode(file)); 1396 if (!task) 1397 return -ESRCH; 1398 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth); 1399 put_task_struct(task); 1400 return simple_read_from_buffer(buf, count, ppos, numbuf, len); 1401 } 1402 1403 static const struct file_operations proc_fail_nth_operations = { 1404 .read = proc_fail_nth_read, 1405 .write = proc_fail_nth_write, 1406 }; 1407 #endif 1408 1409 1410 #ifdef CONFIG_SCHED_DEBUG 1411 /* 1412 * Print out various scheduling related per-task fields: 1413 */ 1414 static int sched_show(struct seq_file *m, void *v) 1415 { 1416 struct inode *inode = m->private; 1417 struct pid_namespace *ns = proc_pid_ns(inode); 1418 struct task_struct *p; 1419 1420 p = get_proc_task(inode); 1421 if (!p) 1422 return -ESRCH; 1423 proc_sched_show_task(p, ns, m); 1424 1425 put_task_struct(p); 1426 1427 return 0; 1428 } 1429 1430 static ssize_t 1431 sched_write(struct file *file, const char __user *buf, 1432 size_t count, loff_t *offset) 1433 { 1434 struct inode *inode = file_inode(file); 1435 struct task_struct *p; 1436 1437 p = get_proc_task(inode); 1438 if (!p) 1439 return -ESRCH; 1440 proc_sched_set_task(p); 1441 1442 put_task_struct(p); 1443 1444 return count; 1445 } 1446 1447 static int sched_open(struct inode *inode, struct file *filp) 1448 { 1449 return single_open(filp, sched_show, inode); 1450 } 1451 1452 static const struct file_operations proc_pid_sched_operations = { 1453 .open = sched_open, 1454 .read = seq_read, 1455 .write = sched_write, 1456 .llseek = seq_lseek, 1457 .release = single_release, 1458 }; 1459 1460 #endif 1461 1462 #ifdef CONFIG_SCHED_AUTOGROUP 1463 /* 1464 * Print out autogroup related information: 1465 */ 1466 static int sched_autogroup_show(struct seq_file *m, void *v) 1467 { 1468 struct inode *inode = m->private; 1469 struct task_struct *p; 1470 1471 p = get_proc_task(inode); 1472 if (!p) 1473 return -ESRCH; 1474 proc_sched_autogroup_show_task(p, m); 1475 1476 put_task_struct(p); 1477 1478 return 0; 1479 } 1480 1481 static ssize_t 1482 sched_autogroup_write(struct file *file, const char __user *buf, 1483 size_t count, loff_t *offset) 1484 { 1485 struct inode *inode = file_inode(file); 1486 struct task_struct *p; 1487 char buffer[PROC_NUMBUF]; 1488 int nice; 1489 int err; 1490 1491 memset(buffer, 0, sizeof(buffer)); 1492 if (count > sizeof(buffer) - 1) 1493 count = sizeof(buffer) - 1; 1494 if (copy_from_user(buffer, buf, count)) 1495 return -EFAULT; 1496 1497 err = kstrtoint(strstrip(buffer), 0, &nice); 1498 if (err < 0) 1499 return err; 1500 1501 p = get_proc_task(inode); 1502 if (!p) 1503 return -ESRCH; 1504 1505 err = proc_sched_autogroup_set_nice(p, nice); 1506 if (err) 1507 count = err; 1508 1509 put_task_struct(p); 1510 1511 return count; 1512 } 1513 1514 static int sched_autogroup_open(struct inode *inode, struct file *filp) 1515 { 1516 int ret; 1517 1518 ret = single_open(filp, sched_autogroup_show, NULL); 1519 if (!ret) { 1520 struct seq_file *m = filp->private_data; 1521 1522 m->private = inode; 1523 } 1524 return ret; 1525 } 1526 1527 static const struct file_operations proc_pid_sched_autogroup_operations = { 1528 .open = sched_autogroup_open, 1529 .read = seq_read, 1530 .write = sched_autogroup_write, 1531 .llseek = seq_lseek, 1532 .release = single_release, 1533 }; 1534 1535 #endif /* CONFIG_SCHED_AUTOGROUP */ 1536 1537 static ssize_t comm_write(struct file *file, const char __user *buf, 1538 size_t count, loff_t *offset) 1539 { 1540 struct inode *inode = file_inode(file); 1541 struct task_struct *p; 1542 char buffer[TASK_COMM_LEN]; 1543 const size_t maxlen = sizeof(buffer) - 1; 1544 1545 memset(buffer, 0, sizeof(buffer)); 1546 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count)) 1547 return -EFAULT; 1548 1549 p = get_proc_task(inode); 1550 if (!p) 1551 return -ESRCH; 1552 1553 if (same_thread_group(current, p)) 1554 set_task_comm(p, buffer); 1555 else 1556 count = -EINVAL; 1557 1558 put_task_struct(p); 1559 1560 return count; 1561 } 1562 1563 static int comm_show(struct seq_file *m, void *v) 1564 { 1565 struct inode *inode = m->private; 1566 struct task_struct *p; 1567 1568 p = get_proc_task(inode); 1569 if (!p) 1570 return -ESRCH; 1571 1572 proc_task_name(m, p, false); 1573 seq_putc(m, '\n'); 1574 1575 put_task_struct(p); 1576 1577 return 0; 1578 } 1579 1580 static int comm_open(struct inode *inode, struct file *filp) 1581 { 1582 return single_open(filp, comm_show, inode); 1583 } 1584 1585 static const struct file_operations proc_pid_set_comm_operations = { 1586 .open = comm_open, 1587 .read = seq_read, 1588 .write = comm_write, 1589 .llseek = seq_lseek, 1590 .release = single_release, 1591 }; 1592 1593 static int proc_exe_link(struct dentry *dentry, struct path *exe_path) 1594 { 1595 struct task_struct *task; 1596 struct file *exe_file; 1597 1598 task = get_proc_task(d_inode(dentry)); 1599 if (!task) 1600 return -ENOENT; 1601 exe_file = get_task_exe_file(task); 1602 put_task_struct(task); 1603 if (exe_file) { 1604 *exe_path = exe_file->f_path; 1605 path_get(&exe_file->f_path); 1606 fput(exe_file); 1607 return 0; 1608 } else 1609 return -ENOENT; 1610 } 1611 1612 static const char *proc_pid_get_link(struct dentry *dentry, 1613 struct inode *inode, 1614 struct delayed_call *done) 1615 { 1616 struct path path; 1617 int error = -EACCES; 1618 1619 if (!dentry) 1620 return ERR_PTR(-ECHILD); 1621 1622 /* Are we allowed to snoop on the tasks file descriptors? */ 1623 if (!proc_fd_access_allowed(inode)) 1624 goto out; 1625 1626 error = PROC_I(inode)->op.proc_get_link(dentry, &path); 1627 if (error) 1628 goto out; 1629 1630 nd_jump_link(&path); 1631 return NULL; 1632 out: 1633 return ERR_PTR(error); 1634 } 1635 1636 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 1637 { 1638 char *tmp = (char *)__get_free_page(GFP_KERNEL); 1639 char *pathname; 1640 int len; 1641 1642 if (!tmp) 1643 return -ENOMEM; 1644 1645 pathname = d_path(path, tmp, PAGE_SIZE); 1646 len = PTR_ERR(pathname); 1647 if (IS_ERR(pathname)) 1648 goto out; 1649 len = tmp + PAGE_SIZE - 1 - pathname; 1650 1651 if (len > buflen) 1652 len = buflen; 1653 if (copy_to_user(buffer, pathname, len)) 1654 len = -EFAULT; 1655 out: 1656 free_page((unsigned long)tmp); 1657 return len; 1658 } 1659 1660 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 1661 { 1662 int error = -EACCES; 1663 struct inode *inode = d_inode(dentry); 1664 struct path path; 1665 1666 /* Are we allowed to snoop on the tasks file descriptors? */ 1667 if (!proc_fd_access_allowed(inode)) 1668 goto out; 1669 1670 error = PROC_I(inode)->op.proc_get_link(dentry, &path); 1671 if (error) 1672 goto out; 1673 1674 error = do_proc_readlink(&path, buffer, buflen); 1675 path_put(&path); 1676 out: 1677 return error; 1678 } 1679 1680 const struct inode_operations proc_pid_link_inode_operations = { 1681 .readlink = proc_pid_readlink, 1682 .get_link = proc_pid_get_link, 1683 .setattr = proc_setattr, 1684 }; 1685 1686 1687 /* building an inode */ 1688 1689 void task_dump_owner(struct task_struct *task, umode_t mode, 1690 kuid_t *ruid, kgid_t *rgid) 1691 { 1692 /* Depending on the state of dumpable compute who should own a 1693 * proc file for a task. 1694 */ 1695 const struct cred *cred; 1696 kuid_t uid; 1697 kgid_t gid; 1698 1699 if (unlikely(task->flags & PF_KTHREAD)) { 1700 *ruid = GLOBAL_ROOT_UID; 1701 *rgid = GLOBAL_ROOT_GID; 1702 return; 1703 } 1704 1705 /* Default to the tasks effective ownership */ 1706 rcu_read_lock(); 1707 cred = __task_cred(task); 1708 uid = cred->euid; 1709 gid = cred->egid; 1710 rcu_read_unlock(); 1711 1712 /* 1713 * Before the /proc/pid/status file was created the only way to read 1714 * the effective uid of a /process was to stat /proc/pid. Reading 1715 * /proc/pid/status is slow enough that procps and other packages 1716 * kept stating /proc/pid. To keep the rules in /proc simple I have 1717 * made this apply to all per process world readable and executable 1718 * directories. 1719 */ 1720 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) { 1721 struct mm_struct *mm; 1722 task_lock(task); 1723 mm = task->mm; 1724 /* Make non-dumpable tasks owned by some root */ 1725 if (mm) { 1726 if (get_dumpable(mm) != SUID_DUMP_USER) { 1727 struct user_namespace *user_ns = mm->user_ns; 1728 1729 uid = make_kuid(user_ns, 0); 1730 if (!uid_valid(uid)) 1731 uid = GLOBAL_ROOT_UID; 1732 1733 gid = make_kgid(user_ns, 0); 1734 if (!gid_valid(gid)) 1735 gid = GLOBAL_ROOT_GID; 1736 } 1737 } else { 1738 uid = GLOBAL_ROOT_UID; 1739 gid = GLOBAL_ROOT_GID; 1740 } 1741 task_unlock(task); 1742 } 1743 *ruid = uid; 1744 *rgid = gid; 1745 } 1746 1747 struct inode *proc_pid_make_inode(struct super_block * sb, 1748 struct task_struct *task, umode_t mode) 1749 { 1750 struct inode * inode; 1751 struct proc_inode *ei; 1752 1753 /* We need a new inode */ 1754 1755 inode = new_inode(sb); 1756 if (!inode) 1757 goto out; 1758 1759 /* Common stuff */ 1760 ei = PROC_I(inode); 1761 inode->i_mode = mode; 1762 inode->i_ino = get_next_ino(); 1763 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 1764 inode->i_op = &proc_def_inode_operations; 1765 1766 /* 1767 * grab the reference to task. 1768 */ 1769 ei->pid = get_task_pid(task, PIDTYPE_PID); 1770 if (!ei->pid) 1771 goto out_unlock; 1772 1773 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); 1774 security_task_to_inode(task, inode); 1775 1776 out: 1777 return inode; 1778 1779 out_unlock: 1780 iput(inode); 1781 return NULL; 1782 } 1783 1784 int pid_getattr(const struct path *path, struct kstat *stat, 1785 u32 request_mask, unsigned int query_flags) 1786 { 1787 struct inode *inode = d_inode(path->dentry); 1788 struct pid_namespace *pid = proc_pid_ns(inode); 1789 struct task_struct *task; 1790 1791 generic_fillattr(inode, stat); 1792 1793 stat->uid = GLOBAL_ROOT_UID; 1794 stat->gid = GLOBAL_ROOT_GID; 1795 rcu_read_lock(); 1796 task = pid_task(proc_pid(inode), PIDTYPE_PID); 1797 if (task) { 1798 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) { 1799 rcu_read_unlock(); 1800 /* 1801 * This doesn't prevent learning whether PID exists, 1802 * it only makes getattr() consistent with readdir(). 1803 */ 1804 return -ENOENT; 1805 } 1806 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid); 1807 } 1808 rcu_read_unlock(); 1809 return 0; 1810 } 1811 1812 /* dentry stuff */ 1813 1814 /* 1815 * Set <pid>/... inode ownership (can change due to setuid(), etc.) 1816 */ 1817 void pid_update_inode(struct task_struct *task, struct inode *inode) 1818 { 1819 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid); 1820 1821 inode->i_mode &= ~(S_ISUID | S_ISGID); 1822 security_task_to_inode(task, inode); 1823 } 1824 1825 /* 1826 * Rewrite the inode's ownerships here because the owning task may have 1827 * performed a setuid(), etc. 1828 * 1829 */ 1830 static int pid_revalidate(struct dentry *dentry, unsigned int flags) 1831 { 1832 struct inode *inode; 1833 struct task_struct *task; 1834 1835 if (flags & LOOKUP_RCU) 1836 return -ECHILD; 1837 1838 inode = d_inode(dentry); 1839 task = get_proc_task(inode); 1840 1841 if (task) { 1842 pid_update_inode(task, inode); 1843 put_task_struct(task); 1844 return 1; 1845 } 1846 return 0; 1847 } 1848 1849 static inline bool proc_inode_is_dead(struct inode *inode) 1850 { 1851 return !proc_pid(inode)->tasks[PIDTYPE_PID].first; 1852 } 1853 1854 int pid_delete_dentry(const struct dentry *dentry) 1855 { 1856 /* Is the task we represent dead? 1857 * If so, then don't put the dentry on the lru list, 1858 * kill it immediately. 1859 */ 1860 return proc_inode_is_dead(d_inode(dentry)); 1861 } 1862 1863 const struct dentry_operations pid_dentry_operations = 1864 { 1865 .d_revalidate = pid_revalidate, 1866 .d_delete = pid_delete_dentry, 1867 }; 1868 1869 /* Lookups */ 1870 1871 /* 1872 * Fill a directory entry. 1873 * 1874 * If possible create the dcache entry and derive our inode number and 1875 * file type from dcache entry. 1876 * 1877 * Since all of the proc inode numbers are dynamically generated, the inode 1878 * numbers do not exist until the inode is cache. This means creating the 1879 * the dcache entry in readdir is necessary to keep the inode numbers 1880 * reported by readdir in sync with the inode numbers reported 1881 * by stat. 1882 */ 1883 bool proc_fill_cache(struct file *file, struct dir_context *ctx, 1884 const char *name, unsigned int len, 1885 instantiate_t instantiate, struct task_struct *task, const void *ptr) 1886 { 1887 struct dentry *child, *dir = file->f_path.dentry; 1888 struct qstr qname = QSTR_INIT(name, len); 1889 struct inode *inode; 1890 unsigned type = DT_UNKNOWN; 1891 ino_t ino = 1; 1892 1893 child = d_hash_and_lookup(dir, &qname); 1894 if (!child) { 1895 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1896 child = d_alloc_parallel(dir, &qname, &wq); 1897 if (IS_ERR(child)) 1898 goto end_instantiate; 1899 if (d_in_lookup(child)) { 1900 struct dentry *res; 1901 res = instantiate(child, task, ptr); 1902 d_lookup_done(child); 1903 if (unlikely(res)) { 1904 dput(child); 1905 child = res; 1906 if (IS_ERR(child)) 1907 goto end_instantiate; 1908 } 1909 } 1910 } 1911 inode = d_inode(child); 1912 ino = inode->i_ino; 1913 type = inode->i_mode >> 12; 1914 dput(child); 1915 end_instantiate: 1916 return dir_emit(ctx, name, len, ino, type); 1917 } 1918 1919 /* 1920 * dname_to_vma_addr - maps a dentry name into two unsigned longs 1921 * which represent vma start and end addresses. 1922 */ 1923 static int dname_to_vma_addr(struct dentry *dentry, 1924 unsigned long *start, unsigned long *end) 1925 { 1926 const char *str = dentry->d_name.name; 1927 unsigned long long sval, eval; 1928 unsigned int len; 1929 1930 if (str[0] == '' && str[1] != '-') 1931 return -EINVAL; 1932 len = _parse_integer(str, 16, &sval); 1933 if (len & KSTRTOX_OVERFLOW) 1934 return -EINVAL; 1935 if (sval != (unsigned long)sval) 1936 return -EINVAL; 1937 str += len; 1938 1939 if (*str != '-') 1940 return -EINVAL; 1941 str++; 1942 1943 if (str[0] == '' && str[1]) 1944 return -EINVAL; 1945 len = _parse_integer(str, 16, &eval); 1946 if (len & KSTRTOX_OVERFLOW) 1947 return -EINVAL; 1948 if (eval != (unsigned long)eval) 1949 return -EINVAL; 1950 str += len; 1951 1952 if (*str != '\0') 1953 return -EINVAL; 1954 1955 *start = sval; 1956 *end = eval; 1957 1958 return 0; 1959 } 1960 1961 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags) 1962 { 1963 unsigned long vm_start, vm_end; 1964 bool exact_vma_exists = false; 1965 struct mm_struct *mm = NULL; 1966 struct task_struct *task; 1967 struct inode *inode; 1968 int status = 0; 1969 1970 if (flags & LOOKUP_RCU) 1971 return -ECHILD; 1972 1973 inode = d_inode(dentry); 1974 task = get_proc_task(inode); 1975 if (!task) 1976 goto out_notask; 1977 1978 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS); 1979 if (IS_ERR_OR_NULL(mm)) 1980 goto out; 1981 1982 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { 1983 status = down_read_killable(&mm->mmap_sem); 1984 if (!status) { 1985 exact_vma_exists = !!find_exact_vma(mm, vm_start, 1986 vm_end); 1987 up_read(&mm->mmap_sem); 1988 } 1989 } 1990 1991 mmput(mm); 1992 1993 if (exact_vma_exists) { 1994 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); 1995 1996 security_task_to_inode(task, inode); 1997 status = 1; 1998 } 1999 2000 out: 2001 put_task_struct(task); 2002 2003 out_notask: 2004 return status; 2005 } 2006 2007 static const struct dentry_operations tid_map_files_dentry_operations = { 2008 .d_revalidate = map_files_d_revalidate, 2009 .d_delete = pid_delete_dentry, 2010 }; 2011 2012 static int map_files_get_link(struct dentry *dentry, struct path *path) 2013 { 2014 unsigned long vm_start, vm_end; 2015 struct vm_area_struct *vma; 2016 struct task_struct *task; 2017 struct mm_struct *mm; 2018 int rc; 2019 2020 rc = -ENOENT; 2021 task = get_proc_task(d_inode(dentry)); 2022 if (!task) 2023 goto out; 2024 2025 mm = get_task_mm(task); 2026 put_task_struct(task); 2027 if (!mm) 2028 goto out; 2029 2030 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); 2031 if (rc) 2032 goto out_mmput; 2033 2034 rc = down_read_killable(&mm->mmap_sem); 2035 if (rc) 2036 goto out_mmput; 2037 2038 rc = -ENOENT; 2039 vma = find_exact_vma(mm, vm_start, vm_end); 2040 if (vma && vma->vm_file) { 2041 *path = vma->vm_file->f_path; 2042 path_get(path); 2043 rc = 0; 2044 } 2045 up_read(&mm->mmap_sem); 2046 2047 out_mmput: 2048 mmput(mm); 2049 out: 2050 return rc; 2051 } 2052 2053 struct map_files_info { 2054 unsigned long start; 2055 unsigned long end; 2056 fmode_t mode; 2057 }; 2058 2059 /* 2060 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the 2061 * symlinks may be used to bypass permissions on ancestor directories in the 2062 * path to the file in question. 2063 */ 2064 static const char * 2065 proc_map_files_get_link(struct dentry *dentry, 2066 struct inode *inode, 2067 struct delayed_call *done) 2068 { 2069 if (!capable(CAP_SYS_ADMIN)) 2070 return ERR_PTR(-EPERM); 2071 2072 return proc_pid_get_link(dentry, inode, done); 2073 } 2074 2075 /* 2076 * Identical to proc_pid_link_inode_operations except for get_link() 2077 */ 2078 static const struct inode_operations proc_map_files_link_inode_operations = { 2079 .readlink = proc_pid_readlink, 2080 .get_link = proc_map_files_get_link, 2081 .setattr = proc_setattr, 2082 }; 2083 2084 static struct dentry * 2085 proc_map_files_instantiate(struct dentry *dentry, 2086 struct task_struct *task, const void *ptr) 2087 { 2088 fmode_t mode = (fmode_t)(unsigned long)ptr; 2089 struct proc_inode *ei; 2090 struct inode *inode; 2091 2092 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | 2093 ((mode & FMODE_READ ) ? S_IRUSR : 0) | 2094 ((mode & FMODE_WRITE) ? S_IWUSR : 0)); 2095 if (!inode) 2096 return ERR_PTR(-ENOENT); 2097 2098 ei = PROC_I(inode); 2099 ei->op.proc_get_link = map_files_get_link; 2100 2101 inode->i_op = &proc_map_files_link_inode_operations; 2102 inode->i_size = 64; 2103 2104 d_set_d_op(dentry, &tid_map_files_dentry_operations); 2105 return d_splice_alias(inode, dentry); 2106 } 2107 2108 static struct dentry *proc_map_files_lookup(struct inode *dir, 2109 struct dentry *dentry, unsigned int flags) 2110 { 2111 unsigned long vm_start, vm_end; 2112 struct vm_area_struct *vma; 2113 struct task_struct *task; 2114 struct dentry *result; 2115 struct mm_struct *mm; 2116 2117 result = ERR_PTR(-ENOENT); 2118 task = get_proc_task(dir); 2119 if (!task) 2120 goto out; 2121 2122 result = ERR_PTR(-EACCES); 2123 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) 2124 goto out_put_task; 2125 2126 result = ERR_PTR(-ENOENT); 2127 if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) 2128 goto out_put_task; 2129 2130 mm = get_task_mm(task); 2131 if (!mm) 2132 goto out_put_task; 2133 2134 result = ERR_PTR(-EINTR); 2135 if (down_read_killable(&mm->mmap_sem)) 2136 goto out_put_mm; 2137 2138 result = ERR_PTR(-ENOENT); 2139 vma = find_exact_vma(mm, vm_start, vm_end); 2140 if (!vma) 2141 goto out_no_vma; 2142 2143 if (vma->vm_file) 2144 result = proc_map_files_instantiate(dentry, task, 2145 (void *)(unsigned long)vma->vm_file->f_mode); 2146 2147 out_no_vma: 2148 up_read(&mm->mmap_sem); 2149 out_put_mm: 2150 mmput(mm); 2151 out_put_task: 2152 put_task_struct(task); 2153 out: 2154 return result; 2155 } 2156 2157 static const struct inode_operations proc_map_files_inode_operations = { 2158 .lookup = proc_map_files_lookup, 2159 .permission = proc_fd_permission, 2160 .setattr = proc_setattr, 2161 }; 2162 2163 static int 2164 proc_map_files_readdir(struct file *file, struct dir_context *ctx) 2165 { 2166 struct vm_area_struct *vma; 2167 struct task_struct *task; 2168 struct mm_struct *mm; 2169 unsigned long nr_files, pos, i; 2170 GENRADIX(struct map_files_info) fa; 2171 struct map_files_info *p; 2172 int ret; 2173 2174 genradix_init(&fa); 2175 2176 ret = -ENOENT; 2177 task = get_proc_task(file_inode(file)); 2178 if (!task) 2179 goto out; 2180 2181 ret = -EACCES; 2182 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) 2183 goto out_put_task; 2184 2185 ret = 0; 2186 if (!dir_emit_dots(file, ctx)) 2187 goto out_put_task; 2188 2189 mm = get_task_mm(task); 2190 if (!mm) 2191 goto out_put_task; 2192 2193 ret = down_read_killable(&mm->mmap_sem); 2194 if (ret) { 2195 mmput(mm); 2196 goto out_put_task; 2197 } 2198 2199 nr_files = 0; 2200 2201 /* 2202 * We need two passes here: 2203 * 2204 * 1) Collect vmas of mapped files with mmap_sem taken 2205 * 2) Release mmap_sem and instantiate entries 2206 * 2207 * otherwise we get lockdep complained, since filldir() 2208 * routine might require mmap_sem taken in might_fault(). 2209 */ 2210 2211 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { 2212 if (!vma->vm_file) 2213 continue; 2214 if (++pos <= ctx->pos) 2215 continue; 2216 2217 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL); 2218 if (!p) { 2219 ret = -ENOMEM; 2220 up_read(&mm->mmap_sem); 2221 mmput(mm); 2222 goto out_put_task; 2223 } 2224 2225 p->start = vma->vm_start; 2226 p->end = vma->vm_end; 2227 p->mode = vma->vm_file->f_mode; 2228 } 2229 up_read(&mm->mmap_sem); 2230 mmput(mm); 2231 2232 for (i = 0; i < nr_files; i++) { 2233 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */ 2234 unsigned int len; 2235 2236 p = genradix_ptr(&fa, i); 2237 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end); 2238 if (!proc_fill_cache(file, ctx, 2239 buf, len, 2240 proc_map_files_instantiate, 2241 task, 2242 (void *)(unsigned long)p->mode)) 2243 break; 2244 ctx->pos++; 2245 } 2246 2247 out_put_task: 2248 put_task_struct(task); 2249 out: 2250 genradix_free(&fa); 2251 return ret; 2252 } 2253 2254 static const struct file_operations proc_map_files_operations = { 2255 .read = generic_read_dir, 2256 .iterate_shared = proc_map_files_readdir, 2257 .llseek = generic_file_llseek, 2258 }; 2259 2260 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS) 2261 struct timers_private { 2262 struct pid *pid; 2263 struct task_struct *task; 2264 struct sighand_struct *sighand; 2265 struct pid_namespace *ns; 2266 unsigned long flags; 2267 }; 2268 2269 static void *timers_start(struct seq_file *m, loff_t *pos) 2270 { 2271 struct timers_private *tp = m->private; 2272 2273 tp->task = get_pid_task(tp->pid, PIDTYPE_PID); 2274 if (!tp->task) 2275 return ERR_PTR(-ESRCH); 2276 2277 tp->sighand = lock_task_sighand(tp->task, &tp->flags); 2278 if (!tp->sighand) 2279 return ERR_PTR(-ESRCH); 2280 2281 return seq_list_start(&tp->task->signal->posix_timers, *pos); 2282 } 2283 2284 static void *timers_next(struct seq_file *m, void *v, loff_t *pos) 2285 { 2286 struct timers_private *tp = m->private; 2287 return seq_list_next(v, &tp->task->signal->posix_timers, pos); 2288 } 2289 2290 static void timers_stop(struct seq_file *m, void *v) 2291 { 2292 struct timers_private *tp = m->private; 2293 2294 if (tp->sighand) { 2295 unlock_task_sighand(tp->task, &tp->flags); 2296 tp->sighand = NULL; 2297 } 2298 2299 if (tp->task) { 2300 put_task_struct(tp->task); 2301 tp->task = NULL; 2302 } 2303 } 2304 2305 static int show_timer(struct seq_file *m, void *v) 2306 { 2307 struct k_itimer *timer; 2308 struct timers_private *tp = m->private; 2309 int notify; 2310 static const char * const nstr[] = { 2311 [SIGEV_SIGNAL] = "signal", 2312 [SIGEV_NONE] = "none", 2313 [SIGEV_THREAD] = "thread", 2314 }; 2315 2316 timer = list_entry((struct list_head *)v, struct k_itimer, list); 2317 notify = timer->it_sigev_notify; 2318 2319 seq_printf(m, "ID: %d\n", timer->it_id); 2320 seq_printf(m, "signal: %d/%px\n", 2321 timer->sigq->info.si_signo, 2322 timer->sigq->info.si_value.sival_ptr); 2323 seq_printf(m, "notify: %s/%s.%d\n", 2324 nstr[notify & ~SIGEV_THREAD_ID], 2325 (notify & SIGEV_THREAD_ID) ? "tid" : "pid", 2326 pid_nr_ns(timer->it_pid, tp->ns)); 2327 seq_printf(m, "ClockID: %d\n", timer->it_clock); 2328 2329 return 0; 2330 } 2331 2332 static const struct seq_operations proc_timers_seq_ops = { 2333 .start = timers_start, 2334 .next = timers_next, 2335 .stop = timers_stop, 2336 .show = show_timer, 2337 }; 2338 2339 static int proc_timers_open(struct inode *inode, struct file *file) 2340 { 2341 struct timers_private *tp; 2342 2343 tp = __seq_open_private(file, &proc_timers_seq_ops, 2344 sizeof(struct timers_private)); 2345 if (!tp) 2346 return -ENOMEM; 2347 2348 tp->pid = proc_pid(inode); 2349 tp->ns = proc_pid_ns(inode); 2350 return 0; 2351 } 2352 2353 static const struct file_operations proc_timers_operations = { 2354 .open = proc_timers_open, 2355 .read = seq_read, 2356 .llseek = seq_lseek, 2357 .release = seq_release_private, 2358 }; 2359 #endif 2360 2361 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf, 2362 size_t count, loff_t *offset) 2363 { 2364 struct inode *inode = file_inode(file); 2365 struct task_struct *p; 2366 u64 slack_ns; 2367 int err; 2368 2369 err = kstrtoull_from_user(buf, count, 10, &slack_ns); 2370 if (err < 0) 2371 return err; 2372 2373 p = get_proc_task(inode); 2374 if (!p) 2375 return -ESRCH; 2376 2377 if (p != current) { 2378 rcu_read_lock(); 2379 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { 2380 rcu_read_unlock(); 2381 count = -EPERM; 2382 goto out; 2383 } 2384 rcu_read_unlock(); 2385 2386 err = security_task_setscheduler(p); 2387 if (err) { 2388 count = err; 2389 goto out; 2390 } 2391 } 2392 2393 task_lock(p); 2394 if (slack_ns == 0) 2395 p->timer_slack_ns = p->default_timer_slack_ns; 2396 else 2397 p->timer_slack_ns = slack_ns; 2398 task_unlock(p); 2399 2400 out: 2401 put_task_struct(p); 2402 2403 return count; 2404 } 2405 2406 static int timerslack_ns_show(struct seq_file *m, void *v) 2407 { 2408 struct inode *inode = m->private; 2409 struct task_struct *p; 2410 int err = 0; 2411 2412 p = get_proc_task(inode); 2413 if (!p) 2414 return -ESRCH; 2415 2416 if (p != current) { 2417 rcu_read_lock(); 2418 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { 2419 rcu_read_unlock(); 2420 err = -EPERM; 2421 goto out; 2422 } 2423 rcu_read_unlock(); 2424 2425 err = security_task_getscheduler(p); 2426 if (err) 2427 goto out; 2428 } 2429 2430 task_lock(p); 2431 seq_printf(m, "%llu\n", p->timer_slack_ns); 2432 task_unlock(p); 2433 2434 out: 2435 put_task_struct(p); 2436 2437 return err; 2438 } 2439 2440 static int timerslack_ns_open(struct inode *inode, struct file *filp) 2441 { 2442 return single_open(filp, timerslack_ns_show, inode); 2443 } 2444 2445 static const struct file_operations proc_pid_set_timerslack_ns_operations = { 2446 .open = timerslack_ns_open, 2447 .read = seq_read, 2448 .write = timerslack_ns_write, 2449 .llseek = seq_lseek, 2450 .release = single_release, 2451 }; 2452 2453 static struct dentry *proc_pident_instantiate(struct dentry *dentry, 2454 struct task_struct *task, const void *ptr) 2455 { 2456 const struct pid_entry *p = ptr; 2457 struct inode *inode; 2458 struct proc_inode *ei; 2459 2460 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode); 2461 if (!inode) 2462 return ERR_PTR(-ENOENT); 2463 2464 ei = PROC_I(inode); 2465 if (S_ISDIR(inode->i_mode)) 2466 set_nlink(inode, 2); /* Use getattr to fix if necessary */ 2467 if (p->iop) 2468 inode->i_op = p->iop; 2469 if (p->fop) 2470 inode->i_fop = p->fop; 2471 ei->op = p->op; 2472 pid_update_inode(task, inode); 2473 d_set_d_op(dentry, &pid_dentry_operations); 2474 return d_splice_alias(inode, dentry); 2475 } 2476 2477 static struct dentry *proc_pident_lookup(struct inode *dir, 2478 struct dentry *dentry, 2479 const struct pid_entry *p, 2480 const struct pid_entry *end) 2481 { 2482 struct task_struct *task = get_proc_task(dir); 2483 struct dentry *res = ERR_PTR(-ENOENT); 2484 2485 if (!task) 2486 goto out_no_task; 2487 2488 /* 2489 * Yes, it does not scale. And it should not. Don't add 2490 * new entries into /proc/<tgid>/ without very good reasons. 2491 */ 2492 for (; p < end; p++) { 2493 if (p->len != dentry->d_name.len) 2494 continue; 2495 if (!memcmp(dentry->d_name.name, p->name, p->len)) { 2496 res = proc_pident_instantiate(dentry, task, p); 2497 break; 2498 } 2499 } 2500 put_task_struct(task); 2501 out_no_task: 2502 return res; 2503 } 2504 2505 static int proc_pident_readdir(struct file *file, struct dir_context *ctx, 2506 const struct pid_entry *ents, unsigned int nents) 2507 { 2508 struct task_struct *task = get_proc_task(file_inode(file)); 2509 const struct pid_entry *p; 2510 2511 if (!task) 2512 return -ENOENT; 2513 2514 if (!dir_emit_dots(file, ctx)) 2515 goto out; 2516 2517 if (ctx->pos >= nents + 2) 2518 goto out; 2519 2520 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) { 2521 if (!proc_fill_cache(file, ctx, p->name, p->len, 2522 proc_pident_instantiate, task, p)) 2523 break; 2524 ctx->pos++; 2525 } 2526 out: 2527 put_task_struct(task); 2528 return 0; 2529 } 2530 2531 #ifdef CONFIG_SECURITY 2532 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 2533 size_t count, loff_t *ppos) 2534 { 2535 struct inode * inode = file_inode(file); 2536 char *p = NULL; 2537 ssize_t length; 2538 struct task_struct *task = get_proc_task(inode); 2539 2540 if (!task) 2541 return -ESRCH; 2542 2543 length = security_getprocattr(task, PROC_I(inode)->op.lsm, 2544 (char*)file->f_path.dentry->d_name.name, 2545 &p); 2546 put_task_struct(task); 2547 if (length > 0) 2548 length = simple_read_from_buffer(buf, count, ppos, p, length); 2549 kfree(p); 2550 return length; 2551 } 2552 2553 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 2554 size_t count, loff_t *ppos) 2555 { 2556 struct inode * inode = file_inode(file); 2557 struct task_struct *task; 2558 void *page; 2559 int rv; 2560 2561 rcu_read_lock(); 2562 task = pid_task(proc_pid(inode), PIDTYPE_PID); 2563 if (!task) { 2564 rcu_read_unlock(); 2565 return -ESRCH; 2566 } 2567 /* A task may only write its own attributes. */ 2568 if (current != task) { 2569 rcu_read_unlock(); 2570 return -EACCES; 2571 } 2572 /* Prevent changes to overridden credentials. */ 2573 if (current_cred() != current_real_cred()) { 2574 rcu_read_unlock(); 2575 return -EBUSY; 2576 } 2577 rcu_read_unlock(); 2578 2579 if (count > PAGE_SIZE) 2580 count = PAGE_SIZE; 2581 2582 /* No partial writes. */ 2583 if (*ppos != 0) 2584 return -EINVAL; 2585 2586 page = memdup_user(buf, count); 2587 if (IS_ERR(page)) { 2588 rv = PTR_ERR(page); 2589 goto out; 2590 } 2591 2592 /* Guard against adverse ptrace interaction */ 2593 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex); 2594 if (rv < 0) 2595 goto out_free; 2596 2597 rv = security_setprocattr(PROC_I(inode)->op.lsm, 2598 file->f_path.dentry->d_name.name, page, 2599 count); 2600 mutex_unlock(¤t->signal->cred_guard_mutex); 2601 out_free: 2602 kfree(page); 2603 out: 2604 return rv; 2605 } 2606 2607 static const struct file_operations proc_pid_attr_operations = { 2608 .read = proc_pid_attr_read, 2609 .write = proc_pid_attr_write, 2610 .llseek = generic_file_llseek, 2611 }; 2612 2613 #define LSM_DIR_OPS(LSM) \ 2614 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \ 2615 struct dir_context *ctx) \ 2616 { \ 2617 return proc_pident_readdir(filp, ctx, \ 2618 LSM##_attr_dir_stuff, \ 2619 ARRAY_SIZE(LSM##_attr_dir_stuff)); \ 2620 } \ 2621 \ 2622 static const struct file_operations proc_##LSM##_attr_dir_ops = { \ 2623 .read = generic_read_dir, \ 2624 .iterate = proc_##LSM##_attr_dir_iterate, \ 2625 .llseek = default_llseek, \ 2626 }; \ 2627 \ 2628 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \ 2629 struct dentry *dentry, unsigned int flags) \ 2630 { \ 2631 return proc_pident_lookup(dir, dentry, \ 2632 LSM##_attr_dir_stuff, \ 2633 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \ 2634 } \ 2635 \ 2636 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \ 2637 .lookup = proc_##LSM##_attr_dir_lookup, \ 2638 .getattr = pid_getattr, \ 2639 .setattr = proc_setattr, \ 2640 } 2641 2642 #ifdef CONFIG_SECURITY_SMACK 2643 static const struct pid_entry smack_attr_dir_stuff[] = { 2644 ATTR("smack", "current", 0666), 2645 }; 2646 LSM_DIR_OPS(smack); 2647 #endif 2648 2649 static const struct pid_entry attr_dir_stuff[] = { 2650 ATTR(NULL, "current", 0666), 2651 ATTR(NULL, "prev", 0444), 2652 ATTR(NULL, "exec", 0666), 2653 ATTR(NULL, "fscreate", 0666), 2654 ATTR(NULL, "keycreate", 0666), 2655 ATTR(NULL, "sockcreate", 0666), 2656 #ifdef CONFIG_SECURITY_SMACK 2657 DIR("smack", 0555, 2658 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops), 2659 #endif 2660 }; 2661 2662 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx) 2663 { 2664 return proc_pident_readdir(file, ctx, 2665 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 2666 } 2667 2668 static const struct file_operations proc_attr_dir_operations = { 2669 .read = generic_read_dir, 2670 .iterate_shared = proc_attr_dir_readdir, 2671 .llseek = generic_file_llseek, 2672 }; 2673 2674 static struct dentry *proc_attr_dir_lookup(struct inode *dir, 2675 struct dentry *dentry, unsigned int flags) 2676 { 2677 return proc_pident_lookup(dir, dentry, 2678 attr_dir_stuff, 2679 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff)); 2680 } 2681 2682 static const struct inode_operations proc_attr_dir_inode_operations = { 2683 .lookup = proc_attr_dir_lookup, 2684 .getattr = pid_getattr, 2685 .setattr = proc_setattr, 2686 }; 2687 2688 #endif 2689 2690 #ifdef CONFIG_ELF_CORE 2691 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 2692 size_t count, loff_t *ppos) 2693 { 2694 struct task_struct *task = get_proc_task(file_inode(file)); 2695 struct mm_struct *mm; 2696 char buffer[PROC_NUMBUF]; 2697 size_t len; 2698 int ret; 2699 2700 if (!task) 2701 return -ESRCH; 2702 2703 ret = 0; 2704 mm = get_task_mm(task); 2705 if (mm) { 2706 len = snprintf(buffer, sizeof(buffer), "%08lx\n", 2707 ((mm->flags & MMF_DUMP_FILTER_MASK) >> 2708 MMF_DUMP_FILTER_SHIFT)); 2709 mmput(mm); 2710 ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 2711 } 2712 2713 put_task_struct(task); 2714 2715 return ret; 2716 } 2717 2718 static ssize_t proc_coredump_filter_write(struct file *file, 2719 const char __user *buf, 2720 size_t count, 2721 loff_t *ppos) 2722 { 2723 struct task_struct *task; 2724 struct mm_struct *mm; 2725 unsigned int val; 2726 int ret; 2727 int i; 2728 unsigned long mask; 2729 2730 ret = kstrtouint_from_user(buf, count, 0, &val); 2731 if (ret < 0) 2732 return ret; 2733 2734 ret = -ESRCH; 2735 task = get_proc_task(file_inode(file)); 2736 if (!task) 2737 goto out_no_task; 2738 2739 mm = get_task_mm(task); 2740 if (!mm) 2741 goto out_no_mm; 2742 ret = 0; 2743 2744 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 2745 if (val & mask) 2746 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 2747 else 2748 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 2749 } 2750 2751 mmput(mm); 2752 out_no_mm: 2753 put_task_struct(task); 2754 out_no_task: 2755 if (ret < 0) 2756 return ret; 2757 return count; 2758 } 2759 2760 static const struct file_operations proc_coredump_filter_operations = { 2761 .read = proc_coredump_filter_read, 2762 .write = proc_coredump_filter_write, 2763 .llseek = generic_file_llseek, 2764 }; 2765 #endif 2766 2767 #ifdef CONFIG_TASK_IO_ACCOUNTING 2768 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole) 2769 { 2770 struct task_io_accounting acct = task->ioac; 2771 unsigned long flags; 2772 int result; 2773 2774 result = mutex_lock_killable(&task->signal->cred_guard_mutex); 2775 if (result) 2776 return result; 2777 2778 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) { 2779 result = -EACCES; 2780 goto out_unlock; 2781 } 2782 2783 if (whole && lock_task_sighand(task, &flags)) { 2784 struct task_struct *t = task; 2785 2786 task_io_accounting_add(&acct, &task->signal->ioac); 2787 while_each_thread(task, t) 2788 task_io_accounting_add(&acct, &t->ioac); 2789 2790 unlock_task_sighand(task, &flags); 2791 } 2792 seq_printf(m, 2793 "rchar: %llu\n" 2794 "wchar: %llu\n" 2795 "syscr: %llu\n" 2796 "syscw: %llu\n" 2797 "read_bytes: %llu\n" 2798 "write_bytes: %llu\n" 2799 "cancelled_write_bytes: %llu\n", 2800 (unsigned long long)acct.rchar, 2801 (unsigned long long)acct.wchar, 2802 (unsigned long long)acct.syscr, 2803 (unsigned long long)acct.syscw, 2804 (unsigned long long)acct.read_bytes, 2805 (unsigned long long)acct.write_bytes, 2806 (unsigned long long)acct.cancelled_write_bytes); 2807 result = 0; 2808 2809 out_unlock: 2810 mutex_unlock(&task->signal->cred_guard_mutex); 2811 return result; 2812 } 2813 2814 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns, 2815 struct pid *pid, struct task_struct *task) 2816 { 2817 return do_io_accounting(task, m, 0); 2818 } 2819 2820 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns, 2821 struct pid *pid, struct task_struct *task) 2822 { 2823 return do_io_accounting(task, m, 1); 2824 } 2825 #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2826 2827 #ifdef CONFIG_USER_NS 2828 static int proc_id_map_open(struct inode *inode, struct file *file, 2829 const struct seq_operations *seq_ops) 2830 { 2831 struct user_namespace *ns = NULL; 2832 struct task_struct *task; 2833 struct seq_file *seq; 2834 int ret = -EINVAL; 2835 2836 task = get_proc_task(inode); 2837 if (task) { 2838 rcu_read_lock(); 2839 ns = get_user_ns(task_cred_xxx(task, user_ns)); 2840 rcu_read_unlock(); 2841 put_task_struct(task); 2842 } 2843 if (!ns) 2844 goto err; 2845 2846 ret = seq_open(file, seq_ops); 2847 if (ret) 2848 goto err_put_ns; 2849 2850 seq = file->private_data; 2851 seq->private = ns; 2852 2853 return 0; 2854 err_put_ns: 2855 put_user_ns(ns); 2856 err: 2857 return ret; 2858 } 2859 2860 static int proc_id_map_release(struct inode *inode, struct file *file) 2861 { 2862 struct seq_file *seq = file->private_data; 2863 struct user_namespace *ns = seq->private; 2864 put_user_ns(ns); 2865 return seq_release(inode, file); 2866 } 2867 2868 static int proc_uid_map_open(struct inode *inode, struct file *file) 2869 { 2870 return proc_id_map_open(inode, file, &proc_uid_seq_operations); 2871 } 2872 2873 static int proc_gid_map_open(struct inode *inode, struct file *file) 2874 { 2875 return proc_id_map_open(inode, file, &proc_gid_seq_operations); 2876 } 2877 2878 static int proc_projid_map_open(struct inode *inode, struct file *file) 2879 { 2880 return proc_id_map_open(inode, file, &proc_projid_seq_operations); 2881 } 2882 2883 static const struct file_operations proc_uid_map_operations = { 2884 .open = proc_uid_map_open, 2885 .write = proc_uid_map_write, 2886 .read = seq_read, 2887 .llseek = seq_lseek, 2888 .release = proc_id_map_release, 2889 }; 2890 2891 static const struct file_operations proc_gid_map_operations = { 2892 .open = proc_gid_map_open, 2893 .write = proc_gid_map_write, 2894 .read = seq_read, 2895 .llseek = seq_lseek, 2896 .release = proc_id_map_release, 2897 }; 2898 2899 static const struct file_operations proc_projid_map_operations = { 2900 .open = proc_projid_map_open, 2901 .write = proc_projid_map_write, 2902 .read = seq_read, 2903 .llseek = seq_lseek, 2904 .release = proc_id_map_release, 2905 }; 2906 2907 static int proc_setgroups_open(struct inode *inode, struct file *file) 2908 { 2909 struct user_namespace *ns = NULL; 2910 struct task_struct *task; 2911 int ret; 2912 2913 ret = -ESRCH; 2914 task = get_proc_task(inode); 2915 if (task) { 2916 rcu_read_lock(); 2917 ns = get_user_ns(task_cred_xxx(task, user_ns)); 2918 rcu_read_unlock(); 2919 put_task_struct(task); 2920 } 2921 if (!ns) 2922 goto err; 2923 2924 if (file->f_mode & FMODE_WRITE) { 2925 ret = -EACCES; 2926 if (!ns_capable(ns, CAP_SYS_ADMIN)) 2927 goto err_put_ns; 2928 } 2929 2930 ret = single_open(file, &proc_setgroups_show, ns); 2931 if (ret) 2932 goto err_put_ns; 2933 2934 return 0; 2935 err_put_ns: 2936 put_user_ns(ns); 2937 err: 2938 return ret; 2939 } 2940 2941 static int proc_setgroups_release(struct inode *inode, struct file *file) 2942 { 2943 struct seq_file *seq = file->private_data; 2944 struct user_namespace *ns = seq->private; 2945 int ret = single_release(inode, file); 2946 put_user_ns(ns); 2947 return ret; 2948 } 2949 2950 static const struct file_operations proc_setgroups_operations = { 2951 .open = proc_setgroups_open, 2952 .write = proc_setgroups_write, 2953 .read = seq_read, 2954 .llseek = seq_lseek, 2955 .release = proc_setgroups_release, 2956 }; 2957 #endif /* CONFIG_USER_NS */ 2958 2959 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 2960 struct pid *pid, struct task_struct *task) 2961 { 2962 int err = lock_trace(task); 2963 if (!err) { 2964 seq_printf(m, "%08x\n", task->personality); 2965 unlock_trace(task); 2966 } 2967 return err; 2968 } 2969 2970 #ifdef CONFIG_LIVEPATCH 2971 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, 2972 struct pid *pid, struct task_struct *task) 2973 { 2974 seq_printf(m, "%d\n", task->patch_state); 2975 return 0; 2976 } 2977 #endif /* CONFIG_LIVEPATCH */ 2978 2979 #ifdef CONFIG_STACKLEAK_METRICS 2980 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns, 2981 struct pid *pid, struct task_struct *task) 2982 { 2983 unsigned long prev_depth = THREAD_SIZE - 2984 (task->prev_lowest_stack & (THREAD_SIZE - 1)); 2985 unsigned long depth = THREAD_SIZE - 2986 (task->lowest_stack & (THREAD_SIZE - 1)); 2987 2988 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n", 2989 prev_depth, depth); 2990 return 0; 2991 } 2992 #endif /* CONFIG_STACKLEAK_METRICS */ 2993 2994 /* 2995 * Thread groups 2996 */ 2997 static const struct file_operations proc_task_operations; 2998 static const struct inode_operations proc_task_inode_operations; 2999 3000 static const struct pid_entry tgid_base_stuff[] = { 3001 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 3002 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 3003 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), 3004 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 3005 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 3006 #ifdef CONFIG_NET 3007 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 3008 #endif 3009 REG("environ", S_IRUSR, proc_environ_operations), 3010 REG("auxv", S_IRUSR, proc_auxv_operations), 3011 ONE("status", S_IRUGO, proc_pid_status), 3012 ONE("personality", S_IRUSR, proc_pid_personality), 3013 ONE("limits", S_IRUGO, proc_pid_limits), 3014 #ifdef CONFIG_SCHED_DEBUG 3015 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 3016 #endif 3017 #ifdef CONFIG_SCHED_AUTOGROUP 3018 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), 3019 #endif 3020 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 3021 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 3022 ONE("syscall", S_IRUSR, proc_pid_syscall), 3023 #endif 3024 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops), 3025 ONE("stat", S_IRUGO, proc_tgid_stat), 3026 ONE("statm", S_IRUGO, proc_pid_statm), 3027 REG("maps", S_IRUGO, proc_pid_maps_operations), 3028 #ifdef CONFIG_NUMA 3029 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), 3030 #endif 3031 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 3032 LNK("cwd", proc_cwd_link), 3033 LNK("root", proc_root_link), 3034 LNK("exe", proc_exe_link), 3035 REG("mounts", S_IRUGO, proc_mounts_operations), 3036 REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 3037 REG("mountstats", S_IRUSR, proc_mountstats_operations), 3038 #ifdef CONFIG_PROC_PAGE_MONITOR 3039 REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 3040 REG("smaps", S_IRUGO, proc_pid_smaps_operations), 3041 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), 3042 REG("pagemap", S_IRUSR, proc_pagemap_operations), 3043 #endif 3044 #ifdef CONFIG_SECURITY 3045 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 3046 #endif 3047 #ifdef CONFIG_KALLSYMS 3048 ONE("wchan", S_IRUGO, proc_pid_wchan), 3049 #endif 3050 #ifdef CONFIG_STACKTRACE 3051 ONE("stack", S_IRUSR, proc_pid_stack), 3052 #endif 3053 #ifdef CONFIG_SCHED_INFO 3054 ONE("schedstat", S_IRUGO, proc_pid_schedstat), 3055 #endif 3056 #ifdef CONFIG_LATENCYTOP 3057 REG("latency", S_IRUGO, proc_lstats_operations), 3058 #endif 3059 #ifdef CONFIG_PROC_PID_CPUSET 3060 ONE("cpuset", S_IRUGO, proc_cpuset_show), 3061 #endif 3062 #ifdef CONFIG_CGROUPS 3063 ONE("cgroup", S_IRUGO, proc_cgroup_show), 3064 #endif 3065 ONE("oom_score", S_IRUGO, proc_oom_score), 3066 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), 3067 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 3068 #ifdef CONFIG_AUDIT 3069 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 3070 REG("sessionid", S_IRUGO, proc_sessionid_operations), 3071 #endif 3072 #ifdef CONFIG_FAULT_INJECTION 3073 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 3074 REG("fail-nth", 0644, proc_fail_nth_operations), 3075 #endif 3076 #ifdef CONFIG_ELF_CORE 3077 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 3078 #endif 3079 #ifdef CONFIG_TASK_IO_ACCOUNTING 3080 ONE("io", S_IRUSR, proc_tgid_io_accounting), 3081 #endif 3082 #ifdef CONFIG_USER_NS 3083 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 3084 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 3085 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), 3086 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), 3087 #endif 3088 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS) 3089 REG("timers", S_IRUGO, proc_timers_operations), 3090 #endif 3091 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations), 3092 #ifdef CONFIG_LIVEPATCH 3093 ONE("patch_state", S_IRUSR, proc_pid_patch_state), 3094 #endif 3095 #ifdef CONFIG_STACKLEAK_METRICS 3096 ONE("stack_depth", S_IRUGO, proc_stack_depth), 3097 #endif 3098 }; 3099 3100 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) 3101 { 3102 return proc_pident_readdir(file, ctx, 3103 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 3104 } 3105 3106 static const struct file_operations proc_tgid_base_operations = { 3107 .read = generic_read_dir, 3108 .iterate_shared = proc_tgid_base_readdir, 3109 .llseek = generic_file_llseek, 3110 }; 3111 3112 struct pid *tgid_pidfd_to_pid(const struct file *file) 3113 { 3114 if (file->f_op != &proc_tgid_base_operations) 3115 return ERR_PTR(-EBADF); 3116 3117 return proc_pid(file_inode(file)); 3118 } 3119 3120 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 3121 { 3122 return proc_pident_lookup(dir, dentry, 3123 tgid_base_stuff, 3124 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff)); 3125 } 3126 3127 static const struct inode_operations proc_tgid_base_inode_operations = { 3128 .lookup = proc_tgid_base_lookup, 3129 .getattr = pid_getattr, 3130 .setattr = proc_setattr, 3131 .permission = proc_pid_permission, 3132 }; 3133 3134 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 3135 { 3136 struct dentry *dentry, *leader, *dir; 3137 char buf[10 + 1]; 3138 struct qstr name; 3139 3140 name.name = buf; 3141 name.len = snprintf(buf, sizeof(buf), "%u", pid); 3142 /* no ->d_hash() rejects on procfs */ 3143 dentry = d_hash_and_lookup(mnt->mnt_root, &name); 3144 if (dentry) { 3145 d_invalidate(dentry); 3146 dput(dentry); 3147 } 3148 3149 if (pid == tgid) 3150 return; 3151 3152 name.name = buf; 3153 name.len = snprintf(buf, sizeof(buf), "%u", tgid); 3154 leader = d_hash_and_lookup(mnt->mnt_root, &name); 3155 if (!leader) 3156 goto out; 3157 3158 name.name = "task"; 3159 name.len = strlen(name.name); 3160 dir = d_hash_and_lookup(leader, &name); 3161 if (!dir) 3162 goto out_put_leader; 3163 3164 name.name = buf; 3165 name.len = snprintf(buf, sizeof(buf), "%u", pid); 3166 dentry = d_hash_and_lookup(dir, &name); 3167 if (dentry) { 3168 d_invalidate(dentry); 3169 dput(dentry); 3170 } 3171 3172 dput(dir); 3173 out_put_leader: 3174 dput(leader); 3175 out: 3176 return; 3177 } 3178 3179 /** 3180 * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 3181 * @task: task that should be flushed. 3182 * 3183 * When flushing dentries from proc, one needs to flush them from global 3184 * proc (proc_mnt) and from all the namespaces' procs this task was seen 3185 * in. This call is supposed to do all of this job. 3186 * 3187 * Looks in the dcache for 3188 * /proc/@pid 3189 * /proc/@tgid/task/@pid 3190 * if either directory is present flushes it and all of it'ts children 3191 * from the dcache. 3192 * 3193 * It is safe and reasonable to cache /proc entries for a task until 3194 * that task exits. After that they just clog up the dcache with 3195 * useless entries, possibly causing useful dcache entries to be 3196 * flushed instead. This routine is proved to flush those useless 3197 * dcache entries at process exit time. 3198 * 3199 * NOTE: This routine is just an optimization so it does not guarantee 3200 * that no dcache entries will exist at process exit time it 3201 * just makes it very unlikely that any will persist. 3202 */ 3203 3204 void proc_flush_task(struct task_struct *task) 3205 { 3206 int i; 3207 struct pid *pid, *tgid; 3208 struct upid *upid; 3209 3210 pid = task_pid(task); 3211 tgid = task_tgid(task); 3212 3213 for (i = 0; i <= pid->level; i++) { 3214 upid = &pid->numbers[i]; 3215 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 3216 tgid->numbers[i].nr); 3217 } 3218 } 3219 3220 static struct dentry *proc_pid_instantiate(struct dentry * dentry, 3221 struct task_struct *task, const void *ptr) 3222 { 3223 struct inode *inode; 3224 3225 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO); 3226 if (!inode) 3227 return ERR_PTR(-ENOENT); 3228 3229 inode->i_op = &proc_tgid_base_inode_operations; 3230 inode->i_fop = &proc_tgid_base_operations; 3231 inode->i_flags|=S_IMMUTABLE; 3232 3233 set_nlink(inode, nlink_tgid); 3234 pid_update_inode(task, inode); 3235 3236 d_set_d_op(dentry, &pid_dentry_operations); 3237 return d_splice_alias(inode, dentry); 3238 } 3239 3240 struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags) 3241 { 3242 struct task_struct *task; 3243 unsigned tgid; 3244 struct pid_namespace *ns; 3245 struct dentry *result = ERR_PTR(-ENOENT); 3246 3247 tgid = name_to_int(&dentry->d_name); 3248 if (tgid == ~0U) 3249 goto out; 3250 3251 ns = dentry->d_sb->s_fs_info; 3252 rcu_read_lock(); 3253 task = find_task_by_pid_ns(tgid, ns); 3254 if (task) 3255 get_task_struct(task); 3256 rcu_read_unlock(); 3257 if (!task) 3258 goto out; 3259 3260 result = proc_pid_instantiate(dentry, task, NULL); 3261 put_task_struct(task); 3262 out: 3263 return result; 3264 } 3265 3266 /* 3267 * Find the first task with tgid >= tgid 3268 * 3269 */ 3270 struct tgid_iter { 3271 unsigned int tgid; 3272 struct task_struct *task; 3273 }; 3274 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 3275 { 3276 struct pid *pid; 3277 3278 if (iter.task) 3279 put_task_struct(iter.task); 3280 rcu_read_lock(); 3281 retry: 3282 iter.task = NULL; 3283 pid = find_ge_pid(iter.tgid, ns); 3284 if (pid) { 3285 iter.tgid = pid_nr_ns(pid, ns); 3286 iter.task = pid_task(pid, PIDTYPE_PID); 3287 /* What we to know is if the pid we have find is the 3288 * pid of a thread_group_leader. Testing for task 3289 * being a thread_group_leader is the obvious thing 3290 * todo but there is a window when it fails, due to 3291 * the pid transfer logic in de_thread. 3292 * 3293 * So we perform the straight forward test of seeing 3294 * if the pid we have found is the pid of a thread 3295 * group leader, and don't worry if the task we have 3296 * found doesn't happen to be a thread group leader. 3297 * As we don't care in the case of readdir. 3298 */ 3299 if (!iter.task || !has_group_leader_pid(iter.task)) { 3300 iter.tgid += 1; 3301 goto retry; 3302 } 3303 get_task_struct(iter.task); 3304 } 3305 rcu_read_unlock(); 3306 return iter; 3307 } 3308 3309 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2) 3310 3311 /* for the /proc/ directory itself, after non-process stuff has been done */ 3312 int proc_pid_readdir(struct file *file, struct dir_context *ctx) 3313 { 3314 struct tgid_iter iter; 3315 struct pid_namespace *ns = proc_pid_ns(file_inode(file)); 3316 loff_t pos = ctx->pos; 3317 3318 if (pos >= PID_MAX_LIMIT + TGID_OFFSET) 3319 return 0; 3320 3321 if (pos == TGID_OFFSET - 2) { 3322 struct inode *inode = d_inode(ns->proc_self); 3323 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK)) 3324 return 0; 3325 ctx->pos = pos = pos + 1; 3326 } 3327 if (pos == TGID_OFFSET - 1) { 3328 struct inode *inode = d_inode(ns->proc_thread_self); 3329 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK)) 3330 return 0; 3331 ctx->pos = pos = pos + 1; 3332 } 3333 iter.tgid = pos - TGID_OFFSET; 3334 iter.task = NULL; 3335 for (iter = next_tgid(ns, iter); 3336 iter.task; 3337 iter.tgid += 1, iter = next_tgid(ns, iter)) { 3338 char name[10 + 1]; 3339 unsigned int len; 3340 3341 cond_resched(); 3342 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE)) 3343 continue; 3344 3345 len = snprintf(name, sizeof(name), "%u", iter.tgid); 3346 ctx->pos = iter.tgid + TGID_OFFSET; 3347 if (!proc_fill_cache(file, ctx, name, len, 3348 proc_pid_instantiate, iter.task, NULL)) { 3349 put_task_struct(iter.task); 3350 return 0; 3351 } 3352 } 3353 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET; 3354 return 0; 3355 } 3356 3357 /* 3358 * proc_tid_comm_permission is a special permission function exclusively 3359 * used for the node /proc/<pid>/task/<tid>/comm. 3360 * It bypasses generic permission checks in the case where a task of the same 3361 * task group attempts to access the node. 3362 * The rationale behind this is that glibc and bionic access this node for 3363 * cross thread naming (pthread_set/getname_np(!self)). However, if 3364 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0, 3365 * which locks out the cross thread naming implementation. 3366 * This function makes sure that the node is always accessible for members of 3367 * same thread group. 3368 */ 3369 static int proc_tid_comm_permission(struct inode *inode, int mask) 3370 { 3371 bool is_same_tgroup; 3372 struct task_struct *task; 3373 3374 task = get_proc_task(inode); 3375 if (!task) 3376 return -ESRCH; 3377 is_same_tgroup = same_thread_group(current, task); 3378 put_task_struct(task); 3379 3380 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) { 3381 /* This file (/proc/<pid>/task/<tid>/comm) can always be 3382 * read or written by the members of the corresponding 3383 * thread group. 3384 */ 3385 return 0; 3386 } 3387 3388 return generic_permission(inode, mask); 3389 } 3390 3391 static const struct inode_operations proc_tid_comm_inode_operations = { 3392 .permission = proc_tid_comm_permission, 3393 }; 3394 3395 /* 3396 * Tasks 3397 */ 3398 static const struct pid_entry tid_base_stuff[] = { 3399 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 3400 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 3401 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 3402 #ifdef CONFIG_NET 3403 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 3404 #endif 3405 REG("environ", S_IRUSR, proc_environ_operations), 3406 REG("auxv", S_IRUSR, proc_auxv_operations), 3407 ONE("status", S_IRUGO, proc_pid_status), 3408 ONE("personality", S_IRUSR, proc_pid_personality), 3409 ONE("limits", S_IRUGO, proc_pid_limits), 3410 #ifdef CONFIG_SCHED_DEBUG 3411 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 3412 #endif 3413 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR, 3414 &proc_tid_comm_inode_operations, 3415 &proc_pid_set_comm_operations, {}), 3416 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 3417 ONE("syscall", S_IRUSR, proc_pid_syscall), 3418 #endif 3419 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops), 3420 ONE("stat", S_IRUGO, proc_tid_stat), 3421 ONE("statm", S_IRUGO, proc_pid_statm), 3422 REG("maps", S_IRUGO, proc_pid_maps_operations), 3423 #ifdef CONFIG_PROC_CHILDREN 3424 REG("children", S_IRUGO, proc_tid_children_operations), 3425 #endif 3426 #ifdef CONFIG_NUMA 3427 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), 3428 #endif 3429 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 3430 LNK("cwd", proc_cwd_link), 3431 LNK("root", proc_root_link), 3432 LNK("exe", proc_exe_link), 3433 REG("mounts", S_IRUGO, proc_mounts_operations), 3434 REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 3435 #ifdef CONFIG_PROC_PAGE_MONITOR 3436 REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 3437 REG("smaps", S_IRUGO, proc_pid_smaps_operations), 3438 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), 3439 REG("pagemap", S_IRUSR, proc_pagemap_operations), 3440 #endif 3441 #ifdef CONFIG_SECURITY 3442 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 3443 #endif 3444 #ifdef CONFIG_KALLSYMS 3445 ONE("wchan", S_IRUGO, proc_pid_wchan), 3446 #endif 3447 #ifdef CONFIG_STACKTRACE 3448 ONE("stack", S_IRUSR, proc_pid_stack), 3449 #endif 3450 #ifdef CONFIG_SCHED_INFO 3451 ONE("schedstat", S_IRUGO, proc_pid_schedstat), 3452 #endif 3453 #ifdef CONFIG_LATENCYTOP 3454 REG("latency", S_IRUGO, proc_lstats_operations), 3455 #endif 3456 #ifdef CONFIG_PROC_PID_CPUSET 3457 ONE("cpuset", S_IRUGO, proc_cpuset_show), 3458 #endif 3459 #ifdef CONFIG_CGROUPS 3460 ONE("cgroup", S_IRUGO, proc_cgroup_show), 3461 #endif 3462 ONE("oom_score", S_IRUGO, proc_oom_score), 3463 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), 3464 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 3465 #ifdef CONFIG_AUDIT 3466 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 3467 REG("sessionid", S_IRUGO, proc_sessionid_operations), 3468 #endif 3469 #ifdef CONFIG_FAULT_INJECTION 3470 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 3471 REG("fail-nth", 0644, proc_fail_nth_operations), 3472 #endif 3473 #ifdef CONFIG_TASK_IO_ACCOUNTING 3474 ONE("io", S_IRUSR, proc_tid_io_accounting), 3475 #endif 3476 #ifdef CONFIG_USER_NS 3477 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 3478 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 3479 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), 3480 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), 3481 #endif 3482 #ifdef CONFIG_LIVEPATCH 3483 ONE("patch_state", S_IRUSR, proc_pid_patch_state), 3484 #endif 3485 }; 3486 3487 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx) 3488 { 3489 return proc_pident_readdir(file, ctx, 3490 tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 3491 } 3492 3493 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 3494 { 3495 return proc_pident_lookup(dir, dentry, 3496 tid_base_stuff, 3497 tid_base_stuff + ARRAY_SIZE(tid_base_stuff)); 3498 } 3499 3500 static const struct file_operations proc_tid_base_operations = { 3501 .read = generic_read_dir, 3502 .iterate_shared = proc_tid_base_readdir, 3503 .llseek = generic_file_llseek, 3504 }; 3505 3506 static const struct inode_operations proc_tid_base_inode_operations = { 3507 .lookup = proc_tid_base_lookup, 3508 .getattr = pid_getattr, 3509 .setattr = proc_setattr, 3510 }; 3511 3512 static struct dentry *proc_task_instantiate(struct dentry *dentry, 3513 struct task_struct *task, const void *ptr) 3514 { 3515 struct inode *inode; 3516 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO); 3517 if (!inode) 3518 return ERR_PTR(-ENOENT); 3519 3520 inode->i_op = &proc_tid_base_inode_operations; 3521 inode->i_fop = &proc_tid_base_operations; 3522 inode->i_flags |= S_IMMUTABLE; 3523 3524 set_nlink(inode, nlink_tid); 3525 pid_update_inode(task, inode); 3526 3527 d_set_d_op(dentry, &pid_dentry_operations); 3528 return d_splice_alias(inode, dentry); 3529 } 3530 3531 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 3532 { 3533 struct task_struct *task; 3534 struct task_struct *leader = get_proc_task(dir); 3535 unsigned tid; 3536 struct pid_namespace *ns; 3537 struct dentry *result = ERR_PTR(-ENOENT); 3538 3539 if (!leader) 3540 goto out_no_task; 3541 3542 tid = name_to_int(&dentry->d_name); 3543 if (tid == ~0U) 3544 goto out; 3545 3546 ns = dentry->d_sb->s_fs_info; 3547 rcu_read_lock(); 3548 task = find_task_by_pid_ns(tid, ns); 3549 if (task) 3550 get_task_struct(task); 3551 rcu_read_unlock(); 3552 if (!task) 3553 goto out; 3554 if (!same_thread_group(leader, task)) 3555 goto out_drop_task; 3556 3557 result = proc_task_instantiate(dentry, task, NULL); 3558 out_drop_task: 3559 put_task_struct(task); 3560 out: 3561 put_task_struct(leader); 3562 out_no_task: 3563 return result; 3564 } 3565 3566 /* 3567 * Find the first tid of a thread group to return to user space. 3568 * 3569 * Usually this is just the thread group leader, but if the users 3570 * buffer was too small or there was a seek into the middle of the 3571 * directory we have more work todo. 3572 * 3573 * In the case of a short read we start with find_task_by_pid. 3574 * 3575 * In the case of a seek we start with the leader and walk nr 3576 * threads past it. 3577 */ 3578 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos, 3579 struct pid_namespace *ns) 3580 { 3581 struct task_struct *pos, *task; 3582 unsigned long nr = f_pos; 3583 3584 if (nr != f_pos) /* 32bit overflow? */ 3585 return NULL; 3586 3587 rcu_read_lock(); 3588 task = pid_task(pid, PIDTYPE_PID); 3589 if (!task) 3590 goto fail; 3591 3592 /* Attempt to start with the tid of a thread */ 3593 if (tid && nr) { 3594 pos = find_task_by_pid_ns(tid, ns); 3595 if (pos && same_thread_group(pos, task)) 3596 goto found; 3597 } 3598 3599 /* If nr exceeds the number of threads there is nothing todo */ 3600 if (nr >= get_nr_threads(task)) 3601 goto fail; 3602 3603 /* If we haven't found our starting place yet start 3604 * with the leader and walk nr threads forward. 3605 */ 3606 pos = task = task->group_leader; 3607 do { 3608 if (!nr--) 3609 goto found; 3610 } while_each_thread(task, pos); 3611 fail: 3612 pos = NULL; 3613 goto out; 3614 found: 3615 get_task_struct(pos); 3616 out: 3617 rcu_read_unlock(); 3618 return pos; 3619 } 3620 3621 /* 3622 * Find the next thread in the thread list. 3623 * Return NULL if there is an error or no next thread. 3624 * 3625 * The reference to the input task_struct is released. 3626 */ 3627 static struct task_struct *next_tid(struct task_struct *start) 3628 { 3629 struct task_struct *pos = NULL; 3630 rcu_read_lock(); 3631 if (pid_alive(start)) { 3632 pos = next_thread(start); 3633 if (thread_group_leader(pos)) 3634 pos = NULL; 3635 else 3636 get_task_struct(pos); 3637 } 3638 rcu_read_unlock(); 3639 put_task_struct(start); 3640 return pos; 3641 } 3642 3643 /* for the /proc/TGID/task/ directories */ 3644 static int proc_task_readdir(struct file *file, struct dir_context *ctx) 3645 { 3646 struct inode *inode = file_inode(file); 3647 struct task_struct *task; 3648 struct pid_namespace *ns; 3649 int tid; 3650 3651 if (proc_inode_is_dead(inode)) 3652 return -ENOENT; 3653 3654 if (!dir_emit_dots(file, ctx)) 3655 return 0; 3656 3657 /* f_version caches the tgid value that the last readdir call couldn't 3658 * return. lseek aka telldir automagically resets f_version to 0. 3659 */ 3660 ns = proc_pid_ns(inode); 3661 tid = (int)file->f_version; 3662 file->f_version = 0; 3663 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns); 3664 task; 3665 task = next_tid(task), ctx->pos++) { 3666 char name[10 + 1]; 3667 unsigned int len; 3668 tid = task_pid_nr_ns(task, ns); 3669 len = snprintf(name, sizeof(name), "%u", tid); 3670 if (!proc_fill_cache(file, ctx, name, len, 3671 proc_task_instantiate, task, NULL)) { 3672 /* returning this tgid failed, save it as the first 3673 * pid for the next readir call */ 3674 file->f_version = (u64)tid; 3675 put_task_struct(task); 3676 break; 3677 } 3678 } 3679 3680 return 0; 3681 } 3682 3683 static int proc_task_getattr(const struct path *path, struct kstat *stat, 3684 u32 request_mask, unsigned int query_flags) 3685 { 3686 struct inode *inode = d_inode(path->dentry); 3687 struct task_struct *p = get_proc_task(inode); 3688 generic_fillattr(inode, stat); 3689 3690 if (p) { 3691 stat->nlink += get_nr_threads(p); 3692 put_task_struct(p); 3693 } 3694 3695 return 0; 3696 } 3697 3698 static const struct inode_operations proc_task_inode_operations = { 3699 .lookup = proc_task_lookup, 3700 .getattr = proc_task_getattr, 3701 .setattr = proc_setattr, 3702 .permission = proc_pid_permission, 3703 }; 3704 3705 static const struct file_operations proc_task_operations = { 3706 .read = generic_read_dir, 3707 .iterate_shared = proc_task_readdir, 3708 .llseek = generic_file_llseek, 3709 }; 3710 3711 void __init set_proc_pid_nlink(void) 3712 { 3713 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 3714 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 3715 } 3716
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.