blob: 8e654468ab676900bdcfb029558a766e1b448691 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080076#include <linux/printk.h>
Paul Menagea4243162007-10-18 23:39:35 -070077#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpuset.h>
79#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050080#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070081#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070082#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070083#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070084#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080085#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040086#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080088#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040089#include <linux/posix-timers.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040090#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080093#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040095#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070097/* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700108 const char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700109 int len;
Al Virod161a132011-07-24 03:36:29 -0400110 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800111 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800112 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700118 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 .mode = MODE, \
120 .iop = IOP, \
121 .fop = FOP, \
122 .op = OP, \
123}
124
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300125#define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700129 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300130 { .proc_get_link = get_link } )
131#define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300133#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Vegard Nossumaed54172008-06-05 22:46:53 -0700138/*
139 * Count the number of hardlinks for the pid_entry table, excluding the .
140 * and .. links.
141 */
142static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
143 unsigned int n)
144{
145 unsigned int i;
146 unsigned int count;
147
148 count = 0;
149 for (i = 0; i < n; ++i) {
150 if (S_ISDIR(entries[i].mode))
151 ++count;
152 }
153
154 return count;
155}
156
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200157static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000159 int result = -ENOENT;
160
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200162 if (task->fs) {
163 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000164 result = 0;
165 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700166 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168}
169
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800170static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171{
David Howells2b0143b2015-03-17 22:25:59 +0000172 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700174
175 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200176 task_lock(task);
177 if (task->fs) {
178 get_fs_pwd(task->fs, path);
179 result = 0;
180 }
181 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182 put_task_struct(task);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return result;
185}
186
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800187static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
David Howells2b0143b2015-03-17 22:25:59 +0000189 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700191
192 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200193 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194 put_task_struct(task);
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return result;
197}
198
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700199static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
200 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700202 struct task_struct *tsk;
203 struct mm_struct *mm;
204 char *page;
205 unsigned long count = _count;
206 unsigned long arg_start, arg_end, env_start, env_end;
207 unsigned long len1, len2, len;
208 unsigned long p;
209 char c;
210 ssize_t rv;
211
212 BUG_ON(*pos < 0);
213
214 tsk = get_proc_task(file_inode(file));
215 if (!tsk)
216 return -ESRCH;
217 mm = get_task_mm(tsk);
218 put_task_struct(tsk);
219 if (!mm)
220 return 0;
221 /* Check if process spawned far enough to have cmdline. */
222 if (!mm->env_end) {
223 rv = 0;
224 goto out_mmput;
225 }
226
227 page = (char *)__get_free_page(GFP_TEMPORARY);
228 if (!page) {
229 rv = -ENOMEM;
230 goto out_mmput;
231 }
232
233 down_read(&mm->mmap_sem);
234 arg_start = mm->arg_start;
235 arg_end = mm->arg_end;
236 env_start = mm->env_start;
237 env_end = mm->env_end;
238 up_read(&mm->mmap_sem);
239
240 BUG_ON(arg_start > arg_end);
241 BUG_ON(env_start > env_end);
242
243 len1 = arg_end - arg_start;
244 len2 = env_end - env_start;
245
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700246 /* Empty ARGV. */
247 if (len1 == 0) {
248 rv = 0;
249 goto out_free_page;
250 }
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700251 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700252 * Inherently racy -- command line shares address space
253 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700254 */
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100255 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_FORCE);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700256 if (rv <= 0)
257 goto out_free_page;
258
259 rv = 0;
260
261 if (c == '\0') {
262 /* Command line (set of strings) occupies whole ARGV. */
263 if (len1 <= *pos)
264 goto out_free_page;
265
266 p = arg_start + *pos;
267 len = len1 - *pos;
268 while (count > 0 && len > 0) {
269 unsigned int _count;
270 int nr_read;
271
272 _count = min3(count, len, PAGE_SIZE);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100273 nr_read = access_remote_vm(mm, p, page, _count,
274 FOLL_FORCE);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700275 if (nr_read < 0)
276 rv = nr_read;
277 if (nr_read <= 0)
278 goto out_free_page;
279
280 if (copy_to_user(buf, page, nr_read)) {
281 rv = -EFAULT;
282 goto out_free_page;
283 }
284
285 p += nr_read;
286 len -= nr_read;
287 buf += nr_read;
288 count -= nr_read;
289 rv += nr_read;
290 }
291 } else {
292 /*
293 * Command line (1 string) occupies ARGV and maybe
294 * extends into ENVP.
295 */
296 if (len1 + len2 <= *pos)
297 goto skip_argv_envp;
298 if (len1 <= *pos)
299 goto skip_argv;
300
301 p = arg_start + *pos;
302 len = len1 - *pos;
303 while (count > 0 && len > 0) {
304 unsigned int _count, l;
305 int nr_read;
306 bool final;
307
308 _count = min3(count, len, PAGE_SIZE);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100309 nr_read = access_remote_vm(mm, p, page, _count,
310 FOLL_FORCE);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700311 if (nr_read < 0)
312 rv = nr_read;
313 if (nr_read <= 0)
314 goto out_free_page;
315
316 /*
317 * Command line can be shorter than whole ARGV
318 * even if last "marker" byte says it is not.
319 */
320 final = false;
321 l = strnlen(page, nr_read);
322 if (l < nr_read) {
323 nr_read = l;
324 final = true;
325 }
326
327 if (copy_to_user(buf, page, nr_read)) {
328 rv = -EFAULT;
329 goto out_free_page;
330 }
331
332 p += nr_read;
333 len -= nr_read;
334 buf += nr_read;
335 count -= nr_read;
336 rv += nr_read;
337
338 if (final)
339 goto out_free_page;
340 }
341skip_argv:
342 /*
343 * Command line (1 string) occupies ARGV and
344 * extends into ENVP.
345 */
346 if (len1 <= *pos) {
347 p = env_start + *pos - len1;
348 len = len1 + len2 - *pos;
349 } else {
350 p = env_start;
351 len = len2;
352 }
353 while (count > 0 && len > 0) {
354 unsigned int _count, l;
355 int nr_read;
356 bool final;
357
358 _count = min3(count, len, PAGE_SIZE);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100359 nr_read = access_remote_vm(mm, p, page, _count,
360 FOLL_FORCE);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700361 if (nr_read < 0)
362 rv = nr_read;
363 if (nr_read <= 0)
364 goto out_free_page;
365
366 /* Find EOS. */
367 final = false;
368 l = strnlen(page, nr_read);
369 if (l < nr_read) {
370 nr_read = l;
371 final = true;
372 }
373
374 if (copy_to_user(buf, page, nr_read)) {
375 rv = -EFAULT;
376 goto out_free_page;
377 }
378
379 p += nr_read;
380 len -= nr_read;
381 buf += nr_read;
382 count -= nr_read;
383 rv += nr_read;
384
385 if (final)
386 goto out_free_page;
387 }
388skip_argv_envp:
389 ;
390 }
391
392out_free_page:
393 free_page((unsigned long)page);
394out_mmput:
395 mmput(mm);
396 if (rv > 0)
397 *pos += rv;
398 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700401static const struct file_operations proc_pid_cmdline_ops = {
402 .read = proc_pid_cmdline_read,
403 .llseek = generic_file_llseek,
404};
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#ifdef CONFIG_KALLSYMS
407/*
408 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
409 * Returns the resolved symbol. If that fails, simply return the address.
410 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700411static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
412 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700414 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700415 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 wchan = get_wchan(task);
418
Jann Horncaaee622016-01-20 15:00:04 -0800419 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
420 && !lookup_symbol_name(wchan, symname))
Joe Perches25ce3192015-04-15 16:18:17 -0700421 seq_printf(m, "%s", symname);
Ingo Molnarb2f73922015-09-30 15:59:17 +0200422 else
Robin Humble1e92a612016-05-09 16:28:46 -0700423 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700424
425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427#endif /* CONFIG_KALLSYMS */
428
Al Viroa9712bc2011-03-23 15:52:50 -0400429static int lock_trace(struct task_struct *task)
430{
431 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
432 if (err)
433 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800434 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400435 mutex_unlock(&task->signal->cred_guard_mutex);
436 return -EPERM;
437 }
438 return 0;
439}
440
441static void unlock_trace(struct task_struct *task)
442{
443 mutex_unlock(&task->signal->cred_guard_mutex);
444}
445
Ken Chen2ec220e2008-11-10 11:26:08 +0300446#ifdef CONFIG_STACKTRACE
447
448#define MAX_STACK_TRACE_DEPTH 64
449
450static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
451 struct pid *pid, struct task_struct *task)
452{
453 struct stack_trace trace;
454 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400455 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300456 int i;
457
458 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
459 if (!entries)
460 return -ENOMEM;
461
462 trace.nr_entries = 0;
463 trace.max_entries = MAX_STACK_TRACE_DEPTH;
464 trace.entries = entries;
465 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300466
Al Viroa9712bc2011-03-23 15:52:50 -0400467 err = lock_trace(task);
468 if (!err) {
469 save_stack_trace_tsk(task, &trace);
470
471 for (i = 0; i < trace.nr_entries; i++) {
Josh Poimboeuf8b927d72016-08-18 10:59:07 -0500472 seq_printf(m, "[<%pK>] %pB\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400473 (void *)entries[i], (void *)entries[i]);
474 }
475 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300476 }
477 kfree(entries);
478
Al Viroa9712bc2011-03-23 15:52:50 -0400479 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300480}
481#endif
482
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530483#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * Provides /proc/PID/schedstat
486 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700487static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
488 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530490 if (unlikely(!sched_info_on()))
491 seq_printf(m, "0 0 0\n");
492 else
493 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700494 (unsigned long long)task->se.sum_exec_runtime,
495 (unsigned long long)task->sched_info.run_delay,
496 task->sched_info.pcount);
497
498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500#endif
501
Arjan van de Ven97455122008-01-25 21:08:34 +0100502#ifdef CONFIG_LATENCYTOP
503static int lstats_show_proc(struct seq_file *m, void *v)
504{
505 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800506 struct inode *inode = m->private;
507 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100508
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800509 if (!task)
510 return -ESRCH;
511 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100512 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800513 struct latency_record *lr = &task->latency_record[i];
514 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100515 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800516 seq_printf(m, "%i %li %li",
517 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100518 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800519 unsigned long bt = lr->backtrace[q];
520 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100521 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800522 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100523 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800524 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100525 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800526 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100527 }
528
529 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800530 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100531 return 0;
532}
533
534static int lstats_open(struct inode *inode, struct file *file)
535{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800536 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800537}
538
Arjan van de Ven97455122008-01-25 21:08:34 +0100539static ssize_t lstats_write(struct file *file, const char __user *buf,
540 size_t count, loff_t *offs)
541{
Al Viro496ad9a2013-01-23 17:07:38 -0500542 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100543
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800544 if (!task)
545 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100546 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800547 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100548
549 return count;
550}
551
552static const struct file_operations proc_lstats_operations = {
553 .open = lstats_open,
554 .read = seq_read,
555 .write = lstats_write,
556 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800557 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100558};
559
560#endif
561
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700562static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
563 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
David Rientjesa7f638f2012-05-29 15:06:47 -0700565 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200566 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Oleg Nesterovef419392016-08-02 14:03:19 -0700568 points = oom_badness(task, NULL, NULL, totalpages) *
569 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700570 seq_printf(m, "%lu\n", points);
571
572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Neil Hormand85f50d2007-10-18 23:40:37 -0700575struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700576 const char *name;
577 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700578};
579
580static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700581 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700582 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
583 [RLIMIT_DATA] = {"Max data size", "bytes"},
584 [RLIMIT_STACK] = {"Max stack size", "bytes"},
585 [RLIMIT_CORE] = {"Max core file size", "bytes"},
586 [RLIMIT_RSS] = {"Max resident set", "bytes"},
587 [RLIMIT_NPROC] = {"Max processes", "processes"},
588 [RLIMIT_NOFILE] = {"Max open files", "files"},
589 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
590 [RLIMIT_AS] = {"Max address space", "bytes"},
591 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
592 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
593 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
594 [RLIMIT_NICE] = {"Max nice priority", NULL},
595 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800596 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700597};
598
599/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700600static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
601 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700602{
603 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700604 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700605
606 struct rlimit rlim[RLIM_NLIMITS];
607
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400608 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700609 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700610 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
611 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700612
613 /*
614 * print the file header
615 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700616 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700617 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700618
619 for (i = 0; i < RLIM_NLIMITS; i++) {
620 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700621 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700622 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700623 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700624 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700625 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700626
627 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700628 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700629 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700630 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700631
632 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700633 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700634 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700635 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700636 }
637
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700638 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700639}
640
Roland McGrathebcb6732008-07-25 19:46:00 -0700641#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700642static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
643 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700644{
645 long nr;
646 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700647 int res;
648
649 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400650 if (res)
651 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700652
653 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700654 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400655 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700656 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400657 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700658 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700659 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
660 nr,
661 args[0], args[1], args[2], args[3], args[4], args[5],
662 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400663 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700664
665 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700666}
667#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/************************************************************************/
670/* Here the fs part begins */
671/************************************************************************/
672
673/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700674static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700676 struct task_struct *task;
677 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700678 /* Allow access to a task's file descriptors if it is us or we
679 * may use ptrace attach to the process and find out that
680 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700681 */
682 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700683 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800684 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700685 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700686 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700687 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800690int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700691{
692 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000693 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700694
695 if (attr->ia_valid & ATTR_MODE)
696 return -EPERM;
697
Jan Kara31051c82016-05-26 16:55:18 +0200698 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200699 if (error)
700 return error;
701
Christoph Hellwig10257742010-06-04 11:30:02 +0200702 setattr_copy(inode, attr);
703 mark_inode_dirty(inode);
704 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700705}
706
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800707/*
708 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
709 * or euid/egid (for hide_pid_min=2)?
710 */
711static bool has_pid_permissions(struct pid_namespace *pid,
712 struct task_struct *task,
713 int hide_pid_min)
714{
715 if (pid->hide_pid < hide_pid_min)
716 return true;
717 if (in_group_p(pid->pid_gid))
718 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800719 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800720}
721
722
723static int proc_pid_permission(struct inode *inode, int mask)
724{
725 struct pid_namespace *pid = inode->i_sb->s_fs_info;
726 struct task_struct *task;
727 bool has_perms;
728
729 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800730 if (!task)
731 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800732 has_perms = has_pid_permissions(pid, task, 1);
733 put_task_struct(task);
734
735 if (!has_perms) {
736 if (pid->hide_pid == 2) {
737 /*
738 * Let's make getdents(), stat(), and open()
739 * consistent with each other. If a process
740 * may not stat() a file, it shouldn't be seen
741 * in procfs at all.
742 */
743 return -ENOENT;
744 }
745
746 return -EPERM;
747 }
748 return generic_permission(inode, mask);
749}
750
751
752
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800753static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700754 .setattr = proc_setattr,
755};
756
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800757static int proc_single_show(struct seq_file *m, void *v)
758{
759 struct inode *inode = m->private;
760 struct pid_namespace *ns;
761 struct pid *pid;
762 struct task_struct *task;
763 int ret;
764
765 ns = inode->i_sb->s_fs_info;
766 pid = proc_pid(inode);
767 task = get_pid_task(pid, PIDTYPE_PID);
768 if (!task)
769 return -ESRCH;
770
771 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
772
773 put_task_struct(task);
774 return ret;
775}
776
777static int proc_single_open(struct inode *inode, struct file *filp)
778{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800779 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800780}
781
782static const struct file_operations proc_single_file_operations = {
783 .open = proc_single_open,
784 .read = seq_read,
785 .llseek = seq_lseek,
786 .release = single_release,
787};
788
Oleg Nesterov5381e162014-10-09 15:25:24 -0700789
790struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
791{
792 struct task_struct *task = get_proc_task(inode);
793 struct mm_struct *mm = ERR_PTR(-ESRCH);
794
795 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800796 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700797 put_task_struct(task);
798
799 if (!IS_ERR_OR_NULL(mm)) {
800 /* ensure this mm_struct can't be freed */
801 atomic_inc(&mm->mm_count);
802 /* but do not pin its memory */
803 mmput(mm);
804 }
805 }
806
807 return mm;
808}
809
Cong Wangb409e572012-05-31 16:26:17 -0700810static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700812 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800813
814 if (IS_ERR(mm))
815 return PTR_ERR(mm);
816
Linus Torvaldse2683372012-01-17 15:21:19 -0800817 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return 0;
819}
820
Cong Wangb409e572012-05-31 16:26:17 -0700821static int mem_open(struct inode *inode, struct file *file)
822{
Djalal Harounibc452b42012-07-30 14:42:28 -0700823 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
824
825 /* OK to pass negative loff_t, we can catch out-of-range */
826 file->f_mode |= FMODE_UNSIGNED_OFFSET;
827
828 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700829}
830
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100831static ssize_t mem_rw(struct file *file, char __user *buf,
832 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Linus Torvaldse2683372012-01-17 15:21:19 -0800834 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100835 unsigned long addr = *ppos;
836 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 char *page;
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100838 unsigned int flags = FOLL_FORCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvaldse2683372012-01-17 15:21:19 -0800840 if (!mm)
841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Mel Gormane12ba742007-10-16 01:25:52 -0700843 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800845 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700847 copied = 0;
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100848 if (!atomic_inc_not_zero(&mm->mm_users))
849 goto free;
850
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100851 if (write)
852 flags |= FOLL_WRITE;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100855 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100857 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 copied = -EFAULT;
859 break;
860 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100861
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100862 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100863 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!copied)
865 copied = -EIO;
866 break;
867 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100868
869 if (!write && copy_to_user(buf, page, this_len)) {
870 copied = -EFAULT;
871 break;
872 }
873
874 buf += this_len;
875 addr += this_len;
876 copied += this_len;
877 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100879 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700880
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100881 mmput(mm);
882free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700883 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return copied;
885}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100887static ssize_t mem_read(struct file *file, char __user *buf,
888 size_t count, loff_t *ppos)
889{
890 return mem_rw(file, buf, count, ppos, 0);
891}
892
893static ssize_t mem_write(struct file *file, const char __user *buf,
894 size_t count, loff_t *ppos)
895{
896 return mem_rw(file, (char __user*)buf, count, ppos, 1);
897}
898
Matt Mackall85863e42008-02-04 22:29:04 -0800899loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 switch (orig) {
902 case 0:
903 file->f_pos = offset;
904 break;
905 case 1:
906 file->f_pos += offset;
907 break;
908 default:
909 return -EINVAL;
910 }
911 force_successful_syscall_return();
912 return file->f_pos;
913}
914
Linus Torvaldse2683372012-01-17 15:21:19 -0800915static int mem_release(struct inode *inode, struct file *file)
916{
917 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100918 if (mm)
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100919 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800920 return 0;
921}
922
Arjan van de Ven00977a52007-02-12 00:55:34 -0800923static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 .llseek = mem_lseek,
925 .read = mem_read,
926 .write = mem_write,
927 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800928 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929};
930
Cong Wangb409e572012-05-31 16:26:17 -0700931static int environ_open(struct inode *inode, struct file *file)
932{
933 return __mem_open(inode, file, PTRACE_MODE_READ);
934}
935
James Pearson315e28c2007-10-16 23:30:17 -0700936static ssize_t environ_read(struct file *file, char __user *buf,
937 size_t count, loff_t *ppos)
938{
James Pearson315e28c2007-10-16 23:30:17 -0700939 char *page;
940 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700941 int ret = 0;
942 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800943 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700944
Mathias Krause8148a732016-05-05 16:22:26 -0700945 /* Ensure the process spawned far enough to have an environment. */
946 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700947 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700948
James Pearson315e28c2007-10-16 23:30:17 -0700949 page = (char *)__get_free_page(GFP_TEMPORARY);
950 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700951 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700952
Al Virod6f64b82011-02-15 22:26:01 -0500953 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700954 if (!atomic_inc_not_zero(&mm->mm_users))
955 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800956
957 down_read(&mm->mmap_sem);
958 env_start = mm->env_start;
959 env_end = mm->env_end;
960 up_read(&mm->mmap_sem);
961
James Pearson315e28c2007-10-16 23:30:17 -0700962 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700963 size_t this_len, max_len;
964 int retval;
965
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800966 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700967 break;
James Pearson315e28c2007-10-16 23:30:17 -0700968
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800969 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700970
Djalal Harounie8905ec2012-07-30 14:42:26 -0700971 max_len = min_t(size_t, PAGE_SIZE, count);
972 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700973
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800974 retval = access_remote_vm(mm, (env_start + src),
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100975 page, this_len, FOLL_FORCE);
James Pearson315e28c2007-10-16 23:30:17 -0700976
977 if (retval <= 0) {
978 ret = retval;
979 break;
980 }
981
982 if (copy_to_user(buf, page, retval)) {
983 ret = -EFAULT;
984 break;
985 }
986
987 ret += retval;
988 src += retval;
989 buf += retval;
990 count -= retval;
991 }
992 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700993 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700994
995free:
James Pearson315e28c2007-10-16 23:30:17 -0700996 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700997 return ret;
998}
999
1000static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -07001001 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -07001002 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001003 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -07001004 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -07001005};
1006
Al Viroc5317162016-10-05 18:43:43 -04001007static int auxv_open(struct inode *inode, struct file *file)
1008{
1009 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1010}
1011
1012static ssize_t auxv_read(struct file *file, char __user *buf,
1013 size_t count, loff_t *ppos)
1014{
1015 struct mm_struct *mm = file->private_data;
1016 unsigned int nwords = 0;
1017 do {
1018 nwords += 2;
1019 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1020 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1021 nwords * sizeof(mm->saved_auxv[0]));
1022}
1023
1024static const struct file_operations proc_auxv_operations = {
1025 .open = auxv_open,
1026 .read = auxv_read,
1027 .llseek = generic_file_llseek,
1028 .release = mem_release,
1029};
1030
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001031static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1032 loff_t *ppos)
1033{
Al Viro496ad9a2013-01-23 17:07:38 -05001034 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001035 char buffer[PROC_NUMBUF];
1036 int oom_adj = OOM_ADJUST_MIN;
1037 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001038
1039 if (!task)
1040 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001041 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1042 oom_adj = OOM_ADJUST_MAX;
1043 else
1044 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1045 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001046 put_task_struct(task);
1047 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1048 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1049}
1050
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001051static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1052{
1053 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001054 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001055 struct task_struct *task;
1056 int err = 0;
1057
1058 task = get_proc_task(file_inode(file));
1059 if (!task)
1060 return -ESRCH;
1061
1062 mutex_lock(&oom_adj_mutex);
1063 if (legacy) {
1064 if (oom_adj < task->signal->oom_score_adj &&
1065 !capable(CAP_SYS_RESOURCE)) {
1066 err = -EACCES;
1067 goto err_unlock;
1068 }
1069 /*
1070 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1071 * /proc/pid/oom_score_adj instead.
1072 */
1073 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1074 current->comm, task_pid_nr(current), task_pid_nr(task),
1075 task_pid_nr(task));
1076 } else {
1077 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1078 !capable(CAP_SYS_RESOURCE)) {
1079 err = -EACCES;
1080 goto err_unlock;
1081 }
1082 }
1083
Michal Hocko44a70ade2016-07-28 15:44:43 -07001084 /*
1085 * Make sure we will check other processes sharing the mm if this is
1086 * not vfrok which wants its own oom_score_adj.
1087 * pin the mm so it doesn't go away and get reused after task_unlock
1088 */
1089 if (!task->vfork_done) {
1090 struct task_struct *p = find_lock_task_mm(task);
1091
1092 if (p) {
1093 if (atomic_read(&p->mm->mm_users) > 1) {
1094 mm = p->mm;
1095 atomic_inc(&mm->mm_count);
1096 }
1097 task_unlock(p);
1098 }
1099 }
1100
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001101 task->signal->oom_score_adj = oom_adj;
1102 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1103 task->signal->oom_score_adj_min = (short)oom_adj;
1104 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001105
1106 if (mm) {
1107 struct task_struct *p;
1108
1109 rcu_read_lock();
1110 for_each_process(p) {
1111 if (same_thread_group(task, p))
1112 continue;
1113
1114 /* do not touch kernel threads or the global init */
1115 if (p->flags & PF_KTHREAD || is_global_init(p))
1116 continue;
1117
1118 task_lock(p);
1119 if (!p->vfork_done && process_shares_mm(p, mm)) {
1120 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",
1121 task_pid_nr(p), p->comm,
1122 p->signal->oom_score_adj, oom_adj,
1123 task_pid_nr(task), task->comm);
1124 p->signal->oom_score_adj = oom_adj;
1125 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1126 p->signal->oom_score_adj_min = (short)oom_adj;
1127 }
1128 task_unlock(p);
1129 }
1130 rcu_read_unlock();
1131 mmdrop(mm);
1132 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001133err_unlock:
1134 mutex_unlock(&oom_adj_mutex);
1135 put_task_struct(task);
1136 return err;
1137}
Michal Hockof913da52016-07-28 15:44:37 -07001138
David Rientjesb72bdfa2015-11-05 18:50:32 -08001139/*
1140 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1141 * kernels. The effective policy is defined by oom_score_adj, which has a
1142 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1143 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1144 * Processes that become oom disabled via oom_adj will still be oom disabled
1145 * with this implementation.
1146 *
1147 * oom_adj cannot be removed since existing userspace binaries use it.
1148 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001149static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1150 size_t count, loff_t *ppos)
1151{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001152 char buffer[PROC_NUMBUF];
1153 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001154 int err;
1155
1156 memset(buffer, 0, sizeof(buffer));
1157 if (count > sizeof(buffer) - 1)
1158 count = sizeof(buffer) - 1;
1159 if (copy_from_user(buffer, buf, count)) {
1160 err = -EFAULT;
1161 goto out;
1162 }
1163
1164 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1165 if (err)
1166 goto out;
1167 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1168 oom_adj != OOM_DISABLE) {
1169 err = -EINVAL;
1170 goto out;
1171 }
1172
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001173 /*
1174 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1175 * value is always attainable.
1176 */
1177 if (oom_adj == OOM_ADJUST_MAX)
1178 oom_adj = OOM_SCORE_ADJ_MAX;
1179 else
1180 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1181
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001182 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001183out:
1184 return err < 0 ? err : count;
1185}
1186
1187static const struct file_operations proc_oom_adj_operations = {
1188 .read = oom_adj_read,
1189 .write = oom_adj_write,
1190 .llseek = generic_file_llseek,
1191};
1192
David Rientjesa63d83f2010-08-09 17:19:46 -07001193static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1194 size_t count, loff_t *ppos)
1195{
Al Viro496ad9a2013-01-23 17:07:38 -05001196 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001197 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001198 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001199 size_t len;
1200
1201 if (!task)
1202 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001203 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001204 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001205 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001206 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1207}
1208
1209static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1210 size_t count, loff_t *ppos)
1211{
David Rientjesa63d83f2010-08-09 17:19:46 -07001212 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001213 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001214 int err;
1215
1216 memset(buffer, 0, sizeof(buffer));
1217 if (count > sizeof(buffer) - 1)
1218 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001219 if (copy_from_user(buffer, buf, count)) {
1220 err = -EFAULT;
1221 goto out;
1222 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001223
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001224 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001225 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001226 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001227 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001228 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1229 err = -EINVAL;
1230 goto out;
1231 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001232
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001233 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001234out:
1235 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001236}
1237
1238static const struct file_operations proc_oom_score_adj_operations = {
1239 .read = oom_score_adj_read,
1240 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001241 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001242};
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244#ifdef CONFIG_AUDITSYSCALL
1245#define TMPBUFLEN 21
1246static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1247 size_t count, loff_t *ppos)
1248{
Al Viro496ad9a2013-01-23 17:07:38 -05001249 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001250 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 ssize_t length;
1252 char tmpbuf[TMPBUFLEN];
1253
Eric W. Biederman99f89552006-06-26 00:25:55 -07001254 if (!task)
1255 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001257 from_kuid(file->f_cred->user_ns,
1258 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001259 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1261}
1262
1263static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1264 size_t count, loff_t *ppos)
1265{
Al Viro496ad9a2013-01-23 17:07:38 -05001266 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001268 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001269 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001271 rcu_read_lock();
1272 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1273 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001275 }
1276 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (*ppos != 0) {
1279 /* No partial writes. */
1280 return -EINVAL;
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001283 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1284 if (rv < 0)
1285 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001286
1287 /* is userspace tring to explicitly UNSET the loginuid? */
1288 if (loginuid == AUDIT_UID_UNSET) {
1289 kloginuid = INVALID_UID;
1290 } else {
1291 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001292 if (!uid_valid(kloginuid))
1293 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001294 }
1295
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001296 rv = audit_set_loginuid(kloginuid);
1297 if (rv < 0)
1298 return rv;
1299 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Arjan van de Ven00977a52007-02-12 00:55:34 -08001302static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .read = proc_loginuid_read,
1304 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001305 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306};
Eric Paris1e0bd752008-03-13 08:15:31 -04001307
1308static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1309 size_t count, loff_t *ppos)
1310{
Al Viro496ad9a2013-01-23 17:07:38 -05001311 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001312 struct task_struct *task = get_proc_task(inode);
1313 ssize_t length;
1314 char tmpbuf[TMPBUFLEN];
1315
1316 if (!task)
1317 return -ESRCH;
1318 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1319 audit_get_sessionid(task));
1320 put_task_struct(task);
1321 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1322}
1323
1324static const struct file_operations proc_sessionid_operations = {
1325 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001326 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001327};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328#endif
1329
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001330#ifdef CONFIG_FAULT_INJECTION
1331static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1332 size_t count, loff_t *ppos)
1333{
Al Viro496ad9a2013-01-23 17:07:38 -05001334 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001335 char buffer[PROC_NUMBUF];
1336 size_t len;
1337 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001338
1339 if (!task)
1340 return -ESRCH;
1341 make_it_fail = task->make_it_fail;
1342 put_task_struct(task);
1343
1344 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001345
1346 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347}
1348
1349static ssize_t proc_fault_inject_write(struct file * file,
1350 const char __user * buf, size_t count, loff_t *ppos)
1351{
1352 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001353 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001354 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001355 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001356
1357 if (!capable(CAP_SYS_RESOURCE))
1358 return -EPERM;
1359 memset(buffer, 0, sizeof(buffer));
1360 if (count > sizeof(buffer) - 1)
1361 count = sizeof(buffer) - 1;
1362 if (copy_from_user(buffer, buf, count))
1363 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001364 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1365 if (rv < 0)
1366 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001367 if (make_it_fail < 0 || make_it_fail > 1)
1368 return -EINVAL;
1369
Al Viro496ad9a2013-01-23 17:07:38 -05001370 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001371 if (!task)
1372 return -ESRCH;
1373 task->make_it_fail = make_it_fail;
1374 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001375
1376 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001377}
1378
Arjan van de Ven00977a52007-02-12 00:55:34 -08001379static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001380 .read = proc_fault_inject_read,
1381 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001382 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001383};
1384#endif
1385
Arjan van de Ven97455122008-01-25 21:08:34 +01001386
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001387#ifdef CONFIG_SCHED_DEBUG
1388/*
1389 * Print out various scheduling related per-task fields:
1390 */
1391static int sched_show(struct seq_file *m, void *v)
1392{
1393 struct inode *inode = m->private;
1394 struct task_struct *p;
1395
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001396 p = get_proc_task(inode);
1397 if (!p)
1398 return -ESRCH;
1399 proc_sched_show_task(p, m);
1400
1401 put_task_struct(p);
1402
1403 return 0;
1404}
1405
1406static ssize_t
1407sched_write(struct file *file, const char __user *buf,
1408 size_t count, loff_t *offset)
1409{
Al Viro496ad9a2013-01-23 17:07:38 -05001410 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001411 struct task_struct *p;
1412
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001413 p = get_proc_task(inode);
1414 if (!p)
1415 return -ESRCH;
1416 proc_sched_set_task(p);
1417
1418 put_task_struct(p);
1419
1420 return count;
1421}
1422
1423static int sched_open(struct inode *inode, struct file *filp)
1424{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001425 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001426}
1427
1428static const struct file_operations proc_pid_sched_operations = {
1429 .open = sched_open,
1430 .read = seq_read,
1431 .write = sched_write,
1432 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001433 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001434};
1435
1436#endif
1437
Mike Galbraith5091faa2010-11-30 14:18:03 +01001438#ifdef CONFIG_SCHED_AUTOGROUP
1439/*
1440 * Print out autogroup related information:
1441 */
1442static int sched_autogroup_show(struct seq_file *m, void *v)
1443{
1444 struct inode *inode = m->private;
1445 struct task_struct *p;
1446
1447 p = get_proc_task(inode);
1448 if (!p)
1449 return -ESRCH;
1450 proc_sched_autogroup_show_task(p, m);
1451
1452 put_task_struct(p);
1453
1454 return 0;
1455}
1456
1457static ssize_t
1458sched_autogroup_write(struct file *file, const char __user *buf,
1459 size_t count, loff_t *offset)
1460{
Al Viro496ad9a2013-01-23 17:07:38 -05001461 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001462 struct task_struct *p;
1463 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001464 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001465 int err;
1466
1467 memset(buffer, 0, sizeof(buffer));
1468 if (count > sizeof(buffer) - 1)
1469 count = sizeof(buffer) - 1;
1470 if (copy_from_user(buffer, buf, count))
1471 return -EFAULT;
1472
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001473 err = kstrtoint(strstrip(buffer), 0, &nice);
1474 if (err < 0)
1475 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001476
1477 p = get_proc_task(inode);
1478 if (!p)
1479 return -ESRCH;
1480
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001481 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001482 if (err)
1483 count = err;
1484
1485 put_task_struct(p);
1486
1487 return count;
1488}
1489
1490static int sched_autogroup_open(struct inode *inode, struct file *filp)
1491{
1492 int ret;
1493
1494 ret = single_open(filp, sched_autogroup_show, NULL);
1495 if (!ret) {
1496 struct seq_file *m = filp->private_data;
1497
1498 m->private = inode;
1499 }
1500 return ret;
1501}
1502
1503static const struct file_operations proc_pid_sched_autogroup_operations = {
1504 .open = sched_autogroup_open,
1505 .read = seq_read,
1506 .write = sched_autogroup_write,
1507 .llseek = seq_lseek,
1508 .release = single_release,
1509};
1510
1511#endif /* CONFIG_SCHED_AUTOGROUP */
1512
john stultz4614a696b2009-12-14 18:00:05 -08001513static ssize_t comm_write(struct file *file, const char __user *buf,
1514 size_t count, loff_t *offset)
1515{
Al Viro496ad9a2013-01-23 17:07:38 -05001516 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001517 struct task_struct *p;
1518 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001519 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001520
1521 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001522 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001523 return -EFAULT;
1524
1525 p = get_proc_task(inode);
1526 if (!p)
1527 return -ESRCH;
1528
1529 if (same_thread_group(current, p))
1530 set_task_comm(p, buffer);
1531 else
1532 count = -EINVAL;
1533
1534 put_task_struct(p);
1535
1536 return count;
1537}
1538
1539static int comm_show(struct seq_file *m, void *v)
1540{
1541 struct inode *inode = m->private;
1542 struct task_struct *p;
1543
1544 p = get_proc_task(inode);
1545 if (!p)
1546 return -ESRCH;
1547
1548 task_lock(p);
1549 seq_printf(m, "%s\n", p->comm);
1550 task_unlock(p);
1551
1552 put_task_struct(p);
1553
1554 return 0;
1555}
1556
1557static int comm_open(struct inode *inode, struct file *filp)
1558{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001559 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001560}
1561
1562static const struct file_operations proc_pid_set_comm_operations = {
1563 .open = comm_open,
1564 .read = seq_read,
1565 .write = comm_write,
1566 .llseek = seq_lseek,
1567 .release = single_release,
1568};
1569
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001570static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001571{
1572 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001573 struct file *exe_file;
1574
David Howells2b0143b2015-03-17 22:25:59 +00001575 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001576 if (!task)
1577 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001578 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001579 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001580 if (exe_file) {
1581 *exe_path = exe_file->f_path;
1582 path_get(&exe_file->f_path);
1583 fput(exe_file);
1584 return 0;
1585 } else
1586 return -ENOENT;
1587}
1588
Al Viro6b255392015-11-17 10:20:54 -05001589static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001590 struct inode *inode,
1591 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001593 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int error = -EACCES;
1595
Al Viro6b255392015-11-17 10:20:54 -05001596 if (!dentry)
1597 return ERR_PTR(-ECHILD);
1598
Eric W. Biederman778c1142006-06-26 00:25:58 -07001599 /* Are we allowed to snoop on the tasks file descriptors? */
1600 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Christoph Hellwig408ef012012-06-18 10:47:03 -04001603 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1604 if (error)
1605 goto out;
1606
Al Viro6e77137b2015-05-02 13:37:52 -04001607 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001608 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609out:
Al Viro008b1502005-08-20 00:17:39 +01001610 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001613static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Mel Gormane12ba742007-10-16 01:25:52 -07001615 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001616 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int len;
1618
1619 if (!tmp)
1620 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001621
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001622 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001623 len = PTR_ERR(pathname);
1624 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001626 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 if (len > buflen)
1629 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001630 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 len = -EFAULT;
1632 out:
1633 free_page((unsigned long)tmp);
1634 return len;
1635}
1636
1637static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1638{
1639 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001640 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001641 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Eric W. Biederman778c1142006-06-26 00:25:58 -07001643 /* Are we allowed to snoop on the tasks file descriptors? */
1644 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001647 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (error)
1649 goto out;
1650
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001651 error = do_proc_readlink(&path, buffer, buflen);
1652 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return error;
1655}
1656
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001657const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001659 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001660 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661};
1662
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001663
1664/* building an inode */
1665
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001666struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001667{
1668 struct inode * inode;
1669 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001670 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001671
1672 /* We need a new inode */
1673
1674 inode = new_inode(sb);
1675 if (!inode)
1676 goto out;
1677
1678 /* Common stuff */
1679 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001680 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001681 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001682 inode->i_op = &proc_def_inode_operations;
1683
1684 /*
1685 * grab the reference to task.
1686 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001687 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001688 if (!ei->pid)
1689 goto out_unlock;
1690
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001691 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001692 rcu_read_lock();
1693 cred = __task_cred(task);
1694 inode->i_uid = cred->euid;
1695 inode->i_gid = cred->egid;
1696 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001697 }
1698 security_task_to_inode(task, inode);
1699
1700out:
1701 return inode;
1702
1703out_unlock:
1704 iput(inode);
1705 return NULL;
1706}
1707
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001708int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001709{
David Howells2b0143b2015-03-17 22:25:59 +00001710 struct inode *inode = d_inode(dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001711 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001712 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001713 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001714
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001715 generic_fillattr(inode, stat);
1716
1717 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001718 stat->uid = GLOBAL_ROOT_UID;
1719 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001720 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1721 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001722 if (!has_pid_permissions(pid, task, 2)) {
1723 rcu_read_unlock();
1724 /*
1725 * This doesn't prevent learning whether PID exists,
1726 * it only makes getattr() consistent with readdir().
1727 */
1728 return -ENOENT;
1729 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001730 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1731 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001732 cred = __task_cred(task);
1733 stat->uid = cred->euid;
1734 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001735 }
1736 }
1737 rcu_read_unlock();
1738 return 0;
1739}
1740
1741/* dentry stuff */
1742
1743/*
1744 * Exceptional case: normally we are not allowed to unhash a busy
1745 * directory. In this case, however, we can do it - no aliasing problems
1746 * due to the way we treat inodes.
1747 *
1748 * Rewrite the inode's ownerships here because the owning task may have
1749 * performed a setuid(), etc.
1750 *
1751 * Before the /proc/pid/status file was created the only way to read
1752 * the effective uid of a /process was to stat /proc/pid. Reading
1753 * /proc/pid/status is slow enough that procps and other packages
1754 * kept stating /proc/pid. To keep the rules in /proc simple I have
1755 * made this apply to all per process world readable and executable
1756 * directories.
1757 */
Al Viro0b728e12012-06-10 16:03:43 -04001758int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001759{
Nick Piggin34286d62011-01-07 17:49:57 +11001760 struct inode *inode;
1761 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001762 const struct cred *cred;
1763
Al Viro0b728e12012-06-10 16:03:43 -04001764 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001765 return -ECHILD;
1766
David Howells2b0143b2015-03-17 22:25:59 +00001767 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001768 task = get_proc_task(inode);
1769
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001770 if (task) {
1771 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1772 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001773 rcu_read_lock();
1774 cred = __task_cred(task);
1775 inode->i_uid = cred->euid;
1776 inode->i_gid = cred->egid;
1777 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001779 inode->i_uid = GLOBAL_ROOT_UID;
1780 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781 }
1782 inode->i_mode &= ~(S_ISUID | S_ISGID);
1783 security_task_to_inode(task, inode);
1784 put_task_struct(task);
1785 return 1;
1786 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001787 return 0;
1788}
1789
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001790static inline bool proc_inode_is_dead(struct inode *inode)
1791{
1792 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1793}
1794
David Howells1dd704b2013-04-12 01:08:50 +01001795int pid_delete_dentry(const struct dentry *dentry)
1796{
1797 /* Is the task we represent dead?
1798 * If so, then don't put the dentry on the lru list,
1799 * kill it immediately.
1800 */
David Howells2b0143b2015-03-17 22:25:59 +00001801 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001802}
1803
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001804const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001805{
1806 .d_revalidate = pid_revalidate,
1807 .d_delete = pid_delete_dentry,
1808};
1809
1810/* Lookups */
1811
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001812/*
1813 * Fill a directory entry.
1814 *
1815 * If possible create the dcache entry and derive our inode number and
1816 * file type from dcache entry.
1817 *
1818 * Since all of the proc inode numbers are dynamically generated, the inode
1819 * numbers do not exist until the inode is cache. This means creating the
1820 * the dcache entry in readdir is necessary to keep the inode numbers
1821 * reported by readdir in sync with the inode numbers reported
1822 * by stat.
1823 */
Al Virof0c3b502013-05-16 12:07:31 -04001824bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001825 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001826 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001827{
Al Virof0c3b502013-05-16 12:07:31 -04001828 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001829 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001830 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001831 unsigned type;
1832 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001833
Al Viro1df98b82013-06-15 11:33:10 +04001834 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001835 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001836 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1837 child = d_alloc_parallel(dir, &qname, &wq);
1838 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001839 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001840 if (d_in_lookup(child)) {
1841 int err = instantiate(d_inode(dir), child, task, ptr);
1842 d_lookup_done(child);
1843 if (err < 0) {
1844 dput(child);
1845 goto end_instantiate;
1846 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001847 }
1848 }
David Howells2b0143b2015-03-17 22:25:59 +00001849 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001850 ino = inode->i_ino;
1851 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001852 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001853 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001854
1855end_instantiate:
1856 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001857}
1858
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001859/*
1860 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1861 * which represent vma start and end addresses.
1862 */
1863static int dname_to_vma_addr(struct dentry *dentry,
1864 unsigned long *start, unsigned long *end)
1865{
1866 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1867 return -EINVAL;
1868
1869 return 0;
1870}
1871
Al Viro0b728e12012-06-10 16:03:43 -04001872static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001873{
1874 unsigned long vm_start, vm_end;
1875 bool exact_vma_exists = false;
1876 struct mm_struct *mm = NULL;
1877 struct task_struct *task;
1878 const struct cred *cred;
1879 struct inode *inode;
1880 int status = 0;
1881
Al Viro0b728e12012-06-10 16:03:43 -04001882 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001883 return -ECHILD;
1884
David Howells2b0143b2015-03-17 22:25:59 +00001885 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001886 task = get_proc_task(inode);
1887 if (!task)
1888 goto out_notask;
1889
Jann Horncaaee622016-01-20 15:00:04 -08001890 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001891 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001892 goto out;
1893
1894 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1895 down_read(&mm->mmap_sem);
1896 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1897 up_read(&mm->mmap_sem);
1898 }
1899
1900 mmput(mm);
1901
1902 if (exact_vma_exists) {
1903 if (task_dumpable(task)) {
1904 rcu_read_lock();
1905 cred = __task_cred(task);
1906 inode->i_uid = cred->euid;
1907 inode->i_gid = cred->egid;
1908 rcu_read_unlock();
1909 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001910 inode->i_uid = GLOBAL_ROOT_UID;
1911 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001912 }
1913 security_task_to_inode(task, inode);
1914 status = 1;
1915 }
1916
1917out:
1918 put_task_struct(task);
1919
1920out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001921 return status;
1922}
1923
1924static const struct dentry_operations tid_map_files_dentry_operations = {
1925 .d_revalidate = map_files_d_revalidate,
1926 .d_delete = pid_delete_dentry,
1927};
1928
Al Viro6b255392015-11-17 10:20:54 -05001929static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001930{
1931 unsigned long vm_start, vm_end;
1932 struct vm_area_struct *vma;
1933 struct task_struct *task;
1934 struct mm_struct *mm;
1935 int rc;
1936
1937 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001938 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001939 if (!task)
1940 goto out;
1941
1942 mm = get_task_mm(task);
1943 put_task_struct(task);
1944 if (!mm)
1945 goto out;
1946
1947 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1948 if (rc)
1949 goto out_mmput;
1950
Artem Fetishev70335ab2014-03-10 15:49:45 -07001951 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001952 down_read(&mm->mmap_sem);
1953 vma = find_exact_vma(mm, vm_start, vm_end);
1954 if (vma && vma->vm_file) {
1955 *path = vma->vm_file->f_path;
1956 path_get(path);
1957 rc = 0;
1958 }
1959 up_read(&mm->mmap_sem);
1960
1961out_mmput:
1962 mmput(mm);
1963out:
1964 return rc;
1965}
1966
1967struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001968 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001969 unsigned long len;
1970 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1971};
1972
Calvin Owensbdb4d102015-09-09 15:35:54 -07001973/*
1974 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1975 * symlinks may be used to bypass permissions on ancestor directories in the
1976 * path to the file in question.
1977 */
1978static const char *
Al Viro6b255392015-11-17 10:20:54 -05001979proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001980 struct inode *inode,
1981 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07001982{
1983 if (!capable(CAP_SYS_ADMIN))
1984 return ERR_PTR(-EPERM);
1985
Al Virofceef392015-12-29 15:58:39 -05001986 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07001987}
1988
1989/*
Al Viro6b255392015-11-17 10:20:54 -05001990 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07001991 */
1992static const struct inode_operations proc_map_files_link_inode_operations = {
1993 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001994 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07001995 .setattr = proc_setattr,
1996};
1997
Al Viroc52a47a2013-06-15 11:15:20 +04001998static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001999proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2000 struct task_struct *task, const void *ptr)
2001{
Al Viro7b540d02012-08-27 14:55:26 -04002002 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002003 struct proc_inode *ei;
2004 struct inode *inode;
2005
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002006 inode = proc_pid_make_inode(dir->i_sb, task);
2007 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002008 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002009
2010 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002011 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002012
Calvin Owensbdb4d102015-09-09 15:35:54 -07002013 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002014 inode->i_size = 64;
2015 inode->i_mode = S_IFLNK;
2016
Al Viro7b540d02012-08-27 14:55:26 -04002017 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002018 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04002019 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002020 inode->i_mode |= S_IWUSR;
2021
2022 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2023 d_add(dentry, inode);
2024
Al Viroc52a47a2013-06-15 11:15:20 +04002025 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002026}
2027
2028static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002029 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002030{
2031 unsigned long vm_start, vm_end;
2032 struct vm_area_struct *vma;
2033 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002034 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002035 struct mm_struct *mm;
2036
Al Viroc52a47a2013-06-15 11:15:20 +04002037 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002038 task = get_proc_task(dir);
2039 if (!task)
2040 goto out;
2041
Al Viroc52a47a2013-06-15 11:15:20 +04002042 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002043 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002044 goto out_put_task;
2045
Al Viroc52a47a2013-06-15 11:15:20 +04002046 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002047 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002048 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002049
2050 mm = get_task_mm(task);
2051 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002052 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002053
2054 down_read(&mm->mmap_sem);
2055 vma = find_exact_vma(mm, vm_start, vm_end);
2056 if (!vma)
2057 goto out_no_vma;
2058
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002059 if (vma->vm_file)
2060 result = proc_map_files_instantiate(dir, dentry, task,
2061 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002062
2063out_no_vma:
2064 up_read(&mm->mmap_sem);
2065 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002066out_put_task:
2067 put_task_struct(task);
2068out:
Al Viroc52a47a2013-06-15 11:15:20 +04002069 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002070}
2071
2072static const struct inode_operations proc_map_files_inode_operations = {
2073 .lookup = proc_map_files_lookup,
2074 .permission = proc_fd_permission,
2075 .setattr = proc_setattr,
2076};
2077
2078static int
Al Virof0c3b502013-05-16 12:07:31 -04002079proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002080{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081 struct vm_area_struct *vma;
2082 struct task_struct *task;
2083 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002084 unsigned long nr_files, pos, i;
2085 struct flex_array *fa = NULL;
2086 struct map_files_info info;
2087 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002088 int ret;
2089
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002090 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002091 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002092 if (!task)
2093 goto out;
2094
2095 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002096 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002097 goto out_put_task;
2098
2099 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002100 if (!dir_emit_dots(file, ctx))
2101 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002102
Al Virof0c3b502013-05-16 12:07:31 -04002103 mm = get_task_mm(task);
2104 if (!mm)
2105 goto out_put_task;
2106 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002107
Al Virof0c3b502013-05-16 12:07:31 -04002108 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002109
Al Virof0c3b502013-05-16 12:07:31 -04002110 /*
2111 * We need two passes here:
2112 *
2113 * 1) Collect vmas of mapped files with mmap_sem taken
2114 * 2) Release mmap_sem and instantiate entries
2115 *
2116 * otherwise we get lockdep complained, since filldir()
2117 * routine might require mmap_sem taken in might_fault().
2118 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002119
Al Virof0c3b502013-05-16 12:07:31 -04002120 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2121 if (vma->vm_file && ++pos > ctx->pos)
2122 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002123 }
Al Virof0c3b502013-05-16 12:07:31 -04002124
2125 if (nr_files) {
2126 fa = flex_array_alloc(sizeof(info), nr_files,
2127 GFP_KERNEL);
2128 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2129 GFP_KERNEL)) {
2130 ret = -ENOMEM;
2131 if (fa)
2132 flex_array_free(fa);
2133 up_read(&mm->mmap_sem);
2134 mmput(mm);
2135 goto out_put_task;
2136 }
2137 for (i = 0, vma = mm->mmap, pos = 2; vma;
2138 vma = vma->vm_next) {
2139 if (!vma->vm_file)
2140 continue;
2141 if (++pos <= ctx->pos)
2142 continue;
2143
2144 info.mode = vma->vm_file->f_mode;
2145 info.len = snprintf(info.name,
2146 sizeof(info.name), "%lx-%lx",
2147 vma->vm_start, vma->vm_end);
2148 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2149 BUG();
2150 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002151 }
Al Virof0c3b502013-05-16 12:07:31 -04002152 up_read(&mm->mmap_sem);
2153
2154 for (i = 0; i < nr_files; i++) {
2155 p = flex_array_get(fa, i);
2156 if (!proc_fill_cache(file, ctx,
2157 p->name, p->len,
2158 proc_map_files_instantiate,
2159 task,
2160 (void *)(unsigned long)p->mode))
2161 break;
2162 ctx->pos++;
2163 }
2164 if (fa)
2165 flex_array_free(fa);
2166 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002167
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002168out_put_task:
2169 put_task_struct(task);
2170out:
2171 return ret;
2172}
2173
2174static const struct file_operations proc_map_files_operations = {
2175 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002176 .iterate_shared = proc_map_files_readdir,
2177 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002178};
2179
Eric Engestromb5946be2016-03-17 14:20:57 -07002180#ifdef CONFIG_CHECKPOINT_RESTORE
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002181struct timers_private {
2182 struct pid *pid;
2183 struct task_struct *task;
2184 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002185 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002186 unsigned long flags;
2187};
2188
2189static void *timers_start(struct seq_file *m, loff_t *pos)
2190{
2191 struct timers_private *tp = m->private;
2192
2193 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2194 if (!tp->task)
2195 return ERR_PTR(-ESRCH);
2196
2197 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2198 if (!tp->sighand)
2199 return ERR_PTR(-ESRCH);
2200
2201 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2202}
2203
2204static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2205{
2206 struct timers_private *tp = m->private;
2207 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2208}
2209
2210static void timers_stop(struct seq_file *m, void *v)
2211{
2212 struct timers_private *tp = m->private;
2213
2214 if (tp->sighand) {
2215 unlock_task_sighand(tp->task, &tp->flags);
2216 tp->sighand = NULL;
2217 }
2218
2219 if (tp->task) {
2220 put_task_struct(tp->task);
2221 tp->task = NULL;
2222 }
2223}
2224
2225static int show_timer(struct seq_file *m, void *v)
2226{
2227 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002228 struct timers_private *tp = m->private;
2229 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002230 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002231 [SIGEV_SIGNAL] = "signal",
2232 [SIGEV_NONE] = "none",
2233 [SIGEV_THREAD] = "thread",
2234 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002235
2236 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002237 notify = timer->it_sigev_notify;
2238
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002239 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002240 seq_printf(m, "signal: %d/%p\n",
2241 timer->sigq->info.si_signo,
2242 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002243 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002244 nstr[notify & ~SIGEV_THREAD_ID],
2245 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2246 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002247 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002248
2249 return 0;
2250}
2251
2252static const struct seq_operations proc_timers_seq_ops = {
2253 .start = timers_start,
2254 .next = timers_next,
2255 .stop = timers_stop,
2256 .show = show_timer,
2257};
2258
2259static int proc_timers_open(struct inode *inode, struct file *file)
2260{
2261 struct timers_private *tp;
2262
2263 tp = __seq_open_private(file, &proc_timers_seq_ops,
2264 sizeof(struct timers_private));
2265 if (!tp)
2266 return -ENOMEM;
2267
2268 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002269 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002270 return 0;
2271}
2272
2273static const struct file_operations proc_timers_operations = {
2274 .open = proc_timers_open,
2275 .read = seq_read,
2276 .llseek = seq_lseek,
2277 .release = seq_release_private,
2278};
Eric Engestromb5946be2016-03-17 14:20:57 -07002279#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002280
John Stultz5de23d42016-03-17 14:20:54 -07002281static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2282 size_t count, loff_t *offset)
2283{
2284 struct inode *inode = file_inode(file);
2285 struct task_struct *p;
2286 u64 slack_ns;
2287 int err;
2288
2289 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2290 if (err < 0)
2291 return err;
2292
2293 p = get_proc_task(inode);
2294 if (!p)
2295 return -ESRCH;
2296
John Stultz4b2bd5f2016-10-07 17:02:33 -07002297 if (p != current) {
2298 if (!capable(CAP_SYS_NICE)) {
2299 count = -EPERM;
2300 goto out;
2301 }
John Stultz5de23d42016-03-17 14:20:54 -07002302
John Stultz4b2bd5f2016-10-07 17:02:33 -07002303 err = security_task_setscheduler(p);
2304 if (err) {
2305 count = err;
2306 goto out;
2307 }
John Stultz904763e2016-10-07 17:02:29 -07002308 }
2309
John Stultz7abbaf92016-10-07 17:02:26 -07002310 task_lock(p);
2311 if (slack_ns == 0)
2312 p->timer_slack_ns = p->default_timer_slack_ns;
2313 else
2314 p->timer_slack_ns = slack_ns;
2315 task_unlock(p);
2316
2317out:
John Stultz5de23d42016-03-17 14:20:54 -07002318 put_task_struct(p);
2319
2320 return count;
2321}
2322
2323static int timerslack_ns_show(struct seq_file *m, void *v)
2324{
2325 struct inode *inode = m->private;
2326 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002327 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002328
2329 p = get_proc_task(inode);
2330 if (!p)
2331 return -ESRCH;
2332
John Stultz4b2bd5f2016-10-07 17:02:33 -07002333 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002334
John Stultz4b2bd5f2016-10-07 17:02:33 -07002335 if (!capable(CAP_SYS_NICE)) {
2336 err = -EPERM;
2337 goto out;
2338 }
2339 err = security_task_getscheduler(p);
2340 if (err)
2341 goto out;
2342 }
John Stultz904763e2016-10-07 17:02:29 -07002343
John Stultz7abbaf92016-10-07 17:02:26 -07002344 task_lock(p);
2345 seq_printf(m, "%llu\n", p->timer_slack_ns);
2346 task_unlock(p);
2347
2348out:
John Stultz5de23d42016-03-17 14:20:54 -07002349 put_task_struct(p);
2350
2351 return err;
2352}
2353
2354static int timerslack_ns_open(struct inode *inode, struct file *filp)
2355{
2356 return single_open(filp, timerslack_ns_show, inode);
2357}
2358
2359static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2360 .open = timerslack_ns_open,
2361 .read = seq_read,
2362 .write = timerslack_ns_write,
2363 .llseek = seq_lseek,
2364 .release = single_release,
2365};
2366
Al Viroc52a47a2013-06-15 11:15:20 +04002367static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002368 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002369{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002370 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002371 struct inode *inode;
2372 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002373
Eric W. Biederman61a28782006-10-02 02:18:49 -07002374 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002375 if (!inode)
2376 goto out;
2377
2378 ei = PROC_I(inode);
2379 inode->i_mode = p->mode;
2380 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002381 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002382 if (p->iop)
2383 inode->i_op = p->iop;
2384 if (p->fop)
2385 inode->i_fop = p->fop;
2386 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002387 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002388 d_add(dentry, inode);
2389 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002390 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002391 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002392out:
Al Viroc52a47a2013-06-15 11:15:20 +04002393 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002394}
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396static struct dentry *proc_pident_lookup(struct inode *dir,
2397 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002398 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002399 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Al Viroc52a47a2013-06-15 11:15:20 +04002401 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002402 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002403 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Al Viroc52a47a2013-06-15 11:15:20 +04002405 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Eric W. Biederman99f89552006-06-26 00:25:55 -07002407 if (!task)
2408 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002410 /*
2411 * Yes, it does not scale. And it should not. Don't add
2412 * new entries into /proc/<tgid>/ without very good reasons.
2413 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002414 last = &ents[nents - 1];
2415 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (p->len != dentry->d_name.len)
2417 continue;
2418 if (!memcmp(dentry->d_name.name, p->name, p->len))
2419 break;
2420 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002421 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto out;
2423
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002424 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002426 put_task_struct(task);
2427out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002428 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Al Virof0c3b502013-05-16 12:07:31 -04002431static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002432 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002433{
Al Virof0c3b502013-05-16 12:07:31 -04002434 struct task_struct *task = get_proc_task(file_inode(file));
2435 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002436
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002437 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002438 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002439
Al Virof0c3b502013-05-16 12:07:31 -04002440 if (!dir_emit_dots(file, ctx))
2441 goto out;
2442
2443 if (ctx->pos >= nents + 2)
2444 goto out;
2445
2446 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2447 if (!proc_fill_cache(file, ctx, p->name, p->len,
2448 proc_pident_instantiate, task, p))
2449 break;
2450 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002451 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002452out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002453 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002458static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2459 size_t count, loff_t *ppos)
2460{
Al Viro496ad9a2013-01-23 17:07:38 -05002461 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002462 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002463 ssize_t length;
2464 struct task_struct *task = get_proc_task(inode);
2465
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002466 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002467 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002468
2469 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002470 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002471 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002472 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002473 if (length > 0)
2474 length = simple_read_from_buffer(buf, count, ppos, p, length);
2475 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002476 return length;
2477}
2478
2479static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2480 size_t count, loff_t *ppos)
2481{
Al Viro496ad9a2013-01-23 17:07:38 -05002482 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002483 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002484 ssize_t length;
2485 struct task_struct *task = get_proc_task(inode);
2486
2487 length = -ESRCH;
2488 if (!task)
2489 goto out_no_task;
2490 if (count > PAGE_SIZE)
2491 count = PAGE_SIZE;
2492
2493 /* No partial writes. */
2494 length = -EINVAL;
2495 if (*ppos != 0)
2496 goto out;
2497
Al Virobb646cd2015-12-24 00:16:30 -05002498 page = memdup_user(buf, count);
2499 if (IS_ERR(page)) {
2500 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002501 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002502 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002503
David Howells107db7c2009-05-08 13:55:27 +01002504 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002505 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002506 if (length < 0)
2507 goto out_free;
2508
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002509 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002510 (char*)file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002511 page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002512 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002513out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002514 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002515out:
2516 put_task_struct(task);
2517out_no_task:
2518 return length;
2519}
2520
Arjan van de Ven00977a52007-02-12 00:55:34 -08002521static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522 .read = proc_pid_attr_read,
2523 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002524 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002525};
2526
Eric Dumazetc5141e62007-05-08 00:26:15 -07002527static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002528 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2529 REG("prev", S_IRUGO, proc_pid_attr_operations),
2530 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2531 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2532 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2533 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002534};
2535
Al Virof0c3b502013-05-16 12:07:31 -04002536static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Al Virof0c3b502013-05-16 12:07:31 -04002538 return proc_pident_readdir(file, ctx,
2539 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
Arjan van de Ven00977a52007-02-12 00:55:34 -08002542static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002544 .iterate_shared = proc_attr_dir_readdir,
2545 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546};
2547
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002548static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002549 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002551 return proc_pident_lookup(dir, dentry,
2552 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002555static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002556 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002557 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002558 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559};
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561#endif
2562
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002563#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002564static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2565 size_t count, loff_t *ppos)
2566{
Al Viro496ad9a2013-01-23 17:07:38 -05002567 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002568 struct mm_struct *mm;
2569 char buffer[PROC_NUMBUF];
2570 size_t len;
2571 int ret;
2572
2573 if (!task)
2574 return -ESRCH;
2575
2576 ret = 0;
2577 mm = get_task_mm(task);
2578 if (mm) {
2579 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2580 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2581 MMF_DUMP_FILTER_SHIFT));
2582 mmput(mm);
2583 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2584 }
2585
2586 put_task_struct(task);
2587
2588 return ret;
2589}
2590
2591static ssize_t proc_coredump_filter_write(struct file *file,
2592 const char __user *buf,
2593 size_t count,
2594 loff_t *ppos)
2595{
2596 struct task_struct *task;
2597 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002598 unsigned int val;
2599 int ret;
2600 int i;
2601 unsigned long mask;
2602
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002603 ret = kstrtouint_from_user(buf, count, 0, &val);
2604 if (ret < 0)
2605 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002606
2607 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002608 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002609 if (!task)
2610 goto out_no_task;
2611
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002612 mm = get_task_mm(task);
2613 if (!mm)
2614 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002615 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002616
2617 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2618 if (val & mask)
2619 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2620 else
2621 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2622 }
2623
2624 mmput(mm);
2625 out_no_mm:
2626 put_task_struct(task);
2627 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002628 if (ret < 0)
2629 return ret;
2630 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002631}
2632
2633static const struct file_operations proc_coredump_filter_operations = {
2634 .read = proc_coredump_filter_read,
2635 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002636 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002637};
2638#endif
2639
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002640#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002641static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002642{
Andrea Righi940389b2008-07-28 00:48:12 +02002643 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002644 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002645 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002646
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002647 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2648 if (result)
2649 return result;
2650
Jann Horncaaee622016-01-20 15:00:04 -08002651 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002652 result = -EACCES;
2653 goto out_unlock;
2654 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002655
Andrea Righi59954772008-07-27 17:29:15 +02002656 if (whole && lock_task_sighand(task, &flags)) {
2657 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002658
Andrea Righi59954772008-07-27 17:29:15 +02002659 task_io_accounting_add(&acct, &task->signal->ioac);
2660 while_each_thread(task, t)
2661 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002662
Andrea Righi59954772008-07-27 17:29:15 +02002663 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002664 }
Joe Perches25ce3192015-04-15 16:18:17 -07002665 seq_printf(m,
2666 "rchar: %llu\n"
2667 "wchar: %llu\n"
2668 "syscr: %llu\n"
2669 "syscw: %llu\n"
2670 "read_bytes: %llu\n"
2671 "write_bytes: %llu\n"
2672 "cancelled_write_bytes: %llu\n",
2673 (unsigned long long)acct.rchar,
2674 (unsigned long long)acct.wchar,
2675 (unsigned long long)acct.syscr,
2676 (unsigned long long)acct.syscw,
2677 (unsigned long long)acct.read_bytes,
2678 (unsigned long long)acct.write_bytes,
2679 (unsigned long long)acct.cancelled_write_bytes);
2680 result = 0;
2681
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002682out_unlock:
2683 mutex_unlock(&task->signal->cred_guard_mutex);
2684 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002685}
Andrea Righi297c5d92008-07-25 01:48:49 -07002686
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002687static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2688 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002689{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002690 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002691}
2692
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002693static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2694 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002695{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002696 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002697}
2698#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002699
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002700#ifdef CONFIG_USER_NS
2701static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002702 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002703{
2704 struct user_namespace *ns = NULL;
2705 struct task_struct *task;
2706 struct seq_file *seq;
2707 int ret = -EINVAL;
2708
2709 task = get_proc_task(inode);
2710 if (task) {
2711 rcu_read_lock();
2712 ns = get_user_ns(task_cred_xxx(task, user_ns));
2713 rcu_read_unlock();
2714 put_task_struct(task);
2715 }
2716 if (!ns)
2717 goto err;
2718
2719 ret = seq_open(file, seq_ops);
2720 if (ret)
2721 goto err_put_ns;
2722
2723 seq = file->private_data;
2724 seq->private = ns;
2725
2726 return 0;
2727err_put_ns:
2728 put_user_ns(ns);
2729err:
2730 return ret;
2731}
2732
2733static int proc_id_map_release(struct inode *inode, struct file *file)
2734{
2735 struct seq_file *seq = file->private_data;
2736 struct user_namespace *ns = seq->private;
2737 put_user_ns(ns);
2738 return seq_release(inode, file);
2739}
2740
2741static int proc_uid_map_open(struct inode *inode, struct file *file)
2742{
2743 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2744}
2745
2746static int proc_gid_map_open(struct inode *inode, struct file *file)
2747{
2748 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2749}
2750
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002751static int proc_projid_map_open(struct inode *inode, struct file *file)
2752{
2753 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2754}
2755
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002756static const struct file_operations proc_uid_map_operations = {
2757 .open = proc_uid_map_open,
2758 .write = proc_uid_map_write,
2759 .read = seq_read,
2760 .llseek = seq_lseek,
2761 .release = proc_id_map_release,
2762};
2763
2764static const struct file_operations proc_gid_map_operations = {
2765 .open = proc_gid_map_open,
2766 .write = proc_gid_map_write,
2767 .read = seq_read,
2768 .llseek = seq_lseek,
2769 .release = proc_id_map_release,
2770};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002771
2772static const struct file_operations proc_projid_map_operations = {
2773 .open = proc_projid_map_open,
2774 .write = proc_projid_map_write,
2775 .read = seq_read,
2776 .llseek = seq_lseek,
2777 .release = proc_id_map_release,
2778};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002779
2780static int proc_setgroups_open(struct inode *inode, struct file *file)
2781{
2782 struct user_namespace *ns = NULL;
2783 struct task_struct *task;
2784 int ret;
2785
2786 ret = -ESRCH;
2787 task = get_proc_task(inode);
2788 if (task) {
2789 rcu_read_lock();
2790 ns = get_user_ns(task_cred_xxx(task, user_ns));
2791 rcu_read_unlock();
2792 put_task_struct(task);
2793 }
2794 if (!ns)
2795 goto err;
2796
2797 if (file->f_mode & FMODE_WRITE) {
2798 ret = -EACCES;
2799 if (!ns_capable(ns, CAP_SYS_ADMIN))
2800 goto err_put_ns;
2801 }
2802
2803 ret = single_open(file, &proc_setgroups_show, ns);
2804 if (ret)
2805 goto err_put_ns;
2806
2807 return 0;
2808err_put_ns:
2809 put_user_ns(ns);
2810err:
2811 return ret;
2812}
2813
2814static int proc_setgroups_release(struct inode *inode, struct file *file)
2815{
2816 struct seq_file *seq = file->private_data;
2817 struct user_namespace *ns = seq->private;
2818 int ret = single_release(inode, file);
2819 put_user_ns(ns);
2820 return ret;
2821}
2822
2823static const struct file_operations proc_setgroups_operations = {
2824 .open = proc_setgroups_open,
2825 .write = proc_setgroups_write,
2826 .read = seq_read,
2827 .llseek = seq_lseek,
2828 .release = proc_setgroups_release,
2829};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002830#endif /* CONFIG_USER_NS */
2831
Kees Cook47830722008-10-06 03:11:58 +04002832static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2833 struct pid *pid, struct task_struct *task)
2834{
Al Viroa9712bc2011-03-23 15:52:50 -04002835 int err = lock_trace(task);
2836 if (!err) {
2837 seq_printf(m, "%08x\n", task->personality);
2838 unlock_trace(task);
2839 }
2840 return err;
Kees Cook47830722008-10-06 03:11:58 +04002841}
2842
Eric W. Biederman801199c2006-10-02 02:18:48 -07002843/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002844 * Thread groups
2845 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002846static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002847static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002848
Eric Dumazetc5141e62007-05-08 00:26:15 -07002849static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002850 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2851 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002852 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002853 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002854 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002855#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002856 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002857#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002858 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002859 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002860 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002861 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002862 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002863#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002864 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002865#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002866#ifdef CONFIG_SCHED_AUTOGROUP
2867 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2868#endif
john stultz4614a696b2009-12-14 18:00:05 -08002869 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002870#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002871 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002872#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002873 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002874 ONE("stat", S_IRUGO, proc_tgid_stat),
2875 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002876 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002877#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002878 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002879#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002880 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2881 LNK("cwd", proc_cwd_link),
2882 LNK("root", proc_root_link),
2883 LNK("exe", proc_exe_link),
2884 REG("mounts", S_IRUGO, proc_mounts_operations),
2885 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2886 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002887#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002888 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002889 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002890 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002891#endif
2892#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002893 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002894#endif
2895#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002896 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002897#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002898#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002899 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002900#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302901#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002902 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002903#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002904#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002905 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002906#endif
Paul Menage8793d852007-10-18 23:39:39 -07002907#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002908 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002909#endif
Paul Menagea4243162007-10-18 23:39:35 -07002910#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002911 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002912#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002913 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002914 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002915 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002916#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002917 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2918 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002919#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002920#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002921 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002922#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002923#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002924 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002925#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002926#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002927 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002928#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002929#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002930 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002931#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002932#ifdef CONFIG_USER_NS
2933 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2934 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002935 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002936 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002937#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002938#ifdef CONFIG_CHECKPOINT_RESTORE
2939 REG("timers", S_IRUGO, proc_timers_operations),
2940#endif
John Stultz5de23d42016-03-17 14:20:54 -07002941 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002942};
2943
Al Virof0c3b502013-05-16 12:07:31 -04002944static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945{
Al Virof0c3b502013-05-16 12:07:31 -04002946 return proc_pident_readdir(file, ctx,
2947 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002948}
2949
Arjan van de Ven00977a52007-02-12 00:55:34 -08002950static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002951 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002952 .iterate_shared = proc_tgid_base_readdir,
2953 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954};
2955
Al Viro00cd8dd2012-06-10 17:13:09 -04002956static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2957{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002958 return proc_pident_lookup(dir, dentry,
2959 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002960}
2961
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002962static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002963 .lookup = proc_tgid_base_lookup,
2964 .getattr = pid_getattr,
2965 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002966 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002967};
2968
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002969static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002971 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002972 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002973 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Eric W. Biederman48e64842006-06-26 00:25:48 -07002975 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002976 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002977 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002978 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002979 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08002980 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002981 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08002984 if (pid == tgid)
2985 return;
2986
Eric W. Biederman48e64842006-06-26 00:25:48 -07002987 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002988 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2989 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002990 if (!leader)
2991 goto out;
2992
2993 name.name = "task";
2994 name.len = strlen(name.name);
2995 dir = d_hash_and_lookup(leader, &name);
2996 if (!dir)
2997 goto out_put_leader;
2998
2999 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003000 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003001 dentry = d_hash_and_lookup(dir, &name);
3002 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003003 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003004 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003006
3007 dput(dir);
3008out_put_leader:
3009 dput(leader);
3010out:
3011 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
Randy Dunlap0895e912007-10-21 21:00:10 -07003014/**
3015 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3016 * @task: task that should be flushed.
3017 *
3018 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003019 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003020 * in. This call is supposed to do all of this job.
3021 *
3022 * Looks in the dcache for
3023 * /proc/@pid
3024 * /proc/@tgid/task/@pid
3025 * if either directory is present flushes it and all of it'ts children
3026 * from the dcache.
3027 *
3028 * It is safe and reasonable to cache /proc entries for a task until
3029 * that task exits. After that they just clog up the dcache with
3030 * useless entries, possibly causing useful dcache entries to be
3031 * flushed instead. This routine is proved to flush those useless
3032 * dcache entries at process exit time.
3033 *
3034 * NOTE: This routine is just an optimization so it does not guarantee
3035 * that no dcache entries will exist at process exit time it
3036 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003037 */
3038
3039void proc_flush_task(struct task_struct *task)
3040{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003041 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003042 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003043 struct upid *upid;
3044
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003045 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003046 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003047
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003048 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003049 upid = &pid->numbers[i];
3050 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003051 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003052 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003053}
3054
Al Viroc52a47a2013-06-15 11:15:20 +04003055static int proc_pid_instantiate(struct inode *dir,
3056 struct dentry * dentry,
3057 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003058{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003059 struct inode *inode;
3060
Eric W. Biederman61a28782006-10-02 02:18:49 -07003061 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003062 if (!inode)
3063 goto out;
3064
3065 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3066 inode->i_op = &proc_tgid_base_inode_operations;
3067 inode->i_fop = &proc_tgid_base_operations;
3068 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003069
Miklos Szeredibfe86842011-10-28 14:13:29 +02003070 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3071 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003072
Nick Pigginfb045ad2011-01-07 17:49:55 +11003073 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003074
3075 d_add(dentry, inode);
3076 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003077 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003078 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003079out:
Al Viroc52a47a2013-06-15 11:15:20 +04003080 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003081}
3082
Al Viro00cd8dd2012-06-10 17:13:09 -04003083struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003085 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003088 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003090 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (tgid == ~0U)
3092 goto out;
3093
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003094 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003095 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003096 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (task)
3098 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003099 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 if (!task)
3101 goto out;
3102
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003103 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003104 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105out:
Al Viroc52a47a2013-06-15 11:15:20 +04003106 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003110 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003113struct tgid_iter {
3114 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003115 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003116};
3117static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3118{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003119 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003121 if (iter.task)
3122 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003123 rcu_read_lock();
3124retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003125 iter.task = NULL;
3126 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003127 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003128 iter.tgid = pid_nr_ns(pid, ns);
3129 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003130 /* What we to know is if the pid we have find is the
3131 * pid of a thread_group_leader. Testing for task
3132 * being a thread_group_leader is the obvious thing
3133 * todo but there is a window when it fails, due to
3134 * the pid transfer logic in de_thread.
3135 *
3136 * So we perform the straight forward test of seeing
3137 * if the pid we have found is the pid of a thread
3138 * group leader, and don't worry if the task we have
3139 * found doesn't happen to be a thread group leader.
3140 * As we don't care in the case of readdir.
3141 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003142 if (!iter.task || !has_group_leader_pid(iter.task)) {
3143 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003144 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003145 }
3146 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003148 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003149 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
Eric W. Biederman00978752014-07-31 03:10:50 -07003152#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003155int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003157 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003158 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003159 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Al Viro021ada72013-03-29 19:27:05 -04003161 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Eric W. Biederman00978752014-07-31 03:10:50 -07003164 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003165 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003166 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003167 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003168 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003169 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003170 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003171 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003172 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3173 return 0;
3174 ctx->pos = pos = pos + 1;
3175 }
3176 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003177 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003178 for (iter = next_tgid(ns, iter);
3179 iter.task;
3180 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003181 char name[PROC_NUMBUF];
3182 int len;
3183 if (!has_pid_permissions(ns, iter.task, 2))
3184 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003185
Al Virof0c3b502013-05-16 12:07:31 -04003186 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3187 ctx->pos = iter.tgid + TGID_OFFSET;
3188 if (!proc_fill_cache(file, ctx, name, len,
3189 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003190 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
Al Virof0c3b502013-05-16 12:07:31 -04003194 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 return 0;
3196}
3197
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003198/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003199 * proc_tid_comm_permission is a special permission function exclusively
3200 * used for the node /proc/<pid>/task/<tid>/comm.
3201 * It bypasses generic permission checks in the case where a task of the same
3202 * task group attempts to access the node.
3203 * The rationale behind this is that glibc and bionic access this node for
3204 * cross thread naming (pthread_set/getname_np(!self)). However, if
3205 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3206 * which locks out the cross thread naming implementation.
3207 * This function makes sure that the node is always accessible for members of
3208 * same thread group.
3209 */
3210static int proc_tid_comm_permission(struct inode *inode, int mask)
3211{
3212 bool is_same_tgroup;
3213 struct task_struct *task;
3214
3215 task = get_proc_task(inode);
3216 if (!task)
3217 return -ESRCH;
3218 is_same_tgroup = same_thread_group(current, task);
3219 put_task_struct(task);
3220
3221 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3222 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3223 * read or written by the members of the corresponding
3224 * thread group.
3225 */
3226 return 0;
3227 }
3228
3229 return generic_permission(inode, mask);
3230}
3231
3232static const struct inode_operations proc_tid_comm_inode_operations = {
3233 .permission = proc_tid_comm_permission,
3234};
3235
3236/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003237 * Tasks
3238 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003239static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003240 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003241 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003242 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003243#ifdef CONFIG_NET
3244 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3245#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003246 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003247 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003248 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003249 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003250 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003251#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003252 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003253#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003254 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3255 &proc_tid_comm_inode_operations,
3256 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003257#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003258 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003259#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003260 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003261 ONE("stat", S_IRUGO, proc_tid_stat),
3262 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003263 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003264#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003265 REG("children", S_IRUGO, proc_tid_children_operations),
3266#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003267#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003268 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003269#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003270 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3271 LNK("cwd", proc_cwd_link),
3272 LNK("root", proc_root_link),
3273 LNK("exe", proc_exe_link),
3274 REG("mounts", S_IRUGO, proc_mounts_operations),
3275 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003276#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003277 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003278 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003279 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003280#endif
3281#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003282 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003283#endif
3284#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003285 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003286#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003287#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003288 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003289#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303290#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003291 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003292#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003293#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003294 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003295#endif
Paul Menage8793d852007-10-18 23:39:39 -07003296#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003297 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003298#endif
Paul Menagea4243162007-10-18 23:39:35 -07003299#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003300 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003301#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003302 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003303 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003304 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003305#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003306 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003307 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003308#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003309#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003310 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003311#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003312#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003313 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003314#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003315#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003316 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003317#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003318#ifdef CONFIG_USER_NS
3319 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3320 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003321 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003322 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003323#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003324};
3325
Al Virof0c3b502013-05-16 12:07:31 -04003326static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003327{
Al Virof0c3b502013-05-16 12:07:31 -04003328 return proc_pident_readdir(file, ctx,
3329 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003330}
3331
Al Viro00cd8dd2012-06-10 17:13:09 -04003332static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3333{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003334 return proc_pident_lookup(dir, dentry,
3335 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003336}
3337
Arjan van de Ven00977a52007-02-12 00:55:34 -08003338static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003339 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003340 .iterate_shared = proc_tid_base_readdir,
3341 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003342};
3343
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003344static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003345 .lookup = proc_tid_base_lookup,
3346 .getattr = pid_getattr,
3347 .setattr = proc_setattr,
3348};
3349
Al Viroc52a47a2013-06-15 11:15:20 +04003350static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003351 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003352{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003353 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003354 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003355
3356 if (!inode)
3357 goto out;
3358 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3359 inode->i_op = &proc_tid_base_inode_operations;
3360 inode->i_fop = &proc_tid_base_operations;
3361 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003362
Miklos Szeredibfe86842011-10-28 14:13:29 +02003363 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3364 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003365
Nick Pigginfb045ad2011-01-07 17:49:55 +11003366 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003367
3368 d_add(dentry, inode);
3369 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003370 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003371 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003372out:
Al Viroc52a47a2013-06-15 11:15:20 +04003373 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003374}
3375
Al Viro00cd8dd2012-06-10 17:13:09 -04003376static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003377{
Al Viroc52a47a2013-06-15 11:15:20 +04003378 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003379 struct task_struct *task;
3380 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003381 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003382 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003383
3384 if (!leader)
3385 goto out_no_task;
3386
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003387 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003388 if (tid == ~0U)
3389 goto out;
3390
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003391 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003392 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003393 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003394 if (task)
3395 get_task_struct(task);
3396 rcu_read_unlock();
3397 if (!task)
3398 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003399 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003400 goto out_drop_task;
3401
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003402 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003403out_drop_task:
3404 put_task_struct(task);
3405out:
3406 put_task_struct(leader);
3407out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003408 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003409}
3410
3411/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003412 * Find the first tid of a thread group to return to user space.
3413 *
3414 * Usually this is just the thread group leader, but if the users
3415 * buffer was too small or there was a seek into the middle of the
3416 * directory we have more work todo.
3417 *
3418 * In the case of a short read we start with find_task_by_pid.
3419 *
3420 * In the case of a seek we start with the leader and walk nr
3421 * threads past it.
3422 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003423static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3424 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003425{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003426 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003427 unsigned long nr = f_pos;
3428
3429 if (nr != f_pos) /* 32bit overflow? */
3430 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003431
Eric W. Biedermancc288732006-06-26 00:26:01 -07003432 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003433 task = pid_task(pid, PIDTYPE_PID);
3434 if (!task)
3435 goto fail;
3436
3437 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003438 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003439 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003440 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003441 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003442 }
3443
3444 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003445 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003446 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003447
3448 /* If we haven't found our starting place yet start
3449 * with the leader and walk nr threads forward.
3450 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003451 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003452 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003453 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003454 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003455 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003456fail:
3457 pos = NULL;
3458 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003459found:
3460 get_task_struct(pos);
3461out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003462 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003463 return pos;
3464}
3465
3466/*
3467 * Find the next thread in the thread list.
3468 * Return NULL if there is an error or no next thread.
3469 *
3470 * The reference to the input task_struct is released.
3471 */
3472static struct task_struct *next_tid(struct task_struct *start)
3473{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003474 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003475 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003476 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003477 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003478 if (thread_group_leader(pos))
3479 pos = NULL;
3480 else
3481 get_task_struct(pos);
3482 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003483 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003484 put_task_struct(start);
3485 return pos;
3486}
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003489static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003491 struct inode *inode = file_inode(file);
3492 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003493 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003494 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003496 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003497 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Al Virof0c3b502013-05-16 12:07:31 -04003499 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003502 /* f_version caches the tgid value that the last readdir call couldn't
3503 * return. lseek aka telldir automagically resets f_version to 0.
3504 */
Al Viro3aa33772014-10-31 00:42:35 -04003505 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003506 tid = (int)file->f_version;
3507 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003508 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003509 task;
Al Virof0c3b502013-05-16 12:07:31 -04003510 task = next_tid(task), ctx->pos++) {
3511 char name[PROC_NUMBUF];
3512 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003513 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003514 len = snprintf(name, sizeof(name), "%d", tid);
3515 if (!proc_fill_cache(file, ctx, name, len,
3516 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003517 /* returning this tgid failed, save it as the first
3518 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003519 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003520 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003524
Al Virof0c3b502013-05-16 12:07:31 -04003525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003527
3528static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3529{
David Howells2b0143b2015-03-17 22:25:59 +00003530 struct inode *inode = d_inode(dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003531 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003532 generic_fillattr(inode, stat);
3533
Eric W. Biederman99f89552006-06-26 00:25:55 -07003534 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003535 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003536 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003537 }
3538
3539 return 0;
3540}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003541
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003542static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003543 .lookup = proc_task_lookup,
3544 .getattr = proc_task_getattr,
3545 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003546 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003547};
3548
Arjan van de Ven00977a52007-02-12 00:55:34 -08003549static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003550 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003551 .iterate_shared = proc_task_readdir,
3552 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003553};