blob: 8040f9d1cf07ed42b134f59cb5319fadbaac504f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/proc/base.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * proc base directory handling functions
8 *
9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10 * Instead of using magical inumbers to determine the kind of object
11 * we allocate and fill in-core inodes upon lookup. They don't even
12 * go into icache. We cache the reference to task_struct upon lookup too.
13 * Eventually it should become a filesystem in its own. We don't use the
14 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070015 *
16 *
17 * Changelog:
18 * 17-Jan-2005
19 * Allan Bezerra
20 * Bruna Moreira <bruna.moreira@indt.org.br>
21 * Edjard Mota <edjard.mota@indt.org.br>
22 * Ilias Biris <ilias.biris@indt.org.br>
23 * Mauricio Lin <mauricio.lin@indt.org.br>
24 *
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 *
27 * A new process specific entry (smaps) included in /proc. It shows the
28 * size of rss for each memory area. The maps entry lacks information
29 * about physical memory size (rss) for each mapped file, i.e.,
30 * rss information for executables and library files.
31 * This additional information is useful for any tools that need to know
32 * about physical memory consumption for a process specific library.
33 *
34 * Changelog:
35 * 21-Feb-2005
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
38 *
39 * ChangeLog:
40 * 10-Mar-2005
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
43 *
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
46 *
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
50
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080051#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/errno.h>
54#include <linux/time.h>
55#include <linux/proc_fs.h>
56#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020057#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080059#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040061#include <linux/fdtable.h>
Kent Overstreet94f8f3b2019-03-11 23:31:18 -070062#include <linux/generic-radix-tree.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/string.h>
64#include <linux/seq_file.h>
65#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080066#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070068#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070069#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030071#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070072#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070073#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/mount.h>
75#include <linux/security.h>
76#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070077#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080078#include <linux/printk.h>
Alexey Dobriyanefb1a572018-02-06 15:37:24 -080079#include <linux/cache.h>
Paul Menagea4243162007-10-18 23:39:35 -070080#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/cpuset.h>
82#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050083#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070084#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070085#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070086#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070087#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080088#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040089#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010091#include <linux/sched/autogroup.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010092#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010093#include <linux/sched/coredump.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010094#include <linux/sched/debug.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010095#include <linux/sched/stat.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040096#include <linux/posix-timers.h>
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080097#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040099#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Alexey Dobriyanac7f1062018-02-06 15:36:59 -0800101#include "../../lib/kstrtox.h"
102
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700103/* NOTE:
104 * Implementing inode permission operations in /proc is almost
105 * certainly an error. Permission checks need to happen during
106 * each system call not at open time. The reason is that most of
107 * what we wish to check for permissions in /proc varies at runtime.
108 *
109 * The classic example of a problem is opening file descriptors
110 * in /proc for a task before it execs a suid executable.
111 */
112
Alexey Dobriyanefb1a572018-02-06 15:37:24 -0800113static u8 nlink_tid __ro_after_init;
114static u8 nlink_tgid __ro_after_init;
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700117 const char *name;
Alexey Dobriyan623f5942016-12-12 16:45:08 -0800118 unsigned int len;
Al Virod161a132011-07-24 03:36:29 -0400119 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800120 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800121 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Eric W. Biederman61a28782006-10-02 02:18:49 -0700125#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700126 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700127 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700128 .mode = MODE, \
129 .iop = IOP, \
130 .fop = FOP, \
131 .op = OP, \
132}
133
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300134#define DIR(NAME, MODE, iops, fops) \
135 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700137 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700138 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300139 { .proc_get_link = get_link } )
140#define REG(NAME, MODE, fops) \
141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300142#define ONE(NAME, MODE, show) \
Casey Schaufler6d9c9392018-09-21 17:16:59 -0700143 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800144 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300145 { .proc_show = show } )
Casey Schaufler6d9c9392018-09-21 17:16:59 -0700146#define ATTR(LSM, NAME, MODE) \
147 NOD(NAME, (S_IFREG|(MODE)), \
148 NULL, &proc_pid_attr_operations, \
149 { .lsm = LSM })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Vegard Nossumaed54172008-06-05 22:46:53 -0700151/*
152 * Count the number of hardlinks for the pid_entry table, excluding the .
153 * and .. links.
154 */
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800155static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
Vegard Nossumaed54172008-06-05 22:46:53 -0700156 unsigned int n)
157{
158 unsigned int i;
159 unsigned int count;
160
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800161 count = 2;
Vegard Nossumaed54172008-06-05 22:46:53 -0700162 for (i = 0; i < n; ++i) {
163 if (S_ISDIR(entries[i].mode))
164 ++count;
165 }
166
167 return count;
168}
169
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200170static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000172 int result = -ENOENT;
173
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700174 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200175 if (task->fs) {
176 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000177 result = 0;
178 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700179 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000180 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700181}
182
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800183static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700184{
David Howells2b0143b2015-03-17 22:25:59 +0000185 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700186 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700187
188 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200189 task_lock(task);
190 if (task->fs) {
191 get_fs_pwd(task->fs, path);
192 result = 0;
193 }
194 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700195 put_task_struct(task);
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return result;
198}
199
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800200static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
David Howells2b0143b2015-03-17 22:25:59 +0000202 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700204
205 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200206 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700207 put_task_struct(task);
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return result;
210}
211
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700212static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
Linus Torvalds5ab82712018-05-17 15:17:33 -0700213 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Linus Torvalds3d712542019-07-13 13:40:13 -0700215 unsigned long arg_start, arg_end;
Linus Torvalds5ab82712018-05-17 15:17:33 -0700216 unsigned long pos, len;
217 char *page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700218
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700219 /* Check if process spawned far enough to have cmdline. */
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700220 if (!mm->env_end)
221 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700222
Yang Shi88aa7cc2018-06-07 17:05:28 -0700223 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700224 arg_start = mm->arg_start;
225 arg_end = mm->arg_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700226 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700227
Linus Torvalds5ab82712018-05-17 15:17:33 -0700228 if (arg_start >= arg_end)
229 return 0;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700230
Linus Torvalds5ab82712018-05-17 15:17:33 -0700231 /* We're not going to care if "*ppos" has high bits set */
Linus Torvalds5ab82712018-05-17 15:17:33 -0700232 /* .. but we do check the result is in the proper range */
Linus Torvalds3d712542019-07-13 13:40:13 -0700233 pos = arg_start + *ppos;
234 if (pos < arg_start || pos >= arg_end)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700235 return 0;
Linus Torvalds3d712542019-07-13 13:40:13 -0700236 if (count > arg_end - pos)
237 count = arg_end - pos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700238
Linus Torvalds5ab82712018-05-17 15:17:33 -0700239 page = (char *)__get_free_page(GFP_KERNEL);
240 if (!page)
241 return -ENOMEM;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700242
Linus Torvalds5ab82712018-05-17 15:17:33 -0700243 len = 0;
244 while (count) {
245 int got;
246 size_t size = min_t(size_t, PAGE_SIZE, count);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700247
Linus Torvalds3d712542019-07-13 13:40:13 -0700248 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
249 if (got <= 0)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700250 break;
Linus Torvalds3d712542019-07-13 13:40:13 -0700251 got -= copy_to_user(buf, page, got);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700252 if (unlikely(!got)) {
253 if (!len)
254 len = -EFAULT;
255 break;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700256 }
Linus Torvalds5ab82712018-05-17 15:17:33 -0700257 pos += got;
258 buf += got;
259 len += got;
260 count -= got;
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700261 }
262
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700263 free_page((unsigned long)page);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700264 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700267static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
268 size_t count, loff_t *pos)
269{
270 struct mm_struct *mm;
271 ssize_t ret;
272
273 mm = get_task_mm(tsk);
274 if (!mm)
275 return 0;
276
277 ret = get_mm_cmdline(mm, buf, count, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 mmput(mm);
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700279 return ret;
280}
281
282static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
283 size_t count, loff_t *pos)
284{
285 struct task_struct *tsk;
286 ssize_t ret;
287
288 BUG_ON(*pos < 0);
289
290 tsk = get_proc_task(file_inode(file));
291 if (!tsk)
292 return -ESRCH;
293 ret = get_task_cmdline(tsk, buf, count, pos);
294 put_task_struct(tsk);
295 if (ret > 0)
296 *pos += ret;
297 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700300static const struct file_operations proc_pid_cmdline_ops = {
301 .read = proc_pid_cmdline_read,
302 .llseek = generic_file_llseek,
303};
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#ifdef CONFIG_KALLSYMS
306/*
307 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
308 * Returns the resolved symbol. If that fails, simply return the address.
309 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700310static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
311 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700313 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700314 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700316 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
317 goto print0;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700320 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700321 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700322 return 0;
323 }
Joe Perches25ce3192015-04-15 16:18:17 -0700324
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700325print0:
326 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329#endif /* CONFIG_KALLSYMS */
330
Al Viroa9712bc2011-03-23 15:52:50 -0400331static int lock_trace(struct task_struct *task)
332{
333 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
334 if (err)
335 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800336 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400337 mutex_unlock(&task->signal->cred_guard_mutex);
338 return -EPERM;
339 }
340 return 0;
341}
342
343static void unlock_trace(struct task_struct *task)
344{
345 mutex_unlock(&task->signal->cred_guard_mutex);
346}
347
Ken Chen2ec220e2008-11-10 11:26:08 +0300348#ifdef CONFIG_STACKTRACE
349
350#define MAX_STACK_TRACE_DEPTH 64
351
352static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
353 struct pid *pid, struct task_struct *task)
354{
Ken Chen2ec220e2008-11-10 11:26:08 +0300355 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400356 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300357
Jann Hornf8a00ce2018-10-05 15:51:58 -0700358 /*
359 * The ability to racily run the kernel stack unwinder on a running task
360 * and then observe the unwinder output is scary; while it is useful for
361 * debugging kernel issues, it can also allow an attacker to leak kernel
362 * stack contents.
363 * Doing this in a manner that is at least safe from races would require
364 * some work to ensure that the remote task can not be scheduled; and
365 * even then, this would still expose the unwinder as local attack
366 * surface.
367 * Therefore, this interface is restricted to root.
368 */
369 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
370 return -EACCES;
371
Kees Cook6da2ec52018-06-12 13:55:00 -0700372 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
373 GFP_KERNEL);
Ken Chen2ec220e2008-11-10 11:26:08 +0300374 if (!entries)
375 return -ENOMEM;
376
Al Viroa9712bc2011-03-23 15:52:50 -0400377 err = lock_trace(task);
378 if (!err) {
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200379 unsigned int i, nr_entries;
Alexey Dobriyan5d008fb2018-06-07 17:10:17 -0700380
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200381 nr_entries = stack_trace_save_tsk(task, entries,
382 MAX_STACK_TRACE_DEPTH, 0);
Al Viroa9712bc2011-03-23 15:52:50 -0400383
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200384 for (i = 0; i < nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800385 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400386 }
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200387
Al Viroa9712bc2011-03-23 15:52:50 -0400388 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300389 }
390 kfree(entries);
391
Al Viroa9712bc2011-03-23 15:52:50 -0400392 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300393}
394#endif
395
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530396#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/*
398 * Provides /proc/PID/schedstat
399 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700400static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
401 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530403 if (unlikely(!sched_info_on()))
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800404 seq_puts(m, "0 0 0\n");
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530405 else
406 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700407 (unsigned long long)task->se.sum_exec_runtime,
408 (unsigned long long)task->sched_info.run_delay,
409 task->sched_info.pcount);
410
411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413#endif
414
Arjan van de Ven97455122008-01-25 21:08:34 +0100415#ifdef CONFIG_LATENCYTOP
416static int lstats_show_proc(struct seq_file *m, void *v)
417{
418 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800419 struct inode *inode = m->private;
420 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100421
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800422 if (!task)
423 return -ESRCH;
424 seq_puts(m, "Latency Top version : v0.1\n");
Alexey Dobriyanf6d2f582018-08-21 21:54:34 -0700425 for (i = 0; i < LT_SAVECOUNT; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800426 struct latency_record *lr = &task->latency_record[i];
427 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100428 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800429 seq_printf(m, "%i %li %li",
430 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100431 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800432 unsigned long bt = lr->backtrace[q];
Thomas Gleixneraccddc42019-04-10 12:28:08 +0200433
Joe Perches34e49d42011-01-12 17:00:30 -0800434 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100435 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800436 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100437 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800438 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100439 }
440
441 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800442 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100443 return 0;
444}
445
446static int lstats_open(struct inode *inode, struct file *file)
447{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800448 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800449}
450
Arjan van de Ven97455122008-01-25 21:08:34 +0100451static ssize_t lstats_write(struct file *file, const char __user *buf,
452 size_t count, loff_t *offs)
453{
Al Viro496ad9a2013-01-23 17:07:38 -0500454 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100455
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800456 if (!task)
457 return -ESRCH;
Lin Fenge02c9b02019-05-14 15:42:34 -0700458 clear_tsk_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800459 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100460
461 return count;
462}
463
464static const struct file_operations proc_lstats_operations = {
465 .open = lstats_open,
466 .read = seq_read,
467 .write = lstats_write,
468 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800469 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100470};
471
472#endif
473
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700474static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
475 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Arun KSca79b0c2018-12-28 00:34:29 -0800477 unsigned long totalpages = totalram_pages() + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200478 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Oleg Nesterovef419392016-08-02 14:03:19 -0700480 points = oom_badness(task, NULL, NULL, totalpages) *
481 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700482 seq_printf(m, "%lu\n", points);
483
484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Neil Hormand85f50d2007-10-18 23:40:37 -0700487struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700488 const char *name;
489 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700490};
491
492static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700493 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700494 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
495 [RLIMIT_DATA] = {"Max data size", "bytes"},
496 [RLIMIT_STACK] = {"Max stack size", "bytes"},
497 [RLIMIT_CORE] = {"Max core file size", "bytes"},
498 [RLIMIT_RSS] = {"Max resident set", "bytes"},
499 [RLIMIT_NPROC] = {"Max processes", "processes"},
500 [RLIMIT_NOFILE] = {"Max open files", "files"},
501 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
502 [RLIMIT_AS] = {"Max address space", "bytes"},
503 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
504 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
505 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
506 [RLIMIT_NICE] = {"Max nice priority", NULL},
507 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800508 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700509};
510
511/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700512static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
513 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700514{
515 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700516 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700517
518 struct rlimit rlim[RLIM_NLIMITS];
519
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400520 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700521 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700522 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
523 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700524
525 /*
526 * print the file header
527 */
Alexey Dobriyanafe922c2019-01-03 15:26:09 -0800528 seq_puts(m, "Limit "
529 "Soft Limit "
530 "Hard Limit "
531 "Units \n");
Neil Hormand85f50d2007-10-18 23:40:37 -0700532
533 for (i = 0; i < RLIM_NLIMITS; i++) {
534 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700535 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700536 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700537 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700538 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700539 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700540
541 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700542 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700543 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700544 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700545
546 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700547 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700548 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700549 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700550 }
551
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700552 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700553}
554
Roland McGrathebcb6732008-07-25 19:46:00 -0700555#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700556static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
557 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700558{
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500559 struct syscall_info info;
560 u64 *args = &info.data.args[0];
Joe Perches25ce3192015-04-15 16:18:17 -0700561 int res;
562
563 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400564 if (res)
565 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700566
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500567 if (task_current_syscall(task, &info))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700568 seq_puts(m, "running\n");
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500569 else if (info.data.nr < 0)
570 seq_printf(m, "%d 0x%llx 0x%llx\n",
571 info.data.nr, info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400572 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700573 seq_printf(m,
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500574 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
575 info.data.nr,
Roland McGrathebcb6732008-07-25 19:46:00 -0700576 args[0], args[1], args[2], args[3], args[4], args[5],
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500577 info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400578 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700579
580 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700581}
582#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/************************************************************************/
585/* Here the fs part begins */
586/************************************************************************/
587
588/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700589static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700591 struct task_struct *task;
592 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700593 /* Allow access to a task's file descriptors if it is us or we
594 * may use ptrace attach to the process and find out that
595 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700596 */
597 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700598 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800599 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700600 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700601 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700602 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800605int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700606{
607 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000608 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700609
610 if (attr->ia_valid & ATTR_MODE)
611 return -EPERM;
612
Jan Kara31051c82016-05-26 16:55:18 +0200613 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200614 if (error)
615 return error;
616
Christoph Hellwig10257742010-06-04 11:30:02 +0200617 setattr_copy(inode, attr);
618 mark_inode_dirty(inode);
619 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700620}
621
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800622/*
623 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
624 * or euid/egid (for hide_pid_min=2)?
625 */
626static bool has_pid_permissions(struct pid_namespace *pid,
627 struct task_struct *task,
628 int hide_pid_min)
629{
630 if (pid->hide_pid < hide_pid_min)
631 return true;
632 if (in_group_p(pid->pid_gid))
633 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800634 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800635}
636
637
638static int proc_pid_permission(struct inode *inode, int mask)
639{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200640 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800641 struct task_struct *task;
642 bool has_perms;
643
644 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800645 if (!task)
646 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800647 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800648 put_task_struct(task);
649
650 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800651 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800652 /*
653 * Let's make getdents(), stat(), and open()
654 * consistent with each other. If a process
655 * may not stat() a file, it shouldn't be seen
656 * in procfs at all.
657 */
658 return -ENOENT;
659 }
660
661 return -EPERM;
662 }
663 return generic_permission(inode, mask);
664}
665
666
667
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800668static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700669 .setattr = proc_setattr,
670};
671
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800672static int proc_single_show(struct seq_file *m, void *v)
673{
674 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200675 struct pid_namespace *ns = proc_pid_ns(inode);
676 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800677 struct task_struct *task;
678 int ret;
679
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800680 task = get_pid_task(pid, PIDTYPE_PID);
681 if (!task)
682 return -ESRCH;
683
684 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
685
686 put_task_struct(task);
687 return ret;
688}
689
690static int proc_single_open(struct inode *inode, struct file *filp)
691{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800692 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800693}
694
695static const struct file_operations proc_single_file_operations = {
696 .open = proc_single_open,
697 .read = seq_read,
698 .llseek = seq_lseek,
699 .release = single_release,
700};
701
Oleg Nesterov5381e162014-10-09 15:25:24 -0700702
703struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
704{
705 struct task_struct *task = get_proc_task(inode);
706 struct mm_struct *mm = ERR_PTR(-ESRCH);
707
708 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800709 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700710 put_task_struct(task);
711
712 if (!IS_ERR_OR_NULL(mm)) {
713 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800714 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700715 /* but do not pin its memory */
716 mmput(mm);
717 }
718 }
719
720 return mm;
721}
722
Cong Wangb409e572012-05-31 16:26:17 -0700723static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700725 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800726
727 if (IS_ERR(mm))
728 return PTR_ERR(mm);
729
Linus Torvaldse2683372012-01-17 15:21:19 -0800730 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return 0;
732}
733
Cong Wangb409e572012-05-31 16:26:17 -0700734static int mem_open(struct inode *inode, struct file *file)
735{
Djalal Harounibc452b42012-07-30 14:42:28 -0700736 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
737
738 /* OK to pass negative loff_t, we can catch out-of-range */
739 file->f_mode |= FMODE_UNSIGNED_OFFSET;
740
741 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700742}
743
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100744static ssize_t mem_rw(struct file *file, char __user *buf,
745 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Linus Torvaldse2683372012-01-17 15:21:19 -0800747 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100748 unsigned long addr = *ppos;
749 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700751 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvaldse2683372012-01-17 15:21:19 -0800753 if (!mm)
754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Michal Hocko0ee931c2017-09-13 16:28:29 -0700756 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800758 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700760 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800761 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100762 goto free;
763
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700764 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100767 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100769 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 copied = -EFAULT;
771 break;
772 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100773
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100774 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100775 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (!copied)
777 copied = -EIO;
778 break;
779 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100780
781 if (!write && copy_to_user(buf, page, this_len)) {
782 copied = -EFAULT;
783 break;
784 }
785
786 buf += this_len;
787 addr += this_len;
788 copied += this_len;
789 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100791 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700792
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100793 mmput(mm);
794free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700795 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return copied;
797}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100799static ssize_t mem_read(struct file *file, char __user *buf,
800 size_t count, loff_t *ppos)
801{
802 return mem_rw(file, buf, count, ppos, 0);
803}
804
805static ssize_t mem_write(struct file *file, const char __user *buf,
806 size_t count, loff_t *ppos)
807{
808 return mem_rw(file, (char __user*)buf, count, ppos, 1);
809}
810
Matt Mackall85863e42008-02-04 22:29:04 -0800811loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 switch (orig) {
814 case 0:
815 file->f_pos = offset;
816 break;
817 case 1:
818 file->f_pos += offset;
819 break;
820 default:
821 return -EINVAL;
822 }
823 force_successful_syscall_return();
824 return file->f_pos;
825}
826
Linus Torvaldse2683372012-01-17 15:21:19 -0800827static int mem_release(struct inode *inode, struct file *file)
828{
829 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100830 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100831 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800832 return 0;
833}
834
Arjan van de Ven00977a52007-02-12 00:55:34 -0800835static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .llseek = mem_lseek,
837 .read = mem_read,
838 .write = mem_write,
839 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800840 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841};
842
Cong Wangb409e572012-05-31 16:26:17 -0700843static int environ_open(struct inode *inode, struct file *file)
844{
845 return __mem_open(inode, file, PTRACE_MODE_READ);
846}
847
James Pearson315e28c2007-10-16 23:30:17 -0700848static ssize_t environ_read(struct file *file, char __user *buf,
849 size_t count, loff_t *ppos)
850{
James Pearson315e28c2007-10-16 23:30:17 -0700851 char *page;
852 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700853 int ret = 0;
854 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800855 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700856
Mathias Krause8148a732016-05-05 16:22:26 -0700857 /* Ensure the process spawned far enough to have an environment. */
858 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700859 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700860
Michal Hocko0ee931c2017-09-13 16:28:29 -0700861 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700862 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700863 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700864
Al Virod6f64b82011-02-15 22:26:01 -0500865 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800866 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700867 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800868
Yang Shi88aa7cc2018-06-07 17:05:28 -0700869 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800870 env_start = mm->env_start;
871 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700872 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800873
James Pearson315e28c2007-10-16 23:30:17 -0700874 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700875 size_t this_len, max_len;
876 int retval;
877
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800878 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700879 break;
James Pearson315e28c2007-10-16 23:30:17 -0700880
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800881 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700882
Djalal Harounie8905ec2012-07-30 14:42:26 -0700883 max_len = min_t(size_t, PAGE_SIZE, count);
884 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700885
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200886 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700887
888 if (retval <= 0) {
889 ret = retval;
890 break;
891 }
892
893 if (copy_to_user(buf, page, retval)) {
894 ret = -EFAULT;
895 break;
896 }
897
898 ret += retval;
899 src += retval;
900 buf += retval;
901 count -= retval;
902 }
903 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700904 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700905
906free:
James Pearson315e28c2007-10-16 23:30:17 -0700907 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700908 return ret;
909}
910
911static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700912 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700913 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100914 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700915 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700916};
917
Al Viroc5317162016-10-05 18:43:43 -0400918static int auxv_open(struct inode *inode, struct file *file)
919{
920 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
921}
922
923static ssize_t auxv_read(struct file *file, char __user *buf,
924 size_t count, loff_t *ppos)
925{
926 struct mm_struct *mm = file->private_data;
927 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700928
929 if (!mm)
930 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400931 do {
932 nwords += 2;
933 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
934 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
935 nwords * sizeof(mm->saved_auxv[0]));
936}
937
938static const struct file_operations proc_auxv_operations = {
939 .open = auxv_open,
940 .read = auxv_read,
941 .llseek = generic_file_llseek,
942 .release = mem_release,
943};
944
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800945static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
946 loff_t *ppos)
947{
Al Viro496ad9a2013-01-23 17:07:38 -0500948 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800949 char buffer[PROC_NUMBUF];
950 int oom_adj = OOM_ADJUST_MIN;
951 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800952
953 if (!task)
954 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -0700955 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
956 oom_adj = OOM_ADJUST_MAX;
957 else
958 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
959 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800960 put_task_struct(task);
961 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
962 return simple_read_from_buffer(buf, count, ppos, buffer, len);
963}
964
Michal Hocko1d5f0ac2016-07-28 15:44:40 -0700965static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
966{
967 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -0700968 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -0700969 struct task_struct *task;
970 int err = 0;
971
972 task = get_proc_task(file_inode(file));
973 if (!task)
974 return -ESRCH;
975
976 mutex_lock(&oom_adj_mutex);
977 if (legacy) {
978 if (oom_adj < task->signal->oom_score_adj &&
979 !capable(CAP_SYS_RESOURCE)) {
980 err = -EACCES;
981 goto err_unlock;
982 }
983 /*
984 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
985 * /proc/pid/oom_score_adj instead.
986 */
987 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
988 current->comm, task_pid_nr(current), task_pid_nr(task),
989 task_pid_nr(task));
990 } else {
991 if ((short)oom_adj < task->signal->oom_score_adj_min &&
992 !capable(CAP_SYS_RESOURCE)) {
993 err = -EACCES;
994 goto err_unlock;
995 }
996 }
997
Michal Hocko44a70ade2016-07-28 15:44:43 -0700998 /*
999 * Make sure we will check other processes sharing the mm if this is
1000 * not vfrok which wants its own oom_score_adj.
1001 * pin the mm so it doesn't go away and get reused after task_unlock
1002 */
1003 if (!task->vfork_done) {
1004 struct task_struct *p = find_lock_task_mm(task);
1005
1006 if (p) {
1007 if (atomic_read(&p->mm->mm_users) > 1) {
1008 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001009 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001010 }
1011 task_unlock(p);
1012 }
1013 }
1014
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001015 task->signal->oom_score_adj = oom_adj;
1016 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1017 task->signal->oom_score_adj_min = (short)oom_adj;
1018 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001019
1020 if (mm) {
1021 struct task_struct *p;
1022
1023 rcu_read_lock();
1024 for_each_process(p) {
1025 if (same_thread_group(task, p))
1026 continue;
1027
1028 /* do not touch kernel threads or the global init */
1029 if (p->flags & PF_KTHREAD || is_global_init(p))
1030 continue;
1031
1032 task_lock(p);
1033 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001034 p->signal->oom_score_adj = oom_adj;
1035 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1036 p->signal->oom_score_adj_min = (short)oom_adj;
1037 }
1038 task_unlock(p);
1039 }
1040 rcu_read_unlock();
1041 mmdrop(mm);
1042 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001043err_unlock:
1044 mutex_unlock(&oom_adj_mutex);
1045 put_task_struct(task);
1046 return err;
1047}
Michal Hockof913da52016-07-28 15:44:37 -07001048
David Rientjesb72bdfa2015-11-05 18:50:32 -08001049/*
1050 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1051 * kernels. The effective policy is defined by oom_score_adj, which has a
1052 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1053 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1054 * Processes that become oom disabled via oom_adj will still be oom disabled
1055 * with this implementation.
1056 *
1057 * oom_adj cannot be removed since existing userspace binaries use it.
1058 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001059static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1060 size_t count, loff_t *ppos)
1061{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001062 char buffer[PROC_NUMBUF];
1063 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001064 int err;
1065
1066 memset(buffer, 0, sizeof(buffer));
1067 if (count > sizeof(buffer) - 1)
1068 count = sizeof(buffer) - 1;
1069 if (copy_from_user(buffer, buf, count)) {
1070 err = -EFAULT;
1071 goto out;
1072 }
1073
1074 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1075 if (err)
1076 goto out;
1077 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1078 oom_adj != OOM_DISABLE) {
1079 err = -EINVAL;
1080 goto out;
1081 }
1082
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001083 /*
1084 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1085 * value is always attainable.
1086 */
1087 if (oom_adj == OOM_ADJUST_MAX)
1088 oom_adj = OOM_SCORE_ADJ_MAX;
1089 else
1090 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1091
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001092 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001093out:
1094 return err < 0 ? err : count;
1095}
1096
1097static const struct file_operations proc_oom_adj_operations = {
1098 .read = oom_adj_read,
1099 .write = oom_adj_write,
1100 .llseek = generic_file_llseek,
1101};
1102
David Rientjesa63d83f2010-08-09 17:19:46 -07001103static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1104 size_t count, loff_t *ppos)
1105{
Al Viro496ad9a2013-01-23 17:07:38 -05001106 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001107 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001108 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001109 size_t len;
1110
1111 if (!task)
1112 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001113 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001114 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001115 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001116 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1117}
1118
1119static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1120 size_t count, loff_t *ppos)
1121{
David Rientjesa63d83f2010-08-09 17:19:46 -07001122 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001123 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001124 int err;
1125
1126 memset(buffer, 0, sizeof(buffer));
1127 if (count > sizeof(buffer) - 1)
1128 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001129 if (copy_from_user(buffer, buf, count)) {
1130 err = -EFAULT;
1131 goto out;
1132 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001133
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001134 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001135 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001136 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001137 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001138 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1139 err = -EINVAL;
1140 goto out;
1141 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001142
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001143 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001144out:
1145 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001146}
1147
1148static const struct file_operations proc_oom_score_adj_operations = {
1149 .read = oom_score_adj_read,
1150 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001151 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001152};
1153
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05001154#ifdef CONFIG_AUDIT
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001155#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1157 size_t count, loff_t *ppos)
1158{
Al Viro496ad9a2013-01-23 17:07:38 -05001159 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001160 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 ssize_t length;
1162 char tmpbuf[TMPBUFLEN];
1163
Eric W. Biederman99f89552006-06-26 00:25:55 -07001164 if (!task)
1165 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001167 from_kuid(file->f_cred->user_ns,
1168 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001169 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1171}
1172
1173static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1174 size_t count, loff_t *ppos)
1175{
Al Viro496ad9a2013-01-23 17:07:38 -05001176 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001178 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001179 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001181 rcu_read_lock();
1182 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1183 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001185 }
1186 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (*ppos != 0) {
1189 /* No partial writes. */
1190 return -EINVAL;
1191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001193 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1194 if (rv < 0)
1195 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001196
1197 /* is userspace tring to explicitly UNSET the loginuid? */
1198 if (loginuid == AUDIT_UID_UNSET) {
1199 kloginuid = INVALID_UID;
1200 } else {
1201 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001202 if (!uid_valid(kloginuid))
1203 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001204 }
1205
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001206 rv = audit_set_loginuid(kloginuid);
1207 if (rv < 0)
1208 return rv;
1209 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Arjan van de Ven00977a52007-02-12 00:55:34 -08001212static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 .read = proc_loginuid_read,
1214 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001215 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216};
Eric Paris1e0bd752008-03-13 08:15:31 -04001217
1218static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1219 size_t count, loff_t *ppos)
1220{
Al Viro496ad9a2013-01-23 17:07:38 -05001221 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001222 struct task_struct *task = get_proc_task(inode);
1223 ssize_t length;
1224 char tmpbuf[TMPBUFLEN];
1225
1226 if (!task)
1227 return -ESRCH;
1228 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1229 audit_get_sessionid(task));
1230 put_task_struct(task);
1231 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1232}
1233
1234static const struct file_operations proc_sessionid_operations = {
1235 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001236 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001237};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238#endif
1239
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001240#ifdef CONFIG_FAULT_INJECTION
1241static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1242 size_t count, loff_t *ppos)
1243{
Al Viro496ad9a2013-01-23 17:07:38 -05001244 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001245 char buffer[PROC_NUMBUF];
1246 size_t len;
1247 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001248
1249 if (!task)
1250 return -ESRCH;
1251 make_it_fail = task->make_it_fail;
1252 put_task_struct(task);
1253
1254 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001255
1256 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001257}
1258
1259static ssize_t proc_fault_inject_write(struct file * file,
1260 const char __user * buf, size_t count, loff_t *ppos)
1261{
1262 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001263 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001264 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001265 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001266
1267 if (!capable(CAP_SYS_RESOURCE))
1268 return -EPERM;
1269 memset(buffer, 0, sizeof(buffer));
1270 if (count > sizeof(buffer) - 1)
1271 count = sizeof(buffer) - 1;
1272 if (copy_from_user(buffer, buf, count))
1273 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001274 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1275 if (rv < 0)
1276 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001277 if (make_it_fail < 0 || make_it_fail > 1)
1278 return -EINVAL;
1279
Al Viro496ad9a2013-01-23 17:07:38 -05001280 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001281 if (!task)
1282 return -ESRCH;
1283 task->make_it_fail = make_it_fail;
1284 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001285
1286 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001287}
1288
Arjan van de Ven00977a52007-02-12 00:55:34 -08001289static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001290 .read = proc_fault_inject_read,
1291 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001292 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001293};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001294
1295static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1296 size_t count, loff_t *ppos)
1297{
1298 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001299 int err;
1300 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001301
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001302 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001303 if (err)
1304 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001305
1306 task = get_proc_task(file_inode(file));
1307 if (!task)
1308 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001309 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001310 put_task_struct(task);
1311
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001312 return count;
1313}
1314
1315static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1316 size_t count, loff_t *ppos)
1317{
1318 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001319 char numbuf[PROC_NUMBUF];
1320 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001321
1322 task = get_proc_task(file_inode(file));
1323 if (!task)
1324 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001325 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001326 put_task_struct(task);
Alexey Dobriyana44937f2018-08-21 21:54:27 -07001327 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001328}
1329
1330static const struct file_operations proc_fail_nth_operations = {
1331 .read = proc_fail_nth_read,
1332 .write = proc_fail_nth_write,
1333};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001334#endif
1335
Arjan van de Ven97455122008-01-25 21:08:34 +01001336
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001337#ifdef CONFIG_SCHED_DEBUG
1338/*
1339 * Print out various scheduling related per-task fields:
1340 */
1341static int sched_show(struct seq_file *m, void *v)
1342{
1343 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001344 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001345 struct task_struct *p;
1346
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001347 p = get_proc_task(inode);
1348 if (!p)
1349 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001350 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001351
1352 put_task_struct(p);
1353
1354 return 0;
1355}
1356
1357static ssize_t
1358sched_write(struct file *file, const char __user *buf,
1359 size_t count, loff_t *offset)
1360{
Al Viro496ad9a2013-01-23 17:07:38 -05001361 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001362 struct task_struct *p;
1363
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001364 p = get_proc_task(inode);
1365 if (!p)
1366 return -ESRCH;
1367 proc_sched_set_task(p);
1368
1369 put_task_struct(p);
1370
1371 return count;
1372}
1373
1374static int sched_open(struct inode *inode, struct file *filp)
1375{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001376 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001377}
1378
1379static const struct file_operations proc_pid_sched_operations = {
1380 .open = sched_open,
1381 .read = seq_read,
1382 .write = sched_write,
1383 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001384 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001385};
1386
1387#endif
1388
Mike Galbraith5091faa2010-11-30 14:18:03 +01001389#ifdef CONFIG_SCHED_AUTOGROUP
1390/*
1391 * Print out autogroup related information:
1392 */
1393static int sched_autogroup_show(struct seq_file *m, void *v)
1394{
1395 struct inode *inode = m->private;
1396 struct task_struct *p;
1397
1398 p = get_proc_task(inode);
1399 if (!p)
1400 return -ESRCH;
1401 proc_sched_autogroup_show_task(p, m);
1402
1403 put_task_struct(p);
1404
1405 return 0;
1406}
1407
1408static ssize_t
1409sched_autogroup_write(struct file *file, const char __user *buf,
1410 size_t count, loff_t *offset)
1411{
Al Viro496ad9a2013-01-23 17:07:38 -05001412 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001413 struct task_struct *p;
1414 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001415 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001416 int err;
1417
1418 memset(buffer, 0, sizeof(buffer));
1419 if (count > sizeof(buffer) - 1)
1420 count = sizeof(buffer) - 1;
1421 if (copy_from_user(buffer, buf, count))
1422 return -EFAULT;
1423
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001424 err = kstrtoint(strstrip(buffer), 0, &nice);
1425 if (err < 0)
1426 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001427
1428 p = get_proc_task(inode);
1429 if (!p)
1430 return -ESRCH;
1431
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001432 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001433 if (err)
1434 count = err;
1435
1436 put_task_struct(p);
1437
1438 return count;
1439}
1440
1441static int sched_autogroup_open(struct inode *inode, struct file *filp)
1442{
1443 int ret;
1444
1445 ret = single_open(filp, sched_autogroup_show, NULL);
1446 if (!ret) {
1447 struct seq_file *m = filp->private_data;
1448
1449 m->private = inode;
1450 }
1451 return ret;
1452}
1453
1454static const struct file_operations proc_pid_sched_autogroup_operations = {
1455 .open = sched_autogroup_open,
1456 .read = seq_read,
1457 .write = sched_autogroup_write,
1458 .llseek = seq_lseek,
1459 .release = single_release,
1460};
1461
1462#endif /* CONFIG_SCHED_AUTOGROUP */
1463
john stultz4614a696b2009-12-14 18:00:05 -08001464static ssize_t comm_write(struct file *file, const char __user *buf,
1465 size_t count, loff_t *offset)
1466{
Al Viro496ad9a2013-01-23 17:07:38 -05001467 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001468 struct task_struct *p;
1469 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001470 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001471
1472 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001473 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001474 return -EFAULT;
1475
1476 p = get_proc_task(inode);
1477 if (!p)
1478 return -ESRCH;
1479
1480 if (same_thread_group(current, p))
1481 set_task_comm(p, buffer);
1482 else
1483 count = -EINVAL;
1484
1485 put_task_struct(p);
1486
1487 return count;
1488}
1489
1490static int comm_show(struct seq_file *m, void *v)
1491{
1492 struct inode *inode = m->private;
1493 struct task_struct *p;
1494
1495 p = get_proc_task(inode);
1496 if (!p)
1497 return -ESRCH;
1498
Tejun Heo88b72b32018-05-18 08:47:13 -07001499 proc_task_name(m, p, false);
1500 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001501
1502 put_task_struct(p);
1503
1504 return 0;
1505}
1506
1507static int comm_open(struct inode *inode, struct file *filp)
1508{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001509 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001510}
1511
1512static const struct file_operations proc_pid_set_comm_operations = {
1513 .open = comm_open,
1514 .read = seq_read,
1515 .write = comm_write,
1516 .llseek = seq_lseek,
1517 .release = single_release,
1518};
1519
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001520static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001521{
1522 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001523 struct file *exe_file;
1524
David Howells2b0143b2015-03-17 22:25:59 +00001525 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001526 if (!task)
1527 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001528 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001529 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001530 if (exe_file) {
1531 *exe_path = exe_file->f_path;
1532 path_get(&exe_file->f_path);
1533 fput(exe_file);
1534 return 0;
1535 } else
1536 return -ENOENT;
1537}
1538
Al Viro6b255392015-11-17 10:20:54 -05001539static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001540 struct inode *inode,
1541 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001543 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int error = -EACCES;
1545
Al Viro6b255392015-11-17 10:20:54 -05001546 if (!dentry)
1547 return ERR_PTR(-ECHILD);
1548
Eric W. Biederman778c1142006-06-26 00:25:58 -07001549 /* Are we allowed to snoop on the tasks file descriptors? */
1550 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Christoph Hellwig408ef012012-06-18 10:47:03 -04001553 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1554 if (error)
1555 goto out;
1556
Al Viro6e77137b2015-05-02 13:37:52 -04001557 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001558 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559out:
Al Viro008b1502005-08-20 00:17:39 +01001560 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001563static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001565 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001566 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int len;
1568
1569 if (!tmp)
1570 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001571
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001572 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001573 len = PTR_ERR(pathname);
1574 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001576 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 if (len > buflen)
1579 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001580 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 len = -EFAULT;
1582 out:
1583 free_page((unsigned long)tmp);
1584 return len;
1585}
1586
1587static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1588{
1589 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001590 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001591 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Eric W. Biederman778c1142006-06-26 00:25:58 -07001593 /* Are we allowed to snoop on the tasks file descriptors? */
1594 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001597 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (error)
1599 goto out;
1600
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001601 error = do_proc_readlink(&path, buffer, buflen);
1602 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return error;
1605}
1606
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001607const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001609 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001610 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611};
1612
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001613
1614/* building an inode */
1615
Al Viroc6eb50d2017-09-30 14:45:42 -04001616void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001617 kuid_t *ruid, kgid_t *rgid)
1618{
1619 /* Depending on the state of dumpable compute who should own a
1620 * proc file for a task.
1621 */
1622 const struct cred *cred;
1623 kuid_t uid;
1624 kgid_t gid;
1625
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001626 if (unlikely(task->flags & PF_KTHREAD)) {
1627 *ruid = GLOBAL_ROOT_UID;
1628 *rgid = GLOBAL_ROOT_GID;
1629 return;
1630 }
1631
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001632 /* Default to the tasks effective ownership */
1633 rcu_read_lock();
1634 cred = __task_cred(task);
1635 uid = cred->euid;
1636 gid = cred->egid;
1637 rcu_read_unlock();
1638
1639 /*
1640 * Before the /proc/pid/status file was created the only way to read
1641 * the effective uid of a /process was to stat /proc/pid. Reading
1642 * /proc/pid/status is slow enough that procps and other packages
1643 * kept stating /proc/pid. To keep the rules in /proc simple I have
1644 * made this apply to all per process world readable and executable
1645 * directories.
1646 */
1647 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1648 struct mm_struct *mm;
1649 task_lock(task);
1650 mm = task->mm;
1651 /* Make non-dumpable tasks owned by some root */
1652 if (mm) {
1653 if (get_dumpable(mm) != SUID_DUMP_USER) {
1654 struct user_namespace *user_ns = mm->user_ns;
1655
1656 uid = make_kuid(user_ns, 0);
1657 if (!uid_valid(uid))
1658 uid = GLOBAL_ROOT_UID;
1659
1660 gid = make_kgid(user_ns, 0);
1661 if (!gid_valid(gid))
1662 gid = GLOBAL_ROOT_GID;
1663 }
1664 } else {
1665 uid = GLOBAL_ROOT_UID;
1666 gid = GLOBAL_ROOT_GID;
1667 }
1668 task_unlock(task);
1669 }
1670 *ruid = uid;
1671 *rgid = gid;
1672}
1673
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001674struct inode *proc_pid_make_inode(struct super_block * sb,
1675 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001676{
1677 struct inode * inode;
1678 struct proc_inode *ei;
1679
1680 /* We need a new inode */
1681
1682 inode = new_inode(sb);
1683 if (!inode)
1684 goto out;
1685
1686 /* Common stuff */
1687 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001688 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001689 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001690 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001691 inode->i_op = &proc_def_inode_operations;
1692
1693 /*
1694 * grab the reference to task.
1695 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001696 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001697 if (!ei->pid)
1698 goto out_unlock;
1699
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001700 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001701 security_task_to_inode(task, inode);
1702
1703out:
1704 return inode;
1705
1706out_unlock:
1707 iput(inode);
1708 return NULL;
1709}
1710
David Howellsa528d352017-01-31 16:46:22 +00001711int pid_getattr(const struct path *path, struct kstat *stat,
1712 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001713{
David Howellsa528d352017-01-31 16:46:22 +00001714 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001715 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001716 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001717
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001718 generic_fillattr(inode, stat);
1719
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001720 stat->uid = GLOBAL_ROOT_UID;
1721 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001722 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001723 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1724 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001725 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001726 rcu_read_unlock();
1727 /*
1728 * This doesn't prevent learning whether PID exists,
1729 * it only makes getattr() consistent with readdir().
1730 */
1731 return -ENOENT;
1732 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001733 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001734 }
1735 rcu_read_unlock();
1736 return 0;
1737}
1738
1739/* dentry stuff */
1740
1741/*
Al Viro1bbc5512018-05-02 21:26:16 -04001742 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1743 */
1744void pid_update_inode(struct task_struct *task, struct inode *inode)
1745{
1746 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1747
1748 inode->i_mode &= ~(S_ISUID | S_ISGID);
1749 security_task_to_inode(task, inode);
1750}
1751
1752/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001753 * Rewrite the inode's ownerships here because the owning task may have
1754 * performed a setuid(), etc.
1755 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001756 */
Al Viro1bbc5512018-05-02 21:26:16 -04001757static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001758{
Nick Piggin34286d62011-01-07 17:49:57 +11001759 struct inode *inode;
1760 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001761
Al Viro0b728e12012-06-10 16:03:43 -04001762 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001763 return -ECHILD;
1764
David Howells2b0143b2015-03-17 22:25:59 +00001765 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001766 task = get_proc_task(inode);
1767
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001769 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001770 put_task_struct(task);
1771 return 1;
1772 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001773 return 0;
1774}
1775
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001776static inline bool proc_inode_is_dead(struct inode *inode)
1777{
1778 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1779}
1780
David Howells1dd704b2013-04-12 01:08:50 +01001781int pid_delete_dentry(const struct dentry *dentry)
1782{
1783 /* Is the task we represent dead?
1784 * If so, then don't put the dentry on the lru list,
1785 * kill it immediately.
1786 */
David Howells2b0143b2015-03-17 22:25:59 +00001787 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001788}
1789
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001790const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001791{
1792 .d_revalidate = pid_revalidate,
1793 .d_delete = pid_delete_dentry,
1794};
1795
1796/* Lookups */
1797
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001798/*
1799 * Fill a directory entry.
1800 *
1801 * If possible create the dcache entry and derive our inode number and
1802 * file type from dcache entry.
1803 *
1804 * Since all of the proc inode numbers are dynamically generated, the inode
1805 * numbers do not exist until the inode is cache. This means creating the
1806 * the dcache entry in readdir is necessary to keep the inode numbers
1807 * reported by readdir in sync with the inode numbers reported
1808 * by stat.
1809 */
Al Virof0c3b502013-05-16 12:07:31 -04001810bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07001811 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001812 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001813{
Al Virof0c3b502013-05-16 12:07:31 -04001814 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001815 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001816 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001817 unsigned type = DT_UNKNOWN;
1818 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001819
Al Viro1df98b82013-06-15 11:33:10 +04001820 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001821 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001822 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1823 child = d_alloc_parallel(dir, &qname, &wq);
1824 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001825 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001826 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001827 struct dentry *res;
1828 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001829 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001830 if (unlikely(res)) {
1831 dput(child);
1832 child = res;
Al Virod85b3992018-06-08 01:17:11 -04001833 if (IS_ERR(child))
1834 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001835 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001836 }
1837 }
David Howells2b0143b2015-03-17 22:25:59 +00001838 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001839 ino = inode->i_ino;
1840 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001841 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04001842end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04001843 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001844}
1845
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001846/*
1847 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1848 * which represent vma start and end addresses.
1849 */
1850static int dname_to_vma_addr(struct dentry *dentry,
1851 unsigned long *start, unsigned long *end)
1852{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001853 const char *str = dentry->d_name.name;
1854 unsigned long long sval, eval;
1855 unsigned int len;
1856
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001857 if (str[0] == '0' && str[1] != '-')
1858 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001859 len = _parse_integer(str, 16, &sval);
1860 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001861 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001862 if (sval != (unsigned long)sval)
1863 return -EINVAL;
1864 str += len;
1865
1866 if (*str != '-')
1867 return -EINVAL;
1868 str++;
1869
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001870 if (str[0] == '0' && str[1])
1871 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001872 len = _parse_integer(str, 16, &eval);
1873 if (len & KSTRTOX_OVERFLOW)
1874 return -EINVAL;
1875 if (eval != (unsigned long)eval)
1876 return -EINVAL;
1877 str += len;
1878
1879 if (*str != '\0')
1880 return -EINVAL;
1881
1882 *start = sval;
1883 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001884
1885 return 0;
1886}
1887
Al Viro0b728e12012-06-10 16:03:43 -04001888static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001889{
1890 unsigned long vm_start, vm_end;
1891 bool exact_vma_exists = false;
1892 struct mm_struct *mm = NULL;
1893 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001894 struct inode *inode;
1895 int status = 0;
1896
Al Viro0b728e12012-06-10 16:03:43 -04001897 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001898 return -ECHILD;
1899
David Howells2b0143b2015-03-17 22:25:59 +00001900 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001901 task = get_proc_task(inode);
1902 if (!task)
1903 goto out_notask;
1904
Jann Horncaaee622016-01-20 15:00:04 -08001905 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001906 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001907 goto out;
1908
1909 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1910 down_read(&mm->mmap_sem);
1911 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1912 up_read(&mm->mmap_sem);
1913 }
1914
1915 mmput(mm);
1916
1917 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001918 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1919
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001920 security_task_to_inode(task, inode);
1921 status = 1;
1922 }
1923
1924out:
1925 put_task_struct(task);
1926
1927out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001928 return status;
1929}
1930
1931static const struct dentry_operations tid_map_files_dentry_operations = {
1932 .d_revalidate = map_files_d_revalidate,
1933 .d_delete = pid_delete_dentry,
1934};
1935
Al Viro6b255392015-11-17 10:20:54 -05001936static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001937{
1938 unsigned long vm_start, vm_end;
1939 struct vm_area_struct *vma;
1940 struct task_struct *task;
1941 struct mm_struct *mm;
1942 int rc;
1943
1944 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001945 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001946 if (!task)
1947 goto out;
1948
1949 mm = get_task_mm(task);
1950 put_task_struct(task);
1951 if (!mm)
1952 goto out;
1953
1954 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1955 if (rc)
1956 goto out_mmput;
1957
Artem Fetishev70335ab2014-03-10 15:49:45 -07001958 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001959 down_read(&mm->mmap_sem);
1960 vma = find_exact_vma(mm, vm_start, vm_end);
1961 if (vma && vma->vm_file) {
1962 *path = vma->vm_file->f_path;
1963 path_get(path);
1964 rc = 0;
1965 }
1966 up_read(&mm->mmap_sem);
1967
1968out_mmput:
1969 mmput(mm);
1970out:
1971 return rc;
1972}
1973
1974struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08001975 unsigned long start;
1976 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04001977 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001978};
1979
Calvin Owensbdb4d102015-09-09 15:35:54 -07001980/*
1981 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1982 * symlinks may be used to bypass permissions on ancestor directories in the
1983 * path to the file in question.
1984 */
1985static const char *
Al Viro6b255392015-11-17 10:20:54 -05001986proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001987 struct inode *inode,
1988 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07001989{
1990 if (!capable(CAP_SYS_ADMIN))
1991 return ERR_PTR(-EPERM);
1992
Al Virofceef392015-12-29 15:58:39 -05001993 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07001994}
1995
1996/*
Al Viro6b255392015-11-17 10:20:54 -05001997 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07001998 */
1999static const struct inode_operations proc_map_files_link_inode_operations = {
2000 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002001 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002002 .setattr = proc_setattr,
2003};
2004
Al Viro0168b9e2018-05-03 09:21:05 -04002005static struct dentry *
2006proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002007 struct task_struct *task, const void *ptr)
2008{
Al Viro7b540d02012-08-27 14:55:26 -04002009 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002010 struct proc_inode *ei;
2011 struct inode *inode;
2012
Al Viro0168b9e2018-05-03 09:21:05 -04002013 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002014 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2015 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002016 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002017 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002018
2019 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002020 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002021
Calvin Owensbdb4d102015-09-09 15:35:54 -07002022 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002023 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002024
2025 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002026 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002027}
2028
2029static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002030 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002031{
2032 unsigned long vm_start, vm_end;
2033 struct vm_area_struct *vma;
2034 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002035 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002036 struct mm_struct *mm;
2037
Al Viro0168b9e2018-05-03 09:21:05 -04002038 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002039 task = get_proc_task(dir);
2040 if (!task)
2041 goto out;
2042
Al Viro0168b9e2018-05-03 09:21:05 -04002043 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002044 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002045 goto out_put_task;
2046
Al Viro0168b9e2018-05-03 09:21:05 -04002047 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002048 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002049 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002050
2051 mm = get_task_mm(task);
2052 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002053 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002054
2055 down_read(&mm->mmap_sem);
2056 vma = find_exact_vma(mm, vm_start, vm_end);
2057 if (!vma)
2058 goto out_no_vma;
2059
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002060 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002061 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002062 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002063
2064out_no_vma:
2065 up_read(&mm->mmap_sem);
2066 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002067out_put_task:
2068 put_task_struct(task);
2069out:
Al Viro0168b9e2018-05-03 09:21:05 -04002070 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002071}
2072
2073static const struct inode_operations proc_map_files_inode_operations = {
2074 .lookup = proc_map_files_lookup,
2075 .permission = proc_fd_permission,
2076 .setattr = proc_setattr,
2077};
2078
2079static int
Al Virof0c3b502013-05-16 12:07:31 -04002080proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002082 struct vm_area_struct *vma;
2083 struct task_struct *task;
2084 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002085 unsigned long nr_files, pos, i;
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002086 GENRADIX(struct map_files_info) fa;
Al Virof0c3b502013-05-16 12:07:31 -04002087 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002088 int ret;
2089
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002090 genradix_init(&fa);
2091
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002092 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002093 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002094 if (!task)
2095 goto out;
2096
2097 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002098 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002099 goto out_put_task;
2100
2101 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002102 if (!dir_emit_dots(file, ctx))
2103 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002104
Al Virof0c3b502013-05-16 12:07:31 -04002105 mm = get_task_mm(task);
2106 if (!mm)
2107 goto out_put_task;
2108 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002109
Al Virof0c3b502013-05-16 12:07:31 -04002110 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002111
Al Virof0c3b502013-05-16 12:07:31 -04002112 /*
2113 * We need two passes here:
2114 *
2115 * 1) Collect vmas of mapped files with mmap_sem taken
2116 * 2) Release mmap_sem and instantiate entries
2117 *
2118 * otherwise we get lockdep complained, since filldir()
2119 * routine might require mmap_sem taken in might_fault().
2120 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002121
Al Virof0c3b502013-05-16 12:07:31 -04002122 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002123 if (!vma->vm_file)
2124 continue;
2125 if (++pos <= ctx->pos)
2126 continue;
Al Virof0c3b502013-05-16 12:07:31 -04002127
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002128 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2129 if (!p) {
Al Virof0c3b502013-05-16 12:07:31 -04002130 ret = -ENOMEM;
Al Virof0c3b502013-05-16 12:07:31 -04002131 up_read(&mm->mmap_sem);
2132 mmput(mm);
2133 goto out_put_task;
2134 }
Al Virof0c3b502013-05-16 12:07:31 -04002135
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002136 p->start = vma->vm_start;
2137 p->end = vma->vm_end;
2138 p->mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002139 }
Al Virof0c3b502013-05-16 12:07:31 -04002140 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002141 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002142
2143 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002144 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2145 unsigned int len;
2146
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002147 p = genradix_ptr(&fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002148 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002149 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002150 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002151 proc_map_files_instantiate,
2152 task,
2153 (void *)(unsigned long)p->mode))
2154 break;
2155 ctx->pos++;
2156 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002157
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002158out_put_task:
2159 put_task_struct(task);
2160out:
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002161 genradix_free(&fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002162 return ret;
2163}
2164
2165static const struct file_operations proc_map_files_operations = {
2166 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002167 .iterate_shared = proc_map_files_readdir,
2168 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002169};
2170
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002171#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002172struct timers_private {
2173 struct pid *pid;
2174 struct task_struct *task;
2175 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002176 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002177 unsigned long flags;
2178};
2179
2180static void *timers_start(struct seq_file *m, loff_t *pos)
2181{
2182 struct timers_private *tp = m->private;
2183
2184 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2185 if (!tp->task)
2186 return ERR_PTR(-ESRCH);
2187
2188 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2189 if (!tp->sighand)
2190 return ERR_PTR(-ESRCH);
2191
2192 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2193}
2194
2195static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2196{
2197 struct timers_private *tp = m->private;
2198 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2199}
2200
2201static void timers_stop(struct seq_file *m, void *v)
2202{
2203 struct timers_private *tp = m->private;
2204
2205 if (tp->sighand) {
2206 unlock_task_sighand(tp->task, &tp->flags);
2207 tp->sighand = NULL;
2208 }
2209
2210 if (tp->task) {
2211 put_task_struct(tp->task);
2212 tp->task = NULL;
2213 }
2214}
2215
2216static int show_timer(struct seq_file *m, void *v)
2217{
2218 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002219 struct timers_private *tp = m->private;
2220 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002221 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002222 [SIGEV_SIGNAL] = "signal",
2223 [SIGEV_NONE] = "none",
2224 [SIGEV_THREAD] = "thread",
2225 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002226
2227 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002228 notify = timer->it_sigev_notify;
2229
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002230 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002231 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002232 timer->sigq->info.si_signo,
2233 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002234 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002235 nstr[notify & ~SIGEV_THREAD_ID],
2236 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2237 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002238 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002239
2240 return 0;
2241}
2242
2243static const struct seq_operations proc_timers_seq_ops = {
2244 .start = timers_start,
2245 .next = timers_next,
2246 .stop = timers_stop,
2247 .show = show_timer,
2248};
2249
2250static int proc_timers_open(struct inode *inode, struct file *file)
2251{
2252 struct timers_private *tp;
2253
2254 tp = __seq_open_private(file, &proc_timers_seq_ops,
2255 sizeof(struct timers_private));
2256 if (!tp)
2257 return -ENOMEM;
2258
2259 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002260 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002261 return 0;
2262}
2263
2264static const struct file_operations proc_timers_operations = {
2265 .open = proc_timers_open,
2266 .read = seq_read,
2267 .llseek = seq_lseek,
2268 .release = seq_release_private,
2269};
Eric Engestromb5946be2016-03-17 14:20:57 -07002270#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002271
John Stultz5de23d42016-03-17 14:20:54 -07002272static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2273 size_t count, loff_t *offset)
2274{
2275 struct inode *inode = file_inode(file);
2276 struct task_struct *p;
2277 u64 slack_ns;
2278 int err;
2279
2280 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2281 if (err < 0)
2282 return err;
2283
2284 p = get_proc_task(inode);
2285 if (!p)
2286 return -ESRCH;
2287
John Stultz4b2bd5f2016-10-07 17:02:33 -07002288 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002289 rcu_read_lock();
2290 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2291 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002292 count = -EPERM;
2293 goto out;
2294 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002295 rcu_read_unlock();
John Stultz5de23d42016-03-17 14:20:54 -07002296
John Stultz4b2bd5f2016-10-07 17:02:33 -07002297 err = security_task_setscheduler(p);
2298 if (err) {
2299 count = err;
2300 goto out;
2301 }
John Stultz904763e2016-10-07 17:02:29 -07002302 }
2303
John Stultz7abbaf92016-10-07 17:02:26 -07002304 task_lock(p);
2305 if (slack_ns == 0)
2306 p->timer_slack_ns = p->default_timer_slack_ns;
2307 else
2308 p->timer_slack_ns = slack_ns;
2309 task_unlock(p);
2310
2311out:
John Stultz5de23d42016-03-17 14:20:54 -07002312 put_task_struct(p);
2313
2314 return count;
2315}
2316
2317static int timerslack_ns_show(struct seq_file *m, void *v)
2318{
2319 struct inode *inode = m->private;
2320 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002321 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002322
2323 p = get_proc_task(inode);
2324 if (!p)
2325 return -ESRCH;
2326
John Stultz4b2bd5f2016-10-07 17:02:33 -07002327 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002328 rcu_read_lock();
2329 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2330 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002331 err = -EPERM;
2332 goto out;
2333 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002334 rcu_read_unlock();
2335
John Stultz4b2bd5f2016-10-07 17:02:33 -07002336 err = security_task_getscheduler(p);
2337 if (err)
2338 goto out;
2339 }
John Stultz904763e2016-10-07 17:02:29 -07002340
John Stultz7abbaf92016-10-07 17:02:26 -07002341 task_lock(p);
2342 seq_printf(m, "%llu\n", p->timer_slack_ns);
2343 task_unlock(p);
2344
2345out:
John Stultz5de23d42016-03-17 14:20:54 -07002346 put_task_struct(p);
2347
2348 return err;
2349}
2350
2351static int timerslack_ns_open(struct inode *inode, struct file *filp)
2352{
2353 return single_open(filp, timerslack_ns_show, inode);
2354}
2355
2356static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2357 .open = timerslack_ns_open,
2358 .read = seq_read,
2359 .write = timerslack_ns_write,
2360 .llseek = seq_lseek,
2361 .release = single_release,
2362};
2363
Al Viro0168b9e2018-05-03 09:21:05 -04002364static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2365 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002366{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002367 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002368 struct inode *inode;
2369 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002370
Al Viro0168b9e2018-05-03 09:21:05 -04002371 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002372 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002373 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002374
2375 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002376 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002377 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002378 if (p->iop)
2379 inode->i_op = p->iop;
2380 if (p->fop)
2381 inode->i_fop = p->fop;
2382 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002383 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002384 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002385 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002386}
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388static struct dentry *proc_pident_lookup(struct inode *dir,
2389 struct dentry *dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002390 const struct pid_entry *p,
2391 const struct pid_entry *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002393 struct task_struct *task = get_proc_task(dir);
Al Viro0168b9e2018-05-03 09:21:05 -04002394 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Eric W. Biederman99f89552006-06-26 00:25:55 -07002396 if (!task)
2397 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002399 /*
2400 * Yes, it does not scale. And it should not. Don't add
2401 * new entries into /proc/<tgid>/ without very good reasons.
2402 */
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002403 for (; p < end; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (p->len != dentry->d_name.len)
2405 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002406 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2407 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002411 put_task_struct(task);
2412out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002413 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
Al Virof0c3b502013-05-16 12:07:31 -04002416static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002417 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002418{
Al Virof0c3b502013-05-16 12:07:31 -04002419 struct task_struct *task = get_proc_task(file_inode(file));
2420 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002421
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002422 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002423 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002424
Al Virof0c3b502013-05-16 12:07:31 -04002425 if (!dir_emit_dots(file, ctx))
2426 goto out;
2427
2428 if (ctx->pos >= nents + 2)
2429 goto out;
2430
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002431 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002432 if (!proc_fill_cache(file, ctx, p->name, p->len,
2433 proc_pident_instantiate, task, p))
2434 break;
2435 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002436 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002437out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002438 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002443static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2444 size_t count, loff_t *ppos)
2445{
Al Viro496ad9a2013-01-23 17:07:38 -05002446 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002447 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002448 ssize_t length;
2449 struct task_struct *task = get_proc_task(inode);
2450
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002451 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002452 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002453
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002454 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002455 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002456 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002457 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002458 if (length > 0)
2459 length = simple_read_from_buffer(buf, count, ppos, p, length);
2460 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002461 return length;
2462}
2463
2464static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2465 size_t count, loff_t *ppos)
2466{
Al Viro496ad9a2013-01-23 17:07:38 -05002467 struct inode * inode = file_inode(file);
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002468 struct task_struct *task;
Al Virobb646cd2015-12-24 00:16:30 -05002469 void *page;
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002470 int rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002471
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002472 rcu_read_lock();
2473 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2474 if (!task) {
2475 rcu_read_unlock();
2476 return -ESRCH;
2477 }
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002478 /* A task may only write its own attributes. */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002479 if (current != task) {
2480 rcu_read_unlock();
2481 return -EACCES;
2482 }
Paul Moore35a196b2019-04-19 14:55:12 -04002483 /* Prevent changes to overridden credentials. */
2484 if (current_cred() != current_real_cred()) {
2485 rcu_read_unlock();
2486 return -EBUSY;
2487 }
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002488 rcu_read_unlock();
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002489
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490 if (count > PAGE_SIZE)
2491 count = PAGE_SIZE;
2492
2493 /* No partial writes. */
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002494 if (*ppos != 0)
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002495 return -EINVAL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496
Al Virobb646cd2015-12-24 00:16:30 -05002497 page = memdup_user(buf, count);
2498 if (IS_ERR(page)) {
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002499 rv = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002500 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002501 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502
David Howells107db7c2009-05-08 13:55:27 +01002503 /* Guard against adverse ptrace interaction */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002504 rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2505 if (rv < 0)
David Howells107db7c2009-05-08 13:55:27 +01002506 goto out_free;
2507
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002508 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2509 file->f_path.dentry->d_name.name, page,
2510 count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002511 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002512out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002513 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514out:
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002515 return rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002516}
2517
Arjan van de Ven00977a52007-02-12 00:55:34 -08002518static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002519 .read = proc_pid_attr_read,
2520 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002521 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522};
2523
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002524#define LSM_DIR_OPS(LSM) \
2525static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2526 struct dir_context *ctx) \
2527{ \
2528 return proc_pident_readdir(filp, ctx, \
2529 LSM##_attr_dir_stuff, \
2530 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2531} \
2532\
2533static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2534 .read = generic_read_dir, \
2535 .iterate = proc_##LSM##_attr_dir_iterate, \
2536 .llseek = default_llseek, \
2537}; \
2538\
2539static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2540 struct dentry *dentry, unsigned int flags) \
2541{ \
2542 return proc_pident_lookup(dir, dentry, \
2543 LSM##_attr_dir_stuff, \
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002544 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002545} \
2546\
2547static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2548 .lookup = proc_##LSM##_attr_dir_lookup, \
2549 .getattr = pid_getattr, \
2550 .setattr = proc_setattr, \
2551}
2552
2553#ifdef CONFIG_SECURITY_SMACK
2554static const struct pid_entry smack_attr_dir_stuff[] = {
2555 ATTR("smack", "current", 0666),
2556};
2557LSM_DIR_OPS(smack);
2558#endif
2559
Eric Dumazetc5141e62007-05-08 00:26:15 -07002560static const struct pid_entry attr_dir_stuff[] = {
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002561 ATTR(NULL, "current", 0666),
2562 ATTR(NULL, "prev", 0444),
2563 ATTR(NULL, "exec", 0666),
2564 ATTR(NULL, "fscreate", 0666),
2565 ATTR(NULL, "keycreate", 0666),
2566 ATTR(NULL, "sockcreate", 0666),
2567#ifdef CONFIG_SECURITY_SMACK
2568 DIR("smack", 0555,
2569 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2570#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002571};
2572
Al Virof0c3b502013-05-16 12:07:31 -04002573static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
Al Virof0c3b502013-05-16 12:07:31 -04002575 return proc_pident_readdir(file, ctx,
2576 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
Arjan van de Ven00977a52007-02-12 00:55:34 -08002579static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002581 .iterate_shared = proc_attr_dir_readdir,
2582 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583};
2584
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002585static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002586 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002588 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002589 attr_dir_stuff,
2590 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002593static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002594 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002595 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002596 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597};
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599#endif
2600
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002601#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002602static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2603 size_t count, loff_t *ppos)
2604{
Al Viro496ad9a2013-01-23 17:07:38 -05002605 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002606 struct mm_struct *mm;
2607 char buffer[PROC_NUMBUF];
2608 size_t len;
2609 int ret;
2610
2611 if (!task)
2612 return -ESRCH;
2613
2614 ret = 0;
2615 mm = get_task_mm(task);
2616 if (mm) {
2617 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2618 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2619 MMF_DUMP_FILTER_SHIFT));
2620 mmput(mm);
2621 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2622 }
2623
2624 put_task_struct(task);
2625
2626 return ret;
2627}
2628
2629static ssize_t proc_coredump_filter_write(struct file *file,
2630 const char __user *buf,
2631 size_t count,
2632 loff_t *ppos)
2633{
2634 struct task_struct *task;
2635 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002636 unsigned int val;
2637 int ret;
2638 int i;
2639 unsigned long mask;
2640
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002641 ret = kstrtouint_from_user(buf, count, 0, &val);
2642 if (ret < 0)
2643 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002644
2645 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002646 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002647 if (!task)
2648 goto out_no_task;
2649
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002650 mm = get_task_mm(task);
2651 if (!mm)
2652 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002653 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002654
2655 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2656 if (val & mask)
2657 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2658 else
2659 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2660 }
2661
2662 mmput(mm);
2663 out_no_mm:
2664 put_task_struct(task);
2665 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002666 if (ret < 0)
2667 return ret;
2668 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002669}
2670
2671static const struct file_operations proc_coredump_filter_operations = {
2672 .read = proc_coredump_filter_read,
2673 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002674 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002675};
2676#endif
2677
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002678#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002679static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002680{
Andrea Righi940389b2008-07-28 00:48:12 +02002681 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002682 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002683 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002684
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002685 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2686 if (result)
2687 return result;
2688
Jann Horncaaee622016-01-20 15:00:04 -08002689 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002690 result = -EACCES;
2691 goto out_unlock;
2692 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002693
Andrea Righi59954772008-07-27 17:29:15 +02002694 if (whole && lock_task_sighand(task, &flags)) {
2695 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002696
Andrea Righi59954772008-07-27 17:29:15 +02002697 task_io_accounting_add(&acct, &task->signal->ioac);
2698 while_each_thread(task, t)
2699 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002700
Andrea Righi59954772008-07-27 17:29:15 +02002701 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002702 }
Joe Perches25ce3192015-04-15 16:18:17 -07002703 seq_printf(m,
2704 "rchar: %llu\n"
2705 "wchar: %llu\n"
2706 "syscr: %llu\n"
2707 "syscw: %llu\n"
2708 "read_bytes: %llu\n"
2709 "write_bytes: %llu\n"
2710 "cancelled_write_bytes: %llu\n",
2711 (unsigned long long)acct.rchar,
2712 (unsigned long long)acct.wchar,
2713 (unsigned long long)acct.syscr,
2714 (unsigned long long)acct.syscw,
2715 (unsigned long long)acct.read_bytes,
2716 (unsigned long long)acct.write_bytes,
2717 (unsigned long long)acct.cancelled_write_bytes);
2718 result = 0;
2719
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002720out_unlock:
2721 mutex_unlock(&task->signal->cred_guard_mutex);
2722 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002723}
Andrea Righi297c5d92008-07-25 01:48:49 -07002724
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002725static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2726 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002727{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002728 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002729}
2730
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002731static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2732 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002733{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002734 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002735}
2736#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002737
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002738#ifdef CONFIG_USER_NS
2739static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002740 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002741{
2742 struct user_namespace *ns = NULL;
2743 struct task_struct *task;
2744 struct seq_file *seq;
2745 int ret = -EINVAL;
2746
2747 task = get_proc_task(inode);
2748 if (task) {
2749 rcu_read_lock();
2750 ns = get_user_ns(task_cred_xxx(task, user_ns));
2751 rcu_read_unlock();
2752 put_task_struct(task);
2753 }
2754 if (!ns)
2755 goto err;
2756
2757 ret = seq_open(file, seq_ops);
2758 if (ret)
2759 goto err_put_ns;
2760
2761 seq = file->private_data;
2762 seq->private = ns;
2763
2764 return 0;
2765err_put_ns:
2766 put_user_ns(ns);
2767err:
2768 return ret;
2769}
2770
2771static int proc_id_map_release(struct inode *inode, struct file *file)
2772{
2773 struct seq_file *seq = file->private_data;
2774 struct user_namespace *ns = seq->private;
2775 put_user_ns(ns);
2776 return seq_release(inode, file);
2777}
2778
2779static int proc_uid_map_open(struct inode *inode, struct file *file)
2780{
2781 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2782}
2783
2784static int proc_gid_map_open(struct inode *inode, struct file *file)
2785{
2786 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2787}
2788
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002789static int proc_projid_map_open(struct inode *inode, struct file *file)
2790{
2791 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2792}
2793
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002794static const struct file_operations proc_uid_map_operations = {
2795 .open = proc_uid_map_open,
2796 .write = proc_uid_map_write,
2797 .read = seq_read,
2798 .llseek = seq_lseek,
2799 .release = proc_id_map_release,
2800};
2801
2802static const struct file_operations proc_gid_map_operations = {
2803 .open = proc_gid_map_open,
2804 .write = proc_gid_map_write,
2805 .read = seq_read,
2806 .llseek = seq_lseek,
2807 .release = proc_id_map_release,
2808};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002809
2810static const struct file_operations proc_projid_map_operations = {
2811 .open = proc_projid_map_open,
2812 .write = proc_projid_map_write,
2813 .read = seq_read,
2814 .llseek = seq_lseek,
2815 .release = proc_id_map_release,
2816};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002817
2818static int proc_setgroups_open(struct inode *inode, struct file *file)
2819{
2820 struct user_namespace *ns = NULL;
2821 struct task_struct *task;
2822 int ret;
2823
2824 ret = -ESRCH;
2825 task = get_proc_task(inode);
2826 if (task) {
2827 rcu_read_lock();
2828 ns = get_user_ns(task_cred_xxx(task, user_ns));
2829 rcu_read_unlock();
2830 put_task_struct(task);
2831 }
2832 if (!ns)
2833 goto err;
2834
2835 if (file->f_mode & FMODE_WRITE) {
2836 ret = -EACCES;
2837 if (!ns_capable(ns, CAP_SYS_ADMIN))
2838 goto err_put_ns;
2839 }
2840
2841 ret = single_open(file, &proc_setgroups_show, ns);
2842 if (ret)
2843 goto err_put_ns;
2844
2845 return 0;
2846err_put_ns:
2847 put_user_ns(ns);
2848err:
2849 return ret;
2850}
2851
2852static int proc_setgroups_release(struct inode *inode, struct file *file)
2853{
2854 struct seq_file *seq = file->private_data;
2855 struct user_namespace *ns = seq->private;
2856 int ret = single_release(inode, file);
2857 put_user_ns(ns);
2858 return ret;
2859}
2860
2861static const struct file_operations proc_setgroups_operations = {
2862 .open = proc_setgroups_open,
2863 .write = proc_setgroups_write,
2864 .read = seq_read,
2865 .llseek = seq_lseek,
2866 .release = proc_setgroups_release,
2867};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002868#endif /* CONFIG_USER_NS */
2869
Kees Cook47830722008-10-06 03:11:58 +04002870static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2871 struct pid *pid, struct task_struct *task)
2872{
Al Viroa9712bc2011-03-23 15:52:50 -04002873 int err = lock_trace(task);
2874 if (!err) {
2875 seq_printf(m, "%08x\n", task->personality);
2876 unlock_trace(task);
2877 }
2878 return err;
Kees Cook47830722008-10-06 03:11:58 +04002879}
2880
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002881#ifdef CONFIG_LIVEPATCH
2882static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2883 struct pid *pid, struct task_struct *task)
2884{
2885 seq_printf(m, "%d\n", task->patch_state);
2886 return 0;
2887}
2888#endif /* CONFIG_LIVEPATCH */
2889
Alexander Popovc8d12622018-08-17 01:17:01 +03002890#ifdef CONFIG_STACKLEAK_METRICS
2891static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2892 struct pid *pid, struct task_struct *task)
2893{
2894 unsigned long prev_depth = THREAD_SIZE -
2895 (task->prev_lowest_stack & (THREAD_SIZE - 1));
2896 unsigned long depth = THREAD_SIZE -
2897 (task->lowest_stack & (THREAD_SIZE - 1));
2898
2899 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2900 prev_depth, depth);
2901 return 0;
2902}
2903#endif /* CONFIG_STACKLEAK_METRICS */
2904
Eric W. Biederman801199c2006-10-02 02:18:48 -07002905/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002906 * Thread groups
2907 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002908static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002909static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002910
Eric Dumazetc5141e62007-05-08 00:26:15 -07002911static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002912 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2913 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002914 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002915 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002916 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002917#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002918 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002919#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002920 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002921 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002922 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002923 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002924 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002925#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002926 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002927#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002928#ifdef CONFIG_SCHED_AUTOGROUP
2929 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2930#endif
john stultz4614a696b2009-12-14 18:00:05 -08002931 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002932#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002933 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002934#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002935 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002936 ONE("stat", S_IRUGO, proc_tgid_stat),
2937 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002938 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002940 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002941#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002942 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2943 LNK("cwd", proc_cwd_link),
2944 LNK("root", proc_root_link),
2945 LNK("exe", proc_exe_link),
2946 REG("mounts", S_IRUGO, proc_mounts_operations),
2947 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2948 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002949#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002950 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002951 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002952 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002953 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954#endif
2955#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002956 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002957#endif
2958#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002959 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002960#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002961#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002962 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002963#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302964#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002965 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002966#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002967#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002968 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002969#endif
Paul Menage8793d852007-10-18 23:39:39 -07002970#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002971 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002972#endif
Paul Menagea4243162007-10-18 23:39:35 -07002973#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002974 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002975#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002976 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002977 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002978 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05002979#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002980 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2981 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002983#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002984 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002985 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002986#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002987#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002988 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002989#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002990#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002991 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002992#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002993#ifdef CONFIG_USER_NS
2994 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2995 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002996 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002997 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002998#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002999#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003000 REG("timers", S_IRUGO, proc_timers_operations),
3001#endif
John Stultz5de23d42016-03-17 14:20:54 -07003002 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003003#ifdef CONFIG_LIVEPATCH
3004 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3005#endif
Alexander Popovc8d12622018-08-17 01:17:01 +03003006#ifdef CONFIG_STACKLEAK_METRICS
3007 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3008#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009};
3010
Al Virof0c3b502013-05-16 12:07:31 -04003011static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012{
Al Virof0c3b502013-05-16 12:07:31 -04003013 return proc_pident_readdir(file, ctx,
3014 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015}
3016
Arjan van de Ven00977a52007-02-12 00:55:34 -08003017static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003019 .iterate_shared = proc_tgid_base_readdir,
3020 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003021};
3022
Christian Brauner3eb39f42018-11-19 00:51:56 +01003023struct pid *tgid_pidfd_to_pid(const struct file *file)
3024{
Christian Brauner30d158b2019-06-27 11:35:14 +02003025 if (file->f_op != &proc_tgid_base_operations)
Christian Brauner3eb39f42018-11-19 00:51:56 +01003026 return ERR_PTR(-EBADF);
3027
3028 return proc_pid(file_inode(file));
3029}
3030
Al Viro00cd8dd2012-06-10 17:13:09 -04003031static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3032{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003033 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003034 tgid_base_stuff,
3035 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003036}
3037
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003038static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003039 .lookup = proc_tgid_base_lookup,
3040 .getattr = pid_getattr,
3041 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003042 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003043};
3044
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003045static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003047 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003048 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003049 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Eric W. Biederman48e64842006-06-26 00:25:48 -07003051 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003052 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003053 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003054 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003055 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003056 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003057 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003060 if (pid == tgid)
3061 return;
3062
Eric W. Biederman48e64842006-06-26 00:25:48 -07003063 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003064 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003065 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003066 if (!leader)
3067 goto out;
3068
3069 name.name = "task";
3070 name.len = strlen(name.name);
3071 dir = d_hash_and_lookup(leader, &name);
3072 if (!dir)
3073 goto out_put_leader;
3074
3075 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003076 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003077 dentry = d_hash_and_lookup(dir, &name);
3078 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003079 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003080 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003082
3083 dput(dir);
3084out_put_leader:
3085 dput(leader);
3086out:
3087 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Randy Dunlap0895e912007-10-21 21:00:10 -07003090/**
3091 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3092 * @task: task that should be flushed.
3093 *
3094 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003095 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003096 * in. This call is supposed to do all of this job.
3097 *
3098 * Looks in the dcache for
3099 * /proc/@pid
3100 * /proc/@tgid/task/@pid
3101 * if either directory is present flushes it and all of it'ts children
3102 * from the dcache.
3103 *
3104 * It is safe and reasonable to cache /proc entries for a task until
3105 * that task exits. After that they just clog up the dcache with
3106 * useless entries, possibly causing useful dcache entries to be
3107 * flushed instead. This routine is proved to flush those useless
3108 * dcache entries at process exit time.
3109 *
3110 * NOTE: This routine is just an optimization so it does not guarantee
3111 * that no dcache entries will exist at process exit time it
3112 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003113 */
3114
3115void proc_flush_task(struct task_struct *task)
3116{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003117 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003118 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003119 struct upid *upid;
3120
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003121 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003122 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003123
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003124 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003125 upid = &pid->numbers[i];
3126 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003127 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003128 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003129}
3130
Al Viro0168b9e2018-05-03 09:21:05 -04003131static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003132 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003133{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003134 struct inode *inode;
3135
Al Viro0168b9e2018-05-03 09:21:05 -04003136 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003137 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003138 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003139
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003140 inode->i_op = &proc_tgid_base_inode_operations;
3141 inode->i_fop = &proc_tgid_base_operations;
3142 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003143
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003144 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003145 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003146
Nick Pigginfb045ad2011-01-07 17:49:55 +11003147 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003148 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003149}
3150
Zhikang Zhang867aacc2019-03-05 15:50:29 -08003151struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003155 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003156 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003158 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (tgid == ~0U)
3160 goto out;
3161
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003162 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003163 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003164 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (task)
3166 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003167 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (!task)
3169 goto out;
3170
Al Viro0168b9e2018-05-03 09:21:05 -04003171 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003172 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173out:
Al Viro0168b9e2018-05-03 09:21:05 -04003174 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003178 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003179 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003181struct tgid_iter {
3182 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003183 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003184};
3185static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3186{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003187 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003189 if (iter.task)
3190 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003191 rcu_read_lock();
3192retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003193 iter.task = NULL;
3194 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003195 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003196 iter.tgid = pid_nr_ns(pid, ns);
3197 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003198 /* What we to know is if the pid we have find is the
3199 * pid of a thread_group_leader. Testing for task
3200 * being a thread_group_leader is the obvious thing
3201 * todo but there is a window when it fails, due to
3202 * the pid transfer logic in de_thread.
3203 *
3204 * So we perform the straight forward test of seeing
3205 * if the pid we have found is the pid of a thread
3206 * group leader, and don't worry if the task we have
3207 * found doesn't happen to be a thread group leader.
3208 * As we don't care in the case of readdir.
3209 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003210 if (!iter.task || !has_group_leader_pid(iter.task)) {
3211 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003212 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003213 }
3214 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003216 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003217 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Eric W. Biederman00978752014-07-31 03:10:50 -07003220#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003223int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003225 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003226 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003227 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Al Viro021ada72013-03-29 19:27:05 -04003229 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Eric W. Biederman00978752014-07-31 03:10:50 -07003232 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003233 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003234 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003235 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003236 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003237 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003238 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003239 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003240 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3241 return 0;
3242 ctx->pos = pos = pos + 1;
3243 }
3244 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003245 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003246 for (iter = next_tgid(ns, iter);
3247 iter.task;
3248 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003249 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003250 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003251
3252 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003253 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003254 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003255
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003256 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003257 ctx->pos = iter.tgid + TGID_OFFSET;
3258 if (!proc_fill_cache(file, ctx, name, len,
3259 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003260 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
Al Virof0c3b502013-05-16 12:07:31 -04003264 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return 0;
3266}
3267
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003268/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003269 * proc_tid_comm_permission is a special permission function exclusively
3270 * used for the node /proc/<pid>/task/<tid>/comm.
3271 * It bypasses generic permission checks in the case where a task of the same
3272 * task group attempts to access the node.
3273 * The rationale behind this is that glibc and bionic access this node for
3274 * cross thread naming (pthread_set/getname_np(!self)). However, if
3275 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3276 * which locks out the cross thread naming implementation.
3277 * This function makes sure that the node is always accessible for members of
3278 * same thread group.
3279 */
3280static int proc_tid_comm_permission(struct inode *inode, int mask)
3281{
3282 bool is_same_tgroup;
3283 struct task_struct *task;
3284
3285 task = get_proc_task(inode);
3286 if (!task)
3287 return -ESRCH;
3288 is_same_tgroup = same_thread_group(current, task);
3289 put_task_struct(task);
3290
3291 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3292 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3293 * read or written by the members of the corresponding
3294 * thread group.
3295 */
3296 return 0;
3297 }
3298
3299 return generic_permission(inode, mask);
3300}
3301
3302static const struct inode_operations proc_tid_comm_inode_operations = {
3303 .permission = proc_tid_comm_permission,
3304};
3305
3306/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003307 * Tasks
3308 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003309static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003310 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003311 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003312 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003313#ifdef CONFIG_NET
3314 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3315#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003316 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003317 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003318 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003319 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003320 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003321#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003322 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003323#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003324 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3325 &proc_tid_comm_inode_operations,
3326 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003327#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003328 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003329#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003330 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003331 ONE("stat", S_IRUGO, proc_tid_stat),
3332 ONE("statm", S_IRUGO, proc_pid_statm),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003333 REG("maps", S_IRUGO, proc_pid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003334#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003335 REG("children", S_IRUGO, proc_tid_children_operations),
3336#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003337#ifdef CONFIG_NUMA
Vlastimil Babka871305b2018-08-21 21:52:48 -07003338 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003339#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003340 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3341 LNK("cwd", proc_cwd_link),
3342 LNK("root", proc_root_link),
3343 LNK("exe", proc_exe_link),
3344 REG("mounts", S_IRUGO, proc_mounts_operations),
3345 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003346#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003347 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003348 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003349 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003350 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003351#endif
3352#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003353 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003354#endif
3355#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003356 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003357#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003358#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003359 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003360#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303361#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003362 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003363#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003364#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003365 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003366#endif
Paul Menage8793d852007-10-18 23:39:39 -07003367#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003368 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003369#endif
Paul Menagea4243162007-10-18 23:39:35 -07003370#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003371 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003372#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003373 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003374 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003375 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003376#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003377 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003378 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003379#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003380#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003381 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003382 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003383#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003384#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003385 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003386#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003387#ifdef CONFIG_USER_NS
3388 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3389 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003390 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003391 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003392#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003393#ifdef CONFIG_LIVEPATCH
3394 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3395#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003396};
3397
Al Virof0c3b502013-05-16 12:07:31 -04003398static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003399{
Al Virof0c3b502013-05-16 12:07:31 -04003400 return proc_pident_readdir(file, ctx,
3401 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003402}
3403
Al Viro00cd8dd2012-06-10 17:13:09 -04003404static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3405{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003406 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003407 tid_base_stuff,
3408 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003409}
3410
Arjan van de Ven00977a52007-02-12 00:55:34 -08003411static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003412 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003413 .iterate_shared = proc_tid_base_readdir,
3414 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003415};
3416
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003417static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003418 .lookup = proc_tid_base_lookup,
3419 .getattr = pid_getattr,
3420 .setattr = proc_setattr,
3421};
3422
Al Viro0168b9e2018-05-03 09:21:05 -04003423static struct dentry *proc_task_instantiate(struct dentry *dentry,
3424 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003425{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003426 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003427 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003428 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003429 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003430
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003431 inode->i_op = &proc_tid_base_inode_operations;
3432 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003433 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003434
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003435 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003436 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003437
Nick Pigginfb045ad2011-01-07 17:49:55 +11003438 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003439 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003440}
3441
Al Viro00cd8dd2012-06-10 17:13:09 -04003442static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003443{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003444 struct task_struct *task;
3445 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003446 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003447 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003448 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003449
3450 if (!leader)
3451 goto out_no_task;
3452
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003453 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003454 if (tid == ~0U)
3455 goto out;
3456
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003457 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003458 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003459 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003460 if (task)
3461 get_task_struct(task);
3462 rcu_read_unlock();
3463 if (!task)
3464 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003465 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003466 goto out_drop_task;
3467
Al Viro0168b9e2018-05-03 09:21:05 -04003468 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003469out_drop_task:
3470 put_task_struct(task);
3471out:
3472 put_task_struct(leader);
3473out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003474 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003475}
3476
3477/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003478 * Find the first tid of a thread group to return to user space.
3479 *
3480 * Usually this is just the thread group leader, but if the users
3481 * buffer was too small or there was a seek into the middle of the
3482 * directory we have more work todo.
3483 *
3484 * In the case of a short read we start with find_task_by_pid.
3485 *
3486 * In the case of a seek we start with the leader and walk nr
3487 * threads past it.
3488 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003489static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3490 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003491{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003492 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003493 unsigned long nr = f_pos;
3494
3495 if (nr != f_pos) /* 32bit overflow? */
3496 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003497
Eric W. Biedermancc288732006-06-26 00:26:01 -07003498 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003499 task = pid_task(pid, PIDTYPE_PID);
3500 if (!task)
3501 goto fail;
3502
3503 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003504 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003505 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003506 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003507 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003508 }
3509
3510 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003511 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003512 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003513
3514 /* If we haven't found our starting place yet start
3515 * with the leader and walk nr threads forward.
3516 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003517 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003518 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003519 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003520 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003521 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003522fail:
3523 pos = NULL;
3524 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003525found:
3526 get_task_struct(pos);
3527out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003528 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003529 return pos;
3530}
3531
3532/*
3533 * Find the next thread in the thread list.
3534 * Return NULL if there is an error or no next thread.
3535 *
3536 * The reference to the input task_struct is released.
3537 */
3538static struct task_struct *next_tid(struct task_struct *start)
3539{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003540 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003541 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003542 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003543 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003544 if (thread_group_leader(pos))
3545 pos = NULL;
3546 else
3547 get_task_struct(pos);
3548 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003549 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003550 put_task_struct(start);
3551 return pos;
3552}
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003555static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003557 struct inode *inode = file_inode(file);
3558 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003559 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003560 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003562 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003563 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Al Virof0c3b502013-05-16 12:07:31 -04003565 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003568 /* f_version caches the tgid value that the last readdir call couldn't
3569 * return. lseek aka telldir automagically resets f_version to 0.
3570 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003571 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003572 tid = (int)file->f_version;
3573 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003574 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003575 task;
Al Virof0c3b502013-05-16 12:07:31 -04003576 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003577 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003578 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003579 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003580 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003581 if (!proc_fill_cache(file, ctx, name, len,
3582 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003583 /* returning this tgid failed, save it as the first
3584 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003585 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003586 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003590
Al Virof0c3b502013-05-16 12:07:31 -04003591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003593
David Howellsa528d352017-01-31 16:46:22 +00003594static int proc_task_getattr(const struct path *path, struct kstat *stat,
3595 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003596{
David Howellsa528d352017-01-31 16:46:22 +00003597 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003598 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003599 generic_fillattr(inode, stat);
3600
Eric W. Biederman99f89552006-06-26 00:25:55 -07003601 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003602 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003603 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003604 }
3605
3606 return 0;
3607}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003608
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003609static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003610 .lookup = proc_task_lookup,
3611 .getattr = proc_task_getattr,
3612 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003613 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003614};
3615
Arjan van de Ven00977a52007-02-12 00:55:34 -08003616static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003617 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003618 .iterate_shared = proc_task_readdir,
3619 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003620};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003621
3622void __init set_proc_pid_nlink(void)
3623{
3624 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3625 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3626}