blob: 89091588a8876629698f9c3de265d5bba2c040be [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>
Connor O'Brien6e7b83d2018-01-31 18:11:57 -080090#include <linux/cpufreq_times.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040091#ifdef CONFIG_HARDWALL
92#include <asm/hardwall.h>
93#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080094#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040096#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Alexey Dobriyane0a1a012018-02-06 15:36:59 -080098#include "../../lib/kstrtox.h"
99
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700100/* NOTE:
101 * Implementing inode permission operations in /proc is almost
102 * certainly an error. Permission checks need to happen during
103 * each system call not at open time. The reason is that most of
104 * what we wish to check for permissions in /proc varies at runtime.
105 *
106 * The classic example of a problem is opening file descriptors
107 * in /proc for a task before it execs a suid executable.
108 */
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700111 const char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700112 int len;
Al Virod161a132011-07-24 03:36:29 -0400113 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800114 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800115 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700121 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 .mode = MODE, \
123 .iop = IOP, \
124 .fop = FOP, \
125 .op = OP, \
126}
127
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300128#define DIR(NAME, MODE, iops, fops) \
129 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
130#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700131 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700132 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300133 { .proc_get_link = get_link } )
134#define REG(NAME, MODE, fops) \
135 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800137 NOD(NAME, (S_IFREG|(MODE)), \
138 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300139 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Vegard Nossumaed54172008-06-05 22:46:53 -0700141/*
142 * Count the number of hardlinks for the pid_entry table, excluding the .
143 * and .. links.
144 */
145static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
146 unsigned int n)
147{
148 unsigned int i;
149 unsigned int count;
150
151 count = 0;
152 for (i = 0; i < n; ++i) {
153 if (S_ISDIR(entries[i].mode))
154 ++count;
155 }
156
157 return count;
158}
159
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200160static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000162 int result = -ENOENT;
163
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700164 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200165 if (task->fs) {
166 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 result = 0;
168 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700169 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000170 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171}
172
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800173static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700174{
David Howells2b0143b2015-03-17 22:25:59 +0000175 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700176 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177
178 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200179 task_lock(task);
180 if (task->fs) {
181 get_fs_pwd(task->fs, path);
182 result = 0;
183 }
184 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return result;
188}
189
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800190static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
David Howells2b0143b2015-03-17 22:25:59 +0000192 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194
195 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200196 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700197 put_task_struct(task);
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return result;
200}
201
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700202static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
203 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700205 struct task_struct *tsk;
206 struct mm_struct *mm;
207 char *page;
208 unsigned long count = _count;
209 unsigned long arg_start, arg_end, env_start, env_end;
210 unsigned long len1, len2, len;
211 unsigned long p;
212 char c;
213 ssize_t rv;
214
215 BUG_ON(*pos < 0);
216
217 tsk = get_proc_task(file_inode(file));
218 if (!tsk)
219 return -ESRCH;
220 mm = get_task_mm(tsk);
221 put_task_struct(tsk);
222 if (!mm)
223 return 0;
224 /* Check if process spawned far enough to have cmdline. */
225 if (!mm->env_end) {
226 rv = 0;
227 goto out_mmput;
228 }
229
230 page = (char *)__get_free_page(GFP_TEMPORARY);
231 if (!page) {
232 rv = -ENOMEM;
233 goto out_mmput;
234 }
235
236 down_read(&mm->mmap_sem);
237 arg_start = mm->arg_start;
238 arg_end = mm->arg_end;
239 env_start = mm->env_start;
240 env_end = mm->env_end;
241 up_read(&mm->mmap_sem);
242
243 BUG_ON(arg_start > arg_end);
244 BUG_ON(env_start > env_end);
245
246 len1 = arg_end - arg_start;
247 len2 = env_end - env_start;
248
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700249 /* Empty ARGV. */
250 if (len1 == 0) {
251 rv = 0;
252 goto out_free_page;
253 }
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700254 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700255 * Inherently racy -- command line shares address space
256 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700257 */
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200258 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700259 if (rv <= 0)
260 goto out_free_page;
261
262 rv = 0;
263
264 if (c == '\0') {
265 /* Command line (set of strings) occupies whole ARGV. */
266 if (len1 <= *pos)
267 goto out_free_page;
268
269 p = arg_start + *pos;
270 len = len1 - *pos;
271 while (count > 0 && len > 0) {
272 unsigned int _count;
273 int nr_read;
274
275 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200276 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700277 if (nr_read < 0)
278 rv = nr_read;
279 if (nr_read <= 0)
280 goto out_free_page;
281
282 if (copy_to_user(buf, page, nr_read)) {
283 rv = -EFAULT;
284 goto out_free_page;
285 }
286
287 p += nr_read;
288 len -= nr_read;
289 buf += nr_read;
290 count -= nr_read;
291 rv += nr_read;
292 }
293 } else {
294 /*
295 * Command line (1 string) occupies ARGV and maybe
296 * extends into ENVP.
297 */
298 if (len1 + len2 <= *pos)
299 goto skip_argv_envp;
300 if (len1 <= *pos)
301 goto skip_argv;
302
303 p = arg_start + *pos;
304 len = len1 - *pos;
305 while (count > 0 && len > 0) {
306 unsigned int _count, l;
307 int nr_read;
308 bool final;
309
310 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200311 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700312 if (nr_read < 0)
313 rv = nr_read;
314 if (nr_read <= 0)
315 goto out_free_page;
316
317 /*
318 * Command line can be shorter than whole ARGV
319 * even if last "marker" byte says it is not.
320 */
321 final = false;
322 l = strnlen(page, nr_read);
323 if (l < nr_read) {
324 nr_read = l;
325 final = true;
326 }
327
328 if (copy_to_user(buf, page, nr_read)) {
329 rv = -EFAULT;
330 goto out_free_page;
331 }
332
333 p += nr_read;
334 len -= nr_read;
335 buf += nr_read;
336 count -= nr_read;
337 rv += nr_read;
338
339 if (final)
340 goto out_free_page;
341 }
342skip_argv:
343 /*
344 * Command line (1 string) occupies ARGV and
345 * extends into ENVP.
346 */
347 if (len1 <= *pos) {
348 p = env_start + *pos - len1;
349 len = len1 + len2 - *pos;
350 } else {
351 p = env_start;
352 len = len2;
353 }
354 while (count > 0 && len > 0) {
355 unsigned int _count, l;
356 int nr_read;
357 bool final;
358
359 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200360 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
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
Jann Horn3c5dc3f2018-10-05 15:51:58 -0700458 /*
459 * The ability to racily run the kernel stack unwinder on a running task
460 * and then observe the unwinder output is scary; while it is useful for
461 * debugging kernel issues, it can also allow an attacker to leak kernel
462 * stack contents.
463 * Doing this in a manner that is at least safe from races would require
464 * some work to ensure that the remote task can not be scheduled; and
465 * even then, this would still expose the unwinder as local attack
466 * surface.
467 * Therefore, this interface is restricted to root.
468 */
469 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
470 return -EACCES;
471
Ken Chen2ec220e2008-11-10 11:26:08 +0300472 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
473 if (!entries)
474 return -ENOMEM;
475
476 trace.nr_entries = 0;
477 trace.max_entries = MAX_STACK_TRACE_DEPTH;
478 trace.entries = entries;
479 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300480
Al Viroa9712bc2011-03-23 15:52:50 -0400481 err = lock_trace(task);
482 if (!err) {
483 save_stack_trace_tsk(task, &trace);
484
485 for (i = 0; i < trace.nr_entries; i++) {
Josh Poimboeuf8b927d72016-08-18 10:59:07 -0500486 seq_printf(m, "[<%pK>] %pB\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400487 (void *)entries[i], (void *)entries[i]);
488 }
489 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300490 }
491 kfree(entries);
492
Al Viroa9712bc2011-03-23 15:52:50 -0400493 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300494}
495#endif
496
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530497#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/*
499 * Provides /proc/PID/schedstat
500 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700501static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
502 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530504 if (unlikely(!sched_info_on()))
505 seq_printf(m, "0 0 0\n");
506 else
507 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700508 (unsigned long long)task->se.sum_exec_runtime,
509 (unsigned long long)task->sched_info.run_delay,
510 task->sched_info.pcount);
511
512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514#endif
515
Arjan van de Ven97455122008-01-25 21:08:34 +0100516#ifdef CONFIG_LATENCYTOP
517static int lstats_show_proc(struct seq_file *m, void *v)
518{
519 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800520 struct inode *inode = m->private;
521 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100522
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800523 if (!task)
524 return -ESRCH;
525 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100526 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800527 struct latency_record *lr = &task->latency_record[i];
528 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100529 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800530 seq_printf(m, "%i %li %li",
531 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100532 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800533 unsigned long bt = lr->backtrace[q];
534 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100535 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800536 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100537 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800538 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100539 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800540 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100541 }
542
543 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800544 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100545 return 0;
546}
547
548static int lstats_open(struct inode *inode, struct file *file)
549{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800550 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800551}
552
Arjan van de Ven97455122008-01-25 21:08:34 +0100553static ssize_t lstats_write(struct file *file, const char __user *buf,
554 size_t count, loff_t *offs)
555{
Al Viro496ad9a2013-01-23 17:07:38 -0500556 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100557
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800558 if (!task)
559 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100560 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800561 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100562
563 return count;
564}
565
566static const struct file_operations proc_lstats_operations = {
567 .open = lstats_open,
568 .read = seq_read,
569 .write = lstats_write,
570 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800571 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100572};
573
574#endif
575
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700576static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
577 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
David Rientjesa7f638f2012-05-29 15:06:47 -0700579 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200580 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Oleg Nesterovef419392016-08-02 14:03:19 -0700582 points = oom_badness(task, NULL, NULL, totalpages) *
583 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700584 seq_printf(m, "%lu\n", points);
585
586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Neil Hormand85f50d2007-10-18 23:40:37 -0700589struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700590 const char *name;
591 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700592};
593
594static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700595 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700596 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
597 [RLIMIT_DATA] = {"Max data size", "bytes"},
598 [RLIMIT_STACK] = {"Max stack size", "bytes"},
599 [RLIMIT_CORE] = {"Max core file size", "bytes"},
600 [RLIMIT_RSS] = {"Max resident set", "bytes"},
601 [RLIMIT_NPROC] = {"Max processes", "processes"},
602 [RLIMIT_NOFILE] = {"Max open files", "files"},
603 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
604 [RLIMIT_AS] = {"Max address space", "bytes"},
605 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
606 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
607 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
608 [RLIMIT_NICE] = {"Max nice priority", NULL},
609 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800610 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700611};
612
613/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700614static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
615 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700616{
617 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700618 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700619
620 struct rlimit rlim[RLIM_NLIMITS];
621
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400622 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700623 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700624 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
625 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700626
627 /*
628 * print the file header
629 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700630 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700631 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700632
633 for (i = 0; i < RLIM_NLIMITS; i++) {
634 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700635 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700636 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700637 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700638 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700639 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700640
641 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700642 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700643 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700644 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700645
646 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700647 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700648 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700649 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700650 }
651
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700652 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700653}
654
Roland McGrathebcb6732008-07-25 19:46:00 -0700655#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700656static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
657 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700658{
659 long nr;
660 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700661 int res;
662
663 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400664 if (res)
665 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700666
667 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700668 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400669 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700670 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400671 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700672 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700673 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
674 nr,
675 args[0], args[1], args[2], args[3], args[4], args[5],
676 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400677 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700678
679 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700680}
681#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/************************************************************************/
684/* Here the fs part begins */
685/************************************************************************/
686
687/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700688static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700690 struct task_struct *task;
691 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700692 /* Allow access to a task's file descriptors if it is us or we
693 * may use ptrace attach to the process and find out that
694 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700695 */
696 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700697 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800698 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700699 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700700 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700701 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800704int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700705{
706 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000707 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700708
709 if (attr->ia_valid & ATTR_MODE)
710 return -EPERM;
711
Jan Kara31051c82016-05-26 16:55:18 +0200712 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200713 if (error)
714 return error;
715
Christoph Hellwig10257742010-06-04 11:30:02 +0200716 setattr_copy(inode, attr);
717 mark_inode_dirty(inode);
718 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700719}
720
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800721/*
722 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
723 * or euid/egid (for hide_pid_min=2)?
724 */
725static bool has_pid_permissions(struct pid_namespace *pid,
726 struct task_struct *task,
727 int hide_pid_min)
728{
729 if (pid->hide_pid < hide_pid_min)
730 return true;
731 if (in_group_p(pid->pid_gid))
732 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800733 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800734}
735
736
737static int proc_pid_permission(struct inode *inode, int mask)
738{
739 struct pid_namespace *pid = inode->i_sb->s_fs_info;
740 struct task_struct *task;
741 bool has_perms;
742
743 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800744 if (!task)
745 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800746 has_perms = has_pid_permissions(pid, task, 1);
747 put_task_struct(task);
748
749 if (!has_perms) {
750 if (pid->hide_pid == 2) {
751 /*
752 * Let's make getdents(), stat(), and open()
753 * consistent with each other. If a process
754 * may not stat() a file, it shouldn't be seen
755 * in procfs at all.
756 */
757 return -ENOENT;
758 }
759
760 return -EPERM;
761 }
762 return generic_permission(inode, mask);
763}
764
765
766
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800767static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700768 .setattr = proc_setattr,
769};
770
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800771static int proc_single_show(struct seq_file *m, void *v)
772{
773 struct inode *inode = m->private;
774 struct pid_namespace *ns;
775 struct pid *pid;
776 struct task_struct *task;
777 int ret;
778
779 ns = inode->i_sb->s_fs_info;
780 pid = proc_pid(inode);
781 task = get_pid_task(pid, PIDTYPE_PID);
782 if (!task)
783 return -ESRCH;
784
785 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
786
787 put_task_struct(task);
788 return ret;
789}
790
791static int proc_single_open(struct inode *inode, struct file *filp)
792{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800793 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800794}
795
796static const struct file_operations proc_single_file_operations = {
797 .open = proc_single_open,
798 .read = seq_read,
799 .llseek = seq_lseek,
800 .release = single_release,
801};
802
Oleg Nesterov5381e162014-10-09 15:25:24 -0700803
804struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
805{
806 struct task_struct *task = get_proc_task(inode);
807 struct mm_struct *mm = ERR_PTR(-ESRCH);
808
809 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800810 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700811 put_task_struct(task);
812
813 if (!IS_ERR_OR_NULL(mm)) {
814 /* ensure this mm_struct can't be freed */
815 atomic_inc(&mm->mm_count);
816 /* but do not pin its memory */
817 mmput(mm);
818 }
819 }
820
821 return mm;
822}
823
Cong Wangb409e572012-05-31 16:26:17 -0700824static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700826 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800827
828 if (IS_ERR(mm))
829 return PTR_ERR(mm);
830
Linus Torvaldse2683372012-01-17 15:21:19 -0800831 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833}
834
Cong Wangb409e572012-05-31 16:26:17 -0700835static int mem_open(struct inode *inode, struct file *file)
836{
Djalal Harounibc452b42012-07-30 14:42:28 -0700837 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
838
839 /* OK to pass negative loff_t, we can catch out-of-range */
840 file->f_mode |= FMODE_UNSIGNED_OFFSET;
841
842 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700843}
844
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100845static ssize_t mem_rw(struct file *file, char __user *buf,
846 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvaldse2683372012-01-17 15:21:19 -0800848 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100849 unsigned long addr = *ppos;
850 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700852 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Linus Torvaldse2683372012-01-17 15:21:19 -0800854 if (!mm)
855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Mel Gormane12ba742007-10-16 01:25:52 -0700857 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800859 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700861 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100862 if (!atomic_inc_not_zero(&mm->mm_users))
863 goto free;
864
Linus Torvalds272ddc82016-10-24 19:00:44 -0700865 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
866 flags = FOLL_FORCE;
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100867 if (write)
868 flags |= FOLL_WRITE;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100871 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100873 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 copied = -EFAULT;
875 break;
876 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100877
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100878 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100879 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!copied)
881 copied = -EIO;
882 break;
883 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100884
885 if (!write && copy_to_user(buf, page, this_len)) {
886 copied = -EFAULT;
887 break;
888 }
889
890 buf += this_len;
891 addr += this_len;
892 copied += this_len;
893 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100895 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700896
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100897 mmput(mm);
898free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700899 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return copied;
901}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100903static ssize_t mem_read(struct file *file, char __user *buf,
904 size_t count, loff_t *ppos)
905{
906 return mem_rw(file, buf, count, ppos, 0);
907}
908
909static ssize_t mem_write(struct file *file, const char __user *buf,
910 size_t count, loff_t *ppos)
911{
912 return mem_rw(file, (char __user*)buf, count, ppos, 1);
913}
914
Matt Mackall85863e42008-02-04 22:29:04 -0800915loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 switch (orig) {
918 case 0:
919 file->f_pos = offset;
920 break;
921 case 1:
922 file->f_pos += offset;
923 break;
924 default:
925 return -EINVAL;
926 }
927 force_successful_syscall_return();
928 return file->f_pos;
929}
930
Linus Torvaldse2683372012-01-17 15:21:19 -0800931static int mem_release(struct inode *inode, struct file *file)
932{
933 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100934 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100935 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800936 return 0;
937}
938
Arjan van de Ven00977a52007-02-12 00:55:34 -0800939static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 .llseek = mem_lseek,
941 .read = mem_read,
942 .write = mem_write,
943 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800944 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945};
946
Cong Wangb409e572012-05-31 16:26:17 -0700947static int environ_open(struct inode *inode, struct file *file)
948{
949 return __mem_open(inode, file, PTRACE_MODE_READ);
950}
951
James Pearson315e28c2007-10-16 23:30:17 -0700952static ssize_t environ_read(struct file *file, char __user *buf,
953 size_t count, loff_t *ppos)
954{
James Pearson315e28c2007-10-16 23:30:17 -0700955 char *page;
956 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700957 int ret = 0;
958 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800959 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700960
Mathias Krause8148a732016-05-05 16:22:26 -0700961 /* Ensure the process spawned far enough to have an environment. */
962 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700963 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700964
James Pearson315e28c2007-10-16 23:30:17 -0700965 page = (char *)__get_free_page(GFP_TEMPORARY);
966 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700967 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700968
Al Virod6f64b82011-02-15 22:26:01 -0500969 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700970 if (!atomic_inc_not_zero(&mm->mm_users))
971 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800972
973 down_read(&mm->mmap_sem);
974 env_start = mm->env_start;
975 env_end = mm->env_end;
976 up_read(&mm->mmap_sem);
977
James Pearson315e28c2007-10-16 23:30:17 -0700978 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700979 size_t this_len, max_len;
980 int retval;
981
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800982 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700983 break;
James Pearson315e28c2007-10-16 23:30:17 -0700984
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800985 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700986
Djalal Harounie8905ec2012-07-30 14:42:26 -0700987 max_len = min_t(size_t, PAGE_SIZE, count);
988 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700989
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200990 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700991
992 if (retval <= 0) {
993 ret = retval;
994 break;
995 }
996
997 if (copy_to_user(buf, page, retval)) {
998 ret = -EFAULT;
999 break;
1000 }
1001
1002 ret += retval;
1003 src += retval;
1004 buf += retval;
1005 count -= retval;
1006 }
1007 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -07001008 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -07001009
1010free:
James Pearson315e28c2007-10-16 23:30:17 -07001011 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -07001012 return ret;
1013}
1014
1015static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -07001016 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -07001017 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001018 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -07001019 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -07001020};
1021
Al Viroc5317162016-10-05 18:43:43 -04001022static int auxv_open(struct inode *inode, struct file *file)
1023{
1024 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1025}
1026
1027static ssize_t auxv_read(struct file *file, char __user *buf,
1028 size_t count, loff_t *ppos)
1029{
1030 struct mm_struct *mm = file->private_data;
1031 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -07001032
1033 if (!mm)
1034 return 0;
Al Viroc5317162016-10-05 18:43:43 -04001035 do {
1036 nwords += 2;
1037 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1038 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1039 nwords * sizeof(mm->saved_auxv[0]));
1040}
1041
1042static const struct file_operations proc_auxv_operations = {
1043 .open = auxv_open,
1044 .read = auxv_read,
1045 .llseek = generic_file_llseek,
1046 .release = mem_release,
1047};
1048
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001049static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1050 loff_t *ppos)
1051{
Al Viro496ad9a2013-01-23 17:07:38 -05001052 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001053 char buffer[PROC_NUMBUF];
1054 int oom_adj = OOM_ADJUST_MIN;
1055 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001056
1057 if (!task)
1058 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001059 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1060 oom_adj = OOM_ADJUST_MAX;
1061 else
1062 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1063 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001064 put_task_struct(task);
1065 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1066 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1067}
1068
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001069static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1070{
1071 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001072 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001073 struct task_struct *task;
1074 int err = 0;
1075
1076 task = get_proc_task(file_inode(file));
1077 if (!task)
1078 return -ESRCH;
1079
1080 mutex_lock(&oom_adj_mutex);
1081 if (legacy) {
1082 if (oom_adj < task->signal->oom_score_adj &&
1083 !capable(CAP_SYS_RESOURCE)) {
1084 err = -EACCES;
1085 goto err_unlock;
1086 }
1087 /*
1088 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1089 * /proc/pid/oom_score_adj instead.
1090 */
1091 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1092 current->comm, task_pid_nr(current), task_pid_nr(task),
1093 task_pid_nr(task));
1094 } else {
1095 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1096 !capable(CAP_SYS_RESOURCE)) {
1097 err = -EACCES;
1098 goto err_unlock;
1099 }
1100 }
1101
Michal Hocko44a70ade2016-07-28 15:44:43 -07001102 /*
1103 * Make sure we will check other processes sharing the mm if this is
1104 * not vfrok which wants its own oom_score_adj.
1105 * pin the mm so it doesn't go away and get reused after task_unlock
1106 */
1107 if (!task->vfork_done) {
1108 struct task_struct *p = find_lock_task_mm(task);
1109
1110 if (p) {
1111 if (atomic_read(&p->mm->mm_users) > 1) {
1112 mm = p->mm;
1113 atomic_inc(&mm->mm_count);
1114 }
1115 task_unlock(p);
1116 }
1117 }
1118
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001119 task->signal->oom_score_adj = oom_adj;
1120 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1121 task->signal->oom_score_adj_min = (short)oom_adj;
1122 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001123
1124 if (mm) {
1125 struct task_struct *p;
1126
1127 rcu_read_lock();
1128 for_each_process(p) {
1129 if (same_thread_group(task, p))
1130 continue;
1131
1132 /* do not touch kernel threads or the global init */
1133 if (p->flags & PF_KTHREAD || is_global_init(p))
1134 continue;
1135
1136 task_lock(p);
1137 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001138 p->signal->oom_score_adj = oom_adj;
1139 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1140 p->signal->oom_score_adj_min = (short)oom_adj;
1141 }
1142 task_unlock(p);
1143 }
1144 rcu_read_unlock();
1145 mmdrop(mm);
1146 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001147err_unlock:
1148 mutex_unlock(&oom_adj_mutex);
1149 put_task_struct(task);
1150 return err;
1151}
Michal Hockof913da52016-07-28 15:44:37 -07001152
David Rientjesb72bdfa2015-11-05 18:50:32 -08001153/*
1154 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1155 * kernels. The effective policy is defined by oom_score_adj, which has a
1156 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1157 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1158 * Processes that become oom disabled via oom_adj will still be oom disabled
1159 * with this implementation.
1160 *
1161 * oom_adj cannot be removed since existing userspace binaries use it.
1162 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001163static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1164 size_t count, loff_t *ppos)
1165{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001166 char buffer[PROC_NUMBUF];
1167 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001168 int err;
1169
1170 memset(buffer, 0, sizeof(buffer));
1171 if (count > sizeof(buffer) - 1)
1172 count = sizeof(buffer) - 1;
1173 if (copy_from_user(buffer, buf, count)) {
1174 err = -EFAULT;
1175 goto out;
1176 }
1177
1178 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1179 if (err)
1180 goto out;
1181 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1182 oom_adj != OOM_DISABLE) {
1183 err = -EINVAL;
1184 goto out;
1185 }
1186
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001187 /*
1188 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1189 * value is always attainable.
1190 */
1191 if (oom_adj == OOM_ADJUST_MAX)
1192 oom_adj = OOM_SCORE_ADJ_MAX;
1193 else
1194 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1195
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001196 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001197out:
1198 return err < 0 ? err : count;
1199}
1200
1201static const struct file_operations proc_oom_adj_operations = {
1202 .read = oom_adj_read,
1203 .write = oom_adj_write,
1204 .llseek = generic_file_llseek,
1205};
1206
David Rientjesa63d83f2010-08-09 17:19:46 -07001207static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1208 size_t count, loff_t *ppos)
1209{
Al Viro496ad9a2013-01-23 17:07:38 -05001210 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001211 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001212 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001213 size_t len;
1214
1215 if (!task)
1216 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001217 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001218 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001219 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001220 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1221}
1222
1223static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1224 size_t count, loff_t *ppos)
1225{
David Rientjesa63d83f2010-08-09 17:19:46 -07001226 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001227 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001228 int err;
1229
1230 memset(buffer, 0, sizeof(buffer));
1231 if (count > sizeof(buffer) - 1)
1232 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001233 if (copy_from_user(buffer, buf, count)) {
1234 err = -EFAULT;
1235 goto out;
1236 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001237
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001238 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001239 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001240 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001241 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001242 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1243 err = -EINVAL;
1244 goto out;
1245 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001246
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001247 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001248out:
1249 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001250}
1251
1252static const struct file_operations proc_oom_score_adj_operations = {
1253 .read = oom_score_adj_read,
1254 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001255 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001256};
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258#ifdef CONFIG_AUDITSYSCALL
1259#define TMPBUFLEN 21
1260static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1261 size_t count, loff_t *ppos)
1262{
Al Viro496ad9a2013-01-23 17:07:38 -05001263 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001264 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 ssize_t length;
1266 char tmpbuf[TMPBUFLEN];
1267
Eric W. Biederman99f89552006-06-26 00:25:55 -07001268 if (!task)
1269 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001271 from_kuid(file->f_cred->user_ns,
1272 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001273 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1275}
1276
1277static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1278 size_t count, loff_t *ppos)
1279{
Al Viro496ad9a2013-01-23 17:07:38 -05001280 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001282 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001283 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001285 rcu_read_lock();
1286 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1287 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001289 }
1290 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (*ppos != 0) {
1293 /* No partial writes. */
1294 return -EINVAL;
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001297 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1298 if (rv < 0)
1299 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001300
1301 /* is userspace tring to explicitly UNSET the loginuid? */
1302 if (loginuid == AUDIT_UID_UNSET) {
1303 kloginuid = INVALID_UID;
1304 } else {
1305 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001306 if (!uid_valid(kloginuid))
1307 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001308 }
1309
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001310 rv = audit_set_loginuid(kloginuid);
1311 if (rv < 0)
1312 return rv;
1313 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Arjan van de Ven00977a52007-02-12 00:55:34 -08001316static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 .read = proc_loginuid_read,
1318 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001319 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320};
Eric Paris1e0bd752008-03-13 08:15:31 -04001321
1322static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1323 size_t count, loff_t *ppos)
1324{
Al Viro496ad9a2013-01-23 17:07:38 -05001325 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001326 struct task_struct *task = get_proc_task(inode);
1327 ssize_t length;
1328 char tmpbuf[TMPBUFLEN];
1329
1330 if (!task)
1331 return -ESRCH;
1332 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1333 audit_get_sessionid(task));
1334 put_task_struct(task);
1335 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1336}
1337
1338static const struct file_operations proc_sessionid_operations = {
1339 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001340 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001341};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342#endif
1343
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001344#ifdef CONFIG_FAULT_INJECTION
1345static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1346 size_t count, loff_t *ppos)
1347{
Al Viro496ad9a2013-01-23 17:07:38 -05001348 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001349 char buffer[PROC_NUMBUF];
1350 size_t len;
1351 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001352
1353 if (!task)
1354 return -ESRCH;
1355 make_it_fail = task->make_it_fail;
1356 put_task_struct(task);
1357
1358 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001359
1360 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001361}
1362
1363static ssize_t proc_fault_inject_write(struct file * file,
1364 const char __user * buf, size_t count, loff_t *ppos)
1365{
1366 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001367 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001368 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001369 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001370
1371 if (!capable(CAP_SYS_RESOURCE))
1372 return -EPERM;
1373 memset(buffer, 0, sizeof(buffer));
1374 if (count > sizeof(buffer) - 1)
1375 count = sizeof(buffer) - 1;
1376 if (copy_from_user(buffer, buf, count))
1377 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001378 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1379 if (rv < 0)
1380 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001381 if (make_it_fail < 0 || make_it_fail > 1)
1382 return -EINVAL;
1383
Al Viro496ad9a2013-01-23 17:07:38 -05001384 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001385 if (!task)
1386 return -ESRCH;
1387 task->make_it_fail = make_it_fail;
1388 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001389
1390 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001391}
1392
Arjan van de Ven00977a52007-02-12 00:55:34 -08001393static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001394 .read = proc_fault_inject_read,
1395 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001396 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001397};
1398#endif
1399
Arjan van de Ven97455122008-01-25 21:08:34 +01001400
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001401#ifdef CONFIG_SCHED_DEBUG
1402/*
1403 * Print out various scheduling related per-task fields:
1404 */
1405static int sched_show(struct seq_file *m, void *v)
1406{
1407 struct inode *inode = m->private;
1408 struct task_struct *p;
1409
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001410 p = get_proc_task(inode);
1411 if (!p)
1412 return -ESRCH;
1413 proc_sched_show_task(p, m);
1414
1415 put_task_struct(p);
1416
1417 return 0;
1418}
1419
1420static ssize_t
1421sched_write(struct file *file, const char __user *buf,
1422 size_t count, loff_t *offset)
1423{
Al Viro496ad9a2013-01-23 17:07:38 -05001424 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001425 struct task_struct *p;
1426
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001427 p = get_proc_task(inode);
1428 if (!p)
1429 return -ESRCH;
1430 proc_sched_set_task(p);
1431
1432 put_task_struct(p);
1433
1434 return count;
1435}
1436
1437static int sched_open(struct inode *inode, struct file *filp)
1438{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001439 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001440}
1441
1442static const struct file_operations proc_pid_sched_operations = {
1443 .open = sched_open,
1444 .read = seq_read,
1445 .write = sched_write,
1446 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001447 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001448};
1449
1450#endif
1451
Pavankumar Kondeti92564312017-03-22 14:49:54 +05301452/*
1453 * Print out various scheduling related per-task fields:
1454 */
1455
1456#ifdef CONFIG_SMP
1457
1458static int sched_wake_up_idle_show(struct seq_file *m, void *v)
1459{
1460 struct inode *inode = m->private;
1461 struct task_struct *p;
1462
1463 p = get_proc_task(inode);
1464 if (!p)
1465 return -ESRCH;
1466
1467 seq_printf(m, "%d\n", sched_get_wake_up_idle(p));
1468
1469 put_task_struct(p);
1470
1471 return 0;
1472}
1473
1474static ssize_t
1475sched_wake_up_idle_write(struct file *file, const char __user *buf,
1476 size_t count, loff_t *offset)
1477{
1478 struct inode *inode = file_inode(file);
1479 struct task_struct *p;
1480 char buffer[PROC_NUMBUF];
1481 int wake_up_idle, err;
1482
1483 memset(buffer, 0, sizeof(buffer));
1484 if (count > sizeof(buffer) - 1)
1485 count = sizeof(buffer) - 1;
1486 if (copy_from_user(buffer, buf, count)) {
1487 err = -EFAULT;
1488 goto out;
1489 }
1490
1491 err = kstrtoint(strstrip(buffer), 0, &wake_up_idle);
1492 if (err)
1493 goto out;
1494
1495 p = get_proc_task(inode);
1496 if (!p)
1497 return -ESRCH;
1498
1499 err = sched_set_wake_up_idle(p, wake_up_idle);
1500
1501 put_task_struct(p);
1502
1503out:
1504 return err < 0 ? err : count;
1505}
1506
1507static int sched_wake_up_idle_open(struct inode *inode, struct file *filp)
1508{
1509 return single_open(filp, sched_wake_up_idle_show, inode);
1510}
1511
1512static const struct file_operations proc_pid_sched_wake_up_idle_operations = {
1513 .open = sched_wake_up_idle_open,
1514 .read = seq_read,
1515 .write = sched_wake_up_idle_write,
1516 .llseek = seq_lseek,
1517 .release = single_release,
1518};
1519
1520#endif /* CONFIG_SMP */
1521
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08001522#ifdef CONFIG_SCHED_WALT
Pavankumar Kondeti92564312017-03-22 14:49:54 +05301523
1524static int sched_init_task_load_show(struct seq_file *m, void *v)
1525{
1526 struct inode *inode = m->private;
1527 struct task_struct *p;
1528
1529 p = get_proc_task(inode);
1530 if (!p)
1531 return -ESRCH;
1532
1533 seq_printf(m, "%d\n", sched_get_init_task_load(p));
1534
1535 put_task_struct(p);
1536
1537 return 0;
1538}
1539
1540static ssize_t
1541sched_init_task_load_write(struct file *file, const char __user *buf,
1542 size_t count, loff_t *offset)
1543{
1544 struct inode *inode = file_inode(file);
1545 struct task_struct *p;
1546 char buffer[PROC_NUMBUF];
1547 int init_task_load, err;
1548
1549 memset(buffer, 0, sizeof(buffer));
1550 if (count > sizeof(buffer) - 1)
1551 count = sizeof(buffer) - 1;
1552 if (copy_from_user(buffer, buf, count)) {
1553 err = -EFAULT;
1554 goto out;
1555 }
1556
1557 err = kstrtoint(strstrip(buffer), 0, &init_task_load);
1558 if (err)
1559 goto out;
1560
1561 p = get_proc_task(inode);
1562 if (!p)
1563 return -ESRCH;
1564
1565 err = sched_set_init_task_load(p, init_task_load);
1566
1567 put_task_struct(p);
1568
1569out:
1570 return err < 0 ? err : count;
1571}
1572
1573static int sched_init_task_load_open(struct inode *inode, struct file *filp)
1574{
1575 return single_open(filp, sched_init_task_load_show, inode);
1576}
1577
1578static const struct file_operations proc_pid_sched_init_task_load_operations = {
1579 .open = sched_init_task_load_open,
1580 .read = seq_read,
1581 .write = sched_init_task_load_write,
1582 .llseek = seq_lseek,
1583 .release = single_release,
1584};
1585
1586static int sched_group_id_show(struct seq_file *m, void *v)
1587{
1588 struct inode *inode = m->private;
1589 struct task_struct *p;
1590
1591 p = get_proc_task(inode);
1592 if (!p)
1593 return -ESRCH;
1594
1595 seq_printf(m, "%d\n", sched_get_group_id(p));
1596
1597 put_task_struct(p);
1598
1599 return 0;
1600}
1601
1602static ssize_t
1603sched_group_id_write(struct file *file, const char __user *buf,
1604 size_t count, loff_t *offset)
1605{
1606 struct inode *inode = file_inode(file);
1607 struct task_struct *p;
1608 char buffer[PROC_NUMBUF];
1609 int group_id, err;
1610
1611 memset(buffer, 0, sizeof(buffer));
1612 if (count > sizeof(buffer) - 1)
1613 count = sizeof(buffer) - 1;
1614 if (copy_from_user(buffer, buf, count)) {
1615 err = -EFAULT;
1616 goto out;
1617 }
1618
1619 err = kstrtoint(strstrip(buffer), 0, &group_id);
1620 if (err)
1621 goto out;
1622
1623 p = get_proc_task(inode);
1624 if (!p)
1625 return -ESRCH;
1626
1627 err = sched_set_group_id(p, group_id);
1628
1629 put_task_struct(p);
1630
1631out:
1632 return err < 0 ? err : count;
1633}
1634
1635static int sched_group_id_open(struct inode *inode, struct file *filp)
1636{
1637 return single_open(filp, sched_group_id_show, inode);
1638}
1639
1640static const struct file_operations proc_pid_sched_group_id_operations = {
1641 .open = sched_group_id_open,
1642 .read = seq_read,
1643 .write = sched_group_id_write,
1644 .llseek = seq_lseek,
1645 .release = single_release,
1646};
1647
Puja Gupta487dec62017-06-27 10:13:50 -07001648#endif /* CONFIG_SCHED_WALT */
Pavankumar Kondeti92564312017-03-22 14:49:54 +05301649
Mike Galbraith5091faa2010-11-30 14:18:03 +01001650#ifdef CONFIG_SCHED_AUTOGROUP
1651/*
1652 * Print out autogroup related information:
1653 */
1654static int sched_autogroup_show(struct seq_file *m, void *v)
1655{
1656 struct inode *inode = m->private;
1657 struct task_struct *p;
1658
1659 p = get_proc_task(inode);
1660 if (!p)
1661 return -ESRCH;
1662 proc_sched_autogroup_show_task(p, m);
1663
1664 put_task_struct(p);
1665
1666 return 0;
1667}
1668
1669static ssize_t
1670sched_autogroup_write(struct file *file, const char __user *buf,
1671 size_t count, loff_t *offset)
1672{
Al Viro496ad9a2013-01-23 17:07:38 -05001673 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001674 struct task_struct *p;
1675 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001676 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001677 int err;
1678
1679 memset(buffer, 0, sizeof(buffer));
1680 if (count > sizeof(buffer) - 1)
1681 count = sizeof(buffer) - 1;
1682 if (copy_from_user(buffer, buf, count))
1683 return -EFAULT;
1684
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001685 err = kstrtoint(strstrip(buffer), 0, &nice);
1686 if (err < 0)
1687 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001688
1689 p = get_proc_task(inode);
1690 if (!p)
1691 return -ESRCH;
1692
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001693 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001694 if (err)
1695 count = err;
1696
1697 put_task_struct(p);
1698
1699 return count;
1700}
1701
1702static int sched_autogroup_open(struct inode *inode, struct file *filp)
1703{
1704 int ret;
1705
1706 ret = single_open(filp, sched_autogroup_show, NULL);
1707 if (!ret) {
1708 struct seq_file *m = filp->private_data;
1709
1710 m->private = inode;
1711 }
1712 return ret;
1713}
1714
1715static const struct file_operations proc_pid_sched_autogroup_operations = {
1716 .open = sched_autogroup_open,
1717 .read = seq_read,
1718 .write = sched_autogroup_write,
1719 .llseek = seq_lseek,
1720 .release = single_release,
1721};
1722
1723#endif /* CONFIG_SCHED_AUTOGROUP */
1724
john stultz4614a696b2009-12-14 18:00:05 -08001725static ssize_t comm_write(struct file *file, const char __user *buf,
1726 size_t count, loff_t *offset)
1727{
Al Viro496ad9a2013-01-23 17:07:38 -05001728 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001729 struct task_struct *p;
1730 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001731 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001732
1733 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001734 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001735 return -EFAULT;
1736
1737 p = get_proc_task(inode);
1738 if (!p)
1739 return -ESRCH;
1740
1741 if (same_thread_group(current, p))
1742 set_task_comm(p, buffer);
1743 else
1744 count = -EINVAL;
1745
1746 put_task_struct(p);
1747
1748 return count;
1749}
1750
1751static int comm_show(struct seq_file *m, void *v)
1752{
1753 struct inode *inode = m->private;
1754 struct task_struct *p;
1755
1756 p = get_proc_task(inode);
1757 if (!p)
1758 return -ESRCH;
1759
1760 task_lock(p);
1761 seq_printf(m, "%s\n", p->comm);
1762 task_unlock(p);
1763
1764 put_task_struct(p);
1765
1766 return 0;
1767}
1768
1769static int comm_open(struct inode *inode, struct file *filp)
1770{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001771 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001772}
1773
1774static const struct file_operations proc_pid_set_comm_operations = {
1775 .open = comm_open,
1776 .read = seq_read,
1777 .write = comm_write,
1778 .llseek = seq_lseek,
1779 .release = single_release,
1780};
1781
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001782static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001783{
1784 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001785 struct file *exe_file;
1786
David Howells2b0143b2015-03-17 22:25:59 +00001787 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001788 if (!task)
1789 return -ENOENT;
Mateusz Guzikcd81a912016-08-23 16:20:38 +02001790 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001791 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001792 if (exe_file) {
1793 *exe_path = exe_file->f_path;
1794 path_get(&exe_file->f_path);
1795 fput(exe_file);
1796 return 0;
1797 } else
1798 return -ENOENT;
1799}
1800
Al Viro6b255392015-11-17 10:20:54 -05001801static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001802 struct inode *inode,
1803 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001805 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 int error = -EACCES;
1807
Al Viro6b255392015-11-17 10:20:54 -05001808 if (!dentry)
1809 return ERR_PTR(-ECHILD);
1810
Eric W. Biederman778c1142006-06-26 00:25:58 -07001811 /* Are we allowed to snoop on the tasks file descriptors? */
1812 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Christoph Hellwig408ef012012-06-18 10:47:03 -04001815 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1816 if (error)
1817 goto out;
1818
Al Viro6e771372015-05-02 13:37:52 -04001819 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001820 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821out:
Al Viro008b1502005-08-20 00:17:39 +01001822 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001825static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Mel Gormane12ba742007-10-16 01:25:52 -07001827 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001828 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 int len;
1830
1831 if (!tmp)
1832 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001833
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001834 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001835 len = PTR_ERR(pathname);
1836 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001838 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (len > buflen)
1841 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001842 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 len = -EFAULT;
1844 out:
1845 free_page((unsigned long)tmp);
1846 return len;
1847}
1848
1849static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1850{
1851 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001852 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001853 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Eric W. Biederman778c1142006-06-26 00:25:58 -07001855 /* Are we allowed to snoop on the tasks file descriptors? */
1856 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001859 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (error)
1861 goto out;
1862
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001863 error = do_proc_readlink(&path, buffer, buflen);
1864 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return error;
1867}
1868
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001869const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001871 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001872 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873};
1874
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001875
1876/* building an inode */
1877
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001878struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001879{
1880 struct inode * inode;
1881 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001882 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001883
1884 /* We need a new inode */
1885
1886 inode = new_inode(sb);
1887 if (!inode)
1888 goto out;
1889
1890 /* Common stuff */
1891 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001892 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001893 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001894 inode->i_op = &proc_def_inode_operations;
1895
1896 /*
1897 * grab the reference to task.
1898 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001899 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001900 if (!ei->pid)
1901 goto out_unlock;
1902
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001903 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001904 rcu_read_lock();
1905 cred = __task_cred(task);
1906 inode->i_uid = cred->euid;
1907 inode->i_gid = cred->egid;
1908 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001909 }
1910 security_task_to_inode(task, inode);
1911
1912out:
1913 return inode;
1914
1915out_unlock:
1916 iput(inode);
1917 return NULL;
1918}
1919
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001920int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001921{
David Howells2b0143b2015-03-17 22:25:59 +00001922 struct inode *inode = d_inode(dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001923 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001924 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001925 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001926
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001927 generic_fillattr(inode, stat);
1928
1929 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001930 stat->uid = GLOBAL_ROOT_UID;
1931 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001932 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1933 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001934 if (!has_pid_permissions(pid, task, 2)) {
1935 rcu_read_unlock();
1936 /*
1937 * This doesn't prevent learning whether PID exists,
1938 * it only makes getattr() consistent with readdir().
1939 */
1940 return -ENOENT;
1941 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001942 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1943 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001944 cred = __task_cred(task);
1945 stat->uid = cred->euid;
1946 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001947 }
1948 }
1949 rcu_read_unlock();
1950 return 0;
1951}
1952
1953/* dentry stuff */
1954
1955/*
1956 * Exceptional case: normally we are not allowed to unhash a busy
1957 * directory. In this case, however, we can do it - no aliasing problems
1958 * due to the way we treat inodes.
1959 *
1960 * Rewrite the inode's ownerships here because the owning task may have
1961 * performed a setuid(), etc.
1962 *
1963 * Before the /proc/pid/status file was created the only way to read
1964 * the effective uid of a /process was to stat /proc/pid. Reading
1965 * /proc/pid/status is slow enough that procps and other packages
1966 * kept stating /proc/pid. To keep the rules in /proc simple I have
1967 * made this apply to all per process world readable and executable
1968 * directories.
1969 */
Al Viro0b728e12012-06-10 16:03:43 -04001970int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001971{
Nick Piggin34286d62011-01-07 17:49:57 +11001972 struct inode *inode;
1973 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001974 const struct cred *cred;
1975
Al Viro0b728e12012-06-10 16:03:43 -04001976 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001977 return -ECHILD;
1978
David Howells2b0143b2015-03-17 22:25:59 +00001979 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001980 task = get_proc_task(inode);
1981
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001982 if (task) {
1983 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1984 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001985 rcu_read_lock();
1986 cred = __task_cred(task);
1987 inode->i_uid = cred->euid;
1988 inode->i_gid = cred->egid;
1989 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001990 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001991 inode->i_uid = GLOBAL_ROOT_UID;
1992 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001993 }
1994 inode->i_mode &= ~(S_ISUID | S_ISGID);
1995 security_task_to_inode(task, inode);
1996 put_task_struct(task);
1997 return 1;
1998 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001999 return 0;
2000}
2001
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08002002static inline bool proc_inode_is_dead(struct inode *inode)
2003{
2004 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
2005}
2006
David Howells1dd704b2013-04-12 01:08:50 +01002007int pid_delete_dentry(const struct dentry *dentry)
2008{
2009 /* Is the task we represent dead?
2010 * If so, then don't put the dentry on the lru list,
2011 * kill it immediately.
2012 */
David Howells2b0143b2015-03-17 22:25:59 +00002013 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01002014}
2015
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002016const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002017{
2018 .d_revalidate = pid_revalidate,
2019 .d_delete = pid_delete_dentry,
2020};
2021
2022/* Lookups */
2023
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07002024/*
2025 * Fill a directory entry.
2026 *
2027 * If possible create the dcache entry and derive our inode number and
2028 * file type from dcache entry.
2029 *
2030 * Since all of the proc inode numbers are dynamically generated, the inode
2031 * numbers do not exist until the inode is cache. This means creating the
2032 * the dcache entry in readdir is necessary to keep the inode numbers
2033 * reported by readdir in sync with the inode numbers reported
2034 * by stat.
2035 */
Al Virof0c3b502013-05-16 12:07:31 -04002036bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002037 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002038 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002039{
Al Virof0c3b502013-05-16 12:07:31 -04002040 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04002041 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002042 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04002043 unsigned type;
2044 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002045
Al Viro1df98b82013-06-15 11:33:10 +04002046 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002047 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04002048 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2049 child = d_alloc_parallel(dir, &qname, &wq);
2050 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04002051 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04002052 if (d_in_lookup(child)) {
2053 int err = instantiate(d_inode(dir), child, task, ptr);
2054 d_lookup_done(child);
2055 if (err < 0) {
2056 dput(child);
2057 goto end_instantiate;
2058 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07002059 }
2060 }
David Howells2b0143b2015-03-17 22:25:59 +00002061 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04002062 ino = inode->i_ino;
2063 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002064 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04002065 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04002066
2067end_instantiate:
2068 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002069}
2070
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002071/*
2072 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2073 * which represent vma start and end addresses.
2074 */
2075static int dname_to_vma_addr(struct dentry *dentry,
2076 unsigned long *start, unsigned long *end)
2077{
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08002078 const char *str = dentry->d_name.name;
2079 unsigned long long sval, eval;
2080 unsigned int len;
2081
2082 len = _parse_integer(str, 16, &sval);
2083 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002084 return -EINVAL;
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08002085 if (sval != (unsigned long)sval)
2086 return -EINVAL;
2087 str += len;
2088
2089 if (*str != '-')
2090 return -EINVAL;
2091 str++;
2092
2093 len = _parse_integer(str, 16, &eval);
2094 if (len & KSTRTOX_OVERFLOW)
2095 return -EINVAL;
2096 if (eval != (unsigned long)eval)
2097 return -EINVAL;
2098 str += len;
2099
2100 if (*str != '\0')
2101 return -EINVAL;
2102
2103 *start = sval;
2104 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002105
2106 return 0;
2107}
2108
Al Viro0b728e12012-06-10 16:03:43 -04002109static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002110{
2111 unsigned long vm_start, vm_end;
2112 bool exact_vma_exists = false;
2113 struct mm_struct *mm = NULL;
2114 struct task_struct *task;
2115 const struct cred *cred;
2116 struct inode *inode;
2117 int status = 0;
2118
Al Viro0b728e12012-06-10 16:03:43 -04002119 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120 return -ECHILD;
2121
David Howells2b0143b2015-03-17 22:25:59 +00002122 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002123 task = get_proc_task(inode);
2124 if (!task)
2125 goto out_notask;
2126
Jann Horncaaee622016-01-20 15:00:04 -08002127 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07002128 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002129 goto out;
2130
2131 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2132 down_read(&mm->mmap_sem);
2133 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2134 up_read(&mm->mmap_sem);
2135 }
2136
2137 mmput(mm);
2138
2139 if (exact_vma_exists) {
2140 if (task_dumpable(task)) {
2141 rcu_read_lock();
2142 cred = __task_cred(task);
2143 inode->i_uid = cred->euid;
2144 inode->i_gid = cred->egid;
2145 rcu_read_unlock();
2146 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08002147 inode->i_uid = GLOBAL_ROOT_UID;
2148 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002149 }
2150 security_task_to_inode(task, inode);
2151 status = 1;
2152 }
2153
2154out:
2155 put_task_struct(task);
2156
2157out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002158 return status;
2159}
2160
2161static const struct dentry_operations tid_map_files_dentry_operations = {
2162 .d_revalidate = map_files_d_revalidate,
2163 .d_delete = pid_delete_dentry,
2164};
2165
Al Viro6b255392015-11-17 10:20:54 -05002166static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002167{
2168 unsigned long vm_start, vm_end;
2169 struct vm_area_struct *vma;
2170 struct task_struct *task;
2171 struct mm_struct *mm;
2172 int rc;
2173
2174 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002175 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002176 if (!task)
2177 goto out;
2178
2179 mm = get_task_mm(task);
2180 put_task_struct(task);
2181 if (!mm)
2182 goto out;
2183
2184 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2185 if (rc)
2186 goto out_mmput;
2187
Artem Fetishev70335ab2014-03-10 15:49:45 -07002188 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002189 down_read(&mm->mmap_sem);
2190 vma = find_exact_vma(mm, vm_start, vm_end);
2191 if (vma && vma->vm_file) {
2192 *path = vma->vm_file->f_path;
2193 path_get(path);
2194 rc = 0;
2195 }
2196 up_read(&mm->mmap_sem);
2197
2198out_mmput:
2199 mmput(mm);
2200out:
2201 return rc;
2202}
2203
2204struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04002205 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002206 unsigned long len;
2207 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2208};
2209
Calvin Owensbdb4d102015-09-09 15:35:54 -07002210/*
2211 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2212 * symlinks may be used to bypass permissions on ancestor directories in the
2213 * path to the file in question.
2214 */
2215static const char *
Al Viro6b255392015-11-17 10:20:54 -05002216proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002217 struct inode *inode,
2218 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002219{
2220 if (!capable(CAP_SYS_ADMIN))
2221 return ERR_PTR(-EPERM);
2222
Al Virofceef392015-12-29 15:58:39 -05002223 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002224}
2225
2226/*
Al Viro6b255392015-11-17 10:20:54 -05002227 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002228 */
2229static const struct inode_operations proc_map_files_link_inode_operations = {
2230 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002231 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002232 .setattr = proc_setattr,
2233};
2234
Al Viroc52a47a2013-06-15 11:15:20 +04002235static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002236proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2237 struct task_struct *task, const void *ptr)
2238{
Al Viro7b540d02012-08-27 14:55:26 -04002239 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002240 struct proc_inode *ei;
2241 struct inode *inode;
2242
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002243 inode = proc_pid_make_inode(dir->i_sb, task);
2244 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002245 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002246
2247 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002248 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002249
Calvin Owensbdb4d102015-09-09 15:35:54 -07002250 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002251 inode->i_size = 64;
2252 inode->i_mode = S_IFLNK;
2253
Al Viro7b540d02012-08-27 14:55:26 -04002254 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002255 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04002256 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002257 inode->i_mode |= S_IWUSR;
2258
2259 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2260 d_add(dentry, inode);
2261
Al Viroc52a47a2013-06-15 11:15:20 +04002262 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002263}
2264
2265static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002266 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002267{
2268 unsigned long vm_start, vm_end;
2269 struct vm_area_struct *vma;
2270 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002271 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002272 struct mm_struct *mm;
2273
Al Viroc52a47a2013-06-15 11:15:20 +04002274 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002275 task = get_proc_task(dir);
2276 if (!task)
2277 goto out;
2278
Al Viroc52a47a2013-06-15 11:15:20 +04002279 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002280 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002281 goto out_put_task;
2282
Al Viroc52a47a2013-06-15 11:15:20 +04002283 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002284 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002285 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002286
2287 mm = get_task_mm(task);
2288 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002289 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002290
2291 down_read(&mm->mmap_sem);
2292 vma = find_exact_vma(mm, vm_start, vm_end);
2293 if (!vma)
2294 goto out_no_vma;
2295
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002296 if (vma->vm_file)
2297 result = proc_map_files_instantiate(dir, dentry, task,
2298 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002299
2300out_no_vma:
2301 up_read(&mm->mmap_sem);
2302 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002303out_put_task:
2304 put_task_struct(task);
2305out:
Al Viroc52a47a2013-06-15 11:15:20 +04002306 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002307}
2308
2309static const struct inode_operations proc_map_files_inode_operations = {
2310 .lookup = proc_map_files_lookup,
2311 .permission = proc_fd_permission,
2312 .setattr = proc_setattr,
2313};
2314
2315static int
Al Virof0c3b502013-05-16 12:07:31 -04002316proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002317{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002318 struct vm_area_struct *vma;
2319 struct task_struct *task;
2320 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002321 unsigned long nr_files, pos, i;
2322 struct flex_array *fa = NULL;
2323 struct map_files_info info;
2324 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002325 int ret;
2326
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002327 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002328 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002329 if (!task)
2330 goto out;
2331
2332 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002333 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002334 goto out_put_task;
2335
2336 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002337 if (!dir_emit_dots(file, ctx))
2338 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002339
Al Virof0c3b502013-05-16 12:07:31 -04002340 mm = get_task_mm(task);
2341 if (!mm)
2342 goto out_put_task;
2343 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002344
Al Virof0c3b502013-05-16 12:07:31 -04002345 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002346
Al Virof0c3b502013-05-16 12:07:31 -04002347 /*
2348 * We need two passes here:
2349 *
2350 * 1) Collect vmas of mapped files with mmap_sem taken
2351 * 2) Release mmap_sem and instantiate entries
2352 *
2353 * otherwise we get lockdep complained, since filldir()
2354 * routine might require mmap_sem taken in might_fault().
2355 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002356
Al Virof0c3b502013-05-16 12:07:31 -04002357 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2358 if (vma->vm_file && ++pos > ctx->pos)
2359 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002360 }
Al Virof0c3b502013-05-16 12:07:31 -04002361
2362 if (nr_files) {
2363 fa = flex_array_alloc(sizeof(info), nr_files,
2364 GFP_KERNEL);
2365 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2366 GFP_KERNEL)) {
2367 ret = -ENOMEM;
2368 if (fa)
2369 flex_array_free(fa);
2370 up_read(&mm->mmap_sem);
2371 mmput(mm);
2372 goto out_put_task;
2373 }
2374 for (i = 0, vma = mm->mmap, pos = 2; vma;
2375 vma = vma->vm_next) {
2376 if (!vma->vm_file)
2377 continue;
2378 if (++pos <= ctx->pos)
2379 continue;
2380
2381 info.mode = vma->vm_file->f_mode;
2382 info.len = snprintf(info.name,
2383 sizeof(info.name), "%lx-%lx",
2384 vma->vm_start, vma->vm_end);
2385 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2386 BUG();
2387 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002388 }
Al Virof0c3b502013-05-16 12:07:31 -04002389 up_read(&mm->mmap_sem);
2390
2391 for (i = 0; i < nr_files; i++) {
2392 p = flex_array_get(fa, i);
2393 if (!proc_fill_cache(file, ctx,
2394 p->name, p->len,
2395 proc_map_files_instantiate,
2396 task,
2397 (void *)(unsigned long)p->mode))
2398 break;
2399 ctx->pos++;
2400 }
2401 if (fa)
2402 flex_array_free(fa);
2403 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002404
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002405out_put_task:
2406 put_task_struct(task);
2407out:
2408 return ret;
2409}
2410
2411static const struct file_operations proc_map_files_operations = {
2412 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002413 .iterate_shared = proc_map_files_readdir,
2414 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002415};
2416
Eric Engestromb5946be2016-03-17 14:20:57 -07002417#ifdef CONFIG_CHECKPOINT_RESTORE
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002418struct timers_private {
2419 struct pid *pid;
2420 struct task_struct *task;
2421 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002422 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002423 unsigned long flags;
2424};
2425
2426static void *timers_start(struct seq_file *m, loff_t *pos)
2427{
2428 struct timers_private *tp = m->private;
2429
2430 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2431 if (!tp->task)
2432 return ERR_PTR(-ESRCH);
2433
2434 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2435 if (!tp->sighand)
2436 return ERR_PTR(-ESRCH);
2437
2438 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2439}
2440
2441static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2442{
2443 struct timers_private *tp = m->private;
2444 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2445}
2446
2447static void timers_stop(struct seq_file *m, void *v)
2448{
2449 struct timers_private *tp = m->private;
2450
2451 if (tp->sighand) {
2452 unlock_task_sighand(tp->task, &tp->flags);
2453 tp->sighand = NULL;
2454 }
2455
2456 if (tp->task) {
2457 put_task_struct(tp->task);
2458 tp->task = NULL;
2459 }
2460}
2461
2462static int show_timer(struct seq_file *m, void *v)
2463{
2464 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002465 struct timers_private *tp = m->private;
2466 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002467 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002468 [SIGEV_SIGNAL] = "signal",
2469 [SIGEV_NONE] = "none",
2470 [SIGEV_THREAD] = "thread",
2471 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002472
2473 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002474 notify = timer->it_sigev_notify;
2475
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002476 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002477 seq_printf(m, "signal: %d/%p\n",
2478 timer->sigq->info.si_signo,
2479 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002480 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002481 nstr[notify & ~SIGEV_THREAD_ID],
2482 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2483 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002484 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002485
2486 return 0;
2487}
2488
2489static const struct seq_operations proc_timers_seq_ops = {
2490 .start = timers_start,
2491 .next = timers_next,
2492 .stop = timers_stop,
2493 .show = show_timer,
2494};
2495
2496static int proc_timers_open(struct inode *inode, struct file *file)
2497{
2498 struct timers_private *tp;
2499
2500 tp = __seq_open_private(file, &proc_timers_seq_ops,
2501 sizeof(struct timers_private));
2502 if (!tp)
2503 return -ENOMEM;
2504
2505 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002506 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002507 return 0;
2508}
2509
2510static const struct file_operations proc_timers_operations = {
2511 .open = proc_timers_open,
2512 .read = seq_read,
2513 .llseek = seq_lseek,
2514 .release = seq_release_private,
2515};
Eric Engestromb5946be2016-03-17 14:20:57 -07002516#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002517
John Stultz5de23d42016-03-17 14:20:54 -07002518static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2519 size_t count, loff_t *offset)
2520{
2521 struct inode *inode = file_inode(file);
2522 struct task_struct *p;
2523 u64 slack_ns;
2524 int err;
2525
2526 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2527 if (err < 0)
2528 return err;
2529
2530 p = get_proc_task(inode);
2531 if (!p)
2532 return -ESRCH;
2533
John Stultz4b2bd5f2016-10-07 17:02:33 -07002534 if (p != current) {
2535 if (!capable(CAP_SYS_NICE)) {
2536 count = -EPERM;
2537 goto out;
2538 }
John Stultz5de23d42016-03-17 14:20:54 -07002539
John Stultz4b2bd5f2016-10-07 17:02:33 -07002540 err = security_task_setscheduler(p);
2541 if (err) {
2542 count = err;
2543 goto out;
2544 }
John Stultz904763e2016-10-07 17:02:29 -07002545 }
2546
John Stultz7abbaf92016-10-07 17:02:26 -07002547 task_lock(p);
2548 if (slack_ns == 0)
2549 p->timer_slack_ns = p->default_timer_slack_ns;
2550 else
2551 p->timer_slack_ns = slack_ns;
2552 task_unlock(p);
2553
2554out:
John Stultz5de23d42016-03-17 14:20:54 -07002555 put_task_struct(p);
2556
2557 return count;
2558}
2559
2560static int timerslack_ns_show(struct seq_file *m, void *v)
2561{
2562 struct inode *inode = m->private;
2563 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002564 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002565
2566 p = get_proc_task(inode);
2567 if (!p)
2568 return -ESRCH;
2569
John Stultz4b2bd5f2016-10-07 17:02:33 -07002570 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002571
John Stultz4b2bd5f2016-10-07 17:02:33 -07002572 if (!capable(CAP_SYS_NICE)) {
2573 err = -EPERM;
2574 goto out;
2575 }
2576 err = security_task_getscheduler(p);
2577 if (err)
2578 goto out;
2579 }
John Stultz904763e2016-10-07 17:02:29 -07002580
John Stultz7abbaf92016-10-07 17:02:26 -07002581 task_lock(p);
2582 seq_printf(m, "%llu\n", p->timer_slack_ns);
2583 task_unlock(p);
2584
2585out:
John Stultz5de23d42016-03-17 14:20:54 -07002586 put_task_struct(p);
2587
2588 return err;
2589}
2590
2591static int timerslack_ns_open(struct inode *inode, struct file *filp)
2592{
2593 return single_open(filp, timerslack_ns_show, inode);
2594}
2595
2596static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2597 .open = timerslack_ns_open,
2598 .read = seq_read,
2599 .write = timerslack_ns_write,
2600 .llseek = seq_lseek,
2601 .release = single_release,
2602};
2603
Al Viroc52a47a2013-06-15 11:15:20 +04002604static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002605 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002606{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002607 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002608 struct inode *inode;
2609 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002610
Eric W. Biederman61a28782006-10-02 02:18:49 -07002611 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002612 if (!inode)
2613 goto out;
2614
2615 ei = PROC_I(inode);
2616 inode->i_mode = p->mode;
2617 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002618 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002619 if (p->iop)
2620 inode->i_op = p->iop;
2621 if (p->fop)
2622 inode->i_fop = p->fop;
2623 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002624 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002625 d_add(dentry, inode);
2626 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002627 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002628 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002629out:
Al Viroc52a47a2013-06-15 11:15:20 +04002630 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002631}
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633static struct dentry *proc_pident_lookup(struct inode *dir,
2634 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002635 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002636 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Al Viroc52a47a2013-06-15 11:15:20 +04002638 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002639 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002640 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Al Viroc52a47a2013-06-15 11:15:20 +04002642 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Eric W. Biederman99f89552006-06-26 00:25:55 -07002644 if (!task)
2645 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002647 /*
2648 * Yes, it does not scale. And it should not. Don't add
2649 * new entries into /proc/<tgid>/ without very good reasons.
2650 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002651 last = &ents[nents - 1];
2652 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (p->len != dentry->d_name.len)
2654 continue;
2655 if (!memcmp(dentry->d_name.name, p->name, p->len))
2656 break;
2657 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002658 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 goto out;
2660
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002661 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002663 put_task_struct(task);
2664out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002665 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
Al Virof0c3b502013-05-16 12:07:31 -04002668static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002669 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002670{
Al Virof0c3b502013-05-16 12:07:31 -04002671 struct task_struct *task = get_proc_task(file_inode(file));
2672 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002673
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002674 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002675 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002676
Al Virof0c3b502013-05-16 12:07:31 -04002677 if (!dir_emit_dots(file, ctx))
2678 goto out;
2679
2680 if (ctx->pos >= nents + 2)
2681 goto out;
2682
2683 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2684 if (!proc_fill_cache(file, ctx, p->name, p->len,
2685 proc_pident_instantiate, task, p))
2686 break;
2687 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002688 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002689out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002690 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002695static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2696 size_t count, loff_t *ppos)
2697{
Al Viro496ad9a2013-01-23 17:07:38 -05002698 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002699 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002700 ssize_t length;
2701 struct task_struct *task = get_proc_task(inode);
2702
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002703 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002704 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002705
2706 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002707 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002708 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002709 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002710 if (length > 0)
2711 length = simple_read_from_buffer(buf, count, ppos, p, length);
2712 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002713 return length;
2714}
2715
2716static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2717 size_t count, loff_t *ppos)
2718{
Al Viro496ad9a2013-01-23 17:07:38 -05002719 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002720 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002721 ssize_t length;
2722 struct task_struct *task = get_proc_task(inode);
2723
2724 length = -ESRCH;
2725 if (!task)
2726 goto out_no_task;
2727 if (count > PAGE_SIZE)
2728 count = PAGE_SIZE;
2729
2730 /* No partial writes. */
2731 length = -EINVAL;
2732 if (*ppos != 0)
2733 goto out;
2734
Al Virobb646cd2015-12-24 00:16:30 -05002735 page = memdup_user(buf, count);
2736 if (IS_ERR(page)) {
2737 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002738 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002739 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002740
David Howells107db7c2009-05-08 13:55:27 +01002741 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002742 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002743 if (length < 0)
2744 goto out_free;
2745
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002746 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002747 (char*)file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002748 page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002749 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002750out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002751 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002752out:
2753 put_task_struct(task);
2754out_no_task:
2755 return length;
2756}
2757
Arjan van de Ven00977a52007-02-12 00:55:34 -08002758static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002759 .read = proc_pid_attr_read,
2760 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002761 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002762};
2763
Eric Dumazetc5141e62007-05-08 00:26:15 -07002764static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002765 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2766 REG("prev", S_IRUGO, proc_pid_attr_operations),
2767 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2768 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2769 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2770 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002771};
2772
Al Virof0c3b502013-05-16 12:07:31 -04002773static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Al Virof0c3b502013-05-16 12:07:31 -04002775 return proc_pident_readdir(file, ctx,
2776 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
Arjan van de Ven00977a52007-02-12 00:55:34 -08002779static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002781 .iterate_shared = proc_attr_dir_readdir,
2782 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783};
2784
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002785static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002786 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002788 return proc_pident_lookup(dir, dentry,
2789 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002792static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002793 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002794 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002795 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796};
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798#endif
2799
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002800#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002801static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2802 size_t count, loff_t *ppos)
2803{
Al Viro496ad9a2013-01-23 17:07:38 -05002804 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002805 struct mm_struct *mm;
2806 char buffer[PROC_NUMBUF];
2807 size_t len;
2808 int ret;
2809
2810 if (!task)
2811 return -ESRCH;
2812
2813 ret = 0;
2814 mm = get_task_mm(task);
2815 if (mm) {
2816 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2817 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2818 MMF_DUMP_FILTER_SHIFT));
2819 mmput(mm);
2820 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2821 }
2822
2823 put_task_struct(task);
2824
2825 return ret;
2826}
2827
2828static ssize_t proc_coredump_filter_write(struct file *file,
2829 const char __user *buf,
2830 size_t count,
2831 loff_t *ppos)
2832{
2833 struct task_struct *task;
2834 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002835 unsigned int val;
2836 int ret;
2837 int i;
2838 unsigned long mask;
2839
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002840 ret = kstrtouint_from_user(buf, count, 0, &val);
2841 if (ret < 0)
2842 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002843
2844 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002845 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002846 if (!task)
2847 goto out_no_task;
2848
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002849 mm = get_task_mm(task);
2850 if (!mm)
2851 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002852 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002853
2854 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2855 if (val & mask)
2856 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2857 else
2858 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2859 }
2860
2861 mmput(mm);
2862 out_no_mm:
2863 put_task_struct(task);
2864 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002865 if (ret < 0)
2866 return ret;
2867 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002868}
2869
2870static const struct file_operations proc_coredump_filter_operations = {
2871 .read = proc_coredump_filter_read,
2872 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002873 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002874};
2875#endif
2876
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002877#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002878static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002879{
Andrea Righi940389b2008-07-28 00:48:12 +02002880 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002881 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002882 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002883
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002884 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2885 if (result)
2886 return result;
2887
Jann Horncaaee622016-01-20 15:00:04 -08002888 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002889 result = -EACCES;
2890 goto out_unlock;
2891 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002892
Andrea Righi59954772008-07-27 17:29:15 +02002893 if (whole && lock_task_sighand(task, &flags)) {
2894 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002895
Andrea Righi59954772008-07-27 17:29:15 +02002896 task_io_accounting_add(&acct, &task->signal->ioac);
2897 while_each_thread(task, t)
2898 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002899
Andrea Righi59954772008-07-27 17:29:15 +02002900 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002901 }
Joe Perches25ce3192015-04-15 16:18:17 -07002902 seq_printf(m,
2903 "rchar: %llu\n"
2904 "wchar: %llu\n"
2905 "syscr: %llu\n"
2906 "syscw: %llu\n"
2907 "read_bytes: %llu\n"
2908 "write_bytes: %llu\n"
2909 "cancelled_write_bytes: %llu\n",
2910 (unsigned long long)acct.rchar,
2911 (unsigned long long)acct.wchar,
2912 (unsigned long long)acct.syscr,
2913 (unsigned long long)acct.syscw,
2914 (unsigned long long)acct.read_bytes,
2915 (unsigned long long)acct.write_bytes,
2916 (unsigned long long)acct.cancelled_write_bytes);
2917 result = 0;
2918
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002919out_unlock:
2920 mutex_unlock(&task->signal->cred_guard_mutex);
2921 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002922}
Andrea Righi297c5d92008-07-25 01:48:49 -07002923
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002924static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2925 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002926{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002927 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002928}
2929
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002930static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2931 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002932{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002933 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002934}
2935#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002936
Imran Khana9788f42017-09-18 15:48:30 +05302937#ifdef CONFIG_DETECT_HUNG_TASK
2938static ssize_t proc_hung_task_detection_enabled_read(struct file *file,
2939 char __user *buf, size_t count, loff_t *ppos)
2940{
2941 struct task_struct *task = get_proc_task(file_inode(file));
2942 char buffer[PROC_NUMBUF];
2943 size_t len;
2944 bool hang_detection_enabled;
2945
2946 if (!task)
2947 return -ESRCH;
2948 hang_detection_enabled = task->hang_detection_enabled;
2949 put_task_struct(task);
2950
2951 len = snprintf(buffer, sizeof(buffer), "%d\n", hang_detection_enabled);
2952
2953 return simple_read_from_buffer(buf, sizeof(buffer), ppos, buffer, len);
2954}
2955
2956static ssize_t proc_hung_task_detection_enabled_write(struct file *file,
2957 const char __user *buf, size_t count, loff_t *ppos)
2958{
2959 struct task_struct *task;
2960 bool hang_detection_enabled;
2961 int rv;
2962
2963 rv = kstrtobool_from_user(buf, count, &hang_detection_enabled);
2964 if (rv < 0)
2965 return rv;
2966
2967 task = get_proc_task(file_inode(file));
2968 if (!task)
2969 return -ESRCH;
2970 task->hang_detection_enabled = hang_detection_enabled;
2971 put_task_struct(task);
2972
2973 return count;
2974}
2975
2976static const struct file_operations proc_hung_task_detection_enabled_operations = {
2977 .read = proc_hung_task_detection_enabled_read,
2978 .write = proc_hung_task_detection_enabled_write,
2979 .llseek = generic_file_llseek,
2980};
2981#endif
2982
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002983#ifdef CONFIG_USER_NS
2984static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002985 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002986{
2987 struct user_namespace *ns = NULL;
2988 struct task_struct *task;
2989 struct seq_file *seq;
2990 int ret = -EINVAL;
2991
2992 task = get_proc_task(inode);
2993 if (task) {
2994 rcu_read_lock();
2995 ns = get_user_ns(task_cred_xxx(task, user_ns));
2996 rcu_read_unlock();
2997 put_task_struct(task);
2998 }
2999 if (!ns)
3000 goto err;
3001
3002 ret = seq_open(file, seq_ops);
3003 if (ret)
3004 goto err_put_ns;
3005
3006 seq = file->private_data;
3007 seq->private = ns;
3008
3009 return 0;
3010err_put_ns:
3011 put_user_ns(ns);
3012err:
3013 return ret;
3014}
3015
3016static int proc_id_map_release(struct inode *inode, struct file *file)
3017{
3018 struct seq_file *seq = file->private_data;
3019 struct user_namespace *ns = seq->private;
3020 put_user_ns(ns);
3021 return seq_release(inode, file);
3022}
3023
3024static int proc_uid_map_open(struct inode *inode, struct file *file)
3025{
3026 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
3027}
3028
3029static int proc_gid_map_open(struct inode *inode, struct file *file)
3030{
3031 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
3032}
3033
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003034static int proc_projid_map_open(struct inode *inode, struct file *file)
3035{
3036 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
3037}
3038
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003039static const struct file_operations proc_uid_map_operations = {
3040 .open = proc_uid_map_open,
3041 .write = proc_uid_map_write,
3042 .read = seq_read,
3043 .llseek = seq_lseek,
3044 .release = proc_id_map_release,
3045};
3046
3047static const struct file_operations proc_gid_map_operations = {
3048 .open = proc_gid_map_open,
3049 .write = proc_gid_map_write,
3050 .read = seq_read,
3051 .llseek = seq_lseek,
3052 .release = proc_id_map_release,
3053};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003054
3055static const struct file_operations proc_projid_map_operations = {
3056 .open = proc_projid_map_open,
3057 .write = proc_projid_map_write,
3058 .read = seq_read,
3059 .llseek = seq_lseek,
3060 .release = proc_id_map_release,
3061};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003062
3063static int proc_setgroups_open(struct inode *inode, struct file *file)
3064{
3065 struct user_namespace *ns = NULL;
3066 struct task_struct *task;
3067 int ret;
3068
3069 ret = -ESRCH;
3070 task = get_proc_task(inode);
3071 if (task) {
3072 rcu_read_lock();
3073 ns = get_user_ns(task_cred_xxx(task, user_ns));
3074 rcu_read_unlock();
3075 put_task_struct(task);
3076 }
3077 if (!ns)
3078 goto err;
3079
3080 if (file->f_mode & FMODE_WRITE) {
3081 ret = -EACCES;
3082 if (!ns_capable(ns, CAP_SYS_ADMIN))
3083 goto err_put_ns;
3084 }
3085
3086 ret = single_open(file, &proc_setgroups_show, ns);
3087 if (ret)
3088 goto err_put_ns;
3089
3090 return 0;
3091err_put_ns:
3092 put_user_ns(ns);
3093err:
3094 return ret;
3095}
3096
3097static int proc_setgroups_release(struct inode *inode, struct file *file)
3098{
3099 struct seq_file *seq = file->private_data;
3100 struct user_namespace *ns = seq->private;
3101 int ret = single_release(inode, file);
3102 put_user_ns(ns);
3103 return ret;
3104}
3105
3106static const struct file_operations proc_setgroups_operations = {
3107 .open = proc_setgroups_open,
3108 .write = proc_setgroups_write,
3109 .read = seq_read,
3110 .llseek = seq_lseek,
3111 .release = proc_setgroups_release,
3112};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003113#endif /* CONFIG_USER_NS */
3114
Kees Cook47830722008-10-06 03:11:58 +04003115static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3116 struct pid *pid, struct task_struct *task)
3117{
Al Viroa9712bc2011-03-23 15:52:50 -04003118 int err = lock_trace(task);
3119 if (!err) {
3120 seq_printf(m, "%08x\n", task->personality);
3121 unlock_trace(task);
3122 }
3123 return err;
Kees Cook47830722008-10-06 03:11:58 +04003124}
3125
Eric W. Biederman801199c2006-10-02 02:18:48 -07003126/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003127 * Thread groups
3128 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003129static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003130static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07003131
Eric Dumazetc5141e62007-05-08 00:26:15 -07003132static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003133 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3134 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08003135 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003136 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003137 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07003138#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003139 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07003140#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003141 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003142 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003143 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003144 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003145 ONE("limits", S_IRUGO, proc_pid_limits),
Pavankumar Kondeti92564312017-03-22 14:49:54 +05303146#ifdef CONFIG_SMP
3147 REG("sched_wake_up_idle", S_IRUGO|S_IWUSR, proc_pid_sched_wake_up_idle_operations),
3148#endif
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08003149#ifdef CONFIG_SCHED_WALT
Pavankumar Kondeti92564312017-03-22 14:49:54 +05303150 REG("sched_init_task_load", S_IRUGO|S_IWUSR, proc_pid_sched_init_task_load_operations),
3151 REG("sched_group_id", S_IRUGO|S_IWUGO, proc_pid_sched_group_id_operations),
3152#endif
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003153#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003154 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003155#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01003156#ifdef CONFIG_SCHED_AUTOGROUP
3157 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3158#endif
john stultz4614a696b2009-12-14 18:00:05 -08003159 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003160#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003161 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003162#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003163 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003164 ONE("stat", S_IRUGO, proc_tgid_stat),
3165 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003166 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003167#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003168 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003169#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003170 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3171 LNK("cwd", proc_cwd_link),
3172 LNK("root", proc_root_link),
3173 LNK("exe", proc_exe_link),
3174 REG("mounts", S_IRUGO, proc_mounts_operations),
3175 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3176 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Minchan Kimff926892013-05-09 16:21:24 +09003177#ifdef CONFIG_PROCESS_RECLAIM
Zhenhua Huang90305282019-10-14 19:35:19 +08003178 REG("reclaim", 0222, proc_reclaim_operations),
Minchan Kimff926892013-05-09 16:21:24 +09003179#endif
Matt Mackall1e883282008-02-04 22:29:07 -08003180#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003181 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003182 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione66689622017-09-06 16:25:08 -07003183 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003184 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003185#endif
3186#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003187 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003188#endif
3189#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003190 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003191#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003192#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003193 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003194#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303195#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003196 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003197#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003198#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003199 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003200#endif
Paul Menage8793d852007-10-18 23:39:39 -07003201#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003202 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003203#endif
Paul Menagea4243162007-10-18 23:39:35 -07003204#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003205 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003206#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003207 ONE("oom_score", S_IRUGO, proc_oom_score),
Greg Hackmann3a6594d2017-11-07 13:21:20 -08003208 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3209 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003210#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003211 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3212 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003213#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003214#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003215 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003216#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003217#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003218 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003219#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003220#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003221 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003222#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003223#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003224 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003225#endif
Imran Khana9788f42017-09-18 15:48:30 +05303226#ifdef CONFIG_DETECT_HUNG_TASK
3227 REG("hang_detection_enabled", 0666,
3228 proc_hung_task_detection_enabled_operations),
3229#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003230#ifdef CONFIG_USER_NS
3231 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3232 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003233 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003234 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003235#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003236#ifdef CONFIG_CHECKPOINT_RESTORE
3237 REG("timers", S_IRUGO, proc_timers_operations),
3238#endif
John Stultz5de23d42016-03-17 14:20:54 -07003239 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Connor O'Brien6e7b83d2018-01-31 18:11:57 -08003240#ifdef CONFIG_CPU_FREQ_TIMES
3241 ONE("time_in_state", 0444, proc_time_in_state_show),
3242#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003243};
3244
Al Virof0c3b502013-05-16 12:07:31 -04003245static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003246{
Al Virof0c3b502013-05-16 12:07:31 -04003247 return proc_pident_readdir(file, ctx,
3248 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003249}
3250
Arjan van de Ven00977a52007-02-12 00:55:34 -08003251static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003252 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003253 .iterate_shared = proc_tgid_base_readdir,
3254 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003255};
3256
Christian Brauner6dc8e7c2018-11-19 00:51:56 +01003257struct pid *tgid_pidfd_to_pid(const struct file *file)
3258{
3259 if (!d_is_dir(file->f_path.dentry) ||
3260 (file->f_op != &proc_tgid_base_operations))
3261 return ERR_PTR(-EBADF);
3262
3263 return proc_pid(file_inode(file));
3264}
3265
Al Viro00cd8dd2012-06-10 17:13:09 -04003266static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3267{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003268 return proc_pident_lookup(dir, dentry,
3269 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003270}
3271
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003272static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003273 .lookup = proc_tgid_base_lookup,
3274 .getattr = pid_getattr,
3275 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003276 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003277};
3278
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003279static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003281 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003282 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003283 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Eric W. Biederman48e64842006-06-26 00:25:48 -07003285 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003286 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003287 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003288 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003289 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003290 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003291 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003294 if (pid == tgid)
3295 return;
3296
Eric W. Biederman48e64842006-06-26 00:25:48 -07003297 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003298 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3299 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003300 if (!leader)
3301 goto out;
3302
3303 name.name = "task";
3304 name.len = strlen(name.name);
3305 dir = d_hash_and_lookup(leader, &name);
3306 if (!dir)
3307 goto out_put_leader;
3308
3309 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003310 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003311 dentry = d_hash_and_lookup(dir, &name);
3312 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003313 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003314 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003316
3317 dput(dir);
3318out_put_leader:
3319 dput(leader);
3320out:
3321 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
Randy Dunlap0895e912007-10-21 21:00:10 -07003324/**
3325 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3326 * @task: task that should be flushed.
3327 *
3328 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003329 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003330 * in. This call is supposed to do all of this job.
3331 *
3332 * Looks in the dcache for
3333 * /proc/@pid
3334 * /proc/@tgid/task/@pid
3335 * if either directory is present flushes it and all of it'ts children
3336 * from the dcache.
3337 *
3338 * It is safe and reasonable to cache /proc entries for a task until
3339 * that task exits. After that they just clog up the dcache with
3340 * useless entries, possibly causing useful dcache entries to be
3341 * flushed instead. This routine is proved to flush those useless
3342 * dcache entries at process exit time.
3343 *
3344 * NOTE: This routine is just an optimization so it does not guarantee
3345 * that no dcache entries will exist at process exit time it
3346 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003347 */
3348
3349void proc_flush_task(struct task_struct *task)
3350{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003351 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003352 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003353 struct upid *upid;
3354
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003355 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003356 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003357
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003358 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003359 upid = &pid->numbers[i];
3360 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003361 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003362 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003363}
3364
Al Viroc52a47a2013-06-15 11:15:20 +04003365static int proc_pid_instantiate(struct inode *dir,
3366 struct dentry * dentry,
3367 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003368{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003369 struct inode *inode;
3370
Eric W. Biederman61a28782006-10-02 02:18:49 -07003371 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003372 if (!inode)
3373 goto out;
3374
3375 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3376 inode->i_op = &proc_tgid_base_inode_operations;
3377 inode->i_fop = &proc_tgid_base_operations;
3378 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003379
Miklos Szeredibfe86842011-10-28 14:13:29 +02003380 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3381 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003382
Nick Pigginfb045ad2011-01-07 17:49:55 +11003383 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003384
3385 d_add(dentry, inode);
3386 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003387 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003388 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003389out:
Al Viroc52a47a2013-06-15 11:15:20 +04003390 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003391}
3392
Al Viro00cd8dd2012-06-10 17:13:09 -04003393struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003395 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003398 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003400 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 if (tgid == ~0U)
3402 goto out;
3403
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003404 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003405 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003406 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 if (task)
3408 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003409 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 if (!task)
3411 goto out;
3412
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003413 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003414 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415out:
Al Viroc52a47a2013-06-15 11:15:20 +04003416 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417}
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003420 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003421 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003423struct tgid_iter {
3424 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003425 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003426};
3427static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3428{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003429 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003431 if (iter.task)
3432 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003433 rcu_read_lock();
3434retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003435 iter.task = NULL;
3436 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003437 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003438 iter.tgid = pid_nr_ns(pid, ns);
3439 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003440 /* What we to know is if the pid we have find is the
3441 * pid of a thread_group_leader. Testing for task
3442 * being a thread_group_leader is the obvious thing
3443 * todo but there is a window when it fails, due to
3444 * the pid transfer logic in de_thread.
3445 *
3446 * So we perform the straight forward test of seeing
3447 * if the pid we have found is the pid of a thread
3448 * group leader, and don't worry if the task we have
3449 * found doesn't happen to be a thread group leader.
3450 * As we don't care in the case of readdir.
3451 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003452 if (!iter.task || !has_group_leader_pid(iter.task)) {
3453 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003454 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003455 }
3456 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003458 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003459 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460}
3461
Eric W. Biederman00978752014-07-31 03:10:50 -07003462#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003465int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003467 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003468 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003469 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Al Viro021ada72013-03-29 19:27:05 -04003471 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Eric W. Biederman00978752014-07-31 03:10:50 -07003474 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003475 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003476 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003477 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003478 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003479 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003480 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003481 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003482 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3483 return 0;
3484 ctx->pos = pos = pos + 1;
3485 }
3486 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003487 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003488 for (iter = next_tgid(ns, iter);
3489 iter.task;
3490 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003491 char name[PROC_NUMBUF];
3492 int len;
Eric Dumazet9618fba2017-01-24 15:18:07 -08003493
3494 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -04003495 if (!has_pid_permissions(ns, iter.task, 2))
3496 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003497
Al Virof0c3b502013-05-16 12:07:31 -04003498 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3499 ctx->pos = iter.tgid + TGID_OFFSET;
3500 if (!proc_fill_cache(file, ctx, name, len,
3501 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003502 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
Al Virof0c3b502013-05-16 12:07:31 -04003506 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return 0;
3508}
3509
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003510/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003511 * proc_tid_comm_permission is a special permission function exclusively
3512 * used for the node /proc/<pid>/task/<tid>/comm.
3513 * It bypasses generic permission checks in the case where a task of the same
3514 * task group attempts to access the node.
3515 * The rationale behind this is that glibc and bionic access this node for
3516 * cross thread naming (pthread_set/getname_np(!self)). However, if
3517 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3518 * which locks out the cross thread naming implementation.
3519 * This function makes sure that the node is always accessible for members of
3520 * same thread group.
3521 */
3522static int proc_tid_comm_permission(struct inode *inode, int mask)
3523{
3524 bool is_same_tgroup;
3525 struct task_struct *task;
3526
3527 task = get_proc_task(inode);
3528 if (!task)
3529 return -ESRCH;
3530 is_same_tgroup = same_thread_group(current, task);
3531 put_task_struct(task);
3532
3533 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3534 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3535 * read or written by the members of the corresponding
3536 * thread group.
3537 */
3538 return 0;
3539 }
3540
3541 return generic_permission(inode, mask);
3542}
3543
3544static const struct inode_operations proc_tid_comm_inode_operations = {
3545 .permission = proc_tid_comm_permission,
3546};
3547
3548/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003549 * Tasks
3550 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003551static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003552 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003553 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003554 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003555#ifdef CONFIG_NET
3556 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3557#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003558 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003559 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003560 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003561 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003562 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003563#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003564 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003565#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003566 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3567 &proc_tid_comm_inode_operations,
3568 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003569#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003570 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003571#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003572 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003573 ONE("stat", S_IRUGO, proc_tid_stat),
3574 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003575 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003576#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003577 REG("children", S_IRUGO, proc_tid_children_operations),
3578#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003579#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003580 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003581#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003582 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3583 LNK("cwd", proc_cwd_link),
3584 LNK("root", proc_root_link),
3585 LNK("exe", proc_exe_link),
3586 REG("mounts", S_IRUGO, proc_mounts_operations),
3587 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003588#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003589 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003590 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione66689622017-09-06 16:25:08 -07003591 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003592 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003593#endif
3594#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003595 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003596#endif
3597#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003598 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003599#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003600#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003601 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003602#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303603#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003604 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003605#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003606#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003607 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003608#endif
Paul Menage8793d852007-10-18 23:39:39 -07003609#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003610 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003611#endif
Paul Menagea4243162007-10-18 23:39:35 -07003612#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003613 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003614#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003615 ONE("oom_score", S_IRUGO, proc_oom_score),
Greg Hackmann3a6594d2017-11-07 13:21:20 -08003616 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3617 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003618#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003619 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003620 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003621#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003622#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003623 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003624#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003625#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003626 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003627#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003628#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003629 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003630#endif
Imran Khana9788f42017-09-18 15:48:30 +05303631#ifdef CONFIG_DETECT_HUNG_TASK
3632 REG("hang_detection_enabled", 0666,
3633 proc_hung_task_detection_enabled_operations),
3634#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003635#ifdef CONFIG_USER_NS
3636 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3637 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003638 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003639 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003640#endif
Connor O'Brien6e7b83d2018-01-31 18:11:57 -08003641#ifdef CONFIG_CPU_FREQ_TIMES
3642 ONE("time_in_state", 0444, proc_time_in_state_show),
3643#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003644};
3645
Al Virof0c3b502013-05-16 12:07:31 -04003646static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003647{
Al Virof0c3b502013-05-16 12:07:31 -04003648 return proc_pident_readdir(file, ctx,
3649 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003650}
3651
Al Viro00cd8dd2012-06-10 17:13:09 -04003652static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3653{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003654 return proc_pident_lookup(dir, dentry,
3655 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003656}
3657
Arjan van de Ven00977a52007-02-12 00:55:34 -08003658static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003659 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003660 .iterate_shared = proc_tid_base_readdir,
3661 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003662};
3663
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003664static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003665 .lookup = proc_tid_base_lookup,
3666 .getattr = pid_getattr,
3667 .setattr = proc_setattr,
3668};
3669
Al Viroc52a47a2013-06-15 11:15:20 +04003670static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003671 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003672{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003673 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003674 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003675
3676 if (!inode)
3677 goto out;
3678 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3679 inode->i_op = &proc_tid_base_inode_operations;
3680 inode->i_fop = &proc_tid_base_operations;
3681 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003682
Miklos Szeredibfe86842011-10-28 14:13:29 +02003683 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3684 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003685
Nick Pigginfb045ad2011-01-07 17:49:55 +11003686 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003687
3688 d_add(dentry, inode);
3689 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003690 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003691 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003692out:
Al Viroc52a47a2013-06-15 11:15:20 +04003693 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003694}
3695
Al Viro00cd8dd2012-06-10 17:13:09 -04003696static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003697{
Al Viroc52a47a2013-06-15 11:15:20 +04003698 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003699 struct task_struct *task;
3700 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003701 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003702 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003703
3704 if (!leader)
3705 goto out_no_task;
3706
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003707 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003708 if (tid == ~0U)
3709 goto out;
3710
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003711 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003712 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003713 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003714 if (task)
3715 get_task_struct(task);
3716 rcu_read_unlock();
3717 if (!task)
3718 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003719 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003720 goto out_drop_task;
3721
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003722 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003723out_drop_task:
3724 put_task_struct(task);
3725out:
3726 put_task_struct(leader);
3727out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003728 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003729}
3730
3731/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003732 * Find the first tid of a thread group to return to user space.
3733 *
3734 * Usually this is just the thread group leader, but if the users
3735 * buffer was too small or there was a seek into the middle of the
3736 * directory we have more work todo.
3737 *
3738 * In the case of a short read we start with find_task_by_pid.
3739 *
3740 * In the case of a seek we start with the leader and walk nr
3741 * threads past it.
3742 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003743static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3744 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003745{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003746 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003747 unsigned long nr = f_pos;
3748
3749 if (nr != f_pos) /* 32bit overflow? */
3750 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003751
Eric W. Biedermancc288732006-06-26 00:26:01 -07003752 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003753 task = pid_task(pid, PIDTYPE_PID);
3754 if (!task)
3755 goto fail;
3756
3757 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003758 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003759 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003760 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003761 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003762 }
3763
3764 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003765 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003766 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003767
3768 /* If we haven't found our starting place yet start
3769 * with the leader and walk nr threads forward.
3770 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003771 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003772 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003773 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003774 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003775 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003776fail:
3777 pos = NULL;
3778 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003779found:
3780 get_task_struct(pos);
3781out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003782 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003783 return pos;
3784}
3785
3786/*
3787 * Find the next thread in the thread list.
3788 * Return NULL if there is an error or no next thread.
3789 *
3790 * The reference to the input task_struct is released.
3791 */
3792static struct task_struct *next_tid(struct task_struct *start)
3793{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003794 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003795 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003796 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003797 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003798 if (thread_group_leader(pos))
3799 pos = NULL;
3800 else
3801 get_task_struct(pos);
3802 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003803 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003804 put_task_struct(start);
3805 return pos;
3806}
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003809static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003811 struct inode *inode = file_inode(file);
3812 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003813 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003814 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003816 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003817 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Al Virof0c3b502013-05-16 12:07:31 -04003819 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003822 /* f_version caches the tgid value that the last readdir call couldn't
3823 * return. lseek aka telldir automagically resets f_version to 0.
3824 */
Al Viro3aa33772014-10-31 00:42:35 -04003825 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003826 tid = (int)file->f_version;
3827 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003828 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003829 task;
Al Virof0c3b502013-05-16 12:07:31 -04003830 task = next_tid(task), ctx->pos++) {
3831 char name[PROC_NUMBUF];
3832 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003833 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003834 len = snprintf(name, sizeof(name), "%d", tid);
3835 if (!proc_fill_cache(file, ctx, name, len,
3836 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003837 /* returning this tgid failed, save it as the first
3838 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003839 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003840 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003844
Al Virof0c3b502013-05-16 12:07:31 -04003845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003847
3848static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3849{
David Howells2b0143b2015-03-17 22:25:59 +00003850 struct inode *inode = d_inode(dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003851 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003852 generic_fillattr(inode, stat);
3853
Eric W. Biederman99f89552006-06-26 00:25:55 -07003854 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003855 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003856 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003857 }
3858
3859 return 0;
3860}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003861
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003862static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003863 .lookup = proc_task_lookup,
3864 .getattr = proc_task_getattr,
3865 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003866 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003867};
3868
Arjan van de Ven00977a52007-02-12 00:55:34 -08003869static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003870 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003871 .iterate_shared = proc_task_readdir,
3872 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003873};