blob: a3efc2427c7478c9cc3f79880197f2f333a69944 [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
Alexey Dobriyanac7f1062018-02-06 15:36:59 -0800103#include "../../lib/kstrtox.h"
104
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700105/* NOTE:
106 * Implementing inode permission operations in /proc is almost
107 * certainly an error. Permission checks need to happen during
108 * each system call not at open time. The reason is that most of
109 * what we wish to check for permissions in /proc varies at runtime.
110 *
111 * The classic example of a problem is opening file descriptors
112 * in /proc for a task before it execs a suid executable.
113 */
114
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800115static u8 nlink_tid;
116static u8 nlink_tgid;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700119 const char *name;
Alexey Dobriyan623f5942016-12-12 16:45:08 -0800120 unsigned int len;
Al Virod161a132011-07-24 03:36:29 -0400121 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800122 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800123 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700124 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Eric W. Biederman61a28782006-10-02 02:18:49 -0700127#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700128 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700129 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700130 .mode = MODE, \
131 .iop = IOP, \
132 .fop = FOP, \
133 .op = OP, \
134}
135
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136#define DIR(NAME, MODE, iops, fops) \
137 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
138#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700139 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700140 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300141 { .proc_get_link = get_link } )
142#define REG(NAME, MODE, fops) \
143 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300144#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800145 NOD(NAME, (S_IFREG|(MODE)), \
146 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300147 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Vegard Nossumaed54172008-06-05 22:46:53 -0700149/*
150 * Count the number of hardlinks for the pid_entry table, excluding the .
151 * and .. links.
152 */
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800153static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
Vegard Nossumaed54172008-06-05 22:46:53 -0700154 unsigned int n)
155{
156 unsigned int i;
157 unsigned int count;
158
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800159 count = 2;
Vegard Nossumaed54172008-06-05 22:46:53 -0700160 for (i = 0; i < n; ++i) {
161 if (S_ISDIR(entries[i].mode))
162 ++count;
163 }
164
165 return count;
166}
167
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200168static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000170 int result = -ENOENT;
171
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700172 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200173 if (task->fs) {
174 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000175 result = 0;
176 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700177 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000178 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700179}
180
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800181static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700182{
David Howells2b0143b2015-03-17 22:25:59 +0000183 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700184 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700185
186 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200187 task_lock(task);
188 if (task->fs) {
189 get_fs_pwd(task->fs, path);
190 result = 0;
191 }
192 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700193 put_task_struct(task);
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return result;
196}
197
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800198static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Howells2b0143b2015-03-17 22:25:59 +0000200 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700202
203 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200204 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700205 put_task_struct(task);
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return result;
208}
209
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700210static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
211 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700213 struct task_struct *tsk;
214 struct mm_struct *mm;
215 char *page;
216 unsigned long count = _count;
217 unsigned long arg_start, arg_end, env_start, env_end;
218 unsigned long len1, len2, len;
219 unsigned long p;
220 char c;
221 ssize_t rv;
222
223 BUG_ON(*pos < 0);
224
225 tsk = get_proc_task(file_inode(file));
226 if (!tsk)
227 return -ESRCH;
228 mm = get_task_mm(tsk);
229 put_task_struct(tsk);
230 if (!mm)
231 return 0;
232 /* Check if process spawned far enough to have cmdline. */
233 if (!mm->env_end) {
234 rv = 0;
235 goto out_mmput;
236 }
237
Michal Hocko0ee931c2017-09-13 16:28:29 -0700238 page = (char *)__get_free_page(GFP_KERNEL);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700239 if (!page) {
240 rv = -ENOMEM;
241 goto out_mmput;
242 }
243
244 down_read(&mm->mmap_sem);
245 arg_start = mm->arg_start;
246 arg_end = mm->arg_end;
247 env_start = mm->env_start;
248 env_end = mm->env_end;
249 up_read(&mm->mmap_sem);
250
251 BUG_ON(arg_start > arg_end);
252 BUG_ON(env_start > env_end);
253
254 len1 = arg_end - arg_start;
255 len2 = env_end - env_start;
256
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700257 /* Empty ARGV. */
258 if (len1 == 0) {
259 rv = 0;
260 goto out_free_page;
261 }
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700262 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700263 * Inherently racy -- command line shares address space
264 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700265 */
Linus Torvalds272ddc82016-10-24 19:00:44 -0700266 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700267 if (rv <= 0)
268 goto out_free_page;
269
270 rv = 0;
271
272 if (c == '\0') {
273 /* Command line (set of strings) occupies whole ARGV. */
274 if (len1 <= *pos)
275 goto out_free_page;
276
277 p = arg_start + *pos;
278 len = len1 - *pos;
279 while (count > 0 && len > 0) {
280 unsigned int _count;
281 int nr_read;
282
283 _count = min3(count, len, PAGE_SIZE);
Linus Torvalds272ddc82016-10-24 19:00:44 -0700284 nr_read = access_remote_vm(mm, p, page, _count, 0);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700285 if (nr_read < 0)
286 rv = nr_read;
287 if (nr_read <= 0)
288 goto out_free_page;
289
290 if (copy_to_user(buf, page, nr_read)) {
291 rv = -EFAULT;
292 goto out_free_page;
293 }
294
295 p += nr_read;
296 len -= nr_read;
297 buf += nr_read;
298 count -= nr_read;
299 rv += nr_read;
300 }
301 } else {
302 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700303 * Command line (1 string) occupies ARGV and
304 * extends into ENVP.
305 */
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800306 struct {
307 unsigned long p;
308 unsigned long len;
309 } cmdline[2] = {
310 { .p = arg_start, .len = len1 },
311 { .p = env_start, .len = len2 },
312 };
313 loff_t pos1 = *pos;
314 unsigned int i;
315
316 i = 0;
317 while (i < 2 && pos1 >= cmdline[i].len) {
318 pos1 -= cmdline[i].len;
319 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700320 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800321 while (i < 2) {
322 p = cmdline[i].p + pos1;
323 len = cmdline[i].len - pos1;
324 while (count > 0 && len > 0) {
325 unsigned int _count, l;
326 int nr_read;
327 bool final;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700328
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800329 _count = min3(count, len, PAGE_SIZE);
330 nr_read = access_remote_vm(mm, p, page, _count, 0);
331 if (nr_read < 0)
332 rv = nr_read;
333 if (nr_read <= 0)
334 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700335
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800336 /*
337 * Command line can be shorter than whole ARGV
338 * even if last "marker" byte says it is not.
339 */
340 final = false;
341 l = strnlen(page, nr_read);
342 if (l < nr_read) {
343 nr_read = l;
344 final = true;
345 }
346
347 if (copy_to_user(buf, page, nr_read)) {
348 rv = -EFAULT;
349 goto out_free_page;
350 }
351
352 p += nr_read;
353 len -= nr_read;
354 buf += nr_read;
355 count -= nr_read;
356 rv += nr_read;
357
358 if (final)
359 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700360 }
361
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800362 /* Only first chunk can be read partially. */
363 pos1 = 0;
364 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700365 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700366 }
367
368out_free_page:
369 free_page((unsigned long)page);
370out_mmput:
371 mmput(mm);
372 if (rv > 0)
373 *pos += rv;
374 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700377static const struct file_operations proc_pid_cmdline_ops = {
378 .read = proc_pid_cmdline_read,
379 .llseek = generic_file_llseek,
380};
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#ifdef CONFIG_KALLSYMS
383/*
384 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
385 * Returns the resolved symbol. If that fails, simply return the address.
386 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700387static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
388 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700390 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700391 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 wchan = get_wchan(task);
394
Jann Horncaaee622016-01-20 15:00:04 -0800395 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
396 && !lookup_symbol_name(wchan, symname))
Joe Perches25ce3192015-04-15 16:18:17 -0700397 seq_printf(m, "%s", symname);
Ingo Molnarb2f73922015-09-30 15:59:17 +0200398 else
Robin Humble1e92a612016-05-09 16:28:46 -0700399 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700400
401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403#endif /* CONFIG_KALLSYMS */
404
Al Viroa9712bc2011-03-23 15:52:50 -0400405static int lock_trace(struct task_struct *task)
406{
407 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
408 if (err)
409 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800410 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400411 mutex_unlock(&task->signal->cred_guard_mutex);
412 return -EPERM;
413 }
414 return 0;
415}
416
417static void unlock_trace(struct task_struct *task)
418{
419 mutex_unlock(&task->signal->cred_guard_mutex);
420}
421
Ken Chen2ec220e2008-11-10 11:26:08 +0300422#ifdef CONFIG_STACKTRACE
423
424#define MAX_STACK_TRACE_DEPTH 64
425
426static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
427 struct pid *pid, struct task_struct *task)
428{
429 struct stack_trace trace;
430 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400431 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300432 int i;
433
434 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
435 if (!entries)
436 return -ENOMEM;
437
438 trace.nr_entries = 0;
439 trace.max_entries = MAX_STACK_TRACE_DEPTH;
440 trace.entries = entries;
441 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300442
Al Viroa9712bc2011-03-23 15:52:50 -0400443 err = lock_trace(task);
444 if (!err) {
445 save_stack_trace_tsk(task, &trace);
446
447 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800448 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400449 }
450 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300451 }
452 kfree(entries);
453
Al Viroa9712bc2011-03-23 15:52:50 -0400454 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300455}
456#endif
457
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530458#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/*
460 * Provides /proc/PID/schedstat
461 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700462static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
463 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530465 if (unlikely(!sched_info_on()))
466 seq_printf(m, "0 0 0\n");
467 else
468 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700469 (unsigned long long)task->se.sum_exec_runtime,
470 (unsigned long long)task->sched_info.run_delay,
471 task->sched_info.pcount);
472
473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475#endif
476
Arjan van de Ven97455122008-01-25 21:08:34 +0100477#ifdef CONFIG_LATENCYTOP
478static int lstats_show_proc(struct seq_file *m, void *v)
479{
480 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800481 struct inode *inode = m->private;
482 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100483
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800484 if (!task)
485 return -ESRCH;
486 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100487 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800488 struct latency_record *lr = &task->latency_record[i];
489 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100490 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800491 seq_printf(m, "%i %li %li",
492 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100493 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800494 unsigned long bt = lr->backtrace[q];
495 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100496 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800497 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100498 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800499 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100500 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800501 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100502 }
503
504 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800505 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100506 return 0;
507}
508
509static int lstats_open(struct inode *inode, struct file *file)
510{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800511 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800512}
513
Arjan van de Ven97455122008-01-25 21:08:34 +0100514static ssize_t lstats_write(struct file *file, const char __user *buf,
515 size_t count, loff_t *offs)
516{
Al Viro496ad9a2013-01-23 17:07:38 -0500517 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100518
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800519 if (!task)
520 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100521 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800522 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100523
524 return count;
525}
526
527static const struct file_operations proc_lstats_operations = {
528 .open = lstats_open,
529 .read = seq_read,
530 .write = lstats_write,
531 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800532 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100533};
534
535#endif
536
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700537static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
538 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
David Rientjesa7f638f2012-05-29 15:06:47 -0700540 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200541 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Oleg Nesterovef419392016-08-02 14:03:19 -0700543 points = oom_badness(task, NULL, NULL, totalpages) *
544 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700545 seq_printf(m, "%lu\n", points);
546
547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Neil Hormand85f50d2007-10-18 23:40:37 -0700550struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700551 const char *name;
552 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700553};
554
555static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700556 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700557 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
558 [RLIMIT_DATA] = {"Max data size", "bytes"},
559 [RLIMIT_STACK] = {"Max stack size", "bytes"},
560 [RLIMIT_CORE] = {"Max core file size", "bytes"},
561 [RLIMIT_RSS] = {"Max resident set", "bytes"},
562 [RLIMIT_NPROC] = {"Max processes", "processes"},
563 [RLIMIT_NOFILE] = {"Max open files", "files"},
564 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
565 [RLIMIT_AS] = {"Max address space", "bytes"},
566 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
567 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
568 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
569 [RLIMIT_NICE] = {"Max nice priority", NULL},
570 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800571 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700572};
573
574/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700575static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
576 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700577{
578 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700579 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700580
581 struct rlimit rlim[RLIM_NLIMITS];
582
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400583 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700584 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700585 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
586 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700587
588 /*
589 * print the file header
590 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700591 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700592 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700593
594 for (i = 0; i < RLIM_NLIMITS; i++) {
595 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700596 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700597 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700598 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700599 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700600 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700601
602 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700603 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700604 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700605 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700606
607 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700608 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700609 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700610 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700611 }
612
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700613 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700614}
615
Roland McGrathebcb6732008-07-25 19:46:00 -0700616#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700617static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
618 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700619{
620 long nr;
621 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700622 int res;
623
624 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400625 if (res)
626 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700627
628 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700629 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400630 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700631 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400632 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700633 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700634 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
635 nr,
636 args[0], args[1], args[2], args[3], args[4], args[5],
637 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400638 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700639
640 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700641}
642#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/************************************************************************/
645/* Here the fs part begins */
646/************************************************************************/
647
648/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700649static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700651 struct task_struct *task;
652 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700653 /* Allow access to a task's file descriptors if it is us or we
654 * may use ptrace attach to the process and find out that
655 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700656 */
657 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700658 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800659 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700660 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700661 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700662 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800665int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700666{
667 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000668 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700669
670 if (attr->ia_valid & ATTR_MODE)
671 return -EPERM;
672
Jan Kara31051c82016-05-26 16:55:18 +0200673 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200674 if (error)
675 return error;
676
Christoph Hellwig10257742010-06-04 11:30:02 +0200677 setattr_copy(inode, attr);
678 mark_inode_dirty(inode);
679 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700680}
681
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800682/*
683 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
684 * or euid/egid (for hide_pid_min=2)?
685 */
686static bool has_pid_permissions(struct pid_namespace *pid,
687 struct task_struct *task,
688 int hide_pid_min)
689{
690 if (pid->hide_pid < hide_pid_min)
691 return true;
692 if (in_group_p(pid->pid_gid))
693 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800694 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800695}
696
697
698static int proc_pid_permission(struct inode *inode, int mask)
699{
700 struct pid_namespace *pid = inode->i_sb->s_fs_info;
701 struct task_struct *task;
702 bool has_perms;
703
704 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800705 if (!task)
706 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800707 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800708 put_task_struct(task);
709
710 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800711 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800712 /*
713 * Let's make getdents(), stat(), and open()
714 * consistent with each other. If a process
715 * may not stat() a file, it shouldn't be seen
716 * in procfs at all.
717 */
718 return -ENOENT;
719 }
720
721 return -EPERM;
722 }
723 return generic_permission(inode, mask);
724}
725
726
727
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800728static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700729 .setattr = proc_setattr,
730};
731
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800732static int proc_single_show(struct seq_file *m, void *v)
733{
734 struct inode *inode = m->private;
735 struct pid_namespace *ns;
736 struct pid *pid;
737 struct task_struct *task;
738 int ret;
739
740 ns = inode->i_sb->s_fs_info;
741 pid = proc_pid(inode);
742 task = get_pid_task(pid, PIDTYPE_PID);
743 if (!task)
744 return -ESRCH;
745
746 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
747
748 put_task_struct(task);
749 return ret;
750}
751
752static int proc_single_open(struct inode *inode, struct file *filp)
753{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800754 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800755}
756
757static const struct file_operations proc_single_file_operations = {
758 .open = proc_single_open,
759 .read = seq_read,
760 .llseek = seq_lseek,
761 .release = single_release,
762};
763
Oleg Nesterov5381e162014-10-09 15:25:24 -0700764
765struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
766{
767 struct task_struct *task = get_proc_task(inode);
768 struct mm_struct *mm = ERR_PTR(-ESRCH);
769
770 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800771 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700772 put_task_struct(task);
773
774 if (!IS_ERR_OR_NULL(mm)) {
775 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800776 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700777 /* but do not pin its memory */
778 mmput(mm);
779 }
780 }
781
782 return mm;
783}
784
Cong Wangb409e572012-05-31 16:26:17 -0700785static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700787 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800788
789 if (IS_ERR(mm))
790 return PTR_ERR(mm);
791
Linus Torvaldse2683372012-01-17 15:21:19 -0800792 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return 0;
794}
795
Cong Wangb409e572012-05-31 16:26:17 -0700796static int mem_open(struct inode *inode, struct file *file)
797{
Djalal Harounibc452b42012-07-30 14:42:28 -0700798 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
799
800 /* OK to pass negative loff_t, we can catch out-of-range */
801 file->f_mode |= FMODE_UNSIGNED_OFFSET;
802
803 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700804}
805
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100806static ssize_t mem_rw(struct file *file, char __user *buf,
807 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Linus Torvaldse2683372012-01-17 15:21:19 -0800809 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100810 unsigned long addr = *ppos;
811 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700813 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Linus Torvaldse2683372012-01-17 15:21:19 -0800815 if (!mm)
816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Michal Hocko0ee931c2017-09-13 16:28:29 -0700818 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800820 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700822 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800823 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100824 goto free;
825
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700826 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100829 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100831 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 copied = -EFAULT;
833 break;
834 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100835
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100836 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100837 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!copied)
839 copied = -EIO;
840 break;
841 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100842
843 if (!write && copy_to_user(buf, page, this_len)) {
844 copied = -EFAULT;
845 break;
846 }
847
848 buf += this_len;
849 addr += this_len;
850 copied += this_len;
851 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100853 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700854
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100855 mmput(mm);
856free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700857 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return copied;
859}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100861static ssize_t mem_read(struct file *file, char __user *buf,
862 size_t count, loff_t *ppos)
863{
864 return mem_rw(file, buf, count, ppos, 0);
865}
866
867static ssize_t mem_write(struct file *file, const char __user *buf,
868 size_t count, loff_t *ppos)
869{
870 return mem_rw(file, (char __user*)buf, count, ppos, 1);
871}
872
Matt Mackall85863e42008-02-04 22:29:04 -0800873loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 switch (orig) {
876 case 0:
877 file->f_pos = offset;
878 break;
879 case 1:
880 file->f_pos += offset;
881 break;
882 default:
883 return -EINVAL;
884 }
885 force_successful_syscall_return();
886 return file->f_pos;
887}
888
Linus Torvaldse2683372012-01-17 15:21:19 -0800889static int mem_release(struct inode *inode, struct file *file)
890{
891 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100892 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100893 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800894 return 0;
895}
896
Arjan van de Ven00977a52007-02-12 00:55:34 -0800897static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 .llseek = mem_lseek,
899 .read = mem_read,
900 .write = mem_write,
901 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800902 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903};
904
Cong Wangb409e572012-05-31 16:26:17 -0700905static int environ_open(struct inode *inode, struct file *file)
906{
907 return __mem_open(inode, file, PTRACE_MODE_READ);
908}
909
James Pearson315e28c2007-10-16 23:30:17 -0700910static ssize_t environ_read(struct file *file, char __user *buf,
911 size_t count, loff_t *ppos)
912{
James Pearson315e28c2007-10-16 23:30:17 -0700913 char *page;
914 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700915 int ret = 0;
916 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800917 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700918
Mathias Krause8148a732016-05-05 16:22:26 -0700919 /* Ensure the process spawned far enough to have an environment. */
920 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700921 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700922
Michal Hocko0ee931c2017-09-13 16:28:29 -0700923 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700924 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700925 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700926
Al Virod6f64b82011-02-15 22:26:01 -0500927 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800928 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700929 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800930
931 down_read(&mm->mmap_sem);
932 env_start = mm->env_start;
933 env_end = mm->env_end;
934 up_read(&mm->mmap_sem);
935
James Pearson315e28c2007-10-16 23:30:17 -0700936 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700937 size_t this_len, max_len;
938 int retval;
939
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800940 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700941 break;
James Pearson315e28c2007-10-16 23:30:17 -0700942
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800943 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700944
Djalal Harounie8905ec2012-07-30 14:42:26 -0700945 max_len = min_t(size_t, PAGE_SIZE, count);
946 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700947
Linus Torvalds272ddc82016-10-24 19:00:44 -0700948 retval = access_remote_vm(mm, (env_start + src), page, this_len, 0);
James Pearson315e28c2007-10-16 23:30:17 -0700949
950 if (retval <= 0) {
951 ret = retval;
952 break;
953 }
954
955 if (copy_to_user(buf, page, retval)) {
956 ret = -EFAULT;
957 break;
958 }
959
960 ret += retval;
961 src += retval;
962 buf += retval;
963 count -= retval;
964 }
965 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700966 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700967
968free:
James Pearson315e28c2007-10-16 23:30:17 -0700969 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700970 return ret;
971}
972
973static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700974 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700975 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100976 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700977 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700978};
979
Al Viroc5317162016-10-05 18:43:43 -0400980static int auxv_open(struct inode *inode, struct file *file)
981{
982 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
983}
984
985static ssize_t auxv_read(struct file *file, char __user *buf,
986 size_t count, loff_t *ppos)
987{
988 struct mm_struct *mm = file->private_data;
989 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700990
991 if (!mm)
992 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400993 do {
994 nwords += 2;
995 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
996 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
997 nwords * sizeof(mm->saved_auxv[0]));
998}
999
1000static const struct file_operations proc_auxv_operations = {
1001 .open = auxv_open,
1002 .read = auxv_read,
1003 .llseek = generic_file_llseek,
1004 .release = mem_release,
1005};
1006
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001007static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1008 loff_t *ppos)
1009{
Al Viro496ad9a2013-01-23 17:07:38 -05001010 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001011 char buffer[PROC_NUMBUF];
1012 int oom_adj = OOM_ADJUST_MIN;
1013 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001014
1015 if (!task)
1016 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001017 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1018 oom_adj = OOM_ADJUST_MAX;
1019 else
1020 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1021 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001022 put_task_struct(task);
1023 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1024 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1025}
1026
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001027static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1028{
1029 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001030 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001031 struct task_struct *task;
1032 int err = 0;
1033
1034 task = get_proc_task(file_inode(file));
1035 if (!task)
1036 return -ESRCH;
1037
1038 mutex_lock(&oom_adj_mutex);
1039 if (legacy) {
1040 if (oom_adj < task->signal->oom_score_adj &&
1041 !capable(CAP_SYS_RESOURCE)) {
1042 err = -EACCES;
1043 goto err_unlock;
1044 }
1045 /*
1046 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1047 * /proc/pid/oom_score_adj instead.
1048 */
1049 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1050 current->comm, task_pid_nr(current), task_pid_nr(task),
1051 task_pid_nr(task));
1052 } else {
1053 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1054 !capable(CAP_SYS_RESOURCE)) {
1055 err = -EACCES;
1056 goto err_unlock;
1057 }
1058 }
1059
Michal Hocko44a70ade2016-07-28 15:44:43 -07001060 /*
1061 * Make sure we will check other processes sharing the mm if this is
1062 * not vfrok which wants its own oom_score_adj.
1063 * pin the mm so it doesn't go away and get reused after task_unlock
1064 */
1065 if (!task->vfork_done) {
1066 struct task_struct *p = find_lock_task_mm(task);
1067
1068 if (p) {
1069 if (atomic_read(&p->mm->mm_users) > 1) {
1070 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001071 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001072 }
1073 task_unlock(p);
1074 }
1075 }
1076
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001077 task->signal->oom_score_adj = oom_adj;
1078 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1079 task->signal->oom_score_adj_min = (short)oom_adj;
1080 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001081
1082 if (mm) {
1083 struct task_struct *p;
1084
1085 rcu_read_lock();
1086 for_each_process(p) {
1087 if (same_thread_group(task, p))
1088 continue;
1089
1090 /* do not touch kernel threads or the global init */
1091 if (p->flags & PF_KTHREAD || is_global_init(p))
1092 continue;
1093
1094 task_lock(p);
1095 if (!p->vfork_done && process_shares_mm(p, mm)) {
1096 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",
1097 task_pid_nr(p), p->comm,
1098 p->signal->oom_score_adj, oom_adj,
1099 task_pid_nr(task), task->comm);
1100 p->signal->oom_score_adj = oom_adj;
1101 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1102 p->signal->oom_score_adj_min = (short)oom_adj;
1103 }
1104 task_unlock(p);
1105 }
1106 rcu_read_unlock();
1107 mmdrop(mm);
1108 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001109err_unlock:
1110 mutex_unlock(&oom_adj_mutex);
1111 put_task_struct(task);
1112 return err;
1113}
Michal Hockof913da52016-07-28 15:44:37 -07001114
David Rientjesb72bdfa2015-11-05 18:50:32 -08001115/*
1116 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1117 * kernels. The effective policy is defined by oom_score_adj, which has a
1118 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1119 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1120 * Processes that become oom disabled via oom_adj will still be oom disabled
1121 * with this implementation.
1122 *
1123 * oom_adj cannot be removed since existing userspace binaries use it.
1124 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001125static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1126 size_t count, loff_t *ppos)
1127{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001128 char buffer[PROC_NUMBUF];
1129 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001130 int err;
1131
1132 memset(buffer, 0, sizeof(buffer));
1133 if (count > sizeof(buffer) - 1)
1134 count = sizeof(buffer) - 1;
1135 if (copy_from_user(buffer, buf, count)) {
1136 err = -EFAULT;
1137 goto out;
1138 }
1139
1140 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1141 if (err)
1142 goto out;
1143 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1144 oom_adj != OOM_DISABLE) {
1145 err = -EINVAL;
1146 goto out;
1147 }
1148
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001149 /*
1150 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1151 * value is always attainable.
1152 */
1153 if (oom_adj == OOM_ADJUST_MAX)
1154 oom_adj = OOM_SCORE_ADJ_MAX;
1155 else
1156 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1157
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001158 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001159out:
1160 return err < 0 ? err : count;
1161}
1162
1163static const struct file_operations proc_oom_adj_operations = {
1164 .read = oom_adj_read,
1165 .write = oom_adj_write,
1166 .llseek = generic_file_llseek,
1167};
1168
David Rientjesa63d83f2010-08-09 17:19:46 -07001169static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1170 size_t count, loff_t *ppos)
1171{
Al Viro496ad9a2013-01-23 17:07:38 -05001172 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001173 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001174 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001175 size_t len;
1176
1177 if (!task)
1178 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001179 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001180 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001181 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001182 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1183}
1184
1185static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1186 size_t count, loff_t *ppos)
1187{
David Rientjesa63d83f2010-08-09 17:19:46 -07001188 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001189 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001190 int err;
1191
1192 memset(buffer, 0, sizeof(buffer));
1193 if (count > sizeof(buffer) - 1)
1194 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001195 if (copy_from_user(buffer, buf, count)) {
1196 err = -EFAULT;
1197 goto out;
1198 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001199
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001200 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001201 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001202 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001203 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001204 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1205 err = -EINVAL;
1206 goto out;
1207 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001208
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001209 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001210out:
1211 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001212}
1213
1214static const struct file_operations proc_oom_score_adj_operations = {
1215 .read = oom_score_adj_read,
1216 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001217 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001218};
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001221#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1223 size_t count, loff_t *ppos)
1224{
Al Viro496ad9a2013-01-23 17:07:38 -05001225 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001226 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 ssize_t length;
1228 char tmpbuf[TMPBUFLEN];
1229
Eric W. Biederman99f89552006-06-26 00:25:55 -07001230 if (!task)
1231 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001233 from_kuid(file->f_cred->user_ns,
1234 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001235 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1237}
1238
1239static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1240 size_t count, loff_t *ppos)
1241{
Al Viro496ad9a2013-01-23 17:07:38 -05001242 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001244 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001245 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001247 rcu_read_lock();
1248 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1249 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001251 }
1252 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (*ppos != 0) {
1255 /* No partial writes. */
1256 return -EINVAL;
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001259 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1260 if (rv < 0)
1261 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001262
1263 /* is userspace tring to explicitly UNSET the loginuid? */
1264 if (loginuid == AUDIT_UID_UNSET) {
1265 kloginuid = INVALID_UID;
1266 } else {
1267 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001268 if (!uid_valid(kloginuid))
1269 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001270 }
1271
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001272 rv = audit_set_loginuid(kloginuid);
1273 if (rv < 0)
1274 return rv;
1275 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Arjan van de Ven00977a52007-02-12 00:55:34 -08001278static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 .read = proc_loginuid_read,
1280 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001281 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282};
Eric Paris1e0bd752008-03-13 08:15:31 -04001283
1284static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1285 size_t count, loff_t *ppos)
1286{
Al Viro496ad9a2013-01-23 17:07:38 -05001287 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001288 struct task_struct *task = get_proc_task(inode);
1289 ssize_t length;
1290 char tmpbuf[TMPBUFLEN];
1291
1292 if (!task)
1293 return -ESRCH;
1294 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1295 audit_get_sessionid(task));
1296 put_task_struct(task);
1297 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1298}
1299
1300static const struct file_operations proc_sessionid_operations = {
1301 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001302 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001303};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304#endif
1305
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001306#ifdef CONFIG_FAULT_INJECTION
1307static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1308 size_t count, loff_t *ppos)
1309{
Al Viro496ad9a2013-01-23 17:07:38 -05001310 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001311 char buffer[PROC_NUMBUF];
1312 size_t len;
1313 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001314
1315 if (!task)
1316 return -ESRCH;
1317 make_it_fail = task->make_it_fail;
1318 put_task_struct(task);
1319
1320 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001321
1322 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001323}
1324
1325static ssize_t proc_fault_inject_write(struct file * file,
1326 const char __user * buf, size_t count, loff_t *ppos)
1327{
1328 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001329 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001330 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001331 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001332
1333 if (!capable(CAP_SYS_RESOURCE))
1334 return -EPERM;
1335 memset(buffer, 0, sizeof(buffer));
1336 if (count > sizeof(buffer) - 1)
1337 count = sizeof(buffer) - 1;
1338 if (copy_from_user(buffer, buf, count))
1339 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001340 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1341 if (rv < 0)
1342 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001343 if (make_it_fail < 0 || make_it_fail > 1)
1344 return -EINVAL;
1345
Al Viro496ad9a2013-01-23 17:07:38 -05001346 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347 if (!task)
1348 return -ESRCH;
1349 task->make_it_fail = make_it_fail;
1350 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001351
1352 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001353}
1354
Arjan van de Ven00977a52007-02-12 00:55:34 -08001355static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001356 .read = proc_fault_inject_read,
1357 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001358 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001359};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001360
1361static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1362 size_t count, loff_t *ppos)
1363{
1364 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001365 int err;
1366 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001367
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001368 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001369 if (err)
1370 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001371
1372 task = get_proc_task(file_inode(file));
1373 if (!task)
1374 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001375 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001376 put_task_struct(task);
1377
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001378 return count;
1379}
1380
1381static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1382 size_t count, loff_t *ppos)
1383{
1384 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001385 char numbuf[PROC_NUMBUF];
1386 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001387
1388 task = get_proc_task(file_inode(file));
1389 if (!task)
1390 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001391 len = snprintf(numbuf, sizeof(numbuf), "%u\n", 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{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001911 const char *str = dentry->d_name.name;
1912 unsigned long long sval, eval;
1913 unsigned int len;
1914
1915 len = _parse_integer(str, 16, &sval);
1916 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001917 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001918 if (sval != (unsigned long)sval)
1919 return -EINVAL;
1920 str += len;
1921
1922 if (*str != '-')
1923 return -EINVAL;
1924 str++;
1925
1926 len = _parse_integer(str, 16, &eval);
1927 if (len & KSTRTOX_OVERFLOW)
1928 return -EINVAL;
1929 if (eval != (unsigned long)eval)
1930 return -EINVAL;
1931 str += len;
1932
1933 if (*str != '\0')
1934 return -EINVAL;
1935
1936 *start = sval;
1937 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001938
1939 return 0;
1940}
1941
Al Viro0b728e12012-06-10 16:03:43 -04001942static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001943{
1944 unsigned long vm_start, vm_end;
1945 bool exact_vma_exists = false;
1946 struct mm_struct *mm = NULL;
1947 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001948 struct inode *inode;
1949 int status = 0;
1950
Al Viro0b728e12012-06-10 16:03:43 -04001951 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001952 return -ECHILD;
1953
David Howells2b0143b2015-03-17 22:25:59 +00001954 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001955 task = get_proc_task(inode);
1956 if (!task)
1957 goto out_notask;
1958
Jann Horncaaee622016-01-20 15:00:04 -08001959 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001960 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001961 goto out;
1962
1963 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1964 down_read(&mm->mmap_sem);
1965 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1966 up_read(&mm->mmap_sem);
1967 }
1968
1969 mmput(mm);
1970
1971 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001972 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1973
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001974 security_task_to_inode(task, inode);
1975 status = 1;
1976 }
1977
1978out:
1979 put_task_struct(task);
1980
1981out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001982 return status;
1983}
1984
1985static const struct dentry_operations tid_map_files_dentry_operations = {
1986 .d_revalidate = map_files_d_revalidate,
1987 .d_delete = pid_delete_dentry,
1988};
1989
Al Viro6b255392015-11-17 10:20:54 -05001990static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001991{
1992 unsigned long vm_start, vm_end;
1993 struct vm_area_struct *vma;
1994 struct task_struct *task;
1995 struct mm_struct *mm;
1996 int rc;
1997
1998 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001999 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002000 if (!task)
2001 goto out;
2002
2003 mm = get_task_mm(task);
2004 put_task_struct(task);
2005 if (!mm)
2006 goto out;
2007
2008 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2009 if (rc)
2010 goto out_mmput;
2011
Artem Fetishev70335ab2014-03-10 15:49:45 -07002012 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002013 down_read(&mm->mmap_sem);
2014 vma = find_exact_vma(mm, vm_start, vm_end);
2015 if (vma && vma->vm_file) {
2016 *path = vma->vm_file->f_path;
2017 path_get(path);
2018 rc = 0;
2019 }
2020 up_read(&mm->mmap_sem);
2021
2022out_mmput:
2023 mmput(mm);
2024out:
2025 return rc;
2026}
2027
2028struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002029 unsigned long start;
2030 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002031 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002032};
2033
Calvin Owensbdb4d102015-09-09 15:35:54 -07002034/*
2035 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2036 * symlinks may be used to bypass permissions on ancestor directories in the
2037 * path to the file in question.
2038 */
2039static const char *
Al Viro6b255392015-11-17 10:20:54 -05002040proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002041 struct inode *inode,
2042 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002043{
2044 if (!capable(CAP_SYS_ADMIN))
2045 return ERR_PTR(-EPERM);
2046
Al Virofceef392015-12-29 15:58:39 -05002047 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002048}
2049
2050/*
Al Viro6b255392015-11-17 10:20:54 -05002051 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002052 */
2053static const struct inode_operations proc_map_files_link_inode_operations = {
2054 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002055 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002056 .setattr = proc_setattr,
2057};
2058
Al Viroc52a47a2013-06-15 11:15:20 +04002059static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002060proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2061 struct task_struct *task, const void *ptr)
2062{
Al Viro7b540d02012-08-27 14:55:26 -04002063 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002064 struct proc_inode *ei;
2065 struct inode *inode;
2066
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002067 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2068 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2069 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002070 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002071 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072
2073 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002074 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002075
Calvin Owensbdb4d102015-09-09 15:35:54 -07002076 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002078
2079 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2080 d_add(dentry, inode);
2081
Al Viroc52a47a2013-06-15 11:15:20 +04002082 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002083}
2084
2085static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002086 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002087{
2088 unsigned long vm_start, vm_end;
2089 struct vm_area_struct *vma;
2090 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002091 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002092 struct mm_struct *mm;
2093
Al Viroc52a47a2013-06-15 11:15:20 +04002094 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002095 task = get_proc_task(dir);
2096 if (!task)
2097 goto out;
2098
Al Viroc52a47a2013-06-15 11:15:20 +04002099 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002100 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002101 goto out_put_task;
2102
Al Viroc52a47a2013-06-15 11:15:20 +04002103 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002104 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002105 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002106
2107 mm = get_task_mm(task);
2108 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002109 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002110
2111 down_read(&mm->mmap_sem);
2112 vma = find_exact_vma(mm, vm_start, vm_end);
2113 if (!vma)
2114 goto out_no_vma;
2115
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002116 if (vma->vm_file)
2117 result = proc_map_files_instantiate(dir, dentry, task,
2118 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002119
2120out_no_vma:
2121 up_read(&mm->mmap_sem);
2122 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002123out_put_task:
2124 put_task_struct(task);
2125out:
Al Viroc52a47a2013-06-15 11:15:20 +04002126 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002127}
2128
2129static const struct inode_operations proc_map_files_inode_operations = {
2130 .lookup = proc_map_files_lookup,
2131 .permission = proc_fd_permission,
2132 .setattr = proc_setattr,
2133};
2134
2135static int
Al Virof0c3b502013-05-16 12:07:31 -04002136proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002137{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002138 struct vm_area_struct *vma;
2139 struct task_struct *task;
2140 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002141 unsigned long nr_files, pos, i;
2142 struct flex_array *fa = NULL;
2143 struct map_files_info info;
2144 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002145 int ret;
2146
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002147 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002148 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002149 if (!task)
2150 goto out;
2151
2152 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002153 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002154 goto out_put_task;
2155
2156 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002157 if (!dir_emit_dots(file, ctx))
2158 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002159
Al Virof0c3b502013-05-16 12:07:31 -04002160 mm = get_task_mm(task);
2161 if (!mm)
2162 goto out_put_task;
2163 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002164
Al Virof0c3b502013-05-16 12:07:31 -04002165 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002166
Al Virof0c3b502013-05-16 12:07:31 -04002167 /*
2168 * We need two passes here:
2169 *
2170 * 1) Collect vmas of mapped files with mmap_sem taken
2171 * 2) Release mmap_sem and instantiate entries
2172 *
2173 * otherwise we get lockdep complained, since filldir()
2174 * routine might require mmap_sem taken in might_fault().
2175 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002176
Al Virof0c3b502013-05-16 12:07:31 -04002177 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2178 if (vma->vm_file && ++pos > ctx->pos)
2179 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002180 }
Al Virof0c3b502013-05-16 12:07:31 -04002181
2182 if (nr_files) {
2183 fa = flex_array_alloc(sizeof(info), nr_files,
2184 GFP_KERNEL);
2185 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2186 GFP_KERNEL)) {
2187 ret = -ENOMEM;
2188 if (fa)
2189 flex_array_free(fa);
2190 up_read(&mm->mmap_sem);
2191 mmput(mm);
2192 goto out_put_task;
2193 }
2194 for (i = 0, vma = mm->mmap, pos = 2; vma;
2195 vma = vma->vm_next) {
2196 if (!vma->vm_file)
2197 continue;
2198 if (++pos <= ctx->pos)
2199 continue;
2200
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002201 info.start = vma->vm_start;
2202 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002203 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002204 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2205 BUG();
2206 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002207 }
Al Virof0c3b502013-05-16 12:07:31 -04002208 up_read(&mm->mmap_sem);
2209
2210 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002211 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2212 unsigned int len;
2213
Al Virof0c3b502013-05-16 12:07:31 -04002214 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002215 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002216 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002217 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002218 proc_map_files_instantiate,
2219 task,
2220 (void *)(unsigned long)p->mode))
2221 break;
2222 ctx->pos++;
2223 }
2224 if (fa)
2225 flex_array_free(fa);
2226 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002227
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002228out_put_task:
2229 put_task_struct(task);
2230out:
2231 return ret;
2232}
2233
2234static const struct file_operations proc_map_files_operations = {
2235 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002236 .iterate_shared = proc_map_files_readdir,
2237 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002238};
2239
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002240#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002241struct timers_private {
2242 struct pid *pid;
2243 struct task_struct *task;
2244 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002245 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002246 unsigned long flags;
2247};
2248
2249static void *timers_start(struct seq_file *m, loff_t *pos)
2250{
2251 struct timers_private *tp = m->private;
2252
2253 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2254 if (!tp->task)
2255 return ERR_PTR(-ESRCH);
2256
2257 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2258 if (!tp->sighand)
2259 return ERR_PTR(-ESRCH);
2260
2261 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2262}
2263
2264static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2265{
2266 struct timers_private *tp = m->private;
2267 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2268}
2269
2270static void timers_stop(struct seq_file *m, void *v)
2271{
2272 struct timers_private *tp = m->private;
2273
2274 if (tp->sighand) {
2275 unlock_task_sighand(tp->task, &tp->flags);
2276 tp->sighand = NULL;
2277 }
2278
2279 if (tp->task) {
2280 put_task_struct(tp->task);
2281 tp->task = NULL;
2282 }
2283}
2284
2285static int show_timer(struct seq_file *m, void *v)
2286{
2287 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002288 struct timers_private *tp = m->private;
2289 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002290 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002291 [SIGEV_SIGNAL] = "signal",
2292 [SIGEV_NONE] = "none",
2293 [SIGEV_THREAD] = "thread",
2294 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002295
2296 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002297 notify = timer->it_sigev_notify;
2298
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002299 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002300 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002301 timer->sigq->info.si_signo,
2302 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002303 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002304 nstr[notify & ~SIGEV_THREAD_ID],
2305 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2306 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002307 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002308
2309 return 0;
2310}
2311
2312static const struct seq_operations proc_timers_seq_ops = {
2313 .start = timers_start,
2314 .next = timers_next,
2315 .stop = timers_stop,
2316 .show = show_timer,
2317};
2318
2319static int proc_timers_open(struct inode *inode, struct file *file)
2320{
2321 struct timers_private *tp;
2322
2323 tp = __seq_open_private(file, &proc_timers_seq_ops,
2324 sizeof(struct timers_private));
2325 if (!tp)
2326 return -ENOMEM;
2327
2328 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002329 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002330 return 0;
2331}
2332
2333static const struct file_operations proc_timers_operations = {
2334 .open = proc_timers_open,
2335 .read = seq_read,
2336 .llseek = seq_lseek,
2337 .release = seq_release_private,
2338};
Eric Engestromb5946be2016-03-17 14:20:57 -07002339#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002340
John Stultz5de23d42016-03-17 14:20:54 -07002341static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2342 size_t count, loff_t *offset)
2343{
2344 struct inode *inode = file_inode(file);
2345 struct task_struct *p;
2346 u64 slack_ns;
2347 int err;
2348
2349 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2350 if (err < 0)
2351 return err;
2352
2353 p = get_proc_task(inode);
2354 if (!p)
2355 return -ESRCH;
2356
John Stultz4b2bd5f2016-10-07 17:02:33 -07002357 if (p != current) {
2358 if (!capable(CAP_SYS_NICE)) {
2359 count = -EPERM;
2360 goto out;
2361 }
John Stultz5de23d42016-03-17 14:20:54 -07002362
John Stultz4b2bd5f2016-10-07 17:02:33 -07002363 err = security_task_setscheduler(p);
2364 if (err) {
2365 count = err;
2366 goto out;
2367 }
John Stultz904763e2016-10-07 17:02:29 -07002368 }
2369
John Stultz7abbaf92016-10-07 17:02:26 -07002370 task_lock(p);
2371 if (slack_ns == 0)
2372 p->timer_slack_ns = p->default_timer_slack_ns;
2373 else
2374 p->timer_slack_ns = slack_ns;
2375 task_unlock(p);
2376
2377out:
John Stultz5de23d42016-03-17 14:20:54 -07002378 put_task_struct(p);
2379
2380 return count;
2381}
2382
2383static int timerslack_ns_show(struct seq_file *m, void *v)
2384{
2385 struct inode *inode = m->private;
2386 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002387 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002388
2389 p = get_proc_task(inode);
2390 if (!p)
2391 return -ESRCH;
2392
John Stultz4b2bd5f2016-10-07 17:02:33 -07002393 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002394
John Stultz4b2bd5f2016-10-07 17:02:33 -07002395 if (!capable(CAP_SYS_NICE)) {
2396 err = -EPERM;
2397 goto out;
2398 }
2399 err = security_task_getscheduler(p);
2400 if (err)
2401 goto out;
2402 }
John Stultz904763e2016-10-07 17:02:29 -07002403
John Stultz7abbaf92016-10-07 17:02:26 -07002404 task_lock(p);
2405 seq_printf(m, "%llu\n", p->timer_slack_ns);
2406 task_unlock(p);
2407
2408out:
John Stultz5de23d42016-03-17 14:20:54 -07002409 put_task_struct(p);
2410
2411 return err;
2412}
2413
2414static int timerslack_ns_open(struct inode *inode, struct file *filp)
2415{
2416 return single_open(filp, timerslack_ns_show, inode);
2417}
2418
2419static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2420 .open = timerslack_ns_open,
2421 .read = seq_read,
2422 .write = timerslack_ns_write,
2423 .llseek = seq_lseek,
2424 .release = single_release,
2425};
2426
Al Viroc52a47a2013-06-15 11:15:20 +04002427static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002428 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002429{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002430 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002431 struct inode *inode;
2432 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002433
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002434 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002435 if (!inode)
2436 goto out;
2437
2438 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002439 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002440 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002441 if (p->iop)
2442 inode->i_op = p->iop;
2443 if (p->fop)
2444 inode->i_fop = p->fop;
2445 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002446 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002447 d_add(dentry, inode);
2448 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002449 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002450 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002451out:
Al Viroc52a47a2013-06-15 11:15:20 +04002452 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002453}
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455static struct dentry *proc_pident_lookup(struct inode *dir,
2456 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002457 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002458 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Al Viroc52a47a2013-06-15 11:15:20 +04002460 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002461 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002462 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Al Viroc52a47a2013-06-15 11:15:20 +04002464 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Eric W. Biederman99f89552006-06-26 00:25:55 -07002466 if (!task)
2467 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002469 /*
2470 * Yes, it does not scale. And it should not. Don't add
2471 * new entries into /proc/<tgid>/ without very good reasons.
2472 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002473 last = &ents[nents];
2474 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (p->len != dentry->d_name.len)
2476 continue;
2477 if (!memcmp(dentry->d_name.name, p->name, p->len))
2478 break;
2479 }
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002480 if (p >= last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 goto out;
2482
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002483 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002485 put_task_struct(task);
2486out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002487 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489
Al Virof0c3b502013-05-16 12:07:31 -04002490static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002491 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002492{
Al Virof0c3b502013-05-16 12:07:31 -04002493 struct task_struct *task = get_proc_task(file_inode(file));
2494 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002495
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002497 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002498
Al Virof0c3b502013-05-16 12:07:31 -04002499 if (!dir_emit_dots(file, ctx))
2500 goto out;
2501
2502 if (ctx->pos >= nents + 2)
2503 goto out;
2504
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002505 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002506 if (!proc_fill_cache(file, ctx, p->name, p->len,
2507 proc_pident_instantiate, task, p))
2508 break;
2509 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002510 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002512 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002517static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2518 size_t count, loff_t *ppos)
2519{
Al Viro496ad9a2013-01-23 17:07:38 -05002520 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002521 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522 ssize_t length;
2523 struct task_struct *task = get_proc_task(inode);
2524
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002525 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002526 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002527
2528 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002529 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002530 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002532 if (length > 0)
2533 length = simple_read_from_buffer(buf, count, ppos, p, length);
2534 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002535 return length;
2536}
2537
2538static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2539 size_t count, loff_t *ppos)
2540{
Al Viro496ad9a2013-01-23 17:07:38 -05002541 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002542 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543 ssize_t length;
2544 struct task_struct *task = get_proc_task(inode);
2545
2546 length = -ESRCH;
2547 if (!task)
2548 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002549
2550 /* A task may only write its own attributes. */
2551 length = -EACCES;
2552 if (current != task)
2553 goto out;
2554
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555 if (count > PAGE_SIZE)
2556 count = PAGE_SIZE;
2557
2558 /* No partial writes. */
2559 length = -EINVAL;
2560 if (*ppos != 0)
2561 goto out;
2562
Al Virobb646cd2015-12-24 00:16:30 -05002563 page = memdup_user(buf, count);
2564 if (IS_ERR(page)) {
2565 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002566 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002567 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002568
David Howells107db7c2009-05-08 13:55:27 +01002569 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002570 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002571 if (length < 0)
2572 goto out_free;
2573
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002574 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002575 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002576 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002577out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002578 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002579out:
2580 put_task_struct(task);
2581out_no_task:
2582 return length;
2583}
2584
Arjan van de Ven00977a52007-02-12 00:55:34 -08002585static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002586 .read = proc_pid_attr_read,
2587 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002588 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002589};
2590
Eric Dumazetc5141e62007-05-08 00:26:15 -07002591static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002592 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2593 REG("prev", S_IRUGO, proc_pid_attr_operations),
2594 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2595 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2596 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2597 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002598};
2599
Al Virof0c3b502013-05-16 12:07:31 -04002600static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Al Virof0c3b502013-05-16 12:07:31 -04002602 return proc_pident_readdir(file, ctx,
2603 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Arjan van de Ven00977a52007-02-12 00:55:34 -08002606static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002608 .iterate_shared = proc_attr_dir_readdir,
2609 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610};
2611
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002612static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002613 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002615 return proc_pident_lookup(dir, dentry,
2616 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002619static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002620 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002621 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002622 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623};
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625#endif
2626
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002627#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002628static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2629 size_t count, loff_t *ppos)
2630{
Al Viro496ad9a2013-01-23 17:07:38 -05002631 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002632 struct mm_struct *mm;
2633 char buffer[PROC_NUMBUF];
2634 size_t len;
2635 int ret;
2636
2637 if (!task)
2638 return -ESRCH;
2639
2640 ret = 0;
2641 mm = get_task_mm(task);
2642 if (mm) {
2643 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2644 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2645 MMF_DUMP_FILTER_SHIFT));
2646 mmput(mm);
2647 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2648 }
2649
2650 put_task_struct(task);
2651
2652 return ret;
2653}
2654
2655static ssize_t proc_coredump_filter_write(struct file *file,
2656 const char __user *buf,
2657 size_t count,
2658 loff_t *ppos)
2659{
2660 struct task_struct *task;
2661 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002662 unsigned int val;
2663 int ret;
2664 int i;
2665 unsigned long mask;
2666
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002667 ret = kstrtouint_from_user(buf, count, 0, &val);
2668 if (ret < 0)
2669 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002670
2671 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002672 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002673 if (!task)
2674 goto out_no_task;
2675
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002676 mm = get_task_mm(task);
2677 if (!mm)
2678 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002679 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002680
2681 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2682 if (val & mask)
2683 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2684 else
2685 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2686 }
2687
2688 mmput(mm);
2689 out_no_mm:
2690 put_task_struct(task);
2691 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002692 if (ret < 0)
2693 return ret;
2694 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002695}
2696
2697static const struct file_operations proc_coredump_filter_operations = {
2698 .read = proc_coredump_filter_read,
2699 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002700 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002701};
2702#endif
2703
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002704#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002705static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002706{
Andrea Righi940389b2008-07-28 00:48:12 +02002707 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002708 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002709 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002710
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002711 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2712 if (result)
2713 return result;
2714
Jann Horncaaee622016-01-20 15:00:04 -08002715 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002716 result = -EACCES;
2717 goto out_unlock;
2718 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002719
Andrea Righi59954772008-07-27 17:29:15 +02002720 if (whole && lock_task_sighand(task, &flags)) {
2721 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002722
Andrea Righi59954772008-07-27 17:29:15 +02002723 task_io_accounting_add(&acct, &task->signal->ioac);
2724 while_each_thread(task, t)
2725 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002726
Andrea Righi59954772008-07-27 17:29:15 +02002727 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002728 }
Joe Perches25ce3192015-04-15 16:18:17 -07002729 seq_printf(m,
2730 "rchar: %llu\n"
2731 "wchar: %llu\n"
2732 "syscr: %llu\n"
2733 "syscw: %llu\n"
2734 "read_bytes: %llu\n"
2735 "write_bytes: %llu\n"
2736 "cancelled_write_bytes: %llu\n",
2737 (unsigned long long)acct.rchar,
2738 (unsigned long long)acct.wchar,
2739 (unsigned long long)acct.syscr,
2740 (unsigned long long)acct.syscw,
2741 (unsigned long long)acct.read_bytes,
2742 (unsigned long long)acct.write_bytes,
2743 (unsigned long long)acct.cancelled_write_bytes);
2744 result = 0;
2745
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002746out_unlock:
2747 mutex_unlock(&task->signal->cred_guard_mutex);
2748 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002749}
Andrea Righi297c5d92008-07-25 01:48:49 -07002750
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002751static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2752 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002753{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002754 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002755}
2756
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002757static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2758 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002759{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002760 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002761}
2762#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002763
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002764#ifdef CONFIG_USER_NS
2765static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002766 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002767{
2768 struct user_namespace *ns = NULL;
2769 struct task_struct *task;
2770 struct seq_file *seq;
2771 int ret = -EINVAL;
2772
2773 task = get_proc_task(inode);
2774 if (task) {
2775 rcu_read_lock();
2776 ns = get_user_ns(task_cred_xxx(task, user_ns));
2777 rcu_read_unlock();
2778 put_task_struct(task);
2779 }
2780 if (!ns)
2781 goto err;
2782
2783 ret = seq_open(file, seq_ops);
2784 if (ret)
2785 goto err_put_ns;
2786
2787 seq = file->private_data;
2788 seq->private = ns;
2789
2790 return 0;
2791err_put_ns:
2792 put_user_ns(ns);
2793err:
2794 return ret;
2795}
2796
2797static int proc_id_map_release(struct inode *inode, struct file *file)
2798{
2799 struct seq_file *seq = file->private_data;
2800 struct user_namespace *ns = seq->private;
2801 put_user_ns(ns);
2802 return seq_release(inode, file);
2803}
2804
2805static int proc_uid_map_open(struct inode *inode, struct file *file)
2806{
2807 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2808}
2809
2810static int proc_gid_map_open(struct inode *inode, struct file *file)
2811{
2812 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2813}
2814
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002815static int proc_projid_map_open(struct inode *inode, struct file *file)
2816{
2817 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2818}
2819
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002820static const struct file_operations proc_uid_map_operations = {
2821 .open = proc_uid_map_open,
2822 .write = proc_uid_map_write,
2823 .read = seq_read,
2824 .llseek = seq_lseek,
2825 .release = proc_id_map_release,
2826};
2827
2828static const struct file_operations proc_gid_map_operations = {
2829 .open = proc_gid_map_open,
2830 .write = proc_gid_map_write,
2831 .read = seq_read,
2832 .llseek = seq_lseek,
2833 .release = proc_id_map_release,
2834};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002835
2836static const struct file_operations proc_projid_map_operations = {
2837 .open = proc_projid_map_open,
2838 .write = proc_projid_map_write,
2839 .read = seq_read,
2840 .llseek = seq_lseek,
2841 .release = proc_id_map_release,
2842};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002843
2844static int proc_setgroups_open(struct inode *inode, struct file *file)
2845{
2846 struct user_namespace *ns = NULL;
2847 struct task_struct *task;
2848 int ret;
2849
2850 ret = -ESRCH;
2851 task = get_proc_task(inode);
2852 if (task) {
2853 rcu_read_lock();
2854 ns = get_user_ns(task_cred_xxx(task, user_ns));
2855 rcu_read_unlock();
2856 put_task_struct(task);
2857 }
2858 if (!ns)
2859 goto err;
2860
2861 if (file->f_mode & FMODE_WRITE) {
2862 ret = -EACCES;
2863 if (!ns_capable(ns, CAP_SYS_ADMIN))
2864 goto err_put_ns;
2865 }
2866
2867 ret = single_open(file, &proc_setgroups_show, ns);
2868 if (ret)
2869 goto err_put_ns;
2870
2871 return 0;
2872err_put_ns:
2873 put_user_ns(ns);
2874err:
2875 return ret;
2876}
2877
2878static int proc_setgroups_release(struct inode *inode, struct file *file)
2879{
2880 struct seq_file *seq = file->private_data;
2881 struct user_namespace *ns = seq->private;
2882 int ret = single_release(inode, file);
2883 put_user_ns(ns);
2884 return ret;
2885}
2886
2887static const struct file_operations proc_setgroups_operations = {
2888 .open = proc_setgroups_open,
2889 .write = proc_setgroups_write,
2890 .read = seq_read,
2891 .llseek = seq_lseek,
2892 .release = proc_setgroups_release,
2893};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002894#endif /* CONFIG_USER_NS */
2895
Kees Cook47830722008-10-06 03:11:58 +04002896static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2897 struct pid *pid, struct task_struct *task)
2898{
Al Viroa9712bc2011-03-23 15:52:50 -04002899 int err = lock_trace(task);
2900 if (!err) {
2901 seq_printf(m, "%08x\n", task->personality);
2902 unlock_trace(task);
2903 }
2904 return err;
Kees Cook47830722008-10-06 03:11:58 +04002905}
2906
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002907#ifdef CONFIG_LIVEPATCH
2908static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2909 struct pid *pid, struct task_struct *task)
2910{
2911 seq_printf(m, "%d\n", task->patch_state);
2912 return 0;
2913}
2914#endif /* CONFIG_LIVEPATCH */
2915
Eric W. Biederman801199c2006-10-02 02:18:48 -07002916/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917 * Thread groups
2918 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002919static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002920static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002921
Eric Dumazetc5141e62007-05-08 00:26:15 -07002922static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2924 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002925 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002926 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002927 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002928#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002929 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002930#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002931 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002932 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002933 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002934 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002935 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002936#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002937 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002938#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002939#ifdef CONFIG_SCHED_AUTOGROUP
2940 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2941#endif
john stultz4614a696b2009-12-14 18:00:05 -08002942 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002943#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002944 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002945#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002946 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 ONE("stat", S_IRUGO, proc_tgid_stat),
2948 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002949 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002950#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002951 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002952#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002953 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2954 LNK("cwd", proc_cwd_link),
2955 LNK("root", proc_root_link),
2956 LNK("exe", proc_exe_link),
2957 REG("mounts", S_IRUGO, proc_mounts_operations),
2958 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2959 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002960#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002961 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002962 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002963 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002964 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002965#endif
2966#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002967 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002968#endif
2969#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002970 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002971#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002972#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002973 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002974#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302975#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002976 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002977#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002978#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002979 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002980#endif
Paul Menage8793d852007-10-18 23:39:39 -07002981#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002982 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002983#endif
Paul Menagea4243162007-10-18 23:39:35 -07002984#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002985 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002986#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002987 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002988 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002989 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002991 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2992 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002993#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002994#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002995 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002996 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002997#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002998#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002999 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003000#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003001#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003002 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003003#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003004#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003005 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003006#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003007#ifdef CONFIG_USER_NS
3008 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3009 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003010 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003011 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003012#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003013#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003014 REG("timers", S_IRUGO, proc_timers_operations),
3015#endif
John Stultz5de23d42016-03-17 14:20:54 -07003016 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003017#ifdef CONFIG_LIVEPATCH
3018 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3019#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003020};
3021
Al Virof0c3b502013-05-16 12:07:31 -04003022static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003023{
Al Virof0c3b502013-05-16 12:07:31 -04003024 return proc_pident_readdir(file, ctx,
3025 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003026}
3027
Arjan van de Ven00977a52007-02-12 00:55:34 -08003028static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003029 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003030 .iterate_shared = proc_tgid_base_readdir,
3031 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003032};
3033
Al Viro00cd8dd2012-06-10 17:13:09 -04003034static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3035{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003036 return proc_pident_lookup(dir, dentry,
3037 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003038}
3039
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003040static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003041 .lookup = proc_tgid_base_lookup,
3042 .getattr = pid_getattr,
3043 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003044 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003045};
3046
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003047static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003049 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003050 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003051 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Eric W. Biederman48e64842006-06-26 00:25:48 -07003053 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003054 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003055 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003056 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003057 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003058 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003059 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003062 if (pid == tgid)
3063 return;
3064
Eric W. Biederman48e64842006-06-26 00:25:48 -07003065 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003066 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003067 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003068 if (!leader)
3069 goto out;
3070
3071 name.name = "task";
3072 name.len = strlen(name.name);
3073 dir = d_hash_and_lookup(leader, &name);
3074 if (!dir)
3075 goto out_put_leader;
3076
3077 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003078 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003079 dentry = d_hash_and_lookup(dir, &name);
3080 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003081 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003082 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003084
3085 dput(dir);
3086out_put_leader:
3087 dput(leader);
3088out:
3089 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
Randy Dunlap0895e912007-10-21 21:00:10 -07003092/**
3093 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3094 * @task: task that should be flushed.
3095 *
3096 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003097 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003098 * in. This call is supposed to do all of this job.
3099 *
3100 * Looks in the dcache for
3101 * /proc/@pid
3102 * /proc/@tgid/task/@pid
3103 * if either directory is present flushes it and all of it'ts children
3104 * from the dcache.
3105 *
3106 * It is safe and reasonable to cache /proc entries for a task until
3107 * that task exits. After that they just clog up the dcache with
3108 * useless entries, possibly causing useful dcache entries to be
3109 * flushed instead. This routine is proved to flush those useless
3110 * dcache entries at process exit time.
3111 *
3112 * NOTE: This routine is just an optimization so it does not guarantee
3113 * that no dcache entries will exist at process exit time it
3114 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003115 */
3116
3117void proc_flush_task(struct task_struct *task)
3118{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003119 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003120 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003121 struct upid *upid;
3122
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003123 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003124 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003125
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003126 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003127 upid = &pid->numbers[i];
3128 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003129 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003130 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003131}
3132
Al Viroc52a47a2013-06-15 11:15:20 +04003133static int proc_pid_instantiate(struct inode *dir,
3134 struct dentry * dentry,
3135 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003136{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003137 struct inode *inode;
3138
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003139 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003140 if (!inode)
3141 goto out;
3142
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003143 inode->i_op = &proc_tgid_base_inode_operations;
3144 inode->i_fop = &proc_tgid_base_operations;
3145 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003146
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003147 set_nlink(inode, nlink_tgid);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003148
Nick Pigginfb045ad2011-01-07 17:49:55 +11003149 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003150
3151 d_add(dentry, inode);
3152 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003153 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003154 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003155out:
Al Viroc52a47a2013-06-15 11:15:20 +04003156 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003157}
3158
Al Viro00cd8dd2012-06-10 17:13:09 -04003159struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003161 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003164 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003166 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 if (tgid == ~0U)
3168 goto out;
3169
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003170 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003171 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003172 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (task)
3174 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003175 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if (!task)
3177 goto out;
3178
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003179 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003180 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181out:
Al Viroc52a47a2013-06-15 11:15:20 +04003182 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003186 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003187 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003189struct tgid_iter {
3190 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003191 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003192};
3193static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3194{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003195 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003197 if (iter.task)
3198 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003199 rcu_read_lock();
3200retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003201 iter.task = NULL;
3202 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003203 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003204 iter.tgid = pid_nr_ns(pid, ns);
3205 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003206 /* What we to know is if the pid we have find is the
3207 * pid of a thread_group_leader. Testing for task
3208 * being a thread_group_leader is the obvious thing
3209 * todo but there is a window when it fails, due to
3210 * the pid transfer logic in de_thread.
3211 *
3212 * So we perform the straight forward test of seeing
3213 * if the pid we have found is the pid of a thread
3214 * group leader, and don't worry if the task we have
3215 * found doesn't happen to be a thread group leader.
3216 * As we don't care in the case of readdir.
3217 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003218 if (!iter.task || !has_group_leader_pid(iter.task)) {
3219 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003220 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003221 }
3222 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003224 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003225 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
Eric W. Biederman00978752014-07-31 03:10:50 -07003228#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003231int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003233 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003234 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003235 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Al Viro021ada72013-03-29 19:27:05 -04003237 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Eric W. Biederman00978752014-07-31 03:10:50 -07003240 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003241 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003242 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003243 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003244 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003245 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003246 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003247 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003248 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3249 return 0;
3250 ctx->pos = pos = pos + 1;
3251 }
3252 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003253 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003254 for (iter = next_tgid(ns, iter);
3255 iter.task;
3256 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003257 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003258 int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003259
3260 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003261 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003262 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003263
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003264 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003265 ctx->pos = iter.tgid + TGID_OFFSET;
3266 if (!proc_fill_cache(file, ctx, name, len,
3267 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003268 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
Al Virof0c3b502013-05-16 12:07:31 -04003272 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return 0;
3274}
3275
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003276/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003277 * proc_tid_comm_permission is a special permission function exclusively
3278 * used for the node /proc/<pid>/task/<tid>/comm.
3279 * It bypasses generic permission checks in the case where a task of the same
3280 * task group attempts to access the node.
3281 * The rationale behind this is that glibc and bionic access this node for
3282 * cross thread naming (pthread_set/getname_np(!self)). However, if
3283 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3284 * which locks out the cross thread naming implementation.
3285 * This function makes sure that the node is always accessible for members of
3286 * same thread group.
3287 */
3288static int proc_tid_comm_permission(struct inode *inode, int mask)
3289{
3290 bool is_same_tgroup;
3291 struct task_struct *task;
3292
3293 task = get_proc_task(inode);
3294 if (!task)
3295 return -ESRCH;
3296 is_same_tgroup = same_thread_group(current, task);
3297 put_task_struct(task);
3298
3299 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3300 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3301 * read or written by the members of the corresponding
3302 * thread group.
3303 */
3304 return 0;
3305 }
3306
3307 return generic_permission(inode, mask);
3308}
3309
3310static const struct inode_operations proc_tid_comm_inode_operations = {
3311 .permission = proc_tid_comm_permission,
3312};
3313
3314/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003315 * Tasks
3316 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003317static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003318 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003319 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003320 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003321#ifdef CONFIG_NET
3322 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3323#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003324 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003325 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003326 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003327 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003328 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003329#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003330 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003331#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003332 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3333 &proc_tid_comm_inode_operations,
3334 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003335#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003336 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003337#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003338 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003339 ONE("stat", S_IRUGO, proc_tid_stat),
3340 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003341 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003342#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003343 REG("children", S_IRUGO, proc_tid_children_operations),
3344#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003345#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003346 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003347#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003348 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3349 LNK("cwd", proc_cwd_link),
3350 LNK("root", proc_root_link),
3351 LNK("exe", proc_exe_link),
3352 REG("mounts", S_IRUGO, proc_mounts_operations),
3353 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003354#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003355 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003356 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003357 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003358 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003359#endif
3360#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003361 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003362#endif
3363#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003364 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003365#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003366#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003367 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003368#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303369#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003370 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003371#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003372#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003373 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003374#endif
Paul Menage8793d852007-10-18 23:39:39 -07003375#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003376 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003377#endif
Paul Menagea4243162007-10-18 23:39:35 -07003378#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003379 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003380#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003381 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003382 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003383 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003384#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003385 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003386 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003387#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003388#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003389 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003390 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003391#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003392#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003393 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003394#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003395#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003396 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003397#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003398#ifdef CONFIG_USER_NS
3399 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3400 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003401 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003402 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003403#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003404#ifdef CONFIG_LIVEPATCH
3405 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3406#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003407};
3408
Al Virof0c3b502013-05-16 12:07:31 -04003409static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003410{
Al Virof0c3b502013-05-16 12:07:31 -04003411 return proc_pident_readdir(file, ctx,
3412 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003413}
3414
Al Viro00cd8dd2012-06-10 17:13:09 -04003415static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3416{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003417 return proc_pident_lookup(dir, dentry,
3418 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003419}
3420
Arjan van de Ven00977a52007-02-12 00:55:34 -08003421static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003422 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003423 .iterate_shared = proc_tid_base_readdir,
3424 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003425};
3426
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003427static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003428 .lookup = proc_tid_base_lookup,
3429 .getattr = pid_getattr,
3430 .setattr = proc_setattr,
3431};
3432
Al Viroc52a47a2013-06-15 11:15:20 +04003433static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003434 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003435{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003436 struct inode *inode;
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003437 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003438
3439 if (!inode)
3440 goto out;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003441 inode->i_op = &proc_tid_base_inode_operations;
3442 inode->i_fop = &proc_tid_base_operations;
3443 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003444
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003445 set_nlink(inode, nlink_tid);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003446
Nick Pigginfb045ad2011-01-07 17:49:55 +11003447 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003448
3449 d_add(dentry, inode);
3450 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003451 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003452 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003453out:
Al Viroc52a47a2013-06-15 11:15:20 +04003454 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003455}
3456
Al Viro00cd8dd2012-06-10 17:13:09 -04003457static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003458{
Al Viroc52a47a2013-06-15 11:15:20 +04003459 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003460 struct task_struct *task;
3461 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003462 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003463 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003464
3465 if (!leader)
3466 goto out_no_task;
3467
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003468 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003469 if (tid == ~0U)
3470 goto out;
3471
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003472 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003473 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003474 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003475 if (task)
3476 get_task_struct(task);
3477 rcu_read_unlock();
3478 if (!task)
3479 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003480 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003481 goto out_drop_task;
3482
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003483 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003484out_drop_task:
3485 put_task_struct(task);
3486out:
3487 put_task_struct(leader);
3488out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003489 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003490}
3491
3492/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003493 * Find the first tid of a thread group to return to user space.
3494 *
3495 * Usually this is just the thread group leader, but if the users
3496 * buffer was too small or there was a seek into the middle of the
3497 * directory we have more work todo.
3498 *
3499 * In the case of a short read we start with find_task_by_pid.
3500 *
3501 * In the case of a seek we start with the leader and walk nr
3502 * threads past it.
3503 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003504static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3505 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003506{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003507 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003508 unsigned long nr = f_pos;
3509
3510 if (nr != f_pos) /* 32bit overflow? */
3511 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003512
Eric W. Biedermancc288732006-06-26 00:26:01 -07003513 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003514 task = pid_task(pid, PIDTYPE_PID);
3515 if (!task)
3516 goto fail;
3517
3518 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003519 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003520 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003521 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003522 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003523 }
3524
3525 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003526 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003527 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003528
3529 /* If we haven't found our starting place yet start
3530 * with the leader and walk nr threads forward.
3531 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003532 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003533 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003534 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003535 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003536 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003537fail:
3538 pos = NULL;
3539 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003540found:
3541 get_task_struct(pos);
3542out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003543 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003544 return pos;
3545}
3546
3547/*
3548 * Find the next thread in the thread list.
3549 * Return NULL if there is an error or no next thread.
3550 *
3551 * The reference to the input task_struct is released.
3552 */
3553static struct task_struct *next_tid(struct task_struct *start)
3554{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003555 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003556 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003557 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003558 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003559 if (thread_group_leader(pos))
3560 pos = NULL;
3561 else
3562 get_task_struct(pos);
3563 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003564 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003565 put_task_struct(start);
3566 return pos;
3567}
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003570static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003572 struct inode *inode = file_inode(file);
3573 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003574 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003575 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003577 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003578 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Al Virof0c3b502013-05-16 12:07:31 -04003580 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003583 /* f_version caches the tgid value that the last readdir call couldn't
3584 * return. lseek aka telldir automagically resets f_version to 0.
3585 */
Al Viro3aa33772014-10-31 00:42:35 -04003586 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003587 tid = (int)file->f_version;
3588 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003589 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003590 task;
Al Virof0c3b502013-05-16 12:07:31 -04003591 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003592 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003593 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003594 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003595 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003596 if (!proc_fill_cache(file, ctx, name, len,
3597 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003598 /* returning this tgid failed, save it as the first
3599 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003600 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003601 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003605
Al Virof0c3b502013-05-16 12:07:31 -04003606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003608
David Howellsa528d352017-01-31 16:46:22 +00003609static int proc_task_getattr(const struct path *path, struct kstat *stat,
3610 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003611{
David Howellsa528d352017-01-31 16:46:22 +00003612 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003613 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003614 generic_fillattr(inode, stat);
3615
Eric W. Biederman99f89552006-06-26 00:25:55 -07003616 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003617 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003618 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003619 }
3620
3621 return 0;
3622}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003623
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003624static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003625 .lookup = proc_task_lookup,
3626 .getattr = proc_task_getattr,
3627 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003628 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003629};
3630
Arjan van de Ven00977a52007-02-12 00:55:34 -08003631static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003632 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003633 .iterate_shared = proc_task_readdir,
3634 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003635};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003636
3637void __init set_proc_pid_nlink(void)
3638{
3639 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3640 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3641}