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