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