blob: 31934cb9dfc8864fd0b2ac6f1e77f8a6b55c99f7 [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>
Paul Menagea4243162007-10-18 23:39:35 -070078#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/cpuset.h>
80#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050081#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070082#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070083#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070084#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070085#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080086#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040087#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090088#include <linux/slab.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010089#include <linux/sched/autogroup.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010090#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010091#include <linux/sched/coredump.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010092#include <linux/sched/debug.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010093#include <linux/sched/stat.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080094#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040095#include <linux/posix-timers.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040096#ifdef CONFIG_HARDWALL
97#include <asm/hardwall.h>
98#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080099#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400101#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
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 Dobriyan1270dd82016-12-12 16:45:32 -0800113static u8 nlink_tid;
114static u8 nlink_tgid;
115
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
242 down_read(&mm->mmap_sem);
243 arg_start = mm->arg_start;
244 arg_end = mm->arg_end;
245 env_start = mm->env_start;
246 env_end = mm->env_end;
247 up_read(&mm->mmap_sem);
248
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 */
Linus Torvalds272ddc82016-10-24 19:00:44 -0700264 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700265 if (rv <= 0)
266 goto out_free_page;
267
268 rv = 0;
269
270 if (c == '\0') {
271 /* Command line (set of strings) occupies whole ARGV. */
272 if (len1 <= *pos)
273 goto out_free_page;
274
275 p = arg_start + *pos;
276 len = len1 - *pos;
277 while (count > 0 && len > 0) {
278 unsigned int _count;
279 int nr_read;
280
281 _count = min3(count, len, PAGE_SIZE);
Linus Torvalds272ddc82016-10-24 19:00:44 -0700282 nr_read = access_remote_vm(mm, p, page, _count, 0);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700283 if (nr_read < 0)
284 rv = nr_read;
285 if (nr_read <= 0)
286 goto out_free_page;
287
288 if (copy_to_user(buf, page, nr_read)) {
289 rv = -EFAULT;
290 goto out_free_page;
291 }
292
293 p += nr_read;
294 len -= nr_read;
295 buf += nr_read;
296 count -= nr_read;
297 rv += nr_read;
298 }
299 } else {
300 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700301 * Command line (1 string) occupies ARGV and
302 * extends into ENVP.
303 */
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800304 struct {
305 unsigned long p;
306 unsigned long len;
307 } cmdline[2] = {
308 { .p = arg_start, .len = len1 },
309 { .p = env_start, .len = len2 },
310 };
311 loff_t pos1 = *pos;
312 unsigned int i;
313
314 i = 0;
315 while (i < 2 && pos1 >= cmdline[i].len) {
316 pos1 -= cmdline[i].len;
317 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700318 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800319 while (i < 2) {
320 p = cmdline[i].p + pos1;
321 len = cmdline[i].len - pos1;
322 while (count > 0 && len > 0) {
323 unsigned int _count, l;
324 int nr_read;
325 bool final;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700326
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800327 _count = min3(count, len, PAGE_SIZE);
328 nr_read = access_remote_vm(mm, p, page, _count, 0);
329 if (nr_read < 0)
330 rv = nr_read;
331 if (nr_read <= 0)
332 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700333
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800334 /*
335 * Command line can be shorter than whole ARGV
336 * even if last "marker" byte says it is not.
337 */
338 final = false;
339 l = strnlen(page, nr_read);
340 if (l < nr_read) {
341 nr_read = l;
342 final = true;
343 }
344
345 if (copy_to_user(buf, page, nr_read)) {
346 rv = -EFAULT;
347 goto out_free_page;
348 }
349
350 p += nr_read;
351 len -= nr_read;
352 buf += nr_read;
353 count -= nr_read;
354 rv += nr_read;
355
356 if (final)
357 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700358 }
359
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800360 /* Only first chunk can be read partially. */
361 pos1 = 0;
362 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700363 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700364 }
365
366out_free_page:
367 free_page((unsigned long)page);
368out_mmput:
369 mmput(mm);
370 if (rv > 0)
371 *pos += rv;
372 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700375static const struct file_operations proc_pid_cmdline_ops = {
376 .read = proc_pid_cmdline_read,
377 .llseek = generic_file_llseek,
378};
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#ifdef CONFIG_KALLSYMS
381/*
382 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
383 * Returns the resolved symbol. If that fails, simply return the address.
384 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700385static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
386 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700388 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700389 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 wchan = get_wchan(task);
392
Jann Horncaaee622016-01-20 15:00:04 -0800393 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
394 && !lookup_symbol_name(wchan, symname))
Joe Perches25ce3192015-04-15 16:18:17 -0700395 seq_printf(m, "%s", symname);
Ingo Molnarb2f73922015-09-30 15:59:17 +0200396 else
Robin Humble1e92a612016-05-09 16:28:46 -0700397 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700398
399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401#endif /* CONFIG_KALLSYMS */
402
Al Viroa9712bc2011-03-23 15:52:50 -0400403static int lock_trace(struct task_struct *task)
404{
405 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
406 if (err)
407 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800408 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400409 mutex_unlock(&task->signal->cred_guard_mutex);
410 return -EPERM;
411 }
412 return 0;
413}
414
415static void unlock_trace(struct task_struct *task)
416{
417 mutex_unlock(&task->signal->cred_guard_mutex);
418}
419
Ken Chen2ec220e2008-11-10 11:26:08 +0300420#ifdef CONFIG_STACKTRACE
421
422#define MAX_STACK_TRACE_DEPTH 64
423
424static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
425 struct pid *pid, struct task_struct *task)
426{
427 struct stack_trace trace;
428 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400429 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300430 int i;
431
432 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
433 if (!entries)
434 return -ENOMEM;
435
436 trace.nr_entries = 0;
437 trace.max_entries = MAX_STACK_TRACE_DEPTH;
438 trace.entries = entries;
439 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300440
Al Viroa9712bc2011-03-23 15:52:50 -0400441 err = lock_trace(task);
442 if (!err) {
443 save_stack_trace_tsk(task, &trace);
444
445 for (i = 0; i < trace.nr_entries; i++) {
Josh Poimboeuf8b927d72016-08-18 10:59:07 -0500446 seq_printf(m, "[<%pK>] %pB\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400447 (void *)entries[i], (void *)entries[i]);
448 }
449 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300450 }
451 kfree(entries);
452
Al Viroa9712bc2011-03-23 15:52:50 -0400453 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300454}
455#endif
456
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530457#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * Provides /proc/PID/schedstat
460 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700461static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
462 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530464 if (unlikely(!sched_info_on()))
465 seq_printf(m, "0 0 0\n");
466 else
467 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700468 (unsigned long long)task->se.sum_exec_runtime,
469 (unsigned long long)task->sched_info.run_delay,
470 task->sched_info.pcount);
471
472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474#endif
475
Arjan van de Ven97455122008-01-25 21:08:34 +0100476#ifdef CONFIG_LATENCYTOP
477static int lstats_show_proc(struct seq_file *m, void *v)
478{
479 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800480 struct inode *inode = m->private;
481 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100482
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800483 if (!task)
484 return -ESRCH;
485 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100486 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800487 struct latency_record *lr = &task->latency_record[i];
488 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100489 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800490 seq_printf(m, "%i %li %li",
491 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100492 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800493 unsigned long bt = lr->backtrace[q];
494 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100495 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800496 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100497 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800498 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100499 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800500 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100501 }
502
503 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800504 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100505 return 0;
506}
507
508static int lstats_open(struct inode *inode, struct file *file)
509{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800510 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800511}
512
Arjan van de Ven97455122008-01-25 21:08:34 +0100513static ssize_t lstats_write(struct file *file, const char __user *buf,
514 size_t count, loff_t *offs)
515{
Al Viro496ad9a2013-01-23 17:07:38 -0500516 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100517
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800518 if (!task)
519 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100520 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800521 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100522
523 return count;
524}
525
526static const struct file_operations proc_lstats_operations = {
527 .open = lstats_open,
528 .read = seq_read,
529 .write = lstats_write,
530 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800531 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100532};
533
534#endif
535
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700536static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
537 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
David Rientjesa7f638f2012-05-29 15:06:47 -0700539 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200540 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Oleg Nesterovef419392016-08-02 14:03:19 -0700542 points = oom_badness(task, NULL, NULL, totalpages) *
543 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700544 seq_printf(m, "%lu\n", points);
545
546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Neil Hormand85f50d2007-10-18 23:40:37 -0700549struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700550 const char *name;
551 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700552};
553
554static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700555 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700556 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
557 [RLIMIT_DATA] = {"Max data size", "bytes"},
558 [RLIMIT_STACK] = {"Max stack size", "bytes"},
559 [RLIMIT_CORE] = {"Max core file size", "bytes"},
560 [RLIMIT_RSS] = {"Max resident set", "bytes"},
561 [RLIMIT_NPROC] = {"Max processes", "processes"},
562 [RLIMIT_NOFILE] = {"Max open files", "files"},
563 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
564 [RLIMIT_AS] = {"Max address space", "bytes"},
565 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
566 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
567 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
568 [RLIMIT_NICE] = {"Max nice priority", NULL},
569 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800570 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700571};
572
573/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700574static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
575 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700576{
577 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700578 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700579
580 struct rlimit rlim[RLIM_NLIMITS];
581
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400582 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700583 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700584 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
585 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700586
587 /*
588 * print the file header
589 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700590 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700591 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700592
593 for (i = 0; i < RLIM_NLIMITS; i++) {
594 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700595 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700596 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700597 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700598 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700599 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700600
601 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700602 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700603 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700604 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700605
606 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700607 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700608 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700609 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700610 }
611
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700612 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700613}
614
Roland McGrathebcb6732008-07-25 19:46:00 -0700615#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700616static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
617 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700618{
619 long nr;
620 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700621 int res;
622
623 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400624 if (res)
625 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700626
627 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700628 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400629 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700630 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400631 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700632 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700633 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
634 nr,
635 args[0], args[1], args[2], args[3], args[4], args[5],
636 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400637 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700638
639 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700640}
641#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/************************************************************************/
644/* Here the fs part begins */
645/************************************************************************/
646
647/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700648static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700650 struct task_struct *task;
651 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700652 /* Allow access to a task's file descriptors if it is us or we
653 * may use ptrace attach to the process and find out that
654 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700655 */
656 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700657 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800658 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700659 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700660 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700661 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800664int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700665{
666 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000667 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700668
669 if (attr->ia_valid & ATTR_MODE)
670 return -EPERM;
671
Jan Kara31051c82016-05-26 16:55:18 +0200672 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200673 if (error)
674 return error;
675
Christoph Hellwig10257742010-06-04 11:30:02 +0200676 setattr_copy(inode, attr);
677 mark_inode_dirty(inode);
678 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700679}
680
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800681/*
682 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
683 * or euid/egid (for hide_pid_min=2)?
684 */
685static bool has_pid_permissions(struct pid_namespace *pid,
686 struct task_struct *task,
687 int hide_pid_min)
688{
689 if (pid->hide_pid < hide_pid_min)
690 return true;
691 if (in_group_p(pid->pid_gid))
692 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800693 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800694}
695
696
697static int proc_pid_permission(struct inode *inode, int mask)
698{
699 struct pid_namespace *pid = inode->i_sb->s_fs_info;
700 struct task_struct *task;
701 bool has_perms;
702
703 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800704 if (!task)
705 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800706 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800707 put_task_struct(task);
708
709 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800710 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800711 /*
712 * Let's make getdents(), stat(), and open()
713 * consistent with each other. If a process
714 * may not stat() a file, it shouldn't be seen
715 * in procfs at all.
716 */
717 return -ENOENT;
718 }
719
720 return -EPERM;
721 }
722 return generic_permission(inode, mask);
723}
724
725
726
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800727static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700728 .setattr = proc_setattr,
729};
730
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800731static int proc_single_show(struct seq_file *m, void *v)
732{
733 struct inode *inode = m->private;
734 struct pid_namespace *ns;
735 struct pid *pid;
736 struct task_struct *task;
737 int ret;
738
739 ns = inode->i_sb->s_fs_info;
740 pid = proc_pid(inode);
741 task = get_pid_task(pid, PIDTYPE_PID);
742 if (!task)
743 return -ESRCH;
744
745 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
746
747 put_task_struct(task);
748 return ret;
749}
750
751static int proc_single_open(struct inode *inode, struct file *filp)
752{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800753 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800754}
755
756static const struct file_operations proc_single_file_operations = {
757 .open = proc_single_open,
758 .read = seq_read,
759 .llseek = seq_lseek,
760 .release = single_release,
761};
762
Oleg Nesterov5381e162014-10-09 15:25:24 -0700763
764struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
765{
766 struct task_struct *task = get_proc_task(inode);
767 struct mm_struct *mm = ERR_PTR(-ESRCH);
768
769 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800770 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700771 put_task_struct(task);
772
773 if (!IS_ERR_OR_NULL(mm)) {
774 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800775 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700776 /* but do not pin its memory */
777 mmput(mm);
778 }
779 }
780
781 return mm;
782}
783
Cong Wangb409e572012-05-31 16:26:17 -0700784static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700786 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800787
788 if (IS_ERR(mm))
789 return PTR_ERR(mm);
790
Linus Torvaldse2683372012-01-17 15:21:19 -0800791 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
793}
794
Cong Wangb409e572012-05-31 16:26:17 -0700795static int mem_open(struct inode *inode, struct file *file)
796{
Djalal Harounibc452b42012-07-30 14:42:28 -0700797 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
798
799 /* OK to pass negative loff_t, we can catch out-of-range */
800 file->f_mode |= FMODE_UNSIGNED_OFFSET;
801
802 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700803}
804
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100805static ssize_t mem_rw(struct file *file, char __user *buf,
806 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Linus Torvaldse2683372012-01-17 15:21:19 -0800808 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100809 unsigned long addr = *ppos;
810 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700812 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvaldse2683372012-01-17 15:21:19 -0800814 if (!mm)
815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Michal Hocko0ee931c2017-09-13 16:28:29 -0700817 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800819 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700821 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800822 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100823 goto free;
824
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700825 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100828 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100830 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 copied = -EFAULT;
832 break;
833 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100834
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100835 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100836 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (!copied)
838 copied = -EIO;
839 break;
840 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100841
842 if (!write && copy_to_user(buf, page, this_len)) {
843 copied = -EFAULT;
844 break;
845 }
846
847 buf += this_len;
848 addr += this_len;
849 copied += this_len;
850 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100852 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700853
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100854 mmput(mm);
855free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700856 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return copied;
858}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100860static ssize_t mem_read(struct file *file, char __user *buf,
861 size_t count, loff_t *ppos)
862{
863 return mem_rw(file, buf, count, ppos, 0);
864}
865
866static ssize_t mem_write(struct file *file, const char __user *buf,
867 size_t count, loff_t *ppos)
868{
869 return mem_rw(file, (char __user*)buf, count, ppos, 1);
870}
871
Matt Mackall85863e42008-02-04 22:29:04 -0800872loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 switch (orig) {
875 case 0:
876 file->f_pos = offset;
877 break;
878 case 1:
879 file->f_pos += offset;
880 break;
881 default:
882 return -EINVAL;
883 }
884 force_successful_syscall_return();
885 return file->f_pos;
886}
887
Linus Torvaldse2683372012-01-17 15:21:19 -0800888static int mem_release(struct inode *inode, struct file *file)
889{
890 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100891 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100892 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800893 return 0;
894}
895
Arjan van de Ven00977a52007-02-12 00:55:34 -0800896static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 .llseek = mem_lseek,
898 .read = mem_read,
899 .write = mem_write,
900 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800901 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902};
903
Cong Wangb409e572012-05-31 16:26:17 -0700904static int environ_open(struct inode *inode, struct file *file)
905{
906 return __mem_open(inode, file, PTRACE_MODE_READ);
907}
908
James Pearson315e28c2007-10-16 23:30:17 -0700909static ssize_t environ_read(struct file *file, char __user *buf,
910 size_t count, loff_t *ppos)
911{
James Pearson315e28c2007-10-16 23:30:17 -0700912 char *page;
913 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700914 int ret = 0;
915 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800916 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700917
Mathias Krause8148a732016-05-05 16:22:26 -0700918 /* Ensure the process spawned far enough to have an environment. */
919 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700920 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700921
Michal Hocko0ee931c2017-09-13 16:28:29 -0700922 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700923 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700924 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700925
Al Virod6f64b82011-02-15 22:26:01 -0500926 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800927 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700928 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800929
930 down_read(&mm->mmap_sem);
931 env_start = mm->env_start;
932 env_end = mm->env_end;
933 up_read(&mm->mmap_sem);
934
James Pearson315e28c2007-10-16 23:30:17 -0700935 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700936 size_t this_len, max_len;
937 int retval;
938
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800939 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700940 break;
James Pearson315e28c2007-10-16 23:30:17 -0700941
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800942 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700943
Djalal Harounie8905ec2012-07-30 14:42:26 -0700944 max_len = min_t(size_t, PAGE_SIZE, count);
945 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700946
Linus Torvalds272ddc82016-10-24 19:00:44 -0700947 retval = access_remote_vm(mm, (env_start + src), page, this_len, 0);
James Pearson315e28c2007-10-16 23:30:17 -0700948
949 if (retval <= 0) {
950 ret = retval;
951 break;
952 }
953
954 if (copy_to_user(buf, page, retval)) {
955 ret = -EFAULT;
956 break;
957 }
958
959 ret += retval;
960 src += retval;
961 buf += retval;
962 count -= retval;
963 }
964 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700965 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700966
967free:
James Pearson315e28c2007-10-16 23:30:17 -0700968 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700969 return ret;
970}
971
972static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700973 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700974 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100975 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700976 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700977};
978
Al Viroc5317162016-10-05 18:43:43 -0400979static int auxv_open(struct inode *inode, struct file *file)
980{
981 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
982}
983
984static ssize_t auxv_read(struct file *file, char __user *buf,
985 size_t count, loff_t *ppos)
986{
987 struct mm_struct *mm = file->private_data;
988 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700989
990 if (!mm)
991 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400992 do {
993 nwords += 2;
994 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
995 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
996 nwords * sizeof(mm->saved_auxv[0]));
997}
998
999static const struct file_operations proc_auxv_operations = {
1000 .open = auxv_open,
1001 .read = auxv_read,
1002 .llseek = generic_file_llseek,
1003 .release = mem_release,
1004};
1005
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001006static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1007 loff_t *ppos)
1008{
Al Viro496ad9a2013-01-23 17:07:38 -05001009 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001010 char buffer[PROC_NUMBUF];
1011 int oom_adj = OOM_ADJUST_MIN;
1012 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001013
1014 if (!task)
1015 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001016 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1017 oom_adj = OOM_ADJUST_MAX;
1018 else
1019 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1020 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001021 put_task_struct(task);
1022 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1023 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1024}
1025
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001026static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1027{
1028 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001029 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001030 struct task_struct *task;
1031 int err = 0;
1032
1033 task = get_proc_task(file_inode(file));
1034 if (!task)
1035 return -ESRCH;
1036
1037 mutex_lock(&oom_adj_mutex);
1038 if (legacy) {
1039 if (oom_adj < task->signal->oom_score_adj &&
1040 !capable(CAP_SYS_RESOURCE)) {
1041 err = -EACCES;
1042 goto err_unlock;
1043 }
1044 /*
1045 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1046 * /proc/pid/oom_score_adj instead.
1047 */
1048 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1049 current->comm, task_pid_nr(current), task_pid_nr(task),
1050 task_pid_nr(task));
1051 } else {
1052 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1053 !capable(CAP_SYS_RESOURCE)) {
1054 err = -EACCES;
1055 goto err_unlock;
1056 }
1057 }
1058
Michal Hocko44a70ade2016-07-28 15:44:43 -07001059 /*
1060 * Make sure we will check other processes sharing the mm if this is
1061 * not vfrok which wants its own oom_score_adj.
1062 * pin the mm so it doesn't go away and get reused after task_unlock
1063 */
1064 if (!task->vfork_done) {
1065 struct task_struct *p = find_lock_task_mm(task);
1066
1067 if (p) {
1068 if (atomic_read(&p->mm->mm_users) > 1) {
1069 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001070 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001071 }
1072 task_unlock(p);
1073 }
1074 }
1075
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001076 task->signal->oom_score_adj = oom_adj;
1077 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1078 task->signal->oom_score_adj_min = (short)oom_adj;
1079 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001080
1081 if (mm) {
1082 struct task_struct *p;
1083
1084 rcu_read_lock();
1085 for_each_process(p) {
1086 if (same_thread_group(task, p))
1087 continue;
1088
1089 /* do not touch kernel threads or the global init */
1090 if (p->flags & PF_KTHREAD || is_global_init(p))
1091 continue;
1092
1093 task_lock(p);
1094 if (!p->vfork_done && process_shares_mm(p, mm)) {
1095 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",
1096 task_pid_nr(p), p->comm,
1097 p->signal->oom_score_adj, oom_adj,
1098 task_pid_nr(task), task->comm);
1099 p->signal->oom_score_adj = oom_adj;
1100 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1101 p->signal->oom_score_adj_min = (short)oom_adj;
1102 }
1103 task_unlock(p);
1104 }
1105 rcu_read_unlock();
1106 mmdrop(mm);
1107 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001108err_unlock:
1109 mutex_unlock(&oom_adj_mutex);
1110 put_task_struct(task);
1111 return err;
1112}
Michal Hockof913da52016-07-28 15:44:37 -07001113
David Rientjesb72bdfa2015-11-05 18:50:32 -08001114/*
1115 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1116 * kernels. The effective policy is defined by oom_score_adj, which has a
1117 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1118 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1119 * Processes that become oom disabled via oom_adj will still be oom disabled
1120 * with this implementation.
1121 *
1122 * oom_adj cannot be removed since existing userspace binaries use it.
1123 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001124static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1125 size_t count, loff_t *ppos)
1126{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001127 char buffer[PROC_NUMBUF];
1128 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001129 int err;
1130
1131 memset(buffer, 0, sizeof(buffer));
1132 if (count > sizeof(buffer) - 1)
1133 count = sizeof(buffer) - 1;
1134 if (copy_from_user(buffer, buf, count)) {
1135 err = -EFAULT;
1136 goto out;
1137 }
1138
1139 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1140 if (err)
1141 goto out;
1142 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1143 oom_adj != OOM_DISABLE) {
1144 err = -EINVAL;
1145 goto out;
1146 }
1147
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001148 /*
1149 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1150 * value is always attainable.
1151 */
1152 if (oom_adj == OOM_ADJUST_MAX)
1153 oom_adj = OOM_SCORE_ADJ_MAX;
1154 else
1155 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1156
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001157 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001158out:
1159 return err < 0 ? err : count;
1160}
1161
1162static const struct file_operations proc_oom_adj_operations = {
1163 .read = oom_adj_read,
1164 .write = oom_adj_write,
1165 .llseek = generic_file_llseek,
1166};
1167
David Rientjesa63d83f2010-08-09 17:19:46 -07001168static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1169 size_t count, loff_t *ppos)
1170{
Al Viro496ad9a2013-01-23 17:07:38 -05001171 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001172 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001173 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001174 size_t len;
1175
1176 if (!task)
1177 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001178 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001179 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001180 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001181 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1182}
1183
1184static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1185 size_t count, loff_t *ppos)
1186{
David Rientjesa63d83f2010-08-09 17:19:46 -07001187 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001188 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001189 int err;
1190
1191 memset(buffer, 0, sizeof(buffer));
1192 if (count > sizeof(buffer) - 1)
1193 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001194 if (copy_from_user(buffer, buf, count)) {
1195 err = -EFAULT;
1196 goto out;
1197 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001198
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001199 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001200 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001201 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001202 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001203 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1204 err = -EINVAL;
1205 goto out;
1206 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001207
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001208 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001209out:
1210 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001211}
1212
1213static const struct file_operations proc_oom_score_adj_operations = {
1214 .read = oom_score_adj_read,
1215 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001216 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001217};
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001220#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1222 size_t count, loff_t *ppos)
1223{
Al Viro496ad9a2013-01-23 17:07:38 -05001224 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001225 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 ssize_t length;
1227 char tmpbuf[TMPBUFLEN];
1228
Eric W. Biederman99f89552006-06-26 00:25:55 -07001229 if (!task)
1230 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001232 from_kuid(file->f_cred->user_ns,
1233 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001234 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1236}
1237
1238static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1239 size_t count, loff_t *ppos)
1240{
Al Viro496ad9a2013-01-23 17:07:38 -05001241 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001243 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001244 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001246 rcu_read_lock();
1247 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1248 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001250 }
1251 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (*ppos != 0) {
1254 /* No partial writes. */
1255 return -EINVAL;
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001258 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1259 if (rv < 0)
1260 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001261
1262 /* is userspace tring to explicitly UNSET the loginuid? */
1263 if (loginuid == AUDIT_UID_UNSET) {
1264 kloginuid = INVALID_UID;
1265 } else {
1266 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001267 if (!uid_valid(kloginuid))
1268 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001269 }
1270
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001271 rv = audit_set_loginuid(kloginuid);
1272 if (rv < 0)
1273 return rv;
1274 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Arjan van de Ven00977a52007-02-12 00:55:34 -08001277static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 .read = proc_loginuid_read,
1279 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001280 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281};
Eric Paris1e0bd752008-03-13 08:15:31 -04001282
1283static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1284 size_t count, loff_t *ppos)
1285{
Al Viro496ad9a2013-01-23 17:07:38 -05001286 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001287 struct task_struct *task = get_proc_task(inode);
1288 ssize_t length;
1289 char tmpbuf[TMPBUFLEN];
1290
1291 if (!task)
1292 return -ESRCH;
1293 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1294 audit_get_sessionid(task));
1295 put_task_struct(task);
1296 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1297}
1298
1299static const struct file_operations proc_sessionid_operations = {
1300 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001301 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001302};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303#endif
1304
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001305#ifdef CONFIG_FAULT_INJECTION
1306static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1307 size_t count, loff_t *ppos)
1308{
Al Viro496ad9a2013-01-23 17:07:38 -05001309 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001310 char buffer[PROC_NUMBUF];
1311 size_t len;
1312 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001313
1314 if (!task)
1315 return -ESRCH;
1316 make_it_fail = task->make_it_fail;
1317 put_task_struct(task);
1318
1319 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001320
1321 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001322}
1323
1324static ssize_t proc_fault_inject_write(struct file * file,
1325 const char __user * buf, size_t count, loff_t *ppos)
1326{
1327 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001328 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001329 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001330 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001331
1332 if (!capable(CAP_SYS_RESOURCE))
1333 return -EPERM;
1334 memset(buffer, 0, sizeof(buffer));
1335 if (count > sizeof(buffer) - 1)
1336 count = sizeof(buffer) - 1;
1337 if (copy_from_user(buffer, buf, count))
1338 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001339 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1340 if (rv < 0)
1341 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001342 if (make_it_fail < 0 || make_it_fail > 1)
1343 return -EINVAL;
1344
Al Viro496ad9a2013-01-23 17:07:38 -05001345 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001346 if (!task)
1347 return -ESRCH;
1348 task->make_it_fail = make_it_fail;
1349 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001350
1351 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001352}
1353
Arjan van de Ven00977a52007-02-12 00:55:34 -08001354static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001355 .read = proc_fault_inject_read,
1356 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001357 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001358};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001359
1360static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1361 size_t count, loff_t *ppos)
1362{
1363 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001364 int err;
1365 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001366
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001367 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001368 if (err)
1369 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001370
1371 task = get_proc_task(file_inode(file));
1372 if (!task)
1373 return -ESRCH;
1374 WRITE_ONCE(task->fail_nth, n);
1375 put_task_struct(task);
1376
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001377 return count;
1378}
1379
1380static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1381 size_t count, loff_t *ppos)
1382{
1383 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001384 char numbuf[PROC_NUMBUF];
1385 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001386
1387 task = get_proc_task(file_inode(file));
1388 if (!task)
1389 return -ESRCH;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001390 len = snprintf(numbuf, sizeof(numbuf), "%u\n",
1391 READ_ONCE(task->fail_nth));
Akinobu Mitabfc74092017-07-14 14:49:54 -07001392 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001393 put_task_struct(task);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001394
1395 return len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001396}
1397
1398static const struct file_operations proc_fail_nth_operations = {
1399 .read = proc_fail_nth_read,
1400 .write = proc_fail_nth_write,
1401};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001402#endif
1403
Arjan van de Ven97455122008-01-25 21:08:34 +01001404
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001405#ifdef CONFIG_SCHED_DEBUG
1406/*
1407 * Print out various scheduling related per-task fields:
1408 */
1409static int sched_show(struct seq_file *m, void *v)
1410{
1411 struct inode *inode = m->private;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001412 struct pid_namespace *ns = inode->i_sb->s_fs_info;
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001413 struct task_struct *p;
1414
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001415 p = get_proc_task(inode);
1416 if (!p)
1417 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001418 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001419
1420 put_task_struct(p);
1421
1422 return 0;
1423}
1424
1425static ssize_t
1426sched_write(struct file *file, const char __user *buf,
1427 size_t count, loff_t *offset)
1428{
Al Viro496ad9a2013-01-23 17:07:38 -05001429 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001430 struct task_struct *p;
1431
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001432 p = get_proc_task(inode);
1433 if (!p)
1434 return -ESRCH;
1435 proc_sched_set_task(p);
1436
1437 put_task_struct(p);
1438
1439 return count;
1440}
1441
1442static int sched_open(struct inode *inode, struct file *filp)
1443{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001444 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001445}
1446
1447static const struct file_operations proc_pid_sched_operations = {
1448 .open = sched_open,
1449 .read = seq_read,
1450 .write = sched_write,
1451 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001452 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001453};
1454
1455#endif
1456
Mike Galbraith5091faa2010-11-30 14:18:03 +01001457#ifdef CONFIG_SCHED_AUTOGROUP
1458/*
1459 * Print out autogroup related information:
1460 */
1461static int sched_autogroup_show(struct seq_file *m, void *v)
1462{
1463 struct inode *inode = m->private;
1464 struct task_struct *p;
1465
1466 p = get_proc_task(inode);
1467 if (!p)
1468 return -ESRCH;
1469 proc_sched_autogroup_show_task(p, m);
1470
1471 put_task_struct(p);
1472
1473 return 0;
1474}
1475
1476static ssize_t
1477sched_autogroup_write(struct file *file, const char __user *buf,
1478 size_t count, loff_t *offset)
1479{
Al Viro496ad9a2013-01-23 17:07:38 -05001480 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001481 struct task_struct *p;
1482 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001483 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001484 int err;
1485
1486 memset(buffer, 0, sizeof(buffer));
1487 if (count > sizeof(buffer) - 1)
1488 count = sizeof(buffer) - 1;
1489 if (copy_from_user(buffer, buf, count))
1490 return -EFAULT;
1491
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001492 err = kstrtoint(strstrip(buffer), 0, &nice);
1493 if (err < 0)
1494 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001495
1496 p = get_proc_task(inode);
1497 if (!p)
1498 return -ESRCH;
1499
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001500 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001501 if (err)
1502 count = err;
1503
1504 put_task_struct(p);
1505
1506 return count;
1507}
1508
1509static int sched_autogroup_open(struct inode *inode, struct file *filp)
1510{
1511 int ret;
1512
1513 ret = single_open(filp, sched_autogroup_show, NULL);
1514 if (!ret) {
1515 struct seq_file *m = filp->private_data;
1516
1517 m->private = inode;
1518 }
1519 return ret;
1520}
1521
1522static const struct file_operations proc_pid_sched_autogroup_operations = {
1523 .open = sched_autogroup_open,
1524 .read = seq_read,
1525 .write = sched_autogroup_write,
1526 .llseek = seq_lseek,
1527 .release = single_release,
1528};
1529
1530#endif /* CONFIG_SCHED_AUTOGROUP */
1531
john stultz4614a696b2009-12-14 18:00:05 -08001532static ssize_t comm_write(struct file *file, const char __user *buf,
1533 size_t count, loff_t *offset)
1534{
Al Viro496ad9a2013-01-23 17:07:38 -05001535 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001536 struct task_struct *p;
1537 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001538 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001539
1540 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001541 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001542 return -EFAULT;
1543
1544 p = get_proc_task(inode);
1545 if (!p)
1546 return -ESRCH;
1547
1548 if (same_thread_group(current, p))
1549 set_task_comm(p, buffer);
1550 else
1551 count = -EINVAL;
1552
1553 put_task_struct(p);
1554
1555 return count;
1556}
1557
1558static int comm_show(struct seq_file *m, void *v)
1559{
1560 struct inode *inode = m->private;
1561 struct task_struct *p;
1562
1563 p = get_proc_task(inode);
1564 if (!p)
1565 return -ESRCH;
1566
1567 task_lock(p);
1568 seq_printf(m, "%s\n", p->comm);
1569 task_unlock(p);
1570
1571 put_task_struct(p);
1572
1573 return 0;
1574}
1575
1576static int comm_open(struct inode *inode, struct file *filp)
1577{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001578 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001579}
1580
1581static const struct file_operations proc_pid_set_comm_operations = {
1582 .open = comm_open,
1583 .read = seq_read,
1584 .write = comm_write,
1585 .llseek = seq_lseek,
1586 .release = single_release,
1587};
1588
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001589static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001590{
1591 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001592 struct file *exe_file;
1593
David Howells2b0143b2015-03-17 22:25:59 +00001594 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001595 if (!task)
1596 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001597 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001598 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001599 if (exe_file) {
1600 *exe_path = exe_file->f_path;
1601 path_get(&exe_file->f_path);
1602 fput(exe_file);
1603 return 0;
1604 } else
1605 return -ENOENT;
1606}
1607
Al Viro6b255392015-11-17 10:20:54 -05001608static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001609 struct inode *inode,
1610 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001612 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 int error = -EACCES;
1614
Al Viro6b255392015-11-17 10:20:54 -05001615 if (!dentry)
1616 return ERR_PTR(-ECHILD);
1617
Eric W. Biederman778c1142006-06-26 00:25:58 -07001618 /* Are we allowed to snoop on the tasks file descriptors? */
1619 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Christoph Hellwig408ef012012-06-18 10:47:03 -04001622 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1623 if (error)
1624 goto out;
1625
Al Viro6e77137b2015-05-02 13:37:52 -04001626 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001627 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628out:
Al Viro008b1502005-08-20 00:17:39 +01001629 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001632static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001634 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001635 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int len;
1637
1638 if (!tmp)
1639 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001640
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001641 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001642 len = PTR_ERR(pathname);
1643 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001645 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 if (len > buflen)
1648 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001649 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 len = -EFAULT;
1651 out:
1652 free_page((unsigned long)tmp);
1653 return len;
1654}
1655
1656static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1657{
1658 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001659 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001660 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Eric W. Biederman778c1142006-06-26 00:25:58 -07001662 /* Are we allowed to snoop on the tasks file descriptors? */
1663 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001666 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (error)
1668 goto out;
1669
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001670 error = do_proc_readlink(&path, buffer, buflen);
1671 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return error;
1674}
1675
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001676const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001678 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001679 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680};
1681
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001682
1683/* building an inode */
1684
Al Viroc6eb50d2017-09-30 14:45:42 -04001685void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001686 kuid_t *ruid, kgid_t *rgid)
1687{
1688 /* Depending on the state of dumpable compute who should own a
1689 * proc file for a task.
1690 */
1691 const struct cred *cred;
1692 kuid_t uid;
1693 kgid_t gid;
1694
1695 /* Default to the tasks effective ownership */
1696 rcu_read_lock();
1697 cred = __task_cred(task);
1698 uid = cred->euid;
1699 gid = cred->egid;
1700 rcu_read_unlock();
1701
1702 /*
1703 * Before the /proc/pid/status file was created the only way to read
1704 * the effective uid of a /process was to stat /proc/pid. Reading
1705 * /proc/pid/status is slow enough that procps and other packages
1706 * kept stating /proc/pid. To keep the rules in /proc simple I have
1707 * made this apply to all per process world readable and executable
1708 * directories.
1709 */
1710 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1711 struct mm_struct *mm;
1712 task_lock(task);
1713 mm = task->mm;
1714 /* Make non-dumpable tasks owned by some root */
1715 if (mm) {
1716 if (get_dumpable(mm) != SUID_DUMP_USER) {
1717 struct user_namespace *user_ns = mm->user_ns;
1718
1719 uid = make_kuid(user_ns, 0);
1720 if (!uid_valid(uid))
1721 uid = GLOBAL_ROOT_UID;
1722
1723 gid = make_kgid(user_ns, 0);
1724 if (!gid_valid(gid))
1725 gid = GLOBAL_ROOT_GID;
1726 }
1727 } else {
1728 uid = GLOBAL_ROOT_UID;
1729 gid = GLOBAL_ROOT_GID;
1730 }
1731 task_unlock(task);
1732 }
1733 *ruid = uid;
1734 *rgid = gid;
1735}
1736
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001737struct inode *proc_pid_make_inode(struct super_block * sb,
1738 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739{
1740 struct inode * inode;
1741 struct proc_inode *ei;
1742
1743 /* We need a new inode */
1744
1745 inode = new_inode(sb);
1746 if (!inode)
1747 goto out;
1748
1749 /* Common stuff */
1750 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001751 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001752 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001753 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001754 inode->i_op = &proc_def_inode_operations;
1755
1756 /*
1757 * grab the reference to task.
1758 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001759 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001760 if (!ei->pid)
1761 goto out_unlock;
1762
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001763 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764 security_task_to_inode(task, inode);
1765
1766out:
1767 return inode;
1768
1769out_unlock:
1770 iput(inode);
1771 return NULL;
1772}
1773
David Howellsa528d352017-01-31 16:46:22 +00001774int pid_getattr(const struct path *path, struct kstat *stat,
1775 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001776{
David Howellsa528d352017-01-31 16:46:22 +00001777 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778 struct task_struct *task;
David Howellsa528d352017-01-31 16:46:22 +00001779 struct pid_namespace *pid = path->dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001780
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781 generic_fillattr(inode, stat);
1782
1783 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001784 stat->uid = GLOBAL_ROOT_UID;
1785 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001786 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1787 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001788 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001789 rcu_read_unlock();
1790 /*
1791 * This doesn't prevent learning whether PID exists,
1792 * it only makes getattr() consistent with readdir().
1793 */
1794 return -ENOENT;
1795 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001796 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001797 }
1798 rcu_read_unlock();
1799 return 0;
1800}
1801
1802/* dentry stuff */
1803
1804/*
1805 * Exceptional case: normally we are not allowed to unhash a busy
1806 * directory. In this case, however, we can do it - no aliasing problems
1807 * due to the way we treat inodes.
1808 *
1809 * 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 Viro0b728e12012-06-10 16:03:43 -04001813int 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) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001825 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1826
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001827 inode->i_mode &= ~(S_ISUID | S_ISGID);
1828 security_task_to_inode(task, inode);
1829 put_task_struct(task);
1830 return 1;
1831 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001832 return 0;
1833}
1834
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001835static inline bool proc_inode_is_dead(struct inode *inode)
1836{
1837 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1838}
1839
David Howells1dd704b2013-04-12 01:08:50 +01001840int pid_delete_dentry(const struct dentry *dentry)
1841{
1842 /* Is the task we represent dead?
1843 * If so, then don't put the dentry on the lru list,
1844 * kill it immediately.
1845 */
David Howells2b0143b2015-03-17 22:25:59 +00001846 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001847}
1848
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001849const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001850{
1851 .d_revalidate = pid_revalidate,
1852 .d_delete = pid_delete_dentry,
1853};
1854
1855/* Lookups */
1856
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001857/*
1858 * Fill a directory entry.
1859 *
1860 * If possible create the dcache entry and derive our inode number and
1861 * file type from dcache entry.
1862 *
1863 * Since all of the proc inode numbers are dynamically generated, the inode
1864 * numbers do not exist until the inode is cache. This means creating the
1865 * the dcache entry in readdir is necessary to keep the inode numbers
1866 * reported by readdir in sync with the inode numbers reported
1867 * by stat.
1868 */
Al Virof0c3b502013-05-16 12:07:31 -04001869bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001870 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001871 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001872{
Al Virof0c3b502013-05-16 12:07:31 -04001873 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001874 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001875 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001876 unsigned type;
1877 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001878
Al Viro1df98b82013-06-15 11:33:10 +04001879 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001880 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001881 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1882 child = d_alloc_parallel(dir, &qname, &wq);
1883 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001884 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001885 if (d_in_lookup(child)) {
1886 int err = instantiate(d_inode(dir), child, task, ptr);
1887 d_lookup_done(child);
1888 if (err < 0) {
1889 dput(child);
1890 goto end_instantiate;
1891 }
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;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001897 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001898 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001899
1900end_instantiate:
1901 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001902}
1903
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001904/*
1905 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1906 * which represent vma start and end addresses.
1907 */
1908static int dname_to_vma_addr(struct dentry *dentry,
1909 unsigned long *start, unsigned long *end)
1910{
1911 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1912 return -EINVAL;
1913
1914 return 0;
1915}
1916
Al Viro0b728e12012-06-10 16:03:43 -04001917static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001918{
1919 unsigned long vm_start, vm_end;
1920 bool exact_vma_exists = false;
1921 struct mm_struct *mm = NULL;
1922 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001923 struct inode *inode;
1924 int status = 0;
1925
Al Viro0b728e12012-06-10 16:03:43 -04001926 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001927 return -ECHILD;
1928
David Howells2b0143b2015-03-17 22:25:59 +00001929 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001930 task = get_proc_task(inode);
1931 if (!task)
1932 goto out_notask;
1933
Jann Horncaaee622016-01-20 15:00:04 -08001934 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001935 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001936 goto out;
1937
1938 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1939 down_read(&mm->mmap_sem);
1940 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1941 up_read(&mm->mmap_sem);
1942 }
1943
1944 mmput(mm);
1945
1946 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001947 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1948
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001949 security_task_to_inode(task, inode);
1950 status = 1;
1951 }
1952
1953out:
1954 put_task_struct(task);
1955
1956out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001957 return status;
1958}
1959
1960static const struct dentry_operations tid_map_files_dentry_operations = {
1961 .d_revalidate = map_files_d_revalidate,
1962 .d_delete = pid_delete_dentry,
1963};
1964
Al Viro6b255392015-11-17 10:20:54 -05001965static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001966{
1967 unsigned long vm_start, vm_end;
1968 struct vm_area_struct *vma;
1969 struct task_struct *task;
1970 struct mm_struct *mm;
1971 int rc;
1972
1973 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001974 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001975 if (!task)
1976 goto out;
1977
1978 mm = get_task_mm(task);
1979 put_task_struct(task);
1980 if (!mm)
1981 goto out;
1982
1983 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1984 if (rc)
1985 goto out_mmput;
1986
Artem Fetishev70335ab2014-03-10 15:49:45 -07001987 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001988 down_read(&mm->mmap_sem);
1989 vma = find_exact_vma(mm, vm_start, vm_end);
1990 if (vma && vma->vm_file) {
1991 *path = vma->vm_file->f_path;
1992 path_get(path);
1993 rc = 0;
1994 }
1995 up_read(&mm->mmap_sem);
1996
1997out_mmput:
1998 mmput(mm);
1999out:
2000 return rc;
2001}
2002
2003struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04002004 fmode_t mode;
Alexey Dobriyan9a87fe02016-12-12 16:45:11 -08002005 unsigned int len;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002006 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2007};
2008
Calvin Owensbdb4d102015-09-09 15:35:54 -07002009/*
2010 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2011 * symlinks may be used to bypass permissions on ancestor directories in the
2012 * path to the file in question.
2013 */
2014static const char *
Al Viro6b255392015-11-17 10:20:54 -05002015proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002016 struct inode *inode,
2017 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002018{
2019 if (!capable(CAP_SYS_ADMIN))
2020 return ERR_PTR(-EPERM);
2021
Al Virofceef392015-12-29 15:58:39 -05002022 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002023}
2024
2025/*
Al Viro6b255392015-11-17 10:20:54 -05002026 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002027 */
2028static const struct inode_operations proc_map_files_link_inode_operations = {
2029 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002030 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002031 .setattr = proc_setattr,
2032};
2033
Al Viroc52a47a2013-06-15 11:15:20 +04002034static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002035proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2036 struct task_struct *task, const void *ptr)
2037{
Al Viro7b540d02012-08-27 14:55:26 -04002038 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002039 struct proc_inode *ei;
2040 struct inode *inode;
2041
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002042 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2043 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2044 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002045 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002046 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002047
2048 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002049 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002050
Calvin Owensbdb4d102015-09-09 15:35:54 -07002051 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002052 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002053
2054 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2055 d_add(dentry, inode);
2056
Al Viroc52a47a2013-06-15 11:15:20 +04002057 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002058}
2059
2060static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002061 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002062{
2063 unsigned long vm_start, vm_end;
2064 struct vm_area_struct *vma;
2065 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002066 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002067 struct mm_struct *mm;
2068
Al Viroc52a47a2013-06-15 11:15:20 +04002069 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002070 task = get_proc_task(dir);
2071 if (!task)
2072 goto out;
2073
Al Viroc52a47a2013-06-15 11:15:20 +04002074 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002075 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002076 goto out_put_task;
2077
Al Viroc52a47a2013-06-15 11:15:20 +04002078 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002079 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002080 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081
2082 mm = get_task_mm(task);
2083 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002084 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002085
2086 down_read(&mm->mmap_sem);
2087 vma = find_exact_vma(mm, vm_start, vm_end);
2088 if (!vma)
2089 goto out_no_vma;
2090
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002091 if (vma->vm_file)
2092 result = proc_map_files_instantiate(dir, dentry, task,
2093 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002094
2095out_no_vma:
2096 up_read(&mm->mmap_sem);
2097 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002098out_put_task:
2099 put_task_struct(task);
2100out:
Al Viroc52a47a2013-06-15 11:15:20 +04002101 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002102}
2103
2104static const struct inode_operations proc_map_files_inode_operations = {
2105 .lookup = proc_map_files_lookup,
2106 .permission = proc_fd_permission,
2107 .setattr = proc_setattr,
2108};
2109
2110static int
Al Virof0c3b502013-05-16 12:07:31 -04002111proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002112{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002113 struct vm_area_struct *vma;
2114 struct task_struct *task;
2115 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002116 unsigned long nr_files, pos, i;
2117 struct flex_array *fa = NULL;
2118 struct map_files_info info;
2119 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120 int ret;
2121
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002122 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002123 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002124 if (!task)
2125 goto out;
2126
2127 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002128 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002129 goto out_put_task;
2130
2131 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002132 if (!dir_emit_dots(file, ctx))
2133 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002134
Al Virof0c3b502013-05-16 12:07:31 -04002135 mm = get_task_mm(task);
2136 if (!mm)
2137 goto out_put_task;
2138 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002139
Al Virof0c3b502013-05-16 12:07:31 -04002140 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002141
Al Virof0c3b502013-05-16 12:07:31 -04002142 /*
2143 * We need two passes here:
2144 *
2145 * 1) Collect vmas of mapped files with mmap_sem taken
2146 * 2) Release mmap_sem and instantiate entries
2147 *
2148 * otherwise we get lockdep complained, since filldir()
2149 * routine might require mmap_sem taken in might_fault().
2150 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002151
Al Virof0c3b502013-05-16 12:07:31 -04002152 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2153 if (vma->vm_file && ++pos > ctx->pos)
2154 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002155 }
Al Virof0c3b502013-05-16 12:07:31 -04002156
2157 if (nr_files) {
2158 fa = flex_array_alloc(sizeof(info), nr_files,
2159 GFP_KERNEL);
2160 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2161 GFP_KERNEL)) {
2162 ret = -ENOMEM;
2163 if (fa)
2164 flex_array_free(fa);
2165 up_read(&mm->mmap_sem);
2166 mmput(mm);
2167 goto out_put_task;
2168 }
2169 for (i = 0, vma = mm->mmap, pos = 2; vma;
2170 vma = vma->vm_next) {
2171 if (!vma->vm_file)
2172 continue;
2173 if (++pos <= ctx->pos)
2174 continue;
2175
2176 info.mode = vma->vm_file->f_mode;
2177 info.len = snprintf(info.name,
2178 sizeof(info.name), "%lx-%lx",
2179 vma->vm_start, vma->vm_end);
2180 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2181 BUG();
2182 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002183 }
Al Virof0c3b502013-05-16 12:07:31 -04002184 up_read(&mm->mmap_sem);
2185
2186 for (i = 0; i < nr_files; i++) {
2187 p = flex_array_get(fa, i);
2188 if (!proc_fill_cache(file, ctx,
2189 p->name, p->len,
2190 proc_map_files_instantiate,
2191 task,
2192 (void *)(unsigned long)p->mode))
2193 break;
2194 ctx->pos++;
2195 }
2196 if (fa)
2197 flex_array_free(fa);
2198 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002199
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002200out_put_task:
2201 put_task_struct(task);
2202out:
2203 return ret;
2204}
2205
2206static const struct file_operations proc_map_files_operations = {
2207 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002208 .iterate_shared = proc_map_files_readdir,
2209 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002210};
2211
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002212#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002213struct timers_private {
2214 struct pid *pid;
2215 struct task_struct *task;
2216 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002217 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002218 unsigned long flags;
2219};
2220
2221static void *timers_start(struct seq_file *m, loff_t *pos)
2222{
2223 struct timers_private *tp = m->private;
2224
2225 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2226 if (!tp->task)
2227 return ERR_PTR(-ESRCH);
2228
2229 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2230 if (!tp->sighand)
2231 return ERR_PTR(-ESRCH);
2232
2233 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2234}
2235
2236static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2237{
2238 struct timers_private *tp = m->private;
2239 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2240}
2241
2242static void timers_stop(struct seq_file *m, void *v)
2243{
2244 struct timers_private *tp = m->private;
2245
2246 if (tp->sighand) {
2247 unlock_task_sighand(tp->task, &tp->flags);
2248 tp->sighand = NULL;
2249 }
2250
2251 if (tp->task) {
2252 put_task_struct(tp->task);
2253 tp->task = NULL;
2254 }
2255}
2256
2257static int show_timer(struct seq_file *m, void *v)
2258{
2259 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002260 struct timers_private *tp = m->private;
2261 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002262 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002263 [SIGEV_SIGNAL] = "signal",
2264 [SIGEV_NONE] = "none",
2265 [SIGEV_THREAD] = "thread",
2266 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002267
2268 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002269 notify = timer->it_sigev_notify;
2270
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002271 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002272 seq_printf(m, "signal: %d/%p\n",
2273 timer->sigq->info.si_signo,
2274 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002275 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002276 nstr[notify & ~SIGEV_THREAD_ID],
2277 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2278 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002279 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002280
2281 return 0;
2282}
2283
2284static const struct seq_operations proc_timers_seq_ops = {
2285 .start = timers_start,
2286 .next = timers_next,
2287 .stop = timers_stop,
2288 .show = show_timer,
2289};
2290
2291static int proc_timers_open(struct inode *inode, struct file *file)
2292{
2293 struct timers_private *tp;
2294
2295 tp = __seq_open_private(file, &proc_timers_seq_ops,
2296 sizeof(struct timers_private));
2297 if (!tp)
2298 return -ENOMEM;
2299
2300 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002301 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002302 return 0;
2303}
2304
2305static const struct file_operations proc_timers_operations = {
2306 .open = proc_timers_open,
2307 .read = seq_read,
2308 .llseek = seq_lseek,
2309 .release = seq_release_private,
2310};
Eric Engestromb5946be2016-03-17 14:20:57 -07002311#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002312
John Stultz5de23d42016-03-17 14:20:54 -07002313static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2314 size_t count, loff_t *offset)
2315{
2316 struct inode *inode = file_inode(file);
2317 struct task_struct *p;
2318 u64 slack_ns;
2319 int err;
2320
2321 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2322 if (err < 0)
2323 return err;
2324
2325 p = get_proc_task(inode);
2326 if (!p)
2327 return -ESRCH;
2328
John Stultz4b2bd5f2016-10-07 17:02:33 -07002329 if (p != current) {
2330 if (!capable(CAP_SYS_NICE)) {
2331 count = -EPERM;
2332 goto out;
2333 }
John Stultz5de23d42016-03-17 14:20:54 -07002334
John Stultz4b2bd5f2016-10-07 17:02:33 -07002335 err = security_task_setscheduler(p);
2336 if (err) {
2337 count = err;
2338 goto out;
2339 }
John Stultz904763e2016-10-07 17:02:29 -07002340 }
2341
John Stultz7abbaf92016-10-07 17:02:26 -07002342 task_lock(p);
2343 if (slack_ns == 0)
2344 p->timer_slack_ns = p->default_timer_slack_ns;
2345 else
2346 p->timer_slack_ns = slack_ns;
2347 task_unlock(p);
2348
2349out:
John Stultz5de23d42016-03-17 14:20:54 -07002350 put_task_struct(p);
2351
2352 return count;
2353}
2354
2355static int timerslack_ns_show(struct seq_file *m, void *v)
2356{
2357 struct inode *inode = m->private;
2358 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002359 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002360
2361 p = get_proc_task(inode);
2362 if (!p)
2363 return -ESRCH;
2364
John Stultz4b2bd5f2016-10-07 17:02:33 -07002365 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002366
John Stultz4b2bd5f2016-10-07 17:02:33 -07002367 if (!capable(CAP_SYS_NICE)) {
2368 err = -EPERM;
2369 goto out;
2370 }
2371 err = security_task_getscheduler(p);
2372 if (err)
2373 goto out;
2374 }
John Stultz904763e2016-10-07 17:02:29 -07002375
John Stultz7abbaf92016-10-07 17:02:26 -07002376 task_lock(p);
2377 seq_printf(m, "%llu\n", p->timer_slack_ns);
2378 task_unlock(p);
2379
2380out:
John Stultz5de23d42016-03-17 14:20:54 -07002381 put_task_struct(p);
2382
2383 return err;
2384}
2385
2386static int timerslack_ns_open(struct inode *inode, struct file *filp)
2387{
2388 return single_open(filp, timerslack_ns_show, inode);
2389}
2390
2391static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2392 .open = timerslack_ns_open,
2393 .read = seq_read,
2394 .write = timerslack_ns_write,
2395 .llseek = seq_lseek,
2396 .release = single_release,
2397};
2398
Al Viroc52a47a2013-06-15 11:15:20 +04002399static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002400 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002401{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002402 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002403 struct inode *inode;
2404 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002405
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002406 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002407 if (!inode)
2408 goto out;
2409
2410 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002411 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002412 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002413 if (p->iop)
2414 inode->i_op = p->iop;
2415 if (p->fop)
2416 inode->i_fop = p->fop;
2417 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002418 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002419 d_add(dentry, inode);
2420 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002421 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002422 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002423out:
Al Viroc52a47a2013-06-15 11:15:20 +04002424 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002425}
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427static struct dentry *proc_pident_lookup(struct inode *dir,
2428 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002429 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002430 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Al Viroc52a47a2013-06-15 11:15:20 +04002432 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002433 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002434 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Al Viroc52a47a2013-06-15 11:15:20 +04002436 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Eric W. Biederman99f89552006-06-26 00:25:55 -07002438 if (!task)
2439 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002441 /*
2442 * Yes, it does not scale. And it should not. Don't add
2443 * new entries into /proc/<tgid>/ without very good reasons.
2444 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002445 last = &ents[nents];
2446 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (p->len != dentry->d_name.len)
2448 continue;
2449 if (!memcmp(dentry->d_name.name, p->name, p->len))
2450 break;
2451 }
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002452 if (p >= last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 goto out;
2454
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002455 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002457 put_task_struct(task);
2458out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002459 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Al Virof0c3b502013-05-16 12:07:31 -04002462static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002463 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002464{
Al Virof0c3b502013-05-16 12:07:31 -04002465 struct task_struct *task = get_proc_task(file_inode(file));
2466 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002467
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002468 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002469 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002470
Al Virof0c3b502013-05-16 12:07:31 -04002471 if (!dir_emit_dots(file, ctx))
2472 goto out;
2473
2474 if (ctx->pos >= nents + 2)
2475 goto out;
2476
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002477 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002478 if (!proc_fill_cache(file, ctx, p->name, p->len,
2479 proc_pident_instantiate, task, p))
2480 break;
2481 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002482 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002483out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002484 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002489static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2490 size_t count, loff_t *ppos)
2491{
Al Viro496ad9a2013-01-23 17:07:38 -05002492 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002493 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002494 ssize_t length;
2495 struct task_struct *task = get_proc_task(inode);
2496
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002497 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002498 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002499
2500 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002501 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002502 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002503 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002504 if (length > 0)
2505 length = simple_read_from_buffer(buf, count, ppos, p, length);
2506 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002507 return length;
2508}
2509
2510static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2511 size_t count, loff_t *ppos)
2512{
Al Viro496ad9a2013-01-23 17:07:38 -05002513 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002514 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002515 ssize_t length;
2516 struct task_struct *task = get_proc_task(inode);
2517
2518 length = -ESRCH;
2519 if (!task)
2520 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002521
2522 /* A task may only write its own attributes. */
2523 length = -EACCES;
2524 if (current != task)
2525 goto out;
2526
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002527 if (count > PAGE_SIZE)
2528 count = PAGE_SIZE;
2529
2530 /* No partial writes. */
2531 length = -EINVAL;
2532 if (*ppos != 0)
2533 goto out;
2534
Al Virobb646cd2015-12-24 00:16:30 -05002535 page = memdup_user(buf, count);
2536 if (IS_ERR(page)) {
2537 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002538 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002539 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002540
David Howells107db7c2009-05-08 13:55:27 +01002541 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002542 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002543 if (length < 0)
2544 goto out_free;
2545
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002546 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002547 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002548 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002549out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002550 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551out:
2552 put_task_struct(task);
2553out_no_task:
2554 return length;
2555}
2556
Arjan van de Ven00977a52007-02-12 00:55:34 -08002557static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002558 .read = proc_pid_attr_read,
2559 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002560 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002561};
2562
Eric Dumazetc5141e62007-05-08 00:26:15 -07002563static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002564 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2565 REG("prev", S_IRUGO, proc_pid_attr_operations),
2566 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2567 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2568 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2569 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002570};
2571
Al Virof0c3b502013-05-16 12:07:31 -04002572static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Al Virof0c3b502013-05-16 12:07:31 -04002574 return proc_pident_readdir(file, ctx,
2575 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Arjan van de Ven00977a52007-02-12 00:55:34 -08002578static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002580 .iterate_shared = proc_attr_dir_readdir,
2581 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582};
2583
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002584static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002585 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002587 return proc_pident_lookup(dir, dentry,
2588 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002591static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002592 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002593 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002594 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595};
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597#endif
2598
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002599#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002600static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2601 size_t count, loff_t *ppos)
2602{
Al Viro496ad9a2013-01-23 17:07:38 -05002603 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002604 struct mm_struct *mm;
2605 char buffer[PROC_NUMBUF];
2606 size_t len;
2607 int ret;
2608
2609 if (!task)
2610 return -ESRCH;
2611
2612 ret = 0;
2613 mm = get_task_mm(task);
2614 if (mm) {
2615 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2616 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2617 MMF_DUMP_FILTER_SHIFT));
2618 mmput(mm);
2619 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2620 }
2621
2622 put_task_struct(task);
2623
2624 return ret;
2625}
2626
2627static ssize_t proc_coredump_filter_write(struct file *file,
2628 const char __user *buf,
2629 size_t count,
2630 loff_t *ppos)
2631{
2632 struct task_struct *task;
2633 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002634 unsigned int val;
2635 int ret;
2636 int i;
2637 unsigned long mask;
2638
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002639 ret = kstrtouint_from_user(buf, count, 0, &val);
2640 if (ret < 0)
2641 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002642
2643 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002644 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002645 if (!task)
2646 goto out_no_task;
2647
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002648 mm = get_task_mm(task);
2649 if (!mm)
2650 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002651 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002652
2653 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2654 if (val & mask)
2655 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2656 else
2657 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2658 }
2659
2660 mmput(mm);
2661 out_no_mm:
2662 put_task_struct(task);
2663 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002664 if (ret < 0)
2665 return ret;
2666 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002667}
2668
2669static const struct file_operations proc_coredump_filter_operations = {
2670 .read = proc_coredump_filter_read,
2671 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002672 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002673};
2674#endif
2675
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002676#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002677static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002678{
Andrea Righi940389b2008-07-28 00:48:12 +02002679 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002680 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002681 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002682
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002683 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2684 if (result)
2685 return result;
2686
Jann Horncaaee622016-01-20 15:00:04 -08002687 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002688 result = -EACCES;
2689 goto out_unlock;
2690 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002691
Andrea Righi59954772008-07-27 17:29:15 +02002692 if (whole && lock_task_sighand(task, &flags)) {
2693 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002694
Andrea Righi59954772008-07-27 17:29:15 +02002695 task_io_accounting_add(&acct, &task->signal->ioac);
2696 while_each_thread(task, t)
2697 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002698
Andrea Righi59954772008-07-27 17:29:15 +02002699 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002700 }
Joe Perches25ce3192015-04-15 16:18:17 -07002701 seq_printf(m,
2702 "rchar: %llu\n"
2703 "wchar: %llu\n"
2704 "syscr: %llu\n"
2705 "syscw: %llu\n"
2706 "read_bytes: %llu\n"
2707 "write_bytes: %llu\n"
2708 "cancelled_write_bytes: %llu\n",
2709 (unsigned long long)acct.rchar,
2710 (unsigned long long)acct.wchar,
2711 (unsigned long long)acct.syscr,
2712 (unsigned long long)acct.syscw,
2713 (unsigned long long)acct.read_bytes,
2714 (unsigned long long)acct.write_bytes,
2715 (unsigned long long)acct.cancelled_write_bytes);
2716 result = 0;
2717
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002718out_unlock:
2719 mutex_unlock(&task->signal->cred_guard_mutex);
2720 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002721}
Andrea Righi297c5d92008-07-25 01:48:49 -07002722
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002723static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2724 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002725{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002726 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002727}
2728
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002729static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2730 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002731{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002732 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002733}
2734#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002735
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002736#ifdef CONFIG_USER_NS
2737static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002738 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002739{
2740 struct user_namespace *ns = NULL;
2741 struct task_struct *task;
2742 struct seq_file *seq;
2743 int ret = -EINVAL;
2744
2745 task = get_proc_task(inode);
2746 if (task) {
2747 rcu_read_lock();
2748 ns = get_user_ns(task_cred_xxx(task, user_ns));
2749 rcu_read_unlock();
2750 put_task_struct(task);
2751 }
2752 if (!ns)
2753 goto err;
2754
2755 ret = seq_open(file, seq_ops);
2756 if (ret)
2757 goto err_put_ns;
2758
2759 seq = file->private_data;
2760 seq->private = ns;
2761
2762 return 0;
2763err_put_ns:
2764 put_user_ns(ns);
2765err:
2766 return ret;
2767}
2768
2769static int proc_id_map_release(struct inode *inode, struct file *file)
2770{
2771 struct seq_file *seq = file->private_data;
2772 struct user_namespace *ns = seq->private;
2773 put_user_ns(ns);
2774 return seq_release(inode, file);
2775}
2776
2777static int proc_uid_map_open(struct inode *inode, struct file *file)
2778{
2779 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2780}
2781
2782static int proc_gid_map_open(struct inode *inode, struct file *file)
2783{
2784 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2785}
2786
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002787static int proc_projid_map_open(struct inode *inode, struct file *file)
2788{
2789 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2790}
2791
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002792static const struct file_operations proc_uid_map_operations = {
2793 .open = proc_uid_map_open,
2794 .write = proc_uid_map_write,
2795 .read = seq_read,
2796 .llseek = seq_lseek,
2797 .release = proc_id_map_release,
2798};
2799
2800static const struct file_operations proc_gid_map_operations = {
2801 .open = proc_gid_map_open,
2802 .write = proc_gid_map_write,
2803 .read = seq_read,
2804 .llseek = seq_lseek,
2805 .release = proc_id_map_release,
2806};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002807
2808static const struct file_operations proc_projid_map_operations = {
2809 .open = proc_projid_map_open,
2810 .write = proc_projid_map_write,
2811 .read = seq_read,
2812 .llseek = seq_lseek,
2813 .release = proc_id_map_release,
2814};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002815
2816static int proc_setgroups_open(struct inode *inode, struct file *file)
2817{
2818 struct user_namespace *ns = NULL;
2819 struct task_struct *task;
2820 int ret;
2821
2822 ret = -ESRCH;
2823 task = get_proc_task(inode);
2824 if (task) {
2825 rcu_read_lock();
2826 ns = get_user_ns(task_cred_xxx(task, user_ns));
2827 rcu_read_unlock();
2828 put_task_struct(task);
2829 }
2830 if (!ns)
2831 goto err;
2832
2833 if (file->f_mode & FMODE_WRITE) {
2834 ret = -EACCES;
2835 if (!ns_capable(ns, CAP_SYS_ADMIN))
2836 goto err_put_ns;
2837 }
2838
2839 ret = single_open(file, &proc_setgroups_show, ns);
2840 if (ret)
2841 goto err_put_ns;
2842
2843 return 0;
2844err_put_ns:
2845 put_user_ns(ns);
2846err:
2847 return ret;
2848}
2849
2850static int proc_setgroups_release(struct inode *inode, struct file *file)
2851{
2852 struct seq_file *seq = file->private_data;
2853 struct user_namespace *ns = seq->private;
2854 int ret = single_release(inode, file);
2855 put_user_ns(ns);
2856 return ret;
2857}
2858
2859static const struct file_operations proc_setgroups_operations = {
2860 .open = proc_setgroups_open,
2861 .write = proc_setgroups_write,
2862 .read = seq_read,
2863 .llseek = seq_lseek,
2864 .release = proc_setgroups_release,
2865};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002866#endif /* CONFIG_USER_NS */
2867
Kees Cook47830722008-10-06 03:11:58 +04002868static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2869 struct pid *pid, struct task_struct *task)
2870{
Al Viroa9712bc2011-03-23 15:52:50 -04002871 int err = lock_trace(task);
2872 if (!err) {
2873 seq_printf(m, "%08x\n", task->personality);
2874 unlock_trace(task);
2875 }
2876 return err;
Kees Cook47830722008-10-06 03:11:58 +04002877}
2878
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002879#ifdef CONFIG_LIVEPATCH
2880static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2881 struct pid *pid, struct task_struct *task)
2882{
2883 seq_printf(m, "%d\n", task->patch_state);
2884 return 0;
2885}
2886#endif /* CONFIG_LIVEPATCH */
2887
Eric W. Biederman801199c2006-10-02 02:18:48 -07002888/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002889 * Thread groups
2890 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002891static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002892static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002893
Eric Dumazetc5141e62007-05-08 00:26:15 -07002894static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002895 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2896 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002897 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002898 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002899 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002900#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002901 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002902#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002903 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002904 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002905 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002906 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002907 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002908#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002909 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002910#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002911#ifdef CONFIG_SCHED_AUTOGROUP
2912 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2913#endif
john stultz4614a696b2009-12-14 18:00:05 -08002914 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002915#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002916 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002917#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002918 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002919 ONE("stat", S_IRUGO, proc_tgid_stat),
2920 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002921 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002922#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002923 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002924#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002925 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2926 LNK("cwd", proc_cwd_link),
2927 LNK("root", proc_root_link),
2928 LNK("exe", proc_exe_link),
2929 REG("mounts", S_IRUGO, proc_mounts_operations),
2930 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2931 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002932#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002933 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002934 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002935 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002936 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002937#endif
2938#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002939 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002940#endif
2941#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002942 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002943#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002944#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002945 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002946#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302947#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002948 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002949#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002950#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002951 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002952#endif
Paul Menage8793d852007-10-18 23:39:39 -07002953#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002954 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002955#endif
Paul Menagea4243162007-10-18 23:39:35 -07002956#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002957 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002958#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002959 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002960 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002961 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002962#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002963 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2964 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002965#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002966#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002967 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002968 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002969#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002970#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002971 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002972#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002973#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002974 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002975#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002976#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002977 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002978#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002979#ifdef CONFIG_USER_NS
2980 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2981 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002982 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002983 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002984#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002985#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002986 REG("timers", S_IRUGO, proc_timers_operations),
2987#endif
John Stultz5de23d42016-03-17 14:20:54 -07002988 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002989#ifdef CONFIG_LIVEPATCH
2990 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
2991#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002992};
2993
Al Virof0c3b502013-05-16 12:07:31 -04002994static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002995{
Al Virof0c3b502013-05-16 12:07:31 -04002996 return proc_pident_readdir(file, ctx,
2997 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002998}
2999
Arjan van de Ven00977a52007-02-12 00:55:34 -08003000static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003001 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003002 .iterate_shared = proc_tgid_base_readdir,
3003 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003004};
3005
Al Viro00cd8dd2012-06-10 17:13:09 -04003006static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3007{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003008 return proc_pident_lookup(dir, dentry,
3009 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003010}
3011
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003012static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003013 .lookup = proc_tgid_base_lookup,
3014 .getattr = pid_getattr,
3015 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003016 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003017};
3018
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003019static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003021 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003022 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003023 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Eric W. Biederman48e64842006-06-26 00:25:48 -07003025 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003026 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003027 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003028 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003029 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003030 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003031 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003034 if (pid == tgid)
3035 return;
3036
Eric W. Biederman48e64842006-06-26 00:25:48 -07003037 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003038 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3039 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003040 if (!leader)
3041 goto out;
3042
3043 name.name = "task";
3044 name.len = strlen(name.name);
3045 dir = d_hash_and_lookup(leader, &name);
3046 if (!dir)
3047 goto out_put_leader;
3048
3049 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003050 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003051 dentry = d_hash_and_lookup(dir, &name);
3052 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003053 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003054 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003056
3057 dput(dir);
3058out_put_leader:
3059 dput(leader);
3060out:
3061 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Randy Dunlap0895e912007-10-21 21:00:10 -07003064/**
3065 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3066 * @task: task that should be flushed.
3067 *
3068 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003069 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003070 * in. This call is supposed to do all of this job.
3071 *
3072 * Looks in the dcache for
3073 * /proc/@pid
3074 * /proc/@tgid/task/@pid
3075 * if either directory is present flushes it and all of it'ts children
3076 * from the dcache.
3077 *
3078 * It is safe and reasonable to cache /proc entries for a task until
3079 * that task exits. After that they just clog up the dcache with
3080 * useless entries, possibly causing useful dcache entries to be
3081 * flushed instead. This routine is proved to flush those useless
3082 * dcache entries at process exit time.
3083 *
3084 * NOTE: This routine is just an optimization so it does not guarantee
3085 * that no dcache entries will exist at process exit time it
3086 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003087 */
3088
3089void proc_flush_task(struct task_struct *task)
3090{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003091 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003092 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003093 struct upid *upid;
3094
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003095 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003096 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003097
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003098 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003099 upid = &pid->numbers[i];
3100 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003101 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003102 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003103}
3104
Al Viroc52a47a2013-06-15 11:15:20 +04003105static int proc_pid_instantiate(struct inode *dir,
3106 struct dentry * dentry,
3107 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003108{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003109 struct inode *inode;
3110
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003111 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003112 if (!inode)
3113 goto out;
3114
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003115 inode->i_op = &proc_tgid_base_inode_operations;
3116 inode->i_fop = &proc_tgid_base_operations;
3117 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003118
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003119 set_nlink(inode, nlink_tgid);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003120
Nick Pigginfb045ad2011-01-07 17:49:55 +11003121 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003122
3123 d_add(dentry, inode);
3124 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003125 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003126 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003127out:
Al Viroc52a47a2013-06-15 11:15:20 +04003128 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003129}
3130
Al Viro00cd8dd2012-06-10 17:13:09 -04003131struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003133 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003136 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003138 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (tgid == ~0U)
3140 goto out;
3141
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003142 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003143 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003144 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 if (task)
3146 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003147 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 if (!task)
3149 goto out;
3150
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003151 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003152 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153out:
Al Viroc52a47a2013-06-15 11:15:20 +04003154 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003158 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003159 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003161struct tgid_iter {
3162 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003163 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003164};
3165static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3166{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003167 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003169 if (iter.task)
3170 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003171 rcu_read_lock();
3172retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003173 iter.task = NULL;
3174 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003175 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003176 iter.tgid = pid_nr_ns(pid, ns);
3177 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003178 /* What we to know is if the pid we have find is the
3179 * pid of a thread_group_leader. Testing for task
3180 * being a thread_group_leader is the obvious thing
3181 * todo but there is a window when it fails, due to
3182 * the pid transfer logic in de_thread.
3183 *
3184 * So we perform the straight forward test of seeing
3185 * if the pid we have found is the pid of a thread
3186 * group leader, and don't worry if the task we have
3187 * found doesn't happen to be a thread group leader.
3188 * As we don't care in the case of readdir.
3189 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003190 if (!iter.task || !has_group_leader_pid(iter.task)) {
3191 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003192 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003193 }
3194 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003196 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003197 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
Eric W. Biederman00978752014-07-31 03:10:50 -07003200#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003203int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003205 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003206 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003207 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Al Viro021ada72013-03-29 19:27:05 -04003209 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Eric W. Biederman00978752014-07-31 03:10:50 -07003212 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003213 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003214 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003215 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003216 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003217 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003218 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003219 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003220 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3221 return 0;
3222 ctx->pos = pos = pos + 1;
3223 }
3224 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003225 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003226 for (iter = next_tgid(ns, iter);
3227 iter.task;
3228 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003229 char name[PROC_NUMBUF];
3230 int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003231
3232 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003233 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003234 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003235
Al Virof0c3b502013-05-16 12:07:31 -04003236 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3237 ctx->pos = iter.tgid + TGID_OFFSET;
3238 if (!proc_fill_cache(file, ctx, name, len,
3239 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003240 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
Al Virof0c3b502013-05-16 12:07:31 -04003244 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return 0;
3246}
3247
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003248/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003249 * proc_tid_comm_permission is a special permission function exclusively
3250 * used for the node /proc/<pid>/task/<tid>/comm.
3251 * It bypasses generic permission checks in the case where a task of the same
3252 * task group attempts to access the node.
3253 * The rationale behind this is that glibc and bionic access this node for
3254 * cross thread naming (pthread_set/getname_np(!self)). However, if
3255 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3256 * which locks out the cross thread naming implementation.
3257 * This function makes sure that the node is always accessible for members of
3258 * same thread group.
3259 */
3260static int proc_tid_comm_permission(struct inode *inode, int mask)
3261{
3262 bool is_same_tgroup;
3263 struct task_struct *task;
3264
3265 task = get_proc_task(inode);
3266 if (!task)
3267 return -ESRCH;
3268 is_same_tgroup = same_thread_group(current, task);
3269 put_task_struct(task);
3270
3271 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3272 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3273 * read or written by the members of the corresponding
3274 * thread group.
3275 */
3276 return 0;
3277 }
3278
3279 return generic_permission(inode, mask);
3280}
3281
3282static const struct inode_operations proc_tid_comm_inode_operations = {
3283 .permission = proc_tid_comm_permission,
3284};
3285
3286/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003287 * Tasks
3288 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003289static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003290 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003291 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003292 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003293#ifdef CONFIG_NET
3294 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3295#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003296 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003297 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003298 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003299 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003300 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003301#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003302 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003303#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003304 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3305 &proc_tid_comm_inode_operations,
3306 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003307#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003308 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003309#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003310 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003311 ONE("stat", S_IRUGO, proc_tid_stat),
3312 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003313 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003314#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003315 REG("children", S_IRUGO, proc_tid_children_operations),
3316#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003317#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003318 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003319#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003320 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3321 LNK("cwd", proc_cwd_link),
3322 LNK("root", proc_root_link),
3323 LNK("exe", proc_exe_link),
3324 REG("mounts", S_IRUGO, proc_mounts_operations),
3325 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003326#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003327 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003328 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003329 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003330 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003331#endif
3332#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003333 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003334#endif
3335#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003336 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003337#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003338#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003339 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003340#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303341#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003342 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003343#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003344#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003345 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003346#endif
Paul Menage8793d852007-10-18 23:39:39 -07003347#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003348 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003349#endif
Paul Menagea4243162007-10-18 23:39:35 -07003350#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003351 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003352#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003353 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003354 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003355 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003356#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003357 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003358 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003359#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003360#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003361 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003362 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003363#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003364#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003365 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003366#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003367#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003368 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003369#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003370#ifdef CONFIG_USER_NS
3371 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3372 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003373 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003374 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003375#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003376#ifdef CONFIG_LIVEPATCH
3377 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3378#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003379};
3380
Al Virof0c3b502013-05-16 12:07:31 -04003381static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003382{
Al Virof0c3b502013-05-16 12:07:31 -04003383 return proc_pident_readdir(file, ctx,
3384 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003385}
3386
Al Viro00cd8dd2012-06-10 17:13:09 -04003387static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3388{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003389 return proc_pident_lookup(dir, dentry,
3390 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003391}
3392
Arjan van de Ven00977a52007-02-12 00:55:34 -08003393static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003394 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003395 .iterate_shared = proc_tid_base_readdir,
3396 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003397};
3398
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003399static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003400 .lookup = proc_tid_base_lookup,
3401 .getattr = pid_getattr,
3402 .setattr = proc_setattr,
3403};
3404
Al Viroc52a47a2013-06-15 11:15:20 +04003405static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003406 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003407{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003408 struct inode *inode;
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003409 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003410
3411 if (!inode)
3412 goto out;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003413 inode->i_op = &proc_tid_base_inode_operations;
3414 inode->i_fop = &proc_tid_base_operations;
3415 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003416
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003417 set_nlink(inode, nlink_tid);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003418
Nick Pigginfb045ad2011-01-07 17:49:55 +11003419 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003420
3421 d_add(dentry, inode);
3422 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003423 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003424 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003425out:
Al Viroc52a47a2013-06-15 11:15:20 +04003426 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003427}
3428
Al Viro00cd8dd2012-06-10 17:13:09 -04003429static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003430{
Al Viroc52a47a2013-06-15 11:15:20 +04003431 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003432 struct task_struct *task;
3433 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003434 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003435 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436
3437 if (!leader)
3438 goto out_no_task;
3439
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003440 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003441 if (tid == ~0U)
3442 goto out;
3443
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003444 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003445 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003446 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003447 if (task)
3448 get_task_struct(task);
3449 rcu_read_unlock();
3450 if (!task)
3451 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003452 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003453 goto out_drop_task;
3454
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003455 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003456out_drop_task:
3457 put_task_struct(task);
3458out:
3459 put_task_struct(leader);
3460out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003461 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003462}
3463
3464/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003465 * Find the first tid of a thread group to return to user space.
3466 *
3467 * Usually this is just the thread group leader, but if the users
3468 * buffer was too small or there was a seek into the middle of the
3469 * directory we have more work todo.
3470 *
3471 * In the case of a short read we start with find_task_by_pid.
3472 *
3473 * In the case of a seek we start with the leader and walk nr
3474 * threads past it.
3475 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003476static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3477 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003478{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003479 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003480 unsigned long nr = f_pos;
3481
3482 if (nr != f_pos) /* 32bit overflow? */
3483 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003484
Eric W. Biedermancc288732006-06-26 00:26:01 -07003485 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003486 task = pid_task(pid, PIDTYPE_PID);
3487 if (!task)
3488 goto fail;
3489
3490 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003491 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003492 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003493 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003494 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003495 }
3496
3497 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003498 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003499 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003500
3501 /* If we haven't found our starting place yet start
3502 * with the leader and walk nr threads forward.
3503 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003504 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003505 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003506 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003507 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003508 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003509fail:
3510 pos = NULL;
3511 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003512found:
3513 get_task_struct(pos);
3514out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003515 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003516 return pos;
3517}
3518
3519/*
3520 * Find the next thread in the thread list.
3521 * Return NULL if there is an error or no next thread.
3522 *
3523 * The reference to the input task_struct is released.
3524 */
3525static struct task_struct *next_tid(struct task_struct *start)
3526{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003527 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003528 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003529 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003530 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003531 if (thread_group_leader(pos))
3532 pos = NULL;
3533 else
3534 get_task_struct(pos);
3535 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003536 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003537 put_task_struct(start);
3538 return pos;
3539}
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003542static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003544 struct inode *inode = file_inode(file);
3545 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003546 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003547 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003549 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003550 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Al Virof0c3b502013-05-16 12:07:31 -04003552 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003555 /* f_version caches the tgid value that the last readdir call couldn't
3556 * return. lseek aka telldir automagically resets f_version to 0.
3557 */
Al Viro3aa33772014-10-31 00:42:35 -04003558 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003559 tid = (int)file->f_version;
3560 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003561 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003562 task;
Al Virof0c3b502013-05-16 12:07:31 -04003563 task = next_tid(task), ctx->pos++) {
3564 char name[PROC_NUMBUF];
3565 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003566 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003567 len = snprintf(name, sizeof(name), "%d", tid);
3568 if (!proc_fill_cache(file, ctx, name, len,
3569 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003570 /* returning this tgid failed, save it as the first
3571 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003572 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003573 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003577
Al Virof0c3b502013-05-16 12:07:31 -04003578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003580
David Howellsa528d352017-01-31 16:46:22 +00003581static int proc_task_getattr(const struct path *path, struct kstat *stat,
3582 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003583{
David Howellsa528d352017-01-31 16:46:22 +00003584 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003585 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003586 generic_fillattr(inode, stat);
3587
Eric W. Biederman99f89552006-06-26 00:25:55 -07003588 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003589 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003590 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003591 }
3592
3593 return 0;
3594}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003595
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003596static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003597 .lookup = proc_task_lookup,
3598 .getattr = proc_task_getattr,
3599 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003600 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003601};
3602
Arjan van de Ven00977a52007-02-12 00:55:34 -08003603static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003604 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003605 .iterate_shared = proc_task_readdir,
3606 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003607};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003608
3609void __init set_proc_pid_nlink(void)
3610{
3611 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3612 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3613}