1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/proc/base.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * proc base directory handling functions
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
101da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object
111da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even
121da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too.
131da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the
141da177e4SLinus Torvalds * rest of procfs anymore.
15e070ad49SMauricio Lin *
16e070ad49SMauricio Lin *
17e070ad49SMauricio Lin * Changelog:
18e070ad49SMauricio Lin * 17-Jan-2005
19e070ad49SMauricio Lin * Allan Bezerra
20e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br>
21e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br>
22e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br>
23e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br>
24e070ad49SMauricio Lin *
25e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26e070ad49SMauricio Lin *
27e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the
28e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information
29e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e.,
30e070ad49SMauricio Lin * rss information for executables and library files.
31e070ad49SMauricio Lin * This additional information is useful for any tools that need to know
32e070ad49SMauricio Lin * about physical memory consumption for a process specific library.
33e070ad49SMauricio Lin *
34e070ad49SMauricio Lin * Changelog:
35e070ad49SMauricio Lin * 21-Feb-2005
36e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37e070ad49SMauricio Lin * Pud inclusion in the page table walking.
38e070ad49SMauricio Lin *
39e070ad49SMauricio Lin * ChangeLog:
40e070ad49SMauricio Lin * 10-Mar-2005
41e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT:
42e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins.
43e070ad49SMauricio Lin *
44e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>:
45e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages.
46e070ad49SMauricio Lin *
47e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>:
48e070ad49SMauricio Lin * Overall revision about smaps.
491da177e4SLinus Torvalds */
501da177e4SLinus Torvalds
517c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
521da177e4SLinus Torvalds
531da177e4SLinus Torvalds #include <linux/errno.h>
541da177e4SLinus Torvalds #include <linux/time.h>
551da177e4SLinus Torvalds #include <linux/proc_fs.h>
561da177e4SLinus Torvalds #include <linux/stat.h>
575995477aSAndrea Righi #include <linux/task_io_accounting_ops.h>
581da177e4SLinus Torvalds #include <linux/init.h>
5916f7e0feSRandy Dunlap #include <linux/capability.h>
601da177e4SLinus Torvalds #include <linux/file.h>
619f3acc31SAl Viro #include <linux/fdtable.h>
6294f8f3b0SKent Overstreet #include <linux/generic-radix-tree.h>
631da177e4SLinus Torvalds #include <linux/string.h>
641da177e4SLinus Torvalds #include <linux/seq_file.h>
651da177e4SLinus Torvalds #include <linux/namei.h>
666b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
671da177e4SLinus Torvalds #include <linux/mm.h>
68a63d83f4SDavid Rientjes #include <linux/swap.h>
69b835996fSDipankar Sarma #include <linux/rcupdate.h>
7054354c6aSKees Cook #include <linux/kallsyms.h>
712ec220e2SKen Chen #include <linux/stacktrace.h>
72d85f50d5SNeil Horman #include <linux/resource.h>
735096add8SKees Cook #include <linux/module.h>
741da177e4SLinus Torvalds #include <linux/mount.h>
751da177e4SLinus Torvalds #include <linux/security.h>
761da177e4SLinus Torvalds #include <linux/ptrace.h>
7787ebdc00SAndrew Morton #include <linux/printk.h>
78efb1a57dSAlexey Dobriyan #include <linux/cache.h>
79a424316cSPaul Menage #include <linux/cgroup.h>
801da177e4SLinus Torvalds #include <linux/cpuset.h>
811da177e4SLinus Torvalds #include <linux/audit.h>
825addc5ddSAl Viro #include <linux/poll.h>
831651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
848ac773b4SAlexey Dobriyan #include <linux/oom.h>
853cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8660347f67SPavel Emelyanov #include <linux/pid_namespace.h>
8722d917d8SEric W. Biederman #include <linux/user_namespace.h>
88*85525080SAdrian Ratiu #include <linux/fs_parser.h>
895ad4e53bSAl Viro #include <linux/fs_struct.h>
905a0e3ad6STejun Heo #include <linux/slab.h>
914eb5aaa3SIngo Molnar #include <linux/sched/autogroup.h>
926e84f315SIngo Molnar #include <linux/sched/mm.h>
93f7ccbae4SIngo Molnar #include <linux/sched/coredump.h>
94b17b0153SIngo Molnar #include <linux/sched/debug.h>
953905f9adSIngo Molnar #include <linux/sched/stat.h>
9648f6a7a5SPavel Emelyanov #include <linux/posix-timers.h>
9704a8682aSAndrei Vagin #include <linux/time_namespace.h>
98e79f15a4SChen Yu #include <linux/resctrl.h>
99c2f273ebSOhhoon Kwon #include <linux/cn_proc.h>
100d21077fbSStefan Roesch #include <linux/ksm.h>
10143d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
1021da177e4SLinus Torvalds #include "internal.h"
103faf60af1SCyrill Gorcunov #include "fd.h"
1041da177e4SLinus Torvalds
105ac7f1061SAlexey Dobriyan #include "../../lib/kstrtox.h"
106ac7f1061SAlexey Dobriyan
1070f2fe20fSEric W. Biederman /* NOTE:
1080f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost
1090f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during
1100f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of
1110f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime.
1120f2fe20fSEric W. Biederman *
1130f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors
1140f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable.
1150f2fe20fSEric W. Biederman */
1160f2fe20fSEric W. Biederman
117efb1a57dSAlexey Dobriyan static u8 nlink_tid __ro_after_init;
118efb1a57dSAlexey Dobriyan static u8 nlink_tgid __ro_after_init;
1191270dd8dSAlexey Dobriyan
120*85525080SAdrian Ratiu enum proc_mem_force {
121*85525080SAdrian Ratiu PROC_MEM_FORCE_ALWAYS,
122*85525080SAdrian Ratiu PROC_MEM_FORCE_PTRACE,
123*85525080SAdrian Ratiu PROC_MEM_FORCE_NEVER
124*85525080SAdrian Ratiu };
125*85525080SAdrian Ratiu
126*85525080SAdrian Ratiu static enum proc_mem_force proc_mem_force_override __ro_after_init =
127*85525080SAdrian Ratiu IS_ENABLED(CONFIG_PROC_MEM_NO_FORCE) ? PROC_MEM_FORCE_NEVER :
128*85525080SAdrian Ratiu IS_ENABLED(CONFIG_PROC_MEM_FORCE_PTRACE) ? PROC_MEM_FORCE_PTRACE :
129*85525080SAdrian Ratiu PROC_MEM_FORCE_ALWAYS;
130*85525080SAdrian Ratiu
131*85525080SAdrian Ratiu static const struct constant_table proc_mem_force_table[] __initconst = {
132*85525080SAdrian Ratiu { "always", PROC_MEM_FORCE_ALWAYS },
133*85525080SAdrian Ratiu { "ptrace", PROC_MEM_FORCE_PTRACE },
134*85525080SAdrian Ratiu { "never", PROC_MEM_FORCE_NEVER },
135*85525080SAdrian Ratiu { }
136*85525080SAdrian Ratiu };
137*85525080SAdrian Ratiu
early_proc_mem_force_override(char * buf)138*85525080SAdrian Ratiu static int __init early_proc_mem_force_override(char *buf)
139*85525080SAdrian Ratiu {
140*85525080SAdrian Ratiu if (!buf)
141*85525080SAdrian Ratiu return -EINVAL;
142*85525080SAdrian Ratiu
143*85525080SAdrian Ratiu /*
144*85525080SAdrian Ratiu * lookup_constant() defaults to proc_mem_force_override to preseve
145*85525080SAdrian Ratiu * the initial Kconfig choice in case an invalid param gets passed.
146*85525080SAdrian Ratiu */
147*85525080SAdrian Ratiu proc_mem_force_override = lookup_constant(proc_mem_force_table,
148*85525080SAdrian Ratiu buf, proc_mem_force_override);
149*85525080SAdrian Ratiu
150*85525080SAdrian Ratiu return 0;
151*85525080SAdrian Ratiu }
152*85525080SAdrian Ratiu early_param("proc_mem.force_override", early_proc_mem_force_override);
153*85525080SAdrian Ratiu
1541da177e4SLinus Torvalds struct pid_entry {
155cedbccabSAlexey Dobriyan const char *name;
156623f594eSAlexey Dobriyan unsigned int len;
157d161a13fSAl Viro umode_t mode;
158c5ef1c42SArjan van de Ven const struct inode_operations *iop;
15900977a59SArjan van de Ven const struct file_operations *fop;
16020cdc894SEric W. Biederman union proc_op op;
1611da177e4SLinus Torvalds };
1621da177e4SLinus Torvalds
16361a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \
16420cdc894SEric W. Biederman .name = (NAME), \
165c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \
16620cdc894SEric W. Biederman .mode = MODE, \
16720cdc894SEric W. Biederman .iop = IOP, \
16820cdc894SEric W. Biederman .fop = FOP, \
16920cdc894SEric W. Biederman .op = OP, \
17020cdc894SEric W. Biederman }
17120cdc894SEric W. Biederman
172631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \
173631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
174631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \
17561a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \
17620cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \
177631f9c18SAlexey Dobriyan { .proc_get_link = get_link } )
178631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \
179631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
180631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \
181be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \
182be614086SEric W. Biederman NULL, &proc_single_file_operations, \
183631f9c18SAlexey Dobriyan { .proc_show = show } )
1846d9c939dSCasey Schaufler #define ATTR(LSM, NAME, MODE) \
1856d9c939dSCasey Schaufler NOD(NAME, (S_IFREG|(MODE)), \
1866d9c939dSCasey Schaufler NULL, &proc_pid_attr_operations, \
1876d9c939dSCasey Schaufler { .lsm = LSM })
1881da177e4SLinus Torvalds
189aed54175SVegard Nossum /*
190aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the .
191aed54175SVegard Nossum * and .. links.
192aed54175SVegard Nossum */
pid_entry_nlink(const struct pid_entry * entries,unsigned int n)1931270dd8dSAlexey Dobriyan static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
194aed54175SVegard Nossum unsigned int n)
195aed54175SVegard Nossum {
196aed54175SVegard Nossum unsigned int i;
197aed54175SVegard Nossum unsigned int count;
198aed54175SVegard Nossum
1991270dd8dSAlexey Dobriyan count = 2;
200aed54175SVegard Nossum for (i = 0; i < n; ++i) {
201aed54175SVegard Nossum if (S_ISDIR(entries[i].mode))
202aed54175SVegard Nossum ++count;
203aed54175SVegard Nossum }
204aed54175SVegard Nossum
205aed54175SVegard Nossum return count;
206aed54175SVegard Nossum }
207aed54175SVegard Nossum
get_task_root(struct task_struct * task,struct path * root)208f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
2091da177e4SLinus Torvalds {
2107c2c7d99SHugh Dickins int result = -ENOENT;
2117c2c7d99SHugh Dickins
2120494f6ecSMiklos Szeredi task_lock(task);
213f7ad3c6bSMiklos Szeredi if (task->fs) {
214f7ad3c6bSMiklos Szeredi get_fs_root(task->fs, root);
2157c2c7d99SHugh Dickins result = 0;
2167c2c7d99SHugh Dickins }
2170494f6ecSMiklos Szeredi task_unlock(task);
2187c2c7d99SHugh Dickins return result;
2190494f6ecSMiklos Szeredi }
2200494f6ecSMiklos Szeredi
proc_cwd_link(struct dentry * dentry,struct path * path)2217773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
2220494f6ecSMiklos Szeredi {
2232b0143b5SDavid Howells struct task_struct *task = get_proc_task(d_inode(dentry));
2240494f6ecSMiklos Szeredi int result = -ENOENT;
22599f89551SEric W. Biederman
22699f89551SEric W. Biederman if (task) {
227f7ad3c6bSMiklos Szeredi task_lock(task);
228f7ad3c6bSMiklos Szeredi if (task->fs) {
229f7ad3c6bSMiklos Szeredi get_fs_pwd(task->fs, path);
230f7ad3c6bSMiklos Szeredi result = 0;
231f7ad3c6bSMiklos Szeredi }
232f7ad3c6bSMiklos Szeredi task_unlock(task);
23399f89551SEric W. Biederman put_task_struct(task);
23499f89551SEric W. Biederman }
2351da177e4SLinus Torvalds return result;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds
proc_root_link(struct dentry * dentry,struct path * path)2387773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
2391da177e4SLinus Torvalds {
2402b0143b5SDavid Howells struct task_struct *task = get_proc_task(d_inode(dentry));
2411da177e4SLinus Torvalds int result = -ENOENT;
24299f89551SEric W. Biederman
24399f89551SEric W. Biederman if (task) {
244f7ad3c6bSMiklos Szeredi result = get_task_root(task, path);
24599f89551SEric W. Biederman put_task_struct(task);
24699f89551SEric W. Biederman }
2471da177e4SLinus Torvalds return result;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds
250d26d0cd9SLinus Torvalds /*
251d26d0cd9SLinus Torvalds * If the user used setproctitle(), we just get the string from
252d26d0cd9SLinus Torvalds * user space at arg_start, and limit it to a maximum of one page.
253d26d0cd9SLinus Torvalds */
get_mm_proctitle(struct mm_struct * mm,char __user * buf,size_t count,unsigned long pos,unsigned long arg_start)254d26d0cd9SLinus Torvalds static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
255d26d0cd9SLinus Torvalds size_t count, unsigned long pos,
256d26d0cd9SLinus Torvalds unsigned long arg_start)
257d26d0cd9SLinus Torvalds {
258d26d0cd9SLinus Torvalds char *page;
259d26d0cd9SLinus Torvalds int ret, got;
260d26d0cd9SLinus Torvalds
261d26d0cd9SLinus Torvalds if (pos >= PAGE_SIZE)
262d26d0cd9SLinus Torvalds return 0;
263d26d0cd9SLinus Torvalds
264d26d0cd9SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL);
265d26d0cd9SLinus Torvalds if (!page)
266d26d0cd9SLinus Torvalds return -ENOMEM;
267d26d0cd9SLinus Torvalds
268d26d0cd9SLinus Torvalds ret = 0;
269d26d0cd9SLinus Torvalds got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
270d26d0cd9SLinus Torvalds if (got > 0) {
271d26d0cd9SLinus Torvalds int len = strnlen(page, got);
272d26d0cd9SLinus Torvalds
273d26d0cd9SLinus Torvalds /* Include the NUL character if it was found */
274d26d0cd9SLinus Torvalds if (len < got)
275d26d0cd9SLinus Torvalds len++;
276d26d0cd9SLinus Torvalds
277d26d0cd9SLinus Torvalds if (len > pos) {
278d26d0cd9SLinus Torvalds len -= pos;
279d26d0cd9SLinus Torvalds if (len > count)
280d26d0cd9SLinus Torvalds len = count;
281d26d0cd9SLinus Torvalds len -= copy_to_user(buf, page+pos, len);
282d26d0cd9SLinus Torvalds if (!len)
283d26d0cd9SLinus Torvalds len = -EFAULT;
284d26d0cd9SLinus Torvalds ret = len;
285d26d0cd9SLinus Torvalds }
286d26d0cd9SLinus Torvalds }
287d26d0cd9SLinus Torvalds free_page((unsigned long)page);
288d26d0cd9SLinus Torvalds return ret;
289d26d0cd9SLinus Torvalds }
290d26d0cd9SLinus Torvalds
get_mm_cmdline(struct mm_struct * mm,char __user * buf,size_t count,loff_t * ppos)291e4b4e441SLinus Torvalds static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
2925ab82718SLinus Torvalds size_t count, loff_t *ppos)
2931da177e4SLinus Torvalds {
294c2c0bb44SAlexey Dobriyan unsigned long arg_start, arg_end, env_start, env_end;
2955ab82718SLinus Torvalds unsigned long pos, len;
296d26d0cd9SLinus Torvalds char *page, c;
297c2c0bb44SAlexey Dobriyan
298c2c0bb44SAlexey Dobriyan /* Check if process spawned far enough to have cmdline. */
299e4b4e441SLinus Torvalds if (!mm->env_end)
300e4b4e441SLinus Torvalds return 0;
301c2c0bb44SAlexey Dobriyan
30288aa7cc6SYang Shi spin_lock(&mm->arg_lock);
303c2c0bb44SAlexey Dobriyan arg_start = mm->arg_start;
304c2c0bb44SAlexey Dobriyan arg_end = mm->arg_end;
305c2c0bb44SAlexey Dobriyan env_start = mm->env_start;
306c2c0bb44SAlexey Dobriyan env_end = mm->env_end;
30788aa7cc6SYang Shi spin_unlock(&mm->arg_lock);
308c2c0bb44SAlexey Dobriyan
3095ab82718SLinus Torvalds if (arg_start >= arg_end)
3105ab82718SLinus Torvalds return 0;
3116a6cbe75SAlexey Dobriyan
312c2c0bb44SAlexey Dobriyan /*
313d26d0cd9SLinus Torvalds * We allow setproctitle() to overwrite the argument
314d26d0cd9SLinus Torvalds * strings, and overflow past the original end. But
315d26d0cd9SLinus Torvalds * only when it overflows into the environment area.
316c2c0bb44SAlexey Dobriyan */
317d26d0cd9SLinus Torvalds if (env_start != arg_end || env_end < env_start)
3185ab82718SLinus Torvalds env_start = env_end = arg_end;
319d26d0cd9SLinus Torvalds len = env_end - arg_start;
320f5b65348SLinus Torvalds
3215ab82718SLinus Torvalds /* We're not going to care if "*ppos" has high bits set */
322d26d0cd9SLinus Torvalds pos = *ppos;
323d26d0cd9SLinus Torvalds if (pos >= len)
324d26d0cd9SLinus Torvalds return 0;
325d26d0cd9SLinus Torvalds if (count > len - pos)
326d26d0cd9SLinus Torvalds count = len - pos;
327d26d0cd9SLinus Torvalds if (!count)
3285ab82718SLinus Torvalds return 0;
329c2c0bb44SAlexey Dobriyan
330d26d0cd9SLinus Torvalds /*
331d26d0cd9SLinus Torvalds * Magical special case: if the argv[] end byte is not
332d26d0cd9SLinus Torvalds * zero, the user has overwritten it with setproctitle(3).
333d26d0cd9SLinus Torvalds *
334d26d0cd9SLinus Torvalds * Possible future enhancement: do this only once when
335d26d0cd9SLinus Torvalds * pos is 0, and set a flag in the 'struct file'.
336d26d0cd9SLinus Torvalds */
337d26d0cd9SLinus Torvalds if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
338d26d0cd9SLinus Torvalds return get_mm_proctitle(mm, buf, count, pos, arg_start);
339d26d0cd9SLinus Torvalds
340d26d0cd9SLinus Torvalds /*
341d26d0cd9SLinus Torvalds * For the non-setproctitle() case we limit things strictly
342d26d0cd9SLinus Torvalds * to the [arg_start, arg_end[ range.
343d26d0cd9SLinus Torvalds */
344d26d0cd9SLinus Torvalds pos += arg_start;
3453d712546SLinus Torvalds if (pos < arg_start || pos >= arg_end)
346c2c0bb44SAlexey Dobriyan return 0;
3473d712546SLinus Torvalds if (count > arg_end - pos)
3483d712546SLinus Torvalds count = arg_end - pos;
3493cb4e162SAlexey Dobriyan
3505ab82718SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL);
3515ab82718SLinus Torvalds if (!page)
3525ab82718SLinus Torvalds return -ENOMEM;
353c2c0bb44SAlexey Dobriyan
3545ab82718SLinus Torvalds len = 0;
3555ab82718SLinus Torvalds while (count) {
3565ab82718SLinus Torvalds int got;
3575ab82718SLinus Torvalds size_t size = min_t(size_t, PAGE_SIZE, count);
3585ab82718SLinus Torvalds
3593d712546SLinus Torvalds got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
3603d712546SLinus Torvalds if (got <= 0)
3615ab82718SLinus Torvalds break;
3623d712546SLinus Torvalds got -= copy_to_user(buf, page, got);
3635ab82718SLinus Torvalds if (unlikely(!got)) {
3645ab82718SLinus Torvalds if (!len)
3655ab82718SLinus Torvalds len = -EFAULT;
3665ab82718SLinus Torvalds break;
3675ab82718SLinus Torvalds }
3685ab82718SLinus Torvalds pos += got;
3695ab82718SLinus Torvalds buf += got;
3705ab82718SLinus Torvalds len += got;
3715ab82718SLinus Torvalds count -= got;
372c2c0bb44SAlexey Dobriyan }
373a0a07b87SAlexey Dobriyan
374c2c0bb44SAlexey Dobriyan free_page((unsigned long)page);
3755ab82718SLinus Torvalds return len;
376c2c0bb44SAlexey Dobriyan }
377c2c0bb44SAlexey Dobriyan
get_task_cmdline(struct task_struct * tsk,char __user * buf,size_t count,loff_t * pos)378e4b4e441SLinus Torvalds static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
379e4b4e441SLinus Torvalds size_t count, loff_t *pos)
380e4b4e441SLinus Torvalds {
381e4b4e441SLinus Torvalds struct mm_struct *mm;
382e4b4e441SLinus Torvalds ssize_t ret;
383e4b4e441SLinus Torvalds
384e4b4e441SLinus Torvalds mm = get_task_mm(tsk);
385e4b4e441SLinus Torvalds if (!mm)
386e4b4e441SLinus Torvalds return 0;
387e4b4e441SLinus Torvalds
388e4b4e441SLinus Torvalds ret = get_mm_cmdline(mm, buf, count, pos);
389c2c0bb44SAlexey Dobriyan mmput(mm);
390e4b4e441SLinus Torvalds return ret;
391e4b4e441SLinus Torvalds }
392e4b4e441SLinus Torvalds
proc_pid_cmdline_read(struct file * file,char __user * buf,size_t count,loff_t * pos)393e4b4e441SLinus Torvalds static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
394e4b4e441SLinus Torvalds size_t count, loff_t *pos)
395e4b4e441SLinus Torvalds {
396e4b4e441SLinus Torvalds struct task_struct *tsk;
397e4b4e441SLinus Torvalds ssize_t ret;
398e4b4e441SLinus Torvalds
399e4b4e441SLinus Torvalds BUG_ON(*pos < 0);
400e4b4e441SLinus Torvalds
401e4b4e441SLinus Torvalds tsk = get_proc_task(file_inode(file));
402e4b4e441SLinus Torvalds if (!tsk)
403e4b4e441SLinus Torvalds return -ESRCH;
404e4b4e441SLinus Torvalds ret = get_task_cmdline(tsk, buf, count, pos);
405e4b4e441SLinus Torvalds put_task_struct(tsk);
406e4b4e441SLinus Torvalds if (ret > 0)
407e4b4e441SLinus Torvalds *pos += ret;
408e4b4e441SLinus Torvalds return ret;
409c2c0bb44SAlexey Dobriyan }
410c2c0bb44SAlexey Dobriyan
411c2c0bb44SAlexey Dobriyan static const struct file_operations proc_pid_cmdline_ops = {
412c2c0bb44SAlexey Dobriyan .read = proc_pid_cmdline_read,
413c2c0bb44SAlexey Dobriyan .llseek = generic_file_llseek,
414c2c0bb44SAlexey Dobriyan };
415c2c0bb44SAlexey Dobriyan
4161da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
4171da177e4SLinus Torvalds /*
4181da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format.
4191da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address.
4201da177e4SLinus Torvalds */
proc_pid_wchan(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)421edfcd606SAlexey Dobriyan static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
422edfcd606SAlexey Dobriyan struct pid *pid, struct task_struct *task)
4231da177e4SLinus Torvalds {
424ffb45122SAlexey Dobriyan unsigned long wchan;
42554354c6aSKees Cook char symname[KSYM_NAME_LEN];
4261da177e4SLinus Torvalds
42754354c6aSKees Cook if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
42854354c6aSKees Cook goto print0;
42954354c6aSKees Cook
4301da177e4SLinus Torvalds wchan = get_wchan(task);
43154354c6aSKees Cook if (wchan && !lookup_symbol_name(wchan, symname)) {
43254354c6aSKees Cook seq_puts(m, symname);
43354354c6aSKees Cook return 0;
43454354c6aSKees Cook }
43525ce3191SJoe Perches
43654354c6aSKees Cook print0:
43724b2ec21SAlexey Dobriyan seq_putc(m, '0');
43825ce3191SJoe Perches return 0;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
4411da177e4SLinus Torvalds
lock_trace(struct task_struct * task)442a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
443a9712bc1SAl Viro {
444f7cfd871SEric W. Biederman int err = down_read_killable(&task->signal->exec_update_lock);
445a9712bc1SAl Viro if (err)
446a9712bc1SAl Viro return err;
447caaee623SJann Horn if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
448f7cfd871SEric W. Biederman up_read(&task->signal->exec_update_lock);
449a9712bc1SAl Viro return -EPERM;
450a9712bc1SAl Viro }
451a9712bc1SAl Viro return 0;
452a9712bc1SAl Viro }
453a9712bc1SAl Viro
unlock_trace(struct task_struct * task)454a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
455a9712bc1SAl Viro {
456f7cfd871SEric W. Biederman up_read(&task->signal->exec_update_lock);
457a9712bc1SAl Viro }
458a9712bc1SAl Viro
4592ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
4602ec220e2SKen Chen
4612ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64
4622ec220e2SKen Chen
proc_pid_stack(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)4632ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
4642ec220e2SKen Chen struct pid *pid, struct task_struct *task)
4652ec220e2SKen Chen {
4662ec220e2SKen Chen unsigned long *entries;
467a9712bc1SAl Viro int err;
4682ec220e2SKen Chen
469f8a00cefSJann Horn /*
470f8a00cefSJann Horn * The ability to racily run the kernel stack unwinder on a running task
471f8a00cefSJann Horn * and then observe the unwinder output is scary; while it is useful for
472f8a00cefSJann Horn * debugging kernel issues, it can also allow an attacker to leak kernel
473f8a00cefSJann Horn * stack contents.
474f8a00cefSJann Horn * Doing this in a manner that is at least safe from races would require
475f8a00cefSJann Horn * some work to ensure that the remote task can not be scheduled; and
476f8a00cefSJann Horn * even then, this would still expose the unwinder as local attack
477f8a00cefSJann Horn * surface.
478f8a00cefSJann Horn * Therefore, this interface is restricted to root.
479f8a00cefSJann Horn */
480f8a00cefSJann Horn if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
481f8a00cefSJann Horn return -EACCES;
482f8a00cefSJann Horn
4836da2ec56SKees Cook entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
4846da2ec56SKees Cook GFP_KERNEL);
4852ec220e2SKen Chen if (!entries)
4862ec220e2SKen Chen return -ENOMEM;
4872ec220e2SKen Chen
488a9712bc1SAl Viro err = lock_trace(task);
489a9712bc1SAl Viro if (!err) {
490e988e5ecSThomas Gleixner unsigned int i, nr_entries;
4915d008fb4SAlexey Dobriyan
492e988e5ecSThomas Gleixner nr_entries = stack_trace_save_tsk(task, entries,
493e988e5ecSThomas Gleixner MAX_STACK_TRACE_DEPTH, 0);
4942ec220e2SKen Chen
495e988e5ecSThomas Gleixner for (i = 0; i < nr_entries; i++) {
4968f5abe84SLinus Torvalds seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
4972ec220e2SKen Chen }
498e988e5ecSThomas Gleixner
499a9712bc1SAl Viro unlock_trace(task);
500a9712bc1SAl Viro }
5012ec220e2SKen Chen kfree(entries);
5022ec220e2SKen Chen
503a9712bc1SAl Viro return err;
5042ec220e2SKen Chen }
5052ec220e2SKen Chen #endif
5062ec220e2SKen Chen
5075968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
5081da177e4SLinus Torvalds /*
5091da177e4SLinus Torvalds * Provides /proc/PID/schedstat
5101da177e4SLinus Torvalds */
proc_pid_schedstat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)511f6e826caSAlexey Dobriyan static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
512f6e826caSAlexey Dobriyan struct pid *pid, struct task_struct *task)
5131da177e4SLinus Torvalds {
5145968ceceSNaveen N. Rao if (unlikely(!sched_info_on()))
51508b55775SAlexey Dobriyan seq_puts(m, "0 0 0\n");
5165968ceceSNaveen N. Rao else
51725ce3191SJoe Perches seq_printf(m, "%llu %llu %lu\n",
518826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime,
519826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay,
5202d72376bSIngo Molnar task->sched_info.pcount);
52125ce3191SJoe Perches
52225ce3191SJoe Perches return 0;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds #endif
5251da177e4SLinus Torvalds
5269745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
lstats_show_proc(struct seq_file * m,void * v)5279745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
5289745512cSArjan van de Ven {
5299745512cSArjan van de Ven int i;
53013d77c37SHiroshi Shimamoto struct inode *inode = m->private;
53113d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode);
5329745512cSArjan van de Ven
53313d77c37SHiroshi Shimamoto if (!task)
53413d77c37SHiroshi Shimamoto return -ESRCH;
53513d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n");
536f6d2f584SAlexey Dobriyan for (i = 0; i < LT_SAVECOUNT; i++) {
53734e49d4fSJoe Perches struct latency_record *lr = &task->latency_record[i];
53834e49d4fSJoe Perches if (lr->backtrace[0]) {
5399745512cSArjan van de Ven int q;
5409745512cSArjan van de Ven seq_printf(m, "%i %li %li",
54134e49d4fSJoe Perches lr->count, lr->time, lr->max);
5429745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
54334e49d4fSJoe Perches unsigned long bt = lr->backtrace[q];
544accddc41SThomas Gleixner
54534e49d4fSJoe Perches if (!bt)
5469745512cSArjan van de Ven break;
54734e49d4fSJoe Perches seq_printf(m, " %ps", (void *)bt);
5489745512cSArjan van de Ven }
5499d6de12fSAlexey Dobriyan seq_putc(m, '\n');
5509745512cSArjan van de Ven }
5519745512cSArjan van de Ven
5529745512cSArjan van de Ven }
55313d77c37SHiroshi Shimamoto put_task_struct(task);
5549745512cSArjan van de Ven return 0;
5559745512cSArjan van de Ven }
5569745512cSArjan van de Ven
lstats_open(struct inode * inode,struct file * file)5579745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
5589745512cSArjan van de Ven {
55913d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode);
560d6643d12SHiroshi Shimamoto }
561d6643d12SHiroshi Shimamoto
lstats_write(struct file * file,const char __user * buf,size_t count,loff_t * offs)5629745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
5639745512cSArjan van de Ven size_t count, loff_t *offs)
5649745512cSArjan van de Ven {
565496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file));
5669745512cSArjan van de Ven
56713d77c37SHiroshi Shimamoto if (!task)
56813d77c37SHiroshi Shimamoto return -ESRCH;
569e02c9b0dSLin Feng clear_tsk_latency_tracing(task);
57013d77c37SHiroshi Shimamoto put_task_struct(task);
5719745512cSArjan van de Ven
5729745512cSArjan van de Ven return count;
5739745512cSArjan van de Ven }
5749745512cSArjan van de Ven
5759745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
5769745512cSArjan van de Ven .open = lstats_open,
5779745512cSArjan van de Ven .read = seq_read,
5789745512cSArjan van de Ven .write = lstats_write,
5799745512cSArjan van de Ven .llseek = seq_lseek,
58013d77c37SHiroshi Shimamoto .release = single_release,
5819745512cSArjan van de Ven };
5829745512cSArjan van de Ven
5839745512cSArjan van de Ven #endif
5849745512cSArjan van de Ven
proc_oom_score(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)5856ba51e37SAlexey Dobriyan static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
5866ba51e37SAlexey Dobriyan struct pid *pid, struct task_struct *task)
5871da177e4SLinus Torvalds {
588ca79b0c2SArun KS unsigned long totalpages = totalram_pages() + total_swap_pages;
589b95c35e7SOleg Nesterov unsigned long points = 0;
5909066e5cfSYafang Shao long badness;
5911da177e4SLinus Torvalds
5929066e5cfSYafang Shao badness = oom_badness(task, totalpages);
5939066e5cfSYafang Shao /*
5949066e5cfSYafang Shao * Special case OOM_SCORE_ADJ_MIN for all others scale the
5959066e5cfSYafang Shao * badness value into [0, 2000] range which we have been
5969066e5cfSYafang Shao * exporting for a long time so userspace might depend on it.
5979066e5cfSYafang Shao */
5989066e5cfSYafang Shao if (badness != LONG_MIN)
5999066e5cfSYafang Shao points = (1000 + badness * 1000 / (long)totalpages) * 2 / 3;
6009066e5cfSYafang Shao
60125ce3191SJoe Perches seq_printf(m, "%lu\n", points);
60225ce3191SJoe Perches
60325ce3191SJoe Perches return 0;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds
606d85f50d5SNeil Horman struct limit_names {
607cedbccabSAlexey Dobriyan const char *name;
608cedbccabSAlexey Dobriyan const char *unit;
609d85f50d5SNeil Horman };
610d85f50d5SNeil Horman
611d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
612cff4edb5SKees Cook [RLIMIT_CPU] = {"Max cpu time", "seconds"},
613d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"},
614d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"},
615d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"},
616d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"},
617d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"},
618d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"},
619d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"},
620d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
621d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"},
622d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"},
623d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
624d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
625d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL},
626d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
6278808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
628d85f50d5SNeil Horman };
629d85f50d5SNeil Horman
630d85f50d5SNeil Horman /* Display limits for a process */
proc_pid_limits(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)6311c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
6321c963eb1SAlexey Dobriyan struct pid *pid, struct task_struct *task)
633d85f50d5SNeil Horman {
634d85f50d5SNeil Horman unsigned int i;
635d85f50d5SNeil Horman unsigned long flags;
636d85f50d5SNeil Horman
637d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS];
638d85f50d5SNeil Horman
639a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags))
640d85f50d5SNeil Horman return 0;
641d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
642d85f50d5SNeil Horman unlock_task_sighand(task, &flags);
643d85f50d5SNeil Horman
644d85f50d5SNeil Horman /*
645d85f50d5SNeil Horman * print the file header
646d85f50d5SNeil Horman */
647afe922c2SAlexey Dobriyan seq_puts(m, "Limit "
648afe922c2SAlexey Dobriyan "Soft Limit "
649afe922c2SAlexey Dobriyan "Hard Limit "
650afe922c2SAlexey Dobriyan "Units \n");
651d85f50d5SNeil Horman
652d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) {
653d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY)
6541c963eb1SAlexey Dobriyan seq_printf(m, "%-25s %-20s ",
655d85f50d5SNeil Horman lnames[i].name, "unlimited");
656d85f50d5SNeil Horman else
6571c963eb1SAlexey Dobriyan seq_printf(m, "%-25s %-20lu ",
658d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur);
659d85f50d5SNeil Horman
660d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY)
6611c963eb1SAlexey Dobriyan seq_printf(m, "%-20s ", "unlimited");
662d85f50d5SNeil Horman else
6631c963eb1SAlexey Dobriyan seq_printf(m, "%-20lu ", rlim[i].rlim_max);
664d85f50d5SNeil Horman
665d85f50d5SNeil Horman if (lnames[i].unit)
6661c963eb1SAlexey Dobriyan seq_printf(m, "%-10s\n", lnames[i].unit);
667d85f50d5SNeil Horman else
6681c963eb1SAlexey Dobriyan seq_putc(m, '\n');
669d85f50d5SNeil Horman }
670d85f50d5SNeil Horman
6711c963eb1SAlexey Dobriyan return 0;
672d85f50d5SNeil Horman }
673d85f50d5SNeil Horman
674ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
proc_pid_syscall(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)67509d93bd6SAlexey Dobriyan static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
67609d93bd6SAlexey Dobriyan struct pid *pid, struct task_struct *task)
677ebcb6734SRoland McGrath {
678631b7abaSSteven Rostedt (Red Hat) struct syscall_info info;
679631b7abaSSteven Rostedt (Red Hat) u64 *args = &info.data.args[0];
68025ce3191SJoe Perches int res;
68125ce3191SJoe Perches
68225ce3191SJoe Perches res = lock_trace(task);
683a9712bc1SAl Viro if (res)
684a9712bc1SAl Viro return res;
685ebcb6734SRoland McGrath
686631b7abaSSteven Rostedt (Red Hat) if (task_current_syscall(task, &info))
68709d93bd6SAlexey Dobriyan seq_puts(m, "running\n");
688631b7abaSSteven Rostedt (Red Hat) else if (info.data.nr < 0)
689631b7abaSSteven Rostedt (Red Hat) seq_printf(m, "%d 0x%llx 0x%llx\n",
690631b7abaSSteven Rostedt (Red Hat) info.data.nr, info.sp, info.data.instruction_pointer);
691a9712bc1SAl Viro else
69209d93bd6SAlexey Dobriyan seq_printf(m,
693631b7abaSSteven Rostedt (Red Hat) "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
694631b7abaSSteven Rostedt (Red Hat) info.data.nr,
695ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5],
696631b7abaSSteven Rostedt (Red Hat) info.sp, info.data.instruction_pointer);
697a9712bc1SAl Viro unlock_trace(task);
69825ce3191SJoe Perches
69925ce3191SJoe Perches return 0;
700ebcb6734SRoland McGrath }
701ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
702ebcb6734SRoland McGrath
7031da177e4SLinus Torvalds /************************************************************************/
7041da177e4SLinus Torvalds /* Here the fs part begins */
7051da177e4SLinus Torvalds /************************************************************************/
7061da177e4SLinus Torvalds
7071da177e4SLinus Torvalds /* permission checks */
proc_fd_access_allowed(struct inode * inode)70851a18734SQi Zheng static bool proc_fd_access_allowed(struct inode *inode)
7091da177e4SLinus Torvalds {
710778c1144SEric W. Biederman struct task_struct *task;
71151a18734SQi Zheng bool allowed = false;
712df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we
713df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that
714df26c40eSEric W. Biederman * information.
715778c1144SEric W. Biederman */
716778c1144SEric W. Biederman task = get_proc_task(inode);
717df26c40eSEric W. Biederman if (task) {
718caaee623SJann Horn allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
719778c1144SEric W. Biederman put_task_struct(task);
720df26c40eSEric W. Biederman }
721778c1144SEric W. Biederman return allowed;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds
proc_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)724c1632a0fSChristian Brauner int proc_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
725549c7297SChristian Brauner struct iattr *attr)
7266d76fa58SLinus Torvalds {
7276d76fa58SLinus Torvalds int error;
7282b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
7296d76fa58SLinus Torvalds
7306d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE)
7316d76fa58SLinus Torvalds return -EPERM;
7326d76fa58SLinus Torvalds
733c1632a0fSChristian Brauner error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
7341025774cSChristoph Hellwig if (error)
7356d76fa58SLinus Torvalds return error;
7361025774cSChristoph Hellwig
737c1632a0fSChristian Brauner setattr_copy(&nop_mnt_idmap, inode, attr);
7381025774cSChristoph Hellwig return 0;
7396d76fa58SLinus Torvalds }
7406d76fa58SLinus Torvalds
7410499680aSVasiliy Kulikov /*
7420499680aSVasiliy Kulikov * May current process learn task's sched/cmdline info (for hide_pid_min=1)
7430499680aSVasiliy Kulikov * or euid/egid (for hide_pid_min=2)?
7440499680aSVasiliy Kulikov */
has_pid_permissions(struct proc_fs_info * fs_info,struct task_struct * task,enum proc_hidepid hide_pid_min)745fa10fed3SAlexey Gladkov static bool has_pid_permissions(struct proc_fs_info *fs_info,
7460499680aSVasiliy Kulikov struct task_struct *task,
747e61bb8b3SAlexey Gladkov enum proc_hidepid hide_pid_min)
7480499680aSVasiliy Kulikov {
74924a71ce5SAlexey Gladkov /*
75024a71ce5SAlexey Gladkov * If 'hidpid' mount option is set force a ptrace check,
75124a71ce5SAlexey Gladkov * we indicate that we are using a filesystem syscall
75224a71ce5SAlexey Gladkov * by passing PTRACE_MODE_READ_FSCREDS
75324a71ce5SAlexey Gladkov */
75424a71ce5SAlexey Gladkov if (fs_info->hide_pid == HIDEPID_NOT_PTRACEABLE)
75524a71ce5SAlexey Gladkov return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
75624a71ce5SAlexey Gladkov
757fa10fed3SAlexey Gladkov if (fs_info->hide_pid < hide_pid_min)
7580499680aSVasiliy Kulikov return true;
759fa10fed3SAlexey Gladkov if (in_group_p(fs_info->pid_gid))
7600499680aSVasiliy Kulikov return true;
761caaee623SJann Horn return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
7620499680aSVasiliy Kulikov }
7630499680aSVasiliy Kulikov
7640499680aSVasiliy Kulikov
proc_pid_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)7654609e1f1SChristian Brauner static int proc_pid_permission(struct mnt_idmap *idmap,
766549c7297SChristian Brauner struct inode *inode, int mask)
7670499680aSVasiliy Kulikov {
768fa10fed3SAlexey Gladkov struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
7690499680aSVasiliy Kulikov struct task_struct *task;
7700499680aSVasiliy Kulikov bool has_perms;
7710499680aSVasiliy Kulikov
7720499680aSVasiliy Kulikov task = get_proc_task(inode);
773a2ef990aSXiaotian Feng if (!task)
774a2ef990aSXiaotian Feng return -ESRCH;
775fa10fed3SAlexey Gladkov has_perms = has_pid_permissions(fs_info, task, HIDEPID_NO_ACCESS);
7760499680aSVasiliy Kulikov put_task_struct(task);
7770499680aSVasiliy Kulikov
7780499680aSVasiliy Kulikov if (!has_perms) {
779fa10fed3SAlexey Gladkov if (fs_info->hide_pid == HIDEPID_INVISIBLE) {
7800499680aSVasiliy Kulikov /*
7810499680aSVasiliy Kulikov * Let's make getdents(), stat(), and open()
7820499680aSVasiliy Kulikov * consistent with each other. If a process
7830499680aSVasiliy Kulikov * may not stat() a file, it shouldn't be seen
7840499680aSVasiliy Kulikov * in procfs at all.
7850499680aSVasiliy Kulikov */
7860499680aSVasiliy Kulikov return -ENOENT;
7870499680aSVasiliy Kulikov }
7880499680aSVasiliy Kulikov
7890499680aSVasiliy Kulikov return -EPERM;
7900499680aSVasiliy Kulikov }
7914609e1f1SChristian Brauner return generic_permission(&nop_mnt_idmap, inode, mask);
7920499680aSVasiliy Kulikov }
7930499680aSVasiliy Kulikov
7940499680aSVasiliy Kulikov
7950499680aSVasiliy Kulikov
796c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
7976d76fa58SLinus Torvalds .setattr = proc_setattr,
7986d76fa58SLinus Torvalds };
7996d76fa58SLinus Torvalds
proc_single_show(struct seq_file * m,void * v)800be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
801be614086SEric W. Biederman {
802be614086SEric W. Biederman struct inode *inode = m->private;
8039d78edeaSAlexey Gladkov struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
80476f668beSChristoph Hellwig struct pid *pid = proc_pid(inode);
805be614086SEric W. Biederman struct task_struct *task;
806be614086SEric W. Biederman int ret;
807be614086SEric W. Biederman
808be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID);
809be614086SEric W. Biederman if (!task)
810be614086SEric W. Biederman return -ESRCH;
811be614086SEric W. Biederman
812be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
813be614086SEric W. Biederman
814be614086SEric W. Biederman put_task_struct(task);
815be614086SEric W. Biederman return ret;
816be614086SEric W. Biederman }
817be614086SEric W. Biederman
proc_single_open(struct inode * inode,struct file * filp)818be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
819be614086SEric W. Biederman {
820c6a34058SJovi Zhang return single_open(filp, proc_single_show, inode);
821be614086SEric W. Biederman }
822be614086SEric W. Biederman
823be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
824be614086SEric W. Biederman .open = proc_single_open,
825be614086SEric W. Biederman .read = seq_read,
826be614086SEric W. Biederman .llseek = seq_lseek,
827be614086SEric W. Biederman .release = single_release,
828be614086SEric W. Biederman };
829be614086SEric W. Biederman
8305381e169SOleg Nesterov
proc_mem_open(struct inode * inode,unsigned int mode)8315381e169SOleg Nesterov struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
8321da177e4SLinus Torvalds {
8335381e169SOleg Nesterov struct task_struct *task = get_proc_task(inode);
8345381e169SOleg Nesterov struct mm_struct *mm = ERR_PTR(-ESRCH);
835e268337dSLinus Torvalds
8365381e169SOleg Nesterov if (task) {
837caaee623SJann Horn mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
838e268337dSLinus Torvalds put_task_struct(task);
839e268337dSLinus Torvalds
8405381e169SOleg Nesterov if (!IS_ERR_OR_NULL(mm)) {
8416d08f2c7SOleg Nesterov /* ensure this mm_struct can't be freed */
842f1f10076SVegard Nossum mmgrab(mm);
8436d08f2c7SOleg Nesterov /* but do not pin its memory */
8446d08f2c7SOleg Nesterov mmput(mm);
8456d08f2c7SOleg Nesterov }
8465381e169SOleg Nesterov }
8475381e169SOleg Nesterov
8485381e169SOleg Nesterov return mm;
8495381e169SOleg Nesterov }
8505381e169SOleg Nesterov
__mem_open(struct inode * inode,struct file * file,unsigned int mode)8515381e169SOleg Nesterov static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
8525381e169SOleg Nesterov {
8535381e169SOleg Nesterov struct mm_struct *mm = proc_mem_open(inode, mode);
8545381e169SOleg Nesterov
8555381e169SOleg Nesterov if (IS_ERR(mm))
8565381e169SOleg Nesterov return PTR_ERR(mm);
8576d08f2c7SOleg Nesterov
858e268337dSLinus Torvalds file->private_data = mm;
8591da177e4SLinus Torvalds return 0;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds
mem_open(struct inode * inode,struct file * file)862b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
863b409e578SCong Wang {
864bc452b4bSDjalal Harouni int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
865bc452b4bSDjalal Harouni
866bc452b4bSDjalal Harouni /* OK to pass negative loff_t, we can catch out-of-range */
867bc452b4bSDjalal Harouni file->f_mode |= FMODE_UNSIGNED_OFFSET;
868bc452b4bSDjalal Harouni
869bc452b4bSDjalal Harouni return ret;
870b409e578SCong Wang }
871b409e578SCong Wang
proc_mem_foll_force(struct file * file,struct mm_struct * mm)872*85525080SAdrian Ratiu static bool proc_mem_foll_force(struct file *file, struct mm_struct *mm)
873*85525080SAdrian Ratiu {
874*85525080SAdrian Ratiu struct task_struct *task;
875*85525080SAdrian Ratiu bool ptrace_active = false;
876*85525080SAdrian Ratiu
877*85525080SAdrian Ratiu switch (proc_mem_force_override) {
878*85525080SAdrian Ratiu case PROC_MEM_FORCE_NEVER:
879*85525080SAdrian Ratiu return false;
880*85525080SAdrian Ratiu case PROC_MEM_FORCE_PTRACE:
881*85525080SAdrian Ratiu task = get_proc_task(file_inode(file));
882*85525080SAdrian Ratiu if (task) {
883*85525080SAdrian Ratiu ptrace_active = READ_ONCE(task->ptrace) &&
884*85525080SAdrian Ratiu READ_ONCE(task->mm) == mm &&
885*85525080SAdrian Ratiu READ_ONCE(task->parent) == current;
886*85525080SAdrian Ratiu put_task_struct(task);
887*85525080SAdrian Ratiu }
888*85525080SAdrian Ratiu return ptrace_active;
889*85525080SAdrian Ratiu default:
890*85525080SAdrian Ratiu return true;
891*85525080SAdrian Ratiu }
892*85525080SAdrian Ratiu }
893*85525080SAdrian Ratiu
mem_rw(struct file * file,char __user * buf,size_t count,loff_t * ppos,int write)894572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
895572d34b9SOleg Nesterov size_t count, loff_t *ppos, int write)
8961da177e4SLinus Torvalds {
897e268337dSLinus Torvalds struct mm_struct *mm = file->private_data;
898572d34b9SOleg Nesterov unsigned long addr = *ppos;
899572d34b9SOleg Nesterov ssize_t copied;
9001da177e4SLinus Torvalds char *page;
901272ddc8bSLinus Torvalds unsigned int flags;
9021da177e4SLinus Torvalds
903e268337dSLinus Torvalds if (!mm)
904e268337dSLinus Torvalds return 0;
9051da177e4SLinus Torvalds
9060ee931c4SMichal Hocko page = (char *)__get_free_page(GFP_KERNEL);
9071da177e4SLinus Torvalds if (!page)
908e268337dSLinus Torvalds return -ENOMEM;
9091da177e4SLinus Torvalds
910f7ca54f4SFrederik Deweerdt copied = 0;
911388f7934SVegard Nossum if (!mmget_not_zero(mm))
9126d08f2c7SOleg Nesterov goto free;
9136d08f2c7SOleg Nesterov
914*85525080SAdrian Ratiu flags = write ? FOLL_WRITE : 0;
915*85525080SAdrian Ratiu if (proc_mem_foll_force(file, mm))
916*85525080SAdrian Ratiu flags |= FOLL_FORCE;
9176347e8d5SLorenzo Stoakes
9181da177e4SLinus Torvalds while (count > 0) {
919d238692bSMarcelo Henrique Cerri size_t this_len = min_t(size_t, count, PAGE_SIZE);
9201da177e4SLinus Torvalds
921572d34b9SOleg Nesterov if (write && copy_from_user(page, buf, this_len)) {
9221da177e4SLinus Torvalds copied = -EFAULT;
9231da177e4SLinus Torvalds break;
9241da177e4SLinus Torvalds }
925572d34b9SOleg Nesterov
9266347e8d5SLorenzo Stoakes this_len = access_remote_vm(mm, addr, page, this_len, flags);
927572d34b9SOleg Nesterov if (!this_len) {
9281da177e4SLinus Torvalds if (!copied)
9291da177e4SLinus Torvalds copied = -EIO;
9301da177e4SLinus Torvalds break;
9311da177e4SLinus Torvalds }
932572d34b9SOleg Nesterov
933572d34b9SOleg Nesterov if (!write && copy_to_user(buf, page, this_len)) {
934572d34b9SOleg Nesterov copied = -EFAULT;
935572d34b9SOleg Nesterov break;
9361da177e4SLinus Torvalds }
937572d34b9SOleg Nesterov
938572d34b9SOleg Nesterov buf += this_len;
939572d34b9SOleg Nesterov addr += this_len;
940572d34b9SOleg Nesterov copied += this_len;
941572d34b9SOleg Nesterov count -= this_len;
942572d34b9SOleg Nesterov }
943572d34b9SOleg Nesterov *ppos = addr;
94430cd8903SKOSAKI Motohiro
9456d08f2c7SOleg Nesterov mmput(mm);
9466d08f2c7SOleg Nesterov free:
94730cd8903SKOSAKI Motohiro free_page((unsigned long) page);
9481da177e4SLinus Torvalds return copied;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds
mem_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)951572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
952572d34b9SOleg Nesterov size_t count, loff_t *ppos)
953572d34b9SOleg Nesterov {
954572d34b9SOleg Nesterov return mem_rw(file, buf, count, ppos, 0);
955572d34b9SOleg Nesterov }
956572d34b9SOleg Nesterov
mem_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)957572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
958572d34b9SOleg Nesterov size_t count, loff_t *ppos)
959572d34b9SOleg Nesterov {
960572d34b9SOleg Nesterov return mem_rw(file, (char __user*)buf, count, ppos, 1);
961572d34b9SOleg Nesterov }
962572d34b9SOleg Nesterov
mem_lseek(struct file * file,loff_t offset,int orig)96385863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
9641da177e4SLinus Torvalds {
9651da177e4SLinus Torvalds switch (orig) {
9661da177e4SLinus Torvalds case 0:
9671da177e4SLinus Torvalds file->f_pos = offset;
9681da177e4SLinus Torvalds break;
9691da177e4SLinus Torvalds case 1:
9701da177e4SLinus Torvalds file->f_pos += offset;
9711da177e4SLinus Torvalds break;
9721da177e4SLinus Torvalds default:
9731da177e4SLinus Torvalds return -EINVAL;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds force_successful_syscall_return();
9761da177e4SLinus Torvalds return file->f_pos;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds
mem_release(struct inode * inode,struct file * file)979e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
980e268337dSLinus Torvalds {
981e268337dSLinus Torvalds struct mm_struct *mm = file->private_data;
98271879d3cSOleg Nesterov if (mm)
9836d08f2c7SOleg Nesterov mmdrop(mm);
984e268337dSLinus Torvalds return 0;
985e268337dSLinus Torvalds }
986e268337dSLinus Torvalds
98700977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
9881da177e4SLinus Torvalds .llseek = mem_lseek,
9891da177e4SLinus Torvalds .read = mem_read,
9901da177e4SLinus Torvalds .write = mem_write,
9911da177e4SLinus Torvalds .open = mem_open,
992e268337dSLinus Torvalds .release = mem_release,
9931da177e4SLinus Torvalds };
9941da177e4SLinus Torvalds
environ_open(struct inode * inode,struct file * file)995b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
996b409e578SCong Wang {
997b409e578SCong Wang return __mem_open(inode, file, PTRACE_MODE_READ);
998b409e578SCong Wang }
999b409e578SCong Wang
environ_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1000315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
1001315e28c8SJames Pearson size_t count, loff_t *ppos)
1002315e28c8SJames Pearson {
1003315e28c8SJames Pearson char *page;
1004315e28c8SJames Pearson unsigned long src = *ppos;
1005b409e578SCong Wang int ret = 0;
1006b409e578SCong Wang struct mm_struct *mm = file->private_data;
1007a3b609efSMateusz Guzik unsigned long env_start, env_end;
1008315e28c8SJames Pearson
10098148a73cSMathias Krause /* Ensure the process spawned far enough to have an environment. */
10108148a73cSMathias Krause if (!mm || !mm->env_end)
1011b409e578SCong Wang return 0;
1012315e28c8SJames Pearson
10130ee931c4SMichal Hocko page = (char *)__get_free_page(GFP_KERNEL);
1014315e28c8SJames Pearson if (!page)
1015b409e578SCong Wang return -ENOMEM;
1016315e28c8SJames Pearson
1017d6f64b89SAl Viro ret = 0;
1018388f7934SVegard Nossum if (!mmget_not_zero(mm))
1019b409e578SCong Wang goto free;
1020a3b609efSMateusz Guzik
102188aa7cc6SYang Shi spin_lock(&mm->arg_lock);
1022a3b609efSMateusz Guzik env_start = mm->env_start;
1023a3b609efSMateusz Guzik env_end = mm->env_end;
102488aa7cc6SYang Shi spin_unlock(&mm->arg_lock);
1025a3b609efSMateusz Guzik
1026315e28c8SJames Pearson while (count > 0) {
1027e8905ec2SDjalal Harouni size_t this_len, max_len;
1028e8905ec2SDjalal Harouni int retval;
1029e8905ec2SDjalal Harouni
1030a3b609efSMateusz Guzik if (src >= (env_end - env_start))
1031e8905ec2SDjalal Harouni break;
1032315e28c8SJames Pearson
1033a3b609efSMateusz Guzik this_len = env_end - (env_start + src);
1034315e28c8SJames Pearson
1035e8905ec2SDjalal Harouni max_len = min_t(size_t, PAGE_SIZE, count);
1036e8905ec2SDjalal Harouni this_len = min(max_len, this_len);
1037315e28c8SJames Pearson
10387f7ccc2cSWilly Tarreau retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
1039315e28c8SJames Pearson
1040315e28c8SJames Pearson if (retval <= 0) {
1041315e28c8SJames Pearson ret = retval;
1042315e28c8SJames Pearson break;
1043315e28c8SJames Pearson }
1044315e28c8SJames Pearson
1045315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) {
1046315e28c8SJames Pearson ret = -EFAULT;
1047315e28c8SJames Pearson break;
1048315e28c8SJames Pearson }
1049315e28c8SJames Pearson
1050315e28c8SJames Pearson ret += retval;
1051315e28c8SJames Pearson src += retval;
1052315e28c8SJames Pearson buf += retval;
1053315e28c8SJames Pearson count -= retval;
1054315e28c8SJames Pearson }
1055315e28c8SJames Pearson *ppos = src;
1056315e28c8SJames Pearson mmput(mm);
1057b409e578SCong Wang
1058b409e578SCong Wang free:
1059315e28c8SJames Pearson free_page((unsigned long) page);
1060315e28c8SJames Pearson return ret;
1061315e28c8SJames Pearson }
1062315e28c8SJames Pearson
1063315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
1064b409e578SCong Wang .open = environ_open,
1065315e28c8SJames Pearson .read = environ_read,
106687df8424SArnd Bergmann .llseek = generic_file_llseek,
1067b409e578SCong Wang .release = mem_release,
1068315e28c8SJames Pearson };
1069315e28c8SJames Pearson
auxv_open(struct inode * inode,struct file * file)1070c5317167SAl Viro static int auxv_open(struct inode *inode, struct file *file)
1071c5317167SAl Viro {
1072c5317167SAl Viro return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1073c5317167SAl Viro }
1074c5317167SAl Viro
auxv_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1075c5317167SAl Viro static ssize_t auxv_read(struct file *file, char __user *buf,
1076c5317167SAl Viro size_t count, loff_t *ppos)
1077c5317167SAl Viro {
1078c5317167SAl Viro struct mm_struct *mm = file->private_data;
1079c5317167SAl Viro unsigned int nwords = 0;
108006b2849dSLeon Yu
108106b2849dSLeon Yu if (!mm)
108206b2849dSLeon Yu return 0;
1083c5317167SAl Viro do {
1084c5317167SAl Viro nwords += 2;
1085c5317167SAl Viro } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1086c5317167SAl Viro return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1087c5317167SAl Viro nwords * sizeof(mm->saved_auxv[0]));
1088c5317167SAl Viro }
1089c5317167SAl Viro
1090c5317167SAl Viro static const struct file_operations proc_auxv_operations = {
1091c5317167SAl Viro .open = auxv_open,
1092c5317167SAl Viro .read = auxv_read,
1093c5317167SAl Viro .llseek = generic_file_llseek,
1094c5317167SAl Viro .release = mem_release,
1095c5317167SAl Viro };
1096c5317167SAl Viro
oom_adj_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1097fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1098fa0cbbf1SDavid Rientjes loff_t *ppos)
1099fa0cbbf1SDavid Rientjes {
1100496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file));
1101fa0cbbf1SDavid Rientjes char buffer[PROC_NUMBUF];
1102fa0cbbf1SDavid Rientjes int oom_adj = OOM_ADJUST_MIN;
1103fa0cbbf1SDavid Rientjes size_t len;
1104fa0cbbf1SDavid Rientjes
1105fa0cbbf1SDavid Rientjes if (!task)
1106fa0cbbf1SDavid Rientjes return -ESRCH;
1107fa0cbbf1SDavid Rientjes if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1108fa0cbbf1SDavid Rientjes oom_adj = OOM_ADJUST_MAX;
1109fa0cbbf1SDavid Rientjes else
1110fa0cbbf1SDavid Rientjes oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1111fa0cbbf1SDavid Rientjes OOM_SCORE_ADJ_MAX;
1112fa0cbbf1SDavid Rientjes put_task_struct(task);
111366606567SCharles Haithcock if (oom_adj > OOM_ADJUST_MAX)
111466606567SCharles Haithcock oom_adj = OOM_ADJUST_MAX;
1115fa0cbbf1SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1116fa0cbbf1SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len);
1117fa0cbbf1SDavid Rientjes }
1118fa0cbbf1SDavid Rientjes
__set_oom_adj(struct file * file,int oom_adj,bool legacy)11191d5f0acbSMichal Hocko static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
11201d5f0acbSMichal Hocko {
112144a70adeSMichal Hocko struct mm_struct *mm = NULL;
11221d5f0acbSMichal Hocko struct task_struct *task;
11231d5f0acbSMichal Hocko int err = 0;
11241d5f0acbSMichal Hocko
11251d5f0acbSMichal Hocko task = get_proc_task(file_inode(file));
11261d5f0acbSMichal Hocko if (!task)
11271d5f0acbSMichal Hocko return -ESRCH;
11281d5f0acbSMichal Hocko
11291d5f0acbSMichal Hocko mutex_lock(&oom_adj_mutex);
11301d5f0acbSMichal Hocko if (legacy) {
11311d5f0acbSMichal Hocko if (oom_adj < task->signal->oom_score_adj &&
11321d5f0acbSMichal Hocko !capable(CAP_SYS_RESOURCE)) {
11331d5f0acbSMichal Hocko err = -EACCES;
11341d5f0acbSMichal Hocko goto err_unlock;
11351d5f0acbSMichal Hocko }
11361d5f0acbSMichal Hocko /*
11371d5f0acbSMichal Hocko * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
11381d5f0acbSMichal Hocko * /proc/pid/oom_score_adj instead.
11391d5f0acbSMichal Hocko */
11401d5f0acbSMichal Hocko pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
11411d5f0acbSMichal Hocko current->comm, task_pid_nr(current), task_pid_nr(task),
11421d5f0acbSMichal Hocko task_pid_nr(task));
11431d5f0acbSMichal Hocko } else {
11441d5f0acbSMichal Hocko if ((short)oom_adj < task->signal->oom_score_adj_min &&
11451d5f0acbSMichal Hocko !capable(CAP_SYS_RESOURCE)) {
11461d5f0acbSMichal Hocko err = -EACCES;
11471d5f0acbSMichal Hocko goto err_unlock;
11481d5f0acbSMichal Hocko }
11491d5f0acbSMichal Hocko }
11501d5f0acbSMichal Hocko
115144a70adeSMichal Hocko /*
115244a70adeSMichal Hocko * Make sure we will check other processes sharing the mm if this is
115344a70adeSMichal Hocko * not vfrok which wants its own oom_score_adj.
115444a70adeSMichal Hocko * pin the mm so it doesn't go away and get reused after task_unlock
115544a70adeSMichal Hocko */
115644a70adeSMichal Hocko if (!task->vfork_done) {
115744a70adeSMichal Hocko struct task_struct *p = find_lock_task_mm(task);
115844a70adeSMichal Hocko
115944a70adeSMichal Hocko if (p) {
116067197a4fSSuren Baghdasaryan if (test_bit(MMF_MULTIPROCESS, &p->mm->flags)) {
116144a70adeSMichal Hocko mm = p->mm;
1162f1f10076SVegard Nossum mmgrab(mm);
116344a70adeSMichal Hocko }
116444a70adeSMichal Hocko task_unlock(p);
116544a70adeSMichal Hocko }
116644a70adeSMichal Hocko }
116744a70adeSMichal Hocko
11681d5f0acbSMichal Hocko task->signal->oom_score_adj = oom_adj;
11691d5f0acbSMichal Hocko if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
11701d5f0acbSMichal Hocko task->signal->oom_score_adj_min = (short)oom_adj;
11711d5f0acbSMichal Hocko trace_oom_score_adj_update(task);
117244a70adeSMichal Hocko
117344a70adeSMichal Hocko if (mm) {
117444a70adeSMichal Hocko struct task_struct *p;
117544a70adeSMichal Hocko
117644a70adeSMichal Hocko rcu_read_lock();
117744a70adeSMichal Hocko for_each_process(p) {
117844a70adeSMichal Hocko if (same_thread_group(task, p))
117944a70adeSMichal Hocko continue;
118044a70adeSMichal Hocko
118144a70adeSMichal Hocko /* do not touch kernel threads or the global init */
118244a70adeSMichal Hocko if (p->flags & PF_KTHREAD || is_global_init(p))
118344a70adeSMichal Hocko continue;
118444a70adeSMichal Hocko
118544a70adeSMichal Hocko task_lock(p);
118644a70adeSMichal Hocko if (!p->vfork_done && process_shares_mm(p, mm)) {
118744a70adeSMichal Hocko p->signal->oom_score_adj = oom_adj;
118844a70adeSMichal Hocko if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
118944a70adeSMichal Hocko p->signal->oom_score_adj_min = (short)oom_adj;
119044a70adeSMichal Hocko }
119144a70adeSMichal Hocko task_unlock(p);
119244a70adeSMichal Hocko }
119344a70adeSMichal Hocko rcu_read_unlock();
119444a70adeSMichal Hocko mmdrop(mm);
119544a70adeSMichal Hocko }
11961d5f0acbSMichal Hocko err_unlock:
11971d5f0acbSMichal Hocko mutex_unlock(&oom_adj_mutex);
11981d5f0acbSMichal Hocko put_task_struct(task);
11991d5f0acbSMichal Hocko return err;
12001d5f0acbSMichal Hocko }
1201f913da59SMichal Hocko
1202b72bdfa7SDavid Rientjes /*
1203b72bdfa7SDavid Rientjes * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1204b72bdfa7SDavid Rientjes * kernels. The effective policy is defined by oom_score_adj, which has a
1205b72bdfa7SDavid Rientjes * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1206b72bdfa7SDavid Rientjes * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1207b72bdfa7SDavid Rientjes * Processes that become oom disabled via oom_adj will still be oom disabled
1208b72bdfa7SDavid Rientjes * with this implementation.
1209b72bdfa7SDavid Rientjes *
1210b72bdfa7SDavid Rientjes * oom_adj cannot be removed since existing userspace binaries use it.
1211b72bdfa7SDavid Rientjes */
oom_adj_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1212fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1213fa0cbbf1SDavid Rientjes size_t count, loff_t *ppos)
1214fa0cbbf1SDavid Rientjes {
1215fa0cbbf1SDavid Rientjes char buffer[PROC_NUMBUF];
1216fa0cbbf1SDavid Rientjes int oom_adj;
1217fa0cbbf1SDavid Rientjes int err;
1218fa0cbbf1SDavid Rientjes
1219fa0cbbf1SDavid Rientjes memset(buffer, 0, sizeof(buffer));
1220fa0cbbf1SDavid Rientjes if (count > sizeof(buffer) - 1)
1221fa0cbbf1SDavid Rientjes count = sizeof(buffer) - 1;
1222fa0cbbf1SDavid Rientjes if (copy_from_user(buffer, buf, count)) {
1223fa0cbbf1SDavid Rientjes err = -EFAULT;
1224fa0cbbf1SDavid Rientjes goto out;
1225fa0cbbf1SDavid Rientjes }
1226fa0cbbf1SDavid Rientjes
1227fa0cbbf1SDavid Rientjes err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1228fa0cbbf1SDavid Rientjes if (err)
1229fa0cbbf1SDavid Rientjes goto out;
1230fa0cbbf1SDavid Rientjes if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1231fa0cbbf1SDavid Rientjes oom_adj != OOM_DISABLE) {
1232fa0cbbf1SDavid Rientjes err = -EINVAL;
1233fa0cbbf1SDavid Rientjes goto out;
1234fa0cbbf1SDavid Rientjes }
1235fa0cbbf1SDavid Rientjes
1236fa0cbbf1SDavid Rientjes /*
1237fa0cbbf1SDavid Rientjes * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1238fa0cbbf1SDavid Rientjes * value is always attainable.
1239fa0cbbf1SDavid Rientjes */
1240fa0cbbf1SDavid Rientjes if (oom_adj == OOM_ADJUST_MAX)
1241fa0cbbf1SDavid Rientjes oom_adj = OOM_SCORE_ADJ_MAX;
1242fa0cbbf1SDavid Rientjes else
1243fa0cbbf1SDavid Rientjes oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1244fa0cbbf1SDavid Rientjes
12451d5f0acbSMichal Hocko err = __set_oom_adj(file, oom_adj, true);
1246fa0cbbf1SDavid Rientjes out:
1247fa0cbbf1SDavid Rientjes return err < 0 ? err : count;
1248fa0cbbf1SDavid Rientjes }
1249fa0cbbf1SDavid Rientjes
1250fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = {
1251fa0cbbf1SDavid Rientjes .read = oom_adj_read,
1252fa0cbbf1SDavid Rientjes .write = oom_adj_write,
1253fa0cbbf1SDavid Rientjes .llseek = generic_file_llseek,
1254fa0cbbf1SDavid Rientjes };
1255fa0cbbf1SDavid Rientjes
oom_score_adj_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1256a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1257a63d83f4SDavid Rientjes size_t count, loff_t *ppos)
1258a63d83f4SDavid Rientjes {
1259496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file));
1260a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF];
1261a9c58b90SDavid Rientjes short oom_score_adj = OOM_SCORE_ADJ_MIN;
1262a63d83f4SDavid Rientjes size_t len;
1263a63d83f4SDavid Rientjes
1264a63d83f4SDavid Rientjes if (!task)
1265a63d83f4SDavid Rientjes return -ESRCH;
1266a63d83f4SDavid Rientjes oom_score_adj = task->signal->oom_score_adj;
1267a63d83f4SDavid Rientjes put_task_struct(task);
1268a9c58b90SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1269a63d83f4SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len);
1270a63d83f4SDavid Rientjes }
1271a63d83f4SDavid Rientjes
oom_score_adj_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1272a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1273a63d83f4SDavid Rientjes size_t count, loff_t *ppos)
1274a63d83f4SDavid Rientjes {
1275a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF];
12760a8cb8e3SAlexey Dobriyan int oom_score_adj;
1277a63d83f4SDavid Rientjes int err;
1278a63d83f4SDavid Rientjes
1279a63d83f4SDavid Rientjes memset(buffer, 0, sizeof(buffer));
1280a63d83f4SDavid Rientjes if (count > sizeof(buffer) - 1)
1281a63d83f4SDavid Rientjes count = sizeof(buffer) - 1;
1282723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) {
1283723548bfSDavid Rientjes err = -EFAULT;
1284723548bfSDavid Rientjes goto out;
1285723548bfSDavid Rientjes }
1286a63d83f4SDavid Rientjes
12870a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1288a63d83f4SDavid Rientjes if (err)
1289723548bfSDavid Rientjes goto out;
1290a63d83f4SDavid Rientjes if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1291723548bfSDavid Rientjes oom_score_adj > OOM_SCORE_ADJ_MAX) {
1292723548bfSDavid Rientjes err = -EINVAL;
1293723548bfSDavid Rientjes goto out;
1294723548bfSDavid Rientjes }
1295a63d83f4SDavid Rientjes
12961d5f0acbSMichal Hocko err = __set_oom_adj(file, oom_score_adj, false);
1297723548bfSDavid Rientjes out:
1298723548bfSDavid Rientjes return err < 0 ? err : count;
1299a63d83f4SDavid Rientjes }
1300a63d83f4SDavid Rientjes
1301a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1302a63d83f4SDavid Rientjes .read = oom_score_adj_read,
1303a63d83f4SDavid Rientjes .write = oom_score_adj_write,
13046038f373SArnd Bergmann .llseek = default_llseek,
1305a63d83f4SDavid Rientjes };
1306a63d83f4SDavid Rientjes
13074b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
1308b4eb4f7fSAlexey Dobriyan #define TMPBUFLEN 11
proc_loginuid_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)13091da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
13101da177e4SLinus Torvalds size_t count, loff_t *ppos)
13111da177e4SLinus Torvalds {
1312496ad9aaSAl Viro struct inode * inode = file_inode(file);
131399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode);
13141da177e4SLinus Torvalds ssize_t length;
13151da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN];
13161da177e4SLinus Torvalds
131799f89551SEric W. Biederman if (!task)
131899f89551SEric W. Biederman return -ESRCH;
13191da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1320e1760bd5SEric W. Biederman from_kuid(file->f_cred->user_ns,
1321e1760bd5SEric W. Biederman audit_get_loginuid(task)));
132299f89551SEric W. Biederman put_task_struct(task);
13231da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds
proc_loginuid_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)13261da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
13271da177e4SLinus Torvalds size_t count, loff_t *ppos)
13281da177e4SLinus Torvalds {
1329496ad9aaSAl Viro struct inode * inode = file_inode(file);
13301da177e4SLinus Torvalds uid_t loginuid;
1331e1760bd5SEric W. Biederman kuid_t kloginuid;
1332774636e1SAlexey Dobriyan int rv;
13331da177e4SLinus Torvalds
13344ea33a97SJens Axboe /* Don't let kthreads write their own loginuid */
13354ea33a97SJens Axboe if (current->flags & PF_KTHREAD)
13364ea33a97SJens Axboe return -EPERM;
13374ea33a97SJens Axboe
13387dc52157SPaul E. McKenney rcu_read_lock();
13397dc52157SPaul E. McKenney if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
13407dc52157SPaul E. McKenney rcu_read_unlock();
13411da177e4SLinus Torvalds return -EPERM;
13427dc52157SPaul E. McKenney }
13437dc52157SPaul E. McKenney rcu_read_unlock();
13441da177e4SLinus Torvalds
13451da177e4SLinus Torvalds if (*ppos != 0) {
13461da177e4SLinus Torvalds /* No partial writes. */
13471da177e4SLinus Torvalds return -EINVAL;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds
1350774636e1SAlexey Dobriyan rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1351774636e1SAlexey Dobriyan if (rv < 0)
1352774636e1SAlexey Dobriyan return rv;
135381407c84SEric Paris
135481407c84SEric Paris /* is userspace tring to explicitly UNSET the loginuid? */
135581407c84SEric Paris if (loginuid == AUDIT_UID_UNSET) {
135681407c84SEric Paris kloginuid = INVALID_UID;
135781407c84SEric Paris } else {
1358e1760bd5SEric W. Biederman kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1359774636e1SAlexey Dobriyan if (!uid_valid(kloginuid))
1360774636e1SAlexey Dobriyan return -EINVAL;
136181407c84SEric Paris }
1362e1760bd5SEric W. Biederman
1363774636e1SAlexey Dobriyan rv = audit_set_loginuid(kloginuid);
1364774636e1SAlexey Dobriyan if (rv < 0)
1365774636e1SAlexey Dobriyan return rv;
1366774636e1SAlexey Dobriyan return count;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds
136900977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
13701da177e4SLinus Torvalds .read = proc_loginuid_read,
13711da177e4SLinus Torvalds .write = proc_loginuid_write,
137287df8424SArnd Bergmann .llseek = generic_file_llseek,
13731da177e4SLinus Torvalds };
13741e0bd755SEric Paris
proc_sessionid_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)13751e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
13761e0bd755SEric Paris size_t count, loff_t *ppos)
13771e0bd755SEric Paris {
1378496ad9aaSAl Viro struct inode * inode = file_inode(file);
13791e0bd755SEric Paris struct task_struct *task = get_proc_task(inode);
13801e0bd755SEric Paris ssize_t length;
13811e0bd755SEric Paris char tmpbuf[TMPBUFLEN];
13821e0bd755SEric Paris
13831e0bd755SEric Paris if (!task)
13841e0bd755SEric Paris return -ESRCH;
13851e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
13861e0bd755SEric Paris audit_get_sessionid(task));
13871e0bd755SEric Paris put_task_struct(task);
13881e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
13891e0bd755SEric Paris }
13901e0bd755SEric Paris
13911e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
13921e0bd755SEric Paris .read = proc_sessionid_read,
139387df8424SArnd Bergmann .llseek = generic_file_llseek,
13941e0bd755SEric Paris };
13951da177e4SLinus Torvalds #endif
13961da177e4SLinus Torvalds
1397f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
proc_fault_inject_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1398f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1399f4f154fdSAkinobu Mita size_t count, loff_t *ppos)
1400f4f154fdSAkinobu Mita {
1401496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file));
1402f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF];
1403f4f154fdSAkinobu Mita size_t len;
1404f4f154fdSAkinobu Mita int make_it_fail;
1405f4f154fdSAkinobu Mita
1406f4f154fdSAkinobu Mita if (!task)
1407f4f154fdSAkinobu Mita return -ESRCH;
1408f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail;
1409f4f154fdSAkinobu Mita put_task_struct(task);
1410f4f154fdSAkinobu Mita
1411f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
14120c28f287SAkinobu Mita
14130c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len);
1414f4f154fdSAkinobu Mita }
1415f4f154fdSAkinobu Mita
proc_fault_inject_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1416f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1417f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos)
1418f4f154fdSAkinobu Mita {
1419f4f154fdSAkinobu Mita struct task_struct *task;
1420774636e1SAlexey Dobriyan char buffer[PROC_NUMBUF];
1421f4f154fdSAkinobu Mita int make_it_fail;
1422774636e1SAlexey Dobriyan int rv;
1423f4f154fdSAkinobu Mita
1424f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE))
1425f4f154fdSAkinobu Mita return -EPERM;
1426f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer));
1427f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1)
1428f4f154fdSAkinobu Mita count = sizeof(buffer) - 1;
1429f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count))
1430f4f154fdSAkinobu Mita return -EFAULT;
1431774636e1SAlexey Dobriyan rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1432774636e1SAlexey Dobriyan if (rv < 0)
1433774636e1SAlexey Dobriyan return rv;
143416caed31SDave Jones if (make_it_fail < 0 || make_it_fail > 1)
143516caed31SDave Jones return -EINVAL;
143616caed31SDave Jones
1437496ad9aaSAl Viro task = get_proc_task(file_inode(file));
1438f4f154fdSAkinobu Mita if (!task)
1439f4f154fdSAkinobu Mita return -ESRCH;
1440f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail;
1441f4f154fdSAkinobu Mita put_task_struct(task);
1442cba8aafeSVincent Li
1443cba8aafeSVincent Li return count;
1444f4f154fdSAkinobu Mita }
1445f4f154fdSAkinobu Mita
144600977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1447f4f154fdSAkinobu Mita .read = proc_fault_inject_read,
1448f4f154fdSAkinobu Mita .write = proc_fault_inject_write,
144987df8424SArnd Bergmann .llseek = generic_file_llseek,
1450f4f154fdSAkinobu Mita };
1451e41d5818SDmitry Vyukov
proc_fail_nth_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1452e41d5818SDmitry Vyukov static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1453e41d5818SDmitry Vyukov size_t count, loff_t *ppos)
1454e41d5818SDmitry Vyukov {
1455e41d5818SDmitry Vyukov struct task_struct *task;
14569049f2f6SAkinobu Mita int err;
14579049f2f6SAkinobu Mita unsigned int n;
1458e41d5818SDmitry Vyukov
14599049f2f6SAkinobu Mita err = kstrtouint_from_user(buf, count, 0, &n);
1460e41d5818SDmitry Vyukov if (err)
1461e41d5818SDmitry Vyukov return err;
14621203c8e6SAkinobu Mita
14631203c8e6SAkinobu Mita task = get_proc_task(file_inode(file));
14641203c8e6SAkinobu Mita if (!task)
14651203c8e6SAkinobu Mita return -ESRCH;
14669f7118b2SAlexey Dobriyan task->fail_nth = n;
14671203c8e6SAkinobu Mita put_task_struct(task);
14681203c8e6SAkinobu Mita
1469e41d5818SDmitry Vyukov return count;
1470e41d5818SDmitry Vyukov }
1471e41d5818SDmitry Vyukov
proc_fail_nth_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1472e41d5818SDmitry Vyukov static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1473e41d5818SDmitry Vyukov size_t count, loff_t *ppos)
1474e41d5818SDmitry Vyukov {
1475e41d5818SDmitry Vyukov struct task_struct *task;
1476bfc74093SAkinobu Mita char numbuf[PROC_NUMBUF];
1477bfc74093SAkinobu Mita ssize_t len;
1478e41d5818SDmitry Vyukov
1479e41d5818SDmitry Vyukov task = get_proc_task(file_inode(file));
1480e41d5818SDmitry Vyukov if (!task)
1481e41d5818SDmitry Vyukov return -ESRCH;
14829f7118b2SAlexey Dobriyan len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
14831203c8e6SAkinobu Mita put_task_struct(task);
1484a44937feSAlexey Dobriyan return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1485e41d5818SDmitry Vyukov }
1486e41d5818SDmitry Vyukov
1487e41d5818SDmitry Vyukov static const struct file_operations proc_fail_nth_operations = {
1488e41d5818SDmitry Vyukov .read = proc_fail_nth_read,
1489e41d5818SDmitry Vyukov .write = proc_fail_nth_write,
1490e41d5818SDmitry Vyukov };
1491f4f154fdSAkinobu Mita #endif
1492f4f154fdSAkinobu Mita
14939745512cSArjan van de Ven
149443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
149543ae34cbSIngo Molnar /*
149643ae34cbSIngo Molnar * Print out various scheduling related per-task fields:
149743ae34cbSIngo Molnar */
sched_show(struct seq_file * m,void * v)149843ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
149943ae34cbSIngo Molnar {
150043ae34cbSIngo Molnar struct inode *inode = m->private;
15019d78edeaSAlexey Gladkov struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
150243ae34cbSIngo Molnar struct task_struct *p;
150343ae34cbSIngo Molnar
150443ae34cbSIngo Molnar p = get_proc_task(inode);
150543ae34cbSIngo Molnar if (!p)
150643ae34cbSIngo Molnar return -ESRCH;
150774dc3384SAleksa Sarai proc_sched_show_task(p, ns, m);
150843ae34cbSIngo Molnar
150943ae34cbSIngo Molnar put_task_struct(p);
151043ae34cbSIngo Molnar
151143ae34cbSIngo Molnar return 0;
151243ae34cbSIngo Molnar }
151343ae34cbSIngo Molnar
151443ae34cbSIngo Molnar static ssize_t
sched_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)151543ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
151643ae34cbSIngo Molnar size_t count, loff_t *offset)
151743ae34cbSIngo Molnar {
1518496ad9aaSAl Viro struct inode *inode = file_inode(file);
151943ae34cbSIngo Molnar struct task_struct *p;
152043ae34cbSIngo Molnar
152143ae34cbSIngo Molnar p = get_proc_task(inode);
152243ae34cbSIngo Molnar if (!p)
152343ae34cbSIngo Molnar return -ESRCH;
152443ae34cbSIngo Molnar proc_sched_set_task(p);
152543ae34cbSIngo Molnar
152643ae34cbSIngo Molnar put_task_struct(p);
152743ae34cbSIngo Molnar
152843ae34cbSIngo Molnar return count;
152943ae34cbSIngo Molnar }
153043ae34cbSIngo Molnar
sched_open(struct inode * inode,struct file * filp)153143ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
153243ae34cbSIngo Molnar {
1533c6a34058SJovi Zhang return single_open(filp, sched_show, inode);
153443ae34cbSIngo Molnar }
153543ae34cbSIngo Molnar
153643ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
153743ae34cbSIngo Molnar .open = sched_open,
153843ae34cbSIngo Molnar .read = seq_read,
153943ae34cbSIngo Molnar .write = sched_write,
154043ae34cbSIngo Molnar .llseek = seq_lseek,
15415ea473a1SAlexey Dobriyan .release = single_release,
154243ae34cbSIngo Molnar };
154343ae34cbSIngo Molnar
154443ae34cbSIngo Molnar #endif
154543ae34cbSIngo Molnar
15465091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
15475091faa4SMike Galbraith /*
15485091faa4SMike Galbraith * Print out autogroup related information:
15495091faa4SMike Galbraith */
sched_autogroup_show(struct seq_file * m,void * v)15505091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
15515091faa4SMike Galbraith {
15525091faa4SMike Galbraith struct inode *inode = m->private;
15535091faa4SMike Galbraith struct task_struct *p;
15545091faa4SMike Galbraith
15555091faa4SMike Galbraith p = get_proc_task(inode);
15565091faa4SMike Galbraith if (!p)
15575091faa4SMike Galbraith return -ESRCH;
15585091faa4SMike Galbraith proc_sched_autogroup_show_task(p, m);
15595091faa4SMike Galbraith
15605091faa4SMike Galbraith put_task_struct(p);
15615091faa4SMike Galbraith
15625091faa4SMike Galbraith return 0;
15635091faa4SMike Galbraith }
15645091faa4SMike Galbraith
15655091faa4SMike Galbraith static ssize_t
sched_autogroup_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)15665091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
15675091faa4SMike Galbraith size_t count, loff_t *offset)
15685091faa4SMike Galbraith {
1569496ad9aaSAl Viro struct inode *inode = file_inode(file);
15705091faa4SMike Galbraith struct task_struct *p;
15715091faa4SMike Galbraith char buffer[PROC_NUMBUF];
15720a8cb8e3SAlexey Dobriyan int nice;
15735091faa4SMike Galbraith int err;
15745091faa4SMike Galbraith
15755091faa4SMike Galbraith memset(buffer, 0, sizeof(buffer));
15765091faa4SMike Galbraith if (count > sizeof(buffer) - 1)
15775091faa4SMike Galbraith count = sizeof(buffer) - 1;
15785091faa4SMike Galbraith if (copy_from_user(buffer, buf, count))
15795091faa4SMike Galbraith return -EFAULT;
15805091faa4SMike Galbraith
15810a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &nice);
15820a8cb8e3SAlexey Dobriyan if (err < 0)
15830a8cb8e3SAlexey Dobriyan return err;
15845091faa4SMike Galbraith
15855091faa4SMike Galbraith p = get_proc_task(inode);
15865091faa4SMike Galbraith if (!p)
15875091faa4SMike Galbraith return -ESRCH;
15885091faa4SMike Galbraith
15892e5b5b3aSHiroshi Shimamoto err = proc_sched_autogroup_set_nice(p, nice);
15905091faa4SMike Galbraith if (err)
15915091faa4SMike Galbraith count = err;
15925091faa4SMike Galbraith
15935091faa4SMike Galbraith put_task_struct(p);
15945091faa4SMike Galbraith
15955091faa4SMike Galbraith return count;
15965091faa4SMike Galbraith }
15975091faa4SMike Galbraith
sched_autogroup_open(struct inode * inode,struct file * filp)15985091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
15995091faa4SMike Galbraith {
16005091faa4SMike Galbraith int ret;
16015091faa4SMike Galbraith
16025091faa4SMike Galbraith ret = single_open(filp, sched_autogroup_show, NULL);
16035091faa4SMike Galbraith if (!ret) {
16045091faa4SMike Galbraith struct seq_file *m = filp->private_data;
16055091faa4SMike Galbraith
16065091faa4SMike Galbraith m->private = inode;
16075091faa4SMike Galbraith }
16085091faa4SMike Galbraith return ret;
16095091faa4SMike Galbraith }
16105091faa4SMike Galbraith
16115091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
16125091faa4SMike Galbraith .open = sched_autogroup_open,
16135091faa4SMike Galbraith .read = seq_read,
16145091faa4SMike Galbraith .write = sched_autogroup_write,
16155091faa4SMike Galbraith .llseek = seq_lseek,
16165091faa4SMike Galbraith .release = single_release,
16175091faa4SMike Galbraith };
16185091faa4SMike Galbraith
16195091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
16205091faa4SMike Galbraith
162104a8682aSAndrei Vagin #ifdef CONFIG_TIME_NS
timens_offsets_show(struct seq_file * m,void * v)162204a8682aSAndrei Vagin static int timens_offsets_show(struct seq_file *m, void *v)
162304a8682aSAndrei Vagin {
162404a8682aSAndrei Vagin struct task_struct *p;
162504a8682aSAndrei Vagin
162604a8682aSAndrei Vagin p = get_proc_task(file_inode(m->file));
162704a8682aSAndrei Vagin if (!p)
162804a8682aSAndrei Vagin return -ESRCH;
162904a8682aSAndrei Vagin proc_timens_show_offsets(p, m);
163004a8682aSAndrei Vagin
163104a8682aSAndrei Vagin put_task_struct(p);
163204a8682aSAndrei Vagin
163304a8682aSAndrei Vagin return 0;
163404a8682aSAndrei Vagin }
163504a8682aSAndrei Vagin
timens_offsets_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)163604a8682aSAndrei Vagin static ssize_t timens_offsets_write(struct file *file, const char __user *buf,
163704a8682aSAndrei Vagin size_t count, loff_t *ppos)
163804a8682aSAndrei Vagin {
163904a8682aSAndrei Vagin struct inode *inode = file_inode(file);
164004a8682aSAndrei Vagin struct proc_timens_offset offsets[2];
164104a8682aSAndrei Vagin char *kbuf = NULL, *pos, *next_line;
164204a8682aSAndrei Vagin struct task_struct *p;
164304a8682aSAndrei Vagin int ret, noffsets;
164404a8682aSAndrei Vagin
164504a8682aSAndrei Vagin /* Only allow < page size writes at the beginning of the file */
164604a8682aSAndrei Vagin if ((*ppos != 0) || (count >= PAGE_SIZE))
164704a8682aSAndrei Vagin return -EINVAL;
164804a8682aSAndrei Vagin
164904a8682aSAndrei Vagin /* Slurp in the user data */
165004a8682aSAndrei Vagin kbuf = memdup_user_nul(buf, count);
165104a8682aSAndrei Vagin if (IS_ERR(kbuf))
165204a8682aSAndrei Vagin return PTR_ERR(kbuf);
165304a8682aSAndrei Vagin
165404a8682aSAndrei Vagin /* Parse the user data */
165504a8682aSAndrei Vagin ret = -EINVAL;
165604a8682aSAndrei Vagin noffsets = 0;
165704a8682aSAndrei Vagin for (pos = kbuf; pos; pos = next_line) {
165804a8682aSAndrei Vagin struct proc_timens_offset *off = &offsets[noffsets];
165994d440d6SAndrei Vagin char clock[10];
166004a8682aSAndrei Vagin int err;
166104a8682aSAndrei Vagin
166204a8682aSAndrei Vagin /* Find the end of line and ensure we don't look past it */
166304a8682aSAndrei Vagin next_line = strchr(pos, '\n');
166404a8682aSAndrei Vagin if (next_line) {
166504a8682aSAndrei Vagin *next_line = '\0';
166604a8682aSAndrei Vagin next_line++;
166704a8682aSAndrei Vagin if (*next_line == '\0')
166804a8682aSAndrei Vagin next_line = NULL;
166904a8682aSAndrei Vagin }
167004a8682aSAndrei Vagin
167194d440d6SAndrei Vagin err = sscanf(pos, "%9s %lld %lu", clock,
167204a8682aSAndrei Vagin &off->val.tv_sec, &off->val.tv_nsec);
167304a8682aSAndrei Vagin if (err != 3 || off->val.tv_nsec >= NSEC_PER_SEC)
167404a8682aSAndrei Vagin goto out;
167594d440d6SAndrei Vagin
167694d440d6SAndrei Vagin clock[sizeof(clock) - 1] = 0;
167794d440d6SAndrei Vagin if (strcmp(clock, "monotonic") == 0 ||
167894d440d6SAndrei Vagin strcmp(clock, __stringify(CLOCK_MONOTONIC)) == 0)
167994d440d6SAndrei Vagin off->clockid = CLOCK_MONOTONIC;
168094d440d6SAndrei Vagin else if (strcmp(clock, "boottime") == 0 ||
168194d440d6SAndrei Vagin strcmp(clock, __stringify(CLOCK_BOOTTIME)) == 0)
168294d440d6SAndrei Vagin off->clockid = CLOCK_BOOTTIME;
168394d440d6SAndrei Vagin else
168494d440d6SAndrei Vagin goto out;
168594d440d6SAndrei Vagin
168604a8682aSAndrei Vagin noffsets++;
168704a8682aSAndrei Vagin if (noffsets == ARRAY_SIZE(offsets)) {
168804a8682aSAndrei Vagin if (next_line)
168904a8682aSAndrei Vagin count = next_line - kbuf;
169004a8682aSAndrei Vagin break;
169104a8682aSAndrei Vagin }
169204a8682aSAndrei Vagin }
169304a8682aSAndrei Vagin
169404a8682aSAndrei Vagin ret = -ESRCH;
169504a8682aSAndrei Vagin p = get_proc_task(inode);
169604a8682aSAndrei Vagin if (!p)
169704a8682aSAndrei Vagin goto out;
169804a8682aSAndrei Vagin ret = proc_timens_set_offset(file, p, offsets, noffsets);
169904a8682aSAndrei Vagin put_task_struct(p);
170004a8682aSAndrei Vagin if (ret)
170104a8682aSAndrei Vagin goto out;
170204a8682aSAndrei Vagin
170304a8682aSAndrei Vagin ret = count;
170404a8682aSAndrei Vagin out:
170504a8682aSAndrei Vagin kfree(kbuf);
170604a8682aSAndrei Vagin return ret;
170704a8682aSAndrei Vagin }
170804a8682aSAndrei Vagin
timens_offsets_open(struct inode * inode,struct file * filp)170904a8682aSAndrei Vagin static int timens_offsets_open(struct inode *inode, struct file *filp)
171004a8682aSAndrei Vagin {
171104a8682aSAndrei Vagin return single_open(filp, timens_offsets_show, inode);
171204a8682aSAndrei Vagin }
171304a8682aSAndrei Vagin
171404a8682aSAndrei Vagin static const struct file_operations proc_timens_offsets_operations = {
171504a8682aSAndrei Vagin .open = timens_offsets_open,
171604a8682aSAndrei Vagin .read = seq_read,
171704a8682aSAndrei Vagin .write = timens_offsets_write,
171804a8682aSAndrei Vagin .llseek = seq_lseek,
171904a8682aSAndrei Vagin .release = single_release,
172004a8682aSAndrei Vagin };
172104a8682aSAndrei Vagin #endif /* CONFIG_TIME_NS */
172204a8682aSAndrei Vagin
comm_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)17234614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
17244614a696Sjohn stultz size_t count, loff_t *offset)
17254614a696Sjohn stultz {
1726496ad9aaSAl Viro struct inode *inode = file_inode(file);
17274614a696Sjohn stultz struct task_struct *p;
17284614a696Sjohn stultz char buffer[TASK_COMM_LEN];
1729830e0fc9SDavid Rientjes const size_t maxlen = sizeof(buffer) - 1;
17304614a696Sjohn stultz
17314614a696Sjohn stultz memset(buffer, 0, sizeof(buffer));
1732830e0fc9SDavid Rientjes if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
17334614a696Sjohn stultz return -EFAULT;
17344614a696Sjohn stultz
17354614a696Sjohn stultz p = get_proc_task(inode);
17364614a696Sjohn stultz if (!p)
17374614a696Sjohn stultz return -ESRCH;
17384614a696Sjohn stultz
1739c2f273ebSOhhoon Kwon if (same_thread_group(current, p)) {
17404614a696Sjohn stultz set_task_comm(p, buffer);
1741c2f273ebSOhhoon Kwon proc_comm_connector(p);
1742c2f273ebSOhhoon Kwon }
17434614a696Sjohn stultz else
17444614a696Sjohn stultz count = -EINVAL;
17454614a696Sjohn stultz
17464614a696Sjohn stultz put_task_struct(p);
17474614a696Sjohn stultz
17484614a696Sjohn stultz return count;
17494614a696Sjohn stultz }
17504614a696Sjohn stultz
comm_show(struct seq_file * m,void * v)17514614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
17524614a696Sjohn stultz {
17534614a696Sjohn stultz struct inode *inode = m->private;
17544614a696Sjohn stultz struct task_struct *p;
17554614a696Sjohn stultz
17564614a696Sjohn stultz p = get_proc_task(inode);
17574614a696Sjohn stultz if (!p)
17584614a696Sjohn stultz return -ESRCH;
17594614a696Sjohn stultz
176088b72b31STejun Heo proc_task_name(m, p, false);
176188b72b31STejun Heo seq_putc(m, '\n');
17624614a696Sjohn stultz
17634614a696Sjohn stultz put_task_struct(p);
17644614a696Sjohn stultz
17654614a696Sjohn stultz return 0;
17664614a696Sjohn stultz }
17674614a696Sjohn stultz
comm_open(struct inode * inode,struct file * filp)17684614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
17694614a696Sjohn stultz {
1770c6a34058SJovi Zhang return single_open(filp, comm_show, inode);
17714614a696Sjohn stultz }
17724614a696Sjohn stultz
17734614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
17744614a696Sjohn stultz .open = comm_open,
17754614a696Sjohn stultz .read = seq_read,
17764614a696Sjohn stultz .write = comm_write,
17774614a696Sjohn stultz .llseek = seq_lseek,
17784614a696Sjohn stultz .release = single_release,
17794614a696Sjohn stultz };
17804614a696Sjohn stultz
proc_exe_link(struct dentry * dentry,struct path * exe_path)17817773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1782925d1c40SMatt Helsley {
1783925d1c40SMatt Helsley struct task_struct *task;
1784925d1c40SMatt Helsley struct file *exe_file;
1785925d1c40SMatt Helsley
17862b0143b5SDavid Howells task = get_proc_task(d_inode(dentry));
1787925d1c40SMatt Helsley if (!task)
1788925d1c40SMatt Helsley return -ENOENT;
1789cd81a917SMateusz Guzik exe_file = get_task_exe_file(task);
1790925d1c40SMatt Helsley put_task_struct(task);
1791925d1c40SMatt Helsley if (exe_file) {
1792925d1c40SMatt Helsley *exe_path = exe_file->f_path;
1793925d1c40SMatt Helsley path_get(&exe_file->f_path);
1794925d1c40SMatt Helsley fput(exe_file);
1795925d1c40SMatt Helsley return 0;
1796925d1c40SMatt Helsley } else
1797925d1c40SMatt Helsley return -ENOENT;
1798925d1c40SMatt Helsley }
1799925d1c40SMatt Helsley
proc_pid_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)18006b255391SAl Viro static const char *proc_pid_get_link(struct dentry *dentry,
1801fceef393SAl Viro struct inode *inode,
1802fceef393SAl Viro struct delayed_call *done)
18031da177e4SLinus Torvalds {
1804408ef013SChristoph Hellwig struct path path;
18051da177e4SLinus Torvalds int error = -EACCES;
18061da177e4SLinus Torvalds
18076b255391SAl Viro if (!dentry)
18086b255391SAl Viro return ERR_PTR(-ECHILD);
18096b255391SAl Viro
1810778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */
1811778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode))
18121da177e4SLinus Torvalds goto out;
18131da177e4SLinus Torvalds
1814408ef013SChristoph Hellwig error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1815408ef013SChristoph Hellwig if (error)
1816408ef013SChristoph Hellwig goto out;
1817408ef013SChristoph Hellwig
18181bc82070SAleksa Sarai error = nd_jump_link(&path);
18191da177e4SLinus Torvalds out:
1820008b150aSAl Viro return ERR_PTR(error);
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds
do_proc_readlink(const struct path * path,char __user * buffer,int buflen)18235b09c9feSAl Viro static int do_proc_readlink(const struct path *path, char __user *buffer, int buflen)
18241da177e4SLinus Torvalds {
18253a72917cSHao Lee char *tmp = kmalloc(PATH_MAX, GFP_KERNEL);
18263dcd25f3SJan Blunck char *pathname;
18271da177e4SLinus Torvalds int len;
18281da177e4SLinus Torvalds
18291da177e4SLinus Torvalds if (!tmp)
18301da177e4SLinus Torvalds return -ENOMEM;
18311da177e4SLinus Torvalds
18323a72917cSHao Lee pathname = d_path(path, tmp, PATH_MAX);
18333dcd25f3SJan Blunck len = PTR_ERR(pathname);
18343dcd25f3SJan Blunck if (IS_ERR(pathname))
18351da177e4SLinus Torvalds goto out;
18363a72917cSHao Lee len = tmp + PATH_MAX - 1 - pathname;
18371da177e4SLinus Torvalds
18381da177e4SLinus Torvalds if (len > buflen)
18391da177e4SLinus Torvalds len = buflen;
18403dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len))
18411da177e4SLinus Torvalds len = -EFAULT;
18421da177e4SLinus Torvalds out:
18433a72917cSHao Lee kfree(tmp);
18441da177e4SLinus Torvalds return len;
18451da177e4SLinus Torvalds }
18461da177e4SLinus Torvalds
proc_pid_readlink(struct dentry * dentry,char __user * buffer,int buflen)18471da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
18481da177e4SLinus Torvalds {
18491da177e4SLinus Torvalds int error = -EACCES;
18502b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
18513dcd25f3SJan Blunck struct path path;
18521da177e4SLinus Torvalds
1853778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */
1854778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode))
18551da177e4SLinus Torvalds goto out;
18561da177e4SLinus Torvalds
18577773fbc5SCyrill Gorcunov error = PROC_I(inode)->op.proc_get_link(dentry, &path);
18581da177e4SLinus Torvalds if (error)
18591da177e4SLinus Torvalds goto out;
18601da177e4SLinus Torvalds
18613dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen);
18623dcd25f3SJan Blunck path_put(&path);
18631da177e4SLinus Torvalds out:
18641da177e4SLinus Torvalds return error;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds
1867faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = {
18681da177e4SLinus Torvalds .readlink = proc_pid_readlink,
18696b255391SAl Viro .get_link = proc_pid_get_link,
18706d76fa58SLinus Torvalds .setattr = proc_setattr,
18711da177e4SLinus Torvalds };
18721da177e4SLinus Torvalds
187328a6d671SEric W. Biederman
187428a6d671SEric W. Biederman /* building an inode */
187528a6d671SEric W. Biederman
task_dump_owner(struct task_struct * task,umode_t mode,kuid_t * ruid,kgid_t * rgid)1876c6eb50d2SAl Viro void task_dump_owner(struct task_struct *task, umode_t mode,
187768eb94f1SEric W. Biederman kuid_t *ruid, kgid_t *rgid)
187868eb94f1SEric W. Biederman {
187968eb94f1SEric W. Biederman /* Depending on the state of dumpable compute who should own a
188068eb94f1SEric W. Biederman * proc file for a task.
188168eb94f1SEric W. Biederman */
188268eb94f1SEric W. Biederman const struct cred *cred;
188368eb94f1SEric W. Biederman kuid_t uid;
188468eb94f1SEric W. Biederman kgid_t gid;
188568eb94f1SEric W. Biederman
18862e0ad552SAlexey Dobriyan if (unlikely(task->flags & PF_KTHREAD)) {
18872e0ad552SAlexey Dobriyan *ruid = GLOBAL_ROOT_UID;
18882e0ad552SAlexey Dobriyan *rgid = GLOBAL_ROOT_GID;
18892e0ad552SAlexey Dobriyan return;
18902e0ad552SAlexey Dobriyan }
18912e0ad552SAlexey Dobriyan
189268eb94f1SEric W. Biederman /* Default to the tasks effective ownership */
189368eb94f1SEric W. Biederman rcu_read_lock();
189468eb94f1SEric W. Biederman cred = __task_cred(task);
189568eb94f1SEric W. Biederman uid = cred->euid;
189668eb94f1SEric W. Biederman gid = cred->egid;
189768eb94f1SEric W. Biederman rcu_read_unlock();
189868eb94f1SEric W. Biederman
189968eb94f1SEric W. Biederman /*
190068eb94f1SEric W. Biederman * Before the /proc/pid/status file was created the only way to read
190168eb94f1SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading
190268eb94f1SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages
190368eb94f1SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have
190468eb94f1SEric W. Biederman * made this apply to all per process world readable and executable
190568eb94f1SEric W. Biederman * directories.
190668eb94f1SEric W. Biederman */
190768eb94f1SEric W. Biederman if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
190868eb94f1SEric W. Biederman struct mm_struct *mm;
190968eb94f1SEric W. Biederman task_lock(task);
191068eb94f1SEric W. Biederman mm = task->mm;
191168eb94f1SEric W. Biederman /* Make non-dumpable tasks owned by some root */
191268eb94f1SEric W. Biederman if (mm) {
191368eb94f1SEric W. Biederman if (get_dumpable(mm) != SUID_DUMP_USER) {
191468eb94f1SEric W. Biederman struct user_namespace *user_ns = mm->user_ns;
191568eb94f1SEric W. Biederman
191668eb94f1SEric W. Biederman uid = make_kuid(user_ns, 0);
191768eb94f1SEric W. Biederman if (!uid_valid(uid))
191868eb94f1SEric W. Biederman uid = GLOBAL_ROOT_UID;
191968eb94f1SEric W. Biederman
192068eb94f1SEric W. Biederman gid = make_kgid(user_ns, 0);
192168eb94f1SEric W. Biederman if (!gid_valid(gid))
192268eb94f1SEric W. Biederman gid = GLOBAL_ROOT_GID;
192368eb94f1SEric W. Biederman }
192468eb94f1SEric W. Biederman } else {
192568eb94f1SEric W. Biederman uid = GLOBAL_ROOT_UID;
192668eb94f1SEric W. Biederman gid = GLOBAL_ROOT_GID;
192768eb94f1SEric W. Biederman }
192868eb94f1SEric W. Biederman task_unlock(task);
192968eb94f1SEric W. Biederman }
193068eb94f1SEric W. Biederman *ruid = uid;
193168eb94f1SEric W. Biederman *rgid = gid;
193268eb94f1SEric W. Biederman }
193368eb94f1SEric W. Biederman
proc_pid_evict_inode(struct proc_inode * ei)19347bc3e6e5SEric W. Biederman void proc_pid_evict_inode(struct proc_inode *ei)
19357bc3e6e5SEric W. Biederman {
19367bc3e6e5SEric W. Biederman struct pid *pid = ei->pid;
19377bc3e6e5SEric W. Biederman
19387bc3e6e5SEric W. Biederman if (S_ISDIR(ei->vfs_inode.i_mode)) {
193963f818f4SEric W. Biederman spin_lock(&pid->lock);
19407bc3e6e5SEric W. Biederman hlist_del_init_rcu(&ei->sibling_inodes);
194163f818f4SEric W. Biederman spin_unlock(&pid->lock);
19427bc3e6e5SEric W. Biederman }
19437bc3e6e5SEric W. Biederman
19447bc3e6e5SEric W. Biederman put_pid(pid);
19457bc3e6e5SEric W. Biederman }
19467bc3e6e5SEric W. Biederman
proc_pid_make_inode(struct super_block * sb,struct task_struct * task,umode_t mode)1947db978da8SAndreas Gruenbacher struct inode *proc_pid_make_inode(struct super_block *sb,
1948db978da8SAndreas Gruenbacher struct task_struct *task, umode_t mode)
194928a6d671SEric W. Biederman {
195028a6d671SEric W. Biederman struct inode * inode;
195128a6d671SEric W. Biederman struct proc_inode *ei;
19527bc3e6e5SEric W. Biederman struct pid *pid;
195328a6d671SEric W. Biederman
195428a6d671SEric W. Biederman /* We need a new inode */
195528a6d671SEric W. Biederman
195628a6d671SEric W. Biederman inode = new_inode(sb);
195728a6d671SEric W. Biederman if (!inode)
195828a6d671SEric W. Biederman goto out;
195928a6d671SEric W. Biederman
196028a6d671SEric W. Biederman /* Common stuff */
196128a6d671SEric W. Biederman ei = PROC_I(inode);
1962db978da8SAndreas Gruenbacher inode->i_mode = mode;
196385fe4025SChristoph Hellwig inode->i_ino = get_next_ino();
1964e9d7d3cbSJeff Layton inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
196528a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations;
196628a6d671SEric W. Biederman
196728a6d671SEric W. Biederman /*
196828a6d671SEric W. Biederman * grab the reference to task.
196928a6d671SEric W. Biederman */
19707bc3e6e5SEric W. Biederman pid = get_task_pid(task, PIDTYPE_PID);
19717bc3e6e5SEric W. Biederman if (!pid)
197228a6d671SEric W. Biederman goto out_unlock;
197328a6d671SEric W. Biederman
19747bc3e6e5SEric W. Biederman /* Let the pid remember us for quick removal */
19757bc3e6e5SEric W. Biederman ei->pid = pid;
19767bc3e6e5SEric W. Biederman
197768eb94f1SEric W. Biederman task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
197828a6d671SEric W. Biederman security_task_to_inode(task, inode);
197928a6d671SEric W. Biederman
198028a6d671SEric W. Biederman out:
198128a6d671SEric W. Biederman return inode;
198228a6d671SEric W. Biederman
198328a6d671SEric W. Biederman out_unlock:
198428a6d671SEric W. Biederman iput(inode);
198528a6d671SEric W. Biederman return NULL;
198628a6d671SEric W. Biederman }
198728a6d671SEric W. Biederman
1988d919a1e7SZhihao Cheng /*
1989d919a1e7SZhihao Cheng * Generating an inode and adding it into @pid->inodes, so that task will
1990d919a1e7SZhihao Cheng * invalidate inode's dentry before being released.
1991d919a1e7SZhihao Cheng *
1992d919a1e7SZhihao Cheng * This helper is used for creating dir-type entries under '/proc' and
1993d919a1e7SZhihao Cheng * '/proc/<tgid>/task'. Other entries(eg. fd, stat) under '/proc/<tgid>'
1994d919a1e7SZhihao Cheng * can be released by invalidating '/proc/<tgid>' dentry.
1995d919a1e7SZhihao Cheng * In theory, dentries under '/proc/<tgid>/task' can also be released by
1996d919a1e7SZhihao Cheng * invalidating '/proc/<tgid>' dentry, we reserve it to handle single
1997d919a1e7SZhihao Cheng * thread exiting situation: Any one of threads should invalidate its
1998d919a1e7SZhihao Cheng * '/proc/<tgid>/task/<pid>' dentry before released.
1999d919a1e7SZhihao Cheng */
proc_pid_make_base_inode(struct super_block * sb,struct task_struct * task,umode_t mode)2000d919a1e7SZhihao Cheng static struct inode *proc_pid_make_base_inode(struct super_block *sb,
2001d919a1e7SZhihao Cheng struct task_struct *task, umode_t mode)
2002d919a1e7SZhihao Cheng {
2003d919a1e7SZhihao Cheng struct inode *inode;
2004d919a1e7SZhihao Cheng struct proc_inode *ei;
2005d919a1e7SZhihao Cheng struct pid *pid;
2006d919a1e7SZhihao Cheng
2007d919a1e7SZhihao Cheng inode = proc_pid_make_inode(sb, task, mode);
2008d919a1e7SZhihao Cheng if (!inode)
2009d919a1e7SZhihao Cheng return NULL;
2010d919a1e7SZhihao Cheng
2011d919a1e7SZhihao Cheng /* Let proc_flush_pid find this directory inode */
2012d919a1e7SZhihao Cheng ei = PROC_I(inode);
2013d919a1e7SZhihao Cheng pid = ei->pid;
2014d919a1e7SZhihao Cheng spin_lock(&pid->lock);
2015d919a1e7SZhihao Cheng hlist_add_head_rcu(&ei->sibling_inodes, &pid->inodes);
2016d919a1e7SZhihao Cheng spin_unlock(&pid->lock);
2017d919a1e7SZhihao Cheng
2018d919a1e7SZhihao Cheng return inode;
2019d919a1e7SZhihao Cheng }
2020d919a1e7SZhihao Cheng
pid_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)2021b74d24f7SChristian Brauner int pid_getattr(struct mnt_idmap *idmap, const struct path *path,
2022549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int query_flags)
202328a6d671SEric W. Biederman {
2024a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry);
2025fa10fed3SAlexey Gladkov struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
202628a6d671SEric W. Biederman struct task_struct *task;
2027c69e8d9cSDavid Howells
20280d72b928SJeff Layton generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
202928a6d671SEric W. Biederman
2030dcb0f222SEric W. Biederman stat->uid = GLOBAL_ROOT_UID;
2031dcb0f222SEric W. Biederman stat->gid = GLOBAL_ROOT_GID;
203294116929SAlexey Dobriyan rcu_read_lock();
203328a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID);
203428a6d671SEric W. Biederman if (task) {
2035fa10fed3SAlexey Gladkov if (!has_pid_permissions(fs_info, task, HIDEPID_INVISIBLE)) {
20360499680aSVasiliy Kulikov rcu_read_unlock();
20370499680aSVasiliy Kulikov /*
20380499680aSVasiliy Kulikov * This doesn't prevent learning whether PID exists,
20390499680aSVasiliy Kulikov * it only makes getattr() consistent with readdir().
20400499680aSVasiliy Kulikov */
20410499680aSVasiliy Kulikov return -ENOENT;
20420499680aSVasiliy Kulikov }
204368eb94f1SEric W. Biederman task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
204428a6d671SEric W. Biederman }
204528a6d671SEric W. Biederman rcu_read_unlock();
204628a6d671SEric W. Biederman return 0;
204728a6d671SEric W. Biederman }
204828a6d671SEric W. Biederman
204928a6d671SEric W. Biederman /* dentry stuff */
205028a6d671SEric W. Biederman
205128a6d671SEric W. Biederman /*
20521bbc5513SAl Viro * Set <pid>/... inode ownership (can change due to setuid(), etc.)
20531bbc5513SAl Viro */
pid_update_inode(struct task_struct * task,struct inode * inode)20541bbc5513SAl Viro void pid_update_inode(struct task_struct *task, struct inode *inode)
20551bbc5513SAl Viro {
20561bbc5513SAl Viro task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
20571bbc5513SAl Viro
20581bbc5513SAl Viro inode->i_mode &= ~(S_ISUID | S_ISGID);
20591bbc5513SAl Viro security_task_to_inode(task, inode);
20601bbc5513SAl Viro }
20611bbc5513SAl Viro
20621bbc5513SAl Viro /*
206328a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have
206428a6d671SEric W. Biederman * performed a setuid(), etc.
206528a6d671SEric W. Biederman *
206628a6d671SEric W. Biederman */
pid_revalidate(struct dentry * dentry,unsigned int flags)20671bbc5513SAl Viro static int pid_revalidate(struct dentry *dentry, unsigned int flags)
206828a6d671SEric W. Biederman {
206934286d66SNick Piggin struct inode *inode;
207034286d66SNick Piggin struct task_struct *task;
2071da4d6b9cSStephen Brennan int ret = 0;
2072c69e8d9cSDavid Howells
2073da4d6b9cSStephen Brennan rcu_read_lock();
2074da4d6b9cSStephen Brennan inode = d_inode_rcu(dentry);
2075da4d6b9cSStephen Brennan if (!inode)
2076da4d6b9cSStephen Brennan goto out;
2077da4d6b9cSStephen Brennan task = pid_task(proc_pid(inode), PIDTYPE_PID);
207834286d66SNick Piggin
207928a6d671SEric W. Biederman if (task) {
20801bbc5513SAl Viro pid_update_inode(task, inode);
2081da4d6b9cSStephen Brennan ret = 1;
208228a6d671SEric W. Biederman }
2083da4d6b9cSStephen Brennan out:
2084da4d6b9cSStephen Brennan rcu_read_unlock();
2085da4d6b9cSStephen Brennan return ret;
208628a6d671SEric W. Biederman }
208728a6d671SEric W. Biederman
proc_inode_is_dead(struct inode * inode)2088d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode)
2089d855a4b7SOleg Nesterov {
2090d855a4b7SOleg Nesterov return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
2091d855a4b7SOleg Nesterov }
2092d855a4b7SOleg Nesterov
pid_delete_dentry(const struct dentry * dentry)20931dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry)
20941dd704b6SDavid Howells {
20951dd704b6SDavid Howells /* Is the task we represent dead?
20961dd704b6SDavid Howells * If so, then don't put the dentry on the lru list,
20971dd704b6SDavid Howells * kill it immediately.
20981dd704b6SDavid Howells */
20992b0143b5SDavid Howells return proc_inode_is_dead(d_inode(dentry));
21001dd704b6SDavid Howells }
21011dd704b6SDavid Howells
21026b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
210328a6d671SEric W. Biederman {
210428a6d671SEric W. Biederman .d_revalidate = pid_revalidate,
210528a6d671SEric W. Biederman .d_delete = pid_delete_dentry,
210628a6d671SEric W. Biederman };
210728a6d671SEric W. Biederman
210828a6d671SEric W. Biederman /* Lookups */
210928a6d671SEric W. Biederman
21101c0d04c9SEric W. Biederman /*
21111c0d04c9SEric W. Biederman * Fill a directory entry.
21121c0d04c9SEric W. Biederman *
21131c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and
21141c0d04c9SEric W. Biederman * file type from dcache entry.
21151c0d04c9SEric W. Biederman *
21161c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode
2117d2928e85SRandy Dunlap * numbers do not exist until the inode is cache. This means creating
21181c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers
21191c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported
21201c0d04c9SEric W. Biederman * by stat.
21211c0d04c9SEric W. Biederman */
proc_fill_cache(struct file * file,struct dir_context * ctx,const char * name,unsigned int len,instantiate_t instantiate,struct task_struct * task,const void * ptr)2122f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx,
2123a4ef3895SAlexey Dobriyan const char *name, unsigned int len,
2124c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr)
212561a28784SEric W. Biederman {
2126f0c3b509SAl Viro struct dentry *child, *dir = file->f_path.dentry;
21271df98b8bSAl Viro struct qstr qname = QSTR_INIT(name, len);
212861a28784SEric W. Biederman struct inode *inode;
21290168b9e3SAl Viro unsigned type = DT_UNKNOWN;
21300168b9e3SAl Viro ino_t ino = 1;
213161a28784SEric W. Biederman
21321df98b8bSAl Viro child = d_hash_and_lookup(dir, &qname);
213361a28784SEric W. Biederman if (!child) {
21343781764bSAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
21353781764bSAl Viro child = d_alloc_parallel(dir, &qname, &wq);
21363781764bSAl Viro if (IS_ERR(child))
213761a28784SEric W. Biederman goto end_instantiate;
21383781764bSAl Viro if (d_in_lookup(child)) {
21390168b9e3SAl Viro struct dentry *res;
21400168b9e3SAl Viro res = instantiate(child, task, ptr);
21413781764bSAl Viro d_lookup_done(child);
21420168b9e3SAl Viro if (unlikely(res)) {
21430168b9e3SAl Viro dput(child);
21440168b9e3SAl Viro child = res;
2145d85b399bSAl Viro if (IS_ERR(child))
2146d85b399bSAl Viro goto end_instantiate;
21471df98b8bSAl Viro }
21481df98b8bSAl Viro }
21493781764bSAl Viro }
21502b0143b5SDavid Howells inode = d_inode(child);
215161a28784SEric W. Biederman ino = inode->i_ino;
215261a28784SEric W. Biederman type = inode->i_mode >> 12;
215361a28784SEric W. Biederman dput(child);
2154d85b399bSAl Viro end_instantiate:
2155f0c3b509SAl Viro return dir_emit(ctx, name, len, ino, type);
215661a28784SEric W. Biederman }
215761a28784SEric W. Biederman
2158640708a2SPavel Emelyanov /*
2159640708a2SPavel Emelyanov * dname_to_vma_addr - maps a dentry name into two unsigned longs
2160640708a2SPavel Emelyanov * which represent vma start and end addresses.
2161640708a2SPavel Emelyanov */
dname_to_vma_addr(struct dentry * dentry,unsigned long * start,unsigned long * end)2162640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
2163640708a2SPavel Emelyanov unsigned long *start, unsigned long *end)
2164640708a2SPavel Emelyanov {
2165ac7f1061SAlexey Dobriyan const char *str = dentry->d_name.name;
2166ac7f1061SAlexey Dobriyan unsigned long long sval, eval;
2167ac7f1061SAlexey Dobriyan unsigned int len;
2168ac7f1061SAlexey Dobriyan
216935318db5SAlexey Dobriyan if (str[0] == '0' && str[1] != '-')
217035318db5SAlexey Dobriyan return -EINVAL;
2171ac7f1061SAlexey Dobriyan len = _parse_integer(str, 16, &sval);
2172ac7f1061SAlexey Dobriyan if (len & KSTRTOX_OVERFLOW)
2173640708a2SPavel Emelyanov return -EINVAL;
2174ac7f1061SAlexey Dobriyan if (sval != (unsigned long)sval)
2175ac7f1061SAlexey Dobriyan return -EINVAL;
2176ac7f1061SAlexey Dobriyan str += len;
2177ac7f1061SAlexey Dobriyan
2178ac7f1061SAlexey Dobriyan if (*str != '-')
2179ac7f1061SAlexey Dobriyan return -EINVAL;
2180ac7f1061SAlexey Dobriyan str++;
2181ac7f1061SAlexey Dobriyan
218235318db5SAlexey Dobriyan if (str[0] == '0' && str[1])
218335318db5SAlexey Dobriyan return -EINVAL;
2184ac7f1061SAlexey Dobriyan len = _parse_integer(str, 16, &eval);
2185ac7f1061SAlexey Dobriyan if (len & KSTRTOX_OVERFLOW)
2186ac7f1061SAlexey Dobriyan return -EINVAL;
2187ac7f1061SAlexey Dobriyan if (eval != (unsigned long)eval)
2188ac7f1061SAlexey Dobriyan return -EINVAL;
2189ac7f1061SAlexey Dobriyan str += len;
2190ac7f1061SAlexey Dobriyan
2191ac7f1061SAlexey Dobriyan if (*str != '\0')
2192ac7f1061SAlexey Dobriyan return -EINVAL;
2193ac7f1061SAlexey Dobriyan
2194ac7f1061SAlexey Dobriyan *start = sval;
2195ac7f1061SAlexey Dobriyan *end = eval;
2196640708a2SPavel Emelyanov
2197640708a2SPavel Emelyanov return 0;
2198640708a2SPavel Emelyanov }
2199640708a2SPavel Emelyanov
map_files_d_revalidate(struct dentry * dentry,unsigned int flags)22000b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
2201640708a2SPavel Emelyanov {
2202640708a2SPavel Emelyanov unsigned long vm_start, vm_end;
2203640708a2SPavel Emelyanov bool exact_vma_exists = false;
2204640708a2SPavel Emelyanov struct mm_struct *mm = NULL;
2205640708a2SPavel Emelyanov struct task_struct *task;
2206640708a2SPavel Emelyanov struct inode *inode;
2207640708a2SPavel Emelyanov int status = 0;
2208640708a2SPavel Emelyanov
22090b728e19SAl Viro if (flags & LOOKUP_RCU)
2210640708a2SPavel Emelyanov return -ECHILD;
2211640708a2SPavel Emelyanov
22122b0143b5SDavid Howells inode = d_inode(dentry);
2213640708a2SPavel Emelyanov task = get_proc_task(inode);
2214640708a2SPavel Emelyanov if (!task)
2215640708a2SPavel Emelyanov goto out_notask;
2216640708a2SPavel Emelyanov
2217caaee623SJann Horn mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
22182344bec7SCong Wang if (IS_ERR_OR_NULL(mm))
2219640708a2SPavel Emelyanov goto out;
2220640708a2SPavel Emelyanov
2221640708a2SPavel Emelyanov if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2222d8ed45c5SMichel Lespinasse status = mmap_read_lock_killable(mm);
2223cd9e2bb8SKonstantin Khlebnikov if (!status) {
2224cd9e2bb8SKonstantin Khlebnikov exact_vma_exists = !!find_exact_vma(mm, vm_start,
2225cd9e2bb8SKonstantin Khlebnikov vm_end);
2226d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
2227640708a2SPavel Emelyanov }
2228cd9e2bb8SKonstantin Khlebnikov }
2229640708a2SPavel Emelyanov
2230640708a2SPavel Emelyanov mmput(mm);
2231640708a2SPavel Emelyanov
2232640708a2SPavel Emelyanov if (exact_vma_exists) {
223368eb94f1SEric W. Biederman task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
223468eb94f1SEric W. Biederman
2235640708a2SPavel Emelyanov security_task_to_inode(task, inode);
2236640708a2SPavel Emelyanov status = 1;
2237640708a2SPavel Emelyanov }
2238640708a2SPavel Emelyanov
2239640708a2SPavel Emelyanov out:
2240640708a2SPavel Emelyanov put_task_struct(task);
2241640708a2SPavel Emelyanov
2242640708a2SPavel Emelyanov out_notask:
2243640708a2SPavel Emelyanov return status;
2244640708a2SPavel Emelyanov }
2245640708a2SPavel Emelyanov
2246640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
2247640708a2SPavel Emelyanov .d_revalidate = map_files_d_revalidate,
2248640708a2SPavel Emelyanov .d_delete = pid_delete_dentry,
2249640708a2SPavel Emelyanov };
2250640708a2SPavel Emelyanov
map_files_get_link(struct dentry * dentry,struct path * path)22516b255391SAl Viro static int map_files_get_link(struct dentry *dentry, struct path *path)
2252640708a2SPavel Emelyanov {
2253640708a2SPavel Emelyanov unsigned long vm_start, vm_end;
2254640708a2SPavel Emelyanov struct vm_area_struct *vma;
2255640708a2SPavel Emelyanov struct task_struct *task;
2256640708a2SPavel Emelyanov struct mm_struct *mm;
2257640708a2SPavel Emelyanov int rc;
2258640708a2SPavel Emelyanov
2259640708a2SPavel Emelyanov rc = -ENOENT;
22602b0143b5SDavid Howells task = get_proc_task(d_inode(dentry));
2261640708a2SPavel Emelyanov if (!task)
2262640708a2SPavel Emelyanov goto out;
2263640708a2SPavel Emelyanov
2264640708a2SPavel Emelyanov mm = get_task_mm(task);
2265640708a2SPavel Emelyanov put_task_struct(task);
2266640708a2SPavel Emelyanov if (!mm)
2267640708a2SPavel Emelyanov goto out;
2268640708a2SPavel Emelyanov
2269640708a2SPavel Emelyanov rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2270640708a2SPavel Emelyanov if (rc)
2271640708a2SPavel Emelyanov goto out_mmput;
2272640708a2SPavel Emelyanov
2273d8ed45c5SMichel Lespinasse rc = mmap_read_lock_killable(mm);
2274cd9e2bb8SKonstantin Khlebnikov if (rc)
2275cd9e2bb8SKonstantin Khlebnikov goto out_mmput;
2276cd9e2bb8SKonstantin Khlebnikov
227770335abbSArtem Fetishev rc = -ENOENT;
2278640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end);
2279640708a2SPavel Emelyanov if (vma && vma->vm_file) {
2280640708a2SPavel Emelyanov *path = vma->vm_file->f_path;
2281640708a2SPavel Emelyanov path_get(path);
2282640708a2SPavel Emelyanov rc = 0;
2283640708a2SPavel Emelyanov }
2284d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
2285640708a2SPavel Emelyanov
2286640708a2SPavel Emelyanov out_mmput:
2287640708a2SPavel Emelyanov mmput(mm);
2288640708a2SPavel Emelyanov out:
2289640708a2SPavel Emelyanov return rc;
2290640708a2SPavel Emelyanov }
2291640708a2SPavel Emelyanov
2292640708a2SPavel Emelyanov struct map_files_info {
229320d28cdeSAlexey Dobriyan unsigned long start;
229420d28cdeSAlexey Dobriyan unsigned long end;
22957b540d06SAl Viro fmode_t mode;
2296640708a2SPavel Emelyanov };
2297640708a2SPavel Emelyanov
2298bdb4d100SCalvin Owens /*
229912886f8aSAdrian Reber * Only allow CAP_SYS_ADMIN and CAP_CHECKPOINT_RESTORE to follow the links, due
230012886f8aSAdrian Reber * to concerns about how the symlinks may be used to bypass permissions on
230112886f8aSAdrian Reber * ancestor directories in the path to the file in question.
2302bdb4d100SCalvin Owens */
2303bdb4d100SCalvin Owens static const char *
proc_map_files_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)23046b255391SAl Viro proc_map_files_get_link(struct dentry *dentry,
2305fceef393SAl Viro struct inode *inode,
2306fceef393SAl Viro struct delayed_call *done)
2307bdb4d100SCalvin Owens {
230812886f8aSAdrian Reber if (!checkpoint_restore_ns_capable(&init_user_ns))
2309bdb4d100SCalvin Owens return ERR_PTR(-EPERM);
2310bdb4d100SCalvin Owens
2311fceef393SAl Viro return proc_pid_get_link(dentry, inode, done);
2312bdb4d100SCalvin Owens }
2313bdb4d100SCalvin Owens
2314bdb4d100SCalvin Owens /*
23156b255391SAl Viro * Identical to proc_pid_link_inode_operations except for get_link()
2316bdb4d100SCalvin Owens */
2317bdb4d100SCalvin Owens static const struct inode_operations proc_map_files_link_inode_operations = {
2318bdb4d100SCalvin Owens .readlink = proc_pid_readlink,
23196b255391SAl Viro .get_link = proc_map_files_get_link,
2320bdb4d100SCalvin Owens .setattr = proc_setattr,
2321bdb4d100SCalvin Owens };
2322bdb4d100SCalvin Owens
23230168b9e3SAl Viro static struct dentry *
proc_map_files_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)23240168b9e3SAl Viro proc_map_files_instantiate(struct dentry *dentry,
2325640708a2SPavel Emelyanov struct task_struct *task, const void *ptr)
2326640708a2SPavel Emelyanov {
23277b540d06SAl Viro fmode_t mode = (fmode_t)(unsigned long)ptr;
2328640708a2SPavel Emelyanov struct proc_inode *ei;
2329640708a2SPavel Emelyanov struct inode *inode;
2330640708a2SPavel Emelyanov
23310168b9e3SAl Viro inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2332db978da8SAndreas Gruenbacher ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2333db978da8SAndreas Gruenbacher ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2334640708a2SPavel Emelyanov if (!inode)
23350168b9e3SAl Viro return ERR_PTR(-ENOENT);
2336640708a2SPavel Emelyanov
2337640708a2SPavel Emelyanov ei = PROC_I(inode);
23386b255391SAl Viro ei->op.proc_get_link = map_files_get_link;
2339640708a2SPavel Emelyanov
2340bdb4d100SCalvin Owens inode->i_op = &proc_map_files_link_inode_operations;
2341640708a2SPavel Emelyanov inode->i_size = 64;
2342640708a2SPavel Emelyanov
2343640708a2SPavel Emelyanov d_set_d_op(dentry, &tid_map_files_dentry_operations);
23440168b9e3SAl Viro return d_splice_alias(inode, dentry);
2345640708a2SPavel Emelyanov }
2346640708a2SPavel Emelyanov
proc_map_files_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)2347640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
234800cd8dd3SAl Viro struct dentry *dentry, unsigned int flags)
2349640708a2SPavel Emelyanov {
2350640708a2SPavel Emelyanov unsigned long vm_start, vm_end;
2351640708a2SPavel Emelyanov struct vm_area_struct *vma;
2352640708a2SPavel Emelyanov struct task_struct *task;
23530168b9e3SAl Viro struct dentry *result;
2354640708a2SPavel Emelyanov struct mm_struct *mm;
2355640708a2SPavel Emelyanov
23560168b9e3SAl Viro result = ERR_PTR(-ENOENT);
2357640708a2SPavel Emelyanov task = get_proc_task(dir);
2358640708a2SPavel Emelyanov if (!task)
2359640708a2SPavel Emelyanov goto out;
2360640708a2SPavel Emelyanov
23610168b9e3SAl Viro result = ERR_PTR(-EACCES);
2362caaee623SJann Horn if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2363640708a2SPavel Emelyanov goto out_put_task;
2364640708a2SPavel Emelyanov
23650168b9e3SAl Viro result = ERR_PTR(-ENOENT);
2366640708a2SPavel Emelyanov if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2367eb94cd96SCyrill Gorcunov goto out_put_task;
2368640708a2SPavel Emelyanov
2369640708a2SPavel Emelyanov mm = get_task_mm(task);
2370640708a2SPavel Emelyanov if (!mm)
2371eb94cd96SCyrill Gorcunov goto out_put_task;
2372640708a2SPavel Emelyanov
2373cd9e2bb8SKonstantin Khlebnikov result = ERR_PTR(-EINTR);
2374d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm))
2375cd9e2bb8SKonstantin Khlebnikov goto out_put_mm;
2376cd9e2bb8SKonstantin Khlebnikov
2377cd9e2bb8SKonstantin Khlebnikov result = ERR_PTR(-ENOENT);
2378640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end);
2379640708a2SPavel Emelyanov if (!vma)
2380640708a2SPavel Emelyanov goto out_no_vma;
2381640708a2SPavel Emelyanov
238205f56484SStanislav Kinsbursky if (vma->vm_file)
23830168b9e3SAl Viro result = proc_map_files_instantiate(dentry, task,
23847b540d06SAl Viro (void *)(unsigned long)vma->vm_file->f_mode);
2385640708a2SPavel Emelyanov
2386640708a2SPavel Emelyanov out_no_vma:
2387d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
2388cd9e2bb8SKonstantin Khlebnikov out_put_mm:
2389640708a2SPavel Emelyanov mmput(mm);
2390640708a2SPavel Emelyanov out_put_task:
2391640708a2SPavel Emelyanov put_task_struct(task);
2392640708a2SPavel Emelyanov out:
23930168b9e3SAl Viro return result;
2394640708a2SPavel Emelyanov }
2395640708a2SPavel Emelyanov
2396640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
2397640708a2SPavel Emelyanov .lookup = proc_map_files_lookup,
2398640708a2SPavel Emelyanov .permission = proc_fd_permission,
2399640708a2SPavel Emelyanov .setattr = proc_setattr,
2400640708a2SPavel Emelyanov };
2401640708a2SPavel Emelyanov
2402640708a2SPavel Emelyanov static int
proc_map_files_readdir(struct file * file,struct dir_context * ctx)2403f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2404640708a2SPavel Emelyanov {
2405640708a2SPavel Emelyanov struct vm_area_struct *vma;
2406640708a2SPavel Emelyanov struct task_struct *task;
2407640708a2SPavel Emelyanov struct mm_struct *mm;
2408f0c3b509SAl Viro unsigned long nr_files, pos, i;
240994f8f3b0SKent Overstreet GENRADIX(struct map_files_info) fa;
2410f0c3b509SAl Viro struct map_files_info *p;
2411640708a2SPavel Emelyanov int ret;
24125f14b924SLiam R. Howlett struct vma_iterator vmi;
2413640708a2SPavel Emelyanov
241494f8f3b0SKent Overstreet genradix_init(&fa);
241594f8f3b0SKent Overstreet
2416640708a2SPavel Emelyanov ret = -ENOENT;
2417f0c3b509SAl Viro task = get_proc_task(file_inode(file));
2418640708a2SPavel Emelyanov if (!task)
2419640708a2SPavel Emelyanov goto out;
2420640708a2SPavel Emelyanov
2421640708a2SPavel Emelyanov ret = -EACCES;
2422caaee623SJann Horn if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2423640708a2SPavel Emelyanov goto out_put_task;
2424640708a2SPavel Emelyanov
2425640708a2SPavel Emelyanov ret = 0;
2426f0c3b509SAl Viro if (!dir_emit_dots(file, ctx))
2427eb94cd96SCyrill Gorcunov goto out_put_task;
2428640708a2SPavel Emelyanov
2429640708a2SPavel Emelyanov mm = get_task_mm(task);
2430640708a2SPavel Emelyanov if (!mm)
2431eb94cd96SCyrill Gorcunov goto out_put_task;
2432cd9e2bb8SKonstantin Khlebnikov
243389154dd5SMichel Lespinasse ret = mmap_read_lock_killable(mm);
2434cd9e2bb8SKonstantin Khlebnikov if (ret) {
2435cd9e2bb8SKonstantin Khlebnikov mmput(mm);
2436cd9e2bb8SKonstantin Khlebnikov goto out_put_task;
2437cd9e2bb8SKonstantin Khlebnikov }
2438640708a2SPavel Emelyanov
2439640708a2SPavel Emelyanov nr_files = 0;
2440640708a2SPavel Emelyanov
2441640708a2SPavel Emelyanov /*
2442640708a2SPavel Emelyanov * We need two passes here:
2443640708a2SPavel Emelyanov *
2444c1e8d7c6SMichel Lespinasse * 1) Collect vmas of mapped files with mmap_lock taken
2445c1e8d7c6SMichel Lespinasse * 2) Release mmap_lock and instantiate entries
2446640708a2SPavel Emelyanov *
2447640708a2SPavel Emelyanov * otherwise we get lockdep complained, since filldir()
2448c1e8d7c6SMichel Lespinasse * routine might require mmap_lock taken in might_fault().
2449640708a2SPavel Emelyanov */
2450640708a2SPavel Emelyanov
24515f14b924SLiam R. Howlett pos = 2;
24525f14b924SLiam R. Howlett vma_iter_init(&vmi, mm, 0);
24535f14b924SLiam R. Howlett for_each_vma(vmi, vma) {
2454640708a2SPavel Emelyanov if (!vma->vm_file)
2455640708a2SPavel Emelyanov continue;
2456f0c3b509SAl Viro if (++pos <= ctx->pos)
2457640708a2SPavel Emelyanov continue;
2458640708a2SPavel Emelyanov
245994f8f3b0SKent Overstreet p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
246094f8f3b0SKent Overstreet if (!p) {
246194f8f3b0SKent Overstreet ret = -ENOMEM;
246289154dd5SMichel Lespinasse mmap_read_unlock(mm);
246394f8f3b0SKent Overstreet mmput(mm);
246494f8f3b0SKent Overstreet goto out_put_task;
2465640708a2SPavel Emelyanov }
246694f8f3b0SKent Overstreet
246794f8f3b0SKent Overstreet p->start = vma->vm_start;
246894f8f3b0SKent Overstreet p->end = vma->vm_end;
246994f8f3b0SKent Overstreet p->mode = vma->vm_file->f_mode;
2470640708a2SPavel Emelyanov }
247189154dd5SMichel Lespinasse mmap_read_unlock(mm);
2472fe079a5eSAlexey Dobriyan mmput(mm);
2473640708a2SPavel Emelyanov
2474640708a2SPavel Emelyanov for (i = 0; i < nr_files; i++) {
247520d28cdeSAlexey Dobriyan char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
247620d28cdeSAlexey Dobriyan unsigned int len;
247720d28cdeSAlexey Dobriyan
247894f8f3b0SKent Overstreet p = genradix_ptr(&fa, i);
247920d28cdeSAlexey Dobriyan len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2480f0c3b509SAl Viro if (!proc_fill_cache(file, ctx,
248120d28cdeSAlexey Dobriyan buf, len,
2482640708a2SPavel Emelyanov proc_map_files_instantiate,
24837b540d06SAl Viro task,
2484f0c3b509SAl Viro (void *)(unsigned long)p->mode))
2485640708a2SPavel Emelyanov break;
2486f0c3b509SAl Viro ctx->pos++;
2487640708a2SPavel Emelyanov }
2488640708a2SPavel Emelyanov
2489640708a2SPavel Emelyanov out_put_task:
2490640708a2SPavel Emelyanov put_task_struct(task);
2491640708a2SPavel Emelyanov out:
249294f8f3b0SKent Overstreet genradix_free(&fa);
2493640708a2SPavel Emelyanov return ret;
2494640708a2SPavel Emelyanov }
2495640708a2SPavel Emelyanov
2496640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2497640708a2SPavel Emelyanov .read = generic_read_dir,
2498f50752eaSAl Viro .iterate_shared = proc_map_files_readdir,
2499f50752eaSAl Viro .llseek = generic_file_llseek,
2500640708a2SPavel Emelyanov };
2501640708a2SPavel Emelyanov
2502b18b6a9cSNicolas Pitre #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
250348f6a7a5SPavel Emelyanov struct timers_private {
250448f6a7a5SPavel Emelyanov struct pid *pid;
250548f6a7a5SPavel Emelyanov struct task_struct *task;
250648f6a7a5SPavel Emelyanov struct sighand_struct *sighand;
250757b8015eSPavel Emelyanov struct pid_namespace *ns;
250848f6a7a5SPavel Emelyanov unsigned long flags;
250948f6a7a5SPavel Emelyanov };
251048f6a7a5SPavel Emelyanov
timers_start(struct seq_file * m,loff_t * pos)251148f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos)
251248f6a7a5SPavel Emelyanov {
251348f6a7a5SPavel Emelyanov struct timers_private *tp = m->private;
251448f6a7a5SPavel Emelyanov
251548f6a7a5SPavel Emelyanov tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
251648f6a7a5SPavel Emelyanov if (!tp->task)
251748f6a7a5SPavel Emelyanov return ERR_PTR(-ESRCH);
251848f6a7a5SPavel Emelyanov
251948f6a7a5SPavel Emelyanov tp->sighand = lock_task_sighand(tp->task, &tp->flags);
252048f6a7a5SPavel Emelyanov if (!tp->sighand)
252148f6a7a5SPavel Emelyanov return ERR_PTR(-ESRCH);
252248f6a7a5SPavel Emelyanov
252348f6a7a5SPavel Emelyanov return seq_list_start(&tp->task->signal->posix_timers, *pos);
252448f6a7a5SPavel Emelyanov }
252548f6a7a5SPavel Emelyanov
timers_next(struct seq_file * m,void * v,loff_t * pos)252648f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
252748f6a7a5SPavel Emelyanov {
252848f6a7a5SPavel Emelyanov struct timers_private *tp = m->private;
252948f6a7a5SPavel Emelyanov return seq_list_next(v, &tp->task->signal->posix_timers, pos);
253048f6a7a5SPavel Emelyanov }
253148f6a7a5SPavel Emelyanov
timers_stop(struct seq_file * m,void * v)253248f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v)
253348f6a7a5SPavel Emelyanov {
253448f6a7a5SPavel Emelyanov struct timers_private *tp = m->private;
253548f6a7a5SPavel Emelyanov
253648f6a7a5SPavel Emelyanov if (tp->sighand) {
253748f6a7a5SPavel Emelyanov unlock_task_sighand(tp->task, &tp->flags);
253848f6a7a5SPavel Emelyanov tp->sighand = NULL;
253948f6a7a5SPavel Emelyanov }
254048f6a7a5SPavel Emelyanov
254148f6a7a5SPavel Emelyanov if (tp->task) {
254248f6a7a5SPavel Emelyanov put_task_struct(tp->task);
254348f6a7a5SPavel Emelyanov tp->task = NULL;
254448f6a7a5SPavel Emelyanov }
254548f6a7a5SPavel Emelyanov }
254648f6a7a5SPavel Emelyanov
show_timer(struct seq_file * m,void * v)254748f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v)
254848f6a7a5SPavel Emelyanov {
254948f6a7a5SPavel Emelyanov struct k_itimer *timer;
255057b8015eSPavel Emelyanov struct timers_private *tp = m->private;
255157b8015eSPavel Emelyanov int notify;
2552cedbccabSAlexey Dobriyan static const char * const nstr[] = {
255357b8015eSPavel Emelyanov [SIGEV_SIGNAL] = "signal",
255457b8015eSPavel Emelyanov [SIGEV_NONE] = "none",
255557b8015eSPavel Emelyanov [SIGEV_THREAD] = "thread",
255657b8015eSPavel Emelyanov };
255748f6a7a5SPavel Emelyanov
255848f6a7a5SPavel Emelyanov timer = list_entry((struct list_head *)v, struct k_itimer, list);
255957b8015eSPavel Emelyanov notify = timer->it_sigev_notify;
256057b8015eSPavel Emelyanov
256148f6a7a5SPavel Emelyanov seq_printf(m, "ID: %d\n", timer->it_id);
2562ba3edf1fSLinus Torvalds seq_printf(m, "signal: %d/%px\n",
256325ce3191SJoe Perches timer->sigq->info.si_signo,
256457b8015eSPavel Emelyanov timer->sigq->info.si_value.sival_ptr);
256557b8015eSPavel Emelyanov seq_printf(m, "notify: %s/%s.%d\n",
256657b8015eSPavel Emelyanov nstr[notify & ~SIGEV_THREAD_ID],
256757b8015eSPavel Emelyanov (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
256857b8015eSPavel Emelyanov pid_nr_ns(timer->it_pid, tp->ns));
256915ef0298SPavel Tikhomirov seq_printf(m, "ClockID: %d\n", timer->it_clock);
257048f6a7a5SPavel Emelyanov
257148f6a7a5SPavel Emelyanov return 0;
257248f6a7a5SPavel Emelyanov }
257348f6a7a5SPavel Emelyanov
257448f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = {
257548f6a7a5SPavel Emelyanov .start = timers_start,
257648f6a7a5SPavel Emelyanov .next = timers_next,
257748f6a7a5SPavel Emelyanov .stop = timers_stop,
257848f6a7a5SPavel Emelyanov .show = show_timer,
257948f6a7a5SPavel Emelyanov };
258048f6a7a5SPavel Emelyanov
proc_timers_open(struct inode * inode,struct file * file)258148f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file)
258248f6a7a5SPavel Emelyanov {
258348f6a7a5SPavel Emelyanov struct timers_private *tp;
258448f6a7a5SPavel Emelyanov
258548f6a7a5SPavel Emelyanov tp = __seq_open_private(file, &proc_timers_seq_ops,
258648f6a7a5SPavel Emelyanov sizeof(struct timers_private));
258748f6a7a5SPavel Emelyanov if (!tp)
258848f6a7a5SPavel Emelyanov return -ENOMEM;
258948f6a7a5SPavel Emelyanov
259048f6a7a5SPavel Emelyanov tp->pid = proc_pid(inode);
25919d78edeaSAlexey Gladkov tp->ns = proc_pid_ns(inode->i_sb);
259248f6a7a5SPavel Emelyanov return 0;
259348f6a7a5SPavel Emelyanov }
259448f6a7a5SPavel Emelyanov
259548f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = {
259648f6a7a5SPavel Emelyanov .open = proc_timers_open,
259748f6a7a5SPavel Emelyanov .read = seq_read,
259848f6a7a5SPavel Emelyanov .llseek = seq_lseek,
259948f6a7a5SPavel Emelyanov .release = seq_release_private,
260048f6a7a5SPavel Emelyanov };
2601b5946beaSEric Engestrom #endif
2602640708a2SPavel Emelyanov
timerslack_ns_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)26035de23d43SJohn Stultz static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
26045de23d43SJohn Stultz size_t count, loff_t *offset)
26055de23d43SJohn Stultz {
26065de23d43SJohn Stultz struct inode *inode = file_inode(file);
26075de23d43SJohn Stultz struct task_struct *p;
26085de23d43SJohn Stultz u64 slack_ns;
26095de23d43SJohn Stultz int err;
26105de23d43SJohn Stultz
26115de23d43SJohn Stultz err = kstrtoull_from_user(buf, count, 10, &slack_ns);
26125de23d43SJohn Stultz if (err < 0)
26135de23d43SJohn Stultz return err;
26145de23d43SJohn Stultz
26155de23d43SJohn Stultz p = get_proc_task(inode);
26165de23d43SJohn Stultz if (!p)
26175de23d43SJohn Stultz return -ESRCH;
26185de23d43SJohn Stultz
26194b2bd5feSJohn Stultz if (p != current) {
26208da0b4f6SBenjamin Gordon rcu_read_lock();
26218da0b4f6SBenjamin Gordon if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
26228da0b4f6SBenjamin Gordon rcu_read_unlock();
26237abbaf94SJohn Stultz count = -EPERM;
26247abbaf94SJohn Stultz goto out;
26257abbaf94SJohn Stultz }
26268da0b4f6SBenjamin Gordon rcu_read_unlock();
26277abbaf94SJohn Stultz
2628904763e1SJohn Stultz err = security_task_setscheduler(p);
2629904763e1SJohn Stultz if (err) {
2630904763e1SJohn Stultz count = err;
2631904763e1SJohn Stultz goto out;
2632904763e1SJohn Stultz }
26334b2bd5feSJohn Stultz }
2634904763e1SJohn Stultz
26355de23d43SJohn Stultz task_lock(p);
26365de23d43SJohn Stultz if (slack_ns == 0)
26375de23d43SJohn Stultz p->timer_slack_ns = p->default_timer_slack_ns;
26385de23d43SJohn Stultz else
26395de23d43SJohn Stultz p->timer_slack_ns = slack_ns;
26405de23d43SJohn Stultz task_unlock(p);
26415de23d43SJohn Stultz
26427abbaf94SJohn Stultz out:
26435de23d43SJohn Stultz put_task_struct(p);
26445de23d43SJohn Stultz
26455de23d43SJohn Stultz return count;
26465de23d43SJohn Stultz }
26475de23d43SJohn Stultz
timerslack_ns_show(struct seq_file * m,void * v)26485de23d43SJohn Stultz static int timerslack_ns_show(struct seq_file *m, void *v)
26495de23d43SJohn Stultz {
26505de23d43SJohn Stultz struct inode *inode = m->private;
26515de23d43SJohn Stultz struct task_struct *p;
26525de23d43SJohn Stultz int err = 0;
26535de23d43SJohn Stultz
26545de23d43SJohn Stultz p = get_proc_task(inode);
26555de23d43SJohn Stultz if (!p)
26565de23d43SJohn Stultz return -ESRCH;
26575de23d43SJohn Stultz
26584b2bd5feSJohn Stultz if (p != current) {
26598da0b4f6SBenjamin Gordon rcu_read_lock();
26608da0b4f6SBenjamin Gordon if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
26618da0b4f6SBenjamin Gordon rcu_read_unlock();
26627abbaf94SJohn Stultz err = -EPERM;
26637abbaf94SJohn Stultz goto out;
26647abbaf94SJohn Stultz }
26658da0b4f6SBenjamin Gordon rcu_read_unlock();
26668da0b4f6SBenjamin Gordon
2667904763e1SJohn Stultz err = security_task_getscheduler(p);
2668904763e1SJohn Stultz if (err)
2669904763e1SJohn Stultz goto out;
26704b2bd5feSJohn Stultz }
2671904763e1SJohn Stultz
26725de23d43SJohn Stultz task_lock(p);
26735de23d43SJohn Stultz seq_printf(m, "%llu\n", p->timer_slack_ns);
26745de23d43SJohn Stultz task_unlock(p);
26755de23d43SJohn Stultz
26767abbaf94SJohn Stultz out:
26775de23d43SJohn Stultz put_task_struct(p);
26785de23d43SJohn Stultz
26795de23d43SJohn Stultz return err;
26805de23d43SJohn Stultz }
26815de23d43SJohn Stultz
timerslack_ns_open(struct inode * inode,struct file * filp)26825de23d43SJohn Stultz static int timerslack_ns_open(struct inode *inode, struct file *filp)
26835de23d43SJohn Stultz {
26845de23d43SJohn Stultz return single_open(filp, timerslack_ns_show, inode);
26855de23d43SJohn Stultz }
26865de23d43SJohn Stultz
26875de23d43SJohn Stultz static const struct file_operations proc_pid_set_timerslack_ns_operations = {
26885de23d43SJohn Stultz .open = timerslack_ns_open,
26895de23d43SJohn Stultz .read = seq_read,
26905de23d43SJohn Stultz .write = timerslack_ns_write,
26915de23d43SJohn Stultz .llseek = seq_lseek,
26925de23d43SJohn Stultz .release = single_release,
26935de23d43SJohn Stultz };
26945de23d43SJohn Stultz
proc_pident_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)26950168b9e3SAl Viro static struct dentry *proc_pident_instantiate(struct dentry *dentry,
26960168b9e3SAl Viro struct task_struct *task, const void *ptr)
2697444ceed8SEric W. Biederman {
2698c5141e6dSEric Dumazet const struct pid_entry *p = ptr;
2699444ceed8SEric W. Biederman struct inode *inode;
2700444ceed8SEric W. Biederman struct proc_inode *ei;
2701444ceed8SEric W. Biederman
27020168b9e3SAl Viro inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2703444ceed8SEric W. Biederman if (!inode)
27040168b9e3SAl Viro return ERR_PTR(-ENOENT);
2705444ceed8SEric W. Biederman
2706444ceed8SEric W. Biederman ei = PROC_I(inode);
2707444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode))
2708bfe86848SMiklos Szeredi set_nlink(inode, 2); /* Use getattr to fix if necessary */
2709444ceed8SEric W. Biederman if (p->iop)
2710444ceed8SEric W. Biederman inode->i_op = p->iop;
2711444ceed8SEric W. Biederman if (p->fop)
2712444ceed8SEric W. Biederman inode->i_fop = p->fop;
2713444ceed8SEric W. Biederman ei->op = p->op;
27141bbc5513SAl Viro pid_update_inode(task, inode);
2715fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations);
27160168b9e3SAl Viro return d_splice_alias(inode, dentry);
2717444ceed8SEric W. Biederman }
2718444ceed8SEric W. Biederman
proc_pident_lookup(struct inode * dir,struct dentry * dentry,const struct pid_entry * p,const struct pid_entry * end)27191da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
27201da177e4SLinus Torvalds struct dentry *dentry,
2721d5a572a4SAlexey Dobriyan const struct pid_entry *p,
2722d5a572a4SAlexey Dobriyan const struct pid_entry *end)
27231da177e4SLinus Torvalds {
272499f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir);
27250168b9e3SAl Viro struct dentry *res = ERR_PTR(-ENOENT);
27261da177e4SLinus Torvalds
272799f89551SEric W. Biederman if (!task)
272899f89551SEric W. Biederman goto out_no_task;
27291da177e4SLinus Torvalds
273020cdc894SEric W. Biederman /*
273120cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add
273220cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons.
273320cdc894SEric W. Biederman */
2734d5a572a4SAlexey Dobriyan for (; p < end; p++) {
27351da177e4SLinus Torvalds if (p->len != dentry->d_name.len)
27361da177e4SLinus Torvalds continue;
273726b95137SAlexey Dobriyan if (!memcmp(dentry->d_name.name, p->name, p->len)) {
273826b95137SAlexey Dobriyan res = proc_pident_instantiate(dentry, task, p);
27391da177e4SLinus Torvalds break;
27401da177e4SLinus Torvalds }
274126b95137SAlexey Dobriyan }
274299f89551SEric W. Biederman put_task_struct(task);
274399f89551SEric W. Biederman out_no_task:
27440168b9e3SAl Viro return res;
27451da177e4SLinus Torvalds }
27461da177e4SLinus Torvalds
proc_pident_readdir(struct file * file,struct dir_context * ctx,const struct pid_entry * ents,unsigned int nents)2747f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2748c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents)
274928a6d671SEric W. Biederman {
2750f0c3b509SAl Viro struct task_struct *task = get_proc_task(file_inode(file));
2751f0c3b509SAl Viro const struct pid_entry *p;
275228a6d671SEric W. Biederman
275328a6d671SEric W. Biederman if (!task)
2754f0c3b509SAl Viro return -ENOENT;
275528a6d671SEric W. Biederman
2756f0c3b509SAl Viro if (!dir_emit_dots(file, ctx))
275728a6d671SEric W. Biederman goto out;
27581da177e4SLinus Torvalds
2759f0c3b509SAl Viro if (ctx->pos >= nents + 2)
2760f0c3b509SAl Viro goto out;
2761f0c3b509SAl Viro
2762bac5f5d5SAlexey Dobriyan for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2763f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, p->name, p->len,
2764f0c3b509SAl Viro proc_pident_instantiate, task, p))
2765f0c3b509SAl Viro break;
2766f0c3b509SAl Viro ctx->pos++;
2767f0c3b509SAl Viro }
276828a6d671SEric W. Biederman out:
276961a28784SEric W. Biederman put_task_struct(task);
2770f0c3b509SAl Viro return 0;
27711da177e4SLinus Torvalds }
27721da177e4SLinus Torvalds
27731da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
proc_pid_attr_open(struct inode * inode,struct file * file)2774591a22c1SKees Cook static int proc_pid_attr_open(struct inode *inode, struct file *file)
2775591a22c1SKees Cook {
277694f0b2d4SLinus Torvalds file->private_data = NULL;
277794f0b2d4SLinus Torvalds __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
277894f0b2d4SLinus Torvalds return 0;
2779591a22c1SKees Cook }
2780591a22c1SKees Cook
proc_pid_attr_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)278128a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
278228a6d671SEric W. Biederman size_t count, loff_t *ppos)
278328a6d671SEric W. Biederman {
2784496ad9aaSAl Viro struct inode * inode = file_inode(file);
278504ff9708SAl Viro char *p = NULL;
278628a6d671SEric W. Biederman ssize_t length;
278728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode);
278828a6d671SEric W. Biederman
278928a6d671SEric W. Biederman if (!task)
279004ff9708SAl Viro return -ESRCH;
279128a6d671SEric W. Biederman
27926d9c939dSCasey Schaufler length = security_getprocattr(task, PROC_I(inode)->op.lsm,
2793c8e477c6SAl Viro file->f_path.dentry->d_name.name,
279404ff9708SAl Viro &p);
279528a6d671SEric W. Biederman put_task_struct(task);
279604ff9708SAl Viro if (length > 0)
279704ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length);
279804ff9708SAl Viro kfree(p);
279928a6d671SEric W. Biederman return length;
280028a6d671SEric W. Biederman }
280128a6d671SEric W. Biederman
proc_pid_attr_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)280228a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
280328a6d671SEric W. Biederman size_t count, loff_t *ppos)
280428a6d671SEric W. Biederman {
2805496ad9aaSAl Viro struct inode * inode = file_inode(file);
280641089b6dSAlexey Dobriyan struct task_struct *task;
2807bb646cdbSAl Viro void *page;
280841089b6dSAlexey Dobriyan int rv;
280928a6d671SEric W. Biederman
2810bfb819eaSKees Cook /* A task may only write when it was the opener. */
2811591a22c1SKees Cook if (file->private_data != current->mm)
2812bfb819eaSKees Cook return -EPERM;
2813bfb819eaSKees Cook
281441089b6dSAlexey Dobriyan rcu_read_lock();
281541089b6dSAlexey Dobriyan task = pid_task(proc_pid(inode), PIDTYPE_PID);
281641089b6dSAlexey Dobriyan if (!task) {
281741089b6dSAlexey Dobriyan rcu_read_unlock();
281841089b6dSAlexey Dobriyan return -ESRCH;
281941089b6dSAlexey Dobriyan }
2820b21507e2SStephen Smalley /* A task may only write its own attributes. */
282141089b6dSAlexey Dobriyan if (current != task) {
282241089b6dSAlexey Dobriyan rcu_read_unlock();
282341089b6dSAlexey Dobriyan return -EACCES;
282441089b6dSAlexey Dobriyan }
282535a196beSPaul Moore /* Prevent changes to overridden credentials. */
282635a196beSPaul Moore if (current_cred() != current_real_cred()) {
282735a196beSPaul Moore rcu_read_unlock();
282835a196beSPaul Moore return -EBUSY;
282935a196beSPaul Moore }
283041089b6dSAlexey Dobriyan rcu_read_unlock();
2831b21507e2SStephen Smalley
283228a6d671SEric W. Biederman if (count > PAGE_SIZE)
283328a6d671SEric W. Biederman count = PAGE_SIZE;
283428a6d671SEric W. Biederman
283528a6d671SEric W. Biederman /* No partial writes. */
283628a6d671SEric W. Biederman if (*ppos != 0)
283741089b6dSAlexey Dobriyan return -EINVAL;
283828a6d671SEric W. Biederman
2839bb646cdbSAl Viro page = memdup_user(buf, count);
2840bb646cdbSAl Viro if (IS_ERR(page)) {
284141089b6dSAlexey Dobriyan rv = PTR_ERR(page);
284228a6d671SEric W. Biederman goto out;
2843bb646cdbSAl Viro }
284428a6d671SEric W. Biederman
2845107db7c7SDavid Howells /* Guard against adverse ptrace interaction */
284641089b6dSAlexey Dobriyan rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
284741089b6dSAlexey Dobriyan if (rv < 0)
2848107db7c7SDavid Howells goto out_free;
2849107db7c7SDavid Howells
28506d9c939dSCasey Schaufler rv = security_setprocattr(PROC_I(inode)->op.lsm,
28516d9c939dSCasey Schaufler file->f_path.dentry->d_name.name, page,
28526d9c939dSCasey Schaufler count);
2853b21507e2SStephen Smalley mutex_unlock(¤t->signal->cred_guard_mutex);
285428a6d671SEric W. Biederman out_free:
2855bb646cdbSAl Viro kfree(page);
285628a6d671SEric W. Biederman out:
285741089b6dSAlexey Dobriyan return rv;
285828a6d671SEric W. Biederman }
285928a6d671SEric W. Biederman
286000977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
2861591a22c1SKees Cook .open = proc_pid_attr_open,
286228a6d671SEric W. Biederman .read = proc_pid_attr_read,
286328a6d671SEric W. Biederman .write = proc_pid_attr_write,
286487df8424SArnd Bergmann .llseek = generic_file_llseek,
2865591a22c1SKees Cook .release = mem_release,
286628a6d671SEric W. Biederman };
286728a6d671SEric W. Biederman
28686d9c939dSCasey Schaufler #define LSM_DIR_OPS(LSM) \
28696d9c939dSCasey Schaufler static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
28706d9c939dSCasey Schaufler struct dir_context *ctx) \
28716d9c939dSCasey Schaufler { \
28726d9c939dSCasey Schaufler return proc_pident_readdir(filp, ctx, \
28736d9c939dSCasey Schaufler LSM##_attr_dir_stuff, \
28746d9c939dSCasey Schaufler ARRAY_SIZE(LSM##_attr_dir_stuff)); \
28756d9c939dSCasey Schaufler } \
28766d9c939dSCasey Schaufler \
28776d9c939dSCasey Schaufler static const struct file_operations proc_##LSM##_attr_dir_ops = { \
28786d9c939dSCasey Schaufler .read = generic_read_dir, \
28790a2c2baaSLinus Torvalds .iterate_shared = proc_##LSM##_attr_dir_iterate, \
28806d9c939dSCasey Schaufler .llseek = default_llseek, \
28816d9c939dSCasey Schaufler }; \
28826d9c939dSCasey Schaufler \
28836d9c939dSCasey Schaufler static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
28846d9c939dSCasey Schaufler struct dentry *dentry, unsigned int flags) \
28856d9c939dSCasey Schaufler { \
28866d9c939dSCasey Schaufler return proc_pident_lookup(dir, dentry, \
28876d9c939dSCasey Schaufler LSM##_attr_dir_stuff, \
2888d5a572a4SAlexey Dobriyan LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
28896d9c939dSCasey Schaufler } \
28906d9c939dSCasey Schaufler \
28916d9c939dSCasey Schaufler static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
28926d9c939dSCasey Schaufler .lookup = proc_##LSM##_attr_dir_lookup, \
28936d9c939dSCasey Schaufler .getattr = pid_getattr, \
28946d9c939dSCasey Schaufler .setattr = proc_setattr, \
28956d9c939dSCasey Schaufler }
28966d9c939dSCasey Schaufler
28976d9c939dSCasey Schaufler #ifdef CONFIG_SECURITY_SMACK
28986d9c939dSCasey Schaufler static const struct pid_entry smack_attr_dir_stuff[] = {
28996d9c939dSCasey Schaufler ATTR("smack", "current", 0666),
29006d9c939dSCasey Schaufler };
29016d9c939dSCasey Schaufler LSM_DIR_OPS(smack);
29026d9c939dSCasey Schaufler #endif
29036d9c939dSCasey Schaufler
29046413f852SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR
29056413f852SJohn Johansen static const struct pid_entry apparmor_attr_dir_stuff[] = {
29066413f852SJohn Johansen ATTR("apparmor", "current", 0666),
29076413f852SJohn Johansen ATTR("apparmor", "prev", 0444),
29086413f852SJohn Johansen ATTR("apparmor", "exec", 0666),
29096413f852SJohn Johansen };
29106413f852SJohn Johansen LSM_DIR_OPS(apparmor);
29116413f852SJohn Johansen #endif
29126413f852SJohn Johansen
2913c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
29146d9c939dSCasey Schaufler ATTR(NULL, "current", 0666),
29156d9c939dSCasey Schaufler ATTR(NULL, "prev", 0444),
29166d9c939dSCasey Schaufler ATTR(NULL, "exec", 0666),
29176d9c939dSCasey Schaufler ATTR(NULL, "fscreate", 0666),
29186d9c939dSCasey Schaufler ATTR(NULL, "keycreate", 0666),
29196d9c939dSCasey Schaufler ATTR(NULL, "sockcreate", 0666),
29206d9c939dSCasey Schaufler #ifdef CONFIG_SECURITY_SMACK
29216d9c939dSCasey Schaufler DIR("smack", 0555,
29226d9c939dSCasey Schaufler proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
29236d9c939dSCasey Schaufler #endif
29246413f852SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR
29256413f852SJohn Johansen DIR("apparmor", 0555,
29266413f852SJohn Johansen proc_apparmor_attr_dir_inode_ops, proc_apparmor_attr_dir_ops),
29276413f852SJohn Johansen #endif
292828a6d671SEric W. Biederman };
292928a6d671SEric W. Biederman
proc_attr_dir_readdir(struct file * file,struct dir_context * ctx)2930f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
29311da177e4SLinus Torvalds {
2932f0c3b509SAl Viro return proc_pident_readdir(file, ctx,
293372d9dcfcSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
29341da177e4SLinus Torvalds }
29351da177e4SLinus Torvalds
293600977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
29371da177e4SLinus Torvalds .read = generic_read_dir,
2938f50752eaSAl Viro .iterate_shared = proc_attr_dir_readdir,
2939f50752eaSAl Viro .llseek = generic_file_llseek,
29401da177e4SLinus Torvalds };
29411da177e4SLinus Torvalds
proc_attr_dir_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)294272d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
294300cd8dd3SAl Viro struct dentry *dentry, unsigned int flags)
29441da177e4SLinus Torvalds {
29457bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry,
2946d5a572a4SAlexey Dobriyan attr_dir_stuff,
2947d5a572a4SAlexey Dobriyan attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
29481da177e4SLinus Torvalds }
29491da177e4SLinus Torvalds
2950c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
295172d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup,
295299f89551SEric W. Biederman .getattr = pid_getattr,
29536d76fa58SLinus Torvalds .setattr = proc_setattr,
29541da177e4SLinus Torvalds };
29551da177e4SLinus Torvalds
29561da177e4SLinus Torvalds #endif
29571da177e4SLinus Torvalds
2958698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
proc_coredump_filter_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)29593cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
29603cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos)
29613cb4a0bbSKawai, Hidehiro {
2962496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file));
29633cb4a0bbSKawai, Hidehiro struct mm_struct *mm;
29643cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF];
29653cb4a0bbSKawai, Hidehiro size_t len;
29663cb4a0bbSKawai, Hidehiro int ret;
29673cb4a0bbSKawai, Hidehiro
29683cb4a0bbSKawai, Hidehiro if (!task)
29693cb4a0bbSKawai, Hidehiro return -ESRCH;
29703cb4a0bbSKawai, Hidehiro
29713cb4a0bbSKawai, Hidehiro ret = 0;
29723cb4a0bbSKawai, Hidehiro mm = get_task_mm(task);
29733cb4a0bbSKawai, Hidehiro if (mm) {
29743cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n",
29753cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >>
29763cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT));
29773cb4a0bbSKawai, Hidehiro mmput(mm);
29783cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
29793cb4a0bbSKawai, Hidehiro }
29803cb4a0bbSKawai, Hidehiro
29813cb4a0bbSKawai, Hidehiro put_task_struct(task);
29823cb4a0bbSKawai, Hidehiro
29833cb4a0bbSKawai, Hidehiro return ret;
29843cb4a0bbSKawai, Hidehiro }
29853cb4a0bbSKawai, Hidehiro
proc_coredump_filter_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)29863cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
29873cb4a0bbSKawai, Hidehiro const char __user *buf,
29883cb4a0bbSKawai, Hidehiro size_t count,
29893cb4a0bbSKawai, Hidehiro loff_t *ppos)
29903cb4a0bbSKawai, Hidehiro {
29913cb4a0bbSKawai, Hidehiro struct task_struct *task;
29923cb4a0bbSKawai, Hidehiro struct mm_struct *mm;
29933cb4a0bbSKawai, Hidehiro unsigned int val;
29943cb4a0bbSKawai, Hidehiro int ret;
29953cb4a0bbSKawai, Hidehiro int i;
29963cb4a0bbSKawai, Hidehiro unsigned long mask;
29973cb4a0bbSKawai, Hidehiro
2998774636e1SAlexey Dobriyan ret = kstrtouint_from_user(buf, count, 0, &val);
2999774636e1SAlexey Dobriyan if (ret < 0)
3000774636e1SAlexey Dobriyan return ret;
30013cb4a0bbSKawai, Hidehiro
30023cb4a0bbSKawai, Hidehiro ret = -ESRCH;
3003496ad9aaSAl Viro task = get_proc_task(file_inode(file));
30043cb4a0bbSKawai, Hidehiro if (!task)
30053cb4a0bbSKawai, Hidehiro goto out_no_task;
30063cb4a0bbSKawai, Hidehiro
30073cb4a0bbSKawai, Hidehiro mm = get_task_mm(task);
30083cb4a0bbSKawai, Hidehiro if (!mm)
30093cb4a0bbSKawai, Hidehiro goto out_no_mm;
301041a0c249SColin Ian King ret = 0;
30113cb4a0bbSKawai, Hidehiro
30123cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
30133cb4a0bbSKawai, Hidehiro if (val & mask)
30143cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
30153cb4a0bbSKawai, Hidehiro else
30163cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
30173cb4a0bbSKawai, Hidehiro }
30183cb4a0bbSKawai, Hidehiro
30193cb4a0bbSKawai, Hidehiro mmput(mm);
30203cb4a0bbSKawai, Hidehiro out_no_mm:
30213cb4a0bbSKawai, Hidehiro put_task_struct(task);
30223cb4a0bbSKawai, Hidehiro out_no_task:
3023774636e1SAlexey Dobriyan if (ret < 0)
30243cb4a0bbSKawai, Hidehiro return ret;
3025774636e1SAlexey Dobriyan return count;
30263cb4a0bbSKawai, Hidehiro }
30273cb4a0bbSKawai, Hidehiro
30283cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
30293cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read,
30303cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write,
303187df8424SArnd Bergmann .llseek = generic_file_llseek,
30323cb4a0bbSKawai, Hidehiro };
30333cb4a0bbSKawai, Hidehiro #endif
30343cb4a0bbSKawai, Hidehiro
3035aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
do_io_accounting(struct task_struct * task,struct seq_file * m,int whole)303619aadc98SAlexey Dobriyan static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
3037aba76fdbSAndrew Morton {
3038940389b8SAndrea Righi struct task_io_accounting acct = task->ioac;
3039297c5d92SAndrea Righi unsigned long flags;
3040293eb1e7SVasiliy Kulikov int result;
3041297c5d92SAndrea Righi
3042f7cfd871SEric W. Biederman result = down_read_killable(&task->signal->exec_update_lock);
3043293eb1e7SVasiliy Kulikov if (result)
3044293eb1e7SVasiliy Kulikov return result;
3045293eb1e7SVasiliy Kulikov
3046caaee623SJann Horn if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
3047293eb1e7SVasiliy Kulikov result = -EACCES;
3048293eb1e7SVasiliy Kulikov goto out_unlock;
3049293eb1e7SVasiliy Kulikov }
30501d1221f3SVasiliy Kulikov
30515995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) {
3052b2d002dbSAndrea Righi struct task_struct *t = task;
3053297c5d92SAndrea Righi
30545995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac);
30555995477aSAndrea Righi while_each_thread(task, t)
30565995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac);
3057297c5d92SAndrea Righi
3058297c5d92SAndrea Righi unlock_task_sighand(task, &flags);
3059297c5d92SAndrea Righi }
306025ce3191SJoe Perches seq_printf(m,
3061aba76fdbSAndrew Morton "rchar: %llu\n"
3062aba76fdbSAndrew Morton "wchar: %llu\n"
3063aba76fdbSAndrew Morton "syscr: %llu\n"
3064aba76fdbSAndrew Morton "syscw: %llu\n"
3065aba76fdbSAndrew Morton "read_bytes: %llu\n"
3066aba76fdbSAndrew Morton "write_bytes: %llu\n"
3067aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n",
30687c44319dSAlexander Beregalov (unsigned long long)acct.rchar,
30697c44319dSAlexander Beregalov (unsigned long long)acct.wchar,
30707c44319dSAlexander Beregalov (unsigned long long)acct.syscr,
30717c44319dSAlexander Beregalov (unsigned long long)acct.syscw,
30727c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes,
30737c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes,
30747c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes);
307525ce3191SJoe Perches result = 0;
307625ce3191SJoe Perches
3077293eb1e7SVasiliy Kulikov out_unlock:
3078f7cfd871SEric W. Biederman up_read(&task->signal->exec_update_lock);
3079293eb1e7SVasiliy Kulikov return result;
3080aba76fdbSAndrew Morton }
3081297c5d92SAndrea Righi
proc_tid_io_accounting(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)308219aadc98SAlexey Dobriyan static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
308319aadc98SAlexey Dobriyan struct pid *pid, struct task_struct *task)
3084297c5d92SAndrea Righi {
308519aadc98SAlexey Dobriyan return do_io_accounting(task, m, 0);
3086297c5d92SAndrea Righi }
3087297c5d92SAndrea Righi
proc_tgid_io_accounting(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)308819aadc98SAlexey Dobriyan static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
308919aadc98SAlexey Dobriyan struct pid *pid, struct task_struct *task)
3090297c5d92SAndrea Righi {
309119aadc98SAlexey Dobriyan return do_io_accounting(task, m, 1);
3092297c5d92SAndrea Righi }
3093297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
3094aba76fdbSAndrew Morton
309522d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
proc_id_map_open(struct inode * inode,struct file * file,const struct seq_operations * seq_ops)309622d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
3097ccf94f1bSFabian Frederick const struct seq_operations *seq_ops)
309822d917d8SEric W. Biederman {
309922d917d8SEric W. Biederman struct user_namespace *ns = NULL;
310022d917d8SEric W. Biederman struct task_struct *task;
310122d917d8SEric W. Biederman struct seq_file *seq;
310222d917d8SEric W. Biederman int ret = -EINVAL;
310322d917d8SEric W. Biederman
310422d917d8SEric W. Biederman task = get_proc_task(inode);
310522d917d8SEric W. Biederman if (task) {
310622d917d8SEric W. Biederman rcu_read_lock();
310722d917d8SEric W. Biederman ns = get_user_ns(task_cred_xxx(task, user_ns));
310822d917d8SEric W. Biederman rcu_read_unlock();
310922d917d8SEric W. Biederman put_task_struct(task);
311022d917d8SEric W. Biederman }
311122d917d8SEric W. Biederman if (!ns)
311222d917d8SEric W. Biederman goto err;
311322d917d8SEric W. Biederman
311422d917d8SEric W. Biederman ret = seq_open(file, seq_ops);
311522d917d8SEric W. Biederman if (ret)
311622d917d8SEric W. Biederman goto err_put_ns;
311722d917d8SEric W. Biederman
311822d917d8SEric W. Biederman seq = file->private_data;
311922d917d8SEric W. Biederman seq->private = ns;
312022d917d8SEric W. Biederman
312122d917d8SEric W. Biederman return 0;
312222d917d8SEric W. Biederman err_put_ns:
312322d917d8SEric W. Biederman put_user_ns(ns);
312422d917d8SEric W. Biederman err:
312522d917d8SEric W. Biederman return ret;
312622d917d8SEric W. Biederman }
312722d917d8SEric W. Biederman
proc_id_map_release(struct inode * inode,struct file * file)312822d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
312922d917d8SEric W. Biederman {
313022d917d8SEric W. Biederman struct seq_file *seq = file->private_data;
313122d917d8SEric W. Biederman struct user_namespace *ns = seq->private;
313222d917d8SEric W. Biederman put_user_ns(ns);
313322d917d8SEric W. Biederman return seq_release(inode, file);
313422d917d8SEric W. Biederman }
313522d917d8SEric W. Biederman
proc_uid_map_open(struct inode * inode,struct file * file)313622d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
313722d917d8SEric W. Biederman {
313822d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_uid_seq_operations);
313922d917d8SEric W. Biederman }
314022d917d8SEric W. Biederman
proc_gid_map_open(struct inode * inode,struct file * file)314122d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
314222d917d8SEric W. Biederman {
314322d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_gid_seq_operations);
314422d917d8SEric W. Biederman }
314522d917d8SEric W. Biederman
proc_projid_map_open(struct inode * inode,struct file * file)3146f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file)
3147f76d207aSEric W. Biederman {
3148f76d207aSEric W. Biederman return proc_id_map_open(inode, file, &proc_projid_seq_operations);
3149f76d207aSEric W. Biederman }
3150f76d207aSEric W. Biederman
315122d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
315222d917d8SEric W. Biederman .open = proc_uid_map_open,
315322d917d8SEric W. Biederman .write = proc_uid_map_write,
315422d917d8SEric W. Biederman .read = seq_read,
315522d917d8SEric W. Biederman .llseek = seq_lseek,
315622d917d8SEric W. Biederman .release = proc_id_map_release,
315722d917d8SEric W. Biederman };
315822d917d8SEric W. Biederman
315922d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
316022d917d8SEric W. Biederman .open = proc_gid_map_open,
316122d917d8SEric W. Biederman .write = proc_gid_map_write,
316222d917d8SEric W. Biederman .read = seq_read,
316322d917d8SEric W. Biederman .llseek = seq_lseek,
316422d917d8SEric W. Biederman .release = proc_id_map_release,
316522d917d8SEric W. Biederman };
3166f76d207aSEric W. Biederman
3167f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = {
3168f76d207aSEric W. Biederman .open = proc_projid_map_open,
3169f76d207aSEric W. Biederman .write = proc_projid_map_write,
3170f76d207aSEric W. Biederman .read = seq_read,
3171f76d207aSEric W. Biederman .llseek = seq_lseek,
3172f76d207aSEric W. Biederman .release = proc_id_map_release,
3173f76d207aSEric W. Biederman };
31749cc46516SEric W. Biederman
proc_setgroups_open(struct inode * inode,struct file * file)31759cc46516SEric W. Biederman static int proc_setgroups_open(struct inode *inode, struct file *file)
31769cc46516SEric W. Biederman {
31779cc46516SEric W. Biederman struct user_namespace *ns = NULL;
31789cc46516SEric W. Biederman struct task_struct *task;
31799cc46516SEric W. Biederman int ret;
31809cc46516SEric W. Biederman
31819cc46516SEric W. Biederman ret = -ESRCH;
31829cc46516SEric W. Biederman task = get_proc_task(inode);
31839cc46516SEric W. Biederman if (task) {
31849cc46516SEric W. Biederman rcu_read_lock();
31859cc46516SEric W. Biederman ns = get_user_ns(task_cred_xxx(task, user_ns));
31869cc46516SEric W. Biederman rcu_read_unlock();
31879cc46516SEric W. Biederman put_task_struct(task);
31889cc46516SEric W. Biederman }
31899cc46516SEric W. Biederman if (!ns)
31909cc46516SEric W. Biederman goto err;
31919cc46516SEric W. Biederman
31929cc46516SEric W. Biederman if (file->f_mode & FMODE_WRITE) {
31939cc46516SEric W. Biederman ret = -EACCES;
31949cc46516SEric W. Biederman if (!ns_capable(ns, CAP_SYS_ADMIN))
31959cc46516SEric W. Biederman goto err_put_ns;
31969cc46516SEric W. Biederman }
31979cc46516SEric W. Biederman
31989cc46516SEric W. Biederman ret = single_open(file, &proc_setgroups_show, ns);
31999cc46516SEric W. Biederman if (ret)
32009cc46516SEric W. Biederman goto err_put_ns;
32019cc46516SEric W. Biederman
32029cc46516SEric W. Biederman return 0;
32039cc46516SEric W. Biederman err_put_ns:
32049cc46516SEric W. Biederman put_user_ns(ns);
32059cc46516SEric W. Biederman err:
32069cc46516SEric W. Biederman return ret;
32079cc46516SEric W. Biederman }
32089cc46516SEric W. Biederman
proc_setgroups_release(struct inode * inode,struct file * file)32099cc46516SEric W. Biederman static int proc_setgroups_release(struct inode *inode, struct file *file)
32109cc46516SEric W. Biederman {
32119cc46516SEric W. Biederman struct seq_file *seq = file->private_data;
32129cc46516SEric W. Biederman struct user_namespace *ns = seq->private;
32139cc46516SEric W. Biederman int ret = single_release(inode, file);
32149cc46516SEric W. Biederman put_user_ns(ns);
32159cc46516SEric W. Biederman return ret;
32169cc46516SEric W. Biederman }
32179cc46516SEric W. Biederman
32189cc46516SEric W. Biederman static const struct file_operations proc_setgroups_operations = {
32199cc46516SEric W. Biederman .open = proc_setgroups_open,
32209cc46516SEric W. Biederman .write = proc_setgroups_write,
32219cc46516SEric W. Biederman .read = seq_read,
32229cc46516SEric W. Biederman .llseek = seq_lseek,
32239cc46516SEric W. Biederman .release = proc_setgroups_release,
32249cc46516SEric W. Biederman };
322522d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
322622d917d8SEric W. Biederman
proc_pid_personality(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)322747830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
322847830723SKees Cook struct pid *pid, struct task_struct *task)
322947830723SKees Cook {
3230a9712bc1SAl Viro int err = lock_trace(task);
3231a9712bc1SAl Viro if (!err) {
323247830723SKees Cook seq_printf(m, "%08x\n", task->personality);
3233a9712bc1SAl Viro unlock_trace(task);
3234a9712bc1SAl Viro }
3235a9712bc1SAl Viro return err;
323647830723SKees Cook }
323747830723SKees Cook
32387c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
proc_pid_patch_state(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)32397c23b330SJosh Poimboeuf static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
32407c23b330SJosh Poimboeuf struct pid *pid, struct task_struct *task)
32417c23b330SJosh Poimboeuf {
32427c23b330SJosh Poimboeuf seq_printf(m, "%d\n", task->patch_state);
32437c23b330SJosh Poimboeuf return 0;
32447c23b330SJosh Poimboeuf }
32457c23b330SJosh Poimboeuf #endif /* CONFIG_LIVEPATCH */
32467c23b330SJosh Poimboeuf
324776093853Sxu xin #ifdef CONFIG_KSM
proc_pid_ksm_merging_pages(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)324876093853Sxu xin static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace *ns,
324976093853Sxu xin struct pid *pid, struct task_struct *task)
325076093853Sxu xin {
325176093853Sxu xin struct mm_struct *mm;
325276093853Sxu xin
325376093853Sxu xin mm = get_task_mm(task);
325476093853Sxu xin if (mm) {
325576093853Sxu xin seq_printf(m, "%lu\n", mm->ksm_merging_pages);
325676093853Sxu xin mmput(mm);
325776093853Sxu xin }
325876093853Sxu xin
325976093853Sxu xin return 0;
326076093853Sxu xin }
proc_pid_ksm_stat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)3261cb4df4caSxu xin static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
3262cb4df4caSxu xin struct pid *pid, struct task_struct *task)
3263cb4df4caSxu xin {
3264cb4df4caSxu xin struct mm_struct *mm;
3265cb4df4caSxu xin
3266cb4df4caSxu xin mm = get_task_mm(task);
3267cb4df4caSxu xin if (mm) {
3268cb4df4caSxu xin seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
326999ed145fSChengming Zhou seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(mm));
3270d21077fbSStefan Roesch seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
3271d21077fbSStefan Roesch seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
3272cb4df4caSxu xin mmput(mm);
3273cb4df4caSxu xin }
3274cb4df4caSxu xin
3275cb4df4caSxu xin return 0;
3276cb4df4caSxu xin }
327776093853Sxu xin #endif /* CONFIG_KSM */
327876093853Sxu xin
3279c8d12627SAlexander Popov #ifdef CONFIG_STACKLEAK_METRICS
proc_stack_depth(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)3280c8d12627SAlexander Popov static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
3281c8d12627SAlexander Popov struct pid *pid, struct task_struct *task)
3282c8d12627SAlexander Popov {
3283c8d12627SAlexander Popov unsigned long prev_depth = THREAD_SIZE -
3284c8d12627SAlexander Popov (task->prev_lowest_stack & (THREAD_SIZE - 1));
3285c8d12627SAlexander Popov unsigned long depth = THREAD_SIZE -
3286c8d12627SAlexander Popov (task->lowest_stack & (THREAD_SIZE - 1));
3287c8d12627SAlexander Popov
3288c8d12627SAlexander Popov seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
3289c8d12627SAlexander Popov prev_depth, depth);
3290c8d12627SAlexander Popov return 0;
3291c8d12627SAlexander Popov }
3292c8d12627SAlexander Popov #endif /* CONFIG_STACKLEAK_METRICS */
3293c8d12627SAlexander Popov
3294801199ceSEric W. Biederman /*
329528a6d671SEric W. Biederman * Thread groups
329628a6d671SEric W. Biederman */
329700977a59SArjan van de Ven static const struct file_operations proc_task_operations;
3298c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
329920cdc894SEric W. Biederman
3300c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
3301631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3302631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3303640708a2SPavel Emelyanov DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
33047bc3fa01SKalesh Singh DIR("fdinfo", S_IRUGO|S_IXUGO, proc_fdinfo_inode_operations, proc_fdinfo_operations),
33056b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3306b2211a36SAndrew Morton #ifdef CONFIG_NET
3307631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3308b2211a36SAndrew Morton #endif
3309631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations),
3310c5317167SAl Viro REG("auxv", S_IRUSR, proc_auxv_operations),
3311631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status),
331235a35046SDjalal Harouni ONE("personality", S_IRUSR, proc_pid_personality),
33131c963eb1SAlexey Dobriyan ONE("limits", S_IRUGO, proc_pid_limits),
331443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3315631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
331643ae34cbSIngo Molnar #endif
33175091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
33185091faa4SMike Galbraith REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
33195091faa4SMike Galbraith #endif
332004a8682aSAndrei Vagin #ifdef CONFIG_TIME_NS
332104a8682aSAndrei Vagin REG("timens_offsets", S_IRUGO|S_IWUSR, proc_timens_offsets_operations),
332204a8682aSAndrei Vagin #endif
33234614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3324ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
332509d93bd6SAlexey Dobriyan ONE("syscall", S_IRUSR, proc_pid_syscall),
3326ebcb6734SRoland McGrath #endif
3327c2c0bb44SAlexey Dobriyan REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3328631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat),
3329631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm),
3330b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_pid_maps_operations),
333128a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3332b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
333328a6d671SEric W. Biederman #endif
3334631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3335631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link),
3336631f9c18SAlexey Dobriyan LNK("root", proc_root_link),
3337631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link),
3338631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations),
3339631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3340631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations),
33411e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3342631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3343b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3344493b0e9dSDaniel Colascione REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
334532ed74a4SDjalal Harouni REG("pagemap", S_IRUSR, proc_pagemap_operations),
334628a6d671SEric W. Biederman #endif
334728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3348631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
334928a6d671SEric W. Biederman #endif
335028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3351edfcd606SAlexey Dobriyan ONE("wchan", S_IRUGO, proc_pid_wchan),
335228a6d671SEric W. Biederman #endif
33532ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
335435a35046SDjalal Harouni ONE("stack", S_IRUSR, proc_pid_stack),
335528a6d671SEric W. Biederman #endif
33565968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
3357f6e826caSAlexey Dobriyan ONE("schedstat", S_IRUGO, proc_pid_schedstat),
335828a6d671SEric W. Biederman #endif
33599745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3360631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations),
33619745512cSArjan van de Ven #endif
33628793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
336352de4779SZefan Li ONE("cpuset", S_IRUGO, proc_cpuset_show),
336428a6d671SEric W. Biederman #endif
3365a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3366006f4ac4SZefan Li ONE("cgroup", S_IRUGO, proc_cgroup_show),
3367a424316cSPaul Menage #endif
3368e79f15a4SChen Yu #ifdef CONFIG_PROC_CPU_RESCTRL
3369e79f15a4SChen Yu ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3370e79f15a4SChen Yu #endif
33716ba51e37SAlexey Dobriyan ONE("oom_score", S_IRUGO, proc_oom_score),
3372fa0cbbf1SDavid Rientjes REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3373a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
33744b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
3375631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3376631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations),
337728a6d671SEric W. Biederman #endif
3378f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3379631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3380168c42bcSAkinobu Mita REG("fail-nth", 0644, proc_fail_nth_operations),
3381f4f154fdSAkinobu Mita #endif
3382698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
3383631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
33843cb4a0bbSKawai, Hidehiro #endif
3385aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
338619aadc98SAlexey Dobriyan ONE("io", S_IRUSR, proc_tgid_io_accounting),
3387aba76fdbSAndrew Morton #endif
338822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
338922d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
339022d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3391f76d207aSEric W. Biederman REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
33929cc46516SEric W. Biederman REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
339322d917d8SEric W. Biederman #endif
3394b18b6a9cSNicolas Pitre #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
339548f6a7a5SPavel Emelyanov REG("timers", S_IRUGO, proc_timers_operations),
339648f6a7a5SPavel Emelyanov #endif
33975de23d43SJohn Stultz REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
33987c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
33997c23b330SJosh Poimboeuf ONE("patch_state", S_IRUSR, proc_pid_patch_state),
34007c23b330SJosh Poimboeuf #endif
3401c8d12627SAlexander Popov #ifdef CONFIG_STACKLEAK_METRICS
3402c8d12627SAlexander Popov ONE("stack_depth", S_IRUGO, proc_stack_depth),
3403c8d12627SAlexander Popov #endif
340468bc30bbSAubrey Li #ifdef CONFIG_PROC_PID_ARCH_STATUS
340568bc30bbSAubrey Li ONE("arch_status", S_IRUGO, proc_pid_arch_status),
340668bc30bbSAubrey Li #endif
34070d8315ddSYiFei Zhu #ifdef CONFIG_SECCOMP_CACHE_DEBUG
34080d8315ddSYiFei Zhu ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
34090d8315ddSYiFei Zhu #endif
341076093853Sxu xin #ifdef CONFIG_KSM
341176093853Sxu xin ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages),
3412cb4df4caSxu xin ONE("ksm_stat", S_IRUSR, proc_pid_ksm_stat),
341376093853Sxu xin #endif
341428a6d671SEric W. Biederman };
341528a6d671SEric W. Biederman
proc_tgid_base_readdir(struct file * file,struct dir_context * ctx)3416f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
341728a6d671SEric W. Biederman {
3418f0c3b509SAl Viro return proc_pident_readdir(file, ctx,
341928a6d671SEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
342028a6d671SEric W. Biederman }
342128a6d671SEric W. Biederman
342200977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
342328a6d671SEric W. Biederman .read = generic_read_dir,
3424f50752eaSAl Viro .iterate_shared = proc_tgid_base_readdir,
3425f50752eaSAl Viro .llseek = generic_file_llseek,
342628a6d671SEric W. Biederman };
342728a6d671SEric W. Biederman
tgid_pidfd_to_pid(const struct file * file)34283eb39f47SChristian Brauner struct pid *tgid_pidfd_to_pid(const struct file *file)
34293eb39f47SChristian Brauner {
343030d158b1SChristian Brauner if (file->f_op != &proc_tgid_base_operations)
34313eb39f47SChristian Brauner return ERR_PTR(-EBADF);
34323eb39f47SChristian Brauner
34333eb39f47SChristian Brauner return proc_pid(file_inode(file));
34343eb39f47SChristian Brauner }
34353eb39f47SChristian Brauner
proc_tgid_base_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)343600cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
343700cd8dd3SAl Viro {
34387bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry,
3439d5a572a4SAlexey Dobriyan tgid_base_stuff,
3440d5a572a4SAlexey Dobriyan tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
344128a6d671SEric W. Biederman }
344228a6d671SEric W. Biederman
3443c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
344428a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup,
344528a6d671SEric W. Biederman .getattr = pid_getattr,
344628a6d671SEric W. Biederman .setattr = proc_setattr,
34470499680aSVasiliy Kulikov .permission = proc_pid_permission,
344828a6d671SEric W. Biederman };
344928a6d671SEric W. Biederman
34500895e91dSRandy Dunlap /**
34517bc3e6e5SEric W. Biederman * proc_flush_pid - Remove dcache entries for @pid from the /proc dcache.
34527bc3e6e5SEric W. Biederman * @pid: pid that should be flushed.
34530895e91dSRandy Dunlap *
34547bc3e6e5SEric W. Biederman * This function walks a list of inodes (that belong to any proc
34557bc3e6e5SEric W. Biederman * filesystem) that are attached to the pid and flushes them from
34567bc3e6e5SEric W. Biederman * the dentry cache.
34570895e91dSRandy Dunlap *
34580895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until
34590895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with
34600895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be
34617bc3e6e5SEric W. Biederman * flushed instead. This routine is provided to flush those useless
34627bc3e6e5SEric W. Biederman * dcache entries when a process is reaped.
34630895e91dSRandy Dunlap *
34640895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee
34657bc3e6e5SEric W. Biederman * that no dcache entries will exist after a process is reaped
34667bc3e6e5SEric W. Biederman * it just makes it very unlikely that any will persist.
346760347f67SPavel Emelyanov */
346860347f67SPavel Emelyanov
proc_flush_pid(struct pid * pid)34697bc3e6e5SEric W. Biederman void proc_flush_pid(struct pid *pid)
347060347f67SPavel Emelyanov {
347163f818f4SEric W. Biederman proc_invalidate_siblings_dcache(&pid->inodes, &pid->lock);
347260347f67SPavel Emelyanov }
347360347f67SPavel Emelyanov
proc_pid_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)34740168b9e3SAl Viro static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3475c5141e6dSEric Dumazet struct task_struct *task, const void *ptr)
3476444ceed8SEric W. Biederman {
3477444ceed8SEric W. Biederman struct inode *inode;
3478444ceed8SEric W. Biederman
3479d919a1e7SZhihao Cheng inode = proc_pid_make_base_inode(dentry->d_sb, task,
3480d919a1e7SZhihao Cheng S_IFDIR | S_IRUGO | S_IXUGO);
3481444ceed8SEric W. Biederman if (!inode)
34820168b9e3SAl Viro return ERR_PTR(-ENOENT);
3483444ceed8SEric W. Biederman
3484444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations;
3485444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations;
3486444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE;
3487aed54175SVegard Nossum
34881270dd8dSAlexey Dobriyan set_nlink(inode, nlink_tgid);
34891bbc5513SAl Viro pid_update_inode(task, inode);
3490444ceed8SEric W. Biederman
3491fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations);
34920168b9e3SAl Viro return d_splice_alias(inode, dentry);
3493444ceed8SEric W. Biederman }
3494444ceed8SEric W. Biederman
proc_pid_lookup(struct dentry * dentry,unsigned int flags)3495867aaccfSZhikang Zhang struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
34961da177e4SLinus Torvalds {
34971da177e4SLinus Torvalds struct task_struct *task;
34981da177e4SLinus Torvalds unsigned tgid;
3499fa10fed3SAlexey Gladkov struct proc_fs_info *fs_info;
3500b488893aSPavel Emelyanov struct pid_namespace *ns;
35010168b9e3SAl Viro struct dentry *result = ERR_PTR(-ENOENT);
35021da177e4SLinus Torvalds
3503dbcdb504SAlexey Dobriyan tgid = name_to_int(&dentry->d_name);
35041da177e4SLinus Torvalds if (tgid == ~0U)
35051da177e4SLinus Torvalds goto out;
35061da177e4SLinus Torvalds
3507fa10fed3SAlexey Gladkov fs_info = proc_sb_info(dentry->d_sb);
3508fa10fed3SAlexey Gladkov ns = fs_info->pid_ns;
3509de758734SEric W. Biederman rcu_read_lock();
3510b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns);
35111da177e4SLinus Torvalds if (task)
35121da177e4SLinus Torvalds get_task_struct(task);
3513de758734SEric W. Biederman rcu_read_unlock();
35141da177e4SLinus Torvalds if (!task)
35151da177e4SLinus Torvalds goto out;
35161da177e4SLinus Torvalds
351724a71ce5SAlexey Gladkov /* Limit procfs to only ptraceable tasks */
351824a71ce5SAlexey Gladkov if (fs_info->hide_pid == HIDEPID_NOT_PTRACEABLE) {
351924a71ce5SAlexey Gladkov if (!has_pid_permissions(fs_info, task, HIDEPID_NO_ACCESS))
352024a71ce5SAlexey Gladkov goto out_put_task;
352124a71ce5SAlexey Gladkov }
352224a71ce5SAlexey Gladkov
35230168b9e3SAl Viro result = proc_pid_instantiate(dentry, task, NULL);
352424a71ce5SAlexey Gladkov out_put_task:
352548e6484dSEric W. Biederman put_task_struct(task);
35261da177e4SLinus Torvalds out:
35270168b9e3SAl Viro return result;
35281da177e4SLinus Torvalds }
35291da177e4SLinus Torvalds
35301da177e4SLinus Torvalds /*
35310804ef4bSEric W. Biederman * Find the first task with tgid >= tgid
35320bc58a91SEric W. Biederman *
35331da177e4SLinus Torvalds */
353419fd4bb2SEric W. Biederman struct tgid_iter {
353519fd4bb2SEric W. Biederman unsigned int tgid;
35360804ef4bSEric W. Biederman struct task_struct *task;
353719fd4bb2SEric W. Biederman };
next_tgid(struct pid_namespace * ns,struct tgid_iter iter)353819fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
353919fd4bb2SEric W. Biederman {
35400804ef4bSEric W. Biederman struct pid *pid;
35411da177e4SLinus Torvalds
354219fd4bb2SEric W. Biederman if (iter.task)
354319fd4bb2SEric W. Biederman put_task_struct(iter.task);
35440804ef4bSEric W. Biederman rcu_read_lock();
35450804ef4bSEric W. Biederman retry:
354619fd4bb2SEric W. Biederman iter.task = NULL;
354719fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns);
35480804ef4bSEric W. Biederman if (pid) {
354919fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns);
35503147d8aaSEric W. Biederman iter.task = pid_task(pid, PIDTYPE_TGID);
35513147d8aaSEric W. Biederman if (!iter.task) {
355219fd4bb2SEric W. Biederman iter.tgid += 1;
35530804ef4bSEric W. Biederman goto retry;
355419fd4bb2SEric W. Biederman }
355519fd4bb2SEric W. Biederman get_task_struct(iter.task);
35561da177e4SLinus Torvalds }
3557454cc105SEric W. Biederman rcu_read_unlock();
355819fd4bb2SEric W. Biederman return iter;
35591da177e4SLinus Torvalds }
35601da177e4SLinus Torvalds
35610097875bSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
35621da177e4SLinus Torvalds
35631da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
proc_pid_readdir(struct file * file,struct dir_context * ctx)3564f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx)
35651da177e4SLinus Torvalds {
356619fd4bb2SEric W. Biederman struct tgid_iter iter;
3567fa10fed3SAlexey Gladkov struct proc_fs_info *fs_info = proc_sb_info(file_inode(file)->i_sb);
35689d78edeaSAlexey Gladkov struct pid_namespace *ns = proc_pid_ns(file_inode(file)->i_sb);
3569f0c3b509SAl Viro loff_t pos = ctx->pos;
35701da177e4SLinus Torvalds
3571021ada7dSAl Viro if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3572f0c3b509SAl Viro return 0;
35731da177e4SLinus Torvalds
35740097875bSEric W. Biederman if (pos == TGID_OFFSET - 2) {
3575fa10fed3SAlexey Gladkov struct inode *inode = d_inode(fs_info->proc_self);
3576db963164SAl Viro if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3577f0c3b509SAl Viro return 0;
35780097875bSEric W. Biederman ctx->pos = pos = pos + 1;
3579021ada7dSAl Viro }
35800097875bSEric W. Biederman if (pos == TGID_OFFSET - 1) {
3581fa10fed3SAlexey Gladkov struct inode *inode = d_inode(fs_info->proc_thread_self);
35820097875bSEric W. Biederman if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
35830097875bSEric W. Biederman return 0;
35840097875bSEric W. Biederman ctx->pos = pos = pos + 1;
35850097875bSEric W. Biederman }
35860097875bSEric W. Biederman iter.tgid = pos - TGID_OFFSET;
358719fd4bb2SEric W. Biederman iter.task = NULL;
358819fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter);
358919fd4bb2SEric W. Biederman iter.task;
359019fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) {
3591e3912ac3SAlexey Dobriyan char name[10 + 1];
3592a4ef3895SAlexey Dobriyan unsigned int len;
35933ba4bceeSEric Dumazet
35943ba4bceeSEric Dumazet cond_resched();
3595fa10fed3SAlexey Gladkov if (!has_pid_permissions(fs_info, iter.task, HIDEPID_INVISIBLE))
3596f0c3b509SAl Viro continue;
35970499680aSVasiliy Kulikov
3598e3912ac3SAlexey Dobriyan len = snprintf(name, sizeof(name), "%u", iter.tgid);
3599f0c3b509SAl Viro ctx->pos = iter.tgid + TGID_OFFSET;
3600f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, name, len,
3601f0c3b509SAl Viro proc_pid_instantiate, iter.task, NULL)) {
360219fd4bb2SEric W. Biederman put_task_struct(iter.task);
3603f0c3b509SAl Viro return 0;
36041da177e4SLinus Torvalds }
36051da177e4SLinus Torvalds }
3606f0c3b509SAl Viro ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
36071da177e4SLinus Torvalds return 0;
36081da177e4SLinus Torvalds }
36091da177e4SLinus Torvalds
36100bc58a91SEric W. Biederman /*
36111b3044e3SJanis Danisevskis * proc_tid_comm_permission is a special permission function exclusively
36121b3044e3SJanis Danisevskis * used for the node /proc/<pid>/task/<tid>/comm.
36131b3044e3SJanis Danisevskis * It bypasses generic permission checks in the case where a task of the same
36141b3044e3SJanis Danisevskis * task group attempts to access the node.
36151b3044e3SJanis Danisevskis * The rationale behind this is that glibc and bionic access this node for
36161b3044e3SJanis Danisevskis * cross thread naming (pthread_set/getname_np(!self)). However, if
36171b3044e3SJanis Danisevskis * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
36181b3044e3SJanis Danisevskis * which locks out the cross thread naming implementation.
36191b3044e3SJanis Danisevskis * This function makes sure that the node is always accessible for members of
36201b3044e3SJanis Danisevskis * same thread group.
36211b3044e3SJanis Danisevskis */
proc_tid_comm_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)36224609e1f1SChristian Brauner static int proc_tid_comm_permission(struct mnt_idmap *idmap,
3623549c7297SChristian Brauner struct inode *inode, int mask)
36241b3044e3SJanis Danisevskis {
36251b3044e3SJanis Danisevskis bool is_same_tgroup;
36261b3044e3SJanis Danisevskis struct task_struct *task;
36271b3044e3SJanis Danisevskis
36281b3044e3SJanis Danisevskis task = get_proc_task(inode);
36291b3044e3SJanis Danisevskis if (!task)
36301b3044e3SJanis Danisevskis return -ESRCH;
36311b3044e3SJanis Danisevskis is_same_tgroup = same_thread_group(current, task);
36321b3044e3SJanis Danisevskis put_task_struct(task);
36331b3044e3SJanis Danisevskis
36341b3044e3SJanis Danisevskis if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
36351b3044e3SJanis Danisevskis /* This file (/proc/<pid>/task/<tid>/comm) can always be
36361b3044e3SJanis Danisevskis * read or written by the members of the corresponding
36371b3044e3SJanis Danisevskis * thread group.
36381b3044e3SJanis Danisevskis */
36391b3044e3SJanis Danisevskis return 0;
36401b3044e3SJanis Danisevskis }
36411b3044e3SJanis Danisevskis
36424609e1f1SChristian Brauner return generic_permission(&nop_mnt_idmap, inode, mask);
36431b3044e3SJanis Danisevskis }
36441b3044e3SJanis Danisevskis
36451b3044e3SJanis Danisevskis static const struct inode_operations proc_tid_comm_inode_operations = {
3646ccf61486SAleksa Sarai .setattr = proc_setattr,
36471b3044e3SJanis Danisevskis .permission = proc_tid_comm_permission,
36481b3044e3SJanis Danisevskis };
36491b3044e3SJanis Danisevskis
36501b3044e3SJanis Danisevskis /*
365128a6d671SEric W. Biederman * Tasks
365228a6d671SEric W. Biederman */
3653c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3654631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
36557bc3fa01SKalesh Singh DIR("fdinfo", S_IRUGO|S_IXUGO, proc_fdinfo_inode_operations, proc_fdinfo_operations),
36566b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
36576ba8ed79SEric W. Biederman #ifdef CONFIG_NET
36586ba8ed79SEric W. Biederman DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
36596ba8ed79SEric W. Biederman #endif
3660631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations),
3661c5317167SAl Viro REG("auxv", S_IRUSR, proc_auxv_operations),
3662631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status),
366335a35046SDjalal Harouni ONE("personality", S_IRUSR, proc_pid_personality),
36641c963eb1SAlexey Dobriyan ONE("limits", S_IRUGO, proc_pid_limits),
366543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3666631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
366743ae34cbSIngo Molnar #endif
36681b3044e3SJanis Danisevskis NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
36691b3044e3SJanis Danisevskis &proc_tid_comm_inode_operations,
36701b3044e3SJanis Danisevskis &proc_pid_set_comm_operations, {}),
3671ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
367209d93bd6SAlexey Dobriyan ONE("syscall", S_IRUSR, proc_pid_syscall),
3673ebcb6734SRoland McGrath #endif
3674c2c0bb44SAlexey Dobriyan REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3675631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat),
3676631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm),
3677871305bbSVlastimil Babka REG("maps", S_IRUGO, proc_pid_maps_operations),
36782e13ba54SIago López Galeiras #ifdef CONFIG_PROC_CHILDREN
367981841161SCyrill Gorcunov REG("children", S_IRUGO, proc_tid_children_operations),
368081841161SCyrill Gorcunov #endif
368128a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3682871305bbSVlastimil Babka REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
368328a6d671SEric W. Biederman #endif
3684631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3685631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link),
3686631f9c18SAlexey Dobriyan LNK("root", proc_root_link),
3687631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link),
3688631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations),
3689631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
36901e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3691631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3692871305bbSVlastimil Babka REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3693493b0e9dSDaniel Colascione REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
369432ed74a4SDjalal Harouni REG("pagemap", S_IRUSR, proc_pagemap_operations),
369528a6d671SEric W. Biederman #endif
369628a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3697631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
369828a6d671SEric W. Biederman #endif
369928a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3700edfcd606SAlexey Dobriyan ONE("wchan", S_IRUGO, proc_pid_wchan),
370128a6d671SEric W. Biederman #endif
37022ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
370335a35046SDjalal Harouni ONE("stack", S_IRUSR, proc_pid_stack),
370428a6d671SEric W. Biederman #endif
37055968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO
3706f6e826caSAlexey Dobriyan ONE("schedstat", S_IRUGO, proc_pid_schedstat),
370728a6d671SEric W. Biederman #endif
37089745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3709631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations),
37109745512cSArjan van de Ven #endif
37118793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
371252de4779SZefan Li ONE("cpuset", S_IRUGO, proc_cpuset_show),
371328a6d671SEric W. Biederman #endif
3714a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3715006f4ac4SZefan Li ONE("cgroup", S_IRUGO, proc_cgroup_show),
3716a424316cSPaul Menage #endif
3717e79f15a4SChen Yu #ifdef CONFIG_PROC_CPU_RESCTRL
3718e79f15a4SChen Yu ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3719e79f15a4SChen Yu #endif
37206ba51e37SAlexey Dobriyan ONE("oom_score", S_IRUGO, proc_oom_score),
3721fa0cbbf1SDavid Rientjes REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3722a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
37234b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
3724631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
372526ec3c64SAl Viro REG("sessionid", S_IRUGO, proc_sessionid_operations),
372628a6d671SEric W. Biederman #endif
3727f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3728631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
37291203c8e6SAkinobu Mita REG("fail-nth", 0644, proc_fail_nth_operations),
3730f4f154fdSAkinobu Mita #endif
3731297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
373219aadc98SAlexey Dobriyan ONE("io", S_IRUSR, proc_tid_io_accounting),
3733297c5d92SAndrea Righi #endif
373422d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
373522d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
373622d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3737f76d207aSEric W. Biederman REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
37389cc46516SEric W. Biederman REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
373922d917d8SEric W. Biederman #endif
37407c23b330SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
37417c23b330SJosh Poimboeuf ONE("patch_state", S_IRUSR, proc_pid_patch_state),
37427c23b330SJosh Poimboeuf #endif
374368bc30bbSAubrey Li #ifdef CONFIG_PROC_PID_ARCH_STATUS
374468bc30bbSAubrey Li ONE("arch_status", S_IRUGO, proc_pid_arch_status),
374568bc30bbSAubrey Li #endif
37460d8315ddSYiFei Zhu #ifdef CONFIG_SECCOMP_CACHE_DEBUG
37470d8315ddSYiFei Zhu ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
37480d8315ddSYiFei Zhu #endif
374976093853Sxu xin #ifdef CONFIG_KSM
375076093853Sxu xin ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages),
3751cb4df4caSxu xin ONE("ksm_stat", S_IRUSR, proc_pid_ksm_stat),
375276093853Sxu xin #endif
375328a6d671SEric W. Biederman };
375428a6d671SEric W. Biederman
proc_tid_base_readdir(struct file * file,struct dir_context * ctx)3755f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
375628a6d671SEric W. Biederman {
3757f0c3b509SAl Viro return proc_pident_readdir(file, ctx,
375828a6d671SEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
375928a6d671SEric W. Biederman }
376028a6d671SEric W. Biederman
proc_tid_base_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)376100cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
376200cd8dd3SAl Viro {
37637bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry,
3764d5a572a4SAlexey Dobriyan tid_base_stuff,
3765d5a572a4SAlexey Dobriyan tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
376628a6d671SEric W. Biederman }
376728a6d671SEric W. Biederman
376800977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
376928a6d671SEric W. Biederman .read = generic_read_dir,
3770f50752eaSAl Viro .iterate_shared = proc_tid_base_readdir,
3771f50752eaSAl Viro .llseek = generic_file_llseek,
377228a6d671SEric W. Biederman };
377328a6d671SEric W. Biederman
3774c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
377528a6d671SEric W. Biederman .lookup = proc_tid_base_lookup,
377628a6d671SEric W. Biederman .getattr = pid_getattr,
377728a6d671SEric W. Biederman .setattr = proc_setattr,
377828a6d671SEric W. Biederman };
377928a6d671SEric W. Biederman
proc_task_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)37800168b9e3SAl Viro static struct dentry *proc_task_instantiate(struct dentry *dentry,
37810168b9e3SAl Viro struct task_struct *task, const void *ptr)
3782444ceed8SEric W. Biederman {
3783444ceed8SEric W. Biederman struct inode *inode;
3784d919a1e7SZhihao Cheng inode = proc_pid_make_base_inode(dentry->d_sb, task,
3785d919a1e7SZhihao Cheng S_IFDIR | S_IRUGO | S_IXUGO);
3786444ceed8SEric W. Biederman if (!inode)
37870168b9e3SAl Viro return ERR_PTR(-ENOENT);
37881bbc5513SAl Viro
3789444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations;
3790444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations;
3791444ceed8SEric W. Biederman inode->i_flags |= S_IMMUTABLE;
3792aed54175SVegard Nossum
37931270dd8dSAlexey Dobriyan set_nlink(inode, nlink_tid);
37941bbc5513SAl Viro pid_update_inode(task, inode);
3795444ceed8SEric W. Biederman
3796fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations);
37970168b9e3SAl Viro return d_splice_alias(inode, dentry);
3798444ceed8SEric W. Biederman }
3799444ceed8SEric W. Biederman
proc_task_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)380000cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
380128a6d671SEric W. Biederman {
380228a6d671SEric W. Biederman struct task_struct *task;
380328a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir);
380428a6d671SEric W. Biederman unsigned tid;
3805fa10fed3SAlexey Gladkov struct proc_fs_info *fs_info;
3806b488893aSPavel Emelyanov struct pid_namespace *ns;
38070168b9e3SAl Viro struct dentry *result = ERR_PTR(-ENOENT);
380828a6d671SEric W. Biederman
380928a6d671SEric W. Biederman if (!leader)
381028a6d671SEric W. Biederman goto out_no_task;
381128a6d671SEric W. Biederman
3812dbcdb504SAlexey Dobriyan tid = name_to_int(&dentry->d_name);
381328a6d671SEric W. Biederman if (tid == ~0U)
381428a6d671SEric W. Biederman goto out;
381528a6d671SEric W. Biederman
3816fa10fed3SAlexey Gladkov fs_info = proc_sb_info(dentry->d_sb);
3817fa10fed3SAlexey Gladkov ns = fs_info->pid_ns;
381828a6d671SEric W. Biederman rcu_read_lock();
3819b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns);
382028a6d671SEric W. Biederman if (task)
382128a6d671SEric W. Biederman get_task_struct(task);
382228a6d671SEric W. Biederman rcu_read_unlock();
382328a6d671SEric W. Biederman if (!task)
382428a6d671SEric W. Biederman goto out;
3825bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task))
382628a6d671SEric W. Biederman goto out_drop_task;
382728a6d671SEric W. Biederman
38280168b9e3SAl Viro result = proc_task_instantiate(dentry, task, NULL);
382928a6d671SEric W. Biederman out_drop_task:
383028a6d671SEric W. Biederman put_task_struct(task);
383128a6d671SEric W. Biederman out:
383228a6d671SEric W. Biederman put_task_struct(leader);
383328a6d671SEric W. Biederman out_no_task:
38340168b9e3SAl Viro return result;
383528a6d671SEric W. Biederman }
383628a6d671SEric W. Biederman
383728a6d671SEric W. Biederman /*
38380bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space.
38390bc58a91SEric W. Biederman *
38400bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users
38410bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the
38420bc58a91SEric W. Biederman * directory we have more work todo.
38430bc58a91SEric W. Biederman *
38440bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid.
38450bc58a91SEric W. Biederman *
38460bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr
38470bc58a91SEric W. Biederman * threads past it.
38480bc58a91SEric W. Biederman */
first_tid(struct pid * pid,int tid,loff_t f_pos,struct pid_namespace * ns)38499f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
38509f6e963fSOleg Nesterov struct pid_namespace *ns)
38510bc58a91SEric W. Biederman {
3852d855a4b7SOleg Nesterov struct task_struct *pos, *task;
38539f6e963fSOleg Nesterov unsigned long nr = f_pos;
38549f6e963fSOleg Nesterov
38559f6e963fSOleg Nesterov if (nr != f_pos) /* 32bit overflow? */
38569f6e963fSOleg Nesterov return NULL;
38570bc58a91SEric W. Biederman
3858cc288738SEric W. Biederman rcu_read_lock();
3859d855a4b7SOleg Nesterov task = pid_task(pid, PIDTYPE_PID);
3860d855a4b7SOleg Nesterov if (!task)
3861d855a4b7SOleg Nesterov goto fail;
3862d855a4b7SOleg Nesterov
3863d855a4b7SOleg Nesterov /* Attempt to start with the tid of a thread */
38649f6e963fSOleg Nesterov if (tid && nr) {
3865b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns);
3866d855a4b7SOleg Nesterov if (pos && same_thread_group(pos, task))
3867a872ff0cSOleg Nesterov goto found;
38680bc58a91SEric W. Biederman }
38690bc58a91SEric W. Biederman
38700bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */
38719f6e963fSOleg Nesterov if (nr >= get_nr_threads(task))
3872c986c14aSOleg Nesterov goto fail;
3873a872ff0cSOleg Nesterov
3874a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start
3875a872ff0cSOleg Nesterov * with the leader and walk nr threads forward.
3876a872ff0cSOleg Nesterov */
3877dce8f8edSOleg Nesterov for_each_thread(task, pos) {
38789f6e963fSOleg Nesterov if (!nr--)
3879c986c14aSOleg Nesterov goto found;
3880dce8f8edSOleg Nesterov };
3881c986c14aSOleg Nesterov fail:
3882a872ff0cSOleg Nesterov pos = NULL;
3883a872ff0cSOleg Nesterov goto out;
3884a872ff0cSOleg Nesterov found:
3885a872ff0cSOleg Nesterov get_task_struct(pos);
3886a872ff0cSOleg Nesterov out:
3887cc288738SEric W. Biederman rcu_read_unlock();
38880bc58a91SEric W. Biederman return pos;
38890bc58a91SEric W. Biederman }
38900bc58a91SEric W. Biederman
38910bc58a91SEric W. Biederman /*
38920bc58a91SEric W. Biederman * Find the next thread in the thread list.
38930bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread.
38940bc58a91SEric W. Biederman *
38950bc58a91SEric W. Biederman * The reference to the input task_struct is released.
38960bc58a91SEric W. Biederman */
next_tid(struct task_struct * start)38970bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
38980bc58a91SEric W. Biederman {
3899c1df7fb8SOleg Nesterov struct task_struct *pos = NULL;
3900cc288738SEric W. Biederman rcu_read_lock();
3901c1df7fb8SOleg Nesterov if (pid_alive(start)) {
39020bc58a91SEric W. Biederman pos = next_thread(start);
3903c1df7fb8SOleg Nesterov if (thread_group_leader(pos))
39040bc58a91SEric W. Biederman pos = NULL;
3905c1df7fb8SOleg Nesterov else
3906c1df7fb8SOleg Nesterov get_task_struct(pos);
3907c1df7fb8SOleg Nesterov }
3908cc288738SEric W. Biederman rcu_read_unlock();
39090bc58a91SEric W. Biederman put_task_struct(start);
39100bc58a91SEric W. Biederman return pos;
39110bc58a91SEric W. Biederman }
39120bc58a91SEric W. Biederman
39131da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
proc_task_readdir(struct file * file,struct dir_context * ctx)3914f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx)
39151da177e4SLinus Torvalds {
3916d855a4b7SOleg Nesterov struct inode *inode = file_inode(file);
3917d855a4b7SOleg Nesterov struct task_struct *task;
3918b488893aSPavel Emelyanov struct pid_namespace *ns;
3919f0c3b509SAl Viro int tid;
39201da177e4SLinus Torvalds
3921d855a4b7SOleg Nesterov if (proc_inode_is_dead(inode))
3922f0c3b509SAl Viro return -ENOENT;
39231da177e4SLinus Torvalds
3924f0c3b509SAl Viro if (!dir_emit_dots(file, ctx))
3925d855a4b7SOleg Nesterov return 0;
39261da177e4SLinus Torvalds
39270bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't
39280bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0.
39290bc58a91SEric W. Biederman */
39309d78edeaSAlexey Gladkov ns = proc_pid_ns(inode->i_sb);
3931f0c3b509SAl Viro tid = (int)file->f_version;
3932f0c3b509SAl Viro file->f_version = 0;
3933d855a4b7SOleg Nesterov for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
39340bc58a91SEric W. Biederman task;
3935f0c3b509SAl Viro task = next_tid(task), ctx->pos++) {
3936e3912ac3SAlexey Dobriyan char name[10 + 1];
3937a4ef3895SAlexey Dobriyan unsigned int len;
39380658a096SFlorian Weimer
3939b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns);
39400658a096SFlorian Weimer if (!tid)
39410658a096SFlorian Weimer continue; /* The task has just exited. */
3942e3912ac3SAlexey Dobriyan len = snprintf(name, sizeof(name), "%u", tid);
3943f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, name, len,
3944f0c3b509SAl Viro proc_task_instantiate, task, NULL)) {
39450bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first
39460bc58a91SEric W. Biederman * pid for the next readir call */
3947f0c3b509SAl Viro file->f_version = (u64)tid;
39480bc58a91SEric W. Biederman put_task_struct(task);
39491da177e4SLinus Torvalds break;
39500bc58a91SEric W. Biederman }
39511da177e4SLinus Torvalds }
3952d855a4b7SOleg Nesterov
3953f0c3b509SAl Viro return 0;
39541da177e4SLinus Torvalds }
39556e66b52bSEric W. Biederman
proc_task_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)3956b74d24f7SChristian Brauner static int proc_task_getattr(struct mnt_idmap *idmap,
3957549c7297SChristian Brauner const struct path *path, struct kstat *stat,
3958a528d35eSDavid Howells u32 request_mask, unsigned int query_flags)
39596e66b52bSEric W. Biederman {
3960a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry);
396199f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode);
39620d72b928SJeff Layton generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
39636e66b52bSEric W. Biederman
396499f89551SEric W. Biederman if (p) {
396599f89551SEric W. Biederman stat->nlink += get_nr_threads(p);
396699f89551SEric W. Biederman put_task_struct(p);
39676e66b52bSEric W. Biederman }
39686e66b52bSEric W. Biederman
39696e66b52bSEric W. Biederman return 0;
39706e66b52bSEric W. Biederman }
397128a6d671SEric W. Biederman
3972c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
397328a6d671SEric W. Biederman .lookup = proc_task_lookup,
397428a6d671SEric W. Biederman .getattr = proc_task_getattr,
397528a6d671SEric W. Biederman .setattr = proc_setattr,
39760499680aSVasiliy Kulikov .permission = proc_pid_permission,
397728a6d671SEric W. Biederman };
397828a6d671SEric W. Biederman
397900977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
398028a6d671SEric W. Biederman .read = generic_read_dir,
3981f50752eaSAl Viro .iterate_shared = proc_task_readdir,
3982f50752eaSAl Viro .llseek = generic_file_llseek,
398328a6d671SEric W. Biederman };
39841270dd8dSAlexey Dobriyan
set_proc_pid_nlink(void)39851270dd8dSAlexey Dobriyan void __init set_proc_pid_nlink(void)
39861270dd8dSAlexey Dobriyan {
39871270dd8dSAlexey Dobriyan nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
39881270dd8dSAlexey Dobriyan nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
39891270dd8dSAlexey Dobriyan }
3990