blob: b9e41832315a6ade8c0c0177c83b4d1c862e4340 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080076#include <linux/printk.h>
Paul Menagea4243162007-10-18 23:39:35 -070077#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpuset.h>
79#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050080#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070081#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070082#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070083#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070084#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080085#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040086#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080088#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040089#include <linux/posix-timers.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040090#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080093#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040095#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Alexey Dobriyane0a1a012018-02-06 15:36:59 -080097#include "../../lib/kstrtox.h"
98
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070099/* NOTE:
100 * Implementing inode permission operations in /proc is almost
101 * certainly an error. Permission checks need to happen during
102 * each system call not at open time. The reason is that most of
103 * what we wish to check for permissions in /proc varies at runtime.
104 *
105 * The classic example of a problem is opening file descriptors
106 * in /proc for a task before it execs a suid executable.
107 */
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700110 const char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700111 int len;
Al Virod161a132011-07-24 03:36:29 -0400112 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800113 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800114 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
Eric W. Biederman61a28782006-10-02 02:18:49 -0700118#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700120 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700121 .mode = MODE, \
122 .iop = IOP, \
123 .fop = FOP, \
124 .op = OP, \
125}
126
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300127#define DIR(NAME, MODE, iops, fops) \
128 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
129#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700130 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700131 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300132 { .proc_get_link = get_link } )
133#define REG(NAME, MODE, fops) \
134 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300135#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300138 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Vegard Nossumaed54172008-06-05 22:46:53 -0700140/*
141 * Count the number of hardlinks for the pid_entry table, excluding the .
142 * and .. links.
143 */
144static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145 unsigned int n)
146{
147 unsigned int i;
148 unsigned int count;
149
150 count = 0;
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
153 ++count;
154 }
155
156 return count;
157}
158
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200159static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000161 int result = -ENOENT;
162
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700163 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200164 if (task->fs) {
165 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000166 result = 0;
167 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000169 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170}
171
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800172static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173{
David Howells2b0143b2015-03-17 22:25:59 +0000174 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176
177 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200178 task_lock(task);
179 if (task->fs) {
180 get_fs_pwd(task->fs, path);
181 result = 0;
182 }
183 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700184 put_task_struct(task);
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return result;
187}
188
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800189static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
David Howells2b0143b2015-03-17 22:25:59 +0000191 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700193
194 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200195 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700196 put_task_struct(task);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return result;
199}
200
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700201static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
202 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700204 struct task_struct *tsk;
205 struct mm_struct *mm;
206 char *page;
207 unsigned long count = _count;
208 unsigned long arg_start, arg_end, env_start, env_end;
209 unsigned long len1, len2, len;
210 unsigned long p;
211 char c;
212 ssize_t rv;
213
214 BUG_ON(*pos < 0);
215
216 tsk = get_proc_task(file_inode(file));
217 if (!tsk)
218 return -ESRCH;
219 mm = get_task_mm(tsk);
220 put_task_struct(tsk);
221 if (!mm)
222 return 0;
223 /* Check if process spawned far enough to have cmdline. */
224 if (!mm->env_end) {
225 rv = 0;
226 goto out_mmput;
227 }
228
229 page = (char *)__get_free_page(GFP_TEMPORARY);
230 if (!page) {
231 rv = -ENOMEM;
232 goto out_mmput;
233 }
234
235 down_read(&mm->mmap_sem);
236 arg_start = mm->arg_start;
237 arg_end = mm->arg_end;
238 env_start = mm->env_start;
239 env_end = mm->env_end;
240 up_read(&mm->mmap_sem);
241
242 BUG_ON(arg_start > arg_end);
243 BUG_ON(env_start > env_end);
244
245 len1 = arg_end - arg_start;
246 len2 = env_end - env_start;
247
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700248 /* Empty ARGV. */
249 if (len1 == 0) {
250 rv = 0;
251 goto out_free_page;
252 }
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700253 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700254 * Inherently racy -- command line shares address space
255 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700256 */
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200257 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700258 if (rv <= 0)
259 goto out_free_page;
260
261 rv = 0;
262
263 if (c == '\0') {
264 /* Command line (set of strings) occupies whole ARGV. */
265 if (len1 <= *pos)
266 goto out_free_page;
267
268 p = arg_start + *pos;
269 len = len1 - *pos;
270 while (count > 0 && len > 0) {
271 unsigned int _count;
272 int nr_read;
273
274 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200275 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700276 if (nr_read < 0)
277 rv = nr_read;
278 if (nr_read <= 0)
279 goto out_free_page;
280
281 if (copy_to_user(buf, page, nr_read)) {
282 rv = -EFAULT;
283 goto out_free_page;
284 }
285
286 p += nr_read;
287 len -= nr_read;
288 buf += nr_read;
289 count -= nr_read;
290 rv += nr_read;
291 }
292 } else {
293 /*
294 * Command line (1 string) occupies ARGV and maybe
295 * extends into ENVP.
296 */
297 if (len1 + len2 <= *pos)
298 goto skip_argv_envp;
299 if (len1 <= *pos)
300 goto skip_argv;
301
302 p = arg_start + *pos;
303 len = len1 - *pos;
304 while (count > 0 && len > 0) {
305 unsigned int _count, l;
306 int nr_read;
307 bool final;
308
309 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200310 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700311 if (nr_read < 0)
312 rv = nr_read;
313 if (nr_read <= 0)
314 goto out_free_page;
315
316 /*
317 * Command line can be shorter than whole ARGV
318 * even if last "marker" byte says it is not.
319 */
320 final = false;
321 l = strnlen(page, nr_read);
322 if (l < nr_read) {
323 nr_read = l;
324 final = true;
325 }
326
327 if (copy_to_user(buf, page, nr_read)) {
328 rv = -EFAULT;
329 goto out_free_page;
330 }
331
332 p += nr_read;
333 len -= nr_read;
334 buf += nr_read;
335 count -= nr_read;
336 rv += nr_read;
337
338 if (final)
339 goto out_free_page;
340 }
341skip_argv:
342 /*
343 * Command line (1 string) occupies ARGV and
344 * extends into ENVP.
345 */
346 if (len1 <= *pos) {
347 p = env_start + *pos - len1;
348 len = len1 + len2 - *pos;
349 } else {
350 p = env_start;
351 len = len2;
352 }
353 while (count > 0 && len > 0) {
354 unsigned int _count, l;
355 int nr_read;
356 bool final;
357
358 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200359 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700360 if (nr_read < 0)
361 rv = nr_read;
362 if (nr_read <= 0)
363 goto out_free_page;
364
365 /* Find EOS. */
366 final = false;
367 l = strnlen(page, nr_read);
368 if (l < nr_read) {
369 nr_read = l;
370 final = true;
371 }
372
373 if (copy_to_user(buf, page, nr_read)) {
374 rv = -EFAULT;
375 goto out_free_page;
376 }
377
378 p += nr_read;
379 len -= nr_read;
380 buf += nr_read;
381 count -= nr_read;
382 rv += nr_read;
383
384 if (final)
385 goto out_free_page;
386 }
387skip_argv_envp:
388 ;
389 }
390
391out_free_page:
392 free_page((unsigned long)page);
393out_mmput:
394 mmput(mm);
395 if (rv > 0)
396 *pos += rv;
397 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700400static const struct file_operations proc_pid_cmdline_ops = {
401 .read = proc_pid_cmdline_read,
402 .llseek = generic_file_llseek,
403};
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#ifdef CONFIG_KALLSYMS
406/*
407 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
408 * Returns the resolved symbol. If that fails, simply return the address.
409 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700410static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
411 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700413 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700414 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 wchan = get_wchan(task);
417
Jann Horncaaee622016-01-20 15:00:04 -0800418 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
419 && !lookup_symbol_name(wchan, symname))
Joe Perches25ce3192015-04-15 16:18:17 -0700420 seq_printf(m, "%s", symname);
Ingo Molnarb2f73922015-09-30 15:59:17 +0200421 else
Robin Humble1e92a612016-05-09 16:28:46 -0700422 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700423
424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426#endif /* CONFIG_KALLSYMS */
427
Al Viroa9712bc2011-03-23 15:52:50 -0400428static int lock_trace(struct task_struct *task)
429{
430 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
431 if (err)
432 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800433 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400434 mutex_unlock(&task->signal->cred_guard_mutex);
435 return -EPERM;
436 }
437 return 0;
438}
439
440static void unlock_trace(struct task_struct *task)
441{
442 mutex_unlock(&task->signal->cred_guard_mutex);
443}
444
Ken Chen2ec220e2008-11-10 11:26:08 +0300445#ifdef CONFIG_STACKTRACE
446
447#define MAX_STACK_TRACE_DEPTH 64
448
449static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
450 struct pid *pid, struct task_struct *task)
451{
452 struct stack_trace trace;
453 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400454 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300455 int i;
456
Jann Horn3c5dc3f2018-10-05 15:51:58 -0700457 /*
458 * The ability to racily run the kernel stack unwinder on a running task
459 * and then observe the unwinder output is scary; while it is useful for
460 * debugging kernel issues, it can also allow an attacker to leak kernel
461 * stack contents.
462 * Doing this in a manner that is at least safe from races would require
463 * some work to ensure that the remote task can not be scheduled; and
464 * even then, this would still expose the unwinder as local attack
465 * surface.
466 * Therefore, this interface is restricted to root.
467 */
468 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
469 return -EACCES;
470
Ken Chen2ec220e2008-11-10 11:26:08 +0300471 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
472 if (!entries)
473 return -ENOMEM;
474
475 trace.nr_entries = 0;
476 trace.max_entries = MAX_STACK_TRACE_DEPTH;
477 trace.entries = entries;
478 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300479
Al Viroa9712bc2011-03-23 15:52:50 -0400480 err = lock_trace(task);
481 if (!err) {
482 save_stack_trace_tsk(task, &trace);
483
484 for (i = 0; i < trace.nr_entries; i++) {
Josh Poimboeuf8b927d72016-08-18 10:59:07 -0500485 seq_printf(m, "[<%pK>] %pB\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400486 (void *)entries[i], (void *)entries[i]);
487 }
488 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300489 }
490 kfree(entries);
491
Al Viroa9712bc2011-03-23 15:52:50 -0400492 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300493}
494#endif
495
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530496#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/*
498 * Provides /proc/PID/schedstat
499 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700500static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
501 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530503 if (unlikely(!sched_info_on()))
504 seq_printf(m, "0 0 0\n");
505 else
506 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700507 (unsigned long long)task->se.sum_exec_runtime,
508 (unsigned long long)task->sched_info.run_delay,
509 task->sched_info.pcount);
510
511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513#endif
514
Arjan van de Ven97455122008-01-25 21:08:34 +0100515#ifdef CONFIG_LATENCYTOP
516static int lstats_show_proc(struct seq_file *m, void *v)
517{
518 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800519 struct inode *inode = m->private;
520 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100521
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800522 if (!task)
523 return -ESRCH;
524 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100525 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800526 struct latency_record *lr = &task->latency_record[i];
527 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100528 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800529 seq_printf(m, "%i %li %li",
530 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100531 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800532 unsigned long bt = lr->backtrace[q];
533 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100534 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800535 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100536 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800537 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100538 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800539 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100540 }
541
542 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800543 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100544 return 0;
545}
546
547static int lstats_open(struct inode *inode, struct file *file)
548{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800549 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800550}
551
Arjan van de Ven97455122008-01-25 21:08:34 +0100552static ssize_t lstats_write(struct file *file, const char __user *buf,
553 size_t count, loff_t *offs)
554{
Al Viro496ad9a2013-01-23 17:07:38 -0500555 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100556
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800557 if (!task)
558 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100559 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800560 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100561
562 return count;
563}
564
565static const struct file_operations proc_lstats_operations = {
566 .open = lstats_open,
567 .read = seq_read,
568 .write = lstats_write,
569 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800570 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100571};
572
573#endif
574
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700575static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
576 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
David Rientjesa7f638f2012-05-29 15:06:47 -0700578 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200579 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Oleg Nesterovef419392016-08-02 14:03:19 -0700581 points = oom_badness(task, NULL, NULL, totalpages) *
582 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700583 seq_printf(m, "%lu\n", points);
584
585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Neil Hormand85f50d2007-10-18 23:40:37 -0700588struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700589 const char *name;
590 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700591};
592
593static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700594 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700595 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
596 [RLIMIT_DATA] = {"Max data size", "bytes"},
597 [RLIMIT_STACK] = {"Max stack size", "bytes"},
598 [RLIMIT_CORE] = {"Max core file size", "bytes"},
599 [RLIMIT_RSS] = {"Max resident set", "bytes"},
600 [RLIMIT_NPROC] = {"Max processes", "processes"},
601 [RLIMIT_NOFILE] = {"Max open files", "files"},
602 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
603 [RLIMIT_AS] = {"Max address space", "bytes"},
604 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
605 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
606 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
607 [RLIMIT_NICE] = {"Max nice priority", NULL},
608 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800609 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700610};
611
612/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700613static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
614 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700615{
616 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700617 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700618
619 struct rlimit rlim[RLIM_NLIMITS];
620
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400621 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700622 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700623 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
624 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700625
626 /*
627 * print the file header
628 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700629 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700630 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700631
632 for (i = 0; i < RLIM_NLIMITS; i++) {
633 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700634 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700635 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700636 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700637 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700638 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700639
640 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700641 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700642 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700643 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700644
645 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700646 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700647 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700648 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700649 }
650
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700651 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700652}
653
Roland McGrathebcb6732008-07-25 19:46:00 -0700654#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700655static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
656 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700657{
658 long nr;
659 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700660 int res;
661
662 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400663 if (res)
664 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700665
666 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700667 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400668 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700669 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400670 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700671 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700672 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
673 nr,
674 args[0], args[1], args[2], args[3], args[4], args[5],
675 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400676 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700677
678 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700679}
680#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/************************************************************************/
683/* Here the fs part begins */
684/************************************************************************/
685
686/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700687static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700689 struct task_struct *task;
690 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700691 /* Allow access to a task's file descriptors if it is us or we
692 * may use ptrace attach to the process and find out that
693 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700694 */
695 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700696 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800697 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700698 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700699 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700700 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800703int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700704{
705 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000706 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700707
708 if (attr->ia_valid & ATTR_MODE)
709 return -EPERM;
710
Jan Kara31051c82016-05-26 16:55:18 +0200711 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200712 if (error)
713 return error;
714
Christoph Hellwig10257742010-06-04 11:30:02 +0200715 setattr_copy(inode, attr);
716 mark_inode_dirty(inode);
717 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700718}
719
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800720/*
721 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
722 * or euid/egid (for hide_pid_min=2)?
723 */
724static bool has_pid_permissions(struct pid_namespace *pid,
725 struct task_struct *task,
726 int hide_pid_min)
727{
728 if (pid->hide_pid < hide_pid_min)
729 return true;
730 if (in_group_p(pid->pid_gid))
731 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800732 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800733}
734
735
736static int proc_pid_permission(struct inode *inode, int mask)
737{
738 struct pid_namespace *pid = inode->i_sb->s_fs_info;
739 struct task_struct *task;
740 bool has_perms;
741
742 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800743 if (!task)
744 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800745 has_perms = has_pid_permissions(pid, task, 1);
746 put_task_struct(task);
747
748 if (!has_perms) {
749 if (pid->hide_pid == 2) {
750 /*
751 * Let's make getdents(), stat(), and open()
752 * consistent with each other. If a process
753 * may not stat() a file, it shouldn't be seen
754 * in procfs at all.
755 */
756 return -ENOENT;
757 }
758
759 return -EPERM;
760 }
761 return generic_permission(inode, mask);
762}
763
764
765
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800766static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700767 .setattr = proc_setattr,
768};
769
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800770static int proc_single_show(struct seq_file *m, void *v)
771{
772 struct inode *inode = m->private;
773 struct pid_namespace *ns;
774 struct pid *pid;
775 struct task_struct *task;
776 int ret;
777
778 ns = inode->i_sb->s_fs_info;
779 pid = proc_pid(inode);
780 task = get_pid_task(pid, PIDTYPE_PID);
781 if (!task)
782 return -ESRCH;
783
784 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
785
786 put_task_struct(task);
787 return ret;
788}
789
790static int proc_single_open(struct inode *inode, struct file *filp)
791{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800792 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800793}
794
795static const struct file_operations proc_single_file_operations = {
796 .open = proc_single_open,
797 .read = seq_read,
798 .llseek = seq_lseek,
799 .release = single_release,
800};
801
Oleg Nesterov5381e162014-10-09 15:25:24 -0700802
803struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
804{
805 struct task_struct *task = get_proc_task(inode);
806 struct mm_struct *mm = ERR_PTR(-ESRCH);
807
808 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800809 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700810 put_task_struct(task);
811
812 if (!IS_ERR_OR_NULL(mm)) {
813 /* ensure this mm_struct can't be freed */
814 atomic_inc(&mm->mm_count);
815 /* but do not pin its memory */
816 mmput(mm);
817 }
818 }
819
820 return mm;
821}
822
Cong Wangb409e572012-05-31 16:26:17 -0700823static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700825 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800826
827 if (IS_ERR(mm))
828 return PTR_ERR(mm);
829
Linus Torvaldse2683372012-01-17 15:21:19 -0800830 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
832}
833
Cong Wangb409e572012-05-31 16:26:17 -0700834static int mem_open(struct inode *inode, struct file *file)
835{
Djalal Harounibc452b42012-07-30 14:42:28 -0700836 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
837
838 /* OK to pass negative loff_t, we can catch out-of-range */
839 file->f_mode |= FMODE_UNSIGNED_OFFSET;
840
841 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700842}
843
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100844static ssize_t mem_rw(struct file *file, char __user *buf,
845 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Linus Torvaldse2683372012-01-17 15:21:19 -0800847 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100848 unsigned long addr = *ppos;
849 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700851 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvaldse2683372012-01-17 15:21:19 -0800853 if (!mm)
854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Mel Gormane12ba742007-10-16 01:25:52 -0700856 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800858 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700860 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100861 if (!atomic_inc_not_zero(&mm->mm_users))
862 goto free;
863
Linus Torvalds272ddc82016-10-24 19:00:44 -0700864 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
865 flags = FOLL_FORCE;
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100866 if (write)
867 flags |= FOLL_WRITE;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100870 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100872 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 copied = -EFAULT;
874 break;
875 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100876
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100877 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100878 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (!copied)
880 copied = -EIO;
881 break;
882 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100883
884 if (!write && copy_to_user(buf, page, this_len)) {
885 copied = -EFAULT;
886 break;
887 }
888
889 buf += this_len;
890 addr += this_len;
891 copied += this_len;
892 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100894 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700895
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100896 mmput(mm);
897free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700898 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return copied;
900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100902static ssize_t mem_read(struct file *file, char __user *buf,
903 size_t count, loff_t *ppos)
904{
905 return mem_rw(file, buf, count, ppos, 0);
906}
907
908static ssize_t mem_write(struct file *file, const char __user *buf,
909 size_t count, loff_t *ppos)
910{
911 return mem_rw(file, (char __user*)buf, count, ppos, 1);
912}
913
Matt Mackall85863e42008-02-04 22:29:04 -0800914loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 switch (orig) {
917 case 0:
918 file->f_pos = offset;
919 break;
920 case 1:
921 file->f_pos += offset;
922 break;
923 default:
924 return -EINVAL;
925 }
926 force_successful_syscall_return();
927 return file->f_pos;
928}
929
Linus Torvaldse2683372012-01-17 15:21:19 -0800930static int mem_release(struct inode *inode, struct file *file)
931{
932 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100933 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100934 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800935 return 0;
936}
937
Arjan van de Ven00977a52007-02-12 00:55:34 -0800938static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 .llseek = mem_lseek,
940 .read = mem_read,
941 .write = mem_write,
942 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800943 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944};
945
Cong Wangb409e572012-05-31 16:26:17 -0700946static int environ_open(struct inode *inode, struct file *file)
947{
948 return __mem_open(inode, file, PTRACE_MODE_READ);
949}
950
James Pearson315e28c2007-10-16 23:30:17 -0700951static ssize_t environ_read(struct file *file, char __user *buf,
952 size_t count, loff_t *ppos)
953{
James Pearson315e28c2007-10-16 23:30:17 -0700954 char *page;
955 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700956 int ret = 0;
957 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800958 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700959
Mathias Krause8148a732016-05-05 16:22:26 -0700960 /* Ensure the process spawned far enough to have an environment. */
961 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700962 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700963
James Pearson315e28c2007-10-16 23:30:17 -0700964 page = (char *)__get_free_page(GFP_TEMPORARY);
965 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700966 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700967
Al Virod6f64b82011-02-15 22:26:01 -0500968 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700969 if (!atomic_inc_not_zero(&mm->mm_users))
970 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800971
972 down_read(&mm->mmap_sem);
973 env_start = mm->env_start;
974 env_end = mm->env_end;
975 up_read(&mm->mmap_sem);
976
James Pearson315e28c2007-10-16 23:30:17 -0700977 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700978 size_t this_len, max_len;
979 int retval;
980
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800981 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700982 break;
James Pearson315e28c2007-10-16 23:30:17 -0700983
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800984 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700985
Djalal Harounie8905ec2012-07-30 14:42:26 -0700986 max_len = min_t(size_t, PAGE_SIZE, count);
987 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700988
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200989 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700990
991 if (retval <= 0) {
992 ret = retval;
993 break;
994 }
995
996 if (copy_to_user(buf, page, retval)) {
997 ret = -EFAULT;
998 break;
999 }
1000
1001 ret += retval;
1002 src += retval;
1003 buf += retval;
1004 count -= retval;
1005 }
1006 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -07001007 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -07001008
1009free:
James Pearson315e28c2007-10-16 23:30:17 -07001010 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -07001011 return ret;
1012}
1013
1014static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -07001015 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -07001016 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001017 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -07001018 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -07001019};
1020
Al Viroc5317162016-10-05 18:43:43 -04001021static int auxv_open(struct inode *inode, struct file *file)
1022{
1023 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1024}
1025
1026static ssize_t auxv_read(struct file *file, char __user *buf,
1027 size_t count, loff_t *ppos)
1028{
1029 struct mm_struct *mm = file->private_data;
1030 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -07001031
1032 if (!mm)
1033 return 0;
Al Viroc5317162016-10-05 18:43:43 -04001034 do {
1035 nwords += 2;
1036 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1037 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1038 nwords * sizeof(mm->saved_auxv[0]));
1039}
1040
1041static const struct file_operations proc_auxv_operations = {
1042 .open = auxv_open,
1043 .read = auxv_read,
1044 .llseek = generic_file_llseek,
1045 .release = mem_release,
1046};
1047
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001048static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1049 loff_t *ppos)
1050{
Al Viro496ad9a2013-01-23 17:07:38 -05001051 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001052 char buffer[PROC_NUMBUF];
1053 int oom_adj = OOM_ADJUST_MIN;
1054 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001055
1056 if (!task)
1057 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001058 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1059 oom_adj = OOM_ADJUST_MAX;
1060 else
1061 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1062 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001063 put_task_struct(task);
1064 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1065 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1066}
1067
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001068static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1069{
1070 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001071 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001072 struct task_struct *task;
1073 int err = 0;
1074
1075 task = get_proc_task(file_inode(file));
1076 if (!task)
1077 return -ESRCH;
1078
1079 mutex_lock(&oom_adj_mutex);
1080 if (legacy) {
1081 if (oom_adj < task->signal->oom_score_adj &&
1082 !capable(CAP_SYS_RESOURCE)) {
1083 err = -EACCES;
1084 goto err_unlock;
1085 }
1086 /*
1087 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1088 * /proc/pid/oom_score_adj instead.
1089 */
1090 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1091 current->comm, task_pid_nr(current), task_pid_nr(task),
1092 task_pid_nr(task));
1093 } else {
1094 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1095 !capable(CAP_SYS_RESOURCE)) {
1096 err = -EACCES;
1097 goto err_unlock;
1098 }
1099 }
1100
Michal Hocko44a70ade2016-07-28 15:44:43 -07001101 /*
1102 * Make sure we will check other processes sharing the mm if this is
1103 * not vfrok which wants its own oom_score_adj.
1104 * pin the mm so it doesn't go away and get reused after task_unlock
1105 */
1106 if (!task->vfork_done) {
1107 struct task_struct *p = find_lock_task_mm(task);
1108
1109 if (p) {
1110 if (atomic_read(&p->mm->mm_users) > 1) {
1111 mm = p->mm;
1112 atomic_inc(&mm->mm_count);
1113 }
1114 task_unlock(p);
1115 }
1116 }
1117
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001118 task->signal->oom_score_adj = oom_adj;
1119 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1120 task->signal->oom_score_adj_min = (short)oom_adj;
1121 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001122
1123 if (mm) {
1124 struct task_struct *p;
1125
1126 rcu_read_lock();
1127 for_each_process(p) {
1128 if (same_thread_group(task, p))
1129 continue;
1130
1131 /* do not touch kernel threads or the global init */
1132 if (p->flags & PF_KTHREAD || is_global_init(p))
1133 continue;
1134
1135 task_lock(p);
1136 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001137 p->signal->oom_score_adj = oom_adj;
1138 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1139 p->signal->oom_score_adj_min = (short)oom_adj;
1140 }
1141 task_unlock(p);
1142 }
1143 rcu_read_unlock();
1144 mmdrop(mm);
1145 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001146err_unlock:
1147 mutex_unlock(&oom_adj_mutex);
1148 put_task_struct(task);
1149 return err;
1150}
Michal Hockof913da52016-07-28 15:44:37 -07001151
David Rientjesb72bdfa2015-11-05 18:50:32 -08001152/*
1153 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1154 * kernels. The effective policy is defined by oom_score_adj, which has a
1155 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1156 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1157 * Processes that become oom disabled via oom_adj will still be oom disabled
1158 * with this implementation.
1159 *
1160 * oom_adj cannot be removed since existing userspace binaries use it.
1161 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001162static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1163 size_t count, loff_t *ppos)
1164{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001165 char buffer[PROC_NUMBUF];
1166 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001167 int err;
1168
1169 memset(buffer, 0, sizeof(buffer));
1170 if (count > sizeof(buffer) - 1)
1171 count = sizeof(buffer) - 1;
1172 if (copy_from_user(buffer, buf, count)) {
1173 err = -EFAULT;
1174 goto out;
1175 }
1176
1177 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1178 if (err)
1179 goto out;
1180 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1181 oom_adj != OOM_DISABLE) {
1182 err = -EINVAL;
1183 goto out;
1184 }
1185
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001186 /*
1187 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1188 * value is always attainable.
1189 */
1190 if (oom_adj == OOM_ADJUST_MAX)
1191 oom_adj = OOM_SCORE_ADJ_MAX;
1192 else
1193 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1194
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001195 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001196out:
1197 return err < 0 ? err : count;
1198}
1199
1200static const struct file_operations proc_oom_adj_operations = {
1201 .read = oom_adj_read,
1202 .write = oom_adj_write,
1203 .llseek = generic_file_llseek,
1204};
1205
David Rientjesa63d83f2010-08-09 17:19:46 -07001206static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1207 size_t count, loff_t *ppos)
1208{
Al Viro496ad9a2013-01-23 17:07:38 -05001209 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001210 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001211 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001212 size_t len;
1213
1214 if (!task)
1215 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001216 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001217 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001218 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001219 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1220}
1221
1222static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1223 size_t count, loff_t *ppos)
1224{
David Rientjesa63d83f2010-08-09 17:19:46 -07001225 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001226 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001227 int err;
1228
1229 memset(buffer, 0, sizeof(buffer));
1230 if (count > sizeof(buffer) - 1)
1231 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001232 if (copy_from_user(buffer, buf, count)) {
1233 err = -EFAULT;
1234 goto out;
1235 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001236
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001237 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001238 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001239 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001240 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001241 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1242 err = -EINVAL;
1243 goto out;
1244 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001245
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001246 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001247out:
1248 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001249}
1250
1251static const struct file_operations proc_oom_score_adj_operations = {
1252 .read = oom_score_adj_read,
1253 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001254 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001255};
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257#ifdef CONFIG_AUDITSYSCALL
1258#define TMPBUFLEN 21
1259static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1260 size_t count, loff_t *ppos)
1261{
Al Viro496ad9a2013-01-23 17:07:38 -05001262 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001263 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 ssize_t length;
1265 char tmpbuf[TMPBUFLEN];
1266
Eric W. Biederman99f89552006-06-26 00:25:55 -07001267 if (!task)
1268 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001270 from_kuid(file->f_cred->user_ns,
1271 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001272 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1274}
1275
1276static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1277 size_t count, loff_t *ppos)
1278{
Al Viro496ad9a2013-01-23 17:07:38 -05001279 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001281 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001282 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001284 rcu_read_lock();
1285 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1286 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001288 }
1289 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (*ppos != 0) {
1292 /* No partial writes. */
1293 return -EINVAL;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001296 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1297 if (rv < 0)
1298 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001299
1300 /* is userspace tring to explicitly UNSET the loginuid? */
1301 if (loginuid == AUDIT_UID_UNSET) {
1302 kloginuid = INVALID_UID;
1303 } else {
1304 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001305 if (!uid_valid(kloginuid))
1306 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001307 }
1308
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001309 rv = audit_set_loginuid(kloginuid);
1310 if (rv < 0)
1311 return rv;
1312 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Arjan van de Ven00977a52007-02-12 00:55:34 -08001315static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 .read = proc_loginuid_read,
1317 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001318 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319};
Eric Paris1e0bd752008-03-13 08:15:31 -04001320
1321static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1322 size_t count, loff_t *ppos)
1323{
Al Viro496ad9a2013-01-23 17:07:38 -05001324 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001325 struct task_struct *task = get_proc_task(inode);
1326 ssize_t length;
1327 char tmpbuf[TMPBUFLEN];
1328
1329 if (!task)
1330 return -ESRCH;
1331 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1332 audit_get_sessionid(task));
1333 put_task_struct(task);
1334 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1335}
1336
1337static const struct file_operations proc_sessionid_operations = {
1338 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001339 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001340};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341#endif
1342
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001343#ifdef CONFIG_FAULT_INJECTION
1344static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1345 size_t count, loff_t *ppos)
1346{
Al Viro496ad9a2013-01-23 17:07:38 -05001347 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001348 char buffer[PROC_NUMBUF];
1349 size_t len;
1350 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001351
1352 if (!task)
1353 return -ESRCH;
1354 make_it_fail = task->make_it_fail;
1355 put_task_struct(task);
1356
1357 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001358
1359 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001360}
1361
1362static ssize_t proc_fault_inject_write(struct file * file,
1363 const char __user * buf, size_t count, loff_t *ppos)
1364{
1365 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001366 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001367 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001368 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001369
1370 if (!capable(CAP_SYS_RESOURCE))
1371 return -EPERM;
1372 memset(buffer, 0, sizeof(buffer));
1373 if (count > sizeof(buffer) - 1)
1374 count = sizeof(buffer) - 1;
1375 if (copy_from_user(buffer, buf, count))
1376 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001377 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1378 if (rv < 0)
1379 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001380 if (make_it_fail < 0 || make_it_fail > 1)
1381 return -EINVAL;
1382
Al Viro496ad9a2013-01-23 17:07:38 -05001383 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001384 if (!task)
1385 return -ESRCH;
1386 task->make_it_fail = make_it_fail;
1387 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001388
1389 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001390}
1391
Arjan van de Ven00977a52007-02-12 00:55:34 -08001392static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001393 .read = proc_fault_inject_read,
1394 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001395 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001396};
1397#endif
1398
Arjan van de Ven97455122008-01-25 21:08:34 +01001399
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001400#ifdef CONFIG_SCHED_DEBUG
1401/*
1402 * Print out various scheduling related per-task fields:
1403 */
1404static int sched_show(struct seq_file *m, void *v)
1405{
1406 struct inode *inode = m->private;
1407 struct task_struct *p;
1408
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001409 p = get_proc_task(inode);
1410 if (!p)
1411 return -ESRCH;
1412 proc_sched_show_task(p, m);
1413
1414 put_task_struct(p);
1415
1416 return 0;
1417}
1418
1419static ssize_t
1420sched_write(struct file *file, const char __user *buf,
1421 size_t count, loff_t *offset)
1422{
Al Viro496ad9a2013-01-23 17:07:38 -05001423 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001424 struct task_struct *p;
1425
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001426 p = get_proc_task(inode);
1427 if (!p)
1428 return -ESRCH;
1429 proc_sched_set_task(p);
1430
1431 put_task_struct(p);
1432
1433 return count;
1434}
1435
1436static int sched_open(struct inode *inode, struct file *filp)
1437{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001438 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001439}
1440
1441static const struct file_operations proc_pid_sched_operations = {
1442 .open = sched_open,
1443 .read = seq_read,
1444 .write = sched_write,
1445 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001446 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001447};
1448
1449#endif
1450
Mike Galbraith5091faa2010-11-30 14:18:03 +01001451#ifdef CONFIG_SCHED_AUTOGROUP
1452/*
1453 * Print out autogroup related information:
1454 */
1455static int sched_autogroup_show(struct seq_file *m, void *v)
1456{
1457 struct inode *inode = m->private;
1458 struct task_struct *p;
1459
1460 p = get_proc_task(inode);
1461 if (!p)
1462 return -ESRCH;
1463 proc_sched_autogroup_show_task(p, m);
1464
1465 put_task_struct(p);
1466
1467 return 0;
1468}
1469
1470static ssize_t
1471sched_autogroup_write(struct file *file, const char __user *buf,
1472 size_t count, loff_t *offset)
1473{
Al Viro496ad9a2013-01-23 17:07:38 -05001474 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001475 struct task_struct *p;
1476 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001477 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001478 int err;
1479
1480 memset(buffer, 0, sizeof(buffer));
1481 if (count > sizeof(buffer) - 1)
1482 count = sizeof(buffer) - 1;
1483 if (copy_from_user(buffer, buf, count))
1484 return -EFAULT;
1485
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001486 err = kstrtoint(strstrip(buffer), 0, &nice);
1487 if (err < 0)
1488 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001489
1490 p = get_proc_task(inode);
1491 if (!p)
1492 return -ESRCH;
1493
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001494 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001495 if (err)
1496 count = err;
1497
1498 put_task_struct(p);
1499
1500 return count;
1501}
1502
1503static int sched_autogroup_open(struct inode *inode, struct file *filp)
1504{
1505 int ret;
1506
1507 ret = single_open(filp, sched_autogroup_show, NULL);
1508 if (!ret) {
1509 struct seq_file *m = filp->private_data;
1510
1511 m->private = inode;
1512 }
1513 return ret;
1514}
1515
1516static const struct file_operations proc_pid_sched_autogroup_operations = {
1517 .open = sched_autogroup_open,
1518 .read = seq_read,
1519 .write = sched_autogroup_write,
1520 .llseek = seq_lseek,
1521 .release = single_release,
1522};
1523
1524#endif /* CONFIG_SCHED_AUTOGROUP */
1525
john stultz4614a696b2009-12-14 18:00:05 -08001526static ssize_t comm_write(struct file *file, const char __user *buf,
1527 size_t count, loff_t *offset)
1528{
Al Viro496ad9a2013-01-23 17:07:38 -05001529 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001530 struct task_struct *p;
1531 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001532 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001533
1534 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001535 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001536 return -EFAULT;
1537
1538 p = get_proc_task(inode);
1539 if (!p)
1540 return -ESRCH;
1541
1542 if (same_thread_group(current, p))
1543 set_task_comm(p, buffer);
1544 else
1545 count = -EINVAL;
1546
1547 put_task_struct(p);
1548
1549 return count;
1550}
1551
1552static int comm_show(struct seq_file *m, void *v)
1553{
1554 struct inode *inode = m->private;
1555 struct task_struct *p;
1556
1557 p = get_proc_task(inode);
1558 if (!p)
1559 return -ESRCH;
1560
1561 task_lock(p);
1562 seq_printf(m, "%s\n", p->comm);
1563 task_unlock(p);
1564
1565 put_task_struct(p);
1566
1567 return 0;
1568}
1569
1570static int comm_open(struct inode *inode, struct file *filp)
1571{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001572 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001573}
1574
1575static const struct file_operations proc_pid_set_comm_operations = {
1576 .open = comm_open,
1577 .read = seq_read,
1578 .write = comm_write,
1579 .llseek = seq_lseek,
1580 .release = single_release,
1581};
1582
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001583static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001584{
1585 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001586 struct file *exe_file;
1587
David Howells2b0143b2015-03-17 22:25:59 +00001588 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001589 if (!task)
1590 return -ENOENT;
Mateusz Guzikcd81a912016-08-23 16:20:38 +02001591 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001592 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001593 if (exe_file) {
1594 *exe_path = exe_file->f_path;
1595 path_get(&exe_file->f_path);
1596 fput(exe_file);
1597 return 0;
1598 } else
1599 return -ENOENT;
1600}
1601
Al Viro6b255392015-11-17 10:20:54 -05001602static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001603 struct inode *inode,
1604 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001606 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 int error = -EACCES;
1608
Al Viro6b255392015-11-17 10:20:54 -05001609 if (!dentry)
1610 return ERR_PTR(-ECHILD);
1611
Eric W. Biederman778c1142006-06-26 00:25:58 -07001612 /* Are we allowed to snoop on the tasks file descriptors? */
1613 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Christoph Hellwig408ef012012-06-18 10:47:03 -04001616 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1617 if (error)
1618 goto out;
1619
Al Viro6e771372015-05-02 13:37:52 -04001620 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001621 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622out:
Al Viro008b1502005-08-20 00:17:39 +01001623 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001626static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Mel Gormane12ba742007-10-16 01:25:52 -07001628 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001629 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 int len;
1631
1632 if (!tmp)
1633 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001634
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001635 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001636 len = PTR_ERR(pathname);
1637 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001639 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 if (len > buflen)
1642 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001643 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 len = -EFAULT;
1645 out:
1646 free_page((unsigned long)tmp);
1647 return len;
1648}
1649
1650static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1651{
1652 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001653 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001654 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Eric W. Biederman778c1142006-06-26 00:25:58 -07001656 /* Are we allowed to snoop on the tasks file descriptors? */
1657 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001660 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (error)
1662 goto out;
1663
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001664 error = do_proc_readlink(&path, buffer, buflen);
1665 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return error;
1668}
1669
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001670const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001672 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001673 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674};
1675
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001676
1677/* building an inode */
1678
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001679struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001680{
1681 struct inode * inode;
1682 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001683 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001684
1685 /* We need a new inode */
1686
1687 inode = new_inode(sb);
1688 if (!inode)
1689 goto out;
1690
1691 /* Common stuff */
1692 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001693 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001694 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001695 inode->i_op = &proc_def_inode_operations;
1696
1697 /*
1698 * grab the reference to task.
1699 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001700 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001701 if (!ei->pid)
1702 goto out_unlock;
1703
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001704 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001705 rcu_read_lock();
1706 cred = __task_cred(task);
1707 inode->i_uid = cred->euid;
1708 inode->i_gid = cred->egid;
1709 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001710 }
1711 security_task_to_inode(task, inode);
1712
1713out:
1714 return inode;
1715
1716out_unlock:
1717 iput(inode);
1718 return NULL;
1719}
1720
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001721int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001722{
David Howells2b0143b2015-03-17 22:25:59 +00001723 struct inode *inode = d_inode(dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001724 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001725 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001726 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001727
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001728 generic_fillattr(inode, stat);
1729
1730 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001731 stat->uid = GLOBAL_ROOT_UID;
1732 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001733 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1734 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001735 if (!has_pid_permissions(pid, task, 2)) {
1736 rcu_read_unlock();
1737 /*
1738 * This doesn't prevent learning whether PID exists,
1739 * it only makes getattr() consistent with readdir().
1740 */
1741 return -ENOENT;
1742 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001743 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1744 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001745 cred = __task_cred(task);
1746 stat->uid = cred->euid;
1747 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001748 }
1749 }
1750 rcu_read_unlock();
1751 return 0;
1752}
1753
1754/* dentry stuff */
1755
1756/*
1757 * Exceptional case: normally we are not allowed to unhash a busy
1758 * directory. In this case, however, we can do it - no aliasing problems
1759 * due to the way we treat inodes.
1760 *
1761 * Rewrite the inode's ownerships here because the owning task may have
1762 * performed a setuid(), etc.
1763 *
1764 * Before the /proc/pid/status file was created the only way to read
1765 * the effective uid of a /process was to stat /proc/pid. Reading
1766 * /proc/pid/status is slow enough that procps and other packages
1767 * kept stating /proc/pid. To keep the rules in /proc simple I have
1768 * made this apply to all per process world readable and executable
1769 * directories.
1770 */
Al Viro0b728e12012-06-10 16:03:43 -04001771int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001772{
Nick Piggin34286d62011-01-07 17:49:57 +11001773 struct inode *inode;
1774 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001775 const struct cred *cred;
1776
Al Viro0b728e12012-06-10 16:03:43 -04001777 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001778 return -ECHILD;
1779
David Howells2b0143b2015-03-17 22:25:59 +00001780 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001781 task = get_proc_task(inode);
1782
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001783 if (task) {
1784 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1785 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001786 rcu_read_lock();
1787 cred = __task_cred(task);
1788 inode->i_uid = cred->euid;
1789 inode->i_gid = cred->egid;
1790 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001791 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001792 inode->i_uid = GLOBAL_ROOT_UID;
1793 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001794 }
1795 inode->i_mode &= ~(S_ISUID | S_ISGID);
1796 security_task_to_inode(task, inode);
1797 put_task_struct(task);
1798 return 1;
1799 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001800 return 0;
1801}
1802
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001803static inline bool proc_inode_is_dead(struct inode *inode)
1804{
1805 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1806}
1807
David Howells1dd704b2013-04-12 01:08:50 +01001808int pid_delete_dentry(const struct dentry *dentry)
1809{
1810 /* Is the task we represent dead?
1811 * If so, then don't put the dentry on the lru list,
1812 * kill it immediately.
1813 */
David Howells2b0143b2015-03-17 22:25:59 +00001814 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001815}
1816
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001817const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001818{
1819 .d_revalidate = pid_revalidate,
1820 .d_delete = pid_delete_dentry,
1821};
1822
1823/* Lookups */
1824
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001825/*
1826 * Fill a directory entry.
1827 *
1828 * If possible create the dcache entry and derive our inode number and
1829 * file type from dcache entry.
1830 *
1831 * Since all of the proc inode numbers are dynamically generated, the inode
1832 * numbers do not exist until the inode is cache. This means creating the
1833 * the dcache entry in readdir is necessary to keep the inode numbers
1834 * reported by readdir in sync with the inode numbers reported
1835 * by stat.
1836 */
Al Virof0c3b502013-05-16 12:07:31 -04001837bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001838 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001839 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001840{
Al Virof0c3b502013-05-16 12:07:31 -04001841 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001842 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001843 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001844 unsigned type;
1845 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001846
Al Viro1df98b82013-06-15 11:33:10 +04001847 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001848 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001849 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1850 child = d_alloc_parallel(dir, &qname, &wq);
1851 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001852 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001853 if (d_in_lookup(child)) {
1854 int err = instantiate(d_inode(dir), child, task, ptr);
1855 d_lookup_done(child);
1856 if (err < 0) {
1857 dput(child);
1858 goto end_instantiate;
1859 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001860 }
1861 }
David Howells2b0143b2015-03-17 22:25:59 +00001862 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001863 ino = inode->i_ino;
1864 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001865 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001866 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001867
1868end_instantiate:
1869 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001870}
1871
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001872/*
1873 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1874 * which represent vma start and end addresses.
1875 */
1876static int dname_to_vma_addr(struct dentry *dentry,
1877 unsigned long *start, unsigned long *end)
1878{
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08001879 const char *str = dentry->d_name.name;
1880 unsigned long long sval, eval;
1881 unsigned int len;
1882
1883 len = _parse_integer(str, 16, &sval);
1884 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001885 return -EINVAL;
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08001886 if (sval != (unsigned long)sval)
1887 return -EINVAL;
1888 str += len;
1889
1890 if (*str != '-')
1891 return -EINVAL;
1892 str++;
1893
1894 len = _parse_integer(str, 16, &eval);
1895 if (len & KSTRTOX_OVERFLOW)
1896 return -EINVAL;
1897 if (eval != (unsigned long)eval)
1898 return -EINVAL;
1899 str += len;
1900
1901 if (*str != '\0')
1902 return -EINVAL;
1903
1904 *start = sval;
1905 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001906
1907 return 0;
1908}
1909
Al Viro0b728e12012-06-10 16:03:43 -04001910static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001911{
1912 unsigned long vm_start, vm_end;
1913 bool exact_vma_exists = false;
1914 struct mm_struct *mm = NULL;
1915 struct task_struct *task;
1916 const struct cred *cred;
1917 struct inode *inode;
1918 int status = 0;
1919
Al Viro0b728e12012-06-10 16:03:43 -04001920 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001921 return -ECHILD;
1922
David Howells2b0143b2015-03-17 22:25:59 +00001923 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001924 task = get_proc_task(inode);
1925 if (!task)
1926 goto out_notask;
1927
Jann Horncaaee622016-01-20 15:00:04 -08001928 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001929 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001930 goto out;
1931
1932 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1933 down_read(&mm->mmap_sem);
1934 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1935 up_read(&mm->mmap_sem);
1936 }
1937
1938 mmput(mm);
1939
1940 if (exact_vma_exists) {
1941 if (task_dumpable(task)) {
1942 rcu_read_lock();
1943 cred = __task_cred(task);
1944 inode->i_uid = cred->euid;
1945 inode->i_gid = cred->egid;
1946 rcu_read_unlock();
1947 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001948 inode->i_uid = GLOBAL_ROOT_UID;
1949 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001950 }
1951 security_task_to_inode(task, inode);
1952 status = 1;
1953 }
1954
1955out:
1956 put_task_struct(task);
1957
1958out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001959 return status;
1960}
1961
1962static const struct dentry_operations tid_map_files_dentry_operations = {
1963 .d_revalidate = map_files_d_revalidate,
1964 .d_delete = pid_delete_dentry,
1965};
1966
Al Viro6b255392015-11-17 10:20:54 -05001967static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001968{
1969 unsigned long vm_start, vm_end;
1970 struct vm_area_struct *vma;
1971 struct task_struct *task;
1972 struct mm_struct *mm;
1973 int rc;
1974
1975 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001976 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001977 if (!task)
1978 goto out;
1979
1980 mm = get_task_mm(task);
1981 put_task_struct(task);
1982 if (!mm)
1983 goto out;
1984
1985 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1986 if (rc)
1987 goto out_mmput;
1988
Artem Fetishev70335ab2014-03-10 15:49:45 -07001989 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001990 down_read(&mm->mmap_sem);
1991 vma = find_exact_vma(mm, vm_start, vm_end);
1992 if (vma && vma->vm_file) {
1993 *path = vma->vm_file->f_path;
1994 path_get(path);
1995 rc = 0;
1996 }
1997 up_read(&mm->mmap_sem);
1998
1999out_mmput:
2000 mmput(mm);
2001out:
2002 return rc;
2003}
2004
2005struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04002006 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002007 unsigned long len;
2008 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2009};
2010
Calvin Owensbdb4d102015-09-09 15:35:54 -07002011/*
2012 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2013 * symlinks may be used to bypass permissions on ancestor directories in the
2014 * path to the file in question.
2015 */
2016static const char *
Al Viro6b255392015-11-17 10:20:54 -05002017proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002018 struct inode *inode,
2019 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002020{
2021 if (!capable(CAP_SYS_ADMIN))
2022 return ERR_PTR(-EPERM);
2023
Al Virofceef392015-12-29 15:58:39 -05002024 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002025}
2026
2027/*
Al Viro6b255392015-11-17 10:20:54 -05002028 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002029 */
2030static const struct inode_operations proc_map_files_link_inode_operations = {
2031 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002032 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002033 .setattr = proc_setattr,
2034};
2035
Al Viroc52a47a2013-06-15 11:15:20 +04002036static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002037proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2038 struct task_struct *task, const void *ptr)
2039{
Al Viro7b540d02012-08-27 14:55:26 -04002040 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002041 struct proc_inode *ei;
2042 struct inode *inode;
2043
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002044 inode = proc_pid_make_inode(dir->i_sb, task);
2045 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002046 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002047
2048 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002049 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002050
Calvin Owensbdb4d102015-09-09 15:35:54 -07002051 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002052 inode->i_size = 64;
2053 inode->i_mode = S_IFLNK;
2054
Al Viro7b540d02012-08-27 14:55:26 -04002055 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002056 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04002057 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002058 inode->i_mode |= S_IWUSR;
2059
2060 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2061 d_add(dentry, inode);
2062
Al Viroc52a47a2013-06-15 11:15:20 +04002063 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002064}
2065
2066static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002067 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002068{
2069 unsigned long vm_start, vm_end;
2070 struct vm_area_struct *vma;
2071 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002072 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002073 struct mm_struct *mm;
2074
Al Viroc52a47a2013-06-15 11:15:20 +04002075 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002076 task = get_proc_task(dir);
2077 if (!task)
2078 goto out;
2079
Al Viroc52a47a2013-06-15 11:15:20 +04002080 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002081 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002082 goto out_put_task;
2083
Al Viroc52a47a2013-06-15 11:15:20 +04002084 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002085 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002086 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002087
2088 mm = get_task_mm(task);
2089 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002090 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002091
2092 down_read(&mm->mmap_sem);
2093 vma = find_exact_vma(mm, vm_start, vm_end);
2094 if (!vma)
2095 goto out_no_vma;
2096
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002097 if (vma->vm_file)
2098 result = proc_map_files_instantiate(dir, dentry, task,
2099 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002100
2101out_no_vma:
2102 up_read(&mm->mmap_sem);
2103 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002104out_put_task:
2105 put_task_struct(task);
2106out:
Al Viroc52a47a2013-06-15 11:15:20 +04002107 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108}
2109
2110static const struct inode_operations proc_map_files_inode_operations = {
2111 .lookup = proc_map_files_lookup,
2112 .permission = proc_fd_permission,
2113 .setattr = proc_setattr,
2114};
2115
2116static int
Al Virof0c3b502013-05-16 12:07:31 -04002117proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002118{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002119 struct vm_area_struct *vma;
2120 struct task_struct *task;
2121 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002122 unsigned long nr_files, pos, i;
2123 struct flex_array *fa = NULL;
2124 struct map_files_info info;
2125 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002126 int ret;
2127
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002128 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002129 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002130 if (!task)
2131 goto out;
2132
2133 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002134 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002135 goto out_put_task;
2136
2137 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002138 if (!dir_emit_dots(file, ctx))
2139 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002140
Al Virof0c3b502013-05-16 12:07:31 -04002141 mm = get_task_mm(task);
2142 if (!mm)
2143 goto out_put_task;
2144 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002145
Al Virof0c3b502013-05-16 12:07:31 -04002146 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002147
Al Virof0c3b502013-05-16 12:07:31 -04002148 /*
2149 * We need two passes here:
2150 *
2151 * 1) Collect vmas of mapped files with mmap_sem taken
2152 * 2) Release mmap_sem and instantiate entries
2153 *
2154 * otherwise we get lockdep complained, since filldir()
2155 * routine might require mmap_sem taken in might_fault().
2156 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002157
Al Virof0c3b502013-05-16 12:07:31 -04002158 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2159 if (vma->vm_file && ++pos > ctx->pos)
2160 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002161 }
Al Virof0c3b502013-05-16 12:07:31 -04002162
2163 if (nr_files) {
2164 fa = flex_array_alloc(sizeof(info), nr_files,
2165 GFP_KERNEL);
2166 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2167 GFP_KERNEL)) {
2168 ret = -ENOMEM;
2169 if (fa)
2170 flex_array_free(fa);
2171 up_read(&mm->mmap_sem);
2172 mmput(mm);
2173 goto out_put_task;
2174 }
2175 for (i = 0, vma = mm->mmap, pos = 2; vma;
2176 vma = vma->vm_next) {
2177 if (!vma->vm_file)
2178 continue;
2179 if (++pos <= ctx->pos)
2180 continue;
2181
2182 info.mode = vma->vm_file->f_mode;
2183 info.len = snprintf(info.name,
2184 sizeof(info.name), "%lx-%lx",
2185 vma->vm_start, vma->vm_end);
2186 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2187 BUG();
2188 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002189 }
Al Virof0c3b502013-05-16 12:07:31 -04002190 up_read(&mm->mmap_sem);
2191
2192 for (i = 0; i < nr_files; i++) {
2193 p = flex_array_get(fa, i);
2194 if (!proc_fill_cache(file, ctx,
2195 p->name, p->len,
2196 proc_map_files_instantiate,
2197 task,
2198 (void *)(unsigned long)p->mode))
2199 break;
2200 ctx->pos++;
2201 }
2202 if (fa)
2203 flex_array_free(fa);
2204 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002205
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002206out_put_task:
2207 put_task_struct(task);
2208out:
2209 return ret;
2210}
2211
2212static const struct file_operations proc_map_files_operations = {
2213 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002214 .iterate_shared = proc_map_files_readdir,
2215 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002216};
2217
Eric Engestromb5946be2016-03-17 14:20:57 -07002218#ifdef CONFIG_CHECKPOINT_RESTORE
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002219struct timers_private {
2220 struct pid *pid;
2221 struct task_struct *task;
2222 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002223 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002224 unsigned long flags;
2225};
2226
2227static void *timers_start(struct seq_file *m, loff_t *pos)
2228{
2229 struct timers_private *tp = m->private;
2230
2231 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2232 if (!tp->task)
2233 return ERR_PTR(-ESRCH);
2234
2235 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2236 if (!tp->sighand)
2237 return ERR_PTR(-ESRCH);
2238
2239 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2240}
2241
2242static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2243{
2244 struct timers_private *tp = m->private;
2245 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2246}
2247
2248static void timers_stop(struct seq_file *m, void *v)
2249{
2250 struct timers_private *tp = m->private;
2251
2252 if (tp->sighand) {
2253 unlock_task_sighand(tp->task, &tp->flags);
2254 tp->sighand = NULL;
2255 }
2256
2257 if (tp->task) {
2258 put_task_struct(tp->task);
2259 tp->task = NULL;
2260 }
2261}
2262
2263static int show_timer(struct seq_file *m, void *v)
2264{
2265 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002266 struct timers_private *tp = m->private;
2267 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002268 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002269 [SIGEV_SIGNAL] = "signal",
2270 [SIGEV_NONE] = "none",
2271 [SIGEV_THREAD] = "thread",
2272 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002273
2274 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002275 notify = timer->it_sigev_notify;
2276
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002277 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002278 seq_printf(m, "signal: %d/%p\n",
2279 timer->sigq->info.si_signo,
2280 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002281 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002282 nstr[notify & ~SIGEV_THREAD_ID],
2283 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2284 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002285 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002286
2287 return 0;
2288}
2289
2290static const struct seq_operations proc_timers_seq_ops = {
2291 .start = timers_start,
2292 .next = timers_next,
2293 .stop = timers_stop,
2294 .show = show_timer,
2295};
2296
2297static int proc_timers_open(struct inode *inode, struct file *file)
2298{
2299 struct timers_private *tp;
2300
2301 tp = __seq_open_private(file, &proc_timers_seq_ops,
2302 sizeof(struct timers_private));
2303 if (!tp)
2304 return -ENOMEM;
2305
2306 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002307 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002308 return 0;
2309}
2310
2311static const struct file_operations proc_timers_operations = {
2312 .open = proc_timers_open,
2313 .read = seq_read,
2314 .llseek = seq_lseek,
2315 .release = seq_release_private,
2316};
Eric Engestromb5946be2016-03-17 14:20:57 -07002317#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002318
John Stultz5de23d42016-03-17 14:20:54 -07002319static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2320 size_t count, loff_t *offset)
2321{
2322 struct inode *inode = file_inode(file);
2323 struct task_struct *p;
2324 u64 slack_ns;
2325 int err;
2326
2327 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2328 if (err < 0)
2329 return err;
2330
2331 p = get_proc_task(inode);
2332 if (!p)
2333 return -ESRCH;
2334
John Stultz4b2bd5f2016-10-07 17:02:33 -07002335 if (p != current) {
2336 if (!capable(CAP_SYS_NICE)) {
2337 count = -EPERM;
2338 goto out;
2339 }
John Stultz5de23d42016-03-17 14:20:54 -07002340
John Stultz4b2bd5f2016-10-07 17:02:33 -07002341 err = security_task_setscheduler(p);
2342 if (err) {
2343 count = err;
2344 goto out;
2345 }
John Stultz904763e2016-10-07 17:02:29 -07002346 }
2347
John Stultz7abbaf92016-10-07 17:02:26 -07002348 task_lock(p);
2349 if (slack_ns == 0)
2350 p->timer_slack_ns = p->default_timer_slack_ns;
2351 else
2352 p->timer_slack_ns = slack_ns;
2353 task_unlock(p);
2354
2355out:
John Stultz5de23d42016-03-17 14:20:54 -07002356 put_task_struct(p);
2357
2358 return count;
2359}
2360
2361static int timerslack_ns_show(struct seq_file *m, void *v)
2362{
2363 struct inode *inode = m->private;
2364 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002365 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002366
2367 p = get_proc_task(inode);
2368 if (!p)
2369 return -ESRCH;
2370
John Stultz4b2bd5f2016-10-07 17:02:33 -07002371 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002372
John Stultz4b2bd5f2016-10-07 17:02:33 -07002373 if (!capable(CAP_SYS_NICE)) {
2374 err = -EPERM;
2375 goto out;
2376 }
2377 err = security_task_getscheduler(p);
2378 if (err)
2379 goto out;
2380 }
John Stultz904763e2016-10-07 17:02:29 -07002381
John Stultz7abbaf92016-10-07 17:02:26 -07002382 task_lock(p);
2383 seq_printf(m, "%llu\n", p->timer_slack_ns);
2384 task_unlock(p);
2385
2386out:
John Stultz5de23d42016-03-17 14:20:54 -07002387 put_task_struct(p);
2388
2389 return err;
2390}
2391
2392static int timerslack_ns_open(struct inode *inode, struct file *filp)
2393{
2394 return single_open(filp, timerslack_ns_show, inode);
2395}
2396
2397static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2398 .open = timerslack_ns_open,
2399 .read = seq_read,
2400 .write = timerslack_ns_write,
2401 .llseek = seq_lseek,
2402 .release = single_release,
2403};
2404
Al Viroc52a47a2013-06-15 11:15:20 +04002405static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002406 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002407{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002408 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002409 struct inode *inode;
2410 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002411
Eric W. Biederman61a28782006-10-02 02:18:49 -07002412 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002413 if (!inode)
2414 goto out;
2415
2416 ei = PROC_I(inode);
2417 inode->i_mode = p->mode;
2418 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002419 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002420 if (p->iop)
2421 inode->i_op = p->iop;
2422 if (p->fop)
2423 inode->i_fop = p->fop;
2424 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002425 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002426 d_add(dentry, inode);
2427 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002428 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002429 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002430out:
Al Viroc52a47a2013-06-15 11:15:20 +04002431 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002432}
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434static struct dentry *proc_pident_lookup(struct inode *dir,
2435 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002436 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002437 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Al Viroc52a47a2013-06-15 11:15:20 +04002439 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002440 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002441 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Al Viroc52a47a2013-06-15 11:15:20 +04002443 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Eric W. Biederman99f89552006-06-26 00:25:55 -07002445 if (!task)
2446 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002448 /*
2449 * Yes, it does not scale. And it should not. Don't add
2450 * new entries into /proc/<tgid>/ without very good reasons.
2451 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002452 last = &ents[nents - 1];
2453 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (p->len != dentry->d_name.len)
2455 continue;
2456 if (!memcmp(dentry->d_name.name, p->name, p->len))
2457 break;
2458 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002459 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 goto out;
2461
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002462 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002464 put_task_struct(task);
2465out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002466 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Al Virof0c3b502013-05-16 12:07:31 -04002469static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002470 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002471{
Al Virof0c3b502013-05-16 12:07:31 -04002472 struct task_struct *task = get_proc_task(file_inode(file));
2473 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002474
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002475 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002476 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002477
Al Virof0c3b502013-05-16 12:07:31 -04002478 if (!dir_emit_dots(file, ctx))
2479 goto out;
2480
2481 if (ctx->pos >= nents + 2)
2482 goto out;
2483
2484 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2485 if (!proc_fill_cache(file, ctx, p->name, p->len,
2486 proc_pident_instantiate, task, p))
2487 break;
2488 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002489 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002491 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2497 size_t count, loff_t *ppos)
2498{
Al Viro496ad9a2013-01-23 17:07:38 -05002499 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002500 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002501 ssize_t length;
2502 struct task_struct *task = get_proc_task(inode);
2503
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002504 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002505 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002506
2507 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002508 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002509 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002510 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002511 if (length > 0)
2512 length = simple_read_from_buffer(buf, count, ppos, p, length);
2513 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514 return length;
2515}
2516
2517static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2518 size_t count, loff_t *ppos)
2519{
Al Viro496ad9a2013-01-23 17:07:38 -05002520 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002521 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522 ssize_t length;
2523 struct task_struct *task = get_proc_task(inode);
2524
2525 length = -ESRCH;
2526 if (!task)
2527 goto out_no_task;
2528 if (count > PAGE_SIZE)
2529 count = PAGE_SIZE;
2530
2531 /* No partial writes. */
2532 length = -EINVAL;
2533 if (*ppos != 0)
2534 goto out;
2535
Al Virobb646cd2015-12-24 00:16:30 -05002536 page = memdup_user(buf, count);
2537 if (IS_ERR(page)) {
2538 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002539 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002540 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002541
David Howells107db7c2009-05-08 13:55:27 +01002542 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002543 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002544 if (length < 0)
2545 goto out_free;
2546
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002547 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002548 (char*)file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002549 page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002550 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002552 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002553out:
2554 put_task_struct(task);
2555out_no_task:
2556 return length;
2557}
2558
Arjan van de Ven00977a52007-02-12 00:55:34 -08002559static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002560 .read = proc_pid_attr_read,
2561 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002562 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002563};
2564
Eric Dumazetc5141e62007-05-08 00:26:15 -07002565static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002566 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2567 REG("prev", S_IRUGO, proc_pid_attr_operations),
2568 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2569 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2570 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2571 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002572};
2573
Al Virof0c3b502013-05-16 12:07:31 -04002574static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Al Virof0c3b502013-05-16 12:07:31 -04002576 return proc_pident_readdir(file, ctx,
2577 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
Arjan van de Ven00977a52007-02-12 00:55:34 -08002580static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002582 .iterate_shared = proc_attr_dir_readdir,
2583 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584};
2585
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002586static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002587 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002589 return proc_pident_lookup(dir, dentry,
2590 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002593static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002594 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002595 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002596 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597};
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599#endif
2600
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002601#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002602static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2603 size_t count, loff_t *ppos)
2604{
Al Viro496ad9a2013-01-23 17:07:38 -05002605 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002606 struct mm_struct *mm;
2607 char buffer[PROC_NUMBUF];
2608 size_t len;
2609 int ret;
2610
2611 if (!task)
2612 return -ESRCH;
2613
2614 ret = 0;
2615 mm = get_task_mm(task);
2616 if (mm) {
2617 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2618 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2619 MMF_DUMP_FILTER_SHIFT));
2620 mmput(mm);
2621 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2622 }
2623
2624 put_task_struct(task);
2625
2626 return ret;
2627}
2628
2629static ssize_t proc_coredump_filter_write(struct file *file,
2630 const char __user *buf,
2631 size_t count,
2632 loff_t *ppos)
2633{
2634 struct task_struct *task;
2635 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002636 unsigned int val;
2637 int ret;
2638 int i;
2639 unsigned long mask;
2640
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002641 ret = kstrtouint_from_user(buf, count, 0, &val);
2642 if (ret < 0)
2643 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002644
2645 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002646 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002647 if (!task)
2648 goto out_no_task;
2649
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002650 mm = get_task_mm(task);
2651 if (!mm)
2652 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002653 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002654
2655 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2656 if (val & mask)
2657 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2658 else
2659 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2660 }
2661
2662 mmput(mm);
2663 out_no_mm:
2664 put_task_struct(task);
2665 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002666 if (ret < 0)
2667 return ret;
2668 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002669}
2670
2671static const struct file_operations proc_coredump_filter_operations = {
2672 .read = proc_coredump_filter_read,
2673 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002674 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002675};
2676#endif
2677
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002678#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002679static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002680{
Andrea Righi940389b2008-07-28 00:48:12 +02002681 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002682 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002683 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002684
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002685 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2686 if (result)
2687 return result;
2688
Jann Horncaaee622016-01-20 15:00:04 -08002689 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002690 result = -EACCES;
2691 goto out_unlock;
2692 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002693
Andrea Righi59954772008-07-27 17:29:15 +02002694 if (whole && lock_task_sighand(task, &flags)) {
2695 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002696
Andrea Righi59954772008-07-27 17:29:15 +02002697 task_io_accounting_add(&acct, &task->signal->ioac);
2698 while_each_thread(task, t)
2699 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002700
Andrea Righi59954772008-07-27 17:29:15 +02002701 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002702 }
Joe Perches25ce3192015-04-15 16:18:17 -07002703 seq_printf(m,
2704 "rchar: %llu\n"
2705 "wchar: %llu\n"
2706 "syscr: %llu\n"
2707 "syscw: %llu\n"
2708 "read_bytes: %llu\n"
2709 "write_bytes: %llu\n"
2710 "cancelled_write_bytes: %llu\n",
2711 (unsigned long long)acct.rchar,
2712 (unsigned long long)acct.wchar,
2713 (unsigned long long)acct.syscr,
2714 (unsigned long long)acct.syscw,
2715 (unsigned long long)acct.read_bytes,
2716 (unsigned long long)acct.write_bytes,
2717 (unsigned long long)acct.cancelled_write_bytes);
2718 result = 0;
2719
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002720out_unlock:
2721 mutex_unlock(&task->signal->cred_guard_mutex);
2722 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002723}
Andrea Righi297c5d92008-07-25 01:48:49 -07002724
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002725static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2726 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002727{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002728 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002729}
2730
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002731static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2732 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002733{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002734 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002735}
2736#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002737
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002738#ifdef CONFIG_USER_NS
2739static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002740 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002741{
2742 struct user_namespace *ns = NULL;
2743 struct task_struct *task;
2744 struct seq_file *seq;
2745 int ret = -EINVAL;
2746
2747 task = get_proc_task(inode);
2748 if (task) {
2749 rcu_read_lock();
2750 ns = get_user_ns(task_cred_xxx(task, user_ns));
2751 rcu_read_unlock();
2752 put_task_struct(task);
2753 }
2754 if (!ns)
2755 goto err;
2756
2757 ret = seq_open(file, seq_ops);
2758 if (ret)
2759 goto err_put_ns;
2760
2761 seq = file->private_data;
2762 seq->private = ns;
2763
2764 return 0;
2765err_put_ns:
2766 put_user_ns(ns);
2767err:
2768 return ret;
2769}
2770
2771static int proc_id_map_release(struct inode *inode, struct file *file)
2772{
2773 struct seq_file *seq = file->private_data;
2774 struct user_namespace *ns = seq->private;
2775 put_user_ns(ns);
2776 return seq_release(inode, file);
2777}
2778
2779static int proc_uid_map_open(struct inode *inode, struct file *file)
2780{
2781 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2782}
2783
2784static int proc_gid_map_open(struct inode *inode, struct file *file)
2785{
2786 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2787}
2788
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002789static int proc_projid_map_open(struct inode *inode, struct file *file)
2790{
2791 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2792}
2793
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002794static const struct file_operations proc_uid_map_operations = {
2795 .open = proc_uid_map_open,
2796 .write = proc_uid_map_write,
2797 .read = seq_read,
2798 .llseek = seq_lseek,
2799 .release = proc_id_map_release,
2800};
2801
2802static const struct file_operations proc_gid_map_operations = {
2803 .open = proc_gid_map_open,
2804 .write = proc_gid_map_write,
2805 .read = seq_read,
2806 .llseek = seq_lseek,
2807 .release = proc_id_map_release,
2808};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002809
2810static const struct file_operations proc_projid_map_operations = {
2811 .open = proc_projid_map_open,
2812 .write = proc_projid_map_write,
2813 .read = seq_read,
2814 .llseek = seq_lseek,
2815 .release = proc_id_map_release,
2816};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002817
2818static int proc_setgroups_open(struct inode *inode, struct file *file)
2819{
2820 struct user_namespace *ns = NULL;
2821 struct task_struct *task;
2822 int ret;
2823
2824 ret = -ESRCH;
2825 task = get_proc_task(inode);
2826 if (task) {
2827 rcu_read_lock();
2828 ns = get_user_ns(task_cred_xxx(task, user_ns));
2829 rcu_read_unlock();
2830 put_task_struct(task);
2831 }
2832 if (!ns)
2833 goto err;
2834
2835 if (file->f_mode & FMODE_WRITE) {
2836 ret = -EACCES;
2837 if (!ns_capable(ns, CAP_SYS_ADMIN))
2838 goto err_put_ns;
2839 }
2840
2841 ret = single_open(file, &proc_setgroups_show, ns);
2842 if (ret)
2843 goto err_put_ns;
2844
2845 return 0;
2846err_put_ns:
2847 put_user_ns(ns);
2848err:
2849 return ret;
2850}
2851
2852static int proc_setgroups_release(struct inode *inode, struct file *file)
2853{
2854 struct seq_file *seq = file->private_data;
2855 struct user_namespace *ns = seq->private;
2856 int ret = single_release(inode, file);
2857 put_user_ns(ns);
2858 return ret;
2859}
2860
2861static const struct file_operations proc_setgroups_operations = {
2862 .open = proc_setgroups_open,
2863 .write = proc_setgroups_write,
2864 .read = seq_read,
2865 .llseek = seq_lseek,
2866 .release = proc_setgroups_release,
2867};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002868#endif /* CONFIG_USER_NS */
2869
Kees Cook47830722008-10-06 03:11:58 +04002870static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2871 struct pid *pid, struct task_struct *task)
2872{
Al Viroa9712bc2011-03-23 15:52:50 -04002873 int err = lock_trace(task);
2874 if (!err) {
2875 seq_printf(m, "%08x\n", task->personality);
2876 unlock_trace(task);
2877 }
2878 return err;
Kees Cook47830722008-10-06 03:11:58 +04002879}
2880
Eric W. Biederman801199c2006-10-02 02:18:48 -07002881/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002882 * Thread groups
2883 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002884static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002885static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002886
Eric Dumazetc5141e62007-05-08 00:26:15 -07002887static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002888 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2889 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002890 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002891 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002892 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002893#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002894 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002895#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002896 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002897 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002898 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002899 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002900 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002901#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002902 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002903#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002904#ifdef CONFIG_SCHED_AUTOGROUP
2905 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2906#endif
john stultz4614a696b2009-12-14 18:00:05 -08002907 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002908#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002909 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002910#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002911 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002912 ONE("stat", S_IRUGO, proc_tgid_stat),
2913 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002914 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002915#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002916 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002918 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2919 LNK("cwd", proc_cwd_link),
2920 LNK("root", proc_root_link),
2921 LNK("exe", proc_exe_link),
2922 REG("mounts", S_IRUGO, proc_mounts_operations),
2923 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2924 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002925#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002926 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002927 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002928 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002929#endif
2930#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002931 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002932#endif
2933#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002934 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002935#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002936#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002937 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002938#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302939#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002940 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002941#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002942#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002943 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002944#endif
Paul Menage8793d852007-10-18 23:39:39 -07002945#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002946 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002947#endif
Paul Menagea4243162007-10-18 23:39:35 -07002948#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002949 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002950#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002951 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002952 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002953 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002955 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2956 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002957#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002958#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002959 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002960#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002961#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002962 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002963#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002964#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002965 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002966#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002967#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002968 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002969#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002970#ifdef CONFIG_USER_NS
2971 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2972 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002973 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002974 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002975#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002976#ifdef CONFIG_CHECKPOINT_RESTORE
2977 REG("timers", S_IRUGO, proc_timers_operations),
2978#endif
John Stultz5de23d42016-03-17 14:20:54 -07002979 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002980};
2981
Al Virof0c3b502013-05-16 12:07:31 -04002982static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002983{
Al Virof0c3b502013-05-16 12:07:31 -04002984 return proc_pident_readdir(file, ctx,
2985 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002986}
2987
Arjan van de Ven00977a52007-02-12 00:55:34 -08002988static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002989 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002990 .iterate_shared = proc_tgid_base_readdir,
2991 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002992};
2993
Al Viro00cd8dd2012-06-10 17:13:09 -04002994static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2995{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002996 return proc_pident_lookup(dir, dentry,
2997 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002998}
2999
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003000static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003001 .lookup = proc_tgid_base_lookup,
3002 .getattr = pid_getattr,
3003 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003004 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003005};
3006
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003007static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003009 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003010 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003011 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Eric W. Biederman48e64842006-06-26 00:25:48 -07003013 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003014 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003015 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003016 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003017 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003018 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003019 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003022 if (pid == tgid)
3023 return;
3024
Eric W. Biederman48e64842006-06-26 00:25:48 -07003025 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003026 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3027 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003028 if (!leader)
3029 goto out;
3030
3031 name.name = "task";
3032 name.len = strlen(name.name);
3033 dir = d_hash_and_lookup(leader, &name);
3034 if (!dir)
3035 goto out_put_leader;
3036
3037 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003038 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003039 dentry = d_hash_and_lookup(dir, &name);
3040 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003041 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003042 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003044
3045 dput(dir);
3046out_put_leader:
3047 dput(leader);
3048out:
3049 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Randy Dunlap0895e912007-10-21 21:00:10 -07003052/**
3053 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3054 * @task: task that should be flushed.
3055 *
3056 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003057 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003058 * in. This call is supposed to do all of this job.
3059 *
3060 * Looks in the dcache for
3061 * /proc/@pid
3062 * /proc/@tgid/task/@pid
3063 * if either directory is present flushes it and all of it'ts children
3064 * from the dcache.
3065 *
3066 * It is safe and reasonable to cache /proc entries for a task until
3067 * that task exits. After that they just clog up the dcache with
3068 * useless entries, possibly causing useful dcache entries to be
3069 * flushed instead. This routine is proved to flush those useless
3070 * dcache entries at process exit time.
3071 *
3072 * NOTE: This routine is just an optimization so it does not guarantee
3073 * that no dcache entries will exist at process exit time it
3074 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003075 */
3076
3077void proc_flush_task(struct task_struct *task)
3078{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003079 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003080 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003081 struct upid *upid;
3082
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003083 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003084 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003085
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003086 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003087 upid = &pid->numbers[i];
3088 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003089 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003090 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003091}
3092
Al Viroc52a47a2013-06-15 11:15:20 +04003093static int proc_pid_instantiate(struct inode *dir,
3094 struct dentry * dentry,
3095 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003096{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003097 struct inode *inode;
3098
Eric W. Biederman61a28782006-10-02 02:18:49 -07003099 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003100 if (!inode)
3101 goto out;
3102
3103 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3104 inode->i_op = &proc_tgid_base_inode_operations;
3105 inode->i_fop = &proc_tgid_base_operations;
3106 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003107
Miklos Szeredibfe86842011-10-28 14:13:29 +02003108 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3109 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003110
Nick Pigginfb045ad2011-01-07 17:49:55 +11003111 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003112
3113 d_add(dentry, inode);
3114 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003115 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003116 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003117out:
Al Viroc52a47a2013-06-15 11:15:20 +04003118 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003119}
3120
Al Viro00cd8dd2012-06-10 17:13:09 -04003121struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003123 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003126 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003128 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (tgid == ~0U)
3130 goto out;
3131
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003132 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003133 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003134 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (task)
3136 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003137 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (!task)
3139 goto out;
3140
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003141 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003142 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143out:
Al Viroc52a47a2013-06-15 11:15:20 +04003144 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003148 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003149 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003151struct tgid_iter {
3152 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003153 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003154};
3155static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3156{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003157 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003159 if (iter.task)
3160 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003161 rcu_read_lock();
3162retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003163 iter.task = NULL;
3164 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003165 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003166 iter.tgid = pid_nr_ns(pid, ns);
3167 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003168 /* What we to know is if the pid we have find is the
3169 * pid of a thread_group_leader. Testing for task
3170 * being a thread_group_leader is the obvious thing
3171 * todo but there is a window when it fails, due to
3172 * the pid transfer logic in de_thread.
3173 *
3174 * So we perform the straight forward test of seeing
3175 * if the pid we have found is the pid of a thread
3176 * group leader, and don't worry if the task we have
3177 * found doesn't happen to be a thread group leader.
3178 * As we don't care in the case of readdir.
3179 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003180 if (!iter.task || !has_group_leader_pid(iter.task)) {
3181 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003182 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003183 }
3184 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003186 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003187 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
Eric W. Biederman00978752014-07-31 03:10:50 -07003190#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003193int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003195 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003196 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003197 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Al Viro021ada72013-03-29 19:27:05 -04003199 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Eric W. Biederman00978752014-07-31 03:10:50 -07003202 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003203 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003204 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003205 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003206 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003207 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003208 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003209 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003210 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3211 return 0;
3212 ctx->pos = pos = pos + 1;
3213 }
3214 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003215 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003216 for (iter = next_tgid(ns, iter);
3217 iter.task;
3218 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003219 char name[PROC_NUMBUF];
3220 int len;
Eric Dumazet9618fba2017-01-24 15:18:07 -08003221
3222 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -04003223 if (!has_pid_permissions(ns, iter.task, 2))
3224 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003225
Al Virof0c3b502013-05-16 12:07:31 -04003226 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3227 ctx->pos = iter.tgid + TGID_OFFSET;
3228 if (!proc_fill_cache(file, ctx, name, len,
3229 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003230 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
Al Virof0c3b502013-05-16 12:07:31 -04003234 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return 0;
3236}
3237
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003238/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003239 * proc_tid_comm_permission is a special permission function exclusively
3240 * used for the node /proc/<pid>/task/<tid>/comm.
3241 * It bypasses generic permission checks in the case where a task of the same
3242 * task group attempts to access the node.
3243 * The rationale behind this is that glibc and bionic access this node for
3244 * cross thread naming (pthread_set/getname_np(!self)). However, if
3245 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3246 * which locks out the cross thread naming implementation.
3247 * This function makes sure that the node is always accessible for members of
3248 * same thread group.
3249 */
3250static int proc_tid_comm_permission(struct inode *inode, int mask)
3251{
3252 bool is_same_tgroup;
3253 struct task_struct *task;
3254
3255 task = get_proc_task(inode);
3256 if (!task)
3257 return -ESRCH;
3258 is_same_tgroup = same_thread_group(current, task);
3259 put_task_struct(task);
3260
3261 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3262 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3263 * read or written by the members of the corresponding
3264 * thread group.
3265 */
3266 return 0;
3267 }
3268
3269 return generic_permission(inode, mask);
3270}
3271
3272static const struct inode_operations proc_tid_comm_inode_operations = {
3273 .permission = proc_tid_comm_permission,
3274};
3275
3276/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003277 * Tasks
3278 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003279static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003280 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003281 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003282 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003283#ifdef CONFIG_NET
3284 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3285#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003286 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003287 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003288 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003289 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003290 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003291#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003292 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003293#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003294 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3295 &proc_tid_comm_inode_operations,
3296 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003297#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003298 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003299#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003300 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003301 ONE("stat", S_IRUGO, proc_tid_stat),
3302 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003303 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003304#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003305 REG("children", S_IRUGO, proc_tid_children_operations),
3306#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003307#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003308 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003309#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003310 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3311 LNK("cwd", proc_cwd_link),
3312 LNK("root", proc_root_link),
3313 LNK("exe", proc_exe_link),
3314 REG("mounts", S_IRUGO, proc_mounts_operations),
3315 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003316#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003317 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003318 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003319 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003320#endif
3321#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003322 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003323#endif
3324#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003325 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003326#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003327#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003328 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003329#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303330#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003331 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003332#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003333#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003334 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003335#endif
Paul Menage8793d852007-10-18 23:39:39 -07003336#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003337 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003338#endif
Paul Menagea4243162007-10-18 23:39:35 -07003339#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003340 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003341#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003342 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003343 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003344 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003345#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003346 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003347 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003348#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003349#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003350 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003351#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003352#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003353 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003354#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003355#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003356 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003357#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003358#ifdef CONFIG_USER_NS
3359 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3360 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003361 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003362 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003363#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003364};
3365
Al Virof0c3b502013-05-16 12:07:31 -04003366static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003367{
Al Virof0c3b502013-05-16 12:07:31 -04003368 return proc_pident_readdir(file, ctx,
3369 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003370}
3371
Al Viro00cd8dd2012-06-10 17:13:09 -04003372static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3373{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003374 return proc_pident_lookup(dir, dentry,
3375 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003376}
3377
Arjan van de Ven00977a52007-02-12 00:55:34 -08003378static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003379 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003380 .iterate_shared = proc_tid_base_readdir,
3381 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003382};
3383
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003384static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003385 .lookup = proc_tid_base_lookup,
3386 .getattr = pid_getattr,
3387 .setattr = proc_setattr,
3388};
3389
Al Viroc52a47a2013-06-15 11:15:20 +04003390static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003391 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003392{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003393 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003394 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003395
3396 if (!inode)
3397 goto out;
3398 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3399 inode->i_op = &proc_tid_base_inode_operations;
3400 inode->i_fop = &proc_tid_base_operations;
3401 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003402
Miklos Szeredibfe86842011-10-28 14:13:29 +02003403 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3404 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003405
Nick Pigginfb045ad2011-01-07 17:49:55 +11003406 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003407
3408 d_add(dentry, inode);
3409 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003410 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003411 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003412out:
Al Viroc52a47a2013-06-15 11:15:20 +04003413 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003414}
3415
Al Viro00cd8dd2012-06-10 17:13:09 -04003416static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003417{
Al Viroc52a47a2013-06-15 11:15:20 +04003418 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003419 struct task_struct *task;
3420 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003421 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003422 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003423
3424 if (!leader)
3425 goto out_no_task;
3426
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003427 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003428 if (tid == ~0U)
3429 goto out;
3430
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003431 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003432 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003433 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003434 if (task)
3435 get_task_struct(task);
3436 rcu_read_unlock();
3437 if (!task)
3438 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003439 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003440 goto out_drop_task;
3441
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003442 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003443out_drop_task:
3444 put_task_struct(task);
3445out:
3446 put_task_struct(leader);
3447out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003448 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003449}
3450
3451/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003452 * Find the first tid of a thread group to return to user space.
3453 *
3454 * Usually this is just the thread group leader, but if the users
3455 * buffer was too small or there was a seek into the middle of the
3456 * directory we have more work todo.
3457 *
3458 * In the case of a short read we start with find_task_by_pid.
3459 *
3460 * In the case of a seek we start with the leader and walk nr
3461 * threads past it.
3462 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003463static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3464 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003465{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003466 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003467 unsigned long nr = f_pos;
3468
3469 if (nr != f_pos) /* 32bit overflow? */
3470 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003471
Eric W. Biedermancc288732006-06-26 00:26:01 -07003472 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003473 task = pid_task(pid, PIDTYPE_PID);
3474 if (!task)
3475 goto fail;
3476
3477 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003478 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003479 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003480 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003481 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003482 }
3483
3484 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003485 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003486 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003487
3488 /* If we haven't found our starting place yet start
3489 * with the leader and walk nr threads forward.
3490 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003491 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003492 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003493 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003494 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003495 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003496fail:
3497 pos = NULL;
3498 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003499found:
3500 get_task_struct(pos);
3501out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003502 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003503 return pos;
3504}
3505
3506/*
3507 * Find the next thread in the thread list.
3508 * Return NULL if there is an error or no next thread.
3509 *
3510 * The reference to the input task_struct is released.
3511 */
3512static struct task_struct *next_tid(struct task_struct *start)
3513{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003514 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003515 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003516 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003517 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003518 if (thread_group_leader(pos))
3519 pos = NULL;
3520 else
3521 get_task_struct(pos);
3522 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003523 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003524 put_task_struct(start);
3525 return pos;
3526}
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003529static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003531 struct inode *inode = file_inode(file);
3532 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003533 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003534 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003536 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003537 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Al Virof0c3b502013-05-16 12:07:31 -04003539 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003542 /* f_version caches the tgid value that the last readdir call couldn't
3543 * return. lseek aka telldir automagically resets f_version to 0.
3544 */
Al Viro3aa33772014-10-31 00:42:35 -04003545 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003546 tid = (int)file->f_version;
3547 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003548 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003549 task;
Al Virof0c3b502013-05-16 12:07:31 -04003550 task = next_tid(task), ctx->pos++) {
3551 char name[PROC_NUMBUF];
3552 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003553 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003554 len = snprintf(name, sizeof(name), "%d", tid);
3555 if (!proc_fill_cache(file, ctx, name, len,
3556 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003557 /* returning this tgid failed, save it as the first
3558 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003559 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003560 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003564
Al Virof0c3b502013-05-16 12:07:31 -04003565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003567
3568static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3569{
David Howells2b0143b2015-03-17 22:25:59 +00003570 struct inode *inode = d_inode(dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003571 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003572 generic_fillattr(inode, stat);
3573
Eric W. Biederman99f89552006-06-26 00:25:55 -07003574 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003575 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003576 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003577 }
3578
3579 return 0;
3580}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003581
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003582static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003583 .lookup = proc_task_lookup,
3584 .getattr = proc_task_getattr,
3585 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003586 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003587};
3588
Arjan van de Ven00977a52007-02-12 00:55:34 -08003589static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003590 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003591 .iterate_shared = proc_task_readdir,
3592 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003593};