blob: ad047977ed0457f78cfe793a3541cb0b8adb27f1 [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
Kees Cook6da2ec52018-06-12 13:55:00 -0700410 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
411 GFP_KERNEL);
Ken Chen2ec220e2008-11-10 11:26:08 +0300412 if (!entries)
413 return -ENOMEM;
414
415 trace.nr_entries = 0;
416 trace.max_entries = MAX_STACK_TRACE_DEPTH;
417 trace.entries = entries;
418 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300419
Al Viroa9712bc2011-03-23 15:52:50 -0400420 err = lock_trace(task);
421 if (!err) {
Alexey Dobriyan5d008fb2018-06-07 17:10:17 -0700422 unsigned int i;
423
Al Viroa9712bc2011-03-23 15:52:50 -0400424 save_stack_trace_tsk(task, &trace);
425
426 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800427 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400428 }
429 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300430 }
431 kfree(entries);
432
Al Viroa9712bc2011-03-23 15:52:50 -0400433 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300434}
435#endif
436
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530437#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*
439 * Provides /proc/PID/schedstat
440 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700441static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
442 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530444 if (unlikely(!sched_info_on()))
445 seq_printf(m, "0 0 0\n");
446 else
447 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700448 (unsigned long long)task->se.sum_exec_runtime,
449 (unsigned long long)task->sched_info.run_delay,
450 task->sched_info.pcount);
451
452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454#endif
455
Arjan van de Ven97455122008-01-25 21:08:34 +0100456#ifdef CONFIG_LATENCYTOP
457static int lstats_show_proc(struct seq_file *m, void *v)
458{
459 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800460 struct inode *inode = m->private;
461 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100462
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800463 if (!task)
464 return -ESRCH;
465 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100466 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800467 struct latency_record *lr = &task->latency_record[i];
468 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100469 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800470 seq_printf(m, "%i %li %li",
471 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100472 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800473 unsigned long bt = lr->backtrace[q];
474 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100475 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800476 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100477 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800478 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100479 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800480 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100481 }
482
483 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800484 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100485 return 0;
486}
487
488static int lstats_open(struct inode *inode, struct file *file)
489{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800490 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800491}
492
Arjan van de Ven97455122008-01-25 21:08:34 +0100493static ssize_t lstats_write(struct file *file, const char __user *buf,
494 size_t count, loff_t *offs)
495{
Al Viro496ad9a2013-01-23 17:07:38 -0500496 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100497
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800498 if (!task)
499 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100500 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800501 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100502
503 return count;
504}
505
506static const struct file_operations proc_lstats_operations = {
507 .open = lstats_open,
508 .read = seq_read,
509 .write = lstats_write,
510 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800511 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100512};
513
514#endif
515
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700516static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
517 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
David Rientjesa7f638f2012-05-29 15:06:47 -0700519 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200520 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Oleg Nesterovef419392016-08-02 14:03:19 -0700522 points = oom_badness(task, NULL, NULL, totalpages) *
523 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700524 seq_printf(m, "%lu\n", points);
525
526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Neil Hormand85f50d2007-10-18 23:40:37 -0700529struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700530 const char *name;
531 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700532};
533
534static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700535 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700536 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
537 [RLIMIT_DATA] = {"Max data size", "bytes"},
538 [RLIMIT_STACK] = {"Max stack size", "bytes"},
539 [RLIMIT_CORE] = {"Max core file size", "bytes"},
540 [RLIMIT_RSS] = {"Max resident set", "bytes"},
541 [RLIMIT_NPROC] = {"Max processes", "processes"},
542 [RLIMIT_NOFILE] = {"Max open files", "files"},
543 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
544 [RLIMIT_AS] = {"Max address space", "bytes"},
545 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
546 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
547 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
548 [RLIMIT_NICE] = {"Max nice priority", NULL},
549 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800550 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700551};
552
553/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700554static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
555 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700556{
557 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700558 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700559
560 struct rlimit rlim[RLIM_NLIMITS];
561
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400562 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700563 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700564 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
565 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700566
567 /*
568 * print the file header
569 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700570 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700571 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700572
573 for (i = 0; i < RLIM_NLIMITS; i++) {
574 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700575 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700576 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700577 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700578 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700579 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700580
581 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700582 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700583 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700584 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700585
586 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700587 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700588 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700589 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700590 }
591
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700592 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700593}
594
Roland McGrathebcb6732008-07-25 19:46:00 -0700595#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700596static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
597 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700598{
599 long nr;
600 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700601 int res;
602
603 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400604 if (res)
605 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700606
607 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700608 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400609 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700610 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400611 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700612 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700613 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
614 nr,
615 args[0], args[1], args[2], args[3], args[4], args[5],
616 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400617 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700618
619 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700620}
621#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/************************************************************************/
624/* Here the fs part begins */
625/************************************************************************/
626
627/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700628static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700630 struct task_struct *task;
631 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700632 /* Allow access to a task's file descriptors if it is us or we
633 * may use ptrace attach to the process and find out that
634 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700635 */
636 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700637 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800638 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700639 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700640 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700641 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800644int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700645{
646 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000647 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700648
649 if (attr->ia_valid & ATTR_MODE)
650 return -EPERM;
651
Jan Kara31051c82016-05-26 16:55:18 +0200652 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200653 if (error)
654 return error;
655
Christoph Hellwig10257742010-06-04 11:30:02 +0200656 setattr_copy(inode, attr);
657 mark_inode_dirty(inode);
658 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700659}
660
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800661/*
662 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
663 * or euid/egid (for hide_pid_min=2)?
664 */
665static bool has_pid_permissions(struct pid_namespace *pid,
666 struct task_struct *task,
667 int hide_pid_min)
668{
669 if (pid->hide_pid < hide_pid_min)
670 return true;
671 if (in_group_p(pid->pid_gid))
672 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800673 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800674}
675
676
677static int proc_pid_permission(struct inode *inode, int mask)
678{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200679 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800680 struct task_struct *task;
681 bool has_perms;
682
683 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800684 if (!task)
685 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800686 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800687 put_task_struct(task);
688
689 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800690 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800691 /*
692 * Let's make getdents(), stat(), and open()
693 * consistent with each other. If a process
694 * may not stat() a file, it shouldn't be seen
695 * in procfs at all.
696 */
697 return -ENOENT;
698 }
699
700 return -EPERM;
701 }
702 return generic_permission(inode, mask);
703}
704
705
706
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800707static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700708 .setattr = proc_setattr,
709};
710
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800711static int proc_single_show(struct seq_file *m, void *v)
712{
713 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200714 struct pid_namespace *ns = proc_pid_ns(inode);
715 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800716 struct task_struct *task;
717 int ret;
718
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800719 task = get_pid_task(pid, PIDTYPE_PID);
720 if (!task)
721 return -ESRCH;
722
723 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
724
725 put_task_struct(task);
726 return ret;
727}
728
729static int proc_single_open(struct inode *inode, struct file *filp)
730{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800731 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800732}
733
734static const struct file_operations proc_single_file_operations = {
735 .open = proc_single_open,
736 .read = seq_read,
737 .llseek = seq_lseek,
738 .release = single_release,
739};
740
Oleg Nesterov5381e162014-10-09 15:25:24 -0700741
742struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
743{
744 struct task_struct *task = get_proc_task(inode);
745 struct mm_struct *mm = ERR_PTR(-ESRCH);
746
747 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800748 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700749 put_task_struct(task);
750
751 if (!IS_ERR_OR_NULL(mm)) {
752 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800753 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700754 /* but do not pin its memory */
755 mmput(mm);
756 }
757 }
758
759 return mm;
760}
761
Cong Wangb409e572012-05-31 16:26:17 -0700762static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700764 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800765
766 if (IS_ERR(mm))
767 return PTR_ERR(mm);
768
Linus Torvaldse2683372012-01-17 15:21:19 -0800769 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
771}
772
Cong Wangb409e572012-05-31 16:26:17 -0700773static int mem_open(struct inode *inode, struct file *file)
774{
Djalal Harounibc452b42012-07-30 14:42:28 -0700775 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
776
777 /* OK to pass negative loff_t, we can catch out-of-range */
778 file->f_mode |= FMODE_UNSIGNED_OFFSET;
779
780 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700781}
782
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100783static ssize_t mem_rw(struct file *file, char __user *buf,
784 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Linus Torvaldse2683372012-01-17 15:21:19 -0800786 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100787 unsigned long addr = *ppos;
788 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700790 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Linus Torvaldse2683372012-01-17 15:21:19 -0800792 if (!mm)
793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Michal Hocko0ee931c2017-09-13 16:28:29 -0700795 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800797 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700799 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800800 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100801 goto free;
802
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700803 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100806 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100808 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 copied = -EFAULT;
810 break;
811 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100812
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100813 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100814 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!copied)
816 copied = -EIO;
817 break;
818 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100819
820 if (!write && copy_to_user(buf, page, this_len)) {
821 copied = -EFAULT;
822 break;
823 }
824
825 buf += this_len;
826 addr += this_len;
827 copied += this_len;
828 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100830 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700831
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100832 mmput(mm);
833free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700834 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return copied;
836}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100838static ssize_t mem_read(struct file *file, char __user *buf,
839 size_t count, loff_t *ppos)
840{
841 return mem_rw(file, buf, count, ppos, 0);
842}
843
844static ssize_t mem_write(struct file *file, const char __user *buf,
845 size_t count, loff_t *ppos)
846{
847 return mem_rw(file, (char __user*)buf, count, ppos, 1);
848}
849
Matt Mackall85863e42008-02-04 22:29:04 -0800850loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 switch (orig) {
853 case 0:
854 file->f_pos = offset;
855 break;
856 case 1:
857 file->f_pos += offset;
858 break;
859 default:
860 return -EINVAL;
861 }
862 force_successful_syscall_return();
863 return file->f_pos;
864}
865
Linus Torvaldse2683372012-01-17 15:21:19 -0800866static int mem_release(struct inode *inode, struct file *file)
867{
868 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100869 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100870 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800871 return 0;
872}
873
Arjan van de Ven00977a52007-02-12 00:55:34 -0800874static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 .llseek = mem_lseek,
876 .read = mem_read,
877 .write = mem_write,
878 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800879 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880};
881
Cong Wangb409e572012-05-31 16:26:17 -0700882static int environ_open(struct inode *inode, struct file *file)
883{
884 return __mem_open(inode, file, PTRACE_MODE_READ);
885}
886
James Pearson315e28c2007-10-16 23:30:17 -0700887static ssize_t environ_read(struct file *file, char __user *buf,
888 size_t count, loff_t *ppos)
889{
James Pearson315e28c2007-10-16 23:30:17 -0700890 char *page;
891 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700892 int ret = 0;
893 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800894 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700895
Mathias Krause8148a732016-05-05 16:22:26 -0700896 /* Ensure the process spawned far enough to have an environment. */
897 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700898 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700899
Michal Hocko0ee931c2017-09-13 16:28:29 -0700900 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700901 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700902 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700903
Al Virod6f64b82011-02-15 22:26:01 -0500904 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800905 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700906 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800907
Yang Shi88aa7cc2018-06-07 17:05:28 -0700908 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800909 env_start = mm->env_start;
910 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700911 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800912
James Pearson315e28c2007-10-16 23:30:17 -0700913 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700914 size_t this_len, max_len;
915 int retval;
916
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800917 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700918 break;
James Pearson315e28c2007-10-16 23:30:17 -0700919
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800920 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700921
Djalal Harounie8905ec2012-07-30 14:42:26 -0700922 max_len = min_t(size_t, PAGE_SIZE, count);
923 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700924
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200925 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700926
927 if (retval <= 0) {
928 ret = retval;
929 break;
930 }
931
932 if (copy_to_user(buf, page, retval)) {
933 ret = -EFAULT;
934 break;
935 }
936
937 ret += retval;
938 src += retval;
939 buf += retval;
940 count -= retval;
941 }
942 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700943 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700944
945free:
James Pearson315e28c2007-10-16 23:30:17 -0700946 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700947 return ret;
948}
949
950static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700951 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700952 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100953 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700954 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700955};
956
Al Viroc5317162016-10-05 18:43:43 -0400957static int auxv_open(struct inode *inode, struct file *file)
958{
959 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
960}
961
962static ssize_t auxv_read(struct file *file, char __user *buf,
963 size_t count, loff_t *ppos)
964{
965 struct mm_struct *mm = file->private_data;
966 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700967
968 if (!mm)
969 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400970 do {
971 nwords += 2;
972 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
973 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
974 nwords * sizeof(mm->saved_auxv[0]));
975}
976
977static const struct file_operations proc_auxv_operations = {
978 .open = auxv_open,
979 .read = auxv_read,
980 .llseek = generic_file_llseek,
981 .release = mem_release,
982};
983
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800984static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
985 loff_t *ppos)
986{
Al Viro496ad9a2013-01-23 17:07:38 -0500987 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800988 char buffer[PROC_NUMBUF];
989 int oom_adj = OOM_ADJUST_MIN;
990 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800991
992 if (!task)
993 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -0700994 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
995 oom_adj = OOM_ADJUST_MAX;
996 else
997 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
998 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800999 put_task_struct(task);
1000 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1001 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1002}
1003
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001004static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1005{
1006 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001007 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001008 struct task_struct *task;
1009 int err = 0;
1010
1011 task = get_proc_task(file_inode(file));
1012 if (!task)
1013 return -ESRCH;
1014
1015 mutex_lock(&oom_adj_mutex);
1016 if (legacy) {
1017 if (oom_adj < task->signal->oom_score_adj &&
1018 !capable(CAP_SYS_RESOURCE)) {
1019 err = -EACCES;
1020 goto err_unlock;
1021 }
1022 /*
1023 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1024 * /proc/pid/oom_score_adj instead.
1025 */
1026 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1027 current->comm, task_pid_nr(current), task_pid_nr(task),
1028 task_pid_nr(task));
1029 } else {
1030 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1031 !capable(CAP_SYS_RESOURCE)) {
1032 err = -EACCES;
1033 goto err_unlock;
1034 }
1035 }
1036
Michal Hocko44a70ade2016-07-28 15:44:43 -07001037 /*
1038 * Make sure we will check other processes sharing the mm if this is
1039 * not vfrok which wants its own oom_score_adj.
1040 * pin the mm so it doesn't go away and get reused after task_unlock
1041 */
1042 if (!task->vfork_done) {
1043 struct task_struct *p = find_lock_task_mm(task);
1044
1045 if (p) {
1046 if (atomic_read(&p->mm->mm_users) > 1) {
1047 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001048 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001049 }
1050 task_unlock(p);
1051 }
1052 }
1053
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001054 task->signal->oom_score_adj = oom_adj;
1055 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1056 task->signal->oom_score_adj_min = (short)oom_adj;
1057 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001058
1059 if (mm) {
1060 struct task_struct *p;
1061
1062 rcu_read_lock();
1063 for_each_process(p) {
1064 if (same_thread_group(task, p))
1065 continue;
1066
1067 /* do not touch kernel threads or the global init */
1068 if (p->flags & PF_KTHREAD || is_global_init(p))
1069 continue;
1070
1071 task_lock(p);
1072 if (!p->vfork_done && process_shares_mm(p, mm)) {
1073 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",
1074 task_pid_nr(p), p->comm,
1075 p->signal->oom_score_adj, oom_adj,
1076 task_pid_nr(task), task->comm);
1077 p->signal->oom_score_adj = oom_adj;
1078 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1079 p->signal->oom_score_adj_min = (short)oom_adj;
1080 }
1081 task_unlock(p);
1082 }
1083 rcu_read_unlock();
1084 mmdrop(mm);
1085 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001086err_unlock:
1087 mutex_unlock(&oom_adj_mutex);
1088 put_task_struct(task);
1089 return err;
1090}
Michal Hockof913da52016-07-28 15:44:37 -07001091
David Rientjesb72bdfa2015-11-05 18:50:32 -08001092/*
1093 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1094 * kernels. The effective policy is defined by oom_score_adj, which has a
1095 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1096 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1097 * Processes that become oom disabled via oom_adj will still be oom disabled
1098 * with this implementation.
1099 *
1100 * oom_adj cannot be removed since existing userspace binaries use it.
1101 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001102static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1103 size_t count, loff_t *ppos)
1104{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001105 char buffer[PROC_NUMBUF];
1106 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001107 int err;
1108
1109 memset(buffer, 0, sizeof(buffer));
1110 if (count > sizeof(buffer) - 1)
1111 count = sizeof(buffer) - 1;
1112 if (copy_from_user(buffer, buf, count)) {
1113 err = -EFAULT;
1114 goto out;
1115 }
1116
1117 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1118 if (err)
1119 goto out;
1120 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1121 oom_adj != OOM_DISABLE) {
1122 err = -EINVAL;
1123 goto out;
1124 }
1125
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001126 /*
1127 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1128 * value is always attainable.
1129 */
1130 if (oom_adj == OOM_ADJUST_MAX)
1131 oom_adj = OOM_SCORE_ADJ_MAX;
1132 else
1133 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1134
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001135 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001136out:
1137 return err < 0 ? err : count;
1138}
1139
1140static const struct file_operations proc_oom_adj_operations = {
1141 .read = oom_adj_read,
1142 .write = oom_adj_write,
1143 .llseek = generic_file_llseek,
1144};
1145
David Rientjesa63d83f2010-08-09 17:19:46 -07001146static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1147 size_t count, loff_t *ppos)
1148{
Al Viro496ad9a2013-01-23 17:07:38 -05001149 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001150 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001151 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001152 size_t len;
1153
1154 if (!task)
1155 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001156 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001157 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001158 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001159 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1160}
1161
1162static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1163 size_t count, loff_t *ppos)
1164{
David Rientjesa63d83f2010-08-09 17:19:46 -07001165 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001166 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001167 int err;
1168
1169 memset(buffer, 0, sizeof(buffer));
1170 if (count > sizeof(buffer) - 1)
1171 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001172 if (copy_from_user(buffer, buf, count)) {
1173 err = -EFAULT;
1174 goto out;
1175 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001176
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001177 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001178 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001179 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001180 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001181 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1182 err = -EINVAL;
1183 goto out;
1184 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001185
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001186 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001187out:
1188 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001189}
1190
1191static const struct file_operations proc_oom_score_adj_operations = {
1192 .read = oom_score_adj_read,
1193 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001194 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001195};
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001198#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1200 size_t count, loff_t *ppos)
1201{
Al Viro496ad9a2013-01-23 17:07:38 -05001202 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001203 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ssize_t length;
1205 char tmpbuf[TMPBUFLEN];
1206
Eric W. Biederman99f89552006-06-26 00:25:55 -07001207 if (!task)
1208 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001210 from_kuid(file->f_cred->user_ns,
1211 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001212 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1214}
1215
1216static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1217 size_t count, loff_t *ppos)
1218{
Al Viro496ad9a2013-01-23 17:07:38 -05001219 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001221 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001222 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001224 rcu_read_lock();
1225 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1226 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001228 }
1229 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (*ppos != 0) {
1232 /* No partial writes. */
1233 return -EINVAL;
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001236 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1237 if (rv < 0)
1238 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001239
1240 /* is userspace tring to explicitly UNSET the loginuid? */
1241 if (loginuid == AUDIT_UID_UNSET) {
1242 kloginuid = INVALID_UID;
1243 } else {
1244 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001245 if (!uid_valid(kloginuid))
1246 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001247 }
1248
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001249 rv = audit_set_loginuid(kloginuid);
1250 if (rv < 0)
1251 return rv;
1252 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Arjan van de Ven00977a52007-02-12 00:55:34 -08001255static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 .read = proc_loginuid_read,
1257 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001258 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259};
Eric Paris1e0bd752008-03-13 08:15:31 -04001260
1261static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1262 size_t count, loff_t *ppos)
1263{
Al Viro496ad9a2013-01-23 17:07:38 -05001264 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001265 struct task_struct *task = get_proc_task(inode);
1266 ssize_t length;
1267 char tmpbuf[TMPBUFLEN];
1268
1269 if (!task)
1270 return -ESRCH;
1271 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1272 audit_get_sessionid(task));
1273 put_task_struct(task);
1274 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1275}
1276
1277static const struct file_operations proc_sessionid_operations = {
1278 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001279 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001280};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281#endif
1282
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001283#ifdef CONFIG_FAULT_INJECTION
1284static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1285 size_t count, loff_t *ppos)
1286{
Al Viro496ad9a2013-01-23 17:07:38 -05001287 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001288 char buffer[PROC_NUMBUF];
1289 size_t len;
1290 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001291
1292 if (!task)
1293 return -ESRCH;
1294 make_it_fail = task->make_it_fail;
1295 put_task_struct(task);
1296
1297 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001298
1299 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001300}
1301
1302static ssize_t proc_fault_inject_write(struct file * file,
1303 const char __user * buf, size_t count, loff_t *ppos)
1304{
1305 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001306 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001307 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001308 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001309
1310 if (!capable(CAP_SYS_RESOURCE))
1311 return -EPERM;
1312 memset(buffer, 0, sizeof(buffer));
1313 if (count > sizeof(buffer) - 1)
1314 count = sizeof(buffer) - 1;
1315 if (copy_from_user(buffer, buf, count))
1316 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001317 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1318 if (rv < 0)
1319 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001320 if (make_it_fail < 0 || make_it_fail > 1)
1321 return -EINVAL;
1322
Al Viro496ad9a2013-01-23 17:07:38 -05001323 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001324 if (!task)
1325 return -ESRCH;
1326 task->make_it_fail = make_it_fail;
1327 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001328
1329 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001330}
1331
Arjan van de Ven00977a52007-02-12 00:55:34 -08001332static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001333 .read = proc_fault_inject_read,
1334 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001335 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001336};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001337
1338static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1339 size_t count, loff_t *ppos)
1340{
1341 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001342 int err;
1343 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001344
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001345 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001346 if (err)
1347 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001348
1349 task = get_proc_task(file_inode(file));
1350 if (!task)
1351 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001352 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001353 put_task_struct(task);
1354
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001355 return count;
1356}
1357
1358static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1359 size_t count, loff_t *ppos)
1360{
1361 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001362 char numbuf[PROC_NUMBUF];
1363 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001364
1365 task = get_proc_task(file_inode(file));
1366 if (!task)
1367 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001368 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001369 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001370 put_task_struct(task);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001371
1372 return len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001373}
1374
1375static const struct file_operations proc_fail_nth_operations = {
1376 .read = proc_fail_nth_read,
1377 .write = proc_fail_nth_write,
1378};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001379#endif
1380
Arjan van de Ven97455122008-01-25 21:08:34 +01001381
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001382#ifdef CONFIG_SCHED_DEBUG
1383/*
1384 * Print out various scheduling related per-task fields:
1385 */
1386static int sched_show(struct seq_file *m, void *v)
1387{
1388 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001389 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001390 struct task_struct *p;
1391
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001392 p = get_proc_task(inode);
1393 if (!p)
1394 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001395 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001396
1397 put_task_struct(p);
1398
1399 return 0;
1400}
1401
1402static ssize_t
1403sched_write(struct file *file, const char __user *buf,
1404 size_t count, loff_t *offset)
1405{
Al Viro496ad9a2013-01-23 17:07:38 -05001406 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001407 struct task_struct *p;
1408
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001409 p = get_proc_task(inode);
1410 if (!p)
1411 return -ESRCH;
1412 proc_sched_set_task(p);
1413
1414 put_task_struct(p);
1415
1416 return count;
1417}
1418
1419static int sched_open(struct inode *inode, struct file *filp)
1420{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001421 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001422}
1423
1424static const struct file_operations proc_pid_sched_operations = {
1425 .open = sched_open,
1426 .read = seq_read,
1427 .write = sched_write,
1428 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001429 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001430};
1431
1432#endif
1433
Mike Galbraith5091faa2010-11-30 14:18:03 +01001434#ifdef CONFIG_SCHED_AUTOGROUP
1435/*
1436 * Print out autogroup related information:
1437 */
1438static int sched_autogroup_show(struct seq_file *m, void *v)
1439{
1440 struct inode *inode = m->private;
1441 struct task_struct *p;
1442
1443 p = get_proc_task(inode);
1444 if (!p)
1445 return -ESRCH;
1446 proc_sched_autogroup_show_task(p, m);
1447
1448 put_task_struct(p);
1449
1450 return 0;
1451}
1452
1453static ssize_t
1454sched_autogroup_write(struct file *file, const char __user *buf,
1455 size_t count, loff_t *offset)
1456{
Al Viro496ad9a2013-01-23 17:07:38 -05001457 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001458 struct task_struct *p;
1459 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001460 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001461 int err;
1462
1463 memset(buffer, 0, sizeof(buffer));
1464 if (count > sizeof(buffer) - 1)
1465 count = sizeof(buffer) - 1;
1466 if (copy_from_user(buffer, buf, count))
1467 return -EFAULT;
1468
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001469 err = kstrtoint(strstrip(buffer), 0, &nice);
1470 if (err < 0)
1471 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001472
1473 p = get_proc_task(inode);
1474 if (!p)
1475 return -ESRCH;
1476
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001477 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001478 if (err)
1479 count = err;
1480
1481 put_task_struct(p);
1482
1483 return count;
1484}
1485
1486static int sched_autogroup_open(struct inode *inode, struct file *filp)
1487{
1488 int ret;
1489
1490 ret = single_open(filp, sched_autogroup_show, NULL);
1491 if (!ret) {
1492 struct seq_file *m = filp->private_data;
1493
1494 m->private = inode;
1495 }
1496 return ret;
1497}
1498
1499static const struct file_operations proc_pid_sched_autogroup_operations = {
1500 .open = sched_autogroup_open,
1501 .read = seq_read,
1502 .write = sched_autogroup_write,
1503 .llseek = seq_lseek,
1504 .release = single_release,
1505};
1506
1507#endif /* CONFIG_SCHED_AUTOGROUP */
1508
john stultz4614a696b2009-12-14 18:00:05 -08001509static ssize_t comm_write(struct file *file, const char __user *buf,
1510 size_t count, loff_t *offset)
1511{
Al Viro496ad9a2013-01-23 17:07:38 -05001512 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001513 struct task_struct *p;
1514 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001515 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001516
1517 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001518 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001519 return -EFAULT;
1520
1521 p = get_proc_task(inode);
1522 if (!p)
1523 return -ESRCH;
1524
1525 if (same_thread_group(current, p))
1526 set_task_comm(p, buffer);
1527 else
1528 count = -EINVAL;
1529
1530 put_task_struct(p);
1531
1532 return count;
1533}
1534
1535static int comm_show(struct seq_file *m, void *v)
1536{
1537 struct inode *inode = m->private;
1538 struct task_struct *p;
1539
1540 p = get_proc_task(inode);
1541 if (!p)
1542 return -ESRCH;
1543
Tejun Heo88b72b32018-05-18 08:47:13 -07001544 proc_task_name(m, p, false);
1545 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001546
1547 put_task_struct(p);
1548
1549 return 0;
1550}
1551
1552static int comm_open(struct inode *inode, struct file *filp)
1553{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001554 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001555}
1556
1557static const struct file_operations proc_pid_set_comm_operations = {
1558 .open = comm_open,
1559 .read = seq_read,
1560 .write = comm_write,
1561 .llseek = seq_lseek,
1562 .release = single_release,
1563};
1564
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001565static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001566{
1567 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001568 struct file *exe_file;
1569
David Howells2b0143b2015-03-17 22:25:59 +00001570 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001571 if (!task)
1572 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001573 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001574 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001575 if (exe_file) {
1576 *exe_path = exe_file->f_path;
1577 path_get(&exe_file->f_path);
1578 fput(exe_file);
1579 return 0;
1580 } else
1581 return -ENOENT;
1582}
1583
Al Viro6b255392015-11-17 10:20:54 -05001584static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001585 struct inode *inode,
1586 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001588 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int error = -EACCES;
1590
Al Viro6b255392015-11-17 10:20:54 -05001591 if (!dentry)
1592 return ERR_PTR(-ECHILD);
1593
Eric W. Biederman778c1142006-06-26 00:25:58 -07001594 /* Are we allowed to snoop on the tasks file descriptors? */
1595 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Christoph Hellwig408ef012012-06-18 10:47:03 -04001598 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1599 if (error)
1600 goto out;
1601
Al Viro6e77137b2015-05-02 13:37:52 -04001602 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001603 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604out:
Al Viro008b1502005-08-20 00:17:39 +01001605 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001608static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001610 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001611 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int len;
1613
1614 if (!tmp)
1615 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001616
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001617 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001618 len = PTR_ERR(pathname);
1619 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001621 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 if (len > buflen)
1624 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001625 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 len = -EFAULT;
1627 out:
1628 free_page((unsigned long)tmp);
1629 return len;
1630}
1631
1632static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1633{
1634 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001635 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001636 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Eric W. Biederman778c1142006-06-26 00:25:58 -07001638 /* Are we allowed to snoop on the tasks file descriptors? */
1639 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001642 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (error)
1644 goto out;
1645
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001646 error = do_proc_readlink(&path, buffer, buflen);
1647 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return error;
1650}
1651
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001652const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001654 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001655 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656};
1657
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001658
1659/* building an inode */
1660
Al Viroc6eb50d2017-09-30 14:45:42 -04001661void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001662 kuid_t *ruid, kgid_t *rgid)
1663{
1664 /* Depending on the state of dumpable compute who should own a
1665 * proc file for a task.
1666 */
1667 const struct cred *cred;
1668 kuid_t uid;
1669 kgid_t gid;
1670
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001671 if (unlikely(task->flags & PF_KTHREAD)) {
1672 *ruid = GLOBAL_ROOT_UID;
1673 *rgid = GLOBAL_ROOT_GID;
1674 return;
1675 }
1676
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001677 /* Default to the tasks effective ownership */
1678 rcu_read_lock();
1679 cred = __task_cred(task);
1680 uid = cred->euid;
1681 gid = cred->egid;
1682 rcu_read_unlock();
1683
1684 /*
1685 * Before the /proc/pid/status file was created the only way to read
1686 * the effective uid of a /process was to stat /proc/pid. Reading
1687 * /proc/pid/status is slow enough that procps and other packages
1688 * kept stating /proc/pid. To keep the rules in /proc simple I have
1689 * made this apply to all per process world readable and executable
1690 * directories.
1691 */
1692 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1693 struct mm_struct *mm;
1694 task_lock(task);
1695 mm = task->mm;
1696 /* Make non-dumpable tasks owned by some root */
1697 if (mm) {
1698 if (get_dumpable(mm) != SUID_DUMP_USER) {
1699 struct user_namespace *user_ns = mm->user_ns;
1700
1701 uid = make_kuid(user_ns, 0);
1702 if (!uid_valid(uid))
1703 uid = GLOBAL_ROOT_UID;
1704
1705 gid = make_kgid(user_ns, 0);
1706 if (!gid_valid(gid))
1707 gid = GLOBAL_ROOT_GID;
1708 }
1709 } else {
1710 uid = GLOBAL_ROOT_UID;
1711 gid = GLOBAL_ROOT_GID;
1712 }
1713 task_unlock(task);
1714 }
1715 *ruid = uid;
1716 *rgid = gid;
1717}
1718
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001719struct inode *proc_pid_make_inode(struct super_block * sb,
1720 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001721{
1722 struct inode * inode;
1723 struct proc_inode *ei;
1724
1725 /* We need a new inode */
1726
1727 inode = new_inode(sb);
1728 if (!inode)
1729 goto out;
1730
1731 /* Common stuff */
1732 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001733 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001734 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001735 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001736 inode->i_op = &proc_def_inode_operations;
1737
1738 /*
1739 * grab the reference to task.
1740 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001741 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001742 if (!ei->pid)
1743 goto out_unlock;
1744
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001745 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001746 security_task_to_inode(task, inode);
1747
1748out:
1749 return inode;
1750
1751out_unlock:
1752 iput(inode);
1753 return NULL;
1754}
1755
David Howellsa528d352017-01-31 16:46:22 +00001756int pid_getattr(const struct path *path, struct kstat *stat,
1757 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001758{
David Howellsa528d352017-01-31 16:46:22 +00001759 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001760 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001762
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001763 generic_fillattr(inode, stat);
1764
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001765 stat->uid = GLOBAL_ROOT_UID;
1766 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001767 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1769 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001770 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001771 rcu_read_unlock();
1772 /*
1773 * This doesn't prevent learning whether PID exists,
1774 * it only makes getattr() consistent with readdir().
1775 */
1776 return -ENOENT;
1777 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001778 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001779 }
1780 rcu_read_unlock();
1781 return 0;
1782}
1783
1784/* dentry stuff */
1785
1786/*
Al Viro1bbc5512018-05-02 21:26:16 -04001787 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1788 */
1789void pid_update_inode(struct task_struct *task, struct inode *inode)
1790{
1791 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1792
1793 inode->i_mode &= ~(S_ISUID | S_ISGID);
1794 security_task_to_inode(task, inode);
1795}
1796
1797/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001798 * Rewrite the inode's ownerships here because the owning task may have
1799 * performed a setuid(), etc.
1800 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001801 */
Al Viro1bbc5512018-05-02 21:26:16 -04001802static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001803{
Nick Piggin34286d62011-01-07 17:49:57 +11001804 struct inode *inode;
1805 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001806
Al Viro0b728e12012-06-10 16:03:43 -04001807 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001808 return -ECHILD;
1809
David Howells2b0143b2015-03-17 22:25:59 +00001810 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001811 task = get_proc_task(inode);
1812
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001813 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001814 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001815 put_task_struct(task);
1816 return 1;
1817 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001818 return 0;
1819}
1820
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001821static inline bool proc_inode_is_dead(struct inode *inode)
1822{
1823 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1824}
1825
David Howells1dd704b2013-04-12 01:08:50 +01001826int pid_delete_dentry(const struct dentry *dentry)
1827{
1828 /* Is the task we represent dead?
1829 * If so, then don't put the dentry on the lru list,
1830 * kill it immediately.
1831 */
David Howells2b0143b2015-03-17 22:25:59 +00001832 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001833}
1834
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001835const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001836{
1837 .d_revalidate = pid_revalidate,
1838 .d_delete = pid_delete_dentry,
1839};
1840
1841/* Lookups */
1842
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001843/*
1844 * Fill a directory entry.
1845 *
1846 * If possible create the dcache entry and derive our inode number and
1847 * file type from dcache entry.
1848 *
1849 * Since all of the proc inode numbers are dynamically generated, the inode
1850 * numbers do not exist until the inode is cache. This means creating the
1851 * the dcache entry in readdir is necessary to keep the inode numbers
1852 * reported by readdir in sync with the inode numbers reported
1853 * by stat.
1854 */
Al Virof0c3b502013-05-16 12:07:31 -04001855bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07001856 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001857 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001858{
Al Virof0c3b502013-05-16 12:07:31 -04001859 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001860 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001861 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001862 unsigned type = DT_UNKNOWN;
1863 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001864
Al Viro1df98b82013-06-15 11:33:10 +04001865 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001866 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001867 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1868 child = d_alloc_parallel(dir, &qname, &wq);
1869 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001870 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001871 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001872 struct dentry *res;
1873 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001874 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001875 if (unlikely(res)) {
1876 dput(child);
1877 child = res;
Al Virod85b3992018-06-08 01:17:11 -04001878 if (IS_ERR(child))
1879 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001880 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001881 }
1882 }
David Howells2b0143b2015-03-17 22:25:59 +00001883 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001884 ino = inode->i_ino;
1885 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001886 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04001887end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04001888 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001889}
1890
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001891/*
1892 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1893 * which represent vma start and end addresses.
1894 */
1895static int dname_to_vma_addr(struct dentry *dentry,
1896 unsigned long *start, unsigned long *end)
1897{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001898 const char *str = dentry->d_name.name;
1899 unsigned long long sval, eval;
1900 unsigned int len;
1901
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001902 if (str[0] == '0' && str[1] != '-')
1903 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001904 len = _parse_integer(str, 16, &sval);
1905 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001906 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001907 if (sval != (unsigned long)sval)
1908 return -EINVAL;
1909 str += len;
1910
1911 if (*str != '-')
1912 return -EINVAL;
1913 str++;
1914
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001915 if (str[0] == '0' && str[1])
1916 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001917 len = _parse_integer(str, 16, &eval);
1918 if (len & KSTRTOX_OVERFLOW)
1919 return -EINVAL;
1920 if (eval != (unsigned long)eval)
1921 return -EINVAL;
1922 str += len;
1923
1924 if (*str != '\0')
1925 return -EINVAL;
1926
1927 *start = sval;
1928 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001929
1930 return 0;
1931}
1932
Al Viro0b728e12012-06-10 16:03:43 -04001933static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001934{
1935 unsigned long vm_start, vm_end;
1936 bool exact_vma_exists = false;
1937 struct mm_struct *mm = NULL;
1938 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001939 struct inode *inode;
1940 int status = 0;
1941
Al Viro0b728e12012-06-10 16:03:43 -04001942 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001943 return -ECHILD;
1944
David Howells2b0143b2015-03-17 22:25:59 +00001945 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001946 task = get_proc_task(inode);
1947 if (!task)
1948 goto out_notask;
1949
Jann Horncaaee622016-01-20 15:00:04 -08001950 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001951 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001952 goto out;
1953
1954 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1955 down_read(&mm->mmap_sem);
1956 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1957 up_read(&mm->mmap_sem);
1958 }
1959
1960 mmput(mm);
1961
1962 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001963 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1964
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001965 security_task_to_inode(task, inode);
1966 status = 1;
1967 }
1968
1969out:
1970 put_task_struct(task);
1971
1972out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001973 return status;
1974}
1975
1976static const struct dentry_operations tid_map_files_dentry_operations = {
1977 .d_revalidate = map_files_d_revalidate,
1978 .d_delete = pid_delete_dentry,
1979};
1980
Al Viro6b255392015-11-17 10:20:54 -05001981static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001982{
1983 unsigned long vm_start, vm_end;
1984 struct vm_area_struct *vma;
1985 struct task_struct *task;
1986 struct mm_struct *mm;
1987 int rc;
1988
1989 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001990 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001991 if (!task)
1992 goto out;
1993
1994 mm = get_task_mm(task);
1995 put_task_struct(task);
1996 if (!mm)
1997 goto out;
1998
1999 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2000 if (rc)
2001 goto out_mmput;
2002
Artem Fetishev70335ab2014-03-10 15:49:45 -07002003 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002004 down_read(&mm->mmap_sem);
2005 vma = find_exact_vma(mm, vm_start, vm_end);
2006 if (vma && vma->vm_file) {
2007 *path = vma->vm_file->f_path;
2008 path_get(path);
2009 rc = 0;
2010 }
2011 up_read(&mm->mmap_sem);
2012
2013out_mmput:
2014 mmput(mm);
2015out:
2016 return rc;
2017}
2018
2019struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002020 unsigned long start;
2021 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002022 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002023};
2024
Calvin Owensbdb4d102015-09-09 15:35:54 -07002025/*
2026 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2027 * symlinks may be used to bypass permissions on ancestor directories in the
2028 * path to the file in question.
2029 */
2030static const char *
Al Viro6b255392015-11-17 10:20:54 -05002031proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002032 struct inode *inode,
2033 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002034{
2035 if (!capable(CAP_SYS_ADMIN))
2036 return ERR_PTR(-EPERM);
2037
Al Virofceef392015-12-29 15:58:39 -05002038 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002039}
2040
2041/*
Al Viro6b255392015-11-17 10:20:54 -05002042 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002043 */
2044static const struct inode_operations proc_map_files_link_inode_operations = {
2045 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002046 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002047 .setattr = proc_setattr,
2048};
2049
Al Viro0168b9e2018-05-03 09:21:05 -04002050static struct dentry *
2051proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002052 struct task_struct *task, const void *ptr)
2053{
Al Viro7b540d02012-08-27 14:55:26 -04002054 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002055 struct proc_inode *ei;
2056 struct inode *inode;
2057
Al Viro0168b9e2018-05-03 09:21:05 -04002058 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002059 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2060 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002061 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002062 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002063
2064 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002065 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002066
Calvin Owensbdb4d102015-09-09 15:35:54 -07002067 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002068 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002069
2070 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002071 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072}
2073
2074static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002075 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002076{
2077 unsigned long vm_start, vm_end;
2078 struct vm_area_struct *vma;
2079 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002080 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081 struct mm_struct *mm;
2082
Al Viro0168b9e2018-05-03 09:21:05 -04002083 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002084 task = get_proc_task(dir);
2085 if (!task)
2086 goto out;
2087
Al Viro0168b9e2018-05-03 09:21:05 -04002088 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002089 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002090 goto out_put_task;
2091
Al Viro0168b9e2018-05-03 09:21:05 -04002092 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002093 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002094 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002095
2096 mm = get_task_mm(task);
2097 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002098 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002099
2100 down_read(&mm->mmap_sem);
2101 vma = find_exact_vma(mm, vm_start, vm_end);
2102 if (!vma)
2103 goto out_no_vma;
2104
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002105 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002106 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002107 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108
2109out_no_vma:
2110 up_read(&mm->mmap_sem);
2111 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002112out_put_task:
2113 put_task_struct(task);
2114out:
Al Viro0168b9e2018-05-03 09:21:05 -04002115 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002116}
2117
2118static const struct inode_operations proc_map_files_inode_operations = {
2119 .lookup = proc_map_files_lookup,
2120 .permission = proc_fd_permission,
2121 .setattr = proc_setattr,
2122};
2123
2124static int
Al Virof0c3b502013-05-16 12:07:31 -04002125proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002126{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002127 struct vm_area_struct *vma;
2128 struct task_struct *task;
2129 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002130 unsigned long nr_files, pos, i;
2131 struct flex_array *fa = NULL;
2132 struct map_files_info info;
2133 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002134 int ret;
2135
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002136 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002137 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002138 if (!task)
2139 goto out;
2140
2141 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002142 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002143 goto out_put_task;
2144
2145 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002146 if (!dir_emit_dots(file, ctx))
2147 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002148
Al Virof0c3b502013-05-16 12:07:31 -04002149 mm = get_task_mm(task);
2150 if (!mm)
2151 goto out_put_task;
2152 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002153
Al Virof0c3b502013-05-16 12:07:31 -04002154 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002155
Al Virof0c3b502013-05-16 12:07:31 -04002156 /*
2157 * We need two passes here:
2158 *
2159 * 1) Collect vmas of mapped files with mmap_sem taken
2160 * 2) Release mmap_sem and instantiate entries
2161 *
2162 * otherwise we get lockdep complained, since filldir()
2163 * routine might require mmap_sem taken in might_fault().
2164 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002165
Al Virof0c3b502013-05-16 12:07:31 -04002166 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2167 if (vma->vm_file && ++pos > ctx->pos)
2168 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002169 }
Al Virof0c3b502013-05-16 12:07:31 -04002170
2171 if (nr_files) {
2172 fa = flex_array_alloc(sizeof(info), nr_files,
2173 GFP_KERNEL);
2174 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2175 GFP_KERNEL)) {
2176 ret = -ENOMEM;
2177 if (fa)
2178 flex_array_free(fa);
2179 up_read(&mm->mmap_sem);
2180 mmput(mm);
2181 goto out_put_task;
2182 }
2183 for (i = 0, vma = mm->mmap, pos = 2; vma;
2184 vma = vma->vm_next) {
2185 if (!vma->vm_file)
2186 continue;
2187 if (++pos <= ctx->pos)
2188 continue;
2189
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002190 info.start = vma->vm_start;
2191 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002192 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002193 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2194 BUG();
2195 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002196 }
Al Virof0c3b502013-05-16 12:07:31 -04002197 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002198 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002199
2200 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002201 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2202 unsigned int len;
2203
Al Virof0c3b502013-05-16 12:07:31 -04002204 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002205 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002206 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002207 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002208 proc_map_files_instantiate,
2209 task,
2210 (void *)(unsigned long)p->mode))
2211 break;
2212 ctx->pos++;
2213 }
2214 if (fa)
2215 flex_array_free(fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002216
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002217out_put_task:
2218 put_task_struct(task);
2219out:
2220 return ret;
2221}
2222
2223static const struct file_operations proc_map_files_operations = {
2224 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002225 .iterate_shared = proc_map_files_readdir,
2226 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002227};
2228
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002229#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002230struct timers_private {
2231 struct pid *pid;
2232 struct task_struct *task;
2233 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002234 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002235 unsigned long flags;
2236};
2237
2238static void *timers_start(struct seq_file *m, loff_t *pos)
2239{
2240 struct timers_private *tp = m->private;
2241
2242 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2243 if (!tp->task)
2244 return ERR_PTR(-ESRCH);
2245
2246 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2247 if (!tp->sighand)
2248 return ERR_PTR(-ESRCH);
2249
2250 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2251}
2252
2253static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2254{
2255 struct timers_private *tp = m->private;
2256 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2257}
2258
2259static void timers_stop(struct seq_file *m, void *v)
2260{
2261 struct timers_private *tp = m->private;
2262
2263 if (tp->sighand) {
2264 unlock_task_sighand(tp->task, &tp->flags);
2265 tp->sighand = NULL;
2266 }
2267
2268 if (tp->task) {
2269 put_task_struct(tp->task);
2270 tp->task = NULL;
2271 }
2272}
2273
2274static int show_timer(struct seq_file *m, void *v)
2275{
2276 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002277 struct timers_private *tp = m->private;
2278 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002279 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002280 [SIGEV_SIGNAL] = "signal",
2281 [SIGEV_NONE] = "none",
2282 [SIGEV_THREAD] = "thread",
2283 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002284
2285 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002286 notify = timer->it_sigev_notify;
2287
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002288 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002289 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002290 timer->sigq->info.si_signo,
2291 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002292 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002293 nstr[notify & ~SIGEV_THREAD_ID],
2294 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2295 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002296 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002297
2298 return 0;
2299}
2300
2301static const struct seq_operations proc_timers_seq_ops = {
2302 .start = timers_start,
2303 .next = timers_next,
2304 .stop = timers_stop,
2305 .show = show_timer,
2306};
2307
2308static int proc_timers_open(struct inode *inode, struct file *file)
2309{
2310 struct timers_private *tp;
2311
2312 tp = __seq_open_private(file, &proc_timers_seq_ops,
2313 sizeof(struct timers_private));
2314 if (!tp)
2315 return -ENOMEM;
2316
2317 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002318 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002319 return 0;
2320}
2321
2322static const struct file_operations proc_timers_operations = {
2323 .open = proc_timers_open,
2324 .read = seq_read,
2325 .llseek = seq_lseek,
2326 .release = seq_release_private,
2327};
Eric Engestromb5946be2016-03-17 14:20:57 -07002328#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002329
John Stultz5de23d42016-03-17 14:20:54 -07002330static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2331 size_t count, loff_t *offset)
2332{
2333 struct inode *inode = file_inode(file);
2334 struct task_struct *p;
2335 u64 slack_ns;
2336 int err;
2337
2338 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2339 if (err < 0)
2340 return err;
2341
2342 p = get_proc_task(inode);
2343 if (!p)
2344 return -ESRCH;
2345
John Stultz4b2bd5f2016-10-07 17:02:33 -07002346 if (p != current) {
2347 if (!capable(CAP_SYS_NICE)) {
2348 count = -EPERM;
2349 goto out;
2350 }
John Stultz5de23d42016-03-17 14:20:54 -07002351
John Stultz4b2bd5f2016-10-07 17:02:33 -07002352 err = security_task_setscheduler(p);
2353 if (err) {
2354 count = err;
2355 goto out;
2356 }
John Stultz904763e2016-10-07 17:02:29 -07002357 }
2358
John Stultz7abbaf92016-10-07 17:02:26 -07002359 task_lock(p);
2360 if (slack_ns == 0)
2361 p->timer_slack_ns = p->default_timer_slack_ns;
2362 else
2363 p->timer_slack_ns = slack_ns;
2364 task_unlock(p);
2365
2366out:
John Stultz5de23d42016-03-17 14:20:54 -07002367 put_task_struct(p);
2368
2369 return count;
2370}
2371
2372static int timerslack_ns_show(struct seq_file *m, void *v)
2373{
2374 struct inode *inode = m->private;
2375 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002376 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002377
2378 p = get_proc_task(inode);
2379 if (!p)
2380 return -ESRCH;
2381
John Stultz4b2bd5f2016-10-07 17:02:33 -07002382 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002383
John Stultz4b2bd5f2016-10-07 17:02:33 -07002384 if (!capable(CAP_SYS_NICE)) {
2385 err = -EPERM;
2386 goto out;
2387 }
2388 err = security_task_getscheduler(p);
2389 if (err)
2390 goto out;
2391 }
John Stultz904763e2016-10-07 17:02:29 -07002392
John Stultz7abbaf92016-10-07 17:02:26 -07002393 task_lock(p);
2394 seq_printf(m, "%llu\n", p->timer_slack_ns);
2395 task_unlock(p);
2396
2397out:
John Stultz5de23d42016-03-17 14:20:54 -07002398 put_task_struct(p);
2399
2400 return err;
2401}
2402
2403static int timerslack_ns_open(struct inode *inode, struct file *filp)
2404{
2405 return single_open(filp, timerslack_ns_show, inode);
2406}
2407
2408static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2409 .open = timerslack_ns_open,
2410 .read = seq_read,
2411 .write = timerslack_ns_write,
2412 .llseek = seq_lseek,
2413 .release = single_release,
2414};
2415
Al Viro0168b9e2018-05-03 09:21:05 -04002416static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2417 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002418{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002419 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002420 struct inode *inode;
2421 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002422
Al Viro0168b9e2018-05-03 09:21:05 -04002423 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002424 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002425 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002426
2427 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002428 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002429 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002430 if (p->iop)
2431 inode->i_op = p->iop;
2432 if (p->fop)
2433 inode->i_fop = p->fop;
2434 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002435 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002436 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002437 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002438}
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static struct dentry *proc_pident_lookup(struct inode *dir,
2441 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002442 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002443 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002445 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002446 const struct pid_entry *p, *last;
Al Viro0168b9e2018-05-03 09:21:05 -04002447 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Eric W. Biederman99f89552006-06-26 00:25:55 -07002449 if (!task)
2450 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002452 /*
2453 * Yes, it does not scale. And it should not. Don't add
2454 * new entries into /proc/<tgid>/ without very good reasons.
2455 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002456 last = &ents[nents];
2457 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (p->len != dentry->d_name.len)
2459 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002460 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2461 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002465 put_task_struct(task);
2466out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002467 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Al Virof0c3b502013-05-16 12:07:31 -04002470static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002471 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002472{
Al Virof0c3b502013-05-16 12:07:31 -04002473 struct task_struct *task = get_proc_task(file_inode(file));
2474 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002475
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002476 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002477 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002478
Al Virof0c3b502013-05-16 12:07:31 -04002479 if (!dir_emit_dots(file, ctx))
2480 goto out;
2481
2482 if (ctx->pos >= nents + 2)
2483 goto out;
2484
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002485 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002486 if (!proc_fill_cache(file, ctx, p->name, p->len,
2487 proc_pident_instantiate, task, p))
2488 break;
2489 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002491out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002492 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002497static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2498 size_t count, loff_t *ppos)
2499{
Al Viro496ad9a2013-01-23 17:07:38 -05002500 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002501 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502 ssize_t length;
2503 struct task_struct *task = get_proc_task(inode);
2504
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002506 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002507
2508 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002509 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002510 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002512 if (length > 0)
2513 length = simple_read_from_buffer(buf, count, ppos, p, length);
2514 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002515 return length;
2516}
2517
2518static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2519 size_t count, loff_t *ppos)
2520{
Al Viro496ad9a2013-01-23 17:07:38 -05002521 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002522 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523 ssize_t length;
2524 struct task_struct *task = get_proc_task(inode);
2525
2526 length = -ESRCH;
2527 if (!task)
2528 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002529
2530 /* A task may only write its own attributes. */
2531 length = -EACCES;
2532 if (current != task)
2533 goto out;
2534
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002535 if (count > PAGE_SIZE)
2536 count = PAGE_SIZE;
2537
2538 /* No partial writes. */
2539 length = -EINVAL;
2540 if (*ppos != 0)
2541 goto out;
2542
Al Virobb646cd2015-12-24 00:16:30 -05002543 page = memdup_user(buf, count);
2544 if (IS_ERR(page)) {
2545 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002546 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002547 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002548
David Howells107db7c2009-05-08 13:55:27 +01002549 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002550 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002551 if (length < 0)
2552 goto out_free;
2553
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002554 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002555 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002556 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002557out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002558 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002559out:
2560 put_task_struct(task);
2561out_no_task:
2562 return length;
2563}
2564
Arjan van de Ven00977a52007-02-12 00:55:34 -08002565static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002566 .read = proc_pid_attr_read,
2567 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002568 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002569};
2570
Eric Dumazetc5141e62007-05-08 00:26:15 -07002571static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002572 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2573 REG("prev", S_IRUGO, proc_pid_attr_operations),
2574 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2575 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2576 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2577 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002578};
2579
Al Virof0c3b502013-05-16 12:07:31 -04002580static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Al Virof0c3b502013-05-16 12:07:31 -04002582 return proc_pident_readdir(file, ctx,
2583 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Arjan van de Ven00977a52007-02-12 00:55:34 -08002586static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002588 .iterate_shared = proc_attr_dir_readdir,
2589 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590};
2591
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002592static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002593 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002595 return proc_pident_lookup(dir, dentry,
2596 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002599static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002600 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002601 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002602 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603};
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605#endif
2606
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002607#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002608static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2609 size_t count, loff_t *ppos)
2610{
Al Viro496ad9a2013-01-23 17:07:38 -05002611 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002612 struct mm_struct *mm;
2613 char buffer[PROC_NUMBUF];
2614 size_t len;
2615 int ret;
2616
2617 if (!task)
2618 return -ESRCH;
2619
2620 ret = 0;
2621 mm = get_task_mm(task);
2622 if (mm) {
2623 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2624 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2625 MMF_DUMP_FILTER_SHIFT));
2626 mmput(mm);
2627 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2628 }
2629
2630 put_task_struct(task);
2631
2632 return ret;
2633}
2634
2635static ssize_t proc_coredump_filter_write(struct file *file,
2636 const char __user *buf,
2637 size_t count,
2638 loff_t *ppos)
2639{
2640 struct task_struct *task;
2641 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002642 unsigned int val;
2643 int ret;
2644 int i;
2645 unsigned long mask;
2646
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002647 ret = kstrtouint_from_user(buf, count, 0, &val);
2648 if (ret < 0)
2649 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002650
2651 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002652 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002653 if (!task)
2654 goto out_no_task;
2655
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002656 mm = get_task_mm(task);
2657 if (!mm)
2658 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002659 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002660
2661 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2662 if (val & mask)
2663 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2664 else
2665 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2666 }
2667
2668 mmput(mm);
2669 out_no_mm:
2670 put_task_struct(task);
2671 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002672 if (ret < 0)
2673 return ret;
2674 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002675}
2676
2677static const struct file_operations proc_coredump_filter_operations = {
2678 .read = proc_coredump_filter_read,
2679 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002680 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002681};
2682#endif
2683
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002684#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002685static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002686{
Andrea Righi940389b2008-07-28 00:48:12 +02002687 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002688 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002689 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002690
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002691 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2692 if (result)
2693 return result;
2694
Jann Horncaaee622016-01-20 15:00:04 -08002695 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002696 result = -EACCES;
2697 goto out_unlock;
2698 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002699
Andrea Righi59954772008-07-27 17:29:15 +02002700 if (whole && lock_task_sighand(task, &flags)) {
2701 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002702
Andrea Righi59954772008-07-27 17:29:15 +02002703 task_io_accounting_add(&acct, &task->signal->ioac);
2704 while_each_thread(task, t)
2705 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002706
Andrea Righi59954772008-07-27 17:29:15 +02002707 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002708 }
Joe Perches25ce3192015-04-15 16:18:17 -07002709 seq_printf(m,
2710 "rchar: %llu\n"
2711 "wchar: %llu\n"
2712 "syscr: %llu\n"
2713 "syscw: %llu\n"
2714 "read_bytes: %llu\n"
2715 "write_bytes: %llu\n"
2716 "cancelled_write_bytes: %llu\n",
2717 (unsigned long long)acct.rchar,
2718 (unsigned long long)acct.wchar,
2719 (unsigned long long)acct.syscr,
2720 (unsigned long long)acct.syscw,
2721 (unsigned long long)acct.read_bytes,
2722 (unsigned long long)acct.write_bytes,
2723 (unsigned long long)acct.cancelled_write_bytes);
2724 result = 0;
2725
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002726out_unlock:
2727 mutex_unlock(&task->signal->cred_guard_mutex);
2728 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002729}
Andrea Righi297c5d92008-07-25 01:48:49 -07002730
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002731static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2732 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002733{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002734 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002735}
2736
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002737static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2738 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002739{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002740 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002741}
2742#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002743
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002744#ifdef CONFIG_USER_NS
2745static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002746 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002747{
2748 struct user_namespace *ns = NULL;
2749 struct task_struct *task;
2750 struct seq_file *seq;
2751 int ret = -EINVAL;
2752
2753 task = get_proc_task(inode);
2754 if (task) {
2755 rcu_read_lock();
2756 ns = get_user_ns(task_cred_xxx(task, user_ns));
2757 rcu_read_unlock();
2758 put_task_struct(task);
2759 }
2760 if (!ns)
2761 goto err;
2762
2763 ret = seq_open(file, seq_ops);
2764 if (ret)
2765 goto err_put_ns;
2766
2767 seq = file->private_data;
2768 seq->private = ns;
2769
2770 return 0;
2771err_put_ns:
2772 put_user_ns(ns);
2773err:
2774 return ret;
2775}
2776
2777static int proc_id_map_release(struct inode *inode, struct file *file)
2778{
2779 struct seq_file *seq = file->private_data;
2780 struct user_namespace *ns = seq->private;
2781 put_user_ns(ns);
2782 return seq_release(inode, file);
2783}
2784
2785static int proc_uid_map_open(struct inode *inode, struct file *file)
2786{
2787 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2788}
2789
2790static int proc_gid_map_open(struct inode *inode, struct file *file)
2791{
2792 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2793}
2794
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002795static int proc_projid_map_open(struct inode *inode, struct file *file)
2796{
2797 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2798}
2799
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002800static const struct file_operations proc_uid_map_operations = {
2801 .open = proc_uid_map_open,
2802 .write = proc_uid_map_write,
2803 .read = seq_read,
2804 .llseek = seq_lseek,
2805 .release = proc_id_map_release,
2806};
2807
2808static const struct file_operations proc_gid_map_operations = {
2809 .open = proc_gid_map_open,
2810 .write = proc_gid_map_write,
2811 .read = seq_read,
2812 .llseek = seq_lseek,
2813 .release = proc_id_map_release,
2814};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002815
2816static const struct file_operations proc_projid_map_operations = {
2817 .open = proc_projid_map_open,
2818 .write = proc_projid_map_write,
2819 .read = seq_read,
2820 .llseek = seq_lseek,
2821 .release = proc_id_map_release,
2822};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002823
2824static int proc_setgroups_open(struct inode *inode, struct file *file)
2825{
2826 struct user_namespace *ns = NULL;
2827 struct task_struct *task;
2828 int ret;
2829
2830 ret = -ESRCH;
2831 task = get_proc_task(inode);
2832 if (task) {
2833 rcu_read_lock();
2834 ns = get_user_ns(task_cred_xxx(task, user_ns));
2835 rcu_read_unlock();
2836 put_task_struct(task);
2837 }
2838 if (!ns)
2839 goto err;
2840
2841 if (file->f_mode & FMODE_WRITE) {
2842 ret = -EACCES;
2843 if (!ns_capable(ns, CAP_SYS_ADMIN))
2844 goto err_put_ns;
2845 }
2846
2847 ret = single_open(file, &proc_setgroups_show, ns);
2848 if (ret)
2849 goto err_put_ns;
2850
2851 return 0;
2852err_put_ns:
2853 put_user_ns(ns);
2854err:
2855 return ret;
2856}
2857
2858static int proc_setgroups_release(struct inode *inode, struct file *file)
2859{
2860 struct seq_file *seq = file->private_data;
2861 struct user_namespace *ns = seq->private;
2862 int ret = single_release(inode, file);
2863 put_user_ns(ns);
2864 return ret;
2865}
2866
2867static const struct file_operations proc_setgroups_operations = {
2868 .open = proc_setgroups_open,
2869 .write = proc_setgroups_write,
2870 .read = seq_read,
2871 .llseek = seq_lseek,
2872 .release = proc_setgroups_release,
2873};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002874#endif /* CONFIG_USER_NS */
2875
Kees Cook47830722008-10-06 03:11:58 +04002876static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2877 struct pid *pid, struct task_struct *task)
2878{
Al Viroa9712bc2011-03-23 15:52:50 -04002879 int err = lock_trace(task);
2880 if (!err) {
2881 seq_printf(m, "%08x\n", task->personality);
2882 unlock_trace(task);
2883 }
2884 return err;
Kees Cook47830722008-10-06 03:11:58 +04002885}
2886
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002887#ifdef CONFIG_LIVEPATCH
2888static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2889 struct pid *pid, struct task_struct *task)
2890{
2891 seq_printf(m, "%d\n", task->patch_state);
2892 return 0;
2893}
2894#endif /* CONFIG_LIVEPATCH */
2895
Eric W. Biederman801199c2006-10-02 02:18:48 -07002896/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002897 * Thread groups
2898 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002899static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002900static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002901
Eric Dumazetc5141e62007-05-08 00:26:15 -07002902static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002903 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2904 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002905 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002906 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002907 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002908#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002909 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002910#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002911 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002912 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002913 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002914 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002915 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002916#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002917 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002918#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002919#ifdef CONFIG_SCHED_AUTOGROUP
2920 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2921#endif
john stultz4614a696b2009-12-14 18:00:05 -08002922 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002923#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002924 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002925#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002926 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002927 ONE("stat", S_IRUGO, proc_tgid_stat),
2928 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002929 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002930#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002931 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002932#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002933 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2934 LNK("cwd", proc_cwd_link),
2935 LNK("root", proc_root_link),
2936 LNK("exe", proc_exe_link),
2937 REG("mounts", S_IRUGO, proc_mounts_operations),
2938 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2939 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002940#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002941 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002942 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002943 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002944 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945#endif
2946#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002948#endif
2949#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002950 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002951#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002952#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002953 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302955#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002956 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002957#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002958#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002959 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002960#endif
Paul Menage8793d852007-10-18 23:39:39 -07002961#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002962 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002963#endif
Paul Menagea4243162007-10-18 23:39:35 -07002964#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002965 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002966#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002967 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002968 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002969 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002970#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002971 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2972 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002973#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002974#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002975 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002976 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002977#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002978#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002979 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002980#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002981#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002982 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002983#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002984#ifdef CONFIG_USER_NS
2985 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2986 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002987 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002988 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002989#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002990#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002991 REG("timers", S_IRUGO, proc_timers_operations),
2992#endif
John Stultz5de23d42016-03-17 14:20:54 -07002993 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002994#ifdef CONFIG_LIVEPATCH
2995 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
2996#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002997};
2998
Al Virof0c3b502013-05-16 12:07:31 -04002999static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003000{
Al Virof0c3b502013-05-16 12:07:31 -04003001 return proc_pident_readdir(file, ctx,
3002 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003003}
3004
Arjan van de Ven00977a52007-02-12 00:55:34 -08003005static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003006 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003007 .iterate_shared = proc_tgid_base_readdir,
3008 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009};
3010
Al Viro00cd8dd2012-06-10 17:13:09 -04003011static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3012{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003013 return proc_pident_lookup(dir, dentry,
3014 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015}
3016
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003017static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018 .lookup = proc_tgid_base_lookup,
3019 .getattr = pid_getattr,
3020 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003021 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003022};
3023
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003024static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003026 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003027 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003028 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Eric W. Biederman48e64842006-06-26 00:25:48 -07003030 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003031 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003032 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003033 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003034 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003035 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003036 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003039 if (pid == tgid)
3040 return;
3041
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", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003044 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003045 if (!leader)
3046 goto out;
3047
3048 name.name = "task";
3049 name.len = strlen(name.name);
3050 dir = d_hash_and_lookup(leader, &name);
3051 if (!dir)
3052 goto out_put_leader;
3053
3054 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003055 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003056 dentry = d_hash_and_lookup(dir, &name);
3057 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003058 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003059 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003061
3062 dput(dir);
3063out_put_leader:
3064 dput(leader);
3065out:
3066 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
Randy Dunlap0895e912007-10-21 21:00:10 -07003069/**
3070 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3071 * @task: task that should be flushed.
3072 *
3073 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003074 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003075 * in. This call is supposed to do all of this job.
3076 *
3077 * Looks in the dcache for
3078 * /proc/@pid
3079 * /proc/@tgid/task/@pid
3080 * if either directory is present flushes it and all of it'ts children
3081 * from the dcache.
3082 *
3083 * It is safe and reasonable to cache /proc entries for a task until
3084 * that task exits. After that they just clog up the dcache with
3085 * useless entries, possibly causing useful dcache entries to be
3086 * flushed instead. This routine is proved to flush those useless
3087 * dcache entries at process exit time.
3088 *
3089 * NOTE: This routine is just an optimization so it does not guarantee
3090 * that no dcache entries will exist at process exit time it
3091 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003092 */
3093
3094void proc_flush_task(struct task_struct *task)
3095{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003096 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003097 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003098 struct upid *upid;
3099
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003100 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003101 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003102
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003103 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003104 upid = &pid->numbers[i];
3105 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003106 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003107 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003108}
3109
Al Viro0168b9e2018-05-03 09:21:05 -04003110static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003111 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003112{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003113 struct inode *inode;
3114
Al Viro0168b9e2018-05-03 09:21:05 -04003115 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003116 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003117 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003118
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003119 inode->i_op = &proc_tgid_base_inode_operations;
3120 inode->i_fop = &proc_tgid_base_operations;
3121 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003122
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003123 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003124 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003125
Nick Pigginfb045ad2011-01-07 17:49:55 +11003126 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003127 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003128}
3129
Al Viro00cd8dd2012-06-10 17:13:09 -04003130struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
3132 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003134 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003135 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003137 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (tgid == ~0U)
3139 goto out;
3140
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003141 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003142 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003143 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (task)
3145 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003146 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (!task)
3148 goto out;
3149
Al Viro0168b9e2018-05-03 09:21:05 -04003150 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003151 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152out:
Al Viro0168b9e2018-05-03 09:21:05 -04003153 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003157 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003158 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003160struct tgid_iter {
3161 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003162 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003163};
3164static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3165{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003166 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003168 if (iter.task)
3169 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003170 rcu_read_lock();
3171retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003172 iter.task = NULL;
3173 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003174 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003175 iter.tgid = pid_nr_ns(pid, ns);
3176 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003177 /* What we to know is if the pid we have find is the
3178 * pid of a thread_group_leader. Testing for task
3179 * being a thread_group_leader is the obvious thing
3180 * todo but there is a window when it fails, due to
3181 * the pid transfer logic in de_thread.
3182 *
3183 * So we perform the straight forward test of seeing
3184 * if the pid we have found is the pid of a thread
3185 * group leader, and don't worry if the task we have
3186 * found doesn't happen to be a thread group leader.
3187 * As we don't care in the case of readdir.
3188 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003189 if (!iter.task || !has_group_leader_pid(iter.task)) {
3190 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003191 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003192 }
3193 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003195 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003196 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Eric W. Biederman00978752014-07-31 03:10:50 -07003199#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003202int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003204 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003205 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003206 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Al Viro021ada72013-03-29 19:27:05 -04003208 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Eric W. Biederman00978752014-07-31 03:10:50 -07003211 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003212 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003213 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003214 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003215 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003216 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003217 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003218 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003219 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3220 return 0;
3221 ctx->pos = pos = pos + 1;
3222 }
3223 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003224 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003225 for (iter = next_tgid(ns, iter);
3226 iter.task;
3227 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003228 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003229 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003230
3231 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003232 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003233 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003234
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003235 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003236 ctx->pos = iter.tgid + TGID_OFFSET;
3237 if (!proc_fill_cache(file, ctx, name, len,
3238 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003239 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
Al Virof0c3b502013-05-16 12:07:31 -04003243 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return 0;
3245}
3246
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003247/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003248 * proc_tid_comm_permission is a special permission function exclusively
3249 * used for the node /proc/<pid>/task/<tid>/comm.
3250 * It bypasses generic permission checks in the case where a task of the same
3251 * task group attempts to access the node.
3252 * The rationale behind this is that glibc and bionic access this node for
3253 * cross thread naming (pthread_set/getname_np(!self)). However, if
3254 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3255 * which locks out the cross thread naming implementation.
3256 * This function makes sure that the node is always accessible for members of
3257 * same thread group.
3258 */
3259static int proc_tid_comm_permission(struct inode *inode, int mask)
3260{
3261 bool is_same_tgroup;
3262 struct task_struct *task;
3263
3264 task = get_proc_task(inode);
3265 if (!task)
3266 return -ESRCH;
3267 is_same_tgroup = same_thread_group(current, task);
3268 put_task_struct(task);
3269
3270 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3271 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3272 * read or written by the members of the corresponding
3273 * thread group.
3274 */
3275 return 0;
3276 }
3277
3278 return generic_permission(inode, mask);
3279}
3280
3281static const struct inode_operations proc_tid_comm_inode_operations = {
3282 .permission = proc_tid_comm_permission,
3283};
3284
3285/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003286 * Tasks
3287 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003288static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003289 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003290 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003291 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003292#ifdef CONFIG_NET
3293 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3294#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003295 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003296 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003297 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003298 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003299 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003300#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003301 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003302#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003303 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3304 &proc_tid_comm_inode_operations,
3305 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003306#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003307 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003308#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003309 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003310 ONE("stat", S_IRUGO, proc_tid_stat),
3311 ONE("statm", S_IRUGO, proc_pid_statm),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003312 REG("maps", S_IRUGO, proc_pid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003313#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003314 REG("children", S_IRUGO, proc_tid_children_operations),
3315#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003316#ifdef CONFIG_NUMA
Vlastimil Babka871305b2018-08-21 21:52:48 -07003317 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003318#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003319 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3320 LNK("cwd", proc_cwd_link),
3321 LNK("root", proc_root_link),
3322 LNK("exe", proc_exe_link),
3323 REG("mounts", S_IRUGO, proc_mounts_operations),
3324 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003325#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003326 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003327 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003328 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003329 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003330#endif
3331#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003332 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003333#endif
3334#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003335 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003336#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003337#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003338 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003339#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303340#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003341 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003342#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003343#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003344 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003345#endif
Paul Menage8793d852007-10-18 23:39:39 -07003346#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003347 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003348#endif
Paul Menagea4243162007-10-18 23:39:35 -07003349#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003350 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003351#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003352 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003353 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003354 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003355#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003356 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003357 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003358#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003359#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003360 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003361 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003362#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003363#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003364 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003365#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003366#ifdef CONFIG_USER_NS
3367 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3368 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003369 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003370 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003371#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003372#ifdef CONFIG_LIVEPATCH
3373 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3374#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003375};
3376
Al Virof0c3b502013-05-16 12:07:31 -04003377static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003378{
Al Virof0c3b502013-05-16 12:07:31 -04003379 return proc_pident_readdir(file, ctx,
3380 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003381}
3382
Al Viro00cd8dd2012-06-10 17:13:09 -04003383static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3384{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003385 return proc_pident_lookup(dir, dentry,
3386 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003387}
3388
Arjan van de Ven00977a52007-02-12 00:55:34 -08003389static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003390 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003391 .iterate_shared = proc_tid_base_readdir,
3392 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003393};
3394
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003395static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003396 .lookup = proc_tid_base_lookup,
3397 .getattr = pid_getattr,
3398 .setattr = proc_setattr,
3399};
3400
Al Viro0168b9e2018-05-03 09:21:05 -04003401static struct dentry *proc_task_instantiate(struct dentry *dentry,
3402 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003403{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003404 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003405 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003406 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003407 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003408
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003409 inode->i_op = &proc_tid_base_inode_operations;
3410 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003411 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003412
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003413 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003414 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003415
Nick Pigginfb045ad2011-01-07 17:49:55 +11003416 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003417 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003418}
3419
Al Viro00cd8dd2012-06-10 17:13:09 -04003420static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003421{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003422 struct task_struct *task;
3423 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003424 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003425 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003426 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003427
3428 if (!leader)
3429 goto out_no_task;
3430
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003431 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003432 if (tid == ~0U)
3433 goto out;
3434
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003435 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003437 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003438 if (task)
3439 get_task_struct(task);
3440 rcu_read_unlock();
3441 if (!task)
3442 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003443 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003444 goto out_drop_task;
3445
Al Viro0168b9e2018-05-03 09:21:05 -04003446 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003447out_drop_task:
3448 put_task_struct(task);
3449out:
3450 put_task_struct(leader);
3451out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003452 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003453}
3454
3455/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003456 * Find the first tid of a thread group to return to user space.
3457 *
3458 * Usually this is just the thread group leader, but if the users
3459 * buffer was too small or there was a seek into the middle of the
3460 * directory we have more work todo.
3461 *
3462 * In the case of a short read we start with find_task_by_pid.
3463 *
3464 * In the case of a seek we start with the leader and walk nr
3465 * threads past it.
3466 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003467static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3468 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003469{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003470 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003471 unsigned long nr = f_pos;
3472
3473 if (nr != f_pos) /* 32bit overflow? */
3474 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003475
Eric W. Biedermancc288732006-06-26 00:26:01 -07003476 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003477 task = pid_task(pid, PIDTYPE_PID);
3478 if (!task)
3479 goto fail;
3480
3481 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003482 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003483 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003484 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003485 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003486 }
3487
3488 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003489 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003490 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003491
3492 /* If we haven't found our starting place yet start
3493 * with the leader and walk nr threads forward.
3494 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003495 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003496 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003497 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003498 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003499 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003500fail:
3501 pos = NULL;
3502 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003503found:
3504 get_task_struct(pos);
3505out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003506 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003507 return pos;
3508}
3509
3510/*
3511 * Find the next thread in the thread list.
3512 * Return NULL if there is an error or no next thread.
3513 *
3514 * The reference to the input task_struct is released.
3515 */
3516static struct task_struct *next_tid(struct task_struct *start)
3517{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003518 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003519 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003520 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003521 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003522 if (thread_group_leader(pos))
3523 pos = NULL;
3524 else
3525 get_task_struct(pos);
3526 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003527 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003528 put_task_struct(start);
3529 return pos;
3530}
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003533static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003535 struct inode *inode = file_inode(file);
3536 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003537 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003538 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003540 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003541 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Al Virof0c3b502013-05-16 12:07:31 -04003543 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003546 /* f_version caches the tgid value that the last readdir call couldn't
3547 * return. lseek aka telldir automagically resets f_version to 0.
3548 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003549 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003550 tid = (int)file->f_version;
3551 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003552 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003553 task;
Al Virof0c3b502013-05-16 12:07:31 -04003554 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003555 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003556 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003557 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003558 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003559 if (!proc_fill_cache(file, ctx, name, len,
3560 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003561 /* returning this tgid failed, save it as the first
3562 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003563 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003564 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003568
Al Virof0c3b502013-05-16 12:07:31 -04003569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003571
David Howellsa528d352017-01-31 16:46:22 +00003572static int proc_task_getattr(const struct path *path, struct kstat *stat,
3573 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003574{
David Howellsa528d352017-01-31 16:46:22 +00003575 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003576 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003577 generic_fillattr(inode, stat);
3578
Eric W. Biederman99f89552006-06-26 00:25:55 -07003579 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003580 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003581 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003582 }
3583
3584 return 0;
3585}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003586
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003587static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003588 .lookup = proc_task_lookup,
3589 .getattr = proc_task_getattr,
3590 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003591 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003592};
3593
Arjan van de Ven00977a52007-02-12 00:55:34 -08003594static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003595 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003596 .iterate_shared = proc_task_readdir,
3597 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003598};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003599
3600void __init set_proc_pid_nlink(void)
3601{
3602 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3603 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3604}