blob: b6572944efc340d89f136c5a9c17ac409c8bef00 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/string.h>
63#include <linux/seq_file.h>
64#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080065#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070067#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070068#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030070#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070071#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070072#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/mount.h>
74#include <linux/security.h>
75#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070076#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080077#include <linux/printk.h>
Alexey Dobriyanefb1a572018-02-06 15:37:24 -080078#include <linux/cache.h>
Paul Menagea4243162007-10-18 23:39:35 -070079#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/cpuset.h>
81#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050082#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070083#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070084#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070085#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070086#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080087#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040088#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090089#include <linux/slab.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010090#include <linux/sched/autogroup.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010091#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010092#include <linux/sched/coredump.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010093#include <linux/sched/debug.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010094#include <linux/sched/stat.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080095#include <linux/flex_array.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) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800143 NOD(NAME, (S_IFREG|(MODE)), \
144 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300145 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Vegard Nossumaed54172008-06-05 22:46:53 -0700147/*
148 * Count the number of hardlinks for the pid_entry table, excluding the .
149 * and .. links.
150 */
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800151static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
Vegard Nossumaed54172008-06-05 22:46:53 -0700152 unsigned int n)
153{
154 unsigned int i;
155 unsigned int count;
156
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800157 count = 2;
Vegard Nossumaed54172008-06-05 22:46:53 -0700158 for (i = 0; i < n; ++i) {
159 if (S_ISDIR(entries[i].mode))
160 ++count;
161 }
162
163 return count;
164}
165
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200166static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000168 int result = -ENOENT;
169
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200171 if (task->fs) {
172 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000173 result = 0;
174 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000176 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700177}
178
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800179static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700180{
David Howells2b0143b2015-03-17 22:25:59 +0000181 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700182 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700183
184 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200185 task_lock(task);
186 if (task->fs) {
187 get_fs_pwd(task->fs, path);
188 result = 0;
189 }
190 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700191 put_task_struct(task);
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return result;
194}
195
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800196static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
David Howells2b0143b2015-03-17 22:25:59 +0000198 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700200
201 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200202 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700203 put_task_struct(task);
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return result;
206}
207
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700208static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
Linus Torvalds5ab82712018-05-17 15:17:33 -0700209 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700211 unsigned long arg_start, arg_end, env_start, env_end;
Linus Torvalds5ab82712018-05-17 15:17:33 -0700212 unsigned long pos, len;
213 char *page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700214
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700215 /* Check if process spawned far enough to have cmdline. */
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700216 if (!mm->env_end)
217 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700218
Yang Shi88aa7cc2018-06-07 17:05:28 -0700219 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700220 arg_start = mm->arg_start;
221 arg_end = mm->arg_end;
222 env_start = mm->env_start;
223 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700224 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700225
Linus Torvalds5ab82712018-05-17 15:17:33 -0700226 if (arg_start >= arg_end)
227 return 0;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700228
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700229 /*
Linus Torvalds5ab82712018-05-17 15:17:33 -0700230 * We have traditionally allowed the user to re-write
231 * the argument strings and overflow the end result
232 * into the environment section. But only do that if
233 * the environment area is contiguous to the arguments.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700234 */
Linus Torvalds5ab82712018-05-17 15:17:33 -0700235 if (env_start != arg_end || env_start >= env_end)
236 env_start = env_end = arg_end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700237
Linus Torvalds5ab82712018-05-17 15:17:33 -0700238 /* We're not going to care if "*ppos" has high bits set */
239 pos = arg_start + *ppos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700240
Linus Torvalds5ab82712018-05-17 15:17:33 -0700241 /* .. but we do check the result is in the proper range */
242 if (pos < arg_start || pos >= env_end)
243 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700244
Linus Torvalds5ab82712018-05-17 15:17:33 -0700245 /* .. and we never go past env_end */
246 if (env_end - pos < count)
247 count = env_end - pos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700248
Linus Torvalds5ab82712018-05-17 15:17:33 -0700249 page = (char *)__get_free_page(GFP_KERNEL);
250 if (!page)
251 return -ENOMEM;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700252
Linus Torvalds5ab82712018-05-17 15:17:33 -0700253 len = 0;
254 while (count) {
255 int got;
256 size_t size = min_t(size_t, PAGE_SIZE, count);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700257
Linus Torvalds5ab82712018-05-17 15:17:33 -0700258 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
259 if (got <= 0)
260 break;
261
262 /* Don't walk past a NUL character once you hit arg_end */
263 if (pos + got >= arg_end) {
264 int n = 0;
265
266 /*
267 * If we started before 'arg_end' but ended up
268 * at or after it, we start the NUL character
269 * check at arg_end-1 (where we expect the normal
270 * EOF to be).
271 *
272 * NOTE! This is smaller than 'got', because
273 * pos + got >= arg_end
274 */
275 if (pos < arg_end)
276 n = arg_end - pos - 1;
277
278 /* Cut off at first NUL after 'n' */
279 got = n + strnlen(page+n, got-n);
280 if (!got)
281 break;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700282 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800283
Linus Torvalds5ab82712018-05-17 15:17:33 -0700284 got -= copy_to_user(buf, page, got);
285 if (unlikely(!got)) {
286 if (!len)
287 len = -EFAULT;
288 break;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700289 }
Linus Torvalds5ab82712018-05-17 15:17:33 -0700290 pos += got;
291 buf += got;
292 len += got;
293 count -= got;
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700294 }
295
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700296 free_page((unsigned long)page);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700297 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700300static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
301 size_t count, loff_t *pos)
302{
303 struct mm_struct *mm;
304 ssize_t ret;
305
306 mm = get_task_mm(tsk);
307 if (!mm)
308 return 0;
309
310 ret = get_mm_cmdline(mm, buf, count, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 mmput(mm);
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700312 return ret;
313}
314
315static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
316 size_t count, loff_t *pos)
317{
318 struct task_struct *tsk;
319 ssize_t ret;
320
321 BUG_ON(*pos < 0);
322
323 tsk = get_proc_task(file_inode(file));
324 if (!tsk)
325 return -ESRCH;
326 ret = get_task_cmdline(tsk, buf, count, pos);
327 put_task_struct(tsk);
328 if (ret > 0)
329 *pos += ret;
330 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700333static const struct file_operations proc_pid_cmdline_ops = {
334 .read = proc_pid_cmdline_read,
335 .llseek = generic_file_llseek,
336};
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#ifdef CONFIG_KALLSYMS
339/*
340 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
341 * Returns the resolved symbol. If that fails, simply return the address.
342 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700343static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
344 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700346 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700347 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700349 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
350 goto print0;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700353 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700354 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700355 return 0;
356 }
Joe Perches25ce3192015-04-15 16:18:17 -0700357
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700358print0:
359 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362#endif /* CONFIG_KALLSYMS */
363
Al Viroa9712bc2011-03-23 15:52:50 -0400364static int lock_trace(struct task_struct *task)
365{
366 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
367 if (err)
368 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800369 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400370 mutex_unlock(&task->signal->cred_guard_mutex);
371 return -EPERM;
372 }
373 return 0;
374}
375
376static void unlock_trace(struct task_struct *task)
377{
378 mutex_unlock(&task->signal->cred_guard_mutex);
379}
380
Ken Chen2ec220e2008-11-10 11:26:08 +0300381#ifdef CONFIG_STACKTRACE
382
383#define MAX_STACK_TRACE_DEPTH 64
384
385static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
386 struct pid *pid, struct task_struct *task)
387{
388 struct stack_trace trace;
389 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400390 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300391
Kees Cook6da2ec52018-06-12 13:55:00 -0700392 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
393 GFP_KERNEL);
Ken Chen2ec220e2008-11-10 11:26:08 +0300394 if (!entries)
395 return -ENOMEM;
396
397 trace.nr_entries = 0;
398 trace.max_entries = MAX_STACK_TRACE_DEPTH;
399 trace.entries = entries;
400 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300401
Al Viroa9712bc2011-03-23 15:52:50 -0400402 err = lock_trace(task);
403 if (!err) {
Alexey Dobriyan5d008fb2018-06-07 17:10:17 -0700404 unsigned int i;
405
Al Viroa9712bc2011-03-23 15:52:50 -0400406 save_stack_trace_tsk(task, &trace);
407
408 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800409 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400410 }
411 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300412 }
413 kfree(entries);
414
Al Viroa9712bc2011-03-23 15:52:50 -0400415 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300416}
417#endif
418
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530419#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
421 * Provides /proc/PID/schedstat
422 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700423static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
424 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530426 if (unlikely(!sched_info_on()))
427 seq_printf(m, "0 0 0\n");
428 else
429 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700430 (unsigned long long)task->se.sum_exec_runtime,
431 (unsigned long long)task->sched_info.run_delay,
432 task->sched_info.pcount);
433
434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436#endif
437
Arjan van de Ven97455122008-01-25 21:08:34 +0100438#ifdef CONFIG_LATENCYTOP
439static int lstats_show_proc(struct seq_file *m, void *v)
440{
441 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800442 struct inode *inode = m->private;
443 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100444
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800445 if (!task)
446 return -ESRCH;
447 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100448 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800449 struct latency_record *lr = &task->latency_record[i];
450 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100451 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800452 seq_printf(m, "%i %li %li",
453 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100454 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800455 unsigned long bt = lr->backtrace[q];
456 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100457 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800458 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100459 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800460 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100461 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800462 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100463 }
464
465 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800466 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100467 return 0;
468}
469
470static int lstats_open(struct inode *inode, struct file *file)
471{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800472 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800473}
474
Arjan van de Ven97455122008-01-25 21:08:34 +0100475static ssize_t lstats_write(struct file *file, const char __user *buf,
476 size_t count, loff_t *offs)
477{
Al Viro496ad9a2013-01-23 17:07:38 -0500478 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100479
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800480 if (!task)
481 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100482 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800483 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100484
485 return count;
486}
487
488static const struct file_operations proc_lstats_operations = {
489 .open = lstats_open,
490 .read = seq_read,
491 .write = lstats_write,
492 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800493 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100494};
495
496#endif
497
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700498static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
499 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
David Rientjesa7f638f2012-05-29 15:06:47 -0700501 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200502 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Oleg Nesterovef419392016-08-02 14:03:19 -0700504 points = oom_badness(task, NULL, NULL, totalpages) *
505 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700506 seq_printf(m, "%lu\n", points);
507
508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Neil Hormand85f50d2007-10-18 23:40:37 -0700511struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700512 const char *name;
513 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700514};
515
516static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700517 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700518 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
519 [RLIMIT_DATA] = {"Max data size", "bytes"},
520 [RLIMIT_STACK] = {"Max stack size", "bytes"},
521 [RLIMIT_CORE] = {"Max core file size", "bytes"},
522 [RLIMIT_RSS] = {"Max resident set", "bytes"},
523 [RLIMIT_NPROC] = {"Max processes", "processes"},
524 [RLIMIT_NOFILE] = {"Max open files", "files"},
525 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
526 [RLIMIT_AS] = {"Max address space", "bytes"},
527 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
528 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
529 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
530 [RLIMIT_NICE] = {"Max nice priority", NULL},
531 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800532 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700533};
534
535/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700536static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
537 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700538{
539 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700540 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700541
542 struct rlimit rlim[RLIM_NLIMITS];
543
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400544 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700545 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700546 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
547 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700548
549 /*
550 * print the file header
551 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700552 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700553 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700554
555 for (i = 0; i < RLIM_NLIMITS; i++) {
556 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700557 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700558 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700559 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700560 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700561 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700562
563 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700564 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700565 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700566 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700567
568 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700569 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700570 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700571 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700572 }
573
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700574 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700575}
576
Roland McGrathebcb6732008-07-25 19:46:00 -0700577#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700578static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
579 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700580{
581 long nr;
582 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700583 int res;
584
585 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400586 if (res)
587 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700588
589 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700590 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400591 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700592 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400593 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700594 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700595 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
596 nr,
597 args[0], args[1], args[2], args[3], args[4], args[5],
598 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400599 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700600
601 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700602}
603#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/************************************************************************/
606/* Here the fs part begins */
607/************************************************************************/
608
609/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700610static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700612 struct task_struct *task;
613 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700614 /* Allow access to a task's file descriptors if it is us or we
615 * may use ptrace attach to the process and find out that
616 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700617 */
618 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700619 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800620 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700621 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700622 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700623 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800626int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700627{
628 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000629 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700630
631 if (attr->ia_valid & ATTR_MODE)
632 return -EPERM;
633
Jan Kara31051c82016-05-26 16:55:18 +0200634 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200635 if (error)
636 return error;
637
Christoph Hellwig10257742010-06-04 11:30:02 +0200638 setattr_copy(inode, attr);
639 mark_inode_dirty(inode);
640 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700641}
642
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800643/*
644 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
645 * or euid/egid (for hide_pid_min=2)?
646 */
647static bool has_pid_permissions(struct pid_namespace *pid,
648 struct task_struct *task,
649 int hide_pid_min)
650{
651 if (pid->hide_pid < hide_pid_min)
652 return true;
653 if (in_group_p(pid->pid_gid))
654 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800655 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800656}
657
658
659static int proc_pid_permission(struct inode *inode, int mask)
660{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200661 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800662 struct task_struct *task;
663 bool has_perms;
664
665 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800666 if (!task)
667 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800668 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800669 put_task_struct(task);
670
671 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800672 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800673 /*
674 * Let's make getdents(), stat(), and open()
675 * consistent with each other. If a process
676 * may not stat() a file, it shouldn't be seen
677 * in procfs at all.
678 */
679 return -ENOENT;
680 }
681
682 return -EPERM;
683 }
684 return generic_permission(inode, mask);
685}
686
687
688
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800689static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700690 .setattr = proc_setattr,
691};
692
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800693static int proc_single_show(struct seq_file *m, void *v)
694{
695 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200696 struct pid_namespace *ns = proc_pid_ns(inode);
697 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800698 struct task_struct *task;
699 int ret;
700
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800701 task = get_pid_task(pid, PIDTYPE_PID);
702 if (!task)
703 return -ESRCH;
704
705 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
706
707 put_task_struct(task);
708 return ret;
709}
710
711static int proc_single_open(struct inode *inode, struct file *filp)
712{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800713 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800714}
715
716static const struct file_operations proc_single_file_operations = {
717 .open = proc_single_open,
718 .read = seq_read,
719 .llseek = seq_lseek,
720 .release = single_release,
721};
722
Oleg Nesterov5381e162014-10-09 15:25:24 -0700723
724struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
725{
726 struct task_struct *task = get_proc_task(inode);
727 struct mm_struct *mm = ERR_PTR(-ESRCH);
728
729 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800730 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700731 put_task_struct(task);
732
733 if (!IS_ERR_OR_NULL(mm)) {
734 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800735 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700736 /* but do not pin its memory */
737 mmput(mm);
738 }
739 }
740
741 return mm;
742}
743
Cong Wangb409e572012-05-31 16:26:17 -0700744static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700746 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800747
748 if (IS_ERR(mm))
749 return PTR_ERR(mm);
750
Linus Torvaldse2683372012-01-17 15:21:19 -0800751 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753}
754
Cong Wangb409e572012-05-31 16:26:17 -0700755static int mem_open(struct inode *inode, struct file *file)
756{
Djalal Harounibc452b42012-07-30 14:42:28 -0700757 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
758
759 /* OK to pass negative loff_t, we can catch out-of-range */
760 file->f_mode |= FMODE_UNSIGNED_OFFSET;
761
762 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700763}
764
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100765static ssize_t mem_rw(struct file *file, char __user *buf,
766 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Linus Torvaldse2683372012-01-17 15:21:19 -0800768 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100769 unsigned long addr = *ppos;
770 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700772 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvaldse2683372012-01-17 15:21:19 -0800774 if (!mm)
775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Michal Hocko0ee931c2017-09-13 16:28:29 -0700777 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800779 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700781 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800782 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100783 goto free;
784
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700785 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100788 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100790 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 copied = -EFAULT;
792 break;
793 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100794
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100795 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100796 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!copied)
798 copied = -EIO;
799 break;
800 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100801
802 if (!write && copy_to_user(buf, page, this_len)) {
803 copied = -EFAULT;
804 break;
805 }
806
807 buf += this_len;
808 addr += this_len;
809 copied += this_len;
810 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100812 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700813
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100814 mmput(mm);
815free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700816 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return copied;
818}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100820static ssize_t mem_read(struct file *file, char __user *buf,
821 size_t count, loff_t *ppos)
822{
823 return mem_rw(file, buf, count, ppos, 0);
824}
825
826static ssize_t mem_write(struct file *file, const char __user *buf,
827 size_t count, loff_t *ppos)
828{
829 return mem_rw(file, (char __user*)buf, count, ppos, 1);
830}
831
Matt Mackall85863e42008-02-04 22:29:04 -0800832loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 switch (orig) {
835 case 0:
836 file->f_pos = offset;
837 break;
838 case 1:
839 file->f_pos += offset;
840 break;
841 default:
842 return -EINVAL;
843 }
844 force_successful_syscall_return();
845 return file->f_pos;
846}
847
Linus Torvaldse2683372012-01-17 15:21:19 -0800848static int mem_release(struct inode *inode, struct file *file)
849{
850 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100851 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100852 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800853 return 0;
854}
855
Arjan van de Ven00977a52007-02-12 00:55:34 -0800856static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 .llseek = mem_lseek,
858 .read = mem_read,
859 .write = mem_write,
860 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800861 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862};
863
Cong Wangb409e572012-05-31 16:26:17 -0700864static int environ_open(struct inode *inode, struct file *file)
865{
866 return __mem_open(inode, file, PTRACE_MODE_READ);
867}
868
James Pearson315e28c2007-10-16 23:30:17 -0700869static ssize_t environ_read(struct file *file, char __user *buf,
870 size_t count, loff_t *ppos)
871{
James Pearson315e28c2007-10-16 23:30:17 -0700872 char *page;
873 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700874 int ret = 0;
875 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800876 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700877
Mathias Krause8148a732016-05-05 16:22:26 -0700878 /* Ensure the process spawned far enough to have an environment. */
879 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700880 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700881
Michal Hocko0ee931c2017-09-13 16:28:29 -0700882 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700883 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700884 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700885
Al Virod6f64b82011-02-15 22:26:01 -0500886 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800887 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700888 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800889
Yang Shi88aa7cc2018-06-07 17:05:28 -0700890 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800891 env_start = mm->env_start;
892 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700893 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800894
James Pearson315e28c2007-10-16 23:30:17 -0700895 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700896 size_t this_len, max_len;
897 int retval;
898
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800899 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700900 break;
James Pearson315e28c2007-10-16 23:30:17 -0700901
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800902 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700903
Djalal Harounie8905ec2012-07-30 14:42:26 -0700904 max_len = min_t(size_t, PAGE_SIZE, count);
905 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700906
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200907 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700908
909 if (retval <= 0) {
910 ret = retval;
911 break;
912 }
913
914 if (copy_to_user(buf, page, retval)) {
915 ret = -EFAULT;
916 break;
917 }
918
919 ret += retval;
920 src += retval;
921 buf += retval;
922 count -= retval;
923 }
924 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700925 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700926
927free:
James Pearson315e28c2007-10-16 23:30:17 -0700928 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700929 return ret;
930}
931
932static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700933 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700934 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100935 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700936 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700937};
938
Al Viroc5317162016-10-05 18:43:43 -0400939static int auxv_open(struct inode *inode, struct file *file)
940{
941 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
942}
943
944static ssize_t auxv_read(struct file *file, char __user *buf,
945 size_t count, loff_t *ppos)
946{
947 struct mm_struct *mm = file->private_data;
948 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700949
950 if (!mm)
951 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400952 do {
953 nwords += 2;
954 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
955 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
956 nwords * sizeof(mm->saved_auxv[0]));
957}
958
959static const struct file_operations proc_auxv_operations = {
960 .open = auxv_open,
961 .read = auxv_read,
962 .llseek = generic_file_llseek,
963 .release = mem_release,
964};
965
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800966static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
967 loff_t *ppos)
968{
Al Viro496ad9a2013-01-23 17:07:38 -0500969 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800970 char buffer[PROC_NUMBUF];
971 int oom_adj = OOM_ADJUST_MIN;
972 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800973
974 if (!task)
975 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -0700976 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
977 oom_adj = OOM_ADJUST_MAX;
978 else
979 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
980 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800981 put_task_struct(task);
982 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
983 return simple_read_from_buffer(buf, count, ppos, buffer, len);
984}
985
Michal Hocko1d5f0ac2016-07-28 15:44:40 -0700986static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
987{
988 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -0700989 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -0700990 struct task_struct *task;
991 int err = 0;
992
993 task = get_proc_task(file_inode(file));
994 if (!task)
995 return -ESRCH;
996
997 mutex_lock(&oom_adj_mutex);
998 if (legacy) {
999 if (oom_adj < task->signal->oom_score_adj &&
1000 !capable(CAP_SYS_RESOURCE)) {
1001 err = -EACCES;
1002 goto err_unlock;
1003 }
1004 /*
1005 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1006 * /proc/pid/oom_score_adj instead.
1007 */
1008 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1009 current->comm, task_pid_nr(current), task_pid_nr(task),
1010 task_pid_nr(task));
1011 } else {
1012 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1013 !capable(CAP_SYS_RESOURCE)) {
1014 err = -EACCES;
1015 goto err_unlock;
1016 }
1017 }
1018
Michal Hocko44a70ade2016-07-28 15:44:43 -07001019 /*
1020 * Make sure we will check other processes sharing the mm if this is
1021 * not vfrok which wants its own oom_score_adj.
1022 * pin the mm so it doesn't go away and get reused after task_unlock
1023 */
1024 if (!task->vfork_done) {
1025 struct task_struct *p = find_lock_task_mm(task);
1026
1027 if (p) {
1028 if (atomic_read(&p->mm->mm_users) > 1) {
1029 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001030 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001031 }
1032 task_unlock(p);
1033 }
1034 }
1035
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001036 task->signal->oom_score_adj = oom_adj;
1037 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1038 task->signal->oom_score_adj_min = (short)oom_adj;
1039 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001040
1041 if (mm) {
1042 struct task_struct *p;
1043
1044 rcu_read_lock();
1045 for_each_process(p) {
1046 if (same_thread_group(task, p))
1047 continue;
1048
1049 /* do not touch kernel threads or the global init */
1050 if (p->flags & PF_KTHREAD || is_global_init(p))
1051 continue;
1052
1053 task_lock(p);
1054 if (!p->vfork_done && process_shares_mm(p, mm)) {
1055 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1056 task_pid_nr(p), p->comm,
1057 p->signal->oom_score_adj, oom_adj,
1058 task_pid_nr(task), task->comm);
1059 p->signal->oom_score_adj = oom_adj;
1060 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1061 p->signal->oom_score_adj_min = (short)oom_adj;
1062 }
1063 task_unlock(p);
1064 }
1065 rcu_read_unlock();
1066 mmdrop(mm);
1067 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001068err_unlock:
1069 mutex_unlock(&oom_adj_mutex);
1070 put_task_struct(task);
1071 return err;
1072}
Michal Hockof913da52016-07-28 15:44:37 -07001073
David Rientjesb72bdfa2015-11-05 18:50:32 -08001074/*
1075 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1076 * kernels. The effective policy is defined by oom_score_adj, which has a
1077 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1078 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1079 * Processes that become oom disabled via oom_adj will still be oom disabled
1080 * with this implementation.
1081 *
1082 * oom_adj cannot be removed since existing userspace binaries use it.
1083 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001084static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1085 size_t count, loff_t *ppos)
1086{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001087 char buffer[PROC_NUMBUF];
1088 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001089 int err;
1090
1091 memset(buffer, 0, sizeof(buffer));
1092 if (count > sizeof(buffer) - 1)
1093 count = sizeof(buffer) - 1;
1094 if (copy_from_user(buffer, buf, count)) {
1095 err = -EFAULT;
1096 goto out;
1097 }
1098
1099 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1100 if (err)
1101 goto out;
1102 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1103 oom_adj != OOM_DISABLE) {
1104 err = -EINVAL;
1105 goto out;
1106 }
1107
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001108 /*
1109 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1110 * value is always attainable.
1111 */
1112 if (oom_adj == OOM_ADJUST_MAX)
1113 oom_adj = OOM_SCORE_ADJ_MAX;
1114 else
1115 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1116
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001117 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001118out:
1119 return err < 0 ? err : count;
1120}
1121
1122static const struct file_operations proc_oom_adj_operations = {
1123 .read = oom_adj_read,
1124 .write = oom_adj_write,
1125 .llseek = generic_file_llseek,
1126};
1127
David Rientjesa63d83f2010-08-09 17:19:46 -07001128static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1129 size_t count, loff_t *ppos)
1130{
Al Viro496ad9a2013-01-23 17:07:38 -05001131 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001132 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001133 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001134 size_t len;
1135
1136 if (!task)
1137 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001138 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001139 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001140 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001141 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1142}
1143
1144static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1145 size_t count, loff_t *ppos)
1146{
David Rientjesa63d83f2010-08-09 17:19:46 -07001147 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001148 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001149 int err;
1150
1151 memset(buffer, 0, sizeof(buffer));
1152 if (count > sizeof(buffer) - 1)
1153 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001154 if (copy_from_user(buffer, buf, count)) {
1155 err = -EFAULT;
1156 goto out;
1157 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001158
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001159 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001160 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001161 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001162 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001163 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1164 err = -EINVAL;
1165 goto out;
1166 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001167
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001168 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001169out:
1170 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001171}
1172
1173static const struct file_operations proc_oom_score_adj_operations = {
1174 .read = oom_score_adj_read,
1175 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001176 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001177};
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001180#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1182 size_t count, loff_t *ppos)
1183{
Al Viro496ad9a2013-01-23 17:07:38 -05001184 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001185 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ssize_t length;
1187 char tmpbuf[TMPBUFLEN];
1188
Eric W. Biederman99f89552006-06-26 00:25:55 -07001189 if (!task)
1190 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001192 from_kuid(file->f_cred->user_ns,
1193 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001194 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1196}
1197
1198static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1199 size_t count, loff_t *ppos)
1200{
Al Viro496ad9a2013-01-23 17:07:38 -05001201 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001203 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001204 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001206 rcu_read_lock();
1207 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1208 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001210 }
1211 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (*ppos != 0) {
1214 /* No partial writes. */
1215 return -EINVAL;
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001218 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1219 if (rv < 0)
1220 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001221
1222 /* is userspace tring to explicitly UNSET the loginuid? */
1223 if (loginuid == AUDIT_UID_UNSET) {
1224 kloginuid = INVALID_UID;
1225 } else {
1226 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001227 if (!uid_valid(kloginuid))
1228 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001229 }
1230
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001231 rv = audit_set_loginuid(kloginuid);
1232 if (rv < 0)
1233 return rv;
1234 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Arjan van de Ven00977a52007-02-12 00:55:34 -08001237static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 .read = proc_loginuid_read,
1239 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001240 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241};
Eric Paris1e0bd752008-03-13 08:15:31 -04001242
1243static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1244 size_t count, loff_t *ppos)
1245{
Al Viro496ad9a2013-01-23 17:07:38 -05001246 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001247 struct task_struct *task = get_proc_task(inode);
1248 ssize_t length;
1249 char tmpbuf[TMPBUFLEN];
1250
1251 if (!task)
1252 return -ESRCH;
1253 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1254 audit_get_sessionid(task));
1255 put_task_struct(task);
1256 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1257}
1258
1259static const struct file_operations proc_sessionid_operations = {
1260 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001261 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001262};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263#endif
1264
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001265#ifdef CONFIG_FAULT_INJECTION
1266static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1267 size_t count, loff_t *ppos)
1268{
Al Viro496ad9a2013-01-23 17:07:38 -05001269 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001270 char buffer[PROC_NUMBUF];
1271 size_t len;
1272 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001273
1274 if (!task)
1275 return -ESRCH;
1276 make_it_fail = task->make_it_fail;
1277 put_task_struct(task);
1278
1279 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001280
1281 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001282}
1283
1284static ssize_t proc_fault_inject_write(struct file * file,
1285 const char __user * buf, size_t count, loff_t *ppos)
1286{
1287 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001288 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001289 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001290 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001291
1292 if (!capable(CAP_SYS_RESOURCE))
1293 return -EPERM;
1294 memset(buffer, 0, sizeof(buffer));
1295 if (count > sizeof(buffer) - 1)
1296 count = sizeof(buffer) - 1;
1297 if (copy_from_user(buffer, buf, count))
1298 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001299 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1300 if (rv < 0)
1301 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001302 if (make_it_fail < 0 || make_it_fail > 1)
1303 return -EINVAL;
1304
Al Viro496ad9a2013-01-23 17:07:38 -05001305 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001306 if (!task)
1307 return -ESRCH;
1308 task->make_it_fail = make_it_fail;
1309 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001310
1311 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001312}
1313
Arjan van de Ven00977a52007-02-12 00:55:34 -08001314static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001315 .read = proc_fault_inject_read,
1316 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001317 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001318};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001319
1320static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1321 size_t count, loff_t *ppos)
1322{
1323 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001324 int err;
1325 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001326
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001327 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001328 if (err)
1329 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001330
1331 task = get_proc_task(file_inode(file));
1332 if (!task)
1333 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001334 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001335 put_task_struct(task);
1336
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001337 return count;
1338}
1339
1340static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1341 size_t count, loff_t *ppos)
1342{
1343 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001344 char numbuf[PROC_NUMBUF];
1345 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001346
1347 task = get_proc_task(file_inode(file));
1348 if (!task)
1349 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001350 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001351 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001352 put_task_struct(task);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001353
1354 return len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001355}
1356
1357static const struct file_operations proc_fail_nth_operations = {
1358 .read = proc_fail_nth_read,
1359 .write = proc_fail_nth_write,
1360};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001361#endif
1362
Arjan van de Ven97455122008-01-25 21:08:34 +01001363
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001364#ifdef CONFIG_SCHED_DEBUG
1365/*
1366 * Print out various scheduling related per-task fields:
1367 */
1368static int sched_show(struct seq_file *m, void *v)
1369{
1370 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001371 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001372 struct task_struct *p;
1373
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001374 p = get_proc_task(inode);
1375 if (!p)
1376 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001377 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001378
1379 put_task_struct(p);
1380
1381 return 0;
1382}
1383
1384static ssize_t
1385sched_write(struct file *file, const char __user *buf,
1386 size_t count, loff_t *offset)
1387{
Al Viro496ad9a2013-01-23 17:07:38 -05001388 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001389 struct task_struct *p;
1390
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001391 p = get_proc_task(inode);
1392 if (!p)
1393 return -ESRCH;
1394 proc_sched_set_task(p);
1395
1396 put_task_struct(p);
1397
1398 return count;
1399}
1400
1401static int sched_open(struct inode *inode, struct file *filp)
1402{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001403 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001404}
1405
1406static const struct file_operations proc_pid_sched_operations = {
1407 .open = sched_open,
1408 .read = seq_read,
1409 .write = sched_write,
1410 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001411 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001412};
1413
1414#endif
1415
Mike Galbraith5091faa2010-11-30 14:18:03 +01001416#ifdef CONFIG_SCHED_AUTOGROUP
1417/*
1418 * Print out autogroup related information:
1419 */
1420static int sched_autogroup_show(struct seq_file *m, void *v)
1421{
1422 struct inode *inode = m->private;
1423 struct task_struct *p;
1424
1425 p = get_proc_task(inode);
1426 if (!p)
1427 return -ESRCH;
1428 proc_sched_autogroup_show_task(p, m);
1429
1430 put_task_struct(p);
1431
1432 return 0;
1433}
1434
1435static ssize_t
1436sched_autogroup_write(struct file *file, const char __user *buf,
1437 size_t count, loff_t *offset)
1438{
Al Viro496ad9a2013-01-23 17:07:38 -05001439 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001440 struct task_struct *p;
1441 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001442 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001443 int err;
1444
1445 memset(buffer, 0, sizeof(buffer));
1446 if (count > sizeof(buffer) - 1)
1447 count = sizeof(buffer) - 1;
1448 if (copy_from_user(buffer, buf, count))
1449 return -EFAULT;
1450
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001451 err = kstrtoint(strstrip(buffer), 0, &nice);
1452 if (err < 0)
1453 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001454
1455 p = get_proc_task(inode);
1456 if (!p)
1457 return -ESRCH;
1458
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001459 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001460 if (err)
1461 count = err;
1462
1463 put_task_struct(p);
1464
1465 return count;
1466}
1467
1468static int sched_autogroup_open(struct inode *inode, struct file *filp)
1469{
1470 int ret;
1471
1472 ret = single_open(filp, sched_autogroup_show, NULL);
1473 if (!ret) {
1474 struct seq_file *m = filp->private_data;
1475
1476 m->private = inode;
1477 }
1478 return ret;
1479}
1480
1481static const struct file_operations proc_pid_sched_autogroup_operations = {
1482 .open = sched_autogroup_open,
1483 .read = seq_read,
1484 .write = sched_autogroup_write,
1485 .llseek = seq_lseek,
1486 .release = single_release,
1487};
1488
1489#endif /* CONFIG_SCHED_AUTOGROUP */
1490
john stultz4614a696b2009-12-14 18:00:05 -08001491static ssize_t comm_write(struct file *file, const char __user *buf,
1492 size_t count, loff_t *offset)
1493{
Al Viro496ad9a2013-01-23 17:07:38 -05001494 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001495 struct task_struct *p;
1496 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001497 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001498
1499 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001500 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001501 return -EFAULT;
1502
1503 p = get_proc_task(inode);
1504 if (!p)
1505 return -ESRCH;
1506
1507 if (same_thread_group(current, p))
1508 set_task_comm(p, buffer);
1509 else
1510 count = -EINVAL;
1511
1512 put_task_struct(p);
1513
1514 return count;
1515}
1516
1517static int comm_show(struct seq_file *m, void *v)
1518{
1519 struct inode *inode = m->private;
1520 struct task_struct *p;
1521
1522 p = get_proc_task(inode);
1523 if (!p)
1524 return -ESRCH;
1525
Tejun Heo88b72b32018-05-18 08:47:13 -07001526 proc_task_name(m, p, false);
1527 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001528
1529 put_task_struct(p);
1530
1531 return 0;
1532}
1533
1534static int comm_open(struct inode *inode, struct file *filp)
1535{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001536 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001537}
1538
1539static const struct file_operations proc_pid_set_comm_operations = {
1540 .open = comm_open,
1541 .read = seq_read,
1542 .write = comm_write,
1543 .llseek = seq_lseek,
1544 .release = single_release,
1545};
1546
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001547static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001548{
1549 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001550 struct file *exe_file;
1551
David Howells2b0143b2015-03-17 22:25:59 +00001552 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001553 if (!task)
1554 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001555 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001556 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001557 if (exe_file) {
1558 *exe_path = exe_file->f_path;
1559 path_get(&exe_file->f_path);
1560 fput(exe_file);
1561 return 0;
1562 } else
1563 return -ENOENT;
1564}
1565
Al Viro6b255392015-11-17 10:20:54 -05001566static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001567 struct inode *inode,
1568 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001570 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int error = -EACCES;
1572
Al Viro6b255392015-11-17 10:20:54 -05001573 if (!dentry)
1574 return ERR_PTR(-ECHILD);
1575
Eric W. Biederman778c1142006-06-26 00:25:58 -07001576 /* Are we allowed to snoop on the tasks file descriptors? */
1577 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Christoph Hellwig408ef012012-06-18 10:47:03 -04001580 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1581 if (error)
1582 goto out;
1583
Al Viro6e77137b2015-05-02 13:37:52 -04001584 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001585 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586out:
Al Viro008b1502005-08-20 00:17:39 +01001587 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001590static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001592 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001593 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int len;
1595
1596 if (!tmp)
1597 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001598
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001599 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001600 len = PTR_ERR(pathname);
1601 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001603 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (len > buflen)
1606 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001607 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 len = -EFAULT;
1609 out:
1610 free_page((unsigned long)tmp);
1611 return len;
1612}
1613
1614static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1615{
1616 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001617 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001618 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Eric W. Biederman778c1142006-06-26 00:25:58 -07001620 /* Are we allowed to snoop on the tasks file descriptors? */
1621 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001624 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (error)
1626 goto out;
1627
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001628 error = do_proc_readlink(&path, buffer, buflen);
1629 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return error;
1632}
1633
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001634const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001636 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001637 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638};
1639
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001640
1641/* building an inode */
1642
Al Viroc6eb50d2017-09-30 14:45:42 -04001643void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001644 kuid_t *ruid, kgid_t *rgid)
1645{
1646 /* Depending on the state of dumpable compute who should own a
1647 * proc file for a task.
1648 */
1649 const struct cred *cred;
1650 kuid_t uid;
1651 kgid_t gid;
1652
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001653 if (unlikely(task->flags & PF_KTHREAD)) {
1654 *ruid = GLOBAL_ROOT_UID;
1655 *rgid = GLOBAL_ROOT_GID;
1656 return;
1657 }
1658
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001659 /* Default to the tasks effective ownership */
1660 rcu_read_lock();
1661 cred = __task_cred(task);
1662 uid = cred->euid;
1663 gid = cred->egid;
1664 rcu_read_unlock();
1665
1666 /*
1667 * Before the /proc/pid/status file was created the only way to read
1668 * the effective uid of a /process was to stat /proc/pid. Reading
1669 * /proc/pid/status is slow enough that procps and other packages
1670 * kept stating /proc/pid. To keep the rules in /proc simple I have
1671 * made this apply to all per process world readable and executable
1672 * directories.
1673 */
1674 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1675 struct mm_struct *mm;
1676 task_lock(task);
1677 mm = task->mm;
1678 /* Make non-dumpable tasks owned by some root */
1679 if (mm) {
1680 if (get_dumpable(mm) != SUID_DUMP_USER) {
1681 struct user_namespace *user_ns = mm->user_ns;
1682
1683 uid = make_kuid(user_ns, 0);
1684 if (!uid_valid(uid))
1685 uid = GLOBAL_ROOT_UID;
1686
1687 gid = make_kgid(user_ns, 0);
1688 if (!gid_valid(gid))
1689 gid = GLOBAL_ROOT_GID;
1690 }
1691 } else {
1692 uid = GLOBAL_ROOT_UID;
1693 gid = GLOBAL_ROOT_GID;
1694 }
1695 task_unlock(task);
1696 }
1697 *ruid = uid;
1698 *rgid = gid;
1699}
1700
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001701struct inode *proc_pid_make_inode(struct super_block * sb,
1702 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001703{
1704 struct inode * inode;
1705 struct proc_inode *ei;
1706
1707 /* We need a new inode */
1708
1709 inode = new_inode(sb);
1710 if (!inode)
1711 goto out;
1712
1713 /* Common stuff */
1714 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001715 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001716 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001717 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001718 inode->i_op = &proc_def_inode_operations;
1719
1720 /*
1721 * grab the reference to task.
1722 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001723 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001724 if (!ei->pid)
1725 goto out_unlock;
1726
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001727 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001728 security_task_to_inode(task, inode);
1729
1730out:
1731 return inode;
1732
1733out_unlock:
1734 iput(inode);
1735 return NULL;
1736}
1737
David Howellsa528d352017-01-31 16:46:22 +00001738int pid_getattr(const struct path *path, struct kstat *stat,
1739 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001740{
David Howellsa528d352017-01-31 16:46:22 +00001741 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001742 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001743 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001744
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001745 generic_fillattr(inode, stat);
1746
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001747 stat->uid = GLOBAL_ROOT_UID;
1748 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001749 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001750 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1751 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001752 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001753 rcu_read_unlock();
1754 /*
1755 * This doesn't prevent learning whether PID exists,
1756 * it only makes getattr() consistent with readdir().
1757 */
1758 return -ENOENT;
1759 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001760 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761 }
1762 rcu_read_unlock();
1763 return 0;
1764}
1765
1766/* dentry stuff */
1767
1768/*
Al Viro1bbc5512018-05-02 21:26:16 -04001769 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1770 */
1771void pid_update_inode(struct task_struct *task, struct inode *inode)
1772{
1773 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1774
1775 inode->i_mode &= ~(S_ISUID | S_ISGID);
1776 security_task_to_inode(task, inode);
1777}
1778
1779/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001780 * Rewrite the inode's ownerships here because the owning task may have
1781 * performed a setuid(), etc.
1782 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001783 */
Al Viro1bbc5512018-05-02 21:26:16 -04001784static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001785{
Nick Piggin34286d62011-01-07 17:49:57 +11001786 struct inode *inode;
1787 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001788
Al Viro0b728e12012-06-10 16:03:43 -04001789 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001790 return -ECHILD;
1791
David Howells2b0143b2015-03-17 22:25:59 +00001792 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001793 task = get_proc_task(inode);
1794
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001795 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001796 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001797 put_task_struct(task);
1798 return 1;
1799 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001800 return 0;
1801}
1802
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001803static inline bool proc_inode_is_dead(struct inode *inode)
1804{
1805 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1806}
1807
David Howells1dd704b2013-04-12 01:08:50 +01001808int pid_delete_dentry(const struct dentry *dentry)
1809{
1810 /* Is the task we represent dead?
1811 * If so, then don't put the dentry on the lru list,
1812 * kill it immediately.
1813 */
David Howells2b0143b2015-03-17 22:25:59 +00001814 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001815}
1816
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001817const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001818{
1819 .d_revalidate = pid_revalidate,
1820 .d_delete = pid_delete_dentry,
1821};
1822
1823/* Lookups */
1824
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001825/*
1826 * Fill a directory entry.
1827 *
1828 * If possible create the dcache entry and derive our inode number and
1829 * file type from dcache entry.
1830 *
1831 * Since all of the proc inode numbers are dynamically generated, the inode
1832 * numbers do not exist until the inode is cache. This means creating the
1833 * the dcache entry in readdir is necessary to keep the inode numbers
1834 * reported by readdir in sync with the inode numbers reported
1835 * by stat.
1836 */
Al Virof0c3b502013-05-16 12:07:31 -04001837bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07001838 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001839 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001840{
Al Virof0c3b502013-05-16 12:07:31 -04001841 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001842 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001843 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001844 unsigned type = DT_UNKNOWN;
1845 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001846
Al Viro1df98b82013-06-15 11:33:10 +04001847 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001848 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001849 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1850 child = d_alloc_parallel(dir, &qname, &wq);
1851 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001852 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001853 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001854 struct dentry *res;
1855 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001856 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001857 if (unlikely(res)) {
1858 dput(child);
1859 child = res;
Al Virod85b3992018-06-08 01:17:11 -04001860 if (IS_ERR(child))
1861 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001862 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001863 }
1864 }
David Howells2b0143b2015-03-17 22:25:59 +00001865 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001866 ino = inode->i_ino;
1867 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001868 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04001869end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04001870 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001871}
1872
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001873/*
1874 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1875 * which represent vma start and end addresses.
1876 */
1877static int dname_to_vma_addr(struct dentry *dentry,
1878 unsigned long *start, unsigned long *end)
1879{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001880 const char *str = dentry->d_name.name;
1881 unsigned long long sval, eval;
1882 unsigned int len;
1883
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001884 if (str[0] == '0' && str[1] != '-')
1885 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001886 len = _parse_integer(str, 16, &sval);
1887 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001888 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001889 if (sval != (unsigned long)sval)
1890 return -EINVAL;
1891 str += len;
1892
1893 if (*str != '-')
1894 return -EINVAL;
1895 str++;
1896
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001897 if (str[0] == '0' && str[1])
1898 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001899 len = _parse_integer(str, 16, &eval);
1900 if (len & KSTRTOX_OVERFLOW)
1901 return -EINVAL;
1902 if (eval != (unsigned long)eval)
1903 return -EINVAL;
1904 str += len;
1905
1906 if (*str != '\0')
1907 return -EINVAL;
1908
1909 *start = sval;
1910 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001911
1912 return 0;
1913}
1914
Al Viro0b728e12012-06-10 16:03:43 -04001915static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001916{
1917 unsigned long vm_start, vm_end;
1918 bool exact_vma_exists = false;
1919 struct mm_struct *mm = NULL;
1920 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001921 struct inode *inode;
1922 int status = 0;
1923
Al Viro0b728e12012-06-10 16:03:43 -04001924 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001925 return -ECHILD;
1926
David Howells2b0143b2015-03-17 22:25:59 +00001927 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001928 task = get_proc_task(inode);
1929 if (!task)
1930 goto out_notask;
1931
Jann Horncaaee622016-01-20 15:00:04 -08001932 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001933 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001934 goto out;
1935
1936 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1937 down_read(&mm->mmap_sem);
1938 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1939 up_read(&mm->mmap_sem);
1940 }
1941
1942 mmput(mm);
1943
1944 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001945 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1946
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001947 security_task_to_inode(task, inode);
1948 status = 1;
1949 }
1950
1951out:
1952 put_task_struct(task);
1953
1954out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001955 return status;
1956}
1957
1958static const struct dentry_operations tid_map_files_dentry_operations = {
1959 .d_revalidate = map_files_d_revalidate,
1960 .d_delete = pid_delete_dentry,
1961};
1962
Al Viro6b255392015-11-17 10:20:54 -05001963static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001964{
1965 unsigned long vm_start, vm_end;
1966 struct vm_area_struct *vma;
1967 struct task_struct *task;
1968 struct mm_struct *mm;
1969 int rc;
1970
1971 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001972 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001973 if (!task)
1974 goto out;
1975
1976 mm = get_task_mm(task);
1977 put_task_struct(task);
1978 if (!mm)
1979 goto out;
1980
1981 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1982 if (rc)
1983 goto out_mmput;
1984
Artem Fetishev70335ab2014-03-10 15:49:45 -07001985 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001986 down_read(&mm->mmap_sem);
1987 vma = find_exact_vma(mm, vm_start, vm_end);
1988 if (vma && vma->vm_file) {
1989 *path = vma->vm_file->f_path;
1990 path_get(path);
1991 rc = 0;
1992 }
1993 up_read(&mm->mmap_sem);
1994
1995out_mmput:
1996 mmput(mm);
1997out:
1998 return rc;
1999}
2000
2001struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002002 unsigned long start;
2003 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002004 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002005};
2006
Calvin Owensbdb4d102015-09-09 15:35:54 -07002007/*
2008 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2009 * symlinks may be used to bypass permissions on ancestor directories in the
2010 * path to the file in question.
2011 */
2012static const char *
Al Viro6b255392015-11-17 10:20:54 -05002013proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002014 struct inode *inode,
2015 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002016{
2017 if (!capable(CAP_SYS_ADMIN))
2018 return ERR_PTR(-EPERM);
2019
Al Virofceef392015-12-29 15:58:39 -05002020 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002021}
2022
2023/*
Al Viro6b255392015-11-17 10:20:54 -05002024 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002025 */
2026static const struct inode_operations proc_map_files_link_inode_operations = {
2027 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002028 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002029 .setattr = proc_setattr,
2030};
2031
Al Viro0168b9e2018-05-03 09:21:05 -04002032static struct dentry *
2033proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002034 struct task_struct *task, const void *ptr)
2035{
Al Viro7b540d02012-08-27 14:55:26 -04002036 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002037 struct proc_inode *ei;
2038 struct inode *inode;
2039
Al Viro0168b9e2018-05-03 09:21:05 -04002040 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002041 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2042 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002043 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002044 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002045
2046 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002047 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002048
Calvin Owensbdb4d102015-09-09 15:35:54 -07002049 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002050 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002051
2052 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002053 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002054}
2055
2056static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002057 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002058{
2059 unsigned long vm_start, vm_end;
2060 struct vm_area_struct *vma;
2061 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002062 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002063 struct mm_struct *mm;
2064
Al Viro0168b9e2018-05-03 09:21:05 -04002065 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002066 task = get_proc_task(dir);
2067 if (!task)
2068 goto out;
2069
Al Viro0168b9e2018-05-03 09:21:05 -04002070 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002071 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072 goto out_put_task;
2073
Al Viro0168b9e2018-05-03 09:21:05 -04002074 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002075 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002076 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077
2078 mm = get_task_mm(task);
2079 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002080 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081
2082 down_read(&mm->mmap_sem);
2083 vma = find_exact_vma(mm, vm_start, vm_end);
2084 if (!vma)
2085 goto out_no_vma;
2086
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002087 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002088 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002089 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002090
2091out_no_vma:
2092 up_read(&mm->mmap_sem);
2093 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002094out_put_task:
2095 put_task_struct(task);
2096out:
Al Viro0168b9e2018-05-03 09:21:05 -04002097 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002098}
2099
2100static const struct inode_operations proc_map_files_inode_operations = {
2101 .lookup = proc_map_files_lookup,
2102 .permission = proc_fd_permission,
2103 .setattr = proc_setattr,
2104};
2105
2106static int
Al Virof0c3b502013-05-16 12:07:31 -04002107proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002109 struct vm_area_struct *vma;
2110 struct task_struct *task;
2111 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002112 unsigned long nr_files, pos, i;
2113 struct flex_array *fa = NULL;
2114 struct map_files_info info;
2115 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002116 int ret;
2117
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002118 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002119 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120 if (!task)
2121 goto out;
2122
2123 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002124 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002125 goto out_put_task;
2126
2127 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002128 if (!dir_emit_dots(file, ctx))
2129 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002130
Al Virof0c3b502013-05-16 12:07:31 -04002131 mm = get_task_mm(task);
2132 if (!mm)
2133 goto out_put_task;
2134 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002135
Al Virof0c3b502013-05-16 12:07:31 -04002136 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002137
Al Virof0c3b502013-05-16 12:07:31 -04002138 /*
2139 * We need two passes here:
2140 *
2141 * 1) Collect vmas of mapped files with mmap_sem taken
2142 * 2) Release mmap_sem and instantiate entries
2143 *
2144 * otherwise we get lockdep complained, since filldir()
2145 * routine might require mmap_sem taken in might_fault().
2146 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002147
Al Virof0c3b502013-05-16 12:07:31 -04002148 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2149 if (vma->vm_file && ++pos > ctx->pos)
2150 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002151 }
Al Virof0c3b502013-05-16 12:07:31 -04002152
2153 if (nr_files) {
2154 fa = flex_array_alloc(sizeof(info), nr_files,
2155 GFP_KERNEL);
2156 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2157 GFP_KERNEL)) {
2158 ret = -ENOMEM;
2159 if (fa)
2160 flex_array_free(fa);
2161 up_read(&mm->mmap_sem);
2162 mmput(mm);
2163 goto out_put_task;
2164 }
2165 for (i = 0, vma = mm->mmap, pos = 2; vma;
2166 vma = vma->vm_next) {
2167 if (!vma->vm_file)
2168 continue;
2169 if (++pos <= ctx->pos)
2170 continue;
2171
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002172 info.start = vma->vm_start;
2173 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002174 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002175 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2176 BUG();
2177 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002178 }
Al Virof0c3b502013-05-16 12:07:31 -04002179 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002180 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002181
2182 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002183 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2184 unsigned int len;
2185
Al Virof0c3b502013-05-16 12:07:31 -04002186 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002187 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002188 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002189 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002190 proc_map_files_instantiate,
2191 task,
2192 (void *)(unsigned long)p->mode))
2193 break;
2194 ctx->pos++;
2195 }
2196 if (fa)
2197 flex_array_free(fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002198
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002199out_put_task:
2200 put_task_struct(task);
2201out:
2202 return ret;
2203}
2204
2205static const struct file_operations proc_map_files_operations = {
2206 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002207 .iterate_shared = proc_map_files_readdir,
2208 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002209};
2210
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002211#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002212struct timers_private {
2213 struct pid *pid;
2214 struct task_struct *task;
2215 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002216 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002217 unsigned long flags;
2218};
2219
2220static void *timers_start(struct seq_file *m, loff_t *pos)
2221{
2222 struct timers_private *tp = m->private;
2223
2224 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2225 if (!tp->task)
2226 return ERR_PTR(-ESRCH);
2227
2228 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2229 if (!tp->sighand)
2230 return ERR_PTR(-ESRCH);
2231
2232 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2233}
2234
2235static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2236{
2237 struct timers_private *tp = m->private;
2238 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2239}
2240
2241static void timers_stop(struct seq_file *m, void *v)
2242{
2243 struct timers_private *tp = m->private;
2244
2245 if (tp->sighand) {
2246 unlock_task_sighand(tp->task, &tp->flags);
2247 tp->sighand = NULL;
2248 }
2249
2250 if (tp->task) {
2251 put_task_struct(tp->task);
2252 tp->task = NULL;
2253 }
2254}
2255
2256static int show_timer(struct seq_file *m, void *v)
2257{
2258 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002259 struct timers_private *tp = m->private;
2260 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002261 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002262 [SIGEV_SIGNAL] = "signal",
2263 [SIGEV_NONE] = "none",
2264 [SIGEV_THREAD] = "thread",
2265 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002266
2267 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002268 notify = timer->it_sigev_notify;
2269
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002270 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002271 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002272 timer->sigq->info.si_signo,
2273 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002274 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002275 nstr[notify & ~SIGEV_THREAD_ID],
2276 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2277 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002278 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002279
2280 return 0;
2281}
2282
2283static const struct seq_operations proc_timers_seq_ops = {
2284 .start = timers_start,
2285 .next = timers_next,
2286 .stop = timers_stop,
2287 .show = show_timer,
2288};
2289
2290static int proc_timers_open(struct inode *inode, struct file *file)
2291{
2292 struct timers_private *tp;
2293
2294 tp = __seq_open_private(file, &proc_timers_seq_ops,
2295 sizeof(struct timers_private));
2296 if (!tp)
2297 return -ENOMEM;
2298
2299 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002300 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002301 return 0;
2302}
2303
2304static const struct file_operations proc_timers_operations = {
2305 .open = proc_timers_open,
2306 .read = seq_read,
2307 .llseek = seq_lseek,
2308 .release = seq_release_private,
2309};
Eric Engestromb5946be2016-03-17 14:20:57 -07002310#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002311
John Stultz5de23d42016-03-17 14:20:54 -07002312static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2313 size_t count, loff_t *offset)
2314{
2315 struct inode *inode = file_inode(file);
2316 struct task_struct *p;
2317 u64 slack_ns;
2318 int err;
2319
2320 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2321 if (err < 0)
2322 return err;
2323
2324 p = get_proc_task(inode);
2325 if (!p)
2326 return -ESRCH;
2327
John Stultz4b2bd5f2016-10-07 17:02:33 -07002328 if (p != current) {
2329 if (!capable(CAP_SYS_NICE)) {
2330 count = -EPERM;
2331 goto out;
2332 }
John Stultz5de23d42016-03-17 14:20:54 -07002333
John Stultz4b2bd5f2016-10-07 17:02:33 -07002334 err = security_task_setscheduler(p);
2335 if (err) {
2336 count = err;
2337 goto out;
2338 }
John Stultz904763e2016-10-07 17:02:29 -07002339 }
2340
John Stultz7abbaf92016-10-07 17:02:26 -07002341 task_lock(p);
2342 if (slack_ns == 0)
2343 p->timer_slack_ns = p->default_timer_slack_ns;
2344 else
2345 p->timer_slack_ns = slack_ns;
2346 task_unlock(p);
2347
2348out:
John Stultz5de23d42016-03-17 14:20:54 -07002349 put_task_struct(p);
2350
2351 return count;
2352}
2353
2354static int timerslack_ns_show(struct seq_file *m, void *v)
2355{
2356 struct inode *inode = m->private;
2357 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002358 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002359
2360 p = get_proc_task(inode);
2361 if (!p)
2362 return -ESRCH;
2363
John Stultz4b2bd5f2016-10-07 17:02:33 -07002364 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002365
John Stultz4b2bd5f2016-10-07 17:02:33 -07002366 if (!capable(CAP_SYS_NICE)) {
2367 err = -EPERM;
2368 goto out;
2369 }
2370 err = security_task_getscheduler(p);
2371 if (err)
2372 goto out;
2373 }
John Stultz904763e2016-10-07 17:02:29 -07002374
John Stultz7abbaf92016-10-07 17:02:26 -07002375 task_lock(p);
2376 seq_printf(m, "%llu\n", p->timer_slack_ns);
2377 task_unlock(p);
2378
2379out:
John Stultz5de23d42016-03-17 14:20:54 -07002380 put_task_struct(p);
2381
2382 return err;
2383}
2384
2385static int timerslack_ns_open(struct inode *inode, struct file *filp)
2386{
2387 return single_open(filp, timerslack_ns_show, inode);
2388}
2389
2390static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2391 .open = timerslack_ns_open,
2392 .read = seq_read,
2393 .write = timerslack_ns_write,
2394 .llseek = seq_lseek,
2395 .release = single_release,
2396};
2397
Al Viro0168b9e2018-05-03 09:21:05 -04002398static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2399 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002400{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002401 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002402 struct inode *inode;
2403 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002404
Al Viro0168b9e2018-05-03 09:21:05 -04002405 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002406 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002407 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002408
2409 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002410 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002411 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002412 if (p->iop)
2413 inode->i_op = p->iop;
2414 if (p->fop)
2415 inode->i_fop = p->fop;
2416 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002417 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002418 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002419 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002420}
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422static struct dentry *proc_pident_lookup(struct inode *dir,
2423 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002424 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002425 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002427 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002428 const struct pid_entry *p, *last;
Al Viro0168b9e2018-05-03 09:21:05 -04002429 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Eric W. Biederman99f89552006-06-26 00:25:55 -07002431 if (!task)
2432 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002434 /*
2435 * Yes, it does not scale. And it should not. Don't add
2436 * new entries into /proc/<tgid>/ without very good reasons.
2437 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002438 last = &ents[nents];
2439 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (p->len != dentry->d_name.len)
2441 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002442 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2443 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002447 put_task_struct(task);
2448out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002449 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
Al Virof0c3b502013-05-16 12:07:31 -04002452static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002453 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002454{
Al Virof0c3b502013-05-16 12:07:31 -04002455 struct task_struct *task = get_proc_task(file_inode(file));
2456 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002457
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002458 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002459 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002460
Al Virof0c3b502013-05-16 12:07:31 -04002461 if (!dir_emit_dots(file, ctx))
2462 goto out;
2463
2464 if (ctx->pos >= nents + 2)
2465 goto out;
2466
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002467 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002468 if (!proc_fill_cache(file, ctx, p->name, p->len,
2469 proc_pident_instantiate, task, p))
2470 break;
2471 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002472 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002473out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002474 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002479static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2480 size_t count, loff_t *ppos)
2481{
Al Viro496ad9a2013-01-23 17:07:38 -05002482 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002483 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002484 ssize_t length;
2485 struct task_struct *task = get_proc_task(inode);
2486
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002487 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002488 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002489
2490 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002491 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002492 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002493 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002494 if (length > 0)
2495 length = simple_read_from_buffer(buf, count, ppos, p, length);
2496 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002497 return length;
2498}
2499
2500static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2501 size_t count, loff_t *ppos)
2502{
Al Viro496ad9a2013-01-23 17:07:38 -05002503 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002504 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505 ssize_t length;
2506 struct task_struct *task = get_proc_task(inode);
2507
2508 length = -ESRCH;
2509 if (!task)
2510 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002511
2512 /* A task may only write its own attributes. */
2513 length = -EACCES;
2514 if (current != task)
2515 goto out;
2516
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002517 if (count > PAGE_SIZE)
2518 count = PAGE_SIZE;
2519
2520 /* No partial writes. */
2521 length = -EINVAL;
2522 if (*ppos != 0)
2523 goto out;
2524
Al Virobb646cd2015-12-24 00:16:30 -05002525 page = memdup_user(buf, count);
2526 if (IS_ERR(page)) {
2527 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002528 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002529 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002530
David Howells107db7c2009-05-08 13:55:27 +01002531 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002532 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002533 if (length < 0)
2534 goto out_free;
2535
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002536 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002537 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002538 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002539out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002540 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002541out:
2542 put_task_struct(task);
2543out_no_task:
2544 return length;
2545}
2546
Arjan van de Ven00977a52007-02-12 00:55:34 -08002547static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002548 .read = proc_pid_attr_read,
2549 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002550 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551};
2552
Eric Dumazetc5141e62007-05-08 00:26:15 -07002553static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002554 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2555 REG("prev", S_IRUGO, proc_pid_attr_operations),
2556 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2557 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2558 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2559 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002560};
2561
Al Virof0c3b502013-05-16 12:07:31 -04002562static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Al Virof0c3b502013-05-16 12:07:31 -04002564 return proc_pident_readdir(file, ctx,
2565 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
Arjan van de Ven00977a52007-02-12 00:55:34 -08002568static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002570 .iterate_shared = proc_attr_dir_readdir,
2571 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572};
2573
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002574static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002575 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002577 return proc_pident_lookup(dir, dentry,
2578 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002581static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002582 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002583 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002584 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585};
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587#endif
2588
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002589#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002590static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2591 size_t count, loff_t *ppos)
2592{
Al Viro496ad9a2013-01-23 17:07:38 -05002593 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002594 struct mm_struct *mm;
2595 char buffer[PROC_NUMBUF];
2596 size_t len;
2597 int ret;
2598
2599 if (!task)
2600 return -ESRCH;
2601
2602 ret = 0;
2603 mm = get_task_mm(task);
2604 if (mm) {
2605 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2606 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2607 MMF_DUMP_FILTER_SHIFT));
2608 mmput(mm);
2609 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2610 }
2611
2612 put_task_struct(task);
2613
2614 return ret;
2615}
2616
2617static ssize_t proc_coredump_filter_write(struct file *file,
2618 const char __user *buf,
2619 size_t count,
2620 loff_t *ppos)
2621{
2622 struct task_struct *task;
2623 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002624 unsigned int val;
2625 int ret;
2626 int i;
2627 unsigned long mask;
2628
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002629 ret = kstrtouint_from_user(buf, count, 0, &val);
2630 if (ret < 0)
2631 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002632
2633 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002634 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002635 if (!task)
2636 goto out_no_task;
2637
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002638 mm = get_task_mm(task);
2639 if (!mm)
2640 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002641 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002642
2643 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2644 if (val & mask)
2645 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2646 else
2647 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2648 }
2649
2650 mmput(mm);
2651 out_no_mm:
2652 put_task_struct(task);
2653 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002654 if (ret < 0)
2655 return ret;
2656 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002657}
2658
2659static const struct file_operations proc_coredump_filter_operations = {
2660 .read = proc_coredump_filter_read,
2661 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002662 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002663};
2664#endif
2665
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002666#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002667static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002668{
Andrea Righi940389b2008-07-28 00:48:12 +02002669 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002670 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002671 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002672
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002673 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2674 if (result)
2675 return result;
2676
Jann Horncaaee622016-01-20 15:00:04 -08002677 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002678 result = -EACCES;
2679 goto out_unlock;
2680 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002681
Andrea Righi59954772008-07-27 17:29:15 +02002682 if (whole && lock_task_sighand(task, &flags)) {
2683 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002684
Andrea Righi59954772008-07-27 17:29:15 +02002685 task_io_accounting_add(&acct, &task->signal->ioac);
2686 while_each_thread(task, t)
2687 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002688
Andrea Righi59954772008-07-27 17:29:15 +02002689 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002690 }
Joe Perches25ce3192015-04-15 16:18:17 -07002691 seq_printf(m,
2692 "rchar: %llu\n"
2693 "wchar: %llu\n"
2694 "syscr: %llu\n"
2695 "syscw: %llu\n"
2696 "read_bytes: %llu\n"
2697 "write_bytes: %llu\n"
2698 "cancelled_write_bytes: %llu\n",
2699 (unsigned long long)acct.rchar,
2700 (unsigned long long)acct.wchar,
2701 (unsigned long long)acct.syscr,
2702 (unsigned long long)acct.syscw,
2703 (unsigned long long)acct.read_bytes,
2704 (unsigned long long)acct.write_bytes,
2705 (unsigned long long)acct.cancelled_write_bytes);
2706 result = 0;
2707
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002708out_unlock:
2709 mutex_unlock(&task->signal->cred_guard_mutex);
2710 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002711}
Andrea Righi297c5d92008-07-25 01:48:49 -07002712
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002713static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2714 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002715{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002716 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002717}
2718
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002719static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2720 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002721{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002722 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002723}
2724#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002725
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002726#ifdef CONFIG_USER_NS
2727static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002728 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002729{
2730 struct user_namespace *ns = NULL;
2731 struct task_struct *task;
2732 struct seq_file *seq;
2733 int ret = -EINVAL;
2734
2735 task = get_proc_task(inode);
2736 if (task) {
2737 rcu_read_lock();
2738 ns = get_user_ns(task_cred_xxx(task, user_ns));
2739 rcu_read_unlock();
2740 put_task_struct(task);
2741 }
2742 if (!ns)
2743 goto err;
2744
2745 ret = seq_open(file, seq_ops);
2746 if (ret)
2747 goto err_put_ns;
2748
2749 seq = file->private_data;
2750 seq->private = ns;
2751
2752 return 0;
2753err_put_ns:
2754 put_user_ns(ns);
2755err:
2756 return ret;
2757}
2758
2759static int proc_id_map_release(struct inode *inode, struct file *file)
2760{
2761 struct seq_file *seq = file->private_data;
2762 struct user_namespace *ns = seq->private;
2763 put_user_ns(ns);
2764 return seq_release(inode, file);
2765}
2766
2767static int proc_uid_map_open(struct inode *inode, struct file *file)
2768{
2769 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2770}
2771
2772static int proc_gid_map_open(struct inode *inode, struct file *file)
2773{
2774 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2775}
2776
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002777static int proc_projid_map_open(struct inode *inode, struct file *file)
2778{
2779 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2780}
2781
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002782static const struct file_operations proc_uid_map_operations = {
2783 .open = proc_uid_map_open,
2784 .write = proc_uid_map_write,
2785 .read = seq_read,
2786 .llseek = seq_lseek,
2787 .release = proc_id_map_release,
2788};
2789
2790static const struct file_operations proc_gid_map_operations = {
2791 .open = proc_gid_map_open,
2792 .write = proc_gid_map_write,
2793 .read = seq_read,
2794 .llseek = seq_lseek,
2795 .release = proc_id_map_release,
2796};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002797
2798static const struct file_operations proc_projid_map_operations = {
2799 .open = proc_projid_map_open,
2800 .write = proc_projid_map_write,
2801 .read = seq_read,
2802 .llseek = seq_lseek,
2803 .release = proc_id_map_release,
2804};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002805
2806static int proc_setgroups_open(struct inode *inode, struct file *file)
2807{
2808 struct user_namespace *ns = NULL;
2809 struct task_struct *task;
2810 int ret;
2811
2812 ret = -ESRCH;
2813 task = get_proc_task(inode);
2814 if (task) {
2815 rcu_read_lock();
2816 ns = get_user_ns(task_cred_xxx(task, user_ns));
2817 rcu_read_unlock();
2818 put_task_struct(task);
2819 }
2820 if (!ns)
2821 goto err;
2822
2823 if (file->f_mode & FMODE_WRITE) {
2824 ret = -EACCES;
2825 if (!ns_capable(ns, CAP_SYS_ADMIN))
2826 goto err_put_ns;
2827 }
2828
2829 ret = single_open(file, &proc_setgroups_show, ns);
2830 if (ret)
2831 goto err_put_ns;
2832
2833 return 0;
2834err_put_ns:
2835 put_user_ns(ns);
2836err:
2837 return ret;
2838}
2839
2840static int proc_setgroups_release(struct inode *inode, struct file *file)
2841{
2842 struct seq_file *seq = file->private_data;
2843 struct user_namespace *ns = seq->private;
2844 int ret = single_release(inode, file);
2845 put_user_ns(ns);
2846 return ret;
2847}
2848
2849static const struct file_operations proc_setgroups_operations = {
2850 .open = proc_setgroups_open,
2851 .write = proc_setgroups_write,
2852 .read = seq_read,
2853 .llseek = seq_lseek,
2854 .release = proc_setgroups_release,
2855};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002856#endif /* CONFIG_USER_NS */
2857
Kees Cook47830722008-10-06 03:11:58 +04002858static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2859 struct pid *pid, struct task_struct *task)
2860{
Al Viroa9712bc2011-03-23 15:52:50 -04002861 int err = lock_trace(task);
2862 if (!err) {
2863 seq_printf(m, "%08x\n", task->personality);
2864 unlock_trace(task);
2865 }
2866 return err;
Kees Cook47830722008-10-06 03:11:58 +04002867}
2868
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002869#ifdef CONFIG_LIVEPATCH
2870static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2871 struct pid *pid, struct task_struct *task)
2872{
2873 seq_printf(m, "%d\n", task->patch_state);
2874 return 0;
2875}
2876#endif /* CONFIG_LIVEPATCH */
2877
Eric W. Biederman801199c2006-10-02 02:18:48 -07002878/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002879 * Thread groups
2880 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002881static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002882static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002883
Eric Dumazetc5141e62007-05-08 00:26:15 -07002884static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002885 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2886 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002887 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002888 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002889 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002890#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002891 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002892#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002893 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002894 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002895 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002896 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002897 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002898#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002899 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002900#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002901#ifdef CONFIG_SCHED_AUTOGROUP
2902 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2903#endif
john stultz4614a696b2009-12-14 18:00:05 -08002904 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002905#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002906 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002907#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002908 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002909 ONE("stat", S_IRUGO, proc_tgid_stat),
2910 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002911 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002912#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002913 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002914#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002915 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2916 LNK("cwd", proc_cwd_link),
2917 LNK("root", proc_root_link),
2918 LNK("exe", proc_exe_link),
2919 REG("mounts", S_IRUGO, proc_mounts_operations),
2920 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2921 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002922#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002924 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002925 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002926 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002927#endif
2928#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002929 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002930#endif
2931#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002932 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002934#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002935 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002936#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302937#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002938 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002940#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002941 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002942#endif
Paul Menage8793d852007-10-18 23:39:39 -07002943#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002944 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945#endif
Paul Menagea4243162007-10-18 23:39:35 -07002946#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002947 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002948#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002949 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002950 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002951 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002952#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002953 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2954 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002955#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002956#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002957 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002958 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002959#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002960#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002961 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002962#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002963#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002964 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002965#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002966#ifdef CONFIG_USER_NS
2967 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2968 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002969 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002970 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002971#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002972#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002973 REG("timers", S_IRUGO, proc_timers_operations),
2974#endif
John Stultz5de23d42016-03-17 14:20:54 -07002975 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002976#ifdef CONFIG_LIVEPATCH
2977 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
2978#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002979};
2980
Al Virof0c3b502013-05-16 12:07:31 -04002981static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982{
Al Virof0c3b502013-05-16 12:07:31 -04002983 return proc_pident_readdir(file, ctx,
2984 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002985}
2986
Arjan van de Ven00977a52007-02-12 00:55:34 -08002987static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002988 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002989 .iterate_shared = proc_tgid_base_readdir,
2990 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002991};
2992
Al Viro00cd8dd2012-06-10 17:13:09 -04002993static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2994{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002995 return proc_pident_lookup(dir, dentry,
2996 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002997}
2998
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002999static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003000 .lookup = proc_tgid_base_lookup,
3001 .getattr = pid_getattr,
3002 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003003 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003004};
3005
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003006static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003008 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003009 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003010 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Eric W. Biederman48e64842006-06-26 00:25:48 -07003012 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003013 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003014 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003015 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003016 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003017 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003018 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003021 if (pid == tgid)
3022 return;
3023
Eric W. Biederman48e64842006-06-26 00:25:48 -07003024 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003025 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003026 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003027 if (!leader)
3028 goto out;
3029
3030 name.name = "task";
3031 name.len = strlen(name.name);
3032 dir = d_hash_and_lookup(leader, &name);
3033 if (!dir)
3034 goto out_put_leader;
3035
3036 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003037 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003038 dentry = d_hash_and_lookup(dir, &name);
3039 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003040 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003041 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003043
3044 dput(dir);
3045out_put_leader:
3046 dput(leader);
3047out:
3048 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Randy Dunlap0895e912007-10-21 21:00:10 -07003051/**
3052 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3053 * @task: task that should be flushed.
3054 *
3055 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003056 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003057 * in. This call is supposed to do all of this job.
3058 *
3059 * Looks in the dcache for
3060 * /proc/@pid
3061 * /proc/@tgid/task/@pid
3062 * if either directory is present flushes it and all of it'ts children
3063 * from the dcache.
3064 *
3065 * It is safe and reasonable to cache /proc entries for a task until
3066 * that task exits. After that they just clog up the dcache with
3067 * useless entries, possibly causing useful dcache entries to be
3068 * flushed instead. This routine is proved to flush those useless
3069 * dcache entries at process exit time.
3070 *
3071 * NOTE: This routine is just an optimization so it does not guarantee
3072 * that no dcache entries will exist at process exit time it
3073 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003074 */
3075
3076void proc_flush_task(struct task_struct *task)
3077{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003078 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003079 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003080 struct upid *upid;
3081
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003082 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003083 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003084
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003085 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003086 upid = &pid->numbers[i];
3087 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003088 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003089 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003090}
3091
Al Viro0168b9e2018-05-03 09:21:05 -04003092static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003093 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003094{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003095 struct inode *inode;
3096
Al Viro0168b9e2018-05-03 09:21:05 -04003097 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003098 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003099 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003100
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003101 inode->i_op = &proc_tgid_base_inode_operations;
3102 inode->i_fop = &proc_tgid_base_operations;
3103 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003104
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003105 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003106 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003107
Nick Pigginfb045ad2011-01-07 17:49:55 +11003108 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003109 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003110}
3111
Al Viro00cd8dd2012-06-10 17:13:09 -04003112struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003116 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003117 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003119 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (tgid == ~0U)
3121 goto out;
3122
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003123 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003124 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003125 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (task)
3127 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003128 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (!task)
3130 goto out;
3131
Al Viro0168b9e2018-05-03 09:21:05 -04003132 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003133 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134out:
Al Viro0168b9e2018-05-03 09:21:05 -04003135 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003139 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003140 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003142struct tgid_iter {
3143 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003144 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003145};
3146static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3147{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003148 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003150 if (iter.task)
3151 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003152 rcu_read_lock();
3153retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003154 iter.task = NULL;
3155 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003156 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003157 iter.tgid = pid_nr_ns(pid, ns);
3158 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003159 /* What we to know is if the pid we have find is the
3160 * pid of a thread_group_leader. Testing for task
3161 * being a thread_group_leader is the obvious thing
3162 * todo but there is a window when it fails, due to
3163 * the pid transfer logic in de_thread.
3164 *
3165 * So we perform the straight forward test of seeing
3166 * if the pid we have found is the pid of a thread
3167 * group leader, and don't worry if the task we have
3168 * found doesn't happen to be a thread group leader.
3169 * As we don't care in the case of readdir.
3170 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003171 if (!iter.task || !has_group_leader_pid(iter.task)) {
3172 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003173 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003174 }
3175 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003177 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003178 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
Eric W. Biederman00978752014-07-31 03:10:50 -07003181#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003184int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003186 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003187 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003188 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Al Viro021ada72013-03-29 19:27:05 -04003190 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Eric W. Biederman00978752014-07-31 03:10:50 -07003193 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003194 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003195 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003196 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003197 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003198 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003199 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003200 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003201 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3202 return 0;
3203 ctx->pos = pos = pos + 1;
3204 }
3205 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003206 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003207 for (iter = next_tgid(ns, iter);
3208 iter.task;
3209 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003210 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003211 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003212
3213 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003214 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003215 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003216
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003217 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003218 ctx->pos = iter.tgid + TGID_OFFSET;
3219 if (!proc_fill_cache(file, ctx, name, len,
3220 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003221 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
Al Virof0c3b502013-05-16 12:07:31 -04003225 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return 0;
3227}
3228
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003229/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003230 * proc_tid_comm_permission is a special permission function exclusively
3231 * used for the node /proc/<pid>/task/<tid>/comm.
3232 * It bypasses generic permission checks in the case where a task of the same
3233 * task group attempts to access the node.
3234 * The rationale behind this is that glibc and bionic access this node for
3235 * cross thread naming (pthread_set/getname_np(!self)). However, if
3236 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3237 * which locks out the cross thread naming implementation.
3238 * This function makes sure that the node is always accessible for members of
3239 * same thread group.
3240 */
3241static int proc_tid_comm_permission(struct inode *inode, int mask)
3242{
3243 bool is_same_tgroup;
3244 struct task_struct *task;
3245
3246 task = get_proc_task(inode);
3247 if (!task)
3248 return -ESRCH;
3249 is_same_tgroup = same_thread_group(current, task);
3250 put_task_struct(task);
3251
3252 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3253 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3254 * read or written by the members of the corresponding
3255 * thread group.
3256 */
3257 return 0;
3258 }
3259
3260 return generic_permission(inode, mask);
3261}
3262
3263static const struct inode_operations proc_tid_comm_inode_operations = {
3264 .permission = proc_tid_comm_permission,
3265};
3266
3267/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003268 * Tasks
3269 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003270static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003271 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003272 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003273 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003274#ifdef CONFIG_NET
3275 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3276#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003277 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003278 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003279 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003280 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003281 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003282#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003283 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003284#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003285 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3286 &proc_tid_comm_inode_operations,
3287 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003288#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003289 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003290#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003291 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003292 ONE("stat", S_IRUGO, proc_tid_stat),
3293 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003294 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003295#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003296 REG("children", S_IRUGO, proc_tid_children_operations),
3297#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003298#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003299 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003300#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003301 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3302 LNK("cwd", proc_cwd_link),
3303 LNK("root", proc_root_link),
3304 LNK("exe", proc_exe_link),
3305 REG("mounts", S_IRUGO, proc_mounts_operations),
3306 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003307#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003308 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003309 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003310 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003311 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003312#endif
3313#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003314 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003315#endif
3316#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003317 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003318#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003319#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003320 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003321#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303322#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003323 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003324#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003325#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003326 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003327#endif
Paul Menage8793d852007-10-18 23:39:39 -07003328#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003329 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003330#endif
Paul Menagea4243162007-10-18 23:39:35 -07003331#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003332 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003333#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003334 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003335 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003336 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003337#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003338 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003339 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003340#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003341#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003342 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003343 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003344#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003345#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003346 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003347#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003348#ifdef CONFIG_USER_NS
3349 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3350 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003351 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003352 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003353#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003354#ifdef CONFIG_LIVEPATCH
3355 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3356#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003357};
3358
Al Virof0c3b502013-05-16 12:07:31 -04003359static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003360{
Al Virof0c3b502013-05-16 12:07:31 -04003361 return proc_pident_readdir(file, ctx,
3362 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003363}
3364
Al Viro00cd8dd2012-06-10 17:13:09 -04003365static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3366{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003367 return proc_pident_lookup(dir, dentry,
3368 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003369}
3370
Arjan van de Ven00977a52007-02-12 00:55:34 -08003371static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003372 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003373 .iterate_shared = proc_tid_base_readdir,
3374 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003375};
3376
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003377static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003378 .lookup = proc_tid_base_lookup,
3379 .getattr = pid_getattr,
3380 .setattr = proc_setattr,
3381};
3382
Al Viro0168b9e2018-05-03 09:21:05 -04003383static struct dentry *proc_task_instantiate(struct dentry *dentry,
3384 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003385{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003386 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003387 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003388 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003389 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003390
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003391 inode->i_op = &proc_tid_base_inode_operations;
3392 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003393 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003394
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003395 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003396 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003397
Nick Pigginfb045ad2011-01-07 17:49:55 +11003398 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003399 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003400}
3401
Al Viro00cd8dd2012-06-10 17:13:09 -04003402static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003403{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003404 struct task_struct *task;
3405 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003406 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003407 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003408 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003409
3410 if (!leader)
3411 goto out_no_task;
3412
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003413 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003414 if (tid == ~0U)
3415 goto out;
3416
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003417 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003418 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003419 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003420 if (task)
3421 get_task_struct(task);
3422 rcu_read_unlock();
3423 if (!task)
3424 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003425 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003426 goto out_drop_task;
3427
Al Viro0168b9e2018-05-03 09:21:05 -04003428 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003429out_drop_task:
3430 put_task_struct(task);
3431out:
3432 put_task_struct(leader);
3433out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003434 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003435}
3436
3437/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003438 * Find the first tid of a thread group to return to user space.
3439 *
3440 * Usually this is just the thread group leader, but if the users
3441 * buffer was too small or there was a seek into the middle of the
3442 * directory we have more work todo.
3443 *
3444 * In the case of a short read we start with find_task_by_pid.
3445 *
3446 * In the case of a seek we start with the leader and walk nr
3447 * threads past it.
3448 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003449static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3450 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003451{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003452 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003453 unsigned long nr = f_pos;
3454
3455 if (nr != f_pos) /* 32bit overflow? */
3456 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003457
Eric W. Biedermancc288732006-06-26 00:26:01 -07003458 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003459 task = pid_task(pid, PIDTYPE_PID);
3460 if (!task)
3461 goto fail;
3462
3463 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003464 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003465 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003466 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003467 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003468 }
3469
3470 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003471 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003472 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003473
3474 /* If we haven't found our starting place yet start
3475 * with the leader and walk nr threads forward.
3476 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003477 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003478 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003479 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003480 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003481 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003482fail:
3483 pos = NULL;
3484 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003485found:
3486 get_task_struct(pos);
3487out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003488 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003489 return pos;
3490}
3491
3492/*
3493 * Find the next thread in the thread list.
3494 * Return NULL if there is an error or no next thread.
3495 *
3496 * The reference to the input task_struct is released.
3497 */
3498static struct task_struct *next_tid(struct task_struct *start)
3499{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003500 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003501 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003502 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003503 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003504 if (thread_group_leader(pos))
3505 pos = NULL;
3506 else
3507 get_task_struct(pos);
3508 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003509 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003510 put_task_struct(start);
3511 return pos;
3512}
3513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003515static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003517 struct inode *inode = file_inode(file);
3518 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003519 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003520 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003522 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003523 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Al Virof0c3b502013-05-16 12:07:31 -04003525 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003528 /* f_version caches the tgid value that the last readdir call couldn't
3529 * return. lseek aka telldir automagically resets f_version to 0.
3530 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003531 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003532 tid = (int)file->f_version;
3533 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003534 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003535 task;
Al Virof0c3b502013-05-16 12:07:31 -04003536 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003537 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003538 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003539 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003540 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003541 if (!proc_fill_cache(file, ctx, name, len,
3542 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003543 /* returning this tgid failed, save it as the first
3544 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003545 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003546 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003550
Al Virof0c3b502013-05-16 12:07:31 -04003551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003553
David Howellsa528d352017-01-31 16:46:22 +00003554static int proc_task_getattr(const struct path *path, struct kstat *stat,
3555 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003556{
David Howellsa528d352017-01-31 16:46:22 +00003557 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003558 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003559 generic_fillattr(inode, stat);
3560
Eric W. Biederman99f89552006-06-26 00:25:55 -07003561 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003562 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003563 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003564 }
3565
3566 return 0;
3567}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003568
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003569static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003570 .lookup = proc_task_lookup,
3571 .getattr = proc_task_getattr,
3572 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003573 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003574};
3575
Arjan van de Ven00977a52007-02-12 00:55:34 -08003576static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003577 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003578 .iterate_shared = proc_task_readdir,
3579 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003580};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003581
3582void __init set_proc_pid_nlink(void)
3583{
3584 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3585 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3586}