1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * proc base directory handling functions
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.
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>
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
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.
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/generic-radix-tree.h>
63 #include <linux/string.h>
64 #include <linux/seq_file.h>
65 #include <linux/namei.h>
66 #include <linux/mnt_namespace.h>
68 #include <linux/swap.h>
69 #include <linux/rcupdate.h>
70 #include <linux/kallsyms.h>
71 #include <linux/stacktrace.h>
72 #include <linux/resource.h>
73 #include <linux/module.h>
74 #include <linux/mount.h>
75 #include <linux/security.h>
76 #include <linux/ptrace.h>
77 #include <linux/tracehook.h>
78 #include <linux/printk.h>
79 #include <linux/cache.h>
80 #include <linux/cgroup.h>
81 #include <linux/cpuset.h>
82 #include <linux/audit.h>
83 #include <linux/poll.h>
84 #include <linux/nsproxy.h>
85 #include <linux/oom.h>
86 #include <linux/elf.h>
87 #include <linux/pid_namespace.h>
88 #include <linux/user_namespace.h>
89 #include <linux/fs_struct.h>
90 #include <linux/slab.h>
91 #include <linux/sched/autogroup.h>
92 #include <linux/sched/mm.h>
93 #include <linux/sched/coredump.h>
94 #include <linux/sched/debug.h>
95 #include <linux/sched/stat.h>
96 #include <linux/posix-timers.h>
97 #include <linux/time_namespace.h>
98 #include <linux/resctrl.h>
99 #include <trace/events/oom.h>
100 #include "internal.h"
103 #include "../../lib/kstrtox.h"
106 * Implementing inode permission operations in /proc is almost
107 * certainly an error. Permission checks need to happen during
108 * each system call not at open time. The reason is that most of
109 * what we wish to check for permissions in /proc varies at runtime.
111 * The classic example of a problem is opening file descriptors
112 * in /proc for a task before it execs a suid executable.
115 static u8 nlink_tid __ro_after_init
;
116 static u8 nlink_tgid __ro_after_init
;
122 const struct inode_operations
*iop
;
123 const struct file_operations
*fop
;
127 #define NOD(NAME, MODE, IOP, FOP, OP) { \
129 .len = sizeof(NAME) - 1, \
136 #define DIR(NAME, MODE, iops, fops) \
137 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
138 #define LNK(NAME, get_link) \
139 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
140 &proc_pid_link_inode_operations, NULL, \
141 { .proc_get_link = get_link } )
142 #define REG(NAME, MODE, fops) \
143 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
144 #define ONE(NAME, MODE, show) \
145 NOD(NAME, (S_IFREG|(MODE)), \
146 NULL, &proc_single_file_operations, \
147 { .proc_show = show } )
148 #define ATTR(LSM, NAME, MODE) \
149 NOD(NAME, (S_IFREG|(MODE)), \
150 NULL, &proc_pid_attr_operations, \
154 * Count the number of hardlinks for the pid_entry table, excluding the .
157 static unsigned int __init
pid_entry_nlink(const struct pid_entry
*entries
,
164 for (i
= 0; i
< n
; ++i
) {
165 if (S_ISDIR(entries
[i
].mode
))
172 static int get_task_root(struct task_struct
*task
, struct path
*root
)
174 int result
= -ENOENT
;
178 get_fs_root(task
->fs
, root
);
185 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
187 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
188 int result
= -ENOENT
;
193 get_fs_pwd(task
->fs
, path
);
197 put_task_struct(task
);
202 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
204 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
205 int result
= -ENOENT
;
208 result
= get_task_root(task
, path
);
209 put_task_struct(task
);
215 * If the user used setproctitle(), we just get the string from
216 * user space at arg_start, and limit it to a maximum of one page.
218 static ssize_t
get_mm_proctitle(struct mm_struct
*mm
, char __user
*buf
,
219 size_t count
, unsigned long pos
,
220 unsigned long arg_start
)
225 if (pos
>= PAGE_SIZE
)
228 page
= (char *)__get_free_page(GFP_KERNEL
);
233 got
= access_remote_vm(mm
, arg_start
, page
, PAGE_SIZE
, FOLL_ANON
);
235 int len
= strnlen(page
, got
);
237 /* Include the NUL character if it was found */
245 len
-= copy_to_user(buf
, page
+pos
, len
);
251 free_page((unsigned long)page
);
255 static ssize_t
get_mm_cmdline(struct mm_struct
*mm
, char __user
*buf
,
256 size_t count
, loff_t
*ppos
)
258 unsigned long arg_start
, arg_end
, env_start
, env_end
;
259 unsigned long pos
, len
;
262 /* Check if process spawned far enough to have cmdline. */
266 spin_lock(&mm
->arg_lock
);
267 arg_start
= mm
->arg_start
;
268 arg_end
= mm
->arg_end
;
269 env_start
= mm
->env_start
;
270 env_end
= mm
->env_end
;
271 spin_unlock(&mm
->arg_lock
);
273 if (arg_start
>= arg_end
)
277 * We allow setproctitle() to overwrite the argument
278 * strings, and overflow past the original end. But
279 * only when it overflows into the environment area.
281 if (env_start
!= arg_end
|| env_end
< env_start
)
282 env_start
= env_end
= arg_end
;
283 len
= env_end
- arg_start
;
285 /* We're not going to care if "*ppos" has high bits set */
289 if (count
> len
- pos
)
295 * Magical special case: if the argv[] end byte is not
296 * zero, the user has overwritten it with setproctitle(3).
298 * Possible future enhancement: do this only once when
299 * pos is 0, and set a flag in the 'struct file'.
301 if (access_remote_vm(mm
, arg_end
-1, &c
, 1, FOLL_ANON
) == 1 && c
)
302 return get_mm_proctitle(mm
, buf
, count
, pos
, arg_start
);
305 * For the non-setproctitle() case we limit things strictly
306 * to the [arg_start, arg_end[ range.
309 if (pos
< arg_start
|| pos
>= arg_end
)
311 if (count
> arg_end
- pos
)
312 count
= arg_end
- pos
;
314 page
= (char *)__get_free_page(GFP_KERNEL
);
321 size_t size
= min_t(size_t, PAGE_SIZE
, count
);
323 got
= access_remote_vm(mm
, pos
, page
, size
, FOLL_ANON
);
326 got
-= copy_to_user(buf
, page
, got
);
327 if (unlikely(!got
)) {
338 free_page((unsigned long)page
);
342 static ssize_t
get_task_cmdline(struct task_struct
*tsk
, char __user
*buf
,
343 size_t count
, loff_t
*pos
)
345 struct mm_struct
*mm
;
348 mm
= get_task_mm(tsk
);
352 ret
= get_mm_cmdline(mm
, buf
, count
, pos
);
357 static ssize_t
proc_pid_cmdline_read(struct file
*file
, char __user
*buf
,
358 size_t count
, loff_t
*pos
)
360 struct task_struct
*tsk
;
365 tsk
= get_proc_task(file_inode(file
));
368 ret
= get_task_cmdline(tsk
, buf
, count
, pos
);
369 put_task_struct(tsk
);
375 static const struct file_operations proc_pid_cmdline_ops
= {
376 .read
= proc_pid_cmdline_read
,
377 .llseek
= generic_file_llseek
,
380 #ifdef CONFIG_KALLSYMS
382 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
383 * Returns the resolved symbol. If that fails, simply return the address.
385 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
386 struct pid
*pid
, struct task_struct
*task
)
389 char symname
[KSYM_NAME_LEN
];
391 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
394 wchan
= get_wchan(task
);
395 if (wchan
&& !lookup_symbol_name(wchan
, symname
)) {
396 seq_puts(m
, symname
);
404 #endif /* CONFIG_KALLSYMS */
406 static int lock_trace(struct task_struct
*task
)
408 int err
= mutex_lock_killable(&task
->signal
->exec_update_mutex
);
411 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
412 mutex_unlock(&task
->signal
->exec_update_mutex
);
418 static void unlock_trace(struct task_struct
*task
)
420 mutex_unlock(&task
->signal
->exec_update_mutex
);
423 #ifdef CONFIG_STACKTRACE
425 #define MAX_STACK_TRACE_DEPTH 64
427 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
428 struct pid
*pid
, struct task_struct
*task
)
430 unsigned long *entries
;
434 * The ability to racily run the kernel stack unwinder on a running task
435 * and then observe the unwinder output is scary; while it is useful for
436 * debugging kernel issues, it can also allow an attacker to leak kernel
438 * Doing this in a manner that is at least safe from races would require
439 * some work to ensure that the remote task can not be scheduled; and
440 * even then, this would still expose the unwinder as local attack
442 * Therefore, this interface is restricted to root.
444 if (!file_ns_capable(m
->file
, &init_user_ns
, CAP_SYS_ADMIN
))
447 entries
= kmalloc_array(MAX_STACK_TRACE_DEPTH
, sizeof(*entries
),
452 err
= lock_trace(task
);
454 unsigned int i
, nr_entries
;
456 nr_entries
= stack_trace_save_tsk(task
, entries
,
457 MAX_STACK_TRACE_DEPTH
, 0);
459 for (i
= 0; i
< nr_entries
; i
++) {
460 seq_printf(m
, "[<0>] %pB\n", (void *)entries
[i
]);
471 #ifdef CONFIG_SCHED_INFO
473 * Provides /proc/PID/schedstat
475 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
476 struct pid
*pid
, struct task_struct
*task
)
478 if (unlikely(!sched_info_on()))
479 seq_puts(m
, "0 0 0\n");
481 seq_printf(m
, "%llu %llu %lu\n",
482 (unsigned long long)task
->se
.sum_exec_runtime
,
483 (unsigned long long)task
->sched_info
.run_delay
,
484 task
->sched_info
.pcount
);
490 #ifdef CONFIG_LATENCYTOP
491 static int lstats_show_proc(struct seq_file
*m
, void *v
)
494 struct inode
*inode
= m
->private;
495 struct task_struct
*task
= get_proc_task(inode
);
499 seq_puts(m
, "Latency Top version : v0.1\n");
500 for (i
= 0; i
< LT_SAVECOUNT
; i
++) {
501 struct latency_record
*lr
= &task
->latency_record
[i
];
502 if (lr
->backtrace
[0]) {
504 seq_printf(m
, "%i %li %li",
505 lr
->count
, lr
->time
, lr
->max
);
506 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
507 unsigned long bt
= lr
->backtrace
[q
];
511 seq_printf(m
, " %ps", (void *)bt
);
517 put_task_struct(task
);
521 static int lstats_open(struct inode
*inode
, struct file
*file
)
523 return single_open(file
, lstats_show_proc
, inode
);
526 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
527 size_t count
, loff_t
*offs
)
529 struct task_struct
*task
= get_proc_task(file_inode(file
));
533 clear_tsk_latency_tracing(task
);
534 put_task_struct(task
);
539 static const struct file_operations proc_lstats_operations
= {
542 .write
= lstats_write
,
544 .release
= single_release
,
549 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
550 struct pid
*pid
, struct task_struct
*task
)
552 unsigned long totalpages
= totalram_pages() + total_swap_pages
;
553 unsigned long points
= 0;
555 points
= oom_badness(task
, totalpages
) * 1000 / totalpages
;
556 seq_printf(m
, "%lu\n", points
);
566 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
567 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
568 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
569 [RLIMIT_DATA
] = {"Max data size", "bytes"},
570 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
571 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
572 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
573 [RLIMIT_NPROC
] = {"Max processes", "processes"},
574 [RLIMIT_NOFILE
] = {"Max open files", "files"},
575 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
576 [RLIMIT_AS
] = {"Max address space", "bytes"},
577 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
578 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
579 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
580 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
581 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
582 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
585 /* Display limits for a process */
586 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
587 struct pid
*pid
, struct task_struct
*task
)
592 struct rlimit rlim
[RLIM_NLIMITS
];
594 if (!lock_task_sighand(task
, &flags
))
596 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
597 unlock_task_sighand(task
, &flags
);
600 * print the file header
607 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
608 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
609 seq_printf(m
, "%-25s %-20s ",
610 lnames
[i
].name
, "unlimited");
612 seq_printf(m
, "%-25s %-20lu ",
613 lnames
[i
].name
, rlim
[i
].rlim_cur
);
615 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
616 seq_printf(m
, "%-20s ", "unlimited");
618 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
621 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
629 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
630 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
631 struct pid
*pid
, struct task_struct
*task
)
633 struct syscall_info info
;
634 u64
*args
= &info
.data
.args
[0];
637 res
= lock_trace(task
);
641 if (task_current_syscall(task
, &info
))
642 seq_puts(m
, "running\n");
643 else if (info
.data
.nr
< 0)
644 seq_printf(m
, "%d 0x%llx 0x%llx\n",
645 info
.data
.nr
, info
.sp
, info
.data
.instruction_pointer
);
648 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
650 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
651 info
.sp
, info
.data
.instruction_pointer
);
656 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
658 /************************************************************************/
659 /* Here the fs part begins */
660 /************************************************************************/
662 /* permission checks */
663 static int proc_fd_access_allowed(struct inode
*inode
)
665 struct task_struct
*task
;
667 /* Allow access to a task's file descriptors if it is us or we
668 * may use ptrace attach to the process and find out that
671 task
= get_proc_task(inode
);
673 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
674 put_task_struct(task
);
679 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
682 struct inode
*inode
= d_inode(dentry
);
684 if (attr
->ia_valid
& ATTR_MODE
)
687 error
= setattr_prepare(dentry
, attr
);
691 setattr_copy(inode
, attr
);
692 mark_inode_dirty(inode
);
697 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
698 * or euid/egid (for hide_pid_min=2)?
700 static bool has_pid_permissions(struct pid_namespace
*pid
,
701 struct task_struct
*task
,
704 if (pid
->hide_pid
< hide_pid_min
)
706 if (in_group_p(pid
->pid_gid
))
708 return ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
712 static int proc_pid_permission(struct inode
*inode
, int mask
)
714 struct pid_namespace
*pid
= proc_pid_ns(inode
);
715 struct task_struct
*task
;
718 task
= get_proc_task(inode
);
721 has_perms
= has_pid_permissions(pid
, task
, HIDEPID_NO_ACCESS
);
722 put_task_struct(task
);
725 if (pid
->hide_pid
== HIDEPID_INVISIBLE
) {
727 * Let's make getdents(), stat(), and open()
728 * consistent with each other. If a process
729 * may not stat() a file, it shouldn't be seen
737 return generic_permission(inode
, mask
);
742 static const struct inode_operations proc_def_inode_operations
= {
743 .setattr
= proc_setattr
,
746 static int proc_single_show(struct seq_file
*m
, void *v
)
748 struct inode
*inode
= m
->private;
749 struct pid_namespace
*ns
= proc_pid_ns(inode
);
750 struct pid
*pid
= proc_pid(inode
);
751 struct task_struct
*task
;
754 task
= get_pid_task(pid
, PIDTYPE_PID
);
758 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
760 put_task_struct(task
);
764 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
766 return single_open(filp
, proc_single_show
, inode
);
769 static const struct file_operations proc_single_file_operations
= {
770 .open
= proc_single_open
,
773 .release
= single_release
,
777 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
779 struct task_struct
*task
= get_proc_task(inode
);
780 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
783 mm
= mm_access(task
, mode
| PTRACE_MODE_FSCREDS
);
784 put_task_struct(task
);
786 if (!IS_ERR_OR_NULL(mm
)) {
787 /* ensure this mm_struct can't be freed */
789 /* but do not pin its memory */
797 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
799 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
804 file
->private_data
= mm
;
808 static int mem_open(struct inode
*inode
, struct file
*file
)
810 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
812 /* OK to pass negative loff_t, we can catch out-of-range */
813 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
818 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
819 size_t count
, loff_t
*ppos
, int write
)
821 struct mm_struct
*mm
= file
->private_data
;
822 unsigned long addr
= *ppos
;
830 page
= (char *)__get_free_page(GFP_KERNEL
);
835 if (!mmget_not_zero(mm
))
838 flags
= FOLL_FORCE
| (write
? FOLL_WRITE
: 0);
841 int this_len
= min_t(int, count
, PAGE_SIZE
);
843 if (write
&& copy_from_user(page
, buf
, this_len
)) {
848 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, flags
);
855 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
869 free_page((unsigned long) page
);
873 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
874 size_t count
, loff_t
*ppos
)
876 return mem_rw(file
, buf
, count
, ppos
, 0);
879 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
880 size_t count
, loff_t
*ppos
)
882 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
885 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
889 file
->f_pos
= offset
;
892 file
->f_pos
+= offset
;
897 force_successful_syscall_return();
901 static int mem_release(struct inode
*inode
, struct file
*file
)
903 struct mm_struct
*mm
= file
->private_data
;
909 static const struct file_operations proc_mem_operations
= {
914 .release
= mem_release
,
917 static int environ_open(struct inode
*inode
, struct file
*file
)
919 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
922 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
923 size_t count
, loff_t
*ppos
)
926 unsigned long src
= *ppos
;
928 struct mm_struct
*mm
= file
->private_data
;
929 unsigned long env_start
, env_end
;
931 /* Ensure the process spawned far enough to have an environment. */
932 if (!mm
|| !mm
->env_end
)
935 page
= (char *)__get_free_page(GFP_KERNEL
);
940 if (!mmget_not_zero(mm
))
943 spin_lock(&mm
->arg_lock
);
944 env_start
= mm
->env_start
;
945 env_end
= mm
->env_end
;
946 spin_unlock(&mm
->arg_lock
);
949 size_t this_len
, max_len
;
952 if (src
>= (env_end
- env_start
))
955 this_len
= env_end
- (env_start
+ src
);
957 max_len
= min_t(size_t, PAGE_SIZE
, count
);
958 this_len
= min(max_len
, this_len
);
960 retval
= access_remote_vm(mm
, (env_start
+ src
), page
, this_len
, FOLL_ANON
);
967 if (copy_to_user(buf
, page
, retval
)) {
981 free_page((unsigned long) page
);
985 static const struct file_operations proc_environ_operations
= {
986 .open
= environ_open
,
987 .read
= environ_read
,
988 .llseek
= generic_file_llseek
,
989 .release
= mem_release
,
992 static int auxv_open(struct inode
*inode
, struct file
*file
)
994 return __mem_open(inode
, file
, PTRACE_MODE_READ_FSCREDS
);
997 static ssize_t
auxv_read(struct file
*file
, char __user
*buf
,
998 size_t count
, loff_t
*ppos
)
1000 struct mm_struct
*mm
= file
->private_data
;
1001 unsigned int nwords
= 0;
1007 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
1008 return simple_read_from_buffer(buf
, count
, ppos
, mm
->saved_auxv
,
1009 nwords
* sizeof(mm
->saved_auxv
[0]));
1012 static const struct file_operations proc_auxv_operations
= {
1015 .llseek
= generic_file_llseek
,
1016 .release
= mem_release
,
1019 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
1022 struct task_struct
*task
= get_proc_task(file_inode(file
));
1023 char buffer
[PROC_NUMBUF
];
1024 int oom_adj
= OOM_ADJUST_MIN
;
1029 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
1030 oom_adj
= OOM_ADJUST_MAX
;
1032 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
1034 put_task_struct(task
);
1035 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
1036 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1039 static int __set_oom_adj(struct file
*file
, int oom_adj
, bool legacy
)
1041 static DEFINE_MUTEX(oom_adj_mutex
);
1042 struct mm_struct
*mm
= NULL
;
1043 struct task_struct
*task
;
1046 task
= get_proc_task(file_inode(file
));
1050 mutex_lock(&oom_adj_mutex
);
1052 if (oom_adj
< task
->signal
->oom_score_adj
&&
1053 !capable(CAP_SYS_RESOURCE
)) {
1058 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1059 * /proc/pid/oom_score_adj instead.
1061 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1062 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1065 if ((short)oom_adj
< task
->signal
->oom_score_adj_min
&&
1066 !capable(CAP_SYS_RESOURCE
)) {
1073 * Make sure we will check other processes sharing the mm if this is
1074 * not vfrok which wants its own oom_score_adj.
1075 * pin the mm so it doesn't go away and get reused after task_unlock
1077 if (!task
->vfork_done
) {
1078 struct task_struct
*p
= find_lock_task_mm(task
);
1081 if (atomic_read(&p
->mm
->mm_users
) > 1) {
1089 task
->signal
->oom_score_adj
= oom_adj
;
1090 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1091 task
->signal
->oom_score_adj_min
= (short)oom_adj
;
1092 trace_oom_score_adj_update(task
);
1095 struct task_struct
*p
;
1098 for_each_process(p
) {
1099 if (same_thread_group(task
, p
))
1102 /* do not touch kernel threads or the global init */
1103 if (p
->flags
& PF_KTHREAD
|| is_global_init(p
))
1107 if (!p
->vfork_done
&& process_shares_mm(p
, mm
)) {
1108 p
->signal
->oom_score_adj
= oom_adj
;
1109 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1110 p
->signal
->oom_score_adj_min
= (short)oom_adj
;
1118 mutex_unlock(&oom_adj_mutex
);
1119 put_task_struct(task
);
1124 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1125 * kernels. The effective policy is defined by oom_score_adj, which has a
1126 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1127 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1128 * Processes that become oom disabled via oom_adj will still be oom disabled
1129 * with this implementation.
1131 * oom_adj cannot be removed since existing userspace binaries use it.
1133 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
1134 size_t count
, loff_t
*ppos
)
1136 char buffer
[PROC_NUMBUF
];
1140 memset(buffer
, 0, sizeof(buffer
));
1141 if (count
> sizeof(buffer
) - 1)
1142 count
= sizeof(buffer
) - 1;
1143 if (copy_from_user(buffer
, buf
, count
)) {
1148 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
1151 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
1152 oom_adj
!= OOM_DISABLE
) {
1158 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1159 * value is always attainable.
1161 if (oom_adj
== OOM_ADJUST_MAX
)
1162 oom_adj
= OOM_SCORE_ADJ_MAX
;
1164 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
1166 err
= __set_oom_adj(file
, oom_adj
, true);
1168 return err
< 0 ? err
: count
;
1171 static const struct file_operations proc_oom_adj_operations
= {
1172 .read
= oom_adj_read
,
1173 .write
= oom_adj_write
,
1174 .llseek
= generic_file_llseek
,
1177 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1178 size_t count
, loff_t
*ppos
)
1180 struct task_struct
*task
= get_proc_task(file_inode(file
));
1181 char buffer
[PROC_NUMBUF
];
1182 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1187 oom_score_adj
= task
->signal
->oom_score_adj
;
1188 put_task_struct(task
);
1189 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1190 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1193 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1194 size_t count
, loff_t
*ppos
)
1196 char buffer
[PROC_NUMBUF
];
1200 memset(buffer
, 0, sizeof(buffer
));
1201 if (count
> sizeof(buffer
) - 1)
1202 count
= sizeof(buffer
) - 1;
1203 if (copy_from_user(buffer
, buf
, count
)) {
1208 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1211 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1212 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1217 err
= __set_oom_adj(file
, oom_score_adj
, false);
1219 return err
< 0 ? err
: count
;
1222 static const struct file_operations proc_oom_score_adj_operations
= {
1223 .read
= oom_score_adj_read
,
1224 .write
= oom_score_adj_write
,
1225 .llseek
= default_llseek
,
1229 #define TMPBUFLEN 11
1230 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1231 size_t count
, loff_t
*ppos
)
1233 struct inode
* inode
= file_inode(file
);
1234 struct task_struct
*task
= get_proc_task(inode
);
1236 char tmpbuf
[TMPBUFLEN
];
1240 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1241 from_kuid(file
->f_cred
->user_ns
,
1242 audit_get_loginuid(task
)));
1243 put_task_struct(task
);
1244 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1247 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1248 size_t count
, loff_t
*ppos
)
1250 struct inode
* inode
= file_inode(file
);
1256 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1263 /* No partial writes. */
1267 rv
= kstrtou32_from_user(buf
, count
, 10, &loginuid
);
1271 /* is userspace tring to explicitly UNSET the loginuid? */
1272 if (loginuid
== AUDIT_UID_UNSET
) {
1273 kloginuid
= INVALID_UID
;
1275 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1276 if (!uid_valid(kloginuid
))
1280 rv
= audit_set_loginuid(kloginuid
);
1286 static const struct file_operations proc_loginuid_operations
= {
1287 .read
= proc_loginuid_read
,
1288 .write
= proc_loginuid_write
,
1289 .llseek
= generic_file_llseek
,
1292 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1293 size_t count
, loff_t
*ppos
)
1295 struct inode
* inode
= file_inode(file
);
1296 struct task_struct
*task
= get_proc_task(inode
);
1298 char tmpbuf
[TMPBUFLEN
];
1302 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1303 audit_get_sessionid(task
));
1304 put_task_struct(task
);
1305 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1308 static const struct file_operations proc_sessionid_operations
= {
1309 .read
= proc_sessionid_read
,
1310 .llseek
= generic_file_llseek
,
1314 #ifdef CONFIG_FAULT_INJECTION
1315 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1316 size_t count
, loff_t
*ppos
)
1318 struct task_struct
*task
= get_proc_task(file_inode(file
));
1319 char buffer
[PROC_NUMBUF
];
1325 make_it_fail
= task
->make_it_fail
;
1326 put_task_struct(task
);
1328 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1330 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1333 static ssize_t
proc_fault_inject_write(struct file
* file
,
1334 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1336 struct task_struct
*task
;
1337 char buffer
[PROC_NUMBUF
];
1341 if (!capable(CAP_SYS_RESOURCE
))
1343 memset(buffer
, 0, sizeof(buffer
));
1344 if (count
> sizeof(buffer
) - 1)
1345 count
= sizeof(buffer
) - 1;
1346 if (copy_from_user(buffer
, buf
, count
))
1348 rv
= kstrtoint(strstrip(buffer
), 0, &make_it_fail
);
1351 if (make_it_fail
< 0 || make_it_fail
> 1)
1354 task
= get_proc_task(file_inode(file
));
1357 task
->make_it_fail
= make_it_fail
;
1358 put_task_struct(task
);
1363 static const struct file_operations proc_fault_inject_operations
= {
1364 .read
= proc_fault_inject_read
,
1365 .write
= proc_fault_inject_write
,
1366 .llseek
= generic_file_llseek
,
1369 static ssize_t
proc_fail_nth_write(struct file
*file
, const char __user
*buf
,
1370 size_t count
, loff_t
*ppos
)
1372 struct task_struct
*task
;
1376 err
= kstrtouint_from_user(buf
, count
, 0, &n
);
1380 task
= get_proc_task(file_inode(file
));
1384 put_task_struct(task
);
1389 static ssize_t
proc_fail_nth_read(struct file
*file
, char __user
*buf
,
1390 size_t count
, loff_t
*ppos
)
1392 struct task_struct
*task
;
1393 char numbuf
[PROC_NUMBUF
];
1396 task
= get_proc_task(file_inode(file
));
1399 len
= snprintf(numbuf
, sizeof(numbuf
), "%u\n", task
->fail_nth
);
1400 put_task_struct(task
);
1401 return simple_read_from_buffer(buf
, count
, ppos
, numbuf
, len
);
1404 static const struct file_operations proc_fail_nth_operations
= {
1405 .read
= proc_fail_nth_read
,
1406 .write
= proc_fail_nth_write
,
1411 #ifdef CONFIG_SCHED_DEBUG
1413 * Print out various scheduling related per-task fields:
1415 static int sched_show(struct seq_file
*m
, void *v
)
1417 struct inode
*inode
= m
->private;
1418 struct pid_namespace
*ns
= proc_pid_ns(inode
);
1419 struct task_struct
*p
;
1421 p
= get_proc_task(inode
);
1424 proc_sched_show_task(p
, ns
, m
);
1432 sched_write(struct file
*file
, const char __user
*buf
,
1433 size_t count
, loff_t
*offset
)
1435 struct inode
*inode
= file_inode(file
);
1436 struct task_struct
*p
;
1438 p
= get_proc_task(inode
);
1441 proc_sched_set_task(p
);
1448 static int sched_open(struct inode
*inode
, struct file
*filp
)
1450 return single_open(filp
, sched_show
, inode
);
1453 static const struct file_operations proc_pid_sched_operations
= {
1456 .write
= sched_write
,
1457 .llseek
= seq_lseek
,
1458 .release
= single_release
,
1463 #ifdef CONFIG_SCHED_AUTOGROUP
1465 * Print out autogroup related information:
1467 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1469 struct inode
*inode
= m
->private;
1470 struct task_struct
*p
;
1472 p
= get_proc_task(inode
);
1475 proc_sched_autogroup_show_task(p
, m
);
1483 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1484 size_t count
, loff_t
*offset
)
1486 struct inode
*inode
= file_inode(file
);
1487 struct task_struct
*p
;
1488 char buffer
[PROC_NUMBUF
];
1492 memset(buffer
, 0, sizeof(buffer
));
1493 if (count
> sizeof(buffer
) - 1)
1494 count
= sizeof(buffer
) - 1;
1495 if (copy_from_user(buffer
, buf
, count
))
1498 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1502 p
= get_proc_task(inode
);
1506 err
= proc_sched_autogroup_set_nice(p
, nice
);
1515 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1519 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1521 struct seq_file
*m
= filp
->private_data
;
1528 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1529 .open
= sched_autogroup_open
,
1531 .write
= sched_autogroup_write
,
1532 .llseek
= seq_lseek
,
1533 .release
= single_release
,
1536 #endif /* CONFIG_SCHED_AUTOGROUP */
1538 #ifdef CONFIG_TIME_NS
1539 static int timens_offsets_show(struct seq_file
*m
, void *v
)
1541 struct task_struct
*p
;
1543 p
= get_proc_task(file_inode(m
->file
));
1546 proc_timens_show_offsets(p
, m
);
1553 static ssize_t
timens_offsets_write(struct file
*file
, const char __user
*buf
,
1554 size_t count
, loff_t
*ppos
)
1556 struct inode
*inode
= file_inode(file
);
1557 struct proc_timens_offset offsets
[2];
1558 char *kbuf
= NULL
, *pos
, *next_line
;
1559 struct task_struct
*p
;
1562 /* Only allow < page size writes at the beginning of the file */
1563 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
1566 /* Slurp in the user data */
1567 kbuf
= memdup_user_nul(buf
, count
);
1569 return PTR_ERR(kbuf
);
1571 /* Parse the user data */
1574 for (pos
= kbuf
; pos
; pos
= next_line
) {
1575 struct proc_timens_offset
*off
= &offsets
[noffsets
];
1578 /* Find the end of line and ensure we don't look past it */
1579 next_line
= strchr(pos
, '\n');
1583 if (*next_line
== '\0')
1587 err
= sscanf(pos
, "%u %lld %lu", &off
->clockid
,
1588 &off
->val
.tv_sec
, &off
->val
.tv_nsec
);
1589 if (err
!= 3 || off
->val
.tv_nsec
>= NSEC_PER_SEC
)
1592 if (noffsets
== ARRAY_SIZE(offsets
)) {
1594 count
= next_line
- kbuf
;
1600 p
= get_proc_task(inode
);
1603 ret
= proc_timens_set_offset(file
, p
, offsets
, noffsets
);
1614 static int timens_offsets_open(struct inode
*inode
, struct file
*filp
)
1616 return single_open(filp
, timens_offsets_show
, inode
);
1619 static const struct file_operations proc_timens_offsets_operations
= {
1620 .open
= timens_offsets_open
,
1622 .write
= timens_offsets_write
,
1623 .llseek
= seq_lseek
,
1624 .release
= single_release
,
1626 #endif /* CONFIG_TIME_NS */
1628 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1629 size_t count
, loff_t
*offset
)
1631 struct inode
*inode
= file_inode(file
);
1632 struct task_struct
*p
;
1633 char buffer
[TASK_COMM_LEN
];
1634 const size_t maxlen
= sizeof(buffer
) - 1;
1636 memset(buffer
, 0, sizeof(buffer
));
1637 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1640 p
= get_proc_task(inode
);
1644 if (same_thread_group(current
, p
))
1645 set_task_comm(p
, buffer
);
1654 static int comm_show(struct seq_file
*m
, void *v
)
1656 struct inode
*inode
= m
->private;
1657 struct task_struct
*p
;
1659 p
= get_proc_task(inode
);
1663 proc_task_name(m
, p
, false);
1671 static int comm_open(struct inode
*inode
, struct file
*filp
)
1673 return single_open(filp
, comm_show
, inode
);
1676 static const struct file_operations proc_pid_set_comm_operations
= {
1679 .write
= comm_write
,
1680 .llseek
= seq_lseek
,
1681 .release
= single_release
,
1684 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1686 struct task_struct
*task
;
1687 struct file
*exe_file
;
1689 task
= get_proc_task(d_inode(dentry
));
1692 exe_file
= get_task_exe_file(task
);
1693 put_task_struct(task
);
1695 *exe_path
= exe_file
->f_path
;
1696 path_get(&exe_file
->f_path
);
1703 static const char *proc_pid_get_link(struct dentry
*dentry
,
1704 struct inode
*inode
,
1705 struct delayed_call
*done
)
1708 int error
= -EACCES
;
1711 return ERR_PTR(-ECHILD
);
1713 /* Are we allowed to snoop on the tasks file descriptors? */
1714 if (!proc_fd_access_allowed(inode
))
1717 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1721 error
= nd_jump_link(&path
);
1723 return ERR_PTR(error
);
1726 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1728 char *tmp
= (char *)__get_free_page(GFP_KERNEL
);
1735 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1736 len
= PTR_ERR(pathname
);
1737 if (IS_ERR(pathname
))
1739 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1743 if (copy_to_user(buffer
, pathname
, len
))
1746 free_page((unsigned long)tmp
);
1750 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1752 int error
= -EACCES
;
1753 struct inode
*inode
= d_inode(dentry
);
1756 /* Are we allowed to snoop on the tasks file descriptors? */
1757 if (!proc_fd_access_allowed(inode
))
1760 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1764 error
= do_proc_readlink(&path
, buffer
, buflen
);
1770 const struct inode_operations proc_pid_link_inode_operations
= {
1771 .readlink
= proc_pid_readlink
,
1772 .get_link
= proc_pid_get_link
,
1773 .setattr
= proc_setattr
,
1777 /* building an inode */
1779 void task_dump_owner(struct task_struct
*task
, umode_t mode
,
1780 kuid_t
*ruid
, kgid_t
*rgid
)
1782 /* Depending on the state of dumpable compute who should own a
1783 * proc file for a task.
1785 const struct cred
*cred
;
1789 if (unlikely(task
->flags
& PF_KTHREAD
)) {
1790 *ruid
= GLOBAL_ROOT_UID
;
1791 *rgid
= GLOBAL_ROOT_GID
;
1795 /* Default to the tasks effective ownership */
1797 cred
= __task_cred(task
);
1803 * Before the /proc/pid/status file was created the only way to read
1804 * the effective uid of a /process was to stat /proc/pid. Reading
1805 * /proc/pid/status is slow enough that procps and other packages
1806 * kept stating /proc/pid. To keep the rules in /proc simple I have
1807 * made this apply to all per process world readable and executable
1810 if (mode
!= (S_IFDIR
|S_IRUGO
|S_IXUGO
)) {
1811 struct mm_struct
*mm
;
1814 /* Make non-dumpable tasks owned by some root */
1816 if (get_dumpable(mm
) != SUID_DUMP_USER
) {
1817 struct user_namespace
*user_ns
= mm
->user_ns
;
1819 uid
= make_kuid(user_ns
, 0);
1820 if (!uid_valid(uid
))
1821 uid
= GLOBAL_ROOT_UID
;
1823 gid
= make_kgid(user_ns
, 0);
1824 if (!gid_valid(gid
))
1825 gid
= GLOBAL_ROOT_GID
;
1828 uid
= GLOBAL_ROOT_UID
;
1829 gid
= GLOBAL_ROOT_GID
;
1837 void proc_pid_evict_inode(struct proc_inode
*ei
)
1839 struct pid
*pid
= ei
->pid
;
1841 if (S_ISDIR(ei
->vfs_inode
.i_mode
)) {
1842 spin_lock(&pid
->lock
);
1843 hlist_del_init_rcu(&ei
->sibling_inodes
);
1844 spin_unlock(&pid
->lock
);
1850 struct inode
*proc_pid_make_inode(struct super_block
* sb
,
1851 struct task_struct
*task
, umode_t mode
)
1853 struct inode
* inode
;
1854 struct proc_inode
*ei
;
1857 /* We need a new inode */
1859 inode
= new_inode(sb
);
1865 inode
->i_mode
= mode
;
1866 inode
->i_ino
= get_next_ino();
1867 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
1868 inode
->i_op
= &proc_def_inode_operations
;
1871 * grab the reference to task.
1873 pid
= get_task_pid(task
, PIDTYPE_PID
);
1877 /* Let the pid remember us for quick removal */
1879 if (S_ISDIR(mode
)) {
1880 spin_lock(&pid
->lock
);
1881 hlist_add_head_rcu(&ei
->sibling_inodes
, &pid
->inodes
);
1882 spin_unlock(&pid
->lock
);
1885 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1886 security_task_to_inode(task
, inode
);
1896 int pid_getattr(const struct path
*path
, struct kstat
*stat
,
1897 u32 request_mask
, unsigned int query_flags
)
1899 struct inode
*inode
= d_inode(path
->dentry
);
1900 struct pid_namespace
*pid
= proc_pid_ns(inode
);
1901 struct task_struct
*task
;
1903 generic_fillattr(inode
, stat
);
1905 stat
->uid
= GLOBAL_ROOT_UID
;
1906 stat
->gid
= GLOBAL_ROOT_GID
;
1908 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1910 if (!has_pid_permissions(pid
, task
, HIDEPID_INVISIBLE
)) {
1913 * This doesn't prevent learning whether PID exists,
1914 * it only makes getattr() consistent with readdir().
1918 task_dump_owner(task
, inode
->i_mode
, &stat
->uid
, &stat
->gid
);
1927 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1929 void pid_update_inode(struct task_struct
*task
, struct inode
*inode
)
1931 task_dump_owner(task
, inode
->i_mode
, &inode
->i_uid
, &inode
->i_gid
);
1933 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1934 security_task_to_inode(task
, inode
);
1938 * Rewrite the inode's ownerships here because the owning task may have
1939 * performed a setuid(), etc.
1942 static int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1944 struct inode
*inode
;
1945 struct task_struct
*task
;
1947 if (flags
& LOOKUP_RCU
)
1950 inode
= d_inode(dentry
);
1951 task
= get_proc_task(inode
);
1954 pid_update_inode(task
, inode
);
1955 put_task_struct(task
);
1961 static inline bool proc_inode_is_dead(struct inode
*inode
)
1963 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1966 int pid_delete_dentry(const struct dentry
*dentry
)
1968 /* Is the task we represent dead?
1969 * If so, then don't put the dentry on the lru list,
1970 * kill it immediately.
1972 return proc_inode_is_dead(d_inode(dentry
));
1975 const struct dentry_operations pid_dentry_operations
=
1977 .d_revalidate
= pid_revalidate
,
1978 .d_delete
= pid_delete_dentry
,
1984 * Fill a directory entry.
1986 * If possible create the dcache entry and derive our inode number and
1987 * file type from dcache entry.
1989 * Since all of the proc inode numbers are dynamically generated, the inode
1990 * numbers do not exist until the inode is cache. This means creating the
1991 * the dcache entry in readdir is necessary to keep the inode numbers
1992 * reported by readdir in sync with the inode numbers reported
1995 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1996 const char *name
, unsigned int len
,
1997 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1999 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
2000 struct qstr qname
= QSTR_INIT(name
, len
);
2001 struct inode
*inode
;
2002 unsigned type
= DT_UNKNOWN
;
2005 child
= d_hash_and_lookup(dir
, &qname
);
2007 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
2008 child
= d_alloc_parallel(dir
, &qname
, &wq
);
2010 goto end_instantiate
;
2011 if (d_in_lookup(child
)) {
2013 res
= instantiate(child
, task
, ptr
);
2014 d_lookup_done(child
);
2015 if (unlikely(res
)) {
2019 goto end_instantiate
;
2023 inode
= d_inode(child
);
2025 type
= inode
->i_mode
>> 12;
2028 return dir_emit(ctx
, name
, len
, ino
, type
);
2032 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2033 * which represent vma start and end addresses.
2035 static int dname_to_vma_addr(struct dentry
*dentry
,
2036 unsigned long *start
, unsigned long *end
)
2038 const char *str
= dentry
->d_name
.name
;
2039 unsigned long long sval
, eval
;
2042 if (str
[0] == '0' && str
[1] != '-')
2044 len
= _parse_integer(str
, 16, &sval
);
2045 if (len
& KSTRTOX_OVERFLOW
)
2047 if (sval
!= (unsigned long)sval
)
2055 if (str
[0] == '0' && str
[1])
2057 len
= _parse_integer(str
, 16, &eval
);
2058 if (len
& KSTRTOX_OVERFLOW
)
2060 if (eval
!= (unsigned long)eval
)
2073 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
2075 unsigned long vm_start
, vm_end
;
2076 bool exact_vma_exists
= false;
2077 struct mm_struct
*mm
= NULL
;
2078 struct task_struct
*task
;
2079 struct inode
*inode
;
2082 if (flags
& LOOKUP_RCU
)
2085 inode
= d_inode(dentry
);
2086 task
= get_proc_task(inode
);
2090 mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
2091 if (IS_ERR_OR_NULL(mm
))
2094 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
2095 status
= down_read_killable(&mm
->mmap_sem
);
2097 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
,
2099 up_read(&mm
->mmap_sem
);
2105 if (exact_vma_exists
) {
2106 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
2108 security_task_to_inode(task
, inode
);
2113 put_task_struct(task
);
2119 static const struct dentry_operations tid_map_files_dentry_operations
= {
2120 .d_revalidate
= map_files_d_revalidate
,
2121 .d_delete
= pid_delete_dentry
,
2124 static int map_files_get_link(struct dentry
*dentry
, struct path
*path
)
2126 unsigned long vm_start
, vm_end
;
2127 struct vm_area_struct
*vma
;
2128 struct task_struct
*task
;
2129 struct mm_struct
*mm
;
2133 task
= get_proc_task(d_inode(dentry
));
2137 mm
= get_task_mm(task
);
2138 put_task_struct(task
);
2142 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
2146 rc
= down_read_killable(&mm
->mmap_sem
);
2151 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2152 if (vma
&& vma
->vm_file
) {
2153 *path
= vma
->vm_file
->f_path
;
2157 up_read(&mm
->mmap_sem
);
2165 struct map_files_info
{
2166 unsigned long start
;
2172 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2173 * symlinks may be used to bypass permissions on ancestor directories in the
2174 * path to the file in question.
2177 proc_map_files_get_link(struct dentry
*dentry
,
2178 struct inode
*inode
,
2179 struct delayed_call
*done
)
2181 if (!capable(CAP_SYS_ADMIN
))
2182 return ERR_PTR(-EPERM
);
2184 return proc_pid_get_link(dentry
, inode
, done
);
2188 * Identical to proc_pid_link_inode_operations except for get_link()
2190 static const struct inode_operations proc_map_files_link_inode_operations
= {
2191 .readlink
= proc_pid_readlink
,
2192 .get_link
= proc_map_files_get_link
,
2193 .setattr
= proc_setattr
,
2196 static struct dentry
*
2197 proc_map_files_instantiate(struct dentry
*dentry
,
2198 struct task_struct
*task
, const void *ptr
)
2200 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
2201 struct proc_inode
*ei
;
2202 struct inode
*inode
;
2204 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, S_IFLNK
|
2205 ((mode
& FMODE_READ
) ? S_IRUSR
: 0) |
2206 ((mode
& FMODE_WRITE
) ? S_IWUSR
: 0));
2208 return ERR_PTR(-ENOENT
);
2211 ei
->op
.proc_get_link
= map_files_get_link
;
2213 inode
->i_op
= &proc_map_files_link_inode_operations
;
2216 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2217 return d_splice_alias(inode
, dentry
);
2220 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2221 struct dentry
*dentry
, unsigned int flags
)
2223 unsigned long vm_start
, vm_end
;
2224 struct vm_area_struct
*vma
;
2225 struct task_struct
*task
;
2226 struct dentry
*result
;
2227 struct mm_struct
*mm
;
2229 result
= ERR_PTR(-ENOENT
);
2230 task
= get_proc_task(dir
);
2234 result
= ERR_PTR(-EACCES
);
2235 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2238 result
= ERR_PTR(-ENOENT
);
2239 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2242 mm
= get_task_mm(task
);
2246 result
= ERR_PTR(-EINTR
);
2247 if (down_read_killable(&mm
->mmap_sem
))
2250 result
= ERR_PTR(-ENOENT
);
2251 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2256 result
= proc_map_files_instantiate(dentry
, task
,
2257 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2260 up_read(&mm
->mmap_sem
);
2264 put_task_struct(task
);
2269 static const struct inode_operations proc_map_files_inode_operations
= {
2270 .lookup
= proc_map_files_lookup
,
2271 .permission
= proc_fd_permission
,
2272 .setattr
= proc_setattr
,
2276 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2278 struct vm_area_struct
*vma
;
2279 struct task_struct
*task
;
2280 struct mm_struct
*mm
;
2281 unsigned long nr_files
, pos
, i
;
2282 GENRADIX(struct map_files_info
) fa
;
2283 struct map_files_info
*p
;
2289 task
= get_proc_task(file_inode(file
));
2294 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2298 if (!dir_emit_dots(file
, ctx
))
2301 mm
= get_task_mm(task
);
2305 ret
= down_read_killable(&mm
->mmap_sem
);
2314 * We need two passes here:
2316 * 1) Collect vmas of mapped files with mmap_sem taken
2317 * 2) Release mmap_sem and instantiate entries
2319 * otherwise we get lockdep complained, since filldir()
2320 * routine might require mmap_sem taken in might_fault().
2323 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2326 if (++pos
<= ctx
->pos
)
2329 p
= genradix_ptr_alloc(&fa
, nr_files
++, GFP_KERNEL
);
2332 up_read(&mm
->mmap_sem
);
2337 p
->start
= vma
->vm_start
;
2338 p
->end
= vma
->vm_end
;
2339 p
->mode
= vma
->vm_file
->f_mode
;
2341 up_read(&mm
->mmap_sem
);
2344 for (i
= 0; i
< nr_files
; i
++) {
2345 char buf
[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2348 p
= genradix_ptr(&fa
, i
);
2349 len
= snprintf(buf
, sizeof(buf
), "%lx-%lx", p
->start
, p
->end
);
2350 if (!proc_fill_cache(file
, ctx
,
2352 proc_map_files_instantiate
,
2354 (void *)(unsigned long)p
->mode
))
2360 put_task_struct(task
);
2366 static const struct file_operations proc_map_files_operations
= {
2367 .read
= generic_read_dir
,
2368 .iterate_shared
= proc_map_files_readdir
,
2369 .llseek
= generic_file_llseek
,
2372 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2373 struct timers_private
{
2375 struct task_struct
*task
;
2376 struct sighand_struct
*sighand
;
2377 struct pid_namespace
*ns
;
2378 unsigned long flags
;
2381 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2383 struct timers_private
*tp
= m
->private;
2385 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2387 return ERR_PTR(-ESRCH
);
2389 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2391 return ERR_PTR(-ESRCH
);
2393 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2396 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2398 struct timers_private
*tp
= m
->private;
2399 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2402 static void timers_stop(struct seq_file
*m
, void *v
)
2404 struct timers_private
*tp
= m
->private;
2407 unlock_task_sighand(tp
->task
, &tp
->flags
);
2412 put_task_struct(tp
->task
);
2417 static int show_timer(struct seq_file
*m
, void *v
)
2419 struct k_itimer
*timer
;
2420 struct timers_private
*tp
= m
->private;
2422 static const char * const nstr
[] = {
2423 [SIGEV_SIGNAL
] = "signal",
2424 [SIGEV_NONE
] = "none",
2425 [SIGEV_THREAD
] = "thread",
2428 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2429 notify
= timer
->it_sigev_notify
;
2431 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2432 seq_printf(m
, "signal: %d/%px\n",
2433 timer
->sigq
->info
.si_signo
,
2434 timer
->sigq
->info
.si_value
.sival_ptr
);
2435 seq_printf(m
, "notify: %s/%s.%d\n",
2436 nstr
[notify
& ~SIGEV_THREAD_ID
],
2437 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2438 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2439 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2444 static const struct seq_operations proc_timers_seq_ops
= {
2445 .start
= timers_start
,
2446 .next
= timers_next
,
2447 .stop
= timers_stop
,
2451 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2453 struct timers_private
*tp
;
2455 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2456 sizeof(struct timers_private
));
2460 tp
->pid
= proc_pid(inode
);
2461 tp
->ns
= proc_pid_ns(inode
);
2465 static const struct file_operations proc_timers_operations
= {
2466 .open
= proc_timers_open
,
2468 .llseek
= seq_lseek
,
2469 .release
= seq_release_private
,
2473 static ssize_t
timerslack_ns_write(struct file
*file
, const char __user
*buf
,
2474 size_t count
, loff_t
*offset
)
2476 struct inode
*inode
= file_inode(file
);
2477 struct task_struct
*p
;
2481 err
= kstrtoull_from_user(buf
, count
, 10, &slack_ns
);
2485 p
= get_proc_task(inode
);
2491 if (!ns_capable(__task_cred(p
)->user_ns
, CAP_SYS_NICE
)) {
2498 err
= security_task_setscheduler(p
);
2507 p
->timer_slack_ns
= p
->default_timer_slack_ns
;
2509 p
->timer_slack_ns
= slack_ns
;
2518 static int timerslack_ns_show(struct seq_file
*m
, void *v
)
2520 struct inode
*inode
= m
->private;
2521 struct task_struct
*p
;
2524 p
= get_proc_task(inode
);
2530 if (!ns_capable(__task_cred(p
)->user_ns
, CAP_SYS_NICE
)) {
2537 err
= security_task_getscheduler(p
);
2543 seq_printf(m
, "%llu\n", p
->timer_slack_ns
);
2552 static int timerslack_ns_open(struct inode
*inode
, struct file
*filp
)
2554 return single_open(filp
, timerslack_ns_show
, inode
);
2557 static const struct file_operations proc_pid_set_timerslack_ns_operations
= {
2558 .open
= timerslack_ns_open
,
2560 .write
= timerslack_ns_write
,
2561 .llseek
= seq_lseek
,
2562 .release
= single_release
,
2565 static struct dentry
*proc_pident_instantiate(struct dentry
*dentry
,
2566 struct task_struct
*task
, const void *ptr
)
2568 const struct pid_entry
*p
= ptr
;
2569 struct inode
*inode
;
2570 struct proc_inode
*ei
;
2572 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, p
->mode
);
2574 return ERR_PTR(-ENOENT
);
2577 if (S_ISDIR(inode
->i_mode
))
2578 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2580 inode
->i_op
= p
->iop
;
2582 inode
->i_fop
= p
->fop
;
2584 pid_update_inode(task
, inode
);
2585 d_set_d_op(dentry
, &pid_dentry_operations
);
2586 return d_splice_alias(inode
, dentry
);
2589 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2590 struct dentry
*dentry
,
2591 const struct pid_entry
*p
,
2592 const struct pid_entry
*end
)
2594 struct task_struct
*task
= get_proc_task(dir
);
2595 struct dentry
*res
= ERR_PTR(-ENOENT
);
2601 * Yes, it does not scale. And it should not. Don't add
2602 * new entries into /proc/<tgid>/ without very good reasons.
2604 for (; p
< end
; p
++) {
2605 if (p
->len
!= dentry
->d_name
.len
)
2607 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
)) {
2608 res
= proc_pident_instantiate(dentry
, task
, p
);
2612 put_task_struct(task
);
2617 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2618 const struct pid_entry
*ents
, unsigned int nents
)
2620 struct task_struct
*task
= get_proc_task(file_inode(file
));
2621 const struct pid_entry
*p
;
2626 if (!dir_emit_dots(file
, ctx
))
2629 if (ctx
->pos
>= nents
+ 2)
2632 for (p
= ents
+ (ctx
->pos
- 2); p
< ents
+ nents
; p
++) {
2633 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2634 proc_pident_instantiate
, task
, p
))
2639 put_task_struct(task
);
2643 #ifdef CONFIG_SECURITY
2644 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2645 size_t count
, loff_t
*ppos
)
2647 struct inode
* inode
= file_inode(file
);
2650 struct task_struct
*task
= get_proc_task(inode
);
2655 length
= security_getprocattr(task
, PROC_I(inode
)->op
.lsm
,
2656 (char*)file
->f_path
.dentry
->d_name
.name
,
2658 put_task_struct(task
);
2660 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2665 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2666 size_t count
, loff_t
*ppos
)
2668 struct inode
* inode
= file_inode(file
);
2669 struct task_struct
*task
;
2674 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
2679 /* A task may only write its own attributes. */
2680 if (current
!= task
) {
2684 /* Prevent changes to overridden credentials. */
2685 if (current_cred() != current_real_cred()) {
2691 if (count
> PAGE_SIZE
)
2694 /* No partial writes. */
2698 page
= memdup_user(buf
, count
);
2704 /* Guard against adverse ptrace interaction */
2705 rv
= mutex_lock_interruptible(¤t
->signal
->cred_guard_mutex
);
2709 rv
= security_setprocattr(PROC_I(inode
)->op
.lsm
,
2710 file
->f_path
.dentry
->d_name
.name
, page
,
2712 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
2719 static const struct file_operations proc_pid_attr_operations
= {
2720 .read
= proc_pid_attr_read
,
2721 .write
= proc_pid_attr_write
,
2722 .llseek
= generic_file_llseek
,
2725 #define LSM_DIR_OPS(LSM) \
2726 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2727 struct dir_context *ctx) \
2729 return proc_pident_readdir(filp, ctx, \
2730 LSM##_attr_dir_stuff, \
2731 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2734 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2735 .read = generic_read_dir, \
2736 .iterate = proc_##LSM##_attr_dir_iterate, \
2737 .llseek = default_llseek, \
2740 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2741 struct dentry *dentry, unsigned int flags) \
2743 return proc_pident_lookup(dir, dentry, \
2744 LSM##_attr_dir_stuff, \
2745 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2748 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2749 .lookup = proc_##LSM##_attr_dir_lookup, \
2750 .getattr = pid_getattr, \
2751 .setattr = proc_setattr, \
2754 #ifdef CONFIG_SECURITY_SMACK
2755 static const struct pid_entry smack_attr_dir_stuff
[] = {
2756 ATTR("smack", "current", 0666),
2761 static const struct pid_entry attr_dir_stuff
[] = {
2762 ATTR(NULL
, "current", 0666),
2763 ATTR(NULL
, "prev", 0444),
2764 ATTR(NULL
, "exec", 0666),
2765 ATTR(NULL
, "fscreate", 0666),
2766 ATTR(NULL
, "keycreate", 0666),
2767 ATTR(NULL
, "sockcreate", 0666),
2768 #ifdef CONFIG_SECURITY_SMACK
2770 proc_smack_attr_dir_inode_ops
, proc_smack_attr_dir_ops
),
2774 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2776 return proc_pident_readdir(file
, ctx
,
2777 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2780 static const struct file_operations proc_attr_dir_operations
= {
2781 .read
= generic_read_dir
,
2782 .iterate_shared
= proc_attr_dir_readdir
,
2783 .llseek
= generic_file_llseek
,
2786 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2787 struct dentry
*dentry
, unsigned int flags
)
2789 return proc_pident_lookup(dir
, dentry
,
2791 attr_dir_stuff
+ ARRAY_SIZE(attr_dir_stuff
));
2794 static const struct inode_operations proc_attr_dir_inode_operations
= {
2795 .lookup
= proc_attr_dir_lookup
,
2796 .getattr
= pid_getattr
,
2797 .setattr
= proc_setattr
,
2802 #ifdef CONFIG_ELF_CORE
2803 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2804 size_t count
, loff_t
*ppos
)
2806 struct task_struct
*task
= get_proc_task(file_inode(file
));
2807 struct mm_struct
*mm
;
2808 char buffer
[PROC_NUMBUF
];
2816 mm
= get_task_mm(task
);
2818 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2819 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2820 MMF_DUMP_FILTER_SHIFT
));
2822 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2825 put_task_struct(task
);
2830 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2831 const char __user
*buf
,
2835 struct task_struct
*task
;
2836 struct mm_struct
*mm
;
2842 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2847 task
= get_proc_task(file_inode(file
));
2851 mm
= get_task_mm(task
);
2856 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2858 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2860 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2865 put_task_struct(task
);
2872 static const struct file_operations proc_coredump_filter_operations
= {
2873 .read
= proc_coredump_filter_read
,
2874 .write
= proc_coredump_filter_write
,
2875 .llseek
= generic_file_llseek
,
2879 #ifdef CONFIG_TASK_IO_ACCOUNTING
2880 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2882 struct task_io_accounting acct
= task
->ioac
;
2883 unsigned long flags
;
2886 result
= mutex_lock_killable(&task
->signal
->exec_update_mutex
);
2890 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2895 if (whole
&& lock_task_sighand(task
, &flags
)) {
2896 struct task_struct
*t
= task
;
2898 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2899 while_each_thread(task
, t
)
2900 task_io_accounting_add(&acct
, &t
->ioac
);
2902 unlock_task_sighand(task
, &flags
);
2909 "read_bytes: %llu\n"
2910 "write_bytes: %llu\n"
2911 "cancelled_write_bytes: %llu\n",
2912 (unsigned long long)acct
.rchar
,
2913 (unsigned long long)acct
.wchar
,
2914 (unsigned long long)acct
.syscr
,
2915 (unsigned long long)acct
.syscw
,
2916 (unsigned long long)acct
.read_bytes
,
2917 (unsigned long long)acct
.write_bytes
,
2918 (unsigned long long)acct
.cancelled_write_bytes
);
2922 mutex_unlock(&task
->signal
->exec_update_mutex
);
2926 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2927 struct pid
*pid
, struct task_struct
*task
)
2929 return do_io_accounting(task
, m
, 0);
2932 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2933 struct pid
*pid
, struct task_struct
*task
)
2935 return do_io_accounting(task
, m
, 1);
2937 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2939 #ifdef CONFIG_USER_NS
2940 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2941 const struct seq_operations
*seq_ops
)
2943 struct user_namespace
*ns
= NULL
;
2944 struct task_struct
*task
;
2945 struct seq_file
*seq
;
2948 task
= get_proc_task(inode
);
2951 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2953 put_task_struct(task
);
2958 ret
= seq_open(file
, seq_ops
);
2962 seq
= file
->private_data
;
2972 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2974 struct seq_file
*seq
= file
->private_data
;
2975 struct user_namespace
*ns
= seq
->private;
2977 return seq_release(inode
, file
);
2980 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2982 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2985 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2987 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2990 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2992 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2995 static const struct file_operations proc_uid_map_operations
= {
2996 .open
= proc_uid_map_open
,
2997 .write
= proc_uid_map_write
,
2999 .llseek
= seq_lseek
,
3000 .release
= proc_id_map_release
,
3003 static const struct file_operations proc_gid_map_operations
= {
3004 .open
= proc_gid_map_open
,
3005 .write
= proc_gid_map_write
,
3007 .llseek
= seq_lseek
,
3008 .release
= proc_id_map_release
,
3011 static const struct file_operations proc_projid_map_operations
= {
3012 .open
= proc_projid_map_open
,
3013 .write
= proc_projid_map_write
,
3015 .llseek
= seq_lseek
,
3016 .release
= proc_id_map_release
,
3019 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
3021 struct user_namespace
*ns
= NULL
;
3022 struct task_struct
*task
;
3026 task
= get_proc_task(inode
);
3029 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
3031 put_task_struct(task
);
3036 if (file
->f_mode
& FMODE_WRITE
) {
3038 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
3042 ret
= single_open(file
, &proc_setgroups_show
, ns
);
3053 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
3055 struct seq_file
*seq
= file
->private_data
;
3056 struct user_namespace
*ns
= seq
->private;
3057 int ret
= single_release(inode
, file
);
3062 static const struct file_operations proc_setgroups_operations
= {
3063 .open
= proc_setgroups_open
,
3064 .write
= proc_setgroups_write
,
3066 .llseek
= seq_lseek
,
3067 .release
= proc_setgroups_release
,
3069 #endif /* CONFIG_USER_NS */
3071 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
3072 struct pid
*pid
, struct task_struct
*task
)
3074 int err
= lock_trace(task
);
3076 seq_printf(m
, "%08x\n", task
->personality
);
3082 #ifdef CONFIG_LIVEPATCH
3083 static int proc_pid_patch_state(struct seq_file
*m
, struct pid_namespace
*ns
,
3084 struct pid
*pid
, struct task_struct
*task
)
3086 seq_printf(m
, "%d\n", task
->patch_state
);
3089 #endif /* CONFIG_LIVEPATCH */
3091 #ifdef CONFIG_STACKLEAK_METRICS
3092 static int proc_stack_depth(struct seq_file
*m
, struct pid_namespace
*ns
,
3093 struct pid
*pid
, struct task_struct
*task
)
3095 unsigned long prev_depth
= THREAD_SIZE
-
3096 (task
->prev_lowest_stack
& (THREAD_SIZE
- 1));
3097 unsigned long depth
= THREAD_SIZE
-
3098 (task
->lowest_stack
& (THREAD_SIZE
- 1));
3100 seq_printf(m
, "previous stack depth: %lu\nstack depth: %lu\n",
3104 #endif /* CONFIG_STACKLEAK_METRICS */
3109 static const struct file_operations proc_task_operations
;
3110 static const struct inode_operations proc_task_inode_operations
;
3112 static const struct pid_entry tgid_base_stuff
[] = {
3113 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
3114 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3115 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
3116 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3117 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3119 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3121 REG("environ", S_IRUSR
, proc_environ_operations
),
3122 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3123 ONE("status", S_IRUGO
, proc_pid_status
),
3124 ONE("personality", S_IRUSR
, proc_pid_personality
),
3125 ONE("limits", S_IRUGO
, proc_pid_limits
),
3126 #ifdef CONFIG_SCHED_DEBUG
3127 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3129 #ifdef CONFIG_SCHED_AUTOGROUP
3130 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
3132 #ifdef CONFIG_TIME_NS
3133 REG("timens_offsets", S_IRUGO
|S_IWUSR
, proc_timens_offsets_operations
),
3135 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3136 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3137 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3139 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3140 ONE("stat", S_IRUGO
, proc_tgid_stat
),
3141 ONE("statm", S_IRUGO
, proc_pid_statm
),
3142 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
3144 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
3146 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3147 LNK("cwd", proc_cwd_link
),
3148 LNK("root", proc_root_link
),
3149 LNK("exe", proc_exe_link
),
3150 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3151 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3152 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
3153 #ifdef CONFIG_PROC_PAGE_MONITOR
3154 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3155 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
3156 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
3157 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3159 #ifdef CONFIG_SECURITY
3160 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3162 #ifdef CONFIG_KALLSYMS
3163 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3165 #ifdef CONFIG_STACKTRACE
3166 ONE("stack", S_IRUSR
, proc_pid_stack
),
3168 #ifdef CONFIG_SCHED_INFO
3169 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3171 #ifdef CONFIG_LATENCYTOP
3172 REG("latency", S_IRUGO
, proc_lstats_operations
),
3174 #ifdef CONFIG_PROC_PID_CPUSET
3175 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3177 #ifdef CONFIG_CGROUPS
3178 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3180 #ifdef CONFIG_PROC_CPU_RESCTRL
3181 ONE("cpu_resctrl_groups", S_IRUGO
, proc_resctrl_show
),
3183 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3184 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3185 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3187 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3188 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3190 #ifdef CONFIG_FAULT_INJECTION
3191 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3192 REG("fail-nth", 0644, proc_fail_nth_operations
),
3194 #ifdef CONFIG_ELF_CORE
3195 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
3197 #ifdef CONFIG_TASK_IO_ACCOUNTING
3198 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
3200 #ifdef CONFIG_USER_NS
3201 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3202 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3203 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3204 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3206 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3207 REG("timers", S_IRUGO
, proc_timers_operations
),
3209 REG("timerslack_ns", S_IRUGO
|S_IWUGO
, proc_pid_set_timerslack_ns_operations
),
3210 #ifdef CONFIG_LIVEPATCH
3211 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3213 #ifdef CONFIG_STACKLEAK_METRICS
3214 ONE("stack_depth", S_IRUGO
, proc_stack_depth
),
3216 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3217 ONE("arch_status", S_IRUGO
, proc_pid_arch_status
),
3221 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3223 return proc_pident_readdir(file
, ctx
,
3224 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3227 static const struct file_operations proc_tgid_base_operations
= {
3228 .read
= generic_read_dir
,
3229 .iterate_shared
= proc_tgid_base_readdir
,
3230 .llseek
= generic_file_llseek
,
3233 struct pid
*tgid_pidfd_to_pid(const struct file
*file
)
3235 if (file
->f_op
!= &proc_tgid_base_operations
)
3236 return ERR_PTR(-EBADF
);
3238 return proc_pid(file_inode(file
));
3241 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3243 return proc_pident_lookup(dir
, dentry
,
3245 tgid_base_stuff
+ ARRAY_SIZE(tgid_base_stuff
));
3248 static const struct inode_operations proc_tgid_base_inode_operations
= {
3249 .lookup
= proc_tgid_base_lookup
,
3250 .getattr
= pid_getattr
,
3251 .setattr
= proc_setattr
,
3252 .permission
= proc_pid_permission
,
3256 * proc_flush_pid - Remove dcache entries for @pid from the /proc dcache.
3257 * @pid: pid that should be flushed.
3259 * This function walks a list of inodes (that belong to any proc
3260 * filesystem) that are attached to the pid and flushes them from
3263 * It is safe and reasonable to cache /proc entries for a task until
3264 * that task exits. After that they just clog up the dcache with
3265 * useless entries, possibly causing useful dcache entries to be
3266 * flushed instead. This routine is provided to flush those useless
3267 * dcache entries when a process is reaped.
3269 * NOTE: This routine is just an optimization so it does not guarantee
3270 * that no dcache entries will exist after a process is reaped
3271 * it just makes it very unlikely that any will persist.
3274 void proc_flush_pid(struct pid
*pid
)
3276 proc_invalidate_siblings_dcache(&pid
->inodes
, &pid
->lock
);
3280 static struct dentry
*proc_pid_instantiate(struct dentry
* dentry
,
3281 struct task_struct
*task
, const void *ptr
)
3283 struct inode
*inode
;
3285 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3287 return ERR_PTR(-ENOENT
);
3289 inode
->i_op
= &proc_tgid_base_inode_operations
;
3290 inode
->i_fop
= &proc_tgid_base_operations
;
3291 inode
->i_flags
|=S_IMMUTABLE
;
3293 set_nlink(inode
, nlink_tgid
);
3294 pid_update_inode(task
, inode
);
3296 d_set_d_op(dentry
, &pid_dentry_operations
);
3297 return d_splice_alias(inode
, dentry
);
3300 struct dentry
*proc_pid_lookup(struct dentry
*dentry
, unsigned int flags
)
3302 struct task_struct
*task
;
3304 struct pid_namespace
*ns
;
3305 struct dentry
*result
= ERR_PTR(-ENOENT
);
3307 tgid
= name_to_int(&dentry
->d_name
);
3311 ns
= dentry
->d_sb
->s_fs_info
;
3313 task
= find_task_by_pid_ns(tgid
, ns
);
3315 get_task_struct(task
);
3320 result
= proc_pid_instantiate(dentry
, task
, NULL
);
3321 put_task_struct(task
);
3327 * Find the first task with tgid >= tgid
3332 struct task_struct
*task
;
3334 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3339 put_task_struct(iter
.task
);
3343 pid
= find_ge_pid(iter
.tgid
, ns
);
3345 iter
.tgid
= pid_nr_ns(pid
, ns
);
3346 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3347 /* What we to know is if the pid we have find is the
3348 * pid of a thread_group_leader. Testing for task
3349 * being a thread_group_leader is the obvious thing
3350 * todo but there is a window when it fails, due to
3351 * the pid transfer logic in de_thread.
3353 * So we perform the straight forward test of seeing
3354 * if the pid we have found is the pid of a thread
3355 * group leader, and don't worry if the task we have
3356 * found doesn't happen to be a thread group leader.
3357 * As we don't care in the case of readdir.
3359 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3363 get_task_struct(iter
.task
);
3369 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3371 /* for the /proc/ directory itself, after non-process stuff has been done */
3372 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3374 struct tgid_iter iter
;
3375 struct pid_namespace
*ns
= proc_pid_ns(file_inode(file
));
3376 loff_t pos
= ctx
->pos
;
3378 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3381 if (pos
== TGID_OFFSET
- 2) {
3382 struct inode
*inode
= d_inode(ns
->proc_self
);
3383 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3385 ctx
->pos
= pos
= pos
+ 1;
3387 if (pos
== TGID_OFFSET
- 1) {
3388 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3389 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3391 ctx
->pos
= pos
= pos
+ 1;
3393 iter
.tgid
= pos
- TGID_OFFSET
;
3395 for (iter
= next_tgid(ns
, iter
);
3397 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3402 if (!has_pid_permissions(ns
, iter
.task
, HIDEPID_INVISIBLE
))
3405 len
= snprintf(name
, sizeof(name
), "%u", iter
.tgid
);
3406 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3407 if (!proc_fill_cache(file
, ctx
, name
, len
,
3408 proc_pid_instantiate
, iter
.task
, NULL
)) {
3409 put_task_struct(iter
.task
);
3413 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3418 * proc_tid_comm_permission is a special permission function exclusively
3419 * used for the node /proc/<pid>/task/<tid>/comm.
3420 * It bypasses generic permission checks in the case where a task of the same
3421 * task group attempts to access the node.
3422 * The rationale behind this is that glibc and bionic access this node for
3423 * cross thread naming (pthread_set/getname_np(!self)). However, if
3424 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3425 * which locks out the cross thread naming implementation.
3426 * This function makes sure that the node is always accessible for members of
3427 * same thread group.
3429 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3431 bool is_same_tgroup
;
3432 struct task_struct
*task
;
3434 task
= get_proc_task(inode
);
3437 is_same_tgroup
= same_thread_group(current
, task
);
3438 put_task_struct(task
);
3440 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3441 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3442 * read or written by the members of the corresponding
3448 return generic_permission(inode
, mask
);
3451 static const struct inode_operations proc_tid_comm_inode_operations
= {
3452 .permission
= proc_tid_comm_permission
,
3458 static const struct pid_entry tid_base_stuff
[] = {
3459 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3460 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3461 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3463 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3465 REG("environ", S_IRUSR
, proc_environ_operations
),
3466 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3467 ONE("status", S_IRUGO
, proc_pid_status
),
3468 ONE("personality", S_IRUSR
, proc_pid_personality
),
3469 ONE("limits", S_IRUGO
, proc_pid_limits
),
3470 #ifdef CONFIG_SCHED_DEBUG
3471 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3473 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3474 &proc_tid_comm_inode_operations
,
3475 &proc_pid_set_comm_operations
, {}),
3476 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3477 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3479 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3480 ONE("stat", S_IRUGO
, proc_tid_stat
),
3481 ONE("statm", S_IRUGO
, proc_pid_statm
),
3482 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
3483 #ifdef CONFIG_PROC_CHILDREN
3484 REG("children", S_IRUGO
, proc_tid_children_operations
),
3487 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
3489 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3490 LNK("cwd", proc_cwd_link
),
3491 LNK("root", proc_root_link
),
3492 LNK("exe", proc_exe_link
),
3493 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3494 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3495 #ifdef CONFIG_PROC_PAGE_MONITOR
3496 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3497 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
3498 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
3499 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3501 #ifdef CONFIG_SECURITY
3502 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3504 #ifdef CONFIG_KALLSYMS
3505 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3507 #ifdef CONFIG_STACKTRACE
3508 ONE("stack", S_IRUSR
, proc_pid_stack
),
3510 #ifdef CONFIG_SCHED_INFO
3511 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3513 #ifdef CONFIG_LATENCYTOP
3514 REG("latency", S_IRUGO
, proc_lstats_operations
),
3516 #ifdef CONFIG_PROC_PID_CPUSET
3517 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3519 #ifdef CONFIG_CGROUPS
3520 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3522 #ifdef CONFIG_PROC_CPU_RESCTRL
3523 ONE("cpu_resctrl_groups", S_IRUGO
, proc_resctrl_show
),
3525 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3526 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3527 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3529 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3530 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3532 #ifdef CONFIG_FAULT_INJECTION
3533 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3534 REG("fail-nth", 0644, proc_fail_nth_operations
),
3536 #ifdef CONFIG_TASK_IO_ACCOUNTING
3537 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3539 #ifdef CONFIG_USER_NS
3540 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3541 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3542 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3543 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3545 #ifdef CONFIG_LIVEPATCH
3546 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3548 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3549 ONE("arch_status", S_IRUGO
, proc_pid_arch_status
),
3553 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3555 return proc_pident_readdir(file
, ctx
,
3556 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3559 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3561 return proc_pident_lookup(dir
, dentry
,
3563 tid_base_stuff
+ ARRAY_SIZE(tid_base_stuff
));
3566 static const struct file_operations proc_tid_base_operations
= {
3567 .read
= generic_read_dir
,
3568 .iterate_shared
= proc_tid_base_readdir
,
3569 .llseek
= generic_file_llseek
,
3572 static const struct inode_operations proc_tid_base_inode_operations
= {
3573 .lookup
= proc_tid_base_lookup
,
3574 .getattr
= pid_getattr
,
3575 .setattr
= proc_setattr
,
3578 static struct dentry
*proc_task_instantiate(struct dentry
*dentry
,
3579 struct task_struct
*task
, const void *ptr
)
3581 struct inode
*inode
;
3582 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3584 return ERR_PTR(-ENOENT
);
3586 inode
->i_op
= &proc_tid_base_inode_operations
;
3587 inode
->i_fop
= &proc_tid_base_operations
;
3588 inode
->i_flags
|= S_IMMUTABLE
;
3590 set_nlink(inode
, nlink_tid
);
3591 pid_update_inode(task
, inode
);
3593 d_set_d_op(dentry
, &pid_dentry_operations
);
3594 return d_splice_alias(inode
, dentry
);
3597 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3599 struct task_struct
*task
;
3600 struct task_struct
*leader
= get_proc_task(dir
);
3602 struct pid_namespace
*ns
;
3603 struct dentry
*result
= ERR_PTR(-ENOENT
);
3608 tid
= name_to_int(&dentry
->d_name
);
3612 ns
= dentry
->d_sb
->s_fs_info
;
3614 task
= find_task_by_pid_ns(tid
, ns
);
3616 get_task_struct(task
);
3620 if (!same_thread_group(leader
, task
))
3623 result
= proc_task_instantiate(dentry
, task
, NULL
);
3625 put_task_struct(task
);
3627 put_task_struct(leader
);
3633 * Find the first tid of a thread group to return to user space.
3635 * Usually this is just the thread group leader, but if the users
3636 * buffer was too small or there was a seek into the middle of the
3637 * directory we have more work todo.
3639 * In the case of a short read we start with find_task_by_pid.
3641 * In the case of a seek we start with the leader and walk nr
3644 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3645 struct pid_namespace
*ns
)
3647 struct task_struct
*pos
, *task
;
3648 unsigned long nr
= f_pos
;
3650 if (nr
!= f_pos
) /* 32bit overflow? */
3654 task
= pid_task(pid
, PIDTYPE_PID
);
3658 /* Attempt to start with the tid of a thread */
3660 pos
= find_task_by_pid_ns(tid
, ns
);
3661 if (pos
&& same_thread_group(pos
, task
))
3665 /* If nr exceeds the number of threads there is nothing todo */
3666 if (nr
>= get_nr_threads(task
))
3669 /* If we haven't found our starting place yet start
3670 * with the leader and walk nr threads forward.
3672 pos
= task
= task
->group_leader
;
3676 } while_each_thread(task
, pos
);
3681 get_task_struct(pos
);
3688 * Find the next thread in the thread list.
3689 * Return NULL if there is an error or no next thread.
3691 * The reference to the input task_struct is released.
3693 static struct task_struct
*next_tid(struct task_struct
*start
)
3695 struct task_struct
*pos
= NULL
;
3697 if (pid_alive(start
)) {
3698 pos
= next_thread(start
);
3699 if (thread_group_leader(pos
))
3702 get_task_struct(pos
);
3705 put_task_struct(start
);
3709 /* for the /proc/TGID/task/ directories */
3710 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3712 struct inode
*inode
= file_inode(file
);
3713 struct task_struct
*task
;
3714 struct pid_namespace
*ns
;
3717 if (proc_inode_is_dead(inode
))
3720 if (!dir_emit_dots(file
, ctx
))
3723 /* f_version caches the tgid value that the last readdir call couldn't
3724 * return. lseek aka telldir automagically resets f_version to 0.
3726 ns
= proc_pid_ns(inode
);
3727 tid
= (int)file
->f_version
;
3728 file
->f_version
= 0;
3729 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3731 task
= next_tid(task
), ctx
->pos
++) {
3734 tid
= task_pid_nr_ns(task
, ns
);
3735 len
= snprintf(name
, sizeof(name
), "%u", tid
);
3736 if (!proc_fill_cache(file
, ctx
, name
, len
,
3737 proc_task_instantiate
, task
, NULL
)) {
3738 /* returning this tgid failed, save it as the first
3739 * pid for the next readir call */
3740 file
->f_version
= (u64
)tid
;
3741 put_task_struct(task
);
3749 static int proc_task_getattr(const struct path
*path
, struct kstat
*stat
,
3750 u32 request_mask
, unsigned int query_flags
)
3752 struct inode
*inode
= d_inode(path
->dentry
);
3753 struct task_struct
*p
= get_proc_task(inode
);
3754 generic_fillattr(inode
, stat
);
3757 stat
->nlink
+= get_nr_threads(p
);
3764 static const struct inode_operations proc_task_inode_operations
= {
3765 .lookup
= proc_task_lookup
,
3766 .getattr
= proc_task_getattr
,
3767 .setattr
= proc_setattr
,
3768 .permission
= proc_pid_permission
,
3771 static const struct file_operations proc_task_operations
= {
3772 .read
= generic_read_dir
,
3773 .iterate_shared
= proc_task_readdir
,
3774 .llseek
= generic_file_llseek
,
3777 void __init
set_proc_pid_nlink(void)
3779 nlink_tid
= pid_entry_nlink(tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3780 nlink_tgid
= pid_entry_nlink(tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));