blob: 7e9f07bf260d20bb0a0cd4cd6b6b4abe82b23e20 [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 Torvaldsf5b65342018-06-20 09:47:20 +0900238 /* .. and limit it to a maximum of one page of slop */
239 if (env_end >= arg_end + PAGE_SIZE)
240 env_end = arg_end + PAGE_SIZE - 1;
241
Linus Torvalds5ab82712018-05-17 15:17:33 -0700242 /* We're not going to care if "*ppos" has high bits set */
243 pos = arg_start + *ppos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700244
Linus Torvalds5ab82712018-05-17 15:17:33 -0700245 /* .. but we do check the result is in the proper range */
246 if (pos < arg_start || pos >= env_end)
247 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700248
Linus Torvalds5ab82712018-05-17 15:17:33 -0700249 /* .. and we never go past env_end */
250 if (env_end - pos < count)
251 count = env_end - pos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700252
Linus Torvalds5ab82712018-05-17 15:17:33 -0700253 page = (char *)__get_free_page(GFP_KERNEL);
254 if (!page)
255 return -ENOMEM;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700256
Linus Torvalds5ab82712018-05-17 15:17:33 -0700257 len = 0;
258 while (count) {
259 int got;
260 size_t size = min_t(size_t, PAGE_SIZE, count);
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900261 long offset;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700262
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900263 /*
264 * Are we already starting past the official end?
265 * We always include the last byte that is *supposed*
266 * to be NUL
267 */
268 offset = (pos >= arg_end) ? pos - arg_end + 1 : 0;
269
270 got = access_remote_vm(mm, pos - offset, page, size + offset, FOLL_ANON);
271 if (got <= offset)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700272 break;
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900273 got -= offset;
Linus Torvalds5ab82712018-05-17 15:17:33 -0700274
275 /* Don't walk past a NUL character once you hit arg_end */
276 if (pos + got >= arg_end) {
277 int n = 0;
278
279 /*
280 * If we started before 'arg_end' but ended up
281 * at or after it, we start the NUL character
282 * check at arg_end-1 (where we expect the normal
283 * EOF to be).
284 *
285 * NOTE! This is smaller than 'got', because
286 * pos + got >= arg_end
287 */
288 if (pos < arg_end)
289 n = arg_end - pos - 1;
290
291 /* Cut off at first NUL after 'n' */
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900292 got = n + strnlen(page+n, offset+got-n);
293 if (got < offset)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700294 break;
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900295 got -= offset;
296
297 /* Include the NUL if it existed */
298 if (got < size)
299 got++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700300 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800301
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900302 got -= copy_to_user(buf, page+offset, got);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700303 if (unlikely(!got)) {
304 if (!len)
305 len = -EFAULT;
306 break;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700307 }
Linus Torvalds5ab82712018-05-17 15:17:33 -0700308 pos += got;
309 buf += got;
310 len += got;
311 count -= got;
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700312 }
313
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700314 free_page((unsigned long)page);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700315 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700318static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
319 size_t count, loff_t *pos)
320{
321 struct mm_struct *mm;
322 ssize_t ret;
323
324 mm = get_task_mm(tsk);
325 if (!mm)
326 return 0;
327
328 ret = get_mm_cmdline(mm, buf, count, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 mmput(mm);
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700330 return ret;
331}
332
333static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
334 size_t count, loff_t *pos)
335{
336 struct task_struct *tsk;
337 ssize_t ret;
338
339 BUG_ON(*pos < 0);
340
341 tsk = get_proc_task(file_inode(file));
342 if (!tsk)
343 return -ESRCH;
344 ret = get_task_cmdline(tsk, buf, count, pos);
345 put_task_struct(tsk);
346 if (ret > 0)
347 *pos += ret;
348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700351static const struct file_operations proc_pid_cmdline_ops = {
352 .read = proc_pid_cmdline_read,
353 .llseek = generic_file_llseek,
354};
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#ifdef CONFIG_KALLSYMS
357/*
358 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
359 * Returns the resolved symbol. If that fails, simply return the address.
360 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700361static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
362 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700364 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700365 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700367 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
368 goto print0;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700371 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700372 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700373 return 0;
374 }
Joe Perches25ce3192015-04-15 16:18:17 -0700375
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700376print0:
377 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380#endif /* CONFIG_KALLSYMS */
381
Al Viroa9712bc2011-03-23 15:52:50 -0400382static int lock_trace(struct task_struct *task)
383{
384 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
385 if (err)
386 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800387 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400388 mutex_unlock(&task->signal->cred_guard_mutex);
389 return -EPERM;
390 }
391 return 0;
392}
393
394static void unlock_trace(struct task_struct *task)
395{
396 mutex_unlock(&task->signal->cred_guard_mutex);
397}
398
Ken Chen2ec220e2008-11-10 11:26:08 +0300399#ifdef CONFIG_STACKTRACE
400
401#define MAX_STACK_TRACE_DEPTH 64
402
403static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
404 struct pid *pid, struct task_struct *task)
405{
406 struct stack_trace trace;
407 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400408 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300409
Jann Hornf8a00ce2018-10-05 15:51:58 -0700410 /*
411 * The ability to racily run the kernel stack unwinder on a running task
412 * and then observe the unwinder output is scary; while it is useful for
413 * debugging kernel issues, it can also allow an attacker to leak kernel
414 * stack contents.
415 * Doing this in a manner that is at least safe from races would require
416 * some work to ensure that the remote task can not be scheduled; and
417 * even then, this would still expose the unwinder as local attack
418 * surface.
419 * Therefore, this interface is restricted to root.
420 */
421 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
422 return -EACCES;
423
Kees Cook6da2ec52018-06-12 13:55:00 -0700424 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
425 GFP_KERNEL);
Ken Chen2ec220e2008-11-10 11:26:08 +0300426 if (!entries)
427 return -ENOMEM;
428
429 trace.nr_entries = 0;
430 trace.max_entries = MAX_STACK_TRACE_DEPTH;
431 trace.entries = entries;
432 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300433
Al Viroa9712bc2011-03-23 15:52:50 -0400434 err = lock_trace(task);
435 if (!err) {
Alexey Dobriyan5d008fb2018-06-07 17:10:17 -0700436 unsigned int i;
437
Al Viroa9712bc2011-03-23 15:52:50 -0400438 save_stack_trace_tsk(task, &trace);
439
440 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800441 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400442 }
443 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300444 }
445 kfree(entries);
446
Al Viroa9712bc2011-03-23 15:52:50 -0400447 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300448}
449#endif
450
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530451#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*
453 * Provides /proc/PID/schedstat
454 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700455static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
456 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530458 if (unlikely(!sched_info_on()))
459 seq_printf(m, "0 0 0\n");
460 else
461 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700462 (unsigned long long)task->se.sum_exec_runtime,
463 (unsigned long long)task->sched_info.run_delay,
464 task->sched_info.pcount);
465
466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468#endif
469
Arjan van de Ven97455122008-01-25 21:08:34 +0100470#ifdef CONFIG_LATENCYTOP
471static int lstats_show_proc(struct seq_file *m, void *v)
472{
473 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800474 struct inode *inode = m->private;
475 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100476
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800477 if (!task)
478 return -ESRCH;
479 seq_puts(m, "Latency Top version : v0.1\n");
Alexey Dobriyanf6d2f582018-08-21 21:54:34 -0700480 for (i = 0; i < LT_SAVECOUNT; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800481 struct latency_record *lr = &task->latency_record[i];
482 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100483 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800484 seq_printf(m, "%i %li %li",
485 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100486 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800487 unsigned long bt = lr->backtrace[q];
488 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100489 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800490 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100491 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800492 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100493 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800494 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100495 }
496
497 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800498 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100499 return 0;
500}
501
502static int lstats_open(struct inode *inode, struct file *file)
503{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800504 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800505}
506
Arjan van de Ven97455122008-01-25 21:08:34 +0100507static ssize_t lstats_write(struct file *file, const char __user *buf,
508 size_t count, loff_t *offs)
509{
Al Viro496ad9a2013-01-23 17:07:38 -0500510 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100511
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800512 if (!task)
513 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100514 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800515 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100516
517 return count;
518}
519
520static const struct file_operations proc_lstats_operations = {
521 .open = lstats_open,
522 .read = seq_read,
523 .write = lstats_write,
524 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800525 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100526};
527
528#endif
529
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700530static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
531 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
David Rientjesa7f638f2012-05-29 15:06:47 -0700533 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200534 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Oleg Nesterovef419392016-08-02 14:03:19 -0700536 points = oom_badness(task, NULL, NULL, totalpages) *
537 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700538 seq_printf(m, "%lu\n", points);
539
540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Neil Hormand85f50d2007-10-18 23:40:37 -0700543struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700544 const char *name;
545 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700546};
547
548static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700549 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700550 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
551 [RLIMIT_DATA] = {"Max data size", "bytes"},
552 [RLIMIT_STACK] = {"Max stack size", "bytes"},
553 [RLIMIT_CORE] = {"Max core file size", "bytes"},
554 [RLIMIT_RSS] = {"Max resident set", "bytes"},
555 [RLIMIT_NPROC] = {"Max processes", "processes"},
556 [RLIMIT_NOFILE] = {"Max open files", "files"},
557 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
558 [RLIMIT_AS] = {"Max address space", "bytes"},
559 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
560 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
561 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
562 [RLIMIT_NICE] = {"Max nice priority", NULL},
563 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800564 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700565};
566
567/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700568static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
569 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700570{
571 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700572 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700573
574 struct rlimit rlim[RLIM_NLIMITS];
575
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400576 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700577 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700578 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
579 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700580
581 /*
582 * print the file header
583 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700584 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700585 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700586
587 for (i = 0; i < RLIM_NLIMITS; i++) {
588 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700589 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700590 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700591 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700592 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700593 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700594
595 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700596 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700597 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700598 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700599
600 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700601 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700602 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700603 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700604 }
605
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700606 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700607}
608
Roland McGrathebcb6732008-07-25 19:46:00 -0700609#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700610static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
611 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700612{
613 long nr;
614 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700615 int res;
616
617 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400618 if (res)
619 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700620
621 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700622 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400623 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700624 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400625 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700626 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700627 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
628 nr,
629 args[0], args[1], args[2], args[3], args[4], args[5],
630 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400631 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700632
633 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700634}
635#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637/************************************************************************/
638/* Here the fs part begins */
639/************************************************************************/
640
641/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700642static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700644 struct task_struct *task;
645 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700646 /* Allow access to a task's file descriptors if it is us or we
647 * may use ptrace attach to the process and find out that
648 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700649 */
650 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700651 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800652 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700653 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700654 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700655 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800658int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700659{
660 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000661 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700662
663 if (attr->ia_valid & ATTR_MODE)
664 return -EPERM;
665
Jan Kara31051c82016-05-26 16:55:18 +0200666 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200667 if (error)
668 return error;
669
Christoph Hellwig10257742010-06-04 11:30:02 +0200670 setattr_copy(inode, attr);
671 mark_inode_dirty(inode);
672 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700673}
674
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800675/*
676 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
677 * or euid/egid (for hide_pid_min=2)?
678 */
679static bool has_pid_permissions(struct pid_namespace *pid,
680 struct task_struct *task,
681 int hide_pid_min)
682{
683 if (pid->hide_pid < hide_pid_min)
684 return true;
685 if (in_group_p(pid->pid_gid))
686 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800687 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800688}
689
690
691static int proc_pid_permission(struct inode *inode, int mask)
692{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200693 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800694 struct task_struct *task;
695 bool has_perms;
696
697 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800698 if (!task)
699 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800700 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800701 put_task_struct(task);
702
703 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800704 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800705 /*
706 * Let's make getdents(), stat(), and open()
707 * consistent with each other. If a process
708 * may not stat() a file, it shouldn't be seen
709 * in procfs at all.
710 */
711 return -ENOENT;
712 }
713
714 return -EPERM;
715 }
716 return generic_permission(inode, mask);
717}
718
719
720
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800721static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700722 .setattr = proc_setattr,
723};
724
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800725static int proc_single_show(struct seq_file *m, void *v)
726{
727 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200728 struct pid_namespace *ns = proc_pid_ns(inode);
729 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800730 struct task_struct *task;
731 int ret;
732
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800733 task = get_pid_task(pid, PIDTYPE_PID);
734 if (!task)
735 return -ESRCH;
736
737 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
738
739 put_task_struct(task);
740 return ret;
741}
742
743static int proc_single_open(struct inode *inode, struct file *filp)
744{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800745 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800746}
747
748static const struct file_operations proc_single_file_operations = {
749 .open = proc_single_open,
750 .read = seq_read,
751 .llseek = seq_lseek,
752 .release = single_release,
753};
754
Oleg Nesterov5381e162014-10-09 15:25:24 -0700755
756struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
757{
758 struct task_struct *task = get_proc_task(inode);
759 struct mm_struct *mm = ERR_PTR(-ESRCH);
760
761 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800762 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700763 put_task_struct(task);
764
765 if (!IS_ERR_OR_NULL(mm)) {
766 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800767 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700768 /* but do not pin its memory */
769 mmput(mm);
770 }
771 }
772
773 return mm;
774}
775
Cong Wangb409e572012-05-31 16:26:17 -0700776static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700778 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800779
780 if (IS_ERR(mm))
781 return PTR_ERR(mm);
782
Linus Torvaldse2683372012-01-17 15:21:19 -0800783 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
785}
786
Cong Wangb409e572012-05-31 16:26:17 -0700787static int mem_open(struct inode *inode, struct file *file)
788{
Djalal Harounibc452b42012-07-30 14:42:28 -0700789 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
790
791 /* OK to pass negative loff_t, we can catch out-of-range */
792 file->f_mode |= FMODE_UNSIGNED_OFFSET;
793
794 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700795}
796
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100797static ssize_t mem_rw(struct file *file, char __user *buf,
798 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Linus Torvaldse2683372012-01-17 15:21:19 -0800800 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100801 unsigned long addr = *ppos;
802 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700804 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Linus Torvaldse2683372012-01-17 15:21:19 -0800806 if (!mm)
807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Michal Hocko0ee931c2017-09-13 16:28:29 -0700809 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800811 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700813 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800814 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100815 goto free;
816
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700817 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100820 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100822 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 copied = -EFAULT;
824 break;
825 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100826
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100827 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100828 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (!copied)
830 copied = -EIO;
831 break;
832 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100833
834 if (!write && copy_to_user(buf, page, this_len)) {
835 copied = -EFAULT;
836 break;
837 }
838
839 buf += this_len;
840 addr += this_len;
841 copied += this_len;
842 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100844 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700845
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100846 mmput(mm);
847free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700848 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return copied;
850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100852static ssize_t mem_read(struct file *file, char __user *buf,
853 size_t count, loff_t *ppos)
854{
855 return mem_rw(file, buf, count, ppos, 0);
856}
857
858static ssize_t mem_write(struct file *file, const char __user *buf,
859 size_t count, loff_t *ppos)
860{
861 return mem_rw(file, (char __user*)buf, count, ppos, 1);
862}
863
Matt Mackall85863e42008-02-04 22:29:04 -0800864loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 switch (orig) {
867 case 0:
868 file->f_pos = offset;
869 break;
870 case 1:
871 file->f_pos += offset;
872 break;
873 default:
874 return -EINVAL;
875 }
876 force_successful_syscall_return();
877 return file->f_pos;
878}
879
Linus Torvaldse2683372012-01-17 15:21:19 -0800880static int mem_release(struct inode *inode, struct file *file)
881{
882 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100883 if (mm)
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100884 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800885 return 0;
886}
887
Arjan van de Ven00977a52007-02-12 00:55:34 -0800888static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 .llseek = mem_lseek,
890 .read = mem_read,
891 .write = mem_write,
892 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800893 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894};
895
Cong Wangb409e572012-05-31 16:26:17 -0700896static int environ_open(struct inode *inode, struct file *file)
897{
898 return __mem_open(inode, file, PTRACE_MODE_READ);
899}
900
James Pearson315e28c2007-10-16 23:30:17 -0700901static ssize_t environ_read(struct file *file, char __user *buf,
902 size_t count, loff_t *ppos)
903{
James Pearson315e28c2007-10-16 23:30:17 -0700904 char *page;
905 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700906 int ret = 0;
907 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800908 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700909
Mathias Krause8148a732016-05-05 16:22:26 -0700910 /* Ensure the process spawned far enough to have an environment. */
911 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700912 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700913
Michal Hocko0ee931c2017-09-13 16:28:29 -0700914 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700915 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700916 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700917
Al Virod6f64b82011-02-15 22:26:01 -0500918 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800919 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700920 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800921
Yang Shi88aa7cc2018-06-07 17:05:28 -0700922 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800923 env_start = mm->env_start;
924 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700925 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800926
James Pearson315e28c2007-10-16 23:30:17 -0700927 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700928 size_t this_len, max_len;
929 int retval;
930
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800931 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700932 break;
James Pearson315e28c2007-10-16 23:30:17 -0700933
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800934 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700935
Djalal Harounie8905ec2012-07-30 14:42:26 -0700936 max_len = min_t(size_t, PAGE_SIZE, count);
937 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700938
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200939 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700940
941 if (retval <= 0) {
942 ret = retval;
943 break;
944 }
945
946 if (copy_to_user(buf, page, retval)) {
947 ret = -EFAULT;
948 break;
949 }
950
951 ret += retval;
952 src += retval;
953 buf += retval;
954 count -= retval;
955 }
956 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700957 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700958
959free:
James Pearson315e28c2007-10-16 23:30:17 -0700960 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700961 return ret;
962}
963
964static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700965 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700966 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100967 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700968 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700969};
970
Al Viroc5317162016-10-05 18:43:43 -0400971static int auxv_open(struct inode *inode, struct file *file)
972{
973 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
974}
975
976static ssize_t auxv_read(struct file *file, char __user *buf,
977 size_t count, loff_t *ppos)
978{
979 struct mm_struct *mm = file->private_data;
980 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700981
982 if (!mm)
983 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400984 do {
985 nwords += 2;
986 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
987 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
988 nwords * sizeof(mm->saved_auxv[0]));
989}
990
991static const struct file_operations proc_auxv_operations = {
992 .open = auxv_open,
993 .read = auxv_read,
994 .llseek = generic_file_llseek,
995 .release = mem_release,
996};
997
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800998static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
999 loff_t *ppos)
1000{
Al Viro496ad9a2013-01-23 17:07:38 -05001001 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001002 char buffer[PROC_NUMBUF];
1003 int oom_adj = OOM_ADJUST_MIN;
1004 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001005
1006 if (!task)
1007 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001008 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1009 oom_adj = OOM_ADJUST_MAX;
1010 else
1011 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1012 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001013 put_task_struct(task);
1014 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1015 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1016}
1017
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001018static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1019{
1020 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001021 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001022 struct task_struct *task;
1023 int err = 0;
1024
1025 task = get_proc_task(file_inode(file));
1026 if (!task)
1027 return -ESRCH;
1028
1029 mutex_lock(&oom_adj_mutex);
1030 if (legacy) {
1031 if (oom_adj < task->signal->oom_score_adj &&
1032 !capable(CAP_SYS_RESOURCE)) {
1033 err = -EACCES;
1034 goto err_unlock;
1035 }
1036 /*
1037 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1038 * /proc/pid/oom_score_adj instead.
1039 */
1040 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1041 current->comm, task_pid_nr(current), task_pid_nr(task),
1042 task_pid_nr(task));
1043 } else {
1044 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1045 !capable(CAP_SYS_RESOURCE)) {
1046 err = -EACCES;
1047 goto err_unlock;
1048 }
1049 }
1050
Michal Hocko44a70ade2016-07-28 15:44:43 -07001051 /*
1052 * Make sure we will check other processes sharing the mm if this is
1053 * not vfrok which wants its own oom_score_adj.
1054 * pin the mm so it doesn't go away and get reused after task_unlock
1055 */
1056 if (!task->vfork_done) {
1057 struct task_struct *p = find_lock_task_mm(task);
1058
1059 if (p) {
1060 if (atomic_read(&p->mm->mm_users) > 1) {
1061 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001062 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001063 }
1064 task_unlock(p);
1065 }
1066 }
1067
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001068 task->signal->oom_score_adj = oom_adj;
1069 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1070 task->signal->oom_score_adj_min = (short)oom_adj;
1071 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001072
1073 if (mm) {
1074 struct task_struct *p;
1075
1076 rcu_read_lock();
1077 for_each_process(p) {
1078 if (same_thread_group(task, p))
1079 continue;
1080
1081 /* do not touch kernel threads or the global init */
1082 if (p->flags & PF_KTHREAD || is_global_init(p))
1083 continue;
1084
1085 task_lock(p);
1086 if (!p->vfork_done && process_shares_mm(p, mm)) {
1087 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",
1088 task_pid_nr(p), p->comm,
1089 p->signal->oom_score_adj, oom_adj,
1090 task_pid_nr(task), task->comm);
1091 p->signal->oom_score_adj = oom_adj;
1092 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1093 p->signal->oom_score_adj_min = (short)oom_adj;
1094 }
1095 task_unlock(p);
1096 }
1097 rcu_read_unlock();
1098 mmdrop(mm);
1099 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001100err_unlock:
1101 mutex_unlock(&oom_adj_mutex);
1102 put_task_struct(task);
1103 return err;
1104}
Michal Hockof913da52016-07-28 15:44:37 -07001105
David Rientjesb72bdfa2015-11-05 18:50:32 -08001106/*
1107 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1108 * kernels. The effective policy is defined by oom_score_adj, which has a
1109 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1110 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1111 * Processes that become oom disabled via oom_adj will still be oom disabled
1112 * with this implementation.
1113 *
1114 * oom_adj cannot be removed since existing userspace binaries use it.
1115 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001116static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1117 size_t count, loff_t *ppos)
1118{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001119 char buffer[PROC_NUMBUF];
1120 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001121 int err;
1122
1123 memset(buffer, 0, sizeof(buffer));
1124 if (count > sizeof(buffer) - 1)
1125 count = sizeof(buffer) - 1;
1126 if (copy_from_user(buffer, buf, count)) {
1127 err = -EFAULT;
1128 goto out;
1129 }
1130
1131 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1132 if (err)
1133 goto out;
1134 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1135 oom_adj != OOM_DISABLE) {
1136 err = -EINVAL;
1137 goto out;
1138 }
1139
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001140 /*
1141 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1142 * value is always attainable.
1143 */
1144 if (oom_adj == OOM_ADJUST_MAX)
1145 oom_adj = OOM_SCORE_ADJ_MAX;
1146 else
1147 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1148
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001149 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001150out:
1151 return err < 0 ? err : count;
1152}
1153
1154static const struct file_operations proc_oom_adj_operations = {
1155 .read = oom_adj_read,
1156 .write = oom_adj_write,
1157 .llseek = generic_file_llseek,
1158};
1159
David Rientjesa63d83f2010-08-09 17:19:46 -07001160static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1161 size_t count, loff_t *ppos)
1162{
Al Viro496ad9a2013-01-23 17:07:38 -05001163 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001164 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001165 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001166 size_t len;
1167
1168 if (!task)
1169 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001170 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001171 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001172 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001173 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1174}
1175
1176static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1177 size_t count, loff_t *ppos)
1178{
David Rientjesa63d83f2010-08-09 17:19:46 -07001179 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001180 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001181 int err;
1182
1183 memset(buffer, 0, sizeof(buffer));
1184 if (count > sizeof(buffer) - 1)
1185 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001186 if (copy_from_user(buffer, buf, count)) {
1187 err = -EFAULT;
1188 goto out;
1189 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001190
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001191 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001192 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001193 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001194 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001195 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1196 err = -EINVAL;
1197 goto out;
1198 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001199
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001200 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001201out:
1202 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001203}
1204
1205static const struct file_operations proc_oom_score_adj_operations = {
1206 .read = oom_score_adj_read,
1207 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001208 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001209};
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001212#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1214 size_t count, loff_t *ppos)
1215{
Al Viro496ad9a2013-01-23 17:07:38 -05001216 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001217 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 ssize_t length;
1219 char tmpbuf[TMPBUFLEN];
1220
Eric W. Biederman99f89552006-06-26 00:25:55 -07001221 if (!task)
1222 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001224 from_kuid(file->f_cred->user_ns,
1225 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001226 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1228}
1229
1230static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1231 size_t count, loff_t *ppos)
1232{
Al Viro496ad9a2013-01-23 17:07:38 -05001233 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001235 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001236 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001238 rcu_read_lock();
1239 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1240 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001242 }
1243 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (*ppos != 0) {
1246 /* No partial writes. */
1247 return -EINVAL;
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001250 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1251 if (rv < 0)
1252 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001253
1254 /* is userspace tring to explicitly UNSET the loginuid? */
1255 if (loginuid == AUDIT_UID_UNSET) {
1256 kloginuid = INVALID_UID;
1257 } else {
1258 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001259 if (!uid_valid(kloginuid))
1260 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001261 }
1262
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001263 rv = audit_set_loginuid(kloginuid);
1264 if (rv < 0)
1265 return rv;
1266 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Arjan van de Ven00977a52007-02-12 00:55:34 -08001269static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 .read = proc_loginuid_read,
1271 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001272 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273};
Eric Paris1e0bd752008-03-13 08:15:31 -04001274
1275static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1276 size_t count, loff_t *ppos)
1277{
Al Viro496ad9a2013-01-23 17:07:38 -05001278 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001279 struct task_struct *task = get_proc_task(inode);
1280 ssize_t length;
1281 char tmpbuf[TMPBUFLEN];
1282
1283 if (!task)
1284 return -ESRCH;
1285 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1286 audit_get_sessionid(task));
1287 put_task_struct(task);
1288 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1289}
1290
1291static const struct file_operations proc_sessionid_operations = {
1292 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001293 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001294};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295#endif
1296
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001297#ifdef CONFIG_FAULT_INJECTION
1298static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1299 size_t count, loff_t *ppos)
1300{
Al Viro496ad9a2013-01-23 17:07:38 -05001301 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001302 char buffer[PROC_NUMBUF];
1303 size_t len;
1304 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001305
1306 if (!task)
1307 return -ESRCH;
1308 make_it_fail = task->make_it_fail;
1309 put_task_struct(task);
1310
1311 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001312
1313 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001314}
1315
1316static ssize_t proc_fault_inject_write(struct file * file,
1317 const char __user * buf, size_t count, loff_t *ppos)
1318{
1319 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001320 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001321 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001322 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001323
1324 if (!capable(CAP_SYS_RESOURCE))
1325 return -EPERM;
1326 memset(buffer, 0, sizeof(buffer));
1327 if (count > sizeof(buffer) - 1)
1328 count = sizeof(buffer) - 1;
1329 if (copy_from_user(buffer, buf, count))
1330 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001331 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1332 if (rv < 0)
1333 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001334 if (make_it_fail < 0 || make_it_fail > 1)
1335 return -EINVAL;
1336
Al Viro496ad9a2013-01-23 17:07:38 -05001337 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001338 if (!task)
1339 return -ESRCH;
1340 task->make_it_fail = make_it_fail;
1341 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001342
1343 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001344}
1345
Arjan van de Ven00977a52007-02-12 00:55:34 -08001346static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347 .read = proc_fault_inject_read,
1348 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001349 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001350};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001351
1352static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1353 size_t count, loff_t *ppos)
1354{
1355 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001356 int err;
1357 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001358
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001359 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001360 if (err)
1361 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001362
1363 task = get_proc_task(file_inode(file));
1364 if (!task)
1365 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001366 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001367 put_task_struct(task);
1368
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001369 return count;
1370}
1371
1372static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1373 size_t count, loff_t *ppos)
1374{
1375 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001376 char numbuf[PROC_NUMBUF];
1377 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001378
1379 task = get_proc_task(file_inode(file));
1380 if (!task)
1381 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001382 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001383 put_task_struct(task);
Alexey Dobriyana44937f2018-08-21 21:54:27 -07001384 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001385}
1386
1387static const struct file_operations proc_fail_nth_operations = {
1388 .read = proc_fail_nth_read,
1389 .write = proc_fail_nth_write,
1390};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001391#endif
1392
Arjan van de Ven97455122008-01-25 21:08:34 +01001393
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001394#ifdef CONFIG_SCHED_DEBUG
1395/*
1396 * Print out various scheduling related per-task fields:
1397 */
1398static int sched_show(struct seq_file *m, void *v)
1399{
1400 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001401 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001402 struct task_struct *p;
1403
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001404 p = get_proc_task(inode);
1405 if (!p)
1406 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001407 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001408
1409 put_task_struct(p);
1410
1411 return 0;
1412}
1413
1414static ssize_t
1415sched_write(struct file *file, const char __user *buf,
1416 size_t count, loff_t *offset)
1417{
Al Viro496ad9a2013-01-23 17:07:38 -05001418 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001419 struct task_struct *p;
1420
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001421 p = get_proc_task(inode);
1422 if (!p)
1423 return -ESRCH;
1424 proc_sched_set_task(p);
1425
1426 put_task_struct(p);
1427
1428 return count;
1429}
1430
1431static int sched_open(struct inode *inode, struct file *filp)
1432{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001433 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001434}
1435
1436static const struct file_operations proc_pid_sched_operations = {
1437 .open = sched_open,
1438 .read = seq_read,
1439 .write = sched_write,
1440 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001441 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001442};
1443
1444#endif
1445
Mike Galbraith5091faa2010-11-30 14:18:03 +01001446#ifdef CONFIG_SCHED_AUTOGROUP
1447/*
1448 * Print out autogroup related information:
1449 */
1450static int sched_autogroup_show(struct seq_file *m, void *v)
1451{
1452 struct inode *inode = m->private;
1453 struct task_struct *p;
1454
1455 p = get_proc_task(inode);
1456 if (!p)
1457 return -ESRCH;
1458 proc_sched_autogroup_show_task(p, m);
1459
1460 put_task_struct(p);
1461
1462 return 0;
1463}
1464
1465static ssize_t
1466sched_autogroup_write(struct file *file, const char __user *buf,
1467 size_t count, loff_t *offset)
1468{
Al Viro496ad9a2013-01-23 17:07:38 -05001469 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001470 struct task_struct *p;
1471 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001472 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001473 int err;
1474
1475 memset(buffer, 0, sizeof(buffer));
1476 if (count > sizeof(buffer) - 1)
1477 count = sizeof(buffer) - 1;
1478 if (copy_from_user(buffer, buf, count))
1479 return -EFAULT;
1480
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001481 err = kstrtoint(strstrip(buffer), 0, &nice);
1482 if (err < 0)
1483 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001484
1485 p = get_proc_task(inode);
1486 if (!p)
1487 return -ESRCH;
1488
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001489 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001490 if (err)
1491 count = err;
1492
1493 put_task_struct(p);
1494
1495 return count;
1496}
1497
1498static int sched_autogroup_open(struct inode *inode, struct file *filp)
1499{
1500 int ret;
1501
1502 ret = single_open(filp, sched_autogroup_show, NULL);
1503 if (!ret) {
1504 struct seq_file *m = filp->private_data;
1505
1506 m->private = inode;
1507 }
1508 return ret;
1509}
1510
1511static const struct file_operations proc_pid_sched_autogroup_operations = {
1512 .open = sched_autogroup_open,
1513 .read = seq_read,
1514 .write = sched_autogroup_write,
1515 .llseek = seq_lseek,
1516 .release = single_release,
1517};
1518
1519#endif /* CONFIG_SCHED_AUTOGROUP */
1520
john stultz4614a696b2009-12-14 18:00:05 -08001521static ssize_t comm_write(struct file *file, const char __user *buf,
1522 size_t count, loff_t *offset)
1523{
Al Viro496ad9a2013-01-23 17:07:38 -05001524 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001525 struct task_struct *p;
1526 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001527 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001528
1529 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001530 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001531 return -EFAULT;
1532
1533 p = get_proc_task(inode);
1534 if (!p)
1535 return -ESRCH;
1536
1537 if (same_thread_group(current, p))
1538 set_task_comm(p, buffer);
1539 else
1540 count = -EINVAL;
1541
1542 put_task_struct(p);
1543
1544 return count;
1545}
1546
1547static int comm_show(struct seq_file *m, void *v)
1548{
1549 struct inode *inode = m->private;
1550 struct task_struct *p;
1551
1552 p = get_proc_task(inode);
1553 if (!p)
1554 return -ESRCH;
1555
Tejun Heo88b72b32018-05-18 08:47:13 -07001556 proc_task_name(m, p, false);
1557 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001558
1559 put_task_struct(p);
1560
1561 return 0;
1562}
1563
1564static int comm_open(struct inode *inode, struct file *filp)
1565{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001566 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001567}
1568
1569static const struct file_operations proc_pid_set_comm_operations = {
1570 .open = comm_open,
1571 .read = seq_read,
1572 .write = comm_write,
1573 .llseek = seq_lseek,
1574 .release = single_release,
1575};
1576
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001577static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001578{
1579 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001580 struct file *exe_file;
1581
David Howells2b0143b2015-03-17 22:25:59 +00001582 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001583 if (!task)
1584 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001585 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001586 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001587 if (exe_file) {
1588 *exe_path = exe_file->f_path;
1589 path_get(&exe_file->f_path);
1590 fput(exe_file);
1591 return 0;
1592 } else
1593 return -ENOENT;
1594}
1595
Al Viro6b255392015-11-17 10:20:54 -05001596static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001597 struct inode *inode,
1598 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001600 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int error = -EACCES;
1602
Al Viro6b255392015-11-17 10:20:54 -05001603 if (!dentry)
1604 return ERR_PTR(-ECHILD);
1605
Eric W. Biederman778c1142006-06-26 00:25:58 -07001606 /* Are we allowed to snoop on the tasks file descriptors? */
1607 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Christoph Hellwig408ef012012-06-18 10:47:03 -04001610 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1611 if (error)
1612 goto out;
1613
Al Viro6e77137b2015-05-02 13:37:52 -04001614 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001615 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616out:
Al Viro008b1502005-08-20 00:17:39 +01001617 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001620static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001622 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001623 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 int len;
1625
1626 if (!tmp)
1627 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001628
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001629 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001630 len = PTR_ERR(pathname);
1631 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001633 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 if (len > buflen)
1636 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001637 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 len = -EFAULT;
1639 out:
1640 free_page((unsigned long)tmp);
1641 return len;
1642}
1643
1644static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1645{
1646 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001647 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001648 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Eric W. Biederman778c1142006-06-26 00:25:58 -07001650 /* Are we allowed to snoop on the tasks file descriptors? */
1651 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001654 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (error)
1656 goto out;
1657
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001658 error = do_proc_readlink(&path, buffer, buflen);
1659 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return error;
1662}
1663
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001664const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001666 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001667 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668};
1669
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001670
1671/* building an inode */
1672
Al Viroc6eb50d2017-09-30 14:45:42 -04001673void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001674 kuid_t *ruid, kgid_t *rgid)
1675{
1676 /* Depending on the state of dumpable compute who should own a
1677 * proc file for a task.
1678 */
1679 const struct cred *cred;
1680 kuid_t uid;
1681 kgid_t gid;
1682
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001683 if (unlikely(task->flags & PF_KTHREAD)) {
1684 *ruid = GLOBAL_ROOT_UID;
1685 *rgid = GLOBAL_ROOT_GID;
1686 return;
1687 }
1688
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001689 /* Default to the tasks effective ownership */
1690 rcu_read_lock();
1691 cred = __task_cred(task);
1692 uid = cred->euid;
1693 gid = cred->egid;
1694 rcu_read_unlock();
1695
1696 /*
1697 * Before the /proc/pid/status file was created the only way to read
1698 * the effective uid of a /process was to stat /proc/pid. Reading
1699 * /proc/pid/status is slow enough that procps and other packages
1700 * kept stating /proc/pid. To keep the rules in /proc simple I have
1701 * made this apply to all per process world readable and executable
1702 * directories.
1703 */
1704 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1705 struct mm_struct *mm;
1706 task_lock(task);
1707 mm = task->mm;
1708 /* Make non-dumpable tasks owned by some root */
1709 if (mm) {
1710 if (get_dumpable(mm) != SUID_DUMP_USER) {
1711 struct user_namespace *user_ns = mm->user_ns;
1712
1713 uid = make_kuid(user_ns, 0);
1714 if (!uid_valid(uid))
1715 uid = GLOBAL_ROOT_UID;
1716
1717 gid = make_kgid(user_ns, 0);
1718 if (!gid_valid(gid))
1719 gid = GLOBAL_ROOT_GID;
1720 }
1721 } else {
1722 uid = GLOBAL_ROOT_UID;
1723 gid = GLOBAL_ROOT_GID;
1724 }
1725 task_unlock(task);
1726 }
1727 *ruid = uid;
1728 *rgid = gid;
1729}
1730
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001731struct inode *proc_pid_make_inode(struct super_block * sb,
1732 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001733{
1734 struct inode * inode;
1735 struct proc_inode *ei;
1736
1737 /* We need a new inode */
1738
1739 inode = new_inode(sb);
1740 if (!inode)
1741 goto out;
1742
1743 /* Common stuff */
1744 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001745 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001746 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001747 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001748 inode->i_op = &proc_def_inode_operations;
1749
1750 /*
1751 * grab the reference to task.
1752 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001753 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001754 if (!ei->pid)
1755 goto out_unlock;
1756
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001757 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001758 security_task_to_inode(task, inode);
1759
1760out:
1761 return inode;
1762
1763out_unlock:
1764 iput(inode);
1765 return NULL;
1766}
1767
David Howellsa528d352017-01-31 16:46:22 +00001768int pid_getattr(const struct path *path, struct kstat *stat,
1769 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001770{
David Howellsa528d352017-01-31 16:46:22 +00001771 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001772 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001773 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001774
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001775 generic_fillattr(inode, stat);
1776
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001777 stat->uid = GLOBAL_ROOT_UID;
1778 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001779 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001780 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1781 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001782 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001783 rcu_read_unlock();
1784 /*
1785 * This doesn't prevent learning whether PID exists,
1786 * it only makes getattr() consistent with readdir().
1787 */
1788 return -ENOENT;
1789 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001790 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001791 }
1792 rcu_read_unlock();
1793 return 0;
1794}
1795
1796/* dentry stuff */
1797
1798/*
Al Viro1bbc5512018-05-02 21:26:16 -04001799 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1800 */
1801void pid_update_inode(struct task_struct *task, struct inode *inode)
1802{
1803 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1804
1805 inode->i_mode &= ~(S_ISUID | S_ISGID);
1806 security_task_to_inode(task, inode);
1807}
1808
1809/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001810 * Rewrite the inode's ownerships here because the owning task may have
1811 * performed a setuid(), etc.
1812 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001813 */
Al Viro1bbc5512018-05-02 21:26:16 -04001814static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001815{
Nick Piggin34286d62011-01-07 17:49:57 +11001816 struct inode *inode;
1817 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001818
Al Viro0b728e12012-06-10 16:03:43 -04001819 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001820 return -ECHILD;
1821
David Howells2b0143b2015-03-17 22:25:59 +00001822 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001823 task = get_proc_task(inode);
1824
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001825 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001826 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001827 put_task_struct(task);
1828 return 1;
1829 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001830 return 0;
1831}
1832
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001833static inline bool proc_inode_is_dead(struct inode *inode)
1834{
1835 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1836}
1837
David Howells1dd704b2013-04-12 01:08:50 +01001838int pid_delete_dentry(const struct dentry *dentry)
1839{
1840 /* Is the task we represent dead?
1841 * If so, then don't put the dentry on the lru list,
1842 * kill it immediately.
1843 */
David Howells2b0143b2015-03-17 22:25:59 +00001844 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001845}
1846
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001847const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001848{
1849 .d_revalidate = pid_revalidate,
1850 .d_delete = pid_delete_dentry,
1851};
1852
1853/* Lookups */
1854
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001855/*
1856 * Fill a directory entry.
1857 *
1858 * If possible create the dcache entry and derive our inode number and
1859 * file type from dcache entry.
1860 *
1861 * Since all of the proc inode numbers are dynamically generated, the inode
1862 * numbers do not exist until the inode is cache. This means creating the
1863 * the dcache entry in readdir is necessary to keep the inode numbers
1864 * reported by readdir in sync with the inode numbers reported
1865 * by stat.
1866 */
Al Virof0c3b502013-05-16 12:07:31 -04001867bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07001868 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001869 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001870{
Al Virof0c3b502013-05-16 12:07:31 -04001871 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001872 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001873 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001874 unsigned type = DT_UNKNOWN;
1875 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001876
Al Viro1df98b82013-06-15 11:33:10 +04001877 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001878 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001879 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1880 child = d_alloc_parallel(dir, &qname, &wq);
1881 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001882 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001883 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001884 struct dentry *res;
1885 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001886 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001887 if (unlikely(res)) {
1888 dput(child);
1889 child = res;
Al Virod85b3992018-06-08 01:17:11 -04001890 if (IS_ERR(child))
1891 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001892 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001893 }
1894 }
David Howells2b0143b2015-03-17 22:25:59 +00001895 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001896 ino = inode->i_ino;
1897 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001898 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04001899end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04001900 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001901}
1902
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001903/*
1904 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1905 * which represent vma start and end addresses.
1906 */
1907static int dname_to_vma_addr(struct dentry *dentry,
1908 unsigned long *start, unsigned long *end)
1909{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001910 const char *str = dentry->d_name.name;
1911 unsigned long long sval, eval;
1912 unsigned int len;
1913
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001914 if (str[0] == '0' && str[1] != '-')
1915 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001916 len = _parse_integer(str, 16, &sval);
1917 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001918 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001919 if (sval != (unsigned long)sval)
1920 return -EINVAL;
1921 str += len;
1922
1923 if (*str != '-')
1924 return -EINVAL;
1925 str++;
1926
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001927 if (str[0] == '0' && str[1])
1928 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001929 len = _parse_integer(str, 16, &eval);
1930 if (len & KSTRTOX_OVERFLOW)
1931 return -EINVAL;
1932 if (eval != (unsigned long)eval)
1933 return -EINVAL;
1934 str += len;
1935
1936 if (*str != '\0')
1937 return -EINVAL;
1938
1939 *start = sval;
1940 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001941
1942 return 0;
1943}
1944
Al Viro0b728e12012-06-10 16:03:43 -04001945static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001946{
1947 unsigned long vm_start, vm_end;
1948 bool exact_vma_exists = false;
1949 struct mm_struct *mm = NULL;
1950 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001951 struct inode *inode;
1952 int status = 0;
1953
Al Viro0b728e12012-06-10 16:03:43 -04001954 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001955 return -ECHILD;
1956
David Howells2b0143b2015-03-17 22:25:59 +00001957 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001958 task = get_proc_task(inode);
1959 if (!task)
1960 goto out_notask;
1961
Jann Horncaaee622016-01-20 15:00:04 -08001962 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001963 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001964 goto out;
1965
1966 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1967 down_read(&mm->mmap_sem);
1968 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1969 up_read(&mm->mmap_sem);
1970 }
1971
1972 mmput(mm);
1973
1974 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001975 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1976
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001977 security_task_to_inode(task, inode);
1978 status = 1;
1979 }
1980
1981out:
1982 put_task_struct(task);
1983
1984out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001985 return status;
1986}
1987
1988static const struct dentry_operations tid_map_files_dentry_operations = {
1989 .d_revalidate = map_files_d_revalidate,
1990 .d_delete = pid_delete_dentry,
1991};
1992
Al Viro6b255392015-11-17 10:20:54 -05001993static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001994{
1995 unsigned long vm_start, vm_end;
1996 struct vm_area_struct *vma;
1997 struct task_struct *task;
1998 struct mm_struct *mm;
1999 int rc;
2000
2001 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002002 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002003 if (!task)
2004 goto out;
2005
2006 mm = get_task_mm(task);
2007 put_task_struct(task);
2008 if (!mm)
2009 goto out;
2010
2011 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2012 if (rc)
2013 goto out_mmput;
2014
Artem Fetishev70335ab2014-03-10 15:49:45 -07002015 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002016 down_read(&mm->mmap_sem);
2017 vma = find_exact_vma(mm, vm_start, vm_end);
2018 if (vma && vma->vm_file) {
2019 *path = vma->vm_file->f_path;
2020 path_get(path);
2021 rc = 0;
2022 }
2023 up_read(&mm->mmap_sem);
2024
2025out_mmput:
2026 mmput(mm);
2027out:
2028 return rc;
2029}
2030
2031struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002032 unsigned long start;
2033 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002034 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002035};
2036
Calvin Owensbdb4d102015-09-09 15:35:54 -07002037/*
2038 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2039 * symlinks may be used to bypass permissions on ancestor directories in the
2040 * path to the file in question.
2041 */
2042static const char *
Al Viro6b255392015-11-17 10:20:54 -05002043proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002044 struct inode *inode,
2045 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002046{
2047 if (!capable(CAP_SYS_ADMIN))
2048 return ERR_PTR(-EPERM);
2049
Al Virofceef392015-12-29 15:58:39 -05002050 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002051}
2052
2053/*
Al Viro6b255392015-11-17 10:20:54 -05002054 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002055 */
2056static const struct inode_operations proc_map_files_link_inode_operations = {
2057 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002058 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002059 .setattr = proc_setattr,
2060};
2061
Al Viro0168b9e2018-05-03 09:21:05 -04002062static struct dentry *
2063proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002064 struct task_struct *task, const void *ptr)
2065{
Al Viro7b540d02012-08-27 14:55:26 -04002066 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002067 struct proc_inode *ei;
2068 struct inode *inode;
2069
Al Viro0168b9e2018-05-03 09:21:05 -04002070 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002071 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2072 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002073 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002074 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002075
2076 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002077 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002078
Calvin Owensbdb4d102015-09-09 15:35:54 -07002079 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002080 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081
2082 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002083 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002084}
2085
2086static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002087 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002088{
2089 unsigned long vm_start, vm_end;
2090 struct vm_area_struct *vma;
2091 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002092 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002093 struct mm_struct *mm;
2094
Al Viro0168b9e2018-05-03 09:21:05 -04002095 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002096 task = get_proc_task(dir);
2097 if (!task)
2098 goto out;
2099
Al Viro0168b9e2018-05-03 09:21:05 -04002100 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002101 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002102 goto out_put_task;
2103
Al Viro0168b9e2018-05-03 09:21:05 -04002104 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002105 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002106 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002107
2108 mm = get_task_mm(task);
2109 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002110 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002111
2112 down_read(&mm->mmap_sem);
2113 vma = find_exact_vma(mm, vm_start, vm_end);
2114 if (!vma)
2115 goto out_no_vma;
2116
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002117 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002118 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002119 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120
2121out_no_vma:
2122 up_read(&mm->mmap_sem);
2123 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002124out_put_task:
2125 put_task_struct(task);
2126out:
Al Viro0168b9e2018-05-03 09:21:05 -04002127 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002128}
2129
2130static const struct inode_operations proc_map_files_inode_operations = {
2131 .lookup = proc_map_files_lookup,
2132 .permission = proc_fd_permission,
2133 .setattr = proc_setattr,
2134};
2135
2136static int
Al Virof0c3b502013-05-16 12:07:31 -04002137proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002138{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002139 struct vm_area_struct *vma;
2140 struct task_struct *task;
2141 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002142 unsigned long nr_files, pos, i;
2143 struct flex_array *fa = NULL;
2144 struct map_files_info info;
2145 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002146 int ret;
2147
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002148 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002149 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002150 if (!task)
2151 goto out;
2152
2153 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002154 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002155 goto out_put_task;
2156
2157 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002158 if (!dir_emit_dots(file, ctx))
2159 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002160
Al Virof0c3b502013-05-16 12:07:31 -04002161 mm = get_task_mm(task);
2162 if (!mm)
2163 goto out_put_task;
2164 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002165
Al Virof0c3b502013-05-16 12:07:31 -04002166 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002167
Al Virof0c3b502013-05-16 12:07:31 -04002168 /*
2169 * We need two passes here:
2170 *
2171 * 1) Collect vmas of mapped files with mmap_sem taken
2172 * 2) Release mmap_sem and instantiate entries
2173 *
2174 * otherwise we get lockdep complained, since filldir()
2175 * routine might require mmap_sem taken in might_fault().
2176 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002177
Al Virof0c3b502013-05-16 12:07:31 -04002178 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2179 if (vma->vm_file && ++pos > ctx->pos)
2180 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002181 }
Al Virof0c3b502013-05-16 12:07:31 -04002182
2183 if (nr_files) {
2184 fa = flex_array_alloc(sizeof(info), nr_files,
2185 GFP_KERNEL);
2186 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2187 GFP_KERNEL)) {
2188 ret = -ENOMEM;
2189 if (fa)
2190 flex_array_free(fa);
2191 up_read(&mm->mmap_sem);
2192 mmput(mm);
2193 goto out_put_task;
2194 }
2195 for (i = 0, vma = mm->mmap, pos = 2; vma;
2196 vma = vma->vm_next) {
2197 if (!vma->vm_file)
2198 continue;
2199 if (++pos <= ctx->pos)
2200 continue;
2201
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002202 info.start = vma->vm_start;
2203 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002204 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002205 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2206 BUG();
2207 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002208 }
Al Virof0c3b502013-05-16 12:07:31 -04002209 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002210 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002211
2212 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002213 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2214 unsigned int len;
2215
Al Virof0c3b502013-05-16 12:07:31 -04002216 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002217 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002218 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002219 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002220 proc_map_files_instantiate,
2221 task,
2222 (void *)(unsigned long)p->mode))
2223 break;
2224 ctx->pos++;
2225 }
2226 if (fa)
2227 flex_array_free(fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002228
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002229out_put_task:
2230 put_task_struct(task);
2231out:
2232 return ret;
2233}
2234
2235static const struct file_operations proc_map_files_operations = {
2236 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002237 .iterate_shared = proc_map_files_readdir,
2238 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002239};
2240
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002241#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002242struct timers_private {
2243 struct pid *pid;
2244 struct task_struct *task;
2245 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002246 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002247 unsigned long flags;
2248};
2249
2250static void *timers_start(struct seq_file *m, loff_t *pos)
2251{
2252 struct timers_private *tp = m->private;
2253
2254 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2255 if (!tp->task)
2256 return ERR_PTR(-ESRCH);
2257
2258 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2259 if (!tp->sighand)
2260 return ERR_PTR(-ESRCH);
2261
2262 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2263}
2264
2265static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2266{
2267 struct timers_private *tp = m->private;
2268 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2269}
2270
2271static void timers_stop(struct seq_file *m, void *v)
2272{
2273 struct timers_private *tp = m->private;
2274
2275 if (tp->sighand) {
2276 unlock_task_sighand(tp->task, &tp->flags);
2277 tp->sighand = NULL;
2278 }
2279
2280 if (tp->task) {
2281 put_task_struct(tp->task);
2282 tp->task = NULL;
2283 }
2284}
2285
2286static int show_timer(struct seq_file *m, void *v)
2287{
2288 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002289 struct timers_private *tp = m->private;
2290 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002291 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002292 [SIGEV_SIGNAL] = "signal",
2293 [SIGEV_NONE] = "none",
2294 [SIGEV_THREAD] = "thread",
2295 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002296
2297 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002298 notify = timer->it_sigev_notify;
2299
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002300 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002301 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002302 timer->sigq->info.si_signo,
2303 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002304 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002305 nstr[notify & ~SIGEV_THREAD_ID],
2306 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2307 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002308 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002309
2310 return 0;
2311}
2312
2313static const struct seq_operations proc_timers_seq_ops = {
2314 .start = timers_start,
2315 .next = timers_next,
2316 .stop = timers_stop,
2317 .show = show_timer,
2318};
2319
2320static int proc_timers_open(struct inode *inode, struct file *file)
2321{
2322 struct timers_private *tp;
2323
2324 tp = __seq_open_private(file, &proc_timers_seq_ops,
2325 sizeof(struct timers_private));
2326 if (!tp)
2327 return -ENOMEM;
2328
2329 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002330 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002331 return 0;
2332}
2333
2334static const struct file_operations proc_timers_operations = {
2335 .open = proc_timers_open,
2336 .read = seq_read,
2337 .llseek = seq_lseek,
2338 .release = seq_release_private,
2339};
Eric Engestromb5946be2016-03-17 14:20:57 -07002340#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002341
John Stultz5de23d42016-03-17 14:20:54 -07002342static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2343 size_t count, loff_t *offset)
2344{
2345 struct inode *inode = file_inode(file);
2346 struct task_struct *p;
2347 u64 slack_ns;
2348 int err;
2349
2350 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2351 if (err < 0)
2352 return err;
2353
2354 p = get_proc_task(inode);
2355 if (!p)
2356 return -ESRCH;
2357
John Stultz4b2bd5f2016-10-07 17:02:33 -07002358 if (p != current) {
2359 if (!capable(CAP_SYS_NICE)) {
2360 count = -EPERM;
2361 goto out;
2362 }
John Stultz5de23d42016-03-17 14:20:54 -07002363
John Stultz4b2bd5f2016-10-07 17:02:33 -07002364 err = security_task_setscheduler(p);
2365 if (err) {
2366 count = err;
2367 goto out;
2368 }
John Stultz904763e2016-10-07 17:02:29 -07002369 }
2370
John Stultz7abbaf92016-10-07 17:02:26 -07002371 task_lock(p);
2372 if (slack_ns == 0)
2373 p->timer_slack_ns = p->default_timer_slack_ns;
2374 else
2375 p->timer_slack_ns = slack_ns;
2376 task_unlock(p);
2377
2378out:
John Stultz5de23d42016-03-17 14:20:54 -07002379 put_task_struct(p);
2380
2381 return count;
2382}
2383
2384static int timerslack_ns_show(struct seq_file *m, void *v)
2385{
2386 struct inode *inode = m->private;
2387 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002388 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002389
2390 p = get_proc_task(inode);
2391 if (!p)
2392 return -ESRCH;
2393
John Stultz4b2bd5f2016-10-07 17:02:33 -07002394 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002395
John Stultz4b2bd5f2016-10-07 17:02:33 -07002396 if (!capable(CAP_SYS_NICE)) {
2397 err = -EPERM;
2398 goto out;
2399 }
2400 err = security_task_getscheduler(p);
2401 if (err)
2402 goto out;
2403 }
John Stultz904763e2016-10-07 17:02:29 -07002404
John Stultz7abbaf92016-10-07 17:02:26 -07002405 task_lock(p);
2406 seq_printf(m, "%llu\n", p->timer_slack_ns);
2407 task_unlock(p);
2408
2409out:
John Stultz5de23d42016-03-17 14:20:54 -07002410 put_task_struct(p);
2411
2412 return err;
2413}
2414
2415static int timerslack_ns_open(struct inode *inode, struct file *filp)
2416{
2417 return single_open(filp, timerslack_ns_show, inode);
2418}
2419
2420static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2421 .open = timerslack_ns_open,
2422 .read = seq_read,
2423 .write = timerslack_ns_write,
2424 .llseek = seq_lseek,
2425 .release = single_release,
2426};
2427
Al Viro0168b9e2018-05-03 09:21:05 -04002428static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2429 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002430{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002431 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002432 struct inode *inode;
2433 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002434
Al Viro0168b9e2018-05-03 09:21:05 -04002435 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002436 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002437 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002438
2439 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002440 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002441 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002442 if (p->iop)
2443 inode->i_op = p->iop;
2444 if (p->fop)
2445 inode->i_fop = p->fop;
2446 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002447 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002448 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002449 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452static struct dentry *proc_pident_lookup(struct inode *dir,
2453 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002454 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002455 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002457 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002458 const struct pid_entry *p, *last;
Al Viro0168b9e2018-05-03 09:21:05 -04002459 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Eric W. Biederman99f89552006-06-26 00:25:55 -07002461 if (!task)
2462 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002464 /*
2465 * Yes, it does not scale. And it should not. Don't add
2466 * new entries into /proc/<tgid>/ without very good reasons.
2467 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002468 last = &ents[nents];
2469 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (p->len != dentry->d_name.len)
2471 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002472 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2473 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002477 put_task_struct(task);
2478out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002479 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Al Virof0c3b502013-05-16 12:07:31 -04002482static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002483 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002484{
Al Virof0c3b502013-05-16 12:07:31 -04002485 struct task_struct *task = get_proc_task(file_inode(file));
2486 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002487
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002488 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002489 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490
Al Virof0c3b502013-05-16 12:07:31 -04002491 if (!dir_emit_dots(file, ctx))
2492 goto out;
2493
2494 if (ctx->pos >= nents + 2)
2495 goto out;
2496
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002497 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002498 if (!proc_fill_cache(file, ctx, p->name, p->len,
2499 proc_pident_instantiate, task, p))
2500 break;
2501 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002503out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002504 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002509static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2510 size_t count, loff_t *ppos)
2511{
Al Viro496ad9a2013-01-23 17:07:38 -05002512 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002513 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514 ssize_t length;
2515 struct task_struct *task = get_proc_task(inode);
2516
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002517 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002518 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002519
2520 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002521 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002522 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002524 if (length > 0)
2525 length = simple_read_from_buffer(buf, count, ppos, p, length);
2526 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002527 return length;
2528}
2529
2530static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2531 size_t count, loff_t *ppos)
2532{
Al Viro496ad9a2013-01-23 17:07:38 -05002533 struct inode * inode = file_inode(file);
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002534 struct task_struct *task;
Al Virobb646cd2015-12-24 00:16:30 -05002535 void *page;
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002536 int rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002537
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002538 rcu_read_lock();
2539 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2540 if (!task) {
2541 rcu_read_unlock();
2542 return -ESRCH;
2543 }
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002544 /* A task may only write its own attributes. */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002545 if (current != task) {
2546 rcu_read_unlock();
2547 return -EACCES;
2548 }
2549 rcu_read_unlock();
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002550
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551 if (count > PAGE_SIZE)
2552 count = PAGE_SIZE;
2553
2554 /* No partial writes. */
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555 if (*ppos != 0)
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002556 return -EINVAL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002557
Al Virobb646cd2015-12-24 00:16:30 -05002558 page = memdup_user(buf, count);
2559 if (IS_ERR(page)) {
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002560 rv = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002561 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002562 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002563
David Howells107db7c2009-05-08 13:55:27 +01002564 /* Guard against adverse ptrace interaction */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002565 rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2566 if (rv < 0)
David Howells107db7c2009-05-08 13:55:27 +01002567 goto out_free;
2568
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002569 rv = security_setprocattr(file->f_path.dentry->d_name.name, page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002570 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002571out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002572 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002573out:
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002574 return rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002575}
2576
Arjan van de Ven00977a52007-02-12 00:55:34 -08002577static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002578 .read = proc_pid_attr_read,
2579 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002580 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002581};
2582
Eric Dumazetc5141e62007-05-08 00:26:15 -07002583static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002584 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2585 REG("prev", S_IRUGO, proc_pid_attr_operations),
2586 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2587 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2588 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2589 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002590};
2591
Al Virof0c3b502013-05-16 12:07:31 -04002592static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
Al Virof0c3b502013-05-16 12:07:31 -04002594 return proc_pident_readdir(file, ctx,
2595 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
Arjan van de Ven00977a52007-02-12 00:55:34 -08002598static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002600 .iterate_shared = proc_attr_dir_readdir,
2601 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602};
2603
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002604static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002605 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002607 return proc_pident_lookup(dir, dentry,
2608 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002611static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002612 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002613 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002614 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615};
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617#endif
2618
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002619#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002620static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2621 size_t count, loff_t *ppos)
2622{
Al Viro496ad9a2013-01-23 17:07:38 -05002623 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002624 struct mm_struct *mm;
2625 char buffer[PROC_NUMBUF];
2626 size_t len;
2627 int ret;
2628
2629 if (!task)
2630 return -ESRCH;
2631
2632 ret = 0;
2633 mm = get_task_mm(task);
2634 if (mm) {
2635 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2636 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2637 MMF_DUMP_FILTER_SHIFT));
2638 mmput(mm);
2639 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2640 }
2641
2642 put_task_struct(task);
2643
2644 return ret;
2645}
2646
2647static ssize_t proc_coredump_filter_write(struct file *file,
2648 const char __user *buf,
2649 size_t count,
2650 loff_t *ppos)
2651{
2652 struct task_struct *task;
2653 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002654 unsigned int val;
2655 int ret;
2656 int i;
2657 unsigned long mask;
2658
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002659 ret = kstrtouint_from_user(buf, count, 0, &val);
2660 if (ret < 0)
2661 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002662
2663 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002664 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002665 if (!task)
2666 goto out_no_task;
2667
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002668 mm = get_task_mm(task);
2669 if (!mm)
2670 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002671 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002672
2673 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2674 if (val & mask)
2675 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2676 else
2677 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2678 }
2679
2680 mmput(mm);
2681 out_no_mm:
2682 put_task_struct(task);
2683 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002684 if (ret < 0)
2685 return ret;
2686 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002687}
2688
2689static const struct file_operations proc_coredump_filter_operations = {
2690 .read = proc_coredump_filter_read,
2691 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002692 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002693};
2694#endif
2695
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002696#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002697static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002698{
Andrea Righi940389b2008-07-28 00:48:12 +02002699 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002700 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002701 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002702
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002703 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2704 if (result)
2705 return result;
2706
Jann Horncaaee622016-01-20 15:00:04 -08002707 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002708 result = -EACCES;
2709 goto out_unlock;
2710 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002711
Andrea Righi59954772008-07-27 17:29:15 +02002712 if (whole && lock_task_sighand(task, &flags)) {
2713 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002714
Andrea Righi59954772008-07-27 17:29:15 +02002715 task_io_accounting_add(&acct, &task->signal->ioac);
2716 while_each_thread(task, t)
2717 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002718
Andrea Righi59954772008-07-27 17:29:15 +02002719 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002720 }
Joe Perches25ce3192015-04-15 16:18:17 -07002721 seq_printf(m,
2722 "rchar: %llu\n"
2723 "wchar: %llu\n"
2724 "syscr: %llu\n"
2725 "syscw: %llu\n"
2726 "read_bytes: %llu\n"
2727 "write_bytes: %llu\n"
2728 "cancelled_write_bytes: %llu\n",
2729 (unsigned long long)acct.rchar,
2730 (unsigned long long)acct.wchar,
2731 (unsigned long long)acct.syscr,
2732 (unsigned long long)acct.syscw,
2733 (unsigned long long)acct.read_bytes,
2734 (unsigned long long)acct.write_bytes,
2735 (unsigned long long)acct.cancelled_write_bytes);
2736 result = 0;
2737
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002738out_unlock:
2739 mutex_unlock(&task->signal->cred_guard_mutex);
2740 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002741}
Andrea Righi297c5d92008-07-25 01:48:49 -07002742
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002743static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2744 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002745{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002746 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002747}
2748
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002749static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2750 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002751{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002752 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002753}
2754#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002755
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002756#ifdef CONFIG_USER_NS
2757static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002758 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002759{
2760 struct user_namespace *ns = NULL;
2761 struct task_struct *task;
2762 struct seq_file *seq;
2763 int ret = -EINVAL;
2764
2765 task = get_proc_task(inode);
2766 if (task) {
2767 rcu_read_lock();
2768 ns = get_user_ns(task_cred_xxx(task, user_ns));
2769 rcu_read_unlock();
2770 put_task_struct(task);
2771 }
2772 if (!ns)
2773 goto err;
2774
2775 ret = seq_open(file, seq_ops);
2776 if (ret)
2777 goto err_put_ns;
2778
2779 seq = file->private_data;
2780 seq->private = ns;
2781
2782 return 0;
2783err_put_ns:
2784 put_user_ns(ns);
2785err:
2786 return ret;
2787}
2788
2789static int proc_id_map_release(struct inode *inode, struct file *file)
2790{
2791 struct seq_file *seq = file->private_data;
2792 struct user_namespace *ns = seq->private;
2793 put_user_ns(ns);
2794 return seq_release(inode, file);
2795}
2796
2797static int proc_uid_map_open(struct inode *inode, struct file *file)
2798{
2799 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2800}
2801
2802static int proc_gid_map_open(struct inode *inode, struct file *file)
2803{
2804 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2805}
2806
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002807static int proc_projid_map_open(struct inode *inode, struct file *file)
2808{
2809 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2810}
2811
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002812static const struct file_operations proc_uid_map_operations = {
2813 .open = proc_uid_map_open,
2814 .write = proc_uid_map_write,
2815 .read = seq_read,
2816 .llseek = seq_lseek,
2817 .release = proc_id_map_release,
2818};
2819
2820static const struct file_operations proc_gid_map_operations = {
2821 .open = proc_gid_map_open,
2822 .write = proc_gid_map_write,
2823 .read = seq_read,
2824 .llseek = seq_lseek,
2825 .release = proc_id_map_release,
2826};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002827
2828static const struct file_operations proc_projid_map_operations = {
2829 .open = proc_projid_map_open,
2830 .write = proc_projid_map_write,
2831 .read = seq_read,
2832 .llseek = seq_lseek,
2833 .release = proc_id_map_release,
2834};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002835
2836static int proc_setgroups_open(struct inode *inode, struct file *file)
2837{
2838 struct user_namespace *ns = NULL;
2839 struct task_struct *task;
2840 int ret;
2841
2842 ret = -ESRCH;
2843 task = get_proc_task(inode);
2844 if (task) {
2845 rcu_read_lock();
2846 ns = get_user_ns(task_cred_xxx(task, user_ns));
2847 rcu_read_unlock();
2848 put_task_struct(task);
2849 }
2850 if (!ns)
2851 goto err;
2852
2853 if (file->f_mode & FMODE_WRITE) {
2854 ret = -EACCES;
2855 if (!ns_capable(ns, CAP_SYS_ADMIN))
2856 goto err_put_ns;
2857 }
2858
2859 ret = single_open(file, &proc_setgroups_show, ns);
2860 if (ret)
2861 goto err_put_ns;
2862
2863 return 0;
2864err_put_ns:
2865 put_user_ns(ns);
2866err:
2867 return ret;
2868}
2869
2870static int proc_setgroups_release(struct inode *inode, struct file *file)
2871{
2872 struct seq_file *seq = file->private_data;
2873 struct user_namespace *ns = seq->private;
2874 int ret = single_release(inode, file);
2875 put_user_ns(ns);
2876 return ret;
2877}
2878
2879static const struct file_operations proc_setgroups_operations = {
2880 .open = proc_setgroups_open,
2881 .write = proc_setgroups_write,
2882 .read = seq_read,
2883 .llseek = seq_lseek,
2884 .release = proc_setgroups_release,
2885};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002886#endif /* CONFIG_USER_NS */
2887
Kees Cook47830722008-10-06 03:11:58 +04002888static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2889 struct pid *pid, struct task_struct *task)
2890{
Al Viroa9712bc2011-03-23 15:52:50 -04002891 int err = lock_trace(task);
2892 if (!err) {
2893 seq_printf(m, "%08x\n", task->personality);
2894 unlock_trace(task);
2895 }
2896 return err;
Kees Cook47830722008-10-06 03:11:58 +04002897}
2898
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002899#ifdef CONFIG_LIVEPATCH
2900static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2901 struct pid *pid, struct task_struct *task)
2902{
2903 seq_printf(m, "%d\n", task->patch_state);
2904 return 0;
2905}
2906#endif /* CONFIG_LIVEPATCH */
2907
Eric W. Biederman801199c2006-10-02 02:18:48 -07002908/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002909 * Thread groups
2910 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002911static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002912static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002913
Eric Dumazetc5141e62007-05-08 00:26:15 -07002914static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002915 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2916 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002917 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002918 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002919 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002920#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002921 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002922#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002924 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002925 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002926 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002927 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002928#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002929 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002930#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002931#ifdef CONFIG_SCHED_AUTOGROUP
2932 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2933#endif
john stultz4614a696b2009-12-14 18:00:05 -08002934 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002935#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002936 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002937#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002938 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002939 ONE("stat", S_IRUGO, proc_tgid_stat),
2940 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002941 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002942#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002943 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002945 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2946 LNK("cwd", proc_cwd_link),
2947 LNK("root", proc_root_link),
2948 LNK("exe", proc_exe_link),
2949 REG("mounts", S_IRUGO, proc_mounts_operations),
2950 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2951 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002952#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002953 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002954 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002955 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002956 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002957#endif
2958#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002959 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002960#endif
2961#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002962 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002963#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002964#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002965 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002966#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302967#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002968 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002969#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002970#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002971 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002972#endif
Paul Menage8793d852007-10-18 23:39:39 -07002973#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002974 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002975#endif
Paul Menagea4243162007-10-18 23:39:35 -07002976#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002977 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002978#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002979 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002980 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002981 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002983 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2984 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002985#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002986#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002987 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002988 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002989#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002990#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002991 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002992#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002993#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002994 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002995#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002996#ifdef CONFIG_USER_NS
2997 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2998 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002999 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003000 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003001#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003002#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003003 REG("timers", S_IRUGO, proc_timers_operations),
3004#endif
John Stultz5de23d42016-03-17 14:20:54 -07003005 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003006#ifdef CONFIG_LIVEPATCH
3007 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3008#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009};
3010
Al Virof0c3b502013-05-16 12:07:31 -04003011static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012{
Al Virof0c3b502013-05-16 12:07:31 -04003013 return proc_pident_readdir(file, ctx,
3014 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015}
3016
Arjan van de Ven00977a52007-02-12 00:55:34 -08003017static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003019 .iterate_shared = proc_tgid_base_readdir,
3020 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003021};
3022
Al Viro00cd8dd2012-06-10 17:13:09 -04003023static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3024{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003025 return proc_pident_lookup(dir, dentry,
3026 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027}
3028
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003029static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003030 .lookup = proc_tgid_base_lookup,
3031 .getattr = pid_getattr,
3032 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003033 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003034};
3035
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003036static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003038 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003039 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003040 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Eric W. Biederman48e64842006-06-26 00:25:48 -07003042 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003043 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003044 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003045 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003046 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003047 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003048 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003051 if (pid == tgid)
3052 return;
3053
Eric W. Biederman48e64842006-06-26 00:25:48 -07003054 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003055 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003056 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003057 if (!leader)
3058 goto out;
3059
3060 name.name = "task";
3061 name.len = strlen(name.name);
3062 dir = d_hash_and_lookup(leader, &name);
3063 if (!dir)
3064 goto out_put_leader;
3065
3066 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003067 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003068 dentry = d_hash_and_lookup(dir, &name);
3069 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003070 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003071 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003073
3074 dput(dir);
3075out_put_leader:
3076 dput(leader);
3077out:
3078 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Randy Dunlap0895e912007-10-21 21:00:10 -07003081/**
3082 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3083 * @task: task that should be flushed.
3084 *
3085 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003086 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003087 * in. This call is supposed to do all of this job.
3088 *
3089 * Looks in the dcache for
3090 * /proc/@pid
3091 * /proc/@tgid/task/@pid
3092 * if either directory is present flushes it and all of it'ts children
3093 * from the dcache.
3094 *
3095 * It is safe and reasonable to cache /proc entries for a task until
3096 * that task exits. After that they just clog up the dcache with
3097 * useless entries, possibly causing useful dcache entries to be
3098 * flushed instead. This routine is proved to flush those useless
3099 * dcache entries at process exit time.
3100 *
3101 * NOTE: This routine is just an optimization so it does not guarantee
3102 * that no dcache entries will exist at process exit time it
3103 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003104 */
3105
3106void proc_flush_task(struct task_struct *task)
3107{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003108 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003109 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003110 struct upid *upid;
3111
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003112 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003113 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003114
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003115 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003116 upid = &pid->numbers[i];
3117 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003118 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003119 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003120}
3121
Al Viro0168b9e2018-05-03 09:21:05 -04003122static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003123 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003124{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003125 struct inode *inode;
3126
Al Viro0168b9e2018-05-03 09:21:05 -04003127 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003128 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003129 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003130
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003131 inode->i_op = &proc_tgid_base_inode_operations;
3132 inode->i_fop = &proc_tgid_base_operations;
3133 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003134
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003135 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003136 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003137
Nick Pigginfb045ad2011-01-07 17:49:55 +11003138 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003139 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003140}
3141
Al Viro00cd8dd2012-06-10 17:13:09 -04003142struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003146 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003147 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003149 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (tgid == ~0U)
3151 goto out;
3152
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003153 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003154 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003155 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (task)
3157 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003158 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (!task)
3160 goto out;
3161
Al Viro0168b9e2018-05-03 09:21:05 -04003162 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003163 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164out:
Al Viro0168b9e2018-05-03 09:21:05 -04003165 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003169 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003170 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003172struct tgid_iter {
3173 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003174 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003175};
3176static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3177{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003178 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003180 if (iter.task)
3181 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003182 rcu_read_lock();
3183retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003184 iter.task = NULL;
3185 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003186 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003187 iter.tgid = pid_nr_ns(pid, ns);
3188 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003189 /* What we to know is if the pid we have find is the
3190 * pid of a thread_group_leader. Testing for task
3191 * being a thread_group_leader is the obvious thing
3192 * todo but there is a window when it fails, due to
3193 * the pid transfer logic in de_thread.
3194 *
3195 * So we perform the straight forward test of seeing
3196 * if the pid we have found is the pid of a thread
3197 * group leader, and don't worry if the task we have
3198 * found doesn't happen to be a thread group leader.
3199 * As we don't care in the case of readdir.
3200 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003201 if (!iter.task || !has_group_leader_pid(iter.task)) {
3202 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003203 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003204 }
3205 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003207 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003208 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
Eric W. Biederman00978752014-07-31 03:10:50 -07003211#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003214int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003216 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003217 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003218 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Al Viro021ada72013-03-29 19:27:05 -04003220 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Eric W. Biederman00978752014-07-31 03:10:50 -07003223 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003224 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003225 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003226 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003227 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003228 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003229 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003230 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003231 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3232 return 0;
3233 ctx->pos = pos = pos + 1;
3234 }
3235 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003236 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003237 for (iter = next_tgid(ns, iter);
3238 iter.task;
3239 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003240 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003241 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003242
3243 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003244 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003245 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003246
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003247 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003248 ctx->pos = iter.tgid + TGID_OFFSET;
3249 if (!proc_fill_cache(file, ctx, name, len,
3250 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003251 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 }
Al Virof0c3b502013-05-16 12:07:31 -04003255 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return 0;
3257}
3258
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003259/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003260 * proc_tid_comm_permission is a special permission function exclusively
3261 * used for the node /proc/<pid>/task/<tid>/comm.
3262 * It bypasses generic permission checks in the case where a task of the same
3263 * task group attempts to access the node.
3264 * The rationale behind this is that glibc and bionic access this node for
3265 * cross thread naming (pthread_set/getname_np(!self)). However, if
3266 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3267 * which locks out the cross thread naming implementation.
3268 * This function makes sure that the node is always accessible for members of
3269 * same thread group.
3270 */
3271static int proc_tid_comm_permission(struct inode *inode, int mask)
3272{
3273 bool is_same_tgroup;
3274 struct task_struct *task;
3275
3276 task = get_proc_task(inode);
3277 if (!task)
3278 return -ESRCH;
3279 is_same_tgroup = same_thread_group(current, task);
3280 put_task_struct(task);
3281
3282 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3283 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3284 * read or written by the members of the corresponding
3285 * thread group.
3286 */
3287 return 0;
3288 }
3289
3290 return generic_permission(inode, mask);
3291}
3292
3293static const struct inode_operations proc_tid_comm_inode_operations = {
3294 .permission = proc_tid_comm_permission,
3295};
3296
3297/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003298 * Tasks
3299 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003300static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003301 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003302 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003303 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003304#ifdef CONFIG_NET
3305 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3306#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003307 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003308 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003309 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003310 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003311 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003312#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003313 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003314#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003315 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3316 &proc_tid_comm_inode_operations,
3317 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003318#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003319 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003320#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003321 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003322 ONE("stat", S_IRUGO, proc_tid_stat),
3323 ONE("statm", S_IRUGO, proc_pid_statm),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003324 REG("maps", S_IRUGO, proc_pid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003325#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003326 REG("children", S_IRUGO, proc_tid_children_operations),
3327#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003328#ifdef CONFIG_NUMA
Vlastimil Babka871305b2018-08-21 21:52:48 -07003329 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003330#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003331 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3332 LNK("cwd", proc_cwd_link),
3333 LNK("root", proc_root_link),
3334 LNK("exe", proc_exe_link),
3335 REG("mounts", S_IRUGO, proc_mounts_operations),
3336 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003337#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003338 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003339 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003340 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003341 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003342#endif
3343#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003344 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003345#endif
3346#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003347 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003348#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003349#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003350 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003351#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303352#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003353 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003354#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003355#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003356 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003357#endif
Paul Menage8793d852007-10-18 23:39:39 -07003358#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003359 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003360#endif
Paul Menagea4243162007-10-18 23:39:35 -07003361#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003362 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003363#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003364 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003365 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003366 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003367#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003368 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003369 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003370#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003371#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003372 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003373 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003374#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003375#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003376 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003377#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003378#ifdef CONFIG_USER_NS
3379 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3380 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003381 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003382 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003383#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003384#ifdef CONFIG_LIVEPATCH
3385 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3386#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003387};
3388
Al Virof0c3b502013-05-16 12:07:31 -04003389static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003390{
Al Virof0c3b502013-05-16 12:07:31 -04003391 return proc_pident_readdir(file, ctx,
3392 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003393}
3394
Al Viro00cd8dd2012-06-10 17:13:09 -04003395static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3396{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003397 return proc_pident_lookup(dir, dentry,
3398 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003399}
3400
Arjan van de Ven00977a52007-02-12 00:55:34 -08003401static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003402 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003403 .iterate_shared = proc_tid_base_readdir,
3404 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003405};
3406
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003407static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003408 .lookup = proc_tid_base_lookup,
3409 .getattr = pid_getattr,
3410 .setattr = proc_setattr,
3411};
3412
Al Viro0168b9e2018-05-03 09:21:05 -04003413static struct dentry *proc_task_instantiate(struct dentry *dentry,
3414 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003415{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003416 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003417 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003418 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003419 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003420
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003421 inode->i_op = &proc_tid_base_inode_operations;
3422 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003423 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003424
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003425 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003426 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003427
Nick Pigginfb045ad2011-01-07 17:49:55 +11003428 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003429 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003430}
3431
Al Viro00cd8dd2012-06-10 17:13:09 -04003432static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003433{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003434 struct task_struct *task;
3435 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003437 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003438 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003439
3440 if (!leader)
3441 goto out_no_task;
3442
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003443 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003444 if (tid == ~0U)
3445 goto out;
3446
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003447 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003448 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003449 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003450 if (task)
3451 get_task_struct(task);
3452 rcu_read_unlock();
3453 if (!task)
3454 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003455 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003456 goto out_drop_task;
3457
Al Viro0168b9e2018-05-03 09:21:05 -04003458 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003459out_drop_task:
3460 put_task_struct(task);
3461out:
3462 put_task_struct(leader);
3463out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003464 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003465}
3466
3467/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003468 * Find the first tid of a thread group to return to user space.
3469 *
3470 * Usually this is just the thread group leader, but if the users
3471 * buffer was too small or there was a seek into the middle of the
3472 * directory we have more work todo.
3473 *
3474 * In the case of a short read we start with find_task_by_pid.
3475 *
3476 * In the case of a seek we start with the leader and walk nr
3477 * threads past it.
3478 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003479static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3480 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003481{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003482 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003483 unsigned long nr = f_pos;
3484
3485 if (nr != f_pos) /* 32bit overflow? */
3486 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003487
Eric W. Biedermancc288732006-06-26 00:26:01 -07003488 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003489 task = pid_task(pid, PIDTYPE_PID);
3490 if (!task)
3491 goto fail;
3492
3493 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003494 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003495 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003496 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003497 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003498 }
3499
3500 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003501 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003502 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003503
3504 /* If we haven't found our starting place yet start
3505 * with the leader and walk nr threads forward.
3506 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003507 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003508 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003509 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003510 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003511 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003512fail:
3513 pos = NULL;
3514 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003515found:
3516 get_task_struct(pos);
3517out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003518 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003519 return pos;
3520}
3521
3522/*
3523 * Find the next thread in the thread list.
3524 * Return NULL if there is an error or no next thread.
3525 *
3526 * The reference to the input task_struct is released.
3527 */
3528static struct task_struct *next_tid(struct task_struct *start)
3529{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003530 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003531 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003532 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003533 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003534 if (thread_group_leader(pos))
3535 pos = NULL;
3536 else
3537 get_task_struct(pos);
3538 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003539 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003540 put_task_struct(start);
3541 return pos;
3542}
3543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003545static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003547 struct inode *inode = file_inode(file);
3548 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003549 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003550 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003552 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003553 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Al Virof0c3b502013-05-16 12:07:31 -04003555 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003558 /* f_version caches the tgid value that the last readdir call couldn't
3559 * return. lseek aka telldir automagically resets f_version to 0.
3560 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003561 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003562 tid = (int)file->f_version;
3563 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003564 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003565 task;
Al Virof0c3b502013-05-16 12:07:31 -04003566 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003567 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003568 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003569 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003570 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003571 if (!proc_fill_cache(file, ctx, name, len,
3572 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003573 /* returning this tgid failed, save it as the first
3574 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003575 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003576 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003580
Al Virof0c3b502013-05-16 12:07:31 -04003581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003583
David Howellsa528d352017-01-31 16:46:22 +00003584static int proc_task_getattr(const struct path *path, struct kstat *stat,
3585 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003586{
David Howellsa528d352017-01-31 16:46:22 +00003587 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003588 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003589 generic_fillattr(inode, stat);
3590
Eric W. Biederman99f89552006-06-26 00:25:55 -07003591 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003592 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003593 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003594 }
3595
3596 return 0;
3597}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003598
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003599static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003600 .lookup = proc_task_lookup,
3601 .getattr = proc_task_getattr,
3602 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003603 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003604};
3605
Arjan van de Ven00977a52007-02-12 00:55:34 -08003606static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003607 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003608 .iterate_shared = proc_task_readdir,
3609 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003610};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003611
3612void __init set_proc_pid_nlink(void)
3613{
3614 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3615 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3616}