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