blob: 4b27e26fc6de8b53fb7ce8a40bfa4c5283c004d6 [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
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700208static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
209 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700211 struct task_struct *tsk;
212 struct mm_struct *mm;
213 char *page;
214 unsigned long count = _count;
215 unsigned long arg_start, arg_end, env_start, env_end;
216 unsigned long len1, len2, len;
217 unsigned long p;
218 char c;
219 ssize_t rv;
220
221 BUG_ON(*pos < 0);
222
223 tsk = get_proc_task(file_inode(file));
224 if (!tsk)
225 return -ESRCH;
226 mm = get_task_mm(tsk);
227 put_task_struct(tsk);
228 if (!mm)
229 return 0;
230 /* Check if process spawned far enough to have cmdline. */
231 if (!mm->env_end) {
232 rv = 0;
233 goto out_mmput;
234 }
235
Michal Hocko0ee931c2017-09-13 16:28:29 -0700236 page = (char *)__get_free_page(GFP_KERNEL);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700237 if (!page) {
238 rv = -ENOMEM;
239 goto out_mmput;
240 }
241
Yang Shi88aa7cc2018-06-07 17:05:28 -0700242 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700243 arg_start = mm->arg_start;
244 arg_end = mm->arg_end;
245 env_start = mm->env_start;
246 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700247 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700248
249 BUG_ON(arg_start > arg_end);
250 BUG_ON(env_start > env_end);
251
252 len1 = arg_end - arg_start;
253 len2 = env_end - env_start;
254
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700255 /* Empty ARGV. */
256 if (len1 == 0) {
257 rv = 0;
258 goto out_free_page;
259 }
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700260 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700261 * Inherently racy -- command line shares address space
262 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700263 */
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700264 if (access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON) != 1) {
265 rv = 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700266 goto out_free_page;
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700267 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700268
269 rv = 0;
270
271 if (c == '\0') {
272 /* Command line (set of strings) occupies whole ARGV. */
273 if (len1 <= *pos)
274 goto out_free_page;
275
276 p = arg_start + *pos;
277 len = len1 - *pos;
278 while (count > 0 && len > 0) {
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700279 unsigned int nr_read;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700280
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700281 nr_read = min3(count, len, PAGE_SIZE);
282 nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
283 if (nr_read == 0)
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700284 goto out_free_page;
285
286 if (copy_to_user(buf, page, nr_read)) {
287 rv = -EFAULT;
288 goto out_free_page;
289 }
290
291 p += nr_read;
292 len -= nr_read;
293 buf += nr_read;
294 count -= nr_read;
295 rv += nr_read;
296 }
297 } else {
298 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700299 * Command line (1 string) occupies ARGV and
300 * extends into ENVP.
301 */
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800302 struct {
303 unsigned long p;
304 unsigned long len;
305 } cmdline[2] = {
306 { .p = arg_start, .len = len1 },
307 { .p = env_start, .len = len2 },
308 };
309 loff_t pos1 = *pos;
310 unsigned int i;
311
312 i = 0;
313 while (i < 2 && pos1 >= cmdline[i].len) {
314 pos1 -= cmdline[i].len;
315 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700316 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800317 while (i < 2) {
318 p = cmdline[i].p + pos1;
319 len = cmdline[i].len - pos1;
320 while (count > 0 && len > 0) {
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700321 unsigned int nr_read, nr_write;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700322
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700323 nr_read = min3(count, len, PAGE_SIZE);
324 nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
325 if (nr_read == 0)
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800326 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700327
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800328 /*
329 * Command line can be shorter than whole ARGV
330 * even if last "marker" byte says it is not.
331 */
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700332 nr_write = strnlen(page, nr_read);
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800333
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700334 if (copy_to_user(buf, page, nr_write)) {
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800335 rv = -EFAULT;
336 goto out_free_page;
337 }
338
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700339 p += nr_write;
340 len -= nr_write;
341 buf += nr_write;
342 count -= nr_write;
343 rv += nr_write;
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800344
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700345 if (nr_write < nr_read)
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800346 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700347 }
348
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800349 /* Only first chunk can be read partially. */
350 pos1 = 0;
351 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700352 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700353 }
354
355out_free_page:
356 free_page((unsigned long)page);
357out_mmput:
358 mmput(mm);
359 if (rv > 0)
360 *pos += rv;
361 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700364static const struct file_operations proc_pid_cmdline_ops = {
365 .read = proc_pid_cmdline_read,
366 .llseek = generic_file_llseek,
367};
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#ifdef CONFIG_KALLSYMS
370/*
371 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
372 * Returns the resolved symbol. If that fails, simply return the address.
373 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700374static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
375 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700377 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700378 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700380 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
381 goto print0;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700384 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700385 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700386 return 0;
387 }
Joe Perches25ce3192015-04-15 16:18:17 -0700388
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700389print0:
390 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393#endif /* CONFIG_KALLSYMS */
394
Al Viroa9712bc2011-03-23 15:52:50 -0400395static int lock_trace(struct task_struct *task)
396{
397 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
398 if (err)
399 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800400 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400401 mutex_unlock(&task->signal->cred_guard_mutex);
402 return -EPERM;
403 }
404 return 0;
405}
406
407static void unlock_trace(struct task_struct *task)
408{
409 mutex_unlock(&task->signal->cred_guard_mutex);
410}
411
Ken Chen2ec220e2008-11-10 11:26:08 +0300412#ifdef CONFIG_STACKTRACE
413
414#define MAX_STACK_TRACE_DEPTH 64
415
416static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
417 struct pid *pid, struct task_struct *task)
418{
419 struct stack_trace trace;
420 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400421 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300422 int i;
423
424 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
425 if (!entries)
426 return -ENOMEM;
427
428 trace.nr_entries = 0;
429 trace.max_entries = MAX_STACK_TRACE_DEPTH;
430 trace.entries = entries;
431 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300432
Al Viroa9712bc2011-03-23 15:52:50 -0400433 err = lock_trace(task);
434 if (!err) {
435 save_stack_trace_tsk(task, &trace);
436
437 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800438 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400439 }
440 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300441 }
442 kfree(entries);
443
Al Viroa9712bc2011-03-23 15:52:50 -0400444 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300445}
446#endif
447
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530448#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/*
450 * Provides /proc/PID/schedstat
451 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700452static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
453 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530455 if (unlikely(!sched_info_on()))
456 seq_printf(m, "0 0 0\n");
457 else
458 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700459 (unsigned long long)task->se.sum_exec_runtime,
460 (unsigned long long)task->sched_info.run_delay,
461 task->sched_info.pcount);
462
463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465#endif
466
Arjan van de Ven97455122008-01-25 21:08:34 +0100467#ifdef CONFIG_LATENCYTOP
468static int lstats_show_proc(struct seq_file *m, void *v)
469{
470 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800471 struct inode *inode = m->private;
472 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100473
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800474 if (!task)
475 return -ESRCH;
476 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100477 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800478 struct latency_record *lr = &task->latency_record[i];
479 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100480 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800481 seq_printf(m, "%i %li %li",
482 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100483 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800484 unsigned long bt = lr->backtrace[q];
485 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100486 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800487 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100488 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800489 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100490 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800491 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100492 }
493
494 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800495 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100496 return 0;
497}
498
499static int lstats_open(struct inode *inode, struct file *file)
500{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800501 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800502}
503
Arjan van de Ven97455122008-01-25 21:08:34 +0100504static ssize_t lstats_write(struct file *file, const char __user *buf,
505 size_t count, loff_t *offs)
506{
Al Viro496ad9a2013-01-23 17:07:38 -0500507 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100508
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800509 if (!task)
510 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100511 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800512 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100513
514 return count;
515}
516
517static const struct file_operations proc_lstats_operations = {
518 .open = lstats_open,
519 .read = seq_read,
520 .write = lstats_write,
521 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800522 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100523};
524
525#endif
526
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700527static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
528 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
David Rientjesa7f638f2012-05-29 15:06:47 -0700530 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200531 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Oleg Nesterovef419392016-08-02 14:03:19 -0700533 points = oom_badness(task, NULL, NULL, totalpages) *
534 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700535 seq_printf(m, "%lu\n", points);
536
537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Neil Hormand85f50d2007-10-18 23:40:37 -0700540struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700541 const char *name;
542 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700543};
544
545static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700546 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700547 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
548 [RLIMIT_DATA] = {"Max data size", "bytes"},
549 [RLIMIT_STACK] = {"Max stack size", "bytes"},
550 [RLIMIT_CORE] = {"Max core file size", "bytes"},
551 [RLIMIT_RSS] = {"Max resident set", "bytes"},
552 [RLIMIT_NPROC] = {"Max processes", "processes"},
553 [RLIMIT_NOFILE] = {"Max open files", "files"},
554 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
555 [RLIMIT_AS] = {"Max address space", "bytes"},
556 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
557 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
558 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
559 [RLIMIT_NICE] = {"Max nice priority", NULL},
560 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800561 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700562};
563
564/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700565static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
566 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700567{
568 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700569 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700570
571 struct rlimit rlim[RLIM_NLIMITS];
572
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400573 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700574 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700575 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
576 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700577
578 /*
579 * print the file header
580 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700581 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700582 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700583
584 for (i = 0; i < RLIM_NLIMITS; i++) {
585 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700586 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700587 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700588 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700589 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700590 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700591
592 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700593 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700594 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700595 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700596
597 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700598 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700599 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700600 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700601 }
602
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700603 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700604}
605
Roland McGrathebcb6732008-07-25 19:46:00 -0700606#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700607static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
608 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700609{
610 long nr;
611 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700612 int res;
613
614 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400615 if (res)
616 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700617
618 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700619 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400620 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700621 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400622 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700623 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700624 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
625 nr,
626 args[0], args[1], args[2], args[3], args[4], args[5],
627 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400628 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700629
630 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700631}
632#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/************************************************************************/
635/* Here the fs part begins */
636/************************************************************************/
637
638/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700639static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700641 struct task_struct *task;
642 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700643 /* Allow access to a task's file descriptors if it is us or we
644 * may use ptrace attach to the process and find out that
645 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700646 */
647 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700648 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800649 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700650 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700651 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700652 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800655int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700656{
657 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000658 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700659
660 if (attr->ia_valid & ATTR_MODE)
661 return -EPERM;
662
Jan Kara31051c82016-05-26 16:55:18 +0200663 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200664 if (error)
665 return error;
666
Christoph Hellwig10257742010-06-04 11:30:02 +0200667 setattr_copy(inode, attr);
668 mark_inode_dirty(inode);
669 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700670}
671
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800672/*
673 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
674 * or euid/egid (for hide_pid_min=2)?
675 */
676static bool has_pid_permissions(struct pid_namespace *pid,
677 struct task_struct *task,
678 int hide_pid_min)
679{
680 if (pid->hide_pid < hide_pid_min)
681 return true;
682 if (in_group_p(pid->pid_gid))
683 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800684 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800685}
686
687
688static int proc_pid_permission(struct inode *inode, int mask)
689{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200690 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800691 struct task_struct *task;
692 bool has_perms;
693
694 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800695 if (!task)
696 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800697 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800698 put_task_struct(task);
699
700 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800701 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800702 /*
703 * Let's make getdents(), stat(), and open()
704 * consistent with each other. If a process
705 * may not stat() a file, it shouldn't be seen
706 * in procfs at all.
707 */
708 return -ENOENT;
709 }
710
711 return -EPERM;
712 }
713 return generic_permission(inode, mask);
714}
715
716
717
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800718static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700719 .setattr = proc_setattr,
720};
721
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800722static int proc_single_show(struct seq_file *m, void *v)
723{
724 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200725 struct pid_namespace *ns = proc_pid_ns(inode);
726 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800727 struct task_struct *task;
728 int ret;
729
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800730 task = get_pid_task(pid, PIDTYPE_PID);
731 if (!task)
732 return -ESRCH;
733
734 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
735
736 put_task_struct(task);
737 return ret;
738}
739
740static int proc_single_open(struct inode *inode, struct file *filp)
741{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800742 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800743}
744
745static const struct file_operations proc_single_file_operations = {
746 .open = proc_single_open,
747 .read = seq_read,
748 .llseek = seq_lseek,
749 .release = single_release,
750};
751
Oleg Nesterov5381e162014-10-09 15:25:24 -0700752
753struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
754{
755 struct task_struct *task = get_proc_task(inode);
756 struct mm_struct *mm = ERR_PTR(-ESRCH);
757
758 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800759 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700760 put_task_struct(task);
761
762 if (!IS_ERR_OR_NULL(mm)) {
763 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800764 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700765 /* but do not pin its memory */
766 mmput(mm);
767 }
768 }
769
770 return mm;
771}
772
Cong Wangb409e572012-05-31 16:26:17 -0700773static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700775 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800776
777 if (IS_ERR(mm))
778 return PTR_ERR(mm);
779
Linus Torvaldse2683372012-01-17 15:21:19 -0800780 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 0;
782}
783
Cong Wangb409e572012-05-31 16:26:17 -0700784static int mem_open(struct inode *inode, struct file *file)
785{
Djalal Harounibc452b42012-07-30 14:42:28 -0700786 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
787
788 /* OK to pass negative loff_t, we can catch out-of-range */
789 file->f_mode |= FMODE_UNSIGNED_OFFSET;
790
791 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700792}
793
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100794static ssize_t mem_rw(struct file *file, char __user *buf,
795 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Linus Torvaldse2683372012-01-17 15:21:19 -0800797 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100798 unsigned long addr = *ppos;
799 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700801 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvaldse2683372012-01-17 15:21:19 -0800803 if (!mm)
804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Michal Hocko0ee931c2017-09-13 16:28:29 -0700806 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800808 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700810 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800811 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100812 goto free;
813
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700814 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100817 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100819 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 copied = -EFAULT;
821 break;
822 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100823
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100824 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100825 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!copied)
827 copied = -EIO;
828 break;
829 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100830
831 if (!write && copy_to_user(buf, page, this_len)) {
832 copied = -EFAULT;
833 break;
834 }
835
836 buf += this_len;
837 addr += this_len;
838 copied += this_len;
839 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100841 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700842
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100843 mmput(mm);
844free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700845 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return copied;
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100849static ssize_t mem_read(struct file *file, char __user *buf,
850 size_t count, loff_t *ppos)
851{
852 return mem_rw(file, buf, count, ppos, 0);
853}
854
855static ssize_t mem_write(struct file *file, const char __user *buf,
856 size_t count, loff_t *ppos)
857{
858 return mem_rw(file, (char __user*)buf, count, ppos, 1);
859}
860
Matt Mackall85863e42008-02-04 22:29:04 -0800861loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 switch (orig) {
864 case 0:
865 file->f_pos = offset;
866 break;
867 case 1:
868 file->f_pos += offset;
869 break;
870 default:
871 return -EINVAL;
872 }
873 force_successful_syscall_return();
874 return file->f_pos;
875}
876
Linus Torvaldse2683372012-01-17 15:21:19 -0800877static int mem_release(struct inode *inode, struct file *file)
878{
879 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100880 if (mm)
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100881 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800882 return 0;
883}
884
Arjan van de Ven00977a52007-02-12 00:55:34 -0800885static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 .llseek = mem_lseek,
887 .read = mem_read,
888 .write = mem_write,
889 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800890 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891};
892
Cong Wangb409e572012-05-31 16:26:17 -0700893static int environ_open(struct inode *inode, struct file *file)
894{
895 return __mem_open(inode, file, PTRACE_MODE_READ);
896}
897
James Pearson315e28c2007-10-16 23:30:17 -0700898static ssize_t environ_read(struct file *file, char __user *buf,
899 size_t count, loff_t *ppos)
900{
James Pearson315e28c2007-10-16 23:30:17 -0700901 char *page;
902 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700903 int ret = 0;
904 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800905 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700906
Mathias Krause8148a732016-05-05 16:22:26 -0700907 /* Ensure the process spawned far enough to have an environment. */
908 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700909 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700910
Michal Hocko0ee931c2017-09-13 16:28:29 -0700911 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700912 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700913 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700914
Al Virod6f64b82011-02-15 22:26:01 -0500915 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800916 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700917 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800918
Yang Shi88aa7cc2018-06-07 17:05:28 -0700919 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800920 env_start = mm->env_start;
921 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700922 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800923
James Pearson315e28c2007-10-16 23:30:17 -0700924 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700925 size_t this_len, max_len;
926 int retval;
927
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800928 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700929 break;
James Pearson315e28c2007-10-16 23:30:17 -0700930
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800931 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700932
Djalal Harounie8905ec2012-07-30 14:42:26 -0700933 max_len = min_t(size_t, PAGE_SIZE, count);
934 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700935
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200936 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700937
938 if (retval <= 0) {
939 ret = retval;
940 break;
941 }
942
943 if (copy_to_user(buf, page, retval)) {
944 ret = -EFAULT;
945 break;
946 }
947
948 ret += retval;
949 src += retval;
950 buf += retval;
951 count -= retval;
952 }
953 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700954 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700955
956free:
James Pearson315e28c2007-10-16 23:30:17 -0700957 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700958 return ret;
959}
960
961static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700962 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700963 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100964 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700965 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700966};
967
Al Viroc5317162016-10-05 18:43:43 -0400968static int auxv_open(struct inode *inode, struct file *file)
969{
970 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
971}
972
973static ssize_t auxv_read(struct file *file, char __user *buf,
974 size_t count, loff_t *ppos)
975{
976 struct mm_struct *mm = file->private_data;
977 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700978
979 if (!mm)
980 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400981 do {
982 nwords += 2;
983 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
984 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
985 nwords * sizeof(mm->saved_auxv[0]));
986}
987
988static const struct file_operations proc_auxv_operations = {
989 .open = auxv_open,
990 .read = auxv_read,
991 .llseek = generic_file_llseek,
992 .release = mem_release,
993};
994
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800995static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
996 loff_t *ppos)
997{
Al Viro496ad9a2013-01-23 17:07:38 -0500998 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800999 char buffer[PROC_NUMBUF];
1000 int oom_adj = OOM_ADJUST_MIN;
1001 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001002
1003 if (!task)
1004 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001005 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1006 oom_adj = OOM_ADJUST_MAX;
1007 else
1008 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1009 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001010 put_task_struct(task);
1011 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1012 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1013}
1014
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001015static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1016{
1017 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001018 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001019 struct task_struct *task;
1020 int err = 0;
1021
1022 task = get_proc_task(file_inode(file));
1023 if (!task)
1024 return -ESRCH;
1025
1026 mutex_lock(&oom_adj_mutex);
1027 if (legacy) {
1028 if (oom_adj < task->signal->oom_score_adj &&
1029 !capable(CAP_SYS_RESOURCE)) {
1030 err = -EACCES;
1031 goto err_unlock;
1032 }
1033 /*
1034 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1035 * /proc/pid/oom_score_adj instead.
1036 */
1037 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1038 current->comm, task_pid_nr(current), task_pid_nr(task),
1039 task_pid_nr(task));
1040 } else {
1041 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1042 !capable(CAP_SYS_RESOURCE)) {
1043 err = -EACCES;
1044 goto err_unlock;
1045 }
1046 }
1047
Michal Hocko44a70ade2016-07-28 15:44:43 -07001048 /*
1049 * Make sure we will check other processes sharing the mm if this is
1050 * not vfrok which wants its own oom_score_adj.
1051 * pin the mm so it doesn't go away and get reused after task_unlock
1052 */
1053 if (!task->vfork_done) {
1054 struct task_struct *p = find_lock_task_mm(task);
1055
1056 if (p) {
1057 if (atomic_read(&p->mm->mm_users) > 1) {
1058 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001059 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001060 }
1061 task_unlock(p);
1062 }
1063 }
1064
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001065 task->signal->oom_score_adj = oom_adj;
1066 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1067 task->signal->oom_score_adj_min = (short)oom_adj;
1068 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001069
1070 if (mm) {
1071 struct task_struct *p;
1072
1073 rcu_read_lock();
1074 for_each_process(p) {
1075 if (same_thread_group(task, p))
1076 continue;
1077
1078 /* do not touch kernel threads or the global init */
1079 if (p->flags & PF_KTHREAD || is_global_init(p))
1080 continue;
1081
1082 task_lock(p);
1083 if (!p->vfork_done && process_shares_mm(p, mm)) {
1084 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",
1085 task_pid_nr(p), p->comm,
1086 p->signal->oom_score_adj, oom_adj,
1087 task_pid_nr(task), task->comm);
1088 p->signal->oom_score_adj = oom_adj;
1089 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1090 p->signal->oom_score_adj_min = (short)oom_adj;
1091 }
1092 task_unlock(p);
1093 }
1094 rcu_read_unlock();
1095 mmdrop(mm);
1096 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001097err_unlock:
1098 mutex_unlock(&oom_adj_mutex);
1099 put_task_struct(task);
1100 return err;
1101}
Michal Hockof913da52016-07-28 15:44:37 -07001102
David Rientjesb72bdfa2015-11-05 18:50:32 -08001103/*
1104 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1105 * kernels. The effective policy is defined by oom_score_adj, which has a
1106 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1107 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1108 * Processes that become oom disabled via oom_adj will still be oom disabled
1109 * with this implementation.
1110 *
1111 * oom_adj cannot be removed since existing userspace binaries use it.
1112 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001113static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1114 size_t count, loff_t *ppos)
1115{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001116 char buffer[PROC_NUMBUF];
1117 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001118 int err;
1119
1120 memset(buffer, 0, sizeof(buffer));
1121 if (count > sizeof(buffer) - 1)
1122 count = sizeof(buffer) - 1;
1123 if (copy_from_user(buffer, buf, count)) {
1124 err = -EFAULT;
1125 goto out;
1126 }
1127
1128 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1129 if (err)
1130 goto out;
1131 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1132 oom_adj != OOM_DISABLE) {
1133 err = -EINVAL;
1134 goto out;
1135 }
1136
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001137 /*
1138 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1139 * value is always attainable.
1140 */
1141 if (oom_adj == OOM_ADJUST_MAX)
1142 oom_adj = OOM_SCORE_ADJ_MAX;
1143 else
1144 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1145
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001146 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001147out:
1148 return err < 0 ? err : count;
1149}
1150
1151static const struct file_operations proc_oom_adj_operations = {
1152 .read = oom_adj_read,
1153 .write = oom_adj_write,
1154 .llseek = generic_file_llseek,
1155};
1156
David Rientjesa63d83f2010-08-09 17:19:46 -07001157static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1158 size_t count, loff_t *ppos)
1159{
Al Viro496ad9a2013-01-23 17:07:38 -05001160 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001161 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001162 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001163 size_t len;
1164
1165 if (!task)
1166 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001167 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001168 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001169 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001170 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1171}
1172
1173static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1174 size_t count, loff_t *ppos)
1175{
David Rientjesa63d83f2010-08-09 17:19:46 -07001176 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001177 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001178 int err;
1179
1180 memset(buffer, 0, sizeof(buffer));
1181 if (count > sizeof(buffer) - 1)
1182 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001183 if (copy_from_user(buffer, buf, count)) {
1184 err = -EFAULT;
1185 goto out;
1186 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001187
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001188 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001189 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001190 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001191 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001192 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1193 err = -EINVAL;
1194 goto out;
1195 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001196
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001197 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001198out:
1199 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001200}
1201
1202static const struct file_operations proc_oom_score_adj_operations = {
1203 .read = oom_score_adj_read,
1204 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001205 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001206};
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001209#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1211 size_t count, loff_t *ppos)
1212{
Al Viro496ad9a2013-01-23 17:07:38 -05001213 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001214 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ssize_t length;
1216 char tmpbuf[TMPBUFLEN];
1217
Eric W. Biederman99f89552006-06-26 00:25:55 -07001218 if (!task)
1219 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001221 from_kuid(file->f_cred->user_ns,
1222 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001223 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1225}
1226
1227static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1228 size_t count, loff_t *ppos)
1229{
Al Viro496ad9a2013-01-23 17:07:38 -05001230 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001232 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001233 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001235 rcu_read_lock();
1236 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1237 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001239 }
1240 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (*ppos != 0) {
1243 /* No partial writes. */
1244 return -EINVAL;
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001247 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1248 if (rv < 0)
1249 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001250
1251 /* is userspace tring to explicitly UNSET the loginuid? */
1252 if (loginuid == AUDIT_UID_UNSET) {
1253 kloginuid = INVALID_UID;
1254 } else {
1255 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001256 if (!uid_valid(kloginuid))
1257 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001258 }
1259
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001260 rv = audit_set_loginuid(kloginuid);
1261 if (rv < 0)
1262 return rv;
1263 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Arjan van de Ven00977a52007-02-12 00:55:34 -08001266static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 .read = proc_loginuid_read,
1268 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001269 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270};
Eric Paris1e0bd752008-03-13 08:15:31 -04001271
1272static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1273 size_t count, loff_t *ppos)
1274{
Al Viro496ad9a2013-01-23 17:07:38 -05001275 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001276 struct task_struct *task = get_proc_task(inode);
1277 ssize_t length;
1278 char tmpbuf[TMPBUFLEN];
1279
1280 if (!task)
1281 return -ESRCH;
1282 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1283 audit_get_sessionid(task));
1284 put_task_struct(task);
1285 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1286}
1287
1288static const struct file_operations proc_sessionid_operations = {
1289 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001290 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001291};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292#endif
1293
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001294#ifdef CONFIG_FAULT_INJECTION
1295static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1296 size_t count, loff_t *ppos)
1297{
Al Viro496ad9a2013-01-23 17:07:38 -05001298 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001299 char buffer[PROC_NUMBUF];
1300 size_t len;
1301 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001302
1303 if (!task)
1304 return -ESRCH;
1305 make_it_fail = task->make_it_fail;
1306 put_task_struct(task);
1307
1308 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001309
1310 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001311}
1312
1313static ssize_t proc_fault_inject_write(struct file * file,
1314 const char __user * buf, size_t count, loff_t *ppos)
1315{
1316 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001317 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001318 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001319 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001320
1321 if (!capable(CAP_SYS_RESOURCE))
1322 return -EPERM;
1323 memset(buffer, 0, sizeof(buffer));
1324 if (count > sizeof(buffer) - 1)
1325 count = sizeof(buffer) - 1;
1326 if (copy_from_user(buffer, buf, count))
1327 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001328 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1329 if (rv < 0)
1330 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001331 if (make_it_fail < 0 || make_it_fail > 1)
1332 return -EINVAL;
1333
Al Viro496ad9a2013-01-23 17:07:38 -05001334 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001335 if (!task)
1336 return -ESRCH;
1337 task->make_it_fail = make_it_fail;
1338 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001339
1340 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001341}
1342
Arjan van de Ven00977a52007-02-12 00:55:34 -08001343static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001344 .read = proc_fault_inject_read,
1345 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001346 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001348
1349static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1350 size_t count, loff_t *ppos)
1351{
1352 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001353 int err;
1354 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001355
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001356 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001357 if (err)
1358 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001359
1360 task = get_proc_task(file_inode(file));
1361 if (!task)
1362 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001363 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001364 put_task_struct(task);
1365
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001366 return count;
1367}
1368
1369static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1370 size_t count, loff_t *ppos)
1371{
1372 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001373 char numbuf[PROC_NUMBUF];
1374 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001375
1376 task = get_proc_task(file_inode(file));
1377 if (!task)
1378 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001379 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001380 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001381 put_task_struct(task);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001382
1383 return len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001384}
1385
1386static const struct file_operations proc_fail_nth_operations = {
1387 .read = proc_fail_nth_read,
1388 .write = proc_fail_nth_write,
1389};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001390#endif
1391
Arjan van de Ven97455122008-01-25 21:08:34 +01001392
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001393#ifdef CONFIG_SCHED_DEBUG
1394/*
1395 * Print out various scheduling related per-task fields:
1396 */
1397static int sched_show(struct seq_file *m, void *v)
1398{
1399 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001400 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001401 struct task_struct *p;
1402
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001403 p = get_proc_task(inode);
1404 if (!p)
1405 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001406 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001407
1408 put_task_struct(p);
1409
1410 return 0;
1411}
1412
1413static ssize_t
1414sched_write(struct file *file, const char __user *buf,
1415 size_t count, loff_t *offset)
1416{
Al Viro496ad9a2013-01-23 17:07:38 -05001417 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001418 struct task_struct *p;
1419
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001420 p = get_proc_task(inode);
1421 if (!p)
1422 return -ESRCH;
1423 proc_sched_set_task(p);
1424
1425 put_task_struct(p);
1426
1427 return count;
1428}
1429
1430static int sched_open(struct inode *inode, struct file *filp)
1431{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001432 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001433}
1434
1435static const struct file_operations proc_pid_sched_operations = {
1436 .open = sched_open,
1437 .read = seq_read,
1438 .write = sched_write,
1439 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001440 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001441};
1442
1443#endif
1444
Mike Galbraith5091faa2010-11-30 14:18:03 +01001445#ifdef CONFIG_SCHED_AUTOGROUP
1446/*
1447 * Print out autogroup related information:
1448 */
1449static int sched_autogroup_show(struct seq_file *m, void *v)
1450{
1451 struct inode *inode = m->private;
1452 struct task_struct *p;
1453
1454 p = get_proc_task(inode);
1455 if (!p)
1456 return -ESRCH;
1457 proc_sched_autogroup_show_task(p, m);
1458
1459 put_task_struct(p);
1460
1461 return 0;
1462}
1463
1464static ssize_t
1465sched_autogroup_write(struct file *file, const char __user *buf,
1466 size_t count, loff_t *offset)
1467{
Al Viro496ad9a2013-01-23 17:07:38 -05001468 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001469 struct task_struct *p;
1470 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001471 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001472 int err;
1473
1474 memset(buffer, 0, sizeof(buffer));
1475 if (count > sizeof(buffer) - 1)
1476 count = sizeof(buffer) - 1;
1477 if (copy_from_user(buffer, buf, count))
1478 return -EFAULT;
1479
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001480 err = kstrtoint(strstrip(buffer), 0, &nice);
1481 if (err < 0)
1482 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001483
1484 p = get_proc_task(inode);
1485 if (!p)
1486 return -ESRCH;
1487
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001488 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001489 if (err)
1490 count = err;
1491
1492 put_task_struct(p);
1493
1494 return count;
1495}
1496
1497static int sched_autogroup_open(struct inode *inode, struct file *filp)
1498{
1499 int ret;
1500
1501 ret = single_open(filp, sched_autogroup_show, NULL);
1502 if (!ret) {
1503 struct seq_file *m = filp->private_data;
1504
1505 m->private = inode;
1506 }
1507 return ret;
1508}
1509
1510static const struct file_operations proc_pid_sched_autogroup_operations = {
1511 .open = sched_autogroup_open,
1512 .read = seq_read,
1513 .write = sched_autogroup_write,
1514 .llseek = seq_lseek,
1515 .release = single_release,
1516};
1517
1518#endif /* CONFIG_SCHED_AUTOGROUP */
1519
john stultz4614a696b2009-12-14 18:00:05 -08001520static ssize_t comm_write(struct file *file, const char __user *buf,
1521 size_t count, loff_t *offset)
1522{
Al Viro496ad9a2013-01-23 17:07:38 -05001523 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001524 struct task_struct *p;
1525 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001526 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001527
1528 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001529 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001530 return -EFAULT;
1531
1532 p = get_proc_task(inode);
1533 if (!p)
1534 return -ESRCH;
1535
1536 if (same_thread_group(current, p))
1537 set_task_comm(p, buffer);
1538 else
1539 count = -EINVAL;
1540
1541 put_task_struct(p);
1542
1543 return count;
1544}
1545
1546static int comm_show(struct seq_file *m, void *v)
1547{
1548 struct inode *inode = m->private;
1549 struct task_struct *p;
1550
1551 p = get_proc_task(inode);
1552 if (!p)
1553 return -ESRCH;
1554
Tejun Heo88b72b32018-05-18 08:47:13 -07001555 proc_task_name(m, p, false);
1556 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001557
1558 put_task_struct(p);
1559
1560 return 0;
1561}
1562
1563static int comm_open(struct inode *inode, struct file *filp)
1564{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001565 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001566}
1567
1568static const struct file_operations proc_pid_set_comm_operations = {
1569 .open = comm_open,
1570 .read = seq_read,
1571 .write = comm_write,
1572 .llseek = seq_lseek,
1573 .release = single_release,
1574};
1575
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001576static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001577{
1578 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001579 struct file *exe_file;
1580
David Howells2b0143b2015-03-17 22:25:59 +00001581 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001582 if (!task)
1583 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001584 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001585 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001586 if (exe_file) {
1587 *exe_path = exe_file->f_path;
1588 path_get(&exe_file->f_path);
1589 fput(exe_file);
1590 return 0;
1591 } else
1592 return -ENOENT;
1593}
1594
Al Viro6b255392015-11-17 10:20:54 -05001595static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001596 struct inode *inode,
1597 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001599 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int error = -EACCES;
1601
Al Viro6b255392015-11-17 10:20:54 -05001602 if (!dentry)
1603 return ERR_PTR(-ECHILD);
1604
Eric W. Biederman778c1142006-06-26 00:25:58 -07001605 /* Are we allowed to snoop on the tasks file descriptors? */
1606 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Christoph Hellwig408ef012012-06-18 10:47:03 -04001609 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1610 if (error)
1611 goto out;
1612
Al Viro6e77137b2015-05-02 13:37:52 -04001613 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001614 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615out:
Al Viro008b1502005-08-20 00:17:39 +01001616 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001619static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001621 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001622 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 int len;
1624
1625 if (!tmp)
1626 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001627
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001628 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001629 len = PTR_ERR(pathname);
1630 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001632 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 if (len > buflen)
1635 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001636 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 len = -EFAULT;
1638 out:
1639 free_page((unsigned long)tmp);
1640 return len;
1641}
1642
1643static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1644{
1645 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001646 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001647 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Eric W. Biederman778c1142006-06-26 00:25:58 -07001649 /* Are we allowed to snoop on the tasks file descriptors? */
1650 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001653 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (error)
1655 goto out;
1656
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001657 error = do_proc_readlink(&path, buffer, buflen);
1658 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return error;
1661}
1662
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001663const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001665 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001666 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667};
1668
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001669
1670/* building an inode */
1671
Al Viroc6eb50d2017-09-30 14:45:42 -04001672void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001673 kuid_t *ruid, kgid_t *rgid)
1674{
1675 /* Depending on the state of dumpable compute who should own a
1676 * proc file for a task.
1677 */
1678 const struct cred *cred;
1679 kuid_t uid;
1680 kgid_t gid;
1681
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001682 if (unlikely(task->flags & PF_KTHREAD)) {
1683 *ruid = GLOBAL_ROOT_UID;
1684 *rgid = GLOBAL_ROOT_GID;
1685 return;
1686 }
1687
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001688 /* Default to the tasks effective ownership */
1689 rcu_read_lock();
1690 cred = __task_cred(task);
1691 uid = cred->euid;
1692 gid = cred->egid;
1693 rcu_read_unlock();
1694
1695 /*
1696 * Before the /proc/pid/status file was created the only way to read
1697 * the effective uid of a /process was to stat /proc/pid. Reading
1698 * /proc/pid/status is slow enough that procps and other packages
1699 * kept stating /proc/pid. To keep the rules in /proc simple I have
1700 * made this apply to all per process world readable and executable
1701 * directories.
1702 */
1703 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1704 struct mm_struct *mm;
1705 task_lock(task);
1706 mm = task->mm;
1707 /* Make non-dumpable tasks owned by some root */
1708 if (mm) {
1709 if (get_dumpable(mm) != SUID_DUMP_USER) {
1710 struct user_namespace *user_ns = mm->user_ns;
1711
1712 uid = make_kuid(user_ns, 0);
1713 if (!uid_valid(uid))
1714 uid = GLOBAL_ROOT_UID;
1715
1716 gid = make_kgid(user_ns, 0);
1717 if (!gid_valid(gid))
1718 gid = GLOBAL_ROOT_GID;
1719 }
1720 } else {
1721 uid = GLOBAL_ROOT_UID;
1722 gid = GLOBAL_ROOT_GID;
1723 }
1724 task_unlock(task);
1725 }
1726 *ruid = uid;
1727 *rgid = gid;
1728}
1729
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001730struct inode *proc_pid_make_inode(struct super_block * sb,
1731 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001732{
1733 struct inode * inode;
1734 struct proc_inode *ei;
1735
1736 /* We need a new inode */
1737
1738 inode = new_inode(sb);
1739 if (!inode)
1740 goto out;
1741
1742 /* Common stuff */
1743 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001744 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001745 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001746 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001747 inode->i_op = &proc_def_inode_operations;
1748
1749 /*
1750 * grab the reference to task.
1751 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001752 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001753 if (!ei->pid)
1754 goto out_unlock;
1755
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001756 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001757 security_task_to_inode(task, inode);
1758
1759out:
1760 return inode;
1761
1762out_unlock:
1763 iput(inode);
1764 return NULL;
1765}
1766
David Howellsa528d352017-01-31 16:46:22 +00001767int pid_getattr(const struct path *path, struct kstat *stat,
1768 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001769{
David Howellsa528d352017-01-31 16:46:22 +00001770 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001771 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001772 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001773
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001774 generic_fillattr(inode, stat);
1775
1776 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001777 stat->uid = GLOBAL_ROOT_UID;
1778 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001779 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1780 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001781 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001782 rcu_read_unlock();
1783 /*
1784 * This doesn't prevent learning whether PID exists,
1785 * it only makes getattr() consistent with readdir().
1786 */
1787 return -ENOENT;
1788 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001789 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001790 }
1791 rcu_read_unlock();
1792 return 0;
1793}
1794
1795/* dentry stuff */
1796
1797/*
Al Viro1bbc5512018-05-02 21:26:16 -04001798 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1799 */
1800void pid_update_inode(struct task_struct *task, struct inode *inode)
1801{
1802 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1803
1804 inode->i_mode &= ~(S_ISUID | S_ISGID);
1805 security_task_to_inode(task, inode);
1806}
1807
1808/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001809 * Rewrite the inode's ownerships here because the owning task may have
1810 * performed a setuid(), etc.
1811 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001812 */
Al Viro1bbc5512018-05-02 21:26:16 -04001813static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001814{
Nick Piggin34286d62011-01-07 17:49:57 +11001815 struct inode *inode;
1816 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001817
Al Viro0b728e12012-06-10 16:03:43 -04001818 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001819 return -ECHILD;
1820
David Howells2b0143b2015-03-17 22:25:59 +00001821 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001822 task = get_proc_task(inode);
1823
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001824 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001825 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001826 put_task_struct(task);
1827 return 1;
1828 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001829 return 0;
1830}
1831
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001832static inline bool proc_inode_is_dead(struct inode *inode)
1833{
1834 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1835}
1836
David Howells1dd704b2013-04-12 01:08:50 +01001837int pid_delete_dentry(const struct dentry *dentry)
1838{
1839 /* Is the task we represent dead?
1840 * If so, then don't put the dentry on the lru list,
1841 * kill it immediately.
1842 */
David Howells2b0143b2015-03-17 22:25:59 +00001843 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001844}
1845
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001846const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001847{
1848 .d_revalidate = pid_revalidate,
1849 .d_delete = pid_delete_dentry,
1850};
1851
1852/* Lookups */
1853
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001854/*
1855 * Fill a directory entry.
1856 *
1857 * If possible create the dcache entry and derive our inode number and
1858 * file type from dcache entry.
1859 *
1860 * Since all of the proc inode numbers are dynamically generated, the inode
1861 * numbers do not exist until the inode is cache. This means creating the
1862 * the dcache entry in readdir is necessary to keep the inode numbers
1863 * reported by readdir in sync with the inode numbers reported
1864 * by stat.
1865 */
Al Virof0c3b502013-05-16 12:07:31 -04001866bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001867 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001868 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001869{
Al Virof0c3b502013-05-16 12:07:31 -04001870 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001871 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001872 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001873 unsigned type = DT_UNKNOWN;
1874 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001875
Al Viro1df98b82013-06-15 11:33:10 +04001876 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001877 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001878 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1879 child = d_alloc_parallel(dir, &qname, &wq);
1880 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001881 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001882 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001883 struct dentry *res;
1884 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001885 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001886 if (IS_ERR(res))
Al Viro37817642016-04-20 16:31:31 -04001887 goto end_instantiate;
Al Viro0168b9e2018-05-03 09:21:05 -04001888 if (unlikely(res)) {
1889 dput(child);
1890 child = res;
Al Viro37817642016-04-20 16:31:31 -04001891 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001892 }
1893 }
David Howells2b0143b2015-03-17 22:25:59 +00001894 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001895 ino = inode->i_ino;
1896 type = inode->i_mode >> 12;
Al Viro0168b9e2018-05-03 09:21:05 -04001897end_instantiate:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001898 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001899 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001900}
1901
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001902/*
1903 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1904 * which represent vma start and end addresses.
1905 */
1906static int dname_to_vma_addr(struct dentry *dentry,
1907 unsigned long *start, unsigned long *end)
1908{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001909 const char *str = dentry->d_name.name;
1910 unsigned long long sval, eval;
1911 unsigned int len;
1912
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001913 if (str[0] == '0' && str[1] != '-')
1914 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001915 len = _parse_integer(str, 16, &sval);
1916 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001917 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001918 if (sval != (unsigned long)sval)
1919 return -EINVAL;
1920 str += len;
1921
1922 if (*str != '-')
1923 return -EINVAL;
1924 str++;
1925
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001926 if (str[0] == '0' && str[1])
1927 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001928 len = _parse_integer(str, 16, &eval);
1929 if (len & KSTRTOX_OVERFLOW)
1930 return -EINVAL;
1931 if (eval != (unsigned long)eval)
1932 return -EINVAL;
1933 str += len;
1934
1935 if (*str != '\0')
1936 return -EINVAL;
1937
1938 *start = sval;
1939 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001940
1941 return 0;
1942}
1943
Al Viro0b728e12012-06-10 16:03:43 -04001944static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001945{
1946 unsigned long vm_start, vm_end;
1947 bool exact_vma_exists = false;
1948 struct mm_struct *mm = NULL;
1949 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001950 struct inode *inode;
1951 int status = 0;
1952
Al Viro0b728e12012-06-10 16:03:43 -04001953 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001954 return -ECHILD;
1955
David Howells2b0143b2015-03-17 22:25:59 +00001956 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001957 task = get_proc_task(inode);
1958 if (!task)
1959 goto out_notask;
1960
Jann Horncaaee622016-01-20 15:00:04 -08001961 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001962 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001963 goto out;
1964
1965 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1966 down_read(&mm->mmap_sem);
1967 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1968 up_read(&mm->mmap_sem);
1969 }
1970
1971 mmput(mm);
1972
1973 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001974 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1975
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001976 security_task_to_inode(task, inode);
1977 status = 1;
1978 }
1979
1980out:
1981 put_task_struct(task);
1982
1983out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001984 return status;
1985}
1986
1987static const struct dentry_operations tid_map_files_dentry_operations = {
1988 .d_revalidate = map_files_d_revalidate,
1989 .d_delete = pid_delete_dentry,
1990};
1991
Al Viro6b255392015-11-17 10:20:54 -05001992static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001993{
1994 unsigned long vm_start, vm_end;
1995 struct vm_area_struct *vma;
1996 struct task_struct *task;
1997 struct mm_struct *mm;
1998 int rc;
1999
2000 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002001 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002002 if (!task)
2003 goto out;
2004
2005 mm = get_task_mm(task);
2006 put_task_struct(task);
2007 if (!mm)
2008 goto out;
2009
2010 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2011 if (rc)
2012 goto out_mmput;
2013
Artem Fetishev70335ab2014-03-10 15:49:45 -07002014 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002015 down_read(&mm->mmap_sem);
2016 vma = find_exact_vma(mm, vm_start, vm_end);
2017 if (vma && vma->vm_file) {
2018 *path = vma->vm_file->f_path;
2019 path_get(path);
2020 rc = 0;
2021 }
2022 up_read(&mm->mmap_sem);
2023
2024out_mmput:
2025 mmput(mm);
2026out:
2027 return rc;
2028}
2029
2030struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002031 unsigned long start;
2032 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002033 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002034};
2035
Calvin Owensbdb4d102015-09-09 15:35:54 -07002036/*
2037 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2038 * symlinks may be used to bypass permissions on ancestor directories in the
2039 * path to the file in question.
2040 */
2041static const char *
Al Viro6b255392015-11-17 10:20:54 -05002042proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002043 struct inode *inode,
2044 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002045{
2046 if (!capable(CAP_SYS_ADMIN))
2047 return ERR_PTR(-EPERM);
2048
Al Virofceef392015-12-29 15:58:39 -05002049 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002050}
2051
2052/*
Al Viro6b255392015-11-17 10:20:54 -05002053 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002054 */
2055static const struct inode_operations proc_map_files_link_inode_operations = {
2056 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002057 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002058 .setattr = proc_setattr,
2059};
2060
Al Viro0168b9e2018-05-03 09:21:05 -04002061static struct dentry *
2062proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002063 struct task_struct *task, const void *ptr)
2064{
Al Viro7b540d02012-08-27 14:55:26 -04002065 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002066 struct proc_inode *ei;
2067 struct inode *inode;
2068
Al Viro0168b9e2018-05-03 09:21:05 -04002069 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002070 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2071 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002073 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002074
2075 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002076 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077
Calvin Owensbdb4d102015-09-09 15:35:54 -07002078 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002079 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002080
2081 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002082 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002083}
2084
2085static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002086 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002087{
2088 unsigned long vm_start, vm_end;
2089 struct vm_area_struct *vma;
2090 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002091 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002092 struct mm_struct *mm;
2093
Al Viro0168b9e2018-05-03 09:21:05 -04002094 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002095 task = get_proc_task(dir);
2096 if (!task)
2097 goto out;
2098
Al Viro0168b9e2018-05-03 09:21:05 -04002099 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002100 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002101 goto out_put_task;
2102
Al Viro0168b9e2018-05-03 09:21:05 -04002103 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002104 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002105 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002106
2107 mm = get_task_mm(task);
2108 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002109 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002110
2111 down_read(&mm->mmap_sem);
2112 vma = find_exact_vma(mm, vm_start, vm_end);
2113 if (!vma)
2114 goto out_no_vma;
2115
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002116 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002117 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002118 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002119
2120out_no_vma:
2121 up_read(&mm->mmap_sem);
2122 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002123out_put_task:
2124 put_task_struct(task);
2125out:
Al Viro0168b9e2018-05-03 09:21:05 -04002126 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002127}
2128
2129static const struct inode_operations proc_map_files_inode_operations = {
2130 .lookup = proc_map_files_lookup,
2131 .permission = proc_fd_permission,
2132 .setattr = proc_setattr,
2133};
2134
2135static int
Al Virof0c3b502013-05-16 12:07:31 -04002136proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002137{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002138 struct vm_area_struct *vma;
2139 struct task_struct *task;
2140 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002141 unsigned long nr_files, pos, i;
2142 struct flex_array *fa = NULL;
2143 struct map_files_info info;
2144 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002145 int ret;
2146
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002147 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002148 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002149 if (!task)
2150 goto out;
2151
2152 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002153 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002154 goto out_put_task;
2155
2156 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002157 if (!dir_emit_dots(file, ctx))
2158 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002159
Al Virof0c3b502013-05-16 12:07:31 -04002160 mm = get_task_mm(task);
2161 if (!mm)
2162 goto out_put_task;
2163 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002164
Al Virof0c3b502013-05-16 12:07:31 -04002165 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002166
Al Virof0c3b502013-05-16 12:07:31 -04002167 /*
2168 * We need two passes here:
2169 *
2170 * 1) Collect vmas of mapped files with mmap_sem taken
2171 * 2) Release mmap_sem and instantiate entries
2172 *
2173 * otherwise we get lockdep complained, since filldir()
2174 * routine might require mmap_sem taken in might_fault().
2175 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002176
Al Virof0c3b502013-05-16 12:07:31 -04002177 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2178 if (vma->vm_file && ++pos > ctx->pos)
2179 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002180 }
Al Virof0c3b502013-05-16 12:07:31 -04002181
2182 if (nr_files) {
2183 fa = flex_array_alloc(sizeof(info), nr_files,
2184 GFP_KERNEL);
2185 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2186 GFP_KERNEL)) {
2187 ret = -ENOMEM;
2188 if (fa)
2189 flex_array_free(fa);
2190 up_read(&mm->mmap_sem);
2191 mmput(mm);
2192 goto out_put_task;
2193 }
2194 for (i = 0, vma = mm->mmap, pos = 2; vma;
2195 vma = vma->vm_next) {
2196 if (!vma->vm_file)
2197 continue;
2198 if (++pos <= ctx->pos)
2199 continue;
2200
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002201 info.start = vma->vm_start;
2202 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002203 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002204 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2205 BUG();
2206 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002207 }
Al Virof0c3b502013-05-16 12:07:31 -04002208 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002209 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002210
2211 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002212 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2213 unsigned int len;
2214
Al Virof0c3b502013-05-16 12:07:31 -04002215 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002216 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002217 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002218 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002219 proc_map_files_instantiate,
2220 task,
2221 (void *)(unsigned long)p->mode))
2222 break;
2223 ctx->pos++;
2224 }
2225 if (fa)
2226 flex_array_free(fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002227
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002228out_put_task:
2229 put_task_struct(task);
2230out:
2231 return ret;
2232}
2233
2234static const struct file_operations proc_map_files_operations = {
2235 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002236 .iterate_shared = proc_map_files_readdir,
2237 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002238};
2239
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002240#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002241struct timers_private {
2242 struct pid *pid;
2243 struct task_struct *task;
2244 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002245 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002246 unsigned long flags;
2247};
2248
2249static void *timers_start(struct seq_file *m, loff_t *pos)
2250{
2251 struct timers_private *tp = m->private;
2252
2253 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2254 if (!tp->task)
2255 return ERR_PTR(-ESRCH);
2256
2257 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2258 if (!tp->sighand)
2259 return ERR_PTR(-ESRCH);
2260
2261 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2262}
2263
2264static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2265{
2266 struct timers_private *tp = m->private;
2267 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2268}
2269
2270static void timers_stop(struct seq_file *m, void *v)
2271{
2272 struct timers_private *tp = m->private;
2273
2274 if (tp->sighand) {
2275 unlock_task_sighand(tp->task, &tp->flags);
2276 tp->sighand = NULL;
2277 }
2278
2279 if (tp->task) {
2280 put_task_struct(tp->task);
2281 tp->task = NULL;
2282 }
2283}
2284
2285static int show_timer(struct seq_file *m, void *v)
2286{
2287 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002288 struct timers_private *tp = m->private;
2289 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002290 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002291 [SIGEV_SIGNAL] = "signal",
2292 [SIGEV_NONE] = "none",
2293 [SIGEV_THREAD] = "thread",
2294 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002295
2296 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002297 notify = timer->it_sigev_notify;
2298
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002299 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002300 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002301 timer->sigq->info.si_signo,
2302 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002303 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002304 nstr[notify & ~SIGEV_THREAD_ID],
2305 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2306 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002307 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002308
2309 return 0;
2310}
2311
2312static const struct seq_operations proc_timers_seq_ops = {
2313 .start = timers_start,
2314 .next = timers_next,
2315 .stop = timers_stop,
2316 .show = show_timer,
2317};
2318
2319static int proc_timers_open(struct inode *inode, struct file *file)
2320{
2321 struct timers_private *tp;
2322
2323 tp = __seq_open_private(file, &proc_timers_seq_ops,
2324 sizeof(struct timers_private));
2325 if (!tp)
2326 return -ENOMEM;
2327
2328 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002329 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002330 return 0;
2331}
2332
2333static const struct file_operations proc_timers_operations = {
2334 .open = proc_timers_open,
2335 .read = seq_read,
2336 .llseek = seq_lseek,
2337 .release = seq_release_private,
2338};
Eric Engestromb5946be2016-03-17 14:20:57 -07002339#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002340
John Stultz5de23d42016-03-17 14:20:54 -07002341static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2342 size_t count, loff_t *offset)
2343{
2344 struct inode *inode = file_inode(file);
2345 struct task_struct *p;
2346 u64 slack_ns;
2347 int err;
2348
2349 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2350 if (err < 0)
2351 return err;
2352
2353 p = get_proc_task(inode);
2354 if (!p)
2355 return -ESRCH;
2356
John Stultz4b2bd5f2016-10-07 17:02:33 -07002357 if (p != current) {
2358 if (!capable(CAP_SYS_NICE)) {
2359 count = -EPERM;
2360 goto out;
2361 }
John Stultz5de23d42016-03-17 14:20:54 -07002362
John Stultz4b2bd5f2016-10-07 17:02:33 -07002363 err = security_task_setscheduler(p);
2364 if (err) {
2365 count = err;
2366 goto out;
2367 }
John Stultz904763e2016-10-07 17:02:29 -07002368 }
2369
John Stultz7abbaf92016-10-07 17:02:26 -07002370 task_lock(p);
2371 if (slack_ns == 0)
2372 p->timer_slack_ns = p->default_timer_slack_ns;
2373 else
2374 p->timer_slack_ns = slack_ns;
2375 task_unlock(p);
2376
2377out:
John Stultz5de23d42016-03-17 14:20:54 -07002378 put_task_struct(p);
2379
2380 return count;
2381}
2382
2383static int timerslack_ns_show(struct seq_file *m, void *v)
2384{
2385 struct inode *inode = m->private;
2386 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002387 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002388
2389 p = get_proc_task(inode);
2390 if (!p)
2391 return -ESRCH;
2392
John Stultz4b2bd5f2016-10-07 17:02:33 -07002393 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002394
John Stultz4b2bd5f2016-10-07 17:02:33 -07002395 if (!capable(CAP_SYS_NICE)) {
2396 err = -EPERM;
2397 goto out;
2398 }
2399 err = security_task_getscheduler(p);
2400 if (err)
2401 goto out;
2402 }
John Stultz904763e2016-10-07 17:02:29 -07002403
John Stultz7abbaf92016-10-07 17:02:26 -07002404 task_lock(p);
2405 seq_printf(m, "%llu\n", p->timer_slack_ns);
2406 task_unlock(p);
2407
2408out:
John Stultz5de23d42016-03-17 14:20:54 -07002409 put_task_struct(p);
2410
2411 return err;
2412}
2413
2414static int timerslack_ns_open(struct inode *inode, struct file *filp)
2415{
2416 return single_open(filp, timerslack_ns_show, inode);
2417}
2418
2419static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2420 .open = timerslack_ns_open,
2421 .read = seq_read,
2422 .write = timerslack_ns_write,
2423 .llseek = seq_lseek,
2424 .release = single_release,
2425};
2426
Al Viro0168b9e2018-05-03 09:21:05 -04002427static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2428 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002429{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002430 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002431 struct inode *inode;
2432 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002433
Al Viro0168b9e2018-05-03 09:21:05 -04002434 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002435 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002436 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002437
2438 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002439 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002440 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002441 if (p->iop)
2442 inode->i_op = p->iop;
2443 if (p->fop)
2444 inode->i_fop = p->fop;
2445 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002446 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002447 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002448 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451static struct dentry *proc_pident_lookup(struct inode *dir,
2452 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002453 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002454 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002456 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002457 const struct pid_entry *p, *last;
Al Viro0168b9e2018-05-03 09:21:05 -04002458 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Eric W. Biederman99f89552006-06-26 00:25:55 -07002460 if (!task)
2461 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002463 /*
2464 * Yes, it does not scale. And it should not. Don't add
2465 * new entries into /proc/<tgid>/ without very good reasons.
2466 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002467 last = &ents[nents];
2468 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (p->len != dentry->d_name.len)
2470 continue;
2471 if (!memcmp(dentry->d_name.name, p->name, p->len))
2472 break;
2473 }
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002474 if (p >= last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 goto out;
2476
Al Viro0168b9e2018-05-03 09:21:05 -04002477 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002479 put_task_struct(task);
2480out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002481 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Al Virof0c3b502013-05-16 12:07:31 -04002484static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002485 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002486{
Al Virof0c3b502013-05-16 12:07:31 -04002487 struct task_struct *task = get_proc_task(file_inode(file));
2488 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002489
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002491 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002492
Al Virof0c3b502013-05-16 12:07:31 -04002493 if (!dir_emit_dots(file, ctx))
2494 goto out;
2495
2496 if (ctx->pos >= nents + 2)
2497 goto out;
2498
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002499 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002500 if (!proc_fill_cache(file, ctx, p->name, p->len,
2501 proc_pident_instantiate, task, p))
2502 break;
2503 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002504 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002506 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2512 size_t count, loff_t *ppos)
2513{
Al Viro496ad9a2013-01-23 17:07:38 -05002514 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002515 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002516 ssize_t length;
2517 struct task_struct *task = get_proc_task(inode);
2518
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002519 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002520 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002521
2522 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002523 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002524 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002525 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002526 if (length > 0)
2527 length = simple_read_from_buffer(buf, count, ppos, p, length);
2528 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002529 return length;
2530}
2531
2532static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2533 size_t count, loff_t *ppos)
2534{
Al Viro496ad9a2013-01-23 17:07:38 -05002535 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002536 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002537 ssize_t length;
2538 struct task_struct *task = get_proc_task(inode);
2539
2540 length = -ESRCH;
2541 if (!task)
2542 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002543
2544 /* A task may only write its own attributes. */
2545 length = -EACCES;
2546 if (current != task)
2547 goto out;
2548
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002549 if (count > PAGE_SIZE)
2550 count = PAGE_SIZE;
2551
2552 /* No partial writes. */
2553 length = -EINVAL;
2554 if (*ppos != 0)
2555 goto out;
2556
Al Virobb646cd2015-12-24 00:16:30 -05002557 page = memdup_user(buf, count);
2558 if (IS_ERR(page)) {
2559 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002560 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002561 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002562
David Howells107db7c2009-05-08 13:55:27 +01002563 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002564 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002565 if (length < 0)
2566 goto out_free;
2567
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002568 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002569 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002570 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002571out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002572 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002573out:
2574 put_task_struct(task);
2575out_no_task:
2576 return length;
2577}
2578
Arjan van de Ven00977a52007-02-12 00:55:34 -08002579static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002580 .read = proc_pid_attr_read,
2581 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002582 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583};
2584
Eric Dumazetc5141e62007-05-08 00:26:15 -07002585static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002586 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2587 REG("prev", S_IRUGO, proc_pid_attr_operations),
2588 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2589 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2590 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2591 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002592};
2593
Al Virof0c3b502013-05-16 12:07:31 -04002594static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Al Virof0c3b502013-05-16 12:07:31 -04002596 return proc_pident_readdir(file, ctx,
2597 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Arjan van de Ven00977a52007-02-12 00:55:34 -08002600static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002602 .iterate_shared = proc_attr_dir_readdir,
2603 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604};
2605
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002606static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002607 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002609 return proc_pident_lookup(dir, dentry,
2610 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002613static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002614 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002615 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002616 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617};
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619#endif
2620
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002621#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002622static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2623 size_t count, loff_t *ppos)
2624{
Al Viro496ad9a2013-01-23 17:07:38 -05002625 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002626 struct mm_struct *mm;
2627 char buffer[PROC_NUMBUF];
2628 size_t len;
2629 int ret;
2630
2631 if (!task)
2632 return -ESRCH;
2633
2634 ret = 0;
2635 mm = get_task_mm(task);
2636 if (mm) {
2637 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2638 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2639 MMF_DUMP_FILTER_SHIFT));
2640 mmput(mm);
2641 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2642 }
2643
2644 put_task_struct(task);
2645
2646 return ret;
2647}
2648
2649static ssize_t proc_coredump_filter_write(struct file *file,
2650 const char __user *buf,
2651 size_t count,
2652 loff_t *ppos)
2653{
2654 struct task_struct *task;
2655 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002656 unsigned int val;
2657 int ret;
2658 int i;
2659 unsigned long mask;
2660
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002661 ret = kstrtouint_from_user(buf, count, 0, &val);
2662 if (ret < 0)
2663 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002664
2665 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002666 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002667 if (!task)
2668 goto out_no_task;
2669
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002670 mm = get_task_mm(task);
2671 if (!mm)
2672 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002673 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002674
2675 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2676 if (val & mask)
2677 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2678 else
2679 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2680 }
2681
2682 mmput(mm);
2683 out_no_mm:
2684 put_task_struct(task);
2685 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002686 if (ret < 0)
2687 return ret;
2688 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002689}
2690
2691static const struct file_operations proc_coredump_filter_operations = {
2692 .read = proc_coredump_filter_read,
2693 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002694 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002695};
2696#endif
2697
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002698#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002699static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002700{
Andrea Righi940389b2008-07-28 00:48:12 +02002701 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002702 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002703 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002704
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002705 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2706 if (result)
2707 return result;
2708
Jann Horncaaee622016-01-20 15:00:04 -08002709 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002710 result = -EACCES;
2711 goto out_unlock;
2712 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002713
Andrea Righi59954772008-07-27 17:29:15 +02002714 if (whole && lock_task_sighand(task, &flags)) {
2715 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002716
Andrea Righi59954772008-07-27 17:29:15 +02002717 task_io_accounting_add(&acct, &task->signal->ioac);
2718 while_each_thread(task, t)
2719 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002720
Andrea Righi59954772008-07-27 17:29:15 +02002721 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002722 }
Joe Perches25ce3192015-04-15 16:18:17 -07002723 seq_printf(m,
2724 "rchar: %llu\n"
2725 "wchar: %llu\n"
2726 "syscr: %llu\n"
2727 "syscw: %llu\n"
2728 "read_bytes: %llu\n"
2729 "write_bytes: %llu\n"
2730 "cancelled_write_bytes: %llu\n",
2731 (unsigned long long)acct.rchar,
2732 (unsigned long long)acct.wchar,
2733 (unsigned long long)acct.syscr,
2734 (unsigned long long)acct.syscw,
2735 (unsigned long long)acct.read_bytes,
2736 (unsigned long long)acct.write_bytes,
2737 (unsigned long long)acct.cancelled_write_bytes);
2738 result = 0;
2739
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002740out_unlock:
2741 mutex_unlock(&task->signal->cred_guard_mutex);
2742 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002743}
Andrea Righi297c5d92008-07-25 01:48:49 -07002744
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002745static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2746 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002747{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002748 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002749}
2750
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002751static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2752 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002753{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002754 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002755}
2756#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002757
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002758#ifdef CONFIG_USER_NS
2759static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002760 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002761{
2762 struct user_namespace *ns = NULL;
2763 struct task_struct *task;
2764 struct seq_file *seq;
2765 int ret = -EINVAL;
2766
2767 task = get_proc_task(inode);
2768 if (task) {
2769 rcu_read_lock();
2770 ns = get_user_ns(task_cred_xxx(task, user_ns));
2771 rcu_read_unlock();
2772 put_task_struct(task);
2773 }
2774 if (!ns)
2775 goto err;
2776
2777 ret = seq_open(file, seq_ops);
2778 if (ret)
2779 goto err_put_ns;
2780
2781 seq = file->private_data;
2782 seq->private = ns;
2783
2784 return 0;
2785err_put_ns:
2786 put_user_ns(ns);
2787err:
2788 return ret;
2789}
2790
2791static int proc_id_map_release(struct inode *inode, struct file *file)
2792{
2793 struct seq_file *seq = file->private_data;
2794 struct user_namespace *ns = seq->private;
2795 put_user_ns(ns);
2796 return seq_release(inode, file);
2797}
2798
2799static int proc_uid_map_open(struct inode *inode, struct file *file)
2800{
2801 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2802}
2803
2804static int proc_gid_map_open(struct inode *inode, struct file *file)
2805{
2806 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2807}
2808
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002809static int proc_projid_map_open(struct inode *inode, struct file *file)
2810{
2811 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2812}
2813
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002814static const struct file_operations proc_uid_map_operations = {
2815 .open = proc_uid_map_open,
2816 .write = proc_uid_map_write,
2817 .read = seq_read,
2818 .llseek = seq_lseek,
2819 .release = proc_id_map_release,
2820};
2821
2822static const struct file_operations proc_gid_map_operations = {
2823 .open = proc_gid_map_open,
2824 .write = proc_gid_map_write,
2825 .read = seq_read,
2826 .llseek = seq_lseek,
2827 .release = proc_id_map_release,
2828};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002829
2830static const struct file_operations proc_projid_map_operations = {
2831 .open = proc_projid_map_open,
2832 .write = proc_projid_map_write,
2833 .read = seq_read,
2834 .llseek = seq_lseek,
2835 .release = proc_id_map_release,
2836};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002837
2838static int proc_setgroups_open(struct inode *inode, struct file *file)
2839{
2840 struct user_namespace *ns = NULL;
2841 struct task_struct *task;
2842 int ret;
2843
2844 ret = -ESRCH;
2845 task = get_proc_task(inode);
2846 if (task) {
2847 rcu_read_lock();
2848 ns = get_user_ns(task_cred_xxx(task, user_ns));
2849 rcu_read_unlock();
2850 put_task_struct(task);
2851 }
2852 if (!ns)
2853 goto err;
2854
2855 if (file->f_mode & FMODE_WRITE) {
2856 ret = -EACCES;
2857 if (!ns_capable(ns, CAP_SYS_ADMIN))
2858 goto err_put_ns;
2859 }
2860
2861 ret = single_open(file, &proc_setgroups_show, ns);
2862 if (ret)
2863 goto err_put_ns;
2864
2865 return 0;
2866err_put_ns:
2867 put_user_ns(ns);
2868err:
2869 return ret;
2870}
2871
2872static int proc_setgroups_release(struct inode *inode, struct file *file)
2873{
2874 struct seq_file *seq = file->private_data;
2875 struct user_namespace *ns = seq->private;
2876 int ret = single_release(inode, file);
2877 put_user_ns(ns);
2878 return ret;
2879}
2880
2881static const struct file_operations proc_setgroups_operations = {
2882 .open = proc_setgroups_open,
2883 .write = proc_setgroups_write,
2884 .read = seq_read,
2885 .llseek = seq_lseek,
2886 .release = proc_setgroups_release,
2887};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002888#endif /* CONFIG_USER_NS */
2889
Kees Cook47830722008-10-06 03:11:58 +04002890static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2891 struct pid *pid, struct task_struct *task)
2892{
Al Viroa9712bc2011-03-23 15:52:50 -04002893 int err = lock_trace(task);
2894 if (!err) {
2895 seq_printf(m, "%08x\n", task->personality);
2896 unlock_trace(task);
2897 }
2898 return err;
Kees Cook47830722008-10-06 03:11:58 +04002899}
2900
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002901#ifdef CONFIG_LIVEPATCH
2902static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2903 struct pid *pid, struct task_struct *task)
2904{
2905 seq_printf(m, "%d\n", task->patch_state);
2906 return 0;
2907}
2908#endif /* CONFIG_LIVEPATCH */
2909
Eric W. Biederman801199c2006-10-02 02:18:48 -07002910/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002911 * Thread groups
2912 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002913static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002914static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002915
Eric Dumazetc5141e62007-05-08 00:26:15 -07002916static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002917 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2918 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002919 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002920 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002921 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002922#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002924#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002925 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002926 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002927 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002928 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002929 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002930#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002931 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002932#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002933#ifdef CONFIG_SCHED_AUTOGROUP
2934 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2935#endif
john stultz4614a696b2009-12-14 18:00:05 -08002936 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002937#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002938 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002939#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002940 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002941 ONE("stat", S_IRUGO, proc_tgid_stat),
2942 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002943 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002945 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002946#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2948 LNK("cwd", proc_cwd_link),
2949 LNK("root", proc_root_link),
2950 LNK("exe", proc_exe_link),
2951 REG("mounts", S_IRUGO, proc_mounts_operations),
2952 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2953 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002954#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002955 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002956 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002957 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002958 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002959#endif
2960#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002961 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002962#endif
2963#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002964 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002965#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002966#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002967 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002968#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302969#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002970 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002971#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002972#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002973 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002974#endif
Paul Menage8793d852007-10-18 23:39:39 -07002975#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002976 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002977#endif
Paul Menagea4243162007-10-18 23:39:35 -07002978#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002979 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002980#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002981 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002982 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002983 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002984#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002985 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2986 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002987#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002988#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002989 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002990 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002991#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002992#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002993 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002994#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002995#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002996 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002997#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002998#ifdef CONFIG_USER_NS
2999 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3000 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003001 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003002 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003003#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003004#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003005 REG("timers", S_IRUGO, proc_timers_operations),
3006#endif
John Stultz5de23d42016-03-17 14:20:54 -07003007 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003008#ifdef CONFIG_LIVEPATCH
3009 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3010#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003011};
3012
Al Virof0c3b502013-05-16 12:07:31 -04003013static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003014{
Al Virof0c3b502013-05-16 12:07:31 -04003015 return proc_pident_readdir(file, ctx,
3016 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003017}
3018
Arjan van de Ven00977a52007-02-12 00:55:34 -08003019static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003020 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003021 .iterate_shared = proc_tgid_base_readdir,
3022 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003023};
3024
Al Viro00cd8dd2012-06-10 17:13:09 -04003025static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3026{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003027 return proc_pident_lookup(dir, dentry,
3028 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003029}
3030
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003031static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003032 .lookup = proc_tgid_base_lookup,
3033 .getattr = pid_getattr,
3034 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003035 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003036};
3037
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003038static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003040 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003041 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003042 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Eric W. Biederman48e64842006-06-26 00:25:48 -07003044 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003045 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003046 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003047 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003048 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003049 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003050 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003053 if (pid == tgid)
3054 return;
3055
Eric W. Biederman48e64842006-06-26 00:25:48 -07003056 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003057 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003058 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003059 if (!leader)
3060 goto out;
3061
3062 name.name = "task";
3063 name.len = strlen(name.name);
3064 dir = d_hash_and_lookup(leader, &name);
3065 if (!dir)
3066 goto out_put_leader;
3067
3068 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003069 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003070 dentry = d_hash_and_lookup(dir, &name);
3071 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003072 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003073 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003075
3076 dput(dir);
3077out_put_leader:
3078 dput(leader);
3079out:
3080 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
Randy Dunlap0895e912007-10-21 21:00:10 -07003083/**
3084 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3085 * @task: task that should be flushed.
3086 *
3087 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003088 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003089 * in. This call is supposed to do all of this job.
3090 *
3091 * Looks in the dcache for
3092 * /proc/@pid
3093 * /proc/@tgid/task/@pid
3094 * if either directory is present flushes it and all of it'ts children
3095 * from the dcache.
3096 *
3097 * It is safe and reasonable to cache /proc entries for a task until
3098 * that task exits. After that they just clog up the dcache with
3099 * useless entries, possibly causing useful dcache entries to be
3100 * flushed instead. This routine is proved to flush those useless
3101 * dcache entries at process exit time.
3102 *
3103 * NOTE: This routine is just an optimization so it does not guarantee
3104 * that no dcache entries will exist at process exit time it
3105 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003106 */
3107
3108void proc_flush_task(struct task_struct *task)
3109{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003110 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003111 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003112 struct upid *upid;
3113
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003114 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003115 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003116
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003117 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003118 upid = &pid->numbers[i];
3119 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003120 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003121 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003122}
3123
Al Viro0168b9e2018-05-03 09:21:05 -04003124static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003125 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003126{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003127 struct inode *inode;
3128
Al Viro0168b9e2018-05-03 09:21:05 -04003129 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003130 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003131 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003132
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003133 inode->i_op = &proc_tgid_base_inode_operations;
3134 inode->i_fop = &proc_tgid_base_operations;
3135 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003136
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003137 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003138 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003139
Nick Pigginfb045ad2011-01-07 17:49:55 +11003140 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003141 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003142}
3143
Al Viro00cd8dd2012-06-10 17:13:09 -04003144struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
3146 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003148 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003149 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003151 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (tgid == ~0U)
3153 goto out;
3154
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003155 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003156 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003157 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (task)
3159 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003160 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (!task)
3162 goto out;
3163
Al Viro0168b9e2018-05-03 09:21:05 -04003164 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003165 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166out:
Al Viro0168b9e2018-05-03 09:21:05 -04003167 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003171 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003172 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003174struct tgid_iter {
3175 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003176 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003177};
3178static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3179{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003180 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003182 if (iter.task)
3183 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003184 rcu_read_lock();
3185retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003186 iter.task = NULL;
3187 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003188 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003189 iter.tgid = pid_nr_ns(pid, ns);
3190 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003191 /* What we to know is if the pid we have find is the
3192 * pid of a thread_group_leader. Testing for task
3193 * being a thread_group_leader is the obvious thing
3194 * todo but there is a window when it fails, due to
3195 * the pid transfer logic in de_thread.
3196 *
3197 * So we perform the straight forward test of seeing
3198 * if the pid we have found is the pid of a thread
3199 * group leader, and don't worry if the task we have
3200 * found doesn't happen to be a thread group leader.
3201 * As we don't care in the case of readdir.
3202 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003203 if (!iter.task || !has_group_leader_pid(iter.task)) {
3204 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003205 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003206 }
3207 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003209 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003210 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
Eric W. Biederman00978752014-07-31 03:10:50 -07003213#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003216int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003218 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003219 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003220 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Al Viro021ada72013-03-29 19:27:05 -04003222 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Eric W. Biederman00978752014-07-31 03:10:50 -07003225 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003226 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003227 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003228 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003229 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003230 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003231 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003232 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003233 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3234 return 0;
3235 ctx->pos = pos = pos + 1;
3236 }
3237 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003238 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003239 for (iter = next_tgid(ns, iter);
3240 iter.task;
3241 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003242 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003243 int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003244
3245 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003246 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003247 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003248
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003249 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003250 ctx->pos = iter.tgid + TGID_OFFSET;
3251 if (!proc_fill_cache(file, ctx, name, len,
3252 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003253 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 }
Al Virof0c3b502013-05-16 12:07:31 -04003257 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return 0;
3259}
3260
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003261/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003262 * proc_tid_comm_permission is a special permission function exclusively
3263 * used for the node /proc/<pid>/task/<tid>/comm.
3264 * It bypasses generic permission checks in the case where a task of the same
3265 * task group attempts to access the node.
3266 * The rationale behind this is that glibc and bionic access this node for
3267 * cross thread naming (pthread_set/getname_np(!self)). However, if
3268 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3269 * which locks out the cross thread naming implementation.
3270 * This function makes sure that the node is always accessible for members of
3271 * same thread group.
3272 */
3273static int proc_tid_comm_permission(struct inode *inode, int mask)
3274{
3275 bool is_same_tgroup;
3276 struct task_struct *task;
3277
3278 task = get_proc_task(inode);
3279 if (!task)
3280 return -ESRCH;
3281 is_same_tgroup = same_thread_group(current, task);
3282 put_task_struct(task);
3283
3284 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3285 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3286 * read or written by the members of the corresponding
3287 * thread group.
3288 */
3289 return 0;
3290 }
3291
3292 return generic_permission(inode, mask);
3293}
3294
3295static const struct inode_operations proc_tid_comm_inode_operations = {
3296 .permission = proc_tid_comm_permission,
3297};
3298
3299/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003300 * Tasks
3301 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003302static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003303 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003304 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003305 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003306#ifdef CONFIG_NET
3307 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3308#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003309 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003310 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003311 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003312 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003313 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003314#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003315 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003316#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003317 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3318 &proc_tid_comm_inode_operations,
3319 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003320#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003321 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003322#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003323 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003324 ONE("stat", S_IRUGO, proc_tid_stat),
3325 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003326 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003327#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003328 REG("children", S_IRUGO, proc_tid_children_operations),
3329#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003330#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003331 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003332#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003333 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3334 LNK("cwd", proc_cwd_link),
3335 LNK("root", proc_root_link),
3336 LNK("exe", proc_exe_link),
3337 REG("mounts", S_IRUGO, proc_mounts_operations),
3338 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003339#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003340 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003341 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003342 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003343 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003344#endif
3345#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003346 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003347#endif
3348#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003349 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003350#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003351#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003352 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003353#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303354#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003355 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003356#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003357#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003358 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003359#endif
Paul Menage8793d852007-10-18 23:39:39 -07003360#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003361 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003362#endif
Paul Menagea4243162007-10-18 23:39:35 -07003363#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003364 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003365#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003366 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003367 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003368 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003369#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003370 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003371 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003372#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003373#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003374 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003375 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003376#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003377#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003378 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003379#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003380#ifdef CONFIG_USER_NS
3381 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3382 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003383 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003384 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003385#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003386#ifdef CONFIG_LIVEPATCH
3387 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3388#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003389};
3390
Al Virof0c3b502013-05-16 12:07:31 -04003391static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003392{
Al Virof0c3b502013-05-16 12:07:31 -04003393 return proc_pident_readdir(file, ctx,
3394 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003395}
3396
Al Viro00cd8dd2012-06-10 17:13:09 -04003397static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3398{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003399 return proc_pident_lookup(dir, dentry,
3400 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003401}
3402
Arjan van de Ven00977a52007-02-12 00:55:34 -08003403static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003404 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003405 .iterate_shared = proc_tid_base_readdir,
3406 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003407};
3408
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003409static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003410 .lookup = proc_tid_base_lookup,
3411 .getattr = pid_getattr,
3412 .setattr = proc_setattr,
3413};
3414
Al Viro0168b9e2018-05-03 09:21:05 -04003415static struct dentry *proc_task_instantiate(struct dentry *dentry,
3416 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003417{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003418 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003419 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003420 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003421 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003422
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003423 inode->i_op = &proc_tid_base_inode_operations;
3424 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003425 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003426
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003427 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003428 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003429
Nick Pigginfb045ad2011-01-07 17:49:55 +11003430 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003431 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003432}
3433
Al Viro00cd8dd2012-06-10 17:13:09 -04003434static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003435{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436 struct task_struct *task;
3437 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003438 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003439 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003440 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003441
3442 if (!leader)
3443 goto out_no_task;
3444
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003445 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003446 if (tid == ~0U)
3447 goto out;
3448
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003449 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003450 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003451 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003452 if (task)
3453 get_task_struct(task);
3454 rcu_read_unlock();
3455 if (!task)
3456 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003457 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003458 goto out_drop_task;
3459
Al Viro0168b9e2018-05-03 09:21:05 -04003460 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003461out_drop_task:
3462 put_task_struct(task);
3463out:
3464 put_task_struct(leader);
3465out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003466 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003467}
3468
3469/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003470 * Find the first tid of a thread group to return to user space.
3471 *
3472 * Usually this is just the thread group leader, but if the users
3473 * buffer was too small or there was a seek into the middle of the
3474 * directory we have more work todo.
3475 *
3476 * In the case of a short read we start with find_task_by_pid.
3477 *
3478 * In the case of a seek we start with the leader and walk nr
3479 * threads past it.
3480 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003481static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3482 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003483{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003484 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003485 unsigned long nr = f_pos;
3486
3487 if (nr != f_pos) /* 32bit overflow? */
3488 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003489
Eric W. Biedermancc288732006-06-26 00:26:01 -07003490 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003491 task = pid_task(pid, PIDTYPE_PID);
3492 if (!task)
3493 goto fail;
3494
3495 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003496 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003497 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003498 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003499 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003500 }
3501
3502 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003503 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003504 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003505
3506 /* If we haven't found our starting place yet start
3507 * with the leader and walk nr threads forward.
3508 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003509 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003510 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003511 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003512 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003513 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003514fail:
3515 pos = NULL;
3516 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003517found:
3518 get_task_struct(pos);
3519out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003520 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003521 return pos;
3522}
3523
3524/*
3525 * Find the next thread in the thread list.
3526 * Return NULL if there is an error or no next thread.
3527 *
3528 * The reference to the input task_struct is released.
3529 */
3530static struct task_struct *next_tid(struct task_struct *start)
3531{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003532 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003533 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003534 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003535 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003536 if (thread_group_leader(pos))
3537 pos = NULL;
3538 else
3539 get_task_struct(pos);
3540 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003541 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003542 put_task_struct(start);
3543 return pos;
3544}
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003547static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003549 struct inode *inode = file_inode(file);
3550 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003551 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003552 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003554 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003555 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Al Virof0c3b502013-05-16 12:07:31 -04003557 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003560 /* f_version caches the tgid value that the last readdir call couldn't
3561 * return. lseek aka telldir automagically resets f_version to 0.
3562 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003563 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003564 tid = (int)file->f_version;
3565 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003566 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003567 task;
Al Virof0c3b502013-05-16 12:07:31 -04003568 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003569 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003570 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003571 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003572 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003573 if (!proc_fill_cache(file, ctx, name, len,
3574 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003575 /* returning this tgid failed, save it as the first
3576 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003577 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003578 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003582
Al Virof0c3b502013-05-16 12:07:31 -04003583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003585
David Howellsa528d352017-01-31 16:46:22 +00003586static int proc_task_getattr(const struct path *path, struct kstat *stat,
3587 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003588{
David Howellsa528d352017-01-31 16:46:22 +00003589 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003590 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003591 generic_fillattr(inode, stat);
3592
Eric W. Biederman99f89552006-06-26 00:25:55 -07003593 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003594 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003595 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003596 }
3597
3598 return 0;
3599}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003600
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003601static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003602 .lookup = proc_task_lookup,
3603 .getattr = proc_task_getattr,
3604 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003605 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003606};
3607
Arjan van de Ven00977a52007-02-12 00:55:34 -08003608static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003609 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003610 .iterate_shared = proc_task_readdir,
3611 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003612};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003613
3614void __init set_proc_pid_nlink(void)
3615{
3616 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3617 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3618}